/** * Offline token-estimation accuracy benchmark (phase 4, TOKN-01..03). * * A benchmark-only pure library: no network, no configuration loading, no provider * credentials, no persistence. The authoritative count always arrives through an * injected transport and is only accepted when it is a finite positive integer * provider-reported input count that is not marked estimated and carries no cache * detail (the shared proxy usage convention folds cache reads/writes into the * input total; report that total, never a cache-split residual). * * Report DTOs are closed allowlists: fixture id, schema version, canonical digest, * state, metrics, and the benchmark caller's safe metadata (provider kind + model * id) only. Fixture payload text and generated base64 bytes never cross a * serialization boundary. */ import { estimateClaudeRequestTokens } from "../server/claude-messages"; /** Version of the committed synthetic fixture schema. */ export const TOKEN_BENCHMARK_FIXTURE_SCHEMA_VERSION = 1; /** Version of the emitted benchmark report schema. */ export const TOKEN_BENCHMARK_REPORT_SCHEMA_VERSION = 1; /** Bounded generator output: image/document fixtures stay small and inert. */ const MAX_GENERATED_BASE64_BYTES = 4096; /** Fixture tolerance is the hybrid rule agreed for phase 4. */ const FIXTURE_ABSOLUTE_TOLERANCE_FLOOR = 32; const FIXTURE_RELATIVE_TOLERANCE = 0.20; const WEIGHTED_ABSOLUTE_ERROR_LIMIT = 0.10; /** Decimal places used for JSON/human rendering of fractional metrics. */ const METRIC_DECIMALS = 6; /** Shape the local estimator accepts (an Anthropic Messages request body). */ export type ClaudeFixtureBody = { system?: unknown; messages?: unknown; tools?: unknown; }; /** Inline replacement spec for inert base64 bytes generated at runtime. */ export interface FixtureInlineBase64 { /** Deterministic PRNG seed for the generated bytes. */ readonly seed: number; /** Number of underlying bytes to generate before base64 encoding (bounded). */ readonly byteLength: number; } /** Declarative synthetic fixture descriptor. Payloads stay tiny and synthetic. */ export interface TokenBenchmarkFixture { readonly id: string; readonly category: | "text" | "system" | "tools" | "tool_results" | "documents" | "thinking" | "image_metadata" | "mixed"; /** Anthropic-shaped body; image/document sources may inline a marker object. */ readonly body: ClaudeFixtureBody; } /** * Injected authoritative-count transport. The production implementation (the * phase 4 script) resolves the live provider/model target; tests always inject a * fake. It must not retry and must not scrape rendered output — only typed * outcomes cross the seam. */ export type AuthoritativeCountTransport = ( input: AuthoritativeCountCall, ) => AuthoritativeCountResult; export interface AuthoritativeCountCall { readonly fixtureId: string; readonly fixtureSchemaVersion: number; /** SHA-256 digest over the canonical materialized body bytes. */ readonly fixtureDigest: string; readonly materialized: ClaudeFixtureBody; readonly modelId: string; } /** * Provider-reported usage provenance, aligned with the shared OcxUsage * convention. inputTokens must be the total the provider reported for the * request (cache reads/writes already folded in). estimated: true marks * heuristic usage (e.g. Cursor/Kiro adapters) and is rejected as evidence. */ export interface AuthoritativeUsage { readonly inputTokens: number; readonly estimated?: boolean; readonly cachedInputTokens?: number; readonly cacheReadInputTokens?: number; readonly cacheCreationInputTokens?: number; } export type AuthoritativeCountResult = | { readonly state: "supported"; readonly usage: AuthoritativeUsage } | { readonly state: "unsupported"; readonly detail?: string } | { readonly state: "failed"; readonly error?: unknown }; /** Distinct typed outcomes per fixture. */ export type TokenBenchmarkFixtureState = "supported" | "unsupported" | "failed"; export interface TokenBenchmarkRunOptions { /** Safe metadata allowed into reports (model id, never an alias/endpoint). */ readonly modelId: string; /** Safe metadata allowed into reports (stable adapter/provider family). */ readonly providerKind?: string; /** Decimal places for fractional metrics in rendered reports. */ readonly metricDecimals?: number; } export interface TokenBenchmarkFixtureRow { readonly state: TokenBenchmarkFixtureState; /** Stable fixture identifier. */ readonly id: string; /** Manifest schema the fixture materialized against. */ readonly schemaVersion: number; /** SHA-256 hex over the canonical materialized payload bytes. */ readonly digest: string; /** Present only for supported state. */ readonly metrics?: TokenBenchmarkMetrics; /** Present only for unsupported state: coarse classification, never raw text. */ readonly unsupportedReason?: | "transport_declined" | "estimated_usage" | "malformed_usage" | "cache_usage_present"; /** Present only for failed state: fixed classification, never error details. */ readonly failureReason?: "transport_failed"; } export interface TokenBenchmarkMetrics { /** Local estimator output for the materialized body. */ readonly localTokens: number; /** Provider-reported authoritative input tokens. */ readonly authoritativeTokens: number; /** localTokens - authoritativeTokens. */ readonly signedError: number; readonly absoluteError: number; /** absoluteError / authoritativeTokens (0 when authoritative is 0). */ readonly relativeError: number; /** max(32, authoritative * 0.20). */ readonly absoluteTolerance: number; /** Fixture pass: absoluteError <= absoluteTolerance. */ readonly passed: boolean; } export interface TokenBenchmarkReport { readonly reportSchemaVersion: number; readonly fixtureSchemaVersion: number; readonly modelId: string; readonly providerKind: string; readonly status: "pass" | "incomplete"; readonly fixtures: readonly TokenBenchmarkFixtureRow[]; readonly summary: TokenBenchmarkRunSummary; } export interface TokenBenchmarkRunSummary { readonly fixtureCount: number; readonly supportedCount: number; readonly unsupportedCount: number; readonly failedCount: number; readonly allSupportedPassed: boolean; /** sum(absError) / sum(authoritative) across supported fixtures only. */ readonly weightedAbsoluteError: number; readonly weightedErrorWithinLimit: boolean; } /** Deterministic PRNG (xorshift32) seeded from the declared spec. */ function seededBytes(seed: number, byteLength: number): Uint8Array { const clamped = Math.max(0, Math.min(MAX_GENERATED_BASE64_BYTES, Math.floor(byteLength))); const out = new Uint8Array(clamped); let s = (seed >>> 0) || 0x9e3779b9; for (let i = 0; i < clamped; i++) { s ^= s << 13; s >>>= 0; s ^= s >>> 17; s ^= s << 5; s >>>= 0; out[i] = s & 0xff; } return out; } /** Base64-encode inert bytes without Buffer or platform-specific codecs. */ function toBase64(bytes: Uint8Array): string { let binary = ""; for (const byte of bytes) binary += String.fromCharCode(byte); return btoa(binary); } /** Replace the inline marker spec with deterministic inert base64 data. */ function materializeValue(value: unknown): unknown { if (Array.isArray(value)) return value.map(materializeValue); if (value && typeof value === "object") { const rec = value as Record; const spec = rec.__fixtureBase64; if (spec && typeof spec === "object" && !Array.isArray(spec)) { const typed = spec as { seed?: unknown; byteLength?: unknown }; return toBase64( seededBytes( typeof typed.seed === "number" ? typed.seed : 0, typeof typed.byteLength === "number" ? typed.byteLength : 0, ), ); } const out: Record = {}; for (const [key, val] of Object.entries(rec)) { if (key === "__fixtureBase64") continue; out[key] = materializeValue(val); } return out; } return value; } /** Materialize a descriptor into the exact Anthropic-shaped body the estimator sees. */ export function materializeFixture(fixture: TokenBenchmarkFixture): ClaudeFixtureBody { return materializeValue(fixture.body) as ClaudeFixtureBody; } /** Canonical JSON: object keys sorted, arrays ordered, undefined values dropped. */ function canonicalJsonString(value: unknown): string { if (Array.isArray(value)) return "[" + value.map(canonicalJsonString).join(",") + "]"; if (value && typeof value === "object") { const entries = Object.entries(value as Record) .filter(([, v]) => v !== undefined) .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)); return "{" + entries.map(([k, v]) => JSON.stringify(k) + ":" + canonicalJsonString(v)).join(",") + "}"; } return JSON.stringify(value); } /** SHA-256 hex over the canonical materialized body bytes. */ export function canonicalFixtureDigest(materialized: ClaudeFixtureBody): string { const hasher = new Bun.CryptoHasher("sha256"); hasher.update(canonicalJsonString(materialized)); return hasher.digest("hex"); } /** * Acceptable authoritative evidence: a finite, positive integer input count. * Zero, negative, non-finite, and non-numeric usages cannot back a real * non-empty request, so they count as malformed evidence. */ function isAcceptableAuthoritativeInput(value: unknown): value is number { return typeof value === "number" && Number.isFinite(value) && Number.isInteger(value) && value >= 1; } function roundTo(value: number, decimals: number): number { const factor = 10 ** decimals; return Math.round(value * factor) / factor; } function toleranceFor(authoritative: number): number { return Math.max(FIXTURE_ABSOLUTE_TOLERANCE_FLOOR, authoritative * FIXTURE_RELATIVE_TOLERANCE); } function relativeErrorFor(absolute: number, authoritative: number): number { if (authoritative <= 0) return absolute === 0 ? 0 : Number.POSITIVE_INFINITY; return absolute / authoritative; } function metricsFor(local: number, authoritative: number, decimals: number): TokenBenchmarkMetrics { const signed = local - authoritative; const absolute = Math.abs(signed); const tolerance = toleranceFor(authoritative); return { localTokens: local, authoritativeTokens: authoritative, signedError: signed, absoluteError: absolute, relativeError: roundTo(relativeErrorFor(absolute, authoritative), decimals), absoluteTolerance: tolerance, passed: absolute <= tolerance, }; } /** * Run the offline benchmark. The local estimator runs exactly once per * materialized fixture and the injected transport exactly once; there is no * retry, concurrency, or fallback. Failures are typed, never retried, and never * converted into passing evidence. */ export function runTokenBenchmark( fixtures: readonly TokenBenchmarkFixture[], transport: AuthoritativeCountTransport, options: TokenBenchmarkRunOptions, ): TokenBenchmarkReport { const decimals = Math.max(0, Math.floor(options.metricDecimals ?? METRIC_DECIMALS)); const modelId = options.modelId; const providerKind = options.providerKind ?? "provider"; const ordered = [...fixtures].sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)); const rows: TokenBenchmarkFixtureRow[] = ordered.map((fixture) => { let digest = ""; try { const materialized = materializeFixture(fixture); digest = canonicalFixtureDigest(materialized); // The local count is the exact single estimation call under measurement. const local = estimateClaudeRequestTokens(materialized, modelId); const result = transport({ fixtureId: fixture.id, fixtureSchemaVersion: TOKEN_BENCHMARK_FIXTURE_SCHEMA_VERSION, fixtureDigest: digest, materialized, modelId, }); if (result.state === "failed") { return failedRow(fixture.id, digest); } if (result.state === "unsupported") { return unsupportedRow(fixture.id, digest, "transport_declined"); } const usage = result.usage; if (!usage || typeof usage !== "object") { return unsupportedRow(fixture.id, digest, "malformed_usage"); } if (usage.estimated === true) { return unsupportedRow(fixture.id, digest, "estimated_usage"); } const cacheFields = [usage.cachedInputTokens, usage.cacheReadInputTokens, usage.cacheCreationInputTokens]; // Cache detail is deliberately closed for this benchmark. Any present value // must be a finite zero; non-zero *or malformed* detail cannot silently alter // the inclusive inputTokens convention. if (cacheFields.some(v => v !== undefined && (typeof v !== "number" || !Number.isFinite(v) || v !== 0))) { return unsupportedRow(fixture.id, digest, "cache_usage_present"); } if (!isAcceptableAuthoritativeInput(usage.inputTokens)) { return unsupportedRow(fixture.id, digest, "malformed_usage"); } return { state: "supported", id: fixture.id, schemaVersion: TOKEN_BENCHMARK_FIXTURE_SCHEMA_VERSION, digest, metrics: metricsFor(local, usage.inputTokens, decimals), }; } catch { // Transport/materialization failures are typed with no error text exposed. return failedRow(fixture.id, digest); } }); const supportedRows = rows.filter(r => r.state === "supported" && r.metrics); const absSum = supportedRows.reduce((acc, r) => acc + (r.metrics?.absoluteError ?? 0), 0); const authSum = supportedRows.reduce((acc, r) => acc + (r.metrics?.authoritativeTokens ?? 0), 0); const weighted = authSum > 0 ? absSum / authSum : absSum === 0 ? 0 : Number.POSITIVE_INFINITY; const allSupportedPassed = supportedRows.length > 0 && supportedRows.every(r => r.metrics?.passed === true); const failedCount = rows.filter(r => r.state === "failed").length; const status: "pass" | "incomplete" = failedCount === 0 && allSupportedPassed && supportedRows.length > 0 && weighted <= WEIGHTED_ABSOLUTE_ERROR_LIMIT ? "pass" : "incomplete"; return { reportSchemaVersion: TOKEN_BENCHMARK_REPORT_SCHEMA_VERSION, fixtureSchemaVersion: TOKEN_BENCHMARK_FIXTURE_SCHEMA_VERSION, modelId, providerKind, status, fixtures: rows, summary: { fixtureCount: rows.length, supportedCount: supportedRows.length, unsupportedCount: rows.filter(r => r.state === "unsupported").length, failedCount, allSupportedPassed, weightedAbsoluteError: roundTo(weighted, decimals), weightedErrorWithinLimit: supportedRows.length > 0 && weighted <= WEIGHTED_ABSOLUTE_ERROR_LIMIT, }, }; } function failedRow(id: string, digest: string): TokenBenchmarkFixtureRow { return { state: "failed", id, schemaVersion: TOKEN_BENCHMARK_FIXTURE_SCHEMA_VERSION, digest, failureReason: "transport_failed" }; } function unsupportedRow( id: string, digest: string, reason: NonNullable, ): TokenBenchmarkFixtureRow { return { state: "unsupported", id, schemaVersion: TOKEN_BENCHMARK_FIXTURE_SCHEMA_VERSION, digest, unsupportedReason: reason }; } /** * Committed synthetic fixture set: exactly one focused fixture per category * plus the mixed envelope. Payloads are tiny, deterministic, and contain no * captured requests, base64 blobs, or provider-specific strings. */ export const defaultTokenBenchmarkFixtureSet: readonly TokenBenchmarkFixture[] = [ { id: "text-tracer", category: "text", body: { messages: [{ role: "user", content: "Synthetic offline benchmark tracer text for token estimation." }], }, }, { id: "system-blocks", category: "system", body: { system: "You are a meticulous offline benchmark harness that never sends data anywhere.", messages: [{ role: "user", content: "Summarize the offline benchmark purpose briefly." }], }, }, { id: "tools-list", category: "tools", body: { messages: [{ role: "user", content: "List offline benchmark fixture statistics." }], tools: [ { name: "get_fixture_stat", description: "Return a synthetic offline benchmark fixture statistic.", input_schema: { type: "object", properties: { id: { type: "string", description: "Fixture identifier." } }, required: ["id"], }, }, ], }, }, { id: "tool-results", category: "tool_results", body: { messages: [ { role: "assistant", content: [ { type: "tool_use", id: "toolu_01", name: "get_fixture_stat", input: { id: "text-tracer" } }, ], }, { role: "user", content: [ { type: "tool_result", tool_use_id: "toolu_01", content: "fixture statistic: 42 tokens counted offline" }, ], }, ], }, }, { id: "documents-pdf", category: "documents", body: { messages: [ { role: "user", content: [ { type: "document", source: { type: "base64", media_type: "application/pdf", data: { __fixtureBase64: { seed: 41, byteLength: 512 } } }, }, ], }, ], }, }, { id: "thinking-blocks", category: "thinking", body: { messages: [ { role: "assistant", content: [{ type: "thinking", thinking: "offline synthetic reasoning trace about fixture sizing" }], }, { role: "user", content: "Proceed with the offline benchmark." }, ], }, }, { id: "image-metadata", category: "image_metadata", body: { messages: [ { role: "user", content: [ { type: "image", source: { type: "base64", media_type: "image/png", data: { __fixtureBase64: { seed: 7, byteLength: 256 } } }, }, ], }, ], }, }, { id: "mixed-envelope", category: "mixed", body: { system: "Mixed envelope fixture for the offline benchmark.", messages: [ { role: "user", content: [ { type: "text", text: "Mixed envelope: offline benchmark tracer payload." }, { type: "image", source: { type: "base64", media_type: "image/png", data: { __fixtureBase64: { seed: 11, byteLength: 128 } } } }, { type: "document", source: { type: "base64", media_type: "application/pdf", data: { __fixtureBase64: { seed: 13, byteLength: 128 } } } }, ], }, { role: "assistant", content: [{ type: "tool_use", id: "toolu_02", name: "get_fixture_stat", input: { id: "mixed-envelope" } }], }, { role: "user", content: [{ type: "tool_result", tool_use_id: "toolu_02", content: "mixed envelope fixture statistic" }], }, ], tools: [ { name: "get_fixture_stat", description: "Return a synthetic fixture statistic.", input_schema: { type: "object", properties: {}, required: [] } }, ], }, }, ]; /** * Allowlisted report DTO: safe fields only, stable key order, no payload text, * no transport error contents, no credentials, aliases, or account identifiers. */ export function serializeBenchmarkReport(report: TokenBenchmarkReport): string { const dtos = [...report.fixtures] .sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)) .map(row => { const base = { id: row.id, schemaVersion: row.schemaVersion, digest: row.digest, state: row.state, }; if (row.state === "supported" && row.metrics) { const m = row.metrics; return { ...base, metrics: { localTokens: m.localTokens, authoritativeTokens: m.authoritativeTokens, signedError: m.signedError, absoluteError: m.absoluteError, relativeError: roundTo(m.relativeError, METRIC_DECIMALS), absoluteTolerance: m.absoluteTolerance, passed: m.passed, }, }; } if (row.state === "unsupported") { return { ...base, unsupportedReason: row.unsupportedReason ?? "malformed_usage" }; } return { ...base, failureReason: row.failureReason ?? "transport_failed" }; }); const s = report.summary; const dto = { reportSchemaVersion: report.reportSchemaVersion, fixtureSchemaVersion: report.fixtureSchemaVersion, modelId: report.modelId, providerKind: report.providerKind, status: report.status, summary: { fixtureCount: s.fixtureCount, supportedCount: s.supportedCount, unsupportedCount: s.unsupportedCount, failedCount: s.failedCount, allSupportedPassed: s.allSupportedPassed, weightedAbsoluteError: roundTo(s.weightedAbsoluteError, METRIC_DECIMALS), weightedErrorWithinLimit: s.weightedErrorWithinLimit, }, fixtures: dtos, }; return JSON.stringify(dto, null, 2); } /** Deterministic human rendering from the same closed DTO. */ export function formatBenchmarkReport(report: TokenBenchmarkReport): string { const lines: string[] = []; lines.push(`Token estimation accuracy benchmark — report schema ${report.reportSchemaVersion}`); lines.push(`provider kind: ${report.providerKind} model: ${report.modelId}`); const s = report.summary; lines.push( `fixtures: ${s.fixtureCount} supported: ${s.supportedCount} unsupported: ${s.unsupportedCount} failed: ${s.failedCount}`, ); lines.push( `weighted absolute error: ${s.weightedAbsoluteError} within 10% limit: ${s.weightedErrorWithinLimit}`, ); lines.push(`status: ${report.status}`); for (const row of [...report.fixtures].sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0))) { const head = `- ${row.id} [${row.state}] digest=${row.digest}`; if (row.state === "supported" && row.metrics) { const m = row.metrics; lines.push(`${head} local=${m.localTokens} authoritative=${m.authoritativeTokens} absErr=${m.absoluteError} tol=${m.absoluteTolerance} passed=${m.passed}`); } else if (row.state === "unsupported") { lines.push(`${head} reason=${row.unsupportedReason ?? "malformed_usage"}`); } else { lines.push(`${head} reason=${row.failureReason ?? "transport_failed"}`); } } return lines.join("\n"); }