Commit Graph
2 Commits
Author SHA1 Message Date
yetone 9feb1024c7 zen: free models are asked as OpenCode 2.2.5 asks them (#3491)
laomao04_65777 on Discord: Zen's free models answered 429 "Free usage limit
reached. Free models work in OpenCode" to the built-in OpenCode Zen provider,
while real OpenCode 2 on the same machine was served. magpie sent OpenCode
1.x's shape. It now sends what OpenCode 2.2.5 sent in a captured request
(internal/provider/testdata/opencode-2.2.5-chat.json, from a recording proxy),
as the zen-free plugin does since plugins d62193c and 8501988:

- User-Agent opencode/latest/<v>/cli. <v> is the version OpenCode's updater
  (opencode.ai/update/api/latest/cli/npm) names, never below 2.2.5 and never
  anything but a plain x.y.z. It is read in the background at most every 6h,
  with a 3s timeout and no redirects.
- x-opencode-project is OpenCode's project id for the folder the agent says it
  works in: sha1 of git-remote:host/path (origin, else the first remote;
  worktree .git files followed), else of directory:<root or folder>. It is no
  longer "global".
- b3 and traceparent: one trace a conversation, a new span a request.
- The session id goes in x-opencode-session, x-opencode-session-id,
  x-session-affinity and x-session-id. Accept is */*, and x-opencode-request
  is gone.
- On a free request the agent's own headers (Claude Code's x-stainless-*,
  Codex's originator) are dropped. Only Content-Type and Anthropic-Version
  and Anthropic-Beta are kept.
- The core tools are edit, glob, grep, read and shell with 2.2.5's schemas,
  strict false on Chat. Claude Code's Read, Edit, Glob and Grep are offered
  under those names. Its Bash is no longer bash: shell is a stub.
- OpenCode's <env> block, word for word, for the agent's folder, is added to
  the system prompt unless it has one already (OpenCode's own).
- Paths are the v2 catalog's: /inference/openai/v1 for Chat and Responses,
  and /inference/anthropic/v1 for Messages. The models list is unchanged.

Every route takes this: chat, responses and messages through forwardOnce, the
Test button's probe (tiny/zenFreeProbe), and the models list
(listHeadersAt: UA and x-opencode-client).

Sibling: paid Zen and OpenCode Go share OpenCodeClient, so they get 2.2.5's
headers. Their /zen/v1 and /zen/go/v1 paths are left as they are: #4740.

Run on mote (mote-run) at this tree:
- gofmt -l . is clean.
- GOOS=darwin and windows go vet ./... pass. GOOS=linux go vet passes for
  internal/provider and internal/gateway. The whole-tree Linux vet stops in
  Wails' cgo when run from macOS, and in WSL for want of gtk4, before it
  reaches this change.
- go test ./internal/provider ./internal/gateway -count=1 -timeout 45m:
  ok provider (154s) and ok gateway (715s) on base 788057340. After the
  rebase onto a5a9cdf7b, which touched gateway.go and ir.go, the same
  tree passed again: ok provider (138s) and ok gateway (816s), with GOOS
  darwin, linux and windows go vet of both packages.
- Rebased again onto 4c815ac2a, which touched search.go and provider.go.
  At that head gofmt and the three-OS vet of both packages are clean, and
  -run 'OpenCode|Zen|Search|Searcher' passes in both packages. The full
  suites were not rerun after this last rebase.

Broken by hand, each turning a test red (provider and gateway, -run
'OpenCode|Zen', on mote):
- version 1.19.0: TestOpenCodeClientIs225 and TestOpenCodeVersionFromUpdater.
- the old /inference/v1 path: TestOpenCodeFreeURL, TestZenFreeAsOpenCode225,
  TestZenFreeAtInferenceURL and TestZenPublicTestAsksFreeModels.
- caller headers kept: TestZenFreeAsOpenCode225.
- no env block: TestZenFreeAsOpenCode225.
- no strict: TestZenFreeAtInferenceURL.
- project "global": TestOpenCodeClientIs225, TestOpenCodeClientHeaders and
  TestZenFreeAsOpenCode225.
- the same span on every request: TestOpenCodeClientIs225 and
  TestOpenCodeClientHeaders.
- no 2.2.5 floor: TestOpenCodeVersionFromUpdater.
- a changed read schema: TestOpenCodeToolsAre225s.

Not tried:
- A real 200 from a free model: this egress IP's free allowance is spent.
  The captured 2.2.5 request itself got 429.
- Header order and case: Go's HTTP/1 writes headers sorted, and HTTP/2
  lowercases them, so OpenCode's order cannot be copied.
- Windows' project id against real OpenCode on Windows.
2026-11-01 14:48:19 +08:00
yetone d1f57b70ee provider: every request to OpenCode's gateway names its conversation's session, at any port, lists, decisions, usage and the probe too, Codex's thread by its prompt_cache_key (luci on Discord)
OpenCode Go, added as a custom provider and asked from Codex, answered some
users 400 "Request is missing x-opencode-session and cannot be routed
efficiently", the Test button's probe too; a static header on the provider
stopped the 400 but put every conversation on one backend.

OpenCode's own client (packages/core, 4397ee4) sends x-opencode-session =
SessionAffinity (parentID ?? fork ?? id) on every model request, and a
fresh session for stateless calls; the console's inference proxy routes
every /zen and /zen/go path, models and usage included, by it.

- OpenCodeURL/openCodeHost: opencode.ai or a subdomain, by hostname, so a
  base URL naming a port (https://opencode.ai:443/zen/go/v1) is OpenCode's;
  IsOpenCode, OpenCodeFreeURL and openCodeRoot share it. Before, the turn's
  session was set only when p.Host() matched without a port.
- gateway: the turn's seed is the client's own x-opencode-session, else
  sessionOf (X-Magpie-Session, Codex's session_id, …), else the body's
  prompt_cache_key, else conversationID. Without the cache key two Codex
  conversations opened in one folder (same environment message) were one
  session.
- Sign: a request to OpenCode's gateway that names no session gets a fresh
  one (OpenCodeSession), so System One decisions and anything else signed
  are never refused; the user's own header still wins.
- Model lists (FetchURL, FetchAtListed, FetchGemini) get a fresh session
  via listHeadersAt, Go's usage windows via planWindows, the probe a
  session of its own.
- A 4xx/5xx from OpenCode's gateway logs host, path, the sent headers'
  names (never values), x-opencode-session's first 8 chars or "missing",
  and whether the provider's own header pinned it.

Run (worktree, -tags nogui -p 1):
- go test ./internal/gateway -run TestOpenCodeGoSessionPerConversation and
  -run 'OpenCode|Zen|Session|Conversation|Decide|SystemOne|UserHeader|Kilo|Cline|Gemini': ok
- go test ./internal/provider -run TestOpenCodeSessionOnEveryCall and
  -run 'OpenCode|Zen|Sign|Header|Fetch|Model|Probe|PlanQuota|Quota|Decide|Detect|Kilo|Cline|UserAgent': ok
- gofmt -l clean; go vet -tags nogui GOOS=darwin/linux/windows on both
  packages: ok
Broken by hand: seed back to conversationID -> red (threads c and d one
session); old IsOpenCode -> red (port provider's turns two sessions);
Sign's backstop removed -> red; list session removed -> red. The probe's
own OpenCodeClient removed stays green: Sign's backstop gives it a fresh
session anyway.
Not run: the full suite; no request to the real OpenCode gateway; why
luci's users miss the header (port, proxy/mirror host, a client header) is
not confirmed, the new log line will tell.

Full suite: SUITE_LINE
2026-10-15 23:08:04 +08:00