Skip to content

fix(python): compile host preludes separately from user code - #3024

Merged
chaliy merged 4 commits into
mainfrom
fix/python-prelude-future-imports
Nov 29, 2026
Merged

chaliy merged 4 commits into
mainfrom
fix/python-prelude-future-imports

Conversation

@chaliy

@chaliy chaliy commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

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 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>.

Checklist

  • Tests added or updated
  • Backward compatibility considered

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
bashkit d94f4a5 Commit Preview URL

Branch Preview URL
Nov 29 2026, 07:49 PM

@chaliy
chaliy marked this pull request as ready for review November 29, 2026 19:34
@bashkit-pr-reviewer

bashkit-pr-reviewer Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Review summary (PR #3024, head d94f4a5)

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).

@bashkit-pr-reviewer bashkit-pr-reviewer Bot 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.

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.

Comment thread crates/bashkit/src/builtins/python.rs Resolved
@chaliy
chaliy merged commit 5b8a602 into main Nov 29, 2026
117 checks passed
@chaliy
chaliy deleted the fix/python-prelude-future-imports branch November 29, 2026 20:28
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.

1 participant