Skip to content

Commit 5418e15

Browse files
fix(service): normalize user agent for gemini session reuse
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
1 parent bcf84cc commit 5418e15

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

backend/internal/service/gemini_session.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ func BuildGeminiDigestChain(req *antigravity.GeminiRequest) string {
5252
// 返回 16 字符的 Base64 编码的 SHA256 前缀
5353
func GenerateGeminiPrefixHash(userID, apiKeyID int64, ip, userAgent, platform, model string) string {
5454
// 组合所有标识符
55+
normalizedUserAgent := NormalizeSessionUserAgent(userAgent)
5556
combined := strconv.FormatInt(userID, 10) + ":" +
5657
strconv.FormatInt(apiKeyID, 10) + ":" +
5758
ip + ":" +
58-
userAgent + ":" +
59+
normalizedUserAgent + ":" +
5960
platform + ":" +
6061
model
6162

backend/internal/service/gemini_session_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,24 @@ func TestGenerateGeminiPrefixHash(t *testing.T) {
152152
}
153153
}
154154

155+
func TestGenerateGeminiPrefixHash_IgnoresUserAgentVersionNoise(t *testing.T) {
156+
hash1 := GenerateGeminiPrefixHash(1, 100, "192.168.1.1", "Mozilla/5.0 codex_cli_rs/0.1.0", "antigravity", "gemini-2.5-pro")
157+
hash2 := GenerateGeminiPrefixHash(1, 100, "192.168.1.1", "Mozilla/5.0 codex_cli_rs/0.1.1", "antigravity", "gemini-2.5-pro")
158+
159+
if hash1 != hash2 {
160+
t.Fatalf("version-only User-Agent changes should not perturb Gemini prefix hash: %s vs %s", hash1, hash2)
161+
}
162+
}
163+
164+
func TestGenerateGeminiPrefixHash_IgnoresFreeformUserAgentVersionNoise(t *testing.T) {
165+
hash1 := GenerateGeminiPrefixHash(1, 100, "192.168.1.1", "Codex CLI 0.1.0", "antigravity", "gemini-2.5-pro")
166+
hash2 := GenerateGeminiPrefixHash(1, 100, "192.168.1.1", "Codex CLI 0.1.1", "antigravity", "gemini-2.5-pro")
167+
168+
if hash1 != hash2 {
169+
t.Fatalf("free-form version-only User-Agent changes should not perturb Gemini prefix hash: %s vs %s", hash1, hash2)
170+
}
171+
}
172+
155173
func TestParseGeminiSessionValue(t *testing.T) {
156174
tests := []struct {
157175
name string

0 commit comments

Comments
 (0)