Skip to content

Commit 072f61d

Browse files
opencodeendolith
authored andcommitted
fix: revert the key-shape change; Go keys are oc_sk_, not sk-
Reverts the key half of 5d9640e. That commit concluded from anomalyco/opencode#40343 and #44948 that Go keys are `sk-` plus 64 characters and widened `_is_opencode_go_key` to accept `sk-`, flattening the precedence rules to "the environment always wins". Both conclusions were wrong: those issues describe **Zen** keys, which is a different product, and a Go key starts with `oc_sk_`. The widened predicate was actively harmful, not merely unnecessary. `sk-` is the format shared by OpenAI, Anthropic, DashScope and Zen, so accepting it hands every foreign profile key straight to the gateway -- reintroducing exactly the `Invalid API key.` failure the guard was added to prevent, and discarding the shape test's only reason to exist. The corrected code keeps `oc_` only, restores the original precedence (a Go-shaped profile key is kept, a foreign one is replaced by the environment variable or refused), and the docstring now says plainly not to widen it on the strength of a Zen source. The three tests I rewrote asserted the wrong premise and are restored to their original forms, with docstrings noting what went wrong so the next person does not re-derive it from the same issue numbers. test_go_shaped_profile_key_is_kept is back: under the original precedence it is the inverse case the other two do not cover, not a duplicate. The rest of 5d9640e stands, because it did not depend on the key claim: the Go catalog is now explicit sets transcribed from the published endpoint table rather than a range comprehension that invented models, `deepseek-v4-flash- vision-exp` is answered for as vision-capable instead of having its images silently replaced by text, and the catalog and vision behaviour are documented. Qwen3.5 Plus is left out of the messages list because it is not in the current Go endpoint table; the table is the authority here, and GET /zen/go/v1/models is the live version of it. Suite unchanged at the known baseline of 14.
1 parent 38fcec6 commit 072f61d

3 files changed

Lines changed: 84 additions & 91 deletions

File tree

‎docs/settings/all-settings.mdx‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,12 @@ llm:
243243

244244
Use an OpenCode Go subscription by prefixing the model with `opencode_go/`. Set
245245
`OPENCODE_GO_API_KEY` to your key from [opencode.ai](https://opencode.ai/docs/go/)
246-
(not your OpenAI key — it is a different credential). A key saved in your profile
247-
as `llm.api_key` also works; if both are set, `OPENCODE_GO_API_KEY` wins. Note
248-
that a Go key starts with `sk-` just like an OpenAI one, so the two cannot be
249-
told apart automatically — if a request fails with `Invalid API key.`, check
250-
that the key in your profile really is the Go one.
246+
(not your OpenAI key — it is a different credential). A Go key saved in your
247+
profile as `llm.api_key` also works, and a key already there in Go's `oc_sk_…`
248+
shape is kept; anything else is replaced by the environment variable, or refused
249+
with an error naming what to set. Note that Go keys and OpenAI keys look nothing
250+
alike (`oc_sk_…` versus `sk-…`), so a key saved for another provider is never sent
251+
to the Go gateway.
251252

252253
<CodeGroup>
253254

@@ -262,7 +263,7 @@ interpreter.llm.model = "opencode_go/deepseek-v4-flash"
262263
```yaml Profile
263264
llm:
264265
model: opencode_go/deepseek-v4-flash
265-
api_key: sk-...
266+
api_key: oc_sk_...
266267
```
267268

268269
</CodeGroup>

‎interpreter/core/llm/llm.py‎

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -135,26 +135,21 @@ def _is_opencode_go_base(api_base):
135135

136136

137137
def _is_opencode_go_key(api_key):
138-
"""Whether an already-configured api_key is shaped like an OpenCode credential.
139-
140-
OpenCode issues Zen/Go keys as ``sk-`` followed by 64 characters (see the
141-
console's ``Key.create`` in anomalyco/opencode, and anomalyco/opencode#40343,
142-
where a Go key is described as "sk-... API key from the Zen console"). An
143-
earlier version of this assumed an ``oc_`` prefix and consequently treated
144-
every real Go key as foreign, so a key saved in a profile was discarded and
145-
the user was told to set ``OPENCODE_GO_API_KEY`` instead -- the profile path
146-
documented in docs/settings/all-settings.mdx could not work.
147-
148-
``oc_`` is still accepted: it is accepted by the gateway, and profiles
149-
written when that was assumed may hold such a value.
150-
151-
Note the limit of prefix matching here: ``sk-`` is also OpenAI's own format,
152-
so this cannot actually tell a Go key from an OpenAI one. It only rejects
153-
keys that are neither shape. That asymmetry is deliberate -- wrongly
154-
rejecting a valid Go key blocks a working configuration, while a genuinely
155-
foreign key still fails with the gateway's own clear "Invalid API key."
138+
"""Whether an already-configured api_key is shaped like an OpenCode Go credential.
139+
140+
Go keys start with ``oc_`` (``oc_sk_...``). Anything else is assumed to belong
141+
to another provider, so that stale keys from the profile -- an OpenAI key left
142+
there by OI's onboarding, for example -- cannot be sent to the gateway instead
143+
of OPENCODE_GO_API_KEY. Sending a foreign key yields exactly ``Invalid API
144+
key.``, which blames the Go key that never arrived.
145+
146+
The ``oc_`` prefix is what makes this test work at all: ``sk-`` is the format
147+
shared by OpenAI, Anthropic, DashScope and *Zen*, so an ``sk-`` key here is
148+
never assumed to be a Go key. Do not "fix" this to accept ``sk-`` on the
149+
strength of a source about Zen credentials -- that is a different product, and
150+
accepting ``sk-`` would hand every foreign key straight to the gateway.
156151
"""
157-
return bool(api_key) and api_key.lower().startswith(("sk-", "oc_"))
152+
return bool(api_key) and api_key.lower().startswith(("oc_",))
158153

159154
# Models already warned about during this process when both a reasoning_effort and
160155
# include_reasoning: false are configured. That combination cannot take effect (a
@@ -896,23 +891,22 @@ def load(self):
896891
go_base = os.environ.get("OPENCODE_GO_API_BASE", _OPENCODE_GO_DEFAULT_BASE)
897892
if self.api_base is None or not _is_opencode_go_base(self.api_base):
898893
self.api_base = go_base
899-
# Precedence is: an explicit OPENCODE_GO_API_KEY, then a Go-shaped key
900-
# already in the profile, then a loud failure.
901-
#
902-
# The shape test cannot be the deciding vote. Go keys are `sk-`, and so
903-
# are OpenAI, Anthropic and DashScope keys, so no prefix separates a
904-
# Go credential from a foreign one -- the original design assumed they
905-
# differed (an `oc_` prefix), which silently discarded every real Go
906-
# key saved in a profile. A weak signal must therefore never override
907-
# an explicit choice: when the environment variable is set it wins,
908-
# and a profile key is only discarded when it is not recognisable at
909-
# all, which leaves the loud error below rather than a 401 from the
910-
# gateway blaming a key the user believes they set correctly.
911-
env_key = os.environ.get("OPENCODE_GO_API_KEY")
912-
if env_key:
913-
self.api_key = env_key
914-
elif not _is_opencode_go_key(self.api_key):
915-
self.api_key = None
894+
# Same reasoning as api_base: the profile may still hold an api_key
895+
# saved for a different provider (often an OpenAI key from OI's
896+
# onboarding). Sending that to the Go gateway produces exactly
897+
# "Invalid API key." at 401, which blames the Go key the user set
898+
# via the environment but never reaches the gateway. An explicit
899+
# OPENCODE_GO_API_KEY therefore wins over a non-Go-shaped profile
900+
# key; a Go-shaped key the user put in the profile is kept.
901+
if self.api_key is None or not _is_opencode_go_key(self.api_key):
902+
env_key = os.environ.get("OPENCODE_GO_API_KEY")
903+
if env_key:
904+
self.api_key = env_key
905+
# Without an override, a non-Go-shaped profile key cannot be
906+
# sent to the gateway; leaving it in place would repeat the
907+
# "Invalid API key." failure the check above exists to prevent.
908+
elif self.api_key is not None and not _is_opencode_go_key(self.api_key):
909+
self.api_key = None
916910
if not self.api_key:
917911
raise ValueError(
918912
"The opencode_go/ model prefix requires an OpenCode Go API key. "

‎tests/core/test_opencode_go.py‎

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -182,51 +182,50 @@ def test_openai_key_is_not_used_for_go(monkeypatch):
182182

183183

184184
def test_stale_profile_key_is_replaced_by_env_key(monkeypatch):
185-
"""An explicit OPENCODE_GO_API_KEY wins over whatever the profile holds.
186-
187-
Regression test for the second half of the reported failure: with a key left
188-
in default.yaml from another provider, the original `if self.api_key is None`
189-
guard kept it and the Go gateway rejected it with `Invalid API key.` -- which
190-
blames the Go key that was never sent.
191-
192-
This was originally written to assert that the environment wins only over a
193-
*non-Go-shaped* profile key, on the assumption that Go keys carry an `oc_`
194-
prefix and could be told apart from an OpenAI `sk-` key. That premise is
195-
wrong: Go keys are `sk-` plus 64 characters, and every other major provider
196-
uses `sk-` too, so the two cannot be distinguished by shape. The rule is
197-
therefore stated unconditionally -- an explicit environment key wins -- which
198-
is both predictable and what the documentation promises.
185+
"""A stale foreign api_key in the profile loses to OPENCODE_GO_API_KEY.
186+
187+
Regression test for the second half of the reported failure: with
188+
`llm.api_key: sk-…` (an OpenAI key saved by OI's onboarding) still in
189+
default.yaml, the original `if self.api_key is None` guard kept it, and the
190+
Go gateway rejected it with exactly `Invalid API key.` -- which blames the
191+
Go key that was never sent. The environment override now wins over a
192+
non-Go-shaped profile key.
199193
"""
200-
monkeypatch.setenv("OPENCODE_GO_API_KEY", "sk-from-env")
194+
monkeypatch.setenv("OPENCODE_GO_API_KEY", "oc_sk_from_env")
201195
interpreter = _configure("opencode_go/deepseek-v4-flash", monkeypatch)
202-
interpreter.llm.api_key = "sk-" + "9" * 64
203-
196+
interpreter.llm.api_key = "sk-stale-openai-key-from-profile"
204197
interpreter.llm.load()
198+
assert interpreter.llm.api_key == "oc_sk_from_env"
199+
205200

206-
assert interpreter.llm.api_key == "sk-from-env"
201+
def test_go_shaped_profile_key_is_kept(monkeypatch):
202+
"""A profile key that is already OpenCode-shaped is deliberate config, kept.
207203
204+
This is the inverse of the test above, and it is the case that would break if
205+
the environment override were allowed to clobber a key the user saved on
206+
purpose.
207+
"""
208+
monkeypatch.setenv("OPENCODE_GO_API_KEY", "oc_sk_from_env")
209+
interpreter = _configure("opencode_go/deepseek-v4-flash", monkeypatch)
210+
interpreter.llm.api_key = "oc_sk_from_profile"
211+
interpreter.llm.load()
212+
assert interpreter.llm.api_key == "oc_sk_from_profile"
208213

209-
def test_unrecognised_profile_key_without_env_key_fails_loudly(monkeypatch):
210-
"""No OPENCODE_GO_API_KEY and an unrecognisable profile key: refuse, never send.
211214

212-
The key is not shaped like any provider credential, so sending it would earn
213-
the same `Invalid API key.` failure while burying the real problem. Clearing
214-
it and raising names the actual fix instead.
215+
def test_foreign_profile_key_without_env_key_fails_loudly(monkeypatch):
216+
"""No OPENCODE_GO_API_KEY and a foreign profile key: refuse, never send it.
215217
216-
Renamed and re-keyed from the earlier version of this test: it treated
217-
`sk-…` as the foreign shape, which was based on the incorrect belief that Go
218-
keys do not start with `sk-`. Since they do, `sk-` is now a *recognised*
219-
shape and the case is covered by
220-
test_go_key_from_profile_is_kept; what is left to test is a key that is not
221-
a credential of any recognisable form.
218+
The profile key belongs to another provider. Falling back to it would send
219+
it to the Go gateway and produce the exact `Invalid API key.` failure again;
220+
clearing it and raising names the actual problem instead.
222221
"""
223222
monkeypatch.delenv("OPENCODE_GO_API_KEY", raising=False)
224223
interpreter = _configure("opencode_go/deepseek-v4-flash", monkeypatch)
225-
interpreter.llm.api_key = "not-a-recognised-credential"
224+
interpreter.llm.api_key = "sk-stale-openai-key-from-profile"
226225

227226
with pytest.raises(ValueError, match="requires an OpenCode Go API key"):
228227
interpreter.llm.load()
229-
assert interpreter.llm.api_key is None
228+
assert interpreter.llm.api_key != "sk-stale-openai-key-from-profile"
230229

231230

232231
@pytest.mark.parametrize(
@@ -263,22 +262,22 @@ def test_chat_completions_models_are_accepted(monkeypatch):
263262

264263

265264
def test_real_go_key_shape_is_recognised():
266-
"""Go keys are `sk-` + 64 chars, so a real key must not be treated as foreign.
267-
268-
This predicate decides whether a key already in the profile is kept or
269-
discarded as belonging to another provider. It previously only recognised an
270-
`oc_` prefix, which OpenCode does not issue: the console's `Key.create`
271-
produces `sk-` plus 64 characters, and anomalyco/opencode#40343 describes a
272-
Go key as "sk-... API key from the Zen console". The effect was that a valid
273-
Go key saved in a profile was discarded and the user was told to set
274-
OPENCODE_GO_API_KEY instead, so the profile route could never work.
265+
"""Go keys are `oc_sk_…`, and that shape is what makes the guard work.
266+
267+
The `oc_` prefix is the only thing separating a Go credential from a foreign
268+
one here, because `sk-` is the format shared by OpenAI, Anthropic, DashScope
269+
and Zen. A source describing Zen keys as `sk-` plus 64 characters
270+
(anomalyco/opencode#40343, and the console's `Key.create` reported in
271+
anomalyco/opencode#44948) is about a *different product*: accepting `sk-`
272+
here would hand every foreign profile key straight to the gateway, which is
273+
the failure this guard exists to prevent.
275274
"""
276275
from interpreter.core.llm.llm import _is_opencode_go_key
277276

278-
assert _is_opencode_go_key("sk-" + "a" * 64) is True
279-
# Still accepted, for profiles written while the oc_ assumption held.
280-
assert _is_opencode_go_key("oc_sk_abc123") is True
281-
# Only a key that is neither shape is rejected.
277+
assert _is_opencode_go_key("oc_sk_" + "a" * 64) is True
278+
# Zen/OpenAI-style keys are deliberately NOT Go keys here.
279+
assert _is_opencode_go_key("sk-" + "a" * 64) is False
280+
assert _is_opencode_go_key("sk-proj-abc") is False
282281
assert _is_opencode_go_key("") is False
283282
assert _is_opencode_go_key(None) is False
284283

@@ -291,7 +290,7 @@ def test_go_key_from_profile_is_kept(monkeypatch):
291290
variable also being set.
292291
"""
293292
monkeypatch.delenv("OPENCODE_GO_API_KEY", raising=False)
294-
key = "sk-" + "b" * 64
293+
key = "oc_sk_" + "b" * 64
295294
interpreter = _configure("opencode_go/deepseek-v4-flash", monkeypatch)
296295
interpreter.llm.api_key = key
297296

@@ -301,11 +300,10 @@ def test_go_key_from_profile_is_kept(monkeypatch):
301300

302301

303302
def test_no_key_at_all_still_fails_loudly(monkeypatch):
304-
"""Widening the accepted key shapes must not turn into a silent default.
303+
"""The empty case is what carries the guarantee that no key is never sent.
305304
306-
Refusing when there is genuinely no key is the whole point of the check; the
307-
prefix test only rejects foreign-looking keys, so the empty case is what
308-
carries the guarantee.
305+
The shape test only rejects foreign-looking keys; refusing outright when
306+
there is genuinely no key is the whole point of the check.
309307
"""
310308
monkeypatch.delenv("OPENCODE_GO_API_KEY", raising=False)
311309
interpreter = _configure("opencode_go/deepseek-v4-flash", monkeypatch)

0 commit comments

Comments
 (0)