diff --git a/crates/thing-core/src/command/lifecycle.rs b/crates/thing-core/src/command/lifecycle.rs index b09d4fe..1cf8ad2 100644 --- a/crates/thing-core/src/command/lifecycle.rs +++ b/crates/thing-core/src/command/lifecycle.rs @@ -40,6 +40,7 @@ impl Commands { if address.schema.as_str() != CommandResult::SCHEMA_NAME { return answered; } + self.expire(now); let Some(at) = self.live.iter().position(|live| live.interest.matches(address)) else { if !matches!(change, Change::Delete(_)) { answered.diagnostics.push(fault( @@ -65,16 +66,16 @@ impl Commands { // One equality catches a body naming another invocation, and a seq that is not the key's. Record::Result(result) if result.record_key() == address.key.as_str() => result, Record::Result(_) => { - self.malformed(at, "the result is not the one its key names", &mut answered, port, ledger); + self.malformed(at, "the result is not the one its key names", now, &mut answered, port, ledger); return answered; } _ => { - self.malformed(at, "the record at a result address is not a result", &mut answered, port, ledger); + self.malformed(at, "the record at a result address is not a result", now, &mut answered, port, ledger); return answered; } }, _ => { - self.malformed(at, "the result was refused at decode", &mut answered, port, ledger); + self.malformed(at, "the result was refused at decode", now, &mut answered, port, ledger); return answered; } }; @@ -94,7 +95,7 @@ impl Commands { Some(effect) => match validate::effect(effect, trees(&surface), &self.live[at]) { Ok(effect) => Some(effect), Err(detail) => { - self.malformed(at, &detail, &mut answered, port, ledger); + self.malformed(at, &detail, now, &mut answered, port, ledger); return answered; } }, @@ -103,7 +104,7 @@ impl Commands { drafts.clear_held(&surface, &self.live[at].held); } answered.effects.extend(effect); - self.settle(at, State::Completed, None, port, ledger); + self.settle(at, State::Completed, None, now, port, ledger); } Outcome::Denied(denied) => { let mut target = issuing.clone(); @@ -119,13 +120,13 @@ impl Commands { )), } } - self.settle(at, State::Denied, None, port, ledger); + self.settle(at, State::Denied, None, now, port, ledger); if let Some(node) = target { self.marks.insert((surface, node), Mark::Denied { reason: denied.reason.clone() }); } } Outcome::Failed(failed) => { - self.settle(at, State::Failed, Some(Mark::Failed { reason: failed.reason.clone() }), port, ledger); + self.settle(at, State::Failed, Some(Mark::Failed { reason: failed.reason.clone() }), now, port, ledger); } } answered @@ -134,12 +135,13 @@ impl Commands { /// Move every non-terminal invocation whose deadline has come to `TimedOut`. A deadline is /// compared with the time given: a wall clock that went backward delays a timeout, never fires one early. pub fn tick(&mut self, now: Timestamp, port: &mut dyn StatePort, ledger: &mut Ledger) -> Vec { + self.expire(now); let mut diagnostics = Vec::new(); for at in 0..self.live.len() { let live = &self.live[at]; if !live.state.is_terminal() && live.deadline <= now { diagnostics.push(fault(&live.surface, live.issuing.as_ref(), Code::InvocationTimedOut, "no result by the deadline")); - self.settle(at, State::TimedOut, Some(Mark::TimedOut), port, ledger); + self.settle(at, State::TimedOut, Some(Mark::TimedOut), now, port, ledger); } } diagnostics @@ -151,17 +153,26 @@ impl Commands { } /// A result core will not take: the invocation is `Malformed`, and nothing it carried runs. - fn malformed(&mut self, at: usize, detail: &str, answered: &mut Answered, port: &mut dyn StatePort, ledger: &mut Ledger) { + fn malformed(&mut self, at: usize, detail: &str, now: Timestamp, answered: &mut Answered, port: &mut dyn StatePort, ledger: &mut Ledger) { let live = &self.live[at]; answered.diagnostics.push(fault(&live.surface, live.issuing.as_ref(), Code::MalformedResult, detail)); - self.settle(at, State::Malformed, Some(Mark::Malformed), port, ledger); + self.settle(at, State::Malformed, Some(Mark::Malformed), now, port, ledger); + } + + /// Forget the invocations that went terminal a whole timeout ago: a result after that is for an + /// invocation core no longer holds, so the table stays bounded by the work of one timeout. A + /// result inside the window is still judged against the invocation (`ResultAfterTerminal`). + fn expire(&mut self, now: Timestamp) { + let timeout = self.timeout_ms; + self.live.retain(|live| live.done.is_none_or(|done| now < done.plus_ms(timeout))); } /// Enter a terminal state: the interest in results is released in the same call, and the /// issuing node shows the mark. - fn settle(&mut self, at: usize, state: State, mark: Option, port: &mut dyn StatePort, ledger: &mut Ledger) { + fn settle(&mut self, at: usize, state: State, mark: Option, now: Timestamp, port: &mut dyn StatePort, ledger: &mut Ledger) { let live = &mut self.live[at]; live.state = state; + live.done = Some(now); ledger.release(port, &live.interest); if let (Some(mark), Some(node)) = (mark, &live.issuing) { self.marks.insert((live.surface.clone(), node.clone()), mark); diff --git a/crates/thing-core/src/command/mod.rs b/crates/thing-core/src/command/mod.rs index 3318a4e..0777e8d 100644 --- a/crates/thing-core/src/command/mod.rs +++ b/crates/thing-core/src/command/mod.rs @@ -139,6 +139,8 @@ struct Live { /// The highest result `seq` taken. highest: i32, deadline: Timestamp, + /// When it went terminal; `None` while it waits. + done: Option, held: Vec<(NodeId, InputValue)>, scope: Vec, interest: Interest, @@ -445,6 +447,7 @@ impl Commands { state: State::Issued, highest: 0, deadline: now.plus_ms(self.timeout_ms), + done: None, held: prepared.held, scope: prepared.scope, interest, diff --git a/crates/thing-core/tests/command.rs b/crates/thing-core/tests/command.rs index e2c7f2e..36855b0 100644 --- a/crates/thing-core/tests/command.rs +++ b/crates/thing-core/tests/command.rs @@ -1385,3 +1385,41 @@ fn a_held_resume_for_a_surface_left_before_it_bound_is_dropped_and_said() { assert!(back.outbound.is_empty() && back.diagnostics.is_empty()); } +// ----- terminal invocations are kept as long as a late result could matter ----- + +#[test] +fn a_terminal_invocation_is_kept_for_one_timeout_and_then_forgotten() { + // Kept: a result one millisecond short of the timeout after it went terminal is judged against it. + let mut rig = Rig::new(MemState::new(), &go_surface()); + let invocation = rig.mint("go", PINNED_NOW); + rig.feed(&completed(&invocation, 1, false, None), at(100)); + let kept = rig.feed_direct(&completed(&invocation, 2, false, None), at(100 + 15_000 - 1)); + assert_eq!(codes(&kept.diagnostics), vec![Code::ResultAfterTerminal]); + + // Forgotten: at the timeout the result is for an invocation core does not hold. + let mut rig = Rig::new(MemState::new(), &go_surface()); + let invocation = rig.mint("go", PINNED_NOW); + rig.feed(&completed(&invocation, 1, false, None), at(100)); + let gone = rig.feed_direct(&completed(&invocation, 2, false, None), at(100 + 15_000)); + assert_eq!(codes(&gone.diagnostics), vec![Code::UnknownInvocation]); + + // A timeout is terminal too, and counts from the moment the deadline was taken. + let mut rig = Rig::new(MemState::new(), &go_surface()); + let invocation = rig.mint("go", PINNED_NOW); + assert_eq!(codes(&rig.tick(at(15_000))), vec![Code::InvocationTimedOut]); + let late = rig.feed_direct(&completed(&invocation, 1, false, None), at(29_999)); + assert_eq!(codes(&late.diagnostics), vec![Code::ResultAfterTerminal]); + let later = rig.feed_direct(&completed(&invocation, 1, false, None), at(30_000)); + assert_eq!(codes(&later.diagnostics), vec![Code::UnknownInvocation]); +} + +#[test] +fn a_tick_also_forgets_the_terminal_invocations_past_their_time() { + let mut rig = Rig::new(MemState::new(), &go_surface()); + let invocation = rig.mint("go", PINNED_NOW); + rig.feed(&completed(&invocation, 1, false, None), at(100)); + assert!(rig.tick(at(100 + 15_000)).is_empty()); + let gone = rig.feed_direct(&completed(&invocation, 2, false, None), at(100 + 15_000 - 1)); + assert_eq!(codes(&gone.diagnostics), vec![Code::UnknownInvocation]); +} +