lash-core 0.1.0-alpha.1

Sans-IO turn machine and runtime kernel for the lash agent runtime.
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex as StdMutex};

use serde_json::json;
use tokio::sync::Notify;
use tokio_util::sync::CancellationToken;

use super::Session;
use super::execution_context::ModeExecutionContext;
use super::tool_execution::ModeToolReply;
use crate::tool_dispatch::{ToolDispatchOutcome, dispatch_tool_call_with_execution_context};
use crate::{SandboxMessage, ToolCallRecord, ToolContext};

const ASYNC_TOOL_HANDLE_KIND: &str = "task";

pub(crate) type AsyncToolHandleMap = Arc<StdMutex<HashMap<String, AsyncToolHandleEntry>>>;

#[derive(Clone)]
pub(crate) struct AsyncToolHandleEntry {
    pub(super) state: Arc<StdMutex<AsyncToolHandleState>>,
    pub(super) done_notify: Arc<Notify>,
    pub(super) progress_notify: Arc<Notify>,
    pub(super) cancellation: CancellationToken,
    pub(super) metadata: AsyncToolHandleMetadata,
}

#[derive(Clone)]
pub(super) struct AsyncToolHandleMetadata {
    pub(super) tool_name: String,
    pub(super) namespace: AsyncToolHandleNamespace,
    pub(super) identifier: String,
}

#[derive(Clone, PartialEq, Eq)]
pub(super) enum AsyncToolHandleNamespace {
    Monitor,
    Subagent,
    Tool,
}

pub(super) struct AsyncToolHandleState {
    pub(super) join_handle: Option<tokio::task::JoinHandle<()>>,
    pub(super) buffered_messages: Vec<SandboxMessage>,
    pub(super) terminal: Option<AsyncToolTerminal>,
}

#[derive(Clone)]
#[allow(clippy::large_enum_variant)]
pub(super) enum AsyncToolTerminal {
    Completed(ToolDispatchOutcome),
    Cancelled,
    Failed(String),
}

impl AsyncToolHandleEntry {
    pub(super) fn empty_monitor(metadata: AsyncToolHandleMetadata) -> Self {
        Self {
            state: Arc::new(StdMutex::new(AsyncToolHandleState {
                join_handle: None,
                buffered_messages: Vec::new(),
                terminal: None,
            })),
            done_notify: Arc::new(Notify::new()),
            progress_notify: Arc::new(Notify::new()),
            cancellation: CancellationToken::new(),
            metadata,
        }
    }
}

pub(crate) fn live_session_async_handle_ids(map: &AsyncToolHandleMap) -> HashSet<String> {
    map.lock()
        .expect("async tool handle map lock")
        .iter()
        .filter_map(|(id, entry)| {
            if entry.metadata.namespace == AsyncToolHandleNamespace::Monitor {
                return None;
            }
            let is_live = entry
                .state
                .lock()
                .expect("async tool state lock")
                .terminal
                .is_none();
            is_live.then(|| id.clone())
        })
        .collect()
}

pub(crate) fn transfer_session_async_handles(
    from: &AsyncToolHandleMap,
    to: &AsyncToolHandleMap,
    ids: &HashSet<String>,
) -> Result<(), String> {
    if ids.is_empty() || Arc::ptr_eq(from, to) {
        return Ok(());
    }
    let mut from_guard = from.lock().map_err(|_| "async handle map lock poisoned")?;
    let mut to_guard = to.lock().map_err(|_| "async handle map lock poisoned")?;
    for id in ids {
        if to_guard.contains_key(id) {
            return Err(format!("async handle `{id}` already exists in successor"));
        }
    }
    for id in ids {
        if let Some(entry) = from_guard.remove(id) {
            to_guard.insert(id.clone(), entry);
        }
    }
    Ok(())
}

pub(crate) async fn cancel_unreferenced_session_async_handles(
    map: &AsyncToolHandleMap,
    keep: &HashSet<String>,
) -> Result<(), String> {
    let entries = {
        let guard = map.lock().map_err(|_| "async handle map lock poisoned")?;
        guard
            .iter()
            .filter_map(|(id, entry)| {
                if keep.contains(id)
                    || entry.metadata.namespace == AsyncToolHandleNamespace::Monitor
                {
                    return None;
                }
                let is_live = entry
                    .state
                    .lock()
                    .expect("async tool state lock")
                    .terminal
                    .is_none();
                is_live.then(|| (id.clone(), entry.clone()))
            })
            .collect::<Vec<_>>()
    };

    for (_id, entry) in entries {
        cancel_session_async_handle_entry(&entry).await;
    }
    Ok(())
}

async fn cancel_session_async_handle_entry(entry: &AsyncToolHandleEntry) {
    entry.cancellation.cancel();
    let _ = tokio::time::timeout(
        std::time::Duration::from_millis(100),
        entry.done_notify.notified(),
    )
    .await;
    let join_handle = {
        let mut guard = entry.state.lock().expect("async tool state lock");
        if guard.terminal.is_none() {
            guard.join_handle.take()
        } else {
            None
        }
    };
    if let Some(handle) = join_handle {
        handle.abort();
        let _ = handle.await;
    }
    {
        let mut guard = entry.state.lock().expect("async tool state lock");
        if guard.terminal.is_none() {
            guard.terminal = Some(AsyncToolTerminal::Cancelled);
        }
    }
    entry.progress_notify.notify_waiters();
    entry.done_notify.notify_waiters();
}

impl Session {
    pub(crate) fn async_tool_handle_map(&self) -> AsyncToolHandleMap {
        Arc::clone(&self.async_tool_handles)
    }
}

impl ModeExecutionContext {
    pub(super) fn async_tool_handle_value(id: &str, tool_name: &str) -> serde_json::Value {
        json!({
            "__handle__": ASYNC_TOOL_HANDLE_KIND,
            "id": id,
            "tool": tool_name,
        })
    }

    pub(super) fn normalize_async_subagent_name(agent_name: &str) -> Option<String> {
        let mut out = String::new();
        let mut last_was_sep = false;
        for ch in agent_name.chars().flat_map(char::to_lowercase) {
            if ch.is_ascii_alphanumeric() {
                out.push(ch);
                last_was_sep = false;
            } else if !last_was_sep && !out.is_empty() {
                out.push('_');
                last_was_sep = true;
            }
        }
        while out.ends_with('_') {
            out.pop();
        }
        (!out.is_empty()).then_some(out)
    }

    pub(super) fn async_tool_handle_metadata(
        id: &str,
        tool_name: &str,
        args: &serde_json::Value,
    ) -> AsyncToolHandleMetadata {
        if tool_name == "spawn_agent"
            && let Some(agent_name) = args.get("agent_name").and_then(|value| value.as_str())
            && let Some(normalized) = Self::normalize_async_subagent_name(agent_name)
        {
            return AsyncToolHandleMetadata {
                tool_name: tool_name.to_string(),
                namespace: AsyncToolHandleNamespace::Subagent,
                identifier: normalized,
            };
        }
        AsyncToolHandleMetadata {
            tool_name: tool_name.to_string(),
            namespace: AsyncToolHandleNamespace::Tool,
            identifier: id.to_string(),
        }
    }

    pub(super) fn parse_async_tool_handle(
        handle: &serde_json::Value,
    ) -> Result<(String, Option<String>), String> {
        let kind = handle
            .get("__handle__")
            .and_then(|value| value.as_str())
            .ok_or_else(|| "Invalid async handle: missing `__handle__`".to_string())?;
        if kind != ASYNC_TOOL_HANDLE_KIND {
            return Err(format!("Invalid async handle kind: {kind}"));
        }
        let id = handle
            .get("id")
            .and_then(|value| value.as_str())
            .filter(|value| !value.is_empty())
            .ok_or_else(|| "Invalid async handle: missing `id`".to_string())?;
        let tool_name = handle
            .get("tool")
            .and_then(|value| value.as_str())
            .map(str::to_string);
        Ok((id.to_string(), tool_name))
    }

    pub(super) fn async_tool_handle_entry(&self, id: &str) -> Option<AsyncToolHandleEntry> {
        self.async_tool_handles.lock().ok()?.get(id).cloned()
    }

    pub(super) fn flush_async_tool_messages(&self, entry: &AsyncToolHandleEntry) {
        let Some(message_tx) = self.message_tx.as_ref() else {
            return;
        };
        let pending = {
            let mut state = entry.state.lock().expect("async tool state lock");
            std::mem::take(&mut state.buffered_messages)
        };
        for message in pending {
            let _ = message_tx.send(message);
        }
    }

    pub(super) async fn start_async_tool_call(
        &self,
        call_id: String,
        tool_name: String,
        args: serde_json::Value,
    ) -> ModeToolReply {
        let handle_id = uuid::Uuid::new_v4().to_string();
        let state = Arc::new(StdMutex::new(AsyncToolHandleState {
            join_handle: None,
            buffered_messages: Vec::new(),
            terminal: None,
        }));
        let done_notify = Arc::new(Notify::new());
        let progress_notify = Arc::new(Notify::new());
        let cancellation = CancellationToken::new();
        let entry = AsyncToolHandleEntry {
            state: Arc::clone(&state),
            done_notify: Arc::clone(&done_notify),
            progress_notify: Arc::clone(&progress_notify),
            cancellation: cancellation.clone(),
            metadata: Self::async_tool_handle_metadata(&handle_id, &tool_name, &args),
        };
        self.async_tool_handles
            .lock()
            .expect("async tool handle map lock")
            .insert(handle_id.clone(), entry);

        let (progress_tx, mut progress_rx) = tokio::sync::mpsc::unbounded_channel();
        let progress_state = Arc::clone(&state);
        let progress_notify_task = Arc::clone(&progress_notify);
        tokio::spawn(async move {
            while let Some(message) = progress_rx.recv().await {
                {
                    let mut guard = progress_state.lock().expect("async tool state lock");
                    guard.buffered_messages.push(message);
                }
                progress_notify_task.notify_waiters();
            }
            progress_notify_task.notify_waiters();
        });

        let task_state = Arc::clone(&state);
        let task_done_notify = Arc::clone(&done_notify);
        let task_progress_notify = Arc::clone(&progress_notify);
        let task_handle_id = handle_id.clone();
        let task_tool_name = tool_name.clone();
        let task_args = args.clone();
        let dispatch = Arc::clone(&self.dispatch);
        let async_call_id = handle_id.clone();
        let join_handle = tokio::spawn(async move {
            let tool_context = ToolContext::new(
                dispatch.session_id.clone(),
                Arc::clone(&dispatch.host),
                dispatch.turn_context.clone(),
                Arc::clone(&dispatch.attachment_store),
                Some(async_call_id),
            )
            .with_async_task(task_handle_id.clone(), cancellation.clone());
            let outcome = dispatch_tool_call_with_execution_context(
                &dispatch,
                task_tool_name,
                task_args,
                Some(&progress_tx),
                tool_context,
            )
            .await;
            drop(progress_tx);
            let mut guard = task_state.lock().expect("async tool state lock");
            if guard.terminal.is_none() {
                guard.terminal = Some(AsyncToolTerminal::Completed(outcome));
            }
            drop(guard);
            task_progress_notify.notify_waiters();
            task_done_notify.notify_waiters();
        });

        state.lock().expect("async tool state lock").join_handle = Some(join_handle);

        let handle_value = Self::async_tool_handle_value(&handle_id, &tool_name);
        let record = ToolCallRecord {
            call_id: Some(call_id),
            tool: tool_name,
            args,
            output: crate::ToolCallOutput::success(handle_value.clone()),
            duration_ms: 0,
        };
        ModeToolReply::success(handle_value).with_record(record)
    }

    pub(super) async fn await_async_tool_handle(&self, handle: serde_json::Value) -> ModeToolReply {
        let (handle_id, hinted_tool_name) = match Self::parse_async_tool_handle(&handle) {
            Ok(parsed) => parsed,
            Err(err) => return ModeToolReply::error(json!(err)),
        };
        let Some(entry) = self.async_tool_handle_entry(&handle_id) else {
            if hinted_tool_name.as_deref() == Some("monitor") || handle_id.starts_with("monitor:") {
                return self.await_monitor_handle(&handle_id).await;
            }
            return ModeToolReply::error(json!(format!("Unknown async handle: {handle_id}")));
        };
        if entry.metadata.namespace == AsyncToolHandleNamespace::Monitor {
            return self.await_monitor_handle(&handle_id).await;
        }

        loop {
            self.flush_async_tool_messages(&entry);
            let is_done = {
                let guard = entry.state.lock().expect("async tool state lock");
                guard.terminal.is_some()
            };
            if is_done {
                break;
            }
            tokio::select! {
                _ = entry.done_notify.notified() => {}
                _ = entry.progress_notify.notified() => {}
            }
        }
        self.flush_async_tool_messages(&entry);

        let join_handle = {
            let mut guard = entry.state.lock().expect("async tool state lock");
            guard.join_handle.take()
        };
        if let Some(handle) = join_handle
            && let Err(err) = handle.await
            && !err.is_cancelled()
        {
            let mut guard = entry.state.lock().expect("async tool state lock");
            if guard.terminal.is_none() {
                guard.terminal = Some(AsyncToolTerminal::Failed(format!(
                    "async tool task failed: {err}"
                )));
            }
        }

        let terminal = {
            let guard = entry.state.lock().expect("async tool state lock");
            guard.terminal.clone()
        };

        match terminal {
            Some(AsyncToolTerminal::Completed(outcome)) => {
                ModeToolReply::from_output(outcome.record.output)
            }
            Some(AsyncToolTerminal::Cancelled) => {
                ModeToolReply::cancelled("async task was cancelled")
            }
            Some(AsyncToolTerminal::Failed(err)) => ModeToolReply::error(json!(err)),
            None => ModeToolReply::error(json!("async task did not produce a result")),
        }
    }

    pub(super) async fn cancel_async_tool_handle(
        &self,
        handle: serde_json::Value,
    ) -> ModeToolReply {
        let (handle_id, hinted_tool_name) = match Self::parse_async_tool_handle(&handle) {
            Ok(parsed) => parsed,
            Err(err) => return ModeToolReply::error(json!(err)),
        };
        let Some(entry) = self.async_tool_handle_entry(&handle_id) else {
            if hinted_tool_name.as_deref() == Some("monitor") || handle_id.starts_with("monitor:") {
                return self.cancel_monitor_handle(&handle_id).await;
            }
            return ModeToolReply::error(json!(format!("Unknown async handle: {handle_id}")));
        };
        if entry.metadata.namespace == AsyncToolHandleNamespace::Monitor {
            return self.cancel_monitor_handle(&handle_id).await;
        }

        cancel_session_async_handle_entry(&entry).await;
        self.flush_async_tool_messages(&entry);

        ModeToolReply::success(serde_json::Value::Null)
    }
}