Skip to content
Merged
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
36 changes: 30 additions & 6 deletions ds-bot/src/host/im/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const RETRY_MS = 5_000
const RETRY_MAX_MS = 30_000
const SEND_ATTEMPTS = 4
const TYPING_MS = 4_000
const CONSUMED_REQUESTS_KEEP = 200
const NUMBERS = /^\s*\d+(\s+\d+)*\s*$/
// A quoted reply is answered with the message it quotes: the last messages sent
// to a chat are kept by the ids the platform named them, and only their start.
Expand Down Expand Up @@ -571,6 +572,8 @@ export function install(rt) {
const pendingReset = new Set()
// Request id of an IM message → the chat it came from, until that message shows up.
const requests = new Map()
const consumedRequests = new Set()
const withheldOrigins = new Map()
// IM origin held across a part switch until the resumed/handoff message opens.
const resumedOrigins = new Map()
const holdImOrigin = (sessionId) => {
Expand Down Expand Up @@ -611,7 +614,10 @@ export function install(rt) {
}
const claimOrigin = (sessionId, connId, chatKey, senderId, binding) => {
if (sessionId === undefined || connId === undefined) return
if (mixed.has(sessionId)) return
if (mixed.has(sessionId)) {
if (!withheldOrigins.has(sessionId)) withheldOrigins.set(sessionId, false)
return
}
const previous = origins.get(sessionId)
if (previous !== undefined && (previous.connId !== connId || previous.chatKey !== chatKey || previous.senderId !== senderId)) {
dropOrigin(sessionId)
Expand All @@ -630,13 +636,20 @@ export function install(rt) {
origins.set(sessionId, { connId, chatKey, senderId, binding: bound, at: Date.now() })
}
const dropOrigin = (sessionId) => {
if (origins.has(sessionId) && !withheldOrigins.has(sessionId)) withheldOrigins.set(sessionId, false)
origins.delete(sessionId)
mixed.add(sessionId)
for (const [token, held] of resumedOrigins) if (held.activeOn === sessionId) resumedOrigins.delete(token)
}
const resetOrigin = (sessionId) => {
origins.delete(sessionId)
mixed.delete(sessionId)
withheldOrigins.delete(sessionId)
}
const warnWithheldOrigin = (sessionId) => {
if (withheldOrigins.get(sessionId) !== false) return
withheldOrigins.set(sessionId, true)
rt.warn('ds-bot: IM reply withheld: turn origin was dropped or mixed')
}
const clearImOrigin = (sessionId) => {
resetOrigin(sessionId)
Expand Down Expand Up @@ -1692,6 +1705,7 @@ export function install(rt) {
}
if (event.type === 'user/message') {
const source = event.data?.source
if (typeof source?.rpcId === 'string' && consumedRequests.has(source.rpcId)) return
// Pure context notice injected before the user's message on a newly forked foreground;
// never touch pendingReset or drop/mix the IM origin for this turn.
if (source?.kind === SOURCE_KIND && source.role === 'task-handoff') {
Expand Down Expand Up @@ -1744,12 +1758,16 @@ export function install(rt) {
}
} else if (typeof rpcId === 'string' && rpcId.startsWith('im:')) {
if (Number.isInteger(event.seq)) imVia.set(session.id, { ...imVia.get(session.id), [event.seq]: rpcId.split(':')[1] })
// The chat this message came from is the one its turn answers. A message this
// process never registered — after a restart, or a redelivery — leaves no origin.
// Unknown requests cannot claim a chat. Consumed requests are ignored above,
// including redelivery after their turn ended; they must not claim it again.
const asked = requests.get(rpcId)
requests.delete(rpcId)
if (asked === undefined) dropOrigin(session.id)
else claimOrigin(session.id, asked.connId, asked.chatKey, asked.senderId, asked.binding)
else {
consumedRequests.add(rpcId)
if (consumedRequests.size > CONSUMED_REQUESTS_KEEP) consumedRequests.delete(consumedRequests.values().next().value)
claimOrigin(session.id, asked.connId, asked.chatKey, asked.senderId, asked.binding)
}
} else if (source?.kind === REPLY_KIND) {
// Web and phone answers carry no registered IM request.
dropOrigin(session.id)
Expand Down Expand Up @@ -1782,9 +1800,12 @@ export function install(rt) {
return
}
const origin = originOf(session.id)
if (origin === undefined) return
for (const item of rt.displayItems(session.id, [event])) {
if (item.kind !== 'bot' && item.kind !== 'view') continue
if (origin === undefined) {
warnWithheldOrigin(session.id)
continue
}
const body = item.kind === 'bot' ? item.text : viewCardText({ title: item.title, view: item.view }) || (item.title ?? '')
sendReply(origin, body, { sessionId: session.id, botId })
}
Expand All @@ -1794,7 +1815,10 @@ export function install(rt) {
if (line.botId === undefined || line.kind !== undefined || typeof line.text !== 'string' || line.text.trim() === '') return
const roomSessionId = room.sessionId ?? room.id
const origin = originOf(roomSessionId)
if (origin === undefined) return
if (origin === undefined) {
warnWithheldOrigin(roomSessionId)
return
}
sendReply(origin, line.text, { sessionId: roomSessionId, botId: line.botId, prefix: `**${line.who}**: ` })
})

Expand Down
124 changes: 124 additions & 0 deletions ds-bot/test/im.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,130 @@ test('reply origin: an IM message this process never registered sends nothing',
assert.equal(s.fake.sent.length, 0)
})

test('reply origin: a consumed IM rpcId is ignored without withdrawing or claiming an origin', async (t) => {
const s = await start()
t.after(s.stop)
await pairedConn(s)
await dm(s, 'u1', 'hi')
const source = { kind: 'user', rpcId: s.prompts.at(-1).requestId }
s.imTurn('a')
s.emitEvent('a', 'user/message', { source })
s.emitEvent('a', 'assistant/message', { message: { content: [{ type: 'text', text: 'reply' }] } })
await s.flush()
assert.deepEqual(s.fake.sent.map(entry => [entry.chat.key, entry.text]), [['dm-u1', 'reply']])

s.emitEvent('a', 'turn/end', {})
s.emitEvent('a', 'turn/start', {})
s.emitEvent('a', 'user/message', { source })
s.emitEvent('a', 'assistant/message', { message: { content: [{ type: 'text', text: 'not a new IM request' }] } })
await s.flush()
assert.equal(s.fake.sent.length, 1, 'a duplicate cannot claim a later turn')

await dm(s, 'u1', 'next')
s.emitEvent('a', 'user/message', { source: { kind: 'user', rpcId: s.prompts.at(-1).requestId } })
s.emitEvent('a', 'assistant/message', { message: { content: [{ type: 'text', text: 'next reply' }] } })
await s.flush()
assert.deepEqual(s.fake.sent.map(entry => entry.text), ['reply', 'next reply'], 'a duplicate does not poison a later claim')
})

test('reply origin: withheld mixed output warns once per turn without message content', async (t) => {
const s = await start()
t.after(s.stop)
await pairedConn(s)
const warnings = []
s.rt.warn = (...args) => warnings.push(args)
s.emitEvent('a', 'turn/start', {})
s.emitEvent('a', 'user/message', { source: { kind: 'user' } })
s.emitEvent('a', 'assistant/message', { message: { content: [{ type: 'text', text: 'ordinary Web reply' }] } })
await s.flush()
assert.deepEqual(warnings, [], 'ordinary non-IM output does not warn')
for (let turn = 0; turn < 2; turn++) {
await dm(s, 'u1', 'hi')
if (turn === 0) s.imTurn('a')
else s.emitEvent('a', 'turn/start', {})
s.emitEvent('a', 'user/message', { source: { kind: 'peer', from: 'b' } })
if (turn === 1) s.emitEvent('a', 'user/message', { source: { kind: 'user', rpcId: s.prompts.at(-1).requestId } })
assert.equal(warnings.length, turn, 'no warning before output is withheld')
for (let reply = 0; reply < 2; reply++) {
s.emitEvent('a', 'assistant/message', { message: { content: [{ type: 'text', text: 'private reply body' }] } })
}
await s.flush()
assert.equal(warnings.length, turn + 1)
await dm(s, 'u1', 'another request in the mixed turn')
s.emitEvent('a', 'user/message', { source: { kind: 'user', rpcId: s.prompts.at(-1).requestId } })
s.emitEvent('a', 'assistant/message', { message: { content: [{ type: 'text', text: 'still withheld' }] } })
await s.flush()
assert.equal(warnings.length, turn + 1, 'a blocked claim cannot warn twice in one turn')
s.emitEvent('a', 'turn/end', {})
}
assert.equal(s.fake.sent.length, 0)
assert.deepEqual(warnings, [
['ds-bot: IM reply withheld: turn origin was dropped or mixed'],
['ds-bot: IM reply withheld: turn origin was dropped or mixed'],
])
})

test('reply origin: an unknown IM rpcId still withdraws a live origin and warns on output', async (t) => {
const s = await start()
t.after(s.stop)
await pairedConn(s)
await dm(s, 'u1', 'hi')
s.imTurn('a')
const warnings = []
s.rt.warn = (...args) => warnings.push(args)
s.emitEvent('a', 'user/message', { source: { kind: 'user', rpcId: 'im:telegram:unknown' } })
s.emitEvent('a', 'assistant/message', { message: { content: [{ type: 'text', text: 'withheld' }] } })
await s.flush()
assert.equal(s.fake.sent.length, 0)
assert.deepEqual(warnings, [['ds-bot: IM reply withheld: turn origin was dropped or mixed']])
})

test('reply origin: group redelivery keeps the chat and mixed group output warns once', async (t) => {
const s = await start()
t.after(s.stop)
const { one } = await pairedConn(s)
one.chats.g1 = { kind: 'group', title: 'G', target: { roomId: 'r1' }, at: 1 }
await s.rt.save()
await gm(s, 'u1', 'hello group')
const source = { kind: 'user', rpcId: s.prompts.at(-1).requestId }
s.imTurn('r1')
s.emitEvent('r1', 'user/message', { source })
s.fake.sent.length = 0
s.rt.logRoom(s.rt.roomOf('r1'), { who: 'Writer', botId: 'b', text: 'reply' })
await s.flush()
assert.deepEqual(s.fake.sent.map(entry => [entry.chat.key, entry.text]), [['g1', '**Writer**: reply']])
const warnings = []
s.rt.warn = (...args) => warnings.push(args)
s.emitEvent('r1', 'user/message', { source: { kind: 'peer', from: 'b' } })
for (let reply = 0; reply < 2; reply++) s.rt.logRoom(s.rt.roomOf('r1'), { who: 'Writer', botId: 'b', text: 'withheld' })
await s.flush()
assert.equal(s.fake.sent.length, 1)
assert.deepEqual(warnings, [['ds-bot: IM reply withheld: turn origin was dropped or mixed']])
})

test('reply origin: consumed IM rpcIds are bounded and an evicted id fails closed', async (t) => {
const s = await start()
t.after(s.stop)
await pairedConn(s)
let oldest
for (let request = 0; request <= 200; request++) {
await dm(s, 'u1', 'hi')
oldest ??= s.prompts.at(-1).requestId
s.imTurn('a')
s.emitEvent('a', 'turn/end', {})
}
await dm(s, 'u1', 'latest')
s.imTurn('a')
s.emitEvent('a', 'user/message', { source: { kind: 'user', rpcId: s.prompts.at(-2).requestId } })
s.emitEvent('a', 'assistant/message', { message: { content: [{ type: 'text', text: 'recent duplicate ignored' }] } })
await s.flush()
assert.deepEqual(s.fake.sent.map(entry => entry.text), ['recent duplicate ignored'])
s.emitEvent('a', 'user/message', { source: { kind: 'user', rpcId: oldest } })
s.emitEvent('a', 'assistant/message', { message: { content: [{ type: 'text', text: 'evicted request withheld' }] } })
await s.flush()
assert.equal(s.fake.sent.length, 1)
})

test('reply origin: the group line follows the turn that started it', async (t) => {
const s = await start()
t.after(s.stop)
Expand Down
Loading