dsh-agent-control-center 0.3.0 → 0.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.
- package/CHANGELOG.md +55 -0
- package/README.md +9 -5
- package/README.zh.md +6 -4
- package/client.js +1110 -12
- package/docs/01-CONTRACT.md +1 -1
- package/docs/05-ACCEPTANCE-EVIDENCE.md +38 -0
- package/docs/07-GUIDE.md +1 -1
- package/docs/09-ARCHITECTURE.md +2 -2
- package/docs/10-INSTRUCTION-PREVIEW.md +41 -0
- package/docs/11-EXPORT.md +45 -0
- package/docs/12-PLUGIN-HEALTH.md +49 -0
- package/index.js +4 -1
- package/lib/core/index.js +15 -1
- package/lib/core/store.js +5 -1
- package/lib/modules/j-instruction.js +162 -2
- package/lib/modules/l-health.js +862 -0
- package/package.json +1 -1
- package/scripts/verify.mjs +1 -0
- package/src/client/i18n/health.js +260 -0
- package/src/client/i18n/instruction.js +10 -10
- package/src/client/pages/health.js +832 -0
package/docs/01-CONTRACT.md
CHANGED
|
@@ -39,7 +39,7 @@ dsh-agent-control-center/
|
|
|
39
39
|
| HTTP 前缀 | `/__agent-control-center`(GET `/health`,POST `/api`) |
|
|
40
40
|
| 自有存储 | `$DSH_HOME/dsh-agent-control-center/` |
|
|
41
41
|
| locale 命名空间 | `acc` |
|
|
42
|
-
| 版本 | `0.
|
|
42
|
+
| 版本 | `0.4.0` |
|
|
43
43
|
| 兼容声明 | `dsh.compatibility.dsh = ">=0.2.1-rc.1 <0.3.0"`,`engines.dsh` 同区间 |
|
|
44
44
|
|
|
45
45
|
`package.json` 关键字段(照抄,勿改语义):
|
|
@@ -284,3 +284,41 @@ curl -s http://127.0.0.1:38080/__agent-control-center/health
|
|
|
284
284
|
- `instruction.compare` 的 `sameText` 只对「同一 cwd 且两侧都未被截断」有意义;跨 cwd 比较时差异来自不同目录的指令链,页面已用解释文案区分。
|
|
285
285
|
- 会话导出对超大会话(1.9 万事件)耗时 6.2 s,且必然截断;这是只读导出的固有代价,未做流式/分页。
|
|
286
286
|
- 两个新页面只由各自作者做过 mock 与旧实例降级验证,**合并后未做逐项交互回归**(本轮只做了渲染/空态/错误态断言)。
|
|
287
|
+
|
|
288
|
+
---
|
|
289
|
+
|
|
290
|
+
# 附:0.4.0 验收补充(2026-11-29)
|
|
291
|
+
|
|
292
|
+
0.1.0 / 0.2.0 / 0.3.0 的结论仍然有效。0.4.0 新增「插件与设置体检」模块与指令趋势记录。
|
|
293
|
+
|
|
294
|
+
## 测试
|
|
295
|
+
|
|
296
|
+
- `node --test tests/*.test.mjs` → **361 / 361 pass**(0.3.0 为 332)。
|
|
297
|
+
- 新增:`tests/health-check.test.mjs`(26)、instruction 趋势 2 个用例、路径围栏例外 1 个用例。
|
|
298
|
+
- 修了两个真实缺陷并同步更新其测试:`store.updateState` 丢弃 `void` mutator 的草稿(静默丢写)、测试里 400×10ms 的模块就绪预算在并行下会误报(改为 15s 上限)。
|
|
299
|
+
|
|
300
|
+
## 真机(隔离 profile `accverify`,12 模块)
|
|
301
|
+
|
|
302
|
+
| 项 | 结果 |
|
|
303
|
+
|---|---|
|
|
304
|
+
| `/health` | `version 0.4.0`,12 模块:`health=FULL(5)`;其余与 0.3.0 一致 |
|
|
305
|
+
| `health.plugins` | 161 ms,206 行;5 条 anomaly 全部 `unaddressable`(`cordis:include` 等内置行不能用 profile patch 控制) |
|
|
306
|
+
| `health.settings` | 34 ms,22 个命名空间;`own:{ns:'acc',declared:true,enabled:true}`;`redactionVerified:true`(`secrets` 均已脱敏,响应里只有键名) |
|
|
307
|
+
| `health.sections` | 285–430 ms,静态扫描 37 个包 / 5 个文件;命中 `websearch`(`@240xu/dsh-websearch`)+ 本插件常量行;0 冲突;跳过原因如实返回(见限制) |
|
|
308
|
+
| `health.compat` | `level:'full'`、`version:'0.2.2-rc.5'`、`readOnly:false`、`source:'compatibility.current'` |
|
|
309
|
+
| `instruction.trend` | 生成一次「按文件」视图后 `totalSamples:1`、`recordedDays:1`,样本写入插件状态 |
|
|
310
|
+
| `instruction.preview` | 76,631 B、`exact:true`(与 0.3.0 一致) |
|
|
311
|
+
|
|
312
|
+
## 浏览器(真实 Chrome headless)
|
|
313
|
+
|
|
314
|
+
- 导航 **12 项**、mid 布局 2 个组分隔符。
|
|
315
|
+
- `Health check` 页:21,276 字符,4 个状态块(206 插件 / 22 命名空间 / 0 冲突 / compat full)、插件表、命名空间表、section 表、anomalies 与 note 全渲染,**0 错误态、0 console/pageerror**。
|
|
316
|
+
- `Instruction preview` 页在 12 页下仍渲染 60,432 字符。
|
|
317
|
+
- 截图:`docs/assets/health.png`。
|
|
318
|
+
|
|
319
|
+
## 未验证 / 已知限制(0.4.0 新增部分)
|
|
320
|
+
|
|
321
|
+
- `health.sections` 是**静态扫描**:只覆盖构建产物里的字面量注册,注释中的 `settings.section` 与 `id:` 也会被计入(噪声可能);动态生成 id 的插件会漏检。
|
|
322
|
+
- 本插件自身 bundle 若以 `link:` 装在被围栏拒绝的路径(当前即如此),其 section 行来自插件常量并标 `scanned:false`,不是扫描结果。
|
|
323
|
+
- 「声明未装 / 装了未启用」两类异常仍由 `readOnlyReason` 与 profile patch 行间接推断,未读取插件运行态。
|
|
324
|
+
- 逐页交互回归(11→12 页)本轮仍只覆盖渲染/空态/错误态与两组导航断言;逐页交互专项脚本此前由子代理在运行中被环境重启打断,未产出完整报告。
|
package/docs/07-GUIDE.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 用户指南:从「我想做什么」出发
|
|
2
2
|
|
|
3
|
-
> 适用版本 **0.
|
|
3
|
+
> 适用版本 **0.4.0**,目标宿主 DSH **0.2.2-rc.5**(兼容区间 `>=0.2.1-rc.1 <0.3.0`)。
|
|
4
4
|
> 本文按**任务**组织,只描述本插件在真实界面上能做什么、失败时看到什么、提示是什么意思。
|
|
5
5
|
> 安装、权限边界、配置字段和刻意限制见 [README.zh.md](../README.zh.md);
|
|
6
6
|
> 内部实现(模块、请求流、存储、扩展点)见 [09-ARCHITECTURE.md](09-ARCHITECTURE.md);
|
package/docs/09-ARCHITECTURE.md
CHANGED
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
DSH Web GUI (browser)
|
|
27
27
|
├── Settings dialog
|
|
28
28
|
│ └── settings.section id=agent-control-center order=200 ← client.js
|
|
29
|
-
│ └──
|
|
29
|
+
│ └── 12 pages (client-side secondary navigation)
|
|
30
30
|
└── HTTP (same origin)
|
|
31
31
|
└── /__agent-control-center/health GET
|
|
32
32
|
└── /__agent-control-center/api POST {method, params}
|
|
@@ -42,7 +42,7 @@ DSH Web GUI (browser)
|
|
|
42
42
|
|
|
43
43
|
### 2.1 入口 `index.js`
|
|
44
44
|
|
|
45
|
-
- 导出:`name`、`VERSION`(`0.
|
|
45
|
+
- 导出:`name`、`VERSION`(`0.4.0`,与 `package.json` 同步)、`HTTP_PREFIX`(`/__agent-control-center`)、`inject = []`、`apply(ctx, rawConfig)`。
|
|
46
46
|
- `inject = []` 是硬约束:**任何宿主服务缺失都不阻止插件激活**,只让对应模块降级为 PARTIAL/BLOCKED。
|
|
47
47
|
- 启动序列(`apply`):
|
|
48
48
|
1. `normalizeConfig(rawConfig)`:未知字段/类型错误/越界 → **加载期直接抛错**(fail loud,不做静默兜底);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# 模块 J · 指令预览(Instruction preview)
|
|
2
|
+
|
|
3
|
+
> 只读。本模块从不写入、备份、恢复或修改任何指令文件。
|
|
4
|
+
|
|
5
|
+
## 为什么需要它
|
|
6
|
+
|
|
7
|
+
`AGENTS.md` 页面告诉你磁盘上有什么;本页告诉你**模型实际收到了什么**。两者常常不同:预算会省略/截断文件,内容去重会只保留一份,`*.local.md` 覆盖层会追加,而会话里保存的是**写入当时的快照**——之后你改了文件,模型看到的就是旧的。
|
|
8
|
+
|
|
9
|
+
## 三个视图
|
|
10
|
+
|
|
11
|
+
| 视图 | 数据来源 | 性质 | 何时可信 |
|
|
12
|
+
|---|---|---|---|
|
|
13
|
+
| 按文件重组(近似) | 当前 cwd 的指令链 + 宿主预算规则 | `mode:'files'` | `exact:true` 时(两条链都在预算内)与宿主注入字节一致;否则是近似 |
|
|
14
|
+
| 按会话精确 | 真实会话日志里 `user/message` 且 `source.kind==='agent-instructions'` 的记录 | `mode:'session'` | 始终精确——这是宿主**已经写进日志**的原文,含 `seq`/时间/`baseline` 标记 |
|
|
15
|
+
| 对比 | 两者 | `mode:'compare'` | 同 cwd 且两侧都未被截断时,`sameText` 才有判断意义 |
|
|
16
|
+
|
|
17
|
+
## API
|
|
18
|
+
|
|
19
|
+
- `instruction.chain({cwd?})` → 指令链(来源 user-global / agents-home / project / project-local;每文件字节、hash、是否进入有效链、是否被去重(`dedupedBy` 指向胜出文件)、是否被预算省略/截断)+ 每条链的预算。
|
|
20
|
+
- `instruction.preview({cwd?})` → `{mode:'files', exact, text, bytes, budget, sections, omitted, truncated, note}`。
|
|
21
|
+
- `instruction.session({sessionId, includeChanges?})` → `{mode:'session', exact:true, items:[{seq, at, baseline, bytes, text, truncated}], count}`;`includeChanges:false` 只留最后一条基线。
|
|
22
|
+
- `instruction.compare({cwd?, sessionId})` → `{filesBytes, sessionBytes, deltaBytes, sameText, exactFiles, sessionSeq, sessionAt, explanations[]}`。
|
|
23
|
+
- `instruction.summary()` → 两个视图名、模块 A 是否可用、预算是否缺失。
|
|
24
|
+
|
|
25
|
+
## 实现依据
|
|
26
|
+
|
|
27
|
+
- 宿主格式:`@deepseek-ai/dsh-agent-instructions/src/render.ts` —— `<system-reminder>` 框架、`Instructions from: <displayPath>` 段落、`Workspace instruction budget N bytes: omitted …; truncated …` 标记、预算内用完整 intro、超预算退回 compact intro。
|
|
28
|
+
- 注入判定:同包 `src/index.ts` —— 基线消息 `source.kind === 'agent-instructions'` 且 `source.baseline === true`;后续变更批次同 kind 但无 baseline。
|
|
29
|
+
- 文本组装与预算判定由模块 A 的 `agents.render` 提供,本模块不重复实现发现/去重逻辑。
|
|
30
|
+
|
|
31
|
+
## 真机观察(accverify,2026-11-29)
|
|
32
|
+
|
|
33
|
+
- 文件侧:76,621 B,`exact:true`(用户全局 58,423 B + 项目 17,855 B,预算 65,536)。
|
|
34
|
+
- 会话侧:某真实会话取出 29 个指令块,首块 31,472 B 且 `baseline:true`。
|
|
35
|
+
- 对比:同一会话 `files 76,621 B` vs `session 57,138 B` → `deltaBytes -19,483`,`sameText:false`,解释为「基线写入后文件变大」。
|
|
36
|
+
|
|
37
|
+
## 限制
|
|
38
|
+
|
|
39
|
+
- 只能看到**已落盘**的会话;正在运行的会话若尚未写入指令块,会话视图会给 `available:false` + 原因。
|
|
40
|
+
- 重组视图不复制宿主的 candidate-group 去重与截断二分搜索的全部细节,因此超预算时只能标近似;此时以会话视图为准。
|
|
41
|
+
- 单响应上限 2 MiB;超长指令块会按序截断并标 `truncated`。
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# 模块 K · 会话导出(Session export)
|
|
2
|
+
|
|
3
|
+
> 只读。不存在删除、归档、续跑或上传路径。
|
|
4
|
+
|
|
5
|
+
## 行为
|
|
6
|
+
|
|
7
|
+
把**一个**会话的时间线导出成可带走的文本:
|
|
8
|
+
|
|
9
|
+
- `markdown`:标题 + 元信息(session id、cwd、创建/更新时间)+ 逐条时间线(`## User` / `## Assistant` / `### Tool call: <name>` / `> Result ...`),token 用量放在折叠块里。
|
|
10
|
+
- `json`:`{version:1, session:{…}, events:[…]}`,每条含 `seq/at/type/role/…`。
|
|
11
|
+
|
|
12
|
+
字节上限:默认 **256 KiB**,硬上限 **1 MiB**(超出范围直接 `bad_request`,不静默截断)。
|
|
13
|
+
|
|
14
|
+
## 截断语义
|
|
15
|
+
|
|
16
|
+
事件**逐条追加**,在下一个事件会越界前停止:
|
|
17
|
+
|
|
18
|
+
- `truncated` 标明发生了截断;`omittedEvents` 是省略的事件数;
|
|
19
|
+
- **绝不返回半个事件**——Markdown 不会停在代码围栏中间,JSON 永远可 `parse`;
|
|
20
|
+
- 响应里 `bytes` 是实际输出字节,`totalBytes` 是不截断时的完整大小。
|
|
21
|
+
|
|
22
|
+
## 安全与隐私
|
|
23
|
+
|
|
24
|
+
- 工具结果只保留 `isError` 与有界预览(默认 600 字符),不输出原始大 payload;
|
|
25
|
+
- 消息内容里的 ``` 会被转义,避免破坏 Markdown 结构;
|
|
26
|
+
- token 只输出**计数**(input/output/cache read/cache write),不输出 prompt 原文中的密钥;共享脱敏函数仍会处理 `sk-…` 一类前缀;
|
|
27
|
+
- 不读取、不修改宿主文件;导出在内存里完成,下载走浏览器 `Blob`。
|
|
28
|
+
|
|
29
|
+
## API
|
|
30
|
+
|
|
31
|
+
- `export.diagnostics()` → 能力、格式、上限与 `policy`(`readOnly/writesSessionLog/deletesSessions/archivesSessions/externalNetwork`)。
|
|
32
|
+
- `export.sessions({limit, query})` → 可供导出的会话列表(标题、更新时间、事件数、是否归档)。
|
|
33
|
+
- `export.summary({id})` → 计数(events/user/assistant/toolCalls/toolResults/toolErrors/errors)与两种格式的估算字节。
|
|
34
|
+
- `export.session({id, format, maxBytes, includeToolResults})` → 导出正文。
|
|
35
|
+
|
|
36
|
+
## 真机观察(accverify,2026-11-29)
|
|
37
|
+
|
|
38
|
+
- 一个 19,345 事件的会话:`export.summary` 报 443 用户消息 / 2,470 助手消息 / 2,674 工具调用 / 14 错误;估算 Markdown 3.35 MB、JSON 5.40 MB。
|
|
39
|
+
- 同会话按默认 256 KiB 导出:6.2 s,输出 262,069 B,`truncated:true`,省略 12,623 个事件。
|
|
40
|
+
|
|
41
|
+
## 限制
|
|
42
|
+
|
|
43
|
+
- 会话很大时导出必然截断,且耗时随事件数增长(上例 6.2 s);没有流式或分页导出。
|
|
44
|
+
- 只导出本插件能读到的会话(`sessionQuery.readSession`);不可读时会 `available:false` + 原因。
|
|
45
|
+
- 宿主侧若有原生导出能力则本模块会标注 `source:'host'` 并复用;当前版本实测宿主未暴露可直接复用的公开导出方法,因此 `source:'plugin'`,渲染由本模块完成。
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# 模块 L · 插件与设置体检(Plugin health)
|
|
2
|
+
|
|
3
|
+
> 只读:不安装、不启用、不修改任何插件或配置;最多给出「建议动作」文本。
|
|
4
|
+
|
|
5
|
+
## 为什么需要
|
|
6
|
+
|
|
7
|
+
设置页由多个客户端插件各自注册 `settings.section`。两个插件抢同一个 id、或本插件那一行被禁用,都不会报错,只会在界面上表现为「入口消失 / 出现两个相同项 / 配置不生效」。本页把这四类事实摊开:
|
|
8
|
+
|
|
9
|
+
| 证据 | 来源 | 能证明什么 | 不能证明什么 |
|
|
10
|
+
|---|---|---|---|
|
|
11
|
+
| 插件清单 | `pluginManager.listPlugins()` | 装了哪些插件、能否通过 profile patch 控制(`patchId` / `readOnlyReason`) | 插件是否真的在运行 |
|
|
12
|
+
| 设置命名空间 | `settings.describe({redactSecrets:true})` | 22 个命名空间、revision、密钥键名、脱敏是否生效 | 页面怎么渲染 |
|
|
13
|
+
| section 清单 | **静态扫描**客户端 bundle | 构建产物里**字面量**注册的 section id 与 owner | 动态拼出的 id;被围栏拒绝的路径 |
|
|
14
|
+
| 兼容结论 | `core.compatibility` 的活值 | 宿主版本/能力/是否只读 | 具体某个方法为何降级 |
|
|
15
|
+
|
|
16
|
+
## section 冲突检测的方式与局限
|
|
17
|
+
|
|
18
|
+
宿主把 `settings.section` 定义在客户端 slot 层,Host 侧 `settings.describe()` **没有 section 概念**(返回的是 `{ns, schema, value, revision, secrets}`)。因此冲突只能靠读已装插件的客户端 bundle:
|
|
19
|
+
|
|
20
|
+
- 扫描 `$DSH_HOME/profiles/*/node_modules/**` 下的候选产物:`client.js`、`index.js`、`lib/client.js`、`lib/client/index.js`、`dist/client.js`、`dist/client/index.js`;
|
|
21
|
+
- 定位 `settings.section` 字面量,在其后 600 字符窗口里取 `id:` / `id =` 的字符串值(真实插件形如 `ctx.slots.inject('settings.section', () => ctx.slots.register({ name: 'settings.section', id: 'websearch', order: 16 }))`);没有 `id` 时回退取 `name`;
|
|
22
|
+
- `settings.section.icon` 这类**子 slot** 不算 section,已排除;
|
|
23
|
+
- 单文件 ≤ 4 MiB、总读取 ≤ 24 MiB、最多 200 个文件,超限的包会列进「跳过原因」而不是静默漏掉。
|
|
24
|
+
|
|
25
|
+
**局限必须记住**:静态扫描只覆盖构建产物里的字面量,运行时动态生成 id 的插件会漏检;本插件自己的 bundle 若以 `link:` 安装在 `$DSH_HOME` 之外(被路径围栏拒绝读取),则改用插件内常量 `agent-control-center` 补一行并标 `scanned:false`。
|
|
26
|
+
|
|
27
|
+
## API
|
|
28
|
+
|
|
29
|
+
- `health.plugins()` → `{available, source:'pluginManager', items:[{entryId,moduleName,patchId,readOnlyReason}], anomalies:[{kind,detail}]}`
|
|
30
|
+
- `health.settings()` → `{available, namespaces:[{ns,revision,autoGenerate,hasValue,secretKeys}], own:{ns:'acc',declared,enabled}, redactionVerified, note}`
|
|
31
|
+
- `health.sections()` → `{available, method:'static-scan', scanned:{packages,files}, sections:[{id,owner,file}], conflicts:[{kind,id,owners,suggestion}], own, note}`
|
|
32
|
+
- `health.compat()` → `{available, level, version, readOnly, reasons, source}`
|
|
33
|
+
- `health.summary()` → `{generatedAt, plugins:{total,anomalies}, settings:{...}, sections:{count,conflicts}, compat:{level,readOnly}, worst:{severity,id}}`
|
|
34
|
+
|
|
35
|
+
## 真机观察(accverify,2026-11-29,12 模块)
|
|
36
|
+
|
|
37
|
+
| 项 | 结果 |
|
|
38
|
+
|---|---|
|
|
39
|
+
| 插件 | 206 行,5 条 anomaly(全部是 `unaddressable`:如 `cordis:include`、`@deepseek-ai/dsh-…` 内置行无法用 profile patch 控制) |
|
|
40
|
+
| 设置命名空间 | 22 个,`own:{ns:'acc', declared:true, enabled:true}`,`redactionVerified:true` |
|
|
41
|
+
| section 扫描 | 37 个包 / 5 个文件 / 285–430 ms,命中 `websearch`(`@240xu/dsh-websearch`)与本插件自身常量行;**0 冲突** |
|
|
42
|
+
| 兼容 | `level:'full'`、`version:'0.2.2-rc.5'`、`readOnly:false`,`source:'compatibility.current'` |
|
|
43
|
+
| 最差项 | `plugins`(warn)——只因 206 行里有 5 行不可寻址,属正常现象,页面已写明 |
|
|
44
|
+
|
|
45
|
+
## 限制
|
|
46
|
+
|
|
47
|
+
- 「声明了但没装」「装了但没启用」两类异常当前由 `pluginManager` 的 `readOnlyReason` 与 profile patch 行共同推断;与插件实际 enabled 状态存在观测差,页面以 `anomalies.detail` 明示推断依据。
|
|
48
|
+
- 静态扫描不解析 AST:注释里出现 `settings.section` 与 `id:` 也会被计入(真实 bundle 里注释较多,可能产生噪声)。
|
|
49
|
+
- 兼容结论读的是宿主活值,不重新探测每个方法;单个方法为何降级仍要看 Diagnostics 页。
|
package/index.js
CHANGED
|
@@ -15,7 +15,7 @@ import { createRegistry } from './lib/core/registry.js'
|
|
|
15
15
|
|
|
16
16
|
export const name = 'dsh-agent-control-center'
|
|
17
17
|
/** Plugin version, mirrored in package.json. */
|
|
18
|
-
export const VERSION = '0.
|
|
18
|
+
export const VERSION = '0.4.0'
|
|
19
19
|
/** Browser transport prefix. The client fetches it document-relative. */
|
|
20
20
|
export const HTTP_PREFIX = '/__agent-control-center'
|
|
21
21
|
|
|
@@ -58,6 +58,7 @@ const MODULE_MANIFEST = Object.freeze([
|
|
|
58
58
|
{ id: 'recovery', load: () => import('./lib/modules/i-recovery.js') },
|
|
59
59
|
{ id: 'instruction', load: () => import('./lib/modules/j-instruction.js') },
|
|
60
60
|
{ id: 'export', load: () => import('./lib/modules/k-export.js') },
|
|
61
|
+
{ id: 'health', load: () => import('./lib/modules/l-health.js') },
|
|
61
62
|
])
|
|
62
63
|
|
|
63
64
|
/**
|
|
@@ -128,6 +129,8 @@ export function apply(ctx, rawConfig) {
|
|
|
128
129
|
// Compatibility verdict: probed once after mount, then reported in /health
|
|
129
130
|
// so a page can warn before a write on an untested host version.
|
|
130
131
|
const compatibility = createCompatibility({ ctx: routeCtx, caps: core.caps, log: log.info })
|
|
132
|
+
// Modules may read the live verdict (health.compat prefers it over re-evaluating).
|
|
133
|
+
core.compatibility = compatibility
|
|
131
134
|
let compatDetected = false
|
|
132
135
|
void compatibility
|
|
133
136
|
.ensure()
|
package/lib/core/index.js
CHANGED
|
@@ -139,7 +139,21 @@ export function createCore(options) {
|
|
|
139
139
|
if (top === 'llm.env' || top === '.npmrc' || top === '.credentials.yaml' || top.startsWith('.credentials') || top.startsWith('.workbuddy')) {
|
|
140
140
|
return '该路径属于宿主凭据/密钥,插件不读取也不写入'
|
|
141
141
|
}
|
|
142
|
-
|
|
142
|
+
// The settings-section scan reads plugin bundles: package code, never user
|
|
143
|
+
// data. `profiles/<name>/node_modules/**` is allowed for reads and writes
|
|
144
|
+
// are still refused by the write allow-list in module A.
|
|
145
|
+
if (top === 'profiles') {
|
|
146
|
+
const parts = relative.split(path.sep)
|
|
147
|
+
// Plugin code lives under `node_modules`; the scan reads that subtree.
|
|
148
|
+
if (parts.length >= 3 && parts[2] === 'node_modules') return null
|
|
149
|
+
// Directory traversal (`profiles`, `profiles/<name>`) is allowed so a
|
|
150
|
+
// caller can list packages. Profile files never are: a file name in this
|
|
151
|
+
// tree carries an extension (`.npmrc`, `cordis.patch.yml`, `package.json`),
|
|
152
|
+
// a profile directory name does not.
|
|
153
|
+
if (parts.length <= 2 && !path.basename(relative).includes('.')) return null
|
|
154
|
+
return '该目录属于宿主内部数据,插件不读取也不写入'
|
|
155
|
+
}
|
|
156
|
+
if (top === 'storages' || top === 'skills' || top === '.git') {
|
|
143
157
|
return '该目录属于宿主内部数据,插件不读取也不写入'
|
|
144
158
|
}
|
|
145
159
|
return null
|
package/lib/core/store.js
CHANGED
|
@@ -125,7 +125,11 @@ export function createStore(options) {
|
|
|
125
125
|
async function updateState(mutate) {
|
|
126
126
|
return withLock('state', async () => {
|
|
127
127
|
const current = await readState()
|
|
128
|
-
|
|
128
|
+
// A mutator may either return a replacement document or mutate the draft
|
|
129
|
+
// it receives; both forms must persist, so the draft is kept when the
|
|
130
|
+
// mutator returns nothing (its `void` return type documents that form).
|
|
131
|
+
const working = structuredClone(current)
|
|
132
|
+
const draft = mutate(working) ?? working
|
|
129
133
|
const next = { ...draft, version: STATE_VERSION }
|
|
130
134
|
await writeJsonAtomic(paths.state, next)
|
|
131
135
|
return next
|
|
@@ -26,6 +26,12 @@ const ITEM_ENVELOPE_BYTES = 96
|
|
|
26
26
|
const MAX_CLIP_PASSES = 12
|
|
27
27
|
/** Remote failure messages are bounded before they enter a hint. */
|
|
28
28
|
const ERROR_HINT_MAX = 160
|
|
29
|
+
/** One instruction-size sample is recorded at most this often per working directory. */
|
|
30
|
+
const TREND_MIN_INTERVAL_MS = 10 * 60 * 1000
|
|
31
|
+
/** Samples kept in `state.json`; the cap is on the whole ledger, oldest dropped first. */
|
|
32
|
+
const TREND_MAX_SAMPLES = 500
|
|
33
|
+
/** Samples older than this are pruned; the page groups them by local day. */
|
|
34
|
+
const TREND_RETENTION_DAYS = 180
|
|
29
35
|
|
|
30
36
|
/** @param {unknown} value @returns {number} */
|
|
31
37
|
function byteLength(value) {
|
|
@@ -83,6 +89,87 @@ function stringOrNull(value) {
|
|
|
83
89
|
/**
|
|
84
90
|
* @param {Record<string, any>} core
|
|
85
91
|
*/
|
|
92
|
+
/**
|
|
93
|
+
* Append one instruction-size sample to the plugin's own state document.
|
|
94
|
+
*
|
|
95
|
+
* Sampling is throttled per working directory so a page that refreshes often
|
|
96
|
+
* cannot bloat `state.json`, and the ledger is pruned by age and count inside
|
|
97
|
+
* the same atomic read-modify-write that stores it. Failures are reported to
|
|
98
|
+
* the caller but never break the read-only view being displayed.
|
|
99
|
+
*
|
|
100
|
+
* @param {Record<string, any>} storage plugin storage (`core.storage`)
|
|
101
|
+
* @param {{ cwd: string | null, projectRoot: string | null, userGlobalBytes: number, projectBytes: number, totalBytes: number, maxBytes: number | null, exact: boolean }} sample
|
|
102
|
+
* @returns {Promise<{ recorded: boolean, reason?: string }>}
|
|
103
|
+
*/
|
|
104
|
+
async function appendTrendSample(storage, sample) {
|
|
105
|
+
if (typeof storage?.updateState !== 'function') return { recorded: false, reason: '插件存储不可用' }
|
|
106
|
+
const at = Date.now()
|
|
107
|
+
/** @type {{ recorded: boolean, reason?: string }} */
|
|
108
|
+
let outcome = { recorded: false }
|
|
109
|
+
try {
|
|
110
|
+
if (typeof storage.ensureLayout === 'function') await storage.ensureLayout()
|
|
111
|
+
await storage.updateState((/** @type {Record<string, any>} */ state) => {
|
|
112
|
+
const current = state.trend !== null && typeof state.trend === 'object' && Array.isArray(state.trend.samples)
|
|
113
|
+
? state.trend
|
|
114
|
+
: { version: 1, samples: [] }
|
|
115
|
+
const samples = current.samples.filter((/** @type {any} */ entry) => entry !== null && typeof entry === 'object' && Number.isFinite(entry.at))
|
|
116
|
+
const last = samples[samples.length - 1]
|
|
117
|
+
if (last !== undefined && last.cwd === sample.cwd && at - last.at < TREND_MIN_INTERVAL_MS) {
|
|
118
|
+
outcome = { recorded: false, reason: '距上次采样不足 10 分钟' }
|
|
119
|
+
state.trend = { ...current, version: 1, samples }
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
samples.push({ at, ...sample })
|
|
123
|
+
const cutoff = at - TREND_RETENTION_DAYS * 24 * 60 * 60 * 1000
|
|
124
|
+
const kept = samples.filter((/** @type {any} */ entry) => entry.at >= cutoff).slice(-TREND_MAX_SAMPLES)
|
|
125
|
+
state.trend = { version: 1, samples: kept }
|
|
126
|
+
outcome = { recorded: true }
|
|
127
|
+
})
|
|
128
|
+
} catch (error) {
|
|
129
|
+
return { recorded: false, reason: `采样失败:${String(/** @type {any} */ (error)?.message ?? error).slice(0, 120)}` }
|
|
130
|
+
}
|
|
131
|
+
return outcome
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Group recorded samples by local calendar day.
|
|
136
|
+
* @param {any[]} samples
|
|
137
|
+
* @param {number} days window size, ending today
|
|
138
|
+
* @returns {{ day: string, samples: number, avgBytes: number, maxBytes: number, lastBytes: number, budget: number | null }[]}
|
|
139
|
+
*/
|
|
140
|
+
function groupTrendByDay(samples, days) {
|
|
141
|
+
const now = new Date()
|
|
142
|
+
/** @type {Map<string, any[]>} */
|
|
143
|
+
const byDay = new Map()
|
|
144
|
+
for (let index = days - 1; index >= 0; index -= 1) {
|
|
145
|
+
const date = new Date(now.getFullYear(), now.getMonth(), now.getDate() - index)
|
|
146
|
+
byDay.set(dayKey(date), [])
|
|
147
|
+
}
|
|
148
|
+
for (const sample of samples) {
|
|
149
|
+
const date = new Date(sample.at)
|
|
150
|
+
const key = dayKey(date)
|
|
151
|
+
const bucket = byDay.get(key)
|
|
152
|
+
if (bucket !== undefined) bucket.push(sample)
|
|
153
|
+
}
|
|
154
|
+
return [...byDay.entries()].map(([day, entries]) => ({
|
|
155
|
+
day,
|
|
156
|
+
samples: entries.length,
|
|
157
|
+
avgBytes: entries.length === 0 ? 0 : Math.round(entries.reduce((total, entry) => total + (finiteNumber(entry.totalBytes) ?? 0), 0) / entries.length),
|
|
158
|
+
maxBytes: entries.length === 0 ? 0 : Math.max(...entries.map((entry) => finiteNumber(entry.totalBytes) ?? 0)),
|
|
159
|
+
lastBytes: entries.length === 0 ? 0 : finiteNumber(entries[entries.length - 1].totalBytes) ?? 0,
|
|
160
|
+
budget: entries.length === 0 ? null : finiteNumber(entries[entries.length - 1].maxBytes),
|
|
161
|
+
}))
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* @param {Date} date
|
|
166
|
+
* @returns {string} `YYYY-MM-DD` in local time, the same key the usage ledger uses
|
|
167
|
+
*/
|
|
168
|
+
function dayKey(date) {
|
|
169
|
+
const pad = (/** @type {number} */ value) => String(value).padStart(2, '0')
|
|
170
|
+
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
|
|
171
|
+
}
|
|
172
|
+
|
|
86
173
|
export default function createModule(core) {
|
|
87
174
|
const util = core.util
|
|
88
175
|
const errors = core.errors
|
|
@@ -424,6 +511,32 @@ export default function createModule(core) {
|
|
|
424
511
|
}
|
|
425
512
|
}
|
|
426
513
|
|
|
514
|
+
/**
|
|
515
|
+
* Record one sample from a files-side payload. Best-effort: a failed write
|
|
516
|
+
* must not break the view the caller is about to render.
|
|
517
|
+
* @param {Record<string, any>} value
|
|
518
|
+
* @returns {Promise<void>}
|
|
519
|
+
*/
|
|
520
|
+
async function recordTrend(value) {
|
|
521
|
+
if (value === null || typeof value !== 'object' || value.available !== true) return
|
|
522
|
+
const budget = value.budget !== null && typeof value.budget === 'object' ? value.budget : {}
|
|
523
|
+
const userGlobal = budget.userGlobal !== null && typeof budget.userGlobal === 'object' ? budget.userGlobal : {}
|
|
524
|
+
const project = budget.project !== null && typeof budget.project === 'object' ? budget.project : {}
|
|
525
|
+
try {
|
|
526
|
+
await appendTrendSample(core.storage, {
|
|
527
|
+
cwd: typeof value.cwd === 'string' ? value.cwd : null,
|
|
528
|
+
projectRoot: typeof value.projectRoot === 'string' ? value.projectRoot : null,
|
|
529
|
+
userGlobalBytes: finiteNumber(userGlobal.bytes) ?? 0,
|
|
530
|
+
projectBytes: finiteNumber(project.bytes) ?? 0,
|
|
531
|
+
totalBytes: finiteNumber(value.bytes) ?? 0,
|
|
532
|
+
maxBytes: finiteNumber(budget.maxBytes),
|
|
533
|
+
exact: value.exact === true,
|
|
534
|
+
})
|
|
535
|
+
} catch {
|
|
536
|
+
/* the trend ledger is a nicety; it must never fail the rendered view */
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
427
540
|
/**
|
|
428
541
|
* Exact view: replay one logical session log and keep only the durable
|
|
429
542
|
* instruction blocks.
|
|
@@ -549,10 +662,57 @@ export default function createModule(core) {
|
|
|
549
662
|
const p = paramsOf(params)
|
|
550
663
|
return chainValue(cwdOf(p))
|
|
551
664
|
},
|
|
552
|
-
/** File-side rendered text passed through from module A. */
|
|
665
|
+
/** File-side rendered text passed through from module A (also samples the size ledger). */
|
|
553
666
|
'instruction.preview': async (params) => {
|
|
554
667
|
const p = paramsOf(params)
|
|
555
|
-
|
|
668
|
+
const value = await previewValue(cwdOf(p))
|
|
669
|
+
await recordTrend(value)
|
|
670
|
+
return value
|
|
671
|
+
},
|
|
672
|
+
/**
|
|
673
|
+
* Instruction-size trend: samples recorded whenever a files-side view is
|
|
674
|
+
* produced, grouped by local day. Read-only; nothing here touches an
|
|
675
|
+
* instruction file.
|
|
676
|
+
*/
|
|
677
|
+
'instruction.trend': async (params) => {
|
|
678
|
+
const p = paramsOf(params)
|
|
679
|
+
const days = Math.trunc(util.numParam(p, 'days', { min: 1, max: 180, fallback: 30 }))
|
|
680
|
+
/** @type {Record<string, any>} */
|
|
681
|
+
let trend = { version: 1, samples: [] }
|
|
682
|
+
try {
|
|
683
|
+
if (typeof core.storage?.readState === 'function') {
|
|
684
|
+
const state = await core.storage.readState()
|
|
685
|
+
if (state.trend !== null && typeof state.trend === 'object' && Array.isArray(state.trend.samples)) trend = state.trend
|
|
686
|
+
}
|
|
687
|
+
} catch (error) {
|
|
688
|
+
return {
|
|
689
|
+
available: false,
|
|
690
|
+
reason: '读取趋势数据失败',
|
|
691
|
+
hint: util.redact(String(/** @type {any} */ (error)?.message ?? error), ERROR_HINT_MAX),
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
const samples = trend.samples.filter((/** @type {any} */ entry) => entry !== null && typeof entry === 'object' && Number.isFinite(entry.at))
|
|
695
|
+
const series = groupTrendByDay(samples, days)
|
|
696
|
+
const recorded = series.filter((entry) => entry.samples > 0)
|
|
697
|
+
const first = recorded[0]
|
|
698
|
+
const last = recorded[recorded.length - 1]
|
|
699
|
+
return {
|
|
700
|
+
available: true,
|
|
701
|
+
mode: 'trend',
|
|
702
|
+
days,
|
|
703
|
+
series,
|
|
704
|
+
recordedDays: recorded.length,
|
|
705
|
+
totalSamples: samples.length,
|
|
706
|
+
deltaBytes: first === undefined || last === undefined ? 0 : last.lastBytes - first.avgBytes,
|
|
707
|
+
firstDay: first === undefined ? null : first.day,
|
|
708
|
+
lastDay: last === undefined ? null : last.day,
|
|
709
|
+
budget: last === undefined ? null : last.budget,
|
|
710
|
+
overBudgetDays: recorded.filter((entry) => entry.budget !== null && entry.maxBytes > entry.budget).length,
|
|
711
|
+
minIntervalMs: TREND_MIN_INTERVAL_MS,
|
|
712
|
+
retentionDays: TREND_RETENTION_DAYS,
|
|
713
|
+
maxSamples: TREND_MAX_SAMPLES,
|
|
714
|
+
note: '样本在每次生成「按文件」视图时记录(同一目录 10 分钟内只记一次,最多 500 条、保留 180 天);只记录字节与预算,不记录指令内容。',
|
|
715
|
+
}
|
|
556
716
|
},
|
|
557
717
|
/** Exact instruction blocks recorded in one session log. */
|
|
558
718
|
'instruction.session': async (params) => {
|