agentmux_srv_core/backend/blockcontroller/prompt_record.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The transcript record for a message srv delivered to an agent, with where it came from
5//! (docs/specs/SPEC_MESSAGE_ORIGIN_AND_ONE_CONTAINER_2026_10_25.md ยง8, P3).
6//!
7//! The CLIs srv writes this record for run as one process per turn
8//! ([`UserRecordSink`](super::subprocess::UserRecordSink)); an ACP agent and the Codex App Server keep
9//! a session open and never put the user's words in their output, so without it their transcript holds
10//! no trace of what was sent and no way to say who sent it.
11
12use std::sync::Arc;
13
14use crate::backend::message_origin::RecordOrigin;
15use crate::backend::mps;
16use crate::backend::storage::filestore::FileStore;
17use crate::backend::storage::store::Store;
18
19use super::subprocess::{injected_user_record, user_record};
20
21/// The agent's transcript, written by `write`: where its records go and how to find its zone.
22#[derive(Clone)]
23pub(super) struct PromptRecords {
24 pub broker: Option<Arc<mps::Broker>>,
25 pub filestore: Option<Arc<FileStore>>,
26 pub mstore: Option<Arc<Store>>,
27}
28
29impl PromptRecords {
30 /// Store `message` as a user record of `block_id`'s transcript and the agent's global zone, signed
31 /// with `record`'s origin. `shown_by_pane`: the pane typed it and already shows it, so the record is
32 /// published for the stream's sake only; otherwise it is published for the pane to show.
33 ///
34 /// Call it only once the message was handed to the agent: a transcript must not claim a turn that
35 /// never happened.
36 pub(super) fn write(&self, block_id: &str, message: &str, shown_by_pane: bool, record: Option<&RecordOrigin>) {
37 let Some(store) = self.mstore.as_ref() else {
38 return;
39 };
40 let Ok(block) = store.must_get::<crate::backend::obj::Block>(block_id) else {
41 return;
42 };
43 let global_zone = crate::backend::agent_session::agent_zone_for_block_meta(&block.meta);
44 let value = if shown_by_pane { user_record(message) } else { injected_user_record(message) };
45 let line = format!("{value}\n");
46 let persist = if shown_by_pane {
47 crate::backend::transcript::append::persist_user_line
48 } else {
49 crate::backend::transcript::append::persist_injected_user_line
50 };
51 persist(self.broker.as_deref(), block_id, line.as_bytes(), self.filestore.as_ref(), global_zone.as_deref(), record);
52 }
53}