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..e770e24510 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,6 +1159,7 @@ 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); } } @@ -1216,7 +1220,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; } 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 @@