dsh-plugin-yanyan-toolkit 0.0.0-stage → 1.4.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.
Files changed (44) hide show
  1. package/CHANGELOG.md +85 -0
  2. package/LICENSE +29 -0
  3. package/README.en.md +82 -0
  4. package/README.i18n.yaml +7 -0
  5. package/README.md +81 -2
  6. package/README.zh.md +82 -0
  7. package/cordis.patch.yml +43 -0
  8. package/icon.svg +5 -0
  9. package/lib/client.js +201 -0
  10. package/lib/index.js +149 -0
  11. package/locale/en.json +6 -0
  12. package/locale/zh.json +6 -0
  13. package/package.json +82 -4
  14. package/packages/dsh-plugin-yanyan-icon/README.md +33 -0
  15. package/packages/dsh-plugin-yanyan-icon/README.zh.md +33 -0
  16. package/packages/dsh-plugin-yanyan-icon/cordis.patch.yml +7 -0
  17. package/packages/dsh-plugin-yanyan-icon/icon.svg +7 -0
  18. package/packages/dsh-plugin-yanyan-icon/lib/client.js +388 -0
  19. package/packages/dsh-plugin-yanyan-icon/lib/index.js +237 -0
  20. package/packages/dsh-plugin-yanyan-icon/locale/en.json +6 -0
  21. package/packages/dsh-plugin-yanyan-icon/locale/zh.json +6 -0
  22. package/packages/dsh-plugin-yanyan-icon/package.json +32 -0
  23. package/packages/dsh-plugin-yanyan-subagent/README.md +73 -0
  24. package/packages/dsh-plugin-yanyan-subagent/README.zh.md +73 -0
  25. package/packages/dsh-plugin-yanyan-subagent/cordis.patch.yml +8 -0
  26. package/packages/dsh-plugin-yanyan-subagent/icon.svg +8 -0
  27. package/packages/dsh-plugin-yanyan-subagent/lib/client.js +1374 -0
  28. package/packages/dsh-plugin-yanyan-subagent/lib/index.js +992 -0
  29. package/packages/dsh-plugin-yanyan-subagent/lib/legacy-hidden-ids.json +29 -0
  30. package/packages/dsh-plugin-yanyan-subagent/lib/log-rewrite.js +411 -0
  31. package/packages/dsh-plugin-yanyan-subagent/lib/zstd-frames.js +81 -0
  32. package/packages/dsh-plugin-yanyan-subagent/locale/en.json +1 -0
  33. package/packages/dsh-plugin-yanyan-subagent/locale/zh.json +1 -0
  34. package/packages/dsh-plugin-yanyan-subagent/package.json +32 -0
  35. package/packages/dsh-plugin-yanyan-update/README.md +25 -0
  36. package/packages/dsh-plugin-yanyan-update/README.zh.md +25 -0
  37. package/packages/dsh-plugin-yanyan-update/cordis.patch.yml +7 -0
  38. package/packages/dsh-plugin-yanyan-update/icon.svg +5 -0
  39. package/packages/dsh-plugin-yanyan-update/lib/client.js +400 -0
  40. package/packages/dsh-plugin-yanyan-update/lib/index.js +924 -0
  41. package/packages/dsh-plugin-yanyan-update/locale/en.json +1 -0
  42. package/packages/dsh-plugin-yanyan-update/locale/zh.json +1 -0
  43. package/packages/dsh-plugin-yanyan-update/package.json +32 -0
  44. package/scripts/cleanup-ghost-subagents.mjs +241 -0
@@ -0,0 +1,73 @@
1
+ # Subagent Management (dsh-plugin-yanyan-subagent)
2
+
3
+ The third plugin in the "Yanyan Toolkit" aggregate bundle. It lists and manages all subagents of the current session.
4
+
5
+ ## Features
6
+
7
+ - Adds a tab to the conversation header actions (default label "Agent Manager", renameable from the overlay; the name is persisted in browser local storage).
8
+ - Clicking the tab opens an overlay. The overlay is mounted through `ReactDOM.createPortal` into `document.body` so the header's overflow clipping cannot cut it off; when `react-dom` is unavailable it falls back to appending a plain DOM overlay to `document.body`.
9
+ - Lists every subagent of the current session with name, status (running / idle), mode and an estimated idle duration.
10
+ - A trailing delete button (U+2715 MULTIPLICATION X, a plain Unicode math symbol, not an emoji) deletes one subagent after a confirmation. Running subagents are stopped before deletion. The root session (lead) is never deleted.
11
+ - The bottom "Clean All Idle" button immediately batch-deletes every entry shown as idle (anything not running), regardless of the idle threshold (the threshold only applies to timed auto-clean); its criteria match the list and the host /cleanup route, and it reports the count (or a "nothing to clean" hint).
12
+ - The "Settings" section enables "Auto-clean idle subagents" (off by default), with "Idle over (minutes)" and "Check interval (minutes)". When enabled, each check only deletes idle subagents that reached the threshold; a failed list read is fail-closed (nothing is deleted).
13
+
14
+ ## Delete semantics (physical deletion, irreversible)
15
+
16
+ Deleting **really removes the session data** and erases the catalog row at its source; it no longer archives. Layers:
17
+
18
+ 1. Real sessions reuse the existing `@linxin666/dsh-session-archive` physical-delete pipeline (`POST /api/dsh-session-archive/delete`), which cleans the sqlite rdb, session directory, projection cache and archive ledger.
19
+ 2. The host `/purge` route then cleans up again: the session directory (both `session-`-prefixed and bare spellings, with realpath checks), projcache files and parent `subagentCatalog` cache references, `workspace.json` `global.archivedSessionIds` and `tables.workspaces[*].sessionIds` accounting, and `archive-ledger.json`; it also removes the archive marker via `workspaceRegistry.unarchiveSession`.
20
+ 3. mnemon virtual delegations (ids like `mnemon-run-v1-<uuid>`, not real `session-<uuid>` sessions) go straight to layer 2 — calling `archiveSession` on such an id reports `session/not-found`.
21
+ 4. **Catalog root-cause fix (v1.2.0)**: `subagentCatalog` is an **append-only projection** of `subagent/catalog` events in the parent session log (`session.v4.jsonl.zstd`); the official `@deepseek-ai/dsh-chunked-list` has no delete API, so clearing caches or archiving can never remove the source reference and the host rebuilds the row on the next `refreshProjections`. Borrowing the in-place "retirement" idea from `huajiao1998/dsh-plugin-subagent-clean@0.3.0` (MIT, originally `heiheiha798/dsh-plugin-subagent-delete@0.2.3`), `/purge` rewrites the matching `subagent/catalog` event **in place** into `{"type":"subagent-delete/retired","seq":<same>,"time":<same>,"ignorable":true,"data":{"childId","retiredAt"}}`. The `seq`/`time` are preserved (log seq continuity and compaction references stay valid), only `type === "subagent/catalog"` produces directory rows, so the projection never recreates the row. Log decoding/encoding uses `node:zlib` zstd (Node >= 22.15 / 24), with two `stat` checks around an atomic rename for concurrency safety; a failed rewrite is queued to `yanyan-subagent-pending.json` and replayed on next host start.
22
+ 5. **Fallback hidden list**: the client still keeps a persistent hidden list in `localStorage['yanyan-subagent.deletedIds']` (core ids with the `session-` prefix stripped). It filters during rendering, batch cleaning, auto-cleaning and the official list row-tail action, so even a concurrent-write / decode failure cannot make a deleted row reappear.
23
+ 6. **In-memory projection root fix (v1.2.1)**: the official "N subagents" popover (`SubagentHeaderLineage`) reads only the client in-memory store `ctx.sessions.list.getSnapshot().projectionsBySession[*].values.subagentCatalog`; the host reuses the in-memory fold state for a loaded live parent session, so rewriting the on-disk zstd log / deleting projcache never triggers a recompute — already-retired residual rows keep showing and clicking one reports `subagent is unavailable (subagent/not-found)`. To fix this, the host adds `collectRetiredIds` to scan the whole library for `subagent-delete/retired` log rows and ships `hiddenIds` via `/config` (compatibility bridge: an extended `legacy-hidden-ids.json` makes the old host process deliver them immediately); the client's `patchProjections` subscribes to `sessions.list` and, using the host's own `list.set()`, continuously removes hidden childIds from every `projectionsBySession[*].values.subagentCatalog` (idempotent, no self-loop), so the official popover's `directCount` drops to zero and hides itself. This is the real root-cause layer behind "never able to delete".
24
+
25
+ **This operation is irreversible**: a confirmation is required; running subagents are stopped first. The root session (lead) is never deleted.
26
+
27
+ ### Residual rows, one-click cleanup and sidebar sync
28
+
29
+ - A **"Clean residual rows"** button in the overlay calls the host `/scrub` route, which retires catalog rows whose sub-session directory is already gone (leftovers from older archive / rebuild behaviour, figures 2 and 3). After every delete, batch clean, auto-clean and scrub, the client calls `ctx.sessions.refresh()` in addition to `refreshProjections(rootId)` so the **sidebar residual row disappears in sync**.
30
+ - The host `/cleanup` route (and the model tool) batch-deletes with a **single** whole-library log rewrite (`retireCatalogRowsEverywhere`) instead of one rewrite per row.
31
+ - Host `/catalog` lists the still-live `subagent/catalog` events straight from the log (the reliable source of truth, unlike the rebuildable projcache cache).
32
+
33
+ ### Model tools (optional `tools` injection)
34
+
35
+ Three plain-object tools are registered when the profile provides the `tools` service, so an Agent can do the cleanup in one sentence (the `mnemon_forget` idea from the request targets Mnemon memory insights, not subagent sessions, so its "let the Agent trigger cleanup" intent is served here instead):
36
+
37
+ - `yanyan_list_subagents` — list live catalog rows (filter by label).
38
+ - `yanyan_delete_subagent` — permanently delete one subagent (required `subagent_id`).
39
+ - `yanyan_cleanup_subagents` — batch delete (label filter / idle-only / dry-run / limit), matching "list all idle `Mnemon idle checkpoint review` subagents and delete them all".
40
+
41
+ ## Data sources and fallbacks
42
+
43
+ - Primary source is the browser session projection: `ctx.sessions.list.getSnapshot().projectionsBySession[rootId].values.subagentCatalog`.
44
+ - The root session id is derived from `retainedBy.mainView > 0`, falling back to the first session in `projectionsBySession` that carries `subagentCatalog`.
45
+ - If the local projection is unavailable, the client falls back to the host route `/__dsh-plugin-yanyan-subagent/list` (which depends on `subagents.listDescendants` and returns `service-unavailable` when that service is missing).
46
+ - The official projection has no idle-origin field (`lastActiveAt` / `idleSince` have zero hits for subagent UI). Idle duration is estimated in memory from the first time an `inactive` row is observed, and resets on page reload.
47
+
48
+ ## Limitation of the row-tail delete button in the official subagent list (figure 3, optional enhancement)
49
+
50
+ The plugin makes a best-effort attempt with a `MutationObserver`: when the official `[data-subagent-panel]` overlay appears, it appends a small delete button to every `[role="treeitem"]` row. There is a confirmed hard limitation:
51
+
52
+ - Official list rows have **no row-level Slot and no stable identifier such as `data-id`**; the row `aria-label` is only `[primaryLabel, secondary, metrics].join(' ')`, which cannot be reliably mapped back to a `sessionId`.
53
+ - Clicking that button therefore only does fuzzy matching of row text against the local `subagentCatalog` (name containment plus order alignment). **Deletion runs only for a unique match**; otherwise the user is told to use the Agent Manager tab, where a definite `sessionId` is available.
54
+ - This DOM injection is a fragile enhancement: it can break whenever the official DOM changes, and matching can misfire. Prefer the row-tail delete button inside this plugin's own "Agent Manager" tab.
55
+
56
+ ## Configuration
57
+
58
+ `cordis.patch.yml` carries the defaults (the profile override replaces the whole config for the same id without a deep merge, so the host `normalizeConfig()` provides a fallback for every missing key):
59
+
60
+ | Key | Default | Meaning |
61
+ | --- | --- | --- |
62
+ | `tabLabel` | Agent Manager | Header tab label, renameable from the overlay |
63
+ | `autoCleanEnabled` | false | Auto-clean switch, off by default |
64
+ | `idleMinutes` | 30 | Idle threshold in minutes (1..10080) |
65
+ | `checkIntervalMin` | 1 | Auto-clean check interval in minutes (1..1440) |
66
+
67
+ ## Tests
68
+
69
+ ```
70
+ node --test test/*.test.mjs
71
+ ```
72
+
73
+ Pure unit tests cover `normalizeConfig`, `parseCatalog`, `idleFilter`, deletion id validation, in-place log retirement, `collectRetiredIds` / `collectHiddenIds` and `patchProjections` in-memory projection filtering; no DSH service is contacted (68/68 tree-wide tests pass).
@@ -0,0 +1,73 @@
1
+ # 子智能体管理(dsh-plugin-yanyan-subagent)
2
+
3
+ 「研研工具包」聚合插件包中的第三个插件,用于查看与管理当前会话的所有子智能体。
4
+
5
+ ## 功能
6
+
7
+ - 在会话顶部操作区新增一个标签(默认「智能体管理」,可在浮层的「重命名标签」处改名,名称保存在浏览器本地存储)。
8
+ - 点击标签打开浮层(浮层通过 `ReactDOM.createPortal` 挂到 `document.body`,避免被顶部操作区的溢出裁剪裁掉;`react-dom` 不可用时降级为把浮层直接追加到 `document.body` 并用原生 DOM 渲染)。
9
+ - 列出当前会话的所有子智能体,显示名称、状态(运行中 / 空闲)、模式以及估算的空闲时长。
10
+ - 行尾的删除按钮(U+2715 乘号,普通 Unicode 数学符号,非 emoji)可逐个删除,删除前二次确认;正在运行的子智能体删除时会先停止。根会话(lead)本身永不删除。
11
+ - 底部「清理全部空闲」立即清理当前列表里所有显示为空闲(非运行中)的子智能体,并显示清理数量;这是手动操作,**不受「空闲阈值」限制**(阈值只用于定时自动清理)。判定与列表「空闲」标签、host `/cleanup` 语义一致:排除运行中、诊断行、根会话与已在隐藏清单中的条目,不再出现「已清理 0 个」却清不掉条目的情况。
12
+ - 「设置」区可开启「自动清理空闲子智能体」(默认关闭),设置「空闲超过(分钟)」与「巡检间隔(分钟)」。开启后按间隔巡检,只清理达到阈值且处于空闲的子智能体;读取列表数据失败时保守不删(fail-closed)。
13
+
14
+ ## 删除语义(物理删除,不可逆)
15
+
16
+ 删除会**真正移除会话数据并从源头根除目录行**,不再是「归档」。做法分层:
17
+
18
+ 1. 真实会话优先复用现成包 `@linxin666/dsh-session-archive` 的 `POST /api/dsh-session-archive/delete` 物理删除管线(会清理 sqlite rdb、会话目录、投影缓存与归档台账)。
19
+ 2. host 侧 `/purge` 路由再兜底清理一遍:会话目录(含 `session-` 前缀两种拼写、realpath 校验不越界)、projcache 文件与父会话 `subagentCatalog` 缓存引用、`workspace.json` 的 `global.archivedSessionIds` 与 `tables.workspaces[*].sessionIds` 记账、`archive-ledger.json` 台账;并用 `workspaceRegistry.unarchiveSession` 摘掉归档标记。
20
+ 3. 对 mnemon 虚拟委派(id 形如 `mnemon-run-v1-<uuid>`,不是真实 `session-<uuid>` 会话)直接走第 2 层——这类 id 调用 `archiveSession` 会报 `session/not-found`。
21
+ 4. **catalog 根治(v1.2.0)**:`subagentCatalog` 是父会话日志 `session.v4.jsonl.zstd` 里 `subagent/catalog` 事件的 **append-only 投影**(官方 `@deepseek-ai/dsh-chunked-list` 只追加、无删除 API)。因此清缓存、归档、删目录都移除不了底层日志引用,宿主下次 `refreshProjections` 会把条目重建回来。借鉴 `huajiao1998/dsh-plugin-subagent-clean@0.3.0` 的「原位退休」思路(MIT,原始实现 `heiheiha798/dsh-plugin-subagent-delete@0.2.3`),`/purge` 把命中的 `subagent/catalog` 事件**原位改写**为 `{"type":"subagent-delete/retired","seq":<原值>,"time":<原值>,"ignorable":true,"data":{"childId","retiredAt"}}`:`seq`/`time` 原样保留(日志 seq 连续性与压缩事件引用不受影响),而目录投影只在 `type === "subagent/catalog"` 时生成行,于是投影再也不会重建该行。日志解码/编码用 `node:zlib` 的 zstd(Node >= 22.15 / 24),原子 rename 前后各做一次 stat 并发校验;改写失败会排队到 `yanyan-subagent-pending.json`,宿主下次启动补做。
22
+ 5. **隐藏清单兜底**:client 侧仍保留持久化隐藏清单 `localStorage['yanyan-subagent.deletedIds']`(存去 `session-` 前缀后的核心 id),在渲染、批量清理、自动清理与官方列表行尾按钮里持续过滤。即便日志改写遇到并发写入/解码失败,已删条目也不会重新出现。
23
+ 6. **内存投影根治(v1.2.1)**:官方「N 个子智能体」浮层(`SubagentHeaderLineage`)只读 client 内存 store `ctx.sessions.list.getSnapshot().projectionsBySession[*].values.subagentCatalog`;宿主对已加载的 live 父会话复用内存 fold state,单纯改磁盘 zstd 日志 / 删 projcache 都不会让它重算,于是磁盘已退休的残留行仍会显示、点进去报 `subagent is unavailable (subagent/not-found)`。为此 host 新增 `collectRetiredIds` 扫全库 `subagent-delete/retired` 日志行,经 `/config` 下发 `hiddenIds`(兼容桥:扩充 `legacy-hidden-ids.json` 让旧 host 进程即时下发);client 的 `patchProjections` 订阅 `sessions.list`,用宿主自身的 `list.set()` 把隐藏清单里的 childId 从所有 `projectionsBySession[*].values.subagentCatalog` 中持续剔除(幂等收敛、不自激),官方浮层 `directCount` 归零后自动隐藏。这是「一直删不掉」的真正根因层。
24
+
25
+ **该操作不可逆**,删除前会二次确认;正在运行的子智能体删除时先停止。根会话(lead)本身永不删除。
26
+
27
+ ### 残留行、一键清理与侧边栏同步
28
+
29
+ - 浮层底部新增 **「清理残留行」** 按钮,调用 host `/scrub` 路由,把「子会话目录已不在、catalog 行还在」的引用从父会话日志原位退休(专治旧版归档 / 重建遗留的幽灵行,对应图2 / 图3)。每次单删、批量清理、自动清理与残留清理之后,client 除 `refreshProjections(rootId)` 外还会调用 `ctx.sessions.refresh()`,让**侧边栏残留行同步消失**。
30
+ - host `/cleanup` 路由(与模型工具)批量删除时对齐上游做法:所有命中 id **只全库改写一次日志**(`retireCatalogRowsEverywhere`),不是逐条改写。
31
+ - host `/catalog` 直接从日志真相源列出仍会生成目录行的 `subagent/catalog` 事件(比会被重建的 projcache 缓存可靠)。
32
+
33
+ ### 模型工具(可选 `tools` 注入)
34
+
35
+ profile 提供 `tools` 服务时注册三个纯对象工具,让 Agent 一句话就能完成清理(用户建议里的 `mnemon_forget` 删的是 Mnemon 记忆洞察 insight、不是子智能体会话 / catalog 行,按字面调用不对症;其「让 Agent 也能触发清理」的真实意图由此承接):
36
+
37
+ - `yanyan_list_subagents` —— 列出仍存活的目录行(可按标签过滤)。
38
+ - `yanyan_delete_subagent` —— 彻底删除单个子智能体(必填 `subagent_id`)。
39
+ - `yanyan_cleanup_subagents` —— 批量删除(标签过滤 / 仅空闲 / dry-run / 数量上限),对应「列出当前会话里所有空闲的 Mnemonic idle checkpoint review 子代理并全部删掉」。
40
+
41
+ ## 数据来源与降级
42
+
43
+ - 主数据源为浏览器侧会话投影:`ctx.sessions.list.getSnapshot().projectionsBySession[rootId].values.subagentCatalog`。
44
+ - 根会话 id 由 `retainedBy.mainView > 0` 推导,找不到时回退到遍历 `projectionsBySession` 中带 `subagentCatalog` 的会话。
45
+ - 本地投影读不到时,回退到 host 侧 `/__dsh-plugin-yanyan-subagent/list`(依赖宿主的 `subagents.listDescendants`,服务缺失时返回 `service-unavailable`)。
46
+ - 官方投影没有空闲起点字段(`lastActiveAt` / `idleSince` 均无命中),空闲时长由本插件在内存中按「首次观察到 inactive 的时刻」估算,刷新页面后会重置。
47
+
48
+ ## 官方子智能体列表浮层行尾删除按钮的限制(图3,可选增强)
49
+
50
+ 本插件尽力用 `MutationObserver` 在官方子智能体列表浮层 `[data-subagent-panel]` 出现时,给每个 `[role="treeitem"]` 行尾追加一个小删除按钮。但存在已确认的硬限制:
51
+
52
+ - 官方列表行**没有行级 Slot,也没有 `data-id` 等稳定标识**;行的 `aria-label` 只是 `[primaryLabel, secondary, metrics].join(' ')` 的文本拼接,无法可靠反推出 `sessionId`。
53
+ - 因此点击该按钮时只能拿行文本到本地 `subagentCatalog` 做模糊匹配(名称包含匹配 + 顺序对应)。**只有唯一命中时才会执行删除**;无法唯一定位时会提示「无法定位该子智能体,请在智能体管理标签中删除」。
54
+ - 该 DOM 注入是「脆弱增强」:官方 DOM 结构一旦变化即可能失效,且匹配可能误判。它不保证成功,请优先使用本插件自己的「智能体管理」标签里的行尾删除按钮(那里有确定的 `sessionId`)。
55
+
56
+ ## 配置
57
+
58
+ `cordis.patch.yml` 提供默认值(profile 覆盖层对同名 id 是整份替换 config、不做深合并,host 的 `normalizeConfig()` 对每个缺省 key 都做了兜底):
59
+
60
+ | 键 | 默认值 | 含义 |
61
+ | --- | --- | --- |
62
+ | `tabLabel` | 智能体管理 | 顶部标签文字,可在浮层内改名 |
63
+ | `autoCleanEnabled` | false | 自动清理开关,默认关闭 |
64
+ | `idleMinutes` | 30 | 空闲阈值分钟数(1..10080) |
65
+ | `checkIntervalMin` | 1 | 自动清理巡检间隔分钟数(1..1440) |
66
+
67
+ ## 测试
68
+
69
+ ```
70
+ node --test test/*.test.mjs
71
+ ```
72
+
73
+ 纯单元测试覆盖 `normalizeConfig`、`parseCatalog`、`idleFilter`、删除 id 安全校验、日志原位退休、`collectRetiredIds` / `collectHiddenIds` 与 `patchProjections` 内存投影剔除,不连接任何 DSH 服务(全树 68/68 通过)。
@@ -0,0 +1,8 @@
1
+ - insert:
2
+ - id: dsh-plugin-yanyan-subagent
3
+ name: 'dsh-plugin-yanyan-subagent'
4
+ config:
5
+ tabLabel: 智能体管理
6
+ autoCleanEnabled: false
7
+ idleMinutes: 30
8
+ checkIntervalMin: 1
@@ -0,0 +1,8 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" role="img" aria-label="Subagent Management">
2
+ <rect x="4" y="4" width="40" height="40" rx="9" fill="#2f6feb"/>
3
+ <rect x="18" y="9" width="12" height="9" rx="2.5" fill="#ffffff"/>
4
+ <rect x="7" y="30" width="10" height="9" rx="2" fill="#ffffff" opacity="0.9"/>
5
+ <rect x="19" y="30" width="10" height="9" rx="2" fill="#ffffff" opacity="0.9"/>
6
+ <rect x="31" y="30" width="10" height="9" rx="2" fill="#ffffff" opacity="0.9"/>
7
+ <path d="M24 18 L24 24 M24 24 L12 30 M24 24 L24 30 M24 24 L36 30" stroke="#ffffff" stroke-width="1.8" fill="none" stroke-linecap="round"/>
8
+ </svg>