Thing 2.0 cut 7a h2: a completion clears only the drafts it captured, where still held (clear-drafts-clobbers-newer-edit)

Steward decision 2 of the h2 fix brief. Drafts::clear becomes clear_held: a
captured draft is dropped only while the table still holds the captured value,
so an edit made after the press survives the result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-29 00:48:35 +01:00
commit 7a38c397a0
5 changed files with 56 additions and 15 deletions

View file

@ -9,11 +9,11 @@ use thing_contract::{BoolValue, CapturedInput, ChoiceValue, CommandDecl, InputKi
use crate::bind::{Bound, BoundTree, Field};
use crate::input::Drafts;
/// What the captures of one command came to: the values to send, and the laid ids they were read
/// from (the drafts a completed result clears).
/// What the captures of one command came to: the values to send, and the drafts they were read from
/// (laid id and the value the table held), which a completed result clears if still held.
pub(super) struct Captured {
pub inputs: Vec<CapturedInput>,
pub laid: Vec<NodeId>,
pub held: Vec<(NodeId, InputValue)>,
}
/// The laid id of `declared` as seen from a node whose enclosing instance prefixes are `scope`
@ -77,13 +77,15 @@ fn value(tree: &BoundTree, drafts: &Drafts, surface: &str, laid: &NodeId, node:
/// error is the declared id of the first capture with no value to send.
pub(super) fn capture(tree: &BoundTree, drafts: &Drafts, surface: &str, scope: &[String], decl: &CommandDecl) -> Result<Captured, NodeId> {
let index = tree.index();
let mut captured = Captured { inputs: Vec::new(), laid: Vec::new() };
let mut captured = Captured { inputs: Vec::new(), held: Vec::new() };
for declared in &decl.captures {
let laid = laid_id(&index, scope, declared).ok_or_else(|| declared.clone())?;
let node = index.get(&laid).ok_or_else(|| declared.clone())?;
let value = value(tree, drafts, surface, &laid, node).ok_or_else(|| declared.clone())?;
captured.inputs.push(CapturedInput { node: declared.clone(), value });
captured.laid.push(laid);
if let Some(draft) = drafts.get(surface, &laid) {
captured.held.push((laid, draft.clone()));
}
}
Ok(captured)
}

View file

@ -100,7 +100,7 @@ impl Commands {
},
};
if completed.clear_drafts {
drafts.clear(&surface, &self.live[at].laid);
drafts.clear_held(&surface, &self.live[at].held);
}
answered.effects.extend(effect);
self.settle(at, State::Completed, None, port, ledger);

View file

@ -121,7 +121,7 @@ struct Prepared {
command: CommandId,
payload_schema: SchemaId,
captured: Vec<CapturedInput>,
laid: Vec<NodeId>,
held: Vec<(NodeId, InputValue)>,
source: NodeId,
items: Vec<String>,
/// The laid node the user acted on; a resume has none.
@ -139,7 +139,7 @@ struct Live {
/// The highest result `seq` taken.
highest: i32,
deadline: Timestamp,
laid: Vec<NodeId>,
held: Vec<(NodeId, InputValue)>,
scope: Vec<String>,
interest: Interest,
source: NodeId,
@ -377,7 +377,7 @@ impl Commands {
command: decl.id.clone(),
payload_schema: decl.payload_schema.clone(),
captured: captured.inputs,
laid: captured.laid,
held: captured.held,
source: origin.source,
items: origin.items,
issuing: origin.issuing,
@ -416,7 +416,7 @@ impl Commands {
state: State::Issued,
highest: 0,
deadline: now.plus_ms(self.timeout_ms),
laid: prepared.laid,
held: prepared.held,
scope: prepared.scope,
interest,
source: prepared.source,

View file

@ -24,10 +24,14 @@ impl Drafts {
self.values.get(&(surface.to_string(), node.clone()))
}
/// Drop the drafts of `nodes`; a node with no draft is untouched.
pub fn clear(&mut self, surface: &str, nodes: &[NodeId]) {
for node in nodes {
self.values.remove(&(surface.to_string(), node.clone()));
/// Drop each draft in `held` that the table still holds with that value. A draft edited since it
/// was held is newer than what was sent, and stays; a node with no draft is untouched.
pub fn clear_held(&mut self, surface: &str, held: &[(NodeId, InputValue)]) {
for (node, value) in held {
let key = (surface.to_string(), node.clone());
if self.values.get(&key) == Some(value) {
self.values.remove(&key);
}
}
}

View file

@ -281,7 +281,9 @@ fn drafts_are_per_surface_and_clear_or_retain_exactly_what_is_named() {
drafts.set("s1", &nid(node), text_value(node));
}
drafts.set("s2", &nid("a"), text_value("other"));
drafts.clear("s1", &[nid("a"), nid("missing")]);
drafts.clear_held("s1", &[(nid("a"), text_value("a")), (nid("missing"), text_value("x"))]);
// A draft edited since it was held is newer: it stays.
drafts.clear_held("s1", &[(nid("b"), text_value("older"))]);
assert_eq!(drafts.get("s1", &nid("a")), None);
assert_eq!(drafts.get("s1", &nid("b")), Some(&text_value("b")));
assert_eq!(drafts.get("s2", &nid("a")), Some(&text_value("other")));
@ -1189,3 +1191,36 @@ fn removing_a_row_through_the_frame_drops_its_draft() {
);
}
// ----- completion clears what it captured, no more -----
fn tg_surface() -> SurfaceDocument {
surface(PROVIDER, boxed("root", vec![toggle("t", "tg", lit_bool(false)), button("b", "bc")]), vec![decl("tg", &["t"], None), decl("bc", &[], None)])
}
#[test]
fn a_completion_clears_the_drafts_it_captured_and_a_newer_edit_survives() {
for newer in [Some(false), None] {
let mut rig = Rig::new(MemState::new(), &tg_surface());
let mut ids = SeqIds::new();
rig.act(&nid("t"), ActivationKind::Change(bool_value(true)), PINNED_NOW, &mut ids);
let first = rig.commands.take_outbound().pop().expect("minted");
if let Some(value) = newer {
// The node is busy: this edit mints nothing, and it is the draft now.
rig.act(&nid("t"), ActivationKind::Change(bool_value(value)), at(10), &mut ids);
assert!(rig.commands.take_outbound().is_empty());
}
rig.feed(&completed(&first, 1, true, None), at(20));
assert_eq!(rig.draft(&nid("t")), newer.map(bool_value), "newer edit {newer:?}");
}
}
#[test]
fn a_typed_draft_edited_after_the_press_survives_the_completion() {
let mut rig = Rig::new(MemState::new(), &go_surface());
rig.set_draft(&nid("name"), text_value("sent"));
let sent = rig.mint("go", PINNED_NOW);
rig.set_draft(&nid("name"), text_value("typed since"));
rig.feed(&completed(&sent, 1, true, None), at(1));
assert_eq!(rig.draft(&nid("name")), Some(text_value("typed since")));
}