Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

14.8 KB
Raw Blame
//! `threads.transcript_get` and `threads.token_usage` read a thread through
//! the session store, under the key the request's own context resolves.

use super::*;
use crate::core::runtime::{ContextOverlay, CoreContext, DispatchCtx, DomainSet};
use tinyagents_session::transcript::{
    MessageUsage, SessionRef, TranscriptMessage, TranscriptMeta, TranscriptTurn, TurnUsage,
};

const THREAD: &str = "thread-store";

fn ctx_for(workspace: &std::path::Path, profile: Option<&str>) -> DispatchCtx {
    ctx_with_chat_agent(workspace, profile, None)
}

/// A context whose config selects `chat_agent_id` as the web-chat agent.
fn ctx_with_chat_agent(
    workspace: &std::path::Path,
    profile: Option<&str>,
    chat_agent_id: Option<&str>,
) -> DispatchCtx {
    let mut config = crate::config::Config {
        workspace_dir: workspace.to_path_buf(),
        ..crate::config::Config::default()
    };
    config.agent.chat_agent_id = chat_agent_id.map(str::to_string);
    let mut overlay = ContextOverlay::new(
        config,
        DomainSet::kernel(),
        crate::tools::toolpacks::ToolGroups::none(),
    );
    if let Some(profile) = profile {
        overlay = overlay.profile(profile);
    }
    DispatchCtx::new(CoreContext::for_test(DomainSet::full(), None).derive_with(overlay))
}

fn meta(agent: &str, agent_type: &str) -> TranscriptMeta {
    TranscriptMeta {
        agent_name: agent.into(),
        agent_id: Some(agent.into()),
        agent_type: Some(agent_type.into()),
        dispatcher: "native".into(),
        provider: Some("openhuman".into()),
        model: Some("test-model".into()),
        created: "2026-10-15T00:00:00Z".into(),
        updated: "2026-10-15T00:00:00Z".into(),
        turn_count: 1,
        prefix_message_count: None,
        prefix_reset: false,
        input_tokens: 0,
        output_tokens: 0,
        cached_input_tokens: 0,
        charged_amount_usd: 0.0,
        thread_id: Some(THREAD.into()),
        task_id: None,
        session_id: None,
        parent_session_id: None,
    }
}

fn usage(input: u64, output: u64) -> TurnUsage {
    TurnUsage {
        provider: "openhuman".into(),
        model: "test-model".into(),
        usage: MessageUsage {
            input,
            output,
            context_window: 200_000,
            ..Default::default()
        },
        ts: "2026-10-15T00:00:01Z".into(),
        reasoning_content: None,
        tool_calls: Vec::new(),
        iteration: 1,
    }
}

/// A root turn and one sub-agent run of [`THREAD`], written where the session
/// host and the sub-agent runner write them: `ctx`'s transcript store.
async fn write_thread(ctx: &DispatchCtx) {
    let locator = super::super::support::transcript_store(ctx, THREAD)
        .await
        .expect("store")
        .locator();
    let root_meta = meta("orchestrator", "root");
    let turn = vec![
        TranscriptMessage::user("Current Date & Time: today\n\nhello"),
        TranscriptMessage::assistant("Let me think through that."),
        TranscriptMessage::assistant("hi there"),
    ];
    locator
        .open_session(
            &SessionRef::scoped(THREAD, "orchestrator"),
            root_meta.clone(),
        )
        .unwrap()
        .append_turn(TranscriptTurn {
            prev: &[],
            next: &turn,
            meta: &root_meta,
            turn_usage: Some(&usage(100, 10)),
            request_id: Some("req-1"),
            tools: None,
        })
        .unwrap();
    let child_meta = meta("researcher", "subagent");
    let child = vec![
        TranscriptMessage::user("look it up"),
        TranscriptMessage::assistant("found it"),
    ];
    locator
        .open_stem("1700000000_orchestrator__researcher", child_meta.clone())
        .unwrap()
        .append_turn(TranscriptTurn {
            prev: &[],
            next: &child,
            meta: &child_meta,
            turn_usage: Some(&usage(40, 4)),
            request_id: None,
            tools: None,
        })
        .unwrap();
}

#[tokio::test]
async fn transcript_projection_includes_voice_metadata_from_persisted_thread_rows() {
    use crate::threads::store::{blocking, ConversationMessage, CreateConversationThread};
    use serde_json::json;

    let dir = tempfile::tempdir().unwrap();
    let ctx = ctx_for(dir.path(), None);
    let workspace = dir.path().to_path_buf();
    blocking::ensure_thread(
        ctx.clone(),
        workspace.clone(),
        CreateConversationThread {
            id: THREAD.into(),
            title: "Voice notes".into(),
            created_at: "2026-10-15T00:00:00Z".into(),
            parent_thread_id: None,
            labels: None,
            personality_id: None,
            working_dir: None,
        },
    )
    .await
    .unwrap();
    for (id, content, sender, note_id, request_id) in [
        (
            "user-voice",
            "hello",
            "user",
            Some("recording-1"),
            Some("req-1"),
        ),
        (
            "assistant-voice",
            "hi there",
            "agent",
            Some("reply-1"),
            Some("req-1"),
        ),
        ("user-plain", "hello", "user", None, Some("req-2")),
        ("assistant-plain", "hi again", "agent", None, Some("req-2")),
    ] {
        blocking::append_message(
            ctx.clone(),
            workspace.clone(),
            THREAD.into(),
            ConversationMessage {
                id: id.into(),
                content: content.into(),
                message_type: "text".into(),
                extra_metadata: {
                    let mut metadata = json!({});
                    if let Some(note_id) = note_id {
                        metadata["voiceNote"] = json!({"noteId": note_id, "durationMs": 5000});
                    }
                    if let Some(request_id) = request_id {
                        metadata["requestId"] = json!(request_id);
                    }
                    metadata
                },
                sender: sender.into(),
                created_at: "2026-10-15T00:00:02Z".into(),
            },
        )
        .await
        .unwrap();
    }
    write_thread(&ctx).await;
    let locator = super::super::support::transcript_store(&ctx, THREAD)
        .await
        .unwrap()
        .locator();
    let root_meta = meta("orchestrator", "root");
    let decorated_user = "Current Date & Time: today\n\nhello";
    let prior = vec![
        TranscriptMessage::user(decorated_user),
        TranscriptMessage::assistant("Let me think through that."),
        TranscriptMessage::assistant("hi there"),
    ];
    let complete = vec![
        TranscriptMessage::user(decorated_user),
        TranscriptMessage::assistant("Let me think through that."),
        TranscriptMessage::assistant("hi there"),
        TranscriptMessage::user("hello"),
        TranscriptMessage::assistant("hi again"),
    ];
    locator
        .open_session(
            &SessionRef::scoped(THREAD, "orchestrator"),
            root_meta.clone(),
        )
        .unwrap()
        .append_turn(TranscriptTurn {
            prev: &prior,
            next: &complete,
            meta: &root_meta,
            turn_usage: Some(&usage(100, 10)),
            request_id: Some("req-2"),
            tools: None,
        })
        .unwrap();

    let page = transcript_get(
        &ctx,
        TranscriptGetRequest {
            thread_id: THREAD.into(),
            cursor: None,
            limit: None,
        },
    )
    .await
    .unwrap()
    .value
    .data
    .unwrap();
    let voice_user = page.items.iter().find_map(|item| match item {
        tinyagents_session::transcript::view::DisplayItem::UserMessage {
            content,
            request_id,
            extra_metadata,
            ..
        } if content == decorated_user && request_id.as_deref() == Some("req-1") => {
            extra_metadata.as_ref()
        }
        _ => None,
    });
    let plain_duplicate_user = page.items.iter().find_map(|item| match item {
        tinyagents_session::transcript::view::DisplayItem::UserMessage {
            content,
            request_id,
            extra_metadata,
            ..
        } if content == "hello" && request_id.as_deref() == Some("req-2") => {
            extra_metadata.as_ref()
        }
        _ => None,
    });
    let assistant = page.items.iter().find_map(|item| match item {
        tinyagents_session::transcript::view::DisplayItem::AssistantMessage {
            content,
            extra_metadata,
            ..
        } if content == "hi there" => extra_metadata.as_ref(),
        _ => None,
    });
    let narration = page.items.iter().find_map(|item| match item {
        tinyagents_session::transcript::view::DisplayItem::AssistantMessage {
            content,
            extra_metadata,
            ..
        } if content == "Let me think through that." => extra_metadata.as_ref(),
        _ => None,
    });
    assert_eq!(
        voice_user.and_then(|value| value.pointer("/voiceNote/noteId")),
        Some(&json!("recording-1"))
    );
    assert!(plain_duplicate_user.is_none_or(|value| value.get("voiceNote").is_none()));
    assert_eq!(
        assistant.and_then(|value| value.pointer("/voiceNote/noteId")),
        Some(&json!("reply-1"))
    );
    assert!(narration.is_none_or(|value| value.get("voiceNote").is_none()));
    let mut cursor = None;
    let mut voice_reply_on_its_page = false;
    loop {
        let page = transcript_get(
            &ctx,
            TranscriptGetRequest {
                thread_id: THREAD.into(),
                cursor,
                limit: Some(1),
            },
        )
        .await
        .unwrap()
        .value
        .data
        .unwrap();
        voice_reply_on_its_page |= page.items.iter().any(|item| {
            matches!(
                item,
                tinyagents_session::transcript::view::DisplayItem::AssistantMessage {
                    content,
                    request_id,
                    extra_metadata: Some(metadata),
                    ..
                } if content == "hi there" && request_id.as_deref() == Some("req-1")
                    && metadata.pointer("/voiceNote/noteId") == Some(&json!("reply-1"))
            )
        });
        cursor = page.next_cursor;
        if cursor.is_none() {
            break;
        }
    }
    assert!(
        voice_reply_on_its_page,
        "voice metadata survives paging to its assistant row"
    );
}

#[tokio::test]
async fn a_profiles_thread_is_read_from_its_own_store() {
    let dir = tempfile::tempdir().unwrap();
    let profile = ctx_for(dir.path(), Some("profile-t"));
    write_thread(&profile).await;

    let page = transcript_get(
        &profile,
        TranscriptGetRequest {
            thread_id: THREAD.into(),
            cursor: None,
            limit: None,
        },
    )
    .await
    .expect("transcript page")
    .value
    .data
    .expect("page data");
    assert!(page.has_transcript, "the profile reads its own transcript");
    assert!(!page.items.is_empty());

    let spend = super::super::usage::token_usage(
        &profile,
        super::super::usage::ThreadTokenUsageRequest {
            thread_id: THREAD.into(),
        },
    )
    .await
    .expect("usage")
    .value
    .data
    .expect("usage data");
    assert!(spend.has_usage);
    assert_eq!(
        spend.input_tokens, 140,
        "root and sub-agent are both counted"
    );
    assert_eq!(spend.output_tokens, 14);
    assert_eq!(spend.subagents.len(), 1);
    assert_eq!(spend.subagents[0].agent_id, "researcher");

    // The same workspace without the profile resolves another key, and so
    // never sees the profile's thread.
    let desktop = ctx_for(dir.path(), None);
    let page = transcript_get(
        &desktop,
        TranscriptGetRequest {
            thread_id: THREAD.into(),
            cursor: None,
            limit: None,
        },
    )
    .await
    .unwrap()
    .value
    .data
    .unwrap();
    assert!(!page.has_transcript);
}

#[tokio::test]
async fn a_blank_thread_id_is_refused() {
    let dir = tempfile::tempdir().unwrap();
    let err = transcript_get(
        &ctx_for(dir.path(), None),
        TranscriptGetRequest {
            thread_id: "  ".into(),
            cursor: None,
            limit: None,
        },
    )
    .await
    .expect_err("blank thread id");
    assert!(err.contains("thread_id is required"));
}

/// After the operator changes `[agent] chat_agent_id`, a thread written under
/// the earlier chat agent keeps its transcript store: the readers use the key
/// recorded with the thread, not the one the current config would derive.
#[tokio::test]
async fn changing_the_chat_agent_does_not_strand_an_existing_threads_turns() {
    let _ = crate::agent_registry::host_definition::AgentDefinitionRegistry::init_global_builtins();
    let dir = tempfile::tempdir().unwrap();
    let before = ctx_with_chat_agent(dir.path(), None, None);
    let after = ctx_with_chat_agent(dir.path(), None, Some("summarizer"));

    // The two configs derive different keys, or this proves nothing.
    let rule_before = super::super::support::transcript_store(&before, THREAD)
        .await
        .unwrap()
        .key;
    let rule_after = super::super::support::transcript_store(&after, "unrecorded-thread")
        .await
        .unwrap()
        .key;
    assert_ne!(
        rule_before, rule_after,
        "the chat agent flip changes the key"
    );

    // The first session on the thread records its key, then writes under it.
    let recorded = crate::threads::store::bind_transcript_key(
        &before,
        dir.path(),
        THREAD,
        rule_before.clone(),
    )
    .await;
    assert_eq!(recorded, rule_before);
    write_thread(&before).await;

    let request = || TranscriptGetRequest {
        thread_id: THREAD.into(),
        cursor: None,
        limit: None,
    };
    let page = transcript_get(&after, request())
        .await
        .unwrap()
        .value
        .data
        .unwrap();
    assert!(
        page.has_transcript && !page.items.is_empty(),
        "the earlier turns are still read after the chat agent changed"
    );
    let spend = super::super::usage::token_usage(
        &after,
        super::super::usage::ThreadTokenUsageRequest {
            thread_id: THREAD.into(),
        },
    )
    .await
    .unwrap()
    .value
    .data
    .unwrap();
    assert_eq!(spend.input_tokens, 140);

    // A thread with no recorded key falls back to the current rule.
    let other = tempfile::tempdir().unwrap();
    let unrecorded_before = ctx_with_chat_agent(other.path(), None, None);
    let unrecorded_after = ctx_with_chat_agent(other.path(), None, Some("summarizer"));
    write_thread(&unrecorded_before).await;
    let page = transcript_get(&unrecorded_after, request())
        .await
        .unwrap()
        .value
        .data
        .unwrap();
    assert!(
        !page.has_transcript,
        "without a recorded key the reader keeps today's rule"
    );
}