aboutsummaryrefslogtreecommitdiffstats
path: root/docs/superpowers/specs/2026-11-09-redact-tool-design.md
blob: 34d83da830dc055272c1be7213eed0a6c106033e (plain)
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
# PII redaction tool: design

Date: 2026-11-09. Status: implemented (2026-11-09).

## Goal

Ask the assistant to black out personal data in an attached PDF or image
and get a redacted copy beside the original. The original is never
touched. Text PDFs, scanned PDFs and images are all handled.

## Tool

`examples/tools/redact.toml` and `examples/tools/redact.py` (stdlib only,
like `updates.py`), installed to `~/.config/desktop-assistant/tools/`.

```toml
description = "Redact personal data (PII) from a PDF or image: writes a copy with black boxes, '<name>-redacted.<ext>' beside the original, and opens it for the user to check. Emails, phone numbers, IBANs, codici fiscali and card numbers are always found. Pass in items every other piece of personal data you see in the document: names, addresses, account or document numbers, exactly as written there. The original is not changed."
command = ["redact.py", "{path}", "{items}"]
cloud = ["hetzner"]
timeout = 120

[params.path]
type = "string"
description = "Absolute path of the attached file, as shown in its file block."

[params.items]
type = "string"
description = "Extra strings to black out, one per line, exactly as they appear in the document. Empty if none."
```

## Detection

Two sources, both always applied:

- **Regex**, in the script: email, phone (Italian and international
  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.

## Word boxes

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 go through `magick -auto-orient` first, since `-strip` drops
  the EXIF orientation), then `tesseract <png> - -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.

## Output

Each page rendered to PNG, `magick` draws filled black rectangles over the
matched boxes (2 px pad), then:

- 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, `-strip`ped of EXIF (GPS, owner).
- Nothing matched: nothing is written, and the script says so.

Written as `<name>-redacted.<ext>` beside the original, `-redacted-2`,
`-3`... if taken. Opened with `xdg-open`, detached, for the user to
review every page.

The script prints counts per kind and pages touched, for example
`redacted 7 on 3 pages: 2 email, 1 iban, 4 items. Not found: "Via Roma 3"`.
Matched values are never printed; items that were not found are, so the
model can tell the user what was missed.

## Errors

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: <name> (<abs path>) ---` in every chat, and images, which
   had no block, get `--- image: <name> (<abs path>) ---`. 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
   strings. In `answer()`, a cloud chat is offered a tool when its `cloud`
   is `true` or contains the chat's provider (the part of the model id
   before `:`). `redact` lists only `hetzner`, a no-retention provider: a
   provider added later gets nothing until named.

## Dependencies

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

`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.