feat(agent): get_worker_trace 的 step_ids 超限改为降级返回而非报错

原先 step_ids 超过 5 个直接报错,逼 Agent 重新组织调用、白跑一轮。改为优雅
降级:先去重、剔除非法 id,超出 5 个则返回前 5 步的完整内容,并在结果里用
returned_step_ids / omitted_step_ids / notice 告知本次取了哪些、还有哪些没取,
并提示「若已足够则无需再取剩余」。Agent 立刻拿到数据,是否再取由它自行判断。

保留数量上限的原因不变:detail 是完整原文、不截断,上限用于兜住单个 tool
result 的大小。get_task_worker_trace 经 delegateToTask 复用同一实现,一并生效;
两处工具描述同步更新。

验证: 新增 TestGetWorkerTraceStepIDsDegradeGracefully(含去重/非法 id/前5/
omitted/notice/在限内不加提示),连本地 PG 实跑通过;agent 全量无回归。
This commit is contained in:
Autumn-27 committed 2026-08-24 11:35:49 -04:00
1 parent 6a8e13fcbd
commit 412a993c25
4 files changed
+106 -9

No files matched your search

+72
View File
@@ -272,3 +272,75 @@ func TestBlackboardToolsReadDirectSources(t *testing.T) {
t.Fatalf("inherited trace search: %#v", hits)
}
}
// TestGetWorkerTraceStepIDsDegradeGracefully pins the over-cap behaviour: instead
// of erroring, get_worker_trace returns the first 5 requested steps and tells the
// model which ids it deferred, after de-duplicating and dropping invalid ids.
func TestGetWorkerTraceStepIDsDegradeGracefully(t *testing.T) {
d := testDB(t)
defer d.Close()
task, err := d.CreateTask("trace-cap", "goal", nil, 0, 0)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = d.DeleteTask(task.ID) })
store := d.Exploration(task.ExplorationID)
intent, err := store.AddIntent(map[string]any{"summary": "cap work"}, 1, nil, "planner")
if err != nil {
t.Fatal(err)
}
var stepIDs []int64
for i := range 6 {
sid, err := store.AppendActivity(db.Activity{
NodeID: &intent, Worker: "w", Kind: "result", Tool: "HTTP",
Summary: fmt.Sprintf("step %d", i), Detail: fmt.Sprintf("detail %d", i),
})
if err != nil {
t.Fatal(err)
}
stepIDs = append(stepIDs, sid)
}
tools := NewToolSet(store, "worker")
tools.SetAssetStore(d.Assets(), d.Companies())
// Request 7 ids: a duplicate of the first, an invalid 0, then all 6 real ids.
// After dedup/cleanup that is 6 valid ids — one over the cap.
requested := []int64{stepIDs[0], stepIDs[0], 0, stepIDs[1], stepIDs[2], stepIDs[3], stepIDs[4], stepIDs[5]}
input, _ := json.Marshal(map[string]any{"intent_id": intent, "step_ids": requested})
res := callReadJSON(t, tools.getWorkerTrace(), string(input)).(map[string]any)
returned, _ := res["returned_step_ids"].([]any)
if len(returned) != 5 {
t.Fatalf("returned_step_ids=%v, want the first 5", res["returned_step_ids"])
}
// First 5 distinct valid ids, in request order.
wantReturned := []int64{stepIDs[0], stepIDs[1], stepIDs[2], stepIDs[3], stepIDs[4]}
for i, raw := range returned {
if int64(raw.(float64)) != wantReturned[i] {
t.Fatalf("returned[%d]=%v, want %d", i, raw, wantReturned[i])
}
}
omitted, _ := res["omitted_step_ids"].([]any)
if len(omitted) != 1 || int64(omitted[0].(float64)) != stepIDs[5] {
t.Fatalf("omitted_step_ids=%v, want [%d]", res["omitted_step_ids"], stepIDs[5])
}
if notice, _ := res["notice"].(string); notice == "" {
t.Fatalf("notice missing — model would not know a step was deferred")
}
if steps, _ := res["steps"].([]any); len(steps) != 5 {
t.Fatalf("steps=%d, want 5 detail rows", len(steps))
}
// At or under the cap: no notice, no omitted list.
okInput, _ := json.Marshal(map[string]any{"intent_id": intent, "step_ids": stepIDs[:3]})
okRes := callReadJSON(t, tools.getWorkerTrace(), string(okInput)).(map[string]any)
if _, hasNotice := okRes["notice"]; hasNotice {
t.Fatalf("notice present for an in-cap request: %#v", okRes["notice"])
}
if _, hasOmitted := okRes["omitted_step_ids"]; hasOmitted {
t.Fatalf("omitted_step_ids present for an in-cap request: %#v", okRes["omitted_step_ids"])
}
}