You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(python): compile host preludes separately from user code - #3024
Host-provided Python preludes and user scripts now compile as separate modules in shared globals. Valid leading __future__ imports retain their semantics, and tracebacks retain each module's own line numbers. Initialization frames are named <bashkit prelude>; a collision with the user filename gets a distinct initialization label.
Both stages share one allocator meter, print buffer and invocation deadline. Every Python command still starts with a fresh namespace. The Monty fork adds a named-module feed so this uses the existing incremental execution engine without parsing or rewriting Python directives.
Why
The existing prelude API prepends initialization as text, making valid user code fail and shifting diagnostic lines. This affects any embedder using initialization helpers or namespaces. BashKit's own ToolRegistry also uses this API; its integration test now calls tools.orders.list(...) successfully after a future import.
Part of dobesv/harnx#2973. This PR contains only the generic BashKit fix.
Before / After
With a prelude, from __future__ import annotations previously failed with SyntaxError: from __future__ imports must occur at the beginning of the file.
It now works through stdin, -c and VFS files, with both python and python3. An annotated function calling a host function prints int and 42. Nested exec tracebacks also keep their own locations.
Validation:
Verified five regression failures on unchanged main before implementation.
Python-focused suite: 386 passed; final expanded prelude suite: 9 passed, including a VFS filename-collision regression verified to fail before the fix.
BashKit tool-registry integration suite: all 5 passed, including Python/TypeScript/shared-policy dispatch.
CLI smoke test prints int and 42. With patched curve25519-dalek, all 14 bot-auth signing/verification tests pass.
Python-feature Clippy, formatting, Monty fork drift, knowledge/doc checks and Cargo Vet pass locally. CI's all-feature Clippy and lean-build Clippy pass.
Existing Python call, new-session and parallel (1/16/64) benchmarks complete successfully. The benchmark executable now installs BashKit's required metered allocator; this fixes a pre-existing harness failure. These are smoke measurements, not a before/after performance comparison.
All final-head CI workflows pass: Audit, Lint, full interpreter/execution-plan test jobs, CPython on Linux/macOS/Windows, WASM, examples, all fuzz build/smoke modes, Python/JS bindings, coverage, both oracles, inventory and site.
Cleared the initial Audit blocker by updating the root lockfile to patched curve25519-dalek 5.1.0 (RUSTSEC-2026-0620), with a reviewed delta audit in supply-chain/audits.toml; Cargo Vet passes. Cargo also corrected tempfile's existing getrandom dependency edge during resolution. Advisory policy and all manifest constraints are unchanged. Local Cargo Deny still reports the existing chrono/chrono-tz unmaintained advisories.
The final shipping fixes also prevent traceback source-map collisions and update the vulnerable cryptography dependency. No Harnx changes are included.
Local gate notes: the complete Rust suite and 282 script tests pass under a temporary Linux child subreaper (this container's PID 1 is tail and does not reap orphaned fixture processes). just pre-pr then stops on the unchanged changelog policy mismatch: scripts/check_changelog.py rejects ### Breaking Changes, while knowledge/operations/release-process.md requires it and the existing 0.22.0 changelog contains it. These three files match main. The subsequent Monty fork and Cargo Vet gates pass separately. just test passes under the same reaping wrapper with localhost test-server access enabled, including all 19 serialized failpoint tests.
Risk
Medium: all Monty Python invocations now use the incremental engine, including commands without preludes. Existing Python security, cancellation, resource-limit and tenant-isolation coverage passes. Prelude traceback filenames change to <bashkit prelude>.
Verdict: No blockers. The new push addresses the one P2 from the previous review (python-prelude-filename-collision): run_python now picks "<bashkit prelude: initialization>" for the prelude module when the user script path is literally "<bashkit prelude>" (crates/bashkit/src/builtins/python.rs:467), with a comment explaining the sources-map keying, and a regression test asserts both filenames appear in the traceback (python_prelude_tests.rs, collision test — file now 210 lines vs 193).
Since last review: also picked up Cargo.lock / supply-chain/audits.toml checksum bumps for subtle-family crypto deps — rebase noise from main, unrelated to this change and benign. Docs/knowledge deltas otherwise unchanged in substance. Earlier P2 is resolved; no new findings, so no new review submitted.
Context read: refreshed checkout at d94f4a5; run_python module-assembly block, new collision test, audits.toml hunk, Cargo.lock hunks. Full diff not truncated. No build/test run (no cargo in this shell, reasoned from code).
The reason will be displayed to describe this comment to others. Learn more.
Separate compilation of the prelude is the right fix for the __future__ position bug, and budget sharing (deadline, heap, print cap) plus per-module nesting scans look correct. One minor robustness note below.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
Host-provided Python preludes and user scripts now compile as separate modules in shared globals. Valid leading
__future__imports retain their semantics, and tracebacks retain each module's own line numbers. Initialization frames are named<bashkit prelude>; a collision with the user filename gets a distinct initialization label.Both stages share one allocator meter, print buffer and invocation deadline. Every Python command still starts with a fresh namespace. The Monty fork adds a named-module feed so this uses the existing incremental execution engine without parsing or rewriting Python directives.
Why
The existing prelude API prepends initialization as text, making valid user code fail and shifting diagnostic lines. This affects any embedder using initialization helpers or namespaces. BashKit's own
ToolRegistryalso uses this API; its integration test now callstools.orders.list(...)successfully after a future import.Part of dobesv/harnx#2973. This PR contains only the generic BashKit fix.
Before / After
With a prelude,
from __future__ import annotationspreviously failed withSyntaxError: from __future__ imports must occur at the beginning of the file.It now works through stdin,
-cand VFS files, with bothpythonandpython3. An annotated function calling a host function printsintand42. Nestedexectracebacks also keep their own locations.Validation:
intand42. With patched curve25519-dalek, all 14 bot-auth signing/verification tests pass.curve25519-dalek 5.1.0(RUSTSEC-2026-0620), with a reviewed delta audit insupply-chain/audits.toml; Cargo Vet passes. Cargo also corrected tempfile's existing getrandom dependency edge during resolution. Advisory policy and all manifest constraints are unchanged. Local Cargo Deny still reports the existing chrono/chrono-tz unmaintained advisories.The final shipping fixes also prevent traceback source-map collisions and update the vulnerable cryptography dependency. No Harnx changes are included.
Local gate notes: the complete Rust suite and 282 script tests pass under a temporary Linux child subreaper (this container's PID 1 is
tailand does not reap orphaned fixture processes).just pre-prthen stops on the unchanged changelog policy mismatch:scripts/check_changelog.pyrejects### Breaking Changes, whileknowledge/operations/release-process.mdrequires it and the existing 0.22.0 changelog contains it. These three files match main. The subsequent Monty fork and Cargo Vet gates pass separately.just testpasses under the same reaping wrapper with localhost test-server access enabled, including all 19 serialized failpoint tests.Risk
Medium: all Monty Python invocations now use the incremental engine, including commands without preludes. Existing Python security, cancellation, resource-limit and tenant-isolation coverage passes. Prelude traceback filenames change to
<bashkit prelude>.Checklist