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