A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Clear a deleted Bot's activity and answer the Bots waiting for it
Fei241176a87bd4e037
diff --git a/ds-bot/src/host/roster.js b/ds-bot/src/host/roster.jsindex 981bc34a..2c7e89fd 100644--- a/ds-bot/src/host/roster.js+++ b/ds-bot/src/host/roster.js@@ -451,6 +451,7 @@ export function install(rt) {const current = rt.chatOf(id)// Questions and exchange turns are kept per Session, so every part of the chat is cleared.const parts = new Set([id, current, ...(bot.segments ?? []).map(segment => segment.sessionId)])+ await rt.releaseBot(id, [...parts, ...rt.groupSessionsOf(id)])delete state.bots[id]for (const part of parts) {delete state.questions[part]diff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.jsindex ba4bd434..66a32857 100644--- a/ds-bot/src/host/turns.js+++ b/ds-bot/src/host/turns.js@@ -183,6 +183,44 @@ export function install(rt) {}}+ // The reply a sender waits for, also when it wrote from a group, where it gets the+ // reply in its Session there while it is still a member.+ const tellSender = (id, source, text) => {+ const back = rt.roomOf(source.senderRoomId)?.sessions?.[source.senderSessionId]+ void rt.deliver({+ fromId: rt.selfOf(id),+ toId: source.senderSessionId,+ ...(back ? { sessionId: back } : {}),+ text,+ role: 'reply',+ hop: source.hop ?? 0,+ replyTo: source.exchangeId,+ }).catch(error => rt.warn('ds-bot: reply delivery failed: %s', error))+ }++ // Runs before a deleted Bot leaves the team. Its Sessions stop with it, and the+ // turn/end that follows is no longer the team's, so their activity and turn records go+ // now. Every Bot still waiting for its reply hears that it was deleted: the turn's+ // trigger, requests that joined the turn, and requests it has not read yet.+ async function releaseBot(botId, sessionIds) {+ const text = `${rt.botOf(botId)?.name ?? 'The other Bot'} was deleted before it could answer your message.`+ const waiting = new Map()+ for (const id of sessionIds) {+ const entry = live.get(id)+ const held = [entry?.info, entry?.pending, ...(entry?.joined ?? []).map(joined => joined.info)]+ if (entry?.turn !== undefined) {+ const agent = await rt.resolveAgent(id).catch(() => undefined)+ held.push(...[...(agent?.inbox?.nextStep ?? []), ...(agent?.inbox?.nextTurn ?? [])].map(message => message.source))+ }+ for (const source of held) {+ if (source?.kind === SOURCE_KIND && expectsReply(source) && !answersUser(source)) waiting.set(source.exchangeId, [id, source])+ }+ live.delete(id)+ setActivity(id, undefined)+ }+ for (const [id, source] of waiting.values()) tellSender(id, source, text)+ }+// A group Session is hidden as a whole, so its turns need no marking. A handoff// package that only opens a part is acknowledged with a fixed line, also hidden.const claimTrigger = (sessionId, entry, info) => {@@ -304,7 +342,11 @@ export function install(rt) {ctx.on('session/event', (session, event) => {const id = session?.idif (id === undefined) return- if (!rt.isBotSession(id) && !rt.roomOf(id)) return+ if (!rt.isBotSession(id) && !rt.roomOf(id)) {+ // A deleted Bot's Session can still be ending its turn; nothing of it stays.+ if (live.delete(id)) setActivity(id, undefined)+ return+ }const entry = liveOf(id)if (rt.groupOwners.has(id)) rt.touchRoomTurn?.(id)switch (event.type) {@@ -480,32 +522,13 @@ export function install(rt) {const answer = (source, reply) => {if (!expectsReply(source) || answersUser(source)) returnconst name = rt.botOf(rt.selfOf(id))?.name ?? 'The other Bot'- // A sender that wrote from a group gets the reply in its Session there, while- // it is still a member.- const back = rt.roomOf(source.senderRoomId)?.sessions?.[source.senderSessionId]if (failure !== undefined) {// The sender was told a reply comes later; without this it would wait forever.- void rt.deliver({- fromId: rt.selfOf(id),- toId: source.senderSessionId,- ...(back ? { sessionId: back } : {}),- text: `${name} could not answer your message: its model call failed (${failureLine(failure)}). Tell the user if it matters; do not retry right away.`,- role: 'reply',- hop: source.hop ?? 0,- replyTo: source.exchangeId,- }).catch(error => rt.warn('ds-bot: failure notice delivery failed: %s', error))+ tellSender(id, source, `${name} could not answer your message: its model call failed (${failureLine(failure)}). Tell the user if it matters; do not retry right away.`)} else if (reply !== '' || reason?.kind === 'aborted') {// A turn stopped before it could answer still owes the sender a line,// or it would wait forever for a reply that never comes.- void rt.deliver({- fromId: rt.selfOf(id),- toId: source.senderSessionId,- ...(back ? { sessionId: back } : {}),- text: reply !== '' ? reply : `${name} was stopped before it could answer your message.`,- role: 'reply',- hop: source.hop ?? 0,- replyTo: source.exchangeId,- }).catch(error => rt.warn('ds-bot: reply delivery failed: %s', error))+ tellSender(id, source, reply !== '' ? reply : `${name} was stopped before it could answer your message.`)}}// A retried turn answers the same waiter or sender, so this one passes nothing on.@@ -551,5 +574,5 @@ export function install(rt) {}})- Object.assign(rt, { live, liveOf, roomWaiters, roomProgress, roomInbox, activityView, workView, roomWorkView, callDetail, stopRoomTurn, unattended, userSpoke })+ Object.assign(rt, { live, liveOf, roomWaiters, roomProgress, roomInbox, activityView, workView, roomWorkView, callDetail, stopRoomTurn, unattended, userSpoke, releaseBot })}diff --git a/ds-bot/test/group-work.test.js b/ds-bot/test/group-work.test.jsindex 66f9267a..1f886b7d 100644--- a/ds-bot/test/group-work.test.js+++ b/ds-bot/test/group-work.test.js@@ -482,3 +482,29 @@ test('the team view says what a member is doing in the call\'s own words', asyncconst later = (await host.call('state', {})).valueassert.deepEqual(['said', 'note'].map(key => later.work.bots.a[key]), ['先把 JDK 下下来。', 'Download JDK 21'])})++// 公开#124: a Bot deleted while it worked stayed "thinking" until DSH restarted, and the+// Bot that had asked it something never heard back.+test('deleting a working Bot clears its activity and tells the Bots waiting for its reply', async (t) => {+ const host = await start(t)+ host.emit('b', 'turn/start', { turn: 1 })+ host.emit('b', 'user/message', { content: [{ type: 'text', text: '[Message from Chief]\nwrite the docs' }], source: { kind: 'bot', role: 'request', senderSessionId: 'a', senderName: 'Chief', exchangeId: 'x1', hop: 1 } })+ host.emit('b', 'tool/call', { callId: 'k1', name: 'pwsh', arguments: '{}' })+ // A second request that Writer has not read yet.+ host.agentOf('b').inbox.nextStep.push({ id: 'm2', source: { kind: 'bot', role: 'request', senderSessionId: 'c', senderName: 'Tester', exchangeId: 'x2', hop: 1 } })+ assert.equal((await host.call('state', {})).value.activity.b, 'working')++ const deleted = await host.call('delete-bot', { id: 'b' })+ assert.equal(deleted.ok, true)+ assert.equal((await host.call('state', {})).value.activity.b, undefined)+ await settle(() => host.agentOf('a').calls.length === 1 && host.agentOf('c').calls.length === 1)+ for (const [to, exchangeId] of [['a', 'x1'], ['c', 'x2']]) {+ const reply = host.agentOf(to).calls[0]+ assert.deepEqual([reply.source.role, reply.source.replyTo], ['reply', exchangeId])+ assert.equal(text(reply), '[Reply from Writer]\nWriter was deleted before it could answer your message.')+ }+ // The stopped turn ends after the delete; it brings nothing back.+ host.emit('b', 'turn/start', { turn: 2 })+ host.emit('b', 'turn/end', { turn: 1, reason: { kind: 'aborted' } })+ assert.equal((await host.call('state', {})).value.activity.b, undefined)+})