Clean and segment the mailing list.
Done by an agent. Checked twice.
One valid row per person, every unsubscribe honoured, a segment on each row and a reason for every row removed.
Real files in
- bounces.csv
Eight bounce records, six hard and two soft. 9 lines, 395 bytes. - list_rules.md
The rules for a valid address, for honouring unsubscribes and bounces, for merging repeated people and for the four segments. 22 lines, 2 KB. - subscribers.csv
A 71 row mailing list of invented people at reserved example addresses, with broken addresses, repeated people, a name spelled two ways and blank open dates. 72 lines, 6 KB. - unsubscribes.csv
Ten unsubscribe requests, some typed in other capitals, three for people not on the list and one from a person who later signed up again. 11 lines, 429 bytes.
Sample data written by Atris. Not a real company.
Three steps
- 1.Read the list and the rules
- 2.Remove what must not be mailed
- 3.Segment and write the result
Claude Sonnet 5.5, 43 seconds. No web. It never saw the check.
Checked twice
- Passed: 71 of 71 subscriber rows are accounted for once, 44 kept and 27 removed
- Passed: 27 of 27 removed rows carry the reason found again from the rules and the address as the list wrote it
- Passed: 0 of 44 addresses on the clean list are invalid, unsubscribed, hard bounced, repeated or untidy
- Passed: 44 of 44 kept rows have the segment found again from the dates and 44 of 44 carry their own name, in sub_id order
- Passed: 6 of 6 duplicate rows name the copy that was kept, 0 other removed rows name a kept copy and 44 of 44 kept rows carry the latest open date of their copies
- Passed: summary.md states 11 of 11 counts right: rows_in 71, kept 44, bad_address 9, unsubscribed 8, bounced 4, duplicate 6, new 2, active 13, lapsing 14, dormant 15, unsubscribes_not_on_list 3
Second review by Claude Opus 5.5: pass.
Then we broke a copy of the output 8 ways on purpose. The check caught 8.
sub_id,email,reason,detail S002,@fenwick.example,bad_address, S005,priyanka.nair@example.org,unsubscribed, S012,dario.kemp@,bad_address, S013,oskar.nilsen@quayside.example,unsubscribed, S015,noor ali@quayside.example,bad_address, S016,helga.brandt@pinecrest.example,unsubscribed, S020,katharine.doyle@example.com,duplicate,S011 S021,greta.lund.tidewell.example,bad_address, S025,lena.ostrowski@harbour,bad_address, S029,,bad_address, S031,ivo.marek@tidewell.example,duplicate,S039 S032,sofia.marin@tidewell..example,bad_address, S033,yara.haddad@fenwick.example,bounced,
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 1 of 1 on 1 December 2026. The agent took 43 seconds.
What the second review verified
- I rebuilt both files from the inputs on my own. clean_list.csv (44 rows) and removed.csv (27 rows) match my version exactly, and together they cover all 71 subscriber rows once each.
- Unsubscribe dates and test order: S046 Oskar Nilsen signed up on 2026-08-22, after the 2026-03-10 unsubscribe, so the row correctly stays, while S013 (signed up 2024-02-10) is removed as unsubscribed. S061 anders.lind is both unsubscribed and hard bounced and is correctly given 'unsubscribed' because that test comes first.
- Duplicate merges: S039 ' ivo.marek@tidewell.example' (signed up 2022-11-30) is kept over S031 and S042 and takes the latest open date, 2026-11-10. S003 takes 2026-11-20 from S044. S067 (2025-12-01) is kept over S036 (2026-03-03). Each duplicate row's detail names the row that was kept.
- Summary counts match my own: rows_in 71, kept 44, bad_address 9, unsubscribed 8, bounced 4, duplicate 6, new 2, active 13, lapsing 14, dormant 15, unsubscribes_not_on_list 3 (zofia.kowal, bruno.salas, edith.moreau). S010 signed up 31 days ago, so it is correctly not 'new'; it has never opened, so it is 'dormant'.
What this run does not show
- Every person and address on the list is invented. The run does not test whether a real address can receive mail.
- An address counts as valid by the written format rule only. A well formed address at a dead domain would pass.
- The list has 71 rows. The run does not show how the job goes on a list of many thousands.
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 8 of 8. 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 segment the mailing list function: marketing for: the marketer who is about to press send on a campaign goal: The list holds one valid row for each person who may be emailed, every row has a segment and every removed row has a reason. ## 1. Read the list and the rules Read input/list_rules.md, input/subscribers.csv, input/unsubscribes.csv and input/bounces.csv. Tidy the addresses the way the rules say before comparing anything. ## 2. Remove what must not be mailed Put every subscriber row through the removal tests in the order the rules give them. Keep the first reason that applies. Do not repair or guess an address. Merge the copies of one person the way the rules say. ## 3. Segment and write the result Give every row that stays a segment from the rules. Write output/clean_list.csv with columns sub_id,email,first_name,last_name,segment,last_open_date, in sub_id order, with the tidy address. Write output/removed.csv with columns sub_id,email,reason,detail, with the address as it was written in the input. For a duplicate, detail names the sub_id that was kept. Write output/summary.md with one count per line in the form `kept: 0`, using these labels in this order: rows_in, kept, bad_address, unsubscribed, bounced, duplicate, new, active, lapsing, dormant, unsubscribes_not_on_list. The last one counts the addresses in the unsubscribe file that match no subscriber row. ## Done looks like - Every subscriber row is in exactly one of clean_list.csv and removed.csv. - No address on the clean list is invalid, unsubscribed, hard bounced or repeated. - The counts in summary.md equal the rows in the two files.
input/bounces.csv · 9 lines, 395 bytes
email,bounce_type,bounced_on ines.duarte@tidewell.example,hard,2026-10-02 kofi.mensah@example.com,hard,2026-10-02 nadia.petrova@pinecrest.example,soft,2026-10-02 yara.haddad@fenwick.example,hard,2026-11-06 anders.lind@fenwick.example,hard,2026-11-06 joel.akhtar@quayside.example,soft,2026-11-06 Lucas.Ferrand@harbourlane.example,hard,2026-11-06 wilma.stroud@example.net,hard,2026-11-06
input/list_rules.md · 22 lines, 2 KB
# Mailing list rules List date: 2026-12-01. Work out every age in days from this date. ## Tidy first Trim the spaces from both ends of every email address and make it lower case. Compare and write addresses in this tidy form. Do the same to the addresses in the unsubscribe file and the bounce file. ## Remove rows, testing in this order The first test a row fails is its reason. 1. `bad_address`: the tidy address is not valid. A valid address has no spaces, commas or semicolons, exactly one @, at least one character before the @ and, after the @, a domain of two or more parts joined by single dots. Each domain part is made only of letters, digits and hyphens and is not empty. The part before the @ may use letters, digits, dots, hyphens, underscores and plus signs. A blank address is not valid. Never repair an address. 2. `unsubscribed`: the address is in unsubscribes.csv and the row's signup_date is on or before the unsubscribed_on date. A signup after the unsubscribe is a new yes, so that row stays. 3. `bounced`: the address has a hard bounce in bounces.csv. A soft bounce stays. 4. `duplicate`: among the rows still standing, rows with the same address are one person. Keep the row with the earliest signup_date. If the dates are equal keep the lowest sub_id. The kept row takes the latest last_open_date found on any of its copies. ## Segment the rows that stay Use the first that fits. - `new`: signed up 30 days ago or less. - `active`: last opened 90 days ago or less. - `lapsing`: last opened 91 to 365 days ago. - `dormant`: last opened more than 365 days ago, or never opened (blank).
input/subscribers.csv · 72 lines, 6 KB
sub_id,email,first_name,last_name,signup_date,source,plan,last_open_date S001,alma.ashdown@pinecrest.example,Alma,Ashdown,2025-07-10,website,customer,2025-07-10 S002,@fenwick.example,Ffion,Pryce,2024-12-11,trade show,prospect, S003,mara.voss@pinecrest.example,Mara,Voss,2023-04-11,trade show,prospect,2026-01-15 S004,xiomara.yarrow@quayside.example,Xiomara,Yarrow,2026-04-09,trade show,prospect,2026-04-09 S005,priyanka.nair@example.org,Priyanka,Nair,2025-03-03,webinar,prospect, S006,ravi+news@quayside.example,Ravi,Chowdhury,2024-08-19,trade show,prospect,2026-09-20 S007,quentin.quill@harbourlane.example,Quentin,Quill,2026-01-03,trade show,customer,2026-07-24 S008,aurelio.brightwater@example.org,Aurelio,Brightwater,2024-09-21,trade show,prospect,2025-01-11 S009,celeste.crane@tidewell.example,Celeste,Crane,2024-04-02,webinar,customer,2026-09-16 S010,ottilie.grange@example.org,Ottilie,Grange,2026-10-31,referral,prospect, S011,katharine.doyle@example.com,Katharine,Doyle,2024-10-05,website,customer,2026-10-01 S012,dario.kemp@,Dario,Kemp,2025-04-02,referral,prospect,2026-10-12 S013,oskar.nilsen@quayside.example,Oskar,Nilsen,2024-02-10,referral,customer,2026-02-27 S014,rafael.quiroga@fenwick.example,Rafael,Quiroga,2022-08-09,trade show,prospect,2025-11-30 S015,noor ali@quayside.example,Noor,Ali,2026-02-14,trade show,prospect,2026-11-02 S016,helga.brandt@pinecrest.example,Helga,Brandt,2024-05-02,referral,customer,2026-01-30 S017,rosalind.radley@quayside.example,Rosalind,Radley,2023-03-05,webinar,prospect,2025-10-14 S018,emeka.obi@quayside.example,Emeka,Obi,2024-06-07,referral,prospect,2026-09-01 S019,jarek.jessop@harbourlane.example,Jarek,Jessop,2022-11-11,webinar,prospect,2026-06-22 S020,katharine.doyle@example.com,Katherine,Doyle,2024-10-05,webinar,prospect, S021,greta.lund.tidewell.example,Greta,Lund,2026-11-12,referral,customer, S022,zelda.abernathy@fenwick.example,Zelda,Abernathy,2022-08-17,referral,prospect,2026-10-08 S023,wendell.whitlock@harbourlane.example,Wendell,Whitlock,2026-10-08,referral,prospect,2026-10-08 S024,bashir.bellweather@pinecrest.example,Bashir,Bellweather,2025-05-29,referral,prospect,2025-05-29 S025,lena.ostrowski@harbour,Lena,Ostrowski,2023-07-19,website,prospect,2026-08-30 S026,yusuf.zeller@fenwick.example,Yusuf,Zeller,2024-08-22,trade show,customer,2026-07-04 S027,joel.akhtar@quayside.example,Joel,Akhtar,2025-08-13,website,customer,2026-04-09 S028,petra.lindqvist@pinecrest.example,Petra,Lindqvist,2024-03-18,trade show,prospect,2026-09-02 S029,,Teodor,Banks,2025-06-16,trade show,customer,2026-09-15 S030,nikhil.northcott@pinecrest.example,Nikhil,Northcott,2024-06-14,trade show,prospect, S031,ivo.marek@tidewell.example,Ivo,Marek,2025-02-02,trade show,customer,2025-08-08 S032,sofia.marin@tidewell..example,Sofia,Marin,2024-01-08,trade show,prospect,2025-10-10 S033,yara.haddad@fenwick.example,Yara,Haddad,2026-01-09,website,prospect, S034,lucas.ferrand@harbourlane.example,Lucas,Ferrand,2025-10-28,referral,prospect,2026-10-30 S035,isolde.ingleby@example.com,Isolde,Ingleby,2023-04-19,website,prospect,2025-01-29 S036,selma.aydin@fenwick.example,Selma,Aydin,2026-03-03,trade show,prospect, S037,hamza.hartnell@harbourlane.example,Hamza,Hartnell,2026-01-14,referral,prospect,2026-01-14 S038,sunniva.dahl@tidewell.example,Sunniva,Dahl,2023-01-25,trade show,prospect,2025-12-01 S039, ivo.marek@tidewell.example,Ivo,Marek,2022-11-30,webinar,prospect,2024-06-06 S040,pavel.pemberton@pinecrest.example,Pavel,Pemberton,2023-11-03,webinar,customer,2025-06-17 S041,ines.duarte@tidewell.example,Ines,Duarte,2024-07-07,webinar,prospect,2025-02-02 S042,IVO.MAREK@TIDEWELL.EXAMPLE,Ivo,Marek,2026-11-10,website,prospect,2026-11-10 S043,p.ibarra@@tidewell.example,Paloma,Ibarra,2025-09-09,trade show,prospect,2026-06-21 S044,Mara.Voss@Pinecrest.example,Mara,Voss,2025-07-01,website,prospect,2026-11-20 S045,kalinda.kirkby@example.com,Kalinda,Kirkby,2026-08-10,referral,customer,2026-10-11 S046,oskar.nilsen@quayside.example,Oskar,Nilsen,2026-08-22,webinar,customer,2026-11-28 S047,maeve.oduya@pinecrest.example,Maeve,Oduya,2025-05-30,webinar,customer, S048,carys.hughes@harbourlane.example,Carys,Hughes,2023-09-14,referral,customer,2026-05-02 S049,dmitri.dunmore@pinecrest.example,Dmitri,Dunmore,2022-11-12,trade show,customer,2024-10-24 S050,nadia.petrova@pinecrest.example,Nadia,Petrova,2024-04-04,trade show,prospect,2026-11-17 S051,mirela.maddox@tidewell.example,Mirela,Maddox,2024-11-16,referral,prospect,2026-09-17 S052,minh.tran@tidewell.example,Minh,Tran,2023-11-03,webinar,prospect,2026-07-14 S053,verity.vance@pinecrest.example,Verity,Vance,2024-03-29,webinar,prospect,2026-11-03 S054,leopold.lowther@pinecrest.example,Leopold,Lowther,2025-11-11,website,prospect,2026-07-03 S055,stellan.stanhope@example.org,Stellan,Stanhope,2024-05-18,referral,customer,2026-07-03 S056,farid.fairburn@pinecrest.example,Farid,Fairburn,2023-02-25,website,prospect,2025-06-24 S057,ravi+news@quayside.example,Ravi,Chowdhury,2025-11-11,website,prospect,2026-03-12 S058,idris.calder@example.com,Idris,Calder,2026-11-19,website,prospect,2026-11-25 S059,greta.galloway@fenwick.example,Greta,Galloway,2022-11-10,trade show,prospect,2026-04-29 S060,elowen.eastlake@quayside.example,Elowen,Eastlake,2026-06-10,website,prospect,2026-07-27 S061,anders.lind@fenwick.example,Anders,Lind,2022-03-29,website,prospect,2024-12-01 S062,Tobias.Reyes@Quayside.example,Tobias,Reyes,2025-02-21,webinar,prospect,2026-10-05 S063,britta.calloway@fenwick.example,Britta,Calloway,2023-02-21,trade show,customer,2026-04-21 S064,tamsin.thackeray@fenwick.example,Tamsin,Thackeray,2024-08-28,referral,prospect, S065,Carys.Hughes@harbourlane.example,Carys,Hughes,2025-01-20,website,prospect,2026-06-18 S066,hollis.finch@harbourlane.example,Hollis,Finch,2026-11-01,referral,customer, S067,selma.aydin@fenwick.example,Selma,Aydin,2025-12-01,trade show,customer, S068,ulrich.underhill@tidewell.example,Ulrich,Underhill,2026-02-01,referral,prospect,2026-02-01 S069,KOFI.MENSAH@example.com,Kofi,Mensah,2023-05-15,trade show,customer,2026-03-03 S070,"t.okafor@quayside.example,",Temi,Okafor,2024-09-27,webinar,prospect,2026-11-11 S071,odette.orrin@example.com,Odette,Orrin,2026-04-06,website,prospect,
input/unsubscribes.csv · 11 lines, 429 bytes
email,unsubscribed_on helga.brandt@pinecrest.example,2026-02-11 tobias.reyes@quayside.example,2026-10-19 Minh.Tran@Tidewell.example ,2026-08-01 zofia.kowal@example.net,2025-05-05 carys.hughes@harbourlane.example,2026-06-30 anders.lind@fenwick.example,2025-01-15 bruno.salas@pinecrest.example,2026-04-22 priyanka.nair@example.org,2025-03-03 OSKAR.NILSEN@quayside.example,2026-03-10 edith.moreau@example.com,2026-09-09
output/clean_list.csv · written by the agent, 45 lines
sub_id,email,first_name,last_name,segment,last_open_date S001,alma.ashdown@pinecrest.example,Alma,Ashdown,dormant,2025-07-10 S003,mara.voss@pinecrest.example,Mara,Voss,active,2026-11-20 S004,xiomara.yarrow@quayside.example,Xiomara,Yarrow,lapsing,2026-04-09 S006,ravi+news@quayside.example,Ravi,Chowdhury,active,2026-09-20 S007,quentin.quill@harbourlane.example,Quentin,Quill,lapsing,2026-07-24 S008,aurelio.brightwater@example.org,Aurelio,Brightwater,dormant,2025-01-11 S009,celeste.crane@tidewell.example,Celeste,Crane,active,2026-09-16 S010,ottilie.grange@example.org,Ottilie,Grange,dormant, S011,katharine.doyle@example.com,Katharine,Doyle,active,2026-10-01 S014,rafael.quiroga@fenwick.example,Rafael,Quiroga,dormant,2025-11-30 S017,rosalind.radley@quayside.example,Rosalind,Radley,dormant,2025-10-14 S018,emeka.obi@quayside.example,Emeka,Obi,lapsing,2026-09-01 S019,jarek.jessop@harbourlane.example,Jarek,Jessop,lapsing,2026-06-22 S022,zelda.abernathy@fenwick.example,Zelda,Abernathy,active,2026-10-08 S023,wendell.whitlock@harbourlane.example,Wendell,Whitlock,active,2026-10-08 S024,bashir.bellweather@pinecrest.example,Bashir,Bellweather,dormant,2025-05-29 S026,yusuf.zeller@fenwick.example,Yusuf,Zeller,lapsing,2026-07-04 S027,joel.akhtar@quayside.example,Joel,Akhtar,lapsing,2026-04-09 S028,petra.lindqvist@pinecrest.example,Petra,Lindqvist,active,2026-09-02 S030,nikhil.northcott@pinecrest.example,Nikhil,Northcott,dormant, S035,isolde.ingleby@example.com,Isolde,Ingleby,dormant,2025-01-29 S037,hamza.hartnell@harbourlane.example,Hamza,Hartnell,lapsing,2026-01-14 S038,sunniva.dahl@tidewell.example,Sunniva,Dahl,lapsing,2025-12-01 S039,ivo.marek@tidewell.example,Ivo,Marek,active,2026-11-10 S040,pavel.pemberton@pinecrest.example,Pavel,Pemberton,dormant,2025-06-17 S045,kalinda.kirkby@example.com,Kalinda,Kirkby,active,2026-10-11 S046,oskar.nilsen@quayside.example,Oskar,Nilsen,active,2026-11-28 S047,maeve.oduya@pinecrest.example,Maeve,Oduya,dormant, S049,dmitri.dunmore@pinecrest.example,Dmitri,Dunmore,dormant,2024-10-24 S050,nadia.petrova@pinecrest.example,Nadia,Petrova,active,2026-11-17 S051,mirela.maddox@tidewell.example,Mirela,Maddox,active,2026-09-17 S053,verity.vance@pinecrest.example,Verity,Vance,active,2026-11-03 S054,leopold.lowther@pinecrest.example,Leopold,Lowther,lapsing,2026-07-03 S055,stellan.stanhope@example.org,Stellan,Stanhope,lapsing,2026-07-03 S056,farid.fairburn@pinecrest.example,Farid,Fairburn,dormant,2025-06-24 S058,idris.calder@example.com,Idris,Calder,new,2026-11-25 S059,greta.galloway@fenwick.example,Greta,Galloway,lapsing,2026-04-29 S060,elowen.eastlake@quayside.example,Elowen,Eastlake,lapsing,2026-07-27 S063,britta.calloway@fenwick.example,Britta,Calloway,lapsing,2026-04-21 S064,tamsin.thackeray@fenwick.example,Tamsin,Thackeray,dormant, S066,hollis.finch@harbourlane.example,Hollis,Finch,new, S067,selma.aydin@fenwick.example,Selma,Aydin,dormant, S068,ulrich.underhill@tidewell.example,Ulrich,Underhill,lapsing,2026-02-01 S071,odette.orrin@example.com,Odette,Orrin,dormant,
output/removed.csv · written by the agent, 28 lines
sub_id,email,reason,detail S002,@fenwick.example,bad_address, S005,priyanka.nair@example.org,unsubscribed, S012,dario.kemp@,bad_address, S013,oskar.nilsen@quayside.example,unsubscribed, S015,noor ali@quayside.example,bad_address, S016,helga.brandt@pinecrest.example,unsubscribed, S020,katharine.doyle@example.com,duplicate,S011 S021,greta.lund.tidewell.example,bad_address, S025,lena.ostrowski@harbour,bad_address, S029,,bad_address, S031,ivo.marek@tidewell.example,duplicate,S039 S032,sofia.marin@tidewell..example,bad_address, S033,yara.haddad@fenwick.example,bounced, S034,lucas.ferrand@harbourlane.example,bounced, S036,selma.aydin@fenwick.example,duplicate,S067 S041,ines.duarte@tidewell.example,bounced, S042,IVO.MAREK@TIDEWELL.EXAMPLE,duplicate,S039 S043,p.ibarra@@tidewell.example,bad_address, S044,Mara.Voss@Pinecrest.example,duplicate,S003 S048,carys.hughes@harbourlane.example,unsubscribed, S052,minh.tran@tidewell.example,unsubscribed, S057,ravi+news@quayside.example,duplicate,S006 S061,anders.lind@fenwick.example,unsubscribed, S062,Tobias.Reyes@Quayside.example,unsubscribed, S065,Carys.Hughes@harbourlane.example,unsubscribed, S069,KOFI.MENSAH@example.com,bounced, S070,"t.okafor@quayside.example,",bad_address,
output/summary.md · written by the agent, 11 lines
rows_in: 71 kept: 44 bad_address: 9 unsubscribed: 8 bounced: 4 duplicate: 6 new: 2 active: 13 lapsing: 14 dormant: 15 unsubscribes_not_on_list: 3
check.py · the check, written before the run
"""Cleans and segments the list again from the rules and compares every row with what the agent wrote."""
import csv, re, sys
from collections import Counter
from datetime import date
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 tidy(e): return (e or "").strip().lower()
def read(name): return list(csv.DictReader((root/name).open(encoding="utf-8")))
rules = (root/"input/list_rules.md").read_text(encoding="utf-8")
asof = date.fromisoformat(re.search(r"List date:\s*(\d{4}-\d{2}-\d{2})", rules).group(1))
subs = read("input/subscribers.csv")
unsub = {tidy(r["email"]): r["unsubscribed_on"].strip() for r in read("input/unsubscribes.csv")}
hard = {tidy(r["email"]) for r in read("input/bounces.csv") if r["bounce_type"].strip().lower() == "hard"}
valid = re.compile(r"[a-z0-9._+-]+@[a-z0-9-]+(\.[a-z0-9-]+)+")
reason, standing = {}, []
for r in subs:
e = tidy(r["email"])
if not valid.fullmatch(e): reason[r["sub_id"]] = "bad_address"
elif e in unsub and r["signup_date"].strip() <= unsub[e]: reason[r["sub_id"]] = "unsubscribed"
elif e in hard: reason[r["sub_id"]] = "bounced"
else: standing.append(r)
keeper, last_open, kept_for = {}, {}, {}
for r in sorted(standing, key=lambda r: (r["signup_date"], r["sub_id"])):
e = tidy(r["email"])
if e in keeper: reason[r["sub_id"]] = "duplicate"; kept_for[r["sub_id"]] = keeper[e]["sub_id"]
else: keeper[e] = r
last_open[e] = max(last_open.get(e, ""), r["last_open_date"].strip())
def segment(r):
opened = last_open[tidy(r["email"])]
if (asof - date.fromisoformat(r["signup_date"].strip())).days <= 30: return "new"
if not opened: return "dormant"
age = (asof - date.fromisoformat(opened)).days
return "active" if age <= 90 else "lapsing" if age <= 365 else "dormant"
want = {r["sub_id"]: segment(r) for r in keeper.values()}
cpath, rpath, spath = root/"output/clean_list.csv", root/"output/removed.csv", root/"output/summary.md"
if not cpath.exists() or not rpath.exists() or not spath.exists():
say(False, "clean_list.csv, removed.csv and summary.md were not all written"); sys.exit(1)
clean = [r for r in csv.DictReader(cpath.open(encoding="utf-8")) if (r.get("sub_id") or "").strip()]
gone = [r for r in csv.DictReader(rpath.open(encoding="utf-8")) if (r.get("sub_id") or "").strip()]
cid = Counter(r["sub_id"].strip() for r in clean); gid = Counter(r["sub_id"].strip() for r in gone)
once = sum(1 for r in subs if cid[r["sub_id"]] + gid[r["sub_id"]] == 1)
split = set(cid) == set(want) and set(gid) == set(reason)
say(once == len(subs) and split, f"{once} of {len(subs)} subscriber rows are accounted for once, {len(want)} kept and {len(reason)} removed")
got_reason = {r["sub_id"].strip(): (r.get("reason") or "").strip().lower() for r in gone}
got_email = {r["sub_id"].strip(): (r.get("email") or "") for r in gone}
src = {r["sub_id"]: r for r in subs}
right = sum(1 for k, v in reason.items() if got_reason.get(k) == v and got_email.get(k, "").strip() == src[k]["email"].strip() and tidy(got_email.get(k)) == tidy(src[k]["email"]))
say(right == len(reason), f"{right} of {len(reason)} removed rows carry the reason found again from the rules and the address as the list wrote it")
emails = [tidy(r.get("email")) for r in clean]; by_id = {r["sub_id"]: r for r in subs}
unsafe = sum(1 for r, e in zip(clean, emails) if not valid.fullmatch(e) or e in hard or emails.count(e) > 1 or e != (r.get("email") or "")
or (e in unsub and by_id.get(r["sub_id"].strip(), {}).get("signup_date", "") <= unsub[e]))
say(clean and unsafe == 0, f"{unsafe} of {len(clean)} addresses on the clean list are invalid, unsubscribed, hard bounced, repeated or untidy")
seg = sum(1 for r in clean if want.get(r["sub_id"].strip()) == (r.get("segment") or "").strip().lower())
ids_in_order = [r["sub_id"].strip() for r in clean] == sorted(want)
who = sum(1 for r in clean if r["sub_id"].strip() in want and (r.get("first_name") or "").strip() == by_id[r["sub_id"].strip()]["first_name"].strip()
and (r.get("last_name") or "").strip() == by_id[r["sub_id"].strip()]["last_name"].strip())
say(seg == len(want) and who == len(want) and ids_in_order, f"{seg} of {len(want)} kept rows have the segment found again from the dates and {who} of {len(want)} carry their own name, in sub_id order")
named = sum(1 for r in gone if r["sub_id"].strip() in kept_for and re.search(r"\b%s\b" % kept_for[r["sub_id"].strip()], r.get("detail") or ""))
merged = sum(1 for r in clean if r["sub_id"].strip() in want and (r.get("last_open_date") or "").strip() == last_open[tidy(by_id[r["sub_id"].strip()]["email"])])
def ids_named(r): return set(re.findall(r"\bS\d{3}\b", r.get("detail") or "")) - {r["sub_id"].strip()}
only = sum(1 for r in gone if r["sub_id"].strip() in kept_for and ids_named(r) == {kept_for[r["sub_id"].strip()]})
stray = sum(1 for r in gone if r["sub_id"].strip() not in kept_for and ids_named(r))
say(named == len(kept_for) and only == len(kept_for) and stray == 0 and merged == len(want),
f"{only} of {len(kept_for)} duplicate rows name the copy that was kept, {stray} other removed rows name a kept copy and {merged} of {len(want)} kept rows carry the latest open date of their copies")
summary = spath.read_text(encoding="utf-8")
counts = {"rows_in": len(subs), "kept": len(want), **{k: sum(1 for v in reason.values() if v == k) for k in ("bad_address", "unsubscribed", "bounced", "duplicate")},
**{k: sum(1 for v in want.values() if v == k) for k in ("new", "active", "lapsing", "dormant")},
"unsubscribes_not_on_list": sum(1 for e in unsub if e not in {tidy(r["email"]) for r in subs})}
stated = sum(1 for k, v in counts.items() if re.search(r"(?m)^\W*%s\W+%d\b" % (k, v), summary))
say(stated == len(counts), f"summary.md states {stated} of {len(counts)} counts right: " + ", ".join(f"{k} {v}" for k, v in counts.items()))
sys.exit(0 if ok else 1)
bounces.csv: sample data written by Atris. Fingerprint 793c71cadeb4.
list_rules.md: sample data written by Atris. Fingerprint ae7fa9b69ac4.
subscribers.csv: sample data written by Atris. Fingerprint f9c0e8a0274c.
unsubscribes.csv: sample data written by Atris. Fingerprint 5d8718aa4e6d.
clean_list.csv: written by the agent. Fingerprint cff163fdb768.
removed.csv: written by the agent. Fingerprint d2c32a97ffcb.
summary.md: written by the agent. Fingerprint d67eca517b62.