- Zig 85%
- C 11%
- Shell 3%
- PowerShell 1%
tb-unison-shim #
tb-unison-shim is a Zig shared library that wraps the official TigerBeetle C client (libtb_client) and exposes a callback-free C ABI designed for the Unison TigerBeetle client.
Why This Exists #
Official TigerBeetle clients rely on callback registration in native code. Unison FFI can load dynamic libraries and call symbols, but cannot construct C callbacks directly. This shim absorbs that callback contract and exposes a polling + explicit release model.
When Unison gains proper C callback pointer support, the intended end state is to remove this shim and bind the Unison client directly to libtb_client.
Architecture #
The shim keeps TigerBeetle protocol and networking behavior in libtb_client, while presenting a simpler init -> submit -> wait(timeout) -> release -> deinit lifecycle to FFI consumers.
More details here - ARCHITECTURE.md
Features #
Implemented #
- TigerBeetle client lifecycle (
tb_unison_init,tb_unison_deinit) - Raw request submission (
tb_unison_submit_raw) - Bounded completion wait (
tb_unison_wait_completion_timeout_ms) - Explicit completion ownership release (
tb_unison_release_completion) - Typed submit helper:
tb_unison_submit_create_accounts - Typed submit helper:
tb_unison_submit_lookup_accounts - Typed submit helper:
tb_unison_submit_create_transfers - Typed submit helper:
tb_unison_submit_lookup_transfers - Typed submit helper:
tb_unison_submit_get_account_transfers - Typed submit helper:
tb_unison_submit_get_account_balances - Typed submit helper:
tb_unison_submit_query_accounts - Typed submit helper:
tb_unison_submit_query_transfers - Operation enum coverage for all current TigerBeetle operation tags in raw path
- Unit test coverage for lifecycle, validation, backpressure, timeout, release safety, and metadata
- Live smoke tests against a local TigerBeetle replica
Pending #
- Additional helper-level result decoding for non-account operations
- Broader cross-language ergonomic layer (this repo intentionally focuses on C ABI contract, not full language SDK UX)
Design Principles #
This project intentionally inherits TigerBeetle engineering priorities and is strongly influenced by TigerSTYLE:
- Safety before performance, performance before ergonomics
- Small, explicit ABI surface with explicit ownership and lifecycle
- Bounded resources and bounded waits (
max_inflight,max_completions, timeout-based waits) - Deterministic status mapping and negative-space validation
- Assertions and invariants to protect API boundaries
- Compile-time ABI layout checks for C interop stability
Current gaps in Request guarantees #
- Completion-slot pressure can drop a completed callback payload, and a later
wait_completion_timeout_mscan returnTIMEOUTeven though the request completed inlibtb_client. - Oversized completion payloads are locally remapped to
invalid_data_sizein the shim path, which is not identical to upstream reply propagation. wait_completion_timeout_mstimeout is a local polling deadline, not a TigerBeetle protocol request timeout. This behavior is intentional, but must be clearly distinguished for integrators.
Unison Integration #
The primary consumer is the Unison TigerBeetle client. In that integration:
- Unison project location: https://share.unison-lang.org/@kaychaks/tigerbeetle
- Unison loads
libtb_unison_shimas a dynamic library - Unison calls shim exports via FFI primitives
- Shim handles callback bridging and native completion lifetime
- Unison keeps domain-level encoding/decoding and user-facing API behavior
This keeps the Unison client backend-focused and allows migration paths while preserving a stable public API.
Planned evolution: this shim is transitional infrastructure. If Unison FFI adds callback-pointer support, the Unison client can move to direct libtb_client integration and this repo can be retired.
Build And Test #
./zig/zig build
./zig/zig build test
./zig/zig build smoke
./zig/zig build tidy
./zig/zig build check
Output Artifact #
Build output installs the shared library under:
zig-out/lib/libtb_unison_shim.dylib(macOS)zig-out/lib/libtb_unison_shim.so(Linux)zig-out/lib/tb_unison_shim.dll(Windows)
C ABI Surface #
tb_unison_inittb_unison_deinittb_unison_submit_rawtb_unison_wait_completion_timeout_mstb_unison_release_completiontb_unison_submit_create_accountstb_unison_submit_lookup_accountstb_unison_submit_create_transferstb_unison_submit_lookup_transferstb_unison_submit_get_account_transferstb_unison_submit_get_account_balancestb_unison_submit_query_accountstb_unison_submit_query_transfers
See include/tb_unison_shim.h for full contract details.