Flagship · Pilot · Private production data

Referral Desk

A compliance-first referral pipeline for a Farmers insurance agency: real-estate signals in, qualified and routed opportunities out, with the rules that must never break enforced by Postgres instead of by application code or rep discipline. This page is the evidence chain, not the pitch — every claim below links to a real artifact, and the parts an engineer can run are public on GitHub.

20qualified opportunities generated during the pilot
~$10Kwritten premium influenced, across policies closed by two producers
1,666checks in CI; deploys fail closed on security or compliance breakage

How the ~$10K is computed: listing detected → opportunity created → producer assigned → contact → quote → policy written → premium attributed. The attribution schema itself separates premium from revenue and stamps every policy with an attribution kind and a confidence level (confirmed / probable / assumed) — see the data-model artifact below. The underlying ledger contains client PII, so it is shown redacted in a live walkthrough rather than published here.

The problem, before

Before

  • New listing appears — nobody notices for days
  • Producer manually searches, assembles contact info by hand
  • No relationship context; follow-up is memory-driven
  • Compliance rules live in people’s heads
  • A policy closes and nobody can say which signal started it

With Referral Desk

  • Scheduled scans surface listing activity automatically
  • Deduped, qualified, scored, routed to an owning producer
  • Every touch logged append-only; state is explicit
  • Suppression, opt-out and sequence caps enforced by the database
  • Premium attributed back to the originating relationship

The system, stage by stage

Each stage opens to the real implementation behind it.

Listing signal → scheduled scan

pg_cron drives the cadence (hourly follow-up marking, daily priority refresh). Every run writes a scan_runs row with status running/success/error — job state is a visible table, not a lost message.

Normalization & duplicate detection

Duplicate checks classify matches across RLS visibility. The unit test documents the real bug that forced this: the old check consulted the caller’s own filtered read and reported “no duplicate” about a record it structurally could not see.

/** "NO DUPLICATE" ABOUT A RECORD IT COULD NOT SEE.
 *  The old check filtered matches through the caller's own RLS-limited
 *  read, so a match on a colleague's record vanished and the insert
 *  proceeded. Same inputs must now come back blocked:           */
expect(oldRouteWouldInsert(hits, readable)).toBe(true);   // the bug
expect(decision.blocked).toBe(true);                      // the fix
expect(decision.hidden[0].agent_id).toBe(HIDDEN_AGENT);
Territory match → producer routing

Ownership is a row-level rule: an agent record is editable by an admin, the assigned producer, or its creator — enforced in rls_policies.sql, not in route handlers.

Compliance gate (the part that cannot be bypassed)

Suppression, opt-out/DNC, template approval, and the one-initial + one-follow-up cap are raised as Postgres exceptions before the row exists. Application code cannot route around it; neither can I.

-- BEFORE INSERT ON outreach_events  (production migration, excerpt)
if v_agent.opted_out or v_agent.do_not_contact then
  raise exception 'send blocked: contact has opted out or is do-not-contact';
end if;
if is_email_suppressed(new.to_identity) then
  raise exception 'send blocked: recipient address is on the suppression list';
end if;
if v_template.status <> 'approved' then
  raise exception 'send blocked: template is not approved';
end if;
if new.template_kind = 'follow_up_email' and v_followups >= 1 then
  raise exception 'send blocked: the single permitted follow-up was already sent;
                   the sequence has ended permanently';
end if;
Human review

Marketing email requires an explicitly approved template version and a contact at approved_for_email. The system scales the motion; a person still makes the call.

Delivery webhooks (svix-verified)

This closed a HIGH finding from a security review: the webhook once accepted unsigned requests. Now the raw body is read first, the signature verified, invalid → 401. Fixed and re-verified in production Aug 2026.

// api/webhooks/resend — raw body FIRST, then signature, then parse
const valid = verifyResendWebhook({ secret,
  svixId: req.headers.get('svix-id'),
  svixTimestamp: req.headers.get('svix-timestamp'),
  svixSignature: req.headers.get('svix-signature'),
  rawBody });
if (!valid) {
  return NextResponse.json({ error: 'invalid signature' }, { status: 401 });
}
Append-only outreach log

Delivery status may update. History may not be rewritten, by anyone, ever.

if tg_op = 'DELETE' then
  raise exception 'outreach_events is append-only: DELETE is not permitted';
end if;
if new.body is distinct from old.body
  or new.to_identity is distinct from old.to_identity then
  raise exception 'outreach_events content is immutable; only delivery_status,
                   call_outcome, provider_message_id and meta may change';
end if;
Relationship state → policy attribution

The schema is the honesty mechanism: premium and revenue are separate columns, commission is derived, and every policy carries an attribution kind and a confidence level — confirmed, probable, or assumed. The ~$10K figure on this site is an influenced number and the data model refuses to let it masquerade as booked.

-- Core rule, enforced by the schema: PREMIUM AND REVENUE ARE NEVER
-- THE SAME NUMBER. A policy stores written premium AND the agency
-- commission rate separately; commission is derived, never conflated.
-- Every policy records HOW it is attributed and HOW SURE we are, so an
-- "influenced" number can never masquerade as a booked one.
create type attribution_kind as enum (
  'direct',              -- the professional's own policy
  'referred_household',  -- a client they referred
  ...);
create type attribution_confidence as enum ('confirmed','probable','assumed');

Five invariants, with the tests that hold them

The count is 1,666. The count is not the point — these properties are.

Tenant isolation

Agency A cannot touch Agency B rows — and session-less writes (cron, webhooks, unsubscribe) must state their tenant explicitly. This test ran red before the fix.

/** WHAT BREAKS THE DAY A SECOND AGENCY GOES LIVE.
 *  Every write below happens with NO user session — cron jobs, provider
 *  webhooks, the unsubscribe form. A second organization removes the
 *  tenant inference, and anything that never learned to state its
 *  tenant stops working. These ran red before the fix:
 *    credit ledger, inbound webhook audit log, unsubscribe/STOP.  */

tests/integration/twoTenantWrites.test.ts · runnable copy on GitHub

Duplicate prevention

Re-processing the same contact cannot create a second record, even when the duplicate is invisible to the caller under RLS.

/** "NO DUPLICATE" ABOUT A RECORD IT COULD NOT SEE.
 *  The old check filtered matches through the caller's own RLS-limited
 *  read, so a match on a colleague's record vanished and the insert
 *  proceeded. Same inputs must now come back blocked:           */
expect(oldRouteWouldInsert(hits, readable)).toBe(true);   // the bug
expect(decision.blocked).toBe(true);                      // the fix
expect(decision.hidden[0].agent_id).toBe(HIDDEN_AGENT);

tests/unit/dedupDecision.test.ts · runs green in the public repo CI

Suppression & sequence cap

A suppressed or opted-out contact cannot be emailed; one initial, at most one follow-up, then permanent stop. No template kind is a loophole.

it('blocks a second initial once one initial exists', async () => {
  const database = db({ priorEvents: [{ template_kind: 'initial_email' }] });
  const result = await checkEmailSendable(database, agent(), 'initial_email');
  expect(result.allowed).toBe(false);
  expect(result.reasons.join(' ')).toMatch(/already sent once/i);
});
it('a listing email counts as the initial touch too', ...);   // no loophole

tests/unit/sequenceRules.test.ts

Webhook integrity

Invalid signatures cannot mutate delivery state. Secrets are header-only and timing-safe, and the route fails closed when unconfigured.

it('does NOT accept a query-string secret (header-only)', () => {
  const r = new Request(`...tick?secret=${SECRET}`, {});
  expect(verifyBearerSecret(r, SECRET).ok).toBe(false);
});
it('fails closed (503) when the secret is unconfigured', () => {
  expect(verifyBearerSecret(req(auth), undefined))
    .toEqual({ ok:false, status:503, error:'secret not configured' });
});

tests/unit/webhookAuth.test.ts · runs green in the public repo CI

Invariants inside Postgres

pgTAP asserts the append-only rules in the database itself — including that even service_role holds no DELETE privilege.

select throws_ok(
  $$ update suppression_list set note = 'rewrite history' ... $$,
  'suppression_list is append-only: UPDATE is not permitted');
select ok(
  not has_table_privilege('service_role','suppression_list','DELETE'),
  'even service_role holds no DELETE privilege on suppression_list');

supabase/tests/database/compliance.test.sql

Incidents

Four real ones. Each is documented in the repo — two of them inside the very migration or test that fixed them.

Forged delivery webhooks (HIGH)

The Resend webhook accepted unsigned requests. Found in a security review, fixed with svix verification + raw-body-first parsing, re-verified in production Aug 24, 2026. The verifying route is in the public repo.

An outage recorded people as notified

emailed_at was stamped whether or not the provider accepted. Now it means provider-accepted only; attempts are counted and a repeatedly-failing row is abandoned with its reason recorded, so one dead address cannot wedge the queue.

The login guard had never run

The brute-force guard revoked EXECUTE from every role and granted it back to none — it silently failed on every call. The follow-up migration is its own postmortem, published verbatim in the public repo as sql/postmortem_login_guard.sql.

The duplicate check that could not see

Dedup consulted the caller’s own RLS-filtered read, so duplicates owned by a colleague were invisible and got re-inserted. The rewrite classifies matches across visibility; the test reconstructs the exact failing inputs.

Decisions — and what I deliberately did not build

Postgres instead of a dedicated queue, because pilot volume did not justify Kafka or Redis: pg_cron plus job-state tables give visible, recoverable work. RLS instead of app-only authorization, because multi-tenancy must survive wrong application code — the dedup incident above is the proof. Scheduled scans instead of streaming, because listings do not change minute-to-minute. Human approval before outreach, because a bad automated message to a realtor costs more than a slower correct one. No CRM sync yet, because at one agency the system of record can be the system itself. This architecture is what one agency’s pilot needed — not what 1,000 agencies would need, and it does not pretend otherwise.

Inspect it yourself

GitHub: sanitized artifacts + runnable tests Architecture teardown Book the live walkthrough

The public repo’s tests run in GitHub Actions on every push — an engineer can clone it and have the invariants green in under a minute. The production repo stays private because it operates on a live agency’s encrypted client data; in a walkthrough I open the real code, the real database, and the redacted attribution ledger.