feat(agent): 云端 Agent - 增加上下文预算、任务事实与终态恢复保障

This commit is contained in:
ddCat committed 2026-09-19 20:43:20 +08:00
1 parent 3bf21d79af
commit b52bc9d5f2
61 files changed
+3512 -309

No files matched your search

@@ -19,7 +19,7 @@ func TestCloudAgentContextCompactionPreservesToolPairsAndWrites(t *testing.T) {
map[string]any{"role": "tool", "tool_call_id": i, "content": body})
}
last, _ := json.Marshal(request.Messages[len(request.Messages)-2:])
if !compactCloudAgentContext(&request) {
if !compactCloudAgentContext(&request, cloudAgentContextBudgetFor(64_000, 8_000, "test")) {
t.Fatal("large read bodies not compacted")
}
if len(request.Messages) != 33 || request.Messages[0]["content"] != "original instructions" || request.Messages[4]["content"] != write {
@@ -34,11 +34,63 @@ func TestCloudAgentContextCompactionPreservesToolPairsAndWrites(t *testing.T) {
t.Fatal("tool pair broken")
}
}
if compactCloudAgentContext(&request) {
if compactCloudAgentContext(&request, cloudAgentContextBudgetFor(64_000, 8_000, "test")) {
t.Fatal("compaction is not idempotent")
}
}
func TestCloudAgentContextCompactionPreservesNodeExecutionFacts(t *testing.T) {
nodes := make([]any, 0, 40)
for i := 0; i < 40; i++ {
nodes = append(nodes, map[string]any{
"id": "node-" + strings.Repeat("x", i+1),
"content": strings.Repeat("large rereadable body", 400),
"generation": map[string]any{
"taskId": "task-1", "generationId": "generation-1", "approvalId": "approval-1",
"taskSubmitted": true, "submissionOutcome": "accepted", "phase": "execution",
"diagnosticId": "diag-1", "fieldPath": "generationSpec.options.durationSeconds",
"cancellationSource": "user_request", "writebackReason": "node_deleted",
"billing": map[string]any{"orderId": "order-1", "status": "settled"},
},
})
}
body, _ := json.Marshal(map[string]any{"nodes": nodes, "page": 1})
request := canonicalAgentRequest{Messages: []map[string]any{
{"role": "user", "content": "读取画布"},
{"role": "assistant", "content": "", "tool_calls": []map[string]any{{"id": "read"}}},
{"role": "tool", "tool_call_id": "read", "content": string(body)},
{"role": "assistant", "content": "继续", "tool_calls": []map[string]any{{"id": "latest"}}},
{"role": "tool", "tool_call_id": "latest", "content": `{"status":"ok"}`},
}}
if !compactCloudAgentContext(&request, cloudAgentContextBudgetFor(64_000, 8_000, "test")) {
t.Fatal("large node read was not compacted")
}
var compacted map[string]any
if err := json.Unmarshal([]byte(stringField(request.Messages[2], "content")), &compacted); err != nil {
t.Fatal(err)
}
facts, ok := compacted["observedFacts"].([]any)
if !ok || len(facts) != 40 {
t.Fatalf("observed facts = %#v", compacted["observedFacts"])
}
generation := facts[0].(map[string]any)["generation"].(map[string]any)
for key, want := range map[string]any{
"taskId": "task-1", "generationId": "generation-1", "approvalId": "approval-1", "taskSubmitted": true,
"submissionOutcome": "accepted", "phase": "execution", "diagnosticId": "diag-1",
"fieldPath": "generationSpec.options.durationSeconds", "cancellationSource": "user_request", "writebackReason": "node_deleted",
} {
if generation[key] != want {
t.Fatalf("generation.%s = %#v, want %#v", key, generation[key], want)
}
}
if billing := generation["billing"].(map[string]any); billing["orderId"] != "order-1" || billing["status"] != "settled" {
t.Fatalf("billing facts lost: %#v", billing)
}
if compactCloudAgentContext(&request, cloudAgentContextBudgetFor(64_000, 8_000, "test")) {
t.Fatal("second compaction should be idempotent")
}
}
func TestCloudAgentCanonicalUsesAutomaticToolChoice(t *testing.T) {
req := agentTestRequest()
request := cloudAgentCanonical("system", nil, "读取画布", req)