Repository navigation
Commit 75e04da
authored
test: make the Cohere streaming drift leg compare real event frames (#528)
## Problem
The streaming `/v2/chat` shape leg in `cohere.drift.ts` never compared
anything. It parsed both bodies with `parseDataOnlySSE`, which keeps
only frames that *start* with `data: `. Every Cohere v2 frame is `event:
<type>` + `data: <json>`, so both lists came out empty, the `if
(realChunks.length > 0 && mockChunks.length > 0)` guard was false, and
the test always passed. (A non-200 mock reply was also let through
without a comparison.)
## Fix (test-only; no `src/` behavior change)
- `parseCohereSSE`: strict parser. Each frame must be exactly `event:` +
`data:` with `data.type` equal to the event name. A trailing `data:
[DONE]` is dropped. Any other frame throws instead of being dropped.
- The leg now requires mock status 200 and non-empty event lists on both
sides, opening with `message-start` and closing with `message-end`. It
compares every event type with `compareSSESequences` against SDK shapes
for message-start / content-start / content-delta / content-end /
message-end (the same pattern the Anthropic stream leg uses).
- Live no-generation: the 422 envelope skip is unchanged. A 200 stream
whose `message-end` is the no-generation terminal (`finish_reason:
"ERROR"`, empty usage) is now a skip that gives the upstream code and id
(`NO_VALID_RESPONSE_GENERATED id=...`), the same as #525.
`isCohereNoGenerationStreamEnd` now takes the allowed error types, like
`cohereNoGenerationError`. K5 passes `COHERE_K5_NO_GENERATION`, so K5
behavior is unchanged.
- Offline tests: the parser on the recorded live K5 stream fixture, on
aimock's real stream, and rejection of malformed frames. Classifier test
for the `allowed` param.
## Local RED (current main, live Cohere)
Temporary probe in the leg:
```
RED-PROBE realChunks=0 mockChunks=0 realBytes=1607 mockBytes=709
✓ Cohere drift > streaming /v2/chat shape matches
```
Mutant in `src/cohere.ts` (text stream `message-end`: `finish_reason`
renamed to `finishReason`). The test **still passes**:
```
RED-PROBE realChunks=0 mockChunks=0 realBytes=1607 mockBytes=708
Tests 2 passed | 24 skipped (26)
```
## Local GREEN (this branch, live Cohere)
```
GREEN-PROBE realEvents=13 [message-start,content-start,content-delta x9,content-end,message-end] mockEvents=5 [message-start,content-start,content-delta,content-end,message-end]
```
Two consecutive live runs, probe removed:
```
run 1: ✓ Cohere drift > streaming /v2/chat shape matches 304ms
run 2: ✓ Cohere drift > streaming /v2/chat shape matches 317ms
```
The same `finishReason` mutant now **fails**:
```
× Cohere drift > streaming /v2/chat shape matches
[critical] LLMOCK DRIFT — field in SDK + real API but missing from mock
Path: SSE:message-end.delta.finish_reason
```
A second mutant (drop the `content-end` event) also **fails**:
```
[critical] LLMOCK DRIFT — real API emits event type "content-end" but mock does not
```
Both mutants reverted.
## Non-critical differences now visible (not fixed here)
These come from the comparison, but the existing triangulation rules do
not rate them critical: live `usage.cached_tokens`, live `content-start`
carries `text: ""`, and aimock adds `content.type` on deltas plus
`billed_units.search_units/classifications`. Outside the shape
comparison: live ends with `data: [DONE]` and aimock's Cohere stream
does not.
## Gates
`pnpm typecheck`, `pnpm lint`, `pnpm format:check`, `pnpm build`, `pnpm
test` (377 files, 14080 passed) all pass.
## Follow-up: parse by the SSE spec (review finding)
`parseCohereSSE` threw on legal SSE: a single trailing `\n`, `event:X`
with no space, `:` keepalive comments, and `id:`/`retry:` lines. It now
follows the WHATWG event-stream rules: any line ending (`\n`, `\r\n`,
`\r`), the space after the colon is optional, comments and
`id`/`retry`/unknown fields are ignored, multi-line `data` is joined,
and a final event is dispatched without a trailing blank line. These
still throw: data that is not JSON, data that is not an object, an event
name that does not match `data.type` (an event without `event:` is named
`message` by the spec), and any event after `[DONE]`.
RED: the new offline tests run against the previous parser:
```
× parseCohereSSE > accepts legal SSE variants a server or proxy may send
Error: Unsupported Cohere SSE frame: "event: message-end\ndata: {...}\n"
× parseCohereSSE > rejects malformed data, mismatched names and events after [DONE]
```
GREEN: all 4 parser tests pass. Live streaming leg passed 2 consecutive
times. The `finishReason` mutant still fails on
`SSE:message-end.delta.finish_reason`. Gates pass again (377 files,
14080 tests).3 files changed
Lines changed: 249 additions & 71 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
146 | | - | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
147 | 155 | | |
148 | 156 | | |
149 | 157 | | |
| |||
158 | 166 | | |
159 | 167 | | |
160 | 168 | | |
161 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
162 | 173 | | |
163 | 174 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
134 | | - | |
135 | | - | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
136 | 140 | | |
137 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
138 | 145 | | |
139 | 146 | | |
140 | 147 | | |
141 | | - | |
| 148 | + | |
| 149 | + | |
142 | 150 | | |
143 | 151 | | |
144 | 152 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | 19 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
| 89 | + | |
90 | 90 | | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
98 | 101 | | |
99 | 102 | | |
100 | | - | |
| 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 | + | |
| 198 | + | |
101 | 199 | | |
102 | 200 | | |
103 | 201 | | |
| |||
382 | 480 | | |
383 | 481 | | |
384 | 482 | | |
385 | | - | |
386 | 483 | | |
387 | 484 | | |
388 | 485 | | |
| |||
413 | 510 | | |
414 | 511 | | |
415 | 512 | | |
416 | | - | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
417 | 541 | | |
418 | | - | |
419 | | - | |
420 | | - | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
434 | 549 | | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
442 | | - | |
443 | | - | |
444 | | - | |
445 | | - | |
446 | | - | |
447 | | - | |
448 | | - | |
449 | | - | |
450 | | - | |
451 | | - | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
452 | 563 | | |
453 | | - | |
454 | | - | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
455 | 575 | | |
456 | | - | |
457 | | - | |
458 | | - | |
459 | | - | |
460 | | - | |
461 | | - | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
462 | 592 | | |
463 | | - | |
464 | | - | |
465 | | - | |
466 | | - | |
467 | | - | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
468 | 613 | | |
469 | 614 | | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
470 | 629 | | |
471 | 630 | | |
472 | 631 | | |
| |||
954 | 1113 | | |
955 | 1114 | | |
956 | 1115 | | |
957 | | - | |
| 1116 | + | |
958 | 1117 | | |
959 | 1118 | | |
960 | 1119 | | |
| |||
0 commit comments