pi-foreman 0.0.0-stage → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 pi-foreman contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,180 @@
1
- # Temporary Holding Version
1
+ # pi-foreman
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 在本地机器上运行一个控制台,指挥分布在**远程服务器 / Docker 容器**里的 AI coding agent 做开发工作。
4
+ Agent 的循环、存储、凭据全部留在本地;**只有工具执行落在远端**。
5
+
6
+ ```
7
+ 你的 Mac / Linux
8
+ ├── pi-durable Harness(单进程,单 SQLite)
9
+ │ ├── conversation A ──env──> 服务器 1
10
+ │ ├── conversation B ──env──> 服务器 2 ▸ 容器 X
11
+ │ └── conversation C ──env──> 本地
12
+ ├── pi-env daemon(2MB musl 静态二进制,经 SSH 部署到每个环境)
13
+ └── Web GUI(3 个面板 + 输入框内的模型 / 上下文指示)
14
+ ```
15
+
16
+ ## 它解决什么问题
17
+
18
+ - 远端**没有外网**、或**不允许安装 agent CLI** —— 没关系,远端只需要 sshd 和一个 2MB 静态二进制
19
+ - 想在**容器里**开发而不是在宿主机上 —— 支持 `docker exec` 直达容器
20
+ - 想让**两个环境的 agent 互相提问、对照分析** —— `/name` 给会话起名,在另一个会话里 `@名字` 直接问
21
+ - 想在一个界面里**同时看着所有环境**在干什么 —— 文件树 + 每个会话的上下文占用
22
+
23
+ 这些能力组合起来,**市面上没有现成产品具备**(详见 `docs/decisions.md`)。
24
+
25
+ ## 快速开始
26
+
27
+ ```bash
28
+ # 0. 前置:Node >= 22.19;远端只需 sshd + 密钥认证
29
+ npm install
30
+
31
+ # 1. 生成配置并编辑
32
+ foreman init
33
+ $EDITOR ~/.pi/pi-foreman/config.json
34
+
35
+ # 2. 信任 host key(显示指纹,人工带外核对后才写入)
36
+ foreman trust <env-name> # 只看指纹
37
+ foreman trust <env-name> --yes # 核对无误后接受
38
+
39
+ # 3. 部署 daemon(裸 SSH 环境会自动部署;容器环境走 docker cp + 哈希校验)
40
+ foreman deploy <env-name>
41
+
42
+ # 4. 全链路体检
43
+ foreman doctor
44
+
45
+ # 5. 启动服务(GUI 与 Harness 必须同进程)
46
+ foreman serve # http://127.0.0.1:8788
47
+ ```
48
+
49
+ 开发态前端:
50
+
51
+ ```bash
52
+ cd web && npm install && npm run dev # http://127.0.0.1:5173,/api 代理到 8788
53
+ ```
54
+
55
+ ## CLI
56
+
57
+ | 命令 | 作用 |
58
+ |---|---|
59
+ | `foreman doctor` | 全链路体检:Node / CA / 版本锁步 / 代理→LLM / 各环境连通性 + RTT + 远端工具 |
60
+ | `foreman init` | 生成起始配置 |
61
+ | `foreman envs` | 列出并探测所有环境(在线状态、延迟评级) |
62
+ | `foreman trust <name> [--yes]` | 扫描 / 接受 host key(**永不自动接受**) |
63
+ | `foreman deploy <name>` | 部署 daemon 到环境 |
64
+ | `foreman ask <from> <to> "<问题>" [--timeout 秒] [--new]` | 跨环境提问(无头版,打印回答) |
65
+ | `foreman serve [--port 8788] [--host 127.0.0.1]` | 启动 HTTP + SSE 服务 + GUI |
66
+ | `foreman models` | 列出当前 provider 可用模型 |
67
+ | `foreman conversations` | 列出已登记会话(找 id 用) |
68
+
69
+ ## 配置(`~/.pi/pi-foreman/config.json`)
70
+
71
+ 数据目录可用 `PI_FOREMAN_DATA_DIR` 覆盖。**能派生的值一律不存**(knownHostsFile / hostKeyAlias / envId / daemon 路径都按环境名派生)。
72
+
73
+ `environments` 既可以手写,也可以从 GUI 环境栏的 `+` 添加(`foreman trust` / `foreman deploy` 的向导版)。
74
+ 写入口**只对本机 127.0.0.1 开放**,且只接受白名单字段 —— 见 [`docs/decisions.md`](docs/decisions.md) D-18。
75
+
76
+ ```jsonc
77
+ {
78
+ "version": 1,
79
+ "ssh": {
80
+ "program": null, // null = PATH 里的 ssh
81
+ "configFile": null // null = ~/.ssh/config
82
+ },
83
+ "proxy": { // 省略/全 null = 继承环境变量
84
+ "http": null, "https": null, "noProxy": null
85
+ },
86
+ "model": {
87
+ "provider": "anthropic", // pi-ai 内置名,或第三方 provider id
88
+ "providerModule": "pi-acme/src/extension.ts", // 可选:第三方 provider 模块(jiti 加载,支持 .ts)
89
+ "providerExport": "createAcmeProvider", // 可选:导出名,默认 default
90
+ "modelId": "deepseek-v4.1-flash"
91
+ },
92
+ "harness": {
93
+ "settings": null // 可选:透传 HarnessSettings(progress.outputIntervalMs 等)
94
+ },
95
+ "environments": {
96
+ "my-host": {
97
+ "type": "ssh",
98
+ "host": "ssh-alias", // ~/.ssh/config 别名或主机名
99
+ "user": null, "port": null,
100
+ "identityFile": null, // 必须绝对路径(pi-env 不展开 ~)
101
+ "cwd": "/path/to/project",
102
+ "binDir": "/path/to/tools" // 可选:前置进远端 PATH(放 rg 等)
103
+ },
104
+ "my-container": {
105
+ "type": "ssh",
106
+ "host": "ssh-alias",
107
+ "container": "container-name", // 有此项则走 docker exec -i
108
+ "containerUser": "1000:1000", // 可选:docker exec -u(uid 映射,见 docs/maintenance.md)
109
+ "daemonPath": "/opt/pi-env/pi-env",
110
+ "cwd": "/path/in/container",
111
+ "binDir": "/path/to/tools",
112
+ "shellEnv": { "HOME": "/home/me" } // 可选:注入远端环境变量
113
+ },
114
+ "local": { "type": "local" }
115
+ }
116
+ }
117
+ ```
118
+
119
+ ## GUI 面板(按优先级)
120
+
121
+ 1. **左栏** —— 顶部「新建会话」,下面是平铺的环境(本机在前),每个环境一个点表示连接质量:
122
+ 绿=在线、黄=在线但延迟可用/偏慢、红=离线、灰=未探测(悬停看延迟毫秒数与评级)。**状态是按需探测的**(新建会话 / 手动探测 / 增删环境时),
123
+ 没探过就是「未探测」—— 不会拿旧数据装作现在在线(见 [`docs/decisions.md`](docs/decisions.md) D-19)。
124
+ Host 不再分组(它是配置细节,在环境行的 tooltip 里),`⋯` 菜单里有编辑 / 部署 / 重新探测 / 删除;「+」是**添加环境向导**:配置 → 信任 host key → 部署 daemon → 探测,写完 `config.json` **不用重启 serve**。
125
+ 展开环境看到目录和会话(标题 + 相对时间);**会话发出第一条消息时自动用消息首行起名**,`/name` 可改。
126
+ 会话行悬停有「⫶」可在右侧分屏打开;切走的会话若还在跑,行首有运行中的脉冲点。
127
+ 左下角:与 serve 的连接点(异常时才出文字)· 主题。
128
+ 2. **会话主区** —— 没有顶栏和标签行,左栏就是导航;启动时自动打开最近的会话。
129
+ 分屏时每个面板顶部有一条细标题(标题 + 关闭)。模型切换器在输入框底部左下角,向上弹出
130
+ 3. **右侧 Dock:文件树 / git diff / Skills** —— 标题行三个图标切换;Skills 页**跟随当前会话的目录**(顶部一行只读上下文,点击打开该目录的完整设置),扫描远端 `AGENTS.md` 与 `.agents/skills/`,勾选即保存到目录配置,可过滤、可从本机 Skill 库安装或新建写入目录
131
+
132
+ 输入框底部:模型切换器(向上弹出)和 **`● ctx 42%`** —— 当前会话 prompt 占模型上下文窗口的比例,
133
+ 平时灰色,≥50% 变黄、≥85% 变红;悬停看 token 数、本会话累计与缓存命中率;接近上限就该 `/compact`。
134
+ `/usage` 按需拉一次累计 token,弹按模型的摘要(平时不轮询)。
135
+ 消息区左侧有一条**问答轴**:一轮问答一个灰点(当前轮深灰、最新一轮纯黑),悬停预览问题与回答开头,点击滚到那一轮。
136
+
137
+ 聊天框支持 `/` 命令:`/compact /abort /model /thinking /name /ask /exec /fork /reset /cd /instructions /usage /diff /help`。
138
+
139
+ ### 跨环境协作:`/name` + `@名字`
140
+
141
+ 没有按钮、弹窗和面板,整条链路只有文本:
142
+
143
+ ```
144
+ /name db ← 给当前会话起名(title 就是句柄)
145
+ @db 容器里 redis 的 maxmemory 是多少? ← 在 A 的输入框直接发;/ask @db … 等价
146
+ ```
147
+
148
+ 输入 `@` 会弹出可 @ 的会话列表(↑↓ 选、Enter 插入;重名自动插 `@环境/名字`)。
149
+ 发送前宿主把 `@db` 解析成那个会话(`/name` 过的会话才能被 @),
150
+ 在消息末尾追加一行注释让 agent 调 `ask_peer(targetConversationId=…)` 精确提问。
151
+ A 的工具调用 park 住等 B 的会话回答,答案作为 tool result 回到 A 的上下文 —— 不需要「采纳」。
152
+ `maxDepth(2)` + 环路检测防止 A→B→A 死锁;一条消息里 @ 多个会话就是多环境对比。
153
+
154
+ ## 文档索引
155
+
156
+ | 文档 | 内容 |
157
+ |---|---|
158
+ | [`docs/requirements.md`](docs/requirements.md) | 收敛后的需求 + 实测环境事实 |
159
+ | [`docs/decisions.md`](docs/decisions.md) | 技术选型:每个决定、否决的备选、理由、后果 |
160
+ | [`docs/architecture.md`](docs/architecture.md) | 组件地图、数据流、**关键实现不变量(踩坑清单)** |
161
+ | [`docs/verification.md`](docs/verification.md) | 验证记录:spike / 测试套件 / 浏览器实测 / 实测数字 |
162
+ | [`docs/maintenance.md`](docs/maintenance.md) | 升级义务、已知陷阱、留白 |
163
+ | [`docs/roadmap.md`](docs/roadmap.md) | 下一步与明确推迟项 |
164
+
165
+ ## 测试
166
+
167
+ ```bash
168
+ npm test # env-config(环境写入口)+ v2-e2e(HTTP/WS/ask_peer/自动起名,faux 模型,纯本地)
169
+ npm run typecheck # web
170
+ ```
171
+
172
+ 两套都用 faux 模型与临时数据目录,不需要真实环境或网络。升级 `@earendil-works/*` 后先跑它(见 `docs/maintenance.md`)。
173
+
174
+ ## 状态
175
+
176
+ 自用工具,pre-release。依赖的 `@earendil-works/pi-durable` 标记为 **Experimental**(API 随时会变)—— 本项目用防腐层隔离,详见 `docs/maintenance.md`。
177
+
178
+ ## License
179
+
180
+ 待定(开源前确认;上游 pi 系为 MIT)。
package/package.json CHANGED
@@ -1,6 +1,60 @@
1
1
  {
2
2
  "name": "pi-foreman",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.2",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "description": "Local controller for pi-durable agents working across remote SSH environments",
7
+ "keywords": [
8
+ "pi",
9
+ "ai",
10
+ "coding-agent",
11
+ "ssh",
12
+ "docker",
13
+ "remote",
14
+ "pi-durable",
15
+ "pi-env"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/fengz72/pi-foreman.git"
20
+ },
21
+ "homepage": "https://github.com/fengz72/pi-foreman#readme",
22
+ "bugs": {
23
+ "url": "https://github.com/fengz72/pi-foreman/issues"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "files": [
29
+ "src/",
30
+ "web/dist/",
31
+ "!web/dist/**/*.map",
32
+ "README.md",
33
+ "LICENSE"
34
+ ],
35
+ "engines": {
36
+ "node": ">=22.19.0"
37
+ },
38
+ "bin": {
39
+ "foreman": "src/cli.mjs",
40
+ "pi-foreman": "src/cli.mjs"
41
+ },
42
+ "scripts": {
43
+ "build:web": "npm --prefix web run build",
44
+ "doctor": "node --disable-warning=ExperimentalWarning src/cli.mjs doctor",
45
+ "envs": "node --disable-warning=ExperimentalWarning src/cli.mjs envs",
46
+ "login": "node --disable-warning=ExperimentalWarning src/cli.mjs login",
47
+ "start": "node --disable-warning=ExperimentalWarning src/cli.mjs serve",
48
+ "test": "node --disable-warning=ExperimentalWarning test/env-config.mjs && node --disable-warning=ExperimentalWarning test/v2-e2e.mjs"
49
+ },
50
+ "dependencies": {
51
+ "@earendil-works/chord": "^1.6.1",
52
+ "@earendil-works/pi-ai": "^1.6.1",
53
+ "@earendil-works/pi-durable": "^1.6.1",
54
+ "@earendil-works/pi-env": "^1.6.1",
55
+ "@earendil-works/pi-schema": "^1.6.1",
56
+ "jiti": "^2.7.0",
57
+ "undici": "^8.13.1",
58
+ "ws": "^8.23.0"
59
+ }
60
+ }
@@ -0,0 +1,107 @@
1
+ /**
2
+ * 必须在导入任何 @earendil-works/* 之前执行。
3
+ *
4
+ * 做三件事:
5
+ * 1. CA bundle 兜底 —— Homebrew Node 用 --shared-openssl 构建,若 OpenSSL 的 cert.pem
6
+ * 缺失,Node 连直连站点都会 UNABLE_TO_GET_ISSUER_CERT_LOCALLY。
7
+ * 2. 代理引导 —— pi-durable 没有 proxy 选项,且只设 HTTPS_PROXY 环境变量对 Node 内置
8
+ * fetch 无效;必须 setGlobalDispatcher + install()。
9
+ * 3. 版本锁步断言 —— @earendil-works/* 不绑死固定版本,跟随社区升级,但 5 个核心包
10
+ * 必须彼此版本一致,避免 npm 装出第二份 chord/durable 使 instanceof 静默失效。
11
+ */
12
+ import { existsSync, readFileSync } from "node:fs";
13
+ import { createRequire } from "node:module";
14
+
15
+ const LOCKSTEP_PACKAGES = ["chord", "pi-ai", "pi-durable", "pi-env", "pi-schema"];
16
+
17
+ // ── 1. CA bundle 兜底 ────────────────────────────────────────────────────────
18
+ // 只在检测到「Homebrew OpenSSL 存在但 cert.pem 缺失」时介入,其他平台不动。
19
+ export function ensureCaBundle() {
20
+ if (process.env.SSL_CERT_FILE || process.env.NODE_EXTRA_CA_CERTS) return { applied: false, reason: "already-set" };
21
+
22
+ const opensslDirs = ["/opt/homebrew/etc/openssl@3", "/usr/local/etc/openssl@3"];
23
+ const broken = opensslDirs.find((d) => existsSync(d) && !existsSync(`${d}/cert.pem`));
24
+ if (!broken) return { applied: false, reason: "not-needed" };
25
+
26
+ const candidates = [
27
+ "/opt/homebrew/share/ca-certificates/cacert.pem",
28
+ "/etc/ssl/cert.pem",
29
+ "/etc/ssl/certs/ca-certificates.crt",
30
+ ];
31
+ const bundle = candidates.find((p) => existsSync(p));
32
+ if (!bundle) return { applied: false, reason: "no-bundle-found", broken };
33
+
34
+ // 必须在任何 TLS 连接之前设置才有效
35
+ process.env.SSL_CERT_FILE = bundle;
36
+ return { applied: true, bundle, broken };
37
+ }
38
+
39
+ // ── 2. 代理引导 ──────────────────────────────────────────────────────────────
40
+ /**
41
+ * @param {{http?:string|null, https?:string|null, noProxy?:string|null}} [proxy]
42
+ * 省略或全为 null 时继承环境变量(http_proxy/https_proxy/no_proxy,小写优先)。
43
+ * 注意:undici 在构造时就固化 proxy URI,运行时改代理必须重建 dispatcher。
44
+ */
45
+ export async function installProxyDispatcher(proxy = {}) {
46
+ const undici = await import("undici");
47
+ const options = { allowH2: false, proxyTunnel: true };
48
+ if (proxy.http) options.httpProxy = proxy.http;
49
+ if (proxy.https) options.httpsProxy = proxy.https;
50
+ if (proxy.noProxy) options.noProxy = proxy.noProxy;
51
+
52
+ undici.setGlobalDispatcher(new undici.EnvHttpProxyAgent(options));
53
+ const before = globalThis.fetch;
54
+ undici.install?.();
55
+ return { installed: globalThis.fetch !== before, options };
56
+ }
57
+
58
+ // ── 3. 版本锁步断言(仅校验彼此一致,不绑死固定版本号)──────────────────────
59
+ export function assertVersionLockstep(require = createRequire(import.meta.url)) {
60
+ const found = [];
61
+ for (const name of LOCKSTEP_PACKAGES) {
62
+ const pkg = `@earendil-works/${name}`;
63
+ let version;
64
+ try {
65
+ version = JSON.parse(readFileSync(require.resolve(`${pkg}/package.json`), "utf8")).version;
66
+ } catch {
67
+ try {
68
+ version = JSON.parse(readFileSync(new URL(`../node_modules/${pkg}/package.json`, import.meta.url), "utf8")).version;
69
+ } catch (e) {
70
+ found.push({ pkg, version: `MISSING (${e.code})`, missing: true });
71
+ continue;
72
+ }
73
+ }
74
+ found.push({ pkg, version, missing: false });
75
+ }
76
+
77
+ const missing = found.some((f) => f.missing);
78
+ const versions = new Set(found.filter((f) => !f.missing).map((f) => f.version));
79
+ const ok = !missing && versions.size === 1;
80
+ return { ok, found, version: ok ? [...versions][0] : undefined };
81
+ }
82
+
83
+ export function installedPiVersion() {
84
+ const res = assertVersionLockstep();
85
+ return res.version ?? "unknown";
86
+ }
87
+
88
+ // ── Node 版本 ────────────────────────────────────────────────────────────────
89
+ export function checkNodeVersion() {
90
+ const [maj, min] = process.versions.node.split(".").map(Number);
91
+ const ok = maj > 22 || (maj === 22 && min >= 19);
92
+ return { ok, current: process.versions.node, required: ">=22.19.0" };
93
+ }
94
+
95
+ /** 一次性完成全部引导。返回诊断信息供 doctor 展示。 */
96
+ export async function bootstrap(proxy) {
97
+ const ca = ensureCaBundle();
98
+ const node = checkNodeVersion();
99
+ if (!node.ok) throw new Error(`Node ${node.current} 不满足 ${node.required}`);
100
+ const versions = assertVersionLockstep();
101
+ if (!versions.ok) {
102
+ const detail = versions.found.map((f) => `${f.pkg}@${f.version}`).join(", ");
103
+ throw new Error(`@earendil-works/* 版本不一致或缺失:${detail}\n混版本会装出第二份 chord/durable,导致 instanceof 静默失效。`);
104
+ }
105
+ const dispatcher = await installProxyDispatcher(proxy);
106
+ return { ca, node, versions, dispatcher };
107
+ }