Commit 922afee
authored
fix: keep hasToolResult record/replay symmetric + Anthropic tool_result+text scoping (#319)
## Problem
PR #316 (released as v1.37.3) scoped the replay-side `hasToolResult`
check to the **current turn** (messages after the last `user` message)
but left two paths inconsistent, so v1.37.3 can silently produce
fixtures that never match their own recorded request.
## Root cause
**1. Record/replay split-brain.** The matcher (`src/router.ts`) was
changed to a turn-scoped check, but the recorder (`src/recorder.ts`,
`buildFixtureMatch`) still STAMPED `hasToolResult` with a
whole-conversation predicate `messages.some(m => m.role === "tool")`.
Both operate on the same normalized `ChatCompletionRequest.messages`, so
for a genuinely-recorded **turn-2 leg-1** request `[user, assistant,
tool, user]` (a fresh question whose history still carries turn-1's tool
result):
- Recorder writes `hasToolResult: true` (whole conversation has a tool).
- Matcher on replay computes `false` (nothing after the last user) →
`true !== false` → the fixture can never match its own recorded request.
The comment at `recorder.ts` even claimed the value was "matcher-aware"
— it no longer was.
**2. Anthropic `tool_result` + text ordering.** Anthropic bundles a
leg-2 `tool_result` and accompanying user text into ONE user message.
Normalization emitted `[..., tool, user(text)]`, which is
**byte-identical** to a genuine leg-1 turn, so the turn-scoped check saw
no tool after the last user and misclassified a real leg-2 turn as
`false`. Because the two shapes are indistinguishable *after*
normalization, this can only be fixed where the source-message grouping
is still known — in normalization, not in the shared helper.
## Fix
- Extracted one shared `currentTurnHasToolResult(messages)` helper in
`src/router.ts` (where the other message-shape utilities live and which
`recorder.ts` already imports) and used it on **both** the record side
and the match side. Sharing a single predicate makes the two sides
structurally incapable of drifting. Replaced the false "matcher-aware"
comment.
- Fixed `claudeToCompletionRequest` to emit the accompanying text user
message **before** the tool message(s), so a real Anthropic
leg-2-with-text turn normalizes to `[..., user(text), tool]` and is
classified `true`, while a genuine leg-1 turn (separate messages) stays
`false`. The common `tool_result`-only follow-up is unchanged.
## Red-green proof
Both defects are proven on the **real HTTP record→replay surface** (spin
up a mock upstream, record through the real recorder proxy to disk,
replay the recorded fixtures through the real server/matcher). Source
fixes were `git stash`-ed to observe RED, then restored for GREEN —
tests unchanged between runs.
### RED (source fixes reverted)
```
FAIL hasToolResult record→replay symmetry > replays a genuinely-recorded turn-2 leg-1 request against its own recorded fixture
AssertionError: expected 503 to be 200 // Object.is equality
- Expected 200
+ Received 503
FAIL hasToolResult record→replay symmetry > replays an Anthropic leg-2 turn that bundles tool_result WITH accompanying text
AssertionError: expected 503 to be 200 // Object.is equality
- Expected 200
+ Received 503
FAIL claudeToCompletionRequest > handles tool_result with text blocks alongside in same user message
AssertionError: expected 'tool' to be 'user' // Object.is equality
Expected: "user"
Received: "tool"
```
(503 = strict-mode "No fixture matched": the recorded fixture could not
match its own request.)
### GREEN (source fixes restored, tests unchanged)
```
✓ src/__tests__/hastoolresult-record-replay-symmetry.test.ts (2 tests)
✓ src/__tests__/router.test.ts (144 tests)
✓ src/__tests__/messages.test.ts (73 tests)
✓ src/__tests__/thinking-invariants.test.ts (47 tests)
Test Files 4 passed (4)
Tests 266 passed (266)
```
Both the OpenAI split-brain (Finding 1) and the Anthropic
tool_result+text case (Finding 2) are covered by the real-path HTTP
record→replay test, so both got a genuine RED→GREEN on the actual
failure surface (not tests-against-fakes).
## Tests added
- `src/__tests__/hastoolresult-record-replay-symmetry.test.ts` — full
HTTP record→replay for (a) an OpenAI turn-2 leg-1 request and (b) an
Anthropic `tool_result`+text leg-2 turn; both would 503 pre-fix.
- `router.test.ts` — direct `currentTurnHasToolResult` coverage (leg-2
true; new-turn turn-scoped false; leading-system-message; `lastUserIdx
=== -1` no-user whole-conversation fallback), plus matcher composition
of the scoping with explicit `turnIndex` and `sequenceIndex` gates, and
a system-message-led new-turn case.
- `messages.test.ts` — the two existing tool_result+text tests that
encoded the old (buggy) tool-before-text order were corrected to the
fixed order.
## Full suite
`pnpm format:check`, `pnpm lint`, `pnpm build` all clean. `pnpm test`:
4683 passed, 1 pre-existing wall-clock flake (`timing-replay.test.ts >
replaySpeed 2.0 halves the replay duration`, `expected 209 to be less
than 200`) that passes in isolation and is unrelated to this change.
## Notes
No version bump and no changeset (repo has no `.changeset/`; releases
are cut by a separate release PR). This completes the incomplete fix
from #316.
🤖 Generated with [Claude Code](https://claude.com/claude-code)9 files changed
Lines changed: 487 additions & 66 deletions
File tree
- skills/write-fixtures
- src
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
3 | 16 | | |
4 | 17 | | |
5 | 18 | | |
| |||
Large diffs are not rendered by default.
Lines changed: 197 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
854 | 854 | | |
855 | 855 | | |
856 | 856 | | |
857 | | - | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
858 | 862 | | |
859 | | - | |
860 | | - | |
861 | | - | |
862 | | - | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
863 | 867 | | |
864 | 868 | | |
865 | 869 | | |
| |||
1086 | 1090 | | |
1087 | 1091 | | |
1088 | 1092 | | |
1089 | | - | |
1090 | | - | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
1091 | 1099 | | |
1092 | 1100 | | |
1093 | 1101 | | |
| |||
0 commit comments