diff --git a/packages/webui/src/public/workspace.js b/packages/webui/src/public/workspace.js index 5cdcce423..3f5df5519 100644 --- a/packages/webui/src/public/workspace.js +++ b/packages/webui/src/public/workspace.js @@ -1162,15 +1162,27 @@ // 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. + // A kept answer that is of no use for this hire is read again at once (SP16 review 3): a refusal or no + // answer on each render, so the line recovers with the route; an answer that does not name this hire + // (a hire made after it was read) once per hire until the keep runs out, so a hire the pack really + // does not name does not read packs on every poll. `p` is given only when the drawer should repaint + // on it: nothing is kept, or the kept answer is of no use for this hire. `value` is then the kept + // refusal, or nothing (the line says being read) rather than a kept answer that does not name it. + // `start` false only looks: the drawer's second look after its race starts no read. // 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 drawerPacksKept = new Map(), DRAWER_PACKS_KEEP = 60000; // org -> { at, value, p } - const drawerPacks = (org, instValue, key) => { + const drawerPacksKept = new Map(), DRAWER_PACKS_KEEP = 60000; // org -> { at, value, p, asked } + const drawerPacks = (org, instValue, key, start = true) => { 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)) { + if (!k) drawerPacksKept.set(org, (k = { at: 0, value: null, p: null, asked: new Set() })); + const v = k.value, named = !!v && !v.err && !!packTemplate(item, key, v).template; + const expired = !v || Date.now() - k.at >= DRAWER_PACKS_KEEP; + const again = !!v && (!!v.err || (!named && !k.asked.has(key))); + if (start && !k.p && (expired || again)) { + if (expired) k.asked.clear(); + if (v && !v.err && !named) k.asked.add(key); 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 } }; @@ -1178,7 +1190,8 @@ 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 { value: k.value, p: k.p }; + const wait = k.p && !named ? k.p : null; + return { value: wait && v && !v.err ? null : v, p: wait }; }; // 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. @@ -1235,24 +1248,25 @@ // 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. + // read is still open with nothing of use kept, says it is being read (or the kept refusal) 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 (pk?.p) drawerWhen(pk.p, `${ir.n}p`, id, v => () => html(instRead, v, false)); + return { packs: pk?.value ?? null, pending: pk?.p ? `${ir.n}p` : 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) { + if (pk?.p) { await Promise.race([pk.p, page ?? pk.p]); - pk = drawerPacks(org, ir.value, pick.key); + pk = drawerPacks(org, ir.value, pick.key, false); } - if (pk?.p && !pk.value) { + if (pk?.p) { 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, `${ir.n}p`); } return html(ir.value, pk?.value ?? null, false); } diff --git a/packages/webui/tests/agent-drawer-browser.test.mjs b/packages/webui/tests/agent-drawer-browser.test.mjs index 47325af71..31b6d93c3 100644 --- a/packages/webui/tests/agent-drawer-browser.test.mjs +++ b/packages/webui/tests/agent-drawer-browser.test.mjs @@ -312,6 +312,85 @@ test('SP16 review 2: a drawer whose reads have all answered is not marked pendin assert.ok(later.every(x => x.pending === null), JSON.stringify(later)); }); +// SP16 review 3: HIRE3 adds a later hire of the Coding pack, research-3, to beta's instances read once +// window.__hire3 is set, and to the packs read's Coding hires once window.__packs3 is set. SKEW moves +// Date.now on by window.__skew ms. +const HIRE3 = `(() => { + const f = window.fetch; + const json = (r, v) => new Response(JSON.stringify(v), { status: r.status, headers: { 'content-type': 'application/json' } }); + window.fetch = async (u, i) => { + const r = await f(u, i), s = String(u); + if (window.__hire3 && /\\/api\\/orgs\\/beta\\/bus\\/instances$/.test(s) && r.ok) { + const v = await r.clone().json(); + (v.rows?.[0]?.instances ?? v.instances)['research-3'] = { definition: 'org:hire-${HIRE.replace('0f3c', '1f3c')}', hireDecision: '${HIRE.replace('0f3c', '1f3c')}', running: [], pack: { id: 'coding', origin: 'shipped', project: null } }; + return json(r, v); + } + if (window.__packs3 && /\\/api\\/orgs\\/beta\\/packs$/.test(s) && r.ok) { + const v = await r.clone().json(); + v.packs[0].pack.hires.push({ name: 'research-3', definition: 'researcher', reason: 'More research.' }); + return json(r, v); + } + return r; + }; +})();`; +const SKEW = `(() => { const n = Date.now; Date.now = () => n() + (window.__skew || 0); })();`; +const FINAL = 'Pack Coding, template researcher as the installed pack names it'; +const hiredText = key => `(document.querySelector('[data-ws-drawer="${key}"] [data-drawer-hired]')?.textContent ?? '')`; + +test('SP16 review 3: a hire made after the packs answer was kept is read again at once, and its drawer never says the pack does not name it', async t => { + const { b, wait, text } = await setup(t, { hash: '#/agents?agent=beta%3Aresearch-2', scripts: [OBS, HIRE3] }); + await wait(`${hiredText('beta:research-2')}.includes('template researcher')`); + await b.evaluate('window.__hire3 = true; window.__packs3 = true; location.hash = "#/agents?agent=beta%3Aresearch-3"'); + await wait(`${hiredText('beta:research-3')}.includes('template researcher')`); + assert.equal(await text('[data-ws-drawer="beta:research-3"] [data-drawer-hired]'), FINAL); + const seen = JSON.parse(await b.evaluate('JSON.stringify(window.__ins)')).filter(x => x.id === 'beta:research-3'); + assert.ok(seen.length >= 1 && seen.every(x => !/not named/.test(x.hired ?? '')), JSON.stringify(seen)); + assert.deepEqual(await b.evaluate('window.__packReads'), ['beta', 'beta']); +}); + +test('SP16 review 3: a hire the pack does not name is read again once, then says so without a packs read on every repaint', async t => { + const { b, wait, text } = await setup(t, { hash: '#/agents?agent=beta%3Aresearch-2', scripts: [OBS, HIRE3] }); + await wait(`${hiredText('beta:research-2')}.includes('template researcher')`); + await b.evaluate('window.__hire3 = true; location.hash = "#/agents?agent=beta%3Aresearch-3"'); + await wait(`${hiredText('beta:research-3')}.includes('not named')`); + assert.equal(await text('[data-ws-drawer="beta:research-3"] [data-drawer-hired]'), 'Pack Coding, template not named in the packs read'); + assert.deepEqual(await b.evaluate('window.__packReads'), ['beta', 'beta']); + await renders(b, wait, 2); + assert.equal(await text('[data-ws-drawer="beta:research-3"] [data-drawer-hired]'), 'Pack Coding, template not named in the packs read'); + assert.deepEqual(await b.evaluate('window.__packReads'), ['beta', 'beta']); +}); + +test('SP16 review 3: a packs read that failed is read again on the next render, so the line recovers with the route', async t => { + const { b, wait, text } = await setup(t, { hash: '#/agents?agent=beta%3Aresearch-2', scripts: [OBS, 'window.__packsStatus = 503;'] }); + await wait(`${hiredText('beta:research-2')}.includes('not read')`); + assert.equal(await text('[data-ws-drawer="beta:research-2"] [data-drawer-hired]'), 'Pack coding, template not read: no answer'); + // A kept failure is read again on each render (at most one read open), as before the answer was kept. + await new Promise(r => setTimeout(r, 700)); + const failed = await b.evaluate('window.__packReads.length'); + await b.evaluate(`window.__packsStatus = 0; document.dispatchEvent(new CustomEvent('mosaic-workspace-render'))`); + await wait(`${hiredText('beta:research-2')}.includes('template researcher')`); + assert.equal(await text('[data-ws-drawer="beta:research-2"] [data-drawer-hired]'), FINAL); + assert.equal(await b.evaluate('window.__packReads.length'), failed + 1); + // Recovered, the answer is kept again: a further repaint makes no read. + await renders(b, wait, 1); + assert.equal(await b.evaluate('window.__packReads.length'), failed + 1); +}); + +test('SP16 review 3: once the kept packs answer is 60 s old a repaint still draws it, with one new read and no swap when that read answers', async t => { + const { b, wait } = await setup(t, { hash: '#/agents?agent=beta%3Aresearch-2', scripts: [OBS, SLOW, SKEW] }); + await wait(`${hiredText('beta:research-2')}.includes('template researcher')`); + const from = await b.evaluate('window.__ins.length'); + await b.evaluate('window.__skew = 61000; window.__packsHold = true;'); + await renders(b, wait, 1); + await wait('window.__packReads.length === 2'); + await b.evaluate('window.__packsHold = false; window.__packsRelease()'); + await new Promise(r => setTimeout(r, 700)); + const later = JSON.parse(await b.evaluate(`JSON.stringify(window.__ins.slice(${from}))`)).filter(x => x.id === 'beta:research-2'); + assert.equal(later.length, 1, JSON.stringify(later)); + assert.deepEqual([later[0].hired, later[0].pending], [FINAL, null]); + assert.deepEqual(await b.evaluate('window.__packReads'), ['beta', 'beta']); +}); + test('a field with no read is left out', async t => { // beta:pm: beta's business read names no model for it, and it holds no task, so no project either. const { wait, has, text } = await setup(t, { hash: '#/agents?agent=beta%3Apm' });