mirror of
https://github.com/ranxianglei/billion-context.git
synced 2026-12-04 01:58:07 +08:00
The cache_creation breakdown object (ephemeral_5m_input_tokens /
ephemeral_1h_input_tokens) lives only in the upstream message_start frame,
which #1315 forwards usage-neutral to keep the synthetic terminal the sole
usage authority. The breakdown had no other carrier, so hosts deriving
their prompt-cache TTL from it (Claude Code VS Code countdown icon) lost
it on every turn through bili.
Capture usage.cache_creation verbatim per round (start frame unconditional,
incomplete message_delta frames only when they carry tokens — same relay
zero-echo guard as the numerics) and emit it from buildTerminal next to
cache_creation_input_tokens. Stitched turns take the final round's
breakdown, matching how the totals are chosen. Never synthesized: absent
upstream means absent downstream. The start frame stays neutral, so the
(cherry picked from commit fffa53973a)
984 lines
61 KiB
TypeScript
984 lines
61 KiB
TypeScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import type { Config, CoreMessage } from "acp-kernel";
|
|
import { createCore, createInitialState } from "acp-kernel";
|
|
import type { Session } from "../src/session.ts";
|
|
import { runCompressLoop, createOpenaiAdapter, createAnthropicAdapter, createResponsesAdapter } from "../src/loop/index.ts";
|
|
import type { ParsedStreamEvent } from "../src/loop/index.ts";
|
|
import { buildCompressSystemPrompt } from "../src/compress-tool.ts";
|
|
import { responsesToCore } from "acp-kernel/wire";
|
|
import type { ResponsesRequestBody, AnthropicTextBlock } from "acp-kernel/wire";
|
|
|
|
function makeCtx(id: string, messages: CoreMessage[] = []): {
|
|
core: ReturnType<typeof createCore>;
|
|
config: Config;
|
|
messages: CoreMessage[];
|
|
session: Session;
|
|
log: (m: string) => void;
|
|
proxyUrl?: string;
|
|
textProtocol?: boolean;
|
|
} {
|
|
return {
|
|
core: createCore(),
|
|
config: { modelContextLimit: 200000 } as Config,
|
|
messages,
|
|
session: {
|
|
id,
|
|
meta: {},
|
|
stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 , compressCreditTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 },
|
|
metadata: {},
|
|
state: createInitialState(),
|
|
createdAt: Date.now(),
|
|
lastSeen: Date.now(),
|
|
blockContents: new Map(),
|
|
inFlight: 0,
|
|
persisted: false,
|
|
pendingRetrievals: [],
|
|
},
|
|
log: () => {},
|
|
};
|
|
}
|
|
|
|
async function drain(
|
|
stream: ReadableStream<Uint8Array>,
|
|
ctx: ReturnType<typeof makeCtx>,
|
|
adapter: ReturnType<typeof createOpenaiAdapter> | ReturnType<typeof createAnthropicAdapter> | ReturnType<typeof createResponsesAdapter>,
|
|
requestBody: Record<string, unknown>,
|
|
): Promise<string> {
|
|
const chunks: Buffer[] = [];
|
|
for await (const chunk of runCompressLoop(stream, ctx, requestBody, { url: "http://mock", headers: {} }, adapter, buildCompressSystemPrompt())) {
|
|
chunks.push(chunk);
|
|
}
|
|
return Buffer.concat(chunks).toString("utf8");
|
|
}
|
|
|
|
test("openai adapter: plain text round-trips live + [DONE] termination", async () => {
|
|
const round1 = [
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }] })}\n\n`,
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { content: "Hello" }, finish_reason: null }] })}\n\n`,
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: {}, finish_reason: "stop" }], usage: { prompt_tokens: 5, completion_tokens: 1 } })}\n\n`,
|
|
`data: [DONE]\n\n`,
|
|
].join("");
|
|
const out = await drain(
|
|
new Response(round1, { status: 200 }).body!,
|
|
makeCtx("openai-text"),
|
|
createOpenaiAdapter({ model: "gpt" }),
|
|
{ model: "gpt", messages: [], stream: true },
|
|
);
|
|
assert.ok(out.includes("Hello"), "round-1 text streamed live");
|
|
assert.ok(out.includes("[DONE]"), "[DONE] terminator present");
|
|
assert.ok(/finish_reason/.test(out), "finish chunk present");
|
|
});
|
|
|
|
test("openai adapter: separated usage chunk (choices:[] + usage) captured → lastInputTokens set", async () => {
|
|
const round1 = [
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { content: "Hi" }, finish_reason: null }] })}\n\n`,
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] })}\n\n`,
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [], usage: { prompt_tokens: 42, completion_tokens: 7, prompt_tokens_details: { cached_tokens: 30 } } })}\n\n`,
|
|
`data: [DONE]\n\n`,
|
|
].join("");
|
|
const ctx = makeCtx("openai-usage-sep");
|
|
await drain(
|
|
new Response(round1, { status: 200 }).body!,
|
|
ctx,
|
|
createOpenaiAdapter({ model: "gpt" }),
|
|
{ model: "gpt", messages: [], stream: true },
|
|
);
|
|
assert.ok((ctx.session.stats.lastInputTokens ?? 0) > 0, "usage from choices:[] chunk captured (lastInputTokens > 0)");
|
|
});
|
|
|
|
test("openai adapter: DeepSeek prompt_cache_hit_tokens normalized into stats + completion usage (#779)", async () => {
|
|
const round1 = [
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { content: "Hi" }, finish_reason: null }] })}\n\n`,
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] })}\n\n`,
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [], usage: { prompt_tokens: 42, completion_tokens: 7, prompt_cache_hit_tokens: 30 } })}\n\n`,
|
|
`data: [DONE]\n\n`,
|
|
].join("");
|
|
const ctx = makeCtx("openai-deepseek-cache");
|
|
const out = await drain(
|
|
new Response(round1, { status: 200 }).body!,
|
|
ctx,
|
|
createOpenaiAdapter({ model: "gpt" }),
|
|
{ model: "gpt", messages: [], stream: true },
|
|
);
|
|
assert.equal(ctx.session.stats.cachedTokens, 30, "top-level prompt_cache_hit_tokens counted");
|
|
assert.equal(ctx.session.stats.cacheSamples, 1, "cache sample recorded");
|
|
assert.ok(out.includes('"prompt_tokens_details":{"cached_tokens":30}'), `synthesized completion carries normalized cached_tokens: ${out}`);
|
|
});
|
|
|
|
test("openai adapter: finish-frame usage — standard field wins over prompt_cache_hit_tokens (#779)", async () => {
|
|
const adapter = createOpenaiAdapter({ model: "gpt" });
|
|
const stream = new Response([
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { content: "Hi" }, finish_reason: null }] })}\n\n`,
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: {}, finish_reason: "stop" }], usage: { prompt_tokens: 42, completion_tokens: 7, prompt_tokens_details: { cached_tokens: 25 }, prompt_cache_hit_tokens: 30 } })}\n\n`,
|
|
`data: [DONE]\n\n`,
|
|
].join("")).body!;
|
|
let cached: number | undefined;
|
|
for await (const ev of adapter.parseStream(stream, 1)) {
|
|
if (ev.kind === "usage") cached = ev.cachedTokens;
|
|
}
|
|
assert.equal(cached, 25, "standard field wins when both present");
|
|
});
|
|
|
|
test("openai adapter: acp_status-only round → marker + re-request, no crash", async () => {
|
|
const round1 = [
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }] })}\n\n`,
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_s", type: "function", function: { name: "acp_status", arguments: "{}" } }] }, finish_reason: null }] })}\n\n`,
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] })}\n\n`,
|
|
`data: [DONE]\n\n`,
|
|
].join("");
|
|
const round2 = [
|
|
`data: ${JSON.stringify({ id: "c2", object: "chat.completion.chunk", created: 2, model: "gpt", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] })}\n\n`,
|
|
`data: [DONE]\n\n`,
|
|
].join("");
|
|
let fetchCalls = 0;
|
|
const orig = globalThis.fetch;
|
|
globalThis.fetch = (async () => { fetchCalls++; return new Response(round2, { status: 200 }); }) as typeof fetch;
|
|
try {
|
|
const out = await drain(
|
|
new Response(round1, { status: 200 }).body!,
|
|
makeCtx("openai-status"),
|
|
createOpenaiAdapter({ model: "gpt" }),
|
|
{ model: "gpt", messages: [], stream: true },
|
|
);
|
|
assert.ok(out.includes("[ACP]"), "acp_status marker surfaced");
|
|
assert.ok(out.includes("[DONE]"), "graceful completion ([DONE])");
|
|
assert.equal(fetchCalls, 1, "re-request after acp_status (avoids tool_calls-no-body hang)");
|
|
} finally {
|
|
globalThis.fetch = orig;
|
|
}
|
|
});
|
|
|
|
test("anthropic adapter: plain text round-trips live + message_delta/message_stop termination", async () => {
|
|
const round1 = [
|
|
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_1", usage: { input_tokens: 3 } } })}\n\n`,
|
|
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n`,
|
|
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "Hello" } })}\n\n`,
|
|
`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n`,
|
|
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } })}\n\n`,
|
|
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`,
|
|
].join("");
|
|
const out = await drain(
|
|
new Response(round1, { status: 200 }).body!,
|
|
makeCtx("anthropic-text"),
|
|
createAnthropicAdapter({ model: "claude" }),
|
|
{ model: "claude", messages: [], stream: true, max_tokens: 10 },
|
|
);
|
|
assert.ok(out.includes("Hello"), "round-1 text streamed live");
|
|
assert.ok(out.includes("message_delta"), "message_delta terminal present");
|
|
assert.ok(out.includes("message_stop"), "message_stop terminal present");
|
|
});
|
|
|
|
test("anthropic adapter: relay-echoed message_delta with input_tokens: 0 must NOT overwrite message_start usage", async () => {
|
|
// Some relays echo a schema-shaped `usage` in message_delta where
|
|
// `input_tokens` is present but 0 (the spec field is normally absent).
|
|
// The input context is fixed within a turn — message_start is
|
|
// authoritative — so a 0 in message_delta must be ignored, not merged
|
|
// (it used to zero roundInput → lastInputTokens = cached-only → the nudge
|
|
// denominator collapsed and compression never fired on cached sessions).
|
|
const round1 = [
|
|
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_1", usage: { input_tokens: 55, cache_read_input_tokens: 11 } } })}\n\n`,
|
|
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n`,
|
|
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } })}\n\n`,
|
|
`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n`,
|
|
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { input_tokens: 0, cache_read_input_tokens: 0, output_tokens: 7 } })}\n\n`,
|
|
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`,
|
|
].join("");
|
|
const ctx = { ...makeCtx("anthropic-delta-zero"), protocol: "anthropic" };
|
|
await drain(
|
|
new Response(round1, { status: 200 }).body!,
|
|
ctx,
|
|
createAnthropicAdapter({ model: "claude" }),
|
|
{ model: "claude", messages: [], stream: true, max_tokens: 10 },
|
|
);
|
|
assert.equal(ctx.session.stats.lastInputTokens, 66, "total = input_tokens(55) + cache_read(11); the 0 in message_delta is ignored");
|
|
assert.equal(ctx.session.stats.cachedTokens, 11, "cached portion from message_start preserved");
|
|
});
|
|
|
|
test("anthropic adapter (#299): stitched stream — terminal's complete usage adopts atomically, no double-count", async () => {
|
|
// After a compress re-request, the stream handed to a downstream bili is
|
|
// two rounds stitched: round1's message_start (pre-compress cache_read) +
|
|
// the final synthetic terminal (post-compress input, cache_read
|
|
// legitimately 0). The terminal carries a COMPLETE usage object
|
|
// (input_tokens > 0 AND cache_read_input_tokens present), so it must be
|
|
// adopted atomically — the 0 overwrites the stale cache_read, else the
|
|
// total double-counts (142543 + 118663 = 261206 instead of 142543 →
|
|
// false 131% EMERGENCY nudge + preflight compression).
|
|
const stitched = [
|
|
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_1", usage: { input_tokens: 50000, cache_read_input_tokens: 118663 } } })}\n\n`,
|
|
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n`,
|
|
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } })}\n\n`,
|
|
`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n`,
|
|
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { input_tokens: 142543, cache_read_input_tokens: 0, output_tokens: 7 } })}\n\n`,
|
|
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`,
|
|
].join("");
|
|
const ctx = { ...makeCtx("anthropic-stitched"), protocol: "anthropic" };
|
|
await drain(
|
|
new Response(stitched, { status: 200 }).body!,
|
|
ctx,
|
|
createAnthropicAdapter({ model: "claude" }),
|
|
{ model: "claude", messages: [], stream: true, max_tokens: 10 },
|
|
);
|
|
assert.equal(ctx.session.stats.lastInputTokens, 142543, "total = terminal input(142543) + terminal cache(0); stale message_start cache_read(118663) NOT double-counted");
|
|
assert.equal(ctx.session.stats.cachedTokens, 0, "cached portion = terminal's 0 (atomically adopted)");
|
|
});
|
|
|
|
test("anthropic adapter (#299): stitched terminal after proxy-tool re-request carries complete authoritative usage", async () => {
|
|
// Generation-side contract: the synthetic terminal emitted after a
|
|
// compress/proxy-tool re-request must carry BOTH input_tokens and
|
|
// cache_read_input_tokens with the final round's true values (0 included)
|
|
// so a downstream parser can adopt the usage atomically.
|
|
const round1 = [
|
|
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_1", usage: { input_tokens: 50000, cache_read_input_tokens: 118663 } } })}\n\n`,
|
|
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "tool_use", id: "toolu_s", name: "acp_status", input: {} } })}\n\n`,
|
|
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: "{}" } })}\n\n`,
|
|
`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n`,
|
|
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 1 } })}\n\n`,
|
|
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`,
|
|
].join("");
|
|
const round2 = [
|
|
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_2", usage: { input_tokens: 142543, cache_read_input_tokens: 0 } } })}\n\n`,
|
|
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n`,
|
|
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "done" } })}\n\n`,
|
|
`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n`,
|
|
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 3 } })}\n\n`,
|
|
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`,
|
|
].join("");
|
|
let fetchCalls = 0;
|
|
const orig = globalThis.fetch;
|
|
globalThis.fetch = (async () => { fetchCalls++; return new Response(round2, { status: 200 }); }) as typeof fetch;
|
|
try {
|
|
const out = await drain(
|
|
new Response(round1, { status: 200 }).body!,
|
|
makeCtx("anthropic-stitched-terminal"),
|
|
createAnthropicAdapter({ model: "claude" }),
|
|
{ model: "claude", messages: [], stream: true, max_tokens: 10 },
|
|
);
|
|
assert.equal(fetchCalls, 1, "re-request after proxy tool");
|
|
const terminalLines = out
|
|
.split("\n")
|
|
.filter((l) => l.startsWith("data: ") && l.includes('"message_delta"'))
|
|
.map((l) => JSON.parse(l.slice("data: ".length)) as Record<string, unknown>);
|
|
assert.equal(terminalLines.length, 1, "exactly one (synthetic) message_delta in the stitched output");
|
|
const usage = (terminalLines[0]?.usage ?? {}) as Record<string, unknown>;
|
|
assert.equal(usage.input_tokens, 142543, "terminal carries final round's input_tokens");
|
|
assert.equal(usage.cache_read_input_tokens, 0, "terminal carries final round's cache_read (explicit 0, so downstream can adopt atomically)");
|
|
assert.equal(usage.output_tokens, 3, "terminal carries final round's output_tokens");
|
|
} finally {
|
|
globalThis.fetch = orig;
|
|
}
|
|
});
|
|
|
|
// #1310: the stitched stream's CLIENT-VISIBLE bytes must carry usage in
|
|
// exactly ONE frame — the synthetic terminal. Pre-fix, round-1's raw
|
|
// message_start (pre-fold usage) was forwarded verbatim alongside the
|
|
// post-fold terminal; Claude Code merged input from the terminal with
|
|
// cache_read from the start (94720 + 166610 = 262k > 200k) and refused every
|
|
// further turn locally ("Prompt is too long", session permanently locked).
|
|
test("anthropic adapter (#1310): forwarded message_start is usage-neutral — the terminal is the sole usage authority", async () => {
|
|
const round1 = [
|
|
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_1", usage: { input_tokens: 4, cache_read_input_tokens: 166610, cache_creation_input_tokens: 618 } } })}\n\n`,
|
|
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "tool_use", id: "toolu_s", name: "acp_status", input: {} } })}\n\n`,
|
|
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: "{}" } })}\n\n`,
|
|
`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n`,
|
|
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 1 } })}\n\n`,
|
|
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`,
|
|
].join("");
|
|
const round2 = [
|
|
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_2", usage: { input_tokens: 94720, cache_read_input_tokens: 93000, cache_creation_input_tokens: 120 } } })}\n\n`,
|
|
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n`,
|
|
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "done" } })}\n\n`,
|
|
`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n`,
|
|
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 3 } })}\n\n`,
|
|
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`,
|
|
].join("");
|
|
let fetchCalls = 0;
|
|
const orig = globalThis.fetch;
|
|
globalThis.fetch = (async () => { fetchCalls++; return new Response(round2, { status: 200 }); }) as typeof fetch;
|
|
try {
|
|
const ctx = { ...makeCtx("anthropic-1310-stitched"), protocol: "anthropic" };
|
|
const out = await drain(
|
|
new Response(round1, { status: 200 }).body!,
|
|
ctx,
|
|
createAnthropicAdapter({ model: "claude" }),
|
|
{ model: "claude", messages: [], stream: true, max_tokens: 10 },
|
|
);
|
|
assert.equal(fetchCalls, 1, "re-request after proxy tool");
|
|
const frames = out.split("\n").filter((l) => l.startsWith("data: ")).map((l) => JSON.parse(l.slice(6)) as Record<string, unknown>);
|
|
const start = frames.find((f) => f.type === "message_start");
|
|
assert.ok(start, "start frame forwarded");
|
|
const startUsage = ((start!.message as Record<string, unknown>).usage ?? {}) as Record<string, unknown>;
|
|
assert.equal(startUsage.input_tokens, 0, "#1310: forwarded start carries NO stale pre-fold usage");
|
|
assert.equal(startUsage.cache_read_input_tokens, undefined, "#1310: no stale cache_read for a host to merge");
|
|
const usageFrames = frames.filter((f) => (f.usage ?? undefined) !== undefined && Object.keys(f.usage as Record<string, unknown>).length > 0);
|
|
assert.equal(usageFrames.length, 1, "the synthetic terminal is the ONLY usage-bearing frame");
|
|
const terminalUsage = usageFrames[0]!.usage as Record<string, unknown>;
|
|
assert.equal(terminalUsage.input_tokens, 94720, "terminal: final round input");
|
|
assert.equal(terminalUsage.cache_read_input_tokens, 93000, "terminal: final round cache_read");
|
|
assert.equal(terminalUsage.cache_creation_input_tokens, 120, "terminal: cache_creation rides the sole authority frame");
|
|
assert.equal(terminalUsage.output_tokens, 3, "terminal: output");
|
|
const maxHostTotal = 94720 + 93000 + 120;
|
|
assert.ok(maxHostTotal < 200000, "every merge rule (sum/max/start) now reconstructs a sub-window context");
|
|
assert.equal(ctx.session.stats.lastInputTokens, 187840, "internal ledger unchanged: real final-round total (input+cache_read+creation), not the zeroed start");
|
|
} finally {
|
|
globalThis.fetch = orig;
|
|
}
|
|
});
|
|
|
|
// #1310 companion: a NON-stitched turn keeps its final usage identical — the
|
|
// zeroed start loses nothing because the terminal already carried the same
|
|
// numbers the start frame had.
|
|
test("anthropic adapter (#1310/#4072): non-stitched turn — zeroed start, identical final usage via terminal", async () => {
|
|
const single = [
|
|
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_1", usage: { input_tokens: 4, cache_read_input_tokens: 165132, cache_creation_input_tokens: 618, cache_creation: { ephemeral_5m_input_tokens: 618 } } } })}\n\n`,
|
|
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n`,
|
|
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } })}\n\n`,
|
|
`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n`,
|
|
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 3 } })}\n\n`,
|
|
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`,
|
|
].join("");
|
|
const ctx = { ...makeCtx("anthropic-1310-single"), protocol: "anthropic" };
|
|
const out = await drain(
|
|
new Response(single, { status: 200 }).body!,
|
|
ctx,
|
|
createAnthropicAdapter({ model: "claude" }),
|
|
{ model: "claude", messages: [], stream: true, max_tokens: 10 },
|
|
);
|
|
const frames = out.split("\n").filter((l) => l.startsWith("data: ")).map((l) => JSON.parse(l.slice(6)) as Record<string, unknown>);
|
|
const start = frames.find((f) => f.type === "message_start");
|
|
const startUsage = ((start!.message as Record<string, unknown>).usage ?? {}) as Record<string, unknown>;
|
|
assert.equal(startUsage.input_tokens, 0, "start neutral here too (one consistent rule)");
|
|
const usageFrames = frames.filter((f) => (f.usage ?? undefined) !== undefined && Object.keys(f.usage as Record<string, unknown>).length > 0);
|
|
assert.equal(usageFrames.length, 1, "single usage-bearing frame");
|
|
const terminalUsage = usageFrames[0]!.usage as Record<string, unknown>;
|
|
assert.equal(terminalUsage.input_tokens, 4, "terminal carries the numbers the start frame had");
|
|
assert.equal(terminalUsage.cache_read_input_tokens, 165132);
|
|
assert.equal(terminalUsage.cache_creation_input_tokens, 618);
|
|
// #4072: the cache_creation breakdown (ephemeral_5m/1h) lives ONLY in the
|
|
// upstream message_start — forwarded usage-neutral above — so the terminal
|
|
// must re-carry it verbatim or hosts deriving their prompt-cache TTL from
|
|
// it (Claude Code VS Code countdown icon) lose it.
|
|
assert.deepEqual(terminalUsage.cache_creation, { ephemeral_5m_input_tokens: 618 }, "#4072: terminal carries the start frame's breakdown verbatim");
|
|
assert.equal(startUsage.cache_creation, undefined, "#4072: forwarded start stays neutral — no breakdown for a host to double-read");
|
|
});
|
|
|
|
test("anthropic adapter (#4072): stitched turn — terminal carries the FINAL round's cache_creation breakdown", async () => {
|
|
const round1 = [
|
|
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_1", usage: { input_tokens: 4, cache_read_input_tokens: 166610, cache_creation_input_tokens: 618, cache_creation: { ephemeral_5m_input_tokens: 618 } } } })}\n\n`,
|
|
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "tool_use", id: "toolu_s", name: "acp_status", input: {} } })}\n\n`,
|
|
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: "{}" } })}\n\n`,
|
|
`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n`,
|
|
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 1 } })}\n\n`,
|
|
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`,
|
|
].join("");
|
|
const round2 = [
|
|
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_2", usage: { input_tokens: 94720, cache_read_input_tokens: 93000, cache_creation_input_tokens: 120, cache_creation: { ephemeral_5m_input_tokens: 120 } } } })}\n\n`,
|
|
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n`,
|
|
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "done" } })}\n\n`,
|
|
`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n`,
|
|
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 3 } })}\n\n`,
|
|
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`,
|
|
].join("");
|
|
let fetchCalls = 0;
|
|
const orig = globalThis.fetch;
|
|
globalThis.fetch = (async () => { fetchCalls++; return new Response(round2, { status: 200 }); }) as typeof fetch;
|
|
try {
|
|
const ctx = { ...makeCtx("anthropic-4072-stitched"), protocol: "anthropic" };
|
|
const out = await drain(
|
|
new Response(round1, { status: 200 }).body!,
|
|
ctx,
|
|
createAnthropicAdapter({ model: "claude" }),
|
|
{ model: "claude", messages: [], stream: true, max_tokens: 10 },
|
|
);
|
|
assert.equal(fetchCalls, 1, "re-request after proxy tool");
|
|
const frames = out.split("\n").filter((l) => l.startsWith("data: ")).map((l) => JSON.parse(l.slice(6)) as Record<string, unknown>);
|
|
const usageFrames = frames.filter((f) => (f.usage ?? undefined) !== undefined && Object.keys(f.usage as Record<string, unknown>).length > 0);
|
|
assert.equal(usageFrames.length, 1, "the synthetic terminal is the ONLY usage-bearing frame");
|
|
const terminalUsage = usageFrames[0]!.usage as Record<string, unknown>;
|
|
assert.deepEqual(terminalUsage.cache_creation, { ephemeral_5m_input_tokens: 120 }, "#4072: final round's breakdown wins — same rule as the totals; round 1's pre-fold value must not leak");
|
|
} finally {
|
|
globalThis.fetch = orig;
|
|
}
|
|
});
|
|
|
|
test("anthropic adapter (#4072): no breakdown upstream — terminal gains no cache_creation key (never synthesized)", async () => {
|
|
const single = [
|
|
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_1", usage: { input_tokens: 4, cache_read_input_tokens: 165132, cache_creation_input_tokens: 618 } } })}\n\n`,
|
|
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n`,
|
|
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } })}\n\n`,
|
|
`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n`,
|
|
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 3 } })}\n\n`,
|
|
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`,
|
|
].join("");
|
|
const ctx = { ...makeCtx("anthropic-4072-absent"), protocol: "anthropic" };
|
|
const out = await drain(
|
|
new Response(single, { status: 200 }).body!,
|
|
ctx,
|
|
createAnthropicAdapter({ model: "claude" }),
|
|
{ model: "claude", messages: [], stream: true, max_tokens: 10 },
|
|
);
|
|
const frames = out.split("\n").filter((l) => l.startsWith("data: ")).map((l) => JSON.parse(l.slice(6)) as Record<string, unknown>);
|
|
const usageFrames = frames.filter((f) => (f.usage ?? undefined) !== undefined && Object.keys(f.usage as Record<string, unknown>).length > 0);
|
|
const terminalUsage = usageFrames[0]!.usage as Record<string, unknown>;
|
|
assert.equal(terminalUsage.cache_creation, undefined, "#4072: absent upstream means absent downstream — bili never invents a 5m/1h split");
|
|
});
|
|
|
|
test("anthropic adapter (#4072): incomplete delta echoing an all-zero breakdown does not clobber the start-frame values", async () => {
|
|
const single = [
|
|
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_1", usage: { input_tokens: 4, cache_read_input_tokens: 165132, cache_creation_input_tokens: 618, cache_creation: { ephemeral_5m_input_tokens: 618 } } } })}\n\n`,
|
|
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n`,
|
|
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } })}\n\n`,
|
|
`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n`,
|
|
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 3, input_tokens: 0, cache_read_input_tokens: 0, cache_creation_input_tokens: 0, cache_creation: { ephemeral_5m_input_tokens: 0, ephemeral_1h_input_tokens: 0 } } })}\n\n`,
|
|
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`,
|
|
].join("");
|
|
const ctx = { ...makeCtx("anthropic-4072-zerorelay"), protocol: "anthropic" };
|
|
const out = await drain(
|
|
new Response(single, { status: 200 }).body!,
|
|
ctx,
|
|
createAnthropicAdapter({ model: "claude" }),
|
|
{ model: "claude", messages: [], stream: true, max_tokens: 10 },
|
|
);
|
|
const frames = out.split("\n").filter((l) => l.startsWith("data: ")).map((l) => JSON.parse(l.slice(6)) as Record<string, unknown>);
|
|
const usageFrames = frames.filter((f) => (f.usage ?? undefined) !== undefined && Object.keys(f.usage as Record<string, unknown>).length > 0);
|
|
const terminalUsage = usageFrames[0]!.usage as Record<string, unknown>;
|
|
assert.deepEqual(terminalUsage.cache_creation, { ephemeral_5m_input_tokens: 618 }, "#4072: schema-shaped zero echo (same relay class as #660's input_tokens:0) is not an update");
|
|
assert.equal(terminalUsage.input_tokens, 4, "numerics' > 0 adoption rule still holds beside the breakdown gate");
|
|
});
|
|
|
|
test("anthropic adapter: acp_status-only round → marker + re-request, no crash", async () => {
|
|
const round1 = [
|
|
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_1", usage: { input_tokens: 3 } } })}\n\n`,
|
|
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "tool_use", id: "toolu_s", name: "acp_status", input: {} } })}\n\n`,
|
|
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: "{}" } })}\n\n`,
|
|
`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n`,
|
|
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 1 } })}\n\n`,
|
|
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`,
|
|
].join("");
|
|
const round2 = [
|
|
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_2", usage: { input_tokens: 3 } } })}\n\n`,
|
|
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } })}\n\n`,
|
|
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`,
|
|
].join("");
|
|
let fetchCalls = 0;
|
|
const orig = globalThis.fetch;
|
|
globalThis.fetch = (async () => { fetchCalls++; return new Response(round2, { status: 200 }); }) as typeof fetch;
|
|
try {
|
|
const out = await drain(
|
|
new Response(round1, { status: 200 }).body!,
|
|
makeCtx("anthropic-status"),
|
|
createAnthropicAdapter({ model: "claude" }),
|
|
{ model: "claude", messages: [], stream: true, max_tokens: 10 },
|
|
);
|
|
assert.ok(out.includes("[ACP]"), "acp_status marker surfaced");
|
|
assert.ok(out.includes("message_stop"), "graceful completion (message_stop)");
|
|
assert.equal(fetchCalls, 1, "re-request after acp_status (avoids tool_use-no-body hang)");
|
|
} finally {
|
|
globalThis.fetch = orig;
|
|
}
|
|
});
|
|
|
|
test("openai adapter (S1): a single finish_reason chunk is forwarded exactly once (not double-emitted)", async () => {
|
|
const round1 = [
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }] })}\n\n`,
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] })}\n\n`,
|
|
`data: [DONE]\n\n`,
|
|
].join("");
|
|
const out = await drain(
|
|
new Response(round1, { status: 200 }).body!,
|
|
makeCtx("openai-single-finish"),
|
|
createOpenaiAdapter({ model: "gpt" }),
|
|
{ model: "gpt", messages: [], stream: true },
|
|
);
|
|
const finishCount = (out.match(/"finish_reason":"stop"/g) || []).length;
|
|
assert.equal(finishCount, 1, "finish_reason:\"stop\" emitted exactly once (S1: original finish chunk not passthrough'd + emitCompletion)");
|
|
});
|
|
|
|
test("ACP_LOOP_V2 smoke: import + runCompressLoop round-trips (responses) without crashing", async () => {
|
|
const mod = await import("../src/loop/index.ts");
|
|
assert.equal(typeof mod.runCompressLoop, "function", "runCompressLoop exported");
|
|
assert.equal(typeof mod.pickAdapter, "function", "pickAdapter exported");
|
|
const adapter = mod.pickAdapter("responses", { model: "gpt" });
|
|
assert.equal(typeof adapter.parseStream, "function", "responses adapter has parseStream");
|
|
const openaiA = mod.pickAdapter("openai", { model: "gpt" });
|
|
assert.equal(typeof openaiA.emitCompletion, "function", "openai adapter has emitCompletion");
|
|
const anthA = mod.pickAdapter("anthropic", { model: "claude" });
|
|
assert.equal(typeof anthA.emitText, "function", "anthropic adapter has emitText");
|
|
});
|
|
|
|
function mockStream(...chunks: string[]): ReadableStream<Uint8Array> {
|
|
return new ReadableStream<Uint8Array>({
|
|
start(controller) {
|
|
const enc = new TextEncoder();
|
|
for (const c of chunks) controller.enqueue(enc.encode(c));
|
|
controller.close();
|
|
},
|
|
});
|
|
}
|
|
|
|
function erroringStream(firstChunk: string): ReadableStream<Uint8Array> {
|
|
let calls = 0;
|
|
return new ReadableStream<Uint8Array>({
|
|
async pull(controller) {
|
|
calls++;
|
|
if (calls === 1) {
|
|
controller.enqueue(new TextEncoder().encode(firstChunk));
|
|
return;
|
|
}
|
|
throw new Error("simulated network drop on second read");
|
|
},
|
|
});
|
|
}
|
|
|
|
function sseLf(type: string, data: unknown): string {
|
|
return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`;
|
|
}
|
|
|
|
function sseCrlf(type: string, data: unknown): string {
|
|
return `event: ${type}\r\ndata: ${JSON.stringify(data)}\r\n\r\n`;
|
|
}
|
|
|
|
async function collectParseEvents(
|
|
adapter: ReturnType<typeof createResponsesAdapter> | ReturnType<typeof createOpenaiAdapter> | ReturnType<typeof createAnthropicAdapter>,
|
|
stream: ReadableStream<Uint8Array>,
|
|
round: number,
|
|
): Promise<ParsedStreamEvent[]> {
|
|
const events: ParsedStreamEvent[] = [];
|
|
for await (const ev of adapter.parseStream(stream, round)) events.push(ev);
|
|
return events;
|
|
}
|
|
|
|
test("F1 (CRLF): responses adapter yields text delta from \\r\\n-separated SSE (not silently swallowed)", async () => {
|
|
const crlf = sseCrlf("response.output_text.delta", {
|
|
type: "response.output_text.delta",
|
|
item_id: "m1",
|
|
output_index: 0,
|
|
delta: "HiCRLF",
|
|
});
|
|
const events = await collectParseEvents(createResponsesAdapter(), mockStream(crlf), 1);
|
|
const textEv = events.find((e) => e.kind === "text");
|
|
assert.ok(textEv, "text delta yielded — CRLF (0D0A0D0A) normalized so indexOf(\\n\\n) matches");
|
|
if (textEv && textEv.kind === "text") {
|
|
assert.equal(textEv.delta, "HiCRLF", "delta content intact");
|
|
}
|
|
});
|
|
|
|
test("F1 (CRLF): openai adapter yields text delta from \\r\\n-separated SSE (not silently swallowed)", async () => {
|
|
const crlf = `data: ${JSON.stringify({
|
|
id: "c1",
|
|
object: "chat.completion.chunk",
|
|
created: 1,
|
|
model: "gpt",
|
|
choices: [{ index: 0, delta: { content: "HiCRLF" }, finish_reason: null }],
|
|
})}\r\n\r\n`;
|
|
const events = await collectParseEvents(createOpenaiAdapter({ model: "gpt" }), mockStream(crlf), 1);
|
|
const textEv = events.find((e) => e.kind === "text");
|
|
assert.ok(textEv, "text delta yielded — CRLF (0D0A0D0A) normalized so indexOf(\\n\\n) matches");
|
|
if (textEv && textEv.kind === "text") {
|
|
assert.equal(textEv.delta, "HiCRLF", "delta content intact");
|
|
}
|
|
});
|
|
|
|
test("F1 (CRLF): anthropic adapter yields text delta from \\r\\n-separated SSE (not silently swallowed)", async () => {
|
|
const crlf = sseCrlf("content_block_delta", {
|
|
type: "content_block_delta",
|
|
index: 0,
|
|
delta: { type: "text_delta", text: "HiCRLF" },
|
|
});
|
|
const events = await collectParseEvents(createAnthropicAdapter({ model: "claude" }), mockStream(crlf), 1);
|
|
const textEv = events.find((e) => e.kind === "text");
|
|
assert.ok(textEv, "text delta yielded — CRLF (0D0A0D0A) normalized so indexOf(\\n\\n) matches");
|
|
if (textEv && textEv.kind === "text") {
|
|
assert.equal(textEv.delta, "HiCRLF", "delta content intact");
|
|
}
|
|
});
|
|
|
|
test("F2: responses custom_tool_call meta events have firstRoundOnly=false (passthrough survives round 2+)", async () => {
|
|
const stream = mockStream(
|
|
sseLf("response.output_item.added", {
|
|
type: "response.output_item.added",
|
|
output_index: 0,
|
|
item: { type: "custom_tool_call", id: "ctc_1", call_id: "call_x", name: "code_mode_tool", arguments: "" },
|
|
}),
|
|
sseLf("response.output_item.done", {
|
|
type: "response.output_item.done",
|
|
output_index: 0,
|
|
item: { type: "custom_tool_call", id: "ctc_1", call_id: "call_x", name: "code_mode_tool", arguments: "{}" },
|
|
}),
|
|
);
|
|
const events = await collectParseEvents(createResponsesAdapter(), stream, 2);
|
|
const metas = events.filter((e) => e.kind === "meta");
|
|
assert.ok(metas.length >= 2, "both custom_tool_call events (added+done) yielded in round 2");
|
|
for (const m of metas) {
|
|
if (m.kind === "meta") {
|
|
assert.equal(
|
|
m.firstRoundOnly,
|
|
false,
|
|
"custom_tool_call passthrough has firstRoundOnly=false (not the generic else branch firstRoundOnly:true)",
|
|
);
|
|
}
|
|
}
|
|
});
|
|
|
|
const absorbFcStream = () => mockStream(
|
|
sseLf("response.output_item.added", {
|
|
type: "response.output_item.added",
|
|
output_index: 0,
|
|
item: { type: "function_call", id: "fc_1", call_id: "call_a1", name: "absorb", arguments: "" },
|
|
}),
|
|
sseLf("response.function_call_arguments.delta", {
|
|
type: "response.function_call_arguments.delta",
|
|
item_id: "fc_1",
|
|
output_index: 0,
|
|
delta: '{"ref":"m00003"',
|
|
}),
|
|
sseLf("response.function_call_arguments.done", {
|
|
type: "response.function_call_arguments.done",
|
|
item_id: "fc_1",
|
|
output_index: 0,
|
|
arguments: '{"ref":"m00003","summary":"s"}',
|
|
}),
|
|
sseLf("response.output_item.done", {
|
|
type: "response.output_item.done",
|
|
output_index: 0,
|
|
item: { type: "function_call", id: "fc_1", call_id: "call_a1", name: "absorb", arguments: '{"ref":"m00003","summary":"s"}' },
|
|
}),
|
|
);
|
|
|
|
test("absorb: responses adapter buffers absorb function_call when absorbName is set (PR #615 fix)", async () => {
|
|
const events = await collectParseEvents(createResponsesAdapter(false, undefined, "absorb"), absorbFcStream(), 1);
|
|
const toolCall = events.find((e) => e.kind === "tool_call");
|
|
assert.ok(toolCall, "absorb call surfaced as a structured tool_call event for loop execution");
|
|
if (toolCall && toolCall.kind === "tool_call") {
|
|
assert.equal(toolCall.name, "absorb");
|
|
assert.equal(toolCall.callId, "call_a1");
|
|
assert.equal(toolCall.arguments, '{"ref":"m00003","summary":"s"}');
|
|
assert.ok(!toolCall.passthrough, "buffered proxy call, not a passthrough");
|
|
}
|
|
const rawReplay = events.some((e) => e.kind === "meta" && e.firstRoundOnly === false);
|
|
assert.ok(!rawReplay, "no absorb SSE event raw-replayed to the client");
|
|
});
|
|
|
|
test("absorb: without absorbName the same call is raw-replayed + passthrough (documents pre-fix behavior)", async () => {
|
|
const events = await collectParseEvents(createResponsesAdapter(), absorbFcStream(), 1);
|
|
const toolCall = events.find((e) => e.kind === "tool_call");
|
|
assert.ok(toolCall?.kind === "tool_call" && toolCall.passthrough, "no absorbName → passthrough tool_call (loop skips execution)");
|
|
const rawReplay = events.filter((e) => e.kind === "meta" && e.firstRoundOnly === false);
|
|
assert.ok(rawReplay.length >= 3, "added/delta/done events raw-replayed to the client pre-fix");
|
|
});
|
|
|
|
test("F3: anthropic remaps forwarded block indices to be strictly sequential when tool_use is suppressed", async () => {
|
|
const stream = mockStream(
|
|
sseLf("message_start", { type: "message_start", message: { id: "msg_1", usage: { input_tokens: 3 } } }),
|
|
sseLf("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }),
|
|
sseLf("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "Hello" } }),
|
|
sseLf("content_block_stop", { type: "content_block_stop", index: 0 }),
|
|
sseLf("content_block_start", {
|
|
type: "content_block_start",
|
|
index: 1,
|
|
content_block: { type: "tool_use", id: "toolu_1", name: "get_weather", input: {} },
|
|
}),
|
|
sseLf("content_block_delta", {
|
|
type: "content_block_delta",
|
|
index: 1,
|
|
delta: { type: "input_json_delta", partial_json: '{"city":"SF"}' },
|
|
}),
|
|
sseLf("content_block_stop", { type: "content_block_stop", index: 1 }),
|
|
sseLf("content_block_start", { type: "content_block_start", index: 2, content_block: { type: "text", text: "" } }),
|
|
sseLf("content_block_delta", { type: "content_block_delta", index: 2, delta: { type: "text_delta", text: "World" } }),
|
|
sseLf("content_block_stop", { type: "content_block_stop", index: 2 }),
|
|
sseLf("message_delta", { type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 5 } }),
|
|
sseLf("message_stop", { type: "message_stop" }),
|
|
);
|
|
const adapter = createAnthropicAdapter({ model: "claude" });
|
|
const forwardedBlockIndices: number[] = [];
|
|
const toolCallEvents: ParsedStreamEvent[] = [];
|
|
for await (const ev of adapter.parseStream(stream, 1)) {
|
|
if (ev.kind === "tool_call") {
|
|
toolCallEvents.push(ev);
|
|
continue;
|
|
}
|
|
const buf = ev.kind === "text" ? ev.raw : ev.kind === "meta" ? ev.chunk : undefined;
|
|
if (!buf) continue;
|
|
for (const line of buf.toString("utf8").split("\n")) {
|
|
if (!line.startsWith("data:")) continue;
|
|
try {
|
|
const obj = JSON.parse(line.slice(5).trim()) as Record<string, unknown>;
|
|
if (
|
|
typeof obj.index === "number" &&
|
|
typeof obj.type === "string" &&
|
|
obj.type.startsWith("content_block_")
|
|
) {
|
|
forwardedBlockIndices.push(obj.index);
|
|
}
|
|
} catch {
|
|
}
|
|
}
|
|
}
|
|
|
|
assert.equal(toolCallEvents.length, 1, "suppressed tool_use@1 emitted as a tool_call event");
|
|
const tc = toolCallEvents[0];
|
|
if (tc && tc.kind === "tool_call") {
|
|
assert.equal(tc.name, "get_weather", "tool_call name from content_block_start");
|
|
assert.equal(tc.callId, "toolu_1", "tool_call id from content_block_start");
|
|
assert.ok(
|
|
tc.arguments.includes('"city":"SF"'),
|
|
"tool_call arguments accumulated from input_json_delta",
|
|
);
|
|
}
|
|
|
|
const distinct = [...new Set(forwardedBlockIndices)].sort((a, b) => a - b);
|
|
const expected = Array.from({ length: distinct.length }, (_, i) => i);
|
|
assert.deepEqual(
|
|
distinct,
|
|
expected,
|
|
`forwarded content_block indices STRICTLY SEQUENTIAL (0,1,...) with no gaps/dupes; got ${JSON.stringify(distinct)}`,
|
|
);
|
|
assert.ok(
|
|
distinct.includes(1),
|
|
"text@2 (upstream) remapped to client index 1 because tool_use@1 was suppressed (fills the gap)",
|
|
);
|
|
});
|
|
|
|
test("F4 (network drop): responses parseStream completes normally when upstream read() throws", async () => {
|
|
const firstChunk = sseLf("response.output_text.delta", {
|
|
type: "response.output_text.delta",
|
|
item_id: "m1",
|
|
output_index: 0,
|
|
delta: "before-drop",
|
|
});
|
|
const events = await collectParseEvents(createResponsesAdapter(), erroringStream(firstChunk), 1);
|
|
const textEv = events.find((e) => e.kind === "text");
|
|
assert.ok(textEv, "pre-drop text yielded; iterator completed normally without rethrowing the read() error");
|
|
});
|
|
|
|
test("F4 (network drop): openai parseStream completes normally when upstream read() throws", async () => {
|
|
const firstChunk = `data: ${JSON.stringify({
|
|
id: "c1",
|
|
object: "chat.completion.chunk",
|
|
created: 1,
|
|
model: "gpt",
|
|
choices: [{ index: 0, delta: { content: "before-drop" }, finish_reason: null }],
|
|
})}\n\n`;
|
|
const events = await collectParseEvents(createOpenaiAdapter({ model: "gpt" }), erroringStream(firstChunk), 1);
|
|
const textEv = events.find((e) => e.kind === "text");
|
|
assert.ok(textEv, "pre-drop text yielded; iterator completed normally without rethrowing the read() error");
|
|
});
|
|
|
|
test("F4 (network drop): anthropic parseStream completes normally when upstream read() throws", async () => {
|
|
const firstChunk = sseLf("content_block_delta", {
|
|
type: "content_block_delta",
|
|
index: 0,
|
|
delta: { type: "text_delta", text: "before-drop" },
|
|
});
|
|
const events = await collectParseEvents(createAnthropicAdapter({ model: "claude" }), erroringStream(firstChunk), 1);
|
|
const textEv = events.find((e) => e.kind === "text");
|
|
assert.ok(textEv, "pre-drop text yielded; iterator completed normally without rethrowing the read() error");
|
|
});
|
|
|
|
test("F5: responses empty upstream → emitCompletion produces response.failed (not fabricated completed)", async () => {
|
|
const adapter = createResponsesAdapter();
|
|
let doneFinishReason: string | undefined;
|
|
for await (const ev of adapter.parseStream(mockStream(), 1)) {
|
|
if (ev.kind === "done") doneFinishReason = ev.finishReason;
|
|
}
|
|
assert.equal(doneFinishReason, "failed", "empty upstream yields done.finishReason=failed (not undefined)");
|
|
|
|
const out = adapter.emitCompletion({ finishReason: doneFinishReason }).toString("utf8");
|
|
assert.ok(out.includes("event: response.failed"), "emitCompletion emits response.failed event");
|
|
assert.ok(out.includes('"status":"failed"'), "response object carries status:failed");
|
|
assert.ok(
|
|
!out.includes("event: response.completed"),
|
|
"emitCompletion does NOT fabricate response.completed on empty upstream",
|
|
);
|
|
});
|
|
|
|
test("F6: responses buildRequest preserves instructions + additional_tools prefix (codex prefix-cache fix)", () => {
|
|
const body = {
|
|
instructions: "AGENTS_MD_RULES",
|
|
input: [
|
|
{ type: "additional_tools", name: "shell", tools: [{ type: "function", name: "shell", parameters: {} }] },
|
|
{ type: "message", role: "user", content: "hello codex" },
|
|
],
|
|
stream: true,
|
|
} as unknown as ResponsesRequestBody;
|
|
const projection = responsesToCore(body);
|
|
assert.ok(projection.systemParts.includes("AGENTS_MD_RULES"), "instructions lifted into systemParts");
|
|
assert.ok(projection.preamble.some((i) => i.type === "additional_tools"), "additional_tools captured as preamble");
|
|
|
|
const systemPrompt = buildCompressSystemPrompt();
|
|
const adapter = createResponsesAdapter(false, projection);
|
|
const rebuilt = adapter.buildRequest(projection.msgs, systemPrompt, body) as Record<string, unknown>;
|
|
const input = rebuilt.input as Array<Record<string, unknown>>;
|
|
|
|
assert.equal(!("instructions" in rebuilt), true, "top-level instructions stripped (responses_lite contract)");
|
|
assert.equal(!("previous_response_id" in rebuilt), true, "previous_response_id stripped");
|
|
|
|
assert.equal(input[0]?.type, "additional_tools", "additional_tools stays at input[0] (preamble preserved)");
|
|
const dev = input.find((i) => i.role === "developer");
|
|
assert.ok(dev, "developer message present");
|
|
assert.ok(typeof dev?.content === "string" && dev.content.includes("AGENTS_MD_RULES"), "developer includes original instructions (systemParts) — matches round-1 prefix");
|
|
assert.ok(typeof dev?.content === "string" && dev.content.includes(systemPrompt), "developer includes compress prompt");
|
|
});
|
|
|
|
test("F7: anthropic buildRequest preserves client system + cache_control + merges compress prompt (prefix-cache fix)", () => {
|
|
const clientSystem: AnthropicTextBlock[] = [{ type: "text", text: "YOU_ARE_CLAUDE", cache_control: { type: "ephemeral" } }];
|
|
const systemPrompt = buildCompressSystemPrompt();
|
|
const adapter = createAnthropicAdapter({ model: "claude" }, clientSystem);
|
|
const rebuilt = adapter.buildRequest([], systemPrompt, { model: "claude", messages: [] }) as Record<string, unknown>;
|
|
const system = rebuilt.system;
|
|
assert.ok(Array.isArray(system), "system preserved as structured array (buildSystem keeps array form when original was array)");
|
|
const text = Array.isArray(system)
|
|
? (system as Array<Record<string, unknown>>).map((b) => b.text as string).join("\n\n")
|
|
: (system as string);
|
|
assert.ok(text.includes("YOU_ARE_CLAUDE"), "client system text preserved (not lost in round-2) — matches round-1 prefix");
|
|
assert.ok(text.includes(systemPrompt), "compress prompt merged into system");
|
|
const hasCc = Array.isArray(system) && (system as Array<Record<string, unknown>>).some((b) => b.cache_control);
|
|
assert.ok(hasCc, "cache_control marker preserved on system block (Anthropic prefix-cache anchor)");
|
|
});
|
|
|
|
test("F7b (#1876): anthropic buildRequest keeps client system blocks byte-exact, appends prompt as trailing unmarked block", () => {
|
|
const clientSystem: AnthropicTextBlock[] = [
|
|
{ type: "text", text: "x-anthropic-billing-header: attribution cc_entrypoint=cli" },
|
|
{ type: "text", text: "YOU_ARE_CLAUDE", cache_control: { type: "ephemeral" } },
|
|
{ type: "text", text: "EXTRA_CONTEXT_BLOCK" },
|
|
];
|
|
const systemPrompt = buildCompressSystemPrompt();
|
|
const adapter = createAnthropicAdapter({ model: "claude" }, clientSystem);
|
|
const rebuilt = adapter.buildRequest([], systemPrompt, { model: "claude", messages: [] }) as Record<string, unknown>;
|
|
const system = rebuilt.system;
|
|
assert.ok(Array.isArray(system), "system stays a structured array");
|
|
const blocks = system as Array<Record<string, unknown>>;
|
|
assert.equal(blocks.length, 4, "3 client blocks + 1 appended prompt block — no merge into one block");
|
|
assert.deepEqual(blocks.slice(0, 3), clientSystem, "client blocks byte-exact in order, cache_control stays on its own (non-first) block");
|
|
assert.equal(blocks[3]?.text, systemPrompt, "appended block carries the compress prompt verbatim (no '---' separator)");
|
|
assert.equal(blocks[3]?.cache_control, undefined, "appended block carries no breakpoint (client-managed caching wins)");
|
|
});
|
|
|
|
function byteStream(chunks: Uint8Array[]): ReadableStream<Uint8Array> {
|
|
return new ReadableStream<Uint8Array>({
|
|
start(controller) {
|
|
for (const c of chunks) controller.enqueue(c);
|
|
controller.close();
|
|
},
|
|
});
|
|
}
|
|
|
|
function splitAfterLeadByte(full: Uint8Array, ch: string): [Uint8Array, Uint8Array] {
|
|
const needle = new TextEncoder().encode(ch);
|
|
let off = -1;
|
|
for (let i = 0; i + needle.length <= full.length && off < 0; i++) {
|
|
let match = true;
|
|
for (let j = 0; j < needle.length; j++) {
|
|
if (full[i + j] !== needle[j]) { match = false; break; }
|
|
}
|
|
if (match) off = i;
|
|
}
|
|
assert.ok(off >= 0, `bytes of ${ch} found in payload`);
|
|
return [full.slice(0, off + 1), full.slice(off + 1)];
|
|
}
|
|
|
|
async function cjkChunkSplitRoundTrip(
|
|
name: string,
|
|
adapter: ReturnType<typeof createResponsesAdapter> | ReturnType<typeof createOpenaiAdapter> | ReturnType<typeof createAnthropicAdapter>,
|
|
payload: string,
|
|
): Promise<void> {
|
|
const [c1, c2] = splitAfterLeadByte(new TextEncoder().encode(payload), "留");
|
|
const events = await collectParseEvents(adapter, byteStream([c1, c2]), 1);
|
|
let text = "";
|
|
for (const ev of events) {
|
|
if (ev.kind === "text") text += ev.delta;
|
|
}
|
|
assert.equal(text, "残留", `${name}: multi-byte CJK split across chunk boundary round-trips intact`);
|
|
assert.ok(!text.includes("\uFFFD"), `${name}: no U+FFFD replacement characters`);
|
|
}
|
|
|
|
test("F8 (#541): responses adapter — CJK char split across SSE chunk boundary decodes intact (no U+FFFD)", async () => {
|
|
const payload = sseLf("response.output_text.delta", {
|
|
type: "response.output_text.delta",
|
|
item_id: "m1",
|
|
output_index: 0,
|
|
delta: "残留",
|
|
});
|
|
await cjkChunkSplitRoundTrip("responses", createResponsesAdapter(), payload);
|
|
});
|
|
|
|
test("F8 (#541): openai adapter — CJK char split across SSE chunk boundary decodes intact (no U+FFFD)", async () => {
|
|
const payload =
|
|
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { content: "残留" }, finish_reason: null }] })}\n\n` +
|
|
`data: [DONE]\n\n`;
|
|
await cjkChunkSplitRoundTrip("openai", createOpenaiAdapter({ model: "gpt" }), payload);
|
|
});
|
|
|
|
test("F8 (#541): anthropic adapter — CJK char split across SSE chunk boundary decodes intact (no U+FFFD)", async () => {
|
|
const payload = sseLf("content_block_delta", {
|
|
type: "content_block_delta",
|
|
index: 0,
|
|
delta: { type: "text_delta", text: "残留" },
|
|
});
|
|
await cjkChunkSplitRoundTrip("anthropic", createAnthropicAdapter({ model: "claude" }), payload);
|
|
});
|
|
|
|
test("openai emitCompletion: usage with absent token counts still serializes complete numeric fields (#dsh)", () => {
|
|
const adapter = createOpenaiAdapter({ model: "deepseek-v4-flash" });
|
|
const out = adapter.emitCompletion({ finishReason: "stop", usage: { inputTokens: undefined, outputTokens: undefined } }).toString("utf8");
|
|
const finishLine = out.split("\n").find((l) => l.startsWith("data: ") && l.includes("finish_reason"));
|
|
assert.ok(finishLine, "finish chunk present");
|
|
const parsed = JSON.parse(finishLine.slice("data: ".length)) as { usage?: Record<string, unknown> };
|
|
assert.ok(parsed.usage, "usage object present");
|
|
assert.equal(parsed.usage!.prompt_tokens, 0, "prompt_tokens defaults to 0 (never dropped)");
|
|
assert.equal(parsed.usage!.completion_tokens, 0, "completion_tokens defaults to 0 (never dropped)");
|
|
assert.equal(parsed.usage!.total_tokens, 0);
|
|
const real = adapter.emitCompletion({ finishReason: "stop", usage: { inputTokens: 7, outputTokens: 3 } }).toString("utf8");
|
|
const realLine = real.split("\n").find((l) => l.startsWith("data: ") && l.includes("finish_reason"));
|
|
const realParsed = JSON.parse(realLine!.slice("data: ".length)) as { usage?: Record<string, unknown> };
|
|
assert.deepEqual({ p: realParsed.usage!.prompt_tokens, c: realParsed.usage!.completion_tokens, t: realParsed.usage!.total_tokens }, { p: 7, c: 3, t: 10 });
|
|
});
|
|
|
|
test("F8 (#549): responses adapter accepts data-only SSE frames (no event: lines)", async () => {
|
|
const stream = mockStream(
|
|
`data: ${JSON.stringify({ type: "response.created", response: { id: "resp_1", status: "in_progress", output: [] } })}\n\n`,
|
|
`data: ${JSON.stringify({ type: "response.output_item.added", output_index: 0, item: { type: "message", id: "m1", role: "assistant", content: [] } })}\n\n`,
|
|
`data: ${JSON.stringify({ type: "response.output_text.delta", item_id: "m1", output_index: 0, delta: "HelloDataOnly" })}\n\n`,
|
|
`data: ${JSON.stringify({ type: "response.completed", response: { id: "resp_1", status: "completed", output: [], usage: { input_tokens: 10, output_tokens: 5 } } })}\n\n`,
|
|
);
|
|
const events = await collectParseEvents(createResponsesAdapter(), stream, 1);
|
|
const textEv = events.find((e) => e.kind === "text");
|
|
assert.ok(textEv, "text delta yielded from data-only frame");
|
|
if (textEv && textEv.kind === "text") assert.equal(textEv.delta, "HelloDataOnly", "delta content intact");
|
|
const done = events.find((e) => e.kind === "done");
|
|
assert.ok(done, "terminal done yielded");
|
|
if (done && done.kind === "done") {
|
|
assert.equal(done.finishReason, "completed", "terminal kind taken from data payload type");
|
|
assert.notEqual(done.truncated, true, "not a synthetic truncation failure");
|
|
}
|
|
});
|
|
|
|
test("F8 (#549): explicit event: line wins over data payload type", async () => {
|
|
const stream = mockStream(
|
|
`event: response.completed\ndata: ${JSON.stringify({ type: "response.output_text.delta", item_id: "m1", output_index: 0, delta: "X" })}\n\n`,
|
|
);
|
|
const events = await collectParseEvents(createResponsesAdapter(), stream, 1);
|
|
const done = events.find((e) => e.kind === "done");
|
|
assert.ok(done, "done yielded");
|
|
if (done && done.kind === "done") assert.equal(done.finishReason, "completed", "explicit event: takes precedence over data type");
|
|
});
|
|
|
|
test("F8 (#549): data-only responses stream completes cleanly end-to-end (no synthetic failure)", async () => {
|
|
const round1 = [
|
|
`data: ${JSON.stringify({ type: "response.created", response: { id: "resp_1", status: "in_progress", output: [] } })}\n\n`,
|
|
`data: ${JSON.stringify({ type: "response.output_item.added", output_index: 0, item: { type: "message", id: "m1", role: "assistant", content: [] } })}\n\n`,
|
|
`data: ${JSON.stringify({ type: "response.output_text.delta", item_id: "m1", output_index: 0, delta: "HelloDataOnly" })}\n\n`,
|
|
`data: ${JSON.stringify({ type: "response.completed", response: { id: "resp_1", status: "completed", output: [], usage: { input_tokens: 10, output_tokens: 5 } } })}\n\n`,
|
|
].join("");
|
|
const out = await drain(
|
|
new Response(round1, { status: 200 }).body!,
|
|
makeCtx("responses-dataonly"),
|
|
createResponsesAdapter(),
|
|
{ model: "gpt-5", input: [], stream: true },
|
|
);
|
|
assert.ok(out.includes("HelloDataOnly"), "text streamed live");
|
|
assert.ok(out.includes('"status":"completed"'), "completion emitted with completed status");
|
|
assert.ok(!out.includes("upstream returned no response"), "no synthetic failure for a valid data-only stream");
|
|
});
|