Skip to content

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

ci: enrich GitHub Actions run context for Claude Code telemetry

Clauded6d6c2b67513
diff --git a/cli/beacon/internal/ci/session.go b/cli/beacon/internal/ci/session.go
index 274b99cd..45157458 100644
--- a/cli/beacon/internal/ci/session.go
+++ b/cli/beacon/internal/ci/session.go
@@ -296,15 +296,34 @@ func DefaultLogPath() string {
func detectRunInfo() *schema.RunInfo {
if os.Getenv("GITHUB_ACTIONS") == "true" {
- return &schema.RunInfo{
- Provider: "github_actions",
- RunID: os.Getenv("GITHUB_RUN_ID"),
- Workflow: os.Getenv("GITHUB_WORKFLOW"),
- Commit: os.Getenv("GITHUB_SHA"),
- PR: os.Getenv("GITHUB_REF"),
- Actor: os.Getenv("GITHUB_ACTOR"),
- Ephemeral: true,
+ eventName := os.Getenv("GITHUB_EVENT_NAME")
+ info := &schema.RunInfo{
+ Provider: "github_actions",
+ RunID: os.Getenv("GITHUB_RUN_ID"),
+ RunAttempt: os.Getenv("GITHUB_RUN_ATTEMPT"),
+ Workflow: os.Getenv("GITHUB_WORKFLOW"),
+ Job: os.Getenv("GITHUB_JOB"),
+ EventName: eventName,
+ Commit: os.Getenv("GITHUB_SHA"),
+ Repository: os.Getenv("GITHUB_REPOSITORY"),
+ Actor: os.Getenv("GITHUB_ACTOR"),
+ Ephemeral: true,
}
+ // Prefer the pull-request head ref as the human-readable branch; fall
+ // back to the pushed ref's short name on non-PR events.
+ if head := strings.TrimSpace(os.Getenv("GITHUB_HEAD_REF")); head != "" {
+ info.Branch = head
+ } else {
+ info.Branch = os.Getenv("GITHUB_REF_NAME")
+ }
+ // GITHUB_REF is a non-PR ref (e.g. refs/heads/main) on push events, so
+ // only record PR fields when the trigger is actually a pull request.
+ if isPullRequestEvent(eventName) {
+ ref := os.Getenv("GITHUB_REF")
+ info.PR = ref
+ info.PRNumber = parsePRNumber(ref)
+ }
+ return info
}
if os.Getenv("CI") != "" {
return &schema.RunInfo{Provider: "ci", Ephemeral: true}
@@ -312,6 +331,30 @@ func detectRunInfo() *schema.RunInfo {
return nil
}
+func isPullRequestEvent(eventName string) bool {
+ return eventName == "pull_request" || eventName == "pull_request_target"
+}
+
+// parsePRNumber extracts the pull-request number from a ref of the form
+// refs/pull/<number>/merge (or /head). It returns an empty string when the ref
+// is not a pull-request ref or the number is not purely numeric.
+func parsePRNumber(ref string) string {
+ const prefix = "refs/pull/"
+ if !strings.HasPrefix(ref, prefix) {
+ return ""
+ }
+ num, _, ok := strings.Cut(strings.TrimPrefix(ref, prefix), "/")
+ if !ok || num == "" {
+ return ""
+ }
+ for _, r := range num {
+ if r < '0' || r > '9' {
+ return ""
+ }
+ }
+ return num
+}
+
func terminateProcess(process *os.Process) error {
if process == nil {
return nil
diff --git a/cli/beacon/internal/ci/session_test.go b/cli/beacon/internal/ci/session_test.go
index 556254d4..405b84b5 100644
--- a/cli/beacon/internal/ci/session_test.go
+++ b/cli/beacon/internal/ci/session_test.go
@@ -135,6 +135,100 @@ func TestRunChildInjectsClaudeEnvAndBeaconPaths(t *testing.T) {
}
}
+func TestDetectRunInfoPullRequest(t *testing.T) {
+ t.Setenv("CI", "true")
+ t.Setenv("GITHUB_ACTIONS", "true")
+ t.Setenv("GITHUB_EVENT_NAME", "pull_request")
+ t.Setenv("GITHUB_RUN_ID", "123")
+ t.Setenv("GITHUB_RUN_ATTEMPT", "2")
+ t.Setenv("GITHUB_WORKFLOW", "ci")
+ t.Setenv("GITHUB_JOB", "telemetry")
+ t.Setenv("GITHUB_SHA", "deadbeef")
+ t.Setenv("GITHUB_REPOSITORY", "asymptote-labs/agent-beacon")
+ t.Setenv("GITHUB_ACTOR", "octocat")
+ t.Setenv("GITHUB_HEAD_REF", "feature/telemetry")
+ t.Setenv("GITHUB_REF_NAME", "12/merge")
+ t.Setenv("GITHUB_REF", "refs/pull/12/merge")
+
+ info := detectRunInfo()
+ if info == nil {
+ t.Fatal("detectRunInfo returned nil in GitHub Actions")
+ }
+ if info.Provider != "github_actions" {
+ t.Fatalf("Provider = %q, want github_actions", info.Provider)
+ }
+ if info.RunAttempt != "2" || info.Job != "telemetry" || info.EventName != "pull_request" {
+ t.Fatalf("unexpected run context: %+v", info)
+ }
+ if info.Repository != "asymptote-labs/agent-beacon" {
+ t.Fatalf("Repository = %q", info.Repository)
+ }
+ if info.Branch != "feature/telemetry" {
+ t.Fatalf("Branch = %q, want PR head ref", info.Branch)
+ }
+ if info.PR != "refs/pull/12/merge" || info.PRNumber != "12" {
+ t.Fatalf("PR = %q, PRNumber = %q", info.PR, info.PRNumber)
+ }
+}
+
+func TestDetectRunInfoPushOmitsPR(t *testing.T) {
+ t.Setenv("CI", "true")
+ t.Setenv("GITHUB_ACTIONS", "true")
+ t.Setenv("GITHUB_EVENT_NAME", "push")
+ t.Setenv("GITHUB_HEAD_REF", "")
+ t.Setenv("GITHUB_REF_NAME", "main")
+ t.Setenv("GITHUB_REF", "refs/heads/main")
+
+ info := detectRunInfo()
+ if info == nil {
+ t.Fatal("detectRunInfo returned nil in GitHub Actions")
+ }
+ if info.Branch != "main" {
+ t.Fatalf("Branch = %q, want ref name on push", info.Branch)
+ }
+ if info.PR != "" || info.PRNumber != "" {
+ t.Fatalf("push build should not record PR fields: PR=%q PRNumber=%q", info.PR, info.PRNumber)
+ }
+}
+
+func TestDetectRunInfoGenericCIFallback(t *testing.T) {
+ t.Setenv("GITHUB_ACTIONS", "")
+ t.Setenv("CI", "true")
+
+ info := detectRunInfo()
+ if info == nil {
+ t.Fatal("detectRunInfo returned nil for generic CI")
+ }
+ if info.Provider != "ci" || !info.Ephemeral {
+ t.Fatalf("unexpected generic CI run info: %+v", info)
+ }
+}
+
+func TestDetectRunInfoNotCI(t *testing.T) {
+ t.Setenv("GITHUB_ACTIONS", "")
+ t.Setenv("CI", "")
+
+ if info := detectRunInfo(); info != nil {
+ t.Fatalf("detectRunInfo = %+v, want nil outside CI", info)
+ }
+}
+
+func TestParsePRNumber(t *testing.T) {
+ cases := map[string]string{
+ "refs/pull/12/merge": "12",
+ "refs/pull/7/head": "7",
+ "refs/heads/main": "",
+ "refs/pull//merge": "",
+ "refs/pull/abc/head": "",
+ "": "",
+ }
+ for ref, want := range cases {
+ if got := parsePRNumber(ref); got != want {
+ t.Fatalf("parsePRNumber(%q) = %q, want %q", ref, got, want)
+ }
+ }
+}
+
func fakeExecutable(t *testing.T, name, script string) string {
t.Helper()
path := filepath.Join(t.TempDir(), name)
diff --git a/pkg/asymptotetrace/event.go b/pkg/asymptotetrace/event.go
index ed6515fa..7153966f 100644
--- a/pkg/asymptotetrace/event.go
+++ b/pkg/asymptotetrace/event.go
@@ -61,11 +61,20 @@ type SessionInfo struct {
}
type RunInfo struct {
- Provider string `json:"provider,omitempty"`
- RunID string `json:"run_id,omitempty"`
- Workflow string `json:"workflow,omitempty"`
- Commit string `json:"commit,omitempty"`
+ Provider string `json:"provider,omitempty"`
+ RunID string `json:"run_id,omitempty"`
+ RunAttempt string `json:"run_attempt,omitempty"`
+ Workflow string `json:"workflow,omitempty"`
+ Job string `json:"job,omitempty"`
+ EventName string `json:"event_name,omitempty"`
+ Commit string `json:"commit,omitempty"`
+ Repository string `json:"repository,omitempty"`
+ Branch string `json:"branch,omitempty"`
+ // PR holds the raw pull-request ref (e.g. refs/pull/12/merge) and is only
+ // populated on pull-request events; it is left empty for non-PR refs such
+ // as push builds. PRNumber is the parsed pull-request number.
PR string `json:"pr,omitempty"`
+ PRNumber string `json:"pr_number,omitempty"`
Actor string `json:"actor,omitempty"`
Ephemeral bool `json:"ephemeral,omitempty"`
}