From 629ea28f43bfa5e151ed654550a18978457e5305 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 12:10:47 -0500 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=90=9B=20dashboard:=20render=20the=20?= =?UTF-8?q?Inbox=20once=20the=20viewer's=20role=20is=20known?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Inbox rendered before /api/role resolved, treating an unknown role as read, and nothing re-rendered until the next 60 s refresh. Hold a loading state until the role is known, notify role listeners when it resolves, and retry the role lookup with backoff before falling back to read-only. Signed-off-by: Andy Anderson --- changelog.d/fixed-inbox-render-before-role.md | 1 + dashboard/test/inbox-role-race.test.js | 110 ++++++++++++++++++ src/pkg/dashboard/static-src/js/gh-auth.js | 31 ++++- src/pkg/dashboard/static-src/js/inbox.js | 9 +- src/pkg/dashboard/static/index.html | 40 ++++++- 5 files changed, 183 insertions(+), 8 deletions(-) create mode 100644 changelog.d/fixed-inbox-render-before-role.md create mode 100644 dashboard/test/inbox-role-race.test.js diff --git a/changelog.d/fixed-inbox-render-before-role.md b/changelog.d/fixed-inbox-render-before-role.md new file mode 100644 index 0000000000..48c986a510 --- /dev/null +++ b/changelog.d/fixed-inbox-render-before-role.md @@ -0,0 +1 @@ +- The dashboard Inbox now waits for the viewer's role before its first render and re-renders as soon as the role resolves, so owners no longer lose Share → Hive actions, ▶ Play and the bulk Hive menu until the next 60-second refresh; a failed role lookup is retried with backoff and falls back to read-only. diff --git a/dashboard/test/inbox-role-race.test.js b/dashboard/test/inbox-role-race.test.js new file mode 100644 index 0000000000..00df812c88 --- /dev/null +++ b/dashboard/test/inbox-role-race.test.js @@ -0,0 +1,110 @@ +'use strict'; + +// The Inbox must not render with an unknown role (treated as read) and must +// re-render the moment /api/role resolves, instead of waiting for the 60 s +// refresh. Functions are extracted from the generated dashboard page. +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); + +const indexHtml = fs.readFileSync(path.join(__dirname, '..', '..', 'src', 'pkg', 'dashboard', 'static', 'index.html'), 'utf8'); + +function extractFunction(name) { + const start = indexHtml.indexOf('function ' + name + '('); + assert.notEqual(start, -1, `${name} function exists`); + const brace = indexHtml.indexOf('{', start); + let depth = 0; + for (let i = brace; i < indexHtml.length; i++) { + const ch = indexHtml[i]; + if (ch === '{') depth++; + if (ch === '}') depth--; + if (depth === 0) return indexHtml.slice(start, i + 1); + } + throw new Error(`unterminated ${name}`); +} + +const NAMES = ['inboxRoleKnown', 'inboxOnRoleReady', 'inboxCanShare', 'inboxViewerCanPlay', 'renderInbox', 'hiveAuthResolved', 'hiveFetchRole']; + +function makeSandbox(opts) { + const panel = { innerHTML: '' }; + const sb = { + window: Object.assign({ _hiveRoleListeners: [] }, opts.window || {}), + document: { getElementById: (id) => (id === 'inbox-panel' ? panel : null) }, + panel, + _inboxCache: { items: [{ source: 'plaud', id: 'a', processed: false }], sources: [], settings: {} }, + _inboxSelected: new Set(), + _inboxShowProcessed: false, + _inboxFocusKey: '', + refreshSectionCardShell: () => {}, + setSidebarCountBadge: () => {}, + setSectionSummary: () => {}, + escapeHtml: (s) => String(s), + viewerIsOwner: () => sb.window._hiveRole === 'owner', + inboxSourceIcon: () => '', + inboxCanAdd: () => false, + inboxItemHtml: () => '
', + inboxPreserveAudio: (p, fn) => fn(), + inboxApplyFocus: () => {}, + fetch: opts.fetch, + setTimeout: (fn) => { fn(); return 0; }, + console, Math, Number, Array, Error, Promise, String, + HIVE_ROLE_FETCH_ATTEMPTS: 3, + HIVE_ROLE_RETRY_BASE_MS: 500, + HIVE_ROLE_SERVER_ERROR_STATUS: 500, + }; + vm.createContext(sb); + vm.runInContext(NAMES.map(extractFunction).join('\n'), sb); + // Same registration inbox.js performs at load. + sb.window._hiveRoleListeners.push(sb.inboxOnRoleReady); + return sb; +} + +test('inbox renders a loading state, not a read-only view, while the role is unknown', () => { + const sb = makeSandbox({}); + sb.renderInbox(); + assert.match(sb.panel.innerHTML, /Loading inbox/); + assert.doesNotMatch(sb.panel.innerHTML, /Select all/); +}); + +test('role arriving as owner re-renders the Inbox with the Hive actions', () => { + const sb = makeSandbox({}); + sb.renderInbox(); + assert.match(sb.panel.innerHTML, /Loading inbox/); + const resolved = sb.hiveAuthResolved(); + sb.window._hiveRole = 'owner'; + resolved(); + assert.match(sb.panel.innerHTML, /Select all/); + assert.equal(sb.inboxViewerCanPlay(), true); +}); + +test('role arriving as read keeps the Hive actions hidden', () => { + const sb = makeSandbox({}); + const resolved = sb.hiveAuthResolved(); + sb.window._hiveRole = 'read'; + resolved(); + assert.doesNotMatch(sb.panel.innerHTML, /Loading inbox/); + assert.doesNotMatch(sb.panel.innerHTML, /Select all/); + assert.equal(sb.inboxViewerCanPlay(), false); +}); + +test('role lookup that stays failed resolves auth and falls back to read-only', async () => { + let calls = 0; + const sb = makeSandbox({ fetch: async () => { calls++; throw new Error('network'); } }); + await assert.rejects(sb.hiveFetchRole()); + assert.equal(calls, 3, 'retried before giving up'); + sb.window._hiveRoleFailed = true; + sb.hiveAuthResolved()(); + assert.doesNotMatch(sb.panel.innerHTML, /Loading inbox/); + assert.doesNotMatch(sb.panel.innerHTML, /Select all/); + assert.match(sb.panel.innerHTML, /Could not determine your role/); +}); + +test('role lookup recovers when a retry succeeds', async () => { + let calls = 0; + const sb = makeSandbox({ fetch: async () => { calls++; return calls < 2 ? { status: 503, json: async () => ({}) } : { status: 200, json: async () => ({ role: 'owner' }) }; } }); + const data = await sb.hiveFetchRole(); + assert.equal(data.role, 'owner'); + assert.equal(calls, 2); +}); diff --git a/src/pkg/dashboard/static-src/js/gh-auth.js b/src/pkg/dashboard/static-src/js/gh-auth.js index 4a9329da93..0f5e38a10e 100644 --- a/src/pkg/dashboard/static-src/js/gh-auth.js +++ b/src/pkg/dashboard/static-src/js/gh-auth.js @@ -11,6 +11,11 @@ // browser as the one that asked. let ghFlowID = ''; const GH_AUTH_CHECK_INTERVAL_MS = 300000; + // /api/role is retried on network errors and 5xx before the viewer is + // treated as read-only: 3 attempts, waiting 0.5 s then 1 s between them. + const HIVE_ROLE_FETCH_ATTEMPTS = 3; + const HIVE_ROLE_RETRY_BASE_MS = 500; + const HIVE_ROLE_SERVER_ERROR_STATUS = 500; const GH_AUTH_SUCCESS_DISMISS_MS = 1500; function copyDeviceCode() { @@ -53,10 +58,17 @@ hard reload does not flash the decision pills disabled; when the answer lands, repaint the tracker once — only if something changed. */ function hiveAuthResolved() { + const wasKnown = !!window._hiveAuthKnown; const before = (window._hiveAuthKnown ? 'k' : '') + '|' + String(window._hiveUser || '') + '|' + String(window._hiveRole || ''); window._hiveAuthKnown = true; return function() { const after = 'k|' + String(window._hiveUser || '') + '|' + String(window._hiveRole || ''); + /* Sections that gate on the role (the Inbox) hold a neutral loading + state until it is known; tell them the first time it resolves and + whenever it changes. They push onto window._hiveRoleListeners. */ + if (!wasKnown || after !== before) { + (window._hiveRoleListeners || []).forEach(function(fn) { try { fn(); } catch (e) { console.error(e); } }); + } if (after === before) return; if (typeof renderStageTracker === 'function' && typeof _stageTracker !== 'undefined' && _stageTracker) renderStageTracker(); }; @@ -258,10 +270,23 @@ showToast('Signed out of GitHub', 'info'); } + async function hiveFetchRole() { + let lastErr = null; + for (let attempt = 0; attempt < HIVE_ROLE_FETCH_ATTEMPTS; attempt++) { + if (attempt > 0) await new Promise(function(resolve) { setTimeout(resolve, HIVE_ROLE_RETRY_BASE_MS * Math.pow(2, attempt - 1)); }); + try { + const resp = await fetch('/api/role'); + if (resp.status >= HIVE_ROLE_SERVER_ERROR_STATUS) throw new Error('HTTP ' + resp.status); + return await resp.json(); + } catch (e) { lastErr = e; } + } + throw lastErr; + } + async function checkGHAuth() { try { - const roleResp = await fetch('/api/role'); - const roleData = await roleResp.json(); + const roleData = await hiveFetchRole(); + window._hiveRoleFailed = false; if (roleData.sentinel_label) window._hiveSentinelLabel = roleData.sentinel_label; if (roleData.user) { if (roleData.automerge_label) window._hiveAutoMergeLabel = roleData.automerge_label; @@ -275,7 +300,7 @@ showGHAuthState(true, roleData.user, ghAvatar, roleData.role, roleData.display_name); return; } - } catch (e) {} + } catch (e) { window._hiveRoleFailed = true; } try { const resp = await fetch('/api/gh-user-auth/status'); const data = await resp.json(); diff --git a/src/pkg/dashboard/static-src/js/inbox.js b/src/pkg/dashboard/static-src/js/inbox.js index 2118647470..274a765758 100644 --- a/src/pkg/dashboard/static-src/js/inbox.js +++ b/src/pkg/dashboard/static-src/js/inbox.js @@ -41,6 +41,12 @@ function inboxSourceIcon(src) { return INBOX_SOURCE_ICONS[src] || '📥'; } // Uploaded documents have no audio; uploaded audio/video does. function inboxItemHasAudio(it) { return !it || it.source !== 'upload' || it.upload_kind === 'audio' || it.upload_kind === 'video'; } + // The role is known once /api/role answered (or gave up: then it is + // undefined and every gate treats the viewer as read-only). Before that + // the Inbox renders a neutral loading state, never a guessed 'read'. + function inboxRoleKnown() { return window._hiveAuthKnown === true || window._hiveRole !== undefined; } + function inboxOnRoleReady() { if (_inboxCache) renderInbox(); } + (window._hiveRoleListeners = window._hiveRoleListeners || []).push(inboxOnRoleReady); function inboxItemKey(it) { return it.source + '/' + it.id; } // Playback is a read of private audio: owners and read-write sessions. function inboxViewerCanPlay() { return window._hiveRole === 'owner' || window._hiveRole === 'read-write'; } @@ -635,7 +641,7 @@ const badge = document.getElementById('inbox-sidebar-badge'); const data = _inboxCache; if (!panel) return; - if (!data) { panel.innerHTML = '
Loading inbox…
'; return; } + if (!data || !inboxRoleKnown()) { panel.innerHTML = '
Loading inbox…
'; return; } const items = data.items || []; const unprocessed = Number(data.unprocessed) || 0; if (badge) setSidebarCountBadge(badge, unprocessed, unprocessed + ' unprocessed'); @@ -658,6 +664,7 @@ (owner ? '' : '') + ''; if (data.error) html += '
' + escapeHtml(data.error) + '
'; + if (window._hiveRoleFailed && !window._hiveRole) html += '
Could not determine your role, so actions are read-only. It will retry automatically.
'; if (inboxCanAdd()) html += inboxAddPanelHtml() + inboxUploadsHtml(); if (!settings.spek_ready && owner) html += '

RFC filing needs at least one enabled, unpaused agent to draft the spec with spektacular (inbox.spek.agent overrides the default; inbox.spek.binary names the CLI the agent runs).

'; if (_inboxFocusKey && items.some(it => it.processed && inboxItemKey(it) === _inboxFocusKey)) _inboxShowProcessed = true; diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 55b30aeeb8..13cb9da739 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -47436,6 +47436,12 @@ function inboxSourceIcon(src) { return INBOX_SOURCE_ICONS[src] || '📥'; } // Uploaded documents have no audio; uploaded audio/video does. function inboxItemHasAudio(it) { return !it || it.source !== 'upload' || it.upload_kind === 'audio' || it.upload_kind === 'video'; } + // The role is known once /api/role answered (or gave up: then it is + // undefined and every gate treats the viewer as read-only). Before that + // the Inbox renders a neutral loading state, never a guessed 'read'. + function inboxRoleKnown() { return window._hiveAuthKnown === true || window._hiveRole !== undefined; } + function inboxOnRoleReady() { if (_inboxCache) renderInbox(); } + (window._hiveRoleListeners = window._hiveRoleListeners || []).push(inboxOnRoleReady); function inboxItemKey(it) { return it.source + '/' + it.id; } // Playback is a read of private audio: owners and read-write sessions. function inboxViewerCanPlay() { return window._hiveRole === 'owner' || window._hiveRole === 'read-write'; } @@ -48030,7 +48036,7 @@ const badge = document.getElementById('inbox-sidebar-badge'); const data = _inboxCache; if (!panel) return; - if (!data) { panel.innerHTML = '
Loading inbox…
'; return; } + if (!data || !inboxRoleKnown()) { panel.innerHTML = '
Loading inbox…
'; return; } const items = data.items || []; const unprocessed = Number(data.unprocessed) || 0; if (badge) setSidebarCountBadge(badge, unprocessed, unprocessed + ' unprocessed'); @@ -48053,6 +48059,7 @@ (owner ? '' : '') + ''; if (data.error) html += '
' + escapeHtml(data.error) + '
'; + if (window._hiveRoleFailed && !window._hiveRole) html += '
Could not determine your role, so actions are read-only. It will retry automatically.
'; if (inboxCanAdd()) html += inboxAddPanelHtml() + inboxUploadsHtml(); if (!settings.spek_ready && owner) html += '

RFC filing needs at least one enabled, unpaused agent to draft the spec with spektacular (inbox.spek.agent overrides the default; inbox.spek.binary names the CLI the agent runs).

'; if (_inboxFocusKey && items.some(it => it.processed && inboxItemKey(it) === _inboxFocusKey)) _inboxShowProcessed = true; @@ -78166,6 +78173,11 @@

${model.editIndex === null ? 'Add Event trigger' : 'Edit Event trigger'}

${model.editIndex === null ? 'Add Event trigger' : 'Edit Event trigger'}${model.editIndex === null ? 'Add Event trigger' : 'Edit Event trigger'} 0) await new Promise(function(resolve) { setTimeout(resolve, HIVE_ROLE_RETRY_BASE_MS * Math.pow(2, attempt - 1)); }); + try { + const resp = await fetch('/api/role'); + if (resp.status >= HIVE_ROLE_SERVER_ERROR_STATUS) throw new Error('HTTP ' + resp.status); + return await resp.json(); + } catch (e) { lastErr = e; } + } + throw lastErr; + } + async function checkGHAuth() { try { - const roleResp = await fetch('/api/role'); - const roleData = await roleResp.json(); + const roleData = await hiveFetchRole(); + window._hiveRoleFailed = false; if (roleData.sentinel_label) window._hiveSentinelLabel = roleData.sentinel_label; if (roleData.user) { if (roleData.automerge_label) window._hiveAutoMergeLabel = roleData.automerge_label; @@ -78430,7 +78462,7 @@

${model.editIndex === null ? 'Add Event trigger' : 'Edit Event trigger'}

Date: Mon, 16 Nov 2026 12:16:43 -0500 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=90=9B=20test:=20keep=20the=20async?= =?UTF-8?q?=20prefix=20when=20extracting=20hiveFetchRole?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andy Anderson --- dashboard/test/inbox-role-race.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dashboard/test/inbox-role-race.test.js b/dashboard/test/inbox-role-race.test.js index 00df812c88..8e7fc773bd 100644 --- a/dashboard/test/inbox-role-race.test.js +++ b/dashboard/test/inbox-role-race.test.js @@ -20,7 +20,7 @@ function extractFunction(name) { const ch = indexHtml[i]; if (ch === '{') depth++; if (ch === '}') depth--; - if (depth === 0) return indexHtml.slice(start, i + 1); + if (depth === 0) return (indexHtml.slice(start - 6, start) === 'async ' ? 'async ' : '') + indexHtml.slice(start, i + 1); } throw new Error(`unterminated ${name}`); } From 652293299e3ac2002e73b57a98a0a069527ae639 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 12:36:31 -0500 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=90=9B=20dashboard:=20retry=20/api/ro?= =?UTF-8?q?le=20only=20on=20network=20errors=20and=205xx?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andy Anderson --- src/pkg/dashboard/static-src/js/gh-auth.js | 9 ++++++--- src/pkg/dashboard/static/index.html | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/pkg/dashboard/static-src/js/gh-auth.js b/src/pkg/dashboard/static-src/js/gh-auth.js index 0f5e38a10e..89787a7509 100644 --- a/src/pkg/dashboard/static-src/js/gh-auth.js +++ b/src/pkg/dashboard/static-src/js/gh-auth.js @@ -274,11 +274,14 @@ let lastErr = null; for (let attempt = 0; attempt < HIVE_ROLE_FETCH_ATTEMPTS; attempt++) { if (attempt > 0) await new Promise(function(resolve) { setTimeout(resolve, HIVE_ROLE_RETRY_BASE_MS * Math.pow(2, attempt - 1)); }); + let resp; try { - const resp = await fetch('/api/role'); + resp = await fetch('/api/role'); if (resp.status >= HIVE_ROLE_SERVER_ERROR_STATUS) throw new Error('HTTP ' + resp.status); - return await resp.json(); - } catch (e) { lastErr = e; } + } catch (e) { lastErr = e; continue; } + // An answer that is not retryable (4xx, an HTML 404 from a static + // host) is final: parse it once and let the caller fall back. + return await resp.json(); } throw lastErr; } diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 13cb9da739..bec7cdc23c 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -78436,11 +78436,14 @@

${model.editIndex === null ? 'Add Event trigger' : 'Edit Event trigger'}

0) await new Promise(function(resolve) { setTimeout(resolve, HIVE_ROLE_RETRY_BASE_MS * Math.pow(2, attempt - 1)); }); + let resp; try { - const resp = await fetch('/api/role'); + resp = await fetch('/api/role'); if (resp.status >= HIVE_ROLE_SERVER_ERROR_STATUS) throw new Error('HTTP ' + resp.status); - return await resp.json(); - } catch (e) { lastErr = e; } + } catch (e) { lastErr = e; continue; } + // An answer that is not retryable (4xx, an HTML 404 from a static + // host) is final: parse it once and let the caller fall back. + return await resp.json(); } throw lastErr; } From 4491d9ab0c6cd7736e01ca80da29a9a3fbab0860 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 12:48:17 -0500 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=90=9B=20dashboard:=20settle=20the=20?= =?UTF-8?q?role=20separately=20from=20tracker=20auth=20so=20retries=20neve?= =?UTF-8?q?r=20delay=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andy Anderson --- dashboard/test/inbox-role-race.test.js | 43 +++++--- src/pkg/dashboard/static-src/js/gh-auth.js | 103 ++++++++++++------- src/pkg/dashboard/static-src/js/inbox.js | 8 +- src/pkg/dashboard/static/index.html | 111 +++++++++++++-------- 4 files changed, 171 insertions(+), 94 deletions(-) diff --git a/dashboard/test/inbox-role-race.test.js b/dashboard/test/inbox-role-race.test.js index 8e7fc773bd..e60a5fc088 100644 --- a/dashboard/test/inbox-role-race.test.js +++ b/dashboard/test/inbox-role-race.test.js @@ -25,12 +25,12 @@ function extractFunction(name) { throw new Error(`unterminated ${name}`); } -const NAMES = ['inboxRoleKnown', 'inboxOnRoleReady', 'inboxCanShare', 'inboxViewerCanPlay', 'renderInbox', 'hiveAuthResolved', 'hiveFetchRole']; +const NAMES = ['inboxRoleKnown', 'inboxOnRoleReady', 'inboxCanShare', 'inboxViewerCanPlay', 'renderInbox', 'hiveFetchRoleOnce', 'hiveRoleSettled', 'hiveApplyRoleData', 'hiveRetryRole']; function makeSandbox(opts) { const panel = { innerHTML: '' }; const sb = { - window: Object.assign({ _hiveRoleListeners: [] }, opts.window || {}), + window: { _hiveRoleListeners: [] }, document: { getElementById: (id) => (id === 'inbox-panel' ? panel : null) }, panel, _inboxCache: { items: [{ source: 'plaud', id: 'a', processed: false }], sources: [], settings: {} }, @@ -47,6 +47,8 @@ function makeSandbox(opts) { inboxItemHtml: () => '
', inboxPreserveAudio: (p, fn) => fn(), inboxApplyFocus: () => {}, + // Same effect as the real showGHAuthState: it records the role. + showGHAuthState: (loggedIn, user, avatar, role) => { sb.window._hiveUser = user; if (role) sb.window._hiveRole = role; }, fetch: opts.fetch, setTimeout: (fn) => { fn(); return 0; }, console, Math, Number, Array, Error, Promise, String, @@ -72,30 +74,37 @@ test('role arriving as owner re-renders the Inbox with the Hive actions', () => const sb = makeSandbox({}); sb.renderInbox(); assert.match(sb.panel.innerHTML, /Loading inbox/); - const resolved = sb.hiveAuthResolved(); sb.window._hiveRole = 'owner'; - resolved(); + sb.hiveRoleSettled(false); assert.match(sb.panel.innerHTML, /Select all/); assert.equal(sb.inboxViewerCanPlay(), true); }); test('role arriving as read keeps the Hive actions hidden', () => { const sb = makeSandbox({}); - const resolved = sb.hiveAuthResolved(); sb.window._hiveRole = 'read'; - resolved(); + sb.hiveRoleSettled(false); assert.doesNotMatch(sb.panel.innerHTML, /Loading inbox/); assert.doesNotMatch(sb.panel.innerHTML, /Select all/); assert.equal(sb.inboxViewerCanPlay(), false); }); -test('role lookup that stays failed resolves auth and falls back to read-only', async () => { +test('an unchanged role does not re-render on later auth checks', () => { + const sb = makeSandbox({}); + sb.window._hiveRole = 'owner'; + sb.hiveRoleSettled(false); + sb.panel.innerHTML = 'sentinel'; + sb.hiveRoleSettled(false); + assert.equal(sb.panel.innerHTML, 'sentinel'); +}); + +test('role lookup that stays failed retries, then falls back to read-only and says so', async () => { let calls = 0; const sb = makeSandbox({ fetch: async () => { calls++; throw new Error('network'); } }); - await assert.rejects(sb.hiveFetchRole()); - assert.equal(calls, 3, 'retried before giving up'); - sb.window._hiveRoleFailed = true; - sb.hiveAuthResolved()(); + sb.renderInbox(); + assert.match(sb.panel.innerHTML, /Loading inbox/); + await sb.hiveRetryRole(); + assert.equal(calls, 2, 'retried after the first attempt (made by checkGHAuth)'); assert.doesNotMatch(sb.panel.innerHTML, /Loading inbox/); assert.doesNotMatch(sb.panel.innerHTML, /Select all/); assert.match(sb.panel.innerHTML, /Could not determine your role/); @@ -103,8 +112,14 @@ test('role lookup that stays failed resolves auth and falls back to read-only', test('role lookup recovers when a retry succeeds', async () => { let calls = 0; - const sb = makeSandbox({ fetch: async () => { calls++; return calls < 2 ? { status: 503, json: async () => ({}) } : { status: 200, json: async () => ({ role: 'owner' }) }; } }); - const data = await sb.hiveFetchRole(); - assert.equal(data.role, 'owner'); + const sb = makeSandbox({ fetch: async () => { calls++; return calls < 2 ? { status: 503, json: async () => ({}) } : { status: 200, json: async () => ({ user: 'andy', role: 'owner' }) }; } }); + await sb.hiveRetryRole(); assert.equal(calls, 2); + assert.match(sb.panel.innerHTML, /Select all/); + assert.doesNotMatch(sb.panel.innerHTML, /Could not determine your role/); +}); + +test('a non-retryable answer is not retried', async () => { + const sb = makeSandbox({ fetch: async () => ({ status: 404, json: async () => { throw new Error('not json'); } }) }); + await assert.rejects(sb.hiveFetchRoleOnce(), (e) => !e.retryable); }); diff --git a/src/pkg/dashboard/static-src/js/gh-auth.js b/src/pkg/dashboard/static-src/js/gh-auth.js index 89787a7509..e7a62e48b5 100644 --- a/src/pkg/dashboard/static-src/js/gh-auth.js +++ b/src/pkg/dashboard/static-src/js/gh-auth.js @@ -58,17 +58,10 @@ hard reload does not flash the decision pills disabled; when the answer lands, repaint the tracker once — only if something changed. */ function hiveAuthResolved() { - const wasKnown = !!window._hiveAuthKnown; const before = (window._hiveAuthKnown ? 'k' : '') + '|' + String(window._hiveUser || '') + '|' + String(window._hiveRole || ''); window._hiveAuthKnown = true; return function() { const after = 'k|' + String(window._hiveUser || '') + '|' + String(window._hiveRole || ''); - /* Sections that gate on the role (the Inbox) hold a neutral loading - state until it is known; tell them the first time it resolves and - whenever it changes. They push onto window._hiveRoleListeners. */ - if (!wasKnown || after !== before) { - (window._hiveRoleListeners || []).forEach(function(fn) { try { fn(); } catch (e) { console.error(e); } }); - } if (after === before) return; if (typeof renderStageTracker === 'function' && typeof _stageTracker !== 'undefined' && _stageTracker) renderStageTracker(); }; @@ -270,40 +263,78 @@ showToast('Signed out of GitHub', 'info'); } - async function hiveFetchRole() { - let lastErr = null; - for (let attempt = 0; attempt < HIVE_ROLE_FETCH_ATTEMPTS; attempt++) { - if (attempt > 0) await new Promise(function(resolve) { setTimeout(resolve, HIVE_ROLE_RETRY_BASE_MS * Math.pow(2, attempt - 1)); }); - let resp; - try { - resp = await fetch('/api/role'); - if (resp.status >= HIVE_ROLE_SERVER_ERROR_STATUS) throw new Error('HTTP ' + resp.status); - } catch (e) { lastErr = e; continue; } - // An answer that is not retryable (4xx, an HTML 404 from a static - // host) is final: parse it once and let the caller fall back. - return await resp.json(); + /* One /api/role attempt. Network errors and 5xx are marked retryable; an + answer that is not (4xx, an HTML 404 from a static host) is final. */ + async function hiveFetchRoleOnce() { + let resp; + try { resp = await fetch('/api/role'); } catch (e) { e.retryable = true; throw e; } + if (resp.status >= HIVE_ROLE_SERVER_ERROR_STATUS) { + const err = new Error('HTTP ' + resp.status); + err.retryable = true; + throw err; } - throw lastErr; + return await resp.json(); + } + + /* The role is "settled" once /api/role answered or gave up. Sections that + gate on it (the Inbox) hold a neutral loading state until then and + subscribe via window._hiveRoleListeners; they are notified the first + time and whenever the role or the failed flag changes. This is separate + from _hiveAuthKnown so the tracker never waits on the retries below. */ + function hiveRoleSettled(failed) { + const sig = String(window._hiveRole || '') + '|' + (failed ? 'failed' : 'ok'); + const changed = !window._hiveRoleSettled || window._hiveRoleSig !== sig; + window._hiveRoleFailed = !!failed; + window._hiveRoleSettled = true; + window._hiveRoleSig = sig; + if (!changed) return; + (window._hiveRoleListeners || []).forEach(function(fn) { try { fn(); } catch (e) { console.error(e); } }); + } + + function hiveApplyRoleData(roleData) { + if (roleData.sentinel_label) window._hiveSentinelLabel = roleData.sentinel_label; + if (!roleData.user) return false; + if (roleData.automerge_label) window._hiveAutoMergeLabel = roleData.automerge_label; + /* OIDC identity keys ("ibmid:5500…") are not GitHub logins — don't + fabricate a github.com/.png avatar for them; showGHAuthState + falls back to an initials avatar and the server-resolved + display_name. */ + var ghAvatar = roleData.user.indexOf(':') === -1 + ? 'https://github.com/' + roleData.user + '.png' + : ''; + showGHAuthState(true, roleData.user, ghAvatar, roleData.role, roleData.display_name); + return true; + } + + /* Background retries after a retryable first failure, with exponential + backoff. If they all fail the viewer stays read-only and the Inbox says + so. */ + async function hiveRetryRole() { + if (window._hiveRoleRetrying) return; + window._hiveRoleRetrying = true; + try { + for (let attempt = 1; attempt < HIVE_ROLE_FETCH_ATTEMPTS; attempt++) { + await new Promise(function(resolve) { setTimeout(resolve, HIVE_ROLE_RETRY_BASE_MS * Math.pow(2, attempt - 1)); }); + try { + const roleData = await hiveFetchRoleOnce(); + hiveApplyRoleData(roleData); + hiveRoleSettled(false); + return; + } catch (e) { if (!e.retryable) break; } + } + hiveRoleSettled(true); + } finally { window._hiveRoleRetrying = false; } } async function checkGHAuth() { try { - const roleData = await hiveFetchRole(); - window._hiveRoleFailed = false; - if (roleData.sentinel_label) window._hiveSentinelLabel = roleData.sentinel_label; - if (roleData.user) { - if (roleData.automerge_label) window._hiveAutoMergeLabel = roleData.automerge_label; - /* OIDC identity keys ("ibmid:5500…") are not GitHub logins — don't - fabricate a github.com/.png avatar for them; showGHAuthState - falls back to an initials avatar and the server-resolved - display_name. */ - var ghAvatar = roleData.user.indexOf(':') === -1 - ? 'https://github.com/' + roleData.user + '.png' - : ''; - showGHAuthState(true, roleData.user, ghAvatar, roleData.role, roleData.display_name); - return; - } - } catch (e) { window._hiveRoleFailed = true; } + const roleData = await hiveFetchRoleOnce(); + const applied = hiveApplyRoleData(roleData); + hiveRoleSettled(false); + if (applied) return; + } catch (e) { + if (e && e.retryable) hiveRetryRole(); else hiveRoleSettled(true); + } try { const resp = await fetch('/api/gh-user-auth/status'); const data = await resp.json(); diff --git a/src/pkg/dashboard/static-src/js/inbox.js b/src/pkg/dashboard/static-src/js/inbox.js index 274a765758..9864873c82 100644 --- a/src/pkg/dashboard/static-src/js/inbox.js +++ b/src/pkg/dashboard/static-src/js/inbox.js @@ -41,10 +41,10 @@ function inboxSourceIcon(src) { return INBOX_SOURCE_ICONS[src] || '📥'; } // Uploaded documents have no audio; uploaded audio/video does. function inboxItemHasAudio(it) { return !it || it.source !== 'upload' || it.upload_kind === 'audio' || it.upload_kind === 'video'; } - // The role is known once /api/role answered (or gave up: then it is - // undefined and every gate treats the viewer as read-only). Before that - // the Inbox renders a neutral loading state, never a guessed 'read'. - function inboxRoleKnown() { return window._hiveAuthKnown === true || window._hiveRole !== undefined; } + // The role is known once /api/role settled (answered, or retries gave up: + // then it is undefined and every gate treats the viewer as read-only). + // Before that the Inbox renders a neutral loading state, never a guessed 'read'. + function inboxRoleKnown() { return window._hiveRoleSettled === true || window._hiveRole !== undefined; } function inboxOnRoleReady() { if (_inboxCache) renderInbox(); } (window._hiveRoleListeners = window._hiveRoleListeners || []).push(inboxOnRoleReady); function inboxItemKey(it) { return it.source + '/' + it.id; } diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index bec7cdc23c..7f15ab9438 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -47436,10 +47436,10 @@ function inboxSourceIcon(src) { return INBOX_SOURCE_ICONS[src] || '📥'; } // Uploaded documents have no audio; uploaded audio/video does. function inboxItemHasAudio(it) { return !it || it.source !== 'upload' || it.upload_kind === 'audio' || it.upload_kind === 'video'; } - // The role is known once /api/role answered (or gave up: then it is - // undefined and every gate treats the viewer as read-only). Before that - // the Inbox renders a neutral loading state, never a guessed 'read'. - function inboxRoleKnown() { return window._hiveAuthKnown === true || window._hiveRole !== undefined; } + // The role is known once /api/role settled (answered, or retries gave up: + // then it is undefined and every gate treats the viewer as read-only). + // Before that the Inbox renders a neutral loading state, never a guessed 'read'. + function inboxRoleKnown() { return window._hiveRoleSettled === true || window._hiveRole !== undefined; } function inboxOnRoleReady() { if (_inboxCache) renderInbox(); } (window._hiveRoleListeners = window._hiveRoleListeners || []).push(inboxOnRoleReady); function inboxItemKey(it) { return it.source + '/' + it.id; } @@ -78220,17 +78220,10 @@

${model.editIndex === null ? 'Add Event trigger' : 'Edit Event trigger'}

${model.editIndex === null ? 'Add Event trigger' : 'Edit Event trigger'} 0) await new Promise(function(resolve) { setTimeout(resolve, HIVE_ROLE_RETRY_BASE_MS * Math.pow(2, attempt - 1)); }); - let resp; - try { - resp = await fetch('/api/role'); - if (resp.status >= HIVE_ROLE_SERVER_ERROR_STATUS) throw new Error('HTTP ' + resp.status); - } catch (e) { lastErr = e; continue; } - // An answer that is not retryable (4xx, an HTML 404 from a static - // host) is final: parse it once and let the caller fall back. - return await resp.json(); + /* One /api/role attempt. Network errors and 5xx are marked retryable; an + answer that is not (4xx, an HTML 404 from a static host) is final. */ + async function hiveFetchRoleOnce() { + let resp; + try { resp = await fetch('/api/role'); } catch (e) { e.retryable = true; throw e; } + if (resp.status >= HIVE_ROLE_SERVER_ERROR_STATUS) { + const err = new Error('HTTP ' + resp.status); + err.retryable = true; + throw err; } - throw lastErr; + return await resp.json(); } - async function checkGHAuth() { + /* The role is "settled" once /api/role answered or gave up. Sections that + gate on it (the Inbox) hold a neutral loading state until then and + subscribe via window._hiveRoleListeners; they are notified the first + time and whenever the role or the failed flag changes. This is separate + from _hiveAuthKnown so the tracker never waits on the retries below. */ + function hiveRoleSettled(failed) { + const sig = String(window._hiveRole || '') + '|' + (failed ? 'failed' : 'ok'); + const changed = !window._hiveRoleSettled || window._hiveRoleSig !== sig; + window._hiveRoleFailed = !!failed; + window._hiveRoleSettled = true; + window._hiveRoleSig = sig; + if (!changed) return; + (window._hiveRoleListeners || []).forEach(function(fn) { try { fn(); } catch (e) { console.error(e); } }); + } + + function hiveApplyRoleData(roleData) { + if (roleData.sentinel_label) window._hiveSentinelLabel = roleData.sentinel_label; + if (!roleData.user) return false; + if (roleData.automerge_label) window._hiveAutoMergeLabel = roleData.automerge_label; + /* OIDC identity keys ("ibmid:5500…") are not GitHub logins — don't + fabricate a github.com/.png avatar for them; showGHAuthState + falls back to an initials avatar and the server-resolved + display_name. */ + var ghAvatar = roleData.user.indexOf(':') === -1 + ? 'https://github.com/' + roleData.user + '.png' + : ''; + showGHAuthState(true, roleData.user, ghAvatar, roleData.role, roleData.display_name); + return true; + } + + /* Background retries after a retryable first failure, with exponential + backoff. If they all fail the viewer stays read-only and the Inbox says + so. */ + async function hiveRetryRole() { + if (window._hiveRoleRetrying) return; + window._hiveRoleRetrying = true; try { - const roleData = await hiveFetchRole(); - window._hiveRoleFailed = false; - if (roleData.sentinel_label) window._hiveSentinelLabel = roleData.sentinel_label; - if (roleData.user) { - if (roleData.automerge_label) window._hiveAutoMergeLabel = roleData.automerge_label; - /* OIDC identity keys ("ibmid:5500…") are not GitHub logins — don't - fabricate a github.com/.png avatar for them; showGHAuthState - falls back to an initials avatar and the server-resolved - display_name. */ - var ghAvatar = roleData.user.indexOf(':') === -1 - ? 'https://github.com/' + roleData.user + '.png' - : ''; - showGHAuthState(true, roleData.user, ghAvatar, roleData.role, roleData.display_name); - return; + for (let attempt = 1; attempt < HIVE_ROLE_FETCH_ATTEMPTS; attempt++) { + await new Promise(function(resolve) { setTimeout(resolve, HIVE_ROLE_RETRY_BASE_MS * Math.pow(2, attempt - 1)); }); + try { + const roleData = await hiveFetchRoleOnce(); + hiveApplyRoleData(roleData); + hiveRoleSettled(false); + return; + } catch (e) { if (!e.retryable) break; } } - } catch (e) { window._hiveRoleFailed = true; } + hiveRoleSettled(true); + } finally { window._hiveRoleRetrying = false; } + } + + async function checkGHAuth() { + try { + const roleData = await hiveFetchRoleOnce(); + const applied = hiveApplyRoleData(roleData); + hiveRoleSettled(false); + if (applied) return; + } catch (e) { + if (e && e.retryable) hiveRetryRole(); else hiveRoleSettled(true); + } try { const resp = await fetch('/api/gh-user-auth/status'); const data = await resp.json(); From 1acffb6e49dececb7a290d827c1935ceae6ce971 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Mon, 16 Nov 2026 13:06:46 -0500 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=90=9B=20test:=20sentinel=20pin=20fol?= =?UTF-8?q?lows=20the=20role=20gate=20into=20hiveApplyRoleData?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andy Anderson --- src/pkg/dashboard/sentinel_pill_11416_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pkg/dashboard/sentinel_pill_11416_test.go b/src/pkg/dashboard/sentinel_pill_11416_test.go index 4dca103ad1..ad351952e4 100644 --- a/src/pkg/dashboard/sentinel_pill_11416_test.go +++ b/src/pkg/dashboard/sentinel_pill_11416_test.go @@ -66,7 +66,7 @@ func TestSentinelFlaggedPRPillWiring(t *testing.T) { } // The role read must not depend on a signed-in user. roleRead := strings.Index(html, "if (roleData.sentinel_label)") - userGate := strings.Index(html, "if (roleData.user) {\n if (roleData.automerge_label)") + userGate := strings.Index(html, "if (!roleData.user) return false;") if roleRead < 0 || userGate < 0 || roleRead > userGate { t.Errorf("sentinel label must be read before the roleData.user gate") }