Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: pre-landing review fixes

garrytan9373524e9450
diff --git a/src/commands/upgrade-restart.ts b/src/commands/upgrade-restart.ts
index 51a028388f..a4b389fd53 100644
--- a/src/commands/upgrade-restart.ts
+++ b/src/commands/upgrade-restart.ts
@@ -19,7 +19,7 @@ import { writeCliError } from '../cli/cli-error.ts';
import { setCliExitVerdict } from '../core/cli-force-exit.ts';
import { SERVE_LAUNCHD_LABEL, SERVE_SYSTEMD_UNIT, serveServiceRestartArgv } from '../core/serve-service.ts';
import { autopilotLockPath, resolveAutopilotJob } from '../core/autopilot-paths.ts';
-import { cronLineBelongsToBrain, plistPath, systemdUnitPath } from './autopilot/jobs.ts';
+import { cronLineBelongsToBrain, plistPath, scriptWrapperPath, systemdUnitPath } from './autopilot/jobs.ts';
import { readSupervisorBuild, readSupervisorPid } from '../core/minions/supervisor-pid.ts';
import { parseEtimeToMs, readWorkers } from '../core/minions/worker-registry.ts';
import { installedVersion, runtimeInstallRoot } from '../core/runtime-build.ts';
@@ -128,14 +128,15 @@ function readCrontab(): string | null {
/**
* #8776: what starts autopilot again when it exits, for an autopilot launchd or systemd does not run: this brain's own
- * gbrain cron line (`gbrain autopilot --install` on Linux without a user systemd bus), else a launcher the operator
+ * gbrain cron line (`gbrain autopilot --install` on Linux without a user systemd bus: its wrapper every 5 minutes or more often), else a launcher the operator
* declared with `self_upgrade.autopilot_launcher external`; null when neither, so the upgrade refuses as before.
* `pid` is the daemon this brain's autopilot lock names, the only one the hand-off may stop.
*/
export function autopilotLauncher(crontab: () => string | null = readCrontab, declared: string | undefined = loadConfig()?.self_upgrade?.autopilot_launcher,
lockPid: number | null = readAutopilotLockPid()): AutopilotLauncher | null {
const job = resolveAutopilotJob();
- if (crontab()?.split('\n').some(line => cronLineBelongsToBrain(line, job))) return { kind: 'cron', wait_ms: HANDOFF_WAIT_MS, pid: lockPid };
+ const relaunches = (line: string) => /^\*(?:\/[1-5])?\s+\*\s+\*\s+\*\s+\*\s/.test(line.trim()) && scriptWrapperPath(line) !== null && cronLineBelongsToBrain(line, job);
+ if (crontab()?.split('\n').some(relaunches)) return { kind: 'cron', wait_ms: HANDOFF_WAIT_MS, pid: lockPid };
return declared === 'external' ? { kind: 'declared', wait_ms: HANDOFF_WAIT_MS, pid: lockPid } : null;
}
diff --git a/src/core/upgrade-restart.ts b/src/core/upgrade-restart.ts
index 959b1341cc..05eb833415 100644
--- a/src/core/upgrade-restart.ts
+++ b/src/core/upgrade-restart.ts
@@ -291,7 +291,7 @@ export function planRestart(processes: readonly HostProcess[], ctx: RestartConte
const byPid = new Map(processes.map(p => [p.pid, p]));
const addService = (role: 'serve' | 'autopilot', pid: number) => services.set(role, [...(services.get(role) ?? []), pid]);
const handoffs = new Map<number, Extract<RestartStep, { kind: 'handoff' }>>();
- const launcherOf = (p: HostProcess) => (ctx.autopilotLauncher?.pid === p.pid ? ctx.autopilotLauncher : null);
+ const launcherOf = (p: HostProcess) => (ctx.autopilotLauncher?.pid === p.pid && p.started_ms !== null && p.args ? ctx.autopilotLauncher : null);
const addHandoff = (p: HostProcess, launcher: AutopilotLauncher) => handoffs.set(p.pid, { kind: 'handoff', role: 'autopilot', pid: p.pid, started_ms: p.started_ms, launcher });
const runsUnder = (p: HostProcess, service: ServiceHandle | null) => !!service && service.pid !== null && (p.pid === service.pid || !!p.ancestors?.includes(service.pid));
const kill = (p: HostProcess, instruction: string) => manual.push({ role: p.role, pid: p.pid, version: p.version, argv: ['kill', String(p.pid)], instruction });
@@ -319,6 +319,7 @@ export function planRestart(processes: readonly HostProcess[], ctx: RestartConte
case 'autopilot':
if (runsUnder(p, ctx.autopilotService)) addService('autopilot', p.pid);
else if (launcherOf(p)) addHandoff(p, launcherOf(p)!);
+ else if (ctx.autopilotLauncher?.pid === p.pid) kill(p, `${label(p)} is known only from its heartbeat row (the process table could not be read), so the upgrade cannot confirm it is the process to stop before handing autopilot to its launcher: stop it (\`kill ${p.pid}\`) and let the launcher start it again, then run the upgrade.`);
else manual.push({ role: p.role, pid: p.pid, version: p.version, argv: ['gbrain', 'autopilot', '--install'],
instruction: `${label(p)} was started outside gbrain's autopilot service (by a cron entry, a wrapper or by hand), so the upgrade cannot restart it. `
+ 'Either run `gbrain autopilot --install`, which runs autopilot under launchd, systemd or a gbrain cron line so every upgrade restarts it (retire the launcher that starts it now first, or both race for the autopilot lock), '
diff --git a/test/upgrade-restart-external-autopilot-8776.test.ts b/test/upgrade-restart-external-autopilot-8776.test.ts
index 5df58a869d..5091bdefd9 100644
--- a/test/upgrade-restart-external-autopilot-8776.test.ts
+++ b/test/upgrade-restart-external-autopilot-8776.test.ts
@@ -102,13 +102,24 @@ describe('runRestartSteps and verifyRestart: the hand-off', () => {
});
});
+describe('planRestart: an autopilot known only from its heartbeat row', () => {
+ test('is not handed off, since the upgrade cannot confirm the process to stop, and names kill <pid>', () => {
+ const plan = planRestart([autopilot(30, OLD, { started_ms: null, args: undefined, ancestors: undefined })], ctx({ expected: PENDING_BUILD, autopilotLauncher: CRON }));
+ expect(plan.steps).toEqual([]);
+ expect(plan.manual.map(m => [m.role, m.pid, m.argv])).toEqual([['autopilot', 30, ['kill', '30']]]);
+ expect(plan.manual[0]!.instruction).toContain('known only from its heartbeat row');
+ });
+});
+
describe('autopilotLauncher', () => {
- test('this brain\'s gbrain cron line is a launcher; a commented line, a monitor-only line or none is not; the declared key is the fallback', () => {
+ test('this brain\'s gbrain cron line is a launcher; a commented line, a monitor-only line, a line that does not start its wrapper every 5 minutes or none is not; the declared key is the fallback', () => {
const job = resolveAutopilotJob();
const line = `*/5 * * * * '${join(job.homeDir, 'autopilot-run.sh')}' >> '${job.logPath}' 2>&1${job.cronMarker ? ` ${job.cronMarker}` : ''}`;
expect(autopilotLauncher(() => `MAILTO=""\n${line}\n`, undefined, 30)).toEqual({ kind: 'cron', wait_ms: 360_000, pid: 30 });
expect(autopilotLauncher(() => `# ${line}\n*/30 * * * * gbrain autopilot --status\n`, undefined, 30)).toBeNull();
expect(autopilotLauncher(() => null, undefined, 30)).toBeNull();
+ expect(autopilotLauncher(() => `0 0 * * * gbrain autopilot pause${job.cronMarker ? ` ${job.cronMarker}` : ''}\n`, undefined, 30)).toBeNull();
+ expect(autopilotLauncher(() => `${line.replace('*/5 * * * *', '0 3 * * *')}\n`, undefined, 30)).toBeNull();
expect(autopilotLauncher(() => null, 'external', null)).toEqual({ kind: 'declared', wait_ms: 360_000, pid: null });
});