Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Accept composite run key in token --run-id filter

Claudeb94cd32134c1
diff --git a/cli/beacon/cmd/endpoint_tokens.go b/cli/beacon/cmd/endpoint_tokens.go
index 7017f334..eb8ea161 100644
--- a/cli/beacon/cmd/endpoint_tokens.go
+++ b/cli/beacon/cmd/endpoint_tokens.go
@@ -81,7 +81,10 @@ func runEndpointTokens(cmd *cobra.Command, args []string) error {
continue
}
if runID != "" {
- if record.Event.Run == nil || record.Event.Run.RunID != runID {
+ // Accept either the bare run id or the composite provider/run_id
+ // key shown in the BY RUN rollup.
+ run := record.Event.Run
+ if run == nil || (run.RunID != runID && tokens.RunKey(run.Provider, run.RunID) != runID) {
continue
}
}
diff --git a/cli/beacon/cmd/endpoint_tokens_test.go b/cli/beacon/cmd/endpoint_tokens_test.go
index 4319940b..e61eb7ff 100644
--- a/cli/beacon/cmd/endpoint_tokens_test.go
+++ b/cli/beacon/cmd/endpoint_tokens_test.go
@@ -104,6 +104,25 @@ func TestEndpointTokensJSONReport(t *testing.T) {
}
}
+func TestEndpointTokensRunIDFilterAcceptsBareAndCompositeKeys(t *testing.T) {
+ logPath := writeTokensFixtureLog(t)
+ // The BY RUN rollup labels this run "github_actions/777"; both that
+ // composite key and the bare run id must select the run's usage.
+ for _, runID := range []string{"777", "github_actions/777"} {
+ output := runTokensCommand(t, "--log-path", logPath, "--json", "--run-id", runID)
+ var report tokens.Report
+ if err := json.Unmarshal([]byte(output), &report); err != nil {
+ t.Fatalf("unmarshal JSON report for %q: %v\n%s", runID, err, output)
+ }
+ if len(report.ByRun) != 1 || report.ByRun[0].Key != "github_actions/777" {
+ t.Fatalf("--run-id %q by_run = %#v, want the github_actions/777 run", runID, report.ByRun)
+ }
+ if report.Totals.CostUSD != 0.5 {
+ t.Fatalf("--run-id %q totals = %#v, want the run's cost", runID, report.Totals)
+ }
+ }
+}
+
func TestEndpointTokensEmptyLogSucceeds(t *testing.T) {
logPath := filepath.Join(t.TempDir(), "runtime.jsonl")
output := runTokensCommand(t, "--log-path", logPath)
diff --git a/cli/beacon/internal/endpoint/tokens/tokens.go b/cli/beacon/internal/endpoint/tokens/tokens.go
index 2a67c161..3ec54000 100644
--- a/cli/beacon/internal/endpoint/tokens/tokens.go
+++ b/cli/beacon/internal/endpoint/tokens/tokens.go
@@ -210,14 +210,7 @@ func collectUsageEvents(events []schema.Event) []*usageEvent {
ue.session = event.Session.ID
}
if event.Run != nil {
- switch {
- case event.Run.Provider != "" && event.Run.RunID != "":
- ue.run = event.Run.Provider + "/" + event.Run.RunID
- case event.Run.Provider != "":
- ue.run = event.Run.Provider
- default:
- ue.run = event.Run.RunID
- }
+ ue.run = RunKey(event.Run.Provider, event.Run.RunID)
}
if event.Trace != nil {
ue.traceID = event.Trace.ID
@@ -330,6 +323,21 @@ func setUsageField(u *Usage, field string, value float64) {
}
}
+// RunKey builds the CI run grouping key from a run's provider and id, joined as
+// "provider/run_id". It falls back to whichever part is set so an empty
+// provider never yields a leading slash. The same key labels the BY RUN rollup
+// and is accepted by the --run-id filter.
+func RunKey(provider, runID string) string {
+ switch {
+ case provider != "" && runID != "":
+ return provider + "/" + runID
+ case provider != "":
+ return provider
+ default:
+ return runID
+ }
+}
+
func addGroup(groups map[string]*Usage, key string, delta Usage) {
key = strings.TrimSpace(key)
if key == "" {