/** * Plugin-owned persistence. * * The estimator needs history that outlives a restart: the readings it learns the * price drift from, and the per-minute token buckets it prices them against. Both * live in one small JSON document under `$DSH_HOME/dsh-balance`, written * atomically and only after a debounce, because a token-heavy turn touches the * buckets hundreds of times. * * The correction factors are deliberately **not** persisted: they are re-derived * from readings and buckets on every load, so a calendar fix or a price-table edit * immediately re-classifies past intervals instead of leaving a stale scalar * behind. * * A malformed or missing file is a warning and an empty start, never a boot * failure: a corrupted cache must not take the UI down with it. * * @module dsh-balance/history */ import { type TokenBucketRecord } from './estimator.js'; import type { TokenBuckets } from './pricing.js'; import type { CalendarYear } from './schedule.js'; /** One poll result, as fetched. Totals stay decimal strings per currency. */ export interface ReadingRecord { readonly at: number; readonly usageClockAt: number; /** Currency to total balance, exactly as the platform reported it. */ readonly totals: Readonly>; readonly isAvailable: boolean | null; } /** On-disk history document. */ export interface HistoryFile { readonly version: 1; readonly readings: readonly ReadingRecord[]; /** Key is `"|"`. */ readonly buckets: Readonly>; } /** On-disk remote calendar cache. */ export interface CalendarCacheFile { readonly version: 1; readonly fetchedAt: number; readonly source: string; readonly years: Readonly>; } /** Seed calendar shipped inside the package. */ export interface SeedCalendarFile { readonly version: number; readonly source: string; readonly fetchedAt: number; readonly years: Readonly>; } /** Parse a history document, discarding anything malformed. */ export declare function parseHistoryFile(value: unknown): HistoryFile; /** Parse a calendar document (cache or seed), discarding anything malformed. */ export declare function parseCalendarFile(value: unknown): { fetchedAt: number | null; source: string; years: Record; }; /** Compose a bucket map key. */ export declare function bucketKey(minute: number, route: string): string; /** Split a bucket key back into its parts. */ export declare function parseBucketKey(key: string): { minute: number; route: string; } | null; /** Debounced, atomic history store for one profile. */ export declare class HistoryStore { private readonly file; private readonly root; private readonly debounceMs; private readonly warn; private readings; private buckets; private timer; private writing; private closed; constructor(options: { root: string; debounceMs: number; warn: (message: string, error?: unknown) => void; }); /** Absolute path of the history document. */ get path(): string; /** Read the document, tolerating a missing or malformed file. */ load(): Promise; private bucketMap; /** Readings in append order. */ get readingsView(): readonly ReadingRecord[]; /** Buckets flattened for the estimator, ascending by minute. */ get bucketsView(): readonly TokenBucketRecord[]; /** Routes seen in the retained buckets. */ get routes(): readonly string[]; /** Append one poll result, replacing any reading with the same timestamp. */ appendReading(record: ReadingRecord): void; /** Accumulate token deltas into the current minute's bucket for one route. */ addTokens(minute: number, route: string, delta: TokenBuckets): void; /** Drop readings and buckets older than the retention window. */ prune(now: number, retentionMs: number): void; /** Queue a debounced write. */ scheduleWrite(): void; private snapshot; private write; /** Write immediately, cancelling any pending debounce. */ flush(): Promise; /** Flush and stop accepting writes. */ close(): Promise; } /** * Write a file atomically: same-directory temp file, mode 0600, then rename. * @param path - destination path. * @param body - file contents. * @param warn - diagnostic sink for a failed cleanup. * @throws when the write itself fails, after removing the temp file. */ export declare function writeAtomic(path: string, body: string, warn: (message: string, error?: unknown) => void): Promise; /** Read the remote calendar cache, if present and parseable. */ export declare function loadCalendarCache(root: string, warn: (message: string, error?: unknown) => void): Promise<{ fetchedAt: number | null; source: string; years: Record; } | null>; /** Persist the remote calendar cache. */ export declare function saveCalendarCache(root: string, cache: CalendarCacheFile, warn: (message: string, error?: unknown) => void): Promise; /** * Read the seed calendar shipped beside the compiled host half. * * The path is resolved relative to this module rather than the working directory, * so a profile-installed copy resolves it inside its own package. * * @returns the seed calendar, or null when the file is absent or malformed. */ export declare function loadSeedCalendar(): Promise<{ fetchedAt: number | null; source: string; years: Record; } | null>;