@andares/pi-toolkits 0.11.3 → 0.12.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/README.md +79 -3
- package/README.zh-CN.md +48 -3
- package/package.json +1 -1
- package/src/features/compact/compact.test.ts +5 -5
- package/src/features/compact/index.ts +9 -8
- package/src/features/init/apply.test.ts +159 -3
- package/src/features/init/apply.ts +99 -1
- package/src/features/init/detect.test.ts +1 -2
- package/src/features/init/detect.ts +1 -3
- package/src/features/init/dispatch.test.ts +42 -0
- package/src/features/init/dispatch.ts +0 -3
- package/src/features/init/index.test.ts +41 -0
- package/src/features/init/index.ts +30 -9
- package/src/features/init/plan.ts +0 -2
- package/src/features/init/render.ts +2 -2
- package/src/features/init/spec.ts +5 -3
- package/src/features/init/types.ts +0 -1
- package/src/features/quit-guard/constants.ts +60 -0
- package/src/features/quit-guard/guard.ts +214 -0
- package/src/features/quit-guard/index.ts +292 -0
- package/src/features/quit-guard/quit-guard.test.ts +736 -0
- package/src/features/quit-guard/report.ts +212 -0
- package/src/features/quit-guard/store.ts +144 -0
- package/src/features/review/index.ts +11 -10
- package/src/features/review/prompt.ts +20 -15
- package/src/features/review/review.test.ts +9 -9
- package/src/index.ts +3 -0
- package/src/lib/approval-log.ts +8 -1
- package/src/lib/model-picker.test.ts +6 -4
- package/src/lib/model-picker.ts +3 -2
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
[](https://github.com/earendil-works/pi)
|
|
14
14
|
[](https://github.com/andares/pi-toolkits)
|
|
15
15
|
|
|
16
|
-
In a nutshell:
|
|
16
|
+
In a nutshell: nine ready-to-use capabilities — **read-only consult mode** `ask`, **session-scoped boundary permission** `ask-perm`, **model favorites** `favorites`, **prompt stash** `stash`, **third-party code review** `third-review`, **dedicated compaction model** `compact-model`, **session startup automation** `autostart`, an **agent memory framework** `ask-init`, and a **quit watchdog** `quit-guard` — with more to come.
|
|
17
17
|
|
|
18
18
|
**Requirements**: pi `>=0.99.0`. Every published pi release from 0.99.0 through 1.5.0 has been verified with the full gate (install → `tsc --noEmit` → full test suite → keybinding audit → hash compare of the host files this package patches), each in a sandbox with the whole `@earendil-works/*` family pinned to the version under test. Newer releases are verified as they ship and the floor moves only when the measurement says so; `pnpm check:compat` reproduces the result. Note: pi ≥ 0.99 hosts require host-provided packages to be declared in `peerDependencies` with a `"*"` range — `@earendil-works/pi-tui` and `typebox` (the `scratchpad` tool builds a TypeBox schema) are declared that way; an installed copy in `dependencies` would make pi warn.
|
|
19
19
|
|
|
@@ -57,6 +57,7 @@ After installing, verify in any pi session:
|
|
|
57
57
|
- [🗜️ compact — dedicated compaction model](#compact)
|
|
58
58
|
- [🚀 autostart — session startup automation](#autostart)
|
|
59
59
|
- [🧠 init — agent memory framework](#init)
|
|
60
|
+
- [🧯 quit-guard — quit watchdog](#quit-guard)
|
|
60
61
|
- [Development](#development)
|
|
61
62
|
- [Contributing](#contributing)
|
|
62
63
|
- [License](#license)
|
|
@@ -77,6 +78,7 @@ After installing, verify in any pi session:
|
|
|
77
78
|
| 🗜️ **compact** dedicated compaction model | `/compact-model` | give `/compact` and auto-compact a cheap fast model to summarize with, leaving the main model free to focus on the conversation; zero overhead when unconfigured, falls back to pi's default on any failure |
|
|
78
79
|
| 🚀 **autostart** session startup automation | `.pi/autostart.toml` | declare slash commands to run automatically at session start, per project (first use case: mounting multi-repo directories with `/add-dir`); never runs in untrusted directories, unregistered commands are skipped instead of being sent to the model |
|
|
79
80
|
| 🧠 **init** agent memory framework | `/ask-init` | install or adopt a fixed memory layout (`AGENTS.md` + `specs/` + `.agents/memory` + `.agents/implementations`), migrate an existing one with a snapshot and a coverage table, and audit it later for drift, budgets and duplicated facts |
|
|
81
|
+
| 🧯 **quit-guard** quit watchdog | `/quit-guard` | force-exit when pi's shutdown hangs after the TUI is gone (an extension `session_shutdown` that never settles), write a hang report, and give the shell prompt back; TUI restarts and Ctrl+Z resume disarm it |
|
|
80
82
|
|
|
81
83
|
---
|
|
82
84
|
|
|
@@ -399,7 +401,7 @@ repository root stays readable: `AGENTS.md`, `specs/`, `design/`, `docs/`,
|
|
|
399
401
|
| `/ask-init check` | audit the installed framework (read-only; writes only a runtime report under `.agents/framework/reports/`) |
|
|
400
402
|
| `/ask-init roots` | sync the directories added with `pi-add-dir` into the framework state |
|
|
401
403
|
| `/ask-init --yes` | skip the confirmation panel |
|
|
402
|
-
| `/ask-init --lang zh\|en` | language for generated
|
|
404
|
+
| `/ask-init --lang zh\|en` | rendering language for framework-generated files (per run; default: detected from the project's own docs) |
|
|
403
405
|
|
|
404
406
|
**What the mechanical half guarantees**:
|
|
405
407
|
|
|
@@ -445,12 +447,84 @@ root entry must stay a **scope card** (≤30 lines: what lives there, its comman
|
|
|
445
447
|
which main-repo memory file describes it) — never a second copy of the rules.
|
|
446
448
|
|
|
447
449
|
The framework contract shipped into a project is English (it is read by models and
|
|
448
|
-
published);
|
|
450
|
+
published); framework-generated files are rendered in that project's language. The
|
|
451
|
+
package never directs the model which language to write or reply in.
|
|
449
452
|
|
|
450
453
|
> **After the run**, `/reload` so the new `AGENTS.md` enters the prompt. The
|
|
451
454
|
> framework version lives in `.agents/framework/state.toml`; a project whose state
|
|
452
455
|
> version is newer than the installed tool is never migrated.
|
|
453
456
|
|
|
457
|
+
## 🧯 quit-guard — quit watchdog
|
|
458
|
+
|
|
459
|
+
<a name="quit-guard"></a>
|
|
460
|
+
|
|
461
|
+
**The problem.** pi's interactive `/quit` stops the TUI and then awaits every extension's
|
|
462
|
+
`session_shutdown` handler, one after another, **with no timeout**
|
|
463
|
+
(`runtimeHost.dispose()` → `emitSessionShutdownEvent()`; upstream
|
|
464
|
+
[issue #9997](https://github.com/earendil-works/pi/issues/9997), still unbounded in 1.5.0 and on
|
|
465
|
+
`main`). One handler that never settles leaves pi alive at 0% CPU *after* the terminal has
|
|
466
|
+
already been restored and the final frame reprinted: the screen looks like a frozen "exit
|
|
467
|
+
screen", and the shell prompt never comes back. Ctrl+C and SIGTERM did not end the hung process
|
|
468
|
+
in our measurements (SIGKILL did); once that phase starts, the host has no way out of it.
|
|
469
|
+
Measured with a 20-extension setup (2026-11-05): a healthy quit spends 0.1–3 s in that phase,
|
|
470
|
+
`pi -ne` 0.12 s — and one of three quits with the same setup never finished at all.
|
|
471
|
+
|
|
472
|
+
**What this does.** The guard watches the terminal instead of the event stream, so it still
|
|
473
|
+
works when the handler that hangs was loaded *before* this package (in that case our own
|
|
474
|
+
`session_shutdown` would never run). It arms on the TUI's teardown sequence
|
|
475
|
+
(`\x1b[?2004l` / `\x1b[?1049l`); if the process is still alive `timeoutMs` later, it writes a
|
|
476
|
+
hang report and forces the exit. A TUI that comes back disarms it — an interactive mode switch
|
|
477
|
+
(`/settings` → TUI mode) and Ctrl+Z suspend/resume both re-enter the TUI, and a live session
|
|
478
|
+
must never be killed.
|
|
479
|
+
|
|
480
|
+
### Commands
|
|
481
|
+
|
|
482
|
+
| Command | Effect |
|
|
483
|
+
| --- | --- |
|
|
484
|
+
| `/quit-guard` | status: on/off, timeout, report on/off, newest hang report |
|
|
485
|
+
| `/quit-guard on` / `/quit-guard off` | enable/disable, persisted |
|
|
486
|
+
| `/quit-guard <ms>` | set the deadline (clamped to 2000–60000 ms), persisted |
|
|
487
|
+
|
|
488
|
+
**Configuration** — `<agent-dir>/pi-toolkits-quit-guard.json`, written by the commands; every
|
|
489
|
+
field is optional:
|
|
490
|
+
|
|
491
|
+
```json
|
|
492
|
+
{
|
|
493
|
+
"enabled": true,
|
|
494
|
+
"timeoutMs": 10000,
|
|
495
|
+
"report": true
|
|
496
|
+
}
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
`timeoutMs` counts **from the teardown write**, not from the moment you press Enter, so the time
|
|
500
|
+
the TUI spends stopping is not charged against a slow-but-finishing shutdown. Keep it below your
|
|
501
|
+
slowest healthy quit; 10 s leaves a wide margin (the slowest healthy quit measured above was 5 s
|
|
502
|
+
end to end, of which ~2 s came after the teardown).
|
|
503
|
+
|
|
504
|
+
**Hang reports** — `<agent-dir>/quit-guard/quit-hang-<timestamp>-<pid>.json` records the
|
|
505
|
+
arm/fire timing, the teardown marker, and the process state at the deadline (`activeHandles`,
|
|
506
|
+
`activeRequests`, and the host's libuv handle table). An empty `libuv` table next to a live
|
|
507
|
+
process is the signature of a promise nobody will ever resolve, as opposed to a blocked socket
|
|
508
|
+
or a lingering child process. The file is owner-only (0600 in an 0700 directory) and process
|
|
509
|
+
arguments are redacted with the same rules as the approval log, so `pi --api-key …` does not
|
|
510
|
+
leave the key on disk.
|
|
511
|
+
|
|
512
|
+
`PI_QUIT_GUARD_DEBUG=<file>` appends one line per `arm` / `disarm` / `fire` (plus one per
|
|
513
|
+
extension registration); once the TUI is gone it is the only channel left.
|
|
514
|
+
|
|
515
|
+
### Boundaries
|
|
516
|
+
|
|
517
|
+
- Interactive TUI only. Print (`-p`), JSON, and RPC sessions never write the teardown
|
|
518
|
+
sequences, so the guard stays inert there.
|
|
519
|
+
- The deadline covers the **post-teardown** phase. A shutdown that hangs before the TUI stops
|
|
520
|
+
(not observed; `drainInput` is bounded at 1 s) is out of scope.
|
|
521
|
+
- When it fires, the remaining extension cleanup is skipped **by design** — that cleanup is what
|
|
522
|
+
was hanging. Session entries are appended to the session JSONL as they happen, so the
|
|
523
|
+
transcript is intact up to the last written entry. The process exits 0; if `process.exit()`
|
|
524
|
+
itself is turned into a no-op by an `exit` listener, a 500 ms fallback sends `SIGKILL`.
|
|
525
|
+
- This is a workaround for a host gap. If pi ever bounds `session_shutdown`, the guard becomes
|
|
526
|
+
a no-op safety net and can be removed.
|
|
527
|
+
|
|
454
528
|
---
|
|
455
529
|
|
|
456
530
|
## 🛠️ Development
|
|
@@ -493,6 +567,8 @@ src/
|
|
|
493
567
|
├── permission/ # session-scoped boundary permission: modes, panel,
|
|
494
568
|
│ # bash units, policy, session grants, prompt,
|
|
495
569
|
│ # audit payload, tests
|
|
570
|
+
├── quit-guard/ # quit watchdog: terminal-teardown trigger, settings
|
|
571
|
+
│ # store, hang report, tests
|
|
496
572
|
├── review/ # third-party review: input/command triggers,
|
|
497
573
|
│ # review-model config store, prompt, tests
|
|
498
574
|
└── stash/ # prompt stash: hotkey state machine + tests
|
package/README.zh-CN.md
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
[](https://github.com/earendil-works/pi)
|
|
14
14
|
[](https://github.com/andares/pi-toolkits)
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
一句话介绍:目前包含九个开箱即用的功能——**只读咨询模式** `ask`、**会话级边界权限** `ask-perm`、**模型收藏** `favorites`、**提示词暂存** `stash`、**第三方代码评审** `third-review`、**独立压缩模型** `compact-model`、**会话启动自动化** `autostart`、**agent 记忆框架** `ask-init`、**退出看门狗** `quit-guard`,后续持续追加。
|
|
17
17
|
|
|
18
18
|
**版本要求**:pi `>=0.99.0`。0.99.0 到 1.5.0 的每一个已发布版本都用完整门禁实测过(install → `tsc --noEmit` → 全量测试 → 键位审计 → 对 patch 目标宿主文件做哈希比对),每个版本都在沙箱中把整个 `@earendil-works/*` 家族钉在同一版本。更新的版本随发布逐个验证,下限只在实测结果允许时移动;`pnpm check:compat` 可复现该结论。注:pi ≥ 0.99 的宿主要求「宿主提供的包」一律声明为 peerDependencies `"*"` —— `@earendil-works/pi-tui` 与 `typebox`(ask 的 `scratchpad` 用它构造参数 schema)都按此声明;若放进 `dependencies`,pi 会告警。
|
|
19
19
|
|
|
@@ -57,6 +57,7 @@ pi -e ./src/index.ts
|
|
|
57
57
|
- [🗜️ compact — 独立压缩模型](#compact)
|
|
58
58
|
- [🚀 autostart — 会话启动自动化](#autostart)
|
|
59
59
|
- [🧠 init — agent 记忆框架](#init)
|
|
60
|
+
- [🧯 quit-guard — 退出看门狗](#quit-guard)
|
|
60
61
|
- [开发](#development)
|
|
61
62
|
- [贡献](#contributing)
|
|
62
63
|
- [许可](#license)
|
|
@@ -77,6 +78,7 @@ pi -e ./src/index.ts
|
|
|
77
78
|
| 🗜️ **compact** 独立压缩模型 | `/compact-model` | 给 `/compact` 与 auto-compact 配一个便宜快模型做总结,主模型专注对话;未配置零开销、任何异常回落 pi 默认 |
|
|
78
79
|
| 🚀 **autostart** 会话启动自动化 | `.pi/autostart.toml` | 项目内声明启动时自动执行的斜杠命令(首批用例:`/add-dir` 多仓库挂载);未信任不执行、未注册命令跳过不发给模型 |
|
|
79
80
|
| 🧠 **init** agent 记忆框架 | `/ask-init` | 安装或接管一套固定的记忆结构(`AGENTS.md` + `specs/` + `.agents/memory` + `.agents/implementations`),带快照与覆盖表迁移旧文档,之后可随时审计漂移、预算与重复事实 |
|
|
81
|
+
| 🧯 **quit-guard** 退出看门狗 | `/quit-guard` | TUI 已停、进程却卡在扩展 `session_shutdown` 上时,超时后写诊断报告并强制退出,把命令行提示符还给你;TUI 重启(切换显示模式)与 Ctrl+Z 恢复都会解除看守 |
|
|
80
82
|
|
|
81
83
|
---
|
|
82
84
|
|
|
@@ -397,7 +399,7 @@ run = [
|
|
|
397
399
|
| `/ask-init check` | 审计已装的框架(只读;只在 `.agents/framework/reports/` 留下运行期报告) |
|
|
398
400
|
| `/ask-init roots` | 把 `pi-add-dir` 添加的目录同步进框架状态 |
|
|
399
401
|
| `/ask-init --yes` | 跳过确认面板 |
|
|
400
|
-
| `/ask-init --lang zh\|en` |
|
|
402
|
+
| `/ask-init --lang zh\|en` | 框架生成文件的渲染语言(单次生效;默认按项目自身文档判定) |
|
|
401
403
|
|
|
402
404
|
**机械那一半保证什么**:
|
|
403
405
|
|
|
@@ -430,11 +432,52 @@ run = [
|
|
|
430
432
|
全文注入每次请求,所以根里的入口只能是 **scope card**(≤30 行:这里放什么、它的命令、
|
|
431
433
|
去主仓哪个记忆文件读),绝不能是规则的第二份副本。
|
|
432
434
|
|
|
433
|
-
装进项目的框架契约用英文(给模型读、也随 npm 发布)
|
|
435
|
+
装进项目的框架契约用英文(给模型读、也随 npm 发布);框架生成的文件按项目语言渲染。
|
|
436
|
+
本包不会指示模型用哪种语言写文件或回复。
|
|
434
437
|
|
|
435
438
|
> **跑完记得 `/reload`**,让新的 `AGENTS.md` 进入提示词。框架版本记在
|
|
436
439
|
> `.agents/framework/state.toml`;若项目的状态版本比当前工具新,则拒绝迁移。
|
|
437
440
|
|
|
441
|
+
## 🧯 quit-guard — 退出看门狗
|
|
442
|
+
|
|
443
|
+
<a name="quit-guard"></a>
|
|
444
|
+
|
|
445
|
+
**要解决的问题**。pi 的 `/quit` 先停 TUI,然后**逐个 await** 每个扩展的 `session_shutdown` 处理器,且**没有任何超时**(`runtimeHost.dispose()` → `emitSessionShutdownEvent()`;上游
|
|
446
|
+
[issue #9997](https://github.com/earendil-works/pi/issues/9997),1.5.0 与 `main` 至今未修)。只要有一个处理器永不 settle,pi 就会在终端已还原、最后一帧已重绘之后,以 0% CPU 永远挂着:画面看起来像「停在退出界面」,提示符再也不回来;实测 Ctrl+C 与 SIGTERM 都无法结束这个进程(只有 SIGKILL 有效)——进了这段收尾,宿主自己也没有出口。20 个扩展的环境实测(2026-11-05):健康退出在这段收尾上花 0.1–3 秒,`pi -ne` 0.12 秒;同一环境三次 `/quit` 就有一次**永远不结束**。
|
|
447
|
+
|
|
448
|
+
**这个功能做什么**。看守盯的是**终端写出的收尾序列**,而不是事件流——所以即使卡死的处理器比本包**先**加载(那样我们自己的 `session_shutdown` 根本不会运行),它照样能触发。它在 TUI 收尾序列(`\x1b[?2004l` / `\x1b[?1049l`)出现时开始计时;若 `timeoutMs` 之后进程还活着,就写一份诊断报告并强制退出。TUI **回来**时自动解除:`/settings` 里切换 TUI 显示模式、Ctrl+Z 挂起后 `fg` 恢复,都会让 TUI 重新进入,绝不能误杀活着的会话。
|
|
449
|
+
|
|
450
|
+
### 命令
|
|
451
|
+
|
|
452
|
+
| 命令 | 作用 |
|
|
453
|
+
| --- | --- |
|
|
454
|
+
| `/quit-guard` | 查看状态:开关、超时、报告开关、最近一份诊断报告 |
|
|
455
|
+
| `/quit-guard on` / `/quit-guard off` | 开启/关闭,并持久化 |
|
|
456
|
+
| `/quit-guard <ms>` | 设置超时(钳制在 2000–60000 ms),并持久化 |
|
|
457
|
+
|
|
458
|
+
**配置** —— `<agent-dir>/pi-toolkits-quit-guard.json`,由命令写入;所有字段都可省略:
|
|
459
|
+
|
|
460
|
+
```json
|
|
461
|
+
{
|
|
462
|
+
"enabled": true,
|
|
463
|
+
"timeoutMs": 10000,
|
|
464
|
+
"report": true
|
|
465
|
+
}
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
`timeoutMs` 从**收尾序列写出**那一刻开始算,不是从你按下 Enter 开始算——TUI 自己停下来的时间不计入,所以「慢但能完成」的收尾(记忆库 flush、MCP 关闭)不会被误杀。把它设在「你最慢的健康退出」之上;10 秒余量很大(上例最慢的健康退出端到端 5 秒,其中约 2 秒发生在收尾之后)。
|
|
469
|
+
|
|
470
|
+
**诊断报告** —— `<agent-dir>/quit-guard/quit-hang-<时间戳>-<pid>.json`,记录开始计时/触发的时间、触发用的收尾序列,以及截止时进程的状态(`activeHandles`、`activeRequests`、宿主 libuv 句柄表)。**进程还活着但 `libuv` 表是空的**,就是「有个 Promise 永远不会 settle」的特征,区别于阻塞的 socket 或残留子进程。报告文件**仅本人可读**(0700 目录下的 0600 文件),进程参数按审批日志同一套规则脱敏,所以 `pi --api-key …` 不会把密钥留在盘上。
|
|
471
|
+
|
|
472
|
+
`PI_QUIT_GUARD_DEBUG=<文件>` 会把每次 `arm` / `disarm` / `fire`(以及每次扩展注册)追加一行;TUI 已经没了的时候,这是唯一还能观察的通道。
|
|
473
|
+
|
|
474
|
+
### 边界
|
|
475
|
+
|
|
476
|
+
- 只覆盖交互式 TUI。`-p`(print)、JSON、RPC 模式不会写这些收尾序列,看守在那里完全不生效。
|
|
477
|
+
- 计时覆盖的是**收尾之后**的阶段;在 TUI 停下之前就卡住的情况(未观察到;`drainInput` 上限 1 秒)不在范围内。
|
|
478
|
+
- 触发时**按设计跳过**剩余扩展清理——挂住的就是它。会话条目是随发生随写入 JSONL 的,所以最后一条已写入的记录不会丢。正常路径退出码 0;若 `process.exit()` 本身被 `exit` 监听器变成空操作,500 ms 后会补一记 `SIGKILL`。
|
|
479
|
+
- 这是对宿主缺口的权宜之计:哪天 pi 给 `session_shutdown` 加上限时,看守就退化成无害的保险,可以直接删掉。
|
|
480
|
+
|
|
438
481
|
---
|
|
439
482
|
|
|
440
483
|
## 🛠️ 开发
|
|
@@ -476,6 +519,8 @@ src/
|
|
|
476
519
|
├── permission/ # 会话级边界权限:模式、面板、bash 拆解、
|
|
477
520
|
│ # 策略、会话授权、确认对话框、
|
|
478
521
|
│ # 审计 payload、测试
|
|
522
|
+
├── quit-guard/ # 退出看门狗:终端收尾触发、配置存储、
|
|
523
|
+
│ # 诊断报告、测试
|
|
479
524
|
├── review/ # 第三方评审:输入/指令触发、
|
|
480
525
|
│ # 评审模型配置存储、提示词、测试
|
|
481
526
|
└── stash/ # 提示词暂存:热键状态机 + 测试
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@andares/pi-toolkits",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.1",
|
|
4
4
|
"description": "Integrated toolkit extension for pi — ask mode (read-only Q&A), session-scoped permission checks, model favorites, prompt stash, third-party code review, session autostart, a dedicated compaction model, and an agent memory framework (/ask-init)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -296,7 +296,7 @@ describe("pickCompactModel", () => {
|
|
|
296
296
|
const { ctx, notify } = fakeCtx({ models: [] });
|
|
297
297
|
await pickCompactModel(ctx, getCompactStore());
|
|
298
298
|
expect(notify).toHaveBeenCalledWith(
|
|
299
|
-
expect.stringContaining("
|
|
299
|
+
expect.stringContaining("No models are available"),
|
|
300
300
|
"error",
|
|
301
301
|
);
|
|
302
302
|
});
|
|
@@ -308,7 +308,7 @@ describe("pickCompactModel", () => {
|
|
|
308
308
|
expect(model).toBeUndefined();
|
|
309
309
|
expect(getCompactStore().getModel()).toBe("google/gemini-2.5-flash");
|
|
310
310
|
expect(notify).toHaveBeenCalledWith(
|
|
311
|
-
expect.stringContaining("
|
|
311
|
+
expect.stringContaining("set model to null"),
|
|
312
312
|
"info",
|
|
313
313
|
);
|
|
314
314
|
});
|
|
@@ -326,7 +326,7 @@ describe("pickCompactModel", () => {
|
|
|
326
326
|
await pickCompactModel(ctx, getCompactStore());
|
|
327
327
|
expect(getCompactStore().getModel()).toBe("google/gemini-2.5-flash");
|
|
328
328
|
expect(notify).toHaveBeenCalledWith(
|
|
329
|
-
expect.stringContaining("
|
|
329
|
+
expect.stringContaining("cancelled"),
|
|
330
330
|
"info",
|
|
331
331
|
);
|
|
332
332
|
});
|
|
@@ -336,7 +336,7 @@ describe("pickCompactModel", () => {
|
|
|
336
336
|
const { ctx, notify } = fakeCtx({ customResult: MODELS[0] });
|
|
337
337
|
await pickCompactModel(ctx, getCompactStore());
|
|
338
338
|
expect(notify).toHaveBeenCalledWith(
|
|
339
|
-
expect.stringContaining("
|
|
339
|
+
expect.stringContaining("does not exist or is unavailable"),
|
|
340
340
|
"warning",
|
|
341
341
|
);
|
|
342
342
|
expect(getCompactStore().getModel()).toBe("google/gemini-2.5-flash");
|
|
@@ -451,7 +451,7 @@ describe("session_before_compact handler", () => {
|
|
|
451
451
|
)) as SessionBeforeCompactResult | undefined;
|
|
452
452
|
expect(result).toBeUndefined();
|
|
453
453
|
expect(notify).toHaveBeenCalledWith(
|
|
454
|
-
expect.stringContaining("
|
|
454
|
+
expect.stringContaining("came back empty"),
|
|
455
455
|
"warning",
|
|
456
456
|
);
|
|
457
457
|
});
|
|
@@ -217,7 +217,7 @@ async function runCompactModelCompaction(
|
|
|
217
217
|
if (!summaryText.trim()) {
|
|
218
218
|
if (!signal.aborted) {
|
|
219
219
|
ctx.ui.notify(
|
|
220
|
-
"compact-model:
|
|
220
|
+
"compact-model: the summary came back empty — falling back to pi's default compaction.",
|
|
221
221
|
"warning",
|
|
222
222
|
);
|
|
223
223
|
}
|
|
@@ -244,7 +244,7 @@ async function runCompactModelCompaction(
|
|
|
244
244
|
const message =
|
|
245
245
|
error instanceof Error ? error.message : String(error);
|
|
246
246
|
ctx.ui.notify(
|
|
247
|
-
`compact-model:
|
|
247
|
+
`compact-model: summarization failed (${message}) — falling back to pi's default compaction.`,
|
|
248
248
|
"error",
|
|
249
249
|
);
|
|
250
250
|
return undefined;
|
|
@@ -261,7 +261,7 @@ export async function pickCompactModel(
|
|
|
261
261
|
): Promise<AvailableModel | undefined> {
|
|
262
262
|
if (!ctx.hasUI) {
|
|
263
263
|
ctx.ui.notify(
|
|
264
|
-
"compact-model:
|
|
264
|
+
"compact-model: this environment has no model picker dialog — edit <agent-dir>/pi-toolkits-compact.json to configure it manually.",
|
|
265
265
|
"error",
|
|
266
266
|
);
|
|
267
267
|
return undefined;
|
|
@@ -270,16 +270,17 @@ export async function pickCompactModel(
|
|
|
270
270
|
const configuredModel = resolveConfiguredModel(ctx, configured);
|
|
271
271
|
if (!configuredModel && configured) {
|
|
272
272
|
ctx.ui.notify(
|
|
273
|
-
`compact-model:
|
|
273
|
+
`compact-model: the configured compact model ${configured} does not exist or is unavailable — pick another one.`,
|
|
274
274
|
"warning",
|
|
275
275
|
);
|
|
276
276
|
}
|
|
277
277
|
const model = await openModelPicker(ctx, configuredModel, {
|
|
278
|
-
selectTitle:
|
|
278
|
+
selectTitle:
|
|
279
|
+
"Select the compaction model (first entry is the current default; press Enter to use it)",
|
|
279
280
|
});
|
|
280
281
|
if (!model) {
|
|
281
282
|
ctx.ui.notify(
|
|
282
|
-
"compact-model:
|
|
283
|
+
"compact-model: cancelled — the compact model is unchanged. To clear the setting, edit <agent-dir>/pi-toolkits-compact.json and set model to null.",
|
|
283
284
|
"info",
|
|
284
285
|
);
|
|
285
286
|
return undefined;
|
|
@@ -290,7 +291,7 @@ export async function pickCompactModel(
|
|
|
290
291
|
store.setModel(key);
|
|
291
292
|
}
|
|
292
293
|
ctx.ui.notify(
|
|
293
|
-
`compact-model:
|
|
294
|
+
`compact-model: compact model set to "${model.name}" — it will run the next compaction summary.`,
|
|
294
295
|
"info",
|
|
295
296
|
);
|
|
296
297
|
return model;
|
|
@@ -310,7 +311,7 @@ export function registerCompact(pi: ExtensionAPI): void {
|
|
|
310
311
|
const model = resolveConfiguredModel(ctx, configured);
|
|
311
312
|
if (!model) {
|
|
312
313
|
ctx.ui.notify(
|
|
313
|
-
`compact-model:
|
|
314
|
+
`compact-model: the configured compact model ${configured} does not exist or is unavailable — falling back to pi's default compaction.`,
|
|
314
315
|
"warning",
|
|
315
316
|
);
|
|
316
317
|
return undefined;
|
|
@@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";
|
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
3
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
4
|
import { join } from "node:path";
|
|
5
|
-
import { applyPlan, rewriteText, stateToToml } from "./apply.js";
|
|
5
|
+
import { applyPlan, rewriteText, scrubRetiredFields, stateToToml } from "./apply.js";
|
|
6
6
|
import { parseState } from "./detect.js";
|
|
7
7
|
import { detect } from "./detect.js";
|
|
8
8
|
import { buildPlan } from "./plan.js";
|
|
@@ -96,7 +96,10 @@ describe("applyPlan", () => {
|
|
|
96
96
|
const state = parseState(readFileSync(join(root, ".agents/framework/state.toml"), "utf8"));
|
|
97
97
|
expect(state.version).toBe(1);
|
|
98
98
|
expect(state.migrations.length).toBeGreaterThan(0);
|
|
99
|
-
|
|
99
|
+
// The rendering language is computed per run and never persisted.
|
|
100
|
+
expect(readFileSync(join(root, ".agents/framework/state.toml"), "utf8")).not.toMatch(
|
|
101
|
+
/^\s*lang\s*=/m,
|
|
102
|
+
);
|
|
100
103
|
expect(applied.planPath).toMatch(/^\.agents\/framework\/plans\/plan-.*\.json$/);
|
|
101
104
|
const order = JSON.parse(readFileSync(join(root, applied.planPath ?? ""), "utf8")) as {
|
|
102
105
|
tasks: Array<{ id: string }>;
|
|
@@ -113,6 +116,35 @@ describe("applyPlan", () => {
|
|
|
113
116
|
}
|
|
114
117
|
});
|
|
115
118
|
|
|
119
|
+
describe("retired-field cleanup", () => {
|
|
120
|
+
it("scrubs old plan artifacts during a mutating run", () => {
|
|
121
|
+
const old = `${JSON.stringify(
|
|
122
|
+
{
|
|
123
|
+
framework: "agent-framework",
|
|
124
|
+
mode: "adopt",
|
|
125
|
+
language: "zh",
|
|
126
|
+
tasks: [{ id: "survey", detail: "Read.\nLanguage for everything you write: zh." }],
|
|
127
|
+
},
|
|
128
|
+
null,
|
|
129
|
+
2,
|
|
130
|
+
)}\n`;
|
|
131
|
+
const root = project({ ...LEGACY, ".agents/framework/plans/plan-old.json": old });
|
|
132
|
+
try {
|
|
133
|
+
const applied = applyPlan(planFor(root), {
|
|
134
|
+
now: new Date("2026-10-26T10:00:00+08:00"),
|
|
135
|
+
});
|
|
136
|
+
expect(applied.summary.some((line) => line.includes("removed obsolete fields"))).toBe(true);
|
|
137
|
+
const scrubbed = JSON.parse(
|
|
138
|
+
readFileSync(join(root, ".agents/framework/plans/plan-old.json"), "utf8"),
|
|
139
|
+
) as { language?: unknown; tasks: Array<{ detail: string }> };
|
|
140
|
+
expect("language" in scrubbed).toBe(false);
|
|
141
|
+
expect(scrubbed.tasks[0].detail).toBe("Read.");
|
|
142
|
+
} finally {
|
|
143
|
+
remove(root);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
116
148
|
it("is safe to run twice", () => {
|
|
117
149
|
const root = project(LEGACY);
|
|
118
150
|
try {
|
|
@@ -170,7 +202,6 @@ describe("applyPlan", () => {
|
|
|
170
202
|
updated: "2026-01-02T00:00:00+08:00",
|
|
171
203
|
profile: "core" as const,
|
|
172
204
|
modules: ["design"],
|
|
173
|
-
lang: "zh" as const,
|
|
174
205
|
roots: [
|
|
175
206
|
{
|
|
176
207
|
label: "api",
|
|
@@ -262,3 +293,128 @@ it("does not corrupt non-UTF-8 files during reference rewriting", () => {
|
|
|
262
293
|
remove(root);
|
|
263
294
|
}
|
|
264
295
|
});
|
|
296
|
+
|
|
297
|
+
describe("scrubRetiredFields", () => {
|
|
298
|
+
const STATE = [
|
|
299
|
+
"version = 1",
|
|
300
|
+
'tool = "0.11.0"',
|
|
301
|
+
'created = "2026-01-01T00:00:00+08:00"',
|
|
302
|
+
'updated = "2026-01-01T00:00:00+08:00"',
|
|
303
|
+
'profile = "core"',
|
|
304
|
+
"modules = []",
|
|
305
|
+
'lang = "zh"',
|
|
306
|
+
"",
|
|
307
|
+
"[[managed]]",
|
|
308
|
+
'path = ".agents/memory/README.md"',
|
|
309
|
+
'sha256 = "abc"',
|
|
310
|
+
'template = "memory-readme"',
|
|
311
|
+
"",
|
|
312
|
+
].join("\n");
|
|
313
|
+
|
|
314
|
+
function artifact(withLanguage: boolean, retiredDetail = false): string {
|
|
315
|
+
const detail = retiredDetail
|
|
316
|
+
? "Read the project.\nLanguage for everything you write: en."
|
|
317
|
+
: "Read the project.";
|
|
318
|
+
return `${JSON.stringify(
|
|
319
|
+
{
|
|
320
|
+
framework: "agent-framework",
|
|
321
|
+
mode: "adopt",
|
|
322
|
+
...(withLanguage ? { language: "en" } : {}),
|
|
323
|
+
tasks: [{ id: "survey", kind: "survey", detail, acceptance: [] }],
|
|
324
|
+
},
|
|
325
|
+
null,
|
|
326
|
+
2,
|
|
327
|
+
)}\n`;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
it("removes the retired state line and plan key, idempotently", () => {
|
|
331
|
+
const root = project({
|
|
332
|
+
".agents/framework/state.toml": STATE,
|
|
333
|
+
".agents/framework/plans/plan-a.json": artifact(true, true),
|
|
334
|
+
".agents/framework/plans/plan-b.json": artifact(false),
|
|
335
|
+
});
|
|
336
|
+
try {
|
|
337
|
+
const first = scrubRetiredFields(root);
|
|
338
|
+
expect(first).toEqual({ stateScrubbed: true, plansScrubbed: 1 });
|
|
339
|
+
expect(readFileSync(join(root, ".agents/framework/state.toml"), "utf8")).not.toMatch(/^\s*lang\s*=/m);
|
|
340
|
+
// Everything except the retired line survives untouched.
|
|
341
|
+
expect(readFileSync(join(root, ".agents/framework/state.toml"), "utf8")).toContain('template = "memory-readme"');
|
|
342
|
+
const a = JSON.parse(readFileSync(join(root, ".agents/framework/plans/plan-a.json"), "utf8")) as {
|
|
343
|
+
tasks: Array<{ detail: string }>;
|
|
344
|
+
};
|
|
345
|
+
expect("language" in a).toBe(false);
|
|
346
|
+
expect((a as { mode?: string }).mode).toBe("adopt");
|
|
347
|
+
expect(a.tasks[0].detail).toBe("Read the project.");
|
|
348
|
+
|
|
349
|
+
const second = scrubRetiredFields(root);
|
|
350
|
+
expect(second).toEqual({ stateScrubbed: false, plansScrubbed: 0 });
|
|
351
|
+
} finally {
|
|
352
|
+
remove(root);
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it("is a no-op on a clean or missing install", () => {
|
|
357
|
+
const clean = project({
|
|
358
|
+
".agents/framework/state.toml": STATE.replace('lang = "zh"\n', ""),
|
|
359
|
+
".agents/framework/plans/plan-c.json": artifact(false),
|
|
360
|
+
});
|
|
361
|
+
try {
|
|
362
|
+
expect(scrubRetiredFields(clean)).toEqual({ stateScrubbed: false, plansScrubbed: 0 });
|
|
363
|
+
} finally {
|
|
364
|
+
remove(clean);
|
|
365
|
+
}
|
|
366
|
+
const empty = project({ "README.md": "# demo\n" });
|
|
367
|
+
try {
|
|
368
|
+
expect(scrubRetiredFields(empty)).toEqual({ stateScrubbed: false, plansScrubbed: 0 });
|
|
369
|
+
} finally {
|
|
370
|
+
remove(empty);
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it("leaves malformed plan artifacts untouched", () => {
|
|
375
|
+
const root = project({
|
|
376
|
+
".agents/framework/state.toml": STATE,
|
|
377
|
+
".agents/framework/plans/broken.json": "{ not json",
|
|
378
|
+
});
|
|
379
|
+
try {
|
|
380
|
+
expect(scrubRetiredFields(root)).toEqual({ stateScrubbed: true, plansScrubbed: 0 });
|
|
381
|
+
expect(readFileSync(join(root, ".agents/framework/plans/broken.json"), "utf8")).toBe("{ not json");
|
|
382
|
+
} finally {
|
|
383
|
+
remove(root);
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it("changes nothing under --dry-run", () => {
|
|
388
|
+
const root = project({
|
|
389
|
+
".agents/framework/state.toml": STATE,
|
|
390
|
+
".agents/framework/plans/plan-a.json": artifact(true, true),
|
|
391
|
+
});
|
|
392
|
+
try {
|
|
393
|
+
const before = readFileSync(join(root, ".agents/framework/state.toml"), "utf8");
|
|
394
|
+
const beforePlan = readFileSync(join(root, ".agents/framework/plans/plan-a.json"), "utf8");
|
|
395
|
+
expect(scrubRetiredFields(root, { dryRun: true })).toEqual({
|
|
396
|
+
stateScrubbed: true,
|
|
397
|
+
plansScrubbed: 1,
|
|
398
|
+
});
|
|
399
|
+
expect(readFileSync(join(root, ".agents/framework/state.toml"), "utf8")).toBe(before);
|
|
400
|
+
expect(readFileSync(join(root, ".agents/framework/plans/plan-a.json"), "utf8")).toBe(beforePlan);
|
|
401
|
+
} finally {
|
|
402
|
+
remove(root);
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("only filters the top-level region, never a table key with the same name", () => {
|
|
407
|
+
const withTableKey = `${STATE}\n[[roots]]\nlabel = "api"\nlang = "zh"\n`;
|
|
408
|
+
const root = project({ ".agents/framework/state.toml": withTableKey });
|
|
409
|
+
try {
|
|
410
|
+
scrubRetiredFields(root);
|
|
411
|
+
const text = readFileSync(join(root, ".agents/framework/state.toml"), "utf8");
|
|
412
|
+
// The top-level line is gone; the root entry keeps its own key.
|
|
413
|
+
expect(text.split("\n").filter((line) => line === 'lang = "zh"')).toHaveLength(1);
|
|
414
|
+
expect(text).toContain("[[roots]]");
|
|
415
|
+
expect(text).toContain('label = "api"');
|
|
416
|
+
} finally {
|
|
417
|
+
remove(root);
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
});
|