Skip to content

Commit 27d7665

Browse files
committed
fix: refresh readiness context after repair
1 parent 4312b9c commit 27d7665

2 files changed

Lines changed: 62 additions & 5 deletions

File tree

‎src/cli/daemon-cli/lifecycle.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ type RestartPostCheckContext = {
2020

2121
type RestartParams = {
2222
opts?: { json?: boolean };
23+
repairLoadedService?: (ctx: {
24+
json: boolean;
25+
stdout: NodeJS.WritableStream;
26+
state: unknown;
27+
issues: unknown[];
28+
}) => Promise<unknown>;
2329
postRestartCheck?: (ctx: RestartPostCheckContext) => Promise<void>;
2430
};
2531

@@ -352,6 +358,51 @@ describe("runDaemonRestart health checks", () => {
352358
expect(waitParams.env?.OPENCLAW_SYSTEMD_UNIT).toBe("openclaw-gateway-maintenance.service");
353359
});
354360

361+
it("re-reads the installed service environment after restart repair", async () => {
362+
service.readCommand
363+
.mockResolvedValueOnce({
364+
programArguments: ["openclaw", "gateway", "--port", "18789"],
365+
environment: { OPENCLAW_STATE_DIR: "/tmp/openclaw-stale-state" },
366+
})
367+
.mockResolvedValue({
368+
programArguments: ["openclaw", "gateway", "--port", "19001"],
369+
environment: { OPENCLAW_STATE_DIR: "/tmp/openclaw-repaired-state" },
370+
});
371+
repairLoadedGatewayServiceForStart.mockResolvedValue({
372+
result: "restarted",
373+
message: "Gateway service definition repaired and restarted.",
374+
loaded: true,
375+
});
376+
runServiceRestart.mockImplementation(async (params: RestartParams) => {
377+
await params.repairLoadedService?.({
378+
json: true,
379+
stdout: process.stdout,
380+
state: {},
381+
issues: [{ code: "version-mismatch", message: "old service" }],
382+
});
383+
await params.postRestartCheck?.({
384+
json: true,
385+
stdout: process.stdout,
386+
warnings: [],
387+
fail: (message: string) => {
388+
throw new Error(message);
389+
},
390+
});
391+
return true;
392+
});
393+
394+
await runDaemonRestart({ json: true });
395+
396+
expect(waitForGatewayHealthyRestart).toHaveBeenCalledWith(
397+
expect.objectContaining({
398+
port: 19_001,
399+
env: expect.objectContaining({
400+
OPENCLAW_STATE_DIR: "/tmp/openclaw-repaired-state",
401+
}),
402+
}),
403+
);
404+
});
405+
355406
it("repairs toward an explicitly configured gateway port", async () => {
356407
loadConfig.mockReturnValue({ gateway: { port: 19_001 } });
357408
resolveGatewayPort.mockReturnValue(19_001);

‎src/cli/daemon-cli/lifecycle.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,11 @@ export async function runDaemonRestart(opts: DaemonLifecycleOptions = {}): Promi
351351
let restartedWithoutServiceManager = false;
352352
const restartIntent = resolveGatewayRestartIntentOptions(opts);
353353
const configuredPort = await resolveExplicitGatewayConfigPort();
354-
const managedRestartContext = await resolveGatewayLifecycleContext(service).catch(async () => ({
354+
let managedRestartContext = await resolveGatewayLifecycleContext(service).catch(async () => ({
355355
port: await resolveGatewayPortFallback(),
356356
env: process.env,
357357
}));
358-
const managedRestartPort = configuredPort ?? managedRestartContext.port;
358+
let managedRestartPort = configuredPort ?? managedRestartContext.port;
359359
// An unmanaged run loop keeps its lock port across in-process restarts, even
360360
// when config changes underneath it. Use that port for both the signal and
361361
// health proof or a valid CLI/env override looks like a failed restart.
@@ -375,8 +375,8 @@ export async function runDaemonRestart(opts: DaemonLifecycleOptions = {}): Promi
375375
},
376376
checkTokenDrift: true,
377377
expectedPort: configuredPort,
378-
repairLoadedService: async ({ json, stdout, warn, state, issues }) =>
379-
await repairLoadedGatewayServiceForStart({
378+
repairLoadedService: async ({ json, stdout, warn, state, issues }) => {
379+
const result = await repairLoadedGatewayServiceForStart({
380380
action: "restart",
381381
service,
382382
port: configuredPort,
@@ -385,7 +385,13 @@ export async function runDaemonRestart(opts: DaemonLifecycleOptions = {}): Promi
385385
warn,
386386
state,
387387
issues,
388-
}),
388+
});
389+
// Repair rewrites the service definition, so the old command environment
390+
// no longer identifies where the restarted gateway publishes readiness.
391+
managedRestartContext = await resolveGatewayLifecycleContext(service);
392+
managedRestartPort = configuredPort ?? managedRestartContext.port;
393+
return result;
394+
},
389395
onNotLoaded: async () => {
390396
if (process.platform === "darwin") {
391397
const recovered = await recoverInstalledLaunchAgent({ result: "restarted" });

0 commit comments

Comments
 (0)