Skip to content

Commit 1ba9750

Browse files
authored
test: accept Vertex stream interpreter anomaly; keep measuring after rejections (#526)
## Problem Drift Tests run 38158387279 (main @ 9420afa) failed `k5-stream` with `FAILURE / MAX_TOKENS / WIRE_OR_SDK_REJECTED`. Vertex streamed the known interpreter anomaly as two chunks: a `google:python_interpreter` call, then a `MAX_TOKENS` terminal with one empty text part. The suite only accepted that anomaly in object mode. `k9-object` and `k9-stream` were then `UNATTEMPTED` (`earlier-case-failed`), so we could not see their live behavior. ## Live measurement (2026-10-11, llmock-drift-testing / us-central1 / gemini-2.5-flash, exact drift request bytes) | Case | n | Outcome | Count | Before | After | |---|---|---|---|---|---| | k5-object | 60 | interpreter anomaly, Tokyo | 29 | accepted | accepted | | k5-object | | interpreter anomaly, **London** | 1 | **rejected** | accepted | | k5-object | | MALFORMED_FUNCTION_CALL, `content: {}` | 30 | accepted | accepted | | k5-stream | 60 | two-chunk interpreter anomaly, Tokyo | 30 | **rejected** | accepted | | k5-stream | | two-chunk interpreter anomaly, **London** | 2 | **rejected** | accepted | | k5-stream | | MALFORMED_FUNCTION_CALL | 28 | accepted | accepted | | k9-object | 20 | thought + visible text, MAX_TOKENS | 15 | accepted | accepted | | k9-object | | reasoning-only, MAX_TOKENS (target) | 5 | accepted | accepted | | k9-stream | 20 | thought + visible text + empty terminal | 18 | accepted | accepted | | k9-stream | | reasoning-only, MAX_TOKENS (target) | 2 | accepted | accepted | Before this change, k5-stream was rejected in 32 of 60 runs. After it, all 161 captures are accepted (the 160 local captures plus the CI capture). ## Changes (`src/__tests__/drift/vertex-misbehavior.drift.ts` only) 1. **Comparator, native leg only.** Accepts the stream interpreter anomaly only in its observed shape, checked position by position: chunk 1 carries only the interpreter call, and chunk 2 is the `MAX_TOKENS` terminal with exactly one `{text: ""}` part. Accepts the first-city codes `Tokyo` and `London`, both observed live. The K5 prompt names no cities (it asks for "200 distinct full city names"), so there is no list to derive from the request. The accepted list comes from observation only, and any other city is still rejected. The modeled leg stays strict. Other terminals, function names, cities, args and call fields, and any thought, non-empty or extra parts, are still rejected. 2. **Recurrence runner.** When the comparator rejects a complete HTTP 200, that case is recorded as `FAILURE` and the run continues to the remaining cases. HTTP, transport and byte-bound failures still stop the run. The per-case assertions and the 4-send / 160-token budget are unchanged. ## Red / green - **RED (before the fix):** the CI capture (sha256 `6c4382…87a5`) and the London captures are rejected with `contradictory-target: K5 tool or thought`, the same rejection as CI. The recurrence stopped after 2 requests. Result: 4 failed / 93 passed. - **GREEN (after the fix):** 99 passed / 4 skipped (live-only). - **Mutants that still fail:** the anomaly on the modeled leg; STOP and SAFETY terminals; a non-interpreter name; the declared `archive` tool; non-empty text; a thought part; an extra part; an unobserved city; an extra arg; an extra call field; a second call; call and text merged into one chunk; an empty first chunk with the call and text in the terminal; the call and text in the first chunk with an empty terminal; the call in the terminal with the text in the first chunk. - **Positional check (review fix):** with the old position-blind check, the empty-first-chunk and call-plus-text-in-first-chunk mutants were accepted (2 failed). With the per-chunk check, all 102 pass, and removing any one positional check fails at least one test. All 161 live captures are still accepted. - **Mutation testing:** each new guard was removed in turn, and at least one test fails for each. - **Live** (`AIMOCK_VERTEX_LIVE=1`, ADC token, same project): the file passed 5 consecutive times, 103/103 each time. All 4 cases ran every time, with none UNATTEMPTED. The stream anomaly was accepted live in runs 3 and 4. After the positional fix, 2 more consecutive live passes ran (106/106 each, all 4 cases run), and the stream anomaly was accepted live in both. ## Gates `pnpm typecheck`, `pnpm lint`, `pnpm format:check`, `pnpm build` and `pnpm test` all pass (377 files, 14073 tests).
2 parents 6e2a4c7 + 97fda90 commit 1ba9750

1 file changed

Lines changed: 282 additions & 9 deletions

File tree

‎src/__tests__/drift/vertex-misbehavior.drift.ts‎

Lines changed: 282 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ interface RetainedCase {
2626
}
2727
const MODEL = "gemini-2.5-flash";
2828
const INTERPRETER_CODE = "print(default_api.archive(cities=['Tokyo";
29+
// Exact first-city variants of the native interpreter anomaly. Measured live 2026-10-11 over 120
30+
// k5 runs: 59 Tokyo, 3 London. The K5 prompt names no cities ("200 distinct full city names"), so
31+
// there is no request-derived set; the list is observation-only and unobserved cities still fail.
32+
const INTERPRETER_CODES = [INTERPRETER_CODE, "print(default_api.archive(cities=['London"];
2933
const MAX_BODY_BYTES = 256 * 1024;
3034

3135
function isObject(value: Json | undefined): value is JsonObject {
@@ -211,17 +215,38 @@ function compareVertexObservation(
211215
let classification: "TARGET_COMPARED" | "NOT_TRIGGERED";
212216
let reason: string;
213217
if (cell.id.startsWith("k5")) {
214-
const call = parts.length === 1 ? parts[0].functionCall : undefined;
218+
const call = parts.length > 0 ? parts[0].functionCall : undefined;
219+
// Object mode: the call is the only part. Stream mode splits it across two chunks: chunk 1
220+
// carries only the call, chunk 2 is the MAX_TOKENS terminal carrying exactly one empty,
221+
// non-thought text part (32/60 live k5-stream runs, 2026-10-11). Any other placement or extra
222+
// part stays a contradiction.
223+
const chunkParts = chunks.map((chunk) => {
224+
const first =
225+
isObject(chunk) && Array.isArray(chunk.candidates) ? chunk.candidates[0] : undefined;
226+
return isObject(first) && isObject(first.content) && Array.isArray(first.content.parts)
227+
? first.content.parts
228+
: [];
229+
});
230+
const anomalyParts = cell.stream
231+
? chunks.length === 2 &&
232+
// parts[0] (checked below as the interpreter call) is then chunk 1's only part.
233+
chunkParts[0].length === 1 &&
234+
chunkParts[1].length === 1 &&
235+
isObject(chunkParts[1][0]) &&
236+
Object.keys(chunkParts[1][0]).length === 1 &&
237+
chunkParts[1][0].text === ""
238+
: parts.length === 1;
215239
const anomaly =
216240
origin === "native" &&
217-
!cell.stream &&
241+
anomalyParts &&
218242
terminal === "MAX_TOKENS" &&
219243
isObject(call) &&
220244
call.name === "google:python_interpreter" &&
221245
Object.keys(call).length === 2 &&
222246
isObject(call.args) &&
223247
Object.keys(call.args).length === 1 &&
224-
call.args.code === INTERPRETER_CODE;
248+
typeof call.args.code === "string" &&
249+
INTERPRETER_CODES.includes(call.args.code);
225250
if (anomaly) {
226251
classification = "NOT_TRIGGERED";
227252
reason = "approved-interpreter-anomaly";
@@ -477,6 +502,219 @@ describe("Vertex native malformed-function terminal with empty content", () => {
477502
});
478503
});
479504

505+
// Byte-exact native k5-stream capture from Drift Tests run 38158387279 (2026-10-11), plus local
506+
// London-variant captures (2026-10-11). Response-only; no request headers or credentials.
507+
// Streaming splits the interpreter anomaly across two chunks: the call, then a MAX_TOKENS terminal
508+
// carrying one empty text part. Measured live (2026-10-11, exact drift request, n=60 per mode):
509+
// k5-stream 30 Tokyo + 2 London interpreter anomalies, 28 malformed-function terminals; k5-object
510+
// 29 Tokyo + 1 London interpreter anomalies, 30 malformed-function terminals.
511+
const interpreterStream: RetainedCase = {
512+
id: "k5-stream",
513+
stream: true,
514+
raw: 'data: {"candidates": [{"content": {"role": "model","parts": [{"functionCall": {"name": "google:python_interpreter","args": {"code": "print(default_api.archive(cities=[\'Tokyo"}}}]}}],"usageMetadata": {"trafficType": "ON_DEMAND"},"modelVersion": "gemini-2.5-flash","createTime": "2026-10-11T17:09:25.231112Z","responseId": "xcLLasiNDpTbgLUPwMDKgAk"}\r\n\r\ndata: {"candidates": [{"content": {"role": "model","parts": [{"text": ""}]},"finishReason": "MAX_TOKENS"}],"usageMetadata": {"promptTokenCount": 34,"candidatesTokenCount": 11,"totalTokenCount": 45,"trafficType": "ON_DEMAND","promptTokensDetails": [{"modality": "TEXT","tokenCount": 34}],"candidatesTokensDetails": [{"modality": "TEXT","tokenCount": 11}]},"modelVersion": "gemini-2.5-flash","createTime": "2026-10-11T17:09:25.231112Z","responseId": "xcLLasiNDpTbgLUPwMDKgAk"}\r\n\r\n',
515+
rawHash: "6c438263ff860f73364786968bb01fbd82afe7c6fdd06442035989017e7787a5",
516+
requestHash: "9a2018221c7e7d8776e40ea38a631585b00e705349d81052d98621d0bc1f3fba",
517+
};
518+
const interpreterStreamLondon: RetainedCase = {
519+
...interpreterStream,
520+
raw: 'data: {"candidates": [{"content": {"role": "model","parts": [{"functionCall": {"name": "google:python_interpreter","args": {"code": "print(default_api.archive(cities=[\'London"}}}]}}],"usageMetadata": {"trafficType": "ON_DEMAND"},"modelVersion": "gemini-2.5-flash","createTime": "2026-10-11T17:17:16.118803Z","responseId": "nMTLapOgB4Wi7rcPn6C4qA0"}\r\n\r\ndata: {"candidates": [{"content": {"role": "model","parts": [{"text": ""}]},"finishReason": "MAX_TOKENS"}],"usageMetadata": {"promptTokenCount": 34,"candidatesTokenCount": 11,"totalTokenCount": 45,"trafficType": "ON_DEMAND","promptTokensDetails": [{"modality": "TEXT","tokenCount": 34}],"candidatesTokensDetails": [{"modality": "TEXT","tokenCount": 11}]},"modelVersion": "gemini-2.5-flash","createTime": "2026-10-11T17:17:16.118803Z","responseId": "nMTLapOgB4Wi7rcPn6C4qA0"}\r\n\r\n',
521+
rawHash: "8b27e2f3ddf5003a4d2ed53c5861dabf99e0be0c303e786ebc898c37a5532a8d",
522+
};
523+
const interpreterObjectLondon: RetainedCase = {
524+
id: "k5-object",
525+
stream: false,
526+
raw: '{\n "candidates": [\n {\n "content": {\n "role": "model",\n "parts": [\n {\n "functionCall": {\n "name": "google:python_interpreter",\n "args": {\n "code": "print(default_api.archive(cities=[\'London"\n }\n }\n }\n ]\n },\n "finishReason": "MAX_TOKENS"\n }\n ],\n "usageMetadata": {\n "promptTokenCount": 34,\n "candidatesTokenCount": 11,\n "totalTokenCount": 45,\n "trafficType": "ON_DEMAND",\n "promptTokensDetails": [\n {\n "modality": "TEXT",\n "tokenCount": 34\n }\n ],\n "candidatesTokensDetails": [\n {\n "modality": "TEXT",\n "tokenCount": 11\n }\n ]\n },\n "modelVersion": "gemini-2.5-flash",\n "createTime": "2026-10-11T17:19:41.490500Z",\n "responseId": "LcXLaoT4Hf-EufoPrL-tgA4"\n}\n',
527+
rawHash: "d45618d070ebbb49fa9fc6c5e2edf600bff97aac474645a60161b411e5bf30b1",
528+
requestHash: "9a2018221c7e7d8776e40ea38a631585b00e705349d81052d98621d0bc1f3fba",
529+
};
530+
function editInterpreterStream(edit: (chunks: JsonObject[]) => void) {
531+
return derivative(interpreterStream, edit);
532+
}
533+
534+
describe("Vertex native interpreter anomaly in stream mode and its first-city variants", () => {
535+
it.each([interpreterStream, interpreterStreamLondon, interpreterObjectLondon])(
536+
"classifies captured $id $rawHash as the approved interpreter anomaly",
537+
async (cell) => {
538+
expect(sha256(cell.raw)).toBe(cell.rawHash);
539+
const { observation, decoded } = await replay(cell);
540+
const result = compareVertexObservation(cell, observation, decoded, "native");
541+
expect(result.classification).toBe("NOT_TRIGGERED");
542+
expect(result.reason).toBe("approved-interpreter-anomaly");
543+
expect(result.terminal).toBe("MAX_TOKENS");
544+
expect(result.rawHash).toBe(cell.rawHash);
545+
},
546+
);
547+
548+
it.each([interpreterStream, interpreterStreamLondon, interpreterObjectLondon])(
549+
"rejects captured $id $rawHash on the modeled leg",
550+
async (cell) => {
551+
const { observation, decoded } = await replay(cell);
552+
expect(() => compareVertexObservation(cell, observation, decoded, "modeled")).toThrow(
553+
/contradictory-target/,
554+
);
555+
},
556+
);
557+
558+
it.each<{ name: string; edit: (chunks: JsonObject[]) => void; error: RegExp }>([
559+
{
560+
name: "a STOP terminal",
561+
edit: (chunks) => {
562+
candidate(chunks[1]).finishReason = "STOP";
563+
},
564+
error: /unexpected archive tool schema/,
565+
},
566+
{
567+
name: "a SAFETY terminal",
568+
edit: (chunks) => {
569+
candidate(chunks[1]).finishReason = "SAFETY";
570+
},
571+
error: /unapproved K5 outcome/,
572+
},
573+
{
574+
name: "a non-interpreter function name",
575+
edit: (chunks) => {
576+
interpreter(chunks).name = "other_interpreter";
577+
},
578+
error: /contradictory-target/,
579+
},
580+
{
581+
name: "the declared archive tool",
582+
edit: (chunks) => {
583+
const call = interpreter(chunks);
584+
call.name = "archive";
585+
call.args = { cities: ["Tokyo"] };
586+
},
587+
error: /contradictory-target/,
588+
},
589+
{
590+
name: "non-empty terminal text",
591+
edit: (chunks) => {
592+
contentParts(chunks[1])[0] = { text: "x" };
593+
},
594+
error: /contradictory-target/,
595+
},
596+
{
597+
name: "a thought terminal part",
598+
edit: (chunks) => {
599+
contentParts(chunks[1])[0] = { text: "", thought: true };
600+
},
601+
error: /contradictory-target/,
602+
},
603+
{
604+
name: "an extra terminal part",
605+
edit: (chunks) => {
606+
contentParts(chunks[1]).push({ text: "" });
607+
},
608+
error: /contradictory-target/,
609+
},
610+
{
611+
name: "an unobserved first city",
612+
edit: (chunks) => {
613+
interpreter(chunks).args = { code: "print(default_api.archive(cities=['Paris" };
614+
},
615+
error: /contradictory-target/,
616+
},
617+
{
618+
name: "an extra interpreter argument",
619+
edit: (chunks) => {
620+
interpreter(chunks).args = { code: INTERPRETER_CODE, extra: 1 };
621+
},
622+
error: /contradictory-target/,
623+
},
624+
{
625+
name: "an extra call field",
626+
edit: (chunks) => {
627+
interpreter(chunks).id = "new-field";
628+
},
629+
error: /contradictory-target/,
630+
},
631+
{
632+
name: "the call and empty text merged into one chunk",
633+
edit: (chunks) => {
634+
contentParts(chunks[0]).push({ text: "" });
635+
candidate(chunks[0]).finishReason = "MAX_TOKENS";
636+
chunks[0].usageMetadata = chunks[1].usageMetadata;
637+
chunks.splice(1);
638+
},
639+
error: /contradictory-target/,
640+
},
641+
{
642+
name: "an empty first chunk and the call plus text in the terminal",
643+
edit: (chunks) => {
644+
const call = contentParts(chunks[0]).splice(0);
645+
contentParts(chunks[1]).unshift(...call);
646+
},
647+
error: /contradictory-target/,
648+
},
649+
{
650+
name: "the call and text in the first chunk and an empty terminal",
651+
edit: (chunks) => {
652+
contentParts(chunks[0]).push(...contentParts(chunks[1]).splice(0));
653+
},
654+
error: /contradictory-target/,
655+
},
656+
{
657+
name: "the call in the terminal and the text in the first chunk",
658+
edit: (chunks) => {
659+
const call = contentParts(chunks[0]).splice(0);
660+
contentParts(chunks[0]).push(...contentParts(chunks[1]).splice(0));
661+
contentParts(chunks[1]).push(...call);
662+
},
663+
error: /contradictory-target/,
664+
},
665+
{
666+
name: "a second interpreter call",
667+
edit: (chunks) => {
668+
contentParts(chunks[0]).push({
669+
functionCall: { name: "google:python_interpreter", args: { code: INTERPRETER_CODE } },
670+
});
671+
},
672+
error: /contradictory-target/,
673+
},
674+
])("rejects the stream anomaly with $name", async ({ edit, error }) => {
675+
const raw = editInterpreterStream(edit);
676+
const { observation, decoded } = await replay(interpreterStream, raw);
677+
expect(() =>
678+
compareVertexObservation(interpreterStream, observation, decoded, "native"),
679+
).toThrow(error);
680+
});
681+
682+
it("rejects the stream anomaly's empty text part in object mode", async () => {
683+
const cell = retained[0];
684+
const raw = derivative(cell, (chunks) => {
685+
contentParts(chunks[0]).push({ text: "" });
686+
});
687+
const { observation, decoded } = await replay(cell, raw);
688+
expect(() => compareVertexObservation(cell, observation, decoded, "native")).toThrow(
689+
/contradictory-target/,
690+
);
691+
});
692+
693+
it("recurrence continues past the captured stream anomaly", async () => {
694+
await withVertexSequence(
695+
async (origin, requests) => {
696+
const report = await runVertexRecurrence(localVertexEnvironment, origin);
697+
expect(requests).toHaveLength(4);
698+
expect(report.results[1]).toMatchObject({
699+
cell: "k5-stream",
700+
classification: "NOT_TRIGGERED",
701+
reason: "approved-interpreter-anomaly",
702+
rawHash: interpreterStream.rawHash,
703+
});
704+
expect(report.results.map((result) => result.classification)).toEqual([
705+
"NOT_TRIGGERED",
706+
"NOT_TRIGGERED",
707+
"NOT_TRIGGERED",
708+
"NOT_TRIGGERED",
709+
]);
710+
},
711+
-1,
712+
undefined,
713+
[retained[0], interpreterStream, ...retained.slice(2)],
714+
);
715+
});
716+
});
717+
480718
function derivative(cell: RetainedCase, edit: (chunks: JsonObject[]) => void) {
481719
const chunks = readChunks(
482720
{
@@ -1307,7 +1545,12 @@ async function runVertexRecurrence(env: VertexEnvironment, origin = VERTEX_ORIGI
13071545
complete: observation.complete,
13081546
};
13091547
} catch {
1310-
budget.stop();
1548+
// A complete HTTP 200 the comparator rejected is a per-case finding: record it and keep
1549+
// measuring the remaining cases so one rejection does not hide them. HTTP, transport and
1550+
// byte-bound failures (auth, quota, outage) still stop the run before another send.
1551+
const rejectedOnly =
1552+
observation !== undefined && observation.status === 200 && observation.complete;
1553+
if (!rejectedOnly) budget.stop();
13111554
results[index] = {
13121555
...results[index],
13131556
classification: "FAILURE",
@@ -1321,7 +1564,7 @@ async function runVertexRecurrence(env: VertexEnvironment, origin = VERTEX_ORIGI
13211564
observation?.error ??
13221565
(observation?.status !== 200 ? "HTTP_FAILURE" : "WIRE_OR_SDK_REJECTED"),
13231566
};
1324-
break;
1567+
if (!rejectedOnly) break;
13251568
}
13261569
}
13271570
return {
@@ -1639,22 +1882,52 @@ describe.skipIf(!vertexLiveRequested)(
16391882
},
16401883
);
16411884

1885+
it("Vertex recurrence still stops on a truncated HTTP 200 transport", async () => {
1886+
await withVertexSequence(
1887+
async (origin, requests) => {
1888+
const report = await runVertexRecurrence(localVertexEnvironment, origin);
1889+
expect(requests).toHaveLength(1);
1890+
expect(report.results[0]).toMatchObject({
1891+
classification: "FAILURE",
1892+
status: 200,
1893+
complete: false,
1894+
reason: "TRANSPORT_FAILURE",
1895+
});
1896+
expect(report.results.map((result) => result.classification)).toEqual([
1897+
"FAILURE",
1898+
"UNATTEMPTED",
1899+
"UNATTEMPTED",
1900+
"UNATTEMPTED",
1901+
]);
1902+
expect(report.budget).toEqual({ attempts: 1, tokens: 16, blocked: true });
1903+
},
1904+
-1,
1905+
"cutoff",
1906+
);
1907+
});
1908+
16421909
it("Vertex recurring contradiction preserves actual SDK terminal and usage in failure receipt", async () => {
16431910
await withVertexSequence(
16441911
async (origin, requests) => {
16451912
const report = await runVertexRecurrence(localVertexEnvironment, origin);
16461913
console.info(JSON.stringify({ proof: "Vertex recurrence contradiction fields", report }));
1647-
expect(requests).toHaveLength(1);
1914+
// A comparator rejection of a complete HTTP 200 no longer hides the later cases.
1915+
expect(requests).toHaveLength(4);
16481916
expect(report.results[0]).toMatchObject({
16491917
classification: "FAILURE",
16501918
sdk: "DECODED",
16511919
terminal: "MAX_TOKENS",
16521920
usage: { totalTokenCount: 45 },
1921+
reason: "WIRE_OR_SDK_REJECTED",
16531922
});
16541923
expect(report.results[0].chunks).toHaveLength(1);
1655-
expect(
1656-
report.results.slice(1).every((result) => result.classification === "UNATTEMPTED"),
1657-
).toBe(true);
1924+
expect(report.results.map((result) => result.classification)).toEqual([
1925+
"FAILURE",
1926+
"NOT_TRIGGERED",
1927+
"NOT_TRIGGERED",
1928+
"NOT_TRIGGERED",
1929+
]);
1930+
expect(report.budget).toEqual({ attempts: 4, tokens: 160, blocked: false });
16581931
},
16591932
-1,
16601933
"contradiction",

0 commit comments

Comments
 (0)