Skip to content

Commit 768e1f9

Browse files
authored
fix(exec): recheck legacy approvals after doctor (#118769)
1 parent 4553fad commit 768e1f9

3 files changed

Lines changed: 43 additions & 32 deletions

File tree

‎src/infra/exec-approvals-migration-gate.ts‎

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import path from "node:path";
44
import { resolveExecApprovalsPath } from "./exec-approvals-config.js";
55

66
const DOCTOR_CLAIM_SUFFIX = ".doctor-importing";
7-
const legacyPresenceCache = new Map<string, boolean>();
7+
// Doctor usually runs in another process, so cache only the steady-state absence;
8+
// a present verdict must be re-probed on the next attempt after Doctor finishes.
9+
const legacyAbsenceCache = new Set<string>();
810

911
/**
1012
* Doctor repairs whichever state directory its own environment resolves to, so a bare
@@ -42,26 +44,20 @@ export function assertNoPendingLegacyExecApprovals(
4244
options: { pathMayExist?: (filePath: string) => boolean } = {},
4345
): void {
4446
const sourcePath = resolveExecApprovalsPath();
45-
let hasLegacy = legacyPresenceCache.get(sourcePath);
46-
if (hasLegacy === undefined) {
47-
const probe = options.pathMayExist ?? pathMayExist;
48-
// Bound both Doctor rename directions: source -> claim and claim -> source.
49-
const sourceBefore = probe(sourcePath);
50-
const claim = probe(`${sourcePath}${DOCTOR_CLAIM_SUFFIX}`);
51-
const sourceAfter = probe(sourcePath);
52-
hasLegacy = sourceBefore || claim || sourceAfter;
53-
legacyPresenceCache.set(sourcePath, hasLegacy);
47+
if (legacyAbsenceCache.has(sourcePath)) {
48+
return;
5449
}
55-
if (hasLegacy) {
50+
const probe = options.pathMayExist ?? pathMayExist;
51+
// Bound both Doctor rename directions: source -> claim and claim -> source.
52+
const sourceBefore = probe(sourcePath);
53+
const claim = probe(`${sourcePath}${DOCTOR_CLAIM_SUFFIX}`);
54+
const sourceAfter = probe(sourcePath);
55+
if (sourceBefore || claim || sourceAfter) {
5656
throw new ExecApprovalsMigrationRequiredError(sourcePath);
5757
}
58-
}
59-
60-
/** Forget one process-local legacy probe after Doctor resolves the source. */
61-
export function resetLegacyExecApprovalsPresenceCache(env: NodeJS.ProcessEnv = process.env): void {
62-
legacyPresenceCache.delete(resolveExecApprovalsPath(env));
58+
legacyAbsenceCache.add(sourcePath);
6359
}
6460

6561
export function resetExecApprovalsMigrationGateForTest(): void {
66-
legacyPresenceCache.clear();
62+
legacyAbsenceCache.clear();
6763
}

‎src/infra/exec-approvals-store.test.ts‎

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -382,19 +382,36 @@ describe("exec approvals SQLite store", () => {
382382
}
383383
});
384384

385-
it("blocks runtime reads while the retired JSON or claim exists", () => {
386-
closeOpenClawStateDatabaseForTest();
387-
const stateDir = process.env.OPENCLAW_STATE_DIR;
388-
if (!stateDir) {
389-
throw new Error("missing test state dir");
390-
}
391-
fs.writeFileSync(
392-
path.join(stateDir, "exec-approvals.json"),
393-
serializeExecApprovals({ version: 1, agents: {} }),
394-
);
395-
execApprovalsStoreTesting.reset();
396-
expect(() => loadExecApprovals()).toThrow(ExecApprovalsMigrationRequiredError);
397-
});
385+
it.each([
386+
["source", ""],
387+
["Doctor claim", ".doctor-importing"],
388+
])(
389+
"blocks runtime reads while the retired %s exists, then rechecks after removal",
390+
(_, suffix) => {
391+
closeOpenClawStateDatabaseForTest();
392+
const stateDir = process.env.OPENCLAW_STATE_DIR;
393+
if (!stateDir) {
394+
throw new Error("missing test state dir");
395+
}
396+
const sourcePath = path.join(stateDir, "exec-approvals.json");
397+
const legacyPath = `${sourcePath}${suffix}`;
398+
fs.writeFileSync(legacyPath, serializeExecApprovals({ version: 1, agents: {} }));
399+
execApprovalsStoreTesting.reset();
400+
let caught: unknown;
401+
try {
402+
loadExecApprovals();
403+
} catch (error) {
404+
caught = error;
405+
}
406+
expect(caught).toBeInstanceOf(ExecApprovalsMigrationRequiredError);
407+
expect(caught).toMatchObject({
408+
message: `Legacy exec approvals exist at ${sourcePath}. Run \`openclaw doctor --fix\` with OPENCLAW_STATE_DIR set to ${stateDir} before using exec approvals.`,
409+
});
410+
411+
fs.rmSync(legacyPath);
412+
expect(loadExecApprovals()).toMatchObject({ version: 1, agents: {} });
413+
},
414+
);
398415

399416
it("scopes the doctor command to the blocked state directory", () => {
400417
// A bare `openclaw doctor --fix` repairs the default root, leaving a scoped

‎src/infra/state-migrations.exec-approvals.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
resolveExecApprovalsPath,
77
tryParsePersistedExecApprovals,
88
} from "./exec-approvals-config.js";
9-
import { resetLegacyExecApprovalsPresenceCache } from "./exec-approvals-migration-gate.js";
109
import {
1110
readExecApprovalsConfigRow,
1211
serializeExecApprovals,
@@ -321,7 +320,6 @@ async function migrateWithExclusiveStateOwnership(params: {
321320
`Legacy exec approvals were removed, but their receipt could not be finalized: ${String(error)}`,
322321
);
323322
}
324-
resetLegacyExecApprovalsPresenceCache(params.env);
325323
return {
326324
changes: [decisionMessage(result.decision, result.removeSource)],
327325
warnings,

0 commit comments

Comments
 (0)