feat(domain): R4.2e OD-3 value cap (400,000), rescue-copy guard, 1 GiB linear ceiling, budget measurements

json_js::MAX_JSON_VALUES from the R4.2e measurements (CAP envelope 39,652 values, 612-leaf
export 23,803; ~100 B of tree per value, a 16 MiB dense document 784 MiB with the cap off,
16 MiB with it); draft.restore refuses an over-budget copy as invalid; --max-memory 1 GiB
proven to trap on the proofs build; cargo pdf_budget prints the value table, the Node
budget suite prints the per-class cost (fresh instance, W4 asserted).
This commit is contained in:
bchanot committed 2026-10-18 16:06:36 +02:00
1 parent f8579bc19a
commit 6ee04c96c5
13 files changed
+599 -14

No files matched your search

-1
View File
@@ -10,7 +10,6 @@ pub mod domain;
pub mod facade;
pub mod json;
pub mod ops;
#[cfg(feature = "pdf-read")]
pub mod pdf;
/// Version of the core, reported by the wasm facade and the worker.
+6
View File
@@ -18,6 +18,7 @@ use crate::domain::report::{LateralSide, Observation, Report, SheetRef, Vertebra
use crate::domain::session::{self, ImportFailure, RestoreFailure};
use crate::domain::sheet::{LoadedSheet, SheetIndexData, check_sheet_document};
use crate::facade::{Failure, parse_input, to_value};
use crate::pdf::json_js::{MAX_JSON_VALUES, prescan};
type Outcome = Result<Value, Failure>;
@@ -314,6 +315,11 @@ fn versioned(kind: &'static str, found: Option<f64>) -> Failure {
/// draft, and leaves it where it is.
fn draft_restore(sheet: &LoadedSheet, input: &str) -> Outcome {
let input: DraftRestoreInput = parse_input(input)?;
// OD-3: a token-dense text is refused before the parser allocates its tree; the caller
// sets the text aside verbatim, as for any unreadable draft (`invalid`).
if prescan(input.draft.as_bytes()).values > MAX_JSON_VALUES {
return Err(Failure::new("invalid", "json value budget exceeded"));
}
session::restore_draft(sheet, &input.draft, input.archive.as_deref())
.map_err(|failure| match failure {
RestoreFailure::UnsupportedVersion(found) => versioned("unsupported_version", found),
+9
View File
@@ -26,6 +26,15 @@ use crate::domain::js::{find_lone_surrogate, parse_json};
/// The report and its envelope nest under 16 levels; deeper is not ours (`MAX_JSON_DEPTH`).
pub const MAX_JSON_DEPTH: usize = 64;
/// Values ([`prescan`]) a document may hold before the probe and the rescue copy refuse it
/// (OD-3). Measured at R4.2e in the shipped module: the parser's tree costs about 100 bytes
/// of linear memory per value, a 16 MiB token-dense document holds 8.4 million (784 MiB,
/// what BLK-012 observed); the largest legitimate archive, an envelope at the collection
/// caps (500 observations, 1,000 views, logos) holds 39,652, the 612-leaf export 23,803.
/// Ten times the largest legitimate archive, rounded: the worst case left is about 40 MiB
/// of tree (W4 ≤ 256 MiB). Set here, confirmed by the owner before R4.3 wires the import.
pub const MAX_JSON_VALUES: u64 = 400_000;
const BOM: &[u8] = b"\xEF\xBB\xBF";
/// The signed form of the cap: the depth may go negative, as in the TypeScript.
+2
View File
@@ -6,6 +6,8 @@
//! pinned by the frozen oracles O7 (`tests/oracles/r4/read.json`), O8 (`tolerance.json`) and
//! O9 (`inflate.json`). The writer (`write`) and the import entry (`import`) follow.
#[cfg(feature = "pdf-read")]
pub mod import;
pub mod json_js;
#[cfg(feature = "pdf-read")]
pub mod read;
+3 -9
View File
@@ -12,7 +12,7 @@
//!
//! OD-3 adds one cap the TypeScript did not have: a candidate whose value count (the
//! parser's allocation proxy) exceeds the limit is skipped like one nested too deep, before
//! any parser allocates its tree. Its threshold is measured at R4.2e.
//! any parser allocates its tree (`json_js::MAX_JSON_VALUES`, measured at R4.2e).
use std::borrow::Cow;
@@ -24,8 +24,8 @@ use super::{
has_pdf_header,
};
use crate::pdf::json_js::{
JsonPrescan, decode_lossy, is_versioned_object, prescan, rewrite_overflow_numbers,
starts_json_object,
JsonPrescan, MAX_JSON_VALUES, decode_lossy, is_versioned_object, prescan,
rewrite_overflow_numbers, starts_json_object,
};
/// The caps the probe applies on top of the scanner's own.
@@ -35,12 +35,6 @@ pub struct ProbeLimits {
pub max_json_values: u64,
}
/// Values a candidate may hold before the probe skips it (OD-3). A legitimate archive
/// holds a few thousand values; the threshold is set from the R4.2e measurements (10 × the
/// largest legitimate archive) and confirmed by the owner before the import is wired.
/// Until then the cap is off, so the parity suites measure the TypeScript behaviour.
pub const MAX_JSON_VALUES: u64 = u64::MAX;
impl ProbeLimits {
/// No value cap: the TypeScript behaviour, for the parity suites.
pub const UNBOUNDED: Self = Self {