Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. π₯
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
stable opaque event keys to prevent label-drift duplicate alerts
Developers Digestbd4a621406ba
diff --git a/apps/api/src/services/monitoring/search/run.test.ts b/apps/api/src/services/monitoring/search/run.test.tsindex 942f73e75..0e44f7d65 100644--- a/apps/api/src/services/monitoring/search/run.test.ts+++ b/apps/api/src/services/monitoring/search/run.test.ts@@ -213,7 +213,7 @@ describe("runSearchTarget orchestration", () => {reason: "",}).mockResolvedValueOnce({- matchedKey: undefined,+ matchedKey: "00000000-0000-7000-8000-000000000000",isNew: false,label: "OpenAI IPO",reason: "same",@@ -227,6 +227,54 @@ describe("runSearchTarget orchestration", () => {expect(out.matches).toBe(1);});+ it("mints a stable opaque key for new events (not a label slug)", async () => {+ setSearchResults([+ {+ url: "https://sec.gov/openai",+ title: "OpenAI S-1",+ description: "filing",+ },+ ]);+ setVerdictsByUrl({ "https://sec.gov/openai": verdict() });+ resolveEventMock.mockResolvedValue({+ matchedKey: null,+ isNew: true,+ label: "OpenAI's IPO Filing!!!",+ reason: "",+ });+ const out = await run();+ // Key is the uuid (mocked constant here), not slugify("OpenAI's IPO Filing!!!").+ expect(out.sources[0].eventKey).toBe(+ "00000000-0000-7000-8000-000000000000",+ );+ expect(out.pageUpserts[0].metadata.eventLabel).toBe(+ "OpenAI's IPO Filing!!!",+ );+ });++ it("reuses a matched event's key even when the new label differs (no drift)", async () => {+ setSearchResults([+ {+ url: "https://reuters.com/openai",+ title: "Reuters: OpenAI IPO",+ description: "report",+ },+ ]);+ setVerdictsByUrl({ "https://reuters.com/openai": verdict() });+ resolveEventMock.mockResolvedValue({+ matchedKey: "evt-stable-123",+ isNew: false,+ label: "A completely reworded label",+ reason: "same event",+ });+ const out = await run({+ knownEvents: [{ key: "evt-stable-123", label: "OpenAI IPO" }],+ });+ expect(out.sources[0].eventKey).toBe("evt-stable-123");+ // Reuses the stored label, ignoring the drifted one from this run.+ expect(out.pageUpserts[0].metadata.eventLabel).toBe("OpenAI IPO");+ });+it("downgrades a stale-but-alert verdict to watch (no notify)", async () => {setSearchResults([{diff --git a/apps/api/src/services/monitoring/search/run.ts b/apps/api/src/services/monitoring/search/run.tsindex aba06cbad..7e68faad9 100644--- a/apps/api/src/services/monitoring/search/run.ts+++ b/apps/api/src/services/monitoring/search/run.ts@@ -24,16 +24,6 @@ function windowToTbs(window: string): string {return "qdr:w"; // 7d}-function slugifyEvent(label: string): string {- return (- label- .toLowerCase()- .replace(/[^a-z0-9]+/g, "-")- .replace(/^-+|-+$/g, "")- .slice(0, 64) || "event"- );-}-type SearchTargetInput = {id: string;queries: string[];@@ -280,22 +270,26 @@ export async function runSearchTarget(params: {continue;}- // notify β resolve the real-world event against recent known events.+ // notify β resolve the real-world event against the known events (small list, handed straight+ // to the LLM). Matches reuse the existing stable key; new events mint one.const resolution = await resolveEvent({goal,subject,- result: { title: c.title, url: c.url, evidence: verdict.rationale },+ result: {+ title: c.title,+ url: c.url,+ evidence: effectiveVerdict.rationale,+ },candidates: events,});- const eventKey =- resolution.matchedKey ??- (resolution.label- ? slugifyEvent(resolution.label)- : slugifyEvent(verdict.concept));- const eventLabel =- events.find(e => e.key === eventKey)?.label ||- resolution.label ||- verdict.concept;+ const matched =+ resolution.matchedKey &&+ events.find(e => e.key === resolution.matchedKey);+ // New event β mint a stable, content-independent key so a future relabel can't drift it.+ const eventKey = matched ? matched.key : uuidv7();+ const eventLabel = matched+ ? matched.label+ : resolution.label || effectiveVerdict.concept;const alreadySatisfied =target.alertMode === "first_match" && satisfied.has(eventKey);