ultron-session-core 1.0.0

One session's runtime, shared by every mode through the session worker: the rlm lane (model, provider, system prompt, the single rlm tool), the composition root of its host stack, the format-4 run journal, session naming, Pi read views, and the session actor
Documentation
//! Steering, follow-up and next-run messages queued while the session is idle, then a prompt: the run's admission
//! takes them by `selectAcceptedInbox` (every next-run message; steering and follow-ups by their queue modes) and
//! places them before the prompt in the order they were queued; what it leaves waits in the lane's inbox and the
//! run delivers it. Replays crates/session-core/fixtures/idle_queue.json, recorded from the TypeScript AgentHarness
//! lane by generate-idle-queue.mjs: the same provider requests, the same transcript, the same inbox after admission.

use crate::p_session_support;
use crate::reopen_support;

use p_session_support::{in_order, text, user_texts};
use protocol::watch::QueueKind;
use reopen_support::Home;
use serde_json::{Value, json};
use session_core::core::Op;
use session_journal::ns::LANE_STATE;
use std::collections::HashMap;

fn fixture() -> Value {
    serde_json::from_str(include_str!("../fixtures/idle_queue.json")).unwrap()
}

/// The user texts of a request, without the clock lines.
fn users(request: &Value) -> Vec<Value> {
    user_texts(request)
        .into_iter()
        .filter(|t| t != "[clock] <CLOCK>")
        .collect()
}

fn message_text(message: &Value) -> String {
    match &message["content"] {
        Value::String(text) => text.clone(),
        Value::Array(blocks) => blocks.iter().filter_map(|b| b["text"].as_str()).collect(),
        _ => String::new(),
    }
}

fn replay(scenario: &Value) {
    let name = scenario["name"].as_str().unwrap();
    let home = Home::new(None);
    let answers: Vec<String> = scenario["answers"]
        .as_array()
        .unwrap()
        .iter()
        .map(|a| text(a.as_str().unwrap()))
        .collect();
    let w = home.open(in_order(answers), &[], None);
    for (kind, key) in [
        (QueueKind::Steer, "steeringMode"),
        (QueueKind::FollowUp, "followUpMode"),
    ] {
        w.call(Op::SetQueueMode {
            kind,
            mode: scenario[key].as_str().unwrap().into(),
        })
        .unwrap();
    }
    let mut texts = HashMap::new();
    for queued in scenario["queued"].as_array().unwrap() {
        let kind = QueueKind::parse(queued[0].as_str().unwrap()).unwrap();
        let message = queued[1].as_str().unwrap().to_string();
        let entry = w
            .call(Op::Enqueue {
                kind,
                message: message.clone(),
                images: Vec::new(),
                entry_id: None,
            })
            .unwrap()
            .unwrap();
        texts.insert(entry.as_str().unwrap().to_string(), message);
    }
    // Nothing runs while the messages wait.
    assert!(w.requests().is_empty(), "{name}");
    let lanes_before = home.values(LANE_STATE).len();

    let ended = w
        .call(Op::PromptAndWait {
            message: "request".into(),
            images: Vec::new(),
        })
        .unwrap()
        .unwrap();
    assert_eq!(ended["error"], Value::Null, "{name}: {ended}");

    // Steering the admission left (one-at-a-time, a second steer) reaches the run's first request after the prompt,
    // as TypeScript's agent loop polls steering before its first request.
    let requests: Vec<Vec<Value>> = w.requests().iter().map(users).collect();
    assert_eq!(json!(requests), scenario["requests"], "{name}: requests");
    // The admission's lane state (the first one naming the run) keeps what it did not take, in queue order.
    let admitted = home.values(LANE_STATE)[lanes_before..]
        .iter()
        .find(|state| !state["currentOperationId"].is_null())
        .cloned()
        .unwrap();
    let inbox: Vec<Value> = admitted["inbox"]
        .as_array()
        .unwrap()
        .iter()
        .map(|item| json!([item["kind"], texts[item["entryId"].as_str().unwrap()]]))
        .collect();
    assert_eq!(json!(inbox), scenario["inboxAfterAdmission"], "{name}: inbox");
    // The transcript, as the branch holds it.
    let transcript: Vec<String> = w
        .events("message_end")
        .iter()
        .map(|e| {
            format!(
                "{}: {}",
                e["message"]["role"].as_str().unwrap(),
                message_text(&e["message"])
            )
        })
        .collect();
    assert_eq!(json!(transcript), scenario["branch"], "{name}: transcript");
    assert_eq!(home.values(LANE_STATE).pop().unwrap()["inbox"], json!([]), "{name}");
    w.close();
}

#[test]
fn session_core_idle_queue_takes_every_queued_message_before_the_prompt_in_all_mode() {
    replay(&fixture()["scenarios"][0]);
}

#[test]
fn session_core_idle_queue_takes_the_first_of_each_kind_one_at_a_time_and_delivers_the_rest() {
    replay(&fixture()["scenarios"][1]);
}

#[test]
fn session_core_idle_queue_mixes_one_at_a_time_steering_with_all_follow_ups() {
    replay(&fixture()["scenarios"][2]);
}

#[test]
fn session_core_idle_queue_places_a_lone_follow_up_before_the_prompt() {
    replay(&fixture()["scenarios"][3]);
}

/// A session whose settings name no queue modes delivers every queued message at once, as the TypeScript durable
/// harness does (harness.ts `steeringMode ?? "all"`), and `/settings` shows the same value unset.
#[test]
fn session_core_queue_modes_default_to_all() {
    assert_eq!(config::keys::QUEUE_DEFAULT, "all");
    let home = Home::new(None);
    let w = home.open(in_order(Vec::new()), &[], None);
    let state = w.call(Op::State).unwrap().unwrap();
    assert_eq!(
        (&state["steeringMode"], &state["followUpMode"]),
        (&json!("all"), &json!("all"))
    );
    assert_eq!(session_journal::run::default_settings()["steeringMode"], "all");
    assert_eq!(session_journal::run::default_settings()["followUpMode"], "all");
    w.close();
}