Skip to content

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

Python

fix(codex): never pair a stale response id with an advanced boundary (#194)

justrach2d72e51530ce
diff --git a/scripts/eval/tier1-manifest.json b/scripts/eval/tier1-manifest.json
index 99ebbbdd..0fbcd926 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": 502,
+ "test_count_baseline": 503,
"required_invariants": [
{
"id": "goal-epochs",
diff --git a/src/agent_steps.zig b/src/agent_steps.zig
index c9117e5f..352cbbca 100644
--- a/src/agent_steps.zig
+++ b/src/agent_steps.zig
@@ -82,12 +82,25 @@ pub fn stepResponses(self: *Agent, response: std.json.ObjectMap) !?[]const u8 {
// after this point - the tool results below, and on the following USER turn
// that turn's prompt, since the chain outlives runTurn now. Only while the WS
// session lives; codex_chain.record stamps what the anchor is valid for.
+ // #194: the watermark and the id it is keyed to must move TOGETHER. Advancing
+ // codex_sent_upto while codex_prev_id still points at the PREVIOUS response
+ // makes the next delta start after items the server never received, silently
+ // dropping them from the conversation. A response carrying no usable id (or a
+ // failed dupe) drops the anchor instead; the socket stays and the next request
+ // simply re-sends full input.
if (self.codex_ws != null) {
- codex_chain.record(self);
- if (response.get("id")) |idv| if (idv == .string and idv.string.len > 0) {
+ const fresh: ?[]const u8 = if (response.get("id")) |idv|
+ (if (idv == .string and idv.string.len > 0) self.gpa.dupe(u8, idv.string) catch null else null)
+ else
+ null;
+ if (fresh) |id| {
if (self.codex_prev_id) |old| self.gpa.free(old);
- self.codex_prev_id = self.gpa.dupe(u8, idv.string) catch null;
- };
+ self.codex_prev_id = id;
+ codex_chain.record(self);
+ } else if (self.codex_prev_id) |old| {
+ self.gpa.free(old);
+ self.codex_prev_id = null;
+ }
}
if (calls.items.len > 0) {
diff --git a/src/agent_ws_test.zig b/src/agent_ws_test.zig
index 999d6791..ce815861 100644
--- a/src/agent_ws_test.zig
+++ b/src/agent_ws_test.zig
@@ -191,6 +191,59 @@ test "buildBody (.responses): a cross-turn delta re-anchors when history or sett
try std.testing.expect(std.mem.indexOf(u8, after_clear, "second turn prompt") == null); // cleared, not resurrected
}
+// #194: a response with no usable `id` must not leave a STALE previous_response_id
+// paired with an ADVANCED delta boundary. That combination makes the next request
+// send `previous_response_id: <old>` plus a delta starting after items the server
+// never received, so those items vanish from the conversation with no error. The
+// window used to be one turn (runTurn tore the session down); now that the chain
+// spans turns, the anchor has to be dropped explicitly.
+test "stepResponses (#194): a response with no id drops the anchor instead of advancing it" {
+ 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"));
+
+ var dummy_ws: ws.WsClient = undefined;
+ 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 = true, // suppress the unstreamed-text surface; irrelevant here
+ .label = "",
+ .out = null,
+ .codex_ws = &dummy_ws,
+ .codex_prev_id = try std.testing.allocator.dupe(u8, "resp_old"),
+ };
+ codex_chain.record(&agent); // anchored: server holds messages[0..1] under resp_old
+ try std.testing.expectEqual(@as(usize, 1), agent.codex_sent_upto);
+
+ // A turn appends work, then a response arrives WITHOUT an id.
+ try agent.messages.append(try textMessage(a, "user", "work the server must see"));
+ var no_id: std.json.ObjectMap = .empty;
+ try no_id.put(a, "output", .{ .array = std.json.Array.init(a) });
+ _ = try agent.stepResponses(no_id);
+
+ // The anchor is gone, so the next request re-sends everything. Before the
+ // fix the watermark advanced to 2 while prev_id stayed "resp_old", and the
+ // "work the server must see" message was never transmitted.
+ try std.testing.expect(agent.codex_prev_id == null);
+ try std.testing.expect(!codex_chain.chainUsable(&agent));
+
+ // A response WITH an id anchors normally: id and watermark move together.
+ var with_id: std.json.ObjectMap = .empty;
+ try with_id.put(a, "output", .{ .array = std.json.Array.init(a) });
+ try with_id.put(a, "id", .{ .string = "resp_new" });
+ _ = try agent.stepResponses(with_id);
+ defer std.testing.allocator.free(agent.codex_prev_id.?);
+ try std.testing.expectEqualStrings("resp_new", agent.codex_prev_id.?);
+ try std.testing.expectEqual(agent.messages.items.len, agent.codex_sent_upto);
+}
+
// 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