Review of the seeding path found five ways a seeded database could describe something the copy cannot back up: - Nothing stopped `dev:seed` from running against a live server. SQLite locking lets the seed take the write lock between the server's own writes and commit cleanly; what it cannot do is tell that server its projections and event log were replaced underneath it. Refuse instead, via a pid probe over both state directories. - A thread copied mid-turn kept `state = 'running'`. The session override did not cover it — the turn is read independently, and an unsettled one leaves the thread spinning and its timeline unfoldable forever. Settle those to "interrupted", matching what the server does when a session leaves "running". Streaming messages are finished for the same reason. - Attachment metadata was copied without the files behind it, so a seeded thread rendered an image whose request 404s. Report the referenced ids and copy the matching files alongside the rows. - Orphaned `provider_session_runtime` rows survived the seed naming threads it had just deleted, which the session reaper then tried to stop against a provider instance the worktree may not define. - `--threads` had no upper bound, so a large enough value died with "too many SQL variables" after the target was already emptied. Also corrects the projector-cursor comment: `orchestration_events.sequence` is AUTOINCREMENT, so emptying the log does not reset its high-water mark and sequence does not restart at 1. Zero remains the right cursor, for the reason now stated. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
96 lines
3.5 KiB
TypeScript
96 lines
3.5 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)),
|
|
);
|
|
|
|
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)),
|
|
);
|
|
});
|