graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Release v0.302.40
justrachc3c7ff8a5fdd
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 31573e46..d3f714d4 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,6 +10,15 @@ 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.40++- Integrates the pending stream recovery, claim inventory, workspace skills, stored conversation, MCP configuration, companion update and typed ACP sign-in recovery changes.+- Preserves partial replies and unread tool results, secret-free configuration listings, and atomic installer replacement.+- Hosted compaction remains explicitly opt-in; existing local-summary and provider-owned compaction defaults are unchanged.+- Includes the unattended-run recovery and one-shot result contracts from the unpublished v0.302.39 draft.++See `docs/releases/v0.302.40.md` for the release summary.+## v0.302.39### Unattended runsdiff --git a/docs/adr/0246-acp-reauth-is-route-specific.md b/docs/adr/0285-acp-reauth-is-route-specific.mdsimilarity index 97%rename from docs/adr/0246-acp-reauth-is-route-specific.mdrename to docs/adr/0285-acp-reauth-is-route-specific.mdindex 6075249a..38a16f26 100644--- a/docs/adr/0246-acp-reauth-is-route-specific.md+++ b/docs/adr/0285-acp-reauth-is-route-specific.md@@ -1,4 +1,4 @@-# 0246. ACP reauthentication recovery identifies the exact credential route+# 0285. ACP reauthentication recovery identifies the exact credential routeStatus: accepted 2026-10-03diff --git a/docs/releases/v0.302.40.md b/docs/releases/v0.302.40.mdnew file mode 100644index 00000000..969ed746--- /dev/null+++ b/docs/releases/v0.302.40.md@@ -0,0 +1,14 @@+# Codegraff v0.302.40++This release brings the pending recovery, configuration and update changes together.++- Interrupted streams retry with separate drop and stall budgets; ACP preserves the partial reply for the next turn and resume.+- Workspace skills are available in headless runs. Runaway replies recover without replacing bounded output-limit continuation.+- Claim capacity errors and keyless status list held claims and eligible releases without evicting active owners.+- MCP off-lists and `graff mcp list --json` expose configuration provenance without secret values; project precedence is preserved.+- Stored interactions continue from verified history, with complete prompt usage accounting. Compaction preserves unread tool results.+- ACP authentication failures carry fixed, route-specific sign-in recovery metadata.+- The installer refreshes the code-navigation companion with checksum validation and atomic replacement.+- Hosted compaction validates decisions and retained context, but remains explicitly opt-in with `GRAFF_CLEF_COMPACT=1`. Local-summary defaults and provider-owned compaction routes are unchanged.++Includes the unattended-run recovery and structured one-shot results from the unpublished v0.302.39 draft.diff --git a/install.sh b/install.shindex 105fedd0..4d5c803b 100644--- a/install.sh+++ b/install.sh@@ -169,7 +169,7 @@ fetch_release() {# and runs `--version` is installed. Prints current, installed or failed.CODEDB_REPO="${CODEDB_REPO:-https://github.com/justrach/codedb}"update_codedb() {- local platform="$1" asset="codedb-$1" tag latest current dest tmpd+ local platform="$1" asset="codedb-$1" tag latest current dest tmpd stagedtag="$(curl -fsSLI --max-time 30 -o /dev/null -w '%{url_effective}' "$CODEDB_REPO/releases/latest" 2>/dev/null)" || { echo failed; return 0; }latest="${tag##*/v}"case "$latest" in ''|*/*) echo failed; return 0 ;; esac@@ -192,10 +192,16 @@ update_codedb() {"$tmpd/$asset" --version >/dev/null 2>&1 || { rm -rf "$tmpd"; echo failed; return 0; }# Replace the codedb on PATH when we may write it; otherwise install next to graff.if [ -z "$dest" ] || [ ! -w "$dest" ]; then dest="$INSTALL_DIR/codedb"; fi- mkdir -p "$(dirname "$dest")"- rm -f "$dest"- install -m 0755 "$tmpd/$asset" "$dest" || { rm -rf "$tmpd"; echo failed; return 0; }- darwin_sign "$dest" codedb+ mkdir -p "$(dirname "$dest")" || { rm -rf "$tmpd"; echo failed; return 0; }+ # Stage a fresh inode beside the destination, then replace it atomically.+ # A failed install or signature must leave the existing binary untouched.+ staged="$(mktemp "${dest}.XXXXXX")" || { rm -rf "$tmpd"; echo failed; return 0; }+ if ! install -m 0755 "$tmpd/$asset" "$staged" \+ || ! darwin_sign "$staged" codedb \+ || ! { [ "$(uname -s)" != Darwin ] || codesign --verify --strict "$staged" >/dev/null 2>&1; } \+ || ! mv -f "$staged" "$dest"; then+ rm -f "$staged"; rm -rf "$tmpd"; echo failed; return 0+ firm -rf "$tmpd"echo "installed $latest"}diff --git a/scripts/eval/tier1-manifest.json b/scripts/eval/tier1-manifest.jsonindex 961e4f5a..1739661e 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": 3276,+ "test_count_baseline": 3315,"test_count_slack": 25,"required_invariants": [{diff --git a/scripts/test-mcp-legacy-fallback.py b/scripts/test-mcp-legacy-fallback.pyindex 723f83a3..29176d58 100644--- a/scripts/test-mcp-legacy-fallback.py+++ b/scripts/test-mcp-legacy-fallback.py@@ -132,7 +132,9 @@ class McpFixture(BaseHTTPRequestHandler):assert self.headers.get("Mcp-Protocol-Version") == NEGOTIATED_PROTOCOLassert self.headers.get("Mcp-Session-Id") == SESSION_ID- if ordinal == 3:+ assert ordinal in (3, 4)+ # HTTP initialization is sent concurrently with tools/list.+ if message["method"] == "notifications/initialized":assert message == {"jsonrpc": "2.0","method": "notifications/initialized",@@ -141,7 +143,6 @@ class McpFixture(BaseHTTPRequestHandler):self._respond(202)return- assert ordinal == 4assert message["method"] == "tools/list"assert message["id"] == 3body = json.dumps(@@ -202,6 +203,7 @@ def run(graff: Path) -> None:"GRAFF_FLEET": "off","GRAFF_NO_SMOLIFY": "1","GRAFF_NO_TELEMETRY": "1",+ "GRAFF_REPL_DEBUG": "1",# Private home: no user servers, no cached era from a past run."HOME": str(workspace),"USERPROFILE": str(workspace),@@ -226,6 +228,9 @@ def run(graff: Path) -> None:if McpFixture.failure is not None:raise AssertionError("MCP fixture rejected a request") from McpFixture.failureassert len(McpFixture.requests) == 4, McpFixture.requests+ assert sorted(req["message"]["method"] for req in McpFixture.requests[2:]) == [+ "notifications/initialized", "tools/list"+ ]assert (f"[mcp:fixture] connected (mcp {NEGOTIATED_PROTOCOL}) — 1 tool(s)"in completed.stderrdiff --git a/scripts/test-mcp-stdio-probe.py b/scripts/test-mcp-stdio-probe.pyindex 6b5deb8f..7f97d187 100644--- a/scripts/test-mcp-stdio-probe.py+++ b/scripts/test-mcp-stdio-probe.py@@ -164,6 +164,7 @@ def run_graff(graff: Path, fixture: Path, probe: bool) -> subprocess.CompletedPr"GRAFF_FLEET": "off","GRAFF_NO_SMOLIFY": "1","GRAFF_NO_TELEMETRY": "1",+ "GRAFF_REPL_DEBUG": "1",# A private home per run: the user's own servers stay out, and# the tools/list cache the gate-off run writes cannot turn the# gate-on run into a cache hit that skips the probe.diff --git a/scripts/test-mcp-streamable-http.py b/scripts/test-mcp-streamable-http.pyindex a17afb68..2de363aa 100644--- a/scripts/test-mcp-streamable-http.py+++ b/scripts/test-mcp-streamable-http.py@@ -148,6 +148,9 @@ def run(graff: Path) -> None:"GRAFF_FLEET": "off","GRAFF_NO_SMOLIFY": "1","GRAFF_NO_TELEMETRY": "1",+ "GRAFF_REPL_DEBUG": "1",+ "HOME": str(workspace),+ "USERPROFILE": str(workspace),})completed = subprocess.run(diff --git a/src/acp_live_turn.zig b/src/acp_live_turn.zigindex 23cea41b..57568198 100644--- a/src/acp_live_turn.zig+++ b/src/acp_live_turn.zig@@ -51,12 +51,18 @@ pub const LiveTurn = struct {pub fn errorRecovery(ctx: *anyopaque, err: anyerror) ?@import("auth_recovery.zig").Recovery {const self: *LiveTurn = @ptrCast(@alignCast(ctx));- return if (err == error.ApiError) self.root.last_api_reauth else null;+ return switch (err) {+ error.ApiError, error.ProviderAuthenticationFailed => self.root.last_api_reauth,+ else => null,+ };}pub fn errorMessage(ctx: *anyopaque, err: anyerror) []const u8 {const self: *LiveTurn = @ptrCast(@alignCast(ctx));- return if (err == error.ApiError) self.root.last_api_error orelse "Provider request failed" else @errorName(err);+ return switch (err) {+ error.ApiError, error.ProviderAuthenticationFailed => self.root.last_api_error orelse "Provider request failed",+ else => @errorName(err),+ };}pub fn signalCancel() void {diff --git a/src/acp_reauth_tests.zig b/src/acp_reauth_tests.zigindex 7d67d183..6a9eb552 100644--- a/src/acp_reauth_tests.zig+++ b/src/acp_reauth_tests.zig@@ -1,9 +1,10 @@-//! Focused regression on parsed Responses failures and both ACP dispatch paths.+//! Production terminal-auth handling through transport/Responses fixtures and ACP.const std = @import("std");const Io = std.Io;const Agent = @import("agent.zig").Agent;const provider = @import("provider.zig");const recovery = @import("auth_recovery.zig");+const quota = @import("agent_request_quota.zig");const engine = @import("acp_engine.zig");const LiveTurn = @import("acp_live_turn.zig").LiveTurn;const v2 = @import("acp_v2.zig");@@ -19,14 +20,40 @@ fn root(a: std.mem.Allocator) Agent {fn parsedFailure(ctx: *anyopaque, a: std.mem.Allocator, text: []const u8) anyerror![]const u8 {const live: *LiveTurn = @ptrCast(@alignCast(ctx));if (std.mem.eql(u8, text, "ok")) return "recovered";+ // Model the terminal boundary after the request's one refresh was spent.+ // No OAuth, credential-store access or network is needed by this fixture.+ var refreshed = true;+ if (std.mem.startsWith(u8, text, "http:")) {+ const status = try std.fmt.parseInt(u16, text[5..], 10);+ const err = quota.httpError(status) orelse return error.UnexpectedFixtureStatus;+ if (try quota.handle(live.root, "", quota.transportCode(err), "", &refreshed)) return error.UnexpectedFixtureRetry;+ return err;+ }const parsed = try live.root.parseResponses(text);const failure = parsed.err;- live.root.last_api_reauth = recovery.forFailure(live.root.provider, failure.code, failure.message);+ if (try quota.handle(live.root, "", failure.code, failure.message, &refreshed)) return error.UnexpectedFixtureRetry;live.root.last_api_error = try @import("agent_responses.zig").failureDiagnostic(a, live.root.provider.id, failure);return error.ApiError;}const invalid_token = "{\"detail\":{\"error_code\":\"invalid_token\"}}";+const auth_bodies = [_][]const u8{+ "http:401",+ "{\"error\":{\"code\":401,\"message\":\"rejected\"}}",+ invalid_token,+ "{\"error\":{\"code\":\"token_expired\",\"message\":\"Provided authentication token is expired. Please try signing in again.\"}}",+ "data: {\"type\":\"response.failed\",\"response\":{\"error\":{\"code\":\"token_expired\",\"message\":\"Provided authentication token is expired. Please try signing in again.\"}}}\n",+ "data: {\"type\":\"response.failed\",\"response\":{\"error\":{\"code\":\"subscription_sharing_invalid_user\",\"message\":\"User cannot be verified\"}}}\n",+};+const non_auth_bodies = [_][]const u8{+ "http:402",+ "http:403",+ "{\"error\":{\"code\":403,\"message\":\"route not eligible\"}}",+ "{\"error\":{\"code\":\"server_error\",\"message\":\"upstream unavailable\"}}",+ "data: {\"type\":\"response.failed\",\"response\":{\"error\":{\"code\":\"insufficient_quota\",\"message\":\"invalid_token\"}}}\n",+ "{\"error\":{\"code\":\"server_error\",\"message\":\"authentication service unavailable\"}}",+ "{\"error\":{\"code\":\"subscription_sharing_usage_limit_exceeded\",\"message\":\"authentication service unavailable\"}}",+};fn prompt(a: std.mem.Allocator, text: []const u8) ![]const u8 {var w: Io.Writer.Allocating = .init(a);@@ -51,13 +78,39 @@ fn assertLogin(data: std.json.ObjectMap, id: []const u8, command: []const u8) !vtry std.testing.expectEqual(@as(usize, 3), data.count());}+fn assertV2Failure(a: std.mem.Allocator, output: []const u8, id: ?[]const u8) !void {+ var lines = std.mem.tokenizeScalar(u8, output, '\n');+ var accepted: usize = 0;+ var failures: usize = 0;+ while (lines.next()) |line| {+ const value = try std.json.parseFromSliceLeaky(std.json.Value, a, line, .{});+ try std.testing.expect(value.object.get("error") == null);+ if (value.object.get("result")) |result| {+ try std.testing.expect(result.object.get("messageId") != null);+ accepted += 1;+ }+ const params = value.object.get("params") orelse continue;+ const update = params.object.get("update") orelse continue;+ const meta = update.object.get("_meta") orelse continue;+ if (meta.object.get("graff/error") == null) continue;+ try std.testing.expectEqualStrings("idle", update.object.get("state").?.string);+ try std.testing.expectEqualStrings("_error", update.object.get("stopReason").?.string);+ if (id) |expected| {+ try assertLogin(meta.object.get("graff/reauth").?.object, expected, if (std.mem.eql(u8, expected, "codex")) "codex" else "graff");+ } else try std.testing.expect(meta.object.get("graff/reauth") == null);+ failures += 1;+ }+ try std.testing.expectEqual(@as(usize, 1), accepted);+ try std.testing.expectEqual(@as(usize, 1), failures);+}+test "typed reauth: route and credential source gate the action; non-auth codes win" {var p = route("chatgpt-new");- for ([_][]const u8{ "invalid_token", "invalid_api_key", "authentication_error", "token_expired", "subscription_sharing_invalid_user", "subscription_sharing_v2_invalid_user" }) |code| {+ for ([_][]const u8{ "401", "invalid_token", "invalid_api_key", "authentication_error", "token_expired", "subscription_sharing_invalid_user", "subscription_sharing_v2_invalid_user" }) |code| {try std.testing.expect(recovery.forFailure(p, code, "User cannot be verified") != null);}try std.testing.expect(recovery.forFailure(p, null, "Provided authentication token is expired.") != null);- for ([_][]const u8{ "server_error", "rate_limit_exceeded", "subscription_sharing_usage_limit_exceeded", "subscription_sharing_v2_usage_limit_exceeded", "context_length_exceeded" }) |code| {+ for ([_][]const u8{ "402", "403", "server_error", "rate_limit_exceeded", "subscription_sharing_usage_limit_exceeded", "subscription_sharing_v2_usage_limit_exceeded", "context_length_exceeded" }) |code| {try std.testing.expect(recovery.forFailure(p, code, "authentication service unavailable") == null);}try std.testing.expect(recovery.forFailure(p, null, "request id expired") == null);@@ -68,9 +121,56 @@ test "typed reauth: route and credential source gate the action; non-auth codestry std.testing.expect(recovery.forFailure(p, "invalid_api_key", "Unauthorized") == null);}+test "typed reauth: production terminal handler gates actions and clears stale metadata" {+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer state.deinit();+ var agent = root(state.allocator());+ const cases = [_]struct { etype: []const u8 = "", code: ?[]const u8 = null, msg: []const u8 = "", err: anyerror, action: bool = false }{+ .{ .code = "401", .err = error.ProviderAuthenticationFailed, .action = true },+ .{ .etype = "authentication_error", .err = error.ProviderAuthenticationFailed, .action = true },+ .{ .code = "invalid_token", .msg = "rejected", .err = error.ProviderAuthenticationFailed, .action = true },+ .{ .msg = "token has expired", .err = error.ProviderAuthenticationFailed, .action = true },+ .{ .code = "402", .msg = "authentication required", .err = error.ProviderQuotaExceeded },+ .{ .code = "403", .msg = "route not eligible", .err = error.ProviderAccessDenied },+ .{ .code = "403", .msg = "authentication service unavailable", .err = error.ProviderAuthenticationFailed },+ .{ .code = "server_error", .msg = "Unauthorized", .err = error.ProviderAuthenticationFailed },+ .{ .code = "insufficient_quota", .msg = "invalid_token", .err = error.ProviderQuotaExceeded },+ };+ const failover = @import("credential_failover.zig");+ failover.resetForTest();+ defer failover.resetForTest();+ for ([_][]const u8{ "chatgpt-new", "codex", "openai" }) |id| {+ for ([_]provider.Keys.CredentialSource{ .login, .environment, .stored, .session }) |source| {+ agent.provider = route(id);+ agent.provider.source = source;+ for (cases) |case| {+ agent.last_api_reauth = recovery.forFailure(route("codex"), "401", "");+ // Already spent: the common boundary must not attempt refresh again.+ var refreshed = true;+ try std.testing.expectError(case.err, quota.handle(&agent, case.etype, case.code, case.msg, &refreshed));+ try std.testing.expect(refreshed);+ const action = case.action and source == .login and !std.mem.eql(u8, id, "openai");+ try std.testing.expectEqual(action, agent.last_api_reauth != null);+ if (agent.last_api_reauth) |data| {+ try std.testing.expectEqualStrings(id, data.provider);+ try std.testing.expectEqualStrings(if (std.mem.eql(u8, id, "codex")) "codex" else "graff", data.login.command);+ }+ }+ agent.last_api_reauth = recovery.forFailure(route("codex"), "401", "");+ var refreshed = false;+ try std.testing.expect(!try quota.handle(&agent, "server_error", null, "temporarily unavailable", &refreshed));+ try std.testing.expect(agent.last_api_reauth == null);+ try std.testing.expect(!refreshed);+ }+ }+}+test "typed reauth: v1 parsed HTTP and stream auth errors carry route-specific login; next prompt recovers" {defer v2.resetForTest();v2.gate = false;+ const failover = @import("credential_failover.zig");+ failover.resetForTest();+ defer failover.resetForTest();const cancelled = engine.cancel_flag.swap(false, .acq_rel);defer engine.cancel_flag.store(cancelled, .release);var state = std.heap.ArenaAllocator.init(std.testing.allocator);@@ -81,15 +181,9 @@ test "typed reauth: v1 parsed HTTP and stream auth errors carry route-specific lvar out: Io.Writer.Allocating = .init(a);var live: LiveTurn = .{ .root = &agent, .keys = &keys, .out = &out.writer };var d = dispatch(&live);- const bodies = [_][]const u8{- invalid_token,- "{\"error\":{\"code\":\"token_expired\",\"message\":\"Provided authentication token is expired. Please try signing in again.\"}}",- "data: {\"type\":\"response.failed\",\"response\":{\"error\":{\"code\":\"token_expired\",\"message\":\"Provided authentication token is expired. Please try signing in again.\"}}}\n",- "data: {\"type\":\"response.failed\",\"response\":{\"error\":{\"code\":\"subscription_sharing_invalid_user\",\"message\":\"User cannot be verified\"}}}\n",- };for ([_][]const u8{ "chatgpt-new", "codex" }) |id| {agent.provider = route(id);- for (bodies) |body| {+ for (auth_bodies) |body| {out.clearRetainingCapacity();try engine.handleLine(&d, a, &out.writer, try prompt(a, body));const result = try std.json.parseFromSliceLeaky(std.json.Value, a, std.mem.sliceTo(out.writer.buffered(), '\n'), .{});@@ -97,21 +191,36 @@ test "typed reauth: v1 parsed HTTP and stream auth errors carry route-specific ltry std.testing.expectEqual(@as(i64, -32000), err.get("code").?.integer);try assertLogin(err.get("data").?.object, id, if (std.mem.eql(u8, id, "codex")) "codex" else "graff");try std.testing.expect(result.object.get("result") == null);+ try std.testing.expectEqualStrings("provider authentication failed: sign in again or check the API key", err.get("message").?.string);try std.testing.expect(LiveTurn.errorRecovery(&live, error.StreamDropped) == null);+ try std.testing.expect(LiveTurn.errorRecovery(&live, error.ProviderQuotaExceeded) == null);+ try std.testing.expect(LiveTurn.errorRecovery(&live, error.ProviderAccessDenied) == null);}}// A subsequent server/quota rejection must not reuse the previous action.- for ([_][]const u8{ "server_error", "subscription_sharing_usage_limit_exceeded" }) |code| {- out.clearRetainingCapacity();- const body = try std.fmt.allocPrint(a, "{{\"error\":{{\"code\":\"{s}\",\"message\":\"authentication service unavailable\"}}}}", .{code});- try engine.handleLine(&d, a, &out.writer, try prompt(a, body));- const result = try std.json.parseFromSliceLeaky(std.json.Value, a, std.mem.sliceTo(out.writer.buffered(), '\n'), .{});- const err = result.object.get("error").?.object;- try std.testing.expectEqual(@as(i64, -32603), err.get("code").?.integer);- try std.testing.expect(err.get("data") == null);+ for ([_][]const u8{ "chatgpt-new", "codex" }) |id| {+ agent.provider = route(id);+ for (non_auth_bodies) |body| {+ out.clearRetainingCapacity();+ agent.last_api_reauth = recovery.forFailure(agent.provider, "401", "");+ try engine.handleLine(&d, a, &out.writer, try prompt(a, body));+ const result = try std.json.parseFromSliceLeaky(std.json.Value, a, std.mem.sliceTo(out.writer.buffered(), '\n'), .{});+ const err = result.object.get("error").?.object;+ try std.testing.expectEqual(@as(i64, -32603), err.get("code").?.integer);+ try std.testing.expect(err.get("data") == null);+ }+ agent.provider.source = .environment;+ for (auth_bodies) |body| {+ out.clearRetainingCapacity();+ try engine.handleLine(&d, a, &out.writer, try prompt(a, body));+ const result = try std.json.parseFromSliceLeaky(std.json.Value, a, std.mem.sliceTo(out.writer.buffered(), '\n'), .{});+ const err = result.object.get("error").?.object;+ try std.testing.expectEqual(@as(i64, -32603), err.get("code").?.integer);+ try std.testing.expect(err.get("data") == null);+ }}out.clearRetainingCapacity();- agent.last_api_reauth = recovery.forFailure(agent.provider, "invalid_token", "Unauthorized");+ agent.last_api_reauth = recovery.forFailure(route("codex"), "invalid_token", "Unauthorized");try engine.handleLine(&d, a, &out.writer, try prompt(a, "ok"));try std.testing.expect(agent.last_api_reauth == null);try std.testing.expect(std.mem.indexOf(u8, out.writer.buffered(), "end_turn") != null);@@ -121,6 +230,11 @@ test "typed reauth: v1 parsed HTTP and stream auth errors carry route-specific ltest "typed reauth: v2 accepted prompt carries the same action on the failed idle update" {defer v2.resetForTest();v2.gate = true;+ const failover = @import("credential_failover.zig");+ failover.resetForTest();+ defer failover.resetForTest();+ const cancelled = engine.cancel_flag.swap(false, .acq_rel);+ defer engine.cancel_flag.store(cancelled, .release);var state = std.heap.ArenaAllocator.init(std.testing.allocator);defer state.deinit();const a = state.allocator();@@ -131,19 +245,30 @@ test "typed reauth: v2 accepted prompt carries the same action on the failed idlvar d = dispatch(&live);try engine.handleLine(&d, a, &out.writer, "{\"id\":1,\"method\":\"initialize\",\"params\":{\"protocolVersion\":2}}");out.clearRetainingCapacity();- try engine.handleLine(&d, a, &out.writer, try prompt(a, invalid_token));- var lines = std.mem.tokenizeScalar(u8, out.writer.buffered(), '\n');- var actions: usize = 0;- while (lines.next()) |line| {- const value = try std.json.parseFromSliceLeaky(std.json.Value, a, line, .{});- try std.testing.expect(value.object.get("error") == null);- const params = value.object.get("params") orelse continue;- const update = params.object.get("update") orelse continue;- const meta = update.object.get("_meta") orelse continue;- const data = meta.object.get("graff/reauth") orelse continue;- try std.testing.expectEqualStrings("_error", update.object.get("stopReason").?.string);- try assertLogin(data.object, "chatgpt-new", "graff");- actions += 1;+ for ([_][]const u8{ "chatgpt-new", "codex" }) |id| {+ agent.provider = route(id);+ for (auth_bodies) |body| {+ out.clearRetainingCapacity();+ try engine.handleLine(&d, a, &out.writer, try prompt(a, body));+ try assertV2Failure(a, out.writer.buffered(), id);+ }+ for (non_auth_bodies) |body| {+ out.clearRetainingCapacity();+ agent.last_api_reauth = recovery.forFailure(agent.provider, "401", "");+ try engine.handleLine(&d, a, &out.writer, try prompt(a, body));+ try assertV2Failure(a, out.writer.buffered(), null);+ }+ agent.provider.source = .environment;+ for (auth_bodies) |body| {+ out.clearRetainingCapacity();+ try engine.handleLine(&d, a, &out.writer, try prompt(a, body));+ try assertV2Failure(a, out.writer.buffered(), null);+ }}- try std.testing.expectEqual(@as(usize, 1), actions);+ out.clearRetainingCapacity();+ agent.last_api_reauth = recovery.forFailure(route("codex"), "401", "");+ try engine.handleLine(&d, a, &out.writer, try prompt(a, "ok"));+ try std.testing.expect(agent.last_api_reauth == null);+ try std.testing.expect(std.mem.indexOf(u8, out.writer.buffered(), "end_turn") != null);+ try std.testing.expect(std.mem.indexOf(u8, out.writer.buffered(), "reauth_required") == null);}diff --git a/src/agent_compact.zig b/src/agent_compact.zigindex 2ce3fbd3..8de9ce27 100644--- a/src/agent_compact.zig+++ b/src/agent_compact.zig@@ -120,7 +120,7 @@ pub fn compact(self: *Agent) anyerror!usize {if (@import("compaction_window.zig").latestBlob(self.messages.items) != null) return error.ServerCompactionRequired;// ADR 0261: both hosted paths require the existing experimental opt-in.const hosted = @import("gateway_compact.zig");- if (hosted.hasLogin(self.io)) {+ if (@import("agent_clef_compact.zig").enabled() and 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 it@@ -475,140 +475,8 @@ pub fn capOversizedToolOutputs(self: *Agent, cap: usize) usize {return 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 --git a/src/agent_compact_recover.zig b/src/agent_compact_recover.zigindex bc87c2a9..271ceb19 100644--- a/src/agent_compact_recover.zig+++ b/src/agent_compact_recover.zig@@ -113,6 +113,7 @@ pub fn compactOrRecover(self: *Agent, trim_on_fail: bool) void {self.compact_transport_failures = 0;return; // user hit Esc mid-compaction},+ error.CompactionOff => return, // explicit credit policy: preserve history without emergency trimmingerror.EmptySummary, error.IncompleteSummary, error.ActivePromptPinned => {}, // compact() already explained itelse => {if (main_mod.json_mode)diff --git a/src/agent_request_quota.zig b/src/agent_request_quota.zigindex 8003417d..718b31ee 100644--- a/src/agent_request_quota.zig+++ b/src/agent_request_quota.zig@@ -98,6 +98,8 @@ pub fn transportCode(err: anyerror) ?[]const u8 {/// The existing one-shot login refresh and consented plan handoff remain the/// only exceptions. Never send a quota fault through an auth refresh ladder.pub fn handle(self: *@import("agent.zig").Agent, etype: []const u8, code: ?[]const u8, msg: []const u8, refreshed: *bool) !bool {+ // A retry, handoff or unrelated rejection must not retain a prior action.+ self.last_api_reauth = null;const fault = classify(etype, code, msg) orelse return false;const policy = @import("agent_request_policy.zig");if (fault == .quota and policy.handOffExhaustedPlan(self)) {@@ -108,6 +110,12 @@ pub fn handle(self: *@import("agent.zig").Agent, etype: []const u8, code: ?[]conself.closeCodexWs();return true;}+ // This boundary sees both verified transport statuses and parsed JSON/SSE+ // faults, after the existing refresh/handoff policy has declined a retry.+ // Classification already established auth; retain any structured code so+ // a non-auth code still vetoes a login action (including generic 403).+ if (fault == .authentication)+ self.last_api_reauth = @import("auth_recovery.zig").forFailure(self.provider, code orelse "authentication_error", "");self.last_api_error = switch (fault) {.quota => "provider quota/billing cap: credit or allowance exhausted; retry requires account action",.authentication => "provider authentication failed: sign in again or check the API key",diff --git a/src/agent_turn.zig b/src/agent_turn.zigindex fba83aaa..7ad45209 100644--- a/src/agent_turn.zig+++ b/src/agent_turn.zig@@ -81,11 +81,7 @@ pub fn run(self: *Agent, root_turn_prepared: *std.atomic.Value(bool)) anyerror![if (try @import("agent_length_cutoff.zig").recoverRunaway(self, err, hist_len)) continue;return review_deadline.finish(try @import("agent_model_loop.zig").finishError(self, err));};- const stepped = try @import("agent_steps.zig").stepForWire(self, root);- if (stepped != null and self.completed == null and self.yielded == null) {- if (try @import("agent_length_cutoff.zig").handle(self, root, hist_len)) continue;- }- const done = try length.afterStep(self, root, stepped);+ const done = try length.step(self, root);if (done) |final_text| {if (self.yielded != null) return review_deadline.finish(final_text);if (@import("tool_call_repair.zig").endsTurn(self, final_text)) return review_deadline.finish(final_text); // final: no retry/nudgediff --git a/src/artifact_claim.zig b/src/artifact_claim.zigindex cc02a348..036fe967 100644--- a/src/artifact_claim.zig+++ b/src/artifact_claim.zig@@ -246,13 +246,13 @@ pub fn handleToolIn(arena: Allocator, io: Io, action: []const u8, kind_s: []cons.is_error = true,};const me = selfOwner(io);- const now: i64 = 0;+ if (std.mem.eql(u8, action, "status") and key.len == 0) return .{ .text = try @import("artifact_claim_inventory.zig").status(arena, io, ledger.slice(), me), .is_error = false };if (claim_ledger.ambiguous(ledger.slice(), kind, key, repo)) return .{ .text = "claim repository is ambiguous; specify repo explicitly", .is_error = true };const existing = claim_ledger.findIn(ledger.slice(), kind, key, repo) orelseif (kind == .publication) claim_stale.publicationRecord(ledger.slice(), key, repo) else null;const live = if (existing) |c| ownerLive(io, c.owner) else false;if (std.mem.eql(u8, action, "claim") or std.mem.eql(u8, action, "acquire")) {- const msg = claim_ledger.acquireIn(ledger, storage, kind, key, me, now, live, repo) catch |err| switch (err) {+ const msg = claim_ledger.acquireIn(ledger, storage, kind, key, me, 0, live, repo) catch |err| switch (err) {error.ClaimHeld => return .{ .text = refuseText(arena, kind, key, existing.?.owner), .is_error = true },else => |e| return .{ .text = try @import("artifact_claim_inventory.zig").acquireFail(arena, io, e, ledger.slice(), me), .is_error = true }, // #1614: at capacity, list what can be released};@@ -288,7 +288,7 @@ pub fn handleToolIn(arena: Allocator, io: Io, action: []const u8, kind_s: []cons.id => |id| id,else => receiver.start_id,};- const msg = claim_ledger.handoffIn(ledger, storage, kind, key, me, .{ .session = receiver.session_id, .pid = receiver.pid, .start_id = start_id }, now, live, repo) catch |err| switch (err) {+ const msg = claim_ledger.handoffIn(ledger, storage, kind, key, me, .{ .session = receiver.session_id, .pid = receiver.pid, .start_id = start_id }, 0, live, repo) catch |err| switch (err) {error.ClaimHeld => return .{ .text = "cannot hand off a claim you do not own", .is_error = true },error.NoClaim => return .{ .text = "no claim to hand off", .is_error = true },else => return .{ .text = "handoff failed", .is_error = true },@@ -302,7 +302,7 @@ pub fn handleToolIn(arena: Allocator, io: Io, action: []const u8, kind_s: []consconst v = claim_ledger.verdictIn(ledger.slice(), c.kind, c.key, me, live, c.repo orelse repo);return .{ .text = try std.fmt.allocPrint(arena, "claim {s} {s}: {s} owner=\"{s}\"", .{ @tagName(c.kind), c.key, @tagName(v), c.owner.session }), .is_error = false };}- return .{ .text = if (key.len == 0) try @import("artifact_claim_inventory.zig").status(arena, io, ledger.slice(), me) else "no claim", .is_error = false }; // #1614: keyless status lists the ledger+ return .{ .text = "no claim", .is_error = false };}return .{ .text = "action must be claim, release, handoff, or status", .is_error = true };}diff --git a/src/artifact_claim_inventory.zig b/src/artifact_claim_inventory.zigindex 88e9e585..63678e9b 100644--- a/src/artifact_claim_inventory.zig+++ b/src/artifact_claim_inventory.zig@@ -72,6 +72,23 @@ test "the inventory marks which claims the caller may release" {, text);}+test "keyless publication status lists every claim instead of resolving a single publication" {+ const claims = @import("artifact_claim.zig");+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const ar = arena.allocator();+ const io = std.testing.io;+ claims.resetForTest();+ defer claims.resetForTest();+ claims.setTestOwner(.{ .session = "owner", .pid = 11, .start_id = 1 });+ try std.testing.expect(!(try claims.handleTool(ar, io, "claim", "publication", "release", "")).is_error);+ try std.testing.expect(!(try claims.handleTool(ar, io, "claim", "issue", "12", "")).is_error);+ const result = try claims.handleTool(ar, io, "status", "publication", "", "");+ try std.testing.expect(std.mem.startsWith(u8, result.text, "2/32 claims held:"));+ try std.testing.expect(std.mem.indexOf(u8, result.text, "publication release") != null);+ try std.testing.expect(std.mem.indexOf(u8, result.text, "issue 12") != null);+}+test "a full ledger and a keyless status list the claims (#1614)" {const claims = @import("artifact_claim.zig");var arena = std.heap.ArenaAllocator.init(std.testing.allocator);diff --git a/src/auth_recovery.zig b/src/auth_recovery.zigindex df348a32..731a9be5 100644--- a/src/auth_recovery.zig+++ b/src/auth_recovery.zig@@ -14,9 +14,10 @@ pub fn forFailure(p: provider.Provider, code: ?[]const u8, message: []const u8)if (p.source != .login) return null;// A structured non-auth code wins over incidental auth words in a message.// Do not turn quota, server errors or expired request ids into login actions.+ // 401 is preserved by the HTTP adapter/envelope parser, not guessed from text.if (code) |c| {var auth = false;- for ([_][]const u8{ "invalid_token", "invalid_api_key", "authentication_error", "token_expired", "subscription_sharing_invalid_user", "subscription_sharing_v2_invalid_user" }) |known| {+ for ([_][]const u8{ "401", "invalid_token", "invalid_api_key", "authentication_error", "token_expired", "subscription_sharing_invalid_user", "subscription_sharing_v2_invalid_user" }) |known| {if (std.mem.eql(u8, c, known)) auth = true;}if (!auth) return null;diff --git a/src/cli.zig b/src/cli.zigindex 731a70c0..2e991fc1 100644--- a/src/cli.zig+++ b/src/cli.zig@@ -24,6 +24,12 @@ const update_target = @import("update_target.zig");pub const changelog_text =\\What's new\\──────────+ \\0.302.40+ \\ • stream recovery preserves partial replies across ACP and resume+ \\ • workspace skills in headless runs; actionable claim inventory+ \\ • MCP off-lists and JSON configuration listing; atomic companion updates+ \\ • stored conversation continuation and typed sign-in recovery+ \\\\0.302.39\\ • unattended runs recover from output limits and empty replies\\ • one-shot stream-json reports a resume handle, outcome and usagediff --git a/src/edit_batch.zig b/src/edit_batch.zigindex 2592ab4a..d71a7808 100644--- a/src/edit_batch.zig+++ b/src/edit_batch.zig@@ -313,14 +313,10 @@ test "execBatch and single edit share the same-file write lock" {const io = std.testing.io;var tmp = std.testing.tmpDir(.{});defer tmp.cleanup();- try tmp.dir.writeFile(io, .{- .sub_path = "slow",- .data = "#!/bin/sh\ndir=$(dirname \"$0\")\n: > \"$dir/entered\"\nwhile [ ! -e \"$dir/release\" ]; do /bin/sleep 0.01; done\nexit 1\n",- .flags = .{ .permissions = @fromBackingInt(@intCast(0o755)) },- });var real_buf: [std.fs.max_path_bytes]u8 = undefined;const real_len = try tmp.dir.realPath(io, &real_buf);- const stub = try std.fmt.allocPrint(gpa, "{s}/slow", .{real_buf[0..real_len]});+ // Force the native fallback without depending on a shell child starting.+ const stub = try std.fmt.allocPrint(gpa, "{s}/unavailable-companion", .{real_buf[0..real_len]});defer gpa.free(stub);const saved = edit_verify.companion_bin;defer edit_verify.companion_bin = saved;@@ -340,31 +336,83 @@ test "execBatch and single edit share the same-file write lock" {var client: std.http.Client = undefined;const ctx = edit_verify.testCtx(&client);+ const Probe = struct {+ lock: *Io.Mutex,+ progress: Io.Event = .unset,+ blocked: std.atomic.Value(bool) = .init(false),+ var single_probe: *@This() = undefined;+ var batch_probe: *@This() = undefined;++ fn wait(self: *@This(), data: ?*anyopaque, ptr: *const u32, expected: u32) void {+ if (ptr == @as(*const u32, @ptrCast(&self.lock.state.raw))) {+ self.blocked.store(true, .release);+ self.progress.set(std.testing.io);+ }+ std.testing.io.vtable.futexWaitUncancelable(data, ptr, expected);+ }+ fn waitSingle(data: ?*anyopaque, ptr: *const u32, expected: u32) void {+ single_probe.wait(data, ptr, expected);+ }+ fn waitBatch(data: ?*anyopaque, ptr: *const u32, expected: u32) void {+ batch_probe.wait(data, ptr, expected);+ }+ fn runSingle(c: ToolCtx, path: []const u8, abs: []const u8) !ToolOutput {+ // Also wake on early completion: deleting a lock must fail an+ // assertion, not leave the test waiting forever for contention.+ defer single_probe.progress.set(std.testing.io);+ return edit_verify.applyEdit(c, path, abs, "third", "THIRD", false);+ }+ fn runBatch(c: ToolCtx, value: Value) !ToolOutput {+ defer batch_probe.progress.set(std.testing.io);+ return execBatch(c, value);+ }+ };try tmp.dir.writeFile(io, .{ .sub_path = "f.txt", .data = "first\nsecond\nthird\n" });- var single = try io.concurrent(edit_verify.applyEdit, .{ ctx, rel, resolved, "third", "THIRD", false });- var entered = false;- for (0..500) |_| {- if (tmp.dir.statFile(io, "entered", .{})) |_| {- entered = true;- break;- } else |_| {}- try io.sleep(.fromMilliseconds(1), .awake);- }- if (!entered) try tmp.dir.writeFile(io, .{ .sub_path = "release", .data = "" });- try std.testing.expect(entered);+ const lock = edit_verify.lockPath(io, resolved);+ var locked = true;+ defer if (locked) lock.unlock(io);+ var single_probe: Probe = .{ .lock = lock };+ var batch_probe: Probe = .{ .lock = lock };+ Probe.single_probe = &single_probe;+ Probe.batch_probe = &batch_probe;+ var single_vtable = io.vtable.*;+ single_vtable.futexWaitUncancelable = Probe.waitSingle;+ var batch_vtable = io.vtable.*;+ batch_vtable.futexWaitUncancelable = Probe.waitBatch;+ var single_ctx = ctx;+ single_ctx.io.vtable = &single_vtable;+ var batch_ctx = ctx;+ batch_ctx.io.vtable = &batch_vtable;- var batch = try io.concurrent(execBatch, .{ ctx, input });- // The single edit is inside its companion call while holding the stripe.- // A batch that ignores that stripe would finish and change the file here.- try io.sleep(.fromMilliseconds(50), .awake);+ var single = try io.concurrent(Probe.runSingle, .{ single_ctx, rel, resolved });+ defer {+ if (locked) {+ lock.unlock(io);+ locked = false;+ }+ if (single.await(io)) |out| gpa.free(out.text) else |_| {}+ }+ var batch = try io.concurrent(Probe.runBatch, .{ batch_ctx, input });+ defer {+ if (locked) {+ lock.unlock(io);+ locked = false;+ }+ if (batch.await(io)) |out| gpa.free(out.text) else |_| {}+ }+ // Observe each writer actually waiting on THIS stripe, not just being+ // scheduled. No sleep or companion marker can prove that both contended.+ try single_probe.progress.wait(io);+ try batch_probe.progress.wait(io);+ try std.testing.expect(single_probe.blocked.load(.acquire));+ try std.testing.expect(batch_probe.blocked.load(.acquire));const while_blocked = try tmp.dir.readFileAlloc(io, "f.txt", gpa, .limited(4096));defer gpa.free(while_blocked);- try tmp.dir.writeFile(io, .{ .sub_path = "release", .data = "" });+ try std.testing.expectEqualStrings("first\nsecond\nthird\n", while_blocked);+ lock.unlock(io);+ locked = false;const single_result = try single.await(io);- defer gpa.free(single_result.text);const batch_result = try batch.await(io);- defer gpa.free(batch_result.text);- try std.testing.expectEqualStrings("first\nsecond\nthird\n", while_blocked);try std.testing.expect(!single_result.is_error);try std.testing.expect(!batch_result.is_error);const after = try tmp.dir.readFileAlloc(io, "f.txt", gpa, .limited(4096));diff --git a/src/interactions_steps.zig b/src/interactions_steps.zigindex a92e4c04..b7a8d3bc 100644--- a/src/interactions_steps.zig+++ b/src/interactions_steps.zig@@ -152,71 +152,6 @@ pub fn modelOutput(arena: Allocator, text: []const u8) !Value {return .{ .object = obj };}-/// Rewrite chat-shaped text turns (`{"role":"user"|"assistant","content":…}`)-/// as steps. Several shared paths append those (the REPL/--json/ACP user turn,-/// interrupt markers, the compaction request), and the endpoint rejects the-/// whole request on one ("use step_list input format instead of turn_list").-/// Runs before every send (history_wire.prepare); a converted turn stays-/// converted, so the history a stored Interaction was built from is stable.-/// Empty turns are dropped: an empty text part is rejected too.-pub fn normalizeHistory(arena: Allocator, history: *std.json.Array) void {- var changed = false;- for (history.items) |item| if (chatTurn(item) != null) {- changed = true;- break;- };- if (!changed) return;- var out = std.json.Array.init(arena);- out.ensureTotalCapacity(history.items.len) catch return;- for (history.items) |item| {- const role = chatTurn(item) orelse {- out.appendAssumeCapacity(item);- continue;- };- const text = turnText(arena, item.object.get("content")) catch return;- if (std.mem.trim(u8, text, " \t\r\n").len == 0) continue;- var step_v = (if (std.mem.eql(u8, role, "assistant")) modelOutput(arena, text) else userInput(arena, text)) catch return;- const origin = @import("session_wake.zig").origin_key;- if (item.object.get(origin)) |o| step_v.object.put(arena, origin, o) catch return;- out.appendAssumeCapacity(step_v);- }- history.* = out;-}--/// The role of a chat-shaped user/assistant text turn, or null for anything-/// else (steps, tool messages, assistant turns carrying tool calls).-fn chatTurn(item: Value) ?[]const u8 {- if (item != .object) return null;- if (item.object.get("type") != null) return null;- if (item.object.get("tool_calls") != null) return null;- const role = item.object.get("role") orelse return null;- if (role != .string) return null;- if (!std.mem.eql(u8, role.string, "user") and !std.mem.eql(u8, role.string, "assistant")) return null;- const content = item.object.get("content") orelse return null;- if (content == .string) return role.string;- if (content != .array) return null;- for (content.array.items) |part| {- if (part != .object) return null;- const t = part.object.get("type") orelse return null;- if (t != .string) return null;- if (!std.mem.eql(u8, t.string, "text") and !std.mem.eql(u8, t.string, "input_text") and !std.mem.eql(u8, t.string, "output_text")) return null;- }- return role.string;-}--fn turnText(arena: Allocator, content: ?Value) ![]const u8 {- const c = content orelse return "";- if (c == .string) return c.string;- var out: std.ArrayList(u8) = .empty;- for (c.array.items) |part| {- const t = part.object.get("text") orelse continue;- if (t != .string) continue;- if (out.items.len > 0) try out.append(arena, '\n');- try out.appendSlice(arena, t.string);- }- return out.items;-}-/// One turn on the Interactions wire: echo the model's steps into history,/// dispatch any function_call steps, and answer with the visible text once the/// model stops calling tools. Mirrors stepOpenAI / stepResponses.@@ -280,15 +215,22 @@ pub fn step(self: *@import("agent.zig").Agent, root: std.json.ObjectMap) !?[]con/// A user message becomes a user_input step and assistant text a model_output/// step; content parts keep only the text and image shapes this wire takes.pub fn normalizeHistory(a: Allocator, history: *std.json.Array) void {- for (history.items) |*item| {- if (item.* != .object or item.object.get("type") != null) continue;- const role = item.object.get("role") orelse continue;- const content = item.object.get("content") orelse continue;- if (role != .string) continue;- const user = std.mem.eql(u8, role.string, "user");- if (!user and !std.mem.eql(u8, role.string, "assistant")) continue;- item.* = roleStep(a, user, content, item.object.get(@import("session_wake.zig").origin_key)) catch continue;+ var kept: usize = 0;+ for (history.items) |item| {+ const normalized = normalize: {+ if (item != .object or item.object.get("type") != null) break :normalize item;+ const role = item.object.get("role") orelse break :normalize item;+ const content = item.object.get("content") orelse break :normalize item;+ if (role != .string) break :normalize item;+ const user = std.mem.eql(u8, role.string, "user");+ if (!user and !std.mem.eql(u8, role.string, "assistant")) break :normalize item;+ if (content == .string and std.mem.trim(u8, content.string, " \t\r\n").len == 0) continue;+ break :normalize roleStep(a, user, content, item.object.get(@import("session_wake.zig").origin_key)) catch item;+ };+ history.items[kept] = normalized;+ kept += 1;}+ history.shrinkRetainingCapacity(kept);}fn roleStep(a: Allocator, user: bool, content: Value, origin: ?Value) !Value {diff --git a/src/mcp_config.zig b/src/mcp_config.zigindex 2eb15825..ca2101ad 100644--- a/src/mcp_config.zig+++ b/src/mcp_config.zig@@ -59,8 +59,6 @@ pub const Merged = struct {/// still loads: one bad file must not disable MCP everywhere.invalid_project: bool = false,invalid_global: bool = false,- /// The global file's own `mcpServers`, to tell its entries from imported ones.- global: std.json.ObjectMap = .empty,/// Each file's `disabledMcpServers`: names graff leaves out, even when/// another tool's config supplies them. An entry may keep its full config/// (a client switched a server off) or be `{}` (an imported name).@@ -164,8 +162,8 @@ pub const off_key = "disabledMcpServers";/// config handling.////// `global_is_override` is true only when `GRAFF_MCP_CONFIG` selected the-/// path. A file that exists, parses, and lists no servers is then a wholesale-/// off-switch (pty/harness): the project `.mcp.json` and plugin trees do not+/// path. A file that exists, parses, and has empty server and off-list tables+/// is then a wholesale off-switch: the project `.mcp.json` and plugin trees do not/// merge in. A *populated* override still merges, so relocating the global/// file is unchanged. A missing or invalid override is not an off-switch.pub fn load(io: Io, arena: Allocator, dir: Io.Dir, project_path: []const u8, global_path: ?[]const u8, home: []const u8, global_is_override: bool) Merged {@@ -180,9 +178,8 @@ pub fn load(io: Io, arena: Allocator, dir: Io.Dir, project_path: []const u8, glo// #549: existing + valid + empty override → MCP stays off. `found` is// already true (the file existed), so Registry.init does not treat this// as "no config" and then pick up a project file on a later read.- if (global_is_override and merged.found and !merged.invalid_global and global.count() == 0)+ if (global_is_override and merged.found and !merged.invalid_global and global.count() == 0 and merged.global_off.count() == 0)return merged;- merged.global = global;const project_tables = readServers(io, arena, dir, project_path, &merged.found, &merged.invalid_project);merged.project = project_tables.servers;@@ -208,11 +205,12 @@ pub fn load(io: Io, arena: Allocator, dir: Io.Dir, project_path: []const u8, gloreturn merged;}-/// Take `name` out of the merged set, keeping the config it had (or the-/// off-list's own entry when nothing supplied one) for listings.+/// Take `name` out of the merged set. A full off-list entry wins over lower+/// priority config, including an earlier off-list; `{}` keeps the prior config.fn switchOff(arena: Allocator, merged: *Merged, name: []const u8, off_entry: Value) void {- const config = if (merged.servers.fetchOrderedRemove(name)) |kv| kv.value else off_entry;- if (merged.disabled.get(name) == null) merged.disabled.put(arena, name, config) catch {};+ const removed = merged.servers.fetchOrderedRemove(name);+ const config = if (hasConfig(off_entry)) off_entry else if (removed) |kv| kv.value else merged.disabled.get(name) orelse off_entry;+ merged.disabled.put(arena, name, config) catch {};}/// Name every config file that existed but could not be used. A file graff@@ -417,6 +415,7 @@ test "off-lists take servers out of the merge, imported names included" {try testing.expect(merged.servers.get("parked") == null);try testing.expectEqualStrings("https://g/mcp", merged.disabled.get("docs").?.object.get("url").?.string);try testing.expectEqualStrings("/bin/parked", merged.disabled.get("parked").?.object.get("command").?.string);+ try testing.expectEqualStrings("/bin/kept", merged.disabled.get("kept").?.object.get("command").?.string);try testing.expect(merged.disabled.get("local") == null);try testing.expectEqual(Merged.Source.global, merged.source("docs"));try testing.expectEqual(Merged.Source.global, merged.source("parked"));@@ -424,6 +423,84 @@ test "off-lists take servers out of the merge, imported names included" {try testing.expectEqual(Merged.Source.imported, merged.source("from-elsewhere"));}+test "a disabled-only GRAFF_MCP_CONFIG override preserves project servers and lists disabled entries (#1589)" {+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena_state.deinit();+ var tmp = testing.tmpDir(.{});+ defer tmp.cleanup();+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "global.json", .data =+ \\{"disabledMcpServers":{"parked":{"command":"/bin/parked"}}}+ });+ try tmp.dir.writeFile(testing.io, .{ .sub_path = ".mcp.json", .data =+ \\{"mcpServers":{"local":{"command":"./srv"}}}+ });++ const merged = loadTmpOverride(arena_state.allocator(), tmp.dir);+ try testing.expect(merged.found and !merged.invalid_global and !merged.invalid_project);+ try testing.expectEqual(@as(usize, 1), merged.servers.count());+ try testing.expectEqualStrings("./srv", merged.servers.get("local").?.object.get("command").?.string);+ try testing.expect(merged.project.get("local") != null);+ try testing.expect(merged.servers.get("parked") == null);+ try testing.expectEqual(@as(usize, 1), merged.disabled.count());+ try testing.expectEqualStrings("/bin/parked", merged.disabled.get("parked").?.object.get("command").?.string);+ try testing.expectEqual(Merged.Source.global, merged.source("parked"));+}++test "full project disabled entries beat enabled and already-disabled global config (#1589)" {+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena_state.deinit();+ const globals = [_][]const u8{+ \\{"mcpServers":{"docs":{"url":"https://global.example/mcp"}}}+ ,+ \\{"disabledMcpServers":{"docs":{"url":"https://global-off.example/mcp"}}}+ ,+ \\{"mcpServers":{"docs":{"url":"https://global.example/mcp"}},"disabledMcpServers":{"docs":{}}}+ ,+ };+ for (globals) |global_config| {+ var tmp = testing.tmpDir(.{});+ defer tmp.cleanup();+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "global.json", .data = global_config });+ try tmp.dir.writeFile(testing.io, .{ .sub_path = ".mcp.json", .data =+ \\{"disabledMcpServers":{"docs":{"command":"./project-docs","args":["--local"]}}}+ });++ const merged = loadTmp(arena_state.allocator(), tmp.dir);+ try testing.expectEqual(@as(usize, 0), merged.servers.count());+ try testing.expectEqual(@as(usize, 1), merged.disabled.count());+ const config = merged.disabled.get("docs").?.object;+ try testing.expectEqualStrings("./project-docs", config.get("command").?.string);+ try testing.expectEqualStrings("--local", config.get("args").?.array.items[0].string);+ try testing.expect(config.get("url") == null);+ try testing.expectEqual(Merged.Source.project, merged.source("docs"));+ }+}++test "project disabled tombstones retain enabled or already-disabled global config (#1589)" {+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena_state.deinit();+ const globals = [_][]const u8{+ \\{"mcpServers":{"docs":{"command":"/bin/global-docs"}}}+ ,+ \\{"disabledMcpServers":{"docs":{"command":"/bin/global-docs"}}}+ ,+ };+ for (globals) |global_config| {+ var tmp = testing.tmpDir(.{});+ defer tmp.cleanup();+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "global.json", .data = global_config });+ try tmp.dir.writeFile(testing.io, .{ .sub_path = ".mcp.json", .data =+ \\{"disabledMcpServers":{"docs":{}}}+ });++ const merged = loadTmp(arena_state.allocator(), tmp.dir);+ try testing.expectEqual(@as(usize, 0), merged.servers.count());+ try testing.expectEqual(@as(usize, 1), merged.disabled.count());+ try testing.expectEqualStrings("/bin/global-docs", merged.disabled.get("docs").?.object.get("command").?.string);+ try testing.expectEqual(Merged.Source.global, merged.source("docs"));+ }+}+test "a malformed off-list leaves the servers alone" {var arena_state = std.heap.ArenaAllocator.init(testing.allocator);defer arena_state.deinit();