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/v31.py ADDED
@@ -0,0 +1,380 @@
1
+ """V3.1 bounded structured-output integration; historical selectors stay frozen."""
2
+ from __future__ import annotations
3
+
4
+ import hashlib
5
+ import json
6
+ import re
7
+ import tempfile
8
+ from pathlib import Path
9
+ from typing import Any
10
+
11
+ from .core import DOCKER_IMAGE, InspectionError, _copy_checked, _write_deterministic_zip
12
+ from .v21 import _checked_cases
13
+ from .v22 import _provenance
14
+ from .v24 import _run_docker
15
+ from .v24_contract import check_schema, strict_dumps, validate
16
+ from .v25 import NEGATION, UNSUPPORTED, VAGUE, _hits, _terms
17
+ from .v31_graph import index_graph, stage_graph
18
+ from .v31_checkout import ClosureAnalysis, inspect_v31
19
+
20
+ MAX_CANDIDATES = 256
21
+ MAX_GOAL = 512
22
+ IDENT = re.compile(r"[A-Za-z_][A-Za-z0-9_]*\Z")
23
+ ORDER = re.compile(r"\b(?:then|followed by|afterwards)\b", re.I)
24
+
25
+
26
+ def _type(annotation: str) -> str:
27
+ return {"str": "string", "int": "integer", "float": "number",
28
+ "bool": "boolean"}.get(annotation, "unknown")
29
+
30
+
31
+ def _effective_annotation(parameter: dict) -> str:
32
+ return parameter["annotation"] if parameter["annotation"] != "unknown" else (parameter.get("inferred_type") or "unknown")
33
+
34
+
35
+ def _compatible(output: str, annotation: str) -> bool:
36
+ return (output == annotation and output in {"str", "int", "float", "bool"}
37
+ or output == "int" and annotation == "float")
38
+
39
+
40
+ def _source_bindings(source: dict) -> dict[str, str]:
41
+ return {p["name"]: "$input." + p["name"] for p in source["parameters"] if p["required"]}
42
+
43
+
44
+ def propose(left, right, left_root: Path, right_root: Path, goal: str) -> dict:
45
+ if not isinstance(goal, str) or not 0 < len(goal.strip()) <= MAX_GOAL:
46
+ raise InspectionError("Bounded natural-language goal required")
47
+ if any(a.provenance != "fetched_git_commit" for a in (left, right)):
48
+ raise InspectionError("Pinned fetched commits required")
49
+ indexes = [index_graph(root, a) for root, a in
50
+ zip((left_root, right_root), (left, right), strict=True)]
51
+ candidates = []
52
+ for source in indexes[0][0]:
53
+ if not source["supported"] or not source["parameters"] or source["return_shape"] == "unknown":
54
+ continue
55
+ for target in indexes[1][0]:
56
+ if not target["supported"] or not target["parameters"]:
57
+ continue
58
+ fields = source["return_fields"]
59
+ for output_parameter in target["parameters"]:
60
+ options = []
61
+ if _compatible(source["return_shape"], _effective_annotation(output_parameter)):
62
+ options.append(("$output", source["return_shape"], "direct"))
63
+ for field, proof in sorted(fields.items()):
64
+ if field == output_parameter["name"] and _compatible({"string": "str", "integer": "int", "number": "float", "boolean": "bool"}.get(proof["type"], ""), _effective_annotation(output_parameter)):
65
+ options.append(("$output." + field, proof["type"], "field"))
66
+ for output_binding, output_kind, pattern in options:
67
+ bindings = {p["name"]: (output_binding if p is output_parameter
68
+ else "$output." + p["name"] if
69
+ p["name"] in fields and _compatible(
70
+ {"string": "str", "integer": "int", "number": "float", "boolean": "bool"}.get(fields[p["name"]]["type"], ""), _effective_annotation(p))
71
+ else "$input." + p["name"])
72
+ for p in target["parameters"] if p["required"] or p is output_parameter}
73
+ if len(bindings) > 4 or len(candidates) >= MAX_CANDIDATES:
74
+ raise InspectionError("Candidate enumeration limit exceeded")
75
+ if any(c["source"]["path"] == source["path"] and c["source"]["name"] == source["name"]
76
+ and c["target"]["path"] == target["path"] and c["target"]["name"] == target["name"]
77
+ and c["target_bindings"] == bindings for c in candidates):
78
+ continue
79
+ candidates.append({
80
+ "id": len(candidates), "source": {k: source[k] for k in
81
+ ("module", "path", "name", "closure")},
82
+ "target": {k: target[k] for k in ("module", "path", "name", "closure")},
83
+ "source_bindings": _source_bindings(source),
84
+ "target_bindings": bindings,
85
+ "output_parameter": output_parameter["name"],
86
+ "pattern": "typed_dictionary_field" if pattern == "field" else "direct",
87
+ "type_uncertainty": ([{"parameter": output_parameter["name"],
88
+ "method_lines": output_parameter.get("type_evidence", []),
89
+ "status": "behavioral_hint_not_static_type_proof"}]
90
+ if output_parameter.get("inferred_type") else []),
91
+ "field_evidence": (f"{source['path']}:{fields[output_binding[8:]]['line']}"
92
+ if pattern == "field" else source["shape_evidence"]),
93
+ "verified": False, "requires_confirmation": True})
94
+ return {"schema_version": 31, "goal": goal, "sources": [
95
+ {"url": a.source, "commit": a.commit, "content_sha256": a.content_sha256,
96
+ "file_sha256": a.file_sha256, "kind": a.provenance} for a in (left, right)],
97
+ "indexes": [x[0] for x in indexes], "rejected_modules": [x[1] for x in indexes],
98
+ "candidates": candidates, "verified": False, "proposed": True,
99
+ "risk_flags": ["Static AST and text cannot prove runtime behavior"] }
100
+
101
+
102
+ def _index(proposal: dict, side: int, item: dict) -> dict:
103
+ return next(x for x in proposal["indexes"][side]
104
+ if x["path"] == item["path"] and x["name"] == item["name"])
105
+
106
+
107
+ def select(proposal: dict) -> dict:
108
+ if proposal.get("schema_version") != 31:
109
+ raise InspectionError("V3.1 proposal required")
110
+ goal = proposal["goal"]
111
+ parts = ORDER.split(goal)
112
+ ordered = len(parts) == 2
113
+ left, right = (_terms(parts[0]), _terms(parts[1])) if ordered else (_terms(goal), _terms(goal))
114
+ rankings = []
115
+ for candidate in proposal["candidates"]:
116
+ src = _index(proposal, 0, candidate["source"])
117
+ dst = _index(proposal, 1, candidate["target"])
118
+ src_hits = _hits(left, src["name"])
119
+ dst_hits = _hits(right, dst["name"])
120
+ if candidate["output_parameter"] != dst["parameters"][0]["name"]:
121
+ role = -18
122
+ else:
123
+ role = 12 if ordered else 0
124
+ nonoutput = [p for p in dst["parameters"] if p["required"]
125
+ and p["name"] != candidate["output_parameter"]]
126
+ # An explicit reference or named field gives evidence for a second
127
+ # argument; without it the plan is ranked, never auto-selected.
128
+ field_hits = [p["name"] for p in nonoutput if _terms(p["name"]) & right]
129
+ output_fields = [binding[8:] for binding in candidate["target_bindings"].values()
130
+ if binding.startswith("$output.")]
131
+ explicit_output = bool(output_fields) and all(_terms(field) & right for field in output_fields)
132
+ reference = len(nonoutput) == 1 and "reference" in right
133
+ if reference:
134
+ candidate = {**candidate, "target_bindings": {
135
+ **candidate["target_bindings"], nonoutput[0]["name"]: "$input.reference"}}
136
+ source_optional = [p for p in src["parameters"] if not p["required"]]
137
+ explicit_optional = [p for p in source_optional if
138
+ p["name"].lower() in left or
139
+ ("max" in p["name"].lower() and
140
+ {"maximum", "length"} <= left)]
141
+ if explicit_optional:
142
+ candidate = {**candidate, "source_bindings": {
143
+ **candidate["source_bindings"], **{p["name"]: "$input." + p["name"]
144
+ for p in explicit_optional}}}
145
+ score = 19 * len(src_hits) + 23 * len(dst_hits) + role
146
+ # An exact callable request is stronger than a suffix contained in a
147
+ # different callable (e.g. levenshtein vs damerau_levenshtein).
148
+ if ordered and dst["name"].lower().replace("_", " ") in parts[1].lower():
149
+ score += 14
150
+ score += 13 * (len(field_hits) + int(reference))
151
+ if explicit_output:
152
+ score += 17 * len(output_fields)
153
+ evidence = [{"kind": "source_operation", "tokens": src_hits,
154
+ "source": f"{src['path']}:{src['line']}"},
155
+ {"kind": "target_operation", "tokens": dst_hits,
156
+ "source": f"{dst['path']}:{dst['line']}"},
157
+ {"kind": "argument_role", "bindings": candidate["target_bindings"],
158
+ "reference_explicit": reference, "named_fields": field_hits,
159
+ "typed_output_fields": output_fields,
160
+ "output_field_provenance": candidate["field_evidence"]}]
161
+ rankings.append({**candidate, "rank": 0, "ordinal_score": score,
162
+ "evidence": evidence, "mapping_evidenced": bool(
163
+ (not nonoutput or len(field_hits) == len(nonoutput) or reference)
164
+ and (not output_fields or explicit_output)),
165
+ "verified": False})
166
+ rankings.sort(key=lambda c: (-c["ordinal_score"], c["source"]["path"],
167
+ c["source"]["name"], c["target"]["path"],
168
+ c["target"]["name"], c["output_parameter"]))
169
+ for i, row in enumerate(rankings, 1):
170
+ row["rank"] = i
171
+ terms = _terms(goal)
172
+ reason = None
173
+ if terms & UNSUPPORTED or terms & NEGATION or terms & VAGUE:
174
+ reason = "Unsupported, negated, or vague request"
175
+ elif not rankings:
176
+ reason = "No type-compatible complete mapping"
177
+ elif not rankings[0]["evidence"][0]["tokens"] or not rankings[0]["evidence"][1]["tokens"]:
178
+ reason = "Insufficient evidence for both operation roles"
179
+ elif not rankings[0]["mapping_evidenced"]:
180
+ reason = "Argument or output field lacks explicit role evidence"
181
+ elif rankings[0]["ordinal_score"] < 42:
182
+ reason = "Insufficient ordinal evidence"
183
+ elif len(rankings) > 1 and rankings[0]["ordinal_score"] - rankings[1]["ordinal_score"] < 8:
184
+ reason = "Ambiguous mappings; narrow score margin"
185
+ return {"schema_version": 31, "goal": goal, "sources": proposal["sources"],
186
+ "proposal_sha256": hashlib.sha256(strict_dumps(proposal)).hexdigest(),
187
+ "candidates": rankings[:50], "candidate_count": len(rankings),
188
+ "selected": None if reason else rankings[0], "abstained": bool(reason),
189
+ "abstention_reason": reason, "score_kind": "ordinal_evidence_not_probability",
190
+ "verified": False}
191
+
192
+
193
+ def _validate_bindings(proposal: dict, selected: dict, schemas: tuple[dict, dict, dict]) -> None:
194
+ source = _index(proposal, 0, selected["source"])
195
+ target = _index(proposal, 1, selected["target"])
196
+ input_schema, intermediate_schema, _ = schemas
197
+ if input_schema.get("type") != "object" or input_schema.get("additionalProperties") is not False:
198
+ raise InspectionError("A closed JSON object input schema is required")
199
+ if source["return_shape"] == "dict":
200
+ if (intermediate_schema.get("type") != "object" or
201
+ intermediate_schema.get("additionalProperties") is not False or
202
+ set(intermediate_schema.get("properties", {})) != set(source["return_fields"]) or
203
+ set(intermediate_schema.get("required", [])) != set(source["return_fields"])):
204
+ raise InspectionError("Intermediate schema must close all proven output fields")
205
+ for field, proof in source["return_fields"].items():
206
+ if intermediate_schema["properties"][field].get("type") != proof["type"]:
207
+ raise InspectionError("Intermediate schema disagrees with AST field type")
208
+ for side, function, bindings in (("source", source, selected["source_bindings"]),
209
+ ("target", target, selected["target_bindings"])):
210
+ params = function["parameters"]
211
+ names = {p["name"] for p in params}
212
+ if (set(bindings) - names or any(p["required"] and p["name"] not in bindings for p in params)
213
+ or list(bindings).count(selected["output_parameter"]) > 1):
214
+ raise InspectionError("Invalid or missing argument binding")
215
+ for p in params:
216
+ if p["name"] not in bindings:
217
+ continue
218
+ binding = bindings[p["name"]]
219
+ if binding == "$output":
220
+ if side != "target" or not _compatible(source["return_shape"], _effective_annotation(p)):
221
+ raise InspectionError("Output-to-parameter type mismatch")
222
+ elif isinstance(binding, str) and binding.startswith("$input."):
223
+ field = binding[7:]
224
+ properties = input_schema.get("properties", {})
225
+ if (not IDENT.fullmatch(field) or field not in properties
226
+ or field not in input_schema.get("required", [])
227
+ or properties[field]["type"] != _type(_effective_annotation(p))):
228
+ raise InspectionError("Input field absent, optional or type-incompatible")
229
+ elif side == "target" and isinstance(binding, str) and binding.startswith("$output."):
230
+ field = binding[8:]
231
+ proof = source["return_fields"].get(field, {})
232
+ primitive = {"string": "str", "integer": "int", "number": "float",
233
+ "boolean": "bool"}.get(proof.get("type"), "")
234
+ if (not IDENT.fullmatch(field) or source["return_shape"] != "dict"
235
+ or not primitive
236
+ or field not in intermediate_schema.get("required", [])
237
+ or not _compatible(primitive, _effective_annotation(p))
238
+ or intermediate_schema.get("properties", {}).get(field, {}).get("type") != proof["type"]):
239
+ raise InspectionError("Unproven output-field binding")
240
+ else:
241
+ raise InspectionError("Unknown binding expression")
242
+
243
+
244
+ def _generate(roots, analyses, artifact: Path, selected: dict, schemas) -> str:
245
+ # Python caches imported modules globally. A shared package/module name
246
+ # could silently execute the wrong repository's function.
247
+ left_modules = {p for name in selected['source']['closure'] for p in
248
+ ['.'.join(name.split('.')[:i]) for i in range(1, len(name.split('.')) + 1)]}
249
+ right_modules = {p for name in selected['target']['closure'] for p in
250
+ ['.'.join(name.split('.')[:i]) for i in range(1, len(name.split('.')) + 1)]}
251
+ if left_modules & right_modules:
252
+ raise InspectionError("Conflicting source module namespaces")
253
+ artifact.mkdir()
254
+ modules = [stage_graph(root, analysis, selected[side], artifact / f"source_{i}")
255
+ for i, (root, analysis, side) in enumerate(
256
+ zip(roots, analyses, ("source", "target"), strict=True))]
257
+ # The opt-in source-closure path copies unmodified MIT source into the ZIP.
258
+ # Preserve its copyright/license terms alongside each copied component.
259
+ # Legacy archives remain byte-identical to their frozen evidence.
260
+ for i, (root, analysis) in enumerate(zip(roots, analyses, strict=True)):
261
+ if isinstance(analysis, ClosureAnalysis):
262
+ licenses = [p for p in analysis.files if p.lower() in
263
+ {"license", "license.md", "license.txt"}]
264
+ notices = [p for p in analysis.files if p.lower() in
265
+ {"notice", "notice.md", "notice.txt"}]
266
+ if len(licenses) != 1 or len(notices) > 1 or analysis.license != "MIT-like (heuristic)":
267
+ raise InspectionError("Unverified source attribution files")
268
+ for name in licenses + notices:
269
+ _copy_checked(root, name, artifact / f"source_{i}" / name,
270
+ analysis.file_sha256[name])
271
+ contract = Path(__file__).with_name("v24_contract.py").read_bytes()
272
+ (artifact / "contract.py").write_bytes(contract)
273
+ code = ("import importlib, sys\n"
274
+ "from contract import strict_loads, strict_dumps, validate\n"
275
+ "sys.path.insert(0, '/app/source_0')\nsys.path.insert(0, '/app/source_1')\n"
276
+ f"first = getattr(importlib.import_module({modules[0]!r}), {selected['source']['name']!r})\n"
277
+ f"second = getattr(importlib.import_module({modules[1]!r}), {selected['target']['name']!r})\n"
278
+ f"SCHEMAS = {schemas!r}\n"
279
+ f"SRC = {selected['source_bindings']!r}\nDST = {selected['target_bindings']!r}\n"
280
+ f"SRC_PARAMS = {_index_params(selected, 0)!r}\n"
281
+ f"DST_PARAMS = {_index_params(selected, 1)!r}\n"
282
+ "def invoke(function, params, bindings, value, output):\n"
283
+ " args, kwargs, skipped_positional_default = [], {}, False\n"
284
+ " for name, kind in params:\n"
285
+ " if name not in bindings:\n"
286
+ " if kind == 'positional_only':\n"
287
+ " skipped_positional_default = True\n"
288
+ " continue\n"
289
+ " path = bindings[name]\n"
290
+ " arg = output if path == '$output' else (value[path[7:]] if path.startswith('$input.') else output[path[8:]])\n"
291
+ " if kind == 'positional_only':\n"
292
+ " if skipped_positional_default: raise ValueError('Positional default gap')\n"
293
+ " args.append(arg)\n"
294
+ " else: kwargs[name] = arg\n"
295
+ " return function(*args, **kwargs)\n"
296
+ "def integrate(value):\n"
297
+ " validate(value, SCHEMAS[0])\n"
298
+ " intermediate = invoke(first, SRC_PARAMS, SRC, value, None)\n"
299
+ " validate(intermediate, SCHEMAS[1])\n"
300
+ " result = invoke(second, DST_PARAMS, DST, value, intermediate)\n"
301
+ " validate(result, SCHEMAS[2])\n"
302
+ " return result\n"
303
+ "for raw in sys.stdin.buffer:\n"
304
+ " try:\n"
305
+ " value = integrate(strict_loads(raw))\n"
306
+ " sys.stdout.buffer.write(strict_dumps({'ok': True, 'value': value}))\n"
307
+ " except Exception as exc:\n"
308
+ " sys.stdout.buffer.write(strict_dumps({'ok': False, 'error': type(exc).__name__}))\n")
309
+ (artifact / "run.py").write_text(code, encoding="utf-8")
310
+ return hashlib.sha256(contract).hexdigest()
311
+
312
+
313
+ def _index_params(selected, side):
314
+ # Filled by verify from canonical AST data; no caller-controlled Python text.
315
+ return selected["source_parameters" if side == 0 else "target_parameters"]
316
+
317
+
318
+ def verify(roots, originals, saved: dict, cases: list[dict], schemas: tuple[dict, dict, dict],
319
+ out: Path, *, confirmed: bool) -> dict:
320
+ out.mkdir(parents=True, exist_ok=True)
321
+ archive = out / "prototype.zip"
322
+ archive.unlink(missing_ok=True)
323
+ report: dict[str, Any] = {"status": "failed", "verified": False, "tests": [],
324
+ "runtime_image": DOCKER_IMAGE}
325
+ try:
326
+ if not confirmed:
327
+ raise InspectionError("Explicit confirmation required")
328
+ for schema in schemas:
329
+ check_schema(schema)
330
+ _checked_cases(cases)
331
+ if sum(len(strict_dumps(c)) for c in cases) > 1024 * 1024:
332
+ raise InspectionError("Acceptance case byte limit exceeded")
333
+ for case in cases:
334
+ validate(case["input"], schemas[0])
335
+ if "expected" in case:
336
+ validate(case["expected"], schemas[2])
337
+ analyses = []
338
+ for root, original in zip(roots, originals, strict=True):
339
+ if isinstance(original, ClosureAnalysis):
340
+ current = inspect_v31(root, original.source, original.commit, fetched=True)
341
+ if current != original:
342
+ raise InspectionError("Pinned source changed before verification")
343
+ else:
344
+ current = _provenance(root, original)
345
+ analyses.append(current)
346
+ analyses = tuple(analyses)
347
+ proposal = propose(*analyses, *roots, saved["goal"])
348
+ canonical = select(proposal)
349
+ if saved != canonical or canonical["selected"] is None:
350
+ raise InspectionError("Plan changed or selector abstained")
351
+ selected = dict(canonical["selected"])
352
+ _validate_bindings(proposal, selected, schemas)
353
+ for side in ("source", "target"):
354
+ fn = _index(proposal, 0 if side == "source" else 1, selected[side])
355
+ selected[side + "_parameters"] = [(p["name"], p["kind"]) for p in fn["parameters"]]
356
+ with tempfile.TemporaryDirectory() as tmp:
357
+ artifact = Path(tmp) / "prototype"
358
+ contract_sha = _generate(roots, analyses, artifact, selected, schemas)
359
+ for index, case in enumerate(cases):
360
+ actual = _run_docker(artifact, case["input"])
361
+ passed = (actual == {"ok": False, "error": case["error"]} if "error" in case
362
+ else actual.get("ok") is True and actual.get("value") == case["expected"])
363
+ report["tests"].append({"index": index, "input": case["input"],
364
+ "actual": actual, "passed": passed, "backend": "docker"})
365
+ if not passed:
366
+ raise InspectionError(f"Acceptance case {index} failed")
367
+ report["files_sha256"] = _write_deterministic_zip(artifact, archive)
368
+ report["archive_sha256"] = hashlib.sha256(archive.read_bytes()).hexdigest()
369
+ report["sources_provenance"] = proposal["sources"]
370
+ report["contract_sha256"] = contract_sha
371
+ report["status"], report["verified"] = "passed", True
372
+ except Exception as exc:
373
+ archive.unlink(missing_ok=True)
374
+ report["error"] = f"{type(exc).__name__}: {exc}"
375
+ report.pop("files_sha256", None)
376
+ report.pop("archive_sha256", None)
377
+ (out / "report.json").write_text(json.dumps(report, indent=2, sort_keys=True), encoding="utf-8")
378
+ (out / "report.md").write_text("# StackLatch V3.1 validation\n\n" +
379
+ json.dumps(report, indent=2, sort_keys=True) + "\n", encoding="utf-8")
380
+ return report
@@ -0,0 +1,146 @@
1
+ """V3.1 pinned checkout and static manifest inspection for isolated closures.
2
+
3
+ Historical inspectors remain byte-exact. This path broadens only bounded
4
+ version-declaration parsing; it never installs dependencies or runs metadata.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ import hashlib
9
+ import json
10
+ import os
11
+ import re
12
+ import stat
13
+ import subprocess
14
+ import tomllib
15
+ import urllib.request
16
+ from dataclasses import dataclass
17
+ from pathlib import Path
18
+
19
+ from .core import Analysis, InspectionError, MAX_BYTES, MAX_FILES, SHA, validate_url
20
+
21
+ REQUIREMENT = re.compile(r"([A-Za-z0-9_.-]+)(?:(==|>=|<=|~=|>|<)([A-Za-z0-9_.+!-]+))?\Z")
22
+
23
+
24
+ @dataclass(frozen=True)
25
+ class ClosureAnalysis(Analysis):
26
+ """Explicit V3.1 inspection path; historical Analysis uses frozen rules."""
27
+ closure_opt_in: bool = True
28
+
29
+
30
+ def _dependencies(root: Path, manifests: list[str]) -> dict[str, str]:
31
+ found = {}
32
+ for manifest in manifests:
33
+ path = root / manifest
34
+ if path.name == "pyproject.toml":
35
+ try:
36
+ project = tomllib.loads(path.read_text(encoding="utf-8")).get("project", {})
37
+ declarations = project.get("dependencies", [])
38
+ except (ValueError, UnicodeError) as exc:
39
+ raise InspectionError("Invalid pyproject.toml") from exc
40
+ if not isinstance(declarations, list):
41
+ raise InspectionError("Invalid dependency list")
42
+ elif path.name == "requirements.txt":
43
+ # PEP 508 requirement lines permit a whitespace-delimited comment.
44
+ # Strip only that grammar; a '#' embedded in a URL or name still
45
+ # reaches the strict requirement matcher and is rejected.
46
+ declarations = [re.split(r"\s+#", line, maxsplit=1)[0].strip()
47
+ for line in path.read_text(encoding="utf-8").splitlines()
48
+ if line.strip() and not line.lstrip().startswith("#")]
49
+ else:
50
+ continue
51
+ for declaration in declarations:
52
+ if not isinstance(declaration, str) or len(declaration) > 128:
53
+ raise InspectionError("Invalid bounded dependency declaration")
54
+ match = REQUIREMENT.fullmatch(declaration)
55
+ if not match:
56
+ raise InspectionError(f"Unsupported dependency declaration: {declaration[:80]}")
57
+ name = match[1].lower().replace("_", "-")
58
+ version = (match[2] or "") + (match[3] or "") or "unbounded"
59
+ if name in found and found[name] != version:
60
+ raise InspectionError(f"Conflicting dependency declarations for {name}")
61
+ found[name] = version
62
+ return found
63
+
64
+
65
+ def inspect_v31(root: Path, source: str, commit: str, *, fetched: bool = False) -> Analysis:
66
+ if not SHA.fullmatch(commit) or root.is_symlink() or not root.is_dir():
67
+ raise InspectionError("Pinned commit and regular directory required")
68
+ root = root.resolve()
69
+ files, total = [], 0
70
+ for path in root.rglob("*"):
71
+ if ".git" in path.relative_to(root).parts or "__pycache__" in path.relative_to(root).parts or path.suffix == ".pyc":
72
+ continue
73
+ if path.is_symlink():
74
+ raise InspectionError("Symlinks are not supported")
75
+ if not path.is_file() or not stat.S_ISREG(path.stat().st_mode):
76
+ continue
77
+ total += path.stat().st_size
78
+ files.append(path.relative_to(root).as_posix())
79
+ if len(files) > MAX_FILES or total > MAX_BYTES:
80
+ raise InspectionError("Repository exceeds V1 file or byte limit")
81
+ files.sort()
82
+ manifests = [p for p in files if Path(p).name in
83
+ {"pyproject.toml", "requirements.txt", "package.json", "setup.py"}]
84
+ if not any(p.endswith(".py") for p in files):
85
+ raise InspectionError("Unsupported project: no Python source")
86
+ license_file = next((p for p in files if p.lower() in {"license", "license.md", "license.txt"}), None)
87
+ license_name = None
88
+ if license_file:
89
+ text = (root / license_file).read_text(encoding="utf-8", errors="replace")[:4000].lower()
90
+ license_name = "MIT-like (heuristic)" if all(x in text for x in
91
+ ("mit license", "permission is hereby granted, free of charge",
92
+ 'the software is provided "as is"')) and "gnu general public license" not in text else "unknown"
93
+ digest = hashlib.sha256()
94
+ per_file = {}
95
+ for name in files:
96
+ raw = (root / name).read_bytes()
97
+ per_file[name] = hashlib.sha256(raw).hexdigest()
98
+ digest.update(name.encode("utf-8") + b"\0" + raw)
99
+ return ClosureAnalysis(source, commit, files, "python", manifests, license_name,
100
+ [p for p in files if p.endswith(".py") and (p.startswith("src/") or p.count("/") == 0)],
101
+ _dependencies(root, manifests),
102
+ [p for p in files if p in {"main.py", "app.py", "run.py"}],
103
+ digest.hexdigest(), "fetched_git_commit" if fetched else "local_fixture", per_file)
104
+
105
+
106
+ def checkout_v31(url: str, commit: str, destination: Path) -> Analysis:
107
+ owner, repo = validate_url(url)
108
+ if not SHA.fullmatch(commit):
109
+ raise InspectionError("A full commit SHA is required")
110
+ headers = {"User-Agent": "StackLatch/0.1"}
111
+ token = os.getenv("STACKLATCH_GITHUB_TOKEN")
112
+ if token:
113
+ headers["Authorization"] = f"Bearer {token}"
114
+ with urllib.request.urlopen(urllib.request.Request(
115
+ f"https://api.github.com/repos/{owner}/{repo}", headers=headers), timeout=10) as response:
116
+ metadata = json.load(response)
117
+ if metadata.get("private") or metadata.get("size", 0) > 2048:
118
+ raise InspectionError("Only small public repositories are supported")
119
+ destination.mkdir(parents=True, exist_ok=False)
120
+ env = {k: v for k, v in os.environ.items()
121
+ if k != "STACKLATCH_GITHUB_TOKEN" and not k.startswith("GIT_")}
122
+ env.update({"GIT_TERMINAL_PROMPT": "0", "GIT_CONFIG_NOSYSTEM": "1",
123
+ "GIT_CONFIG_GLOBAL": os.devnull, "GIT_ALLOW_PROTOCOL": "https"})
124
+ commands = [["git", "init", "-q", str(destination)],
125
+ ["git", "-C", str(destination), "remote", "add", "origin",
126
+ f"https://github.com/{owner}/{repo}.git"],
127
+ ["git", "-C", str(destination), "fetch", "--depth=1", "origin", commit]]
128
+ try:
129
+ for command in commands:
130
+ subprocess.run(command, check=True, timeout=60, capture_output=True, text=True, env=env)
131
+ objects = subprocess.check_output(["git", "-C", str(destination), "count-objects", "-v"],
132
+ text=True, env=env)
133
+ sizes = dict(line.split(": ", 1) for line in objects.splitlines() if ": " in line)
134
+ if int(sizes.get("size-pack", 0)) + int(sizes.get("size", 0)) > 8192:
135
+ raise InspectionError("Downloaded Git objects exceed V1 limits")
136
+ subprocess.run(["git", "-C", str(destination), "checkout", "--detach", "FETCH_HEAD"],
137
+ check=True, timeout=60, capture_output=True, text=True, env=env)
138
+ actual = subprocess.check_output(["git", "-C", str(destination), "rev-parse", "HEAD"],
139
+ text=True, env=env).strip()
140
+ kind = subprocess.check_output(["git", "-C", str(destination), "cat-file", "-t", "HEAD"],
141
+ text=True, env=env).strip()
142
+ except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as exc:
143
+ raise InspectionError("Unable to fetch pinned commit") from exc
144
+ if actual != commit or kind != "commit":
145
+ raise InspectionError("Fetched commit differs from requested SHA")
146
+ return inspect_v31(destination, url, commit, fetched=True)