Skip to content

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

Python

Keep first-answer recovery scoped to the current turn

justrach60d5f7d2259a
diff --git a/docs/adr/0139-bounced-answers-stay-turn-local.md b/docs/adr/0139-bounced-answers-stay-turn-local.md
new file mode 100644
index 00000000..3cef6b32
--- /dev/null
+++ b/docs/adr/0139-bounced-answers-stay-turn-local.md
@@ -0,0 +1,24 @@
+# 0139. Bounced answers stay turn-local
+
+Status: accepted
+
+## Context
+
+A lean retry can refer to an earlier answer that a single-result caller never
+received (#1013). Recovering that answer by searching message history also
+finds retries from previous turns and can replace an unrelated new answer.
+
+## Decision
+
+Capture a nonempty first answer only when the current `runTurn` actually
+bounces it. Keep that value on the turn stack, not in persisted history.
+Return it only when the retry performed no tools. A later turn starts with
+no captured answer, and tool progress makes the newer answer authoritative.
+Apply this choice before unfinished-work reconciliation so its stop notice
+is not lost. Explicit informational requests remain exempt from the bounce.
+
+## Verification
+
+The old history scan failed a regression in which a later arithmetic question
+returned an earlier answer. The turn-local version passes that regression,
+first-answer retention, tool-progress, and empty-answer cases.
diff --git a/docs/adr/README.md b/docs/adr/README.md
index ef21c228..5bb37ff7 100644
--- a/docs/adr/README.md
+++ b/docs/adr/README.md
@@ -158,6 +158,7 @@ record only when you need the evidence or the edge cases.
| [0136](0136-follow-up-promotes-a-live-shell.md) | A queued follow-up or force-steer promotes a foreground shell; Esc still kills it. |
| [0137](0137-desktop-appearance-is-a-turn-note.md) | Desktop appearance tokens for `render_html` ride the turn, never the prefix or tool catalog; drawn HTML uses them unless the user named colors. |
| [0138](0138-unselected-splits-use-glass-overlay.md) | Unselected split panes keep their transcript under a light wash; the focused pane stays the working surface. |
+| [0139](0139-bounced-answers-stay-turn-local.md) | Retain a bounced answer only in the current turn, before unfinished-work reconciliation; never recover it from persisted history. |
## When to write one
diff --git a/src/agent.zig b/src/agent.zig
index dd85a030..c7d41aea 100644
--- a/src/agent.zig
+++ b/src/agent.zig
@@ -330,6 +330,7 @@ pub const Agent = struct {
pub fn runTurn(self: *Agent) anyerror![]const u8 {
var pending_work: empty_completion.PendingWork = .{};
+ var bounced_answer: empty_completion.BounceAnswer = .{};
self.completed = null;
self.mcp_context.begin(self.io);
@import("named_work.zig").beginTurn(self);
@@ -395,9 +396,9 @@ pub const Agent = struct {
const done = try @import("agent_steps.zig").stepForWire(self, root);
if (done) |final_text| {
// Retry empty replies; reconcile plain finals with live work (#745).
- if (try empty_completion.handle(self, final_text, hist_len)) continue;
+ if (try bounced_answer.retry(self, final_text, hist_len)) continue;
if (try @import("named_work.zig").handle(self, final_text)) continue;
- if (try pending_work.finish(self, final_text)) |text| return review_deadline.finish(text);
+ if (try pending_work.finish(self, bounced_answer.finish(self, final_text))) |text| return review_deadline.finish(text);
continue;
}
self.empty_completion_retries = 0;
diff --git a/src/agent_empty_completion.zig b/src/agent_empty_completion.zig
index 07553b54..2ad620e5 100644
--- a/src/agent_empty_completion.zig
+++ b/src/agent_empty_completion.zig
@@ -34,6 +34,63 @@ pub fn shouldBounce(unattended: bool, lean: bool, text_only: bool, review: bool,
return std.mem.trim(u8, final_text, " \t\r\n").len > 0;
}
+/// Kept on the runTurn stack, never recovered from persisted message history.
+/// A later turn or a quoted retry note must not revive an earlier answer.
+pub const BounceAnswer = struct {
+ first: ?[]const u8 = null,
+
+ pub fn remember(state: *BounceAnswer, text: []const u8) void {
+ if (state.first == null and std.mem.trim(u8, text, " \t\r\n").len > 0) state.first = text;
+ }
+
+ pub fn retry(state: *BounceAnswer, self: *Agent, text: []const u8, hist_len: usize) !bool {
+ if (!try handle(self, text, hist_len)) return false;
+ state.remember(text);
+ return true;
+ }
+
+ pub fn finish(state: BounceAnswer, self: *const Agent, follow_up: []const u8) []const u8 {
+ return if (self.tool_calls_this_turn == 0) state.first orelse follow_up else follow_up;
+ }
+};
+
+test "#1013 an old bounce cannot replace a later turn's answer" {
+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer state.deinit();
+ var self = pendingFixture(state.allocator());
+ self.tool_calls_this_turn = 0;
+ try self.messages.append(try messages.textMessage(self.arena, "user", "Earlier request"));
+ try self.messages.append(try messages.textMessage(self.arena, "assistant", "Earlier answer"));
+ try self.messages.append(try messages.userNote(self.arena, self.provider.kind, bounce_note));
+ try self.messages.append(try messages.textMessage(self.arena, "assistant", "Earlier retry"));
+ try self.messages.append(try messages.textMessage(self.arena, "user", "What is two plus two?"));
+ const current: BounceAnswer = .{};
+ try std.testing.expectEqualStrings("4", current.finish(&self, "4"));
+}
+
+test "#1013 bounce follow-up without tools keeps the first answer" {
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ var self = pendingFixture(arena.allocator());
+ self.tool_calls_this_turn = 0;
+ var current: BounceAnswer = .{};
+ current.remember("First answer");
+ current.remember(" \n ");
+ try std.testing.expectEqualStrings("First answer", current.finish(&self, "See previous answer"));
+ self.tool_calls_this_turn = 2;
+ try std.testing.expectEqualStrings("Updated answer", current.finish(&self, "Updated answer"));
+}
+
+test "#1013 without a bounce note the follow-up is the answer" {
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ var self = pendingFixture(arena.allocator());
+ self.tool_calls_this_turn = 0;
+ var current: BounceAnswer = .{};
+ current.remember(" \n ");
+ try std.testing.expectEqualStrings("Current answer", current.finish(&self, "Current answer"));
+}
+
/// Handle a degenerate completion inside runTurn. Returns true when the
/// caller should `continue` the loop.
pub fn handle(self: *Agent, final_text: []const u8, hist_len: usize) !bool {
diff --git a/src/task_intent.zig b/src/task_intent.zig
index d0f4ab45..6bee0c9e 100644
--- a/src/task_intent.zig
+++ b/src/task_intent.zig
@@ -42,7 +42,8 @@ pub fn classify(text: []const u8) Intent {
while (words.next()) |raw| {
const word = std.mem.trim(u8, raw, ".");
if (oneOf(word, &.{ "fix", "edit", "implement", "add", "remove", "delete", "rename", "refactor", "write", "create", "update", "patch", "build", "test", "lint", "run", "merge", "push", "deploy", "commit" })) return .general;
- if (oneOf(word, &.{ "summarize", "summarise", "summary", "explain", "describe", "overview", "map" })) informational = true;
+ if (oneOf(word, &.{ "summarize", "summarise", "summary", "explain", "describe", "overview", "map", "research", "investigate", "lookup" })) informational = true;
+ if (std.ascii.eqlIgnoreCase(word, "read-only")) informational = true;
}
return if (informational) .informational else .general;
}
@@ -83,6 +84,8 @@ test "informational intent recognizes summaries and keeps mixed execution reques
"Give me an OVERVIEW of this application.",
"Summarise the tests and architecture",
"Describe the deployment flow",
+ "Read-only research about public projects",
+ "Investigate how this application uses citations",
}) |text| try std.testing.expectEqual(Intent.informational, classify(text));
for ([_][]const u8{
"Fix src/parser.zig and summarize the change", "Explain and then refactor the parser",