Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. πŸ”₯

search scope: allow include+exclude together (exclude wins, enforced on results); keep event resolver window on recent stories

Developers Digestaa1e7d511610
diff --git a/apps/api/src/services/monitoring/search/persist.test.ts b/apps/api/src/services/monitoring/search/persist.test.ts
index 3e511e809..0f1d2e855 100644
--- a/apps/api/src/services/monitoring/search/persist.test.ts
+++ b/apps/api/src/services/monitoring/search/persist.test.ts
@@ -106,6 +106,24 @@ describe("reconstructKnownState", () => {
expect(knownEvents).toEqual([{ key: "e1", label: "e1" }]);
});
+ it("orders events most-recently-seen first (resolver window holds active stories)", () => {
+ const page = (url: string, eventKey: string, updated_at: string) => ({
+ url,
+ metadata: { fingerprint: "f", goalVersion: "gv1", eventKey, eventLabel: eventKey },
+ updated_at,
+ });
+ const { knownEvents } = reconstructKnownState(
+ [
+ page("https://a.com", "old-story", "2026-01-01T00:00:00Z"),
+ page("https://b.com", "active-story", "2026-01-02T00:00:00Z"),
+ // a newer article re-touches the old story β€” it should outrank active-story
+ page("https://c.com", "old-story", "2026-06-01T00:00:00Z"),
+ ],
+ "gv1",
+ );
+ expect(knownEvents.map(e => e.key)).toEqual(["old-story", "active-story"]);
+ });
+
it("ignores pages with no usable metadata", () => {
const { knownPages, knownEvents } = reconstructKnownState(
[
diff --git a/apps/api/src/services/monitoring/search/persist.ts b/apps/api/src/services/monitoring/search/persist.ts
index c99f388c0..f3f2a0c51 100644
--- a/apps/api/src/services/monitoring/search/persist.ts
+++ b/apps/api/src/services/monitoring/search/persist.ts
@@ -30,7 +30,7 @@ export function reconstructKnownState(
goalVersion: string,
): { knownPages: Map<string, KnownPage>; knownEvents: KnownEvent[] } {
const knownPages = new Map<string, KnownPage>();
- const eventsByKey = new Map<string, KnownEvent>();
+ const eventsByKey = new Map<string, KnownEvent & { lastSeenAt: string }>();
for (const page of priorPages) {
const meta = (page.metadata ?? {}) as Record<string, unknown>;
if (
@@ -44,17 +44,28 @@ export function reconstructKnownState(
lastStatus: page.last_status,
});
}
- if (
- meta.goalVersion === goalVersion &&
- typeof meta.eventKey === "string" &&
- !eventsByKey.has(meta.eventKey)
- ) {
- eventsByKey.set(meta.eventKey, {
- key: meta.eventKey,
- label:
- typeof meta.eventLabel === "string" ? meta.eventLabel : meta.eventKey,
- });
+ if (meta.goalVersion === goalVersion && typeof meta.eventKey === "string") {
+ const existing = eventsByKey.get(meta.eventKey);
+ const seenAt = page.updated_at ?? "";
+ if (!existing) {
+ eventsByKey.set(meta.eventKey, {
+ key: meta.eventKey,
+ label:
+ typeof meta.eventLabel === "string"
+ ? meta.eventLabel
+ : meta.eventKey,
+ lastSeenAt: seenAt,
+ });
+ } else if (seenAt > existing.lastSeenAt) {
+ existing.lastSeenAt = seenAt;
+ }
}
}
- return { knownPages, knownEvents: [...eventsByKey.values()] };
+ // Most-recently-seen first: the event resolver truncates this list to ~20
+ // candidates, and active stories must stay inside that window or every new
+ // article about them mints a duplicate event (and a duplicate alert).
+ const knownEvents = [...eventsByKey.values()]
+ .sort((a, b) => (a.lastSeenAt < b.lastSeenAt ? 1 : -1))
+ .map(({ key, label }) => ({ key, label }));
+ return { knownPages, knownEvents };
}
diff --git a/apps/api/src/services/monitoring/search/run.test.ts b/apps/api/src/services/monitoring/search/run.test.ts
index 29b2fabbb..1167d5ba8 100644
--- a/apps/api/src/services/monitoring/search/run.test.ts
+++ b/apps/api/src/services/monitoring/search/run.test.ts
@@ -544,4 +544,42 @@ describe("domain scoping", () => {
const sentQuery = searchMock.mock.calls[0][0].query as string;
expect(sentQuery).toContain("-site:pinterest.com");
});
+
+ it("includeDomains and excludeDomains combine; exclude wins in the query", async () => {
+ await run({
+ target: {
+ includeDomains: ["reuters.com", "spam.example"],
+ excludeDomains: ["spam.example"],
+ } as Partial<typeof baseTarget>,
+ });
+ const sentQuery = searchMock.mock.calls[0][0].query as string;
+ expect(sentQuery).toContain("(site:reuters.com OR site:spam.example)");
+ expect(sentQuery).toContain("-site:spam.example");
+ });
+
+ it("excluded hosts returned by the provider are dropped before judging", async () => {
+ setSearchResults([
+ { url: "https://www.pinterest.com/pin/1", title: "pin", description: "x" },
+ { url: "https://boards.pinterest.com/pin/2", title: "pin", description: "x" },
+ { url: "https://news.com/openai", title: "OpenAI files S-1", description: "x" },
+ ]);
+ setVerdictsByUrl({ "https://news.com/openai": verdict() });
+ const out = await run({
+ target: { excludeDomains: ["pinterest.com"] } as Partial<typeof baseTarget>,
+ });
+ expect(out.resultCount).toBe(1);
+ expect(out.sources.map(s => s.url)).toEqual(["https://news.com/openai"]);
+ expect(scrapeURLMock).toHaveBeenCalledTimes(1);
+ });
+
+ it("a host merely containing an excluded domain is not dropped", async () => {
+ setSearchResults([
+ { url: "https://notpinterest.com/a", title: "t", description: "x" },
+ ]);
+ setVerdictsByUrl({ "https://notpinterest.com/a": verdict() });
+ const out = await run({
+ target: { excludeDomains: ["pinterest.com"] } as Partial<typeof baseTarget>,
+ });
+ expect(out.resultCount).toBe(1);
+ });
});
diff --git a/apps/api/src/services/monitoring/search/run.ts b/apps/api/src/services/monitoring/search/run.ts
index b8378c1b7..c7c888c6f 100644
--- a/apps/api/src/services/monitoring/search/run.ts
+++ b/apps/api/src/services/monitoring/search/run.ts
@@ -29,6 +29,29 @@ function windowToTbs(window: string): string {
return "qdr:w"; // 7d
}
+// Exclude wins over include (Exa/Parallel semantics). The -site: operators in
+// the scoped query are advisory β€” not every search provider honors them β€” so
+// the blocklist is also enforced here on the returned results.
+export function isExcludedDomain(
+ url: string,
+ excludeDomains: string[] | undefined,
+): boolean {
+ if (!excludeDomains?.length) return false;
+ let host: string;
+ try {
+ host = new URL(url).hostname.toLowerCase().replace(/^www\./, "");
+ } catch {
+ return false;
+ }
+ return excludeDomains.some(domain => {
+ const normalized = domain.trim().toLowerCase().replace(/^www\./, "");
+ return (
+ normalized.length > 0 &&
+ (host === normalized || host.endsWith(`.${normalized}`))
+ );
+ });
+}
+
type SearchTargetInput = {
id: string;
queries: string[];
@@ -125,6 +148,7 @@ export async function runSearchTarget(params: {
});
for (const r of results) {
if (!r.url) continue;
+ if (isExcludedDomain(r.url, target.excludeDomains)) continue;
const canonical = canonicalizeUrl(r.url);
if (seenThisRun.has(canonical)) continue;
seenThisRun.add(canonical);
@@ -336,7 +360,9 @@ export async function runSearchTarget(params: {
matches += 1;
satisfied.add(eventKey);
if (!events.some(e => e.key === eventKey)) {
- events.push({ key: eventKey, label: eventLabel });
+ // Newest first: the resolver only sees the first ~20 candidates, and an
+ // event minted earlier in this same run must be visible to later results.
+ events.unshift({ key: eventKey, label: eventLabel });
}
sources.push({
url: c.url,
diff --git a/apps/api/src/services/monitoring/types.ts b/apps/api/src/services/monitoring/types.ts
index c0e29f7b8..f4f50cf0b 100644
--- a/apps/api/src/services/monitoring/types.ts
+++ b/apps/api/src/services/monitoring/types.ts
@@ -66,11 +66,7 @@ const searchTargetSchema = z
recheckAfter: z.enum(["1h", "6h", "24h", "7d"]).optional(),
maxResults: z.number().int().min(1).max(50).optional().default(10),
scrapeOptions: scrapeOptionsSchema,
- })
- .refine(
- t => !(t.includeDomains?.length && t.excludeDomains?.length),
- "includeDomains and excludeDomains cannot both be specified",
- );
+ });
const monitorTargetSchema = z.union([
scrapeTargetSchema,