/** * backpressure.ts — adaptive worker-load governor (stabilization plan * P1.4 + P1.5). * * 2026-07-15 incident inputs this closes: * - embed-heavy jobs (distill/reembed) kept claiming at full concurrency * while the embed provider was saturated (measured 62–142s per embed) — * every lane just deepened the provider's queue; * - retries used a deterministic exponential backoff with NO jitter, so a * wave of jobs failing at the same saturation moment retried in * lockstep ("fetch failed" storms re-amplifying the load); * - a restart re-enqueued orphans and consolidate at boot and the worker * claimed them at full concurrency at the exact moment the daemon was * trying to prove liveness to the watchdog. * * One object owns the lane budget; two signals drive it: * 1. Embed-heavy handler latency EWMA → staircase multiplier (1 → ½ → ¼). * 2. Boot ramp — lanes start at 1 and step up to full over rampStepMs * intervals ("serve traffic first, catch up second" — the job-queue * analog of the embed prober's degraded-start philosophy). * The governor also owns retry shaping: always-positive jitter (never * undercuts a Retry-After floor) plus an aggregate retry-rate clamp that * pushes ALL retry delays up when the system-wide retry rate storms. * * Everything here is pure in-memory arithmetic — no I/O, nothing to leak. * Failure posture: malformed/NaN inputs always degrade toward "assume * normal" (multiplier 1, no clamp), never toward max-throttle — mirrors * resolveWorkerConcurrency's "malformed config never blocks boot". */ /** Handler kinds whose latency feeds the EWMA — the embed/LLM-bound ones. */ export declare const EMBED_HEAVY_KINDS: Set; /** * FEAT-482 fold (worker-starvation wave, item 2 of 3): the lane index * `worker.ts` reserves for preferential local-sqlite-kind claims * (`job-repo.ts`'s `LOCAL_SQLITE_KINDS`). MUST be 0 — `laneBudget()` above * gates lane 0 always-on (`Math.max(1, ...)`; the `laneId >= laneBudget()` * skip check in `worker.ts` can never be true for lane 0), so lane 0 is the * one lane that keeps running even when the embed-latency EWMA throttles * every other lane to zero. An arbitrary/higher lane index would itself get * throttled during the exact saturation event the local/network split * exists to survive, defeating the point of reserving it. */ export declare const RESERVED_LOCAL_LANE_ID = 0; export declare const DEFAULT_SLOW_THRESHOLD_MS = 5000; export declare const DEFAULT_SEVERE_THRESHOLD_MS = 20000; export declare const DEFAULT_RETRY_BUDGET_PER_MIN = 20; export declare const DEFAULT_BOOT_RAMP_STEP_MS = 60000; /** Floor applied to every retry delay while the aggregate retry rate is * over budget — spreads a storm out instead of letting the 1/2/4s * exponential re-slam a saturated provider. */ export declare const RETRY_STORM_FLOOR_MS = 30000; /** Max positive jitter fraction added to retry delays. Positive-only so a * server-supplied Retry-After floor is never undercut. */ export declare const RETRY_JITTER_MAX_FRACTION = 0.3; export interface LoadGovernorOpts { /** Full lane count (resolveWorkerConcurrency's result). */ maxLanes: number; slowThresholdMs?: number; severeThresholdMs?: number; retryBudgetPerMin?: number; /** Omit to disable the boot ramp (lanes start at full — test/CLI use). */ bootRamp?: { rampStepMs?: number; }; /** Injectable clock/random for tests. */ now?: () => number; random?: () => number; } export interface LoadGovernor { /** How many lanes may claim jobs right now (>= 1 always). */ laneBudget(): number; /** Feed a completed/failed handler run; non-embed-heavy kinds are ignored. */ recordHandlerRun(kind: string, durationMs: number): void; /** Feed one retry event (any kind) into the aggregate rate window. */ recordRetry(): void; /** Shape a computed retry delay: storm floor clamp + positive jitter. */ shapeRetryDelay(delayMs: number): number; /** Current embed-latency EWMA — observability (dashboard, /health later). */ ewmaMs(): number; /** Stop ramp timers. Idempotent. */ stop(): void; } export declare function createLoadGovernor(opts: LoadGovernorOpts): LoadGovernor;