/** * src/engine/monitor.ts — bounded delegation monitor: state, trimming, * liveness proof, listing/sorting. * * Ported/adapted from the ZOB harness `delegation-monitor.ts` (state, * bounded-`maxRuns` trimming, liveness proof, list/sort). The liveness * assessment is reworked to a LOCAL attempt shape (`ChildDelegationAttempt`) * and local proof schema — no harness goal-todo types. * * Zero @earendil-works/* imports, zero fs side effects. */ import { type DelegationRunStatus, type DelegationRunView, type DelegationMonitorState } from "./runs.js"; export type DelegationSortMode = "active" | "latest" | "duration" | "agent"; export type DelegationVerdict = "pass" | "warn" | "fail" | "inconclusive"; export type DelegationConfidence = "high" | "medium" | "low"; export interface DelegationSignalBadge { verdict?: DelegationVerdict; confidence?: DelegationConfidence; } export interface DelegationGroupView { parentToolCallId: string; source: string; mode: string; startedAtMs: number; running: number; complete: number; failed: number; runs: DelegationRunView[]; } export interface DelegationSummary { total: number; queued: number; running: number; steered: number; complete: number; failed: number; preflightFailed: number; aborted: number; /** B5: runs that escalated to the master via ask_master (terminal, actionable). */ escalated: number; } /** Create a bounded delegation monitor state (default maxRuns 60). */ export declare function createDelegationMonitorState(maxRuns?: number): DelegationMonitorState; /** True while any run is still queued, running, or steered (active). */ export declare function hasActiveDelegations(state: DelegationMonitorState): boolean; /** Count runs per status. */ export declare function summarizeDelegations(state: DelegationMonitorState): DelegationSummary; /** * Trim the run collection to `maxRuns`. Terminal runs are evicted first * (oldest-first); if still over budget, the oldest runs of any status are * evicted. Non-terminal runs are kept whenever possible. */ export declare function trimDelegationRuns(state: DelegationMonitorState): void; /** Sort a snapshot of runs by the requested mode. */ export declare function listDelegationRuns(state: DelegationMonitorState, sort?: DelegationSortMode, nowMs?: number): DelegationRunView[]; /** Group runs by parent tool call id (chain keeps step order by index). */ export declare function buildDelegationGroups(state: DelegationMonitorState, sort?: DelegationSortMode, nowMs?: number): DelegationGroupView[]; /** Icon per status (copied from the harness). */ export declare function statusIcon(status: DelegationRunStatus): string; /** Compact token count: 340 -> "340", 1234 -> "1.2k", 2500000 -> "2.5M". */ export declare function compactTokens(value: number): string; /** * Human wall-clock duration: 940 -> "940ms", 5000 -> "5s", 74000 -> "1m14s", * 3700000 -> "1h01m". Used by the HUD/Fleet widgets for honest active-run * durations (replaces the old ever-growing runtime uptime line). */ export declare function formatDurationMs(ms: number): string; /** * Readable per-run usage line for the monitor (B4). Distinguishes the two * usage semantics: cumulative run totals (delta-based input/output) and the * current-context snapshot (`ctx`), e.g. `3t · 6.0k in · 150 out · ctx 11.1k * · $0.0090`. "usage n/a" when the run carries no usage (preflight stubs). */ export declare function formatRunUsage(usage: DelegationRunView["usage"]): string; export type ChildDelegationAttemptStatus = "queued" | "running" | "claim_returned" | "accepted" | "rejected" | "failed_preflight" | "failed_runtime" | "cancelled" | "failed_output_gate_format" | "failed_output_gate_semantic" | "output_declared_incomplete" | "liveness_unknown"; /** Local durable-attempt shape consumed by the liveness assessment. */ export interface ChildDelegationAttempt { attemptId: string; runId: string; status: ChildDelegationAttemptStatus; updatedAt: number; finalizedAt?: number; failureHash?: string; gateHash?: string; outputHash?: string; boundGoalRevision?: number; boundGraphRevision?: number; boundTodoRevision?: number; } export type ChildAttemptLivenessStatus = "active" | "inactive" | "unknown"; export type ChildAttemptLivenessSource = "current_monitor" | "restored_monitor" | "durable_attempt" | "none"; export type ChildAttemptLivenessCode = "monitor_active_exact" | "monitor_terminal_exact" | "attempt_id_mismatch" | "run_id_mismatch" | "monitor_attempt_run_mismatch" | "durable_preflight_terminal" | "durable_child_terminal" | "durable_output_terminal" | "restored_nonterminal_without_controller" | "terminal_proof_incomplete" | "nonterminal_without_authoritative_status"; /** Local hash-only liveness proof (mirror of the harness schema shape). */ export interface ChildAttemptLivenessProof { schema: string; status: ChildAttemptLivenessStatus; source: ChildAttemptLivenessSource; code: ChildAttemptLivenessCode; attemptId: string; runId: string; attemptStatus: string; monitorStatus?: string; proofAt: number; proofTimestampHash: string; proofHash: string; bodyStored: false; } /** * Pure, fail-closed liveness assessment for one exact durable attempt. * Missing controllers, PIDs, elapsed time, and restored active-looking monitor * rows never prove inactivity. */ export declare function assessDelegationAttemptLiveness(state: DelegationMonitorState, attempt: ChildDelegationAttempt, expected?: { attemptId: string; runId: string; }): ChildAttemptLivenessProof;