Skip to content

Commit 2e53d46

Browse files
sahrizviclaude
andcommitted
fix: keep a concurrent mirror's record and bound the signal-exit write
- `mirrorBlock` undoes its record on a confirmed "disabled" only while the record is still its own, so a block mirrored concurrently keeps its target. - The signal exit waits at most 500ms for the notice's stderr write, and a write that throws does not block `process.exit`. - Tests: the subprocess test resolves on `close` so stdout is drained, and skips the `sh` pipeline case on Windows; the pending-lookup test waits on a signal from the fetch stub instead of a fixed sleep and cleans up in `finally`; new case for a concurrent record surviving a disabled lookup. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011chv9Rmu7jQXokum1WNcs7
1 parent f6ea10b commit 2e53d46

4 files changed

Lines changed: 61 additions & 13 deletions

File tree

‎packages/opencode/src/altimate/workspace/memory-sync.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -973,12 +973,14 @@ export async function mirrorBlock(block: MemoryBlock, directory?: string): Promi
973973
const binding = await currentBinding(directory)
974974
if (!binding) return
975975
// Recorded before the memory-setting lookup, which can wait on the service: an exit while it is pending
976-
// leaves this block unsent too. Taken back only on a confirmed "disabled".
976+
// leaves this block unsent too. Taken back only on a confirmed "disabled", and only while it is still
977+
// this call's record: other blocks mirror concurrently and may have recorded since.
977978
const previous = mirrorRequests.last
978-
mirrorRequests.last = { directory, binding }
979+
const recorded: MirrorTarget = { directory, binding }
980+
mirrorRequests.last = recorded
979981
const memory = await memoryStatus(binding)
980982
if (memory === "disabled") {
981-
mirrorRequests.last = previous
983+
if (mirrorRequests.last === recorded) mirrorRequests.last = previous
982984
return
983985
}
984986
if (memory !== "enabled") return

‎packages/opencode/src/cli/cmd/run.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,8 +1561,17 @@ You are speaking to a non-technical business executive. Follow these rules stric
15611561
const notice = await unsent
15621562
?.then((m) => m?.unsentMemoryNoticeAfterRun(process.cwd(), 1_000))
15631563
.catch(() => null)
1564-
// process.exit does not wait for a pending write to a piped stderr.
1565-
if (notice) await new Promise<void>((resolve) => process.stderr.write(notice + EOL, () => resolve()))
1564+
// process.exit does not wait for a pending write to a piped stderr; wait briefly, never for a
1565+
// reader that has stopped.
1566+
if (notice)
1567+
await new Promise<void>((resolve) => {
1568+
setTimeout(resolve, 500)
1569+
try {
1570+
process.stderr.write(notice + EOL, () => resolve())
1571+
} catch {
1572+
resolve()
1573+
}
1574+
})
15661575
process.exit(code)
15671576
})
15681577
}

‎packages/opencode/test/altimate/workspace/memory-sync.test.ts‎

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -463,16 +463,52 @@ describe("mirrorBlock", () => {
463463
const inner = globalThis.fetch
464464
let release!: () => void
465465
const held = new Promise<void>((resolve) => (release = resolve))
466+
let entered!: () => void
467+
const lookupStarted = new Promise<void>((resolve) => (entered = resolve))
466468
globalThis.fetch = (async (input: any, init?: any) => {
467-
if (String(input).includes("/datamates/") && !String(input).includes("/memory")) await held
469+
if (String(input).includes("/datamates/") && !String(input).includes("/memory")) {
470+
entered()
471+
await held
472+
}
468473
return inner(input, init)
469474
}) as typeof fetch
470475
const mirror = mirrorBlock(block({ id: "pending-lookup" }), "/repo")
471-
await Bun.sleep(20)
472-
expect(lastMirrorTarget()).toEqual({ directory: "/repo", binding: BINDING as any })
473-
release()
474-
await mirror
475-
globalThis.fetch = inner
476+
try {
477+
await lookupStarted
478+
expect(lastMirrorTarget()).toEqual({ directory: "/repo", binding: BINDING as any })
479+
} finally {
480+
release()
481+
await mirror.catch(() => {})
482+
globalThis.fetch = inner
483+
}
484+
})
485+
486+
test("a disabled lookup takes back only its own record, not one a concurrent mirror made since", async () => {
487+
workspaces = [{ id: 42, name: "acme", memory_enabled: false }]
488+
const inner = globalThis.fetch
489+
let release!: () => void
490+
const held = new Promise<void>((resolve) => (release = resolve))
491+
let entered!: () => void
492+
const lookupStarted = new Promise<void>((resolve) => (entered = resolve))
493+
globalThis.fetch = (async (input: any, init?: any) => {
494+
if (String(input).includes("/datamates/") && !String(input).includes("/memory")) {
495+
entered()
496+
await held
497+
}
498+
return inner(input, init)
499+
}) as typeof fetch
500+
const mirror = mirrorBlock(block({ id: "disabled-slow" }), "/repo")
501+
const newer = { directory: "/other", binding: BINDING as any }
502+
try {
503+
await lookupStarted
504+
// Another block's mirror records its own target while this lookup is pending.
505+
;(globalThis as unknown as Record<symbol, { last?: unknown }>)[Symbol.for("altimate.memory-sync.mirror-requests")].last = newer
506+
} finally {
507+
release()
508+
await mirror.catch(() => {})
509+
globalThis.fetch = inner
510+
}
511+
expect(lastMirrorTarget()).toBe(newer)
476512
})
477513

478514
test("a workspace with memory turned off is not recorded: the block stays local by design", async () => {

‎packages/opencode/test/cli/run/run-stdin-process.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ function collect(child: ReturnType<typeof spawn>): Promise<{ code: number | null
1212
const started = Date.now()
1313
let out = ""
1414
child.stdout!.on("data", (chunk) => (out += chunk))
15-
return new Promise((resolve) => child.on("exit", (code) => resolve({ code, out, ms: Date.now() - started })))
15+
// `close`, not `exit`: it fires only after stdout is drained.
16+
return new Promise((resolve) => child.on("close", (code) => resolve({ code, out, ms: Date.now() - started })))
1617
}
1718

1819
describe("readRunStdin on a real stdin", () => {
@@ -36,7 +37,7 @@ describe("readRunStdin on a real stdin", () => {
3637
expect(JSON.parse((await collect(child)).out)).toEqual({ text: "the prompt\n", timedOut: false })
3738
})
3839

39-
test("a shell pipeline is read to EOF even when its first byte comes after the wait", async () => {
40+
test.skipIf(process.platform === "win32")("a shell pipeline is read to EOF even when its first byte comes after the wait", async () => {
4041
const child = spawn("sh", ["-c", `sleep 0.6; echo piped | "${process.execPath}" "${FIXTURE}"`], { env })
4142
expect(JSON.parse((await collect(child)).out)).toEqual({ text: "piped\n", timedOut: false })
4243
})

0 commit comments

Comments
 (0)