Clean and merge two contact lists.
Done by an agent. Checked twice.
One clean contact list with every person once, every source row accounted for and every conflict between rows shown.
Real files in
- crm_contacts.csv
Thirty one contact rows exported from a customer list, with mixed name, email and phone formats. 32 lines, 3 KB. - event_signups.csv
Twenty five sign up rows from an event form, many for people already on the customer list. 26 lines, 3 KB.
Sample data written by Atris. Not a real company.
Three steps
- 1.Clean every row
- 2.Merge rows into contacts
- 3.Write the result
Claude Sonnet 5.5, 35 seconds. No web. It never saw the check.
Checked twice
- Passed: 56 of 56 source rows appear in exactly one contact
- Passed: 39 of 39 contacts group the same source rows as the merge rebuilt from the input files
- Passed: 39 of 39 contacts have the email, name and phone the cleaning rules give
- Passed: 39 of 39 contacts keep the company and date from the latest row
- Passed: 5 of 5 contacts with a conflict are marked with the right fields and 0 are marked by mistake
- Passed: 4 of 4 counts in summary.md are right: 56 rows read, 39 contacts, 17 duplicates merged, 5 with a conflict
- Passed: 2 of 2 rows with a bad phone are named in summary.md
- Passed: 12 of 12 values kept or dropped in a conflict are shown in summary.md
- Passed: 18 of 18 row ids and phone numbers written in summary.md belong to a contact in conflict or a row with a bad phone
Second review by Claude Opus 5.5: pass.
Then we broke a copy of the output 5 ways on purpose. The check caught 5.
Rows read: 56 Contacts after merge: 39 Duplicate rows merged: 17 Contacts with a conflict: 5 ## Contacts with a conflict - Aisha Rahman (aisha.rahman@lakeshoreprint.example; rows CRM-2023;SU-520) - name: kept "Aisha Rahman", dropped "Aysha Rahman" - company: kept "Lakeshore Print Shop", dropped "Lakeshore Print and Copy" - Elena Petrova (elena.petrova@marigoldflorist.example; rows CRM-2011;SU-522) - phone: kept "(617) 555-0130", dropped "(503) 555-0160" - Hannah Weiss (hannah.weiss@copperfieldbakery.example; rows CRM-2019;SU-512) - company: kept "Copperfield Bakery & Cafe", dropped "Copperfield Bakery"
How this was run
An AI agent on Claude Sonnet 5.5 was given a folder with the files below and a three-step flow, and nothing else. It had no web access and never saw the check. The check was written before the run and made stricter after it. It works the answer out again from the input files. Then Claude Opus 5.5 reviewed the result against the inputs. The input files are sample data written by Atris, with the problems a real week has planted in them.
Run 2 of 2 on 1 December 2026. The agent took 35 seconds. Run 1 passed both checks and the job was run again later. Each run starts clean. Between runs the written flow was made clearer, or a check was corrected or made stricter. Nobody edits the output.
What the second review verified
- Recomputed the merge independently from both input files: 31 CRM rows + 25 signup rows = 56 rows read, 39 contacts, 17 duplicates merged and 5 contacts with a conflict. All four match the top of summary.md, and every one of the 39 output rows matches my email, name, phone, company, last_updated, source_rows and conflicts.
- CRM-2008 "braun, felix" has no email. It correctly joined SU-510 because both rows clean to the name Felix Braun and the phone (720) 555-0127. CRM-2009 Marcus Reed (email "n/a", phone (312) 555-0123) correctly stays its own contact, separate from SU-508's (312) 555-0188.
- Bad phones are exactly CRM-2021 ("206-555-012", 9 digits) and SU-509 ("503 555 01", 8 digits). SU-524 "call front desk" is correctly treated as a blank phone, not a bad one, so Sofia Marino keeps (617) 555-0124.
- Tie rule and kept values: Aisha Rahman's two rows share the date 2026-09-15, so the CRM row wins ("Aisha Rahman" and "Lakeshore Print Shop" kept). Hannah Weiss keeps "Copperfield Bakery & Cafe" from SU-512 (2026-09-22). Tom Becker keeps (312) 555-0199 from CRM-2031 (2026-08-30). The list is sorted by name.
What this run does not show
- Both contact lists were written for this run with the duplicates, conflicts and bad phones placed on purpose. A real list is messier than these cleaning rules cover.
- The check shows every kept and dropped value appears in the summary. It cannot tell whether a value sits beside the right contact.
- This is the second run. The first also passed and was repeated only because the wording of the flow was tidied.
Would the check catch a wrong answer?
A check that cannot fail proves nothing. After the run we took a copy of the output, broke it one small way at a time and ran the check again. It caught 5 of 5. The check was made stricter after the first try at this, and the same untouched output was graded again.
Open every file
flow.md · the three steps the agent followed
# Clean and merge two contact lists
function: data
for: the person who keeps the customer contact list
goal: One clean contact list where every person appears once, every source row is accounted for and every disagreement between rows is shown.
## 1. Clean every row
Read input/crm_contacts.csv and input/event_signups.csv. In both files the first column is the row id,
then name, email, phone, company and the row date. Clean each row with these rules:
- Email: remove spaces at the ends and make it lowercase. An email with no @ counts as blank.
- Name: if it has a comma, the part after the comma goes first ("Lopez, Maria" is "Maria Lopez"). Remove
extra spaces. Write each word with a capital first letter and the rest lowercase.
- Phone: keep only the digits. If there are 11 digits and the first is 1, drop the 1. Ten digits are
written as (415) 555-0100. A field with no digits is a blank phone. Any other count of digits is a bad
phone: treat it as blank and remember the row id.
- Company: remove spaces at the ends and turn runs of spaces into one space. Change nothing else.
## 2. Merge rows into contacts
- Rows with the same cleaned email are the same contact.
- A row with no email joins a contact only when its cleaned name and its cleaned phone (not blank) both
equal the cleaned name and phone of a row in that contact. Otherwise it stays a contact of its own.
- Nothing else is a match. Two people who share a phone or a name are still two contacts.
- For name, phone and company keep the value from the row with the latest date that has that field filled
in. If two rows share the latest date, the row from the crm file wins.
- A field is in conflict when the rows of one contact hold two or more different cleaned values for it,
not counting blanks.
## 3. Write the result
Write output/contacts_clean.csv, one row per contact, sorted by name, with columns
email,name,phone,company,last_updated,source_rows,conflicts.
- last_updated is the latest row date in the contact.
- source_rows is every row id in the contact, joined with a semicolon.
- conflicts lists the fields in conflict out of name, phone and company, in that order, joined with a
semicolon. Leave it blank when there are none.
Write output/summary.md for the list owner. Start with these four lines, each with its number:
`Rows read:`, `Contacts after merge:`, `Duplicate rows merged:` (rows read minus contacts) and
`Contacts with a conflict:`. Then list every contact with a conflict, showing the value kept and the
value dropped. Last, list every row id with a bad phone.
## Done looks like
- Every row id from both files appears in exactly one contact.
- Every email, name and phone in the clean list follows the cleaning rules.
- The four numbers at the top of summary.md equal the counts in contacts_clean.csv.
input/crm_contacts.csv · 32 lines, 3 KB
crm_id,full_name,email,phone,company,last_updated CRM-2001,Ruth Abebe,ruth.abebe@ironwoodfitness.example,415.555.0128,Ironwood Fitness,2026-06-25 CRM-2002,Sofia Marino, sofia.marino@riverbendauto.example,6175550124,Riverbend Auto Repair,2026-03-28 CRM-2003,yuki tanaka,yuki.tanaka@greyfieldlaw.example,(617) 555-0136,Greyfield Law Office,2026-06-09 CRM-2004,Derek Lawson,DEREK.LAWSON@FERNHILLACCOUNTING.EXAMPLE,312-555-0135,Fernhill Accounting,2026-08-19 CRM-2005,Daniel Okafor,daniel.okafor@harborlinen.example,1-312-555-0111,Harbor Linen Supply,2026-06-15 CRM-2006,Priya Nair,priya.nair@tidewaterdental.example,1-617-555-0112,Tidewater Dental Group,2026-04-26 CRM-2007,Henry Cole,henry.cole@northwindmovers.example,1-720-555-0139,Northwind Movers,2026-07-15 CRM-2008,"braun, felix",,(720) 555-0127,Juniper Yoga Studio,2026-08-09 CRM-2009,Marcus Reed,n/a,312-555-0123,Bluebird Childcare,2026-06-04 CRM-2010,"DIAZ, Carmen",carmen.diaz@northwindmovers.example,503.555.0138,Northwind Movers,2026-08-08 CRM-2011,Elena Petrova,Elena.Petrova@Marigoldflorist.Example,(503) 555-0160,Marigold Florist,2026-04-22 CRM-2012,Owen Murphy,OWEN.MURPHY@PINECRESTVET.EXAMPLE,1-312-555-0117,Pinecrest Veterinary,2026-03-07 CRM-2013,Nora Lindqvist, nora.lindqvist@summitridgeroofing.example,503.555.0120,Summit Ridge Roofing,2026-03-15 CRM-2014,Adam Kowalski,adam.kowalski@oldmillbrewing.example,7205550133,Old Mill Brewing,2026-05-28 CRM-2015,Irene Papas,irene.papas@bluebirdchildcare.example,1-415-555-0122,Bluebird Childcare,2026-05-01 CRM-2016,Luis Ortega,luis.ortega@copperfieldbakery.example,(720) 555-0115,Copperfield Bakery,2026-06-21 CRM-2017,LAYLA HADDAD,layla.haddad@juniperyoga.example,503 555 0126,Juniper Yoga Studio,2026-06-03 CRM-2018,Grace Chen,grace.chen@pinecrestvet.example,4155550116,Pinecrest Veterinary,2026-07-01 CRM-2019,Hannah Weiss,hannah.weiss@copperfieldbakery.example,503 555 0114,Copperfield Bakery,2026-08-21 CRM-2020,Victor Hale, victor.hale@lakeshoreprint.example,206 555 0119,Lakeshore Print Shop,2026-07-08 CRM-2021,Caleb Stone, caleb.stone@summitridgeroofing.example,206-555-012,Summit Ridge Roofing,2026-03-23 CRM-2022, Maria Lopez,maria.lopez@harborlinen.example,415-555-0110,Harbor Linen Supply,2026-06-28 CRM-2023,Aisha Rahman,aisha.rahman@lakeshoreprint.example,6175550118,Lakeshore Print Shop,2026-09-15 CRM-2024,PAUL JENSEN,Paul.Jensen@Greyfieldlaw.Example,206-555-0137,Greyfield Law Office,2026-05-13 CRM-2025,Jonah Fisher,jonah.fisher@marigoldflorist.example,503.555.0160,Marigold Florist,2026-08-25 CRM-2026,Sam Whitaker,sam.whitaker@ironwoodfitness.example,312-555-0129,Ironwood Fitness,2026-03-01 CRM-2027,Bianca Silva,bianca.silva@fernhillaccounting.example,(415) 555-0134, Fernhill Accounting ,2026-05-01 CRM-2028, Ethan Park,ethan.park@riverbendauto.example,(206) 555-0125,Riverbend Auto Repair,2026-03-16 CRM-2029,Tom Becker,tom.becker@tidewaterdental.example,1-206-555-0113,Tidewater Dental Group,2026-04-04 CRM-2030,Tessa Nguyen,tessa.nguyen@oldmillbrewing.example,(503) 555-0132,Old Mill Brewing,2026-03-13 CRM-2031,"BECKER, Tom",tom.becker@tidewaterdental.example,312 555 0199,Tidewater Dental Group,2026-08-30
input/event_signups.csv · 26 lines, 3 KB
signup_id,name,email,phone,company,signed_up SU-501,Maria Lopez,Maria.Lopez@Harborlinen.Example,+1 415 555 0110,Harbor Linen Supply,2026-09-28 SU-502,Joy Adeyemi,joy.adeyemi@kingfishertravel.example,4155550146,Kingfisher Travel,2026-09-21 SU-503,Zoe Hartman,zoe.hartman@saltmarshcafe.example,1-415-555-0140,Saltmarsh Cafe,2026-09-03 SU-504,Layla Haddad,layla.haddad@juniperyoga.example,+1 503 555 0126,Juniper Yoga Studio,2026-09-03 SU-505,Nora Lindqvist,nora.lindqvist@summitridgeroofing.example,(617) 555-0177,Summit Ridge Roofing,2026-10-27 SU-506, Grace Chen, grace.chen@pinecrestvet.example,415 555 0116,Pinecrest Veterinary,2026-09-14 SU-507,Simon Frost,Simon.Frost@Kingfishertravel.Example,+1 312 555 0147,Kingfisher Travel,2026-10-24 SU-508,Marcus Reed,marcus.reed@bluebirdchildcare.example,(312) 555-0188,Bluebird Childcare,2026-10-12 SU-509,RUTH ABEBE, ruth.abebe@ironwoodfitness.example,503 555 01,Ironwood Fitness,2026-09-02 SU-510,Felix Braun,felix.braun@juniperyoga.example,720-555-0127,Juniper Yoga Studio,2026-10-11 SU-511,Yuki Tanaka,yuki.tanaka@greyfieldlaw.example,6175550136,Greyfield Law Office,2026-09-16 SU-512, Hannah Weiss, hannah.weiss@copperfieldbakery.example,503.555.0114,Copperfield Bakery & Cafe,2026-09-22 SU-513,Grace Chen,grace.chen@pinecrestvet.example,415 555 0116,Pinecrest Veterinary,2026-10-02 SU-514,Miles Grant,miles.grant@stonebridgepharmacy.example,2065550143,Stonebridge Pharmacy,2026-09-28 SU-515,Kira Novak,kira.novak@stonebridgepharmacy.example,1-617-555-0142,Stonebridge Pharmacy,2026-10-12 SU-516,Priya Nair,priya.nair@tidewaterdental.example ,,Tidewater Dental Group,2026-09-27 SU-517,Tessa Nguyen,tessa.nguyen@oldmillbrewing.example,+1 503 555 0132,Old Mill Brewing,2026-09-19 SU-518,Ibrahim Sesay,ibrahim.sesay@saltmarshcafe.example,(312) 555-0141,Saltmarsh Cafe,2026-09-24 SU-519,Rafael Gomez,Rafael.Gomez@Willowcreeknursery.Example,1-720-555-0145,Willow Creek Nursery,2026-10-13 SU-520,Aysha Rahman,aisha.rahman@lakeshoreprint.example,6175550118,Lakeshore Print and Copy,2026-09-15 SU-521,Irene Papas,irene.papas@bluebirdchildcare.example,1-415-555-0122,Bluebird Childcare,2026-09-24 SU-522,Elena Petrova,elena.petrova@marigoldflorist.example,617.555.0130,,2026-09-03 SU-523,Anna Keller,anna.keller@willowcreeknursery.example,503.555.0144,Willow Creek Nursery,2026-09-28 SU-524,Sofia Marino,sofia.marino@riverbendauto.example,call front desk,Riverbend Auto Repair,2026-09-09 SU-525,Bianca Silva,bianca.silva@fernhillaccounting.example,(415) 555-0134,Fernhill Accounting,2026-10-23
output/contacts_clean.csv · written by the agent, 40 lines
email,name,phone,company,last_updated,source_rows,conflicts adam.kowalski@oldmillbrewing.example,Adam Kowalski,(720) 555-0133,Old Mill Brewing,2026-05-28,CRM-2014, aisha.rahman@lakeshoreprint.example,Aisha Rahman,(617) 555-0118,Lakeshore Print Shop,2026-09-15,CRM-2023;SU-520,name;company anna.keller@willowcreeknursery.example,Anna Keller,(503) 555-0144,Willow Creek Nursery,2026-09-28,SU-523, bianca.silva@fernhillaccounting.example,Bianca Silva,(415) 555-0134,Fernhill Accounting,2026-10-23,CRM-2027;SU-525, caleb.stone@summitridgeroofing.example,Caleb Stone,,Summit Ridge Roofing,2026-03-23,CRM-2021, carmen.diaz@northwindmovers.example,Carmen Diaz,(503) 555-0138,Northwind Movers,2026-08-08,CRM-2010, daniel.okafor@harborlinen.example,Daniel Okafor,(312) 555-0111,Harbor Linen Supply,2026-06-15,CRM-2005, derek.lawson@fernhillaccounting.example,Derek Lawson,(312) 555-0135,Fernhill Accounting,2026-08-19,CRM-2004, elena.petrova@marigoldflorist.example,Elena Petrova,(617) 555-0130,Marigold Florist,2026-09-03,CRM-2011;SU-522,phone ethan.park@riverbendauto.example,Ethan Park,(206) 555-0125,Riverbend Auto Repair,2026-03-16,CRM-2028, felix.braun@juniperyoga.example,Felix Braun,(720) 555-0127,Juniper Yoga Studio,2026-10-11,SU-510;CRM-2008, grace.chen@pinecrestvet.example,Grace Chen,(415) 555-0116,Pinecrest Veterinary,2026-10-02,CRM-2018;SU-506;SU-513, hannah.weiss@copperfieldbakery.example,Hannah Weiss,(503) 555-0114,Copperfield Bakery & Cafe,2026-09-22,CRM-2019;SU-512,company henry.cole@northwindmovers.example,Henry Cole,(720) 555-0139,Northwind Movers,2026-07-15,CRM-2007, ibrahim.sesay@saltmarshcafe.example,Ibrahim Sesay,(312) 555-0141,Saltmarsh Cafe,2026-09-24,SU-518, irene.papas@bluebirdchildcare.example,Irene Papas,(415) 555-0122,Bluebird Childcare,2026-09-24,CRM-2015;SU-521, jonah.fisher@marigoldflorist.example,Jonah Fisher,(503) 555-0160,Marigold Florist,2026-08-25,CRM-2025, joy.adeyemi@kingfishertravel.example,Joy Adeyemi,(415) 555-0146,Kingfisher Travel,2026-09-21,SU-502, kira.novak@stonebridgepharmacy.example,Kira Novak,(617) 555-0142,Stonebridge Pharmacy,2026-10-12,SU-515, layla.haddad@juniperyoga.example,Layla Haddad,(503) 555-0126,Juniper Yoga Studio,2026-09-03,CRM-2017;SU-504, luis.ortega@copperfieldbakery.example,Luis Ortega,(720) 555-0115,Copperfield Bakery,2026-06-21,CRM-2016, ,Marcus Reed,(312) 555-0123,Bluebird Childcare,2026-06-04,CRM-2009, marcus.reed@bluebirdchildcare.example,Marcus Reed,(312) 555-0188,Bluebird Childcare,2026-10-12,SU-508, maria.lopez@harborlinen.example,Maria Lopez,(415) 555-0110,Harbor Linen Supply,2026-09-28,CRM-2022;SU-501, miles.grant@stonebridgepharmacy.example,Miles Grant,(206) 555-0143,Stonebridge Pharmacy,2026-09-28,SU-514, nora.lindqvist@summitridgeroofing.example,Nora Lindqvist,(617) 555-0177,Summit Ridge Roofing,2026-10-27,CRM-2013;SU-505,phone owen.murphy@pinecrestvet.example,Owen Murphy,(312) 555-0117,Pinecrest Veterinary,2026-03-07,CRM-2012, paul.jensen@greyfieldlaw.example,Paul Jensen,(206) 555-0137,Greyfield Law Office,2026-05-13,CRM-2024, priya.nair@tidewaterdental.example,Priya Nair,(617) 555-0112,Tidewater Dental Group,2026-09-27,CRM-2006;SU-516, rafael.gomez@willowcreeknursery.example,Rafael Gomez,(720) 555-0145,Willow Creek Nursery,2026-10-13,SU-519, ruth.abebe@ironwoodfitness.example,Ruth Abebe,(415) 555-0128,Ironwood Fitness,2026-09-02,CRM-2001;SU-509, sam.whitaker@ironwoodfitness.example,Sam Whitaker,(312) 555-0129,Ironwood Fitness,2026-03-01,CRM-2026, simon.frost@kingfishertravel.example,Simon Frost,(312) 555-0147,Kingfisher Travel,2026-10-24,SU-507, sofia.marino@riverbendauto.example,Sofia Marino,(617) 555-0124,Riverbend Auto Repair,2026-09-09,CRM-2002;SU-524, tessa.nguyen@oldmillbrewing.example,Tessa Nguyen,(503) 555-0132,Old Mill Brewing,2026-09-19,CRM-2030;SU-517, tom.becker@tidewaterdental.example,Tom Becker,(312) 555-0199,Tidewater Dental Group,2026-08-30,CRM-2029;CRM-2031,phone victor.hale@lakeshoreprint.example,Victor Hale,(206) 555-0119,Lakeshore Print Shop,2026-07-08,CRM-2020, yuki.tanaka@greyfieldlaw.example,Yuki Tanaka,(617) 555-0136,Greyfield Law Office,2026-09-16,CRM-2003;SU-511, zoe.hartman@saltmarshcafe.example,Zoe Hartman,(415) 555-0140,Saltmarsh Cafe,2026-09-03,SU-503,
output/summary.md · written by the agent, 23 lines
Rows read: 56 Contacts after merge: 39 Duplicate rows merged: 17 Contacts with a conflict: 5 ## Contacts with a conflict - Aisha Rahman (aisha.rahman@lakeshoreprint.example; rows CRM-2023;SU-520) - name: kept "Aisha Rahman", dropped "Aysha Rahman" - company: kept "Lakeshore Print Shop", dropped "Lakeshore Print and Copy" - Elena Petrova (elena.petrova@marigoldflorist.example; rows CRM-2011;SU-522) - phone: kept "(617) 555-0130", dropped "(503) 555-0160" - Hannah Weiss (hannah.weiss@copperfieldbakery.example; rows CRM-2019;SU-512) - company: kept "Copperfield Bakery & Cafe", dropped "Copperfield Bakery" - Nora Lindqvist (nora.lindqvist@summitridgeroofing.example; rows CRM-2013;SU-505) - phone: kept "(617) 555-0177", dropped "(503) 555-0120" - Tom Becker (tom.becker@tidewaterdental.example; rows CRM-2029;CRM-2031) - phone: kept "(312) 555-0199", dropped "(206) 555-0113" ## Row ids with a bad phone - CRM-2021 - SU-509
check.py · the check, written before the run
"""Rebuilds the clean contact list from the two input files and compares it with what the agent wrote."""
import csv, re, sys
from pathlib import Path
root = Path(sys.argv[1]); ok = True
def say(passed, text):
global ok; ok = ok and passed; print(("PASS " if passed else "FAIL ") + text)
def clean_name(s):
if "," in s:
last, first = s.split(",", 1); s = first + " " + last
return " ".join(w[:1].upper() + w[1:].lower() for w in s.split())
def clean_phone(s):
d = re.sub(r"\D", "", s)
if len(d) == 11 and d[0] == "1": d = d[1:]
if not d: return "", False
if len(d) != 10: return "", True
return f"({d[:3]}) {d[3:6]}-{d[6:]}", False
rows = []
for fname, src in (("crm_contacts.csv", "crm"), ("event_signups.csv", "signup")):
for r in csv.reader(list((root/"input"/fname).open())[1:]):
email = r[2].strip().lower()
phone, bad = clean_phone(r[3])
rows.append({"id": r[0].strip(), "src": src, "name": clean_name(r[1]), "email": email if "@" in email else "",
"phone": phone, "bad": bad, "company": " ".join(r[4].split()), "date": r[5].strip(),
"raw": {"name": r[1].strip(), "phone": r[3].strip(), "company": r[4].strip()}})
groups = {}
for r in rows:
if r["email"]: groups.setdefault(r["email"], []).append(r)
solo = []
for r in rows:
if r["email"]: continue
home = [k for k, g in groups.items() if r["phone"] and any(x["name"] == r["name"] and x["phone"] == r["phone"] for x in g if x["email"])]
if len(home) == 1: groups[home[0]].append(r)
else: solo.append([r])
want = {}
for g in list(groups.values()) + solo:
best = sorted(g, key=lambda x: (x["date"], x["src"] == "crm"), reverse=True)
pick = lambda f: next((x[f] for x in best if x[f]), "")
conf = [f for f in ("name", "phone", "company") if len({x[f] for x in g if x[f]}) > 1]
want[frozenset(x["id"] for x in g)] = {"email": next((x["email"] for x in g if x["email"]), ""), "name": pick("name"),
"phone": pick("phone"), "company": pick("company"), "last_updated": best[0]["date"], "conflicts": ";".join(conf),
"shown": [{x[f] for x in g if x[f] == v} | {x["raw"][f] for x in g if x[f] == v} for f in conf for v in sorted({x[f] for x in g if x[f]})]}
path = root/"output/contacts_clean.csv"
if not path.exists():
say(False, "output/contacts_clean.csv was not written"); sys.exit(1)
got_rows = list(csv.DictReader(path.open()))
got, seen = {}, []
for r in got_rows:
ids = [x.strip() for x in (r.get("source_rows") or "").split(";") if x.strip()]
seen += ids; got[frozenset(ids)] = r
all_ids = [r["id"] for r in rows]
once = sum(1 for i in all_ids if seen.count(i) == 1)
say(once == len(all_ids) and len(seen) == len(all_ids), f"{once} of {len(all_ids)} source rows appear in exactly one contact")
same = sum(1 for k in want if k in got)
say(same == len(want) and len(got_rows) == len(want), f"{same} of {len(want)} contacts group the same source rows as the merge rebuilt from the input files")
def n_ok(fields):
return sum(1 for k, w in want.items() if k in got and all((got[k].get(f) or "").strip() == w[f] for f in fields))
a = n_ok(["email", "name", "phone"])
say(a == len(want), f"{a} of {len(want)} contacts have the email, name and phone the cleaning rules give")
b = n_ok(["company", "last_updated"])
say(b == len(want), f"{b} of {len(want)} contacts keep the company and date from the latest row")
nconf = sum(1 for w in want.values() if w["conflicts"])
c = sum(1 for k, w in want.items() if w["conflicts"] and k in got and (got[k].get("conflicts") or "").replace(" ", "") == w["conflicts"])
quiet = sum(1 for k, w in want.items() if not w["conflicts"] and k in got and (got[k].get("conflicts") or "").strip())
say(c == nconf and quiet == 0, f"{c} of {nconf} contacts with a conflict are marked with the right fields and {quiet} are marked by mistake")
summary = (root/"output/summary.md").read_text() if (root/"output/summary.md").exists() else ""
def stated(label):
m = re.search(re.escape(label) + r"\D{0,12}?(\d+)", summary, re.I)
return int(m.group(1)) if m else None
facts = {"Rows read": len(rows), "Contacts after merge": len(want), "Duplicate rows merged": len(rows) - len(want), "Contacts with a conflict": nconf}
good = sum(1 for k, v in facts.items() if stated(k) == v)
say(good == 4, f"{good} of 4 counts in summary.md are right: {len(rows)} rows read, {len(want)} contacts, {len(rows) - len(want)} duplicates merged, {nconf} with a conflict")
bad = [r["id"] for r in rows if r["bad"]]
named = sum(1 for i in bad if i in summary)
say(named == len(bad), f"{named} of {len(bad)} rows with a bad phone are named in summary.md")
# every value kept or dropped in a conflict has to be shown, cleaned or as it was typed
shown = [forms for w in want.values() for forms in w["shown"]]
there = sum(1 for forms in shown if any(v in summary for v in forms))
say(there == len(shown), f"{there} of {len(shown)} values kept or dropped in a conflict are shown in summary.md")
# a row id or a phone number written in the summary has to belong to a conflict or to a bad phone row
conf_ids = {i for k, w in want.items() if w["conflicts"] for i in k} | set(bad)
conf_phones = {r["phone"] for k, w in want.items() if w["conflicts"] for r in rows if r["id"] in k and r["phone"]}
prefixes = "|".join(sorted({re.escape(i.split("-")[0]) for i in all_ids}))
ids_written = re.findall(r"\b(?:%s)-\d+" % prefixes, summary)
phones_written = re.findall(r"\(\d{3}\) \d{3}-\d{4}", summary)
fine = sum(1 for i in ids_written if i in conf_ids) + sum(1 for x in phones_written if x in conf_phones)
written = len(ids_written) + len(phones_written)
say(written > 0 and fine == written, f"{fine} of {written} row ids and phone numbers written in summary.md belong to a contact in conflict or a row with a bad phone")
sys.exit(0 if ok else 1)
crm_contacts.csv: sample data written by Atris. Fingerprint 8631cca2153a.
event_signups.csv: sample data written by Atris. Fingerprint e3ff236baf11.
contacts_clean.csv: written by the agent. Fingerprint 3e21dab0930a.
summary.md: written by the agent. Fingerprint f926a03ecb15.