Skip to content

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

Drain mid-round user messages after the last speaker too

lulu feizhue168eaff33dd
diff --git a/ds-bot/src/host/channel.js b/ds-bot/src/host/channel.js
index 63463382..110370fb 100644
--- a/ds-bot/src/host/channel.js
+++ b/ds-bot/src/host/channel.js
@@ -264,13 +264,28 @@ export function install(rt) {
const sessionId = rt.botOf(body.sessionId) ? rt.chatOf(body.sessionId) : past && past.roomId === undefined ? rt.chatOf(past.botId) : body.sessionId
// While a group is mid-round its queued copy waits out the whole round;
// record it for the running round too so the next speaker sees it now.
- if (rt.roomOf(sessionId) !== undefined && rt.roomBusy(sessionId)) rt.queueRoomMessage(sessionId, { text, early: true })
- await ctx.sessionController.prompt({
- requestId: randomUUID(),
- sessionId,
- mode: 'queue',
- content: [{ type: 'text', text }],
- }, AbortSignal.timeout(30_000))
+ const early = rt.roomOf(sessionId) !== undefined && rt.roomBusy(sessionId)
+ ? rt.queueRoomMessage(sessionId, { text, early: true })
+ : undefined
+ try {
+ await ctx.sessionController.prompt({
+ requestId: randomUUID(),
+ sessionId,
+ mode: 'queue',
+ content: [{ type: 'text', text }],
+ }, AbortSignal.timeout(30_000))
+ } catch (error) {
+ // The round may have drained the record while prompt() was in flight;
+ // then the message reached the room, so this send still succeeded.
+ if (early !== undefined && early.posted === true) return ok(true)
+ if (early !== undefined) {
+ const inbox = rt.roomInbox.get(sessionId) ?? []
+ const at = inbox.indexOf(early)
+ if (at !== -1) inbox.splice(at, 1)
+ void rt.save()
+ }
+ throw error
+ }
return ok(true)
}
case 'update-check': {
diff --git a/ds-bot/src/host/groups.js b/ds-bot/src/host/groups.js
index 8f93528c..5a1785d7 100644
--- a/ds-bot/src/host/groups.js
+++ b/ds-bot/src/host/groups.js
@@ -195,12 +195,14 @@ export function install(rt) {
}
}
+ const freshInbox = room => (rt.roomInbox.get(room.id) ?? [])
+ .filter(item => item.early === true && item.posted !== true && item.consumed !== true)
+
// Messages the user sent while this round is running post into the log between
// speakers, so the members still to speak see them; a member they @ is called
// right after this slot, as if the admin had called it.
function drainRoomInbox(room, round, turns, index) {
- const fresh = (rt.roomInbox.get(room.id) ?? [])
- .filter(item => item.early === true && item.posted !== true && item.consumed !== true)
+ const fresh = freshInbox(room)
if (fresh.length === 0) return
const others = room.members.map(rt.botOf).filter(Boolean)
for (const item of fresh) {
@@ -215,15 +217,20 @@ export function install(rt) {
if (next?.status === 'waiting' && callees.some(peer => peer.id === next.id)) next.note = 'the user mentioned you'
callMembers(room, round, turns, index, callees.filter(peer => peer.id !== next?.id), 'the user', ROOM_ADMIN_TURNS_EACH)
}
+ // The user spoke in this room, so Bots may start their own rounds again.
+ room.botRounds = 0
void rt.save()
}
async function runRound(room, round, signal) {
const limit = round.slots.length + ROOM_EXTRA_TURNS + (room.admin ? ROOM_ADMIN_EXTRA_TURNS : 0)
const turns = new Map()
- for (let index = 0; index < round.slots.length && round.turns < limit; index += 1) {
+ // The inbox check keeps a message sent during the last speaker's turn inside
+ // this round: it drains at the loop's head even once the slots ran out.
+ for (let index = 0; (index < round.slots.length || freshInbox(room).length > 0) && round.turns < limit; index += 1) {
if (signal.aborted) break
drainRoomInbox(room, round, turns, index)
+ if (index >= round.slots.length) break
const slot = round.slots[index]
const member = rt.botOf(slot.id)
// Settings may change mid-round, for example when the admin removes someone.
diff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.js
index ab4d3dbe..ebc19dde 100644
--- a/ds-bot/src/host/turns.js
+++ b/ds-bot/src/host/turns.js
@@ -32,7 +32,10 @@ export function install(rt) {
const roomProgress = new Map()
/** @type {Map<string, {text:string, botId?:string, name?:string, reportBack?:boolean, hop?:number, kickoff?:boolean}[]>} messages a room's next relay posts */
const roomInbox = new Map()
- const queueRoomMessage = (roomId, item) => roomInbox.set(roomId, [...(roomInbox.get(roomId) ?? []), item])
+ const queueRoomMessage = (roomId, item) => {
+ roomInbox.set(roomId, [...(roomInbox.get(roomId) ?? []), item])
+ return item
+ }
const liveOf = id => {
let entry = live.get(id)
diff --git a/ds-bot/test/rounds.test.js b/ds-bot/test/rounds.test.js
index 83781742..bbe10028 100644
--- a/ds-bot/test/rounds.test.js
+++ b/ds-bot/test/rounds.test.js
@@ -1,5 +1,5 @@
import assert from 'node:assert/strict'
-import { mkdtempSync, rmSync } from 'node:fs'
+import { mkdtempSync, readFileSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { test } from 'node:test'
@@ -68,7 +68,7 @@ function start() {
ctx.dispose()
rmSync(home, { recursive: true, force: true })
}
- return { call, emit, userMessage, answer, stop, sent, prompts, ctx }
+ return { call, emit, userMessage, answer, stop, sent, prompts, ctx, home }
}
const settle = () => new Promise(resolve => setTimeout(resolve, 20))
@@ -120,6 +120,90 @@ test('a message sent mid-round joins the round: the next speaker sees it, the qu
assert.equal(sent.length, before)
})
+test('a message sent during the last speaker\'s turn still joins the round', async (t) => {
+ const { call, emit, userMessage, answer, stop, sent, ctx } = start()
+ t.after(stop)
+ const chief = (await call('state', {})).value.mainBotId
+ const writer = (await call('create-bot', { name: 'Writer' })).value
+ const room = (await call('create-room', { members: [chief, writer.id] })).value
+ await call('update-room', { id: room.id, mode: 'everyone' })
+ sent.length = 0
+
+ await emit(room.id, userMessage('Kick it off'))
+ const relay = ctx.registeredTools.get('group_relay').execute({}, { agent: { id: room.id }, callId: 'relay', signal: new AbortController().signal })
+ const first = await waitFor(() => sent.find(entry => entry.message.source.role === 'room'))
+ await answer(first.sessionId, first.message.source.exchangeId, 'Chief first', 1)
+ const second = await waitFor(() => sent.find(entry => entry !== first && entry.message.source.role === 'room'))
+
+ // The last member is already answering when the message lands.
+ assert.equal((await call('send', { sessionId: room.id, text: '@Chief once more late' })).ok, true)
+ await answer(second.sessionId, second.message.source.exchangeId, 'Writer done', 2)
+
+ const third = await waitFor(() => sent.find(entry => entry !== first && entry !== second && entry.message.source.role === 'room'))
+ assert.ok(third, 'Chief is called back for the late message')
+ assert.match(third.message.content[0].text, /@Chief once more late/)
+ assert.match(third.message.content[0].text, /the user mentioned you/)
+ await answer(third.sessionId, third.message.source.exchangeId, 'Back again', 3)
+ const result = JSON.parse(await relay)
+ assert.deepEqual(result.replies.map(reply => reply.name ?? 'User'), ['Chief', 'Writer', 'User', 'Chief'])
+})
+
+test('a mid-round user line resets the room\'s bot-round budget', async (t) => {
+ const { call, emit, userMessage, answer, stop, sent, ctx, home } = start()
+ t.after(stop)
+ const chief = (await call('state', {})).value.mainBotId
+ const writer = (await call('create-bot', { name: 'Writer' })).value
+ const room = (await call('create-room', { name: 'R1', members: [chief, writer.id] })).value
+ await call('update-room', { id: room.id, mode: 'everyone' })
+ sent.length = 0
+
+ await emit(room.id, userMessage('Start'))
+ const relay = ctx.registeredTools.get('group_relay').execute({}, { agent: { id: room.id }, callId: 'relay', signal: new AbortController().signal })
+ const first = await waitFor(() => sent.find(entry => entry.message.source.role === 'room'))
+ await answer(first.sessionId, first.message.source.exchangeId, 'one', 1)
+ const second = await waitFor(() => sent.find(entry => entry !== first && entry.message.source.role === 'room'))
+
+ // A Main Bot posts into the room while the last member is still answering:
+ // that spends one bot-round budget.
+ const posted = await ctx.registeredTools.get('post_to_group').execute({ group: 'R1', message: 'bot note' }, { agent: { id: first.sessionId } })
+ const roomState = () => JSON.parse(readFileSync(join(home, 'state.json'), 'utf8')).rooms[room.id]
+ assert.equal(roomState().botRounds, 1, String(posted))
+
+ // The user's mid-round message counts as the user speaking: the budget reopens.
+ assert.equal((await call('send', { sessionId: room.id, text: 'user back mid-round' })).ok, true)
+ await answer(second.sessionId, second.message.source.exchangeId, 'two', 2)
+ JSON.parse(await relay)
+ assert.equal(roomState().botRounds, 0)
+})
+
+test('a failed send drops the early record so the round never sees it', async (t) => {
+ const { call, emit, userMessage, answer, stop, sent, ctx } = start()
+ t.after(stop)
+ const chief = (await call('state', {})).value.mainBotId
+ const writer = (await call('create-bot', { name: 'Writer' })).value
+ const room = (await call('create-room', { members: [chief, writer.id] })).value
+ await call('update-room', { id: room.id, mode: 'everyone' })
+ sent.length = 0
+
+ await emit(room.id, userMessage('Start'))
+ const relay = ctx.registeredTools.get('group_relay').execute({}, { agent: { id: room.id }, callId: 'relay', signal: new AbortController().signal })
+ const first = await waitFor(() => sent.find(entry => entry.message.source.role === 'room'))
+
+ // While the admin is answering, the Session-side queueing fails.
+ const prompt = ctx.sessionController.prompt
+ ctx.sessionController.prompt = async () => { throw new Error('queue full') }
+ const failed = await call('send', { sessionId: room.id, text: 'ghost mid-round' })
+ ctx.sessionController.prompt = prompt
+ assert.equal(failed.ok, false)
+
+ await answer(first.sessionId, first.message.source.exchangeId, 'one', 1)
+ const second = await waitFor(() => sent.find(entry => entry !== first && entry.message.source.role === 'room'))
+ assert.doesNotMatch(second.message.content[0].text, /ghost mid-round/)
+ await answer(second.sessionId, second.message.source.exchangeId, 'two', 2)
+ const result = JSON.parse(await relay)
+ assert.equal(result.replies.filter(reply => reply.text === 'ghost mid-round').length, 0)
+})
+
test('a message sent between rounds still opens a full round', async (t) => {
const { call, emit, userMessage, answer, stop, sent, ctx } = start()
t.after(stop)