claude-connector: a key is never lent (validate refuses auth api-key with any lease); the meter-fixture test reads at an injected instant

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-29 18:30:45 +01:00
commit b968ada525
4 changed files with 101 additions and 4 deletions

View file

@ -2202,7 +2202,16 @@ fn quota_of(rig: &Rig) -> model_connector::PlanQuotaSnapshot {
/// The windows with their observation stamps zeroed, for comparing readings.
fn readings(rig: &Rig) -> Vec<model_connector::PlanQuota> {
let mut windows = quota_of(rig).windows;
readings_at(rig, None)
}
/// The held windows as of `now` (unix ms), or as of the wall clock when none.
fn readings_at(rig: &Rig, now: Option<u64>) -> Vec<model_connector::PlanQuota> {
let cell = rig.backend.quota();
let mut windows = match now {
Some(now) => cell.snapshot_at(now).windows,
None => cell.snapshot().windows,
};
for window in &mut windows {
window.observed_at_unix_ms = 0;
}
@ -2878,9 +2887,10 @@ fn the_meter_call_reads_the_week_from_a_real_captured_event() {
.read_meter()
.expect("the captured call completes");
assert_eq!((usage.input_tokens, usage.output_tokens), (384, 4));
// 0.06 of the week used; it resets at 1794261600 (unix s).
// 0.06 of the week used; it resets at 1794261600 (unix s). Read as of an instant before
// that reset, so the captured event stays a live reading whatever the wall clock says.
assert_eq!(
readings(&rig),
readings_at(&rig, Some(1_794_261_600_000 - 1)),
vec![window("seven_day", 60, 1_794_261_600_000)]
);
let argv = rig.seen().argv;

View file

@ -248,6 +248,13 @@ impl ClaudeConnectorConfig {
}
fn validate_leases(&self) -> Result<(), ConfigError> {
// A key has no plan and is never lent: no lease socket, no lease row, whatever else the
// document says. Personas call with their own connection keys and need neither.
if self.auth == ClaudeAuth::ApiKey
&& (self.lease_socket.is_some() || !self.lease_callers.is_empty())
{
return Err(ConfigError::Invalid("auth"));
}
let Some(socket) = &self.lease_socket else {
return if self.lease_callers.is_empty() {
Ok(())

View file

@ -155,7 +155,7 @@ impl QuotaCell {
/// The windows as of `now`. One whose reset time has passed is reported
/// `expired` with no utilization: the reading described a window that no
/// longer exists.
fn snapshot_at(&self, now: u64) -> PlanQuotaSnapshot {
pub(crate) fn snapshot_at(&self, now: u64) -> PlanQuotaSnapshot {
let held = self.windows.lock().unwrap_or_else(|e| e.into_inner());
PlanQuotaSnapshot {
windows: held