A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Keep stopped and removed group members from acting for the group
Fei24114bf6ce652b42
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3e003d94..cf5eaf01 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -9,6 +9,7 @@ All notable changes to DS Bot are listed here. The format follows [Keep a Change- In a Bot's chat or a group chat, `@` lists the Bots you can mention first: a group's members and **Everyone**, or the other Bots. Picking one writes `@Name`.- **Reference a chat** in the composer's **+** menu adds another Bot's chat, or a recent conversation outside the team, to your message as context.- A Main Bot can stop another Bot's running turn. The Bot is told why, and its reply goes back to the Main Bot.+- In a group chat, a member that asks for an approval no longer holds up the round. You allow or reject the request in the group's details panel, and a line above the composer names the members who are waiting or at work. What a member says after the round moved on is posted in the group.### Changeddiff --git a/ds-bot/client.js b/ds-bot/client.jsindex 40893309..de418d06 100644--- a/ds-bot/client.js+++ b/ds-bot/client.js@@ -3986,6 +3986,7 @@ function TriggerCell({ node, sessionId, useRoster, useSessions, useTurnClock, usif (source.role === "kickoff" || source.role === "room") return null;if (source.role === "secret" || deferred) return null;if (source.role === "post") {+ if (source.failed !== void 0) return null;const room2 = roster.roomsById[sessionId];const poster = roster.byId[source.senderSessionId] ?? { id: source.senderSessionId, name: source.senderName ?? t("a Bot"), color: "gray" };return (0, import_react6.createElement)(GroupMessage, { bot: poster, text: stripHeader(contentText(data.content)), room: room2, roster, actions, msgKey: `${sessionId}:${data.turn}:post`, useUi });diff --git a/ds-bot/src/client/cells.js b/ds-bot/src/client/cells.jsindex 4a33099f..95e362e4 100644--- a/ds-bot/src/client/cells.js+++ b/ds-bot/src/client/cells.js@@ -325,6 +325,8 @@ export function TriggerCell({ node, sessionId, useRoster, useSessions, useTurnCl// The secret card itself shows what the user did; the note to the Bot stays out.if (source.role === 'secret' || deferred) return nullif (source.role === 'post') {+ // A member's late failure shows as an event of the round it opens.+ if (source.failed !== undefined) return nullconst room = roster.roomsById[sessionId]const poster = roster.byId[source.senderSessionId] ?? { id: source.senderSessionId, name: source.senderName ?? t('a Bot'), color: 'gray' }return h(GroupMessage, { bot: poster, text: stripHeader(contentText(data.content)), room, roster, actions, msgKey: `${sessionId}:${data.turn}:post`, useUi })diff --git a/ds-bot/src/host/approvals.js b/ds-bot/src/host/approvals.jsindex 9816c2ff..d8ca98f8 100644--- a/ds-bot/src/host/approvals.js+++ b/ds-bot/src/host/approvals.js@@ -52,5 +52,12 @@ export function install(rt) {entry.answer(outcome)}- Object.assign(rt, { approvalsView, waitsForApproval, answerApproval })+ // A group Session that leaves its group has no card left to answer from.+ const cancelApprovals = (sessionId) => {+ for (const entry of pending.values()) {+ if (entry.sessionId === sessionId) entry.answer?.('cancelled')+ }+ }++ Object.assign(rt, { approvalsView, waitsForApproval, answerApproval, cancelApprovals })}diff --git a/ds-bot/src/host/channel.js b/ds-bot/src/host/channel.jsindex 96088866..1f90bba4 100644--- a/ds-bot/src/host/channel.js+++ b/ds-bot/src/host/channel.js@@ -77,7 +77,7 @@ export function install(rt) {})// What a host that only reads the team may still answer.// Updates change the dsh profile, not the team, so a reading host may run them too.- const READS = new Set(['state', 'models', 'efforts', 'earlier', 'room-progress', 'exchange', 'usage', 'schedules', 'memory-view', 'browser-wait', 'browser-result', 'diagnostics', 'diagnostics-log', 'diagnostics-clear', 'update-check', 'update-install', 'dsh-update-install', 'repo-stars'])+ const READS = new Set(['state', 'models', 'efforts', 'earlier', 'room-progress', 'exchange', 'usage', 'schedules', 'memory-view', 'browser-wait', 'browser-result', 'diagnostics', 'diagnostics-log', 'diagnostics-clear', 'update-check', 'update-install', 'dsh-update-install', 'repo-stars', 'approval-answer'])function setPrefs(body) {if (body.theme !== undefined) {diff --git a/ds-bot/src/host/groups.js b/ds-bot/src/host/groups.jsindex fb839370..7bee03b8 100644--- a/ds-bot/src/host/groups.js+++ b/ds-bot/src/host/groups.js@@ -180,9 +180,11 @@ export function install(rt) {// model call failed, `{ missed }` when it went quiet for too long, `{ approval }` when// it waits for the user's approval, or null when the round was stopped. The wait// restarts on each event of the member's Session, so a member at work is never cut off.- async function roomTurn(room, member, sessionId, text, note, signal) {+ async function roomTurn(room, member, sessionId, text, note, signal, started) {if (signal.aborted) return nullconst exchangeId = randomUUID()+ const turn = { botId: member.id, sessionId, exchangeId }+ started.push(turn)room.seen[member.id] = room.seqconst done = new Promise((resolve) => {let timer@@ -196,7 +198,7 @@ export function install(rt) {timer = setTimeout(() => finish({ missed: true }), ROOM_REPLY_TIMEOUT_MS)}touch()- rt.roomWaiters.set(exchangeId, { sessionId, resolve: finish, fail: failure => finish({ failure }), touch, approval: () => finish({ approval: true }) })+ rt.roomWaiters.set(exchangeId, { sessionId, botId: member.id, roomId: room.id, resolve: finish, fail: failure => finish({ failure }), touch, approval: () => finish({ approval: true }) })signal.addEventListener('abort', () => finish(null), { once: true })})try {@@ -208,11 +210,19 @@ export function install(rt) {rt.roomWaiters.get(exchangeId)?.resolve(null)}const reply = await done+ // A turn that ended leaves nothing for a stop to cancel.+ if (typeof reply === 'string' || reply?.failure !== undefined) started.splice(started.indexOf(turn), 1)if (reply === null || typeof reply === 'object') return replyreturn stripOwnName(member.name, String(reply ?? '').trim())}- const waitersOf = sessionId => [...rt.roomWaiters.values()].filter(waiter => waiter.sessionId === sessionId)+ // A member that reaches its context limit mid-turn goes on in a fresh Session, so a+ // waiter is also found by the member and group that Session now speaks for.+ const waitersOf = (sessionId) => {+ const owner = rt.groupOwners.get(sessionId)+ return [...rt.roomWaiters.values()].filter(waiter => waiter.sessionId === sessionId+ || (owner !== undefined && waiter.botId === owner.botId && waiter.roomId === owner.roomId))+ }rt.touchRoomTurn = (sessionId) => { for (const waiter of waitersOf(sessionId)) waiter.touch() }rt.yieldRoomTurn = (sessionId) => { for (const waiter of waitersOf(sessionId)) waiter.approval() }@@ -306,7 +316,7 @@ export function install(rt) {const prompt = roomPrompt(room, member, round)round.shown?.push(member.id)const text = session.brief === undefined ? prompt : `${session.brief}\n\n${prompt}`- const reply = await roomTurn(room, member, session.id, text, slot.note ?? 'your turn', signal)+ const reply = await roomTurn(room, member, session.id, text, slot.note ?? 'your turn', signal, round.started)round.speaking = nullif (reply?.failure !== undefined) {// Not a pass: the user sees why the member is silent, and the round goes on.@@ -357,8 +367,9 @@ export function install(rt) {rt.roomInbox.delete(room.id)const posts = queued.length > 0 ? queued : [{ text: '' }]const startSeq = room.seq + 1- // The opening greeting is a cue for the admin, not a line of the group.- for (const post of posts.filter(item => !item.kickoff)) {+ // The opening greeting is a cue for the admin, not a line of the group; a member's+ // late failure is an event of the round.+ for (const post of posts.filter(item => !item.kickoff && !item.failed)) {// Image readings go into the log only; `@` names are picked from what the user typed.const text = post.images === undefined ? post.text : [post.text, ...await rt.imagesForGroup(post.images, room.id)].filter(Boolean).join('\n')appendRoom(room, post.botId@@ -381,6 +392,13 @@ export function install(rt) {replies: [],passed: [],failed: [],+ started: [],+ }+ for (const post of posts.filter(item => item.failed)) {+ const bot = rt.botOf(post.botId)+ const { code, status, message } = post.failed+ round.failed.push(bot?.name ?? post.name)+ round.replies.push({ kind: 'failed', botId: post.botId, name: bot?.name ?? post.name, color: bot?.color, code, ...(status ? { status } : {}), text: message, time: Date.now() })}rt.roomProgress.set(exec.callId, round)try {@@ -390,6 +408,9 @@ export function install(rt) {} finally {rt.roomProgress.delete(exec.callId)}+ if (exec.signal?.aborted) {+ for (const turn of round.started) void rt.stopRoomTurn?.(room.sessions?.[turn.botId] ?? turn.sessionId, turn.exchangeId)+ }await rt.save()for (const post of posts) {if (post.reportBack && rt.botOf(post.botId)) void reportRound(room, post, round)@@ -515,6 +536,20 @@ export function install(rt) {}rt.lateRoomReply = lateRoomReply+ // The round said such a member posts when done; a failed model call is how it ended.+ // Nobody answers it.+ async function lateRoomFailure(sessionId, info, failure) {+ const owner = rt.groupOwners.get(sessionId)+ const room = rt.roomOf(owner?.roomId ?? info.roomId)+ const member = rt.botOf(owner?.botId)+ if (room === undefined || member === undefined || !room.members.includes(member.id)) return+ const { code, status, message } = failure+ await postToRoom(room, member, `Could not finish: its model call failed (${failureLine(failure)}).`, {+ late: true, quiet: true, failed: { code, ...(status ? { status } : {}), message },+ }).catch(error => rt.warn('ds-bot: late group failure from %s failed: %s', member.name, error))+ }+ rt.lateRoomFailure = lateRoomFailure+// A group the user just made opens with its admin asking what the group is for.async function greetRoom(roomId) {const room = rt.roomOf(roomId)@@ -530,8 +565,10 @@ export function install(rt) {async function reportRound(room, post, round) {const lines = round.replies.filter(reply => reply.kind === undefined).map(reply => `${reply.name}: ${reply.text}`)+ const waiting = round.replies.filter(reply => reply.kind === 'approval').map(reply => reply.name)const text = [lines.length > 0 ? `Replies to your post (oldest first):\n${lines.join('\n')}` : 'Nobody replied to your post.',+ waiting.length > 0 ? `Waiting for the user's approval: ${waiting.join(', ')}. What they say afterwards is posted in the group, not sent to you; read_group_chat shows it.` : '',round.passed.length > 0 ? `Stayed silent: ${round.passed.join(', ')}.` : '',round.failed.length > 0 ? `Could not answer because their model call failed: ${round.replies.filter(reply => reply.kind === 'failed').map(reply => `${reply.name} (${failureLine({ code: reply.code, status: reply.status, message: reply.text })})`).join(', ')}.` : '',].filter(Boolean).join('\n\n')diff --git a/ds-bot/src/host/store.js b/ds-bot/src/host/store.jsindex d29df440..55fbe494 100644--- a/ds-bot/src/host/store.js+++ b/ds-bot/src/host/store.js@@ -315,7 +315,10 @@ export function install(rt) {delete room.sessions[botId]if (room.segments !== undefined) delete room.segments[botId]groupOwners.delete(sessionId)- void ctx.workspaceRegistry.archiveSession(sessionId).catch(() => {})+ rt.cancelApprovals?.(sessionId)+ // A plain archive refuses a running Session, which would go on working for a group+ // that no longer has it.+ void ctx.workspaceRegistry.archiveSession(sessionId, { stopActivity: true }).catch(() => {})}const findBot = (ref) => {if (ref === undefined) return undefineddiff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.jsindex eab4a82d..d519969c 100644--- a/ds-bot/src/host/turns.js+++ b/ds-bot/src/host/turns.js@@ -43,6 +43,27 @@ export function install(rt) {/** @type {Map<string, {text:string, botId?:string, name?:string, reportBack?:boolean, hop?:number, kickoff?:boolean, references?:{labels:string[], text:string}[]}[]>} messages a room's next relay posts */const roomInbox = new Map()const queueRoomMessage = (roomId, item) => roomInbox.set(roomId, [...(roomInbox.get(roomId) ?? []), item])+ // Member turns of a group round the user stopped: what they end with stays out of the group.+ const stoppedRoomTurns = new Set()++ // A stopped round stops the member work it started: the turn that answers it, or the+ // delivery still waiting for the member's Session. An earlier turn there keeps going.+ // An exchange whose turn already ended stays in the set, so it is bounded.+ const STOPPED_KEPT = 200+ async function stopRoomTurn(sessionId, exchangeId) {+ stoppedRoomTurns.add(exchangeId)+ if (stoppedRoomTurns.size > STOPPED_KEPT) stoppedRoomTurns.delete(stoppedRoomTurns.values().next().value)+ const agent = await rt.resolveAgent(sessionId).catch(() => undefined)+ if (agent === undefined) return+ const entry = live.get(sessionId)+ if (entry?.turn !== undefined && entry.info?.exchangeId === exchangeId) {+ agent.cancel({ kind: 'hook', reason: 'ds-bot/stopped' })+ return+ }+ for (const message of [...(agent.inbox?.nextStep ?? []), ...(agent.inbox?.nextTurn ?? [])]) {+ if (message.source?.exchangeId === exchangeId && agent.inbox.remove(message.id)) stoppedRoomTurns.delete(exchangeId)+ }+ }const liveOf = id => {let entry = live.get(id)@@ -233,6 +254,7 @@ export function install(rt) {hop: source.hop ?? 0,...(source.late ? { late: true } : {}),...(source.quiet ? { quiet: true } : {}),+ ...(source.failed ? { failed: source.failed } : {}),})} else if (source?.kind === SOURCE_KIND && source.role === 'kickoff' && rt.roomOf(id)) {queueRoomMessage(id, { text: '', kickoff: true })@@ -327,10 +349,15 @@ export function install(rt) {if (info?.role === 'room') {const waiter = roomWaiters.get(info.exchangeId)roomWaiters.delete(info.exchangeId)- if (failure !== undefined) waiter?.fail(failure)- else if (waiter !== undefined) waiter.resolve(reason?.kind === 'aborted' ? null : texts.join('\n\n').trim())- // The round moved on without this member; what it says now still reaches the group.- else if (reason?.kind !== 'aborted') void rt.lateRoomReply?.(id, info, texts.join('\n\n').trim())+ const stopped = stoppedRoomTurns.delete(info.exchangeId)+ if (waiter !== undefined) {+ if (failure !== undefined) waiter.fail(failure)+ else waiter.resolve(reason?.kind === 'aborted' ? null : texts.join('\n\n').trim())+ } else if (!stopped && reason?.kind !== 'aborted') {+ // The round moved on without this member; how its turn ends still reaches the group.+ if (failure !== undefined) void rt.lateRoomFailure?.(id, info, failure)+ else void rt.lateRoomReply?.(id, info, texts.join('\n\n').trim())+ }} else if (info !== undefined) {answer(info, texts.join('\n\n').trim())}@@ -357,5 +384,5 @@ export function install(rt) {}})- Object.assign(rt, { live, liveOf, roomWaiters, roomProgress, roomInbox, activityView, workView, callDetail })+ Object.assign(rt, { live, liveOf, roomWaiters, roomProgress, roomInbox, activityView, workView, callDetail, stopRoomTurn })}diff --git a/ds-bot/test/group-status.test.js b/ds-bot/test/group-status.test.jsindex fa8bf01c..c30f33d3 100644--- a/ds-bot/test/group-status.test.js+++ b/ds-bot/test/group-status.test.js@@ -32,13 +32,28 @@ async function start() {const agentOf = (sessionId) => {if (!agents.has(sessionId)) {const calls = []- agents.set(sessionId, { session: { id: sessionId }, calls, followup(message) { calls.push(message) }, steer(message) { calls.push(message) } })+ const cancelled = []+ // What the Session has not taken up yet; a test empties it when the turn starts.+ const held = []+ const inbox = {+ nextStep: [],+ nextTurn: held,+ remove(id) {+ const at = held.findIndex(message => message.id === id)+ if (at === -1) return false+ held.splice(at, 1)+ return true+ },+ }+ const keep = (message) => { calls.push(message); held.push(message) }+ agents.set(sessionId, { session: { id: sessionId }, calls, cancelled, inbox, followup: keep, steer: keep, cancel(reason) { cancelled.push(reason) } })}return agents.get(sessionId)}+ const archived = []Object.assign(ctx, {llm: { listProviders: () => [], listModels: async () => [], resolveModelInfo: async () => ({}) },- workspaceRegistry: { get: () => undefined, resolveByPath: async () => undefined, create: async path => ({ id: 'w1', path }), archiveSession: async () => {} },+ workspaceRegistry: { get: () => undefined, resolveByPath: async () => undefined, create: async path => ({ id: 'w1', path }), archiveSession: async (id, options) => { archived.push([id, options]) } },sessionController: { create: async () => ({ sessionId: 's1' }), resolveAgent: async sessionId => ({ agent: agentOf(sessionId) }), selectModel: async () => {} },sessionTitle: { rename: async () => {} },sessions: { flush: async () => {} },@@ -58,7 +73,7 @@ async function start() {const askApproval = (sessionId, extra = {}, next = () => 'from-dsh') => ctx.listeners.get('approval/request')[0]({ agent: { session: { id: sessionId } }, toolName: 'pwsh', reason: 'escalate sandbox', ...extra }, next)const stop = () => { ctx.dispose(); rmSync(home, { recursive: true, force: true }) }await tool('list_bots', {}, 'a')- return { agentOf, emit, tool, call, askApproval, stop }+ return { agentOf, emit, tool, call, askApproval, stop, archived }}// Waits on real I/O (state writes) without timers, which some tests mock.@@ -77,9 +92,12 @@ async function openTurn(t, { agentOf, emit, tool }) {void tool('group_relay', {}, 'r1', { callId: 'c1', signal: stop.signal }).then((value) => { result = JSON.parse(value) })await settle(() => agentOf('gb').calls.length === 1)const message = agentOf('gb').calls[0]+ agentOf('gb').inbox.nextTurn.length = 0emit('gb', 'turn/start', { turn: 1 })emit('gb', 'user/message', { content: message.content, source: message.source })- return () => result+ const read = () => result+ read.stop = () => stop.abort()+ return read}test('a group Session\'s approval waits in the team view, frees the round, and the late reply reaches the group', async (t) => {@@ -149,3 +167,83 @@ test('a member at work keeps its turn; one quiet for the whole wait is missed, aawait settle(() => true)assert.equal(host.agentOf('r1').calls.length, 0)})++// Real I/O that does not end in a condition the test can see.+const idle = () => new Promise(resolve => setTimeout(resolve, 50))++test('stopping a round stops the member turn it started, and that turn posts nothing', async (t) => {+ const host = await start()+ t.after(host.stop)+ const result = await openTurn(t, host)+ result.stop()+ await settle(() => result() !== undefined)+ await settle(() => host.agentOf('gb').cancelled.length === 1)+ assert.equal(host.agentOf('gb').cancelled[0].reason, 'ds-bot/stopped')+ // The turn may end with text before the stop lands.+ host.emit('gb', 'assistant/message', { message: { content: [{ type: 'text', text: 'Done anyway.' }] } })+ host.emit('gb', 'turn/end', { turn: 1, reason: { kind: 'completed' } })+ await idle()+ assert.equal(host.agentOf('r1').calls.length, 0)+})++test('stopping a round withdraws a member turn that has not started', async (t) => {+ const host = await start()+ t.after(host.stop)+ let result+ const stop = new AbortController()+ t.after(() => stop.abort())+ void host.tool('group_relay', {}, 'r1', { callId: 'c1', signal: stop.signal }).then((value) => { result = JSON.parse(value) })+ await settle(() => host.agentOf('gb').calls.length === 1)+ stop.abort()+ await settle(() => result !== undefined)+ await settle(() => host.agentOf('gb').inbox.nextTurn.length === 0)+ assert.deepEqual(host.agentOf('gb').cancelled, [])+})++test('deleting a group cancels its members\' waiting approvals and stops their Sessions', async (t) => {+ const host = await start()+ t.after(host.stop)+ await openTurn(t, host)+ const outcome = host.askApproval('gb')+ assert.equal((await host.call('delete-room', { id: 'r1' })).ok, true)+ assert.equal(await outcome, 'cancelled')+ assert.deepEqual((await host.call('state', {})).value.work.approvals, [])+ assert.deepEqual(host.archived.find(([id]) => id === 'gb'), ['gb', { stopActivity: true }])+})++test('a report back names the members still waiting for an approval', async (t) => {+ const host = await start()+ t.after(host.stop)+ const posted = await host.tool('post_to_group', { group: 'Ops', message: 'Copy the doc', report_back: true }, 'a')+ assert.match(posted, /^Posted in "Ops"/)+ const post = host.agentOf('r1').calls[0]+ host.emit('r1', 'user/message', { content: post.content, source: post.source })+ const result = await openTurn(t, host)+ void host.askApproval('gb')+ await settle(() => result() !== undefined)+ await settle(() => host.agentOf('a').calls.length === 1)+ const report = text(host.agentOf('a').calls[0])+ assert.match(report, /Nobody replied to your post\./)+ assert.match(report, /Waiting for the user's approval: Writer\./)+})++test('a member that fails after the round moved on shows the failure in the group, and nobody answers it', async (t) => {+ const host = await start()+ t.after(host.stop)+ const result = await openTurn(t, host)+ const outcome = host.askApproval('gb')+ await settle(() => result() !== undefined)+ const [approval] = (await host.call('state', {})).value.work.approvals+ await host.call('approval-answer', { id: approval.id, choice: 'allow' })+ await outcome+ host.emit('gb', 'turn/end', { turn: 1, reason: { kind: 'error', error: { code: 'RATE_LIMIT', status: 429, message: 'Too many requests' } } })+ await settle(() => host.agentOf('r1').calls.length === 1)+ const post = host.agentOf('r1').calls[0]+ assert.deepEqual([post.source.late, post.source.quiet, post.source.failed], [true, true, { code: 'RATE_LIMIT', status: 429, message: 'Too many requests' }])++ host.emit('r1', 'user/message', { content: post.content, source: post.source })+ const round = JSON.parse(await host.tool('group_relay', {}, 'r1', { callId: 'c2', signal: new AbortController().signal }))+ assert.deepEqual(round.replies.map(reply => [reply.kind, reply.name, reply.code, reply.status]), [['failed', 'Writer', 'RATE_LIMIT', 429]])+ assert.deepEqual(round.failed, ['Writer'])+ assert.equal(host.agentOf('gb').calls.length, 1)+})