Files
hsmc/hsmc/tests/det_descent.rs
T
swaitsandClaude Opus 4.7 3fc0d7c57d feat(default)!: model default(...) as a real LCA-aware transition
`default(...)` is now a transition with the same semantics as any
event-driven transition. It fires immediately after the declaring
state's entries finish and its durings start, applying the standard
exit-to-LCA / enter-to-target algorithm. Three behavioral changes
follow from this unification:

* Optional. A state with children is no longer required to declare a
  `default(...)`. When omitted, the state itself is the resting
  innermost active state; substates are reachable only via explicit
  transitions. This is the "microwave Standby" pattern — Standby
  has substates like LowPower that you enter only on demand, and
  `=> Standby;` lands at Standby itself with no descent.

* Targets anywhere. The default target may be any state in the chart
  — a child (the classic descent case), a sibling, an ancestor,
  anywhere. `default(<sibling-or-ancestor>)` exits the just-entered
  state as part of the default-fire transition; the same rule fires
  whether you wrote it for descent, redirection, or up-bounce.

* Cycle-checked at compile time. The default-edge graph (out-degree
  ≤ 1 per node) is rejected if it contains any cycle, including
  `default(self)`. `state A { default(A); }` and `A → B → A` chains
  fail to compile with a pinpoint error message naming the cycle.
  This guarantees default chains terminate at runtime.

Implementation:

* `validate.rs`: drop the "must be direct child" and "must have
  children" restrictions; add `validate_default_graph` (iterative
  three-color cycle detection via chain walk).

* `ir.rs`: keep the unresolved default ident through to
  `resolve_transitions` so non-child targets resolve against the
  global state-name map (the same map transition targets use).

* `codegen.rs`: precomputed `(from, to)` paths no longer fold default
  descent into the enter slice — each path is LCA-relative
  `(exit, enter)` ending exactly at the explicit target.
  `descend_defaults` (a parent-pointer walk) is replaced by
  `follow_defaults`, which fires real transitions through
  `apply_path` and emits `TransitionFired{Internal}` /
  `TransitionComplete` brackets per link. Up-transitions don't
  trigger default-following (nothing was freshly entered, so no
  state's default fires).

* `active_durings_per_innermost_state`: predicate widened to include
  composites without a default (they can now be the innermost active
  state, so any durings they declare must be raced when current).

BREAKING CHANGES:

* Journal format. Default-fires now emit `TransitionFired` (with
  `reason: TransitionReason::Internal`) and `TransitionComplete`
  brackets around their entry sequence. `Internal` was previously
  reserved and never emitted; it is now active. Tests that
  hand-build expected journal sequences must include these brackets
  around every default-fire.

* Validation. `default(<non-child>)` and `default` on a childless
  state previously raised compile-time errors. Both are now valid.
  Code relying on these as guard rails must adapt.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-28 20:57:01 -06:00

337 lines
11 KiB
Rust

#![allow(dead_code)]
//! Deterministic-flow tests for DEFAULT-DESCENT into composite targets.
//!
//! Spec section "Default child":
//! Whenever a state with children is entered, the default child is entered
//! immediately after that state's entry actions finish — it's effectively
//! an automatic transition into the child.
//!
//! Pins down: entries fire outer-to-inner, default-descent unrolls the chain,
//! the leaf is the innermost active state at the end.
#![cfg(all(feature = "tokio", feature = "journal"))]
#![allow(unexpected_cfgs)]
use hsmc::{statechart, ActionKind, TraceEvent, TransitionReason};
#[derive(Default)]
pub struct Ctx;
#[derive(Debug, Clone)]
pub enum Ev {
Go,
Halt,
}
// Hierarchy: Root → Idle, Root → Tower → L1 → L2 → L3
statechart! {
Desc {
context: Ctx;
events: Ev;
default(Idle);
terminate(Halt);
state Idle {
entry: idle_in;
exit: idle_out;
on(Go) => Tower;
}
state Tower {
entry: tower_in;
exit: tower_out;
default(L1);
state L1 {
entry: l1_in;
exit: l1_out;
default(L2);
state L2 {
entry: l2_in;
exit: l2_out;
default(L3);
state L3 {
entry: l3_in;
exit: l3_out;
}
}
}
}
}
}
impl DescActions for DescActionContext<'_> {
async fn idle_in(&mut self) {}
async fn idle_out(&mut self) {}
async fn tower_in(&mut self) {}
async fn tower_out(&mut self) {}
async fn l1_in(&mut self) {}
async fn l1_out(&mut self) {}
async fn l2_in(&mut self) {}
async fn l2_out(&mut self) {}
async fn l3_in(&mut self) {}
async fn l3_out(&mut self) {}
}
const SR: u16 = 0;
const SI: u16 = 1;
const ST: u16 = 2;
const SL1: u16 = 3;
const SL2: u16 = 4;
const SL3: u16 = 5;
const A_IDLE_IN: u16 = 0;
const A_IDLE_OUT: u16 = 1;
const A_TOWER_IN: u16 = 2;
const A_TOWER_OUT: u16 = 3;
const A_L1_IN: u16 = 4;
const A_L1_OUT: u16 = 5;
const A_L2_IN: u16 = 6;
const A_L2_OUT: u16 = 7;
const A_L3_IN: u16 = 8;
const A_L3_OUT: u16 = 9;
const E_GO: u16 = 0;
fn entry(state: u16, action: u16) -> TraceEvent {
TraceEvent::ActionInvoked {
state,
action,
kind: ActionKind::Entry,
}
}
fn exit_(state: u16, action: u16) -> TraceEvent {
TraceEvent::ActionInvoked {
state,
action,
kind: ActionKind::Exit,
}
}
#[tokio::test(flavor = "current_thread")]
async fn det_descent_target_with_three_default_levels() {
// Idle → Tower: Tower's `default(L1)` fires as a transition, L1's
// `default(L2)` fires, L2's `default(L3)` fires. Each link is a
// real transition with its own TransitionFired/Complete pair.
tokio::task::LocalSet::new()
.run_until(async {
let mut m = Desc::new(Ctx);
let _ = m.dispatch(Ev::Go).await;
let actual = m.take_journal();
let expected = vec![
TraceEvent::Started {
chart_hash: Desc::<8>::CHART_HASH,
},
TraceEvent::EnterBegan { state: SR },
TraceEvent::Entered { state: SR },
// Root → Idle fires as Root's default.
TraceEvent::TransitionFired {
from: Some(SR),
to: SI,
reason: TransitionReason::Internal,
},
TraceEvent::EnterBegan { state: SI },
entry(SI, A_IDLE_IN),
TraceEvent::Entered { state: SI },
TraceEvent::TransitionComplete {
from: Some(SR),
to: SI,
},
TraceEvent::EventReceived { event: E_GO },
TraceEvent::EventDelivered {
handler_state: SI,
event: E_GO,
},
TraceEvent::TransitionFired {
from: Some(SI),
to: ST,
reason: TransitionReason::Event { event: E_GO },
},
TraceEvent::ExitBegan { state: SI },
exit_(SI, A_IDLE_OUT),
TraceEvent::Exited { state: SI },
TraceEvent::EnterBegan { state: ST },
entry(ST, A_TOWER_IN),
TraceEvent::Entered { state: ST },
// Tower's default(L1) fires.
TraceEvent::TransitionFired {
from: Some(ST),
to: SL1,
reason: TransitionReason::Internal,
},
TraceEvent::EnterBegan { state: SL1 },
entry(SL1, A_L1_IN),
TraceEvent::Entered { state: SL1 },
TraceEvent::TransitionComplete {
from: Some(ST),
to: SL1,
},
// L1's default(L2) fires.
TraceEvent::TransitionFired {
from: Some(SL1),
to: SL2,
reason: TransitionReason::Internal,
},
TraceEvent::EnterBegan { state: SL2 },
entry(SL2, A_L2_IN),
TraceEvent::Entered { state: SL2 },
TraceEvent::TransitionComplete {
from: Some(SL1),
to: SL2,
},
// L2's default(L3) fires.
TraceEvent::TransitionFired {
from: Some(SL2),
to: SL3,
reason: TransitionReason::Internal,
},
TraceEvent::EnterBegan { state: SL3 },
entry(SL3, A_L3_IN),
TraceEvent::Entered { state: SL3 },
TraceEvent::TransitionComplete {
from: Some(SL2),
to: SL3,
},
TraceEvent::TransitionComplete {
from: Some(SI),
to: ST,
},
];
assert_eq!(actual, expected);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn det_descent_entries_fire_outer_to_inner() {
tokio::task::LocalSet::new()
.run_until(async {
let mut m = Desc::new(Ctx);
let _ = m.dispatch(Ev::Go).await;
// After the GO event-driven transition: order of Entered
// events should be Tower → L1 → L2 → L3. We split on the
// event-driven TransitionFired specifically (initial-entry
// defaults emit TransitionFired{Internal} too).
let split = m
.journal()
.iter()
.position(|e| {
matches!(
e,
TraceEvent::TransitionFired {
reason: TransitionReason::Event { .. },
..
}
)
})
.unwrap();
let entered: Vec<u16> = m.journal()[split..]
.iter()
.filter_map(|e| match e {
TraceEvent::Entered { state } => Some(*state),
_ => None,
})
.collect();
assert_eq!(entered, vec![ST, SL1, SL2, SL3]);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn det_descent_termination_exits_full_chain() {
// After full descent into L3, terminate exits L3 → L2 → L1 → Tower → Root.
tokio::task::LocalSet::new()
.run_until(async {
let mut m = Desc::new(Ctx);
let _ = m.dispatch(Ev::Go).await;
let _ = m.dispatch(Ev::Halt).await;
let exits: Vec<u16> = m
.journal()
.iter()
.filter_map(|e| match e {
TraceEvent::Exited { state } => Some(*state),
_ => None,
})
.collect();
assert_eq!(exits, vec![SI, SL3, SL2, SL1, ST, SR]);
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn det_descent_action_kind_sequence() {
// After GO: Tower entry kind, L1 entry, L2 entry, L3 entry. All Entry.
tokio::task::LocalSet::new()
.run_until(async {
let mut m = Desc::new(Ctx);
let _ = m.dispatch(Ev::Go).await;
// Split on the event-driven TransitionFired (the initial
// default-fire is also TransitionFired{Internal} but happens
// before GO).
let split = m
.journal()
.iter()
.position(|e| {
matches!(
e,
TraceEvent::TransitionFired {
reason: TransitionReason::Event { .. },
..
}
)
})
.unwrap();
// After the Idle exit, all subsequent ActionInvoked must be Entry kind.
let kinds_after_exit: Vec<ActionKind> = m.journal()[split + 3..]
.iter()
.filter_map(|e| match e {
TraceEvent::ActionInvoked { kind, .. } => Some(*kind),
_ => None,
})
.collect();
assert!(
kinds_after_exit.iter().all(|k| *k == ActionKind::Entry),
"all post-transition actions must be Entry kind, got {:?}",
kinds_after_exit
);
assert_eq!(kinds_after_exit.len(), 4, "Tower + L1 + L2 + L3 entries");
})
.await;
}
#[tokio::test(flavor = "current_thread")]
async fn det_descent_byte_deterministic() {
let run = || async {
tokio::task::LocalSet::new()
.run_until(async {
let mut m = Desc::new(Ctx);
let _ = m.dispatch(Ev::Go).await;
let _ = m.dispatch(Ev::Halt).await;
m.take_journal()
})
.await
};
let first = run().await;
for i in 1..10 {
assert_eq!(first, run().await, "diverged on run {i}");
}
}
#[tokio::test(flavor = "current_thread")]
async fn det_descent_initial_descent_is_idle_only() {
// Initial default chain is Root → Idle. Idle has no children, so only one Entered.
tokio::task::LocalSet::new()
.run_until(async {
let mut m = Desc::new(Ctx);
let _ = m.dispatch(Ev::Halt).await;
let entered: Vec<u16> = m
.journal()
.iter()
.filter_map(|e| match e {
TraceEvent::Entered { state } => Some(*state),
_ => None,
})
.collect();
assert_eq!(entered, vec![SR, SI]);
})
.await;
}