bridge-cpp: async call form - {name}Async siblings, baml::Future, cancellation, co_await - #4078
Conversation
…cellation, co_await Every generated function gains an Async sibling returning baml::Future<T, ThrownU>: get()/wait()/wait_for()/wait_until() mirror std::future, Cancel() requests engine-side cancellation through the v1 ABI's cancel_function_call (the envelope then arrives as a Cancelled panic), and destruction detaches. The registry's per-call cell is now a custom CallState (mutex + condvar + continuation slot) instead of std::promise, so under C++20 a Future is co_await-able: the dispatcher thread resumes the suspended coroutine when the envelope lands. The awaiter is feature-gated; the header stays C++17-clean, and CallSync is now literally StartCall().get(), one code path for both call forms. The fixture harness builds tests/cxx20/*.cc as a second executable only when the toolchain has C++20, keeping the main test binary as proof the generated SDK compiles as plain C++17.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
⏭️ Performance benchmarks were skippedPerf benchmarks (CodSpeed) are opt-in on pull requests — they no longer run on every push. They always run automatically after merge to To run them on this PR, do any of the following, then push a commit (or re-run CI):
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe C++ bridge adds move-only asynchronous futures with waiting, cancellation, and coroutine support. SDK generation emits ChangesC++ async future flow
C++ typed literal flow
Estimated code review effort: 4 (Complex) | ~60 minutes Sequence Diagram(s)sequenceDiagram
participant GeneratedBinding
participant StartCall
participant CallRegistry
participant BamlEngine
participant Future
GeneratedBinding->>StartCall: encode arguments and start async call
StartCall->>CallRegistry: create CallState
StartCall->>BamlEngine: call_function with correlation id
BamlEngine->>CallRegistry: complete result envelope
CallRegistry->>Future: fulfill CallState
Future->>GeneratedBinding: get or resume coroutine with decoded result
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Binary size checks failed❌ 4 violations · ✅ 3 passed
Details & how to fixViolations:
Add/update baselines:
[artifacts.baml-cli]
file_bytes = 19545072
stripped_bytes = 19545120
gzip_bytes = 9330140
[artifacts.bridge_wasm]
file_bytes = 16160508
gzip_bytes = 4402885
[artifacts.baml-cli]
file_bytes = 21070848
stripped_bytes = 21070848
gzip_bytes = 9539636
[artifacts.baml-cli]
file_bytes = 25267920
stripped_bytes = 25267912
gzip_bytes = 10729323Generated by |
…T macro (#4079) Stacked on #4078 (retargets to canary when it merges). BAML literal types stop widening to their base scalars: **each literal value is a distinct C++ type**, so literal unions dispatch and exhaustively match at compile time, and misspellings do not compile. Pure C++17. ## Surface One macro classifies its argument via overloaded constexpr helpers (overload resolution is the dispatch): ```cpp BAML_LIT("draft") // string -> Lit<'d','r','a','f','t'> (Boost.Metaparse char-pack trick, 64-char cap) BAML_LIT(42) // int -> Lit<int64_t{42}> (normalized: a bare int cannot mint a twin type) BAML_LIT(true) // bool -> Lit<true> BAML_LIT(Sentiment::Positive) // enum-variant type (Ty::EnumVariant), no longer widened to the enum ``` ```cpp // status "draft" | "sent" | "paid" baml::match(invoice.status, [](BAML_LIT("draft")) { ... }, [](BAML_LIT("sent")) { ... }, [](BAML_LIT("paid")) { ... }); // exhaustive; add a value in .baml -> build breaks ``` Every `Lit` carries its value statically (`::value`, plus implicit conversion to `string_view`/`int64_t`/`bool`/enum). Any non-blessed shape (`Lit<1>` int-typed, floats, mixed packs, pointers...) lands on a teaching `static_assert`. Generated code never uses the macro - the emitter spells char packs directly. ## Mechanics - `lit.h`: `template <auto... Vs> struct Lit` with LitShape tag-dispatch (no partial-spec ambiguity), `TrimNulls` canonicalization so every spelling of a string lands on one instantiation, `IntLit`/`BoolLit` macro-free alternates. - Codec: encode = the plain scalar/enum arm; decode = base-codec arm handling + exact-value check (mismatch rejects the union arm). Union decode gains a **literal pass** ahead of strict/lenient, so `Lit<"auto">` beats a `std::string` sibling. - Emitter: `Ty::Literal` + `Ty::EnumVariant` emit `::baml::Lit<...>` (proper char escaping incl. `\xNN` and the `i64::MIN` spelling trick); float literals stay widened (float NTTPs are C++20; BAML has none in practice). - Lit unions ride the existing order-canonical + dedup `baml::Union` machinery unchanged. ## Tests - `test_literals.cc` rewritten to typed semantics (returns/round-trips/class-of-literals + implicit-conversion ergonomics + type-level static_asserts). - New shared-fixture coverage, python-first per ground rules: mixed-base literal union `type Flag = "active" | 1 | true` round trip (`test_round_trip_flag_mixed_literal_union` in python, exact-alternative dispatch + match in C++). - `test_complex_models.cc`/`test_enums.cc` updated (Invoice.status literal union, EnumVariant-as-type now a singleton Lit). - `unions_static.cc`: BAML_LIT canonical-identity + int-normalization + Lit-union order-canonicality pins. cpp fixtures 12/12 locally; python + the rest on CI. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added support for strongly typed literal values in generated C++ SDKs. * Added literal handling for strings, integers, booleans, and enum variants. * Added support for mixed literal unions such as `"active"`, `1`, and `true`. * Added compile-time validation and exact literal round-trip behavior. * **Bug Fixes** * Improved union value decoding to prioritize exact literal matches before applying broader type conversions. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
baml_language/sdks/cpp/bridge_cpp/include/baml/codec.h (1)
314-388: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd runtime coverage for literal/base sibling precedence.
Exercise unions such as
"auto" | stringand1 | int, verifying exact values selectLitwhile other values fall back to the base alternative. The current mixed-base literal-only test does not protect this new three-pass behavior.🤖 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/sdks/cpp/bridge_cpp/include/baml/codec.h` around lines 314 - 388, Add runtime tests for union decoding that cover literal/base sibling precedence, including `"auto" | string` and `1 | int`. Verify exact literal values decode to the corresponding Lit alternative, while non-matching values decode to the base string or integer alternative, and keep coverage focused on the three-pass behavior around `Codec<std::variant<Ts...>>::Decode`.
🤖 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/sdks/cpp/bridge_cpp/include/baml/lit.h`:
- Around line 137-153: The public BAML_LIT path currently caps literals at 64
bytes while generated Lit char packs accept arbitrary lengths. Update the
literal construction and matching API around TrimNulls, BAML_LIT, and related
aliases so literals longer than 64 bytes remain nameable and pattern-matchable,
either by removing the public-only cap or by exposing generated
aliases/non-fixed-arity construction; preserve existing behavior for shorter
literals.
---
Nitpick comments:
In `@baml_language/sdks/cpp/bridge_cpp/include/baml/codec.h`:
- Around line 314-388: Add runtime tests for union decoding that cover
literal/base sibling precedence, including `"auto" | string` and `1 | int`.
Verify exact literal values decode to the corresponding Lit alternative, while
non-matching values decode to the base string or integer alternative, and keep
coverage focused on the three-pass behavior around
`Codec<std::variant<Ts...>>::Decode`.
🪄 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: c5126bca-2f57-432b-a146-0379d93f7ff1
📒 Files selected for processing (11)
baml_language/sdk_tests/crates/cpp/function_calls/customizable/tests/test_main.ccbaml_language/sdk_tests/crates/cpp/type_shapes/customizable/tests/test_complex_models.ccbaml_language/sdk_tests/crates/cpp/type_shapes/customizable/tests/test_enums.ccbaml_language/sdk_tests/crates/cpp/type_shapes/customizable/tests/test_literals.ccbaml_language/sdk_tests/crates/cpp/type_shapes/customizable/tests/unions_static.ccbaml_language/sdk_tests/crates/python_pydantic2/type_shapes/customizable/roundtrip_tests/test_literals.pybaml_language/sdk_tests/fixtures/type_shapes/baml_src/ns_literals/types.bamlbaml_language/sdks/cpp/bridge_cpp/include/baml/baml.hbaml_language/sdks/cpp/bridge_cpp/include/baml/codec.hbaml_language/sdks/cpp/bridge_cpp/include/baml/lit.hbaml_language/sdks/cpp/sdkgen_cpp/src/lib.rs
🚧 Files skipped from review as they are similar to previous changes (1)
- baml_language/sdks/cpp/bridge_cpp/include/baml/baml.h
Composed CH8/CH64 expansion blocks instead of 64 flat entries; the cap applies to the user-facing macro only (generated char packs have no limit) and realistically should never be an issue - literal types are short tag strings. The over-cap static_assert now names the decltype escape hatch.
Bridge-week step 10 for C++ (async half; error/panic behavior landed in #4071).
What
Every generated function gains an Async sibling returning
baml::Future<T, ThrownU>:get()/wait()/wait_for()/wait_until()/valid()mirrorstd::future(STYLE.md carve-out 2);Cancel()and the sibling'sAsyncsuffix are our vocabulary (Pascal). The suffix follows the opts-struct convention: verbatim BAML spelling + suffix (probe->probeAsync), allocated through the naming pool.Cancel()calls the v1 ABI'scancel_function_call; the envelope still arrives as abaml.panics.Cancelledpanic andget()throwsBamlCancelled. Destruction detaches (never blocks, never cancels) - Python task-model parity. No Rust changes.CallState(mutex + condvar + continuation slot) instead ofstd::promise, so the dispatcher thread can resume a suspended coroutine when the envelope lands. The awaiter is feature-gated (__cpp_impl_coroutine+__cpp_lib_coroutine); the header stays C++17-clean.CallSyncis now literallyStartCall().get().Tests
test_cancellation.cc: port of python'stest_cancellation.py(portable core: null-return baseline + engine-side cancel; asyncio/BamlCallContext idioms documented as deviations) + C++-specific future semantics (consume-once,wait_fortimeout, detach-on-destroy).test_optional_args.cc(python parity),test_raises.cc(async sibling repeats the doc block),test_errors.cc(typed throw throughget()).futures_static.cc: move-only + ThrownU order-canonicality static asserts.tests/cxx20/test_coawait.cc: 6 live co_await cases (pending resume, value, fast path, typed throw into the coroutine, cancellation, escape-to-join) driven by a minimal completion-latch Task. The harness buildstests/cxx20/*.ccas a second executable only when the toolchain has C++20, so the main binary keeps proving the SDK compiles as plain C++17.All 12
sdk_test_cppfixture tests pass locally (26 C++17 + 6 C++20 cases in function_calls).Note: the size gate is expected to fail until the stale baselines are refreshed - canary has drifted ~600 KB since #4057 and every open PR is at the 3% ceiling (see #4071's report).
Summary by CodeRabbit
*Async) alongside sync calls.baml::Futurefor in-flight calls, including cancellation and optional C++20co_awaitsupport.BAML_LIT(...)for more precise literal and union typing.