From fc0c5a882ed9611da59be9e753445c717df79a75 Mon Sep 17 00:00:00 2001 From: aqiu817 Date: Sun, 15 Nov 2026 23:16:33 +0800 Subject: [PATCH] atomcode 0.3.1: send x-atomcode-session-id (#1185) AtomCode's glm5.3-flash group answers 403 "access disabled by request policy" when a request has no x-atomcode-session-id (#1186). The plugin now sends one: - chat.headers fills it with the session id of the conversation the agent is in; - the loader falls back to one random UUID per signed-in account when the hook didn't run or the session is empty. Version 0.3.1. Run at bbdbcc7a, with no atomcode changes on main since its base: - `bun test packages/atomcode` on mote: 164 pass, 0 fail. - `bun scripts/check.mjs atomcode`: ok. Broken by hand: with main's index.mjs and the PR's tests, run on mica, 3 tests fail (header and signature, per-account stability, chat.headers). The other 161 pass. Not tried: the live AtomCode gateway. The author's probes compare the same request with and without the header (200 against 403). Co-authored-by: aqiu817 <19250671+aqiu817@users.noreply.github.com> --- packages/atomcode/CHANGELOG.md | 18 ++++++ packages/atomcode/index.mjs | 45 +++++++++++++++ packages/atomcode/package.json | 2 +- packages/atomcode/test/plugin.test.mjs | 78 ++++++++++++++++++++++++++ 4 files changed, 142 insertions(+), 1 deletion(-) diff --git a/packages/atomcode/CHANGELOG.md b/packages/atomcode/CHANGELOG.md index 8fea49a..4518b05 100644 --- a/packages/atomcode/CHANGELOG.md +++ b/packages/atomcode/CHANGELOG.md @@ -3,6 +3,24 @@ All notable changes to this project are documented here. The format follows Keep a Changelog and the project uses semantic versioning. +## 0.3.1 - 2026-11-15 + +### Fixed + +- `glm5.3-flash` (and the other reasoning model groups the gateway treats the + same way, e.g. `lumen-alpha`) answered 403 "access disabled by request + policy" on every request: the upstream now requires an + `x-atomcode-session-id` header on that group, and no signed request from + this plugin carried one. +- The new `chat.headers` hook fills the header from magpie's own + per-conversation session id (`input.sessionID`), the same scope the official + client sends. On hosts or paths where that hook does not run (OpenCode entry + points), the loader falls back to one stable UUID per signed-in account. +- Verified against the live gateway: the identical signed body passes with the + header and fails without it. +- Ported from upstream `aqiu817/magpie-atomcode` (2026-11-15), where it was + diagnosed and verified with three controlled probes. + ## 0.3.0 - 2026-11-03 ### Added diff --git a/packages/atomcode/index.mjs b/packages/atomcode/index.mjs index 336cf6c..ffb2ed2 100644 --- a/packages/atomcode/index.mjs +++ b/packages/atomcode/index.mjs @@ -22,6 +22,8 @@ * @module magpie-atomcode */ +import { randomUUID } from "node:crypto"; + import { DEFAULT_CLIENT_VERSION, DEFAULT_CODING_PLAN_API_URL, @@ -202,6 +204,27 @@ export async function AtomCodePlugin(input = {}, options = {}) { */ const gatewayOrigin = originOf(opts.gatewayURL); + /** + * One stable conversation id per signed-in account, sent as + * `x-atomcode-session-id`. + * + * Since 2026-11 the upstream serves at least the glm5.3-flash model group + * only to requests carrying this header; the identical signed body otherwise + * fails with 403 "access disabled by request policy". The official client + * sends a stable UUID per conversation; this plugin has no conversation + * boundary, so one UUID per account is the closest stable analogue and keeps + * the gateway's prefix-cache affinity per account. + */ + const sessionIds = new Map(); + const sessionIdFor = (uid) => { + let id = sessionIds.get(uid); + if (!id) { + id = randomUUID(); + sessionIds.set(uid, id); + } + return id; + }; + const log = (level, message) => { try { client?.app?.log?.({ body: { service: PROVIDER, level, message } }); @@ -618,6 +641,21 @@ export async function AtomCodePlugin(input = {}, options = {}) { }; }, + /** + * Magpie's per-conversation session id → the gateway's session header. + * + * `chat.headers` runs on every gateway request, and `input.sessionID` + * stays constant across the turns of one conversation — the same scope + * the official client sends under `x-atomcode-session-id`. The loader + * falls back to the per-account UUID on hosts or paths where this hook + * did not run (OpenCode entry points, older hosts). + */ + "chat.headers": async (input, output) => { + if (input?.model?.providerID === PROVIDER && input?.sessionID) { + output.headers["x-atomcode-session-id"] = String(input.sessionID); + } + }, + provider: { id: PROVIDER, /** @@ -980,6 +1018,13 @@ export async function AtomCodePlugin(input = {}, options = {}) { } requestHeaders.set("authorization", `Bearer ${current.access}`); requestHeaders.set("user-agent", userAgentOf(clientVersion)); + // glm5.3-flash and the other reasoning groups answer 403 + // "access disabled by request policy" without this header. The + // chat.headers hook fills it with magpie's per-conversation id; + // this per-account UUID only fills in when that did not run. + if (!requestHeaders.has("x-atomcode-session-id")) { + requestHeaders.set("x-atomcode-session-id", sessionIdFor(current.uid)); + } const forwarded = { ...(isRequest diff --git a/packages/atomcode/package.json b/packages/atomcode/package.json index 5ddd6e3..21957ec 100644 --- a/packages/atomcode/package.json +++ b/packages/atomcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-atomcode-auth", - "version": "0.3.0", + "version": "0.3.1", "description": "AtomCode (AtomGit CodingPlan) for Magpie and OpenCode: browser OAuth or local CLI sign-in, signed requests, persistent diagnostics, native plan claim, live models and quota windows", "type": "module", "main": "./opencode.mjs", diff --git a/packages/atomcode/test/plugin.test.mjs b/packages/atomcode/test/plugin.test.mjs index 115e786..fac7224 100644 --- a/packages/atomcode/test/plugin.test.mjs +++ b/packages/atomcode/test/plugin.test.mjs @@ -491,12 +491,90 @@ test("loader:发往网关的请求照常带凭证与签名", async () => { assert.equal(call.headers.get("authorization"), "Bearer tok"); assert.match(call.headers.get("x-atomcode-sig") ?? "", /^v1:[0-9a-f]{64}$/); assert.equal(call.headers.get("user-agent"), "atomcode/5.2.3"); + assert.match( + call.headers.get("x-atomcode-session-id") ?? "", + /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/, + "glm5.3-flash 等模型组的上游准入要求 session-id(无它 → 403 policy disabled)", + ); assert.equal(call.redirect, "error"); } finally { stub.restore(); } }); +test("loader:session-id 每账号稳定、账号间隔离", async () => { + const stub = stubFetch({ statusBody: VALID_STATUS }); + try { + const plugin = await AtomCodePlugin({}, { autoVersion: false }); + const { fetch: patched } = await plugin.auth.loader(async () => AUTH); + await patched(`${GATEWAY}/chat/completions`, { method: "POST", body: "{}" }); + await patched(`${GATEWAY}/chat/completions`, { method: "POST", body: "{}" }); + const ids = stub.calls + .filter((c) => c.url.includes("chat/completions")) + .map((c) => c.headers.get("x-atomcode-session-id")); + assert.equal(ids.length, 2); + assert.ok(ids[0] && ids[0] === ids[1], "同一账号的连续请求共享一个稳定会话 id"); + + const { fetch: patched2 } = await plugin.auth.loader(async () => ({ ...AUTH, uid: "u2" })); + await patched2(`${GATEWAY}/chat/completions`, { method: "POST", body: "{}" }); + const other = stub.calls + .filter((c) => c.url.includes("chat/completions")) + .map((c) => c.headers.get("x-atomcode-session-id"))[2]; + assert.ok(other && other !== ids[0], "不同账号的会话 id 互不相同"); + } finally { + stub.restore(); + } +}); + +test("chat.headers:magpie 会话 id 原样上头,且只对本供应商", async () => { + const stub = stubFetch({ statusBody: VALID_STATUS }); + try { + const plugin = await AtomCodePlugin({}, { autoVersion: false }); + assert.equal(typeof plugin["chat.headers"], "function", "chat.headers 钩子要导出"); + + const output = { headers: {} }; + await plugin["chat.headers"]( + { model: { providerID: "atomcode" }, sessionID: "sess-abc-123" }, + output, + ); + assert.equal( + output.headers["x-atomcode-session-id"], + "sess-abc-123", + "同一对话各轮共享 magpie 的会话 id(官方客户端语义)", + ); + + const other = { headers: {} }; + await plugin["chat.headers"]( + { model: { providerID: "acme" }, sessionID: "sess-abc-123" }, + other, + ); + assert.equal(other.headers["x-atomcode-session-id"], undefined, "别的供应商不加这个头"); + } finally { + stub.restore(); + } +}); + +test("loader:chat.headers 已带会话头时不覆盖(per-conversation 优先)", async () => { + const stub = stubFetch({ statusBody: VALID_STATUS }); + try { + const plugin = await AtomCodePlugin({}, { autoVersion: false }); + const { fetch: patched } = await plugin.auth.loader(async () => AUTH); + await patched(`${GATEWAY}/chat/completions`, { + method: "POST", + body: "{}", + headers: { "x-atomcode-session-id": "sess-from-magpie" }, + }); + const call = stub.calls.find((c) => c.url.includes("chat/completions")); + assert.equal( + call.headers.get("x-atomcode-session-id"), + "sess-from-magpie", + "magpie 会话 id 优先于每账号兜底", + ); + } finally { + stub.restore(); + } +}); + test("loader:校验后冻结目标字符串,URL 在取凭证期间被改也不能换目的地", async () => { const stub = stubFetch({ statusBody: VALID_STATUS }); try {