fix: address review findings on the reconnect flow

Three defects found by Macroscope and Cursor Bugbot:

- The restart window was armed only at click time, but an npm install can
  consume most of the 10-minute request window, so a slow-but-successful
  update restarted after the window had lapsed and was presented as an
  outage. Re-arm from the acknowledgement and from the interrupt path,
  matching what keepPendingForRestart already does for the button expiry.

- Effect.catch on the detached restart fiber only sees typed failures, so
  an unexpected defect skipped the inFlight reset and left a live process
  refusing every further update until restarted by other means. Release
  the guard from Effect.onExit, outside the typed handler. Success still
  deliberately keeps it set: systemd is about to stop the process and a
  second update would race the handoff.

- "A server update is already in progress" is a typed failure, so it
  cleared a restart overlay that the earlier still-pending update needed,
  restoring the harsh reconnect UI mid-restart. That reason now lives in
  contracts as SERVER_SELF_UPDATE_ALREADY_RUNNING_REASON with an
  isAlreadyRunning helper, so the client can distinguish it structurally
  instead of matching prose.

Each fix has a regression test; the defect-path test was confirmed to
fail without the onExit change.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
Theo BrowneandClaude Opus 5 committed 2026-07-24 16:47:51 -07:00
1 parent b462ae5f2f
commit 7aecc5d47a
5 files changed
+181 -9

No files matched your search

+33
View File
@@ -62,6 +62,8 @@ const makeRecordingRunnerLayer = (
readonly stdoutFor?:
| ((command: string, args: ReadonlyArray<string>) => string | undefined)
| undefined;
/** Raises a defect (not a typed failure) to exercise unexpected-error paths. */
readonly dieWhen?: ((command: string, args: ReadonlyArray<string>) => boolean) | undefined;
},
) =>
Layer.succeed(
@@ -70,6 +72,9 @@ const makeRecordingRunnerLayer = (
run: (input) =>
Effect.sync(() => {
commands.push({ command: input.command, args: input.args });
if (options?.dieWhen?.(input.command, input.args) === true) {
throw new Error(`${input.command} died unexpectedly`);
}
const failed = options?.failWhen?.(input.command, input.args) === true;
const versionFromPath =
input.command === NODE_PATH && input.args[1] === "--version"
@@ -281,6 +286,7 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => {
readonly failWhen?: (command: string, args: ReadonlyArray<string>) => boolean;
readonly stdoutFor?: (command: string, args: ReadonlyArray<string>) => string | undefined;
readonly failSpawn?: boolean;
readonly dieWhen?: (command: string, args: ReadonlyArray<string>) => boolean;
}) {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
@@ -353,6 +359,7 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => {
makeRecordingRunnerLayer(commands, {
failWhen: options?.failWhen,
stdoutFor: options?.stdoutFor,
dieWhen: options?.dieWhen,
}),
configLayer,
),
@@ -588,6 +595,32 @@ it.layer(NodeServices.layer)("ServerSelfUpdate.update", (it) => {
}).pipe(Effect.provide(TestClock.layer())),
);
it.effect("permits a retry when the deferred restart dies unexpectedly", () =>
Effect.gen(function* () {
let dieOnRestart = true;
const context = yield* makeContext({
bootService: true,
dieWhen: (command, args) => {
if (command !== "systemctl" || args[1] !== "restart" || !dieOnRestart) {
return false;
}
dieOnRestart = false;
return true;
},
});
const first = yield* context.service.update({ targetVersion: "0.0.29" });
assert.deepEqual(first, { targetVersion: "0.0.29", method: "boot-service" });
yield* TestClock.adjust(Duration.seconds(10));
// A defect bypasses the typed rollback handler, so the in-flight guard
// has to be released outside it — otherwise this live process would
// reject every further update until restarted by other means.
const retry = yield* context.service.update({ targetVersion: "0.0.30" });
assert.deepEqual(retry, { targetVersion: "0.0.30", method: "boot-service" });
}).pipe(Effect.provide(TestClock.layer())),
);
it.effect("restores the previous systemd unit when daemon-reload fails", () =>
Effect.gen(function* () {
const context = yield* makeContext({
+18 -1
View File
@@ -3,6 +3,7 @@
// detached fire-and-forget child that outlives this process, while Effect's
// ChildProcessSpawner ties every child to a scope that kills it.
import {
SERVER_SELF_UPDATE_ALREADY_RUNNING_REASON,
ServerSelfUpdateError,
type ServerSelfUpdateCapability,
type ServerSelfUpdateInput,
@@ -18,6 +19,7 @@ import * as NodeChildProcess from "node:child_process";
import * as Context from "effect/Context";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
@@ -246,7 +248,7 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* (option
const alreadyRunning = yield* Ref.getAndSet(inFlight, true);
if (alreadyRunning) {
return yield* failWith("A server update is already in progress.");
return yield* failWith(SERVER_SELF_UPDATE_ALREADY_RUNNING_REASON);
}
return yield* Effect.gen(function* () {
@@ -387,9 +389,24 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* (option
"Server self-update could not restore the previous systemd unit after a rejected restart.",
).pipe(Effect.annotateLogs({ targetVersion, error: String(rollbackError) })),
),
// The process survived a rejected restart, so it must stay
// updatable — leaving the guard set would reject every retry
// until the server was restarted by other means.
Effect.ensuring(Ref.set(inFlight, false)),
),
),
// The typed catch above cannot see defects or interrupts, which
// would otherwise strand the guard on a live process. A success
// deliberately leaves it set: systemd is about to stop this
// process and a second update would race the handoff.
Effect.onExit((exit) =>
Exit.isSuccess(exit)
? Effect.void
: Effect.logError("Server self-update restart fiber ended unexpectedly.").pipe(
Effect.annotateLogs({ targetVersion, exit: String(exit) }),
Effect.ensuring(Ref.set(inFlight, false)),
),
),
),
);
} else {