Skip to content

Commit b23b964

Browse files
committed
fix(state): surface lease inspection failures
1 parent 7fbfa2b commit b23b964

3 files changed

Lines changed: 45 additions & 3 deletions

File tree

‎src/infra/state-migrations.transcript-directives.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,37 @@ describe("historical transcript directive migration", () => {
624624
expect(opened.db.isOpen).toBe(true);
625625
});
626626

627+
it("surfaces lease inspection failures from preflight", async () => {
628+
const stateDir = makeTempDir(tempDirs, "transcript-directive-lease-inspection-");
629+
const env = { OPENCLAW_STATE_DIR: stateDir };
630+
const opened = openOpenClawAgentDatabase({ agentId: "main", env });
631+
const databasePath = opened.path;
632+
closeOpenClawAgentDatabasesForTest();
633+
const agentDatabaseLease = await import("../state/openclaw-agent-db-lease.js");
634+
vi.spyOn(agentDatabaseLease, "assertNoOpenClawAgentDatabaseLeases").mockImplementation(() => {
635+
throw new Error("shared-state lease inspection failed");
636+
});
637+
638+
const result = await migrateHistoricalTranscriptDirectives({ env });
639+
640+
expect(result.changes).toEqual([]);
641+
expect(result.warnings).toEqual([
642+
expect.stringContaining(
643+
`Skipped historical transcript directive migration preflight for ${databasePath}: Error: shared-state lease inspection failed`,
644+
),
645+
]);
646+
const database = openNodeSqliteDatabase(databasePath, { readOnly: true });
647+
try {
648+
expect(
649+
database
650+
.prepare("SELECT 1 FROM schema_meta WHERE meta_key = ?")
651+
.get("historical-transcript-directives-v1"),
652+
).toBeUndefined();
653+
} finally {
654+
database.close();
655+
}
656+
});
657+
627658
it("leaves canonical archives and their active writer untouched", async () => {
628659
const stateDir = makeTempDir(tempDirs, "transcript-directive-current-archive-");
629660
const env = { OPENCLAW_STATE_DIR: stateDir };

‎src/infra/state-migrations.transcript-directives.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { TranscriptEvent } from "../config/sessions/session-accessor.sqlite
44
import { updateSqliteTranscriptEventJsonInTransaction } from "../config/sessions/session-accessor.sqlite-transcript-store.js";
55
import { OPENCLAW_AGENT_SCHEMA_VERSION } from "../state/openclaw-agent-db-contract.js";
66
import {
7+
OpenClawAgentDatabaseLeaseActiveError,
78
assertAgentDatabaseMaintenanceAuthority,
89
assertNoOpenClawAgentDatabaseLeases,
910
} from "../state/openclaw-agent-db-lease.js";
@@ -248,8 +249,11 @@ function hasActiveAgentDatabaseLease(agentId: string, env: NodeJS.ProcessEnv): b
248249
try {
249250
assertNoOpenClawAgentDatabaseLeases(agentId, { env });
250251
return false;
251-
} catch {
252-
return true;
252+
} catch (error) {
253+
if (error instanceof OpenClawAgentDatabaseLeaseActiveError) {
254+
return true;
255+
}
256+
throw error;
253257
}
254258
}
255259

‎src/state/openclaw-agent-db-lease.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ export const AGENT_DATABASE_MAINTENANCE_LEASE = {
2828
key: "global",
2929
} as const;
3030

31+
export class OpenClawAgentDatabaseLeaseActiveError extends Error {
32+
constructor(message: string) {
33+
super(message);
34+
this.name = "OpenClawAgentDatabaseLeaseActiveError";
35+
}
36+
}
37+
3138
const maintenanceAuthority = new AsyncLocalStorage<OpenClawStateLeaseContext>();
3239

3340
export function runWithAgentDatabaseMaintenanceAuthority<T>(
@@ -204,7 +211,7 @@ export function assertNoOpenClawAgentDatabaseLeases(
204211
}, options);
205212
if (leaseStillExists && (!agentId || row.agent_id === agentId)) {
206213
const remediation = agentId ? "." : "; stop that process and rerun openclaw doctor --fix.";
207-
throw new Error(
214+
throw new OpenClawAgentDatabaseLeaseActiveError(
208215
`Agent ${row.agent_id} database is still open in another process${remediation}`,
209216
);
210217
}

0 commit comments

Comments
 (0)