From 44459a12dddfdaf036aa0088fa68d90134ddf186 Mon Sep 17 00:00:00 2001 From: Saqeb Akhter Date: Wed, 18 Nov 2026 11:04:02 +0400 Subject: [PATCH] fix(discord): claim refused replies before rate limiting (#218) --- docs/architecture-invariants.md | 10 +-------- docs/discord-bridge.md | 12 ++--------- src/discord-bridge.ts | 2 +- src/discord-reply-dedupe.ts | 7 ++++-- test/discord-bridge.test.ts | 36 ++++++++++++++++++++++++++++++- test/discord-reply-dedupe.test.ts | 10 +++++++++ 6 files changed, 54 insertions(+), 23 deletions(-) diff --git a/docs/architecture-invariants.md b/docs/architecture-invariants.md index 773cc28a..691f2162 100644 --- a/docs/architecture-invariants.md +++ b/docs/architecture-invariants.md @@ -1550,14 +1550,6 @@ updates `lastSubmitAt` just like any other Enter. - **The whole last message.** A finished turn or an error posts the CLI's whole last message, not the screen: `WebServer.lastResponseFor` is the response viewer's reader (the body of `GET /api/sessions/:id/last-response`, registered on the route context by `registerSessionRoutes`), so the source is the same per-CLI transcript, or the pane split into turns where a CLI keeps none; a dialog and a shell keep the screen tail (its composer row and the status lines under it cut, never a `❯ 1.` option row). The read is bounded to 4 s and any failure or empty read falls back to the screen. It is split into at most 8 posts of 1,900 characters, a cut code fence closed and reopened, and Markdown tables become aligned code blocks (the separator test strips whitespace first: a regex over a run of spaces is quadratic, and the text is steerable by whatever the agent read). The repeat check is keyed on the text plus the turn it came from (the transcript stamp, else the last submit) and is recorded only after the first post succeeds, so a failed post is sent again and a later turn ending on the same words is not dropped. Only some CLIs raise a turn end (`/api/hook-event`: Claude's `stop` and `idle_prompt`, DeepSeek's `stop`), so the others reach this path through errors, a blocked respawn or a crash loop. - **Cost guards.** The alert tail is a function called only once a post will happen (no tmux exec while off), and `discord-notify` is a per-session token bucket (6 a minute, `429 RATE_LIMITED`). - ### Discord reply replay metadata -`discord-reply-dedupe.json` is a private 0600 data-directory file containing at most -2,048 reply IDs and timestamps retained for seven days. An allowed reply is claimed -by message ID before attachments or terminal input. Store failures log once and fall -back to memory; they never cost a reply. An unreadable or unsafe file is never -overwritten, and reads retry later. Readable but invalid ID-only content warns and -heals on the next write. Recovery keeps older disk rows before newer outage claims -so bounded eviction drops the oldest IDs first. Ambiguous terminal input is never -replayed automatically. +`discord-reply-dedupe.json` is a private 0600 data-directory file containing at most 2,048 reply IDs and timestamps retained for seven days. An allowed reply is claimed by message ID before rate-limit or exited-agent checks, attachments, or terminal input. Store failures log once and fall back to memory; they never cost a reply. An unreadable or unsafe file is never overwritten, and reads retry later. Invalid or oversized regular-file ID-only content warns and heals on the next write. Recovery keeps older disk rows before newer outage claims so bounded eviction drops the oldest IDs first. Ambiguous terminal input is never replayed automatically. diff --git a/docs/discord-bridge.md b/docs/discord-bridge.md index 14598b57..351aef07 100644 --- a/docs/discord-bridge.md +++ b/docs/discord-bridge.md @@ -138,14 +138,6 @@ The text explains `codeman notify "message" --urgent`. `codeman` is on the PATH ## Reply replay protection -The bridge remembers up to 2,048 recent Discord reply IDs for seven days in the private -`discord-reply-dedupe.json` metadata file. It claims an allowed reply before downloading -attachments or typing into its session, so a gateway replay normally cannot type it twice, -including after a restart. The file contains IDs and timestamps only, with mode 0600. +The bridge remembers up to 2,048 recent Discord reply IDs for seven days in the private `discord-reply-dedupe.json` metadata file. It claims an allowed reply before rate-limit or exited-agent checks, downloading attachments, or typing into its session, so a gateway replay normally cannot type it twice, including after a restart. The file contains IDs and timestamps only, with mode 0600. -This is best-effort replay protection, not an exactly-once guarantee. A full disk, unreadable -file, or invalid metadata logs one content-free warning and falls back to in-memory checks. -Readable but invalid ID metadata is replaced on the next write. Files that could not be read are left untouched. Reads and writes retry on later replies; a restart during a metadata outage can lose those -checks. At capacity the oldest entries are evicted, so capacity never blocks new replies. -Notifications retain their existing delivery and retry behavior; this store never suppresses -an outbound alert or replays terminal input. +This is best-effort replay protection, not an exactly-once guarantee. A full disk, unreadable file, or invalid metadata logs one content-free warning and falls back to in-memory checks. Invalid or oversized regular-file ID metadata is replaced on the next write. Files that could not be read are left untouched. Reads and writes retry on later replies; a restart during a metadata outage can lose those checks. At capacity the oldest entries are evicted, so capacity never blocks new replies. Notifications retain their existing delivery and retry behavior; this store never suppresses an outbound alert or replays terminal input. diff --git a/src/discord-bridge.ts b/src/discord-bridge.ts index c1948f88..78560ea6 100644 --- a/src/discord-bridge.ts +++ b/src/discord-bridge.ts @@ -1307,6 +1307,7 @@ export class DiscordBridge { if (!this.cfg.allowedUserIds.includes(m.author?.id)) return; const sessionId = Object.keys(this.threads).find((k) => this.threads[k] === m.channel_id); if (!sessionId) return; + if (!(await this.replies.claim(m.guild_id, m.channel_id, m.id))) return; const t = this.now(); const prev = this.lastReply.get(sessionId); if (prev !== undefined && t - prev < REPLY_MIN_INTERVAL_MS) { @@ -1318,7 +1319,6 @@ export class DiscordBridge { await this.say(m.channel_id, 'The agent in that tab has exited, so nothing was typed.'); return; } - if (!(await this.replies.claim(m.guild_id, m.channel_id, m.id))) return; this.lastReply.set(sessionId, t); const saved = await this.saveAttachments(sessionId, m); const text = sanitiseReply(m.content ?? '', saved.paths); diff --git a/src/discord-reply-dedupe.ts b/src/discord-reply-dedupe.ts index 29cba04a..ea938ec3 100644 --- a/src/discord-reply-dedupe.ts +++ b/src/discord-reply-dedupe.ts @@ -31,8 +31,11 @@ export class DiscordReplyDedupe { let contentRead = false; try { const st = await fs.lstat(path); - if (!st.isFile() || st.isSymbolicLink() || st.size > 512 * 1024) - throw Object.assign(new Error('Unsafe metadata'), { code: 'EINVAL' }); + if (!st.isFile() || st.isSymbolicLink()) throw Object.assign(new Error('Unsafe metadata'), { code: 'EINVAL' }); + if (st.size > 512 * 1024) { + contentRead = true; // A known oversized regular file is invalid metadata, not an IO failure. + throw new Error('Oversized metadata'); + } const text = await fs.readFile(path, 'utf8'); contentRead = true; const rows: unknown = JSON.parse(text); diff --git a/test/discord-bridge.test.ts b/test/discord-bridge.test.ts index 1468def2..fec31c71 100644 --- a/test/discord-bridge.test.ts +++ b/test/discord-bridge.test.ts @@ -462,6 +462,40 @@ describe('DiscordBridge', () => { next.stop(); }); + it('claims rate-limited snowflake replies so replays remain refused across restart', async () => { + const b = await ready(); + await b.onGatewayMessage(reply({ id: '555555555555555555' })); + t += 200; + const refused = reply({ id: '555555555555555556', content: 'too soon' }); + await b.onGatewayMessage(refused); + expect(sendInput).toHaveBeenCalledTimes(1); + expect(posts.some((p) => p.path.includes(encodeURIComponent('⏳')))).toBe(true); + t += 5_000; + const count = posts.length; + await b.onGatewayMessage(refused); + expect(sendInput).toHaveBeenCalledTimes(1); + expect(posts).toHaveLength(count); + b.stop(); + const next = await ready(); + await next.onGatewayMessage(refused); + expect(sendInput).toHaveBeenCalledTimes(1); + await next.onGatewayMessage(reply({ id: '555555555555555557', content: 'new attempt' })); + expect(sendInput).toHaveBeenCalledTimes(2); + next.stop(); + }); + + it('ignores an immediate replay without adding an hourglass to a successful reply', async () => { + const b = await ready(); + const m = reply({ id: '555555555555555555' }); + await b.onGatewayMessage(m); + t += 200; + await b.onGatewayMessage(m); + expect(sendInput).toHaveBeenCalledTimes(1); + expect(posts.filter((p) => p.method === 'PUT')).toHaveLength(1); + expect(posts.some((p) => p.path.includes(encodeURIComponent('⏳')))).toBe(false); + b.stop(); + }); + it('delivers replies and alerts when metadata is corrupt and recovers without restarting', async () => { const b = await ready(); writeFileSync(join(dir, 'discord-reply-dedupe.json'), '{'); @@ -1175,7 +1209,7 @@ describe('DiscordBridge files and startup hint', () => { it('types a reply that waits on a download before a later text reply', async () => { const b = await make(); const slow = b.onGatewayMessage(reply({ content: 'first' })); - await new Promise((r) => setTimeout(r, 0)); // the first reply is under way (gate passed) before the clock moves + await vi.waitFor(() => expect(downloads).toHaveLength(1)); // wait for the claim and rate-limit gate t += 5_000; const fast = b.onGatewayMessage(reply({ id: '555555555555555556', content: 'second', attachments: [] })); await Promise.all([slow, fast]); diff --git a/test/discord-reply-dedupe.test.ts b/test/discord-reply-dedupe.test.ts index 46b180bd..2c51d050 100644 --- a/test/discord-reply-dedupe.test.ts +++ b/test/discord-reply-dedupe.test.ts @@ -121,3 +121,13 @@ it('heals an over-cap readable store rather than remaining memory-only until man expect(await new DiscordReplyDedupe(dir).claim(guild, channel, message)).toBe(false); expect(warn).toHaveBeenCalledTimes(1); }); + +it('heals oversized regular metadata and persists replay protection across restart', async () => { + const path = join(dir, 'discord-reply-dedupe.json'); + await writeFile(path, ' '.repeat(512 * 1024 + 1)); + const warn = vi.fn(); + expect(await new DiscordReplyDedupe(dir, Date.now, warn).claim(guild, channel, message)).toBe(true); + expect(await new DiscordReplyDedupe(dir).claim(guild, channel, message)).toBe(false); + expect((await stat(path)).size).toBeLessThan(512 * 1024); + expect(warn).toHaveBeenCalledTimes(1); +});