@magpie-community/opencode-kiro-auth 0.1.44 → 0.1.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +7 -0
  2. package/index.mjs +54 -6
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -59,6 +59,13 @@ them, are under "Other ways to sign in…".
59
59
  browser sign-in here. Sign in with `kiro-cli login` or the Kiro IDE and
60
60
  use the next method instead; its management calls carry
61
61
  `TokenType: EXTERNAL_IDP`.
62
+ - magpie renews the sign-in ahead of its end. A renewal that fails (Kiro
63
+ unreachable, a 5xx) isn't tried again on every pass: the next waits a
64
+ minute, then twice as long each time, up to an hour, and the failure
65
+ reaches magpie's log once. A refresh token Kiro refused (400, 401, 403)
66
+ marks the account for a new sign-in and isn't tried again until it has
67
+ one. A renewal that works, or a request's own refresh, starts the count
68
+ again.
62
69
  - **Kiro CLI's or Kiro IDE's sign-in**: uses the account kiro-cli is signed
63
70
  in to, or else the Kiro IDE's. Nothing is copied. It is read each time
64
71
  from where they keep it:
package/index.mjs CHANGED
@@ -288,11 +288,16 @@ const EXPIRED_CLI = EXPIRED + " with `kiro-cli login` or the Kiro IDE"
288
288
 
289
289
  // gone is an error saying the sign-in itself is gone — refused, or never
290
290
  // there. The built-in marked no Kiro account lapsed, not even then, nor
291
- // cleared one: every answer says the account is kept. The usage read says
292
- // expired for it, and renewed for a clean read (auth.usage).
291
+ // cleared one: every answer says the account is kept. Here it is marked
292
+ // signIn "expired" by magpie's renewal (auth.refresh, once: plugins#1716)
293
+ // and by the usage read, which says renewed for a clean read (auth.usage).
293
294
  const gone = (msg) => Object.assign(new Error(msg), { gone: true })
294
295
  const EARLY_MS = 2 * 60 * 1000 // a token this close to its end is refreshed before a request
295
296
  const LEAD_MS = 10 * 60 * 1000 // and this close, magpie renews it ahead of time (auth.refresh)
297
+ // a renewal auth.refresh asked for that failed waits base before the next,
298
+ // doubling with each failure in a row up to cap (plugins#1716); now is the
299
+ // clock it reads, held in tests
300
+ const backoff = { base: 60 * 1000, cap: 60 * 60 * 1000, now: () => Date.now() }
296
301
  const fresh = (c, early = EARLY_MS) => c.method === "apikey" || !c.expires || c.expires - Date.now() > early
297
302
 
298
303
  async function post(url, contentType, body, headers = {}) {
@@ -763,11 +768,38 @@ function account(client, magpie) {
763
768
  for (let i = 0; i < 16 && after.has(c.access); i++) c = { ...after.get(c.access) }
764
769
  return c
765
770
  }
771
+ // failing is auth.refresh's renewals that failed, by the refresh token
772
+ // they were tried with (plugins#1716): one isn't tried again till its
773
+ // next, and one Kiro refused (gone) never, till the account holds another
774
+ // refresh token (signed in again). What failed reaches magpie once for
775
+ // each state (failing, ended), not on every pass.
776
+ const failing = new Map() // refresh token → { n, next, final, e, told }
766
777
  const renewed = (c, n) => {
767
778
  after.set(c.access, n)
768
779
  if (after.size > 16) after.delete(after.keys().next().value)
780
+ // renewed, by a request's refresh or magpie's: the count starts again
781
+ failing.delete(c.refresh)
769
782
  return n
770
783
  }
784
+ const failed = (k, e) => {
785
+ const t = failing.get(k) ?? { n: 0, told: "" }
786
+ t.n++
787
+ t.e = e
788
+ t.final = !!e?.gone
789
+ t.next = backoff.now() + Math.min(backoff.base * 2 ** (t.n - 1), backoff.cap)
790
+ failing.set(k, t)
791
+ if (failing.size > 16) failing.delete(failing.keys().next().value)
792
+ return t
793
+ }
794
+ // told throws a state's failure the first time, and is nothing to renew
795
+ // after that. A refusal is marked signIn "expired", so magpie shows the
796
+ // account as one to sign in again and stops asking.
797
+ const told = (t) => {
798
+ const state = t.final ? "ended" : "failing"
799
+ if (t.told === state) return undefined
800
+ t.told = state
801
+ throw t.final ? Object.assign(new Error(t.e.message), { gone: true, signIn: "expired" }) : t.e
802
+ }
771
803
  let lock = Promise.resolve()
772
804
  const locked = (fn) => {
773
805
  const run = lock.then(fn, fn)
@@ -902,7 +934,21 @@ function account(client, magpie) {
902
934
  let c = newest(read)
903
935
  // renewed here already (a request's refresh, or this hook's whose
904
936
  // result magpie hasn't saved yet), the store not yet saying so
905
- if (c.access === read.access || !fresh(c, LEAD_MS)) c = renewed(c, await refresh(c))
937
+ if (c.access === read.access || !fresh(c, LEAD_MS)) {
938
+ const k = c.refresh
939
+ let t = failing.get(k)
940
+ // failed lately: not tried again till its time, nor ever once Kiro
941
+ // refused it
942
+ if (!t || (!t.final && backoff.now() >= t.next)) {
943
+ try {
944
+ c = renewed(c, await refresh(c))
945
+ t = null
946
+ } catch (e) {
947
+ t = failed(k, e)
948
+ }
949
+ }
950
+ if (t) return told(t)
951
+ }
906
952
  held = { id: "oauth:own", c }
907
953
  const out = { access: c.access, expires: c.expires }
908
954
  if (c.refresh !== auth.refresh) out.refresh = c.refresh
@@ -3441,8 +3487,10 @@ export async function KiroAuthPlugin({ client, magpie } = {}) {
3441
3487
  // this. kiro-cli's and the IDE's sign-ins have no end magpie knows
3442
3488
  // (expires 0) and are never asked for.
3443
3489
  refreshLead: LEAD_MS,
3444
- // A failure is thrown as it is, a refusal too: the built-in marked
3445
- // no Kiro account lapsed (see gone).
3490
+ // A failure is thrown once and then backs off, from a minute doubling
3491
+ // to an hour; a refusal (gone) is thrown once marked signIn
3492
+ // "expired", as auth.usage marks it, and isn't tried again until the
3493
+ // account is signed in again (plugins#1716).
3446
3494
  async refresh(auth) {
3447
3495
  if (auth?.type !== "oauth" || auth.source || !auth.access || !auth.refresh) return undefined
3448
3496
  return creds.renew(auth)
@@ -3573,4 +3621,4 @@ export async function KiroAuthPlugin({ client, magpie } = {}) {
3573
3621
  }
3574
3622
 
3575
3623
  // for tests
3576
- export const _internal = { generate, responsesBody, crMessages, crRefusal, features, FEATURES, featureKey, waitOf, timing, TRIES, refresh, usageOf, buildKiro, kiroBody, IMAGE_LIMIT, events, reply, failure, toolID, toolName, toolNames, thinking, nativeFields, listModels, rateOf, contextFirst, configModel, runtimeModel, readCLI, readIDE, regionOf, planName, frames, sealReasoning, unsealReasoning, reasoningScope }
3624
+ export const _internal = { backoff, generate, responsesBody, crMessages, crRefusal, features, FEATURES, featureKey, waitOf, timing, TRIES, refresh, usageOf, buildKiro, kiroBody, IMAGE_LIMIT, events, reply, failure, toolID, toolName, toolNames, thinking, nativeFields, listModels, rateOf, contextFirst, configModel, runtimeModel, readCLI, readIDE, regionOf, planName, frames, sealReasoning, unsealReasoning, reasoningScope }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-kiro-auth",
3
- "version": "0.1.44",
3
+ "version": "0.1.45",
4
4
  "description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie",
5
5
  "type": "module",
6
6
  "main": "./opencode.mjs",