Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

fix(codex): drop the chain on resume and before compaction

justrach62cba3ac65a2
diff --git a/scripts/eval/tier1-manifest.json b/scripts/eval/tier1-manifest.json
index 0fbcd926..3ecaca26 100644
--- a/scripts/eval/tier1-manifest.json
+++ b/scripts/eval/tier1-manifest.json
@@ -10,7 +10,7 @@
"src/main.zig",
"src/repl.zig"
],
- "test_count_baseline": 503,
+ "test_count_baseline": 504,
"required_invariants": [
{
"id": "goal-epochs",
diff --git a/src/agent_compact.zig b/src/agent_compact.zig
index 42161a68..90bafad1 100644
--- a/src/agent_compact.zig
+++ b/src/agent_compact.zig
@@ -98,6 +98,15 @@ pub fn summaryResponseComplete(self: *Agent, root: std.json.ObjectMap) bool {
/// compact() cannot drop the call without losing the number it prints, so the
/// pin cannot be orphaned by a tidy-up that looks harmless.
pub fn compactPrelude(self: *Agent) ?usize {
+ // Drop the codex chain BEFORE the summary request. runTurn used to bracket
+ // every compaction with closeCodexWs; now that the chain spans user turns,
+ // the BETWEEN-turn callers (mainloop's two compactOrRecover sites) have no
+ // such bracket. compact() sets stream_quiet, so its summary request goes out
+ // over codex HTTP - which rejects previous_response_id outright - and
+ // recentContextStart can leave summary_messages LONGER than codex_sent_upto,
+ // so the length guard would not catch it. history_rewrites covers the state
+ // AFTER compaction; this covers the request compaction itself makes.
+ self.closeCodexWs();
if (self.messages.items.len == 0) return null;
pinChildTask(self);
self.last_request_context_overflow = false;
diff --git a/src/agent_compact_test_support.zig b/src/agent_compact_test_support.zig
index a09301eb..45f45ac4 100644
--- a/src/agent_compact_test_support.zig
+++ b/src/agent_compact_test_support.zig
@@ -27,6 +27,11 @@ pub fn subAgent(a: std.mem.Allocator, sub: bool) Agent {
// compactPrelude reports the token figure compact() prints, so the estimate
// path has to be reachable: system prompt, tool json, provider context and
// the two meter anchors it reads.
+ // compactPrelude now drops the codex chain before compaction's own request,
+ // so the fields closeCodexWs reads have to be real even off the codex path.
+ agent.codex_ws = null;
+ agent.codex_prev_id = null;
+ agent.codex_sent_upto = 0;
agent.sys_override = null;
agent.sys_normal = "";
agent.sys_strict = "";
diff --git a/src/agent_ws_test.zig b/src/agent_ws_test.zig
index ce815861..2a3f0ce1 100644
--- a/src/agent_ws_test.zig
+++ b/src/agent_ws_test.zig
@@ -6,6 +6,7 @@ const textMessage = @import("messages.zig").textMessage;
const ws = @import("ws.zig");
const agent_ws = @import("agent_ws.zig");
const codex_chain = @import("codex_chain.zig");
+const agent_compact = @import("agent_compact.zig");
test "closeCodexWs resets the delta session state + frees the response id (codex-ws)" {
var agent: Agent = undefined;
@@ -244,6 +245,51 @@ test "stepResponses (#194): a response with no id drops the anchor instead of ad
try std.testing.expectEqual(agent.messages.items.len, agent.codex_sent_upto);
}
+// runTurn used to bracket every compaction with closeCodexWs, so compact()'s own
+// summary request could never carry a delta. With the chain spanning user turns
+// that bracket is gone from the BETWEEN-turn callers (mainloop's two
+// compactOrRecover sites), and compact() sets stream_quiet -> its request goes
+// over codex HTTP, which rejects previous_response_id outright. The length guard
+// does not save it either: recentContextStart can leave the summary history
+// LONGER than codex_sent_upto. compactPrelude drops the anchor instead.
+test "compactPrelude drops the codex chain before compaction's own request" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+
+ var msgs = std.json.Array.init(a);
+ try msgs.append(try textMessage(a, "user", "first"));
+ try msgs.append(try textMessage(a, "assistant", "second"));
+
+ var agent: Agent = .{
+ .gpa = std.testing.allocator,
+ .arena = a,
+ .io = std.testing.io,
+ .client = undefined,
+ .provider = .{ .id = "codex", .kind = .responses, .auth = .bearer, .url = "https://x/responses", .api_key = "k", .model = "gpt-5.6", .context = 270_000 },
+ .messages = msgs,
+ .sub = false,
+ .label = "",
+ .out = null,
+ .codex_ws = null, // no live socket to deinit; the anchor is what matters
+ .codex_prev_id = try std.testing.allocator.dupe(u8, "resp_live"),
+ };
+ codex_chain.record(&agent);
+
+ _ = agent_compact.compactPrelude(&agent);
+
+ // Anchor gone, so compaction's summary request carries full input and no
+ // previous_response_id - which the codex HTTP endpoint would reject anyway.
+ try std.testing.expect(agent.codex_prev_id == null);
+ try std.testing.expectEqual(@as(usize, 0), agent.codex_sent_upto);
+ try std.testing.expect(!codex_chain.chainUsable(&agent));
+
+ const body = try agent.buildBody(null, false, false, false);
+ defer std.testing.allocator.free(body);
+ try std.testing.expect(std.mem.indexOf(u8, body, "previous_response_id") == null);
+ try std.testing.expect(std.mem.indexOf(u8, body, "\"first\"") != null);
+}
+
// Regression (#codex gpt-5.6): the Codex Responses backend rejects a top-level
// `max_output_tokens` ("Unsupported parameter: max_output_tokens"), which hard-
// failed EVERY turn including the title task. openai/codex never sends it at the
diff --git a/src/session.zig b/src/session.zig
index 01dffb66..fa45283e 100644
--- a/src/session.zig
+++ b/src/session.zig
@@ -486,6 +486,9 @@ pub fn loadSession(root: *Agent, keys: *Keys, arena: Allocator, name: []const u8
root.effort_rejected = false;
root.ws_off = false; // transport failures belong to the prior live session
root.ws_transport_failures = 0;
+ // A restored history is a DIFFERENT conversation, not an extension, and
+ // codex_chain.usable detects no identity swap - it would chain onto the old one.
+ root.closeCodexWs();
root.compact_transport_failures = 0;
root.last_usage_includes_output = false;
root.last_request_context_overflow = false;