graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Expand ACP intent coverage across malformed metadata and protocol boundaries
yxlyx191145022b84
diff --git a/scripts/test-acp-slash-typo.py b/scripts/test-acp-slash-typo.pyindex 0509be10..814a2689 100755--- a/scripts/test-acp-slash-typo.py+++ b/scripts/test-acp-slash-typo.py@@ -4,6 +4,7 @@ with the terminal's refusal and nearest spelling, exactly like the TUI refusesit, and never costs a model turn. Paths and prose still reach the model."""import importlib.utilimport json+import queueimport sysimport tempfileimport time@@ -64,7 +65,51 @@ def run(binary):print("ACP slash typo: answered locally with a suggestion, no model call; paths still reach the model: ok")-def run_explicit(binary):+def finish_prompt(a, start, response, protocol):+ """v2 acknowledges insertion early; wait for idle, not just the ack."""+ assert "error" not in response, response+ if protocol == 1:+ assert response["result"]["stopReason"] == "end_turn", response+ return+ assert response["result"]["messageId"].startswith("msg_user_"), response+ deadline = time.monotonic() + 35+ while time.monotonic() < deadline:+ for event in a.events[start:]:+ update = event.get("params", {}).get("update", {})+ if update.get("sessionUpdate") == "state_update" and update.get("state") == "idle":+ assert update.get("stopReason") == "end_turn", update+ return+ if b"\n" not in a.pending:+ try:+ chunk = a.chunks.get(timeout=.1)+ except queue.Empty:+ continue+ assert chunk is not None, a.exit_detail()+ a.pending += chunk+ while b"\n" in a.pending:+ line, a.pending = a.pending.split(b"\n", 1)+ a.events.append(json.loads(line))+ raise AssertionError("v2 prompt never reached idle")+++OMIT = object()+++def user_texts(request):+ """Read decoded text blocks, so UTF-8 and whitespace checks are exact."""+ texts = []+ for message in request["messages"]:+ if message.get("role") != "user":+ continue+ content = message.get("content", "")+ if isinstance(content, str):+ texts.append(content)+ else:+ texts.extend(block.get("text", "") for block in content if block.get("type") == "text")+ return texts+++def run_explicit(binary, protocol):"""Literal Harness prompts reach the model and remain resumable (#1814)."""with tempfile.TemporaryDirectory(prefix="graff-acp-command-intent-") as temporary:base = Path(temporary).resolve()@@ -73,31 +118,71 @@ def run_explicit(binary):home.mkdir()model = ScriptedModel([], exhausted_text="synthetic prompt answered")port = model.start(0)- a = Acp(binary, cwd, home, port)+ env = {"GRAFF_ACP_V2": "1"} if protocol == 2 else {}+ a = Acp(binary, cwd, home, port, extra_env=env)original = "/ widgets are ordinary text; synthetic parser task."+ unicode_task = "/你好 café e\u0301 🧪"try:- a.request("initialize", {"protocolVersion": 1})+ init = a.request("initialize", {"protocolVersion": protocol, "capabilities": {},+ "info": {"name": "synthetic-test", "version": "1"}})+ assert init["result"]["protocolVersion"] == protocol, initsid = a.request("session/new", {"cwd": str(cwd), "mcpServers": []})["result"]["sessionId"]- def prompt(text, selected=False):+ def prompt(text, selected=False, meta=OMIT):start = len(a.events)- turn = a.request("session/prompt", {- "sessionId": sid,- "prompt": [{"type": "text", "text": text}],- "_meta": {"harness/command": selected},- }, 35)- assert turn["result"]["stopReason"] == "end_turn", turn+ params = {"sessionId": sid, "prompt": [{"type": "text", "text": text}]}+ if selected is not OMIT:+ params["_meta"] = {"harness/command": selected}+ if meta is not OMIT:+ params["_meta"] = meta+ turn = a.request("session/prompt", params, 35)+ finish_prompt(a, start, turn, protocol)return reply_text(a, start)- for text in [original, "/", "/help", "/unknown prose", "/goal discuss this literally",- "/review discuss this literally", "/issue discuss this literally",- "/btw discuss this literally", "/loop discuss this literally", "intro\n/help"]:+ literals = [original, "/", "/help", "/unknown prose", "/goal discuss this literally",+ "/review discuss this literally", "/issue discuss this literally",+ "/btw discuss this literally", "/loop discuss this literally", "intro\n/help",+ unicode_task, " /help ", "\t/goal whitespace literal\r\n", "\n/help\n",+ "/help\n**bold** `code` [link](https://example.invalid)",+ "/help\n```text\n/goal not selected\n```", "// comment, not a command",+ "/help\u00a0nonbreaking space"]+ for text in literals:count = len(model.requests)reply = prompt(text)assert "synthetic prompt answered" in reply, (text, reply)assert len(model.requests) == count + 1, text- encoded = json.dumps(text)[1:-1]- assert encoded in json.dumps(model.requests[-1]["messages"]), text+ assert text in user_texts(model.requests[-1]), repr(text)++ # Same worker and same text: intent must be recomputed every time.+ flags = [(False, False), (True, True), (None, False), (OMIT, True),+ ("true", False), (True, True), ("false", False), (OMIT, True),+ (0, False), (True, True), (1, False), (OMIT, True),+ ([], False), (True, True), ({}, False), (OMIT, True)]+ for flag, command in flags:+ count = len(model.requests)+ reply = prompt("/help", selected=flag)+ assert len(model.requests) == count + (not command), (flag, reply)+ assert ("synthetic prompt answered" in reply) == (not command), (flag, reply)+ if not command:+ assert "/help" in user_texts(model.requests[-1]), flag+ for meta in [None, False, [], {}, {"unrelated": False}]:+ count = len(model.requests)+ reply = prompt("/help", meta=meta)+ assert reply.strip() and "synthetic prompt answered" not in reply, (meta, reply)+ assert len(model.requests) == count, meta++ # A false framed claim and even explicit selection cannot turn an+ # advisory agent room delivery into a user command.+ for selected in [False, True]:+ for from_user in [False, None, "true"]:+ count = len(model.requests)+ reply = prompt("/help", meta={"harness/command": selected, "harness/room": {+ "room_id": "synthetic-room", "from_member": "test-agent",+ "from_user": from_user, "framed": True}})+ assert "synthetic prompt answered" in reply, (selected, from_user, reply)+ assert len(model.requests) == count + 1+ latest = user_texts(model.requests[-1])[-1]+ assert latest.startswith("[room message from test-agent") and "advisory]: /help" in latest, latestcount = len(model.requests)reply = prompt("/help", selected=True)assert "synthetic prompt answered" not in reply and reply.strip(), reply@@ -119,25 +204,43 @@ def run_explicit(binary):resumed_model = ScriptedModel([], exhausted_text="synthetic retry answered")port = resumed_model.start(0)- b = Acp(binary, cwd, home, port)+ b = Acp(binary, cwd, home, port, extra_env=env)try:- b.request("initialize", {"protocolVersion": 1})- loaded = b.request("session/load", {"sessionId": sid, "cwd": str(cwd), "mcpServers": []})+ b.request("initialize", {"protocolVersion": protocol, "capabilities": {},+ "info": {"name": "synthetic-test", "version": "1"}})+ method = "session/resume" if protocol == 2 else "session/load"+ loaded = b.request(method, {"sessionId": sid, "cwd": str(cwd), "mcpServers": []})assert "error" not in loaded, loaded+ start = len(b.events)turn = b.request("session/prompt", {"sessionId": sid,"prompt": [{"type": "text", "text": "retry the synthetic task"}],"_meta": {"harness/command": False},}, 35)- assert turn["result"]["stopReason"] == "end_turn", turn- assert original in json.dumps(resumed_model.requests[-1]["messages"]), "retry lost the original task"+ finish_prompt(b, start, turn, protocol)+ restored = user_texts(resumed_model.requests[-1])+ for text in [original, unicode_task, literals[-1]]:+ assert text in restored, ("retry lost a literal task", repr(text))+ assert "synthetic retry answered" in reply_text(b, start)+ # Restart/load must not cache the previous command's true flag.+ start = len(b.events)+ turn = b.request("session/prompt", {+ "sessionId": sid, "prompt": [{"type": "text", "text": "/help"}],+ "_meta": {"harness/command": False},+ }, 35)+ finish_prompt(b, start, turn, protocol)+ assert len(resumed_model.requests) == 2+ assert "/help" in user_texts(resumed_model.requests[-1])finally:b.close()resumed_model.stop()- print("ACP explicit command intent: literal slash/goal/review/issue/side/loop prompts reach model; selected commands stay local; restart preserves original task: ok")+ print(f"ACP explicit command intent v{protocol}: 18 literals, 16 alternating flags, "+ "5 legacy metadata shapes, 6 agent room envelopes, 3 selected commands, "+ "2 restarted prompts; exact text and durable history: ok")if __name__ == "__main__":binary = Path(sys.argv[1] if len(sys.argv) > 1 else REPO / "zig-out/bin/graff").resolve()run(binary)- run_explicit(binary)+ for protocol in [1, 2]:+ run_explicit(binary, protocol)diff --git a/src/acp_engine_test.zig b/src/acp_engine_test.zigindex e45452f6..36279db3 100644--- a/src/acp_engine_test.zig+++ b/src/acp_engine_test.zig@@ -43,6 +43,178 @@ test "explicit command intent keeps literal slash prompts out of command dispatc}}+test "explicit command intent metadata preserves legacy defaults and rejects malformed flags" {+ const selected = @import("acp_command_intent.zig").selected;+ const Case = struct { json: []const u8, commands: bool };+ const cases = [_]Case{+ .{ .json = "null", .commands = true },+ .{ .json = "[]", .commands = true },+ .{ .json = "false", .commands = true },+ .{ .json = "{}", .commands = true },+ .{ .json = "{\"harness/command\":false}", .commands = true },+ .{ .json = "{\"_meta\":null}", .commands = true },+ .{ .json = "{\"_meta\":false}", .commands = true },+ .{ .json = "{\"_meta\":[]}", .commands = true },+ .{ .json = "{\"_meta\":{}}", .commands = true },+ .{ .json = "{\"_meta\":{\"other\":false}}", .commands = true },+ .{ .json = "{\"_meta\":{\"harness/command\":true}}", .commands = true },+ .{ .json = "{\"_meta\":{\"harness/command\":false}}", .commands = false },+ .{ .json = "{\"_meta\":{\"harness/command\":null}}", .commands = false },+ .{ .json = "{\"_meta\":{\"harness/command\":\"true\"}}", .commands = false },+ .{ .json = "{\"_meta\":{\"harness/command\":\"false\"}}", .commands = false },+ .{ .json = "{\"_meta\":{\"harness/command\":0}}", .commands = false },+ .{ .json = "{\"_meta\":{\"harness/command\":1}}", .commands = false },+ .{ .json = "{\"_meta\":{\"harness/command\":1.5}}", .commands = false },+ .{ .json = "{\"_meta\":{\"harness/command\":[]}}", .commands = false },+ .{ .json = "{\"_meta\":{\"harness/command\":{}}}", .commands = false },+ };+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ try std.testing.expect(selected(null));+ for (cases) |case| {+ const params = try std.json.parseFromSliceLeaky(std.json.Value, arena.allocator(), case.json, .{});+ try std.testing.expectEqual(case.commands, selected(params));+ }+}++const IntentFixture = struct {+ turns: usize = 0,+ slashes: usize = 0,+ inserted: usize = 0,+ last: []const u8 = "",++ fn turn(ctx: *anyopaque, _: Allocator, text: []const u8) anyerror![]const u8 {+ const self: *IntentFixture = @ptrCast(@alignCast(ctx));+ self.turns += 1;+ self.last = text;+ return "model answered";+ }+ fn slash(ctx: *anyopaque, _: Allocator, text: []const u8) anyerror!?[]const u8 {+ const self: *IntentFixture = @ptrCast(@alignCast(ctx));+ self.slashes += 1;+ if (std.mem.eql(u8, text, "/fail")) return error.ApiError;+ return if (std.mem.startsWith(u8, text, "/")) "command executed" else null;+ }+ fn after(ctx: *anyopaque, _: Allocator, _: []const u8, _: ?std.json.Value) void {+ const self: *IntentFixture = @ptrCast(@alignCast(ctx));+ self.inserted += 1;+ }+};++fn intentInit(d: *Dispatch, a: Allocator, w: *Io.Writer, draft: bool) !void {+ const v2 = @import("acp_v2.zig");+ v2.resetForTest();+ v2.gate = draft;+ try handleLine(d, a, w, "{\"id\":1,\"method\":\"initialize\",\"params\":{\"protocolVersion\":2,\"capabilities\":{},\"info\":{\"name\":\"test\",\"version\":\"1\"}}}");+ try std.testing.expectEqual(draft, v2.on());+}++fn intentPrompt(d: *Dispatch, a: Allocator, w: *Io.Writer, text: []const u8, suffix: []const u8) !void {+ const encoded = try std.json.Stringify.valueAlloc(a, text, .{});+ const request = try std.fmt.allocPrint(a, "{{\"id\":2,\"method\":\"session/prompt\",\"params\":{{\"prompt\":[{{\"type\":\"text\",\"text\":{s}}}]{s}}}}}", .{ encoded, suffix });+ try handleLine(d, a, w, request);+}++test "explicit command intent resets across prompts including malformed metadata and command failure v1 v2" {+ defer @import("acp_v2.zig").resetForTest();+ const Case = struct { text: []const u8 = "/help", suffix: []const u8, commands: bool };+ const cases = [_]Case{+ .{ .suffix = ",\"_meta\":{\"harness/command\":false}", .commands = false },+ .{ .suffix = "", .commands = true },+ .{ .suffix = ",\"_meta\":{\"harness/command\":null}", .commands = false },+ .{ .suffix = ",\"_meta\":{\"harness/command\":true}", .commands = true },+ .{ .suffix = ",\"_meta\":{\"harness/command\":\"true\"}", .commands = false },+ .{ .suffix = ",\"_meta\":null", .commands = true },+ .{ .suffix = ",\"_meta\":{\"harness/command\":1}", .commands = false },+ .{ .text = "/fail", .suffix = ",\"_meta\":{\"harness/command\":true}", .commands = true },+ .{ .suffix = ",\"_meta\":{\"harness/command\":false}", .commands = false },+ .{ .suffix = ",\"_meta\":{}", .commands = true },+ };+ for ([_]bool{ false, true }) |draft| {+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ var out: Io.Writer.Allocating = .init(a);+ var fixture: IntentFixture = .{};+ var d: Dispatch = .{ .turn = IntentFixture.turn, .slash = IntentFixture.slash, .after_user = IntentFixture.after, .ctx = &fixture };+ try intentInit(&d, a, &out.writer, draft);+ var turns: usize = 0;+ var slashes: usize = 0;+ for (cases) |case| {+ out.clearRetainingCapacity();+ try intentPrompt(&d, a, &out.writer, case.text, case.suffix);+ try std.testing.expectEqual(case.commands, d.commands);+ if (case.commands) slashes += 1 else turns += 1;+ try std.testing.expectEqual(turns, fixture.turns);+ try std.testing.expectEqual(slashes, fixture.slashes);+ try std.testing.expectEqual(turns, fixture.inserted);+ if (!case.commands) try std.testing.expectEqualStrings(case.text, fixture.last);+ const output = out.writer.buffered();+ if (std.mem.eql(u8, case.text, "/fail")) {+ try std.testing.expect(std.mem.indexOf(u8, output, "ApiError") != null);+ } else {+ try std.testing.expect(std.mem.indexOf(u8, output, "end_turn") != null);+ const expected = if (case.commands) "command executed" else "model answered";+ try std.testing.expect(std.mem.indexOf(u8, output, expected) != null);+ }+ }+ }+}++test "explicit command intent preserves known commands UTF8 Markdown and whitespace byte for byte v1 v2" {+ defer @import("acp_v2.zig").resetForTest();+ const literals = [_][]const u8{+ "/", "/help", "/goal literal objective", "/review literal review", "/issue literal report",+ "/btw literal aside", "/loop literal loop", "/resuem", " /help ", "\t/goal literal\r\n",+ "/help\n**bold** `code` [link](https://example.invalid)",+ "/你好 café e\u{301} 🧪",+ "\n/help\n", "intro\n/help",+ };+ for ([_]bool{ false, true }) |draft| {+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ var out: Io.Writer.Allocating = .init(a);+ var fixture: IntentFixture = .{};+ var d: Dispatch = .{ .turn = IntentFixture.turn, .slash = IntentFixture.slash, .after_user = IntentFixture.after, .ctx = &fixture };+ try intentInit(&d, a, &out.writer, draft);+ for (literals, 0..) |text, i| {+ out.clearRetainingCapacity();+ try intentPrompt(&d, a, &out.writer, text, ",\"_meta\":{\"harness/command\":false}");+ try std.testing.expectEqualStrings(text, fixture.last);+ try std.testing.expectEqual(i + 1, fixture.turns);+ try std.testing.expectEqual(i + 1, fixture.inserted);+ try std.testing.expectEqual(@as(usize, 0), fixture.slashes);+ try std.testing.expect(std.mem.indexOf(u8, out.writer.buffered(), "end_turn") != null);+ }+ }+}++test "explicit command intent cannot bypass agent room framing v1 v2" {+ defer @import("acp_v2.zig").resetForTest();+ for ([_]bool{ false, true }) |draft| {+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ var out: Io.Writer.Allocating = .init(a);+ var fixture: IntentFixture = .{};+ var d: Dispatch = .{ .turn = IntentFixture.turn, .slash = IntentFixture.slash, .ctx = &fixture };+ try intentInit(&d, a, &out.writer, draft);+ for ([_][]const u8{ "false", "true" }) |flag| {+ for ([_][]const u8{ "false", "null", "\"true\"" }) |from_user| {+ const suffix = try std.fmt.allocPrint(a, ",\"_meta\":{{\"harness/command\":{s},\"harness/room\":{{\"room_id\":\"synthetic\",\"from_member\":\"test-agent\",\"from_user\":{s},\"framed\":true}}}}", .{ flag, from_user });+ out.clearRetainingCapacity();+ const before = fixture.turns;+ try intentPrompt(&d, a, &out.writer, "/help", suffix);+ try std.testing.expectEqual(before + 1, fixture.turns);+ try std.testing.expect(std.mem.startsWith(u8, fixture.last, "[room message from test-agent"));+ try std.testing.expect(std.mem.indexOf(u8, fixture.last, "advisory]: /help") != null);+ try std.testing.expect(std.mem.indexOf(u8, out.writer.buffered(), "command executed") == null);+ }+ }+ }+}+test "slash commands refresh occupancy before their terminal response" {const Fixture = struct {fn slash(_: *anyopaque, _: Allocator, _: []const u8) anyerror!?[]const u8 {