/** * MetricsPersister — bridges in-process metrics to disk persistence. * * Periodically flushes MetricsRegistry.snapshot() to a sidecar JSON file at * `.rolebox/state/metrics-{hash}.json` (atomic write pattern) and optionally * appends to a ring-buffered NDJSON event log at * `.rolebox/state/metrics-events-{hash}.ndjson`. * * Both files are gated by ROLEBOX_METRICS: when unset/false, persist(), * flushSync(), and dispose() are complete NO-OPs (zero file I/O). * * Follows the same promise-chaining serialization (_saveLock) and atomic * write (.tmp + renameSync) patterns as TaskStateStore. */ import type { RecoveryMetricsSnapshot } from "../../recovery/types.ts"; export declare class MetricsPersister { private directory; private dirHash; private enabled; private eventLogMaxBytes; /** * Optional provider for recovery metrics snapshots. * When set, the recovery data is included in the persisted metrics file * under a top-level `recovery` key. This bridges the RecoveryMetricsCollector * (which tracks recovery attempts, success rates, error types) into the * metrics pipeline without modifying the RecoveryMetricsCollector's existing API. * * Option A bridge: MetricsPersister calls the provider during serialization * and embeds the snapshot — zero changes to RecoveryMetricsCollector. */ private recoverySnapshotProvider; private _saveLock; /** Serialization lock — chains async writes so only one is in-flight at a time. */ constructor(directory: string, opts?: { enabled?: boolean; eventLogMaxBytes?: number; }); /** * Register a callback that provides the current recovery metrics snapshot. * Called during serialization when the metrics file is written. * Set to null (or call with null) to exclude recovery data from the file. * * Designed to be wired from plugin-hooks.ts where both DispatchManager * (which owns MetricsPersister) and RecoveryEngine are available: * * dispatchManager.setRecoverySnapshotProvider(() => recoveryEngine.getMetrics()); */ setRecoverySnapshotProvider(provider: (() => RecoveryMetricsSnapshot | null) | null): void; /** * Persist a metrics snapshot to disk asynchronously. * * Writes the metrics sidecar JSON file and appends to the NDJSON event log * using the same promise-chaining pattern as TaskStateStore.save() to * serialize concurrent writes. * * NO-OP when metrics gating is disabled (!this.enabled). */ persist(): Promise; /** * Synchronous flush for crash-safety on process exit. * * Writes the metrics file synchronously (atomic pattern). * Never throws — wraps errors in try/catch and logs a warning. * NO-OP when metrics gating is disabled. */ flushSync(): void; /** * Dispose the persister: performs a final synchronous flush and cleans up * any pending state. * * Safe to call multiple times. * NO-OP when metrics gating is disabled. */ dispose(): void; private getStateDir; private getMetricsFilePath; private getEventLogPath; /** * Serialize a MetricsSnapshot to the on-disk sidecar file schema. */ private serializeSnapshot; private _doPersist; /** Synchronous atomic write to the metrics file. */ private _writeMetricsFileSync; /** * Append a single event-log line to the NDJSON file. * If the file exceeds eventLogMaxBytes, truncate by keeping only the last * half of the lines (ring-buffer style). */ private _appendEventLogSync; /** * Truncate the NDJSON event log file to ~half its lines. * Reads all lines, keeps the last portion, rewrites atomically. */ private _truncateEventLog; } //# sourceMappingURL=metrics-persister.d.ts.map