An open-source, faster Devin alternative
TypeScript
React: the backend-recorded host turn is a fixture, and the runner pins the result and settlement invariants
Igor Zalutski55adf0ebd387
diff --git a/react/src/fixtures.test.ts b/react/src/fixtures.test.tsindex 96549184..4a69fc5d 100644--- a/react/src/fixtures.test.ts+++ b/react/src/fixtures.test.ts@@ -1,15 +1,25 @@// Every fixture log under react/test/fixtures reduces through the same// reducer the hook uses. `documented/` holds logs authored to-// docs/agents/events.mdx; `backend-emitted/` holds recordings of what the-// platform actually returned, added once the platform emits the documented-// shape on every runtime. The checks are the contract's invariants, so a-// recording that disagrees with the docs fails here rather than in a page.+// docs/agents/events.mdx; `backend-emitted/` holds logs the platform's own+// tests recorded from a real host turn, in the shape the public API returns.+// The checks are the contract's invariants, so a recording that disagrees+// with the docs fails here rather than in a page: every turn settled, every+// tool row settled with its turn, no duplicate rows, and the result tool's+// committed output a decoded value that is the turn's result.import assert from "node:assert/strict";import { readdirSync, readFileSync } from "node:fs";import test from "node:test";-import { applyEvents, emptyTimeline, turnsOf, type AgentEvent, type Turn } from "./events.js";+import {+ applyEvents,+ emptyTimeline,+ isSettledStatus,+ turnsOf,+ type AgentEvent,+ type ToolCallStatus,+ type Turn,+} from "./events.js";// Tests run from dist/, one level below the package root, like src/.const root = new URL("../test/fixtures/", import.meta.url);@@ -34,6 +44,13 @@ function fixturesIn(directory: string): Array<{ name: string; fixture: Fixture }}));}+/** The `tool.completed` events of a turn that committed the session's result, in log order. */+function committedResults(events: AgentEvent[], turnId: string): AgentEvent[] {+ return events.filter(+ (event) => event.turnId === turnId && event.type === "tool.completed" && event.data.result === true,+ );+}+function check(name: string, fixture: Fixture): void {assert.ok(Array.isArray(fixture.events) && fixture.events.length > 0, `${name}: no events`);const whole = turnsOf(applyEvents(emptyTimeline(), fixture.events));@@ -42,25 +59,37 @@ function check(name: string, fixture: Fixture): void {paged = applyEvents(paged, fixture.events.slice(start, start + 3));}assert.deepEqual(turnsOf(paged), whole, `${name}: replay in pages differs from replay whole`);+ assert.ok(whole.length > 0, `${name}: the log names no turn`);for (const turn of whole) {- const settled = turn.status === "completed" || turn.status === "failed" || turn.status === "cancelled";- if (settled) {- for (const call of turn.toolCalls) {- assert.notEqual(- call.status,- "running",- `${name}: turn ${turn.id} is ${turn.status} but its call ${call.callId} is still running`,- );- }+ // A fixture is a finished log: a turn left running would pin a moving target.+ assert.ok(isSettledStatus(turn.status), `${name}: turn ${turn.id} ends ${turn.status}, not a terminal status`);+ // A call is settled by its own completion or failure, or by the turn's+ // outcome; only an interrupt leaves a call `cancelled`.+ const settled: ToolCallStatus[] =+ turn.status === "cancelled" ? ["completed", "failed", "cancelled"] : ["completed", "failed"];+ for (const call of turn.toolCalls) {+ assert.ok(+ settled.includes(call.status),+ `${name}: turn ${turn.id} is ${turn.status} but its call ${call.callId} ends ${call.status}`,+ );}const ids = turn.toolCalls.map((call) => call.callId);assert.equal(new Set(ids).size, ids.length, `${name}: turn ${turn.id} has duplicate tool rows`);- if (turn.result !== undefined) {- const pinned = fixture.turns?.find((candidate) => candidate.id === turn.id)?.result;- if (typeof pinned !== "string") {- assert.notEqual(typeof turn.result, "string", `${name}: turn ${turn.id} exposes its result as JSON text`);- }+ // The result is the result tool's committed output and nothing else: the+ // same decoded value on the tool row, on the turn, and in the event.+ const committed = committedResults(fixture.events, turn.id);+ const last = committed[committed.length - 1];+ if (!last) {+ assert.equal(turn.result, undefined, `${name}: turn ${turn.id} has a result but committed none`);+ continue;}+ const call = turn.toolCalls.find((candidate) => candidate.callId === last.data.callId);+ assert.ok(call, `${name}: turn ${turn.id} has no tool row for the committed result call`);+ assert.equal(call.status, "completed", `${name}: the result call ${call.callId} ends ${call.status}`);+ assert.equal(typeof call.output, "object", `${name}: the result call's output is not a decoded object`);+ assert.notEqual(call.output, null, `${name}: the result call's output is null`);+ assert.deepEqual(call.output, last.data.output, `${name}: the result call's output differs from the event`);+ assert.deepEqual(turn.result, call.output, `${name}: turn ${turn.id}'s result differs from its committed output`);}if (fixture.turns) assert.deepEqual(whole, fixture.turns, `${name}: turns differ from the pinned turns`);}@@ -77,3 +106,8 @@ test("the documented fixtures cover a result, an interrupt and a failure", () =>const names = fixturesIn("documented").map(({ name }) => name);assert.ok(names.length >= 3, `expected at least three documented fixtures, found ${String(names.length)}`);});++test("a recorded host turn with a result call is among the backend-emitted fixtures", () => {+ const names = fixturesIn("backend-emitted").map(({ name }) => name);+ assert.ok(names.includes("backend-emitted/workerd-turn-with-result.json"), `found ${names.join(", ") || "none"}`);+});diff --git a/react/test/fixtures/backend-emitted/README.md b/react/test/fixtures/backend-emitted/README.mdindex 2f7a498a..e65eab41 100644--- a/react/test/fixtures/backend-emitted/README.md+++ b/react/test/fixtures/backend-emitted/README.md@@ -1,11 +1,12 @@# Backend-emitted event logs-Recorded session logs from the platform go here, one JSON file per recorded-session or turn, as `GET /sessions/<id>/events` returned them through the-public API. Every `*.json` file in this directory is run by-`react/src/fixtures.test.ts` on `npm test`: replayed whole and in pages, and-checked for settled tool rows on settled turns, unique tool rows and a decoded-result.+Session logs the platform's own tests recorded from a real host turn go here,+one JSON file per recorded session or turn, in the shape+`GET /sessions/<id>/events` returns. Every `*.json` file in this directory is+run by `react/src/fixtures.test.ts` on `npm test`: replayed whole and in+pages, and checked for a terminal status on every turn, tool rows settled+with their turn, unique tool rows, and the result tool's committed output as+a decoded value equal to the turn's result.The file shape is the one the authored fixtures in `../documented/` use:@@ -15,8 +16,13 @@ The file shape is the one the authored fixtures in `../documented/` use:`turns` is optional; when present it is compared for deep equality with whatthe reducer produces. Without it the invariants above are still checked.+Other top-level fields (`description`, `recordedBy`, `runtime`) are the+recording's own notes and are not read.-This directory holds only this README until the platform emits the documented-event shape on every runtime (bluecode F2); recordings made before then would-pin the wrong contract. Add recordings here once that ships, do not author-them by hand.+Recordings are copied from the platform's test fixtures, never authored or+edited here; the recording test names the volatile values it normalizes+(ids, timestamps, durations).++| File | Recorded by | What it holds |+| --- | --- | --- |+| `workerd-turn-with-result.json` | the platform's Workerd session test, "public tool events" | One turn: a failed ordinary tool call, then the result tool started, reporting progress and completed by its commit with `result: true` and the decoded value |diff --git a/react/test/fixtures/backend-emitted/workerd-turn-with-result.json b/react/test/fixtures/backend-emitted/workerd-turn-with-result.jsonnew file mode 100644index 00000000..70d2aadd--- /dev/null+++ b/react/test/fixtures/backend-emitted/workerd-turn-with-result.json@@ -0,0 +1,227 @@+{+ "description": "One full turn of a managed session on the Workerd runtime, as the public event log records it: a failed ordinary tool call (call-0), then the agent's result tool (call-1) started, reporting progress, and completed by its commit with the decoded value and result: true. Volatile values are normalized: <uuid>, <timestamp>, <turn>, <render-id>, <n> ms, and every *Ms measurement is 0.",+ "recordedBy": "bluecode test/managed-session-workerd.test.ts, 'public tool events' (the fake Workerd runtime speaking OpenCode's native event shapes through the real Session object)",+ "runtime": "workerd",+ "events": [+ {+ "seq": 1,+ "type": "session.created",+ "data": {+ "agentId": "workerd-public-events",+ "deploymentId": "workerd-public-events:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"+ }+ },+ {+ "seq": 2,+ "type": "runtime.log",+ "data": {+ "phase": "workerd_prewarm_miss",+ "runtime": "workerd",+ "prewarmHit": false,+ "standbyAgeMs": 0,+ "checkoutMs": 0,+ "message": "No prewarmed agent runtime was ready · created on demand"+ }+ },+ {+ "seq": 3,+ "type": "runtime.log",+ "data": {+ "phase": "workerd_session_ready",+ "runtime": "workerd",+ "activation": 1,+ "initializationMs": 0,+ "message": "Agent runtime ready in its Durable Object · activation 1 · OpenCode initialized in <n> ms"+ }+ },+ {+ "seq": 4,+ "type": "runtime.connected",+ "data": {+ "runtimeId": "<runtime-id>",+ "epoch": 0,+ "runtime": "workerd"+ }+ },+ {+ "seq": 5,+ "type": "session.status_changed",+ "data": {+ "from": "new",+ "to": "idle"+ }+ },+ {+ "seq": 6,+ "type": "message.received",+ "turnId": "<turn>",+ "data": {+ "input": "Report the branch [stall 600ms][tool-fails call-0][result-call call-1]",+ "mode": "queue"+ }+ },+ {+ "seq": 7,+ "type": "turn.queued",+ "turnId": "<turn>",+ "data": {+ "mode": "queue"+ }+ },+ {+ "seq": 8,+ "type": "turn.started",+ "turnId": "<turn>",+ "data": {}+ },+ {+ "seq": 9,+ "type": "session.status_changed",+ "data": {+ "from": "idle",+ "to": "running"+ }+ },+ {+ "seq": 10,+ "type": "runtime.log",+ "turnId": "<turn>",+ "data": {+ "phase": "workerd_reused",+ "runtime": "workerd",+ "activation": 1,+ "requestReadyMs": 0,+ "activationAgeMs": 0,+ "message": "Agent runtime reused · activation 1 · turn ready in <n> ms"+ }+ },+ {+ "seq": 11,+ "type": "agent.rendered",+ "turnId": "<turn>",+ "data": {+ "renderId": "<render-id>",+ "responseId": "response-managed-test",+ "providerTurn": 1,+ "stateVersion": 0,+ "instructionsHash": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",+ "instructions": "These are the deployed agent instructions.",+ "enabledTools": [],+ "enabledSubagents": [],+ "requiredConnections": [],+ "enabledMcpServers": [],+ "input": {+ "source": "user",+ "text": "Report the branch [stall 600ms][tool-fails call-0][result-call call-1]"+ },+ "tools": [],+ "model": {+ "provider": "openrouter",+ "model": "anthropic/claude-sonnet-4.6"+ },+ "renderedAt": "<timestamp>"+ }+ },+ {+ "seq": 12,+ "type": "message.delta",+ "turnId": "<turn>",+ "data": {+ "text": "Managed OpenCode is "+ }+ },+ {+ "seq": 13,+ "type": "tool.started",+ "turnId": "<turn>",+ "data": {+ "callId": "call-0",+ "tool": "lookup",+ "title": "lookup",+ "input": {+ "q": "x"+ }+ }+ },+ {+ "seq": 14,+ "type": "tool.failed",+ "turnId": "<turn>",+ "data": {+ "callId": "call-0",+ "tool": "lookup",+ "title": "lookup",+ "message": "lookup exploded"+ }+ },+ {+ "seq": 15,+ "type": "tool.started",+ "turnId": "<turn>",+ "data": {+ "callId": "call-1",+ "tool": "report",+ "title": "report",+ "input": {+ "branch": "task/1"+ }+ }+ },+ {+ "seq": 16,+ "type": "tool.progress",+ "turnId": "<turn>",+ "data": {+ "stage": "verifying",+ "callId": "call-1",+ "tool": "report"+ }+ },+ {+ "seq": 17,+ "type": "tool.completed",+ "turnId": "<turn>",+ "data": {+ "tool": "report",+ "callId": "call-1",+ "title": "report",+ "output": {+ "branch": "task/1"+ },+ "result": true+ }+ },+ {+ "seq": 18,+ "type": "message.delta",+ "turnId": "<turn>",+ "data": {+ "text": "running in the Durable Object."+ }+ },+ {+ "seq": 19,+ "type": "message.completed",+ "turnId": "<turn>",+ "data": {+ "text": "Managed OpenCode is running in the Durable Object.",+ "finalText": "Managed OpenCode is running in the Durable Object.",+ "source": "opencode-workerd"+ }+ },+ {+ "seq": 20,+ "type": "turn.completed",+ "turnId": "<turn>",+ "data": {}+ },+ {+ "seq": 21,+ "type": "session.status_changed",+ "data": {+ "from": "running",+ "to": "idle"+ }+ }+ ]+}