Skip to content

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

Preserve queued handoffs and wake askers after failed replies

Fei2411cdffb8e9fa46
diff --git a/ds-bot/src/host/delivery.js b/ds-bot/src/host/delivery.js
index 6076bdd9..557d690e 100644
--- a/ds-bot/src/host/delivery.js
+++ b/ds-bot/src/host/delivery.js
@@ -23,6 +23,7 @@ export function install(rt) {
if (retried && role !== 'room') return '[Your last reply wrote a tool call as text, so nothing ran]'
if (resumed && role === 'resume') return '[DeepSeek Harness restarted while you were working]'
if (resumed && role !== 'room') return `[Message from ${fromName} · DeepSeek Harness restarted while you were working on it]`
+ if (resent && role === 'reply') return `[Reply from ${fromName} · DeepSeek Harness restarted before it could reach you]`
if (resent && role !== 'room') return `[Message from ${fromName} · DeepSeek Harness restarted before it could reach you]`
if (role === 'reply') return `[Reply from ${fromName}${goesOn}]`
if (role === 'stopped') return `[Stopped by ${fromName}]`
@@ -44,14 +45,15 @@ export function install(rt) {
// `deferred`: a request the Bot already read mid-turn, handed back to answer in a
// turn of its own; its exchange was recorded when it first arrived. `resumed` and
// `retried` carry on a turn that a restart cut off or that wrote its tool call as
- // text (resume.js, turns.js), and `resent` is a message a shutdown cancelled; their
- // exchange is recorded already too. `askedBy` is the member whose @mention called a
- // group turn (groups.js). `fromRoomId`: the group whose Session the sender wrote
- // from; the reply goes back to that Session (turns.js). `files` go along as file
- // blocks, which DSH turns into a read-only path the recipient's tools can read.
- // `length`: the whole message's length when it went as a file (attachLong). `work`:
- // the work (handoffs.js) a result that came back goes on with. `merged`: a request
- // that takes in an earlier one its recipient had not read, under that one's exchange.
+ // text (resume.js, turns.js); their exchange is recorded already too. `resent` is
+ // a message a shutdown cancelled; its first send may not have been recorded.
+ // `askedBy` is the member whose @mention called a group turn (groups.js).
+ // `fromRoomId`: the group whose Session the sender wrote from; the reply goes back
+ // to that Session (turns.js). `files` go along as file blocks, which DSH turns into
+ // a read-only path the recipient's tools can read. `length`: the whole message's
+ // length when it went as a file (attachLong). `work`: the work (handoffs.js) a
+ // result that came back goes on with. `merged`: a request that takes in an earlier
+ // one its recipient had not read, under that one's exchange.
async function deliver({ fromId, toId, sessionId, text, role, replyTo, roomId, audience, note, exchangeId = randomUUID(), steer = false, deferred = false, resumed = false, retried = false, resent = false, merged = false, askedBy, fromRoomId, files = [], length, work }) {
const from = rt.botOf(fromId)
const to = rt.botOf(toId)
@@ -92,16 +94,17 @@ export function install(rt) {
if (steer) agent.steer(message)
else agent.followup(message)
await ctx.sessions.flush(agent.session)
- if (EXCHANGE_ROLES.has(role) && !deferred && !resumed && !retried && !resent) {
+ if (EXCHANGE_ROLES.has(role) && !deferred && !resumed && !retried) {
// Recorded once the message is in the Session: an earlier record would show
// a failed delivery as delivered. The whole text is in its file, or in the
- // Session it goes to; exchangeText reads it back there.
+ // Session it goes to; exchangeText reads it back there. Resends keep any
+ // existing record; only a merged request replaces it with its added text.
const kept = headOf(text, TEXT_KEPT)
const whole = length !== undefined ? { length, files } : kept.length < text.length ? { length: text.length, sessionId: targetSessionId } : {}
const entry = { id: exchangeId, from: fromId, to: toId, text: kept, ...whole, role, time: Date.now(), ...(replyTo ? { replyTo } : {}), ...(audience === 'user' ? { audience } : {}) }
- const earlier = merged ? state.exchanges.findIndex(other => other.id === exchangeId) : -1
+ const earlier = state.exchanges.findIndex(other => other.id === exchangeId)
if (earlier === -1) state.exchanges.push(entry)
- else state.exchanges[earlier] = entry
+ else if (merged) state.exchanges[earlier] = entry
void rt.save().catch(() => {})
}
return exchangeId
diff --git a/ds-bot/src/host/handoffs.js b/ds-bot/src/host/handoffs.js
index 38874b74..cda4c2bd 100644
--- a/ds-bot/src/host/handoffs.js
+++ b/ds-bot/src/host/handoffs.js
@@ -155,13 +155,13 @@ export function install(rt) {
save()
}
- // The reply carrying a result never reached the asker: the result is already
- // kept with the work it was for, so the wait for the message to be read ends
- // here and the asker's next answer names the failure.
+ // The reply carrying a result never reached the asker: the failure reaches it
+ // as an input instead (turns.js), which ends the wait when it is read like any
+ // reply. The result kept with the work it was for is marked, so the answer
+ // that goes up names it.
function answerMissed(id) {
for (const work of Object.values(works())) {
if (!work.open.includes(id)) continue
- work.open = work.open.filter(other => other !== id)
const result = (work.results ?? []).find(item => item.id === id)
if (result !== undefined) result.failed = true
save()
diff --git a/ds-bot/src/host/resume.js b/ds-bot/src/host/resume.js
index e5e46e80..df0f1d79 100644
--- a/ds-bot/src/host/resume.js
+++ b/ds-bot/src/host/resume.js
@@ -120,8 +120,9 @@ export function install(rt) {
/** @type {Record<string, {botId:string, roomId?:string, since:number, trigger?:any, approval?:{tool:string, reason:string}, resumed?:boolean}>} */
let turns = {}
+ // A new delivery can persist before recovery runs; it must keep the old queue.
/** @type {Record<string, {at:number, args:any}[]>} deliveries no turn has taken up, by Session */
- let queued = {}
+ let queued = interrupted.queued
let writing = Promise.resolve()
let stopped = false
let latest
@@ -265,26 +266,33 @@ export function install(rt) {
await rt.deliver({ toId: bot.id, role: 'resume', resumed: true, text, ...(trigger?.work ? { work: trigger.work } : {}) })
}
+ // Where a queued note goes now: its Session if it still is one, else the
+ // current Session of its Bot, or of its group when the message went to one.
+ const sessionFor = (sessionId, args) => (rt.isBotSession(sessionId) ? sessionId
+ : args?.roomId === undefined ? rt.chatOf(args?.toId) : rt.roomOf(args.roomId)?.sessions?.[args.toId])
+
// A hard stop keeps the inbox, which the Session runs once it wakes; only what DSH
- // cancelled is sent again.
+ // cancelled is sent again. The notes stay queued until a turn takes the messages
+ // up, so a send that fails here, or a Session that cannot be opened, leaves them
+ // for the next start instead of losing them.
async function resendQueued(sessionId, list) {
- // A Session that moved on to a new part since is reached at its current one.
- const current = (args) => {
- if (rt.isBotSession(sessionId)) return sessionId
- return args.roomId === undefined ? rt.chatOf(args.toId) : rt.roomOf(args.roomId)?.sessions?.[args.toId]
- }
const fresh = list.filter(item => rt.botOf(item.args?.toId) !== undefined
- && current(item.args) !== undefined && taken?.has(item.args.exchangeId) !== true)
+ && sessionFor(sessionId, item.args) !== undefined && taken?.has(item.args.exchangeId) !== true)
if (fresh.length === 0) return
- const target = current(fresh[0].args)
+ const target = sessionFor(sessionId, fresh[0].args)
const agent = await rt.resolveAgent(target)
const waiting = new Set([...(agent.inbox?.nextStep ?? []), ...(agent.inbox?.nextTurn ?? [])].map(message => message.source?.exchangeId))
for (const { args } of fresh) {
- if (waiting.has(args.exchangeId) || taken?.has(args.exchangeId) === true) continue
+ if (waiting.has(args.exchangeId)) continue
+ if (taken?.has(args.exchangeId) === true) {
+ forgetQueued(args.exchangeId)
+ continue
+ }
// A request that waited through a restart reaches its target late: the work
// it is for is marked, so the answer that comes back says so.
if (args.role === 'request' || args.role === 'stopped') rt.lateWork?.(args.exchangeId)
- await rt.deliver({ ...args, sessionId: current(args), steer: false, resent: true })
+ await deliver({ ...args, sessionId: sessionFor(sessionId, args), steer: false, resent: true })
+ .catch(error => rt.warn('ds-bot: sending again what %s was sent failed: %s', sessionId, error))
}
}
@@ -383,7 +391,20 @@ export function install(rt) {
if (rt.readOnly() !== undefined) return
const { turns: noted, queued: dropped } = interrupted
interrupted = { turns: {}, queued: {} }
- // Taken up now, so another restart does not pick them up twice.
+ // What the last run still had on the way is noted again first, under the
+ // Session it would go to now: nothing is cleared before it is handed over.
+ // A Bot that is gone takes its notes with it; what a turn already read here
+ // is done with.
+ const pending = queued
+ queued = {}
+ for (const [oldId, list] of Object.entries(pending)) {
+ if (!Array.isArray(list)) continue
+ for (const item of list) {
+ if (item?.args?.exchangeId === undefined || taken?.has(item.args.exchangeId) === true || rt.botOf(item.args.toId) === undefined) continue
+ const key = sessionFor(oldId, item.args) ?? oldId
+ queued = { ...queued, [key]: [...(queued[key] ?? []), item] }
+ }
+ }
persist()
// Agents and inboxes are read before anything wakes their Sessions.
const cut = []
diff --git a/ds-bot/src/host/tools.js b/ds-bot/src/host/tools.js
index c8a3a7e9..3ebc984b 100644
--- a/ds-bot/src/host/tools.js
+++ b/ds-bot/src/host/tools.js
@@ -241,7 +241,10 @@ export function install(rt) {
try {
await rt.deliver({ fromId: sender.id, toId: target.id, text: long.text, files: [...long.files, ...(joins ? before.files : [])], length: long.length, role: 'request', steer, exchangeId, ...(joins ? { merged: true } : {}), ...(toUser ? { audience: 'user' } : {}) })
} catch (error) {
- if (!joins) rt.dropWork(exchangeId)
+ // The delivery stays noted for the next start while its Bot exists
+ // (resume.js), and the resent request still answers up the tree, so the
+ // work it opened stays too. Only a target that is gone drops the work.
+ if (!joins && rt.botOf(target.id) === undefined) rt.dropWork(exchangeId)
throw error
}
const sent = toUser
diff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.js
index 25ac8848..ea2846f1 100644
--- a/ds-bot/src/host/turns.js
+++ b/ds-bot/src/host/turns.js
@@ -2,6 +2,7 @@
// triggered, the waiters and inboxes of group rounds, and routing a finished turn's reply.
//
// Turn tracking: which turns were triggered by another Bot, and what they said.
+import { randomUUID } from 'node:crypto'
import { HANDOFF_ACK, MESSAGE_INLINE_MAX, REFERENCE_KIND, REPLY_KIND, SOURCE_KIND, STEER_CUT } from './constants.js'
import { absoluteLinks } from './links.js'
import { textOf } from './text.js'
@@ -27,7 +28,9 @@ export const failureLine = failure => `${failure.code}${failure.status ? ` ${fai
// answer comes from; `source` is the trigger the turn answered.
export function answerSender(rt, id, source, text, long) {
const back = rt.roomOf(source.senderRoomId)?.sessions?.[source.senderSessionId]
- const work = rt.closeWork?.(source.exchangeId, rt.botOf(rt.selfOf(id))?.name ?? 'The other Bot', text)
+ const name = rt.botOf(rt.selfOf(id))?.name ?? 'The other Bot'
+ const work = rt.closeWork?.(source.exchangeId, name, text)
+ const exchangeId = randomUUID()
void rt.deliver({
fromId: rt.selfOf(id),
toId: source.senderSessionId,
@@ -36,13 +39,27 @@ export function answerSender(rt, id, source, text, long) {
...(long?.length === undefined ? {} : { files: long.files, length: long.length }),
role: 'reply',
replyTo: source.exchangeId,
+ exchangeId,
...(work ? { work } : {}),
}).catch((error) => {
rt.warn('ds-bot: reply delivery failed: %s', error)
- // The reply never reached the asker, but its result is already kept with the
- // work it was for: the wait for the message to be read ends, and the asker's
- // next answer names the failure instead of waiting on it forever.
+ // The reply never reached the asker: its note goes — the notice below carries
+ // what it answered — the result kept with the work is marked, and the failure
+ // reaches the asker the way the reply would have, so the work goes on and its
+ // answer names it.
+ rt.forgetDelivery?.(exchangeId)
rt.answerMissed?.(source.exchangeId)
+ const said = `${name} could not send its reply to you (${failureLine(turnFailure(error))}); what it had answered:\n`
+ void rt.deliver({
+ fromId: rt.selfOf(id),
+ toId: source.senderSessionId,
+ ...(back ? { sessionId: back } : {}),
+ text: `${said}${long?.text ?? text}`,
+ ...(long?.length === undefined ? {} : { files: long.files, length: said.length + long.length }),
+ role: 'reply',
+ replyTo: source.exchangeId,
+ ...(work ? { work } : {}),
+ }).catch(again => rt.warn('ds-bot: reply failure notice not delivered: %s', again))
})
}
diff --git a/ds-bot/test/follow-through.test.js b/ds-bot/test/follow-through.test.js
index 5b30196b..d6707a39 100644
--- a/ds-bot/test/follow-through.test.js
+++ b/ds-bot/test/follow-through.test.js
@@ -475,6 +475,7 @@ test('a message a shutdown cancelled is sent again once; one still waiting, take
first.emit('b', 'user/message', { content: notesMessage.content, source: notesMessage.source })
for (const listener of first.ctx.listeners.get('agent/inbox/discarded')) listener({ message: withdrawn })
await settle(() => (notes(home).queued.b ?? []).length === 2)
+ const original = (await first.call('exchange', { a: 'a', b: 'b' })).value.find(entry => entry.id === plan.source.exchangeId)
await first.stop()
t.mock.timers.enable({ apis: ['setTimeout'] })
@@ -487,7 +488,16 @@ test('a message a shutdown cancelled is sent again once; one still waiting, take
assert.deepEqual([again.source.exchangeId, again.source.role], [plan.source.exchangeId, 'request'])
assert.match(text(again), /^\[Message from Chief · DeepSeek Harness restarted before it could reach you\]\nDraft the plan$/)
// The resend is the same exchange, not a new one.
- assert.equal((await second.call('exchange', { a: 'a', b: 'b' })).value.length, 4)
+ const exchanges = (await second.call('exchange', { a: 'a', b: 'b' })).value
+ assert.equal(exchanges.length, 4)
+ assert.deepEqual(exchanges.find(entry => entry.id === plan.source.exchangeId), original)
+ await second.stop()
+
+ // Neither the resend nor a message kept in the inbox is lost at another restart.
+ const third = await start(home)
+ t.mock.timers.tick(3000)
+ await settle(() => third.agentOf('b').calls.length === 2)
+ assert.deepEqual(third.agentOf('b').calls.map(message => message.source.exchangeId), [plan, numbers].map(message => message.source.exchangeId))
})
test('a queued request kept over a restart is sent again however old, and the asker is told it was late', async (t) => {
@@ -546,8 +556,9 @@ test('a delivery that fails is not recorded as delivered, and stays queued for t
return flushing(session)
}
await assert.rejects(first.tool('message_bot', { to: 'Writer', message: 'Draft the plan' }, 'a'), /sess-b is gone/)
- // Nothing is shown as delivered, and the work that was opened for it is gone.
- await settleState(t, home, () => (stateOf(home).exchanges ?? []).length === 0)
+ // Nothing is shown as delivered; the delivery stays queued, and the work it
+ // opened stays with it.
+ await settleState(t, home, () => (stateOf(home).exchanges ?? []).length === 0 && Object.keys(stateOf(home).works ?? {}).length === 1)
await settle(() => (notes(home).queued.b ?? []).length === 1)
await first.stop()
@@ -557,7 +568,7 @@ test('a delivery that fails is not recorded as delivered, and stays queued for t
assert.equal(second.agentOf('b').calls[0].source.role, 'request')
})
-test('a reply that fails to reach the asker is marked, and the work does not wait on it', async (t) => {
+test('a reply that fails to reach the asker reaches it as a failure notice carrying the answer', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] })
const state = seed()
// Chief asked Writer; Writer handed part of it to Reviewer.
@@ -568,28 +579,186 @@ test('a reply that fails to reach the asker is marked, and the work does not wai
}
const home = freshHome(t, state)
const host = await start(home)
- // Reviewer finished and its reply to Writer fails before it reaches the Session.
+ // Reviewer finished; its reply to Writer fails once, so the failure reaches
+ // Writer as a notice instead, carrying what Reviewer answered.
+ let fails = 1
+ const resolving = host.ctx.sessionController.resolveAgent
host.ctx.sessionController.resolveAgent = async (sessionId) => {
- if (sessionId === 'b') throw Object.assign(new Error('Session sess-b is gone'), { code: 'session/not-found' })
- return { agent: host.agentOf(sessionId) }
+ if (sessionId === 'b' && fails > 0) {
+ fails -= 1
+ throw Object.assign(new Error('Session sess-b is gone'), { code: 'session/not-found' })
+ }
+ return resolving(sessionId)
}
run(host, 'c', 1, { content: [{ type: 'text', text: '[Message from Writer]\nCheck the numbers' }], source: { kind: 'bot', form: 'relay', role: 'request', exchangeId: 'y1', senderName: 'Writer', senderSessionId: 'b', hop: 0 } })
host.emit('c', 'assistant/message', { message: { content: [{ type: 'text', text: 'The numbers check out.' }] } })
host.emit('c', 'turn/end', { turn: 1, reason: { kind: 'completed' } })
- await settleState(t, home, () => {
- const work = stateOf(home).works?.x1
- return work !== undefined && work.open.length === 0 && work.results[0]?.failed === true
- })
+ await settle(() => host.agentOf('b').calls.length === 1)
+ const notice = host.agentOf('b').calls[0]
+ assert.deepEqual([notice.source.role, notice.source.replyTo, notice.source.work], ['reply', 'y1', 'x1'])
+ assert.match(text(notice), /^\[Reply from Reviewer · for the work Chief asked you for; your answer goes to Chief once everything you handed on is back\]\nReviewer could not send its reply to you \(session\/not-found: Session sess-b is gone\); what it had answered:\nThe numbers check out\.$/)
+ // The wait stays until the notice is read; the kept result is marked.
+ await settleState(t, home, () => stateOf(home).works?.x1?.results[0]?.failed === true)
+ assert.deepEqual(stateOf(home).works.x1.open, ['y1'])
- // A later turn on the same work still hands the answer up, with the failed
- // reply named as what came back.
- run(host, 'b', 2, { content: [{ type: 'text', text: '[Reply from Reviewer · for the work Chief asked you for]\nGo on' }], source: { kind: 'bot', form: 'relay', role: 'reply', exchangeId: 'z1', senderName: 'Reviewer', senderSessionId: 'c', replyTo: 'y1', work: 'x1' } })
+ // Writer reads the notice, the wait ends, and the answer that goes up names it.
+ run(host, 'b', 2, notice)
host.emit('b', 'assistant/message', { message: { content: [{ type: 'text', text: 'The plan is ready.' }] } })
host.emit('b', 'turn/end', { turn: 2, reason: { kind: 'completed' } })
await settle(() => host.agentOf('a').calls.length === 1)
const reply = text(host.agentOf('a').calls[0])
assert.match(reply, /The plan is ready\./)
assert.match(reply, /Reviewer could not send its reply to you; what it had answered:\nThe numbers check out\./)
+ // Nothing of the lost reply is sent again: the notice carried it.
+ await settle(() => (notes(home).queued.b ?? []).length === 0)
+})
+
+test('a failure notice that cannot reach the asker stays queued and wakes it after restart', async (t) => {
+ t.mock.timers.enable({ apis: ['setTimeout'] })
+ const state = seed()
+ state.bots.c = { id: 'c', name: 'Reviewer', sessionId: 'c', createdAt: 3 }
+ state.works = {
+ x1: { from: 'a', to: 'b', open: ['y1'], results: [], at: 1 },
+ y1: { from: 'b', to: 'c', parent: 'x1', open: [], results: [], at: 2 },
+ }
+ const home = freshHome(t, state)
+ const first = await start(home)
+ first.ctx.sessionController.resolveAgent = async sessionId => sessionId === 'b'
+ ? { error: { code: 'session/not-found', message: 'Session sess-b is gone' } }
+ : { agent: first.agentOf(sessionId) }
+ run(first, 'c', 1, { content: [{ type: 'text', text: '[Message from Writer]\nCheck the numbers' }], source: { kind: 'bot', form: 'relay', role: 'request', exchangeId: 'y1', senderName: 'Writer', senderSessionId: 'b' } })
+ first.emit('c', 'assistant/message', { message: { content: [{ type: 'text', text: 'The numbers check out.' }] } })
+ first.emit('c', 'turn/end', { turn: 1, reason: { kind: 'completed' } })
+ await settle(() => {
+ const queued = notes(home).queued.b ?? []
+ return queued.length === 1 && /could not send its reply/.test(queued[0].args.text)
+ })
+ await settleState(t, home, () => stateOf(home).works?.x1?.results[0]?.failed === true)
+ await first.stop()
+
+ const second = await start(home)
+ t.mock.timers.tick(3000)
+ await settle(() => second.agentOf('b').calls.length === 1)
+ const [notice] = second.agentOf('b').calls
+ assert.deepEqual([notice.source.role, notice.source.replyTo, notice.source.work], ['reply', 'y1', 'x1'])
+ assert.match(text(notice), /could not send its reply.*\nThe numbers check out\.$/)
+ run(second, 'b', 2, notice)
+ second.emit('b', 'assistant/message', { message: { content: [{ type: 'text', text: 'The plan is ready.' }] } })
+ second.emit('b', 'turn/end', { turn: 2, reason: { kind: 'completed' } })
+ await settle(() => second.agentOf('a').calls.length === 1)
+ assert.match(text(second.agentOf('a').calls[0]), /Reviewer could not send its reply to you; what it had answered:\nThe numbers check out\./)
+})
+
+test('new deliveries before recovery do not overwrite the saved queue', async (t) => {
+ t.mock.timers.enable({ apis: ['setTimeout'] })
+ const home = freshHome(t)
+ const first = await start(home)
+ await first.tool('message_bot', { to: 'Writer', message: 'Task before shutdown', when: 'queue' }, 'a')
+ await first.stop()
+
+ const second = await start(home)
+ await second.tool('message_bot', { to: 'Writer', message: 'Task before recovery', when: 'queue' }, 'a')
+ await second.stop()
+ assert.equal(notes(home).queued.b.length, 2)
+
+ const third = await start(home)
+ t.mock.timers.tick(3000)
+ await settle(() => third.agentOf('b').calls.length === 2)
+ assert.match(text(third.agentOf('b').calls[0]), /Task before shutdown$/)
+ assert.match(text(third.agentOf('b').calls[1]), /Task before recovery$/)
+})
+
+test('a resend that cannot open the Session keeps the whole queue for the next start', async (t) => {
+ t.mock.timers.enable({ apis: ['setTimeout'] })
+ const home = freshHome(t)
+ const first = await start(home)
+ for (const message of ['Task 1', 'Task 2', 'Task 3']) await first.tool('message_bot', { to: 'Writer', message, when: 'queue' }, 'a')
+ await settle(() => (notes(home).queued.b ?? []).length === 3)
+ await first.stop()
+
+ // The Session cannot be opened at this start: all three stay queued, none is
+ // silently dropped.
+ const warnings = []
+ const second = await start(home)
+ second.ctx.logger.warn = (...args) => warnings.push(args.join(' '))
+ second.ctx.sessionController.resolveAgent = async (sessionId) => {
+ if (sessionId === 'b') throw Object.assign(new Error('Session sess-b is gone'), { code: 'session/not-found' })
+ return { agent: second.agentOf(sessionId) }
+ }
+ t.mock.timers.tick(3000)
+ await settle(() => warnings.some(line => /sending again/.test(line)))
+ await second.stop()
+ assert.equal((notes(home).queued.b ?? []).length, 3)
+
+ // The next start delivers all three.
+ const third = await start(home)
+ t.mock.timers.tick(3000)
+ await settle(() => third.agentOf('b').calls.length === 3)
+})
+
+test('a request kept for retry keeps its work, and the result flows up after the restart', async (t) => {
+ t.mock.timers.enable({ apis: ['setTimeout'] })
+ const state = seed()
+ state.bots.c = { id: 'c', name: 'Reviewer', sessionId: 'c', createdAt: 3 }
+ state.works = { x1: { from: 'a', to: 'b', open: [], results: [], at: 1 } }
+ const home = freshHome(t, state)
+ const first = await start(home)
+ // Writer is doing the work Chief gave it and hands part of it to Reviewer; the
+ // send fails before it reaches Reviewer, so it stays queued for the next start.
+ run(first, 'b', 1, { content: [{ type: 'text', text: '[Message from Chief]\nDraft the plan' }], source: { kind: 'bot', form: 'relay', role: 'request', exchangeId: 'x1', senderName: 'Chief', senderSessionId: 'a', hop: 0 } })
+ const flushing = first.ctx.sessions.flush
+ first.ctx.sessions.flush = async (session) => {
+ if (session?.id === 'c') throw Object.assign(new Error('Session sess-c is gone'), { code: 'session/not-found' })
+ return flushing(session)
+ }
+ await assert.rejects(first.tool('message_bot', { to: 'Reviewer', message: 'Check the numbers' }, 'b'), /sess-c is gone/)
+ // Writer ends its turn waiting for what it handed on.
+ first.emit('b', 'assistant/message', { message: { content: [{ type: 'text', text: 'Reviewer is on the numbers.' }] } })
+ first.emit('b', 'turn/end', { turn: 1, reason: { kind: 'completed' } })
+ // The delivery stays queued, so the work it opened stays too.
+ await settleState(t, home, () => Object.values(stateOf(home).works ?? {}).some(work => work.to === 'c' && work.parent === 'x1'))
+ const handed = Object.entries(stateOf(home).works).find(([, work]) => work.to === 'c' && work.parent === 'x1')
+ await settle(() => (notes(home).queued.c ?? []).length === 1)
+ await first.stop()
+
+ const second = await start(home)
+ t.mock.timers.tick(3000)
+ await settle(() => second.agentOf('c').calls.length === 1)
+ // Reviewer answers; the result goes back up the work tree to Writer.
+ run(second, 'c', 1, second.agentOf('c').calls[0])
+ second.emit('c', 'assistant/message', { message: { content: [{ type: 'text', text: 'The numbers check out.' }] } })
+ second.emit('c', 'turn/end', { turn: 1, reason: { kind: 'completed' } })
+ await settle(() => second.agentOf('b').calls.length === 1)
+ const reply = second.agentOf('b').calls[0]
+ assert.deepEqual([reply.source.role, reply.source.work, reply.source.replyTo], ['reply', 'x1', handed[0]])
+ run(second, 'b', 2, reply)
+ second.emit('b', 'assistant/message', { message: { content: [{ type: 'text', text: 'The plan is ready.' }] } })
+ second.emit('b', 'turn/end', { turn: 2, reason: { kind: 'completed' } })
+ await settle(() => second.agentOf('a').calls.length === 1)
+ assert.match(text(second.agentOf('a').calls[0]), /Reviewer.*numbers check out/s)
+})
+
+test('a resend whose first send never landed records the exchange once it arrives', async (t) => {
+ t.mock.timers.enable({ apis: ['setTimeout'] })
+ const home = freshHome(t)
+ const first = await start(home)
+ const flushing = first.ctx.sessions.flush
+ first.ctx.sessions.flush = async (session) => {
+ if (session?.id === 'b') throw Object.assign(new Error('Session sess-b is gone'), { code: 'session/not-found' })
+ return flushing(session)
+ }
+ await assert.rejects(first.tool('message_bot', { to: 'Writer', message: 'Draft the plan' }, 'a'), /sess-b is gone/)
+ await settle(() => (notes(home).queued.b ?? []).length === 1)
+ await first.stop()
+
+ const second = await start(home)
+ t.mock.timers.tick(3000)
+ await settle(() => second.agentOf('b').calls.length === 1)
+ // The first send never reached the Session, so the resend records the exchange.
+ const [again] = second.agentOf('b').calls
+ await settleState(t, home, () => (stateOf(home).exchanges ?? []).length === 1)
+ const [entry] = stateOf(home).exchanges
+ assert.deepEqual([entry.id, entry.role, entry.from, entry.to], [again.source.exchangeId, 'request', 'a', 'b'])
})
const WRITTEN = 'Checking now.\n<tool_call>\n<function=pwsh>\n<parameter=command>\nGet-ChildItem E:\\\n</parameter>\n</function>\n</tool_call>'