A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Ask a Bot to make the tool call it wrote as text
Fei2411a3929820f237
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6448f3f3..d9f80950 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -29,6 +29,7 @@ All notable changes to DS Bot are listed here. The format follows [Keep a Change- Bots receive the current time on their first model step and refresh it after ten minutes, even when their DSH preset does not supply a clock.- In a group chat, a conversation referenced in a message reached none of the members, and a reference whose title started with a member's name called that member. Now the members who answer the message get the referenced conversation.- In a group chat, typing `@` and a Bot's name and pressing Enter inserted a conversation reference instead of the Bot's name.+- When a model wrote its tool call as text instead of making it, nothing ran and the Bot stopped as if it were done. The Bot is now asked once to make the call, and the text of the call is kept out of group chats.- Deleting a Bot or group chat also removes its scheduled tasks, including ones left on the Bot's earlier chat parts. Before, armed tasks stayed active and kept running the deleted Session; the archive that would have stopped them was refused and the failure was silent.- A long conversation's fresh part keeps the Bot's model and thinking level. Before, the new part fell back to the default model.- A provider adapter that cannot measure tokens no longer fails every turn. The context size is estimated instead, with one warning in the log.diff --git a/ds-bot/client.js b/ds-bot/client.jsindex de539bd1..21e804fe 100644--- a/ds-bot/client.js+++ b/ds-bot/client.js@@ -472,6 +472,7 @@ var ZH = {"{name} was cut off by a restart and picked its work back up": "{name} 的工作被重启打断,已经接着做","{name} was cut off by a restart again and stopped": "{name} 的工作又被重启打断,已停下","Picked up its work after a restart": "重启后接着做之前的工作",+ "Asked to make the tool call it wrote as text": "工具调用被写成了文字,已让它重新调用","{name} is waiting for your approval": "{name} 等你批准","{first} and {second} are waiting for your approval": "{first}、{second} 等你批准","{first}, {second} and {count} more are waiting for your approval": "{first}、{second} 等 {total} 个等你批准",@@ -4054,6 +4055,7 @@ function TriggerCell({ node, sessionId, useRoster, useSessions, useTurnClock, usif (source.role === "kickoff" || source.role === "room") return null;if (source.role === "secret" || deferred) return null;if (source.role === "resume") return (0, import_react6.createElement)("div", { className: "bt-event" }, t("Picked up its work after a restart"));+ if (source.role === "retry") return (0, import_react6.createElement)("div", { className: "bt-event" }, t("Asked to make the tool call it wrote as text"));if (source.role === "post") {if (source.failed !== void 0 || source.resumed === true) return null;const room2 = roster.roomsById[sessionId];diff --git a/ds-bot/dev/mock-llm.mjs b/ds-bot/dev/mock-llm.mjsindex 537c0773..3f8024a2 100644--- a/ds-bot/dev/mock-llm.mjs+++ b/ds-bot/dev/mock-llm.mjs@@ -29,6 +29,7 @@// 把X拉进群 · 把X踢出群 · 群名改成X · 讨论 (admin @all) · 私聊聊了啥 (repeats the briefing)// 查私聊 关键词 (read_own_chat) · 长文 N · 上下文里有 词 · 撑爆 · 报错 [名] (that member's call, or every member's, fails with 401)// 提权 (members other than the admin run a shell command that asks for full access)+// 写成文字 (members write their tool call as text instead of making it; also in a Bot's own chat)// A handoff turn answers "第 N 段接着做完了". Handoff notes and backend compaction each// return a full eight-section note marked "handoff 提示词" or "backend 提示词" (a Bot's// Session should only see the former; retries add "重试", merges add "合并了上一份笔记").@@ -78,6 +79,7 @@ const tool = (name, input) => ({})const think = value => ({ type: 'thinking', thinking: value })const ESCALATION = '提权测试:用户在群里要求'+const WRITTEN_CALL = '我先看一下目录。\n<tool_call>\n<function=pwsh>\n<parameter=command>\nGet-ChildItem\n</parameter>\n</function>\n</tool_call>'// Deterministic filler that grows a conversation by about `chars` characters.const filler = (chars, tag) => Array.from({ length: Math.ceil(chars / 20) }, (_, index) => `${tag}第${index + 1}段:这是一段用来撑长上下文的测试文字。`).join('').slice(0, chars)const isResults = message => Array.isArray(message?.content) && message.content.some(block => block.type === 'tool_result')@@ -291,6 +293,9 @@ function decide(body) {return [text(`群里回完了,一共 ${replies} 条回复。`)]}if (userText.startsWith('[Group chat') && userText.includes('The user just created this group')) return [text('群建好了,这个群要一起做点什么?')]+ if (/^\[Group chat "[^"]*" · your tool call did not run\]/.test(userText) || userText.startsWith('[Your last reply wrote a tool call as text')) {+ return [text('这次真的调用了,目录看完了。')]+ }if (userText.startsWith('[Group chat')) {// Reads only this round's transcript block. The opener is the user's last line, or// a Bot's post that started the round. Count-off says the next number after the@@ -327,6 +332,7 @@ function decide(body) {const brief = /Your own chat with the user so far[^\n]*\n([\s\S]*?)\nThis brief does not change/.exec(all)?.[1] ?? ''return [text(brief ? `简报最后一句:${brief.split('\n').at(-1)}` : '没有简报。')]}+ if (/写成文字/.test(userLine) && !leads) return [text(WRITTEN_CALL)]const shell = ['pwsh', 'bash'].find(name => tools.has(name))if (/提权/.test(userLine) && !leads && shell) {return [text('这一步要写工作区外的文件,我申请一次提权。'), tool(shell, { command: 'echo ok', description: '在工作区外写一个测试文件', justification: ESCALATION, sandbox_permissions: 'danger-full-access' })]@@ -371,6 +377,7 @@ function decide(body) {if ((order = /^回忆(?:\s+(.+))?$/.exec(userText)) && tools.has('recall')) return [tool('recall', order[1] ? { query: order[1].trim() } : {})]if ((order = /^搜索\s*[::]\s*([^\n]+)/.exec(userText)) && tools.has('web_search')) return [tool('web_search', { queries: order[1].trim().split(/\s*[||]\s*/) })]if (/^看(?:网页|页面)/.test(userText) && tools.has('read_browser')) return [tool('read_browser', {})]+ if (userText.startsWith('写成文字')) return [text(WRITTEN_CALL)]// Context-line tests: grow the context, run a long multi-step turn, search history.if ((order = /^(?:长文|long)\s*(\d+)/.exec(userText))) return [text(filler(Number(order[1]) * 1000, '长文'))]if ((order = /^长任务\s*(\d+)(?:\s+(\d+))?/.exec(userText))) return [text(filler(Number(order[2] ?? 4) * 1000, '步骤0')), tool('list_bots', {})]diff --git a/ds-bot/src/client/cells.js b/ds-bot/src/client/cells.jsindex 97332164..5764be1a 100644--- a/ds-bot/src/client/cells.js+++ b/ds-bot/src/client/cells.js@@ -325,6 +325,7 @@ export function TriggerCell({ node, sessionId, useRoster, useSessions, useTurnCl// The secret card itself shows what the user did; the note to the Bot stays out.if (source.role === 'secret' || deferred) return nullif (source.role === 'resume') return h('div', { className: 'bt-event' }, t('Picked up its work after a restart'))+ if (source.role === 'retry') return h('div', { className: 'bt-event' }, t('Asked to make the tool call it wrote as text'))if (source.role === 'post') {// A member's late failure, or its work picked up after a restart, shows as an// event of the round it opens.diff --git a/ds-bot/src/client/i18n.js b/ds-bot/src/client/i18n.jsindex 2a8a7b96..92ee86c8 100644--- a/ds-bot/src/client/i18n.js+++ b/ds-bot/src/client/i18n.js@@ -416,6 +416,7 @@ const ZH = {'{name} was cut off by a restart and picked its work back up': '{name} 的工作被重启打断,已经接着做','{name} was cut off by a restart again and stopped': '{name} 的工作又被重启打断,已停下','Picked up its work after a restart': '重启后接着做之前的工作',+ 'Asked to make the tool call it wrote as text': '工具调用被写成了文字,已让它重新调用','{name} is waiting for your approval': '{name} 等你批准','{first} and {second} are waiting for your approval': '{first}、{second} 等你批准','{first}, {second} and {count} more are waiting for your approval': '{first}、{second} 等 {total} 个等你批准',diff --git a/ds-bot/src/host/delivery.js b/ds-bot/src/host/delivery.jsindex de5b24a1..8471b10e 100644--- a/ds-bot/src/host/delivery.js+++ b/ds-bot/src/host/delivery.js@@ -12,8 +12,9 @@ export function install(rt) {return resolved.agent}- const headerFor = (role, fromName, roomName, audience, note = 'new message', deferred = false, { resumed = false } = {}) => {+ const headerFor = (role, fromName, roomName, audience, note = 'new message', deferred = false, { resumed = false, retried = false } = {}) => {if (deferred) return `[Message from ${fromName} · you read it during your last task; answer it now]`+ if (retried && role !== 'room') return '[Your last reply wrote a tool call as text, so nothing ran]'if (resumed && role === 'resume') return '[DeepSeek Harness restarted while you were working]'if (resumed && role !== 'room') return `[Message from ${fromName} · DeepSeek Harness restarted while you were working on it]`if (role === 'reply') return `[Reply from ${fromName}]`@@ -31,10 +32,11 @@ export function install(rt) {// `sessionId` defaults to the recipient's own chat; group turns name its group Session.// `deferred`: a request the Bot already read mid-turn, handed back to answer in a- // turn of its own; its exchange was recorded when it first arrived. `resumed` carries- // on a turn that a restart cut off, and `resent` is a message a shutdown cancelled- // (resume.js); their exchange is recorded already too.- async function deliver({ fromId, toId, sessionId, text, role, hop = 0, replyTo, roomId, audience, note, exchangeId = randomUUID(), steer = false, deferred = false, resumed = false, resent = false }) {+ // turn of its own; its exchange was recorded when it first arrived. `resumed` and+ // `retried` carry on a turn that a restart cut off or that wrote its tool call as+ // text (resume.js, turns.js), and `resent` is a message a shutdown cancelled; their+ // exchange is recorded already too.+ async function deliver({ fromId, toId, sessionId, text, role, hop = 0, replyTo, roomId, audience, note, exchangeId = randomUUID(), steer = false, deferred = false, resumed = false, retried = false, resent = false }) {const from = rt.botOf(fromId)const to = rt.botOf(toId)if (to === undefined) throw new Error(`Unknown Bot ${toId}`)@@ -54,8 +56,9 @@ export function install(rt) {...(audience === 'user' ? { audience } : {}),...(deferred ? { deferred: true } : {}),...(resumed ? { resumed: true } : {}),+ ...(retried ? { retried: true } : {}),}- if (EXCHANGE_ROLES.has(role) && !deferred && !resumed && !resent) {+ if (EXCHANGE_ROLES.has(role) && !deferred && !resumed && !retried && !resent) {state.exchanges.push({ id: exchangeId, from: fromId, to: toId, text, role, time: Date.now(), ...(replyTo ? { replyTo } : {}), ...(audience === 'user' ? { audience } : {}) })void rt.save().catch(() => {})}@@ -63,7 +66,7 @@ export function install(rt) {// A steered message cuts into a running turn at its next step; a follow-up// queues for the next turn (see turns.js for how each is routed).const message = createUserMessage({- content: [{ type: 'text', text: `${headerFor(role, fromName, room?.name, audience, note, deferred, { resumed })}\n${text}` }],+ content: [{ type: 'text', text: `${headerFor(role, fromName, room?.name, audience, note, deferred, { resumed, retried })}\n${text}` }],source,})if (steer) agent.steer(message)diff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.jsindex 33a6394e..72e816a5 100644--- a/ds-bot/src/host/turns.js+++ b/ds-bot/src/host/turns.js@@ -31,9 +31,39 @@ export function attachReference(queued, source, text) {post.references = [...(post.references ?? []), { labels, text }]}+// A tool call some models write into their text instead of making it (the XML-like+// form of their own chat template).+const WRITTEN_CALL = /<tool_call>\s*<function=|<function=[\w.-]+>\s*<parameter=/+export const writesToolCall = text => typeof text === 'string' && WRITTEN_CALL.test(text)+export const stripWrittenCalls = text => text+ .replace(/<tool_call>[\s\S]*?(?:<\/tool_call>|$)/g, '')+ .replace(/<function=[\w.-]+>[\s\S]*?(?:<\/function>|$)/g, '')+ .trim()++const RETRY_TEXT = 'Your last reply wrote a tool call as plain text (<tool_call>…), so it did not run and nothing happened. Call the tool through the tool interface now, then reply as you meant to.'+export function install(rt) {const { ctx, state } = rt+ // The retry goes where the turn's trigger came from: a group round's waiter or a+ // Bot's request keeps its exchange; anything else is a nudge in the Bot's own chat.+ const retryWrittenCall = (id, info) => {+ const keeps = info !== undefined && (info.role === 'room' || expectsReply(info))+ return rt.deliver({+ ...(keeps && info.senderSessionId ? { fromId: info.senderSessionId } : {}),+ toId: rt.selfOf(id),+ sessionId: id,+ role: keeps ? info.role : 'retry',+ ...(keeps && info.roomId ? { roomId: info.roomId } : {}),+ hop: keeps ? info.hop ?? 0 : 0,+ ...(keeps && info.exchangeId ? { exchangeId: info.exchangeId } : {}),+ ...(keeps && info.audience ? { audience: info.audience } : {}),+ note: 'your tool call did not run',+ retried: true,+ text: RETRY_TEXT,+ })+ }+/** @type {Map<string, {turn?:number, pending?:any, 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 */@@ -297,8 +327,11 @@ export function install(rt) {break}case 'assistant/message': {- const text = textOf(event.data?.message?.content)+ const content = event.data?.message?.content+ const text = textOf(content)if (text !== '') entry.texts.push(text)+ entry.lastText = text+ entry.lastCalled = Array.isArray(content) && content.some(block => block?.type === 'tool-call')break}case 'turn/end': {@@ -306,6 +339,7 @@ export function install(rt) {const info = entry.infoconst joined = entry.joined ?? []const texts = entry.texts+ const { lastText, lastCalled } = entry// A turn stopped for a switch goes on in the next part, which answers instead.const switched = entry.switched === trueentry.turn = undefined@@ -316,10 +350,23 @@ export function install(rt) {entry.since = undefinedentry.tool = undefinedentry.calls = undefined+ entry.lastText = undefined+ entry.lastCalled = undefinedconst reason = event.data?.reasonconst failure = reason?.kind === 'error' && !switched ? turnFailure(reason.error) : undefinedif (failure !== undefined) noteTurnFailure(id, failure)if (switched) break+ // A model that writes its tool call as text ends the turn as if it were done,+ // though nothing ran. It gets one more turn on the same trigger, so a waiting+ // round or sender gets the real answer; a second time, the text goes on without+ // the call.+ const wroteCall = failure === undefined && reason?.kind === 'completed' && lastCalled !== true && writesToolCall(lastText)+ const retry = wroteCall && info?.retried !== true && !(info?.role === 'room' && stoppedRoomTurns.has(info.exchangeId))+ if (retry) void retryWrittenCall(id, info).catch(error => rt.warn('ds-bot: asking %s to run its written tool call failed: %s', id, error))+ const replyText = () => {+ const reply = (wroteCall ? texts.map(stripWrittenCalls) : texts).join('\n\n').trim()+ return wroteCall && reply === '' ? 'Tried to run a tool but wrote the call as text, so it did not run.' : reply+ }// Whoever the turn answers gets the same three-way rule: a failure notice,// the turn's text (or a stopped-before-answer line), or nothing.const answer = (source, reply) => {@@ -348,20 +395,21 @@ export function install(rt) {}).catch(error => rt.warn('ds-bot: reply delivery failed: %s', error))}}- if (info?.role === 'room') {+ // A retried turn answers the same waiter or sender, so this one passes nothing on.+ if (!retry && info?.role === 'room') {const waiter = roomWaiters.get(info.exchangeId)roomWaiters.delete(info.exchangeId)const stopped = stoppedRoomTurns.delete(info.exchangeId)if (waiter !== undefined) {if (failure !== undefined) waiter.fail(failure)- else waiter.resolve(reason?.kind === 'aborted' ? null : texts.join('\n\n').trim())+ else waiter.resolve(reason?.kind === 'aborted' ? null : replyText())} else if (!stopped && reason?.kind !== 'aborted') {// The round moved on without this member; how its turn ends still reaches the group.if (failure !== undefined) void rt.lateRoomFailure?.(id, info, failure)- else void rt.lateRoomReply?.(id, info, texts.join('\n\n').trim())+ else void rt.lateRoomReply?.(id, info, replyText())}- } else if (info !== undefined) {- answer(info, texts.join('\n\n').trim())+ } else if (!retry && info !== undefined) {+ answer(info, replyText())}for (const request of joined) {// A failed or stopped turn closes the request like any other; a finisheddiff --git a/ds-bot/test/follow-through.test.js b/ds-bot/test/follow-through.test.jsindex 37191173..d2c712d7 100644--- a/ds-bot/test/follow-through.test.js+++ b/ds-bot/test/follow-through.test.js@@ -5,6 +5,7 @@ import { join } from 'node:path'import { test } from 'node:test'import { apply } from '../index.js'import { fakeContext } from './fake-ctx.js'+import { stripWrittenCalls, writesToolCall } from '../src/host/turns.js'// Chief in its own chat 'a'; Writer, the admin of group 'r1', speaks there from 'gb'.const seed = () => ({@@ -212,3 +213,50 @@ test('a message a shutdown cancelled is sent again once; one still waiting, take// The resend is the same exchange, not a new one.assert.equal((await second.call('exchange', { a: 'a', b: 'b' })).value.length, 4)})++const WRITTEN = 'Checking now.\n<tool_call>\n<function=pwsh>\n<parameter=command>\nGet-ChildItem E:\\\n</parameter>\n</function>\n</tool_call>'++test('a tool call written as text gets one more turn on the same round slot', async (t) => {+ const home = freshHome(t)+ const host = await start(home)+ t.after(host.stop)+ const result = await openRoomTurn(t, host)+ host.emit('gb', 'assistant/message', { message: { content: [{ type: 'text', text: WRITTEN }] } })+ host.emit('gb', 'turn/end', { turn: 1, reason: { kind: 'completed' } })+ await settle(() => host.agentOf('gb').calls.length === 2)+ const retry = host.agentOf('gb').calls[1]+ assert.deepEqual([retry.source.role, retry.source.retried, retry.source.exchangeId], ['room', true, host.agentOf('gb').calls[0].source.exchangeId])+ assert.match(text(retry), /^\[Group chat "Ops" · your tool call did not run\]/)+ assert.equal(result(), undefined)++ // Written again: the text goes on without the call.+ run(host, 'gb', 2, retry)+ host.emit('gb', 'assistant/message', { message: { content: [{ type: 'text', text: WRITTEN }] } })+ host.emit('gb', 'turn/end', { turn: 2, reason: { kind: 'completed' } })+ await settle(() => result() !== undefined)+ assert.deepEqual(result().replies.map(reply => reply.text), ['Checking now.'])+ assert.equal(host.agentOf('gb').calls.length, 2)+})++test('in its own chat, a Bot that wrote a tool call as text is asked to make it', async (t) => {+ const home = freshHome(t)+ const host = await start(home)+ t.after(host.stop)+ run(host, 'b', 1, { content: [{ type: 'text', text: 'move the docs' }], source: { kind: 'user' } })+ // A call made for real, with or without text around it, is no retry.+ host.emit('b', 'assistant/message', { message: { content: [{ type: 'text', text: 'On it.' }, { type: 'tool-call', id: 'k1', name: 'pwsh', arguments: '{}' }] } })+ host.emit('b', 'assistant/message', { message: { content: [{ type: 'text', text: WRITTEN }] } })+ host.emit('b', 'turn/end', { turn: 1, reason: { kind: 'completed' } })+ await settle(() => host.agentOf('b').calls.length === 1)+ const retry = host.agentOf('b').calls[0]+ assert.deepEqual([retry.source.role, retry.source.retried], ['retry', true])+ assert.match(text(retry), /^\[Your last reply wrote a tool call as text, so nothing ran\]/)+})++test('a written tool call is told apart from text about one', () => {+ assert.equal(writesToolCall(WRITTEN), true)+ assert.equal(writesToolCall('<function=read>\n<parameter=path>a.md</parameter></function>'), true)+ assert.equal(writesToolCall('Models such as Step write `<tool_call>` tags in their template.'), false)+ assert.equal(writesToolCall(undefined), false)+ assert.equal(stripWrittenCalls(WRITTEN), 'Checking now.')+})