Skip to content

Commit 59e2052

Browse files
test(workspace): report inherited syncs separately in the leak guard
A sync already running when a test starts (beforeAll, module load, an earlier test) is now reported as inherited, not as the test's own. The message notes a deferred start can land on the next test and that a stuck sync stalls later flushes. The skill-list stub accepts a `Request`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_0174HUYiaceApqHP7EKMNgnb
1 parent f73ed57 commit 59e2052

2 files changed

Lines changed: 33 additions & 10 deletions

File tree

‎packages/opencode/test/altimate/workspace/skill-list-fixture.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
* Other hosts pass through to the fetch that was installed before. Returns the restore. */
1010
export function stubEmptySkillList(host: string): () => void {
1111
const original = globalThis.fetch
12-
globalThis.fetch = (async (input: string | URL, init?: RequestInit) => {
13-
const url = new URL(String(input))
12+
globalThis.fetch = (async (input: string | URL | Request, init?: RequestInit) => {
13+
const url = new URL(input instanceof Request ? input.url : String(input))
1414
if (url.host !== host) return original(input, init)
1515
const skills = url.pathname.endsWith("/skills")
1616
// The page is echoed back: the skill-list parser rejects a page that does not match its request.

‎packages/opencode/test/preload.ts‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import fs from "fs/promises"
66
import { setTimeout as sleep } from "node:timers/promises"
77
import { 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+
]
136145
const reportedSyncs = new WeakSet<Promise<unknown>>()
146+
let runningAtStart = new Set<Promise<unknown>>()
147+
beforeEach(() => {
148+
runningAtStart = new Set(runningSyncs().map(([, sync]) => sync))
149+
})
137150
afterEach(() => {
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

Comments
 (0)