Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ds-bot/src/host/delivery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand All @@ -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 } : {}),
Expand Down Expand Up @@ -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 }
Expand Down
11 changes: 9 additions & 2 deletions ds-bot/src/host/turns.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ const PROGRAM_TOOL = 'run_code'

const RETRY_TEXT = 'Your last reply wrote a tool call as plain text (<tool_call>…), so it did not run and nothing happened. Call the tool through the tool interface now, then reply as you meant to.'

// 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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -657,12 +663,13 @@ export function install(rt) {
fromId: request.info.senderSessionId,
toId: rt.selfOf(id),
sessionId: id,
text: request.text,
text: HANDED_BACK,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve request bodies across Session switches

When a joined request crosses a Session switch, the destination Session is not guaranteed to contain its body: parts.js clips handoff history, and its job.bare recovery explicitly carries no previous messages (parts.js:894). In that recovery path, the full request enters the new Session only through handoffSource().joined metadata, which claimTrigger() restores for routing but never adds to model-visible content. Replacing request.text here therefore removes the delivery that previously supplied the pending instructions, leaving the answering turn with a false claim that they are above in this Session. Keep the stored body, or supply a concrete old-part lookup anchor, when the original delivery is absent from the destination context.

AGENTS.md reference: AGENTS.md:L14-L14

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Retain the request body when joined work crosses a part switch. handoffSource().joined carries pending instructions into the fresh part as source metadata, but claimTrigger only restores it for routing. In bare recovery (parts.js, readLines), no previous message body is carried into model-visible content. A reproduced handoff with joined: [{info, text: 'Build the unique pending report with chart Z.'}] therefore sends only this pointer at turn end; the quoted instruction is never given to that model. Gate the pointer on the original context still being present, otherwise replay the stored body or provide an exact recoverable old-part anchor.

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
Expand Down
22 changes: 14 additions & 8 deletions ds-bot/test/bot-messages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down Expand Up @@ -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)
Expand All @@ -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) => {
Expand Down Expand Up @@ -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()
Expand All @@ -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.
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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, {})
})

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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')
Expand Down
5 changes: 4 additions & 1 deletion ds-bot/test/group-work.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading