Forecast: prefer usage artifact for token AIC, retain legacy agent fallback#37427
Merged
Merged
Conversation
Co-authored-by: pelikhan <[email protected]>
Co-authored-by: pelikhan <[email protected]>
Copilot
AI
changed the title
Use usage artifact first for forecast token retrieval
Forecast: prefer Jun 6, 2026
usage artifact for token AIC, retain legacy agent fallback
Copilot created this pull request from a session on behalf of
pelikhan
June 6, 2026 22:19
View session
pelikhan
approved these changes
Jun 6, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the forecast AIC loading path to prefer a lightweight usage artifact for token usage data, while keeping fallbacks to the heavier agent artifacts for backward compatibility.
Changes:
- Extended token-usage file discovery to recognize
usage/agent/token_usage.jsonland accept bothtoken-usage.jsonlandtoken_usage.jsonlnaming variants. - Updated forecast cached-run AIC resolution to attempt artifact downloads in the order:
usage→agent→agent-artifacts, returning the first non-zero AIC found. - Added unit tests covering the usage-first path and a fallback scenario.
Show a summary per file
| File | Description |
|---|---|
| pkg/cli/token_usage.go | Adds usage-artifact-aware token usage file discovery and supports underscore filename variant. |
| pkg/cli/token_usage_test.go | Adds a test for finding token usage in the usage artifact layout. |
| pkg/cli/forecast.go | Implements usage-first artifact download order for AIC resolution when cache is missing/insufficient. |
| pkg/cli/forecast_test.go | Adds tests validating usage-first behavior and fallback sequencing. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 4/4 changed files
- Comments generated: 5
Comment on lines
279
to
+286
| // findTokenUsageFile searches for token-usage.jsonl in the run directory | ||
| func findTokenUsageFile(runDir string) string { | ||
| usageArtifactCandidate := filepath.Join(runDir, "usage", "agent", "token_usage.jsonl") | ||
| if _, err := os.Stat(usageArtifactCandidate); err == nil { | ||
| tokenUsageLog.Printf("Found token usage file in usage artifact: %s", usageArtifactCandidate) | ||
| return usageArtifactCandidate | ||
| } | ||
|
|
Comment on lines
+759
to
+773
| usageFirstFilters := [][]string{ | ||
| {"usage"}, | ||
| {constants.AgentArtifactName}, | ||
| {"agent-artifacts"}, | ||
| } | ||
|
|
||
| for _, filter := range usageFirstFilters { | ||
| if err := forecastDownloadRunArtifacts(context.Background(), runID, dir, verbose, "", "", "", filter); err != nil && !errors.Is(err, ErrNoArtifacts) { | ||
| continue | ||
| } | ||
| tokenUsage, err := forecastAnalyzeTokenUsage(dir, verbose) | ||
| if err != nil || tokenUsage == nil || tokenUsage.TotalAIC <= 0 { | ||
| continue | ||
| } | ||
| return tokenUsage.TotalAIC |
Comment on lines
+766
to
+768
| if err := forecastDownloadRunArtifacts(context.Background(), runID, dir, verbose, "", "", "", filter); err != nil && !errors.Is(err, ErrNoArtifacts) { | ||
| continue | ||
| } |
| require.Equal(t, []string{"usage"}, downloaded) | ||
| } | ||
|
|
||
| func TestLoadCachedRunAIC_FallsBackToLegacyAgentArtifacts(t *testing.T) { |
Comment on lines
+759
to
+763
| usageFirstFilters := [][]string{ | ||
| {"usage"}, | ||
| {constants.AgentArtifactName}, | ||
| {"agent-artifacts"}, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Forecast currently depends on token data that often required downloading the heavier
agentartifact when cache was cold. This change makes forecast fetch token usage from the lightweightusageartifact first, while preserving legacy compatibility paths.What changed
usageagentagent-artifacts(legacy)usage/agent/token_usage.jsonltoken-usage.jsonltoken_usage.jsonlusagetoagentExample (new fallback order in forecast AIC load path)