Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Key a delegated subagent's sub-work by its run, not the child

Fei2411f3de74094e1f
diff --git a/ds-bot/src/host/handoffs.js b/ds-bot/src/host/handoffs.js
index dd7cff91..0dbaa812 100644
--- a/ds-bot/src/host/handoffs.js
+++ b/ds-bot/src/host/handoffs.js
@@ -95,10 +95,12 @@ export function install(rt) {
const workOf = info => (info === undefined ? undefined
: info.role === 'request' && info.audience !== 'user' ? info.exchangeId : info.work)
- // `parent`: the work the sender was doing when it sent this one.
- function openWork(id, { from, to, parent, fromRoom, room, name }) {
+ // `parent`: the work the sender was doing when it sent this one. `child`: the
+ // session id of the subagent a run of this work belongs to — the run, not the
+ // child, is the sub-work, so a child that is continued hands up a result per run.
+ function openWork(id, { from, to, parent, fromRoom, room, name, child }) {
const above = parent === undefined ? undefined : works()[parent]
- works()[id] = { ...(from ? { from } : {}), ...(to ? { to } : {}), ...(room ? { room } : {}), ...(above ? { parent } : {}), ...(fromRoom ? { fromRoom } : {}), ...(name ? { name } : {}), open: [], results: [], at: Date.now() }
+ works()[id] = { ...(from ? { from } : {}), ...(to ? { to } : {}), ...(room ? { room } : {}), ...(above ? { parent } : {}), ...(fromRoom ? { fromRoom } : {}), ...(name ? { name } : {}), ...(child ? { child } : {}), open: [], results: [], at: Date.now() }
if (above !== undefined) above.open.push(id)
save()
}
@@ -115,7 +117,7 @@ export function install(rt) {
if (above !== undefined) {
// 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), ...(text.length > TEXT_KEPT ? { length: text.length } : {}), ...(file ? { file } : {}) })
+ 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() })
whole.set(id, text)
}
for (const result of work.results ?? []) whole.delete(result.id)
@@ -126,21 +128,37 @@ export function install(rt) {
}).catch(() => {})
return above === undefined ? undefined : work.parent
}
- // The work a delegated subagent's child hangs under, or hung under once it was
- // settled: then only the result it left there names it.
- const subWorkParent = id => works()[id]?.parent ?? Object.keys(works()).find(workId => works()[workId].results?.some(result => result.id === id))
- // A subagent's settlement is handed back like a teammate's reply: the child's
- // closing text joins the results of the work it hung under.
- function settleSubWork(id, text) {
- const work = works()[id]
+ // The subagent run a settlement notice names. The notice carries only the
+ // child's id, and a child's runs end one after another, so it is the earliest
+ // settled run of that child not read yet, else a run of it still out.
+ const subWorkRun = (id) => {
+ 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)
+ 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 }
+ }
+ }
+ return settled ?? live
+ }
+ // A settled run is handed back like a teammate's reply: its closing text joins
+ // the results of the work it hung under. `id` is the run's id, `child` the
+ // child's, which named the sub-work before runs did.
+ function settleSubWork(id, text, child) {
+ const key = works()[id] !== undefined ? id : child
+ const work = works()[key]
if (work === undefined) return undefined
const { parent } = work
- closeWork(id, work.name ?? 'subagent', text)
+ closeWork(key, work.name ?? 'subagent', text)
return parent
}
// A fast child can settle before its tool result identifies the call that made it.
const nameSubWork = (id, name) => {
- const work = works()[id] ?? Object.values(works()).flatMap(work => work.results ?? []).find(result => result.id === id)
+ const work = Object.entries(works()).find(([key, work]) => (work.child ?? key) === id)?.[1]
+ ?? Object.values(works()).flatMap(work => work.results ?? []).find(result => (result.child ?? result.id) === id)
if (work === undefined || work.name === name) return
work.name = name
save()
@@ -308,5 +326,5 @@ export function install(rt) {
return lines.join('; ')
}
- Object.assign(rt, { workOf, openWork, closeWork, dropWork, takenWork: taken, lateWork, answerMissed, ownsWork: owns, waitsWork: waits, holdWork: hold, workAsker: askedBy, answerText, upstreamOf, sameWork, forgetBotWork: forgetBot, statusOf, waitingView, settleSubWork, subWorkParent, nameSubWork })
+ Object.assign(rt, { workOf, openWork, closeWork, dropWork, takenWork: taken, lateWork, answerMissed, ownsWork: owns, waitsWork: waits, holdWork: hold, workAsker: askedBy, answerText, upstreamOf, sameWork, forgetBotWork: forgetBot, statusOf, waitingView, settleSubWork, subWorkRun, nameSubWork })
}
diff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.js
index 55d17f5e..6aa8db55 100644
--- a/ds-bot/src/host/turns.js
+++ b/ds-bot/src/host/turns.js
@@ -556,13 +556,15 @@ export function install(rt) {
} else if (source?.kind === REFERENCE_KIND && rt.roomOf(id)) {
attachReference(roomInbox.get(id), source, textOf(event.data.content))
} else if (source?.kind === 'subagent-settled') {
- // A settled sub-work's notice: the work it hung under goes on once a turn
- // reads it, like a reply that came back. One read while a turn does other
- // work comes back in a turn of its own, like any handed-back result.
- const parent = rt.subWorkParent?.(source.senderSessionId)
- if (parent !== undefined) {
- rt.takenWork?.(parent, source.senderSessionId)
- const info = { kind: SOURCE_KIND, role: 'reply', work: parent, replyTo: source.senderSessionId, exchangeId: source.senderSessionId, senderSessionId: source.senderSessionId }
+ // A settled sub-run's notice: the work it hung under goes on once a turn
+ // reads it, like a reply that came back. The notice names only the child;
+ // its run is the earliest of the child's not read yet. One read while a
+ // turn does other work comes back in a turn of its own, like any
+ // handed-back result.
+ const run = rt.subWorkRun?.(source.senderSessionId)
+ if (run !== undefined) {
+ rt.takenWork?.(run.parent, run.id)
+ const info = { kind: SOURCE_KIND, role: 'reply', work: run.parent, replyTo: run.id, exchangeId: run.id, senderSessionId: source.senderSessionId }
if (entry.turn === undefined) entry.pending = info
else if (entry.info === undefined && !opened) {
// DSH opens the turn before it records the message that filled its
@@ -571,7 +573,7 @@ export function install(rt) {
entry.opened = true
entry.answers = true
claimTrigger(id, entry, info)
- } else if (rt.workOf?.(entry.info) !== parent) entry.later = [...(entry.later ?? []), { info, text: textOf(event.data.content) }]
+ } else if (rt.workOf?.(entry.info) !== run.parent) entry.later = [...(entry.later ?? []), { info, text: textOf(event.data.content) }]
}
} else if (source?.kind === 'user' || source?.kind === REPLY_KIND) {
if (rt.roomOf(id) && source.kind === 'user') {
@@ -762,9 +764,11 @@ export function install(rt) {
}
})
- // A subagent a Bot's turn delegates to is work handed on, like message_bot: the
- // child hangs under the work the turn answers until its settlement notice comes
- // back, and its closing message joins that work's results. These are Cordis
+ // A subagent a Bot's turn delegates to is work handed on, like message_bot: each
+ // run of the child hangs under the work the turn answers until its settlement
+ // notice comes back, and its closing message joins that work's results. A child
+ // can be sent more to do once it settled, and that is a fresh run on the same
+ // child id, so the run — not the child — is the sub-work. These are Cordis
// events, not Session events: the dispatch keys `this` by the delegating parent
// Agent, which `carrierKeyOf` hands back.
ctx.on('subagent/start', function (info) {
@@ -772,13 +776,14 @@ export function install(rt) {
const entry = id === undefined ? undefined : live.get(id)
const work = rt.workOf?.(entry?.info)
const self = id === undefined ? undefined : rt.selfOf(id)
- if (work === undefined || self === undefined || !rt.chatOwners.has(id) || typeof info?.id !== 'string' || !rt.ownsWork?.(self, work)) return
- rt.openWork?.(info.id, { from: self, parent: work, name: 'subagent' })
+ if (work === undefined || self === undefined || !rt.chatOwners.has(id) || typeof info?.id !== 'string' || typeof info?.runId !== 'string' || !rt.ownsWork?.(self, work)) return
+ rt.openWork?.(info.runId, { from: self, parent: work, name: 'subagent', child: info.id })
})
- // A settled child closes its sub-work: its closing text joins the results of
+ // A settled run closes its sub-work: its closing text joins the results of
// the work it hung under.
ctx.on('subagent/end', (info) => {
- if (typeof info?.id === 'string') rt.settleSubWork?.(info.id, subEndText(info))
+ if (typeof info?.id !== 'string') return
+ rt.settleSubWork?.(typeof info.runId === 'string' ? info.runId : info.id, subEndText(info), info.id)
})
ctx.on('tools/result', (exec, result) => {
if (!SUBAGENT_TOOLS.has(exec?.name) || result?.isError || typeof result?.value?.subagentId !== 'string') return
diff --git a/ds-bot/test/handoff-results.test.js b/ds-bot/test/handoff-results.test.js
index b2d6b89b..74ee5576 100644
--- a/ds-bot/test/handoff-results.test.js
+++ b/ds-bot/test/handoff-results.test.js
@@ -402,7 +402,7 @@ test('a subagent a Bot delegated to hands its result into the work the turn answ
assert.equal(callsOf(agents, 'a').length, 0)
const works = await worksIn(first)
const [work] = Object.values(works).filter(entry => entry.to === 'b')
- assert.deepEqual(work.open, ['sub1'])
+ assert.deepEqual(work.open, ['r1'])
// The child settles; its closing message is the sub-work's result. DSH opens
// the turn first, then records the notice that filled its inbox.
emitScoped('b', 'subagent/end', { runId: 'r1', provider: 'spawn', id: 'sub1', local: true, stopReason: 'completed', lastAssistantMessage: textBlock('reviewed: three findings') })
@@ -481,8 +481,8 @@ test('parallel delegations get each call\'s own description for its child', asyn
emit('b', 'tool/result', { callId: 'call2', message: { callId: 'call2', content: textBlock('started subagent sub2'), isError: false } })
turns.end('b', 'subagents are on it')
const works = await worksIn(first)
- assert.equal(works.sub1.name, 'subagent deep review')
- assert.equal(Object.values(works).find(work => work.to === 'b').results.find(result => result.id === 'sub2').name, 'subagent draft summary')
+ assert.equal(Object.values(works).find(work => work.child === 'sub1').name, 'subagent deep review')
+ assert.equal(Object.values(works).find(work => work.to === 'b').results.find(result => result.child === 'sub2').name, 'subagent draft summary')
emit('b', 'turn/start', { turn: 2 })
emit('b', 'user/message', { content: textBlock('Background subagent sub2 finished.\nIts closing message:\na summary'), source: { kind: 'subagent-settled', form: 'notice', summary: 'Background subagent sub2 finished.', senderSessionId: 'sub2' } })
emit('b', 'assistant/message', assistantText('summary back, review still out'))
@@ -499,3 +499,43 @@ test('parallel delegations get each call\'s own description for its child', asyn
assert.ok(answer.includes('subagent draft summary:\na summary'), `first child's own name: ${answer}`)
assert.ok(answer.includes('subagent deep review:\nthree findings'), `second child's own name: ${answer}`)
})
+
+// A child that finished can be sent more to do: DSH starts a fresh run on the
+// same child id, with a new runId. Each run's result is its own, so the first
+// one's is not lost to the second's.
+test('a continued subagent hands up each run\'s own result', async (t) => {
+ const { start } = bench(t)
+ const first = await start(seed())
+ const { agents, emit, emitScoped, tool } = first
+ const turns = turnTaker(agents, emit)
+ turns.userTurn('a', 'have Writer do it')
+ await tool('message_bot', { to: 'Writer', message: 'carry out the plan' }, 'a')
+ turns.end('a', 'asked Writer')
+ turns.take('b')
+ emit('b', 'tool/call', { callId: 'call1', name: 'subagent', arguments: JSON.stringify({ description: 'checks', prompt: 'check the plan' }) })
+ emitScoped('b', 'subagent/start', { runId: 'r1', provider: 'spawn', id: 'sub1', local: true })
+ emitScoped('b', 'tools/result', { callId: 'call1', name: 'subagent', arguments: { description: 'checks', prompt: 'check the plan' }, agent: agents.get('b') }, { isError: false, value: { kind: 'activation', subagentId: 'sub1' } })
+ emit('b', 'tool/result', { callId: 'call1', message: { callId: 'call1', content: textBlock('started subagent sub1'), isError: false } })
+ turns.end('b', 'subagent is on it')
+ // The first run settles and the Bot reads its notice, then sends the same
+ // child more work: DSH opens a second run on the same child id.
+ emitScoped('b', 'subagent/end', { runId: 'r1', provider: 'spawn', id: 'sub1', local: true, stopReason: 'completed', lastAssistantMessage: textBlock('COST: 42') })
+ emit('b', 'turn/start', { turn: 2 })
+ emit('b', 'user/message', { content: textBlock('Background subagent sub1 finished.\nIts closing message:\nCOST: 42'), source: { kind: 'subagent-settled', form: 'notice', summary: 'Background subagent sub1 finished.', senderSessionId: 'sub1' } })
+ emitScoped('b', 'subagent/start', { runId: 'r2', provider: 'spawn', id: 'sub1', local: true })
+ emit('b', 'assistant/message', assistantText('sent it the schedule too'))
+ emit('b', 'turn/end', { turn: 2, reason: { kind: 'completed' } })
+ // The answer still waits while the second run is out.
+ await delay(50)
+ assert.equal(callsOf(agents, 'a').length, 0)
+ emitScoped('b', 'subagent/end', { runId: 'r2', provider: 'spawn', id: 'sub1', local: true, stopReason: 'completed', lastAssistantMessage: textBlock('SCHEDULE: Monday') })
+ emit('b', 'turn/start', { turn: 3 })
+ emit('b', 'user/message', { content: textBlock('Background subagent sub1 finished.\nIts closing message:\nSCHEDULE: Monday'), source: { kind: 'subagent-settled', form: 'notice', summary: 'Background subagent sub1 finished.', senderSessionId: 'sub1' } })
+ emit('b', 'assistant/message', assistantText('all back'))
+ emit('b', 'turn/end', { turn: 3, reason: { kind: 'completed' } })
+ await settled(() => callsOf(agents, 'a').length === 1)
+ const answer = textOf(callsOf(agents, 'a')[0][1].content)
+ assert.ok(answer.includes('COST: 42'), `the first run's result goes up: ${answer}`)
+ 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}`)
+})