fix(history): 历史运行复盘读库持久化 —— 杜绝过 Redis TTL 后卡「流式中…」

问题:历史任务超 Redis 流 10min TTL 后,SSE 回放在空流上永久阻塞 → 运行页卡「流式中…」、
轨迹/工具/输出全空。

修复:收尾把最终输出 + 执行轨迹持久化到 PG,历史复盘改读库(不依赖 Redis TTL):
- store:Task 加 output/trace 两列;SaveTaskOutput/SaveTaskTrace/GetRunDetail。
  trace 用 type:text(不是 jsonb)——否则提交时空串 "" 入 jsonb 列会 INSERT 失败、整条任务不落库。
  (已 ALTER 既有 trace 列 jsonb→text。)
- gateway:token/exec 录制器在 done 时把累计的输出/轨迹快照落库。
- 新增 GET /tasks/:id/replay 返回持久化的 {output, exec}。
- RunsView:选中历史运行改 runReplay() 读库(秒回、phase 立即 done/error),不再 SSE 回放。
  即便旧任务无持久化数据,也是 done+空态,绝不再卡「流式中…」。

live:新任务落库 output 305 字(含表格) + 轨迹 5 事件,/replay 正确返回;tsc+vite、gateway 全绿。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-06-26 17:52:26 +08:00
parent 594be6a317
commit d2662a1f37
6 changed files with 89 additions and 15 deletions
@@ -9,6 +9,7 @@ import (
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/gin-contrib/sse"
@@ -85,9 +86,19 @@ func (h *Handler) startTokenRecorder(taskID string) {
return
}
ctx, cancel := context.WithTimeout(context.Background(), 6*time.Minute) // 兜底防泄漏
var out strings.Builder // 收尾时把整段输出快照落库(供过 Redis TTL 的历史复盘)
unsub, err := h.bus.SubscribeTokens(taskID,
func(tok []byte) { _ = h.cache.StreamAppend(ctx, store.ChannelToken, taskID, "token", string(tok)) },
func() { _ = h.cache.StreamAppend(ctx, store.ChannelToken, taskID, "done", ""); cancel() },
func(tok []byte) {
out.Write(tok)
_ = h.cache.StreamAppend(ctx, store.ChannelToken, taskID, "token", string(tok))
},
func() {
_ = h.cache.StreamAppend(ctx, store.ChannelToken, taskID, "done", "")
if out.Len() > 0 {
_ = h.db.SaveTaskOutput(context.Background(), taskID, out.String())
}
cancel()
},
)
if err != nil {
cancel()
@@ -104,9 +115,23 @@ func (h *Handler) startExecRecorder(taskID string) {
return
}
ctx, cancel := context.WithTimeout(context.Background(), 12*time.Minute) // 含 HITL 审批等待,给足
var evs []json.RawMessage // 收尾时把轨迹快照落库(供过 Redis TTL 的历史复盘)
unsub, err := h.bus.SubscribeExec(taskID,
func(data []byte) { _ = h.cache.StreamAppend(ctx, store.ChannelExec, taskID, "exec", string(data)) },
func() { _ = h.cache.StreamAppend(ctx, store.ChannelExec, taskID, "done", ""); cancel() },
func(data []byte) {
cp := make([]byte, len(data))
copy(cp, data)
evs = append(evs, cp)
_ = h.cache.StreamAppend(ctx, store.ChannelExec, taskID, "exec", string(data))
},
func() {
_ = h.cache.StreamAppend(ctx, store.ChannelExec, taskID, "done", "")
if len(evs) > 0 {
if b, err := json.Marshal(evs); err == nil {
_ = h.db.SaveTaskTrace(context.Background(), taskID, string(b))
}
}
cancel()
},
)
if err != nil {
cancel()
@@ -421,6 +446,16 @@ func (h *Handler) StatsOverview(c *gin.Context) {
})
}
// TaskReplay: GET /api/v1/tasks/:id/replay —— 历史运行复盘(持久化的输出 + 轨迹,读库不依赖 Redis TTL)。
func (h *Handler) TaskReplay(c *gin.Context) {
output, trace := h.db.GetRunDetail(c.Request.Context(), c.Param("id"))
var exec []json.RawMessage
if trace != "" {
_ = json.Unmarshal([]byte(trace), &exec)
}
c.JSON(http.StatusOK, gin.H{"output": output, "exec": exec})
}
// Runs: GET /api/v1/runs?limit= —— 运行历史列表(任务 + 评测分级),供「运行 · 观测」复盘。
func (h *Handler) Runs(c *gin.Context) {
limit := 30