Skip to content

Commit 8d414fb

Browse files
committed
fix(doctor): preserve missing state startup signal
1 parent 6bd0f72 commit 8d414fb

5 files changed

Lines changed: 37 additions & 13 deletions

‎src/commands/doctor-state-integrity.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,7 @@ export async function noteStateIntegrity(
10551055
cfg: OpenClawConfig,
10561056
prompter: DoctorPrompterLike,
10571057
configPath?: string,
1058+
options?: { stateDirExistedAtStart?: boolean },
10581059
) {
10591060
const warnings: string[] = [];
10601061
const changes: string[] = [];
@@ -1102,6 +1103,11 @@ export async function noteStateIntegrity(
11021103
}
11031104

11041105
let stateDirExists = existsDir(stateDir);
1106+
if (stateDirExists && options?.stateDirExistedAtStart === false) {
1107+
warnings.push(
1108+
`- State directory was missing at doctor start and was initialized during startup checks (${displayStateDir}).`,
1109+
);
1110+
}
11051111
if (!stateDirExists) {
11061112
warnings.push(
11071113
`- CRITICAL: state directory missing (${displayStateDir}). Sessions, credentials, logs, and config are stored there.`,

‎src/commands/doctor.warns-state-directory-is-missing.e2e.test.ts‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ describe("doctor command", () => {
134134
terminalNoteMock.mockClear();
135135
});
136136

137-
it("warns when the state directory is missing", async () => {
137+
it("reports when the state directory was missing at doctor start", async () => {
138138
mockDoctorConfigSnapshot();
139139

140140
const missingDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-missing-state-"));
@@ -146,8 +146,10 @@ describe("doctor command", () => {
146146
});
147147
});
148148

149-
const stateNote = requireTerminalNote({ messageIncludes: "state directory missing" });
150-
expect(String(stateNote[0])).toContain("CRITICAL");
149+
requireTerminalNote({
150+
title: "State integrity",
151+
messageIncludes: "State directory was missing at doctor start",
152+
});
151153
});
152154

153155
it("routes browser readiness through health contributions and degrades gracefully when browser facade is unavailable", async () => {
@@ -873,7 +875,7 @@ describe("doctor command", () => {
873875
expect(skippedGatewayHealth).toBe(false);
874876
});
875877

876-
it("keeps gateway health probes when env password wins over an exec password ref", async () => {
878+
it("skips password-mode probes when configured password is an exec SecretRef", async () => {
877879
mockDoctorConfigSnapshot({
878880
config: {
879881
gateway: {
@@ -901,6 +903,7 @@ describe("doctor command", () => {
901903
const previousPassword = process.env.OPENCLAW_GATEWAY_PASSWORD;
902904
process.env.OPENCLAW_GATEWAY_PASSWORD = "fallback-password";
903905
try {
906+
callGateway.mockClear();
904907
await doctorCommand(createDoctorRuntime(), {
905908
nonInteractive: true,
906909
workspaceSuggestions: false,
@@ -913,15 +916,12 @@ describe("doctor command", () => {
913916
}
914917
}
915918

916-
const skippedGatewayHealth = terminalNoteMock.mock.calls.some(([message, title]) => {
917-
return (
918-
title === "Gateway" &&
919-
String(message).includes(
920-
"Gateway health probes skipped because gateway credentials use an exec SecretRef.",
921-
)
922-
);
919+
expect(callGateway).not.toHaveBeenCalled();
920+
requireTerminalNote({
921+
title: "Gateway",
922+
messageIncludes:
923+
"Gateway health probes skipped because gateway credentials use an exec SecretRef.",
923924
});
924-
expect(skippedGatewayHealth).toBe(false);
925925
});
926926

927927
it("keeps remote gateway health probes when env token wins over an exec password ref", async () => {

‎src/flows/doctor-health-contribution-runners.state.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ export async function runChannelIngressDeadLettersHealth(): Promise<void> {
8787

8888
export async function runStateIntegrityHealth(ctx: DoctorHealthFlowContext): Promise<void> {
8989
const { noteStateIntegrity } = await loadDoctorStateIntegrityModule();
90-
await noteStateIntegrity(ctx.cfg, ctx.prompter, ctx.configPath);
90+
await noteStateIntegrity(ctx.cfg, ctx.prompter, ctx.configPath, {
91+
stateDirExistedAtStart: ctx.stateDirExistedAtStart,
92+
});
9193
}
9294

9395
export async function runCodexSessionRouteHealth(ctx: DoctorHealthFlowContext): Promise<void> {

‎src/flows/doctor-health-contribution-types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export type DoctorHealthFlowContext = {
3636
postConfigWriteRepairsCommitted?: boolean;
3737
sourceConfigValid: boolean;
3838
configPath: string;
39+
/** Whether the selected state directory already existed before doctor startup work. */
40+
stateDirExistedAtStart?: boolean;
3941
env?: NodeJS.ProcessEnv;
4042
gatewayDetails?: ReturnType<typeof buildGatewayConnectionDetails>;
4143
healthOk?: boolean;

‎src/flows/doctor-health.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Doctor health flow renders interactive health check output.
2+
import fs from "node:fs";
23
import { intro as clackIntro, outro as clackOutro } from "@clack/prompts";
34
import { stylePromptTitle } from "../../packages/terminal-core/src/prompt-style.js";
45
import type { DoctorOptions } from "../commands/doctor-prompter.js";
6+
import { resolveStateDir } from "../config/paths.js";
57
import type { RuntimeEnv } from "../runtime.js";
68
import { createLazyRuntimeModule } from "../shared/lazy-runtime.js";
79
import type { DoctorHealthFlowContext } from "./doctor-health-contributions.js";
@@ -12,9 +14,20 @@ const outro = (message: string) => clackOutro(stylePromptTitle(message) ?? messa
1214

1315
const loadConfigModule = createLazyRuntimeModule(() => import("../config/config.js"));
1416

17+
function stateDirectoryExistsAtDoctorStart(): boolean {
18+
try {
19+
return fs.statSync(resolveStateDir()).isDirectory();
20+
} catch {
21+
return false;
22+
}
23+
}
24+
1525
/** Runs the full interactive doctor flow against the provided or default runtime. */
1626
export async function doctorCommand(runtime?: RuntimeEnv, options: DoctorOptions = {}) {
1727
const effectiveRuntime = runtime ?? (await import("../runtime.js")).defaultRuntime;
28+
// Config loading can initialize SQLite-backed state before integrity runs.
29+
// Preserve the entry fact so doctor can report that automatic initialization.
30+
const stateDirExistedAtStart = stateDirectoryExistsAtDoctorStart();
1831
if (options.repair === true || options.yes === true || options.generateGatewayToken === true) {
1932
const { assertConfigWriteAllowedInCurrentMode } = await loadConfigModule();
2033
assertConfigWriteAllowedInCurrentMode();
@@ -71,6 +84,7 @@ export async function doctorCommand(runtime?: RuntimeEnv, options: DoctorOptions
7184
cfgForPersistence: structuredClone(configResult.cfg),
7285
sourceConfigValid: configResult.sourceConfigValid ?? true,
7386
configPath: configResult.path ?? CONFIG_PATH,
87+
stateDirExistedAtStart,
7488
};
7589
const { runDoctorHealthContributions } = await import("./doctor-health-contributions.js");
7690
await runDoctorHealthContributions(ctx);

0 commit comments

Comments
 (0)