From 73402379618e455e1d77f829091c206af0f1491d Mon Sep 17 00:00:00 2001 From: saket0187 Date: Sun, 29 Nov 2026 18:42:19 +0530 Subject: [PATCH 1/3] fix(storage): stop invoices and vanish checks from overwriting a user's admission and vanish state (cherry picked from commit b0989aea044a6eaf5357de05c83e3b4d302adc60) --- .../keep-user-admission-and-vanish-state.md | 7 + src/@types/repositories.ts | 2 +- src/repositories/event-repository.ts | 8 +- src/repositories/user-repository.ts | 58 ++++---- .../features/invoices/nwc-invoice.feature | 24 ++++ .../features/invoices/nwc-invoice.feature.ts | 69 ++++++++- .../repositories/event-repository.spec.ts | 37 ++++- .../unit/repositories/user-repository.spec.ts | 136 ++++++++++++------ 8 files changed, 262 insertions(+), 79 deletions(-) create mode 100644 .changeset/keep-user-admission-and-vanish-state.md diff --git a/.changeset/keep-user-admission-and-vanish-state.md b/.changeset/keep-user-admission-and-vanish-state.md new file mode 100644 index 000000000..9d52b3ef3 --- /dev/null +++ b/.changeset/keep-user-admission-and-vanish-state.md @@ -0,0 +1,7 @@ +--- +'nostream': patch +--- + +fix(storage): stop invoices and vanish checks from overwriting a user's admission and vanish state + +Creating an admission or subscription invoice upserted its pubkey with `is_admitted` and `is_vanished` set to false, so it cleared both for an existing user. A renewal invoice, including the ones the relay creates itself, took a paying subscriber's admission away, so their events were refused once the admission cache expired, and an invoice anyone could request for a vanished pubkey lifted its request to vanish. An existing row now keeps the admission, vanish and terms fields the caller did not supply, and a new row created without a vanish state starts vanished when a request to vanish for the pubkey is stored. The vanish check for a pubkey without a row no longer overwrites a row another writer created in the meantime and returns that row's state. Both look for the stored request on the primary instead of a replica that may not have it yet. A deliberate limit: any undeleted stored request to vanish counts, as it already did when a user's first event created the row, so one that names only another relay also starts a new row vanished. diff --git a/src/@types/repositories.ts b/src/@types/repositories.ts index 8b6a6a14d..63de189af 100644 --- a/src/@types/repositories.ts +++ b/src/@types/repositories.ts @@ -65,7 +65,7 @@ export interface IEventRepository { findStoredReplaceable(pubkey: Pubkey, kind: number): IQueryResult deleteByPubkeyAndIds(pubkey: Pubkey, ids: EventId[]): Promise deleteByPubkeyExceptKinds(pubkey: Pubkey, excludedKinds: number[]): Promise - hasActiveRequestToVanish(pubkey: Pubkey): Promise + hasActiveRequestToVanish(pubkey: Pubkey, client?: DatabaseClient): Promise deleteExpiredAndRetained(options?: EventRetentionOptions): Promise } diff --git a/src/repositories/event-repository.ts b/src/repositories/event-repository.ts index ab3b9d9c2..4145b02f2 100644 --- a/src/repositories/event-repository.ts +++ b/src/repositories/event-repository.ts @@ -550,8 +550,12 @@ export class EventRepository implements IEventRepository { }) } - public async hasActiveRequestToVanish(pubkey: string): Promise { - const result = await this.readReplicaDbClient('events') + // The answer becomes the user's stored vanish state, so it is never read from a replica that may lag. + public async hasActiveRequestToVanish( + pubkey: string, + client: DatabaseClient = this.masterDbClient, + ): Promise { + const result = await client('events') .select('event_id') .where('event_pubkey', toBuffer(pubkey)) .where('event_kind', EventKinds.REQUEST_TO_VANISH) diff --git a/src/repositories/user-repository.ts b/src/repositories/user-repository.ts index 098afe018..964ae4231 100644 --- a/src/repositories/user-repository.ts +++ b/src/repositories/user-repository.ts @@ -1,4 +1,4 @@ -import { always, applySpec, defaultTo, omit, pipe, prop } from 'ramda' +import { isEmpty, pickBy, prop } from 'ramda' import { DatabaseClient, Pubkey } from '../@types/base' import { DBUser, User } from '../@types/user' import { fromBuffer, fromDBUser, toBuffer } from '../utils/transform' @@ -29,33 +29,34 @@ export class UserRepository implements IUserRepository { const date = new Date() - const row = applySpec({ - pubkey: pipe(prop('pubkey'), toBuffer), - is_admitted: pipe(prop('isAdmitted'), defaultTo(false)), - is_vanished: pipe(prop('isVanished'), defaultTo(false)), - tos_accepted_at: prop('tosAcceptedAt'), - updated_at: always(date), - created_at: always(date), - })(user) + // A new row starts from the stored events, so creating it cannot lift a request to vanish. + const isVanished = user.isVanished ?? (await this.eventRepository.hasActiveRequestToVanish(user.pubkey, client)) - const query = client('users') - .insert(row) - .onConflict('pubkey') - .merge(omit(['pubkey', 'balance', 'created_at'])(row)) + const row = { + pubkey: toBuffer(user.pubkey), + is_admitted: user.isAdmitted ?? false, + is_vanished: isVanished, + tos_accepted_at: user.tosAcceptedAt, + updated_at: date, + created_at: date, + } - return { - then: ( - onfulfilled: (value: number) => T1 | PromiseLike, - onrejected: (reason: any) => T2 | PromiseLike, - ) => query.then(prop('rowCount') as () => number).then(onfulfilled, onrejected), - catch: (onrejected: (reason: any) => T | PromiseLike) => query.catch(onrejected), - toString: (): string => query.toString(), - } as Promise + // An existing row takes only the fields the caller supplied. + const changes: Record = pickBy((value) => value !== undefined, { + is_admitted: user.isAdmitted, + is_vanished: user.isVanished, + tos_accepted_at: user.tosAcceptedAt, + }) + + const insert = client('users').insert(row).onConflict('pubkey') + const query = isEmpty(changes) ? insert.ignore() : insert.merge({ ...changes, updated_at: date }) + + return query.then(prop('rowCount') as () => number) } /** - * Returns vanish state from users.is_vanished, or lazily hydrates a user row from events once - * when no users row exists (single upsert; no duplicate inserts). + * Returns vanish state from users.is_vanished. A missing row is created from the stored events, + * and a row another writer created first is left as it is. */ public async isVanished(pubkey: Pubkey, client: DatabaseClient = this.dbClient): Promise { const existing = await this.findByPubkey(pubkey, client) @@ -63,9 +64,14 @@ export class UserRepository implements IUserRepository { return existing.isVanished } - const vanishedFromEvents = await this.eventRepository.hasActiveRequestToVanish(pubkey) - await this.upsertVanishState(pubkey, vanishedFromEvents, client) - return vanishedFromEvents + await this.upsert({ pubkey }, client) + + const created = await this.findByPubkey(pubkey, client) + if (!created) { + throw new Error(`users row for ${pubkey} is missing after it was created`) + } + + return created.isVanished } public setVanished(pubkey: Pubkey, vanished: boolean, client: DatabaseClient = this.dbClient): Promise { diff --git a/test/integration/features/invoices/nwc-invoice.feature b/test/integration/features/invoices/nwc-invoice.feature index d45adba9e..b77fad470 100644 --- a/test/integration/features/invoices/nwc-invoice.feature +++ b/test/integration/features/invoices/nwc-invoice.feature @@ -22,3 +22,27 @@ Feature: NWC invoice integration When I request an admission invoice for pubkey "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" Then the invoice request response status is 200 And an NWC invoice is stored as pending for pubkey "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" + + # The subscription fixture's own teardown removes the subscriber it creates. + @subscription-plan-change + Scenario: a renewal invoice leaves the subscriber admitted and vanished + Given NWC payments are enabled with URI scheme "nostr+walletconnect" + And the relay sells the plans "basic" at 21000 sats and "pro" at 100000 sats + And NWC wallet service make_invoice responds with a pending invoice + And a subscriber has a running "basic" plan that ends in 10 days + And the subscriber has requested to vanish + When I request a "basic" plan invoice for the subscriber + Then the invoice request response status is 200 + And the subscriber is still admitted and vanished + + Scenario: an invoice for a pubkey without a users row keeps its stored request to vanish + Given someone called Alice + And Alice sends a request_to_vanish event + And the relay has no users row for Alice + And NWC payments are enabled with URI scheme "nostr+walletconnect" + And NWC wallet service make_invoice responds with a pending invoice + When I request an admission invoice for Alice + Then the invoice request response status is 200 + And the relay has a users row for Alice that is vanished + And Alice drafts a text_note event with content "still gone" + Then Alice sends their last draft event unsuccessfully because "blocked: request to vanish active for pubkey" diff --git a/test/integration/features/invoices/nwc-invoice.feature.ts b/test/integration/features/invoices/nwc-invoice.feature.ts index ebcc07f52..b5d019f11 100644 --- a/test/integration/features/invoices/nwc-invoice.feature.ts +++ b/test/integration/features/invoices/nwc-invoice.feature.ts @@ -197,12 +197,12 @@ Given('NWC wallet service make_invoice never responds', async function (this: Wo }, }) }) -When('I request an admission invoice for pubkey {string}', async function (this: World>, pubkey: string) { +const requestInvoice = async (world: World>, pubkey: string, fields: Record) => { const response: AxiosResponse = await axios.post( INVOICES_URL, new URLSearchParams({ tosAccepted: 'yes', - feeSchedule: 'admission', + ...fields, pubkey, }).toString(), { @@ -213,12 +213,73 @@ When('I request an admission invoice for pubkey {string}', async function (this: }, ) - this.parameters.nwcInvoiceHttpResponse = response - this.parameters.nwcTestPubkeys = [...(this.parameters.nwcTestPubkeys ?? []), pubkey] + world.parameters.nwcInvoiceHttpResponse = response + world.parameters.nwcTestPubkeys = [...(world.parameters.nwcTestPubkeys ?? []), pubkey] if (response.status === 400) { throw new Error(`Unexpected 400 response body: ${String(response.data)}`) } +} + +When( + 'I request an admission invoice for pubkey {string}', + async function (this: World>, pubkey: string) { + await requestInvoice(this, pubkey, { feeSchedule: 'admission' }) + }, +) + +const recordInvoicesFor = async (world: World>, pubkey: string) => { + const rows = await getMasterDbClient()('invoices').where('pubkey', Buffer.from(pubkey, 'hex')).select('id') + + world.parameters.nwcInsertedInvoiceIds = [ + ...(world.parameters.nwcInsertedInvoiceIds ?? []), + ...rows.map((row: { id: string }) => row.id), + ] +} + +When('I request an admission invoice for {word}', async function (this: World>, name: string) { + const { pubkey } = this.parameters.identities[name] + + await requestInvoice(this, pubkey, { feeSchedule: 'admission' }) + await recordInvoicesFor(this, pubkey) +}) + +When( + 'I request a {string} plan invoice for the subscriber', + async function (this: World>, planId: string) { + const pubkey = (this.parameters.subscriberPubkey as Buffer).toString('hex') + + await requestInvoice(this, pubkey, { planId }) + await recordInvoicesFor(this, pubkey) + }, +) + +Given('the relay has no users row for {word}', async function (this: World>, name: string) { + const { pubkey } = this.parameters.identities[name] + + await getMasterDbClient()('users').where('pubkey', Buffer.from(pubkey, 'hex')).delete() +}) + +Then( + 'the relay has a users row for {word} that is vanished', + async function (this: World>, name: string) { + const { pubkey } = this.parameters.identities[name] + const user = await getMasterDbClient()('users').where('pubkey', Buffer.from(pubkey, 'hex')).first('is_vanished') + + expect(user).to.deep.equal({ is_vanished: true }) + }, +) + +Given('the subscriber has requested to vanish', async function (this: World>) { + await getMasterDbClient()('users').where('pubkey', this.parameters.subscriberPubkey).update({ is_vanished: true }) +}) + +Then('the subscriber is still admitted and vanished', async function (this: World>) { + const user = await getMasterDbClient()('users') + .where('pubkey', this.parameters.subscriberPubkey) + .first('is_admitted', 'is_vanished') + + expect(user).to.deep.equal({ is_admitted: true, is_vanished: true }) }) Then('the invoice request response status is {int}', function (this: World>, statusCode: number) { diff --git a/test/unit/repositories/event-repository.spec.ts b/test/unit/repositories/event-repository.spec.ts index 712e15013..463c3de84 100644 --- a/test/unit/repositories/event-repository.spec.ts +++ b/test/unit/repositories/event-repository.spec.ts @@ -11,10 +11,11 @@ chai.use(sinonChai) const { expect } = chai -import { ContextMetadataKey, EventDeduplicationMetadataKey, EventTags } from '../../../src/constants/base' +import { ContextMetadataKey, EventDeduplicationMetadataKey, EventKinds, EventTags } from '../../../src/constants/base' import { DatabaseClient } from '../../../src/@types/base' import { EventRepository } from '../../../src/repositories/event-repository' import { MAX_IMPORTED_DELETION_TARGETS } from '../../../src/services/event-import-service' +import { createRecordingKnex } from './recording-knex' describe('EventRepository', () => { let repository: IEventRepository @@ -1110,6 +1111,40 @@ describe('EventRepository', () => { }) }) + describe('hasActiveRequestToVanish', () => { + const pubkey = '22'.repeat(32) + + it('reads the primary, not the replica', async () => { + const primary = createRecordingKnex() + const replica = createRecordingKnex() + primary.respondWith([{ event_id: Buffer.alloc(32) }]) + + const result = await new EventRepository(primary.db, replica.db).hasActiveRequestToVanish(pubkey) + + expect(result).to.equal(true) + expect(primary.queries).to.have.length(1) + expect(replica.queries).to.be.empty + }) + + it('runs one bounded query on the client it is given', async () => { + const primary = createRecordingKnex() + const replica = createRecordingKnex() + const transaction = createRecordingKnex() + + const result = await new EventRepository(primary.db, replica.db).hasActiveRequestToVanish(pubkey, transaction.db) + + expect(result).to.equal(false) + expect(transaction.queries).to.deep.equal([ + { + sql: 'select "event_id" from "events" where "event_pubkey" = $1 and "event_kind" = $2 and "deleted_at" is null limit $3', + bindings: [Buffer.from(pubkey, 'hex'), EventKinds.REQUEST_TO_VANISH, 1], + }, + ]) + expect(primary.queries).to.be.empty + expect(replica.queries).to.be.empty + }) + }) + describe('deleteExpiredAndRetained', () => { it('returns zero counts when retention is disabled', async () => { const result = await repository.deleteExpiredAndRetained({ maxDays: 0 }) diff --git a/test/unit/repositories/user-repository.spec.ts b/test/unit/repositories/user-repository.spec.ts index 855e8faa6..80cc124c2 100644 --- a/test/unit/repositories/user-repository.spec.ts +++ b/test/unit/repositories/user-repository.spec.ts @@ -8,6 +8,7 @@ import { DatabaseClient } from '../../../src/@types/base' import { User } from '../../../src/@types/user' import { IEventRepository, IUserRepository } from '../../../src/@types/repositories' import { UserRepository } from '../../../src/repositories/user-repository' +import { createRecordingKnex } from './recording-knex' chai.use(sinonChai) chai.use(chaiAsPromised) @@ -155,10 +156,11 @@ describe('UserRepository', () => { toString: () => 'fake', } const mergeStub = sandbox.stub().returns(fakeQB) - const onConflictStub = sandbox.stub().returns({ merge: mergeStub }) + const ignoreStub = sandbox.stub().returns(fakeQB) + const onConflictStub = sandbox.stub().returns({ merge: mergeStub, ignore: ignoreStub }) const insertStub = sandbox.stub().returns({ onConflict: onConflictStub }) const client = sandbox.stub().returns({ insert: insertStub }) as unknown as DatabaseClient - return { client, insertStub, onConflictStub, mergeStub } + return { client, insertStub, onConflictStub, mergeStub, ignoreStub } } it('resolves with the row count from the DB response', async () => { @@ -199,6 +201,7 @@ describe('UserRepository', () => { it('defaults is_admitted and is_vanished to false when not provided', async () => { const { client, insertStub } = makeFakeQueryBuilder() + ;(eventRepository.hasActiveRequestToVanish as sinon.SinonStub).resolves(false) await repository.upsert({ pubkey: pubkeyHex }, client) @@ -207,6 +210,16 @@ describe('UserRepository', () => { expect(row.is_vanished).to.equal(false) }) + it('starts a new row vanished when a request to vanish is stored, reading it on the same client', async () => { + const { client, insertStub } = makeFakeQueryBuilder() + ;(eventRepository.hasActiveRequestToVanish as sinon.SinonStub).resolves(true) + + await repository.upsert({ pubkey: pubkeyHex }, client) + + expect(eventRepository.hasActiveRequestToVanish).to.have.been.calledOnceWithExactly(pubkeyHex, client) + expect(insertStub.firstCall.args[0].is_vanished).to.equal(true) + }) + it('conflicts on pubkey and omits pubkey from the merge set', async () => { const { client, onConflictStub, mergeStub } = makeFakeQueryBuilder() @@ -218,6 +231,40 @@ describe('UserRepository', () => { expect(mergeRow).to.not.have.property('created_at') expect(mergeRow).to.have.property('updated_at') }) + + it('leaves an existing row as it is when only the pubkey is given', async () => { + const { db, queries } = createRecordingKnex() + ;(eventRepository.hasActiveRequestToVanish as sinon.SinonStub).resolves(false) + + await repository.upsert({ pubkey: pubkeyHex }, db) + + expect(queries).to.have.length(1) + expect(queries[0].sql).to.match(/ on conflict \("pubkey"\) do nothing$/) + }) + + it('merges only the fields the caller supplied, explicit false and null included', async () => { + const { db, queries } = createRecordingKnex() + + await repository.upsert({ pubkey: pubkeyHex, isVanished: false, tosAcceptedAt: null }, db) + + const [{ sql, bindings }] = queries + expect(sql).to.match( + / on conflict \("pubkey"\) do update set "is_vanished" = \$\d+,"tos_accepted_at" = \$\d+,"updated_at" = \$\d+$/, + ) + expect(bindings.slice(-3, -1)).to.deep.equal([false, null]) + expect(eventRepository.hasActiveRequestToVanish).to.not.have.been.called + }) + + it('merges only admission when only admission is supplied', async () => { + const { db, queries } = createRecordingKnex() + ;(eventRepository.hasActiveRequestToVanish as sinon.SinonStub).resolves(false) + + await repository.upsert({ pubkey: pubkeyHex, isAdmitted: true }, db) + + const [{ sql, bindings }] = queries + expect(sql).to.match(/ on conflict \("pubkey"\) do update set "is_admitted" = \$\d+,"updated_at" = \$\d+$/) + expect(bindings.slice(-2, -1)).to.deep.equal([true]) + }) }) describe('.isVanished', () => { @@ -239,49 +286,48 @@ describe('UserRepository', () => { expect(eventRepository.hasActiveRequestToVanish).to.not.have.been.called }) - it('calls eventRepository when user row does not exist and returns vanish state', async () => { - sandbox.stub(repository, 'findByPubkey').resolves(undefined) - ;(eventRepository.hasActiveRequestToVanish as sinon.SinonStub).resolves(true) - - const fakeQB = { - then: (fn: (v: any) => any) => Promise.resolve({ rowCount: 1 }).then(fn), - catch: (fn: (e: any) => any) => Promise.resolve(1).catch(fn), - toString: () => 'fake', - } - const client = sandbox.stub().returns({ - insert: sandbox.stub().returns({ - onConflict: sandbox.stub().returns({ - merge: sandbox.stub().returns(fakeQB), - }), - }), - }) as unknown as DatabaseClient - - const result = await repository.isVanished(pubkeyHex, client) - - expect(result).to.equal(true) - expect(eventRepository.hasActiveRequestToVanish).to.have.been.calledWith(pubkeyHex) - }) - - it('hydrates vanish=false when eventRepository returns false', async () => { - sandbox.stub(repository, 'findByPubkey').resolves(undefined) - ;(eventRepository.hasActiveRequestToVanish as sinon.SinonStub).resolves(false) - - const fakeQB = { - then: (fn: (v: any) => any) => Promise.resolve({ rowCount: 1 }).then(fn), - catch: (fn: (e: any) => any) => Promise.resolve(1).catch(fn), - toString: () => 'fake', - } - const client = sandbox.stub().returns({ - insert: sandbox.stub().returns({ - onConflict: sandbox.stub().returns({ - merge: sandbox.stub().returns(fakeQB), - }), - }), - }) as unknown as DatabaseClient - - const result = await repository.isVanished(pubkeyHex, client) - - expect(result).to.equal(false) + describe('when no row exists yet', () => { + const vanishedRow = { ...dbUserRow, is_vanished: true } + const stubStoredRequest = (stored: boolean) => + (eventRepository.hasActiveRequestToVanish as sinon.SinonStub).resolves(stored) + + it('creates the row from the stored events and returns what was stored', async () => { + const { db, queries, respondWith } = createRecordingKnex() + stubStoredRequest(true) + respondWith([], 1, [vanishedRow]) + + expect(await repository.isVanished(pubkeyHex, db)).to.equal(true) + expect(eventRepository.hasActiveRequestToVanish).to.have.been.calledOnceWithExactly(pubkeyHex, db) + expect(queries.map(({ sql }) => sql.split(' ')[0])).to.deep.equal(['select', 'insert', 'select']) + expect(queries[1].sql).to.match(/ on conflict \("pubkey"\) do nothing$/) + }) + + it('returns a vanish another writer stored first instead of overwriting it', async () => { + const { db, queries, respondWith } = createRecordingKnex() + stubStoredRequest(false) + respondWith([], 0, [vanishedRow]) + + expect(await repository.isVanished(pubkeyHex, db)).to.equal(true) + expect(queries[1].sql).to.match(/ on conflict \("pubkey"\) do nothing$/) + }) + + it('does not write over a row another writer created first', async () => { + const { db, queries, respondWith } = createRecordingKnex() + stubStoredRequest(true) + respondWith([], 0, [vanishedRow]) + + expect(await repository.isVanished(pubkeyHex, db)).to.equal(true) + expect(queries).to.have.length(3) + expect(queries[1].sql).to.match(/ on conflict \("pubkey"\) do nothing$/) + }) + + it('fails rather than guess when the row cannot be read back', async () => { + const { db, respondWith } = createRecordingKnex() + stubStoredRequest(false) + respondWith([], 1, []) + + await expect(repository.isVanished(pubkeyHex, db)).to.be.rejectedWith('missing after it was created') + }) }) }) From 5c2c3276a50856560da149001fe38f279f96c3ca Mon Sep 17 00:00:00 2001 From: chappie-daemon Date: Sun, 29 Nov 2026 11:32:39 -0500 Subject: [PATCH 2/3] docs: say what the user upsert contract is, and where the vanish query runs The upsert now writes only what the caller supplies, which was implicit in the fix; the interface says so where a caller reads it. The benchmark entry described hasActiveRequestToVanish as a per-message tax, which stopped being true when isVanished began answering from the row it just created. --- src/@types/repositories.ts | 8 ++++++++ src/scripts/benchmark-queries.ts | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/@types/repositories.ts b/src/@types/repositories.ts index 63de189af..9b51a808d 100644 --- a/src/@types/repositories.ts +++ b/src/@types/repositories.ts @@ -232,6 +232,14 @@ export interface IUserSubscriptionRepository { export interface IUserRepository { findByPubkey(pubkey: Pubkey, client?: DatabaseClient): Promise + /** + * Writes only the fields `user` supplies. An existing row keeps everything else — admission, + * vanish state and the ToS timestamp are never reset by omission — and a missing row is + * created with `false` for the flags, except that its vanish state comes from the stored + * request-to-vanish events when the caller does not give one. + * + * Resolves with the number of rows written, so `0` means the row already existed. + */ upsert(user: Partial, client?: DatabaseClient): Promise getBalanceByPubkey(pubkey: Pubkey, client?: DatabaseClient): Promise isVanished(pubkey: Pubkey, client?: DatabaseClient): Promise diff --git a/src/scripts/benchmark-queries.ts b/src/scripts/benchmark-queries.ts index 8fe5d5c46..49d85988c 100644 --- a/src/scripts/benchmark-queries.ts +++ b/src/scripts/benchmark-queries.ts @@ -207,7 +207,7 @@ function buildCases(): BenchmarkCase[] { { name: 'hasActiveRequestToVanish (pubkey + kind=62 + not deleted)', description: - 'Exact query run on every inbound event via UserRepository.isVanished; latency here is a per-message tax. This is the only hot path that filters on deleted_at.', + 'Exact query `UserRepository.isVanished` runs when a pubkey has no users row, on the master so a lagging replica cannot decide what is stored; it is not on the per-message path. This is the only query in the set that filters on deleted_at.', skipIf: (ctx) => (ctx.samplePubkey ? undefined : 'no events rows found'), build: (ctx) => ctx.db('events') From 9a460b19fd6b80d66751fae588a0864640c685a8 Mon Sep 17 00:00:00 2001 From: chappie-daemon Date: Sun, 29 Nov 2026 11:43:17 -0500 Subject: [PATCH 3/3] docs(benchmarks): the retention sweep reads deleted_at too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CodeRabbit caught the claim: the vanish query is the only one reading deleted_at IS NULL, not the only one touching the column — the sweep at :225 reads it with IS NOT NULL. --- src/scripts/benchmark-queries.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scripts/benchmark-queries.ts b/src/scripts/benchmark-queries.ts index 49d85988c..d0d104bdf 100644 --- a/src/scripts/benchmark-queries.ts +++ b/src/scripts/benchmark-queries.ts @@ -207,7 +207,7 @@ function buildCases(): BenchmarkCase[] { { name: 'hasActiveRequestToVanish (pubkey + kind=62 + not deleted)', description: - 'Exact query `UserRepository.isVanished` runs when a pubkey has no users row, on the master so a lagging replica cannot decide what is stored; it is not on the per-message path. This is the only query in the set that filters on deleted_at.', + 'Exact query `UserRepository.isVanished` runs when a pubkey has no users row, on the master so a lagging replica cannot decide what is stored; it is not on the per-message path. This is the only query in the set that reads `deleted_at IS NULL`; the retention sweep reads the same column with `IS NOT NULL`.', skipIf: (ctx) => (ctx.samplePubkey ? undefined : 'no events rows found'), build: (ctx) => ctx.db('events')