diff --git a/changelog.d/fixes/19056-compression-analytics-engine-label.md b/changelog.d/fixes/19056-compression-analytics-engine-label.md new file mode 100644 index 00000000000..d8c9da32d19 --- /dev/null +++ b/changelog.d/fixes/19056-compression-analytics-engine-label.md @@ -0,0 +1 @@ +- **fix(compression): keep the dispatched engine in analytics for multi-engine and skip rows**: the configured-mode label from #19012 now replaces the engine only when a single step ran. A combo that ran several engines, and the "no savings" skip rows, keep the engine the pipeline actually used. diff --git a/open-sse/handlers/chatCore/compressionAnalyticsWrite.ts b/open-sse/handlers/chatCore/compressionAnalyticsWrite.ts index 042efc282a5..088d6e57406 100644 --- a/open-sse/handlers/chatCore/compressionAnalyticsWrite.ts +++ b/open-sse/handlers/chatCore/compressionAnalyticsWrite.ts @@ -59,6 +59,18 @@ function buildRtkPointerFields(rtkPointers: RtkPointer[]) { }; } +/** + * Engine label of a saving row. The configured-mode label only replaces the dispatched engine + * when a single step ran (the lite wrapper); a multi-engine run, such as a combo that took over + * the plan, keeps the engine the pipeline reported. + */ +function analyticsEngine(opts: WriteOpts): string { + const { stats } = opts; + const steps = stats.engineBreakdown?.length ?? 0; + if (opts.reportedMode && steps <= 1) return opts.reportedMode; + return stats.engine ?? opts.mode; +} + function buildAnalyticsRow( opts: WriteOpts, tokensSaved: number, @@ -71,7 +83,7 @@ function buildAnalyticsRow( combo_id: opts.comboName ?? null, provider: opts.provider ?? null, mode: opts.reportedMode ?? opts.mode, - engine: opts.reportedMode ?? stats.engine ?? opts.mode, + engine: analyticsEngine(opts), compression_combo_id: stats.compressionComboId ?? opts.compressionComboId ?? null, original_tokens: stats.originalTokens, compressed_tokens: stats.compressedTokens, @@ -115,7 +127,9 @@ export function writeCompressionSkip(opts: WriteOpts, skipReason: string): Promi combo_id: opts.comboName ?? null, provider: opts.provider ?? null, mode: opts.reportedMode ?? opts.mode, - engine: opts.reportedMode ?? stats.engine ?? opts.mode, + // Skip rows keep the dispatched engine: they carry no per-engine breakdown, and the + // legacy per-engine totals count them under whatever engine they name. + engine: stats.engine ?? opts.mode, compression_combo_id: stats.compressionComboId ?? opts.compressionComboId ?? null, original_tokens: stats.originalTokens, compressed_tokens: stats.compressedTokens, diff --git a/tests/unit/chatcore-compression-analytics-write.test.ts b/tests/unit/chatcore-compression-analytics-write.test.ts index 69602ba15ce..b22a0c5b4cf 100644 --- a/tests/unit/chatcore-compression-analytics-write.test.ts +++ b/tests/unit/chatcore-compression-analytics-write.test.ts @@ -12,7 +12,7 @@ const testDataDir = fs.mkdtempSync(path.join(os.tmpdir(), "omni-comp-analytics-t process.env.DATA_DIR = testDataDir; const coreDb = await import("../../src/lib/db/core.ts"); -const { writeCompressionAnalytics } = +const { writeCompressionAnalytics, writeCompressionSkip } = await import("../../open-sse/handlers/chatCore/compressionAnalyticsWrite.ts"); type Stats = Parameters[0]["stats"]; @@ -115,6 +115,39 @@ test("a wrapped plan records the configured mode as mode and engine", async () = assert.equal(row!.engine, "lite"); }); +test("a multi-engine run keeps the dispatched engine even with a reportedMode", async () => { + // A combo that took over the lite plan ran several engines: mode stays the configured lite, + // but the engine must not claim that only lite ran. + await writeCompressionAnalytics({ + ...baseOpts("ca-req-combo", { + engine: "stacked", + engineBreakdown: [ + { engine: "session-dedup", originalTokens: 200, compressedTokens: 150, durationMs: 2 }, + { engine: "lite", originalTokens: 150, compressedTokens: 80, durationMs: 3 }, + ], + }), + mode: "stacked", + reportedMode: "lite", + }); + const row = analyticsRow("ca-req-combo"); + assert.equal(row!.mode, "lite"); + assert.equal(row!.engine, "stacked"); +}); + +test("a skip row keeps the dispatched engine and reports the configured mode", async () => { + await writeCompressionSkip( + { + ...baseOpts("ca-req-skip", { engine: "stacked" }), + mode: "stacked", + reportedMode: "lite", + }, + "no_savings" + ); + const row = analyticsRow("ca-req-skip"); + assert.equal(row!.mode, "lite"); + assert.equal(row!.engine, "stacked"); +}); + test("without reportedMode the dispatched mode and engine are recorded as before", async () => { await writeCompressionAnalytics({ ...baseOpts("ca-req-stacked", { engine: "stacked" }),