11import { describe , expect , test } from "bun:test" ;
2+ import { mkdirSync , mkdtempSync } from "node:fs" ;
3+ import { tmpdir } from "node:os" ;
4+ import { join } from "node:path" ;
5+ import { retainNativeMainOwner } from "../../src/codex/native-main-owner" ;
26import { flushConfigDirHardening } from "../../src/config/paths" ;
37import {
48 defaultFixtureTeardownDeps ,
59 drainAndRemoveFixtureRoots ,
610 type FixtureTeardownDeps ,
711 type FixtureTeardownPlan ,
812} from "../helpers/fixture-teardown" ;
13+ import { removeTreeWithRetry } from "../helpers/remove-tree" ;
914
1015function harness ( overrides : Partial < FixtureTeardownDeps > = { } ) {
1116 const calls : string [ ] = [ ] ;
@@ -14,6 +19,7 @@ function harness(overrides: Partial<FixtureTeardownDeps> = {}) {
1419 settleConfigFlights : async root => { calls . push ( `flight:${ root } ` ) ; } ,
1520 closeHistory : ( ) => { calls . push ( "history" ) ; } ,
1621 drainAcl : async root => { calls . push ( `acl:${ root } ` ) ; } ,
22+ openLocks : ( ) => [ ] ,
1723 remove : root => { calls . push ( `remove:${ root } ` ) ; } ,
1824 ...overrides ,
1925 } ;
@@ -127,4 +133,71 @@ describe("fixture removal ownership", () => {
127133 h . plan . restoreEnvironment = ( ) => { throw last ; } ;
128134 await expect ( drainAndRemoveFixtureRoots ( h . plan , h . deps ) ) . rejects . toBe ( first ) ;
129135 } ) ;
136+
137+ test ( "an owner still open under a root keeps only that root and names it" , async ( ) => {
138+ const h = harness ( { openLocks : root => ( root === "A" ? [ "A/codex" ] : [ ] ) } ) ;
139+ await expect ( drainAndRemoveFixtureRoots ( h . plan , h . deps ) ) . rejects . toThrow ( "native-main owner still open for A/codex" ) ;
140+ expect ( h . calls ) . not . toContain ( "remove:A" ) ;
141+ expect ( h . calls ) . toContain ( "remove:B" ) ;
142+ expect ( h . calls . filter ( call => call === "restore" ) ) . toHaveLength ( 1 ) ;
143+ } ) ;
144+
145+ test ( "the default lock probe sees a held owner below the root until its release closes" , async ( ) => {
146+ const root = mkdtempSync ( join ( tmpdir ( ) , "ocx-teardown-lock-" ) ) ;
147+ const codexHome = join ( root , "codex" ) ;
148+ const sibling = `${ root } -sibling` ;
149+ mkdirSync ( codexHome ) ;
150+ const owner = retainNativeMainOwner (
151+ { codexHome, homeId : `teardown-lock-${ process . pid } ` } as never ,
152+ { hardenPath : async ( ) => { } } ,
153+ ) ;
154+ try {
155+ await new Promise < void > ( held => owner . subscribe ( snapshot => { if ( snapshot . status === "held" ) held ( ) ; } ) ) ;
156+ expect ( defaultFixtureTeardownDeps . openLocks ( root ) ) . toEqual ( [ codexHome ] ) ;
157+ expect ( defaultFixtureTeardownDeps . openLocks ( codexHome ) ) . toEqual ( [ codexHome ] ) ;
158+ expect ( defaultFixtureTeardownDeps . openLocks ( sibling ) ) . toEqual ( [ ] ) ;
159+ await owner . release ( ) ;
160+ expect ( defaultFixtureTeardownDeps . openLocks ( root ) ) . toEqual ( [ ] ) ;
161+ } finally {
162+ await owner . release ( ) ;
163+ removeTreeWithRetry ( root ) ;
164+ }
165+ } ) ;
166+
167+ test ( "an owner paused mid-acquisition keeps its root until preparation and release finish" , async ( ) => {
168+ const root = mkdtempSync ( join ( tmpdir ( ) , "ocx-teardown-acquire-" ) ) ;
169+ const codexHome = join ( root , "codex" ) ;
170+ mkdirSync ( codexHome ) ;
171+ let entered ! : ( ) => void , resume ! : ( ) => void ;
172+ const hardening = new Promise < void > ( done => { entered = done ; } ) ;
173+ const resumed = new Promise < void > ( done => { resume = done ; } ) ;
174+ const owner = retainNativeMainOwner (
175+ { codexHome, homeId : `teardown-acquire-${ process . pid } ` } as never ,
176+ { hardenPath : async ( ) => { entered ( ) ; await resumed ; } } ,
177+ ) ;
178+ const removed : string [ ] = [ ] ;
179+ const plan = ( ) : FixtureTeardownPlan => ( { roots : [ { path : root , remove : path => { removed . push ( path ) ; removeTreeWithRetry ( path ) ; } } ] , restoreEnvironment : ( ) => { } } ) ;
180+ try {
181+ // The descriptor and SQLite handle exist only in locals while the hardener runs.
182+ await hardening ;
183+ expect ( owner . snapshot ( ) . status ) . toBe ( "acquiring" ) ;
184+ await expect ( drainAndRemoveFixtureRoots ( plan ( ) ) ) . rejects . toThrow ( `native-main owner still open for ${ codexHome } ` ) ;
185+ expect ( removed ) . toEqual ( [ ] ) ;
186+
187+ const released = owner . release ( ) ;
188+ // Still closing: the release waits for the paused acquisition.
189+ await expect ( drainAndRemoveFixtureRoots ( plan ( ) ) ) . rejects . toThrow ( "native-main owner still open" ) ;
190+ expect ( removed ) . toEqual ( [ ] ) ;
191+
192+ resume ( ) ;
193+ await released ;
194+ expect ( defaultFixtureTeardownDeps . openLocks ( root ) ) . toEqual ( [ ] ) ;
195+ await drainAndRemoveFixtureRoots ( plan ( ) ) ;
196+ expect ( removed ) . toEqual ( [ root ] ) ;
197+ } finally {
198+ resume ( ) ;
199+ await owner . release ( ) ;
200+ if ( removed . length === 0 ) removeTreeWithRetry ( root ) ;
201+ }
202+ } ) ;
130203} ) ;
0 commit comments