Skip to content

fix(gateway): preserve OpenCode v2 title requests and avoid WARP pool churn - #169

Merged
jasonxu114514 merged 1 commit into
jasonxu114514:mainfrom
NekomyaDev:upstream-pr-title-fallback
Nov 5, 2026
Merged

jasonxu114514 merged 1 commit into
jasonxu114514:mainfrom
NekomyaDev:upstream-pr-title-fallback

Conversation

@NekomyaDev

Copy link
Copy Markdown
Contributor

Summary

Fixes #164. Completes and extends #102 by handling OpenCode v2 session-title prompts, preventing false-positive tool injection, capping title requests to a single attempt, and providing a clean local fallback on 429.

Problem

  1. Tool Injection Rejection: OpenCode sends background session-title requests with an empty tool list (tools: []). The free-tier upstream accepts this tool-free body, but prepareAnonymousBody attached the complete official CLI toolset to all requests. This caused upstream Zen gates to reject title calls with 403 FreeTierError or 429 FreeUsageLimitError (in: 0).
  2. OpenCode v2 System Prompt: PR fix: do not inject agent tools into title-generation requests #102 only checked for the legacy v1 prompt in user turns ("Generate a title for this conversation"). OpenCode v2 rewritten this into a system prompt beginning "You name conversations so the user can find them later in a list of past sessions...", which escaped PR fix: do not inject agent tools into title-generation requests #102's guard.
  3. Egress Pool Churn: When a title request hit 429, the gateway rotated through the entire WARP pool (up to configured max_attempts), burning every healthy exit in 1–2 seconds and triggering cascading 429 errors for subsequent user agent requests.

Changes

  1. Title Detection (internal/gateway/title.go):
    • Detects both v1 legacy prompt prefixes and v2 system prompt signatures ("you name conversations so the user can find them later", "reply with a title and nothing else", auto-rename).
    • Strict false-positive prevention: requires absent/empty tools, <= 3 messages, and no assistant/tool turns.
  2. Wire Shape Preservation (internal/gateway/upstream.go):
    • Skips renameAndCompleteTools when title generation is detected so the payload reaches upstream tool-free.
    • Normal agent requests continue to receive the full official toolset untouched.
  3. Pool Protection & Fallback (internal/gateway/warp.go, internal/gateway/upstream.go):
    • Caps title requests to 1 attempt (limit = 1) to eliminate egress pool rotation churn.
    • If upstream returns 429 or fails, synthesizes an OpenAI-compliant completion from the initial prompt (UTF-8 safe, max 6 words / 40 runes) in both streaming SSE and JSON formats.

Verification

  • Added internal/gateway/title_test.go:
    • TestIsTitleGenerationRequest_Comprehensive: verifies v1, v2, and false-positive prevention cases.
    • TestPrepareAnonymousBody_TitleVsAgentToolsInjection: verifies title requests stay tool-free while agent requests retain tools.
    • TestSynthesizeTitle_UnicodeAndFallbacks: verifies truncation, UTF-8 safety, and empty fallbacks.
    • TestWarpAnonymous_TitleRequestSingleAttemptFallbackOn429: verifies 1-attempt cap and 429 fallback on mock WARP pool.
  • go vet ./... clean.
  • go test -count=1 -v ./... all passed.
  • go build ./cmd/opencode2api clean.

@jasonxu114514
jasonxu114514 merged commit 86be64b into jasonxu114514:main Nov 5, 2026
jasonxu114514 added a commit that referenced this pull request Nov 5, 2026
#169 called anonymousNodes.CursorForOffset, warp.Pool.ReportWithRetryAfter and
warp.Pool.HasUntriedActive without defining them, so main did not compile.
Add the three methods: an attempt offset shifts the anonymous start node, a
longer Retry-After extends the egress cooldown, and HasUntriedActive matches
the pool's egress keys.

Also:
- rewrite title.go in house style: named limits, a prompt table, no issue
  numbers in comments, and a titleFallback helper shared by both lanes
- document the held-back tool-call helpers in protocol/stream.go
- gofmt internal/admin/server.go
- drop the _test.go files from #169, #170 and #171
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Theory: anonymous-lane 429s are conversation-title requests, not quota

2 participants