Skip to content

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

refactor(server): every word to a machine leaves through one door (#567)

Myriad-Dreamina80789c79a5f
diff --git a/changelog/unreleased/2026-09-01-machines-transport.md b/changelog/unreleased/2026-09-01-machines-transport.md
new file mode 100644
index 00000000..cb5b8346
--- /dev/null
+++ b/changelog/unreleased/2026-09-01-machines-transport.md
@@ -0,0 +1,16 @@
+# One door to a machine: machines/transport/
+
+- **Date:** 2026-09-01
+- **Type:** refactor
+- **Scope:** `server`
+- **PR:** [#567](https://github.com/Prism-Shadow/penguin-harness/pull/567)
+
+[中文版](2026-09-01-machines-transport.zh.md)
+
+Reaching another machine moves behind one directory. `machines/exec.ts` and `machines/targets.ts` become `machines/transport/exec.ts` and `machines/transport/targets.ts`, private behind `transport/index.ts`, and a test scans the source to keep them there. No behavior changes.
+
+## Details
+
+- **The rule is about authority, not sockets.** Opening ssh is how this server acts on a machine at all, so it is worth one place that owns it rather than a spawn at each call site. A caller that opens its own channel also judges the machine by that channel — and "my ssh worked" is not the same fact as "that machine is healthy". Keeping the door single is what lets those be told apart.
+- **Pinned by a source scan** (`machines-transport-boundary.test.ts`) rather than by agreement: nothing outside the directory may spawn `ssh`/`scp`, reach past `transport/index.js`, or import the modules at their old top-level paths. It runs with the suite everywhere and names the offending file. Tests are exempt, since a unit test of a private module imports it by nature.
+- **What sits behind the door is expected to change.** The raw runners the index exports today are what the install path needs; they narrow to a per-machine connection handle once one exists, and a caller importing only from the index will not notice.
diff --git a/changelog/unreleased/2026-09-01-machines-transport.zh.md b/changelog/unreleased/2026-09-01-machines-transport.zh.md
new file mode 100644
index 00000000..657837ed
--- /dev/null
+++ b/changelog/unreleased/2026-09-01-machines-transport.zh.md
@@ -0,0 +1,16 @@
+# 通往机器的唯一入口:machines/transport/
+
+- **Date:** 2026-09-01
+- **Type:** refactor
+- **Scope:** `server`
+- **PR:** [#567](https://github.com/Prism-Shadow/penguin-harness/pull/567)
+
+[English](2026-09-01-machines-transport.md)
+
+触达另一台机器的能力收进一个目录。`machines/exec.ts` 与 `machines/targets.ts` 变为 `machines/transport/exec.ts` 与 `machines/transport/targets.ts`,藏在 `transport/index.ts` 之后,并由一个扫描源码的测试把它们钉在那里。行为没有任何变化。
+
+## 细节
+
+- **这条规矩管的是权限,不是套接字数量。** 打开 ssh 就是本服务端对一台机器施加动作的全部方式,因此值得有一处地方拥有它,而不是每个调用点各自 spawn。自己开通道的调用点,随后也会用那条通道去判断机器的状态——而「我的 ssh 通了」和「那台机器是健康的」并不是同一个事实。把入口保持为单一,正是日后能把这两个事实分开讲的前提。
+- **由源码扫描钉住**(`machines-transport-boundary.test.ts`),而不是靠约定:目录之外不得 spawn `ssh`/`scp`、不得越过 `transport/index.js`、也不得再按旧的顶层路径引用这些模块。它随测试套件在所有平台运行,失败时直接点名文件。测试自身豁免——私有模块的单元测试天然要引用它。
+- **门后的东西预期会变。** index 现在导出的这些原始 runner 是安装路径当下所需;等到有了按机器的连接句柄,它们会收窄成那个句柄,而只从 index 引用的调用方不会察觉。
diff --git a/packages/server/src/machines/install-server.ts b/packages/server/src/machines/install-server.ts
index 48ae6caa..5470b0ec 100644
--- a/packages/server/src/machines/install-server.ts
+++ b/packages/server/src/machines/install-server.ts
@@ -26,7 +26,7 @@ import { runInstallScriptCommand, scpArgs, sshArgs, unpackStoreCommand } from ".
import type { RemoteTarget } from "./commands.js";
import { parseProbeOutput, POSIX_PROBE, WINDOWS_PROBE } from "./detect.js";
import type { RemoteIdentity, RemotePlatform } from "./detect.js";
-import { looksLikeAuthFailure, run, runPiped, runWithInput } from "./exec.js";
+import { looksLikeAuthFailure, run, runPiped, runWithInput } from "./transport/index.js";
/** Which installer runs the far side; also the asset keys deploy.mjs pushes. */
const installerFileFor = (platform: RemotePlatform): string =>
diff --git a/packages/server/src/machines/service.ts b/packages/server/src/machines/service.ts
index 6d6ebf5f..b7dd1dc9 100644
--- a/packages/server/src/machines/service.ts
+++ b/packages/server/src/machines/service.ts
@@ -26,7 +26,7 @@
* 30 MB transfer to reach the same place.
*/
import type { MachineInfo, MachineInstallJob } from "../api/types.js";
-import { listHostAliases, resolveTarget } from "./targets.js";
+import { listHostAliases, resolveTarget } from "./transport/index.js";
import { installOnRemote, resolvePushPlan } from "./install-server.js";
import type { MachinesRepo } from "../db/repos/machines.js";
diff --git a/packages/server/src/machines/exec.ts b/packages/server/src/machines/transport/exec.ts
similarity index 100%
rename from packages/server/src/machines/exec.ts
rename to packages/server/src/machines/transport/exec.ts
diff --git a/packages/server/src/machines/transport/index.ts b/packages/server/src/machines/transport/index.ts
new file mode 100644
index 00000000..4e245821
--- /dev/null
+++ b/packages/server/src/machines/transport/index.ts
@@ -0,0 +1,21 @@
+/**
+ * The transport directory's one door: everything machines/ needs in order to reach another
+ * machine comes through here, and the modules behind it (exec.ts, targets.ts) are private.
+ *
+ * The rule is about AUTHORITY, not about sockets. Opening ssh is how this server acts on a
+ * machine at all — installing a program directory, reading what is there, later holding a
+ * shell and a tunnel open — so it is worth one place that owns it rather than a spawn at
+ * every call site. A caller that opens its own channel also judges the machine by that
+ * channel, and "my ssh worked" is not the same fact as "that machine is healthy"; keeping
+ * the door single is what lets those facts be told apart later.
+ *
+ * machines-transport-boundary.test.ts pins it by scanning the source, so the rule survives
+ * a new call site added in a hurry.
+ *
+ * What sits behind the door is expected to change: the raw runners below are what the
+ * install path needs today, and they narrow to a per-machine connection handle once one
+ * exists. A caller importing only from here does not notice that.
+ */
+export { looksLikeAuthFailure, run, runPiped, runWithInput } from "./exec.js";
+export type { ExecResult } from "./exec.js";
+export { listHostAliases, resolveTarget } from "./targets.js";
diff --git a/packages/server/src/machines/targets.ts b/packages/server/src/machines/transport/targets.ts
similarity index 97%
rename from packages/server/src/machines/targets.ts
rename to packages/server/src/machines/transport/targets.ts
index 6c8be028..e5e01c8c 100644
--- a/packages/server/src/machines/targets.ts
+++ b/packages/server/src/machines/transport/targets.ts
@@ -11,8 +11,8 @@ import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { run } from "./exec.js";
-import { machineIdentity, parseHostAliases, parseSshSettings } from "./ssh-config.js";
-import type { SshSettings } from "./ssh-config.js";
+import { machineIdentity, parseHostAliases, parseSshSettings } from "../ssh-config.js";
+import type { SshSettings } from "../ssh-config.js";
const SSH_DIR = () => path.join(os.homedir(), ".ssh");
diff --git a/packages/server/test/machines-transport-boundary.test.ts b/packages/server/test/machines-transport-boundary.test.ts
new file mode 100644
index 00000000..9bb59c34
--- /dev/null
+++ b/packages/server/test/machines-transport-boundary.test.ts
@@ -0,0 +1,64 @@
+/**
+ * One door per machine: nothing outside machines/transport/ may open ssh or reach around
+ * the directory's index. The rule is worth pinning rather than agreeing to, because the
+ * failure it prevents is quiet — a call site that opens its own channel goes on to judge
+ * the machine by that channel, and "my ssh worked" is not the same fact as "that machine
+ * is healthy".
+ *
+ * A source scan rather than a lint rule: it runs with the suite everywhere, and its failure
+ * message names the offending file. Tests are exempt — a unit test of a private module
+ * imports it by nature.
+ */
+import fs from "node:fs";
+import path from "node:path";
+import { fileURLToPath } from "node:url";
+import { describe, expect, it } from "vitest";
+
+const SRC = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "src");
+
+function* walk(dir: string): Generator<string> {
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
+ const full = path.join(dir, entry.name);
+ if (entry.isDirectory()) yield* walk(full);
+ else if (entry.name.endsWith(".ts")) yield full;
+ }
+}
+
+const files = [...walk(SRC)].map((full) => ({
+ rel: path.relative(SRC, full).replaceAll(path.sep, "/"),
+ text: fs.readFileSync(full, "utf8"),
+}));
+const outside = files.filter((f) => !f.rel.startsWith("machines/transport/"));
+
+describe("the transport boundary", () => {
+ it("only machines/transport/ spawns ssh or scp", () => {
+ const spawners = outside
+ .filter((f) => /\b(?:spawn|execFile)\(\s*["'](?:ssh|scp)["']/.test(f.text))
+ .map((f) => f.rel);
+ expect(spawners).toEqual([]);
+ });
+
+ it("the directory is entered through its index alone", () => {
+ const reachers = outside
+ .filter((f) => /from\s+["'][^"']*\/transport\/(?!index\.js)/.test(f.text))
+ .map((f) => f.rel);
+ expect(reachers).toEqual([]);
+ });
+
+ it("the private modules' old top-level paths stay gone", () => {
+ // Each relative specifier is resolved against the importing file rather than matched by
+ // shape, so a caller anywhere under src/ reaching for `../machines/exec.js` is caught,
+ // not only a sibling writing `./exec.js`.
+ const gone = new Set(["machines/exec.js", "machines/targets.js"]);
+ const specifiers = /from\s+["'](\.{1,2}\/[^"']+)["']/g;
+ const stragglers = outside
+ .filter((f) => {
+ const dir = path.posix.dirname(f.rel);
+ return [...f.text.matchAll(specifiers)].some((m) =>
+ gone.has(path.posix.normalize(path.posix.join(dir, m[1]!))),
+ );
+ })
+ .map((f) => f.rel);
+ expect(stragglers).toEqual([]);
+ });
+});