A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Leave turns that outlive a plugin reload running
Fei2411f1f8a7249de9
diff --git a/ds-bot/src/host/resume.js b/ds-bot/src/host/resume.jsindex 34101e18..93c8e9ef 100644--- a/ds-bot/src/host/resume.js+++ b/ds-bot/src/host/resume.js@@ -118,9 +118,14 @@ export function install(rt) {// Exchanges a turn took up since this start, until the resends are done, so a resend// never repeats one.let taken = Object.keys(interrupted.queued).length > 0 ? new Set() : undefined+ // The first turn event of each Session since this start, until the noted turns are+ // picked up. A turn that ends, or still runs, before one starts here outlived the+ // last run: only DS Bot restarted (the plugin reloaded) and DSH went on.+ let firstTurn = Object.keys(interrupted.turns).length > 0 ? new Map() : undefinedctx.on('session/event', (session, event) => {const id = session?.idif (id === undefined) return+ if ((event.type === 'turn/start' || event.type === 'turn/end') && firstTurn?.has(id) === false) firstTurn.set(id, event.type)if (event.type === 'user/message' && event.data?.source?.kind === SOURCE_KIND && event.data.source.exchangeId) {taken?.add(event.data.source.exchangeId)forgetQueued(event.data.source.exchangeId)@@ -202,17 +207,26 @@ export function install(rt) {async function resumeAll() {await rt.load()if (rt.readOnly() !== undefined) return- const { turns: cut, queued: dropped } = interrupted+ const { turns: noted, queued: dropped } = interruptedinterrupted = { turns: {}, queued: {} }// Taken up now, so another restart does not pick them up twice.persist()- // Inboxes are read before anything wakes their Sessions.+ // Agents and inboxes are read before anything wakes their Sessions.+ const cut = []+ for (const [sessionId, record] of Object.entries(noted)) {+ const first = firstTurn.get(sessionId)+ if (first === 'turn/end') continue+ // Still running: noted again, as if it had started here.+ if (first === undefined && (await rt.resolveAgent(sessionId).catch(() => undefined))?.status === 'running') update(sessionId, () => record)+ else cut.push([sessionId, record])+ }+ firstTurn = undefinedfor (const [sessionId, list] of Object.entries(dropped)) {if (!Array.isArray(list)) continueawait resendQueued(sessionId, list).catch(error => rt.warn('ds-bot: sending again what %s was sent failed: %s', sessionId, error))}taken = undefined- for (const [sessionId, record] of Object.entries(cut)) {+ for (const [sessionId, record] of cut) {await resumeTurn(sessionId, record).catch(error => rt.warn('ds-bot: picking up the turn of %s failed: %s', sessionId, error))}}diff --git a/ds-bot/test/follow-through.test.js b/ds-bot/test/follow-through.test.jsindex 2ad7c838..c0f49ff4 100644--- a/ds-bot/test/follow-through.test.js+++ b/ds-bot/test/follow-through.test.js@@ -164,6 +164,33 @@ test('a request cut off by a restart goes on under the same exchange; own-chat wassert.equal(state.exchanges.length, 0)})+test('when only DS Bot restarts, a turn still running or finished meanwhile is not picked up', async (t) => {+ t.mock.timers.enable({ apis: ['setTimeout'] })+ const home = freshHome(t)+ const first = await start(home)+ 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 } })+ run(first, 'a', 1, { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } })+ await settle(() => inflight(home).b?.trigger !== undefined && inflight(home).a !== undefined)+ await first.stop()++ // DSH went on: Writer is still at work, and Chief's turn ends before the pick-up.+ const second = await start(home)+ second.agentOf('b').status = 'running'+ second.emit('a', 'turn/end', { turn: 1, reason: { kind: 'completed' } })+ t.mock.timers.tick(3000)+ await settle(() => inflight(home).b?.trigger?.exchangeId === 'x1' && inflight(home).a === undefined)+ await new Promise(resolve => setImmediate(resolve))+ assert.deepEqual([second.agentOf('a').calls.length, second.agentOf('b').calls.length], [0, 0])++ // Writer's turn stays noted, so a real restart still picks it up.+ second.emit('b', 'turn/end', { turn: 1, reason: { kind: 'aborted', reason: { kind: 'disposed' } } })+ await second.stop()+ const third = await start(home)+ t.mock.timers.tick(3000)+ await settle(() => third.agentOf('b').calls.length === 1)+ assert.deepEqual([third.agentOf('b').calls[0].source.exchangeId, third.agentOf('a').calls.length], ['x1', 0])+})+test('a finished turn leaves nothing to pick up', async (t) => {t.mock.timers.enable({ apis: ['setTimeout'] })const home = freshHome(t)