From b2154c748588780418ab46d4409f999f1f6ce64c Mon Sep 17 00:00:00 2001 From: jarvis Date: Thu, 3 Sep 2026 23:25:15 -0400 Subject: [PATCH] fix(agent): prefer managed Codex session ID Use AGENT_SESSION_ID before CODEX_THREAD_ID for Codex runtimes while retaining the standalone thread ID fallback.\n\nRefs #17 --- internal/agent/runtime.go | 2 +- internal/agent/runtime_test.go | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/internal/agent/runtime.go b/internal/agent/runtime.go index b9fca86..b14216d 100644 --- a/internal/agent/runtime.go +++ b/internal/agent/runtime.go @@ -15,7 +15,7 @@ const ( var sessionEnvironmentNames = map[string][]string{ "claude": {"CLAUDE_CODE_SESSION_ID"}, - "codex": {"CODEX_THREAD_ID"}, + "codex": {"AGENT_SESSION_ID", "CODEX_THREAD_ID"}, "copilot": {"COPILOT_AGENT_SESSION_ID"}, "goose": {"AGENT_SESSION_ID"}, "hermes": {"HERMES_SESSION_ID", "HERMES_SESSION_KEY"}, diff --git a/internal/agent/runtime_test.go b/internal/agent/runtime_test.go index b2e3650..f16c761 100644 --- a/internal/agent/runtime_test.go +++ b/internal/agent/runtime_test.go @@ -90,7 +90,6 @@ func TestDetectAgentSessionReturnsRawRuntimeIdentifier(t *testing.T) { environment map[string]string expected string }{ - {runtime: "codex", environment: map[string]string{"CODEX_THREAD_ID": "thread-1"}, expected: "thread-1"}, {runtime: "claude", environment: map[string]string{"CLAUDE_CODE_SESSION_ID": "session-2"}, expected: "session-2"}, {runtime: "copilot", environment: map[string]string{"COPILOT_AGENT_SESSION_ID": "session-3"}, expected: "session-3"}, {runtime: "goose", environment: map[string]string{"AGENT_SESSION_ID": "session-4"}, expected: "session-4"}, @@ -107,6 +106,25 @@ func TestDetectAgentSessionReturnsRawRuntimeIdentifier(t *testing.T) { } } +func TestDetectAgentSessionPrefersManagedCodexSession(t *testing.T) { + sessionID, ok := detectAgentSession("codex", testEnvironment(map[string]string{ + "AGENT_SESSION_ID": "session-1", + "CODEX_THREAD_ID": "thread-1", + })) + if !ok || sessionID != "session-1" { + t.Fatalf("Codex session = %q, %v", sessionID, ok) + } +} + +func TestDetectAgentSessionFallsBackToCodexThread(t *testing.T) { + sessionID, ok := detectAgentSession("codex", testEnvironment(map[string]string{ + "CODEX_THREAD_ID": "thread-1", + })) + if !ok || sessionID != "thread-1" { + t.Fatalf("Codex session = %q, %v", sessionID, ok) + } +} + func TestAgentSessionCacheKeySeparatesRawSessionIdentifiers(t *testing.T) { t.Setenv("AGENT", "codex") t.Setenv("CODEX_THREAD_ID", "thread-secret-1")