fix(anatomy): R5.4 records bounded in total, CHANGELOG tables projected before they are joined

This commit is contained in:
bchanot committed 2026-10-23 14:58:40 +02:00
1 parent 777c8000b8
commit 5880dfa959
9 files changed
+258 -6

No files matched your search

+34 -2
View File
@@ -14,7 +14,9 @@
use serde_json::Value;
use super::ops::{apply_op, assert_declared};
use super::records::{ChangeRecord, CurationResult, Recorder, strip_marks, touch_order};
use super::records::{
ChangeRecord, CurationResult, Recorder, record_weight, strip_marks, touch_order,
};
use super::schema::{Change, ChangeSummary, Op, SetOp};
use super::shape::assert_same_shape;
use super::targets::{
@@ -95,10 +97,37 @@ fn mark_to_confirm(
Ok(())
}
/// The total weight of the records (`records::record_weight`): about 16 MiB of text, or a
/// quarter of a million JSON values, far past the equine changeset (test
/// `the_records_cannot_outgrow_their_bound`) and small enough that the reports built from them
/// stay bounded too. Checked after every op, before the next one records anything.
pub const MAX_RECORDED_WEIGHT: usize = 16 * 1024 * 1024;
/// The records counted so far and their weight.
#[derive(Default)]
struct Tally {
counted: usize,
weight: usize,
}
fn assert_recorded(records: &[ChangeRecord], tally: &mut Tally) -> Result<(), PipelineError> {
for record in &records[tally.counted..] {
tally.weight = tally.weight.saturating_add(record_weight(record));
}
tally.counted = records.len();
if tally.weight > MAX_RECORDED_WEIGHT {
return Err(fail(format!(
"the records weigh more than {MAX_RECORDED_WEIGHT} units"
)));
}
Ok(())
}
fn apply_change(
doc: &mut Value,
change: &Change,
records: &mut Vec<ChangeRecord>,
tally: &mut Tally,
) -> Result<(), PipelineError> {
let summary = change.summary();
assert_declared(doc, &change.reference_ids)
@@ -108,10 +137,12 @@ fn apply_change(
let mut recorder = Recorder::new(&summary, op, records, &mut next_touch);
apply_op(doc, &mut recorder)
.and_then(|()| assert_depth(doc))
.and_then(|()| assert_recorded(records, tally))
.map_err(|error| fail(format!("{} op {index}: {error}", change.id)))?;
}
if change.level.needs_confirmation() {
mark_to_confirm(doc, &summary, records)?;
assert_recorded(records, tally).map_err(|error| fail(format!("{}: {error}", change.id)))?;
}
strip_marks(doc);
Ok(())
@@ -204,8 +235,9 @@ pub fn apply_changeset(
.ok_or_else(|| fail("the baseline is not an object"))?
.insert("references".to_owned(), registry);
let mut records = Vec::new();
let mut tally = Tally::default();
for change in changes {
apply_change(&mut doc, change, &mut records)?;
apply_change(&mut doc, change, &mut records, &mut tally)?;
}
curated_issues(&doc)?;
Ok(CurationResult {
+4 -1
View File
@@ -12,7 +12,7 @@ use super::schema::ChangeLevel;
use super::stats::{DocumentStats, stat_rows};
use super::targets::fail;
use crate::curation::PipelineError;
use crate::curation::markdown::{Cell, code, table};
use crate::curation::markdown::{Cell, assert_table_bound, code, table};
use crate::curation::report_guard::assert_authored;
use crate::json::to_js_compact;
@@ -113,6 +113,9 @@ fn level_section(level: ChangeLevel, records: &[ChangeRecord]) -> Result<String,
"Références",
"Confirmation requise",
];
// The rows are bounded through the records' weight; their alignment is checked here,
// before the raw table is joined.
assert_table_bound(&head, &rows)?;
let body = if rows.is_empty() {
"Aucun changement de ce niveau.".to_owned()
} else {
+44
View File
@@ -105,6 +105,50 @@ impl<'a> Recorder<'a> {
}
}
/// What the records hold, in weight units: 64 per JSON value (a `Value` node costs tens of
/// bytes) plus one per byte of every string (the values' and the change's texts, which each
/// record clones). A rename or a remove records the whole item, so the records can grow far
/// past the document; `apply` bounds their total (`MAX_RECORDED_WEIGHT`).
pub fn record_weight(record: &ChangeRecord) -> usize {
let change = &record.change;
let texts = [
change.id.as_str(),
&change.title.fr,
&change.title.en,
&change.rationale,
change.question.as_deref().unwrap_or_default(),
&record.target,
record.field.as_deref().unwrap_or_default(),
];
let text: usize = texts.iter().map(|text| text.len()).sum::<usize>()
+ change.reference_ids.iter().map(String::len).sum::<usize>();
let op_value = match &record.op {
Op::Set(op) => value_weight(&op.value),
Op::Add(op) => value_weight(&op.value),
_ => 0,
};
text + op_value + value_weight(&record.before) + value_weight(&record.after)
}
/// The weight of a value, walked with an explicit stack (no recursion).
fn value_weight(value: &Value) -> usize {
let mut weight = 0;
let mut pending = vec![value];
while let Some(value) = pending.pop() {
weight += 64;
match value {
Value::String(text) => weight += text.len(),
Value::Object(map) => {
weight += map.keys().map(String::len).sum::<usize>();
pending.extend(map.values());
}
Value::Array(items) => pending.extend(items),
_ => {}
}
}
weight
}
/// The touch order of a marked item.
pub fn touch_order(item: &Item) -> Option<u64> {
item.get(TOUCH_MARK).and_then(Value::as_u64)
+25
View File
@@ -48,6 +48,31 @@ pub fn cell(value: &Cell) -> String {
}
}
/// Refuses a table whose alignment would pass `MAX_TABLE_COLUMNS_TOTAL`, from its cells, before
/// the raw table is joined: a cell's bytes bound its display width (an emoji, the one glyph
/// wider than its bytes, is refused by the width table), so the projection never under-counts.
pub fn assert_table_bound(head: &[&str], rows: &[Vec<Cell>]) -> Result<(), PipelineError> {
let mut widths: Vec<usize> = head.iter().map(|text| text.len().max(3)).collect();
for row in rows {
for (width, value) in widths.iter_mut().zip(row) {
let bytes = match value {
Cell::Text(text) => text.len() + text.matches('|').count(),
Cell::Number(_) => 24,
Cell::Null => 1,
};
*width = (*width).max(bytes);
}
}
let row_width = widths.iter().sum::<usize>() + 3 * widths.len() + 1;
if row_width.saturating_mul(rows.len() + 2) > MAX_TABLE_COLUMNS_TOTAL {
return Err(PipelineError::Guard(format!(
"an aligned table of {} rows × {row_width} columns exceeds the {MAX_TABLE_COLUMNS_TOTAL} bound",
rows.len() + 2
)));
}
Ok(())
}
/// `table()`: the raw table the TypeScript wrote before Prettier aligned it.
pub fn table(head: &[&str], rows: &[Vec<Cell>]) -> String {
let mut lines = vec![head.join(" | "), vec!["---"; head.len()].join(" | ")];
+27
View File
@@ -775,6 +775,33 @@ fn the_document_cannot_hold_more_values_than_the_bound() {
assert!(error.contains("more than 200000 values"), "{error}");
}
/// A rename or a remove records the whole item, before and after, with the change's texts:
/// the records are bounded in total (security gate R5.4 round 7: 400 renames of a
/// 5,000-child item asked for 4.3 GB). The equine changeset stays far below the bound.
#[test]
fn the_records_cannot_outgrow_their_bound() {
use faunosteo_core::curation::draft::apply::MAX_RECORDED_WEIGHT;
use faunosteo_core::curation::draft::records::record_weight;
let equine: usize = equine_draft().records.iter().map(record_weight).sum();
println!("equine records weigh {equine} units");
assert!(equine * 8 < MAX_RECORDED_WEIGHT, "{equine}");
let children: Vec<Value> = (0..5_000)
.map(|i| json!({ "id": format!("n{i}"), "name": { "fr": "a", "en": "a" } }))
.collect();
let value = json!({ "id": "many", "name": { "fr": "a", "en": "a" }, "children": children });
let mut ops = vec![json!({ "op": "add", "target": MOBILITY, "value": value })];
for round in 0..200 {
let (from, to) = if round % 2 == 0 {
("many", "other")
} else {
("other", "many")
};
ops.push(json!({ "op": "rename", "target": format!("{MOBILITY}/{from}"), "id": to }));
}
let error = mini_apply(Value::from(ops)).unwrap_err();
assert!(error.contains("the records weigh more than"), "{error}");
}
/// A `__proto__` segment had no faithful reading in the TypeScript (a prototype set, a
/// polluted `Object.prototype`, or nothing written): the port refuses it at the op.
#[test]
+26
View File
@@ -149,3 +149,29 @@ fn every_probe_is_formatted_as_the_typescript_or_refused() {
assert_eq!(same + refused, 798);
println!("{same} identical, {refused} refused");
}
/// One long authored cell pads every row of its level: the CHANGELOG is refused before its
/// table is joined, through the pipeline (security gate R5.4 round 7), while the records stay
/// within their own bound.
#[test]
fn a_changelog_whose_alignment_would_explode_is_refused_before_it_is_built() {
let frozen = oracle();
let mobility = "finding:hip/hip-dysfunctions/mobility";
let sets: Vec<Value> = (0..20)
.map(|i| json!({ "op": "set", "target": mobility, "field": "name.en", "value": format!("v{i}") }))
.collect();
let change = |id: &str, rationale: String, ops: Vec<Value>| {
json!({ "id": id, "level": "A", "kind": "technical",
"title": { "fr": "Essai", "en": "Test" }, "rationale": rationale,
"reference_ids": [], "ops": ops })
};
let long = change("CHG-001", "a".repeat(2_000_000), vec![sets[0].clone()]);
let rows = change("CHG-002", "Essai".to_owned(), sets);
let case = json!({ "changes_file": { "changes": [long, rows] } });
let (changelog, _) = raw_reports(&frozen, &case);
let error = changelog.unwrap_err();
assert!(
matches!(&error, PipelineError::Guard(message) if message.contains("exceeds the")),
"{error}"
);
}