#1941·xberg

feat(redaction): accept external content-inspection findings as redaction input

Author: GoldziherCreated Sep 28, 2026Updated Sep 28, 2026
LabelsenhancementP1

Motivation

A user extracts a document with xberg, runs an external content-inspection (CI) engine over the extracted text, then wants xberg to apply those findings and return a redacted rendering (e.g. Markdown). Reported with Microsoft Presidio; AWS Comprehend, Azure AI Language and Google Cloud DLP are equivalents.

Today RedactionConfig accepts only categories, custom_terms (label + literal + case_sensitive) and custom_patterns (label + regex). There is no way to feed the findings array an external engine emits. redact_with_entities (engine.rs, alef(skip)) accepts a caller entity stream but is Rust-only and not reachable from config or bindings.

Proposed API

Add an external findings source to RedactionConfig:

  • findings: Vec<ExternalRedactionFinding> — inline JSON array.
  • findings_path: Option<PathBuf> — load the array from a JSON (or JSONL) file.
  • CLI: --redaction-findings <path|-> (- = stdin).

Findings are normalized into the same literal-matcher path the NER entity stream already uses (engine.rs::compile_ner_terms), so they redact at every occurrence in every textual field and surface in redaction_report as PiiCategory::Custom(label).

Canonical schema

json
[
  {"label": "PHONE_NUMBER", "text": "(415) 555-0132", "start": 21, "end": 35, "score": 0.85},
  {"label": "PERSON", "text": "John Smith", "score": 0.92}
]
  • label — required. Engine category; becomes PiiCategory::Custom(label).
  • text — the literal to redact. Canonical (see below).
  • start / end — optional, 0-based, start inclusive / end exclusive; audit metadata (do not assume an encoding).
  • score — optional, [0.0, 1.0]; usable with a min_score threshold.
  • Unknown fields must be ignored: vendor payloads carry extras (analysis_explanation, recognition_metadata, warnings), so entries cannot use deny_unknown_fields.

Field aliases (accept raw vendor output)

Canonical Presidio AWS Comprehend Azure Language GCP DLP
label entity_type Type category infoType.name
text (derive from span) (derive from span) text (derive from span)
start start BeginOffset offset location.codepointRange.start
end end EndOffset offset + length location.codepointRange.end
score score Score confidenceScore likelihood

Why values, not offsets, are canonical

Offset encodings differ per engine and are not portable: Presidio/spaCy use Unicode code-point indices, AWS Comprehend uses UTF-8 byte offsets, Azure defaults to UTF-16 code units, GCP reports both byte and code-point ranges. Literal matching is encoding-independent and also covers the Markdown rendering, which is produced separately from the plain text.

If text is absent, xberg may derive it by slicing its own content at start/end under an explicit offset_encoding (utf8_bytes | unicode_code_points | utf16_code_units, default unicode_code_points to match Presidio). This requires the findings to have been computed against the same extraction settings — prefer supplying text.

Acceptance criteria

  • Raw Presidio analyzer output ([{entity_type,start,end,score}]) redacts when text is supplied or derivable.
  • Value-based findings redact all occurrences across all text fields (content, formatted_content, chunks, tables, metadata, …), consistent with custom_terms.
  • Findings appear in redaction_report.findings with category = Custom(label).
  • findings / findings_path work from a config file and --config-json; CLI stdin path works.
  • Unknown fields ignored; malformed entries rejected by RedactionConfig::validate.
  • Finding count capped by a SecurityLimits bound.

Out of scope

  • Offset-only, span-precise redaction — does not survive the plain/Markdown rendering split (cf. #331).
  • Mapping external labels onto the built-in PiiCategory enum.