import { stopLockHolder, type LockInfo } from '../service/instance-lock.js'; export declare function versionVerifyBudgetMs(): number; import { type ReleaseListEntry } from '../service/release.js'; import { type ShimPointer } from '../service/shim.js'; export declare function healthTimeoutMs(): number; /** * Absolute cap on TOTAL verify-health wait time across every boot-grace * chunk (see `waitForHealthWithBootGrace` below) — the outer bound that * still eventually rolls back a daemon instance that is genuinely stuck * before ever reaching `app.listen()`, not merely slow. Defaults to * `BOOT_HARD_CAP_SEC` (watchdog-probe.ts) rather than a second independent * constant: that value was already calibrated against the worst-observed * real boot time (a 573MB SQLCipher DB taking ~6-8 minutes to bind) for the * exact same failure mode on the hang-watchdog side, and re-deriving a * second number here risks the two silently drifting apart. */ export declare function bootHardCapMs(): number; export interface BootGraceHealthResult { healthy: boolean; /** Total wall-clock time actually spent waiting. */ elapsedMs: number; /** Present only when `healthy: false` — why the wait ended. */ gaveUpReason?: 'daemon-dead' | 'hard-cap'; } /** * Boot-grace-aware `/health` wait (mirrors watchdog-probe.ts's boot-grace * fix, commit 35e8f149): a flat `waitForHealth(port, healthTimeoutMs())` * cannot tell a daemon that is merely SLOW TO BOOT (large encrypted DB — * openDb + sqlite-vec load, all before `app.listen()` — observed taking * several minutes) apart from one that is genuinely dead, and used to treat * both identically: roll back the just-written repoint the moment the * default 90s budget elapsed. That misdiagnoses a healthy-but-slow boot as * failed and restarts the identical slow boot from scratch. * * The fix applies the same distinction the watchdog uses, adapted to this * one-shot verify (no persisted cross-invocation state needed here — the * whole wait happens inside a single `reinstallCommand` process): poll * `/health` in `chunkMs`-sized chunks (each chunk reuses the existing * `healthTimeoutMs()` budget); if a chunk doesn't see a 200, check whether * the daemon.lock holder we just started (`instance-lock.ts` writes it * BEFORE the slow bind phase ever runs, so it's a reliable early anchor) is * still OUR instance (`isDaemonInstanceAlive` — liveness AND identity, not * liveness alone, so a recycled PID reads as dead rather than "still * booting"). Still ours and alive -> presumed still booting, try another * chunk. No lock, a dead pid, or a confirmed-recycled pid -> genuinely gone, * fail immediately rather than waiting out the rest of the budget for * nothing. Either way, the total wait is bounded by `bootHardCapMs()` so a * daemon that is truly stuck before ever reaching `app.listen()` still * eventually rolls back instead of hanging `reinstall` forever. * * `onProgress` (optional) is called once per chunk so a human watching a * `reinstall` against a large DB isn't staring at silence for up to 20 * minutes — every chunk reports elapsed/hard-cap budget and, once healthy, * why the wait ended. */ export declare function waitForHealthWithBootGrace(port: number, dataDir: string, opts: { chunkMs: number; hardCapMs?: number; waitForHealthImpl?: (port: number, timeoutMs: number) => Promise; readLockInfoImpl?: (dataDir: string) => LockInfo | null; /** Injectable liveness check for the daemon.lock holder's pid — tests * substitute a stub instead of a real `process.kill(pid, 0)` probe (the * fake pids tests use don't correspond to real processes). Defaults to * the real `isDaemonPidAlive`. */ pidAliveImpl?: (pid: number) => boolean; /** Injectable OS process-start-time resolver for the identity check * (`isDaemonInstanceAlive`) — tests substitute a stub instead of * shelling out to the OS. Defaults to instance-lock.ts's real * `getProcessStartTime`. */ processStartTimeImpl?: (pid: number) => Promise; nowMs?: () => number; /** Per-chunk progress line — tests/production pass the caller's own * `log()` closure; defaults to a no-op so callers that don't care about * progress output (e.g. most tests) don't have to pass one. */ onProgress?: (message: string) => void; }): Promise; /** * Restore the prior shim pointer after a post-repoint failure (AC-2). * `reinstallCommand` calls this from EVERY failure branch that can occur * after the daemon has been stopped and current.json repointed at the new * (unproven) release: install-service throw, start-service throw, the * health-check timeout, and a served-version mismatch. Without this, any of * those branches would leave current.json pinned to a broken release that * the keepalive/restart policy then restarts forever — the exact silent- * daemon-down outage class this command exists to close (FEAT-438 SLICE-417 * review HIGH finding). * * Cross-platform since FEAT-443 SLICE-1: the pointer indirection (current.json * under `/bin`, see shim.ts) now exists on linux/darwin too, * not just win32 — install-flow.ts writes the stable shim unconditionally on * every platform now. The only genuinely-nothing-to-restore case left is a * first install / unreadable current.json (`priorPointer === null`), handled * below — there is no more platform gate. */ export declare function restorePriorPointer(priorPointer: ShimPointer | null, pointerPath: string): string; export interface StepResult { step: string; ok: boolean; detail?: string; /** * True for a step whose failure is deliberately never allowed to flip the * overall run to failed (e.g. prune-releases: a full disk of old releases * is undesirable but not broken). Such a step must still report `ok: false` * — mislabeling it `ok: true` when it failed would be indistinguishable * from a real success to any future consumer that filters `steps` by * `.ok` (this is exactly the shape of bug this audit exists to catch: * `--json` output, or a monitoring script, reading `steps` and seeing a * green row for a step that actually threw). `finish()` excludes `soft` * steps from the pass/fail gate instead, so the "never fail the run over * this" behavior is preserved without lying about what happened. */ soft?: boolean; } /** * Stop whatever currently owns `dataDir` before repointing/restarting * (reinstall stale-lock false-fail root cause, repro'd 2/2 on this box). * * Root cause: the win32 daemon task is registered with * `MultipleInstancesPolicy=IgnoreNew` (task-xml.ts) — a deliberate * anti-pile-up guard for the PT5M keepalive. Task Scheduler decides "is this * task still running" by watching whether the process IT directly launched * (the hidden-VBS wrapper → cmd → launch.cjs, see shim.ts) has exited, NOT * by asking us. `stopLockHolder` below kills the actual daemon by PID * directly (needed because `adapter.stop()` alone can't reliably reach it — * see that function's own doc comment), which cascades back up through * launch.cjs's blocking `spawnSync` to the wrapper eventually exiting too — * but that cascade, plus Task Scheduler's own detection of it, is real * wall-clock time. If the reinstall flow's later `adapter.start()` * (`schtasks /run`) races ahead of that detection, `IgnoreNew` makes `/run` * a SILENT NO-OP: no new daemon process is ever spawned, `daemon.lock` is * never rewritten, and `waitForHealthWithBootGrace` eventually times out * pointing at the pre-existing dead PID as if it were live evidence of a * hang — even though nothing is wrong with the new release at all. * * Fix: end the scheduler's own task/unit FIRST, exactly like `service stop` * already does (cli/service.ts) — `schtasks /end` updates Task Scheduler's * "running" bookkeeping synchronously as part of that call, so the * subsequent `adapter.start()` can never race it. Best-effort: schtasks * errors loudly when the task isn't currently "running" (a normal/expected * outcome here — e.g. the daemon.lock holder may already be confirmed dead * before this ever runs), and on first install there is no task to end at * all yet. `stopLockHolder` remains the real, idempotent success signal. * * Exported (alongside `restorePriorPointer`) so this specific behavior — * adapter-stop-before-PID-kill ordering — is directly unit-testable without * driving a real OS scheduler; `reinstallCommand` itself stays untested in * isolation for the reasons this file's header comment already explains. */ export declare function stopRunningDaemon(dataDir: string, port: number, opts?: { /** Injectable adapter-stop — tests substitute a spy instead of shelling out to schtasks/systemctl/launchctl. */ adapterStopImpl?: () => Promise; /** Injectable stopLockHolder — tests substitute a stub instead of a real PID signal/health probe. */ stopLockHolderImpl?: typeof stopLockHolder; }): Promise; export declare function reinstallCommand(args: string[]): Promise; /** Exported (alongside StepResult) so the soft-vs-hard failure gating logic * is directly unit-testable — see tests/cli/reinstall.test.ts's * "finish() soft-failure gating" block (silent-error audit, 2026-07-13). */ export declare function finish(steps: StepResult[], jsonMode: boolean, warnings?: string[], successMessage?: string): void; /** * Rollback target selection (FEAT-438 SLICE-418 AC-3): the immediately- * previous release is the newest release (per `listReleases`' newest-first * order) whose id differs from `currentId`. Returns null when there is no * such candidate (no releases on disk, or the only one present IS the * current one) — the caller must fail loudly rather than silently repointing * at nothing. Pure/no I/O — exported standalone so it's unit-testable * without a live daemon/schtasks. */ export declare function selectRollbackTarget(releases: ReleaseListEntry[], currentId: string | null): ReleaseListEntry | null;