Pin the two liveness probes to identical behavior

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) <[email protected]>
This commit is contained in:
Theo BrowneandClaude Opus 5 committed 2026-07-26 20:38:49 -07:00
1 parent bc12b990a4
commit 28a69713ed
1 file changed
+47 -1
+47 -1
View File
@@ -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)),
);
});