ambiguous-pointer commented on issue #1645:
URL: https://github.com/apache/dubbo-admin/issues/1645#issuecomment-6877572797
## 解决方案思路
把 deadline 的粒度**从「一次交互」下移到「一次写入」**。交互级的共享 context 只负责携带会话值,写操作的时限各自独立。
### 第 1 步:共享 context 去掉 deadline
`ai/component/agent/react/react.go:337` 当前是:
```go
persistCtx, persistCancel :=
context.WithTimeout(context.WithoutCancel(parent), persistenceTimeout)
```
改为:
```go
// 共享 context 承载整次交互的会话值(sessionID / turnID),
// 刻意不设 deadline:交互耗时由模型与工具决定,30s 预算不足以覆盖它。
persistCtx := context.WithoutCancel(parent)
```
同时 `interactionState` 里的 `persistCancel` 字段可以移除(`cancelPersistence()`
一并删掉),因为共享 context 本来就没有需要取消的超时上下文——`WithoutCancel` 的语义就是「跟随父值的取消,但不继承取消」。
### 第 2 步:三个写点各自取一个有界 context
新增一个统一封装,避免每个写点各写一遍 `WithTimeout`:
```go
// persistenceWriteContext 为单次持久化写入提供一个有界的 context。
// 预算从「交互开始」起算不成立——写入发生在交互末尾,
// 交互本身可能远长于预算,因此每次写入独立计时。
func persistenceWriteContext(ctx context.Context) (context.Context,
context.CancelFunc) {
base := context.WithoutCancel(ctx)
return context.WithTimeout(base, persistenceTimeout)
}
```
然后把三个写点改成:
| 位置 | 改成 |
|---|---|
| `steps.go:231` 工具结果写入 | `writeCtx, cancel := persistenceWriteContext(ctx)`
→ `AddHistoryToTurn(writeCtx, ...)` → `cancel()` |
| `steps.go:251` 最终答案写入 | 同上 |
| `react.go:254` 轮次收尾 | `writeCtx, cancel := persistenceWriteContext(ctx)` →
`NextTurnForTurn(writeCtx, ...)` → `cancel()` |
注意 `cancel()` 要紧跟着写操作调用(不要 `defer` 到函数结束),否则 context 会一直挂着直到交互结束。
### 第 3 步:最终答案写库失败不再吞掉答案
这是本 Issue 里**行为层面的关键修正**,不只是超时问题:
```go
// steps.go:251 现状
if err := ra.messageStore.AddHistoryToTurn(...); err != nil {
return fmt.Errorf("failed to persist final answer: %w", err) // ←
答案已经生成了,却被整个隐藏
}
```
改成:记录日志 + 降级,不向上返回错误。答案已经产出,存储后端不健康不应该让用户拿不到回答:
```go
if err := ra.messageStore.AddHistoryToTurn(writeCtx, ...); err != nil {
rt.GetLogger().Error("Failed to persist final answer",
"session_id", sessionID, "turn_id", turnID, "error", err)
// 答案已经生成,不再向上返回错误;轮次照常收尾。
}
```
轮次收尾(`react.go:254`)同理:如果 `NextTurnForTurn`
失败,倾向于**记录日志并继续**(下一轮仍可基于已写入的历史工作),而不是把整轮判失败。具体取舍要看 `NextTurnForTurn`
的语义——如果它承担的是「标记本轮结束」且失败会导致该轮残留为 open 状态,那就应该保留错误路径,但至少要把最终答案先返回给用户。
### 第 4 步:abort 路径保持不变
`react.go` 中 `cleanupCtx, cancel :=
context.WithTimeout(context.WithoutCancel(parent), persistenceTimeout)` 这类
**abort 路径**的独立超时是正确的,**不要动**。abort
是一次性的、明确要尽快完成的操作,它需要自己的时限;它出问题也只是清理失败,不影响用户可见行为。
### 第 5 步:补回归测试(避免用真实 30s 等待)
`persistenceTimeout` 目前是 `const`,测试无法缩短它。改成 `var`:
```go
// var 而非 const,便于测试构造「交互长于写预算」的场景,
// 而不必真的等待 30s。
var persistenceTimeout = 30 * time.Second
```
然后在 `ai/component/agent/react/store_integration_test.go` 补三个用例:
1. **长交互不丢轮次**:把 `persistenceTimeout` 调到极小(如 10ms),用一个慢 store(每次写入 sleep
超过预算)驱动一次多轮交互,断言工具结果与最终答案**仍然被写入**(各自取到自己的写预算),轮次正常收尾;
2. **写失败不吞答案**:注入一个始终返回错误的 store,断言**最终答案依然返回给用户**,只是日志里有 error;
3. **轮次收尾失败的影响面**:断言 `NextTurnForTurn` 失败时,后续提问仍能读到上一轮已成功写入的历史。
用例里恢复 `persistenceTimeout` 需要 `defer` 复原,避免污染其它用例。
### 已有可用实现
上述第 1、3、4、5 步在 fork 上已有完整实现:
-
分支:`https://github.com/ambiguous-pointer/dubbo-admin/tree/fix/ai-persistence-deadline`
- 提交:`e3f5f79` — *fix(ai): bound each persistence write instead of the whole
interaction*
- 内容:共享 context 改为 `context.WithoutCancel(parent)` 且去掉 deadline;新增
`persistenceWriteContext`;最终答案写失败改为记日志不返回错误;`persistenceTimeout` 改为
`var`;`store_integration_test.go` 增加 137 行回归用例。
**它需要 rebase,且有一处需要人工裁决**:该分支基于 #1592(`feat(ai): enforce session ownership
isolation`)**之前**的状态,它的 diff 里把 `security.AccessContext` 参数从 `AbortTurnForTurn`
/ `NextTurnForTurn` / `AddHistoryToTurn` / `ContextWindowForTurn` / `BeginTurn`
上一并删掉了。rebase 到当前 `ai`(`e6cef77`)时**必须保留 #1592 的 access 隔离改造**——把 deadline 修复与
access 参数回补分开做,先把 #1592 的签名落进基线,再叠加写预算修复,避免把会话归属隔离这个安全属性改没了。
--
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]