Skip to content

Commit 762e47b

Browse files
Duncanwpfleger96
andcommitted
fix(acp): replace get_mut latch with per-in-flight fold accumulators for sticky poison
The observation-time get_mut latch from the prior round closes the within-turn healing escape for baselined sessions, but is a no-op for un-baselined sessions (attach-to-existing path, no seed_zero_baseline): sessions.get_mut() returns None when no entry exists, so an absence observed mid-turn is forgotten at take(). Replace the latch with two per-in-flight-turn fold accumulators (input_absence_observed, output_absence_observed) on UsageTracker: - Reset to false in begin_turn(). - Folded (monotonic OR) at the top of record() for in-flight notifications, BEFORE the delta computation — so subsequent record() calls in the same turn see the accumulated absence even before take() commits it to the session entry. - Included in the delta computation's poison check alongside the session's committed flag and the current notification's own absence. - Committed at take() via OR into input_ever_poisoned / output_ever_poisoned, creating the session entry if none exists. The fold-at-take creates the entry for un-baselined sessions, closing the escape by construction: an absence observed during a turn where no session entry existed is not forgotten at turn boundary. The get_mut latch is removed: the fold accumulator subsumes it for all cases (in-flight, baselined or not). The three baseline-advance paths (record setup, record in-flight, take) and the setup-path poison propagation are unchanged. Adds two regression tests: - unbaselined_within_turn_input_absence_poisons_next_turn - unbaselined_within_turn_output_absence_poisons_next_turn Both FAILED at eb24590 (t2 delta_reliable: true — healed) and PASS at this tip. All prior sticky/within-turn tests continue green. Co-authored-by: Will Pfleger <[email protected]> Signed-off-by: Will Pfleger <[email protected]>
1 parent 25985b4 commit 762e47b

1 file changed

Lines changed: 165 additions & 77 deletions

File tree

‎crates/buzz-acp/src/usage.rs‎

Lines changed: 165 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,20 @@ pub(crate) struct UsageTracker {
286286
/// notification that carries A after an unproven/absent one must NOT
287287
/// resurrect the identity). Reset to `None` in `begin_turn()` and `take()`.
288288
pending_identity: Option<Option<buzz_core::agent_turn_metric::PricingIdentity>>,
289+
/// Per-in-flight-turn fold accumulator for input-field absence.
290+
///
291+
/// Set to `true` the first time any in-flight `record()` call for the
292+
/// current turn observes `accumulated_input_tokens: None`. Monotonically
293+
/// grows (never cleared mid-turn); reset to `false` by `begin_turn()`.
294+
/// At `take()` this value is OR-ed into the session's `input_ever_poisoned`
295+
/// flag, creating or updating the session entry as needed. This ensures
296+
/// the poison is captured even for un-baselined sessions (the
297+
/// attach-to-existing path that skips `seed_zero_baseline` and therefore
298+
/// starts with no entry in `self.sessions`).
299+
input_absence_observed: bool,
300+
/// Per-in-flight-turn fold accumulator for output-field absence.
301+
/// Symmetric contract to `input_absence_observed`.
302+
output_absence_observed: bool,
289303
}
290304

291305
impl UsageTracker {
@@ -299,6 +313,8 @@ impl UsageTracker {
299313
self.in_flight_session = Some(session_id.to_string());
300314
self.pending = None;
301315
self.pending_identity = None;
316+
self.input_absence_observed = false;
317+
self.output_absence_observed = false;
302318
}
303319

304320
/// Process a `usage_update` notification payload.
@@ -338,90 +354,98 @@ impl UsageTracker {
338354
// setup notifications (no in-flight turn) still advance the baseline.
339355
let is_in_flight = self.in_flight_session.as_deref() == Some(session_id);
340356

341-
// Latch poison at observation time — even mid-turn, even for the
342-
// in-flight session. Without this, a second `record()` call in the
343-
// same turn would re-read the unchanged session entry (whose
344-
// `input_ever_poisoned` is still false), see `prev.last_input = Some(…)`,
345-
// and allow the reintroduced value to heal `delta_reliable`.
346-
// Writing here is safe: the baseline counters (`last_input` etc.) are
347-
// NOT updated — only the monotonic poison flags, which can only grow.
348-
// Case 3 (in-flight for another session) is intentionally excluded: we
349-
// drop that notification entirely and leave the other session's state
350-
// untouched to avoid undercounting its next published delta.
351-
let this_in_flight_for_another = self.in_flight_session.is_some() && !is_in_flight;
352-
if !this_in_flight_for_another {
353-
if let Some(state) = self.sessions.get_mut(session_id) {
354-
if current_input.is_none() {
355-
state.input_ever_poisoned = true;
356-
}
357-
if current_output.is_none() {
358-
state.output_ever_poisoned = true;
359-
}
357+
// For in-flight notifications, fold the absence of each field into the
358+
// per-turn accumulators BEFORE computing the delta. This ensures the
359+
// second `record()` call in a turn sees the absence observed by the first,
360+
// even when no session entry exists yet (un-baselined path) and even when
361+
// the second notification reintroduces the field. The fold is monotonic
362+
// (OR — never cleared mid-turn); it is reset by `begin_turn()` and
363+
// committed to the session's sticky flags in `take()`.
364+
//
365+
// Case 3 (in-flight for another session) is intentionally excluded:
366+
// that notification is dropped entirely; leaving the other session's state
367+
// untouched avoids undercounting its next published delta.
368+
if is_in_flight {
369+
if current_input.is_none() {
370+
self.input_absence_observed = true;
371+
}
372+
if current_output.is_none() {
373+
self.output_absence_observed = true;
360374
}
361375
}
362376

363-
let (delta_reliable, turn_input, turn_output, turn_cost, turn_seq) =
364-
match self.sessions.get(session_id) {
365-
None => {
366-
// First notification for this session — no baseline yet.
367-
(false, None, None, None, 1u64)
368-
}
369-
Some(prev) => {
370-
// turn_seq for this pending record is one above the last
371-
// *published* seq — constant for all notifications in this
372-
// turn, advanced only on publish.
373-
let seq = prev.published_seq + 1;
374-
// Sticky-poison check: if ACP ever observed an absent input
375-
// or output snapshot for this session, delta_reliable is
376-
// permanently false. A later reintroduced value must NOT
377-
// heal the reliability — ACP cannot trust the producer's
378-
// permanence guarantee; the prefix delta is irrecoverably
379-
// unknown.
380-
let this_input_absent = current_input.is_none();
381-
let this_output_absent = current_output.is_none();
382-
let input_poisoned = prev.input_ever_poisoned || this_input_absent;
383-
let output_poisoned = prev.output_ever_poisoned || this_output_absent;
384-
if input_poisoned || output_poisoned {
385-
(false, None, None, None, seq)
386-
} else {
387-
match (
388-
current_input,
389-
current_output,
390-
prev.last_input,
391-
prev.last_output,
392-
) {
393-
(Some(ci), Some(co), Some(pi), Some(po)) => {
394-
// Token counter decrease → unreliable delta.
395-
if ci < pi || co < po {
396-
(false, None, None, None, seq)
397-
} else {
398-
let di = ci - pi;
399-
let dout = co - po;
400-
// Cost delta: only when both snapshots have cost.
401-
// A cost *decrease* is also unreliable (NIP-AM: negative
402-
// delta ⇒ delta_reliable false, null all turn fields).
403-
let (dc, cost_reliable) = match (current_cost, prev.last_cost) {
404-
(Some(c), Some(p)) if c >= p => (Some(c - p), true),
405-
(Some(_), Some(_)) => {
406-
// Both present but current < prev — counter decreased.
407-
(None, false)
408-
}
409-
_ => (None, true), // absent on either side: null cost, reliable tokens
410-
};
411-
if cost_reliable {
412-
(true, Some(di), Some(dout), dc, seq)
413-
} else {
414-
// Cost decrease overrides the whole record to unreliable.
415-
(false, None, None, None, seq)
377+
let (delta_reliable, turn_input, turn_output, turn_cost, turn_seq) = match self
378+
.sessions
379+
.get(session_id)
380+
{
381+
None => {
382+
// First notification for this session — no baseline yet.
383+
(false, None, None, None, 1u64)
384+
}
385+
Some(prev) => {
386+
// turn_seq for this pending record is one above the last
387+
// *published* seq — constant for all notifications in this
388+
// turn, advanced only on publish.
389+
let seq = prev.published_seq + 1;
390+
// Sticky-poison check: if ACP ever observed an absent input
391+
// or output snapshot for this session, delta_reliable is
392+
// permanently false. A later reintroduced value must NOT
393+
// heal the reliability — ACP cannot trust the producer's
394+
// permanence guarantee; the prefix delta is irrecoverably
395+
// unknown.
396+
//
397+
// Three sources of poison — all monotonic (OR):
398+
// 1. The session's committed flag from prior turns.
399+
// 2. The per-turn fold accumulator (captures absences seen
400+
// earlier in THIS turn before take() commits them).
401+
// 3. Whether THIS notification is itself absent.
402+
let this_input_absent = current_input.is_none();
403+
let this_output_absent = current_output.is_none();
404+
let input_poisoned =
405+
prev.input_ever_poisoned || self.input_absence_observed || this_input_absent;
406+
let output_poisoned =
407+
prev.output_ever_poisoned || self.output_absence_observed || this_output_absent;
408+
if input_poisoned || output_poisoned {
409+
(false, None, None, None, seq)
410+
} else {
411+
match (
412+
current_input,
413+
current_output,
414+
prev.last_input,
415+
prev.last_output,
416+
) {
417+
(Some(ci), Some(co), Some(pi), Some(po)) => {
418+
// Token counter decrease → unreliable delta.
419+
if ci < pi || co < po {
420+
(false, None, None, None, seq)
421+
} else {
422+
let di = ci - pi;
423+
let dout = co - po;
424+
// Cost delta: only when both snapshots have cost.
425+
// A cost *decrease* is also unreliable (NIP-AM: negative
426+
// delta ⇒ delta_reliable false, null all turn fields).
427+
let (dc, cost_reliable) = match (current_cost, prev.last_cost) {
428+
(Some(c), Some(p)) if c >= p => (Some(c - p), true),
429+
(Some(_), Some(_)) => {
430+
// Both present but current < prev — counter decreased.
431+
(None, false)
416432
}
433+
_ => (None, true), // absent on either side: null cost, reliable tokens
434+
};
435+
if cost_reliable {
436+
(true, Some(di), Some(dout), dc, seq)
437+
} else {
438+
// Cost decrease overrides the whole record to unreliable.
439+
(false, None, None, None, seq)
417440
}
418441
}
419-
// One or both sides absent (no prior baseline) → unreliable.
420-
_ => (false, None, None, None, seq),
421442
}
443+
// One or both sides absent (no prior baseline) → unreliable.
444+
_ => (false, None, None, None, seq),
422445
}
423446
}
424-
};
447+
}
448+
};
425449

426450
// Total-token delta: field-local — never affects `delta_reliable` or
427451
// the input/output deltas. Null when: no baseline exists, either
@@ -597,16 +621,32 @@ impl UsageTracker {
597621
// every in-flight notification carried the same one; emit `None` when
598622
// any notification was absent/unproven or they disagreed.
599623
let folded_identity = self.pending_identity.take().and_then(|inner| inner);
624+
// Consume and reset the per-turn fold accumulators before returning.
625+
// These must be reset even on the None path (no pending record) so a
626+
// subsequent begin_turn/take cycle starts clean.
627+
let input_absence_this_turn = std::mem::replace(&mut self.input_absence_observed, false);
628+
let output_absence_this_turn = std::mem::replace(&mut self.output_absence_observed, false);
600629
let mut record = self.pending.take()?;
601630
record.pricing_identity = folded_identity;
602631
// Advance the committed baseline to this published record so the
603632
// *next* turn measures its delta from here.
604-
// Propagate the sticky-poison flags from the existing state: once
605-
// poisoned, always poisoned — a resumed emission cannot heal the gap.
633+
//
634+
// Compute sticky-poison flags by combining three sources — all monotonic:
635+
// 1. Any prior session-level flag (from a previous turn).
636+
// 2. `input_absence_this_turn` / `output_absence_this_turn` — whether any
637+
// in-flight notification this turn observed an absent field. This is
638+
// the fold accumulator that closes the un-baselined escape: for sessions
639+
// on the attach-to-existing path (no `seed_zero_baseline`), no session
640+
// entry exists yet, so a `get_mut`-based latch would be a no-op — the
641+
// fold captures the absence regardless and commits it here.
642+
// 3. Whether the final published record's cumulative field is None (the
643+
// last-notification check that was already present).
606644
let existing = self.sessions.get(&record.session_id);
607645
let input_ever_poisoned = existing.is_some_and(|s| s.input_ever_poisoned)
646+
|| input_absence_this_turn
608647
|| record.cumulative_input_tokens.is_none();
609648
let output_ever_poisoned = existing.is_some_and(|s| s.output_ever_poisoned)
649+
|| output_absence_this_turn
610650
|| record.cumulative_output_tokens.is_none();
611651
self.sessions.insert(
612652
record.session_id.clone(),
@@ -2604,4 +2644,52 @@ mod tests {
26042644
"poison must persist to next turn (output)"
26052645
);
26062646
}
2647+
2648+
/// Un-baselined session (attach-to-existing path, no seed_zero_baseline):
2649+
/// an absent input snapshot observed mid-turn must poison the session even
2650+
/// though no session entry exists yet — a later reintroduced value must not
2651+
/// heal delta_reliable in the next turn.
2652+
///
2653+
/// This is Paul's probe that FAILED at eb24590e2e — the get_mut latch was a
2654+
/// no-op for un-baselined sessions. The fold accumulator on UsageTracker
2655+
/// captures the absence and commits it at take() regardless of whether a
2656+
/// session entry already exists.
2657+
#[test]
2658+
fn unbaselined_within_turn_input_absence_poisons_next_turn() {
2659+
let mut t = UsageTracker::default();
2660+
// NO seed_zero_baseline — attach-to-existing path
2661+
t.begin_turn("s");
2662+
t.record("s", &payload_opt(None, Some(10))); // poisoned snapshot
2663+
t.record("s", &payload_opt(Some(100), Some(20))); // reintroduced same turn
2664+
let t1 = t.take().expect("t1");
2665+
assert!(!t1.delta_reliable, "t1: no baseline — must be unreliable");
2666+
t.begin_turn("s");
2667+
t.record("s", &payload_opt(Some(150), Some(30)));
2668+
let t2 = t.take().expect("t2");
2669+
assert!(
2670+
!t2.delta_reliable,
2671+
"t2: absence was observed in t1 — sticky poison must hold"
2672+
);
2673+
}
2674+
2675+
/// Symmetric output-field case for the un-baselined escape:
2676+
/// absent output snapshot mid-turn must poison the session and persist to
2677+
/// the next turn, even when no session entry existed at record() time.
2678+
#[test]
2679+
fn unbaselined_within_turn_output_absence_poisons_next_turn() {
2680+
let mut t = UsageTracker::default();
2681+
// NO seed_zero_baseline — attach-to-existing path
2682+
t.begin_turn("s");
2683+
t.record("s", &payload_opt(Some(10), None)); // poisoned snapshot: output absent
2684+
t.record("s", &payload_opt(Some(100), Some(20))); // reintroduced same turn
2685+
let t1 = t.take().expect("t1");
2686+
assert!(!t1.delta_reliable, "t1: no baseline — must be unreliable");
2687+
t.begin_turn("s");
2688+
t.record("s", &payload_opt(Some(150), Some(30)));
2689+
let t2 = t.take().expect("t2");
2690+
assert!(
2691+
!t2.delta_reliable,
2692+
"t2: absence was observed in t1 — sticky poison must hold"
2693+
);
2694+
}
26072695
}

0 commit comments

Comments
 (0)