add token profile diagnostics · Entire

add token profile diagnostics

9ac2ec7·

peyton-alt·3w ago·3 files·+645 added/-0 removed

Sessions

cdebea3708fdView transcript

Changes

3

83 unmodified lines

84
85
86
87
88
89
90

83 unmodified lines

// Noun groups (canonical homes for subcommands).
    cmd.AddCommand(newSessionsCmd())        // 'session' (with 'sessions' as Cobra alias)
    cmd.AddCommand(newCheckpointGroupCmd()) // 'checkpoint' / 'cp' / 'checkpoints'
    cmd.AddCommand(newTokensGroupCmd())     // 'tokens'
    cmd.AddCommand(newAgentGroupCmd())      // 'agent'
    cmd.AddCommand(newAuthCmd())            // 'auth'
    cmd.AddCommand(newDoctorCmd())          // 'doctor' (group: trace/logs/bundle)

Mcmd/entire/cli/root.go+1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

package cli

import (
    "context"
    "encoding/json"
    "errors"
    "fmt"
    "io"

"github.com/entireio/cli/cmd/entire/cli/agent"
    "github.com/entireio/cli/cmd/entire/cli/checkpoint"
    "github.com/entireio/cli/cmd/entire/cli/checkpoint/id"
    "github.com/spf13/cobra"
)

type tokensProfileReport struct {
    Source                   string                        `json:"source"`
    CheckpointsAvailable     int                           `json:"checkpoints_available"`
    CheckpointsAnalyzed      int                           `json:"checkpoints_analyzed"`
    CheckpointsWithTokenData int                           `json:"checkpoints_with_token_data"`
    MissingTokenData         int                           `json:"missing_token_data"`
    MetadataReadWarnings     int                           `json:"metadata_read_warnings,omitempty"`
    Tokens                   *sessionTokensUsage           `json:"tokens,omitempty"`
    Signals                  []tokensProfileSignal         `json:"signals,omitempty"`
    Recommendations          []sessionTokensRecommendation `json:"recommendations,omitempty"`
    Limitations              []string                      `json:"limitations,omitempty"`
}

type tokensProfileSignal struct {
    ID            string   `json:"id"`
    Label         string   `json:"label"`
    Count         int      `json:"count"`
    Percent       int      `json:"percent"`
    CheckpointIDs []string `json:"checkpoint_ids,omitempty"`
}

type tokensProfileSignalDefinition struct {
    id    string
    label string
}

var tokensProfileSignalDefinitions = []tokensProfileSignalDefinition{
    {id: "context-replay-hotspot", label: "Cache/context replay hotspot"},
    {id: "api-call-amplification", label: "API call amplification"},
    {id: "subagent-heavy", label: "Subagent-heavy sessions"},
    {id: "missing-token-data", label: "Missing token data"},
}

func newTokensGroupCmd() *cobra.Command {
    cmd := &cobra.Command{
        Use:   "tokens",
        Short: "Analyze token usage across sessions and checkpoints",
        Long: `Analyze token usage across sessions and checkpoints.

Commands:
  profile  Aggregate token usage across committed checkpoints

Examples:
  entire tokens profile
  entire tokens profile --json`,
        RunE: func(cmd *cobra.Command, _ []string) error {
            return cmd.Help()
        },
    }

cmd.AddCommand(newTokensProfileCmd())
    return cmd
}

func newTokensProfileCmd() *cobra.Command {
    var jsonFlag bool
    var limitFlag int
    var allFlag bool

cmd := &cobra.Command{
        Use:   "profile",
        Short: "Aggregate token usage and recommendations across checkpoint history",
        Long: `Aggregate token usage and recommendations across committed checkpoint history.

The profile reads committed checkpoint metadata only. It does not inspect
transcripts or source files, so it is deterministic and avoids adding token
cost while diagnosing token usage. By default it scans the latest 50 committed
checkpoints; use --limit or --all to change the scope.`,
        Args: cobra.NoArgs,
        RunE: func(cmd *cobra.Command, _ []string) error {
            limit := limitFlag
            if allFlag {
                limit = 0
            } else if limit <= 0 {
                return errors.New("--limit must be positive unless --all is used")
            }
            return runTokensProfile(cmd.Context(), cmd, jsonFlag, limit)
        },
    }

cmd.Flags().BoolVar(&jsonFlag, "json", false, "Output as JSON")
    cmd.Flags().IntVar(&limitFlag, "limit", 50, "Maximum committed checkpoints to analyze")
    cmd.Flags().BoolVar(&allFlag, "all", false, "Analyze all committed checkpoints")
    return cmd
}

func runTokensProfile(ctx context.Context, cmd *cobra.Command, jsonOutput bool, limit int) error {
    repo, err := openRepository(ctx)
    if err != nil {
        cmd.SilenceUsage = true
        fmt.Fprintln(cmd.ErrOrStderr(), "Not a git repository.")
        return NewSilentError(err)
    }
    defer repo.Close()

store := checkpoint.NewCommittedReadStore(ctx, repo)
    infos, err := store.ListCommitted(ctx)
    if err != nil {
        return fmt.Errorf("failed to list checkpoints: %w", err)
    }

report, err := buildTokensProfileReport(ctx, store, infos, limit)
    if err != nil {
        return err
    }

if jsonOutput {
        return writeTokensProfileJSON(cmd.OutOrStdout(), report)
    }
    writeTokensProfileText(cmd.OutOrStdout(), report)
    return nil
}

// Remaining functions continue...