diff --git a/backend/internal/service/gateway_sanitize_test.go b/backend/internal/service/gateway_sanitize_test.go index 3b0a07c9..a70c1a00 100644 --- a/backend/internal/service/gateway_sanitize_test.go +++ b/backend/internal/service/gateway_sanitize_test.go @@ -9,12 +9,12 @@ import ( func TestSanitizeOpenCodeText_RewritesCanonicalSentence(t *testing.T) { in := "You are OpenCode, the best coding agent on the planet." - got := sanitizeOpenCodeText(in) + got := sanitizeSystemText(in) require.Equal(t, strings.TrimSpace(claudeCodeSystemPrompt), got) } -func TestSanitizeOpenCodeText_RewritesOpenCodeKeywords(t *testing.T) { +func TestSanitizeToolText_RewritesOpenCodeKeywords(t *testing.T) { in := "OpenCode and opencode are mentioned." - got := sanitizeOpenCodeText(in) + got := sanitizeToolText(in) require.Equal(t, "Claude Code and Claude are mentioned.", got) } diff --git a/backend/internal/service/gateway_service.go b/backend/internal/service/gateway_service.go index 47ea8593..40354e48 100644 --- a/backend/internal/service/gateway_service.go +++ b/backend/internal/service/gateway_service.go @@ -697,7 +697,10 @@ func normalizeParamNameForOpenCode(name string, cache map[string]string) string return name } -func sanitizeOpenCodeText(text string) string { +// sanitizeSystemText rewrites only the fixed OpenCode identity sentence (if present). +// We intentionally avoid broad keyword replacement in system prompts to prevent +// accidentally changing user-provided instructions. +func sanitizeSystemText(text string) string { if text == "" { return text } @@ -709,6 +712,17 @@ func sanitizeOpenCodeText(text string) string { "You are OpenCode, the best coding agent on the planet.", strings.TrimSpace(claudeCodeSystemPrompt), ) + return text +} + +// sanitizeToolText is intentionally more aggressive than sanitizeSystemText because +// tool descriptions are not user chat content, and some upstreams may flag "opencode" +// strings as non-Claude-Code fingerprints. +func sanitizeToolText(text string) string { + if text == "" { + return text + } + text = sanitizeSystemText(text) text = strings.ReplaceAll(text, "OpenCode", "Claude Code") text = opencodeTextRe.ReplaceAllString(text, "Claude") return text @@ -720,7 +734,7 @@ func sanitizeToolDescription(description string) string { } description = toolDescAbsPathRe.ReplaceAllString(description, "[path]") description = toolDescWinPathRe.ReplaceAllString(description, "[path]") - return sanitizeOpenCodeText(description) + return sanitizeToolText(description) } func normalizeToolInputSchema(inputSchema any, cache map[string]string) { @@ -795,7 +809,7 @@ func normalizeClaudeOAuthRequestBody(body []byte, modelID string, opts claudeOAu if system, ok := req["system"]; ok { switch v := system.(type) { case string: - sanitized := sanitizeOpenCodeText(v) + sanitized := sanitizeSystemText(v) if sanitized != v { req["system"] = sanitized } @@ -812,7 +826,7 @@ func normalizeClaudeOAuthRequestBody(body []byte, modelID string, opts claudeOAu if !ok || text == "" { continue } - sanitized := sanitizeOpenCodeText(text) + sanitized := sanitizeSystemText(text) if sanitized != text { block["text"] = sanitized }