test(host): a chain may write one record twice, and why that works

Two measures on one record is one record, so writing the same record twice in
a frame is both legitimate and likely. It works for a reason worth pinning
rather than rediscovering: **every check happens before any command is
applied**, so the second write is compared to the record's *pre-frame*
counter and not to the one its own sibling just bumped.

Were the checks interleaved with the applies — which is what the previous
commit's per-command record check would look like if it were moved into the
apply loop — the frame would refuse itself with `RecordMoved`. Proved by
injecting exactly that interleaving: this arm fails and nothing else does.

1291 passing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Edward Langley
2026-09-20 00:09:09 -07:00
parent 243b9a9b6b
commit a685503902
+42
View File
@@ -881,6 +881,48 @@ mod tests {
.get_cell(&cell("South")),
Some(&CellValue::Number(20.0)),
);
// ── arm 3: the same record twice in one chain ───────────────────
// Legitimate and likely — two measures on one record is one record —
// and it works for a reason worth pinning rather than rediscovering:
// **every check happens before any command is applied**, so the
// second write is compared to the record's *pre-frame* counter, not
// to the one its own sibling just bumped. Were the checks
// interleaved with the applies, this frame would refuse itself with
// `RecordMoved`.
let mut twice = host();
let (asker, _) = twice.connect(10, 10);
twice.model(asker, write("North", 1.0));
let basis = frame_basis(&twice, &["North"]);
let answer = twice.handle(
asker,
ClientFrame::Commands(vec![
Command::Model {
basis: basis.clone(),
cmd: write("North", 10.0),
},
Command::Model {
basis,
cmd: write("North", 11.0),
},
]),
);
assert!(
!answer
.iter()
.any(|a| matches!(a.msg, ServerFrame::Rejected(_))),
"a chain may write one record twice: {answer:?}"
);
assert_eq!(
twice
.app()
.model_state
.workbook
.model
.get_cell(&cell("North")),
Some(&CellValue::Number(11.0)),
"and the last write in the chain is the one that stands"
);
}
/// **The capability the contract is for**: a chain of data-dependent