From 5e6735f09b3bc24b82c9f4a6b075b2bcbfffa689 Mon Sep 17 00:00:00 2001 From: Andy Anderson Date: Tue, 3 Nov 2026 13:52:03 -0500 Subject: [PATCH 1/4] =?UTF-8?q?=E2=9C=A8=20perf:=20progressively=20load=20?= =?UTF-8?q?the=20dashboard=20shell=20and=20live=20panels?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andy Anderson --- .../changed-progressive-dashboard-loading.md | 1 + .../dashboard/deferred_init_promise_test.go | 2 +- src/pkg/dashboard/progressive_loading.test.js | 284 ++++++++++++++ src/pkg/dashboard/progressive_loading_test.go | 16 + src/pkg/dashboard/server.go | 20 +- .../dashboard/static-src/js/agent-detail.js | 2 +- src/pkg/dashboard/static-src/js/approvals.js | 11 +- .../static-src/js/backends-tokens.js | 6 +- src/pkg/dashboard/static-src/js/boot.js | 6 +- src/pkg/dashboard/static-src/js/chat.js | 5 +- .../static-src/js/config-governor.js | 3 +- src/pkg/dashboard/static-src/js/cost.js | 3 +- .../static-src/js/history-sparklines.js | 16 +- .../dashboard/static-src/js/progressive.js | 159 ++++++++ src/pkg/dashboard/static-src/js/refresh.js | 55 +-- .../static-src/js/shimmy-handlers.js | 80 ++-- .../dashboard/static-src/js/sse-bootstrap.js | 4 +- src/pkg/dashboard/static-src/js/welcome.js | 3 +- src/pkg/dashboard/static-src/manifest.txt | 1 + src/pkg/dashboard/static/index.html | 353 ++++++++++++----- .../dashboard/webstatic/dashboard_script.go | 165 ++++++++ .../webstatic/dashboard_script_test.go | 368 ++++++++++++++++++ 22 files changed, 1348 insertions(+), 215 deletions(-) create mode 100644 changelog.d/changed-progressive-dashboard-loading.md create mode 100644 src/pkg/dashboard/progressive_loading.test.js create mode 100644 src/pkg/dashboard/progressive_loading_test.go create mode 100644 src/pkg/dashboard/static-src/js/progressive.js create mode 100644 src/pkg/dashboard/webstatic/dashboard_script.go create mode 100644 src/pkg/dashboard/webstatic/dashboard_script_test.go diff --git a/changelog.d/changed-progressive-dashboard-loading.md b/changelog.d/changed-progressive-dashboard-loading.md new file mode 100644 index 0000000000..f9bfd7866d --- /dev/null +++ b/changelog.d/changed-progressive-dashboard-loading.md @@ -0,0 +1 @@ +- Show the dashboard shell before downloading its cacheable application script, prioritize the selected and visible panels, limit background refresh concurrency, and progressively render the latest live data without waiting for every panel before restoring navigation. diff --git a/src/pkg/dashboard/deferred_init_promise_test.go b/src/pkg/dashboard/deferred_init_promise_test.go index 6a11a31e87..c69cb36985 100644 --- a/src/pkg/dashboard/deferred_init_promise_test.go +++ b/src/pkg/dashboard/deferred_init_promise_test.go @@ -177,7 +177,7 @@ func TestDeferredInitGuardsCoverEveryEntry(t *testing.T) { // Count TOP-LEVEL entries only: one per line, at the array's indentation. // Counting every `() =>` in the block would also count the nested arrows // inside each entry's own .then chains. - entryLine := regexp.MustCompile(`(?m)^\s+\(\)\s*=>`) + entryLine := regexp.MustCompile(`(?m)^\s+\['[^']+',\s*\(\)\s*=>`) entries := len(entryLine.FindAllString(block, -1)) matched := len(deferredEntryCall.FindAllString(block, -1)) if entries == 0 { diff --git a/src/pkg/dashboard/progressive_loading.test.js b/src/pkg/dashboard/progressive_loading.test.js new file mode 100644 index 0000000000..28c46ffc00 --- /dev/null +++ b/src/pkg/dashboard/progressive_loading.test.js @@ -0,0 +1,284 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); + +const source = fs.readFileSync(path.join(__dirname, 'static-src/js/progressive.js'), 'utf8'); +const html = fs.readFileSync(path.join(__dirname, 'static/index.html'), 'utf8'); +const flush = async () => { for (let i = 0; i < 12; i++) await Promise.resolve(); }; +const deferred = () => { + let resolve; + const promise = new Promise(done => { resolve = done; }); + return { promise, resolve }; +}; + +function environment({ hidden = false, idle = true } = {}) { + const turns = [], timers = [], errors = [], listeners = {}, intervals = new Map(); + let intervalID = 0; + const window = { location: { hash: '' }, innerHeight: 900 }; + if (idle) window.requestIdleCallback = fn => { turns.push(fn); }; + const sections = new Map(); + const document = { + hidden, + getElementById: id => sections.get(id), + addEventListener(type, fn) { (listeners[type] ||= []).push(fn); }, + }; + const statusGuard = html.match(/function dashboardStatusCanRender\(data\) \{[\s\S]*?\n \}/); + assert.ok(statusGuard); + const intervalSource = html.slice(html.indexOf(' function dashboardSetInterval('), html.indexOf(' let historyData =')); + assert.ok(intervalSource.includes('function dashboardRunInterval')); + const api = new Function('window', 'document', 'setTimeout', 'console', 'setInterval', 'clearInterval', + 'let _statusSeqFloor = 0, _lastStatusSeq = 0, _statusInstance = "";\n' + + 'const DASHBOARD_INTERVALS = {};\n' + statusGuard[0] + '\n' + source + '\n' + intervalSource + + '\nreturn {dashboardScheduleWork, dashboardRenderSection, dashboardProgressiveWake, dashboardSetInterval,' + + 'setStatus(floor, latest, instance) {_statusSeqFloor=floor; _lastStatusSeq=latest; _statusInstance=instance;}};' + )(window, document, (fn, ms) => { timers.push({ fn, ms }); }, { + error(...args) { errors.push(args); }, + }, (fn, ms) => { const id = ++intervalID; intervals.set(id, { fn, ms }); return id; }, + id => intervals.delete(id)); + return { + api, window, document, sections, errors, turns, timers, intervals, + start() { timers.shift().fn(); }, + async turn() { assert.ok(turns.length, 'an idle turn must be scheduled'); turns.shift()(); await flush(); }, + visibility(value) { + document.hidden = value; + for (const fn of listeners.visibilitychange || []) fn(); + }, + }; +} + +test('the generated dashboard carries the progressive scheduler', () => { + assert.ok(html.includes(source)); + assert.ok(!html.includes("document.body.style.opacity='0'")); + const afterPaint = html.slice(html.indexOf('function _afterPaint('), html.indexOf('const NAVBAR_CLOCK_TICK_MS')); + assert.ok(afterPaint.includes('requestAnimationFrame')); + assert.ok(!afterPaint.includes('_deferredLoadPromise'), 'navigation cannot await every background API'); +}); + +test('background work yields, stays bounded through decoding, and coalesces repeated ticks', async () => { + const e = environment(); + const gates = [deferred(), deferred(), deferred()]; + const started = []; + const promises = gates.map((gate, index) => + e.api.dashboardScheduleWork('job-' + index, async () => { + started.push(index); + await gate.promise; + return index; + })); + assert.equal(e.api.dashboardScheduleWork('job-0', () => assert.fail('duplicate flight')), promises[0]); + assert.equal(e.turns.length, 0, 'critical status/config get a head start'); + e.start(); + await e.turn(); + await e.turn(); + assert.deepEqual(started, [0, 1]); + assert.equal(e.turns.length, 0, 'no more than two unfinished jobs'); + gates[0].resolve(); + await flush(); + await e.turn(); + assert.deepEqual(started, [0, 1, 2]); + gates[1].resolve(); + gates[2].resolve(); + assert.deepEqual(await Promise.all(promises), [0, 1, 2]); + let refreshed = false; + const again = e.api.dashboardScheduleWork('job-0', () => { refreshed = true; }); + await e.turn(); + await again; + assert.equal(refreshed, true, 'settled jobs can refresh immediately after awaiting them'); +}); + +test('the selected section wins before visible panels, then background panels', async () => { + const e = environment(); + const order = []; + e.window.location.hash = '#section-activity-monitor-section'; + e.sections.set('overview-section', { + getClientRects: () => [{}], + getBoundingClientRect: () => ({ top: 10, bottom: 300 }), + }); + const a = e.api.dashboardScheduleWork('knowledge-stats', () => order.push('background')); + const b = e.api.dashboardScheduleWork('overview-kpi-history', () => order.push('visible')); + const c = e.api.dashboardScheduleWork('stage-tracker', () => order.push('selected')); + e.start(); + while (e.turns.length) await e.turn(); + await Promise.all([a, b, c]); + assert.deepEqual(order, ['selected', 'visible', 'background']); +}); + +test('priority is recomputed after navigation while other work is in flight', async () => { + const e = environment(); + const gate = deferred(), order = []; + e.api.dashboardScheduleWork('first', () => gate.promise); + e.api.dashboardScheduleWork('second', () => gate.promise); + e.api.dashboardScheduleWork('knowledge-stats', () => order.push('knowledge')); + e.api.dashboardScheduleWork('stage-tracker', () => order.push('tracker')); + e.start(); + await e.turn(); + await e.turn(); + e.window.location.hash = '#section-activity-monitor-section'; + gate.resolve(); + await flush(); + await e.turn(); + assert.equal(order[0], 'tracker'); + while (e.turns.length) await e.turn(); +}); + +test('hidden tabs retain jobs without starting them and resume once', async () => { + const e = environment({ hidden: true }); + let runs = 0; + const pending = e.api.dashboardScheduleWork('cost', () => runs++); + e.start(); + assert.equal(e.turns.length, 0); + e.visibility(false); + e.visibility(true); + await e.turn(); + assert.equal(runs, 0, 'a tab can become hidden after scheduling'); + e.visibility(false); + await e.turn(); + await pending; + assert.equal(runs, 1); +}); + +test('synchronous failures reject, release their slot, and do not strand other panels', async () => { + const e = environment(); + const rejected = assert.rejects(e.api.dashboardScheduleWork('bad', () => { + throw new Error('broken panel'); + }), /broken panel/); + let ran = false; + const good = e.api.dashboardScheduleWork('good', () => { ran = true; }); + e.start(); + while (e.turns.length) await e.turn(); + await rejected; + await good; + assert.equal(ran, true); +}); + +test('visibility resume and overlapping poll ticks use the same bounded flights', async () => { + const e = environment(), gate = deferred(), started = []; + for (const key of ['cost', 'git-version', 'stage-tracker']) { + e.api.dashboardSetInterval(key, async () => { + started.push(key); + await gate.promise; + }, 1000); + } + let watchdog = 0; + e.api.dashboardSetInterval('sse-watchdog', () => watchdog++, 1000); + e.visibility(true); + assert.equal(e.intervals.size, 0); + e.visibility(false); + assert.equal(watchdog, 1, 'liveness must not wait behind background panels'); + e.start(); + await e.turn(); + await e.turn(); + assert.equal(started.length, 2); + for (const timer of e.intervals.values()) timer.fn(); + await flush(); + assert.equal(started.length, 2, 'new ticks cannot overlap existing panel requests'); + gate.resolve(); + await flush(); + while (e.turns.length) await e.turn(); + assert.equal(started.length, 3); + assert.equal(new Set(started).size, 3); +}); + +test('rendering coalesces snapshots and paints one section per idle turn', async () => { + const e = environment(), rendered = []; + e.api.dashboardRenderSection('tokens', ['token-panel'], () => rendered.push('stale')); + e.api.dashboardRenderSection('tokens', ['token-panel'], () => rendered.push('latest')); + e.api.dashboardRenderSection('tracker', ['activity-monitor-section'], () => rendered.push('tracker')); + e.window.location.hash = '#section-activity-monitor-section'; + await e.turn(); + assert.deepEqual(rendered, ['tracker']); + await e.turn(); + assert.deepEqual(rendered, ['tracker', 'latest']); +}); + +test('hidden render snapshots stay bounded and resume with the newest state', async () => { + const e = environment({ hidden: true }); + let rendered; + for (let i = 0; i < 100; i++) { + e.api.dashboardRenderSection('agents', ['agents-section'], () => { rendered = i; }); + } + assert.equal(e.turns.length, 0); + e.visibility(false); + await e.turn(); + assert.equal(rendered, 99); + assert.equal(e.turns.length, 0); +}); + +test('a broken renderer is reported without blocking another section', async () => { + const e = environment(); + let ran = false; + e.api.dashboardRenderSection('bad', [], () => { throw new Error('bad render'); }); + e.api.dashboardRenderSection('good', [], () => { ran = true; }); + await e.turn(); + assert.equal(e.errors.length, 1); + await e.turn(); + assert.equal(ran, true); +}); + +test('queued snapshots recheck mutation floors, ordering, and server instance at execution', async () => { + const e = environment(), rendered = []; + e.api.dashboardRenderSection('old', [], () => rendered.push('old'), { statusSeq: 4, statusInstance: 'a' }); + e.api.setStatus(5, 5, 'a'); + await e.turn(); + assert.deepEqual(rendered, [], 'an optimistic mutation cannot be reverted by queued old data'); + e.api.dashboardRenderSection('old-instance', [], () => rendered.push('old-instance'), { statusSeq: 100, statusInstance: 'a' }); + e.api.setStatus(0, 1, 'b'); + await e.turn(); + assert.deepEqual(rendered, [], 'a restarted server invalidates queued snapshots from its predecessor'); + e.api.dashboardRenderSection('older', [], () => rendered.push('older'), { statusSeq: 1, statusInstance: 'b' }); + e.api.setStatus(0, 2, 'b'); + await e.turn(); + assert.deepEqual(rendered, []); + e.api.dashboardRenderSection('fresh', [], () => rendered.push('fresh'), { statusSeq: 2, statusInstance: 'b' }); + await e.turn(); + assert.deepEqual(rendered, ['fresh']); + e.api.dashboardRenderSection('legacy', [], () => rendered.push('legacy'), {}); + await e.turn(); + assert.deepEqual(rendered, ['fresh', 'legacy']); +}); + +test('a queued worker redraw survives newer status snapshots with unchanged workers', async () => { + const e = environment(); + const redraw = html.match(/dashboardRenderSection\('tracker-workers',[^\n]+/); + assert.ok(redraw); + let latestWorkers = ['working'], rendered; + const snapshot = { statusSeq: 1, statusInstance: 'a' }; + new Function('dashboardRenderSection', 'renderStageTracker', 'data', redraw[0])( + e.api.dashboardRenderSection, () => { rendered = latestWorkers; }, snapshot, + ); + e.api.setStatus(0, 2, 'a'); + latestWorkers = ['working', 'newest']; + await e.turn(); + assert.deepEqual(rendered, ['working', 'newest']); +}); + +test('browsers without requestIdleCallback still yield with bounded work', async () => { + const e = environment({ idle: false }); + let ran = false; + const result = e.api.dashboardScheduleWork('cost', () => { ran = true; }); + e.start(); + assert.equal(ran, false); + assert.equal(e.timers.length, 1); + assert.ok(e.timers[0].ms > 0); + e.timers.shift().fn(); + await result; + assert.equal(ran, true); +}); + +test('backend discovery retains its slot until each model request is decoded', async () => { + const discovery = html.match(/async function refreshBackendDiscovery\(force\) \{[\s\S]*?\n \}/); + assert.ok(discovery); + const calls = [], gate = deferred(); + const run = new Function('INFERENCE_BACKENDS', 'fetchInferenceModels', 'fetchConfiguredGateways', + discovery[0] + '\nreturn refreshBackendDiscovery(true);' + )(['one', 'two'], async (backend, force) => { + assert.equal(force, true); + calls.push(backend); + if (backend === 'one') await gate.promise; + }, async () => { calls.push('gateways'); }); + await flush(); + assert.deepEqual(calls, ['one']); + gate.resolve(); + await run; + assert.deepEqual(calls, ['one', 'two', 'gateways']); +}); diff --git a/src/pkg/dashboard/progressive_loading_test.go b/src/pkg/dashboard/progressive_loading_test.go new file mode 100644 index 0000000000..713471b8da --- /dev/null +++ b/src/pkg/dashboard/progressive_loading_test.go @@ -0,0 +1,16 @@ +package dashboard + +import ( + "os/exec" + "testing" +) + +func TestDashboardProgressiveLoading(t *testing.T) { + node, err := exec.LookPath("node") + if err != nil { + t.Skip("node not on PATH — progressive dashboard behavior not executed") + } + if out, err := exec.Command(node, "--test", "progressive_loading.test.js").CombinedOutput(); err != nil { + t.Fatalf("progressive dashboard behavior failed: %v\n%s", err, out) + } +} diff --git a/src/pkg/dashboard/server.go b/src/pkg/dashboard/server.go index 1389c1e6e6..3da0702301 100644 --- a/src/pkg/dashboard/server.go +++ b/src/pkg/dashboard/server.go @@ -1686,13 +1686,12 @@ func (s *Server) Start() error { if err != nil { return fmt.Errorf("loading embedded static files: %w", err) } - // The SPA document gets a dedicated handler with a strong ETag and one-time - // gzip cache (see pkg/dashboard/webstatic): http.FileServer would serve the - // ~1.3 MB inline document uncompressed with no cache validators (embed.FS - // has a zero ModTime, so not even Last-Modified), forcing a full re-download - // on every visit. "/{$}" matches the root path exactly; every other static - // path falls through to the revalidating file server below. + // The SPA shell and content-addressed main script get dedicated ETag/gzip + // handlers. The generated monolith remains embedded for source consumers; + // extraction happens after all startup injections. "/{$}" matches the root + // exactly; other static paths use the revalidating file server below. var idx *webstatic.IndexDocument + var mainScript *webstatic.DashboardScript if rawIndex, err := fs.ReadFile(staticContent, "index.html"); err == nil { // Strings are baked in ONCE here, unlike custom.css which is read per // request: the document carries a strong ETag and one immutable gzip @@ -1703,6 +1702,12 @@ func (s *Server) Start() error { // The kiosk call-to-action badge (#16236) is baked in the same way: // its QR code and copy are static per process. branded = kioskcta.Inject(branded, s.kioskCTAOptions()) + branded, mainScript = webstatic.ExternalizeDashboardScript(branded) + if mainScript != nil { + s.mux.Handle("GET "+mainScript.Path(), mainScript) + } else { + s.logger.Warn("dashboard main script extraction unavailable; serving original inline dashboard") + } idx = webstatic.NewIndexDocument(branded) // Hand the FINAL served bytes to the CSP layer explicitly, rather than // having the document constructor reach out and set global state: @@ -1750,6 +1755,9 @@ func (s *Server) Start() error { // answer even when a saturated node is slow to build the static cache. go idx.Precompress() } + if mainScript != nil { + go mainScript.Precompress() + } // Viewer-independent cost sampling (#13483); the ring's throttle is the // same interval, so this is exactly one snapshot per period. costTicker := time.NewTicker(costHistoryMinIntervalMs * time.Millisecond) diff --git a/src/pkg/dashboard/static-src/js/agent-detail.js b/src/pkg/dashboard/static-src/js/agent-detail.js index b7afa31b7c..97c1423354 100644 --- a/src/pkg/dashboard/static-src/js/agent-detail.js +++ b/src/pkg/dashboard/static-src/js/agent-detail.js @@ -134,6 +134,7 @@ document.querySelectorAll('.oc-nav-item').forEach(i => i.classList.remove('active')); const active = document.querySelector(`.oc-nav-item[data-agent-nav="${(window.CSS && CSS.escape) ? CSS.escape(name) : String(name).replace(/["\\]/g, '\\$&')}"]`); if (active) active.classList.add('active'); + if (typeof dashboardProgressiveWake === 'function') dashboardProgressiveWake(); navPeekRestore('agents-section', { instant: true }); const expanded = navPeekExpand('agents-section'); if (!expanded && isSectionCollapsed('agents-section')) toggleSection('agents-section', { collapsed: false, persist: false, navPeek: true }); @@ -934,4 +935,3 @@ // Agent descriptions now come from the JSON status payload (agent.description field). // This empty map serves as a fallback for agents without a description in config. const AGENT_DESCRIPTIONS = {}; - diff --git a/src/pkg/dashboard/static-src/js/approvals.js b/src/pkg/dashboard/static-src/js/approvals.js index ddaed7d15f..349c5065aa 100644 --- a/src/pkg/dashboard/static-src/js/approvals.js +++ b/src/pkg/dashboard/static-src/js/approvals.js @@ -153,7 +153,13 @@ // Prime both panels and poll them on a modest independent interval. Do not // block initial paint: these run after the main poll is already wired. - function v4RefreshPanels() { fetchHiveAdvice(); fetchACMMReco(); fetchLifecycle(); fetchPRThroughput(); fetchApprovals(); } + function v4RefreshPanels() { + [['hive-advice', fetchHiveAdvice], ['acmm-reco', fetchACMMReco], + ['lifecycle', fetchLifecycle], ['pr-throughput', fetchPRThroughput], + ['approvals', fetchApprovals]].forEach(function([key, fn]) { + dashboardScheduleWork(key, fn).catch(console.error); + }); + } v4RefreshPanels(); dashboardSetInterval('v4-panels', v4RefreshPanels, V4_PANELS_POLL_MS); @@ -193,7 +199,7 @@ } } catch (_) { /* dashboard unreachable — different problem */ } } - checkGhAuth(); + dashboardScheduleWork('gh-auth-banner', checkGhAuth).catch(console.error); dashboardSetInterval('gh-auth-banner', checkGhAuth, GH_AUTH_POLL_MS); function renderGhRateLimits(ghRateLimits) { @@ -305,4 +311,3 @@ function setIfChanged(el, html) { if (el.innerHTML !== html) el.innerHTML = html; } - diff --git a/src/pkg/dashboard/static-src/js/backends-tokens.js b/src/pkg/dashboard/static-src/js/backends-tokens.js index 73d56eb3a9..79ae0a1252 100644 --- a/src/pkg/dashboard/static-src/js/backends-tokens.js +++ b/src/pkg/dashboard/static-src/js/backends-tokens.js @@ -392,11 +392,11 @@ return INFERENCE_MODELS[backend] || []; } } - function refreshBackendDiscovery(force) { - INFERENCE_BACKENDS.forEach(b => fetchInferenceModels(b, force)); + async function refreshBackendDiscovery(force) { + for (const backend of INFERENCE_BACKENDS) await fetchInferenceModels(backend, force); // Load configured gateway names so the backend/method pickers list them // (e.g. openrouter) alongside KNOWN_BACKENDS after the first paint. - fetchConfiguredGateways(); + await fetchConfiguredGateways(); } const CLI_MODEL_CACHE_TTL_MS = 30000; diff --git a/src/pkg/dashboard/static-src/js/boot.js b/src/pkg/dashboard/static-src/js/boot.js index f7e024e434..c73bd07343 100644 --- a/src/pkg/dashboard/static-src/js/boot.js +++ b/src/pkg/dashboard/static-src/js/boot.js @@ -1,4 +1,4 @@ -// Apply layout immediately and hide content until first data render. +// Apply layout immediately; keep the shell visible while live data loads. // NOTE: only body classes are set here — the .layout-toggle buttons do NOT // exist in the DOM yet at this point, so any label write here is a no-op. // applyLayout() (defined in the main script) is the single source of truth @@ -84,9 +84,6 @@ if(window.HIVE_KIOSK){ var kt=null; try { kt=new URL(window.location.href).searchParams.get('theme'); } catch(e) {} m=(kt==='light')?'light':'dark'; } if(!m)m='light'; if(m==='light'){document.body.classList.add('light-mode')} - document.body.style.opacity='0'; - document.body.style.transition='opacity 0.15s ease-in'; - setTimeout(function(){ document.body.style.opacity='1'; }, 3000); document.addEventListener('DOMContentLoaded', function(){ if (typeof applyLayout === 'function' && typeof getLayout === 'function') { applyLayout(getLayout()); @@ -171,4 +168,3 @@ document.addEventListener('DOMContentLoaded', renderStyleState); } })(); - diff --git a/src/pkg/dashboard/static-src/js/chat.js b/src/pkg/dashboard/static-src/js/chat.js index 7f9c37352b..04a20dd0b7 100644 --- a/src/pkg/dashboard/static-src/js/chat.js +++ b/src/pkg/dashboard/static-src/js/chat.js @@ -468,8 +468,8 @@ chatInput.value = localStorage.getItem(CHAT_DRAFT_KEY) || ''; renderChatCheatsheet(); renderChatSuggestions(); - detectChatCapabilities(); - refreshChatPresence(); + dashboardScheduleWork('chat-capabilities', detectChatCapabilities).catch(console.error); + dashboardScheduleWork('chat-presence', refreshChatPresence).catch(console.error); dashboardSetInterval('chat-presence', refreshChatPresence, 30000); chatFab.onclick = () => { @@ -595,4 +595,3 @@ handle.classList.remove('dragging'); }); })(); - diff --git a/src/pkg/dashboard/static-src/js/config-governor.js b/src/pkg/dashboard/static-src/js/config-governor.js index 7cca761953..819710967f 100644 --- a/src/pkg/dashboard/static-src/js/config-governor.js +++ b/src/pkg/dashboard/static-src/js/config-governor.js @@ -1590,7 +1590,7 @@ } return _statSources; } - ensureStatSources(); + dashboardScheduleWork('stat-sources', ensureStatSources).catch(console.error); /* statSourcesForDialog returns the source catalogue for the agent the config dialog is showing. The agent config response carries a catalogue @@ -1802,4 +1802,3 @@ — fixed progression; the last step is the cap. `; } - diff --git a/src/pkg/dashboard/static-src/js/cost.js b/src/pkg/dashboard/static-src/js/cost.js index 3c46fe37e5..c8e33733f3 100644 --- a/src/pkg/dashboard/static-src/js/cost.js +++ b/src/pkg/dashboard/static-src/js/cost.js @@ -707,6 +707,5 @@ renderCost(cost); } catch (e) { /* transient — leave last render in place */ } } - fetchCost(); + dashboardScheduleWork('cost', fetchCost).catch(console.error); dashboardSetInterval('cost', fetchCost, COST_POLL_MS); - diff --git a/src/pkg/dashboard/static-src/js/history-sparklines.js b/src/pkg/dashboard/static-src/js/history-sparklines.js index a57b031e7f..7e958459b9 100644 --- a/src/pkg/dashboard/static-src/js/history-sparklines.js +++ b/src/pkg/dashboard/static-src/js/history-sparklines.js @@ -20,13 +20,22 @@ } function dashboardSetInterval(key, fn, ms) { dashboardClearInterval(key); - const entry = { fn, ms, timer: null }; + const entry = { key, fn, ms, timer: null }; DASHBOARD_INTERVALS[key] = entry; if (!document.hidden) dashboardStartInterval(entry); return entry; } function dashboardStartInterval(entry) { - entry.timer = setInterval(entry.fn, entry.ms); + entry.timer = setInterval(function() { + try { Promise.resolve(dashboardRunInterval(entry)).catch(console.error); } catch (err) { console.error(err); } + }, entry.ms); + } + function dashboardRunInterval(entry) { + if (typeof dashboardScheduleWork === 'function' && + Object.prototype.hasOwnProperty.call(DASHBOARD_TASK_SECTIONS, entry.key)) { + return dashboardScheduleWork(entry.key, entry.fn); + } + return entry.fn(); } function dashboardClearInterval(key) { if (!DASHBOARD_INTERVALS[key]) return; @@ -43,7 +52,7 @@ // A refresh failure must not prevent the other panels from resuming. // Snapshot iteration also permits a callback to clear/replace itself. if (DASHBOARD_INTERVALS[key] === entry) { - try { Promise.resolve(entry.fn()).catch(console.error); } catch (err) { console.error(err); } + try { Promise.resolve(dashboardRunInterval(entry)).catch(console.error); } catch (err) { console.error(err); } } } }); @@ -493,4 +502,3 @@ capHistory(historyData); } } - diff --git a/src/pkg/dashboard/static-src/js/progressive.js b/src/pkg/dashboard/static-src/js/progressive.js new file mode 100644 index 0000000000..e2e25eb330 --- /dev/null +++ b/src/pkg/dashboard/static-src/js/progressive.js @@ -0,0 +1,159 @@ + const DASHBOARD_BACKGROUND_CONCURRENCY = 2; + const DASHBOARD_IDLE_TIMEOUT_MS = 1000; + const DASHBOARD_YIELD_MS = 32; + const DASHBOARD_BACKGROUND_DELAY_MS = 350; + const DASHBOARD_TASK_SECTIONS = Object.freeze({ + 'backend-discovery': ['debug-section'], + 'stat-sources': ['agents-section'], + 'cost': ['cost-panel'], + 'hive-advice': ['advisory-section'], + 'acmm-reco': ['advisory-section'], + 'lifecycle': ['advisory-section'], + 'pr-throughput': ['pr-throughput-section'], + 'approvals': ['approvals-section'], + 'fact-history': ['knowledge-section'], + 'overview-kpi-history': ['overview-section'], + 'history': ['overview-section', 'repos-section'], + 'budget-ignore': ['repos-section'], + 'budget-history': ['governor'], + 'nous-status': ['nous-section'], + 'public-knowledge': ['knowledge-section'], + 'knowledge-stats': ['knowledge-section'], + 'audit-log': ['audit-section'], + 'review-queue': ['review-queue-section'], + 'review-pipeline': ['review-queue-section'], + 'stage-tracker': ['activity-monitor-section'], + 'contributors': ['contributors-section'], + 'leaderboard': ['contributors-section'], + 'git-version': ['debug-section'], + 'acmm-evaluation': ['acmm-eval-section'], + 'version-check': ['debug-section'], + 'version-reload-check': ['debug-section'], + 'weekly-trends': ['overview-section'], + 'timeline': ['governor'], + 'agent-logs': ['logs-section'], + 'debug-section': ['debug-section'], + 'chat-capabilities': [], + 'chat-presence': [], + 'welcome-gh-auth': [], + 'welcome-claude-auth': [], + 'gh-auth-banner': [], + }); + const _dashboardBackgroundJobs = new Map(); + const _dashboardRenderJobs = new Map(); + let _dashboardBackgroundRunning = 0; + let _dashboardBackgroundScheduled = false; + let _dashboardRenderScheduled = false; + let _dashboardBackgroundReady = false; + + function dashboardWorkPriority(sections) { + let priority = 2; + for (const id of sections || []) { + if (window.location.hash === '#section-' + id) return 0; + const el = document.getElementById(id); + if (!el || !el.getClientRects().length) continue; + const rect = el.getBoundingClientRect(); + if (rect.bottom >= 0 && rect.top <= window.innerHeight) priority = 1; + } + return priority; + } + function dashboardNextWork(jobs) { + let next; + let priority = Infinity; + for (const job of jobs.values()) { + if (job.running) continue; + const rank = dashboardWorkPriority(job.sections); + if (rank < priority) { next = job; priority = rank; } + } + return next; + } + function dashboardIdle(callback) { + if (typeof window.requestIdleCallback === 'function') { + window.requestIdleCallback(callback, { timeout: DASHBOARD_IDLE_TIMEOUT_MS }); + } else { + setTimeout(callback, DASHBOARD_YIELD_MS); + } + } + function dashboardWakeBackground() { + if (!_dashboardBackgroundReady || document.hidden || _dashboardBackgroundScheduled || + _dashboardBackgroundRunning >= DASHBOARD_BACKGROUND_CONCURRENCY || + !dashboardNextWork(_dashboardBackgroundJobs)) return; + _dashboardBackgroundScheduled = true; + dashboardIdle(function() { + _dashboardBackgroundScheduled = false; + if (document.hidden) return; + const job = dashboardNextWork(_dashboardBackgroundJobs); + if (!job) return; + job.running = true; + _dashboardBackgroundRunning++; + function finished() { + _dashboardBackgroundJobs.delete(job.key); + _dashboardBackgroundRunning--; + dashboardWakeBackground(); + } + Promise.resolve().then(job.fn).then(function(value) { + finished(); job.resolve(value); + }, function(err) { + finished(); job.reject(err); + }); + dashboardWakeBackground(); + }); + } + // Keep a slot through JSON decoding and rendering, not just response headers. + // Repeated timer ticks share the same flight rather than adding stale work. + function dashboardScheduleWork(key, fn) { + const existing = _dashboardBackgroundJobs.get(key); + if (existing) return existing.promise; + let resolve, reject; + const promise = new Promise(function(ok, fail) { resolve = ok; reject = fail; }); + _dashboardBackgroundJobs.set(key, { + key, fn, promise, resolve, reject, running: false, + sections: DASHBOARD_TASK_SECTIONS[key] || [], + }); + dashboardWakeBackground(); + return promise; + } + function dashboardWakeRender() { + if (document.hidden || _dashboardRenderScheduled || !_dashboardRenderJobs.size) return; + _dashboardRenderScheduled = true; + dashboardIdle(function() { + _dashboardRenderScheduled = false; + if (document.hidden) return; + const job = dashboardNextWork(_dashboardRenderJobs); + if (!job) return; + _dashboardRenderJobs.delete(job.key); + if (job.snapshot && !dashboardStatusCanRender(job.snapshot)) { + dashboardWakeRender(); + return; + } + const anchor = typeof captureScrollAnchor === 'function' ? captureScrollAnchor() : null; + try { + job.fn(); + job.sections.forEach(function(id) { + if (typeof applySectionCollapse === 'function') applySectionCollapse(id); + }); + if (typeof refreshDashboardSectionSummaries === 'function') refreshDashboardSectionSummaries(); + } catch (err) { + console.error('dashboard render ' + job.key + ' failed', err); + } finally { + if (anchor && typeof restoreScrollAnchor === 'function' && + (typeof _hashScrollPending === 'undefined' || !_hashScrollPending)) restoreScrollAnchor(anchor); + dashboardWakeRender(); + } + }); + } + // Latest snapshot wins. Paint one section per idle turn so input and + // navigation can run between large cards, even when SSE arrives in a burst. + function dashboardRenderSection(key, sections, fn, snapshot) { + _dashboardRenderJobs.set(key, { key, sections, fn, snapshot }); + dashboardWakeRender(); + } + function dashboardProgressiveWake() { + dashboardWakeBackground(); + dashboardWakeRender(); + } + document.addEventListener('visibilitychange', dashboardProgressiveWake); + setTimeout(function() { + _dashboardBackgroundReady = true; + dashboardWakeBackground(); + }, DASHBOARD_BACKGROUND_DELAY_MS); diff --git a/src/pkg/dashboard/static-src/js/refresh.js b/src/pkg/dashboard/static-src/js/refresh.js index 11193a0598..e9b865ac63 100644 --- a/src/pkg/dashboard/static-src/js/refresh.js +++ b/src/pkg/dashboard/static-src/js/refresh.js @@ -140,27 +140,22 @@ const n = Number(minSeq); if (Number.isFinite(n) && n > _statusSeqFloor) _statusSeqFloor = n; } + // A snapshot accepted before a mutation can become stale while queued. + function dashboardStatusCanRender(data) { + return data.statusSeq == null || + (data.statusSeq >= _statusSeqFloor && data.statusSeq >= _lastStatusSeq && + (!data.statusInstance || !_statusInstance || data.statusInstance === _statusInstance)); + } var SCROLL_SETTLE_DELAY_MS = 500; var _deferredLoadPromise = null; function _afterPaint(callback) { _hashScrollPending = true; - if (_deferredLoadPromise) { - _deferredLoadPromise.then(function() { - requestAnimationFrame(function() { - requestAnimationFrame(function() { - callback(); - setTimeout(function() { _hashScrollPending = false; }, 200); - }); - }); - }); - } else { + requestAnimationFrame(function() { requestAnimationFrame(function() { - requestAnimationFrame(function() { - setTimeout(callback, SCROLL_SETTLE_DELAY_MS); - setTimeout(function() { _hashScrollPending = false; }, 500); - }); + setTimeout(callback, SCROLL_SETTLE_DELAY_MS); + setTimeout(function() { _hashScrollPending = false; }, SCROLL_SETTLE_DELAY_MS); }); - } + }); } const NAVBAR_CLOCK_TICK_MS = 1000; @@ -503,7 +498,9 @@ window._lastStatus = data; // The tracker's hop cue reads which agent is on which item from this // payload; redraw the rows when that set changes (no extra fetch). - if (typeof stageTrackerWorkers === 'function' && JSON.stringify(stageTrackerWorkers(data)) !== prevWorkers && typeof renderStageTracker === 'function') renderStageTracker(); + if (typeof stageTrackerWorkers === 'function' && JSON.stringify(stageTrackerWorkers(data)) !== prevWorkers && typeof renderStageTracker === 'function') { + dashboardRenderSection('tracker-workers', ['activity-monitor-section'], () => renderStageTracker()); + } applyDashboardFeatureVisibility(); if (typeof refreshThroughputSLA === 'function') refreshThroughputSLA(); if (window._acmmOverride) { @@ -542,25 +539,29 @@ renderSystemAlerts(data.systemAlerts || []); if (!_dropdownOpen && !_configModalOpen) { applyPinOverrides(data.agents || []); - renderAgents(data.agents || []); - renderGovernor(data.governor || {}, data.cadenceMatrix || [], data); + dashboardRenderSection('agents', ['agents-section', 'agent-activity-section'], () => { + if (!_dropdownOpen && !_configModalOpen) renderAgents(data.agents || []); + }, data); + dashboardRenderSection('governor', ['governor'], () => { + if (!_dropdownOpen && !_configModalOpen) renderGovernor(data.governor || {}, data.cadenceMatrix || [], data); + }, data); // Keep the focused agent detail in sync with fresh payloads — its // fast path only rebuilds when the config fingerprint changes. if (_ocSelectedAgent) ocRenderAgentDetail(); } - renderTokens(data.tokens || {}); - renderAdvisoryDigest(data.advisoryDigest || null); - renderRepos(data.repos || []); - renderSwarm(data.swarm || {}); + dashboardRenderSection('tokens', ['token-panel'], () => renderTokens(data.tokens || {}), data); + dashboardRenderSection('advisory', ['advisory-section'], () => renderAdvisoryDigest(data.advisoryDigest || null), data); + dashboardRenderSection('repos', ['repos-section', 'overview-section'], () => renderRepos(data.repos || []), data); + dashboardRenderSection('swarm', ['repos-section'], () => renderSwarm(data.swarm || {}), data); var reposBadge = document.getElementById('repos-sidebar-badge'); if (reposBadge) setSidebarCountBadge(reposBadge, (data.repos || []).length, (data.repos || []).length + ' repositories'); renderPlatform(data.platform || null); - renderHiveAdvice(data.hiveAdvice || null); - renderFleetReport(data.fleetReport || null); - renderSystemGauges(data.systemResources || null); + dashboardRenderSection('hive-advice', ['advisory-section'], () => renderHiveAdvice(data.hiveAdvice || null), data); + dashboardRenderSection('fleet-report', ['advisory-section'], () => renderFleetReport(data.fleetReport || null), data); + dashboardRenderSection('system-gauges', ['debug-section'], () => renderSystemGauges(data.systemResources || null), data); ocUpdateSidebarAgents(); - renderDebugSection(); - renderFAQ(); + dashboardRenderSection('debug', ['debug-section'], () => renderDebugSection(), data); + dashboardRenderSection('faq', ['faq-section'], () => renderFAQ(), data); initializeDashboardSectionCards(); // Restore persisted collapsed state for all collapsible sections const COLLAPSIBLE_SECTION_IDS = [ diff --git a/src/pkg/dashboard/static-src/js/shimmy-handlers.js b/src/pkg/dashboard/static-src/js/shimmy-handlers.js index 638292ec07..718a1bf568 100644 --- a/src/pkg/dashboard/static-src/js/shimmy-handlers.js +++ b/src/pkg/dashboard/static-src/js/shimmy-handlers.js @@ -29,7 +29,7 @@ } triggerAttentionShimmy(); - checkClaudeAuth(); + dashboardScheduleWork('welcome-claude-auth', checkClaudeAuth).catch(console.error); var CLAUDE_AUTH_CHECK_INTERVAL_MS = 60000; dashboardSetInterval('welcome-claude-auth', checkClaudeAuth, CLAUDE_AUTH_CHECK_INTERVAL_MS); @@ -87,59 +87,38 @@ sendPresencePing(); })(); - // Deferred initialization — non-critical fetches staggered after first paint. - // Each entry returns a promise so anchor scrolling can wait for layout to settle. - const DEFERRED_INIT_DELAY_MS = 1500; - const DEFERRED_STAGGER_MS = 200; + // Visible sections win; slow background panels cannot delay navigation. + const DEFERRED_INIT_DELAY_MS = DASHBOARD_BACKGROUND_DELAY_MS; _deferredLoadPromise = new Promise(function(resolveAll) { setTimeout(() => { const deferred = [ - () => Promise.resolve(refreshBackendDiscovery()).then(() => fetchBackendsConfig()), - () => fetchFactHistory(), - () => fetchOverviewKPIHistory().then(() => { dashboardSetInterval('overview-kpi-history', fetchOverviewKPIHistory, HISTORY_REFRESH_MS); }), - () => fetchTrendHistory().then(() => fetchHistory()).then(() => { dashboardSetInterval('history', fetchHistory, HISTORY_REFRESH_MS); }), - () => fetch('/api/budget-ignore').then(r => r.json()).then(d => { budgetIgnored = d.ignored; }).catch(() => {}), - () => fetchBudgetHistory().then(() => { dashboardSetInterval('budget-history', fetchBudgetHistory, HISTORY_REFRESH_MS); }), - () => ensureNousPolling(), - () => fetchPublicKnowledgeSetting(), - () => fetchKnowledgeStats().then(() => { dashboardSetInterval('knowledge-stats', fetchKnowledgeStats, KB_POLL_MS); }), - () => fetchAuditLog().then(() => { dashboardSetInterval('audit-log', fetchAuditLog, AUDIT_POLL_MS); }), - () => fetchReviewQueue().then(() => { dashboardSetInterval('review-queue', fetchReviewQueue, REVIEW_QUEUE_POLL_MS); }), - () => fetchReviewPipeline().then(() => { dashboardSetInterval('review-pipeline', fetchReviewPipeline, REVIEW_PIPELINE_POLL_MS); }), - () => fetchStageTracker().then(() => { dashboardSetInterval('stage-tracker', fetchStageTracker, STAGE_TRACKER_POLL_MS); }), - () => fetchContributors().then(() => { dashboardSetInterval('contributors', fetchContributors, CONTRIBUTOR_POLL_MS); }), - () => fetchLeaderboard().then(() => { dashboardSetInterval('leaderboard', fetchLeaderboard, LEADERBOARD_POLL_MS); }), - () => fetchGitVersion().then(() => { dashboardSetInterval('git-version', fetchGitVersion, GIT_VERSION_POLL_MS); }), - () => fetchACMMEvaluation().then(() => { dashboardSetInterval('acmm-evaluation', fetchACMMEvaluation, ACMM_EVAL_INTERVAL_MS); }), - () => Promise.resolve(startVersionCheck()), - () => fetch('/api/trends?range=week').then(r => r.json()).then(applyTrends).catch(() => {}).then(() => { dashboardSetInterval('weekly-trends', () => { fetch('/api/trends?range=week').then(r => r.json()).then(applyTrends).catch(() => {}); }, TREND_REFRESH_MS); }), - () => fetchTimeline().then(() => { dashboardSetInterval('timeline', fetchTimeline, TIMELINE_REFRESH_MS); }), - () => fetchAgentLogs().then(() => { dashboardSetInterval('agent-logs', fetchAgentLogs, LOGS_REFRESH_MS); }), + ['backend-discovery', () => Promise.resolve(refreshBackendDiscovery()).then(() => fetchBackendsConfig())], + ['fact-history', () => fetchFactHistory()], + ['overview-kpi-history', () => fetchOverviewKPIHistory().then(() => { dashboardSetInterval('overview-kpi-history', fetchOverviewKPIHistory, HISTORY_REFRESH_MS); })], + ['history', () => fetchTrendHistory().then(() => fetchHistory()).then(() => { dashboardSetInterval('history', fetchHistory, HISTORY_REFRESH_MS); })], + ['budget-ignore', () => fetch('/api/budget-ignore').then(r => r.json()).then(d => { budgetIgnored = d.ignored; }).catch(console.error)], + ['budget-history', () => fetchBudgetHistory().then(() => { dashboardSetInterval('budget-history', fetchBudgetHistory, HISTORY_REFRESH_MS); })], + ['nous-status', () => ensureNousPolling()], + ['public-knowledge', () => fetchPublicKnowledgeSetting()], + ['knowledge-stats', () => fetchKnowledgeStats().then(() => { dashboardSetInterval('knowledge-stats', fetchKnowledgeStats, KB_POLL_MS); })], + ['audit-log', () => fetchAuditLog().then(() => { dashboardSetInterval('audit-log', fetchAuditLog, AUDIT_POLL_MS); })], + ['review-queue', () => fetchReviewQueue().then(() => { dashboardSetInterval('review-queue', fetchReviewQueue, REVIEW_QUEUE_POLL_MS); })], + ['review-pipeline', () => fetchReviewPipeline().then(() => { dashboardSetInterval('review-pipeline', fetchReviewPipeline, REVIEW_PIPELINE_POLL_MS); })], + ['stage-tracker', () => fetchStageTracker().then(() => { dashboardSetInterval('stage-tracker', fetchStageTracker, STAGE_TRACKER_POLL_MS); })], + ['contributors', () => fetchContributors().then(() => { dashboardSetInterval('contributors', fetchContributors, CONTRIBUTOR_POLL_MS); })], + ['leaderboard', () => fetchLeaderboard().then(() => { dashboardSetInterval('leaderboard', fetchLeaderboard, LEADERBOARD_POLL_MS); })], + ['git-version', () => fetchGitVersion().then(() => { dashboardSetInterval('git-version', fetchGitVersion, GIT_VERSION_POLL_MS); })], + ['acmm-evaluation', () => fetchACMMEvaluation().then(() => { dashboardSetInterval('acmm-evaluation', fetchACMMEvaluation, ACMM_EVAL_INTERVAL_MS); })], + ['version-check', () => Promise.resolve(startVersionCheck())], + ['weekly-trends', () => fetch('/api/trends?range=week').then(r => r.json()).then(applyTrends).catch(console.error).then(() => { dashboardSetInterval('weekly-trends', () => { return fetch('/api/trends?range=week').then(r => r.json()).then(applyTrends).catch(console.error); }, TREND_REFRESH_MS); })], + ['timeline', () => fetchTimeline().then(() => { dashboardSetInterval('timeline', fetchTimeline, TIMELINE_REFRESH_MS); })], + ['agent-logs', () => fetchAgentLogs().then(() => { dashboardSetInterval('agent-logs', fetchAgentLogs, LOGS_REFRESH_MS); })], ]; - // `Promise.resolve().then(fn)` rather than `fn()`, so ONE bad entry - // cannot strand the whole chain (#6581). `fn()` was called bare inside - // the setTimeout callback, so an entry that threw synchronously — or - // returned undefined and had `.then` called on it, which is what - // fetchTimeline did — threw out of the callback with nothing to catch - // it. That entry's promise then never settled, so Promise.all never - // settled, so resolveAll never ran and _deferredLoadPromise stayed - // pending for the life of the page. _afterPaint awaits it, so first- - // load view restoration (making #oc-agent-detail active, navigating to - // a saved section) silently never happened. - // - // Wrapping turns a synchronous throw into a rejection, which - // `.then(resolve, resolve)` already handles: the entry is recorded as - // finished-badly instead of never finishing, and the other twelve - // entries still complete. The throw is still reported below rather - // than swallowed — the point is that one broken entry degrades to one - // broken feature, not to a dashboard that never finishes initialising. - const allDone = deferred.map((fn, i) => - new Promise(resolve => setTimeout(() => { - Promise.resolve().then(fn).then(resolve, err => { - console.error('deferred init entry ' + i + ' failed', err); - resolve(); - }); - }, i * DEFERRED_STAGGER_MS)) + // A synchronous throw must not strand the other panels (#6581). + const allDone = deferred.map(([key, fn]) => + dashboardScheduleWork(key, () => Promise.resolve().then(fn)).catch(err => { + console.error('deferred init ' + key + ' failed', err); + }) ); Promise.all(allDone).then(resolveAll); }, DEFERRED_INIT_DELAY_MS); @@ -341,4 +320,3 @@ gh84: function (event, A) { markSentinelDirty('exemptLogins', parseSecurityList(this.value)); }, gh85: function (event, A) { markSentinelDirty('repos', parseSecurityList(this.value)); }, }; - diff --git a/src/pkg/dashboard/static-src/js/sse-bootstrap.js b/src/pkg/dashboard/static-src/js/sse-bootstrap.js index 7c013f8bc7..8753d1534a 100644 --- a/src/pkg/dashboard/static-src/js/sse-bootstrap.js +++ b/src/pkg/dashboard/static-src/js/sse-bootstrap.js @@ -113,7 +113,9 @@ const lastData = window._lastStatus || {}; if (lastData.agents) { applyPinOverrides(lastData.agents); - renderAgents(lastData.agents); + dashboardRenderSection('agents', ['agents-section', 'agent-activity-section'], () => { + if (!_dropdownOpen && !_configModalOpen) renderAgents(lastData.agents); + }); // Keep the cost table's "running agents" column live as agents // switch models or pause/resume between cost polls. if (_lastCost) renderCost(_lastCost); diff --git a/src/pkg/dashboard/static-src/js/welcome.js b/src/pkg/dashboard/static-src/js/welcome.js index 6b80577785..e0ded66cc8 100644 --- a/src/pkg/dashboard/static-src/js/welcome.js +++ b/src/pkg/dashboard/static-src/js/welcome.js @@ -536,8 +536,7 @@ updateWelcomeProgress(); } - checkGHAuth(); + dashboardScheduleWork('welcome-gh-auth', checkGHAuth).catch(console.error); dashboardSetInterval('welcome-gh-auth', checkGHAuth, GH_AUTH_CHECK_INTERVAL_MS); initWelcomeDialog(); initNPSSurvey(); - diff --git a/src/pkg/dashboard/static-src/manifest.txt b/src/pkg/dashboard/static-src/manifest.txt index 2f049665d1..468411c7b3 100644 --- a/src/pkg/dashboard/static-src/manifest.txt +++ b/src/pkg/dashboard/static-src/manifest.txt @@ -58,6 +58,7 @@ html/tracker.html html/faq.html glue/main-script-open.html js/core.js +js/progressive.js js/feedback.js js/sections.js js/layout.js diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 7d2c8e03e6..78a4c49248 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -7408,7 +7408,7 @@ } }; })(); -// Apply layout immediately and hide content until first data render. +// Apply layout immediately; keep the shell visible while live data loads. // NOTE: only body classes are set here — the .layout-toggle buttons do NOT // exist in the DOM yet at this point, so any label write here is a no-op. // applyLayout() (defined in the main script) is the single source of truth @@ -7494,9 +7494,6 @@ if(window.HIVE_KIOSK){ var kt=null; try { kt=new URL(window.location.href).searchParams.get('theme'); } catch(e) {} m=(kt==='light')?'light':'dark'; } if(!m)m='light'; if(m==='light'){document.body.classList.add('light-mode')} - document.body.style.opacity='0'; - document.body.style.transition='opacity 0.15s ease-in'; - setTimeout(function(){ document.body.style.opacity='1'; }, 3000); document.addEventListener('DOMContentLoaded', function(){ if (typeof applyLayout === 'function' && typeof getLayout === 'function') { applyLayout(getLayout()); @@ -7581,7 +7578,6 @@ document.addEventListener('DOMContentLoaded', renderStyleState); } })(); - `; } - // ── Watchdog activity strip (#7254) ────────────────────────────────────── // "Review the watchdog-*-observed entries in the Audit Log, then switch to // Heal" is only a data-driven decision if the data is on the page. The @@ -65537,8 +65706,8 @@

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

{ @@ -65664,7 +65833,6 @@

${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'}${model.editIndex === null ? 'Add Event trigger' : 'Edit Event trigger'} { const deferred = [ - () => Promise.resolve(refreshBackendDiscovery()).then(() => fetchBackendsConfig()), - () => fetchFactHistory(), - () => fetchOverviewKPIHistory().then(() => { dashboardSetInterval('overview-kpi-history', fetchOverviewKPIHistory, HISTORY_REFRESH_MS); }), - () => fetchTrendHistory().then(() => fetchHistory()).then(() => { dashboardSetInterval('history', fetchHistory, HISTORY_REFRESH_MS); }), - () => fetch('/api/budget-ignore').then(r => r.json()).then(d => { budgetIgnored = d.ignored; }).catch(() => {}), - () => fetchBudgetHistory().then(() => { dashboardSetInterval('budget-history', fetchBudgetHistory, HISTORY_REFRESH_MS); }), - () => ensureNousPolling(), - () => fetchPublicKnowledgeSetting(), - () => fetchKnowledgeStats().then(() => { dashboardSetInterval('knowledge-stats', fetchKnowledgeStats, KB_POLL_MS); }), - () => fetchAuditLog().then(() => { dashboardSetInterval('audit-log', fetchAuditLog, AUDIT_POLL_MS); }), - () => fetchReviewQueue().then(() => { dashboardSetInterval('review-queue', fetchReviewQueue, REVIEW_QUEUE_POLL_MS); }), - () => fetchReviewPipeline().then(() => { dashboardSetInterval('review-pipeline', fetchReviewPipeline, REVIEW_PIPELINE_POLL_MS); }), - () => fetchStageTracker().then(() => { dashboardSetInterval('stage-tracker', fetchStageTracker, STAGE_TRACKER_POLL_MS); }), - () => fetchContributors().then(() => { dashboardSetInterval('contributors', fetchContributors, CONTRIBUTOR_POLL_MS); }), - () => fetchLeaderboard().then(() => { dashboardSetInterval('leaderboard', fetchLeaderboard, LEADERBOARD_POLL_MS); }), - () => fetchGitVersion().then(() => { dashboardSetInterval('git-version', fetchGitVersion, GIT_VERSION_POLL_MS); }), - () => fetchACMMEvaluation().then(() => { dashboardSetInterval('acmm-evaluation', fetchACMMEvaluation, ACMM_EVAL_INTERVAL_MS); }), - () => Promise.resolve(startVersionCheck()), - () => fetch('/api/trends?range=week').then(r => r.json()).then(applyTrends).catch(() => {}).then(() => { dashboardSetInterval('weekly-trends', () => { fetch('/api/trends?range=week').then(r => r.json()).then(applyTrends).catch(() => {}); }, TREND_REFRESH_MS); }), - () => fetchTimeline().then(() => { dashboardSetInterval('timeline', fetchTimeline, TIMELINE_REFRESH_MS); }), - () => fetchAgentLogs().then(() => { dashboardSetInterval('agent-logs', fetchAgentLogs, LOGS_REFRESH_MS); }), + ['backend-discovery', () => Promise.resolve(refreshBackendDiscovery()).then(() => fetchBackendsConfig())], + ['fact-history', () => fetchFactHistory()], + ['overview-kpi-history', () => fetchOverviewKPIHistory().then(() => { dashboardSetInterval('overview-kpi-history', fetchOverviewKPIHistory, HISTORY_REFRESH_MS); })], + ['history', () => fetchTrendHistory().then(() => fetchHistory()).then(() => { dashboardSetInterval('history', fetchHistory, HISTORY_REFRESH_MS); })], + ['budget-ignore', () => fetch('/api/budget-ignore').then(r => r.json()).then(d => { budgetIgnored = d.ignored; }).catch(console.error)], + ['budget-history', () => fetchBudgetHistory().then(() => { dashboardSetInterval('budget-history', fetchBudgetHistory, HISTORY_REFRESH_MS); })], + ['nous-status', () => ensureNousPolling()], + ['public-knowledge', () => fetchPublicKnowledgeSetting()], + ['knowledge-stats', () => fetchKnowledgeStats().then(() => { dashboardSetInterval('knowledge-stats', fetchKnowledgeStats, KB_POLL_MS); })], + ['audit-log', () => fetchAuditLog().then(() => { dashboardSetInterval('audit-log', fetchAuditLog, AUDIT_POLL_MS); })], + ['review-queue', () => fetchReviewQueue().then(() => { dashboardSetInterval('review-queue', fetchReviewQueue, REVIEW_QUEUE_POLL_MS); })], + ['review-pipeline', () => fetchReviewPipeline().then(() => { dashboardSetInterval('review-pipeline', fetchReviewPipeline, REVIEW_PIPELINE_POLL_MS); })], + ['stage-tracker', () => fetchStageTracker().then(() => { dashboardSetInterval('stage-tracker', fetchStageTracker, STAGE_TRACKER_POLL_MS); })], + ['contributors', () => fetchContributors().then(() => { dashboardSetInterval('contributors', fetchContributors, CONTRIBUTOR_POLL_MS); })], + ['leaderboard', () => fetchLeaderboard().then(() => { dashboardSetInterval('leaderboard', fetchLeaderboard, LEADERBOARD_POLL_MS); })], + ['git-version', () => fetchGitVersion().then(() => { dashboardSetInterval('git-version', fetchGitVersion, GIT_VERSION_POLL_MS); })], + ['acmm-evaluation', () => fetchACMMEvaluation().then(() => { dashboardSetInterval('acmm-evaluation', fetchACMMEvaluation, ACMM_EVAL_INTERVAL_MS); })], + ['version-check', () => Promise.resolve(startVersionCheck())], + ['weekly-trends', () => fetch('/api/trends?range=week').then(r => r.json()).then(applyTrends).catch(console.error).then(() => { dashboardSetInterval('weekly-trends', () => { return fetch('/api/trends?range=week').then(r => r.json()).then(applyTrends).catch(console.error); }, TREND_REFRESH_MS); })], + ['timeline', () => fetchTimeline().then(() => { dashboardSetInterval('timeline', fetchTimeline, TIMELINE_REFRESH_MS); })], + ['agent-logs', () => fetchAgentLogs().then(() => { dashboardSetInterval('agent-logs', fetchAgentLogs, LOGS_REFRESH_MS); })], ]; - // `Promise.resolve().then(fn)` rather than `fn()`, so ONE bad entry - // cannot strand the whole chain (#6581). `fn()` was called bare inside - // the setTimeout callback, so an entry that threw synchronously — or - // returned undefined and had `.then` called on it, which is what - // fetchTimeline did — threw out of the callback with nothing to catch - // it. That entry's promise then never settled, so Promise.all never - // settled, so resolveAll never ran and _deferredLoadPromise stayed - // pending for the life of the page. _afterPaint awaits it, so first- - // load view restoration (making #oc-agent-detail active, navigating to - // a saved section) silently never happened. - // - // Wrapping turns a synchronous throw into a rejection, which - // `.then(resolve, resolve)` already handles: the entry is recorded as - // finished-badly instead of never finishing, and the other twelve - // entries still complete. The throw is still reported below rather - // than swallowed — the point is that one broken entry degrades to one - // broken feature, not to a dashboard that never finishes initialising. - const allDone = deferred.map((fn, i) => - new Promise(resolve => setTimeout(() => { - Promise.resolve().then(fn).then(resolve, err => { - console.error('deferred init entry ' + i + ' failed', err); - resolve(); - }); - }, i * DEFERRED_STAGGER_MS)) + // A synchronous throw must not strand the other panels (#6581). + const allDone = deferred.map(([key, fn]) => + dashboardScheduleWork(key, () => Promise.resolve().then(fn)).catch(err => { + console.error('deferred init ' + key + ' failed', err); + }) ); Promise.all(allDone).then(resolveAll); }, DEFERRED_INIT_DELAY_MS); @@ -67773,7 +67919,6 @@

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

diff --git a/src/pkg/dashboard/webstatic/dashboard_script.go b/src/pkg/dashboard/webstatic/dashboard_script.go new file mode 100644 index 0000000000..80984d2c01 --- /dev/null +++ b/src/pkg/dashboard/webstatic/dashboard_script.go @@ -0,0 +1,165 @@ +package webstatic + +import ( + "bytes" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "net/http" + "strconv" + "sync" +) + +const ( + dashboardMainScriptMarker = "` + +// DashboardScript is the exact main classic script from the final branded +// document. Its full SHA-256 URL changes with its bytes, including branding. +// Register it behind the same authentication middleware as the index document. +type DashboardScript struct { + path string + integrity string + document *IndexDocument + gzipOnce sync.Once + gzipETag string +} + +// ExternalizeDashboardScript replaces only the known main script with a +// same-origin deferred classic script. Keeping one script (not a module or +// wrapper) preserves its global lexical declarations and function bindings. +// The canonical generated index remains untouched for source-string consumers. +// Unexpected markup retains the original, working inline document and its CSP. +func ExternalizeDashboardScript(raw []byte) ([]byte, *DashboardScript) { + marker := []byte(dashboardMainScriptMarker) + if bytes.Count(raw, marker) != 1 { + return raw, nil + } + start := bytes.Index(raw, marker) + bodyStart := start + len("")) + if closeOffset < 0 { + return raw, nil + } + bodyEnd := bodyStart + closeOffset + end := bodyEnd + len("") + // Deferring a script ahead of a later inline script would reverse their + // execution order. Only split the final script element in the document. + if bytes.Contains(bytes.ToLower(raw[end:]), []byte("` +} + +// Precompress prepares both the compressed bytes and their strong validator. +func (s *DashboardScript) Precompress() { + s.gzipOnce.Do(func() { + if body := s.document.compressedBody(); body != nil { + s.gzipETag = ETagFor(body) + } + }) +} + +func (s *DashboardScript) ServeHTTP(w http.ResponseWriter, r *http.Request) { + h := w.Header() + h.Set("X-Content-Type-Options", "nosniff") + if r.URL.Path != s.path { + h.Set("Cache-Control", "no-cache") + http.NotFound(w, r) + return + } + if r.Method != http.MethodGet && r.Method != http.MethodHead { + h.Set("Cache-Control", "no-cache") + h.Set("Allow", "GET, HEAD") + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + return + } + h.Set("Content-Type", "text/javascript; charset=utf-8") + h.Set("Cache-Control", dashboardScriptCache) + h.Add("Vary", "Accept-Encoding") + body, etag := s.document.raw, s.document.etag + if acceptsGzip(r.Header.Get("Accept-Encoding")) { + s.Precompress() + if s.document.gzipped != nil { + body, etag = s.document.gzipped, s.gzipETag + h.Set("Content-Encoding", "gzip") + } + } + h.Set("ETag", etag) + if ifNoneMatchHits(r.Header.Get("If-None-Match"), etag) { + w.WriteHeader(http.StatusNotModified) + return + } + h.Set("Content-Length", strconv.Itoa(len(body))) + w.WriteHeader(http.StatusOK) + if r.Method != http.MethodHead { + _, _ = w.Write(body) + } +} diff --git a/src/pkg/dashboard/webstatic/dashboard_script_test.go b/src/pkg/dashboard/webstatic/dashboard_script_test.go new file mode 100644 index 0000000000..2b943cc58d --- /dev/null +++ b/src/pkg/dashboard/webstatic/dashboard_script_test.go @@ -0,0 +1,368 @@ +package webstatic + +import ( + "bytes" + "compress/gzip" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "io" + "net/http" + "net/http/httptest" + "os" + "os/exec" + "strconv" + "strings" + "testing" +) + +func dashboardScriptFixture() []byte { + return []byte("
Loading
\n " + + dashboardMainScriptMarker + "\n const lexical = '🐝';\n function action() { return lexical; }\n \n") +} + +func requireDashboardScript(t *testing.T, raw []byte) ([]byte, *DashboardScript) { + t.Helper() + shell, asset := ExternalizeDashboardScript(raw) + if asset == nil { + t.Fatal("main script was not externalized") + } + return shell, asset +} + +func TestExternalizeDashboardScriptPreservesBytesAndOrder(t *testing.T) { + raw := InjectBranding(dashboardScriptFixture()) + shell, asset := requireDashboardScript(t, raw) + originalScripts := ExtractInlineScripts(raw) + retainedScripts := ExtractInlineScripts(shell) + if len(originalScripts) != 2 || len(retainedScripts) != 2 { + t.Fatalf("script counts before=%d after=%d, want original boot and recovery bootstrap", len(originalScripts), len(retainedScripts)) + } + if !bytes.Equal(retainedScripts[0], originalScripts[0]) { + t.Fatal("inline boot script changed") + } + if !bytes.Equal(asset.document.raw, originalScripts[1]) { + t.Fatal("main script text, whitespace, lexical bindings or Unicode changed") + } + sum := sha256.Sum256(originalScripts[1]) + if want := "/assets/dashboard-main." + hex.EncodeToString(sum[:]) + ".js"; asset.Path() != want { + t.Fatalf("asset URL = %q, want full content address %q", asset.Path(), want) + } + tag := []byte(``) + replacement := append([]byte(dashboardScriptBootstrap), tag...) + recovered := bytes.Replace(shell, replacement, append(append([]byte("")...), 1) + if !bytes.Equal(recovered, raw) { + t.Fatal("replacing external tag with recovered script did not reproduce exact original HTML") + } + if !bytes.Contains(shell, []byte(BrandingLinkTag)) || bytes.Index(shell, retainedScripts[0]) > bytes.Index(shell, tag) { + t.Fatal("branding or inline boot order was lost") + } + _, same := requireDashboardScript(t, raw) + if same.Path() != asset.Path() { + t.Fatal("identical script bytes produced a different content address") + } + changed := bytes.Replace(raw, []byte("'🐝'"), []byte("'branded bee'"), 1) + _, branded := requireDashboardScript(t, changed) + if branded.Path() == asset.Path() || !bytes.Contains(branded.document.raw, []byte("'branded bee'")) { + t.Fatal("final branded script bytes were not used for content addressing") + } + if bytes.Contains(shell, []byte("type=\"module\"")) || bytes.Contains(shell, []byte("async")) { + t.Fatal("main script must remain an ordered deferred classic script") + } + policy := ScriptSrcElemSources(shell) + if !strings.HasPrefix(policy, "'self' ") || !strings.Contains(policy, CSPScriptHash(retainedScripts[0])) { + t.Fatal("shell CSP must authorize same-origin asset and exact inline boot") + } + if !strings.Contains(policy, CSPScriptHash(retainedScripts[1])) || + bytes.Index(shell, retainedScripts[1]) > bytes.Index(shell, tag) { + t.Fatal("trusted recovery bootstrap must be CSP-authorized before the main script") + } + if strings.Contains(policy, CSPScriptHash(originalScripts[1])) || strings.Contains(policy, "unsafe-inline") { + t.Fatal("shell CSP should not need main-script hash or inline weakening") + } +} + +func TestExternalizeDashboardScriptUnexpectedMarkupIsUnchanged(t *testing.T) { + for name, raw := range map[string][]byte{ + "empty": nil, + "missing": []byte(""), + "unclosed": []byte("" + dashboardMainScriptMarker + "\nmain()"), + "ambiguous": append(dashboardScriptFixture(), dashboardScriptFixture()...), + "later inline": append(dashboardScriptFixture(), []byte("")...), + "later external": append(dashboardScriptFixture(), + []byte(``)...), + "changed tag": bytes.Replace(dashboardScriptFixture(), []byte(dashboardMainScriptMarker), + []byte(strings.Replace(dashboardMainScriptMarker, "