From 4b833d23e9867b24d5ac5cabab7f6c123f809c78 Mon Sep 17 00:00:00 2001
From: Jason Woltje
No Message: this Console does not send chat in ${esc(orgName)}.
`); // SP19: a restart that is running keeps its buttons even when a repaint reads no run between its end and its launch. - if (change && a.level === 'org' && !a.keep && (running(item) > 0 || restarting.has(id))) acts.push(stopActions(pick, a, orgName, !!window.MosaicAgents?.isThread?.(def))); + if (change && a.level === 'org' && !a.keep && (running(item) > 0 || restarts.get(id)?.busy)) acts.push(stopActions(pick, a, orgName, !!window.MosaicAgents?.isThread?.(def))); // Why Stop is not offered, in words, where this Console could otherwise change the organization's agents. // SP13: "not running" is a reason only once the instances read has given the agent's runs. else if (change) acts.push(`${reading ? 'Its state is being read. Stop is offered once it is read and the agent is running.' @@ -1353,7 +1357,7 @@ : a.level !== 'org' ? 'A copy is not stopped from here.' : a.keep ? `${esc(a.name)} is one of ${esc(orgName)}’s protected agents, so it is not stopped from here.` : 'It is not running, so there is nothing to stop.'}
`); - const st = stopState?.id === id ? stopState : null; + const st = actState(id); const actions = chat || change ? `${esc(st?.text || '')}
This Console only reads ${esc(orgName)}. It can not message or stop its agents.
`; @@ -1393,6 +1397,7 @@ // `agent` from the address in place. Either way the page repaints without it and focus returns to the row. function closeDrawer() { if (!stopState?.busy) stopState = null; + for (const [k, r] of restarts) if (!r.busy) restarts.delete(k); if (stateOf()?.[DRAWER_MARK] && queryOf().has('agent')) { history.back(); return; } history.replaceState(unmarked(), '', sansAgent()); document.dispatchEvent(new CustomEvent('mosaic-workspace-render')); @@ -1461,12 +1466,14 @@ // SP19: the drawer's Restart. The agents.js chain ends the agent through its own organization's routes // (the drawer's, which on #/agents is not the page's), waits for the request to close and its runs to // reach 0, then launches it; a conversation agent is not launched. It never regenerates. Each step is - // painted in place; a refusal leaves both buttons usable and focus on Restart. + // painted in place; a refusal before the end leaves both buttons usable and focus on Restart. const refusedWhy = (d, orgName) => { const e = d?.result?.error, m = d?.result?.message; if (d?.status === 403 && e === 'read-only') return `this Console only reads agents in ${orgName} (read-only).`; if (d?.status === 403 && (e === 'unavailable' || e === 'owner-only')) return `this Console is not the owner of ${orgName}, so it does not restart its agents.`; if (e === 'no-bus-host') return `the bus of ${orgName} is not running. Nothing changed.`; + if (e === 'org-archived') return `${orgName} is archived, so its agents are not changed. Nothing changed.`; + if (e === 'org-unavailable') return `${orgName} is not available to this Console. Nothing changed.`; if (d?.result?.sent === false && typeof m === 'string' && m) return m; if (d?.result?.sent === false && typeof e === 'string') return `the bus refused (${e}).`; return STOP_UNKNOWN; @@ -1492,28 +1499,35 @@ async function restartAgent(d) { const id = d.dataset.wsDrawer, org = d.dataset.drawerOrgId, name = id.slice(org.length + 1), orgName = d.dataset.drawerOrgName; const thread = d.querySelector('[data-drawer-restart]')?.dataset.drawerThread === 'yes'; - if (restarting.has(id)) return; - restarting.add(id); + if (restarts.get(id)?.busy) return; + // The question is answered: from now on this agent's drawer draws its restart. + if (stopState?.id === id) stopState = null; + restarts.set(id, { busy: true, text: '' }); + // Paint this agent's drawer in place, when it is on screen and no question of its own holds it. const paint = () => { - const now = document.querySelector(`[data-ws-drawer="${CSS.escape(id)}"]`); if (!now || stopState?.id !== id) return; - const s = now.querySelector('[data-drawer-status]'); if (s) s.textContent = stopState.text; + const now = document.querySelector(`[data-ws-drawer="${CSS.escape(id)}"]`), r = restarts.get(id); if (!now || !r || stopState?.id === id) return; + const s = now.querySelector('[data-drawer-status]'); if (s) s.textContent = r.text; const rb = now.querySelector('[data-drawer-restart]'), sb = now.querySelector('[data-drawer-stop]'); - if (rb) { rb.disabled = !!stopState.busy; rb.firstChild.textContent = stopState.busy ? 'Restarting…' : 'Restart'; } - if (sb) sb.disabled = !!stopState.busy; + if (rb) { rb.disabled = r.busy; rb.firstChild.textContent = r.busy ? 'Restarting…' : 'Restart'; } + if (sb) sb.disabled = r.busy; now.querySelector('.ws-drawer-confirm')?.remove(); }; + // The finished line stays in the entry. A refusal before the end changed nothing, so focus goes back + // to Restart. Once the agent was ended its runs changed: the repaint reads the instances again, so the + // buttons follow them (after a refused launch it has none, so Stop and Restart go), and focus goes to + // the drawer's heading, which every repaint keeps. + const after = new Set(['restarted', 'threadEnded', 'launchSignedOut', 'launchUnknown', 'launchRefused', 'launchOpen', 'launchClosed']); + const done = step => { + document.querySelector(`[data-ws-drawer="${CSS.escape(id)}"]`)?.querySelector(after.has(step) ? '[data-focus-key="drawer-h"]' : '[data-drawer-restart]')?.focus({ preventScroll: true }); + if (after.has(step)) document.dispatchEvent(new CustomEvent('mosaic-workspace-render')); + }; const onStatus = ({ step, busy, text }) => { - if (!busy) restarting.delete(id); - // Another drawer's question or action may hold the state now; this chain then leaves it alone. - if (stopState && stopState.id !== id) return; - stopState = busy ? { id, kind: 'restart', busy: true, text } : { id, kind: 'restart', text }; + restarts.set(id, { busy: !!busy, text }); paint(); - if (busy) return; - document.querySelector(`[data-ws-drawer="${CSS.escape(id)}"] [data-drawer-restart]`)?.focus({ preventScroll: true }); - if (step === 'restarted' || step === 'threadEnded') document.dispatchEvent(new CustomEvent('mosaic-workspace-render')); + if (!busy) done(step); }; try { await window.MosaicAgents.restart(name, thread, { org, reason: RESTART_REASON, text: restartWords(name, orgName), onStatus }); } - catch { restarting.delete(id); stopState = { id, kind: 'restart', text: `Not restarted: ${STOP_UNKNOWN}` }; paint(); } + catch { restarts.set(id, { busy: false, text: `Not restarted: ${STOP_UNKNOWN}` }); paint(); done('endUnknown'); } } document.addEventListener('click', e => { const link = e.target.closest?.('[data-ws-drawer-open]'); @@ -1534,11 +1548,11 @@ if (e.target.closest('[data-drawer-close]')) { closeDrawer(); return; } const id = d.dataset.wsDrawer; // SP19: nothing starts while this agent's Stop or Restart runs. - const held = (stopState?.id === id && stopState.busy) || restarting.has(id); + const held = (stopState?.id === id && stopState.busy) || !!restarts.get(id)?.busy; const ask = e.target.closest('[data-drawer-stop]') ? 'stop' : e.target.closest('[data-drawer-restart]') ? 'restart' : null; if (ask && !held) { const b = d.querySelector(`[data-drawer-${ask}]`), name = d.dataset.drawerName, who = `${name} in ${d.dataset.drawerOrgName}`; - stopState = { id, kind: ask, confirm: true, text: '' }; + stopState = { id, kind: ask, confirm: true, text: '' }; restarts.delete(id); d.querySelector('.ws-drawer-confirm')?.remove(); b.insertAdjacentHTML('afterend', ask === 'stop' ? stopConfirm(name, who) : restartConfirm(name, who, b.dataset.drawerThread === 'yes')); for (const x of d.querySelectorAll('[data-drawer-stop], [data-drawer-restart]')) x.disabled = true; diff --git a/packages/webui/tests/agent-drawer-browser.test.mjs b/packages/webui/tests/agent-drawer-browser.test.mjs index 064c194df..3ae08cdb9 100644 --- a/packages/webui/tests/agent-drawer-browser.test.mjs +++ b/packages/webui/tests/agent-drawer-browser.test.mjs @@ -732,7 +732,7 @@ const closed = (request, action, extra = {}) => ({ request, instance: 'coder', a // Every distinct state of the drawer's actions, from now on: [status, Restart's label, Restart disabled, Stop disabled]. const WATCH = id => `(() => { window.__seen = []; const snap = () => { const d = document.querySelector('[data-ws-drawer="${id}"]'); if (!d) return; const r = d.querySelector('[data-drawer-restart]'), s = d.querySelector('[data-drawer-stop]'); const v = [d.querySelector('[data-drawer-status]')?.textContent ?? '', r ? r.firstChild.textContent : null, !!r?.disabled, !!s?.disabled]; const l = window.__seen.at(-1); if (!l || l.join('|') !== v.join('|')) window.__seen.push(v); }; snap(); new MutationObserver(snap).observe(document.getElementById('bus-view'), { subtree: true, childList: true, characterData: true, attributes: true }); return true; })()`; const status = d => `(document.querySelector('${d} [data-drawer-status]')?.textContent ?? "")`; -const usable = d => `!document.querySelector('${d} [data-drawer-restart]').disabled && !document.querySelector('${d} [data-drawer-stop]').disabled && !document.querySelector('${d} .ws-drawer-confirm')`; +const usable = d => `document.querySelector('${d} [data-drawer-restart]')?.disabled === false && document.querySelector('${d} [data-drawer-stop]')?.disabled === false && !document.querySelector('${d} .ws-drawer-confirm')`; test('SP19: Restart asks first, then ends and launches the agent in its own organization, and never regenerates', async t => { // The reader was last on Beta Works, so the Console's chosen organization is beta. #/agents shows every @@ -828,11 +828,52 @@ test('SP19: a second click while a restart runs does nothing', async t => { assert.equal(await b.evaluate('window.__posts.length'), 1); }); -// Each refusal the two routes can give says so in one plain line and leaves both buttons usable. +// SP19 review: a restart keeps its own state, so another drawer's question or Stop does not lose its +// line, and its drawer still shows it running when the reader comes back. +test('SP19: a restart keeps its line and its running state across another drawer\'s Stop', async t => { + const data = D => { D.acme.instances.ideas = { definition: 'ideas', running: ['run-4'] }; }; + const { b, wait } = await setup(t, { hash: '#/agents?agent=acme%3Acoder', scripts: [FAST], data }); + const d = '[data-ws-drawer="acme:coder"]', i = '[data-ws-drawer="acme:ideas"]'; + await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-restart]')`); + await b.evaluate(`window.__answers.push({ hold: new Promise(r => { window.__release = r; }), status: 403, body: { error: 'unavailable', sent: false } })`); + await b.evaluate(`document.querySelector('${d} [data-drawer-restart]').click()`); + await wait(`document.querySelector('${d} [data-drawer-restart-yes]')`); + await b.evaluate(`document.querySelector('${d} [data-drawer-restart-yes]').click()`); + await wait(`window.__posts.length === 1`); + // Ideas asks to stop and is cancelled; back on coder, its restart still shows as running. + await b.evaluate('location.hash = "#/agents?agent=acme%3Aideas"'); + await wait(`${open('acme:ideas')} && document.querySelector('${i} [data-drawer-stop]')`); + await b.evaluate(`document.querySelector('${i} [data-drawer-stop]').click()`); + await wait(`document.querySelector('${i} [data-drawer-stop-no]')`); + await b.evaluate(`document.querySelector('${i} [data-drawer-stop-no]').click()`); + await b.evaluate('location.hash = "#/agents?agent=acme%3Acoder"'); + await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-restart]')`); + assert.deepEqual(await b.evaluate(`[${status(d)}, document.querySelector('${d} [data-drawer-restart]').firstChild.textContent, document.querySelector('${d} [data-drawer-restart]').disabled, document.querySelector('${d} [data-drawer-stop]').disabled]`), + ['Restarting coder: asking it to end…', 'Restarting…', true, true]); + // Ideas' Stop question is open when coder's restart is refused; coder's drawer still says so after. + await b.evaluate('location.hash = "#/agents?agent=acme%3Aideas"'); + await wait(`${open('acme:ideas')} && document.querySelector('${i} [data-drawer-stop]')`); + await b.evaluate(`document.querySelector('${i} [data-drawer-stop]').click()`); + await wait(`document.querySelector('${i} [data-drawer-stop-no]')`); + await b.evaluate('window.__release()'); + await b.evaluate('new Promise(r => setTimeout(r, 300))'); + assert.equal(await b.evaluate(`!!document.querySelector('${i} [data-drawer-stop-confirm]')`), true, 'the other drawer keeps its question'); + await b.evaluate(`document.querySelector('${i} [data-drawer-stop-no]').click()`); + await b.evaluate('location.hash = "#/agents?agent=acme%3Acoder"'); + await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-restart]')`); + await wait(`${status(d)} === 'Not restarted: this Console is not the owner of Acme Studio, so it does not restart its agents.' && ${usable(d)}`); + assert.equal(await b.evaluate('window.__posts.length'), 1); +}); + +// Each refusal the two routes can give says so in one plain line. Where the agent still runs, both +// buttons are usable again and focus is on Restart; after a refused launch it has no run, so the +// repaint drops Stop and Restart, keeps the line and puts focus on the drawer's heading. const ENDED = [{ body: { request: END, runs: ['run-1'], cancelled: [], taken: [] }, runs: [] }, { body: closed(END, 'end') }]; for (const [label, answers, line] of [ ['not the owner', [{ status: 403, body: { error: 'unavailable', sent: false } }], /^Not restarted: this Console is not the owner of Acme Studio, so it does not restart its agents\.$/], + ['the organization is archived', [{ status: 409, body: { error: 'org-archived', message: 'organization is archived' } }], + /^Not restarted: Acme Studio is archived, so its agents are not changed\. Nothing changed\.$/], ['the bus is stopped', [{ status: 503, body: { error: 'no-bus-host', message: 'no-bus-host: no-bus-host. Nothing changed.', sent: false } }], /^Not restarted: the bus of Acme Studio is not running\. Nothing changed\.$/], ['the end is refused', [{ body: { request: END, runs: ['run-1'], cancelled: [], taken: [] } }, { body: { ...closed(END, 'end', { done: { run: null, transaction: null, refusal: 'instance-kept' } }), reason: 'the instance is kept; unkeep it first' } }], @@ -843,7 +884,7 @@ for (const [label, answers, line] of [ /^coder was ended and not launched: the instance has no harness, model and thinking yet; set its route first\.$/], ['its runs never reach 0', [{ body: { request: END, runs: ['run-1'], cancelled: [], taken: [] } }, { body: closed(END, 'end') }], /^Not restarted: coder was asked to end, but its session is still running\. Read the agents again before trying\.$/], -]) test(`SP19: a refusal says so in the drawer and leaves the buttons usable: ${label}`, async t => { +]) test(`SP19: a refusal says so in the drawer: ${label}`, async t => { const { b, wait } = await setup(t, { hash: '#/agents?agent=acme%3Acoder', scripts: [FAST] }); const d = '[data-ws-drawer="acme:coder"]'; await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-restart]')`); @@ -852,12 +893,20 @@ for (const [label, answers, line] of [ await b.evaluate(`document.querySelector('${d} [data-drawer-restart]').click()`); await wait(`document.querySelector('${d} [data-drawer-restart-yes]')`); await b.evaluate(`document.querySelector('${d} [data-drawer-restart-yes]').click()`); - await wait(`${status(d)} && !document.querySelector('${d} [data-drawer-restart]').disabled`); + const launched = label.startsWith('the launch'); + await wait(launched ? `/ was ended and not launched: /.test(${status(d)}) && !document.querySelector('${d} [data-drawer-restart]')` + : `/^Not restarted: /.test(${status(d)}) && document.querySelector('${d} [data-drawer-restart]')?.disabled === false`); + await b.evaluate('new Promise(r => setTimeout(r, 300))'); assert.match(await b.evaluate(status(d)), line); - assert.equal(await b.evaluate(usable(d)), true, 'both buttons are usable again'); - assert.equal(await b.evaluate(`document.activeElement === document.querySelector('${d} [data-drawer-restart]')`), true, 'focus is back on Restart'); + if (launched) { + assert.deepEqual(await b.evaluate(`[!!document.querySelector('${d} [data-drawer-stop]'), document.querySelector('${d} [data-drawer-nostop]')?.textContent]`), [false, 'It is not running, so there is nothing to stop.']); + assert.equal(await b.evaluate(`document.activeElement === document.querySelector('${d} h2')`), true, `focus is on the drawer heading, not ${await b.evaluate('document.activeElement?.outerHTML.slice(0, 80)')}`); + } else { + assert.equal(await b.evaluate(usable(d)), true, 'both buttons are usable again'); + assert.equal(await b.evaluate(`document.activeElement === document.querySelector('${d} [data-drawer-restart]')`), true, 'focus is back on Restart'); + } const posts = await b.evaluate('window.__posts'); assert.equal(posts.some(p => /regenerate/.test(p.url)), false); - assert.equal(posts.filter(p => p.url.endsWith('/agent/launch')).length, label.startsWith('the launch') ? 1 : 0); + assert.equal(posts.filter(p => p.url.endsWith('/agent/launch')).length, launched ? 1 : 0); assert.equal(await b.evaluate('window.__answers.length'), 0, 'every queued answer was asked for'); });