[ai] 持久化 deadline 覆盖整次交互:超过 30s 后工具结果/最终答案/轮次收尾同时失败,整轮对话丢失

未关闭
#1,645 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 8 天内回复

@ambiguous-pointer 已经在做这个了。

开始于 2026年11月14日。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
go
领域
ai, backend

调研方向

Start in ai/component/agent/react/react.go, where persistenceTimeout and persistCtx are created once per interaction, then follow persistenceContext in steps.go to the three writes: tool results, the final answer, and NextTurnForTurn. The fix proposed in the issue's solution comment should be checked against these call sites. Done means that a slow interaction (over 30s) still persists tool results and turn completion, and that a store failure on the final answer no longer hides the generated answer. Look for existing tests in the react package before changing behavior.

由索引模型根据 Issue 内容生成。

描述

概要

AI 助手的持久化 deadline 覆盖的是整次交互,而不是单次写入。一次交互很容易超过这个 deadline(多次模型调用 + 中间的工具调用),一旦超时,工具结果、最终答案、轮次收尾三处写库同时失败,整轮对话被 abort,该会话的下一个问题从零历史开始。

对用户的表现是:「AI 突然忘了我刚说过的话」,而且是在问题越多、工具越重的会话里越容易触发。

1. 问题

1.1 一个 deadline 被整个交互共享

ai/component/agent/react/react.go:

:76   const persistenceTimeout = 30 * time.Second
...
:337  persistCtx, persistCancel := context.WithTimeout(context.WithoutCancel(parent), persistenceTimeout)
:359  state := &interactionState{... persistCtx: persistCtx, persistCancel: persistCancel}
:362  ctx = context.WithValue(ctx, persistenceContextKey, persistCtx)

persistCtx 在交互开始时创建,30 秒后到期,并被注入整条交互链路的 context。

1.2 三个写库点全部使用这个共享 context

ai/component/agent/react/steps.go:

:291  func persistenceContext(ctx context.Context) context.Context {   // 取回共享的 persistCtx
:231  ra.messageStore.AddHistoryToTurn(persistenceContext(ctx), access, s.Session, turnID, message)   // ① 工具结果
:251  ra.messageStore.AddHistoryToTurn(persistenceContext(ctx), access, sessionID, turnID, message)   // ② 最终答案
:252  return fmt.Errorf("failed to persist final answer: %w", err)

ai/component/agent/react/react.go:

:254  ra.messageStore.NextTurnForTurn(state.persistCtx, access, sessionID, state.turnID)   // ③ 轮次收尾
     interactionErr = fmt.Errorf("failed to complete turn: %w", err)

deadline 一到,① ② ③ 全部返回 context deadline exceeded:

  • ① 工具结果丢失 → 下一轮上下文里没有工具输出;
  • ② 最终答案写库失败 并向上返回错误 → 已经生成好的答案被整个隐藏,用户收到的是失败而不是回答;
  • ③ 轮次收尾失败 → 该轮被 abort,下一次提问从空历史开始。
1.3 关键在于「答案已经生成成功了」

② 的失败发生在答案生成之后。当前实现把「存储暂时不健康」升级成了「用户拿不到回答」,这是错误的取舍。

2. 为什么重要

  • 数据丢失:用户这一轮说过的话、Agent 调过的工具结果、最终答案,在这 30s 之后都不入库。
  • 会话体验断裂:下一轮没有历史,模型看起来「失忆」,用户会重复描述已经给过的上下文。
  • 可用性反噬:存储后端(MySQL/PG)稍有抖动,答案就消失——存储故障被放大成了产品故障。
  • 触发条件不可控:交互耗时取决于模型输出长度和工具调用次数,用户无法预知;长问题 + 慢工具必然越界。

3. 证据

代码位置(ai 分支):

位置 内容
ai/component/agent/react/react.go:76 const persistenceTimeout = 30 * time.Second
ai/component/agent/react/react.go:337 persistCtx, persistCancel := context.WithTimeout(context.WithoutCancel(parent), persistenceTimeout)
ai/component/agent/react/react.go:359-362 persistCtx 存入 interactionState 并经 persistenceContextKey 注入交互 ctx
ai/component/agent/react/steps.go:231 工具结果写入使用共享 ctx
ai/component/agent/react/steps.go:251-252 最终答案写入使用共享 ctx,失败直接返回错误
ai/component/agent/react/react.go:254 轮次收尾使用 state.persistCtx

关键点:persistCtx 的 deadline 是从交互开始计时,而它服务的写操作发生在交互的末尾。一次交互 = 若干次模型调用 + 中间的工具调用,中间任何一段慢都会让末尾的写操作落在 deadline 之外。

4. 影响范围

  • 触发条件:单次交互耗时 > 30s(多轮工具调用、慢模型、慢存储都会叠加)。
  • 受影响组件:ai/component/agent/react 的 ReAct 循环 + ai/store 的会话持久化。
  • 已有可用修复(尚未提交 PR):见本 Issue 的解决方案评论。
主要语言
Go
星标
4.1k
派生
2.2k
平均合并
6 天 2 小时
30 天内合并 PR
4

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

apache/dubbo-admin 的其他 Issue

查看 apache/dubbo-admin 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。