feat(presidio): add {id} keyed-hash placeholders

Merged
JE
2d ago
+380-56

Closes #1675 (proposal 1).

Problem. {n} placeholders are countable and per request. Models extend the sequence: on a list the analyzer only partly recognised (9 replacements), a model wrote 34 made-up <PERSON_n> into one tool-call argument, none of them restorable, and a made-up number that collides with a real one restores to the wrong value. A placeholder a client carries back into a later request is unmappable and sticks.

Change. placeholder_format takes {id} instead of {n}: the ID is HMAC-SHA256(id_salt, type ∘ value) in base32, 12 characters. Opt-in; the default format is unchanged. id_salt is a secret instance setting, required (≥ 16 characters) with {id}; instances sharing a request must share it. Restore, stream restore and tool-call arguments are untouched — they match the format's pattern and look the key up in the same table.

With the same salt the same value keeps its ID across requests, so a placeholder replayed in a later turn resolves again, and an ID the model makes up can never stand for a value.

Tests. placeholder_id_test.go: format validation, salt requirement, IDs keyed by value, type and salt, stability across two requests, every spelling restored (case, Markdown, HTML, JSON-escaped, made-up, wrong length), and a two-turn plugin test. TestPlugins_Presidio_E2E gains a case that configures {id} + id_salt through the admin API and restores a replayed hashed ID through a streamed reply. Sabotage run: reverting the one-line lookup fix in matchKey fails the three restore tests.

Not in this PR. A decision for placeholders that stay unmapped on the way out (proposal 2) and string_replace on tool-call arguments (proposal 3), each as a separate PR. operator: hash remains an unsalted SHA-256 of the value; a separate concern. Docs updated in en and de; zh and pl left as they are.

Summary by CodeRabbit

  • New Features
    • Presidio supports {id} placeholders that generate stable, hashed identifiers for the same value and entity type when the same secret salt is used.
    • Hashed placeholders can be restored across requests when the salt remains consistent.
  • Documentation
    • Documented the 16-character minimum salt requirement, sharing the salt across instances, and how hashed identifiers affect privacy, collision risk, and token usage.
  • Bug Fixes
    • Configuration now rejects {id} formats when the salt is missing or too short, and ignores surrounding whitespace when using the salt.