Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

test: close item 9's array/object coercion and item 12's positive path

Jack Rudenko0ac184aaa4c5
diff --git a/packages/cli/src/handlers/shared/schema-validate.test.ts b/packages/cli/src/handlers/shared/schema-validate.test.ts
index 8361e7ce..550e5d64 100644
--- a/packages/cli/src/handlers/shared/schema-validate.test.ts
+++ b/packages/cli/src/handlers/shared/schema-validate.test.ts
@@ -90,6 +90,90 @@ describe("coerceToSchema: declared types, and a failure keeps the value", () =>
});
});
+/**
+ * The `array` and `object` branches, which shipped untested.
+ *
+ * They are the branches the `<function=NAME><parameter=P>` envelope needs most:
+ * that wire has no types at all, so a tool declaring `string[]` receives the
+ * JSON text `'["a","b"]'` and a tool declaring an object receives `'{"a":1}'`.
+ * Everything here is a call on this tree's own function with argument values —
+ * no provider bytes, no fixture.
+ */
+describe("coerceToSchema: array and object", () => {
+ const schema = {
+ type: "object",
+ properties: {
+ tags: { type: "array", items: { type: "string" } },
+ sizes: { type: "array", items: { type: "number" } },
+ anything: { type: "array" },
+ options: { type: "object" },
+ },
+ };
+
+ test("a JSON array string becomes an array", () => {
+ expect(coerceToSchema(schema, { tags: '["a","b"]' }).args).toEqual({ tags: ["a", "b"] });
+ });
+
+ test("the declared items type is applied to each element", () => {
+ expect(coerceToSchema(schema, { sizes: '["1","2.5"]' }).args).toEqual({ sizes: [1, 2.5] });
+ });
+
+ test("an element that cannot be coerced keeps its own value, and its siblings still convert", () => {
+ // A failed element is never dropped and never guessed at: the harness
+ // re-validates the call, so a wrong type is visible while a missing one is not.
+ expect(coerceToSchema(schema, { sizes: '["1","later"]' }).args).toEqual({
+ sizes: [1, "later"],
+ });
+ });
+
+ test("an array with no declared items is parsed but not touched element-wise", () => {
+ expect(coerceToSchema(schema, { anything: '["1",true,null]' }).args).toEqual({
+ anything: ["1", true, null],
+ });
+ });
+
+ test("a JSON object string is NOT accepted for an array field", () => {
+ // `parseJsonOfKind` keeps the parse only if it decoded to the expected kind.
+ // Without that check a tool declaring a list would receive a map.
+ expect(coerceToSchema(schema, { tags: '{"a":1}' }).args).toEqual({ tags: '{"a":1}' });
+ });
+
+ test("a JSON object string becomes an object", () => {
+ expect(coerceToSchema(schema, { options: '{"depth":2}' }).args).toEqual({
+ options: { depth: 2 },
+ });
+ });
+
+ test("a JSON array string is NOT accepted for an object field", () => {
+ expect(coerceToSchema(schema, { options: "[1,2]" }).args).toEqual({ options: "[1,2]" });
+ });
+
+ test("a string that is not JSON at all keeps its value", () => {
+ expect(coerceToSchema(schema, { tags: "a, b", options: "depth=2" }).args).toEqual({
+ tags: "a, b",
+ options: "depth=2",
+ });
+ });
+
+ test("a bare scalar is not wrapped into a one-element array", () => {
+ // Wrapping would be inventing a shape the model did not write.
+ expect(coerceToSchema(schema, { tags: "a" }).args).toEqual({ tags: "a" });
+ });
+
+ test("a value already of the declared kind is left alone, elements included", () => {
+ // The top-level type already matches, so the branch never runs — the
+ // element-wise pass exists for values parsed OUT of a string, not for
+ // re-typing an array a structured wire already delivered.
+ const args = { sizes: ["1", "2"] };
+ expect(coerceToSchema(schema, args).args).toBe(args);
+ });
+
+ test("the coerced-key list names exactly the keys that changed", () => {
+ const result = coerceToSchema(schema, { tags: '["a"]', options: "not json" });
+ expect(result.coerced).toEqual(["tags"]);
+ });
+});
+
describe("applySchemaDefaults: the schema's own value, for required keys only", () => {
const schema = {
type: "object",
diff --git a/packages/cli/src/handlers/shared/stream-parsers/contentless-turn.test.ts b/packages/cli/src/handlers/shared/stream-parsers/contentless-turn.test.ts
new file mode 100644
index 00000000..564416ac
--- /dev/null
+++ b/packages/cli/src/handlers/shared/stream-parsers/contentless-turn.test.ts
@@ -0,0 +1,189 @@
+/**
+ * Item 12 — a turn that ends NORMALLY always carries at least one content block.
+ *
+ * `stop_reason: "end_turn"` with an empty `content` array is not a shape
+ * Anthropic's API produces. A client that indexes the last block, or simply
+ * renders the turn, gets a success that delivered nothing and no diagnostic.
+ * `openai-sse.ts` `finalize()` therefore emits ONE empty text block in exactly
+ * that case.
+ *
+ * ## The gap this closes
+ *
+ * The item shipped with its NON-firing half covered (the `max_tokens` capture
+ * must not be papered over) and its POSITIVE half declared untested: across
+ * eight live runs no model produced a `stop`-terminated turn with no content at
+ * all, which is unsurprising — it is a provider malfunction, not a behaviour you
+ * can ask for.
+ *
+ * ## Provenance
+ *
+ * No `.sse` fixture was written. The contentless streams here are real captures
+ * REPLAYED WITH RECORDS REMOVED — the same deletion-only method
+ * `ending-taxonomy.test.ts` uses for a dead socket, and for the same reason: a
+ * turn with nothing in it is a capture with its content records missing, which
+ * needs no invention. Nothing is added, reordered or rewritten.
+ */
+
+import { describe, expect, test } from "bun:test";
+import { readFileSync } from "node:fs";
+import { dirname, join } from "node:path";
+import { fileURLToPath } from "node:url";
+import { DefaultAPIFormat } from "../../../adapters/base-api-format.js";
+import { createStreamingResponseHandler } from "./openai-sse.js";
+
+const FIXTURES_DIR = join(
+ dirname(fileURLToPath(import.meta.url)),
+ "..",
+ "..",
+ "..",
+ "test-fixtures",
+ "sse-responses"
+);
+
+const SCHEMAS = [
+ {
+ name: "Read",
+ input_schema: {
+ type: "object",
+ properties: { file_path: { type: "string" } },
+ required: ["file_path"],
+ },
+ },
+];
+
+/** The capture's records, in order, exactly as captured. */
+function records(file: string): string[] {
+ return readFileSync(join(FIXTURES_DIR, file), "utf-8")
+ .split("\n\n")
+ .map((r) => r.trim())
+ .filter((r) => r.length > 0);
+}
+
+const body = (recs: string[]) => `${recs.join("\n\n")}\n\n`;
+
+/** A real capture with the records at `drop` deleted — nothing else changed. */
+function without(file: string, drop: number[]): string {
+ return body(records(file).filter((_, i) => !drop.includes(i)));
+}
+
+async function replay(text: string, toolSchemas?: any[]) {
+ const response = createStreamingResponseHandler(
+ { body: (s: ReadableStream, i?: any) => new Response(s, i) } as any,
+ new Response(
+ new ReadableStream({
+ start(c) {
+ c.enqueue(new TextEncoder().encode(text));
+ c.close();
+ },
+ }),
+ { status: 200, headers: { "Content-Type": "text/event-stream" } }
+ ),
+ new DefaultAPIFormat("test-model") as any,
+ "test-model",
+ null,
+ undefined,
+ toolSchemas
+ );
+
+ const events: any[] = [];
+ const reader = response.body!.getReader();
+ const decoder = new TextDecoder();
+ let buffer = "";
+ while (true) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ buffer += decoder.decode(value, { stream: true });
+ const parts = buffer.split("\n\n");
+ buffer = parts.pop() || "";
+ for (const part of parts) {
+ let dataStr = "";
+ for (const line of part.split("\n").filter((l) => l.trim())) {
+ if (line.startsWith("data: ")) dataStr += line.slice(6);
+ }
+ if (!dataStr || dataStr === "[DONE]") continue;
+ try {
+ events.push(JSON.parse(dataStr));
+ } catch {}
+ }
+ }
+
+ const starts = events.filter((e) => e.type === "content_block_start");
+ return {
+ stopReason: events.find((e) => e.type === "message_delta")?.delta?.stop_reason ?? null,
+ messageStop: events.some((e) => e.type === "message_stop"),
+ blockKinds: starts.map((e) => e.content_block?.type),
+ blockStarts: starts.length,
+ blockStops: events.filter((e) => e.type === "content_block_stop").length,
+ text: events
+ .filter((e) => e.type === "content_block_delta" && e.delta?.type === "text_delta")
+ .map((e) => e.delta.text)
+ .join(""),
+ };
+}
+
+describe("the premise: the captures are the length this file assumes", () => {
+ test("a record that moved would slide every deletion below onto another boundary", () => {
+ expect(records("SEED-openai-text-only.sse").length).toBe(6);
+ expect(records("SEED-openai-tool-call.sse").length).toBe(6);
+ expect(records("gemini-3.1-pro-or-maxtokens-empty.sse").length).toBe(13);
+ });
+});
+
+describe("item 12 FIRES: end_turn with nothing produced", () => {
+ test("a stop-terminated turn whose content records are gone gets one empty text block", async () => {
+ // Records 1-3 are the three `delta.content` chunks. What remains is the
+ // capture's own role-only opener, its own `finish_reason: "stop"` record
+ // and its own `[DONE]` — a provider that said it finished and sent nothing.
+ const r = await replay(without("SEED-openai-text-only.sse", [1, 2, 3]));
+ expect(r.stopReason).toBe("end_turn");
+ expect(r.blockKinds).toEqual(["text"]);
+ expect(r.blockStarts).toBe(1);
+ expect(r.blockStops).toBe(1);
+ // EMPTY, not placeholder prose: prose would enter the conversation history
+ // as the assistant's words and be replayed forever.
+ expect(r.text).toBe("");
+ expect(r.messageStop).toBe(true);
+ });
+
+ test("a turn that ends with no finish_reason and no content gets the same block", async () => {
+ // Only the role-only opener survives: nothing was produced, so this is not a
+ // truncation — there is nothing to have been cut off.
+ const r = await replay(body(records("SEED-openai-text-only.sse").slice(0, 1)));
+ expect(r.stopReason).toBe("end_turn");
+ expect(r.blockKinds).toEqual(["text"]);
+ expect(r.text).toBe("");
+ });
+});
+
+describe("item 12 does NOT fire where emptiness is meaningful", () => {
+ test("a complete turn keeps its one real text block and gains nothing", async () => {
+ const r = await replay(body(records("SEED-openai-text-only.sse")));
+ expect(r.stopReason).toBe("end_turn");
+ expect(r.blockKinds).toEqual(["text"]);
+ expect(r.text).toBe("Hello, I'm a test model.");
+ });
+
+ test("a contentless max_tokens turn stays contentless", async () => {
+ // A real capture of reasoning consuming the entire budget. The emptiness IS
+ // the answer here and must not be papered over.
+ const r = await replay(body(records("gemini-3.1-pro-or-maxtokens-empty.sse")));
+ expect(r.stopReason).toBe("max_tokens");
+ expect(r.blockKinds).not.toContain("text");
+ });
+
+ test("a tool-only turn gains no empty text block", async () => {
+ // Record 1 is this capture's only prose. With it deleted the turn emits a
+ // tool block and nothing else — a tool block IS content, so the guard must
+ // stay silent.
+ const r = await replay(without("SEED-openai-tool-call.sse", [1]), SCHEMAS);
+ expect(r.stopReason).toBe("tool_use");
+ expect(r.blockKinds).toEqual(["tool_use"]);
+ });
+
+ test("a turn that spoke AND called a tool keeps exactly its own two blocks", async () => {
+ const r = await replay(body(records("SEED-openai-tool-call.sse")), SCHEMAS);
+ expect(r.stopReason).toBe("tool_use");
+ expect(r.blockKinds).toEqual(["text", "tool_use"]);
+ expect(r.text).toBe("Let me read that file.");
+ });
+});