From 545d67bd1bc7d8ad73e93aeb3c0ce6799d8d47f9 Mon Sep 17 00:00:00 2001 From: Karibhou Date: Wed, 18 Nov 2026 06:05:29 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20fix:=20preserve=20agent=20po?= =?UTF-8?q?sition=20when=20switching=20methods?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Keep the focused agent anchored through direct and status-driven renders, and restore focus when a control is replaced. Hive-Run: hivecommons/hive#18906 Hive-Plan: agent-switch-scroll Hive-Spec: issue-18906#scroll-focus Co-authored-by: clubanderson <407614+clubanderson@users.noreply.github.com> Signed-off-by: Karibhou --- .../fixed-18906-agent-switch-scroll.md | 1 + .../agent_switch_scroll_18906.test.js | 50 +++++++++++++++++++ .../agents_refresh_scroll_jump_test.go | 1 + .../dashboard/static-src/js/agent-cards.js | 11 +++- src/pkg/dashboard/static-src/js/refresh.js | 7 +++ 5 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 changelog.d/fixed-18906-agent-switch-scroll.md create mode 100644 src/pkg/dashboard/agent_switch_scroll_18906.test.js diff --git a/changelog.d/fixed-18906-agent-switch-scroll.md b/changelog.d/fixed-18906-agent-switch-scroll.md new file mode 100644 index 0000000000..a23e901d32 --- /dev/null +++ b/changelog.d/fixed-18906-agent-switch-scroll.md @@ -0,0 +1 @@ +- Keep the dashboard at the same agent and retain dropdown focus when an agent's method, model, or reasoning effort changes. diff --git a/src/pkg/dashboard/agent_switch_scroll_18906.test.js b/src/pkg/dashboard/agent_switch_scroll_18906.test.js new file mode 100644 index 0000000000..2c93f8da89 --- /dev/null +++ b/src/pkg/dashboard/agent_switch_scroll_18906.test.js @@ -0,0 +1,50 @@ +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'); + +function functionSource(file, name) { + const src = fs.readFileSync(path.join(__dirname, 'static-src/js', file), 'utf8'); + const start = src.indexOf(` function ${name}(`); + const end = src.indexOf('\n }', start); + assert.ok(start >= 0 && end >= 0, `missing ${name}`); + return src.slice(start, end + 6); +} + +test('changed method, model and effort controls retain focus without scrolling', () => { + const document = { activeElement: null }; + const ctx = vm.createContext({ document }); + vm.runInContext(functionSource('agent-cards.js', 'morphDomNode'), ctx); + for (const action of ['switchCli', 'switchModel', 'switchEffort']) { + let focusOptions; + const old = { nodeType: 1, tagName: 'SELECT', outerHTML: action + '-old', replaceWith(next) { this.replacement = next; } }; + const next = { nodeType: 1, tagName: 'SELECT', outerHTML: action + '-new', focus(options) { focusOptions = options; document.activeElement = this; } }; + document.activeElement = old; + ctx.morphDomNode(old, next); + assert.equal(old.replacement, next); + assert.equal(document.activeElement, next); + assert.equal(focusOptions.preventScroll, true); + } + let focused = false; + const old = { nodeType: 1, tagName: 'SELECT', outerHTML: 'old', replaceWith() {} }; + document.activeElement = {}; + ctx.morphDomNode(old, { nodeType: 1, tagName: 'SELECT', outerHTML: 'new', focus() { focused = true; } }); + assert.equal(focused, false, 'refresh must not steal focus'); +}); + +test('scroll anchor restores the same card after a layout change', () => { + let top = -80; + let scrollY = 500; + const card = { getBoundingClientRect: () => ({ top, bottom: top + 200, height: 200 }), getAttribute: () => 'worker', getClientRects: () => [1] }; + const section = { isConnected: true, contains: c => c === card, getClientRects: () => [1], getBoundingClientRect: () => ({ top: -300, bottom: 500 }), querySelectorAll: () => [], querySelector: () => card }; + const window = { scrollY, scrollBy(_x, delta) { scrollY += delta; top -= delta; }, scrollTo(_x, y) { scrollY = y; } }; + const document = { activeElement: { closest: () => card }, querySelectorAll: () => [section] }; + const ctx = vm.createContext({ window, document }); + vm.runInContext(functionSource('refresh.js', 'captureScrollAnchor') + '\n' + functionSource('refresh.js', 'restoreScrollAnchor'), ctx); + const anchor = ctx.captureScrollAnchor(); + top += 120; + ctx.restoreScrollAnchor(anchor); + assert.equal(top, -80); + assert.equal(scrollY, 620); +}); diff --git a/src/pkg/dashboard/agents_refresh_scroll_jump_test.go b/src/pkg/dashboard/agents_refresh_scroll_jump_test.go index 198560e802..149ed4209a 100644 --- a/src/pkg/dashboard/agents_refresh_scroll_jump_test.go +++ b/src/pkg/dashboard/agents_refresh_scroll_jump_test.go @@ -83,6 +83,7 @@ func TestMorphDomNodeKeepsNodesAndState(t *testing.T) { jsFunc(t, html, "morphDomNode"), jsFunc(t, html, "morphDomAttributes"), }, "\n") + ` +const document = { activeElement: null }; class N { constructor(tag, attrs, kids) { this.nodeType = 1; this.tagName = tag; this._attrs = Object.assign({}, attrs || {}); this.childNodes = kids || []; this.childNodes.forEach(k => k.parentNode = this); this.parentNode = null; this.scrollTop = 0; } get attributes() { return Object.keys(this._attrs).map(name => ({ name, value: this._attrs[name] })); } diff --git a/src/pkg/dashboard/static-src/js/agent-cards.js b/src/pkg/dashboard/static-src/js/agent-cards.js index 2ae99d0cd4..c9ce8cdeef 100644 --- a/src/pkg/dashboard/static-src/js/agent-cards.js +++ b/src/pkg/dashboard/static-src/js/agent-cards.js @@ -1137,6 +1137,9 @@ // the viewport grew or shrank with new log lines and pushed the rows // the reader was looking at — the whole page appeared to jump on // every SSE tick, worst in Safari which has no scroll anchoring. + // Direct renders after a setting change do not pass through render()'s + // scroll anchor. Preserve the reader's position for those renders too. + const scrollAnchor = typeof captureScrollAnchor === 'function' ? captureScrollAnchor() : null; const tailFollow = agentTailFollowState(el); morphAgentGrid(el, html); for (const [id, val] of Object.entries(savedInputs)) { @@ -1156,9 +1159,11 @@ if (!(key in tailFollow) || tailFollow[key]) d.scrollTop = d.scrollHeight; }); el.querySelectorAll('[data-agent-resize-y][data-agent-height]').forEach(d => { d.style.height = `${d.dataset.agentHeight}px`; }); + if (scrollAnchor) restoreScrollAnchor(scrollAnchor); } } + // Per-agent "was the terminal tail scrolled to the bottom?" snapshot taken // before a re-render, so the post-render auto-follow respects a reader who // scrolled up inside a pane. @@ -1216,7 +1221,11 @@ // that attribute patching can't reconcile — swap them only when their // markup actually changed. if (from.tagName === 'SELECT' || from.tagName === 'INPUT' || from.tagName === 'TEXTAREA') { - if (from.outerHTML !== to.outerHTML) from.replaceWith(to); + if (from.outerHTML !== to.outerHTML) { + const focused = document.activeElement === from; + from.replaceWith(to); + if (focused && !to.disabled) to.focus({ preventScroll: true }); + } return; } morphDomAttributes(from, to); diff --git a/src/pkg/dashboard/static-src/js/refresh.js b/src/pkg/dashboard/static-src/js/refresh.js index 2ea0771683..9d845a7675 100644 --- a/src/pkg/dashboard/static-src/js/refresh.js +++ b/src/pkg/dashboard/static-src/js/refresh.js @@ -448,6 +448,13 @@ if (rect.bottom > 0 && rect.height > 0) { anchor.cardKey = cards[i].getAttribute('data-agent'); anchor.cardTop = rect.top; break; } } } + // A focused card control is the reader's anchor, even when another + // card in the same row is also visible. + const focusedCard = document.activeElement?.closest('.agent-card[data-agent]'); + if (focusedCard && anchor.el && anchor.el.contains(focusedCard)) { + anchor.cardKey = focusedCard.getAttribute('data-agent'); + anchor.cardTop = focusedCard.getBoundingClientRect().top; + } return anchor; } From 990fbacc4a79b27206b3646977393291cb8595a3 Mon Sep 17 00:00:00 2001 From: Karibhou Date: Wed, 18 Nov 2026 06:19:49 +0100 Subject: [PATCH 2/2] fix: regenerate dashboard bundle Signed-off-by: Karibhou --- src/pkg/dashboard/static-src/js/agent-cards.js | 1 - src/pkg/dashboard/static/index.html | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/pkg/dashboard/static-src/js/agent-cards.js b/src/pkg/dashboard/static-src/js/agent-cards.js index c9ce8cdeef..e770e24510 100644 --- a/src/pkg/dashboard/static-src/js/agent-cards.js +++ b/src/pkg/dashboard/static-src/js/agent-cards.js @@ -1163,7 +1163,6 @@ } } - // Per-agent "was the terminal tail scrolled to the bottom?" snapshot taken // before a re-render, so the post-render auto-follow respects a reader who // scrolled up inside a pane. diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 49ea6e741c..2945091e0a 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -32723,6 +32723,9 @@

All labels

// the viewport grew or shrank with new log lines and pushed the rows // the reader was looking at — the whole page appeared to jump on // every SSE tick, worst in Safari which has no scroll anchoring. + // Direct renders after a setting change do not pass through render()'s + // scroll anchor. Preserve the reader's position for those renders too. + const scrollAnchor = typeof captureScrollAnchor === 'function' ? captureScrollAnchor() : null; const tailFollow = agentTailFollowState(el); morphAgentGrid(el, html); for (const [id, val] of Object.entries(savedInputs)) { @@ -32742,6 +32745,7 @@

All labels

if (!(key in tailFollow) || tailFollow[key]) d.scrollTop = d.scrollHeight; }); el.querySelectorAll('[data-agent-resize-y][data-agent-height]').forEach(d => { d.style.height = `${d.dataset.agentHeight}px`; }); + if (scrollAnchor) restoreScrollAnchor(scrollAnchor); } } @@ -32802,7 +32806,11 @@

All labels

// that attribute patching can't reconcile — swap them only when their // markup actually changed. if (from.tagName === 'SELECT' || from.tagName === 'INPUT' || from.tagName === 'TEXTAREA') { - if (from.outerHTML !== to.outerHTML) from.replaceWith(to); + if (from.outerHTML !== to.outerHTML) { + const focused = document.activeElement === from; + from.replaceWith(to); + if (focused && !to.disabled) to.focus({ preventScroll: true }); + } return; } morphDomAttributes(from, to); @@ -56428,6 +56436,13 @@ if (rect.bottom > 0 && rect.height > 0) { anchor.cardKey = cards[i].getAttribute('data-agent'); anchor.cardTop = rect.top; break; } } } + // A focused card control is the reader's anchor, even when another + // card in the same row is also visible. + const focusedCard = document.activeElement?.closest('.agent-card[data-agent]'); + if (focusedCard && anchor.el && anchor.el.contains(focusedCard)) { + anchor.cardKey = focusedCard.getAttribute('data-agent'); + anchor.cardTop = focusedCard.getBoundingClientRect().top; + } return anchor; }