//! Allocation-count bounds on session hot paths (`SMH-SPEC-SPEC0001`, //! memory accounting). Bounds only tighten. use smith::config::Config; use smith::message::{Message, Role}; use smith::structured::StructuredData; use smith_alloc::{Counting, Scope, measure}; use smith_core::content_codec::{Blocks, EntryContentView}; use smith_core::frame::Frame; use smith_core::session::{EntryContent, EntryFrame, Session}; use smith_core::store::{SessionReader, SessionWriter, open_session}; use smith_core::trace::trace; use std::sync::Mutex; #[path = "support/child.rs"] mod child_fixture; #[global_allocator] static ALLOC: Counting = Counting; /// Scope counters are process-wide; measurements must not overlap. static MEASURING: Mutex<()> = Mutex::new(()); /// `fork_at` over 1000 entries: the branch list, the record `Vec`, and one /// arena slice. Content bytes are copied, never decoded. const FORK_AT_1000_ALLOCATIONS: u64 = 3; /// Encoding a header frame: one buffer sized by the payload hint holds the /// prefix and the whole payload. const HEADER_ENCODE_ALLOCATIONS: u64 = 1; /// Streaming trace over two metadata records: one unrelated and one mutation completion. const TRACE_REPLAY_METADATA_ALLOCATIONS: u64 = 90; #[test] fn streaming_redaction_allocation_count_is_pinned() { use futures::StreamExt; use smith::stream::{StopReason, StreamEvent}; use smith_core::agent::Agent; use smith_core::tools::ToolSession; let stream: smith::provider::StreamFn = std::sync::Arc::new(|_, _| { futures::stream::iter(vec![ Ok(StreamEvent::text_delta( smith::id::MessageId::new(), "public prefix ab", )), Ok(StreamEvent::text_delta( smith::id::MessageId::new(), "ordinary text", )), Ok(StreamEvent::stop(StopReason::EndTurn, None)), ]) .boxed() }); let mut agent = Agent::new(stream, ToolSession::new(std::env::temp_dir()), "mock"); agent.secrets_mut().register("synthetic", "abcdef"); let ((allocations, _), outcome) = session_cost(|| agent.run_turn("input").unwrap()); assert_eq!(outcome.text, "public prefix abordinary text"); // Redactor's no-secret fast path removes one temporary allocation. assert_eq!(allocations, 51); } #[test] fn cancellation_intent_encodes_in_one_fixed_buffer() { let frame = Frame::CancelIntent { submission_id: smith::id::EntryId::new(), }; let ((allocations, _), bytes) = session_cost(|| frame.encode(1024).unwrap()); assert_eq!(allocations, 1); assert_eq!(bytes.len(), 37); } #[test] fn admission_encoding_has_two_bounded_allocations() { let entry = EntryFrame::new( None, &EntryContent::Message(Message::with_text(Role::User, "hello")), ) .unwrap(); let frame = Frame::Admission { entry, key: Some("k".repeat(smith_core::frame::ADMISSION_KEY_BYTES_MAX)), }; let ((allocations, _), bytes) = session_cost(|| frame.encode(1024).unwrap()); assert_eq!(allocations, 2); assert!( bytes.len() <= 1024 + smith_core::frame::HEADER_LEN + smith_core::frame::BODY_PREFIX_LEN ); } #[test] fn work_commit_encoding_uses_one_bounded_buffer() { let submission_id = smith::id::EntryId::new(); let output = EntryFrame::new( Some(submission_id), &EntryContent::Message(Message::with_text(Role::Assistant, "answer")), ) .unwrap(); for frame in [ Frame::Claim { submission_id }, Frame::Settlement { submission_id, reason: Some(smith::stream::StopReason::EndTurn), output: Some(output), }, ] { let ((allocations, _), bytes) = session_cost(|| frame.encode(1024).unwrap()); assert_eq!(allocations, 1); assert!( bytes.len() <= 1024 + smith_core::frame::HEADER_LEN + smith_core::frame::BODY_PREFIX_LEN ); } assert_eq!(std::mem::size_of::(), 1); assert_eq!( std::mem::size_of::<(smith::id::EntryId, smith_core::store::WorkStatus)>(), 17 ); } /// `from_frames` over 1000 decoded frames: arena, stored records, the /// parent-link table, the branch list, and one branch's records, each sized /// up front. Independent of entry count. const FROM_FRAMES_1000_ALLOCATIONS: u64 = 5; fn linear_session(len: usize) -> Session { let mut session = Session::new(); for turn in 0..len { let content = EntryContent::Message(Message::with_text(Role::User, format!("turn {turn}"))); session.append(&content).unwrap(); } session } /// Allocations and bytes charged to [`Scope::Session`] while `run` executes. fn session_cost(run: impl FnOnce() -> T) -> ((u64, u64), T) { let _serial = MEASURING .lock() .unwrap_or_else(std::sync::PoisonError::into_inner); let (out, cost) = measure(Scope::Session, run); ((cost.allocations, cost.bytes), out) } /// Cost of one `fork_at` of the leaf. fn fork_cost(session: &Session) -> (u64, u64) { let leaf = session.selected_id().expect("non-empty session"); let (cost, fork) = session_cost(|| session.fork_at(leaf).expect("leaf is known")); assert_eq!( fork.active_branch().entries().len(), session.active_branch().entries().len() ); let last = fork.active_branch().entries()[fork.active_branch().entries().len() - 1]; assert_eq!(fork.bytes(&last), session.bytes(&last)); cost } /// Cost of restoring `session` from its decoded frames; frame decoding /// itself happens before the measured scope. fn from_frames_cost(session: &Session) -> (u64, u64) { let mut frames = vec![Frame::Header { header: session.header(), }]; frames.extend( session .active_branch() .entries() .iter() .map(|entry| Frame::Known { entry: EntryFrame { id: entry.id, parent: entry.parent, timestamp_ms: entry.timestamp_ms, kind: entry.kind, content: session.bytes(entry).to_vec(), }, }), ); let (cost, restored) = session_cost(|| Session::from_frames(&frames).expect("frames restore")); assert_eq!(&restored, session); cost } #[test] fn selected_context_allocation_is_independent_of_old_and_unrelated_payloads() { fn cost(old_bytes: usize, tag: &str) -> (u64, u64) { let path = std::env::temp_dir().join(format!("smith-effective-{tag}-{}.smh", std::process::id())); let _ = std::fs::remove_file(&path); let cfg = Config::default_valid(); let mut opened = open_session(&path, cfg.clone()).unwrap(); let old = EntryFrame::new( None, &EntryContent::Message(Message::with_text(Role::User, "x".repeat(old_bytes))), ) .unwrap(); let compact = EntryFrame::new( Some(old.id), &EntryContent::Meta { kind: smith_core::session::COMPACTION_KIND.into(), detail: StructuredData::from_serializable(&serde_json::json!({"summary": ""})) .unwrap(), }, ) .unwrap(); let after = EntryFrame::new( Some(compact.id), &EntryContent::Message(Message::with_text(Role::User, "after")), ) .unwrap(); let unrelated = EntryFrame::new( Some(old.id), &EntryContent::Message(Message::with_text(Role::User, "y".repeat(old_bytes))), ) .unwrap(); for entry in [&old, &compact, &after, &unrelated] { opened .writer .append_frame(&Frame::Known { entry: entry.clone(), }) .unwrap(); } opened.writer.select_entry(after.id).unwrap(); drop(opened); let reader = SessionReader::open(&path).unwrap(); let (selected, cost) = measure(Scope::Session, || { reader.selected_session(cfg.frame_bytes_max).unwrap() }); assert_eq!(selected.active_branch().entries().len(), 2); drop(reader); let _ = std::fs::remove_file(path); (cost.allocations, cost.bytes) } let _lock = MEASURING.lock().unwrap(); let small = cost(100, "small"); let large = cost(400_000, "large"); assert_eq!( small, large, "pre-compaction and sibling payloads must not be retained or decoded" ); } #[test] fn fork_at_over_1000_entries_is_bounded_and_reproducible() { let session = linear_session(1000); let first = fork_cost(&session); let second = fork_cost(&session); assert_eq!( first.0, FORK_AT_1000_ALLOCATIONS, "fork_at allocated {} times, bound is {FORK_AT_1000_ALLOCATIONS}", first.0 ); assert_eq!(first, second); } #[test] fn from_frames_allocates_independently_of_entry_count() { let small = from_frames_cost(&linear_session(100)); let large = from_frames_cost(&linear_session(1000)); assert_eq!( large.0, FROM_FRAMES_1000_ALLOCATIONS, "from_frames allocated {} times, bound is {FROM_FRAMES_1000_ALLOCATIONS}", large.0 ); assert_eq!(small.0, large.0); assert_eq!(large, from_frames_cost(&linear_session(1000))); } #[cfg(target_arch = "x86_64")] #[test] fn trace_replay_metadata_allocations_are_pinned() { // scratch_file constructs paths from TMPDIR during the measured replay. // A fixed relative spelling keeps PathBuf growth independent of OS root // length; a child avoids process-wide environment mutation and count noise. const CHILD: &str = "SMITH_TRACE_ALLOCATION_CHILD"; if std::env::var_os(CHILD).is_none() { let root = std::env::temp_dir().join(format!("trace-pins-{}", std::process::id())); std::fs::create_dir(&root).unwrap(); let output = child_fixture::run( std::process::Command::new(std::env::current_exe().unwrap()) .args(["--exact", "trace_replay_metadata_allocations_are_pinned"]) .current_dir(&root) .env("TMPDIR", ".") .env(CHILD, "1"), ); std::fs::remove_dir_all(root).unwrap(); assert!( output.status.success(), "trace allocation child failed: {}{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr) ); return; } let _serial = MEASURING.lock().unwrap(); let path = std::env::temp_dir().join(format!("smith-trace-alloc-{}.smh", std::process::id())); let _ = std::fs::remove_file(&path); let config = Config::default_valid(); let mut opened = open_session(&path, config.clone()).unwrap(); let call_id = smith::ToolCallId::from_u128(1); let call = EntryFrame::new( None, &EntryContent::ToolCall { name: "write".to_string(), input: smith::ToolArguments::from_serializable( &serde_json::json!({"path": "note.txt", "content": "x"}), ) .unwrap(), call_id, }, ) .unwrap(); let result = EntryFrame::new( Some(call.id), &EntryContent::ToolResult { ok: true, output: "written".to_string(), call_id, }, ) .unwrap(); let unrelated = EntryFrame::new( Some(result.id), &EntryContent::Meta { kind: "custom".to_string(), detail: StructuredData::from_serializable( &serde_json::json!({"payload": "x".repeat(4096)}), ) .unwrap(), }, ) .unwrap(); let completion = EntryFrame::new( Some(unrelated.id), &EntryContent::Meta { kind: smith_core::session::TOOL_MUTATION_KIND.to_string(), detail: StructuredData::from_serializable( &serde_json::json!({"call_id": call_id, "mutation_state": "applied"}), ) .unwrap(), }, ) .unwrap(); for entry in [&call, &result, &unrelated, &completion] { opened .writer .append_frame(&Frame::Known { entry: entry.clone(), }) .unwrap(); } drop(opened); let reader = SessionReader::open(&path).unwrap(); let (records, cost) = measure(Scope::Session, || { let mut records = 0usize; trace(&reader, config.frame_bytes_max, |_| { records += 1; Ok(()) }) .unwrap(); records }); assert_eq!(records, 5); assert_eq!( cost.allocations, TRACE_REPLAY_METADATA_ALLOCATIONS, "trace allocations: {cost:?}" ); drop(reader); std::fs::remove_file(path).unwrap(); } #[cfg(target_arch = "x86_64")] #[test] fn decoded_content_view_fits_one_cache_line_and_blocks_three_words() { assert_eq!(std::mem::size_of::>(), 56); assert_eq!(std::mem::size_of::>(), 24); } #[test] fn text_only_content_decode_and_scan_allocate_nothing() { let content = EntryFrame::new( None, &EntryContent::Message(Message::with_text(Role::User, "needle")), ) .unwrap() .content; let (cost, found) = session_cost(|| { let view = EntryContentView::decode(&content).unwrap(); view.contains("needle") }); assert!(found); assert_eq!(cost.0, 0, "text inspection allocated {} times", cost.0); } #[test] fn nested_json_inspection_borrows_without_allocating() { let content = EntryFrame::new( None, &EntryContent::Meta { kind: "event".into(), detail: StructuredData::from_serializable( &serde_json::json!({"outer": [{"value": "needle"}, 0, false, null]}), ) .unwrap(), }, ) .unwrap() .content; let (cost, found) = session_cost(|| { let view = EntryContentView::decode(&content).unwrap(); view.contains("needle") }); assert!(found); assert_eq!(cost.0, 0, "JSON inspection allocated {} times", cost.0); } #[test] fn unrelated_history_does_not_become_resident_selected_context() { // SMH-SPEC-SPEC0001: Sessions, context memory ≠ complete file size. let path = std::env::temp_dir().join(format!("smith-paged-memory-{}.smh", std::process::id())); let _ = std::fs::remove_file(&path); let cfg = Config::default_valid(); let mut writer = SessionWriter::open(&path, cfg.clone()).unwrap(); writer .append_frame(&Frame::Header { header: Session::new().header(), }) .unwrap(); let root = EntryFrame::new( None, &EntryContent::Message(Message::with_text(Role::User, "root")), ) .unwrap(); writer .append_frame(&Frame::Known { entry: root.clone(), }) .unwrap(); for _ in 0..128 { let sibling = EntryFrame::new( Some(root.id), &EntryContent::Message(Message::with_text(Role::User, "x".repeat(32 * 1024))), ) .unwrap(); writer .append_frame(&Frame::Known { entry: sibling }) .unwrap(); } writer.select_entry(root.id).unwrap(); let reader = SessionReader::open(&path).unwrap(); let _serial = MEASURING .lock() .unwrap_or_else(std::sync::PoisonError::into_inner); let (selected, cost) = measure(Scope::IoRequest, || { reader.selected_session(cfg.frame_bytes_max).unwrap() }); assert_eq!(selected.selected_id(), Some(root.id)); assert_eq!(selected.active_branch().entries().len(), 1); assert!( cost.peak < 256 * 1024, "selected context peak {} bytes for {}-byte file", cost.peak, reader.file_size() ); assert!(reader.file_size() > 4 * 1024 * 1024); drop(reader); drop(writer); let _ = std::fs::remove_file(path); } #[test] fn header_frame_encodes_into_one_allocation() { let header = Frame::Header { header: Session::new().header(), }; let (cost, encoded) = session_cost(|| header.encode(smith_core::frame::DEFAULT_MAX_FRAME_BYTES)); assert!(encoded.is_ok()); assert_eq!( cost.0, HEADER_ENCODE_ALLOCATIONS, "header encode allocated {} times, bound is {HEADER_ENCODE_ALLOCATIONS}", cost.0 ); } #[test] fn borrowed_context_edit_decode_allocates_nothing() { use smith_core::context::{ContextEdit, ContextReplacement}; let content = EntryContent::ContextEdit(vec![ ContextEdit { target: smith::id::EntryId::from_u128(1), replacement: Some(ContextReplacement::Text("replacement".into())), }, ContextEdit { target: smith::id::EntryId::from_u128(2), replacement: Some(ContextReplacement::AssistantText("prose".into())), }, ContextEdit { target: smith::id::EntryId::from_u128(3), replacement: Some(ContextReplacement::ToolResultOutput { call_id: smith::id::ToolCallId::from_u128(4), output: "output".into(), }), }, ]); let entry = EntryFrame::new(None, &content).unwrap(); let (cost, decoded) = session_cost(|| EntryContentView::decode(&entry.content).unwrap()); assert_eq!(cost, (0, 0)); assert!(matches!(decoded, EntryContentView::ContextEdit(_))); } #[test] fn context_validation_borrows_large_unedited_payloads() { use smith_core::context::{ContextEdit, ContextReplacement}; let mut session = Session::new(); session .append(&EntryContent::Message(Message::with_text( Role::User, "x".repeat(1 << 20), ))) .unwrap(); let target = session .append(&EntryContent::Message(Message::with_text( Role::User, "target", ))) .unwrap() .id; let edit = ContextEdit { target, replacement: Some(ContextReplacement::AssistantText("wrong role".into())), }; // The only allocation is the rejection diagnostic, not any resident message. let (cost, rejected) = session_cost(|| { session.edit_context_with(&[edit], |_, _| panic!("invalid edit reached persistence")) }); assert!(rejected.is_err()); assert_eq!(cost.0, 1); } #[test] fn repeated_replacements_retain_effective_context_not_edit_history() { use smith_alloc::snapshot; use smith_core::context::{ContextEdit, ContextReplacement}; let config = Config::default_valid(); let original = EntryContent::Message(Message::with_text(Role::User, "raw".repeat(170_000))); let large = "replacement".repeat(5_000); let _serial = MEASURING .lock() .unwrap_or_else(std::sync::PoisonError::into_inner); let mut restored_residency = None; let mut live_residency = None; let mut claimed_residency = None; for count in [10, 100] { let path = std::env::temp_dir().join(format!( "smith-edit-working-set-{}-{count}.smh", smith::id::SessionId::new() )); let mut writer = SessionWriter::open(&path, config.clone()).unwrap(); writer .append_frame(&Frame::Header { header: Session::new().header(), }) .unwrap(); let root = EntryFrame::new(None, &original).unwrap(); writer .append_frame(&Frame::Known { entry: root.clone(), }) .unwrap(); let mut parent = root.id; for index in 0..=count { let entry = EntryFrame::new( Some(parent), &EntryContent::ContextEdit(vec![ContextEdit { target: root.id, replacement: Some(ContextReplacement::Text(if index == count { "tiny".into() } else { large.clone() })), }]), ) .unwrap(); parent = entry.id; writer.append_frame(&Frame::Known { entry }).unwrap(); } let reader = SessionReader::open(&path).unwrap(); let before = snapshot(Scope::IoRequest).live; let ((mut selected, effective), cost) = measure(Scope::IoRequest, || { let selected = reader.selected_session(config.frame_bytes_max).unwrap(); let effective = selected.projected_messages().unwrap(); (selected, effective) }); let resident = snapshot(Scope::IoRequest).live.saturating_sub(before); if let Some(first) = restored_residency { assert_eq!( resident, first, "history growth must not increase residency" ); } restored_residency = Some(resident); assert_eq!( effective[0].blocks, Message::with_text(Role::User, "tiny").blocks ); assert!( resident <= 1536, "{count} replacements retain {resident} bytes; peak {}", cost.peak ); assert!( cost.peak <= 1536 * 1024, "{count} replacements peak {}", cost.peak ); assert_eq!(selected.selected_id(), Some(parent)); assert_eq!(selected.active_branch().entries().len(), 2); assert_eq!( selected.projected_messages().unwrap()[0].blocks, Message::with_text(Role::User, "tiny").blocks ); let raw = selected .active_branch() .entries() .iter() .find(|entry| entry.id == root.id) .unwrap(); assert_eq!(selected.content(raw).unwrap(), original); let mut raw_count = 0; reader .replay_at(parent, config.frame_bytes_max, |_| { raw_count += 1; Ok(()) }) .unwrap(); assert_eq!(raw_count, count + 2); println!( "restore count={count}: resident={resident}, peak={}", cost.peak ); // Reuse caller-owned edits: only implementation working memory is measured. let large_edit = [ContextEdit { target: root.id, replacement: Some(ContextReplacement::Text(large.clone())), }]; let tiny_edit = [ContextEdit { target: root.id, replacement: Some(ContextReplacement::Text("tiny".into())), }]; let (live_effective, live_cost) = measure(Scope::IoRequest, || { for _ in 0..count { selected .edit_context_with(&large_edit, |entry, bytes| { writer.append_entry(entry, bytes) }) .unwrap(); } selected .edit_context_with(&tiny_edit, |entry, bytes| writer.append_entry(entry, bytes)) .unwrap(); selected.projected_messages().unwrap() }); assert_eq!(live_effective, effective); let live_resident = snapshot(Scope::IoRequest).live.saturating_sub(before); if let Some(first) = live_residency { assert_eq!( live_resident, first, "live history growth must not increase residency" ); } live_residency = Some(live_resident); println!( "live count={count}: resident={live_resident}, peak={}", live_cost.peak ); assert!( live_resident <= 1536, "{count} live replacements retain {live_resident}" ); assert!( live_cost.peak <= 1536 * 1024, "{count} live replacements peak {}", live_cost.peak ); assert_eq!( selected.projected_messages().unwrap()[0].blocks, Message::with_text(Role::User, "tiny").blocks ); assert_eq!(selected.active_branch().entries().len(), 2); let queued = Message::with_text(Role::User, "queued"); let receipt = writer.admit(&queued, None, None).unwrap(); let before_claim = snapshot(Scope::IoRequest).live; let ((claimed, request), claim_cost) = measure(Scope::IoRequest, || { let claimed = writer.claim(receipt.submission_id).unwrap(); let request = claimed.projected_messages().unwrap(); (claimed, request) }); let claim_resident = snapshot(Scope::IoRequest).live.saturating_sub(before_claim); println!( "claim count={count}: resident={claim_resident}, peak={}", claim_cost.peak ); if let Some(first) = claimed_residency { assert_eq!(claim_resident, first); } claimed_residency = Some(claim_resident); assert!(claim_resident <= 1536, "claim retains {claim_resident}"); assert!( claim_cost.peak <= 1536 * 1024, "claim peak {}", claim_cost.peak ); assert_eq!(request.len(), 2); assert_eq!(request[0], effective[0]); assert_eq!(request[1], queued); drop(claimed); drop(request); drop(selected); drop(reader); drop(writer); std::fs::remove_file(path).unwrap(); } }