Skip to content

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

Python

fix(ultracode): scan the codeword on the raw prompt only + /clear resets goal/ultracode steering (#178)

Rach Pradhan9c391fe3eff8
diff --git a/src/commands_session.zig b/src/commands_session.zig
index adff2ae5..ccd111b2 100644
--- a/src/commands_session.zig
+++ b/src/commands_session.zig
@@ -51,6 +51,25 @@ const skills_registry = skills.skills_registry;
const title_mod = @import("title.zig");
const setTerminalTitle = title_mod.setTerminalTitle;
+/// Steering state that must not outlive the conversation it was set in.
+/// /clear used to keep goal + ultracode_mode (only /new reset them), so a
+/// 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 {
+ root.goal = null;
+ root.ultracode_mode = false;
+}
+
+test "/clear + /new reset conversation steering — goal and ultracode_mode don't survive (#178)" {
+ var root: Agent = undefined;
+ root.goal = "ultracode: index the statutes";
+ root.ultracode_mode = true;
+ resetConversationSteering(&root);
+ try std.testing.expect(root.goal == null);
+ try std.testing.expect(!root.ultracode_mode);
+}
+
/// 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.
@@ -64,9 +83,12 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,
root.session_title = null; // re-summarize the now-empty conversation
root.ai_title_done = false;
root.todos.clearRetainingCapacity();
+ const had_goal = root.goal != null;
+ resetConversationSteering(root);
saveSession(root, arena, root.session_name) catch {};
setTerminalTitle(out, "Chat", main_mod.g_cwd_display);
try out.writeAll("context cleared — fresh conversation\n");
+ if (had_goal) try out.writeAll("(standing goal dropped — /goal to set a new one)\n");
try out.flush();
return true;
}
@@ -75,8 +97,7 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,
root.last_context_tokens = 0;
root.last_cache_read = 0;
root.todos.clearRetainingCapacity();
- root.goal = null;
- root.ultracode_mode = false;
+ resetConversationSteering(root);
root.session_title = null;
root.ai_title_done = false; // let the new session earn its own AI title
root.tui_header_shown = false;
diff --git a/src/mainloop.zig b/src/mainloop.zig
index 010d0ad0..5c4d6c5d 100644
--- a/src/mainloop.zig
+++ b/src/mainloop.zig
@@ -500,7 +500,7 @@ pub fn run(ctx: *Ctx) !void {
}
// "ultracode" codeword or persistent /ultracode mode: opt turns into multi-agent workflow mode.
- const ultracode_msg = try pickers.applyUltracodeSteering(ctx.arena, msg, ctx.root.ultracode_mode or ctx.root.reasoning == .ultra);
+ const ultracode_msg = try pickers.applyUltracodeSteering(ctx.arena, msg, base_msg, ctx.root.ultracode_mode or ctx.root.reasoning == .ultra);
if (ultracode_msg.explicit) {
if (!main_mod.json_mode) {
if (ctx.interactive) {
diff --git a/src/pickers.zig b/src/pickers.zig
index 8ec89f88..940dbb7e 100644
--- a/src/pickers.zig
+++ b/src/pickers.zig
@@ -433,8 +433,15 @@ const ultracode_persistent_note =
\\structural.]
;
-pub fn applyUltracodeSteering(arena: Allocator, msg: []const u8, persistent_enabled: bool) !UltracodeMessage {
- const explicit = std.ascii.indexOfIgnoreCase(msg, "ultracode") != null;
+/// `raw` is what the user actually typed this turn; `msg` is the assembled
+/// turn message (goal/eval/loop/plan notes may already be appended). The
+/// explicit-codeword scan runs ONLY on `raw`: harness-assembled notes replay
+/// prior context — a /goal set during an ultracode task, a todo echoed
+/// through the goal note — and scanning them made the codeword sticky, so
+/// every turn after /clear bannered as explicit even though the user never
+/// typed the word (#178).
+pub fn applyUltracodeSteering(arena: Allocator, msg: []const u8, raw: []const u8, persistent_enabled: bool) !UltracodeMessage {
+ const explicit = std.ascii.indexOfIgnoreCase(raw, "ultracode") != null;
if (explicit) {
return .{ .text = try std.fmt.allocPrint(arena, "{s}\n\n{s}", .{ msg, ultracode_explicit_note }), .explicit = true };
}
@@ -444,6 +451,27 @@ pub fn applyUltracodeSteering(arena: Allocator, msg: []const u8, persistent_enab
return .{ .text = msg, .explicit = false };
}
+test "applyUltracodeSteering (#178): the codeword scan runs on the raw typed text, not the assembled msg" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ // The word arriving via an appended harness note (e.g. a standing goal)
+ // must NOT count as an invocation — and with persistent mode off, the
+ // message passes through untouched.
+ const assembled = "write the report\n\n[harness note: goal — ultracode the pipeline]";
+ const via_note = try applyUltracodeSteering(a, assembled, "write the report", false);
+ try std.testing.expect(!via_note.explicit);
+ try std.testing.expectEqualStrings(assembled, via_note.text);
+ // The user actually typing it does (case-insensitive) and appends the note.
+ const typed = try applyUltracodeSteering(a, "ULTRACODE fix the bug", "ULTRACODE fix the bug", false);
+ try std.testing.expect(typed.explicit);
+ try std.testing.expect(std.mem.indexOf(u8, typed.text, "codeword") != null);
+ // Persistent mode still applies its note without ever claiming explicit.
+ const persistent = try applyUltracodeSteering(a, assembled, "write the report", true);
+ try std.testing.expect(!persistent.explicit);
+ try std.testing.expect(std.mem.indexOf(u8, persistent.text, "ultracode mode is enabled") != null);
+}
+
const ultracode_on_first = [_]PickItem{
.{ .name = "on", .desc = "Enable ultracode orchestration" },
.{ .name = "off", .desc = "Disable ultracode orchestration" },
@@ -711,15 +739,15 @@ test "applyUltracodeSteering handles explicit and persistent modes" {
defer arena_state.deinit();
const a = arena_state.allocator();
- const plain = try applyUltracodeSteering(a, "fix this", false);
+ const plain = try applyUltracodeSteering(a, "fix this", "fix this", false);
try std.testing.expect(!plain.explicit);
try std.testing.expectEqualStrings("fix this", plain.text);
- const persistent = try applyUltracodeSteering(a, "fix this", true);
+ const persistent = try applyUltracodeSteering(a, "fix this", "fix this", true);
try std.testing.expect(!persistent.explicit);
try std.testing.expect(std.mem.indexOf(u8, persistent.text, "ultracode mode is enabled") != null);
- const explicit = try applyUltracodeSteering(a, "ultracode fix this", true);
+ const explicit = try applyUltracodeSteering(a, "ultracode fix this", "ultracode fix this", true);
try std.testing.expect(explicit.explicit);
try std.testing.expect(std.mem.indexOf(u8, explicit.text, "user invoked the \"ultracode\" codeword") != null);
try std.testing.expect(std.mem.indexOf(u8, explicit.text, "ultracode mode is enabled") == null);
diff --git a/src/session_run.zig b/src/session_run.zig
index 9b12fb4f..d4d6d698 100644
--- a/src/session_run.zig
+++ b/src/session_run.zig
@@ -102,7 +102,7 @@ pub fn runOneshotPrompt(gpa: Allocator, io: Io, arena: Allocator, root: *agent_m
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;
- const ultracode_msg = try pickers.applyUltracodeSteering(arena, prompt_text, root.ultracode_mode or root.reasoning == .ultra);
+ const ultracode_msg = try pickers.applyUltracodeSteering(arena, prompt_text, prompt_text, root.ultracode_mode or root.reasoning == .ultra);
if (ultracode_msg.explicit) {
tracer.note("ultracode", prompt_text[0..@min(prompt_text.len, 120)]);
if (telemetry.g_telem) |t| t.ultracode();