VTCode icon indicating copy to clipboard operation
VTCode copied to clipboard

opencode-go: every request fails with MissingSessionID (x-opencode-session header not sent)

Open glanzel opened this issue 1 month ago • 3 comments

Summary

VT Code 0.181.0 cannot use the opencode-go provider at all. OpenCode Go now requires a stable x-opencode-session header on every request and rejects traffic without it. VT Code never sends it.

Reproduction (cheap: free Go model, no allowance used)

Requires an OpenCode Go API key, but beam-free is one of the free models included in Go, so this consumes no allowance:

# fails
curl -s https://opencode.ai/zen/go/v1/chat/completions \
  -H "Authorization: Bearer $OPENCODE_GO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"beam-free","messages":[{"role":"user","content":"hi"}]}'
# {"type":"error","error":{"type":"MissingSessionID","message":"Request is missing x-opencode-session and cannot be routed efficiently. ..."}}

# same request with the header -> 200 and a normal completion
curl -s https://opencode.ai/zen/go/v1/chat/completions \
  -H "Authorization: Bearer $OPENCODE_GO_API_KEY" \
  -H "x-opencode-session: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{"model":"beam-free","messages":[{"role":"user","content":"hi"}]}'

Same failure through VT Code:

vtcode secret add opencode-go
vtcode --provider opencode-go --model glm-5.3 ask "hi"
# Error: Invalid request: OpenCode Go invalid request: Request is missing x-opencode-session ...

Reproduces for every model I tried (glm-5.3, glm-5.2, gpt-5.6-luna, kimi-k3, beam-free), streaming and non-streaming. vtcode models list and vtcode secret status opencode-go report the provider as ready, so authentication is not the problem.

Cause

https://opencode.ai/v2/docs/console/go/#where-can-i-use-it requires clients to send a stable session id in x-opencode-session (and their own user agent). The provider never sets it.

Zen is not affected today: the same request to https://opencode.ai/zen/v1 succeeds without the header (Zen free models need no key at all, so they are not usable as a repro either).

Workaround (verified)

Run a local proxy in front of the endpoint that adds the header, then point VT Code at it:

export OPENCODE_GO_BASE_URL="http://127.0.0.1:8790/zen/go/v1"

The proxy forwards /zen/go/* to https://opencode.ai, adds one persisted x-opencode-session value to every request, and streams the response body back unchanged (SSE included). With it, VT Code works as expected:

  • vtcode --provider opencode-go --model glm-5.3 ask "..." answers normally
  • streaming chat completions arrive incrementally
  • tool calls work (a request with a list_files tool returns finish_reason: "tool_calls")
  • a multi-turn vtcode exec run completes (five sequential requests, all 200)

Minimal version of the proxy (single file, stdlib only, keeps the API key untouched):

import http.client, http.server, uuid
SESSION = str(uuid.uuid4())  # any stable value per conversation works
class Handler(http.server.BaseHTTPRequestHandler):
    protocol_version = "HTTP/1.1"
    def handle_one_request(self):
        try:
            self.raw_requestline = self.rfile.readline()
            if not self.parse_request():
                return
            body = self.rfile.read(int(self.headers.get("Content-Length") or 0))
            headers = {k: v for k, v in self.headers.items()
                       if k.lower() not in ("host", "connection", "content-length", "transfer-encoding")}
            headers["x-opencode-session"] = SESSION
            conn = http.client.HTTPSConnection("opencode.ai", 443, timeout=600)
            conn.request(self.command, self.path, body=body, headers=headers)
            upstream = conn.getresponse()
            self.send_response(upstream.status)
            for key, value in upstream.getheaders():
                if key.lower() not in ("content-length", "transfer-encoding", "connection", "keep-alive"):
                    self.send_header(key, value)
            self.send_header("Transfer-Encoding", "chunked")
            self.end_headers()
            while chunk := upstream.read(65536):
                self.wfile.write(b"%x\r\n" % len(chunk) + chunk + b"\r\n")
                self.wfile.flush()
            self.wfile.write(b"0\r\n\r\n")
            conn.close()
        except Exception as exc:
            self.log_error("proxy error: %s", exc)
http.server.ThreadingHTTPServer(("127.0.0.1", 8790), Handler).serve_forever()

This is only a workaround; the header belongs in the provider.

Fix: the piece that has to change

OpenAiCompatSpec already has the hook, it is just not wired up for OpenCode Go (xAI uses it for x-grok-conv-id, crates/codegen/vtcode-llm/src/providers/xai.rs):

// crates/codegen/vtcode-llm/src/providers/openai_compat.rs
/// Optional HTTP header name for session/cache affinity (e.g. xAI `x-grok-conv-id`).
fn session_affinity_header() -> Option<&'static str> {
    None
}

dispatch() already attaches it with the lineage from prompt_cache_key, so the value is stable per conversation. The fix is to add the impl in OpenCodeGoInnerSpec (crates/codegen/vtcode-llm/src/providers/opencode_shared.rs):

impl OpenAiCompatSpec for OpenCodeGoInnerSpec {
    // ...
    fn session_affinity_header() -> Option<&'static str> {
        Some("x-opencode-session")
    }
}

The GoProtocol::MessagesApi path in opencode_go.rs goes through AnthropicProvider and builds its own headers, so it needs the same header passed through separately.

glanzel avatar Nov 02 '26 16:11 glanzel

Thanks you for reporting the issue, I will check it soon.

vinhnx avatar Nov 03 '26 00:11 vinhnx

Thank you for the detailed report and the repro, @glanzel — this is fixed. VT Code now sends a stable x-opencode-session on every OpenCode Go request, on both the chat-completions and Messages-API paths, with an instance-stable fallback when no session lineage is available. Your workaround is no longer needed. The fix will ride the next release.

vinhnx avatar Nov 03 '26 02:11 vinhnx

thank you very much

glanzel avatar Nov 03 '26 10:11 glanzel