Skip to content

refactor(bridge-cffi): add target-neutral Wasm support - #4050

Merged
sxlijin merged 1 commit into
canaryfrom
sam/bridge-cffi-wasm
Jul 16, 2026
Merged

sxlijin merged 1 commit into
canaryfrom
sam/bridge-cffi-wasm

Conversation

@sxlijin

@sxlijin sxlijin commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Split bridge_cffi into native and Wasm platform layers while keeping common runtime initialization target-neutral.
  • Add initialize_runtime_from_bytecode_with_sys_ops so callers can inject platform-specific system operations without introducing a sys_wasm dependency into the shared bridge.
  • Preserve the native Rust/C ABI, including the versioned bridge registration API now present on canary.

Why

This creates the shared bridge layer required by TypeScript/Web without mixing browser, Workers, or sys_wasm behavior into bridge_cffi.

Impact

This is stack 1 of 3 and is based on canary; the TypeScript foundation it previously depended on has merged. Native consumers continue using the existing API; Wasm consumers receive the target-neutral entry point used by the next PR.

Checks

  • mise run stow
  • mise run fmt
  • mise run clippy
  • cargo check -p bridge_cffi
  • cargo check -p bridge_cffi --target wasm32-unknown-unknown --no-default-features

Summary by CodeRabbit

  • New Features
    • Added WebAssembly support for runtime initialization and function calls.
    • Added native and WASM runtime handling through platform-specific implementations.
    • Added a public buffer-release API for safely freeing returned data.
    • Added support for initializing runtimes with explicit system operations.
    • Improved function-call handling across native and WASM environments.

@vercel

vercel Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
beps Ready Ready Preview, Comment Jul 16, 2026 5:57am
promptfiddle Ready Ready Preview, Comment Jul 16, 2026 5:57am
promptfiddle2 Ready Ready Preview, Comment Jul 16, 2026 5:57am

Request Review

@vercel
vercel Bot temporarily deployed to Preview – beps July 16, 2026 04:23 Inactive
@coderabbitai

coderabbitai Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The CFFI bridge now separates native and WASM runtime implementations, supports explicit system operations during bytecode initialization, adds native and WASM function-call paths, and exposes buffer deallocation from buffer.rs.

Changes

Cross-platform bridge runtime

Layer / File(s) Summary
Platform dependency and module wiring
baml_language/crates/bridge_cffi/Cargo.toml, baml_language/crates/bridge_cffi/src/lib.rs, baml_language/crates/bridge_cffi/src/api.rs, baml_language/crates/bridge_cffi/src/ffi/runtime.rs
Dependencies are split between native and WASM targets, WASM support metadata is enabled, and runtime/module imports use conditional platform implementations.
Runtime lifecycle and call contracts
baml_language/crates/bridge_cffi/src/lib.rs, baml_language/crates/bridge_cffi/src/lib_native.rs, baml_language/crates/bridge_cffi/src/lib_wasm.rs
Runtime storage and initialization are implemented per platform, bytecode initialization accepts explicit sys_ops, and call identifiers use bex_project::CallId.
Native C ABI invocation and buffer ownership
baml_language/crates/bridge_cffi/src/lib_native.rs, baml_language/crates/bridge_cffi/src/buffer.rs, baml_language/crates/bridge_cffi/src/ffi/objects.rs
Native calls decode inputs, validate IDs, spawn runtime work, return callback results, and free returned buffers through the relocated free_buffer export.
WASM runtime invocation
baml_language/crates/bridge_cffi/src/lib_wasm.rs
WASM calls use thread-local runtime storage, decode and invoke functions asynchronously, encode results or errors, and expose a synchronous wrapper.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant BridgeCffi
  participant PlatformRuntime
  participant Bex
  Caller->>BridgeCffi: initialize runtime and submit encoded call
  BridgeCffi->>PlatformRuntime: store or retrieve platform runtime
  BridgeCffi->>Bex: decode context and invoke function
  Bex-->>BridgeCffi: encoded result or error
  BridgeCffi-->>Caller: callback or returned payload
Loading

Possibly related PRs

Suggested reviewers: hellovai

Poem

I’m a rabbit hopping cross-platform ground,
Native calls and WASM paths now sound.
Buffers sleep when free_buffer sings,
Sys ops guide the runtime’s wings.
Callbacks carry results bright—
A tidy bridge from day to night.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: refactoring bridge-cffi for target-neutral Wasm/native support.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sam/bridge-cffi-wasm

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown

⏭️ Performance benchmarks were skipped

Perf benchmarks (CodSpeed) are opt-in on pull requests — they no longer run on every push. They always run automatically after merge to canary/main.

To run them on this PR, do any of the following, then push a commit (or re-run CI):

  • Add RUN_CODSPEED=1 to the PR description, or
  • Include run-perf or /perf in the PR title or any commit message.

@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 16, 2026 04:30 Inactive
@github-actions

github-actions Bot commented Jul 16, 2026 •

Copy link
Copy Markdown

Binary size checks passed

✅ 7 passed

Artifact Platform File Gzip Gated on Baseline Delta Status
✅ baml-cli Linux 🔒 22.8 MB 9.7 MB file 22.8 MB +58.0 KB (+0.3%) OK
✅ packed-program Linux 🔒 16.3 MB 6.8 MB file 16.3 MB +24.6 KB (+0.2%) OK
✅ baml-cli macOS 🔒 17.6 MB 8.5 MB file 17.5 MB +49.7 KB (+0.3%) OK
✅ packed-program macOS 🔒 12.6 MB 6.0 MB file 12.6 MB +32 B (+0.0%) OK
✅ baml-cli Windows 🔒 19.1 MB 8.7 MB file 18.7 MB +414.7 KB (+2.2%) OK
✅ packed-program Windows 🔒 13.5 MB 6.1 MB file 13.5 MB +5.6 KB (+0.0%) OK
✅ bridge_wasm WASM 15.2 MB 🔒 4.3 MB gzip 4.3 MB +3.6 KB (+0.1%) OK

🔒 = the size this artifact is GATED on (ceiling + delta). Binaries gate on file size (installed binary); WASM gates on gzip (download size). The other size is shown for information only.


Generated by cargo size-gate · workflow run

@sxlijin
sxlijin force-pushed the sam/typescript-runtime-matrix branch from 8173b78 to 4313734 Compare July 16, 2026 04:43
@sxlijin
sxlijin force-pushed the sam/bridge-cffi-wasm branch from 7ae977e to 403a93a Compare July 16, 2026 04:43
@vercel
vercel Bot temporarily deployed to Preview – beps July 16, 2026 04:44 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 16, 2026 04:51 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle July 16, 2026 05:05 Inactive
Base automatically changed from sam/typescript-runtime-matrix to canary July 16, 2026 05:21
@sxlijin
sxlijin force-pushed the sam/bridge-cffi-wasm branch from 403a93a to 361f647 Compare July 16, 2026 05:35
@vercel
vercel Bot temporarily deployed to Preview – beps July 16, 2026 05:36 Inactive
@sxlijin
sxlijin marked this pull request as ready for review July 16, 2026 05:42
@sxlijin
sxlijin enabled auto-merge July 16, 2026 05:42
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 16, 2026 05:43 Inactive

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 361f647f21

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Ok(call) => call,
Err(error) => return error_to_outbound(error),
};
call_and_encode(call.runtime, call.function_name, call.args, call.context).await

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Serialize Wasm results for the wire

When this Wasm entry point is used by a Web/JS caller and a BAML function returns media or a prompt AST, reusing call_and_encode encodes with CffiHandleTableOptions::for_in_process(), which stores those values as BamlHandles instead of inline media_value/prompt_ast_value. The new Wasm platform does not export the native media handle accessors from ffi::handle, so callers receiving these bytes cannot read the returned value and the handle table entry remains opaque; the Wasm path should encode with wire options or expose equivalent handle accessors.

Useful? React with 👍 / 👎.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
baml_language/crates/bridge_cffi/src/buffer.rs (1)

40-41: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Use drop(...) for the deallocation call.

let _ = Vec::from_raw_parts(...) works, but drop(...) makes the one-shot deallocation intent clearer.

♻️ Proposed refactor
-            let _ = Vec::from_raw_parts(buf.ptr as *mut u8, buf.len, buf.len);
+            drop(Vec::from_raw_parts(buf.ptr as *mut u8, buf.len, buf.len));
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@baml_language/crates/bridge_cffi/src/buffer.rs` around lines 40 - 41, Replace
the discard assignment around Vec::from_raw_parts with an explicit drop(...)
call, preserving the existing pointer, length, and capacity arguments and
one-shot deallocation behavior.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@baml_language/crates/bridge_cffi/src/lib_wasm.rs`:
- Around line 61-63: Update call_function_in_wasm_sync to avoid
futures::executor::block_on for promise-backed WASM operations; either remove
this synchronous entrypoint in favor of the async call_function_in_wasm API or
make it explicitly reject work that requires awaiting JavaScript Promises.

---

Nitpick comments:
In `@baml_language/crates/bridge_cffi/src/buffer.rs`:
- Around line 40-41: Replace the discard assignment around Vec::from_raw_parts
with an explicit drop(...) call, preserving the existing pointer, length, and
capacity arguments and one-shot deallocation behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: dc11817e-43a8-4908-8361-33e7ad7e6049

📥 Commits

Reviewing files that changed from the base of the PR and between 0e58a20 and 361f647.

⛔ Files ignored due to path filters (1)
  • baml_language/Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (8)
  • baml_language/crates/bridge_cffi/Cargo.toml
  • baml_language/crates/bridge_cffi/src/api.rs
  • baml_language/crates/bridge_cffi/src/buffer.rs
  • baml_language/crates/bridge_cffi/src/ffi/objects.rs
  • baml_language/crates/bridge_cffi/src/ffi/runtime.rs
  • baml_language/crates/bridge_cffi/src/lib.rs
  • baml_language/crates/bridge_cffi/src/lib_native.rs
  • baml_language/crates/bridge_cffi/src/lib_wasm.rs
💤 Files with no reviewable changes (1)
  • baml_language/crates/bridge_cffi/src/ffi/objects.rs

@vercel
vercel Bot temporarily deployed to Preview – promptfiddle July 16, 2026 05:57 Inactive
@sxlijin
sxlijin added this pull request to the merge queue Jul 16, 2026
Merged via the queue into canary with commit bd55eab Jul 16, 2026
53 checks passed
@sxlijin
sxlijin deleted the sam/bridge-cffi-wasm branch July 16, 2026 06:19
@coderabbitai coderabbitai Bot mentioned this pull request Jul 24, 2026
4 tasks done

This branch was previously deployed

3 inactive deployments
Preview – promptfiddle — 361f647f Deployed Jul 16, 2026 by vercel[bot]
Preview – promptfiddle2 — 361f647f Deployed Jul 16, 2026 by vercel[bot]
Preview – beps — 361f647f Deployed Jul 16, 2026 by vercel[bot]
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