-
Notifications
You must be signed in to change notification settings - Fork 472
Label forecast output as AI Credits and cache AIC across runs #48797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "os" | ||
| "path/filepath" | ||
| "time" | ||
|
|
||
| "github.com/github/gh-aw/pkg/constants" | ||
| ) | ||
|
|
||
| // forecastAICCacheFileName is a forecast-specific cache file written to each run | ||
| // folder alongside the downloaded usage artifact. It stores only the computed AI | ||
| // Credits (AIC) value so that repeated forecast runs can reload the metric without | ||
| // re-scanning the run directory or re-parsing the usage artifact. | ||
| // | ||
| // A dedicated file is used (rather than the shared run_summary.json) because | ||
| // run_summary.json acts as a "fully processed" marker for `gh aw logs`/`audit`; | ||
| // writing a partial summary there would make those commands serve incomplete data. | ||
| const forecastAICCacheFileName = "forecast_aic.json" | ||
|
|
||
| // forecastAICCache is the on-disk payload cached per run for forecasting. | ||
| type forecastAICCache struct { | ||
| CLIVersion string `json:"cli_version"` // CLI version used to compute the AIC (cache invalidation key) | ||
| RunID int64 `json:"run_id"` // Workflow run database ID | ||
| AIC float64 `json:"ai_credits"` // Total AI Credits consumed by the run | ||
| CachedAt time.Time `json:"cached_at"` // When this cache entry was written | ||
| } | ||
|
|
||
| // loadForecastAICCache returns the cached AIC for a run when a valid, version-matching | ||
| // forecast cache file exists. The second return value reports whether a usable cache | ||
| // entry was found. Stale entries (version mismatch, mismatched run ID, or non-positive | ||
| // AIC) are treated as misses so the caller recomputes. | ||
| func loadForecastAICCache(dir string, runID int64) (float64, bool) { | ||
| path := filepath.Join(dir, forecastAICCacheFileName) | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return 0, false | ||
| } | ||
| var c forecastAICCache | ||
| if err := json.Unmarshal(data, &c); err != nil { | ||
| return 0, false | ||
| } | ||
| if c.CLIVersion != GetVersion() || c.RunID != runID || c.AIC <= 0 { | ||
| return 0, false | ||
| } | ||
| return c.AIC, true | ||
| } | ||
|
|
||
| // saveForecastAICCache writes the computed AIC for a run to the forecast cache file. | ||
| // It is best-effort: any error (including a non-positive AIC) is silently ignored so a | ||
| // cache-write failure never fails the forecast. The run directory is expected to already | ||
| // exist (it holds the downloaded usage artifact), but is created defensively. | ||
| func saveForecastAICCache(dir string, runID int64, aic float64) { | ||
| if aic <= 0 { | ||
| return | ||
| } | ||
| c := forecastAICCache{ | ||
| CLIVersion: GetVersion(), | ||
| RunID: runID, | ||
| AIC: aic, | ||
| CachedAt: time.Now().UTC(), | ||
| } | ||
| data, err := json.MarshalIndent(&c, "", " ") | ||
| if err != nil { | ||
| forecastRunLog.Printf("Failed to marshal forecast AIC cache for run %d: %v", runID, err) | ||
| return | ||
| } | ||
| if err := os.MkdirAll(dir, constants.DirPermPublic); err != nil { | ||
| forecastRunLog.Printf("Failed to create dir for forecast AIC cache for run %d: %v", runID, err) | ||
| return | ||
| } | ||
| path := filepath.Join(dir, forecastAICCacheFileName) | ||
| if err := os.WriteFile(path, data, constants.FilePermPublic); err != nil { | ||
| forecastRunLog.Printf("Failed to write forecast AIC cache for run %d: %v", runID, err) | ||
| return | ||
| } | ||
| forecastRunLog.Printf("Wrote forecast AIC cache for run %d: aic=%.3f (path=%s)", runID, aic, path) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| //go:build !integration | ||
|
|
||
| package cli | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestForecastAICCache_RoundTrip(t *testing.T) { | ||
| dir := t.TempDir() | ||
| const runID int64 = 12345 | ||
|
|
||
| // Miss when no cache file exists. | ||
| if _, ok := loadForecastAICCache(dir, runID); ok { | ||
| t.Fatalf("expected cache miss for empty dir") | ||
| } | ||
|
|
||
| // Save then load returns the same value. | ||
| saveForecastAICCache(dir, runID, 42.5) | ||
| got, ok := loadForecastAICCache(dir, runID) | ||
| require.True(t, ok, "expected cache hit after save") | ||
| assert.InDelta(t, 42.5, got, 1e-9) | ||
|
|
||
| // The cache file records the current CLI version. | ||
| data, err := os.ReadFile(filepath.Join(dir, forecastAICCacheFileName)) | ||
| require.NoError(t, err) | ||
| var c forecastAICCache | ||
| require.NoError(t, json.Unmarshal(data, &c)) | ||
| assert.Equal(t, GetVersion(), c.CLIVersion) | ||
| assert.Equal(t, runID, c.RunID) | ||
| } | ||
|
|
||
| func TestForecastAICCache_NonPositiveNotWritten(t *testing.T) { | ||
| dir := t.TempDir() | ||
| saveForecastAICCache(dir, 1, 0) | ||
| saveForecastAICCache(dir, 1, -5) | ||
| if _, err := os.Stat(filepath.Join(dir, forecastAICCacheFileName)); !os.IsNotExist(err) { | ||
| t.Fatalf("expected no cache file for non-positive AIC") | ||
| } | ||
| } | ||
|
|
||
| func TestForecastAICCache_InvalidatedOnVersionMismatch(t *testing.T) { | ||
| dir := t.TempDir() | ||
| const runID int64 = 999 | ||
| c := forecastAICCache{CLIVersion: "some-old-version", RunID: runID, AIC: 10} | ||
| data, err := json.MarshalIndent(&c, "", " ") | ||
| require.NoError(t, err) | ||
| require.NoError(t, os.WriteFile(filepath.Join(dir, forecastAICCacheFileName), data, 0o644)) | ||
|
|
||
| if _, ok := loadForecastAICCache(dir, runID); ok { | ||
| t.Fatalf("expected cache miss on CLI version mismatch") | ||
| } | ||
| } | ||
|
|
||
| func TestForecastAICCache_MismatchedRunID(t *testing.T) { | ||
| dir := t.TempDir() | ||
| saveForecastAICCache(dir, 100, 7.0) | ||
| if _, ok := loadForecastAICCache(dir, 200); ok { | ||
| t.Fatalf("expected cache miss when run ID differs") | ||
| } | ||
| } |
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
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
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 4536c1a by isolating the forecast cache tests in a temporary working directory in
pkg/cli/forecast_test.go.