fix(agent): 修复上下文压缩检查点保存失败

This commit is contained in:
ddcat666
2026-09-27 18:31:24 +08:00
parent 258422757c
commit 367145010f
4 changed files with 189 additions and 16 deletions

View File

@@ -4,6 +4,7 @@ package app
import (
"context"
"encoding/json"
"fmt"
"strings"
"testing"
@@ -120,6 +121,43 @@ func TestCloudAgentCompactionRequestsWhenTokenLineReached(t *testing.T) {
}
}
// Read results are replay caches, not the source of truth. A few large canvas
// snapshots must not make the requested compaction checkpoint impossible to
// persist, otherwise the outer error is misreported as a context overflow and
// the compression task is never even enqueued.
func TestCloudAgentCompactionCanCheckpointAfterPruningOversizedReadCache(t *testing.T) {
s, _, run, state := func() (*Service, *gorm.DB, *model.CloudAgentExecution, cloudAgentRuntime) {
s, db, root := reliableAgentRoot(t)
current, decoded := decodeCloudAgentCompactionState(t, s, root.ID)
return s, db, current, decoded
}()
state.ToolReadResults = make(map[string]cloudAgentCachedToolResult)
state.ToolReadReplays = make(map[string]int)
for index := 0; index < 8; index++ {
key := fmt.Sprintf("canvas_get_state:{\"offset\":%d}", index)
state.ToolReadResults[key] = cloudAgentCachedToolResult{Result: json.RawMessage(`{"content":"` + strings.Repeat("x", 100000) + `"}`)}
state.ToolReadReplays[key] = index + 1
}
state.ActiveTaskID = ""
budget := cloudAgentContextBudgetFor(8_000, 2_000, "test")
requested, err := s.cloudAgentRequestCompaction(run, &state, budget, budget.CompactAtTokens)
if err != nil || !requested {
t.Fatalf("大只读缓存不应阻塞压缩请求: requested=%v err=%v", requested, err)
}
persisted, decoded := decodeCloudAgentCompactionState(t, s, run.ID)
if decoded.ContextCompaction == nil || decoded.ContextCompaction.Status != "requested" {
t.Fatalf("压缩请求没有落库: %+v", decoded.ContextCompaction)
}
if len(decoded.ToolReadResults) >= 8 || len(decoded.ToolReadResults) != len(decoded.ToolReadReplays) {
t.Fatalf("超大只读缓存没有按需清理: results=%d replays=%d", len(decoded.ToolReadResults), len(decoded.ToolReadReplays))
}
if len(persisted.StateJSON) >= cloudAgentStateJSONLimit {
t.Fatalf("裁剪缓存后 StateJSON 仍超过上限: %d", len(persisted.StateJSON))
}
}
// 没有配模型上下文窗口时退回字节/条数兜底判据,且兜底判据同样必须"暂停而不是判死"。
func TestCloudAgentCompactionFallsBackToBytesWithoutModelWindow(t *testing.T) {
s, db, root := reliableAgentRoot(t)