Skip to content

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

Python

note_constraint accepts a standing rule from the user's ask_user answer (#1342)

justracheb69a614c85a
diff --git a/src/ask_user.zig b/src/ask_user.zig
index 8879656c..ed6e1b31 100644
--- a/src/ask_user.zig
+++ b/src/ask_user.zig
@@ -118,6 +118,8 @@ test "noAnswerText keeps cancel as cancel and never calls a lost question a canc
fn finishAnswer(self: *Agent, raw: []const u8) !ExecResult {
const answer = try self.arena.dupe(u8, raw);
+ // #1342: the answer is user-authored text note_constraint may record.
+ @import("ask_user_answers.zig").record(@import("ask_user_answers.zig").turnKey(@import("playbook_glue.zig").currentUserText(self)), answer);
vision.stageGuiImageAttachment(self, answer);
_ = vision_queue.retainReferenced(self, answer);
// Pixels stay queued for flushPending after the text tool result. If the
diff --git a/src/ask_user_answers.zig b/src/ask_user_answers.zig
new file mode 100644
index 00000000..1bc19d14
--- /dev/null
+++ b/src/ask_user_answers.zig
@@ -0,0 +1,70 @@
+//! ask_user answers from the current turn, as sources for note_constraint
+//! (#1342). A standing rule the user types into an ask_user answer is as
+//! user-authored as one in their message, but the answer arrives as a tool
+//! result, so the verbatim check never saw it. Answers are keyed to the user
+//! message they were asked under; a later user turn makes them stale.
+
+const std = @import("std");
+
+const slots = 8;
+const max_bytes = 4096;
+
+const Entry = struct { turn: u64 = 0, len: usize = 0, buf: [max_bytes]u8 = undefined };
+var ring: [slots]Entry = @splat(.{});
+var next: usize = 0;
+var lock: std.atomic.Value(bool) = .init(false);
+
+fn acquire() void {
+ while (lock.swap(true, .acquire)) std.atomic.spinLoopHint();
+}
+fn release() void {
+ lock.store(false, .release);
+}
+
+/// The turn an answer belongs to: the user message it was asked under.
+pub fn turnKey(user_text: []const u8) u64 {
+ return std.hash.Wyhash.hash(0x1342, user_text);
+}
+
+/// Record a user's answer. Longer answers keep their first 4 KiB; a rule
+/// past that point stays unrecordable rather than being accepted on a prefix.
+pub fn record(turn: u64, answer: []const u8) void {
+ acquire();
+ defer release();
+ const n = @min(answer.len, max_bytes);
+ ring[next] = .{ .turn = turn, .len = n };
+ @memcpy(ring[next].buf[0..n], answer[0..n]);
+ next = (next + 1) % slots;
+}
+
+pub fn reset() void {
+ acquire();
+ defer release();
+ ring = @splat(.{});
+ next = 0;
+}
+
+/// Whether `accept(answer, text)` holds for any answer given this turn.
+pub fn any(turn: u64, text: []const u8, accept: *const fn ([]const u8, []const u8) bool) bool {
+ acquire();
+ defer release();
+ for (&ring) |*e| {
+ if (e.len == 0 or e.turn != turn) continue;
+ if (accept(e.buf[0..e.len], text)) return true;
+ }
+ return false;
+}
+
+fn contains(hay: []const u8, needle: []const u8) bool {
+ return std.mem.indexOf(u8, hay, needle) != null;
+}
+
+test "answers count only for the turn they were given in" {
+ reset();
+ defer reset();
+ const turn = turnKey("set up the release");
+ record(turn, "Always run the full suite before tagging. Skip it just this once.");
+ try std.testing.expect(any(turn, "Always run the full suite", contains));
+ try std.testing.expect(!any(turnKey("a later message"), "Always run the full suite", contains));
+ try std.testing.expect(!any(turn, "never tag", contains));
+}
diff --git a/src/issue_789_constraint_tests.zig b/src/issue_789_constraint_tests.zig
index 9e2ed0a9..b3f28042 100644
--- a/src/issue_789_constraint_tests.zig
+++ b/src/issue_789_constraint_tests.zig
@@ -176,3 +176,29 @@ test "#789 v1 user records are legacy unscoped and visibly require review" {
try std.testing.expect(std.mem.indexOf(u8, state, "\"scope\":\"legacy_unscoped\"") != null);
try std.testing.expect(std.mem.indexOf(u8, playbook.authority_note, "require user review with /never") != null);
}
+
+test "#1342 a standing rule typed into an ask_user answer can be recorded verbatim" {
+ try inScratch(struct {
+ fn body(io: Io, arena: std.mem.Allocator) !void {
+ const answers = @import("ask_user_answers.zig");
+ answers.reset();
+ defer answers.reset();
+ var aw: Io.Writer.Allocating = .init(arena);
+ var root = rootStub(arena, &aw.writer);
+ root.named_work_task = "Set up the release branch.";
+ const answer = "Always keep releases on the beta channel until I say otherwise. For this one, skip the changelog.";
+ answers.record(answers.turnKey(glue.currentUserText(&root)), answer);
+
+ // A fragment that drops the qualifier is still refused.
+ try std.testing.expect(glue.noteConstraint(&root, try call(arena, "keep releases on the beta channel", "project")).is_error);
+ const result = glue.noteConstraint(&root, try call(arena, "Always keep releases on the beta channel until I say otherwise.", "project"));
+ try std.testing.expect(!result.is_error);
+ try std.testing.expect(std.mem.indexOf(u8, result.text, "origin=user's ask_user answer") != null);
+ try std.testing.expectEqual(@as(usize, 1), playbook.load(io, arena).len);
+
+ // An answer from an earlier turn is not a source for this one.
+ root.named_work_task = "Now cut the tag.";
+ try std.testing.expect(glue.noteConstraint(&root, try call(arena, "For this one, skip the changelog.", "project")).is_error);
+ }
+ }.body);
+}
diff --git a/src/main.zig b/src/main.zig
index 448a6f04..36513d59 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -579,7 +579,7 @@ test { // ── Unit tests (`zig build test`): pull in tests from imported modu
_ = .{ @import("http2_pool.zig"), @import("agent_stream_h2.zig"), @import("agent_stream_h2_transport_test.zig") }; // h2 stream path, and graff's request/response contract over the pinned http-zig
_ = @import("tui_acp_updates.zig");
_ = @import("acp_preauth.zig"); // credential-free ACP loop must stay in the test root
- _ = .{ @import("acp_protocol.zig"), @import("acp_v2.zig"), @import("acp_v2_prompt.zig"), @import("acp_elicit.zig"), @import("ask_user_args.zig"), @import("acp_engine_test.zig"), @import("subagent_mimo_tests.zig"), @import("mimo_effort_tests.zig"), @import("gateway_picker_catalog.zig") };
+ _ = .{ @import("acp_protocol.zig"), @import("acp_v2.zig"), @import("acp_v2_prompt.zig"), @import("acp_elicit.zig"), @import("ask_user_args.zig"), @import("ask_user_answers.zig"), @import("acp_engine_test.zig"), @import("subagent_mimo_tests.zig"), @import("mimo_effort_tests.zig"), @import("gateway_picker_catalog.zig") };
_ = @import("task_outcome.zig"); // goal-outcome telemetry events
_ = @import("learn_delete.zig"); // #303: its tests were dead until listed here
_ = .{ @import("additional_tests.zig"), @import("req_stats.zig"), @import("exact_reply.zig"), @import("rlm_order_tests.zig"), @import("async_tool_policy.zig"), @import("agent_async_tools.zig"), @import("request_usage_attempts.zig"), @import("learn_formal.zig") };
diff --git a/src/playbook_glue.zig b/src/playbook_glue.zig
index bf3ff848..7859bdbd 100644
--- a/src/playbook_glue.zig
+++ b/src/playbook_glue.zig
@@ -94,6 +94,12 @@ fn exactUserSegment(user: []const u8, text: []const u8) bool {
return false;
}
+/// The user text this turn's constraints must quote: a named work task, else
+/// the latest user message. ask_user answers are keyed to it (#1342).
+pub fn currentUserText(agent: *const Agent) []const u8 {
+ return if (agent.named_work_task.len > 0) agent.named_work_task else messages.latestUserText(agent.messages.items);
+}
+
/// The `note_constraint` meta-tool handler (root-only; the spec lives in
/// root_specs, so a subagent is never even told it exists). Append-only by
/// construction — see this file's header.
@@ -108,12 +114,11 @@ pub fn noteConstraint(agent: *Agent, input: std.json.Value) ExecResult {
};
if (text.len == 0) return .{ .text = "note_constraint needs non-empty exact text from the current user message", .is_error = true };
if (text.len > playbook.max_text) return .{ .text = "constraint NOT recorded: exact text exceeds the 240-byte project-constraint limit; ask the user for a shorter standing rule", .is_error = true };
- const current_user = if (agent.named_work_task.len > 0)
- agent.named_work_task
- else
- messages.latestUserText(agent.messages.items);
- if (current_user.len == 0 or !exactUserSegment(current_user, text)) return .{
- .text = "constraint NOT recorded: `text` must be a complete verbatim sentence or line from the current user message; do not drop task qualifiers, exceptions, or other limiting language",
+ const current_user = currentUserText(agent);
+ const answers = @import("ask_user_answers.zig");
+ const from_user = current_user.len > 0 and exactUserSegment(current_user, text);
+ if (!from_user and !answers.any(answers.turnKey(current_user), text, exactUserSegment)) return .{
+ .text = "constraint NOT recorded: `text` must be a complete verbatim sentence or line from the current user message or the user's ask_user answer this turn; do not drop task qualifiers, exceptions, or other limiting language",
.is_error = true,
};
var prov_buf: [32]u8 = undefined;
@@ -143,7 +148,7 @@ pub fn noteConstraint(agent: *Agent, input: std.json.Value) ExecResult {
"Durable state is saved, but the active prompt could not be refreshed. Do not claim activation; retry note_constraint to reconcile it.";
// ADR 0021: the user just said this. Echoing "constraint recorded" is
// machine state, not progress. The tool result still tells the model.
- return .{ .text = std.fmt.allocPrint(agent.arena, "recorded project constraint {s}: \"{s}\" (scope=project, origin=current user message at {d}). It rides later root, subagent, workflow, and pipeline briefs in this project. Undo without knowing the id via `/never rm <unique text>` or review with `/never`. {s}", .{ r.id, text, now, effect }) catch "project constraint recorded; active prompt refresh status unavailable", .is_error = false };
+ return .{ .text = std.fmt.allocPrint(agent.arena, "recorded project constraint {s}: \"{s}\" (scope=project, origin={s} at {d}). It rides later root, subagent, workflow, and pipeline briefs in this project. Undo without knowing the id via `/never rm <unique text>` or review with `/never`. {s}", .{ r.id, text, if (from_user) "current user message" else "user's ask_user answer", now, effect }) catch "project constraint recorded; active prompt refresh status unavailable", .is_error = false };
}
/// Privacy-safe operational trace: id + success, never constraint text (#644).