Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

test(server): cover the double-fault path of the hot upgrade channel (#425)

Myriad-Dreamin36a3fe3c942e
diff --git a/changelog/unreleased/2026-08-23-double-fault-upgrade-channel-is-covered.md b/changelog/unreleased/2026-08-23-double-fault-upgrade-channel-is-covered.md
new file mode 100644
index 00000000..77c92fa5
--- /dev/null
+++ b/changelog/unreleased/2026-08-23-double-fault-upgrade-channel-is-covered.md
@@ -0,0 +1,24 @@
+# The double-fault path is covered by tests
+
+- **Date:** 2026-08-23
+- **Type:** process
+- **Scope:** `server`
+- **PR:** [#425](https://github.com/Prism-Shadow/penguin-harness/pull/425)
+
+[中文版](2026-08-23-double-fault-upgrade-channel-is-covered.zh.md)
+
+Boot-failure recovery [answers a push that cannot boot](2026-08-20-hot-update-failure-modes.md)
+by re-booting the version that was running. What happens when that re-boot fails too was
+stated at the code — warn, keep `/api/hmr` reachable for a follow-up push, and let a restart
+restore the committed version — and asserted nowhere: the recovery tests all stopped at the
+first fault.
+
+Tests now drive the second one, using a platform that boots once and refuses every boot after
+that.
+
+## Details
+
+- A failed push whose recovery also fails answers with the pushed bundle's own boot error,
+ writes the recovery failure to the machine's log, and leaves the disposed instance in place.
+- A good push over that state lands and serves, without a restart.
+- A restart over that state resumes the version `harness.json` names.
diff --git a/changelog/unreleased/2026-08-23-double-fault-upgrade-channel-is-covered.zh.md b/changelog/unreleased/2026-08-23-double-fault-upgrade-channel-is-covered.zh.md
new file mode 100644
index 00000000..018a6fef
--- /dev/null
+++ b/changelog/unreleased/2026-08-23-double-fault-upgrade-channel-is-covered.zh.md
@@ -0,0 +1,22 @@
+# 二次故障这条路现在有测试覆盖
+
+- **Date:** 2026-08-23
+- **Type:** process
+- **Scope:** `server`
+- **PR:** [#425](https://github.com/Prism-Shadow/penguin-harness/pull/425)
+
+[English](2026-08-23-double-fault-upgrade-channel-is-covered.md)
+
+启动失败的恢复用重启「原本在运行的版本」来
+[回应一个启动不了的推送](2026-08-20-hot-update-failure-modes.zh.md)。而这次重启本身也失败时会发生
+什么,只在代码处写着——告警、让 `/api/hmr` 仍可接收后续推送、重启时恢复已提交的版本——却没有任何
+断言:恢复相关的测试全都停在第一次故障。
+
+现在测试会把第二次故障也走一遍,用的是一个只肯启动一次、之后拒绝任何启动的 platform。
+
+## 细节
+
+- 恢复同样失败的失败推送,返回的是被推送 bundle 自己的启动错误,把恢复失败写进机器自己的日志,并把
+ 已 dispose 的实例留在原处。
+- 在这个状态上推送一个好的版本能落地并开始服务,无需重启。
+- 在这个状态上重启,会恢复 `harness.json` 记录的那个版本。
diff --git a/packages/server/test/hmr-host.test.ts b/packages/server/test/hmr-host.test.ts
index 736f515e..fe5e1a0b 100644
--- a/packages/server/test/hmr-host.test.ts
+++ b/packages/server/test/hmr-host.test.ts
@@ -9,7 +9,7 @@ import fs from "node:fs/promises";
import fsSync from "node:fs";
import path from "node:path";
import zlib from "node:zlib";
-import { afterEach, describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import type { Hono } from "hono";
import type { AppEnv } from "../src/auth/middleware.js";
import { HmrHost } from "../src/hmr/host.js";
@@ -646,3 +646,147 @@ describe("upgrade provenance: recorded with the version, or not at all", () => {
await expect(readHarnessInfo(t.root)).resolves.toMatchObject({ source: null });
});
});
+
+/**
+ * A platform that boots once and throws on every create() after that, counting on a global
+ * so the count survives the cache-busted re-import recovery does. Pushing this, then a
+ * bundle that cannot boot, produces the DOUBLE fault: the push fails, and re-booting the
+ * previous version fails too — the state host.ts's recoverPrevious describes as "the
+ * process serves a half-stopped App until a successful push or a restart".
+ */
+function platformFailingOnReboot(id: string, path: string): string {
+ return `
+const anySchema = {
+ strictParse: (doc) => ({ ok: true, value: doc === undefined ? {} : doc }),
+ describe: () => ({ kind: "any" }),
+};
+const iface = {
+ kind: "iface",
+ name: "platform",
+ version: 1,
+ context: anySchema,
+ methods: ["park", "info"],
+ children: {},
+ migrations: {},
+};
+const impl = {
+ create(_ctx, context) {
+ globalThis.__doubleFaultBoots = (globalThis.__doubleFaultBoots ?? 0) + 1;
+ if (globalThis.__doubleFaultBoots > 1) throw new Error("re-boot refused");
+ return {
+ park: () => context,
+ info: () => ({ impl: ${JSON.stringify(id)} }),
+ http(request) {
+ const { pathname } = new URL(request.url);
+ if (pathname !== ${JSON.stringify(path)}) return null;
+ return new Response(JSON.stringify({ servedBy: ${JSON.stringify(id)} }), {
+ status: 200,
+ headers: { "content-type": "application/json" },
+ });
+ },
+ };
+ },
+};
+export const hotPlatform = { id: ${JSON.stringify(id)}, iface, impl, context: {} };
+`;
+}
+
+const faultGlobals = globalThis as typeof globalThis & { __doubleFaultBoots?: number };
+
+describe("double fault: the upgrade channel survives a failed push whose recovery also fails", () => {
+ let t: TestApp | undefined;
+ let freshRoot: string | undefined;
+
+ afterEach(async () => {
+ if (t) await t.cleanup();
+ if (freshRoot) await fs.rm(freshRoot, { recursive: true, force: true });
+ t = undefined;
+ freshRoot = undefined;
+ faultGlobals.__doubleFaultBoots = 0;
+ });
+
+ it("warns, keeps /api/hmr reachable, and lands the next good push", async () => {
+ faultGlobals.__doubleFaultBoots = 0;
+ t = await createTestApp();
+ const cookie = (await loginAdmin(t.app)).cookie;
+
+ expect(
+ (await pushPlatform(t.app, cookie, platformFailingOnReboot("brittle", "/api/demo/brittle")))
+ .status,
+ ).toBe(200);
+ expect((await t.app.request("/api/demo/brittle", { headers: { cookie } })).status).toBe(200);
+
+ const warnings: string[] = [];
+ const spy = vi.spyOn(process.stderr, "write").mockImplementation(((
+ chunk: string | Uint8Array,
+ ) => {
+ warnings.push(String(chunk));
+ return true;
+ }) as typeof process.stderr.write);
+
+ let bad;
+ try {
+ bad = await pushPlatform(t.app, cookie, BOOM_PLATFORM);
+ } finally {
+ spy.mockRestore();
+ }
+
+ // The push failed for the pushed bundle's own reason, and the second failure — the
+ // previous version refusing to come back — is reported on the machine's own log, which
+ // is the operator's only signal that the App behind the routes is stopped.
+ expect(bad.status).toBe(400);
+ expect(JSON.stringify(await bad.json())).toMatch(/failed to boot.*boom/);
+ expect(warnings.join("")).toMatch(/boot-failure recovery failed too/);
+
+ // The claim under test: /api/hmr is runtime-owned, so it answers out of the runtime's
+ // own routes no matter what state the platform tree is in, and a good push repairs the
+ // installation without a restart.
+ const good = await pushPlatform(
+ t.app,
+ cookie,
+ platformServing(["/api/demo/repaired"], "repaired"),
+ );
+ expect(good.status).toBe(200);
+ const served = await t.app.request("/api/demo/repaired", { headers: { cookie } });
+ expect(served.status).toBe(200);
+ expect((await served.json()) as object).toMatchObject({ servedBy: "repaired" });
+ });
+
+ it("leaves the committed version to be restored by a restart", async () => {
+ faultGlobals.__doubleFaultBoots = 0;
+ t = await createTestApp();
+ const cookie = (await loginAdmin(t.app)).cookie;
+
+ expect(
+ (await pushPlatform(t.app, cookie, platformFailingOnReboot("brittle", "/api/demo/brittle")))
+ .status,
+ ).toBe(200);
+ const before = await t.deps.hmr.ensure();
+ expect((await pushPlatform(t.app, cookie, BOOM_PLATFORM)).status).toBe(400);
+
+ // Recovery really did fail: the disposed instance is still the one the host hands out,
+ // which is the half-stopped state a restart is the other way out of. (Compare the
+ // single-fault case above, where ensure() answers with a freshly booted instance.)
+ expect(await t.deps.hmr.ensure()).toBe(before);
+
+ // A failed push persists nothing, so harness.json still names the version that was
+ // committed before it.
+ const root = t.root;
+ freshRoot = root;
+ t.deps.hmr.dispose();
+ t.deps.channels.dispose();
+ t.deps.db.close();
+ t = undefined; // torn down by hand; skip cleanup() (it would rm(root))
+
+ // A real restart is a fresh process, so the bundle's module state starts over.
+ faultGlobals.__doubleFaultBoots = 0;
+
+ const fresh = new HmrHost(root);
+ try {
+ const instance = await fresh.ensure();
+ expect((instance.api as unknown as { info(): { impl: string } }).info().impl).toBe("brittle");
+ } finally {
+ fresh.dispose();
+ }
+ });
+});