Skip to content
Merged
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
50 changes: 44 additions & 6 deletions ds-bot/src/host/handoffs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ??= {})
Expand Down Expand Up @@ -124,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 })
Expand All @@ -144,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)])
Comment on lines +154 to +156

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Exclude consumed results when matching subagent notices

When a subagent is continued while its earlier parent work still exists, this search keeps selecting the first run even after its settlement notice was read. takenWork() only removes IDs from work.open; it does not remove entries from work.results, which are now searched unconditionally. Consequently, turns.js routes subsequent settlement notices to the earlier parent instead of the work owning the new run. This is reproducible by settling and taking r1 under p1, then settling r2 for the same child under p2: subWorkRun() still returns r1/p1. Track which results have been consumed separately so each notice advances to the correct run.

Useful? React with 👍 / 👎.

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 }
Expand Down Expand Up @@ -225,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
}
Expand Down Expand Up @@ -278,11 +298,27 @@ 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. 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))
Comment on lines +306 to +308

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve recovery controls for aged outstanding handoffs

When a parent work is over 24 hours old and has no results, this filter hides all its Bot handoffs even if the recipient is still running or waiting for approval. It also uses the parent's age, so a newly dispatched child can disappear immediately. The underlying IDs remain in work.open, and waitsWork() still prevents the parent from answering, but waitingView() no longer exposes those waits. Since src/client/context-menu.js builds the give-up actions from waitingView().waits, the user loses the recovery action for precisely these outstanding handoffs. Keep actionable outstanding waits visible, or explicitly settle them rather than only filtering their presentation.

Useful? React with 👍 / 👎.


// 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: work.open
.map(childId => ({ id: childId, work: works()[childId] }))
.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
// these holds their wait, and the sender waits until the reply closes them.
Expand Down Expand Up @@ -329,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)
Comment on lines +368 to +370

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Apply the standalone-ask filter to the status text too

For an unanswered standalone ask older than 24 hours whose recipient is active, this change clears the sender from waitingView() but leaves statusOf() reporting waiting for … to answer. The latter still builds its sent list using only work.from === botId && onIt(work.to), without the new age check. Thus the stale waiting label remains in the status returned to other Bots through list_bots, despite the new test passing for the sidebar projection. Apply the same eligibility predicate to both projections so the reported status actually clears.

Useful? React with 👍 / 👎.

return view
}

Expand Down
80 changes: 80 additions & 0 deletions ds-bot/test/handoff-results.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,83 @@ 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}`)
})

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')
})
Loading