kcode-k1-chat-thread-web-code-check-state 0.1.0

Pure conversation-local latest-check state for K1 Chat Web-code tasks
Documentation
use kcode_k1_chat_boxes::ToolCallId;
use kcode_k1_chat_thread_web_code_check_state::LatestCheckState;
use kcode_k1_chat_web_code_output::{check_failed, check_success};
use kcode_k1_transaction_id::TxId;
use kcode_k1_web_code_document::{CodeDocument, Language};
use kcode_k1_web_code_session::SessionScope;
use kcode_k1_web_package::{AuthorityId, WebFamily, WebId};
use semver::Version;
use std::sync::Arc;

fn call(sequence: u64) -> ToolCallId {
    ToolCallId::new([7; 12], sequence)
}

fn evidence() -> kcode_k1_web_code_session::CheckEvidence {
    let family = WebFamily::new(AuthorityId::new(TxId::from_bytes([1; 12])), "alpha").unwrap();
    let id = WebId::new(family, Version::new(1, 0, 0)).unwrap();
    let document = CodeDocument::new(
        id,
        b"<!-- k1-web/v1\n{\"dependencies\":[]}\n-->\n".to_vec(),
        Language::JavaScript,
        Vec::new(),
    )
    .unwrap();
    let source = Arc::new(document.to_source_package().unwrap());
    let scope = SessionScope::new();
    let opened = scope.recover([9; 12], source.clone()).unwrap();
    scope.mint_evidence([9; 12], &opened, &source).unwrap()
}

fn failed() -> kcode_k1_chat_web_code_output::WebCodeOutput {
    check_failed(
        call(1),
        vec!["message".to_owned()],
        Err("complete failure".to_owned()),
        "diagnostics\nall",
    )
}

#[test]
fn default_clear_and_failure_retag_preserve_exact_output() {
    let mut state = LatestCheckState::new();
    assert!(state.successful_evidence().is_none());
    assert!(state.failed_for_publish(call(2)).is_none());

    let original = failed();
    state.record_failure(original.clone());
    let retagged = state.failed_for_publish(call(8)).unwrap();
    assert_eq!(retagged.tool_call_id, call(8));
    assert_eq!(retagged.messages, original.messages);
    assert_eq!(retagged.result, original.result);
    assert_eq!(retagged.metadata_type, original.metadata_type);
    assert_eq!(retagged.metadata_contents, original.metadata_contents);
    assert!(state.successful_evidence().is_none());

    state.clear();
    assert!(state.failed_for_publish(call(9)).is_none());
}

#[test]
fn success_and_failure_replace_each_other() {
    let mut state = LatestCheckState::new();
    state.record_failure(failed());
    state.record_success(evidence());
    assert!(state.successful_evidence().is_some());
    assert!(state.failed_for_publish(call(3)).is_none());

    state.record_failure(check_success(
        call(4),
        Vec::new(),
        Ok("retained".to_owned()),
    ));
    assert!(state.successful_evidence().is_none());
    let cached = state.failed_for_publish(call(5)).unwrap();
    assert_eq!(cached.tool_call_id, call(5));
    assert_eq!(cached.result, Ok("retained".to_owned()));
}