diff --git a/crates/ghostlight/src/controllers.rs b/crates/ghostlight/src/controllers.rs index 52f6541..3e120fd 100644 --- a/crates/ghostlight/src/controllers.rs +++ b/crates/ghostlight/src/controllers.rs @@ -19632,4 +19632,159 @@ mod tests { drop(mailbox); task.await.unwrap(); } + + /// The same rows as the store sees them while its custody is uncertain. + struct UncertainStore(Arc); + + #[async_trait] + impl ControllerWorkStore for UncertainStore { + async fn lookup( + &self, + command_id: CommandId, + ) -> Result { + Ok(match self.0.lookup(command_id).await? { + ControllerWorkLookup::Confirmed(work) => ControllerWorkLookup::CustodyUncertain(work), + other => other, + }) + } + + async fn persist( + &self, + work: &ControllerWork, + ) -> Result { + self.0.persist(work).await + } + + async fn custody_probe(&self) -> Result { + self.0.custody_probe().await + } + + async fn elaboration_in_flight( + &self, + ) -> Result, ControllerWorkStoreError> { + self.0.elaboration_in_flight().await + } + } + + fn uncertain_runner(mailbox: &WorldMailbox, store: &Arc) -> ControllerRunner { + let port = Arc::new(RecordingPort { + outputs: Mutex::new(Vec::new()), + persisted_before_interpreter: Arc::new(AtomicBool::new(true)), + }); + ControllerRunner::open(mailbox.clone(), port, Arc::new(UncertainStore(store.clone())), models()) + .expect("the fixture ports open") + } + + #[tokio::test] + async fn a_row_read_in_uncertain_custody_is_reported_pending_for_its_own_decision_only() { + let (_directory, mailbox, task) = active_cell_mailbox(vec![ + NewController::OperationalAgent, + NewController::OperationalAgent, + NewController::NarrativePersona, + NewController::NarrativePersona, + ]) + .await; + let snapshot = mailbox.snapshot().await.unwrap(); + let by_mode = |mode: ControllerMode| { + snapshot + .opportunities + .iter() + .filter(|opportunity| opportunity.controller_mode == mode) + .cloned() + .collect::>() + }; + let operational = by_mode(ControllerMode::OperationalAgent); + let narrative = by_mode(ControllerMode::NarrativePersona); + let mut outputs = vec![operational_speaks("The hinge is flooding.", "operational")]; + outputs.extend(narrative_turn_outputs("Mara", "The rain has teeth tonight.")); + let (runner, store) = grouped_runner(&mailbox, outputs); + let operational_command = CommandId::new(); + let narrative_command = CommandId::new(); + runner + .run_operational(operational_command, &operational[0]) + .await + .unwrap(); + runner + .run_narrative(narrative_command, &narrative[0]) + .await + .unwrap(); + + let uncertain = uncertain_runner(&mailbox, &store); + let moved = mailbox.snapshot().await.unwrap(); + let operational_now = fresh_opportunity(&moved, operational[0].scope.subject_id); + let narrative_now = fresh_opportunity(&moved, narrative[0].scope.subject_id); + assert_ne!(operational_now.scope_digest, operational[0].scope_digest); + assert_ne!(narrative_now.scope_digest, narrative[0].scope_digest); + let OperationalRun::Pending(pending) = uncertain + .run_operational(operational_command, &operational_now) + .await + .unwrap() + else { + panic!("an operational row in uncertain custody was not reported pending") + }; + assert_eq!(pending.reason(), ControllerPendingReason::StoreReopenRequired); + let NarrativeRun::Pending(pending) = uncertain + .run_narrative(narrative_command, &narrative_now) + .await + .unwrap() + else { + panic!("a narrative row in uncertain custody was not reported pending") + }; + assert_eq!(pending.reason(), ControllerPendingReason::StoreReopenRequired); + + let other_operational = fresh_opportunity(&moved, operational[1].scope.subject_id); + let other_narrative = fresh_opportunity(&moved, narrative[1].scope.subject_id); + assert!(matches!( + uncertain + .run_operational(operational_command, &other_operational) + .await, + Err(ControllerError::OpportunityMismatch) + )); + assert!(matches!( + uncertain + .run_narrative(narrative_command, &other_narrative) + .await, + Err(ControllerError::OpportunityMismatch) + )); + + drop((runner, uncertain)); + drop(mailbox); + task.await.unwrap(); + } + + #[tokio::test] + async fn a_narrative_row_in_flight_whose_scope_moved_is_refused_before_inference() { + let (_directory, mailbox, task) = active_cell_mailbox(vec![ + NewController::NarrativePersona, + NewController::NarrativePersona, + ]) + .await; + // The first turn's projector call is pending; the second subject's turn + // commits; a further call would panic the port. + let mut outputs = vec![Err(InferenceFault::retryable("the provider is busy"))]; + outputs.extend(narrative_turn_outputs("Tobin", "Then we close the span.")); + let (runner, _store) = grouped_runner(&mailbox, outputs); + let snapshot = mailbox.snapshot().await.unwrap(); + let first = snapshot.opportunities[0].clone(); + let second = snapshot.opportunities[1].clone(); + let command = CommandId::new(); + assert!(matches!( + runner.run_narrative(command, &first).await.unwrap(), + NarrativeRun::Pending(_) + )); + assert!(matches!( + runner.run_narrative(CommandId::new(), &second).await.unwrap(), + NarrativeRun::Completed(_) + )); + let moved = mailbox.snapshot().await.unwrap(); + let fresh = fresh_opportunity(&moved, first.scope.subject_id); + assert_ne!(fresh.scope_digest, first.scope_digest, "the scope did not move"); + let error = runner.run_narrative(command, &fresh).await.unwrap_err(); + assert!(matches!(error, ControllerError::OpportunityMismatch)); + assert_eq!(mailbox.operator_log().await.unwrap().len(), 1); + + drop(runner); + drop(mailbox); + task.await.unwrap(); + } } diff --git a/crates/ghostlight/src/verse/run/tests.rs b/crates/ghostlight/src/verse/run/tests.rs index 6ce9a8e..8c1ea3b 100644 --- a/crates/ghostlight/src/verse/run/tests.rs +++ b/crates/ghostlight/src/verse/run/tests.rs @@ -492,64 +492,71 @@ async fn budget_exhausted_stops_before_the_clock_and_resumes() { assert_eq!(resumed.record.checkpoints[1].committed.len(), 2); } -/// The answer for a committed cell does not depend on where it sits in the -/// tick's frozen order. Here the cell that acted is the second one: the first -/// is still in flight when the run ends, the second commits by another driver, -/// and the resuming run reaches the committed cell last. The factions share a -/// room, so the committed act moved the in-flight cell's scope: that cell is -/// refused before inference (its row still reasons), while the committed cell is -/// answered with its receipt, whatever its own scope has done since. -#[tokio::test] -async fn a_committed_cell_is_answered_whichever_cell_the_resume_reaches_first() { +/// Both cells of a tick committed through another driver before any run opened +/// it, one after the other, so each one's act has since moved the other's +/// scope. The run that opens the tick reaches both committed rows, in whatever +/// order it froze, and answers each with its receipt: no inference, no refusal, +/// no second act. `reverse` flips which cell the other driver committed first, +/// so the answer is shown not to depend on the order the cells are reached in. +async fn resumed_after_both_cells_committed_elsewhere(reverse: bool) { let (_dir, verse) = Harness::fresh().await; - let ended = verse - .run_quietly_under(fault_at(0), config_of(1, 60, 2)) - .await; - assert_eq!( - ended.record.status, - RunStatus::Failed { - reason: "inference_retryable".into() - } + let snapshot = verse.mailbox.snapshot().await.unwrap(); + let graph = verse.mailbox.agency_graph().await.unwrap(); + let cover = derive_cover( + snapshot.world_id, + TickIndex(0), + &snapshot.opportunities, + &graph, + CoverBudget { + cells: 2, + constituent_cap: 8, + urgency_slots: 0, + }, ); - assert_eq!(verse.acts().await, 0, "the first cell was left in flight"); - let world = verse.mailbox.snapshot().await.unwrap().world_id; - let tick = verse.runs.tick(world, 0).unwrap().unwrap(); - assert_eq!(tick.cells.len(), 2); - - // The second frozen cell commits on its own. - let other = ScriptedPort::speaking(); + assert_eq!(cover.cells.len(), 2); + let elsewhere = ScriptedPort::speaking(); let runner = ControllerRunner::open( verse.mailbox.clone(), - other.clone(), + elsewhere.clone(), verse.work.clone(), models(), ) .unwrap(); - let fresh = verse.mailbox.opportunities().await.unwrap(); - let second = refreshed(&tick.cells[1], TickIndex(0), &fresh).unwrap(); - runner.run_cell(&second).await.unwrap(); - assert_eq!(verse.acts().await, 1); + let order: Vec<&Cell> = if reverse { + cover.cells.iter().rev().collect() + } else { + cover.cells.iter().collect() + }; + for cell in order { + let fresh = verse.mailbox.opportunities().await.unwrap(); + let bound = refreshed(&FrozenCell::of(cell), TickIndex(0), &fresh).unwrap(); + runner.run_cell(&bound).await.unwrap(); + } + assert_eq!(verse.acts().await, 2); let resuming = ScriptedPort::speaking(); - let resumed = verse + let ran = verse .run_quietly_under(resuming.clone(), config_of(1, 60, 2)) .await; - assert_eq!(resumed.record.status, RunStatus::Completed); - let [closing] = resumed.record.checkpoints.as_slice() else { - panic!("the resuming run closed one tick") + assert_eq!(ran.record.status, RunStatus::Completed); + let [closing] = ran.record.checkpoints.as_slice() else { + panic!("the run closed one tick") }; assert!(closing.closed); - assert_eq!( - closing.refusals, - vec![Refusal { - cell: 0, - reason: RefusalReason::ScopeMoved - }], - "{closing:?}" - ); - assert_eq!(closing.committed.len(), 1, "{closing:?}"); - assert_eq!(resuming.calls(), 0, "no provider call was spent on a moved scope"); - assert_eq!(verse.acts().await, 1, "no act was committed twice"); + assert!(closing.refusals.is_empty(), "{closing:?}"); + assert_eq!(closing.committed.len(), 2, "{closing:?}"); + assert_eq!(resuming.calls(), 0, "a committed cell was asked again"); + assert_eq!(verse.acts().await, 2, "no act was committed twice"); +} + +#[tokio::test] +async fn committed_cells_are_answered_in_the_order_the_tick_froze() { + resumed_after_both_cells_committed_elsewhere(false).await; +} + +#[tokio::test] +async fn committed_cells_are_answered_against_the_order_the_tick_froze() { + resumed_after_both_cells_committed_elsewhere(true).await; } #[tokio::test]