Skip to content
Python

fix(gui): the turn summary counts changes that worked, and pairs results by call id (OPE-238)

Devika Vermaeeb3c04d91f6
diff --git a/coworker/engine.py b/coworker/engine.py
index de73b859..d4e92d85 100644
--- a/coworker/engine.py
+++ b/coworker/engine.py
@@ -63,6 +63,7 @@ _REVIEWER_PAUSED_TEXT = (
)
from .permissions import CLEARED_BY_MODE, Mode, PermissionEngine
from .providers import AssistantTurn, ProviderClient, ToolCall
+from .risk import RiskClass, classify as _classify_risk
from .providers.errors import friendly_model_error
from .providers.openai_provider import looks_like_unparsed_tool_call, strip_dangling_tool_tag
from .tools import ToolRegistry
@@ -154,16 +155,30 @@ def _summarizes_turn(gen_fn: Callable[..., AsyncIterator[Event]]) -> Callable[..
"tool_call_id": str(event.data.get("tool_call_id") or ""),
"name": str(event.data.get("name") or ""),
"origin": origin,
+ # "change" or "read": only changes count as changes.
+ "effect": str(event.data.get("approval_effect") or "change"),
"status": str(event.data.get("status") or ""),
+ **(
+ {"tier": str(event.data["approval_tier"])}
+ if event.data.get("approval_tier")
+ else {}
+ ),
**(
{"note": str(event.data["approval_note"])}
if event.data.get("approval_note")
else {}
),
+ **(
+ {"exit_code": event.data["exit_code"]}
+ if "exit_code" in event.data
+ else {}
+ ),
+ **({"timed_out": True} if event.data.get("timed_out") else {}),
+ **({"background": True} if event.data.get("background") else {}),
}
)
elif event.type in _TURN_ENDINGS and unasked:
- yield Event(EventType.TURN_SUMMARY, {"count": len(unasked), "actions": unasked})
+ yield Event(EventType.TURN_SUMMARY, _summary_payload(unasked))
unasked = []
yield event
finally:
@@ -174,6 +189,32 @@ def _summarizes_turn(gen_fn: Callable[..., AsyncIterator[Event]]) -> Callable[..
return wrapper
+def _action_failed(action: dict[str, Any]) -> bool:
+ """A listed action that changed nothing: the call errored, the command exited
+ non-zero, or it timed out. A background start is neither done nor failed."""
+ if action.get("status") != "ok":
+ return True
+ if action.get("timed_out"):
+ return True
+ code = action.get("exit_code")
+ return isinstance(code, int) and code != 0
+
+
+def _summary_payload(actions: list[dict[str, Any]]) -> dict[str, Any]:
+ """`count` is what the header says: changes that ran without asking and worked.
+ Failed attempts and read-only actions are listed, and counted apart, so the
+ number means "things that changed state and that you did not approve"."""
+ changes = [a for a in actions if a.get("effect") != "read"]
+ failed = [a for a in changes if _action_failed(a)]
+ reads = [a for a in actions if a.get("effect") == "read"]
+ return {
+ "count": len(changes) - len(failed),
+ "failed": len(failed),
+ "reads": len(reads),
+ "actions": actions,
+ }
+
+
class TurnEngine:
def __init__(
self,
@@ -1209,7 +1250,9 @@ class TurnEngine:
continue
yield Event(
EventType.TOOL_PROPOSED,
- {"name": tool_call.name, "arguments": tool_call.arguments},
+ # The id lets a surface pair this proposal with its own finished event
+ # (OPE-238): two same-named calls in one batch must not swap results.
+ {"name": tool_call.name, "arguments": tool_call.arguments, "tool_call_id": tool_call.id},
)
self._audit(tool_call, stage="proposed")
if _is_mangled(tool_call):
@@ -2171,8 +2214,27 @@ class TurnEngine:
if delegated:
self._authorized_delegates[tool_call.id] = delegated
+ origin = self._approval_origins.get(tool_call.id)
+ if origin and origin.get("origin") in _UNASKED_ORIGINS:
+ # OPE-238: what the turn summary needs to count honestly: did this call
+ # change anything, and which tier (if any) let it through.
+ origin["effect"] = self._effect(tool_call, metadata, decision.tier)
+ if decision.tier:
+ origin["tier"] = decision.tier
yield True
+ def _effect(self, tool_call: ToolCall, metadata: Any, tier: str) -> str:
+ """"change" or "read", from the risk class the permission engine already uses.
+ A shell command is a change unless the read-only classifier said otherwise;
+ connector and MCP calls that reached a cardless origin are writes or sends
+ (connector reads never gate); a web read is a read."""
+ if tier == "readonly_command":
+ return "read"
+ risk = _classify_risk(tool_call.name, metadata, self.permissions.risk_overrides)
+ if risk in (RiskClass.WRITE_LOCAL, RiskClass.EXEC, RiskClass.EXTERNAL):
+ return "change"
+ return "read"
+
def _execute_sync(self, tool_call: ToolCall) -> tuple[Any, str]:
"""Execute one authorized call (runs in a worker thread)."""
started, clock = time.time(), time.monotonic()
@@ -2237,16 +2299,21 @@ class TurnEngine:
display = result.get("_display") or None
result = {k: v for k, v in result.items() if k != "_display"}
origin = self._approval_origins.pop(tool_call.id, None)
+ # OPE-238: the facts the turn summary shows, kept with the result so a reload
+ # shows the same thing the live view did: whether the call failed, and for a
+ # shell command its exit status, time-out, or background start.
+ outcome = _call_outcome(tool_call, result, status)
if origin:
# Provenance survives reload via the same display-only sidecar as the privacy
# counts (owner ruling 2026-08-24) — `_outbound_messages` strips it, so no
# provider ever sees it.
display = {
**(display or {}),
- "approval_origin": origin.get("origin", ""),
- **({"approval_note": origin["note"]} if origin.get("note") else {}),
- **({"approval_grant": origin["grant"]} if origin.get("grant") else {}),
+ **_origin_fields(origin),
+ **outcome,
}
+ elif outcome:
+ display = {**(display or {}), **outcome}
# OPE-186 change 1: what the model sees (and re-reads on every later turn) is
# bounded here, once, for every tool. Provenance above recorded the full result.
result = toolresult.bound_tool_result(
@@ -2301,15 +2368,8 @@ class TurnEngine:
**({"display": display} if display else {}),
**({"standing_rule": rule} if rule else {}),
# (c) quiet provenance chip — same fields the `_display` sidecar persists.
- **(
- {
- "approval_origin": origin.get("origin", ""),
- **({"approval_note": origin["note"]} if origin.get("note") else {}),
- **({"approval_grant": origin["grant"]} if origin.get("grant") else {}),
- }
- if origin
- else {}
- ),
+ **(_origin_fields(origin) if origin else {}),
+ **{k: v for k, v in outcome.items() if k != "status"},
},
)
@@ -3230,6 +3290,36 @@ def _tool_result_message(tool_call: ToolCall, result: Any) -> dict[str, Any]:
}
+def _origin_fields(origin: dict[str, Any]) -> dict[str, Any]:
+ """The approval-provenance fields, as the TOOL_FINISHED event and the persisted
+ `_display` sidecar both carry them."""
+ return {
+ "approval_origin": origin.get("origin", ""),
+ **({"approval_note": origin["note"]} if origin.get("note") else {}),
+ **({"approval_grant": origin["grant"]} if origin.get("grant") else {}),
+ **({"approval_tier": origin["tier"]} if origin.get("tier") else {}),
+ **({"approval_effect": origin["effect"]} if origin.get("effect") else {}),
+ }
+
+
+def _call_outcome(tool_call: ToolCall, result: Any, status: str) -> dict[str, Any]:
+ """What the turn summary needs beyond the 300-char preview (OPE-238): a non-ok
+ status, and for `run_shell` the exit code (None when unknown), whether it timed
+ out, and whether it was only started in the background."""
+ out: dict[str, Any] = {}
+ if status != "ok":
+ out["status"] = status
+ if tool_call.name == "run_shell" and isinstance(result, dict):
+ if result.get("task_id") and result.get("status") == "running":
+ out["background"] = True
+ elif "exit_code" in result:
+ code = result.get("exit_code")
+ out["exit_code"] = code if isinstance(code, int) and not isinstance(code, bool) else None
+ if result.get("timed_out"):
+ out["timed_out"] = True
+ return out
+
+
def _tool_error_message(tool_call: ToolCall, reason: str) -> dict[str, Any]:
return {
"role": "tool",
diff --git a/docs/headless.md b/docs/headless.md
index 3b0e93ab..f48b5cf3 100644
--- a/docs/headless.md
+++ b/docs/headless.md
@@ -174,8 +174,13 @@ record states it.
A turn in which auto-approve let actions run with no approval card (a safe-change tier or a
reviewer "allow") ends with a `turn_summary` event in `events.jsonl`, just before the event
-that ends the turn. It lists each action's tool call id, tool, origin and status; the audit
-log can be filtered by the same call id (`/v1/audit?call_id=…`).
+that ends the turn. Its `count` is the number of changes that ran and worked; `failed` counts
+the attempts that errored, exited non-zero or timed out, and `reads` the read-only actions
+(a read-only command tier, a web read). `actions` lists each one with its tool call id, tool,
+origin, effect (`change` or `read`), status, the tier that let it through when one did, and
+for a shell command its `exit_code`, `timed_out` or `background` flag. The audit log can be
+filtered by the same call id (`/v1/audit?call_id=…`). `tool_proposed` and `tool_finished`
+events carry the same `tool_call_id`.
The trajectory and the exit code are the stable contract.
diff --git a/surfaces/gui/src/App.tsx b/surfaces/gui/src/App.tsx
index 34515aba..6f65ed01 100644
--- a/surfaces/gui/src/App.tsx
+++ b/surfaces/gui/src/App.tsx
@@ -986,7 +986,7 @@ export function App() {
setTodo(normalizeTodos(d.arguments.todos ?? d.arguments.items));
setItems((p) => [
...p,
- { kind: "tool", id: newId(), name: d.name, args: d.arguments, status: "…" },
+ { kind: "tool", id: newId(), name: d.name, args: d.arguments, status: "…", ...(d.tool_call_id ? { callId: String(d.tool_call_id) } : {}) },
]);
break;
case "permission_required":
@@ -1053,19 +1053,20 @@ export function App() {
setItems(p => [...p, { kind: "teamcreated", teamId: c.team_id, workers: c.workers || [], ts: Date.now() / 1000 }]);
}
setItems((p) =>
- updateLastTool(
- p,
- d.name,
- d.status,
- d.result_preview || d.reason,
- d.display?.hidden_by_filters,
- d.standing_rule,
- d.reviewer_reason,
- d.allow_anyway,
- d.approval_origin,
- d.approval_note,
- d.tool_call_id,
- ),
+ updateLastTool(p, d.name, d.status, d.result_preview || d.reason, {
+ hidden: d.display?.hidden_by_filters,
+ standingRule: d.standing_rule,
+ reviewerReason: d.reviewer_reason,
+ allowAnyway: d.allow_anyway,
+ approvalOrigin: d.approval_origin,
+ approvalNote: d.approval_note,
+ callId: d.tool_call_id,
+ effect: d.approval_effect,
+ tier: d.approval_tier,
+ ...("exit_code" in d ? { exitCode: d.exit_code } : {}),
+ timedOut: d.timed_out,
+ background: d.background,
+ }),
);
// §8.4 breaker: the reviewer paused itself for the rest of the turn — say so
// where the user is looking (persisted server-side for reloads) and on the
@@ -2722,37 +2723,61 @@ function WaitingForAgent({ label }: { label?: string }) {
);
}
-function updateLastTool(
+// Pairs a finished event with its pending tool item. By call id when the engine gave one
+// (OPE-238: two same-named calls in one batch finish in call order, and matching the LAST
+// pending item by name handed each one the other's result, exit code and audit link);
+// otherwise the last pending item with that name, as before.
+export function updateLastTool(
items: Item[],
name: string,
status: string,
- preview?: string,
- hidden?: number,
- standingRule?: string,
- reviewerReason?: string,
- allowAnyway?: boolean,
- approvalOrigin?: string,
- approvalNote?: string,
- callId?: string,
+ preview: string | undefined,
+ fields: {
+ hidden?: number;
+ standingRule?: string;
+ reviewerReason?: string;
+ allowAnyway?: boolean;
+ approvalOrigin?: string;
+ approvalNote?: string;
+ callId?: string;
+ effect?: string;
+ tier?: string;
+ exitCode?: number | null;
+ timedOut?: boolean;
+ background?: boolean;
+ } = {},
): Item[] {
const copy = [...items];
- for (let i = copy.length - 1; i >= 0; i--) {
- const it = copy[i];
- if (it.kind === "tool" && it.name === name && it.status === "…") {
- copy[i] = {
- ...it,
- status,
- preview,
- ...(hidden ? { hidden } : {}),
- ...(standingRule ? { standingRule } : {}),
- ...(reviewerReason ? { reviewerReason } : {}),
- ...(allowAnyway ? { allowAnyway } : {}),
- ...(approvalOrigin ? { approvalOrigin } : {}),
- ...(approvalNote ? { approvalNote } : {}),
- ...(callId ? { callId } : {}),
- };
- break;
+ const { hidden, standingRule, reviewerReason, allowAnyway, approvalOrigin, approvalNote, callId, effect, tier, timedOut, background } = fields;
+ let at = -1;
+ if (callId) at = copy.findIndex((it) => it.kind === "tool" && it.callId === callId && it.status === "…");
+ if (at < 0)
+ for (let i = copy.length - 1; i >= 0; i--) {
+ const it = copy[i];
+ if (it.kind === "tool" && it.name === name && it.status === "…") {
+ at = i;
+ break;
+ }
}
+ if (at >= 0) {
+ const it = copy[at] as Extract<Item, { kind: "tool" }>;
+ copy[at] = {
+ ...it,
+ status,
+ preview,
+ ...(hidden ? { hidden } : {}),
+ ...(standingRule ? { standingRule } : {}),
+ ...(reviewerReason ? { reviewerReason } : {}),
+ ...(allowAnyway ? { allowAnyway } : {}),
+ ...(approvalOrigin ? { approvalOrigin } : {}),
+ ...(approvalNote ? { approvalNote } : {}),
+ ...(callId ? { callId } : {}),
+ ...(effect ? { effect } : {}),
+ ...(tier ? { tier } : {}),
+ ...("exitCode" in fields ? { exitCode: fields.exitCode } : {}),
+ ...(timedOut ? { timedOut } : {}),
+ ...(background ? { background } : {}),
+ };
}
return copy;
}
diff --git a/surfaces/gui/src/components/Transcript.tsx b/surfaces/gui/src/components/Transcript.tsx
index 70cf2d0f..84bacc41 100644
--- a/surfaces/gui/src/components/Transcript.tsx
+++ b/surfaces/gui/src/components/Transcript.tsx
@@ -553,6 +553,14 @@ export function Transcript({ items, running, streamingText, onRetry, onOpenConne
| { item: Item; i: number; sources?: MessageSource[]; steps?: Item[] }
> = [];
let run: TurnItem[] = [];
+ // OPE-238: what ran auto-approved in the turn so far. A notice inside a turn (compaction,
+ // continuation, a saved memory) breaks the step group but not the turn: the block is
+ // emitted once, after the answer, when the next user message arrives or the turn is over.
+ let pendingUnasked: ToolItem[] = [];
+ const emitUnasked = () => {
+ if (pendingUnasked.length) blocks.push({ unasked: pendingUnasked });
+ pendingUnasked = [];
+ };
const flush = (live = false) => {
if (!run.length) return;
const turn = [...run];
@@ -567,11 +575,7 @@ export function Transcript({ items, running, streamingText, onRetry, onOpenConne
if (turn.some((it) => it.kind !== "assistant")) blocks.push({ turn, live });
else turn.forEach((t) => blocks.push({ item: t, i: -1 }));
answers.forEach((a) => blocks.push({ item: a, i: -1 }));
- // OPE-238: a finished turn ends with what ran without asking, after its answer.
- if (!live) {
- const unasked = unaskedTools(turn);
- if (unasked.length) blocks.push({ unasked });
- }
+ if (!live) pendingUnasked.push(...unaskedTools(turn));
};
foldTeamUpdates(items).forEach(({ item, sources, steps }) => {
const i = items.indexOf(item);
@@ -587,10 +591,12 @@ export function Transcript({ items, running, streamingText, onRetry, onOpenConne
return;
} else {
flush();
+ if (item.kind === "user" || item.kind === "connector") emitUnasked();
blocks.push({ item, i, sources, steps });
}
});
flush(!!running);
+ if (!running) emitUnasked();
const lastTurnIndex = blocks.reduce((acc, b, i) => ("turn" in b ? i : acc), -1);
return (
diff --git a/surfaces/gui/src/components/UnaskedSummary.test.tsx b/surfaces/gui/src/components/UnaskedSummary.test.tsx
index f54f151a..af8d09d7 100644
--- a/surfaces/gui/src/components/UnaskedSummary.test.tsx
+++ b/surfaces/gui/src/components/UnaskedSummary.test.tsx
@@ -1,7 +1,7 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { cleanup, fireEvent, render, screen, within } from "@testing-library/react";
import { Transcript } from "./Transcript";
-import { exitCodeOf, patchSummary } from "./UnaskedSummary";
+import { MAX_ROWS, errorOf, exitCodeOf, patchSummary, summarize } from "./UnaskedSummary";
import type { Item } from "../types";
afterEach(cleanup);
@@ -52,10 +52,10 @@ function openSummary() {
return screen.getByTestId("unasked-summary-body");
}
-describe("changes made without asking (OPE-238)", () => {
+describe("changes auto-approved (OPE-238)", () => {
it("is collapsed at rest and counts only the unasked actions", () => {
render(<Transcript items={TURN} onApprove={vi.fn()} />);
- expect(screen.getByTestId("unasked-summary-head").textContent).toContain("3 changes made without asking");
+ expect(screen.getByTestId("unasked-summary-head").textContent).toContain("3 changes auto-approved");
expect(screen.queryByTestId("unasked-summary-body")).toBeNull();
expect(screen.queryByText("Three tasks done.")).toBeNull();
});
@@ -95,8 +95,8 @@ describe("changes made without asking (OPE-238)", () => {
expect(within(commands).getByTestId("unasked-exit").textContent).toBe("exit 0");
// Who let each one through.
- expect(files.textContent).toContain("safe change, no card");
- expect(body.textContent).toContain("auto-approved");
+ expect(files.textContent).toContain("safe change");
+ expect(body.textContent).toContain("reviewer");
// The user-approved deploy is not listed.
expect(body.textContent).not.toContain("make deploy");
@@ -153,7 +153,7 @@ describe("changes made without asking (OPE-238)", () => {
{ kind: "assistant", text: "Fixed." },
];
const { container } = render(<Transcript items={items} onApprove={vi.fn()} />);
- expect(screen.getByTestId("unasked-summary-head").textContent).toContain("1 change made without asking");
+ expect(screen.getByTestId("unasked-summary-head").textContent).toContain("1 change auto-approved");
openSummary();
expect(Array.from(container.querySelectorAll(".diff-del")).map((n) => n.textContent)).toContain("-old line");
expect(Array.from(container.querySelectorAll(".diff-add")).map((n) => n.textContent)).toContain("+new line");
@@ -216,7 +216,7 @@ describe("changes made without asking (OPE-238)", () => {
const heads = screen.getAllByTestId("unasked-summary-head").map((h) => h.textContent);
expect(heads.length).toBe(2);
expect(heads[0]).toContain("3 changes");
- expect(heads[1]).toContain("1 change made");
+ expect(heads[1]).toContain("1 change auto-approved");
});
});
@@ -292,3 +292,212 @@ describe("exitCodeOf", () => {
expect(exitCodeOf('{"exit_code": null}')).toBeNull();
});
});
+
+
+// The count means "things that changed state and that nobody approved". A failed attempt
+// changed nothing; a read-only action is listed apart; a background start is neither.
+describe("the count is honest", () => {
+ const turn = (...tools: Item[]): Item[] => [{ kind: "user", text: "work" }, ...tools, { kind: "assistant", text: "Done." }];
+ const shell = (id: string, command: string, extra: Partial<Extract<Item, { kind: "tool" }>> = {}): Item => ({
+ kind: "tool",
+ id,
+ callId: "c_" + id,
+ name: "run_shell",
+ args: { command },
+ status: "ok",
+ approvalOrigin: "reviewer",
+ ...extra,
+ });
+
+ it("a command that exited non-zero is a failure, not a change, even when the preview hides its exit code", () => {
+ const long = "python -c '" + "x = 1; ".repeat(80) + "'";
+ render(
+ <Transcript
+ items={turn(shell("a", "pytest -q", { exitCode: 0 }), shell("b", long, { exitCode: 1, preview: '{"command": "' + long + '"...' }))}
+ onApprove={vi.fn()}
+ />,
+ );
+ const head = screen.getByTestId("unasked-summary-head");
+ expect(head.textContent).toContain("1 change auto-approved");
+ expect(screen.getByTestId("unasked-failed-count").textContent).toContain("1 failed");
+ openSummary();
+ const exits = screen.getAllByTestId("unasked-exit").map((e) => e.textContent);
+ expect(exits).toEqual(["exit 0", "exit 1"]);
+ });
+
+ it("a timed-out command is a failure and says so", () => {
+ render(<Transcript items={turn(shell("a", "sleep 999", { exitCode: null, timedOut: true }))} onApprove={vi.fn()} />);
+ expect(screen.getByTestId("unasked-summary-head").textContent).toContain("No changes auto-approved");
+ expect(screen.getByTestId("unasked-failed-count").textContent).toContain("1 failed");
+ openSummary();
+ const exit = screen.getByTestId("unasked-exit");
+ expect(exit.textContent).toBe("timed out");
+ expect(exit.className).toContain("text-danger");
+ });
+
+ it("a command started in the background is neither done nor failed", () => {
+ render(<Transcript items={turn(shell("a", "npm run dev", { background: true }), shell("b", "pytest -q", { exitCode: 0 }))} onApprove={vi.fn()} />);
+ expect(screen.getByTestId("unasked-summary-head").textContent).toContain("2 changes auto-approved");
+ expect(screen.queryByTestId("unasked-failed-count")).toBeNull();
+ openSummary();
+ expect(screen.getByTestId("unasked-background").textContent).toBe("started in the background");
+ expect(screen.queryAllByTestId("unasked-exit").length).toBe(1);
+ });
+
+ it("a write that failed is a failure with its error, not a green change", () => {
+ const items = turn({
+ kind: "tool",
+ id: "w",
+ callId: "c_w",
+ name: "write_file",
+ args: { path: "/tmp/gen_poc.py", content: "x" },
+ status: "error",
+ preview: '{"error": "Path escapes allowed roots: /tmp/gen_poc.py", "error_type": "ValueError"}',
+ approvalOrigin: "reviewer",
+ });
+ render(<Transcript items={items} onApprove={vi.fn()} />);
+ expect(screen.getByTestId("unasked-summary-head").textContent).toContain("No changes auto-approved · 1 failed");
+ const body = openSummary();
+ expect(within(body).getByTestId("unasked-failed").textContent).toContain("failed · Path escapes allowed roots");
+ });
+
+ it("read-only actions are listed apart and never counted", () => {
+ const items = turn(
+ shell("r1", "cat notes.md", { approvalOrigin: "gate_tier", tier: "readonly_command", effect: "read" }),
+ shell("r2", "git status", { approvalOrigin: "gate_tier", tier: "readonly_command", effect: "read" }),
+ shell("w", "pytest -q", { exitCode: 0, effect: "change" }),
+ );
+ render(<Transcript items={items} onApprove={vi.fn()} />);
+ const head = screen.getByTestId("unasked-summary-head");
+ expect(head.textContent).toContain("1 change auto-approved");
+ expect(screen.getByTestId("unasked-read-count").textContent).toContain("2 read-only");
+ const body = openSummary();
+ expect(within(body).getAllByTestId("unasked-item").length).toBe(1);
+ expect(body.textContent).not.toContain("cat notes.md");
+ fireEvent.click(within(body).getByTestId("unasked-reads-toggle"));
+ const reads = within(body).getAllByTestId("unasked-read-item").map((n) => n.textContent);
+ expect(reads[0]).toContain("cat notes.md");
+ expect(reads[1]).toContain("git status");
+ });
+
+ it("a turn that only read things shows nothing at all", () => {
+ render(
+ <Transcript
+ items={turn(shell("r1", "cat notes.md", { approvalOrigin: "gate_tier", tier: "readonly_command", effect: "read" }))}
+ onApprove={vi.fn()}
+ />,
+ );
+ expect(screen.queryByTestId("unasked-summary")).toBeNull();
+ });
+
+ it("summarize() is the same rule the engine applies", () => {
+ const s = summarize([
+ shell("a", "x", { exitCode: 0 }),
+ shell("b", "y", { exitCode: 2 }),
+ shell("c", "z", { effect: "read" }),
+ { ...shell("d", "w"), status: "error" },
+ ] as any);
+ expect([s.count, s.failed, s.reads.length, s.changes.length]).toEqual([1, 2, 1, 3]);
+ });
+});
+
+describe("rows stay readable", () => {
+ it("several edits of one file collapse into one row with stacked diffs and a link per edit", () => {
+ const edit = (id: string, old: string, neu: string): Item => ({
+ kind: "tool",
+ id,
+ callId: "c_" + id,
+ name: "replace_in_file",
+ args: { path: "src/app.py", old, new: neu },
+ status: "ok",
+ approvalOrigin: "reviewer",
+ });
+ const items: Item[] = [
+ { kind: "user", text: "turn debug off" },
+ edit("e1", "DEBUG = True", "DEBUG = False"),
+ { kind: "tool", id: "o", callId: "c_o", name: "write_file", args: { path: "docs/x.md", content: "hi" }, status: "ok", approvalOrigin: "gate_tier" },
+ edit("e2", 'LOG = "debug"', 'LOG = "info"'),
+ { kind: "assistant", text: "Done." },
+ ];
+ const onOpenAudit = vi.fn();
+ render(<Transcript items={items} onApprove={vi.fn()} onOpenAudit={onOpenAudit} />);
+ // Three calls, three changes: the count is per change, the rows are per file.
+ expect(screen.getByTestId("unasked-summary-head").textContent).toContain("3 changes auto-approved");
+ const files = within(openSummary()).getByTestId("unasked-group-files");
+ const rows = within(files).getAllByTestId("unasked-item");
+ expect(rows.length).toBe(2);
+ expect(within(rows[0]).getByTestId("unasked-item-line").textContent).toBe("Edited src/app.py · 2 edits");
+ expect(within(rows[0]).getAllByTestId("unasked-diff").length).toBe(2);
+ const links = within(rows[0]).getAllByTestId("unasked-audit");
+ expect(links.length).toBe(2);
+ fireEvent.click(links[1]);
+ expect(onOpenAudit).toHaveBeenCalledWith("c_e2");
+ });
+
+ it("a long list shows the first rows and offers the rest", () => {
+ const many: Item[] = Array.from({ length: MAX_ROWS + 5 }, (_, i) => ({
+ kind: "tool" as const,
+ id: "s" + i,
+ callId: "c" + i,
+ name: "run_shell",
+ args: { command: "cmd " + i },
+ status: "ok",
+ exitCode: 0,
+ approvalOrigin: "reviewer",
+ }));
+ render(<Transcript items={[{ kind: "user", text: "go" }, ...many, { kind: "assistant", text: "Done." }]} onApprove={vi.fn()} />);
+ const body = openSummary();
+ expect(within(body).getAllByTestId("unasked-item").length).toBe(MAX_ROWS);
+ const more = within(body).getByTestId("unasked-show-all");
+ expect(more.textContent).toContain(String(MAX_ROWS + 5));
+ fireEvent.click(more);
+ expect(within(body).getAllByTestId("unasked-item").length).toBe(MAX_ROWS + 5);
+ });
+});
+
+// A notice inside a turn (compaction, continuation, a saved memory) breaks the step group,
+// not the turn: one summary per turn, after its answer, and never while it is running.
+describe("one summary per turn", () => {
+ const first: Item = { kind: "tool", id: "t1", callId: "c1", name: "run_shell", args: { command: "pytest -q" }, status: "ok", exitCode: 0, approvalOrigin: "reviewer" };
+ const notice: Item = { kind: "notice", tone: "info", text: "Context compacted" };
+ const second: Item = { kind: "tool", id: "t2", callId: "c2", name: "run_shell", args: { command: "ruff check" }, status: "ok", exitCode: 0, approvalOrigin: "reviewer" };
+
+ it("nothing shows while the turn is running, even after a mid-turn notice", () => {
+ render(<Transcript items={[{ kind: "user", text: "go" }, first, notice, { ...second, status: "…" }]} running onApprove={vi.fn()} />);
+ expect(screen.queryByTestId("unasked-summary")).toBeNull();
+ });
+
+ it("a finished turn with a mid-turn notice gets one summary, after the answer, counting both halves", () => {
+ render(<Transcript items={[{ kind: "user", text: "go" }, first, notice, second, { kind: "assistant", text: "Done." }]} onApprove={vi.fn()} />);
+ const heads = screen.getAllByTestId("unasked-summary-head");
+ expect(heads.length).toBe(1);
+ expect(heads[0].textContent).toContain("2 changes auto-approved");
+ const answer = screen.getByText("Done.");
+ expect(answer.compareDocumentPosition(screen.getByTestId("unasked-summary")) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
+ });
+
+ it("the previous turn's summary sits before the next user message while the new turn runs", () => {
+ render(
+ <Transcript
+ items={[{ kind: "user", text: "go" }, first, { kind: "assistant", text: "Done." }, { kind: "user", text: "again" }, { ...second, status: "…" }]}
+ running
+ onApprove={vi.fn()}
+ />,
+ );
+ const heads = screen.getAllByTestId("unasked-summary-head");
+ expect(heads.length).toBe(1);
+ expect(heads[0].textContent).toContain("1 change auto-approved");
+ const next = screen.getByText("again");
+ expect(screen.getByTestId("unasked-summary").compareDocumentPosition(next) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
+ });
+});
+
+describe("errorOf", () => {
+ it("reads the error text of a failed result, whole or cut short", () => {
+ expect(errorOf('{"error": "Path escapes allowed roots: /tmp/x", "error_type": "ValueError"}')).toBe("Path escapes allowed roots: /tmp/x");
+ expect(errorOf('{"error": "tool call not executed", "reason": "denied"}')).toBe("denied");
+ expect(errorOf('{"error": "boom", "error_type": "Runt')).toBe("boom");
+ expect(errorOf("plain text")).toBe("");
+ expect(errorOf(undefined)).toBe("");
+ });
+});
diff --git a/surfaces/gui/src/components/UnaskedSummary.tsx b/surfaces/gui/src/components/UnaskedSummary.tsx
index 1c644e17..45f9fc1e 100644
--- a/surfaces/gui/src/components/UnaskedSummary.tsx
+++ b/surfaces/gui/src/components/UnaskedSummary.tsx
@@ -4,11 +4,16 @@ import type { Item } from "../types";
import { humanizeTool } from "../humanize";
import { Icon } from "./Icon";
-// OPE-238: "N changes made without asking". After a turn in which auto-approve let actions
-// run with no approval card (a gate tier, OPE-233, or a reviewer "allow"), the user who was
-// away sees every one of them here, with diffs, without opening the audit log. Collapsed at
-// rest; one click expands. Built from the turn's tool items, whose approval origin is
-// persisted on the tool message, so a reloaded session shows the same block.
+// OPE-238: "N changes auto-approved". After a turn in which auto-approve let actions run
+// with no approval card (a gate tier, OPE-233, or a reviewer "allow"), the user who was
+// away sees every change here, with diffs, without opening the audit log. Collapsed at
+// rest; one click expands. Built from the turn's tool items, whose approval origin,
+// effect and outcome are persisted on the tool message, so a reloaded session shows the
+// same block.
+//
+// The count means "things that changed state and that nobody approved": a read-only
+// action (a command the read-only tier classified, a web read) is listed apart and never
+// counted; an attempt that failed is counted as failed, not as a change.
type ToolItem = Extract<Item, { kind: "tool" }>;
@@ -18,6 +23,8 @@ const FILE_TOOLS = new Set(["write_file", "replace_in_file", "apply_patch", "app
const COMMAND_TOOLS = new Set(["run_shell"]);
// A whole-file write can be long: show its head, and say how much is left.
const MAX_DIFF_LINES = 200;
+// A long unattended turn can leave dozens of rows: show the first ones, offer the rest.
+export const MAX_ROWS = 25;
export function unaskedTools(items: Item[]): ToolItem[] {
return items.filter(
@@ -27,13 +34,56 @@ export function unaskedTools(items: Item[]): ToolItem[] {
}
// A shell result is `{"command", "cwd", "exit_code", "output", …}`; the preview may be cut
-// short, so read the field rather than parse the whole thing.
+// short, so read the field rather than parse the whole thing. The engine now puts the exit
+// code on the item itself (`exitCode`); this is the fallback for older transcripts.
export function exitCodeOf(preview: string | undefined): number | null {
if (!preview) return null;
const m = /"exit_code"\s*:\s*(-?\d+)/.exec(preview);
return m ? Number(m[1]) : null;
}
+// The error text of a failed call's result (`{"error": …}` or `{"reason": …}`), for the chip.
+export function errorOf(preview: string | undefined): string {
+ if (!preview) return "";
+ try {
+ const v = JSON.parse(preview);
+ if (v && typeof v === "object") {
+ const text = v.reason || v.error;
+ if (typeof text === "string") return text;
+ }
+ } catch {
+ const m = /"(?:reason|error)"\s*:\s*"((?:[^"\\]|\\.)*)"/.exec(preview);
+ if (m) return m[1];
+ }
+ return "";
+}
+
+export function exitCodeFor(tool: ToolItem): number | null {
+ if ("exitCode" in tool) return typeof tool.exitCode === "number" ? tool.exitCode : null;
+ return exitCodeOf(tool.preview);
+}
+
+export function isRead(tool: ToolItem): boolean {
+ return tool.effect === "read";
+}
+
+// Changed nothing: the call errored, timed out, or the command exited non-zero.
+export function failed(tool: ToolItem): boolean {
+ if (tool.status !== "ok") return true;
+ if (tool.timedOut) return true;
+ const exit = exitCodeFor(tool);
+ return exit !== null && exit !== 0;
+}
+
+export type Summary = { changes: ToolItem[]; reads: ToolItem[]; count: number; failed: number };
+
+export function summarize(tools: ToolItem[]): Summary {
+ const changes = tools.filter((t) => !isRead(t));
+ const reads = tools.filter(isRead);
+ const failedCount = changes.filter(failed).length;
+ return { changes, reads, count: changes.length - failedCount, failed: failedCount };
+}
+
type DiffLine = { kind: "add" | "del" | "ctx" | "meta"; text: string };
function lines(text: unknown): string[] {
@@ -134,18 +184,51 @@ export function patchSummary(name: string, args: any): PatchChange[] {
return changes.filter((c) => c.path);
}
-function UnaskedItem({ tool, onOpenAudit }: { tool: ToolItem; onOpenAudit?: (callId: string) => void }) {
+// Who let it through: the tier by name, else the reviewer.
+function OriginChip({ tool }: { tool: ToolItem }) {
const { t } = useTranslation();
+ const tier = tool.approvalOrigin === "gate_tier";
+ return (
+ <span
+ className="text-label text-faint shrink-0"
+ data-testid="unasked-origin"
+ title={tier ? t("transcript.approval.gate_tier_title") : tool.approvalNote || t("transcript.approval.reviewer_allowed_title")}
+ >
+ {tier ? t("transcript.unasked.by_tier") : t("transcript.unasked.by_reviewer")}
+ </span>
+ );
+}
+
+function AuditLink({ tool, onOpenAudit }: { tool: ToolItem; onOpenAudit?: (callId: string) => void }) {
+ const { t } = useTranslation();
+ if (!onOpenAudit || !tool.callId) return null;
+ return (
+ <button
+ className="ml-auto shrink-0 text-label text-faint hover:text-muted cursor-pointer"
+ data-testid="unasked-audit"
+ onClick={() => onOpenAudit(tool.callId!)}
+ >
+ {t("transcript.unasked.audit")}
+ </button>
+ );
+}
+
+// A row: one tool call, or every edit of one file in this turn (stacked diffs).
+function UnaskedRow({ tools, onOpenAudit }: { tools: ToolItem[]; onOpenAudit?: (callId: string) => void }) {
+ const { t } = useTranslation();
+ const tool = tools[0];
const line = humanizeTool(tool.name, tool.args);
const path = typeof tool.args?.path === "string" ? tool.args.path : "";
const isCommand = COMMAND_TOOLS.has(tool.name);
- const exit = isCommand ? exitCodeOf(tool.preview) : null;
- const failed = tool.status !== "ok";
+ const exit = isCommand && !tool.background ? exitCodeFor(tool) : null;
+ const anyFailed = tools.some(failed);
+ const errorText = tool.status !== "ok" ? errorOf(tool.preview) : "";
const patched = patchSummary(tool.name, tool.args);
+ const dot = tool.background ? "text-faint" : anyFailed ? "text-danger" : "text-ok";
return (
<div data-testid="unasked-item" className="py-0.5">
<div className="flex items-baseline gap-2 px-2">
- <span className={"w-3.5 text-center text-label shrink-0 " + (failed || (exit !== null && exit !== 0) ? "text-danger" : "text-ok")}>●</span>
+ <span className={"w-3.5 text-center text-label shrink-0 " + dot}>●</span>
<span className="min-w-0 text-ui leading-relaxed" data-testid="unasked-item-line">
{isCommand ? (
<code className="font-mono text-meta text-ink break-all">{String(tool.args?.command ?? "")}</code>
@@ -165,8 +248,13 @@ function UnaskedItem({ tool, onOpenAudit }: { tool: ToolItem; onOpenAudit?: (cal
))
) : path ? (
<>
- <span className="text-muted">{line.pre}</span>
+ <span className="text-muted">{tools.length > 1 ? t("transcript.unasked.edited") + " " : line.pre}</span>
<span className="font-mono text-meta text-ink break-all">{path}</span>
+ {tools.length > 1 && (
+ <span className="text-faint" data-testid="unasked-edit-count">
+ {" · " + t("transcript.unasked.edits", { count: tools.length })}
+ </span>
+ )}
</>
) : (
<>
@@ -176,47 +264,86 @@ function UnaskedItem({ tool, onOpenAudit }: { tool: ToolItem; onOpenAudit?: (cal
</>
)}
</span>
- {exit !== null ? (
+ {tool.background ? (
+ <span className="text-label text-faint shrink-0" data-testid="unasked-background">
+ {t("transcript.unasked.background")}
+ </span>
+ ) : tool.timedOut ? (
+ <span className="text-label text-danger shrink-0" data-testid="unasked-exit">
+ {t("transcript.unasked.timed_out")}
+ </span>
+ ) : exit !== null ? (
<span className={"text-label shrink-0 " + (exit === 0 ? "text-ok" : "text-danger")} data-testid="unasked-exit">
{t("transcript.unasked.exit", { code: exit })}
</span>
) : (
- failed && <span className="text-label text-danger shrink-0">{tool.status}</span>
- )}
- <span
- className="text-label text-faint shrink-0"
- title={
- tool.approvalOrigin === "gate_tier"
- ? t("transcript.approval.gate_tier_title")
- : tool.approvalNote || t("transcript.approval.reviewer_allowed_title")
- }
- >
- {tool.approvalOrigin === "gate_tier" ? t("transcript.approval.gate_tier") : t("transcript.approval.auto_approved")}
- </span>
- {onOpenAudit && tool.callId && (
- <button
- className="ml-auto shrink-0 text-label text-faint hover:text-muted cursor-pointer"
- data-testid="unasked-audit"
- onClick={() => onOpenAudit(tool.callId!)}
- >
- {t("transcript.unasked.audit")}
- </button>
+ tool.status !== "ok" && (
+ <span className="text-label text-danger shrink-0 truncate max-w-[40%]" data-testid="unasked-failed" title={errorText}>
+ {t("transcript.unasked.failed")}
+ {errorText ? " · " + errorText : ""}
+ </span>
+ )
)}
+ <OriginChip tool={tool} />
+ {tools.length === 1 && <AuditLink tool={tool} onOpenAudit={onOpenAudit} />}
</div>
- <Diff tool={tool} />
+ {tools.map((edit, i) => (
+ <div key={edit.id || i}>
+ {tools.length > 1 && (
+ <div className="flex items-baseline gap-2 px-2 ml-5 text-label text-faint">
+ <span>{i + 1}.</span>
+ {failed(edit) && <span className="text-danger">{t("transcript.unasked.failed")}</span>}
+ <AuditLink tool={edit} onOpenAudit={onOpenAudit} />
+ </div>
+ )}
+ <Diff tool={edit} />
+ </div>
+ ))}
</div>
);
}
+// Rows in display order: files (edits of one file together), commands, other actions.
+export function rowsOf(changes: ToolItem[]): Array<{ group: "files" | "commands" | "other"; tools: ToolItem[] }> {
+ const rows: Array<{ group: "files" | "commands" | "other"; tools: ToolItem[] }> = [];
+ const byPath = new Map<string, ToolItem[]>();
+ for (const tool of changes.filter((x) => FILE_TOOLS.has(x.name))) {
+ const path = typeof tool.args?.path === "string" ? tool.args.path : "";
+ if (!path) {
+ rows.push({ group: "files", tools: [tool] });
+ continue;
+ }
+ const bucket = byPath.get(path);
+ if (bucket) bucket.push(tool);
+ else {
+ const fresh = [tool];
+ byPath.set(path, fresh);
+ rows.push({ group: "files", tools: fresh });
+ }
+ }
+ for (const tool of changes.filter((x) => COMMAND_TOOLS.has(x.name))) rows.push({ group: "commands", tools: [tool] });
+ for (const tool of changes.filter((x) => !FILE_TOOLS.has(x.name) && !COMMAND_TOOLS.has(x.name)))
+ rows.push({ group: "other", tools: [tool] });
+ return rows;
+}
+
export function UnaskedSummary({ tools, onOpenAudit }: { tools: ToolItem[]; onOpenAudit?: (callId: string) => void }) {
const { t } = useTranslation();
const [open, setOpen] = useState(false);
- if (!tools.length) return null;
- const groups: Array<{ key: "files" | "commands" | "other"; items: ToolItem[] }> = [
- { key: "files" as const, items: tools.filter((x) => FILE_TOOLS.has(x.name)) },
- { key: "commands" as const, items: tools.filter((x) => COMMAND_TOOLS.has(x.name)) },
- { key: "other" as const, items: tools.filter((x) => !FILE_TOOLS.has(x.name) && !COMMAND_TOOLS.has(x.name)) },
- ].filter((g) => g.items.length);
+ const [showAll, setShowAll] = useState(false);
+ const [showReads, setShowReads] = useState(false);
+ const summary = summarize(tools);
+ // Only reads, and nothing attempted: nothing changed, so nothing to say.
+ if (!summary.changes.length) return null;
+ const rows = rowsOf(summary.changes);
+ const shown = showAll ? rows : rows.slice(0, MAX_ROWS);
+ const groups = (["files", "commands", "other"] as const)
+ .map((key) => ({ key, rows: shown.filter((r) => r.group === key) }))
+ .filter((g) => g.rows.length);
+ const head =
+ summary.count > 0
+ ? t("transcript.unasked.summary", { count: summary.count })
+ : t("transcript.unasked.summary_none");
return (
<details className="stepgroup unasked-summary" open={open} data-testid="unasked-summary">
<summary
@@ -228,18 +355,62 @@ export function UnaskedSummary({ tools, onOpenAudit }: { tools: ToolItem[]; onOp
}}
>
<Icon name="chevronDown" size={12} />
- <span>{t("transcript.unasked.summary", { count: tools.length })}</span>
+ <span>{head}</span>
+ {summary.failed > 0 && (
+ <span className="text-faint" data-testid="unasked-failed-count">
+ {" · " + t("transcript.unasked.failed_count", { count: summary.failed })}
+ </span>
+ )}
+ {summary.reads.length > 0 && (
+ <span className="text-faint" data-testid="unasked-read-count">
+ {" · " + t("transcript.unasked.read_count", { count: summary.reads.length })}
+ </span>
+ )}
</summary>
{open && (
<div className="ml-1.5 mt-1 pl-2 border-l-2 border-line flex flex-col gap-1.5" data-testid="unasked-summary-body">
{groups.map((g) => (
<div key={g.key} data-testid={`unasked-group-${g.key}`}>
<div className="px-2 text-label text-faint uppercase tracking-wide">{t(`transcript.unasked.group_${g.key}`)}</div>
- {g.items.map((tool, i) => (
- <UnaskedItem tool={tool} onOpenAudit={onOpenAudit} key={tool.id || i} />
+ {g.rows.map((row, i) => (
+ <UnaskedRow tools={row.tools} onOpenAudit={onOpenAudit} key={row.tools[0].id || i} />
))}
</div>
))}
+ {rows.length > shown.length && (
+ <button
+ className="self-start px-2 text-label text-faint hover:text-muted cursor-pointer"
+ data-testid="unasked-show-all"
+ onClick={() => setShowAll(true)}
+ >
+ {t("transcript.unasked.show_all", { count: rows.length })}
+ </button>
+ )}
+ {summary.reads.length > 0 && (
+ <div data-testid="unasked-reads">
+ <button
+ className="px-2 text-label text-faint hover:text-muted cursor-pointer"
+ data-testid="unasked-reads-toggle"
+ onClick={() => setShowReads((v) => !v)}
+ >
+ {t("transcript.unasked.read_tail", { count: summary.reads.length })}
+ </button>
+ {showReads &&
+ summary.reads.map((tool, i) => (
+ <div className="flex items-baseline gap-2 px-2 ml-3 text-meta" key={tool.id || i} data-testid="unasked-read-item">
+ {COMMAND_TOOLS.has(tool.name) ? (
+ <code className="font-mono text-meta text-muted break-all">{String(tool.args?.command ?? "")}</code>
+ ) : (
+ <span className="text-muted">
+ {humanizeTool(tool.name, tool.args).pre}
+ {humanizeTool(tool.name, tool.args).obj}
+ </span>
+ )}
+ <AuditLink tool={tool} onOpenAudit={onOpenAudit} />
+ </div>
+ ))}
+ </div>
+ )}
</div>
)}
</details>
diff --git a/surfaces/gui/src/itemsFromMessages.test.ts b/surfaces/gui/src/itemsFromMessages.test.ts
index 1e7d1172..f8ff6187 100644
--- a/surfaces/gui/src/itemsFromMessages.test.ts
+++ b/surfaces/gui/src/itemsFromMessages.test.ts
@@ -185,3 +185,33 @@ describe("itemsFromMessages call ids", () => {
expect(tool.approvalOrigin).toBe("reviewer");
});
});
+
+
+// OPE-238: a call's outcome is persisted with its result, so a reload shows what the live
+// view showed: a failed write is not a green "Wrote x", a command keeps its exit status.
+describe("itemsFromMessages outcomes", () => {
+ const replay = (display: any, content = '{"ok": true}') =>
+ itemsFromMessages([
+ { role: "user", content: "go" },
+ { role: "assistant", content: "", tool_calls: [{ id: "call_1", function: { name: "run_shell", arguments: '{"command": "x"}' } }] },
+ { role: "tool", tool_call_id: "call_1", content, _display: display },
+ ] as any).find((i: any) => i.kind === "tool") as any;
+
+ it("a failed call replays with its status", () => {
+ const tool = replay({ approval_origin: "reviewer", status: "error" }, '{"error": "boom"}');
+ expect(tool.status).toBe("error");
+ expect(tool.approvalOrigin).toBe("reviewer");
+ });
+
+ it("a shell command replays with its exit code, time-out or background start", () => {
+ expect(replay({ exit_code: 1 }).exitCode).toBe(1);
+ expect(replay({ exit_code: null, timed_out: true })).toMatchObject({ exitCode: null, timedOut: true });
+ expect(replay({ background: true }).background).toBe(true);
+ expect("exitCode" in replay({ approval_origin: "reviewer" })).toBe(false);
+ });
+
+ it("effect and tier ride along with the origin", () => {
+ const tool = replay({ approval_origin: "gate_tier", approval_effect: "read", approval_tier: "readonly_command" });
+ expect(tool).toMatchObject({ approvalOrigin: "gate_tier", effect: "read", tier: "readonly_command" });
+ });
+});
diff --git a/surfaces/gui/src/itemsFromMessages.ts b/surfaces/gui/src/itemsFromMessages.ts
index 6d095011..9b217e04 100644
--- a/surfaces/gui/src/itemsFromMessages.ts
+++ b/surfaces/gui/src/itemsFromMessages.ts
@@ -19,7 +19,10 @@ export function itemsFromMessages(messages: ConversationMessage[]): Item[] {
// Approval provenance (owner ruling 2026-08-24): who cleared (or refused) the call —
// "reviewer"/"bypass"/"user"/"reviewer_denied" — persisted in the same sidecar so the
// quiet chips survive reload.
- const approvalMeta: Record<string, { origin: string; note?: string; grant?: string }> = {};
+ const approvalMeta: Record<string, { origin: string; note?: string; grant?: string; effect?: string; tier?: string }> = {};
+ // OPE-238: the call's outcome, persisted with the result so a reload shows what the live
+ // view showed: a failed status, and a shell command's exit code / time-out / background start.
+ const outcomes: Record<string, { status?: string; exitCode?: number | null; timedOut?: boolean; background?: boolean }> = {};
for (const m of messages || []) {
if (m.role === "tool" && m.tool_call_id) {
results[m.tool_call_id] =
@@ -31,6 +34,16 @@ export function itemsFromMessages(messages: ConversationMessage[]): Item[] {
origin: String(m._display.approval_origin),
...(m._display.approval_note ? { note: String(m._display.approval_note) } : {}),
...(m._display.approval_grant ? { grant: String(m._display.approval_grant) } : {}),
+ ...(m._display.approval_effect ? { effect: String(m._display.approval_effect) } : {}),
+ ...(m._display.approval_tier ? { tier: String(m._display.approval_tier) } : {}),
+ };
+ const d = m._display && typeof m._display === "object" ? (m._display as any) : null;
+ if (d && (d.status || "exit_code" in d || d.timed_out || d.background))
+ outcomes[m.tool_call_id] = {
+ ...(d.status ? { status: String(d.status) } : {}),
+ ...("exit_code" in d ? { exitCode: typeof d.exit_code === "number" ? d.exit_code : null } : {}),
+ ...(d.timed_out ? { timedOut: true } : {}),
+ ...(d.background ? { background: true } : {}),
};
}
}
@@ -67,7 +80,9 @@ export function itemsFromMessages(messages: ConversationMessage[]): Item[] {
const preview = results[tc.id];
const hidden = hiddenCounts[tc.id];
const meta = approvalMeta[tc.id];
- // A denial (by the reviewer or the user) must not replay as a green step.
+ const outcome = outcomes[tc.id] || {};
+ // A denial (by the reviewer or the user) must not replay as a green step — and a
+ // call that failed must not either (it changed nothing).
const denied = meta && (meta.origin === "reviewer_denied" || meta.grant === "deny");
items.push({
kind: "tool",
@@ -75,10 +90,15 @@ export function itemsFromMessages(messages: ConversationMessage[]): Item[] {
...(tc.id ? { callId: tc.id } : {}),
name: tc.function?.name,
args,
- status: denied ? "denied" : "ok",
+ status: denied ? "denied" : outcome.status || "ok",
preview,
+ ...("exitCode" in outcome ? { exitCode: outcome.exitCode } : {}),
+ ...(outcome.timedOut ? { timedOut: true } : {}),
+ ...(outcome.background ? { background: true } : {}),
...(hidden ? { hidden } : {}),
...(meta ? { approvalOrigin: meta.origin } : {}),
+ ...(meta?.effect ? { effect: meta.effect } : {}),
+ ...(meta?.tier ? { tier: meta.tier } : {}),
...(meta?.note
? meta.origin === "reviewer_denied"
? { reviewerReason: meta.note }
diff --git a/surfaces/gui/src/locales/en.json b/surfaces/gui/src/locales/en.json
index 139b54d4..af21022b 100644
--- a/surfaces/gui/src/locales/en.json
+++ b/surfaces/gui/src/locales/en.json
@@ -290,8 +290,21 @@
"undo": "Undo"
},
"unasked": {
- "summary": "{{count}} changes made without asking",
- "summary_one": "{{count}} change made without asking",
+ "summary": "{{count}} changes auto-approved",
+ "summary_one": "{{count}} change auto-approved",
+ "summary_none": "No changes auto-approved",
+ "failed_count": "{{count}} failed",
+ "read_count": "{{count}} read-only",
+ "read_tail": "{{count}} read-only actions ran auto-approved · not counted as changes",
+ "read_tail_one": "{{count}} read-only action ran auto-approved · not counted as changes",
+ "show_all": "Show all {{count}}",
+ "edits": "{{count}} edits",
+ "edits_one": "{{count}} edit",
+ "failed": "failed",
+ "timed_out": "timed out",
+ "background": "started in the background",
+ "by_tier": "safe change",
+ "by_reviewer": "reviewer",
"group_files": "Files",
"group_commands": "Commands",
"group_other": "Other actions",
diff --git a/surfaces/gui/src/locales/zh.json b/surfaces/gui/src/locales/zh.json
index 07487cdd..94ea78a3 100644
--- a/surfaces/gui/src/locales/zh.json
+++ b/surfaces/gui/src/locales/zh.json
@@ -287,7 +287,18 @@
"undo": "撤销"
},
"unasked": {
- "summary": "{{count}} 项未经询问的更改",
+ "summary": "{{count}} 项更改已自动批准",
+ "summary_none": "没有自动批准的更改",
+ "failed_count": "{{count}} 项失败",
+ "read_count": "{{count}} 项只读",
+ "read_tail": "{{count}} 项只读操作已自动批准 · 不计入更改",
+ "show_all": "显示全部 {{count}} 项",
+ "edits": "{{count}} 次编辑",
+ "failed": "失败",
+ "timed_out": "超时",
+ "background": "已在后台启动",
+ "by_tier": "安全更改",
+ "by_reviewer": "审核器",
"group_files": "文件",
"group_commands": "命令",
"group_other": "其他操作",
diff --git a/surfaces/gui/src/types.ts b/surfaces/gui/src/types.ts
index f3d53ace..093aae4a 100644
--- a/surfaces/gui/src/types.ts
+++ b/surfaces/gui/src/types.ts
@@ -165,8 +165,12 @@ export type Item =
// `approvalOrigin` = why the call ran without a card: "reviewer" (auto-approved by the
// Auto-Approve reviewer; `approvalNote` carries its one-line reason) or "bypass"
// (bypass-approvals mode). Rendered as a quiet debugging chip, deliberately subtle.
- // `callId` = the tool call's id (OPE-238: links the item to its audit rows).
- | { kind: "tool"; id: string; name: string; args: any; status: string; preview?: string; hidden?: number; standingRule?: string; reviewerReason?: string; allowAnyway?: boolean; approvalOrigin?: string; approvalNote?: string; approvalGrant?: string; callId?: string }
+ // `callId` = the tool call's id (OPE-238: links the item to its audit rows, and pairs the
+ // proposed event with its own finished event). `effect` ("change" | "read") and `tier` come
+ // with a cardless origin and decide whether the auto-approved summary counts the call.
+ // `exitCode` / `timedOut` / `background` are a shell command's outcome, read from the
+ // result itself rather than the truncated preview.
+ | { kind: "tool"; id: string; name: string; args: any; status: string; preview?: string; hidden?: number; standingRule?: string; reviewerReason?: string; allowAnyway?: boolean; approvalOrigin?: string; approvalNote?: string; approvalGrant?: string; callId?: string; effect?: string; tier?: string; exitCode?: number | null; timedOut?: boolean; background?: boolean }
| {
kind: "approval";
toolCallId?: string;
diff --git a/surfaces/gui/src/updateLastTool.test.ts b/surfaces/gui/src/updateLastTool.test.ts
new file mode 100644
index 00000000..ba815086
--- /dev/null
+++ b/surfaces/gui/src/updateLastTool.test.ts
@@ -0,0 +1,34 @@
+// OPE-238: a finished event pairs with its own proposed item. Two same-named calls in one
+// batch finish in call order; matching the LAST pending item by name handed each one the
+// other's result, exit code, origin and audit link (seen in review, 28 Oct 2026).
+import { describe, expect, it } from "vitest";
+import { updateLastTool } from "./App";
+import type { Item } from "./types";
+
+const pending = (): Item[] => [
+ { kind: "tool", id: "a", callId: "c_pytest", name: "run_shell", args: { command: "pytest -q" }, status: "…" },
+ { kind: "tool", id: "b", callId: "c_rm", name: "run_shell", args: { command: "rm -rf build" }, status: "…" },
+];
+
+describe("updateLastTool", () => {
+ it("matches by call id, so same-named calls keep their own results", () => {
+ let items = updateLastTool(pending(), "run_shell", "ok", "pytest result", { callId: "c_pytest", approvalOrigin: "user", exitCode: 0 });
+ items = updateLastTool(items, "run_shell", "ok", "rm result", { callId: "c_rm", approvalOrigin: "reviewer", exitCode: 1 });
+ const [a, b] = items as any[];
+ expect(a).toMatchObject({ args: { command: "pytest -q" }, preview: "pytest result", approvalOrigin: "user", exitCode: 0 });
+ expect(b).toMatchObject({ args: { command: "rm -rf build" }, preview: "rm result", approvalOrigin: "reviewer", exitCode: 1 });
+ });
+
+ it("falls back to the last pending item with that name when there is no id", () => {
+ const items = updateLastTool(pending(), "run_shell", "ok", "x", {});
+ expect((items[1] as any).status).toBe("ok");
+ expect((items[0] as any).status).toBe("…");
+ });
+
+ it("keeps a null exit code as unknown and drops absent flags", () => {
+ const [a] = updateLastTool(pending(), "run_shell", "ok", "x", { callId: "c_pytest", exitCode: null, timedOut: true }) as any[];
+ expect(a.exitCode).toBeNull();
+ expect(a.timedOut).toBe(true);
+ expect("background" in a).toBe(false);
+ });
+});
diff --git a/tests/test_turn_summary.py b/tests/test_turn_summary.py
index c121404e..fbb249e0 100644
--- a/tests/test_turn_summary.py
+++ b/tests/test_turn_summary.py
@@ -52,7 +52,10 @@ def test_lists_gate_tier_and_reviewer_allowed_actions_before_turn_end(tmp_path):
assert all(i["status"] == "ok" for i in items)
assert all(i["tool_call_id"] for i in items)
assert items[1]["note"] == "scripted allow" # the reviewer's one-line reason rides along
+ assert items[0]["tier"] == "inert_write" and "tier" not in items[1]
+ assert all(i["effect"] == "change" for i in items)
assert summaries[0].data["count"] == 2
+ assert summaries[0].data["failed"] == 0 and summaries[0].data["reads"] == 0
def test_no_event_when_everything_was_asked(tmp_path):
@@ -178,3 +181,116 @@ def test_audit_store_filters_by_call_id(tmp_path):
assert sorted(r["stage"] for r in rows) == ["finished", "started"]
assert {r["call_id"] for r in rows} == {"a1"}
assert len(store.list()) == 3
+
+
+# -- the count means "changed state, and nobody approved it" ---------------------------
+
+
+def _shell_returning(engine, results: dict[str, dict]):
+ """Replace the test shell with one that answers each command from `results`."""
+
+ def run_shell(command: str) -> dict:
+ """Run a command.
+
+ Args:
+ command: the command line
+ """
+ return results[command]
+
+ engine.registry.register(run_shell, metadata=engine.registry.get("run_shell").metadata)
+
+
+def test_the_proposed_event_carries_the_call_id(tmp_path):
+ # Two same-named calls in one batch must pair with their own finished events.
+ engine, _rows, _approvals = _engine(
+ tmp_path,
+ [
+ _tool_turn(("run_shell", {"command": "pytest -q"}), ("run_shell", {"command": "ruff check"})),
+ AssistantTurn(text="done", finish_reason="stop"),
+ ],
+ )
+ engine.reviewer = _FakeReviewer({"run_shell": "allow"})
+ events = _run(engine, "check")
+ proposed = [ev.data["tool_call_id"] for ev in events if ev.type == EventType.TOOL_PROPOSED]
+ finished = [ev.data["tool_call_id"] for ev in events if ev.type == EventType.TOOL_FINISHED]
+ assert len(proposed) == 2 and all(proposed)
+ assert proposed == finished
+
+
+def test_a_shell_commands_outcome_rides_on_the_finished_event_not_the_preview(tmp_path):
+ long_command = "python -c '" + "x = 1; " * 80 + "'" # pushes exit_code past the 300-char preview
+ engine, _rows, _approvals = _engine(
+ tmp_path,
+ [
+ _tool_turn(("run_shell", {"command": long_command})),
+ _tool_turn(("run_shell", {"command": "sleep 999"})),
+ _tool_turn(("run_shell", {"command": "npm run dev"})),
+ AssistantTurn(text="done", finish_reason="stop"),
+ ],
+ )
+ engine.reviewer = _FakeReviewer({"run_shell": "allow"})
+ _shell_returning(
+ engine,
+ {
+ long_command: {"command": long_command, "cwd": "/w", "exit_code": 1, "output": "", "timed_out": False},
+ "sleep 999": {"command": "sleep 999", "cwd": "/w", "exit_code": None, "output": "", "timed_out": True},
+ "npm run dev": {"task_id": "bg-1", "command": "npm run dev", "status": "running"},
+ },
+ )
+ events = _run(engine, "run things")
+ finished = [ev.data for ev in events if ev.type == EventType.TOOL_FINISHED]
+ assert '"exit_code"' not in finished[0]["result_preview"] # the preview alone would say nothing
+ assert finished[0]["exit_code"] == 1 and "timed_out" not in finished[0]
+ assert finished[1]["exit_code"] is None and finished[1]["timed_out"] is True
+ assert finished[2]["background"] is True and "exit_code" not in finished[2]
+ # The same facts are persisted with the result, so a reload shows the same thing.
+ sidecars = [m["_display"] for m in engine.messages if m.get("role") == "tool"]
+ assert sidecars[0]["exit_code"] == 1
+ assert sidecars[1]["timed_out"] is True
+ assert sidecars[2]["background"] is True
+ # The summary: three changes attempted, two failed, one started in the background.
+ summary = _summaries(events)[0].data
+ assert (summary["count"], summary["failed"], summary["reads"]) == (1, 2, 0)
+ assert summary["actions"][0]["exit_code"] == 1
+ assert summary["actions"][1]["timed_out"] is True
+ assert summary["actions"][2]["background"] is True
+
+
+def test_a_failed_call_keeps_its_status_on_the_persisted_message(tmp_path):
+ engine, _rows, _approvals = _engine(
+ tmp_path,
+ [_tool_turn(("run_shell", {"command": "pytest -q"})), AssistantTurn(text="done", finish_reason="stop")],
+ )
+ engine.reviewer = _FakeReviewer({"run_shell": "allow"})
+
+ def run_shell(command: str) -> str:
+ """Run a command.
+
+ Args:
+ command: the command line
+ """
+ raise RuntimeError("exit 1")
+
+ engine.registry.register(run_shell, metadata=engine.registry.get("run_shell").metadata)
+ events = _run(engine, "run the tests")
+ tool_msg = next(m for m in engine.messages if m.get("role") == "tool")
+ assert tool_msg["_display"]["status"] == "error"
+ assert tool_msg["_display"]["approval_origin"] == "reviewer"
+ summary = _summaries(events)[0].data
+ assert (summary["count"], summary["failed"]) == (0, 1)
+
+
+def test_effect_follows_the_risk_class_and_the_read_only_tier(tmp_path):
+ from coworker.providers import ToolCall
+
+ engine, _rows, _approvals = _engine(tmp_path, [])
+ meta = engine.registry.get("run_shell").metadata
+ shell = ToolCall(id="c1", name="run_shell", arguments={"command": "cat x"})
+ assert engine._effect(shell, meta, "") == "change" # a command is a change unless classified
+ assert engine._effect(shell, meta, "readonly_command") == "read" # OPE-234's tier
+ write = ToolCall(id="c2", name="write_file", arguments={"path": "a.txt", "content": ""})
+ assert engine._effect(write, engine.registry.get("write_file").metadata, "inert_write") == "change"
+ fetch = ToolCall(id="c3", name="web_fetch", arguments={"url": "https://example.com"})
+ assert engine._effect(fetch, None, "") == "read" # a web read is a read
+ mcp = ToolCall(id="c4", name="mcp__jira__create_issue", arguments={})
+ assert engine._effect(mcp, None, "") == "change" # third-party tools that gate are writes or sends