perf(core): provider records over mpsc, single-pass masked request build; mock turn bound 220 allocations
8d71177551335fdb71121b47fb1c5d00c7ae04d4
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
- Parent 1: 5af6f910095b
There are many coding harnesses - but this one is fast
8d71177551335fdb71121b47fb1c5d00c7ae04d4
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
+111 / −48 text lines
+1 / −0 text lines
+6 / −8 text lines
+44 / −25 text lines
provider_records: Option<smith::provider::RecordSpool>, provider_records: Option<smith::provider::RecordDrain>, /// A masked copy of `message`: one allocation per text, never a /// plaintext intermediate. fn mask_message(&self, message: &Message) -> Message { let blocks = message .blocks .iter() .map(|block| match block { ContentBlock::Text(text) => ContentBlock::Text(self.mask(text)), ContentBlock::Thinking(text) => ContentBlock::Thinking(self.mask(text)), ContentBlock::ToolUse { name, input, call_id, } => ContentBlock::ToolUse { name: self.mask(name), input: self.mask_json(input), call_id: *call_id, }, ContentBlock::ToolResult { call_id, ok, output, } => ContentBlock::ToolResult { call_id: *call_id, ok: *ok, output: self.mask(output), }, }) .collect(); Message { id: message.id, role: message.role, blocks, timestamp_ms: message.timestamp_ms, } } records: Option<smith::provider::RecordSpool>, records: Option<smith::provider::RecordDrain>, messages.push(message.clone()); // Provider-bound context never receives registered plaintext. for message in &mut request.messages { for block in &mut message.blocks { match block { ContentBlock::Text(text) | ContentBlock::Thinking(text) => { *text = self.secrets.mask(text); } ContentBlock::ToolUse { name, input, .. } => { *name = self.secrets.mask(name); *input = self.secrets.mask_json(input); } ContentBlock::ToolResult { output, .. } => { *output = self.secrets.mask(output); } } } } let Some(spool) = self.provider_records.clone() else { let Some(drain) = &self.provider_records else {+1 / −0 text lines
+5 / −9 text lines
new file mode 100644+44 / −0 text lines
//! Allocation-count bounds on one agent turn (`SMH-SPEC-SPEC0001`, memory//! accounting). Bounds only tighten.use smith_alloc::{Counting, Scope, enter, snapshot};use smith_harness::{RuntimeConfig, build_agent, mock_text_reply};#[global_allocator]static ALLOC: Counting = Counting;/// One mock text turn on a fresh session: request build, stream consumption,/// two recorded entries, one frame write each.const MOCK_TURN_ALLOCATIONS: u64 = 220;fn mock_turn(tag: &str) -> (u64, u64) { let dir = std::env::temp_dir().join(format!("smith_alloc_{tag}_{}", std::process::id())); let _ = std::fs::remove_dir_all(&dir); std::fs::create_dir_all(&dir).unwrap(); let mut agent = build_agent(&RuntimeConfig::mock(&dir, vec![mock_text_reply("hi")])).unwrap(); let before = snapshot(Scope::Turn); let guard = enter(Scope::Turn); let outcome = agent.run_turn("hello"); drop(guard); let after = snapshot(Scope::Turn); assert_eq!(outcome.unwrap().text, "hi"); let _ = std::fs::remove_dir_all(&dir); ( after.allocations - before.allocations, after.bytes - before.bytes, )}#[test]fn mock_turn_is_bounded_and_reproducible() { // Process-wide lazy state initializes on the first turn only. let _warmup = mock_turn("warmup"); let first = mock_turn("a"); let second = mock_turn("b"); assert_eq!(first, second); assert!( first.0 <= MOCK_TURN_ALLOCATIONS, "turn allocated {} times, bound is {MOCK_TURN_ALLOCATIONS}", first.0 );}+10 / −6 text lines
/// A fresh empty spool.