Fix liveness probe, --json output, and remaining unsettled state
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]>
This commit is contained in:
1 parent
373e7919d1
commit
8a76bb83cc
8 files changed
+167
-26
No files matched your search
+23
-2
@@ -28,7 +28,13 @@ const MAX_THREAD_LIMIT = 32_766;
|
||||
class DevSeedTargetError extends Schema.TaggedErrorClass<DevSeedTargetError>()(
|
||||
"DevSeedTargetError",
|
||||
{
|
||||
reason: Schema.Literals(["shared-home", "missing-target", "not-a-worktree", "server-running"]),
|
||||
reason: Schema.Literals([
|
||||
"shared-home",
|
||||
"missing-target",
|
||||
"not-a-worktree",
|
||||
"server-running",
|
||||
"same-source",
|
||||
]),
|
||||
detail: Schema.String,
|
||||
},
|
||||
) {
|
||||
@@ -56,6 +62,13 @@ class DevSeedTargetError extends Schema.TaggedErrorClass<DevSeedTargetError>()(
|
||||
"which leaves the running server's in-memory read model describing rows that no longer exist.",
|
||||
"Stop it, run the seed, then start it again.",
|
||||
].join("\n");
|
||||
case "same-source":
|
||||
return [
|
||||
`Refusing to seed ${this.detail} from itself.`,
|
||||
"The copy would restore what the delete removed, but the event log and the",
|
||||
"projector cursors are cleared either way — so the directory loses its history",
|
||||
"and keeps only whatever the thread and activity caps selected.",
|
||||
].join("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -167,13 +180,21 @@ const devSeedCli = Command.make("dev-seed", {
|
||||
|
||||
// The whole point is to keep dev data off the real home; overwriting it
|
||||
// here would be the exact accident this command exists to avoid.
|
||||
const [canonicalTarget, canonicalShared] = yield* Effect.all([
|
||||
const [canonicalTarget, canonicalSource, canonicalShared] = yield* Effect.all([
|
||||
fileSystem.realPath(targetBaseDir).pipe(Effect.orElseSucceed(() => targetBaseDir)),
|
||||
fileSystem.realPath(sourceBaseDir).pipe(Effect.orElseSucceed(() => sourceBaseDir)),
|
||||
fileSystem.realPath(sharedHome).pipe(Effect.orElseSucceed(() => sharedHome)),
|
||||
]);
|
||||
if (canonicalTarget === canonicalShared) {
|
||||
return yield* new DevSeedTargetError({ reason: "shared-home", detail: targetBaseDir });
|
||||
}
|
||||
// Seeding a directory from itself is not the data loss it looks like —
|
||||
// the source reads a pre-transaction snapshot, so the copy puts the rows
|
||||
// back. What does not come back is the event log and the cursors, and
|
||||
// anything outside the caps. Nobody means to ask for that.
|
||||
if (canonicalTarget === canonicalSource) {
|
||||
return yield* new DevSeedTargetError({ reason: "same-source", detail: targetBaseDir });
|
||||
}
|
||||
|
||||
const targetDbPath = stateDbPath(path, targetBaseDir);
|
||||
if (!(yield* fileSystem.exists(targetDbPath))) {
|
||||
|
||||
@@ -101,8 +101,10 @@ function makeSource(
|
||||
}
|
||||
// The newest thread is left mid-turn, as a copy taken while an agent is
|
||||
// working would be: a running turn with no completion, and a message still
|
||||
// streaming into it.
|
||||
// streaming into it. The one before it is left "pending" — a user message
|
||||
// accepted but whose provider turn never started.
|
||||
const midFlight = index === threadCount - 1;
|
||||
const queued = index === threadCount - 2;
|
||||
database
|
||||
.prepare(
|
||||
`INSERT INTO projection_turns (thread_id, turn_id, state, requested_at, completed_at, checkpoint_files_json)
|
||||
@@ -111,9 +113,9 @@ function makeSource(
|
||||
.run(
|
||||
threadId,
|
||||
`turn-${threadId}`,
|
||||
midFlight ? "running" : "completed",
|
||||
midFlight ? "running" : queued ? "pending" : "completed",
|
||||
at,
|
||||
midFlight ? null : at,
|
||||
midFlight || queued ? null : at,
|
||||
);
|
||||
database
|
||||
.prepare(`INSERT INTO projection_thread_sessions VALUES (?,'running','claude',?,'boom',?)`)
|
||||
@@ -297,10 +299,11 @@ describe("seedDevDatabase", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// A copied running turn has no agent left to finish it. The session override
|
||||
// alone does not cover this: the turn is read independently, and an unsettled
|
||||
// one keeps the thread spinning and its timeline unfoldable forever.
|
||||
it("settles turns copied mid-flight", () => {
|
||||
// A copied turn has no agent left to finish it, whether it was already
|
||||
// running or still queued behind a "pending" row. The session override alone
|
||||
// does not cover either: the turn is read independently, and an unsettled one
|
||||
// keeps the thread spinning and its timeline unfoldable forever.
|
||||
it("settles turns copied before they finished", () => {
|
||||
withDatabases(({ source, target }) => {
|
||||
seedDevDatabase({
|
||||
sourceDbPath: source,
|
||||
@@ -314,9 +317,11 @@ describe("seedDevDatabase", () => {
|
||||
target,
|
||||
"SELECT state, completed_at FROM projection_turns",
|
||||
);
|
||||
// The fixture puts one running turn and one pending turn in this range.
|
||||
assert.isAbove(turns.length, 0);
|
||||
for (const turn of turns) {
|
||||
assert.notEqual(turn.state, "running");
|
||||
assert.notEqual(turn.state, "pending");
|
||||
assert.isNotNull(turn.completed_at);
|
||||
}
|
||||
});
|
||||
|
||||
+48
-11
@@ -216,6 +216,16 @@ function copyRows(input: {
|
||||
return { copied, skipped };
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn states that describe work an agent still owes: "running" for a turn in
|
||||
* flight, "pending" for one whose user message was accepted but whose provider
|
||||
* turn had not started yet (`insertPendingProjectionTurn` in
|
||||
* apps/server/src/persistence/Layers/ProjectionTurns.ts). Neither can make
|
||||
* progress in a copy, and only the thread's own next message would clear a
|
||||
* pending one.
|
||||
*/
|
||||
const UNSETTLED_TURN_STATES = ["running", "pending"] as const;
|
||||
|
||||
/**
|
||||
* Settles turns copied mid-flight, matching what the server does to a turn
|
||||
* whose session leaves "running": `settledTurnStateForSessionStatus` in
|
||||
@@ -223,26 +233,28 @@ function copyRows(input: {
|
||||
* status the sessions are copied with to "interrupted".
|
||||
*
|
||||
* An UPDATE rather than a copy override, because only the rows that were
|
||||
* actually running should change; the rest keep their real recorded state.
|
||||
* actually unsettled should change; the rest keep their real recorded state.
|
||||
*/
|
||||
function settleRunningTurns(
|
||||
function settleUnfinishedTurns(
|
||||
target: NodeSqlite.DatabaseSync,
|
||||
threadIds: ReadonlyArray<string>,
|
||||
): void {
|
||||
if (threadIds.length === 0 || !hasColumn(target, "projection_turns", "state")) {
|
||||
return;
|
||||
}
|
||||
// completed_at is what marks a turn settled alongside its state; a running
|
||||
// turn has none, and leaving it null keeps the thread unsettled regardless.
|
||||
// completed_at is what marks a turn settled alongside its state; an
|
||||
// unfinished turn has none, and leaving it null keeps the thread unsettled
|
||||
// regardless of the state written here.
|
||||
const setCompletedAt = hasColumn(target, "projection_turns", "completed_at")
|
||||
? `, completed_at = COALESCE(completed_at, requested_at)`
|
||||
: "";
|
||||
target
|
||||
.prepare(
|
||||
`UPDATE projection_turns SET state = 'interrupted'${setCompletedAt}
|
||||
WHERE state = 'running' AND thread_id IN (${placeholders(threadIds.length)})`,
|
||||
WHERE state IN (${placeholders(UNSETTLED_TURN_STATES.length)})
|
||||
AND thread_id IN (${placeholders(threadIds.length)})`,
|
||||
)
|
||||
.run(...threadIds);
|
||||
.run(...UNSETTLED_TURN_STATES, ...threadIds);
|
||||
}
|
||||
|
||||
export function seedDevDatabase(options: DevSeedOptions): DevSeedSummary {
|
||||
@@ -267,6 +279,22 @@ export function seedDevDatabase(options: DevSeedOptions): DevSeedSummary {
|
||||
);
|
||||
}
|
||||
|
||||
// The source is normally the *live* installed app's database — only the
|
||||
// target is guarded against a running server, because reading is harmless.
|
||||
// Reading it across a dozen separate implicit transactions is not: the app
|
||||
// can delete a project between the query that picks it up and the copy that
|
||||
// reads its row, leaving a copied thread whose project no longer exists (no
|
||||
// foreign keys are declared, so nothing catches it). One deferred read
|
||||
// transaction pins every query below to a single snapshot.
|
||||
let sourceTransactionOpen = false;
|
||||
try {
|
||||
source.exec("BEGIN");
|
||||
sourceTransactionOpen = true;
|
||||
} catch {
|
||||
// A concurrent writer can leave the connection unable to start one. The
|
||||
// copy is still worth doing — it just loses snapshot isolation.
|
||||
}
|
||||
|
||||
try {
|
||||
// Threads the user actually touched most recently. Mirrors the sidebar's own
|
||||
// ordering (packages/client-runtime/src/state/threadSort.ts). Both recency
|
||||
@@ -376,11 +404,11 @@ export function seedDevDatabase(options: DevSeedOptions): DevSeedSummary {
|
||||
}),
|
||||
);
|
||||
// A thread copied mid-turn has no agent to finish it. The session status is
|
||||
// forced to "stopped" below, but the turn is read independently: a
|
||||
// `state = 'running'` turn keeps the thread unsettled and unfoldable
|
||||
// forever (deriveUnsettledTurnId in MessagesTimeline.logic.ts). Interrupted
|
||||
// is what the server itself settles an abandoned turn to.
|
||||
settleRunningTurns(target, threadIds);
|
||||
// forced to "stopped" below, but the turn is read independently: an
|
||||
// unfinished turn keeps the thread unsettled and unfoldable forever
|
||||
// (deriveUnsettledTurnId in MessagesTimeline.logic.ts). Interrupted is what
|
||||
// the server itself settles an abandoned turn to.
|
||||
settleUnfinishedTurns(target, threadIds);
|
||||
const messages = record(
|
||||
copyRows({
|
||||
source,
|
||||
@@ -475,6 +503,15 @@ export function seedDevDatabase(options: DevSeedOptions): DevSeedSummary {
|
||||
? cause
|
||||
: new DevSeedError(`could not seed the dev database: ${String(cause)}`);
|
||||
} finally {
|
||||
if (sourceTransactionOpen) {
|
||||
try {
|
||||
// Read-only, so there is nothing to commit; end it before closing so
|
||||
// the snapshot is released explicitly rather than by teardown.
|
||||
source.exec("ROLLBACK");
|
||||
} catch {
|
||||
// Already ended, which is equally fine for a read-only transaction.
|
||||
}
|
||||
}
|
||||
source.close();
|
||||
target.close();
|
||||
}
|
||||
|
||||
@@ -86,6 +86,20 @@ describe("findRunningServerPid", () => {
|
||||
}).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();
|
||||
|
||||
@@ -16,7 +16,8 @@ import * as Schema from "effect/Schema";
|
||||
const STATE_DIRS = ["dev", "userdata"] as const;
|
||||
|
||||
const RuntimeStateProbe = Schema.Struct({
|
||||
pid: Schema.Int,
|
||||
/** Positive, for the reason `isPidLive` explains. */
|
||||
pid: Schema.Int.check(Schema.isGreaterThan(0)),
|
||||
});
|
||||
|
||||
const decodeRuntimeStateProbe = Schema.decodeUnknownEffect(
|
||||
@@ -34,12 +35,23 @@ const decodeRuntimeStateProbe = Schema.decodeUnknownEffect(
|
||||
* case is being told to stop a server that is already stopped.
|
||||
*/
|
||||
const isPidLive = (pid: number): boolean => {
|
||||
// `process.kill` reads non-positive pids as signal-group targets rather than
|
||||
// process lookups — 0 signals the caller's own group, -1 every permitted
|
||||
// process — so neither throws and both would report "live" unconditionally.
|
||||
// The schema rejects those, and this repeats the check so the invariant
|
||||
// travels with the code that depends on it.
|
||||
if (!Number.isSafeInteger(pid) || pid <= 0) {
|
||||
return false;
|
||||
}
|
||||
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";
|
||||
// EPERM means the process exists but belongs to another user. An
|
||||
// out-of-range pid throws a TypeError instead, which has no errno.
|
||||
return typeof cause === "object" && cause !== null && "code" in cause
|
||||
? cause.code === "EPERM"
|
||||
: false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user