graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(#644): /never rm without a needle does not add "rm"
Cursor Agent384b741ecc3b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e931563f..38325bc1 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -23,7 +23,10 @@ current is part of cutting a release.(text prominent, id secondary) and requires two confirmations;Esc/Keep leaves the ledger unchanged. `/never rm <id-or-text>` andan explicit forget/override in the next message still work. The- model cannot retire a rule.+ model cannot retire a rule. `/never rm` without a needle lists+ (or opens the TTY picker) instead of recording `rm` as a constraint;+ a failed tombstone write says the rule is still active; traces+ record `never_retire {id, ok}` with no text (#644).- `bash_output` / `agent_output` `wait_ms>0` always waits for exit(10h). Mid-range values like 60s–240s are no longer a boundeddeadline (#640).diff --git a/docs/adr/0033-user-can-retire-a-standing-constraint.md b/docs/adr/0033-user-can-retire-a-standing-constraint.mdindex 6f946e4a..c884ff30 100644--- a/docs/adr/0033-user-can-retire-a-standing-constraint.md+++ b/docs/adr/0033-user-can-retire-a-standing-constraint.md@@ -39,3 +39,7 @@ matches refuse rather than pick.- A vague "do it anyway" does **not** retire a rule; the user mustname it or use retire language plus a unique match.- `note_constraint` stays append-only.+- `/never rm` / `/never remove` without a needle lists (ACP/pipe) or+ opens the picker (TTY). It never records the verb as a constraint+ (#644). A failed tombstone write says the rule is still active.+ Operational traces record `never_retire {id, ok}` and no text.diff --git a/docs/releases/v0.0.279.md b/docs/releases/v0.0.279.mdindex 94f3a62d..3947a56d 100644--- a/docs/releases/v0.0.279.md+++ b/docs/releases/v0.0.279.md@@ -66,7 +66,11 @@ first, id second) and asks twice before a tombstone. ACP / pipes keepthe text list. `rm` accepts an id or a unique text fragment. Anexplicit "forget / override that rule" in the next user messagetombstones the matching item before the model turn (ADR 0033).-`note_constraint` stays append-only.+`note_constraint` stays append-only. `/never rm` without an id+lists (or opens the picker on a TTY) instead of recording `rm` as+a new constraint; a failed tombstone write says the rule is still+active. Traces record `never_retire {id, ok}` with no constraint+text (#644).## Background job waits (#640)diff --git a/src/playbook.zig b/src/playbook.zigindex 01fc0016..f139df3d 100644--- a/src/playbook.zig+++ b/src/playbook.zig@@ -281,15 +281,20 @@ pub fn add(io: Io, arena: Allocator, text_in: []const u8, source: Source, provenreturn .{ .ok = true, .id = id };}-/// Tombstone `id`. Returns false when no live item carries it, so a caller-/// can say "unknown id" instead of writing a tombstone for nothing.-pub fn retire(io: Io, arena: Allocator, id: []const u8) bool {- if (find(load(io, arena), id) == null) return false;+/// Why a `retire` did or did not land. A write failure is not "unknown id":+/// the item is still live and the caller must say so (#644).+pub const Retire = enum { ok, unknown, write_failed };++/// Tombstone `id`. `.unknown` when no live item carries it (no tombstone+/// written). `.write_failed` when the item exists but the ledger append+/// did not land — the constraint stays active.+pub fn retire(io: Io, arena: Allocator, id: []const u8) Retire {+ if (find(load(io, arena), id) == null) return .unknown;var aw: Io.Writer.Allocating = .init(arena);var s: std.json.Stringify = .{ .writer = &aw.writer };- s.write(.{ .v = @as(u8, 1), .op = "retire", .id = id, .t = util.unixMs(io) }) catch return false;- aw.writer.writeByte('\n') catch return false;- return appendLine(io, aw.writer.buffered());+ s.write(.{ .v = @as(u8, 1), .op = "retire", .id = id, .t = util.unixMs(io) }) catch return .write_failed;+ aw.writer.writeByte('\n') catch return .write_failed;+ return if (appendLine(io, aw.writer.buffered())) .ok else .write_failed;}fn writeSection(w: *Io.Writer, items: []const Item, source: Source, header: []const u8, cap_items: usize, cap_bytes: usize) !void {diff --git a/src/playbook_glue.zig b/src/playbook_glue.zigindex 58eb1f01..bce866f1 100644--- a/src/playbook_glue.zig+++ b/src/playbook_glue.zig@@ -73,15 +73,43 @@ pub fn noteConstraint(agent: *Agent, input: std.json.Value) ExecResult {return .{ .text = std.fmt.allocPrint(agent.arena, "constraint recorded as {s}. It is now in {s} and rides every subagent, workflow and pipeline brief from here on, in this session and in later ones — you do not need to restate it.", .{ r.id, playbook.path }) catch "constraint recorded", .is_error = false };}+/// Privacy-safe operational trace: id + success, never constraint text (#644).+fn emitRetire(root: *Agent, id: []const u8, ok: bool) void {+ if (root.tracer) |tr| tr.write(.{+ .t = tr.elapsedMs(),+ .ev = "never_retire",+ .id = id,+ .ok = ok,+ });+}++fn applyRetire(root: *Agent, arena: Allocator, id: []const u8) playbook.Retire {+ const result = playbook.retire(root.io, arena, id);+ emitRetire(root, id, result == .ok);+ return result;+}+fn retireOne(root: *Agent, arena: Allocator, out: *Io.Writer, id: []const u8, needle: []const u8) !void {- if (playbook.retire(root.io, arena, id)) {- @import("prompt_cache_hud.zig").noteBust(.playbook);- refreshRoot(root, arena);- try out.print("retired {s} — it no longer rides new briefs (the record stays in the log as a tombstone)\n", .{id});- } else try out.print("no live playbook item matching '{s}' — /never lists them (id or unique text)\n", .{needle});+ switch (applyRetire(root, arena, id)) {+ .ok => {+ @import("prompt_cache_hud.zig").noteBust(.playbook);+ refreshRoot(root, arena);+ try out.print("retired {s} — it no longer rides new briefs (the record stays in the log as a tombstone)\n", .{id});+ },+ .write_failed => try out.print("could not write the retirement tombstone to {s} — {s} is still active\n", .{ playbook.path, id }),+ .unknown => try out.print("no live playbook item matching '{s}' — /never lists them (id or unique text)\n", .{needle}),+ }try out.flush();}+/// First whitespace-separated token and the rest. Tabs count as separators+/// so `/never rm<TAB>pb-…` is `rm`, not a new constraint named "rm\t…".+fn wordAndRest(arg: []const u8) struct { word: []const u8, rest: []const u8 } {+ const t = std.mem.trim(u8, arg, " \t");+ const end = std.mem.indexOfAny(u8, t, " \t") orelse t.len;+ return .{ .word = t[0..end], .rest = std.mem.trim(u8, t[end..], " \t") };+}+fn list(io: Io, arena: Allocator, out: *Io.Writer) !void {const items = playbook.load(io, arena);if (items.len == 0) {@@ -144,9 +172,9 @@ pub fn applyUserOverride(root: *Agent, arena: Allocator, user_text: []const u8)if (items.len == 0) return 0;var n: usize = 0;if (extractPlaybookId(user_text)) |id| {- if (playbook.retire(root.io, arena, id)) n += 1;+ if (applyRetire(root, arena, id) == .ok) n += 1;} else if (playbook.findByUniqueText(items, user_text)) |item| {- if (playbook.retire(root.io, arena, item.id)) n += 1;+ if (applyRetire(root, arena, item.id) == .ok) n += 1;}if (n > 0) {@import("prompt_cache_hud.zig").noteBust(.playbook);@@ -161,6 +189,7 @@ pub fn applyUserOverride(root: *Agent, arena: Allocator, user_text: []const u8)/// /never TTY: searchable picker + two confirms; else list/// /never <text> record a user constraint/// /never rm <id|text> retire one (id, or a unique text fragment)+/// /never rm TTY picker, else list + usage — never add "rm" (#644)pub fn command(root: *Agent, arena: Allocator, line: []const u8, out: *Io.Writer) !bool {const trimmed = std.mem.trim(u8, line, " \t\r");const rest = for ([_][]const u8{ "/never", "/constraint" }) |name| {@@ -176,8 +205,23 @@ pub fn command(root: *Agent, arena: Allocator, line: []const u8, out: *Io.Writer}return true;}- if (std.mem.startsWith(u8, arg, "rm ") or std.mem.startsWith(u8, arg, "remove ")) {- const needle = std.mem.trim(u8, arg[if (arg[0] == 'r' and arg[1] == 'm') 3 else 7..], " \t");+ const parts = wordAndRest(arg);+ if (std.mem.eql(u8, parts.word, "rm") or std.mem.eql(u8, parts.word, "remove")) {+ // `/never rm` with no needle used to fall through to add("rm"), leaving+ // the live constraint untouched and no tombstone (#644). List (or the+ // TTY picker) instead; never record the verb as a constraint.+ if (parts.rest.len == 0) {+ switch (try playbook_pick.interactive(root, arena, out)) {+ .fallback => {+ try out.print("rm needs an id or unique text fragment — /never lists them\n", .{});+ try list(root.io, arena, out);+ },+ .handled => {},+ .retire => |id| try retireOne(root, arena, out, id, id),+ }+ return true;+ }+ const needle = parts.rest;const items = playbook.load(root.io, arena);const id = if (playbook.find(items, needle) != null) needle else if (playbook.findByUniqueText(items, needle)) |item| item.id else needle;try retireOne(root, arena, out, id, needle);diff --git a/src/playbook_tests.zig b/src/playbook_tests.zigindex 2e773909..82b9ee7b 100644--- a/src/playbook_tests.zig+++ b/src/playbook_tests.zig@@ -89,7 +89,7 @@ test "store round trip: add/dedupe/retire through the real .graff/playbook.jsonl// Retire is a TOMBSTONE: the add record stays on disk, and the// file only ever grows.const before = (try Io.Dir.cwd().readFileAlloc(io, playbook.path, arena, .limited(1 << 20))).len;- try std.testing.expect(playbook.retire(io, arena, first.id));+ try std.testing.expectEqual(playbook.Retire.ok, playbook.retire(io, arena, first.id));const after = try Io.Dir.cwd().readFileAlloc(io, playbook.path, arena, .limited(1 << 20));try std.testing.expect(after.len > before);try std.testing.expect(std.mem.indexOf(u8, after, "never use emojis in code comments") != null); // history intact@@ -98,7 +98,7 @@ test "store round trip: add/dedupe/retire through the real .graff/playbook.jsonltry std.testing.expectEqual(playbook.Source.learned, live[0].source);// Retiring an unknown id writes nothing and says so.- try std.testing.expect(!playbook.retire(io, arena, "pb-deadbeef"));+ try std.testing.expectEqual(playbook.Retire.unknown, playbook.retire(io, arena, "pb-deadbeef"));// Re-adding a retired item works: the later op wins.try std.testing.expect(playbook.add(io, arena, "never use emojis in code comments", .user, "user:4").ok);@@ -373,6 +373,81 @@ test "/never rm matches unique text and applyUserOverride honors an explicit for}.body);}+test "/never rm without a needle does not add or retire (#644)" {+ try inScratch(struct {+ fn body(io: Io, arena: std.mem.Allocator) !void {+ var aw: Io.Writer.Allocating = .init(arena);+ var root = stubRoot(std.testing.allocator, arena, &aw.writer);+ try std.testing.expect(try glue.command(&root, arena, "/never no navigation dots or scroll hints", &aw.writer));+ try std.testing.expectEqual(@as(usize, 1), playbook.load(io, arena).len);+ const id = try arena.dupe(u8, playbook.load(io, arena)[0].id);++ aw.clearRetainingCapacity();+ try std.testing.expect(try glue.command(&root, arena, "/never rm", &aw.writer));+ const bare = aw.writer.buffered();+ try std.testing.expect(std.mem.indexOf(u8, bare, "rm needs an id") != null);+ try std.testing.expect(std.mem.indexOf(u8, bare, "retired") == null);+ const after_bare = playbook.load(io, arena);+ try std.testing.expectEqual(@as(usize, 1), after_bare.len);+ try std.testing.expectEqualStrings(id, after_bare[0].id);+ try std.testing.expectEqualStrings("no navigation dots or scroll hints", after_bare[0].text);+ const disk = try Io.Dir.cwd().readFileAlloc(io, playbook.path, arena, .limited(1 << 20));+ try std.testing.expect(std.mem.indexOf(u8, disk, "\"op\":\"retire\"") == null);++ aw.clearRetainingCapacity();+ try std.testing.expect(try glue.command(&root, arena, "/never remove", &aw.writer));+ try std.testing.expectEqual(@as(usize, 1), playbook.load(io, arena).len);+ try std.testing.expect(std.mem.indexOf(u8, aw.writer.buffered(), "recorded") == null);++ aw.clearRetainingCapacity();+ const tabbed = try std.fmt.allocPrint(arena, "/never rm\t{s}", .{id});+ try std.testing.expect(try glue.command(&root, arena, tabbed, &aw.writer));+ try std.testing.expect(std.mem.indexOf(u8, aw.writer.buffered(), "retired") != null);+ try std.testing.expectEqual(@as(usize, 0), playbook.load(io, arena).len);+ const after = try Io.Dir.cwd().readFileAlloc(io, playbook.path, arena, .limited(1 << 20));+ try std.testing.expect(std.mem.indexOf(u8, after, "\"op\":\"retire\"") != null);+ }+ }.body);+}++test "/never rm emits a privacy-safe never_retire trace (#644)" {+ try inScratch(struct {+ fn body(io: Io, arena: std.mem.Allocator) !void {+ var taw: Io.Writer.Allocating = .init(arena);+ var tracer: @import("trace.zig").Tracer = .{+ .io = io,+ .gpa = std.testing.allocator,+ .out = &taw.writer,+ .start = Io.Timestamp.now(io, .awake),+ .identity = .{ .run_id = "t", .pid = 1, .session_id = "s" },+ };+ var aw: Io.Writer.Allocating = .init(arena);+ var root = stubRoot(std.testing.allocator, arena, &aw.writer);+ root.tracer = &tracer;++ try std.testing.expect(try glue.command(&root, arena, "/never no scroll hints", &aw.writer));+ const id = try arena.dupe(u8, playbook.load(io, arena)[0].id);++ taw.clearRetainingCapacity();+ try std.testing.expect(try glue.command(&root, arena, "/never rm pb-deadbeef", &aw.writer));+ const miss = taw.writer.buffered();+ try std.testing.expect(std.mem.indexOf(u8, miss, "\"ev\":\"never_retire\"") != null);+ try std.testing.expect(std.mem.indexOf(u8, miss, "\"ok\":false") != null);+ try std.testing.expect(std.mem.indexOf(u8, miss, "scroll") == null);++ taw.clearRetainingCapacity();+ const rm = try std.fmt.allocPrint(arena, "/never rm {s}", .{id});+ try std.testing.expect(try glue.command(&root, arena, rm, &aw.writer));+ const hit = taw.writer.buffered();+ try std.testing.expect(std.mem.indexOf(u8, hit, "\"ev\":\"never_retire\"") != null);+ try std.testing.expect(std.mem.indexOf(u8, hit, id) != null);+ try std.testing.expect(std.mem.indexOf(u8, hit, "\"ok\":true") != null);+ try std.testing.expect(std.mem.indexOf(u8, hit, "scroll") == null);+ try std.testing.expectEqual(@as(usize, 0), playbook.load(io, arena).len);+ }+ }.body);+}+test "note_constraint (#381): appends user items only, is idempotent, and can never retire one" {try inScratch(struct {fn body(io: Io, arena: std.mem.Allocator) !void {