Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/loop/adapter-anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,28 @@ export function createAnthropicAdapter(requestBody: Record<string, unknown>, ori
return Object.keys(out).length > 0 ? out : undefined;
};

// #4072: the cache_creation breakdown object (e.g. { ephemeral_5m_input_tokens })
// rides inside a frame's `usage`. Plain-object guard only — values are passed
// through verbatim, never synthesized (the upstream knows whether the write was
// 5m or 1h).
const cacheCreationOf = (u: Record<string, unknown>): Record<string, unknown> | undefined => {
const cc = u["cache_creation"];
return cc !== null && typeof cc === "object" && !Array.isArray(cc) ? (cc as Record<string, unknown>) : undefined;
};
// Same relays that echo `input_tokens: 0` in an incomplete message_delta usage
// (#660) may echo a schema-shaped all-zero breakdown — adopting it would clobber
// the real start-frame values. Mirror the numerics' adoption rule: only a
// breakdown carrying tokens is an update.
const cacheCreationMeaningful = (cc: Record<string, unknown>): boolean =>
Object.values(cc).some((v) => typeof v === "number" && v > 0);

const buildTerminal = (
stopReason: string,
outputTokens: number,
inputTokens: number,
cachedTokens: number,
creationTokens?: number,
cacheCreation?: Record<string, unknown>,
extras?: Record<string, unknown>,
): Buffer => {
const usage: Record<string, unknown> = {
Expand All @@ -254,6 +270,11 @@ export function createAnthropicAdapter(requestBody: Record<string, unknown>, ori
cache_read_input_tokens: cachedTokens,
};
if (typeof creationTokens === "number") usage.cache_creation_input_tokens = creationTokens;
// #4072: the breakdown's only upstream home is message_start, which is
// forwarded usage-neutral (#1315) — re-carry it here or hosts deriving
// their prompt-cache TTL from it (Claude Code VS Code countdown icon)
// lose it. Absent when no frame carried one; never synthesized.
if (cacheCreation) usage.cache_creation = cacheCreation;
const extra: Record<string, unknown> = {};
// #2701: report the thread tail (latest round's id) when present — on a
// threads turn the client's next continue must target it, not round 1's id.
Expand Down Expand Up @@ -294,6 +315,11 @@ export function createAnthropicAdapter(requestBody: Record<string, unknown>, ori
let roundInput: number | undefined;
let roundCached: number | undefined;
let roundCreation: number | undefined;
// #4072: verbatim cache_creation breakdown for THIS round's frames —
// like the numerics above, the final round's value is what the
// synthetic terminal carries (stitched turns take the post-fold
// round, matching how the totals are chosen).
let roundCacheCreation: Record<string, unknown> | undefined;
let roundOutput: number | undefined;
let stopReason: string | undefined;
let usageYielded = false;
Expand Down Expand Up @@ -408,6 +434,11 @@ export function createAnthropicAdapter(requestBody: Record<string, unknown>, ori
if (typeof u.input_tokens === "number") roundInput = u.input_tokens;
if (typeof u.cache_read_input_tokens === "number") roundCached = u.cache_read_input_tokens;
if (typeof u.cache_creation_input_tokens === "number") roundCreation = u.cache_creation_input_tokens;
// #4072: the breakdown lives in exactly this frame — capture
// it before neutralizeStartUsage scrubs the usage object from
// the forwarded copy below.
const startCacheCreation = cacheCreationOf(u);
if (startCacheCreation) roundCacheCreation = startCacheCreation;
// #1455: suppression keys off ACTUAL forwarding state, not the round
// parameter — a re-fetched stream (blind truncation retry) re-parses
// at the SAME round, and re-emitting the start frame would hand the
Expand Down Expand Up @@ -605,6 +636,8 @@ export function createAnthropicAdapter(requestBody: Record<string, unknown>, ori
roundInput = input;
roundCached = cached;
if (creation !== undefined) roundCreation = creation;
const completeCacheCreation = cacheCreationOf(u);
if (completeCacheCreation) roundCacheCreation = completeCacheCreation;
} else {
// Incomplete usage object: the input context is FIXED within
// a turn, so message_start is authoritative for input/cache
Expand All @@ -617,6 +650,10 @@ export function createAnthropicAdapter(requestBody: Record<string, unknown>, ori
if (input !== undefined && input > 0) roundInput = input;
if (cached !== undefined && cached > 0) roundCached = cached;
if (creation !== undefined && creation > 0) roundCreation = creation;
const incompleteCacheCreation = cacheCreationOf(u);
if (incompleteCacheCreation && cacheCreationMeaningful(incompleteCacheCreation)) {
roundCacheCreation = incompleteCacheCreation;
}
}
const d = (data.delta ?? {}) as Record<string, unknown>;
if (typeof d.stop_reason === "string") stopReason = d.stop_reason;
Expand All @@ -629,6 +666,7 @@ export function createAnthropicAdapter(requestBody: Record<string, unknown>, ori
outputTokens: roundOutput,
cachedTokens: roundCached,
creationTokens: roundCreation,
cacheCreation: roundCacheCreation,
} as ParsedStreamEvent;
}
maybeWarnDegenerate(stopReason);
Expand All @@ -652,6 +690,7 @@ export function createAnthropicAdapter(requestBody: Record<string, unknown>, ori
outputTokens: roundOutput,
cachedTokens: roundCached,
creationTokens: roundCreation,
cacheCreation: roundCacheCreation,
} as ParsedStreamEvent;
}
maybeWarnDegenerate(stopReason);
Expand Down Expand Up @@ -696,6 +735,7 @@ export function createAnthropicAdapter(requestBody: Record<string, unknown>, ori
opts?.usage?.inputTokens ?? 0,
opts?.usage?.cachedTokens ?? 0,
opts?.usage?.creationTokens,
opts?.usage?.cacheCreation,
opts?.terminalExtra,
);
},
Expand Down
12 changes: 9 additions & 3 deletions src/loop/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,12 @@ export type ParsedStreamEvent =
// dropped there, the rebuilt history loses the signature association.
| { kind: "standalone_signature"; signature: string }
| { kind: "tool_call"; name: string; callId: string; arguments: string; passthrough?: boolean; signature?: string }
| { kind: "usage"; inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number }
// #4072: cacheCreation carries the upstream's cache_creation breakdown
// object (e.g. { ephemeral_5m_input_tokens }) verbatim — Anthropic puts it
// in message_start, which the anthropic adapter forwards usage-neutral
// (#1315), so only the synthetic terminal can deliver it to hosts that
// derive their prompt-cache TTL from the breakdown.
| { kind: "usage"; inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number; cacheCreation?: Record<string, unknown> }
// #1766: keys on the upstream terminal frame that bili does not model
// (e.g. Anthropic's safeguard_results) — the loop replaces that frame with
// a synthetic completion, so anything not carried here vanishes from the
Expand All @@ -317,7 +322,7 @@ export type ParsedStreamEvent =

export interface EmitCompletionOpts {
finishReason?: string;
usage?: { inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number };
usage?: { inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number; cacheCreation?: Record<string, unknown> };
terminalExtra?: Record<string, unknown>;
}

Expand Down Expand Up @@ -691,7 +696,7 @@ export async function* runCompressLoop(
const roundParts: RoundPart[] = [];
let reasoningSealed = true;
const calls: ToolCallEmit[] = [];
let usage: { inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number } = {};
let usage: { inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number; cacheCreation?: Record<string, unknown> } = {};
let finishReason: string | undefined;
let streamError: string | undefined;
let sawDone = false;
Expand Down Expand Up @@ -816,6 +821,7 @@ export async function* runCompressLoop(
outputTokens: ev.outputTokens,
cachedTokens: ev.cachedTokens,
creationTokens: ev.creationTokens,
cacheCreation: ev.cacheCreation,
};
} else if (ev.kind === "done") {
sawDone = true;
Expand Down
94 changes: 92 additions & 2 deletions tests/loop-adapters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@ test("anthropic adapter (#1310): forwarded message_start is usage-neutral — th
// #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): non-stitched turn — zeroed start, identical final usage via terminal", async () => {
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 } } })}\n\n`,
`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`,
Expand All @@ -354,6 +354,96 @@ test("anthropic adapter (#1310): non-stitched turn — zeroed start, identical f
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 () => {
Expand Down
Loading