From c428983761761f1b852fdfff65d7c15319aef9c0 Mon Sep 17 00:00:00 2001 From: neotechnet Date: Sat, 31 Oct 2026 20:17:24 -0400 Subject: [PATCH] fix: hand a message back as a pointer, not the body again A message that lands while a Bot is mid-work is delivered twice: the running turn reads it at its next step, then turn/end hands it back so the Bot can answer it in a turn of its own. Both hand-back loops re-sent the whole text (request.text in the joined loop, result.text in the later loop), and both of those come from a user/message event the model has already been shown in that Session. So the model read one message twice, under one exchangeId, with nothing anywhere to catch it. The hand-back keeps its identity: same exchangeId, same role, same sender, still deferred, so the sender still gets its answer and the ledger still records one send. It now carries a one-line pointer instead of the body. deliver takes a new handedBack flag and stamps source.handedBack on the delivery. exchangeText is the one reader that needs to tell the two apart, so it skips a delivery marked handedBack when it reads a message back from the Session for the exchanges dialog, which still shows the real text. test/handback.test.js covers a joined request, a reply handed back for other work, one body across the steer and the hand-back together, and a message to an idle Bot left untouched. Three of its four cases fail on the unpatched tree. Five existing assertions in test/bot-messages.test.js and test/group-work.test.js compared a hand-back against the full body; they now compare it against the pointer. The "name with brackets" case no longer had a body to check, so it proves what still holds: the bracketed sender name leaves a well-formed header. Co-Authored-By: Claude Code --- ds-bot/src/host/delivery.js | 9 +- ds-bot/src/host/turns.js | 11 +- ds-bot/test/bot-messages.test.js | 22 +-- ds-bot/test/group-work.test.js | 5 +- ds-bot/test/handback.test.js | 229 +++++++++++++++++++++++++++++++ 5 files changed, 264 insertions(+), 12 deletions(-) create mode 100644 ds-bot/test/handback.test.js diff --git a/ds-bot/src/host/delivery.js b/ds-bot/src/host/delivery.js index 7d30008dda..f198979bd9 100644 --- a/ds-bot/src/host/delivery.js +++ b/ds-bot/src/host/delivery.js @@ -51,7 +51,10 @@ export function install(rt) { // `length`: the whole message's 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}`) @@ -71,6 +74,7 @@ export function install(rt) { ...(roomId ? { roomId } : {}), ...(audience === 'user' ? { audience } : {}), ...(deferred ? { deferred: true } : {}), + ...(handedBack ? { handedBack: true } : {}), ...(resumed ? { resumed: true } : {}), ...(retried ? { retried: true } : {}), ...(askedBy ? { askedBy } : {}), @@ -114,6 +118,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/turns.js b/ds-bot/src/host/turns.js index a7106b52f4..e2bb6769c2 100644 --- a/ds-bot/src/host/turns.js +++ b/ds-bot/src/host/turns.js @@ -65,6 +65,11 @@ const PROGRAM_TOOL = 'run_code' const RETRY_TEXT = 'Your last reply wrote a tool call as plain text (…), so it did not run and nothing happened. Call the tool through the tool interface now, then reply as you meant to.' +// 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 @@ -565,13 +570,14 @@ export function install(rt) { 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 @@ -657,12 +663,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 717f034289..f8eca48b4c 100644 --- a/ds-bot/test/bot-messages.test.js +++ b/ds-bot/test/bot-messages.test.js @@ -181,7 +181,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']) @@ -226,7 +226,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) @@ -244,7 +244,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) => { @@ -365,6 +368,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() @@ -391,7 +396,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. @@ -412,7 +418,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', () => { @@ -464,7 +470,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, {}) }) @@ -509,7 +515,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. @@ -657,7 +663,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 d70f1a4fed..098152f124 100644 --- a/ds-bot/test/group-work.test.js +++ b/ds-bot/test/group-work.test.js @@ -369,7 +369,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..5c5977680c --- /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 { apply } 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 () => {} }, + }) + apply(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) => ctx.registeredTools.get(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) +})