Anonymizer icon indicating copy to clipboard operation
Anonymizer copied to clipboard

fix(replace): validate substitution values and detected-entity mapping coverage

Open memadi-nv opened this issue 3 weeks ago • 0 comments

Problem

In Substitute mode, already detected entities can remain unchanged even when a generated replacement map passes structural schema validation. Saved local notebook experiments show two failure patterns: missing requested mappings, and copied originals whose Unicode spaces differ from the requested values. In the second case, synthetic values were generated but their mappings were discarded by exact-match filtering.

Observed cases from saved local experiments

These observations come from pre-existing DataDesigner replacement-map generation parquet files, including notebook experiments that use the shared replacement-map stage for Rewrite. They are actual saved experiment outputs, not confirmed production incidents. The files contain parsed generation maps before Anonymizer's filtering, not raw provider HTTP responses.

1. Requested mappings were omitted

A saved GPT-OSS map contained 31 mappings for 33 requested (value, label) pairs, with no unrelated returned pairs. The missing labels were field_of_study and nationality. The input text was 1,199 characters.

The returned entries were structurally valid: schema validation checks entries that exist, not whether every requested pair has an entry. Two requested pairs were absent from the saved generation output.

2. Copied originals changed Unicode spaces

A saved Gemma map contained 23 entries for 23 requested pairs, with synthetic values present. However, six copied originals changed U+202F (narrow no-break space) to U+2009 (thin space). The labels were three company_name entries and one each of state, street_address, and university.

These entries also passed structural schema validation. Six returned (original, label) pairs did not exactly match the requested inventory, despite having synthetic values. Equal input/output entry counts therefore did not establish complete exact-pair coverage.

Local artifact provenance (generated files, not committed)

Both examples record DataDesigner 0.9.1. The observations above compare the saved requested entity inventory directly with the parsed generated replacement map.

  • Omission: docs/notebooks/.anonymizer-artifacts/replace-map-generation_10-06-2026_230425/parquet-files/batch_00000.parquet, zero-based row 21. Parquet SHA-256: 8b3e348df3fc7d4b657b41dfa0c36ff79ba3edd83c10608a9d7304cd1009e12a.
  • Unicode spaces: build/notebooks/disposition-experiment-gemma/.anonymizer-artifacts/replace-map-generation/parquet-files/batch_00000.parquet, zero-based row 2. Parquet SHA-256: 247cc794d102a88f618858e86e2c12bae1f71ad8cc650757784aa2c29b36cebd.

Evidence limits

The scan covered 102 saved generation files and 1,072 saved rows. Of those, 52 rows lacked exact requested pairs; eight had missing pairs with no unrelated entries. Records and inventories repeat across configurations, so these counts are not a production failure rate or a controlled model comparison.

No whitespace-only synthetic values were found in this collection. Rejecting them is a preventive validation improvement. Per-request finish reasons and full token accounting were unavailable, so neither context exhaustion nor the model's internal reason for an omission is established.

Proposed resolution in PR #396

  • Preserve the exact unique requested (value, label) inventory before generation, then check coverage after schema validation and filtering.
  • Prefer exact mappings. Reconcile only supported space variants (U+00A0, U+2009, U+202F, and ASCII space) when exactly one pair in the original inventory matches under the same exact label. Restore the trusted original; leave source text, offsets, and generated synthetic values unchanged. Ambiguous or unmatched entries remain missing.
  • Retain accepted mappings and retry only missing pairs once. Return complete records; report still-incomplete records or failed completeness retries through failed_records while preserving complete records. Repeated occurrences of the same pair need one mapping; no-entity records skip generation.
  • Reject empty and whitespace-only synthetic values while preserving valid one-character and padded values. This shared schema rule also applies to rewrite replacement maps.

DataDesigner's existing whole-response schema retries remain separate from the single completeness retry. This does not introduce per-entry schema recovery. Completeness retry and Unicode reconciliation are Substitute-only; Rewrite's omitted-mapping recovery policy stays unchanged. Initial workflow-level errors retain existing handling.

Validation and scope

Local validation for PR #396 completed with 1,882 passing tests and two skipped tests, including real-adapter preview/batch tests with a local scripted provider, exact/ambiguous Unicode matching, targeted retry and failure handling, and blank-value rejection. Formatting, lint, type, lockfile, copyright, documentation build, and commit checks passed.

Relevant code: engine/schemas/replace.py, engine/replace/llm_replace_workflow.py, engine/replace/replace_runner.py, and engine/replace/strategies.py under src/anonymizer/.

Conflicting duplicates, identity-preserving substitutes, invalid span offsets, and broader Unicode normalization remain outside this issue's scope.

memadi-nv avatar Nov 08 '26 06:11 memadi-nv