-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdisposition_derivation.py
More file actions
269 lines (228 loc) · 11.5 KB
/
Copy pathdisposition_derivation.py
File metadata and controls
269 lines (228 loc) · 11.5 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""Server-side reconstruction of the strict EntityDispositionSchema from the
loose wire-contract SimpleDispositionResult + the per-entity context columns.
The disposition_analyzer LLM now emits a minimal `SimpleDispositionResult`
(8 optional/loose fields per item). This module rebuilds the strict form
deterministically: pair each simple item with its entity context (by id,
with entity_label/value echoes as belt-and-braces), derive needs_protection,
and template protection_reason when the model did not provide one.
No LLM calls; no I/O. Pure python for the reconstruction column.
"""
from __future__ import annotations
import logging
from anonymizer.engine.schemas.rewrite import (
EntityDispositionSchema,
SensitivityDispositionSchema,
SimpleDispositionItem,
SimpleDispositionResult,
)
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Derivation helpers
# ---------------------------------------------------------------------------
def derive_needs_protection(method: str) -> bool:
"""Tautological with EntityDispositionSchema._validate_protection_consistency.
If the model picks any method other than leave_as_is, the entity needs
protection; otherwise it does not. Deriving this instead of asking the
LLM for it eliminates the consistency-rule drift (class K).
"""
return (method or "").strip() != "leave_as_is"
# (category, method) -> template text (without leading sensitivity prefix).
# Sensitivity fills a prefix ("high-risk ...", "moderate-risk ...", "").
_REASON_TEMPLATES: dict[tuple[str, str], str] = {
("direct_identifier", "replace"): "direct identifier — replaced with a contextual surrogate",
("direct_identifier", "remove"): "direct identifier — removed to prevent re-identification",
("direct_identifier", "generalize"): "direct identifier — generalized to reduce re-identification",
("direct_identifier", "suppress_inference"): "direct identifier — suppressed to prevent inference",
("quasi_identifier", "generalize"): "quasi-identifier — generalized to reduce re-identification risk",
("quasi_identifier", "replace"): "quasi-identifier — replaced with a plausible surrogate",
("quasi_identifier", "remove"): "quasi-identifier — removed due to re-identification risk",
("quasi_identifier", "suppress_inference"): "quasi-identifier — suppressed to prevent inference",
("sensitive_attribute", "remove"): "sensitive attribute — removed to prevent disclosure harm",
("sensitive_attribute", "generalize"): "sensitive attribute — generalized to reduce harm",
("sensitive_attribute", "suppress_inference"): "sensitive attribute — suppressed to prevent disclosure",
("sensitive_attribute", "replace"): "sensitive attribute — replaced with a less harmful value",
("latent_identifier", "suppress_inference"): "latent inference — suppressed to prevent deduction",
("latent_identifier", "remove"): "latent identifier — removed to prevent inference",
("latent_identifier", "generalize"): "latent identifier — generalized to reduce inference",
("latent_identifier", "replace"): "latent identifier — replaced with a less specific surrogate",
}
_SENSITIVITY_PREFIX = {"low": "", "medium": "moderate-risk ", "high": "high-risk "}
def template_protection_reason(category: str, method: str, sensitivity: str) -> str:
"""Build a reason string guaranteed ≥10 chars (EntityDispositionSchema min_length).
Used when the LLM omits or emits a too-short protection_reason. Strong
models that provide their own document-specific reason have theirs
kept verbatim by the reconstructor.
"""
method = (method or "").strip()
category = (category or "").strip()
sensitivity = (sensitivity or "").strip().lower()
if method == "leave_as_is":
cat_label = category.replace("_", " ") if category else "entity"
return f"Low-risk {cat_label}; retained as-is for utility."
base = _REASON_TEMPLATES.get((category, method))
if base is None:
cat_label = category.replace("_", " ") if category else "entity"
method_label = method or "an appropriate method"
base = f"{cat_label} — protected via {method_label}"
prefix = _SENSITIVITY_PREFIX.get(sensitivity, "")
reason = (prefix + base).strip()
# Capitalize first letter; template shapes already make this ≥10 chars.
return reason[:1].upper() + reason[1:] if reason else "Protection applied per policy."
# ---------------------------------------------------------------------------
# Entity-context flattening
# ---------------------------------------------------------------------------
def _coerce_entity_list(raw: object) -> list[dict]:
"""DataDesigner hands context columns to custom generators in several
shapes: a pydantic-dump dict with a keyed list, a raw list, a JSON-
encoded string, or None. Normalize to a plain list of dicts.
"""
import json
if raw is None:
return []
if isinstance(raw, str):
raw = raw.strip()
if not raw:
return []
try:
raw = json.loads(raw)
except Exception:
return []
if isinstance(raw, dict):
# pydantic dump of a wrapper schema like EntitiesByValueSchema or
# LatentEntitiesSchema — the inner list lives under one of these keys.
for key in ("entities_by_value", "latent_entities", "entities", "items"):
if key in raw and isinstance(raw[key], list):
raw = raw[key]
break
else:
return []
if not isinstance(raw, list):
return []
out: list[dict] = []
for item in raw:
if isinstance(item, dict):
out.append(item)
elif isinstance(item, str):
# JSON-string-per-item (rare but seen).
try:
parsed = json.loads(item)
if isinstance(parsed, dict):
out.append(parsed)
except Exception:
continue
return out
def _flatten_context(
entities_by_value: object,
latent_entities: object,
) -> list[dict]:
"""Produce a flat, ordered list of {source, entity_label, entity_value}.
Order matches how the disposition prompt enumerates entities:
tagged entries from entities_by_value (one per (value, label) pair)
followed by latent entries. The returned list index+1 is the expected id.
"""
flat: list[dict] = []
for ev in _coerce_entity_list(entities_by_value):
value = ev.get("value", "")
labels = ev.get("labels") or []
if not labels:
flat.append({"source": "tagged", "entity_label": "", "entity_value": value})
continue
for label in labels:
flat.append({"source": "tagged", "entity_label": label, "entity_value": value})
for le in _coerce_entity_list(latent_entities):
flat.append({
"source": "latent",
"entity_label": le.get("label", ""),
"entity_value": le.get("value", ""),
})
return flat
# ---------------------------------------------------------------------------
# Reconstruction
# ---------------------------------------------------------------------------
def reconstruct_full_disposition(
simple: SimpleDispositionResult,
entities_by_value: object = None,
latent_entities: object = None,
) -> SensitivityDispositionSchema:
"""Build the strict disposition from the loose LLM output + context columns.
For each SimpleDispositionItem:
- prefer the model-echoed source/entity_label/entity_value; fall back
to the id-indexed context lookup if the echo is missing or empty.
- derive needs_protection from method.
- keep the LLM protection_reason if it stripped to ≥10 chars, else
template one from (category, method, sensitivity).
Orphan simple items (id outside the context range AND no usable echoes)
are skipped with a warning — better to return a smaller valid schema
than to drop the whole record.
Duplicate ids are de-duplicated (first occurrence wins).
"""
context = _flatten_context(entities_by_value, latent_entities)
seen_ids: set[int] = set()
full_items: list[EntityDispositionSchema] = []
for item in simple.sensitivity_disposition:
if item.id in seen_ids:
logger.warning(
"reconstruct_full_disposition: duplicate id=%s in simple output; keeping first occurrence",
item.id,
)
continue
seen_ids.add(item.id)
# Resolve (source, entity_label, entity_value). Context is the
# AUTHORITATIVE source when the id falls in range — small models
# (gemma4-e2b) routinely echo garbage in these fields (e.g. the
# entity_label in the source slot), so trusting the echo there
# corrupts the strict schema. Fall back to the LLM echo only when
# there is no context entry for this id (orphan).
idx = item.id - 1
if 0 <= idx < len(context):
ctx = context[idx]
src = ctx["source"]
lbl = ctx["entity_label"]
val = ctx["entity_value"]
else:
# Orphan path: id has no context entry. The LLM echoes are the
# only source of truth, but they may be drifted (gemma4-e4b
# observed emitting prompt section names in source). Validate
# the source enum and skip the item if both source and labels
# are unusable — a skipped orphan is better than a ValidationError
# that drops the whole record.
echoed_src = (item.source or "").strip().lower()
src = echoed_src if echoed_src in {"tagged", "latent"} else ""
lbl = item.entity_label or ""
val = item.entity_value or ""
if not src or not lbl or not val:
logger.warning(
"reconstruct_full_disposition: orphan simple item id=%s "
"(missing or drifted source/label/value, out of context range); skipping",
item.id,
)
continue
# Derive derived fields.
method = (item.protection_method_suggestion or "").strip() or "leave_as_is"
needs = derive_needs_protection(method)
# Keep LLM reason if usable, else template.
raw_reason = (item.protection_reason or "").strip()
reason = raw_reason if len(raw_reason) >= 10 else template_protection_reason(
item.category or "", method, item.sensitivity or ""
)
# Default empty LLM-drift slots to sane values so the strict schema
# doesn't reject the row. category/sensitivity are enums at the
# internal layer; empty strings would fail.
category = (item.category or "").strip() or "quasi_identifier"
sensitivity = (item.sensitivity or "").strip().lower() or "medium"
full_items.append(
EntityDispositionSchema(
id=item.id,
source=src,
category=category, # strict schema coerces via its before-validator
sensitivity=sensitivity,
entity_label=lbl,
entity_value=val,
needs_protection=needs,
protection_method_suggestion=method,
protection_reason=reason,
)
)
return SensitivityDispositionSchema(sensitivity_disposition=full_items)