From 0dd4787ae3d46399614c42f04c90095ecb1f9bf6 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 2 Dec 2026 20:20:49 +0100 Subject: [PATCH] Live seeded-Begin check (ignored): real connector, imported snapshot, counted calls and usage Co-Authored-By: Claude Sonnet 5.5 --- crates/ghostlight-dungeon/src/runtime.rs | 127 +++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/crates/ghostlight-dungeon/src/runtime.rs b/crates/ghostlight-dungeon/src/runtime.rs index d6724fa..72f91e0 100644 --- a/crates/ghostlight-dungeon/src/runtime.rs +++ b/crates/ghostlight-dungeon/src/runtime.rs @@ -574,6 +574,133 @@ fn require_no_runtime_custody_failure( mod tests { use super::*; + /// Counts every call and the usage the connector reports, around the real + /// port, for the live check. + struct CountingPort { + inner: Arc, + calls: std::sync::atomic::AtomicU64, + usage: std::sync::Mutex<(u64, u64)>, + } + + #[async_trait::async_trait] + impl InferencePort for CountingPort { + fn prepare( + &self, + request: ghostlight::InferenceRequest, + ) -> Result { + self.inner.prepare(request) + } + + async fn infer( + &self, + request: ghostlight::PreparedInference, + ) -> Result { + self.calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + let output = self.inner.infer(request).await?; + let seen = serde_json::to_value(&output).unwrap_or_default(); + let mut usage = self.usage.lock().unwrap(); + usage.0 += seen["usage"]["prompt"].as_u64().unwrap_or(0); + usage.1 += seen["usage"]["completion"].as_u64().unwrap_or(0); + Ok(output) + } + } + + /// Live: Begin on an imported snapshot through the real connector. + /// `GHOSTLIGHT_LIVE_STATE_ROOT` holds `snapshots/` from `import-vault`, + /// `GHOSTLIGHT_LIVE_PROFILE` names the profile, and the connector address + /// and key credential are the daemon's own (`GHOSTLIGHT_CONNECTOR_ADDR`, + /// `$CREDENTIALS_DIRECTORY/connector-key`). Spends real credit. + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] + #[ignore = "live: spends connector credit; run on Yggdrasil under a unit that loads the connector key"] + async fn live_seeded_begin_reaches_ready_with_a_place_and_people() { + use crate::{ + begin::{BeginRequest, Scale, SeedStatus, Seeding, begin, tests::principal}, + catalog::Catalog, + state_layout::prepare_admitted_state_layout, + timelines::{Shared, Timelines}, + }; + let root = PathBuf::from(std::env::var("GHOSTLIGHT_LIVE_STATE_ROOT").unwrap()); + let profile = std::env::var("GHOSTLIGHT_LIVE_PROFILE").unwrap(); + prepare_admitted_state_layout(&root).unwrap(); + let shared = open_shared().unwrap(); + let counting = Arc::new(CountingPort { + inner: shared.inference.clone(), + calls: Default::default(), + usage: Default::default(), + }); + let shared = Shared { inference: counting.clone(), ..shared }; + let (fatal, _fatal_events) = mpsc::unbounded_channel(); + let timelines = Timelines::new(root.clone(), shared, fatal); + let catalog = Mutex::new(Catalog::open(root.join("catalog.cc")).unwrap()); + let vaults = crate::vaults::SnapshotVaults::new(&root); + let seeding = Arc::new(Seeding::new(Scale::from_env())); + + let started = Instant::now(); + let begun = begin( + &catalog, + &timelines, + &vaults, + &seeding, + &principal("live"), + BeginRequest { + key: uuid::Uuid::new_v4().to_string(), + title: "Ghostlight Live".into(), + name: "Deru".into(), + brief: None, + vault: Some(profile.clone()), + }, + ) + .await + .unwrap(); + println!("LIVE begin returned after {:?}: {}", started.elapsed(), begun.reached); + let ready = loop { + match seeding.status(&begun.timeline) { + None => break started.elapsed(), + Some(SeedStatus::Failed(reason)) => panic!("the seed failed: {reason}"), + Some(SeedStatus::Seeding) if started.elapsed() > Duration::from_secs(1800) => { + panic!("still seeding after 30 minutes") + } + Some(SeedStatus::Seeding) => tokio::time::sleep(Duration::from_secs(2)).await, + } + }; + let (calls, usage) = (counting.calls.load(std::sync::atomic::Ordering::SeqCst), *counting.usage.lock().unwrap()); + println!("LIVE ready after {ready:?}; calls={calls} prompt_tokens={} completion_tokens={}", usage.0, usage.1); + let host = timelines.host(&begun.timeline).await.unwrap(); + let report = |label: &str, world: &ghostlight::WorldSnapshot| { + println!("LIVE {label}: phase={:?}", world.phase); + for place in &world.places { + println!("LIVE place {}", place.label); + } + for subject in &world.subjects { + println!("LIVE subject {:?} {} qualified={}", subject.kind, subject.label, subject.qualified); + } + for row in &world.scale_deficit { + println!("LIVE row {:?} target={} qualified={} deficit={}", row.kind, row.target, row.qualified, row.deficit); + } + }; + let world = host.world.snapshot().await.unwrap(); + report("after seed", &world); + assert_eq!(world.phase, ghostlight::WorldPhase::Active); + assert!(world.places.len() >= 2, "the root and at least one place under it"); + let person = world.scale_deficit.iter().find(|row| row.kind == ghostlight::SubjectKind::Person).unwrap(); + assert!(person.qualified >= 2, "at least two qualifying people"); + + // The first run-ahead sweep, then the world as play would meet it. + tokio::time::sleep(Duration::from_secs(1)).await; + let sweep_started = Instant::now(); + while host.sweeping.try_lock().is_err() && sweep_started.elapsed() < Duration::from_secs(1800) { + tokio::time::sleep(Duration::from_secs(2)).await; + } + let (calls, usage) = (counting.calls.load(std::sync::atomic::Ordering::SeqCst), *counting.usage.lock().unwrap()); + println!( + "LIVE after run-ahead at {:?}: calls={calls} prompt_tokens={} completion_tokens={}", + started.elapsed(), + usage.0, + usage.1 + ); + report("after run-ahead", &host.world.snapshot().await.unwrap()); + } + #[test] fn lanes_default_to_the_ruled_tiers_and_each_reads_its_own_override() { let (models, play) = lane_models_from(|_| None);