A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Hand a delegated subagent's result into the turn's work
Fei2411c1f9ed51b594
diff --git a/ds-bot/src/host/handoffs.js b/ds-bot/src/host/handoffs.jsindex 2cd5d199..6092c302 100644--- a/ds-bot/src/host/handoffs.js+++ b/ds-bot/src/host/handoffs.js@@ -96,9 +96,9 @@ export function install(rt) {: 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 }) {+ function openWork(id, { from, to, parent, fromRoom, room, name }) {const above = parent === undefined ? undefined : works()[parent]- works()[id] = { ...(from ? { from } : {}), ...(to ? { to } : {}), ...(room ? { room } : {}), ...(above ? { parent } : {}), ...(fromRoom ? { fromRoom } : {}), open: [], results: [], at: Date.now() }+ works()[id] = { ...(from ? { from } : {}), ...(to ? { to } : {}), ...(room ? { room } : {}), ...(above ? { parent } : {}), ...(fromRoom ? { fromRoom } : {}), ...(name ? { name } : {}), open: [], results: [], at: Date.now() }if (above !== undefined) above.open.push(id)save()}@@ -126,6 +126,19 @@ 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]+ if (work === undefined) return undefined+ const { parent } = work+ closeWork(id, work.name ?? 'subagent', text)+ return parent+ }+const dropWork = (id) => {const work = works()[id]if (work === undefined) return@@ -247,7 +260,7 @@ export function install(rt) {for (const { work, out } of held(busyWith(running))) {if (work.to !== botId) continueconst forName = nameOf(work.from) ? `, for ${nameOf(work.from)}` : ''- const on = [...new Set(out.map(child => (child.room ? `group "${rt.roomOf(child.room)?.name ?? 'a group'}"` : nameOf(child.to) ?? 'a teammate')))]+ const on = [...new Set(out.map(child => (child.room ? `group "${rt.roomOf(child.room)?.name ?? 'a group'}"` : child.name ?? nameOf(child.to) ?? 'a teammate')))]lines.push(on.length > 0 ? `waiting for ${on.join(' and ')} to hand back work${forName}` : `about to go on with work${forName}, whose results came back`)}if (lines.length === 0) lines.push('idle')@@ -262,5 +275,5 @@ export function install(rt) {return lines.join('; ')}- Object.assign(rt, { workOf, openWork, closeWork, dropWork, takenWork: taken, ownsWork: owns, waitsWork: waits, holdWork: hold, workAsker: askedBy, answerText, upstreamOf, sameWork, forgetBotWork: forgetBot, statusOf, waitingView })+ Object.assign(rt, { workOf, openWork, closeWork, dropWork, takenWork: taken, ownsWork: owns, waitsWork: waits, holdWork: hold, workAsker: askedBy, answerText, upstreamOf, sameWork, forgetBotWork: forgetBot, statusOf, waitingView, settleSubWork, subWorkParent })}diff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.jsindex fbde4ebe..586c8790 100644--- a/ds-bot/src/host/turns.js+++ b/ds-bot/src/host/turns.js@@ -62,6 +62,14 @@ export const stripWrittenCalls = text => text// Under DSH's PTC presentation the model calls only this tool; the program's sub-calls// are the tools it really runs.const PROGRAM_TOOL = 'run_code'+// DSH's delegation tools, named by the tool instance: a spawn child and a fork.+const SUBAGENT_TOOLS = new Set(['subagent', 'subagent_fork'])+// A settled child's closing text, or why it ended without one.+const subEndText = (data) => {+ const text = textOf(data?.lastAssistantMessage)+ if (text !== '') return text+ return `subagent ended${typeof data?.stopReason === 'string' ? ` (${data.stopReason})` : ''} without a closing message.`+}const RETRY_TEXT = 'Your last reply wrote a tool call as plain text (<tool_call>…), so it did not run and nothing happened. Call the tool through the tool interface now, then reply as you meant to.'@@ -393,6 +401,23 @@ export function install(rt) {case 'tool/result':endCall(id, entry, event.data?.callId)break+ case 'subagent/start': {+ // A subagent the turn delegated to is work handed on, like message_bot: it+ // hangs under the work the turn answers until its settlement notice comes+ // back, and its closing message joins that work's results.+ const work = rt.workOf?.(entry.info)+ const self = rt.selfOf(id)+ if (work !== undefined && self !== undefined && rt.chatOwners.has(id) && typeof event.data?.id === 'string' && rt.ownsWork?.(self, work)) {+ const call = [...(entry.calls?.values() ?? [])].findLast(item => SUBAGENT_TOOLS.has(item.name))+ let label+ try { label = JSON.parse(call?.args ?? '{}').description } catch {}+ rt.openWork?.(event.data.id, { from: self, parent: work, name: `subagent${typeof label === 'string' && label.trim() !== '' ? ` ${label.trim()}` : ''}` })+ }+ break+ }+ case 'subagent/end':+ if (typeof event.data?.id === 'string') rt.settleSubWork?.(event.data.id, subEndText(event.data))+ breakcase 'turn/start':setActivity(id, 'thinking')entry.turn = event.data.turn@@ -520,6 +545,17 @@ export function install(rt) {queueRoomMessage(id, { text: reminderText(textOf(event.data.content)) })} 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 }+ if (entry.turn === undefined) entry.pending = info+ else if (rt.workOf?.(entry.info) !== 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') {const content = Array.isArray(event.data.content) ? event.data.content : []diff --git a/ds-bot/test/handoff-results.test.js b/ds-bot/test/handoff-results.test.jsindex 5d838dc2..a6dce9fb 100644--- a/ds-bot/test/handoff-results.test.js+++ b/ds-bot/test/handoff-results.test.js@@ -371,3 +371,50 @@ test('a team saved before the result was kept whole still answers, and says whatconst answer = textOf(callsOf(first.agents, 'a')[0][1].content)assert.match(answer, /Tester:\nx{2000}\n\[cut; 5000 characters in all, the rest was not kept\]/)})++test('a subagent a Bot delegated to hands its result into the work the turn answered', async (t) => {+ const { start } = bench(t)+ const first = await start(seed())+ const { agents, emit, 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')+ // Writer delegates part of the work to a subagent instead of another Bot.+ emit('b', 'tool/call', { callId: 'call1', name: 'subagent', arguments: JSON.stringify({ description: 'deep review', prompt: 'review the plan' }) })+ emit('b', 'subagent/start', { runId: 'r1', provider: 'spawn', id: 'sub1', local: true })+ turns.end('b', 'subagent is on it')+ // The answer waits while the sub-work is out, like a handed-on request.+ await delay(50)+ 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'])+ // The child settles; its closing message is the sub-work's result.+ emit('b', 'subagent/end', { runId: 'r1', provider: 'spawn', id: 'sub1', local: true, stopReason: 'completed', lastAssistantMessage: textBlock('reviewed: three findings') })+ emit('b', 'user/message', { content: textBlock('Subagent sub1 settled.\n\nIts closing message:\nreviewed: three findings'), source: { kind: 'subagent-settled', form: 'notice', senderSessionId: 'sub1' } })+ emit('b', 'turn/start', { turn: 2 })+ emit('b', 'assistant/message', assistantText('all reviewed'))+ emit('b', 'turn/end', { turn: 2, reason: { kind: 'completed' } })+ await settled(() => callsOf(agents, 'a').length === 1)+ const answer = textOf(callsOf(agents, 'a')[0][1].content)+ assert.ok(answer.includes('all reviewed'), `own reply goes up: ${answer}`)+ assert.ok(answer.includes('subagent deep review'), `result is named: ${answer}`)+ assert.ok(answer.includes('reviewed: three findings'), `result text goes up: ${answer}`)+ const after = await worksIn(first)+ assert.equal(Object.keys(after).length, 0)+})++test('a subagent started on a turn that answers no work changes nothing', async (t) => {+ const { start } = bench(t)+ const first = await start(seed())+ const { emit } = first+ const turns = turnTaker(first.agents, first.emit)+ turns.userTurn('b', 'summarize this')+ emit('b', 'tool/call', { callId: 'call1', name: 'subagent', arguments: JSON.stringify({ description: 'a summary', prompt: 'summarize' }) })+ emit('b', 'subagent/start', { runId: 'r1', provider: 'spawn', id: 'sub1', local: true })+ turns.end('b', 'done')+ const works = await worksIn(first)+ assert.deepEqual(works, {})+})