ambiguous-pointer opened a new issue, #1645:
URL: https://github.com/apache/dubbo-admin/issues/1645
## 概要
AI 助手的持久化 deadline 覆盖的是**整次交互**,而不是**单次写入**。一次交互很容易超过这个 deadline(多次模型调用 +
中间的工具调用),一旦超时,**工具结果、最终答案、轮次收尾三处写库同时失败**,整轮对话被 abort,该会话的下一个问题从**零历史**开始。
对用户的表现是:「AI 突然忘了我刚说过的话」,而且是在问题越多、工具越重的会话里越容易触发。
## 1. 问题
### 1.1 一个 deadline 被整个交互共享
`ai/component/agent/react/react.go`:
```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`:
```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`:
```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 的解决方案评论。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]