Skip to main content

velesdb_memory/
limits.rs

1//! Resource caps shared by every adapter (the MCP server and the language
2//! bindings).
3//!
4//! These are security-relevant DoS limits. They live here — not inside any one
5//! adapter — so every transport enforces the *same* numbers without a manual
6//! "keep in sync" comment, and so a build without the `mcp` feature still sees
7//! them. Each adapter formats its own transport-native error; only the values
8//! and the clamping policy are shared.
9
10use crate::service::Metadata;
11
12/// Default hop budget for `why` traversal when the caller supplies none.
13pub const DEFAULT_WHY_HOPS: usize = 2;
14
15/// Maximum accepted fact size (1 MiB) — prevents allocating huge embeddings.
16pub const MAX_FACT_BYTES: usize = 1_048_576;
17
18/// Maximum accepted size of caller-supplied `metadata` (64 KiB), measured as
19/// its serialized JSON form. Metadata is a keyed lookup facet (project,
20/// author, status, …) — a porte-clés, not a payload — so it gets a much
21/// tighter ceiling than [`MAX_FACT_BYTES`]: without one, a caller could smuggle
22/// an arbitrarily large JSON blob through `metadata` on every write path
23/// (`remember`, `remember_with_ttl`, `remember_extracted`, and each
24/// context-compiler fragment's own `metadata`) and force the same unbounded
25/// allocation and storage growth the fact-size cap exists to prevent.
26pub const MAX_METADATA_BYTES: usize = 64 * 1024;
27
28/// The serialized JSON size of `meta`, in bytes. Returns `usize::MAX` if the
29/// map somehow fails to serialize (it never should — `Metadata` is always
30/// valid JSON), so a serialization hiccup fails a size check closed rather
31/// than silently passing an unmeasured payload.
32#[must_use]
33pub fn metadata_bytes(meta: &Metadata) -> usize {
34    serde_json::to_vec(meta).map_or(usize::MAX, |v| v.len())
35}
36
37/// Cap on a `recall` limit — prevents unbounded vector scans (core does not
38/// cap `k`, so the adapters do).
39pub const MAX_RECALL_LIMIT: usize = 1_000;
40
41/// Cap on `why` hop depth — prevents exponential graph fan-out.
42pub const MAX_WHY_HOPS: usize = 10;
43
44/// Maximum accepted size of a single context-compiler fragment (1 MiB, the
45/// same ceiling as [`MAX_FACT_BYTES`]) — prevents a single fragment from
46/// forcing huge allocations in the compile pipeline.
47pub const MAX_FRAGMENT_BYTES: usize = 1_048_576;
48
49/// Cap on the number of fragments in one compile request — bounds the work a
50/// single call can demand across every adapter.
51pub const MAX_FRAGMENTS: usize = 1_024;
52
53/// Maximum accepted size of a fragment's base64-encoded media payload
54/// (US-009, PR1: inline images) — 4 MiB of base64 text, roughly 3 MiB of raw
55/// bytes once decoded. Deliberately separate from [`MAX_FRAGMENT_BYTES`],
56/// which only ever measures [`crate::context::model::ContextFragment::content`]
57/// (the caption): a screenshot is not text, and capping it at the 1 MiB text
58/// ceiling would reject ordinary screenshots outright. Measured against
59/// `bytes_b64.len()` (the encoded string), so the cap can reject an
60/// oversized payload before any base64 decoding is attempted.
61pub const MAX_MEDIA_BYTES: usize = 4 * 1024 * 1024;
62
63/// Aggregate cap on ALL media payloads of one request (base64 length,
64/// summed). Without it, `MAX_FRAGMENTS` fragments each at [`MAX_MEDIA_BYTES`]
65/// would let a single request carry 4 GiB of media — far past the ~1 GiB
66/// worst case the text caps allow. 64 MiB comfortably fits a real
67/// screenshot-heavy session while bounding decode work.
68pub const MAX_TOTAL_MEDIA_BYTES: usize = 64 * 1024 * 1024;
69
70/// Cap on a caller-supplied token budget. A budget cannot force allocations
71/// by itself, but an absurd value would make the savings arithmetic
72/// meaningless, so adapters clamp to this ceiling instead of erroring.
73pub const MAX_TOKEN_BUDGET: u64 = 10_000_000;
74
75/// Clamp a caller-supplied token budget to [`MAX_TOKEN_BUDGET`].
76#[must_use]
77pub fn clamp_token_budget(budget: u64) -> u64 {
78    budget.min(MAX_TOKEN_BUDGET)
79}
80
81/// Clamp a caller-supplied recall limit to [`MAX_RECALL_LIMIT`].
82#[must_use]
83pub fn clamp_recall_limit(k: usize) -> usize {
84    k.min(MAX_RECALL_LIMIT)
85}
86
87/// Clamp a caller-supplied `why` hop budget to [`MAX_WHY_HOPS`].
88#[must_use]
89pub fn clamp_hops(hops: usize) -> usize {
90    hops.min(MAX_WHY_HOPS)
91}