repositories / smith
smith
There are many coding harnesses - but this one is fast
owned by admin
smith-core/tests/admission.rs
Raw//! Durable admission boundary (`SMH-SPEC-SPEC0001`, Submission and receipts).
use smith::config::Config;
use smith::message::{Message, Role};
use smith_core::session::{EntryContent, Session};
use smith_core::store::{SessionReader, create_session, open_session};
struct TestDirectory(std::path::PathBuf);
impl TestDirectory {
fn new() -> Self {
let path =
std::env::temp_dir().join(format!("smith-admission-{}", smith::id::SessionId::new()));
std::fs::create_dir(&path).unwrap();
Self(path)
}
fn path(&self) -> &std::path::Path {
&self.0
}
}
impl Drop for TestDirectory {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
#[test]
fn forked_admissions_are_history_not_owned_work_or_dedup_keys() {
let directory = TestDirectory::new();
let path = directory.path().join("source.smh");
let fork = directory.path().join("fork.smh");
let config = Config::default_valid();
let mut source = create_session(&path, &Session::new(), config.clone()).unwrap();
let input = Message::with_text(Role::User, "hello");
let receipt = source.writer.admit(&input, Some("same-key"), None).unwrap();
source.writer.claim(receipt.submission_id).unwrap();
source
.writer
.settle(
receipt.submission_id,
Some(smith::stream::StopReason::EndTurn),
None,
)
.unwrap();
let mut forked = SessionReader::open(&path)
.unwrap()
.fork_at(receipt.submission_id, &fork, config.clone())
.unwrap();
let reader = SessionReader::open(&fork).unwrap();
reader.validate_work(config.frame_bytes_max).unwrap();
reader
.reject_pending_admissions(config.frame_bytes_max)
.unwrap();
assert_eq!(
reader.selected_tip(config.frame_bytes_max).unwrap(),
Some(receipt.submission_id)
);
assert_eq!(
reader
.receipt(receipt.submission_id, config.frame_bytes_max)
.unwrap(),
None
);
assert_eq!(
reader
.work_status(receipt.submission_id, config.frame_bytes_max)
.unwrap(),
None
);
let mut origins = Vec::new();
let mut inherited_key = None;
reader
.scan(config.frame_bytes_max, |frame| {
match frame {
smith_core::frame::Frame::Origin { source, begin } => origins.push((source, begin)),
smith_core::frame::Frame::Admission { entry, key } => {
assert_eq!(entry.id, receipt.submission_id);
inherited_key = key;
}
_ => {}
}
Ok(())
})
.unwrap();
assert_eq!(
origins,
[(receipt.session_id, true), (receipt.session_id, false)]
);
assert_eq!(inherited_key.as_deref(), Some("same-key"));
assert!(forked.writer.claim(receipt.submission_id).is_err());
let own = forked.writer.admit(&input, Some("same-key"), None).unwrap();
assert_ne!(own.submission_id, receipt.submission_id);
assert_ne!(own.session_id, receipt.session_id);
forked.writer.claim(own.submission_id).unwrap();
forked
.writer
.settle(
own.submission_id,
Some(smith::stream::StopReason::EndTurn),
None,
)
.unwrap();
let reader = SessionReader::open(&fork).unwrap();
reader.validate_work(config.frame_bytes_max).unwrap();
let mut frames = Vec::new();
reader
.scan(config.frame_bytes_max, |frame| {
frames.push(frame);
Ok(())
})
.unwrap();
Session::from_frames(&frames).unwrap();
}
#[test]
fn claimed_work_cannot_be_dispatched_again_after_reopen() {
let directory = TestDirectory::new();
let path = directory.path().join("session.smh");
let config = Config::default_valid();
let mut opened = create_session(&path, &Session::new(), config.clone()).unwrap();
let receipt = opened
.writer
.admit(&Message::with_text(Role::User, "hello"), None, None)
.unwrap();
let second = opened
.writer
.admit(
&Message::with_text(Role::User, "later queued input"),
None,
None,
)
.unwrap();
let before = std::fs::metadata(&path).unwrap().len();
assert!(opened.writer.claim(second.submission_id).is_err());
assert_eq!(std::fs::metadata(&path).unwrap().len(), before);
let claimed = opened.writer.claim(receipt.submission_id).unwrap();
assert_eq!(claimed.active_branch().entries().len(), 1);
drop(opened);
let mut opened = open_session(&path, config.clone()).unwrap();
assert!(opened.writer.claim(receipt.submission_id).is_err());
assert_eq!(
SessionReader::open(&path)
.unwrap()
.work_status(receipt.submission_id, config.frame_bytes_max)
.unwrap(),
Some(smith_core::store::WorkStatus::Running)
);
}
#[test]
fn claim_rejects_ancestry_exceeding_resident_preparation_bytes_before_persistence() {
let directory = TestDirectory::new();
let path = directory.path().join("session.smh");
let mut config = Config::default_valid();
config.frame_bytes_max = 1024;
let mut history = Session::new();
for _ in 0..2 {
history
.append(smith_core::session::EntryContentRef::Message(
&Message::with_text(Role::User, "x".repeat(600)),
))
.unwrap();
}
let mut opened = create_session(&path, &history, config.clone()).unwrap();
let receipt = opened
.writer
.admit(&Message::with_text(Role::User, "queued"), None, None)
.unwrap();
let before = std::fs::metadata(&path).unwrap().len();
assert!(opened.writer.claim(receipt.submission_id).is_err());
assert_eq!(std::fs::metadata(&path).unwrap().len(), before);
assert_eq!(
SessionReader::open(&path)
.unwrap()
.work_status(receipt.submission_id, config.frame_bytes_max)
.unwrap(),
Some(smith_core::store::WorkStatus::Queued)
);
}
#[test]
fn claim_rejects_ancestry_exceeding_resident_preparation_items_before_persistence() {
let directory = TestDirectory::new();
let path = directory.path().join("session.smh");
let config = Config::default_valid();
let mut history = Session::new();
for _ in 0..=smith_core::store::CLAIM_RESIDENT_ITEMS_MAX {
history
.append(smith_core::session::EntryContentRef::Message(
&Message::with_text(Role::User, "x"),
))
.unwrap();
}
let mut opened = create_session(&path, &history, config.clone()).unwrap();
let receipt = opened
.writer
.admit(&Message::with_text(Role::User, "queued"), None, None)
.unwrap();
let before = std::fs::metadata(&path).unwrap().len();
assert!(opened.writer.claim(receipt.submission_id).is_err());
assert_eq!(std::fs::metadata(&path).unwrap().len(), before);
assert_eq!(
SessionReader::open(&path)
.unwrap()
.work_status(receipt.submission_id, config.frame_bytes_max)
.unwrap(),
Some(smith_core::store::WorkStatus::Queued)
);
}
#[test]
fn terminal_output_and_receipt_are_one_commit() {
use smith_core::session::EntryContentRef;
use smith_core::store::WorkStatus;
let directory = TestDirectory::new();
let path = directory.path().join("session.smh");
let config = Config::default_valid();
let mut opened = create_session(&path, &Session::new(), config.clone()).unwrap();
let receipt = opened
.writer
.admit(&Message::with_text(Role::User, "hello"), None, None)
.unwrap();
let mut session = opened.writer.claim(receipt.submission_id).unwrap();
let start = std::fs::metadata(&path).unwrap().len();
let entry = *session
.append(EntryContentRef::Message(&Message::with_text(
Role::Assistant,
"answer",
)))
.unwrap();
let output = smith_core::session::EntryFrame {
id: entry.id,
parent: entry.parent,
timestamp_ms: entry.timestamp_ms,
kind: entry.kind,
content: session.bytes(&entry).to_vec(),
};
opened
.writer
.settle(
receipt.submission_id,
Some(smith::stream::StopReason::EndTurn),
Some(output),
)
.unwrap();
drop(opened);
let bytes = std::fs::read(&path).unwrap();
for end in start as usize..bytes.len() {
let torn = directory.path().join("torn.smh");
std::fs::write(&torn, &bytes[..end]).unwrap();
let reader = SessionReader::open(&torn).unwrap();
assert_eq!(
reader
.work_status(receipt.submission_id, config.frame_bytes_max)
.unwrap(),
Some(WorkStatus::Running)
);
assert_eq!(
reader
.selected_session(config.frame_bytes_max)
.unwrap()
.active_branch()
.entries()
.len(),
1
);
}
let reader = SessionReader::open(&path).unwrap();
assert_eq!(
reader
.work_status(receipt.submission_id, config.frame_bytes_max)
.unwrap(),
Some(WorkStatus::Terminal(Some(
smith::stream::StopReason::EndTurn
)))
);
assert_eq!(
reader
.selected_session(config.frame_bytes_max)
.unwrap()
.active_branch()
.entries()
.len(),
2
);
reader.verify_full(config.frame_bytes_max).unwrap();
let output = reader
.work_output(receipt.submission_id, config.frame_bytes_max)
.unwrap();
assert!(output.is_some());
let fork_path = directory.path().join("fork.smh");
let forked = reader
.fork_at(output.unwrap(), &fork_path, config.clone())
.unwrap();
assert_eq!(forked.session.active_branch().entries().len(), 2);
drop(forked);
let fork_reader = SessionReader::open(&fork_path).unwrap();
fork_reader.verify_full(config.frame_bytes_max).unwrap();
fork_reader
.reject_pending_admissions(config.frame_bytes_max)
.unwrap();
assert_eq!(
fork_reader
.work_status(receipt.submission_id, config.frame_bytes_max)
.unwrap(),
None
);
let mut terminal = false;
fork_reader
.scan_reporting(
config.frame_bytes_max,
|frame| {
if let smith_core::frame::Frame::Settlement {
submission_id,
output: inherited,
..
} = frame
{
assert_eq!(submission_id, receipt.submission_id);
assert_eq!(inherited.map(|entry| entry.id), output);
terminal = true;
}
Ok(true)
},
Err,
)
.unwrap();
assert!(terminal);
let mut frames = Vec::new();
fork_reader
.scan(config.frame_bytes_max, |frame| {
frames.push(frame);
Ok(())
})
.unwrap();
Session::from_frames(&frames).unwrap();
drop(fork_reader);
smith_core::store::repair_session(&fork_path, config.frame_bytes_max).unwrap();
SessionReader::open(&fork_path)
.unwrap()
.verify_full(config.frame_bytes_max)
.unwrap();
drop(reader);
smith_core::store::repair_session(&path, config.frame_bytes_max).unwrap();
let reader = SessionReader::open(&path).unwrap();
assert_eq!(
reader
.work_output(receipt.submission_id, config.frame_bytes_max)
.unwrap(),
output
);
reader.verify_full(config.frame_bytes_max).unwrap();
}
#[test]
fn work_fsm_rejects_duplicate_claim_settlement_without_claim_and_non_fifo_claim() {
use smith_core::frame::Frame;
for case in 0..4 {
let directory = TestDirectory::new();
let path = directory.path().join("session.smh");
let config = Config::default_valid();
let mut opened = create_session(&path, &Session::new(), config.clone()).unwrap();
let first = opened
.writer
.admit(&Message::with_text(Role::User, "first"), None, None)
.unwrap();
let second = opened
.writer
.admit(&Message::with_text(Role::User, "second"), None, None)
.unwrap();
match case {
0 => {
opened.writer.claim(first.submission_id).unwrap();
opened
.writer
.append_frame(&Frame::Claim {
submission_id: first.submission_id,
})
.unwrap();
}
1 => opened
.writer
.append_frame(&Frame::Settlement {
submission_id: first.submission_id,
reason: None,
output: None,
})
.unwrap(),
2 => opened
.writer
.append_frame(&Frame::Claim {
submission_id: second.submission_id,
})
.unwrap(),
_ => {
opened.writer.claim(first.submission_id).unwrap();
opened
.writer
.settle(first.submission_id, None, None)
.unwrap();
opened
.writer
.append_frame(&Frame::Settlement {
submission_id: first.submission_id,
reason: None,
output: None,
})
.unwrap();
}
}
drop(opened);
let reader = SessionReader::open(&path).unwrap();
assert!(
reader
.work_status(first.submission_id, config.frame_bytes_max)
.is_err()
);
assert!(reader.selected_session(config.frame_bytes_max).is_err());
assert!(reader.verify_full(config.frame_bytes_max).is_err());
}
}
#[test]
fn repair_never_discards_corrupt_required_work_evidence() {
let directory = TestDirectory::new();
let path = directory.path().join("session.smh");
let mut opened = create_session(&path, &Session::new(), Config::default_valid()).unwrap();
opened
.writer
.admit(&Message::with_text(Role::User, "hello"), None, None)
.unwrap();
drop(opened);
let mut bytes = std::fs::read(&path).unwrap();
let last = bytes.len() - 1;
bytes[last] ^= 1;
std::fs::write(&path, &bytes).unwrap();
assert!(
smith_core::store::repair_session(&path, Config::default_valid().frame_bytes_max).is_err()
);
assert_eq!(std::fs::read(&path).unwrap(), bytes);
}
#[test]
fn memory_projection_rejects_repeated_archived_admission() {
let directory = TestDirectory::new();
let path = directory.path().join("session.smh");
let mut opened = create_session(&path, &Session::new(), Config::default_valid()).unwrap();
let receipt = opened
.writer
.admit(&Message::with_text(Role::User, "hello"), Some("key"), None)
.unwrap();
opened.writer.claim(receipt.submission_id).unwrap();
opened
.writer
.settle(receipt.submission_id, None, None)
.unwrap();
let bytes = std::fs::read(&path).unwrap();
let mut frames = smith_core::frame::read_frames(
&mut &bytes[smith_core::frame::FILE_PROLOGUE.len()..],
Config::default_valid().frame_bytes_max,
)
.unwrap()
.frames;
let duplicate = frames
.iter()
.find(|frame| matches!(frame, smith_core::frame::Frame::Admission { .. }))
.unwrap()
.clone();
frames.push(duplicate);
assert!(Session::from_frames(&frames).is_err());
}
#[test]
fn invalid_terminal_output_is_rejected_before_any_bytes_are_written() {
let directory = TestDirectory::new();
let path = directory.path().join("session.smh");
let mut opened = create_session(&path, &Session::new(), Config::default_valid()).unwrap();
let receipt = opened
.writer
.admit(&Message::with_text(Role::User, "hello"), None, None)
.unwrap();
opened.writer.claim(receipt.submission_id).unwrap();
let output = smith_core::session::EntryFrame::new(
Some(receipt.submission_id),
&EntryContent::Message(Message::with_text(Role::User, "not assistant")),
)
.unwrap();
let before = std::fs::read(&path).unwrap();
assert!(
opened
.writer
.settle(receipt.submission_id, None, Some(output))
.is_err()
);
assert_eq!(std::fs::read(&path).unwrap(), before);
}
#[test]
fn settlement_cannot_include_another_pending_input_in_its_branch() {
let directory = TestDirectory::new();
let path = directory.path().join("session.smh");
let config = Config::default_valid();
let mut opened = create_session(&path, &Session::new(), config.clone()).unwrap();
let first = opened
.writer
.admit(&Message::with_text(Role::User, "first"), None, None)
.unwrap();
opened
.writer
.admit(&Message::with_text(Role::User, "second"), None, None)
.unwrap();
opened.writer.claim(first.submission_id).unwrap();
let reader = SessionReader::open(&path).unwrap();
let mut session = reader.selected_session(config.frame_bytes_max).unwrap();
let entry = *session
.append(&EntryContent::Message(Message::with_text(
Role::Assistant,
"wrong branch",
)))
.unwrap();
let output = smith_core::session::EntryFrame {
id: entry.id,
parent: entry.parent,
timestamp_ms: entry.timestamp_ms,
kind: entry.kind,
content: session.bytes(&entry).to_vec(),
};
assert!(
opened
.writer
.settle(
first.submission_id,
Some(smith::stream::StopReason::EndTurn),
Some(output)
)
.is_err()
);
}
#[test]
fn incomplete_claim_does_not_expose_running_and_terminal_work_releases_live_capacity() {
let directory = TestDirectory::new();
let path = directory.path().join("session.smh");
let config = Config::default_valid();
let mut opened = create_session(&path, &Session::new(), config.clone()).unwrap();
let first = opened
.writer
.admit(
&Message::with_text(Role::User, "first"),
Some("archive"),
None,
)
.unwrap();
let start = std::fs::metadata(&path).unwrap().len() as usize;
opened.writer.claim(first.submission_id).unwrap();
let bytes = std::fs::read(&path).unwrap();
for end in start..bytes.len() {
let torn = directory.path().join("torn.smh");
std::fs::write(&torn, &bytes[..end]).unwrap();
assert_eq!(
SessionReader::open(&torn)
.unwrap()
.work_status(first.submission_id, config.frame_bytes_max)
.unwrap(),
Some(smith_core::store::WorkStatus::Queued)
);
}
opened
.writer
.settle(first.submission_id, None, None)
.unwrap();
for _ in 0..smith_core::store::PENDING_ADMISSIONS_MAX {
opened
.writer
.admit(&Message::with_text(Role::User, "pending"), None, None)
.unwrap();
}
assert_eq!(
opened
.writer
.admit(
&Message::with_text(Role::User, "first"),
Some("archive"),
None
)
.unwrap(),
first
);
assert_eq!(
SessionReader::open(&path)
.unwrap()
.work_status(first.submission_id, config.frame_bytes_max)
.unwrap(),
Some(smith_core::store::WorkStatus::Terminal(None))
);
}
proptest::proptest! {
#[test]
fn required_work_codec_roundtrips_identity_and_outcome(bits in proptest::prelude::any::<u128>(), tag in 0u8..7) {
use smith_core::frame::{Frame, read_frames};
use smith::stream::StopReason;
let reason = [None, Some(StopReason::EndTurn), Some(StopReason::Limit), Some(StopReason::Abort), Some(StopReason::StopSequence), Some(StopReason::Refusal), Some(StopReason::ContentFilter)][usize::from(tag)];
for frame in [Frame::Claim { submission_id: smith::id::EntryId::from_u128(bits) }, Frame::Settlement { submission_id: smith::id::EntryId::from_u128(bits), reason, output: None }] {
let bytes = frame.encode(1024).unwrap();
let decoded = read_frames(&mut bytes.as_slice(), 1024).unwrap();
proptest::prop_assert_eq!(decoded.frames, vec![frame]);
}
}
}
#[test]
fn lost_response_reopens_original_admission_without_another_input() {
let directory = TestDirectory::new();
let path = directory.path().join("session.smh");
let mut opened = create_session(&path, &Session::new(), Config::default_valid()).unwrap();
let input = Message::with_text(Role::User, "hello");
let original = opened
.writer
.admit(&input, Some("retry-key"), None)
.unwrap();
drop(opened);
let mut opened = open_session(&path, Config::default_valid()).unwrap();
let repeated = opened
.writer
.admit(&input, Some("retry-key"), None)
.unwrap();
assert_eq!(repeated, original);
let reader = SessionReader::open(&path).unwrap();
let entries = reader
.selected_session(Config::default_valid().frame_bytes_max)
.unwrap();
assert_eq!(entries.active_branch().entries().len(), 1);
assert_eq!(
entries
.content(&entries.active_branch().entries()[0])
.unwrap(),
EntryContent::Message(input)
);
}
#[test]
fn keyed_retry_preserves_original_destination_after_other_admissions() {
let directory = TestDirectory::new();
let path = directory.path().join("session.smh");
let mut opened = create_session(&path, &Session::new(), Config::default_valid()).unwrap();
let first = opened
.writer
.admit(&Message::with_text(Role::User, "first"), Some("key"), None)
.unwrap();
let second = opened
.writer
.admit(&Message::with_text(Role::User, "second"), None, None)
.unwrap();
assert_eq!(second.destination, Some(first.submission_id));
drop(opened);
let mut opened = open_session(&path, Config::default_valid()).unwrap();
let length = std::fs::metadata(&path).unwrap().len();
assert_eq!(
opened
.writer
.admit(&Message::with_text(Role::User, "first"), Some("key"), None)
.unwrap(),
first
);
assert!(
opened
.writer
.admit(
&Message::with_text(Role::User, "first"),
Some("key"),
Some(second.submission_id)
)
.is_err()
);
assert_eq!(std::fs::metadata(&path).unwrap().len(), length);
}
#[test]
fn pending_admission_limit_rejects_without_partial_changes() {
let directory = TestDirectory::new();
let path = directory.path().join("session.smh");
let mut opened = create_session(&path, &Session::new(), Config::default_valid()).unwrap();
for _ in 0..smith_core::store::PENDING_ADMISSIONS_MAX {
opened
.writer
.admit(&Message::with_text(Role::User, "input"), None, None)
.unwrap();
}
let length = std::fs::metadata(&path).unwrap().len();
assert!(
opened
.writer
.admit(&Message::with_text(Role::User, "overflow"), None, None)
.is_err()
);
assert_eq!(std::fs::metadata(&path).unwrap().len(), length);
}
#[test]
fn unsupported_required_semantics_fail_closed() {
use smith_core::frame::{FILE_PROLOGUE, Frame, read_frames};
let frame = Frame::Unknown {
kind: 0x8002,
version: 1,
payload: vec![0],
}
.encode(1024)
.unwrap();
assert!(read_frames(&mut frame.as_slice(), 1024).is_err());
let directory = TestDirectory::new();
let path = directory.path().join("unsupported.smh");
let mut bytes = FILE_PROLOGUE.to_vec();
bytes.extend(frame);
std::fs::write(&path, bytes).unwrap();
assert!(
SessionReader::open(&path)
.unwrap()
.selected_session(1024)
.is_err()
);
}
#[test]
#[cfg(target_arch = "x86_64")]
fn receipt_size_is_two_cache_lines_or_less() {
assert!(std::mem::size_of::<smith_core::store::AdmissionReceipt>() <= 64);
}
#[test]
fn conflicting_key_never_changes_file() {
let directory = TestDirectory::new();
let path = directory.path().join("session.smh");
let mut opened = create_session(&path, &Session::new(), Config::default_valid()).unwrap();
opened
.writer
.admit(&Message::with_text(Role::User, "hello"), Some("key"), None)
.unwrap();
let length = std::fs::metadata(&path).unwrap().len();
assert!(
opened
.writer
.admit(
&Message::with_text(Role::User, "different"),
Some("key"),
None
)
.is_err()
);
assert_eq!(std::fs::metadata(&path).unwrap().len(), length);
}
#[test]
fn torn_admission_exposes_neither_input_nor_receipt() {
let directory = TestDirectory::new();
let path = directory.path().join("session.smh");
let mut opened = create_session(&path, &Session::new(), Config::default_valid()).unwrap();
let start = std::fs::metadata(&path).unwrap().len();
let receipt = opened
.writer
.admit(&Message::with_text(Role::User, "hello"), None, None)
.unwrap();
drop(opened);
let end = std::fs::metadata(&path).unwrap().len();
for length in start..end {
let copy = directory.path().join("torn.smh");
std::fs::copy(&path, ©).unwrap();
std::fs::OpenOptions::new()
.write(true)
.open(©)
.unwrap()
.set_len(length)
.unwrap();
let reader = SessionReader::open(©).unwrap();
assert!(
reader
.receipt(
receipt.submission_id,
Config::default_valid().frame_bytes_max
)
.unwrap()
.is_none()
);
assert!(
reader
.selected_session(Config::default_valid().frame_bytes_max)
.unwrap()
.active_branch()
.entries()
.is_empty()
);
}
}
#[test]
fn edited_context_still_enforces_resident_preparation_bytes_before_claim() {
use smith_core::context::{ContextEdit, ContextReplacement};
use smith_core::session::EntryContent;
let directory = TestDirectory::new();
let path = directory.path().join("session.smh");
let mut config = Config::default_valid();
config.frame_bytes_max = 1024;
let mut history = Session::new();
let first = history
.append(&EntryContent::Message(Message::with_text(
Role::User,
"x".repeat(600),
)))
.unwrap()
.id;
history
.append(&EntryContent::Message(Message::with_text(
Role::User,
"y".repeat(600),
)))
.unwrap();
let mut opened = create_session(&path, &history, config.clone()).unwrap();
opened
.writer
.edit_context(&[ContextEdit {
target: first,
replacement: Some(ContextReplacement::Text("z".repeat(600))),
}])
.unwrap();
let receipt = opened
.writer
.admit(&Message::with_text(Role::User, "queued"), None, None)
.unwrap();
let before = std::fs::metadata(&path).unwrap().len();
assert!(opened.writer.claim(receipt.submission_id).is_err());
assert_eq!(std::fs::metadata(&path).unwrap().len(), before);
assert_eq!(
SessionReader::open(&path)
.unwrap()
.work_status(receipt.submission_id, config.frame_bytes_max)
.unwrap(),
Some(smith_core::store::WorkStatus::Queued)
);
}