From 00602b3ad7d613a1f575d1e5395406eb6b319c1d Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Mon, 9 Nov 2026 00:58:43 +0100 Subject: docs: redact tool live check Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 35 ++++++++++++- .../specs/2026-11-09-redact-tool-design.md | 61 +++++++++++++++------- 2 files changed, 77 insertions(+), 19 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 76bc0c8..86c0d94 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -30,7 +30,8 @@ built-in settings. Examples in `examples/`. Spec: Cloud models (2026-10-23): `[[cloud]]` providers in `config.toml`, keys from `key_cmd`, ids `provider:model`, chosen per chat and stored on the -conversation. Only tools with `cloud = true` go to a cloud chat. Spec: +conversation. Only tools with `cloud = true`, or `cloud` naming the provider +(`["hetzner"]`), go to a cloud chat. Spec: `docs/superpowers/specs/2026-10-23-cloud-models-design.md`. Run: `python3 assistant.py` (`--test` for the self-check), or @@ -296,6 +297,38 @@ at the user's choice. - Every call is an admin login, which Foldergram rate limits; untested under repeated questions. +### Redact tool, live check (2026-11-09) + +`redact` (`examples/tools/redact.py`): a copy of an attached PDF or image +with personal data blacked out, `-redacted.` beside it, opened +for review. Regex for emails, phones, IBANs, card numbers and 16-character +codes (codice fiscale); the model adds names and addresses as items. +Output PDFs are page images only. `cloud = ["hetzner"]`: a tool's `cloud` +may name trusted no-retention providers. Attachment blocks now carry the +file's path, images included. Spec: +`docs/superpowers/specs/2026-11-09-redact-tool-design.md`. + +- tesseract had no language data; the standard `ita` and `eng` (16.0 and + 23.5 MB) are in `/usr/share/tessdata/`, copied by hand. +- ImageMagick's policy forbids writing PDF: the script writes it itself. +- Through a second daemon on a scratch socket, config and data dir, with + made-up documents. A text PDF ("Redact the personal data in this."): + the right path, name and address as items, 5 boxes (email, IBAN, + phone, 2 items), no text layer left; tool call at 14.7 s, reply at + 17.6 s (Gemma). The same in a Hetzner chat: same call at 7.2 s. +- An image ("Togli i dati personali da questa immagine."): name and + address as items, email found, but the codice fiscale stayed visible. + tesseract read `80` as `E0`, the strict layout failed, and the model + had not listed it, trusting the description's "always found". The rule + is now any 16-character code with 3+ letters and 3+ digits, and the + description asks for these in items on scans too. Second run: 4 boxes, + the code included. The model still did not list it. +- Review findings fixed before the live check: content outside the + CropBox leaked into the output, `/Rotate` pages were stretched, + letter-spaced text was not matched. +- The model calls the found items "the 2 items you provided": they were + its own. + ### Cloud models, live check (2026-10-23) Hetzner's inference API (`https://inference.hetzner.com/api/v1`, diff --git a/docs/superpowers/specs/2026-11-09-redact-tool-design.md b/docs/superpowers/specs/2026-11-09-redact-tool-design.md index 080d752..34d83da 100644 --- a/docs/superpowers/specs/2026-11-09-redact-tool-design.md +++ b/docs/superpowers/specs/2026-11-09-redact-tool-design.md @@ -1,6 +1,6 @@ # PII redaction tool: design -Date: 2026-11-09. Status: approved, not implemented. +Date: 2026-11-09. Status: implemented (2026-11-09). ## Goal @@ -33,8 +33,15 @@ description = "Extra strings to black out, one per line, exactly as they appear Two sources, both always applied: - **Regex**, in the script: email, phone (Italian and international - forms), IBAN, codice fiscale, card numbers (13-19 digits passing Luhn). - Dates of birth are left out: any date would match. + forms, landlines with brackets, slashes or dashes), IBAN (mod 97), card + numbers (13-19 digits passing Luhn), and any 16-character code mixing + at least 3 letters and 3 digits, which covers a codice fiscale even + when OCR misreads it. A validated match that absorbed a neighbouring + token is retried on its longest valid sub-span. Dates of birth are left + out: any date would match. +- Consecutive single-character words are joined without a space: + letter-spaced text and per-character form boxes come out one glyph per + word. Curly apostrophes match straight ones. - **Model items**: free-form PII the model saw (names, addresses). Matched case-insensitively over runs of consecutive words, so a multi-word name spanning several boxes is found. @@ -45,9 +52,17 @@ Per page: - PDF page with text: `pdftotext -bbox-layout -f N -l N`, boxes in points, scaled to the render's pixels. -- Image, or PDF page with no text (a scan): render with `pdftoppm` (images - used as is), then `tesseract - -l ita+eng tsv` for word boxes in - pixels. +- Image, or PDF page with no text (a scan): render with `pdftoppm` + (images go through `magick -auto-orient` first, since `-strip` drops + the EXIF orientation), then `tesseract - -l ita+eng tsv` for word + boxes in pixels. +- PDF page with text and images (`pdfimages -list`): OCR'd as well, and + OCR words whose centre falls inside a text word's box are dropped. +- Both read the CropBox (`-cropbox`): content a viewer hides outside it + would otherwise show in the output. Output page size comes from the + render, so rotated pages keep their shape. +- Multi-frame images (GIF, TIFF) are refused: tesseract reads only the + first frame. Renders use the attachment rule: 150 dpi, lowered to fit 1600 px. @@ -56,9 +71,11 @@ Renders use the attachment rule: 150 dpi, lowered to fit 1600 px. Each page rendered to PNG, `magick` draws filled black rectangles over the matched boxes (2 px pad), then: -- PDF in: pages joined with `magick` into one PDF. The result is pixels +- PDF in: the script writes the PDF itself, one lossless RGB image per + page (ImageMagick's policy forbids PDF output). The result is pixels only: no text layer, no metadata, nothing under the boxes survives. -- Image in: same format out. +- Image in: same format out, `-strip`ped of EXIF (GPS, owner). +- Nothing matched: nothing is written, and the script says so. Written as `-redacted.` beside the original, `-redacted-2`, `-3`... if taken. Opened with `xdg-open`, detached, for the user to @@ -71,14 +88,17 @@ model can tell the user what was missed. ## Errors -Unreadable path, unsupported type, missing tesseract language data -(`ita`, `eng`), or a failing helper: one-line message, nothing written. +Unreadable path, unwritable folder, multi-frame image, missing +tesseract language data (`ita`, `eng`), or a failing helper: one-line +message, nothing written. An item not found is reported, not fatal. ## Daemon changes 1. **Path in the file block.** `attach()` writes - `--- file: () ---` in every chat. The content already + `--- file: () ---` in every chat, and images, which + had no block, get `--- image: () ---`. A path with a + newline is left out. The content already goes to whatever model the chat uses; the path reveals less than that. 2. **Per-provider tool trust.** A tool's `cloud` is `true` (any provider) or a list of provider names. `load_tools` accepts a bool or a list of @@ -89,20 +109,25 @@ An item not found is reported, not fatal. ## Dependencies -tesseract `ita` and `eng` traineddata, not installed now -(`/usr/share/tessdata` has none). Packaging belongs in `my-slackbuilds`; -sizes are stated before anything is downloaded. +tesseract `ita` and `eng` traineddata, standard set (tessdata, 16.0 and +23.5 MB), copied by hand into `/usr/share/tessdata/` on 2026-11-09. Not +packaged. ## Check -`examples/tools/test-redact.sh`: builds a text PDF and a PNG containing a -known email, IBAN and name, runs `redact.py` with the name as an item, -and asserts that `pdftotext` of the redacted PDF finds none of them, that -pixels at the known boxes are black, and that the original is unchanged. +`redact.py --test`, like `updates.py`: the rules on word lists, then +text, cropped, rotated, mixed text-and-image and scanned PDFs, a PNG, an +EXIF-rotated JPEG and a multi-frame GIF end to end: no text layer, black +pixels at the known boxes, original unchanged, `-redacted-2` on a second +run, errors as one line. ## Ceilings - OCR that misreads a word leaves it unredacted. The viewer review is the safety net, and the reply says what was not found. - A redacted PDF has no selectable text, by design. +- Kerned or diagonal text, and words broken at a line end (`Ros-`/`si`), + are not matched. +- OCR misreads also stop a model item from matching; it comes back as + not found. - The model decides the free-form items; a 12B model can miss some. -- cgit v1.2.3