Skip to main content

agentmux_srv_core/backend/blockcontroller/persistent/
outcome_line.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The `agentmux_session_outcome` frame a persistent controller writes when a
5//! resume's fate is known.
6
7use super::persistent_resume;
8
9/// Builds the NDJSON line for a `persistent_resume::ResumeEffect::
10/// EmitSessionOutcome` — a free function (not a method) so it's callable
11/// directly from the stdout-reader, process-waiter, and stop-path match
12/// arms below, which only hold `_read`/`_wait`-suffixed clones, not
13/// `&self`. See SPEC_AGENT_PANE_HISTORY_ALIGNMENT_2026_08_05.md §2.1.
14pub(super) fn session_outcome_line(
15    outcome: persistent_resume::SessionOutcome,
16    attempted_sid: String,
17    actual_sid: Option<String>,
18) -> String {
19    session_outcome_line_with(outcome, attempted_sid, actual_sid, false, false)
20}
21
22/// The frame for a session the agent ended on purpose (`Agents fresh`), for a
23/// controller that doesn't spawn through `persistent`: `fresh`, nothing
24/// attempted, and a handoff on its way (not an idle start).
25pub(crate) fn fresh_by_choice_line() -> String {
26    session_outcome_line_with(persistent_resume::SessionOutcome::Fresh, String::new(), None, false, false)
27}
28
29/// [`session_outcome_line`], plus whether the fresh session was given
30/// AgentMux's record of the conversation (`continued`, SPEC_DURABLE_
31/// CONVERSATION_MEMORY_2026_09_23.md §4.8). The outcome stays `fresh`: the
32/// provider session is new, and every consumer that scopes scrollback on
33/// `fresh` keeps doing so. Only the pane's label changes.
34///
35/// `idle_start`: no message sits behind the spawn (an eager spawn, a resume
36/// the gate refused), so nobody is talking to the agent yet and the pane
37/// should have it catch up. False when a message started the session. The
38/// pane cannot tell these apart from its own turn state, which can be stale.
39pub(super) fn session_outcome_line_with(
40    outcome: persistent_resume::SessionOutcome,
41    attempted_sid: String,
42    actual_sid: Option<String>,
43    continued: bool,
44    idle_start: bool,
45) -> String {
46    let outcome_str = match outcome {
47        persistent_resume::SessionOutcome::Resumed => "resumed",
48        persistent_resume::SessionOutcome::Fresh => "fresh",
49    };
50    format!(
51        "{}\n",
52        serde_json::json!({
53            "type": "system",
54            "subtype": "agentmux_session_outcome",
55            "outcome": outcome_str,
56            "attempted_sid": attempted_sid,
57            "actual_sid": actual_sid,
58            "continued": continued,
59            "idle_start": idle_start,
60            "timestamp": chrono::Utc::now().to_rfc3339(),
61        })
62    )
63}