File size: 5,954 Bytes
e48a2d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package session

import (
	"encoding/json"
	"testing"
)

// refExtractKey 是快路径出现之前的原实现(整树解析),作为等价性基准保留在测试里。
func refExtractKey(body []byte) string {
	if len(body) == 0 {
		return ""
	}
	var obj map[string]any
	if err := json.Unmarshal(body, &obj); err != nil {
		return ""
	}
	if meta, ok := obj["metadata"].(map[string]any); ok {
		if v := strOrEmpty(meta["conversation_id"]); v != "" {
			return v
		}
		if v := strOrEmpty(meta["conversationId"]); v != "" {
			return v
		}
	}
	if v := strOrEmpty(obj["conversation_id"]); v != "" {
		return v
	}
	if v := strOrEmpty(obj["conversationId"]); v != "" {
		return v
	}
	if v := strOrEmpty(obj["prompt_cache_key"]); v != "" {
		return v
	}
	if hasUserIDKey(obj) {
		return ""
	}
	return deriveKey(obj)
}

// refResolveConversationID 同上,整树版本的 ResolveConversationID。
func refResolveConversationID(body []byte) string {
	if len(body) == 0 {
		return ""
	}
	var obj map[string]any
	if err := json.Unmarshal(body, &obj); err != nil {
		return ""
	}
	if meta, ok := obj["metadata"].(map[string]any); ok {
		if v := strOrEmpty(meta["conversation_id"]); v != "" {
			return v
		}
		if v := strOrEmpty(meta["conversationId"]); v != "" {
			return v
		}
	}
	if v := strOrEmpty(obj["conversation_id"]); v != "" {
		return v
	}
	return strOrEmpty(obj["conversationId"])
}

// TestExtractKeyFastPathEquivalence 快路径必须与整树实现给出完全相同的结果。
//
// 这是本类优化的核心风险:会话键决定账号粘性与前缀缓存槽,判错会让同一对话
// 在两个账号间来回跳(缓存连续 miss、上游计费翻倍)。所以逐例对拍,不靠推理。
func TestExtractKeyFastPathEquivalence(t *testing.T) {
	bodies := []string{
		// 显式键:4 种命名 + 优先级
		`{"metadata":{"conversation_id":"m-snake","conversationId":"m-camel"},"conversation_id":"top-snake","conversationId":"top-camel","prompt_cache_key":"pc","messages":[{"role":"system","content":"s"},{"role":"user","content":"u"}]}`,
		`{"metadata":{"conversationId":"m-camel"},"conversation_id":"top-snake","messages":[]}`,
		`{"conversation_id":"top-snake","conversationId":"top-camel","messages":[]}`,
		`{"conversationId":"top-camel","prompt_cache_key":"pc","messages":[]}`,
		`{"prompt_cache_key":"pc","messages":[]}`,
		// 空串 / 非字符串:都应视为未命中
		`{"conversation_id":"","metadata":{"conversation_id":""},"messages":[]}`,
		`{"conversation_id":123,"metadata":{"conversationId":true},"messages":[]}`,
		`{"metadata":"not-an-object","conversationId":"c","messages":[]}`,
		// user_id 闸
		`{"conversation_id":"","metadata":{"user_id":"u1"},"messages":[{"role":"system","content":"s"},{"role":"user","content":"u"}]}`,
		`{"user_id":"u1","messages":[{"role":"system","content":"s"},{"role":"user","content":"u"}]}`,
		// 派生键
		`{"messages":[{"role":"system","content":"sys"},{"role":"user","content":"first"}]}`,
		`{"messages":[{"role":"user","content":"only-user"}]}`,
		`{"messages":[]}`,
		`{"messages":"nope"}`,
		`{}`,
		``,
		// 非对象 / 坏 JSON
		`[]`,
		`null`,
		`{"conversation_id":"unterminated`,
		// 转义值
		`{"conversation_id":"a\"b\\c","messages":[]}`,
		`{"metadata":{"conversation_id":"\u4e2d\u6587-id"},"messages":[]}`,
		// 深层嵌套后再出现显式键(扫描器要能跳过)
		`{"tools":[{"function":{"parameters":{"$defs":{"a":{"b":[1,2,{"c":"}"}]}}}}}],"conversationId":"after-deep","messages":[]}`,
	}
	for _, b := range bodies {
		want := refExtractKey([]byte(b))
		if got := ExtractKey([]byte(b)); got != want {
			t.Errorf("ExtractKey(%s) = %q want %q", b, got, want)
		}
		wantCID := refResolveConversationID([]byte(b))
		if got := ResolveConversationID([]byte(b)); got != wantCID {
			t.Errorf("ResolveConversationID(%s) = %q want %q", b, got, wantCID)
		}
	}
}

// TestExtractKeyFastPathLargeBody 大 body 上的等价性(快路径的主要收益场景)。
func TestExtractKeyFastPathLargeBody(t *testing.T) {
	block := make([]byte, 0, 200*1024)
	for i := 0; i < 4000; i++ {
		block = append(block, []byte(`{"role":"assistant","content":"some long agent history chunk with \"quotes\" and \n newlines"}`)...)
		if i < 3999 {
			block = append(block, ',')
		}
	}
	bodies := []string{
		`{"conversationId":"big-1","messages":[` + string(block) + `]}`,
		`{"messages":[` + string(block) + `]}`,
	}
	for i, b := range bodies {
		want := refExtractKey([]byte(b))
		if got := ExtractKey([]byte(b)); got != want {
			t.Errorf("case %d: got %q want %q", i, got, want)
		}
	}
}

// TestResolveConversationIDNeverFallsBackToUserID 契约:只认 conversationId。
func TestResolveConversationIDNeverFallsBackToUserID(t *testing.T) {
	body := []byte(`{"user_id":"u-1","metadata":{"user_id":"u-1"},"messages":[]}`)
	if got := ResolveConversationID(body); got != "" {
		t.Fatalf("不应回落 user_id,得到 %q", got)
	}
}

func BenchmarkExtractKeyExplicitLarge(b *testing.B) {
	block := make([]byte, 0, 400*1024)
	for i := 0; i < 8000; i++ {
		block = append(block, []byte(`{"role":"assistant","content":"agent history chunk 0123456789 abcdefghij"}`)...)
		if i < 7999 {
			block = append(block, ',')
		}
	}
	body := []byte(`{"conversationId":"bench-1","messages":[` + string(block) + `]}`)
	b.SetBytes(int64(len(body)))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		if ExtractKey(body) != "bench-1" {
			b.Fatal("wrong key")
		}
	}
}

func BenchmarkExtractKeyRefLarge(b *testing.B) {
	block := make([]byte, 0, 400*1024)
	for i := 0; i < 8000; i++ {
		block = append(block, []byte(`{"role":"assistant","content":"agent history chunk 0123456789 abcdefghij"}`)...)
		if i < 7999 {
			block = append(block, ',')
		}
	}
	body := []byte(`{"conversationId":"bench-1","messages":[` + string(block) + `]}`)
	b.SetBytes(int64(len(body)))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		if refExtractKey(body) != "bench-1" {
			b.Fatal("wrong key")
		}
	}
}