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
//! Session model-context state and compaction/occupancy events.
use crate::{SessionError, SessionManager};
use bcode_session_models::{
ProviderContextSnapshot, RequestContextObservation, RequestContextOccupancy, SessionEvent,
SessionEventKind, SessionId,
};
impl SessionManager {
/// Return the current context generation.
///
/// # Errors
///
/// Returns [`SessionError::NotFound`] when the session does not exist.
pub async fn current_context_epoch(&self, session_id: SessionId) -> Result<u64, SessionError> {
let handle = self.session_handle(session_id).await?;
handle.current_context_epoch().await
}
/// Return authoritative current context occupancy with a bounded projection lookup.
///
/// # Errors
///
/// Returns [`SessionError::NotFound`] when the session does not exist, or a projection error
/// when the occupancy read model is not trustworthy.
pub async fn current_context_occupancy(
&self,
session_id: SessionId,
) -> Result<Option<RequestContextOccupancy>, SessionError> {
let handle = self.session_handle(session_id).await?;
handle.current_context_occupancy().await
}
/// Return the model-visible session events, starting at the latest compaction when possible.
///
/// # Errors
///
/// Returns [`SessionError::NotFound`] when the session does not exist.
pub async fn model_context_events(
&self,
session_id: SessionId,
) -> Result<Vec<SessionEvent>, SessionError> {
let handle = self.session_handle(session_id).await?;
handle.model_context_events().await
}
/// Append a context-compaction summary to a session.
///
/// # Errors
///
/// Returns an error when the session does not exist or the event cannot be persisted.
pub async fn append_context_compacted(
&self,
session_id: SessionId,
summary: String,
compacted_through_sequence: u64,
) -> Result<SessionEvent, SessionError> {
self.append_event(
session_id,
SessionEventKind::ContextCompacted {
summary,
compacted_through_sequence,
},
)
.await
}
/// Append a provider-native context compaction boundary.
///
/// # Errors
///
/// Returns an error when the session does not exist or the event cannot be persisted.
pub async fn append_provider_context_compacted(
&self,
session_id: SessionId,
snapshot: ProviderContextSnapshot,
compacted_through_sequence: u64,
) -> Result<SessionEvent, SessionError> {
self.append_event(
session_id,
SessionEventKind::ProviderContextCompacted {
snapshot,
compacted_through_sequence,
},
)
.await
}
/// Append a context occupancy observation.
///
/// # Errors
///
/// Returns an error when the session does not exist or the event cannot be persisted.
pub async fn append_request_context_observed(
&self,
session_id: SessionId,
observation: RequestContextObservation,
) -> Result<SessionEvent, SessionError> {
self.append_event(
session_id,
SessionEventKind::RequestContextObserved { observation },
)
.await
}
}