Skip to content

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

Python

Preserve notification provenance for internal session reminders

justrachedc6181cce23
diff --git a/docs/adr/0099-gui-saved-sessions-are-snapshots.md b/docs/adr/0099-gui-saved-sessions-are-snapshots.md
index 31733931..1dcac42a 100644
--- a/docs/adr/0099-gui-saved-sessions-are-snapshots.md
+++ b/docs/adr/0099-gui-saved-sessions-are-snapshots.md
@@ -29,6 +29,10 @@ stale or rejected proposals and must not override that saved state. Older
files without the field retain history-based reconstruction. This does not
turn a saved checklist into live execution status.
+Harness-authored budget, compaction, and completion reminders use the same
+notification provenance as generated wakes; they never become the latest human
+request. Actual one-shot prompts retain their human provenance.
+
Generated wakes retain `_graff_origin: "notification"` in saved message objects.
Their provider role remains `user`, but wire serializers omit this internal field.
The GUI renders these as expandable session notices and excludes them from prompt
diff --git a/evals/harness_behavior.jsonl b/evals/harness_behavior.jsonl
index 408ed68c..82969226 100644
--- a/evals/harness_behavior.jsonl
+++ b/evals/harness_behavior.jsonl
@@ -90,3 +90,4 @@
{"id": "informational-summary-bounded", "why": "#884: repository summaries retain bounded evidence gathering and receive one provenance-marked checkpoint without coding-work completion pressure.", "prompt": "go through the codebase and summarize what it does", "workspace_seed": {"README.md": "# Notebook\nA desktop note-taking app. App owns startup, Controller routes user actions, and Store persists notes. See src/App.swift and src/Controller.swift.\n", "src/App.swift": "struct App { let controller: Controller }\n", "src/Controller.swift": "struct Controller { let store: Store }\n", "src/Store.swift": "struct Store { func save(_ text: String) {} }\n", "src/Feature0.swift": "struct Feature0 {}\n", "src/Feature1.swift": "struct Feature1 {}\n", "src/Feature2.swift": "struct Feature2 {}\n", "src/Feature3.swift": "struct Feature3 {}\n", "src/Feature4.swift": "struct Feature4 {}\n", "src/Feature5.swift": "struct Feature5 {}\n", "src/Feature6.swift": "struct Feature6 {}\n", "src/Feature7.swift": "struct Feature7 {}\n", "src/Feature8.swift": "struct Feature8 {}\n", "src/Feature9.swift": "struct Feature9 {}\n", "src/Feature10.swift": "struct Feature10 {}\n", "src/Feature11.swift": "struct Feature11 {}\n", "tests/Feature0Tests.swift": "// Tests for feature 0\n", "tests/Feature1Tests.swift": "// Tests for feature 1\n", "tests/Feature2Tests.swift": "// Tests for feature 2\n", "tests/Feature3Tests.swift": "// Tests for feature 3\n", "tests/Feature4Tests.swift": "// Tests for feature 4\n", "tests/Feature5Tests.swift": "// Tests for feature 5\n", "tests/Feature6Tests.swift": "// Tests for feature 6\n", "tests/Feature7Tests.swift": "// Tests for feature 7\n", "tests/Feature8Tests.swift": "// Tests for feature 8\n", "tests/Feature9Tests.swift": "// Tests for feature 9\n"}, "script": [{"tool": "read_file", "arguments": {"path": "README.md"}}, {"tool": "read_file", "arguments": {"path": "src/App.swift"}}, {"tool": "read_file", "arguments": {"path": "src/Controller.swift"}}, {"tool": "read_file", "arguments": {"path": "src/Store.swift"}}, {"text": "Notebook is a desktop note-taking application. App owns startup, Controller routes user actions, and Store persists notes. The remaining feature modules and tests support that flow."}], "assert": [{"events_at_most": {"match": {"type": "tool_call"}, "count": 4}}, {"events_at_least": {"match": {"type": "tool_call", "name": "read_file"}, "count": 4}}, {"request_contains": {"index": -1, "text": "Summary scope checkpoint:"}}, {"request_lacks": {"index": 0, "text": "Summary scope checkpoint:"}}, {"request_lacks": {"index": -1, "text": "If the user requested a change, a description alone is not completion:"}}, {"final_text_contains": "Notebook is a desktop note-taking application. App owns startup, Controller routes user actions, and Store persists notes. The remaining feature modules and tests support that flow."}]}
{"id": "informational-summary-no-edit-bounce", "why": "#884: a completed summary of supplied evidence must not trigger the edit-oriented first-answer retry.", "args": ["--lean"], "prompt": "Summarize this architecture: App owns startup, Controller routes user actions, and Store persists notes.", "script": [{"text": "Notebook is a desktop note-taking application. App owns startup, Controller routes user actions, and Store persists notes. The remaining feature modules and tests support that flow."}], "assert": [{"events_at_most": {"match": {"type": "tool_call"}, "count": 0}}, {"request_lacks": {"index": -1, "text": "If the user requested a change, a description alone is not completion:"}}, {"request_contains": {"index": 0, "text": "Task scope: first distinguish"}}, {"final_text_contains": "Notebook is a desktop note-taking application. App owns startup, Controller routes user actions, and Store persists notes. The remaining feature modules and tests support that flow."}]}
{"id": "informational-mixed-request-keeps-mutation", "why": "#884: a mixed fix-and-summary request must still read, apply the root-cause change, and verify in the fixture project.", "prompt": "Fix slug.py so whitespace-only labels return an empty string, add a regression test, run the project tests, and summarize the change.", "workspace_seed": {"slug.py": "def slug(label):\n result = \"-\".join(label.strip().lower().split())\n if not result:\n raise ValueError(\"empty label\")\n return result\n", "test_slug.py": "import unittest\nfrom slug import slug\n\nclass SlugTests(unittest.TestCase):\n def test_words(self):\n self.assertEqual(slug(\"Hello World\"), \"hello-world\")\n", "README.md": "Run tests with python3 -m unittest discover.\n"}, "script": [{"tool": "read_file", "arguments": {"path": "slug.py"}}, {"tool": "read_file", "arguments": {"path": "test_slug.py"}}, {"tool": "read_file", "arguments": {"path": "README.md"}}, {"tool": "edit_file", "arguments": {"path": "slug.py", "old_string": " if not result:\n raise ValueError(\"empty label\")\n", "new_string": ""}}, {"tool": "edit_file", "arguments": {"path": "test_slug.py", "old_string": "import unittest\nfrom slug import slug\n\nclass SlugTests(unittest.TestCase):\n def test_words(self):\n self.assertEqual(slug(\"Hello World\"), \"hello-world\")\n", "new_string": "import unittest\nfrom slug import slug\n\nclass SlugTests(unittest.TestCase):\n def test_words(self):\n self.assertEqual(slug(\"Hello World\"), \"hello-world\")\n\n def test_whitespace_only(self):\n self.assertEqual(slug(\" \\t\\n \"), \"\")\n"}}, {"tool": "bash", "arguments": {"command": "python3 -m unittest discover"}}, {"text": "Whitespace-only labels now return an empty string. Both project tests passed."}], "assert": [{"events_at_least": {"match": {"type": "tool_call", "name": "read_file"}, "count": 3}}, {"events_at_least": {"match": {"type": "tool_call", "name": "edit_file"}, "count": 2}}, {"events_at_most": {"match": {"type": "tool_call_finished", "is_error": true}, "count": 0}}, {"request_contains": {"index": -1, "text": "Ran 2 tests"}}, {"request_contains": {"index": -1, "text": "OK"}}, {"request_lacks": {"index": -1, "text": "Summary scope checkpoint:"}}, {"final_text_contains": "Both project tests passed."}]}
+{"id": "internal-budget-note-keeps-wire-clean", "why": "#912: internal budget reminders must remain effective without exposing saved notification metadata on the model wire.", "args": ["--max-model-calls", "2"], "prompt": "Read the checklist and summarize its current state.", "script": [{"tool": "todo_read", "arguments": {}}, {"text": "The checklist is empty."}], "assert": [{"request_contains": {"index": -1, "text": "[budget landing]"}}, {"request_contains": {"index": -1, "text": "Read the checklist and summarize its current state."}}, {"request_lacks": {"index": -1, "text": "_graff_origin"}}, {"final_text_contains": "The checklist is empty."}]}
diff --git a/src/agent_empty_completion.zig b/src/agent_empty_completion.zig
index 6e75b5d0..7367eb3e 100644
--- a/src/agent_empty_completion.zig
+++ b/src/agent_empty_completion.zig
@@ -88,7 +88,7 @@ pub const PendingWork = struct {
state.nudged = true;
const goals = @import("goal_state.zig");
const body = try std.fmt.allocPrint(self.arena, "{s}\n\n{s}", .{ note, goals.renderTodos(self, goals.currentEpoch(self.goal)) });
- try self.messages.append(try @import("named_work.zig").userNudge(self.arena, self.provider.kind, body));
+ try self.messages.append(try @import("session_wake.zig").mark(self.arena, try @import("named_work.zig").userNudge(self.arena, self.provider.kind, body)));
if (self.tracer) |tr| tr.note("pending_work", "plain final reconciled; one retry granted");
return null;
}
@@ -129,7 +129,9 @@ test "#745 plain final retries once after tools, then explicitly stops without c
var state: PendingWork = .{};
try std.testing.expect((try state.finish(&self, "I will keep working")) == null);
try std.testing.expectEqual(@as(usize, 1), self.messages.items.len);
- try std.testing.expect(std.mem.indexOf(u8, messages.latestUserText(self.messages.items), "finish") != null);
+ try std.testing.expect(@import("session_wake.zig").isNotice(self.messages.items[0]));
+ const reminder = try std.json.Stringify.valueAlloc(self.arena, self.messages.items[0], .{});
+ try std.testing.expect(std.mem.indexOf(u8, reminder, "finish") != null);
self.tool_calls_this_turn += 1; // tool progress must not refill the allowance
const final = (try state.finish(&self, "I will keep working")).?;
try std.testing.expect(std.mem.indexOf(u8, final, "Root execution has stopped with 1 open checklist items") != null);
@@ -188,7 +190,10 @@ test "#745 reconciliation supports every wire without coercing status requests i
try self.todos.append(self.arena, .{ .content = "finish", .status = "pending" });
var state: PendingWork = .{};
try std.testing.expect((try state.finish(&self, "status")) == null);
- const body = messages.latestUserText(self.messages.items);
+ const notice = self.messages.items[self.messages.items.len - 1];
+ try std.testing.expect(@import("session_wake.zig").isNotice(notice));
+ try std.testing.expectEqualStrings("", messages.latestUserText(self.messages.items));
+ const body = try std.json.Stringify.valueAlloc(self.arena, notice, .{});
try std.testing.expect(std.mem.indexOf(u8, body, "status-only question") != null);
try std.testing.expect(std.mem.indexOf(u8, body, "wait_ms>0") != null);
if (kind == .responses) try std.testing.expectEqualStrings("message", self.messages.items[0].object.get("type").?.string);
diff --git a/src/messages.zig b/src/messages.zig
index b22c6d22..d7029b09 100644
--- a/src/messages.zig
+++ b/src/messages.zig
@@ -83,8 +83,7 @@ fn userPromptText(msg: Value) ?[]const u8 {
/// Interactions rejects a bare `{"role":"user"}` — every entry on that wire is
/// a typed step — so the shape has to follow the wire, not the caller.
pub fn userNote(arena: Allocator, kind: Provider.Kind, text: []const u8) !Value {
- if (kind == .interactions) return @import("interactions_steps.zig").userInput(arena, text);
- return textMessage(arena, "user", text);
+ return @import("session_wake.zig").typedMessage(arena, kind, text);
}
pub fn toolResultMessage(arena: Allocator, kind: Provider.Kind, call_id: []const u8, raw_text: []const u8, is_error: bool) !Value {
diff --git a/src/session_wake.zig b/src/session_wake.zig
index 804818ee..dd0f69a9 100644
--- a/src/session_wake.zig
+++ b/src/session_wake.zig
@@ -22,7 +22,16 @@ pub fn isNotice(m: std.json.Value) bool {
return v == .string and std.mem.eql(u8, v.string, "notification");
}
pub fn message(a: std.mem.Allocator, text: []const u8) !std.json.Value {
- var m = try @import("messages.zig").textMessage(a, "user", text);
+ return mark(a, try @import("messages.zig").textMessage(a, "user", text));
+}
+pub fn typedMessage(a: std.mem.Allocator, kind: @import("provider.zig").Provider.Kind, text: []const u8) !std.json.Value {
+ return mark(a, if (kind == .interactions)
+ try @import("interactions_steps.zig").userInput(a, text)
+ else
+ try @import("messages.zig").textMessage(a, "user", text));
+}
+pub fn mark(a: std.mem.Allocator, source: std.json.Value) !std.json.Value {
+ var m = source;
try m.object.put(a, origin_key, .{ .string = "notification" });
return m;
}
@@ -120,3 +129,23 @@ test "notification metadata stays off chat, responses and anthropic wires includ
}
try std.testing.expect(isNotice(m)); // serialization must not mutate the saved object
}
+
+test "internal notes retain provenance and do not replace the human request" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ const messages = @import("messages.zig");
+ for ([_]@import("provider.zig").Provider.Kind{ .openai, .responses, .anthropic, .interactions }) |kind| {
+ const human = try @import("named_work.zig").userNudge(a, kind, "Explain parser.zig");
+ const note = try messages.userNote(a, kind, "Internal completion reminder");
+ try std.testing.expect(!isNotice(human));
+ try std.testing.expect(isNotice(note));
+ try std.testing.expectEqualStrings("Explain parser.zig", messages.latestUserText(&.{ human, note }));
+ var aw: std.Io.Writer.Allocating = .init(a);
+ var s: std.json.Stringify = .{ .writer = &aw.writer };
+ try writeWire(&s, note);
+ try std.testing.expect(std.mem.indexOf(u8, aw.written(), origin_key) == null);
+ try std.testing.expect(std.mem.indexOf(u8, aw.written(), "Internal completion reminder") != null);
+ if (kind == .interactions) try std.testing.expectEqualStrings("user_input", note.object.get("type").?.string);
+ }
+}