From 5dc528c13d97dc9107dedb35edc217cd34efe651 Mon Sep 17 00:00:00 2001 From: Yaacov Date: Mon, 23 Nov 2026 07:26:38 +0200 Subject: [PATCH 1/2] test(daemon): rejoin after the daemon drops the closed connection's lease The reconnect-within-grace test reopened the unit as soon as the first client's socket closed, but the daemon handles that close later. When the second open won the race the old lease was still counted (leases 2), which failed main on 11-22 (run 45051468599) and a PR run the same day. The test now watches unit events on the second connection and reopens only after the daemon broadcasts the lease drop, the state the test describes. The harness's event wait now resolves when the event arrives instead of polling its buffer every 25 ms. Both changes sit in SA-DAEMON-REJOIN marker blocks in Oh My Pi's test files. Proven by delaying the daemon's close handling by 300 ms: the old test fails exactly as CI did (expected 1, received 2 at line 110), 3/3; the new test passes with and without the delay. Co-Authored-By: Claude Opus 5.5 --- .../test/daemon/unit-lifecycle.test.ts | 14 ++++++++ .../test/fixtures/daemon-proc-harness.ts | 36 ++++++++++++++++++- scripts/scient/divergences.json | 12 +++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/packages/coding-agent/test/daemon/unit-lifecycle.test.ts b/packages/coding-agent/test/daemon/unit-lifecycle.test.ts index e814836151..3d7e2761e1 100644 --- a/packages/coding-agent/test/daemon/unit-lifecycle.test.ts +++ b/packages/coding-agent/test/daemon/unit-lifecycle.test.ts @@ -6,6 +6,9 @@ */ import { afterEach, describe, expect, it } from "bun:test"; import { DAEMON_RESUME_LEASE_GRACE_MS } from "../../src/daemon/protocol"; +// scient(SA-DAEMON-REJOIN): begin - The rejoin test waits for the daemon's lease-drop event. +import type { DaemonEvent, DaemonUnitEvent } from "../../src/daemon/protocol"; +// scient(SA-DAEMON-REJOIN): end import { type DaemonConn, pidAlive, rejection, shProc, TestDaemon, until } from "../fixtures/daemon-proc-harness"; const TEST_TIMEOUT_MS = 30_000; @@ -102,6 +105,17 @@ describe.skipIf(process.platform === "win32")("daemon unit lifecycle", () => { await first.close(); const second = await d.connect(); + // scient(SA-DAEMON-REJOIN): begin - The client's close settles before the daemon handles it: rejoin once the daemon has dropped the lease. + // Watching first means a drop after the describe still arrives as a buffered event. + await second.request({ op: "watch" }); + if ((await second.describe("rejoin")).leases !== 0) { + await second.event( + (event: DaemonEvent): event is DaemonUnitEvent => + event.ev === "unit" && event.unit.key === "rejoin" && event.unit.leases === 0, + "the dropped lease", + ); + } + // scient(SA-DAEMON-REJOIN): end const again = await second.openProc("rejoin", proc, { policy: { idleMs: 0 } }); expect(again.created).toBe(false); diff --git a/packages/coding-agent/test/fixtures/daemon-proc-harness.ts b/packages/coding-agent/test/fixtures/daemon-proc-harness.ts index 9838292db8..ab1f7a33b6 100644 --- a/packages/coding-agent/test/fixtures/daemon-proc-harness.ts +++ b/packages/coding-agent/test/fixtures/daemon-proc-harness.ts @@ -91,6 +91,9 @@ export class DaemonConn { readonly #pending = new Map void; reject: (error: Error) => void }>(); readonly #onResult: (result: unknown) => void; readonly #welcome = Promise.withResolvers(); + // scient(SA-DAEMON-REJOIN): begin - Event waits are woken by the event, not by polling. + readonly #eventWaiters = new Set<{ match: (event: DaemonEvent) => boolean; fail: (error: Error) => void }>(); + // scient(SA-DAEMON-REJOIN): end #welcomed = false; #isClosed = false; @@ -107,6 +110,10 @@ export class DaemonConn { if (!this.#welcomed) this.#welcome.reject(error); for (const pending of this.#pending.values()) pending.reject(error); this.#pending.clear(); + // scient(SA-DAEMON-REJOIN): begin - A closed connection fails the event waits. + for (const waiter of this.#eventWaiters) waiter.fail(error); + this.#eventWaiters.clear(); + // scient(SA-DAEMON-REJOIN): end closed.resolve(); }, ); @@ -162,7 +169,30 @@ export class DaemonConn { what: string, timeoutMs = 10_000, ): Promise { - return until(() => this.events.find(predicate), what, timeoutMs); + // scient(SA-DAEMON-REJOIN): begin - Resolve from the event handler when a matching event arrives, instead of polling the buffer. + const received = this.events.find(predicate); + if (received) return Promise.resolve(received); + if (this.#isClosed) return Promise.reject(new Error(`Daemon connection closed waiting for ${what}`)); + const { promise, resolve, reject } = Promise.withResolvers(); + const timer = setTimeout(() => { + this.#eventWaiters.delete(waiter); + reject(new Error(`Timed out after ${timeoutMs}ms waiting for ${what}`)); + }, timeoutMs); + const waiter = { + match: (event: DaemonEvent): boolean => { + if (!predicate(event)) return false; + clearTimeout(timer); + resolve(event); + return true; + }, + fail: (error: Error): void => { + clearTimeout(timer); + reject(new Error(`${error.message} waiting for ${what}`)); + }, + }; + this.#eventWaiters.add(waiter); + return promise; + // scient(SA-DAEMON-REJOIN): end } close(): Promise { @@ -181,6 +211,10 @@ export class DaemonConn { } if ("ev" in message) { this.events.push(message as unknown as DaemonEvent); + // scient(SA-DAEMON-REJOIN): begin - Wake the waits this event matches. + const event = this.events.at(-1)!; + for (const waiter of this.#eventWaiters) if (waiter.match(event)) this.#eventWaiters.delete(waiter); + // scient(SA-DAEMON-REJOIN): end return; } const response = message as unknown as DaemonResponse; diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index 32ea71c0de..6de2548573 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -556,6 +556,18 @@ "verification": [ "bun --cwd=packages/coding-agent test test/scient/codex-plugins.test.ts test/scient/codex-plugins-oauth.test.ts test/scient/mcp-host-command.test.ts" ] + }, + "SA-DAEMON-REJOIN": { + "status": "active", + "contract": "Oh My Pi's daemon test \"a client reconnecting within the grace adopts the same process\" reopens the unit only after the daemon broadcasts that it dropped the closed connection's lease. The harness's close() settles when the client socket closes, before the daemon handles the close, so the open could be counted beside the stale lease (leases 2), which failed Scient's main on 2026-11-22. The harness's event wait resolves when a matching event arrives instead of polling its buffer. Test-only; the daemon's connection-bound lease accounting is unchanged. This inherited flake needs an upstream fix.", + "owned": [], + "upstream": [ + "packages/coding-agent/test/daemon/unit-lifecycle.test.ts", + "packages/coding-agent/test/fixtures/daemon-proc-harness.ts" + ], + "verification": [ + "bun --cwd=packages/coding-agent test test/daemon/unit-lifecycle.test.ts test/daemon/proc-units.test.ts test/daemon/board-register.test.ts" + ] } } } From bd376f459d332171b80abea6296aa95a2ab0587f Mon Sep 17 00:00:00 2001 From: Yaacov Date: Mon, 23 Nov 2026 10:54:43 +0200 Subject: [PATCH 2/2] test(daemon): wait for the dropped lease with describe, leave the harness unchanged Codex review: a waiter implementation inside an upstream harness file is logic, not a small hook. The grace test above already waits for this exact lease drop with until() and describe(), so the rejoin test reuses it and the harness is back to Oh My Pi's. Co-Authored-By: Claude Opus 5.5 --- .../test/daemon/unit-lifecycle.test.ts | 16 +++------ .../test/fixtures/daemon-proc-harness.ts | 36 +------------------ scripts/scient/divergences.json | 7 ++-- 3 files changed, 8 insertions(+), 51 deletions(-) diff --git a/packages/coding-agent/test/daemon/unit-lifecycle.test.ts b/packages/coding-agent/test/daemon/unit-lifecycle.test.ts index 3d7e2761e1..831a17ff34 100644 --- a/packages/coding-agent/test/daemon/unit-lifecycle.test.ts +++ b/packages/coding-agent/test/daemon/unit-lifecycle.test.ts @@ -6,9 +6,6 @@ */ import { afterEach, describe, expect, it } from "bun:test"; import { DAEMON_RESUME_LEASE_GRACE_MS } from "../../src/daemon/protocol"; -// scient(SA-DAEMON-REJOIN): begin - The rejoin test waits for the daemon's lease-drop event. -import type { DaemonEvent, DaemonUnitEvent } from "../../src/daemon/protocol"; -// scient(SA-DAEMON-REJOIN): end import { type DaemonConn, pidAlive, rejection, shProc, TestDaemon, until } from "../fixtures/daemon-proc-harness"; const TEST_TIMEOUT_MS = 30_000; @@ -106,15 +103,10 @@ describe.skipIf(process.platform === "win32")("daemon unit lifecycle", () => { const second = await d.connect(); // scient(SA-DAEMON-REJOIN): begin - The client's close settles before the daemon handles it: rejoin once the daemon has dropped the lease. - // Watching first means a drop after the describe still arrives as a buffered event. - await second.request({ op: "watch" }); - if ((await second.describe("rejoin")).leases !== 0) { - await second.event( - (event: DaemonEvent): event is DaemonUnitEvent => - event.ev === "unit" && event.unit.key === "rejoin" && event.unit.leases === 0, - "the dropped lease", - ); - } + await until( + async () => ((await second.describe("rejoin")).leases === 0 ? true : undefined), + "the dropped lease", + ); // scient(SA-DAEMON-REJOIN): end const again = await second.openProc("rejoin", proc, { policy: { idleMs: 0 } }); diff --git a/packages/coding-agent/test/fixtures/daemon-proc-harness.ts b/packages/coding-agent/test/fixtures/daemon-proc-harness.ts index ab1f7a33b6..9838292db8 100644 --- a/packages/coding-agent/test/fixtures/daemon-proc-harness.ts +++ b/packages/coding-agent/test/fixtures/daemon-proc-harness.ts @@ -91,9 +91,6 @@ export class DaemonConn { readonly #pending = new Map void; reject: (error: Error) => void }>(); readonly #onResult: (result: unknown) => void; readonly #welcome = Promise.withResolvers(); - // scient(SA-DAEMON-REJOIN): begin - Event waits are woken by the event, not by polling. - readonly #eventWaiters = new Set<{ match: (event: DaemonEvent) => boolean; fail: (error: Error) => void }>(); - // scient(SA-DAEMON-REJOIN): end #welcomed = false; #isClosed = false; @@ -110,10 +107,6 @@ export class DaemonConn { if (!this.#welcomed) this.#welcome.reject(error); for (const pending of this.#pending.values()) pending.reject(error); this.#pending.clear(); - // scient(SA-DAEMON-REJOIN): begin - A closed connection fails the event waits. - for (const waiter of this.#eventWaiters) waiter.fail(error); - this.#eventWaiters.clear(); - // scient(SA-DAEMON-REJOIN): end closed.resolve(); }, ); @@ -169,30 +162,7 @@ export class DaemonConn { what: string, timeoutMs = 10_000, ): Promise { - // scient(SA-DAEMON-REJOIN): begin - Resolve from the event handler when a matching event arrives, instead of polling the buffer. - const received = this.events.find(predicate); - if (received) return Promise.resolve(received); - if (this.#isClosed) return Promise.reject(new Error(`Daemon connection closed waiting for ${what}`)); - const { promise, resolve, reject } = Promise.withResolvers(); - const timer = setTimeout(() => { - this.#eventWaiters.delete(waiter); - reject(new Error(`Timed out after ${timeoutMs}ms waiting for ${what}`)); - }, timeoutMs); - const waiter = { - match: (event: DaemonEvent): boolean => { - if (!predicate(event)) return false; - clearTimeout(timer); - resolve(event); - return true; - }, - fail: (error: Error): void => { - clearTimeout(timer); - reject(new Error(`${error.message} waiting for ${what}`)); - }, - }; - this.#eventWaiters.add(waiter); - return promise; - // scient(SA-DAEMON-REJOIN): end + return until(() => this.events.find(predicate), what, timeoutMs); } close(): Promise { @@ -211,10 +181,6 @@ export class DaemonConn { } if ("ev" in message) { this.events.push(message as unknown as DaemonEvent); - // scient(SA-DAEMON-REJOIN): begin - Wake the waits this event matches. - const event = this.events.at(-1)!; - for (const waiter of this.#eventWaiters) if (waiter.match(event)) this.#eventWaiters.delete(waiter); - // scient(SA-DAEMON-REJOIN): end return; } const response = message as unknown as DaemonResponse; diff --git a/scripts/scient/divergences.json b/scripts/scient/divergences.json index 6de2548573..4192275795 100644 --- a/scripts/scient/divergences.json +++ b/scripts/scient/divergences.json @@ -559,14 +559,13 @@ }, "SA-DAEMON-REJOIN": { "status": "active", - "contract": "Oh My Pi's daemon test \"a client reconnecting within the grace adopts the same process\" reopens the unit only after the daemon broadcasts that it dropped the closed connection's lease. The harness's close() settles when the client socket closes, before the daemon handles the close, so the open could be counted beside the stale lease (leases 2), which failed Scient's main on 2026-11-22. The harness's event wait resolves when a matching event arrives instead of polling its buffer. Test-only; the daemon's connection-bound lease accounting is unchanged. This inherited flake needs an upstream fix.", + "contract": "Oh My Pi's daemon test \"a client reconnecting within the grace adopts the same process\" reopens the unit only after describe shows the daemon dropped the closed connection's lease, as the grace test above it waits. The harness's close() settles when the client socket closes, before the daemon handles the close, so the open could be counted beside the stale lease (leases 2), which failed Scient's main on 2026-11-22. Test-only; the daemon's connection-bound lease accounting is unchanged. This inherited flake needs an upstream fix.", "owned": [], "upstream": [ - "packages/coding-agent/test/daemon/unit-lifecycle.test.ts", - "packages/coding-agent/test/fixtures/daemon-proc-harness.ts" + "packages/coding-agent/test/daemon/unit-lifecycle.test.ts" ], "verification": [ - "bun --cwd=packages/coding-agent test test/daemon/unit-lifecycle.test.ts test/daemon/proc-units.test.ts test/daemon/board-register.test.ts" + "bun --cwd=packages/coding-agent test test/daemon/unit-lifecycle.test.ts" ] } }