/** * Cost ledger: append-only NDJSON with closed-schema validation. * * Write semantics mirror transition-log.ts: O_APPEND single-line writes, * whole-line atomicity under concurrent writers. Readers skip torn or * malformed lines with a stderr warning. * * Privacy (SR1/FR2.4): validateEntry enforces a closed key set with * numeric/enum/identifier values only — free-form text cannot enter the * ledger regardless of origin (provider, sync, or --manual). * * Design: design.md §2.1, §4.5 */ import type { CostEntry, CostFidelity } from './types.js'; /** * Validate a candidate ledger entry against the closed schema. * * @returns null when valid, otherwise a reason string (safe to log). */ export declare function validateEntry(entry: unknown): string | null; /** * Thrown by `appendEntry` when the closed schema refuses an entry. * * A type rather than a message to parse: sync must tell a refusal from a failed * write, and once caught both as "validation-rejected" with the reason dropped — * 124 records a run, every run, with nothing saying why * (cost-sync-silent-rejections/AC-3, AC-4). */ export declare class InvalidCostEntryError extends Error { /** `validateEntry`'s fixed reason — never a field value, so safe to print (SR1). */ readonly reason: string; constructor(reason: string); } /** * Append one validated entry to a ledger file. Throws `InvalidCostEntryError` on * an invalid entry, and lets a write error through as Node raised it — callers * decide whether that is a logged rejection (hook path) or an error surfaced to * the user (manual path). */ export declare function appendEntry(ledgerPath: string, entry: CostEntry): void; /** * Read all valid entries from a ledger file. Missing file → empty array. * Malformed or schema-invalid lines are skipped with a stderr warning * (line number only — never line content, per SR1). */ export declare function readEntries(ledgerPath: string): CostEntry[]; /** * Replace a ledger file's contents with `entries`, atomically. * * The ledger is append-only for *capture*, which is what makes concurrent hook * firings safe. Reconciliation is a different caller: when `aidlc cost sync` * moves a record out of unattributed.ndjson into an instance ledger, leaving the * original behind would keep reporting money as unattributed that is now * attributed. Append-only cannot express a move. * * Temp-plus-rename so a crash leaves either the old file or the new one, never a * truncated one — and callers must append to the destination BEFORE rewriting the * source, so an interruption duplicates a record rather than losing it. */ export declare function rewriteLedger(ledgerPath: string, entries: CostEntry[]): void; /** Ledger path for an instance. */ export declare function ledgerPath(projectRoot: string, instance: string): string; /** Path of the unattributed ledger. */ export declare function unattributedPath(projectRoot: string): string; /** * Newest cursor recorded for a session across the given ledger files * (instance ledgers plus unattributed — design §2.4). "Newest" is by entry * timestamp; ties resolved by later coverage.to. */ export declare function newestCursor(session_id: string, ledgerPaths: string[]): string | null; /** A cost entry with its counted weight after precedence reduction. */ export interface WeightedEntry { entry: CostEntry; /** 0..1 — fraction of this entry's units that count toward totals. */ weight: number; } /** * Apply read-time fidelity precedence: each entry's weight is the fraction * of its coverage window not overlapped by higher-fidelity coverage for the * same session. `session_id: null` entries always count in full. `unknown` * entries carry no units and get weight 0. * * Linear sweep over per-session sorted window lists — O(n log n). */ export declare function applyPrecedence(entries: CostEntry[]): WeightedEntry[]; /** Sum weighted units per model (tokens) and total credits. */ export interface UsageTotals { tokens: Map; credits: number; fidelityWeights: Record; } export declare function totalUsage(weighted: WeightedEntry[]): UsageTotals; //# sourceMappingURL=ledger.d.ts.map