From 97224c9dd78bc52705eb40f00e5344ede2425f2e Mon Sep 17 00:00:00 2001 From: Junmo Kim Date: Thu, 29 Oct 2026 23:13:56 +0900 Subject: [PATCH] fix(runner): recover sessions after host reboot (#2169) * fix(runner): recover sessions after host reboot * fix(runner): settle startup when process generation changes * test(runner): wait for Hub RPC readiness before resume --- cli/src/runner/buildCliArgs.test.ts | 39 +- cli/src/runner/run.ts | 91 +++-- cli/src/runner/runner.integration.test.ts | 425 +++++++++++++++++++++- cli/src/utils/process.ts | 29 ++ cli/src/utils/processGeneration.test.ts | 68 ++++ 5 files changed, 613 insertions(+), 39 deletions(-) create mode 100644 cli/src/utils/processGeneration.test.ts diff --git a/cli/src/runner/buildCliArgs.test.ts b/cli/src/runner/buildCliArgs.test.ts index db1b4fa3..bd319bd1 100644 --- a/cli/src/runner/buildCliArgs.test.ts +++ b/cli/src/runner/buildCliArgs.test.ts @@ -1,9 +1,9 @@ import { describe, it, expect } from 'vitest' import { execFileSync } from 'node:child_process' -import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { mkdtempSync, readdirSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' -import { buildCliArgs, classifyRecoveredProcessGeneration, createSpawnDeduplicator, releaseRecoveredSpawnDedupe } from './run' +import { buildCliArgs, classifyRecoveredProcessGeneration, createSpawnDeduplicator, probeRecoveredProcessGeneration, releaseRecoveredSpawnDedupe } from './run' describe('buildCliArgs', () => { it('adds --permission-mode for valid permission mode', () => { @@ -554,6 +554,28 @@ describe('createSpawnDeduplicator', () => { }) describe('classifyRecoveredProcessGeneration', () => { + it('releases a recovered record from a different boot even when its PID is alive and its marker is unavailable', () => { + expect(classifyRecoveredProcessGeneration(true, null, 'old-marker', { + pid: 123, threadGroupId: 123, + currentBootId: 'new-boot', persistedBootId: 'old-boot' + })).toBe('exited') + }) + + it('releases a legacy process ID now occupied by a non-leader thread', () => { + expect(classifyRecoveredProcessGeneration(true, null, 'old-marker', { + pid: 123, threadGroupId: 456, currentBootId: null + })).toBe('exited') + }) + + it('does not treat missing boot or thread information as proof of exit', () => { + expect(classifyRecoveredProcessGeneration(true, null, 'old-marker', { + pid: 123, threadGroupId: null, currentBootId: null, persistedBootId: 'old-boot' + })).toBe('quarantined') + expect(classifyRecoveredProcessGeneration(true, 'old-marker', 'old-marker', { + pid: 123, threadGroupId: 123, currentBootId: 'new-boot' + })).toBe('verified') + }) + it('quarantines a live recovered child while its generation marker is unavailable', () => { expect(classifyRecoveredProcessGeneration(true, null, 'persisted-marker')).toBe('quarantined') }) @@ -582,3 +604,16 @@ describe('releaseRecoveredSpawnDedupe', () => { expect(recovered.has(123)).toBe(false) }) }) + + +describe.skipIf(process.platform !== 'linux')('Linux recovered process generation', () => { + it('releases a legacy PID actually occupied by a live non-leader thread', () => { + const tid = readdirSync(`/proc/${process.pid}/task`).map(Number).find(id => id !== process.pid) + expect(tid).toBeDefined() + // Use a real, still-live thread: signal 0 succeeds but ps -p cannot + // provide a process start marker, exactly as after the reported reboot. + expect(process.kill(tid!, 0)).toBe(true) + expect(probeRecoveredProcessGeneration(tid!, { processStartMarker: 'Sun Oct 25 04:46:27 2026' })).toBe('exited') + expect(process.kill(tid!, 0)).toBe(true) + }) +}) diff --git a/cli/src/runner/run.ts b/cli/src/runner/run.ts index 12439871..fc912d50 100644 --- a/cli/src/runner/run.ts +++ b/cli/src/runner/run.ts @@ -14,7 +14,7 @@ import { getEnvironmentInfo } from '@/ui/doctor'; import { spawnHappyCLI } from '@/utils/spawnHappyCLI'; import { writeRunnerState, RunnerLocallyPersistedState, readRunnerState, acquireRunnerLock, releaseRunnerLock } from '@/persistence'; import { getCliArgs } from '@/utils/cliArgs'; -import { getProcessStartMarker, isProcessAlive, isWindows, killProcess, killProcessByChildProcess, killProcessTreeByPid } from '@/utils/process'; +import { getProcessStartMarker, getSystemBootId, getProcessThreadGroupId, normalizeBootId, isProcessAlive, isWindows, killProcess, killProcessByChildProcess, killProcessTreeByPid } from '@/utils/process'; import { findStopSessionOrphanTargets, listWindowsProcessesWithCommandLine, reapRunnerSpawnedOrphans } from '@/runner/orphanReap'; import { decideUntrackedRunnerWebhook } from '@/runner/lateRunnerWebhook'; import { cleanupFailedStartupAfterExit, sessionStartupTimeout, waitForRecoverableStartup } from './sessionStartup'; @@ -114,13 +114,33 @@ export function createSpawnDeduplicator( export function classifyRecoveredProcessGeneration( processAlive: boolean, currentMarker: string | null, - persistedMarker: string + persistedMarker: string, + evidence?: { pid: number; threadGroupId: number | null; currentBootId: string | null; persistedBootId?: string } ): 'verified' | 'quarantined' | 'exited' { if (!processAlive) return 'exited'; + if (evidence?.persistedBootId && evidence.currentBootId && evidence.persistedBootId !== evidence.currentBootId) return 'exited'; + // A runner child is a process leader. kill(pid, 0) also succeeds for Linux + // thread IDs, while ps -p does not report their start time. + if (evidence?.threadGroupId != null && evidence.threadGroupId !== evidence.pid) return 'exited'; if (currentMarker === null) return 'quarantined'; return currentMarker === persistedMarker ? 'verified' : 'exited'; } +export function probeRecoveredProcessGeneration( + pid: number, + record: { processStartMarker: string; bootId?: string } +): 'verified' | 'quarantined' | 'exited' { + if (!isProcessAlive(pid)) return 'exited'; + const evidence = { + pid, + threadGroupId: getProcessThreadGroupId(pid), + currentBootId: getSystemBootId(), + persistedBootId: record.bootId, + }; + if (classifyRecoveredProcessGeneration(true, null, record.processStartMarker, evidence) === 'exited') return 'exited'; + return classifyRecoveredProcessGeneration(true, getProcessStartMarker(pid), record.processStartMarker, evidence); +} + export function releaseRecoveredSpawnDedupe( pid: number, existingSessionIdByChildPid: Map, @@ -346,6 +366,7 @@ export async function startRunner(options: { workspaceRoots?: string[] } = {}): confirmedSessionId?: string; pid: number; processStartMarker: string; + bootId?: string; }; const resumeProcessFile = `${configuration.runnerStateFile}.resume-processes.json`; const persistedResumeProcesses = (() => { @@ -365,6 +386,7 @@ export async function startRunner(options: { workspaceRoots?: string[] } = {}): confirmedSessionId: typeof record.confirmedSessionId === 'string' ? record.confirmedSessionId : undefined, pid: record.pid, processStartMarker: record.processStartMarker, + bootId: normalizeBootId(record.bootId) ?? undefined, }]]; })); } catch (error) { @@ -383,15 +405,16 @@ export async function startRunner(options: { workspaceRoots?: string[] } = {}): }; for (const [pid, record] of [...persistedResumeProcesses]) { const alive = isProcessAlive(pid); - const marker = alive ? getProcessStartMarker(pid) : null; - const generation = classifyRecoveredProcessGeneration(alive, marker, record.processStartMarker); + const generation = probeRecoveredProcessGeneration(pid, record); if (generation === 'verified') { pidToRequestedSessionId.set(pid, record.requestedSessionId); if (record.confirmedSessionId) pidToConfirmedSessionId.set(pid, record.confirmedSessionId); } else if (generation === 'exited') { persistedResumeProcesses.delete(pid); - rememberVerifiedExit(record.requestedSessionId); - if (record.confirmedSessionId) rememberVerifiedExit(record.confirmedSessionId); + if (!alive) { + rememberVerifiedExit(record.requestedSessionId); + if (record.confirmedSessionId) rememberVerifiedExit(record.confirmedSessionId); + } } else { // PID is live but generation probing failed: keep the durable record and // fail closed instead of manufacturing verified-exit evidence. @@ -435,6 +458,12 @@ export async function startRunner(options: { workspaceRoots?: string[] } = {}): return; } + const threadGroupId = getProcessThreadGroupId(pid); + if (threadGroupId !== null && threadGroupId !== pid) { + logger.debug(`[RUNNER RUN] Ignoring session webhook for non-leader thread ${pid}`); + return; + } + logger.debug(`[RUNNER RUN] Session webhook: ${sessionId}, PID: ${pid}, started by: ${sessionMetadata.startedBy || 'unknown'}`); logger.debug(`[RUNNER RUN] Current tracked sessions before webhook: ${Array.from(pidToTrackedSession.keys()).join(', ')}`); @@ -476,7 +505,8 @@ export async function startRunner(options: { workspaceRoots?: string[] } = {}): requestedSessionId: existingSession.requestedHappySessionId ?? sessionId, confirmedSessionId: sessionId, pid, - processStartMarker + processStartMarker, + bootId: getSystemBootId() ?? undefined, }); persistResumeProcesses(); } @@ -564,6 +594,7 @@ export async function startRunner(options: { workspaceRoots?: string[] } = {}): confirmedSessionId: sessionId, pid, processStartMarker, + bootId: getSystemBootId() ?? undefined, }); persistResumeProcesses(); } @@ -926,7 +957,8 @@ export async function startRunner(options: { workspaceRoots?: string[] } = {}): persistedResumeProcesses.set(pid, { requestedSessionId: trackedSession.requestedHappySessionId, pid, - processStartMarker + processStartMarker, + bootId: getSystemBootId() ?? undefined, }); persistResumeProcesses(); } @@ -1394,16 +1426,13 @@ export async function startRunner(options: { workspaceRoots?: string[] } = {}): if (orphan === 'stopped') return 'stopped'; return 'unknown'; } - const currentMarker = getProcessStartMarker(pid); - if (currentMarker === null || currentMarker !== persisted.processStartMarker) { + const generation = probeRecoveredProcessGeneration(pid, persisted); + if (generation === 'quarantined') return 'still_alive'; + if (generation === 'exited') { logger.debug( `[RUNNER RUN] Adopted PID ${pid} generation mismatch; dropping stale tracking for ${sessionId}` ); - pidToTrackedSession.delete(pid); - pidToRequestedSessionId.delete(pid); - pidToConfirmedSessionId.delete(pid); - if (persistedResumeProcesses.delete(pid)) persistResumeProcesses(); - releaseRecoveredSpawnDedupe(pid, existingSessionIdByChildPid, spawnSession); + discardStaleProcess(pid); continue; } } @@ -1478,18 +1507,14 @@ export async function startRunner(options: { workspaceRoots?: string[] } = {}): if (requestedSessionId !== sessionId && confirmedSessionId !== sessionId) continue; if (isProcessAlive(pid)) { if (!persisted) return 'still_alive'; - const currentMarker = getProcessStartMarker(pid); - if (currentMarker === null) return 'still_alive'; - if (currentMarker !== persisted.processStartMarker) { + const generation = probeRecoveredProcessGeneration(pid, persisted); + if (generation === 'quarantined') return 'still_alive'; + if (generation === 'exited') { // PID reuse: drop the stale mapping, but do NOT claim already_gone // or write a verified-exit tombstone — the HAPI CLI for this session // may still be alive under a different PID (#1910). Continue so // other fallback PIDs / argv orphan scan can still reap it. - persistedResumeProcesses.delete(pid); - persistResumeProcesses(); - pidToRequestedSessionId.delete(pid); - pidToConfirmedSessionId.delete(pid); - releaseRecoveredSpawnDedupe(pid, existingSessionIdByChildPid, spawnSession); + discardStaleProcess(pid); continue; } const liveForPid = await liveRegistryRuntimes(); @@ -1582,6 +1607,18 @@ export async function startRunner(options: { workspaceRoots?: string[] } = {}): return 'unknown'; }; + // A reused PID invalidates a binding, not every runtime for that session. + const discardStaleProcess = (pid: number) => { + pidToErrorAwaiter.get(pid)?.(`Session process PID ${pid} generation changed before startup was confirmed`); + pidToTrackedSession.delete(pid); + pidToAwaiter.delete(pid); + pidToErrorAwaiter.delete(pid); + pidToRequestedSessionId.delete(pid); + pidToConfirmedSessionId.delete(pid); + if (persistedResumeProcesses.delete(pid)) persistResumeProcesses(); + releaseRecoveredSpawnDedupe(pid, existingSessionIdByChildPid, spawnSession); + }; + // Handle child process exit const onChildExited = (pid: number) => { pidToErrorAwaiter.get(pid)?.(`Session process PID ${pid} exited before startup was confirmed`); @@ -1811,14 +1848,10 @@ export async function startRunner(options: { workspaceRoots?: string[] } = {}): } const persisted = persistedResumeProcesses.get(pid); if (persisted) { - const generation = classifyRecoveredProcessGeneration( - true, - getProcessStartMarker(pid), - persisted.processStartMarker - ); + const generation = probeRecoveredProcessGeneration(pid, persisted); if (generation === 'exited') { logger.debug(`[RUNNER RUN] Removing stale session with reused PID ${pid}`); - onChildExited(pid); + discardStaleProcess(pid); } else if (generation === 'verified' && pidToRequestedSessionId.get(pid) !== persisted.requestedSessionId) { pidToRequestedSessionId.set(pid, persisted.requestedSessionId); if (persisted.confirmedSessionId) pidToConfirmedSessionId.set(pid, persisted.confirmedSessionId); diff --git a/cli/src/runner/runner.integration.test.ts b/cli/src/runner/runner.integration.test.ts index d0944316..b02a1fdb 100644 --- a/cli/src/runner/runner.integration.test.ts +++ b/cli/src/runner/runner.integration.test.ts @@ -22,7 +22,9 @@ import { describe, it, expect, beforeEach, afterEach, afterAll } from 'vitest'; import { spawn } from 'child_process'; -import { existsSync, unlinkSync, readFileSync, writeFileSync, readdirSync } from 'fs'; +import { randomUUID } from 'node:crypto'; +import { hostname } from 'node:os'; +import { existsSync, mkdirSync, unlinkSync, readFileSync, writeFileSync, readdirSync } from 'fs'; import path, { join } from 'path'; import { configuration } from '@/configuration'; import { @@ -37,7 +39,8 @@ import { readRunnerState, clearRunnerState } from '@/persistence'; import { Metadata } from '@/api/types'; import { spawnHappyCLI } from '@/utils/spawnHappyCLI'; import { getLatestRunnerLog } from '@/ui/logger'; -import { isProcessAlive, isWindows, killProcess, killProcessByChildProcess } from '@/utils/process'; +import { getProcessStartMarker, isProcessAlive, isWindows, killProcess, killProcessByChildProcess } from '@/utils/process'; +import { ApiClient } from '@/api/api'; import { buildTestChildEnv, testOwnedMarker } from '@/test/integrationEnv'; import { trackChildProcess, trackRunnerPid, trackSession, cleanupAllRegisteredProcesses } from '@/test/processRegistry'; import { findTestOwnedProcesses, reapTestOwnedProcesses } from '@/test/auditTestProcesses'; @@ -98,23 +101,21 @@ describe.skipIf(!await isServerHealthy())('Runner Integration Tests', { timeout: return response; } - beforeEach(async () => { - // First ensure no runner is running by checking PID in metadata file - await stopRunner() - + async function startTestRunner(env: NodeJS.ProcessEnv = {}): Promise { // Start fresh runner for this test // This will return and start a background process - we don't need to wait for it const runnerLauncher = spawnHappyCLI(['runner', 'start'], { stdio: 'ignore', // Test-scoped env: neutralizes any outer HAPI/pi session identity and // stamps every child with the run's unique test marker. - env: buildTestChildEnv() + env: { ...buildTestChildEnv(), ...env } }); // Register immediately after spawn so cleanup runs even if this test fails // before reaching its happy-path teardown. trackChildProcess(runnerLauncher, 'runner-launcher'); - // Wait for runner to write its state file (it needs to auth, setup, and start server) + // The state file proves local control is available, not that the Hub has + // received this runner's RPC registrations yet. // Also require the PID to actually be alive: a SIGKILLed runner (or a // crashed one) can leave a stale runner.state.json behind, and reading it // as "started" would make every control call in the test body fail. @@ -130,8 +131,33 @@ describe.skipIf(!await isServerHealthy())('Runner Integration Tests', { timeout: runnerPid = runnerState.pid; trackRunnerPid(runnerPid, 'runner'); + const machineId = runnerState.startedWithMachineId; + if (!machineId) throw new Error('Runner state is missing its machine ID'); + // ApiMachineClient emits its RPC registrations before the running-state + // update on the same socket. Match the PID so the previous runner's state + // cannot satisfy this barrier during a restart. + await waitFor(async () => { + const response = await fetch(`${configuration.apiUrl}/cli/machines/${machineId}`, { + headers: { Authorization: `Bearer ${configuration.cliApiToken}` }, + signal: AbortSignal.timeout(1000), + }); + if (!response.ok) return false; + const { machine } = await response.json() as { + machine?: { active: boolean; runnerState: { status: string; pid?: number } | null }; + }; + return machine?.active === true + && machine.runnerState?.status === 'running' + && machine.runnerState.pid === runnerPid; + }, 10_000); + console.log(`[TEST] Runner started for test: PID=${runnerPid}`); console.log(`[TEST] Runner log file: ${runnerState?.runnerLogPath}`); + } + + beforeEach(async () => { + // First ensure no runner is running by checking PID in metadata file + await stopRunner() + await startTestRunner() }); /** Bounded wrapper around the graceful runner stop. */ @@ -174,6 +200,389 @@ describe.skipIf(!await isServerHealthy())('Runner Integration Tests', { timeout: expect(sessions).toEqual([]); }); + const resumeProcessFile = `${configuration.runnerStateFile}.resume-processes.json`; + const verifiedExitFile = `${configuration.runnerStateFile}.verified-exits.json`; + type ResumeRecord = { + requestedSessionId: string; + confirmedSessionId: string; + pid: number; + processStartMarker: string; + bootId?: string; + }; + + function readResumeRecords(): ResumeRecord[] { + return JSON.parse(readFileSync(resumeProcessFile, 'utf8')); + } + + function expectNoExitTombstone(sessionId: string, pid: number): void { + const exits: string[] = existsSync(verifiedExitFile) + ? JSON.parse(readFileSync(verifiedExitFile, 'utf8')) + : []; + expect(exits).not.toContain(sessionId); + expect(exits).not.toContain(`PID-${pid}`); + } + + async function restartWithResumeRecords(records: ResumeRecord[], env: NodeJS.ProcessEnv = {}): Promise { + const oldPid = runnerPid; + await stopRunnerHttp(); + await waitFor(async () => !isProcessAlive(oldPid)); + writeFileSync(resumeProcessFile, JSON.stringify(records)); + await startTestRunner(env); + } + + async function spawnUnrelatedProcess(): Promise { + const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { + env: buildTestChildEnv(), + stdio: 'ignore', + }); + trackChildProcess(child, 'resume-generation-unrelated-process'); + if (!child.pid) throw new Error('Failed to spawn generation fixture'); + const pid = child.pid; + await waitFor(async () => getProcessStartMarker(pid) !== null); + return pid; + } + + async function findFixtureThread(pid: number): Promise { + let tid: number | undefined; + await waitFor(async () => { + tid = readdirSync(`/proc/${pid}/task`).map(Number).find(candidate => candidate !== pid); + return tid !== undefined; + }); + return tid!; + } + + function processProbeFixture(pid: number | { pidFile: string }): { modeFile: string; env: NodeJS.ProcessEnv } { + const binDir = join(configuration.happyHomeDir, `generation-probe-${randomUUID()}`); + mkdirSync(binDir); + const modeFile = join(binDir, 'mode'); + const quotedModeFile = `'${modeFile.replace(/'/g, `'\\''`)}'`; + const expectedPid = typeof pid === 'number' + ? `'${pid}'` + : `"$(cat '${pid.pidFile.replace(/'/g, `'\\''`)}' 2>/dev/null)"`; + // Only this test-owned PID's lstart probe is changed. All identity, + // process-tree, and orphan scans use the real system ps unchanged. + writeFileSync(join(binDir, 'ps'), `#!/bin/sh +if [ "$1" = '-p' ] && [ "$2" = ${expectedPid} ] && [ "$3" = '-o' ] && [ "$4" = 'lstart=' ] && [ -f ${quotedModeFile} ]; then + case "$(cat ${quotedModeFile})" in + block) exit 1 ;; + replace) printf '%s\\n' 'different-generation'; exit 0 ;; + esac +fi +exec /bin/ps "$@" +`, { mode: 0o755 }); + return { + modeFile, + env: { + PATH: `${binDir}${path.delimiter}${process.env.PATH ?? ''}`, + HAPI_RUNNER_HEARTBEAT_INTERVAL: '200', + HAPI_DISABLE_VERSION_HANDOFF: '1', + }, + }; + } + + it.skipIf(process.platform !== 'linux')('releases a previous-boot record without stopping the reused live PID or claiming session exit', async () => { + const pid = await spawnUnrelatedProcess(); + const currentBoot = readFileSync('/proc/sys/kernel/random/boot_id', 'utf8').trim(); + const oldBoot = currentBoot === '00000000-0000-4000-8000-000000000000' + ? '00000000-0000-4000-8000-000000000001' + : '00000000-0000-4000-8000-000000000000'; + const sessionId = randomUUID(); + await restartWithResumeRecords([{ + requestedSessionId: sessionId, + confirmedSessionId: sessionId, + pid, + // A matching marker must not override proof of a different boot. + processStartMarker: getProcessStartMarker(pid)!, + bootId: oldBoot, + }]); + + expect(readResumeRecords().some(record => record.requestedSessionId === sessionId)).toBe(false); + expect(isProcessAlive(pid)).toBe(true); + expectNoExitTombstone(sessionId, pid); + expect(await stopRunnerSession(sessionId)).toBe('unknown'); + expect(isProcessAlive(pid)).toBe(true); + }); + + it.skipIf(process.platform !== 'linux')('releases a legacy record whose PID was reused by a live nonleader thread', async () => { + const pid = await spawnUnrelatedProcess(); + const tid = await findFixtureThread(pid); + expect(isProcessAlive(tid)).toBe(true); + expect(getProcessStartMarker(tid)).toBeNull(); + const sessionId = randomUUID(); + await restartWithResumeRecords([{ + requestedSessionId: sessionId, + confirmedSessionId: sessionId, + pid: tid, + processStartMarker: 'Sun Oct 25 04:46:27 2026', + }]); + + expect(readResumeRecords().some(record => record.requestedSessionId === sessionId)).toBe(false); + expect(isProcessAlive(pid)).toBe(true); + expect(isProcessAlive(tid)).toBe(true); + expectNoExitTombstone(sessionId, tid); + expect(await stopRunnerSession(sessionId)).toBe('unknown'); + expect(isProcessAlive(tid)).toBe(true); + }); + + it.skipIf(process.platform !== 'linux')('does not adopt or terminate a session webhook naming a nonleader thread', async () => { + const pid = await spawnUnrelatedProcess(); + const tid = await findFixtureThread(pid); + const sessionId = randomUUID(); + await notifyRunnerSessionStarted(sessionId, { + path: '/tmp', + host: hostname(), + homeDir: '/tmp', + happyHomeDir: configuration.happyHomeDir, + happyLibDir: process.cwd(), + happyToolsDir: process.cwd(), + hostPid: tid, + startedBy: 'runner', + machineId: 'test-generation-machine', + }); + + expect((await listRunnerSessions()).some(item => item.happySessionId === sessionId)).toBe(false); + expect(readResumeRecords().some(item => item.requestedSessionId === sessionId)).toBe(false); + expect(isProcessAlive(pid)).toBe(true); + expect(isProcessAlive(tid)).toBe(true); + }); + + it.skipIf(process.platform !== 'linux')('preserves live legacy and same-boot records when restarting the runner', async () => { + const legacyPid = await spawnUnrelatedProcess(); + const currentPid = await spawnUnrelatedProcess(); + const uppercasePid = await spawnUnrelatedProcess(); + const malformedPid = await spawnUnrelatedProcess(); + const bootId = readFileSync('/proc/sys/kernel/random/boot_id', 'utf8').trim(); + const records: ResumeRecord[] = [legacyPid, currentPid, uppercasePid, malformedPid].map(pid => { + const sessionId = randomUUID(); + return { + requestedSessionId: sessionId, + confirmedSessionId: sessionId, + pid, + processStartMarker: getProcessStartMarker(pid)!, + ...(pid === legacyPid ? {} : { + bootId: pid === uppercasePid ? bootId.toUpperCase() + : pid === malformedPid ? 'not-a-boot-id' : bootId, + }), + }; + }); + await restartWithResumeRecords(records); + + const { bootId: _invalidBoot, ...legacyMalformed } = records[3]; + expect(readResumeRecords()).toEqual(expect.arrayContaining([ + records[0], records[1], { ...records[2], bootId }, legacyMalformed, + ])); + for (const record of records) { + expect(isProcessAlive(record.pid)).toBe(true); + expectNoExitTombstone(record.requestedSessionId, record.pid); + } + }); + + it.skipIf(process.platform !== 'linux')('keeps an unverifiable record through stop and releases a later heartbeat mismatch without exit tombstones', async () => { + const pid = await spawnUnrelatedProcess(); + const sessionId = randomUUID(); + const record: ResumeRecord = { + requestedSessionId: sessionId, + confirmedSessionId: sessionId, + pid, + processStartMarker: getProcessStartMarker(pid)!, + bootId: readFileSync('/proc/sys/kernel/random/boot_id', 'utf8').trim(), + }; + const fixture = processProbeFixture(pid); + writeFileSync(fixture.modeFile, 'block'); + await restartWithResumeRecords([record], fixture.env); + + expect(readResumeRecords()).toContainEqual(record); + expect(await stopRunnerSession(sessionId)).toBe('still_alive'); + expect(readResumeRecords()).toContainEqual(record); + expect(isProcessAlive(pid)).toBe(true); + expectNoExitTombstone(sessionId, pid); + + unlinkSync(fixture.modeFile); + const heartbeat = (await readRunnerState())?.lastHeartbeat; + await waitFor(async () => (await readRunnerState())?.lastHeartbeat !== heartbeat); + expect(readResumeRecords()).toContainEqual(record); + expectNoExitTombstone(sessionId, pid); + + writeFileSync(fixture.modeFile, 'replace'); + await waitFor(async () => !readResumeRecords().some(item => item.requestedSessionId === sessionId)); + expect(isProcessAlive(pid)).toBe(true); + expectNoExitTombstone(sessionId, pid); + expect(await stopRunnerSession(sessionId)).toBe('unknown'); + }); + + it.skipIf(process.platform !== 'linux')('preserves an adopted process and its record when stop cannot verify its generation', async () => { + const pid = await spawnUnrelatedProcess(); + const sessionId = randomUUID(); + const fixture = processProbeFixture(pid); + await restartWithResumeRecords([], fixture.env); + await notifyRunnerSessionStarted(sessionId, { + path: '/tmp', + host: hostname(), + homeDir: '/tmp', + happyHomeDir: configuration.happyHomeDir, + happyLibDir: process.cwd(), + happyToolsDir: process.cwd(), + hostPid: pid, + hostStartMarker: getProcessStartMarker(pid)!, + startedBy: 'runner', + machineId: 'test-generation-machine', + }); + const record = readResumeRecords().find(item => item.requestedSessionId === sessionId); + expect(record).toBeDefined(); + + writeFileSync(fixture.modeFile, 'block'); + expect(await stopRunnerSession(sessionId)).toBe('still_alive'); + expect(readResumeRecords()).toContainEqual(record); + expect((await listRunnerSessions()).some(item => item.happySessionId === sessionId)).toBe(true); + expect(isProcessAlive(pid)).toBe(true); + expectNoExitTombstone(sessionId, pid); + + unlinkSync(fixture.modeFile); + const heartbeat = (await readRunnerState())?.lastHeartbeat; + await waitFor(async () => (await readRunnerState())?.lastHeartbeat !== heartbeat); + expect(isProcessAlive(pid)).toBe(true); + expect(readResumeRecords()).toContainEqual(record); + }); + + it.skipIf(process.platform !== 'linux')('settles an unconfirmed startup on generation mismatch and cancels its old kill deadline', async () => { + const fixtureDir = join(configuration.happyHomeDir, `unconfirmed-generation-${randomUUID()}`); + mkdirSync(fixtureDir); + const pidFile = join(fixtureDir, 'pid'); + const preload = join(fixtureDir, 'preload.ts'); + // Bun runs this before the CLI entrypoint. Park only the test session; + // runner launchers and the runner itself still execute their real code. + writeFileSync(preload, `import { writeFileSync } from 'node:fs'; +if (process.argv[2] === 'claude') { + writeFileSync(${JSON.stringify(pidFile)}, String(process.pid)); + setInterval(() => {}, 1000); + await new Promise(() => {}); +} +`); + const fixture = processProbeFixture({ pidFile }); + const webhookTimeoutMs = 3000; + await restartWithResumeRecords([], { + ...fixture.env, + BUN_OPTIONS: `--preload=${preload}`, + HAPI_RUNNER_WEBHOOK_TIMEOUT_MS: String(webhookTimeoutMs), + }); + const state = await readRunnerState(); + const sessionId = randomUUID(); + const spawnRequest = fetch(`http://127.0.0.1:${state!.httpPort}/spawn-session`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ directory: '/tmp', sessionId }), + }).then(async response => ({ status: response.status, body: await response.json() as { error?: string } })); + // Teardown may stop the runner while a deliberately failing Red assertion + // leaves this request pending. Observe its rejection in that case too. + void spawnRequest.catch(() => {}); + await waitFor(async () => existsSync(pidFile)); + const pid = Number(readFileSync(pidFile, 'utf8')); + trackRunnerPid(pid, 'unconfirmed-generation-session'); + await waitFor(async () => readResumeRecords().some(record => record.pid === pid)); + const startupObservedAt = Date.now(); + expect(isProcessAlive(pid)).toBe(true); + + writeFileSync(fixture.modeFile, 'replace'); + await waitFor(async () => !readResumeRecords().some(record => record.pid === pid)); + const result = await Promise.race([ + spawnRequest, + new Promise(resolve => setTimeout(() => resolve(null), 1000)), + ]); + expect(result, 'generation invalidation must settle startup before its old timeout').not.toBeNull(); + expect(result!.status).toBe(500); + expect(result!.body.error).toMatch(/generation changed before startup was confirmed/); + expectNoExitTombstone(sessionId, pid); + + // Cross the original deadline: its timer must have been cancelled instead + // of signalling the now-untrusted PID after the binding was discarded. + await waitFor(async () => Date.now() > startupObservedAt + webhookTimeoutMs + 300, webhookTimeoutMs + 1000); + expect(isProcessAlive(pid)).toBe(true); + expectNoExitTombstone(sessionId, pid); + }); + + it.skipIf(process.platform !== 'linux')('persists the boot ID of a newly spawned session across runner restart', async () => { + const response = await spawnTrackedSession('/tmp'); + expect(response.success).toBe(true); + const record = readResumeRecords().find(item => item.confirmedSessionId === response.sessionId); + expect(record).toBeDefined(); + expect(record!.bootId).toBe(readFileSync('/proc/sys/kernel/random/boot_id', 'utf8').trim()); + await restartWithResumeRecords([record!]); + expect(readResumeRecords()).toContainEqual(record); + expect(isProcessAlive(record!.pid)).toBe(true); + }); + + it.skipIf(process.platform !== 'linux')('resumes the same Hub session once after recovering a stale thread record', async () => { + const pid = await spawnUnrelatedProcess(); + const tid = await findFixtureThread(pid); + const machineId = JSON.parse(readFileSync(configuration.settingsFile, 'utf8')).machineId; + expect(typeof machineId).toBe('string'); + const api = await ApiClient.create(); + const session = await api.getOrCreateSession({ + tag: `resume-generation-${randomUUID()}`, + metadata: { + path: '/tmp', + host: hostname(), + homeDir: '/tmp', + happyHomeDir: configuration.happyHomeDir, + happyLibDir: process.cwd(), + happyToolsDir: process.cwd(), + machineId, + flavor: 'claude', + }, + state: null, + }); + trackSession(session.id, 'resumed-generation-session'); + expect(session.active).toBe(false); + const registrationMarker = join(configuration.happyHomeDir, `rpc-registration-${randomUUID()}`); + const registrationPreload = `${registrationMarker}.ts`; + // Exercise the state-file/Hub-registration gap without delaying RPC or + // resume requests after registration. Preserve register-before-running. + writeFileSync(registrationPreload, `import { writeFileSync } from 'node:fs'; +if (process.argv[2] === 'runner' && process.argv[3] === 'start-sync') { + const { RpcHandlerManager } = await import(${JSON.stringify(join(process.cwd(), 'src/api/rpc/RpcHandlerManager.ts'))}); + const onSocketConnect = RpcHandlerManager.prototype.onSocketConnect; + RpcHandlerManager.prototype.onSocketConnect = function(socket) { + writeFileSync(${JSON.stringify(registrationMarker)}, String(process.pid)); + Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 1500); + return onSocketConnect.call(this, socket); + }; +} +`); + await restartWithResumeRecords([{ + requestedSessionId: session.id, + confirmedSessionId: session.id, + pid: tid, + processStartMarker: 'Sun Oct 25 04:46:27 2026', + }], { BUN_OPTIONS: `--preload=${registrationPreload}` }); + await waitFor(async () => existsSync(registrationMarker)); + expect(readFileSync(registrationMarker, 'utf8')).toBe(String(runnerPid)); + + const auth = await fetch(`${configuration.apiUrl}/api/auth`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ accessToken: configuration.cliApiToken }), + }); + expect(auth.status).toBe(200); + const { token } = await auth.json() as { token: string }; + const resume = () => fetch(`${configuration.apiUrl}/api/sessions/${session.id}/resume`, { + method: 'POST', + headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' }, + body: '{}', + }); + const responses = await Promise.all([resume(), resume()]); + for (const response of responses) { + const body = await response.json(); + expect(body).toEqual({ type: 'success', sessionId: session.id }); + expect(response.status).toBe(200); + } + const retry = await resume(); + expect(retry.status).toBe(200); + const sessions = await listRunnerSessions(); + expect(sessions.filter(item => item.happySessionId === session.id)).toHaveLength(1); + expect(isProcessAlive(pid)).toBe(true); + expect(isProcessAlive(tid)).toBe(true); + }, 40_000); + it('preserves shared Codex support after a runner heartbeat', async () => { const initial = await readRunnerState(); expect(initial?.sharedCodexRuntime).toBe(true); diff --git a/cli/src/utils/process.ts b/cli/src/utils/process.ts index 1842e036..2c5f67b9 100644 --- a/cli/src/utils/process.ts +++ b/cli/src/utils/process.ts @@ -1,5 +1,6 @@ import type { ChildProcess } from 'node:child_process'; import spawn from 'cross-spawn'; +import { readFileSync } from 'node:fs'; export const isWindows = (): boolean => process.platform === 'win32'; @@ -71,6 +72,34 @@ export function windowsProcessMarkerCimCommand(pid: number): string { ); } +/** Linux boot and thread identity are independent of a potentially reused PID. */ +export function normalizeBootId(value: unknown): string | null { + return typeof value === 'string' && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value.trim()) + ? value.trim().toLowerCase() + : null; +} + +export function getSystemBootId(): string | null { + if (process.platform !== 'linux') return null; + try { + return normalizeBootId(readFileSync('/proc/sys/kernel/random/boot_id', 'utf8')); + } catch { + return null; + } +} + +export function getProcessThreadGroupId(pid: number): number | null { + if (process.platform !== 'linux' || !Number.isSafeInteger(pid) || pid <= 0) return null; + try { + const status = readFileSync(`/proc/${pid}/status`, 'utf8'); + const match = /^Tgid:[ \t]+([0-9]+)[ \t]*$/m.exec(status); + const tgid = match ? Number(match[1]) : 0; + return Number.isSafeInteger(tgid) && tgid > 0 ? tgid : null; + } catch { + return null; + } +} + /** Stable marker for one OS PID generation; null means the platform probe failed. */ export function getProcessStartMarker(pid: number): string | null { if (!Number.isInteger(pid) || pid <= 0) return null; diff --git a/cli/src/utils/processGeneration.test.ts b/cli/src/utils/processGeneration.test.ts new file mode 100644 index 00000000..57c644cb --- /dev/null +++ b/cli/src/utils/processGeneration.test.ts @@ -0,0 +1,68 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' +import { readFileSync } from 'node:fs' + +vi.mock('node:fs', async (importOriginal) => { + const actual = await importOriginal() + return { ...actual, readFileSync: vi.fn(actual.readFileSync) } +}) + +import { getSystemBootId, getProcessThreadGroupId, normalizeBootId } from './process' + +const originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform')! +const bootId = 'e7c92900-5693-4ba0-b40c-9dfc57512edb' +const read = vi.mocked(readFileSync) + +function platform(value: string) { + Object.defineProperty(process, 'platform', { configurable: true, value }) +} + +afterEach(() => { + Object.defineProperty(process, 'platform', originalPlatform) + read.mockReset() +}) + +describe('Linux recovered process identity', () => { + it('reads and normalizes boot identity', () => { + platform('linux') + read.mockReturnValue(`${bootId.toUpperCase()}\n`) + expect(getSystemBootId()).toBe(bootId) + expect(read).toHaveBeenCalledWith('/proc/sys/kernel/random/boot_id', 'utf8') + }) + + it.each([undefined, null, '', 'invalid', 17])('rejects malformed boot identity %s', value => { + expect(normalizeBootId(value)).toBeNull() + }) + + it('reads the exact thread group field', () => { + platform('linux') + read.mockReturnValue('Name:\tgdbus\nTgid:\t2067\nPid:\t2109\n') + expect(getProcessThreadGroupId(2109)).toBe(2067) + expect(read).toHaveBeenCalledWith('/proc/2109/status', 'utf8') + }) + + it.each(['Tgid: 0', 'Tgid: -1', 'Tgid: 12junk', 'NotTgid: 12', 'Tgid: 9007199254740992'])('does not invent evidence from %s', value => { + platform('linux') + read.mockReturnValue(value) + expect(getProcessThreadGroupId(2109)).toBeNull() + }) + + it('keeps unavailable procfs evidence unknown', () => { + platform('linux') + read.mockImplementation(() => { throw new Error('EACCES') }) + expect(getSystemBootId()).toBeNull() + expect(getProcessThreadGroupId(2109)).toBeNull() + }) + + it.each(['win32', 'darwin'])('does not probe procfs on %s', value => { + platform(value) + expect(getSystemBootId()).toBeNull() + expect(getProcessThreadGroupId(2109)).toBeNull() + expect(read).not.toHaveBeenCalled() + }) + + it.each([0, -1, NaN, 1.5, Infinity])('does not probe invalid pid %s', value => { + platform('linux') + expect(getProcessThreadGroupId(value)).toBeNull() + expect(read).not.toHaveBeenCalled() + }) +})