/** * Lifecycle utilities — context compression, budget monitoring, cancellation, * and conversation forks. Mirrors several files under `_harness/`. */ export type CompressionStrategy = (messages: unknown[]) => unknown[]; export type PreCompactHook = (messages: readonly unknown[]) => void | Promise; export interface ContextCompressorOptions { threshold?: number; strategy?: CompressionStrategy; onCompress?: (newSize: number) => void; /** * Hook fired just before the compression strategy runs. Useful for * checkpointing or auditing the pre-compact state. Exceptions are * swallowed — the hook must not block the compression path. */ preCompact?: PreCompactHook; } export declare class ContextCompressor { readonly threshold: number; readonly strategy: CompressionStrategy; readonly onCompress?: (newSize: number) => void; readonly preCompact?: PreCompactHook; constructor(opts?: ContextCompressorOptions); shouldCompress(currentTokens: number): boolean; compressMessages(messages: unknown[]): Promise; /** Synchronous convenience wrapper for callers that cannot await. */ compressMessagesSync(messages: unknown[]): unknown[]; } export interface BudgetThreshold { /** Fractional budget ratio to fire at (e.g. 0.8 for 80%). */ readonly ratio: number; /** Optional label for observers/events. */ readonly label?: string; } /** * Frozen dataclass describing a token budget and its warning thresholds. * A `BudgetMonitor` is the mutable runtime half that consumes this. */ export declare class BudgetPolicy { readonly maxTokens: number; readonly thresholds: readonly BudgetThreshold[]; constructor(opts?: { maxTokens?: number; thresholds?: readonly BudgetThreshold[]; }); /** Apply this frozen policy to a fresh monitor, wiring every threshold. */ applyTo(monitor: BudgetMonitor, handler: BudgetThresholdHandler): BudgetMonitor; } export type BudgetThresholdHandler = (monitor: BudgetMonitor) => void; /** * Tracks cumulative token usage and fires callbacks when configurable * thresholds are crossed. Does NOT compress — delegates to handlers. */ export declare class BudgetMonitor { readonly maxTokens: number; private _used; private readonly thresholds; constructor(maxTokens?: number); get used(): number; get utilization(): number; /** Register a callback that fires once when `ratio` of the budget is used. */ onThreshold(ratio: number, handler: BudgetThresholdHandler): this; /** Update used token count. Fires any newly-crossed thresholds. */ update(used: number): void; /** Add to used token count instead of replacing. */ add(deltaTokens: number): void; reset(): void; /** Build an after-model callback that auto-tracks token usage. */ afterModelHook(): (event: { inputTokens?: number; outputTokens?: number; }) => void; } /** * Cooperative cancellation token. Checked before each tool call. * On cancel, the agent is told to stop gracefully. */ export declare class CancellationToken { private _cancelled; snapshot: unknown; get cancelled(): boolean; cancel(snapshot?: unknown): void; reset(): void; throwIfCancelled(): void; } /** * Per-agent cancellation token keyed by agent name. * * Extends `CancellationToken` with an `agentName` so a registry can * address individual agents: cancel only the researcher, leave the * writer running. Pairs with `TokenRegistry` for priority preemption * from the reactor. */ export declare class AgentToken extends CancellationToken { readonly agentName: string; private _resumeCursor; constructor(agentName: string, opts?: { resumeCursor?: number; }); get resumeCursor(): number; /** Cancel the token and record the resume cursor atomically. */ cancelWithCursor(cursor: number): void; reset(): void; } /** * Keyed registry of `AgentToken` instances. * * One registry per session. Agents that need per-instance cancellation * look up (or create) their token by name; the reactor and preemption * plumbing address them through the registry instead of carrying token * references around by hand. */ export declare class TokenRegistry { private readonly tokens; getOrCreate(agentName: string): AgentToken; /** * Replace any existing entry for `token.agentName` with `token`. * Used by the reactor when a rule re-runs: the in-flight handler * keeps a reference to the previous token (so its cancel is still * observable), while lookups address the live run. */ install(token: AgentToken): void; get(agentName: string): AgentToken | null; /** Cancel one agent's token. Returns false if the name is unknown. */ cancel(agentName: string, opts?: { resumeCursor?: number; }): boolean; reset(agentName: string): void; resetAll(): void; names(): string[]; has(agentName: string): boolean; get size(): number; } export interface ForkManagerOptions { maxBranches?: number; } export interface ForkBranch { name: string; state: Record; createdAt: number; } /** * Manages named branches of session state for parallel exploration. * Forks let an agent try multiple approaches and pick the best one. */ export declare class ForkManager { readonly maxBranches: number; private readonly branches; constructor(opts?: ForkManagerOptions); /** Save a snapshot of `state` under `name`. */ fork(name: string, state: Record): ForkBranch; switch(name: string): Record; /** Diff two branches by key. */ diff(a: string, b: string): Record; /** Shallow merge of two branches (b wins on conflict). */ merge(a: string, b: string): Record; list(): string[]; clear(): void; } //# sourceMappingURL=lifecycle.d.ts.map