Follow-up review of the pairing path and a second pass over seeding: - `isPersistedServerRuntimeStateLive` reported non-positive pids as live. `process.kill` reads 0 and -1 as signal-group targets rather than process lookups, so neither throws and both were caught by the "it didn't throw, so it's running" branch. A truncated or hand-edited file recording either would be believed forever — the dev candidate wins the devUrl preference, so it also outranks a genuinely live server elsewhere and mints tokens into a database nothing reads. Rejected in the schema and re-checked at the call site; the errno cast is now a guard, since an out-of-range pid throws a TypeError with no `code`. - `pairing url --json` emitted warnings on stdout ahead of the payload. The probe reads both state directories, so a corrupt file in the one *not* in use broke JSON for any consumer piping it; the quiet-logs layer was only applied around the issuing step that runs after. Verified with a crafted fixture: stdout is now valid JSON. - Turns copied in the `pending` state were left unsettled. Same defect as the `running` case fixed previously — a user message accepted before its provider turn started, which only that thread's next message would ever clear. - The source was read across a dozen implicit transactions while the installed app may be writing to it, so a project could vanish between the query that selected it and the copy that read its row. One deferred read transaction pins every query to a single snapshot. - `--from` and `--to` resolving to the same directory is now refused. Not for the data loss it appears to be (the source reads a pre-transaction snapshot, so the rows come back — verified in both journal modes), but because the event log, the cursors, and everything outside the caps do not. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
110 lines
4.1 KiB
TypeScript
110 lines
4.1 KiB
TypeScript
import * as NodeServices from "@effect/platform-node/NodeServices";
|
|
import { assert, describe, it } from "@effect/vitest";
|
|
import * as Effect from "effect/Effect";
|
|
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";
|
|
|
|
/** A pid that cannot belong to a live process (above the usual pid_max). */
|
|
const DEAD_PID = 0x7ffffffe;
|
|
|
|
const writeRuntimeState = Effect.fn(function* (input: {
|
|
readonly baseDir: string;
|
|
readonly stateDir: "dev" | "userdata";
|
|
readonly contents: string;
|
|
}) {
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const directory = path.join(input.baseDir, input.stateDir);
|
|
yield* fileSystem.makeDirectory(directory, { recursive: true });
|
|
yield* fileSystem.writeFileString(path.join(directory, "server-runtime.json"), input.contents);
|
|
});
|
|
|
|
const runtimeStateJson = (pid: number) =>
|
|
JSON.stringify({
|
|
version: 1,
|
|
pid,
|
|
port: 16_602,
|
|
origin: "http://127.0.0.1:16602",
|
|
startedAt: "2026-07-20T00:00:00.000Z",
|
|
});
|
|
|
|
const makeBaseDir = Effect.fnUntraced(function* () {
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
return yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-runtime-probe-test-" });
|
|
});
|
|
|
|
describe("findRunningServerPid", () => {
|
|
it.effect("reports the pid of a live server in the dev state directory", () =>
|
|
Effect.gen(function* () {
|
|
const baseDir = yield* makeBaseDir();
|
|
yield* writeRuntimeState({
|
|
baseDir,
|
|
stateDir: "dev",
|
|
contents: runtimeStateJson(process.pid),
|
|
});
|
|
|
|
assert.equal(Option.getOrThrow(yield* findRunningServerPid(baseDir)), process.pid);
|
|
}).pipe(Effect.provide(NodeServices.layer)),
|
|
);
|
|
|
|
// An explicit --home-dir puts runtime state in userdata instead, and the
|
|
// caller of a seed has no reason to know which of the two was used.
|
|
it.effect("also finds a live server in the userdata state directory", () =>
|
|
Effect.gen(function* () {
|
|
const baseDir = yield* makeBaseDir();
|
|
yield* writeRuntimeState({
|
|
baseDir,
|
|
stateDir: "userdata",
|
|
contents: runtimeStateJson(process.pid),
|
|
});
|
|
|
|
assert.equal(Option.getOrThrow(yield* findRunningServerPid(baseDir)), process.pid);
|
|
}).pipe(Effect.provide(NodeServices.layer)),
|
|
);
|
|
|
|
// A server killed with SIGKILL never clears its state file. Treating that as
|
|
// "running" would block seeding for good, with nothing to clear it.
|
|
it.effect("ignores a state file whose process is gone", () =>
|
|
Effect.gen(function* () {
|
|
const baseDir = yield* makeBaseDir();
|
|
yield* writeRuntimeState({ baseDir, stateDir: "dev", contents: runtimeStateJson(DEAD_PID) });
|
|
|
|
assert.isTrue(Option.isNone(yield* findRunningServerPid(baseDir)));
|
|
}).pipe(Effect.provide(NodeServices.layer)),
|
|
);
|
|
|
|
// A half-written file says nothing about liveness either way.
|
|
it.effect("ignores a state file it cannot parse", () =>
|
|
Effect.gen(function* () {
|
|
const baseDir = yield* makeBaseDir();
|
|
yield* writeRuntimeState({ baseDir, stateDir: "dev", contents: "{not json" });
|
|
|
|
assert.isTrue(Option.isNone(yield* findRunningServerPid(baseDir)));
|
|
}).pipe(Effect.provide(NodeServices.layer)),
|
|
);
|
|
|
|
// `process.kill` reads these as signal-group targets, not process lookups, so
|
|
// neither throws — a naive probe calls both "live" and blocks seeding forever
|
|
// on a file no server ever wrote.
|
|
for (const pid of [0, -1]) {
|
|
it.effect(`ignores a state file recording pid ${String(pid)}`, () =>
|
|
Effect.gen(function* () {
|
|
const baseDir = yield* makeBaseDir();
|
|
yield* writeRuntimeState({ baseDir, stateDir: "dev", contents: runtimeStateJson(pid) });
|
|
|
|
assert.isTrue(Option.isNone(yield* findRunningServerPid(baseDir)));
|
|
}).pipe(Effect.provide(NodeServices.layer)),
|
|
);
|
|
}
|
|
|
|
it.effect("reports nothing when no state files exist", () =>
|
|
Effect.gen(function* () {
|
|
const baseDir = yield* makeBaseDir();
|
|
assert.isTrue(Option.isNone(yield* findRunningServerPid(baseDir)));
|
|
}).pipe(Effect.provide(NodeServices.layer)),
|
|
);
|
|
});
|