datapi-selfheal 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- datapi_selfheal-0.3.0.dist-info/METADATA +313 -0
- datapi_selfheal-0.3.0.dist-info/RECORD +35 -0
- datapi_selfheal-0.3.0.dist-info/WHEEL +5 -0
- datapi_selfheal-0.3.0.dist-info/entry_points.txt +3 -0
- datapi_selfheal-0.3.0.dist-info/licenses/LICENSE +21 -0
- datapi_selfheal-0.3.0.dist-info/top_level.txt +3 -0
- selfheal_api/__init__.py +106 -0
- selfheal_api/_sandbox_exec.py +124 -0
- selfheal_api/autoheal.py +658 -0
- selfheal_api/billing.py +738 -0
- selfheal_api/cli.py +958 -0
- selfheal_api/dashboard.py +1094 -0
- selfheal_api/eval_fixtures.py +39 -0
- selfheal_api/github_app.py +1050 -0
- selfheal_api/github_jwt.py +145 -0
- selfheal_api/ledger.py +305 -0
- selfheal_api/metrics.py +259 -0
- selfheal_api/middleware.py +297 -0
- selfheal_api/modelgen.py +333 -0
- selfheal_api/notifier.py +426 -0
- selfheal_api/remediation.py +277 -0
- selfheal_api/replay.py +126 -0
- selfheal_api/sandbox.py +157 -0
- selfheal_api/server.py +1480 -0
- selfheal_api/server_hosted.py +1656 -0
- selfheal_api/specwatch.py +203 -0
- selfheal_api/worker.py +212 -0
- sentinel/__init__.py +33 -0
- sentinel/detector.py +626 -0
- sentinel/guard.py +345 -0
- sentinel/llm_fallback.py +256 -0
- synthesizer/__init__.py +31 -0
- synthesizer/openapi_emitter.py +181 -0
- synthesizer/patcher.py +944 -0
- synthesizer/zod_emitter.py +336 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import copy
|
|
4
|
+
import json
|
|
5
|
+
import re
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from pydantic import BaseModel, ConfigDict
|
|
9
|
+
|
|
10
|
+
from sentinel.detector import DriftKind
|
|
11
|
+
from synthesizer.patcher import PatchPlan
|
|
12
|
+
|
|
13
|
+
_YAML_BOOL_OR_NULL = frozenset(
|
|
14
|
+
{"true", "false", "null", "~", "yes", "no", "on", "off", "y", "n"}
|
|
15
|
+
)
|
|
16
|
+
_YAML_NUMBER_RE = re.compile(r"^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$")
|
|
17
|
+
_YAML_SAFE_KEY_RE = re.compile(r"^[A-Za-z0-9_.$][A-Za-z0-9_.$-]*$")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class OpenAPIPatchSpec(BaseModel):
|
|
21
|
+
"""OpenAPI 3.1.0 schema patch specification generated from a PatchPlan."""
|
|
22
|
+
|
|
23
|
+
model_config = ConfigDict(frozen=True)
|
|
24
|
+
|
|
25
|
+
model_name: str
|
|
26
|
+
schema_version: str = "3.1.0"
|
|
27
|
+
json_schema: dict[str, Any]
|
|
28
|
+
aliases: dict[str, list[str]]
|
|
29
|
+
coercions: dict[str, str]
|
|
30
|
+
yaml_snippet: str
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _needs_yaml_quoting(value: str) -> bool:
|
|
34
|
+
if value == "" or value != value.strip():
|
|
35
|
+
return True
|
|
36
|
+
if value.lower() in _YAML_BOOL_OR_NULL:
|
|
37
|
+
return True
|
|
38
|
+
if _YAML_NUMBER_RE.match(value):
|
|
39
|
+
return True
|
|
40
|
+
special_chars = set(":#{}[],&*?|-<>=!%@`\"'\\\n\r\t")
|
|
41
|
+
return any(ch in special_chars for ch in value)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _format_yaml_scalar(value: Any) -> str:
|
|
45
|
+
if value is None:
|
|
46
|
+
return "null"
|
|
47
|
+
if isinstance(value, bool):
|
|
48
|
+
return "true" if value else "false"
|
|
49
|
+
if isinstance(value, (int, float)):
|
|
50
|
+
return str(value)
|
|
51
|
+
if isinstance(value, str):
|
|
52
|
+
if _needs_yaml_quoting(value):
|
|
53
|
+
return json.dumps(value, ensure_ascii=False)
|
|
54
|
+
return value
|
|
55
|
+
return json.dumps(str(value), ensure_ascii=False)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _format_yaml_key(key: str) -> str:
|
|
59
|
+
if _YAML_SAFE_KEY_RE.match(key) and key.lower() not in _YAML_BOOL_OR_NULL:
|
|
60
|
+
return key
|
|
61
|
+
return json.dumps(key, ensure_ascii=False)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _render_yaml_lines(value: Any, indent: int = 0) -> list[str]:
|
|
65
|
+
pad = " " * indent
|
|
66
|
+
if isinstance(value, dict):
|
|
67
|
+
if not value:
|
|
68
|
+
return [f"{pad}{{}}"]
|
|
69
|
+
lines: list[str] = []
|
|
70
|
+
for k, v in value.items():
|
|
71
|
+
key_str = _format_yaml_key(str(k))
|
|
72
|
+
if isinstance(v, dict) and v:
|
|
73
|
+
lines.append(f"{pad}{key_str}:")
|
|
74
|
+
lines.extend(_render_yaml_lines(v, indent + 2))
|
|
75
|
+
elif isinstance(v, list) and v:
|
|
76
|
+
lines.append(f"{pad}{key_str}:")
|
|
77
|
+
lines.extend(_render_yaml_lines(v, indent + 2))
|
|
78
|
+
elif isinstance(v, dict):
|
|
79
|
+
lines.append(f"{pad}{key_str}: {{}}")
|
|
80
|
+
elif isinstance(v, list):
|
|
81
|
+
lines.append(f"{pad}{key_str}: []")
|
|
82
|
+
else:
|
|
83
|
+
lines.append(f"{pad}{key_str}: {_format_yaml_scalar(v)}")
|
|
84
|
+
return lines
|
|
85
|
+
|
|
86
|
+
if isinstance(value, list):
|
|
87
|
+
if not value:
|
|
88
|
+
return [f"{pad}[]"]
|
|
89
|
+
lines = []
|
|
90
|
+
for item in value:
|
|
91
|
+
if isinstance(item, dict) and item:
|
|
92
|
+
item_lines = _render_yaml_lines(item, indent + 2)
|
|
93
|
+
lines.append(f"{pad}- {item_lines[0].lstrip()}")
|
|
94
|
+
lines.extend(item_lines[1:])
|
|
95
|
+
elif isinstance(item, list) and item:
|
|
96
|
+
lines.append(f"{pad}-")
|
|
97
|
+
lines.extend(_render_yaml_lines(item, indent + 2))
|
|
98
|
+
elif isinstance(item, dict):
|
|
99
|
+
lines.append(f"{pad}- {{}}")
|
|
100
|
+
elif isinstance(item, list):
|
|
101
|
+
lines.append(f"{pad}- []")
|
|
102
|
+
else:
|
|
103
|
+
lines.append(f"{pad}- {_format_yaml_scalar(item)}")
|
|
104
|
+
return lines
|
|
105
|
+
|
|
106
|
+
return [f"{pad}{_format_yaml_scalar(value)}"]
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _should_record_coercion(
|
|
110
|
+
drift_kind: DriftKind,
|
|
111
|
+
expected_type: str,
|
|
112
|
+
observed_kind: str,
|
|
113
|
+
has_source_path: bool,
|
|
114
|
+
) -> bool:
|
|
115
|
+
if drift_kind != DriftKind.MISSING_FIELD:
|
|
116
|
+
return True
|
|
117
|
+
if not has_source_path or not observed_kind or observed_kind == "<missing>":
|
|
118
|
+
return False
|
|
119
|
+
return observed_kind.lower() != f"<{expected_type.lower()}>"
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def emit_openapi_schema_patch(
|
|
123
|
+
model_cls: type[BaseModel], plan: PatchPlan
|
|
124
|
+
) -> OpenAPIPatchSpec:
|
|
125
|
+
"""Emit an OpenAPI 3.1.0 schema patch spec from a Pydantic model and PatchPlan."""
|
|
126
|
+
json_schema: dict[str, Any] = copy.deepcopy(model_cls.model_json_schema())
|
|
127
|
+
aliases: dict[str, list[str]] = {}
|
|
128
|
+
coercions: dict[str, str] = {}
|
|
129
|
+
|
|
130
|
+
for action in plan.actions:
|
|
131
|
+
if action.source_path is not None and len(action.source_path) > 0:
|
|
132
|
+
dot_path = ".".join(str(seg) for seg in action.source_path)
|
|
133
|
+
existing = aliases.setdefault(action.target_field, [])
|
|
134
|
+
if dot_path not in existing:
|
|
135
|
+
existing.append(dot_path)
|
|
136
|
+
|
|
137
|
+
if _should_record_coercion(
|
|
138
|
+
drift_kind=action.drift_kind,
|
|
139
|
+
expected_type=action.expected_type,
|
|
140
|
+
observed_kind=action.observed_kind,
|
|
141
|
+
has_source_path=action.source_path is not None,
|
|
142
|
+
):
|
|
143
|
+
coercions[action.target_field] = action.expected_type
|
|
144
|
+
|
|
145
|
+
json_schema["x-selfheal-aliases"] = copy.deepcopy(aliases)
|
|
146
|
+
json_schema["x-selfheal-coercions"] = dict(coercions)
|
|
147
|
+
|
|
148
|
+
model_name = model_cls.__name__
|
|
149
|
+
yaml_lines = _render_yaml_lines({model_name: json_schema})
|
|
150
|
+
yaml_snippet = "\n".join(yaml_lines) + "\n"
|
|
151
|
+
|
|
152
|
+
return OpenAPIPatchSpec(
|
|
153
|
+
model_name=model_name,
|
|
154
|
+
schema_version="3.1.0",
|
|
155
|
+
json_schema=json_schema,
|
|
156
|
+
aliases=aliases,
|
|
157
|
+
coercions=coercions,
|
|
158
|
+
yaml_snippet=yaml_snippet,
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def emit_openapi_document(
|
|
163
|
+
specs: list[OpenAPIPatchSpec],
|
|
164
|
+
*,
|
|
165
|
+
title: str = "Datapi SelfHeal Schemas",
|
|
166
|
+
version: str = "0.2.0",
|
|
167
|
+
) -> dict[str, Any]:
|
|
168
|
+
"""Assemble a valid OpenAPI 3.1.0 document from a list of OpenAPIPatchSpec."""
|
|
169
|
+
schemas: dict[str, Any] = {
|
|
170
|
+
spec.model_name: copy.deepcopy(spec.json_schema) for spec in specs
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
"openapi": "3.1.0",
|
|
174
|
+
"info": {
|
|
175
|
+
"title": title,
|
|
176
|
+
"version": version,
|
|
177
|
+
},
|
|
178
|
+
"components": {
|
|
179
|
+
"schemas": schemas,
|
|
180
|
+
},
|
|
181
|
+
}
|