From d00a8d6cbfeeb7ac05f9084bdae821d773eae86a Mon Sep 17 00:00:00 2001 From: Codeman maintainer Date: Fri, 20 Nov 2026 04:06:03 +0100 Subject: [PATCH] test(discord): the review items of #832 that go on top A test pins the symlink and non-file branch of the reply store (a planted link or a folder at the metadata path is never replaced, and replies are still accepted), the @fileoverview names the file, its bounds, where the bridge claims and the four rules a reader has to keep, and a comment left from the #793 journal approach says store file. Co-Authored-By: Claude Opus 5.5 --- src/discord-reply-dedupe.ts | 24 ++++++++++++++++++++++-- test/discord-reply-dedupe.test.ts | 20 +++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/discord-reply-dedupe.ts b/src/discord-reply-dedupe.ts index ea938ec3..c17e13cb 100644 --- a/src/discord-reply-dedupe.ts +++ b/src/discord-reply-dedupe.ts @@ -1,4 +1,24 @@ -/** @fileoverview Best-effort replay protection. A metadata-store failure must never block a person's reply. */ +/** + * @fileoverview Best-effort replay protection for Discord replies. + * + * Owns `/discord-reply-dedupe.json`: at most 2,048 `guild:channel:message` + * ids with the time each was claimed, kept for 7 days, written 0600 through a temp + * file and a rename. It holds ids only, never message content. + * + * `DiscordBridge.handleMessage()` calls `claim()` once per allowed message, after the + * guild, allowlist and thread gates and before the rate limit, the exited-agent check, + * any download and any typing, so a gateway replay of a message that was refused is + * refused again rather than typed. + * + * Rules a reader has to keep: + * - A store failure never costs a reply: the claim falls back to memory, warns once + * per outage and the reply is still typed. + * - A file that could not be read (an errno failure, a symlink, anything but a regular + * file) is never overwritten. + * - Readable but invalid content (bad JSON, bad rows, oversized) is replaced. + * - Rows read from disk go first, so claims made while the file was unreadable are + * the ones the 2,048 bound keeps. + */ import fs from 'node:fs/promises'; import { join } from 'node:path'; import { randomUUID } from 'node:crypto'; @@ -69,7 +89,7 @@ export class DiscordReplyDedupe { this.dirty = true; } // Before awaiting IO or typing, including during an outage. while (this.seen.size > LIMIT) this.seen.delete(this.seen.keys().next().value!); - if (!this.loaded || !this.dirty) return fresh; // Do not overwrite a journal we could not read. + if (!this.loaded || !this.dirty) return fresh; // Never overwrite a store file we could not read. const tmp = `${path}.${randomUUID()}.tmp`; try { await fs.mkdir(this.dir, { recursive: true }); diff --git a/test/discord-reply-dedupe.test.ts b/test/discord-reply-dedupe.test.ts index 2c51d050..dd1e800c 100644 --- a/test/discord-reply-dedupe.test.ts +++ b/test/discord-reply-dedupe.test.ts @@ -1,4 +1,4 @@ -import { mkdtemp, readFile, rm, writeFile, stat } from 'node:fs/promises'; +import { lstat, mkdir, mkdtemp, readFile, rm, symlink, writeFile, stat } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { afterEach, beforeEach, expect, it, vi } from 'vitest'; @@ -131,3 +131,21 @@ it('heals oversized regular metadata and persists replay protection across resta expect((await stat(path)).size).toBeLessThan(512 * 1024); expect(warn).toHaveBeenCalledTimes(1); }); +it('never replaces a symlink or a folder at the metadata path, and still accepts replies', async () => { + const path = join(dir, 'discord-reply-dedupe.json'); + const target = join(dir, 'target'); + await writeFile(target, 'keep'); + await symlink(target, path); + const warn = vi.fn(), + store = new DiscordReplyDedupe(dir, Date.now, warn); + expect(await store.claim(guild, channel, message)).toBe(true); + expect(await store.claim(guild, channel, message)).toBe(false); + expect(await store.claim(guild, channel, '444444444444444444')).toBe(true); + expect((await lstat(path)).isSymbolicLink()).toBe(true); + expect(await readFile(target, 'utf8')).toBe('keep'); + expect(warn).toHaveBeenCalledTimes(1); + await rm(path); + await mkdir(path); + expect(await new DiscordReplyDedupe(dir, Date.now, warn).claim(guild, channel, message)).toBe(true); + expect((await lstat(path)).isDirectory()).toBe(true); +});