Skip to content

fix(subagent): preserve delegated memory tools under PTC root registries - #778

Merged
Grivn merged 4 commits into
mainfrom
fix/issue-777-ptc-root-delegated-tools
Dec 5, 2026
Merged

Grivn merged 4 commits into
mainfrom
fix/issue-777-ptc-root-delegated-tools

Conversation

@Grivn

@Grivn Grivn commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

提交 PR 前请阅读贡献指南、Contributing Guide与开发和验证指南 / Development and Verification Guide。
Before opening a PR, read the Contributing Guide, 贡献指南, and the Development and Verification Guide / 开发和验证指南。
PR 标题和提交信息必须使用 Conventional Commits(type(scope): subject),且不得包含 emoji。 / PR titles and commits must use Conventional Commits (type(scope): subject) and must not contain emoji.
本仓库接受 Bug 修复、兼容性适配、现有能力增强、性能或体验优化和维护类 PR。全新能力、Provider、持久化格式或安全边界变更必须先提 Issue 并获得维护者确认。 / This repository accepts bug fixes, compatibility work, improvements to existing capabilities, performance or UX optimization, and maintenance. New capabilities, Providers, persistence formats, or security-boundary changes require prior maintainer approval in an Issue.
外部贡献者的仅文档类 PR 不直接接受;请先提 Issue 讨论。维护者的发布说明与文档维护不受此限制。 / Documentation-only PRs from external contributors are not accepted directly; open an Issue first. Maintainer release notes and documentation maintenance are exempt.

摘要 / Summary

修复在 DSH 0.2.3(setRoot() 根工具注册表)PTC 预设下,MnemonSubagentCoordinator.availableTools() 调用 parent.ctx.tools.get(name, parent) 将所有记忆工具误滤为空、导致 mnemon_remember 等委派写入与空闲审查子会话只剩 mnemon_subagent_result 且 MNEMON VIEW TOOLS 被清空的问题;同时按父会话的实际绑定展示工具。只读会话可继续检索,跳过写入提示和写入型复盘。权限变化会刷新工具提示。普通 PTC 的委派写入继续使用 DSH 的作用域和执行守卫。

Fixes delegated write and review tool loss under DSH 0.2.3 PTC root tool registries (setRoot()), where parent.ctx.tools.get(name, parent) returns only run_code and undefined for every bound memory tool. MnemonSubagentCoordinator now resolves PTC-root delegated allowlists from the intersection of live Mnemon registrations and the parent's scoped bindings, modelMemoryWake preserves MNEMON VIEW TOOLS listings, usesToolBatchExecution and text-only PTC transport detection recognize setRoot(), and delegated operations fail fast with an actionable error before spawning a child if required memory tools are unavailable.

关联 Issue 或背景 / Related Issue or Context

Fixes #777

在 DSH 0.2.3-alpha.3 中,@deepseek-ai/dsh-tools 用根工具注册表(setRoot())替代了 setExecutor(),其中 @deepseek-ai/dsh-tool-registry-ptc 的 get(name, scope) 仅返回程序入口 run_code,对所有绑定在 PTC SDK 内的注册工具(包括 mnemon_*)一律返回 undefined。原有逻辑出现以下连锁缺陷:

  1. MnemonSubagentCoordinator.availableTools(parent, tools) 用 parent.ctx.tools.get(name, parent) !== undefined 过滤候选工具,结果全部被滤掉,使 allowedTools 退化为 ['mnemon_subagent_result'],进而经 child.ctx.tools.restrict({ allow }) 把所有继承的记忆工具剥离,造成 mnemon_remember 等委派写入恒定失败、空闲审查直接被跳过。
  2. availableMemoryTools(agent) 在 src/host/view-presentation.ts 中同样依赖 tools.get(name, agent),导致 PTC 根下每轮记忆快照里的 MNEMON VIEW TOOLS 与路由提示被清空。
  3. usesToolBatchExecution(tools) 仅识别 setExecutor 而未识别 setRoot。
  4. 当父 Agent 确实未暴露所需记忆工具(或缺少必需写入工具)时,delegate() 仍会启动空工具子会话而非提前给出可操作错误。

关于 #777 中观察到的 cua 出现在子会话声明中的说明:@deepseek-ai/dsh-cua 在 agent/created 时把 cua 注册到子 Agent 自身的 scope 层(DSH 的 tools.restrict() 按设计仅过滤继承层,不影响自身层注册),而 dsh-mnemon 的 childGuard(tools.guard())已在执行边界拦截所有不在 allowedTools 中的非允许工具(包括 cua),本 PR 亦补充回归测试明确验证该拦截。

涉及区域 / Affected Areas

  • Host 激活、Headless 或 bundle / Host activation, Headless, or bundle
  • 运行时记忆 / Runtime Memory
  • 项目档案 / Project Documents
  • 记忆空间或 Provider / Memory Spaces or Providers
  • 子 Agent 或 Agent 工作流 / Subagent or Agent workflow
  • Web UI 或对话交互 / Web UI or conversation interaction
  • 设置、存储或安全 / Settings, storage, or security
  • CLI、RPC、命令或工具 / CLI, RPC, commands, or tools
  • 安装、更新或发布 / Installation, update, or release
  • 测试、构建或文档 / Tests, build, or documentation
  • 其他(请说明)/ Other (explain below)

PR 类型 / PR Type

  • 面向用户的功能或行为变更 / User-facing feature or behavior change
  • Bug 修复 / Bug fix
  • 增强或优化 / Enhancement or optimization
  • 兼容性适配 / Compatibility change
  • 维护或重构 / Maintenance or refactor
  • 测试或构建 / Tests or build

最新代码确认 / Latest Codebase Confirmation

  • 我已基于最新 main 分支开发,或在提交前已 rebase 或合并最新 main。 / I developed from the latest main, or rebased or merged the latest main before submitting.

同步命令 / Sync command:

git fetch origin main && git checkout main && git pull --ff-only origin main && git checkout -b fix/issue-777-ptc-root-delegated-tools origin/main

AI 编码披露 / AI Coding Disclosure

  • 完全 AI 编码:全部编程改动由 AI 产出,并由贡献者接受和审查。 / Fully AI-coded: AI produced all programming changes, which the contributor accepted and reviewed.
  • 部分 AI 辅助:AI 帮助编写或修改了部分内容。 / Partially AI-assisted: AI helped write or modify part of the change.
  • 未使用 AI 编码辅助。 / No AI coding assistance was used.

使用的 AI 模型 / AI model used:

Gemini (original implementation), GPT-6 (maintainer follow-up)

使用的编码 Agent 工具 / Coding Agent tool used:

Antigravity (original implementation), Codex (maintainer follow-up)

仓库规范检查 / Repository Rules

  • 未修改 DSH 官方源码,未让 tsconfig 指向 DSH 源码 checkout,仅使用正式的 @deepseek-ai/* NPM 契约。 / I did not modify DSH source or point tsconfig at a DSH source checkout, and used only published @deepseek-ai/* NPM contracts.
  • Client 与 Host 边界仍以浏览器安全的 src/host/protocol.ts 为准,没有在两侧重复定义 wire DTO。 / The Client and Host boundary still uses browser-safe src/host/protocol.ts as the source for wire DTOs.
  • 持久化格式、RPC 权限、路径或凭据处理的变更包含兼容或拒绝路径、安全分析和相应测试。 / Changes to persistence formats, RPC authority, paths, or credentials include compatibility or rejection paths, security analysis, and tests.
  • 没有提交 token、密钥、私有记忆、未脱敏日志或生成的 lib/ 文件。 / I did not commit tokens, credentials, private memory, unredacted logs, or generated lib/ files.
  • 用户可见文案和长期文档已同步维护中文与英文版本,命令、配置键和路径保持一致。 / User-facing copy and long-lived documentation are synchronized in Chinese and English, with matching commands, configuration keys, and paths.
  • 会改变发布制品或其元数据的 PR 已添加 changeset;仅测试、CI 或站点文档变更可不添加。 / A PR that changes a published artifact or its metadata includes a changeset; test-only, CI-only, and site-documentation-only changes may omit one.
  • 新增和修改的代码、注释、文档、提交信息不含 emoji。 / New and modified code, comments, documentation, and commits contain no emoji.

兼容性与数据安全 / Compatibility and Data Safety

  • DSH contract compatibility (0.2.1 / 0.2.2 / 0.2.3-alpha.3+):
    • DSH 0.2.1 (legacy single-call tools.execute, presentAs) and DSH 0.2.2 (setExecutor batch execution) are completely unchanged because usesRootToolRegistry(tools) checks typeof tools?.setRoot === 'function'.
    • On DSH 0.2.3+ with a Native root (setRoot present, tools.get('run_code', agent) is undefined), isPtcRootRegistry is false, so scoped tools.get(name, agent) filtering and per-agent restriction visibility continue to work identically.
    • On the tested DSH 0.2.3-alpha.3 PTC root (setRoot present and tools.get('run_code', agent) defined), MnemonSubagentCoordinator.availableTools() intersects live registrations with the scoped tool bindings. Mnemon prefers executable(name, agent) when provided, and uses the optional registered(name, agent) lookup present in the tested alpha. When a PTC host provides neither, Mnemon hides unverified tools and skips automatic write reviews. Explicit delegation retains DSH’s normal execution guards.
  • Security boundary preserved (fail-closed):
    • No tool restriction or guard is relaxed. DSH's ToolsService continues to intersect all ancestor scope restrictions on the child, and Mnemon's childGuard continues to block execution of any tool outside allowedTools (including own-scope tools such as cua, and run_code on text-only non-PTC children).
    • Delegated operations that require memory tools now fail fast before spawning a subagent if none of the requested memory tools or no required completion tool (such as mnemon_remember) is available to the parent Agent.
  • Storage / Providers / Configuration / Upgrade / Rollback: No changes to persistence formats, configuration schema, RPC authority, credentials, or Providers. Safe to upgrade and roll back cleanly.

本地验证 / Local Validation

执行的命令 / Commands run:

pnpm vitest run tests/subagent.spec.ts tests/memory-tools.spec.ts -t "#777"
pnpm vitest run tests/subagent.spec.ts tests/memory-tools.spec.ts tests/source-tool-lifecycle.spec.ts tests/source-tool-lifecycle-host.spec.ts
pnpm run verify
node scripts/check-release-intent.mjs --base origin/main
git diff --check origin/main..HEAD

结果摘要 / Result summary:

Maintainer follow-up on 66da7c1f

  • Official npm @deepseek-ai/dsh@0.2.3-alpha.3, Node 24, real PTC preset and scripted offline model. All seven scenarios passed. The restricted parent retained recall, advertised no writers in the current View and prompt, and started zero automatic review children across two eligible turns. Both snapshot modes refreshed when permissions changed. A normal PTC child wrote a synthetic entry into the selected Memory Space and a read-back returned the exact content.
  • Four targeted repository suites passed all 378 tests. Full pnpm run verify passed, including 3,032 passing root tests (22 conditional skips), workspace packages, deterministic builds, Headless, and package validation.

Contributor validation on the earlier head

  • Regression check before fix: pnpm vitest run tests/subagent.spec.ts tests/memory-tools.spec.ts -t "#777" reproduced #777 with 5 expected failures (delegated write allowlist collapsed to ['mnemon_subagent_result'], PTC-root review skipped, missing fail-fast on unavailable write tools, MNEMON VIEW TOOLS dropped under PTC root, and usesToolBatchExecution returning false on setRoot).
  • Targeted tests after fix: tests/subagent.spec.ts (120/120), tests/memory-tools.spec.ts (78/78), tests/source-tool-lifecycle.spec.ts (26/26), and tests/source-tool-lifecycle-host.spec.ts (28/28) — 252/252 passed.
  • pnpm run verify: passed (exit code 0)
    • verify:docs: passed (1047 local links, 313 Markdown anchors, bilingual paths, media hygiene, official plugin coverage)
    • typecheck and pnpm -r build / pnpm -r typecheck / pnpm -r test: passed
    • verify:build: passed (reproducible double build)
    • test:root: 82 passed, 2 skipped (84 files); 3020 passed, 22 skipped (3042 tests) — the 2 skipped files / 22 skipped tests are pre-existing optional-environment skips unchanged by this PR
    • verify:headless: passed (38 total tools, 8 representative Mnemon tools)
    • verify:package: passed (92 packaged files, publint --strict, attw --pack . --profile esm-only)
  • node scripts/check-release-intent.mjs --base origin/main: passed (Verified changeset coverage for dsh-mnemon.)
  • git diff --check origin/main..HEAD: passed

用户可见变更证据 / Local Feature Evidence

证据 / Evidence:

N/A — Host-only subagent tool-availability and DSH 0.2.3 root-tool-registry compatibility fix with no WebUI changes. Behavior across PTC roots, Native roots, dynamic optional-Source tool registration, fail-fast missing-tool errors, childGuard blocking of foreign own-scope tools (such as cua), View tool listings, and setRoot() batch execution is covered in tests/subagent.spec.ts and tests/memory-tools.spec.ts, alongside the existing real-Host suite in tests/source-tool-lifecycle-host.spec.ts.

@yufeng-liang

Copy link
Copy Markdown

Thanks for this fix — I can confirm it resolves the PTC case end-to-end on a real install.

Environment: DSH 0.2.3-alpha.3 on Windows, agent preset ops-ptc (from the dsh-ops plugin), i.e. tools are exposed through a PTC root registry rather than the plain root registry.

Before (dsh-mnemon 0.5.59 and 0.5.60, both unpatched): every mnemon_remember call came back with

{"delegated": true, "provider": "spawn", "action": "skipped", "summary": "... this run only exposed the result channel, no memory-space write tool was provided ..."}

i.e. MnemonSubagentCoordinator.availableTools() filtered the memory tools through parent.ctx.tools.get(name, parent), which under a PTC root registry only reports run_code, so the delegated child was started with mnemon_subagent_result alone. Exactly the root cause this PR describes.

After building this branch (bd9b9a0) and swapping lib/ into node_modules/dsh-mnemon (package.json bumped to 0.5.60 locally only so the two builds were distinguishable):

{"delegated": true, "provider": "spawn", "action": "added", "memoryBodyIds": ["default"]}

The insight was actually persisted, and Mnemon linked it to an existing related entry. The fast-fail path also never triggered, which is the behaviour I'd expect on a healthy install.

One data point for scope: plain 0.5.60 does not fix this. Its only change over 0.5.59 is adding run_code to the allowed set on the review path; availableTools() on the write path is identical, so mnemon_remember still returns skipped there. So this PR is still required, not superseded by the release.

Happy to re-test any follow-up revision on the same setup.

@Grivn
Grivn merged commit a330467 into main Dec 5, 2026
13 checks passed
@Grivn Grivn mentioned this pull request Dec 5, 2026
12 of 28 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: 委派写入通道缺记忆工具 / Delegated write agents get no memory tools, so mnemon_remember always fails

2 participants