graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Expand opaque compaction state on a provider or model switch
justrach70c7ec9e8bb8
diff --git a/src/agent_encrypted_recovery.zig b/src/agent_encrypted_recovery.zigindex bb69c489..0d75018c 100644--- a/src/agent_encrypted_recovery.zig+++ b/src/agent_encrypted_recovery.zig@@ -10,12 +10,32 @@ pub fn recover(self: *Agent, message: []const u8, code: ?[]const u8, restores: *if (!window.rejectedByProvider(message, code) or restores.* >= max_restores) return false;if (!(checkpoint.restore(self) catch false)) return false;restores.* += 1;+ settleAfterRestore(self, "restored exact pre-compaction input; subsequent conversation retained");+ return true;+}++/// A blob decrypts only where it was minted, so carrying one across a provider+/// or model switch guarantees a rejection. codex binds a compaction to the+/// model that produced it and re-compacts when that hash changes+/// (`compact_remote_v2_installation.rs`, `CompactionReason::CompHashChanged`);+/// reaching the same end state from exact retained input keeps the history+/// portable without replaying foreign ciphertext.+pub fn expandForSeatChange(self: *Agent) bool {+ if (window.latestBlob(self.messages.items) == null) return false;+ if (!(checkpoint.restore(self) catch false)) return false;+ settleAfterRestore(self, "expanded opaque compaction state before a provider or model switch; subsequent conversation retained");+ // The expanded history is real again: drop the fingerprint, or re-derive it+ // if the restored input itself still ends in an opaque item.+ self.compaction_window = window.State.canonical(self.gpa, self.messages.items) catch .{};+ return true;+}++fn settleAfterRestore(self: *Agent, note: []const u8) void {self.closeCodexWs();self.last_context_tokens = 0;self.context_local_tokens = 0;self.last_usage_includes_output = false;self.goal_note_fp = 0;self.history_rewrites +%= 1;- if (self.tracer) |tr| tr.note("encrypted_state_recovery", "restored exact pre-compaction input; subsequent conversation retained");- return true;+ if (self.tracer) |tr| tr.note("encrypted_state_recovery", note);}diff --git a/src/agent_ws_fallback_test.zig b/src/agent_ws_fallback_test.zigindex 3963ea55..601f8563 100644--- a/src/agent_ws_fallback_test.zig+++ b/src/agent_ws_fallback_test.zig@@ -56,7 +56,7 @@ test "#427: a 426 latches SSE at once — no retry burned, and the turn is servevar dials: std.atomic.Value(u8) = .init(0);var sse: std.atomic.Value(u8) = .init(0);var done: std.atomic.Value(bool) = .init(false);- var fut = io.async(mock.refuseUpgrade, .{+ var fut = try io.concurrent(mock.refuseUpgrade, .{io, &server, @as([]const u8, "426 Upgrade Required"), @as(u8, 1), &dials, &sse, &done,});defer fut.await(io);@@ -135,7 +135,7 @@ test "#427: a non-426 handshake failure keeps the ladder's free retry, then latcvar dials: std.atomic.Value(u8) = .init(0);var sse: std.atomic.Value(u8) = .init(0);var done: std.atomic.Value(bool) = .init(false);- var fut = io.async(mock.refuseUpgrade, .{+ var fut = try io.concurrent(mock.refuseUpgrade, .{io, &server, @as([]const u8, "500 Internal Server Error"), @as(u8, 2), &dials, &sse, &done,});defer fut.await(io);diff --git a/src/compaction_checkpoint_tests.zig b/src/compaction_checkpoint_tests.zigindex 20debe80..51d01028 100644--- a/src/compaction_checkpoint_tests.zig+++ b/src/compaction_checkpoint_tests.zig@@ -374,3 +374,41 @@ test "compaction checkpoint allocation failure never installs partial history" {try std.testing.expectEqual(ptr, agent.messages.items.ptr);try expectJson(window_json, agent.messages.items);}++test "a seat switch expands opaque state instead of replaying foreign ciphertext" {+ var f = try Fixture.init();+ defer f.deinit();+ var agent = f.agent();+ agent.codex_ws = null;+ agent.codex_prev_id = null;+ agent.tracer = null;+ agent.history_rewrites = 0;+ const recovery = @import("agent_encrypted_recovery.zig");++ const before = try f.values("[{\"role\":\"user\",\"content\":\"exact input\"}]");+ const window = try f.values(window_json);+ try checkpoint.preserve(&agent, before, window);+ try agent.messages.appendSlice(window);+ try agent.messages.appendSlice(try f.values("[{\"role\":\"user\",\"content\":\"later\"}]"));++ try std.testing.expect(recovery.expandForSeatChange(&agent));+ try expectJson("[{\"role\":\"user\",\"content\":\"exact input\"},{\"role\":\"user\",\"content\":\"later\"}]", agent.messages.items);+ // The window no longer describes the history, so it must not be replayed.+ try std.testing.expect(agent.compaction_window.canonical_blob == null);+ // Nothing opaque left: the guard is idempotent on the expanded history.+ try std.testing.expect(!recovery.expandForSeatChange(&agent));+}++test "a seat switch without a checkpoint leaves history and window untouched" {+ var f = try Fixture.init();+ defer f.deinit();+ var agent = f.agent();+ agent.codex_ws = null;+ agent.codex_prev_id = null;+ agent.tracer = null;+ agent.history_rewrites = 0;+ try agent.messages.appendSlice(try f.values(window_json));++ try std.testing.expect(!@import("agent_encrypted_recovery.zig").expandForSeatChange(&agent));+ try expectJson(window_json, agent.messages.items);+}diff --git a/src/providers.zig b/src/providers.zigindex 1ad90fe9..2d764016 100644--- a/src/providers.zig+++ b/src/providers.zig@@ -73,6 +73,15 @@ pub fn applyProviderInner(root: *Agent, arena: Allocator, p: Provider, persist:note = "history cleared — /keepcontext on to carry it across formats";}}+ // A compaction blob decrypts only on the seat that minted it, so a switch+ // must not hand it to a different provider or model. Expand it back into+ // real items from the exact checkpoint first; without one the request is+ // rejected and the bounded recovery path reports the blocked state.+ const seat_moved = !same_format or+ !std.mem.eql(u8, root.provider.id, p.id) or+ !std.mem.eql(u8, root.provider.model, p.model);+ if (seat_moved and @import("agent_encrypted_recovery.zig").expandForSeatChange(root))+ note = try std.fmt.allocPrint(arena, "{s}; opaque state expanded from checkpoint", .{note});// ADR 0256: attempt_completion's description follows the model family.const gemini = @import("gemini_turns.zig");if (gemini.family(root.provider.model) != gemini.family(p.model)) root.invalidateRootTools();