@andares/pi-toolkits 1.0.2 → 1.1.0

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/CHANGELOG.md CHANGED
@@ -11,6 +11,17 @@ renames that heading to the version being published and refuses an empty section
11
11
 
12
12
  <!-- Notes for the next release go here; `pnpm release` renames this heading. -->
13
13
 
14
+ ## 1.1.0 — 2026-11-25
15
+
16
+ <!-- Notes for the next release go here; `pnpm release` renames this heading. -->
17
+
18
+ - **spec** — running `/ask-spec` without arguments while a spec review is active now leaves the phase; `/ask-spec off` still works.
19
+ - **spec** — after feedback, the next submitted revision starts a new review server and opens a new window instead of printing a dead URL; reopening a closed review restarts it, and the revision history is preserved.
20
+ - **keep** — `/keep-quit` marks the current session unfinished and quits in one step; `/keep-load` lists that backlog, already-loaded rows dimmed but still selectable, and `/keep-drop` (or `ctrl+r` in the list) removes one.
21
+ - **keep** — the next interactive start reports how many kept sessions are still unloaded (`2 not loaded · /keep-load`).
22
+ - **keep** — a loaded session begins with the note you left at quit time, delivered as a message the model can read without triggering a turn.
23
+ - **keep** — the backlog is an append-only ledger: two pi processes can write at once, and a `load` left behind by a killed process is repaired on the next read.
24
+
14
25
  ## 1.0.2 — 2026-11-24
15
26
 
16
27
  <!-- Notes for the next release go here; `pnpm release` renames this heading. -->
@@ -71,3 +82,4 @@ renames that heading to the version being published and refuses an empty section
71
82
 
72
83
 
73
84
 
85
+
package/README.md CHANGED
@@ -13,7 +13,7 @@
13
13
  [![pi >= 0.99](https://img.shields.io/badge/pi-%3E%3D0.99.0-blueviolet)](https://github.com/earendil-works/pi)
14
14
  [![GitHub](https://img.shields.io/badge/github-andares%2Fpi--toolkits-181717?logo=github)](https://github.com/andares/pi-toolkits)
15
15
 
16
- In a nutshell: twelve 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** `ask-review`, **dedicated compaction model** `compact-model`, **session startup automation** `autostart`, an **agent memory framework** `ask-init`, a **spec review in the browser** `ask-spec`, an **acceptance ledger with a browser page** `ask-uat`, a **quit watchdog** `quit-guard`, and **release notes on the first start after an upgrade** `whats-new` — with more to come.
16
+ In a nutshell: thirteen 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** `ask-review`, **dedicated compaction model** `compact-model`, **session startup automation** `autostart`, an **agent memory framework** `ask-init`, a **spec review in the browser** `ask-spec`, an **acceptance ledger with a browser page** `ask-uat`, a **quit watchdog** `quit-guard`, a **session backlog for parallel work** `keep`, and **release notes on the first start after an upgrade** `whats-new` — with more to come.
17
17
 
18
18
  **Requirements**: pi `>=0.99.0`. Every published pi release from 0.99.0 through 1.6.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
 
@@ -58,6 +58,7 @@ After installing, verify in any pi session:
58
58
  - [🚀 autostart — session startup automation](#autostart)
59
59
  - [🧠 init — agent memory framework](#init)
60
60
  - [🧯 quit-guard — quit watchdog](#quit-guard)
61
+ - [🧷 keep — session backlog](#keep)
61
62
  - [🆕 whats-new — release notes after an upgrade](#whats-new)
62
63
  - [Development](#development)
63
64
  - [Contributing](#contributing)
@@ -82,6 +83,7 @@ After installing, verify in any pi session:
82
83
  | 🚀 **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 |
83
84
  | 🧠 **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, then audit/refresh it for drift, budgets and duplicated facts |
84
85
  | 🧯 **quit-guard** quit watchdog | `/quit-guard` | force-exit when an interactive shutdown hangs, whether after the TUI teardown or before it on a signal-triggered quit; write a hang report and give the shell prompt back; TUI restarts and Ctrl+Z resume disarm it |
86
+ | 🧷 **keep** session backlog | `/keep-quit` · `/keep-load` · `/keep-drop` | mark a session unfinished as you quit, reopen the backlog next time (loaded rows dimmed but still selectable), drop what you finished; an append-only ledger, no lock and no host session listing |
85
87
  | 🆕 **whats-new** release notes after an upgrade | automatic | the first `pi` start after an upgrade shows what changed since the version you last ran, once; no slash command or other global namespace is claimed |
86
88
 
87
89
  ---
@@ -298,6 +300,7 @@ After a development task is done, type the `/ask-review` command to summon **a d
298
300
  - **Approve and deny both carry your notes.** They only choose the next step: approve means "start implementing" (with every note applied on the way), deny means "revise the spec and submit again".
299
301
  - **History is kept.** Every submitted revision is stored under `<agent-dir>/ask-spec/<slug>/` and the header lets you reopen any of them read-only. Existing `<agent-dir>/ask-plan/` histories are migrated on first use.
300
302
  - **The review phase uses the boundary checks.** `/ask-perm` is switched on with its defaults when the review starts and restored when it ends; the only file the review phase may write is markdown.
303
+ - **Every revision gets a fresh window.** The review server closes shortly after you send a decision, so the next submitted revision starts a new server (next free port in the 20-port range) and opens a new window instead of pointing you at a dead URL. That is a restart of the page, not of the review: every revision, annotation and decision is kept, and the history drawer still lists the earlier rounds.
301
304
  - **No model switching, ever.** The spec is reviewed and implemented with whatever model the session is on. (`/ask-review` is the one command that does switch, because you asked it to.)
302
305
 
303
306
  **Browser on WSL**: the review server binds `0.0.0.0` and the URL is printed with the WSL address, because Windows cannot reach a loopback-only server inside WSL. The opener prefers a Windows-side launcher (`wslview`, `cmd.exe /c start`, `powershell Start-Process`) and only falls back to `xdg-open` elsewhere — that is the difference between "opens your browser" and "opens a browser inside WSL that you cannot see".
@@ -305,6 +308,7 @@ After a development task is done, type the `/ask-review` command to summon **a d
305
308
  ```text
306
309
  /ask-spec build the export feature # start the review
307
310
  /ask-spec open # re-open the review page
311
+ /ask-spec # while reviewing: leave the phase
308
312
  /ask-spec off # leave the phase (the draft stays where it is)
309
313
  ```
310
314
 
@@ -681,6 +685,43 @@ kill appends `thread-kill heartbeat-stopped …` there.
681
685
 
682
686
  ---
683
687
 
688
+ ## 🧷 keep — session backlog for parallel work
689
+
690
+ <a name="keep"></a>
691
+
692
+ **What this does.** `/keep-quit` marks the current session unfinished and quits in one step, so the
693
+ list of open tasks is written down at the moment you walk away instead of reconstructed from memory
694
+ a day later. `/keep-load` lists exactly that backlog. Loading does **not** remove anything:
695
+ `/keep-drop` (or `ctrl+r` in the list) is the only way out.
696
+
697
+ Kept and loaded are two axes on purpose. A session is *kept* while it is unfinished and *loaded*
698
+ while some pi process has it open, and the second one is a record, not a lock: a hard-killed
699
+ process leaves a stale marker, so a loaded row stays selectable and can be loaded again. The next
700
+ read after that process is gone notices the dead pid and marks the session unloaded again.
701
+
702
+ State is an append-only ledger at `<agent-dir>/pi-toolkits-keep/keep.jsonl` with four events
703
+ (`keep`, `drop`, `load`, `unload`); everything the list shows is a pure fold over it, so two pi
704
+ processes can append at the same time without a lock. Nothing here calls the host's session
705
+ listing (576 ms for this project's 109 sessions, 9.8 s for the whole agent dir) — the backlog reads
706
+ only the ledger, and `rebuild` reads 16 KB per session file when you have to restore it.
707
+
708
+ | command | what it does |
709
+ | --- | --- |
710
+ | `/keep-quit [note]` | mark this session unfinished and quit; the note falls back to the session name, then to the first user message |
711
+ | `/keep-load` | open the backlog: `↑↓` move, enter load, `ctrl+r` drop, esc cancel; already-loaded rows are dimmed but still selectable |
712
+ | `/keep-load <n\|id>` | load by 1-based index or session id prefix |
713
+ | `/keep-load --all` | include other projects, with a project column |
714
+ | `/keep-load rebuild` | re-add keeps from the breadcrumb each session file carries (recovery after the ledger is lost) |
715
+ | `/keep-drop` | remove the current session from the backlog |
716
+
717
+ The next interactive start shows the backlog as one line — `2 not loaded · /keep-load`. Loading a
718
+ session injects the note you left at quit time as a message the model can see without triggering a
719
+ turn, so it knows where you stopped. The current session never lists itself: `/keep-quit` and
720
+ `/keep-drop` are its commands.
721
+
722
+ **Not a lock.** Two processes can still open the same session file; this records that they did, it
723
+ does not prevent it. `pi -ne` and headless runs never write `load`, so they stay invisible here.
724
+
684
725
  ## 🆕 whats-new — release notes after an upgrade
685
726
 
686
727
  <a name="whats-new"></a>
package/README.zh-CN.md CHANGED
@@ -13,7 +13,7 @@
13
13
  [![pi >= 0.99](https://img.shields.io/badge/pi-%3E%3D0.99.0-blueviolet)](https://github.com/earendil-works/pi)
14
14
  [![GitHub](https://img.shields.io/badge/github-andares%2Fpi--toolkits-181717?logo=github)](https://github.com/andares/pi-toolkits)
15
15
 
16
- 一句话介绍:目前包含十二个开箱即用的功能——**只读咨询模式** `ask`、**会话级边界权限** `ask-perm`、**模型收藏** `favorites`、**提示词暂存** `stash`、**第三方代码评审** `ask-review`、**独立压缩模型** `compact-model`、**会话启动自动化** `autostart`、**agent 记忆框架** `ask-init`、**浏览器内规格评审** `ask-spec`、**验收台账与浏览器页面** `ask-uat`、**退出看门狗** `quit-guard`、**升级后首次启动的版本变更提示** `whats-new`,后续持续追加。
16
+ 一句话介绍:目前包含十三个开箱即用的功能——**只读咨询模式** `ask`、**会话级边界权限** `ask-perm`、**模型收藏** `favorites`、**提示词暂存** `stash`、**第三方代码评审** `ask-review`、**独立压缩模型** `compact-model`、**会话启动自动化** `autostart`、**agent 记忆框架** `ask-init`、**浏览器内规格评审** `ask-spec`、**验收台账与浏览器页面** `ask-uat`、**退出看门狗** `quit-guard`、**并行任务的会话暂存** `keep`、**升级后首次启动的版本变更提示** `whats-new`,后续持续追加。
17
17
 
18
18
  **版本要求**:pi `>=0.99.0`。0.99.0 到 1.6.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
 
@@ -58,6 +58,7 @@ pi -e ./src/index.ts
58
58
  - [🚀 autostart — 会话启动自动化](#autostart)
59
59
  - [🧠 init — agent 记忆框架](#init)
60
60
  - [🧯 quit-guard — 退出看门狗](#quit-guard)
61
+ - [🧷 keep — 会话暂存](#keep)
61
62
  - [🆕 whats-new — 升级后的版本变更提示](#whats-new)
62
63
  - [开发](#development)
63
64
  - [贡献](#contributing)
@@ -82,6 +83,7 @@ pi -e ./src/index.ts
82
83
  | 🚀 **autostart** 会话启动自动化 | `.pi/autostart.toml` | 项目内声明启动时自动执行的斜杠命令(首批用例:`/add-dir` 多仓库挂载);未信任不执行、未注册命令跳过不发给模型 |
83
84
  | 🧠 **init** agent 记忆框架 | `/ask-init` | 安装或接管一套固定的记忆结构(`AGENTS.md` + `specs/` + `.agents/memory` + `.agents/implementations`),带快照与覆盖表迁移旧文档,之后可随时审计/刷新漂移、预算与重复事实 |
84
85
  | 🧯 **quit-guard** 退出看门狗 | `/quit-guard` | 交互式退出卡死时,无论发生在 TUI 收尾之后、还是信号触发路径的收尾之前,超时后写诊断报告并强制退出,把命令行提示符还给你;TUI 重启与 Ctrl+Z 恢复都会解除看守 |
86
+ | 🧷 **keep** 会话暂存 | `/keep-quit` · `/keep-load` · `/keep-drop` | 退出时一条命令把当前会话标记为未完成,下次进来直接看这个待办列表(已载入的置灰但仍可选);做完的用 `/keep-drop` 或列表里的 `ctrl+r` 移出。只追加账本,没有锁,也不读宿主的会话列表 |
85
87
  | 🆕 **whats-new** 升级后的版本变更提示 | 自动 | 升级后的首次 `pi` 启动展示「从你上次运行的版本到现在改了什么」,只展示一次;不注册斜杠命令,不占用宿主的全局命令命名空间 |
86
88
 
87
89
  ---
@@ -298,6 +300,7 @@ run = ["/ask-perm allow allow allow ask"]
298
300
  - **批准和提意见都携带批注。** 它们只决定下一步:批准是「开始实施」(所有批注都带上),提意见是「改完 spec 再提交一版」。
299
301
  - **历史留档。** 每次提交都存在 `<agent-dir>/ask-spec/<slug>/`,页头可以只读回看任意一版;旧的 `<agent-dir>/ask-plan/` 历史会在第一次使用时迁移。
300
302
  - **评审期走边界检查。** `/ask-perm` 在评审开始时按默认值开启,结束时恢复;评审期唯一能写的是 markdown。
303
+ - **每一版都有自己的新窗口。** 你提交决定后评审服务很快关端口,所以下一次提交修订会重起服务(20 个端口段里取下一个空闲的)并弹新窗口,而不是把你指向一个死链接。重启的是页面,不是评审:每一版、每条批注和决定都保留,历史抽屉里前几轮仍在。
301
304
  - **绝不切模型。** spec 评审与实施都用会话当前的模型。(`/ask-review` 是唯一会切模型的命令,那是你要求的。)
302
305
 
303
306
  **WSL 下的浏览器**:评审服务器绑 `0.0.0.0`,URL 用 WSL 的地址打印,因为 Windows 连不到 WSL 里只绑 loopback 的服务器。打开器优先用 Windows 侧启动器(`wslview`、`cmd.exe /c start`、`powershell Start-Process`),其它平台才回落到 `xdg-open`——这决定了是「打开你的浏览器」还是「在 WSL 里打开一个你看不见的浏览器」。
@@ -305,6 +308,7 @@ run = ["/ask-perm allow allow allow ask"]
305
308
  ```text
306
309
  /ask-spec 做导出功能 # 进入规格评审
307
310
  /ask-spec open # 重新打开评审页
311
+ /ask-spec # 评审中再次执行:退出评审期
308
312
  /ask-spec off # 退出评审期(草稿留在原处)
309
313
  ```
310
314
 
@@ -591,6 +595,37 @@ refresh 就会正常更新它。
591
595
 
592
596
  ---
593
597
 
598
+ ## 🧷 keep — 并行任务的会话暂存
599
+
600
+ <a name="keep"></a>
601
+
602
+ **做什么。** `/keep-quit` 把「退出」和「这个任务还没做完」合成一次动作:走的时候顺手记下来,不用第二天再凭记忆翻会话。
603
+ `/keep-load` 只列这份待办。**载入不会把会话移出待办**——出列只有 `/keep-drop` 和列表里的 `ctrl+r` 两条路。
604
+
605
+ 「已入账」和「已载入」故意分成两个维度:入账表示没做完,载入表示某个 pi 进程正开着它。后者是**记账不是锁**——
606
+ 进程被硬杀会留下脏标记,所以已载入的行仍然可以选中、可以强行重载;那个进程真的没了之后,下一次读取会发现 pid 不在了,
607
+ 自动补一条解锁记录。
608
+
609
+ 状态是 `<agent-dir>/pi-toolkits-keep/keep.jsonl` 里的只追加事件流(`keep` / `drop` / `load` / `unload`),
610
+ 列表是它的纯函数折叠,所以两个 pi 进程可以同时追加,不需要加锁。这里**不调用宿主的会话列表**(本项目 109 个会话要 576 ms,
611
+ 整个 agent 目录 648 个要 9.8 s)——日常只读账本,只有 `rebuild` 会在恢复时读每个会话文件的尾部 16 KB。
612
+
613
+ | 命令 | 作用 |
614
+ | --- | --- |
615
+ | `/keep-quit [note]` | 标记当前会话未完成并退出;note 省略时依次回落到会话名、首条用户消息 |
616
+ | `/keep-load` | 打开待办:↑↓ 移动、enter 载入、`ctrl+r` 移出、esc 取消;已载入的行置灰但仍可选中 |
617
+ | `/keep-load <序号\|id>` | 按 1 起序号或会话 id 前缀直接载入 |
618
+ | `/keep-load --all` | 含其他项目,并多一列项目名 |
619
+ | `/keep-load rebuild` | 从会话文件里的面包屑重建账本(账本丢了之后的兜底) |
620
+ | `/keep-drop` | 把当前会话移出待办 |
621
+
622
+ 下次交互式启动时,待办只占一行:`2 not loaded · /keep-load`。载入会话后,当初留下的那句备注会作为一条
623
+ **模型能看到、但不触发回合**的消息注入,所以 agent 一上来就知道上次停在哪。列表里永远不出现当前会话本身——
624
+ 它的命令是 `/keep-quit` 和 `/keep-drop`。
625
+
626
+ **不是锁。** 两个进程仍然可以同时打开同一个会话文件;这里只记录发生过,不阻止。`pi -ne` 与 headless 运行不写 `load`,
627
+ 在这里是不可见的。
628
+
594
629
  ## 🆕 whats-new — 升级后的版本变更提示
595
630
 
596
631
  <a name="whats-new"></a>
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@andares/pi-toolkits",
3
- "version": "1.0.2",
4
- "description": "Integrated toolkit extension for pi — ask mode (read-only Q&A), browser-reviewed planning, an acceptance ledger with a browser page, session-scoped permission checks, model favorites, prompt stash, third-party code review, session autostart, a dedicated compaction model, an agent memory framework (/ask-init), a quit watchdog, and release notes on the first start after an upgrade",
3
+ "version": "1.1.0",
4
+ "description": "Integrated toolkit extension for pi — ask mode (read-only Q&A), browser-reviewed planning, an acceptance ledger with a browser page, session-scoped permission checks, model favorites, prompt stash, third-party code review, session autostart, a dedicated compaction model, an agent memory framework (/ask-init), a session backlog (/keep-quit, /keep-load, /keep-drop), a quit watchdog, and release notes on the first start after an upgrade",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -49,7 +49,8 @@
49
49
  "watchdog",
50
50
  "whats-new",
51
51
  "release-notes",
52
- "changelog"
52
+ "changelog",
53
+ "session-backlog"
53
54
  ],
54
55
  "peerDependencies": {
55
56
  "@earendil-works/pi-coding-agent": ">=0.99.0",
@@ -0,0 +1,44 @@
1
+ /**
2
+ * keep feature — constants shared by the ledger, the commands and the picker.
3
+ *
4
+ * Two custom entry types are written into the session file itself:
5
+ * - `keep:unfinished` is the breadcrumb appended by `/keep-quit` (display
6
+ * only, out of the model context) and the marker `rebuild` looks for in a
7
+ * session tail;
8
+ * - `keep:notice` carries the one-line startup notice, rendered through
9
+ * `registerEntryRenderer` so `ui.invalidate()` cannot wipe it.
10
+ *
11
+ * `keep:resume` is a `custom_message`, not an entry: it must reach the model
12
+ * (the note left at quit time is the whole point of coming back).
13
+ */
14
+ import type { KeyId } from "@earendil-works/pi-tui";
15
+
16
+ /** Breadcrumb entry appended when a session is kept. */
17
+ export const KEEP_ENTRY = "keep:unfinished";
18
+ /** Startup notice entry (rendered, never sent to the model). */
19
+ export const KEEP_NOTICE_ENTRY = "keep:notice";
20
+ /** Resume note message injected into the loaded session. */
21
+ export const KEEP_RESUME_ENTRY = "keep:resume";
22
+
23
+ /** `<agent-dir>/pi-toolkits-keep/keep.jsonl`. */
24
+ export const KEEP_DIR = "pi-toolkits-keep";
25
+ export const KEEP_LEDGER_FILE = "keep.jsonl";
26
+
27
+ /**
28
+ * Drop the highlighted row without loading it.
29
+ *
30
+ * `ctrl+r` is free where we need it: the host binds it to
31
+ * `app.session.rename` inside its own session selector, and to nothing in
32
+ * `tui.altScreen.*` (the bindings that preempt a dialog) or in the global
33
+ * table. Our picker owns its own component, so it consumes the key itself.
34
+ * Re-audited with `pnpm check:keybindings ctrl+r` — see the feature section in
35
+ * AGENTS.md for the re-check obligation on host upgrades.
36
+ */
37
+ export const KEEP_DROP_KEY: KeyId = "ctrl+r";
38
+
39
+ /** Rows shown before the list scrolls. */
40
+ export const KEEP_MAX_ROWS = 12;
41
+ /** Note length cap: a note is a reminder, not a summary. */
42
+ export const KEEP_NOTE_MAX = 60;
43
+ /** Bytes read from the end of a session file when rebuilding the ledger. */
44
+ export const KEEP_TAIL_BYTES = 16 * 1024;
@@ -0,0 +1,77 @@
1
+ /**
2
+ * keep feature — text rendering.
3
+ *
4
+ * Pure string building so the rows, the startup notice and the resume note can
5
+ * be asserted without a terminal. Age is deliberately coarse (`12m`, `3h`,
6
+ * `2d`): the exact minute of a decision is in the ledger, not in the list.
7
+ */
8
+ import { basename } from "node:path";
9
+ import type { KeepEntryView } from "./ledger.js";
10
+
11
+ const MINUTE = 60_000;
12
+ const HOUR = 60 * MINUTE;
13
+ const DAY = 24 * HOUR;
14
+ const WEEK = 7 * DAY;
15
+
16
+ /** `now` / `12m` / `3h` / `2d` / `5w`. */
17
+ export function formatAge(iso: string, now: Date): string {
18
+ const then = Date.parse(iso);
19
+ if (Number.isNaN(then)) return "?";
20
+ const diff = now.getTime() - then;
21
+ if (diff < MINUTE) return "now";
22
+ if (diff < HOUR) return `${Math.floor(diff / MINUTE)}m`;
23
+ if (diff < DAY) return `${Math.floor(diff / HOUR)}h`;
24
+ if (diff < 2 * WEEK) return `${Math.floor(diff / DAY)}d`;
25
+ return `${Math.floor(diff / WEEK)}w`;
26
+ }
27
+
28
+ /** `2026-11-24 15:02` in local time (no locale dependence). */
29
+ export function formatStamp(iso: string): string {
30
+ const date = new Date(iso);
31
+ if (Number.isNaN(date.getTime())) return iso;
32
+ const pad = (value: number): string => String(value).padStart(2, "0");
33
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`;
34
+ }
35
+
36
+ /** Last path segment, used as the project column with `--all`. */
37
+ export function projectName(cwd: string): string {
38
+ const name = basename(cwd);
39
+ return name.length > 0 ? name : cwd;
40
+ }
41
+
42
+ /**
43
+ * One list row: state glyph, age, note, then optional project and `missing`.
44
+ * `○` is not loaded, `●` is loaded (the row is dimmed by the picker).
45
+ */
46
+ export function rowText(
47
+ entry: KeepEntryView,
48
+ options: { readonly now: Date; readonly showProject: boolean },
49
+ ): string {
50
+ const glyph = entry.loaded ? "●" : "○";
51
+ const age = formatAge(entry.keptAt, options.now);
52
+ const parts = [entry.note.length > 0 ? entry.note : "(no note)"];
53
+ if (options.showProject) parts.push(projectName(entry.cwd));
54
+ if (entry.missing) parts.push("missing");
55
+ return `${glyph} ${age} ${parts.join(" · ")}`;
56
+ }
57
+
58
+ /** Header line above the rows. */
59
+ export function pickerTitle(notLoaded: number, loaded: number): string {
60
+ const parts = [`${notLoaded} not loaded`];
61
+ if (loaded > 0) parts.push(`${loaded} loaded`);
62
+ return `Keep backlog — ${parts.join(", ")}`;
63
+ }
64
+
65
+ /** Startup notice: one line, counts only, current project only. */
66
+ export function noticeText(count: number): string {
67
+ return `${count} not loaded · /keep-load`;
68
+ }
69
+
70
+ /**
71
+ * Message injected into a loaded session. It is a `custom_message`, so the
72
+ * model sees it on the next turn without the note triggering a turn of its own.
73
+ */
74
+ export function resumeText(entry: KeepEntryView): string {
75
+ const note = entry.note.length > 0 ? entry.note : "(no note)";
76
+ return `[keep] Resumed from the backlog — note left ${formatStamp(entry.keptAt)}: "${note}"`;
77
+ }