Skip to content

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

fix(server): a fleet push that cannot start is recorded with its Project

lmlihengf3e624c2a9a0
diff --git a/changelog/unreleased/2026-11-11-machines-fleet-sync-escape.md b/changelog/unreleased/2026-11-11-machines-fleet-sync-escape.md
new file mode 100644
index 00000000..ee041de1
--- /dev/null
+++ b/changelog/unreleased/2026-11-11-machines-fleet-sync-escape.md
@@ -0,0 +1,54 @@
+# A fleet push that cannot even start is recorded with its Project and its step
+
+- **Date:** 2026-11-11
+- **Type:** fix
+- **Scope:** `server`
+- **Breaking:** no
+
+[中文版](2026-11-11-machines-fleet-sync-escape.zh.md)
+
+Every write to a Project's model config — the whole-table PUT, the default switch, a key minted
+through a platform's OAuth flow — ends in `machines.syncModelsEverywhere(projectId)`, fired and
+forgotten: the person editing a key should not wait for a set of ssh tunnels. A plugin change rides
+the same push.
+
+That push was already best effort for each machine (the connect log carries a sync's own words),
+but the reads in front of those machines were not inside that catch: `list()` asks every alias in
+the ssh config for the session held to it, and one transport that throws there rejected a promise
+nobody holds. The process filed it as a bare `unhandled_rejection` — a record that named neither
+the Project whose edit asked for the push nor the step that failed, in a table where the Project
+column is what a reader filters by.
+
+The push now catches its own escape and reports it: `code` is `fleet_sync_failed`, and the Project
+is attached as the record's context. `MachinesEffects.recordError` gained the two fields — it used
+to carry only the error, so every such failure in this file was one indistinguishable class — and
+the standing-intent timer passes its own `machines_reconnect_failed` through the same port, which is
+the code the boot path in `platform.ts` already used for that fault. Per-machine failures stay
+silent, exactly as before: those have the connect log, and a machine that will not take a credential
+is not the fleet push failing to start.
+
+## Details
+
+- `packages/server/src/machines/service.ts`: `syncModelsEverywhere` wraps its body, including the
+ listing and the row reads that were outside the per-machine `try`, and records an escape through
+ `#effects.recordError(err, "fleet_sync_failed", projectId)`; `recordError` is now
+ `(err, code, projectId?) => void`; the timer's call passes `"machines_reconnect_failed"`; the
+ module's wiring goes through the new pure `machinesErrorRecord(err, code, projectId)`, which fills
+ in `source: "process"` and the Project context only when one asked for the work.
+
+## Tests
+
+- `packages/server/test/machines-fleet-sync.test.ts` (new, 5 cases): a push whose machine list throws
+ is recorded as `fleet_sync_failed` under the Project that asked and its promise still resolves; the
+ same fault with the promise discarded (the shape all three real callers have) leaves nothing
+ unhandled; a connected machine whose own sync fails stays silent; a push reaching no connected
+ machine records nothing; and the record shape carries the Project only when there is one.
+- Counter-proof: with the method reverted to its previous body, the two escape cases fail — the
+ discarded-promise one as an **unhandled rejection** raised from `#liveSession` through `list()`.
+
+## Compatibility
+
+No interface change on the wire, no new dependency, no Project config change. The push's behaviour
+when it succeeds, and its per-machine best effort, are unchanged; a real server's default switch and
+whole-table PUT were run against a fresh data root and left no error record and no unhandled
+rejection behind.
diff --git a/changelog/unreleased/2026-11-11-machines-fleet-sync-escape.zh.md b/changelog/unreleased/2026-11-11-machines-fleet-sync-escape.zh.md
new file mode 100644
index 00000000..2e348fb3
--- /dev/null
+++ b/changelog/unreleased/2026-11-11-machines-fleet-sync-escape.zh.md
@@ -0,0 +1,45 @@
+# 「推送整队机器」连开头都没做到时,错误中心记下是哪个项目、哪一步
+
+- **日期:** 2026-11-11
+- **类型:** 修复
+- **范围:** `server`
+- **破坏性变更:** 否
+
+[English](2026-11-11-machines-fleet-sync-escape.md)
+
+每一次写项目的模型配置 —— 整表 PUT、切默认模型、经平台 OAuth 流程铸出的 key —— 都收在
+`machines.syncModelsEverywhere(projectId)` 上,发出去就不等:改 key 的人不该等一组 ssh 隧道。插件表的改动
+也搭这一趟车。
+
+这趟推送对**每台机器**本来就是尽力而为(一次同步自己的话进连接日志),但机器之前的那几次读不在那个 catch
+里:`list()` 会问 ssh 配置里的每个别名「你现在握着哪条会话」,一条在那里抛错的传输,就让一个没有人持有的
+promise 被拒。进程于是把它记成一条光秃秃的 `unhandled_rejection` —— 既没说这次推送是哪个项目的谁改出来的,
+也没说是哪一步,而面板是按项目列筛选的。
+
+现在推送接住了自己的逃逸并把它报上来:错误码是 `fleet_sync_failed`,项目作为这条记录的语境带上。
+`MachinesEffects.recordError` 因此多了这两个字段 —— 它过去只带错误本身,这个文件里每一种「没有调用方」的失败
+都是同一个分不开的类;「保持连接」定时器走同一个口子,传自己的 `machines_reconnect_failed`,也就是
+`platform.ts` 的启动那一趟给同一类故障已经在用的错误码。**每台机器**自己的失败照旧不记:那些有连接日志,
+而一台机器不肯收下凭证,并不是这趟推送没能开始。
+
+## 细节
+
+- `packages/server/src/machines/service.ts`:`syncModelsEverywhere` 把整个方法体包起来,包括原本在每台机器
+ 的 `try` 之外的列表与行读取,逃逸经 `#effects.recordError(err, "fleet_sync_failed", projectId)` 记下;
+ `recordError` 的签名变成 `(err, code, projectId?) => void`;定时器那一处显式传 `"machines_reconnect_failed"`;
+ 模块的接线改走新增的纯函数 `machinesErrorRecord(err, code, projectId)` —— 它填 `source: "process"`,并且
+ 只在真有项目发问时才带上项目语境。
+
+## 测试
+
+- `packages/server/test/machines-fleet-sync.test.ts`(新增,5 条):机器列表抛错时,按发问的那个项目记一条
+ `fleet_sync_failed`,且调用方的 promise 仍然落地;同一个故障、promise 被丢弃(三个真实调用方都是这个形状)
+ 时没有任何东西逃出进程;某台已连接的机器自己同步失败不记;推送没够到任何已连接机器时一条都不记;以及记录
+ 形状只在有项目时才带项目。
+- 反证:把这个方法退回改动前的写法,两条逃逸用例全红 —— 丢弃 promise 的那条是**从 `#liveSession` 经 `list()`
+ 冒出来的 unhandled rejection**。
+
+## 兼容性
+
+线上接口没变、没有新依赖、项目配置没变。推送成功时的行为、以及它对每台机器的尽力而为都照旧;真实服务上跑过
+一次切默认模型与整表 PUT(一次性数据根),错误中心没有多出一条记录,进程日志没有 unhandled rejection。
diff --git a/packages/server/src/machines/service.ts b/packages/server/src/machines/service.ts
index d60cd805..81a47726 100644
--- a/packages/server/src/machines/service.ts
+++ b/packages/server/src/machines/service.ts
@@ -76,6 +76,7 @@ import { machinesRoutes } from "../http/routes/machines.js";
import { machinesProxy } from "./proxy.js";
import { HttpError } from "../http/errors.js";
import type { Errors } from "../mechanisms/observability.js";
+import type { ErrorRecordArgs } from "../runtime/error-recorder.js";
import type { Access } from "../mechanisms/projects.js";
import { Hono } from "hono";
import { MachinesRepo } from "../db/repos/machines.js";
@@ -135,10 +136,30 @@ export interface MachinesEffects {
now: () => Date;
/**
* Where a failure with no caller to throw to is reported — the standing-intent timer below
- * is the one such path here. Silent by default: a service built outside the platform (a test)
- * has no error table, and the rejection it would have recorded is the test's own to assert.
+ * and the whole-fleet push are the two such paths here. `code` is the class it is filed
+ * under, so one rule in the error centre silences that class and nothing else; `projectId`
+ * is the Project whose edit asked for the work, when there was one (a boot sweep has none).
+ * Silent by default: a service built outside the platform (a test) has no error table, and
+ * the rejection it would have recorded is the test's own to assert.
*/
- recordError: (err: unknown) => void;
+ recordError: (err: unknown, code: string, projectId?: string) => void;
+}
+
+/**
+ * How a machines failure with no caller reaches the error centre (machines/service.ts).
+ *
+ * `process` is the honest source: this is an error escaping a background task, not an HTTP
+ * answer — the boot sweep in platform.ts files its own class the same way. What was missing
+ * was not the record but the context: `code` says which step, and the Project is filled in
+ * when someone's edit is what set the work off, so the panel can point at it instead of
+ * leaving a bare message that could have come from anywhere.
+ */
+export function machinesErrorRecord(
+ err: unknown,
+ code: string,
+ projectId?: string,
+): ErrorRecordArgs {
+ return { source: "process", err, code, ctx: projectId === undefined ? undefined : { projectId } };
}
/** The id of the entry standing for the machine this server runs on. */
@@ -1141,7 +1162,7 @@ export class MachinesService {
// A stop() that arrived while this sweep was awaiting closed the database underneath it:
// that rejection IS the retirement, and reporting it would be telling the error table of a
// database this generation no longer owns about its own shutdown.
- if (!this.#stopped) this.#effects.recordError(err);
+ if (!this.#stopped) this.#effects.recordError(err, "machines_reconnect_failed");
}
}
@@ -1505,24 +1526,35 @@ export class MachinesService {
* interface member is a contract change a pushed platform pays for.
*/
async syncModelsEverywhere(projectId: string): Promise<void> {
- const connected = this.list(projectId).filter(
- (m) => !m.local && m.installed !== null && m.connection !== null,
- );
- await Promise.all(
- connected.map(async (machine) => {
- const address = machine.id;
- const port =
- this.#liveSession(address) === null ? null : (this.repo.get(address)?.remotePort ?? null);
- if (port === null) return;
- try {
- await this.#syncCoalesced(address, this.#targetOf(machine.alias), port, () => [
- projectId,
- ]);
- } catch {
- // Best effort: the connect log is where a sync's own words go; this one has none.
- }
- }),
- );
+ try {
+ const connected = this.list(projectId).filter(
+ (m) => !m.local && m.installed !== null && m.connection !== null,
+ );
+ await Promise.all(
+ connected.map(async (machine) => {
+ const address = machine.id;
+ const port =
+ this.#liveSession(address) === null
+ ? null
+ : (this.repo.get(address)?.remotePort ?? null);
+ if (port === null) return;
+ try {
+ await this.#syncCoalesced(address, this.#targetOf(machine.alias), port, () => [
+ projectId,
+ ]);
+ } catch {
+ // Best effort per machine: the connect log is where a sync's own words go; this one has none.
+ }
+ }),
+ );
+ } catch (err) {
+ // Best effort one level up too. The catch above covers a sync that failed, but listing
+ // the Project's machines and reading their rows happen outside it — and every caller of
+ // this is fire-and-forget (the models PUT, the default switch, a plugin change), so an
+ // escape here reached the process unhandled, filed as a bare `unhandled_rejection` that
+ // named neither the Project nor the step. This files it as one, with both.
+ this.#effects.recordError(err, "fleet_sync_failed", projectId);
+ }
}
}
@@ -1586,9 +1618,10 @@ export class MachinesModule {
{
// The standing-intent timer's failures have no caller to throw to; they go to the error
// centre under the same source and code the boot path uses for the same failure
- // (platform.ts), so one machine that will not come back is one silenceable class.
- recordError: (err) =>
- this.errors.record({ source: "process", err, code: "machines_reconnect_failed" }),
+ // (platform.ts), so one machine that will not come back is one silenceable class. The
+ // code is the port's, not this wiring's: the fleet push files its own class the same way.
+ recordError: (err, code, projectId) =>
+ this.errors.record(machinesErrorRecord(err, code, projectId)),
},
() => this.hmr.assetsDir(),
);
diff --git a/packages/server/test/machines-fleet-sync.test.ts b/packages/server/test/machines-fleet-sync.test.ts
new file mode 100644
index 00000000..dd373377
--- /dev/null
+++ b/packages/server/test/machines-fleet-sync.test.ts
@@ -0,0 +1,167 @@
+/**
+ * The whole-fleet push's escaped failures (machines/service.ts syncModelsEverywhere).
+ *
+ * The push is fired and forgotten — the models PUT, the default switch and a plugin change
+ * all `void` it, because the person editing a key should not wait for a set of ssh tunnels.
+ * Its per-machine failures were already swallowed (the connect log is where a sync's own
+ * words go), but the reads in front of them were not: `list()` asks each alias for its live
+ * session, and a transport throwing there rejected a promise nobody holds. The process then
+ * filed it as a bare `unhandled_rejection` — a message that named neither the Project whose
+ * edit asked for the push nor the step that failed.
+ *
+ * Scenarios:
+ * - A push whose machine list cannot be read is recorded as fleet_sync_failed, with the Project.
+ * - The caller's own promise still resolves: nothing new is thrown at anyone.
+ * - A connected machine whose sync fails on its own stays silent here (best effort, one level down).
+ * - A push that reaches no connected machine records nothing.
+ * - The record shape carries the Project only when one asked for the work.
+ *
+ * Only the reaching-out is faked, the way machines-lifecycle.test.ts does it: the logic
+ * under test is what happens around it.
+ */
+import { afterEach, describe, expect, it } from "vitest";
+import fs from "node:fs";
+import type { DatabaseSync } from "node:sqlite";
+import { openDatabase } from "../src/db/database.js";
+import { MachinesRepo } from "../src/db/repos/machines.js";
+import { MachinesService, machinesErrorRecord } from "../src/machines/service.js";
+import type { MachinesEffects } from "../src/machines/service.js";
+import { remoteLayoutFor } from "../src/machines/layout.js";
+import type { ShellSession } from "../src/machines/transport/index.js";
+import { makeTempRoot } from "./helpers.js";
+
+/** This machine's id; minted from the `machine` table in the real server, fixed here. */
+const LOCAL_ID = "TESTlocalID00000";
+
+describe("the whole-fleet push's escapes", () => {
+ const open: Array<{ store: DatabaseSync; root: string }> = [];
+ const services: MachinesService[] = [];
+
+ afterEach(() => {
+ for (const service of services.splice(0)) service.stop();
+ for (const { store, root } of open.splice(0)) {
+ store.close();
+ fs.rmSync(root, { recursive: true, force: true });
+ }
+ });
+
+ /**
+ * A service over a fresh root and store, with only the reaching-out faked. `reported` is
+ * everything the service would have handed the error centre, in order.
+ */
+ const harness = async (effects: Partial<MachinesEffects>) => {
+ const root = await makeTempRoot();
+ const store = openDatabase(":memory:");
+ open.push({ store, root });
+ // A membership follows its Project out (the foreign key), so the store needs the rows.
+ store
+ .prepare(
+ "INSERT INTO users (user_id, password_hash, is_admin, created_at) VALUES (?, ?, 1, ?)",
+ )
+ .run("admin", "x", "2026-11-11T00:00:00.000Z");
+ store
+ .prepare("INSERT INTO projects (project_id, owner_user_id, created_at) VALUES (?, ?, ?)")
+ .run("proj-a", "admin", "2026-11-11T00:00:00.000Z");
+ const reported: Array<{ err: unknown; code: string; projectId?: string }> = [];
+ const repo = new MachinesRepo(store);
+ const service = new MachinesService(
+ root,
+ LOCAL_ID,
+ repo,
+ {
+ listAliases: () => [],
+ resolvePlan: () => null,
+ now: () => new Date("2026-11-11T00:00:00.000Z"),
+ recordError: (err, code, projectId) => {
+ reported.push({ err, code, projectId });
+ },
+ ...effects,
+ },
+ undefined,
+ remoteLayoutFor("release"),
+ );
+ services.push(service);
+ return { service, repo, reported };
+ };
+
+ it("records a push whose machine list cannot be read, under the Project that asked", async () => {
+ // The transport is the reachable version of this: `list()` asks it for the session held
+ // to an alias, and one that throws takes the whole push down with it.
+ const boom = new Error("the ssh session closed underneath the read");
+ const { service, reported } = await harness({
+ listAliases: () => ["nas"],
+ session: () => {
+ throw boom;
+ },
+ });
+
+ await expect(service.syncModelsEverywhere("proj-a")).resolves.toBeUndefined();
+
+ expect(reported).toHaveLength(1);
+ expect(reported[0]).toEqual({ err: boom, code: "fleet_sync_failed", projectId: "proj-a" });
+ });
+
+ it("raises nothing at a caller that discards the promise — the shape all three real callers have", async () => {
+ const { service, reported } = await harness({
+ listAliases: () => ["nas"],
+ session: () => {
+ throw new Error("the ssh session closed underneath the read");
+ },
+ });
+
+ // `void` is what models.ts (twice) and plugins-installed.ts actually do. A rejection that
+ // escaped would reach vitest as an unhandled rejection and fail this file — which is
+ // precisely the pre-change shape, and what the catch is here to keep off the process.
+ void service.syncModelsEverywhere("proj-a");
+ await new Promise((resolve) => setTimeout(resolve, 5));
+
+ expect(reported.map((r) => r.code)).toEqual(["fleet_sync_failed"]);
+ });
+
+ it("stays silent when a connected machine's own sync fails: that is best effort, one level down", async () => {
+ // A held connection and an installed record put the machine past `list()`'s filter; the
+ // minted session then fails, which is the per-machine catch's case, not this one's.
+ const { service, repo, reported } = await harness({
+ listAliases: () => ["nas"],
+ session: () => ({ pid: 4242 }) as unknown as ShellSession,
+ mintToken: async () => ({ kind: "failed" as const, detail: "no login" }),
+ });
+ repo.patch("ssh:nas", {
+ version: "0.6.0",
+ installedAt: "2026-11-01T00:00:00.000Z",
+ remotePort: 9000,
+ });
+ repo.setMembers("proj-a", ["ssh:nas"]);
+
+ await service.syncModelsEverywhere("proj-a");
+
+ expect(reported).toEqual([]);
+ });
+
+ it("records nothing when the push reaches no connected machine", async () => {
+ const { service, reported } = await harness({ listAliases: () => ["nas"] });
+
+ await service.syncModelsEverywhere("proj-a");
+
+ expect(reported).toEqual([]);
+ });
+
+ it("files the Project only when one asked for the work", () => {
+ const err = new Error("x");
+
+ expect(machinesErrorRecord(err, "fleet_sync_failed", "proj-a")).toEqual({
+ source: "process",
+ err,
+ code: "fleet_sync_failed",
+ ctx: { projectId: "proj-a" },
+ });
+ // The standing-intent timer's sweeps: no Project asked, and this is the record the boot
+ // path in platform.ts writes for the same failure.
+ expect(machinesErrorRecord(err, "machines_reconnect_failed")).toEqual({
+ source: "process",
+ err,
+ code: "machines_reconnect_failed",
+ ctx: undefined,
+ });
+ });
+});