diff --git a/docs/plans/2026-11-04-im.md b/docs/plans/2026-11-04-im.md index 71a1ddecf..f7bae064a 100644 --- a/docs/plans/2026-11-04-im.md +++ b/docs/plans/2026-11-04-im.md @@ -56,10 +56,10 @@ ### 什么发回聊天软件 - 回复只发给发起本轮消息的 IM 连接和会话;同一 Bot 绑定的其他聊天不接收。 -- 网页、手机 App、Bot 之间的消息、定时任务及来源不明的轮次不向 IM 外发。 -- 同一轮混入其他来源后,不再向 IM 外发;后续消息不能把混合轮次重新认领。 -- 群成员回复跟随发起这轮群工作的来源;系统提示和事件行不发。 -- 提问、审批、密钥提示、视图卡沿用同一来源规则。通过 IM 回答提问卡的续答,按被接收的卡片答案回到该 IM 会话;网页和手机答卡不继承 IM 来源。 +- 没有认领 IM 来源的轮次不向 IM 外发;网页、手机 App、Bot 之间的消息和定时任务本身不认领 IM 来源。 +- 一轮认领的 IM 来源保持到整轮结束;其他来源在 IM 消息前后加入都不撤回或阻止认领。不同 IM 连接、会话或发送者的认领发生冲突时,停止外发,本轮不能重新认领;未登记的 IM 请求仍不外发。 +- 群成员回复跟随发起这轮群工作的来源;网页、手机新开一轮不继承上轮 IM 来源,遗留的后台任务不算仍在进行的群轮次。没有 IM 来源的群任务报告留在 App;系统提示和事件行不发。 +- 提问、审批、密钥提示、视图卡沿用同一来源规则。通过 IM 回答提问卡的续答,按被接收的卡片答案回到该 IM 会话;已有 IM 来源的轮次中,网页和手机答卡后的回复仍回到该 IM 会话。 - 回复按平台格式发送;超过平台单条限制的拆成几条,按顺序发。 ### 卡片 diff --git a/ds-bot/src/host/im/index.js b/ds-bot/src/host/im/index.js index 4855837ea..681999814 100644 --- a/ds-bot/src/host/im/index.js +++ b/ds-bot/src/host/im/index.js @@ -8,7 +8,7 @@ import { join } from 'node:path' import { replaceFile } from '../files.js' import { viewCardText } from '../parts.js' import { imPlatform, IM_PLATFORMS } from '../../shared/im-platforms.js' -import { REPLY_KIND, SOURCE_KIND } from '../constants.js' +import { SOURCE_KIND } from '../constants.js' import { extractLocalMarkdownImages, prepareOutboundReply } from './outbound-images.js' import { adapters } from './platforms/index.js' @@ -547,9 +547,9 @@ export function install(rt) { // turn starts — the Web UI, a peer Bot's message, a scheduled task, work picked up // after a restart — has no IM origin, and its replies, views and cards stay in the app. // Nothing is persisted: after a restart there is no origin, so nothing goes out. A - // Bot's origin lives until its turn ends or another source joins it, however long the + // Bot's origin lives until its turn ends or a conflicting IM source joins it, however long the // turn runs; a group's own Session ends its relay turn at once while its members keep - // working, so its origin lives until another source writes in the group or the user + // working, so its origin lives until a new round starts in the group or the user // is unpaired. A request lives until the message it names is admitted, however long // that message waits in the queue behind a busy Bot. const paired = (conn, senderId) => (conn.paired ?? []).some(entry => entry.id === senderId) @@ -635,11 +635,14 @@ export function install(rt) { } origins.set(sessionId, { connId, chatKey, senderId, binding: bound, at: Date.now() }) } + const forgetHeldOrigins = (sessionId) => { + for (const [token, held] of resumedOrigins) if (held.activeOn === sessionId) resumedOrigins.delete(token) + } 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) + forgetHeldOrigins(sessionId) } const resetOrigin = (sessionId) => { origins.delete(sessionId) @@ -1696,7 +1699,9 @@ export function install(rt) { if (room === undefined) { resetOrigin(session.id) } else { - const working = rt.roomActive?.(room.id) || (rt.roomWorkView?.()[room.id] ?? []).length > 0 + const working = rt.roomActive?.(room.id) + || [...rt.groupOwners].some(([id, owner]) => owner.roomId === room.id && rt.live.get(id)?.turn !== undefined) + || [...rt.switchJobs()].some(job => job.roomId === room.id) // A group's own Session can start a turn for the last member's late result after // its relay turn ended, when no work looks active. Keep the origin until the // turn's first message says whether it continues the work the chat asked for. @@ -1716,7 +1721,10 @@ export function install(rt) { // A late member result, part-switch handoff, or task report continues the work the chat asked for; // any other first message starts a fresh turn, which clears the previous turn's origin. const isTaskReport = source?.kind === SOURCE_KIND && source.role === 'task-report' && typeof source.taskId === 'string' - if (pendingReset.delete(session.id) && !(typeof imOrigin === 'string' || isTaskReport || (source?.kind === SOURCE_KIND && source.role === 'post'))) resetOrigin(session.id) + if (pendingReset.delete(session.id) && !(typeof imOrigin === 'string' || isTaskReport || (source?.kind === SOURCE_KIND && source.role === 'post'))) { + forgetHeldOrigins(session.id) + resetOrigin(session.id) + } if (typeof imOrigin === 'string') { const resumed = resumedOrigins.get(imOrigin) resumedOrigins.delete(imOrigin) @@ -1733,26 +1741,20 @@ export function install(rt) { const roomSid = room === undefined ? undefined : rt.sessionOfRoom(room) if (roomSid !== undefined && originFromSaved(saved) !== undefined) { const live = origins.get(roomSid) - const memberId = rt.groupOwners.get(session.id)?.botId const activeRound = rt.roomActive?.(room.id) === true - const otherWorking = (rt.roomWorkView?.()[room.id] ?? []).some(item => item.botId !== memberId) - if (live === undefined && !activeRound && !(mixed.has(roomSid) && otherWorking)) { - // Restore after restart even while sibling group tasks run, and clear stale - // `mixed` left by an already-finished desktop/Web round; stay blocked while - // a live non-IM round is active or a mixed round still has working members. - resetOrigin(roomSid) + if (live === undefined && !activeRound && !mixed.has(roomSid)) { + // Restore after restart even while sibling tasks run, but never reopen + // an active non-IM round or a round blocked by conflicting IM claims. claimOrigin(roomSid, saved.connId, saved.chatKey, saved.senderId, saved.binding) } else if (live !== undefined && (live.connId !== saved.connId || live.chatKey !== saved.chatKey)) { // A different IM chat currently owns the room; mix so neither chat sees the other's report. dropOrigin(roomSid) } - } else if (roomSid !== undefined) { - // A desktop/Web-started group task has no IM origin: drop any restored room - // origin so its report is never forwarded to an IM group chat. + } else if (roomSid !== undefined && (saved !== undefined || origins.has(roomSid))) { dropOrigin(roomSid) } } else if (originFromSaved(saved) === undefined) { - dropOrigin(session.id) + if (saved !== undefined) dropOrigin(session.id) } else { claimOrigin(session.id, saved.connId, saved.chatKey, saved.senderId, saved.binding) } @@ -1768,16 +1770,8 @@ export function install(rt) { 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) - } else if (source?.kind === SOURCE_KIND && source.role === 'post') { - // A member's late result relayed into the group's own Session. The group's work - // is still the answer to the chat that asked, wherever the member runs. } else { - // A message from the Web UI, a peer Bot or a scheduled task: this turn is not an - // IM chat's to answer, even when an earlier turn of this Session was. - dropOrigin(session.id) + // Non-IM input can join the turn, but cannot claim or withdraw its IM chat. } } } diff --git a/ds-bot/test/im.test.js b/ds-bot/test/im.test.js index 03940052e..8b08c0404 100644 --- a/ds-bot/test/im.test.js +++ b/ds-bot/test/im.test.js @@ -1008,7 +1008,7 @@ test('reply origin: a peer Bot message and a scheduled task do not reach IM', as assert.equal(s.fake.sent.length, 0) }) -test('reply origin: another source in the same turn withdraws the IM origin', async (t) => { +test('reply origin: another source in the same turn keeps the IM origin', async (t) => { const s = await start() t.after(s.stop) await pairedConn(s) @@ -1017,7 +1017,140 @@ test('reply origin: another source in the same turn withdraws the IM origin', as s.emitEvent('a', 'user/message', { source: { kind: 'peer', from: 'b' }, content: [{ type: 'text', text: 'also this' }] }) s.emitEvent('a', 'assistant/message', { message: { content: [{ type: 'text', text: 'reply' }] } }) await s.flush() - assert.equal(s.fake.sent.length, 0, 'a mixed turn sends nothing') + assert.deepEqual(s.fake.sent.map(entry => [entry.chat.key, entry.text]), [['dm-u1', 'reply']]) +}) + +for (const group of [false, true]) { + for (const before of [false, true]) { + test(`whole-turn IM origin: card answer ${before ? 'before' : 'after'} IM admission in a ${group ? 'group' : 'direct'} turn`, async (t) => { + const s = await start() + t.after(s.stop) + const { one } = await pairedConn(s) + if (group) { + one.chats.g1 = { kind: 'group', title: 'G', target: { roomId: 'r1' }, at: 1 } + await s.rt.save() + await gm(s, 'u1', 'hello group') + } else { + await dm(s, 'u1', 'hi') + } + const sessionId = group ? 'r1' : 'a' + const admit = () => s.emitEvent(sessionId, 'user/message', { source: { kind: 'user', rpcId: s.prompts.at(-1).requestId } }) + const answer = () => s.emitEvent(sessionId, 'user/message', { source: { kind: 'user-question-reply', callId: 'c1', outcome: 'answered' } }) + s.emitEvent(sessionId, 'turn/start', {}) + if (before) { answer(); admit() } else { admit(); answer() } + s.fake.sent.length = 0 + if (group) s.rt.logRoom(s.rt.roomOf('r1'), { who: 'Writer', botId: 'b', text: 'reply' }) + else s.emitEvent(sessionId, 'assistant/message', { message: { content: [{ type: 'text', text: 'reply' }] } }) + await s.flush() + assert.deepEqual(s.fake.sent.map(entry => [entry.chat.key, entry.text]), [[group ? 'g1' : 'dm-u1', group ? '**Writer**: reply' : 'reply']]) + }) + } + + test(`whole-turn IM origin: every reply survives mid-turn non-IM input in a ${group ? 'group' : 'direct'} turn`, async (t) => { + const s = await start() + t.after(s.stop) + const { one } = await pairedConn(s) + if (group) { + one.chats.g1 = { kind: 'group', title: 'G', target: { roomId: 'r1' }, at: 1 } + await s.rt.save() + await gm(s, 'u1', 'hello group') + } else { + await dm(s, 'u1', 'hi') + } + const sessionId = group ? 'r1' : 'a' + s.imTurn(sessionId) + s.fake.sent.length = 0 + const replies = [] + const reply = () => { + const text = `reply ${replies.length + 1}` + replies.push([group ? 'g1' : 'dm-u1', group ? `**Writer**: ${text}` : text]) + if (group) s.rt.logRoom(s.rt.roomOf('r1'), { who: 'Writer', botId: 'b', text }) + else s.emitEvent(sessionId, 'assistant/message', { message: { content: [{ type: 'text', text }] } }) + } + reply() + for (const source of [ + { kind: 'user' }, + { kind: 'web' }, + { kind: 'phone' }, + { kind: 'schedule', taskId: 't1' }, + { kind: 'peer', from: 'b' }, + { kind: 'bot', role: 'post' }, + ...(!group ? [{ kind: 'bot', role: 'task-report', taskId: 'desktop-task' }] : []), + undefined, + ]) { + s.emitEvent(sessionId, 'user/message', { source }) + reply() + reply() + } + await s.flush() + assert.deepEqual(s.fake.sent.map(entry => [entry.chat.key, entry.text]), replies) + }) +} + +for (const working of [true, false]) test(`desktop round after IM group round, member work=${working}`, 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') + s.imTurn('r1') + s.emitEvent('r1', 'turn/end', {}) + s.rt.roomActive = () => false + s.rt.roomWorkView = () => (working ? { r1: [{ botId: 'b', since: Date.now(), tool: 'task' }] } : {}) + s.fake.sent.length = 0 + s.emitEvent('r1', 'turn/start', {}) + s.emitEvent('r1', 'user/message', { source: { kind: 'user' }, content: [{ type: 'text', text: 'private desktop question' }] }) + s.rt.logRoom(s.rt.roomOf('r1'), { who: 'Writer', botId: 'b', text: 'private desktop answer' }) + await s.flush() + assert.deepEqual(s.fake.sent, []) + await gm(s, 'u1', 'later IM request') + s.emitEvent('r1', 'user/message', { source: { kind: 'user', rpcId: s.prompts.at(-1).requestId } }) + s.rt.logRoom(s.rt.roomOf('r1'), { who: 'Writer', botId: 'b', text: 'IM answer' }) + await s.flush() + assert.deepEqual(s.fake.sent.map(entry => [entry.chat.key, entry.text]), [['g1', '**Writer**: IM answer']]) +}) + +test('a desktop task report before an IM group message does not poison the active round', 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 } + s.rt.botOf('a').tasks = [{ id: 'desktop-task', sessionId: 'desktop-task-session', roomId: 'r1', status: 'completed' }] + await s.rt.save() + s.rt.roomActive = () => true + s.emitEvent('r1', 'turn/start', {}) + s.emitEvent('r1', 'user/message', { source: { kind: 'bot', role: 'task-report', taskId: 'desktop-task', roomId: 'r1' } }) + s.rt.logRoom(s.rt.roomOf('r1'), { who: 'Writer', botId: 'b', text: 'desktop report' }) + await s.flush() + assert.deepEqual(s.fake.sent, []) + await gm(s, 'u1', 'joining IM message') + s.emitEvent('r1', 'user/message', { source: { kind: 'user', rpcId: s.prompts.at(-1).requestId } }) + s.rt.logRoom(s.rt.roomOf('r1'), { who: 'Writer', botId: 'b', text: 'IM reply' }) + await s.flush() + assert.deepEqual(s.fake.sent.map(entry => [entry.chat.key, entry.text]), [['g1', '**Writer**: IM reply']]) +}) + +for (const active of ['round', 'member']) test(`Web input joining an ongoing group ${active} keeps the IM origin`, 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') + s.imTurn('r1') + s.emitEvent('r1', 'turn/end', {}) + s.rt.roomActive = () => active === 'round' + if (active === 'member') { + s.rt.groupOwners.set('member-session', { roomId: 'r1', botId: 'b' }) + s.emitEvent('member-session', 'turn/start', { turn: 1 }) + } + s.fake.sent.length = 0 + s.emitEvent('r1', 'turn/start', {}) + s.emitEvent('r1', 'user/message', { source: { kind: 'user' }, content: [{ type: 'text', text: 'Web steer' }] }) + s.rt.logRoom(s.rt.roomOf('r1'), { who: 'Writer', botId: 'b', text: 'continued answer' }) + await s.flush() + assert.deepEqual(s.fake.sent.map(entry => [entry.chat.key, entry.text]), [['g1', '**Writer**: continued answer']]) }) test('reply origin: the turn ending stops later output from going out', async (t) => { @@ -1043,6 +1176,87 @@ test('reply origin: an IM message this process never registered sends nothing', assert.equal(s.fake.sent.length, 0) }) +for (const group of [false, true]) { + test(`whole-turn IM origin: an unknown IM request blocks later claims in a ${group ? 'group' : 'direct'} turn`, async (t) => { + const s = await start() + t.after(s.stop) + const { one } = await pairedConn(s) + if (group) { + one.chats.g1 = { kind: 'group', title: 'G', target: { roomId: 'r1' }, at: 1 } + await s.rt.save() + await gm(s, 'u1', 'first') + } else { + await dm(s, 'u1', 'first') + } + const sessionId = group ? 'r1' : 'a' + s.imTurn(sessionId) + s.emitEvent(sessionId, 'user/message', { source: { kind: 'user', rpcId: 'im:telegram:unknown' } }) + if (group) await gm(s, 'u1', 'second') + else await dm(s, 'u1', 'second') + s.emitEvent(sessionId, 'user/message', { source: { kind: 'user', rpcId: s.prompts.at(-1).requestId } }) + s.fake.sent.length = 0 + if (group) s.rt.logRoom(s.rt.roomOf('r1'), { who: 'Writer', botId: 'b', text: 'withheld' }) + else s.emitEvent(sessionId, 'assistant/message', { message: { content: [{ type: 'text', text: 'withheld' }] } }) + await s.flush() + assert.deepEqual(s.fake.sent, []) + }) +} + +for (const conflict of ['connection', 'chat', 'sender']) { + test(`whole-turn IM origin: a conflicting ${conflict} cannot be reclaimed by non-IM input`, async (t) => { + const s = await start() + t.after(s.stop) + const { one } = await pairedConn(s) + one.paired.push({ id: 'u2', name: 'U2', at: 1 }) + await s.rt.save() + let incoming = s.fake + if (conflict === 'connection') { + incoming = fakeAdapter() + const original = adapters.dingtalk + adapters.dingtalk = incoming + t.after(() => { adapters.dingtalk = original }) + const conn = await s.rt.imEndpoint('im-add', { platform: 'dingtalk', botId: 'a', fields: FIELDS.dingtalk }) + s.rt.state.im.connections[conn.id].paired.push({ id: 'u1', name: 'U1', at: 1 }) + await s.rt.save() + await s.flush() + } + await dm(s, 'u1', 'first') + s.imTurn('a') + await incoming.handlers.onMessage({ + chat: { key: conflict === 'chat' ? 'dm-other' : 'dm-u1', kind: 'direct', title: 'Chat' }, + sender: { id: conflict === 'sender' ? 'u2' : 'u1', name: 'User' }, text: 'conflicting request', + }) + s.emitEvent('a', 'user/message', { source: { kind: 'user', rpcId: s.prompts.at(-1).requestId } }) + s.emitEvent('a', 'user/message', { source: { kind: 'user-question-reply', callId: 'c1', outcome: 'answered' } }) + await dm(s, 'u1', 'try to reclaim') + s.emitEvent('a', 'user/message', { source: { kind: 'user', rpcId: s.prompts.at(-1).requestId } }) + s.fake.sent.length = 0 + incoming.sent.length = 0 + s.emitEvent('a', 'assistant/message', { message: { content: [{ type: 'text', text: 'withheld' }] } }) + await s.flush() + assert.deepEqual(s.fake.sent, []) + assert.deepEqual(incoming.sent, []) + }) +} + +test('whole-turn IM origin: a task report cannot reopen a conflicting group round', async (t) => { + const s = await start() + t.after(s.stop) + const { one } = await pairedConn(s) + for (const chatKey of ['g1', 'g2']) one.chats[chatKey] = { kind: 'group', title: chatKey, target: { roomId: 'r1' }, at: 1 } + await s.rt.save() + await gm(s, 'u1', 'first') + s.imTurn('r1') + s.rt.botOf('a').tasks = [{ id: 'im-task', sessionId: 'task-1', status: 'completed', imOrigin: s.rt.captureImOrigin('r1') }] + await gm(s, 'u1', 'conflicting', { chat: { key: 'g2', kind: 'group', title: 'G2' } }) + s.emitEvent('r1', 'user/message', { source: { kind: 'user', rpcId: s.prompts.at(-1).requestId } }) + s.emitEvent('r1', 'user/message', { source: { kind: 'bot', role: 'task-report', taskId: 'im-task', roomId: 'r1' } }) + s.fake.sent.length = 0 + s.rt.logRoom(s.rt.roomOf('r1'), { who: 'Writer', botId: 'b', text: 'withheld' }) + await s.flush() + assert.deepEqual(s.fake.sent, []) +}) + 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) @@ -1084,7 +1298,7 @@ test('reply origin: withheld mixed output warns once per turn without message co 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' } }) + s.emitEvent('a', 'user/message', { source: { kind: 'user', rpcId: 'im:telegram:unknown' } }) 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++) { @@ -1137,7 +1351,7 @@ test('reply origin: group redelivery keeps the chat and mixed group output warns 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' } }) + s.emitEvent('r1', 'user/message', { source: { kind: 'user', rpcId: 'im:telegram:unknown' } }) 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) @@ -1453,6 +1667,8 @@ test('a Web question answer cannot inherit a previous IM destination', async t = await pairedConn(s) await dm(s, 'u1', 'first') s.imTurn('a') + s.emitEvent('a', 'turn/end', {}) + s.emitEvent('a', 'turn/start', {}) s.emitEvent('a', 'user/message', { source: { kind: 'user-question-reply', callId: 'c1', outcome: 'answered' } }) s.fake.sent.length = 0 s.emitEvent('a', 'assistant/message', { message: { content: [{ type: 'text', text: 'private answer' }] } }) @@ -1852,7 +2068,7 @@ test('reply origin: overflow switch keeps IM origin, mixed turns stay isolated, await delay(30) assert.deepEqual(fake.sent.map(job => [job.chat.key, job.text]), [['dm-u1', 'recovered after overflow']]) - // 2. A turn already joined by Web UI input before switching does NOT carry the IM origin across parts. + // 2. Web UI input joining the turn before switching keeps the IM origin across parts. await fake.handlers.onMessage({ chat: { key: 'dm-u1', kind: 'direct', title: 'U1' }, sender: { id: 'u1', name: 'U1' }, text: 'mixed task' }) const req2 = prompts.at(-1)?.requestId fake.sent.length = 0 @@ -1869,12 +2085,14 @@ test('reply origin: overflow switch keeps IM origin, mixed turns stay isolated, assert.ok(handoff3) emitEvent(part3, 'turn/start', { turn: 1 }) emitEvent(part3, 'user/message', { source: handoff3.source, content: handoff3.content }) - emitEvent(part3, 'assistant/message', { turn: 1, message: { content: [{ type: 'text', text: 'must stay internal' }] } }) + emitEvent(part3, 'assistant/message', { turn: 1, message: { content: [{ type: 'text', text: 'reply after web steer' }] } }) emitEvent(part3, 'turn/end', { turn: 1, reason: { kind: 'completed' } }) await delay(30) - assert.deepEqual(fake.sent, []) + assert.deepEqual(fake.sent.map(job => [job.chat.key, job.text]), [['dm-u1', 'reply after web steer']]) - // 3. A turn that turns mixed after startSwitch held the origin does not leak it to the fresh part either. + // 3. A conflicting IM claim after startSwitch held the origin cannot leak to the fresh part. + rt.state.im.connections[conn.id].paired.push({ id: 'u2', name: 'U2', at: 1 }) + await rt.save() await fake.handlers.onMessage({ chat: { key: 'dm-u1', kind: 'direct', title: 'U1' }, sender: { id: 'u1', name: 'U1' }, text: 'late mixed task' }) const req3 = prompts.at(-1)?.requestId fake.sent.length = 0 @@ -1884,7 +2102,8 @@ test('reply origin: overflow switch keeps IM origin, mixed turns stay isolated, await preStep(part3, [{ content: [{ type: 'text', text: 'late mixed task' }], source: { kind: 'user', rpcId: req3 } }], 1) emitEvent(part3, 'user/message', { source: { kind: 'user', rpcId: req3 }, content: [{ type: 'text', text: 'late mixed task' }] }) await fireRequestError(part3, 'CONTEXT_WINDOW_EXCEEDED') - emitEvent(part3, 'user/message', { source: { kind: 'user' }, content: [{ type: 'text', text: 'late web steer' }] }) + await fake.handlers.onMessage({ chat: { key: 'dm-u2', kind: 'direct', title: 'U2' }, sender: { id: 'u2', name: 'U2' }, text: 'conflicting task' }) + emitEvent(part3, 'user/message', { source: { kind: 'user', rpcId: prompts.at(-1).requestId }, content: [{ type: 'text', text: 'conflicting task' }] }) emitEvent(part3, 'turn/end', { turn: 2, reason: { kind: 'error' } }) releaseIdle() setIdleGate(undefined) @@ -1930,7 +2149,7 @@ test('reply origin: overflow switch keeps IM origin, mixed turns stay isolated, await delay(30) assert.deepEqual(fake.sent.map(job => [job.chat.key, job.text]), [['g1', '**Writer**: member finished after switch']]) - // 5. Group folder switch invalidated by a Web UI message while switching does not re-attach the old IM origin. + // 5. Web UI input during the group folder switch keeps the IM origin for the ongoing work. fake.sent.length = 0 agentOf(roomPart2).session.header.cwd = '/tmp/stale-group-dir-2' let onCreateEntered @@ -1957,12 +2176,12 @@ test('reply origin: overflow switch keeps IM origin, mixed turns stay isolated, emitEvent(roomPart3, 'turn/start', { turn: 2 }) emitEvent(roomPart3, 'user/message', { source: { kind: 'bot', form: 'relay', role: 'post', exchangeId: 'e-room-web', senderName: 'Writer', senderSessionId: 'b', roomId: 'r1' }, - content: [{ type: 'text', text: 'must not leak after web takeover' }], + content: [{ type: 'text', text: 'reply after group web steer' }], }) - rt.logRoom(room, { who: 'Writer', botId: 'b', text: 'must not leak after web takeover' }) + rt.logRoom(room, { who: 'Writer', botId: 'b', text: 'reply after group web steer' }) emitEvent(roomPart3, 'turn/end', { turn: 2, reason: { kind: 'completed' } }) await delay(30) - assert.deepEqual(fake.sent, []) + assert.deepEqual(fake.sent.map(job => [job.chat.key, job.text]), [['g1', '**Writer**: reply after group web steer']]) // 6. Group folder switch invalidated by another IM chat while switching does not leak to the old IM chat. rt.state.im.connections[conn.id].chats.g2 = { kind: 'group', title: 'Group Two', target: { roomId: 'r1' }, at: 1 } @@ -2484,8 +2703,7 @@ test('P0-5/P0-B: group task-report restores persisted IM origin across restart a updatedAt: Date.now(), imOrigin: captured, }] - // Simulate a desktop/Web message in the same group (dropOrigin adds roomSid to `mixed`), - // whose round has already finished (roomActive === false). + // Start and finish a fresh desktop/Web round without an IM claim. s.emitEvent(roomSid, 'turn/start', { turn: 2 }) s.emitEvent(roomSid, 'user/message', { source: { kind: 'user' }, content: [{ type: 'text', text: 'desktop ping' }] }) s.emitEvent(roomSid, 'turn/end', { turn: 2, reason: { kind: 'completed' } }) @@ -2512,7 +2730,7 @@ test('P0-5/P0-B: group task-report restores persisted IM origin across restart a assert.deepEqual( s.fake.sent.map(entry => [entry.chat.key, entry.text]), [['g1', '**Writer**: Group task finished and verified.']], - 'group task-report must clear stale mixed from finished desktop round and deliver via real room post path', + 'group task-report must restore the origin after a finished desktop round and deliver via real room post path', ) }) @@ -2680,8 +2898,7 @@ test('P0-B: after restart, group task-report restores IM origin even while sibli assert.deepEqual(s.fake.sent, [], 'must not restore IM origin while a non-IM group round is active') s.rt.roomActive = () => false - // A desktop-started sibling task (no imOrigin) finishing right after must drop roomSid - // origin so its desktop result is NEVER forwarded to IM group g1! + // A desktop-started sibling task has no IM destination, even after an IM task report. s.rt.botOf('a').tasks.push({ id: 'k_grp_desktop', title: 'Desktop sibling task', @@ -2693,7 +2910,7 @@ test('P0-B: after restart, group task-report restores IM origin even while sibli startedAt: Date.now(), updatedAt: Date.now(), }) - // First restore g1 origin via IM task, then let desktop task report drop it. + // First restore g1 origin via IM task, then withdraw it for the desktop task report. s.emitEvent('grp-r1-a', 'turn/start', { turn: 5 }) s.emitEvent('grp-r1-a', 'user/message', { source: { kind: 'bot', form: 'relay', role: 'task-report', taskId: 'k_grp_restart', roomId: 'r1' },