Fase 1b: cableado del matcher (ingesta polling, runner shadow/full, comparador)

- config: bloque matcher (MATCHER_MODE off/shadow/full, IRON_PASSWORD, audiencia,
  poll, batch) + validacion
- db: colecciones subscriptions/activefires/notifications_shadow/matcher_state +
  ensureMatcherIndexes (2dsphere en notifications_shadow para el dedupe 500m)
- matcher/context: MatchContext sobre Mongo real (candidatas geo $near 1000km +
  audiencia; dedupe geo $near 500m; lang del owner)
- matcher/ingest: polling de activefires por createdAt>checkpoint (Mongo 3.2 sin
  change streams), checkpoint persistido en matcher_state
- matcher/runner: loop shadow(->notifications_shadow)/full(->notifications),
  idempotente por dedupe+checkpoint
- matcher/compare: comparador de shadow (shadowOnly=spam, realOnly=faltantes,
  contentMismatches) — el gate antes del cutover
- index: arranca el matcher; fake-repo ampliado (upsert, sort, $gt/$lt)
- docs/env/README actualizados

73 tests en verde, typecheck y build OK.
This commit is contained in:
vjrj 2026-07-13 18:07:06 +02:00
parent b3aeb6108f
commit a1ed206985
13 changed files with 531 additions and 19 deletions

View file

@ -47,8 +47,12 @@ function matchField(docVal: any, cond: any): boolean {
return Array.isArray(v) && v.some((x) => valEq(docVal, x));
case '$gte':
return docVal != null && docVal >= (v as any);
case '$gt':
return docVal != null && docVal > (v as any);
case '$lte':
return docVal != null && docVal <= (v as any);
case '$lt':
return docVal != null && docVal < (v as any);
case '$exists':
return (docVal !== undefined) === v;
default:
@ -70,6 +74,7 @@ function applyUpdate(doc: Doc, update: Doc): void {
class FakeCollection {
docs: Doc[] = [];
collectionName = 'fake';
private uniqueFields: string[] | null = null;
async createIndex(spec: Doc, opts?: { unique?: boolean }): Promise<string> {
@ -102,11 +107,19 @@ class FakeCollection {
return this.docs.find((d) => matches(d, filter)) ?? null;
}
async updateOne(filter: Doc, update: Doc): Promise<{ modifiedCount: number }> {
async updateOne(filter: Doc, update: Doc, opts?: { upsert?: boolean }): Promise<{ modifiedCount: number; upsertedCount: number }> {
const d = this.docs.find((x) => matches(x, filter));
if (!d) return { modifiedCount: 0 };
if (!d) {
if (opts?.upsert) {
const created: Doc = { ...filter };
applyUpdate(created, update);
this.docs.push(created);
return { modifiedCount: 0, upsertedCount: 1 };
}
return { modifiedCount: 0, upsertedCount: 0 };
}
applyUpdate(d, update);
return { modifiedCount: 1 };
return { modifiedCount: 1, upsertedCount: 0 };
}
async updateMany(filter: Doc, update: Doc): Promise<{ modifiedCount: number }> {
@ -138,6 +151,16 @@ class FakeCollection {
project() {
return cursor;
},
sort(spec: Doc) {
const [field, dir] = Object.entries(spec)[0] as [string, number];
result = [...result].sort((a, b) => {
const av = a[field];
const bv = b[field];
if (av === bv) return 0;
return (av < bv ? -1 : 1) * (dir < 0 ? -1 : 1);
});
return cursor;
},
limit(n: number) {
result = result.slice(0, n);
return cursor;
@ -151,15 +174,16 @@ class FakeCollection {
}
export function makeRepos(): Repos {
const notifications = new FakeCollection();
const users = new FakeCollection();
const sends = new FakeCollection();
return {
client: {} as any,
db: {} as any,
notifications: notifications as any,
users: users as any,
sends: sends as any,
notifications: new FakeCollection() as any,
users: new FakeCollection() as any,
sends: new FakeCollection() as any,
subscriptions: new FakeCollection() as any,
activefires: new FakeCollection() as any,
notificationsShadow: new FakeCollection() as any,
matcherState: new FakeCollection() as any,
close: async () => {},
};
}