diff --git a/CHANGELOG.md b/CHANGELOG.md index f5f125b3e7..0ee7320460 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,15 @@ All notable changes to DS Bot are listed here. The format follows [Keep a Change ## [Unreleased] +### Changed + +- A question card closes when its Bot finishes a later turn you can see that asks nothing, so a stale card no longer stays on screen until you answer it. A hidden turn that only answers another Bot leaves the card open. A click on a closed card is refused. + +### Added + +- `void_question` lets a Main Bot withdraw another Bot's open question card. The Bot is told, and a late click on the card is refused. +- A Bot that sends work from its own chat with `message_bot` now shows as waiting for the recipient until the reply arrives, on its tile and in its status. + ## [0.2.5] - 2026-11-01 ### Changed diff --git a/ds-bot/README.md b/ds-bot/README.md index 97cad49ae6..1c7cef2fa2 100644 --- a/ds-bot/README.md +++ b/ds-bot/README.md @@ -11,7 +11,7 @@ Tested with dsh `0.2.1-rc.4` (Web) and `0.2.0-rc.2` (Web and Desktop). - **Long-lived Bots**: each Bot is a dsh Session in the "DS Bot" workspace, with its own name, role, instructions and model. - **Working folders**: a Bot can work in a folder you choose instead of the team workspace, and a group chat can set one folder for all its members there. Pick it in the Bot's settings or details panel or in the group's settings, or ask a Bot (`update_bot`, `update_group`). After a change, the chat goes on in a fresh part that starts in the new folder. When a Bot sets a folder outside the team workspace, or adds a Bot to a group that works in one, it asks for your approval if those Bots can only write in their folder. Folders that hold dsh data (`$DSH_HOME`, the team's data) are refused. The team workspace itself moves in Settings > General > Locations; Bots and groups that work there go on in a fresh part in the new folder, and the old folder keeps its files. - **Permission presets**: each Bot runs with its own dsh permission preset (Read only, Workspace or Full access), set in its details panel. A group chat can run all its members with one preset there, in place of their own: turn on **Same for every member** under Permissions in the group's settings. Only you set permissions; Bots cannot change them. -- **Bots work together**: `message_bot` sends a message to another Bot, `create_bot` and `update_bot` add and set up Bots (including a profile photo from a local image file or a data URL), and `ask_user` asks you a question without blocking the other Bots. `show_view` draws a card in a Bot's own chat — text, metrics, tables, progress, choices, confirmations and short forms; an interactive card waits for your answer. +- **Bots work together**: `message_bot` sends a message to another Bot, `create_bot` and `update_bot` add and set up Bots (including a profile photo from a local image file or a data URL), and `ask_user` asks you a question without blocking the other Bots. A question card closes when the Bot finishes a later turn you can see that asks nothing, or when a Main Bot withdraws it with `void_question`; a turn that only answers another Bot leaves it open, and a late click on a closed card is refused. `show_view` draws a card in a Bot's own chat: text, metrics, tables, progress, choices, confirmations and short forms; an interactive card waits for your answer. - **Group chats**: members speak one at a time, and each one reads the latest messages first. A message can `@Name` a member to pass the turn on; type `@` to pick one from a list. **+ → Reference a chat** adds another Bot's chat to a message as context, and in a group the members who answer get it. A member with nothing new to add stays quiet (`[PASS]`). Each member has its own hidden Session per group, so a group never reads a Bot's private chat. - **Roles and permissions**: diff --git a/ds-bot/client.js b/ds-bot/client.js index e4330c8cdf..74f36c2587 100644 --- a/ds-bot/client.js +++ b/ds-bot/client.js @@ -10297,7 +10297,7 @@ function useLandOnMainBot(roster, useSessions, actions) { const lastBot = (0, import_react19.useRef)(null); (0, import_react19.useEffect)(() => { if (!roster.ready || !roster.mainBotId) return void 0; - const [id, blank3] = mainView.split("|"); + const [id] = mainView.split("|"); const bot = id === "" ? void 0 : roster.byId[id]; if (bot !== void 0) { lastBot.current = { botId: bot.id, sessionId: id }; diff --git a/ds-bot/src/client/sidebar.js b/ds-bot/src/client/sidebar.js index 0aa02433e9..9eac44932e 100644 --- a/ds-bot/src/client/sidebar.js +++ b/ds-bot/src/client/sidebar.js @@ -882,7 +882,7 @@ function useLandOnMainBot(roster, useSessions, actions) { const lastBot = useRef(null) useEffect(() => { if (!roster.ready || !roster.mainBotId) return undefined - const [id, blank] = mainView.split('|') + const [id] = mainView.split('|') const bot = id === '' ? undefined : roster.byId[id] if (bot !== undefined) { lastBot.current = { botId: bot.id, sessionId: id } diff --git a/ds-bot/src/host/constants.js b/ds-bot/src/host/constants.js index 988bff72a1..c466322db4 100644 --- a/ds-bot/src/host/constants.js +++ b/ds-bot/src/host/constants.js @@ -64,7 +64,7 @@ export const AGENT_SESSION_TITLE_MAX = 80 // profile, so the list served to one session can be shorter. export const TEAM_TOOLS = [ 'list_bots', 'message_bot', 'stop_bot', 'create_bot', 'update_bot', 'list_bot_models', - 'create_group', 'update_group', 'delete_group', 'post_to_group', 'ask_user', 'show_view', + 'create_group', 'update_group', 'delete_group', 'post_to_group', 'ask_user', 'void_question', 'show_view', 'request_secret', 'list_secrets', 'group_relay', 'read_own_chat', 'read_group_chat', 'remember', 'forget', 'recall', 'read_browser', 'list_capabilities', 'save_capability', 'delete_capability', diff --git a/ds-bot/src/host/delivery.js b/ds-bot/src/host/delivery.js index 557d690e65..d8011a6563 100644 --- a/ds-bot/src/host/delivery.js +++ b/ds-bot/src/host/delivery.js @@ -27,6 +27,7 @@ export function install(rt) { if (resent && role !== 'room') return `[Message from ${fromName} · DeepSeek Harness restarted before it could reach you]` if (role === 'reply') return `[Reply from ${fromName}${goesOn}]` if (role === 'stopped') return `[Stopped by ${fromName}]` + if (role === 'voided') return `[Question voided by ${fromName}]` if (role === 'room') return `[Group chat "${roomName}" · ${note}]` if (role === 'report') return `[Group chat "${roomName}" · replies to your post${goesOn}]` if (role === 'kickoff') return '[Bot team setup]' @@ -54,7 +55,10 @@ export function install(rt) { // length when it went as a file (attachLong). `work`: the work (handoffs.js) a // result that came back goes on with. `merged`: a request that takes in an earlier // one its recipient had not read, under that one's exchange. - async function deliver({ fromId, toId, sessionId, text, role, replyTo, roomId, audience, note, exchangeId = randomUUID(), steer = false, deferred = false, resumed = false, retried = false, resent = false, merged = false, askedBy, fromRoomId, files = [], length, work }) { + // `handedBack`: this delivery is a pointer to a message the recipient already read in + // this Session, not the message itself (turns.js), so the dialogs read the body from + // the delivery that carried it. + async function deliver({ fromId, toId, sessionId, text, role, replyTo, roomId, audience, note, exchangeId = randomUUID(), steer = false, deferred = false, resumed = false, retried = false, resent = false, merged = false, handedBack = false, askedBy, fromRoomId, files = [], length, work }) { const from = rt.botOf(fromId) const to = rt.botOf(toId) if (to === undefined) throw new Error(`Unknown Bot ${toId}`) @@ -74,6 +78,7 @@ export function install(rt) { ...(roomId ? { roomId } : {}), ...(audience === 'user' ? { audience } : {}), ...(deferred ? { deferred: true } : {}), + ...(handedBack ? { handedBack: true } : {}), ...(resumed ? { resumed: true } : {}), ...(retried ? { retried: true } : {}), ...(askedBy ? { askedBy } : {}), @@ -119,6 +124,9 @@ export function install(rt) { try { for await (const event of rt.eventsBackwards(entry.sessionId ?? rt.chatOf(entry.to), AbortSignal.timeout(HISTORY_TIMEOUT_MS))) { if (event.type !== 'user/message' || event.data?.source?.exchangeId !== entry.id) continue + // A hand-back carries a pointer, not the message; the delivery that brought it is + // further back and is what the dialog shows. + if (event.data.source.handedBack === true) continue const delivered = textOf(event.data.content) const text = delivered.slice(delivered.indexOf('\n') + 1) return { text: headOf(text, TEXT_SHOW_MAX), whole: text.length <= TEXT_SHOW_MAX } diff --git a/ds-bot/src/host/handoffs.js b/ds-bot/src/host/handoffs.js index 0dbaa81267..17ad1c809a 100644 --- a/ds-bot/src/host/handoffs.js +++ b/ds-bot/src/host/handoffs.js @@ -258,8 +258,13 @@ export function install(rt) { .filter(([id, work]) => work.to !== undefined && work.open.length > 0 && !busy.has(id)) .map(([id, work]) => ({ id, work, out: work.open.map(childId => works()[childId]).filter(Boolean) })) const busyWith = sessionIds => new Set(sessionIds.map(sessionId => rt.live.get(sessionId)).filter(entry => entry?.turn !== undefined).map(entry => workOf(entry.info)).filter(Boolean)) + // What a Bot sent from outside any work, such as its chat with the user: nothing above + // these holds their wait, and the sender waits until the reply closes them. + const sentOnOwn = () => Object.values(works()) + .filter(work => work.to !== undefined && works()[work.parent] === undefined && rt.botOf(work.from) !== undefined && rt.botOf(work.to) !== undefined) - // Per Bot, the Bots and groups it waits for, and the Bots it does that work for. + // Per Bot, the Bots and groups it waits for, and the Bots it does that work for. `for` + // stays empty for work a Bot sent on its own, which it does for the user. function waitingView() { const view = {} const add = (list, id) => { if (id !== undefined && !list.includes(id)) list.push(id) } @@ -269,6 +274,7 @@ export function install(rt) { for (const child of out) add(child.room ? entry.rooms : entry.bots, child.room ?? child.to) add(entry.for, work.from) } + for (const work of sentOnOwn()) add((view[work.from] ??= { bots: [], rooms: [], for: [] }).bots, work.to) return view } @@ -314,6 +320,8 @@ export function install(rt) { const on = [...new Set(out.map(child => (child.room ? `group "${rt.roomOf(child.room)?.name ?? 'a group'}"` : child.name ?? nameOf(child.to) ?? 'a teammate')))] lines.push(on.length > 0 ? `waiting for ${on.join(' and ')} to hand back work${forName}` : `about to go on with work${forName}, whose results came back`) } + const sent = [...new Set(sentOnOwn().filter(work => work.from === botId).map(work => nameOf(work.to)))] + if (sent.length > 0) lines.push(`waiting for ${sent.join(' and ')} to answer`) if (lines.length === 0) lines.push('idle') if (state.questions?.[chat] !== undefined) lines.push('a question card in its own chat waits for the user\'s answer') if (state.views?.[chat] !== undefined) lines.push('a view card in its own chat waits for the user\'s answer') diff --git a/ds-bot/src/host/privacy.js b/ds-bot/src/host/privacy.js index dc48b973e1..c11cf2822b 100644 --- a/ds-bot/src/host/privacy.js +++ b/ds-bot/src/host/privacy.js @@ -195,6 +195,11 @@ export function install(rt) { if (bot) throw closed(bot, 'turns') return } + case 'void_question': { + const bot = target(args.bot) + if (bot) throw closed(bot, 'questions') + return + } case 'attach_capability': case 'detach_capability': { const bot = target(args.bot) diff --git a/ds-bot/src/host/prompt.js b/ds-bot/src/host/prompt.js index 682b53d54b..5c594001c0 100644 --- a/ds-bot/src/host/prompt.js +++ b/ds-bot/src/host/prompt.js @@ -14,7 +14,7 @@ const TEAM_RULES = [ '- Only for a direct user request in your own chat that needs several steps: write one short sentence of normal reply text before your first tool call, and make that tool call in the same response. Never start such a task with tools alone or end it with a promise. On long tasks, give brief factual updates only at meaningful milestones, then keep working.', '- For "[Message from …]" or "[Reply from …]" messages, do not write plans or progress updates: use tools silently, then reply with the answer. Simple answers do not need progress messages.', '- In a group chat, the group sees what you write while you work, not only your final reply. For work that takes more than a quick look, start with one short sentence on what you will do, in the same response as your first tool call. After that, write only when you find something the group should know or finish a part of the work, in a sentence or two, not at every step. End with the result. Simple answers need no updates.', - '- Offer choices with ask_user (a tap-to-answer card) instead of long option lists in text. Calling it ends your turn at once, so write everything you want to say before the call, in the same reply. The answer arrives as the user\'s next message.', + '- Offer choices with ask_user (a tap-to-answer card) instead of long option lists in text. Calling it ends your turn at once, so write everything you want to say before the call, in the same reply. The answer arrives as the user\'s next message. The card stays open only until your next finished turn the user can see: a turn in your own chat that asks nothing closes it and a later click on it is refused, so ask again if you still need the answer. A turn that answers another Bot leaves it open.', '- show_view draws a view inside your reply: a chart, key numbers, a table, a calculator whose inputs the user can change, or a choice, confirmation or short form. Use it when it shows something better than text, not for every answer. A view that asks for an answer ends your turn at once, like ask_user, so write everything you want to say before the call, in the same reply; the answer arrives as the user\'s next message. It only works in your own chat, not in a group. Never collect a key, token, or password with it; call request_secret. The view argument is limited to 8 KB.', '- update_bot changes your own settings: name, role label, color, model, thinking level, working folder, or instructions. Leave out bot to change yourself. Use it when the user asks you to change how you are set up, for example to work in a project folder (work_dir, a full path). Only a Main Bot can change other Bots; when another Bot needs changing, ask a Main Bot.', '- You may be in group chats with the user and other Bots. You speak in each group from a separate conversation of yours, apart from your own chat with the user. There your turn arrives as a "[Group chat …]" message that shows the speaking order, and your reply is posted to the group. When someone there asks you to do something, do the work in that turn: the group goes on without waiting and sees what you write as you go. Any member can hand work to another with @Name, and the one that asked answers the result. Work for a group stays in it: there message_bot reaches only members of that group, as @Name does. In a group, read_own_chat shows your own chat (the group sees that you looked; share only what the group needs). In your own chat, read_group_chat shows what was said in a group. A group may have an admin, who steers it: the admin can call members with @Name and change the group with update_group. From outside a group (your own chat or another group), post_to_group posts into a group you are in: the admin\'s post starts a new discussion there; another member\'s post calls only the members it names with @Name.', @@ -31,6 +31,7 @@ const MAIN_RULES = [ '- After creating a Bot, tell the user in one line that it is ready, using its exact name.', '- Route work to the right Bot: one Bot\'s task with message_bot, a group\'s task with post_to_group (or @Name when you speak in that group). Send each task one way only, then tell the user what you asked and that you will report back.', '- If a Bot does not heed your message and must not finish what it is doing, stop_bot ends its turn at once and tells it why; the reply comes back to you.', + '- When a question card another Bot left open no longer applies, void_question withdraws it and tells that Bot; a late click on it is refused.', '- Every Bot runs on one model of its own (list_bots shows it). Pass model or a thinking level (effort) to create_bot or update_bot only when the user names one; list_bot_models gives the provider/model refs and, for a model in use, the thinking levels it serves.', '- Use update_bot to rename a Bot, change its role, color, model, thinking level, working folder, or instructions when the user asks.', '- You run the group chats too. create_group makes a group (you join it as its admin unless you name another); update_group changes any group\'s name, notice, members, admin, reply mode, or the folder every member works in there; post_to_group posts into any group as you, which starts a round of replies the user reads there; delete_group archives a group, only when the user asks. list_bots shows every group.', diff --git a/ds-bot/src/host/tools.js b/ds-bot/src/host/tools.js index 5a74daac2a..95ca21a616 100644 --- a/ds-bot/src/host/tools.js +++ b/ds-bot/src/host/tools.js @@ -700,6 +700,39 @@ export function install(rt) { }, })) + rt.teamTools.push(defineTool({ + name: 'void_question', + description: 'Main Bot only. Withdraw the question card another Bot left open for the user, when it no longer applies. The card leaves the screen, a late click on it is refused, and the Bot is told in a new turn.', + parameters: { + bot: { type: 'string', required: true, description: 'Exact Bot name from list_bots' }, + reason: { type: 'string', description: 'Why it was voided; the Bot reads this' }, + }, + output, + async execute(args, exec) { + await rt.load() + const sender = callerBot(exec) + if (sender === undefined || !rt.isMain(sender.id)) refuse('Only a Main Bot can void a question.') + const target = rt.findBot(args.bot) + if (target === undefined) refuse(`No Bot named ${args.bot}. Team: ${Object.values(state.bots).map(bot => bot.name).join(', ')}`) + const chat = rt.chatOf(target.id) + const question = rt.own(state.questions, chat) + if (question === undefined) refuse(`${target.name} has no open question.`) + delete state.questions[chat] + await rt.save() + if (target.id === sender.id) return 'Your question is voided.' + const why = String(args.reason ?? '').trim() + const text = `Your question "${question.question}" is withdrawn, so no answer to it will reach you. Do not act on it.${why ? ` +Reason: ${why}` : ''}` + try { + await rt.deliver({ fromId: sender.id, toId: target.id, text, role: 'voided', ...callerRoom(exec) }) + } catch (error) { + rt.warn('ds-bot: telling %s its question was voided failed: %s', target.name, error) + return `The question of ${target.name} is voided, but ${target.name} could not be told. Tell it with message_bot.` + } + return `The question of ${target.name} is voided and it has been told.` + }, + })) + // Scoped per Bot session, not global: another plugin's global show_view serves // the sessions ds-bot does not own, while this one shadows it inside them. rt.teamTools.push(defineTool({ diff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.js index abc7a971d6..6d55232d4a 100644 --- a/ds-bot/src/host/turns.js +++ b/ds-bot/src/host/turns.js @@ -107,6 +107,11 @@ const RETRY_PROMISE_TEXT = 'Your last reply said you would start working, but yo const PROMISED_WORK = /(?:^(?:收到|好的|明白|好|行|没问题|了解)[,,、。.!!:\s]*(?:我(?:现在|这就|先|马上|立刻|去|来)?(?:开始|着手|去|来|准备)?(?:排查|检查|调查|查看|看看|搜索|搜一下|分析|处理|执行|运行|跑一下|修改|修复|实现|编写|整理|验证|测试|读一下|翻一下)|(?:现在|马上|立刻)?(?:开始|着手)(?:执行|处理|排查|检查|调查|干活|开工)|开工(?:[。!!,,\s]|$))|^(?:开工(?:[。!!,,\s]|$)|我(?:现在|这就|先|马上|立刻)(?:开始|着手|去)(?:排查|检查|调查|查看|搜索|分析|处理|执行|运行|修改|修复|实现|编写|整理|验证|测试))|\b(?:(?:i\s+will|i'll|let\s+me|going\s+to)\s+(?:start|begin|check|look\s+into|investigate|search|run|inspect|read|fix|implement|work\s+on)\b|(?:starting|beginning)\s+(?:the\s+)?(?:investigation|check|search|work|execution|analysis)\b|^on\s+it[,.\s!]+(?:i'll|i\s+will|let\s+me|starting|checking)\b))/i export const promisesWork = text => typeof text === 'string' && text.trim().length <= 240 && PROMISED_WORK.test(text.trim()) +// What a hand-back carries in place of the body. The Bot already read that message in +// this Session, so sending it again makes the model read one message twice; the pointer +// is enough to say the work is over and this one is now due an answer. +export const HANDED_BACK = 'You read its text during that task; it is above in this session. Answer it now.' + export function install(rt) { const { ctx, state } = rt @@ -261,7 +266,7 @@ export function install(rt) { /** @type {Record} */ let activity = {} const activityOf = (tool) => { - if (/^(message_bot|stop_bot|create_bot|update_bot|create_group|update_group|delete_group|post_to_group)$/.test(tool)) return 'sending' + if (/^(message_bot|stop_bot|void_question|create_bot|update_bot|create_group|update_group|delete_group|post_to_group)$/.test(tool)) return 'sending' if (tool === 'group_relay') return 'orbit' if (/search|fetch|browse|browser|read|grep|glob|list/i.test(tool)) return 'searching' return 'working' @@ -590,6 +595,7 @@ export function install(rt) { entry.carried = false let claimedPending let claimedPendingText + entry.card = state.questions[id]?.id if (entry.pending !== undefined) { claimedPending = entry.pending claimedPendingText = entry.pendingText @@ -798,6 +804,7 @@ export function install(rt) { entry.carry = { info: entry.info, joined: entry.joined ?? [], later: entry.later ?? [], texts: entry.texts.slice(0, cut.texts), shown: entry.shown, answers: entry.answers, toolCount: entry.toolCount ?? 0 } } const info = entry.info + const turn = entry.turn const joined = entry.joined ?? [] // What the group already saw of the turn is not posted again. const texts = entry.texts.slice(entry.shown) @@ -825,17 +832,20 @@ export function install(rt) { const later = entry.later ?? [] entry.later = [] if (steerCut || suppressedSubagent) break + const card = entry.card + entry.card = undefined for (const result of later) { void rt.deliver({ fromId: result.info.senderSessionId, toId: rt.selfOf(id), - text: result.text, + text: HANDED_BACK, role: result.info.role, exchangeId: result.info.exchangeId, replyTo: result.info.replyTo, work: result.info.work, ...(result.info.roomId ? { roomId: result.info.roomId } : {}), deferred: true, + handedBack: true, }).catch(error => rt.warn('ds-bot: handing back a result failed: %s', error)) } const reason = event.data?.reason @@ -869,6 +879,16 @@ export function install(rt) { void retryWrittenCall(id, info).catch(error => rt.warn('ds-bot: asking %s to run its written tool call failed: %s', id, error)) } } + // A finished turn the user could see that asked nothing has moved on, so the card + // it left standing is stale. `card` is the card this turn opened on: a turn that + // asked has another id, or none here at all. A hidden Bot-to-Bot turn shows the user + // nothing, so its card still waits for them. A fresh part's handoff turn carries its + // card over; a failed or stopped turn said nothing about it. + const hidden = (state.exchangeTurns[id] ?? []).includes(turn) + if (card !== undefined && state.questions[id]?.id === card && reason?.kind === 'completed' && !retry && !hidden && info?.handoff !== true) { + delete state.questions[id] + void rt.save().catch(() => {}) + } 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 @@ -940,12 +960,13 @@ export function install(rt) { fromId: request.info.senderSessionId, toId: rt.selfOf(id), sessionId: id, - text: request.text, + text: HANDED_BACK, role: request.info.role, exchangeId: request.info.exchangeId, ...(request.info.role === 'room' ? { roomId: request.info.roomId, askedBy: request.info.askedBy, note: 'you read this call during your last task; answer it now' } : {}), ...(request.info.senderRoomId ? { fromRoomId: request.info.senderRoomId } : {}), deferred: true, + handedBack: true, }).catch(error => rt.warn('ds-bot: deferred request delivery failed: %s', error)) } break diff --git a/ds-bot/test/bot-messages.test.js b/ds-bot/test/bot-messages.test.js index 863bad3642..aa3c3da48d 100644 --- a/ds-bot/test/bot-messages.test.js +++ b/ds-bot/test/bot-messages.test.js @@ -170,7 +170,7 @@ test('a Bot busy in a group gets the message in its own chat, and the sender is // A non-Main sender gets no stop_bot hint. emit('ga', 'turn/start', { turn: 1 }) const back = await tool('message_bot', { to: 'Chief', message: 'done' }, 'b') - assert.equal(back, 'Delivered to Chief. Its reply will arrive later as a new message; do not wait for it. Chief now: working in group "Ops" for less than a minute. Your message does not reach its work in group "Ops".') + assert.equal(back, 'Delivered to Chief. Its reply will arrive later as a new message; do not wait for it. Chief now: working in group "Ops" for less than a minute; waiting for Writer to answer. Your message does not reach its work in group "Ops".') }) test('a request steered into a running turn is recorded once and answered in a turn of its own', async (t) => { @@ -190,7 +190,7 @@ test('a request steered into a running turn is recorded once and answered in a t assert.equal(agents.get('a')?.calls.length ?? 0, 0) const [kind, again] = agents.get('b').calls[1] assert.equal(kind, 'followup') - assert.equal(textOf(again.content), '[Message from Chief · you read it during your last task; answer it now]\nwhich year is the data?') + assert.equal(textOf(again.content), `[Message from Chief · you read it during your last task; answer it now]\n${HANDED_BACK}`) assert.equal(again.source.deferred, true) assert.equal(again.source.exchangeId, steered.source.exchangeId) assert.deepEqual((await exchanges()).map(entry => entry.role), ['request']) @@ -235,7 +235,7 @@ test('a user message steered into the running steps marks when it landed', async assert.deepEqual(await steered(), {}) }) -test('a request from a name with brackets keeps its body when replayed after the current work', async (t) => { +test('a request from a name with brackets is handed back under a well-formed header', async (t) => { const seed = team() seed.bots.a.name = 'R]2' const { agents, emit, tool, stop } = await start(seed) @@ -253,7 +253,10 @@ test('a request from a name with brackets keeps its body when replayed after the const [kind, again] = agents.get('b').calls[1] assert.equal(kind, 'followup') assert.equal(again.source.exchangeId, steered.source.exchangeId) - assert.equal(textOf(again.content), `[Message from R]2 · you read it during your last task; answer it now]\n${body}`) + // The bracket in the sender's name leaves the header one pair of brackets, and the + // hand-back points at the body it already read instead of replaying it. + assert.equal(textOf(again.content), `[Message from R]2 · you read it during your last task; answer it now]\n${HANDED_BACK}`) + assert.doesNotMatch(textOf(again.content), /Keep this line/) }) test('a message its recipient has not read yet takes in the next one, however many come', async (t) => { @@ -374,6 +377,8 @@ function turnsHost() { const replySource = (role, extra = {}) => ({ kind: SOURCE_KIND, role, senderSessionId: 'a', senderName: 'Chief', exchangeId: 'x1', ...extra }) const textBlock = (content) => [{ type: 'text', text: content }] const assistantText = text => ({ message: { content: textBlock(text) } }) +// What a hand-back carries instead of the body it already read (turns.js HANDED_BACK). +const HANDED_BACK = 'You read its text during that task; it is above in this session. Answer it now.' test('a stopped notice routes the acknowledgement back like a request', () => { const { rt, emit, delivered } = turnsHost() @@ -400,7 +405,8 @@ test('a request that joins a user-started turn is handed back, not answered with emit('b', 'user/message', { content: textBlock('[Message from Chief]\nmake it shorter'), source: replySource('request', { exchangeId: 'e1' }) }) emit('b', 'assistant/message', assistantText('second half')) emit('b', 'turn/end', { turn: 1, reason: { kind: 'completed' } }) - assert.deepEqual(delivered.map(deferredOf), [['a', 'b', 'b', 'request', 'e1', true, 'make it shorter']]) + assert.deepEqual(delivered.map(deferredOf), [['a', 'b', 'b', 'request', 'e1', true, HANDED_BACK]]) + assert.equal(delivered[0].handedBack, true) assert.deepEqual(rt.state.exchangeTurns, {}) // The handed-back request opens a hidden turn whose text is the reply. @@ -421,7 +427,7 @@ test('a request that joins a Bot-triggered turn is handed back; the trigger gets emit('b', 'assistant/message', assistantText('second half')) emit('b', 'turn/end', { turn: 1, reason: { kind: 'completed' } }) assert.deepEqual(rt.state.exchangeTurns.b, [1]) - assert.deepEqual(delivered.map(job => [job.role, job.replyTo ?? job.exchangeId, job.text]), [['reply', 'e0', 'first half\n\nsecond half'], ['request', 'e1', 'more']]) + assert.deepEqual(delivered.map(job => [job.role, job.replyTo ?? job.exchangeId, job.text]), [['reply', 'e0', 'first half\n\nsecond half'], ['request', 'e1', HANDED_BACK]]) }) test('a joined request in a stopped turn tells its sender, and is not handed back', () => { @@ -473,7 +479,7 @@ test('a schedule-started turn stays visible when a request joins', () => { emit('b', 'user/message', { content: textBlock('[Message from Chief]\nmore'), source: replySource('request', { exchangeId: 'e1' }) }) emit('b', 'assistant/message', assistantText('after')) emit('b', 'turn/end', { turn: 1, reason: { kind: 'completed' } }) - assert.deepEqual(delivered.map(job => [job.role, job.exchangeId, job.text]), [['request', 'e1', 'more']]) + assert.deepEqual(delivered.map(job => [job.role, job.exchangeId, job.text]), [['request', 'e1', HANDED_BACK]]) assert.deepEqual(rt.state.exchangeTurns, {}) }) @@ -518,7 +524,7 @@ test('a joined request crosses a part switch and is handed back in the new part' assert.equal(textOf(replies[0][1].content), '[Reply from Writer]\ndone') assert.equal(handedBack()[0], 'followup') assert.equal(handedBack()[1].source.exchangeId, 'e1') - assert.equal(textOf(handedBack()[1].content), '[Message from Chief · you read it during your last task; answer it now]\nmore') + assert.equal(textOf(handedBack()[1].content), `[Message from Chief · you read it during your last task; answer it now]\n${HANDED_BACK}`) }) // Each Session takes up the newest message it was sent as its next turn. @@ -575,7 +581,9 @@ test('a Bot\'s answer waits for the work it handed on, and carries what came bac const { agents, tool, call } = team const shown = async () => { const { value } = await call('state', {}); return [value.activity.b === 'waiting', value.work.waiting] } const { take, end, handed } = await handedOn(team) - assert.deepEqual(await shown(), [false, {}]) + // Chief sent the work from its own chat, so it waits for Writer until the end. + const sent = { a: { bots: ['b'], rooms: [], for: [] } } + assert.deepEqual(await shown(), [false, sent]) assert.match(handed, /When you have handed on everything, end your turn: each reply brings you back to this work, and your answer goes to Chief once all of it is back\.$/) end('b', 'handed it to Tester') // Nothing goes up yet; Writer waits, and the team can see it. @@ -583,13 +591,13 @@ test('a Bot\'s answer waits for the work it handed on, and carries what came bac assert.deepEqual(Object.values(await worksIn(team)).map(work => [work.from, work.to, work.open.length, work.held]), [['a', 'b', 1, 'handed it to Tester'], ['b', 'c', 0, undefined]]) assert.deepEqual(callsOf(agents, 'a'), []) // Its mark shows it waits, and its row whom for. - assert.deepEqual(await shown(), [true, { b: { bots: ['c'], rooms: [], for: ['a'] } }]) + assert.deepEqual(await shown(), [true, { ...sent, b: { bots: ['c'], rooms: [], for: ['a'] } }]) take('c') end('c', 'two things to fix') await settled(() => callsOf(agents, 'b').length === 2) const back = take('b') - assert.deepEqual(await shown(), [false, {}]) + assert.deepEqual(await shown(), [false, sent]) assert.equal(textOf(back.content), '[Reply from Tester · for the work Chief asked you for; your answer goes to Chief once everything you handed on is back]\ntwo things to fix') end('b', 'fixed both') await settled(() => callsOf(agents, 'a').length === 1) @@ -666,7 +674,7 @@ test('a result read during other work comes back in a turn of its own and goes o await settled(() => callsOf(agents, 'b').length === 3) const [kind, again] = callsOf(agents, 'b')[2] assert.equal(kind, 'followup') - assert.equal(textOf(again.content), '[Reply from Tester · you read it during your last task; go on with the work it is for now · for the work Chief [ops] asked you for; your answer goes to Chief [ops] once everything you handed on is back]\nreviewed') + assert.equal(textOf(again.content), `[Reply from Tester · you read it during your last task; go on with the work it is for now · for the work Chief [ops] asked you for; your answer goes to Chief [ops] once everything you handed on is back]\n${HANDED_BACK}`) assert.deepEqual(callsOf(agents, 'a'), []) take('b') end('b', 'done after the review') diff --git a/ds-bot/test/group-work.test.js b/ds-bot/test/group-work.test.js index 099c57d080..d3e784d531 100644 --- a/ds-bot/test/group-work.test.js +++ b/ds-bot/test/group-work.test.js @@ -350,7 +350,7 @@ test('replies a post waits for are work handed on: the answer to the Bot that as assert.match(posted, /your answer goes to Tester once all of it is back\.$/) host.finish('a', '已经让 Writer 去看了。') const waiting = (await host.call('state', {})).value - assert.deepEqual([waiting.activity.a, waiting.work.waiting], ['waiting', { a: { bots: [], rooms: ['r1'], for: ['c'] } }]) + assert.deepEqual([waiting.activity.a, waiting.work.waiting], ['waiting', { a: { bots: [], rooms: ['r1'], for: ['c'] }, c: { bots: ['a'], rooms: [], for: [] } }]) const done = host.round('c1', host.takePost()) await settle(() => host.agentOf('gb').calls.length === 1) host.begin('gb') @@ -423,7 +423,10 @@ test('a steered group call keeps the later caller when the earlier work ends', a assert.equal(deferred.source.role, 'room') assert.equal(deferred.source.askedBy, 'a') assert.equal(deferred.source.roomId, 'r1') - assert.match(text(deferred), /改用新模板/) + // The later call is the one handed back, and it points at what the member already read. + assert.equal(deferred.source.exchangeId, correction.source.exchangeId) + assert.match(text(deferred), /you read this call during your last task; answer it now/) + assert.doesNotMatch(text(deferred), /改用新模板/) host.finish('gb', '已换成新模板。') await settle(() => host.agentOf('r1').calls.some(message => message.source.askedBy === 'a')) const report = host.agentOf('r1').calls.find(message => message.source.askedBy === 'a') diff --git a/ds-bot/test/handback.test.js b/ds-bot/test/handback.test.js new file mode 100644 index 0000000000..6b8362b5d9 --- /dev/null +++ b/ds-bot/test/handback.test.js @@ -0,0 +1,229 @@ +// A message a Bot already read mid-turn is handed back to answer in a turn of its +// own. The body is already in that Session above, so the hand-back carries a pointer +// to it instead of the whole text again; otherwise the model reads one message twice. +import assert from 'node:assert/strict' +import { mkdtempSync, 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 { createRuntime } from '../index.js' +import { fakeContext } from './fake-ctx.js' +import { install as installTurns } from '../src/host/turns.js' +import { SOURCE_KIND } from '../src/host/constants.js' + +// The line a hand-back carries in place of the body, which is above it in the same Session. +const HANDED_BACK = 'You read its text during that task; it is above in this session. Answer it now.' + +// A turn host that records what turns.js asks delivery to do, without delivering it. +function turnHost({ workOf } = {}) { + let listener + const delivered = [] + const rt = { + ctx: { on: (name, fn) => { if (name === 'session/event') listener = fn } }, + state: { exchangeTurns: {}, questions: {} }, + bots: { a: { id: 'a', name: 'Chief' }, b: { id: 'b', name: 'Writer' } }, + isBotSession: () => true, + roomOf: () => undefined, + groupOwners: new Map(), + chatOwners: new Map(), + botOf: id => rt.bots[id], + selfOf: id => id, + save: async () => {}, + deliver: async (args) => { delivered.push(args) }, + noteFailure() {}, + warn() {}, + switchJobs: () => [], + dropKickoff() {}, + ...(workOf === undefined ? {} : { workOf }), + } + installTurns(rt) + let seq = 0 + const emit = (id, type, data) => listener({ id }, { type, seq: ++seq, data }) + return { rt, emit, delivered } +} + +const relaySource = (role, extra = {}) => ({ + kind: SOURCE_KIND, form: 'relay', role, senderSessionId: 'a', senderName: 'Chief', exchangeId: 'x1', ...extra, +}) +const textBlock = content => [{ type: 'text', text: content }] +const assistantText = text => ({ message: { content: textBlock(text) } }) +const textOf = content => content.map(block => block.text ?? '').join('\n') +// The deferred deliveries: the hand-backs this change is about. +const handBacks = delivered => delivered.filter(job => job.deferred === true) + +test('a joined request is handed back as a pointer, not the body again', () => { + const { emit, delivered } = turnHost() + emit('b', 'turn/start', { turn: 1 }) + emit('b', 'user/message', { content: textBlock('write the letter'), source: { kind: 'user' } }) + emit('b', 'assistant/message', assistantText('first half')) + emit('b', 'user/message', { content: textBlock('[Message from Chief]\nmake it shorter'), source: relaySource('request', { exchangeId: 'e1' }) }) + emit('b', 'assistant/message', assistantText('second half')) + emit('b', 'turn/end', { turn: 1, reason: { kind: 'completed' } }) + + const [handBack] = handBacks(delivered) + assert.equal(handBacks(delivered).length, 1) + // Everything about the hand-back is as it was; only the text is not the body. + assert.equal(handBack.fromId, 'a') + assert.equal(handBack.toId, 'b') + assert.equal(handBack.sessionId, 'b') + assert.equal(handBack.role, 'request') + assert.equal(handBack.exchangeId, 'e1') + assert.equal(handBack.text, HANDED_BACK) + assert.equal(handBack.handedBack, true) + assert.doesNotMatch(handBack.text, /make it shorter/) +}) + +test('a reply handed back for other work is a pointer too', () => { + const { emit, delivered } = turnHost({ workOf: source => source?.work }) + // The turn answers the work Chief asked for under w1; the reply below belongs to w2. + emit('b', 'turn/start', { turn: 1 }) + emit('b', 'user/message', { content: textBlock('[Message from Chief]\ntask'), source: relaySource('request', { exchangeId: 'e0', work: 'w1' }) }) + emit('b', 'assistant/message', assistantText('worked on w1')) + emit('b', 'user/message', { content: textBlock('[Reply from Chief]\nreviewed'), source: relaySource('reply', { exchangeId: 'e1', work: 'w2', replyTo: 'r0' }) }) + emit('b', 'assistant/message', assistantText('still on w1')) + emit('b', 'turn/end', { turn: 1, reason: { kind: 'completed' } }) + + const [handBack] = handBacks(delivered) + assert.equal(handBack.role, 'reply') + assert.equal(handBack.exchangeId, 'e1') + assert.equal(handBack.replyTo, 'r0') + assert.equal(handBack.work, 'w2') + assert.equal(handBack.text, HANDED_BACK) + assert.equal(handBack.handedBack, true) + assert.doesNotMatch(handBack.text, /reviewed/) +}) + +// The rest runs the real relay, so the assertions are on the message the model is given. + +// A two-Bot team: a is Chief, b is Writer. +function team() { + return { + 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: {}, + agents: {}, + exchanges: [], + exchangeTurns: {}, + questions: {}, + prefs: {}, + revision: 0, + } +} + +// Services for a full apply(): resolveAgent returns a per-Session agent that records +// followup/steer calls, so the test sees what the model is actually handed. +async function start(seed) { + const home = mkdtempSync(join(tmpdir(), 'ds-bot-handback-')) + writeFileSync(join(home, 'state.json'), JSON.stringify(seed)) + 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 () => {} }, + }) + const rt = createRuntime(ctx, { home }) + let seq = 0 + const emit = (id, type, data, extra) => { + for (const listener of ctx.listeners.get('session/event') ?? []) { + if (listener.length >= 2) listener({ id }, { type, seq: ++seq, data, ...extra }) + } + } + const tool = (name, args, sessionId) => rt.teamTools.find(def => def.name === name).execute(args, { agent: { id: sessionId } }) + const stop = async () => { + await ctx.dispose() + rmSync(home, { recursive: true, force: true, maxRetries: 5 }) + } + // Loading once fills the Session-owner indexes turns.js filters events by. + await tool('list_bots', {}, 'a') + return { ctx, agents, emit, tool, home, stop } +} + +test('one message reaches the model once: steered with its body, then handed back as a pointer', async (t) => { + const { agents, emit, tool, stop } = await start(team()) + t.after(stop) + + emit('b', 'turn/start', { turn: 1 }) + emit('b', 'user/message', { content: textBlock('write the letter'), source: { kind: 'user' } }) + await tool('message_bot', { to: 'Writer', message: 'which year is the data?' }, 'a') + + // What the running turn was shown, cut in at its next step. + const steered = agents.get('b').calls[0][1] + assert.ok(steered, 'the running turn was steered') + assert.match(textOf(steered.content), /which year is the data\?/) + emit('b', 'user/message', { content: steered.content, source: steered.source }) + emit('b', 'assistant/message', assistantText('the letter, for the user')) + emit('b', 'turn/end', { turn: 1, reason: { kind: 'completed' } }) + for (let count = 0; count < 200 && agents.get('b').calls.length < 2; count += 1) await delay(5) + + // It is still handed back, so the sender still gets its answer. + assert.deepEqual(agents.get('b').calls.map(([kind]) => kind), ['steer', 'followup']) + const [, handBack] = agents.get('b').calls[1] + assert.equal(handBack.source.exchangeId, steered.source.exchangeId) + assert.equal(handBack.source.deferred, true) + assert.equal(handBack.source.handedBack, true) + // The body is in the steer and not in the hand-back: the model reads it once. + assert.doesNotMatch(textOf(handBack.content), /which year is the data\?/) + assert.match(textOf(handBack.content), /it is above in this session/) + const shown = [steered, handBack].filter(message => textOf(message.content).includes('which year is the data?')) + assert.equal(shown.length, 1) +}) + +test('a message that arrives while the Bot is idle keeps its whole body', async (t) => { + const { agents, tool, stop } = await start(team()) + t.after(stop) + + await tool('message_bot', { to: 'Writer', message: 'how are you' }, 'a') + assert.deepEqual(agents.get('b').calls.map(([kind]) => kind), ['steer']) + const [, only] = agents.get('b').calls[0] + // Nothing was read before it, so nothing is handed back: no pointer, no flag. + assert.equal(textOf(only.content), '[Message from Chief]\nhow are you') + assert.equal(only.source.handedBack, undefined) + assert.equal(only.source.deferred, undefined) +}) diff --git a/ds-bot/test/question-lifecycle.test.js b/ds-bot/test/question-lifecycle.test.js new file mode 100644 index 0000000000..b637f309a1 --- /dev/null +++ b/ds-bot/test/question-lifecycle.test.js @@ -0,0 +1,271 @@ +// How a question card ends: answered, overtaken by a finished turn the user can see that +// asks nothing, or voided by a Main Bot. Whatever ended it, a late click is refused. +import assert from 'node:assert/strict' +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { test } from 'node:test' +import { createRuntime } from '../index.js' +import { fakeContext } from './fake-ctx.js' + +function team() { + return { + 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: 'Scout', role: 'Find', sessionId: 'c', createdAt: 3 }, + }, + rooms: {}, + agents: {}, + exchanges: [], + exchangeTurns: {}, + questions: {}, + prefs: {}, + revision: 0, + } +} + +async function start() { + const home = mkdtempSync(join(tmpdir(), 'ds-bot-questions-')) + 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() {}, + cancel() {}, + async whenIdle() {}, + async runMaintenance(fn) { return fn(AbortSignal.timeout(60_000)) }, + inbox: { nextStep: [], nextTurn: [], remove: () => 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) + } + const rt = createRuntime(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) => rt.teamTools.find(def => def.name === name).execute(args, { agent: { id: sessionId }, callId: 'call-1', concludeTurn() {} }) + const call = async (endpoint, payload) => (await route.fetch(new Request('http://localhost/api/bot', { + method: 'POST', body: JSON.stringify({ endpoint, payload }), + }))).json() + const pending = async sessionId => (await call('state', {})).value.questions[sessionId] + const stop = async () => { + await ctx.dispose() + rmSync(home, { recursive: true, force: true, maxRetries: 5 }) + } + await tool('list_bots', {}, 'a') + return { ctx, agentOf, emit, tool, call, pending, stop } +} + +const card = { question: 'Which one?', options: [{ label: 'Left' }, { label: 'Right' }] } + +// One turn of the Bot: it may call `during`, then ends for `reason`. +async function turn(team, id, number, during, reason = { kind: 'completed' }) { + team.emit(id, 'turn/start', { turn: number }) + await during?.() + team.emit(id, 'assistant/message', { message: { content: [{ type: 'text', text: 'Done.' }] } }) + team.emit(id, 'turn/end', { turn: number, reason }) +} + +// One turn another Bot started, on the message delivery really handed over. +async function botTurn(team, id, number, args) { + await team.tool('message_bot', args, 'a') + const [, message] = team.agentOf(id).calls.at(-1) + team.emit(id, 'turn/start', { turn: number }) + team.emit(id, 'user/message', { content: message.content, source: message.source }) + team.emit(id, 'assistant/message', { message: { content: [{ type: 'text', text: 'Done.' }] } }) + team.emit(id, 'turn/end', { turn: number, reason: { kind: 'completed' } }) +} + +test('the turn that asks keeps its card', async (t) => { + const team = await start() + t.after(team.stop) + await turn(team, 'b', 1, () => team.tool('ask_user', card, 'b')) + assert.equal((await team.pending('b')).question, 'Which one?') +}) + +test('a card followed by a plain turn leaves nothing pending', async (t) => { + const team = await start() + t.after(team.stop) + await turn(team, 'b', 1, () => team.tool('ask_user', card, 'b')) + await turn(team, 'b', 2) + assert.equal(await team.pending('b'), undefined) +}) + +test('a hidden Bot-to-Bot turn leaves a waiting card for the user, who can still answer it', async (t) => { + const team = await start() + t.after(team.stop) + await turn(team, 'b', 1, () => team.tool('ask_user', card, 'b')) + const { id } = await team.pending('b') + await botTurn(team, 'b', 2, { to: 'Writer', message: 'Status?' }) + const waiting = await team.pending('b') + assert.equal(waiting?.id, id) + const answered = await team.call('answer-question', { sessionId: 'b', questionId: id, answer: { selected: ['Left'] } }) + assert.equal(answered.ok, true) +}) + +test('a card that outlived a hidden turn closes at the next turn the user sees', async (t) => { + const team = await start() + t.after(team.stop) + await turn(team, 'b', 1, () => team.tool('ask_user', card, 'b')) + await botTurn(team, 'b', 2, { to: 'Writer', message: 'Status?' }) + await turn(team, 'b', 3) + assert.equal(await team.pending('b'), undefined) +}) + +test('a Bot-started turn that answers the user in its own chat clears the card', async (t) => { + const team = await start() + t.after(team.stop) + await turn(team, 'b', 1, () => team.tool('ask_user', card, 'b')) + await botTurn(team, 'b', 2, { to: 'Writer', message: 'Say hello.', reply_to: 'user' }) + assert.equal(await team.pending('b'), undefined) +}) + +test('a turn that asks again leaves only its own card', async (t) => { + const team = await start() + t.after(team.stop) + await turn(team, 'b', 1, () => team.tool('ask_user', card, 'b')) + const first = await team.pending('b') + await turn(team, 'b', 2, () => team.tool('ask_user', { ...card, question: 'And now?' }, 'b')) + const second = await team.pending('b') + assert.notEqual(second.id, first.id) + assert.equal(second.question, 'And now?') +}) + +test('a turn that failed or was stopped asked nothing, so the card stays', async (t) => { + const team = await start() + t.after(team.stop) + await turn(team, 'b', 1, () => team.tool('ask_user', card, 'b')) + await turn(team, 'b', 2, undefined, { kind: 'aborted' }) + assert.notEqual(await team.pending('b'), undefined) + await turn(team, 'b', 3, undefined, { kind: 'error', error: { code: 'NETWORK', message: 'down' } }) + assert.notEqual(await team.pending('b'), undefined) +}) + +test('the handoff turn of a fresh part keeps the card it carried over', async (t) => { + const team = await start() + t.after(team.stop) + await turn(team, 'b', 1, () => team.tool('ask_user', card, 'b')) + team.emit('b', 'turn/start', { turn: 2 }) + team.emit('b', 'user/message', { content: [{ type: 'text', text: 'note' }], source: { kind: 'bot', role: 'handoff', handoff: true } }) + team.emit('b', 'turn/end', { turn: 2, reason: { kind: 'completed' } }) + assert.notEqual(await team.pending('b'), undefined) +}) + +test('a card followed by a Main Bot void leaves nothing pending, and the Bot is told', async (t) => { + const team = await start() + t.after(team.stop) + await turn(team, 'b', 1, () => team.tool('ask_user', card, 'b')) + const said = await team.tool('void_question', { bot: 'Writer', reason: 'Steve changed his mind' }, 'a') + assert.match(said, /Writer/) + assert.equal(await team.pending('b'), undefined) + const [kind, message] = team.agentOf('b').calls.at(-1) + assert.equal(kind, 'followup') + assert.equal(message.source.role, 'voided') + const text = message.content.map(block => block.text).join('\n') + assert.match(text, /Question voided by Chief/) + assert.match(text, /Which one\?/) + assert.match(text, /Steve changed his mind/) +}) + +test('voiding belongs to a Main Bot', async (t) => { + const team = await start() + t.after(team.stop) + await turn(team, 'b', 1, () => team.tool('ask_user', card, 'b')) + await assert.rejects(team.tool('void_question', { bot: 'Writer' }, 'c'), { message: 'Only a Main Bot can void a question.' }) + assert.notEqual(await team.pending('b'), undefined) +}) + +test('voiding says so when the Bot has no open question', async (t) => { + const team = await start() + t.after(team.stop) + await assert.rejects(team.tool('void_question', { bot: 'Writer' }, 'a'), { message: 'Writer has no open question.' }) + await assert.rejects(team.tool('void_question', { bot: 'Nobody' }, 'a'), /No Bot named Nobody/) +}) + +test('a void reaches the card even when telling the Bot fails', async (t) => { + const team = await start() + t.after(team.stop) + await turn(team, 'b', 1, () => team.tool('ask_user', card, 'b')) + team.agentOf('b').followup = () => { throw new Error('Session gone') } + const said = await team.tool('void_question', { bot: 'Writer' }, 'a') + assert.match(said, /could not be told/) + assert.equal(await team.pending('b'), undefined) +}) + +test('a late answer to a voided card is refused and reaches nobody', async (t) => { + const team = await start() + t.after(team.stop) + await turn(team, 'b', 1, () => team.tool('ask_user', card, 'b')) + const { id } = await team.pending('b') + await team.tool('void_question', { bot: 'Writer' }, 'a') + const before = team.agentOf('b').calls.length + const late = await team.call('answer-question', { sessionId: 'b', questionId: id, answer: { selected: ['Left'] } }) + assert.equal(late.ok, false) + assert.equal(late.error.code, 'bot/stale') + assert.equal(team.agentOf('b').calls.length, before) +}) + +test('a late answer to a card a plain turn cleared is refused and reaches nobody', async (t) => { + const team = await start() + t.after(team.stop) + await turn(team, 'b', 1, () => team.tool('ask_user', card, 'b')) + const { id } = await team.pending('b') + await turn(team, 'b', 2) + const late = await team.call('answer-question', { sessionId: 'b', questionId: id, answer: { selected: ['Left'] } }) + assert.equal(late.ok, false) + assert.equal(late.error.code, 'bot/stale') + assert.deepEqual(team.agentOf('b').calls, []) +}) + +test('a normal card answered by the user behaves as it always did', async (t) => { + const team = await start() + t.after(team.stop) + await turn(team, 'b', 1, () => team.tool('ask_user', card, 'b')) + const { id } = await team.pending('b') + const answered = await team.call('answer-question', { sessionId: 'b', questionId: id, answer: { selected: ['Left'] } }) + assert.equal(answered.ok, true) + assert.equal(await team.pending('b'), undefined) + const [kind, message] = team.agentOf('b').calls.at(-1) + assert.equal(kind, 'followup') + assert.equal(message.source.kind, 'user-question-reply') + const twice = await team.call('answer-question', { sessionId: 'b', questionId: id, answer: { selected: ['Left'] } }) + assert.equal(twice.error.code, 'bot/stale') +}) diff --git a/ds-bot/test/waiting-sent.test.js b/ds-bot/test/waiting-sent.test.js new file mode 100644 index 0000000000..ed050491b5 --- /dev/null +++ b/ds-bot/test/waiting-sent.test.js @@ -0,0 +1,202 @@ +// A Bot that sends work from its own chat with the user waits for the answer, though no +// work of its own holds that wait: its tile and its status say so until the answer comes. +import assert from 'node:assert/strict' +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { test } from 'node:test' +import { createRuntime } from '../index.js' +import { fakeContext } from './fake-ctx.js' + +function team() { + return { + 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: 'Scout', role: 'Find', sessionId: 'c', createdAt: 3 }, + }, + rooms: {}, + agents: {}, + exchanges: [], + exchangeTurns: {}, + questions: {}, + prefs: {}, + revision: 0, + } +} + +async function open(home) { + 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() {}, + cancel() {}, + async whenIdle() {}, + async runMaintenance(fn) { return fn(AbortSignal.timeout(60_000)) }, + inbox: { nextStep: [], nextTurn: [], remove: () => 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) + } + const rt = createRuntime(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) => rt.teamTools.find(def => def.name === name).execute(args, { agent: { id: sessionId }, callId: 'call-1', concludeTurn() {} }) + const call = async (endpoint, payload) => (await route.fetch(new Request('http://localhost/api/bot', { + method: 'POST', body: JSON.stringify({ endpoint, payload }), + }))).json() + const waiting = async () => (await call('state', {})).value.work.waiting + await tool('list_bots', {}, 'a') + return { ctx, agentOf, emit, tool, call, waiting } +} + +async function start(t) { + const home = mkdtempSync(join(tmpdir(), 'ds-bot-waiting-sent-')) + writeFileSync(join(home, 'state.json'), JSON.stringify(team())) + const hosts = [] + const host = async () => { + const opened = await open(home) + hosts.push(opened) + return opened + } + t.after(async () => { + for (const { ctx } of hosts) await ctx.dispose() + rmSync(home, { recursive: true, force: true, maxRetries: 5 }) + }) + return { host, close: () => hosts.at(-1).ctx.dispose() } +} + +// The recipient takes the request that delivery really handed over, and may answer it. +function takes(host, id, number) { + const [, message] = host.agentOf(id).calls.at(-1) + host.emit(id, 'turn/start', { turn: number }) + host.emit(id, 'user/message', { content: message.content, source: message.source }) +} +function answers(host, id, number, text = 'Done.') { + host.emit(id, 'assistant/message', { message: { content: [{ type: 'text', text }] } }) + host.emit(id, 'turn/end', { turn: number, reason: { kind: 'completed' } }) +} + +const waitingFor = (...bots) => ({ bots, rooms: [], for: [] }) + +test('a Bot that sends from its own chat waits for the recipient until the reply', async (t) => { + const { host } = await start(t) + const team = await host() + assert.deepEqual(await team.waiting(), {}) + await team.tool('message_bot', { to: 'Writer', message: 'write the docs' }, 'a') + assert.deepEqual((await team.waiting()).a, waitingFor('b')) + takes(team, 'b', 1) + assert.deepEqual((await team.waiting()).a, waitingFor('b')) + answers(team, 'b', 1) + assert.deepEqual(await team.waiting(), {}) +}) + +test('the wait shows as the waiting state and in what other Bots read of it', async (t) => { + const { host } = await start(t) + const team = await host() + await team.tool('message_bot', { to: 'Writer', message: 'write the docs' }, 'a') + assert.equal((await team.call('state', {})).value.activity.a, 'waiting') + assert.match(await team.tool('list_bots', {}, 'c'), /Chief[\s\S]*now: waiting for Writer to answer/) +}) + +test('two sends wait for both, and the wait for one ends with its reply', async (t) => { + const { host } = await start(t) + const team = await host() + await team.tool('message_bot', { to: 'Writer', message: 'write the docs' }, 'a') + await team.tool('message_bot', { to: 'Scout', message: 'find the docs' }, 'a') + assert.deepEqual((await team.waiting()).a, waitingFor('b', 'c')) + takes(team, 'c', 1) + answers(team, 'c', 1) + assert.deepEqual((await team.waiting()).a, waitingFor('b')) +}) + +test('reply_to user is no wait', async (t) => { + const { host } = await start(t) + const team = await host() + await team.tool('message_bot', { to: 'Writer', message: 'tell the user', reply_to: 'user' }, 'a') + assert.deepEqual(await team.waiting(), {}) +}) + +test('a restart keeps the wait', async (t) => { + const { host, close } = await start(t) + const first = await host() + await first.tool('message_bot', { to: 'Writer', message: 'write the docs' }, 'a') + await close() + const second = await host() + assert.deepEqual((await second.waiting()).a, waitingFor('b')) +}) + +test('deleting the recipient ends the wait', async (t) => { + const { host } = await start(t) + const team = await host() + await team.tool('message_bot', { to: 'Writer', message: 'write the docs' }, 'a') + assert.equal((await team.call('delete-bot', { id: 'b' })).ok, true) + assert.deepEqual(await team.waiting(), {}) +}) + +test('deleting the sender ends the wait', async (t) => { + const { host } = await start(t) + const team = await host() + await team.tool('message_bot', { to: 'Writer', message: 'write the docs' }, 'c') + assert.deepEqual((await team.waiting()).c, waitingFor('b')) + assert.equal((await team.call('delete-bot', { id: 'c' })).ok, true) + assert.deepEqual(await team.waiting(), {}) +}) + +test('stopping the recipient ends the wait', async (t) => { + const { host } = await start(t) + const team = await host() + await team.tool('message_bot', { to: 'Writer', message: 'write the docs' }, 'a') + takes(team, 'b', 1) + await team.tool('stop_bot', { bot: 'Writer', reason: 'not needed' }, 'a') + team.emit('b', 'turn/end', { turn: 1, reason: { kind: 'aborted' } }) + assert.deepEqual(await team.waiting(), {}) +}) + +test('work handed on inside work waits under the Bot doing it, not on its own', async (t) => { + const { host } = await start(t) + const team = await host() + await team.tool('message_bot', { to: 'Writer', message: 'write the docs' }, 'a') + takes(team, 'b', 1) + await team.tool('message_bot', { to: 'Scout', message: 'find the sources' }, 'b') + answers(team, 'b', 1, 'Asked Scout.') + assert.deepEqual(await team.waiting(), { a: waitingFor('b'), b: { bots: ['c'], rooms: [], for: ['a'] } }) +})