fix(webui): sp16 review 2, the drawer shows the state as soon as it is read and the template line holds still: when the instances read answers after the page, the drawer repaints at once with the state, and a pack hire's template line says being read until its packs read answers and repaints it (data-drawer-pending names <n> for the instances read and <n>p for the packs read, and a repaint waiting on the fresh drawer runs at the swap); the packs answer is kept per organization for 60 s and a repaint draws from it, also while a newer read is open, so the line no longer goes back to being read on every poll and one read serves the repaints; a drawer whose reads have all answered is no longer marked pending and swapped after paint; the Role row of a pack hire says Hired and the pack is named once, in Hired from; the unreachable holds-no-conversation line is gone; Last activity for a conversation agent reads not shown: reading it means reading the whole conversation; tests pin one packs read across three repaints, the state shown while packs is held, and no pending marker when all reads are in (#1873, lane sp16-drawer-actions)

This commit is contained in:
jason.woltje committed 2026-11-16 11:46:38 -06:00
1 parent d92cdefc1d
commit 70af20e29d
2 files changed
+151 -68

No files matched your search

+83 -62
View File
@@ -1141,45 +1141,44 @@
}
// SP13 (#1873): the drawer's instances read gives the agent's state. The drawer does not hold the
// page for it: once the page's own reads have answered (`opts.page`), a drawer whose instances read
// has not says its state is being read, and repaints when the read answers. That answer is kept for
// the one repaint it asks for; a repaint while the read is still open waits on the same read.
// has not says its state is being read, and repaints when the read answers. A repaint while the read
// is still open waits on the same read; once it answers it is let go, so the next render reads again.
let drawerInst = null, drawerReads = 0; // { org, n, done, value, p }
// Repaints whose read answered before their drawer was on screen, keyed `<read n>:<agent id>`. A
// drawer never painted (closed, or replaced by a later paint) leaves its entry; the oldest go first.
// Repaints whose read answered before their drawer was on screen, keyed by the drawer's
// data-drawer-pending and its agent id. A drawer never painted (closed, or replaced by a later paint)
// leaves its entry; the oldest go first.
const drawerLate = new Map(), DRAWER_LATE_MAX = 8;
const drawerInstRead = org => {
if (drawerInst?.org === org) return drawerInst;
const r = { org, n: ++drawerReads, done: false, value: null };
r.p = data.instances(org).then(v => ({ inst: instancesOf(v), copies: copiesOf(v) }), err => ({ inst: null, copies: {}, err }))
.then(v => { r.done = true; r.value = v; return v; });
r.p.then(() => { if (drawerInst === r) drawerInst = null; });
drawerInst = r;
return r;
};
// SP16 (#1873): the template a pack hire was hired from. Only the packs read names it (owner only):
// the pack's hire of this name points at a definition, and that definition at a template. It is read
// only for an organization agent the instances read says a pack hired, once per instances read, so a
// repaint from the same read reads nothing again (each poll's instances read is new, and so is its packs
// read). The pack is the one installed now; the read does not say which version the hire was made from.
// only for an organization agent the instances read says a pack hired. The answer is kept per
// organization for DRAWER_PACKS_KEEP ms, and a repaint draws from the kept answer, also while a newer
// read is open, so the line does not change on every poll and a poll does not read packs each time.
// The pack is the one installed now; the read does not say which version the hire was made from.
// It never rejects: a refusal, no answer or an answer that is not the packs list each give `err`.
const drawerPacks = (org, ir, key) => {
const item = ir?.value?.inst?.[key];
if (!item || !isHired(item) || typeof item.pack?.id !== 'string') return Promise.resolve(null);
return (ir.packs ??= fetch(`/api/orgs/${enc(org)}/packs`, { cache: 'no-store', redirect: 'manual' }).then(async r => {
if (!r.ok || r.type === 'opaqueredirect') return { err: { status: r.type === 'opaqueredirect' ? 0 : r.status } };
const v = await r.json().catch(() => null);
return Array.isArray(v?.packs) ? { packs: v.packs } : { err: { code: 'no answer' } };
}).catch(() => ({ err: { code: 'no answer' } })));
};
// The reads one agent's drawer waits on: its organization's instances read, then, for a pack hire, the
// packs read. Kept per agent on the instances read, so every open of that agent shares them.
const drawerFull = (org, ir, key) => {
const all = (ir.full ??= new Map());
if (!all.has(key)) {
const f = { done: false, packs: null };
f.p = ir.p.then(() => drawerPacks(org, ir, key)).then(packs => { f.done = true; f.packs = packs; return packs; });
all.set(key, f);
const drawerPacksKept = new Map(), DRAWER_PACKS_KEEP = 60000; // org -> { at, value, p }
const drawerPacks = (org, instValue, key) => {
const item = instValue?.inst?.[key];
if (!item || !isHired(item) || typeof item.pack?.id !== 'string') return null;
let k = drawerPacksKept.get(org);
if (!k) drawerPacksKept.set(org, (k = { at: 0, value: null, p: null }));
if (!k.p && (!k.value || Date.now() - k.at >= DRAWER_PACKS_KEEP)) {
const kept = k;
kept.p = fetch(`/api/orgs/${enc(org)}/packs`, { cache: 'no-store', redirect: 'manual' }).then(async r => {
if (!r.ok || r.type === 'opaqueredirect') return { err: { status: r.type === 'opaqueredirect' ? 0 : r.status } };
const v = await r.json().catch(() => null);
return Array.isArray(v?.packs) ? { packs: v.packs } : { err: { code: 'no answer' } };
}).catch(() => ({ err: { code: 'no answer' } })).then(v => { kept.value = v; kept.at = Date.now(); kept.p = null; return v; });
}
return all.get(key);
return { value: k.value, p: k.p };
};
// The pack's own label and the template its hire of this name is made from, from the packs read. The
// instance names only the pack's id; the installed pack's document carries its label.
@@ -1190,6 +1189,30 @@
const t = entry?.template ?? entry?.from?.template;
return { template: typeof t === 'string' && t ? t : '', label: typeof doc?.label === 'string' && doc.label ? doc.label : '' };
};
// Swaps a drawer still on screen for `html`, keeping focus, and runs at once a repaint already waiting
// on the fresh drawer's marker (its packs read answered before this drawer was painted).
const drawerSwap = (node, html) => {
const key = focusKey(node), tpl = document.createElement('template');
tpl.innerHTML = html;
const fresh = tpl.content.firstElementChild;
if (!fresh) return;
node.replaceWith(fresh);
refocus(fresh, key);
drawerLateRun(fresh);
};
function drawerLateRun(node) {
const k = `${node.dataset.drawerPending}:${node.dataset.wsDrawer}`, redo = node.dataset.drawerPending ? drawerLate.get(k) : null;
if (redo) { drawerLate.delete(k); redo(node); }
}
// Repaints the drawer that carries `mark` once `p` answers: now if it is on screen, else once it is
// painted. `build(answer)` runs when the read answers and gives what draws the drawer at the swap.
const drawerWhen = (p, mark, id, build) => p.then(v => {
const draw = build(v), redo = node => drawerSwap(node, draw());
const node = document.querySelector(`#bus-view [data-ws-drawer="${CSS.escape(id)}"][data-drawer-pending="${mark}"]`);
if (node) { redo(node); return; }
drawerLate.set(`${mark}:${id}`, redo);
while (drawerLate.size > DRAWER_LATE_MAX) drawerLate.delete(drawerLate.keys().next().value);
});
async function agentDrawer(q, opts = {}) {
const pick = agentPick(q);
if (!pick || (opts.only && pick.org !== opts.only)) return '';
@@ -1201,36 +1224,37 @@
typeof opts.configIn === 'function' ? Promise.resolve(opts.configIn(org)).catch(() => null) : null,
ensureThemes(),
]);
// SP16 review: the packs read is raced against the page's reads as the instances read is, so a slow
// packs read holds only the drawer's template line, never the page's paint.
const full = drawerFull(org, ir, pick.key);
await Promise.race([full.p, opts.page ?? full.p]);
const pending = !full.done;
// SP14 (#1873): when the read answers, only the drawer repaints, from the reads it already has; the
// page's own reads are not made again. Each open repaints its own drawer if it is still on screen and
// still waits on this read (data-drawer-pending names the read), so a second agent opened while the
// read was open repaints too. A closed drawer repaints nothing, and the answer is not kept for a later open.
// SP16: the read can answer after this HTML is returned and before the page that carries it is on
// screen. Then the repaint waits in drawerLate, and afterPaint runs it once that drawer is painted.
// The instances read is let go once it answers, even while a packs read is open, so a later render
// reads the state again instead of keeping this answer for as long as the packs read takes.
if (pending) ir.p.then(() => { if (drawerInst === ir) drawerInst = null; });
if (pending) full.p.then(packs => {
const redo = node => {
const key = focusKey(node), tpl = document.createElement('template');
tpl.innerHTML = drawerHtml(pick, { body, business, config, pending: false, instRead: ir.value, packs });
const fresh = tpl.content.firstElementChild;
if (!fresh) return;
node.replaceWith(fresh);
refocus(fresh, key);
};
const node = document.querySelector(`#bus-view [data-ws-drawer="${CSS.escape(id)}"][data-drawer-pending="${ir.n}"]`);
if (node) { redo(node); return; }
drawerLate.set(`${ir.n}:${id}`, redo);
while (drawerLate.size > DRAWER_LATE_MAX) drawerLate.delete(drawerLate.keys().next().value);
});
else if (drawerInst === ir) drawerInst = null;
return drawerHtml(pick, { body, business, config, pending: pending && ir.n, instRead: ir.done ? ir.value : null, packs: full.packs });
const page = opts.page ?? null;
const html = (instRead, packs, pending) => drawerHtml(pick, { body, business, config, pending, instRead, packs });
// Each read the drawer waits on is raced against the page's reads (SP16 review), so neither the
// instances read nor a pack hire's packs read holds the page's paint.
await Promise.race([ir.p, page ?? ir.p]);
// SP14 (#1873): when a read answers, only the drawer repaints, from the reads it already has; the
// page's own reads are not made again. data-drawer-pending names the read it waits on: `<n>` for the
// instances read, `<n>p` for the packs read after it, so each repaint finds only its own drawer. A
// closed drawer repaints nothing. SP16: a read can answer after this HTML is returned and before the
// page that carries it is on screen; then the repaint waits in drawerLate, and afterPaint runs it.
// When the instances read answers, the state is drawn at once, and the template line, if its packs
// read is still open with nothing kept, says it is being read and repaints when that read answers.
const packsStage = instRead => {
const pk = drawerPacks(org, instRead, pick.key);
if (pk?.p && !pk.value) drawerWhen(pk.p, `${ir.n}p`, id, v => () => html(instRead, v, false));
return pk?.p && !pk.value ? { packs: null, pending: `${ir.n}p` } : { packs: pk?.value ?? null, pending: false };
};
if (!ir.done) {
drawerWhen(ir.p, ir.n, id, v => { const s = packsStage(v); return () => html(v, s.packs, s.pending); });
return html(null, null, ir.n);
}
let pk = drawerPacks(org, ir.value, pick.key);
if (pk?.p && !pk.value) {
await Promise.race([pk.p, page ?? pk.p]);
pk = drawerPacks(org, ir.value, pick.key);
}
if (pk?.p && !pk.value) {
drawerWhen(pk.p, `${ir.n}p`, id, v => () => html(ir.value, v, false));
return html(ir.value, null, `${ir.n}p`);
}
return html(ir.value, pk?.value ?? null, false);
}
function drawerHtml(pick, { body, business, config, pending, instRead, packs = null }) {
const { org, key, id } = pick;
@@ -1296,7 +1320,6 @@
else if (chat) acts.push(`<p class="note" data-drawer-nomessage>${reading ? 'No Message: its state is being read, so it is not known whether it holds a conversation.'
: unknown ? `No Message: its state was not read (${esc(instWhy(instRead || {}))}), so it is not known whether it holds a conversation.`
: a.level !== 'org' ? 'No Message: a copy is not messaged from here.'
: window.MosaicAgents?.isThread?.(def) ? `No Message: ${esc(a.name)} holds no conversation now.`
: 'No Message: this Console messages only conversation agents, such as the Assistant.'}</p>`);
else if (change) acts.push(`<p class="note" data-drawer-nomessage>No Message: this Console does not send chat in ${esc(orgName)}.</p>`);
if (change && a.level === 'org' && !a.keep && running(item) > 0) acts.push(stopActions(pick, a, orgName));
@@ -1313,7 +1336,8 @@
: `<p class="note" data-drawer-readonly>This Console only reads ${esc(orgName)}. It can not message or stop its agents.</p>`;
const fields = [
field('org', 'Organization', orgChip(o, org)),
unknown ? '' : field('role', 'Role', `${esc(a.role)}`),
// A pack hire's pack is named once, in Hired from; the Role row says only that it was hired.
unknown ? '' : field('role', 'Role', `${esc(hiredFrom && packOf(item) ? 'Hired' : a.role)}`),
unknown ? '' : field('source', 'Source', source),
hiredFrom ? field('hired', 'Hired from', hiredFrom) : '',
field('state', 'State', unknown ? `<span class="muted">${unknown}</span>` : dot(...a.dot, true)),
@@ -1324,7 +1348,7 @@
// read's time is when it took the role and `since` is when its present state began, so neither is shown.
// An agent holding a conversation has its messages in that thread's messages read, but that read pages
// from the oldest message, so its last message is a walk of every page away; the drawer does not make it.
field('activity', 'Last activity', `<span class="muted">${thread?.thread ? 'not read here: its conversation’s messages read starts at the oldest message' : 'not recorded'}</span>`),
field('activity', 'Last activity', `<span class="muted">${thread?.thread ? 'not shown: reading it means reading the whole conversation' : 'not recorded'}</span>`),
].join('');
return shell(a.name, `<dl class="ws-drawer-fields">${fields}</dl>
<section class="ws-drawer-sec" data-drawer-tasks aria-label="Open tasks assigned to ${esc(a.name)}"><h3>Open tasks</h3>${tasksBody}${tasksAll}</section>
@@ -1342,10 +1366,7 @@
// bus.js calls this after each paint: focus moves into a drawer a row opened, and back to the row
// when the drawer has closed and focus was left nowhere.
function afterPaint() {
for (const node of document.querySelectorAll('#bus-view [data-ws-drawer][data-drawer-pending]')) {
const k = `${node.dataset.drawerPending}:${node.dataset.wsDrawer}`, redo = drawerLate.get(k);
if (redo) { drawerLate.delete(k); redo(node); }
}
for (const node of document.querySelectorAll('#bus-view [data-ws-drawer][data-drawer-pending]')) drawerLateRun(node);
const view = document.getElementById('bus-view'), d = view?.querySelector('[data-ws-drawer]');
const now = d?.dataset.wsDrawer || null, was = drawerShown;
drawerShown = now;