import type { DB } from '../storage/db.js'; import type { Config } from '../config/config.js'; import type { HandlerCtx } from './handler.js'; import type { HandlerRegistry } from './registry.js'; /** * Resolve the number of worker lanes to run from config, clamped to * [MIN_CONCURRENCY, MAX_CONCURRENCY]. Non-finite/non-positive/non-integer * values fall back to 1 (the pre-concurrency default) rather than throwing — * a malformed config must never block daemon boot. * * Exported for tests; production only ever calls this via startWorker. */ export declare function resolveWorkerConcurrency(config: Config): number; export interface WorkerOpts { pollMs: number; registry: HandlerRegistry; db: DB; config: Config; /** Optional pre-built HandlerCtx (or ExtendedHandlerCtx) to pass to handlers. * When omitted, a minimal {db, config} context is constructed. */ ctx?: HandlerCtx; /** Optional liveness sink — the worker stamps lastTickAt each tick so * /health can report worker.alive. */ healthState?: { worker: { lastTickAt: number | null; }; }; /** * Optional load governor (P1.4/P1.5, src/pipeline/backpressure.ts). * When present: lanes with laneId >= governor.laneBudget() skip claiming * (poll-sleep instead — the budget is re-read every tick, so lanes wake * back up as it rises), embed-heavy handler durations feed its EWMA, and * retry delays are shaped (storm floor + positive jitter) through it. * Absent (tests, CLI one-shots): byte-identical to pre-P1.4 behavior. */ governor?: import('./backpressure.js').LoadGovernor; } export interface WorkerHandle { /** * Stop the worker. Resolves AFTER every lane's currently in-flight tick * completes (if any). Callers MUST await this before closing the DB — * otherwise better-sqlite3 may throw SQLITE_MISUSE if a handler is still * writing. */ stop(): Promise; } /** * Start the worker poll loop in-process. * * Runs `resolveWorkerConcurrency(config)` independent claim-execute lanes * (config default 2 as of FEAT-482 — see `pipeline.concurrency`'s doc * comment in config.ts; a lane count of 1 is byte-identical to the original * pre-concurrency single-loop worker and remains the resolver's own * fallback for a malformed config value). * Each lane runs the exact same tick loop below and all lanes share one * `JobRepo` bound to the same `db`, so lanes race on the SAME atomic * pending→running claim (`JobRepo.claimPending`) rather than each getting * their own queue — no job can be claimed by two lanes. That claim is safe * under concurrency by construction: better-sqlite3 is synchronous and * `claimPending()` runs its SELECT+UPDATE inside a single `db.transaction()` * call with no `await` inside, so — because Node.js is single-threaded — no * other lane's tick can interleave in the middle of a claim. Two lanes * calling `claimPending()` "at the same time" from the event loop's * perspective still execute one after the other at the C/SQLite level; the * second lane's UPDATE always sees `state != 'pending'` for a row the first * already claimed and gets `changes === 0`, so `claimPending()` returns null * for it (falls through to the next candidate on its next tick). This was * already true for MAX_ATTEMPTS-poisoning single-lane retries racing a * boot-time orphan-recovery pass; concurrency just adds more callers onto * the same guarantee. * * Each lane's tick: * 1. Claim the highest-priority pending job (atomic state transition * pending→running; lane `RESERVED_LOCAL_LANE_ID` claims among * `LOCAL_SQLITE_KINDS` first — see job-repo.ts's `claimPending` doc * comment and backpressure.ts's `RESERVED_LOCAL_LANE_ID`). * 2. Dispatch to the registered handler. * 3. On success → complete. * 4. On throw → fail + attempts++. If attempts >= MAX_ATTEMPTS → poison. * 5. Repeat immediately if a job was found; otherwise sleep pollMs. * * Concurrency > 1 multiplies local Ollama load — each lane can have an * in-flight LLM/embed call at the same time, so raising this without also * raising Ollama's own concurrency cap just serializes lanes behind Ollama * instead of the job queue. Pair a `pipeline.concurrency > 1` config with the * `OLLAMA_NUM_PARALLEL` environment variable on the Ollama server (see * https://ollama.com — Ollama defaults to 1 in-flight request per model). * * Returns a handle with stop() that terminates every lane after its * currently in-flight tick. */ export declare function startWorker(opts: WorkerOpts): WorkerHandle;