189 lines
11 KiB
Markdown
189 lines
11 KiB
Markdown
# P1.5 · 部门接线 D1/D2 可执行任务卡
|
||
|
||
版本:v1(规划稿;实施会话照卡执行,本会话只维护规划)
|
||
日期:2026-11-24
|
||
基线:v0.10.1(commit 1dfc7c7)+ docs/plans 交接文档 commit(见 HANDOFF 开工核对)。上游方案:DEPARTMENT_PLAN.md、NEXT_PHASE_PLAN.md §4.5。
|
||
所有行号/签名以 2026-11-24 对 src/ 的实测为准;执行前若基线变动先重新核对。
|
||
|
||
## 总览
|
||
|
||
| 卡 | 内容 | 依赖 | 预估 |
|
||
| --- | --- | --- | --- |
|
||
| D1a | kernel 构造 DepartmentManager + routeDeps 暴露 | 无 | 0.5h |
|
||
| D1b | /dsh-brain/departments 管理路由(settings-ui bundle) | D1a | 2–3h |
|
||
| D2a | department_* 工具工厂(6 个工具) | D1a | 2–3h |
|
||
| D2b | memory entry 注册工具 + 开关语义 | D2a | 0.5h |
|
||
| V | verify-departments-wiring.mjs 新增验收 | D1b+D2b | 1–2h |
|
||
|
||
D1a→D1b 与 D1a→D2a 两条链可并行。每卡独立 commit;全部完成后跑 V 卡终验。
|
||
D3(宿主会话挂钩)、D4(设置页页签)不在本轮,见 DEPARTMENT_PLAN §4。
|
||
|
||
---
|
||
|
||
## D1a · kernel 构造 DepartmentManager
|
||
|
||
改动文件:src/kernel.ts(现 324 行)
|
||
|
||
1. import 增加 DepartmentManager:现有行
|
||
`import { LocalMemoryManager, LocalMemoryRemote } from './local-memory/index.ts'`
|
||
改为同时导入 DepartmentManager(index.ts 已导出,无需动 local-memory)。
|
||
2. BrainKernel 类字段区(readonly localMemoryRemote 之后)加:
|
||
`readonly departments: DepartmentManager`
|
||
3. constructor 中 `this.localMemoryRemote = new LocalMemoryRemote(this.localMemory)`
|
||
之后加:`this.departments = new DepartmentManager({ memory: this.localMemory })`
|
||
——共享同一 LocalMemoryManager 实例(库层约定,见 verify-departments.mjs 用法)。
|
||
不做 ensureGlobal 之类的额外初始化:部门私有空间由 create 时懒建。
|
||
4. routeDeps() 返回对象(现有 localMemory: this.localMemory 之后)加:
|
||
`departments: this.departments,`
|
||
5. src/routes.ts 的 RouteDeps 接口加可选字段:
|
||
`/** Department registry (P1.5), when wired in. */ departments?: DepartmentManager`
|
||
(import type 自 ./local-memory/index.ts)。
|
||
|
||
开关语义:DepartmentManager 构造无副作用,常建即可;
|
||
localMemoryEnabled=false 的拒绝发生在路由/工具层(D1b/D2a),不在构造层。
|
||
|
||
验证:npm run typecheck && npm run build;node verify-departments.mjs 保持 21 项全绿。
|
||
commit:feat(departments): construct DepartmentManager in the shared kernel
|
||
|
||
## D1b · /dsh-brain/departments 管理路由
|
||
|
||
改动文件:src/routes.ts(ROUTES 表 + createCoreRoutes)、
|
||
新文件 src/settings-ui/departments-routes.ts(或并入 routes.ts 的 serveDepartments,二选一,建议独立文件对齐 settings-ui/routes.ts 风格)。
|
||
|
||
路由归属:settings-ui bundle(createCoreRoutes 已被 settings-ui entry 注册,
|
||
legacy /cloud-memory 别名自动跟随,无需另接)。
|
||
|
||
1. ROUTES 表加 `departments: '/dsh-brain/departments'`。
|
||
2. createCoreRoutes 数组加一项:
|
||
`{ kind: 'prefix', path: ROUTES.departments, handler: (req,res) => serveDepartments(deps, req, res) }`
|
||
3. serveDepartments 完全仿照 serveLocalMemory 的既有模式:
|
||
- deps.departments === undefined → 503 'departments-unavailable'
|
||
- suffix 解析用 marker '/departments'(同 serveLocalMemory 的 markerAt 写法)
|
||
- 子路由与方法:
|
||
|
||
| suffix | 方法 | 行为 | 错误码 |
|
||
| --- | --- | --- | --- |
|
||
| '' 或 '/' 或 '/list' | GET | list(scope?),scope 取 query(global/project) | 405 |
|
||
| '/create' | POST | create(input);name 必填 trim 非空 | 400 name-required / 403 origin-rejected |
|
||
| '/update' | POST | update(id, patch, expectedRevision?) | 400 id-required / 409 revision-conflict(库层抛错映射) |
|
||
| '/disable' | POST | disable(id, expectedRevision?) | 同上 |
|
||
| '/checkpoint' | POST | checkpoint(id, {currentTask?, handoff?}, expectedRevision?) | 同上 |
|
||
| '/recall' | GET | recall(id, query, k?, workspace?);k clamp 1..5(默认 5) | 400 id-required |
|
||
| '/resume' | POST | resumeSession(id, host?);attach 语义(hostSessionId)同 POST 传入 | 400 id-required |
|
||
|
||
- 所有 POST:sameOrigin(req) 闸门(复用 src/http.ts 现有函数)→ readBody → JSON.parse
|
||
→ 对象校验,失败 400 invalid-json / invalid-body(照抄 serveLocalMemory '/forget' 段)。
|
||
- localMemoryEnabled=false 时统一 503 'local-memory-disabled'(读 settings 判定,
|
||
与工具层口径一致)。
|
||
- 库层错误消息(如 revision 冲突、department-not-found、disabled)用 try/catch 映射:
|
||
已知 message → 409/404/400;未知 → 500,不泄内部栈。
|
||
4. 响应投影:Department 对象直接 JSON 化即可(role 上限 12000 字符仅存储用,
|
||
管理路由是同源页面消费,允许全文;agent 工具侧才做精简投影,见 D2a)。
|
||
|
||
验证:typecheck + build;手工 curl 或 V 卡脚本覆盖:GET list 200、POST create 同源 200/
|
||
跨源 403、缺 name 400、revision 过期 409、禁用后 503。
|
||
commit:feat(departments): management routes under the settings-ui bundle
|
||
|
||
## D2a · department_* 工具工厂
|
||
|
||
新文件:src/memory/departments-tools.ts(对齐 memory/tools.ts 的手写 ToolDefinition 风格,
|
||
不用 defineTool——该文件头注释已说明双模块实例风险,遵守)。
|
||
|
||
导出:`export function createDepartmentTools(deps: DepartmentToolDeps): ToolDefinitionLike[]`
|
||
|
||
DepartmentToolDeps 接口(kernel.toolDeps() 扩展或独立工厂均可,建议独立):
|
||
```ts
|
||
interface DepartmentToolDeps {
|
||
departments: () => DepartmentManager
|
||
settings: () => { localMemoryEnabled: boolean; memoryEnabled: boolean }
|
||
workspace: (exec?: unknown) => string | undefined
|
||
}
|
||
```
|
||
kernel.ts 加 departmentToolDeps() 方法返回上述形状(departments: () => this.departments)。
|
||
|
||
六个工具(名字即 agent 面 API,描述必须声明私有空间语义):
|
||
|
||
| 工具 | 参数(JSON Schema) | 行为 |
|
||
| --- | --- | --- |
|
||
| department_create | name*, role?, scope?(global/project), skills?[], allowedTools?[], readGlobal?, readProject? | create;返回 {id,name,scope,created};重名幂等不覆盖 |
|
||
| department_list | scope? | list;返回精简视图 {id,name,scope,active,currentTask?,revision}(不含 role 全文) |
|
||
| department_remember | departmentId*, content*, category?, tags?[] | remember;返回 {id,duplicate};秘密拒收错误原样透传 |
|
||
| department_recall | departmentId*, query*, k?(1..5 默认 5) | recall(id,query,k,workspace(exec));返回 hits[{origin,content snippet≤600,id}](复用 memory/tools.ts 的 snippet 手法) |
|
||
| department_checkpoint | departmentId*, currentTask?, handoff?, expectedRevision? | checkpoint;返回 {revision,updatedAt} |
|
||
| department_resume | departmentId*, host?, hostSessionId? | resumeSession 或 attachSession(给了 hostSessionId 走 attach);返回 sessionRef |
|
||
|
||
共同契约(照抄 memory/tools.ts 既有约定):
|
||
- execute 抛错 → 宿主转 isError:true;错误消息中文可行动(参照 requireMemoryToken 文案风格)。
|
||
- 前置检查:memoryEnabled=false 或 localMemoryEnabled=false → 抛
|
||
'部门功能当前已禁用…请在 /dsh-brain 页面开启';部门 active=false → 库层已拒绝,透传。
|
||
- 输出统一 renderJson(从 memory/tools.ts 导出复用,已是 export)。
|
||
- workspace 解析:execute(args, exec) 的 exec 经 deps.workspace(exec)(kernel 已有
|
||
workspaceFromExec,勿重造)。
|
||
- recall 的 k 上限 5 硬 clamp,防上下文膨胀(DEPARTMENT_PLAN §5 边界)。
|
||
|
||
验证:typecheck + build;V 卡覆盖工具行为。
|
||
commit:feat(departments): six department_* agent tools
|
||
|
||
## D2b · memory entry 注册
|
||
|
||
改动文件:src/entries/memory.ts(现 134 行)
|
||
|
||
在现有 createMemoryTools 注册循环之后(同一 ctx.inject(['tools']) 回调内)加:
|
||
```ts
|
||
for (const tool of createDepartmentTools(kernel.departmentToolDeps())) {
|
||
toolsCtx.effect(() => tools.register(tool), `dsh-brain-memory: tool \${tool.name}`)
|
||
}
|
||
```
|
||
(模板字符串反引号按正常 TS 写,此处为文档转义。)
|
||
|
||
要点:
|
||
- 工具随 memory bundle 开关整体注册/卸载——与五个 memory_* 工具同生命周期,
|
||
不新增 bundle、不动 inject 声明。
|
||
- systemPrompt 投影段(order 151)本轮不加部门内容(D3 范围)。
|
||
- 自动总结监听器不动。
|
||
|
||
验证:typecheck + build + node verify-plugin-entry.mjs(入口形状不变);
|
||
V 卡验证注册名单含 6 个 department_*。
|
||
commit:feat(departments): register department tools in the memory entry
|
||
|
||
## V · verify-departments-wiring.mjs
|
||
|
||
新文件:仓库根 verify-departments-wiring.mjs(P1 整理前的出生位置;
|
||
若执行时 REPO_CLEANUP_PLAN 已先行,则直接写 tests/verification/ 并改 ../../lib 引用)。
|
||
|
||
harness 风格照抄 verify-departments.mjs:mkdtemp 假 DSH_HOME + project、
|
||
check/throws 计数、动态 import('./lib/...')、结尾汇总 exit code。
|
||
|
||
覆盖清单(对应 DEPARTMENT_PLAN §6 验收):
|
||
1. 工具注册:resetBrainKernel() 后 brainKernel(config,logger),
|
||
createDepartmentTools(kernel.departmentToolDeps()) 长度 6、名字齐全、
|
||
parameters 为合法 JSON Schema 对象。
|
||
2. 工具行为闭环:create→remember→recall(私有命中 origin='department')→
|
||
checkpoint→resume 返回 reusable 引用;k>5 被 clamp。
|
||
3. 开关行为:localMemoryEnabled=false 时 execute 抛禁用错误;
|
||
memoryEnabled=false 同理。
|
||
4. 禁用部门:disable 后 remember/recall 抛错(库层语义透传)。
|
||
5. 路由鉴权:起真实 http server 挂 createCoreRoutes(kernel.routeDeps())
|
||
(参考 verify-brain-panel.mjs 的起服务手法):
|
||
GET list 200;POST create 无 Origin 闸门票据 → 403;同源 → 200;
|
||
缺 name → 400;expectedRevision 过期 → 409;未构造 departments → 503。
|
||
6. legacy 别名:/cloud-memory/departments/list 同样可达。
|
||
7. 环境性跳过口径:无真实宿主依赖,本脚本应 0 SKIP。
|
||
|
||
终验命令(全部通过才算 P1.5 D1+D2 完成):
|
||
```
|
||
npm run typecheck && npm run build
|
||
node scripts/verify-all.mjs # 原 38 过 + 2 环境跳过不变,新增 wiring 全绿
|
||
node verify-plugin-entry.mjs # 入口形状
|
||
```
|
||
commit:test(departments): wiring verification for tools and routes
|
||
|
||
## 边界提醒(执行者必读)
|
||
|
||
- 不改 src/local-memory/departments/ 库层任何字节——21 项验收是既成事实,
|
||
接线只消费其公开 API。
|
||
- 不动 cloud-sync:部门私有空间默认不上云(M4 再议)。
|
||
- 不引新依赖;手写 ToolDefinition,不 import @deepseek-ai/dsh-tools。
|
||
- lib/ 产物由 npm run build 生成后随卡提交(仓库约定 lib 入库)。
|
||
- 每卡 commit 前跑该卡验证命令;V 卡终验后才向用户报"最小可用闭环完成"。
|