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
@@ -210,7 +210,16 @@ const pairingUrlCommand = Command.make("url", {
|
||||
? flags
|
||||
: { ...flags, baseDir: Option.some(worktreeBaseDir) };
|
||||
const config = yield* resolveCliAuthConfig(resolvedFlags, logLevel);
|
||||
const live = yield* findLiveServerRuntimeState(config);
|
||||
// Quieted here rather than only around the issuing layer below: probing
|
||||
// reads both state directories, and a corrupt file in the one *not* in
|
||||
// use logs a Warn to stdout — ahead of the payload, breaking `--json`
|
||||
// for any consumer piping it. The warning is about a file the command
|
||||
// then ignores, so under --json it is noise either way.
|
||||
const live = yield* findLiveServerRuntimeState(config).pipe(
|
||||
Effect.provide(
|
||||
Layer.succeed(References.MinimumLogLevel, flags.json ? "Error" : config.logLevel),
|
||||
),
|
||||
);
|
||||
|
||||
if (Option.isNone(live)) {
|
||||
return yield* new NoRunningServerError({ baseDir: config.baseDir });
|
||||
|
||||
@@ -70,6 +70,30 @@ describe("serverRuntimeState", () => {
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
// `process.kill` reads non-positive pids as signal-group targets rather
|
||||
// than process lookups — 0 is the caller's own group, -1 every permitted
|
||||
// process — so neither throws, and a probe that only catches would call
|
||||
// both "live" no matter what wrote the file.
|
||||
for (const pid of [0, -1]) {
|
||||
it.effect(`reports pid ${String(pid)} as dead rather than signalling a group`, () =>
|
||||
Effect.gen(function* () {
|
||||
assert.isFalse(
|
||||
yield* ServerRuntimeState.isPersistedServerRuntimeStateLive(stateForPid(pid)),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
// Out of range for a pid: process.kill throws a TypeError with no errno,
|
||||
// which the EPERM check must not assume the shape of.
|
||||
it.effect("reports an out-of-range pid as dead", () =>
|
||||
Effect.gen(function* () {
|
||||
assert.isFalse(
|
||||
yield* ServerRuntimeState.isPersistedServerRuntimeStateLive(stateForPid(2 ** 40)),
|
||||
);
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("treats a missing runtime state file as absent", () =>
|
||||
|
||||
@@ -10,7 +10,11 @@ import { formatHostForUrl, isWildcardHost } from "./startupAccess.ts";
|
||||
|
||||
export const PersistedServerRuntimeState = Schema.Struct({
|
||||
version: Schema.Literal(1),
|
||||
pid: Schema.Int,
|
||||
/**
|
||||
* Positive: `process.kill` reads 0 and negatives as signal-group targets, so
|
||||
* a liveness probe on one reports "live" for a process that never existed.
|
||||
*/
|
||||
pid: Schema.Int.check(Schema.isGreaterThan(0)),
|
||||
host: Schema.optional(Schema.String),
|
||||
port: Schema.Int,
|
||||
origin: Schema.String,
|
||||
@@ -107,6 +111,13 @@ export const clearPersistedServerRuntimeState = (path: string) =>
|
||||
);
|
||||
});
|
||||
|
||||
/**
|
||||
* `process.kill` rejects an out-of-range pid with a TypeError rather than an
|
||||
* errno, so the catch below cannot assume either shape.
|
||||
*/
|
||||
const isErrnoException = (cause: unknown): cause is NodeJS.ErrnoException =>
|
||||
typeof cause === "object" && cause !== null && "code" in cause;
|
||||
|
||||
/**
|
||||
* Whether the process that wrote a runtime-state file is still alive.
|
||||
*
|
||||
@@ -123,12 +134,20 @@ export const isPersistedServerRuntimeStateLive = (
|
||||
state: PersistedServerRuntimeState,
|
||||
): Effect.Effect<boolean> =>
|
||||
Effect.sync(() => {
|
||||
// `process.kill` reads non-positive pids as signal-group targets rather
|
||||
// than process lookups: 0 signals the caller's own group and -1 every
|
||||
// permitted process, so neither throws and both would report "live"
|
||||
// unconditionally. A server never records one; a corrupt or truncated file
|
||||
// can, and that file would then be believed forever.
|
||||
if (!Number.isSafeInteger(state.pid) || state.pid <= 0) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
process.kill(state.pid, 0);
|
||||
return true;
|
||||
} catch (cause) {
|
||||
// EPERM means the process exists but belongs to another user.
|
||||
return (cause as NodeJS.ErrnoException).code === "EPERM";
|
||||
return isErrnoException(cause) && cause.code === "EPERM";
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user