Skip to main content

net/ffi/
mod.rs

1//! C FFI bindings for cross-language integration.
2//!
3//! This module provides a C-compatible API for using Net from
4//! other languages (Python, Node.js, Go, etc.).
5//!
6//! # Safety
7//!
8//! All public FFI functions in this module accept raw pointers from C code.
9//! Each is declared `pub unsafe extern "C" fn` so the unsafety is
10//! explicit at the type level; the module-wide contract callers
11//! must uphold is:
12//! - Pointers are valid and properly aligned
13//! - Opaque handle pointers (`*mut T`) were produced by this crate's
14//!   matching constructor (`Box::into_raw` inside the FFI surface).
15//!   Foreign-allocated pointers, even if valid and aligned, will UB
16//!   when consumed by `Box::from_raw` in the corresponding `_free`.
17//! - String pointers point to valid UTF-8 data
18//! - Buffer sizes are accurate
19//! - Handles are not used after `net_shutdown`
20//!
21//! The per-function `# Safety` rustdoc is intentionally suppressed
22//! at the module level — every entry point shares the same contract
23//! and the module doc-comment above (plus `include/README.md`) is
24//! the source of truth. Adding individual `# Safety` blocks would
25//! duplicate the same wording 200 times without adding signal.
26//!
27//! # Thread Safety
28//!
29//! All FFI functions are thread-safe. The event bus handle can be shared
30//! across threads.
31//!
32#![allow(clippy::missing_safety_doc)]
33// The cross-cutting C-side safety contract for every `unsafe` block in
34// this module is documented in the `# Safety` section above:
35// caller-validated pointer / length / lifetime / handle-not-after-shutdown
36// invariants documented in `include/net.h`. Inlining `// SAFETY:` on each
37// block would add ~200 identical "see module preamble" comments without
38// adding any signal beyond what the preamble already says.
39#![expect(
40    clippy::undocumented_unsafe_blocks,
41    reason = "module-wide FFI safety contract documented in the # Safety preamble above"
42)]
43#![expect(
44    clippy::multiple_unsafe_ops_per_block,
45    reason = "FFI entry points routinely deref + write to multiple out-parameter fields under the same caller contract; splitting per-op would obscure the single boundary-cross"
46)]
47
48//! # Tokio runtime restriction
49//!
50//! Internal FFI ops (`net_poll`, `net_flush`, `net_shutdown`,
51//! `net_redex_*`, `net_mesh_new`, the cortex FFI, the mesh FFI)
52//! drive the bus's tokio runtime via `Runtime::block_on`. That
53//! function panics with "Cannot start a runtime from within a
54//! runtime" if the calling thread is already inside a tokio
55//! runtime context. The functions are `extern "C"`, so a panic
56//! unwinds across the FFI boundary into C / Go-cgo / Python /
57//! NAPI — undefined behavior.
58//!
59//! **The common-case C / Go / Python caller has no Rust tokio
60//! runtime, so this is unreachable for them.** The narrow path is:
61//!
62//! - A **Rust** caller loads the cdylib and calls these
63//!   functions from inside its own `#[tokio::main]` (or any
64//!   thread that has called `Runtime::enter()`).
65//! - A non-Rust caller embeds a Rust library that runs its own
66//!   tokio runtime and forwards calls into this cdylib on the
67//!   same thread.
68//!
69//! Both forms are unusual but reachable. **Do not call any FFI
70//! op from a thread that already holds a tokio runtime
71//! context.** If you must, spawn the FFI call on a fresh OS
72//! thread that doesn't carry a runtime guard, or wrap the call
73//! with `tokio::task::spawn_blocking(|| net_xxx(...))` to escape
74//! the worker pool.
75//!
76//! `net_init` (`mod.rs:284-316`) hardens against this for runtime
77//! *construction*; the steady-state ops do not, since the cost
78//! of a `Handle::try_current()` check on every poll would be
79//! measurable for the common path that doesn't hit the bug.
80//!
81//! # `catch_unwind` + caller-held locks
82//!
83//! Several FFI entries (`net_blob_publish`, `net_blob_resolve`,
84//! `net_*_wait_for_token`) wrap their body in
85//! `std::panic::catch_unwind(AssertUnwindSafe(...))` so a panic
86//! during the call returns a typed `NET_ERR_BLOB_PANIC` /
87//! `NET_ERR_PANIC` code rather than unwinding across the FFI
88//! boundary. That stops the substrate-side undefined behavior,
89//! but it does NOT make the wrapped code transparently panic-safe
90//! from the caller's perspective.
91//!
92//! **If the caller invokes an FFI op while holding an OS-level
93//! lock, a `sync.Mutex` (Go), `threading.Lock` (Python), or any
94//! other mutex with poisoning semantics, and the FFI body panics,
95//! the mutex is left in a poisoned state.** Subsequent acquires
96//! on the same mutex by the caller observe the poisoning and
97//! either error (Rust `parking_lot` with `poison_on_unwind`) or
98//! deadlock (Go's `sync.Mutex` doesn't poison; the caller has
99//! observed a return value that may not reflect the state of
100//! the FFI op).
101//!
102//! Recommended caller pattern: **do not hold a caller-side lock
103//! across an FFI call**. Acquire the lock, prepare the inputs,
104//! release the lock, then call the FFI. Re-acquire if you need
105//! to update caller state with the result.
106//!
107//! The hazard is documented per-binding in:
108//!   - Python: `bindings/python/README.md` (caller-mutex notes)
109//!   - Node:   `bindings/node/README.md`
110//!   - Go:     `bindings/go/net/redex.go` lifecycle docs
111//!   - C:      `include/net.h` (every wait-family declaration)
112//!
113//! # Memory Management
114//!
115//! - Handles returned by `net_init` must be freed with `net_shutdown`
116//! - String buffers passed to `net_poll` are owned by the caller
117//! - Error codes are returned as integers (0 = success, negative = error)
118//!
119//! # Example (C)
120//!
121//! ```c
122//! #include "net.h"
123//!
124//! int main() {
125//!     // Initialize with default config
126//!     void* bus = net_init("{\"num_shards\": 4}");
127//!     if (!bus) return 1;
128//!
129//!     // Ingest an event
130//!     int result = net_ingest(bus, "{\"token\": \"hello\"}", 19);
131//!     if (result < 0) { /* handle error */ }
132//!
133//!     // Poll events
134//!     char buffer[65536];
135//!     result = net_poll(bus, "{\"limit\": 100}", buffer, sizeof(buffer));
136//!
137//!     // Shutdown
138//!     net_shutdown(bus);
139//!     return 0;
140//! }
141//! ```
142
143// FFI functions accept raw pointers but are not marked `unsafe` to maintain
144// C ABI compatibility. Safety is documented in the module-level docs.
145#![allow(clippy::not_unsafe_ptr_arg_deref)]
146
147use std::ffi::CStr;
148use std::os::raw::{c_char, c_int};
149use std::ptr;
150
151use tokio::runtime::Runtime;
152
153use crate::bus::EventBus;
154use crate::config::EventBusConfig;
155use crate::consumer::ConsumeRequest;
156use crate::event::{Event, RawEvent};
157
158/// C FFI for CortEX / NetDb / RedexFile. Requires `netdb` (for the
159/// unified facade) and `redex-disk` (for persistent storage paths on
160/// `Redex` / `RedexFile`). Go / cgo consumers target this surface.
161///
162/// `missing_docs` is suppressed on this module: these are extern "C"
163/// shims over already-documented Rust adapters, and the per-function
164/// contract is documented in the binding-side READMEs (Go / TS / Py).
165/// Re-documenting each shim would duplicate with drift risk.
166/// Per-FFI-handle quiescing protocol shared by cortex / mesh
167/// handles to close the audit-#23/#24/#25 use-after-free hazards
168/// when a `_free` races a concurrent op. See module docs for the
169/// soundness story (intentional box leak) and the per-handle
170/// recipe.
171#[cfg(any(
172    all(feature = "netdb", feature = "redex-disk"),
173    feature = "net",
174    feature = "redis",
175))]
176pub mod handle_guard;
177
178#[cfg(all(feature = "netdb", feature = "redex-disk"))]
179#[allow(missing_docs)]
180pub mod cortex;
181
182/// C FFI for the Dataforts Phase 3 blob surface. Exposes the
183/// BlobRef wire codec, the global adapter registry, and the
184/// `publish_blob` / `resolve_payload` helpers for cgo / native
185/// consumers.
186#[cfg(feature = "dataforts")]
187#[allow(missing_docs)]
188pub mod blob;
189
190/// Stub definitions for the `net_mesh_blob_adapter_*` symbols
191/// when the `dataforts / netdb / redex-disk` feature triple is
192/// off. cgo / dlsym consumers link these symbols unconditionally
193/// (see `bindings/go/blob.go`), so a libnet built without the
194/// triple must still satisfy them — each stub returns
195/// `NET_ERR_FEATURE_NOT_BUILT` (or null) so Go programs route to
196/// a clean error rather than fail at program load. The module is
197/// empty when the feature triple is on (the real impls in
198/// `ffi::blob` cover the same symbol names).
199#[allow(missing_docs)]
200pub mod blob_stubs;
201
202/// C FFI for the encrypted-UDP mesh transport + channels. Requires
203/// the `net` feature (which brings in the crypto + transport). Go /
204/// cgo consumers target this surface alongside `ffi::cortex`. See
205/// the `ffi::cortex` note for why `missing_docs` is suppressed here.
206#[cfg(feature = "net")]
207#[allow(missing_docs)]
208pub mod mesh;
209
210/// C FFI for the transport surface (blob + directory transfer over the
211/// fairscheduler stream transport — Transport SDK plan T-C). Drives the
212/// node's transfer engine via the existing `MeshNodeHandle` +
213/// `MeshBlobAdapterHandle`, so it rides `net` + the blob-adapter feature
214/// set (the adapter handle needs `netdb` + `redex-disk`). Feature-off
215/// stubs for builds missing the quad live in `transport_stubs` below.
216#[cfg(all(
217    feature = "net",
218    feature = "dataforts",
219    feature = "netdb",
220    feature = "redex-disk"
221))]
222#[allow(missing_docs)]
223pub mod transport;
224
225/// Feature-off stubs for the transport symbols
226/// (`net_serve_blob_transfer` / `net_fetch_blob*` / `net_store_dir` /
227/// `net_fetch_dir` / `net_dir_manifest_read` / `net_transport_free_buffer`)
228/// when the quad above is not fully built. The Go binding
229/// (`bindings/go/net/transport.go`) links these unconditionally, so a
230/// libnet without the quad must still satisfy them — each stub returns
231/// `NET_ERR_FEATURE_NOT_BUILT` (or null / no-op) so Go programs route to
232/// a clean error rather than fail at program load. Empty (compiled out)
233/// when the quad is on — the real impls in `ffi::transport` then own the
234/// symbol names. Mirrors `ffi::blob_stubs`.
235#[allow(missing_docs)]
236pub mod transport_stubs;
237
238/// C FFI for the `aggregator.registry` RPC client + channel
239/// visibility setter. Stage 5 of `SDK_AGGREGATOR_SUBNET_PLAN.md`.
240/// Rides the `net` feature alongside `ffi::mesh` because every
241/// op needs a `MeshNodeHandle`, and `cortex` because the
242/// underlying `behavior::aggregator` module's RPC surface is
243/// cortex-only (`mesh_rpc`, `cortex::rpc`, `postcard`).
244#[cfg(all(feature = "net", feature = "cortex"))]
245#[allow(missing_docs)]
246pub mod aggregator;
247
248/// C FFI for stateless predicate evaluation (Phase 9c of
249/// `CAPABILITY_SYSTEM_SDK_PLAN.md`). Pure helpers — no handles,
250/// no state. Mirrors the SDK-layer `evaluatePredicate` /
251/// `evaluate_predicate` surface every binding ships, exposed at
252/// the C ABI for raw consumers (C / C++ / Zig / Swift / etc.).
253#[cfg(feature = "net")]
254pub mod predicate;
255
256/// C FFI for stateless capability-set validation (Phase 9a of
257/// `CAPABILITY_SYSTEM_SDK_PLAN.md`). Pure helper — `caps_json`
258/// in, `report_json` out. Mirrors the SDK-layer
259/// `validate_capabilities` surface, exposed at the C ABI for raw
260/// consumers.
261#[cfg(feature = "net")]
262pub mod schema;
263
264/// C FFI for predicate debug-session helpers (Phase 9d of
265/// `CAPABILITY_SYSTEM_SDK_PLAN.md`). Pure helpers — single-eval
266/// `evaluate_with_trace`, corpus-wide
267/// `aggregate_debug_report`, and host-side
268/// `redact_metadata_keys`. Mirror what every other binding
269/// ships at the SDK layer; exposed at the C ABI for raw
270/// consumers.
271#[cfg(feature = "net")]
272pub mod predicate_debug;
273
274/// C FFI for the Redis Streams consumer-side dedup helper. Mirrors
275/// the Rust `net::adapter::RedisStreamDedup` surface for Go / C / Zig
276/// consumers. See `ffi::redis_dedup` module docs for the wire
277/// shape and the dedup contract.
278#[cfg(feature = "redis")]
279pub mod redis_dedup;
280
281#[cfg(feature = "net")]
282use crate::adapter::net::{NetAdapterConfig, ReliabilityConfig, StaticKeypair};
283#[cfg(any(feature = "redis", feature = "jetstream", feature = "net"))]
284use crate::config::AdapterConfig;
285#[cfg(feature = "jetstream")]
286use crate::config::JetStreamAdapterConfig;
287#[cfg(feature = "redis")]
288use crate::config::RedisAdapterConfig;
289#[cfg(feature = "net")]
290use std::ffi::CString;
291
292/// Opaque handle to an event bus instance.
293///
294/// This wraps the EventBus along with a Tokio runtime for async operations.
295///
296/// # Lifetime / soundness
297///
298/// The handle storage is *intentionally leaked* on `net_shutdown` rather
299/// than freed via `Box::from_raw`. Reasoning: every FFI entry point
300/// dereferences the C-side `*mut NetHandle` to access the atomics that
301/// gate shutdown. The previous Dekker-style SeqCst handshake between
302/// `FfiOpGuard::try_enter` (which calls `fetch_add` on `active_ops`) and
303/// `net_shutdown` (which loads `active_ops` then `Box::from_raw`s the
304/// handle) was unsound: SeqCst orders the atomic operations only — the
305/// non-atomic `Box::from_raw` could deallocate the storage between
306/// shutdown's load and a concurrent FFI op's `fetch_add`, producing a
307/// use-after-free on the freed atomic. By never freeing the box, the
308/// atomic memory backing the handle is always valid; concurrent FFI ops
309/// observe `shutting_down=true` after shutdown signals it and bail
310/// before touching `bus`/`runtime`.
311///
312/// `bus` and `runtime` are stored in `ManuallyDrop` so that
313/// `net_shutdown` can `take` them out (via `ptr::read`) in order to
314/// call `bus.shutdown().await`. Because `shutting_down` is set first
315/// and shutdown waits for `active_ops` to drop to zero before reading
316/// these fields, no FFI op can be racing the read. If the wait times
317/// out, the `ptr::read` is skipped and both fields are leaked along
318/// with the box.
319pub struct NetHandle {
320    /// Owned `EventBus`. Read out via `ManuallyDrop::take` during
321    /// shutdown once `active_ops` has drained to zero. After that
322    /// point, `shutting_down` is `true` and no FFI op may access this
323    /// field.
324    bus: std::mem::ManuallyDrop<EventBus>,
325    /// Owned tokio runtime. Same lifetime contract as `bus`.
326    runtime: std::mem::ManuallyDrop<Runtime>,
327    /// Set to `true` once `net_shutdown` begins. All other FFI
328    /// functions check this flag and return `ShuttingDown` before
329    /// touching `bus` / `runtime`.
330    shutting_down: std::sync::atomic::AtomicBool,
331    /// Number of in-flight FFI operations (excluding shutdown itself).
332    /// `net_shutdown` spins until this drops to zero (with a deadline)
333    /// before reading `bus` / `runtime` to call shutdown.
334    active_ops: std::sync::atomic::AtomicU32,
335    /// Set to `true` after `net_shutdown` has consumed `bus` /
336    /// `runtime` via `ManuallyDrop::take`. A second `net_shutdown`
337    /// call observes this and returns `Success` without re-taking
338    /// (which would be UB). FFI ops also check this before touching
339    /// `bus` / `runtime`, defending against a contract-violating
340    /// caller that races a post-shutdown call.
341    bus_taken: std::sync::atomic::AtomicBool,
342    /// Set to `true` after `bus.shutdown()` returns from the
343    /// first `net_shutdown` call. A second/third concurrent
344    /// `net_shutdown` caller spins until this flips before
345    /// returning success — without this gate the second caller
346    /// observed `bus_taken == true` and returned `Success` while
347    /// the first caller was still mid-`block_on(bus.shutdown())`,
348    /// falsely signaling completion of an in-progress shutdown.
349    shutdown_completed: std::sync::atomic::AtomicBool,
350}
351
352/// Maximum time `net_shutdown` will wait for in-flight FFI operations
353/// to complete before giving up. If the deadline expires, the bus is
354/// leaked rather than read out — leaking is correct (the box is
355/// already leaked permanently for soundness reasons) but means the
356/// adapter's `flush()` / `shutdown()` won't run.
357const FFI_SHUTDOWN_DEADLINE: std::time::Duration = std::time::Duration::from_secs(5);
358
359/// RAII guard that increments `active_ops` on creation and decrements on drop.
360struct FfiOpGuard<'a> {
361    handle: &'a NetHandle,
362}
363
364impl<'a> FfiOpGuard<'a> {
365    /// Try to enter an FFI operation. Returns `None` if the handle is
366    /// shutting down or if `bus` / `runtime` have already been taken.
367    ///
368    /// Soundness rests on the fact that the box backing `handle` is
369    /// never freed (see `NetHandle` doc). The `fetch_add` is therefore
370    /// always on valid memory regardless of whether shutdown is in
371    /// progress. The subsequent loads decide whether the op is allowed
372    /// to proceed; if shutdown was signaled or `bus_taken` flipped
373    /// before our increment was visible, we bail without touching
374    /// `bus` / `runtime`. The `bus_taken` check defends against a
375    /// contract-violating caller that races a post-shutdown call: even
376    /// if `shutting_down` was reset somehow, an op that would touch the
377    /// already-taken `ManuallyDrop` fields is rejected.
378    fn try_enter(handle: &'a NetHandle) -> Option<Self> {
379        handle
380            .active_ops
381            .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
382        if handle
383            .shutting_down
384            .load(std::sync::atomic::Ordering::SeqCst)
385            || handle.bus_taken.load(std::sync::atomic::Ordering::SeqCst)
386        {
387            handle
388                .active_ops
389                .fetch_sub(1, std::sync::atomic::Ordering::AcqRel);
390            None
391        } else {
392            Some(Self { handle })
393        }
394    }
395}
396
397impl Drop for FfiOpGuard<'_> {
398    fn drop(&mut self) {
399        self.handle
400            .active_ops
401            .fetch_sub(1, std::sync::atomic::Ordering::AcqRel);
402    }
403}
404
405/// Returns `true` when `handle` is non-null and aligned for
406/// `NetHandle`. Every `extern "C"` entry point that derefs the
407/// raw handle must gate on this — a misaligned pointer produced
408/// by an over-eager `void *` cast in a foreign caller would be
409/// immediate UB on `&*handle`, even before the `is_null` check.
410#[inline]
411fn handle_is_valid(handle: *const NetHandle) -> bool {
412    !handle.is_null() && (handle as usize).is_multiple_of(std::mem::align_of::<NetHandle>())
413}
414
415/// Error codes returned by FFI functions.
416#[repr(C)]
417pub enum NetError {
418    /// Success (no error).
419    Success = 0,
420    /// Null pointer passed.
421    NullPointer = -1,
422    /// Invalid UTF-8 string.
423    InvalidUtf8 = -2,
424    /// Invalid JSON.
425    InvalidJson = -3,
426    /// Initialization failed.
427    InitFailed = -4,
428    /// Ingestion failed (backpressure).
429    IngestionFailed = -5,
430    /// Poll failed.
431    PollFailed = -6,
432    /// Buffer too small.
433    BufferTooSmall = -7,
434    /// Shutting down.
435    ShuttingDown = -8,
436    /// Integer overflow: result does not fit in `c_int`.
437    IntOverflow = -9,
438    /// Stream handle does not belong to the supplied node handle.
439    /// Previously the send-family FFIs accepted any (stream, node)
440    /// pair without verifying they were created from the same node,
441    /// allowing silent cross-session traffic.
442    MismatchedHandles = -10,
443    /// `CString::new` failure: the input bytes are valid UTF-8 by
444    /// Rust's `String` invariant but contain an interior NUL byte
445    /// — and the C ABI cannot represent that, since C strings are
446    /// NUL-terminated. Pre-fix this was reported as
447    /// `InvalidUtf8`, which was wrong: the input is UTF-8-valid;
448    /// it just has a NUL where C expects it not to. A binding
449    /// reading the typed error and seeing "invalid UTF-8" would
450    /// chase the wrong cause.
451    InteriorNul = -11,
452    /// Unknown error.
453    Unknown = -99,
454}
455
456impl From<NetError> for c_int {
457    fn from(e: NetError) -> Self {
458        e as c_int
459    }
460}
461
462/// Enter an FFI operation with lifetime protection. Returns an `FfiOpGuard`
463/// that prevents `net_shutdown` from deallocating the handle until the guard
464/// is dropped. Returns `Err` with the error code if shutdown is in progress.
465#[inline]
466fn enter_ffi_op(handle: &NetHandle) -> Result<FfiOpGuard<'_>, c_int> {
467    FfiOpGuard::try_enter(handle).ok_or(NetError::ShuttingDown.into())
468}
469
470/// Initialize a new event bus.
471///
472/// # Parameters
473///
474/// - `config_json`: JSON configuration string (UTF-8, null-terminated).
475///   Pass NULL or empty string for default configuration.
476///
477/// # Returns
478///
479/// Opaque handle to the event bus, or NULL on failure.
480/// The handle must be freed with `net_shutdown`.
481///
482/// # Example Configuration
483///
484/// ```json
485/// {
486///   "num_shards": 8,
487///   "ring_buffer_capacity": 1048576,
488///   "backpressure_mode": "DropOldest",
489///   "batch": {
490///     "min_size": 1000,
491///     "max_size": 10000,
492///     "max_delay_ms": 10
493///   }
494/// }
495/// ```
496#[unsafe(no_mangle)]
497pub unsafe extern "C" fn net_init(config_json: *const c_char) -> *mut NetHandle {
498    // Parse and validate the config BEFORE constructing the tokio
499    // runtime. Building the runtime first would let any subsequent
500    // early-return path (`CStr::to_str` Err, `parse_config_json`
501    // returning None, `EventBus::new` returning Err) drop the
502    // local `Runtime` on function return. Dropping a multi-thread
503    // tokio runtime from inside ANOTHER tokio runtime's worker
504    // thread panics with "Cannot drop a runtime in a context where
505    // blocking is not allowed", unwinding across this `extern "C"`
506    // boundary into a Python / Go-cgo / NAPI / PyO3 caller —
507    // undefined behaviour. By validating inputs first, the runtime
508    // is only built once we know it will be installed into the
509    // `NetHandle` and survive the call.
510    let config = if config_json.is_null() {
511        EventBusConfig::default()
512    } else {
513        let config_str = match unsafe { CStr::from_ptr(config_json) }.to_str() {
514            Ok("") => EventBusConfig::default(),
515            Ok(s) => match parse_config_json(s) {
516                Some(cfg) => cfg,
517                None => return ptr::null_mut(),
518            },
519            Err(_) => return ptr::null_mut(),
520        };
521        config_str
522    };
523
524    // Now construct the runtime — its lifetime is tied to the
525    // returned `NetHandle` (via `create_with_config`), so the only
526    // remaining drop is on `net_shutdown`, which already handles
527    // it via `runtime.block_on(...)` (see #74) outside any other
528    // tokio context.
529    let runtime = match Runtime::new() {
530        Ok(rt) => rt,
531        Err(_) => return ptr::null_mut(),
532    };
533
534    create_with_config(runtime, config)
535}
536
537/// Parse JSON configuration into EventBusConfig.
538///
539/// Supports:
540/// - `num_shards`: number of shards
541/// - `ring_buffer_capacity`: ring buffer size per shard
542/// - `backpressure_mode`: "DropNewest", "DropOldest", "FailProducer"
543fn parse_config_json(json_str: &str) -> Option<EventBusConfig> {
544    let value: serde_json::Value = serde_json::from_str(json_str).ok()?;
545
546    let mut builder = EventBusConfig::builder();
547
548    if let Some(num_shards) = value.get("num_shards").and_then(|v| v.as_u64()) {
549        let num_shards = u16::try_from(num_shards).ok()?;
550        builder = builder.num_shards(num_shards);
551    }
552
553    if let Some(capacity) = value.get("ring_buffer_capacity").and_then(|v| v.as_u64()) {
554        let capacity = usize::try_from(capacity).ok()?;
555        builder = builder.ring_buffer_capacity(capacity);
556    }
557
558    if let Some(bp_value) = value.get("backpressure_mode") {
559        let bp_mode = if let Some(mode) = bp_value.as_str() {
560            match mode {
561                "DropNewest" | "drop_newest" => crate::config::BackpressureMode::DropNewest,
562                "DropOldest" | "drop_oldest" => crate::config::BackpressureMode::DropOldest,
563                "FailProducer" | "fail_producer" => crate::config::BackpressureMode::FailProducer,
564                // Pre-fix every other string silently fell back to
565                // `DropNewest`. A typo (`"DropOldset"`) thus
566                // changed durability profile at deploy time with
567                // no error. Reject unknowns to match the contract
568                // already enforced by `parse_poll_request_json`.
569                _ => return None,
570            }
571        } else if let Some(obj) = bp_value.as_object() {
572            // Object form: `{"Sample": {"rate": N}}` for the
573            // sampling mode that has an associated value.
574            if let Some(sample) = obj.get("Sample").or_else(|| obj.get("sample")) {
575                let rate = sample.get("rate").and_then(|v| v.as_u64())?;
576                let rate = u32::try_from(rate).ok()?;
577                if rate == 0 {
578                    // Validated again by `EventBusConfig::validate`,
579                    // but reject earlier so the parser surface
580                    // matches the validator surface.
581                    return None;
582                }
583                crate::config::BackpressureMode::Sample { rate }
584            } else {
585                return None;
586            }
587        } else {
588            return None;
589        };
590        builder = builder.backpressure_mode(bp_mode);
591    }
592
593    // Parse Redis config
594    #[cfg(feature = "redis")]
595    if let Some(redis) = value.get("redis") {
596        if let Some(url) = redis.get("url").and_then(|v| v.as_str()) {
597            let mut redis_config = RedisAdapterConfig::new(url);
598
599            if let Some(prefix) = redis.get("prefix").and_then(|v| v.as_str()) {
600                redis_config = redis_config.with_prefix(prefix);
601            }
602            if let Some(max_len) = redis.get("max_stream_len").and_then(|v| v.as_u64()) {
603                let max_len = usize::try_from(max_len).ok()?;
604                redis_config = redis_config.with_max_stream_len(max_len);
605            }
606            if let Some(pipeline_size) = redis.get("pipeline_size").and_then(|v| v.as_u64()) {
607                let pipeline_size = usize::try_from(pipeline_size).ok()?;
608                redis_config = redis_config.with_pipeline_size(pipeline_size);
609            }
610
611            builder = builder.adapter(AdapterConfig::Redis(redis_config));
612        }
613    }
614
615    // Parse JetStream config
616    #[cfg(feature = "jetstream")]
617    if let Some(jetstream) = value.get("jetstream") {
618        if let Some(url) = jetstream.get("url").and_then(|v| v.as_str()) {
619            let mut js_config = JetStreamAdapterConfig::new(url);
620
621            if let Some(prefix) = jetstream.get("prefix").and_then(|v| v.as_str()) {
622                js_config = js_config.with_prefix(prefix);
623            }
624            if let Some(max_messages) = jetstream.get("max_messages").and_then(|v| v.as_i64()) {
625                js_config = js_config.with_max_messages(max_messages);
626            }
627            if let Some(replicas) = jetstream.get("replicas").and_then(|v| v.as_u64()) {
628                let replicas = usize::try_from(replicas).ok()?;
629                js_config = js_config.with_replicas(replicas);
630            }
631
632            builder = builder.adapter(AdapterConfig::JetStream(js_config));
633        }
634    }
635
636    // Parse Net config
637    #[cfg(feature = "net")]
638    if let Some(net) = value.get("net") {
639        let bind_addr: std::net::SocketAddr = net
640            .get("bind_addr")
641            .and_then(|v| v.as_str())
642            .and_then(|s| s.parse().ok())?;
643
644        let peer_addr: std::net::SocketAddr = net
645            .get("peer_addr")
646            .and_then(|v| v.as_str())
647            .and_then(|s| s.parse().ok())?;
648
649        let psk: [u8; 32] = net
650            .get("psk")
651            .and_then(|v| v.as_str())
652            .and_then(|s| hex::decode(s).ok())
653            .and_then(|v| v.try_into().ok())?;
654
655        let role = net
656            .get("role")
657            .and_then(|v| v.as_str())
658            .unwrap_or("initiator");
659
660        let mut net_config = match role {
661            "initiator" => {
662                let peer_pubkey: [u8; 32] = net
663                    .get("peer_public_key")
664                    .and_then(|v| v.as_str())
665                    .and_then(|s| hex::decode(s).ok())
666                    .and_then(|v| v.try_into().ok())?;
667                NetAdapterConfig::initiator(bind_addr, peer_addr, psk, peer_pubkey)
668            }
669            "responder" => {
670                let secret_key: [u8; 32] = net
671                    .get("secret_key")
672                    .and_then(|v| v.as_str())
673                    .and_then(|s| hex::decode(s).ok())
674                    .and_then(|v| v.try_into().ok())?;
675                let public_key: [u8; 32] = net
676                    .get("public_key")
677                    .and_then(|v| v.as_str())
678                    .and_then(|s| hex::decode(s).ok())
679                    .and_then(|v| v.try_into().ok())?;
680                let keypair = StaticKeypair::from_keys(secret_key, public_key);
681                NetAdapterConfig::responder(bind_addr, peer_addr, psk, keypair)
682            }
683            _ => return None,
684        };
685
686        // Apply optional settings
687        if let Some(reliability) = net.get("reliability").and_then(|v| v.as_str()) {
688            net_config = net_config.with_reliability(match reliability {
689                "light" => ReliabilityConfig::Light,
690                "full" => ReliabilityConfig::Full,
691                _ => ReliabilityConfig::None,
692            });
693        }
694
695        if let Some(pool_size) = net.get("packet_pool_size").and_then(|v| v.as_u64()) {
696            if let Ok(size) = usize::try_from(pool_size) {
697                net_config = net_config.with_pool_size(size);
698            }
699        }
700
701        // Reject `0` for `heartbeat_interval_ms` and
702        // `session_timeout_ms`. `EventBusConfig::validate` rejects
703        // zero `Duration`s for `cooldown`, `metrics_window`, etc.,
704        // but the Net adapter's JSON parser had no equivalent guard
705        // — a `0` here flowed through to `Duration::from_millis(0)`,
706        // which on the heartbeat path busy-loops the heartbeat task
707        // and saturates a CPU. Treat zero as a misconfig and refuse
708        // to build the bus, surfacing as `InvalidJson` so the FFI
709        // caller sees a typed failure rather than a hung daemon.
710        if let Some(interval_ms) = net.get("heartbeat_interval_ms").and_then(|v| v.as_u64()) {
711            if interval_ms == 0 {
712                return None;
713            }
714            net_config =
715                net_config.with_heartbeat_interval(std::time::Duration::from_millis(interval_ms));
716        }
717
718        if let Some(timeout_ms) = net.get("session_timeout_ms").and_then(|v| v.as_u64()) {
719            if timeout_ms == 0 {
720                return None;
721            }
722            net_config =
723                net_config.with_session_timeout(std::time::Duration::from_millis(timeout_ms));
724        }
725
726        if let Some(batched) = net.get("batched_io").and_then(|v| v.as_bool()) {
727            net_config = net_config.with_batched_io(batched);
728        }
729
730        builder = builder.adapter(AdapterConfig::Net(Box::new(net_config)));
731    }
732
733    builder.build().ok()
734}
735
736fn create_with_config(runtime: Runtime, config: EventBusConfig) -> *mut NetHandle {
737    let bus = match runtime.block_on(EventBus::new(config)) {
738        Ok(bus) => bus,
739        Err(_) => {
740            // Send the runtime off to a fresh OS thread for
741            // dropping. Dropping a multi-thread tokio `Runtime`
742            // from inside another tokio runtime's worker thread
743            // panics ("Cannot drop a runtime in a context where
744            // blocking is not allowed"); a panic here would unwind
745            // across this `extern "C"` frame. The fresh thread
746            // guarantees a non-tokio context, so the drop is sound
747            // regardless of the caller's runtime environment. We
748            // don't `join()` the thread — the drop completes on
749            // its own and the caller has already been told
750            // `net_init` failed (returning null).
751            std::thread::spawn(move || drop(runtime));
752            return ptr::null_mut();
753        }
754    };
755
756    let handle = Box::new(NetHandle {
757        bus: std::mem::ManuallyDrop::new(bus),
758        runtime: std::mem::ManuallyDrop::new(runtime),
759        shutting_down: std::sync::atomic::AtomicBool::new(false),
760        active_ops: std::sync::atomic::AtomicU32::new(0),
761        bus_taken: std::sync::atomic::AtomicBool::new(false),
762        shutdown_completed: std::sync::atomic::AtomicBool::new(false),
763    });
764
765    Box::into_raw(handle)
766}
767
768/// Ingest a single event.
769///
770/// # Parameters
771///
772/// - `handle`: Event bus handle from `net_init`.
773/// - `event_json`: JSON event string (UTF-8).
774/// - `len`: Length of the event string in bytes.
775///
776/// # Returns
777///
778/// - `0` on success
779/// - Negative error code on failure
780#[unsafe(no_mangle)]
781pub unsafe extern "C" fn net_ingest(
782    handle: *mut NetHandle,
783    event_json: *const c_char,
784    len: usize,
785) -> c_int {
786    if !handle_is_valid(handle) || event_json.is_null() {
787        return NetError::NullPointer.into();
788    }
789
790    let handle = unsafe { &*handle };
791    let _guard = match enter_ffi_op(handle) {
792        Ok(g) => g,
793        Err(err) => return err,
794    };
795
796    // `slice::from_raw_parts` requires `len <= isize::MAX`. A
797    // C caller passing a sign-extended `-1` (or any
798    // `len > isize::MAX as usize`) triggers immediate UB before
799    // any other validation runs. Reject such inputs explicitly
800    // — caller should never see this in practice; surfacing a
801    // typed error is safer than UB.
802    if len > isize::MAX as usize {
803        return NetError::InvalidJson.into();
804    }
805    // Parse event JSON
806    let json_bytes = unsafe { std::slice::from_raw_parts(event_json as *const u8, len) };
807    let json_str = match std::str::from_utf8(json_bytes) {
808        Ok(s) => s,
809        Err(_) => return NetError::InvalidUtf8.into(),
810    };
811
812    let event = match Event::from_str(json_str) {
813        Ok(e) => e,
814        Err(_) => return NetError::InvalidJson.into(),
815    };
816
817    // Ingest
818    match handle.bus.ingest(event) {
819        Ok(_) => NetError::Success.into(),
820        Err(_) => NetError::IngestionFailed.into(),
821    }
822}
823
824/// Ingest a raw JSON string (fastest path).
825///
826/// The JSON string is stored directly without parsing.
827/// This is the recommended method for high-throughput ingestion.
828///
829/// # Parameters
830///
831/// - `handle`: Event bus handle from `net_init`.
832/// - `json`: JSON string (UTF-8).
833/// - `len`: Length of the JSON string in bytes.
834///
835/// # Returns
836///
837/// - `0` on success
838/// - Negative error code on failure
839#[unsafe(no_mangle)]
840pub unsafe extern "C" fn net_ingest_raw(
841    handle: *mut NetHandle,
842    json: *const c_char,
843    len: usize,
844) -> c_int {
845    if !handle_is_valid(handle) || json.is_null() {
846        return NetError::NullPointer.into();
847    }
848
849    let handle = unsafe { &*handle };
850    let _guard = match enter_ffi_op(handle) {
851        Ok(g) => g,
852        Err(err) => return err,
853    };
854
855    // `slice::from_raw_parts` requires `len <= isize::MAX`.
856    if len > isize::MAX as usize {
857        return NetError::InvalidJson.into();
858    }
859    let json_bytes = unsafe { std::slice::from_raw_parts(json as *const u8, len) };
860    let json_str = match std::str::from_utf8(json_bytes) {
861        Ok(s) => s,
862        Err(_) => return NetError::InvalidUtf8.into(),
863    };
864
865    let raw = RawEvent::from_str(json_str);
866
867    match handle.bus.ingest_raw(raw) {
868        Ok(_) => NetError::Success.into(),
869        Err(_) => NetError::IngestionFailed.into(),
870    }
871}
872
873/// Ingest multiple raw JSON strings (fastest batch path).
874///
875/// # Parameters
876///
877/// - `handle`: Event bus handle.
878/// - `jsons`: Array of pointers to JSON strings.
879/// - `lens`: Array of lengths for each JSON string.
880/// - `count`: Number of events in the arrays.
881///
882/// # Returns
883///
884/// Number of successfully ingested events, or negative error code.
885#[unsafe(no_mangle)]
886pub unsafe extern "C" fn net_ingest_raw_batch(
887    handle: *mut NetHandle,
888    jsons: *const *const c_char,
889    lens: *const usize,
890    count: usize,
891) -> c_int {
892    if !handle_is_valid(handle) || jsons.is_null() || lens.is_null() {
893        return NetError::NullPointer.into();
894    }
895    if count == 0 {
896        return 0;
897    }
898
899    let handle = unsafe { &*handle };
900    let _guard = match enter_ffi_op(handle) {
901        Ok(g) => g,
902        Err(err) => return err,
903    };
904    let mut events = Vec::with_capacity(count);
905    // Track per-entry drops so the caller's accounting can
906    // reconcile the returned count against the input count.
907    // Pre-fix per-entry rejects (null pointer, oversized length,
908    // invalid UTF-8) were silently `continue`-d and the caller
909    // saw `count - drops` accepted events without any signal as
910    // to which input indices were dropped. A binding that
911    // attributed the drop to back-pressure and retried got the
912    // wrong indices and double-published the good ones.
913    //
914    // The C-API contract is "returns count of accepted events";
915    // expanding it to take an out-param of dropped indices is
916    // an API addition, not a fix-in-place. Emit `tracing::warn!`
917    // with the offending index AND reason so operators
918    // observing the bus can correlate drop counts to specific
919    // inputs without changing the C surface. For high-volume
920    // bindings this should still be sized at one log line per
921    // dropped entry; if that ever matters in practice the
922    // `*_ex` follow-up can return the indices structurally.
923    let mut dropped_null = 0usize;
924    let mut dropped_oversize = 0usize;
925    let mut dropped_invalid_utf8 = 0usize;
926
927    for i in 0..count {
928        let json_ptr = unsafe { *jsons.add(i) };
929        let len = unsafe { *lens.add(i) };
930
931        if json_ptr.is_null() {
932            tracing::warn!(
933                index = i,
934                "net_ingest_raw_batch: dropping entry with null pointer"
935            );
936            dropped_null += 1;
937            continue;
938        }
939
940        // `slice::from_raw_parts` requires `len <= isize::MAX`.
941        // Skip pathological per-entry lengths rather than UB.
942        if len > isize::MAX as usize {
943            tracing::warn!(
944                index = i,
945                len,
946                "net_ingest_raw_batch: dropping entry with len > isize::MAX"
947            );
948            dropped_oversize += 1;
949            continue;
950        }
951        let json_bytes = unsafe { std::slice::from_raw_parts(json_ptr as *const u8, len) };
952        match std::str::from_utf8(json_bytes) {
953            Ok(json_str) => events.push(RawEvent::from_str(json_str)),
954            Err(_) => {
955                tracing::warn!(
956                    index = i,
957                    "net_ingest_raw_batch: dropping entry with invalid UTF-8"
958                );
959                dropped_invalid_utf8 += 1;
960            }
961        }
962    }
963    let total_dropped = dropped_null + dropped_oversize + dropped_invalid_utf8;
964    if total_dropped > 0 {
965        // Aggregate summary for log-pipeline filters that fold
966        // per-index lines.
967        tracing::warn!(
968            input_count = count,
969            dropped_null,
970            dropped_oversize,
971            dropped_invalid_utf8,
972            "net_ingest_raw_batch: {} of {} entries dropped before ingest",
973            total_dropped,
974            count,
975        );
976    }
977
978    let count = handle.bus.ingest_raw_batch(events);
979    // Returning `c_int::MAX` on overflow would be ambiguous with a real
980    // `INT_MAX` ingest. Signal overflow explicitly so callers doing
981    // accounting in high-throughput paths do not silently miscount.
982    c_int::try_from(count).unwrap_or_else(|_| NetError::IntOverflow.into())
983}
984
985/// Ingest multiple events.
986///
987/// # Parameters
988///
989/// - `handle`: Event bus handle.
990/// - `events_json`: JSON array of events (UTF-8, null-terminated).
991///
992/// # Returns
993///
994/// Number of successfully ingested events, or negative error code.
995#[unsafe(no_mangle)]
996pub unsafe extern "C" fn net_ingest_batch(
997    handle: *mut NetHandle,
998    events_json: *const c_char,
999) -> c_int {
1000    if !handle_is_valid(handle) || events_json.is_null() {
1001        return NetError::NullPointer.into();
1002    }
1003
1004    let handle = unsafe { &*handle };
1005    let _guard = match enter_ffi_op(handle) {
1006        Ok(g) => g,
1007        Err(err) => return err,
1008    };
1009
1010    let json_str = match unsafe { CStr::from_ptr(events_json) }.to_str() {
1011        Ok(s) => s,
1012        Err(_) => return NetError::InvalidUtf8.into(),
1013    };
1014
1015    // Parse as JSON array
1016    let array: Vec<serde_json::Value> = match serde_json::from_str(json_str) {
1017        Ok(a) => a,
1018        Err(_) => return NetError::InvalidJson.into(),
1019    };
1020
1021    let events: Vec<Event> = array.into_iter().map(Event::new).collect();
1022    let count = handle.bus.ingest_batch(events);
1023
1024    // Returning `c_int::MAX` on overflow would be ambiguous with a real
1025    // `INT_MAX` ingest. Signal overflow explicitly — matches the
1026    // `net_ingest_raw_batch` contract.
1027    c_int::try_from(count).unwrap_or_else(|_| NetError::IntOverflow.into())
1028}
1029
1030/// Parse the JSON request body passed to `net_poll` into a
1031/// `ConsumeRequest`. Returns the negative `NetError` code on parse
1032/// failure so the caller can surface it back across FFI. Both `limit`
1033/// and `cursor` are optional, but if either key is present with the
1034/// wrong JSON type it is an explicit error — silently falling back to
1035/// the default would hide caller bugs (e.g. the Go binding that
1036/// previously serialized `cursor` but had it dropped server-side).
1037fn parse_poll_request_json(json_str: &str) -> Result<ConsumeRequest, c_int> {
1038    let value: serde_json::Value =
1039        serde_json::from_str(json_str).map_err(|_| c_int::from(NetError::InvalidJson))?;
1040
1041    let limit = match value.get("limit") {
1042        None | Some(serde_json::Value::Null) => 100usize,
1043        Some(v) => match v.as_u64() {
1044            // `as usize` would silently truncate on 32-bit targets for
1045            // values above `usize::MAX`. Reject such inputs explicitly
1046            // so a caller asking for e.g. 2^33 events on a wasm32
1047            // build gets `InvalidJson` instead of a tiny wrap-around.
1048            Some(n) => usize::try_from(n).map_err(|_| c_int::from(NetError::InvalidJson))?,
1049            None => return Err(NetError::InvalidJson.into()),
1050        },
1051    };
1052    let cursor = match value.get("cursor") {
1053        None | Some(serde_json::Value::Null) => None,
1054        Some(v) => match v.as_str() {
1055            Some(s) => Some(s.to_owned()),
1056            None => return Err(NetError::InvalidJson.into()),
1057        },
1058    };
1059    let mut req = ConsumeRequest::new(limit);
1060    req.from_id = cursor;
1061    Ok(req)
1062}
1063
1064/// Poll events from the bus.
1065///
1066/// # Parameters
1067///
1068/// - `handle`: Event bus handle.
1069/// - `request_json`: JSON request string (UTF-8, null-terminated).
1070///   Example: `{"limit": 100, "ordering": "InsertionTs"}`
1071/// - `out_buffer`: Output buffer for JSON response.
1072/// - `buffer_len`: Size of the output buffer.
1073///
1074/// # Returns
1075///
1076/// - Number of bytes written to buffer on success
1077/// - Negative error code on failure
1078#[unsafe(no_mangle)]
1079pub unsafe extern "C" fn net_poll(
1080    handle: *mut NetHandle,
1081    request_json: *const c_char,
1082    out_buffer: *mut c_char,
1083    buffer_len: usize,
1084) -> c_int {
1085    if !handle_is_valid(handle) || out_buffer.is_null() {
1086        return NetError::NullPointer.into();
1087    }
1088
1089    let handle = unsafe { &*handle };
1090    let _guard = match enter_ffi_op(handle) {
1091        Ok(g) => g,
1092        Err(err) => return err,
1093    };
1094
1095    // Parse request
1096    let request = if request_json.is_null() {
1097        ConsumeRequest::new(100)
1098    } else {
1099        let json_str = match unsafe { CStr::from_ptr(request_json) }.to_str() {
1100            Ok(s) => s,
1101            Err(_) => return NetError::InvalidUtf8.into(),
1102        };
1103        match parse_poll_request_json(json_str) {
1104            Ok(req) => req,
1105            Err(code) => return code,
1106        }
1107    };
1108
1109    // Reject buffers too small to even hold an empty-response
1110    // JSON envelope. This catches the degenerate "tiny buffer"
1111    // case before we hit the adapter — `BufferTooSmall` returned
1112    // here means "no work was done, caller's cursor is unchanged."
1113    // 256 bytes comfortably fits the empty-response JSON below
1114    // even with a long echoed `next_id` cursor.
1115    const MIN_RESPONSE_BUFFER: usize = 256;
1116    if buffer_len < MIN_RESPONSE_BUFFER {
1117        return NetError::BufferTooSmall.into();
1118    }
1119
1120    // Stash the cursor before moving `request` into `poll()` so
1121    // the post-poll fallback can echo it back to the caller. On
1122    // overflow we write a minimal "no events delivered, cursor
1123    // unchanged" response so the caller's next poll re-fetches
1124    // the same range — events are not lost on idempotent
1125    // adapters (Redis XRANGE, JetStream direct_get).
1126    let cursor_snapshot = request.from_id.clone();
1127
1128    // Poll
1129    let response = match handle.runtime.block_on(handle.bus.poll(request)) {
1130        Ok(r) => r,
1131        Err(_) => return NetError::PollFailed.into(),
1132    };
1133
1134    // Serialize response. Events that fail to parse are included as raw
1135    // strings so the caller can see all events and detect parse failures.
1136    let total_events = response.events.len();
1137    let mut parsed_events: Vec<serde_json::Value> = Vec::with_capacity(total_events);
1138    let mut parse_errors: usize = 0;
1139    for e in &response.events {
1140        match e.parse() {
1141            Ok(v) => parsed_events.push(v),
1142            Err(_) => {
1143                parse_errors += 1;
1144                // Include the raw bytes as a string so the caller doesn't silently lose events
1145                if let Ok(raw) = e.raw_str() {
1146                    parsed_events.push(serde_json::Value::String(raw.to_string()));
1147                }
1148            }
1149        }
1150    }
1151    let response_json = match serde_json::to_string(&serde_json::json!({
1152        "events": parsed_events,
1153        "next_id": response.next_id,
1154        "has_more": response.has_more,
1155        "count": parsed_events.len(),
1156        "parse_errors": parse_errors,
1157    })) {
1158        Ok(s) => s,
1159        Err(_) => return NetError::Unknown.into(),
1160    };
1161
1162    // Buffer overflow: emit a minimal fallback response that echoes
1163    // the caller's original cursor as `next_id`. The caller's next
1164    // poll runs against the same range and re-delivers the events
1165    // (idempotent on Redis XRANGE / JetStream direct_get). Without
1166    // this, a caller that trusts `next_id` blindly would advance
1167    // past the unread batch.
1168    if response_json.len() + 1 > buffer_len {
1169        let fallback = serde_json::to_string(&serde_json::json!({
1170            "events": [],
1171            "next_id": cursor_snapshot,
1172            "has_more": true,
1173            "count": 0,
1174            "parse_errors": 0,
1175            "buffer_too_small": true,
1176            "events_dropped": total_events,
1177        }))
1178        .unwrap_or_else(|_| String::from(
1179            r#"{"events":[],"next_id":null,"has_more":true,"count":0,"parse_errors":0,"buffer_too_small":true}"#
1180        ));
1181        if fallback.len() < buffer_len {
1182            unsafe {
1183                ptr::copy_nonoverlapping(
1184                    fallback.as_ptr() as *const c_char,
1185                    out_buffer,
1186                    fallback.len(),
1187                );
1188                *out_buffer.add(fallback.len()) = 0;
1189            }
1190        }
1191        return NetError::BufferTooSmall.into();
1192    }
1193
1194    // Copy to output buffer
1195    unsafe {
1196        ptr::copy_nonoverlapping(
1197            response_json.as_ptr() as *const c_char,
1198            out_buffer,
1199            response_json.len(),
1200        );
1201        *out_buffer.add(response_json.len()) = 0; // Null terminate
1202    }
1203
1204    // Data was already copied into the caller's buffer; a
1205    // `c_int` overflow here means the byte count exceeds c_int's
1206    // range, NOT that the buffer was too small. Returning
1207    // `BufferTooSmall` would tell the caller to "resize and retry"
1208    // when retrying can't fix the actual condition. `IntOverflow`
1209    // is the documented variant for this case.
1210    match c_int::try_from(response_json.len()) {
1211        Ok(n) => n,
1212        Err(_) => NetError::IntOverflow.into(),
1213    }
1214}
1215
1216/// Get event bus statistics.
1217///
1218/// # Parameters
1219///
1220/// - `handle`: Event bus handle.
1221/// - `out_buffer`: Output buffer for JSON statistics.
1222/// - `buffer_len`: Size of the output buffer.
1223///
1224/// # Returns
1225///
1226/// Number of bytes written, or negative error code.
1227#[unsafe(no_mangle)]
1228pub unsafe extern "C" fn net_stats(
1229    handle: *mut NetHandle,
1230    out_buffer: *mut c_char,
1231    buffer_len: usize,
1232) -> c_int {
1233    if !handle_is_valid(handle) || out_buffer.is_null() {
1234        return NetError::NullPointer.into();
1235    }
1236
1237    let handle = unsafe { &*handle };
1238    let _guard = match enter_ffi_op(handle) {
1239        Ok(g) => g,
1240        Err(err) => return err,
1241    };
1242    let stats = handle.bus.stats();
1243    let shard_stats = handle.bus.shard_stats();
1244
1245    let stats_json = match serde_json::to_string(&serde_json::json!({
1246        "events_ingested": stats.events_ingested.load(std::sync::atomic::Ordering::Relaxed),
1247        "events_dropped": stats.events_dropped.load(std::sync::atomic::Ordering::Relaxed),
1248        "batches_dispatched": stats.batches_dispatched.load(std::sync::atomic::Ordering::Relaxed),
1249        "shard_events_ingested": shard_stats.events_ingested,
1250        "shard_events_dropped": shard_stats.events_dropped,
1251        "shard_batches_dispatched": shard_stats.batches_dispatched,
1252    })) {
1253        Ok(s) => s,
1254        Err(_) => return NetError::Unknown.into(),
1255    };
1256
1257    if stats_json.len() + 1 > buffer_len {
1258        return NetError::BufferTooSmall.into();
1259    }
1260
1261    unsafe {
1262        ptr::copy_nonoverlapping(
1263            stats_json.as_ptr() as *const c_char,
1264            out_buffer,
1265            stats_json.len(),
1266        );
1267        *out_buffer.add(stats_json.len()) = 0;
1268    }
1269
1270    // See net_poll above — the data was already copied, so an
1271    // overflowing length is `IntOverflow`, not `BufferTooSmall`.
1272    match c_int::try_from(stats_json.len()) {
1273        Ok(n) => n,
1274        Err(_) => NetError::IntOverflow.into(),
1275    }
1276}
1277
1278/// Flush all pending batches to the adapter.
1279///
1280/// # Parameters
1281///
1282/// - `handle`: Event bus handle.
1283///
1284/// # Returns
1285///
1286/// - `0` on success
1287/// - Negative error code on failure
1288#[unsafe(no_mangle)]
1289pub unsafe extern "C" fn net_flush(handle: *mut NetHandle) -> c_int {
1290    if !handle_is_valid(handle) {
1291        return NetError::NullPointer.into();
1292    }
1293
1294    let handle = unsafe { &*handle };
1295    let _guard = match enter_ffi_op(handle) {
1296        Ok(g) => g,
1297        Err(err) => return err,
1298    };
1299
1300    match handle.runtime.block_on(handle.bus.flush()) {
1301        Ok(_) => NetError::Success.into(),
1302        Err(_) => NetError::Unknown.into(),
1303    }
1304}
1305
1306/// Shut down the event bus and free resources.
1307///
1308/// # Parameters
1309///
1310/// - `handle`: Event bus handle. After this call, the handle is invalid.
1311///
1312/// # Returns
1313///
1314/// - `0` on success
1315/// - Negative error code on failure (including `Unknown` if the
1316///   bounded wait for in-flight FFI operations expired before the bus
1317///   could be shut down cleanly)
1318///
1319/// # Notes
1320///
1321/// The handle's storage is intentionally leaked: the box is never
1322/// returned to the allocator. See `NetHandle`'s docs for why. This is
1323/// a one-time cost per shutdown — typically per-process, since most C
1324/// callers initialize the bus once and shut down once.
1325#[unsafe(no_mangle)]
1326pub unsafe extern "C" fn net_shutdown(handle: *mut NetHandle) -> c_int {
1327    if !handle_is_valid(handle) {
1328        return NetError::NullPointer.into();
1329    }
1330
1331    // Scope the `&NetHandle` borrow into an inner block so it is
1332    // verifiably out of scope before the
1333    // `ManuallyDrop::take(&mut (*handle).bus)` calls below.
1334    // Holding an `&NetHandle` in scope for the whole function
1335    // while taking a raw `&mut (*handle).bus` later would rely on
1336    // NLL ending the immutable borrow before the mutable take —
1337    // a pattern fragile under stacked/tree borrow models. The
1338    // block-scoped borrow makes the lifetime constraint explicit
1339    // and obvious to both the compiler and any future maintainer.
1340    let drained_and_taken = {
1341        // SAFETY: The C contract guarantees `handle` is valid here and that
1342        // `net_shutdown` is not called concurrently with itself. Future
1343        // dereferences of the box from concurrent FFI ops on other threads
1344        // are also sound because we never free the box (see below).
1345        let handle_ref = unsafe { &*handle };
1346
1347        // Signal shutdown so concurrent FFI calls bail before touching
1348        // `bus`/`runtime`. SeqCst pairs with `FfiOpGuard::try_enter`.
1349        handle_ref
1350            .shutting_down
1351            .store(true, std::sync::atomic::Ordering::SeqCst);
1352
1353        // Bounded wait for in-flight ops to drain. Without a deadline, a
1354        // hung concurrent operation (e.g. `net_flush` against a stalled
1355        // adapter) would pin a CPU at 100% inside this loop forever.
1356        //
1357        // `std::hint::spin_loop()` is a CPU pause hint, not a yield. On
1358        // a single-threaded executor (or any configuration where the FFI
1359        // caller's thread is the same one that needs to make progress on
1360        // the in-flight async work) the tight spin starves the very tokio
1361        // worker we're waiting for, *causing* the deadline to expire when
1362        // it otherwise wouldn't. `thread::yield_now` lets the OS schedule
1363        // whatever's blocked, and a 1ms `thread::sleep` between yields
1364        // prevents the loop from saturating a CPU on platforms where
1365        // `yield_now` is a
1366        // near-no-op under low contention. The drain we expect to take
1367        // milliseconds, so a millisecond-granularity poll is fine.
1368        let deadline = std::time::Instant::now() + FFI_SHUTDOWN_DEADLINE;
1369        let mut drained = false;
1370        loop {
1371            if handle_ref
1372                .active_ops
1373                .load(std::sync::atomic::Ordering::SeqCst)
1374                == 0
1375            {
1376                drained = true;
1377                break;
1378            }
1379            if std::time::Instant::now() >= deadline {
1380                break;
1381            }
1382            std::thread::yield_now();
1383            std::thread::sleep(std::time::Duration::from_millis(1));
1384        }
1385
1386        if !drained {
1387            // In-flight ops may still be reading `bus`/`runtime`; reading
1388            // them out via `ManuallyDrop::take` would race those readers.
1389            // Leak both fields along with the box. Future ops still see
1390            // `shutting_down=true` and bail before touching either field,
1391            // so the leaked memory is never read again.
1392            return NetError::Unknown.into();
1393        }
1394
1395        // Idempotent shutdown: if a previous `net_shutdown` already
1396        // moved out the bus/runtime, do not call `ManuallyDrop::take`
1397        // a second time (that would be UB). The first call may still
1398        // be inside `runtime.block_on(bus.shutdown())` though — pre-
1399        // fix the second caller observed `bus_taken == true` and
1400        // returned `Success` immediately, falsely signaling
1401        // completion of an in-progress shutdown. Spin on
1402        // `shutdown_completed` (set by the first caller AFTER
1403        // `bus.shutdown()` returns) so subsequent callers wait for
1404        // the actual completion.
1405        if handle_ref
1406            .bus_taken
1407            .swap(true, std::sync::atomic::Ordering::SeqCst)
1408        {
1409            // Wait for the first caller to actually finish.
1410            // Bounded by the same FFI_SHUTDOWN_DEADLINE as the
1411            // `active_ops` drain — if the first caller is wedged
1412            // longer than that, we surface a Transient error rather
1413            // than block forever.
1414            let inner_deadline = std::time::Instant::now() + FFI_SHUTDOWN_DEADLINE;
1415            while !handle_ref
1416                .shutdown_completed
1417                .load(std::sync::atomic::Ordering::Acquire)
1418            {
1419                if std::time::Instant::now() >= inner_deadline {
1420                    return NetError::Unknown.into();
1421                }
1422                std::thread::yield_now();
1423                std::thread::sleep(std::time::Duration::from_millis(1));
1424            }
1425            return NetError::Success.into();
1426        }
1427        drained
1428    };
1429    let _ = drained_and_taken;
1430
1431    // SAFETY: `active_ops` reached zero with `shutting_down=true`, so:
1432    //   - Every FFI op that started before shutdown has fully
1433    //     completed (decremented `active_ops` on guard drop).
1434    //   - Any future FFI op will observe `shutting_down=true` and
1435    //     bail in `try_enter` before touching `bus` / `runtime`.
1436    // Plus, `bus_taken` was just CAS'd from false → true, so no other
1437    // shutdown is concurrently moving the same fields out. The
1438    // immutable `handle_ref` borrow above has been dropped (block
1439    // scope ended), so the `&mut`-via-raw-pointer below is the
1440    // only live access — no stacked/tree-borrow race.
1441    //
1442    // We deliberately do NOT call `Box::from_raw` here. The box's
1443    // `shutting_down` / `active_ops` / `bus_taken` atomics must remain
1444    // valid memory because future FFI ops still dereference the
1445    // C-side pointer to check them. Leaking the box is the
1446    // correctness fix for the previous use-after-free; the per-handle
1447    // storage cost is a one-time overhead.
1448    let bus = unsafe { std::mem::ManuallyDrop::take(&mut (*handle).bus) };
1449    let runtime = unsafe { std::mem::ManuallyDrop::take(&mut (*handle).runtime) };
1450
1451    // Flush pending batches and gracefully shut down the adapter
1452    // before dropping the runtime. Without this, pending events in
1453    // ring buffers and batch workers would be silently lost.
1454    let result = runtime.block_on(bus.shutdown());
1455
1456    // `bus` and `runtime` go out of scope here and are dropped.
1457    // The leaked box keeps the atomics alive for any straggler ops.
1458
1459    // Signal completion to any second/third caller spinning on
1460    // `shutdown_completed` in the idempotent path above. Done
1461    // AFTER `bus.shutdown()` returns and AFTER the bus / runtime
1462    // drop, so subsequent callers can rely on this flag as a
1463    // hard "shutdown is fully done" barrier.
1464    unsafe { &*handle }
1465        .shutdown_completed
1466        .store(true, std::sync::atomic::Ordering::Release);
1467
1468    match result {
1469        Ok(()) => NetError::Success.into(),
1470        Err(_) => NetError::Unknown.into(),
1471    }
1472}
1473
1474/// Get the number of shards.
1475///
1476/// # Parameters
1477///
1478/// - `handle`: Event bus handle.
1479///
1480/// # Returns
1481///
1482/// Number of shards, or 0 if handle is null.
1483#[unsafe(no_mangle)]
1484pub unsafe extern "C" fn net_num_shards(handle: *mut NetHandle) -> u16 {
1485    if !handle_is_valid(handle) {
1486        return 0;
1487    }
1488    let handle = unsafe { &*handle };
1489    let _guard = match enter_ffi_op(handle) {
1490        Ok(g) => g,
1491        Err(_) => return 0,
1492    };
1493    handle.bus.num_shards()
1494}
1495
1496/// Get the library version.
1497///
1498/// # Returns
1499///
1500/// Version string (static, do not free).
1501#[unsafe(no_mangle)]
1502pub unsafe extern "C" fn net_version() -> *const c_char {
1503    static VERSION: &[u8] = b"0.8.0\0";
1504    VERSION.as_ptr() as *const c_char
1505}
1506
1507/// Generate a new Net keypair.
1508///
1509/// # Returns
1510///
1511/// JSON string with hex-encoded public_key and secret_key.
1512/// The caller must free the returned string with `net_free_string`.
1513/// Returns NULL if Net feature is not enabled.
1514#[cfg(feature = "net")]
1515#[unsafe(no_mangle)]
1516pub unsafe extern "C" fn net_generate_keypair() -> *mut c_char {
1517    let keypair = StaticKeypair::generate();
1518    let json = serde_json::json!({
1519        "public_key": hex::encode(keypair.public_key()),
1520        "secret_key": hex::encode(keypair.secret_key()),
1521    });
1522
1523    match CString::new(json.to_string()) {
1524        Ok(s) => s.into_raw(),
1525        Err(_) => ptr::null_mut(),
1526    }
1527}
1528
1529/// Free a string returned by Net functions.
1530///
1531/// # Parameters
1532///
1533/// - `s`: String pointer returned by `net_generate_keypair` or similar.
1534#[cfg(feature = "net")]
1535#[unsafe(no_mangle)]
1536pub unsafe extern "C" fn net_free_string(s: *mut c_char) {
1537    if !s.is_null() {
1538        unsafe {
1539            drop(CString::from_raw(s));
1540        }
1541    }
1542}
1543
1544// `net.h` declares both `net_generate_keypair` and
1545// `net_free_string` unconditionally — a consumer linking against
1546// a cdylib built without the `net` feature would otherwise hit
1547// a load-time missing-symbol error despite the header advertising
1548// the symbol. Provide always-empty stubs so the symbol is
1549// resolvable on every build configuration. Mirrors the
1550// `nat-traversal` cfg pattern in `mesh.rs`.
1551
1552/// Stub for builds without the `net` feature.
1553///
1554/// `net.h` declares `net_generate_keypair` unconditionally, so
1555/// the symbol must be resolvable on every build configuration.
1556/// Returns NULL since keypair generation requires the net feature.
1557#[cfg(not(feature = "net"))]
1558#[unsafe(no_mangle)]
1559pub unsafe extern "C" fn net_generate_keypair() -> *mut c_char {
1560    ptr::null_mut()
1561}
1562
1563/// Stub for builds without the `net` feature.
1564///
1565/// Mirrors the always-on signature in `net.h`. Reclaims a
1566/// CString-allocated pointer if non-null.
1567#[cfg(not(feature = "net"))]
1568#[unsafe(no_mangle)]
1569pub unsafe extern "C" fn net_free_string(s: *mut c_char) {
1570    if !s.is_null() {
1571        unsafe {
1572            drop(std::ffi::CString::from_raw(s));
1573        }
1574    }
1575}
1576
1577// =========================================================================
1578// Structured (non-JSON) API — _ex variants
1579// =========================================================================
1580
1581/// Ingestion receipt for C consumers.
1582#[repr(C)]
1583pub struct NetReceipt {
1584    /// Shard the event was assigned to.
1585    pub shard_id: u16,
1586    /// Insertion timestamp (nanoseconds).
1587    pub timestamp: u64,
1588}
1589
1590// Pin layout invariants for `NetReceipt`. `#[repr(C)]` already
1591// gives C ABI compatibility per platform, but doesn't catch a
1592// future field-reorder or field-add — both would silently break
1593// any C/Go/Python binding that hard-codes the struct layout.
1594// Static asserts on 64-bit targets (the production deployment
1595// shape) trip CI before such a change reaches a binary release.
1596//
1597// 64-bit: `u16 (2) + 6 pad + u64 (8)` = 16 bytes, alignment 8.
1598#[cfg(target_pointer_width = "64")]
1599const _: () = assert!(
1600    std::mem::size_of::<NetReceipt>() == 16,
1601    "NetReceipt size changed on 64-bit; bindings hard-code 16. \
1602     If the change is intentional, bump the binding versions and \
1603     update this assertion."
1604);
1605#[cfg(target_pointer_width = "64")]
1606const _: () = assert!(
1607    std::mem::align_of::<NetReceipt>() == 8,
1608    "NetReceipt alignment changed on 64-bit; bindings expect 8."
1609);
1610
1611/// A single stored event for C consumers.
1612///
1613/// # Safety contract for callers
1614///
1615/// `id`/`id_len` and `raw`/`raw_len` are produced by Rust as a
1616/// `Box<[u8]>` whose fat-pointer length is reconstructed at free
1617/// time from `id_len` / `raw_len`. The fields are `pub` because
1618/// `#[repr(C)]` exposes them to C, **but they must be treated as
1619/// read-only** between the `net_poll_*` call that produced them
1620/// and the `net_free_poll_result` that consumes them.
1621///
1622/// Mutating `id_len` or `raw_len` (or copying the struct, replacing
1623/// the pointer, and then freeing) causes
1624/// `Box::from_raw(slice_from_raw_parts_mut(ptr, wrong_len))` to be
1625/// undefined behavior on free — the allocator records the
1626/// allocation size and any mismatch is UB.
1627#[repr(C)]
1628pub struct NetEvent {
1629    /// Event ID (not null-terminated, use `id_len`).
1630    /// Read-only after `net_poll_*`; do not mutate.
1631    pub id: *const c_char,
1632    /// Length of the event ID. Read-only after `net_poll_*`; do not
1633    /// mutate (mutation causes UB on free).
1634    pub id_len: usize,
1635    /// Raw JSON payload (not null-terminated, use `raw_len`).
1636    /// Read-only after `net_poll_*`; do not mutate.
1637    pub raw: *const c_char,
1638    /// Length of the raw JSON payload. Read-only after
1639    /// `net_poll_*`; do not mutate (mutation causes UB on free).
1640    pub raw_len: usize,
1641    /// Insertion timestamp (nanoseconds).
1642    pub insertion_ts: u64,
1643    /// Shard ID.
1644    pub shard_id: u16,
1645}
1646
1647// Pin layout invariants for `NetEvent`. See `NetReceipt`'s
1648// asserts for rationale. Bindings (C, Go, Python, Node) hard-
1649// code 48 bytes on 64-bit; an accidental reorder or new field
1650// would silently shift every offset.
1651//
1652// 64-bit: `4 × 8 (ptrs/usize) + u64 (8) + u16 (2) + 6 trail` = 48.
1653#[cfg(target_pointer_width = "64")]
1654const _: () = assert!(
1655    std::mem::size_of::<NetEvent>() == 48,
1656    "NetEvent size changed on 64-bit; bindings hard-code 48. \
1657     If the change is intentional, bump the binding versions and \
1658     update this assertion."
1659);
1660#[cfg(target_pointer_width = "64")]
1661const _: () = assert!(
1662    std::mem::align_of::<NetEvent>() == 8,
1663    "NetEvent alignment changed on 64-bit; bindings expect 8."
1664);
1665
1666/// Poll result for C consumers.
1667#[repr(C)]
1668pub struct NetPollResult {
1669    /// Array of events. Free with `net_free_poll_result`.
1670    pub events: *mut NetEvent,
1671    /// Number of events in the array.
1672    pub count: usize,
1673    /// Cursor for the next poll (null-terminated). NULL if no more.
1674    pub next_id: *mut c_char,
1675    /// 1 if more events are available, 0 otherwise.
1676    pub has_more: c_int,
1677}
1678
1679/// Stats for C consumers.
1680#[repr(C)]
1681pub struct NetStats {
1682    /// Total events ingested.
1683    pub events_ingested: u64,
1684    /// Events dropped due to backpressure.
1685    pub events_dropped: u64,
1686    /// Batches dispatched to the adapter.
1687    pub batches_dispatched: u64,
1688}
1689
1690/// Ingest raw JSON with structured receipt.
1691#[unsafe(no_mangle)]
1692pub unsafe extern "C" fn net_ingest_raw_ex(
1693    handle: *mut NetHandle,
1694    json: *const c_char,
1695    len: usize,
1696    out: *mut NetReceipt,
1697) -> c_int {
1698    if !handle_is_valid(handle) || json.is_null() {
1699        return NetError::NullPointer.into();
1700    }
1701
1702    let handle = unsafe { &*handle };
1703    let _guard = match enter_ffi_op(handle) {
1704        Ok(g) => g,
1705        Err(err) => return err,
1706    };
1707
1708    // `slice::from_raw_parts` requires `len <= isize::MAX`.
1709    if len > isize::MAX as usize {
1710        return NetError::InvalidJson.into();
1711    }
1712    let json_bytes = unsafe { std::slice::from_raw_parts(json as *const u8, len) };
1713    let json_str = match std::str::from_utf8(json_bytes) {
1714        Ok(s) => s,
1715        Err(_) => return NetError::InvalidUtf8.into(),
1716    };
1717
1718    let raw = RawEvent::from_str(json_str);
1719
1720    match handle.bus.ingest_raw(raw) {
1721        Ok((shard_id, timestamp)) => {
1722            if !out.is_null() {
1723                unsafe {
1724                    (*out).shard_id = shard_id;
1725                    (*out).timestamp = timestamp;
1726                }
1727            }
1728            NetError::Success.into()
1729        }
1730        Err(_) => NetError::IngestionFailed.into(),
1731    }
1732}
1733
1734/// Poll events with structured result (no JSON overhead).
1735///
1736/// The caller must free the result with `net_free_poll_result`.
1737#[unsafe(no_mangle)]
1738pub unsafe extern "C" fn net_poll_ex(
1739    handle: *mut NetHandle,
1740    limit: usize,
1741    cursor: *const c_char,
1742    out: *mut NetPollResult,
1743) -> c_int {
1744    if !handle_is_valid(handle) || out.is_null() {
1745        return NetError::NullPointer.into();
1746    }
1747
1748    // Pre-validate `limit` BEFORE calling `bus.poll` — the bus
1749    // advances the consumer cursor before returning, so any
1750    // post-poll allocation failure (e.g. `Layout::array::<NetEvent>`
1751    // overflow on a pathological `count`, or `std::alloc::alloc`
1752    // returning null under memory pressure) would drop the response
1753    // and lose every event the cursor just stepped past. Reject
1754    // requests whose `count * size_of::<NetEvent>` would overflow
1755    // `isize::MAX` (the `Layout::array` cap) up front, so the
1756    // failure happens before the cursor moves.
1757    if limit > 0
1758        && (std::mem::size_of::<NetEvent>())
1759            .checked_mul(limit)
1760            .is_none_or(|v| v > isize::MAX as usize)
1761    {
1762        return NetError::IntOverflow.into();
1763    }
1764
1765    let handle = unsafe { &*handle };
1766    let _guard = match enter_ffi_op(handle) {
1767        Ok(g) => g,
1768        Err(err) => return err,
1769    };
1770
1771    let mut request = ConsumeRequest::new(limit);
1772    if !cursor.is_null() {
1773        if let Ok(s) = unsafe { CStr::from_ptr(cursor) }.to_str() {
1774            if !s.is_empty() {
1775                request = request.from(s);
1776            }
1777        }
1778    }
1779
1780    let response = match handle.runtime.block_on(handle.bus.poll(request)) {
1781        Ok(r) => r,
1782        Err(_) => return NetError::PollFailed.into(),
1783    };
1784
1785    let count = response.events.len();
1786
1787    // Allocate events array.
1788    //
1789    // Each iteration allocates two boxed byte slices via
1790    // `Vec::to_vec().into_boxed_slice()`, which panic on OOM in
1791    // the global allocator. A panic across this `extern "C"`
1792    // body is UB — under the cgo/N-API/cffi unwind model the
1793    // panic propagates into a frame that doesn't expect it. Wrap
1794    // the per-event build in `catch_unwind`, track how many
1795    // events we've fully written, and on panic / mid-loop
1796    // failure free the partial array via `free_events_array`
1797    // so neither UB nor the partial allocations leak.
1798    let events_ptr = if count > 0 {
1799        let layout = match std::alloc::Layout::array::<NetEvent>(count) {
1800            Ok(l) => l,
1801            Err(_) => return NetError::Unknown.into(),
1802        };
1803        let ptr = unsafe { std::alloc::alloc(layout) as *mut NetEvent };
1804        if ptr.is_null() {
1805            return NetError::Unknown.into();
1806        }
1807
1808        // Shared counter so the outer scope can clean up partial
1809        // writes if any iteration panics.
1810        let completed = std::cell::Cell::new(0usize);
1811        let build_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
1812            for (i, event) in response.events.iter().enumerate() {
1813                let id_bytes = event.id.as_bytes().to_vec().into_boxed_slice();
1814                let id_len = id_bytes.len();
1815                let id_ptr = Box::into_raw(id_bytes) as *const c_char;
1816
1817                let raw_bytes = event.raw.to_vec().into_boxed_slice();
1818                let raw_len = raw_bytes.len();
1819                let raw_ptr = Box::into_raw(raw_bytes) as *const c_char;
1820
1821                unsafe {
1822                    ptr.add(i).write(NetEvent {
1823                        id: id_ptr,
1824                        id_len,
1825                        raw: raw_ptr,
1826                        raw_len,
1827                        insertion_ts: event.insertion_ts,
1828                        shard_id: event.shard_id,
1829                    });
1830                }
1831                completed.set(i + 1);
1832            }
1833        }));
1834        if build_result.is_err() {
1835            // A panic landed mid-loop. Free fully-written events
1836            // (those past `completed.get()` were never written, so
1837            // the inner `id`/`raw` pointers aren't valid). The
1838            // events array was allocated for `count` NetEvent
1839            // slots, so the dealloc must use that same layout.
1840            free_events_array_partial(ptr, completed.get(), count);
1841            return NetError::Unknown.into();
1842        }
1843        ptr
1844    } else {
1845        ptr::null_mut()
1846    };
1847
1848    // Leak next_id if present.
1849    let next_id_ptr = match response.next_id {
1850        Some(ref s) => match std::ffi::CString::new(s.as_str()) {
1851            Ok(c) => c.into_raw(),
1852            Err(_) => {
1853                // Free already-allocated events before returning
1854                // error. `s.as_str()` is valid UTF-8 by `String`
1855                // invariant, so this is the interior-NUL path —
1856                // an upstream cursor id that contains `\0` cannot
1857                // round-trip through a C string. Pre-fix this
1858                // returned `InvalidUtf8`, which mis-described
1859                // the cause; bindings now see the more accurate
1860                // `InteriorNul`.
1861                free_events_array(events_ptr, count);
1862                return NetError::InteriorNul.into();
1863            }
1864        },
1865        None => ptr::null_mut(),
1866    };
1867
1868    unsafe {
1869        (*out).events = events_ptr;
1870        (*out).count = count;
1871        (*out).next_id = next_id_ptr;
1872        (*out).has_more = if response.has_more { 1 } else { 0 };
1873    }
1874
1875    NetError::Success.into()
1876}
1877
1878/// Free an events array and all its id/raw allocations.
1879///
1880/// `count` is the number of fully-written events (those whose
1881/// inner `id` / `raw` boxed slices were initialized). It must
1882/// also match the `Layout::array::<NetEvent>` used at allocation
1883/// time — every existing caller writes exactly `count` events
1884/// before invoking this function. For partial-cleanup paths
1885/// (e.g. panic mid-build), use [`free_events_array_partial`].
1886fn free_events_array(events: *mut NetEvent, count: usize) {
1887    free_events_array_partial(events, count, count);
1888}
1889
1890/// Free an events array where only `walk_count` entries have
1891/// fully-initialized `id`/`raw` allocations, but the array
1892/// itself was allocated for `alloc_count` slots. Per-event
1893/// boxes are freed for `0..walk_count`; the array is then
1894/// deallocated with the original `Layout::array::<NetEvent>(alloc_count)`
1895/// to match the allocation. Used by `net_poll_ex`'s panic-mid-loop
1896/// recovery path.
1897fn free_events_array_partial(events: *mut NetEvent, walk_count: usize, alloc_count: usize) {
1898    if events.is_null() || alloc_count == 0 {
1899        return;
1900    }
1901    for i in 0..walk_count {
1902        let event = unsafe { &*events.add(i) };
1903        if !event.id.is_null() {
1904            unsafe {
1905                let _ = Box::from_raw(std::ptr::slice_from_raw_parts_mut(
1906                    event.id as *mut u8,
1907                    event.id_len,
1908                ));
1909            }
1910        }
1911        if !event.raw.is_null() {
1912            unsafe {
1913                let _ = Box::from_raw(std::ptr::slice_from_raw_parts_mut(
1914                    event.raw as *mut u8,
1915                    event.raw_len,
1916                ));
1917            }
1918        }
1919    }
1920    if let Ok(layout) = std::alloc::Layout::array::<NetEvent>(alloc_count) {
1921        unsafe {
1922            std::alloc::dealloc(events as *mut u8, layout);
1923        }
1924    }
1925}
1926
1927/// Free the internal allocations of a poll result returned by `net_poll_ex`.
1928///
1929/// This frees the events array (including each event's `id` and `raw` buffers)
1930/// and the `next_id` string. It does **not** free the `NetPollResult` struct
1931/// itself, which is caller-provided (typically stack-allocated or managed by
1932/// the caller).
1933#[unsafe(no_mangle)]
1934pub unsafe extern "C" fn net_free_poll_result(result: *mut NetPollResult) {
1935    if result.is_null() {
1936        return;
1937    }
1938
1939    let result = unsafe { &mut *result };
1940
1941    // Free events array and all id/raw allocations.
1942    free_events_array(result.events, result.count);
1943
1944    // Free next_id.
1945    if !result.next_id.is_null() {
1946        unsafe {
1947            drop(std::ffi::CString::from_raw(result.next_id));
1948        }
1949    }
1950
1951    // Null the fields so a second `net_free_poll_result` on the
1952    // same struct is a safe no-op rather than a double-free. The
1953    // C header's contract just says "free a poll result"; without
1954    // this clear, a defensive caller calling free twice (or two
1955    // wrappers each calling free in their destructor) would
1956    // re-`Box::from_raw` an already-freed pointer.
1957    result.events = std::ptr::null_mut();
1958    result.count = 0;
1959    result.next_id = std::ptr::null_mut();
1960    result.has_more = 0;
1961}
1962
1963/// Get stats without JSON serialization.
1964#[unsafe(no_mangle)]
1965pub unsafe extern "C" fn net_stats_ex(handle: *mut NetHandle, out: *mut NetStats) -> c_int {
1966    if !handle_is_valid(handle) || out.is_null() {
1967        return NetError::NullPointer.into();
1968    }
1969
1970    let handle = unsafe { &*handle };
1971    let _guard = match enter_ffi_op(handle) {
1972        Ok(g) => g,
1973        Err(err) => return err,
1974    };
1975    let stats = handle.bus.stats();
1976
1977    unsafe {
1978        (*out).events_ingested = stats
1979            .events_ingested
1980            .load(std::sync::atomic::Ordering::Relaxed);
1981        (*out).events_dropped = stats
1982            .events_dropped
1983            .load(std::sync::atomic::Ordering::Relaxed);
1984        (*out).batches_dispatched = stats
1985            .batches_dispatched
1986            .load(std::sync::atomic::Ordering::Relaxed);
1987    }
1988
1989    NetError::Success.into()
1990}
1991
1992#[cfg(test)]
1993mod tests {
1994    use super::*;
1995
1996    #[test]
1997    fn test_parse_config_valid() {
1998        let config = parse_config_json(r#"{"num_shards": 8}"#);
1999        assert!(config.is_some());
2000    }
2001
2002    #[test]
2003    fn test_parse_config_num_shards_overflow() {
2004        // u16::MAX is 65535, so 65536 should fail
2005        let config = parse_config_json(r#"{"num_shards": 65536}"#);
2006        assert!(
2007            config.is_none(),
2008            "num_shards exceeding u16::MAX should fail"
2009        );
2010
2011        // Much larger value should also fail
2012        let config = parse_config_json(r#"{"num_shards": 100000}"#);
2013        assert!(
2014            config.is_none(),
2015            "num_shards exceeding u16::MAX should fail"
2016        );
2017    }
2018
2019    #[test]
2020    fn test_parse_config_num_shards_max_valid() {
2021        // u16::MAX (65535) should be valid
2022        let config = parse_config_json(r#"{"num_shards": 65535}"#);
2023        assert!(config.is_some(), "num_shards at u16::MAX should be valid");
2024    }
2025
2026    #[test]
2027    fn test_parse_config_invalid_json() {
2028        let config = parse_config_json(r#"{"num_shards": invalid}"#);
2029        assert!(config.is_none());
2030    }
2031
2032    #[test]
2033    fn test_parse_config_empty() {
2034        let config = parse_config_json(r#"{}"#);
2035        assert!(config.is_some(), "empty config should use defaults");
2036    }
2037
2038    /// Pin: known `backpressure_mode` strings round-trip; an
2039    /// unknown value (typo) is rejected with `None`, not silently
2040    /// downgraded to `DropNewest`. Pre-fix a deploy-time typo
2041    /// like `"DropOldset"` swapped the operator's intended
2042    /// durability for `DropNewest` with no diagnostic.
2043    #[test]
2044    fn parse_config_rejects_unknown_backpressure_mode() {
2045        // Known values still parse.
2046        for s in [
2047            "DropNewest",
2048            "drop_newest",
2049            "DropOldest",
2050            "drop_oldest",
2051            "FailProducer",
2052            "fail_producer",
2053        ] {
2054            let cfg = parse_config_json(&format!(r#"{{"backpressure_mode": "{}"}}"#, s));
2055            assert!(cfg.is_some(), "known mode `{}` must parse", s);
2056        }
2057
2058        // Typos must fail.
2059        for s in ["DropOldset", "FailProduce", "drop_oldst", "garbage", ""] {
2060            let cfg = parse_config_json(&format!(r#"{{"backpressure_mode": "{}"}}"#, s));
2061            assert!(
2062                cfg.is_none(),
2063                "unknown mode `{}` must reject (pre-fix this silently \
2064                 fell through to DropNewest)",
2065                s,
2066            );
2067        }
2068
2069        // Wrong JSON type also fails — pre-fix this hit the
2070        // `and_then(|v| v.as_str())` short-circuit and was
2071        // ignored entirely.
2072        let cfg = parse_config_json(r#"{"backpressure_mode": 42}"#);
2073        assert!(
2074            cfg.is_none(),
2075            "non-string non-object backpressure_mode must reject"
2076        );
2077        let cfg = parse_config_json(r#"{"backpressure_mode": true}"#);
2078        assert!(cfg.is_none(), "boolean backpressure_mode must reject");
2079    }
2080
2081    /// Pin: the `Sample { rate }` mode is reachable from JSON
2082    /// via `{"backpressure_mode": {"Sample": {"rate": N}}}`,
2083    /// and a zero rate is rejected (validator already rejects
2084    /// it; the parser must too, so the surface is consistent).
2085    #[test]
2086    fn parse_config_supports_sample_mode_with_validation() {
2087        let cfg = parse_config_json(r#"{"backpressure_mode": {"Sample": {"rate": 10}}}"#);
2088        assert!(cfg.is_some(), "Sample with non-zero rate must parse");
2089
2090        let cfg = parse_config_json(r#"{"backpressure_mode": {"Sample": {"rate": 0}}}"#);
2091        assert!(cfg.is_none(), "Sample with rate=0 must reject");
2092
2093        let cfg = parse_config_json(r#"{"backpressure_mode": {"Sample": {}}}"#);
2094        assert!(cfg.is_none(), "Sample missing rate must reject");
2095    }
2096
2097    // Regression: the Go binding's `Poll(limit, cursor)` serializes a
2098    // `"cursor"` field that the FFI JSON path previously ignored —
2099    // cross-shard pagination silently broke. `parse_poll_request_json`
2100    // must round-trip the cursor into `ConsumeRequest.from_id`.
2101    #[test]
2102    fn test_parse_poll_request_preserves_cursor() {
2103        let req = parse_poll_request_json(r#"{"limit": 50, "cursor": "abc:123"}"#).unwrap();
2104        assert_eq!(req.limit, 50);
2105        assert_eq!(req.from_id.as_deref(), Some("abc:123"));
2106    }
2107
2108    #[test]
2109    fn test_parse_poll_request_no_cursor_defaults_to_none() {
2110        let req = parse_poll_request_json(r#"{"limit": 10}"#).unwrap();
2111        assert_eq!(req.limit, 10);
2112        assert_eq!(req.from_id, None);
2113    }
2114
2115    #[test]
2116    fn test_parse_poll_request_empty_uses_default_limit() {
2117        let req = parse_poll_request_json(r#"{}"#).unwrap();
2118        assert_eq!(req.limit, 100);
2119        assert_eq!(req.from_id, None);
2120    }
2121
2122    // Regression: a wrong-typed `"limit"` previously hit
2123    // `.as_u64().unwrap_or(100)` and silently defaulted. Caller bugs
2124    // (e.g. sending a string or a negative number) must surface as
2125    // `InvalidJson` instead.
2126    #[test]
2127    fn test_parse_poll_request_wrong_type_limit_errors() {
2128        let err = parse_poll_request_json(r#"{"limit": "50"}"#).unwrap_err();
2129        assert_eq!(err, c_int::from(NetError::InvalidJson));
2130        let err = parse_poll_request_json(r#"{"limit": -1}"#).unwrap_err();
2131        assert_eq!(err, c_int::from(NetError::InvalidJson));
2132    }
2133
2134    #[test]
2135    fn test_parse_poll_request_wrong_type_cursor_errors() {
2136        let err = parse_poll_request_json(r#"{"cursor": 123}"#).unwrap_err();
2137        assert_eq!(err, c_int::from(NetError::InvalidJson));
2138    }
2139
2140    #[test]
2141    fn test_parse_poll_request_null_fields_use_defaults() {
2142        let req = parse_poll_request_json(r#"{"limit": null, "cursor": null}"#).unwrap();
2143        assert_eq!(req.limit, 100);
2144        assert_eq!(req.from_id, None);
2145    }
2146
2147    /// `usize::MAX` is always a valid usize regardless of target
2148    /// pointer width, so it must parse successfully on both 32- and
2149    /// 64-bit builds. This pins the boundary case.
2150    #[test]
2151    fn test_parse_poll_request_limit_at_usize_max() {
2152        let json = format!(r#"{{"limit": {}}}"#, usize::MAX);
2153        let req = parse_poll_request_json(&json).unwrap();
2154        assert_eq!(req.limit, usize::MAX);
2155    }
2156
2157    /// Regression: `as usize` silently truncates on 32-bit targets
2158    /// for `u64` values above `usize::MAX`. The parser must return
2159    /// `InvalidJson` instead of wrapping. We only run this on 32-bit
2160    /// targets because on 64-bit `usize::MAX == u64::MAX`, leaving
2161    /// nothing that fits in u64 but not usize.
2162    #[cfg(target_pointer_width = "32")]
2163    #[test]
2164    fn test_parse_poll_request_limit_overflows_usize_on_32bit() {
2165        // 2^33 — fits in u64, but exceeds usize::MAX on a 32-bit build.
2166        let err = parse_poll_request_json(r#"{"limit": 8589934592}"#).unwrap_err();
2167        assert_eq!(err, c_int::from(NetError::InvalidJson));
2168    }
2169
2170    /// CR-22: pin parity between the Rust-side `NetError` enum and
2171    /// the two C-header copies. The Rust enum is the source of
2172    /// truth; C / Go consumers `errors.Is` against the named codes.
2173    /// Pre-CR-22 the headers were missing `-9` (IntOverflow) and
2174    /// `-10` (MismatchedHandles); a consumer receiving those values
2175    /// would fall into the unknown-code branch and lose actionable
2176    /// distinction.
2177    ///
2178    /// We extract every integer literal that appears as the
2179    /// right-hand side of an `= ` token in the file and check
2180    /// that each Rust-side value is present in BOTH headers. The
2181    /// test does NOT verify symbolic names; the sealing
2182    /// constraint is the numeric value alone.
2183    ///
2184    /// Both `include_str!` paths point inside `net/crates/net/`.
2185    /// `include/net.go.h` is a manually-synced mirror of the
2186    /// repo-root `go/net.h`. Reaching outside the crate root
2187    /// (`include_str!("../../../../../go/net.h")`) breaks
2188    /// `cargo publish` and any out-of-repo vendoring of this
2189    /// crate, so the in-crate copy is the supported source. A
2190    /// drift between the two surfaces here as a parity-test
2191    /// failure: one of them will be missing the new value.
2192    #[test]
2193    fn cr22_c_header_parity_with_rust_neterror() {
2194        let primary = include_str!("../../include/net.h");
2195        let go_copy = include_str!("../../include/net.go.h");
2196
2197        // The Rust enum's full set of values (mirrors `pub enum
2198        // NetError` above). When a new variant is added in the
2199        // Rust source, this list — AND both headers — must be
2200        // updated together. The asserts that follow then catch a
2201        // missing header update at the next CI run.
2202        let rust_values: &[i32] = &[0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -99];
2203
2204        // Pull every numeric literal that looks like an enum-value
2205        // assignment (`= <number>` followed by `,` or whitespace).
2206        // Whitespace-tolerant: skips `= 0`, `=  0`, `= -10`, etc.
2207        fn extract_assigned_values(src: &str) -> Vec<i32> {
2208            let mut out = Vec::new();
2209            let mut chars = src.chars().peekable();
2210            while let Some(c) = chars.next() {
2211                if c != '=' {
2212                    continue;
2213                }
2214                // Skip whitespace.
2215                while let Some(&peek) = chars.peek() {
2216                    if peek == ' ' || peek == '\t' {
2217                        chars.next();
2218                    } else {
2219                        break;
2220                    }
2221                }
2222                // Optional sign.
2223                let mut buf = String::new();
2224                if let Some(&peek) = chars.peek() {
2225                    if peek == '-' || peek == '+' {
2226                        buf.push(peek);
2227                        chars.next();
2228                    }
2229                }
2230                // Digits.
2231                let mut had_digit = false;
2232                while let Some(&peek) = chars.peek() {
2233                    if peek.is_ascii_digit() {
2234                        buf.push(peek);
2235                        chars.next();
2236                        had_digit = true;
2237                    } else {
2238                        break;
2239                    }
2240                }
2241                if had_digit {
2242                    if let Ok(v) = buf.parse::<i32>() {
2243                        out.push(v);
2244                    }
2245                }
2246            }
2247            out
2248        }
2249
2250        let primary_vals = extract_assigned_values(primary);
2251        let go_vals = extract_assigned_values(go_copy);
2252
2253        for &v in rust_values {
2254            assert!(
2255                primary_vals.contains(&v),
2256                "CR-22 regression: include/net.h is missing the value {} \
2257                 (Rust NetError defines it). Add the matching `NET_ERR_*` \
2258                 enumerator before merging.",
2259                v
2260            );
2261            assert!(
2262                go_vals.contains(&v),
2263                "CR-22 regression: bindings/go/net/net.h is missing the value {} \
2264                 (Rust NetError defines it).",
2265                v
2266            );
2267        }
2268    }
2269
2270    /// CR-5: pin that `examples/capability.c` does not double-include
2271    /// `net.h` and `net.go.h`. Both files use the `NET_SDK_H` include
2272    /// guard, so when both are included in one TU the second is
2273    /// silently skipped — every `net_validate_capabilities` /
2274    /// `net_predicate_*` call the example makes becomes an
2275    /// implicit-declaration error on GCC 14+/Clang 16+, and a silent
2276    /// `int`-return miscompile on older toolchains. The deeper fix
2277    /// (renaming one guard so they compose cleanly) is tracked as
2278    /// CR-28; this test catches the example-level regression.
2279    #[test]
2280    fn cr5_example_does_not_double_include_net_headers() {
2281        let example = include_str!("../../examples/capability.c");
2282        let net_h_included = example.contains("#include \"../include/net.h\"");
2283        let net_go_h_included = example.contains("#include \"../include/net.go.h\"");
2284        assert!(
2285            net_go_h_included,
2286            "examples/capability.c must include net.go.h to declare \
2287             net_validate_capabilities + net_predicate_* symbols"
2288        );
2289        assert!(
2290            !net_h_included,
2291            "examples/capability.c must NOT also include net.h: \
2292             both headers share the NET_SDK_H guard, so the second \
2293             include is silently skipped, leaving the example's \
2294             net_predicate_* calls implicitly declared. Drop the \
2295             redundant include — net.go.h is a superset."
2296        );
2297    }
2298
2299    /// `handle_is_valid` rejects null and any pointer not aligned for
2300    /// `NetHandle`. A foreign caller producing a misaligned pointer
2301    /// (e.g. via an over-eager `void *` cast on a packed struct) hits
2302    /// `&*handle` UB before any other check fires; this gate is the
2303    /// pre-deref discriminator.
2304    #[test]
2305    fn handle_is_valid_rejects_null_and_misaligned() {
2306        // Null is rejected.
2307        assert!(
2308            !handle_is_valid(std::ptr::null::<NetHandle>()),
2309            "null pointer must not be considered a valid handle"
2310        );
2311
2312        // Aligned but non-null is accepted (we use a small backing
2313        // buffer to materialize a pointer without dereferencing it).
2314        // `align_of::<NetHandle>()` is the alignment we must match.
2315        let align = std::mem::align_of::<NetHandle>();
2316        let buf = vec![0u8; align * 2];
2317        let base = buf.as_ptr() as usize;
2318        let aligned = (base + align - 1) & !(align - 1);
2319        let aligned_ptr = aligned as *const NetHandle;
2320        assert!(
2321            handle_is_valid(aligned_ptr),
2322            "aligned non-null pointer must validate (align={align}, ptr={aligned_ptr:p})"
2323        );
2324
2325        // A pointer one byte past `aligned_ptr` is misaligned for any
2326        // type with align > 1, and `NetHandle` (containing `AtomicU32`,
2327        // `AtomicBool`, ManuallyDrop'd EventBus + Runtime) easily
2328        // exceeds 1.
2329        if align > 1 {
2330            let misaligned_ptr = (aligned + 1) as *const NetHandle;
2331            assert!(
2332                !handle_is_valid(misaligned_ptr),
2333                "misaligned pointer must be rejected (align={align}, ptr={misaligned_ptr:p})"
2334            );
2335        }
2336    }
2337
2338    /// Pin: zero values for `heartbeat_interval_ms` and
2339    /// `session_timeout_ms` must reject the entire config (parser
2340    /// returns `None`). Pre-fix the parser threaded `0` through
2341    /// to `Duration::from_millis(0)`, which on the Net adapter's
2342    /// heartbeat path results in a busy-loop that pegs a CPU and
2343    /// produces no diagnostic — the FFI caller saw a successful
2344    /// `net_init` followed by a hung daemon. The validator-level
2345    /// guard for cooldown / metrics_window has no equivalent on
2346    /// the Net-adapter side, so the parser is the only place that
2347    /// can refuse the build.
2348    #[cfg(feature = "net")]
2349    #[test]
2350    fn parse_config_rejects_zero_heartbeat_and_session_timeout() {
2351        // 32-byte hex strings (64 chars) so `hex::decode` produces
2352        // exactly the [u8; 32] the parser requires for `psk` and
2353        // `peer_public_key`.
2354        let psk = "0".repeat(64);
2355        let peer_pk = "1".repeat(64);
2356
2357        // Sanity: a config with both fields *non-zero* must parse
2358        // successfully — proves the rejection in the negative
2359        // cases below is caused by the zero, not a missing
2360        // required field on the surrounding `net` block.
2361        let baseline = format!(
2362            r#"{{"net":{{"bind_addr":"127.0.0.1:9000","peer_addr":"127.0.0.1:9001",
2363                "psk":"{psk}","peer_public_key":"{peer_pk}",
2364                "heartbeat_interval_ms":1000,"session_timeout_ms":30000}}}}"#
2365        );
2366        assert!(
2367            parse_config_json(&baseline).is_some(),
2368            "baseline net config with non-zero heartbeat/session_timeout must parse"
2369        );
2370
2371        // heartbeat_interval_ms = 0 → reject.
2372        let zero_hb = format!(
2373            r#"{{"net":{{"bind_addr":"127.0.0.1:9000","peer_addr":"127.0.0.1:9001",
2374                "psk":"{psk}","peer_public_key":"{peer_pk}",
2375                "heartbeat_interval_ms":0,"session_timeout_ms":30000}}}}"#
2376        );
2377        assert!(
2378            parse_config_json(&zero_hb).is_none(),
2379            "heartbeat_interval_ms=0 must reject (pre-fix this produced a CPU-pegging busy loop)"
2380        );
2381
2382        // session_timeout_ms = 0 → reject.
2383        let zero_to = format!(
2384            r#"{{"net":{{"bind_addr":"127.0.0.1:9000","peer_addr":"127.0.0.1:9001",
2385                "psk":"{psk}","peer_public_key":"{peer_pk}",
2386                "heartbeat_interval_ms":1000,"session_timeout_ms":0}}}}"#
2387        );
2388        assert!(
2389            parse_config_json(&zero_to).is_none(),
2390            "session_timeout_ms=0 must reject"
2391        );
2392    }
2393}