Thing 2.0 cut 7a h2: a resume before its surface binds is held, not lost (resume-silent-before-bind)

Steward decision 4 of the h2 fix brief. Core holds the Resume and runs it in
the frame that first binds its surface, with that frame's now and ids, so it
follows the normal lifecycle and timeout; leaving for another surface first
drops it with an ActivationRefused diagnostic.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-29 00:49:51 +01:00
commit 33281e2a94
2 changed files with 65 additions and 1 deletions

View file

@ -83,6 +83,9 @@ pub struct Core {
drafts: Drafts,
/// What activations said since the last frame; the frame's diagnostics carry it out.
notes: Vec<Diagnostic>,
/// Resumes whose surface had not bound when the host handed them over; each runs at that surface's
/// first bind, or is dropped when the surface is left first.
held: Vec<Resume>,
/// The surface `show` named, and the one whose interest and trees are held.
wanted: Option<Address>,
shown: Option<Address>,
@ -103,6 +106,7 @@ impl Core {
commands: Commands::new(config.client, config.invocation_timeout_ms),
drafts: Drafts::new(),
notes: Vec::new(),
held: Vec::new(),
wanted: None,
shown: None,
last: None,
@ -152,6 +156,15 @@ impl Core {
if rebound {
self.retain_drafts();
}
for resume in std::mem::take(&mut self.held) {
match self.binder.tree(&resume.surface_key) {
Some(tree) => {
let said = self.commands.resume(&resume, tree, &self.drafts, now, ports.ids, port, &mut self.ledger);
diagnostics.extend(said);
}
None => self.held.push(resume),
}
}
diagnostics.extend(self.commands.tick(now, port, &mut self.ledger));
let commands = self.draw(viewport, scale, &mut diagnostics);
@ -191,9 +204,11 @@ impl Core {
/// Run the command an `OpenExternal` effect named, once the external step has returned. The host
/// holds the `Resume` across the navigation and hands it back here; the command is minted, or asks
/// for confirmation, like an activation. A refusal is said in the next frame's `diagnostics`.
/// for confirmation, like an activation. A resume handed over before its surface has bound is held and runs at the first bind; one whose
/// surface is left first is dropped and said. Any other refusal is said in the next frame's `diagnostics`.
pub fn resume(&mut self, resume: &Resume, now: Timestamp, ports: Ports<'_>) {
let Some(tree) = self.binder.tree(&resume.surface_key) else {
self.held.push(resume.clone());
return;
};
let said = self.commands.resume(resume, tree, &self.drafts, now, ports.ids, ports.state, &mut self.ledger);
@ -219,6 +234,16 @@ impl Core {
/// Leave the surface shown and take the one wanted: the interest is held before the one read,
/// and what the read finds enters as a change like any republish.
fn switch(&mut self, port: &mut dyn StatePort, changes: &mut Vec<Change>) {
// A resume is for the surface it names: leaving for another one forgets it.
let wanted = self.wanted.as_ref().map(|address| address.key.as_str().to_string());
for resume in std::mem::take(&mut self.held) {
if wanted.as_deref() == Some(resume.surface_key.as_str()) {
self.held.push(resume);
} else {
let detail = "the surface was left before it bound; the resume is dropped";
self.notes.push(Diagnostic { code: Code::ActivationRefused, surface: resume.surface_key, node: Some(resume.source), detail: detail.into() });
}
}
if let Some(old) = self.shown.take() {
self.resolver.forget(old.key.as_str(), port, &mut self.ledger);
self.binder.forget(old.key.as_str(), port, &mut self.ledger);

View file

@ -1346,3 +1346,42 @@ fn a_resumed_command_that_asks_for_confirmation_waits_for_it() {
assert_eq!(rig.commands.take_outbound()[0].command, CommandId::new("after"));
}
#[test]
fn a_resume_before_its_surface_binds_is_held_and_runs_at_the_first_bind() {
let provider = ProviderId::new(PROVIDER);
let document = go_surface();
let mut state = MemState::new();
state.put(&provider, &document);
let mut core = core();
core.show(provider.clone(), RecordKey::new(document.record_key()));
let resume = Resume { surface_key: document.record_key(), command: CommandId::new("after"), source: nid("go"), items: vec![] };
core.resume(&resume, PINNED_NOW, Ports { state: &mut state, ids: &mut SeqIds::new() });
let first = frame_at(&mut core, &mut state, at(1_000));
assert!(first.diagnostics.is_empty());
assert_eq!(first.outbound.len(), 1);
assert_eq!(first.outbound[0].command, CommandId::new("after"));
// It follows the lifecycle from the bind: the timeout counts from the frame that ran it.
assert_eq!(first.next_wake, Some(at(16_000)));
// It ran once.
assert!(frame_at(&mut core, &mut state, at(1_001)).outbound.is_empty());
}
#[test]
fn a_held_resume_for_a_surface_left_before_it_bound_is_dropped_and_said() {
let provider = ProviderId::new(PROVIDER);
let document = go_surface();
let mut state = MemState::new();
state.put(&provider, &document);
let mut core = core();
core.show(provider.clone(), RecordKey::new(document.record_key()));
let resume = Resume { surface_key: document.record_key(), command: CommandId::new("after"), source: nid("go"), items: vec![] };
core.resume(&resume, PINNED_NOW, Ports { state: &mut state, ids: &mut SeqIds::new() });
core.show(provider.clone(), RecordKey::new("thing:surface:provider.cmd.elsewhere"));
let left = frame_at(&mut core, &mut state, at(1));
assert_eq!(codes(&left.diagnostics), vec![Code::ActivationRefused]);
assert!(left.outbound.is_empty());
core.show(provider, RecordKey::new(document.record_key()));
let back = frame_at(&mut core, &mut state, at(2));
assert!(back.outbound.is_empty() && back.diagnostics.is_empty());
}