Files
yetone 9074723932 zen-free 0.1.5: a request carries a session id OpenCode itself could have made, so Zen's free tier serves it again (#100, xqvvu)
Zen's chat endpoints now answer the free tier's requests 403 FreeTierError
("can only be used from within OpenCode") unless x-opencode-session looks
like an id OpenCode made. 0.1.4 sent a sha256 cut to the ses_ shape; Zen
decodes the id as OpenCode's identifier.ts lays it out (12 hex of
~(Date.now()*0x1000 + counter), then 14 base62) and turns away one whose
counter is not small.

Bisected against the real upstream by replaying 0.1.4's captured request:
- 0.1.4's ses_e9283da055b15JwOAra76bwUx4: 403. The same request with the
  session of a real OpenCode 1.19.0 run: 200. The real UA alone, or a real
  msg_ request id alone: still 403.
- Any timestamp passes (epoch 0, a year back, an hour ahead); the low 3 hex
  ffe/ffd/ff0 (counter 1/2/15) pass, f00/c00/800/001 get 403; 25 or 27
  characters after ses_ get 403.

Now:
- sessionID makes a real descending id with the current time and counter,
  kept per conversation (sha256 of the signal, at most 1000 kept) so a
  conversation's turns share one session. An id the caller sent through
  that OpenCode could have made is passed on unchanged.
- x-opencode-request is an ascending msg_ id, x-opencode-session-id is
  sent too, 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), as
  captured from the real client.

Run: bun test in packages/zen-free, 166 pass. With 0.1.4's hash put back,
the new session tests and the streaming header test go red (3 fail).
tools-smoke.mjs against the real opencode.ai: big-pickle,
mimo-v2.6-flash-free and muse-spark-1.3-contributor-free each 200 with a
Bash call, streamed and not; 0.1.4's provider.mjs in the same run: 403
FreeTierError.

Not done here: magpie maps this 403 to permission_error, which reads as a
sign-in problem; that is magpie's gateway, not this package.
2026-10-13 13:10:07 +08:00

56 lines
2.4 KiB
JavaScript

import assert from "node:assert/strict";
import test from "node:test";
import { opencodeID, realSessionID, sessionID } from "./provider.mjs";
const SHAPE = /^ses_[0-9a-f]{12}[0-9A-Za-z]{14}$/;
// Decode an id the way OpenCode's identifier.ts lays it out.
function decode(id, descending) {
let value = BigInt(`0x${id.slice(4, 16)}`);
if (descending) value = ~value & 0xffffffffffffn;
return { time: Number(value / 0x1000n), counter: Number(value % 0x1000n) };
}
test("a made-up session id is OpenCode's: descending, the time it was made, a small counter", () => {
const before = Date.now();
const id = sessionID(new Headers({ "x-session-id": "conversation-a" }), {});
const after = Date.now();
assert.match(id, SHAPE);
assert.ok(realSessionID(id), id);
const { time, counter } = decode(id, true);
// OpenCode keeps 48 bits, so the time is Date.now() modulo 2^36.
const wrap = 2 ** 36;
assert.ok(time >= before % wrap && time <= after % wrap, `${time} not in ${before}..${after}`);
assert.ok(counter >= 1 && counter < 16, `counter ${counter}`);
});
test("one conversation keeps one session, another gets its own", () => {
const a1 = sessionID(new Headers({ "x-session-id": "conversation-b" }), {});
const a2 = sessionID(new Headers({ "x-session-id": "conversation-b" }), {});
const b = sessionID(new Headers({ "x-session-id": "conversation-c" }), {});
assert.equal(a1, a2);
assert.notEqual(a1, b);
const body = { messages: [{ role: "user", content: "first question" }] };
assert.equal(sessionID(new Headers(), body), sessionID(new Headers(), body));
});
test("OpenCode's own session id is passed through, a hash cut to its shape is not", () => {
const real = "ses_ecc5d8f28ffe9Cy14V8Em7lI2u";
assert.equal(sessionID(new Headers({ "x-opencode-session": real }), {}), real);
// 0.1.4 sent this for "x-session-id: session_123"; Zen answers it 403.
const hashed = "ses_e9283da055b15JwOAra76bwUx4";
assert.equal(realSessionID(hashed), false);
const sent = sessionID(new Headers({ "x-opencode-session": hashed }), {});
assert.notEqual(sent, hashed);
assert.ok(realSessionID(sent), sent);
});
test("ids made in one millisecond count up, as OpenCode's do", () => {
const now = 1_790_000_000_000;
const first = opencodeID("msg", false, now);
const second = opencodeID("msg", false, now);
assert.equal(decode(first, false).counter, 1);
assert.equal(decode(second, false).counter, 2);
assert.ok(second > first);
});