From 016a86906202c57a215a3aa7146b6242ab03eaad Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 4 Nov 2026 04:09:49 +0800 Subject: [PATCH] orcaterm 0.1.5: a refused renewal doesn't sign out a token that still has life (#675) orinsid's idle run (0.1.4, magpie v0.1.2861): the token was issued with a 2h life, the host's first background renewal at +30min got SESSION_EXPIRED from OAuthRefreshToken, and auth.refresh threw signIn "expired" on any message matching /expired/, so the account was marked signed out with 90 minutes of token left. That token drives the chat by itself until its own exp, and magpie's contract is that a failed renewal not marked "expired" is kept and tried again. Now a renewal refused as expired fails without `signIn` while the token's exp (JWT claim or the stored expires) is more than a minute away; once the token has expired too, it still marks the account. Not changed, and not known: why upstream answers SESSION_EXPIRED 30 minutes into a 2h token. The renewal sends only {accessToken} with no cookie, while OrcaTerm's product calls send ot_session (and the server sets an ot_sid session cookie); an idle server-side session timeout is the likely cause. Neither can be checked without a real account, so this only stops the false sign-out; the session may still lapse while idle. Run: bun test in packages/orcaterm (115 pass); the new "still has life" test is red on 0.1.4's index.mjs and green here. Not tried: a real OrcaTerm account, a real idle run. Reported-by: orinsid --- packages/orcaterm/index.mjs | 10 ++++++++ packages/orcaterm/package.json | 2 +- packages/orcaterm/renew.test.mjs | 39 ++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/packages/orcaterm/index.mjs b/packages/orcaterm/index.mjs index 1f437ac..285a591 100644 --- a/packages/orcaterm/index.mjs +++ b/packages/orcaterm/index.mjs @@ -2052,6 +2052,16 @@ export const OrcaTermAuthPlugin = async ({ client } = {}) => { } } catch (err) { if (/expired|invalid|失效|过期/i.test(String(err?.message ?? ""))) { + // A refused renewal is not a signed-out account while the token + // it would replace still works: the access token drives the chat + // by itself until its own exp (#675: SESSION_EXPIRED came 30 + // minutes into a two-hour token). Fail without `signIn`, so + // magpie keeps the sign-in and tries again; a request or a 401 + // after the real expiry still marks it. + const exp = Math.max(readTokenClaims(bearer).exp * 1000, Number(auth?.expires) || 0) + if (exp > Date.now() + 60e3) { + throw new Error(`OrcaTerm renewal refused, the token still has life: ${err?.message ?? err}`) + } throw Object.assign(new Error(`OrcaTerm sign-in expired, scan again: ${err?.message ?? err}`), { signIn: "expired", }) diff --git a/packages/orcaterm/package.json b/packages/orcaterm/package.json index 02fbb9d..369c29c 100644 --- a/packages/orcaterm/package.json +++ b/packages/orcaterm/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-orcaterm-auth", - "version": "0.1.4", + "version": "0.1.5", "description": "OrcaTerm (Tencent Cloud 遨驰终端) AI quota as an OpenAI-compatible provider for magpie and OpenCode", "type": "module", "main": "./opencode.mjs", diff --git a/packages/orcaterm/renew.test.mjs b/packages/orcaterm/renew.test.mjs index 873ebad..2d92f8e 100644 --- a/packages/orcaterm/renew.test.mjs +++ b/packages/orcaterm/renew.test.mjs @@ -44,3 +44,42 @@ test("the lead is one magpie takes as said, not its cap", async () => { expect(Number.isFinite(a.refreshLead)).toBe(true) expect(leadOf(a)).toBe(a.refreshLead) }) + +// #675 (orinsid, 2026-11-04): the first background renewal, 30 minutes into +// a two-hour token, was answered SESSION_EXPIRED and the account was marked +// "sign-in expired" at once. The token itself was good for 90 more minutes +// and drives /assistant/chat on its own, so a refused renewal is only +// terminal once the token it would replace has expired too. +const jwt = (expSec) => + ["eyJhbGciOiJIUzI1NiJ9", Buffer.from(JSON.stringify({ userId: "u1", exp: expSec })).toString("base64url"), "sig-sig-sig"].join(".") + +async function renewWith(expMs) { + const a = await auth() + const real = globalThis.fetch + globalThis.fetch = async () => + new Response(JSON.stringify({ data: { Response: { Error: { Code: "SESSION_EXPIRED", Message: "SESSION_EXPIRED" } } } }), { + status: 200, + headers: { "content-type": "application/json" }, + }) + try { + const token = jwt(Math.floor(expMs / 1000)) + return await a.refresh({ type: "oauth", access: token, refresh: token, expires: expMs }).then( + () => null, + (e) => e, + ) + } finally { + globalThis.fetch = real + } +} + +test("SESSION_EXPIRED on a renewal of a token that still has life does not sign the account out", async () => { + const e = await renewWith(Date.now() + 90 * 60e3) + expect(e).toBeInstanceOf(Error) + expect(e.signIn).toBeUndefined() + expect(String(e.message)).toMatch(/SESSION_EXPIRED/) +}) + +test("SESSION_EXPIRED on a renewal of a token that has expired signs the account out", async () => { + const e = await renewWith(Date.now() - 60e3) + expect(e?.signIn).toBe("expired") +})