Skip to content

Commit 9420afa

Browse files
authored
test: accept native Vertex malformed-function terminal with empty content (#524)
## Problem The Drift Tests run [38104122278](https://github.com/CopilotKit/aimock/actions/runs/38104122278) on `main` @ b9cff07 failed the live Vertex case `k5-object` (`expected [ 'TARGET_COMPARED', 'NOT_TRIGGERED' ] to include 'FAILURE'`). Because of that, `k5-stream`, `k9-object` and `k9-stream` were not attempted (`earlier-case-failed`). Vertex returned HTTP 200 from `gemini-2.5-flash`: ``` "candidates":[{"content":{},"finishReason":"MALFORMED_FUNCTION_CALL","finishMessage":"Malformed function call: print(default_api.archive(cities=['Tokyo"}] ``` The comparator already approves a `MALFORMED_FUNCTION_CALL` terminal as `NOT_TRIGGERED` (reason `malformed-function-terminal`). The retained `k5-stream` capture passes this way. The difference is the shape: in object mode Vertex sent an **empty** `content: {}` object, while the stream capture has no `content` key. Any `content` present had to have `role: "model"` plus a `parts` array, so the check threw `invalid content`. This is a deterministic shape-handling bug in the test comparator, not a new upstream behavior class. ## Change (test-only; no `src/` change) `src/__tests__/drift/vertex-misbehavior.drift.ts`: - On the **native** leg only, when the candidate's `finishReason` is `MALFORMED_FUNCTION_CALL`, an empty `content` object (no keys) is treated the same as absent content. - This covers **object and stream mode on purpose**: an empty `content` means no content in either mode. - Any other content still needs `role: "model"` plus a `parts` array. The modeled (aimock) leg stays strict. - The CI capture is retained byte-exact as a fixture with its sha256 (`4c38ba91…477eb`), with the same request hash as the existing k5 captures. - `withVertexSequence` takes an optional list of served captures, so the recurrence test can serve the CI capture first. ## Local red/green Real surface: the captured raw bytes are replayed through the actual `@google/genai` SDK over an owned localhost HTTP server, then through `compareVertexObservation`. Separately, the CI capture runs through `runVertexRecurrence`, the live path that failed in run 38104122278, against a localhost origin. **RED** (new tests, with the comparator's content check restored to the b9cff07 line): ``` × ... classifies the captured empty-content object as an approved non-trigger AssertionError: invalid content × ... rejects the captured malformed-function terminal without a finish message (got 'invalid content' before reaching the terminal check) × ... accepts empty content with a malformed-function terminal in stream mode AssertionError: invalid content × Vertex recurrence continues past the captured empty-content malformed-function terminal → expected [ … ] to have a length of 4 but got 1 (k5-object FAILURE; later cases earlier-case-failed) Tests 4 failed | 6 passed | 72 skipped (82) ``` **GREEN** (after fix): ``` ✓ Vertex retained native dispositions > 'k5-object' | 'k5-stream' | 'k9-object' | 'k9-stream' preserve genuine wire bytes and approved non-trigger ✓ ... classifies the captured empty-content object as an approved non-trigger (NOT_TRIGGERED / malformed-function-terminal) ✓ ... accepts empty content with a malformed-function terminal in stream mode (NOT_TRIGGERED / malformed-function-terminal) ✓ ... rejects the same empty content on the modeled leg ✓ ... rejects malformed-function content with 'non-model role' ({role:"user"}) ✓ ... rejects malformed-function content with 'non-array parts' ({role:"model", parts:"x"}) ✓ ... rejects malformed-function content with 'missing parts' ({role:"model"}) ✓ ... rejects empty content with a STOP terminal ✓ ... rejects empty content with a MAX_TOKENS terminal ✓ ... rejects the captured malformed-function terminal without a finish message (invalid malformed-function terminal) ✓ Vertex recurrence continues past the captured empty-content malformed-function terminal (4 requests; all four NOT_TRIGGERED; no earlier-case-failed) Whole file: Tests 78 passed | 4 skipped (82) (the 4 skipped are the live-gated cases) ``` **Mutation check:** I replaced each guard clause with `true`, one at a time, and each mutant was caught: - dropping `origin === "native"` fails the modeled-leg test - dropping the `MALFORMED_FUNCTION_CALL` check fails the STOP and MAX_TOKENS tests - dropping the empty-object check fails the role and parts tests - dropping the existing `Array.isArray(content.parts)` check fails the 'non-array parts' and 'missing parts' tests ## Live verification: not run locally **I could not run Vertex live locally.** CI gets its token through GitHub workload identity for the `main` schedule/dispatch runs only. The local gcloud credentials need interactive re-authentication, and no Vertex credential is in the local secret store. The live confirmation is a manual Drift Tests run on `main` after merge. ## Gates `pnpm typecheck`, `pnpm lint`, `pnpm format:check`, `pnpm build`: pass. `pnpm test`: 377 files / 14073 passed, 3 skipped.
2 parents b9cff07 + 1dab38b commit 9420afa

1 file changed

Lines changed: 118 additions & 2 deletions

File tree

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

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,15 @@ function compareVertexObservation(
168168
);
169169
if (candidate.finishMessage !== undefined)
170170
assert(typeof candidate.finishMessage === "string", "invalid finish message");
171-
if (candidate.content !== undefined) {
171+
// Native malformed-function terminals may carry `content: {}` (no keys) instead of omitting
172+
// content; treat exactly that as absent. Object and stream modes are both covered on purpose:
173+
// empty content means no content in either mode. Any other content stays strict.
174+
const emptyMalformedContent =
175+
origin === "native" &&
176+
candidate.finishReason === "MALFORMED_FUNCTION_CALL" &&
177+
isObject(candidate.content) &&
178+
Object.keys(candidate.content).length === 0;
179+
if (candidate.content !== undefined && !emptyMalformedContent) {
172180
assert(
173181
isObject(candidate.content) &&
174182
candidate.content.role === "model" &&
@@ -389,6 +397,86 @@ describe("Vertex retained native dispositions (offline SDK/HTTP replay)", () =>
389397
});
390398
});
391399

400+
// Byte-exact native k5-object capture from Drift Tests run 38104122278 (2026-10-11): in object
401+
// mode Vertex served the malformed-function terminal with an empty `content: {}` object instead of
402+
// omitting content (as the retained k5-stream capture does). Response-only; no request headers.
403+
const malformedEmptyContent: RetainedCase = {
404+
id: "k5-object",
405+
stream: false,
406+
raw: '{\n "candidates": [\n {\n "content": {},\n "finishReason": "MALFORMED_FUNCTION_CALL",\n "finishMessage": "Malformed function call: print(default_api.archive(cities=[\'Tokyo"\n }\n ],\n "usageMetadata": {\n "promptTokenCount": 34,\n "totalTokenCount": 34,\n "trafficType": "ON_DEMAND",\n "promptTokensDetails": [\n {\n "modality": "TEXT",\n "tokenCount": 34\n }\n ]\n },\n "modelVersion": "gemini-2.5-flash",\n "createTime": "2026-10-11T02:08:24.591372Z",\n "responseId": "mO_KaoyMJNLygLUPqbWDoAs"\n}\n',
407+
rawHash: "4c38ba91587ac63d5cdf2c25209743d1670fd67dc01b53b2b6d93aa0948477eb",
408+
requestHash: "9a2018221c7e7d8776e40ea38a631585b00e705349d81052d98621d0bc1f3fba",
409+
};
410+
function editMalformedEmptyContent(edit: (candidate: JsonObject) => void) {
411+
const chunk = parseJson(malformedEmptyContent.raw);
412+
assert(isObject(chunk) && Array.isArray(chunk.candidates) && isObject(chunk.candidates[0]));
413+
edit(chunk.candidates[0]);
414+
return JSON.stringify(chunk);
415+
}
416+
417+
describe("Vertex native malformed-function terminal with empty content", () => {
418+
it("classifies the captured empty-content object as an approved non-trigger", async () => {
419+
expect(sha256(malformedEmptyContent.raw)).toBe(malformedEmptyContent.rawHash);
420+
const { observation, decoded } = await replay(malformedEmptyContent);
421+
const result = compareVertexObservation(malformedEmptyContent, observation, decoded, "native");
422+
expect(result.classification).toBe("NOT_TRIGGERED");
423+
expect(result.reason).toBe("malformed-function-terminal");
424+
expect(result.terminal).toBe("MALFORMED_FUNCTION_CALL");
425+
expect(result.rawHash).toBe(malformedEmptyContent.rawHash);
426+
});
427+
428+
it("rejects the same empty content on the modeled leg", async () => {
429+
const { observation, decoded } = await replay(malformedEmptyContent);
430+
expect(() =>
431+
compareVertexObservation(malformedEmptyContent, observation, decoded, "modeled"),
432+
).toThrow(/invalid content/);
433+
});
434+
435+
it.each<{ name: string; content: JsonObject }>([
436+
{ name: "non-model role", content: { role: "user" } },
437+
{ name: "non-array parts", content: { role: "model", parts: "x" } },
438+
{ name: "missing parts", content: { role: "model" } },
439+
])("rejects malformed-function content with $name", async ({ content }) => {
440+
const raw = editMalformedEmptyContent((candidate) => {
441+
candidate.content = content;
442+
});
443+
const { observation, decoded } = await replay(malformedEmptyContent, raw);
444+
expect(() =>
445+
compareVertexObservation(malformedEmptyContent, observation, decoded, "native"),
446+
).toThrow(/invalid content/);
447+
});
448+
449+
it.each(["STOP", "MAX_TOKENS"])("rejects empty content with a %s terminal", async (reason) => {
450+
const raw = editMalformedEmptyContent((candidate) => {
451+
candidate.finishReason = reason;
452+
});
453+
const { observation, decoded } = await replay(malformedEmptyContent, raw);
454+
expect(() =>
455+
compareVertexObservation(malformedEmptyContent, observation, decoded, "native"),
456+
).toThrow(/invalid content/);
457+
});
458+
459+
it("rejects the captured malformed-function terminal without a finish message", async () => {
460+
const raw = editMalformedEmptyContent((candidate) => {
461+
delete candidate.finishMessage;
462+
});
463+
const { observation, decoded } = await replay(malformedEmptyContent, raw);
464+
expect(() =>
465+
compareVertexObservation(malformedEmptyContent, observation, decoded, "native"),
466+
).toThrow(/invalid malformed-function terminal/);
467+
});
468+
469+
it("accepts empty content with a malformed-function terminal in stream mode", async () => {
470+
const cell: RetainedCase = { ...malformedEmptyContent, id: "k5-stream", stream: true };
471+
const raw = `data: ${editMalformedEmptyContent(() => {})}\r\n\r\n`;
472+
const { observation, decoded } = await replay(cell, raw);
473+
const result = compareVertexObservation(cell, observation, decoded, "native");
474+
expect(result.classification).toBe("NOT_TRIGGERED");
475+
expect(result.reason).toBe("malformed-function-terminal");
476+
expect(result.chunks).toHaveLength(1);
477+
});
478+
});
479+
392480
function derivative(cell: RetainedCase, edit: (chunks: JsonObject[]) => void) {
393481
const chunks = readChunks(
394482
{
@@ -1247,14 +1335,15 @@ async function withVertexSequence(
12471335
run: (origin: string, requests: { path: string; body: string }[]) => Promise<void>,
12481336
failAt = -1,
12491337
transportFault?: "redirect" | "cutoff" | "overflow" | "stall" | "contradiction",
1338+
cells: RetainedCase[] = retained,
12501339
) {
12511340
const requests: { path: string; body: string }[] = [];
12521341
const server = createServer(async (req, res) => {
12531342
const chunks: Buffer[] = [];
12541343
for await (const chunk of req) chunks.push(Buffer.from(chunk));
12551344
const index = requests.length;
12561345
requests.push({ path: req.url ?? "", body: Buffer.concat(chunks).toString("utf8") });
1257-
const cell = retained[index % retained.length];
1346+
const cell = cells[index % cells.length];
12581347
let raw = Buffer.from(cell.raw);
12591348
if (transportFault === "contradiction")
12601349
raw = Buffer.from(cell.raw.replace("google:python_interpreter", "unapproved_interpreter"));
@@ -1571,3 +1660,30 @@ it("Vertex recurring contradiction preserves actual SDK terminal and usage in fa
15711660
"contradiction",
15721661
);
15731662
});
1663+
1664+
it("Vertex recurrence continues past the captured empty-content malformed-function terminal", async () => {
1665+
await withVertexSequence(
1666+
async (origin, requests) => {
1667+
const report = await runVertexRecurrence(localVertexEnvironment, origin);
1668+
console.info(JSON.stringify({ proof: "Vertex recurrence empty-content malformed", report }));
1669+
expect(requests).toHaveLength(4);
1670+
expect(report.results[0]).toMatchObject({
1671+
cell: "k5-object",
1672+
classification: "NOT_TRIGGERED",
1673+
reason: "malformed-function-terminal",
1674+
terminal: "MALFORMED_FUNCTION_CALL",
1675+
rawHash: malformedEmptyContent.rawHash,
1676+
});
1677+
expect(report.results.map((result) => result.classification)).toEqual([
1678+
"NOT_TRIGGERED",
1679+
"NOT_TRIGGERED",
1680+
"NOT_TRIGGERED",
1681+
"NOT_TRIGGERED",
1682+
]);
1683+
expect(report.results.some((result) => result.reason === "earlier-case-failed")).toBe(false);
1684+
},
1685+
-1,
1686+
undefined,
1687+
[malformedEmptyContent, ...retained.slice(1)],
1688+
);
1689+
});

0 commit comments

Comments
 (0)