Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): hold a chat's twins through a fold of its state
Yudhi Armyndharisd435619fe0dd
diff --git a/src/engine/adapters/baileys-chat-state-store.overlap.spec.ts b/src/engine/adapters/baileys-chat-state-store.overlap.spec.tsindex 5ce0b9a9a..0fa32380e 100644--- a/src/engine/adapters/baileys-chat-state-store.overlap.spec.ts+++ b/src/engine/adapters/baileys-chat-state-store.overlap.spec.ts@@ -1,5 +1,5 @@import { Repository } from 'typeorm';-import { ChatStateField, ChatStateStoreService } from './baileys-chat-state-store.service';+import { ChatStateField, ChatStateStoreService, mergeTwinStates } from './baileys-chat-state-store.service';import { ChatState } from './baileys-chat-state.entity';const KEY = (s: string, c: string): string => `${s}\u0000${c}`;@@ -217,26 +217,114 @@ describe('ChatStateStoreService loads that overlap a write, a forget or an unlinexpect(svc.get('s', 'c')?.archived).toBe(true);});- it('does not leave a written twin chat hidden behind a no-row answer a fold read before its write', async () => {- const t = scriptedRepo();- const svc = storeOf(t);- const fold = svc.fold('s', '[email protected]', ['l@lid'], { archived: true });- const write = svc.remember('s', 'l@lid', { pinned: true });- const reload = svc.reload();- const snapshot = await t.start('find'); // the reload reads the empty table- await t.run('findOne', 'l@lid'); // the write finds no row, caches its change and submits the upsert- await snapshot(); // the reload applies its answer and drops what the write cached- await reload;- await t.run('findOne', '[email protected]'); // the fold reads its own chat: no row- const twinRead = await t.start('findOne', 'l@lid'); // then its twin: still no row- await t.run('upsert', 'l@lid'); // the write lands- await twinRead(); // the old no-row answer arrives- await t.drain();- await Promise.all([fold, write]);- expect(t.rows.get(KEY('s', 'l@lid'))?.pinned).toBe(true);- svc.get('s', 'l@lid'); // a chat list read must warm from the table, not trust an absent mark- await t.drain();- expect(svc.get('s', 'l@lid')?.pinned).toBe(true);+ describe('a fold of a chat and a write of its twin', () => {+ const P = '[email protected]';+ const L = 'l@lid';+ /** The chat as a listing reads it: its rows merged field by field. */+ const view = (t: ReturnType<typeof scriptedRepo>) =>+ mergeTwinStates(+ [...t.rows.values()].map(r => ({+ muteEndTime: r.muteEndTime,+ archived: r.archived,+ pinned: r.pinned,+ updatedAt: r.updatedAt?.getTime(),+ observed: r.observed == null ? undefined : (r.observed.split(',').filter(Boolean) as ChatStateField[]),+ })),+ );++ it('does not leave a written twin chat hidden behind a no-row answer a fold read before its write', async () => {+ const t = scriptedRepo();+ const svc = storeOf(t);+ const fold = svc.fold('s', P, [L], { archived: true });+ const write = svc.remember('s', L, { pinned: true });+ const reload = svc.reload();+ const snapshot = await t.start('find'); // the reload reads the empty table+ await t.run('findOne', P); // the fold reads its own chat: no row+ await t.run('findOne', L); // then its twin: no row, and the write of the twin waits for the fold+ await snapshot();+ await reload;+ await t.drain();+ await Promise.all([fold, write]);+ expect(t.rows.get(KEY('s', L))?.pinned).toBe(true);+ svc.get('s', L); // a chat list read must warm from the table, not trust an absent mark+ await t.drain();+ expect(svc.get('s', L)?.pinned).toBe(true);+ });++ it('keeps what a fold merged when a write of the twin in the same tick changes nothing', async () => {+ const all = 'muteEndTime,archived,pinned';+ const t = scriptedRepo([+ { sessionId: 's', chatId: P, muteEndTime: 1234, observed: all, updatedAt: new Date(2) },+ { sessionId: 's', chatId: L, muteEndTime: 555, archived: true, observed: all, updatedAt: new Date(3) },+ ]);+ const svc = storeOf(t);+ const fold = svc.fold('s', P, [L], { pinned: true });+ const write = svc.remember('s', L, { archived: true }); // already archived+ await t.drain();+ await Promise.all([fold, write]);+ expect(view(t)).toMatchObject({ muteEndTime: 555, archived: true, pinned: true });+ });++ it('keeps a write of the twin that starts after the fold read the twin', async () => {+ const t = scriptedRepo([{ sessionId: 's', chatId: L, muteEndTime: 1234, observed: 'muteEndTime' }]);+ const svc = storeOf(t);+ const fold = svc.fold('s', P, [L], { archived: true });+ await t.run('findOne', P);+ await t.run('findOne', L); // the fold has read the twin, muted until 1234+ const write = svc.remember('s', L, { muteEndTime: 1001 });+ await t.drain();+ await Promise.all([fold, write]);+ expect(view(t)).toMatchObject({ muteEndTime: 1001, archived: true });+ });++ it.each([+ ['has a row', [{ sessionId: 's', chatId: L, archived: false, observed: 'archived', updatedAt: new Date(5) }]],+ ['has no row', []],+ ])('keeps a pin of the twin issued while the fold reads the twin, which %s', async (_name, twinRows) => {+ const t = scriptedRepo([+ { sessionId: 's', chatId: P, pinned: false, observed: 'pinned', updatedAt: new Date(3) },+ ...twinRows,+ ]);+ const svc = storeOf(t);+ const fold = svc.fold('s', P, [L], { muteEndTime: 5000 });+ await t.run('findOne', P);+ const twinRead = await t.start('findOne', L); // the twin is read, its answer is still out+ const write = svc.remember('s', L, { pinned: true });+ await twinRead();+ await t.drain();+ await Promise.all([fold, write]);+ expect(view(t)).toMatchObject({ pinned: true, muteEndTime: 5000 });+ });++ it('counts the twin of a fold as changing only until the fold ends', async () => {+ const t = scriptedRepo();+ const svc = storeOf(t);+ const fold = svc.fold('s', P, [L], { archived: true });+ await t.drain();+ await fold;+ const pin = svc.remember('s', L, { pinned: true });+ await t.drain();+ await pin;+ const reload = svc.reload();+ await t.drain();+ await reload;+ expect(svc.get('s', L)?.pinned).toBe(true); // a reload sets aside the row of a chat still changing+ });++ it("lets folds that name each other's chat as a twin, or one twin twice, all finish", async () => {+ const t = scriptedRepo([{ sessionId: 's', chatId: L, pinned: true, observed: 'pinned' }]);+ const svc = storeOf(t);+ let finished = false;+ void Promise.all([+ svc.fold('s', P, [L], { archived: true }),+ svc.fold('s', L, [P], { muteEndTime: 1 }),+ svc.fold('s', P, [L, L], { pinned: true }),+ ]).then(() => (finished = true));+ await t.drain(); // answers every query the folds make: one still waiting after that waits forever+ await settle();+ expect(finished).toBe(true);+ expect(view(t)).toMatchObject({ pinned: true, archived: true, muteEndTime: 1 });+ });});it('does not index an older read-through answer once cache pressure evicted the written chat', async () => {diff --git a/src/engine/adapters/baileys-chat-state-store.service.ts b/src/engine/adapters/baileys-chat-state-store.service.tsindex f7b778eb0..ab534733c 100644--- a/src/engine/adapters/baileys-chat-state-store.service.ts+++ b/src/engine/adapters/baileys-chat-state-store.service.ts@@ -44,7 +44,8 @@ export interface ChatStateStore {* Like {@link remember}, but first folds the rows filed under `twins` (other spellings of the chat)* onto `chatId`: all the rows, read from the table and merged by {@link mergeTwinStates}, are the* merge base, and the twin rows are deleted once the result is written. `create` applies as in- * {@link remember} when no twin row is found.+ * {@link remember} when no twin row is found. Writes of the twins wait for it, so none can land between+ * the read of a twin row and its delete.*/fold(sessionId: string,@@ -157,7 +158,10 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {private readonly sessionWrites = new Map<string, SessionWrites>();private warnedEviction = false;private readonly maxEntries: number;- /** One write chain per chat, so each remember() merges onto the state the previous one left. */+ /**+ * One write chain per chat, so each remember() merges onto the state the previous one left. A fold+ * holds the chains of its twins as well (see {@link enqueue}).+ */private readonly writes = new KeyedMutationQueue();/*** A load (reload, refreshSession, a read-through) reads the table while writes go on, so its rows can@@ -243,7 +247,7 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {remember(sessionId: string, chatId: string, patch: Partial<ChatStateValue>, create = true): Promise<void> {const k = this.key(sessionId, chatId);const generation = this.writesOf(sessionId).generation;- return this.enqueue(sessionId, k, async () => {+ return this.enqueue(sessionId, [k], async () => {await this.applyPatch(k, sessionId, chatId, patch, generation, create);});}@@ -257,21 +261,36 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {): Promise<void> {const k = this.key(sessionId, chatId);const generation = this.writesOf(sessionId).generation;- return this.enqueue(sessionId, k, () => this.applyFold(k, sessionId, chatId, twins, patch, generation, create));+ const keys = [...new Set([k, ...twins.map(twin => this.key(sessionId, twin))])];+ return this.enqueue(sessionId, keys, () => this.applyFold(k, sessionId, chatId, twins, patch, generation, create));}- /** Queues work on the chat's write chain and counts it under the session until it settles. */- private enqueue(sessionId: string, k: string, work: () => Promise<void>): Promise<void> {+ /**+ * Queues work on the write chain of each key and counts it under the session until it settles. The+ * work starts once it is first in line on all of them and holds them until it ends. Every chain is+ * joined in this one synchronous step, so they all order their work alike and two works sharing+ * chains can never each wait for the other; the work itself must not queue on a chain it holds.+ */+ private enqueue(sessionId: string, keys: string[], work: () => Promise<void>): Promise<void> {const { pending } = this.writesOf(sessionId);- this.inFlight.set(k, (this.inFlight.get(k) ?? 0) + 1);- // Settled inside the chain, before the work counts as done, so a load never sees the chat idle+ let release!: () => void;+ const held = new Promise<void>(resolve => (release = resolve));+ const turns = keys.map(k => {+ this.inFlight.set(k, (this.inFlight.get(k) ?? 0) + 1);+ return new Promise<void>(turn =>+ this.writes.enqueue(k, () => {+ turn();+ return held;+ }),+ );+ });+ // Settled inside the chains, before the work counts as done, so a load never sees the chat idle// without also seeing the change.- const done = new Promise<void>((resolve, reject) =>- this.writes.enqueue(k, () =>- work()- .finally(() => this.settled(k))- .then(resolve, reject),- ),+ const done = Promise.all(turns).then(() =>+ work().finally(() => {+ for (const k of keys) this.settled(k);+ release();+ }),);pending.add(done);const settle = () => pending.delete(done);@@ -363,19 +382,29 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {const merged = mergeTwinStates(rows)!;const base = Object.fromEntries(FIELDS.filter(f => saw(merged, f)).map(f => [f, merged[f]]));if (await this.applyPatch(k, sessionId, chatId, { ...base, ...patch }, generation)) {- await this.forget(sessionId, found);+ // Deleted here, not through forget(), which would queue behind this fold on the chains it holds.+ await Promise.all(+ found.map(twin => {+ const tk = this.key(sessionId, twin);+ this.states.delete(tk);+ this.markAbsent(tk);+ return this.drop(sessionId, twin);+ }),+ );}}- /** The row as the table holds it: the cache when it has the key or knows it absent, else a query. */+ /**+ * The row as the table holds it: the cache when it has the key or knows it absent, else a query. The+ * fold holds the chain of every key it reads, so no write or forget of the chat lands between the read+ * and its answer, and a miss is recorded like a listing's, or every fold of a lid-addressed chat would+ * query its twin again.+ */private async peek(k: string, sessionId: string, chatId: string): Promise<ChatStateValue | undefined> {const cached = this.states.get(k);if (cached || this.completeSessions.has(sessionId) || this.absent.has(k)) return cached;- const since = this.touchSeq;const row = await this.repo.findOne({ where: { sessionId, chatId } });- // Recorded like a listing's miss, or every fold of a lid-addressed chat queries its twin again; not when a- // write or forget of the chat came after the read began, which the answer cannot tell.- if (!row && !this.states.has(k) && !this.changedSince(k, since)) this.markAbsent(k);+ if (!row && !this.states.has(k)) this.markAbsent(k);return row ? fromRow(row) : undefined;}@@ -525,21 +554,25 @@ export class ChatStateStoreService implements ChatStateStore, OnModuleInit {const k = this.key(sessionId, chatId);this.states.delete(k);this.markAbsent(k);- return this.enqueue(sessionId, k, async () => {- try {- await this.repo.delete({ sessionId, chatId });- this.markAbsent(k);- } catch (err) {- this.logger.warn(- `Failed to forget chat state for ${chatId}: ${err instanceof Error ? err.message : String(err)}`,- );- }- this.states.delete(k);- });+ return this.enqueue(sessionId, [k], () => this.drop(sessionId, chatId));}),);}+ /** Deletes the chat's row; run with the chat's write chain held. */+ private async drop(sessionId: string, chatId: string): Promise<void> {+ const k = this.key(sessionId, chatId);+ try {+ await this.repo.delete({ sessionId, chatId });+ this.markAbsent(k);+ } catch (err) {+ this.logger.warn(+ `Failed to forget chat state for ${chatId}: ${err instanceof Error ? err.message : String(err)}`,+ );+ }+ this.states.delete(k);+ }+/*** Replaces the session's cached rows, the ones known absent included, with what the table holds now,* so the first chat list after a start already reads it; a row changed or deleted while another node