ghostmode 0.1.1

Code Mode for Rust agents. The model writes a Python script that calls your tools, and ghostmode runs it in a sandbox.
Documentation
use cucumber::{given, then, when};
use futures::FutureExt;

use crate::{Request, World};

#[given(regex = r#"^a sandbox whose signing key is "([A-Z])"$"#)]
fn sandbox_signing_key(world: &mut World, letter: char) {
    world.signing_key = Some([letter as u8; 32]);
}

#[when(regex = r"^the caller cancels run (\d+)$")]
async fn cancel_run(world: &mut World, run_id: u64) {
    world.send(Request::Cancel(run_id)).await;
}

#[then(regex = r"^cancel returns (true|false)$")]
fn cancel_returns(world: &mut World, was_open: bool) {
    assert_eq!(world.cancel_returned, Some(was_open));
}

#[when(regex = r"^the caller stops awaiting the model's execute of (.+)$")]
async fn drop_execute(world: &mut World, code: String) {
    let mut sandbox = world.take_sandbox().await;
    let request = Request::Execute {
        code,
        restart: false,
    };
    let reply = sandbox.reply(request).now_or_never();
    assert!(
        reply.is_none(),
        "execute answered before the caller stopped awaiting it"
    );
    world.sandbox = Some(sandbox);
}

#[when("the caller stops awaiting the dump")]
async fn drop_dump(world: &mut World) {
    let mut sandbox = world.take_sandbox().await;
    let reply = sandbox.reply(Request::Dump).now_or_never();
    assert!(
        reply.is_none(),
        "dump answered before the caller stopped awaiting it"
    );
    world.sandbox = Some(sandbox);
}

#[when("the caller resets the sandbox")]
async fn reset_sandbox(world: &mut World) {
    world.send(Request::Reset).await;
}

#[when("the caller drops the sandbox")]
fn drop_sandbox(world: &mut World) {
    world.sandbox.take().expect("a sandbox");
}

#[when("the caller dumps the sandbox")]
async fn dump_sandbox(world: &mut World) {
    world.send(Request::Dump).await;
}

#[when("the caller restores the dump into a new sandbox")]
async fn restore_dump(world: &mut World) {
    let bytes = world.dump.clone().expect("a dump");
    world.restore(&bytes).await;
}

#[when(regex = r#"^the caller restores the dump under key "([A-Z])" into a new sandbox$"#)]
async fn restore_dump_under_key(world: &mut World, letter: char) {
    world.signing_key = Some([letter as u8; 32]);
    restore_dump(world).await;
}

#[when(
    regex = r"^the caller restores the dump with its (first|last) byte flipped into a new sandbox$"
)]
async fn restore_dump_with_byte_flipped(world: &mut World, position: String) {
    let mut bytes = world.dump.clone().expect("a dump");
    let index = match position.as_str() {
        "first" => 0,
        _ => bytes.len() - 1,
    };
    bytes[index] ^= 1;
    world.restore(&bytes).await;
}

#[when("the caller restores the dump truncated to half its length into a new sandbox")]
async fn restore_dump_truncated_to_half(world: &mut World) {
    let mut bytes = world.dump.clone().expect("a dump");
    bytes.truncate(bytes.len() / 2);
    world.restore(&bytes).await;
}

#[when(regex = r#"^the caller restores the bytes "(.*)" into a new sandbox$"#)]
async fn restore_bytes(world: &mut World, text: String) {
    world.restore(text.as_bytes()).await;
}

#[when("the caller builds the builder that the failed restore returned")]
async fn build_after_failed_restore(world: &mut World) {
    let build = world
        .build_after_failed_restore
        .take()
        .expect("a failed restore");
    world.sandbox = Some(build.await);
}

#[then(regex = r"^the caller's error is that run (\d+) is open$")]
fn error_is_run_open(world: &mut World, run_id: u64) {
    let error = world.caller_error.as_ref().expect("a caller error");
    assert!(
        matches!(error, ghostmode::Error::RunOpen { run_id: open } if *open == run_id),
        "{error:?}"
    );
}

#[then("the caller's error is that the bytes don't restore")]
fn error_is_restore(world: &mut World) {
    let error = world.caller_error.as_ref().expect("a caller error");
    assert!(matches!(error, ghostmode::Error::Restore(_)), "{error:?}");
}

#[then(regex = r"^the caller's error is a failed restore naming (.+)$")]
fn error_is_restore_naming(world: &mut World, cause: String) {
    let error = world.caller_error.as_ref().expect("a caller error");
    assert!(matches!(error, ghostmode::Error::Restore(_)), "{error:?}");
    let chain = format_chain(error);
    assert_eq!(chain.matches(&cause).count(), 1, "{chain}");
}

#[then(regex = r"^the caller's error is that the dump is format version (\d+), not version (\d+)$")]
fn error_is_other_format_version(world: &mut World, found: u8, expected: u8) {
    let error = world.caller_error.as_ref().expect("a caller error");
    assert!(matches!(error, ghostmode::Error::Restore(_)), "{error:?}");
    let chain = format_chain(error);
    let cause = format!("format version {found}, not version {expected}");
    assert_eq!(chain.matches(&cause).count(), 1, "{chain}");
}

fn format_chain(error: &dyn std::error::Error) -> String {
    let mut chain = error.to_string();
    let mut source = error.source();
    while let Some(cause) = source {
        chain.push_str(&format!(": {cause}"));
        source = cause.source();
    }
    chain
}

#[then("the caller's error is that the signature doesn't match")]
fn error_is_signature_mismatch(world: &mut World) {
    let error = world.caller_error.as_ref().expect("a caller error");
    assert!(
        matches!(error, ghostmode::Error::SignatureMismatch),
        "{error:?}"
    );
}

#[then("the sandbox's Debug output doesn't show the signing key")]
fn debug_hides_signing_key(world: &mut World) {
    let key = world.signing_key.expect("a signing key");
    let sandbox = world.sandbox.as_ref().expect("a sandbox");
    let output = format!("{sandbox:?}");
    assert!(output.starts_with("Sandbox"), "{output}");
    assert!(!output.contains(&format!("{key:?}")), "{output}");
}