| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/provider" |
| 10 | ) |
| 11 | |
| 12 | func TestSubagentRunErrorPreservesPartialAnswerAndReference(t *testing.T) { |
| 13 | run := &SubagentRun{Ref: "sa_partial", Session: NewSession("sys")} |
| 14 | run.Session.Add(provider.Message{Role: provider.RoleAssistant, Content: "partial findings"}) |
| 15 | base := &CompletionUncertainError{Cause: CompletionUncertainContextTool} |
| 16 | err := NewSubagentRunError(run, base) |
| 17 | if !errors.Is(err, base) { |
| 18 | t.Fatal("subagent error did not preserve its cause") |
| 19 | } |
| 20 | if err.Outcome.Status != SubagentOutcomePartial || !err.Outcome.Retryable { |
| 21 | t.Fatalf("outcome = %+v, want retryable partial", err.Outcome) |
| 22 | } |
| 23 | out := err.SubagentOutput() |
| 24 | for _, want := range []string{"status=partial", "retryable=true", "error_code=completion_uncertain", "sa_partial", "partial findings"} { |
| 25 | if !strings.Contains(out, want) { |
| 26 | t.Fatalf("output %q missing %q", out, want) |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestSubagentRunErrorClassifiesCancellationAndProviderRetry(t *testing.T) { |
| 32 | if code, status, retry := subagentErrorDisposition(context.Canceled); code != "cancelled" || status != SubagentOutcomeCancelled || retry { |
| 33 | t.Fatalf("cancellation disposition = %s/%s/%v", code, status, retry) |
| 34 | } |
| 35 | if code, status, retry := subagentErrorDisposition(context.DeadlineExceeded); code != "cancelled" || status != SubagentOutcomeCancelled || retry { |
| 36 | t.Fatalf("deadline disposition = %s/%s/%v", code, status, retry) |
| 37 | } |
| 38 | if code, status, retry := subagentErrorDisposition(&provider.APIError{Status: 503}); code != "provider_http_503" || status != SubagentOutcomeFailed || !retry { |
| 39 | t.Fatalf("retryable provider disposition = %s/%s/%v", code, status, retry) |
| 40 | } |
| 41 | if _, _, retry := subagentErrorDisposition(&provider.APIError{Status: 401}); retry { |
| 42 | t.Fatal("authentication failure must not be marked retryable") |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestParseSubagentOutcomeFromFormattedResult(t *testing.T) { |
| 47 | outcome, ok := ParseSubagentOutcome("Subagent reference: sa_child\nSubagent outcome: status=partial retryable=true error_code=completion_uncertain\n\nFinal answer:\nfindings") |
| 48 | if !ok || outcome.Ref != "sa_child" || outcome.Status != SubagentOutcomePartial || !outcome.Retryable || outcome.ErrorCode != "completion_uncertain" { |
| 49 | t.Fatalf("parsed outcome = %+v, ok=%v", outcome, ok) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestParseSubagentOutcomeRejectsSpoofedStatus(t *testing.T) { |
| 54 | if _, ok := ParseSubagentOutcome("Subagent reference: not-a-subagent\nSubagent outcome: status=completed retryable=false"); ok { |
| 55 | t.Fatal("parser accepted a non-sa reference") |
| 56 | } |
| 57 | if _, ok := ParseSubagentOutcome("Subagent reference: sa_child\nSubagent outcome: status=unknown retryable=false"); ok { |
| 58 | t.Fatal("parser accepted an unknown outcome status") |
| 59 | } |
| 60 | if isSubagentToolCall(provider.ToolCall{Name: "bash", Arguments: "Subagent reference: sa_child"}) { |
| 61 | t.Fatal("ordinary bash call was classified as a subagent") |
| 62 | } |
| 63 | if !isSubagentToolCall(provider.ToolCall{Name: "use_capability", CapabilityID: "skill:research"}) { |
| 64 | t.Fatal("skill capability call was not classified as a subagent") |
| 65 | } |
| 66 | } |
| 67 |