teamsmith 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 +21 -0
- package/README.md +158 -2
- package/bin/team.mjs +59 -0
- package/install.sh +94 -0
- package/package.json +37 -4
- package/skills/teamsmith/CHANGELOG.md +763 -0
- package/skills/teamsmith/SKILL.md +312 -0
- package/skills/teamsmith/extension/team-bg.ts +435 -0
- package/skills/teamsmith/extension/team-inbox-watch.ts +1412 -0
- package/skills/teamsmith/extension/team-notify.ts +663 -0
- package/skills/teamsmith/references/agent-adapters.md +643 -0
- package/skills/teamsmith/references/config.md +477 -0
- package/skills/teamsmith/references/meeting.md +124 -0
- package/skills/teamsmith/references/memory.md +133 -0
- package/skills/teamsmith/references/migration.md +227 -0
- package/skills/teamsmith/references/openspec.md +222 -0
- package/skills/teamsmith/references/philosophy.md +95 -0
- package/skills/teamsmith/references/protocol.md +712 -0
- package/skills/teamsmith/references/troubleshooting.md +1384 -0
- package/skills/teamsmith/references/workflows.md +448 -0
- package/skills/teamsmith/scripts/lib/cmd-agents.sh +1709 -0
- package/skills/teamsmith/scripts/lib/cmd-bg.sh +284 -0
- package/skills/teamsmith/scripts/lib/cmd-bootstrap.sh +311 -0
- package/skills/teamsmith/scripts/lib/cmd-config.sh +1156 -0
- package/skills/teamsmith/scripts/lib/cmd-death.sh +562 -0
- package/skills/teamsmith/scripts/lib/cmd-docs.sh +149 -0
- package/skills/teamsmith/scripts/lib/cmd-draft.sh +124 -0
- package/skills/teamsmith/scripts/lib/cmd-init.sh +212 -0
- package/skills/teamsmith/scripts/lib/cmd-meeting.sh +904 -0
- package/skills/teamsmith/scripts/lib/cmd-outbox.sh +196 -0
- package/skills/teamsmith/scripts/lib/cmd-project.sh +642 -0
- package/skills/teamsmith/scripts/lib/cmd-review.sh +1393 -0
- package/skills/teamsmith/scripts/lib/cmd-status.sh +1399 -0
- package/skills/teamsmith/scripts/lib/cmd-update.sh +191 -0
- package/skills/teamsmith/scripts/lib/cmd-watch.sh +1681 -0
- package/skills/teamsmith/scripts/lib/common.sh +4964 -0
- package/skills/teamsmith/scripts/lib/draft-entry.sh +51 -0
- package/skills/teamsmith/scripts/lib/outbox.sh +2042 -0
- package/skills/teamsmith/scripts/lib/tmux-argv.sh +165 -0
- package/skills/teamsmith/scripts/monitor.mjs +616 -0
- package/skills/teamsmith/scripts/panel/README.md +192 -0
- package/skills/teamsmith/scripts/panel/build.sh +47 -0
- package/skills/teamsmith/scripts/panel/bun.lock +109 -0
- package/skills/teamsmith/scripts/panel/draft-send.sh +68 -0
- package/skills/teamsmith/scripts/panel/package.json +24 -0
- package/skills/teamsmith/scripts/panel/panel.js +388 -0
- package/skills/teamsmith/scripts/panel/src/App.tsx +2150 -0
- package/skills/teamsmith/scripts/panel/src/clipboard.ts +154 -0
- package/skills/teamsmith/scripts/panel/src/compose.ts +691 -0
- package/skills/teamsmith/scripts/panel/src/data.ts +555 -0
- package/skills/teamsmith/scripts/panel/src/format.ts +50 -0
- package/skills/teamsmith/scripts/panel/src/layout.ts +2562 -0
- package/skills/teamsmith/scripts/panel/src/main.tsx +894 -0
- package/skills/teamsmith/scripts/panel/src/markdown.ts +340 -0
- package/skills/teamsmith/scripts/panel/src/sanitize.ts +42 -0
- package/skills/teamsmith/scripts/panel/src/settings.ts +154 -0
- package/skills/teamsmith/scripts/panel/src/strings/en.ts +416 -0
- package/skills/teamsmith/scripts/panel/src/strings/index.ts +65 -0
- package/skills/teamsmith/scripts/panel/src/strings/types.ts +9 -0
- package/skills/teamsmith/scripts/panel/src/strings/zh.ts +414 -0
- package/skills/teamsmith/scripts/panel/src/theme.ts +116 -0
- package/skills/teamsmith/scripts/panel/src/types.ts +652 -0
- package/skills/teamsmith/scripts/panel/src/width.ts +105 -0
- package/skills/teamsmith/scripts/panel/tsconfig.json +16 -0
- package/skills/teamsmith/scripts/shim/signal-gate +156 -0
- package/skills/teamsmith/scripts/shim/tmux +403 -0
- package/skills/teamsmith/scripts/team +143 -0
- package/skills/teamsmith/templates/AGENTS.section.md.tmpl +147 -0
- package/skills/teamsmith/templates/BOARD.md.tmpl +18 -0
- package/skills/teamsmith/templates/DECISIONS.md.tmpl +22 -0
- package/skills/teamsmith/templates/OWNERSHIP.md.tmpl +25 -0
- package/skills/teamsmith/templates/PROTOCOL.md.tmpl +84 -0
- package/skills/teamsmith/templates/ROADMAP.md.tmpl +29 -0
- package/skills/teamsmith/templates/config.sh.tmpl +106 -0
- package/skills/teamsmith/templates/memory-seed.md.tmpl +46 -0
- package/skills/teamsmith/templates/pm-prompt.md.tmpl +110 -0
- package/skills/teamsmith/templates/report.md.tmpl +36 -0
- package/skills/teamsmith/templates/task.md.tmpl +65 -0
- package/skills/teamsmith/templates/threads-README.md +17 -0
- package/skills/teamsmith/tests/bg-ledger-harness.mjs +682 -0
- package/skills/teamsmith/tests/bg-ledger.sh +307 -0
- package/skills/teamsmith/tests/checkout-shape-probe.sh +800 -0
- package/skills/teamsmith/tests/config-cli.sh +1562 -0
- package/skills/teamsmith/tests/container-tmux.sh +493 -0
- package/skills/teamsmith/tests/coverage-inventory.sh +173 -0
- package/skills/teamsmith/tests/death-cause.sh +564 -0
- package/skills/teamsmith/tests/delivery-truth.sh +1231 -0
- package/skills/teamsmith/tests/fake-tui.py +450 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/README.md +60 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/judge-second.py +139 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/mock-server.py +29 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/observe.ts +20 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/run-case.sh +79 -0
- package/skills/teamsmith/tests/fixtures/delivery-truth-real/scenario.sh +175 -0
- package/skills/teamsmith/tests/fixtures/p55/flip-p49.sh +229 -0
- package/skills/teamsmith/tests/fixtures/p71-incident/README.md +22 -0
- package/skills/teamsmith/tests/fixtures/p71-incident/dev2.seen +8 -0
- package/skills/teamsmith/tests/fixtures/p71-incident/dev2.wake +9 -0
- package/skills/teamsmith/tests/fixtures/p71-incident/pm-nudges.wake +3 -0
- package/skills/teamsmith/tests/fixtures/signal-lint-cases.txt +40 -0
- package/skills/teamsmith/tests/flip-glue.sh +162 -0
- package/skills/teamsmith/tests/flip-m12-26c.sh +129 -0
- package/skills/teamsmith/tests/flip-m12.sh +185 -0
- package/skills/teamsmith/tests/flip-m16.sh +166 -0
- package/skills/teamsmith/tests/flip-m17.sh +204 -0
- package/skills/teamsmith/tests/flip-m33.sh +284 -0
- package/skills/teamsmith/tests/flip-m34.sh +96 -0
- package/skills/teamsmith/tests/flip-m37.sh +185 -0
- package/skills/teamsmith/tests/flip-m4.3.sh +375 -0
- package/skills/teamsmith/tests/flip-m43.sh +128 -0
- package/skills/teamsmith/tests/flip-m44.sh +262 -0
- package/skills/teamsmith/tests/flip-m45.sh +244 -0
- package/skills/teamsmith/tests/flip-m46.sh +238 -0
- package/skills/teamsmith/tests/flip-m48.sh +304 -0
- package/skills/teamsmith/tests/flip-m6.2.sh +205 -0
- package/skills/teamsmith/tests/flip-m6.3.sh +283 -0
- package/skills/teamsmith/tests/flip-m6.4.sh +267 -0
- package/skills/teamsmith/tests/flip-m6.5.sh +113 -0
- package/skills/teamsmith/tests/flip-m7.1.sh +135 -0
- package/skills/teamsmith/tests/flip-m7.2.sh +321 -0
- package/skills/teamsmith/tests/flip-p144.sh +112 -0
- package/skills/teamsmith/tests/flip-p156.sh +133 -0
- package/skills/teamsmith/tests/flip-p158.sh +301 -0
- package/skills/teamsmith/tests/flip-p160.sh +182 -0
- package/skills/teamsmith/tests/flip-p171.sh +249 -0
- package/skills/teamsmith/tests/flip-p18.1.sh +95 -0
- package/skills/teamsmith/tests/flip-p20.sh +286 -0
- package/skills/teamsmith/tests/flip-p201.sh +249 -0
- package/skills/teamsmith/tests/flip-p210.sh +239 -0
- package/skills/teamsmith/tests/flip-p211.sh +152 -0
- package/skills/teamsmith/tests/flip-p217.sh +288 -0
- package/skills/teamsmith/tests/flip-p22.sh +123 -0
- package/skills/teamsmith/tests/flip-p23.sh +239 -0
- package/skills/teamsmith/tests/flip-p25.sh +165 -0
- package/skills/teamsmith/tests/flip-p71.sh +184 -0
- package/skills/teamsmith/tests/flip-p72.sh +202 -0
- package/skills/teamsmith/tests/frames/README.md +176 -0
- package/skills/teamsmith/tests/frames/p78-conversation-rule-below-box.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-cursor-mid-draft.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-draft-rule-below-cursor-line.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-draft-rule-below-cursor.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-draft-rule-blank-region.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-draft-rule-only.txt +5 -0
- package/skills/teamsmith/tests/frames/p78-spinner-row-below-cursor.txt +6 -0
- package/skills/teamsmith/tests/frames/p78-wider-rule-below-cursor.txt +6 -0
- package/skills/teamsmith/tests/frames/p86-f1-mixed-width-disjoint-box.txt +8 -0
- package/skills/teamsmith/tests/frames/p86-f1-narrower-width-disjoint-box.txt +8 -0
- package/skills/teamsmith/tests/frames/p86-f1-spinner-top-disjoint-box.txt +8 -0
- package/skills/teamsmith/tests/frames/pi-0.85.1-update-banner.txt +30 -0
- package/skills/teamsmith/tests/frames/pi-0.87.0-draft-half-sentence.txt +30 -0
- package/skills/teamsmith/tests/frames/pi-0.87.0-empty-box.txt +30 -0
- package/skills/teamsmith/tests/frames/pi-0.87.0-one-line-draft.txt +30 -0
- package/skills/teamsmith/tests/frames/pi-0.87.0-project-trust-prompt.txt +30 -0
- package/skills/teamsmith/tests/frames/pi-0.99.2-empty-editor.txt +32 -0
- package/skills/teamsmith/tests/frames/pi-0.99.2-human-draft.txt +32 -0
- package/skills/teamsmith/tests/frames/pi-1.0.0-git-error-in-box.txt +32 -0
- package/skills/teamsmith/tests/gate-guard.sh +112 -0
- package/skills/teamsmith/tests/guard-matrix.sh +233 -0
- package/skills/teamsmith/tests/install-shape.sh +731 -0
- package/skills/teamsmith/tests/ledger-ban.sh +612 -0
- package/skills/teamsmith/tests/lib/box-judge.sh +68 -0
- package/skills/teamsmith/tests/lib/checkout-shape.sh +129 -0
- package/skills/teamsmith/tests/lib/loop-scan.awk +114 -0
- package/skills/teamsmith/tests/lib/pty-wait.sh +676 -0
- package/skills/teamsmith/tests/lib/section-guard.sh +855 -0
- package/skills/teamsmith/tests/lib/shell-lex.pl +261 -0
- package/skills/teamsmith/tests/lib/tmp-root.sh +528 -0
- package/skills/teamsmith/tests/lib/tmux-cap.sh +35 -0
- package/skills/teamsmith/tests/lib/tmux-iso-call-probe.sh +81 -0
- package/skills/teamsmith/tests/lib/tmux-iso-probe.sh +46 -0
- package/skills/teamsmith/tests/lib/tmux-iso.sh +381 -0
- package/skills/teamsmith/tests/load-experiment.sh +290 -0
- package/skills/teamsmith/tests/loop-inventory.tsv +84 -0
- package/skills/teamsmith/tests/panel-b2-slow-stub.sh +19 -0
- package/skills/teamsmith/tests/panel-b2.sh +1183 -0
- package/skills/teamsmith/tests/panel-b3-pty-mouse.py +240 -0
- package/skills/teamsmith/tests/panel-b3-pty-tmux-mouse.py +138 -0
- package/skills/teamsmith/tests/panel-b3-stub.sh +279 -0
- package/skills/teamsmith/tests/panel-b3.sh +1301 -0
- package/skills/teamsmith/tests/panel-choices.sh +572 -0
- package/skills/teamsmith/tests/panel-clipboard-model.ts +187 -0
- package/skills/teamsmith/tests/panel-compose-model.ts +290 -0
- package/skills/teamsmith/tests/panel-contrast.mjs +81 -0
- package/skills/teamsmith/tests/panel-cpu-premise.sh +173 -0
- package/skills/teamsmith/tests/panel-cpu.sh +391 -0
- package/skills/teamsmith/tests/panel-flip-m54.sh +207 -0
- package/skills/teamsmith/tests/panel-flip-p123.sh +278 -0
- package/skills/teamsmith/tests/panel-flip-p125.sh +318 -0
- package/skills/teamsmith/tests/panel-flip-p128.sh +259 -0
- package/skills/teamsmith/tests/panel-keyprobe-pty.py +99 -0
- package/skills/teamsmith/tests/panel-keyprobe-stub.sh +41 -0
- package/skills/teamsmith/tests/panel-keyprobe.sh +82 -0
- package/skills/teamsmith/tests/panel-keyprobe.tsx +116 -0
- package/skills/teamsmith/tests/panel-knobs.sh +83 -0
- package/skills/teamsmith/tests/panel-p21.sh +2181 -0
- package/skills/teamsmith/tests/panel-snapshots.sh +802 -0
- package/skills/teamsmith/tests/panel-strings.mjs +195 -0
- package/skills/teamsmith/tests/perf.sh +740 -0
- package/skills/teamsmith/tests/pm-box-real.sh +350 -0
- package/skills/teamsmith/tests/pulse-nudge-key.sh +563 -0
- package/skills/teamsmith/tests/routes.sh +1652 -0
- package/skills/teamsmith/tests/section-budgets.tsv +150 -0
- package/skills/teamsmith/tests/section-guard.sh +231 -0
- package/skills/teamsmith/tests/section-needs-audit.sh +94 -0
- package/skills/teamsmith/tests/section-paths.tsv +142 -0
- package/skills/teamsmith/tests/section-select.sh +655 -0
- package/skills/teamsmith/tests/signal-gate.sh +366 -0
- package/skills/teamsmith/tests/signal-lint-legacy.txt +14 -0
- package/skills/teamsmith/tests/signal-lint.pl +416 -0
- package/skills/teamsmith/tests/skill-load.mjs +65 -0
- package/skills/teamsmith/tests/smoke.sh +19711 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-120-detail.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-120-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-120.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-160-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-160.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-59-detail.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-59-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-59.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-99-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-dark-99.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-120-detail.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-120-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-120.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-160-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-160.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-59-detail.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-59-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-59.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-99-p4.txt +32 -0
- package/skills/teamsmith/tests/snapshots/zh-light-99.txt +32 -0
- package/skills/teamsmith/tests/spec-ledger-refs.tsv +78 -0
- package/skills/teamsmith/tests/spec-refs.sh +826 -0
- package/skills/teamsmith/tests/task-header-model.sh +267 -0
- package/skills/teamsmith/tests/team-bg-flip.sh +239 -0
- package/skills/teamsmith/tests/team-bg-harness.mjs +392 -0
- package/skills/teamsmith/tests/team-bg-stop.sh +434 -0
- package/skills/teamsmith/tests/team-inbox-watch-flip.sh +191 -0
- package/skills/teamsmith/tests/team-inbox-watch-harness.mjs +1559 -0
- package/skills/teamsmith/tests/tmp-hygiene.sh +1190 -0
- package/skills/teamsmith/tests/tmux-lint-legacy.txt +31 -0
- package/skills/teamsmith/tests/tmux-lint.pl +604 -0
- package/skills/teamsmith-init/SKILL.md +100 -0
- package/skills/teamsmith-init/references/bootstrap.md +74 -0
- package/skills/teamsmith-init/templates/bootstrap-prompt.md.tmpl +46 -0
|
@@ -0,0 +1,616 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* teamsmith 监视器(可由 node/bun 运行):把每个 agent 的活动渲染成一段流。
|
|
4
|
+
* 设计上刻意做成项目无关(不读任何其它项目的脚本或状态):
|
|
5
|
+
* - agent 列表来自 <root>/.worktrees/<agent>/(外加 PM 自己的会话)
|
|
6
|
+
* - **Pi(默认)**:会话文件按 Pi 的目录规则推出(--<cwd 去前导 / 并把 / 换成 -->--)
|
|
7
|
+
* - **内部接缝(frozen,为将来非 Pi 适配预留、不承诺兼容)**:配了 --log-glob / TEAM_AGENT_LOG_GLOB 时,显示最新匹配文件的尾部
|
|
8
|
+
* (支持 `~`、绝对/相对路径、`*` `?` `**`,以及 `{agent}` = agent 名)
|
|
9
|
+
* - 两者都没有时只显示「无会话」,不影响上面的团队状态面板
|
|
10
|
+
* - 每段显示:状态 emoji + 名字 + 分支(+脏标记) + 已运行/空闲/事件数 + 最近几条事件
|
|
11
|
+
*
|
|
12
|
+
* node scripts/monitor.mjs --root <proj> [--events 4] [--only dev,verify] [--log-glob '<glob>']
|
|
13
|
+
* [--log-tail-bytes 65536] [--json]
|
|
14
|
+
*
|
|
15
|
+
* 显示安全(日志是**不可信输入**):
|
|
16
|
+
* - **只读尾窗**,绝不整读:日志尾部最多读 --log-tail-bytes / TEAM_AGENT_LOG_TAIL_BYTES
|
|
17
|
+
* 字节(默认 64KiB=65536,硬上限 1MiB=1048576,超了夹住并警告);Pi 会话是
|
|
18
|
+
* 「头部窗口 64KiB(取 session 头)+ 尾窗」(两者都是常量级内存)。
|
|
19
|
+
* 看门狗每轮、每个 agent 都会跑这个,GB 级日志不能把它(和 PM 机器)压垮。
|
|
20
|
+
* - 渲染前剥掉控制序列(ANSI/CSI/OSC/C1、CR/BEL/BS、双向控制字符),
|
|
21
|
+
* 显示**文本**而不是控制字节:OSC 能改窗口标题、清屏、写剪贴板。
|
|
22
|
+
* - 读不到的文件(权限/FIFO/设备/已被删/二进制)降级成「不可用 + 原因」,不崩、不挂死、不静默。
|
|
23
|
+
*/
|
|
24
|
+
import {
|
|
25
|
+
readdirSync,
|
|
26
|
+
statSync,
|
|
27
|
+
lstatSync,
|
|
28
|
+
openSync,
|
|
29
|
+
closeSync,
|
|
30
|
+
readSync,
|
|
31
|
+
fstatSync,
|
|
32
|
+
constants,
|
|
33
|
+
} from 'node:fs'
|
|
34
|
+
import { execFileSync } from 'node:child_process'
|
|
35
|
+
import { homedir } from 'node:os'
|
|
36
|
+
import { join } from 'node:path'
|
|
37
|
+
|
|
38
|
+
const MAX_TEXT = 150
|
|
39
|
+
const STALE_SECONDS = 3600
|
|
40
|
+
const ACTIVE_SECONDS = 120
|
|
41
|
+
// 只读窗口(字节)。默认 64KiB 足够铺满 --events 行;硬上限 1MiB 防止有人配出「借尾部之名整读」。
|
|
42
|
+
const TAIL_DEFAULT_BYTES = 64 * 1024
|
|
43
|
+
const TAIL_CAP_BYTES = 1024 * 1024
|
|
44
|
+
// Pi 会话:头部窗口用来取 session 头(cwd/开始时间),尾部窗口用来取最近消息;中间那段直接跳过。
|
|
45
|
+
const SESSION_HEAD_BYTES = 64 * 1024
|
|
46
|
+
|
|
47
|
+
function arg(name, fallback) {
|
|
48
|
+
const i = process.argv.indexOf(`--${name}`)
|
|
49
|
+
if (i === -1) return fallback
|
|
50
|
+
const v = process.argv[i + 1]
|
|
51
|
+
return v && !v.startsWith('--') ? v : true
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const root = String(arg('root', process.cwd()))
|
|
55
|
+
const eventsPerAgent = Number(arg('events', 4)) || 4
|
|
56
|
+
const only = String(arg('only', '') || '').split(',').filter(Boolean)
|
|
57
|
+
const asJson = arg('json', false) === true
|
|
58
|
+
// 非 Pi 的 agent:显示配置的日志/会话文件的尾部(空 = 不启用,只跟 Pi 会话)
|
|
59
|
+
const logGlob = String(arg('log-glob', process.env.TEAM_AGENT_LOG_GLOB || '') || '')
|
|
60
|
+
const sessionsRoot = join(homedir(), '.pi', 'agent', 'sessions')
|
|
61
|
+
|
|
62
|
+
// ---------------------------------------------------------------- 显示安全:剥掉控制序列
|
|
63
|
+
// 显示文本,不显示控制字节。日志/会话内容都可能是别人写的:
|
|
64
|
+
// OSC(`ESC ]` … BEL/ST):改窗口标题、写剪贴板(OSC 52)、超链接;CSI(`ESC [`):清屏/染色/移动光标;
|
|
65
|
+
// 裸 ESC、BEL、BS、CR:覆盖或污染 PM 的终端;双向控制字符(U+202A–202E、U+2066–2069):文本骗眼睛。
|
|
66
|
+
// 只保留 \n 与 \t(渲染时会再折叠成空格),其余控制字符全部剥掉。
|
|
67
|
+
const OSC_RE = /\u001B\][^\u0007\u001B]*(?:\u0007|\u001B\\)/g
|
|
68
|
+
const CSI_RE = /\u001B\[[0-?]*[ -/]*[@-~]/g
|
|
69
|
+
const C1_CSI_RE = /\u009B[0-?]*[ -/]*[@-~]/g
|
|
70
|
+
const ESC2_RE = /\u001B[@-Z\\-_]/g
|
|
71
|
+
const CTRL_RE = /[\u0000-\u0008\u000B-\u001F\u007F-\u009F\u200B\u200E\u200F\u202A-\u202E\u2060\u2066-\u2069\uFEFF]/g
|
|
72
|
+
|
|
73
|
+
function sanitize(value) {
|
|
74
|
+
return String(value ?? '')
|
|
75
|
+
.replace(OSC_RE, '')
|
|
76
|
+
.replace(CSI_RE, '')
|
|
77
|
+
.replace(C1_CSI_RE, '')
|
|
78
|
+
.replace(ESC2_RE, '')
|
|
79
|
+
.replace(CTRL_RE, '')
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 所有对外输出(文本与 --json 都是)走一遍,字段名也走(防文件名/分支名里夹控制字节)
|
|
83
|
+
function sanitizeDeep(value) {
|
|
84
|
+
if (typeof value === 'string') return sanitize(value)
|
|
85
|
+
if (Array.isArray(value)) return value.map(sanitizeDeep)
|
|
86
|
+
if (value && typeof value === 'object') {
|
|
87
|
+
const out = {}
|
|
88
|
+
for (const [k, v] of Object.entries(value)) out[sanitize(k)] = sanitizeDeep(v)
|
|
89
|
+
return out
|
|
90
|
+
}
|
|
91
|
+
return value
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ---------------------------------------------------------------- 只读窗口(字节数)
|
|
95
|
+
function tailLimitFrom(raw) {
|
|
96
|
+
const def = `${TAIL_DEFAULT_BYTES}`
|
|
97
|
+
if (raw === undefined || raw === null || raw === '' || raw === true) {
|
|
98
|
+
const why = raw === true ? '--log-tail-bytes 需要一个字节数' : ''
|
|
99
|
+
return { bytes: TAIL_DEFAULT_BYTES, note: why ? `${why},回退到默认 ${def} 字节` : '' }
|
|
100
|
+
}
|
|
101
|
+
const n = Number(raw)
|
|
102
|
+
if (!Number.isFinite(n) || n <= 0) {
|
|
103
|
+
return { bytes: TAIL_DEFAULT_BYTES, note: `--log-tail-bytes / TEAM_AGENT_LOG_TAIL_BYTES 的值 ${JSON.stringify(String(raw))} 不是正整数,回退到默认 ${def} 字节` }
|
|
104
|
+
}
|
|
105
|
+
if (n > TAIL_CAP_BYTES) {
|
|
106
|
+
return { bytes: TAIL_CAP_BYTES, note: `--log-tail-bytes 的值 ${Math.floor(n)} 超过硬上限,夹到 ${TAIL_CAP_BYTES} 字节` }
|
|
107
|
+
}
|
|
108
|
+
return { bytes: Math.floor(n), note: '' }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const tailLimitCfg = tailLimitFrom(arg('log-tail-bytes', process.env.TEAM_AGENT_LOG_TAIL_BYTES || ''))
|
|
112
|
+
const tailLimit = tailLimitCfg.bytes
|
|
113
|
+
if (tailLimitCfg.note) process.stderr.write(`monitor: ${sanitize(tailLimitCfg.note)}\n`)
|
|
114
|
+
|
|
115
|
+
// ---------------------------------------------------------------- 有界读取
|
|
116
|
+
function reasonFor(err) {
|
|
117
|
+
const code = (err && (err.code || err.errno)) || ''
|
|
118
|
+
const map = {
|
|
119
|
+
EACCES: '权限不足(EACCES)',
|
|
120
|
+
EPERM: '权限不足(EPERM)',
|
|
121
|
+
ENOENT: '文件不存在了(ENOENT:读之前被删除或移走)',
|
|
122
|
+
EISDIR: '是一个目录(EISDIR)',
|
|
123
|
+
ELOOP: '符号链接循环(ELOOP)',
|
|
124
|
+
ENXIO: '设备不可读(ENXIO)',
|
|
125
|
+
ENAMETOOLONG: '路径过长(ENAMETOOLONG)',
|
|
126
|
+
EMFILE: '打开的文件太多(EMFILE)',
|
|
127
|
+
ENFILE: '系统打开的文件太多(ENFILE)',
|
|
128
|
+
EIO: 'I/O 错误(EIO)',
|
|
129
|
+
}
|
|
130
|
+
if (code && map[code]) return map[code]
|
|
131
|
+
const msg = err && err.message ? String(err.message) : 'unknown'
|
|
132
|
+
return `读取失败(${code || msg})`
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// 只认**普通文件**:FIFO/设备/套接字一律拒绝(对 FIFO 调 readFileSync 会一直阻塞,
|
|
136
|
+
// 看门狗窗口就挂死了)。O_NONBLOCK + fstat 双保险,避免 stat 与 open 之间的竞态。
|
|
137
|
+
function openRegular(path) {
|
|
138
|
+
let fd
|
|
139
|
+
try {
|
|
140
|
+
fd = openSync(path, constants.O_RDONLY | constants.O_NONBLOCK)
|
|
141
|
+
} catch (err) {
|
|
142
|
+
return { ok: false, reason: reasonFor(err) }
|
|
143
|
+
}
|
|
144
|
+
let st
|
|
145
|
+
try {
|
|
146
|
+
st = fstatSync(fd)
|
|
147
|
+
} catch (err) {
|
|
148
|
+
try { closeSync(fd) } catch { /* 关不掉就算了 */ }
|
|
149
|
+
return { ok: false, reason: reasonFor(err) }
|
|
150
|
+
}
|
|
151
|
+
if (st.isDirectory()) {
|
|
152
|
+
try { closeSync(fd) } catch { /* ignore */ }
|
|
153
|
+
return { ok: false, reason: '是一个目录(不是日志文件)' }
|
|
154
|
+
}
|
|
155
|
+
if (!st.isFile()) {
|
|
156
|
+
try { closeSync(fd) } catch { /* ignore */ }
|
|
157
|
+
return { ok: false, reason: '不是普通文件(FIFO/设备/套接字),拒绝读取以免挂死' }
|
|
158
|
+
}
|
|
159
|
+
return { ok: true, fd, size: st.size }
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// 读 [start, start+len) 这一段;文件被截断/还在写时允许短读
|
|
163
|
+
function readRange(fd, start, len) {
|
|
164
|
+
if (!(len > 0)) return { buf: Buffer.alloc(0), error: null }
|
|
165
|
+
const buf = Buffer.allocUnsafe(len)
|
|
166
|
+
let off = 0
|
|
167
|
+
while (off < len) {
|
|
168
|
+
let n
|
|
169
|
+
try {
|
|
170
|
+
n = readSync(fd, buf, off, len - off, start + off)
|
|
171
|
+
} catch (err) {
|
|
172
|
+
return { buf: buf.subarray(0, off), error: reasonFor(err) }
|
|
173
|
+
}
|
|
174
|
+
if (n <= 0) break
|
|
175
|
+
off += n
|
|
176
|
+
}
|
|
177
|
+
return { buf: buf.subarray(0, off), error: null }
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function decodeText(buf) {
|
|
181
|
+
return buf.toString('utf8')
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// 日志尾部:最多读最后 limit 字节。文件比窗口小 → 整读(与历史行为逐字节一致)。
|
|
185
|
+
function readTailText(path, limit) {
|
|
186
|
+
const opened = openRegular(path)
|
|
187
|
+
if (!opened.ok) return { ok: false, reason: opened.reason }
|
|
188
|
+
const size = opened.size
|
|
189
|
+
const from = Math.max(0, size - limit)
|
|
190
|
+
const { buf, error } = readRange(opened.fd, from, size - from)
|
|
191
|
+
try { closeSync(opened.fd) } catch { /* ignore */ }
|
|
192
|
+
if (error && !buf.length) return { ok: false, reason: error, size }
|
|
193
|
+
const text = decodeText(buf)
|
|
194
|
+
if (text.includes('\u0000')) return { ok: false, reason: '内容里有 NUL 字节(疑似二进制或空洞文件),不显示', size }
|
|
195
|
+
let lines = text.split('\n')
|
|
196
|
+
if (from > 0 && lines.length > 1) lines.shift() // 窗口起点那半截行丢掉
|
|
197
|
+
lines = lines.map(l => sanitize(l).replace(/\s+/g, ' ').trim()).filter(Boolean)
|
|
198
|
+
return { ok: true, lines, size, bytes: buf.length, truncated: from > 0, limit }
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function mtimeMs(path) {
|
|
202
|
+
try {
|
|
203
|
+
return statSync(path).mtimeMs
|
|
204
|
+
} catch { /* 断链等:退回 lstat,至少还能报「不可读 + 原因」 */ }
|
|
205
|
+
try {
|
|
206
|
+
return lstatSync(path).mtimeMs
|
|
207
|
+
} catch {
|
|
208
|
+
return null
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function run(cmd, args, cwd) {
|
|
213
|
+
try {
|
|
214
|
+
return execFileSync(cmd, args, { cwd, encoding: 'utf8', timeout: 5000 }).trim()
|
|
215
|
+
} catch {
|
|
216
|
+
return ''
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function sessionDirFor(cwd) {
|
|
221
|
+
if (!cwd) return ''
|
|
222
|
+
return join(sessionsRoot, `--${cwd.replace(/^\//, '').replace(/\//g, '-')}--`)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function newestSession(dir, match) {
|
|
226
|
+
try {
|
|
227
|
+
const files = readdirSync(dir)
|
|
228
|
+
.filter(f => f.endsWith('.jsonl') && (!match || f.includes(match)))
|
|
229
|
+
.map(f => ({ f, m: mtimeMs(join(dir, f)) }))
|
|
230
|
+
.filter(e => e.m !== null)
|
|
231
|
+
.sort((a, b) => b.m - a.m)
|
|
232
|
+
return files.length ? { path: join(dir, files[0].f), mtime: files[0].m / 1000 } : null
|
|
233
|
+
} catch {
|
|
234
|
+
return null
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const icons = { say: '💬', run: '🔧', out: ' ↳', think: '🤔', user: '👤' }
|
|
239
|
+
|
|
240
|
+
// Pi 会话:头部窗口(session 头 + 前面几条,够了)+ 尾窗(最近的消息)。
|
|
241
|
+
// 以前是 readFileSync 整读:几十 MB 的会话在活动流一开就吃几十 MB 内存,且没有上限。
|
|
242
|
+
function readSession(path) {
|
|
243
|
+
const events = []
|
|
244
|
+
let cwd = ''
|
|
245
|
+
let started = null
|
|
246
|
+
const fail = reason => ({ ok: false, reason, events, cwd, started, file_bytes: null, bytes: 0, truncated: false })
|
|
247
|
+
const opened = openRegular(path)
|
|
248
|
+
if (!opened.ok) return fail(opened.reason)
|
|
249
|
+
const size = opened.size
|
|
250
|
+
const headLen = Math.min(SESSION_HEAD_BYTES, size)
|
|
251
|
+
const head = readRange(opened.fd, 0, headLen)
|
|
252
|
+
if (head.error && !head.buf.length) {
|
|
253
|
+
try { closeSync(opened.fd) } catch { /* ignore */ }
|
|
254
|
+
return fail(head.error)
|
|
255
|
+
}
|
|
256
|
+
const headText = decodeText(head.buf)
|
|
257
|
+
if (headText.includes('\u0000')) {
|
|
258
|
+
try { closeSync(opened.fd) } catch { /* ignore */ }
|
|
259
|
+
return fail('内容里有 NUL 字节(疑似二进制),不显示')
|
|
260
|
+
}
|
|
261
|
+
const tailFrom = Math.max(headLen, size - tailLimit) // 与头部窗口不重叠
|
|
262
|
+
const tail = size > headLen ? readRange(opened.fd, tailFrom, size - tailFrom) : { buf: Buffer.alloc(0) }
|
|
263
|
+
try { closeSync(opened.fd) } catch { /* ignore */ }
|
|
264
|
+
|
|
265
|
+
const parseLine = (line, ts) => {
|
|
266
|
+
if (!line.trim()) return
|
|
267
|
+
let entry
|
|
268
|
+
try {
|
|
269
|
+
entry = JSON.parse(line)
|
|
270
|
+
} catch {
|
|
271
|
+
return
|
|
272
|
+
}
|
|
273
|
+
if (entry.type === 'session' && !started) {
|
|
274
|
+
cwd = String(entry.cwd || '')
|
|
275
|
+
started = entry.timestamp ? new Date(entry.timestamp) : null
|
|
276
|
+
return
|
|
277
|
+
}
|
|
278
|
+
if (entry.type !== 'message') return
|
|
279
|
+
const message = entry.message || {}
|
|
280
|
+
const role = message.role
|
|
281
|
+
const stamp = String(entry.timestamp || ts || '')
|
|
282
|
+
const record = (kind, detail) => {
|
|
283
|
+
const d = sanitize(String(detail ?? '')).replace(/\s+/g, ' ').trim().slice(0, MAX_TEXT)
|
|
284
|
+
if (d) events.push({ ts: stamp, kind, text: d })
|
|
285
|
+
}
|
|
286
|
+
if (role === 'user') {
|
|
287
|
+
for (const part of message.content || []) {
|
|
288
|
+
if (part && typeof part === 'object' && part.type === 'text') record('user', part.text)
|
|
289
|
+
}
|
|
290
|
+
return
|
|
291
|
+
}
|
|
292
|
+
for (const part of message.content || []) {
|
|
293
|
+
if (!part || typeof part !== 'object') continue
|
|
294
|
+
if (part.type === 'text') record('say', part.text)
|
|
295
|
+
else if (part.type === 'thinking') record('think', part.thinking)
|
|
296
|
+
else if (part.type === 'toolCall') {
|
|
297
|
+
const a = part.args || {}
|
|
298
|
+
const key = a.command || a.path || a.pattern || a.query || ''
|
|
299
|
+
record('run', `${part.name || '?'} ${key}`)
|
|
300
|
+
} else if (part.type === 'toolResult' || part.type === 'tool') {
|
|
301
|
+
record('out', typeof part.text === 'string' ? part.text : JSON.stringify(part).slice(0, 100))
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
// 头部:文件比窗口大时最后一行可能被切断,只吃完整行
|
|
306
|
+
const headLines = headText.split('\n')
|
|
307
|
+
if (size > headLen) headLines.pop()
|
|
308
|
+
for (const line of headLines) parseLine(line)
|
|
309
|
+
// 尾部:窗口起点在文件中间时丢掉半截行(末行没换行也照实显示——文件可能还在写)
|
|
310
|
+
const tailLines = tail.buf.length ? decodeText(tail.buf).split('\n') : []
|
|
311
|
+
if (tailFrom > 0 && tailLines.length > 1) tailLines.shift()
|
|
312
|
+
for (const line of tailLines) parseLine(line)
|
|
313
|
+
|
|
314
|
+
return {
|
|
315
|
+
ok: true,
|
|
316
|
+
reason: null,
|
|
317
|
+
events,
|
|
318
|
+
cwd,
|
|
319
|
+
started,
|
|
320
|
+
file_bytes: size,
|
|
321
|
+
bytes: head.buf.length + tail.buf.length,
|
|
322
|
+
truncated: size > head.buf.length + tail.buf.length,
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// ---------------------------------------------------------------- 日志通配(非 Pi agent)
|
|
327
|
+
// 不引入依赖:自己展开 `*` `?` `**` 就够用(monitor 只做展示,不需要精确的 glob 语义)。
|
|
328
|
+
function readdirSafe(dir) {
|
|
329
|
+
try {
|
|
330
|
+
return readdirSync(dir, { withFileTypes: true })
|
|
331
|
+
} catch {
|
|
332
|
+
return []
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function walkGlob(dir, segs, out) {
|
|
337
|
+
if (!segs.length) return
|
|
338
|
+
const [head, ...rest] = segs
|
|
339
|
+
if (head === '**') {
|
|
340
|
+
walkGlob(dir, rest, out)
|
|
341
|
+
for (const e of readdirSafe(dir)) {
|
|
342
|
+
if (e.isDirectory()) walkGlob(join(dir, e.name), segs, out)
|
|
343
|
+
}
|
|
344
|
+
return
|
|
345
|
+
}
|
|
346
|
+
if (/[*?]/.test(head)) {
|
|
347
|
+
const re = new RegExp(
|
|
348
|
+
'^' + head.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '[^/]*').replace(/\?/g, '[^/]') + '$',
|
|
349
|
+
)
|
|
350
|
+
for (const e of readdirSafe(dir)) {
|
|
351
|
+
if (!re.test(e.name)) continue
|
|
352
|
+
const next = join(dir, e.name)
|
|
353
|
+
if (!rest.length) {
|
|
354
|
+
// 目录不算「日志」;其余(普通文件、符号链接、FIFO、设备)都收进来,
|
|
355
|
+
// 由读取端判定并给出原因 —— 静默过滤掉才是历史 bug(FIFO/设备/断链都变成「无会话」)
|
|
356
|
+
if (!e.isDirectory()) out.push(next)
|
|
357
|
+
} else if (e.isDirectory()) {
|
|
358
|
+
walkGlob(next, rest, out)
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return
|
|
362
|
+
}
|
|
363
|
+
const next = join(dir, head)
|
|
364
|
+
let st = null
|
|
365
|
+
try {
|
|
366
|
+
st = statSync(next)
|
|
367
|
+
} catch {
|
|
368
|
+
return
|
|
369
|
+
}
|
|
370
|
+
if (!rest.length) {
|
|
371
|
+
if (!st.isDirectory()) out.push(next)
|
|
372
|
+
return
|
|
373
|
+
}
|
|
374
|
+
if (st.isDirectory()) walkGlob(next, rest, out)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function expandGlob(pattern, vars) {
|
|
378
|
+
if (!pattern) return []
|
|
379
|
+
let p = String(pattern).replace(/\{agent\}/g, (vars && vars.agent) || '')
|
|
380
|
+
if (p.startsWith('~')) p = join(homedir(), p.slice(1))
|
|
381
|
+
if (!p.startsWith('/')) p = join(process.cwd(), p)
|
|
382
|
+
const out = []
|
|
383
|
+
walkGlob('/', p.split('/').filter(Boolean), out)
|
|
384
|
+
return out
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// 用最新匹配文件的尾部当这个 agent 的“活动流”(一个都没匹配到返回 null,调用方退回「无会话」;
|
|
388
|
+
// 匹配到了但读不到 → 「不可用 + 原因」)
|
|
389
|
+
function logActivity(name, cwd) {
|
|
390
|
+
const files = expandGlob(logGlob, { agent: name === 'pm (PM)' ? 'pm' : name })
|
|
391
|
+
const stamped = files
|
|
392
|
+
.map(f => ({ f, m: mtimeMs(f) }))
|
|
393
|
+
.filter(e => e.m !== null)
|
|
394
|
+
.sort((a, b) => b.m - a.m)
|
|
395
|
+
if (!stamped.length) return null
|
|
396
|
+
const newest = stamped[0]
|
|
397
|
+
const age = Date.now() / 1000 - newest.m / 1000
|
|
398
|
+
const status = age > STALE_SECONDS ? '⚫ 陈旧' : age < ACTIVE_SECONDS ? '🟢 活跃' : '🟡 静默'
|
|
399
|
+
const branch = branchOf(cwd || root)
|
|
400
|
+
const read = readTailText(newest.f, tailLimit)
|
|
401
|
+
if (!read.ok) {
|
|
402
|
+
return {
|
|
403
|
+
source: 'log',
|
|
404
|
+
logPath: newest.f,
|
|
405
|
+
status,
|
|
406
|
+
available: false,
|
|
407
|
+
reason: read.reason || '读取失败',
|
|
408
|
+
events: [],
|
|
409
|
+
idle: fmtDur(age),
|
|
410
|
+
count: 0,
|
|
411
|
+
branch,
|
|
412
|
+
file_bytes: read.size ?? null,
|
|
413
|
+
tail_bytes: 0,
|
|
414
|
+
truncated: true,
|
|
415
|
+
tail_limit: tailLimit,
|
|
416
|
+
meta: `不可读:${read.reason || '读取失败'}`,
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
const events = read.lines.slice(-eventsPerAgent).map(l => ({ ts: '', kind: 'out', text: l.slice(0, MAX_TEXT) }))
|
|
420
|
+
return {
|
|
421
|
+
source: 'log',
|
|
422
|
+
logPath: newest.f,
|
|
423
|
+
status,
|
|
424
|
+
available: true,
|
|
425
|
+
reason: null,
|
|
426
|
+
events: age > STALE_SECONDS ? [] : events,
|
|
427
|
+
idle: fmtDur(age),
|
|
428
|
+
count: read.lines.length,
|
|
429
|
+
branch,
|
|
430
|
+
file_bytes: read.size,
|
|
431
|
+
tail_bytes: read.bytes,
|
|
432
|
+
truncated: read.truncated,
|
|
433
|
+
tail_limit: tailLimit,
|
|
434
|
+
meta: age > STALE_SECONDS ? `最后活动 ${fmtDur(age)} 前(日志 ${newest.f})` : '',
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// 延后投递队列的一行摘要(delivery-guard):队列为空返回空字符串。
|
|
439
|
+
// 队列文件格式是契约(见 scripts/lib/outbox.sh):<epoch-ms>-<seq>-<target>.msg,年龄从文件名算。
|
|
440
|
+
function outboxSummary(root) {
|
|
441
|
+
const dir = join(root, '.pi/team/state/outbox')
|
|
442
|
+
const list = (p) => {
|
|
443
|
+
try {
|
|
444
|
+
return readdirSync(p).filter((n) => n.endsWith('.msg'))
|
|
445
|
+
} catch {
|
|
446
|
+
return []
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
const active = list(dir)
|
|
450
|
+
const held = list(join(dir, 'held'))
|
|
451
|
+
const all = [...active, ...held].sort()
|
|
452
|
+
if (!all.length) return ''
|
|
453
|
+
const ms = Number(String(all[0]).split('-')[0])
|
|
454
|
+
const age = Number.isFinite(ms) && ms > 0 ? fmtDur(Date.now() / 1000 - ms / 1000) : '?'
|
|
455
|
+
return `outbox ${all.length} 条待投递(held ${held.length})· 最老 ${age} · team outbox list`
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function fmtDur(seconds) {
|
|
459
|
+
const s = Math.max(0, Math.floor(seconds))
|
|
460
|
+
if (s < 60) return `${s}s`
|
|
461
|
+
if (s < 3600) return `${Math.floor(s / 60)}m${String(s % 60).padStart(2, '0')}s`
|
|
462
|
+
return `${Math.floor(s / 3600)}h${String(Math.floor((s % 3600) / 60)).padStart(2, '0')}m`
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function fmtBytes(n) {
|
|
466
|
+
const v = Number(n)
|
|
467
|
+
if (!Number.isFinite(v) || v < 0) return '?'
|
|
468
|
+
if (v < 1024) return `${Math.round(v)}B`
|
|
469
|
+
if (v < 1024 * 1024) return `${(v / 1024).toFixed(v < 10240 ? 1 : 0)}K`
|
|
470
|
+
if (v < 1024 * 1024 * 1024) return `${(v / (1024 * 1024)).toFixed(1)}M`
|
|
471
|
+
return `${(v / (1024 * 1024 * 1024)).toFixed(2)}G`
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
function branchOf(cwd) {
|
|
475
|
+
if (!cwd) return '?'
|
|
476
|
+
const b = run('git', ['-C', cwd, 'rev-parse', '--abbrev-ref', 'HEAD'])
|
|
477
|
+
const dirty = run('git', ['-C', cwd, 'status', '--porcelain'])
|
|
478
|
+
return b ? `${b}${dirty ? '*' : ''}` : '?'
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function targets() {
|
|
482
|
+
const list = []
|
|
483
|
+
if (!only.length || only.includes('pm')) list.push({ name: 'pm (PM)', cwd: root })
|
|
484
|
+
let dirs = []
|
|
485
|
+
try {
|
|
486
|
+
dirs = readdirSync(join(root, '.worktrees'), { withFileTypes: true })
|
|
487
|
+
.filter(d => d.isDirectory() && !d.name.startsWith('review-'))
|
|
488
|
+
.map(d => d.name)
|
|
489
|
+
.sort()
|
|
490
|
+
} catch {
|
|
491
|
+
/* 没有 worktrees */
|
|
492
|
+
}
|
|
493
|
+
for (const name of dirs) {
|
|
494
|
+
if (only.length && !only.includes(name)) continue
|
|
495
|
+
list.push({ name, cwd: join(root, '.worktrees', name) })
|
|
496
|
+
}
|
|
497
|
+
return list
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const now = Date.now()
|
|
501
|
+
const blocks = []
|
|
502
|
+
for (const t of targets()) {
|
|
503
|
+
// ① 显式配了日志通配就先看它(非 Pi adapter 的意图很明确)
|
|
504
|
+
// ② 否则找 Pi 会话:先找“agent 在自己 worktree 里跑”的,再退一步找主仓库目录下 session id 形如 <session>-<agent> 的
|
|
505
|
+
const logBlock = logGlob ? logActivity(t.name, t.cwd) : null
|
|
506
|
+
if (logBlock) {
|
|
507
|
+
blocks.push({ ...t, ...logBlock })
|
|
508
|
+
continue
|
|
509
|
+
}
|
|
510
|
+
const dir = sessionDirFor(t.cwd)
|
|
511
|
+
const found = newestSession(dir) || newestSession(sessionDirFor(root), `-${t.name}.jsonl`) || null
|
|
512
|
+
if (!found) {
|
|
513
|
+
const hint = logGlob
|
|
514
|
+
? `(无 pi 会话;TEAM_AGENT_LOG_GLOB 未匹配到文件:${logGlob})`
|
|
515
|
+
: '(无 pi 会话)'
|
|
516
|
+
blocks.push({ ...t, status: '⚫ 无会话', source: 'none', events: [], meta: hint })
|
|
517
|
+
continue
|
|
518
|
+
}
|
|
519
|
+
const age = now / 1000 - found.mtime
|
|
520
|
+
if (age > STALE_SECONDS) {
|
|
521
|
+
// 一行式渲染(老形状:有 meta、没有 count):陈旧就不再去读文件了
|
|
522
|
+
blocks.push({ ...t, status: '⚫ 陈旧', source: 'pi', sessionPath: found.path, events: [], meta: `最后活动 ${fmtDur(age)} 前` })
|
|
523
|
+
continue
|
|
524
|
+
}
|
|
525
|
+
const sess = readSession(found.path)
|
|
526
|
+
if (!sess.ok) {
|
|
527
|
+
blocks.push({
|
|
528
|
+
...t,
|
|
529
|
+
source: 'pi',
|
|
530
|
+
sessionPath: found.path,
|
|
531
|
+
status: '⚫ 会话不可读',
|
|
532
|
+
available: false,
|
|
533
|
+
reason: sess.reason,
|
|
534
|
+
events: [],
|
|
535
|
+
count: 0,
|
|
536
|
+
idle: fmtDur(age),
|
|
537
|
+
branch: branchOf(sess.cwd || t.cwd),
|
|
538
|
+
file_bytes: sess.file_bytes,
|
|
539
|
+
truncated: false,
|
|
540
|
+
meta: `会话不可读:${sess.reason}`,
|
|
541
|
+
})
|
|
542
|
+
continue
|
|
543
|
+
}
|
|
544
|
+
const { events, cwd, started } = sess
|
|
545
|
+
const last = events.length ? new Date(events[events.length - 1].ts) : started
|
|
546
|
+
const elapsed = started ? fmtDur((now - started.getTime()) / 1000) : '?'
|
|
547
|
+
const idle = last ? fmtDur((now - last.getTime()) / 1000) : '?'
|
|
548
|
+
const status = age < ACTIVE_SECONDS ? '🟢 活跃' : '🟡 静默'
|
|
549
|
+
blocks.push({
|
|
550
|
+
...t,
|
|
551
|
+
source: 'pi',
|
|
552
|
+
sessionPath: found.path,
|
|
553
|
+
status,
|
|
554
|
+
available: true,
|
|
555
|
+
reason: null,
|
|
556
|
+
elapsed,
|
|
557
|
+
idle,
|
|
558
|
+
count: events.length,
|
|
559
|
+
branch: branchOf(cwd || t.cwd),
|
|
560
|
+
file_bytes: sess.file_bytes,
|
|
561
|
+
tail_bytes: sess.bytes,
|
|
562
|
+
truncated: sess.truncated,
|
|
563
|
+
events: events.slice(-eventsPerAgent),
|
|
564
|
+
})
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// 对外输出统一走净化:文本模式与 --json 都是(JSON 里 \u001b 虽然会被转义,但显示它的是人)
|
|
568
|
+
const clean = sanitizeDeep(blocks)
|
|
569
|
+
|
|
570
|
+
if (asJson) {
|
|
571
|
+
console.log(JSON.stringify(clean, null, 2))
|
|
572
|
+
} else {
|
|
573
|
+
const width = 112
|
|
574
|
+
// 延后投递(delivery-guard):队列非空时补一行(空队列不加任何东西)。
|
|
575
|
+
// 数据源就是队列目录本身:<root>/.pi/team/state/outbox/*.msg(held/ 里的也算)
|
|
576
|
+
const obox = outboxSummary(root)
|
|
577
|
+
if (obox) console.log(obox)
|
|
578
|
+
if (!clean.length) {
|
|
579
|
+
console.log('(没有可监视的 agent:先 team add-agent <名字>)')
|
|
580
|
+
}
|
|
581
|
+
for (const b of clean) {
|
|
582
|
+
if (b.meta !== undefined && b.count === undefined) {
|
|
583
|
+
console.log(`${b.status} ${b.name}`.padEnd(40) + (b.meta ? ` ${b.meta}` : ''))
|
|
584
|
+
console.log('-'.repeat(width))
|
|
585
|
+
continue
|
|
586
|
+
}
|
|
587
|
+
if (b.source === 'log') {
|
|
588
|
+
if (b.available === false) {
|
|
589
|
+
console.log(`${b.status} ${b.name.padEnd(20)} [${b.branch}] 日志不可读 ${b.logPath} · ${b.reason}`)
|
|
590
|
+
} else {
|
|
591
|
+
const bound = b.truncated ? ` · 只读尾部 ${fmtBytes(b.tail_limit)}/${fmtBytes(b.file_bytes)}` : ''
|
|
592
|
+
console.log(
|
|
593
|
+
`${b.status} ${b.name.padEnd(20)} [${b.branch}] 日志尾部 ${b.logPath} · 空闲 ${b.idle} · 行 ${b.count}${bound}`,
|
|
594
|
+
)
|
|
595
|
+
}
|
|
596
|
+
} else if (b.available === false) {
|
|
597
|
+
console.log(
|
|
598
|
+
`${b.status} ${b.name.padEnd(20)} [${b.branch}] 会话不可读 ${b.sessionPath || ''} · ${b.reason}`,
|
|
599
|
+
)
|
|
600
|
+
} else {
|
|
601
|
+
console.log(
|
|
602
|
+
`${b.status} ${b.name.padEnd(20)} [${b.branch}] 已运行 ${b.elapsed} · 空闲 ${b.idle} · 事件 ${b.count}`,
|
|
603
|
+
)
|
|
604
|
+
}
|
|
605
|
+
for (const e of b.events) {
|
|
606
|
+
const clock = e.ts.length >= 19 ? e.ts.slice(11, 19) : e.ts
|
|
607
|
+
console.log(` ${clock} ${icons[e.kind] || '·'} ${e.text}`)
|
|
608
|
+
}
|
|
609
|
+
console.log('-'.repeat(width))
|
|
610
|
+
}
|
|
611
|
+
if (!logGlob && clean.length && clean.every(b => b.source === 'none')) {
|
|
612
|
+
console.log(
|
|
613
|
+
' (没有 Pi 会话文件;内部接缝(frozen)可用 TEAM_AGENT_LOG_GLOB 让活动流显示日志尾部)',
|
|
614
|
+
)
|
|
615
|
+
}
|
|
616
|
+
}
|