Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-18906-agent-switch-scroll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Keep the dashboard at the same agent and retain dropdown focus when an agent's method, model, or reasoning effort changes.
50 changes: 50 additions & 0 deletions src/pkg/dashboard/agent_switch_scroll_18906.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
1 change: 1 addition & 0 deletions src/pkg/dashboard/agents_refresh_scroll_jump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] })); }
Expand Down
10 changes: 9 additions & 1 deletion src/pkg/dashboard/static-src/js/agent-cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/pkg/dashboard/static-src/js/refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
17 changes: 16 additions & 1 deletion src/pkg/dashboard/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32723,6 +32723,9 @@ <h2 class="config-title" id="activity-monitor-labels-title">All labels</h2>
// 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)) {
Expand All @@ -32742,6 +32745,7 @@ <h2 class="config-title" id="activity-monitor-labels-title">All labels</h2>
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);
}
}

Expand Down Expand Up @@ -32802,7 +32806,11 @@ <h2 class="config-title" id="activity-monitor-labels-title">All labels</h2>
// 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);
Expand Down Expand Up @@ -56428,6 +56436,13 @@ <h3 class="modal-h">How Agents Use Knowledge (Priming)</h3>
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;
}

Expand Down
Loading