From ab627b546e86cbea7edf9637d2fe7ec3ee40ed5b Mon Sep 17 00:00:00 2001 From: Steve Dempsey Date: Tue, 10 Nov 2026 16:04:54 -0500 Subject: [PATCH 1/3] Stop showing a work as waiting once its reply has arrived The status line other Bots read named a hand-off as still open whenever the work kept an open id, even after that reply had come back. Nothing cleared the id when the answer arrived by another route, so an idle Bot looked loaded on work that had ended days before, and every job watch chased it. A work now stops counting as waiting when the reply for an open id is already on it, and when it has sat past a day with no result at all. A fresh work with no reply still shows as waiting. --- ds-bot/src/host/handoffs.js | 19 +++++++++- ds-bot/test/handoff-results.test.js | 57 +++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/ds-bot/src/host/handoffs.js b/ds-bot/src/host/handoffs.js index 4054b0d384..fc689682c7 100644 --- a/ds-bot/src/host/handoffs.js +++ b/ds-bot/src/host/handoffs.js @@ -16,6 +16,10 @@ const minutes = since => { return count < 1 ? 'for less than a minute' : `for ${count} min` } +// How long a work with no result at all may still count as waiting. Past this the +// reply is taken as lost, so its Bot stops showing as loaded on work that ended. +const WAIT_CEILING_MS = 24 * 60 * 60 * 1000 + export function install(rt) { const { ctx, state } = rt const works = () => (state.works ??= {}) @@ -278,11 +282,24 @@ export function install(rt) { // Two requests from one sender belong to the same work when they were sent for it. const sameWork = (id, parent) => (works()[id]?.parent ?? undefined) === (works()[parent] === undefined ? undefined : parent) + // A work stops waiting when its reply has arrived, or when it has sat past the + // ceiling with no result at all. An open id left behind by a reply that came back + // another way must not make its Bot look loaded for days. + const arrived = (work, childId) => (work.results ?? []).some(result => result.id === childId) + const pastCeiling = work => Date.now() - (work.at ?? 0) > WAIT_CEILING_MS + const forgotten = work => (work.results ?? []).length === 0 && pastCeiling(work) + // The works their Bots wait on, outside a turn that does them (`busy`): each with what // it handed on that has not come back yet. One that came back waits only to be read. const held = busy => Object.entries(works()) .filter(([id, work]) => work.to !== undefined && work.open.length > 0 && !busy.has(id)) - .map(([id, work]) => ({ id, work, out: work.open.map(childId => ({ id: childId, work: works()[childId] })).filter(child => child.work !== undefined) })) + .map(([id, work]) => ({ + id, + work, + out: (forgotten(work) ? [] : work.open.filter(childId => !arrived(work, childId))) + .map(childId => ({ id: childId, work: works()[childId] })) + .filter(child => child.work !== undefined), + })) const busyWith = sessionIds => new Set(sessionIds.map(sessionId => rt.live.get(sessionId)).filter(entry => entry?.turn !== undefined).map(entry => workOf(entry.info)).filter(Boolean)) // What a Bot sent from outside any work, such as its chat with the user: nothing above // these holds their wait, and the sender waits until the reply closes them. diff --git a/ds-bot/test/handoff-results.test.js b/ds-bot/test/handoff-results.test.js index 9ec4072a1a..85daad2b2f 100644 --- a/ds-bot/test/handoff-results.test.js +++ b/ds-bot/test/handoff-results.test.js @@ -583,3 +583,60 @@ test('a continued subagent hands up each run\'s own result', async (t) => { assert.ok(answer.includes('SCHEDULE: Monday'), `the second run's result goes up: ${answer}`) assert.ok(answer.indexOf('COST: 42') < answer.indexOf('SCHEDULE: Monday'), `in run order: ${answer}`) }) + +// The label a Bot shows other Bots, over a hand-off it is done with. A work whose reply +// came back another way kept its open id, so the asker looked loaded for days. +function statusBench(t) { + const { home } = bench(t) + const names = { a: { name: 'Chief' }, b: { name: 'Writer' }, c: { name: 'Tester' } } + const rt = { + home, + state: { works: {} }, + botOf: id => names[id], + readOnly: () => undefined, + warn() {}, + save: async () => {}, + chatOf: id => id, + groupSessionsOf: () => [], + roomOf: () => undefined, + groupOwners: new Map(), + live: new Map(), + approvalsView: () => [], + switchJobs: () => [], + unreadOf: () => [], + } + installHandoffs(rt) + return rt +} + +test('a work whose reply already arrived does not show as waiting', async (t) => { + const rt = statusBench(t) + rt.openWork('parent', { from: 'a', to: 'b' }) + rt.openWork('child', { from: 'b', to: 'c', parent: 'parent' }) + // The reply is on the work, but its open id was never taken. + rt.state.works.parent.results = [{ id: 'child', name: 'Tester', text: 'done', length: 4 }] + // The Bot it went to is running a turn, so nothing else hides the wait. + rt.live.set('c', { turn: {} }) + const line = rt.statusOf('b') + assert.doesNotMatch(line, /to hand back work/, `an answered work is not waited on: ${line}`) + assert.match(line, /whose results came back/, `it says the results are back: ${line}`) +}) + +test('a work past the ceiling with no result stops showing as waiting', async (t) => { + const rt = statusBench(t) + rt.openWork('parent', { from: 'a', to: 'b' }) + rt.openWork('child', { from: 'b', to: 'c', parent: 'parent' }) + rt.state.works.parent.at = Date.now() - 25 * 60 * 60 * 1000 + rt.live.set('c', { turn: {} }) + const line = rt.statusOf('b') + assert.doesNotMatch(line, /to hand back work/, `a work past the ceiling is not waited on: ${line}`) +}) + +test('a fresh work with no reply still shows as waiting', async (t) => { + const rt = statusBench(t) + rt.openWork('parent', { from: 'a', to: 'b' }) + rt.openWork('child', { from: 'b', to: 'c', parent: 'parent' }) + rt.live.set('c', { turn: {} }) + const line = rt.statusOf('b') + assert.match(line, /waiting for Tester to hand back work/, `a real wait is still named: ${line}`) +}) From 348bfc1c4372940796aac6df75891ccc32b7eddf Mon Sep 17 00:00:00 2001 From: Steve Dempsey Date: Tue, 10 Nov 2026 16:07:52 -0500 Subject: [PATCH 2/3] Keep a subagent wait when its hand-off work is old The ceiling first applied to every open id, so a work holding a subagent run for more than a day dropped that wait and took the whole entry out of the wait view, which is the view a restart relies on. A subagent run has no Bot to hand back, so the arrived and ceiling rules now apply only to Bot hand-offs. --- ds-bot/src/host/handoffs.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ds-bot/src/host/handoffs.js b/ds-bot/src/host/handoffs.js index fc689682c7..7135a87806 100644 --- a/ds-bot/src/host/handoffs.js +++ b/ds-bot/src/host/handoffs.js @@ -284,10 +284,12 @@ export function install(rt) { // A work stops waiting when its reply has arrived, or when it has sat past the // ceiling with no result at all. An open id left behind by a reply that came back - // another way must not make its Bot look loaded for days. + // another way must not make its Bot look loaded for days. A subagent run has no Bot + // to hand back, so neither rule applies to it. const arrived = (work, childId) => (work.results ?? []).some(result => result.id === childId) const pastCeiling = work => Date.now() - (work.at ?? 0) > WAIT_CEILING_MS const forgotten = work => (work.results ?? []).length === 0 && pastCeiling(work) + const stillOwed = (work, childId, child) => child.to === undefined || (!arrived(work, childId) && !forgotten(work)) // The works their Bots wait on, outside a turn that does them (`busy`): each with what // it handed on that has not come back yet. One that came back waits only to be read. @@ -296,9 +298,10 @@ export function install(rt) { .map(([id, work]) => ({ id, work, - out: (forgotten(work) ? [] : work.open.filter(childId => !arrived(work, childId))) + out: work.open .map(childId => ({ id: childId, work: works()[childId] })) - .filter(child => child.work !== undefined), + .filter(child => child.work !== undefined) + .filter(child => stillOwed(work, child.id, child.work)), })) const busyWith = sessionIds => new Set(sessionIds.map(sessionId => rt.live.get(sessionId)).filter(entry => entry?.turn !== undefined).map(entry => workOf(entry.info)).filter(Boolean)) // What a Bot sent from outside any work, such as its chat with the user: nothing above From e1fab58a43bf55820f756d3bfab6990f903bee34 Mon Sep 17 00:00:00 2001 From: Steve Dempsey Date: Tue, 10 Nov 2026 16:29:36 -0500 Subject: [PATCH 3/3] Close a handoff when its reply is written, not when it is read A work kept the open id until the Bot that asked got round to reading the answer, so a row read open with its result already attached and every watch saw a phantom handoff. Writing the reply now clears the open id in the same save that attaches the result. A reply that never reaches the asker opens the wait again until the notice carrying it is read, which is the case the follow-through tests already pinned. An ask sent outside any work now marks its sender waiting only while it is recent, so an old unanswered ask cannot label an idle Bot for days. --- ds-bot/src/host/handoffs.js | 28 +++++++++++++++++++++++----- ds-bot/test/handoff-results.test.js | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/ds-bot/src/host/handoffs.js b/ds-bot/src/host/handoffs.js index 7135a87806..ef145c27dd 100644 --- a/ds-bot/src/host/handoffs.js +++ b/ds-bot/src/host/handoffs.js @@ -128,6 +128,9 @@ export function install(rt) { for (const child of Object.values(works())) if (child.parent === id) delete child.parent const above = works()[work.parent] if (above !== undefined) { + // The work reads closed the moment the reply is recorded, not when the Bot that + // asked gets round to reading it. The result below is what carries the answer up. + above.open = (above.open ?? []).filter(other => other !== id) // The whole result goes into a file of its own before the team file names it. const file = keepResult(id, text) above.results.push({ id, name, text: headOf(text, TEXT_KEPT), ...(work.child ? { child: work.child } : {}), ...(text.length > TEXT_KEPT ? { length: text.length } : {}), ...(file ? { file } : {}), at: Date.now(), ...extra }) @@ -148,8 +151,11 @@ export function install(rt) { let settled let live for (const [workId, work] of Object.entries(works())) { - for (const openId of work.open) { - const result = work.results?.find(result => result.id === openId) + const results = work.results ?? [] + // A settled run is no longer open, so the results name it too. + const ids = new Set([...(work.open ?? []), ...results.map(result => result.id)]) + for (const openId of ids) { + const result = results.find(item => item.id === openId) if ((result?.child ?? works()[openId]?.child ?? openId) !== id) continue if (result === undefined) live ??= { id: openId, parent: workId } else if (settled === undefined || (result.at ?? 0) < settled.at) settled = { id: openId, parent: workId, at: result.at ?? 0 } @@ -229,9 +235,19 @@ export function install(rt) { // that goes up names it. function answerMissed(id) { for (const work of Object.values(works())) { - if (!work.open.includes(id)) continue + // A work reads closed as soon as its reply is recorded, so the result is the + // record that the reply belongs to it. const result = (work.results ?? []).find(item => item.id === id) - if (result !== undefined) result.failed = true + if (result === undefined) { + if (!(work.open ?? []).includes(id)) continue + save() + return + } + result.failed = true + // The reply never reached the asker, so the work waits again until the notice + // that carries the answer is read. + work.open ??= [] + if (!work.open.includes(id)) work.open.push(id) save() return } @@ -349,7 +365,9 @@ export function install(rt) { } add(entry.for, work.from) } - for (const work of sentOnOwn()) if (onIt(work.to)) add((view[work.from] ??= { bots: [], rooms: [], for: [] }).bots, work.to) + // An ask sent on its own marks its sender waiting only while it is recent: one + // whose answer never closed it must not label an idle Bot for days. + for (const work of sentOnOwn()) if (onIt(work.to) && !pastCeiling(work)) add((view[work.from] ??= { bots: [], rooms: [], for: [] }).bots, work.to) return view } diff --git a/ds-bot/test/handoff-results.test.js b/ds-bot/test/handoff-results.test.js index 85daad2b2f..e0cce62c96 100644 --- a/ds-bot/test/handoff-results.test.js +++ b/ds-bot/test/handoff-results.test.js @@ -640,3 +640,26 @@ test('a fresh work with no reply still shows as waiting', async (t) => { const line = rt.statusOf('b') assert.match(line, /waiting for Tester to hand back work/, `a real wait is still named: ${line}`) }) + +test('a handoff reads closed as soon as its reply arrives', async (t) => { + const rt = statusBench(t) + rt.openWork('parent', { from: 'a', to: 'b' }) + rt.openWork('child', { from: 'b', to: 'c', parent: 'parent' }) + assert.deepEqual(rt.state.works.parent.open, ['child'], 'the hand-off starts open') + // Writing the reply is what tells the work it is answered. + rt.closeWork('child', 'Tester', 'done') + const parent = rt.state.works.parent + assert.deepEqual(parent.open, [], 'the work reads closed when the reply arrives') + assert.equal(parent.results.length, 1, 'the reply is attached to the work') + assert.equal(parent.results[0].id, 'child', 'the attached reply is the one that answered') +}) + +test('an old ask with no answer stops labelling its sender as waiting', async (t) => { + const rt = statusBench(t) + // Asked on its own, with no work above it: the case the status line showed. + rt.openWork('ask', { from: 'a', to: 'b' }) + rt.live.set('b', { turn: {} }) + assert.ok(rt.waitingView().a !== undefined, 'a fresh ask marks its sender waiting') + rt.state.works.ask.at = Date.now() - 25 * 60 * 60 * 1000 + assert.equal(rt.waitingView().a, undefined, 'an old ask stops marking its sender waiting') +})