Skip to content

armgit / crm

Public Imported from GitHub
0

Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.

TypeScript

feat(api): enhance email domain handling with machine address detection

Lewis Carhart70d7e84b6532
diff --git a/apps/api/src/companies/domain.ts b/apps/api/src/companies/domain.ts
index 0d8a04af..35f95ec3 100644
--- a/apps/api/src/companies/domain.ts
+++ b/apps/api/src/companies/domain.ts
@@ -26,7 +26,20 @@ export function domainFromEmail(
const at = email?.trim().toLowerCase().lastIndexOf("@") ?? -1;
if (at < 1) return null;
const domain = normalizeDomain(email?.slice(at + 1));
- return domain && !FREE_EMAIL_DOMAINS.has(domain) ? domain : null;
+ if (!domain) return null;
+ return FREE_EMAIL_DOMAINS.has(domain) || isMachineDomain(domain)
+ ? null
+ : domain;
+}
+
+export function isMachineDomain(input: string | null | undefined): boolean {
+ const domain = normalizeDomain(input);
+ if (!domain) return false;
+
+ return (
+ MACHINE_DOMAINS.has(domain) ||
+ MACHINE_SUFFIXES.some((suffix) => domain.endsWith(suffix))
+ );
}
const FREE_EMAIL_DOMAINS = new Set([
@@ -52,3 +65,25 @@ const FREE_EMAIL_DOMAINS = new Set([
"qq.com",
"163.com",
]);
+
+const MACHINE_DOMAINS = new Set([
+ "calendar.google.com",
+ "googlegroups.com",
+ "docs.google.com",
+ "drive.google.com",
+ "appspotmail.com",
+ "amazonses.com",
+ "sendgrid.net",
+ "zoomcrc.com",
+]);
+
+const MACHINE_SUFFIXES = [
+ ".calendar.google.com",
+ ".bounces.google.com",
+ ".appspotmail.com",
+ ".amazonses.com",
+ ".sendgrid.net",
+ ".invalid",
+ ".local",
+ ".localhost",
+];
diff --git a/apps/api/src/google/calendar-sync.service.ts b/apps/api/src/google/calendar-sync.service.ts
index 2e120e9b..43b69ab9 100644
--- a/apps/api/src/google/calendar-sync.service.ts
+++ b/apps/api/src/google/calendar-sync.service.ts
@@ -17,7 +17,7 @@ import {
} from "./calendar.client";
import { GoogleMatchService, type MatchContext } from "./google-match.service";
import { GoogleTokenService } from "./google-token.service";
-import type { Participant } from "./participants";
+import { isMachineAddress, type Participant } from "./participants";
import { SyncStateService } from "./sync-state.service";
const MAX_PAGES_PER_TICK = 5;
@@ -294,7 +294,10 @@ export class CalendarSyncService {
event: GoogleEvent,
): Promise<void> {
const attendees = (event.attendees ?? []).filter(
- (attendee) => attendee.email && !attendee.resource,
+ (attendee) =>
+ attendee.email &&
+ !attendee.resource &&
+ !isMachineAddress(attendee.email.toLowerCase()),
);
if (attendees.length === 0) return;
diff --git a/apps/api/src/google/participants.ts b/apps/api/src/google/participants.ts
index a87497ed..fcb9c1dc 100644
--- a/apps/api/src/google/participants.ts
+++ b/apps/api/src/google/participants.ts
@@ -1,4 +1,4 @@
-import { domainFromEmail } from "../companies/domain";
+import { domainFromEmail, isMachineDomain } from "../companies/domain";
export type Participant = {
email: string;
@@ -104,6 +104,21 @@ export function isAutomatedAddress(email: string): boolean {
);
}
+const OPAQUE_LOCAL_PARTS = [
+ /^(c_)?[0-9a-f]{24,}$/,
+ /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,
+];
+
+export function isMachineAddress(email: string): boolean {
+ const at = email.lastIndexOf("@");
+ if (at < 1) return true;
+
+ if (isMachineDomain(email.slice(at + 1))) return true;
+
+ const local = email.slice(0, at).toLowerCase();
+ return OPAQUE_LOCAL_PARTS.some((pattern) => pattern.test(local));
+}
+
export function isDerivedName(
email: string,
firstName: string,
@@ -133,6 +148,7 @@ export function externalParticipants(
return participants.filter((participant) => {
if (options.ourAddresses.has(participant.email)) return false;
if (options.suppressedEmails.has(participant.email)) return false;
+ if (isMachineAddress(participant.email)) return false;
const domain = workDomain(participant.email);
if (!domain) return false;
diff --git a/apps/api/test/domain.spec.ts b/apps/api/test/domain.spec.ts
index 8ddf92fe..343a7aef 100644
--- a/apps/api/test/domain.spec.ts
+++ b/apps/api/test/domain.spec.ts
@@ -1,5 +1,9 @@
import { describe, expect, it } from "bun:test";
-import { domainFromEmail, normalizeDomain } from "../src/companies/domain";
+import {
+ domainFromEmail,
+ isMachineDomain,
+ normalizeDomain,
+} from "../src/companies/domain";
describe("normalizeDomain", () => {
it("reduces anything a human might type to the bare host", () => {
@@ -49,4 +53,23 @@ describe("domainFromEmail", () => {
expect(domainFromEmail(email)).toBeNull();
}
});
+
+ it("never derives a company from infrastructure", () => {
+ for (const email of [
+ ]) {
+ expect(domainFromEmail(email)).toBeNull();
+ }
+ });
+});
+
+describe("isMachineDomain", () => {
+ it("is false for a real host and for nothing", () => {
+ expect(isMachineDomain("stripe.com")).toBe(false);
+ expect(isMachineDomain("calendar.acme.com")).toBe(false);
+ expect(isMachineDomain(null)).toBe(false);
+ expect(isMachineDomain("")).toBe(false);
+ });
});
diff --git a/apps/api/test/google-participants.spec.ts b/apps/api/test/google-participants.spec.ts
index fef0ca08..a201e178 100644
--- a/apps/api/test/google-participants.spec.ts
+++ b/apps/api/test/google-participants.spec.ts
@@ -4,6 +4,7 @@ import {
externalParticipants,
isAutomatedAddress,
isDerivedName,
+ isMachineAddress,
type Participant,
parseAddress,
parseAddressList,
@@ -80,6 +81,53 @@ describe("isAutomatedAddress", () => {
});
});
+describe("isMachineAddress", () => {
+ it("catches the calendars Google invites as though they were people", () => {
+ for (const email of [
+ ]) {
+ expect(isMachineAddress(email)).toBe(true);
+ }
+ });
+
+ it("catches sending infrastructure and reserved hosts", () => {
+ for (const email of [
+ ]) {
+ expect(isMachineAddress(email)).toBe(true);
+ }
+ });
+
+ it("catches an address that is an opaque identifier", () => {
+ for (const email of [
+ ]) {
+ expect(isMachineAddress(email)).toBe(true);
+ }
+ });
+
+ it("leaves real people and real companies alone", () => {
+ for (const email of [
+ ]) {
+ expect(isMachineAddress(email)).toBe(false);
+ }
+ });
+});
+
describe("workDomain", () => {
it("rejects the free hosts", () => {
expect(workDomain("[email protected]")).toBeNull();
@@ -126,6 +174,21 @@ describe("externalParticipants", () => {
expect(result).toEqual([person("[email protected]")]);
});
+ it("never files a shared calendar as a person at a company", () => {
+ const result = externalParticipants(
+ [
+ person(
+ "Interviews scheduled",
+ ),
+ person("[email protected]"),
+ ],
+ options,
+ );
+
+ expect(result).toEqual([person("[email protected]")]);
+ });
+
it("returns nothing for a wholly internal thread", () => {
expect(
externalParticipants([person("[email protected]")], options),
diff --git a/docs/api.md b/docs/api.md
index 516aefc9..437554b5 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -285,6 +285,52 @@ Everything the app reads or writes goes through `nestjs-trpc` routers under
run the generator. Regenerate locally and commit the result with the router
change that caused it.
+## Not every address on a thread is a person
+
+`externalParticipants` (`apps/api/src/google/participants.ts`) is the one gate
+between what Google returns and what becomes a record, and it has to throw away
+three different kinds of thing before it gets to a lead: **us** (the allow-list
+domains and the `User` table), **a decision a rep made** (`SuppressedContact`,
+`SuppressedDomain`), and **an address no human has ever read**.
+
+The third is `isMachineAddress`, and it exists because of a company called
+`group.calendar.google.com` with one contact on it named "Interviews
+scheduled". A secondary Google calendar is invited to an event as an ordinary
+attendee — it is not flagged `resource`, the way a meeting room is — so it
+arrived with a display name and a plausible-looking domain and the sync did
+exactly what it does for a stranger at a customer: made a person, made a
+company, and queued research on both.
+
+- **A machine domain never becomes a company.** `isMachineDomain` in
+ `apps/api/src/companies/domain.ts` sits beside `FREE_EMAIL_DOMAINS` because
+ the two answer the same question — *is this host a company* — and
+ `domainFromEmail` returns `null` for both. That is the load-bearing half:
+ `companyForEmail` is the only way a company is derived from an address, so a
+ caller that never heard of this rule still cannot create one.
+ `.calendar.google.com` covers the shared calendars, the rooms, the imported
+ ICS feeds and the holiday calendars in one entry.
+- **It matches the *host*, never a substring.** `calendar.acme.com` and
+ `sendgrid.com` are somebody's real company; only the exact hosts and the
+ listed suffixes are refused.
+- **An opaque local part is the second door.** `c_f5ec…@` and a bare UUID are
+ identifiers, not names, and they come from providers whose infrastructure
+ hostname we have not learned yet. The patterns are deliberately narrow — 24
+ hex characters or a formatted UUID — because the cost of a false positive
+ here is a real customer who is silently never filed.
+- **It is not a suppression, and it leaves no row.** A rep can still type any of
+ these into the quick-add form; the rule is only that the *inbox* may not
+ decide they are worth a record. `SuppressedContact` is for a person a rep
+ deleted, and writing one for a calendar id would be recording a decision
+ nobody made.
+- **The attendee list is filtered too.** `syncAttendees` drops the same
+ addresses beside `attendee.resource`, so a shared calendar is not listed as
+ somebody who came to the meeting.
+
+Shared inboxes and the machines that send invitations are a separate list,
+`isAutomatedAddress` — `sales@`, `noreply@`, `bookings@`. That one is about the
+*local part*, and it is the reason `[email protected]` never becomes a lead at a
+company we do genuinely sell to.
+
## Deleting a record is a decision the sync has to respect
Each of the three records can be deleted from the three-dot menu in its sheet —