Luigit
repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by admin

smith-core/src/store/nested_tests.rs

Raw
//! SMH-SPEC-SPEC0001: required nested completion recovery and ordinal linkage.
use super::*;
use crate::frame::Frame;
use crate::metadata::{
    NESTED_COMPLETION_KIND, NESTED_INTENT_KIND, NestedEffectCompletion, NestedEffectIntent, encode,
};
use crate::session::EntryContent;
use smith::error::MutationState;

#[path = "../../tests/support/child.rs"]
mod child_fixture;

fn setup() -> (PathBuf, OpenSession, Frame, Frame) {
    let (path, mut opened, mut outer) = super::commit_tests::setup(MutationState::Applied);
    let Frame::Commit {
        checkpoint,
        entries,
    } = &mut outer
    else {
        unreachable!()
    };
    let intent = EntryFrame::new(
        entries[0].parent,
        &EntryContent::Meta {
            kind: NESTED_INTENT_KIND.into(),
            detail: encode(&NestedEffectIntent {
                submission_id: checkpoint.submission_id,
                call_id: checkpoint.call_id,
                index: 0,
                operation: "write",
                component: None,
                input: "redacted",
            })
            .unwrap(),
        },
    )
    .unwrap();
    let completion = EntryFrame::new(
        Some(intent.id),
        &EntryContent::Meta {
            kind: NESTED_COMPLETION_KIND.into(),
            detail: encode(&NestedEffectCompletion {
                submission_id: checkpoint.submission_id,
                call_id: checkpoint.call_id,
                index: 0,
                ok: true,
                mutation_state: MutationState::Applied,
                output: "written".to_string(),
            })
            .unwrap(),
        },
    )
    .unwrap();
    entries[0].parent = Some(completion.id);
    opened
        .writer
        .append_frame(&Frame::Known { entry: intent })
        .unwrap();
    (
        path,
        opened,
        Frame::NestedCommit { entry: completion },
        outer,
    )
}

#[test]
fn malformed_nested_intent_metadata_leaves_file_unchanged() {
    let (path, mut opened, _, _) = setup();
    let generic = EntryFrame::new(
        None,
        &EntryContent::Meta {
            kind: "event".into(),
            detail: smith::structured::StructuredData::from_json("{}").unwrap(),
        },
    )
    .unwrap();
    let generic_kind_len = u32::from_le_bytes(generic.content[1..5].try_into().unwrap()) as usize;
    let detail_offset = 1 + 4 + generic_kind_len;
    let mut malformed_content = vec![3];
    malformed_content.extend_from_slice(&(NESTED_INTENT_KIND.len() as u32).to_le_bytes());
    malformed_content.extend_from_slice(NESTED_INTENT_KIND.as_bytes());
    malformed_content.extend_from_slice(&generic.content[detail_offset..]);
    let record = Entry {
        id: generic.id,
        parent: generic.parent,
        timestamp_ms: generic.timestamp_ms,
        kind: generic.kind,
        content: crate::session::Span { offset: 0, len: 0 },
    };
    let before = std::fs::read(&path).unwrap();

    assert!(
        opened
            .writer
            .append_entry(&record, &malformed_content)
            .is_err()
    );
    assert_eq!(std::fs::read(&path).unwrap(), before);

    drop(opened);
    super::commit_tests::clear(&path);
}

#[test]
fn append_entry_rejects_nested_completion_without_copying_content() {
    // The existing pin includes OS path construction: TMPDIR's spelling is
    // four bytes and the canonical session path is 80 bytes (/tmp + original
    // 75-byte basename). Normalize those inputs, not the allocation ceilings.
    const CHILD: &str = "SMITH_NESTED_ALLOCATION_CHILD";
    if std::env::var_os(CHILD).is_none() {
        let root = std::env::temp_dir();
        let directory = root.join("pins");
        let created = match std::fs::create_dir(&directory) {
            Ok(()) => true,
            Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
                assert!(
                    std::fs::symlink_metadata(&directory).unwrap().is_dir(),
                    "controlled fixture directory must not be a symlink or file"
                );
                false
            }
            Err(error) => panic!("cannot create controlled allocation fixture: {error}"),
        };
        let output = child_fixture::run(std::process::Command::new(std::env::current_exe().unwrap())
            .args(["--exact", "store::nested_tests::append_entry_rejects_nested_completion_without_copying_content"])
            .current_dir(root)
            .env("TMPDIR", "pins")
            .env(CHILD, "1"));
        if created {
            std::fs::remove_dir(directory).unwrap();
        }
        assert!(
            output.status.success(),
            "nested allocation child failed: {}{}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
        return;
    }
    let root = std::fs::canonicalize(std::env::temp_dir()).unwrap();
    let filename_bytes = 80_usize
        .checked_sub(root.as_os_str().as_encoded_bytes().len() + 1)
        .expect("controlled fixture root must leave room for an 80-byte canonical session path");
    assert!(
        filename_bytes >= 8,
        "controlled fixture root must leave room for a unique basename"
    );
    let stem: String = SessionId::new()
        .to_string()
        .chars()
        .cycle()
        .take(filename_bytes - 4)
        .collect();
    let path = root.join(format!("{stem}.smh"));
    let mut opened = create_session(&path, &Session::new(), Config::default_valid()).unwrap();
    let content = EntryFrame::new(
        None,
        &EntryContent::Meta {
            kind: NESTED_COMPLETION_KIND.into(),
            detail: encode(&NestedEffectCompletion {
                submission_id: EntryId::new(),
                call_id: smith::id::ToolCallId::new(),
                index: 0,
                ok: false,
                mutation_state: MutationState::Unknown,
                output: String::from("rejected"),
            })
            .unwrap(),
        },
    )
    .unwrap();
    let record = Entry {
        id: content.id,
        parent: content.parent,
        timestamp_ms: content.timestamp_ms,
        kind: content.kind,
        content: crate::session::Span { offset: 0, len: 0 },
    };
    let before = std::fs::read(&path).unwrap();
    let (result, cost) = smith_alloc::measure(smith_alloc::Scope::IoRequest, || {
        opened.writer.append_entry(&record, &content.content)
    });
    assert_eq!(content.content.len(), 204);
    assert_eq!(cost.allocations, 30);
    assert_eq!(cost.bytes, 133_678);
    assert!(result.is_err());
    assert_eq!(std::fs::read(&path).unwrap(), before);
    drop(opened);
    super::commit_tests::clear(&path);
}

#[test]
fn recovering_root_cancel_intent_never_fabricates_abort_for_unknown_effect() {
    let (path, mut opened, _nested, outer) = setup();
    let Frame::Commit { checkpoint, .. } = outer else {
        unreachable!()
    };
    let id = checkpoint.submission_id;
    drop(opened);
    opened = open_session(&path, Config::default_valid()).unwrap();
    opened.writer.request_cancel(id).unwrap();
    assert!(
        opened
            .writer
            .settle(id, Some(smith::stream::StopReason::Abort), None)
            .is_err()
    );
    assert!(opened.writer.claim(id).is_err());
    drop(opened);
    let limit = Config::default_valid().frame_bytes_max;
    repair_session(&path, limit).unwrap();
    let reader = SessionReader::open(&path).unwrap();
    let work = reader.inspect_work(id, limit).unwrap().unwrap();
    assert_eq!(work.status, WorkStatus::Running);
    assert!(work.cancel_requested);
    drop(reader);
    super::commit_tests::clear(&path);
}

#[test]
fn nested_completion_and_progress_reopen_and_repair_together() {
    let (path, mut opened, nested, outer) = setup();
    let completion = &nested.entries()[0];
    let intent_id = completion.parent.unwrap();
    let (intent, intent_offset) = SessionReader::open(&path)
        .unwrap()
        .find_entry(intent_id, Config::default_valid().frame_bytes_max)
        .unwrap()
        .unwrap();
    opened
        .writer
        .append_frame(&Frame::Index {
            id: intent_id,
            offset: intent_offset,
            parent: intent.parent,
            parent_offset: intent.parent.and_then(|parent| {
                SessionReader::open(&path)
                    .unwrap()
                    .find_entry(parent, Config::default_valid().frame_bytes_max)
                    .unwrap()
                    .map(|(_, offset)| offset)
            }),
        })
        .unwrap();
    opened
        .writer
        .append_frame(&Frame::Checkpoint {
            id: intent_id,
            offset: intent_offset,
        })
        .unwrap();
    opened.writer.append_frame(&nested).unwrap();
    assert!(opened.writer.append_frame(&nested).is_err());
    opened.writer.append_frame(&outer).unwrap();
    drop(opened);
    let limit = Config::default_valid().frame_bytes_max;
    let reader = SessionReader::open(&path).unwrap();
    reader.verify_full(limit).unwrap();
    let session = reader.selected_session(limit).unwrap();
    assert_eq!(session.active_branch().entries().len(), 7);
    let mut frames = Vec::new();
    reader
        .scan(limit, |frame| {
            frames.push(frame);
            Ok(())
        })
        .unwrap();
    assert_eq!(
        Session::from_frames(&frames)
            .unwrap()
            .active_branch()
            .entries()
            .len(),
        7
    );
    let entry = &nested.entries()[0];
    assert_eq!(
        reader.find_entry(entry.id, limit).unwrap().unwrap().0,
        *entry
    );
    drop(reader);
    open_session(&path, Config::default_valid())
        .unwrap()
        .writer
        .select_entry(nested.entries()[0].id)
        .unwrap();
    repair_session(&path, limit).unwrap();
    let repaired = SessionReader::open(&path).unwrap();
    repaired.verify_full(limit).unwrap();
    assert_eq!(
        repaired.selected_tip(limit).unwrap(),
        Some(nested.entries()[0].id)
    );
    assert_eq!(
        repaired
            .selected_session(limit)
            .unwrap()
            .active_branch()
            .entries()
            .len(),
        4
    );
    assert_eq!(
        repaired
            .nested_payload(nested.entries()[0].id, limit)
            .unwrap(),
        "written"
    );
    drop(repaired);
    drop(nested);
    super::commit_tests::clear(&path);
}

#[test]
fn checksum_valid_nested_completion_with_wrong_identity_cannot_advance_root() {
    for field in ["root", "call", "index", "parent"] {
        let (path, mut opened, nested, _) = setup();
        let entry = &nested.entries()[0];
        let EntryContent::Meta { detail, .. } = EntryContent::decode(&entry.content).unwrap()
        else {
            unreachable!()
        };
        let mut completion: NestedEffectCompletion = detail.deserialize_as().unwrap();
        let root = completion.submission_id;
        match field {
            "root" => completion.submission_id = EntryId::new(),
            "call" => completion.call_id = smith::id::ToolCallId::new(),
            "index" => completion.index = 1,
            _ => {}
        }
        let parent = if field == "parent" {
            Some(root)
        } else {
            entry.parent
        };
        let wrong = Frame::NestedCommit {
            entry: EntryFrame::new(
                parent,
                &EntryContent::Meta {
                    kind: NESTED_COMPLETION_KIND.into(),
                    detail: encode(&completion).unwrap(),
                },
            )
            .unwrap(),
        };
        let before = std::fs::read(&path).unwrap();
        assert!(opened.writer.append_frame(&wrong).is_err());
        assert_eq!(std::fs::read(&path).unwrap(), before);
        let limit = Config::default_valid().frame_bytes_max;
        OpenOptions::new()
            .append(true)
            .open(&path)
            .unwrap()
            .write_all(&wrong.encode(limit).unwrap())
            .unwrap();
        drop(opened);
        let reader = SessionReader::open(&path).unwrap();
        assert!(reader.work_status(root, limit).is_err());
        assert!(reader.selected_session(limit).is_err());
        drop(reader);
        super::commit_tests::clear(&path);
    }
}

#[test]
fn absent_torn_or_checksum_invalid_nested_completion_never_authorizes_outer_success() {
    for mode in ["absent", "torn", "checksum"] {
        let (path, mut opened, nested, outer) = setup();
        let Frame::Commit { checkpoint, .. } = &outer else {
            unreachable!()
        };
        assert!(opened.writer.append_frame(&outer).is_err());
        let limit = Config::default_valid().frame_bytes_max;
        if mode != "absent" {
            let mut bytes = nested.encode(limit).unwrap();
            if mode == "torn" {
                bytes.truncate(bytes.len() - 1);
            } else {
                bytes[crate::frame::HEADER_LEN + 8] ^= 1;
            }
            OpenOptions::new()
                .append(true)
                .open(&path)
                .unwrap()
                .write_all(&bytes)
                .unwrap();
        }
        drop(opened);
        let reader = SessionReader::open(&path).unwrap();
        let status = reader.work_status(checkpoint.submission_id, limit);
        if mode == "checksum" {
            assert!(
                status.is_err(),
                "damaged required checkpoint must require recovery"
            );
        } else {
            assert_eq!(status.unwrap(), Some(WorkStatus::Running));
            let session = reader.selected_session(limit).unwrap();
            assert_eq!(
                session.mutation_state(checkpoint.call_id).unwrap(),
                MutationState::Unknown
            );
        }
        drop(reader);
        let reopened = open_session(&path, Config::default_valid());
        if let Ok(mut reopened) = reopened {
            assert!(reopened.writer.claim(checkpoint.submission_id).is_err());
            assert!(
                reopened
                    .writer
                    .settle(checkpoint.submission_id, None, None)
                    .is_err()
            );
        }
        super::commit_tests::clear(&path);
    }
}

/// Two open causal records under one admitted outer ToolCall.
fn causal_setup() -> (PathBuf, OpenSession, Frame, Frame, Frame) {
    use crate::metadata::{NestedCausality, encode_nested};
    let (path, mut opened, mut outer) = super::commit_tests::setup(MutationState::Applied);
    let Frame::Commit {
        checkpoint,
        entries,
    } = &mut outer
    else {
        unreachable!()
    };
    let child_id = EntryId::new();
    let primitive_id = EntryId::new();
    let child_causal = NestedCausality {
        effect_id: child_id,
        parent: None,
        invocation: true,
    };
    let primitive_causal = NestedCausality {
        effect_id: primitive_id,
        parent: Some(child_id),
        invocation: false,
    };
    let mut parent = entries[0].parent;
    for (index, causal) in [(0, child_causal), (1, primitive_causal)] {
        let mut intent = EntryFrame::new(
            parent,
            &EntryContent::Meta {
                kind: NESTED_INTENT_KIND.into(),
                detail: encode_nested(
                    NestedEffectIntent {
                        submission_id: checkpoint.submission_id,
                        call_id: checkpoint.call_id,
                        index,
                        operation: "write",
                        component: None,
                        input: "input",
                    },
                    Some(&causal),
                )
                .unwrap(),
            },
        )
        .unwrap();
        intent.id = causal.effect_id;
        parent = Some(intent.id);
        opened
            .writer
            .append_frame(&Frame::Known { entry: intent })
            .unwrap();
    }
    let primitive = EntryFrame::new(
        parent,
        &EntryContent::Meta {
            kind: NESTED_COMPLETION_KIND.into(),
            detail: encode_nested(
                NestedEffectCompletion {
                    submission_id: checkpoint.submission_id,
                    call_id: checkpoint.call_id,
                    index: 1,
                    ok: true,
                    mutation_state: MutationState::Applied,
                    output: "written",
                },
                Some(&primitive_causal),
            )
            .unwrap(),
        },
    )
    .unwrap();
    let child = EntryFrame::new(
        Some(primitive.id),
        &EntryContent::Meta {
            kind: NESTED_COMPLETION_KIND.into(),
            detail: encode_nested(
                NestedEffectCompletion {
                    submission_id: checkpoint.submission_id,
                    call_id: checkpoint.call_id,
                    index: 0,
                    ok: false,
                    mutation_state: MutationState::NotApplied,
                    output: "parent failed",
                },
                Some(&child_causal),
            )
            .unwrap(),
        },
    )
    .unwrap();
    entries[0].parent = Some(child.id);
    (
        path,
        opened,
        Frame::NestedCommit { entry: primitive },
        Frame::NestedCommit { entry: child },
        outer,
    )
}

#[test]
fn causal_completions_reject_wrong_owner_identity_order_and_duplicates_atomically() {
    for defect in [
        "root",
        "call",
        "index",
        "effect",
        "causal-parent",
        "kind",
        "chronology",
        "out-of-order",
    ] {
        let (path, mut opened, primitive, child, outer) = causal_setup();
        let mut invalid = if defect == "out-of-order" {
            child.clone()
        } else {
            primitive.clone()
        };
        let Frame::NestedCommit { entry } = &mut invalid else {
            unreachable!()
        };
        let EntryContent::Meta { detail, .. } = EntryContent::decode(&entry.content).unwrap()
        else {
            unreachable!()
        };
        let mut value = serde_json::to_value(detail).unwrap();
        match defect {
            "root" => value["submission_id"] = serde_json::to_value(EntryId::new()).unwrap(),
            "call" => {
                value["call_id"] = serde_json::to_value(smith::id::ToolCallId::new()).unwrap()
            }
            "index" => value["index"] = 0.into(),
            "effect" => {
                value["causal"]["effect_id"] = serde_json::to_value(EntryId::new()).unwrap()
            }
            "causal-parent" => value["causal"]["parent"] = serde_json::Value::Null,
            "kind" => {
                value["causal"]["invocation"] = true.into();
                value["mutation_state"] = serde_json::to_value(MutationState::NotApplied).unwrap();
            }
            "chronology" => entry.parent = Some(EntryId::new()),
            _ => {}
        }
        entry.content = EntryFrame::new(
            entry.parent,
            &EntryContent::Meta {
                kind: NESTED_COMPLETION_KIND.into(),
                detail: smith::structured::StructuredData::from_serializable(&value).unwrap(),
            },
        )
        .unwrap()
        .content;
        let before = std::fs::read(&path).unwrap();
        assert!(opened.writer.append_frame(&invalid).is_err(), "{defect}");
        assert_eq!(std::fs::read(&path).unwrap(), before, "{defect}");
        opened.writer.append_frame(&primitive).unwrap();
        let before = std::fs::read(&path).unwrap();
        assert!(opened.writer.append_frame(&primitive).is_err());
        assert_eq!(std::fs::read(&path).unwrap(), before);
        opened.writer.append_frame(&child).unwrap();
        opened.writer.append_frame(&outer).unwrap();
        drop(opened);
        SessionReader::open(&path)
            .unwrap()
            .validate_work(Config::default_valid().frame_bytes_max)
            .unwrap();
        super::commit_tests::clear(&path);
    }
}

#[test]
fn interrupted_child_never_authorizes_retry_or_outer_completion_after_repair() {
    let (path, mut opened, primitive, child, outer) = causal_setup();
    opened.writer.append_frame(&primitive).unwrap();
    let root = match &outer {
        Frame::Commit { checkpoint, .. } => checkpoint.submission_id,
        _ => unreachable!(),
    };
    assert!(opened.writer.append_frame(&outer).is_err());
    drop(opened);
    let limit = Config::default_valid().frame_bytes_max;
    repair_session(&path, limit).unwrap();
    let mut opened = open_session(&path, Config::default_valid()).unwrap();
    assert!(opened.writer.claim(root).is_err());
    assert!(opened.writer.settle(root, None, None).is_err());
    let reader = SessionReader::open(&path).unwrap();
    assert_eq!(
        reader.work_status(root, limit).unwrap(),
        Some(WorkStatus::Running)
    );
    let mut kinds = Vec::new();
    crate::trace::replay(&reader, limit, |record| {
        kinds.push(record.kind);
        Ok(())
    })
    .unwrap();
    assert!(kinds.contains(&crate::trace::TraceKind::NestedEffectCompleted));
    assert!(!kinds.contains(&crate::trace::TraceKind::ChildInvocationCompleted));
    assert_eq!(
        reader
            .nested_payload(primitive.entries()[0].id, limit)
            .unwrap(),
        "written"
    );
    // Explicit host completion remains possible; recovery itself never fabricates it.
    opened.writer.append_frame(&child).unwrap();
    opened.writer.append_frame(&outer).unwrap();
    drop(opened);
    super::commit_tests::clear(&path);
}

#[test]
fn child_output_chunk_cannot_borrow_chronological_predecessor_identity() {
    let (path, mut opened, primitive, child, outer) = causal_setup();
    opened.writer.append_frame(&primitive).unwrap();
    let Frame::Commit { checkpoint, .. } = &outer else {
        unreachable!()
    };
    let content =
        crate::content_codec::EntryContentView::decode(&child.entries()[0].content).unwrap();
    let crate::content_codec::EntryContentView::Meta { detail, .. } = content else {
        unreachable!()
    };
    let causal = crate::metadata::nested_causality(detail).unwrap().unwrap();
    // Last physical entry is a completed primitive, not this open invocation.
    let chunk = Frame::NestedPayload {
        chunk: Box::new(crate::frame::NestedPayloadChunk {
            submission_id: checkpoint.submission_id,
            call_id: checkpoint.call_id,
            effect_id: primitive.entries()[0].id,
            index: 0,
            phase: crate::frame::NestedPayloadPhase::Output,
            ordinal: 0,
            bytes: vec![b'x'; 65_536],
        }),
    };
    assert_ne!(causal.effect_id, primitive.entries()[0].id);
    let before = std::fs::read(&path).unwrap();
    assert!(opened.writer.append_frame(&chunk).is_err());
    assert_eq!(std::fs::read(&path).unwrap(), before);
    opened.writer.append_frame(&child).unwrap();
    opened.writer.append_frame(&outer).unwrap();
    drop(opened);
    super::commit_tests::clear(&path);
}