graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Preserve tool outcomes across desktop protocol updates
Rach Pradhan3dc9ba539257
diff --git a/apps/native/lib/acp.test.ts b/apps/native/lib/acp.test.tsindex 8c20d462..abe73de9 100644--- a/apps/native/lib/acp.test.ts+++ b/apps/native/lib/acp.test.ts@@ -18,6 +18,65 @@ describe("parseRpcLine", () => {});describe("applyAcpUpdate", () => {+ it("replaces ACP tool content, preserves omitted fields, and honors an empty content array", () => {+ let turn = applyAcpUpdate(emptyTurn(), {+ sessionUpdate: "tool_call", toolCallId: "snapshot-tool", title: "shell",+ kind: "execute", status: "in_progress", rawInput: { command: "echo fixture" },+ });+ const output = (text: string) => ({+ sessionUpdate: "tool_call_update", toolCallId: "snapshot-tool", status: "in_progress",+ content: [{ type: "content", content: { type: "text" as const, text } }],+ });+ turn = applyAcpUpdate(turn, output("first snapshot"));+ turn = applyAcpUpdate(turn, output("second snapshot"));+ turn = applyAcpUpdate(turn, output("second snapshot"));+ assert.deepEqual(turn.tools[0]?.detail.map(d => d.text), ["echo fixture", "second snapshot"]);+ turn = applyAcpUpdate(turn, { sessionUpdate: "tool_call_update", toolCallId: "snapshot-tool", status: "completed", rawInput: null });+ assert.deepEqual(turn.tools[0]?.detail.map(d => d.text), ["echo fixture", "second snapshot"]);+ assert.equal(turn.tools[0]?.status, "ok");+ turn = applyAcpUpdate(turn, { sessionUpdate: "tool_call_update", toolCallId: "snapshot-tool", content: [] });+ assert.deepEqual(turn.tools[0]?.detail.map(d => d.text), ["echo fixture"]);+ assert.equal(turn.tools[0]?.status, "ok");+ });++ it("keeps a rejected edit visible without claiming a file change", () => {+ let turn = applyAcpUpdate(emptyTurn(), {+ sessionUpdate: "tool_call", toolCallId: "batch-rejected", title: "fixture.txt",+ kind: "edit", status: "in_progress",+ rawInput: { path: "fixture.txt", edits: [{ old_string: "old", new_string: "new" }] },+ });+ turn = applyAcpUpdate(turn, {+ sessionUpdate: "tool_call_update", toolCallId: "batch-rejected", status: "failed",+ content: [{ type: "content", content: { type: "text", text: "edit span 2/2 failed (no batch changes written)" } }],+ });+ assert.equal(turn.tools[0]?.status, "error");+ assert.match(turn.tools[0]?.detail.at(-1)?.text ?? "", /no batch changes written/);+ assert.deepEqual(turn.diffs, []);+ });++ it("does not replace an earlier successful diff with a later edit failure", () => {+ let turn = applyAcpUpdate(emptyTurn(), {+ sessionUpdate: "tool_call", toolCallId: "first-edit", title: "fixture.txt",+ kind: "edit", status: "in_progress", rawInput: { path: "fixture.txt", old_string: "old", new_string: "new" },+ });+ turn = applyAcpUpdate(turn, {+ sessionUpdate: "tool_call_update", toolCallId: "first-edit", status: "completed",+ content: [{ type: "content", content: { type: "text", text: "replaced 1 span" } }],+ });+ const successful = turn.diffs;+ turn = applyAcpUpdate(turn, {+ sessionUpdate: "tool_call", toolCallId: "second-edit", title: "fixture.txt",+ kind: "edit", status: "in_progress", rawInput: { path: "fixture.txt" },+ });+ turn = applyAcpUpdate(turn, {+ sessionUpdate: "tool_call_update", toolCallId: "second-edit", status: "failed",+ content: [{ type: "content", content: { type: "text", text: "old_string not found (no batch changes written)" } }],+ });+ assert.equal(turn.tools[1]?.status, "error");+ assert.deepEqual(turn.diffs, successful);+ assert.equal(turn.diffs.length, 1);+ });+it("surfaces gui_ask_user as an ask with options", () => {const turn = applyAcpUpdate(emptyTurn(), {sessionUpdate: "gui_ask_user",diff --git a/apps/native/lib/acp.ts b/apps/native/lib/acp.tsindex 913adddd..8d98eaae 100644--- a/apps/native/lib/acp.ts+++ b/apps/native/lib/acp.ts@@ -168,7 +168,8 @@ function upsertTool(turn: AssistantTurn, update: Extract<AcpUpdate, { toolCallId? "error": "running": (prev?.status ?? "running");- const rawInput = update.rawInput && typeof update.rawInput === "object" ? update.rawInput : {};+ const hasInput = !!update.rawInput && typeof update.rawInput === "object";+ const rawInput = hasInput ? update.rawInput! : {};// codedbpro-style tools say `file`, catalog tools say `path`.const path =typeof rawInput.path === "string"@@ -176,17 +177,17 @@ function upsertTool(turn: AssistantTurn, update: Extract<AcpUpdate, { toolCallId: typeof rawInput.file === "string"? rawInput.file: prev?.path;- const contentItems = "content" in update && Array.isArray(update.content) ? update.content : [];+ const hasContent = "content" in update && Array.isArray(update.content);+ const contentItems = hasContent ? update.content! : [];const contentText = contentItems.map((item) => item.content?.text).filter((t): t is string => typeof t === "string").join("\n");- const fromInput = detailFromInput(rawInput);- const detail = contentText- ? [...(prev?.detail ?? fromInput), { text: contentText }]- : fromInput.length- ? fromInput- : (prev?.detail ?? []);+ const inputDetail = hasInput ? boundToolDetail(detailFromInput(rawInput)) : (prev?.acpDetail?.input ?? []);+ const contentDetail = hasContent+ ? boundToolDetail(contentText ? [{ text: contentText }] : [])+ : (prev?.acpDetail?.content ?? (prev && !prev.acpDetail ? prev.detail : []));+ const detail = [...inputDetail, ...contentDetail];const mcp = mcpParts(title);const label = prev && !kind ? prev.name : labelForKind(kind, title);const command = typeof rawInput.command === "string" ? firstLine(rawInput.command) : undefined;@@ -201,6 +202,7 @@ function upsertTool(turn: AssistantTurn, update: Extract<AcpUpdate, { toolCallIdchip: chipRaw === label || humanize(chipRaw) === label ? "" : chipRaw,status,detail: boundToolDetail(detail),+ acpDetail: { input: inputDetail, content: contentDetail },mcpAppId: mcpAppId(contentText) ?? prev?.mcpAppId,htmlArtifactId: htmlArtifactId(contentText) ?? prev?.htmlArtifactId,htmlArtifactTool: !!htmlPreview,@@ -227,7 +229,7 @@ function upsertTool(turn: AssistantTurn, update: Extract<AcpUpdate, { toolCallIdpendingBreak: turn.text.length > 0 ? true : turn.pendingBreak,status: status === "running" || turn.status === "thinking" || turn.status === "ask" ? "streaming" : turn.status,};- if ((merged.icon === "write" || kind === "edit") && (status === "ok" || status === "error")) {+ if ((merged.icon === "write" || kind === "edit") && status === "ok") {return { ...next, diffs: mergeDiff(next, merged, contentText) };}return next;diff --git a/apps/native/lib/graff-events.ts b/apps/native/lib/graff-events.tsindex 8bdebab7..c5942594 100644--- a/apps/native/lib/graff-events.ts+++ b/apps/native/lib/graff-events.ts@@ -56,6 +56,8 @@ export type ToolRow = {chip: string;status: "running" | "ok" | "error" | "interrupted";detail: { text: string; tone?: "add" }[];+ /** ACP updates replace content independently of input; both stay bounded. */+ acpDetail?: { input: { text: string; tone?: "add" }[]; content: { text: string; tone?: "add" }[] };path?: string;/** Wall-clock bracket, measured client-side — rows tick while running. */startedAt?: number;diff --git a/apps/native/package.json b/apps/native/package.jsonindex 01abfbe4..5e3bbb3d 100644--- a/apps/native/package.json+++ b/apps/native/package.json@@ -15,6 +15,7 @@"test:electron": "bun test electron/policy.test.cjs electron/computer.test.cjs","test:webauthn": "node scripts/test-webauthn.mjs","test:desktop": "bun test lib ./electron/*.test.cjs",+ "test:acp-recovery": "bun scripts/test-acp-recovery.mjs","test:session-navigation": "GRAFF_VISUAL_SUITE=session-navigation bun scripts/test-visual.mjs","test:keyboard": "GRAFF_VISUAL_SUITE=keyboard bun scripts/test-visual.mjs","test:tab-drag": "GRAFF_VISUAL_SUITE=tab-drag bun scripts/test-visual.mjs",diff --git a/apps/native/scripts/test-acp-recovery.mjs b/apps/native/scripts/test-acp-recovery.mjsnew file mode 100644index 00000000..48b7df15--- /dev/null+++ b/apps/native/scripts/test-acp-recovery.mjs@@ -0,0 +1,54 @@+import assert from 'node:assert/strict';+import { mkdtemp, readFile, rm } from 'node:fs/promises';+import { tmpdir } from 'node:os';+import { dirname, join, resolve } from 'node:path';+import { fileURLToPath } from 'node:url';+import { spawnSync } from 'node:child_process';+import { applyAcpUpdate, emptyTurn, finishAcpTurn, parseRpcLine } from '../lib/acp.ts';++const root = resolve(dirname(fileURLToPath(import.meta.url)), '../../..');+const temporary = await mkdtemp(join(tmpdir(), 'graff-gui-acp-'));+try {+ const capture = join(temporary, 'updates.json');+ const run = spawnSync('python3', [join(root, 'scripts/test-acp-released-fixes.py'),+ process.argv[2] ?? join(root, 'zig-out/bin/graff'), '--capture', capture],+ { cwd: root, encoding: 'utf8', timeout: 120_000 });+ assert.equal(run.error, undefined, run.error?.message);+ assert.equal(run.status, 0, `${run.stdout}\n${run.stderr}`);+ const { messages } = JSON.parse(await readFile(capture, 'utf8'));+ const turns = [];+ let turn = emptyTurn();+ let rejectedBatch = false;+ let largeHandle = false;+ for (const message of messages) {+ const parsed = parseRpcLine(JSON.stringify(message));+ assert.ok(parsed);+ if (parsed.method === 'session/update') {+ const update = parsed.params.update;+ turn = applyAcpUpdate(turn, update);+ if (typeof update.rawInput?.id === 'number' && update.rawInput.id >= 2 ** 32) {+ assert.ok(Number.isSafeInteger(update.rawInput.id));+ largeHandle = true;+ }+ const text = (Array.isArray(update.content) ? update.content : []).map(item => item.content?.text ?? '').join('\n');+ if (update.status === 'failed' && text.includes('no batch changes written')) {+ assert.deepEqual(turn.diffs, []);+ assert.equal(turn.tools.at(-1)?.status, 'error');+ rejectedBatch = true;+ }+ } else if (parsed.result?.stopReason) {+ turns.push(finishAcpTurn(turn));+ turn = emptyTurn();+ }+ }+ assert.equal(rejectedBatch, true);+ assert.equal(largeHandle, true);+ assert.equal(turns.length, 3);+ assert.deepEqual(turns[0].tools.map(tool => tool.status), ['error', 'ok']);+ assert.equal(turns[0].diffs.length, 1);+ assert.equal(turns[1].tools.filter(tool => tool.status === 'error').length, 2);+ assert.equal(turns[2].tools[0]?.status, 'ok');+ console.log('PASS GUI ACP recovery: real wire errors, corrected edit, large handles, and follow-up render correctly');+} finally {+ await rm(temporary, { recursive: true, force: true });+}diff --git a/scripts/eval-tier1.sh b/scripts/eval-tier1.shindex 4d29c92e..7b35ffa4 100755--- a/scripts/eval-tier1.sh+++ b/scripts/eval-tier1.sh@@ -185,7 +185,8 @@ if wanted shell; thenfipython3 scripts/eval/process_guard.py --timeout 180 zig build-exe src/shell_identity_probe.zig -lc -femit-bin="$probe_dir/identity" &&python3 scripts/eval/process_guard.py --timeout 60 python3 scripts/test-shell-identity.py "$probe_dir/identity" &&- python3 scripts/eval/process_guard.py --timeout 90 python3 scripts/test-shell-restart.py zig-out/bin/graff+ python3 scripts/eval/process_guard.py --timeout 90 python3 scripts/test-shell-restart.py zig-out/bin/graff &&+ python3 scripts/eval/process_guard.py --timeout 90 python3 scripts/test-acp-released-fixes.py zig-out/bin/graff); then :; elserecord_fail shellfidiff --git a/scripts/test-acp-released-fixes.py b/scripts/test-acp-released-fixes.pynew file mode 100644index 00000000..0231c836--- /dev/null+++ b/scripts/test-acp-released-fixes.py@@ -0,0 +1,183 @@+#!/usr/bin/env python3+"""Offline ACP integration: atomic edits and stale shell controls reach clients honestly."""+import argparse+import json+import os+from pathlib import Path+import queue+import re+import signal+import subprocess+import sys+import tempfile+import threading+import time++REPO = Path(__file__).resolve().parents[1]+sys.path.insert(0, str(REPO / 'scripts/eval'))+from mock_model import ScriptedModel+++def result_text(body):+ return [m.get('content', '') for m in body.get('messages', []) if m.get('role') == 'tool'][-1]+++def tool(name, **arguments):+ return {'tool': name, 'arguments': arguments}+++class Model(ScriptedModel):+ def __init__(self, workspace):+ super().__init__([])+ self.workspace = workspace+ self.handle = None+ self.error = None++ def next_reply(self, body):+ with self._lock:+ self.requests.append(body)+ n = len(self.requests)+ try:+ if n == 1:+ return tool('edit_file', path='proof.txt', edits=[+ {'old_string': 'alpha', 'new_string': 'ALPHA'},+ {'old_string': 'missing', 'new_string': 'BETA'}])+ if n == 2:+ assert (self.workspace / 'proof.txt').read_text() == 'alpha\nbeta\n'+ assert 'no batch changes written' in result_text(body)+ return tool('edit_file', path='proof.txt', edits=[+ {'old_string': 'alpha', 'new_string': 'ALPHA'},+ {'old_string': 'beta', 'new_string': 'BETA'}])+ if n == 3:+ assert (self.workspace / 'proof.txt').read_text() == 'ALPHA\nBETA\n'+ return {'text': 'Batch recovered.'}+ if n == 4:+ return tool('shell', action='run', command='printf ACP-LIVE-JOB; sleep 12', run_in_background=True)+ if n == 5:+ match = re.search(r'\[job (\d+) started:', result_text(body))+ assert match, result_text(body)+ self.handle = int(match.group(1))+ assert 2**32 <= self.handle <= 2**53 - 1+ time.sleep(.1)+ return tool('bash_output', id=1, wait_ms=0)+ if n == 6:+ assert 'no live owner' in result_text(body)+ assert 'ACP-LIVE-JOB' not in result_text(body)+ return tool('bash_kill', id=1)+ if n == 7:+ assert 'no process was stopped' in result_text(body)+ return tool('shell', action='output', id=self.handle, wait_ms=0)+ if n == 8:+ assert 'ACP-LIVE-JOB' in result_text(body), result_text(body)+ assert 'running' in result_text(body), result_text(body)+ return tool('shell', action='kill', id=self.handle)+ if n == 9:+ assert 'killed' in result_text(body), result_text(body)+ return {'text': 'Stale handles refused; live handle usable.'}+ if n == 10:+ return tool('read_file', path='proof.txt')+ if n == 11:+ assert 'ALPHA' in result_text(body) and 'BETA' in result_text(body)+ return {'text': 'Follow-up succeeded.'}+ raise AssertionError(f'unexpected model request {n}')+ except Exception as exc:+ self.error = repr(exc)+ return {'text': 'Fixture assertion failed.'}+++def terminal_updates(events):+ return [e['params']['update'] for e in events if e.get('method') == 'session/update'+ and e.get('params', {}).get('update', {}).get('sessionUpdate') == 'tool_call_update'+ and e['params']['update'].get('status') in ('completed', 'failed')+ and e['params']['update'].get('content')]+++def text(update):+ return ''.join(c.get('content', {}).get('text', '') for c in update.get('content', []))+++def main():+ parser = argparse.ArgumentParser()+ parser.add_argument('binary', type=Path, nargs='?', default=REPO / 'zig-out/bin/graff')+ parser.add_argument('--capture', type=Path, help='Write real ACP updates for offline client reducer tests')+ args = parser.parse_args()+ with tempfile.TemporaryDirectory(prefix='graff-acp-released-') as temporary:+ workspace = Path(temporary)+ (workspace / 'proof.txt').write_text('alpha\nbeta\n')+ config = workspace / 'mcp.json'+ config.write_text('{"mcpServers":{}}')+ model = Model(workspace)+ port = model.start(0)+ env = {k: v for k, v in os.environ.items() if not k.endswith('_API_KEY') and not k.startswith(('GRAFF_', 'HARNESS_'))}+ env.update(HOME=temporary, AI_GATEWAY_API_KEY='local', GRAFF_VERCEL_URL=f'http://127.0.0.1:{port}/v1/chat/completions',+ GRAFF_MCP_CONFIG=str(config), GRAFF_NO_TELEMETRY='1', GRAFF_FLEET='off', GRAFF_NO_SMOLIFY='1',+ GRAFF_NO_CODEDB_GUARD='1', GRAFF_BEHAVIOR_UPLOAD='off', NO_COLOR='1')+ process = None+ captured = []+ try:+ with (workspace / 'stderr.log').open('w+') as stderr:+ process = subprocess.Popen([str(args.binary.resolve()), 'acp', '--model', 'vercel', '--yolo', '--old', '--no-lean'],+ cwd=workspace, env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=stderr, text=True, start_new_session=True)+ messages = queue.Queue()+ def reader():+ for line in process.stdout:+ try: messages.put(json.loads(line))+ except ValueError: pass+ messages.put(None)+ threading.Thread(target=reader, daemon=True).start()+ def call(method, params, identifier):+ process.stdin.write(json.dumps(dict(jsonrpc='2.0', method=method, params=params, id=identifier)) + '\n')+ process.stdin.flush()+ updates = []+ deadline = time.monotonic() + 30+ while True:+ message = messages.get(timeout=max(.01, deadline - time.monotonic()))+ assert message is not None, 'ACP exited before responding'+ captured.append(message)+ if message.get('id') == identifier:+ assert 'error' not in message, message+ return message.get('result', {}), updates+ updates.append(message)+ init, _ = call('initialize', {'protocolVersion': 1}, 1)+ assert init.get('protocolVersion') == 1, init+ session, _ = call('session/new', {'cwd': temporary, 'mcpServers': []}, 2)+ sid = session['sessionId']+ def prompt(message, identifier):+ result, updates = call('session/prompt', {'sessionId': sid, 'prompt': [{'type': 'text', 'text': message}]}, identifier)+ assert result.get('stopReason') == 'end_turn', result+ assert model.error is None, model.error+ return updates+ edits = terminal_updates(prompt('Apply the scripted batch, correct its invalid span, then finish.', 3))+ assert len(edits) == 2, edits+ assert [u['status'] for u in edits] == ['failed', 'completed'], edits+ assert 'no batch changes written' in text(edits[0])+ assert '2 edit span(s)' in text(edits[1])+ shell = terminal_updates(prompt('Start a job; reject legacy output and kill handles, then read and stop the valid handle.', 4))+ assert len(shell) == 5, shell+ assert shell[1]['status'] == shell[2]['status'] == 'failed', shell+ assert 'no live owner' in text(shell[1]) and 'no process was stopped' in text(shell[2])+ assert 'ACP-LIVE-JOB' not in text(shell[1])+ assert shell[3]['status'] == 'completed' and 'ACP-LIVE-JOB' in text(shell[3]), shell[3]+ follow = terminal_updates(prompt('Read the edited file and confirm the session still works.', 5))+ assert len(follow) == 1 and follow[0]['status'] == 'completed', follow+ assert len(model.requests) == 11+ if args.capture:+ args.capture.parent.mkdir(parents=True, exist_ok=True)+ fd = os.open(args.capture, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)+ with os.fdopen(fd, 'w') as output:+ json.dump({'edit': edits, 'shell': shell, 'follow': follow, 'messages': captured}, output, indent=2)+ print('PASS ACP: atomic edit failure/recovery, stale shell controls failed, live u64 handle usable, follow-up succeeded')+ finally:+ if process:+ if process.poll() is None:+ process.stdin.close()+ try: process.wait(timeout=4)+ except subprocess.TimeoutExpired:+ os.killpg(process.pid, signal.SIGKILL)+ process.wait(timeout=2)+ process.stdout.close()+ model.stop()+++if __name__ == '__main__':+ main()