A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Keep one bad request from locking up a Bot chat
lulu feizhu0cedadd96bcb
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 83de4c1d..da18b063 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,10 @@ All notable changes to DS Bot are listed here. The format follows [Keep a Change- Every Bot can change its own name, role, color, model and instructions; only a Main Bot changes other Bots.+### Fixed++- #73: Offload images from completed turns and move on when a model request keeps failing.+## [0.1.2] - 2026-10-11### Addeddiff --git a/ds-bot/dev/mock-llm.mjs b/ds-bot/dev/mock-llm.mjsindex 63454b7d..31f51133 100644--- a/ds-bot/dev/mock-llm.mjs+++ b/ds-bot/dev/mock-llm.mjs@@ -11,7 +11,7 @@// Environment: MOCK_PORT, MOCK_LOG, MOCK_DELAY_MS, MOCK_JITTER_MS (random extra latency),// MOCK_LOG_CHARS (prompt length kept per log entry), MOCK_SUMMARY_DELAY_MS (handoff notes// and backend compaction only), MOCK_NATIVE_SEARCH=off (ignore web_search_* tools and-// answer like an endpoint without search).+// answer like an endpoint without search), MOCK_REJECT_IMAGES=1 (reject requests with images).//// Commands to a Bot:// 建群 名: A, B · 群发[回报] 名: 消息 · 改群 名 公告|管理员|模式|加|踢: 值 · 删群 名@@ -55,6 +55,7 @@ const SUMMARY_DELAY = Number(process.env.MOCK_SUMMARY_DELAY_MS ?? DELAY)// How much of each call's last message the log keeps; raise it to read whole prompts.const LOG_CHARS = Number(process.env.MOCK_LOG_CHARS ?? 160)const WATCH = process.env.MOCK_WATCH ?? ''+const REJECT_IMAGES = process.env.MOCK_REJECT_IMAGES === '1'// MOCK_LOG_TOOLS=1 records the request's tool names, to compare two Sessions' tool views.const LOG_TOOLS = process.env.MOCK_LOG_TOOLS === '1'let seq = 0@@ -477,7 +478,7 @@ function searchApi(req, res) {req.on('data', chunk => { raw += chunk })req.on('end', () => {const body = JSON.parse(raw || '{}')- appendFileSync(LOG, JSON.stringify({ t: new Date().toISOString(), id: ++seq, searchApi: { path: req.url, body, auth: req.headers.authorization ? 'bearer' : '' } }) + '\n')+ appendFileSync(LOG, JSON.stringify({ t: new Date().toISOString(), id: ++seq, images: 0, searchApi: { path: req.url, body, auth: req.headers.authorization ? 'bearer' : '' } }) + '\n')res.writeHead(200, { 'content-type': 'application/json' })res.end(JSON.stringify({ results: [{ title: `Search API result: ${body.query}`, url: `https://example.com/search-api/${encodeURIComponent(body.query ?? '')}`, content: `Page text about ${body.query}.` }] }))})@@ -494,23 +495,31 @@ const server = createServer((req, res) => {req.on('data', chunk => { raw += chunk })req.on('end', () => {const body = JSON.parse(raw)+ const images = (body.messages ?? []).reduce((count, message) => count+ + (Array.isArray(message.content) ? message.content.filter(block => block?.type === 'image').length : 0), 0)+ const id = ++seq+ if (REJECT_IMAGES && images > 0) {+ appendFileSync(LOG, JSON.stringify({ t: new Date().toISOString(), id, images, rejectedImages: true, messages: body.messages?.length ?? 0, last: textOf(body.messages?.at(-1)?.content).slice(0, LOG_CHARS) }) + '\n')+ res.writeHead(400, { 'content-type': 'application/json' })+ res.end(JSON.stringify({ type: 'error', error: { type: 'invalid_request_error', message: 'mock: request with images rejected' } }))+ return+ }const search = asksNativeSearch(body)const out = search ? nativeSearch(body) : decide(body)- const id = ++seqif (out === 'overflow') {- appendFileSync(LOG, JSON.stringify({ t: new Date().toISOString(), id, overflow: true, messages: body.messages?.length ?? 0, last: textOf(body.messages?.at(-1)?.content).slice(0, LOG_CHARS) }) + '\n')+ appendFileSync(LOG, JSON.stringify({ t: new Date().toISOString(), id, images, overflow: true, messages: body.messages?.length ?? 0, last: textOf(body.messages?.at(-1)?.content).slice(0, LOG_CHARS) }) + '\n')res.writeHead(400, { 'content-type': 'application/json' })res.end(JSON.stringify({ type: 'error', error: { type: 'invalid_request_error', message: 'prompt is too long: maximum context length exceeded' } }))return}if (out === 'auth') {- appendFileSync(LOG, JSON.stringify({ t: new Date().toISOString(), id, auth: true, messages: body.messages?.length ?? 0 }) + '\n')+ appendFileSync(LOG, JSON.stringify({ t: new Date().toISOString(), id, images, auth: true, messages: body.messages?.length ?? 0 }) + '\n')res.writeHead(401, { 'content-type': 'application/json' })res.end(JSON.stringify({ type: 'error', error: { type: 'authentication_error', message: 'mock: invalid x-api-key sk-mock1234567890' } }))return}if (out === 'fail') {- appendFileSync(LOG, JSON.stringify({ t: new Date().toISOString(), id, fail: true, messages: body.messages?.length ?? 0 }) + '\n')+ appendFileSync(LOG, JSON.stringify({ t: new Date().toISOString(), id, images, fail: true, messages: body.messages?.length ?? 0 }) + '\n')res.writeHead(400, { 'content-type': 'application/json' })res.end(JSON.stringify({ type: 'error', error: { type: 'invalid_request_error', message: 'mock: this note request fails on purpose' } }))return@@ -523,7 +532,7 @@ const server = createServer((req, res) => {// Hashes of the system prompt and tools show whether two Sessions share a cacheable prefix.const digest = value => createHash('sha256').update(JSON.stringify(value ?? null)).digest('hex').slice(0, 12)const watch = WATCH ? { watchHit: JSON.stringify([body.system, body.tools, body.messages]).includes(WATCH) } : {}- appendFileSync(LOG, JSON.stringify({ t: new Date().toISOString(), id, tokens, ...watch, ...LOG_TOOLS ? { toolNames: (body.tools ?? []).map(entry => entry.name ?? entry.type) } : {}, ...search ? { nativeSearch: { model: body.model, tools: body.tools, auth: [req.headers['x-api-key'] ? 'x-api-key' : '', req.headers.authorization ? 'bearer' : ''].filter(Boolean) } } : {}, system: digest(body.system), tools: digest(body.tools), messages: body.messages?.length ?? 0, first: textOf(body.messages?.[0]?.content).slice(0, 60), last: textOf(body.messages?.at(-1)?.content).slice(0, LOG_CHARS), out: out.map(block => (block.type === 'text' && block.text.length > 200 ? { ...block, text: `${block.text.slice(0, 200)}…(${block.text.length})` } : block)) }) + '\n')+ appendFileSync(LOG, JSON.stringify({ t: new Date().toISOString(), id, tokens, images, ...watch, ...LOG_TOOLS ? { toolNames: (body.tools ?? []).map(entry => entry.name ?? entry.type) } : {}, ...search ? { nativeSearch: { model: body.model, tools: body.tools, auth: [req.headers['x-api-key'] ? 'x-api-key' : '', req.headers.authorization ? 'bearer' : ''].filter(Boolean) } } : {}, system: digest(body.system), tools: digest(body.tools), messages: body.messages?.length ?? 0, first: textOf(body.messages?.[0]?.content).slice(0, 60), last: textOf(body.messages?.at(-1)?.content).slice(0, LOG_CHARS), out: out.map(block => (block.type === 'text' && block.text.length > 200 ? { ...block, text: `${block.text.slice(0, 200)}…(${block.text.length})` } : block)) }) + '\n')if (search) {setTimeout(() => {res.writeHead(200, { 'content-type': 'application/json' })diff --git a/ds-bot/src/host/images.js b/ds-bot/src/host/images.jsindex 7b04e3a8..c522db0a 100644--- a/ds-bot/src/host/images.js+++ b/ds-bot/src/host/images.js@@ -8,6 +8,25 @@ import { BlockAssembler } from '@deepseek-ai/dsh-llm'import { OCR_CACHE_MAX, OCR_PROMPT, OCR_RETRY_MS, OCR_TIMEOUT_MS } from './constants.js'import { splitRef } from './text.js'+export function offloadTargets(session, beforeSeq) {+ const targets = []+ for (const seq of session.surface.nodes) {+ if (seq >= beforeSeq) continue+ const event = session.eventAt(seq)+ if (event?.type !== 'user/message' && event?.type !== 'tool/result') continue+ const message = session.deriveEventMessage(event)+ const imageIndexes = []+ let imageIndex = 0+ for (const block of message?.content ?? []) {+ if (block?.type !== 'image') continue+ if (block.offloaded !== true) imageIndexes.push(imageIndex)+ imageIndex += 1+ }+ if (imageIndexes.length > 0) targets.push({ seq, imageIndexes })+ }+ return targets+}+export function install(rt) {const { ctx, config, home } = rt@@ -170,5 +189,23 @@ export function install(rt) {})()})+ ctx.on('agent/status', ({ agent, status }) => {+ if (status !== 'idle' || !agent?.session || !rt.isBotSession(agent.id) || rt.readOnly() !== undefined) return+ const beforeSeq = agent.session.seq+ setImmediate(() => {+ void (async () => {+ try {+ if (!ctx.sessions.messageProjections.some(projection => projection.type === 'image/offload')) return+ const targets = offloadTargets(agent.session, beforeSeq)+ if (targets.length === 0) return+ agent.session.append('image/offload', { targets })+ await ctx.sessions.flush(agent.session)+ } catch (error) {+ rt.warn('ds-bot: old images for %s not offloaded: %s', agent.id, error)+ }+ })()+ })+ })+Object.assign(rt, { imagesForGroup })}diff --git a/ds-bot/src/host/parts.js b/ds-bot/src/host/parts.jsindex 182ced6f..4666acbf 100644--- a/ds-bot/src/host/parts.js+++ b/ds-bot/src/host/parts.js@@ -136,6 +136,7 @@ export function install(rt) {const NOTE_COOLDOWN_MS = [60_000, 300_000, 900_000]const SWITCH_ATTEMPTS = 5const OVERFLOW = 'CONTEXT_WINDOW_EXCEEDED'+ const STUCK_CODES = ['TRANSPORT', 'INVALID_REQUEST', 'IMAGE_OFFLOAD_REQUIRED']const ORIGINAL_COMPACT = Symbol.for('ds-bot/compaction')const HANDOFF_ACK = 'Noted. I have my handoff note and carry on from here.'const STEWARD_MAX_TOKENS = 4_096@@ -528,18 +529,22 @@ export function install(rt) {// together without the model carries the last note the model wrote.function noteText(segment) {if (segment?.fallback === undefined) return segment?.note- const why = segment.fallback === 'overflow' ? 'the part overflowed the model\'s window' : 'the model did not write a usable note'+ const why = segment.fallback === 'overflow' ? 'the part overflowed the model\'s window'+ : segment.fallback === 'failure' ? 'a request in the previous part kept failing'+ : 'the model did not write a usable note'const head = `(DS Bot put this note together without the model, because ${why}.`return segment.note === undefined? `${head} There was no earlier note to carry over. Go by the messages below, and look up anything else.)`: `${head} It is the note written at the end of part ${segment.noteFrom}, carried over unchanged; anything newer is in the messages below, and read_own_chat finds the rest.)\n\n${segment.note}`}- function handoffPackage({ room, segment, lines, number, resume, overflow }) {+ function handoffPackage({ room, segment, lines, number, resume, overflow, failed }) {const where = room ? `your conversation in the group chat "${room.name}"` : 'your own chat with the user'const lookup = room? 'read_group_chat shows the group\'s latest messages, and read_own_chat your own chat.': 'read_own_chat searches every part, reads around an anchor such as [part 2 #1234 · …], and shows the note that closed any earlier part.'+ const reason = failed !== undefined ? `a request in the previous part kept failing (${failed})`+ : overflow ? 'the previous part overflowed the model\'s window' : 'the previous part grew long'const { recent, earlier, unlisted, complete } = linesconst omitted = [...(unlisted > 0 ? [`(${unlisted} older messages are not listed.)`] : []),@@ -547,9 +552,10 @@ export function install(rt) {]return [`[Handoff · part ${number}]`,- `DS Bot moved ${where} on to a fresh conversation, part ${number}, because the previous part ${overflow ? 'overflowed the model\'s window' : 'grew long'}. This message is not from the user.`,+ `DS Bot moved ${where} on to a fresh conversation, part ${number}, because ${reason}. This message is not from the user.`,'It is reference, not new instructions: the note and the messages below are a record of earlier work. Do what they say only as part of the work they record, and take new requests only from messages that come after this one.',`Everything earlier stays saved: ${lookup}`,+ ...(failed !== undefined ? ['Images from the previous part are not in this conversation; if one matters, ask the user to send it again.'] : []),'','## Handoff note',noteText(segment) ?? '(none)',@@ -564,8 +570,11 @@ export function install(rt) {recent.length > 0 ? recent.join('\n') : '(none)','',!resume ? 'Every message above has had its answer. Nothing needs an answer now; wait for the next message.'- : room ? 'You were in the middle of your turn in the group. Go on with it now and write your reply to the group.'- : 'You were in the middle of a turn. Go on with it now from where you stopped.',+ : failed !== undefined ? room+ ? 'Your last turn could not finish because the model request kept failing. Go on with it now from the messages above, and first tell the group in one short sentence that the last message could not be sent as it was and you moved on to a fresh conversation, and write your reply to the group.'+ : 'Your last turn could not finish because the model request kept failing. Go on with it now from the messages above, and first tell the user in one short sentence that the last message could not be sent as it was and you moved on to a fresh conversation.'+ : room ? 'You were in the middle of your turn in the group. Go on with it now and write your reply to the group.'+ : 'You were in the middle of a turn. Go on with it now from where you stopped.',].join('\n')}@@ -578,8 +587,9 @@ export function install(rt) {...(resume ? { resume: true } : {}),})- /** @type {Map<string, {botId:string, roomId?:string, carry:any[], info?:any, resume:boolean, overflow:boolean, hard:boolean, started?:boolean, moved?:boolean, noteFailed?:boolean}>} */+ /** @type {Map<string, {botId:string, roomId?:string, carry:any[], info?:any, resume:boolean, overflow:boolean, failed?:string, hard:boolean, started?:boolean, moved?:boolean, noteFailed?:boolean}>} */const switches = new Map()+ const rescued = new Set()// Parts that have not started a turn yet, with their handoff package's size.const freshParts = new Map()// Context size where a part started, or where a checkpoint or a failed switch left@@ -620,10 +630,10 @@ export function install(rt) {// `hard`: past the hard line or the window, the part moves on even without a note// from the model.- function startSwitch(agent, { resume = false, carry = [], info, overflow = false, hard = overflow } = {}) {+ function startSwitch(agent, { resume = false, carry = [], info, overflow = false, failed, hard = overflow } = {}) {const place = placeOf(agent.id)if (place === undefined || switches.has(agent.id) || readOnly()) return- const job = { ...place, carry: [...carry], info, resume, overflow, hard }+ const job = { ...place, carry: [...carry], info, resume, overflow, failed, hard }switches.set(agent.id, job)void runSwitch(agent, job).catch(error => rt.warn('ds-bot: %s did not move on to a fresh part: %s', rt.botOf(job.botId)?.name ?? agent.id, error))@@ -678,7 +688,7 @@ export function install(rt) {const number = parts.lengthlet writtenlet failure- if (!job.overflow) {+ if (!job.overflow && job.failed === undefined) {try {written = await writeNote(agent, AbortSignal.any([signal, AbortSignal.timeout(NOTE_TIMEOUT_MS)]))} catch (error) {@@ -699,7 +709,7 @@ export function install(rt) {if (failure !== undefined) rt.warn('ds-bot: handoff note for %s failed past the hard line; moving on with the earlier note: %s', bot.name, failure)const previous = parts.at(-2)const noted = written !== undefined ? { note: written.note }- : { fallback: job.overflow ? 'overflow' : 'failed', ...(previous?.note ? { note: previous.note, noteFrom: previous.noteFrom ?? previous.number } : {}) }+ : { fallback: job.failed !== undefined ? 'failure' : job.overflow ? 'overflow' : 'failed', ...(previous?.note ? { note: previous.note, noteFrom: previous.noteFrom ?? previous.number } : {}) }const budget = Math.min(RECENT_TOKENS, Math.floor((lines?.check ?? 4 * RECENT_TOKENS) / 4))const carried = await handoffLines(agent.id, { budget, group: room !== undefined, number })if (signal.aborted) throw signal.reason@@ -729,10 +739,11 @@ export function install(rt) {}}await rt.save()+ if (job.failed !== undefined) rescued.add(freshId)void rt.moveSchedules(agent.id, freshId)job.moved = trueconst resume = job.resume || job.overflow- const text = handoffPackage({ room, segment, lines: carried, number: number + 1, resume, overflow: job.overflow })+ const text = handoffPackage({ room, segment, lines: carried, number: number + 1, resume, overflow: job.overflow, failed: job.failed })freshParts.set(freshId, estimateTokens(text))const fresh = await rt.resolveAgent(freshId)fresh.followup(createUserMessage({ content: [{ type: 'text', text }], source: handoffSource(job, resume) }))@@ -785,6 +796,20 @@ export function install(rt) {return { kind: 'reject' }}, { prepend: true })+ ctx.on('agent/request-error', async (payload, next) => {+ const failure = payload.failure+ if (!STUCK_CODES.includes(failure?.code)) return next()+ const decided = await next()+ if (decided !== undefined || payload.signal?.aborted) return decided+ await rt.load()+ const { agent } = payload+ if (!rt.isBotSession(agent.id) || switches.has(agent.id) || readOnly() || rescued.has(agent.id)) return decided+ const entry = rt.liveOf(agent.id)+ entry.switched = true+ startSwitch(agent, { resume: true, info: entry.info, overflow: true, failed: failure.code })+ return undefined+ }, { prepend: true })+ctx.on('agent/request-error', async (payload, next) => {if (payload.failure?.code !== OVERFLOW || payload.signal?.aborted) return next()await rt.load()@@ -819,8 +844,21 @@ export function install(rt) {}}+ function releaseRescued(agent) {+ if (!rescued.has(agent.id)) return+ const latest = agent.session.seq - 1+ const earliest = Math.max(0, latest - 199)+ for (let seq = latest; seq >= earliest; seq -= 1) {+ const event = agent.session.eventAt(seq)+ if (event?.type !== 'turn/end') continue+ if (event.data?.reason?.kind !== 'error') rescued.delete(agent.id)+ break+ }+ }+ctx.on('agent/status', ({ agent, status }) => {if (status === 'idle' && rt.isBotSession(agent.id)) {+ releaseRescued(agent)void checkContext(agent).catch(error => rt.warn('ds-bot: context check for %s failed: %s', agent.id, error))}})diff --git a/ds-bot/test/images.test.js b/ds-bot/test/images.test.jsnew file mode 100644index 00000000..253f5e15--- /dev/null+++ b/ds-bot/test/images.test.js@@ -0,0 +1,34 @@+import assert from 'node:assert/strict'+import { test } from 'node:test'+import { offloadTargets } from '../src/host/images.js'++test('offloadTargets selects live images from prior user and tool messages', () => {+ const events = [+ { type: 'user/message' },+ { type: 'assistant/message' },+ { type: 'tool/result' },+ { type: 'user/message' },+ { type: 'user/message' },+ ]+ const messages = new Map([+ [events[0], { content: [+ { type: 'image', attachment: { attachmentId: 'a' } },+ { type: 'image', offloaded: true, attachment: { attachmentId: 'b' } },+ { type: 'image', attachment: { attachmentId: 'c' } },+ ] }],+ [events[1], { content: [{ type: 'image', attachment: { attachmentId: 'ignored' } }] }],+ [events[2], { content: [{ type: 'image', attachment: { attachmentId: 'd' } }] }],+ [events[3], { content: [{ type: 'text', text: 'no image' }] }],+ [events[4], { content: [{ type: 'image', attachment: { attachmentId: 'new' } }] }],+ ])+ const session = {+ surface: { nodes: [0, 1, 2, 3, 4] },+ eventAt: seq => events[seq],+ deriveEventMessage: event => messages.get(event),+ }++ assert.deepEqual(offloadTargets(session, 4), [+ { seq: 0, imageIndexes: [0, 2] },+ { seq: 2, imageIndexes: [0] },+ ])+})diff --git a/ds-bot/test/install.test.js b/ds-bot/test/install.test.jsindex 528cd09e..aeafb258 100644--- a/ds-bot/test/install.test.js+++ b/ds-bot/test/install.test.js@@ -66,9 +66,11 @@ test('apply registers listeners, tools, and effects in a fixed order', (t) => {'on agent/created','on agent/pre-step (prepend)','on agent/request-error (prepend)',+ 'on agent/request-error (prepend)','on agent/status','on llm/stream','on llm/stream',+ 'on agent/status','tool request_secret','tool list_secrets','on tools/execute',diff --git a/ds-bot/test/memory.test.js b/ds-bot/test/memory.test.jsindex ece480d3..9be6d4d2 100644--- a/ds-bot/test/memory.test.js+++ b/ds-bot/test/memory.test.js@@ -878,7 +878,7 @@ test('a turn whose Session moves to a fresh part before its review is still revitest('config.memoryReview false leaves memory to the Bots\' own remember calls', async (t) => {const { ctx, call, section, requests, stop } = start({ config: { memoryReview: false, memoryReviewDelayMs: 0 } })t.after(() => stop())- assert.equal(ctx.calls.filter(entry => entry === 'on agent/status').length, 1)+ assert.equal(ctx.calls.filter(entry => entry === 'on agent/status').length, 2)await call('state', {})assert.match(section('b2'), /Call it in that same turn, before you reply/)ctx.emit('agent/status', { agent: { id: 'b2' }, status: 'idle' })