diff --git a/crates/ghostlight/src/verse/calls.rs b/crates/ghostlight/src/verse/calls.rs index 328417b..21d5c72 100644 --- a/crates/ghostlight/src/verse/calls.rs +++ b/crates/ghostlight/src/verse/calls.rs @@ -164,7 +164,6 @@ fn decode_row(row: &CultCacheEnvelope) -> Result = BTreeMap::new(); + let mut counts: BTreeMap = BTreeMap::new(); for row in &rows { let record = decode_row(row)?; + *counts.entry(record.run_id.clone()).or_default() += 1; let tally = runs.entry(record.run_id).or_default(); tally.last_sequence = tally.last_sequence.max(record.sequence); - tally.count += 1; tally.charged = tally.charged.saturating_add(record.charged); } - if runs.values().any(|tally| tally.count != tally.last_sequence) { + // Keys are unique, so a run is dense from 1 exactly when its rows + // number its highest sequence. + if runs.iter().any(|(run, tally)| counts[run] != tally.last_sequence) { return Err(CallRecordError::Corrupt); } Ok(Self { @@ -249,7 +251,6 @@ impl CallRecordStore { state.store.push(&row).map_err(|_| CallRecordError::Store)?; let tally = state.runs.entry(record.run_id.clone()).or_default(); tally.last_sequence = record.sequence; - tally.count += 1; tally.charged = tally.charged.saturating_add(record.charged); Ok(()) } diff --git a/crates/ghostlight/src/verse/calls/tests.rs b/crates/ghostlight/src/verse/calls/tests.rs index 67b8b2a..357fb59 100644 --- a/crates/ghostlight/src/verse/calls/tests.rs +++ b/crates/ghostlight/src/verse/calls/tests.rs @@ -13,6 +13,7 @@ type Script = Vec>; struct ScriptedPort { replies: Mutex>>, calls: AtomicUsize, + lent: AtomicUsize, gate: Option>, } @@ -21,6 +22,7 @@ impl ScriptedPort { Arc::new(Self { replies: Mutex::new(replies.into()), calls: AtomicUsize::new(0), + lent: AtomicUsize::new(0), gate: None, }) } @@ -29,6 +31,7 @@ impl ScriptedPort { Arc::new(Self { replies: Mutex::new(replies.into()), calls: AtomicUsize::new(0), + lent: AtomicUsize::new(0), gate: Some(gate), }) } @@ -44,6 +47,10 @@ impl InferencePort for ScriptedPort { PreparedInference::prepare("verse-test", 4_102_444_800_000, request) } + fn lend_tool_results(&self, _: &PreparedInference, _: Box) { + self.lent.fetch_add(1, Ordering::SeqCst); + } + async fn infer(&self, _: PreparedInference) -> Result { self.calls.fetch_add(1, Ordering::SeqCst); if let Some(gate) = &self.gate { @@ -323,16 +330,42 @@ async fn calls_on_the_wire_hold_their_bound() { while inner.calls() == 0 { tokio::task::yield_now().await; } - let fault = port - .infer(prepared_for("held", 1_200)) - .await - .expect_err("the first call's bound is still held"); + // A second call that was let through would wait on the closed gate. + let second = tokio::time::timeout( + std::time::Duration::from_secs(5), + port.infer(prepared_for("held", 1_200)), + ) + .await + .expect("the second call was refused, not left waiting on the provider"); + let fault = second.expect_err("the first call's bound is still held"); assert_eq!(fault.class(), InferenceFaultClass::BudgetExhausted); assert_eq!(inner.calls(), 1); gate.add_permits(1); first.await.unwrap().expect("the first call completes"); } +struct NoTools; + +impl ToolResultOracle for NoTools { + fn remaining_rounds(&self) -> u32 { + 0 + } + + fn answer(&mut self, _: &str, _: &str) -> Result { + Ok(String::new()) + } +} + +#[test] +fn tool_results_are_lent_to_the_port_underneath() { + let directory = tempfile::tempdir().unwrap(); + let (store, _) = store_in(&directory); + let inner = ScriptedPort::new(Vec::new()); + let port = recording(&inner, &store, "run-lend", u64::MAX); + port.lend_tool_results(&prepared_for("lend", 1_200), Box::new(NoTools)); + assert_eq!(inner.lent.load(Ordering::SeqCst), 1); +} + #[tokio::test] async fn a_reply_with_no_usage_is_charged_its_bound() { let directory = tempfile::tempdir().unwrap(); @@ -594,6 +627,13 @@ fn the_store_refuses_any_row_that_is_not_a_canonical_v1_record() { ("wrong key", v1("run-x/2", payload.clone())), ("not canonical", v1(&key, loose)), ("not a record", v1(&key, vec![1, 2, 3])), + ( + "a run id that is not a key segment", + v1( + &row_key("bad/run", 1), + rmp_serde::to_vec_named(&sample_record("bad/run", 1)).unwrap(), + ), + ), ("sequence zero", record_at(0)), ("a hole in the run", record_at(2)), ];