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]>
73 lines
2.7 KiB
TypeScript
73 lines
2.7 KiB
TypeScript
/**
|
|
* Whether a T3 Code server is currently running against a data directory.
|
|
*
|
|
* A maintained copy of the parts of `apps/server/src/serverRuntimeState.ts` that
|
|
* host scripts need: that package exposes no importable surface to `scripts/`,
|
|
* the same reason `./projection-tables.ts` exists. Only the fields read here are
|
|
* modelled, so a server that adds a field stays readable.
|
|
*/
|
|
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 * as Schema from "effect/Schema";
|
|
|
|
/** The two state directories `deriveServerPaths` can select. */
|
|
const STATE_DIRS = ["dev", "userdata"] as const;
|
|
|
|
const RuntimeStateProbe = Schema.Struct({
|
|
pid: Schema.Int,
|
|
});
|
|
|
|
const decodeRuntimeStateProbe = Schema.decodeUnknownEffect(
|
|
Schema.fromJsonString(RuntimeStateProbe),
|
|
);
|
|
|
|
/**
|
|
* The runtime-state file is removed by a release finalizer, so a server killed
|
|
* with SIGKILL — or one that crashed — leaves it behind. Signal 0 runs the
|
|
* permission and existence checks without delivering a signal, which is what
|
|
* distinguishes "still running" from "left this here on the way out".
|
|
*
|
|
* PIDs are reused eventually, so a false positive is possible in principle. For
|
|
* a guard on a destructive local-dev command that errs the safe way: the worst
|
|
* case is being told to stop a server that is already stopped.
|
|
*/
|
|
const isPidLive = (pid: number): boolean => {
|
|
try {
|
|
process.kill(pid, 0);
|
|
return true;
|
|
} catch (cause) {
|
|
// EPERM means the process exists but belongs to another user.
|
|
return (cause as NodeJS.ErrnoException).code === "EPERM";
|
|
}
|
|
};
|
|
|
|
/**
|
|
* The pid of a live server under `baseDir`, if any.
|
|
*
|
|
* Both state directories are checked because which one a given server chose
|
|
* depends on flags the caller of this should not have to reason about — the
|
|
* same reason `auth pairing url` checks both.
|
|
*/
|
|
export const findRunningServerPid = Effect.fn("findRunningServerPid")(function* (baseDir: string) {
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
|
|
for (const stateDir of STATE_DIRS) {
|
|
const statePath = path.join(baseDir, stateDir, "server-runtime.json");
|
|
const raw = yield* fileSystem.readFileString(statePath).pipe(Effect.option);
|
|
if (Option.isNone(raw)) {
|
|
continue;
|
|
}
|
|
// A malformed or half-written file says nothing about liveness; treating it
|
|
// as "a server is up" would block seeding with no way to clear it.
|
|
const state = yield* decodeRuntimeStateProbe(raw.value).pipe(Effect.option);
|
|
if (Option.isSome(state) && isPidLive(state.value.pid)) {
|
|
return Option.some(state.value.pid);
|
|
}
|
|
}
|
|
|
|
return Option.none<number>();
|
|
});
|