diff --git a/packages/tabbit/package.json b/packages/tabbit/package.json
index 77f4fd6..74d383a 100644
--- a/packages/tabbit/package.json
+++ b/packages/tabbit/package.json
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-tabbit-auth",
- "version": "0.1.4",
+ "version": "0.1.5",
"description": "Tabbit (web.tabbit.com and web.tabbit.ai) as a magpie / OpenCode provider: live model catalog, cookie sign-in, chat completions, cycle usage and the daily check-in",
"type": "module",
"main": "./opencode.mjs",
diff --git a/packages/tabbit/src/api.mjs b/packages/tabbit/src/api.mjs
index 590473d..247e993 100644
--- a/packages/tabbit/src/api.mjs
+++ b/packages/tabbit/src/api.mjs
@@ -197,7 +197,12 @@ async function refusalOf(res) {
// still signed in, the model just isn't on its plan. Measured with
// Kimi-K3.1-Pro on a free account.
const paywalled = res.status === 403 && code === 492
- return { res, body, parsed, detail, code, action, paywalled }
+ // A 403 whose body isn't JSON is a page from something in front of
+ // Tabbit's API (an HTML "Service Unavailable", with no server header,
+ // #1199), not Tabbit refusing the token: the same token answered on every
+ // route half an hour later. It says nothing about the sign-in.
+ const page = res.status === 403 && parsed === null && body.trim() !== ""
+ return { res, body, parsed, detail, code, action, paywalled, page }
}
const refused = (res) => res.status === 401 || res.status === 403
@@ -235,7 +240,7 @@ async function call(creds, path, init = {}, signal, { totalTimeout = true, settl
// way — accepted there, or refused by both (an expired token, which then
// stays on BASE and is reported as before). A failure to reach the other
// site settles nothing, and the first refusal stands.
- if (!known && refusal && !refusal.paywalled) {
+ if (!known && refusal && !refusal.paywalled && !refusal.page) {
let settled = true
for (const other of SITES) {
if (other === site) continue
@@ -253,6 +258,11 @@ async function call(creds, path, init = {}, signal, { totalTimeout = true, settl
refusal = null
break
}
+ // an error page there says nothing of the token either (#1199)
+ if (again.status === 403 && (await refusalOf(again)).page) {
+ settled = false
+ continue
+ }
try {
await again.body?.cancel()
} catch {
@@ -265,6 +275,16 @@ async function call(creds, path, init = {}, signal, { totalTimeout = true, settl
learn(creds.token, site)
}
+ if (refusal?.page) {
+ const title = /
]*>([^<]{1,120})<\/title>/i.exec(refusal.body)?.[1]?.trim()
+ const error = new TabbitError(
+ `Tabbit's service answered HTTP ${res.status} with an error page${title ? ` (${title})` : `: ${refusal.body.slice(0, 200)}`}; the sign-in was not refused`,
+ { status: res.status },
+ )
+ const retryAfter = res.headers.get("retry-after")
+ if (retryAfter) error.retryAfter = retryAfter
+ throw error
+ }
if (refusal) {
const { body, parsed, detail, code, action, paywalled } = refusal
const error = new TabbitError(
diff --git a/packages/tabbit/tests/checkin.test.mjs b/packages/tabbit/tests/checkin.test.mjs
index 967ec94..be1867f 100644
--- a/packages/tabbit/tests/checkin.test.mjs
+++ b/packages/tabbit/tests/checkin.test.mjs
@@ -107,6 +107,23 @@ test("a refused sign-in throws with magpie's expired mark", async () => {
}
})
+test("an HTML error page's 403 fails the check-in without the expired mark (#1199)", async () => {
+ const original = globalThis.fetch
+ globalThis.fetch = async () =>
+ new Response('\n\nService Unavailable', {
+ status: 403,
+ headers: { "content-type": "text/html" },
+ })
+ try {
+ await assert.rejects(
+ () => checkinOf(creds),
+ (error) => error.signIn === undefined && /Service Unavailable/.test(error.message) && !/refused the sign-in/.test(error.message),
+ )
+ } finally {
+ globalThis.fetch = original
+ }
+})
+
test("the status call asks for the scene the service takes", async () => {
const { calls, restore } = stub({ status: open({ signed_today: true }) })
try {
diff --git a/packages/tabbit/tests/plugin.test.mjs b/packages/tabbit/tests/plugin.test.mjs
index dc3e958..a44d12b 100644
--- a/packages/tabbit/tests/plugin.test.mjs
+++ b/packages/tabbit/tests/plugin.test.mjs
@@ -219,6 +219,51 @@ test("a premium-only model's 403 does not mark the sign-in", async () => {
}
})
+// #1199: the reporter's page, from something in front of Tabbit's API
+const EDGE_PAGE = '\n\n\n\nService Unavailable\n\nService Unavailable
\n\n'
+
+test("a 403 that is an HTML error page does not mark the sign-in (#1199)", async () => {
+ const original = globalThis.fetch
+ const hosts = new Set()
+ globalThis.fetch = async (input) => {
+ hosts.add(new URL(typeof input === "string" ? input : input.url).host)
+ return new Response(EDGE_PAGE, { status: 403, headers: { "content-type": "text/html" } })
+ }
+ try {
+ const hooks = await TabbitPlugin()
+ const loader = await hooks.auth.loader(async () => auth)
+ const res = await loader.fetch(`${loader.baseURL}/chat/completions`, {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({ model: "Default", messages: [{ role: "user", content: "hi" }] }),
+ })
+ assert.equal(res.status, 403)
+ assert.equal(res.headers.get("x-magpie-sign-in"), null)
+ const text = await res.text()
+ assert.match(text, /Service Unavailable/)
+ assert.doesNotMatch(text, /refused the sign-in/)
+ } finally {
+ globalThis.fetch = original
+ }
+})
+
+test("a 403 with Tabbit's own JSON still marks the sign-in", async () => {
+ const original = globalThis.fetch
+ globalThis.fetch = async () => Response.json({ detail: "invalid bearer token" }, { status: 403 })
+ try {
+ const hooks = await TabbitPlugin()
+ const loader = await hooks.auth.loader(async () => auth)
+ const res = await loader.fetch(`${loader.baseURL}/chat/completions`, {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({ model: "Default", messages: [{ role: "user", content: "hi" }] }),
+ })
+ assert.equal(res.headers.get("x-magpie-sign-in"), "expired")
+ } finally {
+ globalThis.fetch = original
+ }
+})
+
test("credsOf accepts a bare JWT and a full cookie string", () => {
const bare = credsOf({ key: "aaa.bbb.ccc", metadata: { userId: "u9" } })
assert.match(bare.header, /token=aaa\.bbb\.ccc/)