viewable-one 0.12.8 → 0.12.9
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.
- package/CHANGELOG.md +85 -0
- package/README.md +8 -3
- package/SPAWN.md +24 -4
- package/glytch.mjs +5 -4
- package/lib/agents.mjs +195 -33
- package/lib/auth.mjs +245 -276
- package/lib/authenticatedHttp.mjs +2 -2
- package/lib/boardside.mjs +3 -1
- package/lib/child.mjs +33 -2
- package/lib/codex.mjs +85 -11
- package/lib/codexApps.mjs +84 -24
- package/lib/computerApps.mjs +85 -10
- package/lib/conversationRules.mjs +19 -3
- package/lib/detect.mjs +20 -5
- package/lib/doctor.mjs +18 -2
- package/lib/folderReads.mjs +15 -4
- package/lib/init-connect.mjs +11 -3
- package/lib/lateResume.mjs +4 -1
- package/lib/mcpjson.mjs +99 -1
- package/lib/multiwatch.mjs +12 -4
- package/lib/otherLoginHook.mjs +31 -0
- package/lib/otherLogins.mjs +107 -3
- package/lib/permissionAsk.mjs +30 -5
- package/lib/project-mcp.mjs +7 -27
- package/lib/registry.mjs +46 -2
- package/lib/runnerBeat.mjs +15 -0
- package/lib/runnerCompletion.mjs +28 -22
- package/lib/runnerExecute.mjs +70 -11
- package/lib/runnerGates.mjs +30 -5
- package/lib/runnerLaunch.mjs +30 -3
- package/lib/runnerOps.mjs +2 -2
- package/lib/runnerRegister.mjs +5 -4
- package/lib/runnerWorkspace.mjs +39 -12
- package/lib/selfUninstall.mjs +6 -8
- package/lib/spawn.mjs +9 -0
- package/lib/sync.mjs +10 -4
- package/lib/update.mjs +12 -6
- package/lib/worktree.mjs +75 -31
- package/lib/worktreeSweep.mjs +122 -0
- package/lib/writePolicy.mjs +112 -2
- package/package.json +1 -1
package/lib/auth.mjs
CHANGED
|
@@ -40,6 +40,18 @@
|
|
|
40
40
|
// and the stored personal token is what a later process - unsigned in its own
|
|
41
41
|
// environment - falls back to before ever trying the OAuth refresh path.
|
|
42
42
|
//
|
|
43
|
+
// ONE SIGN-IN PER RECORD (#8324, 2026-11-24). A record is EITHER a personal
|
|
44
|
+
// token OR an OAuth pair, never both. Every `login()`, of either kind,
|
|
45
|
+
// replaces the whole record. Both used to keep the other kind's credential
|
|
46
|
+
// beside the new one, with nothing to say they were the same account: the
|
|
47
|
+
// token response carries no user id and `pmb_` tokens are opaque. So a
|
|
48
|
+
// browser sign-in as B over A's personal token kept sending A's token, and a
|
|
49
|
+
// personal token for B over A's browser sign-in fell back to A's refresh
|
|
50
|
+
// token once B's was refused. A record still holding both (written by an
|
|
51
|
+
// older bridge) is not used at all: it asks for `viewable login` again,
|
|
52
|
+
// which writes a clean one. A same-account merge can return only once the
|
|
53
|
+
// sign-in can prove whose it is.
|
|
54
|
+
//
|
|
43
55
|
// The runner requests the explicit `board runner` grant. Its registered name is
|
|
44
56
|
// display-only; the consented scope selects its longer access-token lifetime.
|
|
45
57
|
|
|
@@ -135,6 +147,40 @@ async function writeAuthFile(data, env = process.env) {
|
|
|
135
147
|
await writeFileAtomic(authFilePath(env), `${JSON.stringify(data, null, 2)}\n`, "utf8", { mode: 0o600 });
|
|
136
148
|
}
|
|
137
149
|
|
|
150
|
+
const SIGN_IN_GAVE_UP = "The sign-in finished after this login had already given up. Run `viewable login` again.";
|
|
151
|
+
|
|
152
|
+
/** Save a new sign-in (#8324): the whole record is replaced, never merged,
|
|
153
|
+
* and the old credential's marks go with it. Written under the refresh lock
|
|
154
|
+
* so a renewal of the previous sign-in that is still in flight cannot write
|
|
155
|
+
* its rotated pair back over this one (it re-reads under the same lock and
|
|
156
|
+
* drops its result once the refresh token it spent is gone).
|
|
157
|
+
*
|
|
158
|
+
* Waits past LOCK_STALE_MS, so a holder that died is always cleared and a
|
|
159
|
+
* live one (bounded by EXCHANGE_TIMEOUT_MS) has finished. Only a filesystem
|
|
160
|
+
* that refuses the lock directory leaves it untaken, and such a filesystem
|
|
161
|
+
* refuses every refresher the same lock too - none of them refreshes
|
|
162
|
+
* without it - so writing anyway cannot race one. A person's sign-in is not
|
|
163
|
+
* refused for it.
|
|
164
|
+
*
|
|
165
|
+
* `stillWanted` is login's own "has this flow already given up" (F2),
|
|
166
|
+
* asked before and after the wait: nothing lands on disk after the CLI
|
|
167
|
+
* was told the sign-in failed.
|
|
168
|
+
*
|
|
169
|
+
* `keepPersonalMark` keeps the personal token's mark, which is fingerprinted
|
|
170
|
+
* by that token: saving the same refused token again must not un-refuse it,
|
|
171
|
+
* and a different token voids the mark on its next read anyway. */
|
|
172
|
+
async function saveSignIn(record, env, now, { keepPersonalMark = false, stillWanted = () => true } = {}) {
|
|
173
|
+
if (!stillWanted()) throw new Error(SIGN_IN_GAVE_UP);
|
|
174
|
+
const lock = await takeRefreshLock(env, now, LOCK_STALE_MS + LOCK_WAIT_MS);
|
|
175
|
+
try {
|
|
176
|
+
if (!stillWanted()) throw new Error(SIGN_IN_GAVE_UP);
|
|
177
|
+
await writeAuthFile(record, env);
|
|
178
|
+
await clearDeadMark(env, keepPersonalMark ? "oauth" : undefined);
|
|
179
|
+
} finally {
|
|
180
|
+
await dropRefreshLock(lock);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
138
184
|
/** Sign out: delete the stored credential. Returns whether there was one.
|
|
139
185
|
* Does not revoke the token at the endpoint - that is `oauth/revoke`, and a
|
|
140
186
|
* laptop with no network right now should still be able to forget its own
|
|
@@ -214,9 +260,9 @@ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
214
260
|
* and has not finished within LOCK_WAIT_MS, or the filesystem refuses it.
|
|
215
261
|
* Never throws: without the cross-process lock the caller does not refresh;
|
|
216
262
|
* it re-reads the credential and mark, returning a usable cached token or null. */
|
|
217
|
-
async function takeRefreshLock(env, now) {
|
|
263
|
+
async function takeRefreshLock(env, now, waitMs = LOCK_WAIT_MS) {
|
|
218
264
|
const path = `${authFilePath(env)}.refresh.lock`;
|
|
219
|
-
const deadline = now() +
|
|
265
|
+
const deadline = now() + waitMs;
|
|
220
266
|
for (;;) {
|
|
221
267
|
try {
|
|
222
268
|
await mkdir(path);
|
|
@@ -311,6 +357,12 @@ const TERMINAL_REFRESH_ERRORS = new Set([
|
|
|
311
357
|
export const REAUTH_REQUIRED_HINT =
|
|
312
358
|
"Your Viewable sign-in has expired and cannot be renewed: run `viewable login` to sign in again.";
|
|
313
359
|
|
|
360
|
+
/** The line for a renewal that went out and got no clear answer (a timeout, a
|
|
361
|
+
* dropped connection, an unreadable reply). The server never refused it, so
|
|
362
|
+
* "expired and cannot be renewed" would be wrong (#8221): the runner tries
|
|
363
|
+
* again by itself, on the backoff below, until it gets a definite answer. */
|
|
364
|
+
export const RENEWAL_NO_ANSWER_HINT = "The last renewal got no answer. Trying again.";
|
|
365
|
+
|
|
314
366
|
/** The personal-token sibling of REAUTH_REQUIRED_HINT (2026-09-19, brief 43
|
|
315
367
|
* review, B2). A `pmb_` token is not refreshed - it is revoked from
|
|
316
368
|
* Settings - so the fix is not `viewable login`, it is a fresh token copied
|
|
@@ -321,7 +373,7 @@ export const PERSONAL_TOKEN_REJECTED_HINT =
|
|
|
321
373
|
|
|
322
374
|
/** The OAuth-pair mark: keyed by the refresh token, same path and shape this
|
|
323
375
|
* file has always used. Every plain (non-`personal`) record's mark lives
|
|
324
|
-
* here
|
|
376
|
+
* here. */
|
|
325
377
|
const deadMarkPath = (env = process.env) => `${authFilePath(env)}.dead`;
|
|
326
378
|
|
|
327
379
|
/** The personal-token mark, keyed by the `pmb_` access token itself
|
|
@@ -336,31 +388,27 @@ const markPathFor = (kind, env) => (kind === "personal" ? personalDeadMarkPath(e
|
|
|
336
388
|
const credentialFingerprint = (token) =>
|
|
337
389
|
createHash("sha256").update(String(token)).digest("hex");
|
|
338
390
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
*
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
* the `pmb_` personal token, and the OAuth pair behind its refresh token.
|
|
357
|
-
* Fingerprinting the mark against whichever one the caller means, rather
|
|
358
|
-
* than always preferring the refresh token, is what the rest of this file
|
|
359
|
-
* is for.
|
|
391
|
+
const filled = (value) => typeof value === "string" && value !== "";
|
|
392
|
+
|
|
393
|
+
/** A record holding a personal token AND an OAuth bearer (#8324). Only an
|
|
394
|
+
* older bridge wrote these: its `login()` kept the other kind's credential
|
|
395
|
+
* beside the new one, with no proof the two were the same account. Nothing
|
|
396
|
+
* can tell now which one is the person's current sign-in, so such a record
|
|
397
|
+
* is never used: `hostedToken` returns null and `reauthRequired` asks for
|
|
398
|
+
* `viewable login`, which writes a clean record. */
|
|
399
|
+
export const isMixedRecord = (stored) =>
|
|
400
|
+
stored?.personal === true && (filled(stored.refreshToken) || filled(stored.oauthAccessToken));
|
|
401
|
+
|
|
402
|
+
/** TWO MARKS (2026-09-19). The personal token and the OAuth pair are two
|
|
403
|
+
* kinds of bearer an endpoint rejects for different reasons, so each kind
|
|
404
|
+
* keeps its mark in its own file, fingerprinted against its own credential.
|
|
405
|
+
* Since #8324 one record holds only one kind; the two files stay so that
|
|
406
|
+
* every existing reader of `auth.json.dead` keeps seeing exactly the
|
|
407
|
+
* OAuth-pair mark it always did.
|
|
360
408
|
*
|
|
361
|
-
* THE BUG THIS
|
|
409
|
+
* THE BUG THIS REPLACED. A single mark, fingerprinted by
|
|
362
410
|
* `fingerprintSubject` (refresh token whenever one was on file), meant that
|
|
363
|
-
* on a
|
|
411
|
+
* on a record holding both kinds a REJECTED PERSONAL TOKEN wrote a mark keyed by the
|
|
364
412
|
* REFRESH TOKEN. `hostedToken`'s `personal: true` branch read that same
|
|
365
413
|
* mark, saw it live, and fell through to `oauthAccessToken(...,
|
|
366
414
|
* forceRefresh: true)` on every single call - not just the first one after
|
|
@@ -446,14 +494,29 @@ async function markCredentialDead(stored, env, now, reason, prior, notify, kind
|
|
|
446
494
|
const keepRefreshWait = reason === "rejected" && prior?.reason === "refresh_transient" && now() < Number(prior.until);
|
|
447
495
|
const tries = Number(prior?.tries ?? 0) + (keepRefreshWait ? 0 : 1);
|
|
448
496
|
const wait = Math.min(DEAD_BACKOFF_START_MS * 2 ** (tries - 1), DEAD_BACKOFF_MAX_MS);
|
|
449
|
-
|
|
497
|
+
// `hinted` is which sentence this mark already said (an old mark said the
|
|
498
|
+
// sign-in one). A refusal after "no answer" is news: the first line promised
|
|
499
|
+
// a retry, and this one says a person must sign in again. A transient try in
|
|
500
|
+
// between says nothing and forgets nothing.
|
|
501
|
+
const hintKind = kind === "personal" ? "personal" : reason === "refresh_uncertain" ? "renewal" : "reauth";
|
|
502
|
+
const priorHint = prior?.hinted ?? (kind === "personal" ? "personal" : "reauth");
|
|
503
|
+
const alreadyToldThem = prior?.notified === true && (reason === "refresh_transient" || priorHint === hintKind);
|
|
504
|
+
const attemptedAt = Number(prior?.attemptedAt);
|
|
450
505
|
const mark = {
|
|
451
506
|
fingerprint: credentialFingerprint(subjectFor(kind, stored)),
|
|
452
507
|
reason,
|
|
453
508
|
tries,
|
|
454
509
|
since: prior?.since ?? new Date(now()).toISOString(),
|
|
455
|
-
|
|
510
|
+
// A refusal is final. A "no answer" is retried: on the same backoff as
|
|
511
|
+
// every other transient failure, but never inside the window in which the
|
|
512
|
+
// server still treats a repeat as the same request (LOCK_STALE_MS, longer
|
|
513
|
+
// than its own grace), counted from when that try started (#8221).
|
|
514
|
+
until: reason === "refresh_refused" ? null
|
|
515
|
+
: reason === "refresh_uncertain" ? Math.max(now() + wait, (Number.isFinite(attemptedAt) ? attemptedAt : now()) + LOCK_STALE_MS)
|
|
516
|
+
: keepRefreshWait ? prior.until : now() + wait,
|
|
517
|
+
...(Number.isFinite(attemptedAt) ? { attemptedAt } : {}),
|
|
456
518
|
notified: alreadyToldThem || reason !== "refresh_transient",
|
|
519
|
+
...(reason !== "refresh_transient" && !alreadyToldThem ? { hinted: hintKind } : prior?.hinted ? { hinted: prior.hinted } : {}),
|
|
457
520
|
};
|
|
458
521
|
try {
|
|
459
522
|
await mkdir(viewableHome(env), { recursive: true });
|
|
@@ -462,7 +525,8 @@ async function markCredentialDead(stored, env, now, reason, prior, notify, kind
|
|
|
462
525
|
// The personal token gets its own sentence (B2): it is rejected, not
|
|
463
526
|
// expired, and the fix is a fresh token from Settings, not `glytch login`.
|
|
464
527
|
if (reason !== "refresh_transient" && !alreadyToldThem) {
|
|
465
|
-
(notify ?? writeToStderr)(kind === "personal" ? PERSONAL_TOKEN_REJECTED_HINT
|
|
528
|
+
(notify ?? writeToStderr)(kind === "personal" ? PERSONAL_TOKEN_REJECTED_HINT
|
|
529
|
+
: reason === "refresh_uncertain" ? RENEWAL_NO_ANSWER_HINT : REAUTH_REQUIRED_HINT);
|
|
466
530
|
}
|
|
467
531
|
return mark;
|
|
468
532
|
}
|
|
@@ -474,20 +538,23 @@ async function markCredentialDead(stored, env, now, reason, prior, notify, kind
|
|
|
474
538
|
* already makes. Deliberately ignores `until`: that bounds when the next
|
|
475
539
|
* access-token probe is allowed. A permanent refresh refusal never probes.
|
|
476
540
|
*
|
|
477
|
-
*
|
|
478
|
-
*
|
|
479
|
-
*
|
|
480
|
-
* and PERSONAL_TOKEN_REJECTED_HINT already said what to do about the other
|
|
481
|
-
* one. So this checks the OAuth mark whenever there IS an OAuth pair to fall
|
|
482
|
-
* back on, and only checks the personal mark when there is nothing else -
|
|
483
|
-
* exactly `hostedToken`'s own branching, below. */
|
|
541
|
+
* Each record holds one kind of credential, so this reads that kind's own
|
|
542
|
+
* mark. A record holding both kinds (#8324, isMixedRecord) always needs a
|
|
543
|
+
* new sign-in: `hostedToken` never uses it. */
|
|
484
544
|
export async function reauthRequired(env = process.env) {
|
|
485
545
|
const stored = await readAuthFile(env);
|
|
486
546
|
if (stored === null) return false;
|
|
487
|
-
if (
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
547
|
+
if (isMixedRecord(stored)) return true;
|
|
548
|
+
return (await matchingDeadMark(stored, env, stored.personal === true ? "personal" : "oauth")) !== null;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/** The sentence for a sign-in that is not working: the "no answer, trying
|
|
552
|
+
* again" line while the runner is still retrying a renewal, the sign-in
|
|
553
|
+
* line otherwise. Same one-file read as reauthRequired. */
|
|
554
|
+
export async function reauthHint(env = process.env) {
|
|
555
|
+
const stored = await readAuthFile(env);
|
|
556
|
+
const mark = stored === null || isMixedRecord(stored) ? null : await matchingDeadMark(stored, env, "oauth");
|
|
557
|
+
return mark?.reason === "refresh_uncertain" ? RENEWAL_NO_ANSWER_HINT : REAUTH_REQUIRED_HINT;
|
|
491
558
|
}
|
|
492
559
|
|
|
493
560
|
/** A 401 the endpoint gave us on a token a forced refresh could not fix -
|
|
@@ -499,14 +566,12 @@ export async function reauthRequired(env = process.env) {
|
|
|
499
566
|
*
|
|
500
567
|
* `token` IS THE BEARER THAT WAS ACTUALLY SENT (2026-09-19) - the caller's
|
|
501
568
|
* own record of what went out on the wire, not a guess re-derived from
|
|
502
|
-
* `auth.json` after the fact.
|
|
503
|
-
*
|
|
504
|
-
* caller knows which one this particular 401 was about. Omitting `token`
|
|
569
|
+
* `auth.json` after the fact. A 401 on a bearer that is no longer on file
|
|
570
|
+
* (refreshed, or replaced by a new sign-in) marks nothing. Omitting `token`
|
|
505
571
|
* (kept for the handful of direct test calls this file's own suite makes) is
|
|
506
|
-
* read as "the record's own
|
|
507
|
-
* `personal: true` record, the OAuth pair otherwise
|
|
508
|
-
*
|
|
509
|
-
* rejection on one, so every real caller now passes `token`. */
|
|
572
|
+
* read as "the record's own credential" - the personal token for a
|
|
573
|
+
* `personal: true` record, the OAuth pair otherwise. Every real caller
|
|
574
|
+
* passes `token`. */
|
|
510
575
|
export async function noteCredentialRejected({ env = process.env, now = () => Date.now(), notify, token } = {}) {
|
|
511
576
|
const stored = await readAuthFile(env);
|
|
512
577
|
if (stored === null) return false;
|
|
@@ -521,23 +586,17 @@ export async function noteCredentialRejected({ env = process.env, now = () => Da
|
|
|
521
586
|
return true;
|
|
522
587
|
}
|
|
523
588
|
|
|
524
|
-
/** Which credential a 401 was about: the
|
|
525
|
-
*
|
|
526
|
-
*
|
|
527
|
-
* the
|
|
528
|
-
*
|
|
529
|
-
*
|
|
530
|
-
* came back, most likely because another process already refreshed - since
|
|
531
|
-
* there is nothing on file to mark it against. */
|
|
589
|
+
/** Which credential a 401 was about: the record's own kind when `token`
|
|
590
|
+
* is its bearer (one record holds one kind since #8324), or, with no
|
|
591
|
+
* `token`, the record's own credential. Returns null when `token` is not
|
|
592
|
+
* the bearer on file - stale by the time the 401 came back, most likely
|
|
593
|
+
* because another process already refreshed or someone signed in again -
|
|
594
|
+
* since there is nothing on file to mark it against. */
|
|
532
595
|
function rejectedKind(stored, token) {
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
}
|
|
538
|
-
if (stored.personal === true) return "personal";
|
|
539
|
-
if (typeof stored.refreshToken === "string" && stored.refreshToken !== "") return "oauth";
|
|
540
|
-
return null;
|
|
596
|
+
const kind = stored.personal === true ? "personal" : "oauth";
|
|
597
|
+
if (filled(token)) return token === stored.accessToken ? kind : null;
|
|
598
|
+
if (kind === "personal") return kind;
|
|
599
|
+
return filled(stored.refreshToken) ? "oauth" : null;
|
|
541
600
|
}
|
|
542
601
|
|
|
543
602
|
/** A valid access token, or null when there is genuinely none to be had.
|
|
@@ -568,55 +627,31 @@ export async function hostedToken({
|
|
|
568
627
|
const stored = await readAuthFile(env);
|
|
569
628
|
if (!stored) return null;
|
|
570
629
|
|
|
571
|
-
// A
|
|
572
|
-
//
|
|
630
|
+
// A RECORD HOLDING BOTH KINDS IS NEVER USED (#8324). An older bridge's
|
|
631
|
+
// `login()` kept the previous sign-in beside the new one, and nothing on
|
|
632
|
+
// file says whether the two belong to the same account. Offering either
|
|
633
|
+
// could send this computer's requests as the wrong person, so neither is
|
|
634
|
+
// offered: the caller reports REAUTH_REQUIRED_HINT (reauthRequired is
|
|
635
|
+
// true), and `viewable login` writes a record of one kind.
|
|
636
|
+
if (isMixedRecord(stored)) return null;
|
|
637
|
+
|
|
638
|
+
// A STORED PERSONAL TOKEN (brief 43, 2026-09-19) IS NOT AN OAUTH RECORD.
|
|
573
639
|
// `login()` writes `personal: true` when it sees VIEWABLE_BOARD_TOKEN in ITS
|
|
574
640
|
// OWN environment - the paste-and-run install sets the variable for one
|
|
575
641
|
// shell, and the daemon that keeps running after that shell is gone needs
|
|
576
|
-
// the same bearer without the variable.
|
|
577
|
-
//
|
|
578
|
-
// `
|
|
579
|
-
//
|
|
642
|
+
// the same bearer without the variable. It has no `accessExpiresAt` to
|
|
643
|
+
// compare against (it would read as expired, being `undefined`), no
|
|
644
|
+
// `refreshToken` to spend, and no refresh attempt - a `pmb_` token is
|
|
645
|
+
// revoked from Settings, not refreshed.
|
|
580
646
|
//
|
|
581
647
|
// IT STILL GOES THROUGH THE DEAD GATE (2026-09-19, brief 43 review, B2).
|
|
582
648
|
// There is no refresh to fail here, but there is still a 401 a caller can
|
|
583
|
-
// observe and report through `noteCredentialRejected
|
|
584
|
-
//
|
|
585
|
-
//
|
|
586
|
-
//
|
|
587
|
-
//
|
|
588
|
-
if (stored.personal === true)
|
|
589
|
-
const oauthPair = hasOAuthPair(stored);
|
|
590
|
-
// PERSONAL-TOKEN DEAD GATE. Checked against the personal mark ONLY -
|
|
591
|
-
// 2026-09-19, the fix for the loop described at subjectFor's own
|
|
592
|
-
// comment. A rejected personal token must stop being offered for the
|
|
593
|
-
// WHOLE backoff window regardless of what the OAuth pair beside it is
|
|
594
|
-
// doing, and an OAuth-pair rejection (case b, below) must never silence
|
|
595
|
-
// the personal token - they are two credentials, and this gate only
|
|
596
|
-
// ever asks about one of them.
|
|
597
|
-
const personalDead = await matchingDeadMark(stored, env, "personal");
|
|
598
|
-
const personalIsDead = personalDead !== null && now() < Number(personalDead.until);
|
|
599
|
-
if (!personalIsDead) return stored.accessToken;
|
|
600
|
-
if (!oauthPair) return null;
|
|
601
|
-
// FALL THROUGH TO THE OAUTH PAIR (2026-09-19, brief 43 review, B7;
|
|
602
|
-
// 2026-09-19 fix). `login()` with VIEWABLE_BOARD_TOKEN over an existing
|
|
603
|
-
// OAuth credential keeps both in the same record, so a personal token
|
|
604
|
-
// with a live dead mark falls through to the OAuth pair rather than
|
|
605
|
-
// handing back null for the whole backoff window while a working
|
|
606
|
-
// credential sits right beside the refused one - but it is an ORDINARY
|
|
607
|
-
// refresh here, not `forceRefresh: true`: the merged record's cached
|
|
608
|
-
// OAuth access token lives in its OWN fields (`oauthAccessToken`,
|
|
609
|
-
// `oauthAccessExpiresAt`, below), never in `accessToken`/
|
|
610
|
-
// `accessExpiresAt` (those describe the personal token and only the
|
|
611
|
-
// personal token, so nothing here has to guess which credential a
|
|
612
|
-
// shared field is about). Once that OAuth access token is fresh, N
|
|
613
|
-
// more polls with the personal mark still live read it straight off
|
|
614
|
-
// disk - no refresh, no rotation - until the caller's own `forceRefresh`
|
|
615
|
-
// or a real 401 on the OAuth side asks for one.
|
|
616
|
-
return oauthAccessToken(stored, {
|
|
617
|
-
env, fetchImpl, now, notify, forceRefresh, log,
|
|
618
|
-
});
|
|
619
|
-
}
|
|
649
|
+
// observe and report through `noteCredentialRejected`, and `subjectFor`
|
|
650
|
+
// fingerprints a personal record's mark against the access token itself,
|
|
651
|
+
// in its own file. A rejected personal token stops being offered for the
|
|
652
|
+
// whole backoff window. There is no other credential to fall back on
|
|
653
|
+
// (#8324): the fix is a fresh token or `viewable login`.
|
|
654
|
+
if (stored.personal === true) return personalToken(stored, env, now);
|
|
620
655
|
|
|
621
656
|
// THE DEAD GATE, and it sits BEFORE the freshness check on purpose - see the
|
|
622
657
|
// block above. Local freshness is a claim about a clock; the mark is what
|
|
@@ -629,30 +664,21 @@ export async function hostedToken({
|
|
|
629
664
|
return oauthAccessToken(stored, { env, fetchImpl, now, notify, forceRefresh, log });
|
|
630
665
|
}
|
|
631
666
|
|
|
632
|
-
/**
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
*
|
|
640
|
-
*
|
|
641
|
-
*
|
|
642
|
-
* must never be read or overwritten here (2026-09-19 fix - this is the
|
|
643
|
-
* answer to "does `accessExpiresAt` on a merged record describe the OAuth
|
|
644
|
-
* access token or the personal one": it is always the personal one on a
|
|
645
|
-
* merged record, which is exactly why the OAuth side gets its own two
|
|
646
|
-
* fields rather than sharing). The dead mark this function writes on a
|
|
647
|
-
* terminal refusal is always the `"oauth"` kind - refusing to renew the
|
|
648
|
-
* refresh token is a fact about the OAuth pair regardless of which field
|
|
649
|
-
* shape asked for the refresh. */
|
|
667
|
+
/** A personal record's token, unless its own mark is live. */
|
|
668
|
+
async function personalToken(stored, env, now) {
|
|
669
|
+
const dead = await matchingDeadMark(stored, env, "personal");
|
|
670
|
+
return dead !== null && now() < Number(dead.until) ? null : stored.accessToken;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
/** The freshness check, the single-flight refresh and its lock, for a
|
|
674
|
+
* plain OAuth record (`accessToken`/`accessExpiresAt` are its bearer).
|
|
675
|
+
* The dead mark this function writes on a terminal refusal is the
|
|
676
|
+
* `"oauth"` kind. */
|
|
650
677
|
async function oauthAccessToken(stored, {
|
|
651
678
|
env, forceRefresh, fetchImpl, now, notify,
|
|
652
679
|
log = () => {},
|
|
653
680
|
}) {
|
|
654
|
-
|
|
655
|
-
// Both ordinary credentials and a personal-token fallback obey this gate.
|
|
681
|
+
// Every OAuth record obeys this gate.
|
|
656
682
|
// Older finite refusals get one re-check after their cooldown.
|
|
657
683
|
const key = authFilePath(env);
|
|
658
684
|
const running = refreshInFlight.get(key);
|
|
@@ -664,29 +690,27 @@ async function oauthAccessToken(stored, {
|
|
|
664
690
|
// and re-read before interpreting the marker as an abandoned exchange.
|
|
665
691
|
const uncertain = dead?.reason === "refresh_uncertain" && dead.until === null;
|
|
666
692
|
if (dead !== null && now() < Number(dead.until)) {
|
|
667
|
-
const t = stored
|
|
693
|
+
const t = stored.accessToken;
|
|
668
694
|
return dead.reason === "refresh_transient" && typeof t === "string" && t !== "" &&
|
|
669
|
-
Number(stored
|
|
695
|
+
Number(stored.accessExpiresAt) > now() ? t : null;
|
|
670
696
|
}
|
|
671
697
|
|
|
672
698
|
const fresh = (row) => {
|
|
673
|
-
const t = row
|
|
674
|
-
return typeof t === "string" && t !== "" && Number(row
|
|
699
|
+
const t = row.accessToken;
|
|
700
|
+
return typeof t === "string" && t !== "" && Number(row.accessExpiresAt) - now() >= REFRESH_SKEW_MS;
|
|
675
701
|
};
|
|
676
|
-
if (!uncertain && !forceRefresh && fresh(stored)) return stored
|
|
702
|
+
if (!uncertain && !forceRefresh && fresh(stored)) return stored.accessToken;
|
|
677
703
|
|
|
678
704
|
if (!stored.refreshToken || !stored.tokenEndpoint || !stored.clientId) {
|
|
679
705
|
if (uncertain) return null;
|
|
680
706
|
// Nothing to refresh WITH. Still usable if it has not actually expired.
|
|
681
|
-
const t = stored
|
|
682
|
-
return typeof t === "string" && t !== "" && Number(stored
|
|
707
|
+
const t = stored.accessToken;
|
|
708
|
+
return typeof t === "string" && t !== "" && Number(stored.accessExpiresAt) > now() ? t : null;
|
|
683
709
|
}
|
|
684
710
|
|
|
685
711
|
// Keyed by the auth file rather than by process: a test with its own
|
|
686
712
|
// GLYTCH_HOME, and a person with two, are genuinely separate credentials
|
|
687
|
-
// and must not queue behind each other.
|
|
688
|
-
// `fields` shape at a time (a record is either personal or is not), so
|
|
689
|
-
// the path alone is still the right key.
|
|
713
|
+
// and must not queue behind each other.
|
|
690
714
|
const attempt = (async () => {
|
|
691
715
|
const lock = await takeRefreshLock(env, now);
|
|
692
716
|
let source = stored;
|
|
@@ -700,22 +724,31 @@ async function oauthAccessToken(stored, {
|
|
|
700
724
|
// disk means somebody refreshed while we waited, and their result is
|
|
701
725
|
// the answer to both questions.
|
|
702
726
|
const current = await readAuthFile(env);
|
|
703
|
-
if (current && current
|
|
727
|
+
if (current && current.accessToken !== stored.accessToken) {
|
|
728
|
+
// A new sign-in may have replaced the record while we waited (#8324):
|
|
729
|
+
// a personal one answers through its own gate, never this refresh.
|
|
730
|
+
if (isMixedRecord(current)) return null;
|
|
731
|
+
return current.personal === true ? personalToken(current, env, now) : current.accessToken;
|
|
732
|
+
}
|
|
704
733
|
source = current ?? stored;
|
|
705
734
|
// A process ahead of us may have recorded a failure while we waited.
|
|
706
735
|
const mark = await matchingDeadMark(source, env, "oauth", true);
|
|
707
|
-
if (
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
736
|
+
if (mark?.reason === "refresh_refused" && mark.until === null) return null;
|
|
737
|
+
// A "no answer" is retried, but only here: inside the lock, after the
|
|
738
|
+
// re-read, once the earlier try is older than the window in which the
|
|
739
|
+
// server still reads a repeat as the same request (#8221, #3275).
|
|
740
|
+
const lastTry = Number(mark?.attemptedAt ?? Date.parse(mark?.since));
|
|
741
|
+
if (mark?.reason === "refresh_uncertain" && mark.until === null &&
|
|
742
|
+
(lock === null || !(now() - lastTry > LOCK_STALE_MS))) {
|
|
743
|
+
// Only the lock holder can declare an abandoned exchange and give it
|
|
744
|
+
// its timed retry and its one notice. A live holder may still finish.
|
|
745
|
+
if (lock !== null) await markCredentialDead(source, env, now, "refresh_uncertain", mark, notify, "oauth");
|
|
713
746
|
return null;
|
|
714
747
|
}
|
|
715
748
|
if (lock === null || (mark !== null && now() < Number(mark.until))) {
|
|
716
|
-
const t = source
|
|
749
|
+
const t = source.accessToken;
|
|
717
750
|
return (mark === null || mark.reason === "refresh_transient") && typeof t === "string" &&
|
|
718
|
-
t !== "" && Number(source
|
|
751
|
+
t !== "" && Number(source.accessExpiresAt) > now() ? t : null;
|
|
719
752
|
}
|
|
720
753
|
// A REFRESH IS ABOUT TO HAPPEN OVER THE NETWORK (2026-09-21, the
|
|
721
754
|
// claim-to-spawn gap). Every path above this line returns early -
|
|
@@ -727,28 +760,38 @@ async function oauthAccessToken(stored, {
|
|
|
727
760
|
// Persist uncertainty BEFORE spending a single-use token. A crash, timeout,
|
|
728
761
|
// unreadable body or failed save must never make it eligible for replay.
|
|
729
762
|
// Unlike the advisory backoff mark, failure to save this gate is fatal.
|
|
763
|
+
// Carry the retry history across BOTH kinds of failed try, or a renewal
|
|
764
|
+
// that alternates between a lost answer and a 503 starts again at one
|
|
765
|
+
// minute every time and repeats its notice (#8221). A transient mark
|
|
766
|
+
// older than the longest wait is history nobody is waiting on.
|
|
767
|
+
const history = mark?.reason === "refresh_uncertain" ||
|
|
768
|
+
(mark?.reason === "refresh_transient" && now() - Number(mark.until) <= DEAD_BACKOFF_MAX_MS) ? mark : null;
|
|
730
769
|
await writeFileAtomic(deadMarkPath(env), `${JSON.stringify({
|
|
731
770
|
fingerprint: credentialFingerprint(source.refreshToken),
|
|
732
|
-
reason: "refresh_uncertain", until: null, since: new Date(now()).toISOString(),
|
|
771
|
+
reason: "refresh_uncertain", until: null, since: history?.since ?? new Date(now()).toISOString(),
|
|
772
|
+
...(history ? { tries: history.tries, notified: history.notified, ...(history.hinted ? { hinted: history.hinted } : {}) } : {}),
|
|
773
|
+
attemptedAt: now(),
|
|
733
774
|
})}\n`, "utf8", { mode: 0o600 });
|
|
734
775
|
attempted = true;
|
|
735
776
|
const refreshed = await refreshStoredTokens(source, { fetchImpl, now });
|
|
736
777
|
const latest = await readAuthFile(env);
|
|
737
|
-
|
|
778
|
+
// Still inside the lock `login()` also takes (#8324), so no sign-in can
|
|
779
|
+
// land between this read and the write below. A record that no longer
|
|
780
|
+
// holds the refresh token this request spent belongs to a newer
|
|
781
|
+
// sign-in, maybe another account's: drop this result, never write it.
|
|
782
|
+
if (!latest || latest.personal === true || latest.refreshToken !== source.refreshToken) return null;
|
|
738
783
|
log(`refreshed hosted token (${Math.round(now() - refreshStartedAt)}ms)`);
|
|
739
|
-
//
|
|
740
|
-
//
|
|
741
|
-
// snapshot from before the request, preserving the new personal bearer.
|
|
742
|
-
const latestFields = oauthFields(latest);
|
|
784
|
+
// Only the OAuth record's own fields are written: whatever else an
|
|
785
|
+
// older bridge left on the record does not ride along.
|
|
743
786
|
const updated = {
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
// Rotation: a refresh token is single-use (board-oauth's own rule),
|
|
747
|
-
// shared by both field shapes because there is only ever one
|
|
748
|
-
// refresh token on a record no matter which access-token field it
|
|
749
|
-
// is renewing.
|
|
787
|
+
accessToken: refreshed.accessToken,
|
|
788
|
+
// Rotation: a refresh token is single-use (board-oauth's own rule).
|
|
750
789
|
refreshToken: refreshed.refreshToken,
|
|
751
|
-
|
|
790
|
+
accessExpiresAt: refreshed.accessExpiresAt,
|
|
791
|
+
appUrl: latest.appUrl,
|
|
792
|
+
mcpUrl: latest.mcpUrl,
|
|
793
|
+
clientId: latest.clientId,
|
|
794
|
+
tokenEndpoint: latest.tokenEndpoint,
|
|
752
795
|
};
|
|
753
796
|
await writeAuthFile(updated, env);
|
|
754
797
|
// The new access token can be tried, but a successful refresh is not
|
|
@@ -760,11 +803,7 @@ async function oauthAccessToken(stored, {
|
|
|
760
803
|
// resets history, so a recovered outage cannot poison future failures.
|
|
761
804
|
// An active 401 loop retries at its deadline and keeps its history.
|
|
762
805
|
// Ordinary transient-only recovery still clears its history.
|
|
763
|
-
|
|
764
|
-
// fallback does not un-reject the personal token that got rejected;
|
|
765
|
-
// see hostedToken's own comment for why offering it again is the bug
|
|
766
|
-
// this whole file exists to fix.
|
|
767
|
-
if (mark?.notified === true && mark.reason !== "refresh_refused" &&
|
|
806
|
+
if (mark?.notified === true && !["refresh_refused", "refresh_uncertain"].includes(mark.reason) &&
|
|
768
807
|
now() - Number(mark.until) <= DEAD_BACKOFF_MAX_MS) {
|
|
769
808
|
try {
|
|
770
809
|
await writeFileAtomic(deadMarkPath(env), `${JSON.stringify({
|
|
@@ -775,7 +814,7 @@ async function oauthAccessToken(stored, {
|
|
|
775
814
|
} else {
|
|
776
815
|
await clearDeadMark(env, "oauth");
|
|
777
816
|
}
|
|
778
|
-
return updated
|
|
817
|
+
return updated.accessToken;
|
|
779
818
|
} catch (error) {
|
|
780
819
|
const current = await readAuthFile(env);
|
|
781
820
|
// A login during the request must not inherit the old token's failure.
|
|
@@ -801,8 +840,8 @@ async function oauthAccessToken(stored, {
|
|
|
801
840
|
error?.terminal === true ? "refresh_refused" : "refresh_transient",
|
|
802
841
|
prior, notify, "oauth");
|
|
803
842
|
if (error?.terminal === true) return null;
|
|
804
|
-
const t = source
|
|
805
|
-
return typeof t === "string" && t !== "" && Number(source
|
|
843
|
+
const t = source.accessToken;
|
|
844
|
+
return typeof t === "string" && t !== "" && Number(source.accessExpiresAt) > now() ? t : null;
|
|
806
845
|
} finally {
|
|
807
846
|
await dropRefreshLock(lock);
|
|
808
847
|
}
|
|
@@ -832,10 +871,7 @@ function refreshRequestNotSent(error, seen = new Set()) {
|
|
|
832
871
|
}
|
|
833
872
|
|
|
834
873
|
/** The bare refresh_token grant: posts it, and returns just the new pair
|
|
835
|
-
* (never the rest of `stored`) so the caller
|
|
836
|
-
* full record it belongs in - `accessToken`/`accessExpiresAt` for a plain
|
|
837
|
-
* OAuth record, `oauthAccessToken`/`oauthAccessExpiresAt` for a merged
|
|
838
|
-
* one's fallback (see oauthAccessToken's `fields`). */
|
|
874
|
+
* (never the rest of `stored`) so the caller writes the record. */
|
|
839
875
|
async function refreshStoredTokens(stored, { fetchImpl, now }) {
|
|
840
876
|
const controller = new AbortController();
|
|
841
877
|
let timer;
|
|
@@ -954,16 +990,12 @@ export async function hostedStatus(env = process.env) {
|
|
|
954
990
|
// cannot be renewed": a `pmb_` token is revoked from Settings, never
|
|
955
991
|
// refreshed, so the fix is a fresh one from there, not `glytch login`).
|
|
956
992
|
//
|
|
957
|
-
//
|
|
958
|
-
//
|
|
959
|
-
//
|
|
960
|
-
|
|
961
|
-
// personal token is NOT what's dead does the row fall back to the
|
|
962
|
-
// OAuth-pair mark, so a merged record whose fallback has itself run dry
|
|
963
|
-
// still reads as `dead: true` rather than silently green.
|
|
993
|
+
// A RECORD HOLDING BOTH KINDS (#8324) reads as a sign-in that must be done
|
|
994
|
+
// again - the "cannot be renewed: run `viewable login`" row - since
|
|
995
|
+
// hostedToken never uses it. `deadReason: "mixed"` says why to a reader.
|
|
996
|
+
if (isMixedRecord(stored)) return { signedIn: true, via: "viewable login", dead: true, deadReason: "mixed" };
|
|
964
997
|
if (stored.personal === true) {
|
|
965
|
-
const
|
|
966
|
-
const mark = personalMark ?? (hasOAuthPair(stored) ? await matchingDeadMark(stored, env, "oauth") : null);
|
|
998
|
+
const mark = await matchingDeadMark(stored, env, "personal");
|
|
967
999
|
return {
|
|
968
1000
|
signedIn: true,
|
|
969
1001
|
via: "personal token",
|
|
@@ -1205,7 +1237,13 @@ function waitForCallback(
|
|
|
1205
1237
|
// `stillWanted` is how the caller's own exchange knows this flow is
|
|
1206
1238
|
// still the live one: nothing may be written to disk after `finish`
|
|
1207
1239
|
// has already rejected (see login's exchange below).
|
|
1208
|
-
|
|
1240
|
+
// `exchanged` stops the exchange clock once the code has been traded
|
|
1241
|
+
// for a token: the bound is on the network round trip, not on the
|
|
1242
|
+
// save after it, which may wait for another process's refresh lock
|
|
1243
|
+
// for longer than this bound (#8324). Dropping a sign-in the server
|
|
1244
|
+
// has already issued, because a refresher held the lock, would leave
|
|
1245
|
+
// the person signed out for nothing.
|
|
1246
|
+
const stored = await exchange(code, { stillWanted: () => !settled, exchanged: () => clearTimeout(exchangeTimer) });
|
|
1209
1247
|
clearTimeout(exchangeTimer);
|
|
1210
1248
|
if (settled) {
|
|
1211
1249
|
// The bound above already answered the CLI. The browser is still
|
|
@@ -1279,65 +1317,16 @@ export async function login({
|
|
|
1279
1317
|
const scripted = typeof (env.VIEWABLE_BOARD_TOKEN ?? env.GLYTCH_BOARD_TOKEN) === "string" ? (env.VIEWABLE_BOARD_TOKEN ?? env.GLYTCH_BOARD_TOKEN).trim() : "";
|
|
1280
1318
|
if (scripted !== "") {
|
|
1281
1319
|
const base = appUrlFrom(appUrl, env);
|
|
1282
|
-
//
|
|
1283
|
-
//
|
|
1284
|
-
//
|
|
1285
|
-
//
|
|
1286
|
-
//
|
|
1287
|
-
//
|
|
1288
|
-
//
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
// other half).
|
|
1293
|
-
//
|
|
1294
|
-
// THE OAUTH ACCESS TOKEN MOVES INTO ITS OWN FIELDS, NOT JUST THE REFRESH
|
|
1295
|
-
// TOKEN (2026-09-19 fix). B7 above kept `refreshToken` alive but left the
|
|
1296
|
-
// still-valid OAuth ACCESS token to be overwritten by `accessToken:
|
|
1297
|
-
// scripted` with no home to move to, and left the old OAuth
|
|
1298
|
-
// `accessExpiresAt` sitting under the new personal token's `accessToken`
|
|
1299
|
-
// - a timestamp that describes a credential no longer in that field. The
|
|
1300
|
-
// merged record's `personal: true` branch in `hostedToken` reads the
|
|
1301
|
-
// OAuth pair from `oauthAccessToken`/`oauthAccessExpiresAt` (added by the
|
|
1302
|
-
// dead-mark fix, same day), never from `accessToken`/`accessExpiresAt`
|
|
1303
|
-
// once `personal: true` - those describe the personal token and only the
|
|
1304
|
-
// personal token. So the still-live OAuth access token has to land there
|
|
1305
|
-
// BEFORE the personal token takes over `accessToken`, or the fallback
|
|
1306
|
-
// finds nothing cached and spends a refresh rotation on a credential that
|
|
1307
|
-
// was already fresh on disk.
|
|
1308
|
-
//
|
|
1309
|
-
// `existing` MAY ALREADY BE A MERGED RECORD (a second `VIEWABLE_BOARD_TOKEN`
|
|
1310
|
-
// login over one written by an earlier such login): its `accessToken` is
|
|
1311
|
-
// already the personal token, not the OAuth one, so moving it into
|
|
1312
|
-
// `oauthAccessToken` here would clobber a live OAuth access token with a
|
|
1313
|
-
// stale personal one. Only a record that was NOT already `personal: true`
|
|
1314
|
-
// has an OAuth access token sitting in the shared `accessToken` field.
|
|
1315
|
-
//
|
|
1316
|
-
// A personal token has no expiry of its own to compare against
|
|
1317
|
-
// (`hostedToken`'s `personal: true` branch never reads `accessExpiresAt`
|
|
1318
|
-
// for it - see that branch's own comment), so `accessExpiresAt` is
|
|
1319
|
-
// cleared rather than left describing the credential it no longer names.
|
|
1320
|
-
const existing = await readAuthFile(env);
|
|
1321
|
-
const keepsOAuth = typeof existing?.refreshToken === "string" && existing.refreshToken !== "";
|
|
1322
|
-
const result = keepsOAuth
|
|
1323
|
-
? {
|
|
1324
|
-
...existing,
|
|
1325
|
-
accessToken: scripted,
|
|
1326
|
-
accessExpiresAt: undefined,
|
|
1327
|
-
personal: true,
|
|
1328
|
-
appUrl: base,
|
|
1329
|
-
mcpUrl: `${base}/mcp`,
|
|
1330
|
-
oauthAccessToken: existing.personal === true ? existing.oauthAccessToken : existing.accessToken,
|
|
1331
|
-
oauthAccessExpiresAt: existing.personal === true ? existing.oauthAccessExpiresAt : existing.accessExpiresAt,
|
|
1332
|
-
}
|
|
1333
|
-
: { accessToken: scripted, personal: true, appUrl: base, mcpUrl: `${base}/mcp` };
|
|
1334
|
-
await writeAuthFile(result, env);
|
|
1335
|
-
log(
|
|
1336
|
-
keepsOAuth
|
|
1337
|
-
? `VIEWABLE_BOARD_TOKEN is set - saved it to ${authDisplayPath(env)} as a personal access token, keeping the ` +
|
|
1338
|
-
"OAuth sign-in already there beside it. No browser needed."
|
|
1339
|
-
: `VIEWABLE_BOARD_TOKEN is set - saved it to ${authDisplayPath(env)} as a personal access token. No browser needed.`,
|
|
1340
|
-
);
|
|
1320
|
+
// THE NEW TOKEN REPLACES THE WHOLE RECORD (#8324). This used to keep an
|
|
1321
|
+
// existing browser sign-in's refresh token, client id and endpoint
|
|
1322
|
+
// beside the new token (brief 43 review, B7), and hostedToken fell back
|
|
1323
|
+
// to that pair once the token was refused. Nothing proves the two are
|
|
1324
|
+
// the same account, so that fallback could send requests as whoever
|
|
1325
|
+
// signed in before. A refused token now asks for a fresh one, or
|
|
1326
|
+
// `viewable login`, instead.
|
|
1327
|
+
const result = { accessToken: scripted, personal: true, appUrl: base, mcpUrl: `${base}/mcp` };
|
|
1328
|
+
await saveSignIn(result, env, now, { keepPersonalMark: true });
|
|
1329
|
+
log(`VIEWABLE_BOARD_TOKEN is set - saved it to ${authDisplayPath(env)} as a personal access token. No browser needed.`);
|
|
1341
1330
|
return result;
|
|
1342
1331
|
}
|
|
1343
1332
|
|
|
@@ -1433,7 +1422,7 @@ export async function login({
|
|
|
1433
1422
|
expectedState: state,
|
|
1434
1423
|
timeoutMs,
|
|
1435
1424
|
exchangeTimeoutMs,
|
|
1436
|
-
exchange: async (code, { stillWanted = () => true } = {}) => {
|
|
1425
|
+
exchange: async (code, { stillWanted = () => true, exchanged = () => {} } = {}) => {
|
|
1437
1426
|
const tokenResponse = await fetchJson(fetchImpl, token_endpoint, {
|
|
1438
1427
|
method: "POST",
|
|
1439
1428
|
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
@@ -1451,6 +1440,7 @@ export async function login({
|
|
|
1451
1440
|
if (typeof tokenResponse.access_token !== "string") {
|
|
1452
1441
|
throw new Error(`${token_endpoint}: no access_token in the response.`);
|
|
1453
1442
|
}
|
|
1443
|
+
if (stillWanted()) exchanged();
|
|
1454
1444
|
const freshOAuth = {
|
|
1455
1445
|
refreshToken: typeof tokenResponse.refresh_token === "string" ? tokenResponse.refresh_token : null,
|
|
1456
1446
|
appUrl: base,
|
|
@@ -1458,47 +1448,26 @@ export async function login({
|
|
|
1458
1448
|
clientId,
|
|
1459
1449
|
tokenEndpoint: token_endpoint,
|
|
1460
1450
|
};
|
|
1461
|
-
|
|
1462
|
-
//
|
|
1463
|
-
//
|
|
1464
|
-
//
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
// straight; one already merged (a second `glytch login` refreshing
|
|
1471
|
-
// the OAuth side of a record `VIEWABLE_BOARD_TOKEN` already merged
|
|
1472
|
-
// into) hits the same branch, since it is still `personal: true`.
|
|
1473
|
-
const existing = await readAuthFile(env);
|
|
1474
|
-
const keepsPersonal = existing?.personal === true && typeof existing.accessToken === "string" && existing.accessToken !== "";
|
|
1475
|
-
const result = keepsPersonal
|
|
1476
|
-
? {
|
|
1477
|
-
...existing,
|
|
1478
|
-
...freshOAuth,
|
|
1479
|
-
personal: true,
|
|
1480
|
-
accessToken: existing.accessToken,
|
|
1481
|
-
accessExpiresAt: existing.accessExpiresAt,
|
|
1482
|
-
oauthAccessToken: tokenResponse.access_token,
|
|
1483
|
-
oauthAccessExpiresAt: freshAccessExpiresAt,
|
|
1484
|
-
}
|
|
1485
|
-
: { ...freshOAuth, accessToken: tokenResponse.access_token, accessExpiresAt: freshAccessExpiresAt };
|
|
1451
|
+
// THIS SIGN-IN REPLACES THE WHOLE RECORD (#8324). A stored personal
|
|
1452
|
+
// token used to be kept in front of it (the "reverse merge"), so a
|
|
1453
|
+
// browser sign-in as another account went on sending the old
|
|
1454
|
+
// account's token. Nothing proves the two are the same account.
|
|
1455
|
+
const result = {
|
|
1456
|
+
...freshOAuth,
|
|
1457
|
+
accessToken: tokenResponse.access_token,
|
|
1458
|
+
accessExpiresAt: now() + Math.max(0, Number(tokenResponse.expires_in) || 0) * 1000,
|
|
1459
|
+
};
|
|
1486
1460
|
// NOTHING LANDS ON DISK AFTER THIS LOGIN HAS ALREADY FAILED (F2). A
|
|
1487
1461
|
// bound that fired, or a server error, rejects the caller while this
|
|
1488
1462
|
// round trip is still in the air; writing the credential afterwards
|
|
1489
1463
|
// would leave a machine signed in by a command that said it was not.
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
await writeAuthFile(result, env);
|
|
1464
|
+
// Asked again once the lock is held: a server error while it waited
|
|
1465
|
+
// still rejects the caller.
|
|
1466
|
+
//
|
|
1494
1467
|
// THE HUMAN ACT THE DEAD MARK ASKS FOR HAS HAPPENED (2026-09-13), so
|
|
1495
|
-
//
|
|
1496
|
-
// credential
|
|
1497
|
-
|
|
1498
|
-
// personal token kept alongside was not replaced by this sign-in, so
|
|
1499
|
-
// whatever `PERSONAL_TOKEN_REJECTED_HINT` already said about it still
|
|
1500
|
-
// stands (2026-09-19 fix - see `keepsPersonal` above).
|
|
1501
|
-
await clearDeadMark(env, keepsPersonal ? "oauth" : undefined);
|
|
1468
|
+
// every mark goes - a new grant means no live mark describes the
|
|
1469
|
+
// credential on file.
|
|
1470
|
+
await saveSignIn(result, env, now, { stillWanted });
|
|
1502
1471
|
return result;
|
|
1503
1472
|
},
|
|
1504
1473
|
});
|