Skip to content

Commit 6e2a4c7

Browse files
authored
test(drift): accept Cohere upstream no-generation outcomes on the live leg (#525)
## Problem Two Cohere live-drift failures on `main` (9420afa). They have the same root cause: - **`cohere.drift.ts:688` `expected object`** in `cohereRecord` ← `cohereUsage` ← `readCohereK5:884` (stream branch). Seen in both legs of PR #524's drift-live-pr run 38157499382. - **`cohere.drift.ts:324` `expected 422 to be 200`** in the non-streaming shape test. Seen in post-merge Drift Tests run 38158387279. Neither appears in any main drift report before 2026-10-11 ~17:00 UTC. I scanned the artifacts of every main/PR drift run back to 37580386296. ## Diagnosis: upstream nondeterminism, not a contract change Sometimes Cohere generates nothing. Live probes of the exact test requests on 2026-10-11 (trial key; 429s excluded): | Request | Outcome | Count | |---|---|---| | K5 object (`tool_choice: REQUIRED`, 128 tok) | 200 `MAX_TOKENS`, normal usage | 15 | | K5 object | **422** `{"error_type":"NO_TOOL_CALL_OR_RESPONSE_GENERATED","id":…,"message":…}` | 5 | | K5 stream | 200 `TOOL_CALL`, normal usage | 29 | | K5 stream | **200 SSE, `message-end` = `{"finish_reason":"ERROR","usage":{},"error":"No tool calls or response was generated…","error_type":"NO_TOOL_CALL_OR_RESPONSE_GENERATED"}`** | 8 | | Shape leg `Say hello` (object) | 200 | 13 | | Shape leg `Say hello` (object) | **422** `{"error_type":"NO_VALID_RESPONSE_GENERATED",…}` | 2 | | Shape leg `Say hello` (stream) | 200 | 12 | | Shape leg `Say hello` (stream) | **422** `NO_VALID_RESPONSE_GENERATED` | 3 | The `usage: {}` on the stream ERROR terminal is exactly what fails at line 688: `usage.tokens` is undefined. The 422 is what fails at line 324. Successful responses kept the same shape throughout, so aimock's Cohere builder has nothing to follow. No `src/` code changes; only test files. ## Fix (live leg only; aimock and replay legs stay strict) - `cohere-model.ts`: `cohereNoGenerationError(status, body, allowed)` matches only a 422 whose keys are exactly `{error_type,id,message}` (non-empty strings) and whose code is in `allowed`. It returns `{errorType, id}`. The codes are per request: the no-tools shape legs (`COHERE_SHAPE_LEG_NO_GENERATION`) accept only `NO_VALID_RESPONSE_GENERATED`, and the K5 tool request (`COHERE_K5_NO_GENERATION`) accepts only `NO_TOOL_CALL_OR_RESPONSE_GENERATED`. `isCohereNoGenerationStreamEnd(delta)` matches only the exact observed ERROR terminal. - K5: `readCohereK5`/`compareCohereK5`/`runCohereK5Modes` take a `{ live }` option. Only the live test sets it. The exact variants become `UPSTREAM_NO_GENERATION`: a 422 on **either** send, or the stream ERROR terminal. The stream variant must not have emitted any tool or content first, and the SDK error must be a `CohereError` with status 422. Anything else rethrows. A refused 422 rethrows with `cause: { sdk, classifier }` so the classifier's reason is kept. - **A K5 run where every send is `UPSTREAM_NO_GENERATION` is a skip, not a pass.** Its reason cites each upstream code and id. One compared send is still a pass. - The shape legs (non-streaming and streaming) skip on the exact 422 envelope. Every `ctx.skip` carries a reason with the upstream code or status and the response id. - Genuine live captures are kept as hashed fixtures. The 422 on the K5 stream send is a constructed replay (not yet observed live) and is marked as such. ## Local red/green **RED: unmodified main @9420afa1, same filtered live command, 8 runs:** run 5 reproduced the CI stack trace exactly: ``` × P1 Cohere native K5 object modeled and stream observed comparison AssertionError: expected object ❯ cohereRecord cohere.drift.ts:688:3 ❯ cohereUsage cohere.drift.ts:698:19 ❯ readCohereK5 cohere.drift.ts:884:13 ``` Replay RED: the new strict-leg test asserts that the unmodified reader throws `expected object` on the captured stream and `HTTP failure` on the captured 422. **GREEN: this branch, same command, 8 consecutive live runs, all passed.** They included real occurrences of the variants: ``` run 2: [object UPSTREAM_NO_GENERATION, stream UPSTREAM_NO_GENERATION] ✓ run 5: [object UPSTREAM_NO_GENERATION, stream TARGET] ✓ run 6: [object UPSTREAM_NO_GENERATION, stream TARGET] ✓ run 7: [object NOT_TRIGGERED, stream UPSTREAM_NO_GENERATION] ✓ runs 1,3,4,8: [object NOT_TRIGGERED, stream TARGET] ✓ ``` **Mutants, round 1 (5/5 killed):** removing the live gate on stream, removing the live gate on object, making the stream-end classifier always true, making the 422 classifier accept any error type, and dropping the tool-before-terminal check. **Mutants, round 2 after review (12/12 killed):** all-no-generation skip disabled · 422 accepted on the object send only · shape leg allows the tool code · `allowed` filter dropped · bare rethrow instead of rethrow-with-cause · `contentCount` guard deleted · `typeof` deleted on id, message and stream `error` · `null`-body guard deleted · 422 `complete` guard deleted · 422 content-type guard deleted. **Live after review, 6 consecutive runs, 0 failures.** Run 1 showed the new skip working live: ``` ↓ P1 Cohere native K5 … [Cohere upstream generated nothing (NO_TOOL_CALL_OR_RESPONSE_GENERATED id=53e39cc7-…; NO_TOOL_CALL_OR_RESPONSE_GENERATED id=88013390-…); no success envelope to compare] ↓ Cohere drift > non-streaming /v2/chat shape matches [Cohere upstream generated nothing (NO_VALID_RESPONSE_GENERATED id=7d6ebc99-…); …] ``` Runs 2–6: K5 passed every time (3× stream TARGET, 2× stream UPSTREAM_NO_GENERATION with the object compared). Two shape-leg sends were skipped with reasons. **Gates (after review):** `pnpm typecheck` ✓, `pnpm lint` ✓, `pnpm format:check` ✓, `pnpm build` ✓, `pnpm test` ✓ (377 files, 14079 passed). Cohere drift file offline: 23 passed / 3 skipped. ## Tradeoff When the stream send hits no-generation (about 22% of probes), that run does not observe K5 `TARGET`. If the object send is compared, the run passes. If both sends generate nothing, the run is reported as skipped. This follows the precedent approved today for genuine upstream nondeterminism: accept the exact variants on the live leg rather than retry. ## Backlog (out of scope) - `parseDataOnlySSE` drops `event:` frames (pre-existing). - The K5 422 path has no body-size check.
2 parents 9420afa + 81edabd commit 6e2a4c7

6 files changed

Lines changed: 692 additions & 25 deletions

File tree

‎src/__tests__/drift/cohere-model.test.ts‎

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@
88
*/
99

1010
import { describe, it, expect } from "vitest";
11-
import { isInfraStatus, selectCohereChatModel, type CohereModelEntry } from "./cohere-model.js";
11+
import { readFileSync } from "node:fs";
12+
import {
13+
COHERE_K5_NO_GENERATION,
14+
COHERE_SHAPE_LEG_NO_GENERATION,
15+
cohereNoGenerationError,
16+
cohereNoGenerationSkipReason,
17+
isCohereNoGenerationStreamEnd,
18+
isInfraStatus,
19+
selectCohereChatModel,
20+
type CohereModelEntry,
21+
} from "./cohere-model.js";
1222

1323
describe("selectCohereChatModel", () => {
1424
it("never selects a deprecated model (the command-r-plus 404 regression)", () => {
@@ -59,3 +69,95 @@ describe("isInfraStatus", () => {
5969
}
6070
});
6171
});
72+
73+
describe("cohereNoGenerationError", () => {
74+
// Genuine live 422 body for the `Say hello` shape-leg request (2026-10-11).
75+
const captured = readFileSync(
76+
new URL("./fixtures/cohere-chat-no-valid-response.raw", import.meta.url),
77+
"utf8",
78+
);
79+
const body = JSON.parse(captured) as Record<string, unknown>;
80+
const shape = COHERE_SHAPE_LEG_NO_GENERATION;
81+
const tool = COHERE_K5_NO_GENERATION;
82+
const toolBody = JSON.stringify({ ...body, error_type: "NO_TOOL_CALL_OR_RESPONSE_GENERATED" });
83+
84+
it("recognizes the genuine captured 422 envelope and returns its code and id", () => {
85+
expect(cohereNoGenerationError(422, captured, shape)).toEqual({
86+
errorType: "NO_VALID_RESPONSE_GENERATED",
87+
id: body.id,
88+
});
89+
expect(cohereNoGenerationError(422, toolBody, tool)).toEqual({
90+
errorType: "NO_TOOL_CALL_OR_RESPONSE_GENERATED",
91+
id: body.id,
92+
});
93+
});
94+
95+
it("accepts only the codes the caller allows", () => {
96+
// A no-tools shape request must not accept the tool-call code, and vice versa.
97+
expect(cohereNoGenerationError(422, toolBody, shape)).toBeNull();
98+
expect(cohereNoGenerationError(422, captured, tool)).toBeNull();
99+
});
100+
101+
it("rejects every other status, shape or error type", () => {
102+
const both = [...shape, ...tool];
103+
expect(cohereNoGenerationError(200, captured, both)).toBeNull();
104+
expect(cohereNoGenerationError(400, captured, both)).toBeNull();
105+
expect(cohereNoGenerationError(422, "not json", both)).toBeNull();
106+
expect(cohereNoGenerationError(422, "null", both)).toBeNull();
107+
expect(cohereNoGenerationError(422, "[]", both)).toBeNull();
108+
for (const mutant of [
109+
{ ...body, error_type: "OTHER" },
110+
{ ...body, id: "" },
111+
{ ...body, id: 123 },
112+
{ ...body, message: "" },
113+
{ ...body, message: 123 },
114+
{ ...body, extra: 1 },
115+
])
116+
expect(
117+
cohereNoGenerationError(422, JSON.stringify(mutant), both),
118+
JSON.stringify(mutant),
119+
).toBeNull();
120+
const missing = { ...body };
121+
delete missing.id;
122+
expect(cohereNoGenerationError(422, JSON.stringify(missing), both)).toBeNull();
123+
});
124+
});
125+
126+
describe("cohereNoGenerationSkipReason", () => {
127+
it("cites every upstream code and id", () => {
128+
const reason = cohereNoGenerationSkipReason([
129+
{ errorType: "NO_TOOL_CALL_OR_RESPONSE_GENERATED", id: "a1" },
130+
{ errorType: "NO_VALID_RESPONSE_GENERATED", id: "b2" },
131+
]);
132+
expect(reason).toContain("NO_TOOL_CALL_OR_RESPONSE_GENERATED id=a1");
133+
expect(reason).toContain("NO_VALID_RESPONSE_GENERATED id=b2");
134+
});
135+
});
136+
137+
describe("isCohereNoGenerationStreamEnd", () => {
138+
const end = {
139+
finish_reason: "ERROR",
140+
usage: {},
141+
error: "No tool calls or response was generated. Try updating messages or tool definitions",
142+
error_type: "NO_TOOL_CALL_OR_RESPONSE_GENERATED",
143+
};
144+
145+
it("recognizes the exact observed mid-stream terminal", () => {
146+
expect(isCohereNoGenerationStreamEnd(end)).toBe(true);
147+
});
148+
149+
it("rejects any deviation", () => {
150+
for (const mutant of [
151+
{ ...end, finish_reason: "COMPLETE" },
152+
{ ...end, usage: { tokens: {} } },
153+
{ ...end, usage: [] },
154+
{ ...end, usage: null },
155+
{ ...end, error: "" },
156+
{ ...end, error: 5 },
157+
{ ...end, error_type: "NO_VALID_RESPONSE_GENERATED" },
158+
{ ...end, extra: 1 },
159+
{ finish_reason: "ERROR", usage: {}, error_type: end.error_type },
160+
])
161+
expect(isCohereNoGenerationStreamEnd(mutant), JSON.stringify(mutant)).toBe(false);
162+
});
163+
});

‎src/__tests__/drift/cohere-model.ts‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,101 @@ export function selectCohereChatModel(models: CohereModelEntry[]): string | null
5050
const preferred = ["command-a-03-2025", "command-r-08-2024", "command-r7b-12-2024"];
5151
return preferred.find((p) => names.includes(p)) ?? names[0] ?? null;
5252
}
53+
54+
/**
55+
* Cohere's documented "the model generated nothing usable" error types. Live
56+
* probes (2026-10-11) observed both as an HTTP 422 envelope with exactly
57+
* `{error_type, id, message}`: NO_VALID_RESPONSE_GENERATED for a plain
58+
* `Say hello` chat request, NO_TOOL_CALL_OR_RESPONSE_GENERATED for a
59+
* `tool_choice: REQUIRED` request. The same tool-request condition also
60+
* arrives mid-stream as a 200 SSE whose `message-end` carries
61+
* `finish_reason: "ERROR"`, `usage: {}` and that error type.
62+
*/
63+
export const COHERE_NO_GENERATION_ERROR_TYPES = [
64+
"NO_VALID_RESPONSE_GENERATED",
65+
"NO_TOOL_CALL_OR_RESPONSE_GENERATED",
66+
] as const;
67+
export type CohereNoGenerationErrorType = (typeof COHERE_NO_GENERATION_ERROR_TYPES)[number];
68+
69+
function isNoGenerationErrorType(value: unknown): value is CohereNoGenerationErrorType {
70+
return (COHERE_NO_GENERATION_ERROR_TYPES as readonly unknown[]).includes(value);
71+
}
72+
73+
/** Codes the no-tools `Say hello` shape legs can receive. */
74+
export const COHERE_SHAPE_LEG_NO_GENERATION = ["NO_VALID_RESPONSE_GENERATED"] as const;
75+
/** Codes the K5 `tool_choice: REQUIRED` request can receive. */
76+
export const COHERE_K5_NO_GENERATION = ["NO_TOOL_CALL_OR_RESPONSE_GENERATED"] as const;
77+
78+
/** An exact upstream no-generation 422 envelope, with the fields a skip reason cites. */
79+
export interface CohereNoGeneration {
80+
errorType: CohereNoGenerationErrorType;
81+
id: string;
82+
}
83+
84+
/**
85+
* Classify a live Cohere response as the exact upstream no-generation 422
86+
* envelope. Returns `{ errorType, id }` only for status 422 with a JSON object
87+
* body whose keys are exactly `error_type`, `id` and `message` (both strings
88+
* non-empty) and whose `error_type` is in `allowed`. Anything else — another
89+
* status, an extra or missing key, a non-string field, another error type, a
90+
* non-JSON or `null` body — returns null, so the caller keeps failing on it.
91+
*
92+
* `allowed` must name only the codes the request can produce: a request with
93+
* no tools passes `["NO_VALID_RESPONSE_GENERATED"]`; a tool request passes
94+
* `["NO_TOOL_CALL_OR_RESPONSE_GENERATED"]`.
95+
*
96+
* This is upstream model nondeterminism, not envelope drift: the same request
97+
* succeeds on the next send. Only the live leg may use it; the aimock leg
98+
* stays strict.
99+
*/
100+
export function cohereNoGenerationError(
101+
status: number,
102+
body: string,
103+
allowed: readonly CohereNoGenerationErrorType[],
104+
): CohereNoGeneration | null {
105+
if (status !== 422) return null;
106+
let parsed: unknown;
107+
try {
108+
parsed = JSON.parse(body);
109+
} catch {
110+
return null;
111+
}
112+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return null;
113+
const record = parsed as Record<string, unknown>;
114+
const keys = Object.keys(record).sort();
115+
if (keys.join(",") !== "error_type,id,message") return null;
116+
if (typeof record.id !== "string" || record.id.length === 0) return null;
117+
if (typeof record.message !== "string" || record.message.length === 0) return null;
118+
if (!isNoGenerationErrorType(record.error_type)) return null;
119+
if (!allowed.includes(record.error_type)) return null;
120+
return { errorType: record.error_type, id: record.id };
121+
}
122+
123+
/** Skip reason that cites the upstream code and response id. */
124+
export function cohereNoGenerationSkipReason(outcomes: CohereNoGeneration[]): string {
125+
return `Cohere upstream generated nothing (${outcomes
126+
.map(({ errorType, id }) => `${errorType} id=${id}`)
127+
.join("; ")}); no success envelope to compare`;
128+
}
129+
130+
/**
131+
* Classify a live Cohere SSE `message-end` delta as the exact mid-stream
132+
* no-generation terminal: keys exactly `finish_reason`, `usage`, `error`,
133+
* `error_type`, with `finish_reason === "ERROR"`, `usage` an empty object,
134+
* `error` a non-empty string and `error_type` NO_TOOL_CALL_OR_RESPONSE_GENERATED.
135+
* Returns false for every other terminal.
136+
*/
137+
export function isCohereNoGenerationStreamEnd(delta: Record<string, unknown>): boolean {
138+
const keys = Object.keys(delta).sort();
139+
if (keys.join(",") !== "error,error_type,finish_reason,usage") return false;
140+
if (delta.finish_reason !== "ERROR") return false;
141+
if (delta.error_type !== "NO_TOOL_CALL_OR_RESPONSE_GENERATED") return false;
142+
if (typeof delta.error !== "string" || delta.error.length === 0) return false;
143+
const usage = delta.usage;
144+
return (
145+
usage !== null &&
146+
typeof usage === "object" &&
147+
!Array.isArray(usage) &&
148+
Object.keys(usage).length === 0
149+
);
150+
}

0 commit comments

Comments
 (0)