Skip to content

fix(recorder): keep OpenRouter delta.reasoning when collapsing streams - #664

Merged
jpr5 merged 1 commit into
mainfrom
fix/recorder-openrouter-reasoning
Dec 3, 2026
Merged

jpr5 merged 1 commit into
mainfrom
fix/recorder-openrouter-reasoning

Conversation

@jpr5

@jpr5 jpr5 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

When aimock records a real OpenRouter streaming chat completion, the reasoning is dropped. OpenRouter streams it as delta.reasoning (with a mirrored delta.reasoning_details array), but the OpenAI-family collapser only read delta.reasoning_content, so the recorded fixture had no reasoning.

The collapser now reads reasoning_content when it is a string, otherwise reasoning. It takes one source per delta, so a delta that carries both fields with the same text (as aimock's own OpenRouter stream will after #663) is not counted twice. reasoning_details is not read anywhere in the collapse path, so it adds nothing here. Streams that only use reasoning_content collapse exactly as before.

The new test file src/__tests__/recorder-openrouter-reasoning.test.ts runs a recorder with providers.openrouter pointed at a local upstream that streams OpenRouter-shaped SSE (the K9 drift shape), posts to /api/v1/chat/completions, and reads the fixture file the recorder writes. It also covers a delta with both fields, a reasoning_content-only control, and a reasoning: null length terminal.

Red (origin/main, before the fix)

 FAIL  recorder-openrouter-reasoning.test.ts > recording an OpenRouter stream keeps delta.reasoning > writes OpenRouter delta.reasoning (with reasoning_details) into the fixture
AssertionError: expected undefined to be 'Work through the arithmetic before giving an answer.'
 FAIL  recorder-openrouter-reasoning.test.ts > collapseOpenAISSE reasoning sources > ignores a null delta.reasoning (OpenRouter length terminal)
AssertionError: expected undefined to be 'abc'
 FAIL  recorder-openrouter-reasoning.test.ts > collapseOpenAISSE reasoning sources > prefers reasoning_content per delta when both fields are strings
AssertionError: expected 'x' to be 'xy'
 Test Files  1 failed (1)
      Tests  3 failed | 2 passed (5)

The both-fields recording test and the reasoning_content control pass on main too. They are there to show the fix does not double-count and does not change existing behavior.

Green (after the fix), with the related suites

 ✓ src/__tests__/stream-collapse-string-cap.test.ts (6 tests)
 ✓ src/__tests__/collapser-robustness.test.ts (26 tests)
 ✓ src/__tests__/openrouter-passthrough-proto.test.ts (3 tests)
 ✓ src/__tests__/recorder-openrouter-reasoning.test.ts (5 tests)
 ✓ src/__tests__/openai-stream-usage-record-replay.test.ts (13 tests)
 ✓ src/__tests__/recorder-reasoning-replay.test.ts (30 tests)
 ✓ src/__tests__/stream-collapse.test.ts (235 tests)
 ✓ src/__tests__/openrouter-chat.test.ts (61 tests)
 ✓ src/__tests__/recorder.test.ts (141 tests)
 Test Files  9 passed (9)
      Tests  520 passed (520)

Prettier, eslint and pnpm run typecheck are clean. pnpm build && pnpm api:report reports 14 entries with 0 failures and no change to etc/.

@jpr5
jpr5 force-pushed the fix/recorder-openrouter-reasoning branch from 6a7a56d to 5cb647a Compare December 3, 2026 23:04
@pkg-pr-new

pkg-pr-new Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@copilotkit/aimock@664

commit: 67d9b83

OpenRouter streams reasoning as delta.reasoning (mirrored in
reasoning_details), not delta.reasoning_content, so recorded OpenRouter
fixtures lost their reasoning.

Read reasoning_content when it is a string, else reasoning. Take one
source per delta so a delta carrying both fields is not double-counted.
@jpr5
jpr5 force-pushed the fix/recorder-openrouter-reasoning branch from 017fb68 to 67d9b83 Compare December 3, 2026 23:18
@jpr5
jpr5 merged commit 8444676 into main Dec 3, 2026
30 checks passed
@jpr5
jpr5 deleted the fix/recorder-openrouter-reasoning branch December 3, 2026 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant