@@ -6,7 +6,7 @@ import fs from "fs/promises"
66import { setTimeout as sleep } from "node:timers/promises"
77import { afterAll } from "bun:test"
88// altimate_change start — the workspace skill-sync leak guard at the end of this file
9- import { afterEach } from "bun:test"
9+ import { afterEach , beforeEach } from "bun:test"
1010// altimate_change end
1111
1212// Set XDG env vars FIRST, before any src/ imports
@@ -133,19 +133,42 @@ initProjectors()
133133// The store is read through its process-global key rather than by importing skill-sync, so this
134134// preload does not change when any test first loads the workspace modules. It never waits: a
135135// guard that waited here shifted the timing of the following test enough to hang one.
136+ // Syncs already running when a test starts are reported as inherited, not as that test's. One
137+ // whose start was deferred past its own test's end still lands on the next test; the message
138+ // says so, since telling them apart would mean tagging each sync with its owner in skill-sync.
139+ type SkillSyncStore = { inFlight ?: Map < string , Promise < unknown > > }
140+ const runningSyncs = ( ) => [
141+ ...( ( globalThis as unknown as Record < symbol , SkillSyncStore | undefined > ) [
142+ Symbol . for ( "altimate.workspace.skill-sync.store" )
143+ ] ?. inFlight ?? [ ] ) ,
144+ ]
136145const reportedSyncs = new WeakSet < Promise < unknown > > ( )
146+ let runningAtStart = new Set < Promise < unknown > > ( )
147+ beforeEach ( ( ) => {
148+ runningAtStart = new Set ( runningSyncs ( ) . map ( ( [ , sync ] ) => sync ) )
149+ } )
137150afterEach ( ( ) => {
138- const store = ( globalThis as unknown as Record < symbol , { inFlight ?: Map < string , Promise < unknown > > } | undefined > ) [
139- Symbol . for ( "altimate.workspace.skill-sync.store" )
140- ]
141- const left = [ ...( store ?. inFlight ?? [ ] ) ] . filter ( ( [ , sync ] ) => ! reportedSyncs . has ( sync ) )
151+ const left = runningSyncs ( ) . filter ( ( [ , sync ] ) => ! reportedSyncs . has ( sync ) )
142152 if ( left . length === 0 ) return
143153 // Blamed once: a sync that never settles must not fail every test after this one.
144154 for ( const [ , sync ] of left ) reportedSyncs . add ( sync )
155+ const dirs = ( syncs : typeof left ) => syncs . map ( ( [ dir ] ) => dir ) . join ( ", " )
156+ const own = left . filter ( ( [ , sync ] ) => ! runningAtStart . has ( sync ) )
157+ const inherited = left . filter ( ( [ , sync ] ) => runningAtStart . has ( sync ) )
145158 throw new Error (
146- `This test left ${ left . length } workspace skill sync(s) running: ${ left . map ( ( [ dir ] ) => dir ) . join ( ", " ) } . ` +
147- "Await what the test starts: recordApprovedBinding(..., { awaitBackfill: true }), or flushPendingSyncs() " +
148- "for a sync the code under test leaves detached." ,
159+ [
160+ own . length > 0 &&
161+ `This test left ${ own . length } workspace skill sync(s) running: ${ dirs ( own ) } ` +
162+ "(or the test before it did, if that test's sync started after it ended)." ,
163+ inherited . length > 0 &&
164+ `${ inherited . length } workspace skill sync(s) were already running when this test started, ` +
165+ `from a beforeAll, module load or an earlier test: ${ dirs ( inherited ) } .` ,
166+ "Await what a test starts: recordApprovedBinding(..., { awaitBackfill: true }), or " +
167+ "flushPendingSyncs() for a sync the code under test leaves detached. A sync that never " +
168+ "settles also holds every later flushPendingSyncs() for its full bound." ,
169+ ]
170+ . filter ( Boolean )
171+ . join ( " " ) ,
149172 )
150173} )
151174// altimate_change end
0 commit comments