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/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+ """StackLatch Core: bounded repository analysis and integration."""
2
+ __version__ = "0.1.0a1"
stacklatch/cli.py ADDED
@@ -0,0 +1,76 @@
1
+ """Explicit Core-only command line interface: proposals and confirmed verification."""
2
+ from __future__ import annotations
3
+
4
+ import argparse
5
+ import json
6
+ import logging
7
+ import shutil
8
+ import tempfile
9
+ from pathlib import Path
10
+
11
+ from .v24_contract import check_schema, strict_dumps, strict_loads
12
+ from .v31 import propose, select, verify
13
+ from .v31_checkout import checkout_v31
14
+
15
+ log = logging.getLogger(__name__)
16
+
17
+
18
+ def main() -> None:
19
+ parser = argparse.ArgumentParser(description="StackLatch Core: pinned Python repository integration (alpha)")
20
+ sub = parser.add_subparsers(dest="action", required=True)
21
+ planning = sub.add_parser("plan", help="Fetch pinned public repositories and write an unverified proposal")
22
+ for flag in ("left", "right", "left-sha", "right-sha", "goal", "out"):
23
+ planning.add_argument("--" + flag, required=True)
24
+ checking = sub.add_parser("verify", help="Run independently supplied cases inside restricted Docker")
25
+ for flag in ("plan", "cases-file", "input-schema", "intermediate-schema", "output-schema", "out"):
26
+ checking.add_argument("--" + flag, required=True)
27
+ checking.add_argument("--confirm", action="store_true", required=True, help="Confirm execution of pinned repository code in Docker")
28
+ args = parser.parse_args()
29
+ logging.basicConfig(level=logging.INFO, format="%(message)s")
30
+ target = Path(args.out)
31
+ target.mkdir(parents=True, exist_ok=True)
32
+ if args.action == "plan":
33
+ try:
34
+ with tempfile.TemporaryDirectory() as temp:
35
+ roots = (Path(temp) / "left", Path(temp) / "right")
36
+ analyses = (checkout_v31(args.left, args.left_sha, roots[0]),
37
+ checkout_v31(args.right, args.right_sha, roots[1]))
38
+ result = select(propose(*analyses, *roots, args.goal))
39
+ (target / "plan.json").write_bytes(strict_dumps(result))
40
+ log.info(json.dumps({"status": "proposed", "verified": False,
41
+ "abstained": result["abstained"],
42
+ "reason": result["abstention_reason"]}))
43
+ except Exception as exc:
44
+ (target / "plan.json").unlink(missing_ok=True)
45
+ log.error("Planning failed: %s: %s", type(exc).__name__, exc)
46
+ raise SystemExit(1) from None
47
+ return
48
+ try:
49
+ if shutil.which("docker") is None:
50
+ raise RuntimeError("Docker is required for restricted verification; install Docker and retry on an authorized host")
51
+ path = Path(args.plan)
52
+ if path.stat().st_size > 1024 * 1024:
53
+ raise ValueError("Plan size limit exceeded")
54
+ saved = strict_loads(path.read_bytes())
55
+ schemas = tuple(check_schema(strict_loads(Path(p).read_bytes())) for p in
56
+ (args.input_schema, args.intermediate_schema, args.output_schema))
57
+ cases = strict_loads(Path(args.cases_file).read_bytes())
58
+ if saved.get("schema_version") != 31 or saved.get("abstained"):
59
+ raise ValueError("Selected V3.1 plan required")
60
+ with tempfile.TemporaryDirectory() as temp:
61
+ roots = (Path(temp) / "left", Path(temp) / "right")
62
+ analyses = tuple(checkout_v31(s["url"], s["commit"], root)
63
+ for s, root in zip(saved["sources"], roots, strict=True))
64
+ result = verify(roots, analyses, saved, cases, schemas, target, confirmed=args.confirm)
65
+ except Exception as exc:
66
+ (target / "prototype.zip").unlink(missing_ok=True)
67
+ result = {"status": "failed", "verified": False,
68
+ "error": f"{type(exc).__name__}: {exc}"}
69
+ (target / "report.json").write_text(json.dumps(result, indent=2), encoding="utf-8")
70
+ log.info(json.dumps({"status": result["status"], "verified": result["verified"]}))
71
+ if not result["verified"]:
72
+ raise SystemExit(1)
73
+
74
+
75
+ if __name__ == "__main__":
76
+ main()
stacklatch/core.py ADDED
@@ -0,0 +1,386 @@
1
+ """Bounded repository inspection, planning and verified sample integration."""
2
+ from __future__ import annotations
3
+
4
+ import hashlib
5
+ import json
6
+ import os
7
+ import re
8
+ import shutil
9
+ import subprocess
10
+ import tempfile
11
+ import tomllib
12
+ import uuid
13
+ import stat
14
+ import urllib.parse
15
+ import urllib.request
16
+ import zipfile
17
+ from dataclasses import asdict, dataclass
18
+ from pathlib import Path
19
+ from typing import Any
20
+
21
+ MAX_BYTES = 2_000_000
22
+ MAX_FILES = 300
23
+ DOCKER_IMAGE = "python:3.12-slim@sha256:b18b261e1b4974b4f321451ad5251ded85abb22c2b8b262da527dda1d031ab30"
24
+ URL = re.compile(r"https://github\.com/([A-Za-z0-9_.-]+)/([A-Za-z0-9_.-]+?)(?:\.git)?/?$")
25
+ SHA = re.compile(r"[0-9a-f]{40}")
26
+
27
+
28
+ class InspectionError(ValueError):
29
+ pass
30
+
31
+
32
+ @dataclass(frozen=True)
33
+ class Analysis:
34
+ source: str
35
+ commit: str
36
+ files: list[str]
37
+ language: str
38
+ manifests: list[str]
39
+ license: str | None
40
+ api: list[str]
41
+ dependencies: dict[str, str]
42
+ entry_points: list[str]
43
+ content_sha256: str
44
+ provenance: str
45
+ file_sha256: dict[str, str]
46
+
47
+
48
+ def validate_archive(archive: Path) -> list[str]:
49
+ """Validate a source ZIP before extraction. Never follow archive links."""
50
+ names: list[str] = []
51
+ total = 0
52
+ with zipfile.ZipFile(archive) as z:
53
+ for item in z.infolist():
54
+ name = item.filename
55
+ parts = Path(name).parts
56
+ if (not name or name.startswith(("/", "\\")) or "\\" in name
57
+ or ".." in parts or any(p in ("", ".") for p in parts)
58
+ or re.match(r"^[A-Za-z]:", name)):
59
+ raise InspectionError("Unsafe archive path")
60
+ if (item.external_attr >> 16) & 0o170000 == 0o120000:
61
+ raise InspectionError("Archive symlinks are not supported")
62
+ if not item.is_dir():
63
+ names.append(name)
64
+ total += item.file_size
65
+ if len(names) > MAX_FILES or total > MAX_BYTES:
66
+ raise InspectionError("Archive exceeds V1 limits")
67
+ if len(names) != len(set(names)):
68
+ raise InspectionError("Duplicate archive entries")
69
+ return names
70
+
71
+
72
+ def _dependencies(root: Path, manifests: list[str]) -> dict[str, str]:
73
+ found: dict[str, str] = {}
74
+ for manifest in manifests:
75
+ path = root / manifest
76
+ if path.name == "pyproject.toml":
77
+ try:
78
+ declarations = tomllib.loads(path.read_text(encoding="utf-8")).get("project", {}).get("dependencies", [])
79
+ except (ValueError, UnicodeError) as exc:
80
+ raise InspectionError("Invalid pyproject.toml") from exc
81
+ elif path.name == "requirements.txt":
82
+ declarations = [line.strip() for line in path.read_text(encoding="utf-8").splitlines()
83
+ if line.strip() and not line.lstrip().startswith("#")]
84
+ else:
85
+ continue
86
+ for declaration in declarations:
87
+ match = re.fullmatch(r"([A-Za-z0-9_.-]+)(?:==([A-Za-z0-9_.+!-]+))?", declaration)
88
+ if not match:
89
+ raise InspectionError(f"Unsupported dependency declaration: {declaration[:80]}")
90
+ name, version = match.group(1).lower().replace("_", "-"), match.group(2) or "unbounded"
91
+ if name in found and found[name] != version:
92
+ raise InspectionError(f"Conflicting dependency declarations for {name}")
93
+ found[name] = version
94
+ return found
95
+
96
+
97
+ def validate_url(url: str) -> tuple[str, str]:
98
+ parsed = urllib.parse.urlsplit(url)
99
+ if parsed.scheme != "https" or parsed.hostname != "github.com" or parsed.port or parsed.query or parsed.fragment or parsed.username:
100
+ raise InspectionError("Only public https://github.com/owner/repository URLs are accepted")
101
+ match = URL.fullmatch(url)
102
+ if not match or match.group(1).startswith(".") or match.group(2).startswith("."):
103
+ raise InspectionError("Invalid GitHub repository URL")
104
+ return match.group(1), match.group(2)
105
+
106
+
107
+ def inspect_tree(root: Path, source: str, commit: str, *, _fetched_git: bool = False,
108
+ v21: bool = False, v22: bool = False) -> Analysis:
109
+ if not SHA.fullmatch(commit):
110
+ raise InspectionError("A full pinned 40-character commit SHA is required")
111
+ if root.is_symlink():
112
+ raise InspectionError("Symlinks are not supported")
113
+ root = root.resolve()
114
+ if not root.is_dir():
115
+ raise InspectionError("Repository directory missing")
116
+ files: list[str] = []
117
+ total = 0
118
+ for path in root.rglob("*"):
119
+ if ".git" in path.relative_to(root).parts or "__pycache__" in path.relative_to(root).parts or path.suffix == ".pyc":
120
+ continue
121
+ if path.is_symlink():
122
+ raise InspectionError("Symlinks are not supported")
123
+ if not path.is_file():
124
+ continue
125
+ total += path.stat().st_size
126
+ files.append(path.relative_to(root).as_posix())
127
+ if len(files) > MAX_FILES or total > MAX_BYTES:
128
+ raise InspectionError("Repository exceeds V1 file or byte limit")
129
+ files.sort()
130
+ manifests = [p for p in files if Path(p).name in
131
+ ({"pyproject.toml", "requirements.txt", "package.json", "setup.py"} if v21 else
132
+ {"pyproject.toml", "requirements.txt", "package.json"})]
133
+ if not manifests and not (v22 and any(p.endswith('.py') for p in files)):
134
+ raise InspectionError("Unsupported project: no recognized manifest")
135
+ language = "python" if (v22 and any(p.endswith('.py') for p in files)) or any(Path(p).name in {"pyproject.toml", "requirements.txt", "setup.py"} for p in manifests) else "javascript"
136
+ license_file = next((p for p in files if p.lower() in {"license", "license.md", "license.txt"}), None)
137
+ license_name = None
138
+ if license_file:
139
+ content = (root / license_file).read_text(encoding="utf-8", errors="replace")[:4000].lower()
140
+ license_name = ("MIT-like (heuristic)" if all(marker in content for marker in
141
+ (("permission is hereby granted, free of charge", 'the software is provided "as is"')
142
+ if v21 else ("mit license", "permission is hereby granted, free of charge", 'the software is provided "as is"')))
143
+ and "gnu general public license" not in content else "unknown")
144
+ api = ([p for p in files if p.endswith(".py") and Path(p).name != "setup.py"
145
+ and not any(part in {"tests", "test", ".github"} for part in Path(p).parts)] if v21 else
146
+ [p for p in files if p.endswith(".py") and (p.startswith("src/") or p.count("/") == 0)])
147
+ entries = [p for p in files if p in {"main.py", "app.py", "run.py"}]
148
+ digest = hashlib.sha256()
149
+ per_file = {}
150
+ for name in files:
151
+ per_file[name] = hashlib.sha256((root / name).read_bytes()).hexdigest()
152
+ digest.update(name.encode("utf-8") + b"\0")
153
+ digest.update((root / name).read_bytes())
154
+ return Analysis(source, commit, files, language, manifests, license_name, api,
155
+ _dependencies(root, manifests), entries, digest.hexdigest(),
156
+ "fetched_git_commit" if _fetched_git else "local_fixture", per_file)
157
+
158
+
159
+ def checkout_public(url: str, commit: str, destination: Path, *, v21: bool = False,
160
+ v22: bool = False) -> Analysis:
161
+ owner, repo = validate_url(url)
162
+ if not SHA.fullmatch(commit):
163
+ raise InspectionError("A full commit SHA is required")
164
+ # Metadata preflight is an optimization; size is also checked on downloaded files.
165
+ headers = {"User-Agent": "StackLatch/0.1"}
166
+ # Optional read-only GitHub API authentication avoids the public CI rate
167
+ # limit. Never include credentials in a Git URL, command line or report.
168
+ token = os.getenv("STACKLATCH_GITHUB_TOKEN")
169
+ if token:
170
+ headers["Authorization"] = f"Bearer {token}"
171
+ request = urllib.request.Request(f"https://api.github.com/repos/{owner}/{repo}", headers=headers)
172
+ with urllib.request.urlopen(request, timeout=10) as response:
173
+ metadata = json.load(response)
174
+ if metadata.get("private") or metadata.get("size", 0) > 2048:
175
+ raise InspectionError("Only small public repositories are supported")
176
+ destination.mkdir(parents=True, exist_ok=False)
177
+ git_url = f"https://github.com/{owner}/{repo}.git"
178
+ commands = [["git", "init", "-q", str(destination)],
179
+ ["git", "-C", str(destination), "remote", "add", "origin", git_url],
180
+ ["git", "-C", str(destination), "fetch", "--depth=1", "origin", commit]]
181
+ git_env = {k: v for k, v in os.environ.items()
182
+ if k != "STACKLATCH_GITHUB_TOKEN" and not k.startswith("GIT_")}
183
+ git_env.update({"GIT_TERMINAL_PROMPT": "0", "GIT_CONFIG_NOSYSTEM": "1",
184
+ "GIT_CONFIG_GLOBAL": os.devnull, "GIT_ALLOW_PROTOCOL": "https"})
185
+ try:
186
+ for command in commands:
187
+ subprocess.run(command, check=True, timeout=60, capture_output=True, text=True,
188
+ env=git_env)
189
+ objects = subprocess.check_output(["git", "-C", str(destination), "count-objects", "-v"], text=True, env=git_env)
190
+ sizes = dict(line.split(": ", 1) for line in objects.splitlines() if ": " in line)
191
+ if int(sizes.get("size-pack", 0)) + int(sizes.get("size", 0)) > 8192:
192
+ raise InspectionError("Downloaded Git objects exceed V1 limits")
193
+ subprocess.run(["git", "-C", str(destination), "checkout", "--detach", "FETCH_HEAD"],
194
+ check=True, timeout=60, capture_output=True, text=True,
195
+ env=git_env)
196
+ except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as exc:
197
+ raise InspectionError("Unable to fetch pinned commit") from exc
198
+ actual = subprocess.check_output(["git", "-C", str(destination), "rev-parse", "HEAD"], text=True, env=git_env).strip()
199
+ if actual != commit:
200
+ raise InspectionError("Fetched commit differs from requested SHA")
201
+ if subprocess.check_output(["git", "-C", str(destination), "cat-file", "-t", "HEAD"], text=True, env=git_env).strip() != "commit":
202
+ raise InspectionError("Fetched Git object is not a commit")
203
+ return inspect_tree(destination, url, commit, _fetched_git=True, v21=v21 or v22, v22=v22)
204
+
205
+
206
+ def plan(left: Analysis, right: Analysis, goal: str) -> dict:
207
+ if not goal.strip():
208
+ raise ValueError("Integration goal is required")
209
+ problems = []
210
+ if left.language != "python" or right.language != "python":
211
+ problems.append("V1 generator supports Python-to-Python only")
212
+ if left.license != "MIT-like (heuristic)" or right.license != "MIT-like (heuristic)":
213
+ problems.append("Licenses require human review; only MIT-like sample text is eligible")
214
+ for name in left.dependencies.keys() & right.dependencies.keys():
215
+ if left.dependencies[name] != right.dependencies[name]:
216
+ problems.append(f"Conflicting dependency {name}: {left.dependencies[name]} vs {right.dependencies[name]}")
217
+ if left.dependencies or right.dependencies:
218
+ problems.append("External dependencies are not installed in the offline V1 runtime")
219
+ return {"goal": goal, "sources": [asdict(left), asdict(right)],
220
+ "method": "Python function adapter", "interfaces": [left.api, right.api],
221
+ "license_note": "Heuristic MIT-like text detection is not a legal compatibility determination",
222
+ "proposed": True, "verified": False,
223
+ "compatibility_problems": problems}
224
+
225
+
226
+ def _copy_checked(root: Path, relative: str, target: Path, expected_hash: str) -> None:
227
+ """Open each component without following links and hash exactly the copied bytes."""
228
+ dir_fd = os.open(root, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
229
+ try:
230
+ for component in Path(relative).parts[:-1]:
231
+ next_fd = os.open(component, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW, dir_fd=dir_fd)
232
+ os.close(dir_fd)
233
+ dir_fd = next_fd
234
+ fd = os.open(Path(relative).name, os.O_RDONLY | os.O_NOFOLLOW, dir_fd=dir_fd)
235
+ try:
236
+ info = os.fstat(fd)
237
+ if not stat.S_ISREG(info.st_mode) or info.st_size > MAX_BYTES:
238
+ raise InspectionError("Source is not a bounded regular file")
239
+ with os.fdopen(fd, "rb", closefd=False) as stream:
240
+ data = stream.read(MAX_BYTES + 1)
241
+ if len(data) > MAX_BYTES or hashlib.sha256(data).hexdigest() != expected_hash:
242
+ raise InspectionError("Source changed during staging")
243
+ target.parent.mkdir(parents=True, exist_ok=True)
244
+ target.write_bytes(data)
245
+ finally:
246
+ os.close(fd)
247
+ finally:
248
+ os.close(dir_fd)
249
+
250
+
251
+ def sample_integration(left: Path, right: Path, target: Path, integration_plan: dict) -> Path:
252
+ """Generate and test the narrowly supported text-normalizer + token-counter pair."""
253
+ if integration_plan["compatibility_problems"]:
254
+ raise InspectionError("Unsupported or license-unverified source pair")
255
+ # Recheck the inputs immediately before copying; caller-supplied plans are not authority.
256
+ analyses = [inspect_tree(p, str(p), "0" * 40) for p in (left, right)]
257
+ if any(actual.content_sha256 != expected["content_sha256"]
258
+ for actual, expected in zip(analyses, integration_plan["sources"], strict=True)):
259
+ raise InspectionError("Source changed after analysis")
260
+ if plan(*analyses, integration_plan["goal"])["compatibility_problems"]:
261
+ raise InspectionError("Sources are no longer eligible for offline integration")
262
+ a = left / "src" / "normalizer.py"
263
+ b = right / "src" / "counter.py"
264
+ if not a.is_file() or not b.is_file():
265
+ raise InspectionError("V1 generator requires normalizer.py and counter.py interfaces")
266
+ for source in (a, b):
267
+ if source.is_symlink():
268
+ raise InspectionError("Symlinks are not supported")
269
+ target.mkdir(parents=True, exist_ok=False)
270
+ _copy_checked(left, "src/normalizer.py", target / "normalizer" / "normalizer.py",
271
+ integration_plan["sources"][0]["file_sha256"]["src/normalizer.py"])
272
+ _copy_checked(right, "src/counter.py", target / "counter" / "counter.py",
273
+ integration_plan["sources"][1]["file_sha256"]["src/counter.py"])
274
+ glue = target / "run.py"
275
+ glue.write_text("from normalizer.normalizer import normalize\nfrom counter.counter import count_words\n\ndef analyze(text: str) -> dict:\n clean = normalize(text)\n return {'normalized': clean, 'words': count_words(clean)}\n\nif __name__ == '__main__':\n import json, sys\n print(json.dumps(analyze(sys.argv[1])))\n", encoding="utf-8")
276
+ (target / "test_integration.py").write_text("from run import analyze\n\ndef test_pipeline():\n assert analyze(' Hello, WORLD! ') == {'normalized': 'hello, world!', 'words': 2}\n", encoding="utf-8")
277
+ return glue
278
+
279
+
280
+ def _write_deterministic_zip(artifact: Path, archive: Path) -> dict[str, str]:
281
+ hashes: dict[str, str] = {}
282
+ with zipfile.ZipFile(archive, "w") as z:
283
+ for path in sorted(p for p in artifact.rglob("*") if p.is_file()):
284
+ name = path.relative_to(artifact).as_posix()
285
+ content = path.read_bytes()
286
+ hashes[name] = hashlib.sha256(content).hexdigest()
287
+ info = zipfile.ZipInfo(name, date_time=(1980, 1, 1, 0, 0, 0))
288
+ info.compress_type = zipfile.ZIP_DEFLATED
289
+ info.create_system = 3
290
+ info.external_attr = 0o100644 << 16
291
+ z.writestr(info, content)
292
+ if validate_archive(archive) != sorted(hashes):
293
+ raise InspectionError("Generated ZIP failed entry validation")
294
+ with zipfile.ZipFile(archive) as z:
295
+ if any(hashlib.sha256(z.read(name)).hexdigest() != digest for name, digest in hashes.items()):
296
+ raise InspectionError("Generated ZIP content hash mismatch")
297
+ return hashes
298
+
299
+
300
+ def _sandbox_command(artifact: Path) -> tuple[list[str], str]:
301
+ if shutil.which("docker"):
302
+ return (["docker", "run", "--rm", "--name", f"stacklatch-{uuid.uuid4().hex}",
303
+ "--network", "none", "--read-only", "--pids-limit", "16",
304
+ "--memory", "128m", "--memory-swap", "128m", "--cpus", "1",
305
+ "--ulimit", "cpu=3:4", "--ulimit", "nproc=32:32", "--cap-drop", "ALL",
306
+ "--security-opt", "no-new-privileges", "--user", "65534:65534",
307
+ "--mount", f"type=bind,src={artifact},dst=/app,readonly", "--workdir", "/app",
308
+ "--entrypoint", "python", DOCKER_IMAGE, "-B", "run.py", " Hello, WORLD! "], "docker")
309
+ if shutil.which("bwrap"):
310
+ paths = [p for p in ("/usr", "/lib", "/lib64", "/bin") if Path(p).exists()]
311
+ binds = [arg for p in paths for arg in ("--ro-bind", p, p)]
312
+ return (["bwrap", "--unshare-all", "--die-with-parent", *binds,
313
+ "--ro-bind", str(artifact), "/app", "--tmpfs", "/tmp", "--proc", "/proc",
314
+ "--chdir", "/app", "/usr/bin/python3", "-B", "run.py", " Hello, WORLD! "], "bubblewrap")
315
+ raise InspectionError("Docker or bubblewrap is required for isolated execution")
316
+
317
+
318
+ def verified_package(left: Path, right: Path, out: Path, integration_plan: dict) -> dict:
319
+ out.mkdir(parents=True, exist_ok=True)
320
+ (out / "prototype.zip").unlink(missing_ok=True)
321
+ report: dict[str, Any] = {"status": "failed", "verified": False, "plan": integration_plan, "tests": []}
322
+ with tempfile.TemporaryDirectory() as temp:
323
+ artifact = Path(temp) / "prototype"
324
+ try:
325
+ sample_integration(left, right, artifact, integration_plan)
326
+ command, backend = _sandbox_command(artifact)
327
+ import resource
328
+ def limits():
329
+ resource.setrlimit(resource.RLIMIT_FSIZE, (2 * 1024 * 1024, 2 * 1024 * 1024))
330
+ if backend == "bubblewrap":
331
+ resource.setrlimit(resource.RLIMIT_CPU, (5, 5))
332
+ resource.setrlimit(resource.RLIMIT_AS, (256 * 1024 * 1024, 256 * 1024 * 1024))
333
+ timeout = 30 if backend == "docker" else 10
334
+ try:
335
+ result = subprocess.run(command, text=True, capture_output=True, timeout=timeout, preexec_fn=limits)
336
+ except subprocess.TimeoutExpired:
337
+ if backend == "docker":
338
+ subprocess.run(["docker", "rm", "-f", command[4]], capture_output=True, timeout=10)
339
+ raise
340
+ report["tests"].append({"command": "sandboxed sample integration", "backend": backend,
341
+ "exit_code": result.returncode,
342
+ "stdout": result.stdout[:1000], "stderr": result.stderr[:1000]})
343
+ if result.returncode != 0 or json.loads(result.stdout) != {"normalized": "hello, world!", "words": 2}:
344
+ raise InspectionError("Execution failed or produced incorrect output")
345
+ test_command = command[:-2] + ["-B", "-c", "from test_integration import test_pipeline; test_pipeline()"]
346
+ test_result = subprocess.run(test_command, text=True, capture_output=True, timeout=timeout, preexec_fn=limits)
347
+ report["tests"].append({"command": "sandboxed integration acceptance test", "backend": backend,
348
+ "exit_code": test_result.returncode, "stdout": test_result.stdout[:1000],
349
+ "stderr": test_result.stderr[:1000]})
350
+ if test_result.returncode != 0:
351
+ raise InspectionError("Sandboxed integration acceptance test failed")
352
+ archive = out / "prototype.zip"
353
+ report["archive_files_sha256"] = _write_deterministic_zip(artifact, archive)
354
+ report["archive_sha256"] = hashlib.sha256(archive.read_bytes()).hexdigest()
355
+ report["sources_provenance"] = [
356
+ {"kind": source["provenance"], "git_commit_verified": source["provenance"] == "fetched_git_commit",
357
+ "git_commit": source["commit"] if source["provenance"] == "fetched_git_commit" else None,
358
+ "fixture_sha_placeholder": source["commit"] if source["provenance"] == "local_fixture" else None,
359
+ "content_sha256": source["content_sha256"]} for source in integration_plan["sources"]]
360
+ report["runtime_image"] = DOCKER_IMAGE if backend == "docker" else "host bubblewrap Python"
361
+ report["status"] = "passed"
362
+ report["verified"] = True
363
+ except Exception as exc:
364
+ report["error"] = f"{type(exc).__name__}: {exc}"
365
+ report["status"] = "failed"
366
+ report["verified"] = False
367
+ (out / "prototype.zip").unlink(missing_ok=True)
368
+ report.pop("archive_sha256", None)
369
+ report.pop("archive_files_sha256", None)
370
+ (out / "report.json").write_text(json.dumps(report, indent=2), encoding="utf-8")
371
+ lines = ["# StackLatch validation", "", f"Status: {report['status']}",
372
+ f"Verified: {report['verified']}", f"Runtime: {report.get('runtime_image', 'unavailable')}",
373
+ f"Archive SHA-256: {report.get('archive_sha256', 'none')}", "",
374
+ "## Execution evidence", ""]
375
+ for test in report["tests"]:
376
+ lines.extend([f"- {test['command']}: exit {test['exit_code']}; backend {test['backend']}",
377
+ f" - stdout: `{test['stdout'].strip()[:200]}`",
378
+ f" - stderr: `{test['stderr'].strip()[:200]}`"])
379
+ lines.extend(["", "## Sources", ""])
380
+ for source in report.get("sources_provenance", []):
381
+ lines.append(f"- {source['kind']}: Git commit verified = {source['git_commit_verified']}; "
382
+ f"content SHA-256 = {source['content_sha256']}")
383
+ if report.get("error"):
384
+ lines.extend(["", f"Error: {report['error']}"])
385
+ (out / "report.md").write_text("\n".join(lines) + "\n", encoding="utf-8")
386
+ return report