fix: v2.1.4 — 二轮加固批次(连接池并发安全 + exec 硬截止/背压 + build-verify 注入防护 + MCP ACL fail-closed + 路由词边界 + runner 逐命令审计 + exec_error 审计 + --since 校验 + 模式恢复容错)
This commit is contained in:
parent
33a2d1ba5a
commit
4c6f177b2c
26
AGENTS.md
26
AGENTS.md
@ -56,7 +56,7 @@ export default function (pi: ExtensionAPI) {
|
||||
|
||||
Four entry points are wired in `package.json` → `pi.extensions`:
|
||||
1. `extensions/provider.ts` — registers the `dg-robot` provider and 4 expert models
|
||||
2. `extensions/tools.ts` — registers all 12 `dg_*` tools (SSH/SFTP/discovery real, KB via MCP client, route/dispatch real, 5 compound workflows + mode evaluate)
|
||||
2. `extensions/tools.ts` — registers all 13 `dg_*` tools (SSH/SFTP/discovery real, KB via MCP client, route/dispatch real, 5 compound workflows + mode evaluate + workflow run)
|
||||
3. `extensions/system-prompt.ts` — injects RDK domain context via `before_agent_start`
|
||||
4. `extensions/telemetry.ts` — `pi.on` tool_execution span hooks (OpenTelemetry)
|
||||
|
||||
@ -104,6 +104,8 @@ lib/
|
||||
├── tros-env.ts # TROS_SETUP_CANDIDATES + sourceTrosEnvCommand (shared by workflows)
|
||||
├── mcp-client.ts # McpHttpClient + createMcpClientFromEnv (Wikihub, JSON-RPC over HTTP)
|
||||
├── mcp-server.ts # v1.6 MCP 服务端: handleMcpJsonRpc/handleMcpHttpBody (JSON-RPC over HTTP)
|
||||
├── debug.ts # DG_DEBUG stderr debug logging (isDebugEnabled/debugLog, zero-alloc no-op)
|
||||
├── compact.ts # v1.2 Context Engineering 输出压缩: maybeCompact (确定性启发式: 头+error/warning+尾+摘要)
|
||||
├── render.ts # pure render*() functions (data vs presentation separation)
|
||||
├── observability.ts # v0.5 观测层: readSessionLogEntries + aggregateUsage (usage stats)
|
||||
├── detector.ts # v1.4 Detector: detectCandidates 序列模式挖掘 + 候选持久化
|
||||
@ -123,9 +125,11 @@ lib/
|
||||
|
||||
`extensions/utils.ts` re-exports all lib/ symbols for backward compatibility with extensions/ and tests/ that import from `"./utils.ts"`. Pi-coupled helpers (provider registration, settings.json writer, mode constants) remain in `utils.ts`.
|
||||
|
||||
### Slash Commands (`prompts/*.md`)
|
||||
### Slash Commands
|
||||
|
||||
Frontmatter: `description`, optional `argument-hint` (`<required> [optional]`). Body uses `$1`, `$2`, `$@`, `${1:-default}` for args. Filename (without `.md`) becomes the command — `dg-help.md` → `/dg-help`.
|
||||
Static commands (`prompts/*.md`): frontmatter `description`, optional `argument-hint` (`<required> [optional]`); body uses `$1`, `$2`, `$@`, `${1:-default}` for args. Filename (without `.md`) becomes the command — `dg-help.md` → `/dg-help`.
|
||||
|
||||
Runtime commands (`/dg-mode` `/dg-setup` `/dg-usage` `/dg-suggest` `/dg-accept` `/dg-registry` `/dg-autoloop`) are registered in `extensions/system-prompt.ts` via `pi.registerCommand()`, NOT static `.md` files.
|
||||
|
||||
### Skills (`skills/<name>/SKILL.md`)
|
||||
|
||||
@ -144,6 +148,10 @@ The extension reads these from the host environment:
|
||||
| `DG_MCP_TOKEN` | MCP bearer token | `dg_kb_search` (v0.3+) |
|
||||
| `DG_SSH_KEY` | SSH private key path (default: `~/.ssh/id_rsa`) | `dg_ssh_exec` (v0.3+) |
|
||||
| `DG_DEBUG` | `1`/`true`/`yes`/`on` 时开启 stderr 调试日志(ssh/discovery/mcp/routing) | 调试(v1.0.3+) |
|
||||
| `DG_SSH_TIMEOUT_MS` | SSH 命令超时毫秒(default `60000`, range [1000, 600000]) | `dg_ssh_exec` (v1.2+) |
|
||||
| `DG_OTEL_ENDPOINT` | OTLP exporter endpoint;设置时从 JsonlSpanExporter 切换 OTLP/HTTP | OpenTelemetry(v1.0+) |
|
||||
| `DG_OTEL_ENABLED` | `0` 时全局禁用 OpenTelemetry(no-op tracer) | 可选 |
|
||||
| `DG_AUTOLOOP` | `1`/`true`/`yes`/`on` 时 session_start 自动触发 `/dg-autoloop` | 可选(v2.0+) |
|
||||
|
||||
`/dg-setup` walks the user through these and persists to `~/.pi/agent/settings.json` env block.
|
||||
|
||||
@ -159,7 +167,7 @@ dgagent/
|
||||
├── extensions/
|
||||
│ ├── provider.ts # dg-robot provider + 4 expert models (delegates to utils)
|
||||
│ ├── tools.ts # all 13 dg_* tools (SSH/SFTP/discovery real; KB via MCP; route/dispatch real; workflow run)
|
||||
│ ├── system-prompt.ts # before_agent_start — RDK context + mode + /dg-setup + /dg-mode
|
||||
│ ├── system-prompt.ts # before_agent_start — RDK context + mode + 7 runtime slash commands
|
||||
│ ├── telemetry.ts # pi.on tool_execution span hooks (OpenTelemetry)
|
||||
│ └── utils.ts # lib/ re-exports + Pi-coupled: SSH pool, exec, SFTP, discovery,
|
||||
│ # config IO, provider helper, settings writer
|
||||
@ -195,8 +203,8 @@ dgagent/
|
||||
│ ├── discussion-transcript.md # design session transcript
|
||||
│ ├── dg-help.md # /dg-help
|
||||
│ └── dg-status.md # /dg-status
|
||||
│ # NOTE: /dg-mode, /dg-setup, /dg-usage, /dg-suggest, /dg-accept, /dg-registry are registered at runtime
|
||||
│ # via system-prompt.ts registerCommand(), not static .md files.
|
||||
│ # NOTE: /dg-mode, /dg-setup, /dg-usage, /dg-suggest, /dg-accept, /dg-registry,
|
||||
│ # /dg-autoloop are registered at runtime via system-prompt.ts registerCommand(), not static .md files.
|
||||
├── AGENTS.md # this file
|
||||
├── DESIGN.md # full design doc (architecture, requirements)
|
||||
├── METHODOLOGY.md # design principles + methodology + ADRs
|
||||
@ -219,7 +227,7 @@ If you are about to make a change that contradicts any of these, STOP and discus
|
||||
|
||||
---
|
||||
|
||||
## Current Status (v1.1)
|
||||
## Current Status (v2.1.4)
|
||||
|
||||
### What's real
|
||||
|
||||
@ -250,6 +258,7 @@ If you are about to make a change that contradicts any of these, STOP and discus
|
||||
- **v1.6 Layer 2 接入层** — `lib/mcp-server.ts` `handleMcpJsonRpc`/`handleMcpHttpBody`(MCP JSON-RPC over HTTP:initialize/tools/list/tools/call,工具 workflow_list/usage_stats/candidate_list,错误 fail-closed)+ `bin/dg.mjs` CLI(usage/suggest/registry/accept/serve-mcp,Node ≥ 23.6 原生 TS import,env 路径覆盖 DGAGENT_*)
|
||||
- **v2.0 全自动闭环** — `lib/autoloop.ts` `runAutoLoop`(observer→detector→generator 一次跑完:自动接受阈值 ≥4、幂等去重、代码 Gate 跳过、注册 trust=auto)+ `collectKbDrafts`(失败经验 → KB 草稿,存 dgagent-kb-drafts.json);`/dg-autoloop [--dry-run|--min|--json]` + `$DG_AUTOLOOP=1` 时 session_start 自动触发;信任阶梯 auto→observed(≥3 次)→trusted(≥5 次)
|
||||
- **v2.1 执行载体 + 命令日志** — `lib/runner.ts` `runRegisteredWorkflow`(把注册 workflow 真实执行:按序列校验、SDB Gate 高危拒绝/确认、不支持工具步骤显式失败;全部成功自动 `recordSafeRun` 推进信任阶梯——补上 v2.0 的死路)+ `dg_workflow_run` 工具(13 号,auto 需 force 确认);`logCommandIo` 把 7 个 slash 命令输出写入 session log(§3.4 不变量)
|
||||
- **v2.1.4 二轮加固** — 连接池并发安全(H1 busy 计数跳过在用连接淘汰 + H2 同板卡在途连接去重单一 TCP);exec SIGTERM→SIGKILL 硬截止(定时器全清理)+ 输出背压(超 `TRUNCATE_LIMITS` 停止缓冲并置 truncated 标志);`sftpPush` 远端 mkdirp 失败显式拒绝;build-verify 注入防护(`assertShellSafePath` 强化 unicode 同形字 + `assertSafePackageNames` 包名白名单);MCP client ACL fail-closed(条目缺 ACL 标记抛 `MCP_ERR_ACL_UNKNOWN`,工具层回落带标签 mock,绝不默认 public);路由关键词 ASCII 词边界(CJK 两侧视为边界);runner 逐命令 SDB 审计 + board_id 一致性前置守卫;`dg_ssh_exec` 失败路径 `action:"exec_error"` 会话日志 + `dg_workflow_run` 日志补 board_id;`--since` 共享解析点 `SinceParseError`(CLI 一行错误非零退出、MCP JSON-RPC 错误、命令可读错误);session_start 模式恢复容错(损坏条目跳过回落默认 free);SFTP 错误注入 + 连接池 + runner 审计 + 词边界 + 输出 Gate 降级 + compact 边界等 9 个新测试文件
|
||||
- **`/dg-setup` wizard** — validates env, writes `~/.pi/agent/settings.json`, registers provider at runtime
|
||||
- **E2E suite** — `tests/e2e/mocked/` chained multi-tool tests (in default run) + `tests/e2e/real/` real-hardware tests (env-gated, `npm run test:e2e`)
|
||||
- **lib/ extraction** — pure business logic in `lib/`, `extensions/utils.ts` is now a thin re-export layer + Pi-coupled helpers
|
||||
@ -275,6 +284,9 @@ If you are about to make a change that contradicts any of these, STOP and discus
|
||||
- **v2.0** ✅ DONE — 全自动闭环(`/dg-autoloop` observer→detector→generator + KB 草稿 + `$DG_AUTOLOOP` session_start 自动触发)
|
||||
- **v2.1** ✅ DONE — 执行载体 + 信任阶梯跑通(`dg_workflow_run` + `lib/runner.ts`)+ 命令日志落盘(`logCommandIo`)
|
||||
- **v2.1.1** ✅ DONE — review 调整:Detector 评分失败率不再加分(走 KB 草稿)+ `isFailedPreview` 词边界 + 死代码清除 + 参数解析收进 lib(命令/CLI 共用)+ 测试弱断言升级(smoke/auditLogId/telemetry)
|
||||
- **v2.1.2** ✅ DONE — 项目 review 修复批次一:MCP 服务回环绑定(H3)+ sftpPush 流式化(H2)+ 路由 provenance 修正(M2)+ engines/CLI 版本守卫(M5)+ Detector 语义标注(M4)
|
||||
- **v2.1.3** ✅ DONE — 项目 review 修复批次二:确认审计诚实化 `attestedBy`(H1)+ severe 正则写上下文匹配(M1)+ 执行后日志失败不再吞结果(M3)
|
||||
- **v2.1.4** ✅ DONE — 二轮加固批次:连接池并发安全(H1/H2)、exec 硬截止与背压、build-verify 注入防护、MCP ACL fail-closed、路由词边界、runner 逐命令审计、exec_error 审计动作、--since 校验、模式恢复容错
|
||||
|
||||
---
|
||||
|
||||
|
||||
47
README.md
47
README.md
@ -2,7 +2,7 @@
|
||||
|
||||
> 地瓜机器人(D-Robotics)RDK 平台的 AI 开发搭档——连接板卡、查询知识、调度专家模型,直达嵌入式开发全流程。
|
||||
|
||||
[]() []()
|
||||
[]() []()
|
||||
|
||||
---
|
||||
|
||||
@ -76,7 +76,7 @@ EOF
|
||||
│ · beginner/free 模式 │
|
||||
│ · /dg-setup, /dg-mode │
|
||||
│ │
|
||||
│ tools.ts (12 tools) │
|
||||
│ tools.ts (13 tools) │
|
||||
│ · dg_ssh_exec ──→ ssh2 ──→ 板卡 │
|
||||
│ · dg_file_sync ──→ SFTP ──→ 板卡 │
|
||||
│ · dg_board_list ──→ mDNS+cfg │
|
||||
@ -89,6 +89,7 @@ EOF
|
||||
│ · dg_kb_search ──→ MCP client │
|
||||
│ · dg_model_route (rules→KB→LLM)│
|
||||
│ · dg_dispatch_expert ──→ complete()│
|
||||
│ · dg_workflow_run (v2.1) │
|
||||
│ │
|
||||
│ provider.ts │
|
||||
│ · dg-robot + 4 专家模型 │
|
||||
@ -108,6 +109,9 @@ EOF
|
||||
│ · tros-env.ts (TROS setup) │
|
||||
│ · mcp-client.ts (Wikihub) │
|
||||
│ · render.ts + telemetry.ts │
|
||||
│ · compact/debug/observability │
|
||||
│ · detector/generator/registry │
|
||||
│ · autoloop/runner (v2.x) │
|
||||
│ · workflows/ (camera-check, │
|
||||
│ safe-exec, push-and-verify, │
|
||||
│ board-health, build-verify) │
|
||||
@ -126,7 +130,7 @@ EOF
|
||||
|
||||
```bash
|
||||
npm run typecheck # TypeScript strict(exit 0 才算通过)
|
||||
npm test # vitest(53 files / 405 tests,402 passed + 3 skipped real-hardware)
|
||||
npm test # vitest(400+ tests,journey→test 一一对应)
|
||||
npm run test:watch # watch 模式
|
||||
```
|
||||
|
||||
@ -134,11 +138,11 @@ npm run test:watch # watch 模式
|
||||
|
||||
```
|
||||
extensions/
|
||||
├── provider.ts # dg-robot provider + 4 models(9 行,委托 utils)
|
||||
├── tools.ts # 12 个 dg_* 工具注册(1449 行)
|
||||
├── system-prompt.ts # RDK 上下文 + mode + 4 commands(181 行)
|
||||
├── telemetry.ts # pi.on tool_execution span hooks(57 行)
|
||||
└── utils.ts # re-export from lib/ + Pi helpers(283 行)
|
||||
├── provider.ts # dg-robot provider + 4 models(委托 utils)
|
||||
├── tools.ts # 13 个 dg_* 工具注册
|
||||
├── system-prompt.ts # RDK 上下文 + mode + 7 runtime slash commands
|
||||
├── telemetry.ts # pi.on tool_execution span hooks
|
||||
└── utils.ts # re-export from lib/ + Pi helpers
|
||||
lib/ # pure business logic(无 Pi 依赖)
|
||||
├── env.ts # process.env helpers
|
||||
├── types.ts # Board / RiskAssessment / ExecResult / details interfaces
|
||||
@ -150,6 +154,15 @@ lib/ # pure business logic(无 Pi 依赖)
|
||||
├── mode-gate.ts # 模式 Gate #8:evaluateModeSwitch(信心 ≥ 0.8 才建议切换)
|
||||
├── tros-env.ts # TROS_SETUP_CANDIDATES + sourceTrosEnvCommand(workflow 共享)
|
||||
├── mcp-client.ts # McpHttpClient(Wikihub,JSON-RPC over HTTP)
|
||||
├── mcp-server.ts # v1.6 MCP 服务端:handleMcpJsonRpc/handleMcpHttpBody(JSON-RPC over HTTP)
|
||||
├── debug.ts # DG_DEBUG stderr 调试日志(isDebugEnabled/debugLog,关闭时零分配 no-op)
|
||||
├── compact.ts # v1.2 Context Engineering 输出压缩:maybeCompact 确定性启发式(头 + error/warning + 尾 + 摘要)
|
||||
├── observability.ts # v0.5 观测层:readSessionLogEntries + aggregateUsage(使用统计/失败率/趋势)
|
||||
├── detector.ts # v1.4 Detector:detectCandidates 序列模式挖掘 + 候选持久化
|
||||
├── generator.ts # v1.5 Generator:候选 → workflow 骨架 + 结构验证 + 落盘
|
||||
├── registry.ts # v1.5 信任分级:trusted/observed/auto 注册表 + 升级规则
|
||||
├── autoloop.ts # v2.0 全自动闭环:runAutoLoop(observer→detector→generator)+ KB 草稿
|
||||
├── runner.ts # v2.1 执行载体:runRegisteredWorkflow(安全执行自动推进信任阶梯)
|
||||
├── render.ts # render*() 纯展示函数(数据/呈现分离)
|
||||
├── telemetry.ts # initTelemetry / getTracer / JsonlSpanExporter(OTel)
|
||||
├── index.ts # barrel exports
|
||||
@ -159,7 +172,7 @@ lib/ # pure business logic(无 Pi 依赖)
|
||||
├── push-and-verify.ts # sftpPush → md5 校验完整性
|
||||
├── board-health.ts # uptime/负载/内存/磁盘/温度 只读体检
|
||||
└── build-verify.ts # colcon build 确定性判定(编译 Gate #6 + 代码 Gate #4)
|
||||
tests/ # 405 tests(53 files,3 skipped real-hardware),journey→test 一一对应
|
||||
tests/ # 400+ tests,journey→test 一一对应
|
||||
```
|
||||
|
||||
**深入阅读**:
|
||||
@ -185,6 +198,17 @@ tests/ # 405 tests(53 files,3 skipped real-hardware),jou
|
||||
| v0.6 | 专家调度 → `dg_dispatch_expert` 真实化(complete() 直调) | ✅ |
|
||||
| v1.0 | Full SDB Gates(6/9)+ OpenTelemetry tracing + E2E test suite(mock + 真实硬件) | ✅ |
|
||||
| v1.1 | SDB Gates **9/9**(代码 Gate + 编译 Gate + 模式 Gate)+ 工具 12 个 | ✅ |
|
||||
| v1.2 | 观测层(`/dg-usage` 使用频率统计)+ Context Engineering 输出压缩 + poka-yoke 加固 | ✅ |
|
||||
| v1.3 | 观测层进阶(`--board`/`--since` 过滤 + 失败率 + 趋势 + `--json` 导出) | ✅ |
|
||||
| v1.4 | Detector 脚本候选(`/dg-suggest` 序列模式挖掘 + 人工拍板 + 候选持久化) | ✅ |
|
||||
| v1.5 | Generator + 信任分级(`/dg-accept` 生成骨架 + `/dg-registry` trusted/observed/auto) | ✅ |
|
||||
| v1.6 | Layer 2 接入层(`bin/dg.mjs` CLI + MCP JSON-RPC over HTTP 服务端) | ✅ |
|
||||
| v2.0 | 全自动闭环(`/dg-autoloop` observer→detector→generator + KB 草稿 + `$DG_AUTOLOOP` 自动触发) | ✅ |
|
||||
| v2.1 | 执行载体 + 信任阶梯跑通(`dg_workflow_run` + `lib/runner.ts`)+ 命令日志(`logCommandIo`) | ✅ |
|
||||
| v2.1.1 | review 调整:Detector 评分失败率不再加分 + 参数解析收进 lib + 弱断言升级 | ✅ |
|
||||
| v2.1.2 | 项目 review 批次一:MCP 服务回环绑定 + sftpPush 流式化 + 路由 provenance 修正 + 版本守卫 | ✅ |
|
||||
| v2.1.3 | 项目 review 批次二:确认审计诚实化(`attestedBy`)+ severe 正则写上下文匹配 + 执行后日志容错 | ✅ |
|
||||
| v2.1.4 | 二轮加固批次:连接池并发安全、exec 硬截止与背压、build-verify 注入防护、MCP ACL fail-closed、路由词边界、runner 逐命令审计、exec_error 审计动作、--since 校验、模式恢复容错 | ✅ |
|
||||
|
||||
---
|
||||
|
||||
@ -198,6 +222,11 @@ tests/ # 405 tests(53 files,3 skipped real-hardware),jou
|
||||
| `DG_SSH_KEY` | SSH 私钥路径(默认 `~/.ssh/id_rsa`) | SSH 工具 |
|
||||
| `DG_MCP_URL` | MCP server URL | v0.4 |
|
||||
| `DG_MCP_TOKEN` | MCP bearer token | v0.4 |
|
||||
| `DG_DEBUG` | `1`/`true`/`yes`/`on` 时开启 stderr 调试日志(ssh/discovery/mcp/routing) | 调试 |
|
||||
| `DG_SSH_TIMEOUT_MS` | SSH 命令超时毫秒(默认 `60000`,范围 [1000, 600000]) | 否 |
|
||||
| `DG_OTEL_ENDPOINT` | OTLP exporter endpoint(设置时切换 OTLP/HTTP) | 否 |
|
||||
| `DG_OTEL_ENABLED` | `0` 时全局禁用 OpenTelemetry(no-op tracer) | 否 |
|
||||
| `DG_AUTOLOOP` | `1`/`true`/`yes`/`on` 时 session_start 自动触发 `/dg-autoloop` | 否 |
|
||||
|
||||
`/dg-setup` 自动配置并持久化到 `~/.pi/agent/settings.json`。
|
||||
|
||||
|
||||
30
SESSION.md
30
SESSION.md
@ -4,13 +4,35 @@
|
||||
|
||||
## 当前 Session
|
||||
|
||||
- **版本**:v2.1.1
|
||||
- **状态**:**SDB Gates 9/9** + 观测层(v1.2/v1.3)+ Detector(v1.4)+ Generator+信任分级(v1.5)+ Layer 2 CLI/MCP(v1.6)+ 全自动闭环(v2.0)+ **执行载体+命令日志(v2.1)** + 403 tests / 53 files + 工具 13 个
|
||||
- **下一步**:**v2.1+ 平台化**(多板卡批量编排、交付自动回滚、workflow 共享市场)——扩展路线图见 DESIGN.md §10;review 遗留讨论项(见下)
|
||||
- **2026-08-17 项目 review**:全库 review(typecheck/test 绿 + 架构/安全/正确性走查)产出 13 项待修复,分两批清完:批次一(v2.1.2,H2/H3/M2/M4/M5 + 4 low)、批次二(v2.1.3,H1/M1/M3 全部遗留项)
|
||||
- **版本**:v2.1.4
|
||||
- **状态**:**SDB Gates 9/9** + 观测层(v1.2/v1.3)+ Detector(v1.4)+ Generator+信任分级(v1.5)+ Layer 2 CLI/MCP(v1.6)+ 全自动闭环(v2.0)+ 执行载体+命令日志(v2.1)+ **二轮加固批次(v2.1.4)** + 546 tests / 63 files + 工具 13 个
|
||||
- **下一步**:**v2.1+ 平台化**(多板卡批量编排、交付自动回滚、workflow 共享市场)——扩展路线图见 DESIGN.md §10
|
||||
- **2026-08-17 项目 review**:全库 review(typecheck/test 绿 + 架构/安全/正确性走查)产出 13 项待修复,分两批清完:批次一(v2.1.2,H2/H3/M2/M4/M5 + 4 low)、批次二(v2.1.3,H1/M1/M3 全部遗留项)、批次三(v2.1.4,二轮深挖 11 项 + 测试盲区 + 文档漂移)
|
||||
|
||||
## 已完成
|
||||
|
||||
### v2.1.4 — 二轮加固批次(2026-08-17)
|
||||
|
||||
- ✅ **H1 连接池在用连接被淘汰**:`BoardConnectionPool` 引入 `busyCount` 引用计数(acquire 递增 / release 递减);空闲淘汰定时器只在 `busyCount === 0` 时调度,`evict()` 遇 busy 条目跳过并重调度——长命令(>60s)执行中不再被 idle sweep 杀连接
|
||||
- ✅ **H2 并发冷启动重复 TCP**:同板卡在途连接去重——`acquire()` 命中 `existing.connecting` 时 await 同一 connect Promise(并发 N 个调用者共享单一 TCP 连接);连接失败双路径清理(发起者 rethrow / 等待者回落冷启动)
|
||||
- ✅ **M3 exec 硬截止**:超时 SIGTERM 后追加 SIGKILL 宽限期(`KILL_GRACE_MS`);channel 正常关闭时**全量清理定时器**(无泄漏 Timer 拖住事件循环)
|
||||
- ✅ **M4 输出背压**:exec stdout/stderr 累积超 `TRUNCATE_LIMITS.maxBytes`(50KB)停止缓冲后续 chunk 并置 `stdout_truncated`/`stderr_truncated` 标志(`SshExecResult extends ExecResult`,结构化类型向后兼容)
|
||||
- ✅ **L1 mkdirp 失败显式拒绝**:`sftpPush` 远端递归 mkdir 错误不再被吞(原来失败会延后成迷惑的 write 错误)
|
||||
- ✅ **M2 build-verify 注入防护**:`colconBuildCommand` 的 `workspaceDir` 走 `assertShellSafePath`(强化:新增单引号/glob/unicode 同形字全角字符拒绝)、`packagesSelect` 走 `assertSafePackageNames` 白名单(`[A-Za-z0-9_-]` 空格分隔,`; rm -rf /`/反引号/`$()`/引号/换行/glob/同形字全拒);`remoteVerifyCommand` 导出函数自防护(不依赖调用方先校验)
|
||||
- ✅ **M6 MCP ACL fail-closed**:`searchKnowledgeBase` 遇 ACL 标记缺失/无法解析抛 `McpError(MCP_ERR_ACL_UNKNOWN)`(与"服务器明确无匹配"的空文本占位区分);工具层 catch 回落**带标签 mock**(绝不默认 public、不泄露原文);整响应畸形 → `MCP_ERR_MALFORMED` typed error
|
||||
- ✅ **L2 路由词边界**:KB 关键词匹配加 ASCII `\b`(先 `escapeRegExp` 转义再嵌入)——"cat" 不再误命中 "category";CJK 不套边界(中文字符在 JS 默认正则非词字符,两侧 ASCII 视为边界)
|
||||
- ✅ **M5 runner 逐命令审计**:`runRegisteredWorkflow` 每个执行步骤产出 SDB 审计条目(复用 `assessRisk` + `writeAuditLog`,与 dg_ssh_exec 同形状);审计写失败 try/catch 表面化(不吞结果)
|
||||
- ✅ **board_id 一致性守卫**:workflow 步骤 board_id 与 workflow 自身 board_id 不一致 → 任何步骤执行**前**显式失败(fail-closed,点名违规步骤)
|
||||
- ✅ **M1 exec_error 审计动作**:`dg_ssh_exec` 失败路径(SSH 错误类/超时/channel 错误)落 `action:"exec_error"` 会话日志(best-effort,rethrow 原异常);`lib/types.ts` 两处 action 联合类型加宽
|
||||
- ✅ **L3 workflow_run 日志补 board_id**:`dg_workflow_run` 的 `logToolCall` 条目补齐 board_id(`/dg-usage --board` 过滤恢复完整)
|
||||
- ✅ **--since 共享校验**:`parseSince` 非法值抛 `SinceParseError`(带合法格式提示)——CLI 一行错误 + 非零退出(不打堆栈)、MCP JSON-RPC 错误、命令可读错误三端一致
|
||||
- ✅ **模式恢复容错**:`session_start` 恢复 mode 时损坏条目(非对象/data 形状异常/mode 非法)跳过 + debugLog,回落默认 free(不再崩溃或恢复出垃圾模式)
|
||||
- ✅ **mock-ssh 契约面扩展**(Wave 0 前置):`MockWriteStream.write` 返回 boolean、`MockReadStream` 补 `pause/resume`、`pullError`/`pushError` SFTP 错误注入(错误流不产数据,忠实 ssh2 行为)、`clients[]` 注册 + `triggerMockClientEvent("close"|"error")`、`setSshConnectBlocked`/`releaseSshConnect` 连接阻塞
|
||||
- ✅ **文档漂移修复**:README/TECHNICAL_ARCHITECTURE/AGENTS——lib 树补齐 v1.2+ 模块、工具数 12→13、slash 命令清单(static 2 + runtime 7)、roadmap 补 v2.1.1–v2.1.4、env 表补全
|
||||
- ✅ **测试盲区补齐**:9 个新测试文件——`ssh-pool`(H1/H2/M3/M4/L1)、`build-verify-security`(注入矩阵)、`mcp-client`(ACL fail-closed)、`routing-boundary`(词边界 CJK 安全)、`output-gate`(降级路径 fail-closed)、`runner-audit`(逐命令审计 + board_id 守卫)、`sftp-failures`(SFTP 错误注入)、`compact`(边界:空/单行/超限/全 error 行/CJK/2000 字符截断)、`cli-since`(CLI 校验 e2e)+ journey 25(mode 恢复容错)/26(since 校验);**546 passed / 3 skipped(63 files,+130 tests)**
|
||||
- ✅ 执行方式:Plan Agent 验证方案 → W0 基线(mock 契约面先行)→ 8 agent 并行(文件所有权互斥)→ 集成(typecheck + 全量 test + gates.test 旧 fail-open 断言更新为新 M6 语义)
|
||||
- ✅ 验证:`npm run typecheck` exit 0;`npm test` 546 passed / 3 skipped(63 files)
|
||||
|
||||
### v2.1.3 — 项目 review 修复批次二(2026-08-17)
|
||||
|
||||
- ✅ **H1 确认审计诚实化**(安全/审计可信):`logUserConfirmation` 新增 `attestedBy: "user" | "agent"`——force=true 路径是**模型自证**不是用户响应,审计 `responded_by` 如实区分;`dg_ssh_exec` / `dg_workflow_safe_exec` 两条 force 路径的审计条目 `user_confirmed` 改为 `false`(action 仍为 `executed_with_force`,动作与确认来源分离);TDD——journey 05 新增 Red 测试断言 force 审计不含用户确认
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
> DESIGN.md 答"做什么";METHODOLOGY.md 答"为什么";本文档答"怎么实现"。
|
||||
>
|
||||
> **读者**:v0.3+ 实现工程师;v0.2 mock reviewer;扩展点评估架构师。
|
||||
> **状态**:v1.0,与代码同步演进。接口变更必须先更新本文档。
|
||||
> **状态**:v2.1.3,与代码同步演进。接口变更必须先更新本文档。
|
||||
|
||||
---
|
||||
|
||||
@ -37,8 +37,8 @@
|
||||
▼ ▼ ▼
|
||||
┌──────────────┐ ┌────────────────┐ ┌──────────────────────┐
|
||||
│ provider.ts │ │ tools.ts │ │ system-prompt.ts │
|
||||
│ dg-robot + │ │ 8 dg_* tools │ │ before_agent_start │
|
||||
│ 4 models │ │ all REAL: SSH/ │ │ 4 slash commands │
|
||||
│ dg-robot + │ │ 13 dg_* tools │ │ before_agent_start │
|
||||
│ 4 models │ │ all REAL: SSH/ │ │ 7 runtime commands │
|
||||
│ stateless │ │ SFTP/discovery │ │ holds currentMode │
|
||||
│ │ │ kb/route/ │ │ session_start restore │
|
||||
│ │ │ dispatch │ │ │
|
||||
@ -77,8 +77,8 @@
|
||||
| 模块 | 入口 | 状态 | 职责 |
|
||||
|---|---|---|---|
|
||||
| `provider.ts` | `pi.extensions[0]` | 无 | `$DG_API_KEY` 存在时注册 `dg-robot` + 4 个专家模型;无 key 则 silent return。v0.3 精简为调 `registerDgRobotProvider(pi)`。 |
|
||||
| `tools.ts` | `pi.extensions[1]` | 无 | 注册 12 个 `dg_*` 工具。SSH/SFTP/discovery 真实(v0.3);kb(MCP client)/route/dispatch 真实(v0.4-v0.6);workflow 5 个(camera-check / safe-exec / push-and-verify / board-health / build-verify)+ mode evaluate(v1.1)。每个 `execute` 经 SDB Gate + `withToolSpan` + `render*` 输出 + `logToolIo`,返回 `AgentToolResult`。 |
|
||||
| `system-prompt.ts` | `pi.extensions[2]` | 有(mode) | `before_agent_start` 注入 RDK 上下文;`session_start` 恢复 mode;4 个 slash command(`/dg-help` `/dg-status` `/dg-mode` `/dg-setup`);持有 `currentMode`。`/dg-setup` 可 runtime 注册 provider。 |
|
||||
| `tools.ts` | `pi.extensions[1]` | 无 | 注册 13 个 `dg_*` 工具。SSH/SFTP/discovery 真实(v0.3);kb(MCP client)/route/dispatch 真实(v0.4-v0.6);workflow 5 个(camera-check / safe-exec / push-and-verify / board-health / build-verify)+ mode evaluate(v1.1)+ workflow run(v2.1,`dg_workflow_run`)。每个 `execute` 经 SDB Gate + `withToolSpan` + `render*` 输出 + `logToolIo`,返回 `AgentToolResult`。 |
|
||||
| `system-prompt.ts` | `pi.extensions[2]` | 有(mode) | `before_agent_start` 注入 RDK 上下文;`session_start` 恢复 mode;7 个 runtime slash command(`/dg-mode` `/dg-setup` `/dg-usage` `/dg-suggest` `/dg-accept` `/dg-registry` `/dg-autoloop`,经 `registerCommand`;`/dg-help` `/dg-status` 为 `prompts/*.md` 静态命令);持有 `currentMode`。`/dg-setup` 可 runtime 注册 provider。 |
|
||||
| `utils.ts` | 非入口 | 无 | lib/ 全部符号重导出 + Pi 耦合 helpers:SSH 连接池 + exec + SFTP + 板卡发现 + config IO + provider 注册 helper + settings 写入 + 审计日志 + 常量(`TRUNCATE_LIMITS`)。路由/调度/MCP/render/telemetry/schemas 实现见 `lib/`。 |
|
||||
|
||||
### 1.3 关键不变量
|
||||
@ -367,7 +367,7 @@ pi.registerCommand("dg-mode", {
|
||||
|
||||
**路径**:`~/.pi/agent/dgagent-audit.log`(NDJSON,append-only,每行一条 JSON)
|
||||
|
||||
**触发点**:全部 12 个工具经 `logToolIo` 写入统一 session log(`appendSessionLog`,JSONL,含 trace_id/span_id);高危 SSH 命令同时走 `writeAuditLog` 双写(backward-compat)
|
||||
**触发点**:全部 13 个工具经 `logToolIo` 写入统一 session log(`appendSessionLog`,JSONL,含 trace_id/span_id);高危 SSH 命令同时走 `writeAuditLog` 双写(backward-compat)
|
||||
|
||||
**写入时机**:
|
||||
1. fatal 命令被拒绝 → `action: "rejected"`
|
||||
@ -392,7 +392,7 @@ pi.registerCommand("dg-mode", {
|
||||
|
||||
**Trace 上下文**:`lib/telemetry.ts` 提供 `initTelemetry()`(NodeTracerProvider + SimpleSpanProcessor + `JsonlSpanExporter` 默认写 `~/.pi/agent/dgagent-traces.jsonl`;`DG_OTEL_ENDPOINT` 存在时切 OTLP/HTTP exporter;`DG_OTEL_ENABLED=0` 时 no-op)。`getTracer()` 惰性初始化,guarded(宿主已注册 provider 则不覆盖)。`appendSessionLog` 将活动 span 的 `trace_id`/`span_id` 写入 `SessionLogEntry`,与 session log 关联。
|
||||
|
||||
**v1.0 实现范围**:12 工具全部 `withToolSpan` + LLM 调用 GenAI 属性 + `pi.on` 事件层(fail-open——telemetry 失败绝不中断工具执行)。
|
||||
**v1.0 实现范围**:13 工具全部 `withToolSpan` + LLM 调用 GenAI 属性 + `pi.on` 事件层(fail-open——telemetry 失败绝不中断工具执行)。
|
||||
|
||||
### 6.3 调试日志(v1.0.3 ✅ 已实现)
|
||||
|
||||
@ -527,15 +527,19 @@ v0.6 评估:**漏洞 5(编排契约)**。
|
||||
|
||||
## 附录 A:环境变量速查
|
||||
|
||||
| Env | Required by | Default | v0.2 状态 |
|
||||
| Env | Required by | Default | 备注 |
|
||||
|---|---|---|---|
|
||||
| `DG_API_KEY` | provider 注册 | 无 | 必填才能注册 provider |
|
||||
| `DG_API_BASE_URL` | provider 注册 | `https://api.d-robotics.com/v1` | 可选 |
|
||||
| `DG_API_PROTOCOL` | provider 注册 | `openai-completions` | 可选,支持 `anthropic-messages` |
|
||||
| `DG_MCP_URL` | `dg_kb_search`(v0.4) | 无 | v0.2 mock 不需要 |
|
||||
| `DG_MCP_TOKEN` | `dg_kb_search`(v0.4) | 无 | v0.2 mock 不需要 |
|
||||
| `DG_SSH_KEY` | `dg_ssh_exec`(v0.3) | `~/.ssh/id_rsa` | v0.2 mock 不需要 |
|
||||
| `DG_MCP_URL` | `dg_kb_search`(v0.4) | 无 | 可选 |
|
||||
| `DG_MCP_TOKEN` | `dg_kb_search`(v0.4) | 无 | 可选 |
|
||||
| `DG_SSH_KEY` | `dg_ssh_exec`(v0.3) | `~/.ssh/id_rsa` | 可选 |
|
||||
| `DG_DEBUG` | stderr 调试日志(ssh/discovery/mcp/routing,v1.0.3) | 未设置 | 可选 |
|
||||
| `DG_SSH_TIMEOUT_MS` | `dg_ssh_exec`(v1.2) | `60000`(范围 [1000, 600000]) | 可选 |
|
||||
| `DG_OTEL_ENDPOINT` | OTel OTLP exporter(v1.0) | 无(默认 JsonlSpanExporter) | 可选 |
|
||||
| `DG_OTEL_ENABLED` | 全局开关(v1.0) | 未设置(`0` 时 no-op tracer) | 可选 |
|
||||
| `DG_AUTOLOOP` | `/dg-autoloop` session_start 自动触发(v2.0) | 未设置 | 可选 |
|
||||
|
||||
## 附录 B:版本与代码对应
|
||||
|
||||
@ -547,4 +551,15 @@ v0.6 评估:**漏洞 5(编排契约)**。
|
||||
| v0.5 | 模型路由规则表 + KB + LLM fallback | utils.ts(rules engine),tools.ts(dg_model_route) |
|
||||
| v0.6 | 子 Agent 调度,Orchestrator-Worker 完整实现 | 新增 orchestrator 模块,tools.ts(dg_dispatch_expert) |
|
||||
| v1.0 | Full SDB Gates(6/9 实现:路由白名单/输出 schema/鉴权 ACL/参数 board_id/确认 Gate/高危),OpenTelemetry tracing(lib/telemetry.ts + extensions/telemetry.ts),E2E 测试套件(mocked 链式 + real 硬件脚手架) | lib/schemas.ts, lib/telemetry.ts, extensions/telemetry.ts, lib/mcp-client.ts, lib/routing.ts, tools.ts, tests/e2e/ |
|
||||
| v1.1(当前) | SDB Gates **9/9 全实现**:代码 Gate + 编译 Gate(`lib/workflows/build-verify.ts` `parseBuildResult` 确定性解析 + `dg_workflow_build_verify`)+ 模式 Gate(`lib/mode-gate.ts` `evaluateModeSwitch` 信心 ≥ 0.8 + `dg_mode_evaluate`)。共享 `lib/tros-env.ts`(TROS setup 候选路径)。工具 12 个。 | lib/workflows/build-verify.ts, lib/mode-gate.ts, lib/tros-env.ts, tools.ts |
|
||||
| v1.1 | SDB Gates **9/9 全实现**:代码 Gate + 编译 Gate(`lib/workflows/build-verify.ts` `parseBuildResult` 确定性解析 + `dg_workflow_build_verify`)+ 模式 Gate(`lib/mode-gate.ts` `evaluateModeSwitch` 信心 ≥ 0.8 + `dg_mode_evaluate`)。共享 `lib/tros-env.ts`(TROS setup 候选路径)。工具 12 个。 | lib/workflows/build-verify.ts, lib/mode-gate.ts, lib/tros-env.ts, tools.ts |
|
||||
| v1.2 | 观测层(`/dg-usage` 使用频率统计)+ Context Engineering 输出压缩(`lib/compact.ts`)+ poka-yoke 加固(`assertShellSafePath`) | lib/compact.ts, lib/debug.ts, tools.ts, extensions/system-prompt.ts |
|
||||
| v1.3 | 观测层进阶(`--board`/`--since` 过滤 + 失败率 + 趋势 + `--json`) | lib/observability.ts, extensions/system-prompt.ts |
|
||||
| v1.4 | Detector 脚本候选(`/dg-suggest` 序列模式挖掘 + 人工拍板 + 候选持久化) | lib/detector.ts, extensions/system-prompt.ts |
|
||||
| v1.5 | Generator + 信任分级(`/dg-accept` 生成骨架 + `/dg-registry` trusted/observed/auto) | lib/generator.ts, lib/registry.ts, extensions/system-prompt.ts |
|
||||
| v1.6 | Layer 2 接入层(`bin/dg.mjs` CLI + MCP JSON-RPC over HTTP 服务端,`dg serve-mcp` 回环绑定) | lib/mcp-server.ts, bin/dg.mjs |
|
||||
| v2.0 | 全自动闭环(`/dg-autoloop` observer→detector→generator + KB 草稿 + `$DG_AUTOLOOP` session_start 自动触发) | lib/autoloop.ts, extensions/system-prompt.ts |
|
||||
| v2.1 | 执行载体 + 命令日志(`dg_workflow_run` 工具 + `lib/runner.ts` `runRegisteredWorkflow` + `logCommandIo`) | lib/runner.ts, tools.ts |
|
||||
| v2.1.1 | review 调整(Detector 评分失败率不再加分 + `isFailedPreview` 词边界 + 死代码清除 + 参数解析收进 lib + 弱断言升级) | lib/detector.ts, lib/observability.ts, extensions/system-prompt.ts |
|
||||
| v2.1.2 | 项目 review 批次一:MCP 服务回环绑定(H3)+ sftpPush 流式化(H2)+ 路由 provenance 修正(M2)+ engines/CLI 版本守卫(M5)+ Detector 语义标注(M4) | lib/mcp-server.ts, bin/dg.mjs, lib/ssh.ts, lib/routing.ts, lib/schemas.ts, package.json |
|
||||
| v2.1.3 | 项目 review 批次二:确认审计诚实化 `attestedBy`(H1)+ severe 正则写上下文匹配(M1)+ 执行后日志失败不吞结果(M3) | lib/risk.ts, extensions/tools.ts, lib/workflows/safe-exec.ts |
|
||||
| v2.1.4(进行中) | 二轮加固批次:连接池并发安全(H1/H2)、exec 硬截止与背压、build-verify 注入防护、MCP ACL fail-closed、路由词边界、runner 逐命令审计、exec_error 审计动作、--since 校验、模式恢复容错 | lib/ssh.ts, lib/runner.ts, lib/workflows/*, lib/mcp-client.ts, lib/mcp-server.ts, lib/routing.ts, lib/observability.ts, extensions/tools.ts |
|
||||
|
||||
14
bin/dg.mjs
14
bin/dg.mjs
@ -53,10 +53,16 @@ function parsePort(args) {
|
||||
}
|
||||
|
||||
async function cmdUsage(args) {
|
||||
const opts = lib.parseUsageArgs(args.join(" "));
|
||||
const { entries } = lib.readSessionLogEntries(readSessionLog());
|
||||
const stats = lib.aggregateUsage(entries, { boardId: opts.filter.boardId, since: opts.filter.since });
|
||||
process.stdout.write(opts.json ? JSON.stringify(stats, null, 2) + "\n" : lib.renderUsageReport(stats).content + "\n");
|
||||
try {
|
||||
const opts = lib.parseUsageArgs(args.join(" "));
|
||||
const { entries } = lib.readSessionLogEntries(readSessionLog());
|
||||
const stats = lib.aggregateUsage(entries, { boardId: opts.filter.boardId, since: opts.filter.since });
|
||||
process.stdout.write(opts.json ? JSON.stringify(stats, null, 2) + "\n" : lib.renderUsageReport(stats).content + "\n");
|
||||
} catch (e) {
|
||||
// v2.1.4:非法 --since 等参数错误 —— 一行错误 + 非零退出,不打印堆栈
|
||||
process.stderr.write(`dg usage: ${e.message}\n`);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
}
|
||||
|
||||
async function cmdSuggest(args) {
|
||||
|
||||
@ -9,11 +9,12 @@ import {
|
||||
parseUsageArgs,
|
||||
parseSuggestArgs,
|
||||
parseAutoLoopArgs,
|
||||
debugLog,
|
||||
type DgMode,
|
||||
type DgEnvConfig,
|
||||
} from "./utils.ts";
|
||||
import { getSessionLogPath, logCommandIo } from "../lib/risk.ts";
|
||||
import { readSessionLogEntries, aggregateUsage } from "../lib/observability.ts";
|
||||
import { readSessionLogEntries, aggregateUsage, SinceParseError } from "../lib/observability.ts";
|
||||
import { detectCandidates, saveCandidates, getCandidatesPath, loadCandidates } from "../lib/detector.ts";
|
||||
import {
|
||||
generateWorkflowSkeleton,
|
||||
@ -127,21 +128,48 @@ export function getCurrentMode(): DgMode {
|
||||
return currentMode;
|
||||
}
|
||||
|
||||
/**
|
||||
* session_start 恢复持久化模式(appendEntry 持久化,倒序取最新合法条目)。
|
||||
*
|
||||
* v2.1.4 加固(fail-closed):session 文件可能被外部编辑/部分写入——
|
||||
* - 非对象 / null 条目跳过(debugLog 记录),不崩溃
|
||||
* - 命中 dg-mode 但 data 形状异常或 mode 值非法:跳过继续往前找
|
||||
* - 全部无效 → 回落默认 free(绝不留存上次会话的陈旧/垃圾模式)
|
||||
* - getEntries() 本身抛异常 → 回落默认 free
|
||||
*/
|
||||
function restoreModeFromEntries(ctx: ExtensionContext): void {
|
||||
const fallbackFree = (reason: string): void => {
|
||||
currentMode = "free";
|
||||
debugLog("mode", reason);
|
||||
};
|
||||
try {
|
||||
const entries = ctx.sessionManager.getEntries();
|
||||
for (let i = entries.length - 1; i >= 0; i--) {
|
||||
const entry = entries[i]!;
|
||||
if (entry.type === "custom" && (entry as { customType?: string }).customType === MODE_CUSTOM_TYPE) {
|
||||
const data = (entry as { data?: { mode?: string } }).data;
|
||||
if (data?.mode === "beginner" || data?.mode === "free") {
|
||||
currentMode = data.mode;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (!Array.isArray(entries)) {
|
||||
fallbackFree("session entries 不是数组,回落默认 free");
|
||||
return;
|
||||
}
|
||||
for (let i = entries.length - 1; i >= 0; i--) {
|
||||
const entry = entries[i];
|
||||
if (typeof entry !== "object" || entry === null) {
|
||||
debugLog("mode", `跳过损坏条目(非对象)@${i}`);
|
||||
continue;
|
||||
}
|
||||
const { type, customType, data } = entry as { type?: unknown; customType?: unknown; data?: unknown };
|
||||
if (type !== "custom" || customType !== MODE_CUSTOM_TYPE) continue;
|
||||
if (typeof data !== "object" || data === null || Array.isArray(data)) {
|
||||
debugLog("mode", `模式条目 data 形状异常(${typeof data}),跳过`);
|
||||
continue;
|
||||
}
|
||||
const mode = (data as { mode?: unknown }).mode;
|
||||
if (mode === "beginner" || mode === "free") {
|
||||
currentMode = mode;
|
||||
return;
|
||||
}
|
||||
debugLog("mode", `模式条目 data.mode 非法(${String(mode)}),跳过`);
|
||||
}
|
||||
fallbackFree("无合法模式条目,回落默认 free");
|
||||
} catch (err) {
|
||||
console.warn("[Mode] 恢复模式失败,使用默认 free", (err as Error).message);
|
||||
fallbackFree(`恢复模式异常,回落默认 free:${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
@ -283,7 +311,11 @@ function registerDgUsageCommand(pi: ExtensionAPI): void {
|
||||
: "";
|
||||
notifyCommand(ctx, "dg-usage", args, content + suffix, "info");
|
||||
} catch (err) {
|
||||
notifyCommand(ctx, "dg-usage", args, `读取使用统计失败:${(err as Error).message}`, "error");
|
||||
// v2.1.4:参数错误(非法 --since)给出原文,不套“读取使用统计失败”误导前缀
|
||||
const message = err instanceof SinceParseError
|
||||
? err.message
|
||||
: `读取使用统计失败:${(err as Error).message}`;
|
||||
notifyCommand(ctx, "dg-usage", args, message, "error");
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
@ -819,13 +819,15 @@ function registerSshExec(pi: ExtensionAPI): void {
|
||||
const loggingWarnings: string[] = [];
|
||||
let auditLogId: string | null = null;
|
||||
try {
|
||||
// M1:执行失败路径必须落 action=exec_error(会话日志 dual-write),
|
||||
// 而不是误标 executed——/dg-usage 失败率统计靠它区分成败。
|
||||
auditLogId = writeAuditLog({
|
||||
session_id: sessionId,
|
||||
board_id: params.board_id,
|
||||
command: params.command,
|
||||
risk_level: risk.level,
|
||||
risk_pattern: risk.pattern,
|
||||
action: "executed",
|
||||
action: "exec_error",
|
||||
exit_code: null,
|
||||
duration_ms: null,
|
||||
user_confirmed: false,
|
||||
@ -848,7 +850,7 @@ function registerSshExec(pi: ExtensionAPI): void {
|
||||
};
|
||||
fail(errorMsg);
|
||||
try {
|
||||
logToolCall({ sessionId, agentInvocationId: _id, toolName: "dg_ssh_exec", params: { board_id: params.board_id, command: params.command, force: params.force }, content, durationMs: Date.now() - startedAt });
|
||||
logToolCall({ sessionId, agentInvocationId: _id, toolName: "dg_ssh_exec", params: { board_id: params.board_id, command: params.command, force: params.force, error: errorMsg, error_type: errorType }, content, durationMs: Date.now() - startedAt });
|
||||
} catch (logErr) {
|
||||
loggingWarnings.push(`会话日志写入失败: ${errText(logErr)}`);
|
||||
}
|
||||
@ -1581,10 +1583,17 @@ function registerWorkflowRun(pi: ExtensionAPI): void {
|
||||
const startedAt = Date.now();
|
||||
const sessionId = ctx.sessionManager.getSessionId();
|
||||
|
||||
// L3:workflow_run 日志条目必须带 board_id,否则 /dg-usage --board 过滤不到。
|
||||
// 优先取工具参数默认板卡;缺省时从步骤 args 里找第一块板卡。
|
||||
const stepBoardId = params.steps
|
||||
.map((s) => (s.args as Record<string, unknown>).board_id)
|
||||
.find((b): b is string => typeof b === "string" && b.length > 0);
|
||||
const logBoardId = params.board_id ?? stepBoardId ?? null;
|
||||
|
||||
const record = findWorkflow(params.name);
|
||||
if (!record) {
|
||||
const content = `❌ 注册表无 workflow:\`${params.name}\`。先运行 /dg-accept 或 /dg-registry。`;
|
||||
logToolCall({ sessionId, agentInvocationId: _id, toolName: "dg_workflow_run", params: { name: params.name }, content, durationMs: Date.now() - startedAt });
|
||||
logToolCall({ sessionId, agentInvocationId: _id, toolName: "dg_workflow_run", params: { name: params.name, board_id: logBoardId }, content, durationMs: Date.now() - startedAt });
|
||||
return textResult(content, freezeResult({ name: params.name, executed: false, reason: "not_registered" }));
|
||||
}
|
||||
if (record.trust === "auto" && !params.force) {
|
||||
@ -1596,7 +1605,7 @@ function registerWorkflowRun(pi: ExtensionAPI): void {
|
||||
"按 DESIGN.md §9.5,auto 等级调用前需确认关键步骤。",
|
||||
"确认执行请重新调用 dg_workflow_run 并设置 force=true。",
|
||||
].join("\n");
|
||||
logToolCall({ sessionId, agentInvocationId: _id, toolName: "dg_workflow_run", params: { name: params.name }, content, durationMs: Date.now() - startedAt });
|
||||
logToolCall({ sessionId, agentInvocationId: _id, toolName: "dg_workflow_run", params: { name: params.name, board_id: logBoardId }, content, durationMs: Date.now() - startedAt });
|
||||
return textResult(content, freezeResult({ name: params.name, executed: false, trust: record.trust, reason: "confirmation_required" }));
|
||||
}
|
||||
|
||||
@ -1607,7 +1616,7 @@ function registerWorkflowRun(pi: ExtensionAPI): void {
|
||||
);
|
||||
const { content } = renderWorkflowRun(result);
|
||||
const truncated = truncateHead(content, TRUNCATE_LIMITS);
|
||||
logToolCall({ sessionId, agentInvocationId: _id, toolName: "dg_workflow_run", params: { name: params.name, steps: params.steps, force: params.force }, content, durationMs: Date.now() - startedAt });
|
||||
logToolCall({ sessionId, agentInvocationId: _id, toolName: "dg_workflow_run", params: { name: params.name, steps: params.steps, force: params.force, board_id: logBoardId }, content, durationMs: Date.now() - startedAt });
|
||||
if (!result.ok) fail(result.errors.join(";") || "workflow run failed");
|
||||
return textResult(
|
||||
truncated.content,
|
||||
|
||||
@ -5,6 +5,13 @@
|
||||
* Implements JSON-RPC 2.0 over HTTP POST transport.
|
||||
*
|
||||
* Per §1.6 Fail-Closed: connection/config errors are thrown, never silently degraded.
|
||||
*
|
||||
* v2.2 (M6) ACL fail-closed 加固:
|
||||
* - 条目 ACL 缺失/无法解析 → 抛 McpError(MCP_ERR_ACL_UNKNOWN),绝不按 public 放行
|
||||
* - 畸形响应(非对象 JSON / content 非数组 / text 块缺 text / 缺 result/error)
|
||||
* → 类型化 McpError,绝不退化成"空结果"式假成功
|
||||
* - isError=true 的工具错误响应绝不当作正常文章返回
|
||||
* - 空内容 = 服务器明确"无匹配",与"ACL 无法确定"(抛错)明确区分
|
||||
*/
|
||||
|
||||
import { getOptionalEnv } from "./env.ts";
|
||||
@ -36,6 +43,12 @@ export type KbAclLevel = "public" | "partner" | "internal";
|
||||
|
||||
const KB_ACL_LEVELS: readonly KbAclLevel[] = ["public", "partner", "internal"];
|
||||
|
||||
/** 本地校验错误:响应结构不符合 JSON-RPC / MCP 契约。 */
|
||||
export const MCP_ERR_MALFORMED = -1;
|
||||
|
||||
/** ACL 无法确定:知识条目缺少可解析的 ACL 标记,fail-closed 拒绝返回。 */
|
||||
export const MCP_ERR_ACL_UNKNOWN = -32002;
|
||||
|
||||
function isAclLevel(x: unknown): x is KbAclLevel {
|
||||
return typeof x === "string" && (KB_ACL_LEVELS as readonly string[]).includes(x);
|
||||
}
|
||||
@ -86,7 +99,7 @@ export class McpHttpClient {
|
||||
capabilities: {},
|
||||
clientInfo: { name: "dgagent", version: "0.4.0" },
|
||||
});
|
||||
if (!result || typeof result !== "object") {
|
||||
if (!result || typeof result !== "object" || Array.isArray(result)) {
|
||||
throw new McpConnectionError("MCP 初始化失败:服务器返回了无效响应");
|
||||
}
|
||||
this.initialized = true;
|
||||
@ -94,34 +107,74 @@ export class McpHttpClient {
|
||||
|
||||
/**
|
||||
* List available tools on the server.
|
||||
* Fail-closed:畸形响应(缺少 tools 数组)抛 McpError,绝不返回"空列表"假成功。
|
||||
*/
|
||||
async listTools(): Promise<McpToolDefinition[]> {
|
||||
await this.ensureInitialized();
|
||||
const result = await this.request("tools/list");
|
||||
if (result && typeof result === "object" && "tools" in result) {
|
||||
return (result as { tools: McpToolDefinition[] }).tools;
|
||||
if (result && typeof result === "object" && !Array.isArray(result) && "tools" in result) {
|
||||
const tools = (result as Record<string, unknown>).tools;
|
||||
if (!Array.isArray(tools)) {
|
||||
throw new McpError("tools/list 返回的 tools 不是数组", MCP_ERR_MALFORMED);
|
||||
}
|
||||
return tools as McpToolDefinition[];
|
||||
}
|
||||
return [];
|
||||
throw new McpError("tools/list 返回了无效响应(缺少 tools 字段)", MCP_ERR_MALFORMED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call a tool on the server.
|
||||
*
|
||||
* Fail-closed 校验(M6):
|
||||
* - content 必须是数组,否则抛 McpError(原实现会 TypeError 崩溃或静默退化)
|
||||
* - 每个块必须是对象且带字符串 type
|
||||
* - type="text" 的块必须带字符串 text(缺失即畸形)
|
||||
* - isError=true 表示工具执行失败,错误内容绝不当作正常结果返回
|
||||
*/
|
||||
async callTool(name: string, args: Record<string, unknown>): Promise<McpToolCallResult> {
|
||||
await this.ensureInitialized();
|
||||
const result = await this.request("tools/call", { name, arguments: args });
|
||||
if (result && typeof result === "object" && "content" in result) {
|
||||
return result as McpToolCallResult;
|
||||
if (!result || typeof result !== "object" || Array.isArray(result) || !("content" in result)) {
|
||||
throw new McpError(`工具 ${name} 返回了无效响应`, MCP_ERR_MALFORMED);
|
||||
}
|
||||
throw new McpError(`工具 ${name} 返回了无效响应`, -1);
|
||||
const content = (result as Record<string, unknown>).content;
|
||||
if (!Array.isArray(content)) {
|
||||
throw new McpError(`工具 ${name} 返回的 content 不是数组`, MCP_ERR_MALFORMED);
|
||||
}
|
||||
for (const block of content) {
|
||||
if (typeof block !== "object" || block === null) {
|
||||
throw new McpError(`工具 ${name} 返回的 content 块不是对象`, MCP_ERR_MALFORMED);
|
||||
}
|
||||
const b = block as Record<string, unknown>;
|
||||
if (typeof b.type !== "string" || b.type.length === 0) {
|
||||
throw new McpError(`工具 ${name} 返回的 content 块缺少 type 字段`, MCP_ERR_MALFORMED);
|
||||
}
|
||||
if (b.type === "text" && typeof b.text !== "string") {
|
||||
throw new McpError(`工具 ${name} 返回的 text 块缺少 text 内容`, MCP_ERR_MALFORMED);
|
||||
}
|
||||
}
|
||||
const toolResult = result as McpToolCallResult;
|
||||
if (toolResult.isError === true) {
|
||||
const errText = toolResult.content
|
||||
.filter((c) => c.type === "text" && c.text !== undefined)
|
||||
.map((c) => c.text!)
|
||||
.join("; ");
|
||||
throw new McpError(
|
||||
`工具 ${name} 执行失败(isError=true):${errText || "服务器未返回错误详情"}`,
|
||||
MCP_ERR_MALFORMED,
|
||||
);
|
||||
}
|
||||
return toolResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the knowledge base. Convenience wrapper around callTool.
|
||||
*
|
||||
* Reads the real ACL from the server response (an `ACL: <level>` marker line)
|
||||
* instead of assuming "public". When the resolved ACL is not in `allowedAcl`,
|
||||
* a filtered "no access" article is returned in place of the real content.
|
||||
* 读取服务器响应中的真实 ACL(`ACL: <level>` 标记行)而非假设 "public"。
|
||||
* Fail-closed(M6):
|
||||
* - ACL 缺失/无法解析 → 抛 McpError(MCP_ERR_ACL_UNKNOWN),条目绝不原样返回
|
||||
* - 空文本 = 服务器明确"无匹配",返回"未找到结果"占位(与 ACL 无法确定区分)
|
||||
* - 解析出的 ACL 不在 allowedAcl 内 → 返回"无权限访问"占位(不泄露原文)
|
||||
*/
|
||||
async searchKnowledgeBase(
|
||||
query: string,
|
||||
@ -134,11 +187,13 @@ export class McpHttpClient {
|
||||
}> {
|
||||
const result = await this.callTool("kb_search", { query, mode });
|
||||
const text = result.content
|
||||
.filter((c) => c.type === "text" && c.text)
|
||||
.map((c) => c.text!)
|
||||
.filter((c) => c.type === "text")
|
||||
.map((c) => c.text)
|
||||
.filter((t): t is string => t !== undefined)
|
||||
.join("\n\n");
|
||||
|
||||
if (!text) {
|
||||
// 服务器明确返回空内容 → "无匹配",既不是错误也不是 ACL 问题
|
||||
return {
|
||||
title: `未找到结果(query="${query}", mode=${mode})`,
|
||||
body: `未找到与 "${query}" 相关的知识条目。请调整关键词后重试。`,
|
||||
@ -149,7 +204,14 @@ export class McpHttpClient {
|
||||
const lines = text.split("\n");
|
||||
const title = lines[0]?.replace(/^#\s+/, "").trim() ?? "知识条目";
|
||||
|
||||
const acl = parseAclFromText(text) ?? "public";
|
||||
const acl = parseAclFromText(text);
|
||||
if (acl === null) {
|
||||
// ACL 缺失/无法解析 → fail-closed:条目被排除,且与"无匹配"明确区分
|
||||
throw new McpError(
|
||||
`知识条目缺少可解析的 ACL 标记(query="${query}",mode=${mode}),已 fail-closed 过滤`,
|
||||
MCP_ERR_ACL_UNKNOWN,
|
||||
);
|
||||
}
|
||||
if (!allowedAcl.includes(acl)) {
|
||||
return {
|
||||
title: "无权限访问",
|
||||
@ -210,24 +272,40 @@ export class McpHttpClient {
|
||||
);
|
||||
}
|
||||
|
||||
let json: Record<string, unknown>;
|
||||
let json: unknown;
|
||||
try {
|
||||
json = (await response.json()) as Record<string, unknown>;
|
||||
json = await response.json();
|
||||
} catch {
|
||||
throw new McpError("MCP 服务器返回了非 JSON 响应", -1);
|
||||
throw new McpError("MCP 服务器返回了非 JSON 响应", MCP_ERR_MALFORMED);
|
||||
}
|
||||
|
||||
if (json.error) {
|
||||
const err = json.error as { message?: string; code?: number };
|
||||
debugLog("mcp", "error response", { id, method, code: err.code ?? -1 });
|
||||
// 响应必须是 JSON 对象:数组/标量/null 都是畸形,不能走 result/error 读取
|
||||
if (typeof json !== "object" || json === null || Array.isArray(json)) {
|
||||
throw new McpError("MCP 服务器返回了非对象 JSON 响应", MCP_ERR_MALFORMED);
|
||||
}
|
||||
const envelope = json as Record<string, unknown>;
|
||||
|
||||
if (envelope.error !== undefined) {
|
||||
const err =
|
||||
typeof envelope.error === "object" && envelope.error !== null
|
||||
? (envelope.error as Record<string, unknown>)
|
||||
: {};
|
||||
const message = typeof err.message === "string" ? err.message : "未知错误";
|
||||
const code = typeof err.code === "number" ? err.code : -1;
|
||||
debugLog("mcp", "error response", { id, method, code });
|
||||
throw new McpError(`MCP 错误:${message}`, code);
|
||||
}
|
||||
|
||||
// JSON-RPC 响应必须携带 result 或 error,两者皆无 = 畸形信封
|
||||
if (!("result" in envelope)) {
|
||||
throw new McpError(
|
||||
`MCP 错误:${err.message ?? "未知错误"}`,
|
||||
err.code ?? -1,
|
||||
`MCP 响应缺少 result/error 字段(method="${method}")`,
|
||||
MCP_ERR_MALFORMED,
|
||||
);
|
||||
}
|
||||
|
||||
debugLog("mcp", "response", { id, method });
|
||||
return json.result;
|
||||
return envelope.result;
|
||||
}
|
||||
}
|
||||
|
||||
@ -242,4 +320,4 @@ export function createMcpClientFromEnv(): McpHttpClient | null {
|
||||
if (!url) return null;
|
||||
const token = getOptionalEnv("DG_MCP_TOKEN");
|
||||
return new McpHttpClient({ serverUrl: url, token });
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
* - deps 注入文件读取,便于单测与 HTTP 包装复用。
|
||||
*/
|
||||
|
||||
import { readSessionLogEntries, aggregateUsage } from "./observability.ts";
|
||||
import { readSessionLogEntries, aggregateUsage, resolveSince, SinceParseError } from "./observability.ts";
|
||||
import { detectCandidates } from "./detector.ts";
|
||||
import type { ScriptCandidate } from "./detector.ts";
|
||||
import type { WorkflowRecord } from "./registry.ts";
|
||||
@ -103,9 +103,11 @@ function callTool(name: string, args: CallArgs, deps: McpServerDeps): { text: st
|
||||
};
|
||||
}
|
||||
case "usage_stats": {
|
||||
// v2.1.4:--since 在共享解析点(resolveSince)验证,非法值抛 SinceParseError → -32602
|
||||
const since = args.since !== undefined ? resolveSince(args.since) : undefined;
|
||||
const stats = aggregateUsage(readSessionLogEntries(deps.readSessionLog()).entries, {
|
||||
boardId: args.boardId,
|
||||
since: args.since,
|
||||
since,
|
||||
});
|
||||
return { text: JSON.stringify(stats, null, 2) };
|
||||
}
|
||||
@ -165,6 +167,8 @@ export function handleMcpJsonRpc(body: unknown, deps: McpServerDeps): McpJsonRpc
|
||||
const content = callTool(params.name, args, deps);
|
||||
return { jsonrpc: "2.0", id, result: { content: [{ type: "text", text: content.text }] } };
|
||||
} catch (e) {
|
||||
// v2.1.4:参数错误(如非法 since)返回 -32602 Invalid params,而非内部错误
|
||||
if (e instanceof SinceParseError) return err(id, -32602, e.message);
|
||||
return err(id, -32603, `Internal error: ${(e as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
@ -189,19 +189,60 @@ export interface UsageArgs {
|
||||
json: boolean;
|
||||
}
|
||||
|
||||
/** `7d`/`24h` → 对应 ISO 时间戳;已是 ISO 格式则原样返回。 */
|
||||
/** --since 有效格式提示(Nd/Nh/ISO),拼进错误消息。 */
|
||||
export const SINCE_VALID_FORMATS =
|
||||
"有效格式:Nd(N 天)/ Nh(N 小时)/ ISO 时间戳(如 7d / 24h / 2026-08-14T00:00:00Z)";
|
||||
|
||||
/**
|
||||
* --since 参数非法时抛出的类型化错误(v2.1.4 加固,fail-closed)。
|
||||
* 调用方据此区分“参数错误”与“内部错误”,给出清晰反馈而不是静默 NaN/垃圾统计。
|
||||
*/
|
||||
export class SinceParseError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = "SinceParseError";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ISO 时间戳形态(匹配 toISOString 输出的字典序可比格式):
|
||||
* 日期可省略时分秒(2026-08-14),秒/毫秒/时区偏移可选。
|
||||
*/
|
||||
const ISO_TIMESTAMP_RE =
|
||||
/^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}(:\d{2}(\.\d{1,9})?)?(Z|[+-]\d{2}:?\d{2})?)?$/;
|
||||
|
||||
/** 形状像 ISO 且 Date.parse 认可(挡住 2026-99-99 / 25 时等非法值)。 */
|
||||
function isIsoTimestamp(val: string): boolean {
|
||||
return ISO_TIMESTAMP_RE.test(val) && !Number.isNaN(Date.parse(val));
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 --since 值并验证(v2.1.4:非法输入抛 SinceParseError,不再静默透传):
|
||||
* - `Nd` / `Nh`(N 为正整数)→ 对应 ISO 时间戳
|
||||
* - ISO 时间戳 → 原样返回
|
||||
* 其余一律拒绝,错误消息列出有效格式。
|
||||
*/
|
||||
export function resolveSince(val: string): string {
|
||||
const dayMatch = /^(\d+)d$/.exec(val);
|
||||
if (dayMatch) return new Date(Date.now() - Number(dayMatch[1]) * 86_400_000).toISOString();
|
||||
if (dayMatch) {
|
||||
const days = Number(dayMatch[1]);
|
||||
if (days < 1) throw new SinceParseError(`since 无效:"${val}"(N 应为正整数)。${SINCE_VALID_FORMATS}`);
|
||||
return new Date(Date.now() - days * 86_400_000).toISOString();
|
||||
}
|
||||
const hourMatch = /^(\d+)h$/.exec(val);
|
||||
if (hourMatch) return new Date(Date.now() - Number(hourMatch[1]) * 3_600_000).toISOString();
|
||||
return val;
|
||||
if (hourMatch) {
|
||||
const hours = Number(hourMatch[1]);
|
||||
if (hours < 1) throw new SinceParseError(`since 无效:"${val}"(N 应为正整数)。${SINCE_VALID_FORMATS}`);
|
||||
return new Date(Date.now() - hours * 3_600_000).toISOString();
|
||||
}
|
||||
if (isIsoTimestamp(val)) return val;
|
||||
throw new SinceParseError(`since 无效:"${val}"。${SINCE_VALID_FORMATS}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 /dg-usage 参数(KISS 确定性,无 shell 语义):
|
||||
* - `--board <id>`:只统计该板卡
|
||||
* - `--since <Nd|Nh|ISO>`:只看最近 N 天/小时,或指定 ISO 时间戳之后
|
||||
* - `--since <Nd|Nh|ISO>`:只看最近 N 天/小时,或指定 ISO 时间戳之后(非法值抛 SinceParseError)
|
||||
* - `--json`:输出原始 JSON 统计
|
||||
* 未知参数忽略(不报错)。
|
||||
*/
|
||||
@ -223,6 +264,8 @@ export function parseUsageArgs(args: string): UsageArgs {
|
||||
if (val && !val.startsWith("--")) {
|
||||
out.filter.since = resolveSince(val);
|
||||
i += 1;
|
||||
} else {
|
||||
throw new SinceParseError(`--since 缺少值。${SINCE_VALID_FORMATS}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -48,6 +48,47 @@ export const MODEL_IDS: Readonly<Record<ExpertType, string>> = {
|
||||
general: "dg-general-v1",
|
||||
};
|
||||
|
||||
// ── Keyword matching ──────────────────────────────────────────────────────────
|
||||
|
||||
/** RegExp 元字符——嵌入动态正则前必须转义,防止关键词里的 `.`/`+` 等被当作语法。 */
|
||||
const REGEX_ESCAPE = /[.*+?^${}()|[\]\\]/g;
|
||||
|
||||
function escapeRegExp(s: string): string {
|
||||
return s.replace(REGEX_ESCAPE, "\\$&");
|
||||
}
|
||||
|
||||
/** 关键词是否由纯 ASCII 词字符(字母/数字/下划线)组成——只有这类关键词需要词边界。 */
|
||||
function isAsciiWordKeyword(kw: string): boolean {
|
||||
return /^[A-Za-z0-9_]+$/.test(kw);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关键词匹配(大小写不敏感)。
|
||||
*
|
||||
* ASCII 关键词(如 "deploy"、"oe")必须满足词边界:两侧只能是"非词字符"或
|
||||
* 字符串边缘。这样 "oe" 不会误命中 "poet"/"does","pid" 不会误命中
|
||||
* "rapid"/"spider"。关键词先做正则元字符转义再嵌入 `\b`。
|
||||
*
|
||||
* CJK 关键词(如 "摄像头")走普通子串匹配:
|
||||
* - 不套 `\b`——CJK 字符在 JS 默认正则里是非词字符,`\b摄像头\b` 在
|
||||
* "查看摄像头画面" 中反而匹配失败(CJK 与 CJK 之间没有词边界);
|
||||
* - 反过来,CJK 紧邻 ASCII 关键词天然构成边界("使用oe工具链" 中 "oe"
|
||||
* 两侧是中文,`\b` 判定成立)。
|
||||
*
|
||||
* 注意:正则刻意不带 `u` flag——`\w` 保持 ASCII 语义,CJK 字符才会被当作
|
||||
* 非词字符参与边界判定;换用 unicode 语义会改变 CJK 行为,需另行测试。
|
||||
*
|
||||
* 导出便于单测直接验证词边界与元字符转义行为(当前规则表没有含元字符的
|
||||
* 关键词,转义路径只能通过本函数覆盖)。
|
||||
*/
|
||||
export function keywordMatches(text: string, kw: string): boolean {
|
||||
if (isAsciiWordKeyword(kw)) {
|
||||
const re = new RegExp(`\\b${escapeRegExp(kw)}\\b`, "i");
|
||||
return re.test(text);
|
||||
}
|
||||
return text.toLowerCase().includes(kw.toLowerCase());
|
||||
}
|
||||
|
||||
// ── Layer 1: Rules table ─────────────────────────────────────────────────────
|
||||
|
||||
/** A single routing rule: keyword → domain + expert + model, with priority. */
|
||||
@ -99,17 +140,17 @@ export const ROUTING_RULES: ReadonlyArray<RoutingRule> = [
|
||||
*
|
||||
* Returns a high-confidence RouteDecision for the highest-priority rule whose
|
||||
* keywords appear in `task`, or `null` when no rule matches. Matching is
|
||||
* case-insensitive. Ties in priority are resolved by insertion order (the
|
||||
* sort is stable).
|
||||
* case-insensitive. ASCII keywords are matched at word boundaries (see
|
||||
* `keywordMatches`); CJK keywords by substring. Ties in priority are resolved
|
||||
* by insertion order (the sort is stable).
|
||||
*/
|
||||
export function routeByRules(task: string): RouteDecision | null {
|
||||
const t = task.toLowerCase();
|
||||
const matchedRules: RoutingRule[] = [];
|
||||
const matchedKeywords: string[] = [];
|
||||
for (const rule of ROUTING_RULES) {
|
||||
let ruleMatched = false;
|
||||
for (const kw of rule.keywords) {
|
||||
if (t.includes(kw.toLowerCase())) {
|
||||
if (keywordMatches(task, kw)) {
|
||||
if (!matchedKeywords.includes(kw)) matchedKeywords.push(kw);
|
||||
ruleMatched = true;
|
||||
}
|
||||
@ -153,9 +194,8 @@ export function routeByKb(kbResults?: KbArticle[]): RouteDecision | null {
|
||||
|
||||
const hits: KbHit[] = [];
|
||||
for (const article of kbResults) {
|
||||
const title = article.title.toLowerCase();
|
||||
for (const rule of ROUTING_RULES) {
|
||||
const kw = rule.keywords.find((k) => title.includes(k.toLowerCase()));
|
||||
const kw = rule.keywords.find((k) => keywordMatches(article.title, k));
|
||||
if (kw) {
|
||||
hits.push({ rule, title: article.title, keyword: kw });
|
||||
break; // first domain match per article
|
||||
|
||||
@ -15,8 +15,8 @@
|
||||
* 信任门禁(auto 需确认)由 dg_workflow_run 工具层处理;本模块只负责执行 + 阶梯。
|
||||
*/
|
||||
|
||||
import type { ExecResult } from "./types.ts";
|
||||
import { assessRisk } from "./risk.ts";
|
||||
import type { ExecResult, SdbAuditEntry } from "./types.ts";
|
||||
import { assessRisk, writeAuditLog } from "./risk.ts";
|
||||
import { discoverBoards } from "./discovery.ts";
|
||||
import { execOnBoard, sftpPush, sftpPull, SshConfigError } from "./ssh.ts";
|
||||
import { checkBoardHealth } from "./workflows/board-health.ts";
|
||||
@ -45,6 +45,10 @@ export interface StepOutcome {
|
||||
tool: string;
|
||||
ok: boolean;
|
||||
summary: string;
|
||||
/** SDB 审计日志 id(命令步骤写入;审计写失败时为 null)。 */
|
||||
auditLogId?: string | null;
|
||||
/** 审计写入失败时的错误信息(命令已执行或已判定,结果仍如实返回)。 */
|
||||
auditWriteError?: string;
|
||||
}
|
||||
|
||||
export interface WorkflowRunResult {
|
||||
@ -78,6 +82,34 @@ function strArg(args: Record<string, unknown>, keys: string[]): string | undefin
|
||||
return typeof v === "string" && v.length > 0 ? v : undefined;
|
||||
}
|
||||
|
||||
/** 审计写入结果:logId 为 null 表示写入失败(error 携带原因)。 */
|
||||
interface StepAudit {
|
||||
logId: string | null;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写一条 SDB 审计(v2.1.4 M5:runner 逐命令审计)。
|
||||
* 与 safe-exec.ts / tools.ts 的 dg_ssh_exec 共用同一 SdbAuditEntry 形状;
|
||||
* 写入失败不抛出(fail-open 透出),命令结果仍如实返回。
|
||||
*/
|
||||
function writeStepAudit(entry: Omit<SdbAuditEntry, "log_id" | "timestamp">): StepAudit {
|
||||
try {
|
||||
return { logId: writeAuditLog(entry) };
|
||||
} catch (err) {
|
||||
return { logId: null, error: err instanceof Error ? err.message : String(err) };
|
||||
}
|
||||
}
|
||||
|
||||
/** 把审计结果(logId / 可选 writeError)附加到步骤结果上。 */
|
||||
function stepWithAudit(outcome: StepOutcome, audit: StepAudit): StepOutcome {
|
||||
return {
|
||||
...outcome,
|
||||
auditLogId: audit.logId,
|
||||
...(audit.error !== undefined ? { auditWriteError: audit.error } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
/** 执行单个步骤(确定性;不支持/非法 → ok=false,不抛异常)。 */
|
||||
async function executeStep(
|
||||
tool: string,
|
||||
@ -96,20 +128,51 @@ async function executeStep(
|
||||
if (!command) return { tool, ok: false, summary: "缺少 command 参数" };
|
||||
if (!boardId) return { tool, ok: false, summary: "缺少 board_id 参数" };
|
||||
const risk = assessRisk(command);
|
||||
const auditBase = {
|
||||
session_id: opts.sessionId ?? null,
|
||||
board_id: boardId,
|
||||
command,
|
||||
risk_level: risk.level,
|
||||
risk_pattern: risk.pattern,
|
||||
user_confirmed: false,
|
||||
agent_invocation_id: opts.agentInvocationId ?? null,
|
||||
};
|
||||
if (risk.level === "fatal") {
|
||||
return { tool, ok: false, summary: `fatal 拒绝:${risk.description}` };
|
||||
return stepWithAudit(
|
||||
{ tool, ok: false, summary: `fatal 拒绝:${risk.description}` },
|
||||
writeStepAudit({ ...auditBase, action: "rejected", exit_code: null, duration_ms: null }),
|
||||
);
|
||||
}
|
||||
if ((risk.level === "severe" || risk.level === "warning") && args.force !== true) {
|
||||
return { tool, ok: false, summary: `需确认(${risk.level}):${risk.description}` };
|
||||
return stepWithAudit(
|
||||
{ tool, ok: false, summary: `需确认(${risk.level}):${risk.description}` },
|
||||
writeStepAudit({ ...auditBase, action: "confirmation_required", exit_code: null, duration_ms: null }),
|
||||
);
|
||||
}
|
||||
const board = await resolveBoard(boardId);
|
||||
if (!board) return { tool, ok: false, summary: `未找到板卡 ${boardId}` };
|
||||
const r: ExecResult = await execOnBoard(board, command, opts.timeoutMs);
|
||||
return {
|
||||
tool,
|
||||
ok: r.exit_code === 0,
|
||||
summary: `exit ${r.exit_code}:${(r.stdout || r.stderr).slice(0, 200)}`,
|
||||
};
|
||||
let r: ExecResult;
|
||||
try {
|
||||
r = await execOnBoard(board, command, opts.timeoutMs);
|
||||
} catch (err) {
|
||||
return stepWithAudit(
|
||||
{ tool, ok: false, summary: `异常:${(err as Error).message}` },
|
||||
writeStepAudit({ ...auditBase, action: "exec_error", exit_code: null, duration_ms: null }),
|
||||
);
|
||||
}
|
||||
return stepWithAudit(
|
||||
{
|
||||
tool,
|
||||
ok: r.exit_code === 0,
|
||||
summary: `exit ${r.exit_code}:${(r.stdout || r.stderr).slice(0, 200)}`,
|
||||
},
|
||||
writeStepAudit({
|
||||
...auditBase,
|
||||
action: args.force === true ? "executed_with_force" : "executed",
|
||||
exit_code: r.exit_code,
|
||||
duration_ms: r.duration_ms,
|
||||
}),
|
||||
);
|
||||
}
|
||||
case "dg_file_sync": {
|
||||
const direction = strArg(args, ["direction"]);
|
||||
@ -204,6 +267,19 @@ export async function runRegisteredWorkflow(
|
||||
}
|
||||
}
|
||||
|
||||
// board_id 守卫(fail-closed):任何步骤显式携带的 board_id 必须与 workflow 默认
|
||||
// board_id 一致;不一致 → 在任何步骤执行前抛错,并点名违规步骤。
|
||||
if (opts.boardId) {
|
||||
for (let i = 0; i < steps.length; i++) {
|
||||
const stepBoardId = strArg(steps[i]!.args, ["board_id", "boardId"]);
|
||||
if (stepBoardId && stepBoardId !== opts.boardId) {
|
||||
throw new SshConfigError(
|
||||
`第 ${i + 1} 步 ${steps[i]!.tool} 的 board_id(${stepBoardId})与 workflow board_id(${opts.boardId})不一致`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const errors: string[] = [];
|
||||
const outcomes: StepOutcome[] = [];
|
||||
let allOk = true;
|
||||
|
||||
374
lib/ssh.ts
374
lib/ssh.ts
@ -12,6 +12,7 @@ import { dirname, join, isAbsolute } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
import { getOptionalEnv } from "./env.ts";
|
||||
import { debugLog } from "./debug.ts";
|
||||
import { TRUNCATE_LIMITS } from "./types.ts";
|
||||
import type { Board, ExecResult, SyncResult, SshErrorMeta } from "./types.ts";
|
||||
|
||||
const { Client } = ssh2;
|
||||
@ -64,8 +65,12 @@ interface PoolEntry {
|
||||
client: SshClient;
|
||||
board: Board;
|
||||
lastActivityAt: number;
|
||||
idleTimer: ReturnType<typeof setTimeout>;
|
||||
/** 空闲淘汰定时器;仅在连接空闲(busyCount === 0)时被调度。 */
|
||||
idleTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
/** 在途连接(H2 并发去重):并发 acquire 复用同一个 connect promise。 */
|
||||
connecting: Promise<SshClient> | null;
|
||||
/** 正在使用该连接的调用方数量(H1 忙碌追踪)。 */
|
||||
busyCount: number;
|
||||
closed: boolean;
|
||||
}
|
||||
|
||||
@ -73,6 +78,10 @@ const IDLE_TIMEOUT_MS = 60_000;
|
||||
const CONNECT_TIMEOUT_MS = 10_000;
|
||||
const KEEPALIVE_INTERVAL_MS = 30_000;
|
||||
const KEEPALIVE_COUNT_MAX = 3;
|
||||
/** SIGTERM 之后到补发 SIGKILL 的宽限期(M3)。 */
|
||||
const KILL_GRACE_MS = 2_000;
|
||||
/** 单条输出流(stdout/stderr)累计字节上限(M4 背压保护)。 */
|
||||
const STREAM_BYTE_CAP = TRUNCATE_LIMITS.maxBytes;
|
||||
|
||||
export class BoardConnectionPool {
|
||||
private pool = new Map<string, PoolEntry>();
|
||||
@ -84,15 +93,32 @@ export class BoardConnectionPool {
|
||||
async acquire(board: Board): Promise<SshClient> {
|
||||
const key = this.keyFor(board);
|
||||
const existing = this.pool.get(key);
|
||||
|
||||
if (existing && existing.closed) {
|
||||
clearTimeout(existing.idleTimer);
|
||||
// 远程已关闭的条目:清掉残留定时器后重建
|
||||
this.clearIdleTimer(existing);
|
||||
this.pool.delete(key);
|
||||
} else if (existing) {
|
||||
existing.lastActivityAt = Date.now();
|
||||
clearTimeout(existing.idleTimer);
|
||||
existing.idleTimer = setTimeout(() => this.evict(key), IDLE_TIMEOUT_MS);
|
||||
debugLog("ssh", "pool hit", { key });
|
||||
return existing.client;
|
||||
// 池命中:若仍在建连,复用同一个在途 connect(H2 去重);否则直接复用就绪连接
|
||||
if (existing.connecting) {
|
||||
try {
|
||||
const client = await existing.connecting;
|
||||
existing.busyCount += 1;
|
||||
existing.lastActivityAt = Date.now();
|
||||
this.armIdleTimer(existing, key);
|
||||
return client;
|
||||
} catch (err) {
|
||||
// 在途连接失败:删除失败条目,交给下方冷启动逻辑重新建连
|
||||
this.clearIdleTimer(existing);
|
||||
this.pool.delete(key);
|
||||
}
|
||||
} else {
|
||||
existing.busyCount += 1;
|
||||
existing.lastActivityAt = Date.now();
|
||||
this.armIdleTimer(existing, key);
|
||||
debugLog("ssh", "pool hit", { key });
|
||||
return existing.client;
|
||||
}
|
||||
}
|
||||
|
||||
debugLog("ssh", "connecting", { key, board_id: board.id });
|
||||
@ -101,37 +127,62 @@ export class BoardConnectionPool {
|
||||
client: new Client(),
|
||||
board,
|
||||
lastActivityAt: Date.now(),
|
||||
idleTimer: undefined as unknown as ReturnType<typeof setTimeout>,
|
||||
idleTimer: undefined,
|
||||
connecting: null,
|
||||
busyCount: 1,
|
||||
closed: false,
|
||||
};
|
||||
entry.idleTimer = setTimeout(() => this.evict(key), IDLE_TIMEOUT_MS);
|
||||
this.pool.set(key, entry);
|
||||
this.armIdleTimer(entry, key);
|
||||
|
||||
const connectPromise = this.connectClient(entry.client, board);
|
||||
entry.connecting = connectPromise;
|
||||
try {
|
||||
await connectPromise;
|
||||
const client = await connectPromise;
|
||||
entry.connecting = null;
|
||||
debugLog("ssh", "connected", { key });
|
||||
entry.client.on("close", () => {
|
||||
entry.closed = true;
|
||||
clearTimeout(entry.idleTimer);
|
||||
this.clearIdleTimer(entry);
|
||||
this.pool.delete(key);
|
||||
});
|
||||
entry.client.on("error", () => {
|
||||
entry.closed = true;
|
||||
clearTimeout(entry.idleTimer);
|
||||
this.clearIdleTimer(entry);
|
||||
this.pool.delete(key);
|
||||
});
|
||||
return entry.client;
|
||||
return client;
|
||||
} catch (err) {
|
||||
clearTimeout(entry.idleTimer);
|
||||
entry.connecting = null;
|
||||
entry.closed = true;
|
||||
this.clearIdleTimer(entry);
|
||||
this.pool.delete(key);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/** 归还连接:释放调用方引用;空闲时重新调度淘汰定时器。 */
|
||||
release(board: Board): void {
|
||||
const key = this.keyFor(board);
|
||||
const entry = this.pool.get(key);
|
||||
if (!entry) return;
|
||||
entry.busyCount = Math.max(0, entry.busyCount - 1);
|
||||
entry.lastActivityAt = Date.now();
|
||||
this.armIdleTimer(entry, key);
|
||||
}
|
||||
|
||||
private armIdleTimer(entry: PoolEntry, key: string): void {
|
||||
this.clearIdleTimer(entry);
|
||||
entry.idleTimer = setTimeout(() => this.evict(key), IDLE_TIMEOUT_MS);
|
||||
}
|
||||
|
||||
private clearIdleTimer(entry: PoolEntry): void {
|
||||
if (entry.idleTimer !== undefined) {
|
||||
clearTimeout(entry.idleTimer);
|
||||
entry.idleTimer = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
private connectClient(client: SshClient, board: Board): Promise<SshClient> {
|
||||
return new Promise<SshClient>((resolve, reject) => {
|
||||
const keyPath = getOptionalEnv("DG_SSH_KEY") ?? join(homedir(), ".ssh", "id_rsa");
|
||||
@ -184,7 +235,14 @@ export class BoardConnectionPool {
|
||||
private evict(key: string): void {
|
||||
const entry = this.pool.get(key);
|
||||
if (!entry) return;
|
||||
if (entry.busyCount > 0) {
|
||||
// 连接正在执行命令(或仍有调用方持有),本轮跳过淘汰;稍后重新调度
|
||||
debugLog("ssh", "evict skipped (busy)", { key, busy_count: entry.busyCount });
|
||||
this.armIdleTimer(entry, key);
|
||||
return;
|
||||
}
|
||||
this.pool.delete(key);
|
||||
entry.closed = true;
|
||||
try {
|
||||
entry.client.end();
|
||||
} catch (err) {
|
||||
@ -193,8 +251,15 @@ export class BoardConnectionPool {
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
for (const key of Array.from(this.pool.keys())) {
|
||||
this.evict(key);
|
||||
for (const [key, entry] of Array.from(this.pool.entries())) {
|
||||
this.clearIdleTimer(entry);
|
||||
this.pool.delete(key);
|
||||
entry.closed = true;
|
||||
try {
|
||||
entry.client.end();
|
||||
} catch (err) {
|
||||
console.warn("[SshPool] dispose: 连接关闭失败", key, (err as Error).message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -203,75 +268,115 @@ export const boardPool = new BoardConnectionPool();
|
||||
|
||||
// ── SSH exec (TECHNICAL_ARCHITECTURE.md §2.1) ────────────────────────────────
|
||||
|
||||
export async function execOnBoard(board: Board, command: string, timeoutMs = 60_000): Promise<ExecResult> {
|
||||
/** execOnBoard 的返回值:在 ExecResult 之上补充背压截断标记(M4)。 */
|
||||
export interface SshExecResult extends ExecResult {
|
||||
stdout_truncated: boolean;
|
||||
stderr_truncated: boolean;
|
||||
}
|
||||
|
||||
export async function execOnBoard(board: Board, command: string, timeoutMs = 60_000): Promise<SshExecResult> {
|
||||
const client = await boardPool.acquire(board);
|
||||
const start = Date.now();
|
||||
debugLog("ssh", "exec", { board_id: board.id, command, timeout_ms: timeoutMs });
|
||||
|
||||
return new Promise<ExecResult>((resolveFn, reject) => {
|
||||
client.exec(command, (err: Error | undefined, channel: ClientChannel) => {
|
||||
if (err) {
|
||||
reject(new SshExecError(`exec 失败:${err.message}`, err));
|
||||
return;
|
||||
}
|
||||
|
||||
const stdoutChunks: Buffer[] = [];
|
||||
const stderrChunks: Buffer[] = [];
|
||||
let settled = false;
|
||||
let timedOut = false;
|
||||
|
||||
const finish = (code: number | null, sig: string | null) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
const killTimerCbk = killTimer;
|
||||
if (killTimerCbk) clearTimeout(killTimerCbk);
|
||||
debugLog("ssh", "exec done", {
|
||||
exit_code: code,
|
||||
signal: sig,
|
||||
duration_ms: Date.now() - start,
|
||||
timed_out: timedOut,
|
||||
});
|
||||
resolveFn({
|
||||
stdout: Buffer.concat(stdoutChunks).toString("utf8"),
|
||||
stderr: Buffer.concat(stderrChunks).toString("utf8"),
|
||||
exit_code: code,
|
||||
signal: sig,
|
||||
duration_ms: Date.now() - start,
|
||||
timed_out: timedOut,
|
||||
});
|
||||
};
|
||||
|
||||
let killTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
const timer = setTimeout(() => {
|
||||
timedOut = true;
|
||||
try {
|
||||
channel.signal("TERM");
|
||||
} catch (err) {
|
||||
console.warn("[SshExec] TERM 信号发送失败", (err as Error).message);
|
||||
try {
|
||||
return await new Promise<SshExecResult>((resolveFn, reject) => {
|
||||
client.exec(command, (err: Error | undefined, channel: ClientChannel) => {
|
||||
if (err) {
|
||||
reject(new SshExecError(`exec 失败:${err.message}`, err));
|
||||
return;
|
||||
}
|
||||
killTimer = setTimeout(() => {
|
||||
try {
|
||||
channel.signal("KILL");
|
||||
} catch (err) {
|
||||
console.warn("[SshExec] KILL 信号发送失败", (err as Error).message);
|
||||
|
||||
const stdoutChunks: Buffer[] = [];
|
||||
const stderrChunks: Buffer[] = [];
|
||||
const stdoutState = { bytes: 0, truncated: false };
|
||||
const stderrState = { bytes: 0, truncated: false };
|
||||
let settled = false;
|
||||
let timedOut = false;
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
let killTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
|
||||
const clearTimers = () => {
|
||||
if (timer !== undefined) {
|
||||
clearTimeout(timer);
|
||||
timer = undefined;
|
||||
}
|
||||
}, 2000);
|
||||
}, timeoutMs);
|
||||
if (killTimer !== undefined) {
|
||||
clearTimeout(killTimer);
|
||||
killTimer = undefined;
|
||||
}
|
||||
};
|
||||
|
||||
channel.on("data", (d: Buffer) => stdoutChunks.push(d));
|
||||
channel.stderr.on("data", (d: Buffer) => stderrChunks.push(d));
|
||||
channel.on("close", (code: number | null, signal: string | null) => finish(code, signal));
|
||||
channel.on("error", (e: Error) => {
|
||||
if (!settled) {
|
||||
const pushChunk = (state: { bytes: number; truncated: boolean }, chunks: Buffer[], chunk: Buffer) => {
|
||||
if (state.truncated) return;
|
||||
const remaining = STREAM_BYTE_CAP - state.bytes;
|
||||
if (chunk.length <= remaining) {
|
||||
chunks.push(chunk);
|
||||
state.bytes += chunk.length;
|
||||
} else {
|
||||
if (remaining > 0) chunks.push(chunk.subarray(0, remaining));
|
||||
state.bytes = STREAM_BYTE_CAP;
|
||||
state.truncated = true;
|
||||
}
|
||||
};
|
||||
|
||||
const finish = (code: number | null, sig: string | null) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
reject(new SshExecError(`channel 错误:${e.message}`, e));
|
||||
}
|
||||
clearTimers();
|
||||
debugLog("ssh", "exec done", {
|
||||
exit_code: code,
|
||||
signal: sig,
|
||||
duration_ms: Date.now() - start,
|
||||
timed_out: timedOut,
|
||||
stdout_truncated: stdoutState.truncated,
|
||||
stderr_truncated: stderrState.truncated,
|
||||
});
|
||||
resolveFn({
|
||||
stdout: Buffer.concat(stdoutChunks).toString("utf8"),
|
||||
stderr: Buffer.concat(stderrChunks).toString("utf8"),
|
||||
exit_code: code,
|
||||
signal: sig,
|
||||
duration_ms: Date.now() - start,
|
||||
timed_out: timedOut,
|
||||
stdout_truncated: stdoutState.truncated,
|
||||
stderr_truncated: stderrState.truncated,
|
||||
});
|
||||
};
|
||||
|
||||
timer = setTimeout(() => {
|
||||
timedOut = true;
|
||||
try {
|
||||
channel.signal("TERM");
|
||||
} catch (err) {
|
||||
console.warn("[SshExec] TERM 信号发送失败", (err as Error).message);
|
||||
}
|
||||
killTimer = setTimeout(() => {
|
||||
killTimer = undefined;
|
||||
try {
|
||||
channel.signal("KILL");
|
||||
} catch (err) {
|
||||
console.warn("[SshExec] KILL 信号发送失败", (err as Error).message);
|
||||
}
|
||||
}, KILL_GRACE_MS);
|
||||
}, timeoutMs);
|
||||
|
||||
channel.on("data", (d: Buffer) => pushChunk(stdoutState, stdoutChunks, d));
|
||||
channel.stderr.on("data", (d: Buffer) => pushChunk(stderrState, stderrChunks, d));
|
||||
channel.on("close", (code: number | null, signal: string | null) => finish(code, signal));
|
||||
channel.on("error", (e: Error) => {
|
||||
if (!settled) {
|
||||
settled = true;
|
||||
clearTimers();
|
||||
reject(new SshExecError(`channel 错误:${e.message}`, e));
|
||||
}
|
||||
});
|
||||
channel.end();
|
||||
});
|
||||
channel.end();
|
||||
});
|
||||
});
|
||||
} finally {
|
||||
boardPool.release(board);
|
||||
}
|
||||
}
|
||||
|
||||
// ── SFTP file sync (push / pull) ─────────────────────────────────────────────
|
||||
@ -292,33 +397,37 @@ export async function sftpPush(
|
||||
const client = await boardPool.acquire(board);
|
||||
const start = Date.now();
|
||||
debugLog("ssh", "sftp push", { board_id: board.id, local_path: localPath, remote_path: remotePath });
|
||||
const sftp = await openSftp(client);
|
||||
try {
|
||||
const remoteDir = dirname(remotePath);
|
||||
await mkdirpRemote(sftp, remoteDir);
|
||||
await new Promise<void>((resolveFn, reject) => {
|
||||
const rs = createReadStream(localPath);
|
||||
const ws = sftp.createWriteStream(remotePath);
|
||||
ws.on("error", reject);
|
||||
rs.on("error", reject);
|
||||
rs.on("data", (chunk: string | Buffer) => {
|
||||
if (typeof chunk === "string") {
|
||||
reject(new SshConfigError(`本地文件流意外输出字符串块:${localPath}`));
|
||||
rs.destroy();
|
||||
return;
|
||||
}
|
||||
ws.write(chunk);
|
||||
const sftp = await openSftp(client);
|
||||
try {
|
||||
const remoteDir = dirname(remotePath);
|
||||
await mkdirpRemote(sftp, remoteDir);
|
||||
await new Promise<void>((resolveFn, reject) => {
|
||||
const rs = createReadStream(localPath);
|
||||
const ws = sftp.createWriteStream(remotePath);
|
||||
ws.on("error", reject);
|
||||
rs.on("error", reject);
|
||||
rs.on("data", (chunk: string | Buffer) => {
|
||||
if (typeof chunk === "string") {
|
||||
reject(new SshConfigError(`本地文件流意外输出字符串块:${localPath}`));
|
||||
rs.destroy();
|
||||
return;
|
||||
}
|
||||
ws.write(chunk);
|
||||
});
|
||||
rs.on("close", () => ws.end(() => resolveFn()));
|
||||
});
|
||||
rs.on("close", () => ws.end(() => resolveFn()));
|
||||
});
|
||||
debugLog("ssh", "sftp push done", { bytes_synced: stat.size, duration_ms: Date.now() - start });
|
||||
return {
|
||||
files_synced: 1,
|
||||
bytes_synced: stat.size,
|
||||
duration_ms: Date.now() - start,
|
||||
};
|
||||
debugLog("ssh", "sftp push done", { bytes_synced: stat.size, duration_ms: Date.now() - start });
|
||||
return {
|
||||
files_synced: 1,
|
||||
bytes_synced: stat.size,
|
||||
duration_ms: Date.now() - start,
|
||||
};
|
||||
} finally {
|
||||
sftp.end();
|
||||
}
|
||||
} finally {
|
||||
sftp.end();
|
||||
boardPool.release(board);
|
||||
}
|
||||
}
|
||||
|
||||
@ -333,29 +442,33 @@ export async function sftpPull(
|
||||
const client = await boardPool.acquire(board);
|
||||
const start = Date.now();
|
||||
debugLog("ssh", "sftp pull", { board_id: board.id, remote_path: remotePath, local_path: localPath });
|
||||
const sftp = await openSftp(client);
|
||||
try {
|
||||
const localDir = dirname(localPath);
|
||||
await mkdir(localDir, { recursive: true });
|
||||
// Stream remote → local incrementally instead of buffering the whole file
|
||||
// in memory (large models/binaries would otherwise risk OOM).
|
||||
await new Promise<void>((resolveFn, reject) => {
|
||||
const ws = createWriteStream(localPath);
|
||||
const rs = sftp.createReadStream(remotePath);
|
||||
ws.on("error", reject);
|
||||
rs.on("error", reject);
|
||||
rs.on("data", (chunk: Buffer) => ws.write(chunk));
|
||||
rs.on("close", () => ws.end(() => resolveFn()));
|
||||
});
|
||||
const stat = statSync(localPath);
|
||||
debugLog("ssh", "sftp pull done", { bytes_synced: stat.size, duration_ms: Date.now() - start });
|
||||
return {
|
||||
files_synced: 1,
|
||||
bytes_synced: stat.size,
|
||||
duration_ms: Date.now() - start,
|
||||
};
|
||||
const sftp = await openSftp(client);
|
||||
try {
|
||||
const localDir = dirname(localPath);
|
||||
await mkdir(localDir, { recursive: true });
|
||||
// Stream remote → local incrementally instead of buffering the whole file
|
||||
// in memory (large models/binaries would otherwise risk OOM).
|
||||
await new Promise<void>((resolveFn, reject) => {
|
||||
const ws = createWriteStream(localPath);
|
||||
const rs = sftp.createReadStream(remotePath);
|
||||
ws.on("error", reject);
|
||||
rs.on("error", reject);
|
||||
rs.on("data", (chunk: Buffer) => ws.write(chunk));
|
||||
rs.on("close", () => ws.end(() => resolveFn()));
|
||||
});
|
||||
const stat = statSync(localPath);
|
||||
debugLog("ssh", "sftp pull done", { bytes_synced: stat.size, duration_ms: Date.now() - start });
|
||||
return {
|
||||
files_synced: 1,
|
||||
bytes_synced: stat.size,
|
||||
duration_ms: Date.now() - start,
|
||||
};
|
||||
} finally {
|
||||
sftp.end();
|
||||
}
|
||||
} finally {
|
||||
sftp.end();
|
||||
boardPool.release(board);
|
||||
}
|
||||
}
|
||||
|
||||
@ -373,15 +486,18 @@ async function mkdirpRemote(sftp: import("ssh2").SFTPWrapper, remoteDir: string)
|
||||
let cwd = "/";
|
||||
for (const part of parts) {
|
||||
cwd = join(cwd, part);
|
||||
try {
|
||||
await new Promise<void>((resolveFn, reject) => {
|
||||
sftp.mkdir(cwd, (err: Error | null | undefined) => {
|
||||
if (err && !err.message.includes("exists")) reject(err);
|
||||
else resolveFn();
|
||||
});
|
||||
await new Promise<void>((resolveFn, reject) => {
|
||||
sftp.mkdir(cwd, (err: Error | null | undefined) => {
|
||||
if (!err) {
|
||||
resolveFn();
|
||||
return;
|
||||
}
|
||||
if (err.message.includes("exists")) {
|
||||
resolveFn();
|
||||
return;
|
||||
}
|
||||
reject(new SshExecError(`远程目录创建失败:${cwd}(${err.message})`, err));
|
||||
});
|
||||
} catch (err) {
|
||||
console.warn("[SshSftp] mkdirp 失败", cwd, (err as Error).message);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -146,7 +146,7 @@ export interface SessionLogEntry {
|
||||
command: string | null;
|
||||
risk_level: RiskLevel | null;
|
||||
risk_pattern: string | null;
|
||||
action: "rejected" | "confirmation_required" | "executed" | "executed_with_force" | null;
|
||||
action: "rejected" | "confirmation_required" | "executed" | "executed_with_force" | "exec_error" | null;
|
||||
exit_code: number | null;
|
||||
duration_ms: number | null;
|
||||
user_confirmed: boolean | null;
|
||||
@ -174,7 +174,7 @@ export interface SdbAuditEntry {
|
||||
command: string;
|
||||
risk_level: RiskLevel;
|
||||
risk_pattern: string;
|
||||
action: "rejected" | "confirmation_required" | "executed" | "executed_with_force";
|
||||
action: "rejected" | "confirmation_required" | "executed" | "executed_with_force" | "exec_error";
|
||||
exit_code: number | null;
|
||||
duration_ms: number | null;
|
||||
user_confirmed: boolean;
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
import { discoverBoards } from "../discovery.ts";
|
||||
import { execOnBoard, SshConfigError, SshExecError } from "../ssh.ts";
|
||||
import { sourceTrosEnvCommand } from "../tros-env.ts";
|
||||
import { assertShellSafePath } from "./push-and-verify.ts";
|
||||
|
||||
export interface BuildVerifyResult {
|
||||
boardId: string;
|
||||
@ -137,11 +138,38 @@ export function parseBuildResult(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* colcon `--packages-select` 的包名白名单:字母/数字/下划线/连字符,空格或制表符
|
||||
* 分隔多包。白名单(而非黑名单)天然拒绝 shell 元字符 / glob / unicode 同形字——
|
||||
* fail-closed。
|
||||
*/
|
||||
const PACKAGE_NAMES_RE = /^[A-Za-z0-9_-]+(?:[ \t]+[A-Za-z0-9_-]+)*$/;
|
||||
|
||||
/**
|
||||
* 校验 `--packages-select` 包名列表。任何非法字符(元字符/引号/glob/unicode
|
||||
* 同形字)→ 抛 SshConfigError,错误信息点名 offending 输入。
|
||||
*/
|
||||
export function assertSafePackageNames(packagesSelect: string): void {
|
||||
if (!PACKAGE_NAMES_RE.test(packagesSelect)) {
|
||||
throw new SshConfigError(
|
||||
`packagesSelect 包含非法字符(仅允许字母/数字/下划线/连字符,空格分隔),已拒绝:${packagesSelect}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造远端编译命令:source TROS 环境 → cd workspace → colcon build。
|
||||
* `2>&1` 合并 stderr 以便统一解析(colcon 的 error 行可能走 stderr)。
|
||||
*
|
||||
* 注入防护(SDB 参数 Gate):`workspaceDir` 走 assertShellSafePath(拒绝 shell
|
||||
* 特殊字符),`packagesSelect` 走白名单 assertSafePackageNames——两者都在命令
|
||||
* 插值点 fail-closed。
|
||||
*/
|
||||
export function colconBuildCommand(workspaceDir: string, packagesSelect?: string): string {
|
||||
assertShellSafePath(workspaceDir, "workspaceDir");
|
||||
if (packagesSelect !== undefined) {
|
||||
assertSafePackageNames(packagesSelect);
|
||||
}
|
||||
const select = packagesSelect ? ` --packages-select ${packagesSelect}` : "";
|
||||
return `${sourceTrosEnvCommand()}; cd "${workspaceDir}" && colcon build${select} 2>&1`;
|
||||
}
|
||||
|
||||
@ -68,17 +68,31 @@ export function parseMd5Sum(stdout: string): string | null {
|
||||
* `stat` run — the overall non-zero exit is surfaced as a verify error.
|
||||
*/
|
||||
export function remoteVerifyCommand(remotePath: string): string {
|
||||
assertShellSafePath(remotePath, "remotePath");
|
||||
return `md5sum "${remotePath}"; stat -c %s "${remotePath}"`;
|
||||
}
|
||||
|
||||
/**
|
||||
* ASCII shell 元字符 + glob 通配符。`remoteVerifyCommand` 用双引号包裹路径,
|
||||
* 这些字符会破坏引号(或将来在非引号上下文触发 glob 展开)——插入前一律拒绝。
|
||||
*/
|
||||
const SHELL_SPECIAL_RE = /["'`$\\;|&<>()*?[\]{}\n\r]/;
|
||||
|
||||
/**
|
||||
* 全角/同形字(shell 元字符的 Unicode lookalike)。bash 不把它们当元字符解析,
|
||||
* 但下游转写或人眼可能混淆——防御性拒绝(fail-closed)。
|
||||
*/
|
||||
const SHELL_LOOKALIKE_RE =
|
||||
/[\u2018\u2019\u201C\u201D\uFF02\uFF40\uFF04\uFF3C\uFF1B\uFF5C\uFF06\uFF1C\uFF1E\uFF08\uFF09]/;
|
||||
|
||||
/**
|
||||
* Reject paths that could break or inject into the remote shell command.
|
||||
* `remoteVerifyCommand` wraps paths in double quotes, so any of these
|
||||
* characters would escape the quoting (poka-yoke, Tool Design principle #5).
|
||||
*/
|
||||
export function assertShellSafePath(path: string, label: string): void {
|
||||
if (/["`$\\;|&<>()\n\r]/.test(path)) {
|
||||
throw new SshConfigError(`${label} 包含 shell 特殊字符(如引号/分号/管道),已拒绝:${path}`);
|
||||
if (SHELL_SPECIAL_RE.test(path) || SHELL_LOOKALIKE_RE.test(path)) {
|
||||
throw new SshConfigError(`${label} 包含 shell 特殊字符(如引号/分号/管道/通配符),已拒绝:${path}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@yusen/dgagent",
|
||||
"version": "2.1.3",
|
||||
"version": "2.1.4",
|
||||
"description": "地瓜机器人 RDK 全栈开发 Agent — 项目创建、ROS2 开发、模型部署、传感器调试一体化 (v1.0: Full SDB Gates + OpenTelemetry + E2E suite)",
|
||||
"keywords": [
|
||||
"pi-package",
|
||||
|
||||
35
tests/e2e/mocked/cli-since.test.ts
Normal file
35
tests/e2e/mocked/cli-since.test.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { spawnSync } from "node:child_process";
|
||||
|
||||
/**
|
||||
* E2E (mocked): dg CLI --since 校验(v2.1.4 加固)。
|
||||
*
|
||||
* 场景:`dg usage --since <非法值>` 必须打印一行清晰错误并退出非零
|
||||
* (无堆栈),而不是静默产出空统计。
|
||||
*/
|
||||
describe("e2e mocked: dg CLI --since validation", () => {
|
||||
function runUsage(args: string[]) {
|
||||
return spawnSync(process.execPath, ["bin/dg.mjs", "usage", ...args], {
|
||||
encoding: "utf8",
|
||||
env: process.env,
|
||||
timeout: 30_000,
|
||||
});
|
||||
}
|
||||
|
||||
it("rejects an invalid --since with a one-line error on stderr and non-zero exit", () => {
|
||||
for (const bad of ["--since 5x", "--since abc", "--since 0d", "--since"]) {
|
||||
const res = runUsage(bad.split(/\s+/));
|
||||
expect(res.status, `dg usage ${bad}`).not.toBe(0);
|
||||
expect(res.stderr, `dg usage ${bad}`).toContain("since");
|
||||
expect(res.stderr, `dg usage ${bad}`).toContain("ISO");
|
||||
// 没有堆栈:一行错误 + 退出码即可
|
||||
expect(res.stderr.split("\n").filter(Boolean), `dg usage ${bad}`).toHaveLength(1);
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps working for a valid --since (Nd)", () => {
|
||||
const res = runUsage(["--since", "7d", "--json"]);
|
||||
expect(res.status).toBe(0);
|
||||
expect(JSON.parse(res.stdout) as { total_calls: number }).toHaveProperty("total_calls");
|
||||
});
|
||||
});
|
||||
@ -25,6 +25,15 @@ export interface MockSftpOptions {
|
||||
pushData?: Buffer;
|
||||
/** Data emitted by `createReadStream`. A Buffer applies to every path; a Record keys by remote path. */
|
||||
pullData?: Buffer | Record<string, Buffer>;
|
||||
/** Simulate a remote read failure: `createReadStream` emits "error" instead of data. */
|
||||
pullError?: Error;
|
||||
/** Simulate a remote write failure: `createWriteStream` emits "error". */
|
||||
pushError?: Error;
|
||||
}
|
||||
|
||||
/** Structural view of MockClient for external event triggers (pool eviction tests). */
|
||||
export interface MockClientLike {
|
||||
handlers: Record<string, Array<(...args: unknown[]) => void>>;
|
||||
}
|
||||
|
||||
export type ConnectFailMode = "auth" | "socket" | null;
|
||||
@ -51,12 +60,14 @@ interface StderrStream {
|
||||
|
||||
interface MockWriteStream {
|
||||
on(event: string, cb?: (err?: Error) => void): MockWriteStream;
|
||||
write(chunk: Buffer): void;
|
||||
write(chunk: Buffer): boolean;
|
||||
end(data?: Buffer | (() => void), cb?: () => void): void;
|
||||
}
|
||||
|
||||
interface MockReadStream {
|
||||
on(event: string, cb: (data?: Buffer) => void): MockReadStream;
|
||||
on(event: string, cb: (arg?: Buffer | Error) => void): MockReadStream;
|
||||
pause(): MockReadStream;
|
||||
resume(): MockReadStream;
|
||||
}
|
||||
|
||||
export const mockSshState: {
|
||||
@ -72,6 +83,11 @@ export const mockSshState: {
|
||||
execError?: Error;
|
||||
pullData?: Buffer | Record<string, Buffer>;
|
||||
pushData?: Buffer;
|
||||
pullError?: Error;
|
||||
pushError?: Error;
|
||||
clients: MockClientLike[];
|
||||
connectBlocked: boolean;
|
||||
pendingReady?: () => void;
|
||||
} = {
|
||||
execHistory: [],
|
||||
pushedFiles: {},
|
||||
@ -80,6 +96,8 @@ export const mockSshState: {
|
||||
exitCode: 0,
|
||||
signal: null,
|
||||
commands: {},
|
||||
clients: [],
|
||||
connectBlocked: false,
|
||||
};
|
||||
|
||||
function resolveCommandResponse(cmd: string): ResolvedCommandResponse {
|
||||
@ -115,6 +133,11 @@ export function resetSshMockState(): void {
|
||||
mockSshState.execError = undefined;
|
||||
mockSshState.pullData = undefined;
|
||||
mockSshState.pushData = undefined;
|
||||
mockSshState.pullError = undefined;
|
||||
mockSshState.pushError = undefined;
|
||||
mockSshState.clients = [];
|
||||
mockSshState.connectBlocked = false;
|
||||
mockSshState.pendingReady = undefined;
|
||||
}
|
||||
|
||||
export function setSshConnectFail(mode: ConnectFailMode): void {
|
||||
@ -132,6 +155,28 @@ export function setSshChannelOpts(opts: MockChannelOptions): void {
|
||||
export function setSshSftpOpts(opts: MockSftpOptions): void {
|
||||
if (opts.pullData !== undefined) mockSshState.pullData = opts.pullData;
|
||||
if (opts.pushData !== undefined) mockSshState.pushData = opts.pushData;
|
||||
if (opts.pullError !== undefined) mockSshState.pullError = opts.pullError;
|
||||
if (opts.pushError !== undefined) mockSshState.pushError = opts.pushError;
|
||||
}
|
||||
|
||||
/** Block "ready" emission so acquire() stays in-flight (pool concurrency tests). */
|
||||
export function setSshConnectBlocked(blocked: boolean): void {
|
||||
mockSshState.connectBlocked = blocked;
|
||||
}
|
||||
|
||||
/** Release a blocked connect: fires the pending "ready" handler. */
|
||||
export function releaseSshConnect(): void {
|
||||
const fn = mockSshState.pendingReady;
|
||||
mockSshState.pendingReady = undefined;
|
||||
if (fn) queueMicrotask(fn);
|
||||
}
|
||||
|
||||
/** Fire a client-level event (close/error) on the Nth-most-recent client (default: last). */
|
||||
export function triggerMockClientEvent(event: "close" | "error", clientIndexFromEnd = 0): void {
|
||||
const idx = mockSshState.clients.length - 1 - clientIndexFromEnd;
|
||||
const client = mockSshState.clients[idx];
|
||||
if (!client) throw new Error(`no mock client at index ${idx}`);
|
||||
for (const h of client.handlers[event] ?? []) queueMicrotask(() => h());
|
||||
}
|
||||
|
||||
export function installSshMock(channelOpts: MockChannelOptions = {}, sftpOpts: MockSftpOptions = {}) {
|
||||
@ -176,11 +221,16 @@ export function installSshMock(channelOpts: MockChannelOptions = {}, sftpOpts: M
|
||||
let captured = mockSshState.pushData;
|
||||
const ws: MockWriteStream = {
|
||||
on(event: string, cb?: (err?: Error) => void) {
|
||||
if (event === "close") queueMicrotask(() => cb?.());
|
||||
if (event === "error" && mockSshState.pushError) {
|
||||
queueMicrotask(() => cb?.(mockSshState.pushError));
|
||||
} else if (event === "close") {
|
||||
queueMicrotask(() => cb?.());
|
||||
}
|
||||
return ws;
|
||||
},
|
||||
write(chunk: Buffer) {
|
||||
captured = captured ? Buffer.concat([captured, chunk]) : Buffer.from(chunk);
|
||||
return true;
|
||||
},
|
||||
end(data?: Buffer | (() => void), cb?: () => void) {
|
||||
const done = typeof data === "function" ? data : cb;
|
||||
@ -194,11 +244,20 @@ export function installSshMock(channelOpts: MockChannelOptions = {}, sftpOpts: M
|
||||
createReadStream(path: string): MockReadStream {
|
||||
const data = resolvePullData(path);
|
||||
const rs: MockReadStream = {
|
||||
on(event: string, cb: (d?: Buffer) => void) {
|
||||
if (event === "data") queueMicrotask(() => cb(Buffer.from(data)));
|
||||
on(event: string, cb: (arg?: Buffer | Error) => void) {
|
||||
if (event === "data" && !mockSshState.pullError) queueMicrotask(() => cb(Buffer.from(data)));
|
||||
if (event === "error" && mockSshState.pullError) {
|
||||
queueMicrotask(() => cb(mockSshState.pullError));
|
||||
}
|
||||
if (event === "close") queueMicrotask(() => cb());
|
||||
return rs;
|
||||
},
|
||||
pause() {
|
||||
return rs;
|
||||
},
|
||||
resume() {
|
||||
return rs;
|
||||
},
|
||||
};
|
||||
return rs;
|
||||
}
|
||||
@ -211,6 +270,9 @@ export function installSshMock(channelOpts: MockChannelOptions = {}, sftpOpts: M
|
||||
|
||||
class MockClient {
|
||||
handlers: Record<string, Array<(...args: unknown[]) => void>> = {};
|
||||
constructor() {
|
||||
mockSshState.clients.push(this);
|
||||
}
|
||||
on(event: string, handler: (...args: unknown[]) => void) {
|
||||
(this.handlers[event] ??= []).push(handler);
|
||||
return this;
|
||||
@ -239,6 +301,8 @@ export function installSshMock(channelOpts: MockChannelOptions = {}, sftpOpts: M
|
||||
level: "client-socket",
|
||||
}),
|
||||
);
|
||||
} else if (mockSshState.connectBlocked) {
|
||||
mockSshState.pendingReady = () => this.handlers.ready?.[0]?.();
|
||||
} else {
|
||||
queueMicrotask(() => this.handlers.ready?.[0]?.());
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { writeFileSync, readFileSync } from "node:fs";
|
||||
import { installSshMock } from "../helpers/mock-ssh.ts";
|
||||
import { installSshMock, setSshConnectFail } from "../helpers/mock-ssh.ts";
|
||||
import { setupJourney } from "../helpers/journey-setup.ts";
|
||||
import { createMockCtx } from "../helpers/mock-pi.ts";
|
||||
import { getToolExecute } from "../helpers/tool-exec.ts";
|
||||
@ -136,4 +136,62 @@ describe("journey 24: dg_workflow_run 执行载体", () => {
|
||||
const details = result.details as { ok: boolean };
|
||||
expect(details.ok).toBe(false);
|
||||
});
|
||||
|
||||
it("ssh_exec failure appends an action=exec_error session log entry", async () => {
|
||||
seed();
|
||||
setSshConnectFail("socket");
|
||||
try {
|
||||
const exec = await getToolExecute(journey.tmp, "dg_ssh_exec");
|
||||
const ctx = createMockCtx({ sessionId: "run-5" });
|
||||
const result = await exec(
|
||||
"call-5",
|
||||
{ board_id: "my-x5", command: "ls /opt", force: false },
|
||||
undefined,
|
||||
undefined,
|
||||
ctx,
|
||||
);
|
||||
// 连接失败以 textResult 如实返回(不吞错),同时落 exec_error 日志
|
||||
expect(result.content[0]!.text).toContain("SSH 连接失败");
|
||||
} finally {
|
||||
setSshConnectFail(null);
|
||||
}
|
||||
// 会话日志必须包含 action=exec_error 的条目(携带 command + board_id)。
|
||||
// 只断言"存在",不数总数——runner 内部审计条目的数量由其他 agent 并行负责。
|
||||
const lines = readFileSync(journey.tmp.sessionLogPath, "utf8").trim().split("\n");
|
||||
const execError = lines
|
||||
.map((l) => JSON.parse(l) as { action: string | null; board_id: string | null; command: string | null })
|
||||
.find((e) => e.action === "exec_error");
|
||||
expect(execError).toBeDefined();
|
||||
expect(execError!.board_id).toBe("my-x5");
|
||||
expect(execError!.command).toBe("ls /opt");
|
||||
});
|
||||
|
||||
it("workflow_run session log entry carries board_id (for /dg-usage --board)", async () => {
|
||||
seed();
|
||||
const exec = await getToolExecute(journey.tmp, "dg_workflow_run");
|
||||
const ctx = createMockCtx({ sessionId: "run-6" });
|
||||
const result = await exec(
|
||||
"call-6",
|
||||
{
|
||||
name: "gen-a1b2c3d4",
|
||||
force: true,
|
||||
board_id: "my-x5",
|
||||
steps: [
|
||||
{ tool: "dg_board_list", args: {} },
|
||||
{ tool: "dg_ssh_exec", args: { command: "ls /opt", board_id: "my-x5" } },
|
||||
],
|
||||
},
|
||||
undefined,
|
||||
undefined,
|
||||
ctx,
|
||||
);
|
||||
expect(result.content[0]!.text).toContain("全部步骤成功");
|
||||
// dg_workflow_run 的 tool_io 条目必须带 board_id,/dg-usage --board 才能过滤到
|
||||
const lines = readFileSync(journey.tmp.sessionLogPath, "utf8").trim().split("\n");
|
||||
const wfRun = lines
|
||||
.map((l) => JSON.parse(l) as { event_type: string; board_id: string | null; tool_io: { tool_name: string } | null })
|
||||
.find((e) => e.event_type === "tool_io" && e.tool_io?.tool_name === "dg_workflow_run");
|
||||
expect(wfRun).toBeDefined();
|
||||
expect(wfRun!.board_id).toBe("my-x5");
|
||||
});
|
||||
});
|
||||
|
||||
133
tests/journeys/25-mode-restore.test.ts
Normal file
133
tests/journeys/25-mode-restore.test.ts
Normal file
@ -0,0 +1,133 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { installBonjourMock } from "../helpers/mock-bonjour.ts";
|
||||
import { installSshMock } from "../helpers/mock-ssh.ts";
|
||||
|
||||
installSshMock();
|
||||
installBonjourMock();
|
||||
|
||||
beforeEach(() => vi.resetModules());
|
||||
|
||||
import { createMockPi, createMockCtx, asExt } from "../helpers/mock-pi.ts";
|
||||
import { loadSystemPrompt, findHandler } from "../helpers/system-prompt-loader.ts";
|
||||
|
||||
/**
|
||||
* Journey 25 (v2.1.4 加固): session_start 模式恢复的容错性。
|
||||
*
|
||||
* 场景:session 文件可能被外部编辑/部分写入——恢复路径必须对损坏条目
|
||||
* fail-closed:跳过(DG_DEBUG 记录)而不是崩溃或恢复垃圾模式;没有合法
|
||||
* 模式条目时回落默认 free。
|
||||
*/
|
||||
describe("journey 25: mode restore robustness (corrupted entries)", () => {
|
||||
async function loadHandlers() {
|
||||
const factory = await loadSystemPrompt();
|
||||
const pi = createMockPi();
|
||||
factory(asExt(pi));
|
||||
const sessionStart = await findHandler(pi, "session_start");
|
||||
const beforeAgent = await findHandler(pi, "before_agent_start");
|
||||
return { pi, sessionStart, beforeAgent };
|
||||
}
|
||||
|
||||
async function modeAfterStart(handlers: { beforeAgent: (event: unknown, ctx: unknown) => unknown }, ctx: unknown): Promise<string> {
|
||||
const result = (await handlers.beforeAgent({ systemPrompt: "base" }, ctx)) as { systemPrompt: string };
|
||||
return result.systemPrompt.includes("**Active mode: beginner**") ? "beginner" : "free";
|
||||
}
|
||||
|
||||
it("skips a latest corrupted mode value and restores the earlier valid entry", async () => {
|
||||
const { sessionStart, beforeAgent } = await loadHandlers();
|
||||
const ctx = createMockCtx({
|
||||
entries: [
|
||||
{ type: "custom", customType: "dg-mode", data: { mode: "beginner", setAt: 1 } },
|
||||
{ type: "custom", customType: "dg-mode", data: { mode: "expert", setAt: 2 } },
|
||||
],
|
||||
});
|
||||
await sessionStart({}, ctx);
|
||||
expect(await modeAfterStart({ beforeAgent }, ctx)).toBe("beginner");
|
||||
});
|
||||
|
||||
it("skips mode entries whose data is not an object and keeps scanning", async () => {
|
||||
const { sessionStart, beforeAgent } = await loadHandlers();
|
||||
for (const badData of ["beginner", 42, ["beginner"], null]) {
|
||||
const ctx = createMockCtx({
|
||||
entries: [
|
||||
{ type: "custom", customType: "dg-mode", data: { mode: "beginner", setAt: 1 } },
|
||||
{ type: "custom", customType: "dg-mode", data: badData },
|
||||
],
|
||||
});
|
||||
await sessionStart({}, ctx);
|
||||
expect(await modeAfterStart({ beforeAgent }, ctx), `data=${JSON.stringify(badData)}`).toBe("beginner");
|
||||
}
|
||||
});
|
||||
|
||||
it("skips a mode entry with no data field and restores the earlier valid one", async () => {
|
||||
const { sessionStart, beforeAgent } = await loadHandlers();
|
||||
const ctx = createMockCtx({
|
||||
entries: [
|
||||
{ type: "custom", customType: "dg-mode", data: { mode: "beginner", setAt: 1 } },
|
||||
{ type: "custom", customType: "dg-mode" },
|
||||
],
|
||||
});
|
||||
await sessionStart({}, ctx);
|
||||
expect(await modeAfterStart({ beforeAgent }, ctx)).toBe("beginner");
|
||||
});
|
||||
|
||||
it("resets to default free when all mode entries are corrupted (not stale mode)", async () => {
|
||||
const { sessionStart, beforeAgent } = await loadHandlers();
|
||||
const goodCtx = createMockCtx({
|
||||
entries: [{ type: "custom", customType: "dg-mode", data: { mode: "beginner", setAt: 1 } }],
|
||||
});
|
||||
await sessionStart({}, goodCtx);
|
||||
expect(await modeAfterStart({ beforeAgent }, goodCtx)).toBe("beginner");
|
||||
|
||||
// 同一进程内第二次 session_start:全是损坏条目 → 必须回落 free,而不是保留上次的 beginner
|
||||
const badCtx = createMockCtx({
|
||||
entries: [{ type: "custom", customType: "dg-mode", data: { mode: "expert", setAt: 2 } }],
|
||||
});
|
||||
await sessionStart({}, badCtx);
|
||||
expect(await modeAfterStart({ beforeAgent }, badCtx)).toBe("free");
|
||||
});
|
||||
|
||||
it("skips null / non-object entries without crashing", async () => {
|
||||
const { sessionStart, beforeAgent } = await loadHandlers();
|
||||
const withValid = createMockCtx({
|
||||
entries: [
|
||||
null,
|
||||
"garbage",
|
||||
{ type: "custom", customType: "dg-mode", data: { mode: "beginner", setAt: 1 } },
|
||||
],
|
||||
});
|
||||
await sessionStart({}, withValid);
|
||||
expect(await modeAfterStart({ beforeAgent }, withValid)).toBe("beginner");
|
||||
|
||||
const allBad = createMockCtx({ entries: [null, "garbage", 42] });
|
||||
await sessionStart({}, allBad);
|
||||
expect(await modeAfterStart({ beforeAgent }, allBad)).toBe("free");
|
||||
});
|
||||
|
||||
it("falls back to free when getEntries() itself throws", async () => {
|
||||
const { sessionStart, beforeAgent } = await loadHandlers();
|
||||
const throwingCtx = {
|
||||
...createMockCtx(),
|
||||
sessionManager: {
|
||||
getSessionId: () => "s",
|
||||
getEntries: () => {
|
||||
throw new Error("session file corrupt");
|
||||
},
|
||||
},
|
||||
};
|
||||
await sessionStart({}, throwingCtx);
|
||||
expect(await modeAfterStart({ beforeAgent }, throwingCtx)).toBe("free");
|
||||
});
|
||||
|
||||
it("falls back to free when getEntries() returns a non-array", async () => {
|
||||
const { sessionStart, beforeAgent } = await loadHandlers();
|
||||
const nonArrayCtx = {
|
||||
...createMockCtx(),
|
||||
sessionManager: {
|
||||
getSessionId: () => "s",
|
||||
getEntries: () => "not-an-array",
|
||||
},
|
||||
};
|
||||
await sessionStart({}, nonArrayCtx);
|
||||
expect(await modeAfterStart({ beforeAgent }, nonArrayCtx)).toBe("free");
|
||||
});
|
||||
});
|
||||
96
tests/journeys/26-usage-since-validation.test.ts
Normal file
96
tests/journeys/26-usage-since-validation.test.ts
Normal file
@ -0,0 +1,96 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { appendFileSync } from "node:fs";
|
||||
import { setupJourney } from "../helpers/journey-setup.ts";
|
||||
import { createMockPi, createMockCtx, asExt } from "../helpers/mock-pi.ts";
|
||||
import { loadSystemPrompt, findCommand } from "../helpers/system-prompt-loader.ts";
|
||||
|
||||
/**
|
||||
* Journey 26 (v2.1.4 加固): /dg-usage 对非法 --since 的 fail-closed 反馈。
|
||||
*
|
||||
* 场景:用户输入 /dg-usage --since 5x 之类无法解析的值时,命令必须给出
|
||||
* 可读错误(列出有效格式),而不是静默产出 NaN/垃圾统计或空结果。
|
||||
*/
|
||||
const journey = setupJourney();
|
||||
|
||||
interface NotifyCall {
|
||||
message: string;
|
||||
type?: "info" | "warning" | "error";
|
||||
}
|
||||
|
||||
async function loadUsageCommand() {
|
||||
const utils = await import("../../extensions/utils.ts");
|
||||
journey.tmp.apply(utils);
|
||||
const factory = await loadSystemPrompt();
|
||||
const pi = createMockPi();
|
||||
factory(asExt(pi));
|
||||
return findCommand(pi, "dg-usage");
|
||||
}
|
||||
|
||||
function writeToolIoEntry(toolName: string): void {
|
||||
const entry = {
|
||||
log_id: `log-${toolName}-${Math.random().toString(36).slice(2)}`,
|
||||
timestamp: new Date().toISOString(),
|
||||
event_type: "tool_io",
|
||||
session_id: "s1",
|
||||
agent_invocation_id: "call-1",
|
||||
board_id: null,
|
||||
command: null,
|
||||
risk_level: null,
|
||||
risk_pattern: null,
|
||||
action: null,
|
||||
exit_code: null,
|
||||
duration_ms: 10,
|
||||
user_confirmed: null,
|
||||
tool_io: { tool_name: toolName, params: {}, result_preview: "", truncated: false, duration_ms: 10 },
|
||||
risk_process: null,
|
||||
user_confirmation: null,
|
||||
};
|
||||
appendFileSync(journey.tmp.sessionLogPath, JSON.stringify(entry) + "\n", { encoding: "utf8" });
|
||||
}
|
||||
|
||||
describe("journey 26: /dg-usage --since 校验", () => {
|
||||
it("rejects an invalid --since with a readable error listing valid formats", async () => {
|
||||
const handler = await loadUsageCommand();
|
||||
const notified: NotifyCall[] = [];
|
||||
const ctx = createMockCtx({
|
||||
ui: { notify: (message: string, type?: "info" | "warning" | "error") => notified.push({ message, type }) },
|
||||
});
|
||||
await handler("--since 5x", ctx);
|
||||
|
||||
expect(notified).toHaveLength(1);
|
||||
expect(notified[0]!.type).toBe("error");
|
||||
expect(notified[0]!.message).toContain("since");
|
||||
expect(notified[0]!.message).toContain("Nd");
|
||||
expect(notified[0]!.message).toContain("ISO");
|
||||
// 错误来源是参数校验,不是“读取统计失败”——前缀不能误导
|
||||
expect(notified[0]!.message).not.toContain("读取使用统计失败");
|
||||
});
|
||||
|
||||
it("rejects a bare --since with no value", async () => {
|
||||
const handler = await loadUsageCommand();
|
||||
const notified: NotifyCall[] = [];
|
||||
const ctx = createMockCtx({
|
||||
ui: { notify: (message: string, type?: "info" | "warning" | "error") => notified.push({ message, type }) },
|
||||
});
|
||||
await handler("--since", ctx);
|
||||
|
||||
expect(notified).toHaveLength(1);
|
||||
expect(notified[0]!.type).toBe("error");
|
||||
expect(notified[0]!.message).toContain("--since");
|
||||
});
|
||||
|
||||
it("still renders a normal report for a valid --since", async () => {
|
||||
writeToolIoEntry("dg_ssh_exec");
|
||||
const handler = await loadUsageCommand();
|
||||
const notified: NotifyCall[] = [];
|
||||
const ctx = createMockCtx({
|
||||
ui: { notify: (message: string, type?: "info" | "warning" | "error") => notified.push({ message, type }) },
|
||||
});
|
||||
await handler("--since 7d", ctx);
|
||||
|
||||
expect(notified).toHaveLength(1);
|
||||
expect(notified[0]!.type).toBe("info");
|
||||
expect(notified[0]!.message).toContain("工具使用统计");
|
||||
expect(notified[0]!.message).toContain("dg_ssh_exec");
|
||||
});
|
||||
});
|
||||
193
tests/unit/build-verify-security.test.ts
Normal file
193
tests/unit/build-verify-security.test.ts
Normal file
@ -0,0 +1,193 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import {
|
||||
colconBuildCommand,
|
||||
assertSafePackageNames,
|
||||
} from "../../lib/workflows/build-verify.ts";
|
||||
import { assertShellSafePath } from "../../lib/workflows/push-and-verify.ts";
|
||||
|
||||
/**
|
||||
* Unit: build-verify 注入防护(review M2)。
|
||||
*
|
||||
* Contract: colconBuildCommand 的每个插值点——workspaceDir(路径,双引号包裹)与
|
||||
* packagesSelect(包名,未加引号直插)——都必须 fail-closed:任何 shell 元字符、
|
||||
* 引号、glob、unicode 同形字都被拒绝并抛清晰错误;合法路径/包名照常通过。
|
||||
*/
|
||||
describe("unit: build-verify — workspaceDir 注入防护", () => {
|
||||
it("rejects shell metacharacter injection in workspaceDir", () => {
|
||||
const attempts = [
|
||||
"/root/rdk_ws; rm -rf /",
|
||||
"/root/rdk_ws && rm -rf /",
|
||||
"/root/rdk_ws | cat /etc/passwd",
|
||||
"/root/rdk`ws`",
|
||||
"/root/$(whoami)/ws",
|
||||
"/root/rdk_ws; $(whoami)",
|
||||
];
|
||||
for (const bad of attempts) {
|
||||
expect(() => colconBuildCommand(bad), bad).toThrow("shell 特殊字符");
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects single and double quotes in workspaceDir", () => {
|
||||
expect(() => colconBuildCommand("/root/rdk'ws")).toThrow("shell 特殊字符");
|
||||
expect(() => colconBuildCommand('/root/rdk"ws')).toThrow("shell 特殊字符");
|
||||
});
|
||||
|
||||
it("rejects newline in workspaceDir", () => {
|
||||
expect(() => colconBuildCommand("/root/rdk_ws\necho pwned")).toThrow("shell 特殊字符");
|
||||
expect(() => colconBuildCommand("/root/rdk_ws\recho pwned")).toThrow("shell 特殊字符");
|
||||
});
|
||||
|
||||
it("rejects glob characters in workspaceDir", () => {
|
||||
for (const bad of ["/root/rdk_ws*", "/root/rdk_ws?", "/root/rdk_[ws]", "/root/rdk_{ws}"]) {
|
||||
expect(() => colconBuildCommand(bad), bad).toThrow("shell 特殊字符");
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects unicode lookalikes of shell metacharacters in workspaceDir", () => {
|
||||
const lookalikes = [
|
||||
"/root/rdk_ws\uFF1B rm -rf /",
|
||||
"/root/rdk_ws\uFF5Ccat /etc/passwd",
|
||||
"/root/rdk\uFF02ws",
|
||||
"/root/rdk\uFF40ws",
|
||||
"/root/rdk_ws\uFF04(ls)",
|
||||
"/root/rdk_ws\uFF06\uFF06 rm",
|
||||
];
|
||||
for (const bad of lookalikes) {
|
||||
expect(() => colconBuildCommand(bad), JSON.stringify(bad)).toThrow("shell 特殊字符");
|
||||
}
|
||||
});
|
||||
|
||||
it("accepts legitimate workspace paths", () => {
|
||||
expect(() => colconBuildCommand("/root/rdk_ws")).not.toThrow();
|
||||
expect(() => colconBuildCommand("/root/rdk_ws/")).not.toThrow();
|
||||
expect(() => colconBuildCommand("/opt/rdk ws/src")).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: build-verify — packagesSelect 注入防护", () => {
|
||||
it("rejects shell metacharacter injection in packagesSelect", () => {
|
||||
const attempts = [
|
||||
"pkg; rm -rf /",
|
||||
"pkg`id`",
|
||||
"$(whoami)",
|
||||
"pkg_a;pkg_b",
|
||||
"pkg|id",
|
||||
"pkg&id",
|
||||
"pkg<id",
|
||||
"pkg>id",
|
||||
];
|
||||
for (const bad of attempts) {
|
||||
expect(() => colconBuildCommand("/root/rdk_ws", bad), bad).toThrow("非法字符");
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects single and double quotes in packagesSelect", () => {
|
||||
expect(() => colconBuildCommand("/root/rdk_ws", "pkg a' b")).toThrow("非法字符");
|
||||
expect(() => colconBuildCommand("/root/rdk_ws", 'pkg a" b')).toThrow("非法字符");
|
||||
});
|
||||
|
||||
it("rejects newline in packagesSelect", () => {
|
||||
expect(() => colconBuildCommand("/root/rdk_ws", "pkg\necho pwned")).toThrow("非法字符");
|
||||
});
|
||||
|
||||
it("rejects glob characters in packagesSelect", () => {
|
||||
for (const bad of ["pkg*", "pkg?", "[pkg]", "pkg{}"]) {
|
||||
expect(() => colconBuildCommand("/root/rdk_ws", bad), bad).toThrow("非法字符");
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects unicode lookalikes in packagesSelect via whitelist", () => {
|
||||
expect(() => assertSafePackageNames("camera\uFF1Bdriver")).toThrow("非法字符");
|
||||
expect(() => assertSafePackageNames("camera\uFF3Cdriver")).toThrow("非法字符");
|
||||
expect(() => assertSafePackageNames("\uFF04(whoami)")).toThrow("非法字符");
|
||||
});
|
||||
|
||||
it("accepts legitimate package names, including multi-package", () => {
|
||||
expect(() => colconBuildCommand("/root/rdk_ws", "camera_driver")).not.toThrow();
|
||||
expect(() => colconBuildCommand("/root/rdk_ws", "pkg_a pkg_b")).not.toThrow();
|
||||
expect(() => assertSafePackageNames("my-pkg_123")).not.toThrow();
|
||||
});
|
||||
|
||||
it("rejects empty or whitespace-only packagesSelect", () => {
|
||||
expect(() => assertSafePackageNames("")).toThrow("非法字符");
|
||||
expect(() => assertSafePackageNames(" ")).toThrow("非法字符");
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: build-verify — assertShellSafePath 共享 guard 加固", () => {
|
||||
it("rejects single quotes, glob chars, and unicode lookalikes", () => {
|
||||
const attempts = [
|
||||
"/opt/a'b.bin",
|
||||
"/opt/a*b.bin",
|
||||
"/opt/a?b.bin",
|
||||
"/opt/a[b].bin",
|
||||
"/opt/a;b.bin",
|
||||
"/opt/a\uFF1Bb.bin",
|
||||
"/opt/a\uFF02b.bin",
|
||||
];
|
||||
for (const bad of attempts) {
|
||||
expect(() => assertShellSafePath(bad, "remotePath"), bad).toThrow("shell 特殊字符");
|
||||
}
|
||||
});
|
||||
|
||||
it("still accepts plain paths", () => {
|
||||
expect(() => assertShellSafePath("/opt/models/model.bin", "remotePath")).not.toThrow();
|
||||
expect(() => assertShellSafePath("/tmp/with space.bin", "remotePath")).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: build-verify — buildVerify 在 SSH 前拦截注入", () => {
|
||||
let tmpDir: string;
|
||||
|
||||
beforeEach(() => {
|
||||
tmpDir = mkdtempSync(join(tmpdir(), "dgagent-test-"));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(tmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const baseBoard = {
|
||||
id: "my-x5",
|
||||
host: "192.168.1.100",
|
||||
port: 22,
|
||||
username: "root",
|
||||
model: "RDK X5",
|
||||
soc: "Sunrise X5",
|
||||
tops: 10,
|
||||
status: "online" as const,
|
||||
discovered_via: "config" as const,
|
||||
};
|
||||
|
||||
it("rejects an unsafe workspaceDir before execOnBoard is called", async () => {
|
||||
vi.resetModules();
|
||||
const execOnBoard = vi.fn();
|
||||
vi.doMock("../../lib/discovery.ts", () => ({
|
||||
discoverBoards: vi.fn().mockResolvedValue({ boards: [baseBoard], warnings: [] }),
|
||||
}));
|
||||
vi.doMock("../../lib/ssh.ts", () => ({
|
||||
execOnBoard,
|
||||
SshConfigError: class extends Error {
|
||||
constructor(msg: string) {
|
||||
super(msg);
|
||||
this.name = "SshConfigError";
|
||||
}
|
||||
},
|
||||
SshExecError: class extends Error {
|
||||
constructor(msg: string) {
|
||||
super(msg);
|
||||
this.name = "SshExecError";
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
const { buildVerify } = await import("../../lib/workflows/build-verify.ts");
|
||||
await expect(
|
||||
buildVerify({ boardId: "my-x5", workspaceDir: "/root/rdk_ws; rm -rf /" }),
|
||||
).rejects.toThrow("shell 特殊字符");
|
||||
expect(execOnBoard).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@ -1,5 +1,6 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { maybeCompact } from "../../lib/compact.ts";
|
||||
import { TRUNCATE_LIMITS } from "../../lib/types.ts";
|
||||
|
||||
/**
|
||||
* Unit: deterministic output compaction (lib/compact.ts).
|
||||
@ -55,3 +56,88 @@ describe("unit: maybeCompact (确定性输出压缩)", () => {
|
||||
expect(r1.keyLines).toEqual(r2.keyLines);
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: maybeCompact 边界 — 空输入 / 单行 / off-by-one", () => {
|
||||
it("empty input is not compacted", () => {
|
||||
const r = maybeCompact("");
|
||||
expect(r.compacted).toBe(false);
|
||||
expect(r.head).toBe("");
|
||||
expect(r.tail).toBe("");
|
||||
expect(r.keyLines).toEqual([]);
|
||||
expect(r.summary).toContain("未超阈值");
|
||||
});
|
||||
|
||||
it("a single long line over maxChars compacts with whole-line head + tail", () => {
|
||||
const r = maybeCompact("a".repeat(5000));
|
||||
expect(r.compacted).toBe(true);
|
||||
expect(r.head).toBe("a".repeat(5000));
|
||||
expect(r.tail).toBe("a".repeat(1500));
|
||||
expect(r.keyLines).toEqual([]);
|
||||
expect(r.originalChars).toBe(5000);
|
||||
});
|
||||
|
||||
it("exactly maxChars (4000) is NOT compacted; one char more compacts", () => {
|
||||
expect(maybeCompact("a".repeat(4000)).compacted).toBe(false);
|
||||
expect(maybeCompact("a".repeat(4001)).compacted).toBe(true);
|
||||
});
|
||||
|
||||
it("exactly maxLines (500) is NOT compacted; one line more compacts", () => {
|
||||
expect(maybeCompact(Array(500).fill("line").join("\n")).compacted).toBe(false);
|
||||
expect(maybeCompact(Array(501).fill("line").join("\n")).compacted).toBe(true);
|
||||
});
|
||||
|
||||
it("respects explicit TRUNCATE_LIMITS-style opts (50000 chars / 2000 lines)", () => {
|
||||
const opts = { maxChars: TRUNCATE_LIMITS.maxBytes, maxLines: TRUNCATE_LIMITS.maxLines };
|
||||
expect(maybeCompact("x".repeat(TRUNCATE_LIMITS.maxBytes), opts).compacted).toBe(false);
|
||||
expect(maybeCompact("x".repeat(TRUNCATE_LIMITS.maxBytes + 1), opts).compacted).toBe(true);
|
||||
expect(maybeCompact(Array(TRUNCATE_LIMITS.maxLines).fill("l").join("\n"), opts).compacted).toBe(false);
|
||||
expect(maybeCompact(Array(TRUNCATE_LIMITS.maxLines + 1).fill("l").join("\n"), opts).compacted).toBe(true);
|
||||
});
|
||||
|
||||
it("a trailing newline adds one phantom line (split semantics)", () => {
|
||||
const lines500 = Array(500).fill("l").join("\n") + "\n";
|
||||
expect(maybeCompact(lines500).originalLines).toBe(501);
|
||||
expect(maybeCompact(lines500).compacted).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: maybeCompact 边界 — 关键行语言覆盖(英文 KEY_LINE_RE 局限,已知待修)", () => {
|
||||
it("does NOT capture Chinese error markers", () => {
|
||||
const r = maybeCompact("编译失败:libfoo 未找到\n初始化异常,已退出\n");
|
||||
expect(r.keyLines).toEqual([]);
|
||||
});
|
||||
|
||||
it("does NOT capture the short 'warn:' form", () => {
|
||||
const r = maybeCompact("warn: deprecated api\n");
|
||||
expect(r.keyLines).toEqual([]);
|
||||
});
|
||||
|
||||
it("captures English markers case-insensitively", () => {
|
||||
const input = ["fatal: boom", "FAILED build", "panic: x", "undefined is not a function", "exception thrown", "warning: hot"].join("\n");
|
||||
const r = maybeCompact(input, { maxChars: 1 }); // 强制压缩以触发关键行提取
|
||||
for (const marker of ["fatal: boom", "FAILED build", "panic: x", "undefined is not a function", "exception thrown", "warning: hot"]) {
|
||||
expect(r.keyLines, marker).toContain(marker);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: maybeCompact 边界 — CJK / 多字节内容", () => {
|
||||
it("counts BMP CJK as one unit each (JS length semantics)", () => {
|
||||
expect(maybeCompact("错误".repeat(2000)).compacted).toBe(false);
|
||||
expect(maybeCompact("错误".repeat(2001)).compacted).toBe(true);
|
||||
expect(maybeCompact("错误".repeat(2001)).originalChars).toBe(4002);
|
||||
});
|
||||
|
||||
it("counts astral chars (emoji) as two UTF-16 units each", () => {
|
||||
expect(maybeCompact("🔥".repeat(2000)).compacted).toBe(false);
|
||||
expect(maybeCompact("🔥".repeat(2001)).compacted).toBe(true);
|
||||
expect(maybeCompact("🔥".repeat(2001)).originalChars).toBe(4002);
|
||||
});
|
||||
|
||||
it("keeps CJK content intact in head and tail when compacted", () => {
|
||||
const r = maybeCompact(Array(600).fill("温度过高,已自动降频").join("\n"));
|
||||
expect(r.compacted).toBe(true);
|
||||
expect(r.head).toContain("温度过高,已自动降频");
|
||||
expect(r.tail).toContain("温度过高,已自动降频");
|
||||
});
|
||||
});
|
||||
|
||||
@ -242,11 +242,11 @@ describe("gate: searchKnowledgeBase — ACL filtering", () => {
|
||||
expect(r.acl).toBe("internal");
|
||||
});
|
||||
|
||||
it("defaults to public when no ACL marker is present (backward compat)", async () => {
|
||||
it("rejects fail-closed when no ACL marker is present (M6: never defaults to public)", async () => {
|
||||
stubFetch("# 普通条目\n无 ACL 标记");
|
||||
const r = await makeClient().searchKnowledgeBase("ros2", "semantic");
|
||||
expect(r.title).toBe("普通条目");
|
||||
expect(r.acl).toBe("public");
|
||||
await expect(makeClient().searchKnowledgeBase("ros2", "semantic")).rejects.toThrow(
|
||||
/ACL 标记.*fail-closed|fail-closed 过滤/,
|
||||
);
|
||||
});
|
||||
|
||||
it("parses the Chinese colon form 'ACL:internal'", async () => {
|
||||
|
||||
231
tests/unit/mcp-client.test.ts
Normal file
231
tests/unit/mcp-client.test.ts
Normal file
@ -0,0 +1,231 @@
|
||||
/**
|
||||
* v2.2 (M6) — MCP client ACL fail-closed 单测。
|
||||
*
|
||||
* 覆盖契约(全部 fail-closed):
|
||||
* - 条目缺少 / 无法解析 ACL → 抛 McpError(MCP_ERR_ACL_UNKNOWN),绝不按 public 放行
|
||||
* - 空内容(服务器明确"无匹配")→ "未找到结果"占位,与"ACL 无法确定"明确区分
|
||||
* - 畸形响应(非对象 JSON / content 非数组 / text 块缺 text / 缺 result/error)
|
||||
* → 类型化 McpError,绝不退化成"空结果"式假成功
|
||||
* - isError=true 的工具错误响应绝不当作正常文章返回
|
||||
* - 传输层错误(HTTP 非 2xx / JSON 解析失败)→ McpConnectionError / McpError
|
||||
* - $DG_MCP_URL 未配置 → createMcpClientFromEnv() 返回 null(工具层触发 labeled mock 兜底)
|
||||
*
|
||||
* 注入方式:与 tests/unit/gates.test.ts 一致——stub 全局 fetch(系统边界),
|
||||
* 客户端真实运行(TESTING.md 原则 #5)。
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, afterEach } from "vitest";
|
||||
import {
|
||||
McpHttpClient,
|
||||
McpError,
|
||||
McpConnectionError,
|
||||
MCP_ERR_ACL_UNKNOWN,
|
||||
MCP_ERR_MALFORMED,
|
||||
createMcpClientFromEnv,
|
||||
} from "../../lib/mcp-client.ts";
|
||||
|
||||
type FetchBody = { method: string; id: number };
|
||||
type Stub = (body: FetchBody) => Response;
|
||||
|
||||
/** 按 method 分发的 JSON-RPC 假服务器。 */
|
||||
function stubFetch(stub: Stub): void {
|
||||
vi.stubGlobal("fetch", async (_url: unknown, init?: RequestInit) => {
|
||||
const body = JSON.parse(init?.body as string) as FetchBody;
|
||||
return stub(body);
|
||||
});
|
||||
}
|
||||
|
||||
function jsonRpcResult(result: unknown): Response {
|
||||
return new Response(JSON.stringify({ jsonrpc: "2.0", id: 1, result }), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
/** 标准初始化 + 自定义 tools/call 结果的假服务器。 */
|
||||
function stubKb(callResult: unknown): void {
|
||||
stubFetch((body) => {
|
||||
if (body.method === "initialize") {
|
||||
return jsonRpcResult({ protocolVersion: "2024-11-05" });
|
||||
}
|
||||
if (body.method === "tools/call") {
|
||||
return jsonRpcResult(callResult);
|
||||
}
|
||||
return jsonRpcResult({});
|
||||
});
|
||||
}
|
||||
|
||||
function makeClient(): McpHttpClient {
|
||||
return new McpHttpClient({ serverUrl: "https://mcp.example.com", token: "t" });
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
delete process.env.DG_MCP_URL;
|
||||
delete process.env.DG_MCP_TOKEN;
|
||||
});
|
||||
|
||||
describe("unit: McpHttpClient.searchKnowledgeBase — ACL fail-closed(M6)", () => {
|
||||
it("缺少 ACL 标记的条目被排除:抛 McpError(MCP_ERR_ACL_UNKNOWN),绝不按 public 放行", async () => {
|
||||
stubKb({ content: [{ type: "text", text: "# 普通条目\n无 ACL 标记" }] });
|
||||
|
||||
await expect(makeClient().searchKnowledgeBase("ros2", "semantic")).rejects.toMatchObject({
|
||||
name: "McpError",
|
||||
code: MCP_ERR_ACL_UNKNOWN,
|
||||
});
|
||||
});
|
||||
|
||||
it("无法解析的 ACL(非法级别)同样被排除,抛 McpError(MCP_ERR_ACL_UNKNOWN)", async () => {
|
||||
stubKb({ content: [{ type: "text", text: "# 机密条目\nACL: secret\n正文" }] });
|
||||
|
||||
await expect(makeClient().searchKnowledgeBase("x5", "keyword")).rejects.toMatchObject({
|
||||
name: "McpError",
|
||||
code: MCP_ERR_ACL_UNKNOWN,
|
||||
});
|
||||
});
|
||||
|
||||
it("空内容 = 服务器明确无匹配 → 返回未找到结果占位(不抛错,与 ACL 无法确定区分)", async () => {
|
||||
stubKb({ content: [] });
|
||||
|
||||
const r = await makeClient().searchKnowledgeBase("ros2", "semantic");
|
||||
expect(r.title).toContain("未找到结果");
|
||||
expect(r.acl).toBe("public");
|
||||
});
|
||||
|
||||
it("ACL 匹配 allowedAcl 的条目正常返回", async () => {
|
||||
stubKb({ content: [{ type: "text", text: "# RDK X5 规格\n\n公开内容\n\nACL: public" }] });
|
||||
|
||||
const r = await makeClient().searchKnowledgeBase("x5", "keyword");
|
||||
expect(r.title).toBe("RDK X5 规格");
|
||||
expect(r.body).toContain("公开内容");
|
||||
expect(r.acl).toBe("public");
|
||||
});
|
||||
|
||||
it("ACL 不匹配 allowedAcl 的条目被替换为无权限占位(不泄露原文)", async () => {
|
||||
stubKb({ content: [{ type: "text", text: "# BPU 内部手册\n机密内容\n\nACL: internal" }] });
|
||||
|
||||
const r = await makeClient().searchKnowledgeBase("bpu", "keyword");
|
||||
expect(r.title).toBe("无权限访问");
|
||||
expect(r.body).not.toContain("机密内容");
|
||||
expect(r.acl).toBe("internal");
|
||||
});
|
||||
|
||||
it("扩展 allowedAcl allowlist 后放行 internal 条目", async () => {
|
||||
stubKb({ content: [{ type: "text", text: "# BPU 内部手册\n机密内容\n\nACL: internal" }] });
|
||||
|
||||
const r = await makeClient().searchKnowledgeBase("bpu", "keyword", [
|
||||
"public",
|
||||
"partner",
|
||||
"internal",
|
||||
]);
|
||||
expect(r.title).toBe("BPU 内部手册");
|
||||
expect(r.acl).toBe("internal");
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: McpHttpClient — 畸形响应 fail-closed(M6)", () => {
|
||||
it("非对象 JSON 响应 → 类型化 McpError(不是 TypeError,也不是空结果)", async () => {
|
||||
stubFetch(() =>
|
||||
new Response(JSON.stringify("hello"), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
}),
|
||||
);
|
||||
|
||||
await expect(makeClient().searchKnowledgeBase("x5", "keyword")).rejects.toMatchObject({
|
||||
name: "McpError",
|
||||
code: MCP_ERR_MALFORMED,
|
||||
});
|
||||
});
|
||||
|
||||
it("不可解析的 JSON 响应 → 类型化 McpError", async () => {
|
||||
stubFetch(() => new Response("not-json-at-all", { status: 200 }));
|
||||
|
||||
await expect(makeClient().searchKnowledgeBase("x5", "keyword")).rejects.toMatchObject({
|
||||
name: "McpError",
|
||||
code: MCP_ERR_MALFORMED,
|
||||
});
|
||||
});
|
||||
|
||||
it("content 不是数组 → 类型化 McpError(原实现会 TypeError 崩溃)", async () => {
|
||||
stubKb({ content: "oops" });
|
||||
|
||||
await expect(makeClient().searchKnowledgeBase("x5", "keyword")).rejects.toMatchObject({
|
||||
name: "McpError",
|
||||
code: MCP_ERR_MALFORMED,
|
||||
});
|
||||
});
|
||||
|
||||
it("text 块缺少 text 字段 → 类型化 McpError(绝不静默丢弃后伪装成无匹配)", async () => {
|
||||
stubKb({ content: [{ type: "text" }] });
|
||||
|
||||
await expect(makeClient().searchKnowledgeBase("x5", "keyword")).rejects.toMatchObject({
|
||||
name: "McpError",
|
||||
code: MCP_ERR_MALFORMED,
|
||||
});
|
||||
});
|
||||
|
||||
it("isError=true 的工具错误响应绝不当作正常文章返回", async () => {
|
||||
stubKb({ content: [{ type: "text", text: "服务器内部错误:索引损坏" }], isError: true });
|
||||
|
||||
await expect(makeClient().searchKnowledgeBase("x5", "keyword")).rejects.toMatchObject({
|
||||
name: "McpError",
|
||||
code: MCP_ERR_MALFORMED,
|
||||
});
|
||||
});
|
||||
|
||||
it("JSON-RPC 错误信封 → 透传服务器 code 与 message", async () => {
|
||||
stubFetch((body) => {
|
||||
if (body.method === "initialize") {
|
||||
return jsonRpcResult({ protocolVersion: "2024-11-05" });
|
||||
}
|
||||
return new Response(
|
||||
JSON.stringify({ jsonrpc: "2.0", id: body.id, error: { code: -32601, message: "method not found" } }),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } },
|
||||
);
|
||||
});
|
||||
|
||||
await expect(makeClient().searchKnowledgeBase("x5", "keyword")).rejects.toMatchObject({
|
||||
name: "McpError",
|
||||
code: -32601,
|
||||
message: expect.stringContaining("method not found"),
|
||||
});
|
||||
});
|
||||
|
||||
it("响应缺少 result/error 字段 → 类型化 McpError(畸形信封不返回 undefined)", async () => {
|
||||
stubFetch((body) => {
|
||||
if (body.method === "initialize") {
|
||||
return jsonRpcResult({ protocolVersion: "2024-11-05" });
|
||||
}
|
||||
return new Response(JSON.stringify({ jsonrpc: "2.0", id: body.id }), {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
});
|
||||
|
||||
await expect(makeClient().searchKnowledgeBase("x5", "keyword")).rejects.toMatchObject({
|
||||
name: "McpError",
|
||||
code: MCP_ERR_MALFORMED,
|
||||
});
|
||||
});
|
||||
|
||||
it("HTTP 非 2xx → McpConnectionError(传输层保持 fail-closed)", async () => {
|
||||
stubFetch(() => new Response("boom", { status: 500 }));
|
||||
|
||||
await expect(makeClient().searchKnowledgeBase("x5", "keyword")).rejects.toBeInstanceOf(
|
||||
McpConnectionError,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: createMcpClientFromEnv — 未配置时的 mock 兜底入口", () => {
|
||||
it("DG_MCP_URL 未配置 → 返回 null(工具层据此走 labeled mock 兜底)", () => {
|
||||
delete process.env.DG_MCP_URL;
|
||||
expect(createMcpClientFromEnv()).toBeNull();
|
||||
});
|
||||
|
||||
it("DG_MCP_URL 配置 → 返回客户端实例", () => {
|
||||
process.env.DG_MCP_URL = "https://mcp.example.com";
|
||||
expect(createMcpClientFromEnv()).toBeInstanceOf(McpHttpClient);
|
||||
});
|
||||
});
|
||||
@ -103,6 +103,42 @@ describe("unit: MCP server — tools/call", () => {
|
||||
expect(parsed[0]!.sequence).toEqual(["dg_board_list", "dg_ssh_exec"]);
|
||||
expect(parsed[0]!.occurrences).toBe(3);
|
||||
});
|
||||
|
||||
it("usage_stats with a valid ISO since filters entries", () => {
|
||||
const res = call("tools/call", {
|
||||
name: "usage_stats",
|
||||
arguments: { since: "2026-08-14T10:01:30Z" },
|
||||
});
|
||||
expect(res.error).toBeUndefined();
|
||||
const text = (res.result as { content: Array<{ text: string }> }).content[0]!.text;
|
||||
const parsed = JSON.parse(text) as { total_calls: number; filtered_entries: number };
|
||||
// fixture 6 条时间戳:10:000 / 10:001 / 10:010 / 10:011 / 10:020 / 10:021 → >= 10:01:30Z 只剩 10:020 / 10:021
|
||||
expect(parsed.total_calls).toBe(2);
|
||||
expect(parsed.filtered_entries).toBe(4);
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: MCP server — usage_stats --since 校验(v2.1.4 fail-closed)", () => {
|
||||
it("returns a clean Invalid params error (-32602) for an invalid since, not a silent empty result", () => {
|
||||
for (const bad of ["5x", "abc", "negative", "2026-99-99"]) {
|
||||
const res = call("tools/call", { name: "usage_stats", arguments: { since: bad } });
|
||||
expect(res.result).toBeUndefined();
|
||||
expect(res.error?.code).toBe(-32602);
|
||||
expect(res.error?.message).toContain("since");
|
||||
expect(res.error?.message).toContain("ISO");
|
||||
}
|
||||
});
|
||||
|
||||
it("returns Invalid params error for a bare empty since string", () => {
|
||||
const res = call("tools/call", { name: "usage_stats", arguments: { since: "" } });
|
||||
expect(res.error?.code).toBe(-32602);
|
||||
});
|
||||
|
||||
it("still aggregates normally when no since is provided", () => {
|
||||
const res = call("tools/call", { name: "usage_stats", arguments: {} });
|
||||
const text = (res.result as { content: Array<{ text: string }> }).content[0]!.text;
|
||||
expect((JSON.parse(text) as { total_calls: number }).total_calls).toBe(6);
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: MCP server — error handling (fail-closed)", () => {
|
||||
|
||||
@ -1,5 +1,12 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { readSessionLogEntries, aggregateUsage, isFailedPreview } from "../../lib/observability.ts";
|
||||
import {
|
||||
readSessionLogEntries,
|
||||
aggregateUsage,
|
||||
isFailedPreview,
|
||||
resolveSince,
|
||||
parseUsageArgs,
|
||||
SinceParseError,
|
||||
} from "../../lib/observability.ts";
|
||||
import type { SessionLogEntry } from "../../lib/types.ts";
|
||||
|
||||
/**
|
||||
@ -9,6 +16,8 @@ import type { SessionLogEntry } from "../../lib/types.ts";
|
||||
* aggregateUsage 只统计 tool_io 事件,按调用次数降序,workflow 子集单独列出;
|
||||
* v1.3 新增:board/since 维度过滤、失败率(isFailedPreview 确定性标记)、
|
||||
* 按天趋势;全部确定性(无 LLM)。
|
||||
* v2.1.4 加固:--since 参数在共享解析点(resolveSince/parseUsageArgs)验证,
|
||||
* 非法输入抛类型化 SinceParseError(fail-closed),不再静默产生 NaN/垃圾过滤。
|
||||
*/
|
||||
|
||||
function toolIoEntry(
|
||||
@ -243,3 +252,93 @@ describe("unit: aggregateUsage v1.3 过滤 + 失败率 + 趋势", () => {
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: resolveSince (v2.1.4 --since 共享解析点验证)", () => {
|
||||
it("rejects garbage values with a SinceParseError listing valid formats", () => {
|
||||
for (const bad of ["5x", "abc", "negative", "-5d", "0d", "0h", "7D", "24H", "2026-99-99", "2026-08-14T25:00:00Z", "7 days", "2w", "1.5d", " 7d "]) {
|
||||
expect(() => resolveSince(bad), `resolveSince(${JSON.stringify(bad)})`).toThrow(SinceParseError);
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects non-ISO-looking strings even when Date.parse would accept them", () => {
|
||||
// "Jan 1 2026" 能被 Date.parse 解析,但不是 ISO 时间戳(字典序比较需要 ISO 形态)
|
||||
expect(() => resolveSince("Jan 1 2026")).toThrow(SinceParseError);
|
||||
expect(() => resolveSince("1/1/2026")).toThrow(SinceParseError);
|
||||
expect(() => resolveSince("0")).toThrow(SinceParseError); // Date.parse("0") 在部分引擎有效,必须拒绝
|
||||
});
|
||||
|
||||
it("rejects empty value", () => {
|
||||
expect(() => resolveSince("")).toThrow(SinceParseError);
|
||||
});
|
||||
|
||||
it("error message mentions Nd/Nh/ISO formats", () => {
|
||||
try {
|
||||
resolveSince("5x");
|
||||
expect.unreachable("resolveSince should have thrown");
|
||||
} catch (err) {
|
||||
const msg = (err as Error).message;
|
||||
expect(msg).toContain("Nd");
|
||||
expect(msg).toContain("Nh");
|
||||
expect(msg).toContain("ISO");
|
||||
}
|
||||
});
|
||||
|
||||
it("resolves Nd to an ISO timestamp approximately N days ago", () => {
|
||||
const now = Date.now();
|
||||
const out = resolveSince("7d");
|
||||
const parsed = Date.parse(out);
|
||||
expect(Number.isNaN(parsed)).toBe(false);
|
||||
const diffDays = (now - parsed) / 86_400_000;
|
||||
expect(diffDays).toBeGreaterThan(6.9);
|
||||
expect(diffDays).toBeLessThan(7.1);
|
||||
});
|
||||
|
||||
it("resolves Nh to an ISO timestamp approximately N hours ago", () => {
|
||||
const now = Date.now();
|
||||
const out = resolveSince("24h");
|
||||
const diffHours = (now - Date.parse(out)) / 3_600_000;
|
||||
expect(diffHours).toBeGreaterThan(23.9);
|
||||
expect(diffHours).toBeLessThan(24.1);
|
||||
});
|
||||
|
||||
it("passes valid ISO timestamps through unchanged", () => {
|
||||
expect(resolveSince("2026-08-14T00:00:00.000Z")).toBe("2026-08-14T00:00:00.000Z");
|
||||
expect(resolveSince("2026-08-14")).toBe("2026-08-14");
|
||||
expect(resolveSince("2026-08-14T08:00:00+08:00")).toBe("2026-08-14T08:00:00+08:00");
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: parseUsageArgs (v2.1.4 --since 参数校验)", () => {
|
||||
it("throws SinceParseError for invalid --since values", () => {
|
||||
for (const args of ["--since 5x", "--since abc", "--since negative", "--since 0d", "--since 2026-99-99"]) {
|
||||
expect(() => parseUsageArgs(args), `parseUsageArgs(${JSON.stringify(args)})`).toThrow(SinceParseError);
|
||||
}
|
||||
});
|
||||
|
||||
it("throws SinceParseError for a bare --since with no value", () => {
|
||||
expect(() => parseUsageArgs("--since")).toThrow(SinceParseError);
|
||||
expect(() => parseUsageArgs("--since --json")).toThrow(SinceParseError);
|
||||
});
|
||||
|
||||
it("parses valid --since values (Nd / Nh / ISO)", () => {
|
||||
const byDay = parseUsageArgs("--since 1d");
|
||||
expect(byDay.filter.since).toBe(resolveSince("1d"));
|
||||
const byHour = parseUsageArgs("--since 24h");
|
||||
expect(byHour.filter.since).toBe(resolveSince("24h"));
|
||||
const iso = parseUsageArgs("--since 2026-08-14T00:00:00.000Z");
|
||||
expect(iso.filter.since).toBe("2026-08-14T00:00:00.000Z");
|
||||
});
|
||||
|
||||
it("keeps --board / --json unaffected by since validation", () => {
|
||||
const opts = parseUsageArgs("--board my-x5 --since 7d --json");
|
||||
expect(opts.filter.boardId).toBe("my-x5");
|
||||
expect(opts.json).toBe(true);
|
||||
expect(opts.filter.since).toBe(resolveSince("7d"));
|
||||
});
|
||||
|
||||
it("ignores unknown flags and empty args as before", () => {
|
||||
expect(parseUsageArgs("").filter.since).toBeUndefined();
|
||||
expect(parseUsageArgs("--json").filter.since).toBeUndefined();
|
||||
expect(parseUsageArgs("--bogus x --since 1h").filter.since).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
178
tests/unit/output-gate.test.ts
Normal file
178
tests/unit/output-gate.test.ts
Normal file
@ -0,0 +1,178 @@
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import {
|
||||
validateExpertType,
|
||||
isValidExpertResponse,
|
||||
isValidRouteDecision,
|
||||
} from "../../lib/schemas.ts";
|
||||
import {
|
||||
dispatchExpert,
|
||||
templateDispatch,
|
||||
modelRoute,
|
||||
generalFallback,
|
||||
} from "../../lib/routing.ts";
|
||||
import type { ExpertInvoker, LlmClassifier } from "../../lib/types.ts";
|
||||
|
||||
/**
|
||||
* 输出 Gate 降级测试(test-gap fill):
|
||||
* 证明 LLM 输出非法时 dispatchExpert/modelRoute 走模板兜底且带标签
|
||||
* (source="template"、summary 前缀 "[模板]"),非法载荷永远不会到达调用方;
|
||||
* 合法输出原样通过;路由决策对未知专家做白名单收编(validateExpertType)。
|
||||
*/
|
||||
|
||||
/** 构造一个绕过 TS 类型约束的 ExpertInvoker——模拟上游返回非法结构的运行时行为。 */
|
||||
function looseInvoker(result: unknown): ExpertInvoker {
|
||||
return { invoke: async () => result } as unknown as ExpertInvoker;
|
||||
}
|
||||
|
||||
/** 构造一个绕过 TS 类型约束的 LlmClassifier——同上。 */
|
||||
function looseClassifier(result: unknown): LlmClassifier {
|
||||
return { classifyIntent: async () => result } as unknown as LlmClassifier;
|
||||
}
|
||||
|
||||
/** 模板兜底的标签契约:source=template + summary 前缀 [模板]。 */
|
||||
function expectTemplateLabeled(r: {
|
||||
source: string;
|
||||
summary: string;
|
||||
raw_notes: string;
|
||||
expert: string;
|
||||
model: string;
|
||||
}): void {
|
||||
expect(r.source).toBe("template");
|
||||
expect(r.summary.startsWith("[模板]")).toBe(true);
|
||||
expect(r.raw_notes).toContain("降级模板");
|
||||
}
|
||||
|
||||
describe("unit: dispatchExpert — 非法 LLM 输出 → 模板兜底(fail-closed)", () => {
|
||||
it("空 summary → 模板兜底并带标签", async () => {
|
||||
const r = await dispatchExpert("perception", "YOLO 推理慢", {
|
||||
invoker: looseInvoker({ content: "", usage: { input: 10, output: 5 }, latency_ms: 120 }),
|
||||
});
|
||||
expectTemplateLabeled(r);
|
||||
expect(r.expert).toBe("perception");
|
||||
expect(r.model).toBe("dg-perception-v1");
|
||||
});
|
||||
|
||||
it("纯空白 summary → 模板兜底", async () => {
|
||||
const r = await dispatchExpert("control", "PID 调参", {
|
||||
invoker: looseInvoker({ content: " \n\t", usage: null, latency_ms: 50 }),
|
||||
});
|
||||
expectTemplateLabeled(r);
|
||||
});
|
||||
|
||||
it("非字符串 summary(数字)→ 模板兜底", async () => {
|
||||
const r = await dispatchExpert("deploy", "部署模型", {
|
||||
invoker: looseInvoker({ content: 42, usage: null, latency_ms: 7 }),
|
||||
});
|
||||
expectTemplateLabeled(r);
|
||||
});
|
||||
|
||||
it("畸形 token_usage(input 为字符串)→ 模板兜底", async () => {
|
||||
const r = await dispatchExpert("perception", "检测任务", {
|
||||
invoker: looseInvoker({ content: "看似正常", usage: { input: "a", output: 5 }, latency_ms: 9 }),
|
||||
});
|
||||
expectTemplateLabeled(r);
|
||||
});
|
||||
|
||||
it("缺失 latency_ms → 模板兜底", async () => {
|
||||
const r = await dispatchExpert("general", "任意任务", {
|
||||
invoker: looseInvoker({ content: "看似正常", usage: null }),
|
||||
});
|
||||
expectTemplateLabeled(r);
|
||||
});
|
||||
|
||||
it("兜底结果绝不携带原始非法载荷", async () => {
|
||||
const raw = { content: " ", usage: { input: "x", output: 1 }, latency_ms: 3 };
|
||||
const r = await dispatchExpert("perception", "任务", { invoker: looseInvoker(raw) });
|
||||
expect(r.summary).not.toBe(raw.content);
|
||||
// 返回给调用方的对象必须再次通过输出 Gate
|
||||
expect(isValidExpertResponse(r)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: dispatchExpert — 合法 LLM 输出原样通过", () => {
|
||||
it("通过 Gate 且内容、用量、延迟原样保留", async () => {
|
||||
const invoker: ExpertInvoker = {
|
||||
invoke: vi.fn().mockResolvedValue({
|
||||
content: "感知专家给出的真实建议。",
|
||||
usage: { input: 64, output: 32 },
|
||||
latency_ms: 200,
|
||||
}),
|
||||
};
|
||||
const r = await dispatchExpert("perception", "YOLO 推理", { invoker });
|
||||
expect(r.source).toBe("llm");
|
||||
expect(r.summary).toBe("感知专家给出的真实建议。");
|
||||
expect(r.token_usage).toEqual({ input: 64, output: 32 });
|
||||
expect(r.latency_ms).toBe(200);
|
||||
expect(r.expert).toBe("perception");
|
||||
expect(isValidExpertResponse(r)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: validateExpertType — 路由决策白名单收编", () => {
|
||||
it("未知专家类型被收编为 general,永不外泄", () => {
|
||||
expect(validateExpertType("hacker")).toBe("general");
|
||||
expect(validateExpertType("vision")).toBe("general");
|
||||
expect(validateExpertType(42)).toBe("general");
|
||||
expect(validateExpertType(null)).toBe("general");
|
||||
});
|
||||
|
||||
it("dispatchExpert 对非法 expert 参数收编后安全派发", async () => {
|
||||
const r = await dispatchExpert("vision" as never, "任意任务");
|
||||
expect(r.expert).toBe("general");
|
||||
expect(r.model).toBe("dg-general-v1");
|
||||
expect(r.source).toBe("template");
|
||||
expect(isValidExpertResponse(r)).toBe(true);
|
||||
});
|
||||
|
||||
it("isValidRouteDecision 拒绝未知专家字段", () => {
|
||||
const base = generalFallback("任务");
|
||||
expect(isValidRouteDecision({ ...base, task_type: "vision" })).toBe(false);
|
||||
expect(isValidRouteDecision({ ...base, recommended_expert: "hacker" })).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: modelRoute — LLM 分类输出经输出 Gate 校验", () => {
|
||||
it("分类返回非法 confidence → 输出 Gate 拦截,回退 general", async () => {
|
||||
const classifier = looseClassifier({
|
||||
task_type: "control",
|
||||
confidence: "very-high",
|
||||
reasoning: "幻觉置信度",
|
||||
});
|
||||
const d = await modelRoute("帮我写一份项目周报", { llmClassifier: classifier });
|
||||
expect(d.task_type).toBe("general");
|
||||
expect(d.confidence).toBe("low");
|
||||
expect(d.source).toBe("fallback");
|
||||
expect(isValidRouteDecision(d)).toBe(true);
|
||||
});
|
||||
|
||||
it("分类返回未知 task_type → 回退 general", async () => {
|
||||
const classifier = looseClassifier({
|
||||
task_type: "vision",
|
||||
confidence: "high",
|
||||
reasoning: "幻觉领域",
|
||||
});
|
||||
const d = await modelRoute("帮我写一份项目周报", { llmClassifier: classifier });
|
||||
expect(d.task_type).toBe("general");
|
||||
expect(d.source).toBe("fallback");
|
||||
});
|
||||
|
||||
it("合法分类输出原样通过输出 Gate", async () => {
|
||||
const classifier = looseClassifier({
|
||||
task_type: "control",
|
||||
confidence: "medium",
|
||||
reasoning: "涉及电机控制",
|
||||
});
|
||||
const d = await modelRoute("帮我写一份项目周报", { llmClassifier: classifier });
|
||||
expect(d.task_type).toBe("control");
|
||||
expect(d.source).toBe("llm_fallback");
|
||||
expect(isValidRouteDecision(d)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: templateDispatch — 兜底产物本身合法", () => {
|
||||
it("模板响应恒通过 isValidExpertResponse", () => {
|
||||
for (const expert of ["perception", "control", "deploy", "general"] as const) {
|
||||
expect(isValidExpertResponse(templateDispatch(expert, "任务"))).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
185
tests/unit/routing-boundary.test.ts
Normal file
185
tests/unit/routing-boundary.test.ts
Normal file
@ -0,0 +1,185 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
routeByRules,
|
||||
routeByKb,
|
||||
keywordMatches,
|
||||
} from "../../lib/routing.ts";
|
||||
import type { KbArticle } from "../../lib/types.ts";
|
||||
|
||||
/**
|
||||
* 词边界(L2)单测:
|
||||
* ASCII 关键词只在"非词字符/字符串边缘"处命中——"oe" 不再误命中
|
||||
* "poet"/"does","pid" 不再误命中 "rapid"/"spider";CJK 关键词保持子串匹配,
|
||||
* "查看摄像头画面" 等中文查询不受影响。
|
||||
*/
|
||||
describe("unit: keywordMatches — ASCII 词边界 + CJK 安全", () => {
|
||||
describe("ASCII 关键词词边界", () => {
|
||||
it("oe 在独立词/字符串边缘命中", () => {
|
||||
expect(keywordMatches("使用 OE 工具链", "oe")).toBe(true);
|
||||
expect(keywordMatches("oe", "oe")).toBe(true);
|
||||
expect(keywordMatches("用 OE", "oe")).toBe(true);
|
||||
});
|
||||
|
||||
it("oe 在英文单词内部不命中(poet/does/shoe/foe)", () => {
|
||||
expect(keywordMatches("write a poem", "oe")).toBe(false);
|
||||
expect(keywordMatches("does it work", "oe")).toBe(false);
|
||||
expect(keywordMatches("shoe", "oe")).toBe(false);
|
||||
expect(keywordMatches("foe", "oe")).toBe(false);
|
||||
});
|
||||
|
||||
it("CJK 字符紧邻 ASCII 关键词时构成边界(使用oe工具链)", () => {
|
||||
expect(keywordMatches("使用oe工具链", "oe")).toBe(true);
|
||||
expect(keywordMatches("用oe编译", "oe")).toBe(true);
|
||||
});
|
||||
|
||||
it("pid 不命中 rapid/spider", () => {
|
||||
expect(keywordMatches("rapid 响应", "pid")).toBe(false);
|
||||
expect(keywordMatches("spider 爬虫", "pid")).toBe(false);
|
||||
expect(keywordMatches("调 PID 参数", "pid")).toBe(true);
|
||||
});
|
||||
|
||||
it("大小写不敏感(DEPLOY/OE)", () => {
|
||||
expect(keywordMatches("Deploy the model with OE", "deploy")).toBe(true);
|
||||
expect(keywordMatches("Deploy the model with OE", "oe")).toBe(true);
|
||||
});
|
||||
|
||||
it("正则元字符关键词先转义再匹配", () => {
|
||||
// "." 若未转义,"yoloX" 会被 `yolo.` 命中(. 匹配任意字符)
|
||||
expect(keywordMatches("yoloX", "yolo.")).toBe(false);
|
||||
expect(keywordMatches("yolo.", "yolo.")).toBe(true);
|
||||
// "+" 若未转义,"pid+" 会变成量词
|
||||
expect(keywordMatches("pidp", "pid+")).toBe(false);
|
||||
expect(keywordMatches("pid+", "pid+")).toBe(true);
|
||||
});
|
||||
|
||||
it("下划线是词字符,yolo_model 不命中 yolo(严格 \b 语义)", () => {
|
||||
expect(keywordMatches("yolo_model", "yolo")).toBe(false);
|
||||
expect(keywordMatches("YOLO_部署", "yolo")).toBe(false);
|
||||
});
|
||||
|
||||
it("版本号后缀不构成边界(YOLOv8 不命中 yolo)", () => {
|
||||
expect(keywordMatches("YOLOv8", "yolo")).toBe(false);
|
||||
expect(keywordMatches("YOLOv8 检测", "yolo")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("CJK 关键词子串匹配不受边界逻辑影响", () => {
|
||||
it("摄像头 命中 查看摄像头画面", () => {
|
||||
expect(keywordMatches("查看摄像头画面", "摄像头")).toBe(true);
|
||||
expect(keywordMatches("查看摄像头画面", "检测")).toBe(false);
|
||||
});
|
||||
|
||||
it("CJK 与 ASCII 混合关键词退回子串匹配", () => {
|
||||
expect(keywordMatches("摄像头YOLO检测", "摄像头yolo")).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: routeByRules — 词边界下的规则路由", () => {
|
||||
it("oe 误命中修复:poet/does/shoe 不再路由到 deploy", () => {
|
||||
expect(routeByRules("帮我写一首诗(poem)")).toBeNull();
|
||||
expect(routeByRules("does this work")).toBeNull();
|
||||
expect(routeByRules("这双 shoe 好看")).toBeNull();
|
||||
});
|
||||
|
||||
it("pid 误命中修复:rapid/spider 不再路由到 control", () => {
|
||||
expect(routeByRules("rapid 响应速度")).toBeNull();
|
||||
expect(routeByRules("spider 爬虫")).toBeNull();
|
||||
expect(routeByRules("用 spider 框架")).toBeNull();
|
||||
});
|
||||
|
||||
it("独立词 oe/pid/deploy 照常命中", () => {
|
||||
const deploy = routeByRules("用 oe 工具链编译模型")!;
|
||||
expect(deploy.task_type).toBe("deploy");
|
||||
|
||||
const pid = routeByRules("给电机调 PID 参数")!;
|
||||
expect(pid.task_type).toBe("control");
|
||||
|
||||
const dep = routeByRules("deploy the model")!;
|
||||
expect(dep.task_type).toBe("deploy");
|
||||
});
|
||||
|
||||
it("deployed 不再命中 deploy(严格词边界)", () => {
|
||||
expect(routeByRules("the model was deployed")).toBeNull();
|
||||
});
|
||||
|
||||
it("YOLOv8 裸词不再命中 yolo(无其他关键词时返回 null)", () => {
|
||||
expect(routeByRules("跑 YOLOv8 模型")).toBeNull();
|
||||
});
|
||||
|
||||
it("词边界不破坏多领域优先级裁决(摄像头 + oe)", () => {
|
||||
const d = routeByRules("用摄像头做 oe 部署")!;
|
||||
expect(d.task_type).toBe("perception");
|
||||
expect(d.matched_rules).toEqual(["perception", "deploy"]);
|
||||
});
|
||||
|
||||
it("camera 独立词命中、cameraless 不命中", () => {
|
||||
const hit = routeByRules("check the camera feed")!;
|
||||
expect(hit.task_type).toBe("perception");
|
||||
expect(routeByRules("cameraless device")).toBeNull();
|
||||
});
|
||||
|
||||
it("大小写混合的 ASCII 关键词照常命中", () => {
|
||||
const d = routeByRules("Deploy the model with OE")!;
|
||||
expect(d.task_type).toBe("deploy");
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: routeByRules — CJK 查询安全", () => {
|
||||
it("查看摄像头画面 路由到 perception", () => {
|
||||
const d = routeByRules("查看摄像头画面")!;
|
||||
expect(d.task_type).toBe("perception");
|
||||
expect(d.matched_rules).toEqual(["perception"]);
|
||||
});
|
||||
|
||||
it("纯中文部署查询 路由到 deploy", () => {
|
||||
const d = routeByRules("把模型部署到板卡")!;
|
||||
expect(d.task_type).toBe("deploy");
|
||||
});
|
||||
|
||||
it("CJK 紧邻 ASCII 关键词:使用oe工具链 路由到 deploy", () => {
|
||||
const d = routeByRules("使用oe工具链编译")!;
|
||||
expect(d.task_type).toBe("deploy");
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: routeByKb — 词边界下的 KB 标题匹配", () => {
|
||||
const deployArticle: KbArticle = {
|
||||
title: "BPU 模型部署流程",
|
||||
body: "# 部署",
|
||||
acl: "public",
|
||||
};
|
||||
const perceptionArticle: KbArticle = {
|
||||
title: "摄像头 YOLO 检测调优",
|
||||
body: "# YOLO",
|
||||
acl: "public",
|
||||
};
|
||||
const oeArticle: KbArticle = {
|
||||
title: "OE 工具链使用指南",
|
||||
body: "# OE",
|
||||
acl: "public",
|
||||
};
|
||||
const poemArticle: KbArticle = {
|
||||
title: "写诗 poet 入门",
|
||||
body: "# poem",
|
||||
acl: "public",
|
||||
};
|
||||
|
||||
it("CJK 标题照常命中(部署/摄像头 YOLO 检测)", () => {
|
||||
expect(routeByKb([deployArticle])!.task_type).toBe("deploy");
|
||||
expect(routeByKb([perceptionArticle])!.task_type).toBe("perception");
|
||||
});
|
||||
|
||||
it("标题内独立 OE 命中 deploy", () => {
|
||||
expect(routeByKb([oeArticle])!.task_type).toBe("deploy");
|
||||
});
|
||||
|
||||
it("标题内嵌 oe 的英文单词不再误命中(poet)", () => {
|
||||
expect(routeByKb([poemArticle])).toBeNull();
|
||||
});
|
||||
|
||||
it("无关标题返回 null(RDK X5 硬件规格)", () => {
|
||||
const unrelated: KbArticle = { title: "RDK X5 硬件规格", body: "# X5", acl: "public" };
|
||||
expect(routeByKb([unrelated])).toBeNull();
|
||||
});
|
||||
});
|
||||
207
tests/unit/runner-audit.test.ts
Normal file
207
tests/unit/runner-audit.test.ts
Normal file
@ -0,0 +1,207 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
||||
import { writeFileSync, readFileSync } from "node:fs";
|
||||
import { installSshMock, setSshConnectFail, resetSshMockState } from "../helpers/mock-ssh.ts";
|
||||
import { runRegisteredWorkflow } from "../../lib/runner.ts";
|
||||
import { boardPool } from "../../lib/ssh.ts";
|
||||
import {
|
||||
registerWorkflow,
|
||||
findWorkflow,
|
||||
_setWorkflowsPath,
|
||||
_setPromoteSafeRuns,
|
||||
_setAutoToObservedRuns,
|
||||
} from "../../lib/registry.ts";
|
||||
import { _setBoardsConfigPath } from "../../lib/discovery.ts";
|
||||
import { _setAuditLogPath, _setSessionLogPath } from "../../lib/risk.ts";
|
||||
import { createTmpFs } from "../helpers/tmp-fs.ts";
|
||||
|
||||
/**
|
||||
* Unit: v2.1.4 M5 — runner 逐命令审计 + board_id 守卫。
|
||||
*
|
||||
* Contract:
|
||||
* - 每个 dg_ssh_exec 步骤产生一条 SDB 审计(rejected/confirmation_required/
|
||||
* executed/executed_with_force/exec_error),形状与 safe-exec.ts / tools.ts 一致。
|
||||
* - 审计写失败 fail-open 透出(step.auditWriteError),不吞命令结果、不抛异常。
|
||||
* - 步骤 board_id 与 workflow board_id 不一致 → 任何步骤执行前抛错(点名步骤)。
|
||||
*/
|
||||
|
||||
installSshMock({ stdout: "ok\n", exitCode: 0 });
|
||||
|
||||
let tmp: ReturnType<typeof createTmpFs>;
|
||||
|
||||
function seedBoard(): void {
|
||||
writeFileSync(
|
||||
tmp.boardsPath,
|
||||
JSON.stringify({ version: 1, boards: [{ id: "my-x5", host: "192.168.1.100", username: "root" }] }),
|
||||
"utf8",
|
||||
);
|
||||
}
|
||||
|
||||
function seedWorkflow(overrides: Partial<Parameters<typeof registerWorkflow>[0]> = {}): void {
|
||||
registerWorkflow({
|
||||
name: "gen-a1b2c3d4",
|
||||
id: "a1b2c3d4",
|
||||
sequence: ["dg_board_list", "dg_ssh_exec"],
|
||||
trust: "auto",
|
||||
source: "generated",
|
||||
createdAt: "2026-08-14T00:00:00Z",
|
||||
safeRuns: 0,
|
||||
generatedFrom: "a1b2c3d4",
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
interface AuditLine {
|
||||
action: string;
|
||||
command: string | null;
|
||||
board_id: string | null;
|
||||
risk_level: string | null;
|
||||
exit_code: number | null;
|
||||
session_id: string | null;
|
||||
}
|
||||
|
||||
function readAuditLog(): AuditLine[] {
|
||||
let raw: string;
|
||||
try {
|
||||
raw = readFileSync(tmp.auditLogPath, "utf8");
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) return [];
|
||||
return trimmed.split("\n").map((l) => JSON.parse(l) as AuditLine);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
resetSshMockState();
|
||||
boardPool.dispose();
|
||||
tmp = createTmpFs();
|
||||
_setBoardsConfigPath(tmp.boardsPath);
|
||||
_setWorkflowsPath(tmp.workflowsPath);
|
||||
_setAuditLogPath(tmp.auditLogPath);
|
||||
_setSessionLogPath(tmp.sessionLogPath);
|
||||
_setPromoteSafeRuns(5);
|
||||
_setAutoToObservedRuns(3);
|
||||
seedBoard();
|
||||
});
|
||||
|
||||
afterEach(() => tmp.cleanup());
|
||||
|
||||
describe("unit: runRegisteredWorkflow — 逐命令审计", () => {
|
||||
it("executed command step writes action=executed audit entry (exit_code/board_id/risk_level/session)", async () => {
|
||||
seedWorkflow();
|
||||
const r = await runRegisteredWorkflow(
|
||||
"gen-a1b2c3d4",
|
||||
[
|
||||
{ tool: "dg_board_list", args: {} },
|
||||
{ tool: "dg_ssh_exec", args: { command: "ls /opt", board_id: "my-x5" } },
|
||||
],
|
||||
{ sessionId: "s-1", agentInvocationId: "inv-1" },
|
||||
);
|
||||
expect(r.ok).toBe(true);
|
||||
const executed = readAuditLog().find((a) => a.command === "ls /opt");
|
||||
expect(executed).toBeDefined();
|
||||
expect(executed!.action).toBe("executed");
|
||||
expect(executed!.board_id).toBe("my-x5");
|
||||
expect(executed!.risk_level).toBe("safe");
|
||||
expect(executed!.exit_code).toBe(0);
|
||||
expect(executed!.session_id).toBe("s-1");
|
||||
});
|
||||
|
||||
it("fatal command step writes action=rejected audit entry", async () => {
|
||||
seedWorkflow();
|
||||
const r = await runRegisteredWorkflow("gen-a1b2c3d4", [
|
||||
{ tool: "dg_board_list", args: {} },
|
||||
{ tool: "dg_ssh_exec", args: { command: "dd if=/dev/zero of=/dev/mmcblk0", board_id: "my-x5" } },
|
||||
]);
|
||||
expect(r.ok).toBe(false);
|
||||
const rejected = readAuditLog().find((a) => a.command === "dd if=/dev/zero of=/dev/mmcblk0");
|
||||
expect(rejected).toBeDefined();
|
||||
expect(rejected!.action).toBe("rejected");
|
||||
expect(rejected!.risk_level).toBe("fatal");
|
||||
expect(rejected!.exit_code).toBeNull();
|
||||
});
|
||||
|
||||
it("warning command step without force writes action=confirmation_required audit entry", async () => {
|
||||
seedWorkflow();
|
||||
const r = await runRegisteredWorkflow("gen-a1b2c3d4", [
|
||||
{ tool: "dg_board_list", args: {} },
|
||||
{ tool: "dg_ssh_exec", args: { command: "reboot", board_id: "my-x5" } },
|
||||
]);
|
||||
expect(r.ok).toBe(false);
|
||||
const confirm = readAuditLog().find((a) => a.command === "reboot");
|
||||
expect(confirm).toBeDefined();
|
||||
expect(confirm!.action).toBe("confirmation_required");
|
||||
expect(confirm!.risk_level).toBe("warning");
|
||||
});
|
||||
|
||||
it("exec failure writes action=exec_error audit entry (command + board_id)", async () => {
|
||||
seedWorkflow();
|
||||
setSshConnectFail("socket");
|
||||
try {
|
||||
const r = await runRegisteredWorkflow(
|
||||
"gen-a1b2c3d4",
|
||||
[
|
||||
{ tool: "dg_board_list", args: {} },
|
||||
{ tool: "dg_ssh_exec", args: { command: "ls /opt", board_id: "my-x5" } },
|
||||
],
|
||||
{ sessionId: "s-4" },
|
||||
);
|
||||
expect(r.ok).toBe(false);
|
||||
} finally {
|
||||
setSshConnectFail(null);
|
||||
}
|
||||
const execError = readAuditLog().find((a) => a.action === "exec_error");
|
||||
expect(execError).toBeDefined();
|
||||
expect(execError!.command).toBe("ls /opt");
|
||||
expect(execError!.board_id).toBe("my-x5");
|
||||
});
|
||||
|
||||
it("audit write failure surfaces as step.auditWriteError and does not throw", async () => {
|
||||
seedWorkflow();
|
||||
_setAuditLogPath(tmp.dir); // 指向目录 → appendFileSync 抛 EISDIR
|
||||
const r = await runRegisteredWorkflow(
|
||||
"gen-a1b2c3d4",
|
||||
[
|
||||
{ tool: "dg_board_list", args: {} },
|
||||
{ tool: "dg_ssh_exec", args: { command: "ls /opt", board_id: "my-x5" } },
|
||||
],
|
||||
{ sessionId: "s-5" },
|
||||
);
|
||||
expect(r.ok).toBe(true);
|
||||
const step = r.steps[1]!;
|
||||
expect(step.auditLogId).toBeNull();
|
||||
expect(step.auditWriteError).toContain("EISDIR");
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: runRegisteredWorkflow — board_id 守卫", () => {
|
||||
it("step board_id mismatch with workflow board_id fails before any step executes", async () => {
|
||||
seedWorkflow();
|
||||
await expect(
|
||||
runRegisteredWorkflow(
|
||||
"gen-a1b2c3d4",
|
||||
[
|
||||
{ tool: "dg_board_list", args: {} },
|
||||
{ tool: "dg_ssh_exec", args: { command: "ls /opt", board_id: "other-board" } },
|
||||
],
|
||||
{ boardId: "my-x5" },
|
||||
),
|
||||
).rejects.toThrow("第 2 步 dg_ssh_exec 的 board_id(other-board)与 workflow board_id(my-x5)不一致");
|
||||
expect(findWorkflow("gen-a1b2c3d4")!.safeRuns).toBe(0);
|
||||
expect(readAuditLog()).toEqual([]);
|
||||
});
|
||||
|
||||
it("matching step board_id with workflow board_id executes normally", async () => {
|
||||
seedWorkflow();
|
||||
const r = await runRegisteredWorkflow(
|
||||
"gen-a1b2c3d4",
|
||||
[
|
||||
{ tool: "dg_board_list", args: {} },
|
||||
{ tool: "dg_ssh_exec", args: { command: "ls /opt", board_id: "my-x5" } },
|
||||
],
|
||||
{ boardId: "my-x5" },
|
||||
);
|
||||
expect(r.ok).toBe(true);
|
||||
expect(r.safeRuns).toBe(1);
|
||||
});
|
||||
});
|
||||
48
tests/unit/sftp-failures.test.ts
Normal file
48
tests/unit/sftp-failures.test.ts
Normal file
@ -0,0 +1,48 @@
|
||||
import { describe, it, expect, beforeEach } from "vitest";
|
||||
import { join } from "node:path";
|
||||
import { writeFileSync } from "node:fs";
|
||||
import { installSshMock, setSshSftpOpts, resetSshMockState } from "../helpers/mock-ssh.ts";
|
||||
import { setupJourney } from "../helpers/journey-setup.ts";
|
||||
import { sftpPush, sftpPull } from "../../lib/ssh.ts";
|
||||
|
||||
/**
|
||||
* Unit: sftpPush / sftpPull 失败传播(v2.1.4 M5)。
|
||||
*
|
||||
* Contract: 注入 mock-ssh 的 pullError / pushError 后,lib/ssh.ts 的
|
||||
* sftpPull / sftpPush 必须以清晰错误 reject(远程读流 / 写流报错不吞、不静默)。
|
||||
* 只针对 lib/ssh.ts 当前行为,不修改其实现。
|
||||
*/
|
||||
|
||||
installSshMock();
|
||||
const journey = setupJourney();
|
||||
|
||||
const board = {
|
||||
id: "my-x5",
|
||||
host: "192.168.1.100",
|
||||
port: 22,
|
||||
username: "root",
|
||||
model: "RDK X5",
|
||||
soc: "Sunrise X5",
|
||||
tops: 10,
|
||||
status: "online" as const,
|
||||
discovered_via: "config" as const,
|
||||
};
|
||||
|
||||
describe("unit: sftpPush / sftpPull 失败传播", () => {
|
||||
beforeEach(() => {
|
||||
resetSshMockState();
|
||||
});
|
||||
|
||||
it("sftpPull rejects with the injected pullError (remote read stream error)", async () => {
|
||||
setSshSftpOpts({ pullError: new Error("remote read boom") });
|
||||
const localFile = join(journey.tmp.dir, "pull-fail.bin");
|
||||
await expect(sftpPull(board, "/var/log/syslog", localFile)).rejects.toThrow("remote read boom");
|
||||
});
|
||||
|
||||
it("sftpPush rejects with the injected pushError (remote write stream error)", async () => {
|
||||
setSshSftpOpts({ pushError: new Error("remote write boom") });
|
||||
const localFile = join(journey.tmp.dir, "push-fail.bin");
|
||||
writeFileSync(localFile, Buffer.from("payload"));
|
||||
await expect(sftpPush(board, localFile, "/opt/models/x.bin")).rejects.toThrow("remote write boom");
|
||||
});
|
||||
});
|
||||
347
tests/unit/ssh-pool.test.ts
Normal file
347
tests/unit/ssh-pool.test.ts
Normal file
@ -0,0 +1,347 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
|
||||
/**
|
||||
* Unit: lib/ssh.ts hardening (v2.1.4 二轮加固).
|
||||
*
|
||||
* 自包含的 ssh2 mock(在系统边界 mock ssh2,内部代码真实运行),额外提供:
|
||||
* - channel.signal() 调用记录(M3 TERM→KILL 观测)
|
||||
* - 手动驱动 channel 的 data/close 事件(M3/M4 确定性控制)
|
||||
* - connect 阻塞/释放(H2 并发冷启动时序控制)
|
||||
* - client.end() 记录(H1 淘汰观测)
|
||||
* - sftp.mkdir 失败注入(L1)
|
||||
*/
|
||||
|
||||
// ── ssh2 mock(vi.mock 提升到 import 之前;工厂内仅在方法体引用 mockState) ──
|
||||
|
||||
interface TestChannel {
|
||||
signalCalls: string[];
|
||||
emitData(d: Buffer): void;
|
||||
emitStderr(d: Buffer): void;
|
||||
emitClose(code: number | null, sig: string | null): void;
|
||||
}
|
||||
|
||||
interface MockState {
|
||||
clients: unknown[];
|
||||
endedClients: unknown[];
|
||||
channels: TestChannel[];
|
||||
connectBlocked: boolean;
|
||||
pendingReady: (() => void) | undefined;
|
||||
execError: Error | undefined;
|
||||
mkdirError: Error | undefined;
|
||||
mkdirs: string[];
|
||||
}
|
||||
|
||||
const mockState: MockState = {
|
||||
clients: [],
|
||||
endedClients: [],
|
||||
channels: [],
|
||||
connectBlocked: false,
|
||||
pendingReady: undefined,
|
||||
execError: undefined,
|
||||
mkdirError: undefined,
|
||||
mkdirs: [],
|
||||
};
|
||||
|
||||
function resetMockState(): void {
|
||||
mockState.clients = [];
|
||||
mockState.endedClients = [];
|
||||
mockState.channels = [];
|
||||
mockState.connectBlocked = false;
|
||||
mockState.pendingReady = undefined;
|
||||
mockState.execError = undefined;
|
||||
mockState.mkdirError = undefined;
|
||||
mockState.mkdirs = [];
|
||||
}
|
||||
|
||||
vi.mock("ssh2", () => {
|
||||
class MockChannel {
|
||||
signalCalls: string[] = [];
|
||||
private dataHandlers: Array<(...args: unknown[]) => void> = [];
|
||||
private stderrHandlers: Array<(...args: unknown[]) => void> = [];
|
||||
private closeHandler: ((...args: unknown[]) => void) | null = null;
|
||||
readonly stderr = {
|
||||
on: (event: string, cb: (...args: unknown[]) => void) => {
|
||||
if (event === "data") this.stderrHandlers.push(cb);
|
||||
return this.stderr;
|
||||
},
|
||||
};
|
||||
on(event: string, cb: (...args: unknown[]) => void): this {
|
||||
if (event === "data") this.dataHandlers.push(cb);
|
||||
else if (event === "close") this.closeHandler = cb;
|
||||
return this;
|
||||
}
|
||||
signal(sig: string): void {
|
||||
this.signalCalls.push(sig);
|
||||
}
|
||||
end(): void {}
|
||||
emitData(d: Buffer): void {
|
||||
for (const h of this.dataHandlers) h(d);
|
||||
}
|
||||
emitStderr(d: Buffer): void {
|
||||
for (const h of this.stderrHandlers) h(d);
|
||||
}
|
||||
emitClose(code: number | null, sig: string | null): void {
|
||||
this.closeHandler?.(code, sig);
|
||||
}
|
||||
}
|
||||
|
||||
class MockSftp {
|
||||
mkdir(path: string, cb: (err: Error | null | undefined) => void): void {
|
||||
mockState.mkdirs.push(path);
|
||||
queueMicrotask(() => cb(mockState.mkdirError ?? null));
|
||||
}
|
||||
end(): void {}
|
||||
}
|
||||
|
||||
class MockClient {
|
||||
handlers: Record<string, Array<(...args: unknown[]) => void>> = {};
|
||||
constructor() {
|
||||
mockState.clients.push(this);
|
||||
}
|
||||
on(event: string, handler: (...args: unknown[]) => void): this {
|
||||
(this.handlers[event] ??= []).push(handler);
|
||||
return this;
|
||||
}
|
||||
removeListener(event: string, handler: (...args: unknown[]) => void): this {
|
||||
const arr = this.handlers[event];
|
||||
if (arr) {
|
||||
const idx = arr.indexOf(handler);
|
||||
if (idx >= 0) arr.splice(idx, 1);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
connect(): this {
|
||||
if (mockState.connectBlocked) {
|
||||
mockState.pendingReady = () => this.handlers.ready?.[0]?.();
|
||||
} else {
|
||||
this.handlers.ready?.[0]?.();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
exec(_cmd: string, cb: (err: Error | undefined, ch: unknown) => void): void {
|
||||
if (mockState.execError) {
|
||||
queueMicrotask(() => cb(mockState.execError, undefined));
|
||||
return;
|
||||
}
|
||||
const ch = new MockChannel();
|
||||
mockState.channels.push(ch);
|
||||
queueMicrotask(() => cb(undefined, ch));
|
||||
}
|
||||
sftp(cb: (err: Error | undefined, sftp: unknown) => void): void {
|
||||
queueMicrotask(() => cb(undefined, new MockSftp()));
|
||||
}
|
||||
end(): void {
|
||||
mockState.endedClients.push(this);
|
||||
}
|
||||
}
|
||||
return { default: { Client: MockClient } };
|
||||
});
|
||||
|
||||
// ── 真实代码(vi.mock 提升后 import 才会拿到 mock) ─────────────────────────
|
||||
|
||||
import { BoardConnectionPool, execOnBoard, sftpPush } from "../../lib/ssh.ts";
|
||||
import { boardPool } from "../../lib/ssh.ts";
|
||||
import type { Board } from "../../lib/types.ts";
|
||||
|
||||
const IDLE_TIMEOUT_MS = 60_000;
|
||||
|
||||
function makeBoard(id = "x5"): Board {
|
||||
return {
|
||||
id,
|
||||
host: "192.168.1.100",
|
||||
port: 22,
|
||||
username: "root",
|
||||
model: "RDK X5",
|
||||
soc: "Sunrise X5",
|
||||
tops: 10,
|
||||
status: "online",
|
||||
discovered_via: "config",
|
||||
};
|
||||
}
|
||||
|
||||
async function flushMicrotasks(): Promise<void> {
|
||||
for (let i = 0; i < 20; i++) await Promise.resolve();
|
||||
}
|
||||
|
||||
let tmpKey: string;
|
||||
let keySeq = 0;
|
||||
|
||||
beforeEach(() => {
|
||||
resetMockState();
|
||||
tmpKey = join(tmpdir(), `dgagent-ssh-key-${process.pid}-${keySeq++}`);
|
||||
writeFileSync(tmpKey, "-----BEGIN RSA PRIVATE KEY-----\ntest-key\n-----END RSA PRIVATE KEY-----");
|
||||
process.env.DG_SSH_KEY = tmpKey;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
boardPool.dispose();
|
||||
delete process.env.DG_SSH_KEY;
|
||||
try {
|
||||
rmSync(tmpKey);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
describe("unit: BoardConnectionPool hardening", () => {
|
||||
describe("H1: idle eviction must skip busy connections", () => {
|
||||
it("destroys an idle entry after the idle timeout", async () => {
|
||||
vi.useFakeTimers();
|
||||
const pool = new BoardConnectionPool();
|
||||
const board = makeBoard();
|
||||
await pool.acquire(board);
|
||||
pool.release(board);
|
||||
expect(mockState.clients).toHaveLength(1);
|
||||
expect(mockState.endedClients).toHaveLength(0);
|
||||
|
||||
vi.advanceTimersByTime(IDLE_TIMEOUT_MS);
|
||||
|
||||
expect(mockState.endedClients).toHaveLength(1);
|
||||
await pool.acquire(board);
|
||||
expect(mockState.clients).toHaveLength(2);
|
||||
pool.dispose();
|
||||
});
|
||||
|
||||
it("keeps a busy entry alive across an eviction sweep", async () => {
|
||||
vi.useFakeTimers();
|
||||
const pool = new BoardConnectionPool();
|
||||
const board = makeBoard();
|
||||
await pool.acquire(board);
|
||||
expect(mockState.clients).toHaveLength(1);
|
||||
|
||||
vi.advanceTimersByTime(IDLE_TIMEOUT_MS);
|
||||
vi.advanceTimersByTime(IDLE_TIMEOUT_MS);
|
||||
|
||||
expect(mockState.endedClients).toHaveLength(0);
|
||||
expect(mockState.clients).toHaveLength(1);
|
||||
|
||||
pool.release(board);
|
||||
vi.advanceTimersByTime(IDLE_TIMEOUT_MS);
|
||||
expect(mockState.endedClients).toHaveLength(1);
|
||||
pool.dispose();
|
||||
});
|
||||
});
|
||||
|
||||
describe("H2: concurrent cold-start dedup", () => {
|
||||
it("N concurrent acquires for one board open exactly one connection", async () => {
|
||||
const pool = new BoardConnectionPool();
|
||||
const board = makeBoard();
|
||||
mockState.connectBlocked = true;
|
||||
const pending = Promise.all([
|
||||
pool.acquire(board),
|
||||
pool.acquire(board),
|
||||
pool.acquire(board),
|
||||
]);
|
||||
mockState.connectBlocked = false;
|
||||
mockState.pendingReady?.();
|
||||
mockState.pendingReady = undefined;
|
||||
|
||||
const clients = await pending;
|
||||
expect(mockState.clients).toHaveLength(1);
|
||||
expect(clients[0]).toBe(clients[1]);
|
||||
expect(clients[1]).toBe(clients[2]);
|
||||
pool.release(board);
|
||||
pool.release(board);
|
||||
pool.release(board);
|
||||
pool.dispose();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: execOnBoard hardening", () => {
|
||||
describe("M3: TERM then KILL, no leaked timers", () => {
|
||||
it("sends TERM at timeout and KILL after the grace period", async () => {
|
||||
vi.useFakeTimers();
|
||||
const board = makeBoard();
|
||||
const p = execOnBoard(board, "hang", 60_000);
|
||||
await flushMicrotasks();
|
||||
expect(mockState.channels).toHaveLength(1);
|
||||
const ch = mockState.channels[0]!;
|
||||
|
||||
vi.advanceTimersByTime(60_000);
|
||||
expect(ch.signalCalls).toEqual(["TERM"]);
|
||||
vi.advanceTimersByTime(2_000);
|
||||
expect(ch.signalCalls).toEqual(["TERM", "KILL"]);
|
||||
|
||||
ch.emitClose(143, "KILL");
|
||||
const r = await p;
|
||||
expect(r.timed_out).toBe(true);
|
||||
expect(r.signal).toBe("KILL");
|
||||
|
||||
vi.advanceTimersByTime(600_000);
|
||||
expect(ch.signalCalls).toEqual(["TERM", "KILL"]);
|
||||
});
|
||||
|
||||
it("normal close clears exec timers so TERM/KILL never fire", async () => {
|
||||
vi.useFakeTimers();
|
||||
const board = makeBoard();
|
||||
const p = execOnBoard(board, "ls", 60_000);
|
||||
await flushMicrotasks();
|
||||
const ch = mockState.channels[0]!;
|
||||
ch.emitData(Buffer.from("ok"));
|
||||
ch.emitClose(0, null);
|
||||
|
||||
const r = await p;
|
||||
expect(r.timed_out).toBe(false);
|
||||
expect(ch.signalCalls).toEqual([]);
|
||||
|
||||
vi.advanceTimersByTime(600_000);
|
||||
expect(ch.signalCalls).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("M4: output backpressure", () => {
|
||||
it("caps stdout at TRUNCATE_LIMITS and sets stdout_truncated", async () => {
|
||||
const board = makeBoard();
|
||||
const p = execOnBoard(board, "big", 60_000);
|
||||
await flushMicrotasks();
|
||||
const ch = mockState.channels[0]!;
|
||||
ch.emitData(Buffer.alloc(30_000, 0x61));
|
||||
ch.emitData(Buffer.alloc(30_000, 0x62));
|
||||
ch.emitData(Buffer.alloc(10_000, 0x63));
|
||||
ch.emitClose(0, null);
|
||||
|
||||
const r = await p;
|
||||
expect(r.stdout.length).toBe(50_000);
|
||||
expect(r.stdout_truncated).toBe(true);
|
||||
expect(r.stderr_truncated).toBe(false);
|
||||
});
|
||||
|
||||
it("caps stderr independently and sets stderr_truncated", async () => {
|
||||
const board = makeBoard();
|
||||
const p = execOnBoard(board, "big-err", 60_000);
|
||||
await flushMicrotasks();
|
||||
const ch = mockState.channels[0]!;
|
||||
ch.emitStderr(Buffer.alloc(60_000, 0x65));
|
||||
ch.emitClose(0, null);
|
||||
|
||||
const r = await p;
|
||||
expect(r.stderr.length).toBe(50_000);
|
||||
expect(r.stderr_truncated).toBe(true);
|
||||
expect(r.stdout_truncated).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit: sftpPush hardening", () => {
|
||||
describe("L1: remote mkdir failure must surface", () => {
|
||||
it("rejects with a clear error when remote mkdir fails", async () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "dgagent-l1-"));
|
||||
const localFile = join(dir, "payload.bin");
|
||||
writeFileSync(localFile, "hello");
|
||||
try {
|
||||
mockState.mkdirError = new Error("Permission denied");
|
||||
const board = makeBoard();
|
||||
await expect(
|
||||
sftpPush(board, localFile, "/opt/sub/dir/payload.bin"),
|
||||
).rejects.toThrow("远程目录创建失败");
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user