diff --git a/services/flysim/crates/fly-session/src/coordinator.rs b/services/flysim/crates/fly-session/src/coordinator.rs index 7e6189f..9d54739 100644 --- a/services/flysim/crates/fly-session/src/coordinator.rs +++ b/services/flysim/crates/fly-session/src/coordinator.rs @@ -336,6 +336,9 @@ pub struct Coordinator { /// Set by whoever asks for a normal pause, possibly while a transition is in flight. pause: std::sync::Arc, episode: Option, + /// The rollback policy the composition declares, if any (`legacy-ratchet-rollback-v1` is + /// the only one defined). Without one, a task's rollback request is a task failure. + rollback_policy: Option, lifecycle_acks: Vec<(WorkerRef, DomainRequestId)>, stats: Stats, /// The ordered actions this session took, for the ordering assertions. @@ -431,6 +434,7 @@ impl Coordinator { pacing: None, pause: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)), episode: None, + rollback_policy: None, lifecycle_acks: Vec::new(), stats: Stats::default(), audit: Vec::new(), @@ -526,6 +530,22 @@ impl Coordinator { self.timelines.positions() } + /// Declares the composition's rollback policy (`workers-v1` section 4 and `step-v1` section 6, + /// both amended 2026-09-23). Only `legacy-ratchet-rollback-v1` is defined. Until the rollback + /// sequence is wired (TASK-01), a declared rollback request pauses the session at its boundary + /// like a terminal one, which is safe; an undeclared one fails the epoch. + pub fn declare_rollback_policy(&mut self, policy: &str) -> Result<(), DomainError> { + if policy != fly_session_types::extensions::ROLLBACK_POLICY { + return Err(DomainError::invalid(format!("{policy} is not a defined rollback policy"))); + } + self.rollback_policy = Some(id(policy)); + Ok(()) + } + + pub fn rollback_policy(&self) -> Option<&Id> { + self.rollback_policy.as_ref() + } + pub fn episode_request(&self) -> Option<&EpisodeRequest> { self.episode.as_ref() } @@ -1676,6 +1696,22 @@ impl Coordinator { self.fail_now(e, "task-evaluate") })?; self.audit.push(format!("evaluate:{k}")); + if let Some(request) = &evaluation.episode + && request.kind == EpisodeRequestKind::Rollback + && self.rollback_policy.is_none() + { + // `workers-v1` section 4 (2026-09-23): a composition that declares no rollback policy + // treats the request as a task failure. The brains and the world already moved, so + // the epoch fails rather than committing a transition whose outcome it cannot apply. + return Err(self.fail_now( + DomainError::new( + ErrorCode::Unsupported, + "the task asked for a rollback and this composition declares no rollback policy", + MutationCertainty::Applied, + ), + "task-evaluate", + )); + } let mut outcomes = evaluation.outcomes.clone(); let mut next_contexts = evaluation.next_contexts.clone(); for agent in self.agent_ids() { diff --git a/services/flysim/crates/fly-session/src/task.rs b/services/flysim/crates/fly-session/src/task.rs index db8d8cd..bbb2bf4 100644 --- a/services/flysim/crates/fly-session/src/task.rs +++ b/services/flysim/crates/fly-session/src/task.rs @@ -208,6 +208,10 @@ pub enum Terminal { Counter(i64), /// This many transitions were evaluated. AfterTransitions(u64), + /// After this many transitions, ask for a *rollback* instead: the request a composition with + /// a declared rollback policy may make, and one without it must treat as a task failure + /// (`workers-v1` section 4, 2026-09-23). Only a test asks the counter task for this. + RollbackAfterTransitions(u64), } /// The deterministic counter task: rewards come from the arena counter each agent moved. @@ -404,15 +408,36 @@ impl Task for CounterTask { let terminal = match self.terminal { Terminal::Never => false, Terminal::Counter(target) => new >= target, - Terminal::AfterTransitions(n) => self.transitions >= n, + Terminal::AfterTransitions(n) | Terminal::RollbackAfterTransitions(n) => { + self.transitions >= n + } }; - // The synthetic task only ever asks for the end of the episode; `rollback` belongs to a - // composition that declares a rollback policy (workers-v1 section 4, 2026-09-23). - let episode = terminal.then(|| EpisodeRequest { - kind: EpisodeRequestKind::Terminal, - reason: id("counter-target"), - outcome: TypedValue::new(episode_schema(), json!({"counter": new, "transitions": self.transitions})) + let rollback = matches!(self.terminal, Terminal::RollbackAfterTransitions(_)); + // The synthetic task asks for the end of the episode; `rollback` belongs to a composition + // that declares a rollback policy (workers-v1 section 4, 2026-09-23), and the synthetic + // composition declares none, so asking for one is how a test meets the refusal. + let episode = terminal.then(|| { + if rollback { + EpisodeRequest { + kind: EpisodeRequestKind::Rollback, + reason: id("stall"), + outcome: fly_session_types::gameboy::RollbackRequest { + slot_id: "best".to_owned(), + trigger: "stall".to_owned(), + } + .to_typed(), + } + } else { + EpisodeRequest { + kind: EpisodeRequestKind::Terminal, + reason: id("counter-target"), + outcome: TypedValue::new( + episode_schema(), + json!({"counter": new, "transitions": self.transitions}), + ) .expect("a synthetic typed value fits the contract"), + } + } }); Ok(Evaluation { outcomes, diff --git a/services/flysim/crates/fly-session/tests/session.rs b/services/flysim/crates/fly-session/tests/session.rs index 48dc7d6..fcce346 100644 --- a/services/flysim/crates/fly-session/tests/session.rs +++ b/services/flysim/crates/fly-session/tests/session.rs @@ -24,6 +24,8 @@ both_transports!( bootstrap_cannot_advance_the_world_or_produce_a_reward, the_committed_snapshot_names_the_boundary_that_just_ended, a_terminal_episode_pauses_at_its_own_boundary, + a_rollback_request_without_a_declared_policy_fails_the_epoch, + a_declared_rollback_policy_holds_the_session_at_its_boundary, status_answers_with_the_committed_boundary, a_worker_refuses_a_second_initialize, a_single_agent_composition_runs_the_same_transaction, @@ -308,6 +310,48 @@ async fn a_terminal_episode_pauses_at_its_own_boundary(via: Via) { f.shutdown().await; } +/// `workers-v1` section 4 (2026-09-23): a composition that declares no rollback policy treats a +/// rollback request as a task failure. The transition does not commit and the epoch is fenced. +async fn a_rollback_request_without_a_declared_policy_fails_the_epoch(via: Via) { + let config = HarnessConfig { + terminal: fly_session::task::Terminal::RollbackAfterTransitions(2), + ..HarnessConfig::default() + }; + let mut f = fixture(via, config).await; + within("bootstrap", f.harness.coordinator.bootstrap()).await.unwrap(); + assert!(f.harness.coordinator.rollback_policy().is_none()); + within("step 1", f.harness.coordinator.step()).await.unwrap(); + let err = within("step 2", f.harness.coordinator.step()).await.expect_err("refused"); + assert_eq!(err.error.code, ErrorCode::Unsupported); + assert!(err.error.message.contains("no rollback policy"), "{}", err.error.message); + assert!(f.harness.coordinator.is_fenced()); + assert_eq!(f.harness.coordinator.stats().commits, 2, "two agents committed transition 1; transition 2 committed nothing"); + f.shutdown().await; +} + +/// With `legacy-ratchet-rollback-v1` declared the request is accepted: the transition commits and +/// the session holds at its boundary with the request recorded, for the policy to run. +async fn a_declared_rollback_policy_holds_the_session_at_its_boundary(via: Via) { + let config = HarnessConfig { + terminal: fly_session::task::Terminal::RollbackAfterTransitions(2), + ..HarnessConfig::default() + }; + let mut f = fixture(via, config).await; + f.harness + .coordinator + .declare_rollback_policy("legacy-ratchet-rollback-v1") + .unwrap(); + assert!(f.harness.coordinator.declare_rollback_policy("reset-everything").is_err()); + within("bootstrap", f.harness.coordinator.bootstrap()).await.unwrap(); + let reports = within("run", f.harness.coordinator.run(4)).await.unwrap(); + assert_eq!(reports.len(), 2); + assert!(!f.harness.coordinator.is_fenced()); + assert_eq!(f.harness.coordinator.phase(), Phase::Paused(2)); + let request = f.harness.coordinator.episode_request().unwrap(); + assert_eq!(request.kind, EpisodeRequestKind::Rollback); + f.shutdown().await; +} + /// `Worker.Status` answers with the worker's own committed boundary and progress. async fn status_answers_with_the_committed_boundary(via: Via) { let mut f = default_fixture(via).await;