inkstone 0.1.4: strict fake /keys, refused-key cleanup test, injectable probe backoff (#841)

The fake GET /api/tokenplan/v1/keys in keys.test.mjs's serveKeys() now
behaves like the live platform: no `uaa-token` cookie gives -10002, and
any query parameter gives -15100. A new test covers the third listKeys
call site, the 401 cleanup in ensureKey: the probe refuses the new key,
and the test checks that the refused key is deleted by name (#839).

probeWithRetry takes its delays as an optional third argument, and
ensureKey passes `probeDelays` through to the probe of a key it just
created. The default delays are unchanged, so nothing outside tests
behaves differently. The new test passes [0] and doesn't wait through
the ~15s backoff. Because lib/ changed, inkstone goes to 0.1.4.

Run at the reviewed head c4ab4717, merged onto main 7e01fa81, on mote
(Bun 1.4.2) with a temp HOME:
- packages/inkstone `bun test`: 102 pass / 0 fail
- `bun scripts/check.mjs inkstone`: ok
- repo-root `bun test` and the full `bun scripts/check.mjs`: CI run
  42476974057 at this head, green. The mote run waited 23 minutes
  behind 10 queued suites on the machine's suite lock and was dequeued
  without running.
Broken by hand, each red:
- query back at keyCount's listKeys: 3 fail (blank-prompt create,
  ten-key limit, the new cleanup test)
- query back at pruneKeys' listKeys: 2 fail (both pruning tests)
- query back at the 401 cleanup listKeys: 1 fail (the new test;
  main's tests stayed green there)
- listKeys on the bearer instead of the cookie: 7 fail
- probeDelays not passed through: 1 fail (the new test runs past
  bun's 5s limit at 15.0s)

#847 (also inkstone 0.1.4) merges cleanly on top, but it has to move to
0.1.5.
This commit is contained in:
Binyu Wang
2026-11-08 20:16:18 +08:00
committed by GitHub
parent 7e01fa8118
commit e7af7856e5
3 changed files with 44 additions and 6 deletions
+40 -1
View File
@@ -15,7 +15,15 @@ const now = (n) => new Date(Date.UTC(2026, 10, 1, 0, 0, n)).toISOString()
function serveKeys(initial = []) {
const keys = initial.map((k) => ({ ...k }))
f.route("GET /v1/models", () => json({ object: "list", data: [] }))
f.route("GET /api/tokenplan/v1/keys", () => ok({ total: keys.length, items: keys }))
f.route("GET /api/tokenplan/v1/keys", (r) => {
// the platform as it is now: /keys rejects a bearer-only request
// (-10002) and rejects any query param (-15100) - the plugin must
// send the uaa-token cookie and no pagination query
const cookie = r.headers.get("cookie") ?? ""
if (!cookie.startsWith("uaa-token=") || cookie === "uaa-token=") return fail(-10002, "request is not authenticated")
if ([...r.query.keys()].length > 0) return fail(-15100, "API Key fetch failed, refresh page and retry")
return ok({ total: keys.length, items: keys })
})
f.route("POST /api/tokenplan/v1/keys", (r) => {
const name = r.json?.name
keys.push({ id: `id-${keys.length + 1}`, name, status: "active", created_at: now(keys.length + 1) })
@@ -207,3 +215,34 @@ test("an idempotency clash retries with a FRESH name and returns the key string"
expect(posted[1]).not.toBe(posted[0])
expect(posted[0]).toBe(refusedName)
})
test("a refused new key is deleted by name - the cleanup listKeys carries no query", async () => {
f = fakeInkStone()
const keys = [{ id: "id-manual", name: "manual", status: "active", created_at: now(0) }]
// the probe always refuses the new key, so ensureKey falls into its
// 401-cleanup path, which lists the keys to find the one it just made
f.route("GET /v1/models", () => json({ error: "refused" }, 401))
f.route("GET /api/tokenplan/v1/keys", (r) => {
// the platform as it is now: /keys rejects a bearer-only request
// (-10002), and rejects any query param (-15100). With this fake,
// putting the pagination query back on the cleanup listKeys call makes
// the list fail and the refused key survive - this test goes red.
if ((r.headers.get("cookie") ?? "") !== "uaa-token=[REDACTED_VALUE]") return fail(-10002, "request is not authenticated")
if ([...r.query.keys()].length > 0) return fail(-15100, "API Key fetch failed, refresh page and retry")
return ok({ total: keys.length, items: keys })
})
f.route("POST /api/tokenplan/v1/keys", (r) => {
keys.push({ id: "id-new", name: r.json?.name, status: "active", created_at: now(keys.length + 1) })
return ok({ key: "sk-refused" })
})
f.route("DELETE /api/tokenplan/v1/keys/id-new", (r) => {
const i = keys.findIndex((k) => k.id === "id-new")
if (i >= 0) keys.splice(i, 1)
return ok({})
})
// probeDelays: [0] skips probeWithRetry's real backoff (0+1.5+3+4.5+6s)
// so the refusal is final immediately and this test stays fast
await expect(ensureKey("[REDACTED_VALUE]", "u", { create: true, probeDelays: [0] })).rejects.toMatchObject({ status: 401 })
expect(keys.some((k) => k.id === "id-new")).toBe(false)
expect(keys).toHaveLength(1)
})
+3 -4
View File
@@ -39,8 +39,7 @@ const at = (value) => {
}
/** 新 Key 生效前网关可能要几秒:给几次机会,别把"还没生效"当成"被拒绝"。 */
export async function probeWithRetry(apiKey, log) {
const delays = [0, 1500, 3000, 4500, 6000]
export async function probeWithRetry(apiKey, log, delays = [0, 1500, 3000, 4500, 6000]) {
let last = null
for (let i = 0; i < delays.length; i++) {
if (delays[i]) await new Promise((r) => setTimeout(r, delays[i]))
@@ -121,7 +120,7 @@ export async function pruneKeys(token, username, { freeSlots = 0, keep = 2, prot
* - 没粘且允许创建 → 数一下,满了先清理(可选),再创建并探活。
* 抛错时带 `code`:`key_limit`(满了且没开清理)、`refused`(Key 被平台拒)。
*/
export async function ensureKey(token, username, { apiKey = "", create = true, prune = false, log } = {}) {
export async function ensureKey(token, username, { apiKey = "", create = true, prune = false, log, probeDelays } = {}) {
const pasted = String(apiKey ?? "").trim()
if (pasted) {
await probeWithRetry(pasted, log)
@@ -203,7 +202,7 @@ export async function ensureKey(token, username, { apiKey = "", create = true, p
)
}
try {
await probeWithRetry(created, log)
await probeWithRetry(created, log, probeDelays)
} catch (e) {
if (e?.status === 401 || e?.status === 403) {
// 建出来却被拒:删掉这把,别给账号留垃圾。
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-inkstone-auth",
"version": "0.1.3",
"version": "0.1.4",
"description": "Intern InkStone (书生·端砚) TokenPlan in OpenCode and magpie: one console sign-in gives the ink-credit balance, the 5-hour and 7-day windows and the credit packs, and (with an API key) the plan's models for chat",
"type": "module",
"main": "./opencode.mjs",