Verse call records: kill the first mutation round's survivors
The in-memory run tally no longer counts rows (only open used it); the store test pins a record whose run id is not a key segment; the lend_tool_results forwarding has a test; the in-flight test fails by timeout instead of hanging when a held call is let through. Builds: yes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e28bf3fd55
commit
f74dec6675
2 changed files with 49 additions and 8 deletions
|
|
@ -164,7 +164,6 @@ fn decode_row(row: &CultCacheEnvelope) -> Result<ProviderCallRecord, CallRecordE
|
|||
#[derive(Default)]
|
||||
struct RunTally {
|
||||
last_sequence: u64,
|
||||
count: u64,
|
||||
charged: u64,
|
||||
}
|
||||
|
||||
|
|
@ -192,14 +191,17 @@ impl CallRecordStore {
|
|||
.map_err(|_| CallRecordError::Store)?;
|
||||
let rows = store.pull_all().map_err(|_| CallRecordError::Store)?;
|
||||
let mut runs: BTreeMap<String, RunTally> = BTreeMap::new();
|
||||
let mut counts: BTreeMap<String, u64> = 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(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ type Script = Vec<Result<InferenceOutput, InferenceFault>>;
|
|||
struct ScriptedPort {
|
||||
replies: Mutex<VecDeque<Result<InferenceOutput, InferenceFault>>>,
|
||||
calls: AtomicUsize,
|
||||
lent: AtomicUsize,
|
||||
gate: Option<Arc<tokio::sync::Semaphore>>,
|
||||
}
|
||||
|
||||
|
|
@ -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<dyn ToolResultOracle>) {
|
||||
self.lent.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
async fn infer(&self, _: PreparedInference) -> Result<InferenceOutput, InferenceFault> {
|
||||
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<String, crate::ControllerError> {
|
||||
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)),
|
||||
];
|
||||
|
|
|
|||
Loading…
Reference in a new issue