logs.gokuls.in

5 pull requests merged across 1 repo

Offline-Protocol/offline-protocol-sdk

Closes out the non-blocking items left from the reviews of #570, #571 and #572.

What changes

1. A refused C registration now always calls the host's drop(ctx). The guide promised exactly one drop call, including on refusal. A null handle in any of the three setters, and an incomplete storage provider, were refused before the library wrapped ctx, so drop never ran. The setters and the storage adapter now wrap ctx first. offline_protocol_initialize_mls builds both providers before checking either, so refusing one still drops the other.

2. The quick start no longer leaks an error. Its ignored local_address failure did not clear err. The library writes over err without freeing it, so the next failure leaked the previous strings.

3. MlsStorageError is documented as append-only C ABI. OfflineProtocolStorageStatus is each variant's position plus one, so a compiled C provider answers by number. K6, C2, the header comment and the guard's failure message now say to append and never renumber.

4. The generator refuses a namespace function named like an engine method. offline_protocol_call tries the namespace first, so a shared name would silently run the wrong function.

5. A guard pins the Android opt-out. --no-default-features turns off every default, not only capi. The new guard reads the crate's default features and the three commands that build without the C binding. It fails if a command does not add back every default except capi, or if a command stops opting out.

Verification

  • cargo test -p offline-protocol-uniffi --lib: 260 passed, 4 new tests.
  • The three new drop tests fail against the old code. The feature guard fails with a second default feature, and again with the opt-out removed from the React Native Android script.
  • cargo clippy --workspace -- -D warnings, and the crate again with --no-default-features.
  • scripts/generate-c-header.sh --check after regenerating the header, and the dispatcher generator's --check. The generated dispatcher is unchanged.
  • make -C bindings/c/consumer-check: both link modes pass on macOS.
  • The quick start compiles under -std=c99 -Wall -Wextra -Wpedantic -Werror.

Not in this PR

  • The release workflow_dispatch dry run. The Windows .dll.lib and macOS install-name steps still have not run in CI.
  • A Windows leg in the C Binding CI job.

Third of three stacked PRs; based on #571, which is based on #570. This PR adds the guide for the C API, its rows across the docs, and the release packaging.

  • bindings/c/README.md says where the header and library come from, then gives a quick start, method naming and encoding, events, and the route for other languages. The quick start was compiled with -std=c99 -Wpedantic -Werror, linked and run end to end against the file stores before this commit: it derives an address and queues a message.
  • Rows wherever the other bindings have one: the docs index, the bridge contract's document table and its "what each binding owes" table, and the README's install table and features line. They also go in the local API chapter, whose method table the C API shares, and in CLAUDE.md (the knowledge table, the regeneration commands, the rules list and the unsafe rule). The CHANGELOG gets an Unreleased entry.

Release packaging

  • The desktop archives now carry offline_protocol.h beside UniFFI's internal offline_protocolFFI.h. Their README no longer calls that internal header one "for consumers linking directly", which it never was. It gains a C section with the link line for each platform.
  • The Windows archive carries offline_protocol_uniffi.dll.lib. Without it an MSVC linker cannot link the DLL. The Windows build job collects it, and the release verifies it before anything is published.
  • scripts/tests/test-package-release-assets.sh asserts both. It gains two missing-input negative controls, missing-c-header and missing-windows-import-library, each of which fails with no assets written.

The static archive is deliberately not shipped. Built for release it is 131.6 MiB, against an 11 MiB shared library on macOS arm64. The plan had it in the archives; that size is why it is not. CI still builds and links it, and the guide says how to build it.

Type of change

  • docs: documentation
  • chore: release packaging

Checklist

  • cargo fmt --all -- --check passes
  • cargo clippy --workspace -- -D warnings passes
  • cargo test --workspace passes (32 suites)
  • cargo-deny is satisfied
  • Commits follow Conventional Commits
  • Docs / CHANGELOG.md updated
  • No em dashes in new prose; every relative link resolves
  • test-package-release-assets.sh, check-license-consistency.sh, check-crate-readmes.sh and shellcheck pass; actionlint clean

Breaking changes

None. The desktop archives gain files and lose none.

Notes for reviewers

  • Not exercised: the release workflow change runs only on a tag or a dispatch. A workflow_dispatch dry run from this branch would exercise the Windows collection step before the next release.

Second of three stacked PRs; based on #570. This adds the C API itself: twelve extern "C" functions, a committed header, the guards that keep both honest, and a C program that CI compiles and runs.

FunctionCovers
offline_protocol_create / _freeThe engine, from a ProtocolConfig as JSON
offline_protocol_callEvery declaration the dispatcher reaches, by its local API name
offline_protocol_call_staticderive_address, parse_invite, verify_identity_assertion, before any engine exists
offline_protocol_set_event_callback / _poll_eventEvents as JSON, pushed or pulled
offline_protocol_set_transport_callbackThe four outbound notifications
offline_protocol_initialize_mlsTwo storage providers written in C
offline_protocol_run_storage_conformanceThe conformance suite against a C provider (C11)
offline_protocol_string_free / _error_clear / _versionOwnership and version

The header bindings/c/include/offline_protocol.h is generated by cbindgen 0.29.4, pinned in scripts/generate-c-header.sh. The contract is docs/bridges/c.md.

Decisions that are easy to undo by accident

  • Every entry point catches a panic and answers Internal. A panic that reaches an extern "C" boundary aborts the host process.
  • Every callback carries a drop. The library calls it once when it lets go of the callback. That covers a refused registration, and a replaced event callback that is still running on another thread.
  • A storage answer outside the six named values is read as the operation's own failure, never as CorruptedData. The engine answers corruption by minting a new record key, which strands every sealed record.
  • The transport kind is an int32_t, not the enum type. A value outside an enum's range is undefined behaviour in Rust.
  • The storage struct's delete member is remove. delete is a C++ keyword, and the header compiles as C++.
  • The header check never uses cbindgen --verify. That flag reports a stale header and also rewrites it, which I found when it rewrote the committed file during a negative control. The check generates a scratch copy and compares.

Guards

Each was turned red on purpose before pushing:

  • every_c_entry_point_is_declared_in_the_committed_header: the #[no_mangle] set equals the header's declarations.
  • unsafe_is_confined_to_the_c_boundary: ffi.rs, adapters.rs and their tests are the only files in the crate that write unsafe. The red run named host_log.rs.
  • the_hand_written_declarations_are_the_ones_the_bridge_doc_lists: the generator's skip list equals the table in docs/bridges/c.md.
  • the_storage_status_values_are_the_definitions_positions_plus_one.
  • every_bridge_reads_the_telemetry_config_section now also pins that the C binding fills os.

The C side

bindings/c/consumer-check is a C99 program built with -Wall -Wextra -Wpedantic -Werror, with its own mutex-guarded storage provider. It is linked against the shared library and then against the static one, run both times, and the header is compiled as C++11. It checks:

  • the version, and an instance-less call
  • events on the calling thread, matching the polled copy
  • every status, including EncryptFailed at position 6 for a send before storage, which agrees with the local API's -32006
  • the conformance suite green against the C provider
  • initialize_mls over two C providers yielding an off1 address
  • the built-in file stores through offline_protocol_call

It caught one real ordering rule while being written: initialize_mls belongs before start.

The new c-binding CI job runs the header check, the dispatcher generator's --check, the library build and the consumer check, on Ubuntu and macOS.

Type of change

  • feat: new feature

Checklist

  • cargo fmt --all -- --check passes
  • cargo clippy --workspace -- -D warnings passes. Under --all-targets the crate's count is unchanged, 454 pre-existing in lib.rs tests and none in the new files.
  • cargo test --workspace passes (32 suites on the tip of the stack; 246 in this crate)
  • cargo-deny is satisfied
  • Commits follow Conventional Commits
  • Docs: docs/bridges/c.md here; the guide and index rows in the next PR
  • New unsafe only at the FFI boundary, each block with a SAFETY comment, confined by a guard
  • The UDL did not change
  • actionlint clean on both workflows

Breaking changes

None. These are additive symbols in the existing cdylib and staticlib; nothing existing changes signature.

Notes for reviewers

  • Not verified locally: Linux. The consumer check ran on macOS arm64 only. The CI job covers Linux once this stack reaches main. Windows is linked by no CI job; the next PR ships its import library.
  • Not a security boundary: the header documents threading and re-entrancy (K2 in the contract). A callback must not call back into the engine, and offline_protocol_poll_event is the supported alternative.

First of three stacked PRs that add a C API to the SDK. This one is the safe Rust layer the C functions will sit on. It has no extern "C" yet.

The design is the local API called in process. A method is named as the local API's method table names it: bare for the engine and the namespace, services. and data. for the other two objects. Its parameters are a JSON object keyed by the definition's parameter names, and its result is JSON. The encoding is the local API's:

  • Integers are held to their declared width.
  • Bytes are padded standard base64.
  • Enums use the definition's spelling.
  • Omitted dictionary fields take the definition's defaults.
  • Unknown keys are refused.

Engine refusals keep the definition's variant name and its zero-based position, the same n as the local API's -32000 - n.

Why JSON rather than one C function per declaration. Events already cross every boundary as JSON (C3), so a C host parses JSON whatever the call surface is. A typed C API would be about 220 functions and 46 structures with nested ownership and nothing to catch drift. And a fixed set of C functions survives every change to the definition, which is what a Go, C# or C++ wrapper needs.

What is in it

  • capi/generated.rs. One dispatch arm per declaration (211) and the JSON codec of every enum and dictionary. It is generated from offline_protocol.udl, checked in, and verified by the_generated_c_dispatch_is_current. Every arm compiles against the real wrapper signatures, so the compiler checks that the definition and the wrapper agree.
  • tools/capi-dispatch-gen. Writes that file. It is its own workspace, because a declaration renamed in the definition leaves the checked-in file calling the old name, and the FFI crate then does not compile until it is regenerated. That case was exercised: rename, crate red, tool regenerates, crate green.
  • capi/codec.rs. A Codec trait chosen by the wrapper's Rust type. It is not serde on the wrapper types, which have no Deserialize, and whose Serialize spells TransportType as wifiDirect where the definition says WiFiDirect.
  • The generator refuses a UDL type form or default it has no rule for, rather than guessing.
  • enable_telemetry fills config.os from the build target and refuses one from the caller (C12). The host supplies os_major.
  • Eleven declarations take a callback or an object, or construct one. They are listed in HAND_WRITTEN and answered with a pointer to their own entry point, which arrives in the next PR.

Related issues

None filed. The design plan is summarised above.

Type of change

  • feat: new feature

Checklist

  • cargo fmt --all -- --check passes
  • cargo clippy --workspace -- -D warnings passes
  • cargo test --workspace passes (run on the tip of the stack: 32 suites)
  • cargo-deny is satisfied (one new edge to the base64 0.22.1 already in the graph)
  • Commits follow Conventional Commits
  • Docs / CHANGELOG.md: in the third PR of the stack
  • No new unsafe (none in this PR)
  • The UDL did not change

Breaking changes

None. The interface definition, the generated bindings and the wire are unchanged. The only change outside the new module is one line in lib.rs and one dependency edge.

Notes for reviewers

  • The stack: this PR, then the extern "C" layer with header, guards and C consumer check, then docs and release packaging. CI runs only on PRs targeting main, so the two children get CI once this merges and they are retargeted.
  • Negative controls run before pushing: renaming one UDL parameter turns the staleness test red.
  • Worth scrutiny: codec.rs's Fields::take_or. Defaults come from the generator's reading of the UDL literal (null, true, false, 256 are the only forms in use), never from the binding.

Refs #514. Does not close it: the two-phone soak from the issue has to pass first.

Why

PR #516 numbered native events so a loss could be attributed, but found no cause and recovers nothing. Both gap shapes in #514 share one defect: the engine acts irreversibly before it emits the only event that reports the action.

  • Receiver: the id is dedup-marked and the ACK sent, then message_received is emitted. A lost emit means the sender has a receipt and the app has nothing. A retry is re-ACKed as a duplicate.
  • Sender: the pending ACK, retry and outbox entry are removed, then message_delivered is emitted. "No retry" in the issue proves the core consumed the ACK.

The hop that lost those three events is unproven. So this PR makes a loss after numbering recoverable, and closes the core-side paths that dropped one before numbering without a trace.

What changes

  • Replay instead of report. Kotlin and Swift each keep the last 256 events handed to JavaScript (EventReplayRing, no React imports), recorded in the same locked block that spends the seq. A new replayEvents(fromSeq, toSeq) bridge method serves them. On a skipped number, index.ts pauses dispatch, pulls the range, and dispatches it in place. Recovery is reported as native_event_replayed (info). native_event_gap now reports only what the ring no longer held. The pause is bounded at 2 s and 1024 deferred events. A module without replayEvents keeps the old behaviour. destroy() drops a paused replay, as it drops every held event of a torn-down account.
  • Held settlements. message_delivered and message_failed join message_received, file_received and message_decryption_failed in the per-message hold, on all three layers.
  • Poisoned lock. lock_shared_state recovers and clears a poisoned mutex. A refusal used to skip the emit for an already ACKed or settled message, and every later one.
  • Callback failures are visible. A Kotlin exception in onEvent is caught and logged by event type. It used to become a Rust panic recorded only under a target the host logger filters out. The FFI closure isolates a throwing callback so the poll queue still gets the event. Handler panics are counted and logged by telemetry_name(), no longer by ?event, which printed identifiers.

No UDL or wire change, so no binding regeneration.

Verification

GateResult
js-ci-harness/event-seq-replay.test.js (new, in test:js)13/13; ignoring served events fails 5
npm run test:js, tsc --noEmitgreen
Android JVM suite, clean copy695/695, EventReplayRingTest 7/7
iOS SwiftPM harness466/466, EventReplayRingTests 4/4
OfflineProtocolModule.swift typecheck with React headersclean; planted error reported
react_native_events_are_numbered_for_gap_detection, react_native_buffered_inbound_event_set_matches_nativepass; removing the Kotlin record call fails the first
New core testspass; restoring the refusing lock fails the poison test
cargo fmt --check, clippy -D warnings, cargo test --workspace --lib, cargo doc -D warningsclean

Behaviour apps see

A held message_delivered or message_failed can arrive after the app returns to the foreground. UPGRADING ยง28 says to read latency_ms and dedupe on message_id.

Not done

  • No device run. Rerun the issue's soak on the Infinix and Seeker with every event logged by seq (the demo app now logs both gap diagnostics). Acceptance: sent = received = delivered, and no unrecovered gap.
  • Swift Package and Android Library assembly jobs were not run locally.