zc2 0.0.25

P2P compute broker with credit-based billing, WAL, and broker mesh support
//! Wire format re-exports for the broker.
//!
//! Single source of truth lives in the [`zakuro_wire`] crate, defined in
//! the zakuro repo at <https://github.com/zakuro-ai/zakuro/tree/master/crates/zakuro-wire>.
//! The broker reuses those types verbatim — never duplicates them — so
//! any schema change forces a coordinated `zakuro-wire` version bump
//! that both repos pick up via the resolver.
//!
//! See zakuro RFC 0001 — Wire format: replace cloudpickle with postcard —
//! for the architectural decision.

#[cfg(test)]
mod tests {
    //! Cross-repo contract checks.
    //!
    //! The frozen-bytes snapshot test in
    //! `crates/zakuro-wire/tests/snapshots.rs` (zakuro repo) guards the
    //! schema's wire shape. These tests here in zc are sanity checks
    //! that the broker is in fact pulling in the same crate version and
    //! seeing the same byte representation.

    use zakuro_wire::{Envelope, ResourceLimits, WireVersion};

    /// A roundtrip that has to produce identical bytes to the canonical
    /// snapshot in the zakuro-wire crate's `tests/snapshots.rs`. If
    /// this drifts the broker is on a different `zakuro-wire` than the
    /// worker — that's the bug this test exists to catch.
    #[test]
    fn envelope_roundtrip_matches_zakuro_wire_canonical() {
        let env = Envelope {
            version: WireVersion::V1,
            job_id: "canonical-job".into(),
            tenant_id: "canonical-tenant".into(),
            callable: vec![0xde, 0xad, 0xbe, 0xef],
            args: vec![0x00, 0x01, 0x02],
            hmac: [0xAA; 32],
            resource_limits: ResourceLimits {
                cpus: 1.0,
                memory_mb: 1024,
                gpus: 0,
                timeout_seconds: 30,
            },
        };
        let bytes = postcard::to_allocvec(&env).expect("encode");
        let back: Envelope = postcard::from_bytes(&bytes).expect("decode");
        assert_eq!(env, back);

        // Frozen hex from zakuro-wire/tests/snapshots.rs::FROZEN_ENVELOPE_HEX.
        // If this assertion fires, the broker is pinned to a
        // `zakuro-wire` whose schema doesn't match the worker's.
        const FROZEN_HEX: &str = concat!(
            "00",
            "0d63616e6f6e6963616c2d6a6f62",
            "1063616e6f6e6963616c2d74656e616e74",
            "04deadbeef",
            "03000102",
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
            "0000803f",
            "8008",
            "00",
            "1e",
        );
        let actual_hex: String = bytes.iter().map(|b| format!("{b:02x}")).collect();
        assert_eq!(
            actual_hex, FROZEN_HEX,
            "broker's zakuro-wire envelope shape drifted from the worker's. \
             Bump `zakuro-wire` on both sides in lock-step."
        );
    }
}