Files
Strata/tools/test_check_harness_bundle.py
Niko1221 3766287fca Release gate: check_harness_bundle.py reads the Harness BUNDLE.json out of an installer and fails unless it is the pinned one
For a staged folder, an install folder, BUNDLE.json, a .tar.gz / .zip (the macOS Strata.app archive), an AppImage (unsquashfs) or a setup.exe / .dmg (7-Zip).
Fails unless commit, version and builtAt are right, the patch ids are exactly the pinned three, package / file / afterSha256 equal the pin's, the platform
is the installer's, and the patched files inside hash to afterSha256 (BUNDLE.json is a record, the file is the fact). A target it cannot read fails.
Run on the real 0.2.24 files it fails the Windows install (commit null, two patches missing, two files unpatched) and passes the macOS app archive.
tools/test_check_harness_bundle.py covers the folder, tar, zip, AppImage (when squashfs-tools is there) and the failure cases.
2026-12-02 14:22:05 +01:00

216 lines
10 KiB
Python

"""tools/packaging/check_harness_bundle.py: the release gate that every installer ships the pinned Harness with all three Strata patches."""
import hashlib
import io
import json
import os
import shutil
import struct
import subprocess
import sys
import tarfile
import tempfile
import unittest
import zipfile
from pathlib import Path
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "packaging"))
import check_harness_bundle as gate # noqa: E402
REPO = Path(__file__).resolve().parent.parent
COMMIT = "571902028a75c64c1d767f4ae5fd099485f0ddf0"
FILES = { # patched file -> bytes of the patched version (a stand-in; the test pin is built from these)
"node_modules/@earendil-works/pi-ai/dist/api/simple-options.js": b"// patched pi-ai\n",
"node_modules/@deepseek-ai/dsh-tool-fs-search/lib/index.js": b"// patched search\n",
"node_modules/@deepseek-ai/dsh-mcp-client/lib/index.js": b"// patched mcp\n",
}
IDS = ["strata-pi-ai-context-safety-v2", "strata-fs-search-partial-v1", "strata-mcp-session-reconnect-v1"]
PKGS = ["@earendil-works/pi-ai", "@deepseek-ai/dsh-tool-fs-search", "@deepseek-ai/dsh-mcp-client"]
def sha(b):
return hashlib.sha256(b).hexdigest()
def pin():
return {"component": "deepseek-harness", "version": "0.2.1-alpha.3", "commit": COMMIT,
"patches": [{"id": i, "package": p, "file": f, "afterSha256": sha(FILES[f])} for i, p, f in zip(IDS, PKGS, FILES)]}
def bundle(platform="win32", **changes):
b = {"component": "deepseek-harness", "version": "0.2.1-alpha.3", "commit": COMMIT, "platform": platform, "builtAt": "2026-12-02T01:31:59.504Z",
"patches": [{"id": p["id"], "package": p["package"], "file": p["file"], "afterSha256": p["afterSha256"]} for p in pin()["patches"]]}
b.update(changes)
return b
def write_tree(root, b, files=FILES):
root.mkdir(parents=True, exist_ok=True)
(root / "BUNDLE.json").write_text(json.dumps(b), encoding="utf-8")
for rel, data in files.items():
(root / rel).parent.mkdir(parents=True, exist_ok=True)
(root / rel).write_bytes(data)
class GateTests(unittest.TestCase):
def setUp(self):
self.tmp = Path(tempfile.mkdtemp(prefix="strata-harness-gate-"))
self.addCleanup(shutil.rmtree, self.tmp, ignore_errors=True)
self.pin = pin()
self.pin_file = self.tmp / "pin.json"
self.pin_file.write_text(json.dumps(self.pin), encoding="utf-8")
def run_gate(self, *targets, extra=()):
out = io.StringIO()
real = sys.stdout
sys.stdout = out
try:
code = gate.main(["--pin", str(self.pin_file), *extra, *targets])
finally:
sys.stdout = real
return code, out.getvalue()
def test_a_staged_folder_with_everything_passes(self):
write_tree(self.tmp / "h", bundle())
code, text = self.run_gate(str(self.tmp / "h"))
self.assertEqual(code, 0, text)
self.assertIn("HARNESS GATE OK", text)
def test_an_install_folder_with_harness_inside_passes(self):
write_tree(self.tmp / "app" / "harness", bundle())
self.assertEqual(self.run_gate(str(self.tmp / "app"))[0], 0)
def test_the_0224_windows_bundle_fails_three_ways(self):
# commit null, one patch only (what the 0.2.24 setup.exe carried), the two other files unpatched
b = bundle(commit=None, builtAt="2026-10-16T17:45:39.441Z", patches=bundle()["patches"][:1])
old = dict(FILES)
old["node_modules/@deepseek-ai/dsh-tool-fs-search/lib/index.js"] = b"// original search\n"
old["node_modules/@deepseek-ai/dsh-mcp-client/lib/index.js"] = b"// original mcp\n"
write_tree(self.tmp / "h", b, old)
code, text = self.run_gate(str(self.tmp / "h"))
self.assertEqual(code, 1)
self.assertIn("commit None", text)
self.assertIn("strata-fs-search-partial-v1 is missing", text)
self.assertIn("strata-mcp-session-reconnect-v1 is missing", text)
self.assertIn("dsh-mcp-client/lib/index.js hashes to", text)
def test_a_bundle_json_that_claims_the_patch_but_a_file_that_is_not_patched_fails(self):
files = dict(FILES)
files["node_modules/@deepseek-ai/dsh-mcp-client/lib/index.js"] = b"// original mcp\n"
write_tree(self.tmp / "h", bundle(), files)
code, text = self.run_gate(str(self.tmp / "h"))
self.assertEqual(code, 1)
self.assertIn("patched file node_modules/@deepseek-ai/dsh-mcp-client/lib/index.js hashes to", text)
self.assertEqual(self.run_gate(str(self.tmp / "h"), extra=["--no-files"])[0], 0) # the record alone is what --no-files trusts
def test_other_commit_version_unknown_patch_missing_builtat_and_wrong_platform_each_fail(self):
cases = {
"commit": bundle(commit="a" * 40),
"version": bundle(version="0.2.2"),
"builtAt": bundle(builtAt=None),
"unknown patch": bundle(patches=bundle()["patches"] + [{"id": "strata-extra-v1", "package": "x", "file": "x", "afterSha256": "0" * 64}]),
"patch hash": bundle(patches=[dict(bundle()["patches"][0], afterSha256="0" * 64)] + bundle()["patches"][1:]),
"empty patches": bundle(patches=[]),
"no patches key": {k: v for k, v in bundle().items() if k != "patches"},
}
for name, b in cases.items():
with self.subTest(name):
write_tree(self.tmp / name.replace(" ", "_"), b)
self.assertEqual(self.run_gate(str(self.tmp / name.replace(" ", "_")))[0], 1)
write_tree(self.tmp / "plat", bundle(platform="linux"))
self.assertEqual(self.run_gate(str(self.tmp / "plat"), extra=["--platform", "win32"])[0], 1)
self.assertEqual(self.run_gate(str(self.tmp / "plat"), extra=["--platform", "linux"])[0], 0)
def test_the_platform_follows_the_installer_name(self):
self.assertEqual(gate.expected_platform("Strata-0.2.25-windows-x64-setup.exe"), "win32")
self.assertEqual(gate.expected_platform("Strata-0.2.25-linux-x86_64.AppImage"), "linux")
self.assertEqual(gate.expected_platform("Strata-0.2.25-macos-arm64.dmg"), "darwin")
self.assertEqual(gate.expected_platform("Strata.app.tar.gz"), "darwin")
self.assertIsNone(gate.expected_platform("folder"))
def test_the_macos_app_archive_is_read_and_checked_as_darwin(self):
stage = self.tmp / "mac"
write_tree(stage / "Strata.app" / "Contents" / "Resources" / "harness", bundle("darwin"))
archive = self.tmp / "Strata.app.tar.gz"
with tarfile.open(archive, "w:gz") as tf:
tf.add(stage / "Strata.app", arcname="Strata.app")
code, text = self.run_gate(str(archive))
self.assertEqual(code, 0, text)
self.assertIn("darwin", text)
write_tree(stage / "bad" / "Strata.app" / "Contents" / "Resources" / "harness", bundle("win32"))
bad = self.tmp / "Strata-bad.app.tar.gz"
with tarfile.open(bad, "w:gz") as tf:
tf.add(stage / "bad" / "Strata.app", arcname="Strata.app")
self.assertEqual(self.run_gate(str(bad))[0], 1)
def test_a_zip_install_is_read(self):
stage = self.tmp / "z"
write_tree(stage / "harness", bundle())
archive = self.tmp / "install.zip"
with zipfile.ZipFile(archive, "w") as zf:
for p in (stage).rglob("*"):
if p.is_file():
zf.write(p, p.relative_to(stage).as_posix())
self.assertEqual(self.run_gate(str(archive))[0], 0)
def test_something_that_cannot_be_read_fails_and_is_never_skipped(self):
for name in ("missing.exe", "nothing"):
code, text = self.run_gate(str(self.tmp / name))
self.assertEqual(code, 1)
self.assertIn("cannot read", text)
(self.tmp / "empty").mkdir()
self.assertEqual(self.run_gate(str(self.tmp / "empty"))[0], 1)
(self.tmp / "broken.json").write_text("{", encoding="utf-8")
(self.tmp / "BUNDLE.json").write_text("{", encoding="utf-8")
self.assertEqual(self.run_gate(str(self.tmp / "BUNDLE.json"))[0], 1)
def test_one_failing_target_fails_the_run(self):
write_tree(self.tmp / "good", bundle())
write_tree(self.tmp / "bad", bundle(commit=None))
code, text = self.run_gate(str(self.tmp / "good"), str(self.tmp / "bad"))
self.assertEqual(code, 1)
self.assertIn("1 of 2 pass", text)
def test_the_squashfs_offset_is_the_end_of_the_elf_section_headers(self):
head = bytearray(64)
head[:4] = b"\x7fELF"
head[4], head[5] = 2, 1
struct.pack_into("<Q", head, 0x28, 1000) # e_shoff
struct.pack_into("<HH", head, 0x3A, 64, 29) # e_shentsize, e_shnum
elf = self.tmp / "runtime"
elf.write_bytes(bytes(head) + b"\0" * 100)
self.assertEqual(gate.appimage_squashfs_offset(elf), 1000 + 64 * 29)
(self.tmp / "notelf").write_bytes(b"MZ" + b"\0" * 80)
with self.assertRaises(gate.Unreadable):
gate.appimage_squashfs_offset(self.tmp / "notelf")
@unittest.skipUnless(shutil.which("mksquashfs") and shutil.which("unsquashfs"), "squashfs-tools not installed")
def test_an_appimage_is_read_through_unsquashfs(self):
root = self.tmp / "AppDir"
write_tree(root / "usr" / "lib" / "Strata" / "harness", bundle("linux"))
image = self.tmp / "fs.squashfs"
subprocess.run(["mksquashfs", str(root), str(image), "-noappend", "-quiet"], check=True, capture_output=True)
head = bytearray(64)
head[:4] = b"\x7fELF"
head[4], head[5] = 2, 1
struct.pack_into("<Q", head, 0x28, 64)
struct.pack_into("<HH", head, 0x3A, 64, 1) # the runtime ends at 128
runtime = bytes(head) + b"\0" * 64
appimage = self.tmp / "Strata-0.2.25-linux-x86_64.AppImage"
appimage.write_bytes(runtime + image.read_bytes())
code, text = self.run_gate(str(appimage))
self.assertEqual(code, 0, text)
self.assertIn("appimage linux", text)
class RepoPin(unittest.TestCase):
def test_the_real_pin_names_a_commit_a_version_and_three_patches(self):
p = json.loads((REPO / "app" / "desktop" / "harness-pin.json").read_text(encoding="utf-8"))
self.assertRegex(p["commit"], r"^[0-9a-f]{40}$")
self.assertEqual([x["id"] for x in p["patches"]], IDS)
for x in p["patches"]:
self.assertRegex(x["afterSha256"], r"^[0-9a-f]{64}$")
if __name__ == "__main__":
unittest.main()