test(desktop): let the attach-race winner exist before the loop gives up

The test wrote the winning Core's receipt and token from inside the
spawn, without awaiting it, while the readiness loop ran with zero
delay. On a slow runner the loop finished first, the re-read found a
receipt without its token, and start() reported a false conflict. The
probe now waits for the winner's files, which is the order the test is
about.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
alvinfeiandClaude Opus 5.5 committed 2026-10-27 09:00:50 +08:00
1 parent d89afb3887
commit 567a8b349d
1 file changed
+13 -2
@@ -234,9 +234,14 @@ describe("Core ownership", () => {
it("attaches when a standalone Core wins the race with the first spawn", async () => {
const dataDir = await mkdtemp(path.join(tmpdir(), "core-attach-race-"));
let probes = 0;
// The winner has finished publishing its receipt and token by the time
// our child loses the lock. Writing them from inside the spawn raced the
// readiness loop: on a slow runner the token was not there yet, the
// re-read rejected the receipt, and the test saw a false conflict.
let winner: Promise<void> = Promise.resolve();
const spawnProcess = vi.fn(() => {
const child = fakeChild();
void writeRuntime(dataDir, {
winner = writeRuntime(dataDir, {
schema_version: 1,
service: "one-memory-core",
instance_id: "cli-race-winner",
@@ -245,7 +250,9 @@ describe("Core ownership", () => {
host: "127.0.0.1",
port: 9236,
started_at: "2026-09-19T01:00:00Z",
}).then(() => child.emit("exit", 1, null));
}).then(() => {
child.emit("exit", 1, null);
});
return child;
});
const supervisor = new CoreSupervisor({
@@ -256,6 +263,10 @@ describe("Core ownership", () => {
spawnProcess,
probeHealth: async (port, token) => {
probes += 1;
// Our own child never answers: it lost the race. Waiting here for the
// winner's files keeps the readiness loop from concluding before the
// winner exists, which is the order this test is about.
await winner;
if (port !== 9236 || token !== "attached-secret")
throw new Error("not attached yet");
return {