Skip to content

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

Guard Codex usage reconciliation writes

shukieshah4a7bff0e7ae2
diff --git a/cli/beacon-hooks/cmd/codex_usage_sync.go b/cli/beacon-hooks/cmd/codex_usage_sync.go
index 78ed87a3..75121d30 100644
--- a/cli/beacon-hooks/cmd/codex_usage_sync.go
+++ b/cli/beacon-hooks/cmd/codex_usage_sync.go
@@ -1,6 +1,8 @@
package cmd
import (
+ "os"
+ "strings"
"time"
"github.com/spf13/cobra"
@@ -42,6 +44,10 @@ func maybeReconcileCodexUsage(logger *logging.Logger) {
}
func reconcileCodexUsage(logger *logging.Logger) error {
+ if !endpointLoggingConfigured() {
+ logger.Debug("Skipping Codex usage reconciliation because endpoint logging is not configured")
+ return nil
+ }
result, err := codexusage.ReconcileAndWrite(codexusage.ReconcileOptions{}, func(event codexusage.UsageEvent) error {
fields := codexUsageFields(event)
if err := logger.EndpointEvent("token.usage", "metric", "info", "Codex token usage observed", fields); err != nil {
@@ -56,6 +62,15 @@ func reconcileCodexUsage(logger *logging.Logger) error {
return nil
}
+func endpointLoggingConfigured() bool {
+ for _, key := range []string{"BEACON_ENDPOINT_LOG", "BEACON_CLOUD_LOG_PATH", "BEACON_LOG_PATH", "BEACON_RUNTIME_LOG"} {
+ if strings.TrimSpace(os.Getenv(key)) != "" {
+ return true
+ }
+ }
+ return strings.TrimSpace(os.Getenv("BEACON_ENDPOINT_MODE")) != ""
+}
+
func codexUsageFields(event codexusage.UsageEvent) map[string]interface{} {
sessionID := "codex:" + event.SessionID
if event.SessionID == "" {
diff --git a/cli/beacon-hooks/cmd/codex_usage_sync_test.go b/cli/beacon-hooks/cmd/codex_usage_sync_test.go
index 0275aa7e..94389acf 100644
--- a/cli/beacon-hooks/cmd/codex_usage_sync_test.go
+++ b/cli/beacon-hooks/cmd/codex_usage_sync_test.go
@@ -69,3 +69,40 @@ func TestRunCodexUsageSyncWritesTokenUsageEvent(t *testing.T) {
t.Fatalf("idempotent run wrote %d events, want 1", got)
}
}
+
+func TestRunCodexUsageSyncWithoutEndpointLogDoesNotMarkSeen(t *testing.T) {
+ tmp := t.TempDir()
+ statePath := filepath.Join(tmp, "state.json")
+ sessionDir := filepath.Join(tmp, "codex", "sessions")
+ if err := os.MkdirAll(sessionDir, 0755); err != nil {
+ t.Fatal(err)
+ }
+ sessionPath := filepath.Join(sessionDir, "session.jsonl")
+ lines := []string{
+ `{"type":"session_meta","timestamp":"2026-06-29T10:00:00Z","payload":{"id":"codex-session-1"}}`,
+ `{"type":"turn_context","timestamp":"2026-06-29T10:00:01Z","payload":{"model":"gpt-5","turn_id":"turn-1"}}`,
+ `{"type":"event_msg","timestamp":"2026-06-29T10:00:03Z","payload":{"type":"token_count","info":{"last_token_usage":{"input_tokens":10,"output_tokens":2,"cached_input_tokens":4}}}}`,
+ }
+ if err := os.WriteFile(sessionPath, []byte(strings.Join(lines, "\n")+"\n"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ t.Setenv("BEACON_ENDPOINT_LOG", "")
+ t.Setenv("BEACON_ENDPOINT_MODE", "")
+ t.Setenv("BEACON_CODEX_USAGE_STATE", statePath)
+ t.Setenv("BEACON_CODEX_SESSIONS_DIR", sessionDir)
+ origPlatform := platformFlag
+ platformFlag = "codex"
+ t.Cleanup(func() { platformFlag = origPlatform })
+
+ runHookWithInput(t, runCodexUsageSync, map[string]interface{}{"hook_event_name": "Stop"})
+ if _, err := os.Stat(statePath); !os.IsNotExist(err) {
+ t.Fatalf("state path exists or stat failed after skipped reconciliation: %v", err)
+ }
+
+ logPath := filepath.Join(tmp, "runtime.jsonl")
+ t.Setenv("BEACON_ENDPOINT_LOG", logPath)
+ runHookWithInput(t, runCodexUsageSync, map[string]interface{}{"hook_event_name": "Stop"})
+ if got := len(endpointEvents(t, logPath)); got != 1 {
+ t.Fatalf("events after configuring endpoint log = %d, want 1", got)
+ }
+}
diff --git a/pkg/asymptoteobserve/codexusage/codexusage.go b/pkg/asymptoteobserve/codexusage/codexusage.go
index b0531c40..56fd2417 100644
--- a/pkg/asymptoteobserve/codexusage/codexusage.go
+++ b/pkg/asymptoteobserve/codexusage/codexusage.go
@@ -115,6 +115,7 @@ func (g *forkGate) suppresses(lineType string, turnID string) bool {
return true
}
if turnID == "" {
+ g.active = false
return true
}
if ms := uuidV7Millis(turnID); ms != 0 && ms < g.createdMs {
diff --git a/pkg/asymptoteobserve/codexusage/codexusage_test.go b/pkg/asymptoteobserve/codexusage/codexusage_test.go
index 2cd2ef78..d1ade730 100644
--- a/pkg/asymptoteobserve/codexusage/codexusage_test.go
+++ b/pkg/asymptoteobserve/codexusage/codexusage_test.go
@@ -63,6 +63,29 @@ func TestParseFileKeepsLatestTokenSnapshotPerTurn(t *testing.T) {
}
}
+func TestParseFileForkGateDoesNotStickOnEmptyTurnID(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "fork.jsonl")
+ lines := []string{
+ `{"type":"session_meta","timestamp":"2026-06-29T10:00:00Z","payload":{"id":"forked-session","forked_from_id":"parent-session","timestamp":"2026-06-29T10:00:00Z"}}`,
+ `{"type":"turn_context","timestamp":"2026-06-29T10:00:01Z","payload":{"model":"gpt-5","turn_id":""}}`,
+ `{"type":"event_msg","timestamp":"2026-06-29T10:00:02Z","payload":{"type":"token_count","info":{"last_token_usage":{"input_tokens":10,"output_tokens":2,"cached_input_tokens":4}}}}`,
+ }
+ if err := os.WriteFile(path, []byte(strings.Join(lines, "\n")+"\n"), 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ events, err := ParseFile(path, ParseOptions{})
+ if err != nil {
+ t.Fatalf("ParseFile returned error: %v", err)
+ }
+ if len(events) != 1 {
+ t.Fatalf("events = %#v, want token usage after empty turn id clears fork gate", events)
+ }
+ if events[0].InputTokens != 6 || events[0].CacheReadTokens != 4 || events[0].OutputTokens != 2 {
+ t.Fatalf("tokens = %#v", events[0])
+ }
+}
+
func TestReconcileUsesStateForIdempotence(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "session.jsonl")