A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Pick up Bot work that a restart dropped
Fei2411904705d0ff37
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 06c34b11..6448f3f3 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,6 +10,7 @@ All notable changes to DS Bot are listed here. The format follows [Keep a Change- **Reference a chat** in the composer's **+** menu adds another Bot's chat, or a recent conversation outside the team, to your message as context.- A Main Bot can stop another Bot's running turn. The Bot is told why, and its reply goes back to the Main Bot.- In a group chat, a member that asks for an approval no longer holds up the round. You allow or reject the request in the group's details panel, and a line above the composer names the members who are waiting or at work. What a member says after the round moved on is posted in the group.+- A Bot that was working when DeepSeek Harness closed or restarted picks its work back up when DS Bot starts again. A group chat shows which member was cut off, and a Bot that was answering another Bot still sends its answer.### Changeddiff --git a/ds-bot/client.js b/ds-bot/client.jsindex 7baa35f9..de539bd1 100644--- a/ds-bot/client.js+++ b/ds-bot/client.js@@ -469,6 +469,9 @@ var ZH = {"{name} is typing…": "{name} 正在输入…","{name} didn't answer in time": "{name} 没有及时回应","{name} paused for your approval": "{name} 停下来等你批准",+ "{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": "重启后接着做之前的工作","{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} 个等你批准",@@ -4050,8 +4053,9 @@ function TriggerCell({ node, sessionId, useRoster, useSessions, useTurnClock, usif (source.handoff === true) return roster.roomsById[sessionId] ? null : (0, import_react6.createElement)(EarlierParts, { sessionId, roster, actions });if (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 === "post") {- if (source.failed !== void 0) return null;+ if (source.failed !== void 0 || source.resumed === true) return null;const room2 = roster.roomsById[sessionId];const poster = roster.byId[source.senderSessionId] ?? { id: source.senderSessionId, name: source.senderName ?? t("a Bot"), color: "gray" };return (0, import_react6.createElement)(GroupMessage, { bot: poster, text: stripHeader(contentText(data.content)), room: room2, roster, actions, msgKey: `${sessionId}:${data.turn}:post`, useUi });@@ -4301,6 +4305,12 @@ function GroupReplies({ sessionId, callId, done, content, roster, actions, useUiif (reply.kind === "missed") {return (0, import_react6.createElement)("div", { key: `${reply.botId}-${index}`, className: "bt-event" }, t("{name} didn't answer in time", { name: bot.name }));}+ if (reply.kind === "resumed") {+ return (0, import_react6.createElement)("div", { key: `${reply.botId}-${index}`, className: "bt-event" }, t("{name} was cut off by a restart and picked its work back up", { name: bot.name }));+ }+ if (reply.kind === "cut") {+ return (0, import_react6.createElement)("div", { key: `${reply.botId}-${index}`, className: "bt-event" }, t("{name} was cut off by a restart again and stopped", { name: bot.name }));+ }if (reply.kind === "approval") {return (0, import_react6.createElement)("div",diff --git a/ds-bot/index.js b/ds-bot/index.jsindex 22eeb515..327762d6 100644--- a/ds-bot/index.js+++ b/ds-bot/index.js@@ -17,6 +17,7 @@ import * as tools from './src/host/tools.js'import * as chatIndex from './src/host/chat-index.js'import * as groups from './src/host/groups.js'import * as approvals from './src/host/approvals.js'+import * as resume from './src/host/resume.js'import * as memory from './src/host/memory.js'import * as parts from './src/host/parts.js'import * as images from './src/host/images.js'@@ -38,7 +39,7 @@ export const inject = ['llm', 'tools', 'systemPrompt', 'sessionController', 'ses// the functions other modules call; they call them through `rt` at run time, never during// install. Listeners run and tools are listed in registration order, so this order holds:// usage meters model requests after every listener that rewrites them.-const MODULES = [diagnostics, store, models, permissions, turns, delivery, roster, agents, prompt, timeContext, tools, chatIndex, groups, approvals, memory, parts, images, secrets, connectors, schedules, browser, usage, update, dshUpdate, channel]+const MODULES = [diagnostics, store, models, permissions, turns, delivery, roster, agents, prompt, timeContext, tools, chatIndex, groups, approvals, resume, memory, parts, images, secrets, connectors, schedules, browser, usage, update, dshUpdate, channel]export function apply(ctx, config = {}) {const home = config.home ?? join(process.env.DSH_HOME ?? join(homedir(), '.dsh'), 'bot')diff --git a/ds-bot/src/client/cells.js b/ds-bot/src/client/cells.jsindex 95e362e4..97332164 100644--- a/ds-bot/src/client/cells.js+++ b/ds-bot/src/client/cells.js@@ -324,9 +324,11 @@ export function TriggerCell({ node, sessionId, useRoster, useSessions, useTurnClif (source.role === 'kickoff' || source.role === 'room') return null// The secret card itself shows what the user did; the note to the Bot stays out.if (source.role === 'secret' || deferred) return null+ if (source.role === 'resume') return h('div', { className: 'bt-event' }, t('Picked up its work after a restart'))if (source.role === 'post') {- // A member's late failure shows as an event of the round it opens.- if (source.failed !== undefined) return null+ // A member's late failure, or its work picked up after a restart, shows as an+ // event of the round it opens.+ if (source.failed !== undefined || source.resumed === true) return nullconst room = roster.roomsById[sessionId]const poster = roster.byId[source.senderSessionId] ?? { id: source.senderSessionId, name: source.senderName ?? t('a Bot'), color: 'gray' }return h(GroupMessage, { bot: poster, text: stripHeader(contentText(data.content)), room, roster, actions, msgKey: `${sessionId}:${data.turn}:post`, useUi })@@ -564,6 +566,12 @@ function GroupReplies({ sessionId, callId, done, content, roster, actions, useUiif (reply.kind === 'missed') {return h('div', { key: `${reply.botId}-${index}`, className: 'bt-event' }, t("{name} didn't answer in time", { name: bot.name }))}+ if (reply.kind === 'resumed') {+ return h('div', { key: `${reply.botId}-${index}`, className: 'bt-event' }, t('{name} was cut off by a restart and picked its work back up', { name: bot.name }))+ }+ if (reply.kind === 'cut') {+ return h('div', { key: `${reply.botId}-${index}`, className: 'bt-event' }, t('{name} was cut off by a restart again and stopped', { name: bot.name }))+ }if (reply.kind === 'approval') {return h('div', { key: `${reply.botId}-${index}`, className: 'bt-event' },h('button', { type: 'button', className: 'bt-event-whole', onClick: () => actions.showDetails(sessionId) },diff --git a/ds-bot/src/client/i18n.js b/ds-bot/src/client/i18n.jsindex a58e0a46..2a8a7b96 100644--- a/ds-bot/src/client/i18n.js+++ b/ds-bot/src/client/i18n.js@@ -413,6 +413,9 @@ const ZH = {'{name} is typing…': '{name} 正在输入…',"{name} didn't answer in time": '{name} 没有及时回应','{name} paused for your approval': '{name} 停下来等你批准',+ '{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': '重启后接着做之前的工作','{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/approvals.js b/ds-bot/src/host/approvals.jsindex d8ca98f8..9e7fa71a 100644--- a/ds-bot/src/host/approvals.js+++ b/ds-bot/src/host/approvals.js@@ -29,11 +29,17 @@ export function install(rt) {at: Date.now(),}pending.set(id, entry)- if (owner === undefined) return Promise.resolve(next()).finally(() => pending.delete(id))+ rt.noteApproval?.(sessionId, { tool: entry.tool, reason: entry.reason.slice(0, 200) })+ const settled = () => {+ pending.delete(id)+ rt.noteApproval?.(sessionId, undefined)+ }+ if (owner === undefined) return Promise.resolve(next()).finally(settled)rt.yieldRoomTurn?.(sessionId)return new Promise((resolve) => {entry.answer = (outcome) => {- if (!pending.delete(id)) return+ if (!pending.has(id)) return+ settled()resolve(outcome)}if (request.signal?.aborted) entry.answer('cancelled')diff --git a/ds-bot/src/host/delivery.js b/ds-bot/src/host/delivery.jsindex 50c656ec..de5b24a1 100644--- a/ds-bot/src/host/delivery.js+++ b/ds-bot/src/host/delivery.js@@ -12,8 +12,10 @@ export function install(rt) {return resolved.agent}- const headerFor = (role, fromName, roomName, audience, note = 'new message', deferred = false) => {+ const headerFor = (role, fromName, roomName, audience, note = 'new message', deferred = false, { resumed = false } = {}) => {if (deferred) return `[Message from ${fromName} · you read it during your last task; answer it now]`+ 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}]`if (role === 'stopped') return `[Stopped by ${fromName}]`if (role === 'room') return `[Group chat "${roomName}" · ${note}]`@@ -29,8 +31,10 @@ 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.- async function deliver({ fromId, toId, sessionId, text, role, hop = 0, replyTo, roomId, audience, note, exchangeId = randomUUID(), steer = false, deferred = false }) {+ // 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 }) {const from = rt.botOf(fromId)const to = rt.botOf(toId)if (to === undefined) throw new Error(`Unknown Bot ${toId}`)@@ -49,8 +53,9 @@ export function install(rt) {...(roomId ? { roomId } : {}),...(audience === 'user' ? { audience } : {}),...(deferred ? { deferred: true } : {}),+ ...(resumed ? { resumed: true } : {}),}- if (EXCHANGE_ROLES.has(role) && !deferred) {+ if (EXCHANGE_ROLES.has(role) && !deferred && !resumed && !resent) {state.exchanges.push({ id: exchangeId, from: fromId, to: toId, text, role, time: Date.now(), ...(replyTo ? { replyTo } : {}), ...(audience === 'user' ? { audience } : {}) })void rt.save().catch(() => {})}@@ -58,7 +63,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)}\n${text}` }],+ content: [{ type: 'text', text: `${headerFor(role, fromName, room?.name, audience, note, deferred, { resumed })}\n${text}` }],source,})if (steer) agent.steer(message)diff --git a/ds-bot/src/host/groups.js b/ds-bot/src/host/groups.jsindex 2bc942e7..3dc6ee50 100644--- a/ds-bot/src/host/groups.js+++ b/ds-bot/src/host/groups.js@@ -48,9 +48,12 @@ const unseenLines = (room, memberId) => {return room.log.filter(line => line.seq > since && line.botId !== memberId).slice(-ROOM_DELTA_MAX)}-const lineText = line => (line.kind === 'lookup'- ? `[${line.who} looked up its own chat${line.text ? ` for "${line.text}"` : ''}]`- : `${line.who}: ${line.text}`)+const lineText = (line) => {+ if (line.kind === 'lookup') return `[${line.who} looked up its own chat${line.text ? ` for "${line.text}"` : ''}]`+ if (line.kind === 'resumed') return `[${line.who}'s work here was cut off when DeepSeek Harness restarted; it is picking it back up and posts when done]`+ if (line.kind === 'cut') return `[${line.who}'s work here was cut off by a restart again, so it stopped; ask it to go on if it still matters]`+ return `${line.who}: ${line.text}`+}const transcript = lines => lines.map(lineText).join('\n')// Models often echo the transcript format ("Name: …") despite the instruction.@@ -350,8 +353,8 @@ export function install(rt) {const posts = queued.length > 0 ? queued : [{ text: '' }]const startSeq = room.seq + 1// The opening greeting is a cue for the admin, not a line of the group; a member's- // late failure is an event of the round.- for (const post of posts.filter(item => !item.kickoff && !item.failed)) {+ // late failure, or its work picked up after a restart, is an event of the round.+ for (const post of posts.filter(item => !item.kickoff && !item.failed && !item.resumed)) {// Image readings go into the log only; `@` names are picked from what the user typed.const text = post.images === undefined ? post.text : [post.text, ...await rt.imagesForGroup(post.images, room.id)].filter(Boolean).join('\n')appendRoom(room, post.botId@@ -360,7 +363,8 @@ export function install(rt) {}if (posts.some(post => !post.botId && !post.kickoff)) room.botRounds = 0await rt.save()- const opener = posts.at(-1)+ // An event post never picks the speakers over a message queued with it.+ const opener = posts.findLast(post => !post.failed && !post.resumed) ?? posts.at(-1)const references = posts.flatMap(post => post.references ?? [])const round = {roomId: room.id,@@ -382,6 +386,14 @@ export function install(rt) {round.failed.push(bot?.name ?? post.name)round.replies.push({ kind: 'failed', botId: post.botId, name: bot?.name ?? post.name, color: bot?.color, code, ...(status ? { status } : {}), text: message, time: Date.now() })}+ // The log line tells the admin and the other members on their next turn.+ for (const post of posts.filter(item => item.resumed)) {+ const bot = rt.botOf(post.botId)+ const name = bot?.name ?? post.name+ appendRoom(room, { who: name, botId: post.botId, kind: post.again ? 'cut' : 'resumed', text: '' })+ round.replies.push({ kind: post.again ? 'cut' : 'resumed', botId: post.botId, name, color: bot?.color, time: Date.now() })+ }+ if (posts.some(post => post.resumed)) await rt.save()rt.roomProgress.set(exec.callId, round)try {await runRound(room, round, exec.signal, exec.agent)diff --git a/ds-bot/src/host/resume.js b/ds-bot/src/host/resume.jsnew file mode 100644index 00000000..86a9e930--- /dev/null+++ b/ds-bot/src/host/resume.js@@ -0,0 +1,223 @@+// Resume: Bot work that DeepSeek Harness dropped when it closed or restarted. DSH closes+// a turn it never finished as interrupted when the Session next opens, and a clean+// shutdown also cancels the messages still waiting in a Session's inbox; nothing would+// ever run either again, and whoever waited for them was not told. DS Bot notes each+// running Bot turn and each message it delivers until a turn takes it up, in+// inflight.json; what is still noted at the next start is picked up once.+import { randomUUID } from 'node:crypto'+import { readFileSync } from 'node:fs'+import { mkdir, writeFile } from 'node:fs/promises'+import { join } from 'node:path'+import { SOURCE_KIND } from './constants.js'++// Long enough for DSH to finish opening its workspaces after a start.+const RESUME_DELAY_MS = 3000+// A delivery waiting longer than this was not lost to a restart.+const QUEUED_KEPT_MS = 24 * 60 * 60 * 1000+const QUEUED_MAX = 50++export const resumeText = record => [+ 'DeepSeek Harness restarted while you were working on this, so your last turn stopped before it finished.',+ record.approval ? `You were waiting for the user's approval to run ${record.approval.tool || 'a tool'}${record.approval.reason ? ` (${record.approval.reason})` : ''}; that request is gone, so ask again if you still need it.` : '',+ 'Check what is already done (files, commands, messages) before you repeat anything, then finish the task and give your reply.',+].filter(Boolean).join(' ')++// The trigger fields a resumed turn needs to answer the same sender.+const triggerOf = info => ({+ role: info.role,+ ...(info.exchangeId ? { exchangeId: info.exchangeId } : {}),+ ...(info.senderSessionId ? { senderSessionId: info.senderSessionId } : {}),+ ...(info.roomId ? { roomId: info.roomId } : {}),+ ...(typeof info.hop === 'number' ? { hop: info.hop } : {}),+ ...(info.audience ? { audience: info.audience } : {}),+})++const readNotes = (file) => {+ try {+ const parsed = JSON.parse(readFileSync(file, 'utf8'))+ const object = value => (value !== null && typeof value === 'object' && !Array.isArray(value) ? value : {})+ return { turns: object(parsed?.turns), queued: object(parsed?.queued) }+ } catch {+ return { turns: {}, queued: {} }+ }+}++export function install(rt) {+ const { ctx, home } = rt+ const file = join(home, 'inflight.json')++ // What the last run left; read before this run notes anything.+ let interrupted = readNotes(file)++ /** @type {Record<string, {botId:string, roomId?:string, since:number, trigger?:any, approval?:{tool:string, reason:string}, resumed?:boolean}>} */+ let turns = {}+ /** @type {Record<string, {at:number, args:any}[]>} deliveries no turn has taken up, by Session */+ let queued = {}+ let writing = Promise.resolve()+ let stopped = false+ const persist = () => {+ // Only the host that saves the team runs its work again.+ if (stopped || rt.readOnly() !== undefined) return+ const snapshot = JSON.stringify({ turns, queued })+ // Written in place: a rename over the file fails on Windows while anything reads+ // it, and a torn write only loses what one restart would pick up.+ writing = writing.then(async () => {+ await mkdir(home, { recursive: true })+ await writeFile(file, snapshot, 'utf8')+ }).catch(error => rt.warn('ds-bot: noting running turns failed: %s', error))+ }+ const update = (id, change) => {+ const next = change(turns[id])+ if (next === turns[id]) return+ turns = { ...turns }+ if (next === undefined) delete turns[id]+ else turns[id] = next+ persist()+ }+ const forgetQueued = (exchangeId) => {+ for (const [sessionId, list] of Object.entries(queued)) {+ const at = list.findIndex(item => item.args.exchangeId === exchangeId)+ if (at === -1) continue+ const rest = list.toSpliced(at, 1)+ queued = { ...queued }+ if (rest.length === 0) delete queued[sessionId]+ else queued[sessionId] = rest+ persist()+ return+ }+ }++ const deliver = rt.deliver+ rt.deliver = async (args) => {+ const noted = { ...args, exchangeId: args.exchangeId ?? randomUUID() }+ const sessionId = noted.sessionId ?? rt.chatOf(noted.toId)+ // Noted before it is sent: a turn may take it up before the send returns.+ queued = { ...queued, [sessionId]: [...(queued[sessionId] ?? []), { at: Date.now(), args: noted }].slice(-QUEUED_MAX) }+ persist()+ try {+ return await deliver(noted)+ } catch (error) {+ forgetQueued(noted.exchangeId)+ throw error+ }+ }++ // A message is done with once a turn takes it up, or once it leaves the inbox untaken+ // while DS Bot runs: the user stopped the Bot, or DS Bot moved or withdrew it.+ ctx.on('agent/inbox/discarded', ({ message } = {}) => {+ if (message?.source?.kind === SOURCE_KIND && message.source.exchangeId) forgetQueued(message.source.exchangeId)+ })+ // Exchanges a turn took up since this start, until the resends are done, so a resend+ // never repeats one.+ let taken = Object.keys(interrupted.queued).length > 0 ? new Set() : undefined+ ctx.on('session/event', (session, event) => {+ const id = session?.id+ if (id === undefined) return+ if (event.type === 'user/message' && event.data?.source?.kind === SOURCE_KIND && event.data.source.exchangeId) {+ taken?.add(event.data.source.exchangeId)+ forgetQueued(event.data.source.exchangeId)+ }+ if (!rt.isBotSession(id)) return+ switch (event.type) {+ case 'turn/start': {+ const owner = rt.groupOwners.get(id)+ const botId = owner?.botId ?? rt.chatOwners.get(id)+ update(id, () => ({ botId, ...(owner ? { roomId: owner.roomId } : {}), since: event.time ?? Date.now() }))+ break+ }+ case 'user/message': {+ if (event.data?.source?.kind !== SOURCE_KIND) break+ // turns.js, installed earlier, has already claimed the turn's trigger.+ const info = rt.live.get(id)?.info+ update(id, record => (record === undefined || record.trigger !== undefined || info === undefined ? record+ : { ...record, trigger: triggerOf(info), ...(info.resumed ? { resumed: true } : {}) }))+ break+ }+ case 'turn/end': {+ // DSH stops every turn as it shuts down; that one is picked up at the next start.+ const reason = event.data?.reason+ if (reason?.kind === 'aborted' && reason.reason?.kind === 'disposed') break+ update(id, () => undefined)+ break+ }+ }+ })++ // An approval the restart drops is named when the turn is picked up.+ rt.noteApproval = (sessionId, approval) => update(sessionId, record => (record === undefined ? record+ : approval === undefined ? (({ approval: _, ...rest }) => rest)(record) : { ...record, approval }))++ async function resumeTurn(sessionId, record) {+ const bot = rt.botOf(record.botId)+ if (bot === undefined) return+ const text = resumeText(record)+ if (record.roomId !== undefined) {+ const room = rt.roomOf(record.roomId)+ if (room === undefined || !room.members.includes(bot.id)) return+ await rt.postToRoom(room, bot, '', { resumed: true, quiet: true, ...(record.resumed ? { again: true } : {}) })+ // A turn already picked up once and cut off again stays stopped: a restart loop+ // must not run it forever.+ if (record.resumed) return+ await rt.deliver({ toId: bot.id, sessionId: room.sessions?.[bot.id] ?? sessionId, role: 'room', roomId: room.id, note: 'picking up after a restart', resumed: true, text })+ return+ }+ if (record.resumed) return+ const trigger = record.trigger+ if ((trigger?.role === 'request' || trigger?.role === 'stopped') && trigger.senderSessionId && trigger.audience !== 'user' && rt.botOf(trigger.senderSessionId)) {+ // The sender still waits for this turn's answer, so it goes on under the same exchange.+ await rt.deliver({ fromId: trigger.senderSessionId, toId: bot.id, role: trigger.role, hop: trigger.hop ?? 0, ...(trigger.exchangeId ? { exchangeId: trigger.exchangeId } : {}), resumed: true, text })+ return+ }+ await rt.deliver({ toId: bot.id, role: 'resume', resumed: true, text })+ }++ // A hard stop keeps the inbox, which the Session runs once it wakes; only what DSH+ // cancelled is sent again.+ async function resendQueued(sessionId, list) {+ // A Session that moved on to a new part since is reached at its current one.+ const current = (args) => {+ if (rt.isBotSession(sessionId)) return sessionId+ return args.roomId === undefined ? rt.chatOf(args.toId) : rt.roomOf(args.roomId)?.sessions?.[args.toId]+ }+ const fresh = list.filter(item => Date.now() - item.at < QUEUED_KEPT_MS && rt.botOf(item.args?.toId) !== undefined+ && current(item.args) !== undefined && taken?.has(item.args.exchangeId) !== true)+ if (fresh.length === 0) return+ const target = current(fresh[0].args)+ const agent = await rt.resolveAgent(target)+ const waiting = new Set([...(agent.inbox?.nextStep ?? []), ...(agent.inbox?.nextTurn ?? [])].map(message => message.source?.exchangeId))+ for (const { args } of fresh) {+ if (waiting.has(args.exchangeId) || taken?.has(args.exchangeId) === true) continue+ await rt.deliver({ ...args, sessionId: current(args), steer: false, resent: true })+ }+ }++ async function resumeAll() {+ await rt.load()+ if (rt.readOnly() !== undefined) return+ const { turns: cut, queued: dropped } = interrupted+ interrupted = { turns: {}, queued: {} }+ // Taken up now, so another restart does not pick them up twice.+ persist()+ // Inboxes are read before anything wakes their Sessions.+ for (const [sessionId, list] of Object.entries(dropped)) {+ if (!Array.isArray(list)) continue+ await resendQueued(sessionId, list).catch(error => rt.warn('ds-bot: sending again what %s was sent failed: %s', sessionId, error))+ }+ taken = undefined+ for (const [sessionId, record] of Object.entries(cut)) {+ await resumeTurn(sessionId, record).catch(error => rt.warn('ds-bot: picking up the turn of %s failed: %s', sessionId, error))+ }+ }++ ctx.effect(() => {+ const pending = Object.keys(interrupted.turns).length > 0 || Object.keys(interrupted.queued).length > 0+ const timer = pending ? setTimeout(() => void resumeAll().catch(error => rt.warn('ds-bot: picking up interrupted work failed: %s', error)), RESUME_DELAY_MS) : undefined+ timer?.unref?.()+ // What the turns stopped by this shutdown left stays as last noted.+ return () => {+ stopped = true+ clearTimeout(timer)+ return writing+ }+ }, 'ds-bot: resume interrupted turns')+}diff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.jsindex d519969c..33a6394e 100644--- a/ds-bot/src/host/turns.js+++ b/ds-bot/src/host/turns.js@@ -255,6 +255,8 @@ export function install(rt) {...(source.late ? { late: true } : {}),...(source.quiet ? { quiet: true } : {}),...(source.failed ? { failed: source.failed } : {}),+ ...(source.resumed ? { resumed: true } : {}),+ ...(source.again ? { again: true } : {}),})} else if (source?.kind === SOURCE_KIND && source.role === 'kickoff' && rt.roomOf(id)) {queueRoomMessage(id, { text: '', kickoff: true })diff --git a/ds-bot/test/follow-through.test.js b/ds-bot/test/follow-through.test.jsnew file mode 100644index 00000000..37191173--- /dev/null+++ b/ds-bot/test/follow-through.test.js@@ -0,0 +1,214 @@+import assert from 'node:assert/strict'+import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import { test } from 'node:test'+import { apply } from '../index.js'+import { fakeContext } from './fake-ctx.js'++// Chief in its own chat 'a'; Writer, the admin of group 'r1', speaks there from 'gb'.+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 },+ },+ rooms: { r1: { id: 'r1', name: 'Ops', members: ['a', 'b'], mode: 'admin', admin: 'b', named: true, sessions: { b: 'gb' }, log: [], seq: 0, seen: {} } },+ agents: {},+ exchanges: [],+ exchangeTurns: {},+ questions: {},+ prefs: {},+ revision: 0,+})++// One DS Bot host on `home`; a second start on the same home is a restart.+async function start(home) {+ const ctx = fakeContext()+ const agents = new Map()+ const agentOf = (sessionId) => {+ if (!agents.has(sessionId)) {+ const calls = []+ const inbox = { nextStep: [], nextTurn: [], remove: () => false }+ const keep = (message) => { calls.push(message) }+ agents.set(sessionId, { session: { id: sessionId }, calls, inbox, followup: keep, steer: keep, cancel() {} })+ }+ 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 () => {} },+ 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, exec = {}) => ctx.registeredTools.get(name).execute(args, { agent: { id: sessionId }, ...exec })+ const askApproval = sessionId => ctx.listeners.get('approval/request')[0]({ agent: { session: { id: sessionId } }, toolName: 'pwsh', reason: 'escalate sandbox' }, () => 'from-dsh')+ const call = async (endpoint, payload) => (await route.fetch(new Request('http://localhost/api/bot', { method: 'POST', body: JSON.stringify({ endpoint, payload }) }))).json()+ await tool('list_bots', {}, 'a')+ return { ctx, agentOf, emit, tool, call, askApproval, stop: () => ctx.dispose() }+}++const freshHome = (t) => {+ const home = mkdtempSync(join(tmpdir(), 'ds-bot-follow-'))+ writeFileSync(join(home, 'state.json'), JSON.stringify(seed()))+ t.after(() => rmSync(home, { recursive: true, force: true }))+ return home+}+const settle = async (check) => {+ const until = Date.now() + 3000+ while (!check() && Date.now() < until) await new Promise(resolve => setImmediate(resolve))+ assert.ok(check(), 'condition not reached')+}+const text = message => message.content.map(block => block.text ?? '').join('\n')+const notes = (home) => {+ try { return JSON.parse(readFileSync(join(home, 'inflight.json'), 'utf8')) } catch { return { turns: {}, queued: {} } }+}+const inflight = home => notes(home).turns ?? {}+// A message as DSH would replay it: the turn starts and takes it up.+const run = (host, sessionId, turn, message) => {+ host.emit(sessionId, 'turn/start', { turn })+ host.emit(sessionId, 'user/message', { content: message.content, source: message.source })+}++// Starts a round in 'r1' and opens Writer's turn in 'gb'.+async function openRoomTurn(t, host) {+ let result+ const stop = new AbortController()+ t.after(() => stop.abort())+ void host.tool('group_relay', {}, 'r1', { callId: 'c1', signal: stop.signal }).then((value) => { result = JSON.parse(value) })+ await settle(() => host.agentOf('gb').calls.length === 1)+ run(host, 'gb', 1, host.agentOf('gb').calls[0])+ return () => result+}++test('a group turn cut off by a restart is picked up once, and the group is told', async (t) => {+ t.mock.timers.enable({ apis: ['setTimeout'] })+ const home = freshHome(t)+ const first = await start(home)+ await openRoomTurn(t, first)+ void first.askApproval('gb')+ await settle(() => inflight(home).gb?.approval !== undefined)+ assert.deepEqual([inflight(home).gb.botId, inflight(home).gb.roomId, inflight(home).gb.trigger.role], ['b', 'r1', 'room'])+ first.stop()++ const second = await start(home)+ t.after(second.stop)+ t.mock.timers.tick(3000)+ await settle(() => second.agentOf('gb').calls.length === 1)+ const post = second.agentOf('r1').calls[0]+ assert.deepEqual([post.source.role, post.source.resumed, post.source.quiet, post.source.senderSessionId], ['post', true, true, 'b'])+ const resumed = second.agentOf('gb').calls[0]+ assert.deepEqual([resumed.source.role, resumed.source.resumed, resumed.source.roomId], ['room', true, 'r1'])+ assert.match(text(resumed), /^\[Group chat "Ops" · picking up after a restart\]/)+ assert.match(text(resumed), /approval to run pwsh \(escalate sandbox\)/)+ await settle(() => Object.keys(inflight(home)).length === 0)++ // The group shows the event, and the admin reads it on its next turn.+ second.emit('r1', 'user/message', { content: post.content, source: post.source })+ const relay = JSON.parse(await second.tool('group_relay', {}, 'r1', { callId: 'c2', signal: new AbortController().signal }))+ assert.deepEqual(relay.replies.map(reply => [reply.kind, reply.name]), [['resumed', 'Writer']])+ const state = JSON.parse(readFileSync(join(home, 'state.json'), 'utf8'))+ assert.equal(state.rooms.r1.log.at(-1).kind, 'resumed')++ // Cut off again while picking it up: the group hears it, and the turn stays stopped.+ run(second, 'gb', 2, resumed)+ await settle(() => inflight(home).gb?.resumed === true)+ second.stop()+ const third = await start(home)+ t.after(third.stop)+ t.mock.timers.tick(3000)+ await settle(() => third.agentOf('r1').calls.length === 1)+ assert.equal(third.agentOf('r1').calls[0].source.again, true)+ assert.equal(third.agentOf('gb').calls.length, 0)+})++test('a request cut off by a restart goes on under the same exchange; own-chat work resumes in the chat', async (t) => {+ t.mock.timers.enable({ apis: ['setTimeout'] })+ const home = freshHome(t)+ const first = await start(home)+ run(first, 'b', 1, { content: [{ type: 'text', text: '[Message from Chief]\nDraft the plan' }], source: { kind: 'bot', form: 'relay', role: 'request', exchangeId: 'x1', senderName: 'Chief', senderSessionId: 'a', hop: 0 } })+ run(first, 'a', 1, { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } })+ await settle(() => inflight(home).b?.trigger !== undefined && inflight(home).a !== undefined)+ first.stop()++ const second = await start(home)+ t.after(second.stop)+ t.mock.timers.tick(3000)+ await settle(() => second.agentOf('b').calls.length === 1 && second.agentOf('a').calls.length === 1)+ const request = second.agentOf('b').calls[0]+ assert.deepEqual([request.source.role, request.source.exchangeId, request.source.senderSessionId, request.source.resumed], ['request', 'x1', 'a', true])+ assert.match(text(request), /^\[Message from Chief · DeepSeek Harness restarted while you were working on it\]/)+ const own = second.agentOf('a').calls[0]+ assert.deepEqual([own.source.role, own.source.resumed], ['resume', true])+ assert.match(text(own), /^\[DeepSeek Harness restarted while you were working\]/)+ const state = JSON.parse(readFileSync(join(home, 'state.json'), 'utf8'))+ assert.equal(state.exchanges.length, 0)+})++test('a finished turn leaves nothing to pick up', async (t) => {+ t.mock.timers.enable({ apis: ['setTimeout'] })+ const home = freshHome(t)+ const first = await start(home)+ run(first, 'a', 1, { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } })+ first.emit('a', 'turn/end', { turn: 1, reason: { kind: 'completed' } })+ await settle(() => Object.keys(inflight(home)).length === 0 && existsSync(join(home, 'inflight.json')))+ first.stop()+ const second = await start(home)+ t.after(second.stop)+ t.mock.timers.tick(3000)+ await new Promise(resolve => setImmediate(resolve))+ assert.equal(second.agentOf('a').calls.length, 0)+})++test('a turn DSH stops as it shuts down stays noted; one the user stops does not', async (t) => {+ const home = freshHome(t)+ const host = await start(home)+ t.after(host.stop)+ run(host, 'a', 1, { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } })+ run(host, 'b', 1, { content: [{ type: 'text', text: 'hello' }], source: { kind: 'user' } })+ host.emit('a', 'turn/end', { turn: 1, reason: { kind: 'aborted', reason: { kind: 'disposed' } } })+ host.emit('b', 'turn/end', { turn: 1, reason: { kind: 'aborted', reason: { kind: 'user' } } })+ await settle(() => inflight(home).a !== undefined && inflight(home).b === undefined)+})++test('a message a shutdown cancelled is sent again once; one still waiting, taken up or withdrawn is not', async (t) => {+ const home = freshHome(t)+ const first = await start(home)+ const send = message => first.tool('message_bot', { to: 'Writer', message, when: 'queue' }, 'a')+ await send('Draft the plan')+ await send('Check the numbers')+ await send('Read the notes')+ await send('Never mind')+ const [plan, numbers, notesMessage, withdrawn] = first.agentOf('b').calls+ // Taken up by a turn, or withdrawn while DS Bot runs (the user stopped the Bot).+ first.emit('b', 'user/message', { content: notesMessage.content, source: notesMessage.source })+ for (const listener of first.ctx.listeners.get('agent/inbox/discarded')) listener({ message: withdrawn })+ await settle(() => (notes(home).queued.b ?? []).length === 2)+ // Lets the team file's last write land before the restart reads it.+ await new Promise(resolve => setTimeout(resolve, 100))+ first.stop()++ t.mock.timers.enable({ apis: ['setTimeout'] })+ const second = await start(home)+ t.after(second.stop)+ // A hard stop keeps the inbox: that message runs when the Session wakes.+ second.agentOf('b').inbox.nextTurn.push(numbers)+ t.mock.timers.tick(3000)+ await settle(() => second.agentOf('b').calls.length === 1)+ const [again] = second.agentOf('b').calls+ assert.deepEqual([again.source.exchangeId, again.source.role, text(again)], [plan.source.exchangeId, 'request', text(plan)])+ // The resend is the same exchange, not a new one.+ assert.equal((await second.call('exchange', { a: 'a', b: 'b' })).value.length, 4)+})diff --git a/ds-bot/test/install.test.js b/ds-bot/test/install.test.jsindex 5fd05932..ee0f733a 100644--- a/ds-bot/test/install.test.js+++ b/ds-bot/test/install.test.js@@ -58,6 +58,9 @@ test('apply registers listeners, tools, and effects in a fixed order', (t) => {'tool read_group_chat','on llm/stream','on approval/request (prepend)',+ 'on agent/inbox/discarded',+ 'on session/event',+ 'effect ds-bot: resume interrupted turns','effect ds-bot: memory section','prompt bot-memory 645','tool remember',