Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.
TypeScript
feat(agent): stop suggesting a URL that already matches the field (#120)
github-actions[bot]ed43055be288
diff --git a/apps/agent/agent/lib/blank-facts.ts b/apps/agent/agent/lib/blank-facts.tsindex c3e3d8a4..f0595249 100644--- a/apps/agent/agent/lib/blank-facts.ts+++ b/apps/agent/agent/lib/blank-facts.ts@@ -1,5 +1,6 @@import { db, FactStatus } from "@crm/db";import {+ canonicalValue,type FactField,type FactSubject,factColumn,@@ -178,11 +179,12 @@ function groupByField(proposals: Proposal[]): [Proposal, ...Proposal[]][] {}function redundant(group: Proposal[], value: string | null): Proposal[] {+ const onRecord = value ? canonicalValue(value) : null;const kept = new Set<string>();return group.filter((row) => {- const seen = row.value.trim().toLowerCase();- if (value && seen === value.trim().toLowerCase()) return true;+ const seen = canonicalValue(row.value);+ if (seen === onRecord) return true;if (kept.has(seen)) return true;kept.add(seen);diff --git a/apps/agent/agent/lib/facts.ts b/apps/agent/agent/lib/facts.tsindex 83bda180..ebcc02e1 100644--- a/apps/agent/agent/lib/facts.ts+++ b/apps/agent/agent/lib/facts.ts@@ -345,6 +345,32 @@ function isEmpty({return !contact[column];}-function sameValue(a: string, b: string): boolean {- return a.trim().toLowerCase() === b.trim().toLowerCase();+const HOST_ALIASES: Record<string, string> = {+ "twitter.com": "x.com",+ "mobile.twitter.com": "x.com",+};++export function sameValue(a: string, b: string): boolean {+ return canonicalValue(a) === canonicalValue(b);+}++export function canonicalValue(value: string): string {+ const text = value.trim().replace(/\s+/g, " ").toLowerCase();+ const url = asWebUrl(text);++ if (!url) return text;++ const host = url.host.replace(/^www\./, "");+ const path = url.pathname.replace(/\/+$/, "");++ return `${HOST_ALIASES[host] ?? host}${path}`;+}++function asWebUrl(value: string): URL | null {+ try {+ const url = new URL(value);+ return url.protocol === "http:" || url.protocol === "https:" ? url : null;+ } catch {+ return null;+ }}diff --git a/apps/agent/test/blank-facts.integration.spec.ts b/apps/agent/test/blank-facts.integration.spec.tsindex 97a371d5..41267736 100644--- a/apps/agent/test/blank-facts.integration.spec.ts+++ b/apps/agent/test/blank-facts.integration.spec.ts@@ -112,6 +112,25 @@ describe("sweepBlankFacts", () => {expect(await statusOf(echo)).toBe("SUPERSEDED");});+ it("clears a suggestion that differs only by a trailing slash", async () => {+ await db.contact.update({+ where: { id: contactId },+ data: { linkedinUrl: "https://www.linkedin.com/in/pogrebs" },+ });++ const echo = await propose({+ field: "linkedinUrl",+ value: "https://www.linkedin.com/in/pogrebs/",+ score: 0.61,+ });++ const sweep = await sweepBlankFacts();++ expect(sweep.settled).toBe(1);+ expect(sweep.waiting).toBe(0);+ expect(await statusOf(echo)).toBe("SUPERSEDED");+ });+it("keeps one of two suggestions offering the same value", async () => {await db.contact.update({where: { id: contactId },diff --git a/apps/agent/test/same-value.spec.ts b/apps/agent/test/same-value.spec.tsnew file mode 100644index 00000000..fa9cf6f1--- /dev/null+++ b/apps/agent/test/same-value.spec.ts@@ -0,0 +1,70 @@+import { describe, expect, it } from "bun:test";+import { canonicalValue, sameValue } from "../agent/lib/facts";++describe("sameValue", () => {+ it("ignores a trailing slash", () => {+ expect(+ sameValue(+ "https://www.linkedin.com/in/pogrebs",+ "https://www.linkedin.com/in/pogrebs/",+ ),+ ).toBe(true);+ });++ it("ignores www, the scheme and case", () => {+ expect(+ sameValue(+ "http://LinkedIn.com/in/Pogrebs/",+ "https://www.linkedin.com/in/pogrebs",+ ),+ ).toBe(true);+ });++ it("reads twitter.com and x.com as the same account", () => {+ expect(+ sameValue("https://twitter.com/pogrebs", "https://x.com/pogrebs/"),+ ).toBe(true);+ });++ it("still separates two different profiles", () => {+ expect(+ sameValue(+ "https://www.linkedin.com/in/pogrebs",+ "https://www.linkedin.com/in/pogrebs-2",+ ),+ ).toBe(false);+ });++ it("does not merge two hosts that only look alike", () => {+ expect(+ sameValue("https://github.com/pogrebs", "https://gitlab.com/pogrebs"),+ ).toBe(false);+ });++ it("collapses the whitespace in a plain value", () => {+ expect(sameValue(" CEO & Co-founder ", "ceo & co-founder")).toBe(true);+ });++ it("leaves a plain value otherwise alone", () => {+ expect(sameValue("Head of Security", "Head of Marketing")).toBe(false);+ });++ it("drops a query string, because a profile is its path", () => {+ expect(canonicalValue("https://x.com/pogrebs?lang=en")).toBe(+ "x.com/pogrebs",+ );+ expect(+ sameValue(+ "https://www.linkedin.com/in/pogrebs?originalSubdomain=uk",+ "https://www.linkedin.com/in/pogrebs",+ ),+ ).toBe(true);+ });++ it("compares a value that is not a URL as text", () => {+ expect(canonicalValue("CEO & Co-founder")).toBe("ceo & co-founder");+ expect(canonicalValue("mailto:[email protected]")).toBe(+ "mailto:[email protected]",+ );+ });+});diff --git a/docs/agent.md b/docs/agent.mdindex 79d7f4c8..5e388d97 100644--- a/docs/agent.md+++ b/docs/agent.md@@ -155,7 +155,14 @@ wrong in the direction that looks useful.same blank, and the sheet shows one at a time — so left alone, accepting one revealsthe next, forever. The same rule holds when a rep accepts one (`decideFact`).- **The same value is never offered twice.** A second `PROPOSED` row with a value- already waiting is refused at the write path, not deduplicated on read. **The refusal+ already waiting is refused at the write path, not deduplicated on read.+- **"The same value" is `sameValue`, and a URL is compared as an address, not a+ string.** `canonicalValue` lowercases, collapses whitespace, and for `http(s)` drops+ the scheme, `www.`, a trailing slash and the query, and reads `twitter.com` as+ `x.com`. Compared byte for byte, `…/in/pogrebs/` is not `…/in/pogrebs`, so the record+ showed a suggestion offering back the URL already in the field — the exact click this+ whole rule exists to remove. **The stored value is still what the source said**; only+ the comparison normalises. **The refusalis for offers only** — the check runs after `applies` is decided, so evidence that hasreached `VERIFIED` since the offer was made still lands, settles the suggestion itmatches, and replaces the older value. Refusing it there left a weaker value on the