From 6e07c1d4cf0e8405bc0220c405eb00194d36e1f0 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 29 Nov 2026 00:48:48 +0100 Subject: [PATCH] Thing 2.0 cut 7a h2: a change value is validated before it is drafted (unvalidated-change-values) Steward decision 3 of the h2 fix brief. A non-finite slider value is refused, a finite one is clamped to the slider's range, and a select choice naming no declared option is refused. A refused change is not drafted and answers with Code::ActivationRefused. Co-Authored-By: Claude Opus 5.5 --- crates/thing-core/src/command/capture.rs | 19 +++++++++++ crates/thing-core/src/command/mod.rs | 10 +++++- crates/thing-core/src/diagnostic.rs | 4 +++ crates/thing-core/tests/command.rs | 42 ++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) diff --git a/crates/thing-core/src/command/capture.rs b/crates/thing-core/src/command/capture.rs index 185a801..12522ac 100644 --- a/crates/thing-core/src/command/capture.rs +++ b/crates/thing-core/src/command/capture.rs @@ -89,3 +89,22 @@ pub(super) fn capture(tree: &BoundTree, drafts: &Drafts, surface: &str, scope: & } Ok(captured) } + +/// The value a change input takes, or why it takes none. A slider is a range input: a value that is +/// not finite is refused, and a finite one outside its range is clamped to it. A select takes only an +/// option it declares (bound options included, as bound for this node). +pub(super) fn change(tree: &BoundTree, laid: &NodeId, node: &Node, value: &InputValue) -> Result { + match (node, value) { + (Node::Slider(slider), InputValue::Number(at)) => { + if !at.value.is_finite() { + return Err("the slider value is not a finite number"); + } + Ok(InputValue::Number(NumberValue { value: at.value.max(slider.min).min(slider.max) })) + } + (Node::Select(_), InputValue::Choice(chosen)) => match tree.value(laid, Field::Options) { + Some(Bound::Options(options)) if options.iter().any(|option| option.value == chosen.value) => Ok(value.clone()), + _ => Err("the choice names no declared option"), + }, + _ => Ok(value.clone()), + } +} diff --git a/crates/thing-core/src/command/mod.rs b/crates/thing-core/src/command/mod.rs index b59feb5..ef664a8 100644 --- a/crates/thing-core/src/command/mod.rs +++ b/crates/thing-core/src/command/mod.rs @@ -176,6 +176,11 @@ fn fault(surface: &str, node: Option<&NodeId>, code: Code, detail: impl Into Diagnostic { + fault(surface, Some(node), Code::ActivationRefused, detail) +} + /// A v4 UUID from 16 bytes: the version and variant bits are core's, so a minted id is always /// UUID-shaped and dot-free (a result key splits at its last dot). fn uuid(ids: &mut dyn IdSource) -> String { @@ -248,7 +253,10 @@ impl Commands { let Some(located) = tree.locate(&act.node) else { return Vec::new() }; let Some(CommandRef::Id(command)) = command_of(located.node, &act.kind) else { return Vec::new() }; if let ActivationKind::Change(value) = &act.kind { - drafts.set(&surface, &act.node, value.clone()); + match capture::change(tree, &act.node, located.node, value) { + Ok(taken) => drafts.set(&surface, &act.node, taken), + Err(detail) => return vec![refused(&surface, &act.node, detail)], + } } if matches!(located.node, Node::Button(_)) && tree.value(&act.node, Field::Enabled) == Some(&Bound::Bool(false)) { return Vec::new(); diff --git a/crates/thing-core/src/diagnostic.rs b/crates/thing-core/src/diagnostic.rs index 5625d05..e03c5a4 100644 --- a/crates/thing-core/src/diagnostic.rs +++ b/crates/thing-core/src/diagnostic.rs @@ -59,6 +59,10 @@ pub enum Code { CaptureUnavailable, /// A command capture naming no node, or a node that holds no input value. CaptureUndeclared, + /// An activation core refused to act on: a kind the node does not take, a node nobody laid, a change + /// value outside what the input allows, a command blocked by a confirmation, or a resume that can no + /// longer run. Nothing was minted; the detail says which. + ActivationRefused, } impl Code { diff --git a/crates/thing-core/tests/command.rs b/crates/thing-core/tests/command.rs index 3a12fa3..aa7b698 100644 --- a/crates/thing-core/tests/command.rs +++ b/crates/thing-core/tests/command.rs @@ -1224,3 +1224,45 @@ fn a_typed_draft_edited_after_the_press_survives_the_completion() { assert_eq!(rig.draft(&nid("name")), Some(text_value("typed since"))); } +// ----- a change is validated before it is drafted ----- + +fn range_surface() -> SurfaceDocument { + let slider = Node::Slider(SliderNode { common: common("vol"), min: 0.0, max: 1.0, step: 0.1, value: lit_number(0.2), commit: command("go") }); + let select = Node::Select(SelectNode { + common: common("pick"), + options: OptionList::Literal(LiteralOptions { options: vec![SelectOption { value: "a".into(), label: "A".into() }] }), + value: record_bind(path(&["Theme"]), None), + on_change: command("pk"), + }); + surface(PROVIDER, boxed("root", vec![slider, select]), vec![decl("go", &["vol"], None), decl("pk", &["pick"], None)]) +} + +#[test] +fn a_slider_change_is_refused_when_not_finite_and_clamped_to_its_range() { + for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] { + let mut rig = Rig::new(MemState::new(), &range_surface()); + let refused = rig.act(&nid("vol"), ActivationKind::Change(number_value(value)), PINNED_NOW, &mut SeqIds::new()); + assert_eq!(codes(&refused), vec![Code::ActivationRefused], "{value}"); + assert!(rig.commands.take_outbound().is_empty()); + assert_eq!(rig.draft(&nid("vol")), None); + } + for (sent, taken) in [(5.0, 1.0), (-3.0, 0.0), (0.0, 0.0), (1.0, 1.0), (0.4, 0.4)] { + let mut rig = Rig::new(MemState::new(), &range_surface()); + let answered = rig.act(&nid("vol"), ActivationKind::Change(number_value(sent)), PINNED_NOW, &mut SeqIds::new()); + assert!(answered.is_empty(), "{sent}"); + assert_eq!(rig.draft(&nid("vol")), Some(number_value(taken))); + assert_eq!(rig.commands.take_outbound()[0].captured[0].value, number_value(taken)); + } +} + +#[test] +fn a_select_change_naming_no_declared_option_is_refused() { + let mut rig = Rig::new(MemState::new(), &range_surface()); + let refused = rig.act(&nid("pick"), ActivationKind::Change(choice_value("nope")), PINNED_NOW, &mut SeqIds::new()); + assert_eq!(codes(&refused), vec![Code::ActivationRefused]); + assert!(rig.commands.take_outbound().is_empty()); + assert_eq!(rig.draft(&nid("pick")), None); + assert!(rig.act(&nid("pick"), ActivationKind::Change(choice_value("a")), at(1), &mut SeqIds::new()).is_empty()); + assert_eq!(rig.draft(&nid("pick")), Some(choice_value("a"))); +} +