Skip to content

Commit 5a14f36

Browse files
rebekzclaude
andcommitted
fix(intel-agent): wire quota eviction; count String overhead in the SSE cap
Two artifacts that claimed a memory guarantee they did not provide. 1. evict_expired_at was DEAD CODE. Defined at quota.rs:160, its only call sites were inside mod tests (which starts at line 193), so no production path ever called it. Worse than a missing feature: a public method with that name and passing unit tests tells a reviewer expiry eviction is handled. Same failure class as the README that claimed the quota bypass was closed. Now called from check_and_record_at, which already holds &mut self under the caller's mutex — no new lock, timer, or background task. The O(n) scan per admission is acceptable and self-limiting: n is the number of tracked scopes, and the scan reclaims stale entries precisely when accumulation makes it worth paying for. Eviction semantics are unchanged; the pre-existing window tests pass unmodified, which is what establishes that evicting an elapsed entry and resetting one produce the same count. 2. The SSE buffer cap undercounted real memory by roughly 25x. data_bytes accumulated only line content length, but data_lines is a Vec<String> and each String carries ptr/len/cap overhead the 8 MB cap never counted. A stream of 1-byte data: lines charged ~1 byte each while consuming ~25, so the cap could admit on the order of 200 MB — while its own doc comment claimed to bound memory "under adversarial streams". Accounting now charges size_of::<String>() per retained line, computed rather than hardcoded. SSE_BUFFER_CAP keeps its exact value; only the accounting changed. The doc comment now states what is counted and stays honest that allocator bookkeeping and unused Vec capacity are not. Untouched by design, all proven live at 19d74ec: UTF-8/chunk behaviour, terminal_received, notify_incomplete_answer, and quota scope keys. Gates re-run independently by the orchestrator with exit codes captured directly: 63 unit (was 61) + 14 e2e pass, clippy --all-targets -D warnings clean, fmt clean. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent b4b2baa commit 5a14f36

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

‎crates/buzz-intel-agent/src/intel.rs‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,12 @@ where
387387
stop
388388
}
389389

390-
/// Hard cap on buffered SSE bytes (incomplete line + accumulated data lines).
391-
/// Mirrors the ACP NDJSON max line size to bound memory under adversarial streams.
390+
/// Hard cap on accounted SSE memory: incomplete line bytes, retained data
391+
/// contents, and one [`String`] metadata slot per retained data line.
392+
///
393+
/// This includes the dominant per-line overhead; allocator bookkeeping and
394+
/// unused `Vec` capacity are not counted. The cap mirrors the ACP NDJSON max
395+
/// line size to bound memory under adversarial streams.
392396
pub const SSE_BUFFER_CAP: usize = 8 * 1024 * 1024;
393397

394398
/// Incremental SSE parser that tolerates frames split across arbitrary **byte** chunks.
@@ -400,7 +404,7 @@ pub const SSE_BUFFER_CAP: usize = 8 * 1024 * 1024;
400404
pub struct SseByteParser {
401405
buf: Vec<u8>,
402406
data_lines: Vec<String>,
403-
/// Running total of bytes held in `data_lines` (for the size cap).
407+
/// Retained data content plus `size_of::<String>()` per line.
404408
data_bytes: usize,
405409
event_name: Option<String>,
406410
}
@@ -474,7 +478,7 @@ impl SseByteParser {
474478
} else if let Some(rest) = line.strip_prefix("data:") {
475479
// Spec: single space after colon is conventional; strip one.
476480
let data = rest.strip_prefix(' ').unwrap_or(rest);
477-
let add = data.len();
481+
let add = data.len().saturating_add(std::mem::size_of::<String>());
478482
if self.data_bytes.saturating_add(add) > SSE_BUFFER_CAP {
479483
return Err(AdapterError::Intel(format!(
480484
"sse data_lines exceeded {SSE_BUFFER_CAP} bytes (possible runaway stream)"
@@ -1015,6 +1019,28 @@ mod tests {
10151019
);
10161020
}
10171021

1022+
#[test]
1023+
fn sse_buffer_cap_counts_per_line_overhead() {
1024+
let mut p = SseByteParser::new();
1025+
let retained_per_line = 1 + std::mem::size_of::<String>();
1026+
let attempts = SSE_BUFFER_CAP / retained_per_line + 1;
1027+
let mut error = None;
1028+
1029+
for _ in 0..attempts {
1030+
if let Err(err) = p.push(b"data: x\n") {
1031+
error = Some(err);
1032+
break;
1033+
}
1034+
}
1035+
1036+
let err = error.expect("per-line metadata must count toward the SSE buffer cap");
1037+
assert!(
1038+
err.to_string().contains("sse buffer exceeded")
1039+
|| err.to_string().contains("sse data_lines exceeded"),
1040+
"unexpected err: {err}"
1041+
);
1042+
}
1043+
10181044
#[test]
10191045
fn sse_error_frame_stops_with_message() {
10201046
let mut p = SseByteParser::new();

‎crates/buzz-intel-agent/src/quota.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ impl TurnQuota {
116116
};
117117
};
118118

119+
// Admission pays an O(n) scan over tracked scopes. The map is bounded by
120+
// active community/channel/agent scopes, and a large map is exactly when
121+
// reclaiming elapsed scopes is worth the scan and immediately shrinks n.
122+
self.evict_expired_at(now);
123+
119124
let window = self.cfg.window;
120125
let entry = self.windows.entry(key.to_owned()).or_insert(Window {
121126
started_at: now,
@@ -344,6 +349,25 @@ mod tests {
344349
assert_eq!(q.tracked_scopes(), 0);
345350
}
346351

352+
#[test]
353+
fn enforcement_evicts_expired_scopes_via_check_and_record() {
354+
let mut q = TurnQuota::new(cfg(5, 60));
355+
let t0 = Instant::now();
356+
q.check_and_record_at("expired-a", t0);
357+
q.check_and_record_at("expired-b", t0);
358+
assert_eq!(q.tracked_scopes(), 2);
359+
360+
assert!(matches!(
361+
q.check_and_record_at("current", t0 + Duration::from_secs(61)),
362+
QuotaDecision::Allow { .. }
363+
));
364+
assert_eq!(
365+
q.tracked_scopes(),
366+
1,
367+
"normal enforcement must evict elapsed scopes before adding the current one"
368+
);
369+
}
370+
347371
#[test]
348372
fn scope_key_is_stable_and_carries_no_secrets() {
349373
let k = scope_key(

0 commit comments

Comments
 (0)