fix(discord): claim refused replies before rate limiting (#218)

This commit is contained in:
Saqeb Akhter committed 2026-11-18 11:04:02 +04:00
1 parent 7343274d5f
commit 44459a12dd
6 files changed
+54 -23

No files matched your search

+1 -9
View File
@@ -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. - **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`). - **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 replay metadata
`discord-reply-dedupe.json` is a private 0600 data-directory file containing at most `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.
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.
+2 -10
View File
@@ -138,14 +138,6 @@ The text explains `codeman notify "message" --urgent`. `codeman` is on the PATH
## Reply replay protection ## Reply replay protection
The bridge remembers up to 2,048 recent Discord reply IDs for seven days in the private 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.
`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.
This is best-effort replay protection, not an exactly-once guarantee. A full disk, unreadable 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.
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.
+1 -1
View File
@@ -1307,6 +1307,7 @@ export class DiscordBridge {
if (!this.cfg.allowedUserIds.includes(m.author?.id)) return; if (!this.cfg.allowedUserIds.includes(m.author?.id)) return;
const sessionId = Object.keys(this.threads).find((k) => this.threads[k] === m.channel_id); const sessionId = Object.keys(this.threads).find((k) => this.threads[k] === m.channel_id);
if (!sessionId) return; if (!sessionId) return;
if (!(await this.replies.claim(m.guild_id, m.channel_id, m.id))) return;
const t = this.now(); const t = this.now();
const prev = this.lastReply.get(sessionId); const prev = this.lastReply.get(sessionId);
if (prev !== undefined && t - prev < REPLY_MIN_INTERVAL_MS) { 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.'); await this.say(m.channel_id, 'The agent in that tab has exited, so nothing was typed.');
return; return;
} }
if (!(await this.replies.claim(m.guild_id, m.channel_id, m.id))) return;
this.lastReply.set(sessionId, t); this.lastReply.set(sessionId, t);
const saved = await this.saveAttachments(sessionId, m); const saved = await this.saveAttachments(sessionId, m);
const text = sanitiseReply(m.content ?? '', saved.paths); const text = sanitiseReply(m.content ?? '', saved.paths);
+5 -2
View File
@@ -31,8 +31,11 @@ export class DiscordReplyDedupe {
let contentRead = false; let contentRead = false;
try { try {
const st = await fs.lstat(path); const st = await fs.lstat(path);
if (!st.isFile() || st.isSymbolicLink() || st.size > 512 * 1024) if (!st.isFile() || st.isSymbolicLink()) throw Object.assign(new Error('Unsafe metadata'), { code: 'EINVAL' });
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'); const text = await fs.readFile(path, 'utf8');
contentRead = true; contentRead = true;
const rows: unknown = JSON.parse(text); const rows: unknown = JSON.parse(text);
+35 -1
View File
@@ -462,6 +462,40 @@ describe('DiscordBridge', () => {
next.stop(); 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 () => { it('delivers replies and alerts when metadata is corrupt and recovers without restarting', async () => {
const b = await ready(); const b = await ready();
writeFileSync(join(dir, 'discord-reply-dedupe.json'), '{'); 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 () => { it('types a reply that waits on a download before a later text reply', async () => {
const b = await make(); const b = await make();
const slow = b.onGatewayMessage(reply({ content: 'first' })); 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; t += 5_000;
const fast = b.onGatewayMessage(reply({ id: '555555555555555556', content: 'second', attachments: [] })); const fast = b.onGatewayMessage(reply({ id: '555555555555555556', content: 'second', attachments: [] }));
await Promise.all([slow, fast]); await Promise.all([slow, fast]);
+10
View File
@@ -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(await new DiscordReplyDedupe(dir).claim(guild, channel, message)).toBe(false);
expect(warn).toHaveBeenCalledTimes(1); 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);
});