-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
256 lines (202 loc) · 7.6 KB
/
Copy pathutils.py
File metadata and controls
256 lines (202 loc) · 7.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
from typing import Dict, List, Any, Tuple, Optional
from collections import defaultdict
from omegaconf import OmegaConf
def get_config(config_path: str):
conf = OmegaConf.load(config_path)
OmegaConf.resolve(conf)
return conf
def _to_goal_type_list(x: Any) -> List[str]:
if x is None:
return []
if isinstance(x, str):
x = x.strip()
return [x] if x else []
if isinstance(x, list):
out = []
for item in x:
if isinstance(item, str):
item = item.strip()
if item:
out.append(item)
return out
return []
def _dedup_keep_order(items: List[str]) -> List[str]:
seen = set()
out = []
for x in items:
if x not in seen:
seen.add(x)
out.append(x)
return out
def collect_raw_goal_types(
data: List[Dict[str, Any]],
registry: "PatternRegistry",
pattern_set: Dict[str, List[str]],
) -> List[str]:
raw_goal_types = []
# from data
for example in data:
raw_goal_types.extend(_to_goal_type_list(example.get("goal_type", None)))
# from pattern_set keys
for gt in pattern_set.keys():
raw_goal_types.extend(_to_goal_type_list(gt))
# from registry schemas
for pid in registry.ids():
schema = registry.get(pid).schema
raw_goal_types.extend(_to_goal_type_list(schema.get("goal_type", None)))
return _dedup_keep_order(raw_goal_types)
def apply_goal_type_mapping_to_registry(
registry: "PatternRegistry",
goal_type_mapping: Dict[str, str],
) -> "PatternRegistry":
for pid in registry.ids():
pattern = registry.get(pid)
raw_goal_types = _to_goal_type_list(pattern.schema.get("goal_type", None))
if not raw_goal_types:
continue
canonical_goal_types = []
for gt in raw_goal_types:
canonical_gt = goal_type_mapping.get(gt, gt)
if canonical_gt:
canonical_goal_types.append(canonical_gt)
pattern.schema["goal_type"] = _dedup_keep_order(canonical_goal_types)
return registry
def rebuild_pattern_set_from_registry(
registry: "PatternRegistry",
) -> Dict[str, List[str]]:
pattern_set = defaultdict(list)
for pid in registry.ids():
schema = registry.get(pid).schema
goal_types = _to_goal_type_list(schema.get("goal_type", None))
for gt in goal_types:
if pid not in pattern_set[gt]:
pattern_set[gt].append(pid)
return dict(pattern_set)
def apply_goal_type_mapping_to_data(
data: List[Dict[str, Any]],
goal_type_mapping: Dict[str, str],
) -> List[Dict[str, Any]]:
for example in data:
if "goal_type" not in example:
continue
raw_goal_types = _to_goal_type_list(example.get("goal_type", None))
if not raw_goal_types:
continue
canonical_goal_types = []
for gt in raw_goal_types:
canonical_gt = goal_type_mapping.get(gt, gt)
if canonical_gt:
canonical_goal_types.append(canonical_gt)
canonical_goal_types = _dedup_keep_order(canonical_goal_types)
if len(canonical_goal_types) == 1:
example["goal_type"] = canonical_goal_types[0]
else:
example["goal_type"] = canonical_goal_types
return data
def parse_score_result(
results: Dict[str, Any],
attack_weight: float = 0.8,
template_weight: float = 0.2,
) -> Tuple[float, str, Dict[str, Any]]:
if not isinstance(results, dict):
raise ValueError(f"Expected dict from scorer, got: {type(results)}")
attack_score = float(results.get("attack_score", 0.0))
template_score = float(results.get("template_score", 0.0))
analysis = results.get("analysis", "")
# reward = attack success score directly
# Range: [0, 1]
reward = attack_score
if reward >= 0.7:
label = "high_risk"
elif reward >= 0.4:
label = "medium_risk"
else:
label = "low_risk"
evidence = {
"attack_score": attack_score,
"template_score": template_score,
"analysis": analysis,
}
return reward, label, evidence
def get_goal_type(
goal: str,
retriever: "Retriever",
candidate_goal_type_set: List[str],
verbose: bool = False,
) -> List[Dict[str, Any]]:
if len(candidate_goal_type_set) == 0:
return None
result = retriever.extract_goal_type(
goal=goal,
goal_type_set=candidate_goal_type_set,
)
extracted_goal_type = result.get("goal_type", candidate_goal_type_set[0])
return extracted_goal_type
def preprocess_stage_a_artifacts(
data: List[Dict[str, Any]],
registry: "PatternRegistry",
pattern_set: Dict[str, List[str]],
retriever: "Retriever",
verbose: bool = False,
) -> Tuple["PatternRegistry", Dict[str, List[str]], List[Dict[str, Any]], Dict[str, str]]:
"""
Normalize overlapping / inconsistent goal types across:
- dataset examples
- pattern_set keys
- registry schema["goal_type"]
Then optionally fill missing example goal_type using GoalTypeExtractor.
Returns:
registry, pattern_set, data, goal_type_mapping
"""
raw_goal_type_set = collect_raw_goal_types(
data=data,
registry=registry,
pattern_set=pattern_set,
)
if verbose:
print("\n===== Raw Goal Types Before Normalization =====")
print(f"num_raw_goal_types: {len(raw_goal_type_set)}")
print(f"raw_goal_types: {raw_goal_type_set}")
if len(raw_goal_type_set) == 0:
goal_type_mapping = {}
else:
norm_result = retriever.normalize_goal_types(raw_goal_type_set)
goal_type_mapping = norm_result.get("mapping", {})
# defensive completion
for gt in raw_goal_type_set:
if gt not in goal_type_mapping:
goal_type_mapping[gt] = gt
if verbose:
print("\n===== Goal Type Normalization =====")
print("mapping:")
for k in sorted(goal_type_mapping.keys()):
print(f" {k} -> {goal_type_mapping[k]}")
analysis = norm_result.get("analysis", "")
if analysis:
print("\nanalysis:")
print(analysis)
# apply mapping to registry
registry = apply_goal_type_mapping_to_registry(
registry=registry,
goal_type_mapping=goal_type_mapping,
)
# rebuild pattern_set from normalized registry instead of trusting old keys
pattern_set = rebuild_pattern_set_from_registry(registry)
# apply mapping to data
data = apply_goal_type_mapping_to_data(
data=data,
goal_type_mapping=goal_type_mapping,
)
if verbose:
print("\n===== Loaded Registry (Normalized) =====")
print(f"num_patterns_in_registry: {len(registry.patterns)}")
sample_registry_ids = registry.ids()[:5]
print(f"sample_registry_ids: {sample_registry_ids}")
print("\n===== Loaded Pattern Set (Normalized) =====")
print(f"num_goal_types_in_pattern_set: {len(pattern_set)}")
print(f"goal_types: {list(pattern_set.keys())[:10]}")
for goal_type, pids in list(pattern_set.items())[:10]:
print(f"\n[pattern_set] goal_type={goal_type}")
print(f"num_patterns: {len(pids)}")
print(f"pattern_ids: {pids[:10]}")
return registry, pattern_set, data, goal_type_mapping