@zhushanwen/pi-pending-notifications 0.7.5 → 0.7.7

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.
Files changed (3) hide show
  1. package/README.md +72 -0
  2. package/package.json +2 -2
  3. package/src/index.ts +17 -5
package/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # @zhushanwen/pi-pending-notifications
2
+
3
+ 跨 extension 的异步操作注册/查询机制:workflow / subagent / bash 后台任务等长耗时异步操作在启动与结束时经 Pi EventBus 广播事件,本扩展监听事件并将注册/注销落盘为 session entries,同时提供 `pending_notifications` 工具供 LLM 查询当前活跃的异步操作(数量或列表)。
4
+
5
+ ## 提供的工具
6
+
7
+ ### `pending_notifications`
8
+
9
+ | 参数 | 类型 | 说明 |
10
+ |------|------|------|
11
+ | `action` | `"count"` \| `"list"` | `count` 返回活跃操作数量;`list` 返回列表(类型 / 名称 / id) |
12
+
13
+ `list` 返回文本示例:
14
+
15
+ ```
16
+ 2 pending operation(s):
17
+ - [subagent] 审查登录模块 (id=sa-xxxx)
18
+ - [workflow] 重构数据层 (id=run-xxxx)
19
+ ```
20
+
21
+ ## 核心机制
22
+
23
+ - **事件契约**:异步操作的宿主扩展经 Pi EventBus 广播:
24
+ - `pending:register { id, type, name }` —— 操作启动(`type` ∈ `workflow` / `subagent` / `bash`)
25
+ - `pending:unregister { id, reason }` —— 操作结束(`reason` 映射为终态 status)
26
+ - 发送方:subagent-core(subagent 与 workflow run 生命周期:启动注册 / 结束注销 / 崩溃恢复 sweep 补注销)、base-tool-enhance(bash 后台任务)、subagent-workflow(仅崩溃恢复补注销)
27
+ - **session entries 唯一状态源**:register/unregister 经 `appendEntry` 落盘,无内存第二份状态;活跃集合 = register − unregister 差集,工具投影与写侧去重对同一份 entries 现算,结构上不可分歧
28
+ - **写侧幂等**:重复 register、未知/已注销 id 的 unregister 均忽略,天然不重复落盘
29
+ - **无 TTL**:三类操作均按进程存活口径处理,长任务(>1h)仍视为活跃
30
+ - **跨 session 残留过滤**:查询按当前 session id 过滤 fork 继承的父级注册残留
31
+ - **跨扩展导出**:`countActiveFromEntries()` 供 goal(continuation 守卫)、subagent-workflow(后代存活判定)直接复用差集计算;差集本体单点在 `@zhushanwen/extension-protocol`
32
+
33
+ ## 安装
34
+
35
+ ```bash
36
+ # npm 方式(正式)
37
+ pi install npm:@zhushanwen/pi-pending-notifications
38
+ ```
39
+
40
+ 本地开发可用 `--extension` 直接加载:
41
+
42
+ ```bash
43
+ pi --extension <repo>/extensions/universal/pending-notifications/index.ts "<prompt>"
44
+ ```
45
+
46
+ ## 使用示例
47
+
48
+ LLM 在决定「继续推进还是等待异步操作」前主动查询:
49
+
50
+ ```json
51
+ { "action": "count" }
52
+ ```
53
+
54
+ ```json
55
+ { "action": "list" }
56
+ ```
57
+
58
+ 典型场景:agent 想确认是否真有后台任务在跑(workflow run / subagent / bash 后台命令),避免在异步操作未收口时提前下结论或重复派发。
59
+
60
+ ## 文件结构
61
+
62
+ ```
63
+ pending-notifications/
64
+ ├── index.ts # 入口 — re-export src/index.ts
65
+ ├── mocks/
66
+ │ └── typebox.ts # 测试用 typebox mock
67
+ └── src/
68
+ ├── index.ts # 工厂入口(EventBus 监听 + session 基准 + pending_notifications 工具注册)
69
+ ├── state.ts # PendingEntry 类型 + 差集/写侧判断纯函数(countActiveFromEntries / hasPendingId / isPendingActive)
70
+ └── __tests__/
71
+ └── pending-notifications.test.ts
72
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhushanwen/pi-pending-notifications",
3
- "version": "0.7.5",
3
+ "version": "0.7.7",
4
4
  "description": "Cross-extension async operation registration/query mechanism for Pi.",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -33,7 +33,7 @@
33
33
  "typebox": "*"
34
34
  },
35
35
  "dependencies": {
36
- "@zhushanwen/extension-protocol": "0.12.0",
36
+ "@zhushanwen/extension-protocol": "0.14.0",
37
37
  "@zhushanwen/pi-extension-logger": "0.6.1"
38
38
  },
39
39
  "scripts": {
package/src/index.ts CHANGED
@@ -13,9 +13,10 @@
13
13
  * - index.ts(本文件): 工厂入口(EventBus 写侧监听 + session 基准 + 查询 tool)
14
14
  *
15
15
  * 事件契约(emit 端三组,与 extension-dependencies.json 本包条目 reason 同口径:
16
- * ① subagent-core——notify-host.ts / orchestration/lifecycle.ts /
17
- * worker-message-pump.ts / round-supervisor/reconcile-sweep.ts(崩溃恢复 sweep
18
- * 补注销);② base-tool-enhance——bash 投影(notify.ts register/unregister 主链 +
16
+ * ① subagent-core——notify-host.ts(register + record 终态化 unregister——finalizeRun
17
+ * 直落后的残留发射面,观测通道 = sweep reconciled WARN)/ orchestration/lifecycle.ts
18
+ * (register 主链);
19
+ * ② base-tool-enhance——bash 投影(notify.ts register/unregister 主链 +
19
20
  * process-exit-guard.ts 退出边沿尽力补 emit(pending-reconcile 对账走 appendEntry
20
21
  * 权威路径,不经 emit));
21
22
  * ③ subagent-workflow——仅崩溃恢复时 emit pending:unregister(session-lifecycle.ts
@@ -23,13 +24,24 @@
23
24
  * - emit("pending:register", { id, type, name })
24
25
  * - emit("pending:unregister", { id, reason })
25
26
  *
27
+ * 直落写方(不经本事件、直接 appendEntry 权威落盘——listener 的 isPendingActive
28
+ * 现算幂等门对两族写方统一生效,重复注销被自然拦截):
29
+ * - subagent-core worker-message-pump.ts finalizeRun(reload-closeout D4:workflow
30
+ * run 终态注销直落,emit 发射点已删——reload 转换窗/factory 顺序窗内 emit 丢失
31
+ * 即注销 entry 永缺位);
32
+ * - subagent-core round-supervisor/reconcile-sweep.ts(对账 sweep 补注销,对账走
33
+ * appendEntry 权威路径,不经 emit);
34
+ * - base-tool-enhance pending-reconcile.ts(session_start 对账,同款)。
35
+ *
26
36
  * entry 契约(与 goal before-agent-start.ts 对齐,读取端按 e.data.id 算差集):
27
37
  * - pending:register → { id, type, name, registeredAt, sessionId }
28
38
  * - pending:unregister → { id, reason, status }
29
39
  *
30
40
  * 状态权威源:session entries 是唯一状态——工具投影与写侧去重/活跃判断全部对
31
- * getEntries() 现算(appendEntry 同步入账,pi dist 实证),无内存第二份状态;
32
- * 历史的内存 registry、session_start 重建、TTL 与 shutdown 机器已删除。
41
+ * getEntries() 现算(appendEntry 同步入账,pi dist 实证,锚定 pi-semantics
42
+ * PS-29 extensionAPI appendEntry 直委托 appendCustomEntry + PS-17 _appendEntry
43
+ * 同步 push 无 debounce),无内存第二份状态;历史的内存 registry、
44
+ * session_start 重建、TTL 与 shutdown 机器已删除。
33
45
  *
34
46
  * 监听方式:pi.events.on(Pi 的 EventBus,真实 SDK 为 EventBus.on,非 optional)。
35
47
  * workflow 侧通过 deps.eventBus 注入 pi.events(同一总线)。