Skip to content

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

Python

fix: #714 only skip a user row that is already the last message

Cursor Agent49f9c5962791
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ee46014b..994d3c43 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -24,8 +24,9 @@ current is part of cutting a release.
`~/.graff/sessions`. Cross-workspace rows name their origin. Resume
restores history here; file tools stay in the current cwd.
- `#714` / ADR 0060: the “named a source file” nudge is per unanswered
- mention, not the whole history. Identical user turns are not
- re-injected; three copies fail closed as stuck replay.
+ mention, not the whole history. A host re-submit that ends on the
+ same user row is skipped; three copies fail closed as stuck replay.
+ The same words after an answer still run.
- The gateway is a fallback seat, not the startup default.
- No tag until asked.
diff --git a/docs/adr/0060-named-source-gate-is-per-unanswered-mention.md b/docs/adr/0060-named-source-gate-is-per-unanswered-mention.md
index afa00713..b53ffe31 100644
--- a/docs/adr/0060-named-source-gate-is-per-unanswered-mention.md
+++ b/docs/adr/0060-named-source-gate-is-per-unanswered-mention.md
@@ -22,12 +22,14 @@ The gate keys on **this turn's** user text only (`latestUserText` /
mention is nudged or tools run, it is settled; a later turn must name
a different path.
-`adopt` and the mainloop/ACP inject path skip a user payload that
-already is the latest user text. After three identical injects, fail
-closed and print `stuck replay`.
+`adopt` and the mainloop/ACP inject path skip a user payload only when
+history already **ends** on that exact user prompt (a host re-submit
+with no assistant in between). The same words after an answer are a
+new ask. After three back-to-back identical injects, fail closed and
+print `stuck replay`.
## Consequences
-A greeting after a file question does not re-open the file. A
-literal identical re-ask after an answer is treated as replay (rephrase
-to run it again). The history-wide walk stays as a test helper only.
+A greeting after a file question does not re-open the file. Typing
+the same prompt again after a reply still runs (learn-auto, a re-type).
+The history-wide walk stays as a test helper only.
diff --git a/src/messages.zig b/src/messages.zig
index 029d9cf1..89cbd95b 100644
--- a/src/messages.zig
+++ b/src/messages.zig
@@ -34,6 +34,14 @@ pub fn latestUserText(messages: []const Value) []const u8 {
return "";
}
+/// True when history already ends on this user prompt — a host re-submit,
+/// not a human typing the same words after an answer (#714).
+pub fn trailingUserIs(messages: []const Value, text: []const u8) bool {
+ if (messages.len == 0 or text.len == 0) return false;
+ const t = userPromptText(messages[messages.len - 1]) orelse return false;
+ return std.mem.eql(u8, t, text);
+}
+
fn userPromptText(msg: Value) ?[]const u8 {
if (msg != .object) return null;
const role = msg.object.get("role") orelse return null;
@@ -561,3 +569,14 @@ test "#714: latestUserText sees Responses input_text and skips tool_result" {
try msgs.append(.{ .object = wrapper });
try std.testing.expectEqualStrings("thanks", latestUserText(msgs.items));
}
+
+test "#714: trailingUserIs is only the last message, not an earlier ask" {
+ 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", "do a little work"));
+ try std.testing.expect(trailingUserIs(msgs.items, "do a little work"));
+ try msgs.append(try textMessage(a, "assistant", "LEARN_AUTO_OK"));
+ try std.testing.expect(!trailingUserIs(msgs.items, "do a little work"));
+}
diff --git a/src/repl_convo.zig b/src/repl_convo.zig
index 3907644d..d9596cc7 100644
--- a/src/repl_convo.zig
+++ b/src/repl_convo.zig
@@ -108,7 +108,7 @@ pub const Conversation = struct {
// #714: a host that re-submits the same trailing user row must not
// inject another turn. The named-source gate then re-read the file
// and the session looped the same Q&A.
- if (std.mem.eql(u8, messages_mod.latestUserText(msgs.items), last.text)) return;
+ if (messages_mod.trailingUserIs(msgs.items, last.text)) return;
try msgs.append(try textOf(self.alloc(), last));
}
@@ -200,15 +200,18 @@ test "#714: adopt does not replay an identical trailing user prompt" {
var convo = Conversation.init(testing.allocator);
defer convo.deinit();
try convo.adopt(&.{.{ .role = .user, .text = "Read SPEC.md" }});
+ try convo.adopt(&.{.{ .role = .user, .text = "Read SPEC.md" }});
+ try testing.expectEqual(@as(usize, 1), convo.len());
const msgs = convo.list();
try msgs.append(try messages_mod.textMessage(convo.alloc(), "assistant", "affinity is the git root"));
+ // The same words after an answer are a new ask (learn-auto, a re-type).
try convo.adopt(&.{
.{ .role = .user, .text = "Read SPEC.md" },
.{ .role = .assistant, .text = "affinity is the git root" },
.{ .role = .user, .text = "Read SPEC.md" },
});
- try testing.expectEqual(@as(usize, 2), convo.len());
- try testing.expectEqualStrings("assistant", roleOf(msgs.items[1]));
+ try testing.expectEqual(@as(usize, 3), convo.len());
+ try testing.expectEqualStrings("Read SPEC.md", msgs.items[2].object.get("content").?.string);
}
test "reset clears the conversation; rewind drops back to before the last prompt" {
diff --git a/src/turn_dedup.zig b/src/turn_dedup.zig
index 88da0ec0..1d4814dc 100644
--- a/src/turn_dedup.zig
+++ b/src/turn_dedup.zig
@@ -25,16 +25,13 @@ pub fn resetForTest() void {
hits = 0;
}
-fn sameAsLast(text: []const u8, history_tail: []const u8) bool {
- return text.len > 0 and std.mem.eql(u8, history_tail, text);
-}
-
pub const Outcome = enum { started, skipped, stuck };
-/// Append `text` as this turn's user message, or skip a duplicate.
+/// Append `text` as this turn's user message, or skip a back-to-back
+/// duplicate. The same words after an assistant reply are a new ask
+/// (learn-auto sends one prompt per model call).
pub fn enqueue(root: *Agent, arena: Allocator, out: ?*Io.Writer, text: []const u8) !Outcome {
- const tail = messages.latestUserText(root.messages.items);
- if (sameAsLast(text, tail)) {
+ if (messages.trailingUserIs(root.messages.items, text)) {
hits +|= 1;
if (hits >= max_replays) {
if (out) |w| {
@@ -92,10 +89,33 @@ test "#714: the first copy of a prompt proceeds" {
try std.testing.expectEqual(@as(usize, 1), agent.messages.items.len);
}
-test "#714: a different prompt resets the replay counter" {
+test "#714: the same words after an assistant reply are a new ask" {
resetForTest();
defer resetForTest();
- try std.testing.expect(sameAsLast("Read SPEC.md", "Read SPEC.md"));
- try std.testing.expect(!sameAsLast("thanks", "Read SPEC.md"));
- try std.testing.expect(!sameAsLast("", "Read SPEC.md"));
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ var agent: Agent = .{
+ .gpa = std.testing.allocator,
+ .arena = a,
+ .io = std.testing.io,
+ .client = 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,
+ };
+ try std.testing.expect(!try enqueueOrSkip(&agent, a, null, "do a little work"));
+ try agent.messages.append(try messages.textMessage(a, "assistant", "LEARN_AUTO_OK"));
+ try std.testing.expect(!try enqueueOrSkip(&agent, a, null, "do a little work"));
+ try std.testing.expectEqual(@as(usize, 3), agent.messages.items.len);
}