Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(upgrade): read the crontab only when an autopilot is running (#8776)

garrytan266007564a9e
diff --git a/src/commands/upgrade-restart.ts b/src/commands/upgrade-restart.ts
index 9fe0c03a89..b90a2a4322 100644
--- a/src/commands/upgrade-restart.ts
+++ b/src/commands/upgrade-restart.ts
@@ -140,6 +140,11 @@ export function autopilotLauncher(crontab: () => string | null = readCrontab, de
return declared === 'external' ? { kind: 'declared', wait_ms: HANDOFF_WAIT_MS, pid: lockPid } : null;
}
+/** The launcher matters only for a running autopilot, so a host without one never reads the crontab. */
+export function launcherFor(processes: HostProcess[], launcher: () => AutopilotLauncher | null = autopilotLauncher): AutopilotLauncher | null {
+ return processes.some(p => p.role === 'autopilot') ? launcher() : null;
+}
+
function readAutopilotLockPid(): number | null {
try {
const pid = Number(readFileSync(autopilotLockPath(), 'utf8').trim());
@@ -287,7 +292,8 @@ export async function preflightHostRestart(opts: HostRestartOptions): Promise<bo
const { sources, close } = await hostSources();
try {
const services = hostServices();
- const plan = planRestart(await collectHostProcesses(sources), { expected: PENDING_BUILD, serveService: services.serve, autopilotService: services.autopilot, stopMcp: opts.stopMcp, ownInstallRoot: ownInstallRoot(), autopilotLauncher: autopilotLauncher() });
+ const processes = await collectHostProcesses(sources);
+ const plan = planRestart(processes, { expected: PENDING_BUILD, serveService: services.serve, autopilotService: services.autopilot, stopMcp: opts.stopMcp, ownInstallRoot: ownInstallRoot(), autopilotLauncher: launcherFor(processes) });
const refusal = restartRefusal(plan, opts.rerunArgv, false);
if (!refusal) return true;
setCliExitVerdict(writeCliError(refusal, 'upgrade', { json: opts.json }));
@@ -306,7 +312,7 @@ export async function upgradeRestartReadiness(): Promise<RestartPlan | null> {
const processes = sources.processTable() === null ? null : await collectHostProcesses(sources);
if (!processes) return null;
const services = hostServices();
- return planRestart(processes, { expected: PENDING_BUILD, serveService: services.serve, autopilotService: services.autopilot, stopMcp: false, ownInstallRoot: ownInstallRoot(), autopilotLauncher: autopilotLauncher() });
+ return planRestart(processes, { expected: PENDING_BUILD, serveService: services.serve, autopilotService: services.autopilot, stopMcp: false, ownInstallRoot: ownInstallRoot(), autopilotLauncher: launcherFor(processes) });
}
/** `gbrain upgrade --restart-only [--stop-mcp] [--dry-run] [--json]`: restart every process onto the installed build and verify. */
@@ -315,7 +321,7 @@ export async function runHostRestart(opts: HostRestartOptions & { dryRun: boolea
try {
const services = hostServices();
const before = await collectHostProcesses(sources);
- const plan = planRestart(before, { expected: VERSION, serveService: services.serve, autopilotService: services.autopilot, stopMcp: opts.stopMcp, ownInstallRoot: ownInstallRoot(), autopilotLauncher: autopilotLauncher() });
+ const plan = planRestart(before, { expected: VERSION, serveService: services.serve, autopilotService: services.autopilot, stopMcp: opts.stopMcp, ownInstallRoot: ownInstallRoot(), autopilotLauncher: launcherFor(before) });
const { probeCliSocket } = await import('../core/cli-socket/probe.ts');
const warmBefore = await probeCliSocket();
const planDoc = { expected: VERSION, processes: before, steps: plan.steps, manual: plan.manual, warm_socket: warmBefore, notes };
diff --git a/test/upgrade-restart-external-autopilot-8776.test.ts b/test/upgrade-restart-external-autopilot-8776.test.ts
index a67022f723..7887cef078 100644
--- a/test/upgrade-restart-external-autopilot-8776.test.ts
+++ b/test/upgrade-restart-external-autopilot-8776.test.ts
@@ -9,7 +9,7 @@
import { describe, expect, test } from 'bun:test';
import { join } from 'node:path';
import { planRestart, restartProblems, runRestartSteps, verifyRestart, PENDING_BUILD, type AutopilotLauncher, type HostProcess, type RestartContext, type RestartExecutor } from '../src/core/upgrade-restart.ts';
-import { autopilotLauncher, restartRefusal } from '../src/commands/upgrade-restart.ts';
+import { autopilotLauncher, launcherFor, restartRefusal } from '../src/commands/upgrade-restart.ts';
import { checkSelfUpgradeHealth } from '../src/commands/doctor/checks/upgrade-health.ts';
import { resolveAutopilotJob } from '../src/core/autopilot-paths.ts';
import { parseSelfUpgradeConfigValue, SELF_UPGRADE_CONFIG_LEAVES } from '../src/core/self-upgrade.ts';
@@ -168,6 +168,22 @@ describe('autopilotLauncher', () => {
expect(autopilotLauncher(() => null, 'external', null)).toEqual({ kind: 'declared', wait_ms: 360_000, pid: null });
});
+ test('the plan reads the crontab only when an autopilot is running: none, no read and no launcher; one, the cron line is read and the hand-off plans', () => {
+ const job = resolveAutopilotJob();
+ const line = `*/5 * * * * '${join(job.homeDir, 'autopilot-run.sh')}' >> '${job.logPath}' 2>&1${job.cronMarker ? ` ${job.cronMarker}` : ''}`;
+ let reads = 0;
+ const launcher = () => autopilotLauncher(() => { reads++; return `${line}\n`; }, undefined, 30);
+ const serve: HostProcess = { pid: 8, ppid: null, role: 'serve', version: OLD, started_ms: 1_000, ancestors: [1], args: ['serve'] };
+ expect(launcherFor([], launcher)).toBeNull();
+ expect(launcherFor([serve], launcher)).toBeNull();
+ expect(reads).toBe(0);
+ const found = launcherFor([serve, autopilot()], launcher);
+ expect(reads).toBe(1);
+ expect(found).toEqual(CRON);
+ expect(planRestart([autopilot()], ctx({ expected: PENDING_BUILD, autopilotLauncher: found })).steps)
+ .toEqual([{ kind: 'handoff', role: 'autopilot', pid: 30, started_ms: 1_000, launcher: CRON }]);
+ });
+
test('self_upgrade.autopilot_launcher is a registered leaf that accepts external only', () => {
expect(SELF_UPGRADE_CONFIG_LEAVES).toContain('autopilot_launcher');
expect(parseSelfUpgradeConfigValue('autopilot_launcher', 'external')).toEqual({ ok: true, value: 'external' });