add agent brief for token diagnostics · Entire

add agent brief for token diagnostics

f5e5b53·

peyton-alt·3w ago·2 files·+292 added/-3 removed

Sessions

f1dfd807cfdcView transcript

Changes

2

68 unmodified lines

69
70
71
72
73
74
75
3 unmodified lines

79
80
81
81
82
83
84
85
86
87
88
89
90
91
92
93
94
2 unmodified lines

97
98
99
92
100
101
102
103
104
105
106
107
108
109
101
110
111
112
113
16 unmodified lines

130
131
132
133
134
135
136
137
138
139
260 unmodified lines

400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500

68 unmodified lines

func newTokensCmd() *cobra.Command {
    var jsonFlag bool
    var currentFlag bool
    var agentBriefFlag bool

cmd := &cobra.Command{
        Use:   "tokens [session-id]",
3 unmodified lines

When no session ID is provided, Entire reports on the most recently active
    session, preferring the current worktree and falling back to the newest session
    if no state matches this worktree. The report uses token and context data Entire
    already captured for the session.",
        Args: cobra.MaximumNArgs(1),
        RunE: func(cmd *cobra.Command, args []string) error {
            if jsonFlag && agentBriefFlag {
                return errors.New("--json and --agent-brief are mutually exclusive")
            }
            if currentFlag && len(args) > 0 {
                return errors.New("--current and session ID argument are mutually exclusive")
            }
2 unmodified lines

if len(args) > 0 {
                sessionID = args[0]
            }
            return runSessionTokens(cmd.Context(), cmd, sessionID, currentFlag, jsonFlag)
        }
    }

cmd.Flags().BoolVar(&jsonFlag, "json", false, "Output as JSON")
    cmd.Flags().BoolVar(&currentFlag, "current", false, "Prefer the current worktree's most recent session")
    cmd.Flags().BoolVar(&agentBriefFlag, "agent-brief", false, "Output compact next-step guidance for agents")
    return cmd
}

func runSessionTokens(ctx context.Context, cmd *cobra.Command, sessionID string, current, jsonOutput bool) error {
func runSessionTokens(ctx context.Context, cmd *cobra.Command, sessionID string, current, jsonOutput, agentBrief bool) error {
    if sessionID == "" || current {
        sessionID = strategy.FindMostRecentSession(ctx)
        if sessionID == ""
16 unmodified lines

if jsonOutput {
        return writeSessionTokensJSON(cmd.OutOrStdout(), report)
    }
    if agentBrief {
        writeSessionTokensAgentBrief(cmd.OutOrStdout(), report)
        return nil
    }
    writeSessionTokensText(cmd.OutOrStdout(), report)
    return nil
}

func writeSessionTokensAgentBrief(w io.Writer, report sessionTokensReport) {
    fmt.Fprintln(w, "Session token brief")
    fmt.Fprintf(w, "Session: %s\n", report.SessionID)
    fmt.Fprintln(w)
    fmt.Fprintln(w, agentBriefUsageLine(report.Tokens))
    fmt.Fprintln(w)
    fmt.Fprintln(w, "Next best action:")
    fmt.Fprintln(w, agentBriefNextAction(report))

signals := agentBriefSignals(report)
    if len(signals) > 0 {
        fmt.Fprintln(w)
        fmt.Fprintln(w, "Signals:")
        for _, signal := range signals {
            fmt.Fprintf(w, "- %s\n", signal)
        }
    }
}

func agentBriefUsageLine(tokens *sessionTokensUsage) string {
    if tokens == nil {
        return "Token usage: unavailable."
    }
    if tokens.CacheRead > 0 {
        return fmt.Sprintf(
            "Token usage: %s total; %s cache/context replay; %s.",
            formatTokenCount(tokens.Total),
            formatPercent(tokenPercent(tokens.CacheRead, tokens.Total)),
            formatAPICalls(tokens.APICalls),
        )
    }
    return fmt.Sprintf("Token usage: %s total; %s.", formatTokenCount(tokens.Total), formatAPICalls(tokens.APICalls))
}

func formatAPICalls(count int) string {
    if count == 1 {
        return "1 API call"
    }
    return fmt.Sprintf("%d API calls", count)
}

func agentBriefNextAction(report sessionTokensReport) string {
    switch {
    case hasTokenRecommendation(report, "context-replay-hotspot") && hasTokenRecommendation(report, "api-call-amplification"):
        return "Summarize the useful findings, then batch the next diagnostic step. Avoid more exploratory reads until you have a narrowed hypothesis."
    case hasTokenRecommendation(report, "context-replay-hotspot"):
        return "Summarize the current useful findings before continuing, and keep the next prompt narrow."
    case hasTokenRecommendation(report, "no-token-data"):
        return "Token usage is not available yet. Use this as a context check, not a spend diagnosis; continue after the next checkpoint captures usage."
    case hasTokenRecommendation(report, "subagent-heavy"):
        return "Keep the next agent or subagent task narrow with a concrete expected output; avoid broad parallel exploration."
    case hasTokenRecommendation(report, "high-context-pressure"):
        return "Preserve the useful findings and compact or restart before adding more broad context."
    case hasTokenRecommendation(report, "long-session"):
        return "Compact or restart after summarizing useful findings if older context is no longer needed."
    default:
        return "Continue normally; no high-signal token optimization is available from this session yet."
    }
}

func agentBriefSignals(report sessionTokensReport) []string {
    var signals []string
    if hasTokenRecommendation(report, "context-replay-hotspot") {
        signals = append(signals, "Cache/context replay dominates token volume.")
    }
    if hasTokenRecommendation(report, "api-call-amplification") {
        signals = append(signals, "API call count is high for one session.")
    }
    if hasTokenRecommendation(report, "subagent-heavy") {
        signals = append(signals, "Subagent usage is a meaningful part of total tokens.")
    }
    if hasTokenRecommendation(report, "high-context-pressure") {
        signals = append(signals, "Context pressure is high.")
    }
    if hasTokenRecommendation(report, "long-session") {
        signals = append(signals, "Session has crossed a long-session or checkpoint boundary.")
    }
    if hasTokenRecommendation(report, "no-token-data") {
        signals = append([]string{"Token usage is unavailable for this session."}, signals...)
    }
    if len(signals) == 0 && report.Tokens != nil {
        signals = append(signals, "No high-signal token risk detected from captured usage.")
    }
    return signals
}

func hasTokenRecommendation(report sessionTokensReport, id string) bool {
    for _, rec := range report.Recommendations {
        if rec.ID == id {
            return true
        }
    }
    return false
}

func writeTokenRecommendations(w io.Writer, recs []sessionTokensRecommendation) {
    fmt.Fprintln(w)
    fmt.Fprintln(w, "Recommendations")

Mcmd/entire/cli/session_tokens.go+111/-3

1164 unmodified lines

1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
26 unmodified lines

1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
13 unmodified lines

1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401

1164 unmodified lines

return false
}

func TestTokensCmd_AgentBriefPrioritizesNextAction(t *testing.T) {
    setupStopTestRepo(t)

ctx := context.Background()
    state := makeSessionState("test-tokens-brief", session.PhaseActive)
    state.AgentType = testAgentClaude
    state.TokenUsage = &agent.TokenUsage{
        InputTokens:         94,
        CacheCreationTokens: 122171,
        CacheReadTokens:     6052424,
        OutputTokens:        38956,
        APICallCount:        70,
    }

if err := strategy.SaveSessionState(ctx, state); err != nil {
        t.Fatalf("SaveSessionState() error = %v", err)
    }

cmd := newTokensCmd()
    var stdout bytes.Buffer
    cmd.SetOut(&stdout)
    cmd.SetArgs([]string{"test-tokens-brief", "--agent-brief"})

if err := cmd.ExecuteContext(ctx); err != nil {
        t.Fatalf("expected no error, got: %v", err)
    }

out := stdout.String()
    checks := []string{
        "Session token brief",
        "Session: test-tokens-brief",
        "Token usage: 6213.6k total; 97.4% cache/context replay; 70 API calls.",
        "Next best action:",
        "Summarize the useful findings, then batch the next diagnostic step.",
        "Signals:",
        "- Cache/context replay dominates token volume.",
        "- API call count is high for one session.",
    }
    for _, check := range checks {
        if !strings.Contains(out, check) {
            t.Errorf("expected %q in output, got:
%s", check, out)
        }
    }
    if strings.Contains(out, "Recommendations") {
        t.Fatalf("expected agent brief to omit regular recommendations section, got:
%s", out)
    }
    if strings.Contains(out, "Likely contributors") {
        t.Fatalf("expected agent brief to omit contributor detail, got:
%s", out)
    }
}

func TestTokensCmd_AgentBriefHighCacheReplayWithoutHighAPICalls(t *testing.T) {
    setupStopTestRepo(t)

ctx := context.Background()
    state := makeSessionState("test-tokens-brief-cache-only", session.PhaseActive)
    state.AgentType = testAgentClaude
    state.TokenUsage = &agent.TokenUsage{
        InputTokens:     27_892,
        CacheReadTokens: 608_896,
        OutputTokens:    865,
        APICallCount:    3,
    }

if err := strategy.SaveSessionState(ctx, state); err != nil {
        t.Fatalf("SaveSessionState() error = %v", err)
    }

cmd := newTokensCmd()
    var stdout bytes.Buffer
    cmd.SetOut(&stdout)
    cmd.SetArgs([]string{"test-tokens-brief-cache-only", "--agent-brief"})

if err := cmd.ExecuteContext(ctx); err != nil {
        t.Fatalf("expected no error, got: %v", err)
    }

out := stdout.String()
    checks := []string{
        "Token usage: 637.7k total; 95.5% cache/context replay; 3 API calls.",
        "Summarize the current useful findings before continuing, and keep the next prompt narrow.",
        "- Cache/context replay dominates token volume.",
    }
    for _, check := range checks {
        if !strings.Contains(out, check) {
            t.Errorf("expected %q in output, got:
%s", check, out)
        }
    }
    if strings.Contains(out, "Continue normally") {
        t.Fatalf("expected high cache replay to avoid continue-normally action, got:
%s", out)
    }
}

func TestTokensCmd_AgentBriefNoTokenData(t *testing.T) {
    setupStopTestRepo(t)

ctx := context.Background()
    state := makeSessionState("test-tokens-brief-missing", session.PhaseActive)
    state.AgentType = testAgentGemini
    state.ContextTokens = 9000
    state.ContextWindowSize = 10000

if err := strategy.SaveSessionState(ctx, state); err != nil {
        t.Fatalf("SaveSessionState() error = %v", err)
    }

cmd := newTokensCmd()
    var stdout bytes.Buffer
    cmd.SetOut(&stdout)
    cmd.SetArgs([]string{"test-tokens-brief-missing", "--agent-brief"})

if err := cmd.ExecuteContext(ctx); err != nil {
        t.Fatalf("expected no error, got: %v", err)
    }

out := stdout.String()
    checks := []string{
        "Session token brief",
        "Session: test-tokens-brief-missing",
        "Token usage: unavailable.",
        "Next best action:",
        "Token usage is not available yet.",
        "Signals:",
        "- Token usage is unavailable for this session.",
        "- Context pressure is high.",
    }
    for _, check := range checks {
        if !strings.Contains(out, check) {
            t.Errorf("expected %q in output, got:
%s", check, out)
        }
    }
}

func TestSessionsCmd_TokensSubcommand(t *testing.T) {
    setupStopTestRepo(t)

26 unmodified lines

}

func TestSessionsCmd_TokensSubcommandAgentBrief(t *testing.T) {
    setupStopTestRepo(t)

ctx := context.Background()
    state := makeSessionState("test-tokens-subcommand-brief", session.PhaseActive)
    state.TokenUsage = &agent.TokenUsage{
        InputTokens:  1200,
        OutputTokens: 300,
        APICallCount: 2,
    }

if err := strategy.SaveSessionState(ctx, state); err != nil {
        t.Fatalf("SaveSessionState() error = %v", err)
    }

cmd := newSessionsCmd()
    var stdout bytes.Buffer
    cmd.SetOut(&stdout)
    cmd.SetArgs([]string{"tokens", "test-tokens-subcommand-brief", "--agent-brief"})

if err := cmd.ExecuteContext(ctx); err != nil {
        t.Fatalf("expected no error, got: %v", err)
    }

out := stdout.String()
    if !strings.Contains(out, "Session token brief") {
        t.Fatalf("expected agent brief output, got:
%s", out)
    }
    if !strings.Contains(out, "Token usage: 1.5k total") {
        t.Fatalf("expected token summary in brief, got:
%s", out)
    }
}

func TestSessionsCmd_HelpIncludesTokensSubcommand(t *testing.T) {
    cmd := newSessionsCmd()
    var stdout bytes.Buffer
13 unmodified lines

}

func TestTokensCmd_JSONAndAgentBriefAreMutuallyExclusive(t *testing.T) {
    setupStopTestRepo(t)

cmd := newTokensCmd()
    cmd.SetArgs([]string{"test-session", "--json", "--agent-brief"})

err := cmd.ExecuteContext(context.Background())
    if err == nil {
        t.Fatal("expected error for --json with --agent-brief")
    }
    if !strings.Contains(err.Error(), "mutually exclusive") {
        t.Fatalf("expected mutually exclusive error, got: %v", err)
    }
}

func TestTokensCmd_PrioritizesContextReplayHotspot(t *testing.T) {
    setupStopTestRepo(t)