| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestSummarizeTrajectoryJoinsCognitionPerRound(t *testing.T) { |
| 9 | path := writeTrajectory(t, "cog.trajectory.jsonl", []string{ |
| 10 | `{"seq":1,"ts":1000,"event":{"kind":"turn_started"}}`, |
| 11 | // Round 1: a 9s gap that bought 900 reasoning tokens (slow round). |
| 12 | `{"seq":2,"ts":9500,"event":{"kind":"usage","usage":{"source":"executor","promptTokens":15000,"reasoningTokens":900,"completionTokens":300}}}`, |
| 13 | `{"seq":3,"ts":10000,"event":{"kind":"tool_dispatch","tool":{"id":"a","name":"bash"}}}`, |
| 14 | `{"seq":4,"ts":10400,"event":{"kind":"tool_result","tool":{"id":"a","name":"bash","durationMs":300}}}`, |
| 15 | // Round 2: a cheap gap into a research delegation; the subagent's own |
| 16 | // usage must not pollute the executor cognition. |
| 17 | `{"seq":5,"ts":11500,"event":{"kind":"usage","usage":{"source":"executor","promptTokens":16000,"reasoningTokens":50,"completionTokens":100}}}`, |
| 18 | `{"seq":6,"ts":12000,"event":{"kind":"tool_dispatch","tool":{"id":"b","name":"research"}}}`, |
| 19 | `{"seq":7,"ts":16000,"event":{"kind":"usage","usage":{"source":"subagent","promptTokens":90000,"reasoningTokens":9999,"completionTokens":9999}}}`, |
| 20 | `{"seq":8,"ts":17000,"event":{"kind":"tool_result","tool":{"id":"b","name":"research","durationMs":5000}}}`, |
| 21 | // Final answer round. |
| 22 | `{"seq":9,"ts":18000,"event":{"kind":"usage","usage":{"source":"executor","promptTokens":17000,"reasoningTokens":20,"completionTokens":40}}}`, |
| 23 | `{"seq":10,"ts":19000,"event":{"kind":"turn_done"}}`, |
| 24 | }) |
| 25 | s, err := summarizeTrajectory(path) |
| 26 | if err != nil { |
| 27 | t.Fatalf("summarizeTrajectory: %v", err) |
| 28 | } |
| 29 | if s.ReasoningTokensTotal != 970 || s.CompletionTokensTotal != 440 { |
| 30 | t.Errorf("executor totals = %d/%d, want 970/440 (subagent excluded)", |
| 31 | s.ReasoningTokensTotal, s.CompletionTokensTotal) |
| 32 | } |
| 33 | if len(s.Rounds) != 3 { |
| 34 | t.Fatalf("got %d round digests, want 3", len(s.Rounds)) |
| 35 | } |
| 36 | r1, r2, r3 := s.Rounds[0], s.Rounds[1], s.Rounds[2] |
| 37 | if r1.GapMs != 9000 || r1.ReasoningTokens != 900 || r1.PromptTokens != 15000 || r1.ToolMs != 300 { |
| 38 | t.Errorf("round 1 digest = %+v, want gap 9000 / reasoning 900 / prompt 15000 / tool 300", r1) |
| 39 | } |
| 40 | if r2.Outcome != "delegation" || r2.ReasoningTokens != 50 || r2.ToolMs != 5000 { |
| 41 | t.Errorf("round 2 digest = %+v, want delegation / reasoning 50 / tool 5000", r2) |
| 42 | } |
| 43 | if len(r2.Actions) != 1 || r2.Actions[0] != "research" { |
| 44 | t.Errorf("round 2 actions = %v, want [research]", r2.Actions) |
| 45 | } |
| 46 | if r3.Outcome != "finalization" || r3.ReasoningTokens != 20 || r3.GapMs != 2000 { |
| 47 | t.Errorf("round 3 digest = %+v, want finalization / reasoning 20 / gap 2000", r3) |
| 48 | } |
| 49 | if s.SlowRounds != 1 || s.SlowRoundGapMs != 9000 || s.SlowRoundReasoningTokens != 900 { |
| 50 | t.Errorf("slow census = %d/%d/%d, want 1/9000/900", |
| 51 | s.SlowRounds, s.SlowRoundGapMs, s.SlowRoundReasoningTokens) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func TestSlowRoundCensusExcludesRecoveryGaps(t *testing.T) { |
| 56 | path := writeTrajectory(t, "recovery.trajectory.jsonl", []string{ |
| 57 | `{"seq":1,"ts":1000,"event":{"kind":"turn_started"}}`, |
| 58 | // A 12s gap dominated by a provider retry is recovery, not cognition. |
| 59 | `{"seq":2,"ts":3000,"event":{"kind":"retrying","retryScope":"stream"}}`, |
| 60 | `{"seq":3,"ts":13000,"event":{"kind":"tool_dispatch","tool":{"id":"a","name":"bash"}}}`, |
| 61 | `{"seq":4,"ts":13300,"event":{"kind":"tool_result","tool":{"id":"a","name":"bash","durationMs":200}}}`, |
| 62 | `{"seq":5,"ts":14000,"event":{"kind":"turn_done"}}`, |
| 63 | }) |
| 64 | s, err := summarizeTrajectory(path) |
| 65 | if err != nil { |
| 66 | t.Fatalf("summarizeTrajectory: %v", err) |
| 67 | } |
| 68 | if s.SlowRounds != 0 { |
| 69 | t.Fatalf("slow rounds = %d, want 0 (recovery gap is not a cognition purchase)", s.SlowRounds) |
| 70 | } |
| 71 | if len(s.Rounds) == 0 || s.Rounds[0].Outcome != "recovery" || s.Rounds[0].GapMs != 12000 { |
| 72 | t.Fatalf("round digest = %+v, want recovery round with its gap still booked", s.Rounds) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func TestRenderCognitionPricesThinking(t *testing.T) { |
| 77 | results := []result{ |
| 78 | {Passed: true, Trajectory: &trajectorySummary{ |
| 79 | ReasoningTokensTotal: 4000, CompletionTokensTotal: 6000, |
| 80 | SlowRounds: 1, SlowRoundGapMs: 9000, SlowRoundReasoningTokens: 900, |
| 81 | ModelGapTotalMs: 30000, |
| 82 | Rounds: []roundDigest{ |
| 83 | {Index: 1, Outcome: "evidence_gain", GapMs: 2000, ReasoningTokens: 100, CompletionTokens: 300}, |
| 84 | {Index: 2, Outcome: "delegation", GapMs: 1500, ToolMs: 5000, ReasoningTokens: 50, CompletionTokens: 100}, |
| 85 | }, |
| 86 | }}, |
| 87 | {Passed: true, Trajectory: &trajectorySummary{}}, // no rounds: excluded |
| 88 | } |
| 89 | got := renderCognition(results) |
| 90 | for _, want := range []string{ |
| 91 | "**Cognition** (1 recorded runs)", |
| 92 | "**reasoning** 4,000 tok", |
| 93 | "**2,000 reasoning/solved**", |
| 94 | "**slow rounds** (≥8s) 1 = 30% of model time, 900 reasoning tok", |
| 95 | "**delegation** 1 rounds (5.0s in subagents)", |
| 96 | } { |
| 97 | if !strings.Contains(got, want) { |
| 98 | t.Errorf("render missing %q in:\n%s", want, got) |
| 99 | } |
| 100 | } |
| 101 | if renderCognition(nil) != "" { |
| 102 | t.Error("no runs must render nothing") |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func TestSummarizeTrajectoryJoinsDeniedDelegationTime(t *testing.T) { |
| 107 | path := writeTrajectory(t, "admit.trajectory.jsonl", []string{ |
| 108 | `{"seq":1,"ts":1000,"event":{"kind":"turn_started"}}`, |
| 109 | `{"seq":2,"ts":2000,"event":{"kind":"tool_dispatch","tool":{"id":"a","name":"research"}}}`, |
| 110 | `{"seq":3,"ts":9000,"event":{"kind":"tool_result","tool":{"id":"a","name":"research","durationMs":7000}}}`, |
| 111 | `{"seq":4,"ts":9100,"delegation_admission":{"tool":"research","verdict":"deny","reason":"local_fix_no_external_need"}}`, |
| 112 | `{"seq":5,"ts":10000,"event":{"kind":"turn_done"}}`, |
| 113 | }) |
| 114 | s, err := summarizeTrajectory(path) |
| 115 | if err != nil { |
| 116 | t.Fatalf("summarizeTrajectory: %v", err) |
| 117 | } |
| 118 | if s.DelegationCalls != 1 || s.DelegationDenies != 1 || s.DeniedDelegationMs != 7000 { |
| 119 | t.Fatalf("admission join = calls %d denies %d denied_ms %d, want 1/1/7000", |
| 120 | s.DelegationCalls, s.DelegationDenies, s.DeniedDelegationMs) |
| 121 | } |
| 122 | got := renderDelegationAdmission([]result{{Trajectory: s}}) |
| 123 | for _, want := range []string{"**1** gated calls", "**would deny** 1 (100%)", "denied calls** 7.0s"} { |
| 124 | if !strings.Contains(got, want) { |
| 125 | t.Errorf("render missing %q in:\n%s", want, got) |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 |