import { runDaemon } from './daemon'; import { type DaemonEvidence } from './singleton-policy'; export declare const LOG_PATH: string; /** * Boot capture for the detached launcher (wscript/VBS chain). Kept SEPARATE * from LOG_PATH: the daemon's own `log()` appends to LOG_PATH, so pointing the * launcher's stdout capture at the same file duplicated every line in * ~/.teamshare/daemon.log. This file only holds output emitted before the * daemon's logger is up (module-load failures, native ABI crashes). */ export declare const LAUNCH_LOG_PATH: string; export declare const AUTOSTART_TASK_NAME = "TeamShareBridge"; /** * Reads the daemon singleton lock and returns the live daemon, or null. * * The authority is `daemon.lock` (exclusive, carries pid+port), not the pid * file: the old version read `daemon.pid` and treated an EMPTY file as "not * running", which is indistinguishable from a missing one (`Number('') === 0`). * Combined with a `clearDaemonPidFile` that deleted the file whenever it * could not parse a pid, every short-lived daemon the watchdog spawned could * erase a live daemon's pid file on exit - so the watchdog read "down", * spawned another, and repeated every 30 seconds indefinitely. */ export declare function runningPid(): number | null; /** True when a TeamShare daemon answers /ping on `port` (for honest status). */ export declare function isPortAnswering(port: number): boolean; /** * Best-effort identity of a pid: image name + command line. null = process * gone or unqueryable - never treat as "ours to kill". * * Re-exported from `daemon-instance`, which owns process identity for the whole * daemon lifecycle: `listOurDaemonPids` and the boot guard must agree with * `stopDaemon` about what "our daemon" means, and two copies of that test is * how they drift apart. */ export { isOurDaemonProcess, listOurDaemonPids, pidInfo, } from './daemon-instance'; /** * Stop EVERY verified daemon on this machine, not just the one in the lock. * * The auto-upgrade needs this (it must not write over a directory a live process * still has mapped), and so does plain `stop`: a lock-only `stop` is what let * three daemons survive a "daemon is not running" and then break the next * `npm i -g` with EBUSY. Returns the pids it could NOT kill so callers can * ABORT rather than proceed — a half-installed bridge is strictly worse than an * out-of-date one. */ export declare function stopAllDaemons(): { stopped: number[]; survivors: number[]; message: string; }; /** * Stop EVERY verified watchdog supervisor on this machine. * * Supervisors are excluded from `stopAllDaemons` on purpose (an upgrade must * never kill the watchdog that restarts it) — but a FULL `stop` must take * them down too, or the 30 s tick revives the daemon the human just stopped. * Same identity discipline as daemons: argv match required, never kill on a * guess. Does NOT touch the OS autostart registration (logon task / * LaunchAgent / systemd stay installed; they stand down on the stopped * marker instead). */ export declare function stopAllSupervisors(): { stopped: number[]; survivors: number[]; }; /** * Ask the live daemon to end every agent's session with rescue (kill + * commit + comment) BEFORE it is killed. Best-effort and bounded: without a * daemon there is nothing to ask, and a stop must never hang on cleanup. * Returns the rescued agent ids (empty when skipped). */ export declare function rescueAllSessions(timeoutMs?: number): Promise; /** * What `status`/`doctor` are allowed to claim about the daemon, and how they * know it. * * `source` exists because "running (pid N)" without it is how a stale file * becomes a mystery: the pid and the proof have to travel together. */ export type DaemonReport = DaemonEvidence; /** * The honest answer to "is the daemon running?", for READ-ONLY commands. * * `verifyRunningPid` used to answer from `daemon.lock` alone, so a daemon whose * lock had been taken over by a dead pid printed `stopped` while it was serving * every wake on the machine. The lock is still consulted first (it is cheapest * and normally right), but it is no longer the only witness: * * lock -> runtime.json -> process enumeration. * * Enumeration is last because it is the only one that costs a process query, and * because a pid it finds with no port behind it is a weaker claim than a daemon * that stamped its own lock. Every candidate must ALSO be verified alive — a * stale file is not evidence — which is why the filter is `live` plus an * identity check, never the file contents on their own. */ export declare function describeRunningDaemon(): DaemonReport; /** * Live daemon pid WITH an identity check, for an honest `status`: a recycled * pid (pid file survives a reboot, Windows hands the number to another * process) clears the stale file and reports stopped instead of "running". */ export declare function verifyRunningPid(): number | null; /** * `start`: spawns the daemon detached (stdio -> ~/.teamshare/daemon.log) and * records the pid. `daemon` stays the foreground alias for debugging. * * ONE DAEMON, ALWAYS (2026-10). The old version asked the pid file one question * — "is the pid in the file healthy?" — and that is not the question that keeps * a machine clean. A daemon holding no lock (lock expired after 24h, cleared by * a half-written install, unreadable) is invisible to it, so every 30s the * watchdog answered "no daemon" and spawned another: three were alive at once, * and `npm i -g` over their still-mapped native DLL failed with EBUSY and left * the bridge unusable. * * So the question is now "which verified daemon processes exist, and does one of * them answer?" (`reconcileSingleton`), and its three answers are the only * outcomes: * - a healthy daemon is already there -> adopt it and return it * - processes exist but nothing answers -> stop them all, spawn one * - nothing at all -> spawn one */ export declare function startDaemon(port?: number): { started: boolean; pid?: number; message: string; }; /** * Backoff between spawn attempts once daemons are dying immediately. * * Exported for the unit test. The first entry is 0 so the FIRST failure is always * retried promptly — a genuinely transient crash must not wait. It then climbs to * a 5-minute ceiling: slow enough to stop the log-spam and the socket churn that * made the incident invisible, fast enough that repairing the install brings the * bridge back within one cycle instead of needing a reboot. */ export declare const CRASH_BACKOFF_MS: readonly [0, 30000, 60000, 120000, 300000]; /** * Healthy watchdog ticks between duplicate sweeps (~5 min at the 30s cadence). * * Exported for the unit test. Enumerating processes is not free (one PowerShell * call), so it must not run every tick — but it must run periodically, because * "one healthy lock" and "one daemon" are different facts and only the second * one is the invariant. */ export declare const SINGLETON_SWEEP_EVERY_TICKS = 10; /** The wait before attempt `streak + 1`, clamped to the table. */ export declare function crashBackoffMs(streak: number): number; export declare function runSupervisor(intervalMs?: number): void; /** * `stop`: terminates EVERYTHING on this machine and stays down until a * manual `start`/`restart`. * * It used to signal exactly one pid — the one in `daemon.lock` — and report * `daemon is not running` whenever that file was stale. That is precisely how * three daemons survived a `stop` on this machine: the human ran it, saw * "daemon is not running", and the three survivors kept their native DLLs * mapped, so the next `npm i -g` aborted with EBUSY and left the install * half-written (every later boot died on a missing * `socket.io-client/build/cjs/index.js`). * * `stop` now asks the OS, not the file. `restart` inherits that, which is why a * restart can no longer leave the old daemon behind to collide with the new one. * * FULL STOP (2026-10): a stop is machine-wide, in this order — * 1. stamp the stopped marker FIRST, so nothing revived mid-stop (a * supervisor tick, a logon, an upgrade helper) can resurrect the daemon; * 2. rescue every agent session through the live daemon (kill + commit + * comment, same routine as per-agent Stop work) instead of orphaning * live harness processes that keep writing with no parent watching; * 3. kill every verified daemon, then every verified supervisor; * 4. kill any session holder pids left behind and clear their locks. * The OS autostart registration stays installed; it stands down on the * marker. Only explicit `start`/`restart` clears the marker. */ export declare function stopDaemon(): { stopped: boolean; message: string; }; /** * Session holders left behind after the daemon is gone: kill the pid named * by each agent lock and clear the file, so no phantom busy (and no live * orphan writing into an adopted tree) survives the stop. Identity-guarded: * a lock naming the current process or an unverifiable pid is cleared, never * killed on a guess. Rescue (commit + comment) is impossible without the * daemon's hub clients, so this is kill-only — the daemon-side rescue in * step 2 above is where work gets saved. */ export declare function stopRemainingSessionHolders(): { killed: number[]; cleared: string[]; }; /** `restart`: stop + start. Clears the manual-stop marker the stop stamps. */ export declare function restartDaemon(port?: number): { message: string; }; /** Registers (on) or removes (off) the login autostart for this platform. */ export declare function setAutostart(enabled: boolean): { ok: boolean; message: string; }; /** Whether autostart is registered for this platform. */ export declare function autostartStatus(): boolean; /** True when the user opted out of the default-on watchdog (`autostart off`). */ export declare function isAutostartOptedOut(): boolean; /** * Honest autostart verdict WITH a reason. On Windows the truth comes from * the LIVE scheduled task action (not the marker, not "the task exists"): * - task runs the stable ~/.teamshare script built for THIS install -> ok * - task runs anything else (the package-dir copy npm upgrades delete) -> * the observed post-upgrade failure state: logon dialog at every boot, * marker never written, and `status` used to report a confident "enabled". * - no task -> the Startup-folder fallback (or nothing) decides. */ export declare function autostartHealth(): { ok: boolean; reason: string; }; /** * Default-on watchdog (2026-09): every bridge install gets the autostart * supervisor unless the user opted out with `autostart off`. Runs as a * detached `autostart on` child so the calling CLI never blocks on the * Windows scheduled-task registration (up to ~60s of powershell). Cheap * sync pre-checks (live task action + launcher content) keep this a no-op on * the common path. * * Deliberately re-registers even when the task already exists: the task can * outlive the script it points at (npm upgrade / dist clean), and that dead * registration is a silent no-op until someone runs `autostart on` by hand. * Triggered from `start`/`restart`/`upgrade`/`connect`, from `status` (the * command people actually run when they see the dialog) and from `daemon` * boot - pre-0.21.71 only start/connect triggered it, so a machine whose * upgrade ran under old code never healed. */ export declare function ensureAutostartDefault(): void; /** Keep the daemon entry import for callers that run in-process. */ export { runDaemon };