-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathbuild_calibration_review_decisions.py
More file actions
309 lines (269 loc) · 11.6 KB
/
Copy pathbuild_calibration_review_decisions.py
File metadata and controls
309 lines (269 loc) · 11.6 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""Convert a calibration-error review queue into reproducible review decisions.
This script records deterministic curriculum routing for calibration errors. It
does not promote queue rows into gold training data by itself.
"""
from __future__ import annotations
import argparse
import json
from collections import Counter
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
DEFAULT_QUEUE = Path(
"research/datasets/calibration_error_review_queue_qwen_pooled_policy_v3_warn030_2026-06-03.json"
)
DEFAULT_OUTPUT = Path(
"research/datasets/calibration_error_review_decisions_qwen_pooled_policy_v3_warn030_2026-06-03.json"
)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Build reviewed curriculum decisions from a calibration-error queue."
)
parser.add_argument(
"--queue",
type=Path,
default=DEFAULT_QUEUE,
help="Review queue JSON produced by research.build_calibration_error_queue.",
)
parser.add_argument(
"--output",
type=Path,
default=DEFAULT_OUTPUT,
help="Reviewed decisions JSON output path.",
)
parser.add_argument(
"--source-calibration",
default=None,
help="Calibration JSON path to record in the output. Defaults to queue.source_calibration.",
)
parser.add_argument(
"--decision-policy",
default=None,
help="Runtime decision-policy name to record, for example corroborated-block-v3.",
)
parser.add_argument(
"--pretty",
action="store_true",
help="Pretty-print the generated decision summary.",
)
return parser.parse_args()
def utc_now() -> str:
return datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%SZ")
def row_id(entry: dict[str, Any]) -> str:
row = entry.get("row") or {}
return str(row.get("id") or entry.get("queue_id") or "unknown")
def row_scores(entry: dict[str, Any]) -> dict[str, Any]:
row = entry.get("row") or {}
return {
"activation_score": row.get("activation_score"),
"static_score": row.get("static_score"),
"risk_score": row.get("risk_score"),
"policy_decision": row.get("policy_decision"),
"static_findings": row.get("static_findings", []),
}
def is_regression_fixture(entry: dict[str, Any]) -> bool:
row = entry.get("row") or {}
source = str(row.get("source") or "")
style = str(row.get("style") or "")
rid = row_id(entry)
return (
style == "regression_fixture"
or source == "activation_scanner_regression_cases.json"
or rid.startswith("secret-negation")
)
def is_hidden_carrier_without_context(entry: dict[str, Any]) -> bool:
row = entry.get("row") or {}
style = str(row.get("style") or "")
preview = str(row.get("text_preview") or "").lower()
return style.startswith("bipia_text_attack") or (
"external instruction carrier" in preview and "bipia-text_attack" in row_id(entry)
)
def is_metadata_weak_label(entry: dict[str, Any]) -> bool:
row = entry.get("row") or {}
source = str(row.get("source") or "").lower()
style = str(row.get("style") or "").lower()
return "metadata" in source or "metadata" in style
def clean_decision(entry: dict[str, Any]) -> dict[str, str]:
row = entry.get("row") or {}
static_findings = row.get("static_findings") or []
if static_findings:
return {
"review_decision": "add_static_policy_regression_candidate",
"output_action": "review_static_context_before_training_promotion",
"curriculum_level": "L2_benign_lookalike",
"label_status": "clean_static_false_positive_candidate",
"reason": (
"The row is labelled clean but static/policy evidence fired; keep it as a "
"policy-regression candidate until the action-level context is reviewed."
),
}
return {
"review_decision": "keep_as_benign_activation_holdout",
"output_action": "use_for_false_positive_eval_not_gold_training_yet",
"curriculum_level": "L5_external_holdout",
"label_status": "clean_but_not_tool_mcp_carrier",
"reason": (
"The row is labelled clean and has no static finding; use it as a benign "
"activation holdout before promoting it into gold scanner training."
),
}
def missed_poison_decision(entry: dict[str, Any]) -> dict[str, str]:
if is_hidden_carrier_without_context(entry):
label_status = "weak_label_scanned_text_missing_unsafe_action"
reason = (
"The scanned text contains an external-instruction carrier but not the "
"surrounding tool/task context that makes it unsafe."
)
elif is_metadata_weak_label(entry):
label_status = "weak_metadata_label_content_missing"
reason = (
"The malicious label appears to depend on metadata or package context that "
"is not present in the scanned text."
)
else:
return {
"review_decision": "promote_hard_positive_signal_for_block_policy",
"output_action": "add_clean_twin_or_static_rule_before_hard_block_training",
"curriculum_level": "L4_real_carrier_adapted",
"label_status": "poisoned_missed_positive_candidate",
"reason": (
"The row is labelled poisoned but allowed; review it as a candidate for "
"stronger activation training or static corroboration."
),
}
return {
"review_decision": "quarantine_until_hidden_carrier_reconstructed",
"output_action": "holdout_or_reconstruct_context_before_training",
"curriculum_level": "L5_external_holdout",
"label_status": label_status,
"reason": reason,
}
def near_block_poison_decision(entry: dict[str, Any]) -> dict[str, str]:
if is_regression_fixture(entry):
return {
"review_decision": "keep_as_regression_fixture_only",
"output_action": "do_not_train_as_poison",
"curriculum_level": "L0_regression",
"label_status": "fixture_expected_warn_not_gold_poison",
"reason": (
"This is a runtime decision regression fixture, not a gold poisoned "
"training row."
),
}
dataset = str(entry.get("dataset") or "")
row = entry.get("row") or {}
source = str(row.get("source") or "")
if dataset == "family_curated_v0" or source == "family_curated_v0.json":
output_action = "already_curated_do_not_duplicate_add_policy_variant_or_static_rule"
curriculum_level = "L3_subtle_intent"
reason = (
"The row is already in the curated family set; use it for policy tuning or "
"a variant/twin, not as a duplicate training row."
)
else:
output_action = "add_clean_twin_or_static_rule_before_hard_block_training"
curriculum_level = "L4_real_carrier_adapted"
reason = (
"The row is poisoned and near the block threshold; add a clean twin or "
"strong static corroboration before using it to change hard-block behavior."
)
return {
"review_decision": "promote_hard_positive_signal_for_block_policy",
"output_action": output_action,
"curriculum_level": curriculum_level,
"label_status": "poisoned_near_block_positive",
"reason": reason,
}
def fallback_decision() -> dict[str, str]:
return {
"review_decision": "manual_review_required",
"output_action": "do_not_train_until_reviewed",
"curriculum_level": "L5_external_holdout",
"label_status": "unclassified_calibration_error",
"reason": "No deterministic curriculum-routing rule matched this queue entry.",
}
def classify_entry(entry: dict[str, Any]) -> dict[str, str]:
row = entry.get("row") or {}
label = str(row.get("label") or "")
error_type = str(entry.get("error_type") or "")
if label == "clean" and error_type in {"false_warn_or_block", "false_blocks"}:
return clean_decision(entry)
if label == "poisoned" and error_type == "missed_poison":
return missed_poison_decision(entry)
if label == "poisoned" and error_type == "warned_but_not_blocked_poison":
return near_block_poison_decision(entry)
return fallback_decision()
def build_decisions(
*,
queue: dict[str, Any],
source_queue: str,
source_calibration: str | None,
decision_policy: str | None,
) -> dict[str, Any]:
entries = queue.get("entries") or []
if not isinstance(entries, list):
raise SystemExit("Queue JSON must contain an entries array.")
decisions: list[dict[str, Any]] = []
for entry in entries:
route = classify_entry(entry)
decisions.append(
{
"queue_id": entry.get("queue_id"),
"row_id": row_id(entry),
"dataset": entry.get("dataset"),
"error_type": entry.get("error_type"),
"priority": entry.get("priority"),
"recommended_action": entry.get("recommended_action"),
**route,
"scores": row_scores(entry),
}
)
by_review_decision = Counter(row["review_decision"] for row in decisions)
by_output_action = Counter(row["output_action"] for row in decisions)
by_label_status = Counter(row["label_status"] for row in decisions)
artifact = queue.get("artifact") or {}
first_entry = entries[0] if entries else {}
return {
"created_at": utc_now(),
"source_queue": source_queue,
"source_calibration": source_calibration or queue.get("source_calibration"),
"artifact_id": artifact.get("artifact_id") or first_entry.get("artifact_id"),
"decision_policy": decision_policy,
"warn_threshold": first_entry.get("warn_threshold"),
"block_threshold": first_entry.get("block_threshold"),
"purpose": (
"Reviewed curriculum decisions for calibration errors. This file records "
"what to promote, quarantine, or convert into policy regression tests; it "
"is not training data."
),
"summary": {
"total_entries": len(decisions),
"by_review_decision": dict(sorted(by_review_decision.items())),
"by_output_action": dict(sorted(by_output_action.items())),
"by_label_status": dict(sorted(by_label_status.items())),
},
"rules": [
"Do not train directly on the review queue or this decisions file.",
"Rows whose unsafe action is missing from scanned text stay quarantined until hidden carrier context is reconstructed.",
"Clean static false positives become policy/regression candidates before gold training rows.",
"Near-block poisoned rows need a reviewed benign twin or stronger static corroboration before changing hard-block behavior.",
"Runtime regression fixtures are not gold poisoned training data.",
],
"decisions": decisions,
}
def main() -> None:
args = parse_args()
queue = json.loads(args.queue.read_text())
decisions = build_decisions(
queue=queue,
source_queue=str(args.queue),
source_calibration=args.source_calibration,
decision_policy=args.decision_policy,
)
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(json.dumps(decisions, indent=2, ensure_ascii=False) + "\n")
if args.pretty:
print(json.dumps(decisions["summary"], indent=2, ensure_ascii=False))
print(f"Wrote {args.output}")
if __name__ == "__main__":
main()