Skip to content

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

Python

Record ACP turn trajectories before context restoration

justracha53a2ce4b905
diff --git a/docs/review-guardrails.md b/docs/review-guardrails.md
index f1a09a89..cc2bcac9 100644
--- a/docs/review-guardrails.md
+++ b/docs/review-guardrails.md
@@ -32,3 +32,9 @@ The three process regressions also run in CI. Set `GRAFF_REVIEW_EVIDENCE` to a p
when running the deadline test to retain its protocol events and harness traces.
These are scripted-model regressions of the actual harness, not a claim about
an unscripted model's review quality.
+
+ACP review turns use the same trajectory recorder as the CLI. Each turn has a
+matching start and finish record; failed or cancelled reviews remain failed in
+the record. Recording happens before the isolated review context is restored.
+The ACP regression checks these records alongside read-only enforcement and
+normal follow-up recovery.
diff --git a/scripts/test-review-acp.py b/scripts/test-review-acp.py
index 23c142df..de1e2221 100644
--- a/scripts/test-review-acp.py
+++ b/scripts/test-review-acp.py
@@ -172,6 +172,22 @@ with tempfile.TemporaryDirectory(prefix="graff-review-acp-probe-") as tmp:
assert len(requests) == 7, len(requests)
assert "parent-only-context" in json.dumps(requests[6])
assert "findings are incomplete" in json.dumps(requests[6])
+ trajectory_rows = []
+ for path in pathlib.Path(tmp, ".graff", "trajectories").glob("*.jsonl"):
+ trajectory_rows.extend(
+ json.loads(line) for line in path.read_text().splitlines()
+ )
+ live_turns = [
+ row
+ for row in trajectory_rows
+ if row.get("kind") == "turn" and row.get("live")
+ ]
+ closed_turns = [
+ row for row in trajectory_rows if row.get("kind") == "turn" and "ok" in row
+ ]
+ assert len(live_turns) == len(closed_turns) == 5, (live_turns, closed_turns)
+ assert [row["id"] for row in live_turns] == [row["id"] for row in closed_turns]
+ assert [row["ok"] for row in closed_turns] == [True, True, True, False, True]
result = dict(
response=response,
model_calls=len(requests),
diff --git a/src/acp_live_turn.zig b/src/acp_live_turn.zig
index ca32c0e8..952b5a5d 100644
--- a/src/acp_live_turn.zig
+++ b/src/acp_live_turn.zig
@@ -11,6 +11,8 @@ const stream = @import("acp_stream.zig");
const review = @import("review.zig");
const messages = @import("messages.zig");
const cancel_source = @import("cancel_source.zig");
+const trace = @import("trace.zig");
+const turn_trace = @import("mainloop_trace.zig");
const proto = @import("acp_protocol.zig");
pub const LiveTurn = struct {
@@ -19,6 +21,8 @@ pub const LiveTurn = struct {
out: *Io.Writer,
session_id: []const u8 = "",
saw_text: bool = false,
+ prev_turn_id: u64 = 0,
+ prev_prompt_fp: [16]u8 = @splat(0),
inbox: ?*@import("acp_inbox.zig").Inbox = null,
pub fn errorMessage(ctx: *anyopaque, err: anyerror) []const u8 {
@@ -60,7 +64,19 @@ pub const LiveTurn = struct {
self.root.out = null;
main_mod.g_out = null;
}
+ const turn_id: u64 = if (trace.g_traj) |trajectory| blk: {
+ const id = trajectory.nextId();
+ trajectory.setTurn(id);
+ break :blk id;
+ } else 0;
+ self.root.tools_used.clear(self.root.io);
+ self.root.tool_calls_this_turn = 0;
+ self.root.model_calls_this_turn = 0;
+ const before = turn_trace.begin(self.root, self.root.io);
+ turn_trace.recordLive(self.root, text, turn_id, self.prev_turn_id);
+ const started = Io.Timestamp.now(self.root.io, .awake);
const result = providers.runTurnWithFallback(self.root, self.keys, arena, null);
+ turn_trace.record(self.root, self.root.io, arena, text, turn_id, started, result, self.root.effectiveContextTokens(), before, &self.prev_turn_id, &self.prev_prompt_fp);
const isolated = context.restore(self.root);
if (isolated) {
self.root.review_mode = parent_review_mode;