-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_examples.py
More file actions
252 lines (194 loc) · 7.47 KB
/
Copy pathvalidate_examples.py
File metadata and controls
252 lines (194 loc) · 7.47 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
#!/usr/bin/env python3
"""
Validate Structural AI Tuning Layer example YAML files against JSON Schemas.
Required packages:
pip install pyyaml jsonschema
Usage:
python scripts/validate_examples.py
"""
import json
import sys
from pathlib import Path
from typing import Any
try:
import yaml
except ImportError as exc:
print("Missing dependency: PyYAML")
print("Install with: pip install pyyaml")
raise SystemExit(1) from exc
try:
from jsonschema import Draft202012Validator, FormatChecker
from jsonschema.exceptions import SchemaError
except ImportError as exc:
print("Missing dependency: jsonschema")
print("Install with: pip install jsonschema")
raise SystemExit(1) from exc
REPO_ROOT = Path(__file__).resolve().parents[1]
VALIDATION_TARGETS = [
{
"name": "Structural Tuning Record",
"schema": "schemas/structural-tuning-record.schema.json",
"example": "examples/structural-tuning-record.example.yaml",
}
]
def load_json(path: Path) -> Any:
"""Load a JSON file."""
try:
with path.open("r", encoding="utf-8") as file:
return json.load(file)
except FileNotFoundError:
raise RuntimeError(f"JSON file not found: {path}")
except json.JSONDecodeError as exc:
raise RuntimeError(f"Invalid JSON in {path}: {exc}") from exc
def load_yaml(path: Path) -> Any:
"""Load a YAML file."""
try:
with path.open("r", encoding="utf-8") as file:
return yaml.safe_load(file)
except FileNotFoundError:
raise RuntimeError(f"YAML file not found: {path}")
except yaml.YAMLError as exc:
raise RuntimeError(f"Invalid YAML in {path}: {exc}") from exc
def format_error_path(error: Any) -> str:
"""Format a jsonschema validation error path."""
if not error.path:
return "<root>"
parts = []
for item in error.path:
if isinstance(item, int):
parts.append(f"[{item}]")
else:
if parts:
parts.append(f".{item}")
else:
parts.append(str(item))
return "".join(parts)
def validate_schema(schema: Any, schema_path: Path) -> None:
"""Validate that the schema itself is valid Draft 2020-12 JSON Schema."""
try:
Draft202012Validator.check_schema(schema)
except SchemaError as exc:
raise RuntimeError(f"Invalid JSON Schema in {schema_path}: {exc.message}") from exc
def validate_example_against_schema(
*,
target_name: str,
schema_path: Path,
example_path: Path,
) -> bool:
"""Validate one YAML example against one JSON Schema."""
print(f"Validating target: {target_name}")
print(f" Schema : {schema_path.relative_to(REPO_ROOT)}")
print(f" Example: {example_path.relative_to(REPO_ROOT)}")
schema = load_json(schema_path)
example = load_yaml(example_path)
validate_schema(schema, schema_path)
validator = Draft202012Validator(schema, format_checker=FormatChecker())
errors = sorted(validator.iter_errors(example), key=lambda error: list(error.path))
if errors:
print(" Result : failed\n")
for error in errors:
print(f" Error at {format_error_path(error)}:")
print(f" {error.message}")
print()
return False
print(" Result : passed\n")
return True
def run_structural_consistency_checks(example: Any) -> list[str]:
"""
Run minimal governance-aware consistency checks.
These checks go beyond JSON Schema validation.
"""
errors: list[str] = []
action = example.get("action", {})
governance_state = example.get("governance_state", {})
recovery_gate = example.get("recovery_gate")
human_review = example.get("human_review", {})
structural_result = example.get("structural_tuning_result", {})
action_type = action.get("action_type")
if action_type == "recovery" and recovery_gate is None:
errors.append("Recovery actions must include a recovery_gate section.")
if recovery_gate is not None:
recovery_gate_status = recovery_gate.get("recovery_gate_status")
recovery_allowed = recovery_gate.get("recovery_allowed")
if recovery_gate_status == "passed" and recovery_allowed is not True:
errors.append(
"recovery_gate_status is passed, but recovery_allowed is not true."
)
if recovery_gate_status in {"blocked", "failed"} and recovery_allowed is True:
errors.append(
"recovery_gate_status is blocked or failed, but recovery_allowed is true."
)
human_review_required = human_review.get("human_review_required")
human_review_status = human_review.get("human_review_status")
if human_review_required is True and human_review_status == "not_required":
errors.append(
"human_review_required is true, but human_review_status is not_required."
)
if human_review_required is False and human_review_status != "not_required":
errors.append(
"human_review_required is false, but human_review_status is not not_required."
)
governance_human_review_status = governance_state.get("human_review_status")
if (
governance_human_review_status is not None
and human_review_status is not None
and governance_human_review_status != human_review_status
):
errors.append(
"governance_state.human_review_status does not match "
"human_review.human_review_status."
)
structural_action_allowed = structural_result.get("action_allowed")
human_review_action_allowed = human_review.get("action_allowed")
if (
structural_action_allowed is not None
and human_review_action_allowed is not None
and structural_action_allowed is True
and human_review_action_allowed is False
):
errors.append(
"structural_tuning_result.action_allowed is true, but "
"human_review.action_allowed is false."
)
return errors
def run_additional_checks(target_name: str, example_path: Path) -> bool:
"""Run target-specific structural consistency checks."""
example = load_yaml(example_path)
if target_name != "Structural Tuning Record":
return True
print(f"Running structural consistency checks: {target_name}")
errors = run_structural_consistency_checks(example)
if errors:
print(" Result : failed\n")
for error in errors:
print(f" Error: {error}")
print()
return False
print(" Result : passed\n")
return True
def main() -> int:
all_passed = True
for target in VALIDATION_TARGETS:
target_name = target["name"]
schema_path = REPO_ROOT / target["schema"]
example_path = REPO_ROOT / target["example"]
try:
schema_passed = validate_example_against_schema(
target_name=target_name,
schema_path=schema_path,
example_path=example_path,
)
structural_passed = run_additional_checks(target_name, example_path)
if not schema_passed or not structural_passed:
all_passed = False
except RuntimeError as exc:
all_passed = False
print(f"Validation failed for target: {target_name}")
print(f"Error: {exc}\n")
if all_passed:
print("All validations passed.")
return 0
print("One or more validations failed.")
return 1
if __name__ == "__main__":
sys.exit(main())