#!/usr/bin/env python3 """ digest-fallback.py: build a digest with NO model involved. WHY. The digest is the deliverable, and a scheduled job once failed to deliver it five mornings in a row, for five different reasons (credential unavailable; a probe misreporting the cause; a probe running without the real run's flags; unescaped backticks killing the script; a run that exhausted itself one step before writing). Every fix addressed one cause and the next morning found another. This file stops trying to prevent failure and guarantees an OUTPUT: it reads files on disk and emits a real, plain digest deterministically. Ugly compared to the model-written one; never absent. SECTIONS (each only if it has content): Alerts open alerts from scripts/alert.py (the problems a job already detected) Coming up dated open items in state.md due within LOOKAHEAD_DAYS, with a COUNTDOWN. A sweep that only shows items due today surfaces the year's most important appointment on the morning it happens. That is not a reminder. Since last digest intraday ledger entries (scripts/day-ledger.py pending) Changed overnight wiki pages modified today Usage: digest-fallback.py [--date YYYY-MM-DD] [--out PATH] Env: KB_ROOT, KB_READER_BASE (link base for the KB reader, default http://localhost:8787) """ import argparse, datetime, html, json, os, re, subprocess, sys, urllib.parse from pathlib import Path KB = Path(os.environ.get("KB_ROOT", Path(__file__).resolve().parent.parent)) BASE = os.environ.get("KB_READER_BASE", "http://localhost:8787").rstrip("/") LOOKAHEAD_DAYS = int(os.environ.get("LOOKAHEAD_DAYS", 14)) DATE = re.compile(r"\b(20\d\d-\d\d-\d\d)\b") def esc(t): return html.escape(t, quote=False) def link(rel, label): return f'{esc(label)}' def md_inline(t): t = esc(t) t = re.sub(r"\[\[([^\]|]+)\]\]", lambda m: link("wiki/" + m.group(1), m.group(1)), t) t = re.sub(r"\*\*(.+?)\*\*", r"\1", t) return re.sub(r"`(.+?)`", r"\1", t) def coming_up(day): """Open '- [ ]' items in state.md carrying a date within [day - 7, day + LOOKAHEAD_DAYS].""" p = KB / "state.md" if not p.exists(): return [] today = datetime.date.fromisoformat(day) out = [] for line in p.read_text(encoding="utf-8", errors="replace").splitlines(): t = line.strip() if not t.startswith("- [ ]"): continue dates = [] for d in DATE.findall(t): try: dates.append(datetime.date.fromisoformat(d)) except ValueError: pass due = [d for d in dates if -7 <= (d - today).days <= LOOKAHEAD_DAYS] if not due: continue n = (min(due) - today).days tag = "OVERDUE" if n < 0 else "TODAY" if n == 0 else "tomorrow" if n == 1 else f"in {n} days" out.append((n, f"{tag} - {md_inline(t[5:].strip()[:220])}")) return [s for _, s in sorted(out)][:10] def run_json(args): try: r = subprocess.run([sys.executable] + args, capture_output=True, text=True, timeout=30, env=dict(os.environ, KB_ROOT=str(KB))) return r.stdout except Exception: return "" def alerts(): out = run_json([str(KB / "scripts" / "alert.py"), "list"]) return [esc(l.split("\t", 2)[-1]) for l in out.splitlines() if l.strip()] def ledger(): try: entries = json.loads(run_json([str(KB / "scripts" / "day-ledger.py"), "pending", "--json"]) or "[]") except json.JSONDecodeError: return [] rows = [] for e in entries: pages = ", ".join(f"[[{p}]]" for p in e.get("pages", [])) rows.append(md_inline(f"**{e.get('source', '')}**: {e.get('summary', '')}" + (f" ({pages})" if pages else ""))) return rows def changed_pages(day): out = [] for p in (KB / "wiki").rglob("*.md") if (KB / "wiki").exists() else []: try: if datetime.date.fromtimestamp(p.stat().st_mtime).isoformat() == day: out.append(p.relative_to(KB).as_posix()) except OSError: pass return sorted(out)[:15] def build(day): pretty = datetime.date.fromisoformat(day).strftime("%A %d %B %Y") def section(title, rows): if not rows: return "" return (f'

{title}

' + "".join(f'

{r}

' for r in rows)) pages = [link(p, Path(p).stem) for p in changed_pages(day)] body = (section("Alerts", alerts()) + section("Coming up", coming_up(day)) + section("Since last digest", ledger()) + section("Changed overnight", pages)) if not body: body = "

Nothing on file for today: no alerts, nothing due in the next two weeks, no intraday ingests.

" return ('' f'' f'Digest {day}' '
' f'
{pretty}
' '
Digest
' '

Built by the deterministic fallback: the scheduled run did not produce ' 'a digest. Content below is real but unsynthesised.

' f'{body}

{link("index.md", "Index")} - ' f'{link("state.md", "State")}

') def main(): ap = argparse.ArgumentParser() ap.add_argument("--date", default=datetime.date.today().isoformat()) ap.add_argument("--out") a = ap.parse_args() out = Path(a.out) if a.out else KB / "outbox" / f"Digest - {a.date}.html" out.parent.mkdir(parents=True, exist_ok=True) out.write_text(build(a.date), encoding="utf-8") print(f"fallback digest written: {out} ({out.stat().st_size:,} bytes)") if __name__ == "__main__": main()