graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Release v0.302.41
justrach94027d422154
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex d3f714d4..7c978fcc 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,6 +10,14 @@ The release workflow uses a tag's section here as its release notes (ahand-written `docs/releases/<tag>.md` wins if present), so keeping this filecurrent is part of cutting a release.+## v0.302.41++- Adds native subscription browser sign-in with shared CLI, REPL and ACP credential recovery; metered-key access remains separate.+- Preserves independent MCP structured results, displays resource links and embedded text, and answers inbound pings during initialization and tool waits.+- Gives loopback inference request-local deadline allowances without extending unrelated remote requests.+- Adds repeatable isolated native-CLI fixture comparisons with explicit routing checks and separately labeled task subsets.+- Retains all changes from v0.302.40 and current main.+## v0.302.40- Integrates the pending stream recovery, claim inventory, workspace skills, stored conversation, MCP configuration, companion update and typed ACP sign-in recovery changes.diff --git a/README.md b/README.mdindex e7c5ab26..067bdb10 100644--- a/README.md+++ b/README.md@@ -62,14 +62,20 @@ the newest beta. Both check the download against the release's `SHA256SUMS`.```shgraff login chatgpt # ChatGPT sign-in (same as: graff login codex)+graff login anthropic # Claude subscription browser sign-in (alias: ant)graff login kimigraff login zaigraff login xai-graff login anthropic # Anthropic Console account (API credits), via Anthropic's `ant` CLI+graff login console # Anthropic Console account (API credits), via Anthropic's `ant` CLIgraff key set deepseek sk-... # any provider by API keygraff --model grok-4.6```+Anthropic browser login uses native OAuth, not the Claude CLI or its credential+files. It requires an eligible account and upstream permission for third-party+subscription access; support can change. `ANTHROPIC_API_KEY` remains the separate+metered route. See [Anthropic sign-in](docs/anthropic-login.md).+From a checkout: `./install.sh` puts the binary in `~/bin`(`HARNESS_NO_PATH=1` skips PATH edits).@@ -168,7 +174,7 @@ sandbox MCP.```graff [flags] interactive sessiongraff -p "prompt" one question (answer on stdout)-graff login [codegraff|chatgpt|kimi|xai|zai] (codex = chatgpt)+graff login [codegraff|chatgpt|anthropic|kimi|xai|zai] (codex = chatgpt)graff key set <provider> <key>graff mcp add <name | url | @scope/pkg | uvx:pkg | json>graff mcp add <name> -- <cmd>diff --git a/docs/adr/0286-mcp-run-task-takes-caller-context.md b/docs/adr/0286-mcp-run-task-takes-caller-context.mdnew file mode 100644index 00000000..df8c04d2--- /dev/null+++ b/docs/adr/0286-mcp-run-task-takes-caller-context.md@@ -0,0 +1,90 @@+# 0286. MCP run_task takes caller context, summarized into text++Status: accepted 2026-10-05++## Context++`graff mcp serve` exposes one tool, `run_task`, which spawns the current+executable as a fresh bounded one-shot (ADR 0129). The only input was `prompt`,+so a caller that already held the relevant background had to either re-derive it+inside the child or paste it into the brief. Callers asked for a channel to hand+context over.++The gateway's `/responses/compact` endpoint cannot serve that channel. It+returns opaque encrypted state that is meaningful only by continuing the+conversation it compacted, so it cannot seed a child that starts with+`--no-resume`. It is also provider-scoped: standalone only for the codegraff+provider on an OpenAI GPT-family model, in-stream-only for codex and+`chatgpt-new`, and absent everywhere else, including xAI (ADR 0002).++## Decision++`run_task` gains one optional argument, `context`: nonblank UTF-8, at most+32 KiB, carrying background the child cannot re-derive.++- At or below 8 KiB, `context` is appended to the child's system prompt+ verbatim via `--append-system-prompt`.+- Above 8 KiB, the server first runs a summarizer child and appends its text+ instead. The summarizer runs `--no-resume --safe --max-model-calls 1+ --max-tool-calls 0` on a 60-second deadline: summarizing is not a reason to+ touch the workspace or to spend the task's own budget.+- If summarization fails, the task is not run and the tool returns an error+ naming the failure. Context is never silently dropped or truncated.+- `structuredContent` reports `context_chars`, `context_summarized` and+ `context_used_chars`, and mirrors the same object under+ `_meta["codegraff/runTask"]` so a raw-result reader needs no prose.+- The same object crosses the ACP boundary: `mcp_call` reads the MCP result's+ mirror, the engine carries it on the `tool_result` event, and the completed+ `tool_call_update` carries it as `_meta["codegraff/runTask"]` beside any+ `graff/view`. Only the five fields cross — the task's text and the caller's+ context never travel with them, and a result that reported nothing gains no+ `_meta` at all.+- Those same facts cross into ACP: the engine carries them on the tool+ outcome (`engine_events.ToolOutcome.run_task`), the `--json` `tool_result`+ line gains a `run_task` object only when one exists, and a completed+ `tool_call_update` carries `_meta["codegraff/runTask"]` alongside any+ `_meta["graff/view"]`. The task's text and the caller's context do not+ travel in that metadata.+- Per-task usage and charge for the CHILD's own execution are **not+ reported**. `usage_reported` is always `false` and+ `usage_unavailable_reason` names the cause, so a client can tell "no+ settlement exists" apart from "the field was dropped". Account-wide gateway+ usage remains authoritative for what the child cost.+- Preprocessing **is** reported, separately, because it is a real settled+ charge: `preprocess` carries `cost_usd`, `usage_complete`, `input_tokens`,+ `output_tokens` and `unavailable_reason`. The summarizer runs as a `--json`+ child (print mode reports no settled charge) with its single request line on+ a file-backed stdin, so the engine's own exact-charge ingestion supplies the+ number. An incomplete report — `usage_complete: false`, or any unpriced or+ missing usage — yields `cost_usd: null` and a reason; a list-rate estimate is+ never presented as a settlement. Preprocessing is never folded into the+ child's total, so it cannot be double-counted.++`context` carries text only. cwd, credentials, model, permission mode and CLI+flags still come from the server launch, as ADR 0129 requires.++## Consequences++Children stay fresh and isolated, and the permission and configuration+boundaries of ADR 0129 are unchanged — `context` adds text, not authority.++Summarizing costs one extra model call per over-long handover. It runs under the+caller's own credentials, so it bills to the same account.++Exact per-task charge reporting is **not implemented**, and the result says so+rather than leaving it to be inferred. A `-p` child exposes no settled gateway+charge, and `pricing_settled.zig` forbids deriving one from a list rate; so+`usage_reported` is `false` with a stated reason. Producing a real number needs+the child driven under the JSON protocol to capture the `turn` event's+`cost_usd`, which the process runner cannot yet do — it hardcodes `stdin =+.ignore` and a JSON child does not exit on its own. Until then, account-wide+gateway usage is the authoritative figure and any per-task total would be a+second, double-counted estimate.++An over-long handover also costs a second child process and up to 60 seconds+before the task starts, against `timeout_seconds`, which is unaffected.++The 8 KiB threshold is a policy number, not a measurement. If handovers are+routinely summarized that would have fit, or routinely passed through that+crowd out the task, that number is the thing to revisit. Streaming, session+continuation and cancellation remain out of scope (ADR 0129).\ No newline at end of filediff --git a/docs/adr/0287-loopback-inference-has-request-local-deadlines.md b/docs/adr/0287-loopback-inference-has-request-local-deadlines.mdnew file mode 100644index 00000000..da9ed92e--- /dev/null+++ b/docs/adr/0287-loopback-inference-has-request-local-deadlines.md@@ -0,0 +1,29 @@+# 0287. Loopback inference has request-local deadlines++Status: accepted 2026-10-17++## Context++Local inference can spend longer on prefill or generation than a managed+endpoint. A short first-frame or HTTP-header watchdog ends a healthy local+turn before any response arrives (#1643). Increasing shared watchdog globals+would also slow recovery for unrelated remote requests.++## Decision++Detect loopback hosts from the parsed endpoint URL, not the model name or a+hostname substring. Give their default streaming silence, HTTP-header wait,+and nonstreaming request a ten-minute allowance. Streaming prose still uses+the reconnect ladder, starting at a quarter of that allowance.++Keep explicitly configured stream, first-frame and nonstreaming deadlines,+and shortened embedded/test deadlines. Compute each allowance per request;+never change process-wide budgets. Apply this in the shared engine HTTP/1.1,+HTTP/2 and nonstreaming paths, so REPL and ACP receive the same behavior.++## Consequences++An unresponsive loopback server can take longer to time out; cancellation+remains active throughout. Remote endpoints retain their existing recovery+budgets. Private-network addresses are not assumed local: callers can set the+existing deadline overrides for those endpoints.diff --git a/docs/adr/0288-anthropic-native-login-is-source-aware.md b/docs/adr/0288-anthropic-native-login-is-source-aware.mdnew file mode 100644index 00000000..4307c22d--- /dev/null+++ b/docs/adr/0288-anthropic-native-login-is-source-aware.md@@ -0,0 +1,40 @@+# 0288 — Native Anthropic login is source-aware++Date: 2026-10-17++## Decision++`graff login anthropic` and `graff login ant` use browser PKCE OAuth, storing+only Graff-owned credentials. The REPL command, model picker, startup and ACP+all use the same loader and bounded authentication-recovery path. A separate+Claude CLI transport must be explicitly selected; native login never silently+launches another agent or imports its credentials.++This supersedes ADR 0281's default login command. Existing Console-profile+credential loading remains supported alongside the native subscription store.++## Why++The two reference approaches are not interchangeable. Pi's native adapter+implements authorization and refresh, then sends Messages requests itself.+The DirectSDK extension instead delegates inference to an installed Claude+CLI, with request admission and tool-execution boundaries around that child.+Calling either one merely "Anthropic login" would hide a substantial execution+and credential-policy change.++An OAuth token also cannot replace an API key without changing the wire:+subscription requests need Bearer authorization, subscription beta headers,+a compatible system preamble and tool naming. These adaptations are gated on+both the first-party provider and the credential source. Key-based requests+and other vendors using the Anthropic message format remain unchanged.++## Consequences++- Model pins retain OAuth authentication; hot login updates the live provider.+- Catalog discovery uses the same credential class as inference.+- Subscription priority and the existing announced metered-key handoff remain;+ a handoff also restores API-key headers and ordinary request formatting.+- Credentials refresh serially with atomic private persistence. Token responses+ never appear in diagnostics. Offline protocol tests use synthetic credentials.+- Native protocol support does not establish upstream approval or account+ eligibility. Live browser login and inference must be verified separately.diff --git a/docs/adr/0289-mcp-results-preserve-typed-and-display-content.md b/docs/adr/0289-mcp-results-preserve-typed-and-display-content.mdnew file mode 100644index 00000000..98823c52--- /dev/null+++ b/docs/adr/0289-mcp-results-preserve-typed-and-display-content.md@@ -0,0 +1,40 @@+# 0289: Preserve MCP typed results independently of display content++Status: accepted++## Context++MCP tool results may contain a human-readable summary, images or resources,+and independent `structuredContent`. Treating structured data as an empty-text+fallback silently loses fields. Resource links and embedded resources also need+visible representations rather than an empty result. A server may send a ping+while the client is waiting for initialization or a tool response.++## Decision++The shared MCP client preserves both representations. Ordinary tool output keeps+text, image staging, resource references and embedded text, followed by structured+JSON when it is not already a mirrored JSON fallback. RLM bindings consume the+complete structured JSON when supplied; display summaries do not replace it.+The typed slice borrows from the final owned output, never the response arena.+Shape learning uses the typed result too.++Resource URIs are displayed, not fetched automatically. Unsupported binary/audio+payloads receive explicit notes without dumping encoded data. Request-scoped+stdio readers answer valid inbound pings with an empty result and the same ID,+including during initialization. Existing elicitation, cancellation, matching+responses, and no-replay rules remain intact.++## Consequences++The common registry/execution path gives REPL and ACP clients the same behavior.+No client-only UI change or model prompt change is needed. This does not add+full output-schema validation, audio playback, or an idle background read pump.+Protocol interoperability fixes do not imply a reduction in model generation+latency; that must be measured separately.++## References++- MCP specification, `2026-07-28/server/tools.mdx`: structured and resource content.+- MCP specification, `2025-11-25/basic/utilities/ping.mdx`: bidirectional ping.+- ADR 0029: MCP inside RLM and return shapes.diff --git a/docs/adr/README.md b/docs/adr/README.mdindex e2be5d7a..29c9394a 100644--- a/docs/adr/README.md+++ b/docs/adr/README.md@@ -300,6 +300,11 @@ record only when you need the evidence or the edge cases.| [0285](0285-acp-reauth-is-route-specific.md) | Terminal ChatGPT auth rejection carries fixed route-specific recovery metadata: ACP v1 `-32000` + `error.data`, v2 failed idle `_meta["graff/reauth"]`; `graff login chatgpt-new` repairs the new store, `codex login` the legacy one. Retry and non-auth error behavior stay unchanged. || [0252](0252-hosted-compaction-with-a-codegraff-login.md) | With a Codegraff login and `GRAFF_CLEF_COMPACT=1` (default-off per 0261), `compact()` asks the gateway first: per-call keep/trim/drop decisions applied to graff's own history by call id, plus a spliced summary when pruning frees too little. Live-turn calls stay; a reply must check out and free a tenth of the context. Refused key or exhausted credits: local for the session; other failures: local for that compaction. |+| [0286](0286-mcp-run-task-takes-caller-context.md) | `run_task` accepts bounded text context and preserves task metadata across the shared engine and ACP. |+| [0287](0287-loopback-inference-has-request-local-deadlines.md) | Loopback inference receives request-local default deadlines; explicit limits and remote recovery budgets remain unchanged. |+| [0288](0288-anthropic-native-login-is-source-aware.md) | Native subscription PKCE login is shared across CLI, REPL and ACP; subscription wire compatibility stays credential-source-aware. |+| [0289](0289-mcp-results-preserve-typed-and-display-content.md) | Preserve typed MCP results independently of display content, show resource references without fetching them and answer pings during shared waits. |+## When to write oneWrite an ADR when a decision is load-bearing and non-obvious: it was reacheddiff --git a/docs/anthropic-login.md b/docs/anthropic-login.mdnew file mode 100644index 00000000..2325f144--- /dev/null+++ b/docs/anthropic-login.md@@ -0,0 +1,56 @@+# Anthropic browser sign-in++`graff login anthropic` (alias `graff login ant`) signs in with native PKCE+OAuth. `/login anthropic` and `/login ant` do the same in a running session.+ACP advertises a terminal sign-in method that runs the same command; its+engine reads and refreshes the same credential as the REPL.+`graff login anthropic --refresh` explicitly refreshes an existing Graff login+without opening the browser. Browser sign-in needs the local loopback callback+on port 53692 to be free; it never cancels another application's listener.++After signing in, select the direct route with `graff --model anthropic` or+`/model anthropic`. A signed-in subscription takes precedence over an+`ANTHROPIC_API_KEY` for that route. The API key remains a metered credential;+if a subscription quota wall triggers the existing reserve-key handoff, Graff+announces the billing change and restores API-key authentication.++For an Anthropic Console account billed with API credits, use the separate+`graff login console` command. It retains the SDK-profile flow through the+`ant` CLI. Console profiles are metered credentials, not subscription logins;+they do not receive the subscription-only request adaptations.++## Credentials and compatibility++The browser flow listens on loopback before opening the authorization page,+checks the OAuth state, and exchanges the authorization code with its PKCE+verifier. Graff stores its own credentials at+`~/.anthropic/credentials/graff-oauth.json`, with a private file and directory,+using atomic replacement. It does not import Pi or Claude Code credentials.+Tokens refresh before expiry; a rejected token gets one bounded recovery+attempt through the same engine path on REPL and ACP.++OAuth requests use Bearer authorization and the subscription protocol's beta+headers, system identity preamble, and compatible tool names. Graff still owns+the actual system instructions, conversation, tool execution and cancellation.+Ordinary API-key requests keep their existing headers and prompt format.++## Upstream eligibility++This implements the protocol, not a promise that an account or third-party+application is approved. Anthropic documents prior approval for products that+offer Claude account login or subscription limits. Account eligibility,+permissions, plan limits and upstream protocol changes can prevent access.+API-key access remains supported independently. Offline protocol tests do not+prove live account approval or a successful subscription inference request.++Reference implementations: `badlogic/pi-mono`'s Anthropic OAuth and Messages+adapter; upstream policy: Claude Agent SDK overview and Claude Code+authentication documentation.++## Separate Claude CLI transport++`joaomj/pi-claude-directsdk` takes a different approach: it uses an installed,+authenticated, unmodified Claude CLI rather than implementing OAuth itself.+That transport is not enabled by these commands. A future explicit CLI mode+must preserve Graff's tool ownership, enforce request admission and clean up+children on cancellation. There is no silent CLI fallback.diff --git a/docs/mcp-server.md b/docs/mcp-server.mdindex 3f5841be..3b3f7993 100644--- a/docs/mcp-server.md+++ b/docs/mcp-server.md@@ -108,23 +108,71 @@ and scope task calls appropriately. Review resulting edits before integrating."name": "run_task","arguments": {"prompt": "Explain how the configuration parser handles missing values. Cite the relevant files. Do not change files.",+ "context": "We are on release/v0.0.292; the parser was rewritten last week and the old walker is gone.","timeout_seconds": 120,"max_model_calls": 8}}```-A useful brief includes the concrete task, relevant context, permitted scope,-and acceptance criteria. There is no inherited client conversation. Prefer+A useful brief includes the concrete task, permitted scope, and acceptance+criteria; background the child cannot re-derive goes in `context`. There is no+inherited client conversation: the child sees only what you pass. Prefersmall explanations, focused reviews, test runs, and narrowly scoped fixes.- `prompt`: required, nonblank, at most 32 KiB of UTF-8 text.-- `timeout_seconds`: 1–300, default 120.-- `max_model_calls`: 1–32, default 8, shared across the child invocation.+- `context`: optional background for the child — relevant history, decisions,+ file paths or constraints. Nonblank, at most 32 KiB of UTF-8 text. It is+ appended to the child's system prompt. At or below 8 KiB it is passed through+ verbatim; above that the server summarizes it first, because a fresh child+ cannot consume the gateway's opaque compaction state and a large handover+ would otherwise crowd out the task itself. Summarizing runs under the same+ credentials, so it bills to the same account, and it never touches the+ workspace: the summarizer runs `--safe` with no tool budget.+- `timeout_seconds`: 1–300, default 120. The summarizer's own deadline is+ capped separately at 60s and does not consume this budget.+- `max_model_calls`: 1–32, default 8, shared across the child invocation. The+ summarizer runs on its own single-call budget.- Root tool calls are capped at 64 per turn.+`context` carries text only. It cannot set cwd, credentials, model, permission+mode, or any CLI flag — those still come from the server launch.+The MCP result contains text, `isError`, and schema-described `structuredContent`-with `text`, `status`, `output_truncated`, `timeout_seconds`, and `max_model_calls`.+with `text`, `status`, `output_truncated`, `timeout_seconds`, `max_model_calls`,+`context_chars`, `context_summarized`, `context_used_chars`, `usage_reported`,+`usage_unavailable_reason`, and `preprocess`. The last three context fields report the context+the caller sent, whether it was summarized, and how much text the child actually+received. If summarization fails the task is not run and the error says so,+rather than silently dropping the context.++The same object is also mirrored under `_meta["codegraff/runTask"]` on the tool+result, so a client that renders the raw result can show supplied-context size+and summarization state without parsing prose. Those facts also cross into ACP:+a completed `tool_call_update` carries `_meta["codegraff/runTask"]` (alongside+`_meta["graff/view"]` when the result opened a view), so an ACP client such as+Harness renders them the same way. Only the fields above travel — never the+task's text and never the caller's context.++**Per-task usage and charge for the child's own execution are not reported.**+`usage_reported` is always `false`, with `usage_unavailable_reason` naming the+cause: the task child runs in print mode, which exposes no settled gateway+charge, and a charge is never derived from list rates+(`src/pricing_settled.zig`). This is a known limit, not an unimplemented field —+account-wide gateway usage remains authoritative for what a task cost.++**Preprocessing is reported, separately.** When context had to be summarized,+`preprocess` carries `cost_usd`, `usage_complete`, `input_tokens`,+`output_tokens` and `unavailable_reason`. The summarizer runs as a `--json`+child — print mode reports no settled charge — with its one request line on a+file-backed stdin, so the engine's exact-charge ingestion supplies the number+and no pipe has to be fed concurrently. An incomplete report+(`usage_complete: false`, or any `unpriced_calls` / `missing_usage_calls`) yields+`cost_usd: null` with a reason; a list-rate estimate is never presented as a+settlement. Preprocessing is reported in its own object and never folded into+the child total, so it cannot be double-counted. When no summarization was+needed, `unavailable_reason` says so and the tokens are zero.+Status is `completed`, `failed`, `timed_out`, or `cancelled`. Process failures and deadlinesreturn an error with partial output; changes made before failure are notrolled back. Successful exit reports the harness answer, not independentdiff --git a/graff-evals/CURSOR-COMPARISON.md b/graff-evals/CURSOR-COMPARISON.mdnew file mode 100644index 00000000..4df83a5a--- /dev/null+++ b/graff-evals/CURSOR-COMPARISON.md@@ -0,0 +1,92 @@+# Native Cursor comparisons++`cursor_compare.py` makes the Argon comparison repeatable and adds an xAI+subscription preset. This is opt-in live spend, separate from offline tier 1.+It reuses `run.py`, the existing deterministic checks and the same 21 core/MCP+fixtures: 11 core and 10 fixture-MCP tasks, three repetitions per arm by default.++| Preset | Graff model and login | Native Cursor model | Requested effort |+|---|---|---|---|+| `argon` | `codegraff/gemini-4-argon`, Codegraff login | `gemini-4-argon-low` | low |+| `xai-sub` | `xai/grok-4.7`, xAI subscription login | `grok-4.7-high` | high |++These compare the same **named model** across different harnesses and provider+backends, not a causal ablation of harness overhead alone. Cursor runs through+native `agent -p`, **not Harness desktop**. Requested reasoning scales may differ.+A missing or changed model label fails routing validation; no automatic fallback.++## Run++From the repository root, with `agent` on PATH and both required logins present:++```sh+zig build -Doptimize=ReleaseFast+# Six smoke runs; does not contribute to the full score.+python3 graff-evals/cursor_compare.py --preset xai-sub --smoke+# 126 interleaved runs, with a private temporary root printed on stdout.+python3 graff-evals/cursor_compare.py --preset xai-sub --reps 3 --workers 4+# Repeat the original Argon comparison through the reusable eval entry point.+python3 graff-evals/cursor_compare.py --preset argon --reps 3 --workers 4+# A labeled subset for MCP regressions; never merged into the full score.+python3 graff-evals/cursor_compare.py --preset xai-sub --task linear-issues --task linear-warm --task linear-quiet --reps 3 --workers 4+```++`--binary PATH` selects a Graff executable; the runner freezes and hashes it+before running either arm. `--cursor-bin PATH` selects the native CLI. The+Cursor version and bundled index hash are recorded; an index change invalidates+the run. Fixture prompts and system prompts are not rewritten.++`--output-root PATH` must be a **new directory outside every repository**. This+keeps private repo instructions and source context out of both sandboxes. Raw+receipts and model transcripts stay there. The fixture MCP script is copied+next to the output root so the existing task setup can find it. `--report-dir+PATH` optionally writes local summary JSON and Markdown elsewhere; it never+publishes anything. Do not commit raw receipts, private reports or root pointers.++An offline summary can be rebuilt without model calls:++```sh+python3 graff-evals/cursor_compare_report.py --root /private/eval-root --output /private/report+python3 -m unittest discover -s graff-evals -p 'test_cursor_compare.py'+```++## Authentication isolation++Graff gets only the selected saved login, an explicit provider/model selection,+and a fresh HOME. The xAI preset copies only the subscription access token and+expiry, **not the refresh token**; API-key fallback and other provider logins+are absent. The isolated route must confirm OAuth/login and subscription billing+before any scored run. xAI access tokens need at least 30 minutes remaining;+the eval does not refresh or rotate the real login. Use `--xai-auth PATH` or+`--codegraff-auth PATH` to select a saved login file.++On macOS the runner reads exactly one named Cursor Keychain access-token item,+never enumerates the Keychain, and never copies a valid refresh token. Elsewhere,+or to use a specific login snapshot, supply `--cursor-auth PATH` with a private+JSON object containing `accessToken`. Cursor's disposable file has a deliberately+invalid refresh value because its CLI requires that field. Do not pass secrets+as command-line arguments. Disposable login files are removed in `finally` after+normal runs/errors; a forced kill can leave private snapshots behind.++Both arms have isolated HOME/XDG directories, fixture-only MCP, no personal+rules, no inherited provider API keys, and no personal repository source. Only a+prewarmed Node compile cache is shared across Cursor runs, not account state.++## Interpretation++A clean pass requires the deterministic check, unchanged verifiers, exact model+routing, native completion, exit zero and no timeout. Graff additionally confirms+its requested effort selection. Incomplete/duplicate/unexpected run matrices+cannot produce a complete report. Failures remain in the score; passing-only+matched timings are reported separately with their both-pass denominator.++Wall time is the complete task-process lifetime. Cursor's self-reported task+duration and the wall time outside it (including CLI startup/teardown) are kept+separate; these are **not matched Graff model timings**. First output includes+startup events and is not TTFT. Comparable costs and Cursor model-call counts are+unknown, not zero. Subscription price is not free inference. Small fixture+results do not establish performance on large real-world repositories.++The original Argon replay is retained locally under+`artifacts/argon-cursor-20261017/`; it is historical evidence, not rerun or mixed+into subsequent preset scores.diff --git a/graff-evals/README.md b/graff-evals/README.mdindex b9493787..077e65f9 100644--- a/graff-evals/README.md+++ b/graff-evals/README.md@@ -29,6 +29,13 @@ harness under test spends model calls.| `mcp-hard` | Linear-shaped fixture MCP tasks that need more than ids and titles: cross-reference issue keys against `src/` (with decoy keys), roll issues up by priority, and join issues with git history by author date. Opt-in. || `aggregate` | Aggregate local data in one script: a per-directory commit digest over a pinned slice of this repository's history, fetched from GitHub in setup. Opt-in. |+For private solo, same-route-team and mixed-route comparisons on the paired fixtures, see [Private Worker Comparisons](WORKER-MATRIX.md). The four-arm example compares two routes alone and with their own workers. Summaries separate lead and worker rate estimates from native full cost; `--summarize` reads saved results offline without rerunning models. Preparation and offline regression checks make no model calls.++For repeatable native Cursor comparisons, see [Cursor Comparisons](CURSOR-COMPARISON.md).+The `argon` preset runs Argon through Codegraff versus Cursor at low effort;+`xai-sub` runs Grok 4.7 through the xAI subscription versus Cursor at high effort.+Both reuse the same 21 core/MCP fixtures with isolated logins and routing checks.+Terminal-Bench and other Harbor datasets run graff through the adapter in [harbor/](harbor/README.md).Published live board (2026-09-09, five harnesses on grok-4.6 SuperGrok):@@ -212,6 +219,8 @@ removes `--model` so production startup loads that provider's catalog. Noother provider credentials, user skills, saved sessions, or MCP configurationare inherited. Root API trace model identity must match for a run to qualify.+For opt-in private lead/worker route comparisons on the existing paired fixtures, see [Private Worker Comparisons](WORKER-MATRIX.md). Preparation and offline regression checks make no model calls.+```shpython3 graff-evals/run.py --suite swe --task config-parse \--harness graff-dev --provider codegraff --model gpt-6-astra \diff --git a/graff-evals/WORKER-MATRIX.md b/graff-evals/WORKER-MATRIX.mdnew file mode 100644index 00000000..47afd78c--- /dev/null+++ b/graff-evals/WORKER-MATRIX.md@@ -0,0 +1,119 @@+# Private Worker Comparisons++`worker_matrix.py` runs the existing paired `subagents` / `subagents-solo`+fixtures with independently selected lead and worker routes. It supports solo,+same-route teams and mixed-route teams without putting account configuration+or experimental results in the repository. It does not change engine defaults.++Preparing a matrix runs only the frozen fixture's initial checker. Executing+it requires a separate, explicit `--run` command and can spend provider credits.+The runner is POSIX-only. It does not replay arbitrary task setup commands.++```sh+python3 graff-evals/worker_matrix.py \+ --manifest "$PRIVATE_MANIFEST" --binary "$GRAFF_BINARY" \+ --output-root "$PRIVATE_OUTPUT"++# Only when live model execution is intended:+python3 graff-evals/worker_matrix.py --run "$PRIVATE_OUTPUT"++# Read an existing batch without model calls or modifying the saved evidence:+python3 graff-evals/worker_matrix.py --summarize "$PRIVATE_OUTPUT"+```++The output directory must be new and outside the repository. Existing runs+are never overwritten or retried. Keep manifests, receipts, credentials,+logs and results private; do not attach them to public trackers.++## Manifest++Create the manifest privately. The route names here are placeholders, not+available models:++```json+{+ "repetitions": 2,+ "max_model_calls": 64,+ "max_tool_calls": 180,+ "cost_stop_usd": "1",+ "auth_env": [],+ "auth_files": [],+ "tasks": [+ {"team": "sa-fix-three", "solo": "solo-fix-three", "workers": 3}+ ],+ "arms": [+ {"id": "solo-a", "root": {"provider": "provider-a", "model": "model-a"}},+ {"id": "team-a", "root": {"provider": "provider-a", "model": "model-a"},+ "worker": {"provider": "provider-a", "model": "model-a"}},+ {"id": "solo-b", "root": {"provider": "provider-b", "model": "model-b"}},+ {"id": "team-b", "root": {"provider": "provider-b", "model": "model-b"},+ "worker": {"provider": "provider-b", "model": "model-b"}}+ ]+}+```++This four-arm matrix compares each route alone and with three workers using+that same route. No separate larger lead is needed. Two repetitions produce+eight trials on the same three-package repair fixture.++Each arm can select a different lead. For a mixed team, add an arm whose `root`+uses route A and whose `worker` uses route B. The order rotates across repetitions and tasks;+cache state is not reset and model-specific default reasoning is retained.++`auth_env` lists only the authentication environment-variable names explicitly+needed for the selected routes. Values are inherited by the harness, never+written into receipts. `auth_files` entries have `source` and `destination`:+the source is an existing private credential or catalog file, and the destination+is relative to each trial's fresh HOME. No other user configuration is inherited.+Provision authentication deliberately; preparation does not sign in or create keys.+Fresh HOME and restricted prompts are isolation measures, not an OS security sandbox.++Other existing pairs include `sa-rename-api` / `solo-rename-api` (three workers),+`sa-sidecar` / `solo-sidecar` (one) and `sa-coverage` / `solo-coverage` (two).+Set `freeze_all_inputs: true` on inventory/reconciliation tasks such as coverage,+where input modifications must not qualify as a fix.++## Measurements++- Frozen executable, runner, measurement, manifest, fixture and grader hashes.+- Held-out checks outside the agent cwd; visible verifiers protected against edits.+- Actual lead/worker route identities, worker receipts and per-role observed tokens.+- Wall time including lead review/repair, exit/timeout status and failed API attempts.+- Native full cost only when tokens and pricing are complete, with no excluded+ subscription calls. A known subtotal is not a full charge, including when zero.++Optional `prices.root` and `prices.worker` on an arm supply explicit hypothetical+per-million rates: `input`, `cached`, `write`, `write_1h`, `output`, a positive+per-request `max_input`, and a required `source` describing the assumption.+The runner never infers a gateway tariff from a direct-provider model name.+Rates apply to each recorded request, not aggregated tokens. Hypothetical+API-equivalent estimates remain separate from native cost.++Summaries include attempted/passed counts and mean wall time, native full cost,+API-equivalent total, and `mean_estimated_role_costs_usd` split into `root` and+`worker`. Role estimates require explicit rates; they are not native per-role+charges. A missing role estimate in any trial keeps that role's mean unknown.+An observed solo run's explicit zero worker estimate remains zero. Older saved+results without role estimates remain readable, but their role means are unknown.+`--summarize` only reads saved `results.json` and prints a JSON report; it does+not execute the harness, recheck graders, rewrite receipts, or validate old+results against today's executable.++Unknown usage or wrong routes stop the batch. Unknown full native cost also+stops it unless a complete explicit rate estimate is available. The between-run+cost stop uses native full cost when known, otherwise that estimate; it is not+a provider-side hard dollar cap. All trials, including failures, enter time+and cost means. Any unknown cost keeps the corresponding full mean unknown.++Fixture passes measure their held-out contracts, not general coding quality.+Documentation coverage checks do not establish full semantic accuracy, and+worker quality is not graded independently of the lead's repairs.++## Offline Regression Checks++```sh+python3 -m unittest discover -s graff-evals -p 'test_worker_matrix.py'+```++These checks use synthetic routes, usage events and a local fake executable.+They make no model or network calls and do not read real credentials.diff --git a/graff-evals/cursor_compare.py b/graff-evals/cursor_compare.pynew file mode 100644index 00000000..7fdcedb0--- /dev/null+++ b/graff-evals/cursor_compare.py@@ -0,0 +1,308 @@+#!/usr/bin/env python3+"""Matched, isolated Graff versus native Cursor comparisons (opt-in live spend)."""+import argparse+import copy+from datetime import datetime, timezone+import hashlib+import json+import os+from pathlib import Path+import shutil+import subprocess+import sys+import tempfile+import time++HERE = Path(__file__).resolve().parent+REPO = HERE.parent+++def catalog():+ return json.loads((HERE / "cursor_compare_presets.json").read_text())+++def select_tasks(data, smoke, requested):+ if requested:+ if smoke or len(set(requested)) != len(requested) or not set(requested) <= set(data["tasks"]):+ raise ValueError("--task requires unique catalog tasks and cannot be combined with --smoke")+ return [task for task in data["tasks"] if task in requested]+ return data["smoke_tasks"] if smoke else data["tasks"]+++def private_json(path, value):+ path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)+ path.write_text(json.dumps(value, indent=2) + "\n")+ path.chmod(0o600)+++def events(stdout):+ rows = []+ for line in stdout.splitlines():+ try:+ row = json.loads(line)+ except ValueError:+ continue+ if isinstance(row, dict):+ rows.append(row)+ return rows+++def cursor_receipt(stdout, expected_label):+ rows = events(stdout)+ labels = sorted({" ".join(r["model"].split()) for r in rows+ if r.get("type") == "system" and isinstance(r.get("model"), str)})+ finals = [r for r in rows if r.get("type") == "result"]+ final = finals[-1] if finals else {}+ return {"observed_model_labels": labels,+ "routing_ok": labels == [expected_label],+ "native_completion_ok": bool(finals) and final.get("subtype") == "success" and not final.get("is_error"),+ "native_task_duration_s": final.get("duration_ms", 0) / 1000 if isinstance(final.get("duration_ms"), (int, float)) else None}+++def graff_receipt(sandbox, provider, model, effort):+ rows = []+ for path in (sandbox / ".graff/traces").glob("*.jsonl"):+ rows.extend(events(path.read_text(errors="replace")))+ api = [r for r in rows if r.get("ev") == "api"]+ roots = [r for r in api if r.get("agent") in ("main", "repl")]+ pairs = sorted({(r["provider"], r["model"]) for r in rows+ if isinstance(r.get("provider"), str) and isinstance(r.get("model"), str)})+ output = (sandbox / ".eval-stdout.txt").read_text(errors="replace")+ effort_ok = "reasoning effort: " + effort.lower() in output.lower()+ return {"observed_provider_models": pairs,+ "observed_root_models": sorted({r.get("model", "") for r in roots}),+ "trace_api_calls": len(api), "effort_selection_ok": effort_ok,+ "routing_ok": bool(roots) and bool(pairs) and effort_ok+ and all(r.get("model") == model for r in roots)+ and all(p == provider for p, _ in pairs),+ "provider_evidence": "trace+isolated-login+explicit-route"}+++def access_credentials(provider, args):+ """Only the selected login, never an API-key fallback or refresh token."""+ path = Path(args.codegraff_auth if provider == "codegraff" else args.xai_auth).expanduser()+ data = json.loads(path.read_text())+ if provider == "codegraff":+ if not data.get("api_key"):+ raise ValueError("Codegraff login missing; run graff login codegraff")+ return {"api_key": data["api_key"]}+ if not data.get("access_token"):+ raise ValueError("xAI subscription login missing; run graff login xai")+ expiry = data.get("expires_at")+ if not isinstance(expiry, (int, float)) or expiry < time.time() + 1800:+ raise ValueError("xAI login has less than 30 minutes remaining; refresh the login before starting")+ return {"access_token": data["access_token"], "expires_at": int(expiry)}+++def cursor_credentials(path):+ if path:+ data = json.loads(Path(path).expanduser().read_text())+ access = data.get("accessToken")+ elif sys.platform == "darwin":+ # One exact named item; no keychain enumeration or refresh credential.+ result = subprocess.run(["security", "find-generic-password", "-s", "cursor-access-token",+ "-a", "cursor-user", "-w"], capture_output=True, text=True)+ access = result.stdout.strip() if result.returncode == 0 else ""+ else:+ raise ValueError("Supply --cursor-auth with a private accessToken snapshot")+ if not access:+ raise ValueError("Cursor access login missing; sign in with agent login")+ # The CLI requires this field but must never rotate the owner's refresh token.+ return {"accessToken": access, "refreshToken": "disabled-for-evaluation"}+++def safe_environment(cache):+ env = {k: os.environ[k] for k in ("PATH", "LANG", "LC_ALL", "SDKROOT", "DEVELOPER_DIR", "TMPDIR") if k in os.environ}+ env.update(GRAFF_NO_TELEMETRY="1", GRAFF_BEHAVIOR_UPLOAD="off", GRAFF_FLEET="off",+ GRAFF_NO_SMOLIFY="1", NO_COLOR="1", DIRENV_DISABLE="1", NODE_COMPILE_CACHE=str(cache))+ return env+++def home_environment(home):+ return {"HOME": str(home), "USERPROFILE": str(home), "XDG_CONFIG_HOME": str(home / ".config"),+ "XDG_CACHE_HOME": str(home / ".cache"), "CURSOR_CONFIG_DIR": str(home / ".cursor"),+ "AGENT_CLI_CREDENTIAL_STORE": "file"}+++def seed_login(home, provider, model, credentials):+ home.mkdir(parents=True, exist_ok=True, mode=0o700)+ (home / ".simple-harness-model").write_text(provider + "\n" + model + "\n")+ relative = ".simple-harness-codegraff.json" if provider == "codegraff" else ".xai/credentials/graff-oauth.json"+ private_json(home / relative, credentials)+++def cleanup_credentials(root):+ for pattern in ("**/.cursor/auth.json", "**/.simple-harness-codegraff.json", "**/.xai/credentials/graff-oauth.json"):+ for path in root.glob(pattern):+ path.unlink(missing_ok=True)+++def configure_runner(runner, preset, root, frozen, agent, credentials, cursor_auth):+ materialize_original = runner.materialize+ harnesses_original = runner.load_harnesses+ parse_original = runner.parse_answer_and_usage+ run_original = runner.one_run++ def materialize(task, sandbox):+ materialize_original(task, sandbox)+ home = Path(sandbox) / ".eval-home"+ if Path(sandbox).name.startswith("graff-"):+ seed_login(home, preset["provider"], preset["models"]["graff"], credentials)+ else:+ private_json(home / ".cursor/auth.json", cursor_auth)+ source = Path(sandbox) / ".mcp.json"+ if source.is_file():+ config = json.loads(source.read_text())+ for spec in config.get("mcpServers", {}).values():+ spec["args"] = [str(Path(sandbox) / arg) if isinstance(arg, str) and (Path(sandbox) / arg).exists() else arg+ for arg in spec.get("args", [])]+ private_json(Path(sandbox) / ".cursor/mcp.json", config)++ def harnesses():+ graff = copy.deepcopy(harnesses_original()["graff-repl"])+ graff["cmd"][0] = str(frozen)+ graff["stdin"] = "/effort " + preset["effort"] + "\n" + graff["stdin"]+ graff["default_model"] = preset["models"]["graff"]+ graff["model_prefix"] = preset["provider"]+ cursor = {"cmd": [str(agent), "-p", "--model", "{model}", "--output-format", "stream-json",+ "--trust", "--force", "--approve-mcps", "--workspace", "{sandbox}", "{prompt}"],+ "answer": "cursor-stream", "default_model": preset["models"]["cursor"]}+ selected = {"graff": graff, "cursor": cursor}+ for harness in selected.values():+ harness.setdefault("env", {}).update(home_environment(Path("{sandbox}/.eval-home")))+ harness["env"]["GRAFF_MCP_CONFIG"] = "{sandbox}/.mcp.json"+ return selected++ def parse(harness, stdout, stderr, sandbox=""):+ if harness["answer"] != "cursor-stream":+ return parse_original(harness, stdout, stderr, sandbox)+ finals = [r for r in events(stdout) if r.get("type") == "result"]+ if not finals:+ return "", {}+ final = finals[-1]+ native = final.get("usage") or {}+ usage = {"native_input_tokens": native.get("inputTokens"), "native_output_tokens": native.get("outputTokens"),+ "native_cache_read_tokens": native.get("cacheReadTokens"), "native_cache_write_tokens": native.get("cacheWriteTokens"),+ "api_ms": final.get("duration_api_ms")}+ return str(final.get("result") or "").strip(), usage++ def one_run(hname, harness, task, model, rep, live=False):+ rec = run_original(hname, harness, task, model, rep, live)+ if "wall_s" not in rec:+ return rec+ sandbox = Path(runner.SANDBOX_DIR) / f"{hname}-{task['id']}-r{rep}"+ if hname == "cursor":+ rec.update(cursor_receipt((sandbox / ".eval-stdout.txt").read_text(), preset["cursor_label"]))+ if rec["native_task_duration_s"] is not None:+ rec["outside_native_duration_s"] = rec["wall_s"] - rec["native_task_duration_s"]+ else:+ rec.update(graff_receipt(sandbox, preset["provider"], model, preset["effort"]))+ rec["native_completion_ok"] = rec.get("exit") == 0 and not rec.get("timed_out")+ rec["list_usd"] = None+ rec["list_price_note"] = "Subscription comparison; comparable marginal costs are unknown, not zero"+ rec["comparison_kind"] = "same-named-model, different harness/provider backends"+ rec["outcome_ok"] = bool(rec["outcome_ok"] and rec["routing_ok"] and rec["native_completion_ok"]+ and rec.get("exit") == 0 and not rec.get("timed_out"))+ return rec++ runner.materialize, runner.load_harnesses = materialize, harnesses+ runner.parse_answer_and_usage, runner.one_run = parse, one_run+ runner.eval_report.line = lambda r: (f"{'PASS' if r.get('outcome_ok') else 'FAIL'} {r['harness']:6} {r['task']:20} "+ f"r{r['rep']} wall={r.get('wall_s')}s artifact={r.get('artifact_ok')} "+ f"route={r.get('routing_ok')} exit={r.get('exit')}")+ runner.eval_report.summarize = lambda rows: print("Comparable prices and Cursor model-call counts are unknown.")+++def main():+ data = catalog()+ parser = argparse.ArgumentParser(description=__doc__)+ parser.add_argument("--preset", choices=sorted(data["presets"]), required=True)+ parser.add_argument("--binary", default=str(REPO / "zig-out/bin/graff"))+ parser.add_argument("--cursor-bin", default="agent")+ parser.add_argument("--cursor-auth", help="private JSON containing accessToken; macOS defaults to one named Keychain item")+ parser.add_argument("--codegraff-auth", default=str(Path.home() / ".simple-harness-codegraff.json"))+ parser.add_argument("--xai-auth", default=str(Path.home() / ".xai/credentials/graff-oauth.json"))+ parser.add_argument("--smoke", action="store_true", help="three fixture pairs, one repetition; excluded from full score")+ parser.add_argument("--task", action="append", help="repeat to select a catalog subset; incompatible with --smoke")+ parser.add_argument("--reps", type=int, default=3)+ parser.add_argument("--workers", type=int, default=4)+ parser.add_argument("--output-root", help="new private receipts root OUTSIDE every repository; otherwise a private temp directory")+ parser.add_argument("--report-dir", help="optional local summary directory (no native logs or credentials)")+ args = parser.parse_args()+ if args.reps < 1 or args.workers < 1:+ parser.error("--reps and --workers must be positive")+ try:+ tasks = select_tasks(data, args.smoke, args.task)+ except ValueError as error:+ parser.error(str(error))+ preset = data["presets"][args.preset]+ credentials = access_credentials(preset["provider"], args)+ cursor_auth = cursor_credentials(args.cursor_auth)+ root = Path(args.output_root).expanduser().resolve() if args.output_root else Path(tempfile.mkdtemp(prefix="cursor-" + args.preset + "-"))+ if any((p / ".git").exists() for p in (root, *root.parents)):+ parser.error("receipts and sandboxes must be outside repositories")+ if args.output_root:+ root.mkdir(parents=True, mode=0o700, exist_ok=False)+ root.chmod(0o700)+ scripts = root / "scripts"+ scripts.mkdir()+ shutil.copy2(REPO / "scripts/linear_fixture_mcp.py", scripts)+ frozen = root / "graff"+ shutil.copy2(Path(args.binary).resolve(), frozen)+ agent_path = shutil.which(args.cursor_bin)+ if not agent_path:+ raise ValueError("Cursor CLI not found; install agent or supply --cursor-bin")+ agent = Path(agent_path).resolve()+ index = agent.with_name("index.js")+ digest = lambda path: hashlib.sha256(path.read_bytes()).hexdigest()+ version = subprocess.check_output([str(agent), "--version"], text=True).strip()+ env = safe_environment(root / "node-compile-cache")+ preflight = root / "preflight-home"+ seed_login(preflight, preset["provider"], preset["models"]["graff"], credentials)+ env.update(home_environment(preflight))+ env["GRAFF_MCP_CONFIG"] = str(root / "empty-mcp.json")+ private_json(root / "empty-mcp.json", {"mcpServers": {}})+ try:+ route = subprocess.run([str(frozen), "route", preset["provider"] + "/" + preset["models"]["graff"]],+ env=env, capture_output=True, text=True, check=True, timeout=30).stdout.strip()+ if "auth: OAuth/login" not in route or (preset["provider"] == "xai" and "billing: subscription, flat-rate" not in route):+ raise ValueError("isolated provider route did not confirm the requested login/subscription")+ subprocess.run([str(agent), "--version"], env=env, capture_output=True, check=True, timeout=90)+ meta = {"preset": args.preset, "models": preset["models"], "provider": preset["provider"], "effort": preset["effort"],+ "repetitions": 1 if args.smoke else args.reps, "tasks": tasks, "workers": min(2, args.workers) if args.smoke else args.workers,+ "mode": "smoke" if args.smoke else "subset" if args.task else "full", "graff_sha256": digest(frozen), "build_mode": "caller-supplied binary (frozen)",+ "cursor_version": version, "cursor_index_sha256": digest(index) if index.is_file() else None,+ "comparison_kind": "same named model, different harnesses/provider backends", "isolated_route": route,+ "credentials": "isolated access-only snapshots; no valid refresh tokens, API-key fallback, personal MCP or rules",+ "latency_note": "wall is task process lifetime; first_out includes startup events and is not TTFT",+ "runtime_cache": "Cursor Node compile cache prewarmed once and shared; account/workspace homes isolated",+ "created_at": datetime.now(timezone.utc).isoformat()}+ private_json(root / "metadata.json", meta)+ print("Private comparison root:", root, flush=True)+ import run as runner+ original_env = dict(os.environ)+ os.environ.clear()+ os.environ.update(env)+ try:+ configure_runner(runner, preset, root, frozen, agent, credentials, cursor_auth)+ sys.argv = [str(HERE / "run.py"), "--harness", "graff,cursor", "--suite", "core,mcp", "--reps", str(meta["repetitions"]),+ "-j", str(meta["workers"]), "--interleave", "--arm", args.preset, "--output-root", str(root / "comparison")]+ for task in tasks:+ sys.argv += ["--task", task]+ runner.main()+ finally:+ os.environ.clear()+ os.environ.update(original_env)+ if index.is_file() and digest(index) != meta["cursor_index_sha256"]:+ raise ValueError("Cursor CLI changed during the comparison")+ from cursor_compare_report import summarize+ report = summarize(root, Path(args.report_dir) if args.report_dir else root / "report")+ if any(report.get("failures", [])):+ return 1+ finally:+ cleanup_credentials(root)+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/graff-evals/cursor_compare_presets.json b/graff-evals/cursor_compare_presets.jsonnew file mode 100644index 00000000..85654df1--- /dev/null+++ b/graff-evals/cursor_compare_presets.json@@ -0,0 +1,26 @@+{+ "tasks": [+ "exact-reply", "fix-fib", "file-ops", "json-transform", "regex-count",+ "refactor-rename", "write-tests", "git-ops", "recall-noise", "csv-sum",+ "dead-code", "linear-issues", "linear-warm", "linear-sidecar", "linear-split",+ "linear-quiet", "linear-quiet-warm", "linear-nohint", "linear-nohint-warm",+ "linear-reduce", "linear-reduce-warm"+ ],+ "smoke_tasks": ["exact-reply", "file-ops", "linear-issues"],+ "presets": {+ "argon": {+ "provider": "codegraff",+ "models": {"graff": "gemini-4-argon", "cursor": "gemini-4-argon-low"},+ "cursor_label": "Gemini 4 Argon Low",+ "effort": "low",+ "description": "Argon via Codegraff login versus native Cursor, low effort"+ },+ "xai-sub": {+ "provider": "xai",+ "models": {"graff": "grok-4.7", "cursor": "grok-4.7-high"},+ "cursor_label": "Grok 4.7 256K High",+ "effort": "high",+ "description": "Grok 4.7 via xAI subscription versus native Cursor, high effort"+ }+ }+}diff --git a/graff-evals/cursor_compare_report.py b/graff-evals/cursor_compare_report.pynew file mode 100644index 00000000..32412eef--- /dev/null+++ b/graff-evals/cursor_compare_report.py@@ -0,0 +1,327 @@+#!/usr/bin/env python3+"""Offline, private Graff/native-Cursor comparison reports; no provider calls."""+import argparse+from collections import Counter+import json+import math+from pathlib import Path+import statistics+from typing import Optional++ARMS = ("graff", "cursor")+FAILURE_FIELDS = (+ "harness", "task", "rep", "artifact_ok", "outcome_ok", "exit", "timed_out",+ "routing_ok", "native_completion_ok", "verifiers_unchanged", "check_note", "error",+)+++def clean(row: dict) -> bool:+ """Match the receipt's complete clean-pass gate, not artifact success alone."""+ return bool(+ row.get("outcome_ok") and row.get("routing_ok")+ and row.get("native_completion_ok") and row.get("exit") == 0+ and not row.get("timed_out") and row.get("verifiers_unchanged")+ )+++def numeric(value) -> bool:+ return (isinstance(value, (int, float)) and not isinstance(value, bool)+ and math.isfinite(value))+++def metric(values, median: bool = False) -> Optional[float]:+ values = list(values)+ if not values:+ return None+ return round((statistics.median if median else statistics.mean)(values), 3)+++def aggregate(items: list, arm: str) -> dict:+ wall = [r["wall_s"] for r in items if numeric(r.get("wall_s"))]+ passed = [r["wall_s"] for r in items if clean(r) and numeric(r.get("wall_s"))]+ # These are Cursor's own measurements, never a matched Graff metric.+ native = [r["native_task_duration_s"] for r in items+ if arm == "cursor" and numeric(r.get("native_task_duration_s"))]+ overhead = [r["outside_native_duration_s"] for r in items+ if arm == "cursor" and numeric(r.get("outside_native_duration_s"))]+ return {+ "runs": len(items), "clean_pass": sum(clean(r) for r in items),+ "artifact_pass": sum(bool(r.get("artifact_ok")) for r in items),+ "timeouts": sum(bool(r.get("timed_out")) for r in items),+ "routing_verified": sum(bool(r.get("routing_ok")) for r in items),+ "mean_wall_s": metric(wall), "median_wall_s": metric(wall, median=True),+ "wall_reported_runs": len(wall),+ "passing_mean_wall_s": metric(passed),+ "passing_median_wall_s": metric(passed, median=True),+ "passing_wall_reported_runs": len(passed),+ "native_task_duration_mean_s": metric(native),+ "native_task_duration_median_s": metric(native, median=True),+ "outside_native_duration_mean_s": metric(overhead),+ "outside_native_duration_median_s": metric(overhead, median=True),+ "native_duration_reported_runs": len(native),+ "outside_native_duration_reported_runs": len(overhead),+ }+++def paired_stats(items: list) -> dict:+ pairs = {}+ for row in items:+ pairs.setdefault((row["task"], row["rep"]), {})[row["harness"]] = row+ both_passed = [p for p in pairs.values() if all(clean(p[a]) for a in ARMS)]+ timed = [p for p in both_passed if all(numeric(p[a].get("wall_s")) for a in ARMS)]+ means = {a: metric(p[a]["wall_s"] for p in timed) for a in ARMS}+ medians = {a: metric((p[a]["wall_s"] for p in timed), median=True) for a in ARMS}+ reduction = None+ if timed:+ graff_mean = statistics.mean(p["graff"]["wall_s"] for p in timed)+ cursor_mean = statistics.mean(p["cursor"]["wall_s"] for p in timed)+ if cursor_mean > 0:+ reduction = round((1 - graff_mean / cursor_mean) * 100, 1)+ return {+ "pairs": len(pairs), "both_clean_passed": len(both_passed),+ "timed_both_clean_passed": len(timed),+ "graff_faster_on_both_passing_pairs": sum(+ p["graff"]["wall_s"] < p["cursor"]["wall_s"] for p in timed),+ "mean_wall_s": means, "median_wall_s": medians,+ "graff_wall_reduction_percent": reduction,+ }+++def validate(meta: dict, rows: list) -> None:+ if not isinstance(meta, dict):+ raise ValueError("metadata.json must contain an object")+ for name, allowed in (+ ("preset", ("argon", "xai-sub")), ("provider", ("codegraff", "xai")),+ ("effort", ("low", "high")), ("mode", ("smoke", "subset", "full")),+ ):+ if meta.get(name) not in allowed:+ raise ValueError("metadata %s must be one of %s" % (name, ", ".join(allowed)))+ for name in ("repetitions", "workers"):+ if type(meta.get(name)) is not int or meta[name] < 1:+ raise ValueError("metadata %s must be a positive integer" % name)+ models = meta.get("models")+ if not isinstance(models, dict) or any(+ not isinstance(models.get(a), str) or not models[a] for a in ARMS+ ):+ raise ValueError("metadata models must name both graff and cursor model ids")+ tasks = meta.get("tasks")+ if (not isinstance(tasks, list) or not tasks+ or any(not isinstance(t, str) or not t for t in tasks)+ or len(tasks) != len(set(tasks))):+ raise ValueError("metadata tasks must be a nonempty list of unique task names")+ expected = {(a, t, rep) for a in ARMS for t in tasks+ for rep in range(1, meta["repetitions"] + 1)}+ observed = Counter()+ task_suites = {}+ for index, row in enumerate(rows, 1):+ if not isinstance(row, dict):+ raise ValueError("result row %d must be an object" % index)+ arm, task, rep = (row.get(k) for k in ("harness", "task", "rep"))+ if not isinstance(arm, str) or not isinstance(task, str) or type(rep) is not int:+ raise ValueError("result row %d needs harness/task strings and an integer rep" % index)+ observed[(arm, task, rep)] += 1+ suite = row.get("suite")+ if not isinstance(suite, str) or not suite or suite == "all":+ raise ValueError("result row %d needs a named suite (not 'all')" % index)+ previous = task_suites.setdefault(task, suite)+ if suite != previous:+ raise ValueError("task %r has inconsistent suites: %r and %r" % (task, previous, suite))+ for field in ("wall_s", "native_task_duration_s", "outside_native_duration_s"):+ value = row.get(field)+ if value is not None and (not numeric(value) or value < 0):+ raise ValueError("result row %d has invalid %s" % (index, field))+ if "mode" in row and row["mode"] != meta["mode"]:+ raise ValueError("result row %d mode differs from metadata; do not mix smoke/full" % index)+ missing = sorted(expected - set(observed))+ extras = sorted(set(observed) - expected)+ duplicates = sorted(k for k, count in observed.items() if count > 1)+ if missing or extras or duplicates:+ parts = []+ for label, keys in (("missing", missing), ("unexpected", extras), ("duplicate", duplicates)):+ if keys:+ preview = ", ".join("%s/%s/rep=%s" % k for k in keys[:8])+ parts.append("%s (%d): %s%s" % (+ label, len(keys), preview, " ..." if len(keys) > 8 else ""))+ raise ValueError("invalid task/repetition/arm matrix: " + "; ".join(parts))+++def caveats(meta: dict) -> list:+ return [+ "Same named model, different provider backends: this is not a provider-controlled "+ "causal ablation of the harness alone. See the recorded model ids and comparison kind.",+ "Both arms request %s effort on their own scales; requested effort scales and "+ "provider-side reasoning implementations differ." % meta["effort"],+ "%d small deterministic fixtures, %d repetitions per arm, %d workers; mode: %s. "+ "This is a small fixture sample, not evidence about large real-world repositories."+ % (len(meta["tasks"]), meta["repetitions"], meta["workers"], meta["mode"]),+ "Graff binary SHA-256 and Cursor version are recorded in metadata, not inferred "+ "from current installations. No claim of a clean source tree is made.",+ "Cursor uses native CLI agent -p, not desktop Harness or its SDK transport.",+ "Synthetic sandboxes use isolated login snapshots, fixture-only MCP, and no "+ "personal project context; credentials are not refreshed.",+ "Smoke runs are excluded from full results. A smoke-mode report is only a smoke "+ "report, not a full benchmark. Only the supplied root's result file is read.",+ "All runs, including failures, are retained. All-run wall timings are separate "+ "from passing-only matched task/repetition pairs; both-pass denominators are explicit.",+ "first_out measures any output including startup events, not time to first model "+ "token (TTFT). Wall time is task process lifetime.",+ "Cursor native task duration and outside-native overhead are reported separately "+ "with their own measurement counts. They are self-reported, not a comparable Graff "+ "metric; outside-native wall time includes CLI startup/teardown.",+ "Comparable dollar cost and model/API-call counts are unknown, never zero. "+ "Native usage receipts, if present, do not establish comparable prices or call counts.",+ "These are private local reports. The reporter never publishes them or makes "+ "network or model calls.",+ ]+++def cell(value) -> str:+ return str(value).replace("|", "\\|").replace("\n", " ").replace("\r", " ")+++def seconds(value) -> str:+ return "unknown" if value is None else "%.2fs" % value+++def markdown(report: dict) -> str:+ meta, summary = report["metadata"], report["summary"]+ lines = [+ "# %s in Graff and native Cursor" % cell(meta["preset"]), "",+ "Private local report — not published.", "",+ "Created: %s. Mode: **%s**. Comparison: %s."+ % (cell(meta.get("created_at", "unknown")), meta["mode"],+ cell(meta.get("comparison_kind", "unknown"))), "",+ "Graff: `%s` via `%s`. Cursor: `%s` via native `agent -p`. Requested effort: %s "+ "on each arm's own scale. %d tasks × %d repetitions × two arms; %d workers."+ % (cell(meta["models"]["graff"]), cell(meta["provider"]),+ cell(meta["models"]["cursor"]), meta["effort"], len(meta["tasks"]),+ meta["repetitions"], meta["workers"]), "",+ "Graff SHA-256: `%s`. Cursor version: `%s`."+ % (cell(meta.get("graff_sha256", "unknown")), cell(meta.get("cursor_version", "unknown"))),+ "",+ "| Suite | System | Clean pass | Mean wall (all runs) | Median wall (all runs) | Measured runs |",+ "|---|---|---:|---:|---:|---:|",+ ]+ for suite, arms in summary.items():+ for arm, stats in arms.items():+ lines.append("| %s | %s | %d/%d | %s | %s | %d/%d |" % (+ cell(suite), arm, stats["clean_pass"], stats["runs"],+ seconds(stats["mean_wall_s"]), seconds(stats["median_wall_s"]),+ stats["wall_reported_runs"], stats["runs"]))+ lines += ["", "## Matched passing-only pairs", "",+ "Means and medians below use the same both-clean, timed pairs for both arms.", "",+ "| Suite | Both clean / all pairs | Timed both-clean pairs | Graff mean / median | Cursor mean / median | Graff faster |",+ "|---|---:|---:|---:|---:|---:|"]+ for suite, paired in report["paired_by_suite"].items():+ timed = paired["timed_both_clean_passed"]+ lines.append("| %s | %d/%d | %d/%d | %s / %s | %s / %s | %d/%d |" % (+ cell(suite), paired["both_clean_passed"], paired["pairs"], timed,+ paired["both_clean_passed"], seconds(paired["mean_wall_s"]["graff"]),+ seconds(paired["median_wall_s"]["graff"]),+ seconds(paired["mean_wall_s"]["cursor"]),+ seconds(paired["median_wall_s"]["cursor"]),+ paired["graff_faster_on_both_passing_pairs"], timed))+ lines += ["", "## Cursor native timing (not a matched Graff metric)", "",+ "| Suite | Native mean / median | Native measured | Outside-native mean / median | Outside measured |",+ "|---|---:|---:|---:|---:|"]+ for suite, arms in summary.items():+ stats = arms["cursor"]+ lines.append("| %s | %s / %s | %d/%d | %s / %s | %d/%d |" % (+ cell(suite), seconds(stats["native_task_duration_mean_s"]),+ seconds(stats["native_task_duration_median_s"]),+ stats["native_duration_reported_runs"], stats["runs"],+ seconds(stats["outside_native_duration_mean_s"]),+ seconds(stats["outside_native_duration_median_s"]),+ stats["outside_native_duration_reported_runs"], stats["runs"]))+ lines += ["", "## Measurement notes", ""]+ for key in ("credentials", "latency_note", "runtime_cache"):+ lines.append("- %s: %s" % (key, cell(meta.get(key, "unknown"))))+ lines += ["", "## Qualification", ""]+ lines += ["- " + c for c in report["caveats"]]+ lines += ["", "## Non-clean runs", ""]+ if not report["failures"]:+ lines.append("None.")+ else:+ lines += ["| System | Task | Rep | Artifact | Outcome | Routing | Native completion | Exit | Timeout | Verifiers unchanged |",+ "|---|---|---:|---|---|---|---|---:|---|---|"]+ keys = ("harness", "task", "rep", "artifact_ok", "outcome_ok", "routing_ok",+ "native_completion_ok", "exit", "timed_out", "verifiers_unchanged")+ for row in report["failures"]:+ lines.append("| " + " | ".join(cell(row.get(k)) for k in keys) + " |")+ lines += ["", "Failure notes are retained in results.json; complete run receipts remain in the private comparison root."]+ return "\n".join(lines) + "\n"+++def without_root(value, roots: list):+ """Keep report contents relocatable, including root paths embedded in errors."""+ if isinstance(value, str):+ for root in roots:+ value = value.replace(root, "<root>")+ return value+ if isinstance(value, list):+ return [without_root(item, roots) for item in value]+ if isinstance(value, dict):+ return {without_root(k, roots): without_root(v, roots) for k, v in value.items()}+ return value+++def summarize(root: Path, output: Path) -> dict:+ """Validate one complete 1-based run matrix and write its private local report."""+ root, output = Path(root), Path(output)+ meta = json.loads((root / "metadata.json").read_text(encoding="utf-8"))+ rows = []+ for line_number, line in enumerate(+ (root / "comparison" / "results.jsonl").read_text(encoding="utf-8").splitlines(), 1+ ):+ if not line.strip():+ continue+ try:+ rows.append(json.loads(line))+ except json.JSONDecodeError as exc:+ raise ValueError("invalid comparison/results.jsonl JSON at line %d" % line_number) from exc+ validate(meta, rows)+ suites = ["all", "core", "mcp"] + sorted({r["suite"] for r in rows} - {"core", "mcp"})+ groups = {s: [r for r in rows if s == "all" or r["suite"] == s] for s in suites}+ summary = {s: {a: aggregate([r for r in group if r["harness"] == a], a)+ for a in ARMS} for s, group in groups.items()}+ paired = {s: paired_stats(group) for s, group in groups.items()}+ report = {+ "metadata": meta, "summary": summary, "paired": paired["all"],+ "paired_by_suite": paired,+ "per_task": {t: {a: aggregate([r for r in rows if r["task"] == t+ and r["harness"] == a], a) for a in ARMS}+ for t in meta["tasks"]},+ "failures": [{k: r.get(k) for k in FAILURE_FIELDS} for r in rows if not clean(r)],+ "caveats": caveats(meta),+ }+ roots = sorted({str(root.absolute()), str(root.resolve())}, key=len, reverse=True)+ # A filesystem root cannot be safely scrubbed by replacing every slash.+ if any(Path(p).parent == Path(p) for p in roots):+ raise ValueError("comparison root must not be the filesystem root")+ report = without_root(report, roots)+ output.mkdir(parents=True, exist_ok=True)+ (output / "results.json").write_text(json.dumps(report, indent=2, allow_nan=False) + "\n", encoding="utf-8")+ (output / "RESULTS.md").write_text(markdown(report), encoding="utf-8")+ print(json.dumps({+ "pass": {a: {"clean": summary["all"][a]["clean_pass"],+ "runs": summary["all"][a]["runs"]} for a in ARMS},+ "wall": {a: {k: summary["all"][a][k] for k in ("mean_wall_s", "median_wall_s")}+ for a in ARMS},+ "pairs": paired["all"], "failures": len(report["failures"]),+ }, separators=(",", ":")))+ return report+++def main() -> None:+ parser = argparse.ArgumentParser(description=__doc__)+ parser.add_argument("--root", required=True, type=Path)+ parser.add_argument("--output", required=True, type=Path)+ args = parser.parse_args()+ try:+ summarize(args.root, args.output)+ except (OSError, ValueError) as exc:+ parser.exit(1, "report failed: %s\n" % exc)+++if __name__ == "__main__":+ main()diff --git a/graff-evals/test_cursor_compare.py b/graff-evals/test_cursor_compare.pynew file mode 100644index 00000000..c8ddf6ed--- /dev/null+++ b/graff-evals/test_cursor_compare.py@@ -0,0 +1,121 @@+"""Offline admission checks for the opt-in native Cursor comparison."""+import argparse+import copy+import json+import os+from pathlib import Path+import tempfile+import time+import unittest+from unittest.mock import patch++import cursor_compare as compare+import cursor_compare_report as report+++class CursorComparisonTests(unittest.TestCase):+ def test_task_subsets_preserve_catalog_order_and_reject_ambiguous_selection(self):+ data = {"tasks": ["a", "b", "c"], "smoke_tasks": ["b"]}+ self.assertEqual(compare.select_tasks(data, False, ["c", "a"]), ["a", "c"])+ self.assertEqual(compare.select_tasks(data, False, None), data["tasks"])+ self.assertEqual(compare.select_tasks(data, True, None), data["smoke_tasks"])+ for smoke, tasks in ((True, ["a"]), (False, ["a", "a"]), (False, ["missing"])):+ with self.assertRaises(ValueError):+ compare.select_tasks(data, smoke, tasks)++ def test_presets_pin_matched_models_and_existing_tasks(self):+ import run+ data = compare.catalog()+ self.assertEqual(len(data["tasks"]), 21)+ self.assertEqual(len(set(data["tasks"])), 21)+ self.assertTrue(set(data["tasks"]) <= set(run.load_tasks()))+ self.assertTrue(set(data["smoke_tasks"]) <= set(data["tasks"]))+ xai = data["presets"]["xai-sub"]+ self.assertEqual((xai["provider"], xai["models"]["graff"], xai["models"]["cursor"], xai["effort"]),+ ("xai", "grok-4.7", "grok-4.7-high", "high"))+ self.assertEqual(data["presets"]["argon"]["models"]["graff"], "gemini-4-argon")++ def test_cursor_requires_exact_model_and_successful_completion(self):+ rows = [{"type": "system", "model": "Grok 4.7 256K High"},+ {"type": "result", "subtype": "success", "is_error": False, "duration_ms": 2500}]+ stdout = "preamble\n" + "\n".join(json.dumps(r) for r in rows)+ receipt = compare.cursor_receipt(stdout, "Grok 4.7 256K High")+ self.assertTrue(receipt["routing_ok"] and receipt["native_completion_ok"])+ self.assertEqual(receipt["native_task_duration_s"], 2.5)+ self.assertFalse(compare.cursor_receipt(stdout, "Gemini 4 Argon Low")["routing_ok"])+ self.assertFalse(compare.cursor_receipt(json.dumps(rows[0]), "Grok 4.7 256K High")["native_completion_ok"])+ rows[1]["is_error"] = True+ self.assertFalse(compare.cursor_receipt("\n".join(map(json.dumps, rows)), "Grok 4.7 256K High")["native_completion_ok"])++ def test_graff_repl_trace_and_effort_gate_reject_other_routes(self):+ with tempfile.TemporaryDirectory() as tmp:+ root = Path(tmp)+ trace = root / ".graff/traces/trace.jsonl"+ trace.parent.mkdir(parents=True)+ (root / ".eval-stdout.txt").write_text("reasoning effort: high\n")+ trace.write_text(json.dumps({"ev": "api", "agent": "repl", "provider": "xai", "model": "grok-4.7"}) + "\n")+ self.assertTrue(compare.graff_receipt(root, "xai", "grok-4.7", "high")["routing_ok"])+ self.assertFalse(compare.graff_receipt(root, "codegraff", "grok-4.7", "high")["routing_ok"])+ self.assertFalse(compare.graff_receipt(root, "xai", "grok-4.7", "low")["routing_ok"])+ self.assertFalse(compare.graff_receipt(root, "xai", "grok-4.6", "high")["routing_ok"])++ def test_subscription_snapshot_excludes_refresh_and_cleanup_removes_it(self):+ with tempfile.TemporaryDirectory() as tmp:+ root = Path(tmp)+ source = root / "source.json"+ source.write_text(json.dumps({"access_token": "fixture-access", "refresh_token": "fixture-refresh", "expires_at": int(time.time()) + 3600}))+ args = argparse.Namespace(xai_auth=str(source))+ snapshot = compare.access_credentials("xai", args)+ self.assertNotIn("refresh_token", snapshot)+ home = root / "sandbox/.eval-home"+ compare.seed_login(home, "xai", "grok-4.7", snapshot)+ stored = home / ".xai/credentials/graff-oauth.json"+ self.assertEqual(stored.stat().st_mode & 0o777, 0o600)+ compare.cleanup_credentials(root)+ self.assertFalse(stored.exists())+ self.assertTrue(source.is_file())+ source.write_text(json.dumps({"access_token": "fixture-access", "expires_at": 1}))+ with self.assertRaises(ValueError):+ compare.access_credentials("xai", args)++ def test_environment_excludes_api_keys_and_personal_configuration(self):+ with patch.dict(os.environ, {"PATH": "/bin", "XAI_API_KEY": "fixture", "CODEGRAFF_API_KEY": "fixture", "CODEX_HOME": "/private/fixture", "GRAFF_MCP_CONFIG": "/private/mcp"}, clear=True):+ env = compare.safe_environment(Path("/tmp/fixture-cache"))+ self.assertEqual(env["PATH"], "/bin")+ self.assertFalse(set(env) & {"XAI_API_KEY", "CODEGRAFF_API_KEY", "CODEX_HOME", "GRAFF_MCP_CONFIG"})++ def matrix(self):+ meta = {"preset": "xai-sub", "provider": "xai", "models": {"graff": "grok-4.7", "cursor": "grok-4.7-high"},+ "effort": "high", "repetitions": 1, "workers": 2, "tasks": ["task-a", "task-b"], "mode": "full"}+ rows = [{"harness": arm, "task": task, "rep": 1, "suite": "core", "wall_s": 2 if arm == "graff" else 4,+ "artifact_ok": True, "outcome_ok": True, "routing_ok": True, "native_completion_ok": True,+ "exit": 0, "timed_out": False, "verifiers_unchanged": True}+ for task in meta["tasks"] for arm in ("graff", "cursor")]+ return meta, rows++ def test_report_rejects_incomplete_duplicate_or_extra_runs(self):+ meta, rows = self.matrix()+ report.validate(meta, rows)+ report.validate(dict(meta, mode="subset"), rows)+ for altered in (rows[:-1], rows + [rows[0]], rows[:-1] + [dict(rows[-1], task="unexpected")]):+ with self.assertRaises(ValueError):+ report.validate(meta, altered)++ def test_report_pairs_only_clean_runs_and_preserves_failures(self):+ meta, rows = self.matrix()+ rows[-1]["routing_ok"] = False+ paired = report.paired_stats(rows)+ self.assertEqual((paired["pairs"], paired["both_clean_passed"], paired["graff_wall_reduction_percent"]), (2, 1, 50.0))+ with tempfile.TemporaryDirectory() as tmp:+ root = Path(tmp)+ (root / "comparison").mkdir()+ (root / "metadata.json").write_text(json.dumps(meta))+ (root / "comparison/results.jsonl").write_text("\n".join(map(json.dumps, rows)) + "\n")+ result = report.summarize(root, root / "report")+ self.assertEqual(result["summary"]["all"]["cursor"]["clean_pass"], 1)+ self.assertEqual(len(result["failures"]), 1)+ self.assertIsNone(result["summary"]["all"]["graff"]["native_task_duration_mean_s"])+++if __name__ == "__main__":+ unittest.main()diff --git a/graff-evals/test_worker_matrix.py b/graff-evals/test_worker_matrix.pynew file mode 100644index 00000000..113b463e--- /dev/null+++ b/graff-evals/test_worker_matrix.py@@ -0,0 +1,263 @@+from contextlib import redirect_stdout+import io+import json+import os+from pathlib import Path+import tempfile+import unittest+from unittest.mock import patch++import worker_matrix as runner+import worker_matrix_measure as metrics++ROOT = {"provider": "fixture-provider", "model": "root-fixture"}+WORKER = {"provider": "fixture-provider", "model": "worker-fixture"}+RATES = {"input": "2", "cached": "0.2", "write": "2.5", "write_1h": "4",+ "output": "10", "max_input": 200000, "source": "Synthetic test rates"}+++def evidence(worker=False):+ usage = [{"ev": "usage", "from_sub": False, "agent": "main", **ROOT,+ "input_tokens": 100, "cache_read_tokens": 50, "cache_write_tokens": 20,+ "output_tokens": 10}]+ if worker:+ usage += [{"ev": "usage", "from_sub": True, "agent": "child", **WORKER,+ "input_tokens": 20, "output_tokens": 2}]+ terminal = {"type": "turn", "complete": True, "usage_complete": True,+ "input_tokens": sum(r["input_tokens"] for r in usage),+ "output_tokens": sum(r["output_tokens"] for r in usage),+ "api_calls": len(usage), "missing_usage_calls": 0,+ "unreported_failed_attempts": 0, "unpriced_calls": len(usage),+ "subscription_calls": 0, "cost_usd": 0}+ events = [terminal]+ if worker:+ events += [{"type": "agent_route", "provider": WORKER["provider"],+ "resolved_model": WORKER["model"]}, {"type": "agent_usage"}]+ return events, usage + [{"ev": "api", "is_error": False} for _ in usage]+++class Measurements(unittest.TestCase):+ def test_complete_tokens_do_not_make_unpriced_calls_free(self):+ events, traces = evidence()+ result = metrics.measure(events, traces, {"root": ROOT}, 1)+ self.assertTrue(result["token_usage_complete"])+ self.assertFalse(result["native_cost_complete"])+ self.assertIsNone(result["native_full_cost_usd"])+ self.assertEqual(result["native_known_subtotal_usd"], 0)+ self.assertIsNone(result["api_equivalent_estimate_usd"])++ def test_explicit_rates_split_roles_and_cache_writes(self):+ events, traces = evidence(True)+ arm = {"root": ROOT, "worker": WORKER, "prices": {"root": RATES, "worker": RATES}}+ result = metrics.measure(events, traces, arm, 1)+ self.assertTrue(result["routing_ok"])+ self.assertTrue(result["delegation_ok"])+ self.assertEqual(result["estimated_role_costs_usd"]["root"], "0.00022")+ self.assertEqual(result["api_equivalent_estimate_usd"], "0.00028")+ traces[0]["cache_ttl"] = "1h"+ self.assertEqual(metrics.measure(events, traces, arm, 1)["estimated_role_costs_usd"]["root"], "0.00025")++ def test_missing_failed_or_malformed_usage_is_unknown(self):+ for field in ("missing_usage_calls", "unreported_failed_attempts"):+ events, traces = evidence()+ events[0][field] = 1+ result = metrics.measure(events, traces, {"root": ROOT, "prices": {"root": RATES}}, 1)+ self.assertFalse(result["token_usage_complete"])+ self.assertIsNone(result["api_equivalent_estimate_usd"])+ events, traces = evidence()+ self.assertFalse(metrics.measure(events, traces, {"root": ROOT}, 1, malformed=1)["token_usage_complete"])+ traces[0]["input_tokens"] = -1+ self.assertFalse(metrics.measure(events, traces, {"root": ROOT}, 1)["token_usage_complete"])++ def test_missing_or_boolean_accounting_counters_do_not_qualify(self):+ events, traces = evidence()+ events[0]["unpriced_calls"] = True+ self.assertFalse(metrics.measure(events, traces, {"root": ROOT}, 1)["token_usage_complete"])+ del events[0]["unpriced_calls"]+ self.assertFalse(metrics.measure(events, traces, {"root": ROOT}, 1)["token_usage_complete"])++ def test_subscription_and_wrong_routes_never_get_full_native_cost(self):+ events, traces = evidence()+ events[0].update(unpriced_calls=0, subscription_calls=1)+ self.assertIsNone(metrics.measure(events, traces, {"root": ROOT}, 1)["native_full_cost_usd"])+ traces[0]["provider"] = "wrong-fixture"+ self.assertFalse(metrics.measure(events, traces, {"root": ROOT}, 1)["routing_ok"])++ def test_missing_workers_cannot_qualify_as_a_team(self):+ events, traces = evidence(True)+ arm = {"root": ROOT, "worker": WORKER}+ self.assertFalse(metrics.measure(events, traces, arm, 3)["delegation_ok"])+ events.pop()+ self.assertFalse(metrics.measure(events, traces, arm, 1)["delegation_ok"])+ self.assertFalse(metrics.measure(events, traces, {"root": ROOT}, 1)["routing_ok"])++ def test_request_band_limit_not_aggregate_tokens(self):+ _, traces = evidence()+ rates = {**RATES, "max_input": 100}+ self.assertIsNotNone(metrics.price_rows([traces[0], traces[0]], rates))+ self.assertIsNone(metrics.price_rows([traces[0]], {**rates, "max_input": 99}))++ def test_incomplete_trial_keeps_full_mean_unknown(self):+ trials = [{"task": "fixture", "arm": "solo", "passed": True, "seconds": 1,+ "native_full_cost_usd": "0.1", "api_equivalent_estimate_usd": "0.2"},+ {"task": "fixture", "arm": "solo", "passed": False, "seconds": 3,+ "native_full_cost_usd": None, "api_equivalent_estimate_usd": "0.4"}]+ group = metrics.summarize(trials)[0]+ self.assertEqual(group["passed"], 1)+ self.assertEqual(group["mean_seconds"], 2)+ self.assertIsNone(group["mean_native_full_cost_usd"])+ self.assertEqual(group["mean_api_equivalent_estimate_usd"], "0.3")++ def test_summary_splits_roles_and_includes_failed_trials(self):+ trials = [+ {"task": "fixture", "arm": "team", "passed": passed, "seconds": seconds,+ "native_full_cost_usd": None, "api_equivalent_estimate_usd": total,+ "estimated_role_costs_usd": {"root": root, "worker": worker}}+ for passed, seconds, root, worker, total in (+ (True, 2, "0.1", "0.2", "0.3"),+ (False, 4, "0.3", "0.4", "0.7"))]+ group = metrics.summarize(trials)[0]+ self.assertEqual(group["attempted"], 2)+ self.assertEqual(group["passed"], 1)+ self.assertEqual(group["mean_seconds"], 3)+ self.assertEqual(group["mean_estimated_role_costs_usd"], {"root": "0.2", "worker": "0.3"})+ self.assertEqual(group["mean_api_equivalent_estimate_usd"], "0.5")+ self.assertIsNone(group["mean_native_full_cost_usd"])++ def test_summary_preserves_unknown_roles_and_explicit_solo_zero(self):+ def trial(roles):+ return {"task": "fixture", "arm": "solo", "passed": True, "seconds": 1,+ "native_full_cost_usd": "0.1", "api_equivalent_estimate_usd": None,+ "estimated_role_costs_usd": roles}+ for roles in ({}, {"root": None, "worker": None}, {"root": "0.2"}):+ group = metrics.summarize([trial({"root": "0.1", "worker": "0"}), trial(roles)])[0]+ self.assertIsNone(group["mean_estimated_role_costs_usd"]["worker"])+ if roles.get("root") is None:+ self.assertIsNone(group["mean_estimated_role_costs_usd"]["root"])+ group = metrics.summarize([trial({"root": "0.1", "worker": "0"})])[0]+ self.assertEqual(group["mean_estimated_role_costs_usd"], {"root": "0.1", "worker": "0"})+ legacy = trial({})+ del legacy["estimated_role_costs_usd"]+ self.assertEqual(metrics.summarize([legacy])[0]["mean_estimated_role_costs_usd"],+ {"root": None, "worker": None})++ def test_saved_summary_never_executes_or_changes_results(self):+ with tempfile.TemporaryDirectory() as temp:+ path = Path(temp) / "results.json"+ trials = [{"task": "fixture", "arm": "solo", "passed": True, "seconds": 2,+ "native_full_cost_usd": None, "api_equivalent_estimate_usd": "0.1",+ "estimated_role_costs_usd": {"root": "0.1", "worker": "0"}}]+ original = json.dumps(trials)+ path.write_text(original)+ output = io.StringIO()+ with patch("sys.argv", ["worker_matrix.py", "--summarize", temp]), \+ patch.object(runner, "batch", side_effect=AssertionError("No execution")), \+ patch.object(runner, "prepare", side_effect=AssertionError("No preparation")), \+ patch.object(runner.subprocess, "Popen", side_effect=AssertionError("No process")), \+ redirect_stdout(output):+ runner.main()+ report = json.loads(output.getvalue())+ self.assertEqual(report["groups"][0]["mean_estimated_role_costs_usd"],+ {"root": "0.1", "worker": "0"})+ self.assertEqual(path.read_text(), original)+ self.assertEqual(list(Path(temp).iterdir()), [path])++ def test_saved_summary_rejects_execution_flags(self):+ for flags in (["--run", "unused"], ["--manifest", "unused"],+ ["--binary", "unused"], ["--output-root", "unused"]):+ with patch("sys.argv", ["worker_matrix.py", "--summarize", "unused", *flags]), \+ patch.object(runner, "batch", side_effect=AssertionError("No execution")), \+ patch.object(runner, "prepare", side_effect=AssertionError("No preparation")), \+ redirect_stdout(io.StringIO()):+ with self.assertRaises(SystemExit) as raised:+ runner.main()+ self.assertEqual(raised.exception.code, 2)++ def test_two_routes_solo_and_same_route_teams_schedule_eight_trials(self):+ arms = [+ {"id": kind + "-" + label, "root": route,+ **({"worker": route} if kind == "team" else {})}+ for label, route in (("a", ROOT), ("b", WORKER))+ for kind in ("solo", "team")]+ manifest = {"repetitions": 2, "max_model_calls": 8, "max_tool_calls": 20,+ "cost_stop_usd": "1", "auth_env": [], "auth_files": [],+ "tasks": [{"team": "sa-fix-three", "solo": "solo-fix-three", "workers": 3}],+ "arms": arms}+ runner.validate(manifest)+ slots = runner.schedule(manifest)+ self.assertEqual(len(slots), 8)+ self.assertEqual({(r["rep"], r["arm"]) for r in slots},+ {(rep, arm["id"]) for rep in (1, 2) for arm in arms})+ self.assertTrue(all(r["task"] == "sa-fix-three" for r in slots))+ self.assertEqual([r["arm"] for r in slots[:4]], [a["id"] for a in arms])+ self.assertEqual([r["arm"] for r in slots[4:]], [a["id"] for a in arms[1:] + arms[:1]])+ self.assertTrue(all(a["worker"] == a["root"] for a in arms if "worker" in a))++[email protected](os.name == "posix", "Local process session runner")+class LocalRunner(unittest.TestCase):+ def test_frozen_opt_in_execution_and_no_reruns(self):+ with tempfile.TemporaryDirectory() as temp:+ base = Path(temp)+ evals = base / "evals"+ fixture = evals / "fixtures" / "small"+ fixture.mkdir(parents=True)+ (evals / "tasks").mkdir()+ (evals / "hidden").mkdir()+ (fixture / "value.py").write_text("value = 0\n")+ verifier = "from value import value\nassert value == 1\n"+ (fixture / "test_value.py").write_text(verifier)+ (evals / "hidden" / "small.py").write_text(+ "import sys\nsys.path.insert(0, '.')\n" + verifier)+ team = {"id": "team", "suite": "subagents", "files_dir": "fixtures/small",+ "prompt": "Fix value", "timeout_s": 30,+ "check": 'python3 test_value.py && python3 "$TASK_ROOT/hidden/small.py"'}+ solo = {**team, "id": "solo", "suite": "subagents-solo", "counterfactual_of": "team"}+ for task in (team, solo):+ (evals / "tasks" / (task["id"] + ".json")).write_text(json.dumps(task))+ binary = base / "stub"+ events, traces = evidence()+ stub = (+ "#!/usr/bin/env python3\nimport json,sys\nfrom pathlib import Path\n"+ "sys.stdin.readline()\nPath('value.py').write_text('value = 1\\n')\n"+ "p=Path('.graff/traces');p.mkdir(parents=True)\n"+ f"rows={traces!r}\n"+ "(p/'fixture.jsonl').write_text(''.join(json.dumps(r)+'\\n' for r in rows))\n"+ f"events={events!r}\n"+ "for e in events: print(json.dumps(e))\n")+ binary.write_text(stub)+ binary.chmod(0o700)+ manifest = {"repetitions": 2, "max_model_calls": 8, "max_tool_calls": 20,+ "cost_stop_usd": "1", "tasks": [{"team": "team", "solo": "solo", "workers": 1}],+ "arms": [{"id": "solo", "root": ROOT, "prices": {"root": RATES}}]}+ config = base / "config.json"+ config.write_text(json.dumps(manifest))+ output = base / "private"+ with patch.object(runner, "EVALS", evals):+ runner.prepare(config, binary, output)+ self.assertFalse((output / "runs").exists())+ runner.batch(output)+ results = json.loads((output / "results.json").read_text())+ self.assertEqual(len(results), 2)+ self.assertTrue(all(r["passed"] for r in results))+ self.assertTrue(all(r["native_full_cost_usd"] is None for r in results))+ with self.assertRaises(ValueError):+ runner.batch(output)+ with self.assertRaises(FileExistsError):+ runner.prepare(config, binary, output)+ for path in output.rglob("*"):+ want = 0o700 if path.is_dir() or path == output / "graff" else 0o600+ self.assertEqual(path.stat().st_mode & 0o777, want)++ def test_manifest_rejects_unbounded_calls_and_auth_path_traversal(self):+ manifest = {"repetitions": 1, "max_model_calls": 0, "max_tool_calls": 20,+ "cost_stop_usd": "1", "tasks": [{"team": "team", "solo": "solo", "workers": 1}],+ "arms": [{"id": "solo", "root": ROOT}]}+ with self.assertRaises(ValueError):+ runner.validate(manifest)+ with self.assertRaises(ValueError):+ runner.relative("../escape")+++if __name__ == "__main__":+ unittest.main()diff --git a/graff-evals/worker_matrix.py b/graff-evals/worker_matrix.pynew file mode 100644index 00000000..f5def281--- /dev/null+++ b/graff-evals/worker_matrix.py@@ -0,0 +1,315 @@+#!/usr/bin/env python3+"""Prepare or run an opt-in private root/worker matrix on paired fixture tasks."""+import argparse+from decimal import Decimal+import hashlib+import json+import os+from pathlib import Path+import re+import shutil+import signal+import subprocess+import sys+import time++import measurement+import worker_matrix_measure as metrics++EVALS = Path(__file__).resolve().parent+REPO = EVALS.parent+NAME = re.compile(r"^[A-Za-z0-9_.-]+$")+LIMITS = "Work only inside this fixture workspace. Never edit tests, read parent directories or credentials, access external services, install anything, use MCP or peer messaging, invoke other coding CLIs, create goals, commit/push, or change model/provider/effort. Do not set model/provider/tier overrides for workers. No nested or replacement workers."+++def digest(path):+ return hashlib.sha256(path.read_bytes()).hexdigest()+++def save(path, data):+ path.write_text(json.dumps(data, indent=2) + "\n")+ path.chmod(0o600)+++def relative(value):+ path = Path(value)+ if path.is_absolute() or ".." in path.parts or not path.parts:+ raise ValueError("Expected a nonempty relative path")+ return path+++def validate(manifest):+ for key in ("repetitions", "max_model_calls", "max_tool_calls"):+ if type(manifest[key]) is not int or manifest[key] < 1:+ raise ValueError(f"{key} must be a positive integer")+ arms = manifest["arms"]+ if not arms or len({a["id"] for a in arms}) != len(arms):+ raise ValueError("Arms must have unique ids")+ for arm in arms:+ if not NAME.fullmatch(arm["id"]):+ raise ValueError("Unsafe arm id")+ for route in (arm["root"], arm.get("worker")):+ if route is not None and any(not isinstance(route[k], str) or not route[k]+ or any(c.isspace() for c in route[k])+ for k in ("provider", "model")):+ raise ValueError("Routes require explicit provider and model")+ for rates in arm.get("prices", {}).values():+ if not rates.get("source") or type(rates["max_input"]) is not int or rates["max_input"] < 1:+ raise ValueError("Rates require a source and positive per-request input limit")+ for key in ("input", "cached", "write", "write_1h", "output"):+ value = Decimal(str(rates[key]))+ if not value.is_finite() or value < 0:+ raise ValueError("Rates must be finite and nonnegative")+ if not manifest["tasks"] or len({p["team"] for p in manifest["tasks"]}) != len(manifest["tasks"]):+ raise ValueError("Task pairs must be nonempty and unique")+ for pair in manifest["tasks"]:+ if not all(NAME.fullmatch(pair[k]) for k in ("team", "solo")):+ raise ValueError("Unsafe task id")+ if type(pair["workers"]) is not int or pair["workers"] < 1:+ raise ValueError("Each task requires a positive worker count")+ for entry in manifest.get("auth_files", []):+ relative(entry["destination"])+ if not Path(entry["source"]).expanduser().is_file():+ raise ValueError("Explicit authentication file missing")+ for name in manifest.get("auth_env", []):+ if not re.fullmatch(r"[A-Z][A-Z0-9_]*", name) or not os.environ.get(name):+ raise ValueError("Explicit authentication environment variable missing")+ limit = Decimal(str(manifest["cost_stop_usd"]))+ if not limit.is_finite() or limit <= 0:+ raise ValueError("cost_stop_usd must be positive")+++def check(workspace, task, task_root, env):+ check_env = {**env, "TASK_ROOT": str(task_root)}+ try:+ result = subprocess.run(task["check"], shell=True, cwd=workspace, env=check_env,+ capture_output=True, text=True, timeout=60)+ return {"returncode": result.returncode, "stdout": result.stdout, "stderr": result.stderr}+ except subprocess.TimeoutExpired:+ return {"returncode": None, "timed_out": True}+++def schedule(manifest):+ slots = []+ for rep in range(manifest["repetitions"]):+ for index, pair in enumerate(manifest["tasks"]):+ rotation = (rep + index) % len(manifest["arms"])+ arms = manifest["arms"][rotation:] + manifest["arms"][:rotation]+ slots.extend({"task": pair["team"], "rep": rep + 1, "arm": a["id"]} for a in arms)+ return slots+++def prepare(manifest_path, binary, root):+ manifest = json.loads(manifest_path.read_text())+ validate(manifest)+ if root.is_relative_to(REPO):+ raise ValueError("Output must be outside the repository")+ tasks = {t["id"]: t for p in (EVALS / "tasks").glob("*.json")+ if (t := json.loads(p.read_text())).get("suite") in ("subagents", "subagents-solo")}+ root.mkdir(mode=0o700)+ save(root / "manifest.json", manifest)+ shutil.copyfile(binary, root / "graff")+ (root / "graff").chmod(0o700)+ receipt = {"schedule": schedule(manifest), "binary_sha256": digest(root / "graff"),+ "manifest_sha256": digest(root / "manifest.json"), "tasks": {},+ "runner_sha256": digest(Path(__file__)),+ "measurement_sha256": digest(Path(metrics.__file__))}+ for pair in manifest["tasks"]:+ team, solo = tasks[pair["team"]], tasks[pair["solo"]]+ if (team["suite"] != "subagents" or solo["suite"] != "subagents-solo"+ or solo.get("counterfactual_of") != team["id"]+ or team["files_dir"] != solo["files_dir"] or team["check"] != solo["check"]):+ raise ValueError("Counterfactual tasks must share fixture and grader")+ folder = root / "inputs" / pair["team"]+ folder.mkdir(parents=True, mode=0o700)+ fixture = EVALS / relative(team["files_dir"])+ if not fixture.resolve().is_relative_to(EVALS.resolve()):+ raise ValueError("Fixture must be inside the eval tree")+ shutil.copytree(fixture, folder / "fixture",+ ignore=shutil.ignore_patterns("__pycache__", "*.pyc", ".git"))+ graders = re.findall(r'\$TASK_ROOT/(hidden/[^\s"\x27;]+)', team["check"])+ if not graders:+ raise ValueError("Paired task requires a held-out grader")+ for name in graders:+ destination = folder / relative(name)+ destination.parent.mkdir(parents=True, exist_ok=True, mode=0o700)+ shutil.copyfile(EVALS / relative(name), destination)+ frozen = {**pair, "team_task": team, "solo_task": solo}+ save(folder / "task.json", frozen)+ baseline = check(folder / "fixture", team, folder, {"PATH": os.environ.get("PATH", "")})+ save(folder / "initial-check.json", baseline)+ if baseline["returncode"] == 0:+ raise ValueError("Fixture unexpectedly already solved")+ receipt["tasks"][pair["team"]] = {+ str(p.relative_to(folder)): digest(p) for p in folder.rglob("*")+ if p.is_file() and "__pycache__" not in p.parts}+ save(root / "receipt.json", receipt)+ for p in root.rglob("*"):+ p.chmod(0o700 if p.is_dir() or p == root / "graff" else 0o600)+++def run_one(root, manifest, receipt, slot):+ inputs = root / "inputs" / slot["task"]+ for name, expected in receipt["tasks"][slot["task"]].items():+ if digest(inputs / name) != expected:+ raise ValueError("Frozen task input changed")+ task_data = json.loads((inputs / "task.json").read_text())+ arm = next(a for a in manifest["arms"] if a["id"] == slot["arm"])+ task = task_data["team_task" if arm.get("worker") else "solo_task"]+ output = root / "runs" / f'{slot["task"]}-r{slot["rep"]}-{slot["arm"]}'+ output.mkdir(parents=True, mode=0o700)+ workspace, home = output / "workspace", output / "home"+ shutil.copytree(inputs / "fixture", workspace, ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))+ measurement.isolate_git(str(workspace))+ protected = measurement.verifier_snapshot(str(workspace), task, str(inputs))+ if task_data.get("freeze_all_inputs"):+ protected.update({str(workspace / name.removeprefix("fixture/")): value+ for name, value in receipt["tasks"][slot["task"]].items()+ if name.startswith("fixture/")})+ home.mkdir(mode=0o700)+ for entry in manifest.get("auth_files", []):+ destination = home / relative(entry["destination"])+ destination.parent.mkdir(parents=True, exist_ok=True, mode=0o700)+ shutil.copyfile(Path(entry["source"]).expanduser(), destination)+ destination.chmod(0o600)+ empty_mcp = home / "empty-mcp.json"+ empty_mcp.write_text('{"mcpServers":{}}')+ env = {k: os.environ[k] for k in ("PATH", "LANG", "LC_ALL", *manifest.get("auth_env", []))+ if k in os.environ}+ env.update(HOME=str(home), USERPROFILE=str(home), PWD=str(workspace),+ XDG_CONFIG_HOME=str(home / ".config"), XDG_CACHE_HOME=str(home / ".cache"),+ GRAFF_MCP_CONFIG=str(empty_mcp), GRAFF_FLEET="off", GRAFF_NO_PLUGINS="1",+ GRAFF_NO_TELEMETRY="1", GRAFF_BEHAVIOR_UPLOAD="off", GRAFF_NO_SMOLIFY="1",+ GRAFF_LEARNING_PRIVACY="local", NO_COLOR="1")+ root_route = arm["root"]+ command = [str(root / "graff"), "--json", "--old", "--yolo", "--no-lean",+ "--model", root_route["provider"] + "/" + root_route["model"],+ "--no-subagent-tier", "--no-telemetry", "--learning-privacy", "local",+ "--max-model-calls", str(manifest["max_model_calls"]),+ "--max-run-tool-calls", str(manifest["max_tool_calls"])]+ worker = arm.get("worker")+ if worker:+ command += ["--subagent-provider", worker["provider"], "--subagent-model", worker["model"]]+ if worker["provider"] != root_route["provider"]:+ command.append("--allow-cross-provider-subagents")+ policy = (f"Use exactly {task_data['workers']} background workers. Launch all before collecting "+ "them, do independent root work meanwhile, collect actual results with agent_output, "+ "review and repair their output yourself, and run visible tests. Pass on all limits."+ if worker else "Do all work yourself. Do not spawn subagents or workflows.")+ prompt = task["prompt"] + "\n\n" + LIMITS + "\n" + policy+ save(output / "invocation.json", {"command": command, "prompt": prompt})+ start = time.perf_counter()+ process = subprocess.Popen(command, cwd=workspace, env=env, stdin=subprocess.PIPE,+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,+ start_new_session=True)+ save(output / "process.json", {"pid": process.pid})+ timed_out = False+ try:+ stdout, stderr = process.communicate(json.dumps({"type": "user", "text": prompt}) + "\n",+ timeout=task["timeout_s"])+ except subprocess.TimeoutExpired:+ timed_out = True+ os.killpg(process.pid, signal.SIGTERM)+ try:+ stdout, stderr = process.communicate(timeout=10)+ except subprocess.TimeoutExpired:+ os.killpg(process.pid, signal.SIGKILL)+ stdout, stderr = process.communicate()+ seconds = time.perf_counter() - start+ measurement.kill_session(process.pid)+ (output / "stdout.jsonl").write_text(stdout)+ (output / "stderr.txt").write_text(stderr)+ events, malformed = metrics.read_rows(output / "stdout.jsonl")+ traces = []+ for path in (workspace / ".graff/traces").glob("*.jsonl"):+ rows, errors = metrics.read_rows(path)+ traces.extend(rows)+ malformed += errors+ observed = metrics.measure(events, traces, arm, task_data["workers"], malformed)+ checks = check(workspace, task, inputs, {"PATH": os.environ.get("PATH", "")})+ verifiers_ok = measurement.verifiers_unchanged(protected)+ result = {**slot, **observed, "seconds": seconds, "returncode": process.returncode,+ "timed_out": timed_out, "check": checks, "verifiers_unchanged": verifiers_ok}+ result["passed"] = bool(checks["returncode"] == 0 and verifiers_ok and observed["routing_ok"]+ and observed["delegation_ok"] and observed["turn_complete"]+ and process.returncode == 0 and not timed_out)+ save(output / "result.json", result)+ return result+++def batch(root):+ manifest = json.loads((root / "manifest.json").read_text())+ validate(manifest)+ receipt = json.loads((root / "receipt.json").read_text())+ if (root / "results.json").exists() or (root / "runs").exists():+ raise ValueError("Never restart or overwrite an attempted matrix")+ for path, field in ((root / "graff", "binary_sha256"),+ (root / "manifest.json", "manifest_sha256"),+ (Path(__file__), "runner_sha256"),+ (Path(metrics.__file__), "measurement_sha256")):+ if digest(path) != receipt[field]:+ raise ValueError("Prepared executable, config or measurement changed")+ results, spent = [], Decimal(0)+ for slot in receipt["schedule"]:+ if digest(root / "graff") != receipt["binary_sha256"]:+ raise ValueError("Prepared executable changed")+ result = run_one(root, manifest, receipt, slot)+ results.append(result)+ save(root / "results.json", results)+ save(root / "summary.json", {"groups": metrics.summarize(results),+ "limitations": "Exploratory paired fixtures; native reasoning defaults, uncontrolled cache. Costs include failed trials; incomplete means stay unknown."})+ print(json.dumps({k: result[k] for k in ("task", "rep", "arm", "passed", "seconds",+ "native_full_cost_usd", "api_equivalent_estimate_usd")}),+ flush=True)+ reason = None+ cost = result["native_full_cost_usd"]+ if cost is None:+ cost = result["api_equivalent_estimate_usd"]+ if not result["routing_ok"] or not result["token_usage_complete"]:+ reason = "Route mismatch or incomplete token usage"+ elif cost is None:+ reason = "Unknown cost; supply a documented hypothetical rate snapshot separately"+ else:+ spent += Decimal(str(cost))+ if spent >= Decimal(str(manifest["cost_stop_usd"])):+ reason = "Between-run cost stop; not a provider-side hard cap"+ if reason:+ save(root / "stopped.json", {"after": slot, "reason": reason})+ break+ save(root / "completion.json", {"attempted": len(results), "planned": len(receipt["schedule"]),+ "budget_accounting_subtotal_usd": str(spent),+ "budget_accounting": "Native full cost when known, otherwise explicit rate estimate; never a settled bill."})+ for path in root.rglob("*"):+ if not path.is_symlink():+ path.chmod(0o700 if path.is_dir() or path == root / "graff" else 0o600)+++def main():+ os.umask(0o077)+ if os.name != "posix":+ raise SystemExit("This local fixture runner requires POSIX process sessions")+ parser = argparse.ArgumentParser(description=__doc__)+ parser.add_argument("--manifest", type=Path, help="private matrix manifest (prepare only)")+ parser.add_argument("--binary", type=Path, help="explicit installed executable to freeze")+ parser.add_argument("--output-root", type=Path, help="new private directory outside the repository")+ parser.add_argument("--run", type=Path, help="explicit opt-in to execute a prepared matrix")+ parser.add_argument("--summarize", type=Path, help="read saved results offline; never execute models")+ args = parser.parse_args()+ if args.summarize and not any((args.run, args.manifest, args.binary, args.output_root)):+ results = json.loads((args.summarize.expanduser().resolve() / "results.json").read_text())+ print(json.dumps({+ "groups": metrics.summarize(results),+ "limitations": "Offline summary of saved results, not fresh validation. Role costs are explicit rate estimates, not settled charges. Includes failed trials; unknown costs remain unknown.",+ }, indent=2))+ elif args.run and not args.summarize and not any((args.manifest, args.binary, args.output_root)):+ batch(args.run.expanduser().resolve())+ elif not args.run and not args.summarize and all((args.manifest, args.binary, args.output_root)):+ root = args.output_root.expanduser().resolve()+ prepare(args.manifest.expanduser().resolve(), args.binary.expanduser().resolve(), root)+ print(root)+ else:+ parser.error("Use --manifest/--binary/--output-root to prepare, --run to execute, or --summarize to read saved results")+++if __name__ == "__main__":+ main()diff --git a/graff-evals/worker_matrix_measure.py b/graff-evals/worker_matrix_measure.pynew file mode 100644index 00000000..60699c9c--- /dev/null+++ b/graff-evals/worker_matrix_measure.py@@ -0,0 +1,144 @@+"""Measurement for private root/worker comparisons; no inferred model prices."""+from decimal import Decimal+import json+import statistics+++def read_rows(path):+ rows, malformed = [], 0+ for line in path.read_text(errors="replace").splitlines():+ if not line.strip():+ continue+ try:+ value = json.loads(line)+ if not isinstance(value, dict):+ raise ValueError("Expected an event object")+ rows.append(value)+ except ValueError:+ malformed += 1+ return rows, malformed+++def price_rows(rows, rates):+ if not rows:+ return Decimal(0)+ if rates is None:+ return None+ total = Decimal(0)+ for row in rows:+ incoming, outgoing = row["input_tokens"], row["output_tokens"]+ cached, written = row.get("cache_read_tokens", 0), row.get("cache_write_tokens", 0)+ if (min(incoming, outgoing, cached, written) < 0 or cached + written > incoming+ or incoming > rates["max_input"]):+ return None+ write = rates["write_1h"] if row.get("cache_ttl") == "1h" else rates["write"]+ total += (+ Decimal(incoming - cached - written) * Decimal(rates["input"])+ + Decimal(cached) * Decimal(rates["cached"])+ + Decimal(written) * Decimal(write)+ + Decimal(outgoing) * Decimal(rates["output"])+ ) / 1000000+ return total+++def measure(events, traces, arm, worker_count, malformed=0):+ terminal = next((e for e in reversed(events) if e.get("type") == "turn"), {})+ usage = [r for r in traces if r.get("ev") == "usage"]+ api = [r for r in traces if r.get("ev") == "api"]+ roots = [r for r in usage if r.get("from_sub") is False]+ children = [r for r in usage if r.get("from_sub") is True]+ routes = [e for e in events if e.get("type") == "agent_route"]+ completions = [e for e in events if e.get("type") == "agent_usage"]+ root, worker = arm["root"], arm.get("worker")++ def identity(row, wanted, model_key="model"):+ return wanted is not None and row.get("provider") == wanted["provider"] and row.get(model_key) == wanted["model"]++ routing_ok = (+ bool(roots) and len(roots) + len(children) == len(usage)+ and all(identity(r, root) for r in roots)+ and all(identity(r, worker) for r in children)+ and all(identity(r, worker, "resolved_model") for r in routes))+ if worker:+ # Each routed worker needs an observed response, not just a spawn receipt.+ labels = {r.get("agent") for r in children}+ delegation_ok = (len(routes) == len(completions) == worker_count+ and len(labels - {None}) == worker_count)+ else:+ delegation_ok = not children and not routes and not any(+ r.get("ev") == "tool" and r.get("name") in ("subagent", "workflow")+ for r in traces)+ valid_tokens = all(+ all(type(r.get(k, 0)) is int and r.get(k, 0) >= 0 for k in (+ "input_tokens", "output_tokens", "cache_read_tokens", "cache_write_tokens"))+ and "input_tokens" in r and "output_tokens" in r+ and r.get("cache_read_tokens", 0) + r.get("cache_write_tokens", 0) <= r["input_tokens"]+ for r in usage)+ valid_counters = all(type(terminal.get(k)) is int and terminal[k] >= 0 for k in (+ "api_calls", "input_tokens", "output_tokens", "missing_usage_calls",+ "unreported_failed_attempts", "unpriced_calls", "subscription_calls"))+ token_complete = bool(+ not malformed and usage and valid_tokens and valid_counters+ and len(api) == len(usage) == terminal.get("api_calls")+ and not any(r.get("is_error") for r in api)+ and terminal.get("missing_usage_calls") == 0+ and terminal.get("unreported_failed_attempts") == 0+ and terminal.get("input_tokens") == sum(r["input_tokens"] for r in usage)+ and terminal.get("output_tokens") == sum(r["output_tokens"] for r in usage))+ native = terminal.get("cost_usd")+ numeric_native = type(native) in (int, float) and Decimal(str(native)).is_finite() and native >= 0+ native_complete = bool(+ token_complete and routing_ok and numeric_native+ and terminal.get("usage_complete") is True+ and terminal.get("unpriced_calls") == 0+ and terminal.get("subscription_calls") == 0)+ estimates = {}+ if token_complete and routing_ok:+ for role, rows in (("root", roots), ("worker", children)):+ value = price_rows(rows, arm.get("prices", {}).get(role))+ estimates[role] = str(value) if value is not None else None+ full_estimate = None+ if estimates and all(v is not None for v in estimates.values()):+ full_estimate = str(sum((Decimal(v) for v in estimates.values()), Decimal(0)))+ return {+ "routing_ok": bool(routing_ok), "delegation_ok": bool(delegation_ok),+ "turn_complete": terminal.get("complete") is True,+ "token_usage_complete": token_complete, "native_cost_complete": native_complete,+ "native_full_cost_usd": native if native_complete else None,+ "native_known_subtotal_usd": native if numeric_native else None,+ "unpriced_calls": terminal.get("unpriced_calls"),+ "subscription_calls": terminal.get("subscription_calls"),+ "missing_usage_calls": terminal.get("missing_usage_calls"),+ "unreported_failed_attempts": terminal.get("unreported_failed_attempts"),+ "api_attempts": len(api), "failed_api_attempts": sum(bool(r.get("is_error")) for r in api),+ "root_calls": len(roots), "worker_calls": len(children),+ "worker_routes": routes, "worker_completions": completions,+ "estimated_role_costs_usd": estimates, "api_equivalent_estimate_usd": full_estimate,+ "pricing_assumptions": arm.get("prices", {}),+ "known_tokens": {+ role: {field: sum(r.get(field, 0) for r in selected)+ for field in ("input_tokens", "cache_read_tokens", "cache_write_tokens", "output_tokens")}+ for role, selected in (("root", roots), ("workers", children))+ } if valid_tokens else {},+ }+++def summarize(results):+ groups = {}+ for key in sorted({(r["task"], r["arm"]) for r in results}):+ trials = [r for r in results if (r["task"], r["arm"]) == key]+ group = {"task": key[0], "arm": key[1], "attempted": len(trials),+ "passed": sum(r["passed"] for r in trials),+ "mean_seconds": statistics.mean(r["seconds"] for r in trials)}+ for field in ("native_full_cost_usd", "api_equivalent_estimate_usd"):+ values = [Decimal(str(r[field])) for r in trials if r[field] is not None]+ group["mean_" + field] = str(sum(values, Decimal(0)) / len(trials)) if len(values) == len(trials) else None+ role_means = {}+ for role in ("root", "worker"):+ values = [r.get("estimated_role_costs_usd", {}).get(role) for r in trials]+ role_means[role] = (+ str(sum((Decimal(str(v)) for v in values), Decimal(0)) / len(trials))+ if all(v is not None for v in values) else None)+ group["mean_estimated_role_costs_usd"] = role_means+ groups[key] = group+ return list(groups.values())diff --git a/lean-proofs/Graff/Provider.lean b/lean-proofs/Graff/Provider.leanindex d83aec38..46a74f27 100644--- a/lean-proofs/Graff/Provider.lean+++ b/lean-proofs/Graff/Provider.lean@@ -25,6 +25,7 @@ inductive Login where| xaiDevice| zaiCli| chatgptBrowser+ | anthropicBrowserderiving DecidableEq, Repr, BEqinductive Catalog where@@ -46,7 +47,7 @@ structure Row wherederiving Repr, BEqdef specs : List Row :=- [ { id := "anthropic", kind := .anthropic, auth := .xApiKey, catalog := .anthropic }+ [ { id := "anthropic", kind := .anthropic, auth := .xApiKey, login := .anthropicBrowser, catalog := .anthropic, subLogin := true }, { id := "codegraff", kind := .openai, auth := .bearer, login := .codegraffDevice, catalog := .openai, takesEffort := true }, { id := "deepseek", kind := .openai, auth := .bearer, takesEffort := true }, { id := "openai", kind := .responses, auth := .bearer }diff --git a/scripts/eval/tier1-manifest.json b/scripts/eval/tier1-manifest.jsonindex 1739661e..77167433 100644--- a/scripts/eval/tier1-manifest.json+++ b/scripts/eval/tier1-manifest.json@@ -14,7 +14,7 @@"src/main.zig","src/repl.zig"],- "test_count_baseline": 3315,+ "test_count_baseline": 3373,"test_count_slack": 25,"required_invariants": [{diff --git a/scripts/test-acp-preauth.py b/scripts/test-acp-preauth.pyindex 8f89f073..f0369ac1 100644--- a/scripts/test-acp-preauth.py+++ b/scripts/test-acp-preauth.py@@ -119,7 +119,14 @@ def main() -> None:"description": "Interactive terminal login (codegraff / Codex / Kimi)","type": "terminal","args": ["login"],- }+ },+ {+ "id": "graff-login-anthropic",+ "name": "graff login anthropic",+ "description": "Claude subscription browser sign-in (alias: ant)",+ "type": "terminal",+ "args": ["login", "anthropic"],+ },]if responses[0].get("id") != 1 or result.get("protocolVersion") != 1:fail("initialize response did not correlate or negotiate ACP v1", proc)diff --git a/scripts/test-mcp-server.py b/scripts/test-mcp-server.pyindex fb2a0635..a5bcbc57 100644--- a/scripts/test-mcp-server.py+++ b/scripts/test-mcp-server.py@@ -89,6 +89,55 @@ def main():assert model.requests, "task never reached the scripted model"finally:model.stop()+ # Caller context: short text rides through, a long handover is summarized+ # first (the summarizer is the child's own first scripted reply).+ short_context = "We are on the release branch."+ long_context = "release notes: " + "detail " * 2000 # over the 8 KiB inline limit+ model = ScriptedModel([{"text": "MCP_SUMMARY_OK"}] + [{"text": "MCP_TASK_OK"}] * 8,+ exhausted_text="MCP_TASK_OK")+ model.start(1234)+ try:+ for ident, context in (("short", short_context), ("big", long_context)):+ call = request("tools/call", {"name": "run_task", "arguments": {+ "prompt": "Reply with MCP_TASK_OK. No files or tools are needed.",+ "context": context, "timeout_seconds": 30,+ }}, ident)+ replies = exchange(exe, env, workspace, handshake + call, ("--model", "lmstudio"))+ result = replies[1]["result"]+ assert not result["isError"], result+ data = result["structuredContent"]+ assert data["context_chars"] == len(context), data+ # Unavailable usage is stated, and the same metadata is mirrored+ # in the namespaced slot for raw-result readers.+ assert data["usage_reported"] is False, data+ assert data["usage_unavailable_reason"], data+ meta = result["_meta"]["codegraff/runTask"]+ assert meta["context_chars"] == data["context_chars"], meta+ assert meta["context_summarized"] == data["context_summarized"], meta+ assert meta["usage_reported"] is False, meta+ # Preprocessing is reported separately from child execution, and+ # an incomplete charge is surfaced as unavailable, never guessed.+ pre = data["preprocess"]+ assert isinstance(pre["usage_complete"], bool), pre+ assert isinstance(pre["input_tokens"], int) and isinstance(pre["output_tokens"], int), pre+ if pre["usage_complete"]:+ assert isinstance(pre["cost_usd"], (int, float)), pre+ assert pre["unavailable_reason"] == "", pre+ else:+ assert pre["cost_usd"] is None, pre+ assert pre["unavailable_reason"], pre+ if ident == "short":+ # No summarization ran, so there is nothing to charge for.+ assert pre["unavailable_reason"] == "no summarization was needed", pre+ assert pre["input_tokens"] == 0 and pre["output_tokens"] == 0, pre+ if ident == "short":+ assert data["context_summarized"] is False, data+ assert data["context_used_chars"] == len(context), data+ else:+ assert data["context_summarized"] is True, data+ assert 0 < data["context_used_chars"] < len(context), data+ finally:+ model.stop()for yolo in (False, True):target = Path(workspace) / "permission-check.txt"model = ScriptedModel([@@ -114,7 +163,7 @@ def main():capture_output=True, env=nested, cwd=workspace, timeout=10)assert run.returncode != 0assert not run.stdout- print("MCP server: discovery, app negotiation and resources, structured results, real task, permission modes, ping, recursion guard passed")+ print("MCP server: discovery, app negotiation and resources, structured results, real task, caller context, permission modes, ping, recursion guard passed")if __name__ == "__main__":diff --git a/spec/kernels/providers.json b/spec/kernels/providers.jsonindex 85a4860c..5a5358b3 100644--- a/spec/kernels/providers.json+++ b/spec/kernels/providers.json@@ -6,9 +6,9 @@"id": "anthropic","kind": "anthropic","auth": "x_api_key",- "login": "api_key",+ "login": "anthropic_browser","catalog": "anthropic",- "sub_login": false,+ "sub_login": true,"takes_effort": false},{diff --git a/spec/ref/provider.py b/spec/ref/provider.pyindex 6a6d9948..1ba8fb73 100644--- a/spec/ref/provider.py+++ b/spec/ref/provider.py@@ -6,7 +6,7 @@ from typing import LiteralKind = Literal["anthropic", "openai", "responses", "interactions"]Auth = Literal["x_api_key", "bearer", "goog_api_key"]-Login = Literal["api_key", "codegraff_device", "codex_device", "kimi_device", "xai_device", "zai_cli", "chatgpt_browser"]+Login = Literal["api_key", "codegraff_device", "codex_device", "kimi_device", "xai_device", "zai_cli", "chatgpt_browser", "anthropic_browser"]Catalog = Literal["baked", "codex", "kimi", "openai", "anthropic"]@@ -31,7 +31,7 @@ def row(SPECS: list[dict] = [- row("anthropic", "anthropic", "x_api_key", catalog="anthropic"),+ row("anthropic", "anthropic", "x_api_key", login="anthropic_browser", catalog="anthropic", sub_login=True),row("codegraff", "openai", "bearer", login="codegraff_device", catalog="openai", takes_effort=True),row("deepseek", "openai", "bearer", takes_effort=True),row("openai", "responses", "bearer"),diff --git a/src/acp_agent_activity.zig b/src/acp_agent_activity.zigindex 39a32562..e59f3a16 100644--- a/src/acp_agent_activity.zig+++ b/src/acp_agent_activity.zig@@ -41,7 +41,7 @@ pub fn updates(a: A, snapshot: activity.Snapshot) ![]std.json.Value {try input.put(a, @tagName(event.input_key), .{ .string = event.text });try stream.writeToolCall(w, snapshot.agent.id, event.id, event.name, .{ .object = input });} else if (std.mem.eql(u8, event.type, "tool_result")) {- try stream.writeToolDone(w, snapshot.agent.id, event.id, event.is_error, event.text);+ try stream.writeToolDone(w, snapshot.agent.id, event.id, event.is_error, event.text, null);} else continue;try rows.append(a, try std.json.parseFromSliceLeaky(std.json.Value, a, w.buffered(), .{}));}diff --git a/src/acp_auth.zig b/src/acp_auth.zigindex 5f03a0e8..696660c5 100644--- a/src/acp_auth.zig+++ b/src/acp_auth.zig@@ -24,7 +24,15 @@ pub const terminal_login = AuthMethod{.args = &.{"login"},};-pub const advertised = [_]AuthMethod{terminal_login};+pub const anthropic_login = AuthMethod{+ .id = "graff-login-anthropic",+ .name = "graff login anthropic",+ .description = "Claude subscription browser sign-in (alias: ant)",+ .type = "terminal",+ .args = &.{ "login", "anthropic" },+};++pub const advertised = [_]AuthMethod{ terminal_login, anthropic_login };pub const required_message = "Authentication required: run `graff login`, then restart the ACP agent.";@@ -50,6 +58,8 @@ test "terminal_login is the registry Terminal Auth shape" {try std.testing.expectEqualStrings("graff-login", terminal_login.id);try std.testing.expectEqualStrings("terminal", terminal_login.type);try std.testing.expectEqualStrings("login", terminal_login.args[0]);+ try std.testing.expectEqualStrings("anthropic", advertised[1].args[1]);+ try std.testing.expectEqualStrings("terminal", advertised[1].type);}test "clientAllowsTerminal reads the v1 capability" {diff --git a/src/acp_config.zig b/src/acp_config.zigindex 68091605..b7f8d51d 100644--- a/src/acp_config.zig+++ b/src/acp_config.zig@@ -60,7 +60,38 @@ test "thought-level options follow the active model allowlist and normalized curtry std.testing.expectEqualStrings("xhigh", narrow.options[3].value);root.provider.id = "anthropic";root.provider.kind = .anthropic;- root.provider.model = "claude-opus-4-8";+ root.provider.model = "claude-opus-4-4";+ try std.testing.expect((try option(&live, arena)) == null);+}++test "Claude thought-level discovery matches native effort support" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const arena = state.allocator();+ var root = try @import("agent_request_body_responses.zig").testAgentFor(arena, "anthropic", .anthropic, "claude-opus-5-5");+ var keys: @import("provider.zig").Keys = .{ .values = @splat(null) };+ var live: LiveTurn = .{ .root = &root, .keys = &keys, .out = undefined };+ root.reasoning = .medium;+ for ([_][]const u8{ "claude-opus-5-5", "claude-opus-4-5", "claude-sonnet-4-6", "claude-fable-5-1", "claude-mythos-5-1" }) |model| {+ root.provider.model = model;+ try std.testing.expect(root.effortApplies());+ const offered = (try option(&live, arena)).?;+ try std.testing.expectEqualStrings("medium", offered.currentValue);+ var medium_offered = false;+ for (offered.options) |value| {+ if (std.mem.eql(u8, value.value, "medium")) medium_offered = true;+ }+ try std.testing.expect(medium_offered);+ }+ for ([_][]const u8{ "claude-opus-4-4", "claude-sonnet-4-5", "claude-haiku-4-5", "unknown" }) |model| {+ root.provider.model = model;+ try std.testing.expect(!root.effortApplies());+ try std.testing.expect((try option(&live, arena)) == null);+ try std.testing.expect(!try set(&live, "medium"));+ }+ root.provider.id = "minimax";+ root.provider.model = "claude-opus-5-5";+ try std.testing.expect(!root.effortApplies());try std.testing.expect((try option(&live, arena)) == null);}diff --git a/src/acp_preauth.zig b/src/acp_preauth.zigindex 28829165..d35b54ca 100644--- a/src/acp_preauth.zig+++ b/src/acp_preauth.zig@@ -87,12 +87,16 @@ test "pre-auth answers multiple initialize requests from the shared engine" {\\, &buf);try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, output, "\"authMethods\""));- try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, output, "graff-login"));var replies = std.mem.tokenizeScalar(u8, output, '\n');var count: usize = 0;while (replies.next()) |line| {const reply = try std.json.parseFromSliceLeaky(std.json.Value, state.allocator(), line, .{});const result = reply.object.get("result").?.object;+ const methods = result.get("authMethods").?.array.items;+ try std.testing.expectEqual(@as(usize, 2), methods.len);+ try std.testing.expectEqualStrings("graff-login", methods[0].object.get("id").?.string);+ try std.testing.expectEqualStrings("graff-login-anthropic", methods[1].object.get("id").?.string);+ try std.testing.expectEqualStrings("anthropic", methods[1].object.get("args").?.array.items[1].string);for ([_][]const u8{ "agentInfo", "agentImplementation" }) |field| {try std.testing.expectEqualStrings("preauth-test", result.get(field).?.object.get("version").?.string);}diff --git a/src/acp_replay.zig b/src/acp_replay.zigindex cc851cb7..c4f38d1c 100644--- a/src/acp_replay.zig+++ b/src/acp_replay.zig@@ -81,7 +81,7 @@ fn finishCall(arena: Allocator, w: *Io.Writer, sid: []const u8, calls: *std.Arra};const text = try visibleText(arena, output);if (text.len > 0) {- try @import("acp_view_meta.zig").writeDone(w, sid, id, if (failed) "failed" else "completed", text);+ try @import("acp_view_meta.zig").writeDone(w, sid, id, if (failed) "failed" else "completed", text, null);} else {try proto.writeNotification(w, "session/update", .{.sessionId = sid,diff --git a/src/acp_run_task_tests.zig b/src/acp_run_task_tests.zignew file mode 100644index 00000000..1c5a515d--- /dev/null+++ b/src/acp_run_task_tests.zig@@ -0,0 +1,74 @@+//! Offline propagation test for the `codegraff/runTask` bridge: a delegated+//! task's context/usage facts must reach the completed ACP+//! `tool_call_update` from a `--json` tool_result line, carrying only the five+//! declared fields — never the task's text or the caller's context.+//!+//! Lives beside acp_stream.zig rather than inside it: that file is already at+//! the 600-line cap, and this exercises its translateEvent boundary.++const std = @import("std");+const Io = std.Io;+const testing = std.testing;+const Value = std.json.Value;+const stream = @import("acp_stream.zig");++/// One `--json` tool_result line carrying a runTask mirror, translated to ACP.+fn translate(a: std.mem.Allocator, wire: []const u8, buf: []u8) ![]const u8 {+ const parsed = try std.json.parseFromSlice(Value, a, wire, .{});+ defer parsed.deinit();+ var w: Io.Writer = .fixed(buf);+ var id_buf: [64]u8 = @splat(0);+ var next: u32 = 0;+ _ = try stream.translateEvent(&w, "s1", parsed.value, &id_buf, &next);+ return w.buffered();+}++test "a delegated task's facts reach the ACP tool_call_update, and nothing else does" {+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var buf: [8192]u8 = undefined;+ const out = try translate(a, "{\"type\":\"tool_result\",\"id\":\"call-9\",\"name\":\"mcp__codegraff__run_task\"," +++ "\"is_error\":false,\"text\":\"the task answer\"," +++ "\"run_task\":{\"context_chars\":40,\"context_used_chars\":7,\"context_summarized\":true," +++ "\"usage_reported\":false,\"usage_unavailable_reason\":\"no settlement\"}}", &buf);++ try testing.expect(std.mem.indexOf(u8, out, "\"sessionUpdate\":\"tool_call_update\"") != null);+ try testing.expect(std.mem.indexOf(u8, out, "\"toolCallId\":\"call-9\"") != null);+ try testing.expect(std.mem.indexOf(u8, out, "\"status\":\"completed\"") != null);+ // The five declared facts, verbatim.+ try testing.expect(std.mem.indexOf(u8, out, "\"codegraff/runTask\":{\"context_chars\":40,\"context_used_chars\":7," +++ "\"context_summarized\":true,\"usage_reported\":false,\"usage_unavailable_reason\":\"no settlement\"}") != null);+ // The answer is the update's content, not smuggled into the metadata.+ try testing.expectEqual(@as(usize, 1), std.mem.count(u8, out, "the task answer"));+ const meta_at = std.mem.indexOf(u8, out, "\"codegraff/runTask\"").?;+ try testing.expect(std.mem.indexOf(u8, out[meta_at..], "the task answer") == null);+ // No view metadata when the result did not open one.+ try testing.expect(std.mem.indexOf(u8, out, "graff/view") == null);+}++test "an ordinary tool result grows no runTask metadata" {+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var buf: [4096]u8 = undefined;+ const out = try translate(a, "{\"type\":\"tool_result\",\"id\":\"call-1\",\"name\":\"read_file\",\"is_error\":false,\"text\":\"fn main\"}", &buf);+ try testing.expect(std.mem.indexOf(u8, out, "tool_call_update") != null);+ try testing.expect(std.mem.indexOf(u8, out, "codegraff/runTask") == null);+ try testing.expect(std.mem.indexOf(u8, out, "\"_meta\"") == null);+}++test "a view and the task facts coexist in one _meta object" {+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena_state.deinit();+ const a = arena_state.allocator();+ var buf: [8192]u8 = undefined;+ const hex = "0123456789abcdef0123456789abcdef";+ const out = try translate(a, "{\"type\":\"tool_result\",\"id\":\"call-2\",\"name\":\"mcp__codegraff__run_task\"," +++ "\"is_error\":false,\"text\":\"[MCP app](/h/.graff/mcp-apps/" ++ hex ++ ".html)\"," +++ "\"run_task\":{\"context_chars\":5,\"usage_reported\":false,\"usage_unavailable_reason\":\"r\"}}", &buf);+ try testing.expect(std.mem.indexOf(u8, out, "\"graff/view\":") != null);+ try testing.expect(std.mem.indexOf(u8, out, "\"codegraff/runTask\":") != null);+ try testing.expect(std.mem.indexOf(u8, out, "\"_meta\":{\"graff/view\":") != null);+ try testing.expect(std.mem.indexOf(u8, out, "\"context_chars\":5") != null);+}diff --git a/src/acp_stream.zig b/src/acp_stream.zigindex 71fb64ff..b1ee1dff 100644--- a/src/acp_stream.zig+++ b/src/acp_stream.zig@@ -108,11 +108,11 @@ pub fn writeToolStatus(w: *Io.Writer, session_id: []const u8, id: []const u8, st});}-pub fn writeToolDone(w: *Io.Writer, session_id: []const u8, id: []const u8, is_error: bool, text: []const u8) !void {+pub fn writeToolDone(w: *Io.Writer, session_id: []const u8, id: []const u8, is_error: bool, text: []const u8, run_task: ?@import("engine_events.zig").RunTaskMeta) !void {const status: []const u8 = if (is_error) "failed" else "completed";v2.breakMessage();- if (text.len == 0) return writeToolStatus(w, session_id, id, status);- try @import("acp_view_meta.zig").writeDone(w, session_id, id, status, text);+ if (text.len == 0 and run_task == null) return writeToolStatus(w, session_id, id, status);+ try @import("acp_view_meta.zig").writeDone(w, session_id, id, status, text, run_task);}fn storeId(id_buf: *[64]u8, id: []const u8) []const u8 {@@ -220,7 +220,7 @@ pub fn translateTracked(try writeToolStatus(w, session_id, use_id, "completed");return .tool_end;}- try writeToolDone(w, session_id, use_id, is_error, text);+ try writeToolDone(w, session_id, use_id, is_error, text, @import("run_task_meta.zig").fromEvent(ev));return .tool_end;}if (std.mem.eql(u8, typ, "ask_user")) {diff --git a/src/acp_subagent_live.zig b/src/acp_subagent_live.zigindex 95472b3e..27247d0d 100644--- a/src/acp_subagent_live.zig+++ b/src/acp_subagent_live.zig@@ -215,9 +215,9 @@ pub const ChildSink = struct {},.tool_result => |v| {if (v.ask_user or std.mem.eql(u8, v.name, "attempt_completion")) return;- stream.writeToolDone(w, self.id, v.id, v.is_error or v.cancelled, v.text) catch return;+ stream.writeToolDone(w, self.id, v.id, v.is_error or v.cancelled, v.text, v.run_task) catch return;},- .tool_rejected => |v| stream.writeToolDone(w, self.id, v.id, true, v.message) catch return,+ .tool_rejected => |v| stream.writeToolDone(w, self.id, v.id, true, v.message, null) catch return,else => return,}w.flush() catch {};@@ -286,9 +286,9 @@ pub const ChildSink = struct {},.tool_result => |v| {if (v.ask_user or std.mem.eql(u8, v.name, "attempt_completion")) return;- stream.writeToolDone(w, self.id, v.id, v.is_error or v.cancelled, v.text) catch return;+ stream.writeToolDone(w, self.id, v.id, v.is_error or v.cancelled, v.text, v.run_task) catch return;},- .tool_rejected => |v| stream.writeToolDone(w, self.id, v.id, true, v.message) catch return,+ .tool_rejected => |v| stream.writeToolDone(w, self.id, v.id, true, v.message, null) catch return,else => return,}const parsed = std.json.parseFromSliceLeaky(std.json.Value, a, w.buffered(), .{}) catch return;diff --git a/src/acp_v2.zig b/src/acp_v2.zigindex f150a8db..572fdb5c 100644--- a/src/acp_v2.zig+++ b/src/acp_v2.zig@@ -160,12 +160,16 @@ const TerminalAuth = struct {type: []const u8 = "terminal",args: []const []const u8,};-const terminal_login = [_]TerminalAuth{.{- .methodId = acp_auth.terminal_login.id,- .name = acp_auth.terminal_login.name,- .description = acp_auth.terminal_login.description,- .args = acp_auth.terminal_login.args,-}};+const terminal_login = blk: {+ var methods: [acp_auth.advertised.len]TerminalAuth = undefined;+ for (acp_auth.advertised, 0..) |method, i| methods[i] = .{+ .methodId = method.id,+ .name = method.name,+ .description = method.description,+ .args = method.args,+ };+ break :blk methods;+};fn clientTerminalAuth(params: ?Value) bool {const p = params orelse return false;@@ -276,4 +280,6 @@ test "v2 initialize uses info and object-marker capabilities" {out = w.buffered();try std.testing.expect(std.mem.indexOf(u8, out, "\"image\":{}") != null);try std.testing.expect(std.mem.indexOf(u8, out, "\"methodId\":\"graff-login\"") != null);+ try std.testing.expect(std.mem.indexOf(u8, out, "\"methodId\":\"graff-login-anthropic\"") != null);+ try std.testing.expect(std.mem.indexOf(u8, out, "\"args\":[\"login\",\"anthropic\"]") != null);}diff --git a/src/acp_view_meta.zig b/src/acp_view_meta.zigindex d63d1797..80e1dc99 100644--- a/src/acp_view_meta.zig+++ b/src/acp_view_meta.zig@@ -44,11 +44,24 @@ pub fn detect(text: []const u8) ?View {return null;}-/// The completed `tool_call_update` for a result with text, plus the view-/// metadata when the text opens with a saved view.-pub fn writeDone(w: *Io.Writer, session_id: []const u8, id: []const u8, status: []const u8, text: []const u8) !void {+/// The completed `tool_call_update` for a result with text, plus the metadata+/// a client needs to render it without parsing prose: the view snapshot when+/// the text opens with one, and the delegated task's context/usage facts when+/// it carried them. The two coexist under one `_meta` object.+pub fn writeDone(w: *Io.Writer, session_id: []const u8, id: []const u8, status: []const u8, text: []const u8, run_task: ?@import("engine_events.zig").RunTaskMeta) !void {const content = .{.{ .type = "content", .content = .{ .type = "text", .text = text } }};- if (detect(text)) |v| {+ const base = .{+ .sessionUpdate = "tool_call_update",+ .toolCallId = id,+ .status = status,+ .content = content,+ };+ const view = detect(text);+ const m = run_task;+ if (view == null and m == null) {+ return proto.writeNotification(w, "session/update", .{ .sessionId = session_id, .update = base });+ }+ if (view != null and m == null) {return proto.writeNotification(w, "session/update", .{.sessionId = session_id,.update = .{@@ -56,17 +69,30 @@ pub fn writeDone(w: *Io.Writer, session_id: []const u8, id: []const u8, status:.toolCallId = id,.status = status,.content = content,- ._meta = .{ .@"graff/view" = v },+ ._meta = .{ .@"graff/view" = view.? },},});}- try proto.writeNotification(w, "session/update", .{+ if (view == null) {+ return proto.writeNotification(w, "session/update", .{+ .sessionId = session_id,+ .update = .{+ .sessionUpdate = "tool_call_update",+ .toolCallId = id,+ .status = status,+ .content = content,+ ._meta = .{ .@"codegraff/runTask" = m.? },+ },+ });+ }+ return proto.writeNotification(w, "session/update", .{.sessionId = session_id,.update = .{.sessionUpdate = "tool_call_update",.toolCallId = id,.status = status,.content = content,+ ._meta = .{ .@"graff/view" = view.?, .@"codegraff/runTask" = m.? },},});}@@ -97,12 +123,40 @@ test "detect: anything else is plain text" {test "writeDone: a view result carries _meta graff/view; other results do not" {var buf: [4096]u8 = undefined;var w: Io.Writer = .fixed(&buf);- try writeDone(&w, "s1", "call-1", "completed", "[Rendered view](/h/.graff/views/" ++ hex ++ ".html) — shown");+ try writeDone(&w, "s1", "call-1", "completed", "[Rendered view](/h/.graff/views/" ++ hex ++ ".html) — shown", null);const out = w.buffered();try testing.expect(std.mem.indexOf(u8, out, "\"graff/view\":{\"kind\":\"html\",\"path\":\"/h/.graff/views/" ++ hex ++ ".html\",\"id\":\"" ++ hex ++ "\"}") != null);try testing.expect(std.mem.indexOf(u8, out, "[Rendered view]") != null); // the text link staysvar buf2: [1024]u8 = undefined;var w2: Io.Writer = .fixed(&buf2);- try writeDone(&w2, "s1", "call-2", "completed", "plain");+ try writeDone(&w2, "s1", "call-2", "completed", "plain", null);try testing.expect(std.mem.indexOf(u8, w2.buffered(), "graff/view") == null);}++const task_meta: @import("engine_events.zig").RunTaskMeta = .{+ .context_chars = 40,+ .context_used_chars = 7,+ .context_summarized = true,+ .usage_reported = false,+ .usage_unavailable_reason = "no settlement",+};++test "writeDone: a delegated task's facts ride _meta alongside a view, and alone" {+ var buf: [4096]u8 = undefined;+ var w: Io.Writer = .fixed(&buf);+ try writeDone(&w, "s1", "call-1", "completed", "plain answer", task_meta);+ const out = w.buffered();+ try testing.expect(std.mem.indexOf(u8, out, "\"codegraff/runTask\":{\"context_chars\":40,\"context_used_chars\":7,\"context_summarized\":true,\"usage_reported\":false,\"usage_unavailable_reason\":\"no settlement\"}") != null);+ try testing.expect(std.mem.indexOf(u8, out, "graff/view") == null);+ // Neither the answer nor the caller's context travels with the facts.+ try testing.expect(std.mem.indexOf(u8, out, "plain answer") != null); // still the tool content+ try testing.expect(std.mem.count(u8, out, "plain answer") == 1);+ // A view and the facts coexist in one _meta object.+ var buf2: [4096]u8 = undefined;+ var w2: Io.Writer = .fixed(&buf2);+ try writeDone(&w2, "s1", "call-2", "completed", "[MCP app](/h/.graff/mcp-apps/" ++ hex ++ ".html)", task_meta);+ const both = w2.buffered();+ try testing.expect(std.mem.indexOf(u8, both, "\"graff/view\":") != null);+ try testing.expect(std.mem.indexOf(u8, both, "\"codegraff/runTask\":") != null);+ try testing.expect(std.mem.indexOf(u8, both, "\"_meta\":{\"graff/view\":") != null);+}diff --git a/src/additional_tests.zig b/src/additional_tests.zigindex 556c2484..40c2e590 100644--- a/src/additional_tests.zig+++ b/src/additional_tests.zig@@ -22,6 +22,11 @@ test {_ = @import("vault_refresh.zig");_ = @import("vault_live.zig");_ = @import("peer_cli.zig");+ // engine_events.zig's tests (that file hit the 600-line cap), and the+ // codegraff/runTask facts that cross MCP result -> tool_result -> ACP _meta.+ _ = @import("engine_events_tests.zig");+ _ = @import("run_task_meta.zig");+ _ = @import("acp_run_task_tests.zig");}test "oneshot -p still uses the live stall-watched transport" {diff --git a/src/agent.zig b/src/agent.zigindex e11518d5..1d13050a 100644--- a/src/agent.zig+++ b/src/agent.zig@@ -190,6 +190,7 @@ pub const Agent = struct {tools_interactions: []const u8 = @import("schema_interactions.zig").tools_interactions_sub,worker_tools: []const u8 = "", // ADR 0227: a spawned child's catalog plus the MCP tools its root loaded (worker_mcp.inherit); empty = the static child catalogtodos: std.ArrayList(TodoItem) = .empty,+ pending_nudged_fp: ?u64 = null, // #1545: fingerprint of the open set the reconciliation guard last nudged; a repeat plain final on an unchanged checklist stops instead of re-remindingeval_cmd: ?[]const u8 = null, // --eval: shell command that scores the current output (eval-driven loop)eval_target: u8 = 90, // --until: stop when the score reaches this (0-100)eval_niche: []const u8 = "", // --niche: fleet niche this eval session optimizes; tags submitted scores into a promotable (niche × tier × suite) cell@@ -525,3 +526,71 @@ test {_ = @import("effort_route.zig");try agent_tests.lazyRootTools(Agent);}++fn pendingStateFixture(arena: Allocator) Agent {+ return .{+ .gpa = arena,+ .arena = arena,+ .io = std.testing.io,+ .client = undefined,+ .provider = .{ .id = "test", .kind = .openai, .auth = .bearer, .url = "", .api_key = "", .model = "test", .context = 100_000 },+ .messages = .init(arena),+ .sub = false,+ .label = "pending",+ .out = null,+ .tool_calls_this_turn = 3,+ .model_calls_this_turn = 4,+ };+}++test "#1545 a repeat plain final on an unchanged checklist stops without re-reminding" {+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const PendingWork = @import("agent_empty_completion.zig").PendingWork;+ var self = pendingStateFixture(arena.allocator());+ try self.todos.append(self.arena, .{ .content = "credit grant", .status = "pending" });+ try self.todos.append(self.arena, .{ .content = "roll out", .status = "in_progress" });+ // Turn 1: the one retry.+ var first: PendingWork = .{};+ try std.testing.expect((try first.finish(&self, "stopping")) == null);+ try std.testing.expectEqual(@as(usize, 1), self.messages.items.len);+ // Turn 2 (fresh per-turn state, same checklist): footer, no new message.+ var second: PendingWork = .{};+ const final = (try second.finish(&self, "stopping")).?;+ try std.testing.expect(std.mem.indexOf(u8, final, "Root execution has stopped with 2 open checklist items") != null);+ try std.testing.expectEqual(@as(usize, 1), self.messages.items.len);+ // Re-planned work re-arms: cancelling one item changes the open set.+ self.todos.items[0].status = "cancelled";+ var third: PendingWork = .{};+ try std.testing.expect((try third.finish(&self, "stopping again")) == null);+ try std.testing.expectEqual(@as(usize, 2), self.messages.items.len);+}++test "#1545 cancelled items are records: skipped by open/allDone, kept by replace, rendered [-]" {+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const PendingWork = @import("agent_empty_completion.zig").PendingWork;+ var self = pendingStateFixture(arena.allocator());+ try self.todos.append(self.arena, .{ .content = "live", .status = "pending" });+ try self.todos.append(self.arena, .{ .content = "abandoned", .status = "cancelled" });+ const goals = @import("goal_state.zig");+ try std.testing.expectEqual(@as(usize, 1), goals.openCount(self.todos.items, 0));+ try std.testing.expect(!goals.allDone(self.todos.items, 0));+ self.todos.items[0].status = "completed";+ try std.testing.expect(goals.allDone(self.todos.items, 0));+ // A replace that omits the cancelled item keeps it (retention, like completed).+ // "live" is mentioned so it clears here (applyTodoWrite re-appends it);+ // the cancelled item survives and nothing counts as cancelled-open.+ const incoming = [_][]const u8{"live"};+ const cleared = @import("goal_todo.zig").clearEpochForReplaceEx(&self.todos, 0, &incoming);+ try std.testing.expectEqual(@as(usize, 0), cleared.dropped_open);+ try std.testing.expectEqual(@as(usize, 1), self.todos.items.len);+ try std.testing.expectEqualStrings("abandoned", self.todos.items[0].content);+ const rendered = goals.renderTodos(&self, 0);+ try std.testing.expect(std.mem.indexOf(u8, rendered, "[-] abandoned") != null);+ // And the guard goes quiet: no open work left once live completes; live+ // is completed here, cancelled never counted, so the final passes through.+ var state: PendingWork = .{};+ try std.testing.expectEqualStrings("done", (try state.finish(&self, "done")).?);+ try std.testing.expectEqual(@as(usize, 0), self.messages.items.len);+}diff --git a/src/agent_empty_completion.zig b/src/agent_empty_completion.zigindex 35366f5d..511f9a18 100644--- a/src/agent_empty_completion.zig+++ b/src/agent_empty_completion.zig@@ -182,6 +182,7 @@ fn handleWithWait(self: *Agent, final_text: []const u8, hist_len: usize, comptim/// on the runTurn stack: tool progress and compaction must not reset it./// Background work still running is live work too: after the reminder, a/// headless run waits for it to report (run_idle.zig).+/// The Agent dedupes reminders across turns for the same ask and open set.pub const PendingWork = struct {nudged: bool = false,idle: @import("run_idle.zig").Hold = .{},@@ -216,6 +217,18 @@ pub const PendingWork = struct {"If stopping is appropriate, explain the actual blocker or status and say root execution is stopping. Never imply the root keeps working after a final reply. " ++"Update the checklist only to reflect real progress; do not mark unfinished work completed to bypass this check.";+ /// New user requests or re-planned open work re-arm the reminder.+ fn openFingerprint(self: *const Agent, epoch: u64) u64 {+ var h = std.hash.Wyhash.init(epoch);+ h.update(messages.latestUserText(self.messages.items));+ for (self.todos.items) |t| {+ if (t.epoch != epoch or t.retired or t.closed()) continue;+ h.update(t.content);+ h.update(t.status);+ }+ return h.final();+ }+fn open(self: *const Agent) usize {if (self.sub or self.review_mode or self.text_only or self.completed != null or main_mod.plan_mode) return 0;if (self.goal) |g| if (g.status == .paused or g.status == .blocked) return 0;@@ -238,12 +251,18 @@ pub const PendingWork = struct {// The reminder first, so independent work runs beside the background// work; the wait applies once the model stops again.if (count > 0 and !state.nudged and canRequest(self)) {- state.nudged = true;const goals = @import("goal_state.zig");- const body = try std.fmt.allocPrint(self.arena, "{s}\n\n{s}", .{ note, goals.renderTodos(self, goals.currentEpoch(self.goal)) });- try self.messages.append(try @import("session_wake.zig").mark(self.arena, try @import("named_work.zig").userNudge(self.arena, self.provider.kind, body)));- if (self.tracer) |tr| tr.note("pending_work", "plain final reconciled; one retry granted");- return null;+ const epoch = goals.currentEpoch(self.goal);+ const fp = openFingerprint(self, epoch);+ state.nudged = true;+ if (self.pending_nudged_fp != fp) {+ self.pending_nudged_fp = fp;+ const body = try std.fmt.allocPrint(self.arena, "{s}\n\n{s}", .{ note, goals.renderTodos(self, epoch) });+ try self.messages.append(try @import("session_wake.zig").mark(self.arena, try @import("named_work.zig").userNudge(self.arena, self.provider.kind, body)));+ if (self.tracer) |tr| tr.note("pending_work", "plain final reconciled; one retry granted");+ return null;+ }+ if (self.tracer) |tr| tr.note("pending_work", "checklist unchanged since last nudge; stopping without re-reminding");}if (canRequest(self) and try state.idle.wait(self)) return null;if (count == 0) return text;@@ -277,6 +296,7 @@ fn pendingFixture(arena: std.mem.Allocator) Agent {self.run_budget = null;self.tool_calls_this_turn = 3; // unlike the zero-tool guards, tools ranself.model_calls_this_turn = 4;+ self.pending_nudged_fp = null;self.tracer = null;self.streamed_text = false;return self;diff --git a/src/agent_request_body.zig b/src/agent_request_body.zigindex db39e2ca..f8b6f63e 100644--- a/src/agent_request_body.zig+++ b/src/agent_request_body.zig@@ -15,6 +15,7 @@ const writeAnthropicMessages = serde.writeAnthropicMessages;const writeAnthropicTools = serde.writeAnthropicTools;const writeKimiTools = serde.writeKimiTools;const writeOpenAIMessageNormalized = serde.writeOpenAIMessageNormalized;+const anthropic_oauth = @import("anthropic_oauth_wire.zig");pub fn responsesOutputLimit(self: *const Agent) u32 {if (self.compaction_request) return 4096;@@ -34,6 +35,8 @@ pub fn buildBody(self: *Agent, tools_in: ?[]const u8, force_tool: bool, stream:switch (self.provider.kind) {.anthropic => {const is_kimi = std.mem.eql(u8, self.provider.id, "kimi");+ const is_oauth = anthropic_oauth.active(self.provider);+ const oauth_names = if (is_oauth) try anthropic_oauth.ToolNames.init(self.scratchAlloc(), tools orelse "[]") else anthropic_oauth.ToolNames{};const claude = @import("claude_wire.zig"); // ADR 0219const is_claude = claude.isClaudeApi(self.provider.id, self.provider.model);try s.objectField("max_tokens");@@ -92,6 +95,7 @@ pub fn buildBody(self: *Agent, tools_in: ?[]const u8, force_tool: bool, stream:const at = if (is_claude) @import("repo_context.zig").staticEnd(self, sys) else sys.len;const head = if (at == 0) sys else sys[0..at];try s.beginArray();+ if (is_oauth) try anthropic_oauth.writeSystemPrefix(&s);try writeSystemBlock(&s, head, cc);if (head.len < sys.len) try writeSystemBlock(&s, sys[head.len..], null);try s.endArray();@@ -104,7 +108,10 @@ pub fn buildBody(self: *Agent, tools_in: ?[]const u8, force_tool: bool, stream:var effort_written = false;if (tools) |t| {try s.objectField("tools");- try writeAnthropicTools(&s, self.scratchAlloc(), t, is_kimi);+ if (is_oauth)+ try oauth_names.writeTools(&s, self.scratchAlloc())+ else+ try writeAnthropicTools(&s, self.scratchAlloc(), t, is_kimi);if (force) {try s.objectField("tool_choice");try s.print("{s}", .{"{\"type\":\"any\"}"});@@ -121,7 +128,10 @@ pub fn buildBody(self: *Agent, tools_in: ?[]const u8, force_tool: bool, stream:// Anthropic API and Kimi's declared Anthropic transport. Kimi also// normalizes all string content into Anthropic text blocks.const cache_msgs = is_claude or is_kimi;- try writeAnthropicMessages(&s, self.messages, if (cache_msgs) cc else null, is_kimi);+ if (is_oauth)+ try oauth_names.writeMessages(&s, self.scratchAlloc(), self.messages, cc)+ else+ try writeAnthropicMessages(&s, self.messages, if (cache_msgs) cc else null, is_kimi);},.openai => {// graff's MakeOpenAiCompat: OpenAI deprecated max_tokens indiff --git a/src/agent_request_policy.zig b/src/agent_request_policy.zigindex 97a68691..6392dc34 100644--- a/src/agent_request_policy.zig+++ b/src/agent_request_policy.zig@@ -78,8 +78,7 @@ fn warnUnpersistedRefresh(self: *Agent) void {/// heals a live session without waiting for it to 401 first. Runs for the root/// and for subagents alike: the credential resolver needs neither the root's/// model catalog nor a `home`, and it resolves the ONE directory every login-/// flow writes (oauth.codexHomeDir), so this read can no longer revert a-/// `/login` that just succeeded — the reported #402 symptom.+/// flow writes (oauth.codexHomeDir), so this read cannot revert a successful `/login`.////// kimi/xai refresh only near expiry; the codex read is unconditional, because a/// ChatGPT access token carries no expiry field we could check cheaply.@@ -91,10 +90,10 @@ fn warnUnpersistedRefresh(self: *Agent) void {/// life of the child. adoptFreshAuth dupes what it keeps onto the session arena,/// so nothing here outlives this frame.pub fn refreshLoginKeyBeforeSend(self: *Agent) void {- if (self.provider.source != .login) return;+ if (!self.provider.source.canRefresh()) return;var scratch = std.heap.ArenaAllocator.init(self.gpa);defer scratch.deinit();- const fresh = oauth.refreshOAuthKey(self.io, self.gpa, scratch.allocator(), self.home, self.provider.id, false, null, self.provider.account) orelse return;+ const fresh = oauth.refreshOAuthKeyForSource(self.io, self.gpa, scratch.allocator(), self.home, self.provider.id, false, null, self.provider.account, self.provider.source) orelse return;// Only adopt on a real CHANGE: an unchanged credential must not churn the// session arena, nor hand the WS transport latch back on every request.if (std.mem.eql(u8, fresh.key, self.provider.api_key)) return;@@ -206,10 +205,10 @@ pub fn retryWithout(self: *Agent, msg: []const u8, force: *bool, stream_usage: */// have helped either, so it does not earn a retry. Neither does a credential/// belonging to a DIFFERENT ChatGPT account (refreshOAuthKey returns null).pub fn retryAfterAuthRefresh(self: *Agent, msg: []const u8, refreshed: *bool) bool {- if (refreshed.* or self.provider.source != .login or quota.isQuotaExceeded(msg) or !isAuthError(msg)) return false;+ if (refreshed.* or !self.provider.source.canRefresh() or quota.isQuotaExceeded(msg) or !isAuthError(msg)) return false;refreshed.* = true;// ADR 0253: when the refresh cannot help, an interactive ChatGPT session signs in again.- const fresh = oauth.refreshOAuthKey(self.io, self.gpa, self.scratchAlloc(), self.home, self.provider.id, true, self.provider.api_key, self.provider.account) orelse return @import("chatgpt_reauth.zig").renewed(self);+ const fresh = oauth.refreshOAuthKeyForSource(self.io, self.gpa, self.scratchAlloc(), self.home, self.provider.id, true, self.provider.api_key, self.provider.account, self.provider.source) orelse return @import("chatgpt_reauth.zig").renewed(self);warnUnpersistedRefresh(self);if (std.mem.eql(u8, fresh.key, self.provider.api_key)) return @import("chatgpt_reauth.zig").renewed(self);adoptFreshAuth(self, fresh);@@ -589,6 +588,7 @@ pub fn handOffExhaustedPlan(self: *Agent) bool {const promotion = credential_failover.promote(self.provider.id) orelse return false;self.provider.api_key = self.arena.dupe(u8, promotion.key) catch promotion.key;self.provider.source = promotion.source;+ self.provider = @import("provider.zig").Provider.withModel(self.provider, self.provider.model);self.ws_off = false;self.ws_transport_failures = 0;if (self.tracer) |tr| tr.note("credential_failover", "plan exhausted — switched to the parked metered key");diff --git a/src/agent_steps.zig b/src/agent_steps.zigindex e4eed765..e527bf93 100644--- a/src/agent_steps.zig+++ b/src/agent_steps.zig@@ -182,6 +182,11 @@ pub fn stepAnthropic(self: *Agent, root: std.json.ObjectMap) !?[]const u8 {return line;}+ const oauth = @import("anthropic_oauth_wire.zig");+ if (oauth.active(self.provider)) {+ const names = try oauth.ToolNames.init(self.scratchAlloc(), self.toolsJson());+ try names.decodeContent(self.arena, content);+ }var assistant: std.json.ObjectMap = .empty;try assistant.put(self.arena, "role", .{ .string = "assistant" });try assistant.put(self.arena, "content", content);diff --git a/src/agent_stream.zig b/src/agent_stream.zigindex 2c8db91e..03747230 100644--- a/src/agent_stream.zig+++ b/src/agent_stream.zig@@ -162,7 +162,7 @@ pub fn postStreamWithClient(self: *Agent, client: *std.http.Client, body: []consif (req.connection) |conn| conn.closing = true;return error.HungRequest;};- hsel.concurrent(.stall, deadlineStallTask, .{ self.io, orig_tio != null, http.head_stall_ms }) catch {+ hsel.concurrent(.stall, deadlineStallTask, .{ self.io, orig_tio != null, http_stall.headBudgetMs(provider.url, http.head_stall_ms, http_stall.streamPolicy(provider.model, provider.url, http.stream_stall_ms)) }) catch {const r = hsel.await() catch |e| {hsel.cancelDiscard();return e;@@ -239,7 +239,7 @@ pub fn postStreamWithClient(self: *Agent, client: *std.http.Client, body: []consvar phase: @import("agent_ws_signal.zig").Phase = .{}; // ADR 0248: commentary does not tightendefer line.deinit();- const policy = http_stall.streamPolicy(self.provider.model, http.stream_stall_ms);+ const policy = http_stall.streamPolicy(self.provider.model, self.provider.url, http.stream_stall_ms);stream: while (true) {// Race the line read against an idle-stall watchdog so a dead stream can't// hang the turn (the Esc escape below is TTY-only, so --json/GUI sessionsdiff --git a/src/agent_stream_h2.zig b/src/agent_stream_h2.zigindex 3a255177..e3ef8a0f 100644--- a/src/agent_stream_h2.zig+++ b/src/agent_stream_h2.zig@@ -88,7 +88,7 @@ pub fn postStream(self: *Agent, body: []const u8, poll_stdin: bool) !?[]u8 {// Dial, send and time-to-first-HEADERS race the head-stall watchdog, as// the HTTP/1.1 send + receiveHead do (issue #54): a half-open socket or a// server that accepts the stream and goes silent must not hang the turn.- switch (try raceHead(self.io, &head, poll_stdin, http.head_stall_ms)) {+ switch (try raceHead(self.io, &head, poll_stdin, http_stall.headBudgetMs(provider.url, http.head_stall_ms, http_stall.streamPolicy(provider.model, provider.url, http.stream_stall_ms)))) {.ok => {},.fallback => return null,}@@ -112,7 +112,7 @@ pub fn postStream(self: *Agent, body: []const u8, poll_stdin: bool) !?[]u8 {var saw_done = false;var prose_open = false;var phase: @import("agent_ws_signal.zig").Phase = .{}; // ADR 0241/0248, same as HTTP/1.1- const policy = http_stall.streamPolicy(provider.model, http.stream_stall_ms);+ const policy = http_stall.streamPolicy(provider.model, provider.url, http.stream_stall_ms);while (true) {// Buffered generation is over once finish_reason arrives; usage trailers keep the normal deadline.diff --git a/src/agent_stream_stall.zig b/src/agent_stream_stall.zigindex 88926bac..7ef2c00d 100644--- a/src/agent_stream_stall.zig+++ b/src/agent_stream_stall.zig@@ -19,7 +19,7 @@ pub fn noteStallRetry(self: *Agent, retries: usize) void {}pub fn stallGiveUpMessage(self: *Agent, retries: usize) ?[]const u8 {- const base = http_stall.streamPolicy(self.provider.model, http.stream_stall_ms).silence_ms;+ const base = http_stall.streamPolicy(self.provider.model, self.provider.url, http.stream_stall_ms).silence_ms;const waited = if (self.stall.tripped_ms != 0) self.stall.tripped_ms else base;return std.fmt.allocPrint(self.arena, "stream stalled: no data from the model for {d}s — ended the turn after {d} reconnect attempts, each waiting longer (stream silence allowance {d}s; set GRAFF_STREAM_STALL_SECS to override)", .{ waited / 1000, retries, base / 1000 }) catch null;}diff --git a/src/agent_tool_batch.zig b/src/agent_tool_batch.zigindex 5a291fec..f666fcbd 100644--- a/src/agent_tool_batch.zig+++ b/src/agent_tool_batch.zig@@ -157,7 +157,7 @@ pub fn takeOutput(self: *Agent, call: ToolCall, output: ToolOutput, handle_thresself.eval_verified = false;self.eval_repair_pending = false;}- return .{ .text = text, .is_error = output.is_error, .cancelled = output.cancelled, .pending = output.pending, .ms = output.ms };+ return .{ .text = text, .is_error = output.is_error, .cancelled = output.cancelled, .pending = output.pending, .ms = output.ms, .run_task = output.run_task };}pub fn handleTarget(self: *Agent) tool_handle.Target {diff --git a/src/agent_tools.zig b/src/agent_tools.zigindex 47e3178b..83425230 100644--- a/src/agent_tools.zig+++ b/src/agent_tools.zig@@ -420,6 +420,7 @@ pub fn sayToolResult(self: *Agent, call: ToolCall, r: ExecResult) void {.ms = r.ms,.meta = isMetaName(name),.ask_user = std.mem.eql(u8, name, "ask_user"),+ .run_task = r.run_task,};const sink = engine_sink.forAgent(self);sink.emit(self.io, .{ .tool_result = ev });diff --git a/src/anthropic_login_tests.zig b/src/anthropic_login_tests.zignew file mode 100644index 00000000..f80ecd61--- /dev/null+++ b/src/anthropic_login_tests.zig@@ -0,0 +1,182 @@+//! Native Anthropic login shares routing, reload and billing across clients.+//! Console profiles stay metered with ordinary Messages formatting.+const std = @import("std");+const Io = std.Io;+const provider = @import("provider.zig");+const billing = @import("billing.zig");+const store = @import("credential_store.zig");+const failover = @import("credential_failover.zig");+const policy = @import("agent_request_policy.zig");+const Agent = @import("agent.zig").Agent;++fn fixtureAgent(io: Io, arena: std.mem.Allocator, home: []const u8, seat: provider.Provider) Agent {+ var agent: Agent = undefined;+ agent.io = io;+ agent.gpa = std.testing.allocator;+ agent.arena = arena;+ agent.scratch_arena = null;+ agent.tracer = null;+ agent.sub = false;+ agent.out = null;+ agent.label = "root";+ agent.home = home;+ agent.model_catalog = null;+ agent.codex_ws = null;+ agent.codex_prev_id = null;+ agent.provider = seat;+ return agent;+}++fn testHome(io: Io, arena: std.mem.Allocator, dir: Io.Dir) ![]const u8 {+ var path: [std.fs.max_path_bytes]u8 = undefined;+ return arena.dupe(u8, path[0..try dir.realPath(io, &path)]);+}++test "Anthropic OAuth routing preserves Bearer on model pins but API keys stay metered" {+ var keys: provider.Keys = .{ .values = @splat(null) };+ try std.testing.expect(keys.set("anthropic", "oauth-test", .login));+ const plan = try keys.providerById("anthropic", "claude-opus-5-5");+ try std.testing.expectEqual(provider.Provider.Auth.bearer, plan.auth);+ try std.testing.expectEqual(billing.Billing.sub, billing.forProvider(plan));+ const pinned = provider.Provider.withModel(plan, "claude-sonnet-5-5");+ try std.testing.expectEqual(provider.Provider.Auth.bearer, pinned.auth);+ try std.testing.expectEqualStrings(plan.api_key, pinned.api_key);+ try std.testing.expectEqual(provider.Keys.CredentialSource.login, pinned.source);+ try std.testing.expect(keys.set("anthropic", "console-test", .profile));+ const console = try keys.providerById("anthropic", "claude-opus-5-5");+ try std.testing.expectEqual(provider.Provider.Auth.bearer, console.auth);+ try std.testing.expect(billing.forProvider(console) != .sub);+ const console_pin = provider.Provider.withModel(console, "claude-sonnet-5-5");+ try std.testing.expectEqual(provider.Provider.Auth.bearer, console_pin.auth);+ try std.testing.expectEqual(provider.Keys.CredentialSource.profile, console_pin.source);+ try std.testing.expect(keys.set("anthropic", "api-test", .environment));+ const metered = try keys.providerById("anthropic", "claude-opus-5-5");+ try std.testing.expectEqual(provider.Provider.Auth.x_api_key, metered.auth);+ try std.testing.expect(billing.forProvider(metered) != .sub);+ try std.testing.expectEqual(provider.Provider.Auth.x_api_key, provider.Provider.withModel(metered, "claude-sonnet-5-5").auth);+}++test "Anthropic OAuth stored plan outranks env key and quota handoff restores API-key auth" {+ const io = std.testing.io;+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const arena = state.allocator();+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const home = try testHome(io, arena, tmp.dir);+ try store.writeOAuth(io, arena, home, ".anthropic", "oauth-test", "refresh-test", @divTrunc(@import("util.zig").unixMs(io), 1000) + 3600);+ failover.resetForTest();+ defer failover.resetForTest();+ var key: ?[]const u8 = "api-test";+ var source: provider.Keys.CredentialSource = .environment;+ failover.preferPlan(io, std.testing.allocator, arena, home, provider.specFor("anthropic").?, &key, &source);+ try std.testing.expectEqualStrings("oauth-test", key.?);+ try std.testing.expectEqual(provider.Keys.CredentialSource.login, source);+ try std.testing.expect(failover.standingBy("anthropic"));+ var keys: provider.Keys = .{ .values = @splat(null) };+ _ = keys.set("anthropic", key.?, source);+ var agent = fixtureAgent(io, arena, home, try keys.providerById("anthropic", "claude-opus-5-5"));+ try std.testing.expect(policy.handOffExhaustedPlan(&agent));+ try std.testing.expectEqualStrings("api-test", agent.provider.api_key);+ try std.testing.expectEqual(provider.Keys.CredentialSource.environment, agent.provider.source);+ try std.testing.expectEqual(provider.Provider.Auth.x_api_key, agent.provider.auth);+ try std.testing.expect(billing.forProvider(agent.provider) != .sub);+ try std.testing.expect(!policy.handOffExhaustedPlan(&agent));+}++test "Anthropic OAuth in-session login reloads active auth and refresh dispatcher adopts disk token" {+ failover.resetForTest();+ defer failover.resetForTest();+ const io = std.testing.io;+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const arena = state.allocator();+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const home = try testHome(io, arena, tmp.dir);+ const expiry = @divTrunc(@import("util.zig").unixMs(io), 1000) + 3600;+ try store.writeOAuth(io, arena, home, ".anthropic", "oauth-test", "refresh-test", expiry);+ var keys: provider.Keys = .{ .values = @splat(null) };+ _ = keys.set("anthropic", "api-test", .environment);+ var agent = fixtureAgent(io, arena, home, try keys.providerById("anthropic", "claude-opus-5-5"));+ @import("picker_auth.zig").reloadLoginKey(&agent, &keys, arena, "anthropic");+ try std.testing.expect(failover.standingBy("anthropic"));+ try std.testing.expectEqualStrings("oauth-test", agent.provider.api_key);+ try std.testing.expectEqual(provider.Provider.Auth.bearer, agent.provider.auth);+ try std.testing.expectEqual(provider.Keys.CredentialSource.login, agent.provider.source);+ try store.writeOAuth(io, arena, home, ".anthropic", "new-oauth-test", "new-refresh-test", expiry);+ const fresh = @import("oauth.zig").refreshOAuthKey(io, std.testing.allocator, arena, home, "anthropic", true, "oauth-test", "").?;+ try std.testing.expectEqualStrings("new-oauth-test", fresh.key);+}++test "Anthropic OAuth catalog uses Bearer and beta while API-key catalog stays unchanged" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ var headers: [4]std.http.Header = undefined;+ const catalog = @import("router_catalog.zig");+ const spec = provider.specFor("anthropic").?;+ const plan = catalog.catalogHeaders(state.allocator(), spec, "oauth-test", .login, &headers).?;+ try std.testing.expectEqual(@as(usize, 4), plan.len);+ try std.testing.expectEqualStrings("Authorization", plan[1].name);+ try std.testing.expectEqualStrings("Bearer oauth-test", plan[1].value);+ try std.testing.expect(std.mem.indexOf(u8, plan[3].value, "oauth-2025-04-20") != null);+ const console = catalog.catalogHeaders(state.allocator(), spec, "console-test", .profile, &headers).?;+ try std.testing.expectEqual(@as(usize, 3), console.len);+ try std.testing.expectEqualStrings("Authorization", console[1].name);+ try std.testing.expectEqualStrings("Bearer console-test", console[1].value);+ const metered = catalog.catalogHeaders(state.allocator(), spec, "api-test", .environment, &headers).?;+ try std.testing.expectEqual(@as(usize, 3), metered.len);+ try std.testing.expectEqualStrings("x-api-key", metered[1].name);+ try std.testing.expectEqualStrings("api-test", metered[1].value);+}++test "Anthropic Console profiles stay metered with ordinary formatting unlike native login" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const arena = state.allocator();+ const wire = @import("anthropic_oauth_wire.zig");+ const tools = "[{\"name\":\"bAsH\",\"description\":\"shell\",\"input_schema\":{\"type\":\"object\"}}]";+ const messages = try std.json.parseFromSliceLeaky(std.json.Value, arena, "[{\"role\":\"user\",\"content\":\"hello\"}]", .{ .allocate = .alloc_always });+ var keys: provider.Keys = .{ .values = @splat(null) };+ for ([_]provider.Keys.CredentialSource{ .profile, .login, .environment }) |source| {+ try std.testing.expect(keys.set("anthropic", "synthetic-token", source));+ var agent: Agent = .{+ .gpa = std.testing.allocator,+ .arena = arena,+ .io = std.testing.io,+ .client = undefined,+ .provider = try keys.providerById("anthropic", "claude-opus-5-5"),+ .messages = messages.array,+ .sub = false,+ .label = "",+ .out = null,+ .sys_normal = "Ordinary Graff instructions.",+ .tools_anthropic = tools,+ };+ const subscription = source == .login;+ try std.testing.expectEqual(subscription, wire.active(agent.provider));+ try std.testing.expectEqual(subscription, billing.forProvider(agent.provider) == .sub);+ try std.testing.expectEqual(if (subscription) billing.CostClass.plan else .api, billing.costFor("anthropic", source));+ try std.testing.expectEqual(if (source == .environment) provider.Provider.Auth.x_api_key else .bearer, agent.provider.auth);+ var headers: [12]std.http.Header = undefined;+ const hs = @import("http_headers.zig").providerHeaders(std.testing.io, agent.provider, "Bearer synthetic-token", &headers);+ var identity = false;+ var bearer = false;+ for (hs) |header| {+ if (std.ascii.eqlIgnoreCase(header.name, "x-app")) identity = true;+ if (std.ascii.eqlIgnoreCase(header.name, "authorization")) bearer = true;+ if (std.ascii.eqlIgnoreCase(header.name, "anthropic-beta"))+ try std.testing.expectEqual(subscription, std.mem.indexOf(u8, header.value, "oauth-2025-04-20") != null);+ }+ try std.testing.expectEqual(subscription, identity);+ try std.testing.expectEqual(source != .environment, bearer);+ const body = try agent.buildBody(tools, false, true, true);+ defer std.testing.allocator.free(body);+ const root = (try std.json.parseFromSliceLeaky(std.json.Value, arena, body, .{ .allocate = .alloc_always })).object;+ const system = root.get("system").?.array.items;+ try std.testing.expectEqual(@as(usize, if (subscription) 2 else 1), system.len);+ try std.testing.expectEqualStrings(agent.sys_normal, system[system.len - 1].object.get("text").?.string);+ try std.testing.expectEqual(subscription, std.mem.indexOf(u8, body, wire.system_prefix) != null);+ try std.testing.expectEqualStrings(if (subscription) "Bash" else "bAsH", root.get("tools").?.array.items[0].object.get("name").?.string);+ }+}diff --git a/src/anthropic_oauth_wire.zig b/src/anthropic_oauth_wire.zignew file mode 100644index 00000000..da228651--- /dev/null+++ b/src/anthropic_oauth_wire.zig@@ -0,0 +1,150 @@+//! Native Anthropic login-token wire compatibility. API keys and gateways+//! continue using their ordinary Anthropic Messages serialization.+const std = @import("std");+const Allocator = std.mem.Allocator;+const Value = std.json.Value;+const Provider = @import("provider.zig").Provider;+const claude = @import("claude_wire.zig");+const serde = @import("serde.zig");++pub const user_agent = "claude-cli/2.1.280";+pub const system_prefix = "You are Claude Code, Anthropic's official CLI for Claude.";+const oauth_beta = "claude-code-20250219,oauth-2025-04-20";++pub fn active(provider: Provider) bool {+ return std.mem.eql(u8, provider.id, "anthropic") and provider.source == .login;+}++pub fn beta(provider: Provider) ?[]const u8 {+ if (!std.mem.eql(u8, provider.id, "anthropic")) return null;+ if (active(provider)) return if (claude.bindsThinking(provider.model)) oauth_beta ++ "," ++ claude.binding_beta else oauth_beta;+ return if (claude.bindsThinking(provider.model)) claude.binding_beta else null;+}++pub fn identityHeaders(buf: []std.http.Header) usize {+ const headers = [_]std.http.Header{+ .{ .name = "accept", .value = "application/json" },+ .{ .name = "user-agent", .value = user_agent },+ .{ .name = "anthropic-dangerous-direct-browser-access", .value = "true" },+ .{ .name = "x-app", .value = "cli" },+ };+ @memcpy(buf[0..headers.len], &headers);+ return headers.len;+}++pub fn writeSystemPrefix(s: *std.json.Stringify) !void {+ try s.write(.{ .type = "text", .text = system_prefix });+}++const known_names = [_][]const u8{+ "Read", "Write", "Edit", "Bash", "Grep", "Glob", "AskUserQuestion",+ "EnterPlanMode", "ExitPlanMode", "KillShell", "NotebookEdit", "Skill", "Task", "TaskOutput",+ "TodoWrite", "WebFetch", "WebSearch",+};++pub fn canonical(name: []const u8) []const u8 {+ for (known_names) |known| if (std.ascii.eqlIgnoreCase(name, known)) return known;+ return name;+}++fn string(obj: Value, field: []const u8) ?[]const u8 {+ if (obj != .object) return null;+ const value = obj.object.get(field) orelse return null;+ return if (value == .string) value.string else null;+}++/// One reversible map per advertised catalog. Case-distinct tools which+/// would share a Claude Code spelling retain their exact original names.+pub const ToolNames = struct {+ tools: []const Value = &.{},++ pub fn init(arena: Allocator, raw: []const u8) !ToolNames {+ if (raw.len == 0) return .{};+ const tools = try std.json.parseFromSliceLeaky(Value, arena, raw, .{ .allocate = .alloc_always });+ return .{ .tools = if (tools == .array) tools.array.items else &.{} };+ }++ pub fn encode(self: ToolNames, name: []const u8) []const u8 {+ const wire = canonical(name);+ var matches: usize = 0;+ for (self.tools) |tool| {+ const actual = string(tool, "name") orelse continue;+ if (std.mem.eql(u8, canonical(actual), wire)) matches += 1;+ }+ return if (matches > 1) name else wire;+ }++ pub fn decode(self: ToolNames, name: []const u8) []const u8 {+ // Exact names win, including a collision group left unchanged.+ for (self.tools) |tool| {+ const actual = string(tool, "name") orelse continue;+ if (std.mem.eql(u8, actual, name)) return actual;+ }+ for (self.tools) |tool| {+ const actual = string(tool, "name") orelse continue;+ if (std.mem.eql(u8, self.encode(actual), name)) return actual;+ }+ return name;+ }++ pub fn writeTools(self: ToolNames, s: *std.json.Stringify, arena: Allocator) !void {+ try s.beginArray();+ for (self.tools) |tool| {+ var copy = tool;+ if (string(tool, "name")) |name| {+ copy = .{ .object = try tool.object.clone(arena) };+ try copy.object.put(arena, "name", .{ .string = self.encode(name) });+ if (copy.object.getPtr("input_schema")) |schema| {+ if (schema.* == .object) schema.* = .{ .object = try schema.object.clone(arena) };+ _ = try serde.defaultRootObjectType(arena, schema);+ }+ }+ try s.write(copy);+ }+ try s.endArray();+ }++ pub fn writeMessages(self: ToolNames, s: *std.json.Stringify, arena: Allocator, messages: std.json.Array, cache: ?[]const u8) !void {+ var copy = std.json.Array.init(arena);+ for (messages.items) |message| {+ var next = message;+ if (message == .object) if (message.object.get("content")) |content| {+ if (content == .array) {+ var blocks = std.json.Array.init(arena);+ for (content.array.items) |block| {+ var encoded = block;+ if (string(block, "type")) |kind| {+ if (std.mem.eql(u8, kind, "tool_use")) if (string(block, "name")) |name| {+ encoded = .{ .object = try block.object.clone(arena) };+ try encoded.object.put(arena, "name", .{ .string = self.encode(name) });+ };+ }+ try blocks.append(encoded);+ }+ next = .{ .object = try message.object.clone(arena) };+ try next.object.put(arena, "content", .{ .array = blocks });+ }+ };+ try copy.append(next);+ }+ try serde.writeAnthropicMessages(s, copy, cache, false);+ }++ /// Decode before storing history or dispatching tools. Only the name is+ /// changed: signed thinking, IDs and arguments remain exactly as returned.+ pub fn decodeContent(self: ToolNames, arena: Allocator, content: Value) !void {+ if (content != .array) return;+ for (content.array.items) |*block| {+ const kind = string(block.*, "type") orelse continue;+ if (!std.mem.eql(u8, kind, "tool_use")) continue;+ const name = string(block.*, "name") orelse continue;+ const actual = self.decode(name);+ if (!std.mem.eql(u8, actual, name))+ try block.object.put(arena, "name", .{ .string = try arena.dupe(u8, actual) });+ }+ }+};++test {+ _ = @import("anthropic_oauth_wire_tests.zig");+}diff --git a/src/anthropic_oauth_wire_tests.zig b/src/anthropic_oauth_wire_tests.zignew file mode 100644index 00000000..32f9bc3e--- /dev/null+++ b/src/anthropic_oauth_wire_tests.zig@@ -0,0 +1,259 @@+//! Native login-token Anthropic request/header and reversible tool-name tests.+const std = @import("std");+const Value = std.json.Value;+const Agent = @import("agent.zig").Agent;+const Provider = @import("provider.zig").Provider;+const oauth = @import("anthropic_oauth_wire.zig");+const headers = @import("http_headers.zig");+const testing = std.testing;++const catalog = "[{\"name\":\"bAsH\",\"description\":\"shell\",\"input_schema\":{\"properties\":{\"name\":{\"type\":\"string\"}}}},{\"name\":\"custom_tool\",\"input_schema\":{\"type\":\"object\"}}]";+const history = "[{\"role\":\"assistant\",\"content\":[{\"type\":\"thinking\",\"thinking\":\"plan\",\"signature\":\"sig-preserve\"},{\"type\":\"tool_use\",\"id\":\"call-1\",\"name\":\"bAsH\",\"input\":{\"name\":\"bAsH\",\"command\":\"true\"}}]},{\"role\":\"user\",\"content\":[{\"type\":\"tool_result\",\"tool_use_id\":\"call-1\",\"content\":\"ok\"}]}]";++fn provider() Provider {+ return .{ .id = "anthropic", .kind = .anthropic, .auth = .bearer, .url = "", .api_key = "opaque-login-token", .source = .login, .model = "claude-opus-5-5", .context = 1_000_000 };+}++fn agentFor(arena: std.mem.Allocator) !Agent {+ const parsed = try std.json.parseFromSliceLeaky(Value, arena, history, .{ .allocate = .alloc_always });+ return .{+ .gpa = testing.allocator,+ .arena = arena,+ .io = testing.io,+ .client = undefined,+ .provider = provider(),+ .messages = parsed.array,+ .sub = false,+ .label = "",+ .out = null,+ .sys_normal = "Actual Graff instructions remain unchanged.",+ .tools_anthropic = catalog,+ };+}++fn field(h: []const std.http.Header, name: []const u8) ?[]const u8 {+ for (h) |item| if (std.ascii.eqlIgnoreCase(item.name, name)) return item.value;+ return null;+}++fn contains(text: []const u8, part: []const u8) bool {+ return std.mem.indexOf(u8, text, part) != null;+}++fn parse(arena: std.mem.Allocator, body: []const u8) !Value {+ return std.json.parseFromSliceLeaky(Value, arena, body, .{ .allocate = .alloc_always });+}++test "Anthropic OAuth headers are source scoped with native CLI identity and Bearer" {+ var p = provider();+ var buf: [12]std.http.Header = undefined;+ const h = headers.providerHeaders(testing.io, p, "Bearer opaque-login-token", &buf);+ try testing.expectEqualStrings("Bearer opaque-login-token", field(h, "authorization").?);+ try testing.expect(field(h, "x-api-key") == null);+ try testing.expectEqualStrings("application/json", field(h, "accept").?);+ try testing.expectEqualStrings("true", field(h, "anthropic-dangerous-direct-browser-access").?);+ try testing.expectEqualStrings("cli", field(h, "x-app").?);+ try testing.expectEqualStrings("claude-cli/2.1.280", field(h, "user-agent").?);+ var user_agents: usize = 0;+ for (h) |item| if (std.ascii.eqlIgnoreCase(item.name, "user-agent")) {+ user_agents += 1;+ };+ try testing.expectEqual(@as(usize, 1), user_agents); // HTTP/2 forwards extras once+ try testing.expect(headers.userAgent(p) == .omit); // no duplicate on HTTP/1.1++ // An OAuth-looking key is still an API key when it came from the environment.+ p.source = .environment;+ p.auth = .x_api_key;+ p.api_key = "sk-ant-oat-looking-but-not-login";+ const api = headers.providerHeaders(testing.io, p, "", &buf);+ try testing.expectEqualStrings(p.api_key, field(api, "x-api-key").?);+ try testing.expect(field(api, "authorization") == null);+ try testing.expect(field(api, "x-app") == null);+ try testing.expect(field(api, "accept") == null);+ try testing.expect(headers.userAgent(p) == .default);+ p.source = .login;+ p.id = "gateway";+ try testing.expect(!oauth.active(p));+ try testing.expect(field(headers.providerHeaders(testing.io, p, "", &buf), "x-app") == null);+ try testing.expect(headers.userAgent(p) == .default);+}++test "Anthropic OAuth beta combines login requirements and existing thinking binding once" {+ var p = provider();+ var buf: [12]std.http.Header = undefined;+ const h = headers.providerHeaders(testing.io, p, "Bearer token", &buf);+ try testing.expectEqualStrings("claude-code-20250219,oauth-2025-04-20,thinking-binding-controls-2026-08-01", field(h, "anthropic-beta").?);+ var count: usize = 0;+ for (h) |item| if (std.mem.eql(u8, item.name, "anthropic-beta")) {+ count += 1;+ };+ try testing.expectEqual(@as(usize, 1), count);+ p.model = "claude-opus-4-8";+ try testing.expectEqualStrings("claude-code-20250219,oauth-2025-04-20", field(headers.providerHeaders(testing.io, p, "Bearer token", &buf), "anthropic-beta").?);+ p.source = .environment;+ try testing.expect(field(headers.providerHeaders(testing.io, p, "Bearer token", &buf), "anthropic-beta") == null);+ p.model = "claude-opus-5-5";+ try testing.expectEqualStrings(@import("claude_wire.zig").binding_beta, field(headers.providerHeaders(testing.io, p, "Bearer token", &buf), "anthropic-beta").?);+}++test "Anthropic OAuth body prepends identity and adapts definitions and history without mutating Graff" {+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ var agent = try agentFor(arena);+ const body = try agent.buildBody(catalog, false, true, true);+ defer testing.allocator.free(body);+ const root = (try parse(arena, body)).object;+ const system = root.get("system").?.array.items;+ try testing.expectEqual(@as(usize, 2), system.len);+ try testing.expectEqualStrings(oauth.system_prefix, system[0].object.get("text").?.string);+ try testing.expect(system[0].object.get("cache_control") == null);+ try testing.expectEqualStrings(agent.sys_normal, system[1].object.get("text").?.string);+ try testing.expect(system[1].object.get("cache_control") != null);+ try testing.expectEqualStrings("Actual Graff instructions remain unchanged.", agent.sys_normal);+ const tools = root.get("tools").?.array.items;+ try testing.expectEqualStrings("Bash", tools[0].object.get("name").?.string);+ try testing.expectEqualStrings("custom_tool", tools[1].object.get("name").?.string);+ try testing.expectEqualStrings("object", tools[0].object.get("input_schema").?.object.get("type").?.string);+ const blocks = root.get("messages").?.array.items[0].object.get("content").?.array.items;+ try testing.expectEqualStrings("sig-preserve", blocks[0].object.get("signature").?.string);+ try testing.expect(blocks[0].object.get("cache_control") == null);+ try testing.expectEqualStrings("Bash", blocks[1].object.get("name").?.string);+ try testing.expectEqualStrings("call-1", blocks[1].object.get("id").?.string);+ try testing.expectEqualStrings("bAsH", blocks[1].object.get("input").?.object.get("name").?.string);+ try testing.expectEqualStrings("bAsH", agent.messages.items[0].object.get("content").?.array.items[1].object.get("name").?.string);+ try testing.expect(contains(body, "\"thinking\":{\"type\":\"adaptive\",\"display\":\"summarized\",\"block_binding\""));+ try testing.expect(contains(body, "\"tool_use_id\":\"call-1\""));+}++test "Anthropic OAuth leaves API key and gateway Anthropic request bodies unchanged" {+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ var agent = try agentFor(arena);+ agent.provider.source = .environment;+ agent.provider.auth = .x_api_key;+ for ([_][]const u8{ "anthropic", "minimax", "codegraff" }) |id| {+ agent.provider.id = id;+ const body = try agent.buildBody(catalog, false, false, false);+ defer testing.allocator.free(body);+ const root = (try parse(arena, body)).object;+ try testing.expect(!contains(body, oauth.system_prefix));+ try testing.expectEqualStrings("bAsH", root.get("tools").?.array.items[0].object.get("name").?.string);+ try testing.expectEqualStrings("bAsH", root.get("messages").?.array.items[0].object.get("content").?.array.items[1].object.get("name").?.string);+ // A gateway login is not a native Anthropic login either.+ agent.provider.source = .login;+ }+}++test "Anthropic OAuth recognizes only known Claude Code tool names case insensitively" {+ const names = [_][]const u8{ "Read", "Write", "Edit", "Bash", "Grep", "Glob", "AskUserQuestion", "EnterPlanMode", "ExitPlanMode", "KillShell", "NotebookEdit", "Skill", "Task", "TaskOutput", "TodoWrite", "WebFetch", "WebSearch" };+ var lower: [32]u8 = undefined;+ for (names) |name| {+ for (name, 0..) |char, i| lower[i] = std.ascii.toLower(char);+ try testing.expectEqualStrings(name, oauth.canonical(lower[0..name.len]));+ }+ for ([_][]const u8{ "read_file", "ask_user", "web_fetch", "Bash_extra", "mcp__Read" }) |name|+ try testing.expectEqualStrings(name, oauth.canonical(name));+}++test "Anthropic OAuth case collisions keep distinct advertised names and reversible dispatch" {+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const raw = "[{\"name\":\"bash\"},{\"name\":\"Bash\"},{\"name\":\"READ\"},{\"name\":\"custom_tool\"}]";+ const names = try oauth.ToolNames.init(arena, raw);+ try testing.expectEqualStrings("bash", names.encode("bash"));+ try testing.expectEqualStrings("Bash", names.encode("Bash"));+ try testing.expectEqualStrings("bash", names.decode("bash"));+ try testing.expectEqualStrings("Bash", names.decode("Bash"));+ try testing.expectEqualStrings("Read", names.encode("READ"));+ try testing.expectEqualStrings("READ", names.decode("Read"));+ try testing.expectEqualStrings("custom_tool", names.decode("custom_tool"));+ var agent = try agentFor(arena);+ const body = try agent.buildBody(raw, false, false, false);+ defer testing.allocator.free(body);+ const tools = (try parse(arena, body)).object.get("tools").?.array.items;+ try testing.expectEqualStrings("bash", tools[0].object.get("name").?.string);+ try testing.expectEqualStrings("Bash", tools[1].object.get("name").?.string);+}++test "Anthropic OAuth streamed response names round trip to actual Graff tools preserving signatures and arguments" {+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ var agent = try agentFor(arena);+ const response = (try agent.assembleAnthropic(+ "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"role\":\"assistant\",\"content\":[]}}\n\n" +++ "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"thinking\",\"thinking\":\"plan\",\"signature\":\"sig-roundtrip\"}}\n\n" +++ "event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n" +++ "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"tool_use\",\"id\":\"roundtrip-id\",\"name\":\"Bash\",\"input\":{}}}\n\n" +++ "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"command\\\":\\\"true\\\"}\"}}\n\n" +++ "event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":1}\n\n" +++ "event: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\"}}\n\n" +++ "event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n",+ )).?;+ const content = response.get("content").?;+ const names = try oauth.ToolNames.init(arena, catalog);+ try names.decodeContent(arena, content);+ const blocks = content.array.items;+ try testing.expectEqualStrings("bAsH", blocks[1].object.get("name").?.string);+ try testing.expectEqualStrings("roundtrip-id", blocks[1].object.get("id").?.string);+ try testing.expectEqualStrings("true", blocks[1].object.get("input").?.object.get("command").?.string);+ try testing.expectEqualStrings("sig-roundtrip", blocks[0].object.get("signature").?.string);+ var message: std.json.ObjectMap = .empty;+ try message.put(arena, "role", .{ .string = "assistant" });+ try message.put(arena, "content", content);+ agent.messages = std.json.Array.init(arena);+ try agent.messages.append(.{ .object = message });+ const body = try agent.buildBody(catalog, false, false, false);+ defer testing.allocator.free(body);+ const replay = (try parse(arena, body)).object.get("messages").?.array.items[0].object.get("content").?.array.items;+ try testing.expectEqualStrings("Bash", replay[1].object.get("name").?.string);+ try testing.expectEqualStrings("sig-roundtrip", replay[0].object.get("signature").?.string);+ try testing.expectEqualStrings("roundtrip-id", replay[1].object.get("id").?.string);+ try testing.expectEqualStrings("true", replay[1].object.get("input").?.object.get("command").?.string);+}++test "Anthropic OAuth title metadata and compaction bodies retain their real system instructions after identity" {+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ var agent = try agentFor(arena);+ agent.sys_override = "Auxiliary call instructions.";+ for ([_]@import("run_budget.zig").CallKind{ .title, .recap, .compaction }) |kind| {+ agent.call_kind = kind;+ agent.sub = kind == .title;+ agent.compaction_request = kind == .compaction;+ const body = try agent.buildBody(null, false, true, true);+ defer testing.allocator.free(body);+ const root = (try parse(arena, body)).object;+ const system = root.get("system").?.array.items;+ try testing.expectEqualStrings(oauth.system_prefix, system[0].object.get("text").?.string);+ try testing.expectEqualStrings(agent.systemPrompt(), system[1].object.get("text").?.string);+ try testing.expect(root.get("tools") == null);+ if (agent.compaction_request) try testing.expect(system[1].object.get("cache_control") == null);+ }+}++test "Anthropic OAuth RLM standalone body supplies identity only for native login source" {+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ var p = provider();+ const build = @import("rlm_query.zig").buildBody;+ const body = try build(testing.allocator, p, "Answer briefly.");+ defer testing.allocator.free(body);+ const root = (try parse(arena, body)).object;+ try testing.expectEqualStrings(oauth.system_prefix, root.get("system").?.array.items[0].object.get("text").?.string);+ try testing.expectEqualStrings("Answer briefly.", root.get("messages").?.array.items[0].object.get("content").?.string);+ p.source = .environment;+ const api_body = try build(testing.allocator, p, "Answer briefly.");+ defer testing.allocator.free(api_body);+ try testing.expect((try parse(arena, api_body)).object.get("system") == null);+ p.source = .login;+ p.id = "minimax";+ const gateway_body = try build(testing.allocator, p, "Answer briefly.");+ defer testing.allocator.free(gateway_body);+ try testing.expect((try parse(arena, gateway_body)).object.get("system") == null);+}diff --git a/src/anthropic_profile.zig b/src/anthropic_profile.zigindex 77fe6e4c..2cc87d4d 100644--- a/src/anthropic_profile.zig+++ b/src/anthropic_profile.zig@@ -1,8 +1,8 @@//! Anthropic Console sign-in: graff uses the credential Anthropic's own CLI//! (`ant auth login`) writes, exactly as Anthropic's SDKs do, so a Console//! account and its workspace's API credits run claude-* models without an API-//! key. graff never runs Anthropic's OAuth client itself: `ant auth login`-//! signs in, and `ant auth print-credentials --access-token` refreshes.+//! key. This Console flow delegates sign-in to `ant auth login`, and+//! refresh to `ant auth print-credentials --access-token`.//!//! Layout (Anthropic SDK `config` package): the directory is//! `ANTHROPIC_CONFIG_DIR`, else `%APPDATA%\Anthropic` on Windows, else@@ -16,7 +16,8 @@//! Precedence follows the SDK's: `ANTHROPIC_API_KEY` wins and profiles are//! not read; a profile named by `ANTHROPIC_PROFILE` outranks a key stored with//! `graff key`; the active profile fills in only when nothing else did.-//! A claude.ai subscription login is not a credential graff uses.+//! Native subscription OAuth is separate (oauth_anthropic.zig); Console+//! profiles remain metered and use ordinary Messages formatting.const std = @import("std");const builtin = @import("builtin");@@ -175,7 +176,7 @@ pub fn pastesCode(environ_map: anytype) bool {return environ_map.get("DISPLAY") == null and environ_map.get("WAYLAND_DISPLAY") == null;}-/// `graff login anthropic`: Anthropic's CLI signs in (browser, Console+/// `graff login console`: Anthropic's CLI signs in (browser, Console/// account, workspace); graff then reads what it wrote.pub fn login(io: Io, arena: Allocator, environ_map: anytype, home: []const u8) !void {const style = &@import("ansi.zig").style;@@ -190,7 +191,7 @@ pub fn login(io: Io, arena: Allocator, environ_map: anytype, home: []const u8) !error.FileNotFound => {try out.writeAll(\\graff signs in to an Anthropic Console account through Anthropic's CLI, `ant`, which isn't installed.- \\Install it, then run `graff login anthropic` again:+ \\Install it, then run `graff login console` again:\\ macOS: brew install anthropics/tap/ant\\ Linux: release binaries at https://github.com/anthropics/anthropic-cli/releases\\ Go: go install github.com/anthropics/anthropic-cli/cmd/ant@latest@@ -215,6 +216,13 @@ pub fn login(io: Io, arena: Allocator, environ_map: anytype, home: []const u8) !try out.flush();}+/// Live environment for Console login/reload from the REPL, which has no Init.+pub const ProcessEnv = struct {+ pub fn get(_: ProcessEnv, name: [:0]const u8) ?[]const u8 {+ return if (std.c.getenv(name.ptr)) |value| std.mem.span(value) else null;+ }+};+const testing = std.testing;const Env = struct {@@ -321,16 +329,42 @@ test "an expiring Console token is refreshed through ant, and sent as a bearer t// The provider built from it authenticates with Bearer, not x-api-key.const provider_mod = @import("provider.zig");var keys: provider_mod.Keys = .{ .values = @splat(null) };- try testing.expect(keys.set("anthropic", "sk-ant-oat01-new", .login));+ try testing.expect(keys.set("anthropic", "sk-ant-oat01-new", .profile));const p = keys.build(provider_mod.specFor("anthropic").?, "sk-ant-oat01-new", "claude-opus-5-5");try testing.expectEqual(provider_mod.Provider.Auth.bearer, p.auth);+ try testing.expect(keys.set("anthropic", "sk-ant-api03-k", .environment));const api_key = keys.build(provider_mod.specFor("anthropic").?, "sk-ant-api03-k", "claude-opus-5-5");try testing.expectEqual(provider_mod.Provider.Auth.x_api_key, api_key.auth);// The model list (router_catalog) authenticates the same way.var hb: [4]std.http.Header = undefined;- const hs = @import("router_catalog.zig").catalogHeaders(a, provider_mod.specFor("anthropic").?, "sk-ant-oat01-new", .login, &hb).?;+ const hs = @import("router_catalog.zig").catalogHeaders(a, provider_mod.specFor("anthropic").?, "sk-ant-oat01-new", .profile, &hb).?;try testing.expectEqualStrings("Authorization", hs[1].name);try testing.expectEqualStrings("Bearer sk-ant-oat01-new", hs[1].value);+ try testing.expectEqual(@as(usize, 3), hs.len); // no subscription beta+ const refreshed = @import("oauth.zig").refreshOAuthKeyForSource(io, testing.allocator, a, "", "anthropic", false, null, "", .profile).?;+ try testing.expectEqualStrings("sk-ant-oat01-new", refreshed.key);+ var agent: @import("agent.zig").Agent = .{+ .gpa = testing.allocator,+ .arena = a,+ .io = io,+ .client = undefined,+ .provider = p,+ .messages = .init(a),+ .sub = false,+ .label = "",+ .out = null,+ };+ agent.provider.api_key = "stale-console-token";+ const policy = @import("agent_request_policy.zig");+ policy.refreshLoginKeyBeforeSend(&agent);+ try testing.expectEqualStrings("sk-ant-oat01-new", agent.provider.api_key);+ agent.provider.api_key = "rejected-console-token";+ var attempted = false;+ try testing.expect(policy.retryAfterAuthRefresh(&agent, "Unauthorized", &attempted));+ try testing.expect(attempted);+ try testing.expectEqualStrings("sk-ant-oat01-new", agent.provider.api_key);+ try testing.expectEqual(provider_mod.Keys.CredentialSource.profile, agent.provider.source);+ try testing.expect(!policy.retryAfterAuthRefresh(&agent, "Unauthorized", &attempted));}test "sign-in pastes a code whenever no local browser can finish it" {diff --git a/src/args.zig b/src/args.zigindex 0ebeb1c2..38e64891 100644--- a/src/args.zig+++ b/src/args.zig@@ -46,6 +46,7 @@ pub const Flags = struct {zai_login: bool = false,chatgpt_login: bool = false,anthropic_login: bool = false,+ anthropic_console_login: bool = false,help_flag: bool = false,version_flag: bool = false,print_flag: bool = false,@@ -269,13 +270,14 @@ pub fn parse(init: std.process.Init) !Flags {if (flags.login_flag and flags.positionals.items.len > 1) {const name = flags.positionals.items[1];const target = loginTarget(name) orelse- std.process.fatal("unknown login '{s}' — graff login [codegraff|chatgpt|kimi|xai|zai|anthropic]", .{name});+ std.process.fatal("unknown login '{s}' — graff login [codegraff|chatgpt|anthropic|kimi|xai|zai]", .{name});flags.codex_login = std.mem.eql(u8, target, "codex");flags.kimi_login = std.mem.eql(u8, target, "kimi");flags.xai_login = std.mem.eql(u8, target, "xai");flags.zai_login = std.mem.eql(u8, target, "zai");flags.chatgpt_login = std.mem.eql(u8, target, "chatgpt-new");- flags.anthropic_login = std.mem.eql(u8, target, "anthropic");+ flags.anthropic_console_login = std.ascii.eqlIgnoreCase(name, "console");+ flags.anthropic_login = std.mem.eql(u8, target, "anthropic") and !flags.anthropic_console_login;}}@@ -342,9 +344,10 @@ pub fn acpLoginIsLogin(positionals: *std.ArrayList([]const u8)) void {pub fn loginTarget(name: []const u8) ?[]const u8 {const aliases = [_][2][]const u8{- .{ "codex", "chatgpt-new" }, .{ "chatgpt", "chatgpt-new" }, .{ "openai", "chatgpt-new" }, .{ "gpt", "chatgpt-new" },- .{ "oai", "chatgpt-new" }, .{ "codegraff", "codegraff" }, .{ "graff", "codegraff" }, .{ "kimi", "kimi" },- .{ "xai", "xai" }, .{ "grok", "xai" }, .{ "anthropic", "anthropic" }, .{ "console", "anthropic" },+ .{ "codex", "chatgpt-new" }, .{ "chatgpt", "chatgpt-new" }, .{ "openai", "chatgpt-new" }, .{ "gpt", "chatgpt-new" },+ .{ "oai", "chatgpt-new" }, .{ "codegraff", "codegraff" }, .{ "graff", "codegraff" }, .{ "kimi", "kimi" },+ .{ "xai", "xai" }, .{ "grok", "xai" }, .{ "ant", "anthropic" }, .{ "anthropic", "anthropic" },+ .{ "console", "anthropic" },};for (aliases) |pair| if (std.ascii.eqlIgnoreCase(name, pair[0])) return pair[1];if (@import("oauth_zai.zig").isLoginName(name)) return "zai";@@ -358,8 +361,9 @@ test "loginTarget: every ChatGPT name, codex included, is the plan's sign-in; untry std.testing.expectEqualStrings("codegraff", loginTarget("graff").?);try std.testing.expectEqualStrings("xai", loginTarget("grok").?);try std.testing.expectEqualStrings("kimi", loginTarget("kimi").?);+ for ([_][]const u8{ "ant", "anthropic", "ANT", "Anthropic", "console", "Console" }) |name|+ try std.testing.expectEqualStrings("anthropic", loginTarget(name).?);try std.testing.expectEqualStrings("zai", loginTarget("zai").?);- try std.testing.expectEqualStrings("anthropic", loginTarget("anthropic").?);try std.testing.expect(loginTarget("chatgptt") == null);try std.testing.expect(loginTarget("") == null);}diff --git a/src/billing.zig b/src/billing.zigindex 743ed277..1c3bedd1 100644--- a/src/billing.zig+++ b/src/billing.zig@@ -9,7 +9,7 @@//!//! 1. the PROVIDER's declared plan (`ProviderSpec.sub_login`), and//! 2. HOW this session's credential was obtained-//! (`Keys.CredentialSource` — `.login` vs `.environment`/`.stored`).+//! (`Keys.CredentialSource` — `.login` vs metered keys/Console profiles).//!//! It is the login that is flat-rate, not the vendor. `graff login xai` buys a//! SuperGrok plan; `XAI_API_KEY` is metered api.x.ai access. Same provider,@@ -101,7 +101,7 @@ pub fn costFor(provider_id: []const u8, source: CredentialSource) CostClass {if (!subscriptionLogin(provider_id)) return .api;return switch (source) {.login, .none => .plan,- .environment, .stored, .session => .api,+ .environment, .profile, .stored, .session => .api,};}@@ -142,9 +142,11 @@ test "a flat-rate login and a metered key on the same seat bill differently (#47// it is only ever reached by login — but the source still gates it.try std.testing.expectEqual(Billing.sub, forSeat("codex", "gpt-5.5", .login));- // A metered vendor's login (none exists today) would still be priced, and- // the codegraff gateway's device login draws on credits, not a plan.- try std.testing.expectEqual(Billing.priced, forSeat("anthropic", "claude-sonnet-4-6", .login));+ // Anthropic's native subscription login is distinct from its metered key;+ // the codegraff gateway's device login still draws on credits, not a plan.+ try std.testing.expectEqual(Billing.sub, forSeat("anthropic", "claude-sonnet-4-6", .login));+ try std.testing.expectEqual(Billing.priced, forSeat("anthropic", "claude-sonnet-4-6", .environment));+ try std.testing.expectEqual(Billing.priced, forSeat("anthropic", "claude-sonnet-4-6", .profile));try std.testing.expectEqual(Billing.priced, forSeat("codegraff", "claude-sonnet-5", .login));// Unpriced stays unpriced: no plan, no price row.@@ -189,6 +191,8 @@ test "costFor: the picker's badge follows the credential, then the provider" {try std.testing.expectEqual(CostClass.credits, costFor("codegraff", .environment));try std.testing.expectEqual(CostClass.api, costFor("openai", .environment));try std.testing.expectEqual(CostClass.api, costFor("anthropic", .stored));+ try std.testing.expectEqual(CostClass.api, costFor("anthropic", .profile));+ try std.testing.expectEqual(CostClass.plan, costFor("anthropic", .login));// Same provider, two credentials, two answers — `graff login xai` buys a// plan, XAI_API_KEY buys metered api.x.ai.@@ -214,7 +218,7 @@ test "costFor agrees with forSeat wherever both have an opinion" {// Two classifiers over one fact is how #471 happened. They answer// different questions, but a plan seat must never be a metered badge.for (provider_mod.provider_specs) |spec| {- for ([_]CredentialSource{ .none, .environment, .login, .stored, .session }) |src| {+ for (std.enums.values(CredentialSource)) |src| {const class = costFor(spec.id, src);const sub = forSeat(spec.id, "definitely-not-in-the-price-sheet", src) == .sub;if (sub) try std.testing.expectEqual(CostClass.plan, class);diff --git a/src/cli.zig b/src/cli.zigindex 2e991fc1..2510a2e8 100644--- a/src/cli.zig+++ b/src/cli.zig@@ -24,6 +24,11 @@ const update_target = @import("update_target.zig");pub const changelog_text =\\What's new\\──────────+ \\0.302.41+ \\ • native subscription sign-in across terminal and ACP clients+ \\ • MCP structured results, resource content and inbound ping handling+ \\ • request-local loopback deadlines and repeatable fixture comparisons+ \\\\0.302.40\\ • stream recovery preserves partial replies across ACP and resume\\ • workspace skills in headless runs; actionable claim inventory@@ -213,7 +218,8 @@ pub const usage_text =\\ graff [-p] "prompt" one-shot: run the prompt, print the answer, exit\\ graff login get a codegraff key (device-code OAuth)\\ graff login chatgpt ChatGPT sign-in (also: codex, openai)- \\ graff login anthropic Anthropic Console sign-in (runs `ant auth login`)+ \\ graff login anthropic Claude subscription browser sign-in (also: ant)+ \\ graff login console metered Console sign-in through the `ant` CLI\\ graff login kimi Kimi Code OAuth login (device-code)\\ graff login xai Grok/SuperGrok OAuth login (device-code)\\ graff login zai Z.AI Coding Plan OAuth logindiff --git a/src/commands_model.zig b/src/commands_model.zigindex b01d2120..9b89a205 100644--- a/src/commands_model.zig+++ b/src/commands_model.zig@@ -351,6 +351,8 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,const login_targets = [_]PickItem{.{ .name = "codegraff", .desc = "free codegraff key (device-code OAuth)" },.{ .name = "chatgpt", .desc = "ChatGPT sign-in (also: codex, openai)" },+ .{ .name = "anthropic", .desc = "Claude subscription sign-in (also: ant)" },+ .{ .name = "console", .desc = "metered Console sign-in through the ant CLI" },.{ .name = "kimi", .desc = "Kimi Code sign-in (device-code OAuth)" },.{ .name = "xai", .desc = "Grok / SuperGrok sign-in (device-code OAuth)" },.{ .name = "zai", .desc = "Z.AI Coding Plan sign-in (CLI OAuth)" },@@ -370,6 +372,7 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,}// Same names as `graff login`: every ChatGPT name, codex included, is// the plan's sign-in, chatgpt-new (ADR 0229).+ const console_login = std.ascii.eqlIgnoreCase(target, "console");target = @import("args.zig").loginTarget(target) orelse target;const home = root.home;@@ -398,6 +401,19 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,try out.flush();return true;};+ } else if (console_login) {+ const profile = @import("anthropic_profile.zig");+ profile.login(root.io, arena, profile.ProcessEnv{}, home) catch |err| {+ try out.print("\xe2\x9c\x97 Console login failed: {t}\n", .{err});+ try out.flush();+ return true;+ };+ } else if (std.mem.eql(u8, target, "anthropic")) {+ @import("oauth_anthropic.zig").login(root.io, root.gpa, arena, home, false) catch |err| {+ try out.print("\xe2\x9c\x97 anthropic login failed: {t}\n", .{err});+ try out.flush();+ return true;+ };} else if (std.mem.eql(u8, target, "chatgpt-new")) {@import("oauth_chatgpt.zig").login(root.io, root.gpa, arena, home) catch |err| {try out.print("\xe2\x9c\x97 chatgpt-new login failed: {t}\n", .{err});@@ -411,12 +427,12 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,try out.flush();return true;}- try out.print("can't log into '{s}' \xe2\x80\x94 try /login codegraff | chatgpt | kimi | xai | zai (others: /key <provider> <key>)\n", .{target});+ try out.print("can't log into '{s}' \xe2\x80\x94 try /login codegraff | chatgpt | anthropic | console | kimi | xai | zai (others: /key <provider> <key>)\n", .{target});try out.flush();return true;}// Login wrote its credential file; pull the key into the live session.- reloadLoginKey(root, keys, arena, target);+ reloadLoginKey(root, keys, arena, if (console_login) "console" else target);try out.flush();return true;}diff --git a/src/credential_failover.zig b/src/credential_failover.zigindex e8e3438a..d97badf8 100644--- a/src/credential_failover.zig+++ b/src/credential_failover.zig@@ -97,6 +97,7 @@ pub fn preferPlan(.xai_device => oauth.loadXaiOAuth(io, gpa, arena, home, false, null),.zai_cli => oauth.loadZaiOAuth(io, gpa, arena, home, false, null),.chatgpt_browser => @import("oauth_chatgpt.zig").loadChatgptOAuth(io, gpa, arena, home, false, null),+ .anthropic_browser => @import("oauth_anthropic.zig").loadAnthropicOAuth(io, gpa, arena, home, false, null),else => null,} orelse return;if (value.*) |metered| park(spec.id, metered, source.*);diff --git a/src/engine_events.zig b/src/engine_events.zigindex b714d8e7..852fd968 100644--- a/src/engine_events.zig+++ b/src/engine_events.zig@@ -78,6 +78,18 @@ pub const ToolInvocation = struct {/// already capped and previewed; `ms` is its measured wall clock. `meta`/// marks a meta tool (schema.isMetaName) — those own their UX and, except/// for ask_user, never carried a result on the wire.+/// What a delegated MCP task reported about the context it was handed, and+/// whether its own cost is known. Only these facts cross into ACP `_meta`+/// (`codegraff/runTask`): the task's text and the caller's context do not+/// travel with them (ADR 0253).+pub const RunTaskMeta = struct {+ context_chars: u64 = 0,+ context_used_chars: u64 = 0,+ context_summarized: bool = false,+ usage_reported: bool = false,+ usage_unavailable_reason: []const u8 = "",+};+pub const ToolOutcome = struct {id: []const u8 = "",name: []const u8,@@ -87,6 +99,8 @@ pub const ToolOutcome = struct {ms: i64 = 0,meta: bool = false,ask_user: bool = false,+ /// Set only for a delegated MCP task that reported context metadata.+ run_task: ?RunTaskMeta = null,};/// A tool call the harness refused before it ran: the verifier boundary, a@@ -461,140 +475,3 @@ pub fn stamp(reserve: bool) Cursor {.sequence = if (reserve) protocol_seq.next() else protocol_seq.current(),};}--test "every variant constructs; only the wire deltas are durable" {- const wire: [2]EngineEvent = .{- .{ .reasoning_delta = .{ .text = "why" } },- .{ .text_delta = .{ .text = "hi" } },- };- for (wire) |ev| try std.testing.expect(durable(ev));- const pulses: [5]EngineEvent = .{- .stream_begin,- .thinking_fold_toggle,- .{ .stream_aborted = .stalled },- .{ .stream_complete = .{ .streamed_text = true } },- .stream_finished,- };- for (pulses) |ev| try std.testing.expect(!durable(ev));-}--test "stamp: reserving draws fresh monotonic ids; observing never advances" {- protocol_seq.resetForTest();- defer protocol_seq.resetForTest();- const a = stamp(true);- const b = stamp(true);- try std.testing.expectEqual(@as(u64, 1), a.sequence);- try std.testing.expectEqual(a.sequence + 1, b.sequence);- // A pulse rides at the last reserved position and reserves nothing.- const o = stamp(false);- try std.testing.expectEqual(b.sequence, o.sequence);- try std.testing.expectEqual(b.sequence, protocol_seq.current());- try std.testing.expectEqual(generation(), o.generation);-}--test "slice 1b: tool-arg prose and transport aborts are presentation pulses" {- // Neither ever appeared on the --json wire (argLiveDelta gates --json- // off; the transport notices were TTY-only), so neither may reserve a- // sequence id — promoting one is a schema_version event, not a default.- const pulses: [5]EngineEvent = .{- .{ .tool_arg_delta = .{ .text = "prose" } },- .{ .tool_output_delta = .{ .name = "bash", .text = "ok" } },- .{ .job_completed = .{ .id = 1, .exit_code = 0 } },- .{ .transport_aborted = .{ .reason = .stalled, .turn_ending = true } },- .{ .transport_aborted = .{ .reason = .dropped, .turn_ending = false } },- };- for (pulses) |ev| try std.testing.expect(!durable(ev));-}--test "slice 1c: the tool bracket is durable per TOOL, not per tag" {- const call: ToolInvocation = .{ .name = "bash", .input = .null };- const ask: ToolInvocation = .{ .name = "ask_user", .input = .null, .ask_user = true };- // An ordinary tool's bracket is the wire's tool_call/tool_call_started…- try std.testing.expect(durable(.{ .tool_call_announced = call }));- try std.testing.expect(durable(.{ .tool_call_started = call }));- // …while ask_user's reaches a --json client as the `ask_user` event, so- // neither half may reserve an id the wire will not spend (#330).- try std.testing.expect(!durable(.{ .tool_call_announced = ask }));- try std.testing.expect(!durable(.{ .tool_call_started = ask }));- // arg_streamed is a TUI-only suppression: the wire still carries the call.- const streamed: ToolInvocation = .{ .name = "bash", .input = .null, .arg_streamed = true };- try std.testing.expect(durable(.{ .tool_call_announced = streamed }));-- const ext: ToolOutcome = .{ .name = "bash", .text = "ok", .is_error = false };- const meta: ToolOutcome = .{ .name = "todo_write", .text = "ok", .is_error = false, .meta = true };- const answer: ToolOutcome = .{ .name = "ask_user", .text = "yes", .is_error = false, .meta = true, .ask_user = true };- try std.testing.expect(durable(.{ .tool_result = ext }));- try std.testing.expect(durable(.{ .tool_call_finished = ext }));- try std.testing.expect(!durable(.{ .tool_result = meta }));- try std.testing.expect(!durable(.{ .tool_call_finished = meta }));- try std.testing.expect(durable(.{ .tool_result = answer }));- try std.testing.expect(durable(.{ .tool_call_finished = answer }));-- // A refusal has always been a wire-only moment.- try std.testing.expect(durable(.{ .tool_rejected = .{ .name = "bash", .input = .null, .reason = "budget", .message = "no" } }));-}--test "slice 1c: the tool-cluster notices are presentation pulses" {- // None of these ever appeared on the --json wire, so promoting one is a- // schema_version event rather than refactor fallout.- const pulses: [6]EngineEvent = .{- .{ .parallel_batch_started = .{ .count = 3 } },- .{ .parallel_batch_finished = .{ .done = 2, .failed = 1, .cancelled = 0 } },- .completion_deferred,- .goal_completed,- .{ .completion_text = .{ .text = "done" } },- .{ .todo_list_updated = .{ .text = "todos" } },- };- for (pulses) |ev| try std.testing.expect(!durable(ev));-}--test "slice 2: the lifecycle cluster is pulses, except the failover the wire carries" {- const pulses: [9]EngineEvent = .{- .{ .session_notice = .{ .text = "loaded 2 saved approval(s)", .tone = .dim } },- .{ .session_banner = .{ .cwd = "/repo", .trace_path = ".graff/traces/a.jsonl" } },- .{ .worktree_entered = .{ .path = ".graff/worktrees/w", .branch = "worktree-w", .autocommit = true } },- .{ .shared_worktree_owner = .{ .session_id = "session-1", .pid = 42, .active_minutes = 5, .goal = "ship safely" } },- .{ .saved_model_unavailable = .{ .saved = "old", .model = "new", .provider = "codex", .blocked = false } },- .{ .mcp_consent_prompt = .{ .count = 3 } },- .{ .session_saved = .{ .name = "session-1", .ext = ".session.json" } },- .run_finished,- // A notice with a badge is still a notice, not a second variant.- .{ .session_notice = .{ .lead = "⚠ YOLO", .text = " mode", .tone = .alert } },- };- for (pulses) |ev| try std.testing.expect(!durable(ev));- // The failover has always reached a --json client as the `model` event, so- // it reserves an id like any other wire line.- try std.testing.expect(durable(.{ .provider_fallback = .{- .from_provider = "codex",- .from_model = "gpt-5.5",- .to_provider = "anthropic",- .to_model = "sonnet",- .to_context = 200_000,- .context_note = "context kept",- } }));-}--test "batch 3: the status line is a presentation pulse, never a wire line" {- // The --json wire has never had a prompt event and must not grow one by- // accident: a client that drives its own turns would read a burned- // sequence id as lost data (#330).- const status: PromptStatus = .{- .model = "gpt-5.6",- .provider_id = "codex",- .cwd = "~/src/graff",- .privacy_label = "Privacy:Aggregate",- .privacy = .aggregate,- .effort = .high,- .context = .{ .tokens = 12_345, .window = 200_000, .compact_at = 160_000 },- .cache_read = 2048,- .cost = .subscription,- };- try std.testing.expect(!durable(.{ .prompt_ready = status }));-}--test "generation only moves forward, one restart at a time" {- const before = generation();- try std.testing.expect(before >= 1);- try std.testing.expectEqual(before + 1, bumpGeneration());- try std.testing.expectEqual(before + 1, generation());-}diff --git a/src/engine_events_tests.zig b/src/engine_events_tests.zignew file mode 100644index 00000000..37db2e9a--- /dev/null+++ b/src/engine_events_tests.zig@@ -0,0 +1,153 @@+//! engine_events.zig's tests, split out when that file reached the 600-line+//! cap (the same move as workflow.zig -> workflow_test.zig). The vocabulary+//! itself stays in engine_events.zig; these only classify it.++const std = @import("std");+const ee = @import("engine_events.zig");+const protocol_seq = @import("protocol_seq.zig");++const EngineEvent = ee.EngineEvent;+const ToolInvocation = ee.ToolInvocation;+const ToolOutcome = ee.ToolOutcome;+const PromptStatus = ee.PromptStatus;+const durable = ee.durable;+const stamp = ee.stamp;+const generation = ee.generation;+const bumpGeneration = ee.bumpGeneration;++test "every variant constructs; only the wire deltas are durable" {+ const wire: [2]EngineEvent = .{+ .{ .reasoning_delta = .{ .text = "why" } },+ .{ .text_delta = .{ .text = "hi" } },+ };+ for (wire) |ev| try std.testing.expect(durable(ev));+ const pulses: [5]EngineEvent = .{+ .stream_begin,+ .thinking_fold_toggle,+ .{ .stream_aborted = .stalled },+ .{ .stream_complete = .{ .streamed_text = true } },+ .stream_finished,+ };+ for (pulses) |ev| try std.testing.expect(!durable(ev));+}++test "stamp: reserving draws fresh monotonic ids; observing never advances" {+ protocol_seq.resetForTest();+ defer protocol_seq.resetForTest();+ const a = stamp(true);+ const b = stamp(true);+ try std.testing.expectEqual(@as(u64, 1), a.sequence);+ try std.testing.expectEqual(a.sequence + 1, b.sequence);+ // A pulse rides at the last reserved position and reserves nothing.+ const o = stamp(false);+ try std.testing.expectEqual(b.sequence, o.sequence);+ try std.testing.expectEqual(b.sequence, protocol_seq.current());+ try std.testing.expectEqual(generation(), o.generation);+}++test "slice 1b: tool-arg prose and transport aborts are presentation pulses" {+ // Neither ever appeared on the --json wire (argLiveDelta gates --json+ // off; the transport notices were TTY-only), so neither may reserve a+ // sequence id — promoting one is a schema_version event, not a default.+ const pulses: [5]EngineEvent = .{+ .{ .tool_arg_delta = .{ .text = "prose" } },+ .{ .tool_output_delta = .{ .name = "bash", .text = "ok" } },+ .{ .job_completed = .{ .id = 1, .exit_code = 0 } },+ .{ .transport_aborted = .{ .reason = .stalled, .turn_ending = true } },+ .{ .transport_aborted = .{ .reason = .dropped, .turn_ending = false } },+ };+ for (pulses) |ev| try std.testing.expect(!durable(ev));+}++test "slice 1c: the tool bracket is durable per TOOL, not per tag" {+ const call: ToolInvocation = .{ .name = "bash", .input = .null };+ const ask: ToolInvocation = .{ .name = "ask_user", .input = .null, .ask_user = true };+ // An ordinary tool's bracket is the wire's tool_call/tool_call_started…+ try std.testing.expect(durable(.{ .tool_call_announced = call }));+ try std.testing.expect(durable(.{ .tool_call_started = call }));+ // …while ask_user's reaches a --json client as the `ask_user` event, so+ // neither half may reserve an id the wire will not spend (#330).+ try std.testing.expect(!durable(.{ .tool_call_announced = ask }));+ try std.testing.expect(!durable(.{ .tool_call_started = ask }));+ // arg_streamed is a TUI-only suppression: the wire still carries the call.+ const streamed: ToolInvocation = .{ .name = "bash", .input = .null, .arg_streamed = true };+ try std.testing.expect(durable(.{ .tool_call_announced = streamed }));++ const ext: ToolOutcome = .{ .name = "bash", .text = "ok", .is_error = false };+ const meta: ToolOutcome = .{ .name = "todo_write", .text = "ok", .is_error = false, .meta = true };+ const answer: ToolOutcome = .{ .name = "ask_user", .text = "yes", .is_error = false, .meta = true, .ask_user = true };+ try std.testing.expect(durable(.{ .tool_result = ext }));+ try std.testing.expect(durable(.{ .tool_call_finished = ext }));+ try std.testing.expect(!durable(.{ .tool_result = meta }));+ try std.testing.expect(!durable(.{ .tool_call_finished = meta }));+ try std.testing.expect(durable(.{ .tool_result = answer }));+ try std.testing.expect(durable(.{ .tool_call_finished = answer }));++ // A refusal has always been a wire-only moment.+ try std.testing.expect(durable(.{ .tool_rejected = .{ .name = "bash", .input = .null, .reason = "budget", .message = "no" } }));+}++test "slice 1c: the tool-cluster notices are presentation pulses" {+ // None of these ever appeared on the --json wire, so promoting one is a+ // schema_version event rather than refactor fallout.+ const pulses: [6]EngineEvent = .{+ .{ .parallel_batch_started = .{ .count = 3 } },+ .{ .parallel_batch_finished = .{ .done = 2, .failed = 1, .cancelled = 0 } },+ .completion_deferred,+ .goal_completed,+ .{ .completion_text = .{ .text = "done" } },+ .{ .todo_list_updated = .{ .text = "todos" } },+ };+ for (pulses) |ev| try std.testing.expect(!durable(ev));+}++test "slice 2: the lifecycle cluster is pulses, except the failover the wire carries" {+ const pulses: [9]EngineEvent = .{+ .{ .session_notice = .{ .text = "loaded 2 saved approval(s)", .tone = .dim } },+ .{ .session_banner = .{ .cwd = "/repo", .trace_path = ".graff/traces/a.jsonl" } },+ .{ .worktree_entered = .{ .path = ".graff/worktrees/w", .branch = "worktree-w", .autocommit = true } },+ .{ .shared_worktree_owner = .{ .session_id = "session-1", .pid = 42, .active_minutes = 5, .goal = "ship safely" } },+ .{ .saved_model_unavailable = .{ .saved = "old", .model = "new", .provider = "codex", .blocked = false } },+ .{ .mcp_consent_prompt = .{ .count = 3 } },+ .{ .session_saved = .{ .name = "session-1", .ext = ".session.json" } },+ .run_finished,+ // A notice with a badge is still a notice, not a second variant.+ .{ .session_notice = .{ .lead = "⚠ YOLO", .text = " mode", .tone = .alert } },+ };+ for (pulses) |ev| try std.testing.expect(!durable(ev));+ // The failover has always reached a --json client as the `model` event, so+ // it reserves an id like any other wire line.+ try std.testing.expect(durable(.{ .provider_fallback = .{+ .from_provider = "codex",+ .from_model = "gpt-5.5",+ .to_provider = "anthropic",+ .to_model = "sonnet",+ .to_context = 200_000,+ .context_note = "context kept",+ } }));+}++test "batch 3: the status line is a presentation pulse, never a wire line" {+ // The --json wire has never had a prompt event and must not grow one by+ // accident: a client that drives its own turns would read a burned+ // sequence id as lost data (#330).+ const status: PromptStatus = .{+ .model = "gpt-5.6",+ .provider_id = "codex",+ .cwd = "~/src/graff",+ .privacy_label = "Privacy:Aggregate",+ .privacy = .aggregate,+ .effort = .high,+ .context = .{ .tokens = 12_345, .window = 200_000, .compact_at = 160_000 },+ .cache_read = 2048,+ .cost = .subscription,+ };+ try std.testing.expect(!durable(.{ .prompt_ready = status }));+}++test "generation only moves forward, one restart at a time" {+ const before = generation();+ try std.testing.expect(before >= 1);+ try std.testing.expectEqual(before + 1, bumpGeneration());+ try std.testing.expectEqual(before + 1, generation());+}diff --git a/src/engine_sink.zig b/src/engine_sink.zigindex a5f5a7e5..351bf5bb 100644--- a/src/engine_sink.zig+++ b/src/engine_sink.zig@@ -422,7 +422,7 @@ fn jsonEmit(ctx: *anyopaque, ev: Stamped) void {.text_delta => |d| jsonLine(w, ev.cursor, .{ .type = "text", .text = d.text }),.tool_call_announced => |t| if (t.id.len > 0) jsonLine(w, ev.cursor, .{ .id = t.id, .type = "tool_call", .name = t.name, .input = t.input }) else jsonLine(w, ev.cursor, .{ .type = "tool_call", .name = t.name, .input = t.input }),.tool_call_started => |t| if (t.id.len > 0) jsonLine(w, ev.cursor, .{ .id = t.id, .type = "tool_call_started", .name = t.name, .input = t.input }) else jsonLine(w, ev.cursor, .{ .type = "tool_call_started", .name = t.name, .input = t.input }),- .tool_result => |r| if (r.id.len > 0) jsonLine(w, ev.cursor, .{ .id = r.id, .type = "tool_result", .name = r.name, .is_error = r.is_error, .text = r.text }) else jsonLine(w, ev.cursor, .{ .type = "tool_result", .name = r.name, .is_error = r.is_error, .text = r.text }),+ .tool_result => |r| writeToolResultJson(w, ev.cursor, r),.tool_call_finished => |r| if (r.id.len > 0) jsonLine(w, ev.cursor, .{ .id = r.id, .type = "tool_call_finished", .name = r.name, .is_error = r.is_error, .ms = r.ms }) else jsonLine(w, ev.cursor, .{ .type = "tool_call_finished", .name = r.name, .is_error = r.is_error, .ms = r.ms }),.tool_rejected => |r| if (r.id.len > 0) jsonLine(w, ev.cursor, .{ .id = r.id, .type = "tool_rejected", .name = r.name, .reason = r.reason, .input = r.input, .message = r.message }) else jsonLine(w, ev.cursor, .{ .type = "tool_rejected", .name = r.name, .reason = r.reason, .input = r.input, .message = r.message }),// The failover's wire half (slice 2). Its `note` is a FIXED wire@@ -451,6 +451,18 @@ fn jsonLine(w: *Io.Writer, cursor: engine_events.Cursor, payload: anytype) voidw.flush() catch return;}+/// The tool_result wire line. `run_task` is appended only when a delegated+/// MCP task reported it: every other result keeps the exact bytes it had, so+/// a client parsing this stream sees no new field it did not ask for.+fn writeToolResultJson(w: *Io.Writer, cursor: engine_events.Cursor, r: engine_events.ToolOutcome) void {+ const m = r.run_task orelse {+ if (r.id.len > 0) return jsonLine(w, cursor, .{ .id = r.id, .type = "tool_result", .name = r.name, .is_error = r.is_error, .text = r.text });+ return jsonLine(w, cursor, .{ .type = "tool_result", .name = r.name, .is_error = r.is_error, .text = r.text });+ };+ if (r.id.len > 0) return jsonLine(w, cursor, .{ .id = r.id, .type = "tool_result", .name = r.name, .is_error = r.is_error, .text = r.text, .run_task = m });+ jsonLine(w, cursor, .{ .type = "tool_result", .name = r.name, .is_error = r.is_error, .text = r.text, .run_task = m });+}+pub fn shortTool(name: []const u8) []const u8 {if (std.mem.eql(u8, name, "read_file")) return "read";if (std.mem.eql(u8, name, "write_file")) return "write";diff --git a/src/exec.zig b/src/exec.zigindex 88761b0d..8fbd08fb 100644--- a/src/exec.zig+++ b/src/exec.zig@@ -190,13 +190,13 @@ fn execToolInner(ctx: ToolCtx, call: ToolCall) !ToolOutput {if (r.is_error) codedbpro_report.onFailure(ctx, call.name, r.text);if (!r.is_error) {const shapes = @import("mcp_shapes.zig");- shapes.remember(ctx, call.name, r.text);+ shapes.remember(ctx, call.name, r.structured_text orelse r.text);// ADR 0238: an rlm bind keeps every field. rlm slims only what it// prints, so a script can still project or save a dropped field.- if (ctx.rlm_host) return .{ .text = r.text, .is_error = false };- return .{ .text = shapes.takeSlimKept(gpa, .{ .io = io, .dir = .cwd() }, r.text), .is_error = false };+ if (ctx.rlm_host) return .{ .text = try r.takeHostText(gpa), .is_error = false, .run_task = r.run_task };+ return .{ .text = shapes.takeSlimKept(gpa, .{ .io = io, .dir = .cwd() }, r.text), .is_error = false, .run_task = r.run_task };}- return .{ .text = r.text, .is_error = r.is_error };+ return .{ .text = r.text, .is_error = r.is_error, .run_task = r.run_task };}const input = call.input;diff --git a/src/goal_state.zig b/src/goal_state.zigindex 6648229a..b2d487e5 100644--- a/src/goal_state.zig+++ b/src/goal_state.zig@@ -25,6 +25,12 @@ const TodoItem = agent_mod.TodoItem;const goal_todo = @import("goal_todo.zig");const repl_glue = @import("repl_glue.zig");+/// Dropped = abandoned by user order, not finished (#1545): kept as the+/// record, invisible to every epoch-scoped query, never nagged for.+pub fn isDropped(status: []const u8) bool {+ return std.mem.eql(u8, status, "dropped");+}+/// Epoch for a goal that replaces `old`, above every epoch already present in/// `todos` (0 is the no-goal/legacy epoch, so the first goal gets 1). Deriving/// it from the live goal alone was not enough (#318): /goal clear nulls the goal@@ -50,24 +56,25 @@ pub fn currentEpoch(goal: ?agent_mod.Goal) u64 {return if (g.status == .complete) 0 else g.epoch;}-/// Not-yet-completed items belonging to `epoch` (retired ones never count).+/// Open items in `epoch` (retired, cancelled and dropped never count, #1545).pub fn openCount(todos: []const TodoItem, epoch: u64) usize {var n: usize = 0;for (todos) |t| {- if (t.epoch == epoch and !t.retired and !t.closed()) n += 1; // #1545: cancelled is parked, not open+ if (t.epoch == epoch and !t.retired and !t.closed() and !isDropped(t.status)) n += 1;}return n;}-/// True only when `epoch` has a nonempty checklist and every item is completed+/// True when `epoch` has a nonempty checklist and every live item is closed.+/// Dropped items are retained history, not completion evidence (#1545)./// (#226 — an empty list is "no plan yet", never "done"). An epoch left holding/// only RETIRED items is empty by this rule, so retiring a finished checklist/// can never read as a fresh completion (#394).pub fn allDone(todos: []const TodoItem, epoch: u64) bool {var seen = false;for (todos) |t| {- if (t.epoch != epoch or t.retired) continue;- if (!t.closed()) return false; // #1545: a cancelled item closes, but never verifies (goal_verify)+ if (t.epoch != epoch or t.retired or isDropped(t.status)) continue;+ if (!t.closed()) return false; // cancelled closes scope, but never verifies (goal_verify)seen = true;}return seen;@@ -80,8 +87,8 @@ pub fn allDone(todos: []const TodoItem, epoch: u64) bool {pub fn parkedOpenCount(todos: []const TodoItem, keep_epoch: u64) usize {var parked: usize = 0;for (todos) |t| {- if (t.epoch == keep_epoch or t.retired) continue;- if (!std.mem.eql(u8, t.status, "completed")) parked += 1;+ if (t.epoch == keep_epoch or t.retired or isDropped(t.status)) continue;+ if (!t.closed()) parked += 1;}return parked;}@@ -97,7 +104,7 @@ pub fn parkedOpenCount(todos: []const TodoItem, keep_epoch: u64) usize {/// items keep their old epoch, i.e. they park.pub fn adoptTodos(todos: []TodoItem, from_epoch: u64, to_epoch: u64) void {for (todos) |*t| {- if (t.epoch != from_epoch or std.mem.eql(u8, t.status, "completed")) continue;+ if (t.epoch != from_epoch or t.retired or t.closed() or isDropped(t.status)) continue;t.epoch = to_epoch;}}@@ -304,7 +311,10 @@ pub fn renderTodos(self: *Agent, epoch: u64) []const u8 {"[x]"else if (std.mem.eql(u8, t.status, "in_progress"))"[~]"- else if (std.mem.eql(u8, t.status, "cancelled")) "[-]" else "[ ]";+ else if (std.mem.eql(u8, t.status, "cancelled") or isDropped(t.status))+ "[-]"+ else+ "[ ]";w.print("{s} {s}\n", .{ mark, t.content }) catch break;}return std.mem.trimEnd(u8, aw.writer.buffered(), "\n");@@ -317,283 +327,3 @@ pub fn renderCurrent(root: *Agent) []const u8 {if (!hasCurrent(root.todos.items, epoch)) return "";return renderTodos(root, epoch);}--test "epochs: nextEpoch clears the live goal AND every parked epoch; currentEpoch 0 without a goal" {- const parked = [_]TodoItem{- .{ .content = "parked by a cleared goal", .status = "pending", .epoch = 4 },- .{ .content = "older", .status = "completed", .epoch = 2 },- };- try std.testing.expectEqual(@as(u64, 1), nextEpoch(null, &.{}));- try std.testing.expectEqual(@as(u64, 4), nextEpoch(.{ .objective = "a", .epoch = 3 }, &.{}));- // No live goal (/goal clear) but parked items: the next goal lands ABOVE- // them instead of colliding with epoch 1 and adopting someone else's list.- try std.testing.expectEqual(@as(u64, 5), nextEpoch(null, &parked));- // A live goal below the high-water mark does not drag the next epoch down.- try std.testing.expectEqual(@as(u64, 5), nextEpoch(.{ .objective = "a", .epoch = 1 }, &parked));- try std.testing.expectEqual(@as(u64, 0), currentEpoch(null));- try std.testing.expectEqual(@as(u64, 7), currentEpoch(.{ .objective = "a", .epoch = 7 }));- // A completed goal is a dead epoch: later todo_write calls are unscoped- // again and its own checklist stays parked (#318 D1). Paused/blocked goals- // are alive - they keep stamping.- try std.testing.expectEqual(@as(u64, 0), currentEpoch(.{ .objective = "a", .epoch = 7, .status = .complete }));- try std.testing.expectEqual(@as(u64, 7), currentEpoch(.{ .objective = "a", .epoch = 7, .status = .paused }));-}--test "openCount/allDone are epoch-scoped; empty checklist is never done (#226)" {- const todos = [_]TodoItem{- .{ .content = "old work", .status = "pending", .epoch = 1 },- .{ .content = "done here", .status = "completed", .epoch = 2 },- .{ .content = "also done", .status = "completed", .epoch = 2 },- };- try std.testing.expectEqual(@as(usize, 1), openCount(&todos, 1));- try std.testing.expectEqual(@as(usize, 0), openCount(&todos, 2));- try std.testing.expect(!allDone(&todos, 1)); // open item- try std.testing.expect(allDone(&todos, 2)); // all its items completed- try std.testing.expect(!allDone(&todos, 3)); // no items at all- try std.testing.expect(!allDone(&.{}, 0));-}--test "parking retains: superseded items are counted and kept, never deleted (#318)" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const ar = arena_state.allocator();- var todos: std.ArrayList(TodoItem) = .empty;- try todos.append(ar, .{ .content = "phase3 audit", .status = "pending", .epoch = 1 });- try todos.append(ar, .{ .content = "phase3 fix", .status = "completed", .epoch = 1 });- try todos.append(ar, .{ .content = "phase2 test", .status = "in_progress", .epoch = 2 });- try std.testing.expectEqual(@as(usize, 1), parkedOpenCount(todos.items, 2)); // only the OPEN phase3 item- try std.testing.expectEqual(@as(usize, 3), todos.items.len); // a mistyped /goal deletes nothing- try std.testing.expectEqual(@as(u64, 1), todos.items[0].epoch); // parked work keeps its own epoch- // Retention is safe because every query is epoch-scoped: parked items are- // invisible to the new goal's open/allDone accounting. (todo_write's- // epoch-scoped replace lives in goal_todo.zig with its own tests.)- try std.testing.expectEqual(@as(usize, 1), openCount(todos.items, 2));- try std.testing.expect(!allDone(todos.items, 2));-}--test "adoptTodos takes OPEN current-epoch items only; finished and parked work stays put" {- var todos = [_]TodoItem{- .{ .content = "a", .status = "pending", .epoch = 0 },- .{ .content = "b", .status = "completed", .epoch = 0 },- .{ .content = "parked by a finished goal", .status = "pending", .epoch = 3 },- };- adoptTodos(&todos, 0, 5);- try std.testing.expectEqual(@as(u64, 5), todos[0].epoch);- try std.testing.expectEqual(@as(u64, 0), todos[1].epoch); // finished work is not the new goal's plan- try std.testing.expectEqual(@as(u64, 3), todos[2].epoch);- // The new goal inherits only real remaining work, so its accounting is honest.- try std.testing.expectEqual(@as(usize, 1), openCount(&todos, 5));- try std.testing.expect(!allDone(&todos, 5));-}--test "a goal formalized over an already-finished list adopts nothing and is not born done (#318)" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const ar = arena_state.allocator();- var root: Agent = undefined;- root.sub = false;- root.review_mode = false;- root.arena = ar;- root.todos = .empty;- root.completion_gate_armed = false;- root.goal = null;- try root.todos.append(ar, .{ .content = "done before the goal", .status = "completed", .epoch = 0 });- adoptTodos(root.todos.items, currentEpoch(root.goal), 1);- root.goal = .{ .objective = "ship", .epoch = 1 };- try std.testing.expect(!hasCurrent(root.todos.items, 1)); // nothing adopted- // Without this the goal would satisfy "checklist present, 0 open" on turn 1- // and complete before any of its work happened; now the gate asks first.- const r = try completionGate(ar, &root);- try std.testing.expect(r != null and std.mem.indexOf(u8, r.?, "no checklist") != null);-}--test "checklistFinished demands THIS process's todo_write; a restored all-[x] list is inert (#318)" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const ar = arena_state.allocator();- var root: Agent = undefined;- root.arena = ar;- root.todos = .empty;- root.goal = null;- root.todos_dirty = false; // exactly what loadSession leaves behind- try root.todos.append(ar, .{ .content = "old work", .status = "completed", .epoch = 0 });- // The goal == null variant: a goal-less run must not stop at iteration 1 just- // because a previous session left a finished epoch-0 list on disk.- try std.testing.expect(allDone(root.todos.items, 0));- try std.testing.expect(!checklistFinished(&root));- root.goal = .{ .objective = "ship", .epoch = 2, .status = .active };- try root.todos.append(ar, .{ .content = "restored item", .status = "completed", .epoch = 2 });- try std.testing.expect(!checklistFinished(&root)); // same for a restored goal's own list- // todo_write in this process is the evidence, and it clears the gate arm too.- root.completion_gate_armed = true;- noteTodoWrite(&root);- try std.testing.expect(root.todos_dirty and !root.completion_gate_armed);- try std.testing.expect(checklistFinished(&root));- // An open item in the current epoch still blocks it.- try root.todos.append(ar, .{ .content = "new work", .status = "pending", .epoch = 2 });- try std.testing.expect(!checklistFinished(&root));-}--test "reconcileRestored retires a restored-active goal whose checklist is already done (#318)" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const ar = arena_state.allocator();- var root: Agent = undefined;- root.arena = ar;- root.todos = .empty;- root.goal = null;- try std.testing.expect(!reconcileRestored(&root)); // no goal- root.goal = .{ .objective = "ship", .epoch = 2, .status = .active };- try std.testing.expect(!reconcileRestored(&root)); // no checklist is not "done" (#226)- try root.todos.append(ar, .{ .content = "a", .status = "completed", .epoch = 2 });- try root.todos.append(ar, .{ .content = "b", .status = "pending", .epoch = 2 });- try std.testing.expect(!reconcileRestored(&root)); // still open work- root.todos.items[1].status = "completed";- try std.testing.expect(reconcileRestored(&root));- try std.testing.expectEqual(agent_mod.GoalStatus.complete, root.goal.?.status);- try std.testing.expect(!reconcileRestored(&root)); // idempotent: only an ACTIVE goal reconciles- // A retired goal stops steering and stops rendering its parked list.- try std.testing.expectEqualStrings("", renderCurrent(&root));- // Parked work from an earlier goal never triggers reconciliation.- root.goal = .{ .objective = "next", .epoch = 3, .status = .active };- try std.testing.expect(!reconcileRestored(&root));-}--test "renderTodos is epoch-scoped; renderCurrent goes quiet on a dead epoch (#318)" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const ar = arena_state.allocator();- var root: Agent = undefined;- root.arena = ar;- root.todos = .empty;- root.goal = .{ .objective = "ship", .epoch = 2 };- try root.todos.append(ar, .{ .content = "old phase3 item", .status = "pending", .epoch = 1 });- try root.todos.append(ar, .{ .content = "current item", .status = "in_progress", .epoch = 2 });- try std.testing.expectEqualStrings("[~] current item", renderTodos(&root, 2));- try std.testing.expectEqualStrings("[ ] old phase3 item", renderTodos(&root, 1)); // retained, still readable- try std.testing.expectEqualStrings("[~] current item", renderCurrent(&root));- try std.testing.expectEqualStrings("(no todos)", renderTodos(&root, 9));- // A completed goal has no current epoch: its list parks, nothing renders.- root.goal.?.status = .complete;- try std.testing.expectEqualStrings("", renderCurrent(&root));-}--test "the completion double-check survives the turn boundary, re-arms on change (#318)" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const ar = arena_state.allocator();- var root: Agent = undefined;- root.sub = false;- root.review_mode = false;- root.arena = ar;- root.todos = .empty;- root.completion_gate_armed = false;- root.completion_refused = false;- root.goal = .{ .objective = "ship", .epoch = 1 };- try root.todos.append(ar, .{ .content = "a", .status = "pending", .epoch = 1 });- // Turn 1: refused; the caller arms the gate and records that the turn worked.- try std.testing.expect((try completionGate(ar, &root)) != null);- noteCompletionRefused(&root);- try std.testing.expect(root.completion_refused);- // Turn boundary: only the per-turn flag clears. A model emits one- // attempt_completion per turn, so clearing the arm here made the refusal- // unresolvable forever - the #318 fix reappearing as the #318 bug.- beginTurn(&root, true); // a new user ask: the arm still survives it, and the open checklist is not retirable- try std.testing.expect(!root.completion_refused);- try std.testing.expect(root.completion_gate_armed);- try std.testing.expect((try completionGate(ar, &root)) == null);- // New evidence (todo_write, or a /goal change) restores the double-check.- resetCompletionGate(&root);- try std.testing.expect((try completionGate(ar, &root)) != null);-}--test "steeringGate: inject on change, suppress repeats, refresh after the interval, reset on empty" {- // Empty note (no goal / paused): nothing injected, fingerprint resets so a- // resumed goal re-injects immediately.- const off = steeringGate("", 123, 5, 8);- try std.testing.expect(!off.inject and off.fp == 0 and off.age == 0);- // First sight of a note: inject.- const g1 = steeringGate("[standing goal: x]", 0, 0, 8);- try std.testing.expect(g1.inject);- // Verbatim repeat: suppressed, age climbs.- const g2 = steeringGate("[standing goal: x]", g1.fp, g1.age, 8);- try std.testing.expect(!g2.inject and g2.age == 1);- // Changed note (new objective/epoch): inject again.- const g3 = steeringGate("[standing goal: y]", g2.fp, g2.age, 8);- try std.testing.expect(g3.inject);- // refresh_every 0 never time-refreshes (ADR 0005: prefix holds the objective).- const held = steeringGate("[standing goal: y]", g3.fp, 99, 0);- try std.testing.expect(!held.inject and held.age == 100);- // A non-zero interval still re-states when it lapses.- const g4 = steeringGate("[standing goal: y]", g3.fp, 7, 8);- try std.testing.expect(g4.inject and g4.age == 0);-}--test "completionGate: open list refused once, no-checklist needs confirm, done list accepts, review exempt (#318)" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const ar = arena_state.allocator();- var root: Agent = undefined;- root.sub = false;- root.review_mode = false;- root.arena = ar;- root.todos = .empty;- root.completion_gate_armed = false;- root.goal = .{ .objective = "ship", .epoch = 1 };- // No checklist yet: first call must confirm; an armed second call closes.- const r1 = try completionGate(ar, &root);- try std.testing.expect(r1 != null and std.mem.indexOf(u8, r1.?, "no checklist") != null);- root.completion_gate_armed = true;- try std.testing.expect((try completionGate(ar, &root)) == null);- // Open current-epoch item: refused with the count.- root.completion_gate_armed = false;- try root.todos.append(ar, .{ .content = "a", .status = "pending", .epoch = 1 });- const r2 = try completionGate(ar, &root);- try std.testing.expect(r2 != null and std.mem.indexOf(u8, r2.?, "1 open") != null);- // Fully completed checklist: accepted immediately.- root.todos.items[0].status = "completed";- try std.testing.expect((try completionGate(ar, &root)) == null);- // Review turns never gate (isolated reviews share the root Agent).- root.review_mode = true;- root.todos.items[0].status = "pending";- try std.testing.expect((try completionGate(ar, &root)) == null);- try std.testing.expect(!goalActive(&root));-}--test "applyGoalSteering: injects once, suppresses repeats, consumes the one-shot note" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const ar = arena_state.allocator();- var root: Agent = undefined;- root.review_mode = false;- root.goal = .{ .objective = "ship it", .epoch = 1 };- root.goal_note_fp = 0;- root.goal_note_age = 0;- root.pending_goal_note = null;- const m1 = try applyGoalSteering(ar, &root, "hi");- try std.testing.expect(std.mem.indexOf(u8, m1, "[standing goal: ship it") != null);- const m2 = try applyGoalSteering(ar, &root, "hi");- try std.testing.expectEqualStrings("hi", m2); // verbatim repeat suppressed- root.pending_goal_note = "[goal update: x]";- const m3 = try applyGoalSteering(ar, &root, "hi");- try std.testing.expect(std.mem.indexOf(u8, m3, "[goal update: x]") != null);- try std.testing.expect(root.pending_goal_note == null); // one-shot consumed- // Review turns are isolated: no steering, gate state untouched.- root.review_mode = true;- root.pending_goal_note = "[goal update: y]";- try std.testing.expectEqualStrings("hi", try applyGoalSteering(ar, &root, "hi"));- try std.testing.expect(root.pending_goal_note != null); // preserved for the next normal turn-}--test "supersededNote and clearedNote name the parked work" {- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena_state.deinit();- const ar = arena_state.allocator();- const s1 = try supersededNote(ar, "phase 3", 4);- try std.testing.expect(std.mem.indexOf(u8, s1, "supersedes the previous goal \"phase 3\"") != null);- try std.testing.expect(std.mem.indexOf(u8, s1, "4 unfinished") != null);- const s2 = try supersededNote(ar, "phase 3", 0);- try std.testing.expect(std.mem.indexOf(u8, s2, "parked") == null);- const c = try clearedNote(ar, 2);- try std.testing.expect(std.mem.indexOf(u8, c, "2 unfinished") != null);-}diff --git a/src/goal_state_tests.zig b/src/goal_state_tests.zignew file mode 100644index 00000000..878f8849--- /dev/null+++ b/src/goal_state_tests.zig@@ -0,0 +1,311 @@+//! Unit tests for goal_state.zig's epoch-scoped queries (#226, #318, #394,+//! #1545): kept out of goal_state.zig (600-line goal) and reached through the+//! `test { _ = ... }` hook in main.zig, mirroring goal_persist_tests.zig.++const std = @import("std");+const agent_mod = @import("agent.zig");+const goal_state = @import("goal_state.zig");+const Agent = agent_mod.Agent;+const TodoItem = agent_mod.TodoItem;+const nextEpoch = goal_state.nextEpoch;+const currentEpoch = goal_state.currentEpoch;+const openCount = goal_state.openCount;+const allDone = goal_state.allDone;+const parkedOpenCount = goal_state.parkedOpenCount;+const adoptTodos = goal_state.adoptTodos;+const hasCurrent = goal_state.hasCurrent;+const completionGate = goal_state.completionGate;+const checklistFinished = goal_state.checklistFinished;+const noteTodoWrite = goal_state.noteTodoWrite;+const noteCompletionRefused = goal_state.noteCompletionRefused;+const beginTurn = goal_state.beginTurn;+const resetCompletionGate = goal_state.resetCompletionGate;+const steeringGate = goal_state.steeringGate;+const goalActive = goal_state.goalActive;+const reconcileRestored = goal_state.reconcileRestored;+const renderCurrent = goal_state.renderCurrent;+const renderTodos = goal_state.renderTodos;+const applyGoalSteering = goal_state.applyGoalSteering;+const supersededNote = goal_state.supersededNote;+const clearedNote = goal_state.clearedNote;+const isDropped = goal_state.isDropped;++test "epochs: nextEpoch clears the live goal AND every parked epoch; currentEpoch 0 without a goal" {+ const parked = [_]TodoItem{+ .{ .content = "parked by a cleared goal", .status = "pending", .epoch = 4 },+ .{ .content = "older", .status = "completed", .epoch = 2 },+ };+ try std.testing.expectEqual(@as(u64, 1), nextEpoch(null, &.{}));+ try std.testing.expectEqual(@as(u64, 4), nextEpoch(.{ .objective = "a", .epoch = 3 }, &.{}));+ // No live goal (/goal clear) but parked items: the next goal lands ABOVE+ // them instead of colliding with epoch 1 and adopting someone else's list.+ try std.testing.expectEqual(@as(u64, 5), nextEpoch(null, &parked));+ // A live goal below the high-water mark does not drag the next epoch down.+ try std.testing.expectEqual(@as(u64, 5), nextEpoch(.{ .objective = "a", .epoch = 1 }, &parked));+ try std.testing.expectEqual(@as(u64, 0), currentEpoch(null));+ try std.testing.expectEqual(@as(u64, 7), currentEpoch(.{ .objective = "a", .epoch = 7 }));+ // A completed goal is a dead epoch: later todo_write calls are unscoped+ // again and its own checklist stays parked (#318 D1). Paused/blocked goals+ // are alive - they keep stamping.+ try std.testing.expectEqual(@as(u64, 0), currentEpoch(.{ .objective = "a", .epoch = 7, .status = .complete }));+ try std.testing.expectEqual(@as(u64, 7), currentEpoch(.{ .objective = "a", .epoch = 7, .status = .paused }));+}++test "openCount/allDone are epoch-scoped; empty checklist is never done (#226)" {+ const todos = [_]TodoItem{+ .{ .content = "old work", .status = "pending", .epoch = 1 },+ .{ .content = "done here", .status = "completed", .epoch = 2 },+ .{ .content = "also done", .status = "completed", .epoch = 2 },+ };+ try std.testing.expectEqual(@as(usize, 1), openCount(&todos, 1));+ try std.testing.expectEqual(@as(usize, 0), openCount(&todos, 2));+ try std.testing.expect(!allDone(&todos, 1)); // open item+ try std.testing.expect(allDone(&todos, 2)); // all its items completed+ try std.testing.expect(!allDone(&todos, 3)); // no items at all+ try std.testing.expect(!allDone(&.{}, 0));+}++test "parking retains: superseded items are counted and kept, never deleted (#318)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ var todos: std.ArrayList(TodoItem) = .empty;+ try todos.append(ar, .{ .content = "phase3 audit", .status = "pending", .epoch = 1 });+ try todos.append(ar, .{ .content = "phase3 fix", .status = "completed", .epoch = 1 });+ try todos.append(ar, .{ .content = "phase2 test", .status = "in_progress", .epoch = 2 });+ try std.testing.expectEqual(@as(usize, 1), parkedOpenCount(todos.items, 2)); // only the OPEN phase3 item+ try std.testing.expectEqual(@as(usize, 3), todos.items.len); // a mistyped /goal deletes nothing+ try std.testing.expectEqual(@as(u64, 1), todos.items[0].epoch); // parked work keeps its own epoch+ // Retention is safe because every query is epoch-scoped: parked items are+ // invisible to the new goal's open/allDone accounting. (todo_write's+ // epoch-scoped replace lives in goal_todo.zig with its own tests.)+ try std.testing.expectEqual(@as(usize, 1), openCount(todos.items, 2));+ try std.testing.expect(!allDone(todos.items, 2));+}++test "adoptTodos takes OPEN current-epoch items only; finished and parked work stays put" {+ var todos = [_]TodoItem{+ .{ .content = "a", .status = "pending", .epoch = 0 },+ .{ .content = "b", .status = "completed", .epoch = 0 },+ .{ .content = "parked by a finished goal", .status = "pending", .epoch = 3 },+ };+ adoptTodos(&todos, 0, 5);+ try std.testing.expectEqual(@as(u64, 5), todos[0].epoch);+ try std.testing.expectEqual(@as(u64, 0), todos[1].epoch); // finished work is not the new goal's plan+ try std.testing.expectEqual(@as(u64, 3), todos[2].epoch);+ // The new goal inherits only real remaining work, so its accounting is honest.+ try std.testing.expectEqual(@as(usize, 1), openCount(&todos, 5));+ try std.testing.expect(!allDone(&todos, 5));+}++test "a goal formalized over an already-finished list adopts nothing and is not born done (#318)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ var root: Agent = undefined;+ root.sub = false;+ root.review_mode = false;+ root.arena = ar;+ root.todos = .empty;+ root.completion_gate_armed = false;+ root.goal = null;+ try root.todos.append(ar, .{ .content = "done before the goal", .status = "completed", .epoch = 0 });+ adoptTodos(root.todos.items, currentEpoch(root.goal), 1);+ root.goal = .{ .objective = "ship", .epoch = 1 };+ try std.testing.expect(!hasCurrent(root.todos.items, 1)); // nothing adopted+ // Without this the goal would satisfy "checklist present, 0 open" on turn 1+ // and complete before any of its work happened; now the gate asks first.+ const r = try completionGate(ar, &root);+ try std.testing.expect(r != null and std.mem.indexOf(u8, r.?, "no checklist") != null);+}++test "checklistFinished demands THIS process's todo_write; a restored all-[x] list is inert (#318)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ var root: Agent = undefined;+ root.arena = ar;+ root.todos = .empty;+ root.goal = null;+ root.todos_dirty = false; // exactly what loadSession leaves behind+ try root.todos.append(ar, .{ .content = "old work", .status = "completed", .epoch = 0 });+ // The goal == null variant: a goal-less run must not stop at iteration 1 just+ // because a previous session left a finished epoch-0 list on disk.+ try std.testing.expect(allDone(root.todos.items, 0));+ try std.testing.expect(!checklistFinished(&root));+ root.goal = .{ .objective = "ship", .epoch = 2, .status = .active };+ try root.todos.append(ar, .{ .content = "restored item", .status = "completed", .epoch = 2 });+ try std.testing.expect(!checklistFinished(&root)); // same for a restored goal's own list+ // todo_write in this process is the evidence, and it clears the gate arm too.+ root.completion_gate_armed = true;+ noteTodoWrite(&root);+ try std.testing.expect(root.todos_dirty and !root.completion_gate_armed);+ try std.testing.expect(checklistFinished(&root));+ // An open item in the current epoch still blocks it.+ try root.todos.append(ar, .{ .content = "new work", .status = "pending", .epoch = 2 });+ try std.testing.expect(!checklistFinished(&root));+}++test "reconcileRestored retires a restored-active goal whose checklist is already done (#318)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ var root: Agent = undefined;+ root.arena = ar;+ root.todos = .empty;+ root.goal = null;+ try std.testing.expect(!reconcileRestored(&root)); // no goal+ root.goal = .{ .objective = "ship", .epoch = 2, .status = .active };+ try std.testing.expect(!reconcileRestored(&root)); // no checklist is not "done" (#226)+ try root.todos.append(ar, .{ .content = "a", .status = "completed", .epoch = 2 });+ try root.todos.append(ar, .{ .content = "b", .status = "pending", .epoch = 2 });+ try std.testing.expect(!reconcileRestored(&root)); // still open work+ root.todos.items[1].status = "completed";+ try std.testing.expect(reconcileRestored(&root));+ try std.testing.expectEqual(agent_mod.GoalStatus.complete, root.goal.?.status);+ try std.testing.expect(!reconcileRestored(&root)); // idempotent: only an ACTIVE goal reconciles+ // A retired goal stops steering and stops rendering its parked list.+ try std.testing.expectEqualStrings("", renderCurrent(&root));+ // Parked work from an earlier goal never triggers reconciliation.+ root.goal = .{ .objective = "next", .epoch = 3, .status = .active };+ try std.testing.expect(!reconcileRestored(&root));+}++test "renderTodos is epoch-scoped; renderCurrent goes quiet on a dead epoch (#318)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ var root: Agent = undefined;+ root.arena = ar;+ root.todos = .empty;+ root.goal = .{ .objective = "ship", .epoch = 2 };+ try root.todos.append(ar, .{ .content = "old phase3 item", .status = "pending", .epoch = 1 });+ try root.todos.append(ar, .{ .content = "current item", .status = "in_progress", .epoch = 2 });+ try std.testing.expectEqualStrings("[~] current item", renderTodos(&root, 2));+ try std.testing.expectEqualStrings("[ ] old phase3 item", renderTodos(&root, 1)); // retained, still readable+ try std.testing.expectEqualStrings("[~] current item", renderCurrent(&root));+ try std.testing.expectEqualStrings("(no todos)", renderTodos(&root, 9));+ // A completed goal has no current epoch: its list parks, nothing renders.+ root.goal.?.status = .complete;+ try std.testing.expectEqualStrings("", renderCurrent(&root));+}++test "the completion double-check survives the turn boundary, re-arms on change (#318)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ var root: Agent = undefined;+ root.sub = false;+ root.review_mode = false;+ root.arena = ar;+ root.todos = .empty;+ root.completion_gate_armed = false;+ root.completion_refused = false;+ root.goal = .{ .objective = "ship", .epoch = 1 };+ try root.todos.append(ar, .{ .content = "a", .status = "pending", .epoch = 1 });+ // Turn 1: refused; the caller arms the gate and records that the turn worked.+ try std.testing.expect((try completionGate(ar, &root)) != null);+ noteCompletionRefused(&root);+ try std.testing.expect(root.completion_refused);+ // Turn boundary: only the per-turn flag clears. A model emits one+ // attempt_completion per turn, so clearing the arm here made the refusal+ // unresolvable forever - the #318 fix reappearing as the #318 bug.+ beginTurn(&root, true); // a new user ask: the arm still survives it, and the open checklist is not retirable+ try std.testing.expect(!root.completion_refused);+ try std.testing.expect(root.completion_gate_armed);+ try std.testing.expect((try completionGate(ar, &root)) == null);+ // New evidence (todo_write, or a /goal change) restores the double-check.+ resetCompletionGate(&root);+ try std.testing.expect((try completionGate(ar, &root)) != null);+}++test "steeringGate: inject on change, suppress repeats, refresh after the interval, reset on empty" {+ // Empty note (no goal / paused): nothing injected, fingerprint resets so a+ // resumed goal re-injects immediately.+ const off = steeringGate("", 123, 5, 8);+ try std.testing.expect(!off.inject and off.fp == 0 and off.age == 0);+ // First sight of a note: inject.+ const g1 = steeringGate("[standing goal: x]", 0, 0, 8);+ try std.testing.expect(g1.inject);+ // Verbatim repeat: suppressed, age climbs.+ const g2 = steeringGate("[standing goal: x]", g1.fp, g1.age, 8);+ try std.testing.expect(!g2.inject and g2.age == 1);+ // Changed note (new objective/epoch): inject again.+ const g3 = steeringGate("[standing goal: y]", g2.fp, g2.age, 8);+ try std.testing.expect(g3.inject);+ // refresh_every 0 never time-refreshes (ADR 0005: prefix holds the objective).+ const held = steeringGate("[standing goal: y]", g3.fp, 99, 0);+ try std.testing.expect(!held.inject and held.age == 100);+ // A non-zero interval still re-states when it lapses.+ const g4 = steeringGate("[standing goal: y]", g3.fp, 7, 8);+ try std.testing.expect(g4.inject and g4.age == 0);+}++test "completionGate: open list refused once, no-checklist needs confirm, done list accepts, review exempt (#318)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ var root: Agent = undefined;+ root.sub = false;+ root.review_mode = false;+ root.arena = ar;+ root.todos = .empty;+ root.completion_gate_armed = false;+ root.goal = .{ .objective = "ship", .epoch = 1 };+ // No checklist yet: first call must confirm; an armed second call closes.+ const r1 = try completionGate(ar, &root);+ try std.testing.expect(r1 != null and std.mem.indexOf(u8, r1.?, "no checklist") != null);+ root.completion_gate_armed = true;+ try std.testing.expect((try completionGate(ar, &root)) == null);+ // Open current-epoch item: refused with the count.+ root.completion_gate_armed = false;+ try root.todos.append(ar, .{ .content = "a", .status = "pending", .epoch = 1 });+ const r2 = try completionGate(ar, &root);+ try std.testing.expect(r2 != null and std.mem.indexOf(u8, r2.?, "1 open") != null);+ // Fully completed checklist: accepted immediately.+ root.todos.items[0].status = "completed";+ try std.testing.expect((try completionGate(ar, &root)) == null);+ // Review turns never gate (isolated reviews share the root Agent).+ root.review_mode = true;+ root.todos.items[0].status = "pending";+ try std.testing.expect((try completionGate(ar, &root)) == null);+ try std.testing.expect(!goalActive(&root));+}++test "applyGoalSteering: injects once, suppresses repeats, consumes the one-shot note" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ var root: Agent = undefined;+ root.review_mode = false;+ root.goal = .{ .objective = "ship it", .epoch = 1 };+ root.goal_note_fp = 0;+ root.goal_note_age = 0;+ root.pending_goal_note = null;+ const m1 = try applyGoalSteering(ar, &root, "hi");+ try std.testing.expect(std.mem.indexOf(u8, m1, "[standing goal: ship it") != null);+ const m2 = try applyGoalSteering(ar, &root, "hi");+ try std.testing.expectEqualStrings("hi", m2); // verbatim repeat suppressed+ root.pending_goal_note = "[goal update: x]";+ const m3 = try applyGoalSteering(ar, &root, "hi");+ try std.testing.expect(std.mem.indexOf(u8, m3, "[goal update: x]") != null);+ try std.testing.expect(root.pending_goal_note == null); // one-shot consumed+ // Review turns are isolated: no steering, gate state untouched.+ root.review_mode = true;+ root.pending_goal_note = "[goal update: y]";+ try std.testing.expectEqualStrings("hi", try applyGoalSteering(ar, &root, "hi"));+ try std.testing.expect(root.pending_goal_note != null); // preserved for the next normal turn+}++test "supersededNote and clearedNote name the parked work" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ const s1 = try supersededNote(ar, "phase 3", 4);+ try std.testing.expect(std.mem.indexOf(u8, s1, "supersedes the previous goal \"phase 3\"") != null);+ try std.testing.expect(std.mem.indexOf(u8, s1, "4 unfinished") != null);+ const s2 = try supersededNote(ar, "phase 3", 0);+ try std.testing.expect(std.mem.indexOf(u8, s2, "parked") == null);+ const c = try clearedNote(ar, 2);+ try std.testing.expect(std.mem.indexOf(u8, c, "2 unfinished") != null);+}diff --git a/src/goal_todo.zig b/src/goal_todo.zigindex 89fd389b..fc16b577 100644--- a/src/goal_todo.zig+++ b/src/goal_todo.zig@@ -45,15 +45,16 @@ pub fn clearEpochForReplaceEx(todos: *std.ArrayList(TodoItem), epoch: u64, incomvar i: usize = 0;while (i < todos.items.len) {const t = todos.items[i];- const done = t.closed(); // completed or cancelled (#1545): omitted, it stays as history+ // Closed and dropped items are history, not plans (#1545, #318).+ const record = t.closed() or goal_state.isDropped(t.status);const mentioned = mentions(incoming, t.content);- const verify = !done and goal_verify_kind.isVerification(t.content);- if (t.epoch != epoch or (done and !mentioned) or (verify and !mentioned)) {+ const verify = !record and goal_verify_kind.isVerification(t.content);+ if (t.epoch != epoch or (record and !mentioned) or (verify and !mentioned)) {if (t.epoch == epoch and verify and !mentioned) out.kept_verify += 1;i += 1;continue;}- if (!done and !mentioned) out.dropped_open += 1;+ if (!record and !mentioned) out.dropped_open += 1;_ = todos.orderedRemove(i);}return out;@@ -496,3 +497,30 @@ test "retirement is epoch-scoped, root-only, and leaves no checklist to completeconst refusal = try goal_state.completionGate(ar, &root);try std.testing.expect(refusal != null and std.mem.indexOf(u8, refusal.?, "no checklist") != null);}++test "#1545 cancelled and dropped records coexist without reopening or adopting abandoned work" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const ar = arena_state.allocator();+ var root = todoRoot(ar);+ _ = try applyTodoWrite(&root, try todosArg(ar,+ \\{"todos":[{"content":"finished work","status":"completed"},+ \\ {"content":"cancelled work","status":"cancelled"},+ \\ {"content":"abandoned work","status":"dropped"}]}+ ));+ try std.testing.expectEqual(@as(usize, 0), goal_state.openCount(root.todos.items, 0));+ try std.testing.expect(goal_state.allDone(root.todos.items, 0));+ try std.testing.expect(!goal_state.allDone(root.todos.items[2..3], 0)); // dropped is not completion evidence+ const res = try applyTodoWrite(&root, try todosArg(ar,+ \\{"todos":[{"content":"next work","status":"pending"}]}+ ));+ try std.testing.expectEqual(@as(usize, 4), root.todos.items.len);+ try std.testing.expectEqual(@as(usize, 0), res.dropped_open);+ try std.testing.expect(std.mem.indexOf(u8, res.text, "[-] cancelled work") != null);+ try std.testing.expect(std.mem.indexOf(u8, res.text, "[-] abandoned work") != null);+ goal_state.adoptTodos(root.todos.items, 0, 1);+ for (root.todos.items[0..3]) |t| try std.testing.expectEqual(@as(u64, 0), t.epoch);+ try std.testing.expectEqual(@as(u64, 1), root.todos.items[3].epoch);+ try std.testing.expectEqual(@as(usize, 0), goal_state.parkedOpenCount(root.todos.items, 1));+ try std.testing.expectEqual(@as(usize, 1), goal_state.openCount(root.todos.items, 1));+}diff --git a/src/http.zig b/src/http.zigindex 61fdc418..5a403f82 100644--- a/src/http.zig+++ b/src/http.zig@@ -222,18 +222,6 @@ test "watchdogError (#134): Esc -> Interrupted; a deadline is the caller's errortry std.testing.expect(watchdogError(.deadline, error.StreamStalled) != error.Interrupted);}-/// Watchdog arm for postWatched: ticks every 200ms so a user Esc aborts a-/// stuck subagent request promptly; otherwise fires at the hard deadline.-fn postWatchdog(io: Io) WatchdogFired {- var waited: u64 = 0;- while (waited < post_deadline_ms) {- io.sleep(.fromMilliseconds(200), .awake) catch return .deadline; // canceled: loser arm, result discarded- waited += 200;- if (Agent.esc_cancel.load(.acquire)) return .esc;- }- return .deadline;-}-// A streaming response idle THIS LONG (not total response time — the watchdog// resets its clock on every line, so it measures the gap between tokens/// keep-alives/reasoning deltas) is a dead or hung connection. This is the@@ -366,7 +354,7 @@ pub fn postWatched(gpa: Allocator, io: Io, client: *std.http.Client, provider: Pvar sel: Io.Select(Done) = .init(io, &done_buf);sel.concurrent(.posted, postTask, .{ gpa, client, provider, body, conv_id }) catchreturn error.HungRequest; // #56 Fix-B: pool exhausted — don't fall back to a bare blocking post() that can hang a subagent turn with no Esc path; return retryable so request() retries + backs off- sel.concurrent(.watchdog, postWatchdog, .{io}) catch {+ sel.concurrent(.watchdog, deadlineStallTask, .{ io, false, stall.postBudgetMs(provider.url, post_deadline_ms) }) catch {const r = sel.await() catch |e| { // posted is the only armsel.cancelDiscard();return e;diff --git a/src/http_headers.zig b/src/http_headers.zigindex 3a4c0578..02083f69 100644--- a/src/http_headers.zig+++ b/src/http_headers.zig@@ -7,6 +7,7 @@ const root = @import("main.zig");const kimi_catalog = @import("kimi_catalog.zig");const cache_affinity = @import("cache_affinity.zig"); // ADR 0069: git-root / scratch seedconst client_tags = @import("client_tags.zig");+const anthropic_oauth = @import("anthropic_oauth_wire.zig");var session_id_buf: [36]u8 = undefined;var session_id_len: usize = 0;@@ -14,9 +15,7 @@ var session_id_len: usize = 0;/// with, and the critical section is a single one-time 36-byte format.var session_id_lock: std.atomic.Value(bool) = .init(false);-/// A UUIDv4 minted once per process. Persisted on the graff session record.-/// Codex's HTTP `session_id` is the prompt-cache key (openai/codex ModelClient-/// defaults `prompt_cache_key` to `session_id`), not this value.+/// Process UUIDv4 persisted with the session; distinct from prompt-cache routing.pub fn sessionId(io: Io) []const u8 {while (session_id_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint();defer session_id_lock.store(false, .release);@@ -178,6 +177,7 @@ pub fn restoreProjectRootId(id: []const u8) void {}pub fn userAgent(provider: Provider) std.http.Client.Request.Headers.Value {+ if (anthropic_oauth.active(provider)) return .omit; // explicit header also reaches HTTP/2if (std.mem.eql(u8, provider.id, "kimi")) {return .{ .override = root.kimi_user_agent };}@@ -191,10 +191,8 @@ pub fn wantsGrokConvId(provider_id: []const u8) bool {return std.mem.eql(u8, provider_id, "xai");}-/// ChatGPT sign-in Responses routes send `session_id` equal to the body's-/// `prompt_cache_key` — the endpoint keys prompt caching on it, and a body-/// key alone returns zero cached tokens. The API-key `openai` route is-/// untested and stays without it.+/// ChatGPT/Codex cache routing needs session_id equal to prompt_cache_key;+/// direct API-key OpenAI requests omit the backend-only header.pub fn wantsSessionIdHeader(provider_id: []const u8) bool {return std.mem.eql(u8, provider_id, "codex") or std.mem.eql(u8, provider_id, "chatgpt-new");}@@ -234,11 +232,13 @@ pub fn providerHeadersWithConv(io: Io, provider: Provider, bearer: []const u8, b// ADR 0219: the beta that goes with the body's thinking.block_binding,// on the Anthropic API and the models that check it (claude_wire).const claude = @import("claude_wire.zig");- if (claude.isClaudeApi(provider.id, provider.model) and claude.bindsThinking(provider.model)) {- buf[count] = .{ .name = "anthropic-beta", .value = claude.binding_beta };+ const beta: ?[]const u8 = if (anthropic_oauth.active(provider)) anthropic_oauth.beta(provider) else if (claude.isClaudeApi(provider.id, provider.model) and claude.bindsThinking(provider.model)) claude.binding_beta else null;+ if (beta) |value| {+ buf[count] = .{ .name = "anthropic-beta", .value = value };count += 1;}}+ if (anthropic_oauth.active(provider)) count += anthropic_oauth.identityHeaders(buf[count..]);if (std.mem.eql(u8, provider.id, "kimi")) {const identity = kimi_catalog.identityHeaders(buf[count..]);count += identity.len;diff --git a/src/http_stall.zig b/src/http_stall.zigindex 04a89f99..10b454ea 100644--- a/src/http_stall.zig+++ b/src/http_stall.zig@@ -97,6 +97,16 @@ pub const StreamPolicy = struct {return .{ .silence_ms = silence, .initial_ms = initial, .prose_base_ms = base_ms };}+ pub fn initForEndpoint(model: []const u8, url: []const u8, base_ms: u64, head_ms: u64, stream_override: bool, head_override: bool) StreamPolicy {+ var policy = init(model, base_ms, head_ms, stream_override, head_override);+ if (localEndpoint(url) and !stream_override and base_ms == 120_000) {+ policy.silence_ms = @max(policy.silence_ms, local_silence_ms);+ policy.prose_base_ms = local_silence_ms;+ if (!head_override and head_ms == 45_000) policy.initial_ms = policy.silence_ms;+ }+ return policy;+ }+pub fn interFrameBudgetMs(self: StreamPolicy, saw_protocol: bool, prose_open: bool, widen: u8) u64 {if (prose_open) return budgetMsWidened(self.prose_base_ms, true, widen);return if (saw_protocol) self.silence_ms else self.initial_ms;@@ -106,8 +116,35 @@ pub const StreamPolicy = struct {/// Compute once per stream, never mutate shared watchdog budgets. Inspect the/// explicit knobs as well as their configured values: an override equal to/// the shipped default is still an override. Validation matches settings.-pub fn streamPolicy(model: []const u8, base_ms: u64) StreamPolicy {- return StreamPolicy.init(model, base_ms, head_ceiling_ms, validSecondsEnv("GRAFF_STREAM_STALL_SECS"), validSecondsEnv("GRAFF_STREAM_HEAD_STALL_SECS"));+pub fn streamPolicy(model: []const u8, url: []const u8, base_ms: u64) StreamPolicy {+ return StreamPolicy.initForEndpoint(model, url, base_ms, head_ceiling_ms, validSecondsEnv("GRAFF_STREAM_STALL_SECS"), validSecondsEnv("GRAFF_STREAM_HEAD_STALL_SECS"));+}++const local_silence_ms: u64 = 10 * 60 * 1000;++/// Match the actual URL host, never a model name, path or hostname substring.+fn localEndpoint(url: []const u8) bool {+ const uri = std.Uri.parse(url) catch return false;+ if (!std.ascii.eqlIgnoreCase(uri.scheme, "http") and !std.ascii.eqlIgnoreCase(uri.scheme, "https")) return false;+ const host = switch (uri.host orelse return false) {+ .raw, .percent_encoded => |value| value,+ };+ return std.ascii.eqlIgnoreCase(host, "localhost") or std.mem.eql(u8, host, "127.0.0.1") or+ std.mem.eql(u8, host, "[::1]") or std.mem.eql(u8, host, "::1");+}++/// Local inference may buffer HTTP headers until prefill/generation completes.+/// Preserve shortened fixture deadlines and explicit streaming overrides.+pub fn headBudgetMs(url: []const u8, configured_ms: u64, policy: StreamPolicy) u64 {+ return if (localEndpoint(url) and configured_ms == 30_000) policy.initial_ms else configured_ms;+}++pub fn postBudgetMs(url: []const u8, configured_ms: u64) u64 {+ return localBudgetMs(url, configured_ms, 300_000, validSecondsEnv("GRAFF_POST_DEADLINE_SECS"));+}++fn localBudgetMs(url: []const u8, configured_ms: u64, default_ms: u64, explicit: bool) u64 {+ return if (!explicit and configured_ms == default_ms and localEndpoint(url)) @max(configured_ms, local_silence_ms) else configured_ms;}fn validSecondsEnv(name: [*:0]const u8) bool {@@ -138,6 +175,41 @@ pub fn expired(waited_ms: u64, base_ms: u64, tokens_flowing: bool) bool {return waited_ms >= budgetMs(base_ms, tokens_flowing);}+test "local inference deadlines follow loopback endpoints and preserve explicit budgets" {+ for ([_][]const u8{ "http://127.0.0.1:8080/v1/chat/completions", "http://localhost:1234/v1/chat/completions", "https://LOCALHOST/v1", "http://[::1]:8080/v1" }) |url| {+ const policy = StreamPolicy.initForEndpoint("custom-local-model", url, 120_000, 45_000, false, false);+ try std.testing.expectEqual(@as(u64, 600_000), policy.interFrameBudgetMs(false, false, 0));+ try std.testing.expectEqual(@as(u64, 600_000), policy.interFrameBudgetMs(true, false, 0));+ try std.testing.expectEqual(@as(u64, 150_000), policy.interFrameBudgetMs(true, true, 0));+ try std.testing.expectEqual(@as(u64, 600_000), headBudgetMs(url, 30_000, policy));+ try std.testing.expectEqual(@as(u64, 123), headBudgetMs(url, 123, policy));+ try std.testing.expectEqual(@as(u64, 600_000), localBudgetMs(url, 300_000, 300_000, false));+ try std.testing.expectEqual(@as(u64, 300_000), localBudgetMs(url, 300_000, 300_000, true));+ try std.testing.expectEqual(@as(u64, 100), localBudgetMs(url, 100, 300_000, false));+ const stream = StreamPolicy.initForEndpoint("custom", url, 120_000, 120_000, true, false);+ try std.testing.expectEqual(@as(u64, 120_000), stream.initial_ms);+ try std.testing.expectEqual(@as(u64, 30_000), stream.interFrameBudgetMs(true, true, 0));+ const head = StreamPolicy.initForEndpoint("custom", url, 120_000, 45_000, false, true);+ try std.testing.expectEqual(@as(u64, 45_000), headBudgetMs(url, 30_000, head));+ try std.testing.expectEqual(@as(u64, 600_000), head.silence_ms);+ }+}++test "local inference deadlines never extend remote hosts or leak between requests" {+ for ([_][]const u8{ "https://api.example.com/v1", "https://localhost.example.com/v1", "https://127.0.0.1.example.com/v1", "https://example.com/localhost", "https://[email protected]/v1", "not a url" }) |url| {+ const policy = StreamPolicy.initForEndpoint("mlx-localhost", url, 120_000, 45_000, false, false);+ try std.testing.expectEqual(@as(u64, 45_000), policy.initial_ms);+ try std.testing.expectEqual(@as(u64, 120_000), policy.silence_ms);+ try std.testing.expectEqual(@as(u64, 30_000), policy.interFrameBudgetMs(true, true, 0));+ try std.testing.expectEqual(@as(u64, 30_000), headBudgetMs(url, 30_000, policy));+ try std.testing.expectEqual(@as(u64, 300_000), localBudgetMs(url, 300_000, 300_000, false));+ }+ const local = StreamPolicy.initForEndpoint("custom", "http://localhost:8080/v1", 120_000, 45_000, false, false);+ const remote = StreamPolicy.init("custom", 120_000, 45_000, false, false);+ try std.testing.expectEqual(@as(u64, 600_000), local.silence_ms);+ try std.testing.expectEqual(@as(u64, 120_000), remote.silence_ms);+}+test "stream policy: Gemini-4 model ids allow buffered tool silence, not slow prose" {const models = [_][]const u8{"gemini-4", "gemini-4-argon", "gemini-4.0-pro", "google/gemini-4-argon",diff --git a/src/main.zig b/src/main.zigindex 0c157b13..3494cabe 100644--- a/src/main.zig+++ b/src/main.zig@@ -549,9 +549,7 @@ const http = @import("http.zig"); // HTTP transport and watchdogs.// Subprocess execution (runCapped, git-worktree mgmt, background bash-job pool) lives in jobs.zig; jobsReap is aliased back for main()'s cleanup defer.const jobs = @import("jobs.zig");const jobsReap = jobs.jobsReap;-// Tool execution (ToolCtx, /rewind snapshots, pre/post-tool hook dispatch, codedb-guard #626, metered-companion router) lives in tools.zig, imported-// as tools_mod since Agent.request/buildBody already have a `tools` param a bare import would shadow. Snapshots is a local alias (main() constructs-// one for /rewind). Subagent/workflow-task spawning (subagent.zig/workflow.zig) and the dispatcher (exec.zig) are imported only for their test{} blocks.+// Tool dispatch and snapshots live in tools.zig; spawning/workflow/exec imports below also expose their tests.const tools_mod = @import("tools.zig");const Snapshots = tools_mod.Snapshots;const subagent = @import("subagent.zig");@@ -563,7 +561,7 @@ test { // ── Unit tests (`zig build test`): pull in tests from imported modu_ = .{ @import("mcp_apps.zig"), @import("html_view.zig"), @import("usage_trace.zig"), @import("providers_confirmation_tests.zig"), @import("repl_model_confirmation_tests.zig"), @import("repl_model_pick_tests.zig"), @import("engine_sink_citation_tests.zig"), @import("cite_markup_stream_tests.zig"), @import("oneshot_citation_tests.zig"), @import("acp_permission.zig"), @import("acp_usage.zig") };_ = .{ @import("mcp_server_tests.zig"), @import("main_test.zig"), @import("artifact_claim_command.zig"), @import("cli_path_hint.zig"), @import("run_budget.zig") };_ = .{ @import("session_catalog.zig"), @import("session_prompt.zig"), @import("session_discovery.zig"), @import("session_workspaces.zig"), @import("commands_resume.zig"), @import("peer_live.zig"), @import("presence_tests.zig"), @import("mcp_rpc_tests.zig") };- _ = .{ @import("prompt_astra.zig"), @import("prompt_guidance.zig"), @import("oauth_chatgpt.zig"), @import("chatgpt_reauth.zig") }; // per-model guidance (Astra, GPT-5.6); ChatGPT plan sign-in (ADR 0253)+ _ = .{ @import("prompt_astra.zig"), @import("prompt_guidance.zig"), @import("oauth_chatgpt.zig"), @import("chatgpt_reauth.zig"), @import("oauth_anthropic.zig"), @import("anthropic_oauth_wire.zig"), @import("anthropic_login_tests.zig") };_ = .{ @import("agent_empty_completion.zig"), @import("agent_length_cutoff.zig"), @import("agent_model_loop.zig"), @import("publication_policy_tests.zig"), @import("jobs_completion_tests.zig"), @import("headless_result.zig"), @import("oneshot.zig"), @import("agent_length.zig"), @import("agent_length_tests.zig") };_ = .{ @import("provider_routing_tests.zig"), @import("codegraff_messages_tests.zig"), @import("tool_result_empty_tests.zig"), @import("history_wire.zig"), @import("path_lookup.zig"), @import("agents_prepare.zig"), @import("catalog_price.zig"), @import("readline_paste_number_tests.zig"), @import("test_hooks.zig"), @import("peer_wake_loop.zig") };_ = .{ @import("list_dir_nearmiss.zig"), @import("tool_preview_tests.zig"), @import("stream_reconnect.zig") };@@ -573,6 +571,8 @@ test { // ── Unit tests (`zig build test`): pull in tests from imported modu_ = @import("agent_responses.zig"); // #1019: type:error frame message extraction_ = @import("agent_server_compact.zig"); // server-side autocompact (codex Responses)_ = .{ @import("compact_status.zig"), @import("compact_mix.zig") }; // compact status + GRAFF_COMPACT_MIX mixture arm+ _ = .{ @import("agent_clef_compact.zig"), @import("agent_clef_compact_tests.zig"), @import("agent_clef_archive.zig") }; // gateway /v1/compact client (Clef pruning) + archive mode+ _ = @import("agent_compact_recover.zig"); // compaction last-resort recovery (split for the ceiling)_ = @import("agent_request_search_tests.zig");_ = @import("agent_ws_steer.zig");_ = @import("agent_ws_signal.zig");@@ -584,7 +584,7 @@ test { // ── Unit tests (`zig build test`): pull in tests from imported modu_ = @import("learn_delete.zig"); // #303: its tests were dead until listed here_ = .{ @import("additional_tests.zig"), @import("req_stats.zig"), @import("exact_reply.zig"), @import("rlm_order_tests.zig"), @import("async_tool_policy.zig"), @import("agent_async_tools.zig"), @import("request_usage_attempts.zig"), @import("learn_formal.zig") };_ = @import("goal_pacing_autonomous_test.zig");- _ = .{ @import("goal_state.zig"), @import("agent_compact_recover.zig"), @import("agent_clef_compact.zig"), @import("agent_clef_compact_tests.zig"), @import("agent_clef_archive.zig") }; // + compaction recovery split, Clef arm (ADR 0260/0261)+ _ = .{ @import("goal_state.zig"), @import("goal_state_tests.zig") };_ = @import("goal_persist_tests.zig");_ = @import("agent_render_cleanup.zig");_ = .{ @import("goal_flow.zig"), @import("shell_identity.zig") };@@ -596,5 +596,5 @@ test { // ── Unit tests (`zig build test`): pull in tests from imported modu_ = .{ @import("acp_agent_activity.zig"), @import("acp_subagent_live.zig"), @import("acp_line_writer.zig") };_ = .{ @import("acp_replay.zig"), @import("python_version.zig"), @import("jev_auto.zig"), @import("background_wait.zig"), @import("gateway_compact_tests.zig") };_ = @import("read_image.zig");- _ = .{ @import("mcp_names.zig"), @import("workspace_history.zig"), @import("file_worktree.zig"), @import("mcp_turn_context.zig"), @import("review_deadline.zig"), @import("issue_cmd.zig"), @import("shell_tool.zig"), @import("acp_ask.zig"), @import("side_steer.zig"), @import("task_workspace_tests.zig"), @import("workspace_prepare_tests.zig"), @import("worktree_reap.zig") };+ _ = .{ @import("mcp_interop_tests.zig"), @import("mcp_inbound.zig"), @import("mcp_names.zig"), @import("workspace_history.zig"), @import("file_worktree.zig"), @import("mcp_turn_context.zig"), @import("review_deadline.zig"), @import("issue_cmd.zig"), @import("shell_tool.zig"), @import("acp_ask.zig"), @import("side_steer.zig"), @import("task_workspace_tests.zig"), @import("workspace_prepare_tests.zig"), @import("worktree_reap.zig") };}diff --git a/src/mcp_call.zig b/src/mcp_call.zigindex 59831ab7..238a15af 100644--- a/src/mcp_call.zig+++ b/src/mcp_call.zig@@ -9,7 +9,7 @@ const mcp_protocol = @import("mcp_protocol.zig");const mcp_elicitation = @import("mcp_elicitation.zig");const initializeServer = mcp_rpc.initializeServer;const request = mcp_rpc.request;-const renderContent = @import("mcp_content.zig").renderContent;+const renderResult = @import("mcp_content.zig").renderResult;const Server = mcp_rpc.Server;/// One server/tool for MRTR retries (mcp_mrtr.resolve).@@ -21,7 +21,24 @@ const McpCall = struct {}};-pub const CallResult = struct { text: []u8, is_error: bool };+pub const CallResult = struct {+ text: []u8,+ is_error: bool,+ /// Borrowed slice of `text`, not a second allocation. RLM consumes this+ /// JSON value rather than parsing a server's human-readable summary.+ structured_text: ?[]const u8 = null,+ /// The delegated task's `codegraff/runTask` mirror, when the server sent+ /// one. Copied out of the response arena before it dies.+ run_task: ?@import("engine_events.zig").RunTaskMeta = null,++ pub fn takeHostText(self: CallResult, a: Allocator) ![]u8 {+ if (self.structured_text) |structured| {+ defer a.free(self.text);+ return try a.dupe(u8, structured);+ }+ return self.text;+ }+};/// A tool whose server connection closed or failed this session (#1523)./// Remembered so a later call or schema load names the closed service@@ -178,6 +195,7 @@ fn invoke(reg: *Registry, out_alloc: Allocator, qualified: []const u8, input: Vaif (!mcp_protocol.resultIsComplete(result))return .{ .text = try out_alloc.dupe(u8, "MCP server returned an unsupported resultType"), .is_error = true };const is_error = if (result.get("isError")) |v| (v == .bool and v.bool) else false;+ const run_task = @import("run_task_meta.zig").fromMcpResult(out_alloc, result);var ow: Io.Writer.Allocating = .init(out_alloc);errdefer ow.deinit();@@ -188,25 +206,16 @@ fn invoke(reg: *Registry, out_alloc: Allocator, qualified: []const u8, input: Vatry ow.writer.print("[MCP app]({s}) — saved interactive result; /mcp apps opens it in a browser.\n", .{p});} else try ow.writer.writeAll("[MCP app view unavailable; ordinary tool output follows.]\n");}- const prefix_len = ow.writer.buffered().len;- if (result.get("content")) |content| try renderContent(&ow.writer, content, .{+ const structured_range = try renderResult(&ow.writer, result, .{.arena = reg.arena(),.slot = ®.pending_image,.label = qualified,.supports_vision = reg.vision_capable,- });- // 2025-06-18+ structured tool output: if the server sent only- // structuredContent (no text blocks), surface it instead of "".- if (ow.writer.buffered().len == prefix_len) {- if (result.get("structuredContent")) |sc| {- var sw: std.json.Stringify = .{ .writer = &ow.writer };- try sw.write(sc);- }- }+ }, response_alloc);const text = try ow.toOwnedSlice();if (mcp_elicitation.looksUnavailable(text)) {out_alloc.free(text);return .{ .text = try out_alloc.dupe(u8, mcp_elicitation.fallback), .is_error = true };}- return .{ .text = text, .is_error = is_error };+ return .{ .text = text, .is_error = is_error, .run_task = run_task, .structured_text = if (structured_range) |range| text[range.start..range.end] else null };}diff --git a/src/mcp_content.zig b/src/mcp_content.zigindex caa3ec8d..93eb9aa0 100644--- a/src/mcp_content.zig+++ b/src/mcp_content.zig@@ -35,6 +35,28 @@ pub fn renderContent(w: *Io.Writer, content: Value, stage: Stage) !void {if (w.buffered().len > 0) try w.writeByte('\n');try w.writeAll(txt.string);};+ if (block.object.get("type")) |kind| if (kind == .string) {+ if (std.mem.eql(u8, kind.string, "resource_link")) {+ try separator(w);+ try w.print("[MCP resource link] {s}: {s}", .{ field(block.object, "name", "resource"), field(block.object, "uri", "URI unavailable") });+ if (block.object.get("description")) |desc| if (desc == .string) try w.print("\n{s}", .{desc.string});+ } else if (std.mem.eql(u8, kind.string, "resource")) {+ try separator(w);+ if (block.object.get("resource")) |resource| {+ if (resource == .object) {+ try w.print("[MCP resource] {s}", .{field(resource.object, "uri", "URI unavailable")});+ if (resource.object.get("text")) |text| {+ if (text == .string) try w.print("\n{s}", .{text.string});+ } else if (resource.object.get("blob") != null) {+ try w.print("\n[binary resource ({s}); payload not rendered]", .{field(resource.object, "mimeType", "unknown MIME type")});+ }+ } else try w.writeAll("[MCP resource unavailable: malformed resource]");+ } else try w.writeAll("[MCP resource unavailable: missing resource]");+ } else if (std.mem.eql(u8, kind.string, "audio")) {+ try separator(w);+ try w.print("[MCP audio ({s}); payload not rendered]", .{field(block.object, "mimeType", "unknown MIME type")});+ }+ };if (vision.mcpImageBlock(block)) |img| {const fate: vision.McpImageFate = if (!stage.supports_vision).no_vision@@ -45,3 +67,37 @@ pub fn renderContent(w: *Io.Writer, content: Value, stage: Stage) !void {}}}++fn separator(w: *Io.Writer) !void {+ if (w.buffered().len > 0) try w.writeByte('\n');+}++fn field(object: std.json.ObjectMap, key: []const u8, fallback: []const u8) []const u8 {+ const value = object.get(key) orelse return fallback;+ return if (value == .string) value.string else fallback;+}++/// Offsets remain valid after the allocating writer transfers its buffer.+pub const StructuredRange = struct { start: usize, end: usize };++/// Keep the human-readable result and its independent structured payload.+/// A plain JSON text mirror stays byte-identical; mixed text/images/resources+/// cannot suppress structuredContent. Scratch dies with the response arena.+pub fn renderResult(w: *Io.Writer, result: std.json.ObjectMap, stage: Stage, scratch: Allocator) !?StructuredRange {+ const start = w.buffered().len;+ if (result.get("content")) |content| try renderContent(w, content, stage);+ const structured = result.get("structuredContent") orelse return null;+ const json = try std.json.Stringify.valueAlloc(scratch, structured, .{});+ const body = std.mem.trim(u8, w.buffered()[start..], " \t\r\n");+ if (body.len > 0) {+ const mirror = std.json.parseFromSliceLeaky(Value, scratch, body, .{}) catch null;+ if (mirror) |value| {+ const canonical = try std.json.Stringify.valueAlloc(scratch, value, .{});+ if (std.mem.eql(u8, json, canonical)) return .{ .start = start, .end = w.buffered().len };+ }+ try w.writeAll("\n[structuredContent]\n");+ }+ const json_start = w.buffered().len;+ try w.writeAll(json);+ return .{ .start = json_start, .end = w.buffered().len };+}diff --git a/src/mcp_inbound.zig b/src/mcp_inbound.zignew file mode 100644index 00000000..5a7a17b0--- /dev/null+++ b/src/mcp_inbound.zig@@ -0,0 +1,121 @@+//! Request-scoped server-to-client MCP ping replies over stdio.++const std = @import("std");+const Io = std.Io;+const Allocator = std.mem.Allocator;+const Value = std.json.Value;+const mcp_stdio = @import("mcp_stdio.zig");++/// Answer only valid JSON-RPC ping requests; leave all other messages alone.+pub fn replyStdio(writer: *Io.Writer, allocator: Allocator, line: []const u8) !bool {+ const parsed = std.json.parseFromSlice(Value, allocator, line, .{ .allocate = .alloc_always }) catch |err| switch (err) {+ error.OutOfMemory => return err,+ else => return false,+ };+ defer parsed.deinit();+ const msg = parsed.value;+ if (msg != .object) return false;+ const version = msg.object.get("jsonrpc") orelse return false;+ if (version != .string or !std.mem.eql(u8, version.string, "2.0")) return false;+ const method = msg.object.get("method") orelse return false;+ if (method != .string or !std.mem.eql(u8, method.string, "ping")) return false;+ if (msg.object.contains("result") or msg.object.contains("error")) return false;+ const id = msg.object.get("id") orelse return false;+ if (id != .integer and id != .string) return false;+ if (msg.object.get("params")) |params| {+ if (params != .object and params != .array) return false;+ }++ var out: Io.Writer.Allocating = .init(allocator);+ defer out.deinit();+ try out.writer.writeAll("{\"jsonrpc\":\"2.0\",\"id\":");+ var json: std.json.Stringify = .{ .writer = &out.writer };+ try json.write(id);+ try out.writer.writeAll(",\"result\":{}}");+ try mcp_stdio.writeRequest(writer, out.writer.buffered());+ return true;+}++const testing = std.testing;++test "MCP inbound ping: numeric ids receive an empty result" {+ const cases = [_]struct { request: []const u8, response: []const u8 }{+ .{ .request = "{\"jsonrpc\":\"2.0\",\"id\":0,\"method\":\"ping\"}", .response = "{\"jsonrpc\":\"2.0\",\"id\":0,\"result\":{}}\n" },+ .{ .request = "{\"jsonrpc\":\"2.0\",\"id\":-17,\"method\":\"ping\",\"params\":{}}", .response = "{\"jsonrpc\":\"2.0\",\"id\":-17,\"result\":{}}\n" },+ .{ .request = "{\"jsonrpc\":\"2.0\",\"id\":9223372036854775807,\"method\":\"ping\"}", .response = "{\"jsonrpc\":\"2.0\",\"id\":9223372036854775807,\"result\":{}}\n" },+ };+ for (cases) |case| {+ var buf: [256]u8 = undefined;+ var writer: Io.Writer = .fixed(&buf);+ try testing.expect(try replyStdio(&writer, testing.allocator, case.request));+ try testing.expectEqualStrings(case.response, writer.buffered());+ }+}++test "MCP inbound ping: string ids are JSON escaped and preserved" {+ var buf: [256]u8 = undefined;+ var writer: Io.Writer = .fixed(&buf);+ try testing.expect(try replyStdio(&writer, testing.allocator,+ \\{"jsonrpc":"2.0","id":"ping\"\\\n\t雪","method":"ping"}+ ));+ const response = try std.json.parseFromSlice(Value, testing.allocator, writer.buffered(), .{});+ defer response.deinit();+ try testing.expectEqualStrings("ping\"\\\n\t雪", response.value.object.get("id").?.string);+ try testing.expectEqualStrings("2.0", response.value.object.get("jsonrpc").?.string);+ try testing.expectEqual(@as(usize, 0), response.value.object.get("result").?.object.count());+ try testing.expectEqual(@as(usize, 3), response.value.object.count());+ try testing.expectEqual(@as(u8, '\n'), writer.buffered()[writer.buffered().len - 1]);+}++test "MCP inbound ping: notifications and unrelated messages receive no reply" {+ const lines = [_][]const u8{+ \\{"jsonrpc":"2.0","method":"ping"}+ ,+ \\{"jsonrpc":"2.0","method":"notifications/progress","params":{"progress":1}}+ ,+ \\{"jsonrpc":"2.0","id":1,"method":"roots/list"}+ ,+ \\{"jsonrpc":"2.0","id":1,"result":{}}+ ,+ \\{"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"unknown"}}+ ,+ };+ var buf: [256]u8 = undefined;+ var writer: Io.Writer = .fixed(&buf);+ for (lines) |line| try testing.expect(!try replyStdio(&writer, testing.allocator, line));+ try testing.expectEqual(@as(usize, 0), writer.buffered().len);+}++test "MCP inbound ping: malformed requests receive no reply" {+ const lines = [_][]const u8{+ "", "not json", "{", "[]", "null",+ \\{"id":1,"method":"ping"}+ ,+ \\{"jsonrpc":"1.0","id":1,"method":"ping"}+ ,+ \\{"jsonrpc":2,"id":1,"method":"ping"}+ ,+ \\{"jsonrpc":"2.0","id":null,"method":"ping"}+ ,+ \\{"jsonrpc":"2.0","id":true,"method":"ping"}+ ,+ \\{"jsonrpc":"2.0","id":1.5,"method":"ping"}+ ,+ \\{"jsonrpc":"2.0","id":{},"method":"ping"}+ ,+ \\{"jsonrpc":"2.0","id":1,"method":7}+ ,+ \\{"jsonrpc":"2.0","id":1,"method":"ping","params":null}+ ,+ \\{"jsonrpc":"2.0","id":1,"method":"ping","params":3}+ ,+ \\{"jsonrpc":"2.0","id":1,"method":"ping","result":{}}+ ,+ \\{"jsonrpc":"2.0","id":1,"method":"ping","error":null}+ ,+ };+ var buf: [256]u8 = undefined;+ var writer: Io.Writer = .fixed(&buf);+ for (lines) |line| try testing.expect(!try replyStdio(&writer, testing.allocator, line));+ try testing.expectEqual(@as(usize, 0), writer.buffered().len);+}diff --git a/src/mcp_interop_tests.zig b/src/mcp_interop_tests.zignew file mode 100644index 00000000..b7b440ee--- /dev/null+++ b/src/mcp_interop_tests.zig@@ -0,0 +1,100 @@+//! Protocol result preservation shared by REPL, ACP and RLM host calls.+const std = @import("std");+const Io = std.Io;+const Value = std.json.Value;+const content = @import("mcp_content.zig");+const CallResult = @import("mcp_call.zig").CallResult;+const testing = std.testing;++fn render(json: []const u8) !CallResult {+ var scratch = std.heap.ArenaAllocator.init(testing.allocator);+ defer scratch.deinit();+ const a = scratch.allocator();+ const result = try std.json.parseFromSliceLeaky(Value, a, json, .{});+ var pending: ?@import("vision.zig").PendingImage = null;+ var out: Io.Writer.Allocating = .init(testing.allocator);+ errdefer out.deinit();+ const range = try content.renderResult(&out.writer, result.object, .{+ .arena = a,+ .slot = &pending,+ .label = "mcp__fixture__data",+ .supports_vision = false,+ }, a);+ const text = try out.toOwnedSlice();+ return .{ .text = text, .is_error = false, .structured_text = if (range) |r| text[r.start..r.end] else null };+}++test "MCP interoperability: human summaries do not suppress structured results or RLM binds" {+ const result = try render(+ \\{"content":[{"type":"text","text":"Done"}],"structuredContent":{"answer":42,"private_field":"kept"}}+ );+ try testing.expect(std.mem.indexOf(u8, result.text, "Done") != null);+ try testing.expect(std.mem.indexOf(u8, result.text, "\"answer\":42") != null);+ // The response arena has already died. RLM gets independent complete JSON,+ // not the summary, a slimmed object, or a slice dangling into that arena.+ const host = try result.takeHostText(testing.allocator);+ defer testing.allocator.free(host);+ const parsed = try std.json.parseFromSlice(Value, testing.allocator, host, .{});+ defer parsed.deinit();+ try testing.expectEqual(@as(i64, 42), parsed.value.object.get("answer").?.integer);+ try testing.expectEqualStrings("kept", parsed.value.object.get("private_field").?.string);+}++test "MCP interoperability: a serialized JSON text mirror is not duplicated" {+ const result = try render(+ \\{"content":[{"type":"text","text":"{ \"answer\": 42 }"}],"structuredContent":{"answer":42}}+ );+ defer testing.allocator.free(result.text);+ try testing.expectEqualStrings("{ \"answer\": 42 }", result.text);+ try testing.expectEqualStrings(result.text, result.structured_text.?);+}++test "MCP interoperability: structured-only results preserve every JSON value kind" {+ for ([_][]const u8{ "{\"answer\":42}", "[1,2,3]", "\"answer\"", "42", "true", "null" }) |json| {+ const source = try std.fmt.allocPrint(testing.allocator, "{{\"structuredContent\":{s}}}", .{json});+ defer testing.allocator.free(source);+ const result = try render(source);+ defer testing.allocator.free(result.text);+ try testing.expectEqualStrings(json, result.text);+ try testing.expectEqualStrings(json, result.structured_text.?);+ }+}++test "MCP interoperability: resource links and embedded text remain visible without fetching" {+ const result = try render(+ \\{"content":[{"type":"resource_link","uri":"fixture://report","name":"Report","description":"Full result"},{"type":"resource","resource":{"uri":"fixture://source","mimeType":"text/plain","text":"nested resource body"}}]}+ );+ defer testing.allocator.free(result.text);+ try testing.expect(std.mem.indexOf(u8, result.text, "Report: fixture://report") != null);+ try testing.expect(std.mem.indexOf(u8, result.text, "Full result") != null);+ try testing.expect(std.mem.indexOf(u8, result.text, "fixture://source") != null);+ try testing.expect(std.mem.indexOf(u8, result.text, "nested resource body") != null);+ try testing.expect(result.structured_text == null);+}++test "MCP interoperability: unsupported media and malformed resources are explicit" {+ const result = try render(+ \\{"content":[{"type":"resource","resource":{"uri":"fixture://binary","mimeType":"application/octet-stream","blob":"b3BhcXVl"}},{"type":"audio","mimeType":"audio/wav","data":"b3BhcXVl"},{"type":"resource"},{"type":"resource","resource":null}]}+ );+ defer testing.allocator.free(result.text);+ try testing.expect(std.mem.indexOf(u8, result.text, "binary resource (application/octet-stream); payload not rendered") != null);+ try testing.expect(std.mem.indexOf(u8, result.text, "MCP audio (audio/wav); payload not rendered") != null);+ try testing.expect(std.mem.indexOf(u8, result.text, "missing resource") != null);+ try testing.expect(std.mem.indexOf(u8, result.text, "malformed resource") != null);+ try testing.expect(std.mem.indexOf(u8, result.text, "b3BhcXVl") == null);+}++test "MCP interoperability: image placeholders cannot hide structured content and plain text stays unchanged" {+ const image = try render(+ \\{"content":[{"type":"image","mimeType":"image/png","data":"aGVsbG8="}],"structuredContent":{"image_count":1}}+ );+ defer testing.allocator.free(image.text);+ try testing.expect(std.mem.indexOf(u8, image.text, "image") != null);+ try testing.expectEqualStrings("{\"image_count\":1}", image.structured_text.?);+ const plain = try render(+ \\{"content":[{"type":"text","text":"original text"}]}+ );+ const host = try plain.takeHostText(testing.allocator);+ defer testing.allocator.free(host);+ try testing.expectEqualStrings("original text", host);+}diff --git a/src/mcp_rpc.zig b/src/mcp_rpc.zigindex 46151d36..6903af9c 100644--- a/src/mcp_rpc.zig+++ b/src/mcp_rpc.zig@@ -10,12 +10,12 @@ const Value = std.json.Value;const Allocator = std.mem.Allocator;const mcp_elicitation = @import("mcp_elicitation.zig");const mcp_http = @import("mcp_http.zig");+const mcp_inbound = @import("mcp_inbound.zig");const mcp_protocol = @import("mcp_protocol.zig");const mcp_stdio = @import("mcp_stdio.zig");const mcp_teardown = @import("mcp_teardown.zig");const mcp_notify = @import("mcp_notify.zig");const mcp_wait = @import("mcp_wait.zig");-const legacy_protocol = mcp_protocol.legacy_protocol;const modern_protocol = mcp_protocol.modern_protocol;@@ -318,12 +318,12 @@ pub fn finishInitialized(server: *Server) void {/// graff already spends on the same child's `initialize`. `GRAFF_MCP_PROBE_MS`/// moves it either way.pub var stdio_probe_timeout_ms: i64 = 5_000;-fn stdioProbeReadTask(server: *Server, response_alloc: Allocator, id: i64) anyerror!Value {const stdio = &server.transport.stdio;const r = &stdio.stdout_reader.interface;while (true) {const line = try mcp_stdio.takeLine(r);+ if (try mcp_inbound.replyStdio(&stdio.stdin_writer.interface, response_alloc, line)) continue;if (mcp_http.matchingResponse(response_alloc, line, id)) |parsed| return parsed;}}diff --git a/src/mcp_server.zig b/src/mcp_server.zigindex 6ac0fca7..f5a933f5 100644--- a/src/mcp_server.zig+++ b/src/mcp_server.zig@@ -9,10 +9,24 @@ const runner = @import("process_runner.zig");pub const Task = struct {prompt: []const u8,+ /// Caller-supplied background for the task, appended to the child's system+ /// prompt. Over `inline_context_max` it is summarized first (ADR 0253).+ context: ?[]const u8 = null,timeout_seconds: u64 = 120,max_model_calls: u64 = 8,};-pub const Result = struct { text: []const u8, isError: bool = false, timed_out: bool = false, cancelled: bool = false, output_truncated: bool = false };+pub const Result = struct {+ text: []const u8,+ isError: bool = false,+ timed_out: bool = false,+ cancelled: bool = false,+ output_truncated: bool = false,+ context_chars: u64 = 0,+ context_summarized: bool = false,+ context_used_chars: u64 = 0,+ /// Set only when the server summarized a handover before the task ran.+ preprocess: ?Preprocess = null,+};pub const Execute = *const fn (?*anyopaque, Allocator, Task) anyerror!Result;pub const Server = struct {@@ -85,24 +99,57 @@ pub const Server = struct {const name = string(params, "name") orelse return failure(out, id, -32602, "Tool name is required");if (!std.mem.eql(u8, name, "run_task")) return failure(out, id, -32602, "Unknown tool");const input = params.object.get("arguments") orelse Value.null;- const task = parseTask(input) catch return failure(out, id, -32602, "Expected prompt (1-32768 bytes), timeout_seconds (1-300), max_model_calls (1-32); no other arguments");+ const task = parseTask(input) catch return failure(out, id, -32602, "Expected prompt (1-32768 bytes), context (1-32768 bytes), timeout_seconds (1-300), max_model_calls (1-32); no other arguments");const result = self.execute(self.context, a, task) catch |err| Result{.text = try std.fmt.allocPrint(a, "Task could not run: {s}", .{@errorName(err)}),.isError = true,};- return answer.send(.{ .content = .{.{ .type = "text", .text = result.text }}, .isError = result.isError, .structuredContent = .{+ const data = .{.text = result.text,.status = if (result.timed_out) "timed_out" else if (result.cancelled) "cancelled" else if (result.isError) "failed" else "completed",.output_truncated = result.output_truncated,.timeout_seconds = task.timeout_seconds,.max_model_calls = task.max_model_calls,- } }, false);+ .context_chars = result.context_chars,+ .context_summarized = result.context_summarized,+ .context_used_chars = result.context_used_chars,+ .usage_reported = usage_reported,+ .usage_unavailable_reason = usage_unavailable_reason,+ // Preprocessing is reported separately from child execution: it is+ // a settled charge the engine ingested, counted once here and not+ // folded into the (unavailable) child total.+ .preprocess = blk: {+ const p = result.preprocess orelse Preprocess{};+ const reason = if (result.preprocess == null)+ "no summarization was needed"+ else if (p.usage_complete) "" else preprocess_unavailable;+ break :blk .{+ .cost_usd = p.cost_usd,+ .usage_complete = p.usage_complete,+ .input_tokens = p.input_tokens,+ .output_tokens = p.output_tokens,+ .unavailable_reason = reason,+ };+ },+ };+ // The same metadata in the namespaced slot, so a client that renders the+ // raw result never has to parse prose to learn what the child was given+ // (the `graff/…` / `codegraff/…` convention used elsewhere).+ return answer.send(.{ .content = .{.{ .type = "text", .text = result.text }}, .isError = result.isError, .structuredContent = data, ._meta = .{ .@"codegraff/runTask" = data } }, false);}};+/// What a caller may rely on about per-task cost, stated rather than omitted.+/// The task child runs in print mode, which exposes no settled gateway charge,+/// and a charge is never derived from a list rate (`pricing_settled.zig`).+/// Reporting `false` here is the honest contract: account-wide gateway usage+/// stays authoritative when no per-task settlement exists (ADR 0253).+const usage_reported = false;+const usage_unavailable_reason = "per-task usage and charge are not reported: the task child runs in print mode, which exposes no settled gateway charge, and a charge is never derived from list rates. Account-wide gateway usage remains authoritative.";+const capabilities = .{ .tools = struct {}{}, .resources = struct {}{} };const server_info = .{ .name = "codegraff", .title = "Codegraff", .version = @import("main.zig").harness_version };-const instructions = "Delegate a small, self-contained task with run_task. Tasks run serially in the server launch directory with fresh context. Include scope and acceptance criteria; inspect the result and changes before integrating.";+const instructions = "Delegate a small, self-contained task with run_task. Tasks run serially in the server launch directory with fresh context; hand background over in context, which is summarized when it is long. Include scope and acceptance criteria; inspect the result and changes before integrating.";/// One reply, in the requesting era's shape.const Answer = struct {@@ -125,13 +172,22 @@ fn string(value: Value, key: []const u8) ?[]const u8 {pub fn parseTask(input: Value) !Task {const prompt = string(input, "prompt") orelse return error.InvalidArguments;- if (std.mem.trim(u8, prompt, " \r\n\t").len == 0 or prompt.len > 32768 or std.mem.indexOfScalar(u8, prompt, 0) != null) return error.InvalidArguments;+ if (std.mem.trim(u8, prompt, " \r\n\t").len == 0 or prompt.len > context_max_bytes or std.mem.indexOfScalar(u8, prompt, 0) != null) return error.InvalidArguments;var task: Task = .{ .prompt = prompt };var it = input.object.iterator();while (it.next()) |entry| {const key = entry.key_ptr.*;if (std.mem.eql(u8, key, "prompt")) continue;const v = entry.value_ptr.*;+ // `context` is the one string argument: background the caller wants the+ // child to have. It cannot carry configuration — only text the child+ // reads (ADR 0253).+ if (std.mem.eql(u8, key, "context")) {+ if (v != .string) return error.InvalidArguments;+ if (std.mem.trim(u8, v.string, " \r\n\t").len == 0 or v.string.len > context_max_bytes or std.mem.indexOfScalar(u8, v.string, 0) != null) return error.InvalidArguments;+ task.context = v.string;+ continue;+ }if (v != .integer or v.integer < 1) return error.InvalidArguments;if (std.mem.eql(u8, key, "timeout_seconds") and v.integer <= 300) {task.timeout_seconds = @intCast(v.integer);@@ -175,7 +231,7 @@ pub fn serve(gpa: Allocator, in: *Io.Reader, out: *Io.Writer, server: *Server) !}}-const Context = struct {+pub const Context = struct {io: Io,exe: []const u8,env: *const std.process.Environ.Map,@@ -183,14 +239,177 @@ const Context = struct {yolo: bool = false,};+/// Caller-supplied background at or below this size rides the child's appended+/// system prompt verbatim; above it the server summarizes first, so a large+/// handover cannot crowd out the child's own task (ADR 0253).+const inline_context_max: usize = 8192;+const context_max_bytes: usize = 32768;+const summary_stdout_cap: usize = 16 * 1024;+const summarize_deadline_ms: u64 = 60_000;++const raw_context_lead = "\n\nBackground context supplied by the caller for this task:\n\n";+const summary_context_lead = "\n\nBackground context supplied by the caller for this task (summarized to fit):\n\n";+const summarize_instruction =+ \\Summarize the context below so another agent can use it as background for a+ \\task. Keep decisions, constraints, file paths, open questions, and anything+ \\needed to continue the work; drop pleasantries, repetition and formatting+ \\nnoise. Answer with the summary alone, and do not use tools.+ \\+ \\<context>+ \\+;+fn execute(raw: ?*anyopaque, a: Allocator, task: Task) !Result {const ctx: *Context = @ptrCast(@alignCast(raw.?));+ var appended: ?[]const u8 = null;+ var summarized = false;+ var used_chars: u64 = 0;+ var preprocess: ?Preprocess = null;+ if (task.context) |context| {+ if (context.len <= inline_context_max) {+ appended = try std.fmt.allocPrint(a, "{s}{s}", .{ raw_context_lead, context });+ used_chars = context.len;+ } else {+ const summary = summarizeContext(a, ctx, context) catch |err| return Result{+ .text = try std.fmt.allocPrint(a, "Task not run: context summarization failed ({s}). Retry with a shorter context, or omit context.", .{@errorName(err)}),+ .isError = true,+ .context_chars = context.len,+ };+ appended = try std.fmt.allocPrint(a, "{s}{s}", .{ summary_context_lead, summary.text });+ summarized = true;+ used_chars = summary.text.len;+ preprocess = summary.usage;+ }+ }+ const argv = try taskArgv(a, ctx, task, appended);+ const run_result = try runner.runCappedWithOptions(a, ctx.io, argv, 64 * 1024, 16 * 1024, task.timeout_seconds * 1000, .{ .environ_map = ctx.env, .kill_process_tree = true });+ var result = try formatResult(a, run_result);+ result.context_chars = if (task.context) |c| c.len else 0;+ result.context_summarized = summarized;+ result.context_used_chars = used_chars;+ result.preprocess = preprocess;+ return result;+}++/// Arguments for one bounded task child. `appended` is the context text to add+/// to the child's system prompt, or null when the caller supplied none.+/// Permissions and model still come from the server launch, never the call+/// (ADR 0129): `context` adds text, not configuration.+pub fn taskArgv(a: Allocator, ctx: *Context, task: Task, appended: ?[]const u8) ![]const []const u8 {var argv: std.ArrayList([]const u8) = .empty;try argv.appendSlice(a, &.{ ctx.exe, "--no-resume", if (ctx.yolo) "--yolo" else "--safe", "--max-model-calls", try std.fmt.allocPrint(a, "{d}", .{task.max_model_calls}), "--max-tool-calls", "64" });if (ctx.model) |model| try argv.appendSlice(a, &.{ "--model", model });+ if (appended) |text| try argv.appendSlice(a, &.{ "--append-system-prompt", text });try argv.appendSlice(a, &.{ "-p", task.prompt });- const run_result = try runner.runCappedWithOptions(a, ctx.io, argv.items, 64 * 1024, 16 * 1024, task.timeout_seconds * 1000, .{ .environ_map = ctx.env, .kill_process_tree = true });- return formatResult(a, run_result);+ return argv.items;+}++/// What the preprocessing step cost, as the summarizer child reported it.+/// `cost_usd` is null whenever no settled charge was available — an incomplete+/// or unpriced report is surfaced as unavailable, never as a list-rate guess.+pub const Preprocess = struct {+ cost_usd: ?f64 = null,+ usage_complete: bool = false,+ input_tokens: u64 = 0,+ output_tokens: u64 = 0,+};++const Summary = struct { text: []const u8, usage: Preprocess };++/// Re-exported for tests: the summarizer's shape without spawning anything.+pub const ParsedSummary = Summary;++const preprocess_unavailable = "the summarizer reported an incomplete charge (missing or unpriced usage); no settled amount is claimed and no list-rate estimate is substituted";++var g_summary_seq: std.atomic.Value(u64) = .init(0);++fn tempPath(a: Allocator, ctx: *Context) ![]const u8 {+ const dir = if (ctx.env.get("TMPDIR")) |t| if (t.len > 0) std.mem.trimEnd(u8, t, "/") else "/tmp" else "/tmp";+ const n = g_summary_seq.fetchAdd(1, .monotonic);+ const stamp: u64 = @intCast(Io.Timestamp.now(ctx.io, .awake).nanoseconds);+ return std.fmt.allocPrint(a, "{s}/graff-mcp-summary-{d}-{d}.json", .{ dir, stamp, n });+}++/// The `turn` event of a `--json` one-shot: its text is the answer, and its+/// usage fields are the settled charge the engine already ingested. Any other+/// line is ignored, and an incomplete report yields a null cost.+pub fn parseTurn(a: Allocator, stdout: []const u8) !Summary {+ var lines = std.mem.tokenizeScalar(u8, stdout, '\n');+ while (lines.next()) |line| {+ const ev = std.json.parseFromSliceLeaky(Value, a, line, .{}) catch continue;+ if (ev != .object) continue;+ const typ = ev.object.get("type") orelse continue;+ if (typ != .string or !std.mem.eql(u8, typ.string, "turn")) continue;+ const raw = ev.object.get("text") orelse continue;+ if (raw != .string) continue;+ const text = std.mem.trim(u8, raw.string, " \r\n\t");+ if (text.len == 0) return error.ContextSummarizationFailed;+ const num = struct {+ fn get(o: std.json.ObjectMap, k: []const u8) u64 {+ const v = o.get(k) orelse return 0;+ return switch (v) {+ .integer => |i| if (i >= 0) @intCast(i) else 0,+ .float => |f| if (f >= 0) @intFromFloat(f) else 0,+ else => 0,+ };+ }+ };+ const complete = if (ev.object.get("usage_complete")) |v| (v == .bool and v.bool) else false;+ const settled = complete and num.get(ev.object, "unpriced_calls") == 0 and num.get(ev.object, "missing_usage_calls") == 0;+ const cost: ?f64 = if (settled) switch (ev.object.get("cost_usd") orelse .null) {+ .float => |f| f,+ .integer => |i| @floatFromInt(i),+ else => null,+ } else null;+ return .{ .text = text, .usage = .{+ .cost_usd = cost,+ .usage_complete = settled and cost != null,+ .input_tokens = num.get(ev.object, "input_tokens"),+ .output_tokens = num.get(ev.object, "output_tokens"),+ } };+ }+ return error.ContextSummarizationFailed;+}++/// Shrink an over-long handover into readable text before it reaches the child.+///+/// A fresh one-shot can only consume text. The gateway's `/responses/compact`+/// endpoint returns opaque server state that is meaningful only by continuing+/// the conversation it compacted, so it cannot seed a child that starts with+/// `--no-resume` (ADR 0002, ADR 0253); the readable summarizer is the arm that+/// fits. The summarizer runs `--safe` with no tool budget — summarizing is not+/// a reason to touch the workspace — and under the caller's own credentials,+/// so its spend lands on the same bill rather than a new one.+fn summarizeContext(a: Allocator, ctx: *Context, context: []const u8) !Summary {+ const brief = try std.fmt.allocPrint(a, "{s}{s}\n</context>\n", .{ summarize_instruction, context });+ // `--json`, not `-p`: print mode reports no settled charge, and the engine+ // already ingests an exact one on this path. Its single request line+ // arrives on a FILE-backed stdin, so nothing has to feed a pipe+ // concurrently and a large handover cannot deadlock the parent.+ var request: Io.Writer.Allocating = .init(a);+ var js: std.json.Stringify = .{ .writer = &request.writer };+ try js.write(.{ .type = "user", .text = brief });+ try request.writer.writeByte('\n');++ const path = try tempPath(a, ctx);+ defer Io.Dir.deleteFileAbsolute(ctx.io, path) catch {};+ {+ const out = Io.Dir.createFileAbsolute(ctx.io, path, .{ .truncate = true }) catch return error.ContextSummarizationFailed;+ defer out.close(ctx.io);+ var wbuf: [4096]u8 = undefined;+ var fw = out.writer(ctx.io, &wbuf);+ fw.interface.writeAll(request.written()) catch return error.ContextSummarizationFailed;+ fw.interface.flush() catch return error.ContextSummarizationFailed;+ }+ const stdin_file = Io.Dir.openFileAbsolute(ctx.io, path, .{ .mode = .read_only }) catch return error.ContextSummarizationFailed;+ defer stdin_file.close(ctx.io);++ var argv: std.ArrayList([]const u8) = .empty;+ try argv.appendSlice(a, &.{ ctx.exe, "--no-resume", "--safe", "--json", "--max-model-calls", "1", "--max-tool-calls", "0" });+ if (ctx.model) |model| try argv.appendSlice(a, &.{ "--model", model });+ const summary_run = try runner.runCappedWithOptions(a, ctx.io, argv.items, summary_stdout_cap, 8 * 1024, summarize_deadline_ms, .{ .environ_map = ctx.env, .kill_process_tree = true, .stdin_file = stdin_file });+ if (!runner.ranOk(summary_run) or summary_run.timed_out or summary_run.cancelled) return error.ContextSummarizationFailed;+ return try parseTurn(a, summary_run.stdout);}pub fn formatResult(a: Allocator, result: runner.CappedRun) !Result {@@ -246,5 +465,5 @@ pub fn run(io: Io, gpa: Allocator, parent: *const std.process.Environ.Map, args:}const tool_catalog =- \\{"tools":[{"name":"run_task","description":"Run a small, self-contained task with Codegraff in the server launch directory. Fresh context per call; include relevant context, scope and acceptance criteria. May edit files and execute commands when server permissions allow. Returns the answer or an explicit failure with partial output. Calls run serially; no continuation or cancellation handle.","inputSchema":{"type":"object","properties":{"prompt":{"type":"string","minLength":1,"maxLength":32768},"timeout_seconds":{"type":"integer","minimum":1,"maximum":300,"default":120},"max_model_calls":{"type":"integer","minimum":1,"maximum":32,"default":8}},"required":["prompt"],"additionalProperties":false},"annotations":{"readOnlyHint":false,"destructiveHint":true,"idempotentHint":false,"openWorldHint":true}}]}+ \\{"tools":[{"name":"run_task","description":"Run a small, self-contained task with Codegraff in the server launch directory. Fresh context per call; pass background the child needs in context (summarized when long) and include scope and acceptance criteria. May edit files and execute commands when server permissions allow. Returns the answer or an explicit failure with partial output. Calls run serially; no continuation or cancellation handle.","inputSchema":{"type":"object","properties":{"prompt":{"type":"string","minLength":1,"maxLength":32768},"context":{"type":"string","minLength":1,"maxLength":32768,"description":"Background for the task: relevant history, decisions, file paths or constraints the child cannot re-derive. Appended to the child's system prompt; over 8192 bytes it is summarized first, and the result reports whether that happened."},"timeout_seconds":{"type":"integer","minimum":1,"maximum":300,"default":120},"max_model_calls":{"type":"integer","minimum":1,"maximum":32,"default":8}},"required":["prompt"],"additionalProperties":false},"annotations":{"readOnlyHint":false,"destructiveHint":true,"idempotentHint":false,"openWorldHint":true}}]};diff --git a/src/mcp_server_app.zig b/src/mcp_server_app.zigindex 0997525a..a6956a6e 100644--- a/src/mcp_server_app.zig+++ b/src/mcp_server_app.zig@@ -29,7 +29,7 @@ pub fn catalog(a: std.mem.Allocator, source: []const u8, apps: bool) !Value {try tool.object.put(a, "_meta", meta);}const schema = try std.json.parseFromSliceLeaky(Value, a,- \\{"type":"object","properties":{"text":{"type":"string"},"status":{"type":"string","enum":["completed","failed","timed_out","cancelled"]},"output_truncated":{"type":"boolean"},"timeout_seconds":{"type":"integer"},"max_model_calls":{"type":"integer"}},"required":["text","status","output_truncated","timeout_seconds","max_model_calls"],"additionalProperties":false}+ \\{"type":"object","properties":{"text":{"type":"string"},"status":{"type":"string","enum":["completed","failed","timed_out","cancelled"]},"output_truncated":{"type":"boolean"},"timeout_seconds":{"type":"integer"},"max_model_calls":{"type":"integer"},"context_chars":{"type":"integer"},"context_summarized":{"type":"boolean"},"context_used_chars":{"type":"integer"},"usage_reported":{"type":"boolean","description":"False: this result carries no per-task usage or settled charge for CHILD EXECUTION. Preprocessing is reported separately in `preprocess`."},"usage_unavailable_reason":{"type":"string","description":"Why per-task child usage is unavailable, and what is authoritative instead. Present whenever usage_reported is false."},"preprocess":{"type":"object","description":"What preparing the context cost, when it had to be summarized. Distinct from child execution; a settled charge counted once, never a list-rate estimate.","properties":{"cost_usd":{"type":["number","null"],"description":"Settled charge for summarization, or null when none was available."},"usage_complete":{"type":"boolean"},"input_tokens":{"type":"integer"},"output_tokens":{"type":"integer"},"unavailable_reason":{"type":"string","description":"Empty when a settled charge is present; otherwise why it is not."}},"required":["cost_usd","usage_complete","input_tokens","output_tokens","unavailable_reason"],"additionalProperties":false}},"required":["text","status","output_truncated","timeout_seconds","max_model_calls","context_chars","context_summarized","context_used_chars","usage_reported","usage_unavailable_reason","preprocess"],"additionalProperties":false}, .{});try tool.object.put(a, "outputSchema", schema);return value;diff --git a/src/mcp_server_tests.zig b/src/mcp_server_tests.zigindex 67efe954..7ba9aff7 100644--- a/src/mcp_server_tests.zig+++ b/src/mcp_server_tests.zig@@ -72,6 +72,113 @@ test "MCP argument validation bounds work and rejects configuration injection" {try std.testing.expectEqual(@as(u64, 32), task.max_model_calls);}+test "MCP context is bounded text, never configuration" {+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ const task = try mcp.parseTask(try std.json.parseFromSliceLeaky(Value, a, "{\"prompt\":\"x\",\"context\":\"prior decisions\"}", .{}));+ try std.testing.expectEqualStrings("prior decisions", task.context.?);+ // Absent context stays absent: the flag is only added when there is text.+ try std.testing.expect((try mcp.parseTask(try std.json.parseFromSliceLeaky(Value, a, "{\"prompt\":\"x\"}", .{}))).context == null);+ const filler = try a.alloc(u8, 32769);+ @memset(filler, 'a');+ const over = try std.fmt.allocPrint(a, "{{\"prompt\":\"x\",\"context\":\"{s}\"}}", .{filler});+ for ([_][]const u8{+ "{\"prompt\":\"x\",\"context\":\"\"}",+ "{\"prompt\":\"x\",\"context\":\" \"}",+ "{\"prompt\":\"x\",\"context\":5}",+ "{\"prompt\":\"x\",\"context\":\"a\\u0000b\"}",+ over,+ }) |input| {+ const value = try std.json.parseFromSliceLeaky(Value, a, input, .{});+ try std.testing.expectError(error.InvalidArguments, mcp.parseTask(value));+ }+}++test "MCP task argv appends context as text and keeps server-owned policy" {+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ var ctx: mcp.Context = .{ .io = std.testing.io, .exe = "/fake/graff", .env = undefined };+ const task: mcp.Task = .{ .prompt = "do the thing", .context = "prior decisions", .max_model_calls = 3 };+ const argv = try mcp.taskArgv(a, &ctx, task, "\n\nBackground context:\n\nprior decisions");+ const joined = try std.mem.join(a, "\u{1}", argv);+ try std.testing.expect(std.mem.indexOf(u8, joined, "--append-system-prompt\u{1}\n\nBackground context:\n\nprior decisions") != null);+ try std.testing.expect(std.mem.indexOf(u8, joined, "--safe") != null);+ try std.testing.expect(std.mem.indexOf(u8, joined, "--yolo") == null);+ // The task prompt is still the last argument, and context never becomes a flag.+ try std.testing.expect(std.mem.endsWith(u8, joined, "-p\u{1}do the thing"));+ // No context means no flag at all.+ const bare = try mcp.taskArgv(a, &ctx, .{ .prompt = "x" }, null);+ try std.testing.expect(std.mem.indexOf(u8, try std.mem.join(a, "\u{1}", bare), "--append-system-prompt") == null);+}++test "MCP reports the context it carried" {+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ var server: mcp.Server = .{ .execute = fake_context, .initialized = true, .ready = true };+ var out: std.Io.Writer.Allocating = .init(a);+ try server.handle(a, &out.writer, "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\",\"params\":{\"name\":\"run_task\",\"arguments\":{\"prompt\":\"x\",\"context\":\"hi\"}}}");+ const response = try std.json.parseFromSliceLeaky(Value, a, out.written(), .{});+ const result = response.object.get("result").?;+ const data = result.object.get("structuredContent").?;+ try std.testing.expectEqual(@as(i64, 40), data.object.get("context_chars").?.integer);+ try std.testing.expect(data.object.get("context_summarized").?.bool);+ try std.testing.expectEqual(@as(i64, 7), data.object.get("context_used_chars").?.integer);+ // Unavailable usage is stated, not omitted: a client must be able to tell+ // "no charge reported" apart from "the field was dropped".+ try std.testing.expect(!data.object.get("usage_reported").?.bool);+ try std.testing.expect(data.object.get("usage_unavailable_reason").?.string.len > 0);+ // The same metadata rides the namespaced slot for raw-result readers.+ const meta = result.object.get("_meta").?.object.get("codegraff/runTask").?;+ try std.testing.expectEqual(@as(i64, 40), meta.object.get("context_chars").?.integer);+ try std.testing.expectEqual(@as(i64, 7), meta.object.get("context_used_chars").?.integer);+ try std.testing.expect(!meta.object.get("usage_reported").?.bool);+ try std.testing.expectEqualStrings(data.object.get("usage_unavailable_reason").?.string, meta.object.get("usage_unavailable_reason").?.string);+}++fn fake_context(_: ?*anyopaque, a: std.mem.Allocator, task: mcp.Task) !mcp.Result {+ _ = a;+ return .{ .text = task.prompt, .context_chars = 40, .context_summarized = true, .context_used_chars = 7 };+}++// The summarizer's settled-charge parsing, without spawning a child.+// A charge is reported only when the engine said the usage was complete and+// nothing was unpriced or missing; otherwise the cost is null, never guessed.+test "summarizer usage: a settled charge is read, an incomplete one is not invented" {+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();++ const good = try mcp.parseTurn(a, "{\"type\":\"text\",\"text\":\"noise\"}\n" +++ "{\"type\":\"turn\",\"text\":\" the summary \",\"cost_usd\":0.0012,\"usage_complete\":true," +++ "\"input_tokens\":1200,\"output_tokens\":30,\"unpriced_calls\":0,\"missing_usage_calls\":0}\n");+ try std.testing.expectEqualStrings("the summary", good.text);+ try std.testing.expect(good.usage.usage_complete);+ try std.testing.expectEqual(@as(f64, 0.0012), good.usage.cost_usd.?);+ try std.testing.expectEqual(@as(u64, 1200), good.usage.input_tokens);+ try std.testing.expectEqual(@as(u64, 30), good.usage.output_tokens);++ // Every way the report can be incomplete: no number is claimed.+ for ([_][]const u8{+ "{\"type\":\"turn\",\"text\":\"s\",\"cost_usd\":0.5,\"usage_complete\":false}",+ "{\"type\":\"turn\",\"text\":\"s\",\"cost_usd\":0.5,\"usage_complete\":true,\"unpriced_calls\":1}",+ "{\"type\":\"turn\",\"text\":\"s\",\"cost_usd\":0.5,\"usage_complete\":true,\"missing_usage_calls\":2}",+ "{\"type\":\"turn\",\"text\":\"s\",\"usage_complete\":true}",+ }) |line| {+ const m = try mcp.parseTurn(a, line);+ try std.testing.expect(m.usage.cost_usd == null);+ try std.testing.expect(!m.usage.usage_complete);+ }++ // No turn event at all, and an empty answer, are failures — not an empty+ // context handed to a child.+ try std.testing.expectError(error.ContextSummarizationFailed, mcp.parseTurn(a, "{\"type\":\"text\",\"text\":\"x\"}\n"));+ try std.testing.expectError(error.ContextSummarizationFailed, mcp.parseTurn(a, "{\"type\":\"turn\",\"text\":\" \"}\n"));+ try std.testing.expectError(error.ContextSummarizationFailed, mcp.parseTurn(a, "not json at all\n"));+}+test "MCP execution errors are tool errors and server remains usable" {var arena = std.heap.ArenaAllocator.init(std.testing.allocator);defer arena.deinit();diff --git a/src/mcp_wait.zig b/src/mcp_wait.zigindex 72e94bcc..25a9a44e 100644--- a/src/mcp_wait.zig+++ b/src/mcp_wait.zig@@ -14,6 +14,7 @@ const Value = std.json.Value;const Allocator = std.mem.Allocator;const mcp_elicitation = @import("mcp_elicitation.zig");const mcp_http = @import("mcp_http.zig");+const mcp_inbound = @import("mcp_inbound.zig");const mcp_notify = @import("mcp_notify.zig");const mcp_stdio = @import("mcp_stdio.zig");@@ -48,6 +49,7 @@ fn readReply(s: Stdio, io: Io, a: Allocator, id: i64) !Value {while (true) {const line = try mcp_stdio.takeLine(s.reader);if (try mcp_elicitation.replyStdio(s.writer, a, line, s.elicit_source)) continue;+ if (try mcp_inbound.replyStdio(s.writer, a, line)) continue;if (mcp_http.matchingResponse(a, line, id)) |parsed| return parsed;_ = mcp_notify.observe(s.sink, io, line);}@@ -116,6 +118,39 @@ test "awaitStdio: notifications are observed and the matching reply returns" {try testing.expect(sink.tools_stale.load(.acquire));}+test "MCP inbound ping: wait reader answers interleaved pings without losing progress or tool result" {+ const Progress = struct {+ var observed = false;+ fn record(_: Io, server: []const u8, text: []const u8) void {+ observed = std.mem.eql(u8, server, "demo") and std.mem.eql(u8, text, "1/2 working");+ }+ };+ Progress.observed = false;+ const saved = mcp_notify.on_progress;+ defer mcp_notify.on_progress = saved;+ mcp_notify.on_progress = Progress.record;+ var arena = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena.deinit();+ var in: Io.Reader = .fixed(+ \\{"jsonrpc":"2.0","id":3,"method":"ping"}+ \\{"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":3,"progress":1,"total":2,"message":"working"}}+ \\{"jsonrpc":"2.0","id":"server-ping","method":"ping","params":{}}+ \\{"jsonrpc":"2.0","id":3,"result":{"content":[{"type":"text","text":"done"}]}}+ \\+ );+ var out_buf: [512]u8 = undefined;+ var out: Io.Writer = .fixed(&out_buf);+ var sink: mcp_notify.Sink = .{ .server = "demo" };+ const got = try readReply(.{ .writer = &out, .reader = &in, .sink = &sink, .elicit_source = "" }, testing.io, arena.allocator(), 3);+ try testing.expect(Progress.observed);+ try testing.expectEqualStrings("done", got.object.get("result").?.object.get("content").?.array.items[0].object.get("text").?.string);+ try testing.expectEqualStrings(+ "{\"jsonrpc\":\"2.0\",\"id\":3,\"result\":{}}\n" +++ "{\"jsonrpc\":\"2.0\",\"id\":\"server-ping\",\"result\":{}}\n",+ out.buffered(),+ );+}+test "awaitStdio: a cancel already requested sends notifications/cancelled" {const saved = cancel_requested;defer cancel_requested = saved;diff --git a/src/oauth.zig b/src/oauth.zigindex 885b570c..0270de84 100644--- a/src/oauth.zig+++ b/src/oauth.zig@@ -1,11 +1,5 @@-//! Login / credential flows for the three OAuth-style providers — Codex-//! (ChatGPT PKCE), Kimi (device-code), and Codegraff (device-code) — plus the-//! on-disk credential loaders each writes/reads. Split out of main.zig (#123).-//!-//! Every entry point takes (io, gpa, arena, home) and reaches no Agent/global-//! state, so the coupling back to main is tiny: ansi for the palette, util for-//! the JSON getters, and a few shared consts (unixMs, the-//! codegraff gateway base) back-imported from main.+//! OAuth login and credential loaders shared by the engine and its clients.+//! Native subscription credentials and Console SDK profiles refresh separately.const std = @import("std");const builtin = @import("builtin");@@ -341,17 +335,25 @@ pub fn loadKimiOAuth(io: Io, gpa: Allocator, arena: Allocator, home: []const u8,/// `account` is this session's ChatGPT account id (empty when unknown or not/// codex) — see loadCodexOAuth's expect_account.pub fn refreshOAuthKey(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, provider_id: []const u8, force: bool, stale: ?[]const u8, account: []const u8) ?helpers.FreshKey {+ return refreshOAuthKeyForSource(io, gpa, arena, home, provider_id, force, stale, account, .login);+}++/// Refresh only the selected credential class; never cross Console/native stores.+pub fn refreshOAuthKeyForSource(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, provider_id: []const u8, force: bool, stale: ?[]const u8, account: []const u8, source: @import("provider.zig").Keys.CredentialSource) ?helpers.FreshKey {+ if (!source.canRefresh()) return null;const is_kimi = std.mem.eql(u8, provider_id, "kimi");const is_xai = std.mem.eql(u8, provider_id, "xai");const is_codex = std.mem.eql(u8, provider_id, "codex");const is_chatgpt = std.mem.eql(u8, provider_id, "chatgpt-new");- if (std.mem.eql(u8, provider_id, "anthropic")) return .{ .key = @import("anthropic_profile.zig").fresh(io, gpa, arena, force) orelse return null };- if (!is_kimi and !is_xai and !is_codex and !is_chatgpt) return null;+ const is_anthropic = std.mem.eql(u8, provider_id, "anthropic");+ if (!is_kimi and !is_xai and !is_codex and !is_chatgpt and !is_anthropic) return null;oauth_refresh_mutex.lockUncancelable(io);defer oauth_refresh_mutex.unlock(io);+ if (is_anthropic and source == .profile) return .{ .key = @import("anthropic_profile.zig").fresh(io, gpa, arena, force) orelse return null };// The ChatGPT plan's new sign-in keeps one record per machine (ADR 0221),// so the login vault never syncs it.if (is_chatgpt) return .{ .key = @import("oauth_chatgpt.zig").loadChatgptOAuth(io, gpa, arena, home, force, stale) orelse return null };+ if (is_anthropic) return .{ .key = @import("oauth_anthropic.zig").loadAnthropicOAuth(io, gpa, arena, home, force, stale) orelse return null };// Vault sync (harness ADR 0005): adopt a newer login, wait while another// device refreshes, or refresh under the lease and publish the result.const live = @import("vault_live.zig").before(io, gpa, arena, home, provider_id, force, oauth_refresh_margin_s);diff --git a/src/oauth_anthropic.zig b/src/oauth_anthropic.zignew file mode 100644index 00000000..3c1ea8e6--- /dev/null+++ b/src/oauth_anthropic.zig@@ -0,0 +1,337 @@+//! Native Anthropic browser PKCE. Only Graff's own .anthropic credential+//! record is read; Claude CLI and Pi files are never consulted.+const std = @import("std");+const Io = std.Io;+const Allocator = std.mem.Allocator;+const helpers = @import("oauth_helpers.zig");+const store = @import("credential_store.zig");+const util = @import("util.zig");+const callback = @import("oauth_callback.zig");++pub const client_id = "9d1c250a-e61b-44d9-88ed-5944d1962f5e";+pub const authorize_endpoint = "https://claude.ai/oauth/authorize";+pub const token_endpoint = "https://platform.claude.com/v1/oauth/token";+pub const scopes = "org:create_api_key user:profile user:inference user:sessions:claude_code user:mcp_servers user:file_upload";+pub const provider_dir = ".anthropic";+const refresh_margin_s = 300;+var refresh_mutex: Io.Mutex = .init;++pub const Record = struct { access: []const u8, refresh: []const u8, expires_at: i64 };+pub const Callback = struct { code: []const u8 = "", state: []const u8 = "", err: []const u8 = "" };++pub fn recordPath(arena: Allocator, home: []const u8) []const u8 {+ return store.oauthPath(arena, home, provider_dir);+}++pub fn readRecord(io: Io, arena: Allocator, home: []const u8) ?Record {+ const data = Io.Dir.cwd().readFileAlloc(io, recordPath(arena, home), arena, .limited(64 * 1024)) catch return null;+ const v = std.json.parseFromSliceLeaky(std.json.Value, arena, data, .{ .allocate = .alloc_always }) catch return null;+ if (v != .object) return null;+ const access = util.strFieldObj(v.object, "access_token") orelse return null;+ if (access.len == 0) return null;+ return .{+ .access = access,+ .refresh = util.strFieldObj(v.object, "refresh_token") orelse "",+ .expires_at = util.intFieldObj(v.object, "expires_at", 0),+ };+}++fn encode(arena: Allocator, s: []const u8) ![]const u8 {+ var out: std.ArrayList(u8) = .empty;+ const hex = "0123456789ABCDEF";+ for (s) |c| {+ if (std.ascii.isAlphanumeric(c) or c == '-' or c == '.' or c == '_' or c == '~')+ try out.append(arena, c)+ else+ try out.appendSlice(arena, &.{ '%', hex[c >> 4], hex[c & 15] });+ }+ return out.items;+}++fn decode(arena: Allocator, s: []const u8) ![]const u8 {+ var out: std.ArrayList(u8) = .empty;+ var i: usize = 0;+ while (i < s.len) : (i += 1) {+ if (s[i] == '%') {+ if (i + 2 >= s.len) return error.BadCallback;+ try out.append(arena, try std.fmt.parseInt(u8, s[i + 1 .. i + 3], 16));+ i += 2;+ } else try out.append(arena, if (s[i] == '+') ' ' else s[i]);+ }+ return out.items;+}++pub fn authorizeUrl(arena: Allocator, redirect: []const u8, verifier: []const u8) ![]const u8 {+ var digest: [32]u8 = undefined;+ std.crypto.hash.sha2.Sha256.hash(verifier, &digest, .{});+ return std.fmt.allocPrint(arena, "{s}?code=true&response_type=code&client_id={s}&redirect_uri={s}&scope={s}&code_challenge_method=S256&code_challenge={s}&state={s}", .{+ authorize_endpoint, client_id, try encode(arena, redirect), try encode(arena, scopes),+ helpers.b64url(arena, &digest), try encode(arena, verifier),+ });+}++pub fn parseCallback(arena: Allocator, line: []const u8) !?Callback {+ var parts = std.mem.tokenizeScalar(u8, line, ' ');+ if (!std.mem.eql(u8, parts.next() orelse return null, "GET")) return null;+ const target = parts.next() orelse return null;+ const q = std.mem.indexOfScalar(u8, target, '?') orelse target.len;+ if (!std.mem.eql(u8, target[0..q], "/callback")) return null;+ var result: Callback = .{};+ var seen: u8 = 0;+ var pairs = std.mem.splitScalar(u8, if (q < target.len) target[q + 1 ..] else "", '&');+ while (pairs.next()) |pair| {+ const eq = std.mem.indexOfScalar(u8, pair, '=') orelse continue;+ const key = pair[0..eq];+ const bit: u8 = if (std.mem.eql(u8, key, "code")) 1 else if (std.mem.eql(u8, key, "state")) 2 else if (std.mem.eql(u8, key, "error")) 4 else continue;+ if (seen & bit != 0) return error.BadCallback;+ seen |= bit;+ const value = try decode(arena, pair[eq + 1 ..]);+ switch (bit) {+ 1 => result.code = value,+ 2 => result.state = value,+ 4 => result.err = value,+ else => unreachable,+ }+ }+ return result;+}++pub const CallbackPolicy = enum { stale, finish };++/// A wrong-state tab must not end a newer sign-in. Matching-state denial+/// belongs to this attempt and is terminal, just like a successful code.+pub fn callbackPolicy(cb: Callback, verifier: []const u8) CallbackPolicy {+ return if (verifier.len > 0 and std.mem.eql(u8, cb.state, verifier)) .finish else .stale;+}++pub fn validateCallback(cb: Callback, verifier: []const u8) !void {+ if (callbackPolicy(cb, verifier) == .stale) return error.OAuthStateMismatch;+ if (std.mem.eql(u8, cb.err, "access_denied")) return error.OAuthDenied;+ if (cb.err.len > 0) return error.OAuthRejected;+ if (cb.code.len == 0) return error.MissingOAuthCode;+}++fn json(arena: Allocator, value: anytype) ![]const u8 {+ var aw: Io.Writer.Allocating = .init(arena);+ var s: std.json.Stringify = .{ .writer = &aw.writer };+ try s.write(value);+ return aw.writer.buffered();+}++fn post(io: Io, gpa: Allocator, arena: Allocator, endpoint: []const u8, body: []const u8, previous_refresh: []const u8) !Record {+ return postWithDeadline(io, gpa, arena, endpoint, body, previous_refresh, .fromSeconds(20));+}++fn delay(io: Io, duration: Io.Duration) void {+ io.sleep(duration, .awake) catch {};+}++fn fetchToken(client: *std.http.Client, endpoint: []const u8, body: []const u8, writer: *Io.Writer) !std.http.Status {+ // Use the request API: fetch's bodyErr().? panics on a cancelled body read.+ var req = try client.request(.POST, try std.Uri.parse(endpoint), .{+ .redirect_behavior = .unhandled,+ .headers = .{ .content_type = .{ .override = "application/json" } },+ });+ defer req.deinit();+ defer if (req.connection) |conn| {+ conn.closing = true;+ };+ req.transfer_encoding = .{ .content_length = body.len };+ var bw = try req.sendBodyUnflushed(&.{});+ try bw.writer.writeAll(body);+ try bw.end();+ try req.connection.?.flush();+ var response = try req.receiveHead(&.{});+ // Neither redirects nor provider error bodies cross this boundary.+ if (response.head.status != .ok) return response.head.status;+ const gpa = client.allocator;+ const dbuf: []u8 = switch (response.head.content_encoding) {+ .identity => &.{},+ .zstd => try gpa.alloc(u8, std.compress.zstd.default_window_len),+ .deflate, .gzip => try gpa.alloc(u8, std.compress.flate.max_window_len),+ .compress => return error.UnsupportedCompressionMethod,+ };+ defer if (dbuf.len != 0) gpa.free(dbuf);+ var transfer: [64]u8 = undefined;+ var decompress: std.http.Decompress = undefined;+ const reader = response.readerDecompressing(&transfer, &decompress, dbuf);+ _ = reader.streamRemaining(writer) catch |err| {+ if (err == error.WriteFailed) return error.ResponseTooLarge;+ return err;+ };+ return response.head.status;+}++/// Request-local deadline seam. Both workers are cancelled and joined before+/// their client/buffers are released, including on stalled headers or bodies.+/// Production code exchange and refresh both use the same 20-second bound.+pub fn postWithDeadline(io: Io, gpa: Allocator, arena: Allocator, endpoint: []const u8, body: []const u8, previous_refresh: []const u8, deadline: Io.Duration) !Record {+ var client: std.http.Client = .{ .allocator = gpa, .io = io };+ defer client.deinit();+ const buf = try arena.alloc(u8, 64 * 1024);+ var writer: Io.Writer = .fixed(buf);+ const Done = union(enum) { fetched: anyerror!std.http.Status, timeout: void };+ var done: [2]Done = undefined;+ var select: Io.Select(Done) = .init(io, &done);+ select.concurrent(.timeout, delay, .{ io, deadline }) catch return error.DeadlineUnavailable;+ select.concurrent(.fetched, fetchToken, .{ &client, endpoint, body, &writer }) catch {+ select.cancelDiscard();+ return error.TransportUnavailable;+ };+ const first = select.await() catch |err| {+ select.cancelDiscard();+ return err;+ };+ select.cancelDiscard();+ const status = switch (first) {+ .fetched => |result| try result,+ .timeout => return error.DeadlineExceeded,+ };+ if (status != .ok) return error.OAuthTokenRejected;+ const v = try std.json.parseFromSliceLeaky(std.json.Value, arena, writer.buffered(), .{ .allocate = .alloc_always });+ if (v != .object or v.object.contains("error")) return error.BadOAuthResponse;+ const access = util.strFieldObj(v.object, "access_token") orelse return error.BadOAuthResponse;+ const refresh = util.strFieldObj(v.object, "refresh_token") orelse previous_refresh;+ const duration = util.intFieldObj(v.object, "expires_in", 0);+ if (access.len == 0 or refresh.len == 0 or duration <= 0) return error.BadOAuthResponse;+ const expires = std.math.add(i64, @divTrunc(util.unixMs(io), 1000), duration) catch return error.BadOAuthResponse;+ return .{ .access = access, .refresh = refresh, .expires_at = expires };+}++/// Endpoint seam for real loopback tests; production always supplies HTTPS.+pub fn exchangeCodeAt(io: Io, gpa: Allocator, arena: Allocator, endpoint: []const u8, redirect: []const u8, verifier: []const u8, cb: Callback) !Record {+ try validateCallback(cb, verifier);+ return post(io, gpa, arena, endpoint, try json(arena, .{+ .grant_type = "authorization_code",+ .code = cb.code,+ .state = cb.state,+ .client_id = client_id,+ .redirect_uri = redirect,+ .code_verifier = verifier,+ }), "");+}++fn save(io: Io, arena: Allocator, home: []const u8, r: Record) !void {+ try store.writeOAuth(io, arena, home, provider_dir, r.access, r.refresh, r.expires_at);+}++/// Preserve the shared warning channel: never print a token response.+pub fn persistRefreshed(io: Io, arena: Allocator, home: []const u8, r: Record) void {+ save(io, arena, home, r) catch save(io, arena, home, r) catch |err| {+ helpers.persist_error = @errorName(err);+ };+}++fn valid(r: Record, now: i64) ?[]const u8 {+ return if (r.expires_at > now) r.access else null;+}++fn lockRecord(io: Io, arena: Allocator, home: []const u8) !Io.File {+ const path = try std.fmt.allocPrint(arena, "{s}.lock", .{recordPath(arena, home)});+ return Io.Dir.cwd().createFile(io, path, .{ .truncate = false, .lock = .exclusive, .permissions = store.private_file });+}++/// Serializes within the process AND across Graff processes. Re-read only+/// after acquiring the file lock so a rotated refresh token is never replayed.+pub fn loadAt(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, force: bool, stale: ?[]const u8, endpoint: []const u8) ?[]const u8 {+ refresh_mutex.lockUncancelable(io);+ defer refresh_mutex.unlock(io);+ const before = readRecord(io, arena, home) orelse return null;+ const now = @divTrunc(util.unixMs(io), 1000);+ const due = before.expires_at <= now + refresh_margin_s;+ if (helpers.supersededToken(force, before.access, stale) and !due) return valid(before, now);+ if (!force and !due) return valid(before, now);+ const lock = lockRecord(io, arena, home) catch return valid(before, now);+ defer lock.close(io);+ const current = readRecord(io, arena, home) orelse return null;+ const now_locked = @divTrunc(util.unixMs(io), 1000);+ if ((!std.mem.eql(u8, current.access, before.access) or !std.mem.eql(u8, current.refresh, before.refresh)) and current.expires_at > now_locked)+ return current.access;+ if (helpers.supersededToken(force, current.access, stale) and current.expires_at > now_locked) return current.access;+ if (current.refresh.len == 0) return valid(current, now_locked);+ const body = json(arena, .{ .grant_type = "refresh_token", .refresh_token = current.refresh, .client_id = client_id }) catch return valid(current, now_locked);+ const next = post(io, gpa, arena, endpoint, body, current.refresh) catch return valid(current, @divTrunc(util.unixMs(io), 1000));+ persistRefreshed(io, arena, home, next);+ return valid(next, @divTrunc(util.unixMs(io), 1000));+}++pub fn loadAnthropicOAuth(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, force: bool, stale: ?[]const u8) ?[]const u8 {+ return loadAt(io, gpa, arena, home, force, stale, token_endpoint);+}++/// A direct bind, NEVER oauth_callback.listen (which cancels a port holder).+/// Failing rather than changing the registered redirect keeps it consistent.+pub fn listen(io: Io, port: u16) !Io.net.Server {+ const address: Io.net.IpAddress = .{ .ip4 = .loopback(port) };+ return address.listen(io, .{});+}++fn saveLogin(io: Io, arena: Allocator, home: []const u8, r: Record) !void {+ refresh_mutex.lockUncancelable(io);+ defer refresh_mutex.unlock(io);+ // No secret is written until writeOAuth applies the private directory modes.+ try Io.Dir.cwd().createDirPath(io, std.fs.path.dirname(recordPath(arena, home)) orelse return error.BadPathName);+ const lock = try lockRecord(io, arena, home);+ defer lock.close(io);+ try save(io, arena, home, r);+}++pub fn login(io: Io, gpa: Allocator, arena: Allocator, home: []const u8, refresh_only: bool) !void {+ var obuf: [2048]u8 = undefined;+ var ow = Io.File.stdout().writer(io, &obuf);+ const out = &ow.interface;+ if (refresh_only) {+ const before = readRecord(io, arena, home) orelse return error.OAuthRefreshFailed;+ const token = loadAnthropicOAuth(io, gpa, arena, home, true, before.access) orelse return error.OAuthRefreshFailed;+ const after = readRecord(io, arena, home) orelse return error.OAuthRefreshFailed;+ if (std.mem.eql(u8, token, before.access) and std.mem.eql(u8, after.refresh, before.refresh) and after.expires_at <= before.expires_at)+ return error.OAuthRefreshFailed;+ try out.writeAll("Anthropic sign-in refreshed.\n");+ return out.flush();+ }+ var server = try listen(io, 53692);+ defer server.deinit(io);+ const redirect = "http://localhost:53692/callback";+ var random: [32]u8 = undefined;+ io.random(&random);+ const verifier = helpers.b64url(arena, &random);+ if (verifier.len == 0) return error.OutOfMemory;+ const url = try authorizeUrl(arena, redirect, verifier);+ try out.print("\nSign in with Anthropic (your browser should open it):\n\n{s}\n\nWaiting for sign-in…\n", .{url});+ try out.flush();+ helpers.openBrowser(io, url);+ while (true) {+ const stream = try server.accept(io);+ defer stream.close(io);+ var rbuf: [16 * 1024]u8 = undefined;+ var reader = Io.net.Stream.Reader.init(stream, io, &rbuf);+ const line = (reader.interface.takeDelimiter('\n') catch null) orelse continue;+ const cb = parseCallback(arena, line) catch {+ callback.answer(io, stream, "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\nConnection: close\r\n\r\n");+ continue;+ } orelse {+ callback.answer(io, stream, callback.not_found);+ continue;+ };+ if (callbackPolicy(cb, verifier) == .stale) {+ callback.answer(io, stream, "HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\nThis callback belongs to an earlier sign-in. Finish the newest sign-in.\n");+ continue;+ }+ const record = exchangeCodeAt(io, gpa, arena, token_endpoint, redirect, verifier, cb) catch |err| {+ callback.answer(io, stream, "HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\nSign-in failed or was denied. Start sign-in again.\n");+ return err;+ };+ saveLogin(io, arena, home, record) catch |err| {+ helpers.persist_error = @errorName(err);+ callback.answer(io, stream, "HTTP/1.1 500 Internal Server Error\r\nConnection: close\r\n\r\nSign-in could not be saved.\n");+ return err;+ };+ callback.answer(io, stream, "HTTP/1.1 200 OK\r\nContent-Type: text/plain; charset=utf-8\r\nConnection: close\r\n\r\nSigned in to Anthropic. You may close this tab.\n");+ try out.writeAll("Signed in to Anthropic.\n");+ return out.flush();+ }+}++test {+ _ = @import("oauth_anthropic_tests.zig");+}diff --git a/src/oauth_anthropic_tests.zig b/src/oauth_anthropic_tests.zignew file mode 100644index 00000000..331170f4--- /dev/null+++ b/src/oauth_anthropic_tests.zig@@ -0,0 +1,334 @@+//! Offline protocol and real HTTP loopback tests for native Anthropic OAuth.+const std = @import("std");+const builtin = @import("builtin");+const Io = std.Io;+const auth = @import("oauth_anthropic.zig");+const store = @import("credential_store.zig");+const helpers = @import("oauth_helpers.zig");+const util = @import("util.zig");++const Mock = struct {+ server: Io.net.Server,+ response: []const u8 = "{\"access_token\":\"new-access\",\"refresh_token\":\"rotated-refresh\",\"expires_in\":3600}",+ status: []const u8 = "200 OK",+ body: [8192]u8 = undefined,+ len: usize = 0,+ content_type: bool = false,+ failed: bool = false,+ block_path: ?[]const u8 = null,++ fn run(self: *Mock, io: Io) void {+ self.serve(io) catch {+ self.failed = true;+ };+ }++ fn serve(self: *Mock, io: Io) !void {+ const stream = try self.server.accept(io);+ defer stream.close(io);+ var buf: [16 * 1024]u8 = undefined;+ var reader = Io.net.Stream.Reader.init(stream, io, &buf);+ const request = (try reader.interface.takeDelimiter('\n')) orelse return error.BadRequest;+ if (!std.mem.startsWith(u8, request, "POST /token HTTP/1.1")) return error.BadRequest;+ var length: usize = 0;+ while (true) {+ const raw = (try reader.interface.takeDelimiter('\n')) orelse return error.BadRequest;+ const line = std.mem.trimEnd(u8, raw, "\r");+ if (line.len == 0) break;+ if (std.ascii.startsWithIgnoreCase(line, "content-length:"))+ length = try std.fmt.parseInt(usize, std.mem.trim(u8, line[15..], " \t"), 10);+ if (std.ascii.startsWithIgnoreCase(line, "content-type:"))+ self.content_type = std.ascii.eqlIgnoreCase(std.mem.trim(u8, line[13..], " \t"), "application/json");+ }+ if (length > self.body.len) return error.TooLarge;+ try reader.interface.readSliceAll(self.body[0..length]);+ self.len = length;+ if (self.block_path) |path| {+ try Io.Dir.cwd().deleteFile(io, path);+ try Io.Dir.cwd().createDir(io, path, store.private_dir);+ }+ var wbuf: [4096]u8 = undefined;+ var writer = Io.net.Stream.Writer.init(stream, io, &wbuf);+ try writer.interface.print("HTTP/1.1 {s}\r\nContent-Type: application/json\r\nContent-Length: {d}\r\nConnection: close\r\n\r\n{s}", .{ self.status, self.response.len, self.response });+ try writer.interface.flush();+ }++ fn endpoint(self: *const Mock, a: std.mem.Allocator) ![]const u8 {+ return std.fmt.allocPrint(a, "http://127.0.0.1:{d}/token", .{self.server.socket.address.getPort()});+ }++ fn object(self: *const Mock, a: std.mem.Allocator) !std.json.ObjectMap {+ return (try std.json.parseFromSliceLeaky(std.json.Value, a, self.body[0..self.len], .{ .allocate = .alloc_always })).object;+ }+};++fn homePath(tmp: *std.testing.TmpDir, a: std.mem.Allocator) ![]const u8 {+ var buf: [std.fs.max_path_bytes]u8 = undefined;+ return a.dupe(u8, buf[0..try tmp.dir.realPath(std.testing.io, &buf)]);+}++fn field(o: std.json.ObjectMap, key: []const u8) []const u8 {+ return util.strFieldObj(o, key) orelse "";+}++test "Anthropic OAuth: public client, scopes and PKCE match browser protocol" {+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ const verifier = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk";+ const url = try auth.authorizeUrl(a, "http://localhost:53692/callback", verifier);+ for ([_][]const u8{+ "https://claude.ai/oauth/authorize?code=true&response_type=code&",+ "client_id=9d1c250a-e61b-44d9-88ed-5944d1962f5e&",+ "redirect_uri=http%3A%2F%2Flocalhost%3A53692%2Fcallback&",+ "scope=org%3Acreate_api_key%20user%3Aprofile%20user%3Ainference%20user%3Asessions%3Aclaude_code%20user%3Amcp_servers%20user%3Afile_upload&",+ "code_challenge_method=S256&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&",+ "state=" ++ verifier,+ }) |part| try std.testing.expect(std.mem.indexOf(u8, url, part) != null);+ var id: [36]u8 = undefined;+ try std.base64.standard.Decoder.decode(&id, "OWQxYzI1MGEtZTYxYi00NGQ5LTg4ZWQtNTk0NGQxOTYyZjVl");+ try std.testing.expectEqualStrings(auth.client_id, &id);+}++test "Anthropic OAuth: callback denial, state mismatch, decoding and duplicate rejection" {+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ const cb = (try auth.parseCallback(a, "GET /callback?code=a%2Bb%22&state=s-1 HTTP/1.1\r")).?;+ try std.testing.expectEqualStrings("a+b\"", cb.code);+ try auth.validateCallback(cb, "s-1");+ try std.testing.expectError(error.OAuthStateMismatch, auth.validateCallback(cb, "wrong"));+ const denied = (try auth.parseCallback(a, "GET /callback?error=access_denied&state=s-1 HTTP/1.1")).?;+ try std.testing.expectError(error.OAuthDenied, auth.validateCallback(denied, "s-1"));+ try std.testing.expectError(error.OAuthStateMismatch, auth.validateCallback(denied, "other"));+ try std.testing.expectError(error.MissingOAuthCode, auth.validateCallback(.{ .state = "s" }, "s"));+ try std.testing.expectError(error.BadCallback, auth.parseCallback(a, "GET /callback?state=one&state=two HTTP/1.1"));+ try std.testing.expectError(error.BadCallback, auth.parseCallback(a, "GET /callback?code=% HTTP/1.1"));+ try std.testing.expect(try auth.parseCallback(a, "GET /favicon.ico HTTP/1.1") == null);+ try std.testing.expect(try auth.parseCallback(a, "GET /callbackx?code=x HTTP/1.1") == null);+}++test "Anthropic OAuth: real loopback authorization code exchange sends JSON and expiry seconds" {+ if (builtin.os.tag == .windows) return error.SkipZigTest;+ const io = std.testing.io;+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ var mock: Mock = .{ .server = try auth.listen(io, 0) };+ defer mock.server.deinit(io);+ var group: Io.Group = .init;+ defer group.cancel(io);+ try group.concurrent(io, Mock.run, .{ &mock, io });+ const now = @divTrunc(util.unixMs(io), 1000);+ const record = try auth.exchangeCodeAt(io, std.testing.allocator, a, try mock.endpoint(a), "http://localhost:53692/callback", "verifier", .{ .code = "code\"value", .state = "verifier" });+ try group.await(io);+ try std.testing.expect(!mock.failed);+ try std.testing.expect(mock.content_type);+ const o = try mock.object(a);+ try std.testing.expectEqualStrings("authorization_code", field(o, "grant_type"));+ try std.testing.expectEqualStrings("code\"value", field(o, "code"));+ try std.testing.expectEqualStrings("verifier", field(o, "state"));+ try std.testing.expectEqualStrings("verifier", field(o, "code_verifier"));+ try std.testing.expectEqualStrings(auth.client_id, field(o, "client_id"));+ try std.testing.expectEqualStrings("http://localhost:53692/callback", field(o, "redirect_uri"));+ try std.testing.expectEqualStrings("new-access", record.access);+ try std.testing.expect(record.expires_at >= now + 3600 and record.expires_at < now + 3610);+}++test "Anthropic OAuth: early refresh persists rotation atomically and stale 401 adopts without HTTP" {+ if (builtin.os.tag == .windows) return error.SkipZigTest;+ const io = std.testing.io;+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const home = try homePath(&tmp, a);+ const now = @divTrunc(util.unixMs(io), 1000);+ try store.writeOAuth(io, a, home, auth.provider_dir, "old", "refresh\"old", now + 299);+ const original = try Io.Dir.cwd().openFile(io, auth.recordPath(a, home), .{});+ defer original.close(io);+ var mock: Mock = .{ .server = try auth.listen(io, 0) };+ defer mock.server.deinit(io);+ var group: Io.Group = .init;+ defer group.cancel(io);+ try group.concurrent(io, Mock.run, .{ &mock, io });+ const result = auth.loadAt(io, std.testing.allocator, a, home, false, null, try mock.endpoint(a));+ try std.testing.expectEqualStrings("new-access", result.?);+ try group.await(io);+ try std.testing.expect(!mock.failed);+ try std.testing.expect(mock.content_type);+ const body = try mock.object(a);+ try std.testing.expectEqualStrings("refresh_token", field(body, "grant_type"));+ try std.testing.expectEqualStrings("refresh\"old", field(body, "refresh_token"));+ try std.testing.expectEqualStrings(auth.client_id, field(body, "client_id"));+ const saved = auth.readRecord(io, a, home).?;+ try std.testing.expectEqualStrings("rotated-refresh", saved.refresh);+ try std.testing.expect(saved.expires_at > now + 3590);+ var rbuf: [1024]u8 = undefined;+ var reader = original.reader(io, &rbuf);+ const old_bytes = try reader.interface.allocRemaining(a, .limited(4096));+ try std.testing.expect(std.mem.indexOf(u8, old_bytes, "\"access_token\":\"old\"") != null);+ const stat = try Io.Dir.cwd().statFile(io, auth.recordPath(a, home), .{});+ try std.testing.expectEqual(@as(std.posix.mode_t, 0o600), stat.permissions.toMode() & 0o777);+ // An unreachable seam proves these paths never mint another credential.+ try std.testing.expectEqualStrings("new-access", auth.loadAt(io, std.testing.allocator, a, home, true, "old", "http://127.0.0.1:1/token").?);+ try std.testing.expectEqualStrings("new-access", auth.loadAt(io, std.testing.allocator, a, home, false, null, "http://127.0.0.1:1/token").?);+}++test "Anthropic OAuth: failed refresh fails closed after expiry but preserves unexpired access" {+ if (builtin.os.tag == .windows) return error.SkipZigTest;+ const io = std.testing.io;+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const home = try homePath(&tmp, a);+ for ([_]i64{ -1, 120 }) |remaining| {+ const now = @divTrunc(util.unixMs(io), 1000);+ try store.writeOAuth(io, a, home, auth.provider_dir, "old", "refresh", now + remaining);+ var mock: Mock = .{ .server = try auth.listen(io, 0), .status = "400 Bad Request", .response = "{\"error\":\"invalid_grant\"}" };+ defer mock.server.deinit(io);+ var group: Io.Group = .init;+ defer group.cancel(io);+ try group.concurrent(io, Mock.run, .{ &mock, io });+ const result = auth.loadAt(io, std.testing.allocator, a, home, true, "old", try mock.endpoint(a));+ try group.await(io);+ try std.testing.expect(!mock.failed);+ if (remaining < 0) try std.testing.expect(result == null) else try std.testing.expectEqualStrings("old", result.?);+ try std.testing.expectEqualStrings("refresh", auth.readRecord(io, a, home).?.refresh);+ }+ try store.writeOAuth(io, a, home, auth.provider_dir, "expired", "", 1);+ try std.testing.expect(auth.loadAt(io, std.testing.allocator, a, home, true, "different", "http://127.0.0.1:1/token") == null);+}++test "Anthropic OAuth: persistence failure reports only a safe error name" {+ const io = std.testing.io;+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ try tmp.dir.writeFile(io, .{ .sub_path = "blocked", .data = "file" });+ const home = try std.fmt.allocPrint(a, "{s}/blocked", .{try homePath(&tmp, a)});+ helpers.persist_error = null;+ auth.persistRefreshed(io, a, home, .{ .access = "private-access", .refresh = "private-refresh", .expires_at = 123 });+ const message = helpers.takePersistError() orelse return error.MissingPersistenceWarning;+ try std.testing.expect(std.mem.indexOf(u8, message, "private-") == null);+ try std.testing.expect(helpers.takePersistError() == null);+}++test "Anthropic OAuth: refresh HTTP success surfaces a failed credential replacement" {+ if (builtin.os.tag == .windows) return error.SkipZigTest;+ const io = std.testing.io;+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const home = try homePath(&tmp, a);+ try store.writeOAuth(io, a, home, auth.provider_dir, "old", "refresh", 1);+ var mock: Mock = .{ .server = try auth.listen(io, 0), .block_path = auth.recordPath(a, home) };+ defer mock.server.deinit(io);+ var group: Io.Group = .init;+ defer group.cancel(io);+ helpers.persist_error = null;+ defer helpers.persist_error = null;+ try group.concurrent(io, Mock.run, .{ &mock, io });+ const fresh = auth.loadAt(io, std.testing.allocator, a, home, false, null, try mock.endpoint(a));+ try group.await(io);+ try std.testing.expect(!mock.failed);+ try std.testing.expectEqualStrings("new-access", fresh.?);+ try std.testing.expect(helpers.takePersistError() != null);+ try std.testing.expect(auth.readRecord(io, a, home) == null);+}++test "Anthropic OAuth: refresh-only login without Graff credentials fails without browser" {+ const io = std.testing.io;+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const home = try homePath(&tmp, a);+ try std.testing.expectError(error.OAuthRefreshFailed, auth.login(io, std.testing.allocator, a, home, true));+ try std.testing.expect(auth.loadAnthropicOAuth(io, std.testing.allocator, a, home, false, null) == null);+}++const Stall = struct {+ server: *Io.net.Server,+ send_head: bool,+ accepted: std.atomic.Value(bool) = .init(false),++ fn run(self: *Stall, io: Io) void {+ const stream = self.server.accept(io) catch return;+ defer stream.close(io);+ self.accepted.store(true, .release);+ if (self.send_head) {+ var buf: [256]u8 = undefined;+ var writer = Io.net.Stream.Writer.init(stream, io, &buf);+ writer.interface.writeAll("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 4096\r\nConnection: close\r\n\r\n{") catch return;+ writer.interface.flush() catch return;+ }+ io.sleep(.fromSeconds(10), .awake) catch {};+ }+};++test "Anthropic OAuth: token HTTP deadline cancels stalled headers and bodies" {+ if (builtin.os.tag == .windows) return error.SkipZigTest;+ const io = std.testing.io;+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ for ([_]bool{ false, true }) |send_head| {+ var server = try auth.listen(io, 0);+ defer server.deinit(io);+ var stall: Stall = .{ .server = &server, .send_head = send_head };+ var group: Io.Group = .init;+ defer group.cancel(io);+ try group.concurrent(io, Stall.run, .{ &stall, io });+ const endpoint = try std.fmt.allocPrint(a, "http://127.0.0.1:{d}/token", .{server.socket.address.getPort()});+ const start = util.unixMs(io);+ try std.testing.expectError(error.DeadlineExceeded, auth.postWithDeadline(io, std.testing.allocator, a, endpoint, "{}", "refresh", .fromMilliseconds(150)));+ try std.testing.expect(stall.accepted.load(.acquire));+ try std.testing.expect(util.unixMs(io) - start < 3000);+ }+}++test "Anthropic OAuth: stale callback policy continues until current code or denial" {+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ const attempts = [_][]const u8{+ "GET /callback?code=old-code&state=old-state HTTP/1.1",+ "GET /callback?error=access_denied&state=old-state HTTP/1.1",+ "GET /callback?code=no-state HTTP/1.1",+ "GET /callback?code=current-code&state=current-state HTTP/1.1",+ };+ var ignored: usize = 0;+ for (attempts) |line| {+ const cb = (try auth.parseCallback(a, line)).?;+ if (auth.callbackPolicy(cb, "current-state") == .stale) {+ ignored += 1;+ continue;+ }+ try std.testing.expectEqualStrings("current-code", cb.code);+ try auth.validateCallback(cb, "current-state");+ break;+ }+ try std.testing.expectEqual(@as(usize, 3), ignored);+ const denied = (try auth.parseCallback(a, "GET /callback?error=access_denied&state=current-state HTTP/1.1")).?;+ try std.testing.expectEqual(auth.CallbackPolicy.finish, auth.callbackPolicy(denied, "current-state"));+ try std.testing.expectError(error.OAuthDenied, auth.validateCallback(denied, "current-state"));+}++test "Anthropic OAuth: unrelated port holder is not cancelled" {+ if (builtin.os.tag == .windows) return error.SkipZigTest;+ const io = std.testing.io;+ var first = try auth.listen(io, 0);+ defer first.deinit(io);+ try std.testing.expectError(error.AddressInUse, auth.listen(io, first.socket.address.getPort()));+ // There was no connect/cancel request: the original listener remains usable.+ try std.testing.expect(first.socket.address.getPort() != 0);+}diff --git a/src/picker_auth.zig b/src/picker_auth.zigindex 61da6bef..47162d5c 100644--- a/src/picker_auth.zig+++ b/src/picker_auth.zig@@ -41,7 +41,9 @@ pub const PickerFn = *const fn (/// (and the Codex account id) into the live Keys AND into the running agent, so/// the current conversation uses it without a restart — the in-session twin of/// the startup loaders.-pub fn reloadLoginKey(root: *Agent, keys: *Keys, arena: Allocator, provider_id: []const u8) void {+pub fn reloadLoginKey(root: *Agent, keys: *Keys, arena: Allocator, login_id: []const u8) void {+ const console_login = std.ascii.eqlIgnoreCase(login_id, "console");+ const provider_id = if (console_login) "anthropic" else login_id;const home = root.home;var codegraff_key: ?[]const u8 = null;for (provider_specs, &keys.values, &keys.sources) |spec, *value, *source| {@@ -81,6 +83,21 @@ pub fn reloadLoginKey(root: *Agent, keys: *Keys, arena: Allocator, provider_id:source.* = .login;reloaded = true;}+ } else if (console_login) {+ const profile = @import("anthropic_profile.zig");+ if (profile.load(root.io, arena, profile.ProcessEnv{}, home, false)) |k| {+ value.* = k;+ source.* = .profile;+ reloaded = true;+ }+ } else if (std.mem.eql(u8, provider_id, "anthropic")) {+ if (@import("oauth_anthropic.zig").loadAnthropicOAuth(root.io, root.gpa, arena, home, false, null)) |k| {+ if (source.* != .login) if (value.*) |metered|+ @import("credential_failover.zig").park(provider_id, metered, source.*);+ value.* = k;+ source.* = .login;+ reloaded = true;+ }} else if (std.mem.eql(u8, provider_id, "chatgpt-new")) {if (@import("oauth_chatgpt.zig").loadChatgptOAuth(root.io, root.gpa, arena, home, false, null)) |k| {value.* = k;@@ -102,7 +119,8 @@ pub fn reloadLoginKey(root: *Agent, keys: *Keys, arena: Allocator, provider_id:if (reloaded) if (value.*) |key| if (std.mem.eql(u8, root.provider.id, provider_id)) {const is_codex = std.mem.eql(u8, provider_id, "codex");policy.adoptFreshAuth(root, .{ .key = key, .account = if (is_codex) keys.codex_account else "" });- root.provider.source = .login;+ root.provider.source = source.*;+ root.provider = provider_mod.Provider.withModel(root.provider, root.provider.model);if (std.mem.eql(u8, provider_id, "zai")) root.provider.url = provider_mod.g_zai_url_override orelse spec.url;root.closeCodexWs(); // the held socket was dialed with the stale bearer};@@ -324,6 +342,12 @@ pub fn offerProviderAuth(try out.flush();return;};+ } else if (std.mem.eql(u8, seat_pid, "anthropic")) {+ @import("oauth_anthropic.zig").login(root.io, root.gpa, arena, home, false) catch |err| {+ try out.print("\xe2\x9c\x97 anthropic login failed: {t}\n", .{err});+ try out.flush();+ return;+ };} else if (std.mem.eql(u8, seat_pid, "chatgpt-new")) {@import("oauth_chatgpt.zig").login(root.io, root.gpa, arena, home) catch |err| {try out.print("\xe2\x9c\x97 chatgpt login failed: {t}\n", .{err});diff --git a/src/process_runner.zig b/src/process_runner.zigindex 53d28b43..ff15b323 100644--- a/src/process_runner.zig+++ b/src/process_runner.zig@@ -112,6 +112,12 @@ pub const CappedRunOptions = struct {/// Clipboard export is not a user turn. Esc would kill osascript and/// Ctrl-V would report a cancelled clipboard read (#1146).ignore_cancel: bool = false,+ /// Passed to the child as its stdin. A `--json` child reads one request+ /// line, runs the turn and exits on EOF, so this drives a machine-readable+ /// one-shot (the summarizer) without a pipe the parent would have to feed+ /// concurrently — a write larger than the pipe buffer would deadlock+ /// against a child that has not started reading yet.+ stdin_file: ?Io.File = null,/// #1271: over the cap, keep the first third and the LAST two thirds of/// each stream around a `[... N bytes truncated ...]` gap instead of only/// the first `cap` bytes. The tail lives in a fixed ring (never more than@@ -251,7 +257,7 @@ pub fn runCappedWithOptions(gpa: Allocator, io: Io, argv: []const []const u8, st.argv = argv,.cwd = options.cwd,.environ_map = options.environ_map,- .stdin = .ignore,+ .stdin = if (options.stdin_file) |f| .{ .file = f } else .ignore,.stdout = .pipe,.stderr = .pipe,.pgid = if (posix_process_groups and options.kill_process_tree) 0 else null,diff --git a/src/provider.zig b/src/provider.zigindex 7f515256..79a3c8f9 100644--- a/src/provider.zig+++ b/src/provider.zig@@ -1,13 +1,5 @@-//! The provider/keys core: wire format + auth style + endpoint per provider-//! (ProviderSpec/provider_specs), the resolved Provider a request is sent-//! with (model, context window, api key), and Keys — one optional API key-//! per provider_specs entry, read from the environment, with the routing-//! logic (providerFor/providerById/defaultProvider) that picks a Provider-//! for a model name. Split out of main.zig (600-line goal). Re-exported-//! (`pub const Provider = provider.Provider;` etc.) so the ~everywhere-//! `main_mod.Provider`/`main_mod.Keys`/`main_mod.provider_specs`-//! back-imports across the other split files keep resolving unchanged.-//! Only needs pricing.zig (contextFor/models) for Keys.build/providerFor.+//! Provider specs, resolved request routes, and source-aware credential keys.+//! Re-exported by main.zig for the engine and its clients.const std = @import("std");@@ -45,7 +37,7 @@ fn contextWindowFor(provider_id: []const u8, model: []const u8) u64 {/// provider_specs; one additional OpenAI-compatible router may be loaded from/// `.graff/.config.router` at startup.pub const ProviderSpec = struct {- pub const LoginKind = enum { api_key, codegraff_device, codex_device, kimi_device, xai_device, zai_cli, chatgpt_browser };+ pub const LoginKind = enum { api_key, codegraff_device, codex_device, kimi_device, xai_device, zai_cli, chatgpt_browser, anthropic_browser };pub const CatalogKind = enum { baked, codex, kimi, openai, anthropic };id: []const u8,@@ -76,7 +68,7 @@ pub const provider_specs = [_]ProviderSpec{// Anthropic publishes its live model list at /v1/models (same x-api-key +// anthropic-version auth as Messages), so new Claude releases appear// without a rebuild; the baked pricing.zig rows stay the offline fallback.- .{ .id = "anthropic", .display_name = "Anthropic", .kind = .anthropic, .auth = .x_api_key, .url = "https://api.anthropic.com/v1/messages", .env_key = "ANTHROPIC_API_KEY", .default_model = "claude-opus-5-5", .catalog = .anthropic, .models_url = "https://api.anthropic.com/v1/models?limit=1000" },+ .{ .id = "anthropic", .display_name = "Anthropic", .kind = .anthropic, .auth = .x_api_key, .url = "https://api.anthropic.com/v1/messages", .env_key = "ANTHROPIC_API_KEY", .default_model = "claude-opus-5-5", .login = .anthropic_browser, .sub_login = true, .catalog = .anthropic, .models_url = "https://api.anthropic.com/v1/models?limit=1000" },.{ .id = "codegraff", .display_name = "Codegraff", .kind = .openai, .auth = .bearer, .url = "https://gateway.codegraff.com/v1/chat/completions", .env_key = "CODEGRAFF_API_KEY", .default_model = "mimo-v2.6-pro", .login = .codegraff_device, .catalog = .openai, .models_url = "https://gateway.codegraff.com/v1/models", .takes_effort = true },.{ .id = "deepseek", .display_name = "DeepSeek", .kind = .openai, .auth = .bearer, .url = "https://api.deepseek.com/chat/completions", .env_key = "DEEPSEEK_API_KEY", .default_model = "deepseek-v4-pro", .takes_effort = true },.{ .id = "openai", .display_name = "OpenAI", .kind = .responses, .auth = .bearer, .url = "https://api.openai.com/v1/responses", .env_key = "OPENAI_API_KEY", .default_model = "gpt-6-sol" },@@ -285,7 +277,7 @@ pub const Provider = struct {return .{.id = spec.id,.kind = if (is_kimi_anthropic or @import("provider_codegraff.zig").usesMessages(spec.id, model)) .anthropic else if (is_xai_responses or @import("provider_codegraff.zig").usesResponses(spec.id, model)) .responses else spec.kind,- .auth = if (is_kimi_anthropic) .x_api_key else if (@import("anthropic_profile.zig").bearerToken(spec.id, base.api_key)) .bearer else spec.auth,+ .auth = if (std.mem.eql(u8, spec.id, "anthropic") and base.source.canRefresh()) .bearer else if (is_kimi_anthropic) .x_api_key else spec.auth,.url = resolvedUrl(spec, model),.api_key = base.api_key,.model = model,@@ -319,14 +311,20 @@ pub const Keys = struct {none,environment,login,+ profile, // Anthropic Console SDK profile: metered, not a subscription.stored,session,+ pub fn canRefresh(self: CredentialSource) bool {+ return self == .login or self == .profile;+ }+pub fn label(self: CredentialSource) []const u8 {return switch (self) {.none => "missing",.environment => "environment",.login => "OAuth/login",+ .profile => "Console profile",.stored => "secure store",.session => "this session",};@@ -381,7 +379,7 @@ pub const Keys = struct {return .{.id = spec.id,.kind = if (is_kimi_anthropic or @import("provider_codegraff.zig").usesMessages(spec.id, model)) .anthropic else if (is_xai_responses or @import("provider_codegraff.zig").usesResponses(spec.id, model)) .responses else spec.kind,- .auth = if (is_kimi_anthropic) .x_api_key else if (@import("anthropic_profile.zig").bearerToken(spec.id, key)) .bearer else spec.auth,+ .auth = if (std.mem.eql(u8, spec.id, "anthropic") and keys.source(spec.id).canRefresh()) .bearer else if (is_kimi_anthropic) .x_api_key else spec.auth,.url = resolvedUrl(spec, model),.api_key = key,.model = model,diff --git a/src/rlm.zig b/src/rlm.zigindex 5c621766..8f31fb06 100644--- a/src/rlm.zig+++ b/src/rlm.zig@@ -247,6 +247,13 @@ fn runHostRaw(ctx: ToolCtx, call: spec_ptc.Call) ToolOutput {// A denial is an error result, so the script stops here.const host_call: tools.ToolCall = .{ .id = if (ctx.host_gate) |gate| gate.call_id else "rlm", .name = ready.name, .input = parsed.value };if (ctx.host_gate) |gate| if (gate.check(gate.context, host_call)) |denied| return denied;+ // #1413: schema loading enables catalog calls, not host bindings for+ // agent-managed tools. Their handlers mutate Agent state inline; they+ // cannot run through the pool-thread executor used by RLM.+ if (@import("schema.zig").isMetaName(ready.name)) return .{+ .text = std.fmt.allocPrint(ctx.gpa, "rlm: {s} is agent-managed and is not supported inside rlm; call {s} directly as a structured tool.", .{ ready.name, ready.name }) catch &.{},+ .is_error = true,+ };var host_ctx = ctx;host_ctx.rlm_host = true;return exec_mod.execTool(host_ctx, host_call);diff --git a/src/rlm_order_tests.zig b/src/rlm_order_tests.zigindex 59a895b3..43fa623a 100644--- a/src/rlm_order_tests.zig+++ b/src/rlm_order_tests.zig@@ -286,6 +286,61 @@ test "rlm bash and write_file at the root ask the permission handler like catalo}}+test "rlm loaded peer_message reports structured-only support and stops the script (#1413)" {+ var f = try Fixture.init();+ defer f.deinit();+ var arena = std.heap.ArenaAllocator.init(gpa);+ defer arena.deinit();+ const fold = @import("native_fold.zig");+ const saved_enabled = fold.enabled;+ defer {+ fold.enabled = saved_enabled;+ fold.resetSession();+ }+ fold.enabled = true;+ fold.resetSession();+ var agent: Agent = .{ .gpa = gpa, .arena = arena.allocator(), .io = io, .client = &f.client, .provider = .{ .id = "fixture", .kind = .openai, .auth = .bearer, .url = "", .api_key = "", .model = "fixture", .context = 1000 }, .messages = .init(arena.allocator()), .sub = false, .label = "root", .out = null, .agent_cwd = f.cwd };+ defer agent.tools_used.deinit(gpa);+ const load = try std.json.parseFromSliceLeaky(std.json.Value, arena.allocator(), "{\"tools\":[\"peer_message\"]}", .{});+ const loaded = try agent.runTools(&.{.{ .id = "load", .name = "load_tool_schemas", .input = load }});+ try std.testing.expect(!loaded[0].is_error);+ try std.testing.expect(fold.isLoaded("peer_message"));+ const results = try agent.runTools(&.{try rlmCall(arena.allocator(), "p = peer_message(action=\"inbox\")\nw = write_file(path=\"marker\", content=\"ran\")\nprint(p)")});+ try std.testing.expect(results[0].is_error);+ try std.testing.expectEqualStrings("rlm: peer_message is agent-managed and is not supported inside rlm; call peer_message directly as a structured tool.", results[0].text);+ try std.testing.expectError(error.FileNotFound, f.tmp.dir.openFile(io, "marker", .{}));+ // Loading still enables the normal inline handler, not a host function.+ const input = try std.json.parseFromSliceLeaky(std.json.Value, arena.allocator(), "{\"action\":\"invalid-fixture-action\"}", .{});+ const direct = try agent.runTools(&.{.{ .id = "direct", .name = "peer_message", .input = input }});+ try std.testing.expect(direct[0].is_error);+ try std.testing.expect(std.mem.startsWith(u8, direct[0].text, "peer_message action must be "));+}++test "rlm agent-managed hosts refuse direct nested and each calls without executing the tail (#1413)" {+ var f = try Fixture.init();+ defer f.deinit();+ for (@import("schema.zig").meta_names) |name| {+ const code = try std.fmt.allocPrint(gpa, "x = {s}()\nw = write_file(path=\"marker\", content=\"ran\")", .{name});+ defer gpa.free(code);+ const out = try f.run(code);+ defer gpa.free(out.text);+ try std.testing.expect(out.is_error);+ try std.testing.expect(std.mem.indexOf(u8, out.text, "is not supported inside rlm") != null);+ try std.testing.expect(std.mem.indexOf(u8, out.text, name) != null);+ try std.testing.expectError(error.FileNotFound, f.tmp.dir.openFile(io, "marker", .{}));+ }+ for ([_][]const u8{+ "print(peer_message(action=\"inbox\"), write_file(path=\"marker\", content=\"ran\"))",+ "x = each([\"inbox\", \"list\"], \"peer_message\", \"action\")\nw = write_file(path=\"marker\", content=\"ran\")",+ }) |code| {+ const out = try f.run(code);+ defer gpa.free(out.text);+ try std.testing.expect(out.is_error);+ try std.testing.expect(std.mem.indexOf(u8, out.text, "call peer_message directly as a structured tool") != null);+ try std.testing.expectError(error.FileNotFound, f.tmp.dir.openFile(io, "marker", .{}));+ }+}+test "rlm destructive git in a yolo subagent hits the subagent block (#1292)" {var f = try Fixture.init();defer f.deinit();diff --git a/src/rlm_query.zig b/src/rlm_query.zigindex 963166e8..8ccd1da2 100644--- a/src/rlm_query.zig+++ b/src/rlm_query.zig@@ -116,6 +116,13 @@ pub fn buildBody(gpa: Allocator, provider: Provider, prompt: []const u8) ![]u8 {try s.write(prompt);},.anthropic => {+ const oauth = @import("anthropic_oauth_wire.zig");+ if (oauth.active(provider)) {+ try s.objectField("system");+ try s.beginArray();+ try oauth.writeSystemPrefix(&s);+ try s.endArray();+ }// ADR 0219: a Claude model that thinks by default spends max_tokens// on thinking first; 256 left no answer. Room, and little thinking.const claude = @import("claude_wire.zig");diff --git a/src/route_report.zig b/src/route_report.zigindex 0c217bb1..06ec4c9f 100644--- a/src/route_report.zig+++ b/src/route_report.zig@@ -51,6 +51,7 @@ pub fn loginOnDisk(io: Io, arena: Allocator, home: []const u8, spec: provider_mo.api_key => return false,.kimi_device => credential_store.oauthPath(arena, home, ".kimi"),.xai_device => credential_store.oauthPath(arena, home, ".xai"),+ .anthropic_browser => credential_store.oauthPath(arena, home, ".anthropic"),.zai_cli => credential_store.oauthPath(arena, home, ".zai"),.chatgpt_browser => return @import("oauth_chatgpt.zig").onDisk(io, arena, home),.codex_device => blk: {diff --git a/src/router_catalog.zig b/src/router_catalog.zigindex 4858eec0..f0a47c73 100644--- a/src/router_catalog.zig+++ b/src/router_catalog.zig@@ -225,16 +225,16 @@ pub fn parseModels(arena: Allocator, provider_id: []const u8, data: []const u8)};}-/// Catalog GET auth mirrors chat auth: Bearer for OpenAI-style routers and an-/// Anthropic Console sign-in (ADR 0281); an Anthropic API key sends x-api-key +-/// anthropic-version. xAI OAuth user tokens also need X-XAI-Token-Auth.+/// Catalog auth matches native subscription, Console profile and API-key routes.pub fn catalogHeaders(arena: Allocator, spec: provider.ProviderSpec, key: []const u8, source: provider.Keys.CredentialSource, buf: *[4]std.http.Header) ?[]const std.http.Header {buf[0] = .{ .name = "Accept", .value = "application/json" };if (spec.auth == .x_api_key) {- const console = @import("anthropic_profile.zig").bearerToken(spec.id, key);- buf[1] = if (console) .{ .name = "Authorization", .value = std.fmt.allocPrint(arena, "Bearer {s}", .{key}) catch return null } else .{ .name = "x-api-key", .value = key };+ const login = std.mem.eql(u8, spec.id, "anthropic") and source == .login;+ const bearer = login or (std.mem.eql(u8, spec.id, "anthropic") and source == .profile);+ buf[1] = .{ .name = if (bearer) "Authorization" else "x-api-key", .value = if (bearer) std.fmt.allocPrint(arena, "Bearer {s}", .{key}) catch return null else key };buf[2] = .{ .name = "anthropic-version", .value = root.anthropic_version };- return buf[0..3];+ buf[3] = .{ .name = "anthropic-beta", .value = "claude-code-20250219,oauth-2025-04-20" };+ return buf[0..if (login) @as(usize, 4) else 3];}const bearer = std.fmt.allocPrint(arena, "Bearer {s}", .{key}) catch return null;buf[1] = .{ .name = "Authorization", .value = bearer };diff --git a/src/run_task_meta.zig b/src/run_task_meta.zignew file mode 100644index 00000000..f05aafb4--- /dev/null+++ b/src/run_task_meta.zig@@ -0,0 +1,143 @@+//! The `codegraff/runTask` facts that travel with a delegated MCP task: from+//! the MCP result's `_meta` mirror, through the engine's `tool_result` event,+//! into ACP `_meta` on the completed `tool_call_update`.+//!+//! Only these five facts move. The task's text and the caller's context never+//! do, and no charge is estimated here — `usage_reported` says whether a+//! settlement exists, it does not invent one (ADR 0253).++const std = @import("std");+const Value = std.json.Value;+const RunTaskMeta = @import("engine_events.zig").RunTaskMeta;++/// The namespaced key on both the MCP result and the ACP update. Same key on+/// purpose: a client can follow one name across the boundary.+pub const key = "codegraff/runTask";++fn intField(o: std.json.ObjectMap, k: []const u8) ?u64 {+ const v = o.get(k) orelse return null;+ return switch (v) {+ .integer => |i| if (i >= 0) @intCast(i) else null,+ else => null,+ };+}++fn boolField(o: std.json.ObjectMap, k: []const u8) bool {+ const v = o.get(k) orelse return false;+ return v == .bool and v.bool;+}++fn strField(o: std.json.ObjectMap, k: []const u8) ?[]const u8 {+ const v = o.get(k) orelse return null;+ return if (v == .string) v.string else null;+}++/// Borrow the five fields out of a `codegraff/runTask` object. The strings+/// stay owned by the JSON that produced them; use `readDup` when that JSON is+/// about to die. `context_chars` is the anchor: without it this is not a+/// runTask object, and a partial one is ignored rather than half-reported.+pub fn read(o: std.json.ObjectMap) ?RunTaskMeta {+ return .{+ .context_chars = intField(o, "context_chars") orelse return null,+ .context_used_chars = intField(o, "context_used_chars") orelse 0,+ .context_summarized = boolField(o, "context_summarized"),+ .usage_reported = boolField(o, "usage_reported"),+ .usage_unavailable_reason = strField(o, "usage_unavailable_reason") orelse "",+ };+}++/// `read`, with the one borrowed string copied into `a`.+pub fn readDup(a: std.mem.Allocator, o: std.json.ObjectMap) ?RunTaskMeta {+ var m = read(o) orelse return null;+ m.usage_unavailable_reason = a.dupe(u8, m.usage_unavailable_reason) catch "";+ return m;+}++/// The mirror inside an MCP `tools/call` result object.+pub fn fromMcpResult(a: std.mem.Allocator, result: std.json.ObjectMap) ?RunTaskMeta {+ const meta = result.get("_meta") orelse return null;+ if (meta != .object) return null;+ const rt = meta.object.get(key) orelse return null;+ if (rt != .object) return null;+ return readDup(a, rt.object);+}++/// The same object as it rides a `--json` tool_result line.+pub fn fromEvent(ev: Value) ?RunTaskMeta {+ if (ev != .object) return null;+ const rt = ev.object.get("run_task") orelse return null;+ if (rt != .object) return null;+ return read(rt.object);+}++const testing = std.testing;++/// These tests parse JSON into an arena: `parseFromSliceLeaky` is leaky by+/// design, and the testing allocator counts every leaked byte.+fn parse(a: std.mem.Allocator, raw: []const u8) !Value {+ return std.json.parseFromSliceLeaky(Value, a, raw, .{});+}++test "read: the five fields round-trip, and a missing anchor is not a runTask object" {+ var arena = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ const v = try parse(a,+ \\{"context_chars":40,"context_used_chars":7,"context_summarized":true,"usage_reported":false,"usage_unavailable_reason":"no settlement"}+ );+ const m = read(v.object).?;+ try testing.expectEqual(@as(u64, 40), m.context_chars);+ try testing.expectEqual(@as(u64, 7), m.context_used_chars);+ try testing.expect(m.context_summarized);+ try testing.expect(!m.usage_reported);+ try testing.expectEqualStrings("no settlement", m.usage_unavailable_reason);+ // Without the anchor this is some other object that happens to share a key.+ const other = try parse(a, "{\"usage_reported\":false}");+ try testing.expect(read(other.object) == null);+}++test "fromMcpResult: reads the namespaced mirror, ignores anything else" {+ var arena = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ const ok = try parse(a,+ \\{"text":"answer","_meta":{"codegraff/runTask":{"context_chars":9,"usage_unavailable_reason":"r"}}}+ );+ const m = fromMcpResult(a, ok.object).?;+ try testing.expectEqual(@as(u64, 9), m.context_chars);+ try testing.expectEqualStrings("r", m.usage_unavailable_reason);+ for ([_][]const u8{+ \\{"text":"answer"}+ ,+ \\{"text":"answer","_meta":{"graff/view":{"kind":"html"}}}+ ,+ \\{"text":"answer","_meta":{"codegraff/runTask":"nope"}}+ ,+ }) |raw| {+ const v = try parse(a, raw);+ try testing.expect(fromMcpResult(a, v.object) == null);+ }+}++test "readDup copies the reason; fromEvent reads the wire line" {+ var arena = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ const v = try parse(a,+ \\{"run_task":{"context_chars":3,"context_summarized":true,"usage_reported":false,"usage_unavailable_reason":"why"}}+ );+ const m = fromEvent(v).?;+ try testing.expectEqual(@as(u64, 3), m.context_chars);+ try testing.expect(m.context_summarized);+ try testing.expectEqualStrings("why", m.usage_unavailable_reason);+ try testing.expect(fromEvent(.null) == null);+ try testing.expect(fromEvent(try parse(a, "{\"type\":\"tool_result\"}")) == null);+ // A dup survives the JSON that produced it.+ var src = std.heap.ArenaAllocator.init(testing.allocator);+ const v2 = try parse(src.allocator(),+ \\{"codegraff/runTask":{"context_chars":1,"usage_unavailable_reason":"kept"}}+ );+ const dup = readDup(a, v2.object.get("codegraff/runTask").?.object).?;+ src.deinit();+ try testing.expectEqualStrings("kept", dup.usage_unavailable_reason);+}diff --git a/src/schema.zig b/src/schema.zigindex dbd663d3..db8e9ee7 100644--- a/src/schema.zig+++ b/src/schema.zig@@ -402,6 +402,8 @@ fn providerLoginKind(id: []const u8) []const u8 {/// drop-and-retry in request() still covers any other model that/// turns out to reject it.pub fn providerTakesEffort(kind: Provider.Kind, id: []const u8, model: []const u8) bool {+ // Native Claude discovery must match output_config.effort serialization (ADR 0219).+ if (kind == .anthropic and std.mem.eql(u8, id, "anthropic")) return @import("claude_wire.zig").takesEffort(model);if (std.mem.startsWith(u8, model, "grok")) return std.mem.eql(u8, id, "xai");if (@import("effort_route.zig").mimoRoute(id, model)) return true;return kind == .responses ordiff --git a/src/session_settings.zig b/src/session_settings.zigindex 0578cc23..6f18a064 100644--- a/src/session_settings.zig+++ b/src/session_settings.zig@@ -243,7 +243,7 @@ pub fn applyEnvKnobs(arena: Allocator, environ_map: anytype) !void {@import("agent_clef_archive.zig").g_enabled = !off;}// #502: xAI defaults to the Responses wire (api.x.ai/v1/responses) —- // first-party server compaction + WS turns. GRAFF_XAI_WIRE=chat (anything+ // WS turns + client-summary compaction. GRAFF_XAI_WIRE=chat (anything// but "responses") moves it back to chat completions; unset keeps the default.if (environ_map.get("GRAFF_XAI_WIRE")) |v| {provider_mod.g_xai_responses = std.ascii.eqlIgnoreCase(std.mem.trim(u8, v, " \t"), "responses");diff --git a/src/spec_provider_conformance.zig b/src/spec_provider_conformance.zigindex eda9cd95..07f0ea37 100644--- a/src/spec_provider_conformance.zig+++ b/src/spec_provider_conformance.zig@@ -34,6 +34,7 @@ fn loginName(l: ProviderSpec.LoginKind) []const u8 {.xai_device => "xai_device",.zai_cli => "zai_cli",.chatgpt_browser => "chatgpt_browser",+ .anthropic_browser => "anthropic_browser",};}diff --git a/src/startup.zig b/src/startup.zigindex f5b26c26..852361ec 100644--- a/src/startup.zig+++ b/src/startup.zig@@ -333,7 +333,7 @@ pub fn runSubcommand(io: Io, gpa: Allocator, arena: Allocator, init: std.process// args.loginTarget owns the names.if (flags.login_flag) {const home = keys_cli.homeEnv(init.environ_map) orelse std.process.fatal("no HOME/USERPROFILE", .{});- if (flags.anthropic_login) try @import("anthropic_profile.zig").login(io, arena, init.environ_map, home) else if (flags.chatgpt_login) try @import("oauth_chatgpt.zig").login(io, gpa, arena, home) else if (flags.xai_login) try oauth.xaiLogin(io, gpa, arena, home) else if (flags.kimi_login) try oauth.kimiLogin(io, gpa, arena, home) else if (flags.zai_login) try oauth.zaiLogin(io, gpa, arena, home) else if (flags.codex_login or flags.refresh_flag) try oauth.codexLogin(io, gpa, arena, home, flags.refresh_flag) else try oauth.codegraffLogin(io, gpa, arena, home);+ if (flags.anthropic_console_login) try @import("anthropic_profile.zig").login(io, arena, init.environ_map, home) else if (flags.anthropic_login) try @import("oauth_anthropic.zig").login(io, gpa, arena, home, flags.refresh_flag) else if (flags.chatgpt_login) try @import("oauth_chatgpt.zig").login(io, gpa, arena, home) else if (flags.xai_login) try oauth.xaiLogin(io, gpa, arena, home) else if (flags.kimi_login) try oauth.kimiLogin(io, gpa, arena, home) else if (flags.zai_login) try oauth.zaiLogin(io, gpa, arena, home) else if (flags.codex_login or flags.refresh_flag) try oauth.codexLogin(io, gpa, arena, home, flags.refresh_flag) else try oauth.codegraffLogin(io, gpa, arena, home);return true;}diff --git a/src/startup_keys.zig b/src/startup_keys.zigindex f7d71ae5..00172f49 100644--- a/src/startup_keys.zig+++ b/src/startup_keys.zig@@ -253,8 +253,9 @@ fn missingCredentials() noreturn {std.process.fatal(\\no API key found. quickest fixes:\\ graff login free codegraff key (device-code OAuth)+ \\ graff login anthropic Claude subscription browser sign-in (also: ant)\\ graff key set <provider> <key> store a key (macOS Keychain, else 0600 file)- \\ graff login anthropic Anthropic Console account (its workspace's API credits)+ \\ graff login console Anthropic Console account (its workspace's API credits)\\ export ANTHROPIC_API_KEY=sk-ant-… or CODEGRAFF/DEEPSEEK/OPENAI/MINIMAX/XIAOMI/KIMI/MOONSHOT/XAI/ZAI _API_KEY\\a Codex CLI login (~/.codex/auth.json) is also picked up automatically., .{});@@ -321,7 +322,7 @@ pub fn resolveKeysOptional(io: Io, gpa: Allocator, arena: Allocator, environ_map// ANTHROPIC_PROFILE outranks a stored key; the active one fills in after.const ap = @import("anthropic_profile.zig");if (keys.get("anthropic") == null) if (ap.load(io, arena, environ_map, keys_cli.homeEnv(environ_map) orelse "", true)) |t| {- _ = keys.set("anthropic", t, .login);+ _ = keys.set("anthropic", t, .profile);};const selective_stored_keys = hasSelectiveStoredKeyScope(model_flag);var stored_keys_loaded = !selective_stored_keys;@@ -339,7 +340,7 @@ pub fn resolveKeysOptional(io: Io, gpa: Allocator, arena: Allocator, environ_map}const home = keys_cli.homeEnv(environ_map) orelse "";if (keys.get("anthropic") == null) if (ap.load(io, arena, environ_map, home, false)) |t| {- _ = keys.set("anthropic", t, .login);+ _ = keys.set("anthropic", t, .profile);};var model_catalog: models_cache.LazyCodexCatalog = .{ .codex_home = oauth.codexHomeDir(arena, home) orelse "" };const saved_model: ?serde.SavedModel = if (model_flag == null) serde.loadModel(io, arena, home) else null;diff --git a/src/todo_reply.zig b/src/todo_reply.zigindex a0b7f221..811ed830 100644--- a/src/todo_reply.zig+++ b/src/todo_reply.zig@@ -15,6 +15,7 @@ pub fn text(arena: Allocator, todos: []const TodoItem, epoch: u64, incoming: []cvar doing: usize = 0;var open: usize = 0;var cancelled: usize = 0;+ var dropped: usize = 0;for (todos) |t| {if (t.epoch != epoch or t.retired) continue;if (std.mem.eql(u8, t.status, "completed")) {@@ -22,7 +23,9 @@ pub fn text(arena: Allocator, todos: []const TodoItem, epoch: u64, incoming: []c} else if (std.mem.eql(u8, t.status, "in_progress")) {doing += 1;} else if (std.mem.eql(u8, t.status, "cancelled")) {- cancelled += 1; // #1545: parked after a scope change, not pending work+ cancelled += 1; // scope changed, not pending work (#1545)+ } else if (@import("goal_state.zig").isDropped(t.status)) {+ dropped += 1;} else {open += 1;}@@ -32,6 +35,7 @@ pub fn text(arena: Allocator, todos: []const TodoItem, epoch: u64, incoming: []ctry w.print("Todo list saved: {d} done, {d} in progress, {d} pending", .{ done, doing, open });if (cancelled > 0) try w.print(", {d} cancelled", .{cancelled});try w.writeAll(".");+ if (dropped > 0) try w.print(" {d} dropped (abandoned by user order; no longer blocks completion).", .{dropped});var kept_header = false;for (todos) |t| {if (t.epoch != epoch or t.retired or mentions(incoming, t.content)) continue;@@ -47,7 +51,7 @@ pub fn text(arena: Allocator, todos: []const TodoItem, epoch: u64, incoming: []cfn mark(status: []const u8) []const u8 {if (std.mem.eql(u8, status, "completed")) return "[x]";if (std.mem.eql(u8, status, "in_progress")) return "[~]";- if (std.mem.eql(u8, status, "cancelled")) return "[-]";+ if (std.mem.eql(u8, status, "cancelled") or @import("goal_state.zig").isDropped(status)) return "[-]";return "[ ]";}@@ -90,3 +94,21 @@ test "todo_write reply: names what graff kept and what the call dropped" {\\(2 open item(s) you left out were dropped; re-list one to keep it), got);}++test "#1545 todo reply distinguishes cancelled and dropped records from pending work" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const todos = [_]TodoItem{+ .{ .content = "finished", .status = "completed", .epoch = 1 },+ .{ .content = "scope changed", .status = "cancelled", .epoch = 1 },+ .{ .content = "abandoned", .status = "dropped", .epoch = 1 },+ .{ .content = "remaining", .status = "pending", .epoch = 1 },+ };+ const got = try text(arena_state.allocator(), &todos, 1, &.{ "finished", "remaining" }, 0, 0);+ try std.testing.expectEqualStrings(+ \\Todo list saved: 1 done, 0 in progress, 1 pending, 1 cancelled. 1 dropped (abandoned by user order; no longer blocks completion).+ \\Kept from before (not in your list):+ \\[-] scope changed+ \\[-] abandoned+ , got);+}diff --git a/src/tools.zig b/src/tools.zigindex 47ff9e1a..1d17b486 100644--- a/src/tools.zig+++ b/src/tools.zig@@ -37,6 +37,9 @@ pub const ExecResult = struct {cancelled: bool = false, // ended by user Esc, not by failing (#266)pending: bool = false, // asynchronous work has no terminal result yetms: i64 = 0, // wall-clock of the tool exec (external tools only; --timing)+ /// Set only when a delegated MCP task reported context/usage metadata+ /// (run_task_meta.zig). Arena-owned like `text`.+ run_task: ?@import("engine_events.zig").RunTaskMeta = null,};pub const AnswerRequest = struct {@@ -85,6 +88,8 @@ pub const ToolOutput = struct {cancelled: bool = false, // ended by user Esc, not by failing (#266)pending: bool = false, // asynchronous work has no terminal result yetms: i64 = 0, // set by execTool+ /// Delegated MCP task facts, when the call reported them (run_task_meta.zig).+ run_task: ?@import("engine_events.zig").RunTaskMeta = null,};// `/rewind`'s pre-write file history lives in snapshots.zig (this file is at