Skip to content

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

Keep a part switch and a pending request safe across a steer cut

Fei2411c74b35c0c1e8
diff --git a/ds-bot/src/host/steer.js b/ds-bot/src/host/steer.js
index 8ff71b8c..17d75d41 100644
--- a/ds-bot/src/host/steer.js
+++ b/ds-bot/src/host/steer.js
@@ -42,7 +42,7 @@ export function install(rt) {
const entry = rt.live.get(id)
const agent = liveAgent(id)
if (!privateOf(id) || rt.readOnly?.() !== undefined || entry === undefined || agent === undefined) return
- if ((rt.switchJobs?.() ?? []).some?.(job => job?.botId === rt.chatOwners.get(id))) return
+ if ([...(rt.switchJobs?.() ?? [])].some(job => job?.botId === rt.chatOwners.get(id))) return
const track = stateOf(id)
if (track.phase !== 'model' || track.cutting) return
if (entry.turn === undefined || (entry.calls?.size ?? 0) > 0 || agent.status !== 'running') return
diff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.js
index ecbf6fca..fbde4ebe 100644
--- a/ds-bot/src/host/turns.js
+++ b/ds-bot/src/host/turns.js
@@ -89,7 +89,7 @@ export function install(rt) {
})
}
- /** @type {Map<string, {turn?:number, pending?:any, info?:any, texts:string[], opened?:boolean, joined?:{info:any, from:number}[]}>} */
+ /** @type {Map<string, {turn?:number, pending?:any, pendingText?:string, info?:any, texts:string[], opened?:boolean, joined?:{info:any, from:number}[]}>} */
const live = new Map()
/** @type {Map<string, {sessionId:string, resolve:(text:string|null)=>void, fail:(failure:any)=>void, touch:()=>void, approval:()=>void}>} member turn waiters keyed by exchange id */
const roomWaiters = new Map()
@@ -409,9 +409,14 @@ export function install(rt) {
entry.later = []
entry.steeredAt = undefined
entry.carried = false
+ let claimedPending
+ let claimedPendingText
if (entry.pending !== undefined) {
- claimTrigger(id, entry, entry.pending)
+ claimedPending = entry.pending
+ claimedPendingText = entry.pendingText
+ claimTrigger(id, entry, claimedPending)
entry.pending = undefined
+ entry.pendingText = undefined
}
if (entry.carry !== undefined) {
const carry = entry.carry
@@ -425,6 +430,13 @@ export function install(rt) {
entry.carried = true
if (carry.info !== undefined) claimTrigger(id, entry, carry.info)
entry.joined = carry.joined
+ // A request the carry displaced as this turn's trigger keeps its deferred
+ // place, the same as a request that joins a running turn.
+ if (carry.info !== undefined && claimedPending !== undefined
+ && claimedPending.exchangeId !== carry.info.exchangeId
+ && (expectsReply(claimedPending) || claimedPending.role === 'room') && !answersUser(claimedPending)) {
+ entry.joined = [...carry.joined, { info: claimedPending, text: claimedPendingText }]
+ }
}
break
case 'user/message': {
@@ -482,7 +494,10 @@ export function install(rt) {
} else if (source?.kind === SOURCE_KIND && source.role === 'resume' && rt.roomOf(id)) {
queueRoomMessage(id, { text: '', resume: true })
} else if (source?.kind === SOURCE_KIND) {
- if (entry.turn === undefined) entry.pending = source
+ if (entry.turn === undefined) {
+ entry.pending = source
+ entry.pendingText = textOf(event.data.content).replace(/^\[(?:[^\r\n]*\]\r?\n|[^\]\r\n]*\])/, '')
+ }
else if (!opened) claimTrigger(id, entry, source)
else if (source.work !== undefined && source.work !== rt.workOf?.(entry.info) && !expectsReply(source)) {
// A result for other work than this turn's informs it, and comes back in a
diff --git a/ds-bot/test/steer.test.js b/ds-bot/test/steer.test.js
index febe3183..11ae5f16 100644
--- a/ds-bot/test/steer.test.js
+++ b/ds-bot/test/steer.test.js
@@ -372,6 +372,58 @@ test('steer carry: a cut turn that ends on error keeps normal failure handling',
await stop()
})
+test('steer carry: a pending request survives a carry that reclaims the trigger', async () => {
+ const { emit, stop, rt, deliveries } = await start()
+ const dm = 'dm-a'
+ await emit(dm, 'turn/start', { turn: 1 })
+ await emit(dm, 'user/message', {
+ content: [{ type: 'text', text: 'do the thing' }],
+ source: { kind: SOURCE_KIND, role: 'request', senderSessionId: 'bot-b', exchangeId: 'x1', senderName: 'Writer' },
+ })
+ await emit(dm, 'assistant/message', { message: { role: 'assistant', content: [{ type: 'text', text: 'half done' }] } })
+ rt.markSteerCut(dm)
+ await emit(dm, 'turn/end', { reason: { kind: 'aborted', reason: { kind: 'hook', reason: STEER_CUT } } })
+ assert.deepEqual(deliveries, [])
+ // Another request lands between the cut and the next turn: it waits as pending.
+ await emit(dm, 'user/message', {
+ content: [{ type: 'text', text: '[Ops → Chief]\nme later' }],
+ source: { kind: SOURCE_KIND, role: 'request', senderSessionId: 'bot-c', exchangeId: 'x2', senderName: 'Ops' },
+ })
+ await emit(dm, 'turn/start', { turn: 2 })
+ const entry = rt.live.get(dm)
+ // The carry's own request reclaims the trigger; the pending one joins it deferred.
+ assert.equal(entry.info?.exchangeId, 'x1')
+ assert.equal(entry.joined.length, 1)
+ assert.equal(entry.joined[0].info.exchangeId, 'x2')
+ assert.equal(entry.joined[0].text, 'me later')
+ await emit(dm, 'assistant/message', { message: { role: 'assistant', content: [{ type: 'text', text: 'all done' }] } })
+ await emit(dm, 'turn/end', { reason: { kind: 'completed' } })
+ const reply = deliveries.find(d => d[0]?.replyTo === 'x1')
+ assert.ok(reply !== undefined)
+ assert.match(reply[0].text, /all done/)
+ const handback = deliveries.find(d => d[0]?.deferred === true)
+ assert.ok(handback !== undefined)
+ assert.equal(handback[0].exchangeId, 'x2')
+ assert.ok(!deliveries.some(d => /was stopped/.test(d[0]?.text ?? '')))
+ await stop()
+})
+
+test('steer cut: a running part switch blocks the cut', async () => {
+ const { emit, stop, rt, agent, calls } = await start()
+ const dm = 'dm-a'
+ // switchJobs() hands back a Map values iterator, never an array.
+ const original = rt.switchJobs
+ rt.switchJobs = () => new Map([['job', { botId: rt.chatOwners.get(dm) }]]).values()
+ await emit(dm, 'turn/start', { turn: 1 })
+ await emit(dm, 'step/start', { turn: 1, step: 1 })
+ const steer = userMessage('还没好吗')
+ agent.inbox.nextStep.push(steer)
+ await emit(dm, 'agent/inbox/spliced', { target: 'next-step', inserted: [steer] })
+ assert.equal(calls.find(c => c[0] === 'cancel'), undefined)
+ rt.switchJobs = original
+ await stop()
+})
+
// Model-cut tests.
test('steer cut: user steer during model phase cancels with hook and moves steer to next-turn', async () => {
const { emit, stop, agent, calls } = await start()