fix(dashboard-testing): pack laboratory archive bytes

This commit is contained in:
2026-10-11 04:14:26 +03:00
parent 7c0324fb5e
commit c0c7f69858
4 changed files with 101 additions and 16 deletions

View File

@@ -9,7 +9,7 @@ from pathlib import Path
import stat
from dashboard_testing_lab_reset_archive import (
digest, read_bytes, require, safe_path, sync_directory, write_bytes,
decode, digest, encode, json_bytes, read_bytes, read_json, require, safe_path, sync_directory, write_bytes,
)
from dashboard_testing_lab_reset_db import node
@@ -17,6 +17,7 @@ ROOTS = ("/app/backend/data/dashboard_testing", "/app/backend/git_repos", "/app/
MARKER = "/app/storage/.native-result-holds/activated"
INDEX = "/app/storage/.native-result-holds/index.json"
LOCK = "/app/storage/.native-result-mutation.catalog.lock"
PACK = "selected-files.json"
# #region Tooling.LabReset.Files.Confined [C:2] [TYPE Function]
@@ -194,6 +195,7 @@ def validate_holds(scope, state):
# #region Tooling.LabReset.Files.Archive [C:3] [TYPE Function]
# @POST One exclusive private canonical pack deduplicates exact selected bytes; every member binds the pack digest.
def archive_files(scope, evidence):
result = []
blobs = {}
@@ -201,27 +203,36 @@ def archive_files(scope, evidence):
for item in manifest["selected"]:
raw = member(root, item)
hashed = item["sha256"]
name = f"bytes-{hashed}"
if hashed not in blobs:
blobs[hashed] = write_bytes(evidence, name, raw)
require(blobs[hashed] == hashed, "LAB_RESET_ARCHIVE_READBACK_FAILED")
result.append({**item, "member": name, "file_metadata": file_metadata(Path(item["path"]))})
return result
require(hashed not in blobs or blobs[hashed] == raw, "LAB_RESET_ARCHIVE_READBACK_FAILED")
blobs[hashed] = raw
result.append({**item, "member": hashed, "file_metadata": file_metadata(Path(item["path"]))})
packed_sha = write_bytes(evidence, PACK, json_bytes(encode(blobs)))
return [{**item, "pack_sha256": packed_sha} for item in result]
# #endregion Tooling.LabReset.Files.Archive
# #region Tooling.LabReset.Files.VerifyArchive [C:3] [TYPE Function]
# @PRE Pack keys, canonical encoding, digest and each original selected member match exactly before any deletion.
def verify_archive_files(scope, evidence, archived):
expected = sorted([item for manifest in scope["files"].values() for item in manifest["selected"]],
key=lambda item: item["path"])
require([{key: value for key, value in item.items() if key not in ("member", "file_metadata")}
require([{key: value for key, value in item.items() if key not in ("member", "file_metadata", "pack_sha256")}
for item in sorted(archived, key=lambda item: item["path"])] == expected,
"LAB_RESET_ARCHIVE_SCOPE_CONFLICT")
pack_path = safe_path(evidence, PACK)
packed_raw = read_bytes(pack_path, private=True)
packed_sha = sha256(packed_raw).hexdigest()
blobs = decode(read_json(pack_path, packed_sha, private=True))
require(type(blobs) is dict and set(blobs) == {item["sha256"] for item in expected}
and json_bytes(encode(blobs)) == packed_raw, "LAB_RESET_ARCHIVE_SCOPE_CONFLICT")
roots = {item["path"]: root for root, manifest in scope["files"].items() for item in manifest["selected"]}
for item in archived:
require(item["member"] == f"bytes-{item['sha256']}", "LAB_RESET_ARCHIVE_SCOPE_CONFLICT")
require(item["member"] == item["sha256"] and item["pack_sha256"] == packed_sha,
"LAB_RESET_ARCHIVE_SCOPE_CONFLICT")
require(item["file_metadata"] == file_metadata(Path(item["path"])), "LAB_RESET_ARCHIVE_SCOPE_CONFLICT")
raw = read_bytes(safe_path(evidence, item["member"]), private=True)
require(len(raw) == item["bytes"] and sha256(raw).hexdigest() == item["sha256"],
raw = blobs[item["member"]]
require(type(raw) is bytes and len(raw) == item["bytes"] and sha256(raw).hexdigest() == item["sha256"]
and raw == member(roots[item["path"]], item),
"LAB_RESET_ARCHIVE_READBACK_FAILED")
# #endregion Tooling.LabReset.Files.VerifyArchive