Repository navigation
fix(memory): 修复 USER.md 编辑后旧事实回流问题 - #749
openjiuwen-sync-bot[bot] wants to merge 1 commit into
Conversation
修复多 agent 画像副本失效及动态抽取回注问题,并保持存储编辑接口兼容。 Refs: #7124 Issue: https://gitcode.com/openJiuwen/jiuwenswarm/issues/7124
|
ToNy_St4rk 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. |
|
head_sha: 变更摘要本 PR 修复 主要改动
|
|
head_sha: 代码审查我已经提交了所有确认的发现,现在给出结论。 审查结论发现汇总(按优先级)
逐文件审查确认
总体风险判断本次改动方向正确且实现整体自洽:三条根因(同正文多副本未全部失效、读取回显历史失效版本、自动抽取回写旧事实)均有对应修复与回归覆盖,写路径的事务/补偿、scope 隔离、鉴权注入与外部注入拒绝链条完整,未发现正确性、安全或破坏性变更级别的问题。遗留风险集中在规模与可观测性:抽取热路径上的全量扫描、共享编辑时按全库 id 逐个 Scope 点查的可扩展性,以及多 Scope 写入的审计记录不完整——均为可渐进优化项,不阻塞合入。
💬 仅评论 |
| assert shadow.get_units(SCOPE, ["a1"])[0].lifecycle is LifecycleState.ACTIVE | ||
| assert shadow.get_units(sibling, ["a2"])[0].lifecycle is LifecycleState.ACTIVE | ||
| assert "上海" in ds._raw_markdown().read_content(SCOPE, "USER.md") | ||
| assert len(shadow.list_units_by_md(SCOPE, "USER.md")) == 2 |
There was a problem hiding this comment.
head_sha: d4a4ed48d29740b252199272cd93546a4a174ef0
🟡 Medium Priority
变更行:tests/unit/storage/test_composite_domain_store.py:273-274(test_authorized_profile_edit_updates_all_agents_atomically 的 fail_update 分支)。
证据链:
- 第 273 行只断言旧正文 "上海" 仍在 md 中,没有像既有用例第 532 行那样断言新正文 "hello NEW" 不在 md 中。
失效模式:在“正确回滚”的前提下,第 274 行应等价于只剩 a1(长度 1)。断言 len(...) == 2 说明失败后发起者 scope 的 shadow 里多留下了一条记录(很可能是本次新增的多 agent 原子写路径在第二个 scope 抛错后,未删除已为发起者插入的新 unit,只回滚了 lifecycle 与 md)。这意味着:md 已回滚为旧正文、shadow 却残留一条 ACTIVE 的新正文 unit,形成 md/shadow 漂移;同时该断言把这个残留写死成“期望行为”,与文件内既有的“原子回滚后不留新 unit”契约自相矛盾。第 273 行缺少“新正文不得残留”的断言,使得只回滚 md 文本却残留 shadow 记录、或回滚成“旧+新”拼接的实现都能通过本用例。
建议:补上“新正文不得残留”的断言(assert "杭州" not in md_text),并把残留计数断言改成与既有回滚契约一致:失败后该 scope 下仅剩旧 unit a1([uid for uid, _ in shadow.list_units_by_md(SCOPE, "USER.md")] == ["a1"]);若实际残留 2 条,则应在生产侧补齐新 unit 的删除。
There was a problem hiding this comment.
head_sha: d4a4ed48d29740b252199272cd93546a4a174ef0
检视意见不采纳,list_units_by_md 是文件级跨 scope 查询,方法文档明确说明 scope 不参与过滤。因此失败后数量为 2,对应预先写入的旧记录 a1、a2,并非当前 scope 残留了新增记录。将结果改为只包含 a1 会错误排除应当保留的 a2。
生产代码在旧记录更新失败时,已调用 shadow.delete_units(scope, new_unit_ids) 删除新记录,并由外层恢复 Markdown;现有用例同时验证两个旧记录仍为 ACTIVE。没有证据表明存在新增记录残留或回滚缺陷。
|
|
||
| async def edit(context, *_args): | ||
| forwarded.append(context) | ||
| return [] |
There was a problem hiding this comment.
head_sha: d4a4ed48d29740b252199272cd93546a4a174ef0
🟡 Medium Priority
变更行:tests/unit/api/test_local_memory_api_md_ops.py:59-61 把 _commands 替换为 SimpleNamespace(edit_md_file=edit),而 edit 声明为 async def edit(context, *_args);但第 82-84 行(以及 77-79 行)是同步调用 MdOpsMixin._edit_shared_document(...),随后第 85-86 行立即断言 authorized == scopes 和 forwarded[0].extensions[...]。
契约/证据链:同文件既有用例(第 462-473、476-483 行)证明 _commands(MemoryCommandService)层对 API 是同步协议——它们直接 api.edit_md_file(...) 取回 list,而它们注入的 _FakeEngine.edit_md_file 是 async def(第 116 行),说明 async→sync 的桥接发生在被本用例整体替换掉的 MemoryCommandService 里,而不在 mixin 内。
失效模式(两条路径都不成立):
- 若
_edit_shared_document为同步方法(与上面_commands同步协议一致):调用 async 假函数只产生协程对象、函数体不执行,forwarded始终为空 → 第 85 行assert forwarded[0]抛 IndexError(deny 分支下第 76 行pytest.raises也会因未真正执行鉴权而 DID NOT RAISE); - 若
_edit_shared_document是协程:第 77/82 行未asyncio.run/await,协程从未被驱动,authorized为空 → 第 85 行断言失败。
即该用例只有在“假命令被真正执行”时才能通过,而async def假实现在同步调用下不可能被驱动——这一项新回归覆盖实际上是失效的(既可能红,也可能在被人改成list返回后失去对“转发上下文”的验证意义)。
建议:把 edit 的 async def 去掉,改为同步 def edit(context, *_args): forwarded.append(context); return [](与 _commands 的同步协议一致);若确认 _edit_shared_document 是协程,则在两条调用处包 asyncio.run(...)。
There was a problem hiding this comment.
head_sha: d4a4ed48d29740b252199272cd93546a4a174ef0
检视意见不采纳,MemoryCommandService.edit_md_file 本身是 async def,MdOpsMixin._edit_shared_document 中通过 asyncio.run(self._commands.edit_md_file(...)) 执行。因此测试中使用 async def edit(...) 与真实协议一致,协程会被执行,转发上下文断言有效。
拒绝分支的鉴权在调用命令服务前同步执行,也能够正常触发 PermissionDeniedError。改成同步假函数反而会使 asyncio.run 收到普通列表而报错。相关用例已实际运行通过。
Paired: GitHub #749 ↔ GitCode !532
What type of PR is this?
关联 Issue
openJiuwen-ai/jiuwenswarm#12991
本 PR 位于 agent-memory,关联问题位于 jiuwenswarm;请在创建 PR 时通过“关联 Issue / Link issue”建立关联(若支持跨仓库选择)。
问题描述
调用 agent_memory_edit_md_file 修改 USER.md 后,工具返回成功,但后续读取仍可能返回旧事实,表现为新旧事实并存、旧事实重复出现。
原因包括:
修复内容
修改范围
仅修改 agent-memory 的文档读写、普通抽取提交检查及对应测试和规约。
未修改 swarm、core,也未调整写锁、工具协议或其他记忆类别的行为。
验证结果
新增回归覆盖:
/kind
Self-checklist:(请自检,在[ ]内打上x,我们将检视你的完成情况,否则会导致pr无法合入)