Skip to content

Commit 4d6457e

Browse files
committed
feat: support extracting X-Amp-Thread-Id header as session id for session affinity
1 parent a188159 commit 4d6457e

2 files changed

Lines changed: 54 additions & 6 deletions

File tree

sdk/cliproxy/auth/selector.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,10 @@ func (s *SessionAffinitySelector) InvalidateAuth(authID string) {
570570
// Priority order:
571571
// 1. metadata.user_id (Claude Code format with _session_{uuid}) - highest priority for Claude Code clients
572572
// 2. X-Session-ID header
573-
// 3. metadata.user_id (non-Claude Code format)
574-
// 4. conversation_id field in request body
575-
// 5. Stable hash from first few messages content (fallback)
573+
// 3. X-Amp-Thread-Id header (Amp CLI thread ID)
574+
// 4. metadata.user_id (non-Claude Code format)
575+
// 5. conversation_id field in request body
576+
// 6. Stable hash from first few messages content (fallback)
576577
func ExtractSessionID(headers http.Header, payload []byte, metadata map[string]any) string {
577578
primary, _ := extractSessionIDs(headers, payload, metadata)
578579
return primary
@@ -608,22 +609,29 @@ func extractSessionIDs(headers http.Header, payload []byte, metadata map[string]
608609
}
609610
}
610611

612+
// 3. X-Amp-Thread-Id header (Amp CLI thread ID)
613+
if headers != nil {
614+
if tid := headers.Get("X-Amp-Thread-Id"); tid != "" {
615+
return "amp:" + tid, ""
616+
}
617+
}
618+
611619
if len(payload) == 0 {
612620
return "", ""
613621
}
614622

615-
// 3. metadata.user_id (non-Claude Code format)
623+
// 4. metadata.user_id (non-Claude Code format)
616624
userID := gjson.GetBytes(payload, "metadata.user_id").String()
617625
if userID != "" {
618626
return "user:" + userID, ""
619627
}
620628

621-
// 4. conversation_id field
629+
// 5. conversation_id field
622630
if convID := gjson.GetBytes(payload, "conversation_id").String(); convID != "" {
623631
return "conv:" + convID, ""
624632
}
625633

626-
// 5. Hash-based fallback from message content
634+
// 6. Hash-based fallback from message content
627635
return extractMessageHashIDs(payload)
628636
}
629637

sdk/cliproxy/auth/selector_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,46 @@ func TestExtractSessionID_Headers(t *testing.T) {
776776
}
777777
}
778778

779+
func TestExtractSessionID_AmpThreadId(t *testing.T) {
780+
t.Parallel()
781+
782+
headers := make(http.Header)
783+
headers.Set("X-Amp-Thread-Id", "T-7873e6bd-6354-4a9a-be2c-c7702c6e1b64")
784+
785+
got := ExtractSessionID(headers, nil, nil)
786+
want := "amp:T-7873e6bd-6354-4a9a-be2c-c7702c6e1b64"
787+
if got != want {
788+
t.Errorf("ExtractSessionID() with X-Amp-Thread-Id = %q, want %q", got, want)
789+
}
790+
}
791+
792+
// TestExtractSessionID_AmpThreadIdLowerPriority verifies X-Amp-Thread-Id is lower
793+
// priority than Claude Code metadata.user_id but higher than conversation_id.
794+
func TestExtractSessionID_AmpThreadIdPriority(t *testing.T) {
795+
t.Parallel()
796+
797+
// X-Amp-Thread-Id should be used when no Claude Code user_id is present
798+
headers := make(http.Header)
799+
headers.Set("X-Amp-Thread-Id", "T-priority-test")
800+
801+
payload := []byte(`{"conversation_id":"conv-12345"}`)
802+
got := ExtractSessionID(headers, payload, nil)
803+
want := "amp:T-priority-test"
804+
if got != want {
805+
t.Errorf("ExtractSessionID() = %q, want %q (Amp thread ID should take priority over conversation_id)", got, want)
806+
}
807+
808+
// Claude Code user_id should take priority over X-Amp-Thread-Id
809+
headers2 := make(http.Header)
810+
headers2.Set("X-Amp-Thread-Id", "T-priority-test")
811+
payload2 := []byte(`{"metadata":{"user_id":"user_xxx_account__session_ac980658-63bd-4fb3-97ba-8da64cb1e344"}}`)
812+
got2 := ExtractSessionID(headers2, payload2, nil)
813+
want2 := "claude:ac980658-63bd-4fb3-97ba-8da64cb1e344"
814+
if got2 != want2 {
815+
t.Errorf("ExtractSessionID() = %q, want %q (Claude Code should take priority over Amp thread ID)", got2, want2)
816+
}
817+
}
818+
779819
// TestExtractSessionID_IdempotencyKey verifies that idempotency_key is intentionally
780820
// ignored for session affinity (it's auto-generated per-request, causing cache misses).
781821
func TestExtractSessionID_IdempotencyKey(t *testing.T) {

0 commit comments

Comments
 (0)