A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Keep queued deliveries whole and tell the asker what a restart did
Fei2411d1170ae5d4fc
diff --git a/ds-bot/src/host/delivery.js b/ds-bot/src/host/delivery.jsindex 7d30008d..6076bdd9 100644--- a/ds-bot/src/host/delivery.js+++ b/ds-bot/src/host/delivery.js@@ -14,7 +14,7 @@ export function install(rt) {return resolved.agent}- const headerFor = (role, fromName, roomName, audience, note = 'new message', deferred = false, { resumed = false, retried = false, asker } = {}) => {+ const headerFor = (role, fromName, roomName, audience, note = 'new message', deferred = false, { resumed = false, retried = false, resent = false, asker } = {}) => {// What came back for work another Bot gave this one says where its answer goes.const goesOn = asker === undefined ? '' : ` · for the work ${asker} asked you for; your answer goes to ${asker} once everything you handed on is back`if (deferred && role === 'reply') return `[Reply from ${fromName} · you read it during your last task; go on with the work it is for now${goesOn}]`@@ -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 !== '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}]`if (role === 'room') return `[Group chat "${roomName}" · ${note}]`@@ -77,24 +78,13 @@ export function install(rt) {...(fromRoomId ? { senderRoomId: fromRoomId } : {}),}const targetSessionId = sessionId ?? rt.chatOf(toId)- if (EXCHANGE_ROLES.has(role) && !deferred && !resumed && !retried && !resent) {- // The whole text is in its file, or in the Session it goes to; exchangeText reads- // it back there.- 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- if (earlier === -1) state.exchanges.push(entry)- else state.exchanges[earlier] = entry- void rt.save().catch(() => {})- }await rt.ensureCapabilities(targetSessionId)const agent = await resolveAgent(targetSessionId)// A steered message cuts into a running turn at its next step; a follow-up// queues for the next turn (see turns.js for how each is routed).const message = createUserMessage({content: [- { type: 'text', text: `${headerFor(role, fromName, room?.name, audience, note, deferred, { resumed, retried, asker })}\n${text}` },+ { type: 'text', text: `${headerFor(role, fromName, room?.name, audience, note, deferred, { resumed, retried, resent, asker })}\n${text}` },...files.map(attachment => ({ type: 'file', attachment })),],source,@@ -102,6 +92,18 @@ 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) {+ // 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.+ 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+ if (earlier === -1) state.exchanges.push(entry)+ else 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.jsindex 2cd5d199..38874b74 100644--- a/ds-bot/src/host/handoffs.js+++ b/ds-bot/src/host/handoffs.js@@ -146,6 +146,29 @@ export function install(rt) {save()}+ // A delivery the restart delayed is marked, so the answer that comes back for+ // the work says the request only arrived then.+ function lateWork(id) {+ const work = works()[id]+ if (work === undefined || work.late === true) return+ work.late = true+ 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.+ 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()+ return+ }+ }+const owns = (botId, workId) => workId !== undefined && works()[workId]?.to === botIdconst waits = workId => (works()[workId]?.open.length ?? 0) > 0function hold(workId, text) {@@ -166,10 +189,13 @@ export function install(rt) {const results = (work?.results ?? []).map(result => {const { text, whole } = resultText(result)const cut = whole ? '' : `\n[cut; ${result.length} characters in all, the rest was not kept]`+ if (result.failed === true) return `${result.name} could not send its reply to you; what it had answered:\n${text}${cut}`return `${result.name}:\n${text}${cut}`})- if (results.length === 0) return said || `${botName} finished without a reply.`- return [said || `${botName} finished and wrote nothing more.`, `[Handed back to ${botName} for this work]`, ...results].join('\n\n')+ const body = results.length === 0 ? said || `${botName} finished without a reply.`+ : [said || `${botName} finished and wrote nothing more.`, `[Handed back to ${botName} for this work]`, ...results].join('\n\n')+ // The request reached its target only after a restart; the answer says so.+ return work?.late === true ? `[This request reached ${botName} only after DeepSeek Harness restarted.]\n\n${body}` : body}// The Bots waiting for the result of these works: whoever asked for each, and on up.@@ -262,5 +288,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, lateWork, answerMissed, ownsWork: owns, waitsWork: waits, holdWork: hold, workAsker: askedBy, answerText, upstreamOf, sameWork, forgetBotWork: forgetBot, statusOf, waitingView })}diff --git a/ds-bot/src/host/resume.js b/ds-bot/src/host/resume.jsindex ce2d3246..e5e46e80 100644--- a/ds-bot/src/host/resume.js+++ b/ds-bot/src/host/resume.js@@ -19,9 +19,6 @@ import { textOf } from './text.js'// Long enough for DSH to finish opening its workspaces after a start.export const RESUME_DELAY_MS = 3000-// A delivery waiting longer than this was not lost to a restart.-const QUEUED_KEPT_MS = 24 * 60 * 60 * 1000-const QUEUED_MAX = 50export const resumeText = record => ['DeepSeek Harness restarted while you were working on this, so your last turn stopped before it finished.',@@ -171,13 +168,16 @@ export function install(rt) {rt.deliver = async (args) => {const noted = { ...args, exchangeId: args.exchangeId ?? randomUUID() }const sessionId = noted.sessionId ?? rt.chatOf(noted.toId)- // Noted before it is sent: a turn may take it up before the send returns.- queued = { ...queued, [sessionId]: [...(queued[sessionId] ?? []), { at: Date.now(), args: noted }].slice(-QUEUED_MAX) }+ // Noted before it is sent: a turn may take it up before the send returns. Notes+ // are kept whole; nothing waits out a delivery silently.+ queued = { ...queued, [sessionId]: [...(queued[sessionId] ?? []), { at: Date.now(), args: noted }] }persist()try {return await deliver(noted)} catch (error) {- forgetQueued(noted.exchangeId)+ // A delivery that never reached the inbox stays noted: the next start sends+ // it again. One whose Bot is gone is dropped, since it can never be sent.+ if (rt.botOf(noted.toId) === undefined) forgetQueued(noted.exchangeId)throw error}}@@ -273,7 +273,7 @@ export function install(rt) {if (rt.isBotSession(sessionId)) return sessionIdreturn args.roomId === undefined ? rt.chatOf(args.toId) : rt.roomOf(args.roomId)?.sessions?.[args.toId]}- const fresh = list.filter(item => Date.now() - item.at < QUEUED_KEPT_MS && rt.botOf(item.args?.toId) !== undefined+ const fresh = list.filter(item => rt.botOf(item.args?.toId) !== undefined&& current(item.args) !== undefined && taken?.has(item.args.exchangeId) !== true)if (fresh.length === 0) returnconst target = current(fresh[0].args)@@ -281,6 +281,9 @@ export function install(rt) {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+ // 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 })}}diff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.jsindex fbde4ebe..25ac8848 100644--- a/ds-bot/src/host/turns.js+++ b/ds-bot/src/host/turns.js@@ -37,7 +37,13 @@ export function answerSender(rt, id, source, text, long) {role: 'reply',replyTo: source.exchangeId,...(work ? { work } : {}),- }).catch(error => rt.warn('ds-bot: reply delivery failed: %s', error))+ }).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.+ rt.answerMissed?.(source.exchangeId)+ })}// A group's own Session never reaches a model, so the snapshot DSH prepared therediff --git a/ds-bot/test/follow-through.test.js b/ds-bot/test/follow-through.test.jsindex e95157c5..5b30196b 100644--- a/ds-bot/test/follow-through.test.js+++ b/ds-bot/test/follow-through.test.js@@ -484,11 +484,114 @@ test('a message a shutdown cancelled is sent again once; one still waiting, taket.mock.timers.tick(3000)await settle(() => second.agentOf('b').calls.length === 1)const [again] = second.agentOf('b').calls- assert.deepEqual([again.source.exchangeId, again.source.role, text(again)], [plan.source.exchangeId, 'request', text(plan)])+ 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)})+test('a queued request kept over a restart is sent again however old, and the asker is told it was late', async (t) => {+ const home = freshHome(t)+ const first = await start(home)+ await first.tool('message_bot', { to: 'Writer', message: 'Draft the plan', when: 'queue' }, 'a')+ await settle(() => (notes(home).queued.b ?? []).length === 1)+ const [plan] = first.agentOf('b').calls+ await first.stop()++ // The note waited through a long stop: it is still sent again, not dropped.+ const file = join(home, 'inflight.json')+ const noted = notes(home)+ noted.queued.b[0].at = Date.now() - 25 * 60 * 60 * 1000+ writeFileSync(file, JSON.stringify(noted))++ t.mock.timers.enable({ apis: ['setTimeout'] })+ const second = await start(home)+ t.mock.timers.tick(3000)+ await settle(() => second.agentOf('b').calls.length === 1)+ const [again] = second.agentOf('b').calls+ 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\]/)++ // The work is marked, so the answer that goes up says the request arrived late.+ await settleState(t, home, () => stateOf(home).works?.[plan.source.exchangeId]?.late === true)+ run(second, 'b', 1, again)+ second.emit('b', 'assistant/message', { message: { content: [{ type: 'text', text: 'The plan is ready.' }] } })+ second.emit('b', 'turn/end', { turn: 1, reason: { kind: 'completed' } })+ await settle(() => second.agentOf('a').calls.length === 1)+ const reply = text(second.agentOf('a').calls[0])+ assert.match(reply, /^\[Reply from Writer\]\n\[This request reached Writer only after DeepSeek Harness restarted\.\]/)+ assert.match(reply, /The plan is ready\./)+})++test('queued deliveries are kept whole, not cut at fifty', async (t) => {+ const home = freshHome(t)+ const first = await start(home)+ for (let at = 1; at <= 55; at += 1) await first.tool('message_bot', { to: 'Writer', message: `Task ${at}`, when: 'queue' }, 'a')+ await settle(() => (notes(home).queued.b ?? []).length === 55)+ await first.stop()++ t.mock.timers.enable({ apis: ['setTimeout'] })+ const second = await start(home)+ t.mock.timers.tick(3000)+ await settle(() => second.agentOf('b').calls.length === 55)+})++test('a delivery that fails is not recorded as delivered, and stays queued for the next start', 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/)+ // Nothing is shown as delivered, and the work that was opened for it is gone.+ await settleState(t, home, () => (stateOf(home).exchanges ?? []).length === 0)+ 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)+ 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) => {+ t.mock.timers.enable({ apis: ['setTimeout'] })+ const state = seed()+ // Chief asked Writer; Writer handed part of it to Reviewer.+ 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 host = await start(home)+ // Reviewer finished and its reply to Writer fails before it reaches the Session.+ 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) }+ }+ 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+ })++ // 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' } })+ 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\./)+})+const WRITTEN = 'Checking now.\n<tool_call>\n<function=pwsh>\n<parameter=command>\nGet-ChildItem E:\\\n</parameter>\n</function>\n</tool_call>'test('a tool call written as text gets one more turn on the same round slot', async (t) => {