Skip to main content

mj_controller/session_manager/
handle.rs

1use super::*;
2
3#[derive(Clone)]
4pub struct SessionManagerControl {
5    pub(super) commands: mpsc::Sender<ManagerCommand>,
6}
7
8#[derive(Clone, Debug)]
9pub struct ManagedSessionHandle {
10    pub(super) session_id: String,
11    pub(super) commands: mpsc::Sender<ActorCommand>,
12    pub(super) releases: mpsc::UnboundedSender<ReturnedConnection>,
13    pub(super) view: watch::Receiver<ManagedSessionView>,
14}
15
16/// A one-command capability issued by the review host while its prompt hold
17/// is open. It is intentionally opaque to callers: the session actor checks
18/// it against the host's live hold registry before bypassing prompt refusal.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct ReviewDeliveryAdmission {
21    pub(super) session_id: String,
22    pub(super) epoch: u64,
23    pub(super) command_id: String,
24}
25
26impl ReviewDeliveryAdmission {
27    pub(crate) fn new(session_id: String, epoch: u64, command_id: String) -> Self {
28        Self {
29            session_id,
30            epoch,
31            command_id,
32        }
33    }
34
35    pub(crate) fn session_id(&self) -> &str {
36        &self.session_id
37    }
38
39    pub(crate) const fn epoch(&self) -> u64 {
40        self.epoch
41    }
42
43    pub(crate) fn command_id(&self) -> &str {
44        &self.command_id
45    }
46}
47
48/// Exclusive ownership of a session actor's existing relay connection.
49///
50/// Lifecycle operations use this instead of opening a competing projection
51/// client. Dropping an unreleased lease drops the proxy connection, which in
52/// turn cancels any ordinary relay checkpoint barrier.
53///
54/// Prompt submissions that arrive while the lease is active are not rejected.
55/// The actor queues them and forwards them in arrival order once the lease is
56/// released or dropped.
57pub struct ManagedSessionLease {
58    pub(super) session_id: String,
59    pub(super) lease_id: Option<u64>,
60    pub(super) connection: Option<StandaloneSession>,
61    pub(super) releases: mpsc::UnboundedSender<ReturnedConnection>,
62}
63
64impl ManagedSessionLease {
65    pub fn connection_mut(&mut self) -> &mut StandaloneSession {
66        self.connection
67            .as_mut()
68            .expect("managed session lease has already been released")
69    }
70
71    /// Swap the leased proxy after the worker process behind it was replaced.
72    /// The actor stays leased, so queued prompts cannot race the new latch.
73    pub fn replace_connection(&mut self, connection: StandaloneSession) {
74        drop(self.connection.take());
75        self.connection = Some(connection);
76    }
77
78    pub fn release(mut self) {
79        let lease_id = self
80            .lease_id
81            .take()
82            .expect("managed session lease has already been released");
83        let connection = self.connection.take();
84        if let Err(error) = self.releases.send(ReturnedConnection {
85            lease_id,
86            connection,
87        }) {
88            tracing::warn!(
89                session_id = %self.session_id,
90                operation = "lease_release",
91                %error,
92                "session actor stopped before receiving released relay connection"
93            );
94        }
95    }
96}
97
98impl Drop for ManagedSessionLease {
99    fn drop(&mut self) {
100        let Some(lease_id) = self.lease_id.take() else {
101            return;
102        };
103        // Drop the proxy before telling the actor to reconnect so the relay
104        // observes EOF and releases any abandoned checkpoint barrier first.
105        drop(self.connection.take());
106        if let Err(error) = self.releases.send(ReturnedConnection {
107            lease_id,
108            connection: None,
109        }) {
110            tracing::warn!(
111                session_id = %self.session_id,
112                operation = "lease_drop",
113                %error,
114                "session actor stopped before receiving dropped relay lease"
115            );
116        }
117    }
118}
119
120impl ManagedSessionHandle {
121    /// Narrow this controller-owned handle to the operations a control surface uses.
122    pub fn client(&self) -> mj_client::session::SessionHandle {
123        mj_client::session::SessionHandle::new(ClientSessionHandle(self.clone()))
124    }
125
126    pub fn session_id(&self) -> &str {
127        &self.session_id
128    }
129
130    pub fn view(&self) -> ManagedSessionView {
131        self.view.borrow().clone()
132    }
133
134    /// Whether the per-session actor behind this handle has retired. The
135    /// manager itself may still be alive with a replacement actor, so callers
136    /// holding long-lived handles use this to reacquire the current one.
137    pub fn is_stopped(&self) -> bool {
138        self.commands.is_closed()
139    }
140
141    pub fn has_changed(&self) -> Result<bool> {
142        self.view.has_changed().context("session manager stopped")
143    }
144
145    pub async fn changed(&mut self) -> Result<ManagedSessionView> {
146        self.view
147            .changed()
148            .await
149            .context("session manager stopped")?;
150        Ok(self.view())
151    }
152
153    pub async fn submit(&self, command_id: String, command: RelayCommand) -> Result<u64> {
154        self.enqueue_submit(command_id, command).await?.wait().await
155    }
156
157    /// Submit the review's corrective prompt through the one admission that
158    /// corresponds to its live prompt hold. Generic submissions continue to
159    /// use [`Self::submit`] and remain subject to review refusal.
160    pub(crate) async fn submit_review_delivery(
161        &self,
162        admission: ReviewDeliveryAdmission,
163        command: RelayCommand,
164    ) -> Result<u64> {
165        let command_id = admission.command_id.clone();
166        self.enqueue_submit_with_admission(command_id, command, Some(admission))
167            .await?
168            .wait()
169            .await
170    }
171
172    pub async fn enqueue_submit(
173        &self,
174        command_id: String,
175        command: RelayCommand,
176    ) -> Result<PendingRelaySubmit> {
177        self.enqueue_submit_with_admission(command_id, command, None)
178            .await
179    }
180
181    pub(super) async fn enqueue_submit_with_admission(
182        &self,
183        command_id: String,
184        command: RelayCommand,
185        admission: Option<ReviewDeliveryAdmission>,
186    ) -> Result<PendingRelaySubmit> {
187        let (reply, response) = oneshot::channel();
188        self.commands
189            .send(ActorCommand::Submit {
190                queued_at: Instant::now(),
191                command_id,
192                command,
193                admission,
194                reply,
195            })
196            .await
197            .context("session manager stopped")?;
198        Ok(PendingRelaySubmit { response })
199    }
200
201    pub async fn sync_now(&self) -> Result<()> {
202        self.enqueue_sync().await?.wait().await
203    }
204
205    pub async fn respond_elicitation(
206        &self,
207        elicitation_id: String,
208        response: ElicitationResponse,
209    ) -> Result<()> {
210        // Every surface answers through here. A reviewing role's form goes
211        // back to that role's harness in the worker, as the review pane's
212        // own answer does.
213        if let Some((role, elicitation_id)) =
214            mj_client::review::parse_reviewer_question_id(&elicitation_id)
215        {
216            self.reviewer_as(
217                Some(role.to_owned()),
218                ReviewerAction::RespondElicitation {
219                    elicitation_id: elicitation_id.to_owned(),
220                    response,
221                },
222            )
223            .await?;
224            return Ok(());
225        }
226        let (reply, result) = oneshot::channel();
227        self.commands
228            .send(ActorCommand::RespondElicitation {
229                elicitation_id,
230                response,
231                reply,
232            })
233            .await
234            .context("session manager stopped")?;
235        result
236            .await
237            .context("session manager stopped")?
238            .map_err(anyhow::Error::msg)
239    }
240
241    pub async fn stop_background_task(&self, background_task_id: String) -> Result<()> {
242        let (reply, result) = oneshot::channel();
243        self.commands
244            .send(ActorCommand::StopBackgroundTask {
245                background_task_id,
246                reply,
247            })
248            .await
249            .context("session manager stopped")?;
250        result
251            .await
252            .context("session manager stopped")?
253            .map_err(anyhow::Error::msg)
254    }
255
256    /// Install background text the harness reads with the next real prompt.
257    /// It creates no transcript turn, so the user never sees it.
258    pub async fn install_prompt_context(&self, text: String) -> Result<()> {
259        let (reply, result) = oneshot::channel();
260        self.commands
261            .send(ActorCommand::InstallPromptContext { text, reply })
262            .await
263            .context("session manager stopped")?;
264        result
265            .await
266            .context("session manager stopped")?
267            .map_err(anyhow::Error::msg)
268    }
269
270    /// Drive the session's second-opinion reviewer.
271    ///
272    /// The reviewer shares this session's relay connection, so its actions
273    /// queue behind the session's own and are refused while a lifecycle
274    /// operation holds the connection.
275    pub async fn reviewer(&self, action: ReviewerAction) -> Result<ReviewerOutcome> {
276        self.reviewer_as(None, action).await
277    }
278
279    /// Drive one reviewing role. `None` is the default role, which is the one
280    /// plan review uses; a turn review in the extended tier names its
281    /// supervisor, its intent analyst, and each specialist lane.
282    pub async fn reviewer_as(
283        &self,
284        role: Option<String>,
285        action: ReviewerAction,
286    ) -> Result<ReviewerOutcome> {
287        let (reply, result) = oneshot::channel();
288        self.commands
289            .send(ActorCommand::Reviewer {
290                role,
291                action,
292                reply,
293            })
294            .await
295            .context("session manager stopped")?;
296        result
297            .await
298            .context("session manager stopped")?
299            .map_err(anyhow::Error::msg)
300    }
301
302    pub async fn enqueue_sync(&self) -> Result<PendingRelaySync> {
303        let (reply, response) = oneshot::channel();
304        self.commands
305            .send(ActorCommand::Sync { reply })
306            .await
307            .context("session manager stopped")?;
308        Ok(PendingRelaySync { response })
309    }
310
311    /// Run `job` on this session actor's own relay connection. The job is
312    /// refused, never dropped, when the actor cannot run it.
313    pub async fn run_on_connection(&self, job: Box<dyn RelayConnectionJob>) {
314        if let Err(error) = self.commands.send(ActorCommand::RelayJob { job }).await
315            && let ActorCommand::RelayJob { job } = error.0
316        {
317            job.refuse(anyhow::anyhow!("session actor stopped"));
318        }
319    }
320
321    pub async fn lease_connection(&self) -> Result<ManagedSessionLease> {
322        self.lease_connection_for(None).await
323    }
324
325    /// Background replacement must never take a busy session's control channel.
326    pub async fn lease_idle_connection(
327        &self,
328        harness: mj_core::config::HarnessKind,
329    ) -> Result<Option<ManagedSessionLease>> {
330        match self.lease_connection_for(Some(harness)).await {
331            Ok(lease) => Ok(Some(lease)),
332            Err(error) if error.is::<SessionNotIdle>() => Ok(None),
333            Err(error) => Err(error),
334        }
335    }
336
337    async fn lease_connection_for(
338        &self,
339        idle_harness: Option<mj_core::config::HarnessKind>,
340    ) -> Result<ManagedSessionLease> {
341        let (reply, response) = oneshot::channel();
342        self.commands
343            .send(ActorCommand::Lease {
344                idle_harness,
345                reply,
346            })
347            .await
348            .context("session manager stopped")?;
349        let (lease_id, connection) = response.await.context("session manager stopped")??;
350        Ok(ManagedSessionLease {
351            session_id: self.session_id.clone(),
352            lease_id: Some(lease_id),
353            connection: Some(connection),
354            releases: self.releases.clone(),
355        })
356    }
357}
358
359#[derive(Debug)]
360pub(super) struct SessionNotIdle;
361
362impl std::fmt::Display for SessionNotIdle {
363    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
364        f.write_str("session still has work in flight; upgrade deferred")
365    }
366}
367
368impl std::error::Error for SessionNotIdle {}
369
370pub struct PendingRelaySubmit {
371    pub(super) response:
372        oneshot::Receiver<std::result::Result<u64, mj_client::session::SubmitFailure>>,
373}
374
375impl PendingRelaySubmit {
376    pub async fn wait(self) -> Result<u64> {
377        self.response
378            .await
379            .map_err(|error| {
380                anyhow::Error::new(error).context(mj_client::session::DeliveryUnconfirmed)
381            })?
382            .map_err(|failure| {
383                let error = anyhow::Error::msg(failure.message);
384                if failure.unconfirmed {
385                    error.context(mj_client::session::DeliveryUnconfirmed)
386                } else if failure.refused {
387                    error.context(mj_client::session::Refused)
388                } else {
389                    error
390                }
391            })
392    }
393}
394
395pub struct PendingRelaySync {
396    pub(super) response: oneshot::Receiver<std::result::Result<(), String>>,
397}
398
399impl PendingRelaySync {
400    pub async fn wait(self) -> Result<()> {
401        self.response
402            .await
403            .context("session manager stopped")?
404            .map_err(anyhow::Error::msg)
405    }
406}