Skip to content

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

Python

fix: keep the first run_task answer after a lean bounce

justrach55a1e53e5084
diff --git a/src/agent.zig b/src/agent.zig
index dd85a030..dd2b2476 100644
--- a/src/agent.zig
+++ b/src/agent.zig
@@ -397,7 +397,7 @@ pub const Agent = struct {
// Retry empty replies; reconcile plain finals with live work (#745).
if (try empty_completion.handle(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, final_text)) |text| return review_deadline.finish(empty_completion.keepFirstIfBounceIdle(self, text));
continue;
}
self.empty_completion_retries = 0;
diff --git a/src/agent_empty_completion.zig b/src/agent_empty_completion.zig
index 07553b54..89b54c2a 100644
--- a/src/agent_empty_completion.zig
+++ b/src/agent_empty_completion.zig
@@ -34,6 +34,41 @@ pub fn shouldBounce(unattended: bool, lean: bool, text_only: bool, review: bool,
return std.mem.trim(u8, final_text, " \t\r\n").len > 0;
}
+fn contentString(msg: std.json.Value) ?[]const u8 {
+ if (msg != .object) return null;
+ const content = msg.object.get("content") orelse return null;
+ if (content == .string and content.string.len > 0) return content.string;
+ return null;
+}
+
+fn isAssistant(msg: std.json.Value) bool {
+ if (msg != .object) return false;
+ const role = msg.object.get("role") orelse return false;
+ return role == .string and std.mem.eql(u8, role.string, "assistant");
+}
+
+/// Lean `-p` bounce is stderr chrome; stdout is the last final. If that
+/// follow-up called no tools, keep the first answer (#1013).
+pub fn keepFirstIfBounceIdle(self: *const Agent, follow_up: []const u8) []const u8 {
+ if (self.tool_calls_this_turn != 0) return follow_up;
+ const items = self.messages.items;
+ var i = items.len;
+ while (i > 0) {
+ i -= 1;
+ const note = contentString(items[i]) orelse continue;
+ if (std.mem.indexOf(u8, note, bounce_note) == null) continue;
+ var j = i;
+ while (j > 0) {
+ j -= 1;
+ if (!isAssistant(items[j])) continue;
+ const first = contentString(items[j]) orelse continue;
+ if (std.mem.trim(u8, first, " \t\r\n").len > 0) return first;
+ }
+ break;
+ }
+ return follow_up;
+}
+
/// 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 {
@@ -275,3 +310,28 @@ test "no-local-tools lean critique is not an edit-oriented fake_done retry" {
try std.testing.expect(!try handle(&self, "The design is coherent and needs no local changes.", before));
try std.testing.expectEqual(before, self.messages.items.len);
}
+
+test "#1013 bounce follow-up without tools keeps the first answer" {
+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer state.deinit();
+ var self = pendingFixture(state.allocator());
+ const first = "TypeSafe AI sources: https://example.com/jev";
+ const meta = "That request was informational only. No files were changed.\n\nThe research is already in the previous answer.";
+ try self.messages.append(try messages.textMessage(self.arena, "user", "Read-only research about public projects"));
+ try self.messages.append(try messages.textMessage(self.arena, "assistant", first));
+ try self.messages.append(try messages.userNote(self.arena, self.provider.kind, bounce_note));
+ self.tool_calls_this_turn = 0;
+ try std.testing.expectEqualStrings(first, keepFirstIfBounceIdle(&self, meta));
+ self.tool_calls_this_turn = 2;
+ try std.testing.expectEqualStrings(meta, keepFirstIfBounceIdle(&self, meta));
+}
+
+test "#1013 without a bounce note the follow-up is the answer" {
+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer state.deinit();
+ var self = pendingFixture(state.allocator());
+ try self.messages.append(try messages.textMessage(self.arena, "user", "what is 2+2"));
+ try self.messages.append(try messages.textMessage(self.arena, "assistant", "4"));
+ self.tool_calls_this_turn = 0;
+ try std.testing.expectEqualStrings("4", keepFirstIfBounceIdle(&self, "4"));
+}
diff --git a/src/task_intent.zig b/src/task_intent.zig
index d0f4ab45..b2f194f3 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 TypeSafe AI projects",
+ "Investigate how Folio 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",