Skip to content

fix(memory): 支持默认模型热更新并透传模型服务错误 - #748

Open
openjiuwen-sync-bot[bot] wants to merge 1 commit into
openJiuwen-ai:mem2.0from
openjiuwenai:sync/pr-531
Open

openjiuwen-sync-bot[bot] wants to merge 1 commit into
openJiuwen-ai:mem2.0from
openjiuwenai:sync/pr-531

Conversation

@openjiuwen-sync-bot

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

Copy link
Copy Markdown
Contributor

Paired: GitHub #748 ↔ GitCode !531

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.
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.
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: df505f2ea3bd080d985e5eed291284d79561baa4

变更摘要

本 PR 主要围绕内存模块的模型服务调用做两类改动:一是新增 LLMServiceError 并在 LLM 调用层统一包装模型服务连接失败与 HTTP 错误,通过入口层映射为 502/504 并透传给上层 SDK;二是为受管配置增加默认模型热更新能力,通过 ManagedConfigService.update_defaults 与 ManagedConfiguration.update_defaults 在不落库默认凭据的前提下切换模型连接,同时保留用户显式覆盖并固定已继承的完整端点。

主要改动

  • 新增模型服务错误类型并统一出口映射:在 jiuwen_memory/common/errors.py 增加 LLMServiceError,根据上游状态码生成可公开的安全消息;jiuwen_memory_entry/core/error_response.py 与 handler.py 将其映射为 HTTP 502,LLMTimeoutError 映射为 504,并在 jiuwen_memory/api/__init__.py 导出两个异常。
  • OpenAILLM 透传模型服务错误:chat 捕获 openai.APIConnectionError 与 openai.APIStatusError,分别转换为无状态码和携带 status_code 的 LLMServiceError,避免将上游响应正文写入错误信息。
  • 支持默认模型热更新:新增 ManagedConfigService.update_defaults,校验变更必须构成完整端点、不得包含锁定字段,更新宿主默认值且不写入默认凭据,并通过 ManagedConfiguration.update_defaults 以 write 权限暴露给可信宿主,不作为 HTTP/Portal 用户配置入口。
  • 固定显式改选后的完整模型连接:save 流程引入 _pin_inherited_endpoints,对已从宿主继承的模型实例,在用户显式修改任一连接字段时补齐完整端点,避免后续宿主默认值变化与用户覆盖混用凭据,并新增“完整端点不可持久化”“恢复与编辑不可同时进行”的校验。
  • 适配层透传模型服务错误:agent_memory_provider.py 的 HTTP 客户端识别 LLMServiceError/LLMTimeoutError 错误体并通过 safe_error_message 转为 AgentMemoryClientError,进程内客户端同样捕获这两类异常,使模型服务错误能到达工具层且不泄露内部细节。

@openjiuwen-collaboration-bot

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

Copy link
Copy Markdown

head_sha: df505f2ea3bd080d985e5eed291284d79561baa4

代码审查

审查结论

本次共报告 5 个问题:P0 0 个、P1 0 个、P2 1 个、P3 4 个。

整体风险判断:改动方向正确(模型服务错误分类化 + 保留用户显式覆盖的默认模型热更新),HTTP/SDK 两侧的错误映射与脱敏链路基本自洽,测试也覆盖了关键路径,未发现会导致构建失败、数据损坏或凭据泄漏的严重缺陷。主要风险集中在新增的 update_defaults / _pin_inherited_endpoints 这一侧:它在失败回滚、与既有 save() 路径的校验强度对齐、以及“继承钉住”对不可写字段的处理上存在缺口(P2 一个状态回滚问题 + P3 三处一致性/可用性问题),另外新增的 LLMServiceError 未接入构建层重试分类。

逐文件审查确认

文件 结果
docs/features/F05-personal-memory-portal.md no issues(未引入不安全命令/配置;与实现语义一致)
docs/specs/S07-common.md no issues(502/504 与脱敏描述与代码一致)
docs/specs/S08-config.md no issues(update_defaults(changes, security=...) 签名、进程内限定、不新增 HTTP 接口均与实现一致)
jiuwen_memory/api/__init__.py no issues(新增导出与 __all__ 同步)
jiuwen_memory/api/managed_configuration.py no issues(复用 _check(security, "write"),未暴露为 HTTP 入口)
jiuwen_memory/common/errors.py 1 个 P3(LLMServiceError 未接入重试分类)
jiuwen_memory/common/llm/llm_impl/openai_llm.py no issues(超时优先捕获、APIConnectionError/APIStatusError 分别包装且日志脱敏)
jiuwen_memory/config/AGENTS.md no issues
jiuwen_memory/config/managed_service.py 3 个问题(P2 回滚缺失;P3 校验强度分叉;P3 不可写 api_key 阻断保存)
jiuwen_memory_adapter/jiuwenswarm/AGENTS.md no issues
jiuwen_memory_adapter/jiuwenswarm/agent_memory_provider.py no issues(HTTP 侧先 _record_failure() 再抛出、batch_add 降级语义保留与 AGENTS 描述一致)
jiuwen_memory_entry/core/error_response.py no issues(502/504 策略、504 固定文案)
jiuwen_memory_entry/core/handler.py 1 个 P3(504 分支回传 str(exc),与 error_response 固定文案不一致)
tests/unit/common/test_openai_llm.py no issues(httpx 已在顶部导入、断言与实现一致)
tests/unit/config/test_model_runtime.py no issues(clients 为 3 元组 (created, entered, release),clients[0][-1] 取最新 client 正确)
tests/unit/jiuwen_memory_adapter/test_agent_memory_provider_unit.py no issues(HTTP/SDK 两条错误传播路径均有用例)
类型 数量
🔴 阻塞 0
🟡 建议 2

💬 仅评论

def publish(model):
result = runtime.configuration.update_defaults(defaults(model), security=object())
assert result["applied"]
return result["version"]

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: df505f2ea3bd080d985e5eed291284d79561baa4

🟡 Medium Priority

建议:增加一个 authorize=lambda sec, op: False(或抛权限错误)的运行时用例,断言 update_defaults 抛 PermissionDeniedError,且随后 reply() 仍返回旧模型,覆盖该特权接口的权限闸门。

Comment thread jiuwen_memory/config/managed_service.py Outdated
previous = self._base
candidate = apply_assembly_overrides(previous, changes, self._fields.fields)
self._validate_context(candidate)
self._inherited_instances.update(instances)

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: df505f2ea3bd080d985e5eed291284d79561baa4

🟡 Medium Priority

变更行:update_defaults 在 try 之前就执行 self._inherited_instances.update(instances)(第 362 行),而 self._base 有回滚(第 371-373 行),_inherited_instances 没有。

影响链:_inherited_instances 是 save() 中 _pin_inherited_endpoints 的唯一开关 —— 一旦某个实例被标记为“继承宿主默认”,用户后续只要改任一 endpoint 字段(如仅改 llm_model),就会把当时的完整连接(target/base_url/api_key)钉进用户覆盖层。

失效模式:宿主调用 update_defaults 后,若第 369 行 _validate_context(self.assembly_context(snapshot)) 或第 370 行 commit 抛错(如用户存量覆盖与新默认冲突、版本冲突),_base 已回滚、宿主默认并未生效,但实例仍留在 _inherited_instances 里。此后该用户的普通保存会被静默钉住“旧的”默认连接,宿主下一次成功的默认切换对这个用户不再生效,与 S08 中「恢复整组连接后使用最新宿主默认值」的设计相反,且状态在进程内永久残留(无清理路径)。触发条件明确,但需要一次失败的热更新,故置信度中等。

建议:把 self._inherited_instances.update(instances) 移到提交成功之后(或在 except 分支中把它回滚到进入函数前的副本),保证它与 self._base 的提交/回滚语义一致:只有宿主默认真正落盘并发布后才登记“继承”状态。

@openjiuwen-collaboration-bot

Copy link
Copy Markdown

head_sha: df505f2ea3bd080d985e5eed291284d79561baa4

任务名称 结果 日志操作
静态检查 ✅SUCCESS 点此跳转
禁用词扫描 ✅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

@openjiuwen-collaboration-bot

Copy link
Copy Markdown

head_sha: a2dc897d8807520661fcbbec3b5cd3183829ec06

任务名称 结果 日志操作
静态检查 ✅SUCCESS 点此跳转
禁用词扫描 ✅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

@openjiuwen-collaboration-bot

Copy link
Copy Markdown

head_sha: 884ea14dabf37249c57de3793986d901fccd8772

任务名称 结果 日志操作
静态检查 ✅SUCCESS 点此跳转
禁用词扫描 ✅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

logger.error("OpenAILLM: invalid model service response")
raise LLMServiceError(response_error=True) from exc

content = response.choices[0].message.content

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: 884ea14dabf37249c57de3793986d901fccd8772

response.choices[0] 在 try 块之外。上游对 200 返回空 choices(内容过滤、部分兼容网关会这么回)时这里抛 IndexError,不会经过上面 APIError → LLMServiceError(response_error=True) 的转换:call_llm_with_retry 把它当普通异常重试,重试耗尽后在 HTTP 入口变成 500 InternalError,和本 PR 写的「响应异常映射 502、不重试」的契约不一致。建议取 content 前判断 if not response.choices: raise LLMServiceError(response_error=True),或把这一行也挪进 try。

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.

[Bug]: 模型服务异常返回通用错误,且缺少保留显式配置的默认模型热更新能力

2 participants