graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Merge optional hosted compaction for v0.302.40
justrache9a4f0a77dc3
diff --cc docs/adr/0252-hosted-compaction-with-a-codegraff-login.mdindex 00000000,ec98f853..c2a290c0mode 000000,100644..100644--- a/docs/adr/0252-hosted-compaction-with-a-codegraff-login.md+++ b/docs/adr/0252-hosted-compaction-with-a-codegraff-login.md@@@ -1,0 -1,75 +1,76 @@@+ # 0252. Hosted compaction with a Codegraff login+-Status: accepted 2026-10-04.++Status: accepted 2026-10-04; default superseded by ADR 0261.++ ## Context++ graff's own compaction asks the session's model for a summary and restarts+ history from it (agent_compact.zig). A summary is lossy in the worst place:+ exact error text, file paths and the constraints a task depends on are what a+ paraphrase drops, and nothing records what was dropped.++ The Codegraff gateway now offers `POST /v1/compact`. It takes a neutral+ transcript (`role`, `text`, `toolUses`, `toolResults`) and answers with a+ decision per tool call — keep it, drop only its result's bulk, or drop the+ call and its result — made by a decision model. Text is never rewritten, the+ first message and the newest few are pinned, and every decision comes back+ with the reply. Pruning alone cannot shrink a session forever, so in `hybrid`+ mode, when pruning frees too little, the gateway also summarizes the middle+ of the transcript and splices that summary in. The call is billed to the+ account like any other request and returns a settled charge receipt.++ ## Decision+-- With a persisted Codegraff login, `compact()` tries the gateway first, with++- With `GRAFF_CLEF_COMPACT=1` and a persisted Codegraff login, `compact()` tries the gateway first, with+ `model: "clef"` and `mode: "hybrid"`. The only credential is the persisted+ login (the same one the effort picker uses), never the chat provider's key.- Without a login nothing changes.++ The opt-in defaults off (ADR 0261); a login alone never enables this path.++ Without a login the existing experimental provider-key path is unchanged.+ - The reply is applied to graff's own wire-format history by call id+ (gateway_compact_wire.zig). Nothing is rebuilt from the neutral copy, so+ reasoning items, images and provider metadata stay as they were.+ - A trimmed result keeps its head and tail plus the #409 pointer to the full+ output when the session is durable.+ - A dropped call goes with its result only in earlier turns. In the live+ turn the call stays and only its result shrinks, so that turn's reasoning,+ thinking blocks and signatures stay paired with every call they reference.+ Earlier turns' reasoning items go, as in the local path (#174).+ - A spliced summary becomes graff's usual handoff note between the pinned+ first message and the kept tail. The cut moves back over any call a kept+ result answers, and an unresolved turn's opening prompt stays verbatim+ (#581).+ - The reply is checked before it is installed: the transcript it returns has+ to be exactly what its decisions imply, the new history may not pair calls+ and results worse than the old one, and it has to free at least a tenth of+ the estimated context. Anything else is a failure.+ - Refusals and failures:+ - A refused key (401/403) turns hosted compaction off for the session and+ compaction stays local.+ - Exhausted credits (`insufficient_credits`, `key_budget_exceeded`) turn it+ off for the session as well. Compaction continues with the local summary,+ and graff says once that it is local and lossy until the account is topped+ up. `gateway_compact.on_credits_exhausted = .off` stops compaction instead.+ - Any other failure — a 5xx, a timeout, a reply that does not check out —+ falls back to the local summary for that compaction only and is noted in+ the trace.+ - The settled charge is added to the session's cost tally; an unsettled reply+ counts its tokens as unpriced.+ - Opaque provider state (Responses `compaction` items) still needs the+ provider's own compaction: `compact()` refuses before the gateway is asked.+ Routes that compact in-stream keep doing so; the gateway replaces the local+ summary wherever graff would have written one, including their near-the-wall+ fallback.+ - `GRAFF_HOSTED_COMPACT=0` keeps compaction local. `GRAFF_COMPACT_URL` and+ `GRAFF_COMPACT_MODEL` point it at another gateway or decision model.++ ## Consequences++ Signed-in sessions send their transcript, tool output included, to the+ Codegraff gateway each time they compact. Each compaction costs a small+ billed request and a round trip, which buys decisions that can be audited and+ results that are trimmed rather than paraphrased. A gateway outage or an empty+ balance never stops a session; it costs that compaction its quality.+ The endpoint may keep more than its six newest messages when it moves its+ cut back over a call and its result; graff accepts any such tail. Revisit to+ send graff's own working set as `preserveRecentMessages`, or when live-turn+ calls can be dropped safely on a given wire.diff --cc docs/adr/README.mdindex ee83d09c,74bae277..e2be5d7a--- a/docs/adr/README.md+++ b/docs/adr/README.md@@@ -278,26 -278,7 +278,27 @@@ record only when you need the evidence| [0249](0249-a-spec-task-ends-at-its-own-tests.md) | The work note no longer says a green public test is not the whole spec: it made the model write an extra verification script on every spec task without changing the pass rate. Spec tasks end at their own tests; the lean note is unchanged. Refines 0024. || [0250](0250-pr-publication-is-not-gated.md) | PR publication is not gated: no local-check, head-CI or claim-review preflight and no completion obligation; drafts complete like any task. Artifact claims and disclosure policy are unchanged. Supersedes 0120, 0123, 0126, 0133 and parts of 0100/0104. || [0251](0251-tool-results-inline-up-to-64-kib.md) | Tool results inline up to 64 KiB (was 16 KiB); `effectiveThreshold` still clamps to half the context window, so small windows are unchanged. The codedb small-read exemption follows the same number. |-| [0252](0252-hosted-compaction-with-a-codegraff-login.md) | With a Codegraff login, `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. |+| [0253](0253-an-interactive-session-signs-in-to-chatgpt-again.md) | When a ChatGPT request is rejected and the refresh cannot replace the token, an interactive root session opens the browser sign-in, waits up to three minutes and retries once; `-p`, piped, `--json`, ACP and sub-agents never do. One unfinished sign-in ends it for the process. |+| [0254](0254-imported-conversations-use-the-configured-route.md) | Imported Claude history uses the configured graff provider and credentials; source models are metadata, and foreign tool results remain historical receipts. |+| [0262](0262-a-follow-up-ends-a-blocking-output-wait.md) | A queued follow-up ends a blocking `action=output` wait like Esc does (the job keeps running and still reports on exit); an ACP prompt arriving mid-turn raises that follow-up without cancelling the turn. Refines ADR 0010. |+| [0255](0255-codegraff-claude-uses-native-messages.md) | Claude aliases on codegraff use the native Messages endpoint; `isClaudeApi` is the one predicate for the real Claude wire. |+| [0256](0256-gemini-takes-fewer-requests-per-task.md) | Gemini models get a working note (chained shell steps, no read-backs, no pasted files, check computed answers) and may batch `attempt_completion` with a final check or write; the completion is recorded only if every batched call succeeded. |+| [0257](0257-a-slimmed-comment-list-names-its-issue.md) | A slimmed MCP comment list keeps the issue id its rows share (`{issue, n, latest_author}`), so parallel results name what they describe. |+| [0258](0258-gemini-chat-sends-repo-context-once.md) | On the Codegraff chat wire, Gemini root turns send the project instructions and layout as the conversation's first user message (ADR 0243's split), so the system prompt every request repeats no longer carries them. |+| [0259](0259-openai-routes-compact-only-server-side.md) | First-party Responses routes (direct API, Codegraff GPT, ChatGPT plan) compact only server-side: no client-summary fallback while the server arm is on. A compaction item's `encrypted_content` is metered at 16 bytes/token, not as text, so a blob-anchored history does not re-compact every step. |+| [0260](0260-clef-compaction-replaces-client-summary.md) | `GRAFF_CLEF_COMPACT=1` routes client-summary compactions through the gateway's server-side `POST /v1/compact` (Clef decision-model pruning, text verbatim); OpenAI-family server paths are untouched and any failure falls back to the summary. |+| [0261](0261-client-summary-stays-the-compactor.md) | The client summary stays the compactor: `GRAFF_CLEF_COMPACT` defaults off (Clef rarely pruned and mostly fell back to the summary in measurement); first-party Responses routes use only the provider's own compaction. Supersedes 0260's default. |+| [0270](0270-a-closed-stdio-mcp-server-restarts-on-its-next-call.md) | A stdio MCP server whose connection closes goes back to dormant with its launch config and restarts on its next call; the failed call is never replayed, and after two restarts in a session its tools are withdrawn as before. |+| [0278](0278-t3-code-acp-profile.md) | An ACP client named `t3-code…` gets a profile: slash commands are the first prompt block, the client's `cwd` is never swapped for an auto-isolated tree, a `model` config option lists reachable models, and `/model` and `/yolo` are not advertised. |+| [0280](0280-mcp-servers-report-state-and-reconnect.md) | Every configured MCP server has one status row (`graff mcp status --json`, `/mcp`); `graff mcp reconnect` / `/mcp reconnect` run a real handshake and refresh its catalog; `graff mcp enable|disable` live in `~/.codegraff/mcp-state.json`; the catalog cache keys on cwd + env, and server `env` overlays graff's own. |+| [0281](0281-anthropic-console-sign-in-reads-the-sdk-profile.md) | Anthropic Console sign-in: graff reads the `user_oauth` profile Anthropic's CLI writes (`ant auth login`), in the SDK's precedence (API key wins), sends it as Bearer, and refreshes through `ant auth print-credentials`; `graff login anthropic` runs `ant auth login`. No claude.ai subscription login. |+| [0282](0282-claude-system-breakpoint-closes-before-the-repo-context.md) | On Claude, the system prompt is two text blocks with the same bytes: the cache breakpoint closes the static instructions, before the project instructions and layout, so every repo shares the tools + instructions prefix. |+| [0283](0283-agents-prepare-and-linked.md) | New task worktrees symlink the gitignored paths `.agents/linked` lists from the main checkout and run `.agents/prepare` when `.graff/workspace.toml` names no setup script. |+| [0284](0284-output-limits-are-recovery-not-completion.md) | Output limits continue with bounded recovery; empty replies back off; one-shots expose a saved-session handle and classified result without claiming unknown costs or exactly-once replay. |+| [0271](0271-gemini-interactions-continue-the-stored-interaction.md) | Direct Gemini (Interactions) requests are stored and continue the last Interaction with `previous_interaction_id` plus only the new steps while a content hash proves graff's history extends it; any rewrite, model switch or rejected id falls back to a full replay; usage meters the whole prompt from the per-invocation counts. Tool choice is `validated`, `minimal` thinking maps to `low`, thought summaries stream, and Gemini 3.1 Pro with tools uses its custom-tools variant. `GRAFF_INTERACTIONS_STORE=0` keeps it stateless. |+| [0272](0272-compaction-never-summarizes-unread-tool-results.md) | A compaction never summarizes tool results the model has not read: the cut keeps the newest model turn and its results verbatim, and the pre-send gate sends a batch that dominates the context and compacts after the model reads it. Summaries copy exact values. Interactions `user_input` steps are turn boundaries. Gemini compacts by default at 80% capped at 300k tokens. |+| [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. |## When to write onediff --cc src/agent_compact.zigindex 199dedbb,85a1e243..2ce3fbd3--- a/src/agent_compact.zig+++ b/src/agent_compact.zig@@@ -118,8 -117,7 +118,11 @@@ pub fn compactPrelude(self: *Agent) ?uspub fn compact(self: *Agent) anyerror!usize {// An opaque item anywhere in history needs the server, never a local summary.if (@import("compaction_window.zig").latestBlob(self.messages.items) != null) return error.ServerCompactionRequired;- // Clef gateway pruning replaces the client summary when eligible.- if (@import("agent_clef_compact.zig").tryCompact(self)) |n| return n;- if (try @import("gateway_compact.zig").run(self)) |freed| return freed; // ADR 0252: a Codegraff login compacts on the gateway first++ // ADR 0261: both hosted paths require the existing experimental opt-in.++ const hosted = @import("gateway_compact.zig");++ if (hosted.hasLogin(self.io)) {++ if (try hosted.run(self)) |freed| return freed;++ } else if (@import("agent_clef_compact.zig").tryCompact(self)) |n| return n;const fork = @import("cache_fork.zig").begin(self); // ADR 0220: read the cached history, not rewrite itdefer @import("cache_fork.zig").end();const pending_tokens = compactPrelude(self) orelse {@@@ -472,8 -469,132 +475,140 @@@ pub fn capOversizedToolOutputs(self: *Areturn reclaimed;}++<<<<<<< HEAD+/// Re-exported recovery entry points (agent_compact_recover.zig owns them).+pub const emergencyTrim = @import("agent_compact_recover.zig").emergencyTrim;+pub const repeatedOpaqueCompactionFailure = @import("agent_compact_recover.zig").repeatedOpaqueCompactionFailure;+pub const repeatedEmptySummaryFailure = @import("agent_compact_recover.zig").repeatedEmptySummaryFailure;+pub const compactOrRecover = @import("agent_compact_recover.zig").compactOrRecover;++=======+ /// Last-resort context recovery when compact() itself can't run — typically+ /// because the history already overflows the window, so the summarization+ /// request overflows too and fails. Drops the oldest messages at a safe+ /// boundary; returns the count dropped (0 if none). Conservatively reduce the+ /// authoritative meter only by the locally measurable reclaimed tokens: hidden+ /// server-side reasoning may make the true reduction larger, but must never let+ /// a partial trim blind the next pre-send gate.+ pub fn emergencyTrim(self: *Agent) usize {+ if (emergencyCutIndex(self.messages.items)) |cut| {+ const before_tokens = self.fullInputEstimateTokens();+ var fresh = std.json.Array.init(self.arena);+ for (self.messages.items[cut..]) |m| fresh.append(m) catch return 0;+ self.messages = fresh;+ self.goal_note_fp = 0; // trimmed history may have carried the goal note (#318)+ self.history_rewrites +%= 1; // and the run's checklist gate's pasted copies (#318)+ // No synthetic message exists here to hang the standing state on (unlike+ // compact()'s handoff), so it rides the next turn's one-shot slot. Only+ // when that slot is free: a queued /goal replace|clear note is the USER's+ // instruction and outranks the harness restating itself (#318).+ if (self.pending_goal_note == null)+ self.pending_goal_note = goal_flow.compactionSnapshot(self.arena, self) catch null;+ const after_tokens = self.fullInputEstimateTokens();+ accountForReclaimedTokens(self, before_tokens -| after_tokens);+ return cut;+ }+ // #163: no clean user turn to cut at (a runaway tool loop). Don't wedge the+ // session — reclaim context by truncating the oldest tool outputs in place,+ // keeping every call/output pair valid. Nonzero = recovered. This too is a+ // rewrite: the stubbed outputs may include the last todo_write render the+ // suppressed run note points the model at (#318).+ if (trimOldestToolOutputs(self) > 0) {+ self.history_rewrites +%= 1;+ if (self.pending_goal_note == null)+ self.pending_goal_note = goal_flow.compactionSnapshot(self.arena, self) catch null;+ return 1;+ }+ return 0;+ }++ /// Auto-compaction with recovery. compact() summarizes the whole history in+ /// one request; once context overflows the window that request overflows too+ /// and fails — historically swallowed silently, wedging the session so every+ /// later turn failed at the same huge token count (issue #88). Surface the+ /// failure and, when `trim_on_fail`, emergency-trim so the next turn has+ /// room. Best-effort; never throws into the REPL loop.+ pub fn repeatedOpaqueCompactionFailure(self: *Agent, err: anyerror) bool {+ const opaque_transport = err == error.ApiError and self.last_request_write_failed;+ if (opaque_transport)+ self.compact_transport_failures +|= 1+ else+ self.compact_transport_failures = 0;+ const threshold = self.provider.compactAt();+ const effective = self.effectiveContextTokens();+ const locally_over_window = self.provider.context > 0 and self.fullRequestEstimateTokens() >= self.provider.context;+ return opaque_transport and+ threshold > 0 and+ self.compact_transport_failures >= 2 and+ (self.provider.nearContextLimit(effective) or locally_over_window);+ }++ /// #379: two consecutive COMPLETED-but-unusable summaries (empty or truncated)+ /// are provably not transport noise — the model, at this context size, is not+ /// going to produce one, and without escalation the over-cap history is+ /// re-shipped forever. Unlike compact_transport_failures this counter survives+ /// a complete response; only a usable summary (or a trim) resets it.+ pub fn repeatedEmptySummaryFailure(self: *Agent, err: anyerror) bool {+ const unusable = err == error.EmptySummary or err == error.IncompleteSummary;+ if (unusable) self.compact_summary_failures +|= 1 else self.compact_summary_failures = 0;+ const threshold = self.provider.compactAt();+ return unusable and threshold > 0 and+ self.compact_summary_failures >= 2 and+ self.effectiveContextTokens() >= threshold;+ }++ pub fn compactOrRecover(self: *Agent, trim_on_fail: bool) void {+ if (compact_cut.lastIsResolved(self.messages.items)) {+ self.compact_pin_degraded = false;+ compact_cut.resetStall(&self.compact_stall);+ }+ if (self.compact_pin_degraded and !trim_on_fail) return;+ const has_opaque = @import("compaction_window.zig").latestBlob(self.messages.items) != null;+ const result = if (has_opaque) server_compact.manualCompact(self) else self.compact();+ if (result) |_| {+ self.compact_transport_failures = 0;+ return;+ } else |err| {+ switch (err) {+ error.Interrupted => {+ self.compact_transport_failures = 0;+ return; // user hit Esc mid-compaction+ },+ error.EmptySummary, error.IncompleteSummary, error.ActivePromptPinned, error.CompactionOff => {}, // compact() already explained it+ else => {+ if (main_mod.json_mode)+ self.emit(.{ .type = "error", .message = std.fmt.allocPrint(self.arena, "auto-compaction failed: {s}", .{@errorName(err)}) catch "auto-compaction failed" })+ else+ self.say("[auto-compaction failed: {t}]\n", .{err}) catch {};+ },+ }+ if (has_opaque) return; // server failed: preserve its canonical window, never emergency-trim it+ const repeated_opaque_overflow = repeatedOpaqueCompactionFailure(self, err);+ const repeated_empty_summary = repeatedEmptySummaryFailure(self, err);+ // The caller's policy is computed before compact() makes its summary+ // request. Override it only for a concrete provider overflow rejection,+ // after two consecutive WriteFailed compaction attempts when the+ // effective meter is near 95% (or local bytes prove over-window), or+ // after two complete-but-unusable summaries while over compact@ (#379).+ // The first failure and ordinary transport outages preserve history.+ if (!trim_on_fail and !self.last_request_context_overflow and !repeated_opaque_overflow and !repeated_empty_summary) return;+ const dropped = self.emergencyTrim();+ if (dropped > 0) {+ self.compact_transport_failures = 0;+ self.compact_summary_failures = 0;+ // #445: a trim is the harsher half of the same boundary — the model+ // lost that history WITHOUT even a summary standing in for it, so+ // the transcript line is worth more here, not less. Hooked at this+ // call site rather than inside emergencyTrim() because the direct+ // emergencyTrim callers drive partially-initialized test agents.+ prompts.noteSessionCompacted(self, self.arena);+ @import("hot_context.zig").afterCompact(self);+ if (main_mod.json_mode)+ self.emit(.{ .type = "compact", .ok = true, .trimmed = dropped })+ else+ self.say("[context emergency-trimmed: dropped {d} old message(s) so the session can continue]\n", .{dropped}) catch {};+ } else if (!main_mod.json_mode) {+ self.say("[warning: context too large to compact and could not be trimmed safely]\n", .{}) catch {};+ }+ }+ }++>>>>>>> origin/feat/gateway-compactiondiff --cc src/gateway_compact.zigindex 00000000,7d6d0355..eddeec34mode 000000,100644..100644--- a/src/gateway_compact.zig+++ b/src/gateway_compact.zig@@@ -1,0 -1,312 +1,317 @@@-//! Hosted compaction (ADR 0252). With a persisted Codegraff login, compaction++//! Hosted compaction (ADR 0252, opt-in per ADR 0261). With++//! GRAFF_CLEF_COMPACT=1 and a persisted Codegraff login, compaction+ //! runs through the gateway's POST /v1/compact before graff's own summary: a+ //! decision model drops stale tool calls and results — text is never+ //! rewritten — and when that frees too little the gateway splices a summary of+ //! the middle in ("hybrid"). gateway_compact_wire.zig maps the history out and+ //! applies the reply back onto graff's own items by call id.+ //!+ //! Without a login nothing changes. A refused key or exhausted credits switch+ //! the hosted path off for the session (`on_credits_exhausted` decides what+ //! credits do next); any other failure — a 5xx, a timeout, a reply that does+ //! not check out — falls back to the local summary for that compaction only.+ //! A gateway problem can cost one compaction its quality, never the session+ //! its progress.++ const std = @import("std");+ const Io = std.Io;+ const Allocator = std.mem.Allocator;+ const Value = std.json.Value;++ const Agent = @import("agent.zig").Agent;+ const Provider = @import("provider.zig").Provider;+ const main_mod = @import("main.zig");+ const wire = @import("gateway_compact_wire.zig");+ const compact_cut = @import("compact_cut.zig");+ const tool_spill = @import("tool_spill.zig");+ const pricing = @import("pricing.zig");+ const acp_compaction = @import("acp_compaction.zig");++ pub const endpoint = "https://gateway.codegraff.com/v1/compact";+ const default_model = "clef";++ /// Bytes a trimmed tool result keeps: head and tail around a gap, plus the+ /// #409 pointer to the full output when the session is durable.+ const stub_cap: usize = 800;+ const stub_fallback = "[older tool output trimmed by compaction]";++ /// A reply must free at least this share of the estimated context, or the+ /// local summary runs instead: installing less would trigger compaction again+ /// on the next request and pay for it again.+ const min_freed_pct: u64 = 10;++ pub const CreditPolicy = enum {+ /// Keep compacting with the local summary; say so once.+ local,+ /// Stop compacting for the rest of the session.+ off,+ };+ /// What a refusal for credits (insufficient_credits, key_budget_exceeded) does.+ pub var on_credits_exhausted: CreditPolicy = .local;++ const Trip = enum(u8) { none, credits, auth };++ var g_mu: Io.Mutex = .init;+ var g_key: []const u8 = "";+ var g_url: []const u8 = endpoint;+ var g_model: []const u8 = default_model;+ var g_disabled = false;+ var g_trip = std.atomic.Value(Trip).init(.none);+ /// Bumped by every hosted compaction that installs, so `/compact` can tell+ /// whether the last line is about the compaction it just asked for.+ var g_seq = std.atomic.Value(u32).init(0);+ var g_line_buf: [160]u8 = undefined;+ var g_line_len: usize = 0;++ fn isOff(v: []const u8) bool {+ const t = std.mem.trim(u8, v, " \t");+ return std.mem.eql(u8, t, "0") or std.ascii.eqlIgnoreCase(t, "off") or+ std.ascii.eqlIgnoreCase(t, "false") or std.ascii.eqlIgnoreCase(t, "no");+ }++ /// GRAFF_HOSTED_COMPACT=0 keeps compaction local. GRAFF_COMPACT_URL and+ /// GRAFF_COMPACT_MODEL point it at another gateway or decision model.+ pub fn configure(arena: Allocator, env: anytype) !void {+ g_disabled = if (env.get("GRAFF_HOSTED_COMPACT")) |v| isOff(v) else false;+ g_url = if (env.get("GRAFF_COMPACT_URL")) |v| try arena.dupe(u8, v) else endpoint;+ g_model = if (env.get("GRAFF_COMPACT_MODEL")) |v| try arena.dupe(u8, v) else default_model;+ g_trip.store(.none, .release);+ }++ /// The persisted Codegraff login, the only credential this endpoint takes+ /// (the chat provider's key may belong to another vendor entirely). Null+ /// clears it. The slice must outlive the session.+ pub fn setLoginKey(io: Io, key: ?[]const u8) void {+ g_mu.lockUncancelable(io);+ defer g_mu.unlock(io);+ g_key = key orelse "";+ g_trip.store(.none, .release); // a new login gets a fresh chance+ }++ fn loginKey(io: Io) ?[]const u8 {+ g_mu.lockUncancelable(io);+ defer g_mu.unlock(io);+ return if (g_key.len > 0) g_key else null;+ }+++pub fn hasLogin(io: Io) bool {++ return loginKey(io) != null;++}+++ pub fn seq() u32 {+ return g_seq.load(.acquire);+ }++ /// What the latest installed hosted compaction did, copied into `out`.+ pub fn lastLine(io: Io, out: []u8) []const u8 {+ g_mu.lockUncancelable(io);+ defer g_mu.unlock(io);+ const n = @min(g_line_len, out.len);+ @memcpy(out[0..n], g_line_buf[0..n]);+ return out[0..n];+ }++ fn setLine(io: Io, line: []const u8) void {+ g_mu.lockUncancelable(io);+ defer g_mu.unlock(io);+ g_line_len = @min(line.len, g_line_buf.len);+ @memcpy(g_line_buf[0..g_line_len], line[0..g_line_len]);+ _ = g_seq.fetchAdd(1, .acq_rel);+ }++ const Stubber = struct {+ note: tool_spill.Note,++ pub fn stub(self: *const Stubber, a: Allocator, full: []const u8) []const u8 {+ if (full.len <= stub_cap) return full;+ const marker = self.note.text(a, full, stub_cap);+ const body = @import("output_elide.zig").headTail(a, full, stub_cap -| (marker.len + 1)) catch return marker;+ return std.fmt.allocPrint(a, "{s}\n{s}", .{ body, marker }) catch marker;+ }+ };++ fn goalText(self: *const Agent) ?[]const u8 {+ const g = self.goal orelse return null;+ return if (g.status == .complete) null else g.objective;+ }++ /// Called first by agent_compact.compact(). Null means "not handled here" —+ /// no login, the opt-out, a refusal earlier in the session, too little+ /// history to decide on, or a failure — and compact() then runs the local+ /// summary exactly as before.+ pub fn run(self: *Agent) anyerror!?usize {- if (g_disabled) return null;++ if (!@import("agent_clef_compact.zig").enabled() or g_disabled or self.review_mode) return null;+ const key = loginKey(self.io) orelse return null;+ switch (g_trip.load(.acquire)) {+ .none => {},+ .auth => return null,+ .credits => return if (on_credits_exhausted == .off) error.CompactionOff else null,+ }+ var scratch = std.heap.ArenaAllocator.init(self.gpa);+ defer scratch.deinit();+ const a = scratch.allocator();+ const msgs = try wire.transcript(a, self.messages.items);+ // The first message and the newest few are pinned: below this the+ // endpoint has nothing to decide, and above its cap it refuses.+ if (msgs.len <= wire.recent_kept + 1 or msgs.len > wire.max_messages) return null;+ const body = try wire.request(a, msgs, g_model, goalText(self));++ self.closeCodexWs(); // the history is about to be rewritten+ @import("agent_compact.zig").pinChildTask(self);+ if (!main_mod.json_mode and @import("repl.zig").g_debug) try self.say("[compacting {d} messages on the gateway…]\n", .{msgs.len});+ var progress = @import("compact_status.zig").begin(self);+ defer progress.end(self);+ var acp_run = acp_compaction.start(!self.sub and self.call_kind != .title);+ defer acp_run.finish(.failed, null, "hosted compaction did not complete"); // no-op once finished++ const provider: Provider = .{ .id = "codegraff", .kind = .openai, .auth = .bearer, .url = g_url, .api_key = key, .model = g_model, .context = 0 };+ const posted = @import("http.zig").postWatched(self.gpa, self.io, self.client, provider, body, null);+ progress.end(self); // the spinner stops before anything below prints+ const raw = posted catch |err| switch (err) {+ error.Interrupted, error.Canceled => {+ acp_run.finish(.cancelled, null, "interrupted");+ return error.Interrupted;+ },+ error.OutOfMemory => return err,+ else => {+ pricing.g_cost.failedWithoutUsage(self.io, 1);+ return fallback(self, &acp_run, @errorName(err));+ },+ };+ defer self.gpa.free(raw);+ const root = std.json.parseFromSliceLeaky(Value, a, raw, .{ .allocate = .alloc_always }) catch {+ pricing.g_cost.failedWithoutUsage(self.io, 1);+ return fallback(self, &acp_run, "unreadable reply");+ };+ if (refusal(root)) |code| return refused(self, &acp_run, code);+ noteUsage(self.io, root);+ const reply = wire.parseReply(a, root) catch |err| switch (err) {+ error.OutOfMemory => return err,+ else => return fallback(self, &acp_run, "unexpected reply"),+ };+ const outcome = install(self, a, msgs, &reply) catch |err| switch (err) {+ error.OutOfMemory => return err,+ else => return fallback(self, &acp_run, @errorName(err)),+ };+ acp_run.finish(.completed, if (reply.summarized) reply.summary else outcome.line, null);+ if (!main_mod.json_mode and @import("repl.zig").g_debug) try self.say("[{s}]\n", .{outcome.line});+ return outcome.freed;+ }++ fn fallback(self: *Agent, acp_run: *acp_compaction.Run, reason: []const u8) ?usize {+ if (self.tracer) |tr| tr.note("hosted_compact_fallback", reason);+ var buf: [128]u8 = undefined;+ const why = std.fmt.bufPrint(&buf, "hosted compaction unavailable ({s}); compacting locally", .{reason}) catch "hosted compaction unavailable; compacting locally";+ acp_run.finish(.failed, null, why);+ if (!main_mod.json_mode) self.say("[{s}]\n", .{why}) catch {};+ return null;+ }++ /// The gateway's error envelope code, if the reply is a refusal.+ pub fn refusal(root: Value) ?[]const u8 {+ if (root != .object) return null;+ const e = root.object.get("error") orelse return null;+ if (e != .object) return "error";+ const code = e.object.get("code") orelse e.object.get("type") orelse return "error";+ return if (code == .string) code.string else "error";+ }++ fn refused(self: *Agent, acp_run: *acp_compaction.Run, code: []const u8) anyerror!?usize {+ const eql = std.mem.eql;+ const credits = eql(u8, code, "insufficient_credits") or eql(u8, code, "key_budget_exceeded");+ const auth = eql(u8, code, "invalid_api_key") or eql(u8, code, "missing_api_key") or eql(u8, code, "insufficient_scope");+ if (!credits and !auth) return fallback(self, acp_run, code);+ g_trip.store(if (credits) .credits else .auth, .release);+ if (self.tracer) |tr| tr.note("hosted_compact_off", code);+ const off = credits and on_credits_exhausted == .off;+ const why = if (off)+ "compaction is off: Codegraff credits are used up — top up to turn it back on"+ else if (credits)+ "Codegraff credits are used up: compacting locally (a lossy summary) until you top up"+ else+ "hosted compaction unavailable (Codegraff key refused); compacting locally";+ acp_run.finish(.failed, null, why);+ if (!main_mod.json_mode) self.say("[{s}]\n", .{why}) catch {};+ return if (off) error.CompactionOff else null;+ }++ fn noteUsage(io: Io, root: Value) void {+ const usage = if (root == .object) root.object.get("usage") orelse Value.null else Value.null;+ if (usage == .object) {+ const input = count(usage.object.get("input_tokens"));+ const output = count(usage.object.get("output_tokens"));+ if (input != null and output != null) {+ if (@import("pricing_gateway.zig").settledCharge(root)) |charge|+ pricing.g_cost.addSettled(io, input.?, output.?, charge)+ else+ pricing.g_cost.addForProvider(io, .unpriced, "codegraff", g_model, input.?, 0, 0, output.?);+ return;+ }+ }+ pricing.g_cost.missingUsage(io);+ }++ fn count(v: ?Value) ?i64 {+ const n = v orelse return null;+ return if (n == .integer and n.integer >= 0) n.integer else null;+ }++ fn estimate(items: []const Value) u64 {+ var total: u64 = 0;+ for (items) |m| total +|= @import("context_tokens.zig").estimatedTokens(m);+ return total;+ }++ const Outcome = struct { freed: usize, line: []const u8 };++ fn install(self: *Agent, a: Allocator, msgs: []const wire.Msg, reply: *const wire.Reply) !Outcome {+ const items = self.messages.items;+ const p = try wire.plan(a, msgs, reply, compact_cut.pinOpening(items));+ var note: ?Value = null;+ if (reply.summarized) {+ const base = try @import("agent_compact.zig").rootHandoff(self, reply.summary);+ const standing = try @import("goal_flow.zig").compactionSnapshot(self.arena, self);+ const discarded = try wire.replaced(a, items, msgs, p);+ const text = try @import("compact_handoff_note.zig").handoff(self.arena, self, base, standing, discarded);+ note = try @import("messages.zig").userNote(self.arena, self.provider.kind, text);+ }+ const stubber: Stubber = .{ .note = .{ .fallback = stub_fallback, .session = tool_spill.sessionFor(self.sub, self.session_name) } };+ // Earlier turns lose dropped calls outright; the live turn keeps them.+ const removable = compact_cut.turnOpeningUserIndex(items) orelse 0;+ const fresh = try wire.apply(self.arena, items, msgs, p, &reply.decisions, removable, note, &stubber);+ try wire.checkPairs(a, items, fresh.items);+ const before = estimate(items);+ const after = estimate(fresh.items);+ if (after * 100 > before * (100 - min_freed_pct)) return error.TooLittleFreed;+ const line = try outcomeLine(self.arena, reply, p);++ self.messages = fresh;+ self.compact_summary_failures = 0;+ self.last_context_tokens = 0;+ self.context_local_tokens = 0;+ if (reply.summarized) self.goal_note_fp = 0; // the injected goal note may be in the summarized middle (#318)+ self.history_rewrites +%= 1; // trimmed results may hold pasted state, e.g. a todo_write render (#318)+ @import("prompts.zig").noteSessionCompacted(self, self.arena);+ @import("hot_context.zig").afterCompact(self);+ const counters = @import("agent_server_compact.zig");+ if (reply.summarized) counters.session_summaries +|= 1 else counters.session_prunes +|= 1;+ setLine(self.io, line);+ if (self.tracer) |tr| tr.note("hosted_compact", line);+ return .{ .freed = @intCast(before - after), .line = line };+ }++ fn outcomeLine(a: Allocator, reply: *const wire.Reply, p: wire.Plan) ![]const u8 {+ var trimmed: usize = 0;+ var dropped: usize = 0;+ var it = reply.decisions.valueIterator();+ while (it.next()) |action| switch (action.*) {+ .drop_result => trimmed += 1,+ .drop_call => dropped += 1,+ .keep => {},+ };+ if (!reply.summarized)+ return std.fmt.allocPrint(a, "compacted on the gateway: trimmed {d} tool results, dropped {d} calls", .{ trimmed, dropped });+ var summarized: usize = 0;+ for (p.keep) |k| summarized += @intFromBool(!k);+ return std.fmt.allocPrint(a, "compacted on the gateway: summarized {d} earlier messages, trimmed {d} tool results, dropped {d} calls", .{ summarized, trimmed, dropped });+ }diff --cc src/gateway_compact_tests.zigindex 00000000,f1adc21e..f8529443mode 000000,100644..100644--- a/src/gateway_compact_tests.zig+++ b/src/gateway_compact_tests.zig@@@ -1,0 -1,328 +1,339 @@@+ //! Hosted compaction (ADR 0252) on fixtures: every wire format maps to the+ //! gateway's neutral transcript, and a reply applies back onto the native+ //! items — trimmed results, dropped calls, a spliced summary — without ever+ //! leaving a result without its call.++ const std = @import("std");+ const Allocator = std.mem.Allocator;+ const Value = std.json.Value;+ const wire = @import("gateway_compact_wire.zig");+ const hosted = @import("gateway_compact.zig");++ const TestStub = struct {+ pub fn stub(_: *const TestStub, a: Allocator, full: []const u8) []const u8 {+ if (full.len <= 8) return full;+ return std.fmt.allocPrint(a, "{s}…[trimmed]", .{full[0..8]}) catch full;+ }+ };++ fn parse(a: Allocator, raw: []const u8) ![]const Value {+ const v = try std.json.parseFromSliceLeaky(Value, a, raw, .{ .allocate = .alloc_always });+ return v.array.items;+ }++ fn field(v: Value, key: []const u8) Value {+ return v.object.get(key) orelse .null;+ }++ test "ADR 0252: Anthropic history folds into neutral messages with calls, results and errors" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const a = state.allocator();+ const items = try parse(a,+ \\[{"role":"user","content":"Fix the failing test"},+ \\ {"role":"assistant","content":[{"type":"thinking","thinking":"plan","signature":"s"},{"type":"text","text":"Reading it"},{"type":"tool_use","id":"t1","name":"read_file","input":{"path":"a.zig"}}]},+ \\ {"role":"user","content":[{"type":"tool_result","tool_use_id":"t1","content":"file body"}]},+ \\ {"role":"assistant","content":[{"type":"tool_use","id":"t2","name":"bash","input":{"cmd":"zig build test"}}]},+ \\ {"role":"user","content":[{"type":"tool_result","tool_use_id":"t2","content":[{"type":"text","text":"1 failed"}],"is_error":true},{"type":"text","text":"also check b"}]},+ \\ {"role":"assistant","content":"Done"}]+ );+ const msgs = try wire.transcript(a, items);+ try std.testing.expectEqual(@as(usize, 6), msgs.len);+ try std.testing.expectEqual(wire.Role.assistant, msgs[1].role);+ try std.testing.expectEqualStrings("Reading it", msgs[1].text);+ try std.testing.expectEqualStrings("t1", msgs[1].uses[0].id);+ try std.testing.expectEqualStrings("a.zig", field(msgs[1].uses[0].input, "path").string);+ try std.testing.expectEqualStrings("file body", msgs[2].results[0].text);+ try std.testing.expectEqualStrings("also check b", msgs[4].text);+ try std.testing.expectEqualStrings("1 failed", msgs[4].results[0].text);+ try std.testing.expect(msgs[4].results[0].is_error);+ for (msgs, 0..) |m, i| try std.testing.expectEqual(i, m.lo);+ }++ test "ADR 0252: Chat tool messages fold into one user message; unparsable arguments are wrapped" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const a = state.allocator();+ const items = try parse(a,+ \\[{"role":"user","content":"go"},+ \\ {"role":"assistant","content":null,"tool_calls":[{"id":"c1","type":"function","function":{"name":"bash","arguments":"{\"cmd\":\"ls\"}"}},{"id":"c2","type":"function","function":{"name":"grep","arguments":"not json"}}]},+ \\ {"role":"tool","tool_call_id":"c1","content":"a\nb"},+ \\ {"role":"tool","tool_call_id":"c2","content":"[error] bad pattern"},+ \\ {"role":"assistant","content":"ok"}]+ );+ const msgs = try wire.transcript(a, items);+ try std.testing.expectEqual(@as(usize, 4), msgs.len);+ try std.testing.expectEqualStrings("ls", field(msgs[1].uses[0].input, "cmd").string);+ try std.testing.expectEqualStrings("not json", field(msgs[1].uses[1].input, "arguments").string);+ try std.testing.expectEqual(@as(usize, 2), msgs[2].lo);+ try std.testing.expectEqual(@as(usize, 4), msgs[2].hi);+ try std.testing.expectEqual(@as(usize, 2), msgs[2].results.len);+ try std.testing.expect(!msgs[2].results[0].is_error);+ try std.testing.expect(msgs[2].results[1].is_error);+ }++ test "ADR 0252: Responses and Interactions items fold the same way; reasoning joins the model's side" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const a = state.allocator();+ const responses = try wire.transcript(a, try parse(a,+ \\[{"role":"user","content":"go"},+ \\ {"type":"reasoning","encrypted_content":"x","summary":[]},+ \\ {"type":"function_call","call_id":"f1","name":"bash","arguments":"{\"cmd\":\"ls\"}"},+ \\ {"type":"function_call_output","call_id":"f1","output":"listing"},+ \\ {"type":"message","role":"assistant","content":[{"type":"output_text","text":"done"}]}]+ ));+ try std.testing.expectEqual(@as(usize, 4), responses.len);+ try std.testing.expectEqual(@as(usize, 1), responses[1].lo);+ try std.testing.expectEqual(@as(usize, 3), responses[1].hi);+ try std.testing.expectEqualStrings("f1", responses[2].results[0].id);+ try std.testing.expectEqualStrings("done", responses[3].text);+ const interactions = try wire.transcript(a, try parse(a,+ \\[{"type":"user_input","content":"go"},+ \\ {"type":"thought","signature":"s"},+ \\ {"type":"function_call","id":"g1","name":"bash","arguments":{"cmd":"ls"}},+ \\ {"type":"function_result","call_id":"g1","name":"bash","result":[{"type":"text","text":"listing"}]},+ \\ {"type":"model_output","content":[{"type":"text","text":"done"}]}]+ ));+ try std.testing.expectEqual(@as(usize, 4), interactions.len);+ try std.testing.expectEqualStrings("go", interactions[0].text);+ try std.testing.expectEqualStrings("ls", field(interactions[1].uses[0].input, "cmd").string);+ try std.testing.expectEqualStrings("listing", interactions[2].results[0].text);+ }++ test "ADR 0252: the request names the model, hybrid mode, the goal and every call by id" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const a = state.allocator();+ const msgs = try wire.transcript(a, try parse(a,+ \\[{"role":"user","content":"go"},+ \\ {"role":"assistant","content":[{"type":"tool_use","id":"t1","name":"read_file","input":{"path":"a.zig"}}]},+ \\ {"role":"user","content":[{"type":"tool_result","tool_use_id":"t1","content":"body","is_error":true}]}]+ ));+ const body = try wire.request(a, msgs, "clef", "ship the fix");+ const v = try std.json.parseFromSliceLeaky(Value, a, body, .{});+ try std.testing.expectEqualStrings("clef", field(v, "model").string);+ try std.testing.expectEqualStrings("hybrid", field(v, "mode").string);+ try std.testing.expectEqualStrings("ship the fix", field(v, "goal").string);+ const sent = field(v, "messages").array.items;+ try std.testing.expectEqualStrings("assistant", field(sent[1], "role").string);+ const use = field(sent[1], "toolUses").array.items[0];+ try std.testing.expectEqualStrings("t1", field(use, "tool_use_id").string);+ try std.testing.expectEqualStrings("a.zig", field(field(use, "input"), "path").string);+ const result = field(sent[2], "toolResults").array.items[0];+ try std.testing.expect(field(result, "isError").bool);+ }++ /// Two finished turns' worth of Anthropic history: t1/t2 in the earlier turn,+ /// t3/t4 in the live one (it opens at item 6).+ const two_turns =+ \\[{"role":"user","content":"first prompt"},+ \\ {"role":"assistant","content":[{"type":"text","text":"look"},{"type":"tool_use","id":"t1","name":"read_file","input":{"path":"a"}}]},+ \\ {"role":"user","content":[{"type":"tool_result","tool_use_id":"t1","content":"aaaaaaaaaaaaaaaaaaaaaaaa"}]},+ \\ {"role":"assistant","content":[{"type":"tool_use","id":"t2","name":"grep","input":{"q":"x"}}]},+ \\ {"role":"user","content":[{"type":"tool_result","tool_use_id":"t2","content":"bbbbbbbbbbbbbbbbbbbbbbbb"}]},+ \\ {"role":"assistant","content":"answer one"},+ \\ {"role":"user","content":"second prompt"},+ \\ {"role":"assistant","content":[{"type":"tool_use","id":"t3","name":"bash","input":{"cmd":"ls"}}]},+ \\ {"role":"user","content":[{"type":"tool_result","tool_use_id":"t3","content":"cccccccccccccccccccccccc"}]},+ \\ {"role":"assistant","content":[{"type":"tool_use","id":"t4","name":"bash","input":{"cmd":"pwd"}}]},+ \\ {"role":"user","content":[{"type":"tool_result","tool_use_id":"t4","content":"short"}]},+ \\ {"role":"assistant","content":"answer two"}]+ ;++ /// The endpoint's transcript for two_turns after t1 drop_result, t2 and t3+ /// drop_call, t4 keep: messages holding only dropped calls are gone.+ const pruned_reply =+ \\{"messages":[+ \\ {"role":"user","text":"first prompt","toolUses":[],"toolResults":[]},+ \\ {"role":"assistant","text":"look","toolUses":[{"tool_use_id":"t1","tool":"read_file","input":{"path":"a"}}],"toolResults":[]},+ \\ {"role":"user","text":"","toolUses":[],"toolResults":[{"tool_use_id":"t1","text":"aaa…[truncated]"}]},+ \\ {"role":"assistant","text":"answer one","toolUses":[],"toolResults":[]},+ \\ {"role":"user","text":"second prompt","toolUses":[],"toolResults":[]},+ \\ {"role":"assistant","text":"","toolUses":[{"tool_use_id":"t4","tool":"bash","input":{"cmd":"pwd"}}],"toolResults":[]},+ \\ {"role":"user","text":"","toolUses":[],"toolResults":[{"tool_use_id":"t4","text":"short"}]},+ \\ {"role":"assistant","text":"answer two","toolUses":[],"toolResults":[]}],+ \\ "decisions":[{"id":"t1","tool":"read_file","tool_use_id":"t1","action":"drop_result"},+ \\ {"id":"t2","tool":"grep","tool_use_id":"t2","action":"drop_call"},+ \\ {"id":"t3","tool":"bash","tool_use_id":"t3","action":"drop_call"},+ \\ {"id":"t4","tool":"bash","tool_use_id":"t4","action":"keep"}],+ \\ "stats":{"summarized":false}}+ ;++ test "ADR 0252: earlier turns lose dropped calls; the live turn keeps its calls and trims their results" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const a = state.allocator();+ const items = try parse(a, two_turns);+ const msgs = try wire.transcript(a, items);+ const reply = try wire.parseReply(a, try std.json.parseFromSliceLeaky(Value, a, pruned_reply, .{ .allocate = .alloc_always }));+ try std.testing.expect(!reply.summarized);+ const p = try wire.plan(a, msgs, &reply, null);+ const stubber: TestStub = .{};+ const fresh = try wire.apply(a, items, msgs, p, &reply.decisions, 6, null, &stubber);+ // t2's pair (items 3, 4) is gone; t3 (live turn) keeps its call.+ try std.testing.expectEqual(@as(usize, 10), fresh.items.len);+ const t1 = field(fresh.items[2], "content").array.items[0];+ try std.testing.expectEqualStrings("aaaaaaaa…[trimmed]", field(t1, "content").string);+ try std.testing.expectEqualStrings("answer one", field(fresh.items[3], "content").string);+ try std.testing.expectEqualStrings("t3", field(field(fresh.items[5], "content").array.items[0], "id").string);+ const t3 = field(fresh.items[6], "content").array.items[0];+ try std.testing.expectEqualStrings("cccccccc…[trimmed]", field(t3, "content").string);+ try std.testing.expectEqualStrings("short", field(field(fresh.items[8], "content").array.items[0], "content").string);+ // The live history was not touched: the fresh one shares untouched items.+ try std.testing.expectEqualStrings("aaaaaaaaaaaaaaaaaaaaaaaa", field(field(items[2], "content").array.items[0], "content").string);+ try wire.checkPairs(a, items, fresh.items);+ }++ test "ADR 0252: a reply that does not match what its decisions imply is refused" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const a = state.allocator();+ const items = try parse(a, two_turns);+ const msgs = try wire.transcript(a, items);+ // The endpoint must never rewrite text: an edited message fails the check.+ const edited = try std.mem.replaceOwned(u8, a, pruned_reply, "answer one", "answer 1");+ const reply = try wire.parseReply(a, try std.json.parseFromSliceLeaky(Value, a, edited, .{ .allocate = .alloc_always }));+ try std.testing.expectError(error.UnexpectedReply, wire.plan(a, msgs, &reply, null));+ // So does a decision list that disagrees with the transcript it came with.+ const kept = try std.mem.replaceOwned(u8, a, pruned_reply, "\"action\":\"drop_call\"}", "\"action\":\"keep\"}");+ const mismatch = try wire.parseReply(a, try std.json.parseFromSliceLeaky(Value, a, kept, .{ .allocate = .alloc_always }));+ try std.testing.expectError(error.UnexpectedReply, wire.plan(a, msgs, &mismatch, null));+ }++ /// Responses history: a task, two earlier calls and a reply, then a new prompt+ /// at item 6 and three calls in the live turn.+ const long_turn =+ \\[{"role":"user","content":"task"},+ \\ {"type":"function_call","call_id":"a1","name":"bash","arguments":"{}"},+ \\ {"type":"function_call_output","call_id":"a1","output":"one"},+ \\ {"type":"function_call","call_id":"a2","name":"bash","arguments":"{}"},+ \\ {"type":"function_call_output","call_id":"a2","output":"two"},+ \\ {"type":"message","role":"assistant","content":[{"type":"output_text","text":"progress"}]},+ \\ {"role":"user","content":"now do X"},+ \\ {"type":"function_call","call_id":"a3","name":"bash","arguments":"{}"},+ \\ {"type":"function_call_output","call_id":"a3","output":"three"},+ \\ {"type":"function_call","call_id":"a4","name":"bash","arguments":"{}"},+ \\ {"type":"function_call_output","call_id":"a4","output":"four"},+ \\ {"type":"function_call","call_id":"a5","name":"bash","arguments":"{}"},+ \\ {"type":"function_call_output","call_id":"a5","output":"five"}+ ;++ fn summarizedReply(a: Allocator, msgs: []const wire.Msg, kept: usize) !wire.Reply {+ var out: std.Io.Writer.Allocating = .init(a);+ const w = &out.writer;+ try w.writeAll("{\"stats\":{\"summarized\":true},\"decisions\":[],\"messages\":[");+ const neutral = try std.json.parseFromSliceLeaky(Value, a, try wire.request(a, msgs, "clef", null), .{ .allocate = .alloc_always });+ const sent = field(neutral, "messages").array.items;+ try std.json.Stringify.value(sent[0], .{}, w);+ try w.writeAll(",{\"role\":\"user\",\"text\":\"[compacted summary of 6 earlier messages]\\nRan two checks.\",\"toolUses\":[]}");+ for (sent[sent.len - kept ..]) |m| {+ try w.writeByte(',');+ try std.json.Stringify.value(m, .{}, w);+ }+ try w.writeAll("]}");+ return wire.parseReply(a, try std.json.parseFromSliceLeaky(Value, a, out.writer.buffered(), .{ .allocate = .alloc_always }));+ }++ test "ADR 0252: a summary keeps the unresolved turn's opening prompt and never orphans a result" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const a = state.allocator();+ // Unresolved (it ends on a tool result): the prompt at item 6 must survive.+ const unresolved = try parse(a, long_turn ++ "]");+ const msgs = try wire.transcript(a, unresolved);+ try std.testing.expectEqual(@as(usize, 13), msgs.len);+ const reply = try summarizedReply(a, msgs, wire.recent_kept);+ try std.testing.expectEqualStrings("Ran two checks.", reply.summary);+ const p = try wire.plan(a, msgs, &reply, 6);+ const stubber: TestStub = .{};+ const note = try @import("messages.zig").userNote(a, .responses, "SUMMARY");+ const fresh = try wire.apply(a, unresolved, msgs, p, &reply.decisions, 6, note, &stubber);+ try std.testing.expectEqualStrings("task", field(fresh.items[0], "content").string);+ try std.testing.expectEqualStrings("now do X", field(fresh.items[2], "content").string);+ try std.testing.expectEqual(@as(usize, 2 + 7), fresh.items.len); // task, summary, prompt, a3..a5 pairs+ try wire.checkPairs(a, unresolved, fresh.items);+ try std.testing.expectEqual(@as(usize, 5), (try wire.replaced(a, unresolved, msgs, p)).len); // items 1-5++ // Resolved by a reply: the newest six now open on a3's RESULT, so the cut+ // moves back over a3's call instead of shipping an orphan.+ const resolved = try parse(a, long_turn +++ \\,{"type":"message","role":"assistant","content":[{"type":"output_text","text":"done"}]}]+ );+ const msgs2 = try wire.transcript(a, resolved);+ const reply2 = try summarizedReply(a, msgs2, wire.recent_kept);+ const p2 = try wire.plan(a, msgs2, &reply2, null);+ const fresh2 = try wire.apply(a, resolved, msgs2, p2, &reply2.decisions, 6, note, &stubber);+ try std.testing.expectEqualStrings("a3", field(fresh2.items[2], "call_id").string);+ try std.testing.expectEqualStrings("function_call", field(fresh2.items[2], "type").string);+ try wire.checkPairs(a, resolved, fresh2.items);++ // An endpoint that moves its own cut back over the pair keeps seven+ // newest messages; the same history results.+ const reply3 = try summarizedReply(a, msgs2, wire.recent_kept + 1);+ const p3 = try wire.plan(a, msgs2, &reply3, null);+ const fresh3 = try wire.apply(a, resolved, msgs2, p3, &reply3.decisions, 6, note, &stubber);+ try std.testing.expectEqual(fresh2.items.len, fresh3.items.len);+ try std.testing.expectEqualStrings("a3", field(fresh3.items[2], "call_id").string);++ // A summary must retain at least the six most recent messages.++ for (1..wire.recent_kept) |kept| {++ try std.testing.expectError(error.UnexpectedReply, wire.plan(a, msgs2, &(try summarizedReply(a, msgs2, kept)), null));++ }+ // Too few left to summarize: refused, not misread.+ try std.testing.expectError(error.UnexpectedReply, wire.plan(a, msgs2, &(try summarizedReply(a, msgs2, msgs2.len - 2)), null));+ }++ test "ADR 0252: a history that pairs worse than the one it replaces is refused" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const a = state.allocator();+ const before = try parse(a,+ \\[{"role":"user","content":"go"},+ \\ {"type":"function_call","call_id":"f1","name":"bash","arguments":"{}"},+ \\ {"type":"function_call_output","call_id":"f1","output":"ok"}]+ );+ try std.testing.expectError(error.UnpairedToolCall, wire.checkPairs(a, before, &.{ before[0], before[2] }));+ try std.testing.expectError(error.UnpairedToolCall, wire.checkPairs(a, before, before[0..2]));+ // An unanswered call the session already had is not this compaction's doing.+ try wire.checkPairs(a, before[0..2], before[0..2]);+ }++ test "ADR 0252: refusals are read from the gateway's error envelope" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const a = state.allocator();+ const credits = try std.json.parseFromSliceLeaky(Value, a, "{\"error\":{\"message\":\"m\",\"type\":\"insufficient_credits\",\"code\":\"insufficient_credits\"}}", .{});+ try std.testing.expectEqualStrings("insufficient_credits", hosted.refusal(credits).?);+ const ok = try std.json.parseFromSliceLeaky(Value, a, "{\"messages\":[],\"decisions\":[]}", .{});+ try std.testing.expect(hosted.refusal(ok) == null);+ }+-test "ADR 0252: the opt-out and a missing login leave compaction local" {++test "ADR 0252: the default, opt-out and a missing login leave compaction local" {++ const clef = @import("agent_clef_compact.zig");++ const was_enabled = clef.g_enabled;++ defer clef.g_enabled = was_enabled;+ const Env = struct {+ off: bool,+ pub fn get(self: @This(), key: []const u8) ?[]const u8 {+ return if (self.off and std.mem.eql(u8, key, "GRAFF_HOSTED_COMPACT")) "0" else null;+ }+ };+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ const a = state.allocator();+ defer hosted.setLoginKey(std.testing.io, null);+ defer hosted.configure(a, Env{ .off = false }) catch {};+ var agent = @import("agent_compact_test_support.zig").subAgent(a, false);+ agent.io = std.testing.io;+ agent.messages = std.json.Array.init(a);+ try hosted.configure(a, Env{ .off = false });++ clef.g_enabled = false;++ hosted.setLoginKey(std.testing.io, "cg_sk_synthetic");++ try std.testing.expect(try hosted.run(&agent) == null); // a login alone never opts in++ clef.g_enabled = true;+ hosted.setLoginKey(std.testing.io, null);+ try std.testing.expect(try hosted.run(&agent) == null); // no login: nothing sent+ try hosted.configure(a, Env{ .off = true });+ hosted.setLoginKey(std.testing.io, "cg_sk_synthetic");+ try std.testing.expect(try hosted.run(&agent) == null); // opted out+ }diff --cc src/gateway_compact_wire.zigindex 00000000,85ec52d7..6713a497mode 000000,100644..100644--- a/src/gateway_compact_wire.zig+++ b/src/gateway_compact_wire.zig@@@ -1,0 -1,570 +1,570 @@@+ //! The wire half of hosted compaction (ADR 0252): graff's provider-shaped+ //! history to the gateway's neutral transcript, and the reply back onto it.+ //! Pure — no Agent, no network — so every wire format's mapping is pinned by+ //! fixtures (gateway_compact_tests.zig).+ //!+ //! Out: consecutive items of one side fold into one neutral message — user+ //! prompts, harness notes and tool results on one side; assistant text, calls+ //! and reasoning on the other — and each message remembers the native range+ //! it came from. In: the endpoint's per-call decisions are applied to the+ //! NATIVE items by call id. Nothing is rebuilt from the neutral copy, so+ //! reasoning, images and provider metadata stay as they were; a hybrid summary+ //! replaces only the native range it summarized.++ const std = @import("std");+ const Allocator = std.mem.Allocator;+ const Value = std.json.Value;+ const ObjectMap = std.json.ObjectMap;++ const sanitizeUtf8 = @import("messages.zig").sanitizeUtf8;++ /// The endpoint's own cap on one transcript.+ pub const max_messages = 2000;+ /// Newest messages the hybrid summary keeps verbatim: the endpoint's default,+ /// which its summary step applies whatever the request says.+ pub const recent_kept = 6;++ pub const Role = enum { user, assistant };+ pub const Use = struct { id: []const u8, name: []const u8, input: Value = .null };+ pub const Result = struct { id: []const u8, text: []const u8 = "", is_error: bool = false };++ pub const Msg = struct {+ role: Role,+ /// The native items [lo, hi) this message folds.+ lo: usize,+ hi: usize,+ text: []const u8 = "",+ uses: []const Use = &.{},+ results: []const Result = &.{},+ };++ const Builder = struct {+ a: Allocator,+ /// Call/result ids only: no text, no argument parsing (pair checks).+ ids_only: bool = false,+ out: std.ArrayList(Msg) = .empty,+ open: ?Msg = null,+ text: std.ArrayList(u8) = .empty,+ uses: std.ArrayList(Use) = .empty,+ results: std.ArrayList(Result) = .empty,++ fn close(b: *Builder) !void {+ var m = b.open orelse return;+ m.text = try b.text.toOwnedSlice(b.a);+ m.uses = try b.uses.toOwnedSlice(b.a);+ m.results = try b.results.toOwnedSlice(b.a);+ try b.out.append(b.a, m);+ b.open = null;+ }++ fn addText(b: *Builder, s: []const u8) !void {+ if (b.ids_only or s.len == 0) return;+ if (b.text.items.len > 0) try b.text.append(b.a, '\n');+ try b.text.appendSlice(b.a, sanitizeUtf8(b.a, s));+ }++ fn use(b: *Builder, id: ?[]const u8, name: ?[]const u8, input: ?Value, wrap_as: []const u8) !void {+ const call_id = id orelse return;+ const args: Value = if (b.ids_only) .null else try argsObject(b.a, input, wrap_as);+ try b.uses.append(b.a, .{ .id = call_id, .name = name orelse "tool", .input = args });+ }++ fn result(b: *Builder, id: ?[]const u8, payload: ?Value, is_error: bool) !void {+ const call_id = id orelse return;+ const text = if (b.ids_only) "" else sanitizeUtf8(b.a, try textOf(b.a, payload orelse .null));+ try b.results.append(b.a, .{ .id = call_id, .text = text, .is_error = is_error });+ }+ };++ fn str(o: ObjectMap, key: []const u8) ?[]const u8 {+ const v = o.get(key) orelse return null;+ return if (v == .string) v.string else null;+ }++ fn eql(a: []const u8, b: []const u8) bool {+ return std.mem.eql(u8, a, b);+ }++ fn errorFlag(o: ObjectMap) bool {+ const v = o.get("is_error") orelse o.get("isError") orelse return false;+ return v == .bool and v.bool;+ }++ /// Which side an item speaks for. Typed items (Responses, Interactions) are+ /// the model's unless they carry a result or a prompt; role messages follow+ /// their role, with tool results and harness notes on the user side.+ fn itemRole(item: Value) ?Role {+ if (item != .object) return null;+ const o = item.object;+ if (str(o, "type")) |t| {+ if (eql(t, "function_call_output") or eql(t, "custom_tool_call_output") or+ eql(t, "function_result") or eql(t, "user_input")) return .user;+ if (!eql(t, "message")) return .assistant; // calls, reasoning, thought, model_output, opaque state+ }+ const role = str(o, "role") orelse return null;+ return if (eql(role, "assistant")) .assistant else .user;+ }++ /// Joined text of a string, a text-bearing object, or an array of parts.+ /// Images and other non-text parts contribute nothing.+ pub fn textOf(a: Allocator, v: Value) ![]const u8 {+ switch (v) {+ .string => |s| return s,+ .object => |o| return str(o, "text") orelse "",+ .array => |arr| {+ var out: std.ArrayList(u8) = .empty;+ for (arr.items) |part| {+ const t = switch (part) {+ .string => |s| s,+ .object => |o| str(o, "text") orelse "",+ else => "",+ };+ if (t.len == 0) continue;+ if (out.items.len > 0) try out.append(a, '\n');+ try out.appendSlice(a, t);+ }+ return out.items;+ },+ else => return "",+ }+ }++ /// The endpoint takes a call's input as a JSON object: Chat/Responses carry a+ /// string of JSON, a freeform tool a raw string, and either may not parse.+ fn argsObject(a: Allocator, v: ?Value, wrap_as: []const u8) !Value {+ const raw = v orelse return .{ .object = .empty };+ switch (raw) {+ .object => return raw,+ .null => return .{ .object = .empty },+ .string => |s| {+ if (std.json.parseFromSliceLeaky(Value, a, s, .{ .allocate = .alloc_always })) |parsed| {+ if (parsed == .object) return parsed;+ } else |_| {}+ },+ else => {},+ }+ var o: ObjectMap = .empty;+ try o.put(a, wrap_as, raw);+ return .{ .object = o };+ }++ fn absorbBlock(b: *Builder, block: Value) !void {+ if (block == .string) return b.addText(block.string);+ if (block != .object) return;+ const o = block.object;+ const t = str(o, "type") orelse return;+ if (eql(t, "text") or eql(t, "input_text") or eql(t, "output_text")) return b.addText(str(o, "text") orelse "");+ if (eql(t, "tool_use")) return b.use(str(o, "id"), str(o, "name"), o.get("input"), "input");+ if (eql(t, "tool_result")) return b.result(str(o, "tool_use_id"), o.get("content"), errorFlag(o));+ }++ fn absorb(b: *Builder, item: Value) !void {+ if (item != .object) return;+ const o = item.object;+ if (str(o, "type")) |t| {+ if (eql(t, "function_call")) return b.use(str(o, "call_id") orelse str(o, "id"), str(o, "name"), o.get("arguments"), "arguments");+ if (eql(t, "custom_tool_call")) return b.use(str(o, "call_id"), str(o, "name"), o.get("input"), "input");+ if (eql(t, "function_call_output") or eql(t, "custom_tool_call_output")) return b.result(str(o, "call_id"), o.get("output"), false);+ if (eql(t, "function_result")) return b.result(str(o, "call_id"), o.get("result"), errorFlag(o));+ if (eql(t, "user_input") or eql(t, "model_output") or eql(t, "message")) {+ if (b.ids_only) return;+ return b.addText(try textOf(b.a, o.get("content") orelse .null));+ }+ return; // reasoning, thought, opaque provider state: nothing readable+ }+ const role = str(o, "role") orelse return;+ if (eql(role, "tool")) {+ // OpenAI Chat has no error bit; graff writes failures with this prefix.+ const content = o.get("content") orelse Value.null;+ const failed = content == .string and std.mem.startsWith(u8, content.string, "[error] ");+ return b.result(str(o, "tool_call_id"), content, failed);+ }+ if (o.get("content")) |content| switch (content) {+ .string => |s| try b.addText(s),+ .array => |arr| for (arr.items) |block| try absorbBlock(b, block),+ else => {},+ };+ if (o.get("tool_calls")) |calls| if (calls == .array) for (calls.array.items) |call| {+ if (call != .object) continue;+ const f = call.object.get("function") orelse continue;+ if (f != .object) continue;+ try b.use(str(call.object, "id"), str(f.object, "name"), f.object.get("arguments"), "arguments");+ };+ }++ fn fold(a: Allocator, items: []const Value, ids_only: bool) ![]const Msg {+ var b: Builder = .{ .a = a, .ids_only = ids_only };+ for (items, 0..) |item, i| {+ const role = itemRole(item) orelse if (b.open) |m| m.role else Role.user;+ if (b.open == null or b.open.?.role != role) {+ try b.close();+ b.open = .{ .role = role, .lo = i, .hi = i };+ }+ b.open.?.hi = i + 1;+ try absorb(&b, item);+ }+ try b.close();+ return b.out.items;+ }++ /// Native history → neutral messages. The ranges are contiguous and cover+ /// every item, so each native item belongs to exactly one message.+ pub fn transcript(a: Allocator, items: []const Value) ![]const Msg {+ return fold(a, items, false);+ }++ /// The POST /v1/compact body.+ pub fn request(a: Allocator, msgs: []const Msg, model: []const u8, goal: ?[]const u8) ![]const u8 {+ const WireUse = struct { tool_use_id: []const u8, tool: []const u8, input: Value };+ const WireResult = struct { tool_use_id: []const u8, text: []const u8, isError: ?bool };+ const WireMsg = struct { role: []const u8, text: []const u8, toolUses: []const WireUse, toolResults: []const WireResult };+ const out = try a.alloc(WireMsg, msgs.len);+ for (msgs, out) |m, *w| {+ const uses = try a.alloc(WireUse, m.uses.len);+ for (m.uses, uses) |u, *x| x.* = .{ .tool_use_id = u.id, .tool = u.name, .input = u.input };+ const results = try a.alloc(WireResult, m.results.len);+ for (m.results, results) |r, *x| x.* = .{ .tool_use_id = r.id, .text = r.text, .isError = if (r.is_error) true else null };+ w.* = .{ .role = @tagName(m.role), .text = m.text, .toolUses = uses, .toolResults = results };+ }+ var aw: std.Io.Writer.Allocating = .init(a);+ const body = .{ .model = model, .mode = "hybrid", .goal = goal, .messages = out };+ try std.json.Stringify.value(body, .{ .emit_null_optional_fields = false }, &aw.writer);+ return aw.writer.buffered();+ }++ pub const Action = enum { keep, drop_result, drop_call };+ pub const Decisions = std.StringHashMapUnmanaged(Action);++ pub const Reply = struct {+ decisions: Decisions = .empty,+ /// The endpoint's transcript after compaction, for validation only.+ messages: []const Value = &.{},+ summarized: bool = false,+ /// The spliced summary without the endpoint's header line.+ summary: []const u8 = "",+ };++ pub fn parseReply(a: Allocator, root: Value) !Reply {+ if (root != .object) return error.UnexpectedReply;+ const msgs = root.object.get("messages") orelse return error.UnexpectedReply;+ const decisions = root.object.get("decisions") orelse return error.UnexpectedReply;+ if (msgs != .array or decisions != .array) return error.UnexpectedReply;+ var reply: Reply = .{ .messages = msgs.array.items };+ for (decisions.array.items) |d| {+ if (d != .object) return error.UnexpectedReply;+ const id = str(d.object, "tool_use_id") orelse return error.UnexpectedReply;+ const action = std.meta.stringToEnum(Action, str(d.object, "action") orelse "") orelse return error.UnexpectedReply;+ try reply.decisions.put(a, id, action);+ }+ if (root.object.get("stats")) |stats| {+ if (stats == .object) if (stats.object.get("summarized")) |v| {+ reply.summarized = v == .bool and v.bool;+ };+ }+ if (reply.summarized) {+ if (reply.messages.len < 2 or reply.messages[1] != .object) return error.UnexpectedReply;+ const text = str(reply.messages[1].object, "text") orelse return error.UnexpectedReply;+ const body = if (std.mem.indexOfScalar(u8, text, '\n')) |nl| text[nl + 1 ..] else text;+ reply.summary = std.mem.trim(u8, body, " \t\r\n");+ if (reply.summary.len == 0) return error.UnexpectedReply;+ }+ return reply;+ }++ fn act(d: *const Decisions, id: ?[]const u8) Action {+ return d.get(id orelse return .keep) orelse .keep;+ }++ /// The endpoint's rule: a message survives unless compaction touched it and+ /// left no text, call, or result behind.+ fn survives(m: Msg, d: *const Decisions) bool {+ var touched = false;+ var left = m.text.len > 0;+ for (m.uses) |u| {+ if (act(d, u.id) == .drop_call) touched = true else left = true;+ }+ for (m.results) |r| switch (act(d, r.id)) {+ .drop_call => touched = true,+ .drop_result => {+ touched = true;+ left = true;+ },+ .keep => left = true,+ };+ return !touched or left;+ }++ fn sameIds(listed: ?Value, comptime T: type, mine: []const T, d: *const Decisions) bool {+ const arr = if (listed) |v| (if (v == .array) v.array.items else return false) else &[_]Value{};+ var k: usize = 0;+ for (mine) |x| {+ if (act(d, x.id) == .drop_call) continue;+ if (k >= arr.len or arr[k] != .object) return false;+ const id = str(arr[k].object, "tool_use_id") orelse return false;+ if (!eql(id, x.id)) return false;+ k += 1;+ }+ return k == arr.len;+ }++ /// Is `r` (from the reply) message `m` after the decisions? Text is compared+ /// byte for byte: the endpoint never rewrites it.+ fn matches(r: Value, m: Msg, d: *const Decisions) bool {+ if (r != .object) return false;+ const role = str(r.object, "role") orelse return false;+ if (!eql(role, @tagName(m.role))) return false;+ if (!eql(str(r.object, "text") orelse "", m.text)) return false;+ return sameIds(r.object.get("toolUses"), Use, m.uses, d) and+ sameIds(r.object.get("toolResults"), Result, m.results, d);+ }++ fn useAt(msgs: []const Msg, id: []const u8) ?usize {+ for (msgs, 0..) |m, j| for (m.uses) |u| if (eql(u.id, id)) return j;+ return null;+ }++ pub fn msgAt(msgs: []const Msg, native: usize) ?usize {+ for (msgs, 0..) |m, j| if (native >= m.lo and native < m.hi) return j;+ return null;+ }++ pub const Plan = struct {+ /// Per neutral message: kept (with its per-call decisions applied).+ keep: []bool,+ /// The neutral message the summary note goes before; null = no summary.+ note_before: ?usize = null,+ };++ /// Check the reply against what the decisions imply, then decide which+ /// neutral messages stay. A summarized reply is [first, summary, newest k]:+ /// k is at least recent_kept, more when the endpoint moved its cut back over+ /// a call/result pair. The cut also moves back here over any call a kept+ /// result answers (a result without its call is rejected by every wire), and+ /// `opening` — an unresolved turn's opening prompt (#581) — stays verbatim.+ pub fn plan(a: Allocator, msgs: []const Msg, reply: *const Reply, opening: ?usize) !Plan {+ const d = &reply.decisions;+ var left: std.ArrayList(usize) = .empty;+ for (msgs, 0..) |m, j| if (survives(m, d)) try left.append(a, j);+ const keep = try a.alloc(bool, msgs.len);+ @memset(keep, true);+ if (!reply.summarized) {+ if (reply.messages.len != left.items.len) return error.UnexpectedReply;+ for (left.items, reply.messages) |j, r| if (!matches(r, msgs[j], d)) return error.UnexpectedReply;+ return .{ .keep = keep };+ }+ // At least two messages summarized between the first and the kept tail.- if (reply.messages.len < 3 or left.items.len < reply.messages.len + 1) return error.UnexpectedReply;++ if (reply.messages.len < recent_kept + 2 or left.items.len < reply.messages.len + 1) return error.UnexpectedReply;+ const head = left.items[0];+ const tail = left.items[left.items.len - (reply.messages.len - 2) ..];+ if (!matches(reply.messages[0], msgs[head], d)) return error.UnexpectedReply;+ for (tail, reply.messages[2..]) |j, r| if (!matches(r, msgs[j], d)) return error.UnexpectedReply;+ var cut = tail[0];+ var moved = true;+ while (moved) {+ moved = false;+ for (msgs[cut..]) |m| for (m.results) |r| {+ const j = useAt(msgs, r.id) orelse continue;+ if (j > head and j < cut) {+ cut = j;+ moved = true;+ }+ };+ }+ @memset(keep, false);+ keep[head] = true;+ @memset(keep[cut..], true);+ if (opening) |t| {+ if (msgAt(msgs, t)) |j| keep[j] = keep[j] or j > head;+ }+ var note_before: ?usize = null;+ for (keep[head + 1 ..], head + 1..) |k, j| if (k) {+ note_before = j;+ break;+ };+ return .{ .keep = keep, .note_before = note_before orelse return error.UnexpectedReply };+ }++ fn cloneObject(a: Allocator, o: ObjectMap) !ObjectMap {+ var c: ObjectMap = .empty;+ try c.ensureTotalCapacity(a, o.count());+ var it = o.iterator();+ while (it.next()) |e| c.putAssumeCapacity(e.key_ptr.*, e.value_ptr.*);+ return c;+ }++ fn withField(a: Allocator, o: ObjectMap, key: []const u8, v: Value) !Value {+ var c = try cloneObject(a, o);+ try c.put(a, key, v);+ return .{ .object = c };+ }++ const Shape = enum { string, parts };++ /// A result payload per its decision: gone with its call, shrunk to a stub,+ /// or untouched. Null removes the item.+ fn shrink(a: Allocator, o: ObjectMap, field: []const u8, action: Action, removable: bool, shape: Shape, stubber: anytype) !?Value {+ if (action == .keep) return .{ .object = o };+ if (action == .drop_call and removable) return null;+ const full = try textOf(a, o.get(field) orelse .null);+ const stub = stubber.stub(a, full);+ if (stub.len >= full.len) return .{ .object = o };+ const payload: Value = switch (shape) {+ .string => .{ .string = stub },+ .parts => blk: {+ var part: ObjectMap = .empty;+ try part.put(a, "type", .{ .string = "text" });+ try part.put(a, "text", .{ .string = stub });+ var parts = std.json.Array.init(a);+ try parts.append(.{ .object = part });+ break :blk .{ .array = parts };+ },+ };+ return try withField(a, o, field, payload);+ }++ fn shrinkBlocks(a: Allocator, o: ObjectMap, d: *const Decisions, removable: bool, stubber: anytype) !?Value {+ const content = o.get("content") orelse return .{ .object = o };+ if (content != .array) return .{ .object = o };+ var blocks = std.json.Array.init(a);+ var changed = false;+ for (content.array.items) |block| {+ const is_result = block == .object and eql(str(block.object, "type") orelse "", "tool_result");+ const action = if (is_result) act(d, str(block.object, "tool_use_id")) else .keep;+ if (action == .keep) {+ try blocks.append(block);+ continue;+ }+ changed = true;+ if (try shrink(a, block.object, "content", action, removable, .string, stubber)) |v| try blocks.append(v);+ }+ if (!changed) return .{ .object = o };+ if (blocks.items.len == 0) return null;+ return try withField(a, o, "content", .{ .array = blocks });+ }++ /// Something a later request can use: text or a call. Reasoning, thinking or+ /// an empty shell left behind by dropped calls is not.+ fn substantive(content: ?Value, calls: ?Value) bool {+ if (calls) |c| if (c == .array and c.array.items.len > 0) return true;+ const v = content orelse return false;+ if (v == .string) return v.string.len > 0;+ if (v != .array) return false;+ for (v.array.items) |block| {+ if (block != .object) continue;+ const t = str(block.object, "type") orelse continue;+ if (eql(t, "tool_use")) return true;+ if (!eql(t, "text")) continue;+ const text: []const u8 = str(block.object, "text") orelse "";+ if (text.len > 0) return true;+ }+ return false;+ }++ fn dropCalls(a: Allocator, o: ObjectMap, d: *const Decisions) !?Value {+ var calls = o.get("tool_calls");+ var calls_changed = false;+ if (calls) |c| if (c == .array) {+ var kept = std.json.Array.init(a);+ for (c.array.items) |call| {+ const id = if (call == .object) str(call.object, "id") else null;+ if (act(d, id) == .drop_call) calls_changed = true else try kept.append(call);+ }+ if (calls_changed) calls = .{ .array = kept };+ };+ var content = o.get("content");+ var content_changed = false;+ if (content) |c| if (c == .array) {+ var kept = std.json.Array.init(a);+ for (c.array.items) |block| {+ const dropped = block == .object and eql(str(block.object, "type") orelse "", "tool_use") and+ act(d, str(block.object, "id")) == .drop_call;+ if (dropped) content_changed = true else try kept.append(block);+ }+ if (content_changed) content = .{ .array = kept };+ };+ if (!calls_changed and !content_changed) return .{ .object = o };+ if (!substantive(content, calls)) return null;+ var c = try cloneObject(a, o);+ if (calls_changed) {+ // An empty tool_calls array is rejected outright; the key goes.+ if (calls.?.array.items.len == 0) _ = c.orderedRemove("tool_calls") else try c.put(a, "tool_calls", calls.?);+ }+ if (content_changed) try c.put(a, "content", content.?);+ return .{ .object = c };+ }++ /// One native item after the decisions. `removable`: the item is from an+ /// earlier turn, so a dropped call goes with its result and stale reasoning+ /// goes too (#174). In the live turn a dropped call only loses its result's+ /// bulk: that turn's reasoning, thinking and signatures stay paired with every+ /// call they reference.+ pub fn applyItem(a: Allocator, item: Value, d: *const Decisions, removable: bool, stubber: anytype) !?Value {+ if (item != .object) return item;+ const o = item.object;+ if (str(o, "type")) |t| {+ if (eql(t, "function_call") or eql(t, "custom_tool_call"))+ return if (removable and act(d, str(o, "call_id") orelse str(o, "id")) == .drop_call) null else item;+ if (eql(t, "function_call_output") or eql(t, "custom_tool_call_output"))+ return shrink(a, o, "output", act(d, str(o, "call_id")), removable, .string, stubber);+ if (eql(t, "function_result"))+ return shrink(a, o, "result", act(d, str(o, "call_id")), removable, .parts, stubber);+ if (eql(t, "reasoning") and removable) return null;+ return item;+ }+ const role = str(o, "role") orelse return item;+ if (eql(role, "tool")) return shrink(a, o, "content", act(d, str(o, "tool_call_id")), removable, .string, stubber);+ if (eql(role, "assistant")) return if (removable) dropCalls(a, o, d) else item;+ if (eql(role, "user")) return shrinkBlocks(a, o, d, removable, stubber);+ return item;+ }++ /// The history to install. `removable_before`: items before this index belong+ /// to earlier turns (see applyItem).+ pub fn apply(a: Allocator, items: []const Value, msgs: []const Msg, p: Plan, d: *const Decisions, removable_before: usize, note: ?Value, stubber: anytype) !std.json.Array {+ var fresh = std.json.Array.init(a);+ try fresh.ensureTotalCapacity(items.len + 1);+ for (msgs, 0..) |m, j| {+ if (p.note_before == j) if (note) |n| try fresh.append(n);+ if (!p.keep[j]) continue;+ for (items[m.lo..m.hi], m.lo..) |item, i| {+ if (try applyItem(a, item, d, i < removable_before, stubber)) |v| try fresh.append(v);+ }+ }+ return fresh;+ }++ /// The items a summary replaced (#409 reads their artifact paths).+ pub fn replaced(a: Allocator, items: []const Value, msgs: []const Msg, p: Plan) ![]const Value {+ var out: std.ArrayList(Value) = .empty;+ for (msgs, p.keep) |m, k| if (!k) try out.appendSlice(a, items[m.lo..m.hi]);+ return out.items;+ }++ const IdSet = std.StringHashMapUnmanaged(void);++ /// Results without an earlier call, and calls without a later result.+ fn unpaired(a: Allocator, items: []const Value) !IdSet {+ const msgs = try fold(a, items, true);+ var calls: IdSet = .empty;+ var answered: IdSet = .empty;+ var bad: IdSet = .empty;+ for (msgs) |m| {+ for (m.uses) |u| try calls.put(a, u.id, {});+ for (m.results) |r| {+ if (!calls.contains(r.id)) try bad.put(a, r.id, {});+ try answered.put(a, r.id, {});+ }+ }+ var it = calls.keyIterator();+ while (it.next()) |id| if (!answered.contains(id.*)) try bad.put(a, id.*, {});+ return bad;+ }++ /// Refuse a history that pairs worse than the one it replaces: a provider+ /// rejects an orphaned result or an unanswered call on every request.+ pub fn checkPairs(a: Allocator, before: []const Value, after: []const Value) !void {+ const was = try unpaired(a, before);+ const now = try unpaired(a, after);+ var it = now.keyIterator();+ while (it.next()) |id| if (!was.contains(id.*)) return error.UnpairedToolCall;+ }diff --cc src/session_settings.zigindex 0e508dae,85db6389..0578cc23--- a/src/session_settings.zig+++ b/src/session_settings.zig@@@ -301,9 -276,10 +302,11 @@@ fn codegraffLoginKey(io: Io, arena: Allpub fn setupSkillsAndTheme(io: Io, arena: Allocator, environ_map: anytype, out: *Io.Writer, flags: args.Flags, use_color: bool, json_mode: bool, cwd_display: []const u8) !ThemeSetup {try applyEnvKnobs(arena, environ_map);+ @import("compact_at.zig").applyStartup(io, arena); // --compact-at > GRAFF_COMPACT_PCT > saved /compact-at@import("python_version.zig").detect(io, environ_map.get("PATH")); // ADR 0242: before any catalog renders- _ = jev_tool.setCodegraffLoginKey(io, codegraffLoginKey(io, arena, environ_map));+ const codegraff_key = codegraffLoginKey(io, arena, environ_map);+ _ = jev_tool.setCodegraffLoginKey(io, codegraff_key);+ @import("gateway_compact.zig").setLoginKey(io, codegraff_key); // ADR 0252: the login also compactsskills.loadSkillSettings(io, arena); // per-skill opt-outs, also gates the auto-connectanim.loadAnimationSetting(io, arena); // {"animation": "..."} → thinking spinner choiceanim.loadThemeSetting(io, arena); // {"theme": "<name>"} → opt-in terminal color theme