Commit Graph
9 Commits
Author SHA1 Message Date
yetone e4197144a8 perf: every package's package-level regexps compile on first use
v0.1.3516 made gateway's and redact's patterns lazy. The other 299
package-level regexp.MustCompile calls, in 148 files across agent,
provider, gui, library, codexcat, sessions, usage, plugin and the rest,
now go through lazyre.New too, so a launch that never reaches them
(`magpie statusline`, run by Claude Code on every message) doesn't
compile them. Fields, maps and params that held them are *lazyre.RE.

Measured on mote (M5 Pro), sandbox HOME, v0.1.3516-equivalent build
(t42) against this one (t43):
- GODEBUG=inittrace=1 sum: 7.39 MB / 69,837 allocs -> 4.62 MB / 46,765
- `magpie statusline`, 3 runs each: instructions retired ~212M -> ~176M,
  peak memory footprint 19.2-19.4 MB -> 16.8-17.0 MB

Run: go vet ./... (darwin) ok; GOOS=windows go vet on each of the 30
changed packages ok; GOOS=linux go vet ok except ., internal/cli and
internal/gui, which stop in wails v3's cgo files when cross-compiled
(the same at main). New TestAllLazyPatternsCompile at the root compiles
all 574 registered patterns (ok), so a bad pattern fails a test rather
than a first use.
Not run before push: the changed packages' own test suites; they run
on mote after it.
2026-11-10 12:54:39 +08:00
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
yetone f0f63bf6b8 provider: Zen's free models answer again: asked at opencode.ai/inference/v1, with stand-in tools that take a parameter, and without x-session-affinity (c9cu on Discord)
c9cu: "opencode muse 1.3 又不行了,magpie v0.1.1239". Zen's free tier moved and
tightened in mid-October 2026, found against the real upstream on 10-15:

- free models answer only at https://opencode.ai/inference/v1 (models.dev's
  opencode api is that URL now); OpenCodeFreeURL maps /zen/... there for a
  free model, never /zen/go or another host, in forwardOnce and the probe;
- the bash and read tools offered must each have at least one parameter:
  the stand-ins (zenfree.go, the probe) now carry OpenCode's own
  (OpenCodeToolSchemas);
- a request carrying x-session-affinity is answered 403 FreeTierError.
  OpenCode 1.19.0, captured through a forwarding proxy, sends Zen neither
  x-session-affinity nor X-Session-Id, so OpenCodeClient takes both out;
  the agent's own session id still names x-opencode-session.
  Bisected header by header: X-Session-Id, Accept and the UA version
  don't matter; x-session-affinity alone turns a 200 into a 403.

Live against the real Zen with the public key (a test file since removed):
muse via /v1/responses streamed (xhigh) and not, big-pickle on chat
streamed, mimo-v2.6-flash-free on chat: all 200; muse via /v1/messages 200.
The same code with x-session-affinity left in: 403 on every case.

Run: gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows
-tags nogui on internal/provider and internal/gateway; the full
go test -tags nogui -count=1 ./... in a sandbox HOME, whose one red was
gateway's TestOpenCodeClientHeaders still expecting the two headers
(updated here), then internal/gateway alone again: ok (678s).
New tests TestZenFreeAtInferenceURL, TestOpenCodeFreeURL and the probe's
non-empty properties check fail on the old code.

The community plugin got the same fix in magpie-community/plugins 5fc0a75
(zen-free 0.1.7); its npm publish is blocked on the repo's publisher setup.
2026-10-15 02:25:17 +08:00
yetone 8e6354714f provider: OpenCode Zen's free models answer again, with a session id OpenCode itself could have made (c9cu on Discord)
c9cu (v0.1.1222, Pi, a group routed to OpenCode Zen's
muse-spark-1.3-contributor-free on /v1/responses) got 403 FreeTierError
"OpenCode's free tier can only be used from within OpenCode" on a setup
that worked the day before. Zen now reads x-opencode-session as
OpenCode's identifier.ts lays an id out (12 hex of ~(Date.now()*0x1000 +
counter), then 14 base62) and refuses one whose counter isn't small.
magpie's built-in sent a sha256 of the conversation cut to that shape,
so its counter was anything. The zen-free plugin hit the same (#100 on
magpie-community/plugins, 0.1.5); the built-in had the same code.

Header diff against the real upstream, same request with curl:
- magpie's ses_e9283da055b15JwOAra76bwUx4 (hash): 403 on /v1/responses
  and /v1/chat/completions.
- ses_ecc5d8f28ffe9Cy14V8Em7lI2u from a real OpenCode 1.19.0 run, the
  old opencode/1.18.34 UA unchanged: 200 on /v1/responses. So it is the
  session, not the UA or the endpoint; muse-spark is still served on
  Responses (Chat answers 400 ModelProtocolUnsupported, as before).

Now (opencode_client.go):
- openCodeSession makes a real descending id from the current time and
  a per-millisecond counter, kept per conversation (the last 1000) so a
  conversation's turns share one session; one OpenCode could have made
  is passed on unchanged, a hash-shaped one is not.
- x-opencode-request is an ascending msg_ id the same way.
- the session also goes in x-opencode-session-id, x-session-affinity
  and X-Session-Id, as OpenCode's packages/core/src/session/
  model-request.ts sends it, and the User-Agent is OpenCode 1.19.0's
  (opencode/1.19.0 ai-sdk/provider-utils/4.0.23 runtime/bun/1.3.14,
  the latest on npm, as the plugin captured it).
- Kilo's task/session id goes through the same openCodeSession (the
  Kilo CLI is OpenCode's fork and makes ids the same way).

Run:
- Through an in-process gateway under a sandbox HOME against the real
  opencode.ai/zen with the public key: muse-spark-1.3-contributor-free
  on /v1/responses with reasoning xhigh, streamed and not, big-pickle
  streamed on Chat, mimo-v2.6-flash-free on Chat: all 200 "pong". The
  same file on origin/main's code: all 403 FreeTierError, the
  reporter's message word for word.
- TestOpenCodeSessionIsOpenCodes, TestOpenCodeSessionsKeptBounded and
  TestOpenCodeClientHeaders (now with a real 1.19.0 session) fail with
  the hash put back by hand.
- gofmt -l, go vet (darwin, GOOS=linux, GOOS=windows -tags nogui) on
  internal/provider and internal/gateway.
- go test -tags nogui -count=1 ./... under a temp HOME on base
  eba718736 (load average ~170): every package ok but provider
  (TestPluginModelsSayExpired, TempDir cleanup race, added to #1543),
  gateway and agent (10m timeout in TestNotAnAPIReplyFailsOver and
  TestOmpLevelSuffix). Rerun with -timeout 40m: provider, gateway and
  agent ok.
- Rebased onto 488569227 (kilo.go's model paging, no overlap) and not
  rebuilt there: the disk was under 10G free.

- Kilo Gateway, anonymous, nvidia/nemotron-3.5-lightning:free through
  the same in-process gateway: 200 twice.
2026-10-13 14:03:58 +08:00
yetone c65ddd988d opencode zen: big-pickle and Zen's other free models without -free in their id are asked as OpenCode asks, not only -free ones (361 on Discord: 能否修复opencode zen免费级无法使用的问题) — magpie already asked Zen's -free models as OpenCode does (streamed, tools named bash and read, a UA of opencode/1.18.34, an x-opencode-session of ses_ + 12 hex + 14 base62, the rules the reporter's picture lists), but OpenCodeFree knew a free model by its -free suffix alone, so big-pickle, free on Zen though its id doesn't say so, was relayed as the client sent it and Zen answered 403 "OpenCode's free tier can only be used from within OpenCode" (500 on Messages and Responses). A model is now free when its id ends in -free, when it is big-pickle or grok-code, or when models.dev's opencode prices it at nothing (input and output 0), so the gateway's reshaping (zenfree.go) and the Test button's probe take it too; a model models.dev prices is not touched. Through an in-process gateway under a sandbox HOME against the real opencode.ai/zen: zen/big-pickle answered 200 on Chat, Messages and Responses (streamed); on the old code 403 FreeTierError on Chat, 500 on the others. TestZenFreeZeroCostModels (big-pickle, a catalog zero-cost model, a priced one) fails on the old code. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass (TestProvidersAnswerWhileListsComeIn flaked once under load, passes 3/3 alone). 2026-10-05 06:07:03 +08:00
yetone b0486663d2 providers: OpenCode Zen's free models answer with no key, as OpenCode signed out asks them (#572: opencode zen的free模型,只能在他自己的cli里面用,能否做插件反代出来?) — magpie has asked Zen's free models as OpenCode does since v0.1.615 (zenfree.go), but the OpenCode Zen preset wanted a key, which means an opencode.ai account. OpenCode itself, signed out, asks Zen with the key "public", and Zen lists and serves its -free models to it (checked: /zen/v1/models with it lists twelve). The preset's key is now optional ("optional: free models need no key"), and a Zen provider saved with none asks with OpenCode's (normalize, provider.OpenCodeAnonymousKey); a key of the user's own is kept, and the editor's blank key still keeps the one saved. TestOpenCodeZenNeedsNoKey (keyless Add refused without the change) is new. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass. 2026-10-02 22:31:49 +08:00
yetone 16e373b924 opencode: Zen's free models answer through magpie from Claude Code, Codex and the model test too (ARNO on Discord: mimo-v2.6-flash-free 没有响应: 403 · OpenCode's free tier can only be used from within OpenCode; the user: 只要不是 claude code,都可以尝试伪装一下) — beyond OpenCode's headers (v0.1.615), Zen's free tier takes only a streamed request offering tools named bash and read. A request to a -free model on opencode.ai now always streams upstream, a client that didn't ask for a stream getting the reply collected; a caller's Bash/Read (any case) go as bash/read, as do earlier calls in the history and a forced tool_choice, and calls in the reply get the caller's names back, streamed or not, in each search round too; when bash or read is missing a stub is offered whose description says never to call it, and a call to a stub never reaches the client (a turn with nothing else ends on its text, or a short note, as a stop). Zen lists its -free models again. Paid Zen models and other providers are untouched. Real requests to opencode.ai/zen/v1 with the anonymous key: Claude Code-style Bash/Read streamed and not, Codex-style shell on /v1/responses, no tools non-stream, and the model test all answered 200. TestZenFree* fail without the change. 2026-10-02 09:23:29 +08:00
yetone 3515d99a9c gateway: a request to OpenCode's gateway (Zen or Go) says it is OpenCode's, as OpenCode itself sends it (the user: free Zen models 403 "OpenCode's free tier can only be used from within OpenCode"; mimicking OpenCode is allowed on this route) — magpie sent User-Agent magpie/<version> and its own conversation id (magpie-…) in x-opencode-session. OpenCode (packages/opencode/src/session/llm/request.ts, for a provider whose id starts with "opencode") sends User-Agent opencode/<version>, x-opencode-session (ses_…), x-opencode-request (msg_…), x-opencode-client and x-opencode-project; every request to opencode.ai now carries them: opencode/1.18.34, the conversation as an id of OpenCode's form (one already ses_… goes as it is, any other is made from it, the same for the same conversation), a fresh msg_… id, cli and global. Prompts and body go as the caller sent them; other providers get none of it. Zen's free tier also asks for OpenCode 1.18.0 or newer (426 otherwise), a streamed request, and tools named bash and read: a live request through the gateway with the anonymous key "public" to mimo-v2.6-flash-free went from 403 to 200 for a streamed one offering bash and read (OpenCode's and Pi's), and stays 403 for Claude Code's Bash/Read or no tools, so the free models stay off the list. TestOpenCodeClientHeaders fails without the change; TestOpenCodeGetsConversationSession now expects OpenCode's form. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass. 2026-10-02 08:28:35 +08:00