dsh-aiyayi 1.5.39 → 1.5.40
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 +15 -1
- package/README.md +1 -1
- package/lib/toolkit.js +117 -35
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
> **维护约定**:新版本发布时在顶部加条目。本文件由 README 正文拆出(2026-11-09 重构):版本条目与踩坑记录原文照录,数字、时间、日期一律不改(量化保真是本插件的规矩)。版本事实以 npm registry 为准。
|
|
4
4
|
>
|
|
5
|
-
> **npm 发布一览(2026-11-12 核对 registry,`npm view dsh-aiyayi versions --json`)**:latest = **1.5.38**(2026-11-12 已发布;本次发版版为 **1.5.39**——toolkit 三件套注册失败修复 + feishu_bind 自动绑定,publish 留待下一步单独执行,发布后以 registry 为准)。已发布:1.0.0、…、1.5.7、**1.5.14**、**1.5.15**、**1.5.16**、**1.5.17**、**1.5.20**、**1.5.21**、**1.5.22**、**1.5.23**、**1.5.24**、**1.5.25**、**1.5.26**、**1.5.27**、**1.5.28**、**1.5.29**、**1.5.30**、**1.5.31**、**1.5.32**、**1.5.33**、**1.5.34**、**1.5.35**、**1.5.36**、**1.5.37**;跳号 1.3.0、1.4.1、1.4.2、1.5.0、1.5.1、1.5.2、1.5.3、**1.5.8–1.5.13** 从未发布到 npm(本地开发线:1.5.8–1.5.11 飞书桥接、1.5.12 思考纪律约束、1.5.13 内置技能同步+feishu-setup 骨架);1.5.14 已发布(坑 #58–#67 修复);1.5.15 已发布(扫码接入改造);1.5.16 已发布(feishu-setup 技能脚本入包);1.5.17 已发布(坑 #68 重入保护修复);**1.5.18 / 1.5.19 也从未发布到 npm**(见下方 1.5.18 / 1.5.19 条目——它们的代码最终随 1.5.20 一起进 npm);**1.5.20 已发布**(飞书正文改走 interactive 卡片 + lark_md,含 1.5.19 附件收发与问答卡片、1.5.20 markdown 渲染,以及 create-bot v6 权限集中化);**1.5.21 已发布**(修飞书回复正文首尾空白行,坑 #72);**1.5.22 已发布**(多应用多长连接改造+chat 归属机制,见下方 1.5.22 条目);**1.5.23–1.5.25 已发布**(230001 边界/补 import/补 feishuCode 挖码三轮装版断言增量修复,见下方对应条目);**1.5.26 已发布**(回发多文本段覆盖丢段修复,坑 #73,见下方 1.5.26 条目);**1.5.27 已发布**(微信通道并入,默认关闭,见下方 1.5.27 条目);**1.5.28 已发布**(PowerShell 版本体检 + 微信通道收尾,见下方 1.5.28 条目);**1.5.29 已发布**(发布包排除 `test/` + 测试残留空目录清理,见下方 1.5.29 条目);**1.5.30 已发布**(create-bot 注释改用 `<记忆根>` 占位,见下方 1.5.30 条目);**1.5.31 已发布**(PowerShell 体检 `major:null` 根因修复:探测链补上 powershell.exe 一级,见下方 1.5.31 条目);**1.5.32 已发布**(行为变更:首次提示同时告知微信 ClawBot 能力 + 「对接默认落到发起对接的会话」固化为 persona 准则/文档;不含路由代码改动,见下方 1.5.32 条目);**1.5.33 已发布**(同一准则落成代码:未绑定 chat 三档复用,不再无条件新建会话,见下方 1.5.33 条目);**1.5.34 已发布**(纯测试修复:`presence-clock` 跨午夜偶发失败,`lib/` 零改动,见下方 1.5.34 条目);**1.5.35 已发布**(`feishu_status` 新增最近入站路由回看 `inboundRouting` 字段,见下方 1.5.35 条目);**1.5.36 已发布**(飞书连接状态失真修复,见下方 1.5.36 条目);**1.5.37 已发布**(压缩前沉淀机制 precompact,见下方 1.5.37 条目);**1.5.38 已发布**(工具包中间层首批 toolkit_cmd / toolkit_read / toolkit_write,见下方 1.5.38 条目);**1.5.39 为本次发版版**(toolkit 三件套注册失败修复 + feishu_bind 自动绑定,见下方 1.5.39 条目;publish 留待下一步,以 registry
|
|
5
|
+
> **npm 发布一览(2026-11-12 核对 registry,`npm view dsh-aiyayi versions --json`)**:latest = **1.5.38**(2026-11-12 已发布;本次发版版为 **1.5.39**——toolkit 三件套注册失败修复 + feishu_bind 自动绑定,publish 留待下一步单独执行,发布后以 registry 为准)。已发布:1.0.0、…、1.5.7、**1.5.14**、**1.5.15**、**1.5.16**、**1.5.17**、**1.5.20**、**1.5.21**、**1.5.22**、**1.5.23**、**1.5.24**、**1.5.25**、**1.5.26**、**1.5.27**、**1.5.28**、**1.5.29**、**1.5.30**、**1.5.31**、**1.5.32**、**1.5.33**、**1.5.34**、**1.5.35**、**1.5.36**、**1.5.37**;跳号 1.3.0、1.4.1、1.4.2、1.5.0、1.5.1、1.5.2、1.5.3、**1.5.8–1.5.13** 从未发布到 npm(本地开发线:1.5.8–1.5.11 飞书桥接、1.5.12 思考纪律约束、1.5.13 内置技能同步+feishu-setup 骨架);1.5.14 已发布(坑 #58–#67 修复);1.5.15 已发布(扫码接入改造);1.5.16 已发布(feishu-setup 技能脚本入包);1.5.17 已发布(坑 #68 重入保护修复);**1.5.18 / 1.5.19 也从未发布到 npm**(见下方 1.5.18 / 1.5.19 条目——它们的代码最终随 1.5.20 一起进 npm);**1.5.20 已发布**(飞书正文改走 interactive 卡片 + lark_md,含 1.5.19 附件收发与问答卡片、1.5.20 markdown 渲染,以及 create-bot v6 权限集中化);**1.5.21 已发布**(修飞书回复正文首尾空白行,坑 #72);**1.5.22 已发布**(多应用多长连接改造+chat 归属机制,见下方 1.5.22 条目);**1.5.23–1.5.25 已发布**(230001 边界/补 import/补 feishuCode 挖码三轮装版断言增量修复,见下方对应条目);**1.5.26 已发布**(回发多文本段覆盖丢段修复,坑 #73,见下方 1.5.26 条目);**1.5.27 已发布**(微信通道并入,默认关闭,见下方 1.5.27 条目);**1.5.28 已发布**(PowerShell 版本体检 + 微信通道收尾,见下方 1.5.28 条目);**1.5.29 已发布**(发布包排除 `test/` + 测试残留空目录清理,见下方 1.5.29 条目);**1.5.30 已发布**(create-bot 注释改用 `<记忆根>` 占位,见下方 1.5.30 条目);**1.5.31 已发布**(PowerShell 体检 `major:null` 根因修复:探测链补上 powershell.exe 一级,见下方 1.5.31 条目);**1.5.32 已发布**(行为变更:首次提示同时告知微信 ClawBot 能力 + 「对接默认落到发起对接的会话」固化为 persona 准则/文档;不含路由代码改动,见下方 1.5.32 条目);**1.5.33 已发布**(同一准则落成代码:未绑定 chat 三档复用,不再无条件新建会话,见下方 1.5.33 条目);**1.5.34 已发布**(纯测试修复:`presence-clock` 跨午夜偶发失败,`lib/` 零改动,见下方 1.5.34 条目);**1.5.35 已发布**(`feishu_status` 新增最近入站路由回看 `inboundRouting` 字段,见下方 1.5.35 条目);**1.5.36 已发布**(飞书连接状态失真修复,见下方 1.5.36 条目);**1.5.37 已发布**(压缩前沉淀机制 precompact,见下方 1.5.37 条目);**1.5.38 已发布**(工具包中间层首批 toolkit_cmd / toolkit_read / toolkit_write,见下方 1.5.38 条目);**1.5.39 为本次发版版**(toolkit 三件套注册失败修复 + feishu_bind 自动绑定,见下方 1.5.39 条目;publish 留待下一步,以 registry 为准);**1.5.40 为当前开发版**(toolkit 执行输出对齐 output.schema——宿主对输出也做 schema 校验,见下方 1.5.40 条目与坑 #78;仅本地提交,不 push 不 publish)。
|
|
6
6
|
>
|
|
7
7
|
> **环境标识约定(2026-11-10 新增)**:写版本条目/坑条目时,**顺手记录当时的 智能体工具-智能体模式-模型供应商-模型**(如 **DSH-创造模式-loomy讯飞-GLM5.3FLASH**),写在条目内(紧跟 git 记录行、或坑条目首行),方便回溯该条目是在哪套智能体/模型环境下产出与验证的。
|
|
8
8
|
|
|
@@ -10,6 +10,18 @@
|
|
|
10
10
|
|
|
11
11
|
---
|
|
12
12
|
|
|
13
|
+
## 1.5.40(2026-11-12 修复:toolkit_cmd / toolkit_write 执行输出不符 output.schema——宿主对工具输出也做 schema 校验,条件性返回字段必须与 schema 对齐,坑 #78)
|
|
14
|
+
|
|
15
|
+
- 环境:DSH-执行智能体(DeepSeek Harness 子会话,模型名未确证)
|
|
16
|
+
- **改了什么(只动 `lib/toolkit.js` + 测试;三工具的行为语义仅"约定值"变化,无对外参数变更)**:
|
|
17
|
+
- **根因(实锤,详见坑 #78)**:`lib/toolkit.js` 三个工具的 `execute` 返回值是**条件性返回**——`toolkit_cmd` 的 `truncated` 只在部分路径出现、`exitCode` 超时/被拒时给 `null`;`toolkit_write` 的 `backupPath` 仅覆盖已有文件时才有,其余给 `null`;`toolkit_read` 的 `mtime` 探测不到时给 `null`。而 **DSH 宿主不只校验注册端 schema,也校验执行输出**:`validateJsonSchemaValue(output.schema, value)` 会把 `null` 当成"字段存在但类型不符"(**不等于缺省**)、`required` 缺一即报 `missing required property`。1.5.39 把 type 改成单类型后实现层没跟上,于是注册通过、一执行就被拒——实测 `toolkit_cmd` 报 `missing required property 'value.truncated'; 'value.exitCode' must be an integer`,`toolkit_write` 报 `'value.backupPath' must be a string`,`toolkit_read` 因不返回 null 字段而幸免。
|
|
18
|
+
- **修法(单一出口归一,不靠"记得每个分支都填")**:新增 `shapeCmdOutput` / `shapeReadOutput` / `shapeWriteOutput` 三个导出函数,三个工具的 `finish()` 一律先过它再返回——所有路径(成功/拒绝/超时/进程拉不起来/内部异常)压成与 `output.schema` 完全一致的形状:字段一个不少、每个字段单类型、不出现 schema 之外的键。可空语义用**约定值**承担并在 description 写明:`exitCode` 拿不到真实退出码时填 **-1**(配合 `ok`/`denied`/`timedOut` 判别)、`backupPath` 未备份填 **空字符串**、`mtime` 探测不到填 **空字符串**、`truncated` 一律 boolean。
|
|
19
|
+
- **schema 随之收紧**:三处 `required` 从"只列部分字段"改为**列全 properties**(因为现在每个字段都恒有值);`toolkit_cmd` 的 `reason` / `whitelisted` / `error`、`toolkit_read` 的 `mtime` / `error`、`toolkit_write` 的 `backupPath` / `reason` / `error` 全部进 required;`toolkit_write` 的 `denied` / `reason` / `error` 一并补进(原 schema 有 properties 但 required 只列三个,属同类隐患)。`toolkit_cmd` 的 render 对 `exitCode < 0` 增注"无真实退出码"。
|
|
20
|
+
- **留痕口径不变**:`usage.jsonl` 的 `exitCode` 仍是 `null`(= 没有真实退出码),约定值只在输出端用,不污染统计。
|
|
21
|
+
- **为什么**:①调用方(本体/模型)会把校验失败当"操作失败",而**底层其实已经执行**——write 的目标文件真实写入了完整内容,cmd 的命令也跑了只是结果被吞,属"bug 兼 UX 问题";②"条件性返回"这种写法天生与 required 冲突,只要 schema 声明了字段就永远无法安全省略。
|
|
22
|
+
- **测试**:`test/toolkit.test.mjs` 新增 6c 节(**真实调用注册出来的 execute**,不是只测纯函数):复刻宿主 `validateJsonSchemaValue` 为守门员(单类型/required/additionalProperties:false/items/oneOf),先自检能抓出今天三种真实失败形态(缺 `truncated`、`exitCode:null`、`backupPath:null`),再逐路断言——`cmd` 成功(exitCode=0)/失败(exitCode=3 透传)/超时(-1 + timedOut)/拒绝(-1 + denied)四路、`write` 新建(backupPath 空串)/覆盖(非空且 .bak 真实存在)/拒绝(空串)三路、`read` 不存在路径(mtime 空串);另加归一函数对脏输入(全 null / 空对象)的兜底断言。**同时把 1.5.39 遗留的「exitCode/mtime/backupPath 不在 required」三条断言反向改写**——那是 1.5.39 用"缺省"表达可空的旧口径,与本版新口径直接冲突,留着就是错的。全量 `node --test`(显式传 35 个 `test/*.test.mjs` 文件——`test/` 下另有 `feishu-status-proof.mjs` / `presence-midnight-proof.mjs` 两个非 `*.test.mjs` 的取证脚本,不参与 `node --test`):**163 项,160 过 3 挂**(挂 = alarm-inject / home-relocate / tools-surface 环境基线,非本次引入,与 1.5.35/1.5.21/1.5.22/1.5.39 条目同基线),零新增失败。
|
|
23
|
+
- 打包与 npm publish **不在本次范围**:本版只做版本号对齐 + 本地提交(不 push、不 publish、不动 profile),发版节奏等李浩定。
|
|
24
|
+
|
|
13
25
|
## 1.5.39(2026-11-12 修复 + 改造:toolkit 三件套注册失败修复;feishu_bind 自动绑定一步接入)
|
|
14
26
|
|
|
15
27
|
- 环境:DSH-执行智能体(DeepSeek Harness 子会话,模型名未确证)
|
|
@@ -1006,3 +1018,5 @@ L571 agent_id: 7553531f-3345-45d1-a455-50a56e2c 5ca7 → message delivered
|
|
|
1006
1018
|
回归测试 `test/feishu-trim.test.mjs` 锁死:无标记路径首尾干净、有标记路径逐字节不变、中间空行不折叠、空/全空白输入不炸。
|
|
1007
1019
|
|
|
1008
1020
|
77. **宿主 tools.register() 的 JSON Schema 校验只接受单一 type 字符串;type 数组必被 UNSUPPORTED_SCHEMA 拒,且注册失败被逐工具 try/catch 吞成 warn、表面看不出(2026-11-12 实锤;1.5.39 修)**:工具定义的 `parameters` / `output.schema` 里任何属性写 `type: ['integer','null']` 这类**数组**,宿主注册校验直接报 "must be a single type string (type arrays are not supported)"(错误类 UNSUPPORTED_SCHEMA)→ 该工具注册失败。更险的是插件 `apply()` 里逐工具 try/catch(toolkit.js 约 921-927 行)把注册异常吞成 `logger.warn`——**插件照常激活、表面一切正常**,工具就是不出现;1.5.38 的 toolkit 三件套就这么整版注册失败,直到下版排查才发现(1.5.38 全部三个工具实际都没装上)。修法:可空语义一律写单类型 + 字段不进 required + 在 description 注明缺省(如 `exitCode: { type: 'integer', description: '…为 null(缺省)' }`)。排查教训:**别拿"插件激活成功"当"工具注册成功"**——注册是否成功要看注册返回/注册表与运行日志里的 warn;回归防线见 `test/toolkit.test.mjs` 6b 节(复刻宿主校验规则逐层扫 type、守门员自检能抓出 type 数组写法)。
|
|
1021
|
+
|
|
1022
|
+
78. **宿主不只校验注册端 schema,也校验「执行输出」;而 `null` ≠ 缺省——条件性返回字段必须与 schema 对齐,否则操作成功、调用方只看到一句校验错误(2026-11-12 实锤;1.5.40 修)**:宿主把工具 `execute` 的返回值拿去跑 `validateJsonSchemaValue(output.schema, value)`(内置 JSON Schema 子集校验器),命中三类违例即判输出非法:①`required` 里的字段缺一个 → `missing required property "value.x"`;②字段存在但类型不符 → `"value.x" must be an integer/string/boolean`——**`null` 算"存在且类型错",不是缺省**;③`additionalProperties: false` 时多了没声明的键也会违例。1.5.39 把 type 改成单类型后,实现层仍是「条件性返回」(`toolkit_cmd` 的 `truncated` 部分路径不出现、`exitCode` 超时/被拒给 `null`;`toolkit_write` 的 `backupPath` 新建给 `null`;`toolkit_read` 的 `mtime` 探测不到给 `null`),于是**注册全过、一执行就被拒**:`toolkit_cmd` 报 `missing required property 'value.truncated'; 'value.exitCode' must be an integer`,`toolkit_write` 报 `'value.backupPath' must be a string`(`toolkit_read` 因不返回 null 字段而幸免)。最坑的一点是**报错发生在校验环节、底层操作其实已经执行**——write 的目标文件真实写入了 63 字节完整内容,cmd 的命令也跑了只是结果被吞,调用方会把校验失败误当"操作失败"。修法:①**单一出口归一**——三个工具所有返回路径(成功/拒绝/超时/拉不起进程/内部异常)一律先过 `shapeCmdOutput`/`shapeReadOutput`/`shapeWriteOutput`,压成与 `output.schema` 逐字段一致的形状(字段不少、单类型、无额外键),别指望"记得每个分支都填";②可空语义用**约定值**承担并在 description 写明:`exitCode` 无真实退出码填 **-1**(配合 `ok`/`denied`/`timedOut` 判别)、`backupPath` 未备份填**空串**、`mtime` 探测不到填**空串**、`truncated` 恒 boolean;③`required` 列全 properties(字段恒有值后才敢这么写),留痕 `usage.jsonl` 仍保留 `null` 旧语义不污染统计。教训:**改 schema 必须同步审实现层的每一个返回路径**(grep 该工具所有 `finish()`/`return` 出口),且**可空字段有两种表达法——"不进 required"(1.5.39 口径)与"进 required + 约定值"(本版口径)——混用就是这次翻车**;注册层测试(6b)永远抓不到输出层问题,必须有"真实调用 execute + 逐字段过校验"的回归(6c 节,含守门员自检能复现今天三种真实报错)。排障顺序也变了:**输出校验失败时先核实底层实际效果**(文件在不在、命令有没有副作用),再定论"失败"。
|
package/README.md
CHANGED
|
@@ -73,7 +73,7 @@ npm 包为平铺结构(仓库若保留 bundle/ 子目录,发布时把其内
|
|
|
73
73
|
- `lib/doc.js` — aiyayi-doc 文档解析插件(1.4.0,挂检索员 preset;ZIP/XML 最小解析零依赖 + 动态加载 shipped libreoffice-kit 做 pdf 页图与旧版转换)
|
|
74
74
|
- `lib/own-tools.js` — 她的工具名单单一真源(注册处登记、放行处判定,防手写白名单漂移)
|
|
75
75
|
- `lib/precompact.js` — aiyayi-precompact 压缩前沉淀检查插件(1.5.37;在 dsh-compaction-basic 动手压缩前,把值得沉淀的任务闭环/结论/新经验先落盘为经验文档+记忆+索引,失败不阻塞主流程;`precompact_run` 显式工具入口 + 接近阈值自动触发)
|
|
76
|
-
- `lib/toolkit.js` — aiyayi-toolkit 工具包中间层插件(1.5.38;首批 3 工具:`toolkit_cmd` 只读命令白名单执行 / `toolkit_read` 文件探测 + 小文件读取 / `toolkit_write` 受限写入;调用留痕落 `<记忆根>/toolkit/usage.jsonl`;简单状态型查询本体直调、不派小伙伴。1.5.39 修复 type 数组 schema 导致三件套注册全败的问题,坑 #77)
|
|
76
|
+
- `lib/toolkit.js` — aiyayi-toolkit 工具包中间层插件(1.5.38;首批 3 工具:`toolkit_cmd` 只读命令白名单执行 / `toolkit_read` 文件探测 + 小文件读取 / `toolkit_write` 受限写入;调用留痕落 `<记忆根>/toolkit/usage.jsonl`;简单状态型查询本体直调、不派小伙伴。1.5.39 修复 type 数组 schema 导致三件套注册全败的问题,坑 #77;1.5.40 修执行输出对齐 output.schema——宿主对工具输出也做 schema 校验,条件性返回字段一律用约定值,坑 #78)
|
|
77
77
|
- `lib/feishu.js` — aiyayi-feishu 飞书桥接宿主插件(1.5.8 起;跨会话服务,长连接收消息、按指令路由到 aiyayi 会话、回发;凭读取记忆根 `feishu/config.json`)
|
|
78
78
|
- `lib/feishu-logic.js` — 飞书纯逻辑(路由/映射/文本抽取/三档复用决策/**长连接状态机**,无外部依赖,可单测;test/feishu-route.test.mjs、test/feishu-reuse-session.test.mjs、test/feishu-status.test.mjs 覆盖)
|
|
79
79
|
- `skills/feishu-setup/` — 内置技能(1.5.13 起随包自动同步到 DSH 技能根):飞书机器人安装/验证/排障全流程(含 2026-11-10 扫码接入改造说明;脚本 `scripts/create-bot.js` v5 等由技能同步维护)
|
package/lib/toolkit.js
CHANGED
|
@@ -101,6 +101,81 @@ export const DEFAULT_CONFIG = {
|
|
|
101
101
|
|
|
102
102
|
const TEXT_BLOCK = (text) => [{ type: 'text', text }]
|
|
103
103
|
|
|
104
|
+
/* ------------------------------------------------------------------ *
|
|
105
|
+
* 输出形状归一(1.5.40 修,坑 #78)
|
|
106
|
+
*
|
|
107
|
+
* **为什么需要这一层**:宿主不只在注册端校验工具 schema,**在输出端也校验**——
|
|
108
|
+
* 工具 execute 的返回值会用 `validateJsonSchemaValue(output.schema, value)` 逐字段验:
|
|
109
|
+
* ①`required` 里的字段缺一个 → `missing required property "value.x"`;
|
|
110
|
+
* ②字段存在但类型不符 → `"value.x" must be an integer/string/boolean`(`null` 不等于缺省,
|
|
111
|
+
* 它会被当成"存在且类型错");
|
|
112
|
+
* ③`additionalProperties: false` 时返回了 schema 没声明的键 → `"value.x" is not a declared property`。
|
|
113
|
+
* 1.5.39 的三件套把 type 改成单类型后,实现层仍是"条件性返回"(exitCode 超时给 null、
|
|
114
|
+
* backupPath 新建给 null、truncated 部分路径干脆不出现)→ 执行其实成功了,宿主却判输出非法、
|
|
115
|
+
* 调用方只看到一句校验错误(底层写盘/命令都真跑了,UX 上像"失败")。
|
|
116
|
+
*
|
|
117
|
+
* 修法:**每个工具的 execute 返回值只从这一个归一函数出**,把所有路径(成功/拒绝/超时/
|
|
118
|
+
* 进程起不来/内部异常)都压成与 output.schema 完全一致的形状:字段一个不少、
|
|
119
|
+
* 每个字段都是单类型、不出现 schema 之外的键。语义映射:
|
|
120
|
+
* - `exitCode`:拿不到真实退出码(拒绝/超时/拉不起进程)时填 **-1**(不是真实退出码);
|
|
121
|
+
* - `backupPath`:未产生备份(新建文件/未走到备份)时填 **空字符串**;
|
|
122
|
+
* - `mtime`:探测不到时填 **空字符串**(read 同款问题,一并修);
|
|
123
|
+
* - `truncated`:一律 boolean,不省缺。
|
|
124
|
+
* 留痕(usage.jsonl)仍保留"null = 没有值"的旧语义,不影响统计口径。
|
|
125
|
+
* ------------------------------------------------------------------ */
|
|
126
|
+
|
|
127
|
+
/** 非负整数归一:不是合法整数的退回缺省(防 NaN/浮点/字符串混进 integer 字段)。 */
|
|
128
|
+
function intOr(value, fallback) {
|
|
129
|
+
return Number.isInteger(value) ? value : fallback
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** toolkit_cmd 输出归一:字段与 CMD_SCHEMA.output.schema 一一对应。 */
|
|
133
|
+
export function shapeCmdOutput(result = {}) {
|
|
134
|
+
return {
|
|
135
|
+
ok: result.ok === true,
|
|
136
|
+
cmd: String(result.cmd ?? ''),
|
|
137
|
+
stdout: String(result.stdout ?? ''),
|
|
138
|
+
stderr: String(result.stderr ?? ''),
|
|
139
|
+
exitCode: intOr(result.exitCode, -1),
|
|
140
|
+
durationMs: intOr(result.durationMs, 0),
|
|
141
|
+
truncated: result.truncated === true,
|
|
142
|
+
denied: result.denied === true,
|
|
143
|
+
timedOut: result.timedOut === true,
|
|
144
|
+
reason: String(result.reason ?? ''),
|
|
145
|
+
whitelisted: String(result.whitelisted ?? ''),
|
|
146
|
+
error: String(result.error ?? ''),
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** toolkit_read 输出归一:字段与 READ_SCHEMA.output.schema 一一对应。 */
|
|
151
|
+
export function shapeReadOutput(result = {}) {
|
|
152
|
+
return {
|
|
153
|
+
ok: result.ok === true,
|
|
154
|
+
exists: result.exists === true,
|
|
155
|
+
path: String(result.path ?? ''),
|
|
156
|
+
size: intOr(result.size, 0),
|
|
157
|
+
mtime: String(result.mtime ?? ''), // 探测不到 = 空串(不是 null,宿主按 string 校验)
|
|
158
|
+
content: String(result.content ?? ''),
|
|
159
|
+
isFile: result.isFile === true,
|
|
160
|
+
isDir: result.isDir === true,
|
|
161
|
+
truncated: result.truncated === true,
|
|
162
|
+
error: String(result.error ?? ''),
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** toolkit_write 输出归一:字段与 WRITE_SCHEMA.output.schema 一一对应。 */
|
|
167
|
+
export function shapeWriteOutput(result = {}) {
|
|
168
|
+
return {
|
|
169
|
+
ok: result.ok === true,
|
|
170
|
+
path: String(result.path ?? ''),
|
|
171
|
+
bytes: intOr(result.bytes, 0),
|
|
172
|
+
backupPath: String(result.backupPath ?? ''), // 未备份 = 空串(不是 null)
|
|
173
|
+
denied: result.denied === true,
|
|
174
|
+
reason: String(result.reason ?? ''),
|
|
175
|
+
error: String(result.error ?? ''),
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
104
179
|
/** 单次调用的留痕记录骨架(字段齐全,方便以后按字段做统计)。 */
|
|
105
180
|
function makeRec(tool, argsSummary) {
|
|
106
181
|
return {
|
|
@@ -360,18 +435,19 @@ export async function runCmd(args, cfg, log) {
|
|
|
360
435
|
const rec = makeRec('toolkit_cmd', capSummary(redactSensitive(cmd)))
|
|
361
436
|
const started = Date.now()
|
|
362
437
|
|
|
363
|
-
/**
|
|
364
|
-
const finish = (
|
|
438
|
+
/** 统一出口:归一输出形状(坑 #78)→ 填留痕 → 返回。 */
|
|
439
|
+
const finish = (raw, status) => {
|
|
440
|
+
// 归一必须在这里做:宿主校验的是 execute 的返回值,任何一条路径漏字段/给 null 都会被拒。
|
|
441
|
+
const result = shapeCmdOutput({ ...raw, durationMs: Date.now() - started })
|
|
365
442
|
rec.status = status
|
|
366
|
-
rec.durationMs =
|
|
367
|
-
rec.outBytes =
|
|
368
|
-
rec.truncated = result.truncated
|
|
369
|
-
rec.ok = result.ok
|
|
370
|
-
|
|
371
|
-
rec.
|
|
372
|
-
rec.
|
|
373
|
-
|
|
374
|
-
result.durationMs = rec.durationMs
|
|
443
|
+
rec.durationMs = result.durationMs
|
|
444
|
+
rec.outBytes = result.stdout.length + result.stderr.length
|
|
445
|
+
rec.truncated = result.truncated
|
|
446
|
+
rec.ok = result.ok
|
|
447
|
+
// 留痕沿用旧语义(null = 没有真实退出码),输出端才用约定值 -1。
|
|
448
|
+
rec.exitCode = result.exitCode < 0 ? null : result.exitCode
|
|
449
|
+
rec.denyReason = result.denied === true ? result.reason : ''
|
|
450
|
+
rec.error = result.error
|
|
375
451
|
try {
|
|
376
452
|
log?.(rec)
|
|
377
453
|
} catch {
|
|
@@ -511,13 +587,14 @@ export function doRead(args, cfg, log) {
|
|
|
511
587
|
const rawPath = String(args?.path ?? '').trim()
|
|
512
588
|
const rec = makeRec('toolkit_read', capSummary(redactSensitive(rawPath)))
|
|
513
589
|
const started = Date.now()
|
|
514
|
-
const finish = (
|
|
590
|
+
const finish = (raw, status) => {
|
|
591
|
+
const result = shapeReadOutput(raw)
|
|
515
592
|
rec.status = status
|
|
516
593
|
rec.durationMs = Date.now() - started
|
|
517
|
-
rec.outBytes = result.content
|
|
518
|
-
rec.truncated = result.truncated
|
|
519
|
-
rec.ok = result.ok
|
|
520
|
-
rec.error =
|
|
594
|
+
rec.outBytes = result.content.length
|
|
595
|
+
rec.truncated = result.truncated
|
|
596
|
+
rec.ok = result.ok
|
|
597
|
+
rec.error = result.error
|
|
521
598
|
try {
|
|
522
599
|
log?.(rec)
|
|
523
600
|
} catch {
|
|
@@ -632,13 +709,14 @@ export function doWrite(args, cfg, log) {
|
|
|
632
709
|
const rawPath = String(args?.path ?? '').trim()
|
|
633
710
|
const rec = makeRec('toolkit_write', capSummary(redactSensitive(rawPath)))
|
|
634
711
|
const started = Date.now()
|
|
635
|
-
const finish = (
|
|
712
|
+
const finish = (raw, status) => {
|
|
713
|
+
const result = shapeWriteOutput(raw)
|
|
636
714
|
rec.status = status
|
|
637
715
|
rec.durationMs = Date.now() - started
|
|
638
|
-
rec.outBytes = result.bytes
|
|
639
|
-
rec.denyReason = result.denied === true ?
|
|
640
|
-
rec.ok = result.ok
|
|
641
|
-
rec.error =
|
|
716
|
+
rec.outBytes = result.bytes
|
|
717
|
+
rec.denyReason = result.denied === true ? result.reason : ''
|
|
718
|
+
rec.ok = result.ok
|
|
719
|
+
rec.error = result.error
|
|
642
720
|
try {
|
|
643
721
|
log?.(rec)
|
|
644
722
|
} catch {
|
|
@@ -752,20 +830,24 @@ const CMD_SCHEMA = {
|
|
|
752
830
|
cmd: { type: 'string' },
|
|
753
831
|
stdout: { type: 'string' },
|
|
754
832
|
stderr: { type: 'string' },
|
|
755
|
-
exitCode: {
|
|
833
|
+
exitCode: {
|
|
834
|
+
type: 'integer',
|
|
835
|
+
description: '进程退出码。恒为整数:命令被拒绝、超时被杀、进程拉不起来等拿不到真实退出码的场景填 -1(约定值,不等于真实退出码,以 ok/denied/timedOut 判别)。',
|
|
836
|
+
},
|
|
756
837
|
durationMs: { type: 'integer' },
|
|
757
|
-
truncated: { type: 'boolean' },
|
|
838
|
+
truncated: { type: 'boolean', description: '输出是否被截断。恒为布尔值(不省缺)。' },
|
|
758
839
|
denied: { type: 'boolean' },
|
|
759
840
|
timedOut: { type: 'boolean' },
|
|
760
|
-
reason: { type: 'string' },
|
|
761
|
-
whitelisted: { type: 'string' },
|
|
841
|
+
reason: { type: 'string', description: '拒绝/失败时的说明;正常时为空字符串。' },
|
|
842
|
+
whitelisted: { type: 'string', description: '命中的只读白名单前缀;被拒绝时为空字符串。' },
|
|
843
|
+
error: { type: 'string', description: '底层错误信息;没有错误时为空字符串。' },
|
|
762
844
|
},
|
|
763
|
-
required: ['ok', 'cmd', 'stdout', 'stderr', 'durationMs', 'truncated', 'denied', 'timedOut'],
|
|
845
|
+
required: ['ok', 'cmd', 'stdout', 'stderr', 'exitCode', 'durationMs', 'truncated', 'denied', 'timedOut', 'reason', 'whitelisted', 'error'],
|
|
764
846
|
},
|
|
765
847
|
render: (_args, value) => {
|
|
766
848
|
if (value.denied) return TEXT_BLOCK(`已拒绝(只读白名单外):${value.reason}`)
|
|
767
849
|
if (value.timedOut) return TEXT_BLOCK(`超时(已杀进程):${value.cmd}\n${value.stdout ?? ''}`)
|
|
768
|
-
const head = `退出码 ${value.exitCode},耗时 ${value.durationMs}ms${value.truncated ? ',输出已截断' : ''}`
|
|
850
|
+
const head = `退出码 ${value.exitCode}${value.exitCode < 0 ? '(无真实退出码:拒绝/超时/拉起失败)' : ''},耗时 ${value.durationMs}ms${value.truncated ? ',输出已截断' : ''}`
|
|
769
851
|
const parts = [head]
|
|
770
852
|
if (value.stdout) parts.push(`--- stdout ---\n${value.stdout}`)
|
|
771
853
|
if (value.stderr) parts.push(`--- stderr ---\n${value.stderr}`)
|
|
@@ -797,14 +879,14 @@ const READ_SCHEMA = {
|
|
|
797
879
|
exists: { type: 'boolean' },
|
|
798
880
|
path: { type: 'string' },
|
|
799
881
|
size: { type: 'integer' },
|
|
800
|
-
mtime: { type: 'string', description: '最后修改时间(ISO
|
|
882
|
+
mtime: { type: 'string', description: '最后修改时间(ISO 字符串)。恒为字符串:目标不存在或探测失败时为空字符串(约定值,配合 exists 判别)。' },
|
|
801
883
|
content: { type: 'string' },
|
|
802
884
|
isFile: { type: 'boolean' },
|
|
803
885
|
isDir: { type: 'boolean' },
|
|
804
|
-
truncated: { type: 'boolean' },
|
|
805
|
-
error: { type: 'string' },
|
|
886
|
+
truncated: { type: 'boolean', description: '内容是否被 maxBytes 截断。恒为布尔值(不省缺)。' },
|
|
887
|
+
error: { type: 'string', description: '探测/读取失败的原因;正常时为空字符串。' },
|
|
806
888
|
},
|
|
807
|
-
required: ['ok', 'exists', 'path', 'size', 'isFile', 'isDir', 'truncated'],
|
|
889
|
+
required: ['ok', 'exists', 'path', 'size', 'mtime', 'content', 'isFile', 'isDir', 'truncated', 'error'],
|
|
808
890
|
},
|
|
809
891
|
render: (_args, value) => {
|
|
810
892
|
if (value.error && !value.ok) return TEXT_BLOCK(`读取失败:${value.path}(${value.error})`)
|
|
@@ -837,12 +919,12 @@ const WRITE_SCHEMA = {
|
|
|
837
919
|
ok: { type: 'boolean' },
|
|
838
920
|
path: { type: 'string' },
|
|
839
921
|
bytes: { type: 'integer' },
|
|
840
|
-
backupPath: { type: 'string', description: '覆盖已有文件时自动备份的 .bak
|
|
922
|
+
backupPath: { type: 'string', description: '覆盖已有文件时自动备份的 .bak 路径。恒为字符串:仅覆盖已有文件时非空,新建文件/未走到备份时为空字符串。' },
|
|
841
923
|
denied: { type: 'boolean' },
|
|
842
|
-
reason: { type: 'string' },
|
|
843
|
-
error: { type: 'string' },
|
|
924
|
+
reason: { type: 'string', description: '拒绝原因的说明;正常时为空字符串。' },
|
|
925
|
+
error: { type: 'string', description: '底层错误信息;没有错误时为空字符串。' },
|
|
844
926
|
},
|
|
845
|
-
required: ['ok', 'path', 'bytes'],
|
|
927
|
+
required: ['ok', 'path', 'bytes', 'backupPath', 'denied', 'reason', 'error'],
|
|
846
928
|
},
|
|
847
929
|
render: (_args, value) => {
|
|
848
930
|
if (value.denied) return TEXT_BLOCK(`写入已拒绝:${value.reason}`)
|