merry-runtime 0.1.2

Structured session, task ledger, context compaction, memory, permission, and tool runtime for Merry.
Documentation
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Interactive producer lifecycle and input-boundary orchestration.

use super::super::{
    commands::{BoundaryAction, CommandDecision, CommandHandlingMode},
    queue::{AcceptedQueuedInput, step_input_from_accepted},
    types::InteractiveError,
};
use super::InteractiveProducer;
use crate::StepInput;
use merry_core::{InteractiveRunState, RuntimeEvent};

impl InteractiveProducer {
    pub(crate) async fn run(mut self) -> Result<(), InteractiveError> {
        if !self.send_state(InteractiveRunState::WaitingForInput).await {
            return Ok(());
        }
        while !self.loop_token.is_cancelled() {
            let subagent_completion_notify = self.subagent_completion_notify.clone();
            let wait_for_subagent_completion = async move {
                match subagent_completion_notify {
                    Some(notify) => notify.notified().await,
                    None => std::future::pending::<()>().await,
                }
            };
            let command = tokio::select! {
                command = self.command_receiver.recv() => command,
                subagent_event = self.subagent_event_receiver.recv() => {
                    if !self.forward_subagent_event(subagent_event).await {
                        return Ok(());
                    }
                    continue;
                }
                _ = wait_for_subagent_completion => {
                    // A completion can be acknowledged by wait_subagents while
                    // this Notify permit is still pending. Only start a
                    // continuation when the queue still contains a notification.
                    if !self.runtime.has_subagent_completion_notifications().await {
                        continue;
                    }
                    if !self.run_coordinator_continuation().await {
                        return self.stop_result();
                    }
                    continue;
                }
            };
            let Some(command) = command else {
                break;
            };

            let Some(decision) = self
                .handle_command(command, CommandHandlingMode::Waiting)
                .await
            else {
                return self.stop_result();
            };
            match decision {
                CommandDecision::Continue => {}
                CommandDecision::RunContinuation => {
                    if !self.run_coordinator_continuation().await {
                        return self.stop_result();
                    }
                }
                CommandDecision::RunPending => {
                    if !self.run_pending_burst().await {
                        return self.stop_result();
                    }
                }
                CommandDecision::RunCompaction {
                    focus,
                    policy,
                    ack_sender,
                } => {
                    if !self.run_manual_compaction(focus, policy, ack_sender).await {
                        return self.stop_result();
                    }
                }
                CommandDecision::Close => {
                    break;
                }
            }
        }

        let _ = self
            .send_event(RuntimeEvent::InteractiveRunStateChanged {
                state: InteractiveRunState::Closed,
            })
            .await;
        self.runtime.close_trajectory();
        let event = RuntimeEvent::Closed;
        let _ = self.send_event(event).await;
        Ok(())
    }

    pub(super) fn stop_result(&mut self) -> Result<(), InteractiveError> {
        self.terminal_error.take().map_or(Ok(()), Err)
    }

    pub(super) async fn run_pending_burst(&mut self) -> bool {
        let accepted = self.queue.accept_all_pending();
        if accepted.is_empty() {
            return self.send_state(InteractiveRunState::WaitingForInput).await;
        }
        self.run_accepted_steps(accepted).await
    }

    pub(super) async fn run_accepted_steps(
        &mut self,
        mut accepted: Vec<AcceptedQueuedInput>,
    ) -> bool {
        self.interrupted = false;
        loop {
            if let Some(event) = self.runtime.activate_pending_plan_mode_for_prompt().await
                && !self.send_runtime_events(vec![event]).await
            {
                return false;
            }
            let Some(input) = step_input_from_accepted(&accepted) else {
                return false;
            };
            let Some(step_events) = self.run_model_phase(input, Some(&accepted)).await else {
                return false;
            };

            let Some(continuation_required) = self.handle_step_outcome(&step_events).await else {
                return false;
            };

            if !self.drain_ready_commands().await {
                return false;
            }

            self.refresh_subagent_continuation_request().await;
            match self.boundary_action(continuation_required) {
                BoundaryAction::UserInput {
                    accepted: next_accepted,
                } => {
                    accepted = next_accepted;
                }
                BoundaryAction::Continuation => {
                    let Some(boundary) = self.run_continuation_steps().await else {
                        return false;
                    };
                    match boundary {
                        BoundaryAction::UserInput {
                            accepted: next_accepted,
                        } => {
                            accepted = next_accepted;
                        }
                        BoundaryAction::Continuation => continue,
                        BoundaryAction::Wait => {
                            if let Some(event) =
                                self.runtime.complete_deferred_plan_mode_exit().await
                                && !self.send_runtime_events(vec![event]).await
                            {
                                return false;
                            }
                            return self.send_state(InteractiveRunState::WaitingForInput).await;
                        }
                    }
                }
                BoundaryAction::Wait => {
                    if let Some(event) = self.runtime.complete_deferred_plan_mode_exit().await
                        && !self.send_runtime_events(vec![event]).await
                    {
                        return false;
                    }
                    return self.send_state(InteractiveRunState::WaitingForInput).await;
                }
            }
        }
    }

    pub(super) async fn run_continuation_steps(&mut self) -> Option<BoundaryAction> {
        let mut first_step = true;
        loop {
            let notification_input = self.take_subagent_notification_input().await;
            let input = if let Some(input) = notification_input {
                input
            } else if let Some(note) = self.hook_continuation_note.take() {
                match StepInput::loop_control_text(&note) {
                    Ok(input) => input,
                    Err(error) => {
                        self.remember_terminal_error(InteractiveError::Runtime { source: error });
                        return None;
                    }
                }
            } else if first_step {
                first_step = false;
                match self.coordinator_continuation_note.take() {
                    Some(note) => match StepInput::loop_control_text(&note) {
                        Ok(input) => input,
                        Err(error) => {
                            self.remember_terminal_error(InteractiveError::Runtime {
                                source: error,
                            });
                            return None;
                        }
                    },
                    None => StepInput::no_new_user_input(),
                }
            } else {
                StepInput::no_new_user_input()
            };
            let step_events = self.run_model_phase(input, None).await?;
            let continuation_required = self.handle_step_outcome(&step_events).await?;
            if !self.drain_ready_commands().await {
                return None;
            }
            self.refresh_subagent_continuation_request().await;
            let boundary = self.boundary_action(continuation_required);
            if matches!(boundary, BoundaryAction::Continuation) {
                continue;
            }
            return Some(boundary);
        }
    }

    pub(super) async fn take_subagent_notification_input(&self) -> Option<StepInput> {
        let statuses = self.runtime.take_subagent_completion_notifications().await;
        if statuses.is_empty() {
            return None;
        }
        let text = crate::subagent::completion_notification_text(&statuses);
        match StepInput::loop_control_text(&text) {
            Ok(input) => Some(input),
            Err(error) => {
                tracing::warn!(%error, "discarding invalid subagent completion notification");
                None
            }
        }
    }

    pub(super) async fn run_coordinator_continuation(&mut self) -> bool {
        let Some(boundary) = self.run_continuation_steps().await else {
            return false;
        };
        match boundary {
            BoundaryAction::UserInput { accepted } => self.run_accepted_steps(accepted).await,
            BoundaryAction::Wait => {
                if let Some(event) = self.runtime.complete_deferred_plan_mode_exit().await
                    && !self.send_runtime_events(vec![event]).await
                {
                    return false;
                }
                self.send_state(InteractiveRunState::WaitingForInput).await
            }
            BoundaryAction::Continuation => true,
        }
    }

    pub(super) async fn run_manual_compaction(
        &mut self,
        focus: Option<String>,
        policy: Option<crate::CitationCompactionPolicy>,
        ack_sender: tokio::sync::oneshot::Sender<
            Result<Option<crate::CompactionOutcome>, InteractiveError>,
        >,
    ) -> bool {
        self.interrupted = false;
        let phase_token = self.loop_token.child_token();
        self.phase_token = Some(phase_token.clone());
        if !self.send_state(InteractiveRunState::RunningModel).await {
            self.phase_token = None;
            return false;
        }

        let (started_tx, mut started_rx) = tokio::sync::oneshot::channel();
        let runtime = self.runtime.clone();
        let permit = self.loop_permit.clone();
        let compact_fut = runtime.compact_context_with_active_permit(
            policy,
            focus,
            phase_token.clone(),
            permit,
            started_tx,
        );
        tokio::pin!(compact_fut);

        let mut started_emitted = false;
        let mut commands_open = true;
        let result = loop {
            tokio::select! {
                result = &mut compact_fut => break result,
                started = &mut started_rx, if !started_emitted => {
                    started_emitted = true;
                    if started.is_ok() {
                        let event = self.runtime.record_manual_compaction_started().await;
                        if !self.send_runtime_events(vec![event]).await {
                            self.phase_token = None;
                            return false;
                        }
                    }
                }
                command = self.command_receiver.recv(), if commands_open => {
                    let Some(command) = command else {
                        commands_open = false;
                        continue;
                    };
                    if self
                        .handle_command(command, CommandHandlingMode::Running)
                        .await
                        .is_none()
                    {
                        self.phase_token = None;
                        return false;
                    }
                }
                subagent_event = self.subagent_event_receiver.recv() => {
                    if !self.forward_subagent_event(subagent_event).await {
                        self.phase_token = None;
                        return false;
                    }
                }
            }
        };

        self.phase_token = None;
        if !started_emitted && started_rx.try_recv().is_ok() {
            let event = self.runtime.record_manual_compaction_started().await;
            if !self.send_runtime_events(vec![event]).await {
                return false;
            }
        }

        match result {
            Ok(Some(outcome)) => {
                let event = self
                    .runtime
                    .record_manual_compaction_completed(&outcome)
                    .await;
                if !self.send_runtime_events(vec![event]).await {
                    return false;
                }
                let _ = ack_sender.send(Ok(Some(outcome)));
            }
            Ok(None) => {
                let _ = ack_sender.send(Ok(None));
            }
            Err(error) => {
                if self.interrupted
                    || (phase_token.is_cancelled() && !self.loop_token.is_cancelled())
                {
                    let event = self
                        .runtime
                        .record_manual_compaction_cancelled("compaction cancelled by user")
                        .await;
                    if !self.send_runtime_events(vec![event]).await {
                        return false;
                    }
                } else if !self.loop_token.is_cancelled() {
                    let event = self
                        .runtime
                        .record_manual_compaction_failed(&error.to_string())
                        .await;
                    if !self.send_runtime_events(vec![event]).await {
                        return false;
                    }
                }
                let _ = ack_sender.send(Err(InteractiveError::from(error)));
            }
        }

        if !self.drain_ready_commands().await {
            return false;
        }
        self.refresh_subagent_continuation_request().await;
        match self.boundary_action(false) {
            BoundaryAction::UserInput { accepted } => self.run_accepted_steps(accepted).await,
            BoundaryAction::Continuation => self.run_coordinator_continuation().await,
            BoundaryAction::Wait => self.send_state(InteractiveRunState::WaitingForInput).await,
        }
    }

    pub(super) async fn drain_ready_commands(&mut self) -> bool {
        while let Ok(command) = self.command_receiver.try_recv() {
            if self
                .handle_command(command, CommandHandlingMode::Running)
                .await
                .is_none()
            {
                return false;
            }
        }
        true
    }

    pub(super) async fn refresh_subagent_continuation_request(&mut self) {
        if self.runtime.has_subagent_completion_notifications().await {
            self.subagent_continuation_requested = true;
        }
    }

    pub(super) fn boundary_action(&mut self, continuation_required: bool) -> BoundaryAction {
        if self.interrupted {
            return BoundaryAction::Wait;
        }

        if self.subagent_continuation_requested {
            self.subagent_continuation_requested = false;
            return BoundaryAction::Continuation;
        }

        if self.queue.can_auto_dispatch() {
            self.interrupted = false;
            return BoundaryAction::UserInput {
                accepted: self.queue.accept_all_pending(),
            };
        }

        if continuation_required || self.coordinator_continuation_note.is_some() {
            return BoundaryAction::Continuation;
        }

        BoundaryAction::Wait
    }
}