Skip to content

Persist prepared bindings and assemble apps with them - #2

Open
noise64 wants to merge 20 commits into
mainfrom
prepared-assets
Open

noise64 wants to merge 20 commits into
mainfrom
prepared-assets

Conversation

@noise64

@noise64 noise64 commented Nov 16, 2026 •

Copy link
Copy Markdown

Touches GOL-1850 (PY-02): the separate prepare and assemble steps that Golem's Python build uses. It does not resolve the issue.

Persisted prepare/assemble API for the Golem Python SDK. It builds on #1, which is merged.

  • Bind, link and initialize are separate steps. ComponentGenerator::generate binds the WIT world, links the libraries and initializes the result. Initialization follows an InitPlan: owned import stubs instead of the borrowed Resolve, the environment, the preopens, and the entropy derived from the linked component, the app name and the Python path files. The Python path search sorts directory entries, so the library order does not depend on the file system.
  • Prepared assets. BindingsPreparer::prepare resolves a WIT world once and returns Prepared: the bindings module, the generated Python bindings, the runtime libraries, the WASI adapter, the standard library, and the symbols and import stubs for initialization. Prepared::write stores content-addressed blobs and a manifest. The SHA-256 of the manifest is the identity. Prepared::read rejects a changed or missing blob, an extra file, a non-canonical manifest, another format and another initialization WIT. The initialization WIT digest normalizes CRLF, so a checkout that converts line endings builds a matching assembler. Unpacking a blob accepts only regular files and directories, so a link in a blob cannot point the build-time mtime update at host files.
  • Assembly. Assembler::assemble links the prepared assets with an app and its native extensions, without WIT processing or code generation. Each Python path entry has a mount name, and the library name of an extension is /{name}/{path}, so the names do not depend on the order of the entries. An app that carries a componentize-py.toml file is rejected, because it would change the bindings. Assembler::output names the file that the caller writes. The build digest skips it, as componentize skips its output, so an output inside a Python path entry does not change the next build.
  • Caller-supplied store and linker. Assembly::initialize takes the caller's Store and Linker, built on the caller's engine with its own WASI implementation, as InitPlan describes: environment, preopens, entropy and build clocks. InitPlan::wasi_builder returns a WASI context builder with these settings. It opens each preopen read-only, so initialization cannot change the directories that it reads: a write fails in the guest with an error that Python can catch. add_trapping_stubs adds the plan's import stubs to any linker. The crate does not choose engine settings or WASI for the caller. componentize and assemble keep this crate's WASI setup and default_engine. Wasmtime moves to 50.0.1, the version that Golem's engine uses; this change alone needs no source change.
  • Assembler without the runtime. The embedded-runtime feature, on by default, builds CPython and the runtime into the crate. Without the feature, the crate reads and assembles prepared assets, which carry their own runtime, and does not build CPython. Preparing and componentize are then not compiled. The crate still depends on wasmtime-wasi, with only its p2 feature, for InitPlan::wasi_builder. The binary requires the feature.
  • CLI. componentize-py prepare <dir> prints the identity. componentize-py assemble --prepared <dir> [--expect-identity <id>] <app> -p [name=]dir links and initializes. It passes its -o file as the output, and creates the file's directory before the digest.

Tests in tests/componentize.rs prepare once and assemble two unrelated apps in separate processes, check that the prepared files do not change, and check that rejected assets do not import the app: the app raises a marker error on import, and the error appears only for intact assets. Another test tries to write into the app directory during import, and checks that each write fails, that the directory does not change and that the component runs. A third test assembles twice with the default Python path and output, so the second build has the first output in the app directory. The app prints a random value that it draws on import, and the value must stay the same; a changed source must change it.

Known gaps: the embedded-runtime check at the start of bind cannot fail, because bind exists only with the feature. No test covers support-library linking with prepared assets, finalization of exported resources, a non-canonical manifest, a missing blob or another format; the digest-mismatch test corrupts the first blob that read_dir returns.

Not solved here: two assemblies of the same input can still differ in a few bytes. The difference follows host metadata of the build directories that assembly creates.

Verification

On this head, 45416d0 (Linux x86_64, local).

  • cargo test --release -- --skip http_p3_example: passed (123 lib, 21 bindings, 14 componentize tests).
  • bash .github/workflows/lint.sh: passed.
  • Red controls: with the preopens changed back to read-write (at 2597911), assemble_leaves_the_app_directory_unchanged fails, because the import writes /app/writer.py. With None instead of the output in the digest, assemble_skips_its_output_in_the_build_digest fails with two different values.
  • Not run: http_p3_example, because its port is taken on the build machine.
  • CI: the PR now targets main, so the Test workflow runs on each head. It passed on Linux, macOS and Windows at 45416d0 (run 47111397895).
  • Not covered: byte-identical assembly (above), and Wasm exceptions on Golem's shared engine.

ComponentGenerator::generate now binds the WIT world, links the libraries and
initializes the result as separate steps. Initialization follows an InitPlan:
owned import stubs instead of the borrowed Resolve, the environment,
preopens and entropy. The linker takes the adapter as input, and the Python
path search sorts directory entries so that the library order does not
depend on the file system.
BindingsPreparer resolves a WIT world once and returns Prepared assets: the
bindings module, the generated Python bindings, the runtime libraries, the
standard library, and the symbols and import stubs that initialization needs.
Prepared::write stores them as content-addressed blobs and a manifest whose
digest is their identity. Prepared::read verifies every blob, the manifest
form and the initialization WIT before use.

Assembler links prepared assets with an app and its native extensions
without WIT processing. Each Python path entry has a mount name, so the
library names of extensions do not depend on the order of the entries. An
app that carries a componentize-py.toml file is rejected, because it would
change the bindings. Assembly::initialize_with lets the caller supply the
engine that runs initialization.

The prepare and assemble subcommands expose this from the command line.
Golem's engine uses Wasmtime 50.0.1, so the crate can run initialization on
an engine that Golem configures.
Assembly::initialize takes an Initializer: the engine, an optional linker
setup and an optional store setup, such as fuel or an epoch deadline that the
engine requires. componentize and assemble use default_engine.

The embedded-runtime feature, on by default, builds CPython and the runtime
into the crate. Without it the crate reads and assembles prepared assets, which
carry their own runtime, and preparing or componentize fail with a clear
error. The binary requires the feature.
Golem's Wasmtime has its own WASI implementation, so the crate cannot build the
WASI context for it. Assembly::initialize now takes the caller's store and
linker, and add_trapping_stubs adds the plan's import stubs to any linker.
initialize_component runs initialization for either. Without the
embedded-runtime feature the crate does not depend on wasmtime-wasi, and
Assembly::initialize_default keeps the crate's own WASI setup for the CLI.

Allow the deprecated filetime::set_file_times call: newer filetime releases
deprecate it, and the std replacement does not set directory times on every
platform.
Without the embedded-runtime feature the crate only reads, assembles and
initializes prepared assets, so the WIT generator, registry client, CLI and
async runtime become optional dependencies of that feature. An embedder that
links the assembler then pulls in fewer crates.
Unpacking a prepared blob created any symlink or hard link in it, and
setting the build mtimes then followed the link to host files.
Preparation writes only files and directories, so unpacking now rejects
every other entry.

The digest of the initialization WIT now normalizes CRLF, so assets
prepared on one platform match an assembler built from a checkout that
converted line endings.

InitPlan::new takes the build entropy from its caller.
@noise64
noise64 marked this pull request as ready for review November 30, 2026 12:41

vigoo commented Dec 1, 2026

Copy link
Copy Markdown

Review findings for the delta from runtime-baseline (7389c59) to this head (cad25ee), excluding PR #1.

No confirmed runtime defect found. Two findings remain:

  1. Medium — test evidence gap: caller-owned initialization is not exercised. src/prepared.rs:487–495 exposes Assembly::initialize(Store<T>, Linker<T>), but the new integration tests in tests/componentize.rs:515–543 invoke CLI assemble, which uses initialize_default. They do not verify constructing the caller’s WASI context from InitPlan, using a caller-owned engine/store/linker, or preserving resource/async imports through persisted assets on that route. Add an actual prepare → write/read → assemble → custom-initialize → invoke test, preferably covering resource/async signatures, before treating the Golem integration as qualified. This is a coverage gap, not evidence that the implementation fails.

  2. Low, non-blocking — document the complete engine requirements. src/lib.rs:1411–1414 lists component-model and async support, but omits wasm_component_model_map, which default_engine enables. Add that requirement to the public initialization contract. The PR description already acknowledges this omission.

The separation into owned prepared assets, linking, and caller-owned initialization fits the intended integration. The review traced canonical manifest and blob validation, archive extraction, named native-library mounts, support-library resolution, and preservation of WASI/stub behavior and temporary-directory lifetimes. No additional architectural objection was found.

Independent checks passed: cargo fmt --all -- --check, git diff --check, cargo check --locked --no-default-features --lib, and cargo test --locked --no-default-features --lib (with COMPONENTIZE_PY_TEST_COUNT=0; the archive test covers regular-file acceptance and symlink/hardlink rejection).

Limits: CPython was not rebuilt and embedded-runtime integration tests were not independently rerun. Custom initialization, prepared resource finalization/support-library execution, and byte-identical assembly remain unverified. The reported author-run tests are not independent verification.

@noise64
noise64 marked this pull request as draft December 1, 2026 14:30

noise64 commented Dec 1, 2026

Copy link
Copy Markdown
Author
  1. Low, non-blocking — document the complete engine requirements.

Fixed in bbed742.

noise64 commented Dec 2, 2026

Copy link
Copy Markdown
Author
  1. Medium — test evidence gap: caller-owned initialization is not exercised.

Added in 40a5548. prepared_route::caller_owned_initialization prepares a world that imports a resource and an async function and exports the same, writes and reads the assets, assembles an app, builds the WASI context from the InitPlan, and initializes with its own engine, store and linker through Assembly::initialize. It then calls the exported resource on two instances, interleaved, and the async export, which awaits the host's async import.

@noise64
noise64 marked this pull request as ready for review December 2, 2026 10:53

vigoo commented Dec 2, 2026

Copy link
Copy Markdown

Follow-up review at 40a5548, against the updated runtime-baseline base (87f80f2), excluding PR #1.

The engine-documentation finding is resolved: initialize_component now names component-model map support.

The new prepared_route::caller_owned_initialization test substantially addresses the initialization coverage finding. It persists and reloads assets, constructs a caller-owned store and linker, initializes the component, and verifies interleaved resource state and an awaited async host call. Independent execution with WASI SDK 34 passed: cargo test --locked --lib test::tests::prepared_route::caller_owned_initialization -- --exact --nocapture (1 passed, 0 failed).

Medium — one narrower test-fidelity gap remains: the initialization WASI setup copies plan.env and plan.preopens, but does not apply plan.entropy or the prescribed zero-origin, stepping wall and monotonic clocks. Wasmtime 50.0.2 WasiCtxBuilder::new defaults to host clocks and random sources. This differs from the documented InitPlan contract and from the default initializer in src/lib.rs:1368–1373.

The passing test therefore establishes caller-owned initialization and resource/async operation, but not faithful construction of the build-time WASI context. Configure the secure/insecure random streams and seed from plan.entropy, configure the specified build clocks, and assert observable import-time captured values so reverting to host defaults fails. This need not assert byte-identical whole-Wasm output. This is a test gap, not a confirmed production defect in Assembly::initialize.

Formatting and diff-whitespace checks also passed. The full suites and Golem’s own WASI implementation were not exercised. The initial build attempt encountered an older SDK missing noeh/libc++.so; the successful test above used SDK 34 with a rebuilt WASI CPython tree.

noise64 commented Dec 2, 2026

Copy link
Copy Markdown
Author

Medium — one narrower test-fidelity gap remains:

Fixed in 07fbd4e. The new InitPlan::wasi_builder returns a WasiCtxBuilder with the plan's environment variables, preopens and entropy, and the build clocks; the caller sets stdin, stdout and stderr. The default initializer and caller_owned_initialization both use it. The test application saves os.urandom(8), time.time() and time.monotonic() at import. The test checks that the saved wall-clock time is under one second, and that all three values equal those of initialize_default on the same assembly. With the old setup, which copies only the environment and preopens, the test fails because the saved time is the host's current time. Golem's assembler now uses wasi_builder instead of its own copy of this setup.

@vigoo vigoo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initialization findings are resolved. The caller-owned and default initializers now use InitPlan::wasi_builder; the test reads back random bytes and both clocks captured at Python import. The targeted test passed with WASI SDK 34, and the library builds with --no-default-features.

One non-blocking correction to the PR description: it says disabling embedded-runtime removes the wasmtime-wasi dependency. cargo tree --no-default-features shows that wasmtime-wasi is now a direct dependency with p2 enabled. Please distinguish skipping the embedded CPython build from removing WASI, and remove the stale note that the engine docs omit map support.

@noise64
noise64 changed the base branch from runtime-baseline to main December 3, 2026 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants