Skip to content

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

Python

fix(prompt): /new and /clear disarm the #445 transcript line again

Rach Pradhand9dce77d76aa
diff --git a/src/commands_session.zig b/src/commands_session.zig
index 92c7cdfd..d6fbdea6 100644
--- a/src/commands_session.zig
+++ b/src/commands_session.zig
@@ -15,6 +15,7 @@ const tools_mod = @import("tools.zig");
const session_mod = @import("session.zig");
const goal_state = @import("goal_state.zig");
const goal_flow = @import("goal_flow.zig");
+const prompts = @import("prompts.zig"); // #445: the transcript line's compaction flag resets with the conversation
const Agent = agent_mod.Agent;
const Keys = provider_mod.Keys;
const ToolCall = tools_mod.ToolCall;
@@ -58,7 +59,7 @@ const setTerminalTitle = title_mod.setTerminalTitle;
/// standing goal containing the word "ultracode" was re-appended to every
/// assembled prompt and kept re-triggering the explicit-codeword banner
/// after "context cleared" (#178).
-fn resetConversationSteering(root: *Agent) void {
+pub fn resetConversationSteering(root: *Agent) void {
root.goal = null;
root.completion_gate_armed = false; // a dropped goal re-arms the completion double-check (#318)
root.todos_dirty = false; // the conversation's checklist dies with it; nothing survives as /loop evidence (#318)
@@ -74,31 +75,6 @@ fn resetConversationSteering(root: *Agent) void {
if (root.goal_flag) |g| root.goal = goal_flow.standingGoalFromFlag(g, null, root.todos.items, 0);
}
-test "/clear + /new reset conversation steering — goal and ultracode_mode don't survive (#178)" {
- var root: Agent = undefined;
- root.goal = .{ .objective = "ultracode: index the statutes" };
- root.ultracode_mode = true;
- root.goal_flag = null; // no --goal: the conversation's goal dies with it
- root.todos = .empty;
- resetConversationSteering(&root);
- try std.testing.expect(root.goal == null);
- try std.testing.expect(!root.ultracode_mode);
-}
-
-test "/clear keeps a --goal standing objective standing (#318 through the /clear door)" {
- var root: Agent = undefined;
- root.ultracode_mode = false;
- root.todos = .empty;
- root.goal_flag = "keep the tree green"; // the user passed --goal
- root.goal = .{ .objective = "keep the tree green", .standing = true, .epoch = 1 };
- resetConversationSteering(&root);
- // Before this, /clear dropped it, the user typed a fresh /goal, got a
- // RETIRABLE one, and the next attempt_completion ended their steering.
- const g = root.goal orelse return error.TestExpectedNonNull;
- try std.testing.expect(g.standing);
- try std.testing.expectEqualStrings("keep the tree green", g.objective);
-}
-
/// Try to handle a session/environment slash command. Returns false (line
/// unhandled) if `line` doesn't match any command in this file — the caller
/// (handleCommand in main.zig) then tries the next peer module.
@@ -117,6 +93,7 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,
root.todos.clearRetainingCapacity();
const had_goal = root.goal != null;
resetConversationSteering(root);
+ prompts.resetSessionCompacted(root, arena); // #445: the save below empties the file the #410 line names
saveSession(root, arena, root.session_name) catch {};
setTerminalTitle(out, "Chat", main_mod.g_cwd_display);
try out.writeAll("context cleared — fresh conversation\n");
@@ -137,6 +114,7 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,
root.title_generation +%= 1;
root.tui_header_shown = false;
root.session_name = try std.fmt.allocPrint(arena, "session-{d}", .{unixMs(root.io)});
+ prompts.resetSessionCompacted(root, arena); // #445: after the rename, so the re-arm reads the NEW session
saveSession(root, arena, root.session_name) catch {};
try out.print("new session → {s}{s}\n", .{ root.session_name, session_ext });
try out.flush();
diff --git a/src/commands_session_test.zig b/src/commands_session_test.zig
new file mode 100644
index 00000000..4021c246
--- /dev/null
+++ b/src/commands_session_test.zig
@@ -0,0 +1,34 @@
+//! Tests for commands_session.zig's conversation-reset helper. They live in a
+//! sibling because commands_session.zig sits at the 600-line cap (AGENTS.md)
+//! and #445 needed room in it for the transcript-line reset call sites.
+//! Reachability is wired in test_hooks.zig — an unreferenced module's tests
+//! compile to nothing and the suite still reports green.
+
+const std = @import("std");
+const Agent = @import("agent.zig").Agent;
+const resetConversationSteering = @import("commands_session.zig").resetConversationSteering;
+
+test "/clear + /new reset conversation steering — goal and ultracode_mode don't survive (#178)" {
+ var root: Agent = undefined;
+ root.goal = .{ .objective = "ultracode: index the statutes" };
+ root.ultracode_mode = true;
+ root.goal_flag = null; // no --goal: the conversation's goal dies with it
+ root.todos = .empty;
+ resetConversationSteering(&root);
+ try std.testing.expect(root.goal == null);
+ try std.testing.expect(!root.ultracode_mode);
+}
+
+test "/clear keeps a --goal standing objective standing (#318 through the /clear door)" {
+ var root: Agent = undefined;
+ root.ultracode_mode = false;
+ root.todos = .empty;
+ root.goal_flag = "keep the tree green"; // the user passed --goal
+ root.goal = .{ .objective = "keep the tree green", .standing = true, .epoch = 1 };
+ resetConversationSteering(&root);
+ // Before this, /clear dropped it, the user typed a fresh /goal, got a
+ // RETIRABLE one, and the next attempt_completion ended their steering.
+ const g = root.goal orelse return error.TestExpectedNonNull;
+ try std.testing.expect(g.standing);
+ try std.testing.expectEqualStrings("keep the tree green", g.objective);
+}
diff --git a/src/prompt_snapshot_tests.zig b/src/prompt_snapshot_tests.zig
index 829afea8..b67a2529 100644
--- a/src/prompt_snapshot_tests.zig
+++ b/src/prompt_snapshot_tests.zig
@@ -365,6 +365,72 @@ test "#445: the transcript line is absent until the first compaction, then rides
try std.testing.expectEqualStrings("CHILD-BASE", child.sys_normal);
}
+// #445: one PROCESS can host several root conversations. /new mints a fresh
+// session_name and /clear empties the history and saves over the file, so both
+// put the durable file back to holding no more than the live window. A flag
+// that only ever went true made one compaction arm the line for the rest of
+// the process — the exact per-call waste this issue exists to remove, walked
+// back in through the commonest flow there is.
+test "#445: /new and /clear disarm the transcript line again, and a later compaction re-arms it" {
+ var a_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer a_state.deinit();
+ const a = a_state.allocator();
+ const saved = prompts.g_session_compacted;
+ defer prompts.g_session_compacted = saved;
+ defer prompts.armSessionTranscript(a, "", .{}, false); // leave the global as the rest of the suite expects it
+
+ var agent: agent_mod.Agent = undefined;
+ agent.sub = false;
+ agent.session_name = "session-long";
+ prompts.g_session_compacted = false;
+ prompts.armSessionTranscript(a, agent.session_name, .{}, false);
+ try prompts.setSystemPrompts(&agent, "BASE", a);
+
+ // A long session compacts: the line arms, as #445's forward boundary says.
+ prompts.noteSessionCompacted(&agent, a);
+ try std.testing.expect(std.mem.indexOf(u8, agent.sys_normal, "session-long.session.json") != null);
+
+ // /new: a brand-new conversation under a brand-new name, in the SAME
+ // process. commands_session calls the reset AFTER the rename, so the
+ // re-arm reads the session that exists now — and finds nothing to say.
+ agent.session_name = "session-new";
+ prompts.resetSessionCompacted(&agent, a);
+ try std.testing.expect(!prompts.g_session_compacted);
+ try std.testing.expectEqualStrings("BASE", agent.sys_normal);
+ for ([_][]const u8{ agent.sys_normal, agent.sys_strict, agent.sys_ultra, agent.sys_ultra_strict }) |v| {
+ try std.testing.expect(std.mem.indexOf(u8, v, "session-long.session.json") == null); // no stale name either
+ try std.testing.expect(std.mem.indexOf(u8, v, "session-new.session.json") == null);
+ }
+
+ // ...and the new conversation earns the line back on its own first
+ // compaction, naming ITS file. The flag is a boundary, not a one-way latch.
+ prompts.noteSessionCompacted(&agent, a);
+ try std.testing.expect(std.mem.indexOf(u8, agent.sys_normal, "session-new.session.json") != null);
+ try std.testing.expect(std.mem.indexOf(u8, agent.sys_normal, "session-long.session.json") == null);
+
+ // /clear: same name, but the immediate saveSession rewrites the durable
+ // file down to an empty history, so the line again describes nothing.
+ prompts.resetSessionCompacted(&agent, a);
+ try std.testing.expect(!prompts.g_session_compacted);
+ try std.testing.expectEqualStrings("BASE", agent.sys_normal);
+
+ // Already false: a second /clear must not touch the prompt at all.
+ agent.sys_normal = "SENTINEL";
+ prompts.resetSessionCompacted(&agent, a);
+ try std.testing.expectEqualStrings("SENTINEL", agent.sys_normal);
+
+ // A subagent's own /clear-equivalent can never disarm the ROOT's line.
+ prompts.g_session_compacted = true;
+ var child: agent_mod.Agent = undefined;
+ child.sub = true;
+ child.session_name = "session-child";
+ child.sys_base = "CHILD-BASE";
+ child.sys_normal = "CHILD-SENTINEL";
+ prompts.resetSessionCompacted(&child, a);
+ try std.testing.expect(prompts.g_session_compacted);
+ try std.testing.expectEqualStrings("CHILD-SENTINEL", child.sys_normal);
+}
+
test "#421: MCP, skill and optional-tool guidance all cost zero when absent" {
var a_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer a_state.deinit();
diff --git a/src/prompts.zig b/src/prompts.zig
index 2f8239e4..3bffcbf6 100644
--- a/src/prompts.zig
+++ b/src/prompts.zig
@@ -313,6 +313,25 @@ pub fn noteSessionCompacted(agent: *Agent, arena: Allocator) void {
setSystemPrompts(agent, agent.sys_base, arena) catch {};
}
+/// #445's inverse boundary, and it is NOT optional: one process can host
+/// several root conversations. `/new` mints a fresh `session_name` and `/clear`
+/// empties the history and saves immediately, so in both cases the durable file
+/// again holds no more than the live window — the exact state the line is not
+/// worth its tokens in. Without this, one compaction armed the line for the
+/// remaining life of the process and a user who compacted, then hit `/new`,
+/// paid for it forever pointing at a file with nothing to recover.
+///
+/// Same idiom as noteSessionCompacted: root-only, a no-op when already false,
+/// and best-effort. Call it AFTER any `session_name` reassignment, so the
+/// re-arm reads the conversation that actually exists now.
+pub fn resetSessionCompacted(agent: *Agent, arena: Allocator) void {
+ if (agent.sub or !g_session_compacted) return;
+ g_session_compacted = false;
+ armSessionTranscript(arena, agent.session_name, detectCaps(), false);
+ if (agent.sys_base.len == 0) return;
+ setSystemPrompts(agent, agent.sys_base, arena) catch {};
+}
+
pub const sub_system_prompt =
\\You are a subagent spawned by an orchestrator agent inside a terminal
\\harness. Complete the assigned task using your tools, without asking
diff --git a/src/test_hooks.zig b/src/test_hooks.zig
index 033f8746..6d4b03dc 100644
--- a/src/test_hooks.zig
+++ b/src/test_hooks.zig
@@ -70,6 +70,10 @@ const scoring_slot_test = @import("scoring_slot_test.zig");
// #273: session.zig's own tests, moved off it for the same reason.
const session_tests = @import("session_tests.zig");
+// #445: moved off commands_session.zig when the transcript-line reset needed
+// three lines there and the file was at exactly 600. The tests are unchanged.
+const commands_session_test = @import("commands_session_test.zig");
+
// #375: `graff acp` (Zed's Agent Client Protocol over stdio). args.zig calls
// one predicate from it, which analyses the file but does not run its tests.
const acp = @import("acp.zig");
@@ -170,6 +174,7 @@ test {
_ = serve_create;
_ = scoring_slot_test;
_ = session_tests;
+ _ = commands_session_test;
_ = mcp_config;
_ = acp;
_ = agent_eval_control_tests;