pi-farm 0.2.1 → 0.2.3
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/README.md +44 -1
- package/bin/pp.js +5 -0
- package/completions/_pp +6 -0
- package/completions/pp.bash +13 -4
- package/docs/architecture.md +151 -0
- package/docs/autostart.md +166 -0
- package/docs/cli.md +207 -0
- package/docs/configuration.md +160 -0
- package/docs/development.md +129 -0
- package/docs/troubleshooting.md +204 -0
- package/package.json +2 -1
- package/src/autostart.js +7 -1
- package/src/commands.js +297 -0
- package/src/help.js +42 -0
- package/src/mesh.js +224 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# 排障手册
|
|
2
|
+
|
|
3
|
+
按「你看到的原文」查。每条都附上验证命令。
|
|
4
|
+
|
|
5
|
+
## 启动类
|
|
6
|
+
|
|
7
|
+
### `session "backend" exited immediately`
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
error: session "backend" exited immediately
|
|
11
|
+
[backend] ...(pane 最后几行输出)
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
`pp up` 在创建会话 600ms 后会检查 pane 是否还活着,死了就把截到的输出打出来。常见原因依次排查:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pp up backend -v --dry-run # ① 打印真实 argv,确认 agent/参数/目录
|
|
18
|
+
pp check backend # ② cwd 是否存在、agent 是否在 PATH
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
- **agent 不在 PATH 上**:systemd/非交互 shell 的 PATH 很短。用绝对路径:`agent: /home/you/.nvm/versions/node/v26.13.1/bin/pi`
|
|
22
|
+
- **pi 参数不合法**:例如 `pi_session` 里有大写以外非法字符,或 `pi_session_dir` 指向不可写的目录。pi 会直接退出
|
|
23
|
+
- **cwd 不存在**:`cwd does not exist: /path`
|
|
24
|
+
- 想看到完整报错:临时加 `remain_on_exit: true`,然后 `pp logs backend -n 200`
|
|
25
|
+
|
|
26
|
+
### `cwd does not exist: /home/you/code/backend`
|
|
27
|
+
|
|
28
|
+
配置文件里的目录在这台机器上不存在(典型:`pp init` 生成的模板)。改 `cwd` 或先 `mkdir -p`。
|
|
29
|
+
|
|
30
|
+
### `directory does not exist: /tmp/x`(`pp new -C`)
|
|
31
|
+
|
|
32
|
+
`-C` 只接受已存在的目录,不会替你创建。
|
|
33
|
+
|
|
34
|
+
### `error: unknown session "9" (7 session(s) configured; see pp ls)`
|
|
35
|
+
|
|
36
|
+
序号从 1 开始,且只按**配置文件里的顺序**,不是 `pp ls` 筛过之后的顺序(筛过时请用名字)。
|
|
37
|
+
|
|
38
|
+
### `tmux session "api" already exists (--force to recreate, or pick another name)`
|
|
39
|
+
|
|
40
|
+
`pp new api` 撞名。`--force` 重建,或换个名字。
|
|
41
|
+
|
|
42
|
+
## tmux 类
|
|
43
|
+
|
|
44
|
+
### `tmux ... can't find pane: =backend`
|
|
45
|
+
|
|
46
|
+
0.2 之前的 bug:`send-keys` / `capture-pane` 的目标必须是 `=name:`(带冒号),而 `has-session` / `kill-session` 用 `=name`。升级即可。自查:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
tmux list-panes -a -F '#{session_name}:#{pane_id}' | head
|
|
50
|
+
tmux send-keys -t '=backend:' 'echo ok' Enter # 冒号不能少
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### `no server running on /tmp/tmux-1000/pi-plus`
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pp ls # 空表 = 确实没有会话
|
|
57
|
+
pp doctor # tmux server 那行会写 not running (starts on demand)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
用了 `--socket pi-plus` 时,你的 shell 里 `tmux ls` 看不到这些会话——需要 `tmux -L pi-plus ls`。
|
|
61
|
+
|
|
62
|
+
### 会话在 `tmux ls` 里有,但 `pp ls` 显示 `off`
|
|
63
|
+
|
|
64
|
+
两边看的不是同一个 socket。检查配置里的 `tmux.socket` 和你 `tmux ls` 用的 socket 是否一致;另外自启单元不会带命令行 `--socket`(只认配置文件里的 `tmux.socket`)。
|
|
65
|
+
|
|
66
|
+
### `STATE=dead`
|
|
67
|
+
|
|
68
|
+
pane 里的进程退出了,`remain-on-exit` 把 pane 留在那儿。看怎么回事:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
pp logs backend -n 200
|
|
72
|
+
pp up backend # 重建(restart: no 的会话必须点名)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## 名字 / remote-pi 类
|
|
76
|
+
|
|
77
|
+
### mesh 里看到的不是我配的名字
|
|
78
|
+
|
|
79
|
+
1. `cat <cwd>/.pi/remote-pi/config.json` 看 `agent_name`
|
|
80
|
+
2. 同一目录两个会话要求不同名字 → 必须 `remote_pi_mode: env`(文件只有一个 `agent_name`,`config` 模式最后一次写赢)
|
|
81
|
+
3. broker 侧同名冲突会临时显示 `name#2`,不会写回配置
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
pp check # 共享 cwd + config 模式会给出警告
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### 想彻底不进 mesh
|
|
88
|
+
|
|
89
|
+
```yaml
|
|
90
|
+
remote_pi_mode: off
|
|
91
|
+
# 或
|
|
92
|
+
remote_pi_name: false
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
`pp new` 加 `--no-remote-pi`。
|
|
96
|
+
|
|
97
|
+
### pi 里 `/session` 看到的名字不是我想的
|
|
98
|
+
|
|
99
|
+
那是 `pi_session_name`(`--name`),不是 tmux 名,也不是 remote-pi 名。看 [configuration.md](configuration.md#名字映射速查)。
|
|
100
|
+
|
|
101
|
+
## 自启类
|
|
102
|
+
|
|
103
|
+
### 重启后一个会话都没有,但 `systemctl --user is-enabled pi-plus` 说 enabled
|
|
104
|
+
|
|
105
|
+
三种可能,按概率:
|
|
106
|
+
|
|
107
|
+
1. **没开 linger**(服务器/无图形登录):`sudo loginctl enable-linger "$USER"`
|
|
108
|
+
2. **WSL2**:发行版要有人调 `wsl.exe` 才会起来的,Windows 侧必须配触发(见 [autostart.md](autostart.md#wsl2只有里面装是不够的))
|
|
109
|
+
3. **单元里没有 `KillMode=none`**:现象是 `Result=success` 但 tmux server 被 systemd 连坐杀掉了。老版本升级后请重装单元:`pp uninstall && pp install`
|
|
110
|
+
|
|
111
|
+
验证是不是第 3 种:
|
|
112
|
+
|
|
113
|
+
```bash
|
|
114
|
+
systemctl --user show pi-plus -p Result -p ExecMainStatus # success
|
|
115
|
+
tmux ls # 空的 → 就是它
|
|
116
|
+
grep KillMode ~/.config/systemd/user/pi-plus.service # 应该 =none
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### 会话起来但立刻又被杀
|
|
120
|
+
|
|
121
|
+
`pp install` 之后又手动 `systemctl --user restart pi-plus.service`:oneshot 每次执行都会跑 `up --reconcile`,如果配置文件里某会话已 dead 且 `restart: always`,会被重建——这是预期行为,不是杀。
|
|
122
|
+
|
|
123
|
+
### 手动 `down` 的会话,巡检又起来了
|
|
124
|
+
|
|
125
|
+
只有 `always` 会。`unless-stopped` / `on-failure` 的手动停止记录在 `~/.local/state/pi-plus/state.json`:
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
cat ~/.local/state/pi-plus/state.json
|
|
129
|
+
pp up backend # 点名启动会清掉这条记录,重新纳入巡检
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## attach 类
|
|
133
|
+
|
|
134
|
+
### 从脚本 / agent 里 `pp attach` 输出乱码或卡住
|
|
135
|
+
|
|
136
|
+
`attach` 需要真正的终端(pty + termios)。非 TTY 直接拒绝,用 `script`、`expect`、管道都不行——你会得到 cooked mode,方向键变成 `ESC [ A` 之类的字面量。
|
|
137
|
+
|
|
138
|
+
- 本机:在真终端里跑 `pp attach backend`
|
|
139
|
+
- 远程:`ssh -t host 'pp attach backend'`(`-t` 必须有)
|
|
140
|
+
- 只想看输出:`pp logs backend -n 500` 或 `tail -f ~/.pi/plus/logs/backend.log`
|
|
141
|
+
|
|
142
|
+
## 发布 / 安装类
|
|
143
|
+
|
|
144
|
+
### `npm i -g pi-farm` 装到的是别的东西或 404
|
|
145
|
+
|
|
146
|
+
内网镜像 `npm config get registry` 指向 Nexus 时拿不到公网包:
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
npm i -g pi-farm --registry https://registry.npmjs.org/
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### `pp: command not found`
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
npm bin -g 2>/dev/null || echo "$(npm root -g)/../bin"
|
|
156
|
+
echo "$PATH" | tr ':' '\n' | grep -i node
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
nvm 的全局 bin 通常在 `~/.nvm/versions/node/vX/bin`,装完要重开 shell 或 `hash -r`。
|
|
160
|
+
|
|
161
|
+
### `pp -V` 的版本和 GitHub 上不一致
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
which -a pp # 可能还有旧的 ~/.local/bin/pp 符号链接
|
|
165
|
+
npm ls -g --depth=0 | grep pi-farm
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## 配置类
|
|
169
|
+
|
|
170
|
+
### `unknown key "pi_sesion" (ignored)` 之类的警告
|
|
171
|
+
|
|
172
|
+
拼写错误被忽略而不是报错(故意这么设计,免得升级时新增字段导致旧配置无法解析)。`pp check` 会列出所有警告。
|
|
173
|
+
|
|
174
|
+
### `config file not found`
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
pp check # 会打印实际搜索过的路径
|
|
178
|
+
PI_PLUS_CONFIG=/path/to/plus.yml pp ls
|
|
179
|
+
pp init # 写一份模板到 ~/.pi/plus.yml
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### 想确认某个字段到底被解析成了什么
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
pp check --json | jq '.sessions[]? // .' # 或
|
|
186
|
+
pp up backend --dry-run -v # 打印最终 argv + env
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
`--dry-run` 会显示形如:
|
|
190
|
+
|
|
191
|
+
```
|
|
192
|
+
[backend] cwd=/home/you/code/backend tmux=backend pi-session=backend remote-pi=backend(mode=config)
|
|
193
|
+
[dry-run] tmux new-session -d -s backend -c /home/you/code/backend -- env ... pi --name backend
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## 还是不行
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
pp doctor --json | jq
|
|
200
|
+
pp check --json | jq
|
|
201
|
+
pp up <name> -v --dry-run
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
把这四条的输出贴到 issue(记得先脱敏路径)。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-farm",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Declarative tmux sessions for pi coding agents (CLI: pp). One YAML drives the tmux session, pi session id/name and remote-pi mesh name, and survives reboots.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"src",
|
|
12
12
|
"examples",
|
|
13
13
|
"completions",
|
|
14
|
+
"docs",
|
|
14
15
|
"README.md",
|
|
15
16
|
"LICENSE"
|
|
16
17
|
],
|
package/src/autostart.js
CHANGED
|
@@ -229,7 +229,13 @@ export function installAutostart({ scriptPath, configPath, watch = false, print
|
|
|
229
229
|
if (timer) {
|
|
230
230
|
run("systemctl", ["--user", "enable", "--now", `${SERVICE_NAME}.timer`], { allowFailure: true });
|
|
231
231
|
} else {
|
|
232
|
-
|
|
232
|
+
// `restart` (not `start`): the oneshot stays "active (exited)" after a
|
|
233
|
+
// run, so `start` would be a no-op when installing over an old unit —
|
|
234
|
+
// which is exactly the "I ran `pp down`, then `pp install`" case.
|
|
235
|
+
const restarted = run("systemctl", ["--user", "restart", `${SERVICE_NAME}.service`], { allowFailure: true });
|
|
236
|
+
if (restarted.status !== 0) {
|
|
237
|
+
run("systemctl", ["--user", "start", `${SERVICE_NAME}.service`], { allowFailure: true });
|
|
238
|
+
}
|
|
233
239
|
}
|
|
234
240
|
run("loginctl", ["enable-linger", process.env.USER || ""], { allowFailure: true });
|
|
235
241
|
}
|
package/src/commands.js
CHANGED
|
@@ -25,6 +25,7 @@ import {
|
|
|
25
25
|
systemdUserDir,
|
|
26
26
|
} from "./autostart.js";
|
|
27
27
|
import { clearStopped, isStopped, markStopped } from "./state.js";
|
|
28
|
+
import { BROADCAST_TARGET, connectPeer, livePeersFor, MeshError, peerPc, probePeers, sessionMeshAddress } from "./mesh.js";
|
|
28
29
|
import { findCommand, suggestCommand } from "./help.js";
|
|
29
30
|
import { EXAMPLE_CONFIG } from "./example.js";
|
|
30
31
|
|
|
@@ -544,6 +545,293 @@ function cmdSend(config, options, ui) {
|
|
|
544
545
|
ui.info(`sent to ${session.name}`);
|
|
545
546
|
}
|
|
546
547
|
|
|
548
|
+
function sessionForAddress(config, address) {
|
|
549
|
+
if (!config) return null;
|
|
550
|
+
for (const session of config.sessions) {
|
|
551
|
+
const base = sessionMeshAddress(session);
|
|
552
|
+
if (base && (address === base || address.startsWith(`${base}#`))) return session;
|
|
553
|
+
}
|
|
554
|
+
return null;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
function meshUsage(ui) {
|
|
558
|
+
ui.error('usage: pp mesh <ls|send|resolve> ... (see "pp help mesh")');
|
|
559
|
+
process.exit(2);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
async function cmdMesh(config, options, ui) {
|
|
563
|
+
const [sub, ...rest] = options.names;
|
|
564
|
+
switch (sub) {
|
|
565
|
+
case undefined:
|
|
566
|
+
case "ls":
|
|
567
|
+
case "list":
|
|
568
|
+
case "peers":
|
|
569
|
+
return cmdMeshLs(config, options, ui);
|
|
570
|
+
case "send":
|
|
571
|
+
case "msg":
|
|
572
|
+
return cmdMeshSend(config, { ...options, names: rest }, ui);
|
|
573
|
+
case "resolve":
|
|
574
|
+
case "addr":
|
|
575
|
+
return cmdMeshResolve(config, { ...options, names: rest }, ui);
|
|
576
|
+
default:
|
|
577
|
+
ui.error(`unknown mesh subcommand: ${sub}`);
|
|
578
|
+
return meshUsage(ui);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
async function cmdMeshLs(config, options, ui) {
|
|
583
|
+
let peers;
|
|
584
|
+
try {
|
|
585
|
+
peers = await probePeers({ timeoutMs: options.wait ?? 3000 });
|
|
586
|
+
} catch (err) {
|
|
587
|
+
ui.error(err.message);
|
|
588
|
+
process.exit(1);
|
|
589
|
+
}
|
|
590
|
+
const rows = peers.map((address, index) => {
|
|
591
|
+
const session = sessionForAddress(config, address);
|
|
592
|
+
return {
|
|
593
|
+
index: index + 1,
|
|
594
|
+
address,
|
|
595
|
+
pc: peerPc(address),
|
|
596
|
+
session: session ? session.name : null,
|
|
597
|
+
};
|
|
598
|
+
});
|
|
599
|
+
if (ui.json) {
|
|
600
|
+
ui.out(JSON.stringify(rows, null, 2));
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
if (rows.length === 0) {
|
|
604
|
+
ui.info("no peers in the mesh");
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
const headers = ["#", "ADDRESS", "PC", "SESSION"];
|
|
608
|
+
const table = rows.map((r) => [String(r.index), r.address, r.pc ?? "-", r.session ?? "-"]);
|
|
609
|
+
const widths = headers.map((h, i) => Math.max(h.length, ...table.map((row) => row[i].length)));
|
|
610
|
+
const pad = (value, width) => value + " ".repeat(Math.max(0, width - value.length));
|
|
611
|
+
ui.out(ui.paint("bold", headers.map((h, i) => pad(h, widths[i])).join(" ").trimEnd()));
|
|
612
|
+
table.forEach((row, i) => {
|
|
613
|
+
const cells = [...row];
|
|
614
|
+
cells[0] = ui.paint("dim", cells[0]);
|
|
615
|
+
if (rows[i].session) cells[3] = ui.paint("green", cells[3]);
|
|
616
|
+
ui.out(cells.map((cell, idx) => pad(cell, widths[idx])).join(" ").trimEnd());
|
|
617
|
+
});
|
|
618
|
+
if (!options.quiet) {
|
|
619
|
+
const local = rows.filter((r) => !r.pc).length;
|
|
620
|
+
ui.out(`\n${rows.length} peer(s): ${local} local, ${rows.length - local} cross-PC`);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
async function cmdMeshResolve(config, options, ui) {
|
|
625
|
+
if (!config) {
|
|
626
|
+
ui.error("no config found; cannot resolve session names (use a raw mesh address instead)");
|
|
627
|
+
process.exit(2);
|
|
628
|
+
}
|
|
629
|
+
const sessions = selectSessions(config, options.names, ui, { all: true });
|
|
630
|
+
let peers = [];
|
|
631
|
+
try {
|
|
632
|
+
peers = await probePeers({ timeoutMs: options.wait ?? 3000 });
|
|
633
|
+
} catch {
|
|
634
|
+
peers = []; // resolving is useful offline; just report the offline state
|
|
635
|
+
}
|
|
636
|
+
const rows = sessions.map((session) => {
|
|
637
|
+
const base = sessionMeshAddress(session);
|
|
638
|
+
const live = livePeersFor(peers, base);
|
|
639
|
+
return {
|
|
640
|
+
name: session.name,
|
|
641
|
+
address: base,
|
|
642
|
+
online: live.length > 0,
|
|
643
|
+
matches: live,
|
|
644
|
+
};
|
|
645
|
+
});
|
|
646
|
+
if (ui.json) {
|
|
647
|
+
ui.out(JSON.stringify(rows, null, 2));
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
for (const row of rows) {
|
|
651
|
+
if (!row.address) {
|
|
652
|
+
ui.out(`${row.name}: no remote-pi name (remote_pi_mode: off?)`);
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
const state = row.online ? ui.paint("green", `online x${row.matches.length}`) : ui.paint("dim", "offline");
|
|
656
|
+
ui.out(`${row.name} → ${row.address} ${state}`);
|
|
657
|
+
if (row.matches.length > 1) {
|
|
658
|
+
for (const match of row.matches) ui.out(` ${ui.paint("dim", match)}`);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
async function cmdMeshSend(config, options, ui) {
|
|
664
|
+
const hasBodyJson = options.bodyJson !== undefined;
|
|
665
|
+
if (options.names.length < (hasBodyJson ? 1 : 2)) {
|
|
666
|
+
ui.error('usage: pp mesh send <target...> "text" [--wait <ms>] [--reply <ms>] [--no-wait] [--body-json <json>]');
|
|
667
|
+
process.exit(2);
|
|
668
|
+
}
|
|
669
|
+
const text = hasBodyJson ? null : options.names[options.names.length - 1];
|
|
670
|
+
const rawTargets = hasBodyJson ? options.names : options.names.slice(0, -1);
|
|
671
|
+
let body = text;
|
|
672
|
+
if (hasBodyJson) {
|
|
673
|
+
try {
|
|
674
|
+
body = JSON.parse(options.bodyJson);
|
|
675
|
+
} catch (err) {
|
|
676
|
+
ui.error(`--body-json is not valid JSON: ${err.message}`);
|
|
677
|
+
process.exit(2);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
// Resolve names/config first: then "unknown session" and a missing config are
|
|
682
|
+
// reported before we bother the broker.
|
|
683
|
+
const wanted = [];
|
|
684
|
+
let broadcast = false;
|
|
685
|
+
for (const raw of rawTargets) {
|
|
686
|
+
if (raw === "all" || raw === "broadcast" || raw === "*") {
|
|
687
|
+
broadcast = true;
|
|
688
|
+
continue;
|
|
689
|
+
}
|
|
690
|
+
if (raw.includes("@") || /^[A-Za-z0-9._-]+:\//.test(raw)) {
|
|
691
|
+
wanted.push({ kind: "address", value: raw });
|
|
692
|
+
continue;
|
|
693
|
+
}
|
|
694
|
+
if (!config) {
|
|
695
|
+
ui.error(`no config found; cannot resolve session "${raw}" (pass a full mesh address instead)`);
|
|
696
|
+
process.exit(2);
|
|
697
|
+
}
|
|
698
|
+
const [session] = selectSessions(config, [raw], ui);
|
|
699
|
+
const base = sessionMeshAddress(session);
|
|
700
|
+
if (!base) {
|
|
701
|
+
ui.error(`"${session.name}" has no remote-pi name (remote_pi_mode: off?)`);
|
|
702
|
+
process.exit(2);
|
|
703
|
+
}
|
|
704
|
+
wanted.push({ kind: "session", value: base, session });
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
let peers = [];
|
|
708
|
+
if (!options.dryRun || wanted.length > 0 || broadcast) {
|
|
709
|
+
try {
|
|
710
|
+
peers = await probePeers({ timeoutMs: options.wait ?? 3000 });
|
|
711
|
+
} catch (err) {
|
|
712
|
+
if (!(err instanceof MeshError) || err.code !== "no_broker") throw err;
|
|
713
|
+
ui.error(err.message);
|
|
714
|
+
process.exit(1);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
const targets = [];
|
|
719
|
+
for (const item of wanted) {
|
|
720
|
+
const live = livePeersFor(peers, item.value);
|
|
721
|
+
if (live.length === 0) {
|
|
722
|
+
if (item.kind === "address") ui.error(`no peer online at ${item.value}`);
|
|
723
|
+
else ui.error(`"${item.session.name}" is not in the mesh yet (${item.value} is offline; is the session running?)`);
|
|
724
|
+
process.exit(1);
|
|
725
|
+
}
|
|
726
|
+
targets.push(...live);
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
const unique = [...new Set(targets)];
|
|
730
|
+
const multicast = broadcast || unique.length > 1;
|
|
731
|
+
if (!broadcast && unique.length === 0) {
|
|
732
|
+
ui.error("no targets");
|
|
733
|
+
process.exit(2);
|
|
734
|
+
}
|
|
735
|
+
if (options.dryRun) {
|
|
736
|
+
const envelope = { from: options.as ?? "pp-mesh", to: broadcast ? BROADCAST_TARGET : multicast ? unique : unique[0], body };
|
|
737
|
+
ui.info(`[dry-run] ${JSON.stringify(envelope)}`);
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
let peer;
|
|
742
|
+
try {
|
|
743
|
+
peer = await connectPeer({ name: options.as ?? "pp-mesh", cwd: process.cwd() });
|
|
744
|
+
} catch (err) {
|
|
745
|
+
ui.error(err.message);
|
|
746
|
+
process.exit(1);
|
|
747
|
+
}
|
|
748
|
+
let sendId = null;
|
|
749
|
+
let waitingForFirstReply = null;
|
|
750
|
+
const replies = [];
|
|
751
|
+
const collecting = options.reply !== undefined && options.reply > 0;
|
|
752
|
+
if (collecting) {
|
|
753
|
+
peer.onMessage((envelope) => {
|
|
754
|
+
if (!envelope || envelope.re !== sendId) return;
|
|
755
|
+
replies.push(envelope);
|
|
756
|
+
if (!ui.json) {
|
|
757
|
+
ui.out(`${ui.paint("green", `reply from ${envelope.from}`)}: ${formatMeshBody(envelope.body)}`);
|
|
758
|
+
}
|
|
759
|
+
waitingForFirstReply?.();
|
|
760
|
+
waitingForFirstReply = null;
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
const collect = async (single = true) => {
|
|
764
|
+
if (!collecting) return;
|
|
765
|
+
await new Promise((resolve) => {
|
|
766
|
+
const timer = setTimeout(resolve, options.reply);
|
|
767
|
+
if (!single) return;
|
|
768
|
+
waitingForFirstReply = () => {
|
|
769
|
+
clearTimeout(timer);
|
|
770
|
+
resolve();
|
|
771
|
+
};
|
|
772
|
+
});
|
|
773
|
+
waitingForFirstReply = null;
|
|
774
|
+
if (ui.json) {
|
|
775
|
+
ui.out(JSON.stringify(replies, null, 2));
|
|
776
|
+
return replies.length;
|
|
777
|
+
}
|
|
778
|
+
if (replies.length === 0) ui.info(`no reply within ${options.reply}ms (the peer may still be working)`);
|
|
779
|
+
return replies.length;
|
|
780
|
+
};
|
|
781
|
+
try {
|
|
782
|
+
if (broadcast) {
|
|
783
|
+
sendId = peer.send(BROADCAST_TARGET, body);
|
|
784
|
+
ui.info(`broadcast to ${peers.length} peer(s) as ${peer.address} (${sendId})`);
|
|
785
|
+
await collect(false);
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
if (multicast) {
|
|
789
|
+
sendId = peer.send(unique, body);
|
|
790
|
+
ui.info(`sent to ${unique.length} peer(s) as ${peer.address} (${sendId})`);
|
|
791
|
+
for (const target of unique) ui.out(` ${ui.paint("dim", target)}`);
|
|
792
|
+
await collect(false);
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
795
|
+
const target = unique[0];
|
|
796
|
+
if (options.noWait) {
|
|
797
|
+
sendId = peer.send(target, body);
|
|
798
|
+
ui.info(`sent to ${target} (${sendId})`);
|
|
799
|
+
await collect();
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
const ack = await peer.sendWithAck(target, body, options.wait ?? 5000);
|
|
803
|
+
sendId = ack.id;
|
|
804
|
+
if (ack.status === "received") {
|
|
805
|
+
ui.info(`delivered to ${target} (${ack.id})`);
|
|
806
|
+
await collect();
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
if (ack.status === "busy") {
|
|
810
|
+
ui.error(`${target} is mid-turn: broker dropped the message (busy). Retry later or use --no-wait.`);
|
|
811
|
+
process.exitCode = 1;
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
if (ack.status === "denied") {
|
|
815
|
+
ui.error(`delivery to ${target} was denied by the broker`);
|
|
816
|
+
process.exitCode = 1;
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
ui.error(`no ACK from ${target} within ${options.wait ?? 5000}ms (the message may still arrive)`);
|
|
820
|
+
process.exitCode = 1;
|
|
821
|
+
} finally {
|
|
822
|
+
peer.close();
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
function formatMeshBody(body) {
|
|
827
|
+
if (typeof body === "string") return body;
|
|
828
|
+
try {
|
|
829
|
+
return JSON.stringify(body);
|
|
830
|
+
} catch {
|
|
831
|
+
return String(body);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
|
|
547
835
|
function cmdCheck(config, options, ui) {
|
|
548
836
|
const tmux = makeTmux(config, options);
|
|
549
837
|
const problems = [];
|
|
@@ -789,6 +1077,15 @@ export async function runCommand(command, options) {
|
|
|
789
1077
|
const config = requireConfig(options, ui);
|
|
790
1078
|
return cmdSend(config, options, ui);
|
|
791
1079
|
}
|
|
1080
|
+
case "mesh": {
|
|
1081
|
+
let config = null;
|
|
1082
|
+
try {
|
|
1083
|
+
config = loadConfig({ configPath: options.config, cwd: options.cwd });
|
|
1084
|
+
} catch {
|
|
1085
|
+
config = null; // raw addresses and `pp mesh ls` work without a config
|
|
1086
|
+
}
|
|
1087
|
+
return cmdMesh(config, options, ui);
|
|
1088
|
+
}
|
|
792
1089
|
case "check":
|
|
793
1090
|
case "validate": {
|
|
794
1091
|
const config = requireConfig(options, ui);
|
package/src/help.js
CHANGED
|
@@ -154,6 +154,48 @@ export const COMMANDS = [
|
|
|
154
154
|
examples: ['pp send backend "run the tests"', 'pp send 3 "yes" --no-enter'],
|
|
155
155
|
see: ["logs", "attach"],
|
|
156
156
|
},
|
|
157
|
+
{
|
|
158
|
+
name: "mesh",
|
|
159
|
+
aliases: [],
|
|
160
|
+
summary: "Talk to the remote-pi agent mesh (ls / send / resolve)",
|
|
161
|
+
usage: "pp mesh <ls|send|resolve> ...",
|
|
162
|
+
details: [
|
|
163
|
+
"pp talks to remote-pi's local broker directly over its UNIX socket",
|
|
164
|
+
"(~/.pi/remote/sessions/local/broker.sock) — no dependencies, no pi needed.",
|
|
165
|
+
"",
|
|
166
|
+
" ls list every peer in the mesh (read-only probe: it never registers,",
|
|
167
|
+
" so nobody sees a peer_joined/peer_left because of it)",
|
|
168
|
+
" resolve map a configured session to its mesh address and online state",
|
|
169
|
+
" send register as an ephemeral peer (default name pp-mesh), deliver the",
|
|
170
|
+
" text, and wait for the broker ACK",
|
|
171
|
+
"",
|
|
172
|
+
"Targets may be a session name or #index (resolved through the config), a raw",
|
|
173
|
+
"mesh address (<cwd>@<name>, or macbook:/path@name for cross-PC), `all`, or",
|
|
174
|
+
"`broadcast`. One target = unicast with ACK (received/busy/denied/timeout);",
|
|
175
|
+
"several targets or `all` = multicast, fire-and-forget.",
|
|
176
|
+
"",
|
|
177
|
+
"A busy peer drops the message (broker ACK `busy`) — retry later or --no-wait.",
|
|
178
|
+
"Cross-PC delivery rides the broker leader's relay bridge, so no pairing.",
|
|
179
|
+
].join("\n"),
|
|
180
|
+
options: [
|
|
181
|
+
"--json JSON output (ls/resolve)",
|
|
182
|
+
"--as <name> mesh name to register as (default: pp-mesh)",
|
|
183
|
+
"--wait <ms> ACK timeout, default 5000",
|
|
184
|
+
"--no-wait fire-and-forget: do not wait for the ACK",
|
|
185
|
+
"--reply <ms> keep listening after the ACK and print the reply envelope(s)",
|
|
186
|
+
"--body-json <json> send structured JSON instead of plain text",
|
|
187
|
+
"--dry-run print the envelope instead of sending it",
|
|
188
|
+
],
|
|
189
|
+
examples: [
|
|
190
|
+
"pp mesh ls",
|
|
191
|
+
"pp mesh resolve backend",
|
|
192
|
+
'pp mesh send backend "the build is green"',
|
|
193
|
+
'pp mesh send 1 3 "heads up: rebooting the box"',
|
|
194
|
+
'pp mesh send all "standup in 5"',
|
|
195
|
+
"pp mesh send macbook:/Users/me/code/api@api \"ping\" --wait 2000",
|
|
196
|
+
],
|
|
197
|
+
see: ["send", "status"],
|
|
198
|
+
},
|
|
157
199
|
{
|
|
158
200
|
name: "check",
|
|
159
201
|
aliases: ["validate"],
|