From 097bf02f1e9514aa40d47c7226014421f6a4ef9e Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 11 Oct 2026 09:08:02 +0200 Subject: [PATCH] Verse run: the open tick is recorded unclosed, and a resumed cell whose scope moved is its own refusal A run that ends inside a tick (budget, pending cell, failed clock) now keeps that tick's checkpoint with closed false, so what its cells committed is not lost. A cell that committed before an interruption meets a moved scope digest when the tick resumes and is refused as ScopeMoved; nothing commits twice. Removes the temporary diagnostic. Builds; not yet re-verified. Co-Authored-By: Claude Opus 5.5 --- crates/ghostlight-verse/src/main.rs | 2 +- crates/ghostlight/src/verse/run.rs | 42 +++++++--- crates/ghostlight/src/verse/run/tests.rs | 98 +++++++++++++++--------- 3 files changed, 94 insertions(+), 48 deletions(-) diff --git a/crates/ghostlight-verse/src/main.rs b/crates/ghostlight-verse/src/main.rs index 3bc026e..52f0c50 100644 --- a/crates/ghostlight-verse/src/main.rs +++ b/crates/ghostlight-verse/src/main.rs @@ -240,7 +240,7 @@ async fn run(flags: Flags) -> Result { println!( "status: {}; {} of {} ticks closed", outcome.record.status, - outcome.record.checkpoints.len(), + outcome.record.ticks_closed(), outcome.record.ticks_requested ); drop(runner); diff --git a/crates/ghostlight/src/verse/run.rs b/crates/ghostlight/src/verse/run.rs index 47c6dec..3dc83dd 100644 --- a/crates/ghostlight/src/verse/run.rs +++ b/crates/ghostlight/src/verse/run.rs @@ -80,8 +80,14 @@ pub enum RefusalReason { Interrupted, /// The world refused the cell's submission. WorldRejected, - /// The cell's subject no longer holds the opportunity it was derived for. + /// The cell's subject no longer holds an opportunity. OpportunityGone, + /// The cell's subject holds an opportunity whose scope moved since the + /// cell's work was bound. A cell that committed earlier in an interrupted + /// tick ends this way when a run resumes the tick: its own act, or a + /// neighbour's in the shared room, changed what it knows. Nothing is + /// committed twice. + ScopeMoved, } #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -91,10 +97,14 @@ pub struct Refusal { pub reason: RefusalReason, } -/// One finished tick. The clock has advanced by the run's span. +/// A tick as the run left it. A closed tick has all its cells run and the +/// clock advanced by the run's span. The tick a run ended inside is recorded +/// too, unclosed, so what its cells committed is not lost; a later run derives +/// that tick again from the world's clock. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct TickCheckpoint { pub tick: u64, + pub closed: bool, pub cells: u32, /// Every command the tick's cells hold a receipt for, in cell order. A /// decline is a commit, so it is listed. @@ -128,6 +138,13 @@ pub struct VerseRunRecord { pub status: RunStatus, } +impl VerseRunRecord { + /// The ticks this run finished: every cell run and the clock advanced. + pub fn ticks_closed(&self) -> usize { + self.checkpoints.iter().filter(|c| c.closed).count() + } +} + /// What a caller asks of one run. The token cap is not here: the recording /// port owns it. #[derive(Clone, Copy, Debug)] @@ -137,8 +154,7 @@ pub struct VerseRunConfig { pub cover: CoverBudget, } -/// A tick as the operator reads it. `closed` is false for the tick a run ended -/// inside, whose cells may have committed before the run stopped. +/// A tick as the operator reads it, including the one a run ended inside. #[derive(Clone, Debug)] pub struct TickReport { pub checkpoint: TickCheckpoint, @@ -146,7 +162,6 @@ pub struct TickReport { pub acts: Vec, pub spent: u64, pub remaining: u64, - pub closed: bool, } impl std::fmt::Display for RunStatus { @@ -171,7 +186,7 @@ impl std::fmt::Display for TickReport { f, "tick {} {}: {} cells, {} acts, {} refused", checkpoint.tick, - if self.closed { "closed" } else { "open" }, + if checkpoint.closed { "closed" } else { "open" }, checkpoint.cells, self.acts.len(), checkpoint.refusals.len() @@ -199,6 +214,7 @@ impl std::fmt::Display for TickReport { RefusalReason::Interrupted => "interrupted", RefusalReason::WorldRejected => "world_rejected", RefusalReason::OpportunityGone => "opportunity_gone", + RefusalReason::ScopeMoved => "scope_moved", }; writeln!(f, " refused: cell {} {reason}", refusal.cell)?; } @@ -322,9 +338,10 @@ fn classify(error: &ControllerError) -> CellEnd { } _ if error.requires_quarantine() => CellEnd::Fatal("controller_quarantined"), ControllerError::World(_) => CellEnd::Refused(RefusalReason::WorldRejected), - ControllerError::NoOpportunity { .. } - | ControllerError::AmbiguousOpportunity - | ControllerError::OpportunityMismatch => CellEnd::Refused(RefusalReason::OpportunityGone), + ControllerError::NoOpportunity { .. } | ControllerError::AmbiguousOpportunity => { + CellEnd::Refused(RefusalReason::OpportunityGone) + } + ControllerError::OpportunityMismatch => CellEnd::Refused(RefusalReason::ScopeMoved), _ => CellEnd::Fatal("controller_error"), } } @@ -485,9 +502,12 @@ pub async fn run_manual( match end { TickEnd::Open(status) => { record.status = status; - store.save(&record)?; if let Some(report) = report { + record.checkpoints.push(report.checkpoint.clone()); + store.save(&record)?; let _ = observe(&report); + } else { + store.save(&record)?; } return Ok(RunOutcome { record }); } @@ -602,6 +622,7 @@ async fn run_tick( let report = TickReport { checkpoint: TickCheckpoint { tick: cover.tick.0, + closed: open.is_none(), cells: cover.cells.len() as u32, committed, refusals, @@ -618,7 +639,6 @@ async fn run_tick( .unwrap_or_default(), spent, remaining: port.cap().saturating_sub(spent), - closed: open.is_none(), }; (open.map_or(TickEnd::Closed, TickEnd::Open), Some(report)) } diff --git a/crates/ghostlight/src/verse/run/tests.rs b/crates/ghostlight/src/verse/run/tests.rs index cdddfcf..8b98e88 100644 --- a/crates/ghostlight/src/verse/run/tests.rs +++ b/crates/ghostlight/src/verse/run/tests.rs @@ -294,6 +294,7 @@ async fn scripted_run_commits_and_checkpoints_every_tick() { assert_eq!(outcome.record.status, RunStatus::Completed); let ticks: Vec = outcome.record.checkpoints.iter().map(|c| c.tick).collect(); assert_eq!(ticks, vec![0, 1, 2]); + assert_eq!(outcome.record.ticks_closed(), 3); assert_eq!(verse.now().await, 3 * u64::from(SPAN)); // Both factions acted every tick, the second on a scope the first moved. assert_eq!(verse.acts().await, 6); @@ -323,6 +324,7 @@ async fn scripted_run_commits_and_checkpoints_every_tick() { assert_eq!(expected.len(), 2); assert_eq!(checkpoint.committed, expected); assert_eq!(checkpoint.cells, 2); + assert!(checkpoint.closed); assert!(checkpoint.refusals.is_empty()); } @@ -363,7 +365,7 @@ async fn scripted_run_commits_and_checkpoints_every_tick() { let mut spent = 0; for (tick, report) in reports.iter().enumerate() { spent += report.checkpoint.tokens; - assert!(report.closed); + assert!(report.checkpoint.closed); assert_eq!(report.acts.len(), 2); assert_eq!(report.spent, spent); assert_eq!(report.remaining, 10_000_000 - spent); @@ -405,15 +407,22 @@ async fn budget_exhausted_stops_before_the_clock_and_resumes() { }) .await; assert_eq!(ended.record.status, RunStatus::BudgetExhausted); - assert!(ended.record.checkpoints.is_empty()); + // The tick it ended inside is recorded, unclosed, with the act it committed. + let [open] = ended.record.checkpoints.as_slice() else { + panic!("the open tick is the run's one checkpoint") + }; + assert!(!open.closed); + assert_eq!(open.tick, 0); + assert_eq!(open.committed.len(), 1); + assert_eq!(ended.record.ticks_closed(), 0); assert_eq!(verse.now().await, 0, "the open tick's clock did not advance"); assert_eq!(first.calls(), 1, "the second call never reached the provider"); assert_eq!(verse.acts().await, 1); // The open tick is reported, unclosed, with what it committed. assert_eq!(reports.len(), 1); - assert!(!reports[0].closed); + assert!(!reports[0].checkpoint.closed); assert_eq!(reports[0].acts.len(), 1); - assert_eq!(reports[0].checkpoint.committed.len(), 1); + assert_eq!(&reports[0].checkpoint, open); assert!(reports[0].remaining < first_bound); assert_eq!( verse.runs.runs(world.world_id).unwrap(), @@ -431,14 +440,18 @@ async fn budget_exhausted_stops_before_the_clock_and_resumes() { assert_eq!(verse.now().await, 2 * u64::from(SPAN)); assert_eq!(second.calls(), 3, "one call finished tick 0, two ran tick 1"); assert_eq!(verse.acts().await, 4, "no act was committed twice"); - // The faction that had acted holds no opportunity until the clock moves, so - // the tick that was open is closed by the one cell left, a command the - // first run did not commit. + // Both factions still hold opportunities. The one the first run left in + // flight finishes; the one that already acted has had its scope moved by + // that act and by its neighbour's, so its cell is refused rather than run + // again. let closing = &resumed.record.checkpoints[0]; - eprintln!("DIAG first={:#?} resumed={:#?}", ended.record, resumed.record); - assert_eq!(closing.cells, 1); + assert!(closing.closed); + assert_eq!(closing.cells, 2); assert_eq!(closing.committed.len(), 1); - assert_ne!(closing.committed[0], reports[0].checkpoint.committed[0]); + assert_ne!(closing.committed[0], open.committed[0]); + assert_eq!(closing.refusals.len(), 1); + assert_eq!(closing.refusals[0].reason, RefusalReason::ScopeMoved); + assert_eq!(resumed.record.ticks_closed(), 2); assert_eq!(resumed.record.checkpoints[1].committed.len(), 2); } @@ -517,7 +530,11 @@ async fn a_pending_cell_ends_the_run_without_the_clock_and_a_later_run_finishes_ reason: "inference_retryable".into() } ); - assert!(ended.record.checkpoints.is_empty()); + assert_eq!(ended.record.ticks_closed(), 0); + let [open] = ended.record.checkpoints.as_slice() else { + panic!("the open tick is recorded") + }; + assert!(!open.closed && open.committed.is_empty()); assert_eq!(verse.now().await, 0); assert_eq!(verse.acts().await, 0); @@ -552,7 +569,7 @@ async fn a_provider_answer_against_the_tool_contract_ends_the_run_without_the_cl reason: "inference_recovery_required".into() } ); - assert!(outcome.record.checkpoints.is_empty()); + assert_eq!(outcome.record.ticks_closed(), 0); assert_eq!(verse.now().await, 0); } @@ -592,6 +609,7 @@ fn the_operator_reads_refusals_and_how_a_run_ended() { let report = TickReport { checkpoint: TickCheckpoint { tick: 4, + closed: false, cells: 3, committed: Vec::new(), refusals: vec![ @@ -607,22 +625,27 @@ fn the_operator_reads_refusals_and_how_a_run_ended() { cell: 0, reason: RefusalReason::Interrupted, }, + Refusal { + cell: 5, + reason: RefusalReason::ScopeMoved, + }, ], tokens: 7, }, acts: Vec::new(), spent: 40, remaining: 60, - closed: false, }; assert_eq!( report.to_string(), - "tick 4 open: 3 cells, 0 acts, 3 refused - refused: cell 1 world_rejected - refused: cell 2 opportunity_gone - refused: cell 0 interrupted - tokens: 7 this tick, 40 spent, 60 remaining -" + concat!( + "tick 4 open: 3 cells, 0 acts, 4 refused\n", + " refused: cell 1 world_rejected\n", + " refused: cell 2 opportunity_gone\n", + " refused: cell 0 interrupted\n", + " refused: cell 5 scope_moved\n", + " tokens: 7 this tick, 40 spent, 60 remaining\n", + ) ); let ended: Vec = [ RunStatus::Running, @@ -688,25 +711,25 @@ async fn an_act_names_its_verb_render_path_and_bound_roles() { }; let text = reports[0].to_string(); let checkpoint = &reports[0].checkpoint; - assert!(text.starts_with("tick 0 closed: 2 cells, 2 acts, 0 refused -"), "{text}"); - assert!(text.contains(&format!( - " - Adrasteia attack [{}] target=Brannoch says \"Adrasteia strikes.\" -", + assert!( + text.starts_with("tick 0 closed: 2 cells, 2 acts, 0 refused\n"), + "{text}" + ); + let attack_line = format!( + "\n Adrasteia attack [{}] target=Brannoch says \"Adrasteia strikes.\"\n", name("attack") - )), "{text}"); - assert!(text.contains(&format!( - " - Brannoch speak [{}] says \"Brannoch answers.\" -", + ); + assert!(text.contains(&attack_line), "{text}"); + let speak_line = format!( + "\n Brannoch speak [{}] says \"Brannoch answers.\"\n", name("speak") - )), "{text}"); - assert!(text.ends_with(&format!( - " tokens: {} this tick, {} spent, {} remaining -", + ); + assert!(text.contains(&speak_line), "{text}"); + let tokens_line = format!( + " tokens: {} this tick, {} spent, {} remaining\n", checkpoint.tokens, reports[0].spent, reports[0].remaining - )), "{text}"); + ); + assert!(text.ends_with(&tokens_line), "{text}"); let speak = acts.iter().find(|act| act.verb == "speak").unwrap(); assert_eq!(speak.speaker_label, "Brannoch"); assert_eq!( @@ -825,13 +848,16 @@ fn an_error_ends_a_cell_as_the_run_must_treat_it() { expected: ControllerMode::OperationalAgent, }, ControllerError::AmbiguousOpportunity, - ControllerError::OpportunityMismatch, ] { assert!(matches!( classify(&gone), CellEnd::Refused(RefusalReason::OpportunityGone) )); } + assert!(matches!( + classify(&ControllerError::OpportunityMismatch), + CellEnd::Refused(RefusalReason::ScopeMoved) + )); assert!(matches!( classify(&ControllerError::CommandMismatch), CellEnd::Fatal("controller_error")