Skip to content

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

Python

Settle a failed turn so resending its prompt makes a new request

Rach Pradhan6c14f36865e3
diff --git a/evals/harness_behavior.jsonl b/evals/harness_behavior.jsonl
index 0ca0ae9c..e13f96f8 100644
--- a/evals/harness_behavior.jsonl
+++ b/evals/harness_behavior.jsonl
@@ -158,3 +158,4 @@
{"why": "#1701: custom root prompts must retain issue lifecycle guidance; scripted policy-delivery coverage, not live-model judgment.", "scripted_only": true, "prompt": "Prepare a generic fixture report; do not publish anything.", "script": [{"text": "Fixture report prepared locally."}], "assert": [{"request_contains": {"index": 0, "text": "A comment alone does not complete a recurrence report."}}, {"request_contains": {"index": 0, "text": "CUSTOM_ROOT"}}, {"final_text_contains": "Fixture report prepared locally."}], "id": "issue-policy-custom-root-startup", "args": ["--system-prompt", "CUSTOM_ROOT"]}
{"why": "#1701: custom root prompts must retain issue lifecycle guidance; scripted policy-delivery coverage, not live-model judgment.", "scripted_only": true, "prompt": "Prepare a generic fixture report; do not publish anything.", "script": [{"text": "Fixture report prepared locally."}], "assert": [{"request_contains": {"index": 0, "text": "A comment alone does not complete a recurrence report."}}, {"request_contains": {"index": 0, "text": "CUSTOM_ROOT"}}, {"final_text_contains": "Fixture report prepared locally."}], "id": "issue-policy-custom-root-replaced", "controls_before_prompt": [{"type": "set_system_prompt", "text": "CUSTOM_ROOT"}]}
{"id": "dependency-symlink-confinement", "why": "#1748: native reads and directory listings must accept in-checkout dependency links while still refusing symlink escapes.", "scripted_only": true, "workspace_seed": {"node_modules/.store/pkg/docs/guide.md": "synthetic dependency guide\n"}, "prompt": "Read and list the synthetic linked dependency, then check that the escape link is refused.", "script": [{"tool": "bash", "arguments": {"command": "ln -s .store/pkg node_modules/pkg && ln -s / escape"}}, {"tool": "read_file", "arguments": {"path": "node_modules/pkg/docs/guide.md"}}, {"tool": "codedb", "arguments": {"command": "list_dir node_modules/pkg"}}, {"tool": "read_file", "arguments": {"path": "escape/no-such-synthetic-file"}}, {"text": "Dependency links accepted; escape refused."}], "assert": [{"request_contains": {"index": 2, "text": "synthetic dependency guide"}}, {"request_contains": {"index": 3, "text": "- node_modules/pkg/"}}, {"request_contains": {"index": 3, "text": "guide.md"}}, {"request_contains": {"index": 4, "text": "outside the working directory"}}, {"event": {"type": "tool_call_finished", "name": "read_file", "is_error": false}}, {"event": {"type": "tool_call_finished", "name": "codedb", "is_error": false}}, {"event": {"type": "tool_call_finished", "name": "read_file", "is_error": true}}, {"final_text_contains": "Dependency links accepted; escape refused."}]}
+{"id": "retry-after-failed-request-1729", "why": "#1729: a turn that failed before any reply left its prompt as the last history row, so resending the identical prompt read as a host replay and finished without a request. The failed turn must leave a boundary row so the retry makes a new request that carries it.", "args": [], "prompt": "Reply with retry ready.", "second_prompt": "Reply with retry ready.", "script": [{"http_status": 418, "error": "scripted outage"}, {"text": "retry ready"}], "assert": [{"request_count": 2}, {"request_contains": {"index": 1, "text": "[request failed:"}}, {"final_text_contains": "retry ready"}]}
diff --git a/src/acp_live_turn.zig b/src/acp_live_turn.zig
index 84d5a02a..3af3b240 100644
--- a/src/acp_live_turn.zig
+++ b/src/acp_live_turn.zig
@@ -222,6 +222,8 @@ pub const LiveTurn = struct {
defer output_lock.unlock(self.root.io);
try proto.writeSessionUpdate(self.out, self.session_id, marker);
}
+ // #1729: same row as the REPL, so resending this prompt is a new ask.
+ try @import("turn_dedup.zig").settleFailedTurn(self.root, arena, err);
// #753: an API interruption is a failed turn, not a dead ACP
// process. Save so the next prompt (and a respawn --resume) still
// sees the tool results and the background-agent ledger.
diff --git a/src/mainloop.zig b/src/mainloop.zig
index 3f64a6d9..b2179877 100644
--- a/src/mainloop.zig
+++ b/src/mainloop.zig
@@ -498,6 +498,7 @@ pub fn run(ctx: *Ctx) !void {
if (recovery_meter >= ctx.root.provider.compactAt()) {
ctx.root.autocompact(recovery_meter);
}
+ try @import("turn_dedup.zig").settleFailedTurn(ctx.root, ctx.arena, err); // #1729: a retry of this prompt is a new ask
session.saveSession(ctx.root, ctx.arena, ctx.root.session_name) catch {};
continue;
},
@@ -508,6 +509,7 @@ pub fn run(ctx: *Ctx) !void {
} else {
ctx.root.say("[turn aborted: {t}]\n", .{e}) catch {};
}
+ try @import("turn_dedup.zig").settleFailedTurn(ctx.root, ctx.arena, e);
session.saveSession(ctx.root, ctx.arena, ctx.root.session_name) catch {};
continue;
},
diff --git a/src/oneshot.zig b/src/oneshot.zig
index 748dd47b..b75de72e 100644
--- a/src/oneshot.zig
+++ b/src/oneshot.zig
@@ -70,6 +70,7 @@ pub fn runOneshotPrompt(gpa: Allocator, io: Io, arena: Allocator, root: *agent_m
if (telemetry.g_telem) |t| t.flush();
if (tracer.behavior) |behavior| behavior.finish(.failed);
pricing.printUsageFooter(io);
+ @import("turn_dedup.zig").settleFailedTurn(root, arena, err) catch {}; // #1729: a resumed retry is a new ask
const saved = save(root, arena);
const detail = root.last_api_error orelse @errorName(err);
const failure = result.classify(err, detail);
diff --git a/src/test_hooks.zig b/src/test_hooks.zig
index c89c1ba9..85c4d22b 100644
--- a/src/test_hooks.zig
+++ b/src/test_hooks.zig
@@ -378,6 +378,7 @@ test {
_ = @import("commands_resume.zig"); // #697: --resume SOURCE --branch DEST parser
_ = @import("session_list.zig"); // #712: device-wide saved-session listing
_ = @import("turn_dedup.zig"); // #714: identical user turns do not replay
+ _ = @import("turn_dedup_tests.zig"); // #1729: a failed turn's retry is a new ask
_ = @import("net_efficiency_test.zig");
_ = @import("codex_tool_search.zig"); // hosted tool_search on gpt-5.4+ Codex
_ = @import("acp_tool_view.zig"); // #1287/#1289: ACP locations and edit diffs
diff --git a/src/turn_dedup.zig b/src/turn_dedup.zig
index 8f40401c..b880b0d6 100644
--- a/src/turn_dedup.zig
+++ b/src/turn_dedup.zig
@@ -79,6 +79,24 @@ pub fn enqueueOrSkip(root: *Agent, arena: Allocator, out: ?*Io.Writer, text: []c
return try enqueue(root, arena, out, text) != .started;
}
+/// #1729: a turn that ended in an error before any reply left its prompt as
+/// the last row of history, so resending the same words read as the host
+/// re-submitting a pending row and was skipped without a request. Close the
+/// failed turn with an assistant row that says what streamed (if anything)
+/// and why no reply came. It is persisted with the session, so the boundary
+/// survives a checkpoint reload, and the model sees the request failed. A
+/// no-op when the turn already left a row after the prompt (a cancel or
+/// stream-end marker, a tool result), so those paths keep their own text.
+pub fn settleFailedTurn(root: *Agent, arena: Allocator, err: anyerror) !void {
+ const items = root.messages.items;
+ if (items.len == 0 or messages.userPromptText(items[items.len - 1]) == null) return;
+ const why = root.last_api_error orelse @errorName(err);
+ const marker = try std.fmt.allocPrint(arena, "[request failed: {s}; no reply was received]", .{why});
+ const partial = std.mem.trim(u8, root.partial_text.items, " \t\r\n");
+ const text = if (partial.len == 0) marker else try std.fmt.allocPrint(arena, "{s}\n\n{s}", .{ partial, marker });
+ try root.messages.append(try messages.textMessage(arena, "assistant", text));
+}
+
test "#714: the first copy of a prompt proceeds" {
resetForTest();
defer resetForTest();
diff --git a/src/turn_dedup_tests.zig b/src/turn_dedup_tests.zig
new file mode 100644
index 00000000..8dccb655
--- /dev/null
+++ b/src/turn_dedup_tests.zig
@@ -0,0 +1,143 @@
+//! #1729: a failed turn must not turn its own retry into a "duplicate".
+//!
+//! turn_dedup skips a prompt only when history already ends on that exact
+//! user row (#714: a host re-submitting a pending row). A turn that failed
+//! before any reply used to leave its prompt in that position, so resending
+//! the same words after the endpoint recovered finished without a request.
+//! `settleFailedTurn` closes the failed turn with an assistant row; these
+//! tests pin that the retry starts, that the boundary survives a checkpoint
+//! round trip on every wire, and that the #714 guard still holds.
+
+const std = @import("std");
+const Io = std.Io;
+const Value = std.json.Value;
+const Allocator = std.mem.Allocator;
+
+const Agent = @import("agent.zig").Agent;
+const messages = @import("messages.zig");
+const dedup = @import("turn_dedup.zig");
+const session = @import("session.zig");
+const session_writer = @import("session_writer.zig");
+const session_transcript = @import("session_transcript.zig");
+
+fn testAgent(a: Allocator, client: ?*std.http.Client) Agent {
+ return .{
+ .gpa = std.testing.allocator,
+ .arena = a,
+ .io = std.testing.io,
+ .client = if (client) |c| c else undefined,
+ .provider = .{ .id = "xai", .kind = .openai, .auth = .bearer, .url = "", .api_key = "k", .model = "grok-4.6", .context = 100_000 },
+ .messages = .init(a),
+ .sub = false,
+ .label = "test",
+ .out = null,
+ .session_title = "retry test",
+ };
+}
+
+fn lastText(agent: *Agent) []const u8 {
+ const last = agent.messages.items[agent.messages.items.len - 1];
+ return last.object.get("content").?.string;
+}
+
+test "#1729: an identical prompt after a failed turn starts a new request" {
+ dedup.resetForTest();
+ defer dedup.resetForTest();
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var agent = testAgent(a, null);
+ defer agent.partial_text.deinit(std.testing.allocator);
+ try std.testing.expectEqual(dedup.Outcome.started, try dedup.enqueue(&agent, a, null, "summarize the README"));
+ agent.last_api_error = "network error: ConnectionRefused (gave up after 6 attempts)";
+ try dedup.settleFailedTurn(&agent, a, error.ApiError);
+ try std.testing.expectEqual(@as(usize, 2), agent.messages.items.len);
+ try std.testing.expectEqualStrings(
+ "[request failed: network error: ConnectionRefused (gave up after 6 attempts); no reply was received]",
+ lastText(&agent),
+ );
+ // The endpoint is back and the user sends the same words: a new ask, not
+ // a host replay. Repeating it after each failure never trips the stuck
+ // latch either, because every attempt was a real request.
+ for (0..dedup.max_replays + 1) |_| {
+ try std.testing.expectEqual(dedup.Outcome.started, try dedup.enqueue(&agent, a, null, "summarize the README"));
+ try dedup.settleFailedTurn(&agent, a, error.ConnectionRefused);
+ }
+ try std.testing.expectEqualStrings("[request failed: network error: ConnectionRefused (gave up after 6 attempts); no reply was received]", lastText(&agent));
+}
+
+test "#1729: the boundary keeps what streamed and falls back to the error name" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var agent = testAgent(a, null);
+ defer agent.partial_text.deinit(std.testing.allocator);
+ try agent.messages.append(try messages.textMessage(a, "user", "go"));
+ try agent.partial_text.appendSlice(std.testing.allocator, " half an answer \n");
+ try dedup.settleFailedTurn(&agent, a, error.FallbackConsentRequired);
+ try std.testing.expectEqualStrings("half an answer\n\n[request failed: FallbackConsentRequired; no reply was received]", lastText(&agent));
+}
+
+test "#1729: a turn that already left a row after its prompt is not settled twice" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var agent = testAgent(a, null);
+ defer agent.partial_text.deinit(std.testing.allocator);
+ try dedup.settleFailedTurn(&agent, a, error.ApiError); // empty history
+ try std.testing.expectEqual(@as(usize, 0), agent.messages.items.len);
+ try agent.messages.append(try messages.textMessage(a, "user", "go"));
+ // A cancel or stream-end marker already closed the turn.
+ try agent.messages.append(try messages.textMessage(a, "assistant", "[response ended early: connection dropped]"));
+ try dedup.settleFailedTurn(&agent, a, error.StreamDropped);
+ try std.testing.expectEqual(@as(usize, 2), agent.messages.items.len);
+}
+
+test "#1729: a host re-submitting a pending prompt is still skipped (#714)" {
+ dedup.resetForTest();
+ defer dedup.resetForTest();
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var agent = testAgent(a, null);
+ try std.testing.expectEqual(dedup.Outcome.started, try dedup.enqueue(&agent, a, null, "Read SPEC.md"));
+ // No failure was recorded: the row is still pending, so the copy is a replay.
+ try std.testing.expectEqual(dedup.Outcome.skipped, try dedup.enqueue(&agent, a, null, "Read SPEC.md"));
+ try std.testing.expectEqual(dedup.Outcome.skipped, try dedup.enqueue(&agent, a, null, "Read SPEC.md"));
+ try std.testing.expectEqual(dedup.Outcome.stuck, try dedup.enqueue(&agent, a, null, "Read SPEC.md"));
+ try std.testing.expectEqual(@as(usize, 1), agent.messages.items.len);
+}
+
+test "#1729: the failed-request boundary survives a checkpoint reload on every wire" {
+ const gpa = std.testing.allocator;
+ dedup.resetForTest();
+ defer dedup.resetForTest();
+ session_writer.resetForTest();
+ defer session_writer.resetForTest();
+ session_transcript.resetForTest();
+ defer session_transcript.resetForTest();
+ var arena_state = std.heap.ArenaAllocator.init(gpa);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var client: std.http.Client = .{ .allocator = gpa, .io = std.testing.io };
+ var agent = testAgent(a, &client);
+ defer agent.partial_text.deinit(gpa);
+ try std.testing.expectEqual(dedup.Outcome.started, try dedup.enqueue(&agent, a, null, "summarize the README"));
+ try dedup.settleFailedTurn(&agent, a, error.ApiError);
+ try session.saveSessionTo(&agent, a, tmp.dir, "retry");
+ session.flushSaves();
+ const bytes = try tmp.dir.readFileAlloc(std.testing.io, ".graff/sessions/retry.session.json", gpa, .limited(64 * 1024));
+ defer gpa.free(bytes);
+ // Load applies history_wire.prepare for the session's wire; the boundary
+ // must stay the last row whichever wire the session resumes on.
+ for ([_]@import("provider.zig").Provider.Kind{ .openai, .responses, .anthropic, .interactions }) |kind| {
+ const saved = try std.json.parseFromSliceLeaky(Value, a, bytes, .{ .allocate = .alloc_always });
+ var reloaded = testAgent(a, null);
+ reloaded.messages = saved.object.get("messages").?.array;
+ @import("history_wire.zig").prepare(a, kind, &reloaded.messages);
+ try std.testing.expect(!messages.trailingUserIs(reloaded.messages.items, "summarize the README"));
+ try std.testing.expectEqual(dedup.Outcome.started, try dedup.enqueue(&reloaded, a, null, "summarize the README"));
+ }
+}