Skip to content

fix(agent): 会话真相源后端化 + loop 健壮性 + usage 修复 - #56

Merged
Color2333 merged 1 commit into
mainfrom
fix/agent-conversation-persistence
Jul 18, 2026
Merged

fix(agent): 会话真相源后端化 + loop 健壮性 + usage 修复#56
Color2333 merged 1 commit into
mainfrom
fix/agent-conversation-persistence

Conversation

@Color2333

Copy link
Copy Markdown
Owner

概述

修复 8 个经实测确认的 agent 问题(P0+P1+P2),核心是把会话真相源从前端 localStorage 移到后端 DB。

修复清单

后端真相源(修①②③)

  • ① 会话脱节agent_chat SSE 首事件返 conversation_init {conversation_id},前端采用后端 id 作 localStorage key,不再每轮新建会话
  • ② 不拼历史:后端按 conversation_id 从 DB 读全量历史重建 OpenAI messages(_db_messages_to_openai),不再依赖前端重发全历史
  • ③ payload 爆涨:前端只发本次新 user 消息(删 634-662 全历史遍历),历史由后端拼

持久化修复(修④⑤)

  • ④ done 重复存stream_with_save done 事件加 saved_done 标志,只存一次 assistant;agent_service_chat_iter/_confirm_iter/_reject_iter 多余 yield done(loop 内部已发,此前导致 done=2~3 重复持久化)
  • ⑤ confirm 后空洞/agent/confirm/agent/reject_stream_with_save_for_action,tool/assistant 消息落 DB(之前 confirm 后重载页面出现空洞)

loop 健壮性(修性(修⑧⑨⑩⑬)

  • ⑨ 非法 JSON 崩溃tool_call 非法 JSON 参数 try/except,跳过该 tool + yield error,不崩整个 run
  • ⑧ 多 confirm 工具丢弃:一轮内多个 confirm 工具,首个挂起,其余转 tool 消息提示 LLM 下轮重提(之前只处理 confirm_calls[0],导致 tool_calls/tool_result 不配对)
  • ⑩ max_rounds 静默截断:max_rounds 耗尽时 yield text_delta 提示再 done,不静默截断
  • ⑬ usage 传错 provider:usage 回调传 self.llm.provider(真实 provider),不再错传 event.model

用户画像(修⑭)

  • ⑭ count 不准_build_user_profilePaperRepository.count_by_read_status 查真实总数,不用 limit 列表的 len(被 limit 截断,与真实总数不一致)

前端

  • SSEEventTypeconversation_init
  • AgentSessionContext.processSSEconversation_init case,setActiveId 到后端 id
  • sendMessage 只发本次新消息,删全历史重发
  • useConversations/ConversationCtx 暴露 setActiveId

测试

tests/test_agent_conversation.py 8 个用例:

  • ⑭ count_by_read_status 真实总数 vs limit 列表截断
  • ①② 后端拼历史 + 新消息去重
  • ④ done 去重语义
  • ⑤ confirm 从 pending action 取 conversation_id
  • ⑨ 非法 JSON 不抛异常
  • ⑬ usage 回调用 llm.provider

pytest tests/ → 69 passed, 2 skipped;ruff check 全绿。

不做(本轮)

  • ⑮用户隔离(需 auth 改造,大)
  • ⑯死代码清理(独立 PR)
  • ⑰-㉑前端 UX 打磨(hover copy/nav 重叠/retry 重复/无超时)
  • ⑪⑫兜底内容/deep_read 同步下载(pipeline 深改)

风险

  • :后端拼历史需正确重建 tool/assistant 消息格式(OpenAI 要求 tool_calls 与 tool result 配对),格式错会触发 LLM 报错。测试覆盖 _db_messages_to_openai
  • :前端 setActiveId 切后端 id 时 localStorage 旧 key 残留——新对话起用后端 id,旧 localStorage 对话保持前端 id(存量不迁移,可接受)。

修复 8 个经实测确认的 agent 问题(P0+P1+P2):

后端真相源(修①②③):
- agent_chat SSE 首事件返 conversation_init {conversation_id},
  前端采用后端 id 作 localStorage key,不再每轮新建会话
- 后端按 conversation_id 从 DB 读全量历史重建 OpenAI messages,
  前端只发本次新 user 消息,消除 payload 随轮次爆涨

持久化修复(修④⑤):
- stream_with_save done 事件加 saved_done 标志,只存一次 assistant
- agent_service 删 _chat_iter/_confirm_iter/_reject_iter 多余 yield done
  (loop 内部已发,此前导致 done=2~3 重复持久化)
- /agent/confirm 与 /agent/reject 加 stream_with_save,tool/assistant
  消息落 DB(之前 confirm 后重载页面出现空洞)

loop 健壮性(修性(修⑧⑨⑩⑬):
- tool_call 非法 JSON 参数 try/except,跳过该 tool + 报错,不崩整个 run
- 一轮内多个 confirm 工具:首个挂起,其余转 tool 消息提示 LLM 下轮重提
- max_rounds 耗尽时 yield text_delta 提示再 done,不静默截断
- usage 回调传 self.llm.provider(真实 provider),不再错传 event.model

用户画像(修⑭):
- _build_user_profile 用 PaperRepository.count_by_read_status 查真实总数,
  不用 limit 列表的 len(被 limit 截断,与真实总数不一致)

前端:
- SSEEventType 加 conversation_init
- AgentSessionContext processSSE 加 conversation_init case,
  setActiveId 到后端 id;sendMessage 只发本次新消息,删全历史重发
- useConversations/ConversationCtx 暴露 setActiveId

测试:tests/test_agent_conversation.py 8 个用例覆盖上述修复
@Color2333
Color2333 merged commit da14688 into main Jul 18, 2026
2 checks passed
@Color2333
Color2333 deleted the fix/agent-conversation-persistence branch July 18, 2026 14:35
@github-actions

Copy link
Copy Markdown

🔍 OpenCode PR Review Required

这是一个受保护的分支,merge 前需要进行 code review。

请运行以下命令进行 OpenCode review:

/oc review https://github.com/Color2333/PaperMind/pull/$PR_NUM

或者在 PR 页面评论 /oc 来触发 OpenCode review。


This is an automated reminder from PR Review Gate.

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.

1 participant