{"v":{"id":458,"url":"https:\/\/www-s01.apinet.com\/codeforge\/api\/v1\/repos\/apiintl\/hhmanager\/issues\/205","html_url":"https:\/\/www-s01.apinet.com\/codeforge\/apiintl\/hhmanager\/issues\/205","number":205,"user":{"id":1,"login":"charles","login_name":"","source_id":0,"full_name":"Charles Willgren","email":"charles@apinet.com","avatar_url":"https:\/\/www-s01.apinet.com\/codeforge\/avatars\/d3c894f7297c5a18eb3f6ac837c335b11a00aa9cc0961323771632b1e2d61bb2","html_url":"https:\/\/www-s01.apinet.com\/codeforge\/charles","language":"","is_admin":false,"last_login":"0001-01-01T00:00:00Z","created":"2025-12-08T10:03:41-06:00","restricted":false,"active":false,"prohibit_login":false,"location":"","pronouns":"","website":"","description":"","visibility":"public","followers_count":0,"following_count":0,"starred_repos_count":0,"username":"charles"},"original_author":"","original_author_id":0,"title":"Authorization faxes: finish the pipeline, using Presidio-redacted samples to calibrate it","body":"**Background.** Service authorizations reach the agencies by fax every day. These are the state's Form 2101 (Authorization for Community Care) and Form 2067 (Case Information), printed and then faxed. We planned to read them automatically: OCR the fields and checkboxes, let an administrator check what was read, and use the result. The plan is `docs\/processed\/FaxPlan.md` (June 2026, archived). The decisions in it still apply: **everything runs on our own servers, no cloud OCR**, a template per form, a human review step, about 20 faxes a day. This issue now tracks the remaining work.\n\n## What is built\n\n- **Intake.** `scripts\/fax-fetch-email.php` reads the `Fax_API@legacyhhc.com` mailbox over IMAP\/OAuth2 (`docs\/FaxMailboxSetup.md`). Senders are trusted by envelope against `FAX_EMAIL_SENDER_ALLOWLIST` (#35). `fax_routes` maps the receiving number to an agency.\n- **Processing.** `scripts\/fax-worker.php` calls the Python worker in `fax\/` (preprocess \u2192 classify \u2192 register \u2192 extract with Tesseract + OpenCV). The ROI templates are in `fax\/templates\/`. `fax\/register.py` aligns each page to the form's printed border, because print-then-fax never lines up exactly. Values are stored encrypted in `fax_jobs` \/ `fax_pages` \/ `fax_fields`.\n- **Review.** `\/fax-queue` and `\/fax-review\/{pk}`: the page image with the boxes drawn on it, editable fields, and Accept. Gated on `role\/Administrator`. `faxImage` checks the agency (fixed in the HIPAA sweep).\n- **Tooling.** `scripts\/fax-reprocess.php --debug` prints non-PHI diagnostics and `fax\/overlay.py` draws the boxes on a page.\n\n## What is not done\n\n1. **Phase 0 never happened.** The plan called for 30\u201350 real faxes of each form to calibrate and measure against. We have **one**, `ExampleFax.pdf`, which was redacted by hand in Photoshop. That's the blocker, and it is why this issue proposes Presidio (below).\n2. **The 2067 template is uncalibrated** and has no `registration` block. The 2101 was calibrated against that one example. Its schedule hours, copays, agency contact, signature, `diagnosis_40` and `comments_agency` boxes are unverified.\n3. **Production scheduling isn't in the repo.** `scripts\/setup-systemd.sh` installs no fetch or worker timers (FaxMailboxSetup \u00a75 describes them). Check what prod actually runs, and whether admin consent was ever granted for the mailbox app (FaxMailboxSetup \u00a71\u20133).\n4. **An accepted fax goes nowhere.** Accept sets a status, and nothing links the fax to a client or compares it with the authorization in VESTA (`hh_auth`). That comparison is what the work is for. **Decided: it raises a flag against VESTA** (see step 6).\n5. **Corrections are not kept as tuning data** (plan \u00a75.8), and there are no accuracy numbers (\u00a79).\n6. **Originals are not in the document store.** `fax_jobs.original_pdf_path_enc` is a path in a workflow-specific store. DocumentStoragePlan says fax originals move to the blob store later. This should happen before production volume builds up.\n7. **Review access is the access level, not a right.** A `fax.review\/*`-style right is now possible (the roles work came after the plan). It needs `account_type_may_hold()` classification and must stay staff-only.\n\n## Microsoft Presidio redacts the samples for analysis\n\nWe can't calibrate or measure on faxes nobody is allowed to look at, and hand-redacting 60\u2013100 pages doesn't scale. [Presidio](https:\/\/github.com\/microsoft\/presidio) is MIT-licensed Python that runs entirely on-prem (spaCy NER plus pattern recognizers). Its `presidio-image-redactor` OCRs with Tesseract, which we already run, and blacks out what it finds. So it keeps to the plan's \"no PHI egress\" rule.\n\n**A new tool, `fax\/redact.py`, run on the box where the faxes are:**\n\n- **Layer 1, template redaction (deterministic).** On a classified and registered page, black out every ROI the template marks as identifying: name, client no., Medicaid no., address, phone, DOB, caseworker and so on. Add a `phi: true` key per field in `*_roi.json`. This is the layer we can trust, because we know where those fields are.\n- **Layer 2, Presidio over everything else.** Run the analyzer on the OCR words outside the Layer 1 boxes (comments, diagnosis, the fax header banner, handwriting OCR happened to catch). Add custom recognizers for our identifiers: Medicaid\/client numbers, TX contract numbers (`{number}-{program}-{payer}`), BJNs, NPIs, phone and fax numbers. Presidio's built-ins don't know these.\n- **Keep the printed form.** Preprinted labels, ruled lines and checkbox marks stay. They are what registration, classification and checkbox thresholds are calibrated on.\n- **Keep the dates (decided).** Authorization dates (form date, begin\/end, date of order) stay, and so do units, service codes, RUG and the 2060 score: they are what the flag compares. **DOB is still redacted.** Presidio's `DATE_TIME` entity is turned off, and DOB is handled by Layer 1's box.\n- **Output removes pixels, not just covers them.** Rasterize, fill black, write a new image-only PDF, and strip metadata (the same standard as `redact.py` in apiconnect#28).\n- **Leftover check.** Re-OCR the output and **fail** if any value read from a Layer 1 field, or any Layer 2 hit, still appears. A sample that fails is not written.\n- **A person still looks, then samples come to dev (decided).** Presidio's recall on noisy fax OCR won't be perfect, so someone pages through each redacted sample on the secure box first. Checked samples come to dev into a **gitignored** folder, never the repo (unlike `ExampleFax.pdf`). With the dates kept they aren't de-identified under Safe Harbor. They are a limited data set, so treat them as restricted: the dev box only, and never passed to an outside service, Claude included. For Claude-assisted analysis use the masked-layout approach from apiconnect#28.\n\n**What the corpus can answer.** Layout, registration, classification, checkbox detection, and which boxes are blank or filled. Because the dates and codes are kept, it also measures **OCR accuracy on those fields**, which are the ones the flag depends on. Accuracy on names and identifiers can't be measured from it, because they're gone. Measure those on the server against reviewer corrections, and report only aggregate numbers.\n\n## Overlap with apiconnect#28 (Workshop Document redaction)\n\nBoth start from scanned PDFs and use PyMuPDF + Tesseract, and both need \"the pixels are gone\" plus a save-time leftover check. #28's follow-up also has Claude suggest fields from a masked layout. Presidio could be the **local** suggester there, run before or instead of the Claude call. Keep the code separate (Workshop Document is meant to be separable, and the fax redactor knows our templates), but use one approach and one leftover-check rule. Whichever is built second should take its recognizers and verification from the first.\n\n## Work, in order\n\n1. **Get faxes flowing on prod**: mailbox consent\/auth, `fax_routes` rows, fetch and worker timers in `setup-systemd.sh`. Faxes then accumulate for the corpus. Nobody needs to work the queue yet.\n2. **`fax\/redact.py`**: Layer 1 + Layer 2 + leftover check + a test on `ExampleFax.pdf` and synthetic pages (pure parts tested standalone, as everywhere else).\n3. **Build the corpus**: 30\u201350 of each form, redacted, checked, and copied to dev.\n4. **Calibrate**: 2067 boxes and registration. Re-verify 2101 across the corpus, with the perturbation test (offset, stretch, rotate).\n5. **Measure**: keep reviewer corrections (encrypted). A script reports per-field accuracy without values. Set the auto-accept threshold from those numbers.\n6. **Flag against VESTA.** Only an **accepted** fax raises flags, never raw OCR.\n   - **Match the fax to a client** by client no. \/ Medicaid no. through the same resolver the report joins use (`VestaResolver`, against the agency's live cases). If the number reaches nobody or two people, raise no flag and say so on the review screen. Don't guess.\n   - **Compare it with `hh_auth`** by contract\/authorization: begin and end dates, units, service items, and (for a 2101) New \/ Update \/ Terminate.\n   - **Direction and lag.** The fax is the newer source: the office keys VESTA *from* it. So the flag means \"VESTA hasn't caught up with this fax\", raised only after a grace period (default N days from the fax date, setting to agree with the office). PipelineReview has the lesson: write down each feed's cadence before comparing. MESAV is not involved.\n   - **Candidate flag types:** *Fax authorization not in VESTA*; *Fax dates differ from VESTA*; *Fax units differ from VESTA*; *Fax terminates an authorization VESTA still shows*. They resolve automatically when the next VESTA import agrees. One flag per (client, type, authorization), with `source_ref` = the fax's authorization key, so the flag key stays stable.\n   - Pure comparison rules in their own `code\/*_rules.inc.php` file (or `hh_rules.inc.php`), labels in `hh_flag_display.inc.php`, and a sweep that owns its family, like `TMHP_*_FLAG_TYPES`. Add entries in Flags.md and FlagRules.md.\n7. **Hardening**: originals into the document store, a review right in place of `role\/Administrator`, and a living `docs\/Fax.md` reference.\n\n## Decisions\n\n- [x] Dates in redacted samples: **keep them** (DOB still redacted). (2026-10-21)\n- [x] Samples may come to **dev** after a person has checked them. (2026-10-21)\n- [x] An accepted fax **raises a flag against VESTA**. (2026-10-21)\n- [ ] Grace period before a fax-vs-VESTA difference is flagged (ask the office how long keying normally takes).\n- [ ] Confirm the mailbox app consent status with whoever has tenant access.","ref":"","assets":[],"labels":[{"id":33,"name":"action","exclusive":false,"is_archived":false,"color":"d97706","description":"Action item tracked on the Connect dashboard","url":"https:\/\/www-s01.apinet.com\/codeforge\/api\/v1\/repos\/apiintl\/hhmanager\/labels\/33"},{"id":11,"name":"area:documents","exclusive":false,"is_archived":false,"color":"1d76db","description":"Uploaded file storage and serving","url":"https:\/\/www-s01.apinet.com\/codeforge\/api\/v1\/repos\/apiintl\/hhmanager\/labels\/11"},{"id":3,"name":"design","exclusive":false,"is_archived":false,"color":"5319e7","description":"Design to agree before building","url":"https:\/\/www-s01.apinet.com\/codeforge\/api\/v1\/repos\/apiintl\/hhmanager\/labels\/3"},{"id":1,"name":"feature","exclusive":false,"is_archived":false,"color":"0e8a16","description":"New user-facing capability","url":"https:\/\/www-s01.apinet.com\/codeforge\/api\/v1\/repos\/apiintl\/hhmanager\/labels\/1"},{"id":18,"name":"size:weeks","exclusive":false,"is_archived":false,"color":"bfbfbf","description":"A week or more","url":"https:\/\/www-s01.apinet.com\/codeforge\/api\/v1\/repos\/apiintl\/hhmanager\/labels\/18"}],"milestone":null,"assignee":null,"assignees":null,"state":"open","is_locked":false,"comments":2,"created_at":"2026-10-21T17:42:44-05:00","updated_at":"2026-11-03T14:41:14-06:00","closed_at":null,"due_date":null,"pull_request":null,"repository":{"id":27,"name":"hhmanager","owner":"apiintl","full_name":"apiintl\/hhmanager"},"pin_order":0}}