File size: 13,675 Bytes
6aa6c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""Content-pinned clean-task deployment, without native imports or resets.

Only two CuRobo path-config files are derived. Original source and asset roots
are never edited. The package's reference snapshot is not a Git attestation.
"""
from __future__ import annotations

import argparse
import hashlib
import json
import os
from pathlib import Path, PurePosixPath
import stat

SNAPSHOT_SCHEMA = "starvla-rsi.robotwin-native-snapshot/v1"
DEPLOYMENT_SCHEMA = "starvla-rsi.robotwin-deployment/v1"
SNAPSHOT_FILE = Path(__file__).with_name("native_snapshot.json")
CONFIGS = tuple(f"assets/embodiments/aloha-agilex/curobo_{side}.yml" for side in ("left", "right"))
TEMPLATES = tuple(f"assets/embodiments/aloha-agilex/curobo_{side}_tmp.yml" for side in ("left", "right"))
SOURCE_TREES = ("envs", "task_config", "assets/embodiments/aloha-agilex",
                "assets/objects/050_bell", "assets/objects/060_kitchenpot")
EXTRA_FILES = ("LICENSE", "script/eval_policy.py", "script/requirements.txt",
               "description/task_instruction/lift_pot.json", "description/task_instruction/click_bell.json",
               "assets/objects/objaverse/list.json", "assets/objects/same.json")
IGNORED_DIRS = {".git", "__pycache__", ".pytest_cache", ".cache"}
MAX_MANIFEST_BYTES = 4 * 1024 * 1024
MAX_FILES = 10000


def digest(path: Path) -> str:
    result = hashlib.sha256()
    with path.open("rb") as stream:
        while chunk := stream.read(4 * 1024 * 1024):
            result.update(chunk)
    return result.hexdigest()


def _relative(value: str) -> str:
    if (not isinstance(value, str) or not value or "\\" in value or "\x00" in value
            or PurePosixPath(value).is_absolute()
            or any(part in ("", ".", "..") for part in value.split("/"))):
        raise ValueError("unsafe deployment path")
    return value


def _regular(root: Path, relative: str) -> Path:
    path = root
    for part in _relative(relative).split("/"):
        path = path / part
        if path.is_symlink():
            raise ValueError(f"symlink is not a pinned regular file: {relative}")
    if not path.is_file():
        raise ValueError(f"missing deployment file: {relative}")
    return path


def _canonical(value) -> bytes:
    return json.dumps(value, sort_keys=True, separators=(",", ":"), allow_nan=False).encode()


def _json(path: Path) -> dict:
    if path.is_symlink() or not path.is_file() or path.stat().st_size > MAX_MANIFEST_BYTES:
        raise ValueError("invalid or oversized manifest")

    def unique(pairs):
        value = {}
        for key, item in pairs:
            if key in value:
                raise ValueError("duplicate manifest key")
            value[key] = item
        return value

    payload = json.loads(path.read_bytes(), object_pairs_hook=unique,
                         parse_constant=lambda value: (_ for _ in ()).throw(ValueError("nonfinite manifest")))
    if not isinstance(payload, dict):
        raise ValueError("manifest must be an object")
    return payload


def _snapshot_files(root: Path):
    """Inventory clean-track runtime closure; omit unrelated CuRobo docs/Git."""
    selected = set(EXTRA_FILES)
    for relative in SOURCE_TREES:
        base = root / relative
        if not base.is_dir() or base.is_symlink():
            raise ValueError(f"missing regular source/asset directory: {relative}")
        for directory, children, names in os.walk(base, followlinks=False):
            children[:] = [name for name in children if name not in IGNORED_DIRS]
            if Path(directory) == root / "envs/curobo":
                children[:] = [name for name in children if name == "src"]
                names = []
            if Path(directory) == root / "envs/curobo/src":
                children[:] = [name for name in children if name == "curobo"]
                names = []
            for name in children:
                if (Path(directory) / name).is_symlink():
                    raise ValueError("source/asset directory symlink requires explicit materialization")
            for name in names:
                if name.endswith((".pyc", ".pyo")):
                    continue
                path = Path(directory) / name
                if path.is_symlink() or not stat.S_ISREG(path.stat().st_mode):
                    raise ValueError("source/asset inventory requires regular files")
                selected.add(path.relative_to(root).as_posix())
    if len(selected) > MAX_FILES:
        raise ValueError("source/asset inventory exceeds file bound")
    return sorted(selected)


def inspect_reference(root: Path) -> dict:
    root = root.resolve(strict=True)
    rows = []
    for relative in _snapshot_files(root):
        path = _regular(root, relative)
        rows.append({"path": relative, "size_bytes": path.stat().st_size, "sha256": digest(path)})
    return {"schema_version": SNAPSHOT_SCHEMA, "native_source_revision": None,
            "scope": "content-pinned local archive; clean Aloha lift_pot/click_bell only; not upstream-full attestation",
            "files": rows, "inventory_sha256": hashlib.sha256(_canonical(rows)).hexdigest()}


def reference_snapshot() -> dict:
    value = _json(SNAPSHOT_FILE)
    if (value.get("schema_version") != SNAPSHOT_SCHEMA or value.get("native_source_revision") is not None
            or not isinstance(value.get("files"), list) or not 1 <= len(value["files"]) <= MAX_FILES):
        raise ValueError("invalid packaged native snapshot")
    previous = ""
    for row in value["files"]:
        if set(row) != {"path", "size_bytes", "sha256"}:
            raise ValueError("invalid snapshot row")
        relative = _relative(row["path"])
        if (relative <= previous or type(row["size_bytes"]) is not int or row["size_bytes"] < 0
                or not isinstance(row["sha256"], str) or len(row["sha256"]) != 64
                or any(character not in "0123456789abcdef" for character in row["sha256"])):
            raise ValueError("invalid snapshot ordering/hash")
        previous = relative
    if hashlib.sha256(_canonical(value["files"])).hexdigest() != value.get("inventory_sha256"):
        raise ValueError("packaged native snapshot digest differs")
    return value


def _derived(root: Path, relative: str, snapshot: dict) -> bytes:
    template = TEMPLATES[CONFIGS.index(relative)]
    source = _regular(root, template)
    expected = next((row for row in snapshot["files"] if row["path"] == template), None)
    if expected is None:
        raise ValueError("pinned CuRobo path template is missing")
    if digest(source) != expected["sha256"]:
        raise ValueError("CuRobo path template changed")
    text = source.read_text()
    if "${ASSETS_PATH}" not in text and "$ASSETS_PATH" not in text:
        raise ValueError("expected native path placeholder is absent")
    return text.replace("${ASSETS_PATH}", str(root)).replace("$ASSETS_PATH", str(root)).encode()


def prepare(reference_root: Path, output: Path) -> dict:
    """Copy matching bytes to a new root; failures keep a partial deployment."""
    source = reference_root.resolve(strict=True)
    target = output.absolute()
    if target != target.resolve() or target.exists() or target.is_symlink() or target == source or source.is_relative_to(target) or target.is_relative_to(source):
        raise ValueError("deployment destination must be new and independent of the reference root")
    snapshot = reference_snapshot()
    target.mkdir(parents=True, exist_ok=False)
    for row in snapshot["files"]:
        original = _regular(source, row["path"])
        destination = target / row["path"]
        destination.parent.mkdir(parents=True, exist_ok=True)
        copied = hashlib.sha256()
        with original.open("rb") as reader, destination.open("xb") as writer:
            while block := reader.read(4 * 1024 * 1024):
                copied.update(block)
                writer.write(block)
        if destination.stat().st_size != row["size_bytes"] or copied.hexdigest() != row["sha256"]:
            raise ValueError(f"reference file changed or differs from pinned bytes: {row['path']}")
    derived = []
    for relative, template in zip(CONFIGS, TEMPLATES):
        path = target / relative
        original = path.read_bytes()
        payload = _derived(target, relative, snapshot)
        path.write_bytes(payload)
        derived.append({"path": relative, "template": template,
                        "reference_sha256": hashlib.sha256(original).hexdigest(),
                        "deployed_sha256": hashlib.sha256(payload).hexdigest(),
                        "size_bytes": len(payload), "transform": "native template ASSETS_PATH expansion only"})
    manifest = {"schema_version": DEPLOYMENT_SCHEMA, "root": str(target),
                "reference_snapshot_sha256": digest(SNAPSHOT_FILE),
                "reference_inventory_sha256": snapshot["inventory_sha256"],
                "native_source_revision": None, "derived_files": derived,
                "limitations": "file identity only; no native reset, asset-release completeness or loaded-memory attestation"}
    with (target / "deployment.json").open("x") as stream:
        stream.write(json.dumps(manifest, indent=2, allow_nan=False) + "\n")
    return verify(target, digest(target / "deployment.json"))


def verify(root: Path, manifest_sha256: str) -> dict:
    if (not isinstance(manifest_sha256, str) or len(manifest_sha256) != 64
            or any(c not in "0123456789abcdef" for c in manifest_sha256)):
        raise ValueError("deployment manifest SHA-256 must be 64 lowercase hexadecimal characters")
    original_root = root.absolute()
    root = root.resolve(strict=True)
    if root != original_root or root.is_symlink():
        raise ValueError("deployment root must be canonical and not a symlink")
    manifest_path = _regular(root, "deployment.json")
    if digest(manifest_path) != manifest_sha256:
        raise ValueError("deployment manifest digest mismatch")
    manifest = _json(manifest_path)
    snapshot = reference_snapshot()
    if (manifest.get("schema_version") != DEPLOYMENT_SCHEMA or manifest.get("root") != str(root)
            or manifest.get("reference_snapshot_sha256") != digest(SNAPSHOT_FILE)
            or manifest.get("reference_inventory_sha256") != snapshot["inventory_sha256"]
            or manifest.get("native_source_revision") is not None):
        raise ValueError("deployment identity changed")
    rows = {row["path"]: row for row in snapshot["files"]}
    expected_derived = []
    for relative, template in zip(CONFIGS, TEMPLATES):
        payload = _derived(root, relative, snapshot)
        expected_derived.append({"path": relative, "template": template,
                                 "reference_sha256": rows[relative]["sha256"],
                                 "deployed_sha256": hashlib.sha256(payload).hexdigest(),
                                 "size_bytes": len(payload), "transform": "native template ASSETS_PATH expansion only"})
    if manifest.get("derived_files") != expected_derived:
        raise ValueError("unexpected deployment transformation")
    expected = {relative: dict(row) for relative, row in rows.items()}
    for row in expected_derived:
        expected[row["path"]] = {"path": row["path"], "size_bytes": row["size_bytes"],
                                  "sha256": row["deployed_sha256"]}
    actual = set()
    for directory, children, names in os.walk(root, followlinks=False):
        for child in children:
            if (Path(directory) / child).is_symlink():
                raise ValueError("unverified deployment directory symlink")
        for name in names:
            relative = (Path(directory) / name).relative_to(root).as_posix()
            if relative != "deployment.json":
                actual.add(relative)
    if actual != set(expected):
        raise ValueError("deployment file set changed; caches, overrides and extra files are not allowed")
    for relative, row in expected.items():
        path = _regular(root, relative)
        if path.stat().st_size != row["size_bytes"] or digest(path) != row["sha256"]:
            raise ValueError(f"deployment content changed: {relative}")
    return {"deployment_root": str(root), "deployment_manifest_sha256": manifest_sha256,
            "reference_snapshot_sha256": digest(SNAPSHOT_FILE),
            "reference_inventory_sha256": snapshot["inventory_sha256"],
            "native_source_revision": None, "file_count": len(expected),
            "total_bytes": sum(row["size_bytes"] for row in expected.values()),
            "derived_files": expected_derived, "status": "content_verified_not_native_validated"}


def main(argv=None):
    parser = argparse.ArgumentParser(description=__doc__)
    subparsers = parser.add_subparsers(dest="command", required=True)
    prepare_parser = subparsers.add_parser("prepare", help="materialize a new matching clean-task deployment")
    prepare_parser.add_argument("--reference-root", type=Path, required=True)
    prepare_parser.add_argument("--output-dir", type=Path, required=True)
    check_parser = subparsers.add_parser("check", help="hash files only; no SIM imports/reset")
    check_parser.add_argument("--root", type=Path, required=True)
    check_parser.add_argument("--manifest-sha256", required=True)
    arguments = parser.parse_args(argv)
    if arguments.command == "prepare":
        result = prepare(arguments.reference_root, arguments.output_dir)
    else:
        result = verify(arguments.root, arguments.manifest_sha256)
    print(json.dumps(result, indent=2, allow_nan=False))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())