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,336 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections import defaultdict
|
|
4
|
+
|
|
5
|
+
from sentinel.detector import DriftKind
|
|
6
|
+
from synthesizer.patcher import PatchAction, PatchPlan
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _ts_default_for_type(expected_type: str) -> str:
|
|
10
|
+
exp = expected_type.lower()
|
|
11
|
+
if "none" in exp:
|
|
12
|
+
return "null"
|
|
13
|
+
if exp.startswith("list"):
|
|
14
|
+
return "[]"
|
|
15
|
+
if exp.startswith("dict"):
|
|
16
|
+
return "{}"
|
|
17
|
+
if exp == "bool":
|
|
18
|
+
return "false"
|
|
19
|
+
if exp == "int" or exp == "float":
|
|
20
|
+
return "0"
|
|
21
|
+
if exp == "str":
|
|
22
|
+
return "''"
|
|
23
|
+
return "null"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _emit_ts_action_lines(action: PatchAction, action_idx: int = 0) -> list[str]:
|
|
27
|
+
f = action.target_field
|
|
28
|
+
exp = action.expected_type.lower()
|
|
29
|
+
lines: list[str] = []
|
|
30
|
+
v0 = f"_src_{f}_{action_idx}_0"
|
|
31
|
+
v1 = f"_src_{f}_{action_idx}_1"
|
|
32
|
+
|
|
33
|
+
if action.target_parent_path and action.source_path is not None:
|
|
34
|
+
parent_var = f"_parent_{f}_{action_idx}"
|
|
35
|
+
p_attr = str(action.target_parent_path[0])
|
|
36
|
+
lines.append(
|
|
37
|
+
f" if (typeof normalized[{p_attr!r}] !== 'object' "
|
|
38
|
+
f"|| normalized[{p_attr!r}] === null || Array.isArray(normalized[{p_attr!r}])) {{"
|
|
39
|
+
)
|
|
40
|
+
lines.append(f" normalized[{p_attr!r}] = {{}};")
|
|
41
|
+
lines.append(" }")
|
|
42
|
+
lines.append(
|
|
43
|
+
f" const {parent_var} = normalized[{p_attr!r}] as Record<string, unknown>;"
|
|
44
|
+
)
|
|
45
|
+
for sub_p in action.target_parent_path[1:]:
|
|
46
|
+
lines.append(
|
|
47
|
+
f" if (typeof {parent_var}[{sub_p!r}] !== 'object' "
|
|
48
|
+
f"|| {parent_var}[{sub_p!r}] === null) {{ {parent_var}[{sub_p!r}] = {{}}; }}"
|
|
49
|
+
)
|
|
50
|
+
target_obj = parent_var
|
|
51
|
+
for sub_p in action.target_parent_path[1:]:
|
|
52
|
+
target_obj = f"({target_obj}[{sub_p!r}] as Record<string, unknown>)"
|
|
53
|
+
if len(action.source_path) == 1:
|
|
54
|
+
src = str(action.source_path[0])
|
|
55
|
+
lines.append(f" if (!({f!r} in {target_obj}) && ({src!r} in normalized)) {{")
|
|
56
|
+
lines.append(f" {target_obj}[{f!r}] = normalized[{src!r}];")
|
|
57
|
+
lines.append(" }")
|
|
58
|
+
return lines
|
|
59
|
+
|
|
60
|
+
if action.source_path is not None:
|
|
61
|
+
if len(action.source_path) == 1:
|
|
62
|
+
src = str(action.source_path[0])
|
|
63
|
+
lines.append(
|
|
64
|
+
f" if (!({f!r} in normalized) && ({src!r} in normalized)) {{"
|
|
65
|
+
)
|
|
66
|
+
lines.append(f" normalized[{f!r}] = normalized[{src!r}];")
|
|
67
|
+
lines.append(" }")
|
|
68
|
+
elif len(action.source_path) == 2:
|
|
69
|
+
p0, p1 = str(action.source_path[0]), str(action.source_path[1])
|
|
70
|
+
lines.append(
|
|
71
|
+
f" const {v0} = normalized[{p0!r}];"
|
|
72
|
+
)
|
|
73
|
+
lines.append(
|
|
74
|
+
f" if (!({f!r} in normalized) && typeof {v0} === 'object' "
|
|
75
|
+
f"&& {v0} !== null && {p1!r} in {v0}) {{"
|
|
76
|
+
)
|
|
77
|
+
lines.append(
|
|
78
|
+
f" normalized[{f!r}] = ({v0} as Record<string, unknown>)[{p1!r}];"
|
|
79
|
+
)
|
|
80
|
+
lines.append(" }")
|
|
81
|
+
elif len(action.source_path) == 3:
|
|
82
|
+
p0, p1, p2 = (
|
|
83
|
+
str(action.source_path[0]),
|
|
84
|
+
str(action.source_path[1]),
|
|
85
|
+
str(action.source_path[2]),
|
|
86
|
+
)
|
|
87
|
+
lines.append(f" const {v0} = normalized[{p0!r}];")
|
|
88
|
+
lines.append(
|
|
89
|
+
f" if (!({f!r} in normalized) && typeof {v0} === 'object' "
|
|
90
|
+
f"&& {v0} !== null && {p1!r} in {v0}) {{"
|
|
91
|
+
)
|
|
92
|
+
lines.append(
|
|
93
|
+
f" const {v1} = ({v0} as Record<string, unknown>)[{p1!r}];"
|
|
94
|
+
)
|
|
95
|
+
lines.append(
|
|
96
|
+
f" if (typeof {v1} === 'object' && {v1} !== null "
|
|
97
|
+
f"&& {p2!r} in {v1}) {{"
|
|
98
|
+
)
|
|
99
|
+
lines.append(
|
|
100
|
+
f" normalized[{f!r}] = ({v1} as Record<string, unknown>)[{p2!r}];"
|
|
101
|
+
)
|
|
102
|
+
lines.append(" }")
|
|
103
|
+
lines.append(" }")
|
|
104
|
+
|
|
105
|
+
if action.drift_kind == DriftKind.MISSING_FIELD and action.source_path is None:
|
|
106
|
+
default_expr = _ts_default_for_type(exp)
|
|
107
|
+
lines.append(f" if (!({f!r} in normalized)) {{")
|
|
108
|
+
lines.append(f" normalized[{f!r}] = {default_expr};")
|
|
109
|
+
lines.append(" }")
|
|
110
|
+
|
|
111
|
+
if action.drift_kind == DriftKind.NULLABILITY_DRIFT and "none" not in exp:
|
|
112
|
+
default_expr = _ts_default_for_type(exp)
|
|
113
|
+
lines.append(f" if (normalized[{f!r}] === null) {{")
|
|
114
|
+
lines.append(f" normalized[{f!r}] = {default_expr};")
|
|
115
|
+
lines.append(" }")
|
|
116
|
+
|
|
117
|
+
if "list[" in exp:
|
|
118
|
+
lines.append(
|
|
119
|
+
f" if ({f!r} in normalized && !Array.isArray(normalized[{f!r}])) {{"
|
|
120
|
+
)
|
|
121
|
+
lines.append(
|
|
122
|
+
f" normalized[{f!r}] = normalized[{f!r}] === null ? [] : [normalized[{f!r}]];"
|
|
123
|
+
)
|
|
124
|
+
lines.append(" }")
|
|
125
|
+
if "list[str]" in exp:
|
|
126
|
+
lines.append(f" if (Array.isArray(normalized[{f!r}])) {{")
|
|
127
|
+
lines.append(
|
|
128
|
+
f" normalized[{f!r}] = (normalized[{f!r}] as unknown[]).map((v) => String(v));"
|
|
129
|
+
)
|
|
130
|
+
lines.append(" }")
|
|
131
|
+
elif "list[int]" in exp:
|
|
132
|
+
lines.append(f" if (Array.isArray(normalized[{f!r}])) {{")
|
|
133
|
+
lines.append(
|
|
134
|
+
f" normalized[{f!r}] = (normalized[{f!r}] as unknown[])"
|
|
135
|
+
f".filter((v) => typeof v !== 'boolean' && !Number.isNaN(Number(v)))"
|
|
136
|
+
f".map((v) => Math.trunc(Number(v)));"
|
|
137
|
+
)
|
|
138
|
+
lines.append(" }")
|
|
139
|
+
elif "list[float]" in exp:
|
|
140
|
+
lines.append(f" if (Array.isArray(normalized[{f!r}])) {{")
|
|
141
|
+
lines.append(
|
|
142
|
+
f" normalized[{f!r}] = (normalized[{f!r}] as unknown[])"
|
|
143
|
+
f".filter((v) => typeof v !== 'boolean' && !Number.isNaN(Number(v)))"
|
|
144
|
+
f".map((v) => Number(v));"
|
|
145
|
+
)
|
|
146
|
+
lines.append(" }")
|
|
147
|
+
elif "list[bool]" in exp:
|
|
148
|
+
lines.append(f" if (Array.isArray(normalized[{f!r}])) {{")
|
|
149
|
+
lines.append(
|
|
150
|
+
f" normalized[{f!r}] = (normalized[{f!r}] as unknown[])"
|
|
151
|
+
f".map((v) => _parseBool(v));"
|
|
152
|
+
)
|
|
153
|
+
lines.append(" }")
|
|
154
|
+
elif "int" in exp and "dict" not in exp:
|
|
155
|
+
lines.append(
|
|
156
|
+
f" if ({f!r} in normalized && (typeof normalized[{f!r}] === 'string' "
|
|
157
|
+
f"|| typeof normalized[{f!r}] === 'number')) {{"
|
|
158
|
+
)
|
|
159
|
+
lines.append(f" const _num = Number(normalized[{f!r}]);")
|
|
160
|
+
lines.append(" if (!Number.isNaN(_num)) {")
|
|
161
|
+
lines.append(f" normalized[{f!r}] = Math.trunc(_num);")
|
|
162
|
+
lines.append(" }")
|
|
163
|
+
lines.append(" }")
|
|
164
|
+
elif "float" in exp and "dict" not in exp:
|
|
165
|
+
lines.append(
|
|
166
|
+
f" if ({f!r} in normalized && (typeof normalized[{f!r}] === 'string' "
|
|
167
|
+
f"|| typeof normalized[{f!r}] === 'number')) {{"
|
|
168
|
+
)
|
|
169
|
+
lines.append(f" const _num = Number(normalized[{f!r}]);")
|
|
170
|
+
lines.append(" if (!Number.isNaN(_num)) {")
|
|
171
|
+
lines.append(f" normalized[{f!r}] = _num;")
|
|
172
|
+
lines.append(" }")
|
|
173
|
+
lines.append(" }")
|
|
174
|
+
elif "bool" in exp:
|
|
175
|
+
lines.append(f" if ({f!r} in normalized) {{")
|
|
176
|
+
lines.append(f" normalized[{f!r}] = _parseBool(normalized[{f!r}]);")
|
|
177
|
+
lines.append(" }")
|
|
178
|
+
elif exp == "str" or exp == "str | none":
|
|
179
|
+
lines.append(
|
|
180
|
+
f" if ({f!r} in normalized && normalized[{f!r}] !== null "
|
|
181
|
+
f"&& typeof normalized[{f!r}] !== 'string') {{"
|
|
182
|
+
)
|
|
183
|
+
lines.append(f" if (typeof normalized[{f!r}] === 'object') {{")
|
|
184
|
+
lines.append(
|
|
185
|
+
f" const _obj = normalized[{f!r}] as Record<string, unknown>;"
|
|
186
|
+
)
|
|
187
|
+
lines.append(
|
|
188
|
+
" for (const _k of ['id', 'code', 'value', 'name', 'email', 'address']) {"
|
|
189
|
+
)
|
|
190
|
+
lines.append(" if (typeof _obj[_k] === 'string') {")
|
|
191
|
+
lines.append(f" normalized[{f!r}] = _obj[_k];")
|
|
192
|
+
lines.append(" break;")
|
|
193
|
+
lines.append(" }")
|
|
194
|
+
lines.append(" }")
|
|
195
|
+
lines.append(" } else {")
|
|
196
|
+
lines.append(f" normalized[{f!r}] = String(normalized[{f!r}]);")
|
|
197
|
+
lines.append(" }")
|
|
198
|
+
lines.append(" }")
|
|
199
|
+
|
|
200
|
+
return lines
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def synthesize_zod_preprocessor(plan: PatchPlan) -> str:
|
|
204
|
+
grouped: dict[str, list[PatchAction]] = defaultdict(list)
|
|
205
|
+
for action in plan.actions:
|
|
206
|
+
grouped[action.target_model].append(action)
|
|
207
|
+
|
|
208
|
+
model_paths: dict[str, tuple[str, ...]] = {plan.model_name: ()}
|
|
209
|
+
for model_name, actions in grouped.items():
|
|
210
|
+
if model_name == plan.model_name:
|
|
211
|
+
continue
|
|
212
|
+
for act in actions:
|
|
213
|
+
parent_path = act.nested_path or act.target_parent_path
|
|
214
|
+
if parent_path:
|
|
215
|
+
model_paths[model_name] = tuple(str(s) for s in parent_path)
|
|
216
|
+
break
|
|
217
|
+
|
|
218
|
+
ordered_models = sorted(
|
|
219
|
+
[m for m in grouped if m != plan.model_name],
|
|
220
|
+
key=lambda m: -len(model_paths.get(m, ())),
|
|
221
|
+
)
|
|
222
|
+
if plan.model_name not in ordered_models:
|
|
223
|
+
ordered_models.append(plan.model_name)
|
|
224
|
+
|
|
225
|
+
known_paths = set(model_paths.values())
|
|
226
|
+
sections: list[str] = ["import { z } from 'zod';", ""]
|
|
227
|
+
if any("bool" in act.expected_type.lower() for act in plan.actions):
|
|
228
|
+
sections.extend(
|
|
229
|
+
[
|
|
230
|
+
"function _parseBool(v: unknown): unknown {",
|
|
231
|
+
" if (typeof v === 'boolean') return v;",
|
|
232
|
+
" if (typeof v === 'string') {",
|
|
233
|
+
" const s = v.trim().toLowerCase();",
|
|
234
|
+
" if (s === 'true' || s === '1' || s === 'yes') return true;",
|
|
235
|
+
" if (s === 'false' || s === '0' || s === 'no') return false;",
|
|
236
|
+
" return v;",
|
|
237
|
+
" }",
|
|
238
|
+
" if (typeof v === 'number') return v !== 0;",
|
|
239
|
+
" return v;",
|
|
240
|
+
"}",
|
|
241
|
+
"",
|
|
242
|
+
]
|
|
243
|
+
)
|
|
244
|
+
for model_name in ordered_models:
|
|
245
|
+
actions = grouped.get(model_name, [])
|
|
246
|
+
fn_name = f"normalize{model_name}Drift"
|
|
247
|
+
sections.append(f"export function {fn_name}(data: unknown): unknown {{")
|
|
248
|
+
sections.append(" if (typeof data !== 'object' || data === null || Array.isArray(data)) {")
|
|
249
|
+
sections.append(" return data;")
|
|
250
|
+
sections.append(" }")
|
|
251
|
+
sections.append(
|
|
252
|
+
" const normalized: Record<string, unknown> = "
|
|
253
|
+
"{ ...(data as Record<string, unknown>) };"
|
|
254
|
+
)
|
|
255
|
+
for idx, action in enumerate(actions):
|
|
256
|
+
sections.extend(_emit_ts_action_lines(action, action_idx=idx))
|
|
257
|
+
|
|
258
|
+
my_path = model_paths.get(model_name)
|
|
259
|
+
if my_path is not None:
|
|
260
|
+
child_links: dict[str, str] = {}
|
|
261
|
+
deep_links: list[tuple[tuple[str, ...], str]] = []
|
|
262
|
+
for other_model, other_path in model_paths.items():
|
|
263
|
+
if other_model == model_name or len(other_path) <= len(my_path):
|
|
264
|
+
continue
|
|
265
|
+
if other_path[: len(my_path)] != my_path:
|
|
266
|
+
continue
|
|
267
|
+
rel_path = other_path[len(my_path) :]
|
|
268
|
+
child_fn = f"normalize{other_model}Drift"
|
|
269
|
+
if len(rel_path) == 1:
|
|
270
|
+
child_links.setdefault(rel_path[0], child_fn)
|
|
271
|
+
else:
|
|
272
|
+
has_intermediate = any(
|
|
273
|
+
other_path[:k] in known_paths
|
|
274
|
+
for k in range(len(my_path) + 1, len(other_path))
|
|
275
|
+
)
|
|
276
|
+
if not has_intermediate:
|
|
277
|
+
deep_links.append((rel_path, child_fn))
|
|
278
|
+
|
|
279
|
+
for child_field, child_fn in child_links.items():
|
|
280
|
+
sections.append(
|
|
281
|
+
f" if (Array.isArray(normalized[{child_field!r}])) {{"
|
|
282
|
+
)
|
|
283
|
+
sections.append(
|
|
284
|
+
f" normalized[{child_field!r}] = "
|
|
285
|
+
f"(normalized[{child_field!r}] as unknown[]).map((item) => {child_fn}(item));"
|
|
286
|
+
)
|
|
287
|
+
sections.append(
|
|
288
|
+
f" }} else if (typeof normalized[{child_field!r}] === 'object' "
|
|
289
|
+
f"&& normalized[{child_field!r}] !== null) {{"
|
|
290
|
+
)
|
|
291
|
+
sections.append(
|
|
292
|
+
f" normalized[{child_field!r}] = {child_fn}(normalized[{child_field!r}]);"
|
|
293
|
+
)
|
|
294
|
+
sections.append(" }")
|
|
295
|
+
|
|
296
|
+
for link_idx, (rel_path, child_fn) in enumerate(deep_links):
|
|
297
|
+
helper_name = f"_link_{link_idx}"
|
|
298
|
+
head_field = rel_path[0]
|
|
299
|
+
tail_json = "[" + ", ".join(repr(s) for s in rel_path[1:]) + "]"
|
|
300
|
+
sections.append(
|
|
301
|
+
f" const {helper_name} = (node: unknown, path: string[]): unknown => {{"
|
|
302
|
+
)
|
|
303
|
+
sections.append(f" if (path.length === 0) return {child_fn}(node);")
|
|
304
|
+
sections.append(
|
|
305
|
+
f" if (Array.isArray(node)) "
|
|
306
|
+
f"return node.map((item) => {helper_name}(item, path));"
|
|
307
|
+
)
|
|
308
|
+
sections.append(" if (typeof node === 'object' && node !== null) {")
|
|
309
|
+
sections.append(" const rec = { ...(node as Record<string, unknown>) };")
|
|
310
|
+
sections.append(" const [head, ...rest] = path;")
|
|
311
|
+
sections.append(
|
|
312
|
+
f" if (head in rec) rec[head] = {helper_name}(rec[head], rest);"
|
|
313
|
+
)
|
|
314
|
+
sections.append(" return rec;")
|
|
315
|
+
sections.append(" }")
|
|
316
|
+
sections.append(" return node;")
|
|
317
|
+
sections.append(" };")
|
|
318
|
+
sections.append(f" if ({head_field!r} in normalized) {{")
|
|
319
|
+
sections.append(
|
|
320
|
+
f" normalized[{head_field!r}] = "
|
|
321
|
+
f"{helper_name}(normalized[{head_field!r}], {tail_json});"
|
|
322
|
+
)
|
|
323
|
+
sections.append(" }")
|
|
324
|
+
|
|
325
|
+
sections.append(" return normalized;")
|
|
326
|
+
sections.append("}")
|
|
327
|
+
sections.append("")
|
|
328
|
+
|
|
329
|
+
root_fn = f"normalize{plan.model_name}Drift"
|
|
330
|
+
sections.append(
|
|
331
|
+
"export function createSelfHealZodAdapter<T extends z.ZodTypeAny>(schema: T) {"
|
|
332
|
+
)
|
|
333
|
+
sections.append(f" return z.preprocess({root_fn}, schema);")
|
|
334
|
+
sections.append("}")
|
|
335
|
+
sections.append("")
|
|
336
|
+
return "\n".join(sections)
|