weirdnet-agent 0.5.7

Weirdnet agent runtime: a model-agnostic agent loop with streaming events and cancellation, tool sources and registry, tiered approval policy with per-site grants and standing allows, research budgets, traces with citation checks, and remote inference (infer/1 and infer/2, catalog, pool with failover and resume).
Documentation
//! Per-model media and request limits: what a client needs to size images, audio and whole
//! requests for one model, such as fitting a video clip's frames and soundtrack to it.
//!
//! Every field is absent when the node doesn't know it, never defaulted: a node fills in what it
//! measured when the model loaded, or what its operator set for that hardware, and leaves the
//! rest out. A client that needs a missing limit should say so rather than guess. All fields are
//! left off the wire when absent, so peers before 0.5.7 read and send entries unchanged.
//!
//! Catalog entries carry them as [`CatalogModel::limits`](crate::remote::catalog::CatalogModel::limits);
//! the context window stays in [`CatalogModel::context`](crate::remote::catalog::CatalogModel::context).

use serde::{Deserialize, Serialize};

/// Media and request limits for one model. See the [module docs](self).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct MediaLimits {
    /// Most images (`image_url` parts) one request may carry.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_images: Option<u32>,
    /// Tokens one image costs. With [`image_patch`](Self::image_patch) set, the most one image
    /// costs (at [`image_edge`](Self::image_edge)); otherwise every image costs this much.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub image_tokens: Option<u32>,
    /// For models whose image cost grows with size: the edge in pixels of the square one image
    /// token covers (an image costs `ceil(w / patch) * ceil(h / patch)` tokens after scaling to
    /// [`image_edge`](Self::image_edge)). Absent: a fixed cost per image.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub image_patch: Option<u32>,
    /// The longest image edge in pixels the model uses; larger images are scaled down to it, so
    /// sending more only costs bytes.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub image_edge: Option<u32>,
    /// Longest audio input (`input_audio` part) in seconds.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub audio_max_seconds: Option<u32>,
    /// Tokens one minute of audio costs (per minute so fractional per-second rates such as 6.25
    /// stay exact; see [`audio_tokens_per_second`](Self::audio_tokens_per_second)).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub audio_tokens_per_minute: Option<u32>,
    /// Largest request body in bytes the serving side accepts (an OpenAI bridge's
    /// `max_request_bytes`, for example), so a client sizes frames to fit the bytes as well as
    /// the tokens.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_request_bytes: Option<u64>,
}

impl MediaLimits {
    /// No limits known.
    pub const UNKNOWN: Self = Self {
        max_images: None,
        image_tokens: None,
        image_patch: None,
        image_edge: None,
        audio_max_seconds: None,
        audio_tokens_per_minute: None,
        max_request_bytes: None,
    };

    pub fn new() -> Self {
        Self::UNKNOWN
    }

    /// Whether no limit is known (what is left off the wire).
    pub fn is_unknown(&self) -> bool {
        *self == Self::UNKNOWN
    }

    pub fn with_max_images(mut self, n: u32) -> Self {
        self.max_images = Some(n);
        self
    }

    /// Every image costs `tokens`.
    pub fn with_image_tokens(mut self, tokens: u32) -> Self {
        self.image_tokens = Some(tokens);
        self
    }

    /// The image cost grows with size: one token per `patch` x `patch` pixels.
    pub fn with_image_patch(mut self, patch: u32) -> Self {
        self.image_patch = Some(patch);
        self
    }

    pub fn with_image_edge(mut self, pixels: u32) -> Self {
        self.image_edge = Some(pixels);
        self
    }

    pub fn with_audio_max_seconds(mut self, seconds: u32) -> Self {
        self.audio_max_seconds = Some(seconds);
        self
    }

    pub fn with_audio_tokens_per_minute(mut self, tokens: u32) -> Self {
        self.audio_tokens_per_minute = Some(tokens);
        self
    }

    pub fn with_max_request_bytes(mut self, bytes: u64) -> Self {
        self.max_request_bytes = Some(bytes);
        self
    }

    /// [`audio_tokens_per_minute`](Self::audio_tokens_per_minute) per second.
    pub fn audio_tokens_per_second(&self) -> Option<f64> {
        self.audio_tokens_per_minute.map(|t| f64::from(t) / 60.0)
    }

    /// What one `width` x `height` image costs in tokens, if known: the fixed cost, or with
    /// [`image_patch`](Self::image_patch) the patches after scaling down to
    /// [`image_edge`](Self::image_edge), capped at [`image_tokens`](Self::image_tokens).
    pub fn image_cost(&self, width: u32, height: u32) -> Option<u32> {
        let Some(patch) = self.image_patch.filter(|p| *p > 0) else {
            return self.image_tokens;
        };
        let (w, h) = self.scaled(width, height);
        let tokens = u64::from(w.div_ceil(patch)) * u64::from(h.div_ceil(patch));
        let tokens = u32::try_from(tokens).unwrap_or(u32::MAX);
        Some(self.image_tokens.map_or(tokens, |cap| tokens.min(cap)))
    }

    /// `width` x `height` scaled down (aspect kept) so the longer edge fits
    /// [`image_edge`](Self::image_edge); unchanged when it fits or no edge is known.
    pub fn scaled(&self, width: u32, height: u32) -> (u32, u32) {
        let long = width.max(height);
        match self.image_edge {
            Some(edge) if edge > 0 && long > edge => {
                let s = |v: u32| ((u64::from(v) * u64::from(edge)).div_ceil(u64::from(long)) as u32).max(1);
                (s(width), s(height))
            }
            _ => (width, height),
        }
    }

    /// What `millis` of audio costs in tokens (rounded up), if known.
    pub fn audio_cost(&self, millis: u64) -> Option<u64> {
        self.audio_tokens_per_minute.map(|t| (u64::from(t) * millis).div_ceil(60_000))
    }

    /// Each field from `self` when known, else from `fallback`: operator settings over measured
    /// ones (`operator.or(measured)`).
    pub fn or(self, fallback: Self) -> Self {
        Self {
            max_images: self.max_images.or(fallback.max_images),
            image_tokens: self.image_tokens.or(fallback.image_tokens),
            image_patch: self.image_patch.or(fallback.image_patch),
            image_edge: self.image_edge.or(fallback.image_edge),
            audio_max_seconds: self.audio_max_seconds.or(fallback.audio_max_seconds),
            audio_tokens_per_minute: self.audio_tokens_per_minute.or(fallback.audio_tokens_per_minute),
            max_request_bytes: self.max_request_bytes.or(fallback.max_request_bytes),
        }
    }

    /// The stricter of two copies, field by field: fewer images, a smaller edge, shorter audio,
    /// fewer request bytes, a smaller patch and higher token costs. A field known on one side
    /// only keeps that side's value, so merging an engine's limits with a model's loses nothing;
    /// a field neither side knows stays absent.
    pub fn tightest(self, other: Self) -> Self {
        fn pick<T: Ord>(a: Option<T>, b: Option<T>, f: fn(T, T) -> T) -> Option<T> {
            match (a, b) {
                (Some(a), Some(b)) => Some(f(a, b)),
                (a, b) => a.or(b),
            }
        }
        Self {
            max_images: pick(self.max_images, other.max_images, Ord::min),
            image_tokens: pick(self.image_tokens, other.image_tokens, Ord::max),
            image_patch: pick(self.image_patch, other.image_patch, Ord::min),
            image_edge: pick(self.image_edge, other.image_edge, Ord::min),
            audio_max_seconds: pick(self.audio_max_seconds, other.audio_max_seconds, Ord::min),
            audio_tokens_per_minute: pick(self.audio_tokens_per_minute, other.audio_tokens_per_minute, Ord::max),
            max_request_bytes: pick(self.max_request_bytes, other.max_request_bytes, Ord::min),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn full() -> MediaLimits {
        MediaLimits::new()
            .with_max_images(8)
            .with_image_tokens(1024)
            .with_image_patch(28)
            .with_image_edge(896)
            .with_audio_max_seconds(30)
            .with_audio_tokens_per_minute(375)
            .with_max_request_bytes(32 << 20)
    }

    #[test]
    fn unknown_limits_are_left_off_the_wire() {
        assert!(MediaLimits::default().is_unknown());
        assert_eq!(serde_json::to_string(&MediaLimits::UNKNOWN).unwrap(), "{}");
        let partial = MediaLimits::new().with_max_images(4);
        assert_eq!(serde_json::to_string(&partial).unwrap(), r#"{"max_images":4}"#);
        let back: MediaLimits = serde_json::from_str("{}").unwrap();
        assert!(back.is_unknown());
    }

    #[test]
    fn round_trips_through_json_and_cbor() {
        for l in [MediaLimits::UNKNOWN, MediaLimits::new().with_image_tokens(256), full()] {
            let j: MediaLimits = serde_json::from_str(&serde_json::to_string(&l).unwrap()).unwrap();
            assert_eq!(j, l);
            let c: MediaLimits = cbor4ii::serde::from_slice(&cbor4ii::serde::to_vec(Vec::new(), &l).unwrap()).unwrap();
            assert_eq!(c, l);
        }
        let v = serde_json::to_value(full()).unwrap();
        for k in ["max_images", "image_tokens", "image_patch", "image_edge", "audio_max_seconds", "audio_tokens_per_minute", "max_request_bytes"] {
            assert!(v.get(k).is_some(), "{k}");
        }
    }

    #[test]
    fn image_and_audio_costs() {
        let fixed = MediaLimits::new().with_image_tokens(256);
        assert_eq!(fixed.image_cost(4000, 3000), Some(256));
        assert_eq!(MediaLimits::UNKNOWN.image_cost(10, 10), None);
        let patched = MediaLimits::new().with_image_patch(28).with_image_edge(896);
        assert_eq!(patched.scaled(1792, 896), (896, 448));
        assert_eq!(patched.scaled(500, 300), (500, 300));
        assert_eq!(patched.image_cost(1792, 896), Some(32 * 16));
        assert_eq!(patched.image_cost(29, 28), Some(2));
        assert_eq!(patched.with_image_tokens(100).image_cost(1792, 896), Some(100), "capped");
        assert_eq!(MediaLimits::new().with_image_patch(14).image_cost(28, 28), Some(4), "no edge: as sent");
        let audio = MediaLimits::new().with_audio_tokens_per_minute(375);
        assert_eq!(audio.audio_tokens_per_second(), Some(6.25));
        assert_eq!(audio.audio_cost(60_000), Some(375));
        assert_eq!(audio.audio_cost(1_000), Some(7), "rounded up");
        assert_eq!(MediaLimits::UNKNOWN.audio_cost(1_000), None);
    }

    #[test]
    fn merging() {
        let a = MediaLimits::new().with_max_images(8).with_image_tokens(256).with_max_request_bytes(10);
        let b = MediaLimits::new().with_max_images(2).with_image_tokens(576).with_image_edge(336);
        let t = a.tightest(b);
        assert_eq!((t.max_images, t.image_tokens, t.image_edge, t.max_request_bytes), (Some(2), Some(576), Some(336), Some(10)));
        assert_eq!(t.audio_max_seconds, None);
        assert_eq!(a.tightest(MediaLimits::UNKNOWN), a);
        let operator = MediaLimits::new().with_max_images(1);
        let o = operator.or(a);
        assert_eq!((o.max_images, o.image_tokens), (Some(1), Some(256)));
    }
}