import { type ChildProcess } from "node:child_process"; import { EventEmitter } from "node:events"; import type { Config } from "./config.js"; import type { Secrets } from "./env.js"; import { Events } from "./events.js"; import { type JobRecord, type JobStore } from "./jobs.js"; import type { Logger } from "./logger.js"; import type { ManagedSkills } from "./cloud/managed.js"; import { type JobAnswer } from "./progress.js"; import { AccountStateStore } from "./accounts.js"; import type { ReadinessCache } from "./readiness.js"; import type { SkillRegistry } from "./registry.js"; export interface QueueDeps { store: JobStore; config: Config; registry: SkillRegistry; secrets: () => Secrets; /** Secrets from the .env file only; defaults to `secrets`. */ fileSecrets?: () => Secrets; logger: Logger; /** Where `job.*` events are published; the server's bus in `serve`, a private one otherwise. */ events?: Events; /** The environment runs are built from (default `process.env`); tests pass their own. */ processEnv?: NodeJS.ProcessEnv; /** How often the progress files of running jobs are read (default 1000 ms). */ progressPollMs?: number; /** Is the runner installed and logged in? Checked before a job spawns; a `fallback:` runner takes over or the job fails fast. */ readiness?: ReadinessCache; /** What runs taught about each account (`jobs/.accounts.json`); default: next to the job store's directories. */ accountStates?: AccountStateStore; /** Cloud-managed skills: asked right before a job of one runs whether the cloud still holds the installed definition (`freshen`). */ managed?: Pick; } /** * In-memory queue with FIFO within fresh/replay classes, bounded fairness, a global concurrency cap and one-at-a-time per skill by default. Jobs are * persisted before they are enqueued, so a crash loses nothing but the running processes. */ export declare class JobQueue extends EventEmitter { private readonly deps; private queued; private running; /** The account each job was given (by job id), so concurrent picks spread out; cleared when the job finishes. */ private accountOf; /** Pool picks run one at a time: each sees the accounts the ones before it handed out. */ private picking; private stopping; private dispatchPaused; private lastStartedWasReplay; private watcher?; /** Typed `job.*` events (`job.queued`, `job.started`, `job.updated`, `job.cancelled`, `job.finished`). */ readonly events: Events; constructor(deps: QueueDeps); enqueue(job: JobRecord): void; stats(): { running: number; queued: number; running_ids: string[]; }; isActive(id: string): boolean; /** The running (preferred) or queued job of this skill, if any: what `schedule.overlap: skip` looks at. */ inFlight(skill: string): JobRecord | undefined; /** The running (preferred) or queued job of this skill with the same delivery fingerprint, if any. */ findInFlight(skill: string, fingerprint: string): JobRecord | undefined; cancel(id: string): boolean; /** Resolves with the final record, or the current one when the timeout elapses first. */ waitFor(id: string, timeoutMs: number): Promise; /** * A person answers the question a running job is waiting on: the files are written (the agent's `ask` call picks them * up), the record and the timeout clock are updated and `job.answered` is emitted. Undefined when this queue does not * run the job; `NoQuestionError` when it is not waiting for anyone. */ answer(id: string, input: { text: string; option?: string; by?: string; }): JobAnswer | undefined; /** Stops starting new jobs and waits up to `timeoutMs` for the running ones to finish on their own; returns how many are still running. */ drain(timeoutMs: number): Promise; /** Called synchronously just before a graceful process exit, once intake and cloud sync have closed. */ preservePendingOrder(): void; /** Resume dispatch after an abandoned drain; running attempts, retries and questions were never stopped. */ resumeDispatch(): void; /** Stops starting new jobs and terminates running ones (they are marked interrupted). */ shutdown(): Promise; private perSkillLimit; private workspaceFor; private tick; private ensureWatcher; /** Turns the new lines of a running job's `progress.jsonl` into record updates and `job.*` events. */ private readProgress; private applyProgress; private recordAnswer; private finish; private get accountStates(); /** * The account of `pool` job `jobId` should use: in the pool's order (`account_policy.strategy`, `usage_avoid_percent`, * the jobs each account runs now), skipping accounts that are not defined, disabled, limited (until their reset) or * `exclude`d, and those whose readiness check fails. The account it returns is reserved for the job at once * (`last_picked_at`, and counted as running), so the next pick, even of a job starting at the same moment, rotates on. */ private pickAccount; private pickAccountNow; private choosePoolAccount; /** The first usable runner of `candidates` other than `exclude` (`shell` only for a skill that has a command), with an account of its pool when it has one. */ private firstReady; /** Writes what the attempt taught about its account to `jobs/.accounts.json` and says so on the bus when it matters. */ private recordAccount; /** Waits `ms` between attempts, or less when the job is cancelled or the queue stops meanwhile. */ private backoff; private execute; /** One run of the job's runner: spawn, stream, wait, parse. Does not finish the job. */ private attempt; } export declare function killTree(child: ChildProcess | undefined, signal: NodeJS.Signals): void;