From 28a69713edf29c821aa9a8cfc73f77b0c415d153 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Sun, 26 Jul 2026 01:11:39 -0700 Subject: [PATCH] Pin the two liveness probes to identical behavior MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The sync probe added for the pre-commit re-check duplicates the Effect one's rules by hand — positive pid, live process, unparseable means no. Two implementations of one rule drift apart silently, and this branch has already produced several bugs of exactly that shape. Asserts every case against both probes rather than trusting them to agree: live in either state dir, dead pid, pid 0 and -1, malformed JSON, and JSON with no pid. Confirmed the parity test fails when the sync probe's positive-pid guard is removed. Co-Authored-By: Claude Opus 5 (1M context) --- scripts/lib/server-runtime-probe.test.ts | 48 +++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/scripts/lib/server-runtime-probe.test.ts b/scripts/lib/server-runtime-probe.test.ts index 49be5108bf..c501878fa7 100644 --- a/scripts/lib/server-runtime-probe.test.ts +++ b/scripts/lib/server-runtime-probe.test.ts @@ -5,7 +5,7 @@ import * as FileSystem from "effect/FileSystem"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; -import { findRunningServerPid } from "./server-runtime-probe.ts"; +import { findRunningServerPid, findRunningServerPidSync } from "./server-runtime-probe.ts"; /** A pid that cannot belong to a live process (above the usual pid_max). */ const DEAD_PID = 0x7ffffffe; @@ -107,3 +107,49 @@ describe("findRunningServerPid", () => { }).pipe(Effect.provide(NodeServices.layer)), ); }); + +/** + * The sync probe exists only because `seedDevDatabase` holds an open SQLite + * transaction and cannot await an Effect. Two implementations of one rule drift + * apart silently, so every case is asserted against both rather than trusting + * them to agree. + */ +describe("findRunningServerPidSync agrees with the Effect probe", () => { + const cases = [ + { name: "a live server in dev", stateDir: "dev", contents: runtimeStateJson(process.pid) }, + { + name: "a live server in userdata", + stateDir: "userdata", + contents: runtimeStateJson(process.pid), + }, + { name: "a dead pid", stateDir: "dev", contents: runtimeStateJson(DEAD_PID) }, + { name: "pid 0", stateDir: "dev", contents: runtimeStateJson(0) }, + { name: "pid -1", stateDir: "dev", contents: runtimeStateJson(-1) }, + { name: "unparseable json", stateDir: "dev", contents: "{not json" }, + { name: "json without a pid", stateDir: "dev", contents: JSON.stringify({ version: 1 }) }, + ] as const; + + for (const testCase of cases) { + it.effect(testCase.name, () => + Effect.gen(function* () { + const baseDir = yield* makeBaseDir(); + yield* writeRuntimeState({ + baseDir, + stateDir: testCase.stateDir, + contents: testCase.contents, + }); + + const viaEffect = Option.getOrUndefined(yield* findRunningServerPid(baseDir)); + assert.equal(findRunningServerPidSync(baseDir), viaEffect); + }).pipe(Effect.provide(NodeServices.layer)), + ); + } + + it.effect("both report nothing when no state files exist", () => + Effect.gen(function* () { + const baseDir = yield* makeBaseDir(); + assert.isTrue(Option.isNone(yield* findRunningServerPid(baseDir))); + assert.isUndefined(findRunningServerPidSync(baseDir)); + }).pipe(Effect.provide(NodeServices.layer)), + ); +});