1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//! Daemon implementation of the single-version session observability seam:
//! [`DaemonSessionObservability`] merges the live runtime projection with the
//! session resource plane and paginates the full message history.
use std::collections::HashMap;
use std::sync::Arc;
use anyhow::{Context as _, Result};
use async_trait::async_trait;
use parking_lot::Mutex;
use theway_core::{Session, SessionTreeEntry};
use theway_transport::session_observability::{
ListSessionMessagesRequest, SessionMessagePage, SessionObservabilityOps,
};
use theway_transport::transport::SessionOps;
use theway_transport::wire::{WireSessionSnapshot, WireStatus};
use crate::feed_replay::{borrowed_wire_blocks, is_user_input_record, paired_user_inputs};
use crate::runtime_storage::SessionRepository;
pub(crate) struct DaemonSessionObservability {
session_ops: Arc<dyn SessionOps>,
session_states: Arc<Mutex<HashMap<String, WireStatus>>>,
latest: Arc<Mutex<WireStatus>>,
repo: Arc<dyn SessionRepository>,
}
impl DaemonSessionObservability {
pub(crate) fn new(
session_ops: Arc<dyn SessionOps>,
session_states: Arc<Mutex<HashMap<String, WireStatus>>>,
latest: Arc<Mutex<WireStatus>>,
repo: Arc<dyn SessionRepository>,
) -> Self {
Self {
session_ops,
session_states,
latest,
repo,
}
}
fn live_status(&self, session_id: &str) -> Option<WireStatus> {
self.session_states
.lock()
.get(session_id)
.cloned()
.or_else(|| {
let latest = self.latest.lock();
(latest.session_id == session_id).then(|| latest.clone())
})
}
}
#[async_trait]
impl SessionObservabilityOps for DaemonSessionObservability {
async fn authoritative_snapshot(&self, session_id: &str) -> Result<WireSessionSnapshot> {
// 1. Resource snapshot: session identity/info, graph nodes, lineage.
let resource = self
.session_ops
.session_snapshot(session_id)
.await
.with_context(|| format!("load resource snapshot for session {session_id}"));
// 2. Live projection (per-session map first, then the active latest).
let Some(live) = self.live_status(session_id) else {
// No live projection: this is a cold /resume or explicit-session
// read. Seed the resource snapshot with the persisted transcript
// so the client sees the conversation immediately; the full
// runtime is still built lazily on the first message.
let mut resource = resource?;
if let Ok(page) = self
.list_session_messages(&ListSessionMessagesRequest {
session_id: session_id.to_string(),
before_entry_id: None,
limit: u32::MAX,
})
.await
{
resource.feed.blocks = page.blocks;
}
return Ok(resource);
};
let Ok(mut resource) = resource else {
// Lazy sessions (issue #46) have a live runtime but no materialized
// repository record yet; project from live until the first write.
return Ok(WireSessionSnapshot::from(&live));
};
// 3. Merge: runtime / feed / system_context / dags / subagents /
// the sidebar come from live; info identity fields / graph nodes
// / active node / lineage come from the resource plane.
//
// The sidebar is live runtime inventory (skills / triggers /
// cron / MCP / tool counts) — the resource plane's
// `session_snapshot` cannot know it and always carries the empty
// snapshot, so `info` merges field-by-field: identity from the
// resource, the sidebar from the live projection.
let mut merged = WireSessionSnapshot::from(&live);
merged.session_id = if resource.session_id.is_empty() {
merged.session_id
} else {
resource.session_id.clone()
};
merged.info = resource.info;
merged.info.sidebar = live.sidebar.clone();
merged.graph_state.nodes = std::mem::take(&mut resource.graph_state.nodes);
merged.graph_state.active_node_id = resource.graph_state.active_node_id.take();
merged.lineage = resource.lineage;
Ok(merged)
}
async fn list_session_messages(
&self,
request: &ListSessionMessagesRequest,
) -> Result<SessionMessagePage> {
let limit = request.effective_limit() as usize;
let store = self
.repo
.open(&request.session_id)
.await?
.with_context(|| format!("no session matches id {}", request.session_id))?;
let session = Session::from_store(store);
let branch = session.branch(None).await?;
// `total` and the cursor count transcript messages: a `custom:user_input`
// record entry describes a message, it is not one.
let messages: Vec<&SessionTreeEntry> = branch
.iter()
.filter(|entry| matches!(entry, SessionTreeEntry::Message { .. }))
.filter(|entry| !is_user_input_record(entry))
.collect();
let total = messages.len() as u64;
// Cursor semantics: `before_entry_id` is exclusive. Unknown cursor →
// empty page (the client's cursor predates a fork/compaction).
let before = match request.before_entry_id.as_deref() {
Some(id) => match messages.iter().position(|entry| entry.id() == id) {
Some(position) => position,
None => {
return Ok(SessionMessagePage {
session_id: request.session_id.clone(),
blocks: Vec::new(),
next_before_entry_id: None,
has_more: false,
total,
});
}
},
None => messages.len(),
};
let start = before.saturating_sub(limit);
let page = &messages[start..before];
// The pairing spans the whole branch, so a page that starts after a record
// still renders the record's text and chips for the message it contains.
let paired = paired_user_inputs(&branch);
let blocks = borrowed_wire_blocks(page, &paired);
Ok(SessionMessagePage {
session_id: request.session_id.clone(),
blocks,
next_before_entry_id: page.first().map(|entry| entry.id().to_string()),
has_more: start > 0,
total,
})
}
}
#[cfg(test)]
// Test files live in `tests/session_observability/` (mirror of src), pulled in
// by path so they keep unit-test semantics (private access). See docs/rust-test-files.md.
tests_bridge_macro::tests_bridge!("session_observability");