Skip to content

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

Python

fix: #714 keep the named-source mention on the Agent, not a process global

Cursor Agent0f01fa2753aa
diff --git a/src/agent.zig b/src/agent.zig
index 3e9a26c6..6e64427a 100644
--- a/src/agent.zig
+++ b/src/agent.zig
@@ -210,7 +210,9 @@ pub const Agent = struct {
compact_stall: @import("compact_cut.zig").Stall = .{}, // #706: same-boundary no-progress cuts
empty_completion_retries: u8 = 0, // degenerate empty completions re-asked this turn (agent_empty_completion.zig)
named_work_nudges: u8 = 0, // named-file / zero-tool nudge this turn (named_work.zig)
- precompact_note_gen: ?u32 = null, // #391: history_rewrites at the last pre-compaction note-to-self, so one history generation buys at most one note however often compaction is retried (compact_note.decideCalls)
+ named_work_task: []const u8 = "", // this turn's user text; arena-owned, never a child pointer
+ named_work_settled: []const u8 = "", // mention already nudged or tooled
+ precompact_note_gen: ?u32 = null, // #391: one pre-compaction note per history generation
ws_off: bool = false, // codex ws transport disabled for this session after a handshake/transport fallback to SSE (#codex-ws)
ws_transport_failures: u8 = 0, // consecutive WS failures; retry once before latching persistent SSE
streamed_text: bool = false, // the last request printed its text live
diff --git a/src/named_work.zig b/src/named_work.zig
index 71023be8..78854f08 100644
--- a/src/named_work.zig
+++ b/src/named_work.zig
@@ -17,25 +17,32 @@ pub const nudge_text =
"You named a source file and have not used a tool. Read the named path " ++
"and edit it before answering — do not describe a fix you have not applied.";
-/// The current turn's user text. Responses history often drops `role=user`
-/// after the first step, so `handle` must not depend on walking messages.
+/// Test-only fallback. Production copies onto the Agent so a scout's
+/// arena cannot leave a dangling pointer for the parent's handle (#714).
var remembered_task: []const u8 = "";
const source_needles = [_][]const u8{ ".py", ".zig", ".jsx", ".js", ".tsx", ".ts", "SPEC.md" };
+fn copyOnto(self: *Agent, text: []const u8) []const u8 {
+ return self.arena.dupe(u8, text) catch "";
+}
+
+fn taskOf(self: *const Agent) []const u8 {
+ return if (self.named_work_task.len > 0) self.named_work_task else remembered_task;
+}
+
pub fn remember(text: []const u8) void {
remembered_task = text;
}
+pub fn rememberOn(self: *Agent, text: []const u8) void {
+ self.named_work_task = copyOnto(self, text);
+}
+
pub fn resetForTest() void {
remembered_task = "";
- settled_task = "";
}
-/// Last mention this process already nudged or saw tools for. A later
-/// turn must name a *different* source path before the gate fires again.
-var settled_task: []const u8 = "";
-
fn lastUserText(self: *Agent) []const u8 {
return messages_mod.latestUserText(self.messages.items);
}
@@ -44,16 +51,21 @@ fn lastUserText(self: *Agent) []const u8 {
/// Does not clear a `-p` remember() if history has no user string yet.
pub fn rememberFrom(self: *Agent) void {
const t = lastUserText(self);
- if (t.len > 0) remember(t);
+ if (t.len > 0) {
+ rememberOn(self, t);
+ return;
+ }
+ if (remembered_task.len > 0 and self.named_work_task.len == 0) rememberOn(self, remembered_task);
}
pub fn beginTurn(self: *Agent) void {
rememberFrom(self);
- if (!std.mem.eql(u8, remembered_task, settled_task)) self.named_work_nudges = 0;
+ if (!std.mem.eql(u8, taskOf(self), self.named_work_settled)) self.named_work_nudges = 0;
}
/// True when `text` names a source path (not a greeting.txt-style data file).
pub fn hasNamedSource(text: []const u8) bool {
+ if (text.len == 0) return false;
for (source_needles) |needle| {
var from: usize = 0;
while (std.mem.indexOfPos(u8, text, from, needle)) |at| {
@@ -130,18 +142,19 @@ const isResponsesInputText = messages_mod.isResponsesInputText;
/// Re-anchor the Responses chain so the new item is not a dropped delta.
pub fn handle(self: *Agent, _: []const u8) !bool {
if (self.named_work_nudges >= max_nudges) return false;
+ const task = taskOf(self);
if (self.tools_used.count() != 0) {
- if (hasNamedSource(remembered_task)) settled_task = remembered_task;
+ if (hasNamedSource(task)) self.named_work_settled = copyOnto(self, task);
return false;
}
// Per unanswered mention (#714): this turn's user text only. Walking
// all history re-fired after a successful read because SPEC.md stayed
// in an earlier message.
- if (!hasNamedSource(remembered_task)) return false;
- if (std.mem.eql(u8, remembered_task, settled_task)) return false;
+ if (!hasNamedSource(task)) return false;
+ if (std.mem.eql(u8, task, self.named_work_settled)) return false;
if (std.mem.eql(u8, lastUserText(self), nudge_text)) return false;
self.named_work_nudges += 1;
- settled_task = remembered_task;
+ self.named_work_settled = copyOnto(self, task);
self.closeCodexWs();
try self.messages.append(try userNudge(self.arena, self.provider.kind, nudge_text));
return true;
@@ -272,7 +285,7 @@ test "#714: a later greeting does not re-fire because history still names SPEC.m
try msgs.append(try messages_mod.textMessage(a, "user", "thanks"));
var agent = testAgent(a, msgs);
rememberFrom(&agent);
- try std.testing.expectEqualStrings("thanks", remembered_task);
+ try std.testing.expectEqualStrings("thanks", agent.named_work_task);
try std.testing.expect(conversationNamesSource(agent.messages.items));
try std.testing.expect(!try handle(&agent, "you're welcome"));
}
@@ -293,3 +306,17 @@ test "#714: tools on a named path settle the gate for that mention" {
agent.named_work_nudges = 0;
try std.testing.expect(!try handle(&agent, "still fib.py"));
}
+
+test "#714: handle reads the Agent copy, not a process-global leftover" {
+ resetForTest();
+ defer resetForTest();
+ remember("Fix scout_worker.py"); // what a finished child's arena used to leave behind
+ 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 messages_mod.textMessage(a, "user", "thanks"));
+ var agent = testAgent(a, msgs);
+ rememberOn(&agent, "thanks");
+ try std.testing.expect(!try handle(&agent, "you're welcome"));
+}
diff --git a/src/session_run.zig b/src/session_run.zig
index 1df844a9..effafb8e 100644
--- a/src/session_run.zig
+++ b/src/session_run.zig
@@ -144,7 +144,7 @@ pub fn runFrontendCommands(gpa: Allocator, io: Io, environ_map: anytype, root: *
pub fn runOneshotPrompt(gpa: Allocator, io: Io, arena: Allocator, root: *agent_mod.Agent, keys: *provider_mod.Keys, tracer: *trace.Tracer, out: *Io.Writer, prompt_text: []const u8) !void {
if (@import("goal_pacing.zig").oneshotSlashRefusal(prompt_text)) |why| std.process.fatal("{s}", .{why}); // usage error, not a prompt
main_mod.unattended = true;
- @import("named_work.zig").remember(prompt_text);
+ @import("named_work.zig").rememberOn(root, prompt_text);
root.in = null; // gate: deny instead of prompt; ask_user: self-decide
root.out = null; // tool progress → stderr; stdout carries only the answer
root.stream_quiet = true;