A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Follow a steer cut to its continuation when reconciling after a restart
Fei2411838bfb7759d4
diff --git a/ds-bot/src/host/resume.js b/ds-bot/src/host/resume.jsindex e507ddac..80c153ba 100644--- a/ds-bot/src/host/resume.js+++ b/ds-bot/src/host/resume.js@@ -13,7 +13,7 @@ import { randomUUID } from 'node:crypto'import { readFileSync } from 'node:fs'import { mkdir, writeFile } from 'node:fs/promises'import { join } from 'node:path'-import { HISTORY_TIMEOUT_MS, MESSAGE_INLINE_MAX, SOURCE_KIND } from './constants.js'+import { HISTORY_TIMEOUT_MS, MESSAGE_INLINE_MAX, SOURCE_KIND, STEER_CUT } from './constants.js'import { answerSender, failureLine, turnFailure } from './turns.js'import { textOf } from './text.js'@@ -56,11 +56,24 @@ export function turnsOf(events) {return turns}-// The last turn that took up an exchange: what it said and how it ended.+// The last turn that took up an exchange: what it said and how it ended. A turn cut+// by a user steer keeps going in the turn that starts right after it, so the reason+// that counts is the continuation's.export function turnOf(events, exchangeId) {const turns = turnsOf(events)+ const steerCut = turn => turn?.reason?.kind === 'aborted'+ && turn.reason.reason?.kind === 'hook'+ && turn.reason.reason?.reason === STEER_CUTfor (let at = turns.length - 1; at >= 0; at -= 1) {- if (turns[at].took.some(source => source.exchangeId === exchangeId)) return turns[at]+ if (!turns[at].took.some(source => source.exchangeId === exchangeId)) continue+ let found = turns[at]+ let next = at + 1+ while (steerCut(found) && next < turns.length) {+ const follow = turns[next]+ found = { ...follow, took: [...found.took, ...follow.took], texts: [...found.texts, ...follow.texts] }+ next += 1+ }+ return steerCut(found) ? { ...found, reason: undefined } : found}return undefined}diff --git a/ds-bot/test/steer.test.js b/ds-bot/test/steer.test.jsindex 11ae5f16..c59bf6de 100644--- a/ds-bot/test/steer.test.js+++ b/ds-bot/test/steer.test.js@@ -4,6 +4,7 @@ import { tmpdir } from 'node:os'import { join, resolve } from 'node:path'import { test } from 'node:test'import { createRuntime } from '../index.js'+import { turnOf } from '../src/host/resume.js'import { fakeContext } from './fake-ctx.js'import { SOURCE_KIND, STEER_CUT } from '../src/host/constants.js'@@ -468,3 +469,35 @@ test('steer cut: earlier steers and queued next-turn keep order', async () => {assert.deepEqual(agent.inbox.nextStep.map(m => m.content[0].text), ['one'])await stop()})++test('steer resume: turnOf follows a steer cut to its continuation', async () => {+ const turn = (n, events = []) => [+ { type: 'turn/start', data: { turn: n } },+ ...events,+ ]+ const said = text => ({ type: 'assistant/message', data: { message: { content: [{ type: 'text', text }] } } })+ const took = exchangeId => ({ type: 'user/message', data: { source: { kind: SOURCE_KIND, role: 'request', exchangeId }, content: [{ type: 'text', text: 'do' }] } })+ const ended = reason => ({ type: 'turn/end', data: { reason } })+ const cut = { kind: 'aborted', reason: { kind: 'hook', reason: STEER_CUT } }++ // A cut turn whose continuation answered: the continuation's reason and the texts merged.+ const follow = turnOf([+ ...turn(1, [took('x1'), said('a'), ended(cut)]),+ ...turn(2, [said('b'), ended({ kind: 'completed' })]),+ ], 'x1')+ assert.deepEqual(follow?.reason, { kind: 'completed' })+ assert.deepEqual(follow?.texts, ['a', 'b'])++ // A cut turn with no continuation yet: read as unfinished, not as stopped.+ const dangling = turnOf([+ ...turn(1, [took('x1'), said('a'), ended(cut)]),+ ], 'x1')+ assert.equal(dangling?.reason, undefined)+ assert.deepEqual(dangling?.texts, ['a'])++ // A user stop stays a stop.+ const stopped = turnOf([+ ...turn(1, [took('x1'), ended({ kind: 'aborted', reason: { kind: 'user' } })]),+ ], 'x1')+ assert.deepEqual(stopped?.reason, { kind: 'aborted', reason: { kind: 'user' } })+})