@@ -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).
781821func TestExtractSessionID_IdempotencyKey (t * testing.T ) {
0 commit comments