im: reject a frame that arrives after the intake stopped

This commit is contained in:
MicroBlock committed 2026-11-12 15:57:42 +08:00
1 parent 0e0bd71085
commit 4e1b821fcb
2 files changed
+10 -8

No files matched your search

+7 -5
View File
@@ -86,7 +86,8 @@ const ignore = (): Promise<ImOutcome> => Promise.resolve({ kind: 'ignored' })
*
* Contract:
* - {@link receive} resolves only after the message record *and* the event receipt are durable; the adapter ACKs
* afterwards. A rejection (a storage failure) therefore leaves the platform event unacked and a redelivery is
* afterwards. A rejection — a storage failure, or a frame arriving after {@link dispose} — therefore leaves the
* platform event unacked and a redelivery is
* expected. A message over `maxInputBytes` is remembered and acknowledged instead: refusing it would have the
* platform redeliver a message that can never be accepted. The platform reads of a frame (a card, a merged forward,
* the text of a quoted reply) happen outside the store chain and in arrival order per chat, so a slow read delays
@@ -139,11 +140,12 @@ export class ImIntake {
* @param session - the adapter session of one received platform message.
*/
private async receiveFrame(session: Session): Promise<void> {
// A frame that arrives while this intake is shutting down is not acknowledged: the platform redelivers it to a
// process that can store it.
if (this.disposed) return
// A frame that arrives while this intake is shutting down rejects instead of returning: a listener that returns
// is an accepted frame, so the adapter would acknowledge a message this process never stored and the platform
// would never redeliver it.
if (this.disposed) throw new ImError('unavailable', 'The IM intake is shutting down; this frame was not stored.')
if (session.type !== 'message' || session.message === undefined) return
const message = session.message
if (session.type !== 'message' || message === undefined) return
const bot = session.bot
// Some platforms echo the bot's own messages back; those are recorded through `outbound`, not intake.
if (message.sender.id === bot.selfId) return
@@ -188,11 +188,11 @@ describe('ImIntake admission', () => {
expect(store.get(ImMessageKey('fake:bot', 'm-slow'))?.text).toBe('read back')
})
it('acknowledges nothing for a frame that arrives after it stopped', async () => {
it('rejects a frame that arrives after it stopped, so the platform redelivers it', async () => {
const { bot, store, intake } = await boot()
await intake.dispose()
// The platform redelivers it to a process that can store it, so this one neither stores nor settles it.
await deliver(bot, 'evt-late', inbound({ id: 'm-late' }))
// A listener that returns is an accepted frame — the adapter acknowledges it — so this one rejects instead.
await expect(deliver(bot, 'evt-late', inbound({ id: 'm-late' }))).rejects.toThrow('shutting down')
expect(store.get(ImMessageKey('fake:bot', 'm-late'))).toBeUndefined()
})