Skip to main content

agentmux_srv_core/backend/transcript/
origin_ledger.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! What srv delivered to a provider that echoes the prompt into its own output, and where each
5//! message came from (docs/specs/SPEC_MESSAGE_ORIGIN_AND_ONE_CONTAINER_2026_10_25.md ยง8, P3).
6//!
7//! srv writes no user record for such a provider, so the line in its transcript is the provider's
8//! own, and nothing in it says who sent the words. The delivery path says which text it handed to
9//! which block ([`expect`]); the place that stores the provider's output asks whether a user line
10//! is one of those ([`take`]) and, when it is, signs the origin onto the line
11//! (`append::append_output_line`). Matching is by block and exact text, oldest first, the way
12//! `viewer::origin` pairs a phone's message. A line nobody expected gets no origin: it is untyped,
13//! and a viewer shows it in the container, never as the human.
14//!
15//! An entry nobody takes within [`KEEP`] (the provider never echoed it) is dropped, and a block
16//! holds at most [`PER_BLOCK`], so words typed faster than a provider echoes them can't grow it.
17
18use std::sync::atomic::{AtomicUsize, Ordering};
19use std::sync::OnceLock;
20use std::time::{Duration, Instant};
21
22use crate::backend::message_origin::RecordOrigin;
23
24/// How long a delivered message waits for the provider's echo. A queued message is delivered when
25/// the provider starts, so this is generous.
26pub const KEEP: Duration = Duration::from_secs(600);
27
28/// The most entries kept for one block; the oldest goes first.
29pub const PER_BLOCK: usize = 64;
30
31struct Expected {
32    block_id: String,
33    text: String,
34    record: RecordOrigin,
35    at: Instant,
36}
37
38static EXPECTED: OnceLock<parking_lot::Mutex<Vec<Expected>>> = OnceLock::new();
39/// Entries waiting, read without the lock so storing a line costs nothing while none are.
40static WAITING: AtomicUsize = AtomicUsize::new(0);
41
42fn expected() -> &'static parking_lot::Mutex<Vec<Expected>> {
43    EXPECTED.get_or_init(|| parking_lot::Mutex::new(Vec::new()))
44}
45
46fn prune(list: &mut Vec<Expected>, now: Instant) {
47    list.retain(|e| now.duration_since(e.at) < KEEP);
48    WAITING.store(list.len(), Ordering::Release);
49}
50
51/// `text` was delivered to `block_id`'s provider, from `record`.
52pub fn expect(block_id: &str, text: &str, record: RecordOrigin) {
53    let mut list = expected().lock();
54    let now = Instant::now();
55    prune(&mut list, now);
56    if list.iter().filter(|e| e.block_id == block_id).count() >= PER_BLOCK {
57        if let Some(oldest) = list.iter().position(|e| e.block_id == block_id) {
58            list.remove(oldest);
59        }
60    }
61    list.push(Expected { block_id: block_id.to_string(), text: text.trim().to_string(), record, at: now });
62    WAITING.store(list.len(), Ordering::Release);
63}
64
65/// The record for the user line `text` of `block_id`'s output, taken out of the ledger; `None`
66/// when srv delivered no such message (or already paired it).
67pub fn take(block_id: &str, text: &str) -> Option<RecordOrigin> {
68    if WAITING.load(Ordering::Acquire) == 0 {
69        return None;
70    }
71    let text = text.trim();
72    let mut list = expected().lock();
73    prune(&mut list, Instant::now());
74    let at = list.iter().position(|e| e.block_id == block_id && e.text == text)?;
75    let record = list.remove(at).record;
76    WAITING.store(list.len(), Ordering::Release);
77    Some(record)
78}
79
80#[cfg(test)]
81#[path = "origin_ledger_tests.rs"]
82mod tests;