Files
Agent-Native/runtime/subagent/subagent_test.go
T
weichenandCursor 6fd1d441a9
ci / vet-and-test (pull_request) Successful in 45s
ci / docker-checkout-hostgateway (pull_request) Successful in 1s
Keep tool pairs and fold reminders into the last result
Why: Old tool results were filling the window, compaction could split a tool group or commit a stale summary, and reminders arrived as extra user messages.
Evidence: Compaction retries a conflict and opens the circuit after three failures; clearing keeps the last five tool results; a stale file write is rejected; RPC reports started, queued, or handled.
Impact: Requests over the context window replace older tool results with a placeholder. Rewriting an existing file now requires a read in the same run. Subagent text over 32000 characters is capped.
Refs: feature/context-engineering
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-03 16:19:36 +00:00

110 lines
3.8 KiB
Go

package subagent
import (
"context"
"encoding/json"
"strings"
"testing"
"agent-native/runtime/tools"
)
func TestRegisterNilFactoryNoop(t *testing.T) {
reg := tools.NewRegistry(t.TempDir())
Register(reg, nil)
for _, d := range reg.Decls() {
if d.Name == "delegate_task" {
t.Fatal("nil factory must not register delegate_task")
}
}
}
func TestDelegateToolRoundTrip(t *testing.T) {
var got Request
reg := tools.NewRegistry(t.TempDir())
Register(reg, func(ctx context.Context, req Request) (Result, error) {
got = req
return Result{
Success: true,
Text: "调研完成:结论 A。",
Files: map[string][]byte{"notes.md": []byte("n"), "data/table.csv": []byte("c")},
}, nil
})
res := reg.Execute(context.Background(), "delegate_task", json.RawMessage(`{
"goal":"调研主题 X",
"inputs":"docs/v2 规范",
"write_scope":"research/",
"non_goals":"不做实现",
"done_criteria":"产出三来源对照表",
"deliverables":["notes.md","data/table.csv"]
}`))
if res.IsError {
t.Fatal(res.Content)
}
if got.Goal != "调研主题 X" || got.WriteScope != "research/" || got.DoneCriteria != "产出三来源对照表" {
t.Fatalf("delegation fields lost: %+v", got)
}
if !strings.Contains(res.Content, "[sub-agent succeeded]") ||
!strings.Contains(res.Content, "调研完成") ||
!strings.Contains(res.Content, "files: data/table.csv, notes.md") {
t.Fatalf("result text wrong:\n%s", res.Content)
}
}
func TestDelegateToolFailureAndValidation(t *testing.T) {
reg := tools.NewRegistry(t.TempDir())
Register(reg, func(ctx context.Context, req Request) (Result, error) {
return Result{Success: false, Err: "子任务超出预算"}, nil
})
res := reg.Execute(context.Background(), "delegate_task", json.RawMessage(`{"goal":"g","done_criteria":"d"}`))
if !res.IsError || !strings.Contains(res.Content, "[sub-agent failed]") || !strings.Contains(res.Content, "子任务超出预算") {
t.Fatalf("failure must propagate, got %+v", res)
}
// goal 必填(schema 校验拦截)。
res = reg.Execute(context.Background(), "delegate_task", json.RawMessage(`{"done_criteria":"d"}`))
if !res.IsError || !strings.Contains(res.Content, "required argument goal") {
t.Fatalf("missing goal must fail schema validation, got %+v", res)
}
}
func TestDelegateToolFactoryErrorAndCancel(t *testing.T) {
reg := tools.NewRegistry(t.TempDir())
Register(reg, func(ctx context.Context, req Request) (Result, error) {
return Result{}, context.Canceled
})
res := reg.Execute(context.Background(), "delegate_task", json.RawMessage(`{"goal":"g","done_criteria":"d"}`))
if !res.IsError || !strings.Contains(res.Content, "sub-agent failed") {
t.Fatalf("factory error must surface, got %+v", res)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
res = reg.Execute(ctx, "delegate_task", json.RawMessage(`{"goal":"g","done_criteria":"d"}`))
if !res.IsError || !strings.Contains(res.Content, "canceled before start") {
t.Fatalf("pre-canceled delegation must not start, got %+v", res)
}
}
func TestCapParentTextWritesOverflowPath(t *testing.T) {
shown, full := CapParentText("短")
if shown != "短" || full != nil {
t.Fatalf("short: %q %d", shown, len(full))
}
body := strings.Repeat("字", ParentResultCap+3)
shown, full = CapParentText(body)
if full == nil || string(full) != body {
t.Fatal("full text not returned")
}
if len([]rune(shown)) != ParentResultCap {
t.Fatalf("shown runes %d", len([]rune(shown)))
}
reg := tools.NewRegistry(t.TempDir())
Register(reg, func(ctx context.Context, req Request) (Result, error) {
return Result{Success: true, Text: body}, nil
})
res := reg.Execute(context.Background(), "delegate_task", json.RawMessage(`{"goal":"g","done_criteria":"d"}`))
if res.IsError || !strings.Contains(res.Content, "subagent-result.txt") {
t.Fatalf("capped result: %+v", res)
}
}