-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathandersproject.py
More file actions
913 lines (799 loc) · 35.3 KB
/
Copy pathandersproject.py
File metadata and controls
913 lines (799 loc) · 35.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
#!/usr/bin/env python3
"""
Anders Project — daily audit of intermediate COA opinions.
Reads opinions already downloaded by pdrbot, identifies those resolving
an Anders/Gainous brief in a trial (non-plea) case, and checks whether:
1. The COA opinion identifies the elements of the charged offense AND
describes the evidence supporting each element.
2. If not, whether the Anders brief itself did so.
A daily heartbeat email is sent to the recipients in TO_ADDRS from the
authenticated EMAIL_FROM identity (PDRbot@iacls.org) summarizing the day's
Anders findings: total opinions, Anders count, plea vs.
trial split, and for trial cases whether the elements-and-evidence requirement
was met by the opinion, by the brief, or neither. Deficient cases (and their
brief PDFs as attachments) are included in the same email.
Usage:
python andersproject.py # analyze yesterday's opinions
python andersproject.py --date 2026-04-22
python andersproject.py --date 2026-04-22 --dry-run
python andersproject.py --reanalyze # force re-analysis of all
python andersproject.py --report-only # email pending items without re-analyzing
"""
import argparse
import json
import logging
import os
import re
import smtplib
import sqlite3
import sys
import time
from datetime import date, datetime, timedelta
from email import encoders
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from pathlib import Path
# pdrbot lives one directory up from this script's perspective — we're in PDRbot/
ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(ROOT))
sys.path.insert(0, os.path.expanduser('~/github/mwb_common'))
from mwb_claude import call_claude, call_claude_ex
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s',
)
LOG = logging.getLogger('andersproject')
DB_PATH = ROOT / 'data' / 'pdrbot.db'
# Send from the identity the SMTP account actually authenticates as. Fastmail
# rejects (551 5.7.1) outbound mail whose From header is not an authorised
# identity on the account, and the account authenticates as EMAIL_FROM.
FROM_ADDR = os.environ.get('EMAIL_FROM', 'PDRbot@iacls.org')
TO_ADDRS = ['mb@ivi3.com', 'mcjernig@cougarnet.uh.edu']
# ── Claude prompts ────────────────────────────────────────────────────────────
OPINION_PROMPT = """\
You are a Texas appellate practice expert reviewing a Court of Appeals opinion.
Answer ONLY with a JSON object — no other text.
{
"is_anders": true/false,
"is_trial": true/false/null,
"opinion_lists_elements": true/false/null,
"offense_name": "string or null",
"notes": "one sentence"
}
Definitions:
- "is_anders": true if the opinion states that appointed counsel filed an Anders
brief (sometimes called an Anders/Gainous brief) representing there are no
non-frivolous appellate issues.
- "is_trial": true if the underlying conviction followed a jury or bench trial;
false if it followed a guilty or no-contest plea; null if is_anders is false
or the opinion does not contain enough information to determine this.
- "opinion_lists_elements": true if the opinion (a) names the specific statutory
elements of the charged offense AND (b) describes the trial evidence that
supports each element. false if it does not do both. null if is_anders is false
or is_trial is false.
- "offense_name": the name of the charged offense if identifiable, else null.
OPINION TEXT:
"""
BRIEF_PROMPT = """\
You are a Texas appellate practice expert reviewing an Anders brief.
Answer ONLY with a JSON object — no other text.
{
"is_trial": true/false/null,
"brief_lists_elements": true/false/null,
"notes": "one sentence"
}
Definitions:
- "is_trial": true if the underlying conviction followed a jury or bench trial;
false if it followed a guilty or no-contest plea; null only if the brief
contains no information about the mode of conviction.
- "brief_lists_elements": true if the brief (a) identifies the specific statutory
elements of the charged offense AND (b) describes the trial evidence that
supports each element in the sufficiency-of-evidence review. false if it does
not do both. null if is_trial is false (plea cases are out of scope).
BRIEF TEXT:
"""
# ── Database ──────────────────────────────────────────────────────────────────
SCHEMA = """\
CREATE TABLE IF NOT EXISTS anders_analyses (
id INTEGER PRIMARY KEY AUTOINCREMENT,
opinion_id INTEGER NOT NULL UNIQUE,
case_number TEXT NOT NULL,
court TEXT,
opinion_date DATE,
is_anders INTEGER,
is_trial INTEGER,
opinion_lists_elements INTEGER,
brief_lists_elements INTEGER,
brief_url TEXT,
brief_pdf_path TEXT,
offense_name TEXT,
notes TEXT,
analyzed_at TIMESTAMP DEFAULT (datetime('now','localtime')),
model TEXT,
FOREIGN KEY (opinion_id) REFERENCES opinions(id)
);
CREATE TABLE IF NOT EXISTS anders_report_items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
case_number TEXT NOT NULL,
court TEXT,
opinion_date DATE,
case_url TEXT,
brief_url TEXT,
offense_name TEXT,
failure_reason TEXT,
reported_at TIMESTAMP DEFAULT (datetime('now','localtime')),
emailed_at TIMESTAMP
);
"""
def init_schema(conn: sqlite3.Connection) -> None:
conn.executescript(SCHEMA)
conn.commit()
# ── PDF text extraction ───────────────────────────────────────────────────────
def extract_text(pdf_path: Path, max_chars: int = 50_000) -> str:
import subprocess
try:
r = subprocess.run(
['pdftotext', '-layout', str(pdf_path), '-'],
capture_output=True, text=True, timeout=60,
)
text = r.stdout.strip()
if len(text) < 100:
r = subprocess.run(
['pdftotext', str(pdf_path), '-'],
capture_output=True, text=True, timeout=60,
)
text = r.stdout.strip()
return text[:max_chars]
except Exception as e:
LOG.warning('pdftotext failed for %s: %s', pdf_path, e)
return ''
# ── Anders brief fetching from search.txcourts.gov ───────────────────────────
def _court_coa_code(court_str: str) -> str:
"""Convert 'COA14' → 'cos14', 'COA01' → 'cos01', etc."""
m = re.search(r'(\d+)', court_str)
if m:
return f"cos{int(m.group(1)):02d}"
return 'cos01'
def fetch_anders_brief(case_number: str, court: str) -> tuple[str | None, Path | None]:
"""
Fetch the Anders brief PDF for a case from search.txcourts.gov.
Returns (brief_url, local_pdf_path) or (None, None).
"""
import requests
from bs4 import BeautifulSoup
coa = _court_coa_code(court)
base = 'https://search.txcourts.gov'
case_url = f'{base}/Case.aspx?cn={case_number}&coa={coa}'
headers = {
'User-Agent': (
'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) '
'Gecko/20100101 Firefox/115.0'
)
}
try:
r = requests.get(case_url, headers=headers, timeout=30)
if r.status_code != 200:
return None, None
except Exception as e:
LOG.warning('fetch_anders_brief: %s', e)
return None, None
soup = BeautifulSoup(r.text, 'lxml')
brief_grid = soup.find('div', {'id': 'ctl00_ContentPlaceHolder1_grdBriefs'})
if not brief_grid:
return None, None
link_re = re.compile(r'\[(https?://[^\]]+)\]')
for row in brief_grid.find_all('tr', class_=lambda c: c and ('rgRow' in c or 'rgAltRow' in c)):
# Match the Event Type column exactly. Substring matching on the whole
# row picks up the State's "Brief Waiver-Anders Response" rows, which
# share the word "Anders" but link to a different PDF.
tds = row.find_all('td', recursive=False)
if len(tds) < 2:
continue
if tds[1].get_text(strip=True).lower() != 'anders brief filed':
continue
# Appellant's Anders brief row — first PDF link is the brief itself
# (the second link, when present, is the notice of filing).
for a in row.find_all('a'):
href = a.get('href', '')
if not href.startswith('http'):
href = base + '/' + href.lstrip('/')
if 'SearchMedia' in href or href.lower().endswith('.pdf'):
# Download it
brief_url = href
try:
pr = requests.get(brief_url, headers=headers, timeout=60)
if pr.status_code == 200 and pr.content[:5] == b'%PDF-':
out_dir = ROOT / 'data' / 'anders_briefs'
out_dir.mkdir(parents=True, exist_ok=True)
safe = re.sub(r'[^A-Za-z0-9._-]', '_', case_number)
pdf_path = out_dir / f'{safe}_anders_brief.pdf'
pdf_path.write_bytes(pr.content)
LOG.info(' Downloaded Anders brief: %s', pdf_path.name)
return brief_url, pdf_path
except Exception as e:
LOG.warning(' Brief download failed: %s', e)
return brief_url, None
return None, None
# ── Claude calls ──────────────────────────────────────────────────────────────
def _parse_json(text: str) -> dict:
# Prefer the outermost balanced object (first '{' .. last '}'). This handles
# a brace appearing inside a string value (e.g. the "notes" field), which the
# old brace-free regex `\{[^{}]*\}` would refuse to match, silently dropping
# an otherwise-valid verdict to {} — and, for the brief judgment, to a
# spurious null rather than the model's real True/False.
start = text.find('{')
end = text.rfind('}')
if start != -1 and end > start:
try:
return json.loads(text[start:end + 1])
except json.JSONDecodeError:
pass
# Fallback: the original brace-free match, in case the outer slice spans two
# separate objects (e.g. a prose reply that happens to contain stray braces).
m = re.search(r'\{[^{}]*\}', text, re.DOTALL)
if m:
try:
return json.loads(m.group(0))
except json.JSONDecodeError:
pass
LOG.warning('No JSON in Claude response: %r', text[:200])
return {}
def analyze_opinion(pdf_path: Path) -> dict:
text = extract_text(pdf_path)
if not text:
return {}
try:
raw, model_used = call_claude_ex(OPINION_PROMPT + text, timeout=120)
except Exception as e:
LOG.warning('Claude opinion analysis failed: %s', e)
return {}
result = _parse_json(raw)
if result:
# Record the model that actually answered — call_claude_ex falls
# CLI(Opus)→SDK(Opus)→Grok, and a silent downgrade to Grok must be
# visible rather than masked by a hardcoded model string.
result['_model'] = model_used
return result
def analyze_brief(pdf_path: Path) -> dict:
text = extract_text(pdf_path)
if not text:
return {}
try:
raw, model_used = call_claude_ex(BRIEF_PROMPT + text, timeout=120)
except Exception as e:
LOG.warning('Claude brief analysis failed: %s', e)
return {}
result = _parse_json(raw)
if result:
result['_model'] = model_used
return result
# ── Core analysis loop ────────────────────────────────────────────────────────
def process_opinion(conn: sqlite3.Connection, row: tuple, reanalyze: bool = False) -> None:
op_id, case_number, court, op_date, file_path, case_url = row
# Skip if already analyzed (unless --reanalyze)
existing = conn.execute(
'SELECT id FROM anders_analyses WHERE opinion_id=?', (op_id,)
).fetchone()
if existing and not reanalyze:
return
pdf_path = ROOT / file_path
if not pdf_path.exists():
LOG.warning('PDF not found: %s', pdf_path)
return
LOG.info('Analyzing %s (%s)', case_number, court)
result = analyze_opinion(pdf_path)
if not result:
LOG.warning(' No result for %s', case_number)
return
is_anders = result.get('is_anders')
is_trial = result.get('is_trial')
opinion_lists = result.get('opinion_lists_elements')
offense_name = result.get('offense_name')
notes = result.get('notes', '')
op_model = result.get('_model')
LOG.info(' is_anders=%s is_trial=%s opinion_lists_elements=%s (model=%s)',
is_anders, is_trial, opinion_lists, op_model)
brief_lists = None
brief_url = None
brief_pdf_path = None
# Every model that answered a brief judgment for this case, in order — so the
# stored provenance shows if any brief verdict came from a fallback (Grok).
brief_models: list = []
# If Anders and is_trial ambiguous from opinion, consult the brief
if is_anders and is_trial is None:
LOG.info(' is_trial ambiguous from opinion; fetching brief to resolve')
brief_url, brief_pdf = fetch_anders_brief(case_number, court)
if brief_pdf:
brief_pdf_path = str(brief_pdf)
br = analyze_brief(brief_pdf)
brief_models.append(br.get('_model'))
resolved = br.get('is_trial')
if resolved is not None:
is_trial = resolved
LOG.info(' is_trial resolved from brief: %s', is_trial)
brief_lists = br.get('brief_lists_elements')
LOG.info(' brief_lists_elements=%s (model=%s)',
brief_lists, br.get('_model'))
else:
LOG.info(' Anders brief not found or not downloadable')
time.sleep(1.0)
# If Anders + trial + opinion did NOT list elements, check the brief (if not already fetched)
if is_anders and is_trial and opinion_lists is False and brief_url is None:
LOG.info(' Fetching Anders brief from search.txcourts.gov...')
brief_url, brief_pdf = fetch_anders_brief(case_number, court)
if brief_pdf:
brief_pdf_path = str(brief_pdf)
br = analyze_brief(brief_pdf)
brief_models.append(br.get('_model'))
brief_lists = br.get('brief_lists_elements')
LOG.info(' brief_lists_elements=%s (model=%s)',
brief_lists, br.get('_model'))
else:
LOG.info(' Anders brief not found or not downloadable')
time.sleep(1.0)
# Verify before flagging DEFICIENT. A DEFICIENT flag publicly accuses a named
# appointed attorney and the court, so a single brief judgment is too weak to
# rest it on — one flaky sample, or a silent fallback to Grok during an Opus
# outage, produced exactly that false positive on 02-25-00442-CR (Criminal
# Trespass), whose brief does recite the elements and the supporting evidence.
# When the cheap first pass would flag (opinion False + brief False), re-run
# the brief judgment to best-of-3 and require a majority of explicit False
# votes to stand. A tie, a majority True, or unparseable votes clear the flag.
if (is_anders and is_trial and opinion_lists is False
and brief_lists is False and brief_pdf_path):
brief_pdf = Path(brief_pdf_path)
false_votes = 1 # the first pass already returned False
true_votes = 0
for _ in range(2):
time.sleep(1.0)
br = analyze_brief(brief_pdf)
brief_models.append(br.get('_model'))
v = br.get('brief_lists_elements')
if v is True:
true_votes += 1
elif v is False:
false_votes += 1
if false_votes >= 2:
brief_lists = False
LOG.info(' brief DEFICIENT confirmed (%d False / %d True of 3); '
'models=%s', false_votes, true_votes, brief_models)
else:
brief_lists = True
LOG.info(' brief NOT deficient on re-check (%d False / %d True of 3);'
' clearing flag; models=%s',
false_votes, true_votes, brief_models)
# Provenance of the models that actually answered — replaces the old
# hardcoded 'claude-opus-4-7', which lied whenever the chain fell back.
def _fmt_models(models: list) -> str:
seen = [m for m in models if m]
# de-dup preserving order
uniq = list(dict.fromkeys(seen))
return '+'.join(uniq) if uniq else 'unknown'
model_provenance = f"opinion={_fmt_models([op_model])}"
if brief_models:
model_provenance += f" brief={_fmt_models(brief_models)}"
# Upsert analysis record
if existing and reanalyze:
conn.execute(
'''UPDATE anders_analyses SET
is_anders=?, is_trial=?, opinion_lists_elements=?,
brief_lists_elements=?, brief_url=?, brief_pdf_path=?,
offense_name=?, notes=?, analyzed_at=datetime('now','localtime'),
model=?
WHERE opinion_id=?''',
(
int(is_anders) if is_anders is not None else None,
int(is_trial) if is_trial is not None else None,
int(opinion_lists) if opinion_lists is not None else None,
int(brief_lists) if brief_lists is not None else None,
brief_url, brief_pdf_path, offense_name, notes,
model_provenance, op_id,
),
)
else:
conn.execute(
'''INSERT OR REPLACE INTO anders_analyses
(opinion_id, case_number, court, opinion_date,
is_anders, is_trial, opinion_lists_elements,
brief_lists_elements, brief_url, brief_pdf_path,
offense_name, notes, model)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)''',
(
op_id, case_number, court, op_date,
int(is_anders) if is_anders is not None else None,
int(is_trial) if is_trial is not None else None,
int(opinion_lists) if opinion_lists is not None else None,
int(brief_lists) if brief_lists is not None else None,
brief_url, brief_pdf_path, offense_name, notes,
model_provenance,
),
)
conn.commit()
# Flag for report if Anders + trial + neither opinion nor brief listed elements
if is_anders and is_trial:
failure_reason = None
if opinion_lists is False and brief_lists is False:
failure_reason = 'Neither opinion nor Anders brief lists elements and supporting evidence'
elif opinion_lists is False and brief_lists is None:
failure_reason = 'Opinion does not list elements and supporting evidence; brief not available for check'
if failure_reason:
conn.execute(
'''INSERT OR IGNORE INTO anders_report_items
(case_number, court, opinion_date, case_url,
brief_url, offense_name, failure_reason)
VALUES (?,?,?,?,?,?,?)''',
(case_number, court, op_date, case_url,
brief_url, offense_name, failure_reason),
)
conn.commit()
LOG.info(' FLAGGED: %s', failure_reason)
# ── Email report ──────────────────────────────────────────────────────────────
def build_summary(conn: sqlite3.Connection, target_date: str) -> dict:
"""Compile the daily Anders summary used by the heartbeat email."""
total = conn.execute(
'SELECT COUNT(*) FROM opinions WHERE opinion_date=?',
(target_date,),
).fetchone()[0]
rows = conn.execute(
'''SELECT aa.case_number, aa.court, aa.is_trial,
aa.opinion_lists_elements, aa.brief_lists_elements,
aa.brief_url, aa.offense_name, o.case_url
FROM anders_analyses aa
LEFT JOIN opinions o ON o.id = aa.opinion_id
WHERE aa.opinion_date=? AND aa.is_anders=1
ORDER BY aa.case_number''',
(target_date,),
).fetchall()
cols = ('case_number', 'court', 'is_trial', 'opinion_lists',
'brief_lists', 'brief_url', 'offense', 'case_url')
anders = [dict(zip(cols, r)) for r in rows]
plea = [a for a in anders if a['is_trial'] == 0]
trial = [a for a in anders if a['is_trial'] == 1]
unknown = [a for a in anders if a['is_trial'] not in (0, 1)]
opinion_covers = [a for a in trial if a['opinion_lists'] == 1]
brief_covers = [a for a in trial if a['opinion_lists'] == 0
and a['brief_lists'] == 1]
deficient = [a for a in trial if a['opinion_lists'] == 0
and a['brief_lists'] == 0]
brief_unavailable = [a for a in trial if a['opinion_lists'] == 0
and a['brief_lists'] is None]
return {
'total_opinions': total,
'anders_count': len(anders),
'plea_count': len(plea),
'trial_count': len(trial),
'unknown_count': len(unknown),
'opinion_covers': opinion_covers,
'brief_covers': brief_covers,
'deficient': deficient,
'brief_unavailable': brief_unavailable,
'all_anders': anders,
}
def _subject(summary: dict, target_date: str) -> str:
flagged = len(summary['deficient']) + len(summary['brief_unavailable'])
if flagged:
return (f'Anders Project — DEFICIENT: {flagged} case(s) — '
f'{target_date}')
if summary['anders_count'] == 0:
return f'Anders Project — {target_date} — no Anders opinions'
if summary['trial_count'] == 0:
return (f'Anders Project — {target_date} — '
f'{summary["anders_count"]} Anders (all plea/revocation)')
if summary['brief_covers']:
return (f'Anders Project — {target_date} — '
f'{summary["trial_count"]} trial Anders, '
f'{len(summary["brief_covers"])} covered by brief only')
return (f'Anders Project — {target_date} — '
f'{summary["trial_count"]} trial Anders, opinions list elements')
def _bullet_list_text(cases: list[dict]) -> list[str]:
out = []
for a in cases:
offense = a.get('offense') or '—'
out.append(f' • {a["case_number"]} ({a["court"]}) — {offense}')
return out
def _html_report(items: list[dict], target_date: str,
summary: dict | None = None) -> str:
rows = ''
for it in items:
case_link = (f'<a href="{it["case_url"]}">{it["case_number"]}</a>'
if it.get('case_url') else it['case_number'])
brief_link = (f'<a href="{it["brief_url"]}">Brief PDF</a>'
if it.get('brief_url') else '—')
rows += f"""
<tr>
<td>{case_link}</td>
<td>{it.get('court','')}</td>
<td>{it.get('offense_name') or '—'}</td>
<td>{it.get('failure_reason','')}</td>
<td>{brief_link}</td>
</tr>"""
deficiency_section = ''
if items:
deficiency_section = f"""
<h3>Deficient cases</h3>
<div class="banner"><strong>{len(items)} case(s)</strong> where appointed counsel
filed an Anders brief in a trial case but neither the COA opinion nor the brief
identified the elements of the charged offense and described supporting evidence.
</div>
<table>
<thead><tr>
<th>Case</th><th>Court</th><th>Offense</th><th>Deficiency</th><th>Brief</th>
</tr></thead>
<tbody>{rows}</tbody>
</table>
"""
heartbeat_section = ''
if summary is not None:
def _li(cases):
if not cases:
return ''
lis = ''.join(
f'<li>{a["case_number"]} ({a["court"]}) — '
f'{a.get("offense") or "—"}</li>'
for a in cases
)
return f'<ul>{lis}</ul>'
s = summary
trial_block = ''
if s['trial_count']:
parts = [f'<li>Trial: {s["trial_count"]}']
if s['opinion_covers']:
parts.append(
f'<div>Opinion lists elements: '
f'{len(s["opinion_covers"])}{_li(s["opinion_covers"])}</div>'
)
if s['brief_covers']:
parts.append(
f'<div>Opinion does not, but brief does: '
f'{len(s["brief_covers"])}{_li(s["brief_covers"])}</div>'
)
if s['deficient']:
parts.append(
f'<div><strong>Neither (DEFICIENT):</strong> '
f'{len(s["deficient"])}{_li(s["deficient"])}</div>'
)
if s['brief_unavailable']:
parts.append(
f'<div>Brief unavailable: '
f'{len(s["brief_unavailable"])}'
f'{_li(s["brief_unavailable"])}</div>'
)
parts.append('</li>')
trial_block = ''.join(parts)
else:
trial_block = '<li>Trial: 0</li>'
heartbeat_section = f"""
<h3>Daily heartbeat</h3>
<ul>
<li>Opinions analyzed: {s['total_opinions']}</li>
<li>Anders briefs identified: {s['anders_count']}</li>
<li>Plea: {s['plea_count']}</li>
{trial_block}
</ul>
"""
case_pages_section = ''
if summary is not None and summary.get('all_anders'):
links = ''.join(
(f'<li><a href="{a["case_url"]}">{a["case_number"]}</a> '
f'({a["court"]}) — {a.get("offense") or "—"}</li>'
if a.get('case_url') else
f'<li>{a["case_number"]} ({a["court"]}) — '
f'{a.get("offense") or "—"}</li>')
for a in summary['all_anders']
)
case_pages_section = f"""
<h3>Anders case pages</h3>
<ul>{links}</ul>
"""
return f"""<!DOCTYPE html><html><head><style>
body{{font-family:sans-serif;font-size:14px}}
table{{border-collapse:collapse;width:100%}}
th,td{{border:1px solid #ccc;padding:6px 10px;text-align:left}}
th{{background:#f0f0f0}}
.banner{{background:#fff3cd;border:1px solid #ffc107;padding:10px;
margin-bottom:16px;border-radius:4px}}
ul{{margin:4px 0}}
</style></head><body>
<h2>Anders Project — {target_date}</h2>
{heartbeat_section}
{case_pages_section}
{deficiency_section}
<p style="color:#666;font-size:12px">Generated by andersproject · {date.today().isoformat()}</p>
</body></html>"""
def _text_report(items: list[dict], target_date: str,
summary: dict | None = None) -> str:
lines = [f'Anders Project — daily summary for {target_date}', '']
if summary is not None:
s = summary
lines += [
f'Opinions analyzed: {s["total_opinions"]}',
f'Anders briefs identified: {s["anders_count"]}',
f' Plea: {s["plea_count"]}',
]
lines.append(f' Trial: {s["trial_count"]}')
if s['opinion_covers']:
lines.append(f' Opinion lists elements: {len(s["opinion_covers"])}')
lines += _bullet_list_text(s['opinion_covers'])
if s['brief_covers']:
lines.append(
f' Opinion does not, but brief does: '
f'{len(s["brief_covers"])}'
)
lines += _bullet_list_text(s['brief_covers'])
if s['deficient']:
lines.append(f' NEITHER (deficient): {len(s["deficient"])}')
lines += _bullet_list_text(s['deficient'])
if s['brief_unavailable']:
lines.append(
f' Brief unavailable: {len(s["brief_unavailable"])}'
)
lines += _bullet_list_text(s['brief_unavailable'])
if s['unknown_count']:
lines.append(f' is_trial unknown: {s["unknown_count"]}')
lines.append('')
if summary is not None and summary.get('all_anders'):
lines.append('Anders case pages:')
for a in summary['all_anders']:
url = a.get('case_url') or '(no link)'
lines.append(
f" • {a['case_number']} ({a['court']}) — "
f"{a.get('offense') or '—'}: {url}"
)
lines.append('')
if items:
lines.append('---')
lines.append(f'Deficient case details — {len(items)} case(s)')
lines.append('')
for it in items:
lines += [
f"Case: {it['case_number']}",
f"Court: {it.get('court','')}",
f"Offense: {it.get('offense_name') or '—'}",
f"Issue: {it.get('failure_reason','')}",
]
if it.get('case_url'):
lines.append(f"COA: {it['case_url']}")
if it.get('brief_url'):
lines.append(f"Brief: {it['brief_url']}")
lines.append('')
return '\n'.join(lines)
def send_report(items: list[dict], target_date: str,
attachments: list[Path], dry_run: bool = False,
summary: dict | None = None) -> None:
smtp_host = os.environ.get('EMAIL_SMTP_HOST', 'smtp.gmail.com')
smtp_port = int(os.environ.get('EMAIL_SMTP_PORT', '587'))
smtp_user = os.environ.get('EMAIL_AUTH_USER', os.environ.get('EMAIL_FROM', ''))
smtp_pass = os.environ.get('EMAIL_PASSWORD', '')
if summary is not None:
subject = _subject(summary, target_date)
else:
subject = (f'Anders Brief Deficiency Report — {len(items)} case(s)'
f' — {target_date}')
msg = MIMEMultipart('mixed')
msg['From'] = FROM_ADDR
msg['To'] = ', '.join(TO_ADDRS)
msg['Subject'] = subject
alt = MIMEMultipart('alternative')
alt.attach(MIMEText(_text_report(items, target_date, summary), 'plain'))
alt.attach(MIMEText(_html_report(items, target_date, summary), 'html'))
msg.attach(alt)
for p in attachments:
if p.exists():
with open(p, 'rb') as f:
part = MIMEApplication(f.read(), Name=p.name)
part['Content-Disposition'] = f'attachment; filename="{p.name}"'
msg.attach(part)
if dry_run:
print('=== DRY RUN — not sending ===')
print(f'From: {FROM_ADDR}')
print(f'To: {", ".join(TO_ADDRS)}')
print(f'Subj: {subject}')
print(f'Attachments: {[p.name for p in attachments if p.exists()]}')
print()
print(_text_report(items, target_date, summary))
return
try:
if smtp_port == 465:
server = smtplib.SMTP_SSL(smtp_host, smtp_port, timeout=30)
else:
server = smtplib.SMTP(smtp_host, smtp_port, timeout=30)
server.ehlo()
server.starttls()
if smtp_user and smtp_pass:
server.login(smtp_user, smtp_pass)
server.sendmail(FROM_ADDR, TO_ADDRS, msg.as_bytes())
server.quit()
LOG.info('Report emailed to %s', ', '.join(TO_ADDRS))
except Exception as e:
LOG.error('Email failed: %s', e)
raise
# ── Main ──────────────────────────────────────────────────────────────────────
def main() -> None:
ap = argparse.ArgumentParser(description='Anders brief audit for COA opinions')
ap.add_argument('--date', default=None,
help='opinion date to analyze, YYYY-MM-DD (default: yesterday)')
ap.add_argument('--dry-run', action='store_true',
help='print report without emailing')
ap.add_argument('--reanalyze', action='store_true',
help='re-analyze opinions already in anders_analyses')
ap.add_argument('--report-only', action='store_true',
help='skip analysis; just email pending report items')
ap.add_argument('--db', default=str(DB_PATH))
args = ap.parse_args()
if args.date:
target_date = args.date
else:
yesterday = date.today() - timedelta(days=1)
target_date = yesterday.isoformat()
conn = sqlite3.connect(args.db)
conn.execute('PRAGMA foreign_keys = ON')
init_schema(conn)
if not args.report_only:
# Fetch opinions for target date not yet analyzed
skip_clause = ('' if args.reanalyze
else 'AND o.id NOT IN (SELECT opinion_id FROM anders_analyses)')
rows = conn.execute(
f'''SELECT o.id, o.case_number, o.court, o.opinion_date,
o.file_path, o.case_url
FROM opinions o
WHERE o.opinion_date = ?
{skip_clause}
ORDER BY o.id''',
(target_date,),
).fetchall()
LOG.info('Opinions to analyze for %s: %d', target_date, len(rows))
for row in rows:
try:
process_opinion(conn, row, reanalyze=args.reanalyze)
except Exception as e:
LOG.error('Error processing %s: %s', row[1], e)
time.sleep(0.5)
# Build the daily heartbeat summary
summary = build_summary(conn, target_date)
LOG.info(
'Heartbeat: %d opinions, %d Anders (%d plea, %d trial); '
'opinion-covers=%d, brief-covers=%d, deficient=%d, brief-unavail=%d',
summary['total_opinions'], summary['anders_count'],
summary['plea_count'], summary['trial_count'],
len(summary['opinion_covers']), len(summary['brief_covers']),
len(summary['deficient']), len(summary['brief_unavailable']),
)
# Collect unsent report items (deficient cases)
items_rows = conn.execute(
'''SELECT case_number, court, opinion_date, case_url,
brief_url, offense_name, failure_reason
FROM anders_report_items
WHERE emailed_at IS NULL
ORDER BY opinion_date DESC, case_number'''
).fetchall()
cols = ['case_number', 'court', 'opinion_date', 'case_url',
'brief_url', 'offense_name', 'failure_reason']
items = [dict(zip(cols, r)) for r in items_rows]
if items:
LOG.info('%d deficient item(s) to report', len(items))
# Collect brief PDFs for attachment (deficient cases only)
attachments: list[Path] = []
for it in items:
row = conn.execute(
'''SELECT aa.brief_pdf_path FROM anders_analyses aa
JOIN opinions o ON o.id = aa.opinion_id
WHERE o.case_number = ? AND aa.brief_pdf_path IS NOT NULL
LIMIT 1''',
(it['case_number'],),
).fetchone()
if row and row[0]:
p = Path(row[0])
if p.exists():
attachments.append(p)
send_report(items, target_date, attachments,
dry_run=args.dry_run, summary=summary)
if items and not args.dry_run:
conn.execute(
"UPDATE anders_report_items SET emailed_at=datetime('now','localtime') "
"WHERE emailed_at IS NULL"
)
conn.commit()
conn.close()
if __name__ == '__main__':
main()