diff --git a/.github/large-files.txt b/.github/large-files.txt index 4cbbe9fb1..f13cd1456 100644 --- a/.github/large-files.txt +++ b/.github/large-files.txt @@ -9,3 +9,5 @@ assets/profile-pictures/flowers.png # Narration receipts the practice packages are built from design/tai-chi/narration-receipt.json design/tai-chi/narration-male-receipt.json +# Exact digest-bound Arcade library artwork shipped for offline browsing. +apps/bundled/arcade.games/bd0a779ce62c2324812926f1579a64881db309430cff839565b28d1bc5b6420f.zx diff --git a/CHANGELOG.md b/CHANGELOG.md index d845a78d3..6d6d56870 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [Unreleased] + +- Include Arcade and its library artwork offline, while games download separately. +- Keep every shipped app and also include complete apps below 1 MB without changing app selections. + ## [2.1.62] - 2026-11-02 - Edit app order with a single move grip and no extra Hide or Show overlay. diff --git a/apps/bundled.json b/apps/bundled.json new file mode 100644 index 000000000..2ee0f9a77 --- /dev/null +++ b/apps/bundled.json @@ -0,0 +1,13 @@ +{ + "arcade": { + "id": "inbe.arcade", + "version": "0.7.118", + "host_api": 41, + "key_id": "inbe-2026-10", + "bundled": true, + "path": "apps/bundled/arcade.zx", + "bytes": 7674, + "sha256": "e6b3bb38f75201a4c4722b8cd8411e0538a264104271c3dcddf4418f6fd4d665", + "source": "https://github.com/waozixyz/arcade" + } +} diff --git a/apps/bundled/arcade.games/bd0a779ce62c2324812926f1579a64881db309430cff839565b28d1bc5b6420f.zx b/apps/bundled/arcade.games/bd0a779ce62c2324812926f1579a64881db309430cff839565b28d1bc5b6420f.zx new file mode 100644 index 000000000..193225917 Binary files /dev/null and b/apps/bundled/arcade.games/bd0a779ce62c2324812926f1579a64881db309430cff839565b28d1bc5b6420f.zx differ diff --git a/apps/bundled/arcade.zx b/apps/bundled/arcade.zx new file mode 100644 index 000000000..756965177 Binary files /dev/null and b/apps/bundled/arcade.zx differ diff --git a/docs/apps.md b/docs/apps.md index 7b0bf387b..52a8a0f2a 100644 --- a/docs/apps.md +++ b/docs/apps.md @@ -315,6 +315,26 @@ publishing an immutable payload and atomically replacing its current pointer. Root packages must contain exactly the three signed recommended dependencies. Malformed packages cannot apply writes through a mismatched host protocol. +Bundling is additive: the existing six packages always ship regardless of +size, and complete locally built node apps smaller than **1,000,000 bytes** +also ship as offline baselines. Required app artwork counts toward that limit; +the shared Apps controller already supplied by the host counts once. A tiny +collection catalog is not the size of its downloadable games. Arcade is +explicitly included in `apps/bundled.json`, independently of the automatic +limit: its 7,674-byte catalog plus 1,512,084-byte library artwork ship together. +Its five games (65,639,505 bytes) remain separate, consented downloads. The +reviewed local artifacts are digest-pinned; builds never fetch or execute an +unsigned remote app to qualify it. `scripts/small_app_bundling.py` writes the +selection and sizes to `build/small-app-bundling.json`. + +Optional baselines live under `assets/apps/bundled/`, preserving the historical +`cells/` IDs and dependency validation. They appear in Apps even without a +network and take a device-local slot only when added. Existing selections, +hidden apps and sidebar order are unchanged. Signed installed updates take +precedence; the embedded fallback retains publisher scope, host API, digest, +resource, capability and identity validation. App size includes library art; +downloadable games retain their own sizes and installation state. + Runtime resources are selected in `scripts/prepare-package-assets.py`; source icons, store banners, prompts and test images are excluded. Sun Salutation keeps its full four-character, 30 fps timeline while storing identical PNGs @@ -606,8 +626,9 @@ so an app added to the node appears without an app update. The publisher pin Installing one assigns one of eight device-local slots (package indexes after the bundled seven, setting `node_app_slot_`) and downloads it through the package manager with the same signature, hash, size, interface, trial -and cache rules as the bundled apps. Such an app is never part of the root -package or its dependencies. It runs in its own VM through one generic +and cache rules as the bundled apps. Small local apps and explicitly declared +apps can also have a root-embedded baseline, outside its dependencies. Each +runs in its own VM through one generic interface (`apps/node_types` from the Apps package, host interface 12, and 13 for the media library, which Diary also reads from 14): frame and route messages, hosted Kryon widgets, `AppLocaleHost`, private records, diff --git a/mk/apps.mk b/mk/apps.mk index 00fc85a89..1ec9b1082 100644 --- a/mk/apps.mk +++ b/mk/apps.mk @@ -12,6 +12,8 @@ ENTITY_SOURCES = $(wildcard $(ENTITY_DIR)/src/*.zi $(ENTITY_DIR)/assets/entity/* # own pinned graph, so its digest identifies the reviewed Apps release. APPS_DIR := build/packages/apps APPS_BUNDLE ?= build/apps-ui/apps.zx +NODE_APP_NAMES := $(shell python3 -c 'import json; print(" ".join(json.load(open("apps/versions.json")).get("node_apps", {})))') +NODE_APP_BUNDLES := $(addprefix $(BUILD_DIR)/node-apps/,$(addsuffix .zx,$(NODE_APP_NAMES))) $(BUILD_DIR)/apps-ui/apps.zx: ziran.lock scripts/build-apps.py $(wildcard $(APPS_DIR)/ziran.lock $(APPS_DIR)/src/*.zi $(APPS_DIR)/ui/*.zi $(APPS_DIR)/ui/locales/*.json) env -u DISPLAY -u WAYLAND_DISPLAY PATH="$(abspath $(dir $(ZIRAN_BIN))):$$PATH" \ python3 scripts/build-apps.py --ziran $(abspath $(ZIRAN_BIN)) --output $(abspath $(dir $@)) @@ -19,13 +21,12 @@ $(BUILD_DIR)/apps-ui/apps.zx: ziran.lock scripts/build-apps.py $(wildcard $(APPS apps: $(APP_BUNDLES) $(INBE_BUNDLE) $(INBE_FULL_BUNDLE) $(APP_BUNDLES) $(INBE_BUNDLE) $(INBE_FULL_BUNDLE): CHANGELOG.md $(wildcard locales/changelog/*.md) scripts/release_notes.py $(APPS_BUNDLE) src/apps/apps_controller.zi +$(APP_BUNDLES) $(INBE_BUNDLE) $(INBE_FULL_BUNDLE): $(NODE_APP_BUNDLES) scripts/small_app_bundling.py apps/bundled.json $(wildcard apps/bundled/*.zx apps/bundled/*.games/*.zx) $(APP_BUNDLES) $(INBE_BUNDLE) $(INBE_FULL_BUNDLE) &: $(ENTITY_SOURCES) $(APP_SOURCES) $(APP_RESOURCES) ziran.lock apps/versions.json scripts/check-package-versions.py scripts/generate-package-versions.py scripts/prepare-package-assets.py scripts/achievement_icons.py scripts/animation_inventory.py scripts/build-apps.sh src/practices/sun_salutation/sun_salutation_assets.zi src/practices/sun_salutation/sun_salutation_inventory.zi $(IMAGE_FILES) $(SOUND_FILES) $(FONT_FILES) $(STYLE_FILES) $(LOCALE_FILES) $(ZIRAN_BIN) sh scripts/build-apps.sh $(ZIRAN_BIN) $(BUILD_DIR)/apps $(APPS_BUNDLE) -# Apps the node offers build on their own, never into the root package. -NODE_APP_NAMES := $(shell python3 -c 'import json; print(" ".join(json.load(open("apps/versions.json")).get("node_apps", {})))') -NODE_APP_BUNDLES := $(addprefix $(BUILD_DIR)/node-apps/,$(addsuffix .zx,$(NODE_APP_NAMES))) +# Build node apps independently before selecting the small offline baselines. $(NODE_APP_BUNDLES) &: $(APP_SOURCES) $(shell rg --files $(addprefix assets/,$(NODE_APP_NAMES)) 2>/dev/null) apps/versions.json scripts/build-node-apps.sh scripts/prepare-package-assets.py scripts/achievement_icons.py $(ZIRAN_BIN) sh scripts/build-node-apps.sh $(ZIRAN_BIN) diff --git a/mk/tests-apps.mk b/mk/tests-apps.mk index 16a87ecaf..3c55c1ac0 100644 --- a/mk/tests-apps.mk +++ b/mk/tests-apps.mk @@ -84,6 +84,10 @@ package-workflow-test: node-app-games-test: python3 tests/node_app_games_test.py +.PHONY: small-app-bundling-test +small-app-bundling-test: + python3 tests/small_app_bundling_test.py + .PHONY: package-manifest-test package-manifest-test: python3 tests/package_manifests_test.py diff --git a/mk/tests-suite.mk b/mk/tests-suite.mk index 54814e641..e0d5fddf4 100644 --- a/mk/tests-suite.mk +++ b/mk/tests-suite.mk @@ -97,6 +97,7 @@ test: audio-runtime-test test: audio-meter-zi-test test: profile-picture-policy-test test: assets-zi-test +test: small-app-bundling-test test: android-package-assets-test package-size-test test: asset-files-zi-test test: settings-status-zi-test diff --git a/scripts/build-apps.sh b/scripts/build-apps.sh index fe8e9a03e..62bbf4f58 100644 --- a/scripts/build-apps.sh +++ b/scripts/build-apps.sh @@ -57,10 +57,9 @@ for feature in lists habits practices diary lumi leaderboard; do --entry "$entry:Main" -o "$output/$feature.zx" \ "apps/$source/$source.zi" done -# Removing a sidebar shortcut never removes its bundled package. -for feature in lists habits practices diary lumi leaderboard; do - cp "$output/$feature.zx" build/bundled-apps/ -done +# Keep the historical shipped set and add complete packages below 1 decimal MB. +# Optional apps remain outside cells/ and never change saved app selections. +python3 scripts/small_app_bundling.py --bundles "$output" # The package folder keeps its historical name cells/ for released hosts. # shellcheck disable=SC2086 "$ziran" bundle --root . --module-path src $packages \ diff --git a/scripts/build-node-apps.sh b/scripts/build-node-apps.sh index 6bd540e97..57164629a 100644 --- a/scripts/build-node-apps.sh +++ b/scripts/build-node-apps.sh @@ -1,7 +1,6 @@ #!/bin/sh -# Apps the node offers (node_apps in apps/versions.json) are built on their -# own into build/node-apps. They never go into the root package or its -# apps: released hosts reject a root that carries an app they don't know. +# Apps the node offers build independently into build/node-apps. Complete +# small apps also ship as optional baselines, outside the root's cells/ IDs. set -eu root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) cd "$root" diff --git a/scripts/small_app_bundling.py b/scripts/small_app_bundling.py new file mode 100644 index 000000000..177d5b1cf --- /dev/null +++ b/scripts/small_app_bundling.py @@ -0,0 +1,171 @@ +#!/usr/bin/env python3 +"""Retain shipped apps and add complete local packages below one decimal MB.""" +import argparse +from hashlib import sha256 +import json +from pathlib import Path +import re +import shutil + +from practice_content import bundle_assets + + +LIMIT_BYTES = 1_000_000 +MANDATORY = ('lists', 'habits', 'practices', 'diary', 'lumi', 'leaderboard') + + +def cover_paths(catalog): + for child in catalog.get('children', []): + cover = child.get('cover', {}) + for path in (child.get('preview'), child.get('icon'), cover.get('portrait'), + cover.get('landscape'), cover.get('logo')): + if path: + yield path + + +def validate_dependencies(catalog): + # Bundles contain their compiled imports. The only supported external + # dependencies here are the catalog's separately installed game graph. + if catalog.get('dependencies'): + raise ValueError('unsupported required package dependencies') + children = catalog.get('children', []) + if len(children) > 16: + raise ValueError('too many child packages') + entries = {child['id']: child for child in children if 'id' in child} + visiting, visited = set(), set() + + def visit(identity): + if identity in visiting: + raise ValueError('cyclic child dependencies') + if identity in visited: + return + if identity not in entries: + raise ValueError('missing child dependency') + visiting.add(identity) + for dependency in entries[identity].get('dependencies', []): + visit(dependency) + visiting.remove(identity) + visited.add(identity) + + for identity in entries: + visit(identity) + + +def validate_payload(item): + if (not isinstance(item.get('sha256'), str) + or not re.fullmatch('[0-9a-f]{64}', item['sha256']) + or type(item.get('bytes')) is not int or not 8 <= item['bytes'] <= 64 * 1024 * 1024): + raise ValueError('invalid catalog payload digest or size') + + +def inspect_bundle(path): + data = path.read_bytes() + assets = bundle_assets(data) + catalog = json.loads(assets.get('assets/apps/catalog.json', b'{}')) + validate_dependencies(catalog) + external = {} + for child in catalog.get('children', []): + validate_payload(child) + if child['bundle'] not in assets: + external[child['sha256']] = child['bytes'] + art = catalog.get('art') + if art: + validate_payload(art) + # Child games install separately with consent. Library artwork belongs to + # the launcher: count it even when its catalog is only a few kilobytes. + complete = True + if not art: + complete = all(p in assets for p in cover_paths(catalog)) + elif 'assets/apps/art.zx' in assets: + data_art = assets['assets/apps/art.zx'] + complete = (len(data_art) == art['bytes'] and sha256(data_art).hexdigest() == art['sha256']) + if complete: + pictures = bundle_assets(data_art) + complete = all(p in pictures or p in assets for p in cover_paths(catalog)) + art_bytes = art['bytes'] if art and 'assets/apps/art.zx' not in assets else 0 + return dict(bundle_bytes=len(data), complete_bytes=len(data) + art_bytes, complete=complete, + required_art_bytes=art_bytes, + optional_download_bytes=sum(external.values()), + optional_downloads=external, sha256=sha256(data).hexdigest()) + + +def stage(ledger, declarations, bundles, optional, destination): + destination.mkdir(parents=True, exist_ok=True) + # The historical shipped set is independent of size and sidebar choices. + for name in MANDATORY: + shutil.copyfile(bundles / (name + '.zx'), destination / (name + '.zx')) + # This generated inventory is separate from the mandatory shipped set. + # Reusing a staging directory must not leak an excluded/removed candidate. + if optional.exists(): + shutil.rmtree(optional) + optional.mkdir(parents=True) + candidates = [(name, bundles.parent / 'node-apps' / (name + '.zx'), None) + for name in ledger.get('node_apps', {})] + candidates += [(name, Path(item['path']), item) + for name, item in declarations.items()] + report = dict(limit_bytes=LIMIT_BYTES, mandatory=list(MANDATORY), optional={}) + selected = [] + releases = {} + for name, path, declaration in candidates: + if (not name.isascii() or not name.replace('-', '').isalnum() + or not name.islower() or name in report['optional'] or name in MANDATORY): + raise ValueError('invalid or duplicate optional app name') + info = inspect_bundle(path) + if declaration and (declaration['sha256'] != info['sha256'] + or declaration['bytes'] != info['bundle_bytes'] + or declaration['id'] != 'inbe.' + name): + raise ValueError('local app differs from its reviewed declaration') + catalog = json.loads(bundle_assets(path.read_bytes()).get('assets/apps/catalog.json', b'{}')) + art = catalog.get('art') + resource = None + if art and info['complete_bytes'] != info['bundle_bytes']: + resource = path.parent / (name + '.games') / (art['sha256'] + '.zx') + info['complete'] = (resource.is_file() and resource.stat().st_size == art['bytes'] + and sha256(resource.read_bytes()).hexdigest() == art['sha256']) + if info['complete']: + pictures = bundle_assets(resource.read_bytes()) + packed = bundle_assets(path.read_bytes()) + info['complete'] = all(p in pictures or p in packed for p in cover_paths(catalog)) + forced = bool(declaration and declaration.get('bundled')) + if forced and not info['complete']: + raise ValueError('explicitly bundled app is missing required artwork') + info['included'] = info['complete'] and (forced or info['complete_bytes'] < LIMIT_BYTES) + info['explicit'] = forced + report['optional'][name] = info + if info['included']: + shutil.copyfile(path, optional / (name + '.zx')) + if resource: + target = optional / name / resource.name + target.parent.mkdir(parents=True, exist_ok=True) + shutil.copyfile(resource, target) + selected.append(name) + source = declaration or ledger['node_apps'][name] + releases[name] = dict(id='inbe.' + name, version=source['version'], + sequence=source.get('sequence', 1), host_api=source['host_api'], + key_id=source.get('key_id', 'inbe-2026-10'), + sha256=info['sha256'], bytes=info['bundle_bytes'], + complete_bytes=info['complete_bytes']) + (optional / 'catalog.txt').write_text(''.join(name + '\n' for name in selected)) + (optional / 'releases.json').write_text(json.dumps(releases, separators=(',', ':')) + '\n') + return report + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--bundles', type=Path, default=Path('build/apps')) + parser.add_argument('--optional', type=Path, default=Path('build/package-assets/inbe/assets/apps/bundled')) + parser.add_argument('--mandatory', type=Path, default=Path('build/bundled-apps')) + parser.add_argument('--report', type=Path, default=Path('build/small-app-bundling.json')) + args = parser.parse_args() + report = stage(json.loads(Path('apps/versions.json').read_text()), + json.loads(Path('apps/bundled.json').read_text()), + args.bundles, args.optional, args.mandatory) + args.report.write_text(json.dumps(report, indent=2) + '\n') + for name, info in report['optional'].items(): + print(f"{name}: {info['bundle_bytes']} package bytes, " + + f"{info['complete_bytes']} bytes with required artwork, " + + ('embedded offline' if info['included'] else 'separate optional download')) + + +if __name__ == '__main__': + main() diff --git a/src/apps/node_app_embedded.zi b/src/apps/node_app_embedded.zi new file mode 100644 index 000000000..18096443b --- /dev/null +++ b/src/apps/node_app_embedded.zi @@ -0,0 +1,112 @@ +// Optional baselines belong to the trusted root package, outside its stable +// cells/ dependency set. They occupy a node slot only when the user adds one. +#import, file "node_apps.zi"; +#import, file "../app/assets.zi"; +#import, file "package_files.zi"; +#import, file "package_release.zi"; +#import, file "package_manager.zi"; +#import, file "host_api.zi"; +#import, file "versions.zi"; +#import "json_scan" +#import "std/text_buffer" +#import, file "catalog.zi"; +#import, file "../storage/app_records.zi"; +#import, file "../app/text_buffers.zi"; + +NodeAppEmbeddedRelease :: (name: string) -> PackageRelease { + if !NodeAppNameValid(name) { + return PackageRelease.{} + } + metadata := AssetRead("assets/apps/bundled/releases.json") + if !metadata.found || metadata.data.count > 16384 { + return PackageRelease.{} + } + at := FieldAt(metadata.data, 0, name) + api := PackageNumber(metadata.data, at, "host_api") + index := NodeAppSlotFor(name) + oldest := PackageHostAPIOldestFor(ifx index >= 0 then NodeAppIndex(index) else -1) + if at < 0 || api < oldest || api > PackageHostAPI { + return PackageRelease.{} + } + release: PackageRelease + unused PackageField(metadata.data, at, "version", release.version[:]) + unused PackageField(metadata.data, at, "sha256", release.hash[:]) + unused PackageField(metadata.data, at, "key_id", release.key_id[:]) + release.size = PackageNumber(metadata.data, at, "bytes") + release.sequence = cast(s32)PackageNumber(metadata.data, at, "sequence") + id: [32]u8 + expected := PackageText("inbe.", name) + defer { + PackageFree(expected) + } + if PackageField(metadata.data, at, "id", id[:]) != expected || + release.size <= 0 || release.size > PackageMaximumBytes || release.sequence < 1 || + release.version[0] == 0 { + return PackageRelease.{} + } + // Metadata is authenticated by the root package. Keep the same publisher + // scope check as downloaded releases; an embedded app gains no new key. + key: [65]u8 + key_json := PackageText("{\"key_id\":\"", TextUntilNul(release.key_id[:])) + text := PackageText(key_json, "\"}") + trusted := PackageTrustedKeyFor(text, expected, key[:]) + PackageFree(key_json) + PackageFree(text) + release.valid = trusted + return release +} + +NodeAppEmbedded :: (name: string) -> AssetView { + if !NodeAppNameValid(name) { + return AssetView.{} + } + prefix := PackageText("assets/apps/bundled/", name) + path := PackageText(prefix, ".zx") + defer { + PackageFree(prefix) + PackageFree(path) + } + release := NodeAppEmbeddedRelease(name) + asset := AssetRead(path) + if !release.valid || !asset.found || asset.data.count != release.size { + return AssetView.{} + } + return asset +} + +NodeAppEmbeddedOffers :: () { + catalog := AssetRead("assets/apps/bundled/catalog.txt") + if !catalog.found || catalog.data.count > 4096 { + return + } + at: s64 = 0 + while at < catalog.data.count && node_offer_count < NODE_APP_OFFERS { + end := at + while end < catalog.data.count && catalog.data[end] != #char "\n" { + end += 1 + } + name := catalog.data[at:end] + asset := NodeAppEmbedded(name) + if asset.found && !NodeAppOffered(name) { + offer := *node_offers[node_offer_count] + offer.* = NodeAppOffer.{} + unused AppCopyText(name, offer.name[:]) + unused AppCopyText(NodeAppTitle(name), offer.title[:]) + offer.size = NodeAppEmbeddedSize(name) + node_offer_count += 1 + } + at = end + 1 + } +} + +NodeAppEmbeddedSize :: (name: string) -> s64 { + metadata := AssetRead("assets/apps/bundled/releases.json") + if !metadata.found || metadata.data.count > 16384 { + return 0 + } + at := FieldAt(metadata.data, 0, name) + if at < 0 { + return 0 + } + return PackageNumber(metadata.data, at, "complete_bytes") +} diff --git a/src/apps/node_app_open.zi b/src/apps/node_app_open.zi index ca95de32a..5cc17a123 100644 --- a/src/apps/node_app_open.zi +++ b/src/apps/node_app_open.zi @@ -13,12 +13,14 @@ #import, file "package_files.zi"; #import, file "package_release.zi"; #import, file "catalog.zi"; +#import, file "node_app_embedded.zi"; node_open_work: *PackageWork; node_open_preparation: *AppPreparation; node_open_slot: s32 = -1; node_open_revision: s32; node_open_local: bool; +node_open_embedded: bool; node_open_canceled: bool; node_open_rejected: bool; node_open_requested: [NodeAppSlots]bool; @@ -74,7 +76,8 @@ NodeOpenQueue :: (slot: s32, replacement: bool = false) -> bool { node_open_failed[slot] = false } if node_open_failed[slot] || - (!replacement && !NodeAppLocal(NodeAppName(slot)) && !PackageAvailable(NodeAppIndex(slot))) { + (!replacement && !NodeAppLocal(NodeAppName(slot)) && !PackageAvailable(NodeAppIndex(slot)) && + !NodeAppEmbedded(NodeAppName(slot)).found) { return false } node_open_requested[slot] = true @@ -106,8 +109,23 @@ NodeOpenBegin :: (slot: s32) -> bool { if !local { release = PackageInstalledRelease(index) } + node_open_embedded = false if !local && !release.valid { - return false + asset := NodeAppEmbedded(NodeAppName(slot)) + if !asset.found { + return false + } + node_open_work = PackageWorkEmbedded(asset.data, NodeTreeAccount(), NodeAppName(slot)) + if node_open_work == null { + return false + } + node_open_work.release = NodeAppEmbeddedRelease(NodeAppName(slot)) + node_open_work.ok = PackageVerifyBytes(node_open_work.bytes, node_open_work.release) + node_open_slot = slot + node_open_revision = PackageRevisionFor(index) + node_open_local = false + node_open_embedded = true + return true } path := "" if local { @@ -148,6 +166,7 @@ NodeOpenActivate :: () -> bool { slot := node_open_slot digest := TextUntilNul(node_open_work.digest[:]) runner := *node_runners[slot] + runner.embedded_release = ifx node_open_embedded then node_open_work.release else PackageRelease.{} if !NodeAppStart(runner, node_open_work.bytes, digest, node_open_preparation) || (runner.nested == null && !NodeAppAssetsValid(runner.app.bundle, NodeAppName(slot))) || (!NodeInstalledCurrent(slot, digest) && !NodeInstalledScanStart(slot)) { @@ -222,14 +241,14 @@ NodeOpenPump :: (activate: bool) -> bool { NodeOpenFail() return true } - if !node_open_local && !PackageReleaseTrialAllows(index, node_open_work.release) { + if !node_open_local && !node_open_embedded && !PackageReleaseTrialAllows(index, node_open_work.release) { NodeOpenFail() return true } if !NodeAppCloseRuntime(slot) { return true } - if !node_open_local { + if !node_open_local && !node_open_embedded { package_cached_releases[index] = node_open_work.release unused CopyCString(TextUntilNul(node_open_work.release.version[:]), package_versions[index][:]) } diff --git a/src/apps/node_app_replacements.zi b/src/apps/node_app_replacements.zi index a571cec39..21aaa28a7 100644 --- a/src/apps/node_app_replacements.zi +++ b/src/apps/node_app_replacements.zi @@ -14,6 +14,7 @@ #import, file "catalog.zi"; #import, file "node_apps.zi"; #import, file "node_app_runner.zi"; +#import, file "node_app_tree.zi"; #import, file "package_manager.zi"; #import, file "package_files.zi"; #import, file "package_release.zi"; @@ -42,7 +43,7 @@ NodeAppCarries :: (slot: s32, name: string) -> bool { if child == null || TextUntilNul(child.expected_id[:]) != name { return false } - release := package_cached_releases[NodeAppIndex(slot)] + release := NodeTreeRelease(slot) if !release.valid { return false } diff --git a/src/apps/node_app_runner.zi b/src/apps/node_app_runner.zi index 5ccb53e50..2d6f3ef21 100644 --- a/src/apps/node_app_runner.zi +++ b/src/apps/node_app_runner.zi @@ -2,6 +2,7 @@ #import "hosted_app" #import "kryon/app_context_host" Nested :: #import "apps/host"; +#import, file "package_release.zi"; NodeAppRunner :: struct { app: HostedApp @@ -9,6 +10,9 @@ NodeAppRunner :: struct { portable: AppContext generic: bool release_hash: [65]u8 + // Bind a trusted-root fallback to its own publisher, never stale metadata + // from a downloaded release that failed current-descriptor validation. + embedded_release: PackageRelease // Last package generation reviewed at the host's safe reload boundary. checked_revision: s32 // Keep a verified replacement pending when the live app cannot save yet. diff --git a/src/apps/node_app_tree.zi b/src/apps/node_app_tree.zi index 340748d63..ee4fdbf57 100644 --- a/src/apps/node_app_tree.zi +++ b/src/apps/node_app_tree.zi @@ -97,6 +97,9 @@ NodeTreeRelease :: (slot: s32) -> PackageRelease { if node_tree_validating && package_index == index && package_release.valid { return package_release } + if node_runners[slot].embedded_release.valid { + return node_runners[slot].embedded_release + } return package_cached_releases[index] } diff --git a/src/apps/node_apps.zi b/src/apps/node_apps.zi index d8c62e8c6..16febc64f 100644 --- a/src/apps/node_apps.zi +++ b/src/apps/node_apps.zi @@ -6,6 +6,7 @@ // no host code. Whether an app is installed syncs with the account. #import "bundle_host" #import "apps/preparation" +#import "apps/tree" // Radio's background playback (app/background_audio.zi), declared here so // apps do not import Radio's streaming, music and achievements. node_audio_c :: #system_library "c"; @@ -40,6 +41,7 @@ Time :: #import "date_time_linux"; #import, file "node_app_snapshot.zi"; #import, file "node_app_replacements.zi"; #import, file "node_app_open.zi"; +#import, file "node_app_embedded.zi"; #import, file "node_child_packages.zi"; #import, file "node_icon_work.zi"; #import, file "achievement_media.zi"; @@ -245,6 +247,7 @@ NodeAppInstalled :: (slot: s32) -> bool { return false } return NodeAppRunning(slot) || NodeAppOpening(slot) || PackageAvailable(NodeAppIndex(slot)) || + NodeAppEmbedded(NodeAppName(slot)).found || node_installed_releases[slot][0] != cast(u8)0 } @@ -253,7 +256,15 @@ NodeAppPackageSize :: (slot: s32) -> s64 { return 0 } if NodeAppRunning(slot) { - return node_runners[slot].bundle_bytes + bytes := node_runners[slot].bundle_bytes + host := node_runners[slot].nested + if host != null { + root := AppTreeNode(*host.tree, AppHandle.{0, 1}) + if root != null { + bytes += root.catalog.art_bytes + } + } + return bytes } release := package_cached_releases[NodeAppIndex(slot)] if release.valid { @@ -442,7 +453,8 @@ NodeAppOfferSize :: (index: s32) -> s64 { if index < 0 || index >= node_offer_count { return 0 } - return node_offers[index].size + embedded := NodeAppEmbeddedSize(NodeAppOfferName(index)) + return ifx embedded > 0 then embedded else node_offers[index].size } // Every published offer is visible unless another app replaced it. @@ -572,6 +584,7 @@ NodeAppsInitialize :: () { } node_initialized = true NodeAppLoadOffers() + NodeAppEmbeddedOffers() NodeAppLocalLoad() slot: s32 = 0 while slot < NodeAppSlots { @@ -1715,6 +1728,7 @@ NodeAppCatalogFinish :: () { if target == node_offer_count { node_offer_count += 1 } } NodeAppSaveOffers() + NodeAppEmbeddedOffers() NodeAppLocalOffer() node_catalog_phase = 0 } diff --git a/src/apps/node_child_packages.zi b/src/apps/node_child_packages.zi index 22c10692b..78571943f 100644 --- a/src/apps/node_child_packages.zi +++ b/src/apps/node_child_packages.zi @@ -232,6 +232,25 @@ NodeChildPump :: () { if node_child_work == null { NodeChildFinish(false) } return } + // Root packages can carry a collection's digest-bound library art. + // Keep the normal child verifier and memory limit; games still + // follow the existing consented download path. + embedded_path: Vec(u8) + BuilderPrint(*embedded_path, "assets/apps/bundled/%/%.zx", NodeAppName(child.root), + TextUntilNul(child.digest[:])) + embedded_name := BuilderFinish(embedded_path) + embedded := AssetRead(embedded_name) + PackageFree(embedded_name) + if embedded.found { + node_child_work = PackageWorkEmbedded(embedded.data, TextUntilNul(child.account[:]), + PackageID(NodeAppIndex(child.root))) + if node_child_work != null { + node_child_work.path = PackageText(child.path, "") + } else { + NodeChildFinish(false) + } + return + } url: Vec(u8) BuilderPrint(*url, "%/api/v2/packages/%/%.zx", NodeChildOrigin(child.root), PackageID(NodeAppIndex(child.root)), TextUntilNul(child.digest[:])) diff --git a/src/apps/package_work.zi b/src/apps/package_work.zi index d9e4d4d22..ba470b8e0 100644 --- a/src/apps/package_work.zi +++ b/src/apps/package_work.zi @@ -106,3 +106,21 @@ PackageWorkCreate :: (path: string, immutable: string, account: string, } return work } + +// Root assets have already crossed the host's trust boundary. Own a copy so +// replacing the root cannot invalidate a preparation still using its bytes. +PackageWorkEmbedded :: (bytes: string, account: string, identity: string) -> *PackageWork { + work := cast(*PackageWork)PackageWorkAllocate(size_of(PackageWork)) + if work == null { + return null + } + work.* = PackageWork.{} + work.bytes = PackageText(bytes, "") + work.account = PackageText(account, "") + work.identity = PackageText(identity, "") + payload := cast(*TextDescriptor)(*work.bytes) + Sha256Hex(payload.data, work.bytes.count, *work.digest[0]) + work.ready = true + work.ok = work.bytes.count > 0 + return work +} diff --git a/tests/node_app_embedded_behavior.zi b/tests/node_app_embedded_behavior.zi new file mode 100644 index 000000000..9499b6313 --- /dev/null +++ b/tests/node_app_embedded_behavior.zi @@ -0,0 +1,187 @@ +// Trusted-root baselines use the same digest, API, publisher and identity +// boundaries as the loader, while stale signed metadata keeps its own key. +#import, file "../src/apps/node_app_embedded.zi"; +#import, file "../src/apps/node_app_open.zi"; +#import, file "../src/apps/node_app_tree.zi"; +#import, file "../src/apps/node_app_runner.zi"; +#import, file "../src/apps/node_apps.zi"; +#import, file "../src/apps/package_work.zi"; +#import, file "../src/apps/package_manager.zi"; +#import, file "../src/apps/package_release.zi"; +#import, file "../src/apps/package_files.zi"; +#import, file "../src/apps/package_history.zi"; +#import, file "../src/app/assets.zi"; +#import, file "../src/apps/catalog.zi"; +#import, file "node_app_icons_behavior.zi"; +#import "std/text_buffer" +#import "std/vec" +#import "std/c_string" +#import "std/file_linux" +#import "apps/tree" +#import "apps/host" +#import, file "../src/apps/node_child_packages.zi"; + +embedded_c :: #system_library "c"; +EmbeddedNow :: () -> float64 #foreign embedded_c "IconsNow"; +EmbeddedYield :: () #foreign embedded_c "IconsYield"; + +embedded_test_bytes: string; +embedded_test_metadata: string; +embedded_test_publishers: string; +embedded_test_arcade: string; +embedded_test_art: string; +embedded_test_controller: string; + +EmbeddedTestAsset :: (path: string) -> AssetView { + data := "" + if path == "assets/apps/bundled/fixture.zx" { data = embedded_test_bytes } + if path == "assets/apps/bundled/arcade.zx" { data = embedded_test_arcade } + if path == "assets/apps/bundled/arcade/bd0a779ce62c2324812926f1579a64881db309430cff839565b28d1bc5b6420f.zx" { + data = embedded_test_art + } + if path == "apps.zx" { data = embedded_test_controller } + if path == "assets/apps/bundled/releases.json" { data = embedded_test_metadata } + if path == "apps/publishers.json" { + data = embedded_test_publishers + } + return AssetView.{.found = data.count > 0, .data = data} +} + +EmbeddedTestMetadata :: (id: string, key: string, api: s32, size: s64, hash: string) { + PackageFree(embedded_test_metadata) + text: Vec(u8) + BuilderPrint(*text, "{\"fixture\":{\"id\":\"%\",\"key_id\":\"%\",\"host_api\":%,\"bytes\":%,\"sequence\":1,\"version\":\"1.0.0\",\"sha256\":\"%\",\"complete_bytes\":%}}", + id, key, api, size, hash, size) + embedded_test_metadata = BuilderFinish(text) +} + +NodeAppEmbeddedCheck :: () -> s32 { + slot := cast(s32)(NodeAppSlots - 2) + embedded_test_bytes = PackageRead(IconsEnvironmentPath("NODE_OPEN_LEAF_BUNDLE"), PackageMaximumBytes) + embedded_test_publishers = PackageRead(IconsEnvironmentPath("NODE_EMBEDDED_PUBLISHERS"), 16384) + hash := IconsEnvironmentPath("NODE_OPEN_LEAF_SHA256") + if embedded_test_bytes.count < 8 { return 160 } + AssetBindProvider(EmbeddedTestAsset) + defer { + AssetUnbindProvider() + PackageFree(embedded_test_bytes) + PackageFree(embedded_test_metadata) + PackageFree(embedded_test_publishers) + PackageFree(embedded_test_arcade) + PackageFree(embedded_test_art) + PackageFree(embedded_test_controller) + embedded_test_bytes = "" + embedded_test_metadata = "" + node_runners[slot].embedded_release = PackageRelease.{} + package_cached_releases[NodeAppIndex(slot)] = PackageRelease.{} + NodeAppAssign(slot, "") + } + NodeAppAssign(slot, "fixture") + EmbeddedTestMetadata("inbe.fixture", "baseline", 41, embedded_test_bytes.count, hash) + if !NodeAppEmbeddedRelease("fixture").valid || !NodeAppEmbedded("fixture").found || + !NodeAppInstalled(slot) { return 161 } + keys: [2]string = .["unknown", "outside"] + for key: keys { + EmbeddedTestMetadata("inbe.fixture", key, 41, embedded_test_bytes.count, hash) + if NodeAppEmbeddedRelease("fixture").valid { return 162 } + } + apis: [2]s32 = .[40, 65535] + for api: apis { + EmbeddedTestMetadata("inbe.fixture", "baseline", api, embedded_test_bytes.count, hash) + if NodeAppEmbeddedRelease("fixture").valid { return 163 } + } + EmbeddedTestMetadata("inbe.someone-else", "baseline", 41, embedded_test_bytes.count, hash) + if NodeAppEmbeddedRelease("fixture").valid { return 164 } + EmbeddedTestMetadata("inbe.fixture", "baseline", 41, PackageMaximumBytes + 1, hash) + if NodeAppEmbeddedRelease("fixture").valid { return 165 } + EmbeddedTestMetadata("inbe.fixture", "baseline", 41, embedded_test_bytes.count, + "0000000000000000000000000000000000000000000000000000000000000000") + if !NodeOpenBegin(slot) || node_open_work.ok { return 166 } + if !NodeOpenDiscard() { return 167 } + + // Invalid/missing current descriptors cannot make fallback bytes borrow + // a different cached publisher. Pending work cannot relabel a live app. + EmbeddedTestMetadata("inbe.fixture", "baseline", 41, embedded_test_bytes.count, hash) + previous: PackageRelease + previous.valid = true + unused CopyCString("other", previous.key_id[:]) + package_cached_releases[NodeAppIndex(slot)] = previous + node_runners[slot].embedded_release = previous + if !NodeOpenBegin(slot) || !node_open_work.ok || + TextUntilNul(node_open_work.release.key_id[:]) != "baseline" { return 168 } + bound := NodeTreeRelease(slot) + if TextUntilNul(bound.key_id[:]) != "other" { return 168 } + if !NodeOpenDiscard() { return 169 } + node_runners[slot].embedded_release = PackageRelease.{} + // Open the exact portable identity through production preparation and + // activation, then verify the publisher remains bound after job disposal. + if !NodeAppOpen(slot) { return 170 } + deadline := EmbeddedNow() + 10.0 + while !NodeAppRunning(slot) && EmbeddedNow() < deadline { + unused NodeOpenPump(true) + EmbeddedYield() + } + bound = NodeTreeRelease(slot) + if !NodeAppRunning(slot) || TextUntilNul(bound.key_id[:]) != "baseline" { + return 171 + } + if !NodeAppClose(slot) { return 172 } + + // A genuine newer signed installation wins over the root baseline. + metadata := PackageRead(IconsEnvironmentPath("NODE_EMBEDDED_UPDATE"), 65536) + defer { PackageFree(metadata) } + release := PackageTrustedRelease(metadata, NodeAppIndex(slot)) + if !release.valid || TextUntilNul(release.version[:]) != "2.0.0" { return 173 } + current := PackagePath(NodeAppIndex(slot), ".current.json") + payload := PackagePayloadPath(NodeAppIndex(slot), TextUntilNul(release.hash[:])) + updated := PackageRead(IconsEnvironmentPath("NODE_OPEN_ROOT_BUNDLE"), PackageMaximumBytes) + defer { + unused RemoveFile(current) + unused RemoveFile(payload) + PackageFree(current) + PackageFree(payload) + PackageFree(updated) + } + if !PackageWrite(current, metadata) || !PackageWrite(payload, updated) { return 174 } + if !NodeOpenBegin(slot) || node_open_embedded || + TextUntilNul(node_open_work.release.version[:]) != "2.0.0" || + TextUntilNul(node_open_work.release.key_id[:]) != "other" { return 175 } + deadline = EmbeddedNow() + 10.0 + while !PackageWorkReady(node_open_work) && EmbeddedNow() < deadline { EmbeddedYield() } + if !node_open_work.ok { return 176 } + if !NodeOpenDiscard() { return 177 } + // The actual Arcade collection opens using this host's existing trusted + // controller. Its complete library art resolves offline; games stay absent. + PackageFree(embedded_test_metadata) + embedded_test_metadata = PackageRead(IconsEnvironmentPath("NODE_EMBEDDED_REAL_METADATA"), 16384) + embedded_test_arcade = PackageRead(IconsEnvironmentPath("NODE_EMBEDDED_ARCADE"), PackageMaximumBytes) + embedded_test_art = PackageRead(IconsEnvironmentPath("NODE_EMBEDDED_ART"), PackageMaximumBytes) + embedded_test_controller = PackageRead(IconsEnvironmentPath("NODE_EMBEDDED_CONTROLLER"), PackageMaximumBytes) + NodeAppAssign(slot, "arcade") + package_cached_releases[NodeAppIndex(slot)] = PackageRelease.{} + if !NodeAppOpen(slot) { return 178 } + deadline = EmbeddedNow() + 10.0 + while !NodeAppRunning(slot) && EmbeddedNow() < deadline { + unused NodeOpenPump(true) + EmbeddedYield() + } + host := node_runners[slot].nested + if host == null { return 179 } + root := AppTreeNode(*host.tree, AppHandle.{0, 1}) + if root == null || root.catalog.count != 5 || NodeAppPackageSize(slot) != 1519758 { return 180 } + path := "assets/arcade/covers/shovey-portrait.jpg" + deadline = EmbeddedNow() + 10.0 + while !AppHostAsset(root, path).found && EmbeddedNow() < deadline { + NodeChildPump() + EmbeddedYield() + } + if !AppHostAsset(root, path).found || node_child_memory != 1512084 { return 181 } + child := root.catalog.children + while child != null { + if AppHostAsset(root, TextUntilNul(child.entry.bundle[:])).found { return 182 } + if !AppHostAsset(root, TextUntilNul(child.entry.cover.portrait[:])).found { return 183 } + child = child.next + } + if !NodeAppClose(slot) { return 184 } + return 0 +} diff --git a/tests/node_app_snapshot_behavior.zi b/tests/node_app_snapshot_behavior.zi index f9c8ff81f..1b43e6ac1 100644 --- a/tests/node_app_snapshot_behavior.zi +++ b/tests/node_app_snapshot_behavior.zi @@ -12,6 +12,7 @@ #import, file "delegate_account_host.zi"; #import, file "node_app_icons_behavior.zi"; #import, file "node_tree_images_behavior.zi"; +#import, file "node_app_embedded_behavior.zi"; #import, file "background_audio_stub.zi"; #import, file "../src/apps/node_app_snapshot.zi"; #import, file "../src/apps/node_app_tree.zi"; @@ -321,6 +322,8 @@ Check :: () -> s32 { if images != 0 { return images } packages := SnapshotPackageOwnerCheck() if packages != 0 { return packages } + embedded := NodeAppEmbeddedCheck() + if embedded != 0 { return embedded } storage_close() return 0 } diff --git a/tests/node_app_snapshot_test.py b/tests/node_app_snapshot_test.py index c8557abfb..8136290e5 100644 --- a/tests/node_app_snapshot_test.py +++ b/tests/node_app_snapshot_test.py @@ -2,6 +2,7 @@ import argparse import ctypes.util import hashlib +import json import os from pathlib import Path import shutil @@ -10,6 +11,10 @@ import struct import subprocess import tempfile import zlib +import sys + +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey def main(): @@ -83,6 +88,25 @@ Main :: () -> s32 { command += ['--asset-dir', f'assets/fixture={assets}'] command.append(str(guest)) subprocess.run(command, cwd=root, env=environment, check=True) + sys.path.insert(0, str(root / 'build/packages/apps/scripts')) + from node_release import signing_message + key = Ed25519PrivateKey.generate() + public = key.public_key().public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw).hex() + publishers = [dict(key_id='baseline', public_key='1' * 64, app_ids=['inbe.fixture']), + dict(key_id='other', public_key=public, app_ids=['inbe.fixture']), + dict(key_id='outside', public_key='3' * 64, app_ids=['inbe.someone-else'])] + publishers += json.loads((root / 'apps/publishers.json').read_text()) + (fixture / 'publishers.json').write_text(json.dumps(publishers)) + sys.path.insert(0, str(root / 'scripts')) + from practice_content import bundle_assets + root_assets = bundle_assets((root / 'build/inbe.zx').read_bytes()) + (fixture / 'embedded-releases.json').write_bytes(root_assets['assets/apps/bundled/releases.json']) + release = dict(app_id='inbe.fixture', sequence=2, version='2.0.0', runtime='kryon-app-v1', + host_api=41, module_api=2, data_schema=0, key_id='other', dependencies=[], + artifacts=[dict(variant='module', sha256=hashlib.sha256(packed_root.read_bytes()).hexdigest(), + size=packed_root.stat().st_size)]) + release['signature'] = key.sign(signing_message(release)).hex() + (fixture / 'update.json').write_text(json.dumps(release)) subprocess.run([str(args.ziran.resolve()), 'build', '--target=c', '--define', 'PLATFORM_DESKTOP', '--no-main', '--root', str(root / 'tests'), *module_paths, *package_paths, '-o', str(generated), str(root / 'tests/node_app_snapshot_behavior.zi'), @@ -115,6 +139,7 @@ Main :: () -> s32 { if result.returncode: raise SystemExit('Snapshot native link failed; see compiler diagnostics above') with tempfile.TemporaryDirectory(prefix='profile-', dir=work) as profile: + (Path(profile) / 'packages').mkdir() subprocess.run([str(work / 'test')], env=environment | { 'APP_DATA_ROOT': profile, 'ICON_CACHE_PATH': str(Path(profile) / 'fixture.icon'), @@ -122,10 +147,18 @@ Main :: () -> s32 { 'NODE_OPEN_ROOT_BUNDLE': str(packed_root), 'NODE_OPEN_ROOT_SHA256': hashlib.sha256(packed_root.read_bytes()).hexdigest(), 'NODE_OPEN_LEAF_SHA256': hashlib.sha256(leaf.read_bytes()).hexdigest(), + 'NODE_OPEN_LEAF_BUNDLE': str(leaf), + 'NODE_EMBEDDED_PUBLISHERS': str(fixture / 'publishers.json'), + 'NODE_EMBEDDED_UPDATE': str(fixture / 'update.json'), + 'NODE_EMBEDDED_REAL_METADATA': str(fixture / 'embedded-releases.json'), + 'NODE_EMBEDDED_ARCADE': str(root / 'apps/bundled/arcade.zx'), + 'NODE_EMBEDDED_ART': str(root / 'apps/bundled/arcade.games/bd0a779ce62c2324812926f1579a64881db309430cff839565b28d1bc5b6420f.zx'), + 'NODE_EMBEDDED_CONTROLLER': str(root / 'build/apps-ui/apps.zx'), **{f'ICON_{name.upper()}_BUNDLE': str(fixture / f'{name}.zx') for name in ('valid', 'invalid', 'empty', 'corrupt')}, }, check=True) print('Installed apps: leaf discovery, background icons, stale-job isolation, deferred images and shared mappings passed') + print('Embedded apps: identity, publisher scope, API, size and digest rejection; fallback publisher binding; newer signed update precedence; actual Arcade with complete art and no games opened offline') if __name__ == '__main__': diff --git a/tests/small_app_bundling_test.py b/tests/small_app_bundling_test.py new file mode 100644 index 000000000..cb70473ff --- /dev/null +++ b/tests/small_app_bundling_test.py @@ -0,0 +1,173 @@ +"""The size rule adds offline apps without removing the shipped baseline.""" +from hashlib import sha256 +import json +from pathlib import Path +import sys +import tempfile +import unittest + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts')) +from small_app_bundling import inspect_bundle, stage, MANDATORY, LIMIT_BYTES +from node_app_games_test import fixture_bundle + +ROOT = Path(__file__).resolve().parents[1] + + +class BundlingTests(unittest.TestCase): + def setUp(self): + work = ROOT / 'work/small-app-bundling' + work.mkdir(parents=True, exist_ok=True) + self.temporary = tempfile.TemporaryDirectory(dir=work) + self.addCleanup(self.temporary.cleanup) + self.root = Path(self.temporary.name) + self.bundles = self.root / 'apps' + self.nodes = self.root / 'node-apps' + self.bundles.mkdir() + self.nodes.mkdir() + # Mandatory apps larger than the limit must remain byte-identical. + self.old = b'previously shipped' * 65000 + for name in MANDATORY: + (self.bundles / (name + '.zx')).write_bytes(self.old) + self.ledger = dict(node_apps={}) + self.optional = self.root / 'optional' + self.mandatory = self.root / 'mandatory' + + def candidate(self, name, size): + empty = fixture_bundle({'assets/payload': b''}) + data = fixture_bundle({'assets/payload': b'x' * (size - len(empty))}) + self.assertEqual(len(data), size) + path = self.nodes / (name + '.zx') + path.write_bytes(data) + self.ledger['node_apps'][name] = dict(version='1.0.0', host_api=41, sequence=1) + return path + + def test_strict_decimal_boundary_and_additive_baseline(self): + self.candidate('small', LIMIT_BYTES - 1) + self.candidate('equal', LIMIT_BYTES) + self.candidate('large', LIMIT_BYTES + 1) + report = stage(self.ledger, {}, self.bundles, self.optional, self.mandatory) + self.assertEqual(report['mandatory'], list(MANDATORY)) + self.assertEqual([p.name for p in sorted(self.optional.glob('*.zx'))], ['small.zx']) + self.assertEqual((self.optional / 'catalog.txt').read_text(), 'small\n') + for name in MANDATORY: + self.assertEqual((self.mandatory / (name + '.zx')).read_bytes(), self.old) + + def test_external_art_counts_and_explicit_app_is_retained(self): + path = self.candidate('collection', 100) + art = fixture_bundle({'assets/collection/cover.png': b'x' * LIMIT_BYTES}) + digest = sha256(art).hexdigest() + game = dict(bundle='assets/collection/game.zx', sha256='a' * 64, bytes=20_000_000) + catalog = dict(children=[game], art=dict(sha256=digest, bytes=len(art))) + path.write_bytes(fixture_bundle({'assets/apps/catalog.json': json.dumps(catalog).encode()})) + resources = path.parent / 'collection.games' + resources.mkdir() + (resources / (digest + '.zx')).write_bytes(art) + report = stage(self.ledger, {}, self.bundles, self.optional, self.mandatory) + info = report['optional']['collection'] + self.assertGreater(info['complete_bytes'], LIMIT_BYTES) + self.assertFalse(info['included']) + self.assertEqual(info['optional_download_bytes'], 20_000_000) + self.assertEqual(info['required_art_bytes'], len(art)) + declaration = dict(path=str(path), id='inbe.collection', version='1.0.0', host_api=41, + bytes=path.stat().st_size, sha256=sha256(path.read_bytes()).hexdigest(), bundled=True) + report = stage({}, {'collection': declaration}, self.bundles, self.optional, self.mandatory) + self.assertTrue(report['optional']['collection']['included']) + self.assertEqual((self.optional / 'collection' / (digest + '.zx')).read_bytes(), art) + (resources / (digest + '.zx')).write_bytes(b'wrong artwork') + with self.assertRaisesRegex(ValueError, 'missing required artwork'): + stage({}, {'collection': declaration}, self.bundles, self.optional, self.mandatory) + + def test_reviewed_arcade_catalog_and_art_are_exact_without_games(self): + declarations = json.loads((ROOT / 'apps/bundled.json').read_text()) + item = declarations['arcade'] + path = ROOT / item['path'] + info = inspect_bundle(path) + self.assertEqual(info['bundle_bytes'], 7674) + self.assertEqual(info['sha256'], item['sha256']) + self.assertEqual(info['complete_bytes'], 1_519_758) + info = stage({}, declarations, self.bundles, self.optional, self.mandatory)['optional']['arcade'] + self.assertTrue(info['included']) + self.assertEqual(len(list((self.optional / 'arcade').glob('*.zx'))), 1) + self.assertEqual(sum(info['optional_downloads'].values()), 65_639_505) + item['sha256'] = '0' * 64 + with self.assertRaisesRegex(ValueError, 'reviewed declaration'): + stage({}, declarations, self.bundles, self.optional, self.mandatory) + + def test_missing_inline_cover_cannot_qualify_as_complete(self): + path = self.candidate('incomplete', 100) + catalog = dict(children=[dict(bundle='assets/incomplete/game.zx', sha256='a' * 64, + bytes=100, cover=dict(portrait='assets/incomplete/missing.png'))]) + path.write_bytes(fixture_bundle({'assets/apps/catalog.json': json.dumps(catalog).encode()})) + report = stage(self.ledger, {}, self.bundles, self.optional, self.mandatory) + self.assertFalse(report['optional']['incomplete']['included']) + + def test_unhandled_required_dependencies_and_child_cycles_fail(self): + path = self.candidate('collection', 100) + for catalog in (dict(dependencies=['required-large-library']), + dict(children=[dict(id='one', dependencies=['missing'])]), + dict(children=[dict(id='one', dependencies=['two']), + dict(id='two', dependencies=['one'])])): + with self.subTest(catalog=catalog): + path.write_bytes(fixture_bundle({'assets/apps/catalog.json': json.dumps(catalog).encode()})) + with self.assertRaises(ValueError): + inspect_bundle(path) + + def test_art_bundle_must_contain_declared_covers(self): + path = self.candidate('collection', 100) + art = fixture_bundle({}) + digest = sha256(art).hexdigest() + catalog = dict(children=[dict(bundle='game.zx', sha256='a' * 64, bytes=100, + cover=dict(portrait='missing.png'))], + art=dict(sha256=digest, bytes=len(art))) + path.write_bytes(fixture_bundle({'assets/apps/catalog.json': json.dumps(catalog).encode()})) + resources = path.parent / 'collection.games' + resources.mkdir() + (resources / (digest + '.zx')).write_bytes(art) + report = stage(self.ledger, {}, self.bundles, self.optional, self.mandatory) + self.assertFalse(report['optional']['collection']['included']) + + def test_reused_staging_drops_only_excluded_optional_inventory(self): + self.candidate('small', LIMIT_BYTES - 1) + stage(self.ledger, {}, self.bundles, self.optional, self.mandatory) + self.assertTrue((self.optional / 'small.zx').exists()) + self.candidate('small', LIMIT_BYTES) + stage(self.ledger, {}, self.bundles, self.optional, self.mandatory) + self.assertFalse((self.optional / 'small.zx').exists()) + self.candidate('small', LIMIT_BYTES - 1) + stage(self.ledger, {}, self.bundles, self.optional, self.mandatory) + stage({}, {}, self.bundles, self.optional, self.mandatory) + self.assertFalse((self.optional / 'small.zx').exists()) + for name in MANDATORY: + self.assertEqual((self.mandatory / (name + '.zx')).read_bytes(), self.old) + + def test_bad_art_metadata_never_becomes_a_filesystem_path(self): + path = self.candidate('collection', 100) + for digest, size in [('../outside', 10), ('a' * 64, -1), ('a' * 64, True), ('a' * 64, 100_000_000)]: + catalog = dict(children=[], art=dict(sha256=digest, bytes=size)) + path.write_bytes(fixture_bundle({'assets/apps/catalog.json': json.dumps(catalog).encode()})) + with self.assertRaisesRegex(ValueError, 'invalid catalog payload'): + stage(self.ledger, {}, self.bundles, self.optional, self.mandatory) + + def test_inline_art_must_match_digest_and_resolve_covers(self): + path = self.candidate('collection', 100) + art = fixture_bundle({'assets/collection/portrait.png': b'image'}) + catalog = dict(children=[dict(bundle='game.zx', sha256='a' * 64, bytes=100, + cover=dict(portrait='assets/collection/portrait.png'))], + art=dict(sha256=sha256(art).hexdigest(), bytes=len(art))) + def pack(): + path.write_bytes(fixture_bundle({'assets/apps/catalog.json': json.dumps(catalog).encode(), + 'assets/apps/art.zx': art})) + pack() + info = inspect_bundle(path) + self.assertTrue(info['complete']) + self.assertEqual(info['complete_bytes'], path.stat().st_size) + catalog['children'][0]['cover']['portrait'] = 'missing.png' + pack() + self.assertFalse(inspect_bundle(path)['complete']) + catalog['art']['sha256'] = '0' * 64 + pack() + self.assertFalse(inspect_bundle(path)['complete']) + + +if __name__ == '__main__': + unittest.main()