Metadata-Version: 2.4
Name: predsync
Version: 0.1.0rc4
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Topic :: Scientific/Engineering
Summary: Python bindings for predsync, a deterministic low-bandwidth state sync protocol
Home-Page: https://predsync.tgodfrey.com/book/index.html
License: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Documentation, https://predsync.tgodfrey.com/api/python/index.html
Project-URL: Homepage, https://predsync.tgodfrey.com/book/index.html
Project-URL: Source, https://github.com/tgodfrey0/predsync

# predsync

[![CI](https://github.com/tgodfrey0/predsync/actions/workflows/ci.yml/badge.svg)](https://github.com/tgodfrey0/predsync/actions/workflows/ci.yml)
[![Crates.io](https://img.shields.io/crates/v/predsync)](https://crates.io/crates/predsync)
[![PyPI](https://img.shields.io/pypi/v/predsync)](https://pypi.org/project/predsync/)

`predsync` is a deterministic, low-bandwidth protocol for replicating predictor
state across uncontrolled, lossy, one-way links. It is a codec-shaped library:
the host owns the transport, and predsync owns the bytes and the receiver's
trust in each field.

The sender omits a field while its predictor stays within that field's declared
tolerance. Includes and refreshes carry true values; periodic resets let a
receiver recover without a back channel.

## Features

- Byte-exact wire format with LSB-first bit packing and CRC-16/IBM-SDLC.
- Schema-driven field types, predictors, tolerances and recovery schedules.
- One encoder per sender and one decoder per incoming stream; no global state.
- Alloc and caller-provided fixed-storage APIs in Rust, including `no_std`.
- C ABI, Python bindings and generated typed wrappers for all three languages.
- Golden vectors, differential traces, fuzz targets and Cortex-M4 determinism checks.

## Documentation

Published documentation starts at <https://predsync.tgodfrey.com/>.

- [Library guide and protocol reference](https://predsync.tgodfrey.com/book/index.html)
- [Rust API](https://predsync.tgodfrey.com/api/rust/index.html)
- [Python API](https://predsync.tgodfrey.com/api/python/index.html)
- [C ABI](https://predsync.tgodfrey.com/api/c/html/index.html)
- [Guarantees and measured costs](https://predsync.tgodfrey.com/book/guarantees.html)
- [Testing](https://predsync.tgodfrey.com/book/testing.html)

Generated documentation can be built locally with `pixi run docs` and written
to `docs/site/`.

## Rust quickstart

Add the facade crate with `predsync = "=0.1.0"` (or
`predsync = { version = "=0.1.0", default-features = false, features = ["alloc"] }`
on embedded hosts). It re-exports schema, core and compiler APIs and installs
the `predsync` CLI.

```rust
use predsync::{parse_schema, Decoder, DecoderConfig, Encoder, EncoderConfig, Slot, Value};

const SCHEMA: &str = r#"
name: telemetry
sync: { refresh: 8, reset: 32 }
fields:
  - name: x
    type: f64
    quantise: 0.01
    range: [-100.0, 100.0]
    predictor: { type: linear, window: 4 }
    tolerance: { abs: 0.01 }
"#;

fn main() {
    let set = parse_schema(SCHEMA).unwrap().set;
    let mut encoder = Encoder::new(&set, 0, EncoderConfig::default()).unwrap();
    let mut decoder = Decoder::new(&set, DecoderConfig { resync_n: 1 }).unwrap();

    for tick in 0..3u64 {
        let values = [Value::Scalar(Slot::from_f64(1.25))];
        let plan = encoder.plan(&values, tick * 1_000_000).unwrap();
        let mut frame = vec![0u8; plan.len];
        encoder.commit(plan, &mut frame).unwrap();
        let decoded = decoder.feed(&frame).unwrap();
        assert_eq!(decoded.fields[0].value, values[0]);
    }
}
```

## Python quickstart

The wheel does not bundle schema YAML. Load the schema from a file path in your
project; this example uses the checked-in demo schema from a source checkout:

```python
from pathlib import Path

import predsync
from predsync.generated import Telemetry, TelemetryMode

schema_yaml = Path("examples/generated_example/schema/telemetry.yaml").read_text()
sample = Telemetry(
    x=1.25,
    mode=TelemetryMode.Active,
    label="nominal",
    samples=[7, 8, 9],
)

encoder = predsync.Encoder(schema_yaml, message=Telemetry.message_index, tick_ns=1_000_000)
decoder = predsync.Decoder(schema_yaml, message=Telemetry.message_index, resync_n=1)
for tick in range(3):
    frame, _ = encoder.encode(sample.to_values(), tick * 1_000_000)
    assert Telemetry.from_values(decoder.feed(frame).values()) == sample
```

For a runnable host with a lossy-link self-test, real UDP loopback and Ctrl-C
statistics on predicted fields and byte savings, see
[`examples/python`](examples/python/README.md).

## C and command line

The C ABI is documented in [the C API guide](docs/book/src/api/c.md), with a
complete program in [`examples/c/quickstart.c`](examples/c/quickstart.c). To
validate schemas or generate a Rust wrapper module without a `build.rs`:

```sh
pixi run schema-check --set examples/schema_sets/alpha --set examples/schema_sets/beta
pixi run schema-build --set examples/generated_example/schema/telemetry.yaml --out src/telemetry.rs
```

The equivalent direct commands use
`cargo run -p predsync --bin predsync -- schema_check` and
`... schema_build`. Install the tool with `cargo install predsync --locked`;
see [the CLI guide](docs/book/src/quickstart/cli.md).

## Examples

| Example | Location |
|---|---|
| Generated typed Rust wrappers | [`examples/generated_example`](examples/generated_example/README.md) (see also the [book chapter](docs/book/src/examples/generated.md)) |
| UDP datagrams | [`examples/udp`](examples/udp/README.md) |
| Python UDP host | [`examples/python`](examples/python/README.md) |
| Serial byte stream with COBS | [`examples/serial_cobs`](examples/serial_cobs/README.md) |
| ROS 2 `r2r` nodes | [`examples/ros2`](examples/ros2/README.md) |
| C ABI quickstart | [`examples/c/quickstart.c`](examples/c/quickstart.c) |

The long-running senders (UDP, Python UDP, serial/COBS and the ROS 2 talker)
print Ctrl-C summaries with predicted fields, byte savings, percentage size
reduction and message sizes.

## Development

The development environment is defined by `pixi.toml` and `pixi.lock`.

```sh
pixi run test       # Rust workspace tests
pixi run check      # all local checks, including docs and examples
pixi run ci         # check plus the 60-second fuzz stage
pixi run docs       # mdBook, rustdoc, pdoc, Doxygen and link checks
pixi run examples   # example builds and deterministic self-tests
```

See [Development and release](docs/book/src/development.md) for the full task
list and publishing dry runs.

## Licence

MIT. See [LICENSE](LICENSE).

