import { type SingletonDecision } from './singleton-policy'; export interface DaemonInstance { pid: number; /** The port the daemon actually BOUND (may differ from config.port). */ port: number; version: string; startedAt: string; /** Which mechanism produced this answer - for honest diagnostics. */ source: 'lock' | 'port-scan'; } /** True when `pid` names a live process. EPERM means alive but not ours. */ export declare function pidAlive(pid: number): boolean; /** * The live daemon according to `daemon.lock`, or null. * * A lock whose pid is dead, or which is older than LOCK_STALE_MS, is treated * as abandoned and the file is cleared. This is the ONLY path that decides * synchronously, which is what lets the watchdog tick stay sync. */ export declare function readLiveDaemonLock(): DaemonInstance | null; /** * Claim daemon ownership for this process. `wx` makes it genuinely exclusive: * exactly one caller can win, and a loser never overwrites the winner. * * Returns false when a LIVE daemon already owns this machine - the caller * must then exit "already running" rather than bind a fallback port. */ export declare function acquireDaemonLock(): boolean; /** * Record the bound port once `startProbeServer` returns. The lock is written * as pid+port+version so `status`/`stop`/`doctor` never need the port scan. */ export declare function updateDaemonLock(port: number, version: string): void; /** * Adopt a daemon we found by scanning, so the synchronous lock path works * again. Never overwrites a lock held by a different live pid. */ export declare function adoptDaemonLock(found: DaemonInstance): void; /** * The raw lock file, with NO liveness check and NO side effects. * * `readLiveDaemonLock` clears an abandoned lock as it reads, which is right for * deciding but wrong for explaining: `status` has to be able to say "the lock * names pid 24148, which is dead" AFTER the read that cleaned it up. */ export declare function readDaemonLockFile(): DaemonInstance | null; /** * Release the lock, but ONLY when this process owns it. * * The `!current` shortcut that used to live in the pid-file version is * deliberately absent: an unreadable or empty lock is not evidence that we * hold it, and treating it as such let every transient daemon delete a live * daemon's file on `process.on('exit')`. * * This is the EXIT path only. Reclaiming an abandoned lock is * `clearAbandonedLock`'s job - calling this one for that can never succeed, * because the abandoned lock's pid is by definition not `process.pid`. */ export declare function clearDaemonLock(): void; /** Clear the legacy pid file, same ownership rule. */ export declare function clearOwnPidFile(): void; /** * Ask whoever is on `port` whether they are a TeamShare bridge. * * This is the tie-breaker between case (a) "another bridge" and case (b) * "a foreign squatter". A short timeout is deliberate: it runs on the * bind path, where a slow answer must not stall daemon startup. */ export declare function probeBridgePort(port: number, timeoutMs?: number): Promise<{ version: string; } | null>; /** * The discovery fallback: probe every published candidate port for a live * bridge. Used when `daemon.lock` is missing or unreadable - which is the * exact state a crashed or externally-cleaned install lands in. */ export declare function scanForBridge(): Promise; /** * The single answer to "is a daemon running?". * * Lock first (authoritative, synchronous, carries the pid). Only when it is * absent or abandoned do we pay for the port scan, and whatever that finds is * ADOPTED so the next call is free. * * A port-scan hit has `pid: 0` because `/ping` does not expose one - callers * that need to signal the process must fall back to the legacy pid file. */ export declare function resolveDaemonInstance(): Promise; /** The live daemon, sync only. The watchdog uses this so its tick stays sync. */ export declare function resolveDaemonInstanceSync(): DaemonInstance | null; interface PidInfo { /** Process image name (`node`, `GameInputSvc`, …) when queryable. */ name: string | null; /** Full command line when queryable. */ cmdline: string | null; } /** * Best-effort identity of a pid: image name + command line. * null = process gone or unqueryable — never treat as "ours to kill". * * The NAME half matters: after a reboot Windows recycles pids, so a * command-line-only probe returns null for protected processes and a stale * pid file would "match" a system service. */ export declare function pidInfo(pid: number): PidInfo | null; /** * true = verifiably one of OUR daemons (`node …/index.js daemon`). * false = a foreign process (PID reuse) — NEVER kill it. * null = unqueryable — callers treat it as "not ours", which fails safe. * * The watchdog supervisor is excluded by the same test: its argv says * `supervisor`, not `daemon`. An upgrade stops daemons, never the watchdog that * restarts them. */ export declare function isOurDaemonProcess(pid: number): boolean | null; /** * Every pid on this machine that is VERIFIABLY a bridge daemon, ascending. * * `daemon.lock` is NOT good enough on its own: during the incident that * motivated this, the lock held pid 24148 (already dead) while THREE real * daemons ran on three different ports. Anything that trusts the lock alone * concludes "daemon is not running" and starts another one. * * Unqueryable processes (access denied) are simply absent; callers treat * "cannot enumerate" as "cannot confirm", which is the safe direction. */ export declare function listOurDaemonPids(options?: { exclude?: readonly number[]; }): number[]; /** * Every pid that is VERIFIABLY a bridge watchdog supervisor (`node * …/index.js supervisor`), ascending. * * The supervisor is excluded from `listOurDaemonPids` on purpose (an upgrade * must never kill the watchdog that restarts it) — but a FULL `stop` must * take it down too, or its 30 s tick revives the daemon the human just * stopped. Same identity discipline as daemons: argv match required, never * kill on a guess. */ export declare function listOurSupervisorPids(options?: { exclude?: readonly number[]; }): number[]; /** * true = verifiably one of OUR watchdog supervisors (`node … supervisor`). * false = a foreign process (PID reuse) — NEVER kill it. * null = unqueryable — callers treat it as "not ours", which fails safe. */ export declare function isOurSupervisorProcess(pid: number): boolean | null; /** * Every pid whose command line references this bridge install - daemons, * supervisor, upgrade helper, launcher stubs, orphaned sessions. * * Deliberately WIDER than `listOurDaemonPids`, which requires the word * `daemon` because it feeds a decision to KILL. This one feeds a decision to * WARN, and the processes that most often hold a locked native DLL are exactly * the ones the kill filter excludes on purpose (the supervisor) or misses * entirely (a leftover session child). Narrowing first would make the check * blind to the case it exists for. * * Never returns an unverified pid: the command line must actually mention this * install's directory or the bridge entry points. */ export declare function listBridgeProcessPids(installDir?: string, options?: { exclude?: readonly number[]; }): number[]; /** Terminate one verified daemon pid: SIGTERM, then force on Windows. */ export declare function killDaemonPid(pid: number, timeoutMs?: number): boolean; /** Sync `/ping` probe: does a bridge answer `ok:true` on this port? */ export declare function probePortAnswering(port: number, timeoutMs?: number): boolean; /** * Is a daemon healthy RIGHT NOW, and on which port? * * The lock/runtime ports are probed first because they are the answer ~always; * the full candidate list is the fallback for a daemon whose files were lost. * Probing costs one short-lived node process per port, so it runs on the repair * path only — never inside the 30s watchdog tick. */ export declare function findAnsweringDaemonPort(): number | null; export interface ReconcileResult { decision: SingletonDecision; /** Log line for the caller: what happened and what it did about it. */ message: string; /** Pids this call actually terminated. */ stopped: number[]; /** Pids it could NOT terminate — the caller must not pretend otherwise. */ survivors: number[]; } /** * Converge the machine on EXACTLY ONE daemon, then report what the caller * should do (`singleton-policy.ts` owns the decision; this owns the effects). * * Effects, in order: enumerate verified daemon pids → probe for a live * `/ping` → decide → terminate the losers → adopt the survivor into * `daemon.lock`/`daemon.pid` so the NEXT watchdog tick reads a truthful file * instead of spawning again. * * The adoption is the second half of the fix: without it the watchdog would * re-derive the same "no daemon" answer every 30 seconds from a lock that * describes a process nobody can find. */ export declare function reconcileSingleton(options?: { exclude?: readonly number[]; timeoutMs?: number; }): ReconcileResult; export {};