add checkpoint token comparison · Entire
add checkpoint token comparison
8fd1961·
peyton-alt·3w ago·2 files·+569 added/-13 removed
Sessions
cea04e74bd49View transcript
Changes
2
cmd/entire/cli
Mcheckpoint_tokens.go+182/-13
Msessions_test.go+387
4 unmodified lines
16 unmodified lines
4 unmodified lines
encoding/json fmt io strconv strings
\t\t\t\t\t\t\t\t\t
"github.com/entireio/cli/cmd/entire/cli/agent"
define the struct for session tokens report
type checkpointTokensReport struct {
\t\t\t\tContext *sessionTokensContext json:"context,omitempty"
\t\t\t\tContributors []sessionTokensContributor json:"contributors,omitempty"
\t\t\t\tRecommendations []sessionTokensRecommendation json:"recommendations,omitempty"
\t\t\t\tComparison *checkpointTokensComparison json:"comparison,omitempty"
\t\t\t\tLimitations []string json:"limitations,omitempty"
}
type checkpointTokensComparison struct {
\t\t\t\tBaselineCheckpointID string json:"baseline_checkpoint_id"
\t\t\t\tTargetCheckpointID string json:"target_checkpoint_id"
\t\t\t\tStatus string json:"status"
\t\t\t\tTotal *checkpointTokensMetricDelta json:"total,omitempty"
\t\t\t\tCacheRead *checkpointTokensMetricDelta json:"cache_read,omitempty"
\t\t\t\tAPICalls *checkpointTokensMetricDelta json:"api_calls,omitempty"
\t\t\t\tQualification string json:"qualification"
\t\t\t\tLimitations []string json:"limitations,omitempty"
}
type checkpointTokensMetricDelta struct {
\t\t\t\tBaseline int json:"baseline"
\t\t\t\tCurrent int json:"current"
\t\t\t\tChange int json:"change"
\t\t\t\tChangePercent *float64 json:"change_percent,omitempty"
\t\t\t\tDirection string json:"direction"
}
const ( \t\t\t\tcheckpointComparisonStatusUnavailable = "unavailable" \t\t\t\tcheckpointComparisonStatusObservedReduction = "observed_reduction" \t\t\t\tcheckpointComparisonStatusObservedIncrease = "observed_increase" \t\t\t\tcheckpointComparisonStatusObservedNoChange = "observed_no_change" )
func newCheckpointTokensCmd() *cobra.Command { \t\t\t\tvar jsonFlag bool \t\t\t\tvar compareFlag string
\t\t\t\tcmd := &cobra.Command{
\t\t\t\t\tUse: "tokens
\t\t\t\tcmd.Flags().BoolVar(&jsonFlag, "json", false, "Output as JSON") \t\t\t\tcmd.Flags().StringVar(&compareFlag, "compare", "", "Compare against a baseline checkpoint ID") \t\t\t\treturn cmd }
func runCheckpointTokens(ctx context.Context, cmd *cobra.Command, checkpointIDPrefix string, jsonOutput bool) error { \t\t\t\tcpID, lookup, err := resolveExplainCheckpointID(ctx, cmd.ErrOrStderr(), explainExportOptions{target: checkpointIDPrefix}) \t\t\t\tif err != nil { \t\t\t\t\treturn tokenCommandError(err) \t\t\t\t}
\t\t\t\tif comparePrefix != "" { \t\t\t\t\tb, baselineLookup, err := loadCheckpointTokensReport(ctx, cmd, comparePrefix) \t\t\t\t\tif baselineLookup != nil { \t\t\t\t\t\tdefer baselineLookup.Close() \t\t\t\t\t} \t\t\t\t\tif err != nil { \t\t\t\t\t\treturn tokenCommandError(err) \t\t\t\t}\nif jsonOutput { \t\t\t\t\t\twriteCheckpointTokensJSON(cmd.OutOrStdout(), report) \t\t\t\t} \t\t\t\twriteCheckpointTokensText(cmd.OutOrStdout(), report) \t\t\t\treturn nil }
// Load checkpoint tokens report func loadCheckpointTokensReport(ctx context.Context, cmd *cobra.Command, checkpointIDPrefix string) (checkpointTokensReport, *explainCheckpointLookup, error) { \t\t\t\tcpID, lookup, err := resolveExplainCheckpointID(ctx, cmd.ErrOrStderr(), explainExportOptions{target: checkpointIDPrefix}) \t\t\t\tif err != nil { \t\t\t\t\treturn checkpointTokensReport{}, lookup, err \t\t\t\t}
\t\t\t\tsummary, err := lookup.store.ReadCommitted(ctx, cpID) \t\t\t\tif err != nil { \t\t\t\t\treturn tokenCommandError(fmt.Errorf("failed to read checkpoint: %w", err)) \t\t\t\t}
\t\t\t\tif summary == nil || len(summary.Sessions) == 0 { \t\t\t\t\tcmd.SilenceUsage = true \t\t\t\t\tfmt.Fprintln(cmd.ErrOrStderr(), "Checkpoint not found.") \t\t\t\t\treturn NewSilentError(fmt.Errorf("%w: %s", checkpoint.ErrCheckpointNotFound, checkpointIDPrefix)) \t\t\t\t}
\t\t\t\tmetas, metadataWarnings, err := readCheckpointTokenSessionMetadata(ctx, lookup.store, cpID, len(summary.Sessions)) \t\t\t\tif err != nil { \t\t\t\t\treturn tokenCommandError(err) \t\t\t\t}
\t\t\t\treport := buildCheckpointTokensReport(cpID, summary, metas, metadataWarnings) \t\t\t\treturn report, lookup, nil }