@@ -11,6 +11,7 @@ import type {
1111 PluginDoctorStateMigrationContext ,
1212} from "openclaw/plugin-sdk/runtime-doctor-migrations" ;
1313import { getSessionEntry , upsertSessionEntry } from "openclaw/plugin-sdk/session-store-runtime" ;
14+ import { closeOpenClawAgentDatabasesForTest } from "openclaw/plugin-sdk/sqlite-runtime-testing" ;
1415import { afterEach , describe , expect , it } from "vitest" ;
1516import {
1617 legacyConfigRules ,
@@ -59,6 +60,16 @@ function openBindingStore(env: NodeJS.ProcessEnv) {
5960 } ) ;
6061}
6162
63+ async function removeCodexDoctorFixture ( stateDir : string ) : Promise < void > {
64+ // Doctor migrations open per-agent databases and leave the shared state database open under
65+ // the temporary state dir; both must be released before removal or Windows keeps the files
66+ // locked and the removal fails with EBUSY. Agent close first: it releases leases through
67+ // shared state, so the reverse order can reopen it.
68+ closeOpenClawAgentDatabasesForTest ( ) ;
69+ resetPluginStateStoreForTests ( ) ;
70+ await fs . rm ( stateDir , { recursive : true , force : true } ) ;
71+ }
72+
6273async function createBindingMigrationFixture ( options : {
6374 binding ?: Record < string , unknown > ;
6475 legacySharedRoot ?: boolean ;
@@ -272,7 +283,7 @@ describe("codex doctor contract", () => {
272283 } ) ,
273284 ) . toMatchObject ( { agentHarnessId : "codex" } ) ;
274285 } finally {
275- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
286+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
276287 }
277288 } ) ;
278289
@@ -357,7 +368,7 @@ describe("codex doctor contract", () => {
357368 fs . readFile ( fixture . storePath , "utf8" ) . then ( JSON . parse ) ,
358369 ) . resolves . not . toHaveProperty ( "agent:main:session-1.agentHarnessId" ) ;
359370
360- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
371+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
361372 } ) ;
362373
363374 it . each ( [
@@ -396,7 +407,7 @@ describe("codex doctor contract", () => {
396407 await expect ( fs . access ( `${ fixture . sidecarPath } .migrated` ) ) . rejects . toThrow ( ) ;
397408 await expect ( openBindingStore ( fixture . env ) . entries ( ) ) . resolves . toEqual ( [ ] ) ;
398409
399- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
410+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
400411 } ) ;
401412
402413 it ( "migrates a shared-root binding to the configured system agent" , async ( ) => {
@@ -448,7 +459,7 @@ describe("codex doctor contract", () => {
448459 ) . toMatchObject ( { agentHarnessId : "codex" } ) ;
449460 await expect ( fs . access ( `${ fixture . sidecarPath } .migrated` ) ) . resolves . toBeUndefined ( ) ;
450461
451- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
462+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
452463 } ) ;
453464
454465 it ( "keeps an agent-scoped shared-root binding with its explicit owner" , async ( ) => {
@@ -492,7 +503,7 @@ describe("codex doctor contract", () => {
492503 ) . resolves . toMatchObject ( { sessionId : "explicit-ops-owner" } ) ;
493504 await expect ( fs . access ( `${ fixture . sidecarPath } .migrated` ) ) . resolves . toBeUndefined ( ) ;
494505
495- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
506+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
496507 } ) ;
497508
498509 it ( "bounds oversized legacy fingerprints before plugin-state import" , async ( ) => {
@@ -560,7 +571,7 @@ describe("codex doctor contract", () => {
560571 warnings : [ ] ,
561572 } ) ;
562573
563- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
574+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
564575 } ) ;
565576
566577 it ( "normalizes a partial raw conversation import before copying the session row" , async ( ) => {
@@ -638,7 +649,7 @@ describe("codex doctor contract", () => {
638649 warnings : [ ] ,
639650 } ) ;
640651
641- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
652+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
642653 } ) ;
643654
644655 it ( "normalizes retained raw conversation and session rows before comparison" , async ( ) => {
@@ -710,7 +721,7 @@ describe("codex doctor contract", () => {
710721 warnings : [ ] ,
711722 } ) ;
712723
713- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
724+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
714725 } ) ;
715726
716727 it ( "rejects an explicit session file locator outside the session directory" , async ( ) => {
@@ -743,7 +754,7 @@ describe("codex doctor contract", () => {
743754 ) . toBeUndefined ( ) ;
744755 await expect ( openBindingStore ( fixture . env ) . entries ( ) ) . resolves . toEqual ( [ ] ) ;
745756
746- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
757+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
747758 } ) ;
748759
749760 it ( "deduplicates session-store aliases before classifying binding ownership" , async ( ) => {
@@ -795,7 +806,7 @@ describe("codex doctor contract", () => {
795806 expect ( configuredIndex [ "agent:main:aliased-store" ] ) . not . toHaveProperty ( "agentHarnessId" ) ;
796807 expect ( targetIndex [ "agent:main:aliased-store" ] ) . not . toHaveProperty ( "agentHarnessId" ) ;
797808
798- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
809+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
799810 } ) ;
800811
801812 it ( "resolves relative session files from a symlinked store path" , async ( ) => {
@@ -847,7 +858,7 @@ describe("codex doctor contract", () => {
847858 `${ sessionKey } .agentHarnessId` ,
848859 ) ;
849860
850- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
861+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
851862 } ) ;
852863
853864 it . each ( [
@@ -929,7 +940,7 @@ describe("codex doctor contract", () => {
929940 retired : true ,
930941 } ) ;
931942
932- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
943+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
933944 } ,
934945 ) ;
935946
@@ -986,7 +997,7 @@ describe("codex doctor contract", () => {
986997 retired : true ,
987998 } ) ;
988999
989- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
1000+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
9901001 } ) ;
9911002
9921003 it ( "does not resurrect a retired session generation from its legacy sidecar" , async ( ) => {
@@ -1041,7 +1052,7 @@ describe("codex doctor contract", () => {
10411052 fs . readFile ( path . join ( fixture . sessionsDir , "sessions.json" ) , "utf8" ) . then ( JSON . parse ) ,
10421053 ) . resolves . not . toHaveProperty ( `${ sessionKey } .agentHarnessId` ) ;
10431054
1044- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
1055+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
10451056 } ) ;
10461057
10471058 it . each ( [ "active" , "cleared" ] as const ) (
@@ -1081,7 +1092,7 @@ describe("codex doctor contract", () => {
10811092 warnings : [ ] ,
10821093 } ) ;
10831094
1084- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
1095+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
10851096 } ,
10861097 ) ;
10871098
@@ -1106,7 +1117,7 @@ describe("codex doctor contract", () => {
11061117 await expect ( fs . access ( fixture . sidecarPath ) ) . rejects . toThrow ( ) ;
11071118 await expect ( fs . access ( `${ fixture . sidecarPath } .migrated` ) ) . resolves . toBeUndefined ( ) ;
11081119
1109- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
1120+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
11101121 } ) ;
11111122
11121123 it ( "retains a zero-owner sidecar when canonical plugin state is malformed" , async ( ) => {
@@ -1135,7 +1146,7 @@ describe("codex doctor contract", () => {
11351146 await expect ( fs . access ( fixture . sidecarPath ) ) . resolves . toBeUndefined ( ) ;
11361147 await expect ( store . lookup ( bindingKey ) ) . resolves . toEqual ( malformed ) ;
11371148
1138- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
1149+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
11391150 } ) ;
11401151
11411152 it ( "retains mixed Codex and foreign ambiguous binding owners" , async ( ) => {
@@ -1164,7 +1175,7 @@ describe("codex doctor contract", () => {
11641175 await expect ( fs . access ( fixture . sidecarPath ) ) . resolves . toBeUndefined ( ) ;
11651176 await expect ( openBindingStore ( fixture . env ) . entries ( ) ) . resolves . toEqual ( [ ] ) ;
11661177
1167- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
1178+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
11681179 } ) ;
11691180
11701181 it ( "retains a sidecar owned by a foreign harness without importing plugin state" , async ( ) => {
@@ -1188,7 +1199,7 @@ describe("codex doctor contract", () => {
11881199 await expect ( fs . access ( fixture . sidecarPath ) ) . resolves . toBeUndefined ( ) ;
11891200 await expect ( openBindingStore ( fixture . env ) . entries ( ) ) . resolves . toEqual ( [ ] ) ;
11901201
1191- await fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ;
1202+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
11921203 } ) ;
11931204
11941205 it . each ( [
@@ -1230,10 +1241,8 @@ describe("codex doctor contract", () => {
12301241 await expect ( fs . access ( `${ fixture . sidecarPath } .migrated` ) ) . rejects . toThrow ( ) ;
12311242 await expect ( openBindingStore ( fixture . env ) . entries ( ) ) . resolves . toEqual ( [ ] ) ;
12321243
1233- await Promise . all ( [
1234- fs . rm ( fixture . stateDir , { recursive : true , force : true } ) ,
1235- fs . rm ( externalDir , { recursive : true , force : true } ) ,
1236- ] ) ;
1244+ await removeCodexDoctorFixture ( fixture . stateDir ) ;
1245+ await fs . rm ( externalDir , { recursive : true , force : true } ) ;
12371246 } ) ;
12381247
12391248 it ( "does not scan above stateDir or follow escaped external store locators" , async ( ) => {
@@ -1284,10 +1293,8 @@ describe("codex doctor contract", () => {
12841293
12851294 await expect ( migration . detectLegacyState ( params ) ) . resolves . toBeNull ( ) ;
12861295
1287- await Promise . all ( [
1288- fs . rm ( outerDir , { recursive : true , force : true } ) ,
1289- fs . rm ( outsideDir , { recursive : true , force : true } ) ,
1290- ] ) ;
1296+ await removeCodexDoctorFixture ( outerDir ) ;
1297+ await fs . rm ( outsideDir , { recursive : true , force : true } ) ;
12911298 } ) ;
12921299
12931300 it ( "renames old approval-routed destructive plugin policy values" , ( ) => {
0 commit comments