Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## Unreleased

- **Person gate hardened (guard against agent mistakes, not a same-user defence).** The token store is read from the OS account home only (`HOME`, `USERPROFILE`, `HUNCH_HOME` ignored); `HUNCH_PERSON_TOKEN` is removed from the environment after it is read; `derive-rules --accept-changes`/`--drop` and `review --reject-duplicates`/`--approve-import`/`--decline-import` need a person; `new constraint`, `derive-rules` and `capture-comments` without a person write Drafts with `agent_recorded` provenance; re-challenging keeps the first challenge; `hunch ui` strips the token and shows the CLI command on exit 4.
- **Four more person-gate bypasses closed.** MCP `hunch_review_imported_adr` now needs the human's confirmation to decline as well as approve (otherwise refused, with `hunch review --decline-import <id>`). `hunch test` promoted guards and `hunch review-memory capture --apply` (including automatic review memory) are written as `draft` unless a person token is supplied. MCP `hunch_record_correction` refuses to re-record an existing active rule (use `hunch_challenge`). `hunch record-constraint` over a draft or retired rule now says it kept that status.
- **MCP `supersedes` challenges the old rule instead of retiring it.** `hunch_record_correction` with `supersedes` opens a challenge (reason "superseded by <new id>") on each target and logs it; the old rule stays enforced until a person runs `hunch replace`. An agent never retires a rule.
- **Re-recording a retired rule through MCP yields a Draft.** The rule comes back as `draft` with `valid_to` cleared and no `retired_from`; a person approves it with `hunch review --confirm <id>`. A human-confirmed rule is still refused.
- **Retiring a rule remembers its status.** `hunch remove`, `retire-constraint` and `replace` share one helper that records `retired_from`, so `hunch restore` returns a Draft to Draft and an Active rule to Active.
- **`nuryel_write` cannot change a rule's lifecycle.** On the constraints facet an existing rule keeps its status, challenge, `retired_from` and source fields; a new rule is always a Draft.
- **Person-only rule actions are enforced by the CLI; see docs/person-actions.md.**
- **Rules have a lifecycle: Draft, Active, Challenged, Retired.** A person approves a Draft with `hunch review --confirm <id>`; Draft rules warn but never block. Any agent can challenge a rule (`hunch_challenge` over MCP, or `hunch challenge <id> --reason`); a challenged rule keeps blocking until a person clears it (`hunch challenge clear <id> --reason`), replaces it (`hunch replace <old> --with <new> --reason`) or retires it (`hunch remove <id> --reason`). Rules link to their `source_session` (hook `session_id`) and `source_run` (`HUNCH_RUN_ID`). Rule checks are logged to the git-ignored `.hunch/local/rule-checks.jsonl`, which never leaves the machine and holds no code or prompt contents. New: `hunch rules --json`, `hunch rule show <id> --json`. See [docs/rule-lifecycle.md](docs/rule-lifecycle.md).
- **Agent-recorded corrections start as Drafts, which warn but never block.** `hunch_record_correction` now writes status `draft`; the rule warns on matching edits and never blocks (also under `hunch check --strict`) until a person runs `hunch review --confirm <id>`. The first write of a Draft raises the manifest to schema v4. An older build warns that the store is newer and skips the v4-only records it cannot parse. Mixed-version teams should upgrade together.
- **Public data-format change: schema v4.** The rule `status` enum gains `"draft"`, rules gain lifecycle fields, and `schema_version` becomes 4. The migration keeps every existing rule Active. Code that reads `.hunch/` JSON and switches on `status` must accept `"draft"`.

## 1.60.0 — 2026-11-23

- **Antigravity's Stop hook gets 30 s instead of 15 s.** Hunch wires no post-tool event for Antigravity, so its Stop gate judges every file that differs from HEAD: the changed-file list, then the Veto and the invariant check, each with its own git budget. On slow git that can pass 15 s, and Antigravity then killed the hook, which gave no verdict and lost its Stop bookkeeping. `hunch init` now writes `timeout: 30` for the Stop handler, Antigravity's documented default; PreInvocation and PreToolUse keep 15 s. `hunch update` does not rewrite hook entries: an existing install picks this up by re-running `hunch init`, which replaces only Hunch's own entries. (#688)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ Profiles retain their revision, sources, confidence, and freshness.

- [Full documentation](https://www.hunchmemory.com/docs)
- [Copy-paste cookbook](https://www.hunchmemory.com/cookbook)
- [Rule lifecycle](docs/rule-lifecycle.md): draft, active, challenged and retired rules
- [Changelog](CHANGELOG.md)
- [VS Code extension](vscode-extension/README.md)
- [Contributing](CONTRIBUTING.md)
Expand Down
52 changes: 52 additions & 0 deletions docs/person-actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Person-only actions

Some rule actions are decisions a person makes. The `hunch` CLI enforces this itself, in `src/core/personGate.ts`, before it writes anything.

## Which commands need a person

- `hunch review --confirm <id>` and `hunch review --unconfirm <id>` (approve a draft, countersign a record, take it back)
- `hunch review --accept <id>`, `--accept-verified` and `--reject <id>`, and `hunch derive-rules --accept <id> --expect <hash>` (approve or reject a draft or queued proposal)
- `hunch challenge clear <id>`
- `hunch replace <old> --with <new>`
- `hunch retire-constraint <id>`
- `hunch remove`, `hunch restore`, `hunch edit` and `hunch link` when the target is a constraint (`con_*`). The CLI decides by resolving the record, not by how the id is spelled.
- `hunch record-constraint` when a rule with the same id already exists. Re-recording keeps the existing status, challenge, `retired_from` and source session/run. When no such rule exists and no person is verified, the rule is created as a `draft` with `agent_recorded` provenance; a person approves it with `hunch review --confirm <id>`.
- `hunch derive-rules --accept-changes` (gated per changed rule, before any write), `hunch derive-rules --drop`, `hunch review --reject-duplicates`, `--approve-import` and `--decline-import` need a person.
- `hunch new constraint`, `hunch derive-rules` and `hunch capture-comments` without a person write only a Draft with `agent_recorded` provenance; they never stamp `human_confirmed`. `hunch challenge <id>` on an already challenged rule is refused and keeps the first challenge.
- (older note) `hunch derive-rules --accept-changes` and `hunch capture-comments` rewrite rules from docs and comments. They keep the existing rule's status, challenge, `retired_from` and source session/run.
- MCP `hunch_review_imported_adr` needs the human's confirmation in the client prompt for `decline` as well as `approve`. Without it nothing is written and the agent is told to have the person run `hunch review --decline-import <id>`.
- `hunch test` (a promoted regression guard) and `hunch review-memory capture --apply` (and the automatic review-memory capture) write a `draft` unless a valid `HUNCH_PERSON_TOKEN` is supplied. They never prompt. A draft warns but never blocks, whatever its severity; a person approves it with `hunch review --confirm <id>`.
- MCP `hunch_record_correction` refuses to re-record an existing active (or challenged) rule: use `hunch_challenge`. A draft can still be re-recorded; a retired rule returns as a draft.
- `hunch record-constraint` by a person over an existing draft or retired rule keeps that status and now says so ("kept as draft: approve with `hunch review --confirm <id>`" or "kept retired").

Challenging a rule (`hunch challenge <id>`) is open to agents: it only flags a rule for review.

## Two ways to prove you are a person

1. **Terminal confirmation.** In an interactive terminal (stdin and stdout are both a TTY) the CLI asks you to type the rule id: `Type con_x to confirm: <action>`. Anything else refuses.
2. **Person token.** A trusted tool, such as the Nuryel control plane, sends the token in the `HUNCH_PERSON_TOKEN` environment variable. The CLI compares its sha256 with the hash in `~/.hunch/person-token.json` (constant-time). If `HUNCH_PERSON_TOKEN` is set and the check fails for any reason (no file, symlink, not a regular file, unreadable, wrong token) the action is refused and the CLI does not fall back to a terminal prompt.

## Creating the token

Run once, in your own terminal:

```
hunch person-token init # first time
hunch person-token init --rotate # replace it; the old token stops working
```

The command refuses without a terminal. It prints the token once; store it as Nuryel's `HUNCH_PERSON_TOKEN`. Only the hash is stored, in `~/.hunch/person-token.json` (`{"v":1,"sha256":"...","created":"..."}`, mode 0600, written atomically). It refuses to write through a symlinked file or `.hunch` directory. The store is read from the operating-system account home only (`os.userInfo().homedir`); `HOME`, `USERPROFILE` and `HUNCH_HOME` are ignored, so an agent cannot point the CLI at a token store it wrote itself. The CLI removes `HUNCH_PERSON_TOKEN` from its own environment at startup, so the programs it spawns never inherit it.

## Exit code 4

A refused person-only action exits with code **4** ("person required") and prints why on stderr. Nothing is changed: the check runs before any store write.

## Threat model

The gate is a guard against an agent acting by mistake with normal tools. It is not a security boundary against a determined program running as the same user. This stops agents acting through normal tools: an agent's shell has no terminal and no token, so the CLI refuses. It is not a defence against a deliberately hostile process running as the same user, which can read the token file's directory, edit the store files directly, or run its own code.

## The shell guard is advisory

The PreToolUse shell guard (`src/core/lifecycleShellGuard.ts`) still refuses these commands typed from an agent's shell, so the agent gets a clear message early. It matches command text and can be bypassed by scripts or indirection. The CLI gate above is the enforcement.

The UI (`hunch ui`) never holds the token: its spawned CLI runs without `HUNCH_PERSON_TOKEN`, so a person-only action from the page is refused (exit 4) and the page shows the exact command to run in your own terminal.
78 changes: 78 additions & 0 deletions docs/rule-lifecycle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Rule lifecycle

A rule (constraint) has a life cycle: an agent can propose it, a person approves it, anyone can challenge it, and it can be replaced or retired. This page describes the states, the commands, the data shapes and the local check log.

## States

```
Draft -> Active -> (Challenged) -> Retired
```

| State | Meaning | Effect in hooks |
| --- | --- | --- |
| Draft | Proposed, not yet approved by a person. | Warns, never blocks. |
| Active | Approved by a person. | Enforced as written (blocking, warning or advisory). |
| Challenged | An agent or person disputes it; it stays Active underneath. | Keeps blocking until a person clears or retires it. |
| Retired | Removed or replaced. | Not enforced. Kept for history. |

A challenge cannot lift a block. Only a person can end it.

## Commands

| Action | Who | Command |
| --- | --- | --- |
| Approve a Draft | person | `hunch review --confirm <id>` |
| Challenge | any agent or person | MCP `hunch_challenge`, or `hunch challenge <id> --reason "<why>"` |
| Clear a challenge | person only | `hunch challenge clear <id> --reason "<why>"` |
| Replace | person | `hunch replace <old> --with <new> --reason "<why>"` |
| Retire | person | `hunch remove <id> --reason "<why>"` |
| List rules | anyone | `hunch rules [--json]` |
| Show one rule | anyone | `hunch rule show <id> [--json]` |

A reason is recorded with every challenge, clear, replace and retire.

### Who may do what

The person gate is a guard against agents acting by mistake with normal tools, not a defence against a determined process running as the same user. Newly gated: `derive-rules --accept-changes`, `--drop`, `review --reject-duplicates`, `--approve-import`, `--decline-import`. Without a person, `new constraint`, `derive-rules` and `capture-comments` write only Drafts with `agent_recorded` provenance. Each rule records `source_session` and `source_run` in the rule log. An existing challenge is kept: challenging again is refused.

Also gated: MCP `hunch_review_imported_adr` (decline needs the human's confirmation like approve), `hunch test` promotion and `review-memory capture --apply` (drafts without a person token), and MCP `hunch_record_correction` over an existing active rule (refused; use `hunch_challenge`).

An agent has exactly one lifecycle tool over MCP: `hunch_challenge`. No MCP tool clears a challenge, approves a Draft, retires a rule or replaces one. `hunch_record_correction` with `supersedes` does not retire the old rule either: it opens a challenge on each superseded rule (reason "superseded by <new id>", logged like `hunch_challenge`) and the old rule stays enforced until a person runs `hunch replace`. An agent that re-records a retired rule gets a Draft back, never an Active rule. `nuryel_write` on the constraints facet keeps an existing rule's status, challenge and source fields, and creates a new rule as a Draft. The CLI enforces the person-only commands itself: `hunch review --confirm`, `--unconfirm`, `--accept`, `--accept-verified` and `--reject`, `hunch challenge clear`, `hunch replace`, `hunch retire-constraint`, `hunch derive-rules --accept`, `hunch record-constraint`, and `hunch remove`, `restore`, `edit` and `link` on a rule. Each one asks a person to type the rule id in a terminal, or accepts a registered person token (`HUNCH_PERSON_TOKEN`, set up with `hunch person-token init`; the Nuryel control plane sends it on a person's click). With neither it prints "Only a person can ..." and exits 4 before writing anything. A rule recorded with `hunch record-constraint` without a person becomes a Draft with `agent_recorded` provenance, and an existing rule keeps its status and challenge. Setup, the full command list and the limits of this check are in [person-actions.md](person-actions.md). The hook's PreToolUse Bash guard still denies these commands by command text as an early warning; it is best-effort (a script can bypass it), and the CLI check is the enforcement. Restoring a removed rule returns it to the state it had before removal, so a Draft never becomes Active without a person.

## Source linking

A rule records where it came from: `source_session` is the hook `session_id` of the agent session that proposed it, and `source_run` comes from the `HUNCH_RUN_ID` environment variable when the agent was started by a runner. Both are optional and are identifiers only.

## Local rule-check log

Each rule check appends one line to `.hunch/local/rule-checks.jsonl`. The file is git-ignored and never leaves the machine: it is not synced, exported or sent to any server. Each line is a JSON object with `kind` (`check`, `challenge` or `challenge_clear`), `at`, `rule_id`, `verdict` (allowed, warned, blocked; a denied edit logs blocked for every enforced rule in scope), `status` (the rule's state at the time), `file` (repo-relative path), `tool` (the agent tool that triggered the check), `session_id` and `run_id`. A `challenge` or `challenge_clear` line carries `reason` instead of `verdict`; both the MCP tool and the CLI (`hunch challenge`, `hunch challenge clear`) write it. At 4 MiB the log is renamed to `rule-checks.jsonl.1` (replacing any older one) and a new file starts. A symlinked `.hunch`, `.hunch/local` or log file is refused. It never holds code, file contents or prompts.

## JSON shapes

`hunch rules --json` prints an array of rule summaries:

```json
[{ "id": "con_...", "statement": "...", "severity": "blocking", "scope": ["src/**"], "status": "active", "challenge": null, "source_decision": null, "source_session": null, "source_run": null, "why": null, "superseded_by": null, "valid_from": "...", "valid_to": null }]
```

`status` is the display state: `draft`, `active`, `challenged` or `retired`. `challenge` is one object (`reason`, `by_session`, `by_run`, `at`) or `null`; there is no history. `superseded_by` is read from the retired rule's rationale text ("Superseded by con_..."), not from a separate link field. `hunch rule show <id> --json` prints the same fields plus `why_detail` (the source decision's `id`, `title`, `rationale` and `rejected` options) or `null`. Treat the exact field list as that of the schema version you run; unknown fields should be ignored by readers.

`hunch review --json` `rules[]` and `hunch why <file> --json` `constraints[]` use a smaller rule reference shape, not the full `hunch rules --json` item: `id`, `statement`, `severity`, `scope`, `status` (the stored status), `displayState` (`draft`, `active`, `challenged` or `retired`), `challenge` and `source_decision`; `why` adds `stale`. Read `rules --json` for `source_session`, `source_run`, `why` and `superseded_by`.

`hunch review --json` prints the review queue:

```json
{ "minGrounded": 0.5, "imported": [DECISION], "ready": [DECISION + { "grounded": 0, "pruned": 0, "critic": null }], "scrutiny": [same as ready], "rules": [RULE] }
```

where DECISION is `{ "id", "title", "decision", "status", "source", "confidence" }` and RULE is a rule summary as in `hunch rules --json`.

`hunch why <target> --json` prints one of three shapes. By record id: `{ "target", "kind", "record", "displayState"? }` (`displayState` only for a rule), or `{ "target", "record": null }` when nothing matches. By file or symbol: `{ "target", "decisions": [DECISION + { "stale" }], "constraints": [RULE + { "stale" }], "bugs": [{ "id", "title", "status", "root_cause" }], "symbols": [{ "name", "fan_in", "churn_90d" }] }`.

## Data format: schema v4

- The `status` enum gains `"draft"`.
- Rules gain the lifecycle fields: `source_session`, `source_run` and `challenge`. Retirement and replacement stay in `valid_to` and the rationale text.
- `schema_version` becomes 4. `hunch migrate` migrates in place.
- Existing rules stay Active. Nothing existing becomes Draft or starts warning instead of blocking.
- Readers of `.hunch/` JSON that switch on `status` must accept `"draft"`. The first write of a Draft, a challenge or a session/run link raises a v3 manifest to v4, so an older build warns that the store is newer and skips the v4-only records it cannot parse. (A manifest older than v3 is left alone until `hunch migrate`, so no earlier migration is skipped.) Agent-recorded corrections are now written as Drafts: a team mixing older and newer builds must upgrade together.
2 changes: 1 addition & 1 deletion src/cli/automaticReviewMemory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export function registerAutomaticReviewMemory(command: Command,
}
}
report.rules = report.rules.filter(r => report.entries.some(e => e.status === "ready" && e.rule_id === r.id));
if (report.rules.length) capture(report.rules, packet.repository, !!opts.private);
if (report.rules.length) report.rules = capture(report.rules, packet.repository, !!opts.private) ?? report.rules;
for (const entry of report.entries) if (entry.status === "ready") entry.status = "saved";
}
report.applied = !opts.dryRun;
Expand Down
Loading
Loading