Skip to content

fix(memory): align extraction, tool outcomes and bounded recall - #751

Open
openjiuwen-sync-bot[bot] wants to merge 1 commit into
openJiuwen-ai:release/0.2.2from
openjiuwenai:sync/pr-533
Open

openjiuwen-sync-bot[bot] wants to merge 1 commit into
openJiuwen-ai:release/0.2.2from
openjiuwenai:sync/pr-533

Conversation

@openjiuwen-sync-bot

@openjiuwen-sync-bot openjiuwen-sync-bot Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Paired: GitHub #751 ↔ GitCode !533

What type of PR is this?

/kind

Self-checklist:(请自检,在[ ]内打上x,我们将检视你的完成情况,否则会导致pr无法合入)

    • 设计:PR对应的方案是否已经经过Maintainer评审,方案检视意见是否均已答复并完成方案修改
    • 测试:PR中的代码是否已有UT/ST测试用例进行充分的覆盖,新增测试用例是否随本PR一并上库或已经上库
    • 验证:PR描述信息中是否已包含对该PR对应的Feature、Refactor、Bugfix的预期目标达成情况的详细验证结果描述
    • 接口:是否涉及对外接口变更,相应变更已得到接口评审组织的通过,API对应的注释信息已经刷新正确
    • 文档:是否涉及官网文档修改,如果涉及请及时提交资料到Doc仓

Linked Closing Issues:

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


zhengpeng seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


zhengpeng seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@openjiuwen-collaboration-bot

Copy link
Copy Markdown

head_sha: 552ce84d8323025a5c64064fe4189d5b27417675

变更摘要

本 PR 围绕记忆能力的“抽取—工具结果—召回”三条链路做对齐:统一显式记忆工具的返回契约为 success 结构,并把检索/读改故障按调用来源区分处理(显式工具透出安全错误、自动预取按来源降级);为预取注入增加 tail_within_budget 的条目与字符双预算上限;强化 TeamLlmExtractor 的提示词与候选解析策略校验(语言一致、证据逐字、来源 ID 归一化、丢弃计数日志);同时在集体写入链路中传递 request_id 并输出结构化统计日志。

主要改动

  • 工具返回契约统一为 success 结构:handle_tool_call、memory_tools、coding_memory、work_memory、team_memory 中的 {"error": ...}、{"stored": ...}、{"result": ...} 统一改为 {"success": True/False, ...},空结果返回 {"success": True, "results": [], "count": 0},新增 _tool_error_json 辅助函数,调用方无需再区分错误字段与业务字段。
  • 错误传播与降级策略重构:_post_verb 的 raise_on_write_error 重命名为 raise_on_error,新增 _validated_units 校验 add/read_md_file/edit_md_file 的响应结构;search 在熔断、传输失败、非法响应时抛 AgentMemoryClientError 而非返回空列表,显式工具据此返回失败,而 _bounded 包裹的预取保持静默降级;新增 ordinary_request 上下文用于临时解除集合坐标绑定。
  • 召回结果增加有界预算:base_document_scene 新增 tail_within_budget(DEFAULT_INJECT_MAX_ITEMS=20、DEFAULT_INJECT_MAX_CHARS=2000)并过滤凭据条目,_bounded 支持传入 timeout 共享统一截止时间;work_memory 将画像、检索与 MEMORY.md 回退改为 asyncio.gather 并行并在无命中时回退,team_memory 与 coding_memory 的画像/文档注入同样改用统一尾部预算。
  • 团队抽取器候选解析加固:parse_candidates 引入 _MAX_CANDIDATES=32、_VALID_LABELS,用 _strip_source_id_shell 归一化 source_id,将 kind 为标签值时改写为 experience 并回填 label,按 dropped 计数器分类统计丢弃原因,并以 request_id 输出不含明文的摘要日志;提示词新增“内容与标题跟随来源语言、不得翻译”“用户显式要求不要记住/仅本轮保密则跳过”等规则。
  • 集体写入链路与作用域对齐:collective_ops 用 set_request_id/reset_request_id 在阻塞抽取中传递请求 ID,并输出 accepted/written/deduplicated/skipped_member/skipped_destination 计数日志;_HttpClient._scope_payload 在 collective_document 模式下把占位用户(空串、__default__、认证身份同名)映射为 session="";TeamMemoryScene 为无会话隔离的 main_agent 增加普通项目记忆双向召回。

@openjiuwen-collaboration-bot

openjiuwen-collaboration-bot Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

head_sha: 552ce84d8323025a5c64064fe4189d5b27417675

代码审查

审查结论

  • [建议] jiuwen_memory_adapter/jiuwenswarm/base_document_scene.py:199 fix: 恢复补充项目召回的独立超时降级,避免补充检索失败拖垮显式搜索
  • [建议] jiuwen_memory_adapter/jiuwenswarm/agent_memory_provider.py:2843 fix: HTTP 检索整批强校验,单条命中字段缺失即作废全部结果
  • [建议] jiuwen_memory_adapter/jiuwenswarm/work_memory.py:326 fix: work 场景工具返回值新旧格式并存,_procedural/_profile 仍用旧 result/error 结构
  • [建议] tests/unit/jiuwen_memory_adapter/test_agent_memory_provider_unit.py:3826 fix: 用 0.4s/0.5s 竞态超时驱动 prefetch 易假失败
类型 数量
🔴 阻塞 0
🟡 建议 4

💬 仅评论

async def recall():
return await asyncio.wait_for(provider.prefetch("question", project="proj-x"), 0.4)

out_with_proj = asyncio.run(recall())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

head_sha: 552ce84d8323025a5c64064fe4189d5b27417675

🟡 Medium Priority

建议:去掉 0.4s/0.5s 的时间竞赛:让 _FallbackClient.search 不再等待事件(或在需要"搜索挂起"语义时直接 raise TimeoutError),并把外层 asyncio.wait_for(..., 0.4) 换成直接的 await provider.prefetch(...);同时在用例内(asyncio.run 内部)创建 asyncio.Event,避免跨 loop 复用。

if not isinstance(items, list) or any(
not isinstance(item, dict) or not isinstance(item.get("content"), str) for item in items
):
raise AgentMemoryClientError("Memory search returned an invalid response.")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

head_sha: 552ce84d8323025a5c64064fe4189d5b27417675

🟡 Medium Priority

变更点:_HttpClient.search 新增整响应强校验(2844-2847 行)——只要 items 中任意一条不是 dict 或 item.get("content") 不是 str,就抛出 AgentMemoryClientError("Memory search returned an invalid response.");而下方的映射(2848-2856 行)仍用 it.get("content", "") 兜底,说明代码本身就容忍 content 缺失。

受影响契约:HTTP 检索原先只跳过非 dict 项、缺失 content 时回落空串,允许部分命中可用;变更后单条异常字段即可让整批结果作废。

失败模式:当服务端版本返回的命中项不带顶层 content(本模块 docstring 明确说明序列化 MemoryUnit 不含 content 属性、需从 segments 重建,add/read_md_file/edit_md_file 均如此)、或个别命中项字段为 null 时,本次检索整体抛错:显式工具(agent_memory_search / team_memory_search / coding 检索)由“返回其余命中”退化为返回 {"success": false, ...};自动预取(经 _bounded)退化为 0 命中,coding_memory._prefetch 会因此落到 MEMORY.md 文档兜底,整轮语义召回失效。

建议:结构级校验(data 为 dict、items 为 list)保留抛错;条目级校验改为过滤/兜底:跳过非 dict 项,content 缺失时用 self._content_view(it) 从 segments 重建,避免单条异常字段作废整批检索结果。

return json.dumps({
"success": False,
"error": "Memory write failed; check the service logs for details.",
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

head_sha: 552ce84d8323025a5c64064fe4189d5b27417675

🟡 Medium Priority

变更:本 diff 把 work 场景的 _search(296-297)、_conclude(304-306、326-330、338)、_video_submit/_video_status、未知工具与新禁用分支(212、215、223、233)统一改成 {"success": bool, ...} 信封,失败时显式带 success: False。
影响面:WorkMemoryScene.handle_tool_call(199-205)仍同时分发 agent_memory_procedural 与 agent_memory_profile 两个未改动的处理器,它们保持旧结构:_procedural 在参数缺失、写入失败(data is None)、以及 not data.item_id(第 370-376 行注释明确“不可报成功(false success)”)三种失败路径下只返回 {"error": ...};_profile 返回 {"result": ...}。
失败模式:同一场景、同一 dispatch 表内的工具输出契约出现分叉——调用方/模型按新约定读取 result["success"] 时会 KeyError(工作场景写入类工具中只有 procedural 缺该字段),而按旧约定 result 读取 conclude/search 会拿不到值。本 PR 的目标(issue #​367:记忆工具调用失败必须可被感知)对 agent_memory_procedural 的失败路径没有生效,仍与 provider 层 _tool_error_json 的 {"success": False, "error": ...} 约定不一致(agent_memory_provider.py:832-833)。触发条件:显式直调 agent_memory_procedural(未注册进 schema,但 handle_tool_call 仍可达)且写入失败或抽取为空。

建议:将 _procedural(346、361、371-376、377 行)与 _profile(244、255 行)的返回统一改为 {"success": ...} 信封,与本次修改的 _search/_conclude 保持一致;这类失败分支应返回 {"success": False, "error": ...}。

hits = await context.client.search(query, context.scope, top_k=top_k, filters=filters)
if exclude_credentials:
return [hit for hit in hits if not is_credential_item(hit)]
return hits

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

head_sha: 552ce84d8323025a5c64064fe4189d5b27417675

🟡 Medium Priority

变更点:base_document_scene.py:202 删除了 _search_collective_project 内的 cls._bounded(...) 包裹,改为直接 hits = await context.client.search(...);同时 search_project_memory(同文件 141-152 行)改为 asyncio.gather(ordinary, supplement) 且未对 supplement 加 _bounded。

受影响契约:BaseDocumentScene.project_recall() 的调用方依赖“补充召回是可选增强、失败即降级为空列表”。本次同一 PR 又把 _HttpClient.search(raise_on_error=True)与 _InProcessClient.search 改成失败时抛 AgentMemoryClientError(原先返回 []),两条改动叠加后,补充召回从“静默降级”变成“必然抛出”。

失败模式:work_memory.py:273 的 agent_memory_search 工具与 coding_memory.py:157 的显式 search(supplement=True) 都直接走 search_project_memory,其中 supplement(带 collective 坐标的项目召回)没有被 _bounded 保护。当服务端熔断开启、连接失败、超时或返回非法响应时,即使主检索 ordinary 已成功,整个 asyncio.gather 也会抛出异常,导致显式检索工具整体返回失败,而不仅是丢掉补充条目;此前该路径会返回主检索结果。agent_memory_provider.prefetch(1619-1630 行)已在外层补了 _bounded,说明本意是让补充召回可降级,但 search_project_memory 这个共用入口没有同步。

建议:在 _search_collective_project 内把 await context.client.search(...) 重新包回 cls._bounded(..., operation="project search"),或在 search_project_memory 中对 supplement 使用 cls._bounded,保证补充召回失败时降级为空列表而不是让整次检索失败。

@openjiuwen-collaboration-bot

Copy link
Copy Markdown

head_sha: 552ce84d8323025a5c64064fe4189d5b27417675

任务名称 结果 日志操作
静态检查 ❌FAILED 点此跳转
禁用词扫描 ✅SUCCESS 点此跳转
防投毒检查 ✅SUCCESS 点此跳转
开源合规检查 ✅SUCCESS 点此跳转
UT测试 ✅SUCCESS 点此跳转
ST测试 N/A N/A
build 编译包 N/A N/A
ruff codecheck ✅SUCCESS N/A
issue关联检查 ✅SUCCESS N/A
pr size检查 N/A N/A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant