A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Deliver group corrections at the next step without losing their caller
Fei241107238744eff1
diff --cc ds-bot/src/host/delivery.jsindex 96220244,96220244..3da64cbd--- a/ds-bot/src/host/delivery.js+++ b/ds-bot/src/host/delivery.js@@@ -19,7 -19,7 +19,7 @@@ export function install(rt)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}]`if (deferred && role === 'report') return `[Group chat "${roomName}" · replies to your post · you read them during your last task; go on with the work they are for now${goesOn}]`-- if (deferred) return `[Message from ${fromName} · you read it during your last task; answer it now]`++ if (deferred && role !== 'room') return `[Message from ${fromName} · you read it during your last task; answer it now]`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]`diff --cc ds-bot/src/host/groups.jsindex 4b54ab76,223913d8..7236d05e--- a/ds-bot/src/host/groups.js+++ b/ds-bot/src/host/groups.js@@@ -326,10 -326,10 +326,10 @@@ export function install(rt)signal.addEventListener('abort', () => finish(null), { once: true })})try {-- await rt.deliver({ fromId: undefined, toId: member.id, sessionId, role: 'room', roomId: room.id, exchangeId, note, text, files, ...(by ? { askedBy: by } : {}) })++ await rt.deliver({ fromId: undefined, toId: member.id, sessionId, role: 'room', roomId: room.id, exchangeId, note, text, files, steer: busy, ...(by ? { askedBy: by } : {}) })// Still held by an earlier request: this turn cannot start until the user answers.if (rt.waitsForApproval?.(sessionId)) rt.roomWaiters.get(exchangeId)?.approval()-- // Still at earlier work: the call waits in its inbox and is answered after that.++ // Still at earlier work: the call steers it at its next step; the round goes on.else if (busy) rt.roomWaiters.get(exchangeId)?.resolve({ working: true })} catch (error) {rt.warn('ds-bot: group delivery to %s failed: %s', member.name, error)diff --cc ds-bot/src/host/turns.jsindex a0e31295,9e563b71..a7106b52--- a/ds-bot/src/host/turns.js+++ b/ds-bot/src/host/turns.js@@@ -467,7 -474,7 +474,7 @@@ export function install(rt)// A result for other work than this turn's informs it, and comes back in a// turn of its own once this one ends, which goes on with that work.entry.later = [...(entry.later ?? []), { info: source, text: textOf(event.data.content).replace(/^\[(?:[^\r\n]*\]\r?\n|[^\]\r\n]*\])/, '') }]-- } else if (expectsReply(source) && !answersUser(source)++ } else if ((expectsReply(source) || source.role === 'room') && !answersUser(source)&& source.exchangeId !== entry.info?.exchangeId&& !(entry.joined ?? []).some(joined => joined.info.exchangeId === source.exchangeId)) {// A request that lands inside a running turn steers that work but is@@@ -628,10 -644,10 +644,13 @@@finish(info, replyText())}for (const request of joined) {++ if (request.info.role === 'room' && stoppedRoomTurns.delete(request.info.exchangeId)) continue// A failed or stopped turn closes the request like any other; a finished// one hands it back to the Bot to answer next.if (failure !== undefined || reason?.kind === 'aborted') {-- answer(request.info, '')++ if (request.info.role === 'room') {++ if (failure !== undefined) void rt.lateRoomFailure?.(id, request.info, failure)++ } else answer(request.info, '')continue}void rt.deliver({@@@ -639,8 -655,8 +658,9 @@@toId: rt.selfOf(id),sessionId: id,text: request.text,-- role: 'request',++ role: request.info.role,exchangeId: request.info.exchangeId,++ ...(request.info.role === 'room' ? { roomId: request.info.roomId, askedBy: request.info.askedBy, note: 'you read this call during your last task; answer it now' } : {}),...(request.info.senderRoomId ? { fromRoomId: request.info.senderRoomId } : {}),deferred: true,}).catch(error => rt.warn('ds-bot: deferred request delivery failed: %s', error))diff --cc ds-bot/test/group-work.test.jsindex 68366f69,eea9c588..1ead3aff--- a/ds-bot/test/group-work.test.js+++ b/ds-bot/test/group-work.test.js@@@ -35,19 -35,18 +35,20 @@@ async function start(t, room, servicesconst calls = []const cancelled = []const held = []++ const nextStep = []const inbox = {-- nextStep: [],++ nextStep,nextTurn: held,remove(id) {-- const at = held.findIndex(message => message.id === id)-- if (at === -1) return false-- held.splice(at, 1)-- return true++ for (const list of [nextStep, held]) {++ const at = list.findIndex(message => message.id === id)++ if (at !== -1) { list.splice(at, 1); return true }++ }++ return false},}- const kinds = []- const keep = kind => (message) => { calls.push(message); held.push(message); kinds.push(kind) }- agents.set(sessionId, { session: { id: sessionId }, calls, cancelled, inbox, kinds, followup: keep('followup'), steer: keep('steer'), cancel(reason) { cancelled.push(reason) } })- 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) } })++ const keep = list => message => { calls.push(message); list.push(message) }++ agents.set(sessionId, { session: { id: sessionId }, calls, cancelled, inbox, followup: keep(held), steer: keep(nextStep), cancel(reason) { cancelled.push(reason) } })}return agents.get(sessionId)}@@@ -79,6 -78,6 +80,7 @@@const begin = (sessionId) => {const message = agentOf(sessionId).calls.at(-1)agentOf(sessionId).inbox.nextTurn.length = 0++ agentOf(sessionId).inbox.nextStep.length = 0const turn = (turns.get(sessionId) ?? 0) + 1turns.set(sessionId, turn)emit(sessionId, 'turn/start', { turn })@@@ -304,8 -303,8 +306,42 @@@ test('a member called while still at eaassert.deepEqual(kinds(result()), [['working', 'Writer']])// A member at work has not run its turn here, and an event is not a message.assert.equal(result().replies[0].duration, undefined)-- // The call waits for the work before it.-- assert.equal(host.agentOf('gb').inbox.nextTurn.length, 1)++ // The correction reaches the running turn at its next step.++ assert.equal(host.agentOf('gb').inbox.nextTurn.length, 0)++ assert.equal(host.agentOf('gb').inbox.nextStep.length, 1)++ assert.match(text(host.agentOf('gb').inbox.nextStep[0]), /再看看这个/)++})++++test('a steered group call keeps the later caller when the earlier work ends', async (t) => {++ t.mock.timers.enable({ apis: ['setTimeout'] })++ const host = await start(t)++ const first = host.round('c1', host.say('@Writer 整理文档'))++ await settle(() => host.agentOf('gb').calls.length === 1)++ host.begin('gb')++ host.emit('gb', 'tool/call', { callId: 'work', name: 'write', arguments: '{}' })++ t.mock.timers.tick(ROOM_WORK_GRACE_MS)++ await settle(() => first() !== undefined)++++ await host.tool('post_to_group', { group: 'Ops', message: '@Writer 改用新模板' }, 'a')++ const second = host.round('c2', host.takePost())++ await settle(() => second() !== undefined)++ const inbox = host.agentOf('gb').inbox++ const correction = inbox.nextStep[0]++ assert.ok(correction)++ inbox.remove(correction.id)++ host.emit('gb', 'user/message', { content: correction.content, source: correction.source })++ host.finish('gb', '原来的工作完成了。')++++ await settle(() => inbox.nextTurn.length === 1)++ const deferred = host.begin('gb')++ assert.equal(deferred.source.role, 'room')++ assert.equal(deferred.source.askedBy, 'a')++ assert.equal(deferred.source.roomId, 'r1')++ assert.match(text(deferred), /改用新模板/)++ host.finish('gb', '已换成新模板。')++ await settle(() => host.agentOf('r1').calls.some(message => message.source.askedBy === 'a'))++ const report = host.agentOf('r1').calls.find(message => message.source.askedBy === 'a')++ assert.match(text(report), /已换成新模板/)})test('a quick team tool does not let the round go on', async (t) => {