-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine_scope.py
More file actions
343 lines (312 loc) · 13.3 KB
/
Copy pathengine_scope.py
File metadata and controls
343 lines (312 loc) · 13.3 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""Normalized Engine-Scope row contract and artifact loaders."""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any, Dict, Iterable, List, Sequence
from evidence_contract import evidence_event_from_engine_scope_row
ENGINE_SCOPE_SCHEMA_VERSION = 1
_CONTEXT_KEYS = (
"task",
"seed",
"query_offset",
"split",
"loop",
"window",
"global_window",
"profile",
"source_family",
)
def _iter_artifact_paths(paths: Sequence[str | Path]) -> List[Path]:
artifact_paths: List[Path] = []
for raw_path in paths:
path = Path(raw_path)
if path.is_dir():
artifact_paths.extend(sorted(path.glob("*.json")))
elif path.is_file():
artifact_paths.append(path)
else:
raise FileNotFoundError(f"artifact path not found: {path}")
if not artifact_paths:
raise ValueError("no artifact files found")
deduped: List[Path] = []
seen = set()
for path in artifact_paths:
resolved = path.resolve()
if resolved not in seen:
seen.add(resolved)
deduped.append(path)
return deduped
def _merged_context(row: Dict[str, Any], context: Dict[str, Any] | None) -> Dict[str, Any]:
merged = dict(context or {})
for key in _CONTEXT_KEYS:
if key in row and row[key] is not None:
merged[key] = row[key]
return merged
def _normalize_engine_scope_row(
row: Dict[str, Any],
*,
artifact_path: Path | None = None,
context: Dict[str, Any] | None = None,
) -> Dict[str, Any]:
merged = _merged_context(row, context)
item = dict(row)
item.setdefault("schema_version", ENGINE_SCOPE_SCHEMA_VERSION)
item.setdefault("source_family", merged.get("source_family"))
if artifact_path is not None and not item.get("_artifact_path"):
item["_artifact_path"] = str(artifact_path)
else:
item.setdefault("_artifact_path", str(artifact_path) if artifact_path is not None else None)
for key in _CONTEXT_KEYS:
item.setdefault(key, merged.get(key))
return item
def _query_attribution_to_engine_scope_row(
row: Dict[str, Any],
*,
source_family: str,
artifact_path: Path | None = None,
context: Dict[str, Any] | None = None,
) -> Dict[str, Any]:
merged = _merged_context(row, context)
return {
"schema_version": ENGINE_SCOPE_SCHEMA_VERSION,
"row_type": "query_profile",
"source_family": source_family,
"_artifact_path": str(artifact_path) if artifact_path is not None else None,
"task": merged.get("task"),
"seed": merged.get("seed"),
"query_offset": merged.get("query_offset"),
"loop": merged.get("loop"),
"window": merged.get("window"),
"global_window": merged.get("global_window"),
"split": merged.get("split"),
"profile": merged.get("profile"),
"query_id": row.get("query_id"),
"query_text": row.get("query_text"),
"query_token_count": row.get("query_token_count"),
"query_char_count": row.get("query_char_count"),
"query_stopword_ratio": row.get("query_stopword_ratio"),
"query_numeric_token_count": row.get("query_numeric_token_count"),
"query_negation_count": row.get("query_negation_count"),
"query_claim_cue_count": row.get("query_claim_cue_count"),
"delta_ndcg_at_10": row.get("delta_ndcg_at_10"),
"delta_mrr": row.get("delta_mrr"),
"delta_recall_at_10": row.get("delta_recall_at_10"),
"rank_delta": row.get("rank_delta"),
"baseline_rank": row.get("baseline_rank"),
"candidate_rank": row.get("candidate_rank"),
"top10_overlap_with_baseline": row.get("top10_overlap_with_baseline"),
"top_doc_changed": row.get("top_doc_changed"),
"action": row.get("action"),
"global_variance": row.get("global_variance"),
"jaccard": row.get("jaccard"),
"mask_density": row.get("mask_density"),
"reformulation_variant_count": row.get("reformulation_variant_count"),
"reformulation_changed": row.get("reformulation_changed"),
"fault_class": row.get("fault_class"),
"baseline_score_margin": row.get("baseline_score_margin"),
"baseline_top_score": row.get("baseline_top_score"),
"query_norm": row.get("query_norm"),
"masked_rank": row.get("masked_rank"),
"gate_applied": row.get("gate_applied"),
"selected_delta_ndcg_at_10": row.get("selected_delta_ndcg_at_10"),
"route": row.get("route"),
"retrieval_intervention": row.get("retrieval_intervention"),
"gate_decision": row.get("gate_decision"),
"evaluator_outcome": row.get("evaluator_outcome"),
"safety_outcome": row.get("safety_outcome"),
"promotion_blocker": row.get("promotion_blocker"),
"decision": row.get("decision"),
}
def _mask_example_to_engine_scope_row(
row: Dict[str, Any],
*,
source_family: str,
artifact_path: Path | None = None,
context: Dict[str, Any] | None = None,
) -> Dict[str, Any]:
merged = _merged_context(row, context)
return {
"schema_version": ENGINE_SCOPE_SCHEMA_VERSION,
"row_type": "mask_probe",
"source_family": source_family,
"_artifact_path": str(artifact_path) if artifact_path is not None else None,
"task": merged.get("task"),
"seed": merged.get("seed"),
"query_offset": merged.get("query_offset"),
"loop": merged.get("loop"),
"window": merged.get("window"),
"global_window": merged.get("global_window"),
"split": merged.get("split"),
"profile": merged.get("profile"),
"query_id": row.get("query_id"),
"query_text": row.get("query_text"),
"query_token_count": row.get("query_token_count"),
"query_char_count": row.get("query_char_count"),
"query_stopword_ratio": row.get("query_stopword_ratio"),
"query_numeric_token_count": row.get("query_numeric_token_count"),
"query_negation_count": row.get("query_negation_count"),
"query_claim_cue_count": row.get("query_claim_cue_count"),
"delta_ndcg_at_10": row.get("delta_ndcg_at_10"),
"delta_mrr": row.get("delta_mrr"),
"delta_recall_at_10": row.get("delta_recall_at_10"),
"rank_delta": row.get("rank_delta"),
"baseline_rank": row.get("baseline_rank"),
"candidate_rank": row.get("candidate_rank"),
"top10_overlap_with_baseline": row.get("top10_overlap_with_baseline"),
"top_doc_changed": row.get("top_doc_changed"),
"action": row.get("action"),
"global_variance": row.get("global_variance"),
"jaccard": row.get("jaccard"),
"mask_density": row.get("mask_density"),
"reformulation_variant_count": row.get("reformulation_variant_count"),
"reformulation_changed": row.get("reformulation_changed"),
"fault_class": row.get("fault_class"),
"baseline_score_margin": row.get("baseline_score_margin"),
"baseline_top_score": row.get("baseline_top_score"),
"query_norm": row.get("query_norm"),
"masked_rank": row.get("masked_rank"),
"gate_applied": row.get("gate_applied"),
"selected_delta_ndcg_at_10": row.get("selected_delta_ndcg_at_10"),
"route": row.get("route"),
"retrieval_intervention": row.get("retrieval_intervention"),
"gate_decision": row.get("gate_decision"),
"evaluator_outcome": row.get("evaluator_outcome"),
"safety_outcome": row.get("safety_outcome"),
"promotion_blocker": row.get("promotion_blocker"),
"decision": row.get("decision"),
}
def build_engine_scope_rows(
*,
query_attribution_rows: Iterable[Dict[str, Any]] | None = None,
mask_example_rows: Iterable[Dict[str, Any]] | None = None,
source_family: str,
artifact_path: Path | None = None,
context: Dict[str, Any] | None = None,
) -> List[Dict[str, Any]]:
rows: List[Dict[str, Any]] = []
for row in query_attribution_rows or ():
rows.append(
_query_attribution_to_engine_scope_row(
dict(row),
source_family=source_family,
artifact_path=artifact_path,
context=context,
)
)
for row in mask_example_rows or ():
rows.append(
_mask_example_to_engine_scope_row(
dict(row),
source_family=source_family,
artifact_path=artifact_path,
context=context,
)
)
return rows
def _extract_engine_scope_rows(
payload: Any,
*,
artifact_path: Path,
context: Dict[str, Any] | None = None,
) -> List[Dict[str, Any]]:
"""Extract legacy-derived Engine-Scope rows from artifacts without direct rows."""
active_context = dict(context or {})
rows: List[Dict[str, Any]] = []
if isinstance(payload, dict):
for key in _CONTEXT_KEYS:
if key in payload and payload[key] is not None:
active_context[key] = payload[key]
query_rows = payload.get("query_attribution_rows")
if isinstance(query_rows, list):
rows.extend(
build_engine_scope_rows(
query_attribution_rows=[row for row in query_rows if isinstance(row, dict)],
source_family=str(active_context.get("source_family") or "legacy_query_attribution"),
artifact_path=artifact_path,
context=active_context,
)
)
mask_rows = payload.get("mask_example_rows")
if isinstance(mask_rows, list):
rows.extend(
build_engine_scope_rows(
mask_example_rows=[row for row in mask_rows if isinstance(row, dict)],
source_family=str(active_context.get("source_family") or "legacy_mask_probe"),
artifact_path=artifact_path,
context=active_context,
)
)
for key, value in payload.items():
if key in {"engine_scope_rows", "query_attribution_rows", "mask_example_rows"}:
continue
rows.extend(_extract_engine_scope_rows(value, artifact_path=artifact_path, context=active_context))
elif isinstance(payload, list):
for item in payload:
rows.extend(_extract_engine_scope_rows(item, artifact_path=artifact_path, context=active_context))
return rows
def _extract_direct_engine_scope_rows(
payload: Any,
*,
artifact_path: Path,
context: Dict[str, Any] | None = None,
) -> List[Dict[str, Any]]:
active_context = dict(context or {})
rows: List[Dict[str, Any]] = []
if isinstance(payload, dict):
for key in _CONTEXT_KEYS:
if key in payload and payload[key] is not None:
active_context[key] = payload[key]
direct_rows = payload.get("engine_scope_rows")
if isinstance(direct_rows, list):
for row in direct_rows:
if isinstance(row, dict):
rows.append(
_normalize_engine_scope_row(
row,
artifact_path=artifact_path,
context=active_context,
)
)
for key, value in payload.items():
if key == "engine_scope_rows":
continue
rows.extend(_extract_direct_engine_scope_rows(value, artifact_path=artifact_path, context=active_context))
elif isinstance(payload, list):
for item in payload:
rows.extend(_extract_direct_engine_scope_rows(item, artifact_path=artifact_path, context=active_context))
return rows
def load_engine_scope_rows(paths: Sequence[str | Path]) -> List[Dict[str, Any]]:
"""Load engine_scope_rows from modern or legacy artifacts."""
combined: List[Dict[str, Any]] = []
for path in _iter_artifact_paths(paths):
payload = json.loads(path.read_text(encoding="utf-8"))
direct_rows = _extract_direct_engine_scope_rows(payload, artifact_path=path)
if direct_rows:
combined.extend(direct_rows)
continue
combined.extend(_extract_engine_scope_rows(payload, artifact_path=path))
if not combined:
raise ValueError("no engine_scope_rows found in the supplied artifacts")
return combined
def summarize_engine_scope_rows(rows: Iterable[Dict[str, Any]]) -> Dict[str, Any]:
rows_list = [dict(row) for row in rows]
by_source_family: Dict[str, int] = {}
by_row_type: Dict[str, int] = {}
tasks = set()
for row in rows_list:
source_family = str(row.get("source_family") or "unknown")
row_type = str(row.get("row_type") or "unknown")
by_source_family[source_family] = by_source_family.get(source_family, 0) + 1
by_row_type[row_type] = by_row_type.get(row_type, 0) + 1
if row.get("task"):
tasks.add(str(row["task"]))
return {
"schema_version": ENGINE_SCOPE_SCHEMA_VERSION,
"row_count": len(rows_list),
"source_families": by_source_family,
"row_types": by_row_type,
"tasks": sorted(tasks),
}
def engine_scope_rows_to_evidence_events(rows: Iterable[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""Convert Engine-Scope rows into the shared evidence-event contract."""
return [evidence_event_from_engine_scope_row(row) for row in rows]