返回 CodeWhale
README.md
根目录 / crates / tui / tests / fixtures / conformance / README.md
1 # Conformance fixtures
2
3 The gate for the Rust → TypeScript edge migration (`CURRENT_DECISIONS.md` §26;
4 TS-EXTENSION-HOST-DESIGN §9.3). A subsystem that moves to the extension host —
5 MCP client, hook orchestration, script tools, tool adapters, slash commands —
6 flips its flag only after it reproduces these goldens on these fixtures. The
7 goldens are what the Rust implementation does today; they are not a spec
8 written in the abstract.
9
10 Everything here is language-neutral: JSON, JSONL, raw SSE bytes, and POSIX
11 shell. No fixture was captured from a real provider or contains a secret.
12
13 The Rust runner is `crates/tui/src/conformance/` (one golden test per family,
14 plus hardening controls):
15
16 ```sh
17 cargo test -p codewhale-tui --lib -- conformance::
18 # Re-record after an intended change, then review the diff like source:
19 CODEWHALE_CONFORMANCE_UPDATE=1 cargo test -p codewhale-tui --lib -- conformance::
20 ```
21
22 Default is compare-only. Update mode rewrites drifted goldens and refuses to
23 run when `CI` is set. A golden change is a behavior change and is reviewed as
24 one. Harness deadlines, incomplete turns, broken invariants, and empty cases
25 are failures in both modes and cannot be recorded as goldens. Provider timeout
26 errors are legitimate observable outcomes; a harness timing out is missing
27 evidence.
28
29 Platforms: `sse`, `responses`, and `mcp` run everywhere. `events`, `prompt` and `hooks`
30 goldens were recorded on Unix and compile only there — hook fixtures are POSIX
31 shell, and a Windows turn differs in shell and path facts no golden covers
32 yet. Recording Windows goldens is open work, not an implied capability.
33
34 Every `events` and `prompt` case declares `recorded_platform`: the execution
35 boundary its goldens were recorded under (`sandbox_enforcement`: `local_os`,
36 `unavailable` or `external_backend`; `no_new_privs_active`: `null` where the
37 flag does not exist, otherwise a bool). The Engine names this posture to the
38 model in `<turn_meta>`, so the harness replays the recorded facts instead of
39 probing the runner. Without that, a macOS-recorded golden fails on a Linux
40 runner for a fact about the machine, not the Engine. A case that omits the
41 field fails loud. The same record carries `os` and `shell` (`macos`, `/bin/bash`: the shell
42 dispatcher names the recording process's `$SHELL` path verbatim),
43 which the `## Environment` block renders into the frozen prompt prefix and so
44 into `prefix_cache_change`'s prefix hash; the harness replays those too, and
45 `host_tools`: the optional tools whose backend the recording machine had
46 (`code_execution`, `pandoc_convert`, `image_ocr`), which
47 change the registry the `tool_request_snapshot` counts.
48 Production always probes the host. The per-platform label
49 text is owned and tested in `sandbox::policy`. All current cases were
50 recorded on macOS. A Linux- or Windows-recorded golden is separate evidence.
51
52 ## Families
53
54 Each case is `<family>/<name>.case.json` (input, hand-written) plus one or
55 more `<name>.golden.*` files (output, recorded).
56
57 | family | input | production path | proves |
58 |---|---|---|---|
59 | `sse/` | `<name>.sse` recorded bytes + route | `CodewhaleClient::create_message_stream` via a loopback HTTP server | wire adapter: bytes → normalized stream events, incl. tool-call deltas, reasoning, usage, mid-stream errors, truncation |
60 | `responses/` | route, request, or `.sse` bytes | `RouteResolver`, `prepare_outbound_request`, and the same loopback stream | profile id, Responses request body, and termination differences |
61 | `events/` | scripted provider + host driver | `Engine::run` → `Engine::run_turn` | one turn → protocol `EventMsg` sequence + provider request count + workspace side effects |
62 | `mcp/` | scripted MCP server transcript + host steps | `McpPool` behind `Engine::execute_mcp_tool_with_pool` | catalog normalization and call results for one dispatch |
63 | `prompt/` | session config + workspace files | first `MessageRequest` of a real turn | model-visible prefix bytes (system prompt + tool catalog) |
64 | `hooks/` | user hook config + one tool call | `run_tool_call_before_hooks` / `HookExecutor::execute` | hook verdict fold, env contract, schema-1 stdin |
65
66 The host protocol corpus that Rust serde and the TypeScript host both parse
67 already lives in `../extension_host/protocol/`; it is not duplicated here.
68
69 ## Normalized formats
70
71 ### Stream events (`sse` goldens, `events` provider scripts)
72
73 One JSON object per event in exactly the serde shape of
74 `codewhale_models::StreamEvent` — the Anthropic Messages event vocabulary:
75 `message_start`, `content_block_start` (`text` / `thinking` / `tool_use` /
76 `server_tool_use`), `content_block_delta` (`text_delta` / `thinking_delta` /
77 `input_json_delta` / `signature_delta` / `reasoning_state_delta`),
78 `content_block_stop`, `message_delta` (stop reason + usage), `message_stop`,
79 `ping`, `error`, `tool_projection_warning`. The Rust runner proves every golden
80 event line deserializes into `StreamEvent` and serializes back unchanged, so an
81 `sse` golden can be pasted into an `events` script.
82
83 An `sse` golden's first line is `{"request": [{"method", "path"}]}` — the
84 request the adapter sent. A stream that errors yields
85 `{"type": "stream_failure", "detail": …}`; a request that fails before a
86 stream yields `{"type": "open_failure", "detail": …}`.
87
88 ### Turn events (`events` goldens)
89
90 Each line is `codewhale_protocol::EventMsg` as serialized (`"event"` tag), with:
91
92 - the per-session `thread_id` / `session_id` envelope removed;
93 - `tool_call_heartbeat` dropped (a liveness pulse, timing-dependent);
94 - UUIDs → `<uuid:N>` numbered by first appearance; RFC 3339 timestamps →
95 `<timestamp>`; the local date `<turn_meta>` states → `<today>`; `created_at`, `duration_ms`, `first_token_ms`, `request_ms`,
96 `elapsed_ms`, `pinned_combined_hash` → `"<masked>"`; temp paths →
97 `<WORKSPACE>` / `<HOME>` / `<TMP>`;
98 - `tool_catalog` and `system_prompt` bodies → `"<pinned by the prompt family>"`;
99 - an uninterrupted run of completion events put in a canonical order,
100 because parallel completions race and two tools' pairs can interleave:
101 `operation_activity_completed` observations by the established `span_id`,
102 then `tool_call_complete` events by `tool_call_id`; outcomes, span
103 relationships, and event counts stay exact, and no error or other event
104 is crossed. The activity events after a parallel batch's `Executing N ...
105 parallel chunk(s)` status are ordered the same way with starts first (a fast
106 tool can finish before its sibling starts), only when the run is causally
107 valid: every span starts once and completes once after its start, and a
108 tool completes after its own activity;
109 - keys sorted.
110
111 The last line is `{"harness_summary": {model_requests, non_streaming_requests,
112 workspace_after}}`: how many provider requests the turn made and every file
113 left in the workspace (path → sha256 prefix). `invariants` in a case are
114 checked independently of the golden (`workspace_file_absent`,
115 `workspace_file_present`, `max_model_requests`).
116
117 `events/provider_error_after_tool_call` requires the C02-05 (#6561) authority:
118 a failed response executes no collected tool call and issues no retry. Its
119 invariants are checked before recording, with no exemption for known defects.
120
121 ### MCP transcripts (`mcp` goldens)
122
123 The case's `server` object scripts a Streamable HTTP MCP server: one answer
124 per list method (`initialize`, `tools/list`, `resources/list`,
125 `resources/templates/list`, `prompts/list`) and, for `tools/call`,
126 `resources/read` and `prompts/get`, a list of `{"match": {…params}, …}`
127 entries answering with `result`, `error`, `progress` (notifications sent as
128 SSE before the result), or `hold` (never answer until the client goes away).
129 Notifications get `202`, a `GET` gets `405`, unknown methods get `-32601`.
130
131 `steps` are what a host does: `catalog`, or `call` a model-facing tool name
132 with `input` (optionally `"cancel": "after_server_holds"`). The golden records
133 `boot`, the catalog (`codewhale_models::Tool` list), each call as
134 `{"ok": {success, content, metadata, content_blocks}}` (JSON content parsed) or
135 `{"err": {kind, detail}}`, and `server_received`: every `tools/call`,
136 `resources/read` and `prompts/get` the server actually saw. The server URL is
137 masked. The golden does not name the dispatch: every dispatch must produce the
138 same file.
139
140 ### Prompt bytes (`prompt` goldens)
141
142 `<name>.golden.json` holds sha256 of the system prompt JSON, of the tool
143 catalog JSON, and of both (`prefix_sha256`), with sizes and tool names in
144 catalog order; `.system.golden.txt` and `.tools.golden.json` are the readable
145 bodies. Bytes are hashed as serialized — key order is part of the cached
146 prefix. Masks: temp paths, and the environment block's `- platform:` and
147 `- shell:` lines. The turn driver applies the case's `recorded_platform.host_tools` on
148 every host. The golden captures every tool definition the Engine puts on
149 the first request in wire order. Deferred tools enter that prefix only if
150 the Engine actually advertises them there. The
151 runner also fails if two identical sessions produce different prefixes.
152
153 ### Hook receipts (`hooks` goldens)
154
155 `hooks` is user configuration in the `[[hooks.hooks]]` shape. `{{capture}} NAME`
156 in a command runs a helper that saves the hook's stdin and every documented
157 environment variable (`CODEWHALE_*` / `DEEPSEEK_*`, see `HOOK_ENV_CONTRACT`).
158 The golden records the outcome — `{"admit": {requires_approval, updated_input,
159 additional_context}}` or `{"refuse": {kind, detail}}` for `tool_call_before`,
160 the observer results (including exact stdout, stderr and error) for
161 `tool_call_after` — and, per hook, whether it ran,
162 its stdin (the schema-1 document, parsed) and its contract environment. The
163 hook session id and temp paths are masked. POSIX shell: Unix only.
164
165 ## Running a TypeScript implementation against these fixtures
166
167 A TS implementation proves parity by producing byte-identical normalized
168 output — same masks, same key order (sorted, except prompt bytes) — for every
169 case, and by passing the Rust runner once it is wired in:
170
171 - **MCP (Phase 2).** Implement `HostMcpDispatch` and add it to `DISPATCHES` in
172 `crates/tui/src/conformance/mcp.rs`. The transcript server is a real loopback
173 URL, so the Node host connects to it with the official SDK exactly as it
174 would to a user's server. The same `mcp/*.golden.json` must pass unchanged.
175 - **Hooks (Phase 2).** Run the hook orchestration under test with each case's
176 `hooks` and tool call; the `{{capture}}` helper is plain `sh`, so the
177 captured stdin/env must match the golden without changes.
178 - **Tool adapters, slash commands, MCP catalog (Phases 2–3).** Whatever moves,
179 `prompt/*` must not change unless the change is intended and re-recorded in
180 the same PR — a prefix change invalidates every user's KV cache.
181 - **Provider wire adapters (Phase 4, gated).** Replay each `sse/*.sse` with its
182 case's chunking/framing and emit the stream-event format above.
183 - **Turn events.** The turn loop stays in Rust (D10); `events/*` is the
184 regression net around it while its edges move. A host-side consumer of
185 `EventMsg` can read these goldens as its contract.
186
187 Fields named `detail` carry human-readable error text and are compared exactly,
188 including by a second implementation. Only the explicitly documented masks and
189 projections apply. No whitespace, line-ending, error-text, or event-order normalization may
190 be added to make a drift pass. `.gitattributes` pins fixture checkout bytes to LF.
191
192 ## Coverage boundaries
193
194 The initial corpus contains 28 scenarios: 6 turns, 6 hooks, 2 MCP transcripts,
195 3 prefixes, and 11 SSE recordings. Each SSE case must reach its loopback server
196 and produce a normalized stream event; an incorrectly bound client that fails
197 before sending is missing evidence. Client construction resolves the case model
198 through the production route authority. Harness stalls are tested separately
199 using a hung Engine provider, an unanswered real MCP call, and an open SSE socket.
200
201 This is the Rust reference corpus, not TypeScript parity evidence. No TypeScript
202 dispatch is registered yet. The MCP cases use the production direct-call seam;
203 full-turn MCP admission, credentials/OAuth, stdio servers, reconnect/retry races,
204 and hosted server binaries need separate fixtures. Hook cases cover before-tool
205 admission and synchronous after-tool observers; message-submit/session hooks,
206 background observers, cancellation and process-tree teardown are not covered.
207 The turns do not qualify persistence/resume, nested multi-tool cardinality,
208 non-draining client backpressure, or every client projection. Slash-command,
209 script-tool, and migrated adapter calls need their own fixtures before flipping.
210 The Plan prefix records its advertised catalog and frozen prompt bytes.
211 It advertises `edit` and `write` and omits `bash`. Full-turn tests cover
212 read-only admission and refusal.
213
214 Windows turn/hook/prefix fixtures, Linux
215 replay, hosted CI, native app behavior, and real-provider acceptance are separate
216 evidence. This corpus supplies none of those receipts by itself.
217
217 lines MARKDOWN