Skip to content

Commit 0822e8d

Browse files
authored
fix(qa): preserve missing Ollama cache telemetry (#116424)
1 parent 9bebf7a commit 0822e8d

5 files changed

Lines changed: 160 additions & 5 deletions

File tree

‎extensions/ollama/src/stream-runtime.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,19 @@ describe("buildAssistantMessage", () => {
12021202
total: 0,
12031203
});
12041204
});
1205+
1206+
it("records unavailable cache telemetry when Ollama omits the cache split", () => {
1207+
const response = {
1208+
model: "qwen3:32b",
1209+
created_at: "2026-01-01T00:00:00Z",
1210+
message: { role: "assistant" as const, content: "ok" },
1211+
done: true,
1212+
prompt_eval_count: 10,
1213+
eval_count: 2,
1214+
};
1215+
const result = buildAssistantMessage(response, modelInfo);
1216+
expect(result.usage.cacheTelemetry).toEqual({ state: "unavailable" });
1217+
});
12051218
});
12061219

12071220
// Helper: build a ReadableStreamDefaultReader from NDJSON lines

‎extensions/ollama/src/stream.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,17 +561,24 @@ function buildUsageWithNoCost(params: {
561561
output?: number;
562562
cacheRead?: number;
563563
cacheWrite?: number;
564+
cacheTelemetry?: Usage["cacheTelemetry"];
564565
totalTokens?: number;
565566
}): Usage {
566567
const input = params.input ?? 0;
567568
const output = params.output ?? 0;
568569
const cacheRead = params.cacheRead ?? 0;
569570
const cacheWrite = params.cacheWrite ?? 0;
571+
const cacheTelemetry =
572+
params.cacheTelemetry ??
573+
(params.cacheRead !== undefined && params.cacheWrite !== undefined
574+
? { state: "available" as const }
575+
: { state: "unavailable" as const });
570576
return {
571577
input,
572578
output,
573579
cacheRead,
574580
cacheWrite,
581+
cacheTelemetry,
575582
totalTokens: params.totalTokens ?? input + output,
576583
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
577584
};

‎extensions/qa-lab/src/runtime-parity-cache-diagnostics.test.ts‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,114 @@ describe("runtime parity prompt-cache diagnostics", () => {
9898
});
9999
});
100100

101+
it("treats Ollama adapter cache zeros as unavailable telemetry", async () => {
102+
const tempRoot = await seedRuntimeParityCacheTranscript([
103+
{ role: "user", content: "Inspect cache telemetry." },
104+
{
105+
role: "assistant",
106+
content: "done",
107+
api: "ollama",
108+
provider: "ollama",
109+
model: "qwen3.5:9b",
110+
usage: {
111+
input: 22,
112+
output: 7,
113+
total: 29,
114+
cacheRead: 0,
115+
cacheWrite: 0,
116+
cacheTelemetry: { state: "unavailable" },
117+
},
118+
},
119+
]);
120+
121+
const cell = await captureRuntimeParityCell({
122+
runtime: "openclaw",
123+
gateway: { tempRoot },
124+
scenarioResult: { status: "pass" },
125+
wallClockMs: 10,
126+
});
127+
128+
expect(cell.usage).toEqual({
129+
inputTokens: 22,
130+
outputTokens: 7,
131+
totalTokens: 29,
132+
});
133+
expect(cell.cacheDiagnostics).toEqual({
134+
assistantTurns: 1,
135+
cacheTelemetryTurns: 0,
136+
cacheHitTurns: 0,
137+
cacheWriteTurns: 0,
138+
cacheMisses: [],
139+
cacheMissInputTokens: 0,
140+
unmeasuredPostWarmTurns: [],
141+
});
142+
});
143+
144+
it("preserves unavailable cache telemetry from pre-marker Ollama transcripts", async () => {
145+
const tempRoot = await seedRuntimeParityCacheTranscript([
146+
{ role: "user", content: "Inspect cache telemetry." },
147+
{
148+
role: "assistant",
149+
content: "done",
150+
api: "ollama",
151+
provider: "ollama",
152+
model: "qwen3.5:9b",
153+
usage: { input: 22, output: 7, total: 29, cacheRead: 0, cacheWrite: 0 },
154+
},
155+
]);
156+
157+
const cell = await captureRuntimeParityCell({
158+
runtime: "openclaw",
159+
gateway: { tempRoot },
160+
scenarioResult: { status: "pass" },
161+
wallClockMs: 10,
162+
});
163+
164+
expect(cell.usage).toEqual({
165+
inputTokens: 22,
166+
outputTokens: 7,
167+
totalTokens: 29,
168+
});
169+
expect(cell.cacheDiagnostics?.cacheTelemetryTurns).toBe(0);
170+
});
171+
172+
it("preserves an explicitly measured zero-cache result", async () => {
173+
const tempRoot = await seedRuntimeParityCacheTranscript([
174+
{ role: "user", content: "Inspect cache telemetry." },
175+
{
176+
role: "assistant",
177+
content: "done",
178+
api: "ollama",
179+
provider: "ollama",
180+
model: "future-ollama",
181+
usage: {
182+
input: 22,
183+
output: 7,
184+
total: 29,
185+
cacheRead: 0,
186+
cacheWrite: 0,
187+
cacheTelemetry: { state: "available" },
188+
},
189+
},
190+
]);
191+
192+
const cell = await captureRuntimeParityCell({
193+
runtime: "openclaw",
194+
gateway: { tempRoot },
195+
scenarioResult: { status: "pass" },
196+
wallClockMs: 10,
197+
});
198+
199+
expect(cell.usage).toEqual({
200+
inputTokens: 22,
201+
outputTokens: 7,
202+
totalTokens: 29,
203+
cacheRead: 0,
204+
cacheWrite: 0,
205+
});
206+
expect(cell.cacheDiagnostics?.cacheTelemetryTurns).toBe(1);
207+
});
208+
101209
it("identifies the first complete cache miss after a cache-warming write", () => {
102210
const diagnostics = buildRuntimeParityCacheDiagnostics([
103211
usage(3, { cacheRead: 0, cacheWrite: 24_407 }),

‎extensions/qa-lab/src/runtime-parity.ts‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,14 @@ function readUsageTotals(raw: unknown): RuntimeParityUsage {
241241
readFiniteNumber(usage.outputTokens) ??
242242
readFiniteNumber(usage.output_tokens) ??
243243
0;
244-
const cacheRead = readFiniteNumber(usage.cacheRead) ?? readFiniteNumber(usage.cache_read_tokens);
245-
const cacheWrite =
246-
readFiniteNumber(usage.cacheWrite) ?? readFiniteNumber(usage.cache_write_tokens);
244+
const cacheTelemetryUnavailable =
245+
isMessageRecord(usage.cacheTelemetry) && usage.cacheTelemetry.state === "unavailable";
246+
const cacheRead = cacheTelemetryUnavailable
247+
? undefined
248+
: (readFiniteNumber(usage.cacheRead) ?? readFiniteNumber(usage.cache_read_tokens));
249+
const cacheWrite = cacheTelemetryUnavailable
250+
? undefined
251+
: (readFiniteNumber(usage.cacheWrite) ?? readFiniteNumber(usage.cache_write_tokens));
247252
const componentTotal = inputTokens + outputTokens + (cacheRead ?? 0) + (cacheWrite ?? 0);
248253
const totalTokens =
249254
readFiniteNumber(usage.total) ??
@@ -259,6 +264,26 @@ function readUsageTotals(raw: unknown): RuntimeParityUsage {
259264
};
260265
}
261266

267+
function readAssistantUsage(message: Record<string, unknown>): RuntimeParityUsage {
268+
const usage = readUsageTotals(message.usage ?? null);
269+
if (!isMessageRecord(message.usage) || isMessageRecord(message.usage.cacheTelemetry)) {
270+
return usage;
271+
}
272+
const provider = readNonEmptyString(message.provider)?.toLowerCase();
273+
const api = readNonEmptyString(message.api)?.toLowerCase();
274+
if (
275+
(provider === "ollama" || api === "ollama") &&
276+
usage.cacheRead === 0 &&
277+
usage.cacheWrite === 0
278+
) {
279+
// Transcripts written before cacheTelemetry was added contain Ollama's
280+
// required placeholder zeros. Explicit current provenance always wins.
281+
delete usage.cacheRead;
282+
delete usage.cacheWrite;
283+
}
284+
return usage;
285+
}
286+
262287
function addUsage(target: RuntimeParityUsage, next: RuntimeParityUsage) {
263288
target.inputTokens += next.inputTokens;
264289
target.outputTokens += next.outputTokens;
@@ -1002,7 +1027,7 @@ function aggregateUsage(records: RuntimeParityTranscriptRecord[]): RuntimeParity
10021027
if (record.role !== "assistant") {
10031028
continue;
10041029
}
1005-
const usage = readUsageTotals(record.message.usage ?? null);
1030+
const usage = readAssistantUsage(record.message);
10061031
addUsage(totals, usage);
10071032
}
10081033
return totals;
@@ -1474,7 +1499,7 @@ export async function captureRuntimeParityCell(
14741499
cacheDiagnostics: buildRuntimeParityCacheDiagnostics(
14751500
transcriptRecords
14761501
.filter((record) => record.role === "assistant")
1477-
.map((record) => readUsageTotals(record.message.usage ?? null)),
1502+
.map((record) => readAssistantUsage(record.message)),
14781503
),
14791504
wallClockMs: params.wallClockMs,
14801505
...(params.bootstrapWallClockMs === undefined

‎packages/llm-core/src/types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ export interface Usage {
273273
output: number;
274274
cacheRead: number;
275275
cacheWrite: number;
276+
/** Whether the provider reported a cache-read/write token split. */
277+
cacheTelemetry?: { state: "available" | "unavailable" };
276278
/** Subset of `cacheWrite` written with 1-hour retention when reported. */
277279
cacheWrite1h?: number;
278280
/** Exact context snapshot for the final provider iteration. */

0 commit comments

Comments
 (0)