fix(agent): 会话真相源后端化 + loop 健壮性 + usage 修复 - #56
Merged
Conversation
修复 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 个用例覆盖上述修复
🔍 OpenCode PR Review Required这是一个受保护的分支,merge 前需要进行 code review。 请运行以下命令进行 OpenCode review: 或者在 PR 页面评论 This is an automated reminder from PR Review Gate. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概述
修复 8 个经实测确认的 agent 问题(P0+P1+P2),核心是把会话真相源从前端 localStorage 移到后端 DB。
修复清单
后端真相源(修①②③)
agent_chatSSE 首事件返conversation_init {conversation_id},前端采用后端 id 作 localStorage key,不再每轮新建会话_db_messages_to_openai),不再依赖前端重发全历史持久化修复(修④⑤)
stream_with_savedone 事件加saved_done标志,只存一次 assistant;agent_service删_chat_iter/_confirm_iter/_reject_iter多余yield done(loop 内部已发,此前导致 done=2~3 重复持久化)/agent/confirm与/agent/reject加_stream_with_save_for_action,tool/assistant 消息落 DB(之前 confirm 后重载页面出现空洞)loop 健壮性(修性(修⑧⑨⑩⑬)
tool_call非法 JSON 参数 try/except,跳过该 tool + yield error,不崩整个 runconfirm_calls[0],导致 tool_calls/tool_result 不配对)self.llm.provider(真实 provider),不再错传event.model用户画像(修⑭)
_build_user_profile用PaperRepository.count_by_read_status查真实总数,不用 limit 列表的 len(被 limit 截断,与真实总数不一致)前端
SSEEventType加conversation_initAgentSessionContext.processSSE加conversation_initcase,setActiveId到后端 idsendMessage只发本次新消息,删全历史重发useConversations/ConversationCtx暴露setActiveId测试
tests/test_agent_conversation.py8 个用例:pytest tests/→ 69 passed, 2 skipped;ruff check全绿。不做(本轮)
风险
_db_messages_to_openai。