stacklatch 0.1.0a1__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.
stacklatch/v22.py ADDED
@@ -0,0 +1,483 @@
1
+ """V2.2 static interface discovery and independently verified Docker adapters.
2
+
3
+ The index is evidence for ranking, never a proof of semantics or safety. Source
4
+ modules are imported only inside the existing restricted Docker runtime.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ import ast
9
+ import hashlib
10
+ import json
11
+ import math
12
+ import re
13
+ import subprocess
14
+ import tempfile
15
+ from dataclasses import asdict, dataclass
16
+ from pathlib import Path
17
+ from typing import Any
18
+
19
+ from .core import (Analysis, DOCKER_IMAGE, InspectionError, _copy_checked,
20
+ _write_deterministic_zip, inspect_tree)
21
+ from .v21 import _checked_cases, _execute
22
+
23
+ IDENT = re.compile(r"[A-Za-z_][A-Za-z0-9_]*\Z")
24
+ DENIED = {"eval", "exec", "compile", "open", "__import__", "globals", "locals",
25
+ "getattr", "setattr", "delattr", "breakpoint", "input"}
26
+ STDLIB = {"re", "math", "argparse", "sys", "pathlib", "typing"}
27
+
28
+
29
+ def _metadata_v22(root: Path, analysis: Analysis) -> None:
30
+ """Read dependency declarations statically; never execute build metadata."""
31
+ import tomllib
32
+ for path in analysis.manifests:
33
+ if path.endswith("pyproject.toml"):
34
+ data = tomllib.loads((root / path).read_text(encoding="utf-8"))
35
+ poetry = data.get("tool", {}).get("poetry", {}).get("dependencies", {})
36
+ if not isinstance(poetry, dict) or set(poetry) - {"python"}:
37
+ raise InspectionError("Poetry runtime dependencies unsupported")
38
+ if path.endswith("setup.py"):
39
+ tree = _read_ast(root, analysis, path)
40
+ calls = [n for n in ast.walk(tree) if isinstance(n, ast.Call)
41
+ and ((isinstance(n.func, ast.Name) and n.func.id == "setup")
42
+ or (isinstance(n.func, ast.Attribute)
43
+ and isinstance(n.func.value, ast.Name)
44
+ and n.func.value.id == "setuptools" and n.func.attr == "setup"))]
45
+ if len(calls) != 1 or calls[0].args or any(k.arg is None for k in calls[0].keywords):
46
+ raise InspectionError("Ambiguous setup.py metadata")
47
+ for key in calls[0].keywords:
48
+ if key.arg in {"install_requires", "extras_require", "setup_requires"}:
49
+ if not _literal(key.value) or ast.literal_eval(key.value):
50
+ raise InspectionError("setup.py runtime dependencies unsupported")
51
+
52
+
53
+ def _module(path: str) -> str:
54
+ parts = list(Path(path).parts)
55
+ if parts and parts[0] == "src":
56
+ parts.pop(0)
57
+ if not parts or parts[-1] == "setup.py" or not parts[-1].endswith(".py"):
58
+ raise InspectionError("Unsupported Python module path")
59
+ parts[-1] = parts[-1][:-3]
60
+ if parts[-1] == "__init__":
61
+ parts.pop()
62
+ if not parts or any(not IDENT.fullmatch(p) for p in parts):
63
+ raise InspectionError("Invalid module path")
64
+ return ".".join(parts)
65
+
66
+
67
+ def _read_ast(root: Path, analysis: Analysis, path: str) -> ast.Module:
68
+ if path not in analysis.file_sha256:
69
+ raise InspectionError("Module absent from pinned source")
70
+ raw = (root / path).read_bytes()
71
+ if hashlib.sha256(raw).hexdigest() != analysis.file_sha256[path]:
72
+ raise InspectionError("Source changed before AST inspection")
73
+ try:
74
+ return ast.parse(raw, filename=path)
75
+ except (SyntaxError, UnicodeError) as exc:
76
+ raise InspectionError("Invalid Python source") from exc
77
+
78
+
79
+ def _literal(node: ast.AST) -> bool:
80
+ if (isinstance(node, ast.Call) and isinstance(node.func, ast.Name)
81
+ and node.func.id == "float" and len(node.args) == 1 and not node.keywords
82
+ and isinstance(node.args[0], ast.Constant)
83
+ and node.args[0].value in {"inf", "-inf"}):
84
+ return True
85
+ try:
86
+ ast.literal_eval(node)
87
+ return True
88
+ except (ValueError, TypeError, SyntaxError):
89
+ return False
90
+
91
+
92
+ def _constant_value(node: ast.AST, names: set[str]) -> bool:
93
+ if _literal(node):
94
+ return True
95
+ if isinstance(node, (ast.List, ast.Tuple, ast.Set)):
96
+ return all(_constant_value(x, names) for x in node.elts)
97
+ if isinstance(node, ast.Dict):
98
+ return all(_constant_value(k, names) and _constant_value(v, names)
99
+ for k, v in zip(node.keys, node.values, strict=True))
100
+ if isinstance(node, ast.Name):
101
+ return node.id in names
102
+ if isinstance(node, ast.Call):
103
+ return (isinstance(node.func, ast.Attribute) and isinstance(node.func.value, ast.Name)
104
+ and node.func.value.id == "re" and node.func.attr == "compile"
105
+ and len(node.args) in (1, 2) and not node.keywords
106
+ and all(_literal(x) for x in node.args))
107
+ # Audited inverse lookup used by the pinned pluralizer. Only a previously
108
+ # accepted literal dictionary can be the iteration source.
109
+ if isinstance(node, ast.DictComp) and len(node.generators) == 1:
110
+ gen = node.generators[0]
111
+ return (not gen.ifs and not gen.is_async
112
+ and isinstance(gen.target, ast.Tuple)
113
+ and [x.id for x in gen.target.elts if isinstance(x, ast.Name)] == ["k", "v"]
114
+ and isinstance(gen.iter, ast.Call) and not gen.iter.args
115
+ and not gen.iter.keywords and isinstance(gen.iter.func, ast.Attribute)
116
+ and gen.iter.func.attr == "items"
117
+ and isinstance(gen.iter.func.value, ast.Name)
118
+ and gen.iter.func.value.id in names
119
+ and isinstance(node.key, ast.Name) and node.key.id == "v"
120
+ and isinstance(node.value, ast.Name) and node.value.id == "k")
121
+ return False
122
+
123
+
124
+ def _module_policy(tree: ast.Module, module: str, *, initializer: bool = False) -> list[str]:
125
+ """Allow a bounded import-time grammar; reject dynamic module execution."""
126
+ imported: list[str] = []
127
+ constants: set[str] = set()
128
+ for node in tree.body:
129
+ if isinstance(node, ast.Expr) and isinstance(node.value, ast.Constant) and isinstance(node.value.value, str):
130
+ continue
131
+ if isinstance(node, ast.Import):
132
+ if any(x.name not in STDLIB or x.asname for x in node.names):
133
+ raise InspectionError("Unaudited module import")
134
+ imported.extend(x.name for x in node.names)
135
+ elif isinstance(node, ast.ImportFrom):
136
+ if node.level or any(x.asname or x.name == "*" for x in node.names):
137
+ raise InspectionError("Unaudited from-import")
138
+ if node.module == "__future__" and [x.name for x in node.names] == ["annotations"]:
139
+ continue
140
+ if node.module == "typing" and all(x.name == "Final" for x in node.names):
141
+ imported.append("typing.Final")
142
+ continue
143
+ if node.module == "pathlib" and [x.name for x in node.names] == ["Path"]:
144
+ imported.append("pathlib.Path")
145
+ continue
146
+ if initializer and node.module and (node.module == module or node.module.startswith(module + ".")):
147
+ imported.append(node.module)
148
+ continue
149
+ raise InspectionError("Unaudited from-import")
150
+ elif isinstance(node, (ast.Assign, ast.AnnAssign)):
151
+ targets = node.targets if isinstance(node, ast.Assign) else [node.target]
152
+ value = node.value
153
+ if (len(targets) != 1 or not isinstance(targets[0], ast.Name)
154
+ or value is None or not _constant_value(value, constants)):
155
+ raise InspectionError("Dynamic module constant")
156
+ constants.add(targets[0].id)
157
+ elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
158
+ if isinstance(node, ast.AsyncFunctionDef) or node.decorator_list or node.args.vararg or node.args.kwarg:
159
+ raise InspectionError("Unsupported function definition")
160
+ if any(not _literal(x) for x in node.args.defaults + [d for d in node.args.kw_defaults if d]):
161
+ raise InspectionError("Evaluated function default")
162
+ # Annotated parameters must not evaluate arbitrary expressions at import.
163
+ if "from __future__ import annotations" not in ast.unparse(tree):
164
+ anns = [x.annotation for x in node.args.posonlyargs + node.args.args + node.args.kwonlyargs]
165
+ anns.append(node.returns)
166
+ if any(a is not None and not isinstance(a, ast.Name) for a in anns):
167
+ raise InspectionError("Evaluated annotation")
168
+ elif (isinstance(node, ast.If) and isinstance(node.test, ast.Compare)
169
+ and isinstance(node.test.left, ast.Name) and node.test.left.id == "__name__"
170
+ and len(node.test.ops) == 1 and isinstance(node.test.ops[0], ast.Eq)
171
+ and len(node.test.comparators) == 1
172
+ and isinstance(node.test.comparators[0], ast.Constant)
173
+ and node.test.comparators[0].value == "__main__" and not node.orelse):
174
+ continue
175
+ else:
176
+ raise InspectionError("Unsupported import-time statement")
177
+ return imported
178
+
179
+
180
+ def _closure_policy(tree: ast.Module, selected: ast.FunctionDef) -> None:
181
+ functions = {n.name: n for n in tree.body if isinstance(n, ast.FunctionDef)}
182
+ pending, seen = [selected.name], set()
183
+ while pending:
184
+ name = pending.pop()
185
+ if name in seen:
186
+ continue
187
+ seen.add(name)
188
+ for node in ast.walk(functions[name]):
189
+ if isinstance(node, (ast.Import, ast.ImportFrom)):
190
+ raise InspectionError("Selected function closure imports dynamically")
191
+ if isinstance(node, ast.Name) and node.id in DENIED:
192
+ raise InspectionError("Unsafe function name")
193
+ if isinstance(node, ast.Attribute) and node.attr.startswith("__"):
194
+ raise InspectionError("Unsafe function attribute")
195
+ if isinstance(node, ast.Call):
196
+ if isinstance(node.func, ast.Name) and node.func.id in functions:
197
+ pending.append(node.func.id)
198
+ if isinstance(node.func, ast.Name) and node.func.id in DENIED:
199
+ raise InspectionError("Unsafe function call")
200
+
201
+
202
+ def _return_shape(fn: ast.FunctionDef) -> tuple[str, list[str], list[str]]:
203
+ keys: set[str] = set()
204
+ evidence = []
205
+ for node in ast.walk(fn):
206
+ if isinstance(node, ast.Return) and isinstance(node.value, ast.Dict):
207
+ literal_keys = [k.value for k in node.value.keys
208
+ if isinstance(k, ast.Constant) and isinstance(k.value, str)]
209
+ if len(literal_keys) == len(node.value.keys):
210
+ keys.update(literal_keys)
211
+ evidence.append(f"literal dict return at line {node.lineno}")
212
+ if keys:
213
+ return "dict", sorted(keys), evidence
214
+ if fn.returns is not None:
215
+ annotation = ast.unparse(fn.returns)
216
+ if annotation in {"str", "int", "float", "bool", "dict"}:
217
+ return annotation, [], [f"return annotation at line {fn.lineno}"]
218
+ if any(isinstance(n, ast.Return) and isinstance(n.value, ast.Constant)
219
+ and isinstance(n.value.value, str) for n in ast.walk(fn)):
220
+ return "str", [], [f"literal string return in function at line {fn.lineno}"]
221
+ return "unknown", [], []
222
+
223
+
224
+ def index_repository(root: Path, analysis: Analysis) -> list[dict[str, Any]]:
225
+ if analysis.language != "python" or analysis.license != "MIT-like (heuristic)" or analysis.dependencies:
226
+ raise InspectionError("Only MIT-like Python without runtime dependencies is supported")
227
+ _metadata_v22(root, analysis)
228
+ index: list[dict[str, Any]] = []
229
+ for path in sorted(analysis.api):
230
+ if path.endswith("/__init__.py") or path == "__init__.py":
231
+ continue
232
+ if hashlib.sha256((root / path).read_bytes()).hexdigest() != analysis.file_sha256[path]:
233
+ raise InspectionError("Source changed before AST inspection")
234
+ try:
235
+ module = _module(path)
236
+ tree = _read_ast(root, analysis, path)
237
+ imports = _module_policy(tree, module)
238
+ except InspectionError as exc:
239
+ index.append({"path": path, "supported": False, "reason": str(exc)})
240
+ continue
241
+ for fn in (n for n in tree.body if isinstance(n, ast.FunctionDef) and not n.name.startswith("_")):
242
+ params = []
243
+ args = fn.args.posonlyargs + fn.args.args
244
+ defaults = [None] * (len(args) - len(fn.args.defaults)) + fn.args.defaults
245
+ for arg, default in zip(args, defaults, strict=True):
246
+ params.append({"name": arg.arg, "annotation": ast.unparse(arg.annotation) if arg.annotation else "unknown",
247
+ "required": default is None,
248
+ "default": (ast.literal_eval(default) if default is not None
249
+ and isinstance(default, (ast.Constant, ast.List, ast.Tuple, ast.Dict))
250
+ else ast.unparse(default) if default is not None else None)})
251
+ shape, keys, shape_evidence = _return_shape(fn)
252
+ item = {"path": path, "module": module, "name": fn.name,
253
+ "line": fn.lineno, "end_line": fn.end_lineno,
254
+ "file_sha256": analysis.file_sha256[path], "parameters": params,
255
+ "return_annotation": ast.unparse(fn.returns) if fn.returns else "unknown",
256
+ "return_shape": shape, "return_keys": keys,
257
+ "shape_evidence": shape_evidence, "docstring": ast.get_docstring(fn) or "",
258
+ "imports": imports, "supported": True, "reason": None}
259
+ try:
260
+ _closure_policy(tree, fn)
261
+ if fn.args.kwonlyargs:
262
+ raise InspectionError("Keyword-only parameters unsupported")
263
+ except InspectionError as exc:
264
+ item["supported"], item["reason"] = False, str(exc)
265
+ index.append(item)
266
+ return index
267
+
268
+
269
+ def _score(left: dict[str, Any], right: dict[str, Any], pattern: str) -> tuple[int, list[dict[str, Any]]]:
270
+ contributions: list[dict[str, Any]] = []
271
+ def add(points: int, maximum: int, category: str, reason: str, evidence: str) -> None:
272
+ contributions.append({"category": category, "points": points, "maximum": maximum,
273
+ "reason": reason, "evidence": evidence})
274
+ output = left["return_shape"]
275
+ required = [p for p in right["parameters"] if p["required"]]
276
+ if pattern == "direct":
277
+ target_type = required[0]["annotation"]
278
+ compatible = output == target_type and output != "unknown"
279
+ add(35 if compatible else 0, 35, "type", "matching annotations" if compatible else "type unknown or inferred",
280
+ f"{left['path']}:{left['line']} → {right['path']}:{right['line']}")
281
+ else:
282
+ add(0, 35, "type", "dictionary key value types not statically established",
283
+ f"{left['path']}:{left['line']}")
284
+ add(25 if pattern == "fields" else (15 if output != "unknown" else 0), 25,
285
+ "shape", "literal dictionary keys" if pattern == "fields" else f"return shape {output}",
286
+ f"{left['path']}:{left['line']}")
287
+ names = {p["name"].lower() for p in right["parameters"]}
288
+ overlap = len(names & set(left.get("return_keys", [])))
289
+ add(min(15, 7 * overlap), 15, "names", f"{overlap} exact key/parameter matches",
290
+ f"{left['path']}:{left['line']} → {right['path']}:{right['line']}")
291
+ tokens = set(re.findall(r"[a-z]{4,}", (left["docstring"] + " " + left["name"]).lower()))
292
+ other = set(re.findall(r"[a-z]{4,}", (right["docstring"] + " " + right["name"]).lower()))
293
+ add(min(10, 2 * len(tokens & other)), 10, "docs", "shared descriptive tokens; semantic hint only",
294
+ f"{left['path']}:{left['line']} → {right['path']}:{right['line']}")
295
+ add(15 if all(not p["required"] for p in right["parameters"][len(required):]) else 0,
296
+ 15, "constraints", "optional remaining parameters", f"{right['path']}:{right['line']}")
297
+ return sum(x["points"] for x in contributions), contributions
298
+
299
+
300
+ def propose(left: Analysis, right: Analysis, left_root: Path, right_root: Path, goal: str) -> dict[str, Any]:
301
+ if not isinstance(goal, str) or not goal.strip():
302
+ raise InspectionError("A goal is required")
303
+ if any(a.provenance != "fetched_git_commit" for a in (left, right)):
304
+ raise InspectionError("Pinned fetched commits required")
305
+ indexes = [index_repository(root, analysis) for root, analysis in
306
+ zip((left_root, right_root), (left, right), strict=True)]
307
+ candidates = []
308
+ for src in indexes[0]:
309
+ if not src.get("supported") or len(src.get("parameters", [])) != 1:
310
+ continue
311
+ for target in indexes[1]:
312
+ if not target.get("supported"):
313
+ continue
314
+ required = [p for p in target["parameters"] if p["required"]]
315
+ if len(required) == 1:
316
+ pattern = "direct"
317
+ elif len(required) == 2 and src["return_shape"] == "dict" and src["return_keys"]:
318
+ pattern = "fields"
319
+ else:
320
+ continue
321
+ score, contributions = _score(src, target, pattern)
322
+ candidates.append({"id": len(candidates), "pattern": pattern, "score": score,
323
+ "score_kind": "ranking_evidence_not_probability",
324
+ "contributions": contributions,
325
+ "source": {"path": src["path"], "module": src["module"], "name": src["name"]},
326
+ "target": {"path": target["path"], "module": target["module"], "name": target["name"]},
327
+ "target_required": [p["name"] for p in required],
328
+ "available_fields": src["return_keys"],
329
+ "mapping_suggestion": ({required[0]["name"]: "$output"} if pattern == "direct"
330
+ else {p["name"]: p["name"] for p in required
331
+ if p["name"] in src["return_keys"]}),
332
+ "requires_confirmation": True,
333
+ "uncertainty": ["AST and names cannot prove semantic compatibility",
334
+ "Independent acceptance oracles are required"],
335
+ "verified": False})
336
+ candidates.sort(key=lambda c: (-c["score"], c["source"]["path"], c["source"]["name"],
337
+ c["target"]["path"], c["target"]["name"]))
338
+ for i, candidate in enumerate(candidates):
339
+ candidate["id"] = i
340
+ candidate["ties"] = [j for j, other in enumerate(candidates)
341
+ if j != i and other["score"] == candidate["score"]]
342
+ return {"schema_version": 22, "goal": goal, "proposed": True, "verified": False,
343
+ "sources": [{"url": a.source, "commit": a.commit, "content_sha256": a.content_sha256,
344
+ "file_sha256": a.file_sha256, "kind": a.provenance}
345
+ for a in (left, right)],
346
+ "indexes": indexes, "candidates": candidates,
347
+ "risk_flags": ["MIT-like license detection is heuristic",
348
+ "Ranking is not a correctness probability or verification"]}
349
+
350
+
351
+ def _provenance(root: Path, original: Analysis) -> Analysis:
352
+ actual = subprocess.run(["git", "-C", str(root), "rev-parse", "HEAD"],
353
+ check=True, capture_output=True, text=True, timeout=10).stdout.strip()
354
+ kind = subprocess.run(["git", "-C", str(root), "cat-file", "-t", "HEAD"],
355
+ check=True, capture_output=True, text=True, timeout=10).stdout.strip()
356
+ if actual != original.commit or kind != "commit":
357
+ raise InspectionError("Pinned Git HEAD changed")
358
+ current = inspect_tree(root, original.source, original.commit, _fetched_git=True,
359
+ v21=True, v22=True)
360
+ if current.content_sha256 != original.content_sha256 or current.file_sha256 != original.file_sha256:
361
+ raise InspectionError("Pinned source changed")
362
+ return current
363
+
364
+
365
+ def _stage(root: Path, analysis: Analysis, selected: dict[str, str], target: Path) -> str:
366
+ module = selected["module"]
367
+ path = selected["path"]
368
+ parts = list(Path(path).parts)
369
+ if parts[0] == "src":
370
+ parts.pop(0)
371
+ destination = target.joinpath(*parts)
372
+ _copy_checked(root, path, destination, analysis.file_sha256[path])
373
+ # Package initializers are imported by Python; copy exact audited bytes.
374
+ for depth in range(1, len(parts)):
375
+ prefix = Path(*Path(path).parts[:-len(parts)]) if len(parts) != len(Path(path).parts) else Path()
376
+ init = (prefix / Path(*parts[:depth]) / "__init__.py").as_posix()
377
+ if init in analysis.file_sha256:
378
+ tree = _read_ast(root, analysis, init)
379
+ _module_policy(tree, ".".join(parts[:depth]), initializer=True)
380
+ _copy_checked(root, init, target / Path(*parts[:depth]) / "__init__.py",
381
+ analysis.file_sha256[init])
382
+ return module
383
+
384
+
385
+ def _generate(roots: tuple[Path, Path], analyses: tuple[Analysis, Analysis],
386
+ artifact: Path, plan: dict[str, Any]) -> None:
387
+ artifact.mkdir()
388
+ candidate = plan["candidate"]
389
+ modules = [_stage(root, analysis, candidate[side], artifact / f"source_{i}")
390
+ for i, (root, analysis, side) in enumerate(
391
+ zip(roots, analyses, ("source", "target"), strict=True))]
392
+ mapping = plan["mapping"]
393
+ args = ", ".join(f"{key}=intermediate" if value == "$output"
394
+ else f"{key}=intermediate[{value!r}]" for key, value in mapping.items())
395
+ code = ("import json, sys\n"
396
+ "sys.path.insert(0, '/app/source_0')\n"
397
+ "sys.path.insert(0, '/app/source_1')\n"
398
+ f"from {modules[0]} import {candidate['source']['name']} as first\n"
399
+ f"from {modules[1]} import {candidate['target']['name']} as second\n\n"
400
+ "def integrate(value):\n"
401
+ + (" if not isinstance(value, str):\n raise TypeError('Expected str input')\n"
402
+ if plan["input_type"] == "str" else
403
+ " if isinstance(value, bool) or not isinstance(value, (int, float)):\n"
404
+ " raise TypeError('Expected numeric input')\n")
405
+ + " intermediate = first(value)\n"
406
+ + f" return second({args})\n\n"
407
+ + "if __name__ == '__main__':\n"
408
+ + " try:\n print(json.dumps({'ok': True, 'value': integrate(json.loads(sys.argv[1]))}))\n"
409
+ + " except Exception as exc:\n"
410
+ + " print(json.dumps({'ok': False, 'error': type(exc).__name__}))\n")
411
+ (artifact / "run.py").write_text(code, encoding="utf-8")
412
+ (artifact / "integration_plan.json").write_text(json.dumps(plan, indent=2, sort_keys=True),
413
+ encoding="utf-8")
414
+
415
+
416
+ def _mapping(candidate: dict[str, Any], mapping: dict[str, str]) -> None:
417
+ if not isinstance(mapping, dict) or set(mapping) != set(candidate["target_required"]):
418
+ raise InspectionError("Every required target argument needs an explicit binding")
419
+ if candidate["pattern"] == "direct":
420
+ if list(mapping.values()) != ["$output"]:
421
+ raise InspectionError("Invalid direct output binding")
422
+ elif (candidate["pattern"] == "fields"
423
+ and (len(set(mapping.values())) != len(mapping)
424
+ or any(value not in candidate["available_fields"] for value in mapping.values()))):
425
+ raise InspectionError("Unknown or reused dictionary field")
426
+ if any(not IDENT.fullmatch(key) for key in mapping):
427
+ raise InspectionError("Invalid parameter name")
428
+
429
+
430
+ def verify(roots: tuple[Path, Path], originals: tuple[Analysis, Analysis],
431
+ proposal: dict[str, Any], candidate_id: int, mapping: dict[str, str],
432
+ cases: list[dict[str, Any]], input_type: str, out: Path,
433
+ *, confirmed: bool) -> dict[str, Any]:
434
+ out.mkdir(parents=True, exist_ok=True)
435
+ archive = out / "prototype.zip"
436
+ archive.unlink(missing_ok=True)
437
+ report: dict[str, Any] = {"status": "failed", "verified": False, "tests": [],
438
+ "runtime_image": DOCKER_IMAGE}
439
+ try:
440
+ if not confirmed or input_type not in {"str", "number"}:
441
+ raise InspectionError("Explicit semantic confirmation and input contract required")
442
+ _checked_cases(cases)
443
+ analyses = tuple(_provenance(root, original)
444
+ for root, original in zip(roots, originals, strict=True))
445
+ canonical = propose(*analyses, *roots, proposal["goal"])
446
+ if proposal != canonical or type(candidate_id) is not int or not 0 <= candidate_id < len(canonical["candidates"]):
447
+ raise InspectionError("Proposal differs from reindexed pinned source")
448
+ candidate = canonical["candidates"][candidate_id]
449
+ _mapping(candidate, mapping)
450
+ plan = {"schema_version": 22, "goal": canonical["goal"], "proposed": True,
451
+ "verified": False, "candidate": candidate, "mapping": mapping,
452
+ "input_type": input_type, "sources": canonical["sources"],
453
+ "independent_cases": cases}
454
+ with tempfile.TemporaryDirectory() as temp:
455
+ artifact = Path(temp) / "prototype"
456
+ _generate(roots, analyses, artifact, plan)
457
+ for i, case in enumerate(cases):
458
+ actual = _execute(artifact, case["input"])
459
+ passed = (actual == {"ok": False, "error": case["error"]} if "error" in case
460
+ else actual.get("ok") is True and
461
+ (math.isclose(actual.get("value"), case["expected"], rel_tol=0,
462
+ abs_tol=case["abs_tol"])
463
+ if "abs_tol" in case and type(actual.get("value")) in (float, int)
464
+ and type(case["expected"]) in (float, int)
465
+ else actual.get("value") == case["expected"]))
466
+ report["tests"].append({"index": i, "input": case["input"], "actual": actual,
467
+ "expected": {k: v for k, v in case.items() if k != "input"},
468
+ "passed": passed, "backend": "docker"})
469
+ if not passed:
470
+ raise InspectionError(f"Independent acceptance case {i} failed")
471
+ report["files_sha256"] = _write_deterministic_zip(artifact, archive)
472
+ report["archive_sha256"] = hashlib.sha256(archive.read_bytes()).hexdigest()
473
+ report["sources_provenance"] = canonical["sources"]
474
+ report["status"], report["verified"] = "passed", True
475
+ except Exception as exc:
476
+ archive.unlink(missing_ok=True)
477
+ report["error"] = f"{type(exc).__name__}: {exc}"
478
+ report.pop("archive_sha256", None)
479
+ report.pop("files_sha256", None)
480
+ (out / "report.json").write_text(json.dumps(report, indent=2), encoding="utf-8")
481
+ (out / "report.md").write_text("# StackLatch V2.2 validation\n\n" +
482
+ json.dumps(report, indent=2) + "\n", encoding="utf-8")
483
+ return report
stacklatch/v23.py ADDED
@@ -0,0 +1,159 @@
1
+ """Deterministic goal-aware proposals. All scoring is static and unverified.
2
+
3
+ The fixed rules were written after the original holdout labels were public.
4
+ Some token choices overlap those labels; see README evaluation provenance.
5
+ The published holdout therefore cannot establish independent generalization.
6
+ Ranking scores are ordinal evidence, never probabilities.
7
+ """
8
+ from __future__ import annotations
9
+
10
+ import itertools
11
+ import re
12
+ from typing import Any
13
+
14
+ from .core import InspectionError
15
+ from .v22 import _mapping
16
+
17
+ STOP = frozenset("a an the into in on of to then and input text markdown heading noun english make turn calculate determine whether is for with".split())
18
+ AMBIGUOUS = frozenset("change human readable density something various any".split())
19
+ # Lexicon includes published-holdout words. It is frozen for subsequent tests,
20
+ # but cannot be treated as independently developed from the original holdout.
21
+ ALIASES = {"url": "slug", "safe": "slug", "plural": "pluralize",
22
+ "singular": "singularize", "count": "count", "case": "case"}
23
+ NO_MATCH = frozenset("translate german french palindrome sentiment".split())
24
+ MAX_MAPPINGS = 120
25
+
26
+
27
+ def _words(value: str) -> list[str]:
28
+ value = re.sub(r"([a-z])([A-Z])", r"\1 \2", value)
29
+ return re.findall(r"[a-z]+", value.lower())
30
+
31
+
32
+ def _stem(value: str) -> str:
33
+ if value.endswith("ies") and len(value) > 4:
34
+ return value[:-3] + "y"
35
+ if value.endswith("s") and len(value) > 4 and not value.endswith("ss"):
36
+ return value[:-1]
37
+ return value
38
+
39
+
40
+ def _match(token: str, name: str) -> bool:
41
+ parts = _words(name)
42
+ return any(_stem(token) == _stem(part) or (len(token) > 4 and part.startswith(token))
43
+ for part in parts)
44
+
45
+
46
+ def _name_hits(goal: str, name: str) -> list[str]:
47
+ tokens = [_stem(t) for t in _words(goal) if t not in STOP]
48
+ return sorted({t for t in tokens if _match(t, name) or _match(ALIASES.get(t, ""), name)})
49
+
50
+
51
+ def _ratio(goal: str) -> tuple[str, str] | None:
52
+ """Identify an explicitly ordered numerator/denominator phrase, not a function name."""
53
+ match = re.search(r"\b([a-z][a-z_-]*)\s+per\s+([a-z][a-z_-]*)\b", goal.lower())
54
+ return (_stem(match[1]), _stem(match[2])) if match else None
55
+
56
+
57
+ def _expanded(proposal: dict[str, Any]) -> list[dict[str, Any]]:
58
+ """Enumerate eligible complete mappings without peeking at the gold labels."""
59
+ expanded = []
60
+ for original in proposal["candidates"]:
61
+ required = original["target_required"]
62
+ if original["pattern"] == "direct":
63
+ mappings = [{required[0]: "$output"}]
64
+ else:
65
+ fields = original["available_fields"]
66
+ if len(fields) < len(required):
67
+ continue
68
+ if len(fields) > 12 or len(required) > 3:
69
+ continue
70
+ mappings = [dict(zip(required, values, strict=True))
71
+ for values in itertools.permutations(fields, len(required))]
72
+ if len(mappings) > MAX_MAPPINGS:
73
+ continue
74
+ for mapping in mappings:
75
+ _mapping(original, mapping)
76
+ expanded.append({"candidate_id": original["id"], "pattern": original["pattern"],
77
+ "source": original["source"], "target": original["target"],
78
+ "mapping": mapping, "structural_score": original["score"]})
79
+ return expanded
80
+
81
+
82
+ def rank_goal(proposal: dict[str, Any]) -> dict[str, Any]:
83
+ """Rank every structurally eligible candidate, retaining explicit uncertainty.
84
+
85
+ No repository module is imported or executed. A caller still has to confirm
86
+ semantics and supply independent acceptance cases to V2.2's Docker verify.
87
+ """
88
+ goal = proposal.get("goal")
89
+ if not isinstance(goal, str) or not goal.strip() or not proposal.get("proposed"):
90
+ raise InspectionError("A canonical static proposal with goal is required")
91
+ tokens = _words(goal)
92
+ ratio = _ratio(goal)
93
+ results = []
94
+ for candidate in _expanded(proposal):
95
+ src = candidate["source"]["name"]
96
+ dst = candidate["target"]["name"]
97
+ src_hits, dst_hits = _name_hits(goal, src), _name_hits(goal, dst)
98
+ evidence = []
99
+ relevance = 8 * len(src_hits) + 12 * len(dst_hits)
100
+ if src_hits:
101
+ evidence.append({"kind": "source_name", "goal_tokens": src_hits,
102
+ "source": f"{candidate['source']['path']}:{src}"})
103
+ if dst_hits:
104
+ evidence.append({"kind": "target_name", "goal_tokens": dst_hits,
105
+ "source": f"{candidate['target']['path']}:{dst}"})
106
+ if "whether" in tokens or "determine" in tokens:
107
+ if dst.startswith("is_"):
108
+ relevance += 16
109
+ evidence.append({"kind": "boolean_intent", "goal_tokens": ["whether", "determine"]})
110
+ elif dst.startswith("is_"):
111
+ relevance -= 12
112
+ if tokens and tokens[-1] in {"plural", "singular"} and _match(tokens[-1], dst):
113
+ relevance += 16
114
+ evidence.append({"kind": "requested_result", "goal_tokens": [tokens[-1]]})
115
+ if "count" in tokens and dst.startswith("count"):
116
+ relevance += 8
117
+ if "then" in tokens:
118
+ # The action after 'then' describes the target; avoid reversing a pipeline.
119
+ tail = goal.lower().split("then", 1)[1]
120
+ tail_hits = _name_hits(tail, dst)
121
+ relevance += 14 * len(tail_hits)
122
+ if tail_hits:
123
+ evidence.append({"kind": "target_order", "goal_tokens": tail_hits})
124
+ if ratio and candidate["pattern"] == "fields":
125
+ fields = list(candidate["mapping"].values())
126
+ target_text = dst + " " + next(
127
+ (x.get("docstring", "") for x in proposal["indexes"][1]
128
+ if x.get("path") == candidate["target"]["path"] and x.get("name") == dst), "")
129
+ divides = "divid" in target_text.lower() or " a/b " in target_text.lower()
130
+ if divides and len(fields) == 2 and _match(ratio[0], fields[0]) and _match(ratio[1], fields[1]):
131
+ relevance += 48
132
+ evidence.append({"kind": "ordered_ratio", "goal_tokens": list(ratio),
133
+ "binding": candidate["mapping"]})
134
+ if candidate["pattern"] == "fields" and not ratio:
135
+ relevance -= 12
136
+ results.append({**candidate, "relevance_score": relevance,
137
+ "ranking_score": relevance, "evidence": evidence,
138
+ "score_kind": "ordinal_evidence_not_probability",
139
+ "verified": False, "requires_confirmation": True})
140
+ results.sort(key=lambda x: (-x["ranking_score"], -x["structural_score"],
141
+ x["source"]["path"], x["source"]["name"],
142
+ x["target"]["path"], x["target"]["name"],
143
+ tuple(sorted(x["mapping"].items()))))
144
+ for i, item in enumerate(results):
145
+ item["rank"] = i + 1
146
+ reason = None
147
+ if any(t in NO_MATCH for t in tokens):
148
+ reason = "Unsupported requested transformation; no evidenced interface"
149
+ elif any(t in AMBIGUOUS for t in tokens) and not ratio:
150
+ reason = "Goal leaves the requested transformation or field binding ambiguous"
151
+ elif not results or not results[0]["evidence"] or results[0]["ranking_score"] < 16:
152
+ reason = "Insufficient goal evidence for both interface roles"
153
+ elif len(results) > 1 and results[0]["ranking_score"] == results[1]["ranking_score"]:
154
+ reason = "Equal semantic evidence for distinct complete mappings"
155
+ return {"schema_version": 23, "goal": goal, "sources": proposal["sources"],
156
+ "candidates": results, "selected": None if reason else results[0],
157
+ "abstained": reason is not None, "abstention_reason": reason,
158
+ "verified": False, "risk_flags": proposal["risk_flags"] +
159
+ ["Semantic evidence is incomplete; independent Docker oracles required"]}