fix(anatomy): R5.4 records bounded in total, CHANGELOG tables projected before they are joined
This commit is contained in:
1 parent
777c8000b8
commit
5880dfa959
9 files changed
+258
-6
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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(" | ")];
|
||||
|
||||
Reference in new issue
Block a user