/** * Folding a decoded session log into usage numbers. * * The bucket arithmetic is a deliberate line-by-line port of DSH's own * `tokenUsageProjectionDefinition` (`@deepseek-ai/dsh-token-meter`, * `src/usage-projection.ts`). Two rules from that projection are the whole * reason this module exists rather than a simpler sum: * * - A later settlement in the SAME `(turn, step)` REPLACES the earlier * sample instead of adding to it. A streamed message reports usage more * than once; summing them double-counts the same call. * - `llm/retry-started` closes the replacement slot, so the retried attempt * contributes a second, separately billed sample for that step. * * Ignoring either rule produces totals that disagree with what the harness * itself reports, which is exactly the bug a usage dashboard cannot afford. * * Beyond the totals, this fold also attributes each contribution to a local * day and to a model route so the panel can chart history. Those are extra * indexes over the same arithmetic — the totals never depend on them. * * @module dsh-token-dashboard/host/fold */ import type { RawLogEvent } from './session-log.js'; import type { ContextPressure, CountedBuckets, DayUsage, RouteUsage } from '../shared/types.js'; /** Mutable accumulator with the raw projection bucket names. */ interface Buckets { uncachedInputTokens: number; outputTokens: number; cacheReadTokens: number; cacheWriteTokens: number; reasoningTokens: number; } /** One route's running totals, plus its display identity. */ interface RouteAccumulator { provider: string; model: string; responses: number; buckets: Buckets; } /** One day's running totals. */ interface DayAccumulator { buckets: Buckets; turns: number; responses: number; } /** The fold's complete result for one session. */ export interface SessionFold { /** Projection-equivalent totals for the whole log. */ totals: CountedBuckets; /** Per-route totals, insertion-ordered by first appearance. */ routes: Map; /** Per-day totals keyed by `YYYY-MM-DD` in the requested zone. */ days: Map; /** Turns started in the log. */ turns: number; /** * Assistant settlements recorded in the log, counted per `(turn, step)` slot. * * A retried step opens a new slot and so counts twice; a streamed re-report * of the same slot replaces its predecessor and counts once. This is the same * basis as every `responses` on the route and day accumulators, so the four * tallies always agree. */ responses: number; /** Wall-clock duration of the longest completed turn, in milliseconds. */ longestTurnMs: number; /** Newest observed context occupancy. */ context?: ContextPressure; /** Session title, when the log carries one. */ title?: string; /** * Reasoning effort per request, keyed by the configured label. * * Read from `request/header`, which is written once per outgoing request * envelope, so the tally counts requests rather than messages. */ efforts: Map; /** Skill invocations, keyed by skill name. */ skills: Map; /** Epoch milliseconds of the newest event. */ updatedAt: number; } /** Options controlling day bucketing. */ export interface FoldOptions { /** IANA zone used to derive day keys. */ timeZone: string; } declare const zeroBuckets: () => Buckets; /** * Sum the four billed buckets. * * `reasoningTokens` is deliberately excluded: it is a SUBSET of * `outputTokens`, so adding it would bill the same tokens twice. Matches the * reference's own `usageTokens()`. */ declare function totalOf(buckets: Buckets): number; /** Detach a mutable bucket record for storage on the wire. */ declare function counted(buckets: Buckets): CountedBuckets; declare const addInto: (target: Buckets, delta: Buckets, sign: 1 | -1) => void; /** * Format an epoch millisecond instant as `YYYY-MM-DD` in a zone. * * `Intl.DateTimeFormat` is used rather than manual offset math so DST * transitions and half-hour zones bucket correctly. * @param timeMs - Epoch milliseconds. * @param timeZone - IANA zone name. * @returns The local date key. */ export declare function dayKeyInZone(timeMs: number, timeZone: string): string; /** * Fold one session's decoded events into usage numbers. * * Events are expected in log order; the fold is order-sensitive by design, * because replacement depends on seeing a settlement's predecessor first. * @param events - Durable events in log order, header included. * @param options - Day-bucketing options. * @returns The session's folded usage. */ export declare function foldSession(events: readonly RawLogEvent[], options: FoldOptions): SessionFold; /** Convert a route accumulator into its wire form. */ export declare function routeToWire(accumulator: RouteAccumulator): RouteUsage; /** Convert a day accumulator into its wire form. */ export declare function dayToWire(date: string, accumulator: DayAccumulator): DayUsage; export { totalOf, counted, addInto, zeroBuckets }; export type { Buckets, DayAccumulator, RouteAccumulator };