teamsmith 0.0.0-stage → 0.1.1
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 +156 -2
- package/bin/team.mjs +59 -0
- package/install.sh +94 -0
- package/package.json +37 -4
- package/skills/teamsmith/CHANGELOG.md +747 -0
- package/skills/teamsmith/SKILL.md +312 -0
- package/skills/teamsmith/extension/team-bg.ts +399 -0
- package/skills/teamsmith/extension/team-inbox-watch.ts +1412 -0
- package/skills/teamsmith/extension/team-notify.ts +524 -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 +208 -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/checkout-shape-probe.sh +571 -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 +77 -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 +648 -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 +19599 -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,399 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* teamsmith · team-bg 扩展 — 团队机械的后台任务车道(零第三方依赖,不进用户会话)
|
|
3
|
+
*
|
|
4
|
+
* 解决的问题:一条长门禁/长构建会把整个回合占住。这里给 agent 两个工具:
|
|
5
|
+
* team_bg_run 起一个**脱离**子进程,立刻返回 job id + pid;输出落 <会话自己的根>/state/bg/<id>.log(有界,留尾段)
|
|
6
|
+
* team_bg_wait 收割:等到作业结束,把退出码 + 日志尾段内联返回;**收割过的作业完成时静默**
|
|
7
|
+
*
|
|
8
|
+
* 唤醒纪律(照 oh-my-pi issue #689 的两条教训,E8 探针实测过):
|
|
9
|
+
* - 已收割 → 静默:结果已经在 agent 手里,再叫它就是白烧一个回合;
|
|
10
|
+
* - 多条同时完成 → 合并成**一条** followUp 消息(triggerTurn),且只在 agent 空闲时投递。
|
|
11
|
+
*
|
|
12
|
+
* 生命周期(Pi 文档的硬约束):factory 里不起任何后台资源;日志裁剪定时器在第一次真正跑作业时才建、
|
|
13
|
+
* session_shutdown 里清掉。作业是 detached 的:session 重启/退出**不杀**它们,日志与账本留在 state/。
|
|
14
|
+
*
|
|
15
|
+
* 作用域(M30 修正):本扩展的产物是**会话本地**的 —— 日志/账本跟着会话自己的工作树走
|
|
16
|
+
* (见 findRoot 的注释);收件箱/投递那类**接口**状态不归它管(那是 inbox-watch 的活,它待在主工作树)。
|
|
17
|
+
* 只认本扩展自己 job 表里的作业(不订阅任何第三方包的事件总线);只服务 dispatch/PM 启动链注入 `-e`
|
|
18
|
+
* 的团队会话 —— 用户自己的 pi 会话没有它。
|
|
19
|
+
*/
|
|
20
|
+
import type { ExtensionAPI } from '@earendil-works/pi-coding-agent'
|
|
21
|
+
import { execFileSync, spawn } from 'node:child_process'
|
|
22
|
+
import { appendFileSync, closeSync, existsSync, mkdirSync, openSync, readFileSync, readSync, statSync, writeFileSync } from 'node:fs'
|
|
23
|
+
import { dirname, join, resolve } from 'node:path'
|
|
24
|
+
|
|
25
|
+
type Job = {
|
|
26
|
+
id: string
|
|
27
|
+
pid: number
|
|
28
|
+
command: string
|
|
29
|
+
cwd: string
|
|
30
|
+
log: string
|
|
31
|
+
startedAt: number
|
|
32
|
+
done: boolean
|
|
33
|
+
exitCode: number | null
|
|
34
|
+
harvested: boolean // 结果已经交到 agent 手里(或正在等它)
|
|
35
|
+
notified: boolean // 完成通知已经投递过(一条通知最多一次)
|
|
36
|
+
waiters: Array<() => void>
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const DEFAULT_MAX_BYTES = 512 * 1024 // 每个作业日志的磁盘上限(可被 TEAM_BG_LOG_MAX_BYTES 覆盖)
|
|
40
|
+
const TRIM_EVERY_MS = 2000
|
|
41
|
+
const MERGE_WINDOW_MS = 250 // 同一拍完成的作业合并成一条通知
|
|
42
|
+
const DEFAULT_WAIT_MS = 15 * 60 * 1000
|
|
43
|
+
const TAIL_BYTES = 8 * 1024
|
|
44
|
+
const TRUNC_MARK = '[team-bg] truncated: output exceeded the log cap, earlier output dropped.'
|
|
45
|
+
const CMD_DISPLAY_MAX = 100 // 结果文本里命令的码点上限(超出 → 前 100 码点 + …)
|
|
46
|
+
const CMD_ELLIPSIS = '…'
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 定位**本会话自己的根**:team-bg 的产物(作业日志、账本、合并窗口)都是会话本地的,
|
|
50
|
+
* 必须落在会话自己的工作树里 —— worktree 会话 = 它的 worktree,主工作树会话 = 主工作树。
|
|
51
|
+
*
|
|
52
|
+
* 为什么不用 `--git-common-dir`(M27 的旧写法,M30 修正):那个路径指向**主工作树**(linked worktree 的
|
|
53
|
+
* .git 是主仓库里的一个文件),于是 worker 在 worktree 里跑的长门禁日志会被写进共享账本所在地
|
|
54
|
+
* `.pi/team/state/bg/`(M30 现场:门禁自己的 stdout 里带着各段夹具的名字,把「把门禁放后台跑」变成了
|
|
55
|
+
* 下一次 M16 隔离断言的假泄漏)。会话本地的产物写进自己的工作树,也符合「只改自己的目录」。
|
|
56
|
+
*
|
|
57
|
+
* 顺序:会话 cwd 的工作树(有 config.sh)> TEAM_ROOT(显式)> 向上查找 > git 主工作树(最后的兜底:
|
|
58
|
+
* 工作树里没有 config.sh 的旧分支/目录在项目外时,宁可写共享根,也不要让工具直接不可用 ——
|
|
59
|
+
* `team_bg_run` 的返回里点明了 log 的真实路径)。TEAM_STATE_DIR 仍然压过这一切。
|
|
60
|
+
*/
|
|
61
|
+
function findRoot(cwd: string): string {
|
|
62
|
+
try {
|
|
63
|
+
const top = execFileSync('git', ['-C', cwd, 'rev-parse', '--path-format=absolute', '--show-toplevel'],
|
|
64
|
+
{ encoding: 'utf8', timeout: 5000 }).trim()
|
|
65
|
+
if (top && existsSync(join(top, '.pi/team/config.sh'))) return resolve(top)
|
|
66
|
+
} catch {
|
|
67
|
+
/* 不是 git 仓库 / 没有 git */
|
|
68
|
+
}
|
|
69
|
+
const envRoot = process.env.TEAM_ROOT
|
|
70
|
+
if (envRoot && existsSync(join(envRoot, '.pi/team/config.sh'))) return resolve(envRoot)
|
|
71
|
+
let dir = resolve(cwd)
|
|
72
|
+
for (;;) {
|
|
73
|
+
if (existsSync(join(dir, '.pi/team/config.sh'))) return dir
|
|
74
|
+
const parent = dirname(dir)
|
|
75
|
+
if (parent === dir) break
|
|
76
|
+
dir = parent
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
const out = execFileSync('git', ['-C', cwd, 'rev-parse', '--path-format=absolute', '--git-common-dir'],
|
|
80
|
+
{ encoding: 'utf8', timeout: 5000 })
|
|
81
|
+
const main = dirname(out.trim())
|
|
82
|
+
if (existsSync(join(main, '.pi/team/config.sh'))) return main
|
|
83
|
+
} catch {
|
|
84
|
+
/* ignore */
|
|
85
|
+
}
|
|
86
|
+
return ''
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function stateDir(root: string): string {
|
|
90
|
+
return process.env.TEAM_STATE_DIR || join(root, '.pi/team/state')
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function maxBytes(): number {
|
|
94
|
+
const n = Number(process.env.TEAM_BG_LOG_MAX_BYTES ?? NaN)
|
|
95
|
+
return Number.isFinite(n) && n >= 256 ? n : DEFAULT_MAX_BYTES
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function appendLedger(root: string, line: string): void {
|
|
99
|
+
try {
|
|
100
|
+
mkdirSync(stateDir(root), { recursive: true })
|
|
101
|
+
appendFileSync(join(stateDir(root), 'bg.log'), `${new Date().toISOString()} ${line}\n`)
|
|
102
|
+
} catch {
|
|
103
|
+
/* 账本绝不能影响会话 */
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** 读文件尾段(最多 limit 字节);truncated = 前面还有没读到的内容。 */
|
|
108
|
+
function tailOf(file: string, limit: number): { text: string; truncated: boolean } {
|
|
109
|
+
try {
|
|
110
|
+
const size = statSync(file).size
|
|
111
|
+
const start = Math.max(0, size - limit)
|
|
112
|
+
const fd = openSync(file, 'r')
|
|
113
|
+
try {
|
|
114
|
+
const buf = Buffer.alloc(size - start)
|
|
115
|
+
readSync(fd, buf, 0, buf.length, start)
|
|
116
|
+
const text = buf.toString('utf8')
|
|
117
|
+
return { text, truncated: start > 0 || text.includes(TRUNC_MARK) }
|
|
118
|
+
} finally {
|
|
119
|
+
closeSync(fd)
|
|
120
|
+
}
|
|
121
|
+
} catch {
|
|
122
|
+
return { text: '', truncated: false }
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** 日志超上限就地截断:保留尾段 + 一行标记(有界,且尾部信号不丢)。 */
|
|
127
|
+
function trimLog(file: string): void {
|
|
128
|
+
const cap = maxBytes()
|
|
129
|
+
try {
|
|
130
|
+
if (statSync(file).size <= cap) return
|
|
131
|
+
const { text } = tailOf(file, Math.max(1024, Math.floor(cap / 2)))
|
|
132
|
+
writeFileSync(file, `${TRUNC_MARK} cap=${cap}\n${text}`)
|
|
133
|
+
} catch {
|
|
134
|
+
/* ignore */
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function slug(raw: string): string {
|
|
139
|
+
const s = raw.replace(/[^A-Za-z0-9._-]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 32)
|
|
140
|
+
return s || 'job'
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* 结果文本里的命令行:**单行化**(换行/连串空白 → 一个空格)后**缩略**。
|
|
145
|
+
*
|
|
146
|
+
* 用户反馈(P33):结果只有 job id/pid/log,看不出「我到底跑的是什么命令」。所以两个工具都点名命令;
|
|
147
|
+
* 多行命令单行化后一眼能读,超过 100 个**码点**截断加 `…`。切法必须是**码点**(`Array.from`):
|
|
148
|
+
* 按字节切会造出非法 UTF-8(P28 的教训),按 UTF-16 单元切会把代理对(emoji/CJK 扩展区)劈成半个字符。
|
|
149
|
+
* 完整命令始终原样留在 `details.cmd` 里,回收割方/夹具使用。
|
|
150
|
+
*/
|
|
151
|
+
function displayCmd(raw: string): string {
|
|
152
|
+
const one = raw.replace(/\s+/g, ' ').trim()
|
|
153
|
+
const cps = Array.from(one)
|
|
154
|
+
return cps.length <= CMD_DISPLAY_MAX ? one : `${cps.slice(0, CMD_DISPLAY_MAX).join('')}${CMD_ELLIPSIS}`
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* /proc/<pid>/stat 的 (进程组, 启动时间指纹):第 5 与第 22 字段。
|
|
159
|
+
* comm 字段带空格/括号,所以按**最后一个** `)` 切开再数。读不到 → 两个都是空串
|
|
160
|
+
* (记录照写;`team bg stop` 拿空指纹做身份比对会拒 —— fail-closed 方向)。
|
|
161
|
+
*/
|
|
162
|
+
function procStat(pid: number): { pgid: string; start: string } {
|
|
163
|
+
try {
|
|
164
|
+
const stat = readFileSync(`/proc/${pid}/stat`, 'utf8')
|
|
165
|
+
const rest = stat.slice(stat.lastIndexOf(')') + 1).trim().split(/\s+/)
|
|
166
|
+
return { pgid: rest[2] ?? '', start: rest[19] ?? '' }
|
|
167
|
+
} catch {
|
|
168
|
+
return { pgid: '', start: '' }
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* state/bg/<id>.job:作业的身份记录(P159)。`team bg list|stop` 只读这里 —— 停作业用的是
|
|
174
|
+
* 记录里的 (pid, 启动时间指纹),绝不按名字/命令行/tree 找进程。在返回 job id **之前**写。
|
|
175
|
+
* 写不进去不抛(会话不能因此中断):没有记录 = 停不了,而那是 `team bg list` 里看得见的 fail-closed。
|
|
176
|
+
*/
|
|
177
|
+
function writeJobRecord(root: string, job: Job): void {
|
|
178
|
+
try {
|
|
179
|
+
const dir = join(stateDir(root), 'bg')
|
|
180
|
+
mkdirSync(dir, { recursive: true })
|
|
181
|
+
const { pgid, start } = procStat(job.pid)
|
|
182
|
+
const one = job.command.replace(/[\r\n]+/g, ' ')
|
|
183
|
+
writeFileSync(join(dir, `${job.id}.job`),
|
|
184
|
+
`id=${job.id}\npid=${job.pid}\npgid=${pgid}\nstart=${start}\ncwd=${job.cwd}\nlog=${job.log}\ncmd=${one}\n`)
|
|
185
|
+
} catch {
|
|
186
|
+
/* 记录写不进去 = 这个作业停不了(fail-closed,且 team bg list 里可见);作业本身照常跑 */
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export default function (pi: ExtensionAPI) {
|
|
191
|
+
const jobs = new Map<string, Job>()
|
|
192
|
+
let active = false // agent 正在跑(工具调用/流式输出)——完成通知要等它闲下来
|
|
193
|
+
let trimTimer: ReturnType<typeof setInterval> | null = null
|
|
194
|
+
let flushTimer: ReturnType<typeof setTimeout> | null = null
|
|
195
|
+
|
|
196
|
+
const stopTimers = (): void => {
|
|
197
|
+
if (trimTimer) { clearInterval(trimTimer); trimTimer = null }
|
|
198
|
+
if (flushTimer) { clearTimeout(flushTimer); flushTimer = null }
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** 把「已完成、未收割、未通知」的作业合并成一条消息投递;没有就什么都不做。 */
|
|
202
|
+
const flushPending = (root: string): void => {
|
|
203
|
+
const ready = [...jobs.values()].filter(j => j.done && !j.harvested && !j.notified)
|
|
204
|
+
if (!ready.length) return
|
|
205
|
+
for (const j of ready) j.notified = true
|
|
206
|
+
const lines = ready.map(j => `- ${j.id} exit=${j.exitCode} log=${j.log}`)
|
|
207
|
+
const text =
|
|
208
|
+
`[team-bg] ${ready.length} background job(s) finished and are not harvested yet:\n${lines.join('\n')}\n` +
|
|
209
|
+
'Run team_bg_wait <id> to harvest one (the result comes back inline; a harvested job never wakes you again).'
|
|
210
|
+
appendLedger(root, `wake count=${ready.length} ids=${ready.map(j => j.id).join(',')}`)
|
|
211
|
+
try {
|
|
212
|
+
pi.sendMessage({ customType: 'teamsmith-bg', content: text, display: true },
|
|
213
|
+
{ triggerTurn: true, deliverAs: 'followUp' })
|
|
214
|
+
} catch {
|
|
215
|
+
/* 会话正在退出等场合:账本已经记了,不抛 */
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const scheduleFlush = (root: string): void => {
|
|
220
|
+
if (flushTimer) return
|
|
221
|
+
flushTimer = setTimeout(() => { flushTimer = null; try { flushPending(root) } catch { /* ignore */ } }, MERGE_WINDOW_MS)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const finishJob = (root: string, job: Job, code: number | null): void => {
|
|
225
|
+
if (job.done) return
|
|
226
|
+
job.done = true
|
|
227
|
+
job.exitCode = code
|
|
228
|
+
trimLog(job.log)
|
|
229
|
+
for (const wake of job.waiters.splice(0)) { try { wake() } catch { /* ignore */ } }
|
|
230
|
+
if (job.harvested) return // #689 第 1 条:已收割 → 静默
|
|
231
|
+
if (!active) scheduleFlush(root) // 空闲:合并窗口结束就唤醒;正在跑则留给 agent_settled 的合并投递
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** 等作业结束;超时/被中断 → false(收割意图由调用方撤销,完成时照常唤醒)。 */
|
|
235
|
+
const waitForExit = (job: Job, timeout: number, signal: any): Promise<boolean> => {
|
|
236
|
+
if (job.done) return Promise.resolve(true)
|
|
237
|
+
return new Promise<boolean>(resolve => {
|
|
238
|
+
let settled = false
|
|
239
|
+
const waiter = (): void => finish(true)
|
|
240
|
+
const finish = (ok: boolean): void => {
|
|
241
|
+
if (settled) return
|
|
242
|
+
settled = true
|
|
243
|
+
if (timer) clearTimeout(timer)
|
|
244
|
+
try { signal?.removeEventListener?.('abort', onAbort) } catch { /* ignore */ }
|
|
245
|
+
job.waiters = job.waiters.filter(w => w !== waiter)
|
|
246
|
+
resolve(ok)
|
|
247
|
+
}
|
|
248
|
+
const onAbort = (): void => finish(false)
|
|
249
|
+
const timer = timeout > 0 ? setTimeout(() => finish(false), timeout) : null
|
|
250
|
+
job.waiters.push(waiter)
|
|
251
|
+
try { signal?.addEventListener?.('abort', onAbort, { once: true }) } catch { /* ignore */ }
|
|
252
|
+
})
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// ---- 工具 -----------------------------------------------------------------
|
|
256
|
+
pi.registerTool({
|
|
257
|
+
name: 'team_bg_run',
|
|
258
|
+
label: 'Team background job',
|
|
259
|
+
description:
|
|
260
|
+
'Start a long command (a gate, a build) as a detached background job and return immediately with a job id. ' +
|
|
261
|
+
'Output goes to state/bg/<id>.log (bounded). Harvest it with team_bg_wait before you end your turn; ' +
|
|
262
|
+
'otherwise the team wakes you once when it finishes.',
|
|
263
|
+
promptSnippet: 'team_bg_run: start a long command in the background (returns a job id immediately)',
|
|
264
|
+
promptGuidelines: [
|
|
265
|
+
'Use team_bg_run for long gates or builds, and harvest every job with team_bg_wait before ending your turn ' +
|
|
266
|
+
'(a turn must not end with unharvested background jobs).',
|
|
267
|
+
],
|
|
268
|
+
parameters: {
|
|
269
|
+
type: 'object',
|
|
270
|
+
properties: {
|
|
271
|
+
command: { type: 'string', description: 'Shell command to run (bash -c, with your environment and cwd)' },
|
|
272
|
+
name: { type: 'string', description: 'Short label used in the job id and the log path' },
|
|
273
|
+
cwd: { type: 'string', description: 'Working directory (defaults to the session cwd)' },
|
|
274
|
+
},
|
|
275
|
+
required: ['command'],
|
|
276
|
+
additionalProperties: false,
|
|
277
|
+
} as any,
|
|
278
|
+
async execute(_toolCallId: string, params: any, _signal: any, _onUpdate: any, ctx: any) {
|
|
279
|
+
const root = findRoot(String(ctx?.cwd ?? process.cwd()))
|
|
280
|
+
if (!root) throw new Error('team_bg_run: not a teamsmith project (no .pi/team/config.sh found)')
|
|
281
|
+
const command = String(params?.command ?? '')
|
|
282
|
+
if (!command.trim()) throw new Error('team_bg_run: empty command')
|
|
283
|
+
const cwd = resolve(String(params?.cwd || ctx?.cwd || process.cwd()))
|
|
284
|
+
const dir = join(stateDir(root), 'bg')
|
|
285
|
+
mkdirSync(dir, { recursive: true })
|
|
286
|
+
const base = slug(String(params?.name ?? 'job'))
|
|
287
|
+
let id = base
|
|
288
|
+
for (let i = 2; jobs.has(id); i++) id = `${base}-${i}`
|
|
289
|
+
const log = join(dir, `${id}.log`)
|
|
290
|
+
const fd = openSync(log, 'w')
|
|
291
|
+
let child: ReturnType<typeof spawn>
|
|
292
|
+
try {
|
|
293
|
+
child = spawn('bash', ['-c', command], { cwd, detached: true, stdio: ['ignore', fd, fd], env: process.env })
|
|
294
|
+
} finally {
|
|
295
|
+
closeSync(fd)
|
|
296
|
+
}
|
|
297
|
+
const job: Job = {
|
|
298
|
+
id, pid: child.pid ?? 0, command, cwd, log, startedAt: Date.now(),
|
|
299
|
+
done: false, exitCode: null, harvested: false, notified: false, waiters: [],
|
|
300
|
+
}
|
|
301
|
+
jobs.set(id, job)
|
|
302
|
+
if (job.pid > 0) writeJobRecord(root, job)
|
|
303
|
+
child.on('exit', (code: number | null) => finishJob(root, job, code))
|
|
304
|
+
child.on('error', (error: Error) => {
|
|
305
|
+
appendLedger(root, `spawn-error id=${id} ${String(error?.message ?? error)}`)
|
|
306
|
+
finishJob(root, job, -1)
|
|
307
|
+
})
|
|
308
|
+
child.unref()
|
|
309
|
+
if (!trimTimer) {
|
|
310
|
+
trimTimer = setInterval(() => { for (const j of jobs.values()) if (!j.done) trimLog(j.log) }, TRIM_EVERY_MS)
|
|
311
|
+
}
|
|
312
|
+
return {
|
|
313
|
+
content: [{ type: 'text', text: `job ${id} started (pid ${job.pid}); log: ${log}\n cmd: ${displayCmd(command)}\nHarvest it with team_bg_wait ${id}.` }],
|
|
314
|
+
details: { id, pid: job.pid, log, cwd, cmd: command },
|
|
315
|
+
}
|
|
316
|
+
},
|
|
317
|
+
} as any)
|
|
318
|
+
|
|
319
|
+
pi.registerTool({
|
|
320
|
+
name: 'team_bg_wait',
|
|
321
|
+
label: 'Team background job wait',
|
|
322
|
+
description: 'Wait for a job started by team_bg_run, harvest it and return its exit code plus the log tail inline.',
|
|
323
|
+
promptSnippet: 'team_bg_wait: harvest a team_bg_run job (waits if it is still running)',
|
|
324
|
+
parameters: {
|
|
325
|
+
type: 'object',
|
|
326
|
+
properties: {
|
|
327
|
+
id: { type: 'string', description: 'Job id returned by team_bg_run' },
|
|
328
|
+
timeout_ms: { type: 'number', description: `How long to wait in ms (default ${DEFAULT_WAIT_MS}; 0 = just report status)` },
|
|
329
|
+
},
|
|
330
|
+
required: ['id'],
|
|
331
|
+
additionalProperties: false,
|
|
332
|
+
} as any,
|
|
333
|
+
async execute(_toolCallId: string, params: any, signal: any) {
|
|
334
|
+
const job = jobs.get(String(params?.id ?? ''))
|
|
335
|
+
if (!job) {
|
|
336
|
+
const known = [...jobs.keys()].join(', ') || '(none)'
|
|
337
|
+
throw new Error(`team_bg_wait: unknown job "${String(params?.id ?? '')}" (this session started: ${known})`)
|
|
338
|
+
}
|
|
339
|
+
const timeout = Number.isFinite(Number(params?.timeout_ms))
|
|
340
|
+
? Math.max(0, Number(params.timeout_ms))
|
|
341
|
+
: DEFAULT_WAIT_MS
|
|
342
|
+
let running = false
|
|
343
|
+
if (!job.done && timeout > 0) {
|
|
344
|
+
job.harvested = true // 收割意图先落地:保证完成时静默(#689 第 1 条)
|
|
345
|
+
const exited = await waitForExit(job, timeout, signal)
|
|
346
|
+
if (!exited && !job.done) {
|
|
347
|
+
job.harvested = false // 没收成 → 撤销意图,完成时照常唤醒
|
|
348
|
+
running = true
|
|
349
|
+
}
|
|
350
|
+
} else if (!job.done) {
|
|
351
|
+
running = true
|
|
352
|
+
}
|
|
353
|
+
if (running) {
|
|
354
|
+
return {
|
|
355
|
+
content: [{ type: 'text', text: `job ${job.id} is still running (pid ${job.pid}); log: ${job.log}\n cmd: ${displayCmd(job.command)}` }],
|
|
356
|
+
details: { id: job.id, running: true, log: job.log, cmd: job.command },
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
job.harvested = true
|
|
360
|
+
const { text, truncated } = tailOf(job.log, TAIL_BYTES)
|
|
361
|
+
const secs = ((Date.now() - job.startedAt) / 1000).toFixed(1)
|
|
362
|
+
const body = truncated ? `${TRUNC_MARK}\n${text}` : text
|
|
363
|
+
return {
|
|
364
|
+
content: [{
|
|
365
|
+
type: 'text',
|
|
366
|
+
text: `job ${job.id} finished exit=${job.exitCode} after ${secs}s; log: ${job.log}\n cmd: ${displayCmd(job.command)}\n--- tail ---\n${body}`,
|
|
367
|
+
}],
|
|
368
|
+
details: { id: job.id, exitCode: job.exitCode, log: job.log, truncated, cmd: job.command },
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
} as any)
|
|
372
|
+
|
|
373
|
+
pi.on('session_start', async () => {
|
|
374
|
+
stopTimers()
|
|
375
|
+
jobs.clear()
|
|
376
|
+
active = false
|
|
377
|
+
})
|
|
378
|
+
|
|
379
|
+
pi.on('agent_start', async () => { active = true })
|
|
380
|
+
|
|
381
|
+
pi.on('agent_settled', async (_event: any, ctx: any) => {
|
|
382
|
+
active = false
|
|
383
|
+
try {
|
|
384
|
+
const root = findRoot(String(ctx?.cwd ?? process.cwd()))
|
|
385
|
+
if (!root) return
|
|
386
|
+
const open = [...jobs.values()].filter(j => !j.harvested)
|
|
387
|
+
const label = (j: Job): string => `${j.id}:${j.done ? `exit${j.exitCode}` : 'running'}`
|
|
388
|
+
appendLedger(root, `settled-with-unharvested=${open.length}${open.length ? ` jobs=${open.map(label).join(',')}` : ''}`)
|
|
389
|
+
flushPending(root)
|
|
390
|
+
} catch {
|
|
391
|
+
/* 账本/投递失败不能打断会话 */
|
|
392
|
+
}
|
|
393
|
+
})
|
|
394
|
+
|
|
395
|
+
pi.on('session_shutdown', async () => {
|
|
396
|
+
stopTimers()
|
|
397
|
+
jobs.clear() // 作业本身是 detached 的:不杀;日志与账本留在 state/ 里
|
|
398
|
+
})
|
|
399
|
+
}
|