A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Preserve complete delegated results across restarts
klarkxy80893d46bfe4
diff --git a/docs/plans/2026-10-16-bot-handoffs.md b/docs/plans/2026-10-16-bot-handoffs.mdindex 97997e75..9182b7ca 100644--- a/docs/plans/2026-10-16-bot-handoffs.md+++ b/docs/plans/2026-10-16-bot-handoffs.md@@ -111,3 +111,4 @@ Bot 之间派活原来按数量拦,不看对方的状态:- 2026-10-16:定下(#262)。- 2026-10-16:Bot 的形象动画表示「在等别的 Bot」(#262)。+- 2026-10-23:交回的结果全文放在 `bot/handoff-results/` 的一份文件里,`state.json` 只存开头,所以重启不会把已经完成的结果截短(#479)。diff --git a/ds-bot/README.md b/ds-bot/README.mdindex 73997b83..60503710 100644--- a/ds-bot/README.md+++ b/ds-bot/README.md@@ -192,6 +192,7 @@ Files in `bot/`:|---|---|| `state.json` | The team: Bots, groups, Bot-to-Bot messages, preferences and open questions. A long message keeps only its first 2,000 characters here; all of it is in the Session it went to. || `group-lines/` | Long group lines, up to the 20,000 characters a member reads, one file per line; each is removed when its line leaves the group's latest 60 lines or the group is deleted |+| `handoff-results/` | The whole result of a piece of work a Bot handed on, when it is longer than the 2,000 characters `state.json` keeps, one file per result; each is removed once the answer it went into has gone up || `state.lock` | The dsh that is using this data (pid and host), its mtime bumped as a heartbeat. It is removed when that dsh exits; a leftover whose heartbeat has stopped is taken over by the next dsh to load. || `chat-index.sqlite` | Full-text index of older chat parts (`read_own_chat`); rebuilt if deleted || `usage.sqlite` | Token counts for the Usage page; rebuilt from the Session logs if deleted, losing only the calls outside the logs and the names of deleted Bots |diff --git a/ds-bot/src/host/handoffs.js b/ds-bot/src/host/handoffs.jsindex 6ef820f9..2cd5d199 100644--- a/ds-bot/src/host/handoffs.js+++ b/ds-bot/src/host/handoffs.js@@ -5,6 +5,9 @@// that came back, together with what came back. Results find their way up by// themselves, so no Bot needs to message one that waits for it, and that is the one// hand-off refused. The open works are kept in state.json so a restart waits on.+import { createHash } from 'node:crypto'+import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'+import { join } from 'node:path'import { TEXT_KEPT } from './constants.js'import { headOf } from './text.js'@@ -21,6 +24,72 @@ export function install(rt) {const save = () => void rt.save().catch(() => {})const nameOf = id => rt.botOf(id)?.name+ // The whole result of a work, in a file of its own: state.json is written whole on+ // every save, so it keeps only the start of a long result, and a restart would cut+ // that result short before it reached the Bot that asked for it.+ const resultsDir = join(rt.home, 'handoff-results')+ const resultPath = name => join(resultsDir, name)+ // A work id is a UUID, but a saved team could carry any name: it stays inside the+ // folder, and one that is not plain falls back to a hash of the id.+ const resultName = id => {+ const text = String(id)+ return `${/^[A-Za-z0-9_-]{1,80}$/.test(text) ? text : createHash('sha256').update(text).digest('hex')}.txt`+ }+ const keptName = file => (typeof file === 'string' && /^[A-Za-z0-9_-]+\.txt$/.test(file) ? file : undefined)+ const writes = () => rt.readOnly?.() === undefined++ // The result of a work, whole, in a file of its own. The file is written before the+ // team file names it, so a result that is claimed to exist is there.+ function keepResult(id, text) {+ if (text.length <= TEXT_KEPT || !writes()) return undefined+ const name = resultName(id)+ const path = resultPath(name)+ const temp = `${path}.${process.pid}.tmp`+ try {+ mkdirSync(resultsDir, { recursive: true })+ writeFileSync(temp, text, 'utf8')+ renameSync(temp, path)+ return name+ } catch (error) {+ rt.warn('ds-bot: result of work %s not kept whole: %s', id, error)+ try {+ rmSync(temp, { force: true })+ } catch {}+ return undefined+ }+ }++ // The copy of a result that is not needed any more: the answer it went into has it.+ const dropResult = file => {+ const name = keptName(file)+ if (name === undefined || !writes()) return+ try {+ // save() can resolve without writing during shutdown. Check the durable+ // references too, so an old state can still recover its result after restart.+ const saved = JSON.parse(readFileSync(join(rt.home, 'state.json'), 'utf8'))+ if (Object.values(saved.works ?? {}).some(work => (work.results ?? []).some(result => result.file === name))) return+ rmSync(resultPath(name), { force: true })+ } catch {}+ }++ // What came back for a work: the whole result if it is still to be found, else the+ // start of it. A result that a restart cut, or whose file is gone, is reported as+ // such and not passed on as if it were whole.+ function resultText(result) {+ if (whole.has(result.id)) return { text: whole.get(result.id), whole: true }+ if (result.length === undefined) return { text: result.text ?? '', whole: true }+ const name = keptName(result.file)+ if (name !== undefined) {+ try {+ const text = readFileSync(resultPath(name), 'utf8')+ if (text.length === result.length) return { text, whole: true }+ } catch {+ // The file is gone or unreadable: the start of the result is all there is.+ }+ }+ return { text: result.text ?? '', whole: false }+ }+// The work a turn does: the request it answers, or the work a result that came back// (or a turn picked up again) goes on with.const workOf = info => (info === undefined ? undefined@@ -44,20 +113,30 @@ export function install(rt) {for (const child of Object.values(works())) if (child.parent === id) delete child.parentconst above = works()[work.parent]if (above !== undefined) {- above.results.push({ id, name, text: headOf(text, TEXT_KEPT), ...(text.length > TEXT_KEPT ? { length: text.length } : {}) })+ // The whole result goes into a file of its own before the team file names it.+ const file = keepResult(id, text)+ above.results.push({ id, name, text: headOf(text, TEXT_KEPT), ...(text.length > TEXT_KEPT ? { length: text.length } : {}), ...(file ? { file } : {}) })whole.set(id, text)}- for (const result of work.results) whole.delete(result.id)- save()+ for (const result of work.results ?? []) whole.delete(result.id)+ // Old state can still name these files until the replacement state lands.+ // A failed save must leave them available to the next host.+ void rt.save().then(() => {+ for (const result of work.results ?? []) dropResult(result.file)+ }).catch(() => {})return above === undefined ? undefined : work.parent}const dropWork = (id) => {- if (works()[id] === undefined) return- const { parent } = works()[id]+ const work = works()[id]+ if (work === undefined) return+ const { parent } = workdelete works()[id]const above = works()[parent]if (above !== undefined) above.open = above.open.filter(other => other !== id)- save()+ for (const result of work.results ?? []) whole.delete(result.id)+ void rt.save().then(() => {+ for (const result of work.results ?? []) dropResult(result.file)+ }).catch(() => {})}function taken(workId, id) {@@ -85,8 +164,8 @@ export function install(rt) {const work = works()[workId]const said = reply !== '' ? reply : work?.held ?? ''const results = (work?.results ?? []).map(result => {- const text = whole.get(result.id) ?? result.text- const cut = whole.has(result.id) || result.length === undefined ? '' : `\n[cut at ${TEXT_KEPT} characters by a restart]`+ const { text, whole } = resultText(result)+ const cut = whole ? '' : `\n[cut; ${result.length} characters in all, the rest was not kept]`return `${result.name}:\n${text}${cut}`})if (results.length === 0) return said || `${botName} finished without a reply.`diff --git a/ds-bot/test/handoff-results.test.js b/ds-bot/test/handoff-results.test.jsnew file mode 100644index 00000000..5d838dc2--- /dev/null+++ b/ds-bot/test/handoff-results.test.js@@ -0,0 +1,373 @@+// The result of work a Bot handed on, when it is longer than state.json keeps: the+// whole result has to survive a restart, so the Bot that asked for it gets all of it.+import assert from 'node:assert/strict'+import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import { setTimeout as delay } from 'node:timers/promises'+import { test } from 'node:test'+import { apply } from '../index.js'+import { fakeContext } from './fake-ctx.js'+import { SOURCE_KIND } from '../src/host/constants.js'+import { install as installHandoffs } from '../src/host/handoffs.js'++const seed = () => ({+ version: 1,+ mainBotIds: ['a'],+ bots: {+ a: { id: 'a', name: 'Chief', role: 'Boss', sessionId: 'a', createdAt: 1 },+ b: { id: 'b', name: 'Writer', role: 'Write', sessionId: 'b', createdAt: 2 },+ c: { id: 'c', name: 'Tester', role: 'Test', sessionId: 'c', createdAt: 3 },+ d: { id: 'd', name: 'Editor', role: 'Edit', sessionId: 'd', createdAt: 4 },+ },+ rooms: {},+ agents: {},+ exchanges: [],+ exchangeTurns: {},+ questions: {},+ prefs: {},+ revision: 0,+})++// A host over the plugin's own folder. Starting the same folder again is a restart:+// the first host is closed before the second one starts.+async function startHost(home, team) {+ if (team !== undefined) writeFileSync(join(home, 'state.json'), JSON.stringify(team))+ const ctx = fakeContext()+ const agents = new Map()+ const agentOf = (sessionId) => {+ if (!agents.has(sessionId)) {+ const calls = []+ agents.set(sessionId, {+ session: { id: sessionId, requestHeader: () => undefined },+ id: sessionId,+ calls,+ status: 'idle',+ followup(message) { calls.push(['followup', message]) },+ steer(message) { calls.push(['steer', message]) },+ inject(message) { calls.push(['inject', message]) },+ cancel(cause) { calls.push(['cancel', cause]) },+ async whenIdle() {},+ async runMaintenance(fn) { return fn(AbortSignal.timeout(60_000)) },+ inbox: {+ nextStep: [],+ nextTurn: [],+ remove(id) {+ for (const list of [this.nextStep, this.nextTurn]) {+ const at = list.findIndex(message => message.id === id)+ if (at !== -1) return list.splice(at, 1).length === 1+ }+ return false+ },+ },+ })+ }+ return agents.get(sessionId)+ }+ Object.assign(ctx, {+ llm: { listProviders: () => [], listModels: async () => [], resolveModelInfo: async () => ({}) },+ workspaceRegistry: {+ get: () => undefined,+ resolveByPath: async () => undefined,+ create: async path => ({ id: 'w1', path }),+ archiveSession: async () => {},+ },+ sessionController: {+ create: async () => ({ sessionId: 's1' }),+ resolveAgent: async sessionId => ({ agent: agentOf(sessionId) }),+ selectModel: async () => {},+ page: async () => ({ records: [], hasMore: false }),+ },+ sessionTitle: { rename: async () => {} },+ sessions: { flush: async () => {} },+ })+ let route+ const register = ctx.connection.fetch.register+ ctx.connection.fetch.register = (options) => {+ route = options+ return register(options)+ }+ apply(ctx, { home })+ let seq = 0+ const emit = (id, type, data) => {+ for (const listener of ctx.listeners.get('session/event') ?? []) {+ if (listener.length >= 2) listener({ id }, { type, seq: ++seq, data })+ }+ }+ const tool = (name, args, sessionId) => ctx.registeredTools.get(name).execute(args, { agent: { id: sessionId } })+ const call = async (endpoint, payload) => (await route.fetch(new Request('http://localhost/api/bot', {+ method: 'POST', body: JSON.stringify({ endpoint, payload }),+ }))).json()+ // Loading once fills the Session-owner indexes turns.js filters events by.+ await tool('list_bots', {}, 'a')+ return { ctx, agents, agentOf, emit, tool, call, home, stop: () => ctx.dispose() }+}++// The plugin folder of a test, and the hosts over it. A second host over the same+// folder is a restart. Everything is closed and removed when the test ends.+function bench(t) {+ const home = mkdtempSync(join(tmpdir(), 'ds-bot-handoff-results-'))+ const hosts = []+ t.after(async () => {+ for (const host of hosts.reverse()) await host.stop()+ rmSync(home, { recursive: true, force: true, maxRetries: 5 })+ })+ return { home, start: async team => { const host = await startHost(home, team); hosts.push(host); return host } }+}++const textBlock = content => [{ type: 'text', text: content }]+const assistantText = text => ({ message: { content: textBlock(text) } })+const textOf = content => content.map(block => block.text ?? '').join('\n')+const callsOf = (agents, id) => agents.get(id)?.calls ?? []+const settled = async (check) => {+ for (let count = 0; count < 200 && !check(); count += 1) await delay(5)+}++// Each Session takes up the newest message it was sent as its next turn.+function turnTaker(agents, emit) {+ const turns = new Map()+ const open = (id, message) => {+ turns.set(id, (turns.get(id) ?? 0) + 1)+ emit(id, 'turn/start', { turn: turns.get(id) })+ emit(id, 'user/message', { content: message.content, source: message.source })+ return message+ }+ const take = id => open(id, agents.get(id).calls.at(-1)[1])+ const end = (id, text) => {+ if (text !== undefined) emit(id, 'assistant/message', assistantText(text))+ emit(id, 'turn/end', { turn: turns.get(id), reason: { kind: 'completed' } })+ }+ const userTurn = (id, text) => {+ turns.set(id, (turns.get(id) ?? 0) + 1)+ emit(id, 'turn/start', { turn: turns.get(id) })+ emit(id, 'user/message', { content: textBlock(text), source: { kind: 'user' } })+ }+ return { take, open, end, userTurn }+}++// The works the team file holds, once the saves made so far are done.+const worksIn = async ({ call, home }) => {+ await call('set-prefs', {})+ return JSON.parse(readFileSync(join(home, 'state.json'), 'utf8')).works ?? {}+}+const keptIn = home => (existsSync(join(home, 'handoff-results')) ? readdirSync(join(home, 'handoff-results')) : [])++// Chief asks Writer, which hands part of it on to Tester, which answers.+async function handedOn({ start }, review) {+ const first = await start(seed())+ const { agents, emit, tool } = first+ const turns = turnTaker(agents, emit)+ turns.userTurn('a', 'have Writer do it')+ await tool('message_bot', { to: 'Writer', message: 'carry out the plan' }, 'a')+ turns.end('a', 'asked Writer')+ turns.take('b')+ await tool('message_bot', { to: 'Tester', message: 'review my work' }, 'b')+ turns.end('b', 'handed it on')+ turns.take('c')+ turns.end('c', review)+ await settled(() => callsOf(agents, 'b').length === 2)+ return { first, agents, result: callsOf(agents, 'b')[1][1] }+}++const LONG = '审查意见。'.repeat(1000)++test('a result longer than 2,000 characters survives a restart before the Bot that asked for it answers', async (t) => {+ const { home, start } = bench(t)+ const { first, result } = await handedOn({ start }, LONG)+ // The whole result is in a file of its own, and the team file names it.+ const works = await worksIn(first)+ const [work] = Object.values(works)+ assert.equal(work.results.length, 1)+ assert.equal(work.results[0].text.length, 2000)+ assert.equal(work.results[0].length, LONG.length)+ assert.deepEqual(keptIn(home), [`${work.results[0].id}.txt`])+ assert.equal(readFileSync(join(home, 'handoff-results', `${work.results[0].id}.txt`), 'utf8'), LONG)++ // The host restarts before the Writer answers.+ await first.stop()+ const second = await start()+ const again = turnTaker(second.agents, second.emit)+ again.open('b', result)+ again.end('b', 'fixed both')+ await settled(() => callsOf(second.agents, 'a').length === 1)+ const answer = textOf(callsOf(second.agents, 'a')[0][1].content)+ assert.ok(answer.includes(LONG), 'the whole result goes up')+ assert.ok(!answer.includes('the rest was not kept'))+})++test('a short result stays in the team file, and no file of its own is made', async (t) => {+ const { home, start } = bench(t)+ const { first } = await handedOn({ start }, 'two things to fix')+ const works = await worksIn(first)+ const [work] = Object.values(works)+ assert.equal(work.results[0].text, 'two things to fix')+ assert.equal(work.results[0].length, undefined)+ assert.deepEqual(keptIn(home), [])+})++test('a result inside a result survives a restart too', async (t) => {+ const { home, start } = bench(t)+ const first = await start(seed())+ const { agents, emit, tool } = first+ const turns = turnTaker(agents, emit)+ turns.userTurn('a', 'have Writer do it')+ await tool('message_bot', { to: 'Writer', message: 'carry out the plan' }, 'a')+ turns.end('a', 'asked Writer')+ turns.take('b')+ await tool('message_bot', { to: 'Tester', message: 'review my work' }, 'b')+ turns.end('b', 'handed it on')+ turns.take('c')+ await tool('message_bot', { to: 'Editor', message: 'proofread it' }, 'c')+ turns.end('c', 'handed it on')+ turns.take('d')+ turns.end('d', LONG)+ await settled(() => callsOf(agents, 'c').length === 2)+ const fromEditor = callsOf(agents, 'c')[1][1]+ const works = await worksIn(first)+ const [testerId, tester] = Object.entries(works).find(([, work]) => work.to === 'c')+ assert.equal(tester.results.length, 1)+ assert.equal(tester.results[0].length, LONG.length)+ assert.deepEqual(keptIn(home), [`${tester.results[0].id}.txt`])++ // The host restarts before the Tester answers.+ await first.stop()+ const second = await start()+ const again = turnTaker(second.agents, second.emit)+ again.open('c', fromEditor)+ again.end('c', 'the review is done')+ await settled(() => callsOf(second.agents, 'b').length === 1)+ const toWriter = callsOf(second.agents, 'b')[0][1]+ assert.ok(textOf(toWriter.content).includes(LONG), 'the result inside the result goes up')+ // The old copy goes only after the replacement state has been saved.+ await settled(() => keptIn(home).length === 1 && keptIn(home)[0] === `${testerId}.txt`)+ assert.deepEqual(keptIn(home), [`${testerId}.txt`])++ // The Writer picks that up and answers the Chief.+ const last = turnTaker(second.agents, second.emit)+ last.open('b', toWriter)+ last.end('b', 'all done')+ await settled(() => callsOf(second.agents, 'a').length === 1)+ const toChief = textOf(callsOf(second.agents, 'a')[0][1].content)+ assert.ok(toChief.includes(LONG), 'the result inside the result is still there')+ assert.ok(toChief.includes('the review is done'))+ await settled(() => keptIn(home).length === 0)+ assert.deepEqual(keptIn(home), [])+})++test('a result whose file is gone is reported, not passed on as if it were whole', async (t) => {+ const { home, start } = bench(t)+ const { first, result } = await handedOn({ start }, LONG)+ rmSync(join(home, 'handoff-results'), { recursive: true, force: true })+ await first.stop()+ const second = await start()+ const again = turnTaker(second.agents, second.emit)+ again.open('b', result)+ again.end('b', 'fixed both')+ await settled(() => callsOf(second.agents, 'a').length === 1)+ const answer = textOf(callsOf(second.agents, 'a')[0][1].content)+ assert.ok(answer.includes(LONG.slice(0, 2000)))+ assert.ok(!answer.includes(LONG))+ assert.match(answer, /\[cut; 5000 characters in all, the rest was not kept\]/)+})++test('a result file that was cut short is reported as well', async (t) => {+ const { home, start } = bench(t)+ const { first, result } = await handedOn({ start }, LONG)+ const works = await worksIn(first)+ const [work] = Object.values(works)+ writeFileSync(join(home, 'handoff-results', `${work.results[0].id}.txt`), LONG.slice(0, 100), 'utf8')+ await first.stop()+ const second = await start()+ const again = turnTaker(second.agents, second.emit)+ again.open('b', result)+ again.end('b', 'fixed both')+ await settled(() => callsOf(second.agents, 'a').length === 1)+ const answer = textOf(callsOf(second.agents, 'a')[0][1].content)+ assert.ok(!answer.includes(LONG))+ assert.match(answer, /\[cut; 5000 characters in all, the rest was not kept\]/)+})++test('a result that could not be written is not claimed, and is reported after a restart', async (t) => {+ const { home, start } = bench(t)+ // A file where the folder of the results should be: the write cannot succeed.+ writeFileSync(join(home, 'handoff-results'), 'not a folder', 'utf8')+ const { first, result } = await handedOn({ start }, LONG)+ const works = await worksIn(first)+ const [work] = Object.values(works)+ assert.equal(work.results[0].length, LONG.length)+ assert.equal(work.results[0].file, undefined)+ await first.stop()+ const second = await start()+ const again = turnTaker(second.agents, second.emit)+ again.open('b', result)+ again.end('b', 'fixed both')+ await settled(() => callsOf(second.agents, 'a').length === 1)+ const answer = textOf(callsOf(second.agents, 'a')[0][1].content)+ assert.ok(!answer.includes(LONG))+ assert.match(answer, /\[cut; 5000 characters in all, the rest was not kept\]/)+})++test('closing or dropping work retains result files until its state save succeeds', async (t) => {+ const { home } = bench(t)+ const rt = { home, state: { works: {} }, botOf: () => undefined, readOnly: () => undefined, warn() {}, save: async () => {} }+ installHandoffs(rt)+ rt.openWork('parent', { from: 'a', to: 'b' })+ rt.openWork('child', { from: 'b', to: 'c', parent: 'parent' })+ rt.closeWork('child', 'Tester', LONG)+ await delay(0)+ const saved = JSON.parse(JSON.stringify(rt.state))+ writeFileSync(join(home, 'state.json'), JSON.stringify(saved))+ assert.deepEqual(keptIn(home), ['child.txt'])+ let rejectSave+ rt.save = () => new Promise((resolve, reject) => { rejectSave = reject })+ rt.closeWork('parent', 'Writer', LONG)+ assert.deepEqual(keptIn(home), ['child.txt'])+ rejectSave(new Error('disk full'))+ await delay(0)+ assert.deepEqual(keptIn(home), ['child.txt'])+ const restarted = { ...rt, state: saved, save: async () => {} }+ installHandoffs(restarted)+ assert.ok(restarted.answerText('parent', 'done', 'Writer').includes(LONG))+ let finishSave+ restarted.save = () => new Promise(resolve => { finishSave = resolve })+ restarted.dropWork('parent')+ assert.deepEqual(keptIn(home), ['child.txt'])+ writeFileSync(join(home, 'state.json'), JSON.stringify(restarted.state))+ finishSave()+ await delay(0)+ assert.deepEqual(keptIn(home), [])+})++test('a save that resolves without writing during shutdown keeps the durable result', async (t) => {+ const { home } = bench(t)+ const rt = { home, state: { works: {} }, botOf: () => undefined, readOnly: () => undefined, warn() {}, save: async () => {} }+ installHandoffs(rt)+ rt.openWork('parent', { from: 'a', to: 'b' })+ rt.openWork('child', { from: 'b', to: 'c', parent: 'parent' })+ rt.closeWork('child', 'Tester', LONG)+ await delay(0)+ const saved = JSON.parse(JSON.stringify(rt.state))+ writeFileSync(join(home, 'state.json'), JSON.stringify(saved))+ rt.closeWork('parent', 'Writer', LONG)+ await delay(0)+ assert.deepEqual(keptIn(home), ['child.txt'])+ const restarted = { ...rt, state: saved }+ installHandoffs(restarted)+ assert.ok(restarted.answerText('parent', 'done', 'Writer').includes(LONG))+})++test('a team saved before the result was kept whole still answers, and says what is missing', async (t) => {+ const { start } = bench(t)+ const old = seed()+ old.works = { w1: { from: 'a', to: 'b', open: [], results: [{ id: 'w2', name: 'Tester', text: 'x'.repeat(2000), length: 5000 }], at: Date.now() } }+ const first = await start(old)+ const turns = turnTaker(first.agents, first.emit)+ turns.open('b', {+ content: textBlock('[Reply from Tester]\nthe review'),+ source: { kind: SOURCE_KIND, role: 'reply', exchangeId: 'x9', senderSessionId: 'c', senderName: 'Tester', work: 'w1', replyTo: 'w2' },+ })+ turns.end('b', 'fixed both')+ await settled(() => callsOf(first.agents, 'a').length === 1)+ const answer = textOf(callsOf(first.agents, 'a')[0][1].content)+ assert.match(answer, /Tester:\nx{2000}\n\[cut; 5000 characters in all, the rest was not kept\]/)+})