A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Reach the subagents service so Stop all stops a group
AdamPlatin1239ee9cfbd4534
diff --git a/ds-bot/src/host/channel.js b/ds-bot/src/host/channel.jsindex 720b7c38..de603025 100644--- a/ds-bot/src/host/channel.js+++ b/ds-bot/src/host/channel.js@@ -367,16 +367,19 @@ export function install(rt) {case 'approval-answer':rt.answerApproval(body.id, body.choice)return ok(true)- case 'stop-member-work': {- const stopped = await rt.stopMemberWork(String(body.roomId ?? ''), String(body.botId ?? ''))- rt.goalUserStopped?.(String(body.roomId ?? ''))- return ok(stopped)- }- case 'stop-room-work': {- const stopped = await rt.stopRoomWork(String(body.roomId ?? ''))- rt.goalUserStopped?.(String(body.roomId ?? ''))- return ok(stopped)- }+ // A stop that fails partway still parks the goal, or it wakes the admin again.+ case 'stop-member-work':+ try {+ return ok(await rt.stopMemberWork(String(body.roomId ?? ''), String(body.botId ?? '')))+ } finally {+ rt.goalUserStopped?.(String(body.roomId ?? ''))+ }+ case 'stop-room-work':+ try {+ return ok(await rt.stopRoomWork(String(body.roomId ?? '')))+ } finally {+ rt.goalUserStopped?.(String(body.roomId ?? ''))+ }case 'goal-draft': {await rt.load()const result = await rt.goalDraft(String(body.roomId ?? ''), body.text)diff --git a/ds-bot/src/host/groups.js b/ds-bot/src/host/groups.jsindex 9c6d5651..68fb182f 100644--- a/ds-bot/src/host/groups.js+++ b/ds-bot/src/host/groups.js@@ -1008,7 +1008,10 @@ export function install(rt) {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)+ for (const turn of round.started) {+ void rt.stopRoomTurn?.(room.sessions?.[turn.botId] ?? turn.sessionId, turn.exchangeId)+ ?.catch(error => rt.warn('ds-bot: stopping a turn of the round in %s failed: %s', room.name, error))+ }} else if (rt.goalDrive !== undefined) {// A round that ends with an active goal and nothing running may wake the admin.try {@@ -1356,15 +1359,27 @@ export function install(rt) {// The user stops everything at work in this group at once: the round the group's own// Session runs, which takes the member turns it started with it, then each member's// turn here. The round goes first, so it calls no one else meanwhile. Work the same- // Bots do in their own chats or other groups goes on.+ // Bots do in their own chats or other groups goes on. What cannot be stopped does not+ // keep the rest going; the first failure is reported once the others are stopped.async function stopRoomWork(roomId) {await rt.load()const room = rt.roomOf(roomId)if (room === undefined) return false+ const failures = []+ const attempt = async (work) => {+ try {+ return await work()+ } catch (error) {+ rt.warn('ds-bot: stopping work in %s failed: %s', room.name, error)+ failures.push(error)+ return false+ }+ }const round = rt.roomBusy(room.id)- if (round) (await rt.resolveAgent(rt.sessionOfRoom(room))).cancel({ kind: 'user' })+ if (round) await attempt(async () => (await rt.resolveAgent(rt.sessionOfRoom(room))).cancel({ kind: 'user' }))let stopped = false- for (const botId of room.members) stopped = (await stopMemberWork(room.id, botId)) || stopped+ for (const botId of room.members) stopped = (await attempt(() => stopMemberWork(room.id, botId))) || stopped+ if (failures.length > 0) throw failures[0]return round || stopped}rt.stopRoomWork = stopRoomWorkdiff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.jsindex 641d7b31..ac4e19a4 100644--- a/ds-bot/src/host/turns.js+++ b/ds-bot/src/host/turns.js@@ -232,7 +232,8 @@ export function install(rt) {stoppedSubagentParents.add(sessionId)for (const childId of childIds) {rememberStopped(stoppedSubagents, childId)- await Promise.resolve(ctx.subagents?.interrupt?.(childId, { kind: 'user', parentSessionId: sessionId })).catch(() => {})+ // Not in the plugin's inject list, where ctx.subagents throws; interrupt can throw too.+ try { await ctx.get?.('subagents')?.interrupt?.(childId, { kind: 'user', parentSessionId: sessionId }) } catch {}const childAgent = ctx.get?.('agents')?.get?.(childId)if (childAgent !== undefined) {try { childAgent.cancel?.({ kind: 'user' }) } catch {}diff --git a/ds-bot/test/fake-ctx.js b/ds-bot/test/fake-ctx.jsindex 9e9a995f..095bcc57 100644--- a/ds-bot/test/fake-ctx.js+++ b/ds-bot/test/fake-ctx.js@@ -46,6 +46,8 @@ export function fakeContext({ services = {} } = {}) {sections,logger: { warn() {}, info() {} },get: name => services[name],+ // As in real cordis: the plugin does not inject this service, so reading it throws.+ get subagents() { throw new Error('cannot get property "subagents" without inject') },on(name, listener, options) {calls.push(`on ${name}${options?.prepend ? ' (prepend)' : ''}`)listeners.set(name, [...(listeners.get(name) ?? []), listener])diff --git a/ds-bot/test/group-goal.test.js b/ds-bot/test/group-goal.test.jsindex 00e034ef..9b3ab745 100644--- a/ds-bot/test/group-goal.test.js+++ b/ds-bot/test/group-goal.test.js@@ -552,6 +552,17 @@ test('stop-room-work sets the goal waiting and no check follows the notice', asyassert.equal(host.takeGoal(), undefined)})+for (const [endpoint, body] of [['stop-room-work', { roomId: 'r1' }], ['stop-member-work', { roomId: 'r1', botId: 'b' }]]) {+ test(`${endpoint} that fails still sets the goal waiting`, async (t) => {+ const host = await start(t)+ await startGoal(host)+ host.emit('gb', 'turn/start', { turn: 1 })+ host.agentOf('gb').cancel = () => { throw new Error('cancel failed') }+ assert.equal((await host.call(endpoint, body)).ok, false)+ await settle(() => goal(host).idle === true)+ })+}+test('a batch member that finishes first is reported while its peer still works', async (t) => {t.mock.timers.enable({ apis: ['setTimeout'] })const host = await start(t)diff --git a/ds-bot/test/group-work.test.js b/ds-bot/test/group-work.test.jsindex dffa7082..ff71f945 100644--- a/ds-bot/test/group-work.test.js+++ b/ds-bot/test/group-work.test.js@@ -1022,6 +1022,46 @@ test('one press stops the group\'s round and every member at work there, and notassert.deepEqual(kinds(first()).filter(([kind]) => kind === 'stopped'), [['stopped', 'Chief'], ['stopped', 'Writer']])})+test('a member that cannot be stopped leaves the others stopped and reports the failure', async (t) => {+ const host = await start(t)+ host.emit('ga', 'turn/start', { turn: 1 })+ host.emit('gb', 'turn/start', { turn: 1 })+ host.agentOf('ga').cancel = () => { throw new Error('cancel failed') }+ const stopped = await host.call('stop-room-work', { roomId: 'r1' })+ assert.equal(stopped.ok, false)+ assert.match(stopped.error.message, /cancel failed/)+ assert.deepEqual(host.agentOf('gb').cancelled, [{ kind: 'user' }])+ await settle(() => host.agentOf('r1').calls.length === 1)+ assert.equal(host.takePost().source.stopped, true)+})++test('a subagent whose interrupt throws does not stop the member from being stopped', async (t) => {+ const host = await start(t, undefined, {+ subagents: { interrupt: () => { throw new Error('belongs to another parent') } },+ })+ host.emit('gb', 'turn/start', { turn: 1 })+ host.emit('gb', 'subagent/catalog', { childId: 'sub-1', label: 'Long run', mode: 'continuable' })+ const stopped = await host.call('stop-room-work', { roomId: 'r1' })+ assert.deepEqual([stopped.ok, stopped.value], [true, true])+ assert.deepEqual(host.agentOf('gb').cancelled, [{ kind: 'user' }])+})++// An unhandled rejection here would end the Host.+test('a stopped round warns when a member turn cannot be stopped', async (t) => {+ const host = await start(t)+ const warned = []+ host.ctx.logger.warn = (...args) => { warned.push(args.join(' ')) }+ const stop = new AbortController()+ host.emit('r1', 'user/message', host.say('@Writer 写一下'))+ const relay = host.tool('group_relay', {}, 'r1', { callId: 'c1', signal: stop.signal })+ await settle(() => host.agentOf('gb').calls.length === 1)+ host.begin('gb')+ host.agentOf('gb').cancel = () => { throw new Error('cancel failed') }+ stop.abort()+ await relay+ await settle(() => warned.some(line => /cancel failed/.test(line)))+})+test('members still at work show in the team view and in the prompts of the others', async (t) => {const host = await start(t)host.emit('gb', 'turn/start', { turn: 1 })@@ -1557,11 +1597,10 @@ test('consecutive 0-tool ack ping-pong relays pause after 6 turns and go on only// stays marked as working in the group, and stopping the group interrupts the subagent// and suppresses its late settlement notice.test('a group member with a running DSH subagent stays working and stop-room-work interrupts the subagent (#701)', async (t) => {- const host = await start(t)const interrupted = []- host.ctx.subagents = {- interrupt: async (childId, opts) => { interrupted.push({ childId, opts }) },- }+ const host = await start(t, undefined, {+ subagents: { interrupt: async (childId, opts) => { interrupted.push({ childId, opts }) } },+ })const first = host.round('c1', host.say('@Writer 派子代理跑长任务'))await settle(() => host.agentOf('gb').calls.length === 1)host.begin('gb')