Skip to content

fix(graph): preserve clarification turns in context#541

Open
dwyyyyy wants to merge 1 commit into
spring-ai-alibaba:mainfrom
dwyyyyy:codex/fix-clarification-continuation
Open

fix(graph): preserve clarification turns in context#541
dwyyyyy wants to merge 1 commit into
spring-ai-alibaba:mainfrom
dwyyyyy:codex/fix-clarification-continuation

Conversation

@dwyyyyy

@dwyyyyy dwyyyyy commented Jun 10, 2026

Copy link
Copy Markdown

Describe what this PR does / why we need it

修复多轮对话中“用户澄清后无法稳定续接上一轮问题”的问题。

当前流程里,如果 FeasibilityAssessmentNode 将用户请求判断为“需要澄清”,Graph 会流式输出澄清问题,然后在进入 PlannerNode 之前结束本轮执行。

例如:

  1. 用户输入:找出最受欢迎的网络小说
  2. 后端返回:请问您指的“最受欢迎”是按“评分”还是“点击数”来衡量呢?
  3. 用户下一轮输入:按点击数

修复前,MultiTurnContextManager 只会保存有 PlannerNode 输出的轮次。由于“需要澄清”的轮次没有进入 PlannerNodefinishTurn 会直接丢弃这一轮上下文,导致下一轮的 按点击数 会作为一个孤立的新 query 处理,MULTI_TURN_CONTEXT 中没有上一轮原始问题和澄清提示。

这会导致模型无法稳定判断用户是在回答上一轮澄清问题。

Does this pull request fix one issue?

NONE

Describe how you did it

本 PR 调整了多轮上下文记录逻辑:

  1. MultiTurnContextManager 中新增 assistant 输出缓存。
  2. GraphServiceImpl 中,PlannerNode 的流式输出仍然写入 planner 输出;非 PlannerNode 的流式输出会写入 assistant 输出。
  3. finishTurn 时:
    • 如果有 planner 输出,继续按原逻辑保存为 AI计划
    • 如果没有 planner 输出,但有 assistant 输出,则保存为 AI回复
    • 如果两者都没有,仍然跳过历史写入

这样即使流程在澄清阶段结束,也会把本轮用户问题和 assistant 澄清回复保存到多轮上下文中。

修复后,下一轮同一个 threadId 的请求可以在 MULTI_TURN_CONTEXT 中看到:

用户: 找出最受欢迎的网络小说
AI回复: 请问您指的“最受欢迎”是按“评分”还是“点击数”来衡量呢?

因此用户继续输入 按点击数 时,模型可以结合上下文理解这是对上一轮澄清问题的回答,而不是孤立的新 query。

Describe how to verify it

可以通过以下场景验证:

  1. 输入一个需要澄清的模糊问题,例如:找出最受欢迎的网络小说
  2. 确认后端返回澄清问题,例如询问按“评分”还是“点击数”
  3. 在同一个 threadId 下继续输入:按点击数
  4. 确认第二轮请求的 MULTI_TURN_CONTEXT 中包含上一轮用户问题和 assistant 澄清回复
  5. 确认模型能够将 按点击数 理解为上一轮问题的补充条件

已补充 MultiTurnContextManagerTest,覆盖无 PlannerNode 输出但有 assistant 回复时应保存上下文的场景。

本地尝试执行:

./mvnw -pl data-agent-management -Dtest=MultiTurnContextManagerTest test

但当前本地环境 Maven wrapper 执行超时,未能完成测试运行。

Special notes for reviews

本次修改保持原有 planner 输出逻辑不变,仅在没有 planner 输出但存在 assistant 输出时,额外保存该轮上下文,避免澄清轮次丢失。

Before this change, clarification turns could be lost from the backend
multi-turn context. When FeasibilityAssessmentNode classified a user request
as needing clarification, the graph streamed the clarification response and
then ended before entering PlannerNode. MultiTurnContextManager only persisted
turns that had PlannerNode output, so finishTurn discarded these clarification
turns.

That meant a follow-up such as "按点击数" was sent as a new query with no
record of the original question or the assistant's clarification prompt in
MULTI_TURN_CONTEXT. The model therefore could not reliably understand that the
follow-up was answering the previous clarification.

This change records assistant output for the pending turn as a fallback when no
planner output is produced. Planner output is still saved as AI计划. If a turn
ends before PlannerNode but streamed an assistant response, it is now saved as
AI回复 together with the original user question. Empty turns are still skipped.

With this context preserved, the next request on the same thread can see both
the original question and the clarification prompt, so short follow-ups such as
"按点击数" can be interpreted as clarification answers instead of isolated new
queries.
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