(
env: NodeJS.ProcessEnv = process.env,
probe: AutopilotProbe = {},
)
| 80 | * normal syncs are never bricked. |
| 81 | */ |
| 82 | export function detectAutopilot( |
| 83 | env: NodeJS.ProcessEnv = process.env, |
| 84 | probe: AutopilotProbe = {}, |
| 85 | ): AutopilotStatus { |
| 86 | // Secondary signal: known lock files. gbrain #1226 — the lock ignores |
| 87 | // GBRAIN_HOME, so check both the configured home and the default ~/.gbrain. |
| 88 | const lockPaths = probe.lockPaths ?? [ |
| 89 | join(gbrainHome(env), "autopilot.lock"), |
| 90 | join(homedir(), ".gbrain", "autopilot.lock"), |
| 91 | join(gbrainHome(env), "autopilot.pid"), |
| 92 | join(homedir(), ".gbrain", "autopilot.pid"), |
| 93 | ]; |
| 94 | for (const lp of lockPaths) { |
| 95 | if (!existsSync(lp)) continue; |
| 96 | // A lock FILE alone is not proof of life — a crashed daemon leaves a stale |
| 97 | // lock that would otherwise wedge every sync forever (observed: a dead pid |
| 98 | // refused --full indefinitely). Read the holder pid and check liveness. |
| 99 | const pid = readLockPid(lp); |
| 100 | if (pid === null) { |
| 101 | // Can't introspect (no parseable pid) → stay conservative: treat as active. |
| 102 | return { active: true, signal: `lock:${lp}` }; |
| 103 | } |
| 104 | if (isPidAlive(pid)) { |
| 105 | return { active: true, signal: `lock:${lp} (pid ${pid})` }; |
| 106 | } |
| 107 | // Stale lock (holder pid is dead): ignore this signal, keep checking. Pure |
| 108 | // decision function — we do NOT delete the file here; the caller may clean it. |
| 109 | } |
| 110 | // Primary signal: a live `gbrain autopilot` process. |
| 111 | const running = (probe.processRunning ?? defaultProcessRunning)(); |
| 112 | if (running) return { active: true, signal: "process:gbrain autopilot" }; |
| 113 | return { active: false, signal: null }; |
| 114 | } |
| 115 | |
| 116 | /** Read the holder pid from a lock/pid file. Returns null if no integer pid is present. */ |
| 117 | function readLockPid(lockPath: string): number | null { |
no test coverage detected