/** * Privacy-safe session / window stats over RoutingTelemetry (SP-207 / #118). * * Aggregates only numeric and categorical telemetry fields — never prompt, * message, or tool-argument bodies. */ import type { ModelProfile, PriceCatalog, RoutingTelemetry, Tier } from '../../domain/types/index.js'; import { type CostCalibrationSample } from '../../domain/routing/expected-cost.js'; export type RoleCostBucket = 'primary' | 'planning_delegate' | 'other'; export type DeploymentClass = 'local' | 'cloud' | 'unknown'; export interface RoleBucketStats { readonly count: number; readonly total_cost_usd: number; } export interface RoleCostBreakdown { readonly primary: RoleBucketStats; readonly planning_delegate: RoleBucketStats; readonly other: RoleBucketStats; } /** * Compact JSON snapshot for automation / MCP (llm-use `stats_snapshot` analog). * Optional `frontier_savings_usd` is omitted when prices are unavailable (fail closed). */ export interface SessionStatsSnapshot { readonly entry_count: number; readonly total_cost_usd: number; readonly mean_cost_usd: number | null; readonly total_latency_ms: number; readonly mean_latency_ms: number | null; /** Share of entries with planning_delegate_path === 'delegate' (0–1), null if empty. */ readonly planning_delegate_share: number | null; /** Share of entries with non-delegate path (direct / none / null). */ readonly direct_share: number | null; /** Share classified as local (zero-tier) when distinguishable; null if none classified. */ readonly local_share: number | null; /** Share classified as cloud when distinguishable; null if none classified. */ readonly cloud_share: number | null; readonly role_cost: RoleCostBreakdown; /** * Entries with host-reported token actuals (SP-241, #164). Token actuals are * recorded even when the host reports no positive USD cost (subscription). */ readonly actual_usage_count: number; /** * Basis of `total_cost_usd` / `mean_cost_usd` (SP-241, #164): * 'actual' when every entry has a host-reported cost, 'estimated' when none * do, 'mixed' otherwise. Operators should treat 'estimated'/'mixed' totals * as estimate-labeled. */ readonly cost_basis: 'actual' | 'estimated' | 'mixed'; /** * Sum of host-reported actual tokens (input + output + cache read/write) * over entries with token actuals; null when no entry has actuals. */ readonly actual_total_tokens: number | null; /** * Estimated USD saved vs always-frontier baseline. * Formula: sum over entries with token counts of * max(0, tokens/1e6 * frontier_cost_per_1m - cost) * where tokens/cost prefer host-reported actuals when present (SP-241). * Omitted when frontier price inputs are missing (fail closed). */ readonly frontier_savings_usd?: number; /** * Warm rolling actual/estimate cost-calibration buckets (SP-242, #164): * per-model and per-tier mean ratios with sample counts, built from * SP-241 usage actuals. Omitted when no bucket is warm (cold → catalog; * fail closed, same convention as frontier_savings_usd). */ readonly cost_calibration?: readonly CostCalibrationSample[]; } export interface AggregateSessionStatsOptions { /** * USD per 1M tokens for the always-frontier baseline. * When absent / non-finite / ≤0, `frontier_savings_usd` is omitted. */ readonly frontier_cost_per_1m?: number; /** Optional model_id → tier map (e.g. from fleet) for local vs cloud. */ readonly tier_by_model_id?: ReadonlyMap; } /** Mutually exclusive role for cost bucketing. */ export declare function classifyRoleCostBucket(entry: RoutingTelemetry): RoleCostBucket; export declare function classifyDeployment(entry: RoutingTelemetry, tierByModelId?: ReadonlyMap): DeploymentClass; /** * Per-entry USD cost (SP-241, #164): prefer the host-reported actual when * present; fall back to the routing-time estimate. Non-finite values count as 0. */ export declare function resolveEntryCostUsd(entry: RoutingTelemetry): number; /** True when the entry carries a host-reported actual USD cost (SP-241). */ export declare function hasActualCost(entry: RoutingTelemetry): boolean; /** * Host-reported actual total tokens (input + output + cache) for an entry * (SP-241); null when the host reported no token actuals. */ export declare function resolveEntryActualTokens(entry: RoutingTelemetry): number | null; /** * Resolve always-frontier cost/1M from fleet + optional catalog. * Returns undefined when no positive frontier price is available (fail closed). */ export declare function resolveFrontierCostPer1M(fleet?: readonly ModelProfile[], catalog?: PriceCatalog | null): number | undefined; /** * Pure aggregate over routing telemetry for operator stats. * Does not read or emit prompt/message/tool bodies. */ export declare function aggregateSessionStats(entries: readonly RoutingTelemetry[], options?: AggregateSessionStatsOptions): SessionStatsSnapshot; /** * Optional vs-always-frontier savings. Returns undefined when price input is * missing or non-positive (fail closed) or when no entry has token counts. */ export declare function estimateFrontierSavingsUsd(entries: readonly RoutingTelemetry[], frontierCostPer1M: number | undefined): number | undefined; /** Convenience: aggregate with fleet/catalog-derived tier map + frontier price. */ export declare function aggregateSessionStatsFromFleet(entries: readonly RoutingTelemetry[], fleet?: readonly ModelProfile[], catalog?: PriceCatalog | null): SessionStatsSnapshot; /** * Flatten a warm calibration prior into a deterministic, privacy-safe list: * model buckets first, then tier buckets, each sorted by key (SP-242). */ export declare function costCalibrationSamplesFromPrior(prior: CostCalibrationPriorLike): readonly CostCalibrationSample[]; interface CostCalibrationPriorLike { readonly byModel: ReadonlyMap; readonly byTier: ReadonlyMap; } /** Keys that must never appear on a stats snapshot (privacy). */ export declare const SESSION_STATS_FORBIDDEN_KEYS: readonly ["prompt", "prompt_text", "messages", "content", "tool_calls", "tool_args", "pepper"]; export declare function assertSessionStatsPrivacySafe(snapshot: SessionStatsSnapshot): void; export {}; //# sourceMappingURL=session-stats.d.ts.map