tools-kit 0.0.2

Portable Local and MCP tool execution with narrow extension traits.
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
use std::{
    future::Future,
    pin::Pin,
    sync::{Arc, Mutex},
    task::{Context, Poll},
};
use tokio::sync::{Notify, watch};
use tokio::task::JoinHandle;

use crate::{
    ToolError, ToolHandler,
    declaration::ToolInfo,
    local::ToolRegistration,
    mcp::McpClientConfig,
    name::ToolName,
    sources::{local::LocalToolset, mcp::McpToolset},
};

/// Public implementation contract for one shared executable tool source.
/// Use this trait directly with concrete caller implementations and their exact
/// associated handlers. [`Toolset::from_backend`] is optional normalization when a
/// supplied Toolset is needed. Shared shutdown obligations apply to both forms.
///
/// # Examples
///
/// ```
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), tools_kit::ToolError> {
/// use tools_kit::{Toolset, ToolsetBackend};
/// let tools = Toolset::local(Vec::new())?;
/// assert!(tools.list_tools().await?.is_empty());
/// tools.shutdown().await?;
///
/// # Ok(())
/// # }
/// ```
///
/// # Implement an executable caller Toolset
///
/// This runnable caller implementation executes a bounded string echo without tasks,
/// networking, or asynchronous cleanup. It has one unique terminal handler per call,
/// validates its advertised input shape itself, and seals root/handler admission on
/// shared shutdown. More complex implementations must own accepted asynchronous work
/// independently of a shutdown waiter. Neither this Toolset nor its Handler is Clone.
///
/// Direct use keeps the concrete associated child. Moving the same Toolset into
/// [`Toolset::from_backend`] is optional; normalization does not supply missing
/// backend validation, cancellation, or lifecycle behavior.
///
/// ```
/// use std::sync::{Arc, atomic::{AtomicBool, Ordering}};
/// use tools_kit::{ToolError, ToolHandlerBackend, Toolset, ToolsetBackend};
/// use tools_kit::declaration::{ToolInfo, ToolName};
/// use tools_kit::execution::{
///     ToolContent, ToolHandlerInput, ToolHandlerOutput, ToolInvocation, ToolResult,
/// };
/// use tools_kit::schema::JsonSchema;
///
/// struct EchoTools { info: ToolInfo, closed: Arc<AtomicBool> }
/// struct EchoHandler { name: ToolName, closed: Arc<AtomicBool>, terminal: bool }
/// impl EchoTools {
///     fn check(&self) -> Result<(), ToolError> {
///         if self.closed.load(Ordering::SeqCst) {
///             return Err(ToolError::Closed { operation: "echo toolset".into() });
///         }
///         Ok(())
///     }
/// }
/// impl ToolsetBackend for EchoTools {
///     type Handler = EchoHandler;
///     async fn list_tools(&self) -> Result<Vec<ToolInfo>, ToolError> {
///         self.check()?;
///         Ok(vec![self.info.clone()])
///     }
///     async fn tool_handler(&self, name: ToolName) -> Result<EchoHandler, ToolError> {
///         self.check()?;
///         if name != self.info.name { return Err(ToolError::UnknownTool { name }); }
///         Ok(EchoHandler { name, closed: Arc::clone(&self.closed), terminal: false })
///     }
///     async fn shutdown(&self) -> Result<(), ToolError> {
///         // No await: this synchronous toolset owns no unfinished external work.
///         self.closed.store(true, Ordering::SeqCst);
///         Ok(())
///     }
/// }
/// impl ToolHandlerBackend for EchoHandler {
///     async fn step(&mut self, input: ToolHandlerInput) -> Result<ToolHandlerOutput, ToolError> {
///         if self.closed.load(Ordering::SeqCst) {
///             return Err(ToolError::Closed { operation: "echo step".into() });
///         }
///         if self.terminal {
///             return Err(ToolError::Protocol { message: "handler already terminal".into() });
///         }
///         match input {
///             ToolHandlerInput::Invoke(invocation) => {
///                 let Some(text) = invocation.arguments.as_str().filter(|text| text.chars().count() <= 1024) else {
///                     return Err(ToolError::InvalidArguments {
///                         tool: self.name.clone(), message: "expected a string of at most 1024 characters".into(), issues: Vec::new(),
///                     });
///                 };
///                 self.terminal = true;
///                 Ok(ToolHandlerOutput::Completed(ToolResult::text(text)))
///             }
///             ToolHandlerInput::Cancel { reason } => {
///                 self.terminal = true;
///                 Err(ToolError::Cancelled { tool: self.name.clone(), reason })
///             }
///             ToolHandlerInput::Interaction(_) => Err(ToolError::Protocol {
///                 message: "this tool never requests interaction".into(),
///             }),
///         }
///     }
/// }
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), ToolError> {
/// let name = ToolName::new("echo")?;
/// let concrete = EchoTools {
///     info: ToolInfo::new(name.clone(), JsonSchema::strict(serde_json::json!({"type":"string","maxLength":1024}))),
///     closed: Arc::new(AtomicBool::new(false)),
/// };
/// let mut direct = concrete.tool_handler(name.clone()).await?;
/// let input = ToolInvocation {
///     call_id: Some("call-1".into()),
///     arguments: serde_json::json!("unchanged"),
/// };
/// let ToolHandlerOutput::Completed(result) = direct.step(ToolHandlerInput::Invoke(input.clone())).await?
///     else { panic!("echo does not interact") };
/// assert!(!result.is_error);
/// assert!(
///     matches!(result.content.as_slice(), [ToolContent::Text { text, .. }] if text == "unchanged")
/// );
/// assert!(direct.step(ToolHandlerInput::Invoke(input.clone())).await.is_err());
/// let tools = Toolset::from_backend(concrete); // Sole public erased ingress.
/// let mut normalized = tools.tool_handler(name).await?;
/// let observed = normalized.step(ToolHandlerInput::Invoke(input)).await;
/// let cleanup = tools.shutdown().await;
/// if let Err(error) = &cleanup {
///     eprintln!("Cleanup failed: {error}");
/// }
/// let ToolHandlerOutput::Completed(result) = observed? else { panic!("echo does not interact") };
/// assert!(
///     matches!(result.content.as_slice(), [ToolContent::Text { text, .. }] if text == "unchanged")
/// );
/// cleanup?;
/// assert!(tools.list_tools().await.is_err());
/// tools.shutdown().await?;
/// # Ok(())
/// # }
/// ```
///
pub trait ToolsetBackend: Send + Sync + 'static {
    /// Unique resumable handler produced by this tool source.
    type Handler: crate::ToolHandlerBackend;

    /// Lists the complete executable inventory.
    ///
    /// # Errors
    ///
    /// Returns [`ToolError::Closed`] after sealing, or the underlying inventory failure.
    ///
    /// See [`crate::ToolsetBackend`] for a complete example.
    ///
    fn list_tools(&self) -> impl Future<Output = Result<Vec<ToolInfo>, ToolError>> + Send + '_;

    /// Creates a unique handler for one selected tool name.
    ///
    /// # Errors
    ///
    /// Returns an unknown-name error when no declared tool matches, [`ToolError::Closed`] after sealing, or the underlying handler-construction failure.
    ///
    /// See [`crate::local::ToolRegistration`] for a complete example.
    ///
    fn tool_handler(
        &self,
        name: ToolName,
    ) -> impl Future<Output = Result<Self::Handler, ToolError>> + Send + '_;

    /// Seals admission and performs shared exactly-once cleanup.
    ///
    /// This contract applies to direct caller implementations as well as supplied
    /// handles. On first acceptance, retain and track the cleanup independently of
    /// this awaiting future. Dropping a shutdown waiter or the last public value
    /// must not abandon accepted cleanup. Repeated calls observe its terminal
    /// result; they must not start another cleanup. Listing and handler admission
    /// reject after sealing. A consumer may bound its wait with a deadline, so
    /// putting all cleanup solely inside this borrowed future is insufficient.
    ///
    /// # Errors
    ///
    /// Replays the shared cleanup error, including a cleanup timeout, to concurrent and later callers. A failed observation does not authorize abandoning accepted cleanup.
    ///
    /// # Examples
    ///
    /// ```
    ///
    /// # #[tokio::main(flavor = "current_thread")]
    /// # async fn main() -> Result<(), tools_kit::ToolError> {
    /// use tools_kit::{Toolset, ToolsetBackend};
    /// let tools = Toolset::local(Vec::new())?;
    /// let shared = tools.clone();
    /// tools.shutdown().await?;
    /// shared.shutdown().await?;
    /// assert!(shared.list_tools().await.is_err());
    ///
    /// # Ok(())
    /// # }
    /// ```
    ///
    fn shutdown(&self) -> impl Future<Output = Result<(), ToolError>> + Send + '_;
}

/// Opaque cloneable Toolset over Local, MCP, or downstream behavior.
/// Clones share admission and cleanup. Dropping a clone alone does not initiate
/// asynchronous cleanup; explicitly await [`ToolsetBackend::shutdown`].
///
/// See [`crate::ToolsetBackend`] for a complete example.
///
#[derive(Clone)]
pub struct Toolset {
    runtime: Arc<ToolsetRuntime>,
}

struct ToolsetRuntime {
    backend: InnerToolset,
    lifecycle: ToolsetLifecycle,
}

enum InnerToolset {
    Local(LocalToolset),
    Mcp(McpToolset),
    Extension(Arc<dyn ErasedToolsetBackend>),
}

impl Toolset {
    /// Builds an in-process Toolset from complete registrations.
    ///
    /// # Errors
    ///
    /// Returns [`ToolError::DuplicateName`] for duplicate names; an empty inventory is valid.
    ///
    /// See [`crate::ToolsetBackend`] for a complete example.
    ///
    pub fn local(registrations: Vec<ToolRegistration>) -> Result<Self, ToolError> {
        Ok(Self::from_local(LocalToolset::build(registrations)?))
    }

    /// Connects one MCP client Toolset and discovers its inventory.
    /// This example requires a running MCP service at the configured endpoint.
    ///
    /// # Errors
    ///
    /// Returns [`ToolError`] for invalid connection settings, process startup, transport or initialization failure, malformed inventory, or an elapsed request deadline.
    ///
    /// # Examples
    ///
    /// ```no_run
    ///
    /// # #[tokio::main(flavor = "current_thread")]
    /// # async fn main() -> Result<(), tools_kit::ToolError> {
    /// use tools_kit::mcp::{McpClientConfig, McpConnectionConfig};
    /// let connection = McpConnectionConfig::StreamableHttp {
    ///     endpoint: "http://127.0.0.1:8000/mcp".parse().unwrap(),
    ///     headers: Default::default(),
    /// };
    /// let config = McpClientConfig::new(connection);
    /// use tools_kit::{Toolset, ToolsetBackend};
    /// let tools = Toolset::mcp(config).await?;
    /// let inventory = tools.list_tools().await;
    /// let cleanup = tools.shutdown().await;
    /// if let Err(error) = &cleanup {
    ///     eprintln!("Cleanup failed: {error}");
    /// }
    /// let _ = inventory?;
    /// cleanup?;
    ///
    /// # Ok(())
    /// # }
    /// ```
    ///
    pub async fn mcp(config: McpClientConfig) -> Result<Self, ToolError> {
        Ok(Self::from_mcp(McpToolset::connect(config).await?))
    }

    /// Wraps a downstream Toolset implementation.
    /// The associated handler is normalized privately. Concrete callers may instead
    /// retain the backend directly; there is no public child normalization constructor.
    ///
    /// # Examples
    ///
    /// ```
    ///
    /// # #[tokio::main(flavor = "current_thread")]
    /// # async fn main() -> Result<(), tools_kit::ToolError> {
    /// use std::sync::atomic::{AtomicBool, Ordering};
    /// use tools_kit::{ToolError, Toolset, ToolsetBackend, ToolHandlerBackend};
    /// use tools_kit::declaration::{ToolInfo, ToolName};
    /// use tools_kit::execution::{ToolHandlerInput, ToolHandlerOutput};
    /// struct EmptyInventory { closed: AtomicBool }
    /// enum NoHandler {}
    /// impl ToolHandlerBackend for NoHandler {
    ///     async fn step(&mut self, _: ToolHandlerInput) -> Result<ToolHandlerOutput, ToolError> {
    ///         match *self {}
    ///     }
    /// }
    /// impl ToolsetBackend for EmptyInventory {
    ///     type Handler = NoHandler;
    ///     async fn list_tools(&self) -> Result<Vec<ToolInfo>, ToolError> {
    ///         if self.closed.load(Ordering::Acquire) {
    ///             return Err(ToolError::Closed { operation: "list_tools".into() });
    ///         }
    ///         Ok(Vec::new())
    ///     }
    ///     async fn tool_handler(&self, name: ToolName) -> Result<NoHandler, ToolError> {
    ///         if self.closed.load(Ordering::Acquire) {
    ///             return Err(ToolError::Closed { operation: "tool_handler".into() });
    ///         }
    ///         Err(ToolError::UnknownTool { name })
    ///     }
    ///     async fn shutdown(&self) -> Result<(), ToolError> {
    ///         self.closed.store(true, Ordering::Release);
    ///         Ok(()) // This empty implementation owns no asynchronous resources.
    ///     }
    /// }
    /// let concrete = EmptyInventory { closed: AtomicBool::new(false) };
    /// assert!(concrete.list_tools().await?.is_empty());
    /// let normalized = Toolset::from_backend(concrete);
    /// normalized.shutdown().await?;
    /// assert!(normalized.list_tools().await.is_err());
    ///
    /// # Ok(())
    /// # }
    /// ```
    ///
    #[must_use]
    pub fn from_backend<B: ToolsetBackend>(backend: B) -> Self {
        Self {
            runtime: Arc::new(ToolsetRuntime::new(InnerToolset::Extension(Arc::new(
                backend,
            )))),
        }
    }

    fn from_local(toolset: LocalToolset) -> Self {
        Self {
            runtime: Arc::new(ToolsetRuntime::new(InnerToolset::Local(toolset))),
        }
    }

    fn from_mcp(toolset: McpToolset) -> Self {
        Self {
            runtime: Arc::new(ToolsetRuntime::new(InnerToolset::Mcp(toolset))),
        }
    }
}

impl std::fmt::Debug for Toolset {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.debug_struct("Toolset").finish_non_exhaustive()
    }
}

trait ErasedToolsetBackend: Send + Sync {
    fn list_tools_erased(
        &self,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<ToolInfo>, ToolError>> + Send + '_>>;

    fn tool_handler_erased(
        &self,
        name: ToolName,
    ) -> Pin<Box<dyn Future<Output = Result<ToolHandler, ToolError>> + Send + '_>>;

    fn shutdown_erased(&self) -> Pin<Box<dyn Future<Output = Result<(), ToolError>> + Send + '_>>;
}

impl<B: ToolsetBackend> ErasedToolsetBackend for B {
    fn list_tools_erased(
        &self,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<ToolInfo>, ToolError>> + Send + '_>> {
        Box::pin(self.list_tools())
    }

    fn tool_handler_erased(
        &self,
        name: ToolName,
    ) -> Pin<Box<dyn Future<Output = Result<ToolHandler, ToolError>> + Send + '_>> {
        Box::pin(async move {
            self.tool_handler(name)
                .await
                .map(ToolHandler::from_extension)
        })
    }

    fn shutdown_erased(&self) -> Pin<Box<dyn Future<Output = Result<(), ToolError>> + Send + '_>> {
        Box::pin(self.shutdown())
    }
}

impl ToolsetBackend for InnerToolset {
    type Handler = ToolHandler;

    async fn list_tools(&self) -> Result<Vec<ToolInfo>, ToolError> {
        match self {
            Self::Local(toolset) => toolset.list_tools().await,
            Self::Mcp(toolset) => toolset.list_tools().await,
            Self::Extension(toolset) => toolset.list_tools_erased().await,
        }
    }

    async fn tool_handler(&self, name: ToolName) -> Result<Self::Handler, ToolError> {
        match self {
            Self::Local(toolset) => toolset
                .tool_handler(name)
                .await
                .map(ToolHandler::from_local),
            Self::Mcp(toolset) => toolset.tool_handler(name).await.map(ToolHandler::from_mcp),
            Self::Extension(toolset) => toolset.tool_handler_erased(name).await,
        }
    }

    async fn shutdown(&self) -> Result<(), ToolError> {
        match self {
            Self::Local(toolset) => toolset.shutdown().await,
            Self::Mcp(toolset) => toolset.shutdown().await,
            Self::Extension(toolset) => toolset.shutdown_erased().await,
        }
    }
}

impl ToolsetBackend for Toolset {
    type Handler = ToolHandler;

    async fn list_tools(&self) -> Result<Vec<ToolInfo>, ToolError> {
        let _admission = self.runtime.admit("list_tools")?;
        self.runtime.backend.list_tools().await
    }

    async fn tool_handler(&self, name: ToolName) -> Result<Self::Handler, ToolError> {
        let _admission = self.runtime.admit("tool_handler")?;
        self.runtime.backend.tool_handler(name).await
    }

    async fn shutdown(&self) -> Result<(), ToolError> {
        self.runtime.shutdown().await
    }
}

struct ToolsetLifecycle {
    state: Mutex<ToolsetState>,
    changed: Notify,
}

enum ToolsetState {
    Open {
        admitted: usize,
    },
    Closing {
        admitted: usize,
        completion: watch::Receiver<Option<Result<(), ToolError>>>,
        task: JoinHandle<()>,
    },
    Closed {
        terminal: Result<(), ToolError>,
    },
}

struct ToolsetAdmission {
    runtime: Arc<ToolsetRuntime>,
}

impl ToolsetRuntime {
    fn new(backend: InnerToolset) -> Self {
        Self {
            backend,
            lifecycle: ToolsetLifecycle {
                state: Mutex::new(ToolsetState::Open { admitted: 0 }),
                changed: Notify::new(),
            },
        }
    }

    fn admit(self: &Arc<Self>, operation: &'static str) -> Result<ToolsetAdmission, ToolError> {
        let mut state = self.lifecycle.state();
        match &mut *state {
            ToolsetState::Open { admitted } => {
                *admitted = admitted
                    .checked_add(1)
                    .ok_or_else(|| ToolError::Transport {
                        tool: None,
                        message: "toolset admission is exhausted".to_owned(),
                    })?;
                Ok(ToolsetAdmission {
                    runtime: Arc::clone(self),
                })
            }
            ToolsetState::Closing { .. } | ToolsetState::Closed { .. } => {
                Err(ToolError::closed(operation))
            }
        }
    }

    async fn shutdown(self: &Arc<Self>) -> Result<(), ToolError> {
        self.start_shutdown()?;
        self.lifecycle.terminal().await
    }

    fn start_shutdown(self: &Arc<Self>) -> Result<(), ToolError> {
        let mut state = self.lifecycle.state();
        let admitted = match &*state {
            ToolsetState::Open { admitted } => *admitted,
            ToolsetState::Closing { .. } | ToolsetState::Closed { .. } => return Ok(()),
        };
        let handle = tokio::runtime::Handle::try_current().map_err(|_| ToolError::Transport {
            tool: None,
            message: "toolset shutdown requires a Tokio runtime".to_owned(),
        })?;
        let (terminal, completion) = watch::channel(None);
        let runtime = Arc::clone(self);
        let task = handle.spawn(async move {
            terminal.send_replace(Some(runtime.finish_shutdown().await));
        });
        *state = ToolsetState::Closing {
            admitted,
            completion,
            task,
        };
        Ok(())
    }

    async fn finish_shutdown(self: Arc<Self>) -> Result<(), ToolError> {
        self.lifecycle.drain().await;
        self.backend.shutdown().await
    }
}

impl ToolsetLifecycle {
    fn state(&self) -> std::sync::MutexGuard<'_, ToolsetState> {
        self.state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    fn release(&self) {
        let should_notify = {
            let mut state = self.state();
            match &mut *state {
                ToolsetState::Open { admitted } => {
                    debug_assert_ne!(*admitted, 0);
                    *admitted = admitted.saturating_sub(1);
                    false
                }
                ToolsetState::Closing { admitted, .. } => {
                    debug_assert_ne!(*admitted, 0);
                    *admitted = admitted.saturating_sub(1);
                    *admitted == 0
                }
                ToolsetState::Closed { .. } => {
                    debug_assert!(false, "closed toolset retained an admission");
                    false
                }
            }
        };
        if should_notify {
            self.changed.notify_waiters();
        }
    }

    async fn drain(&self) {
        loop {
            let notified = self.changed.notified();
            tokio::pin!(notified);
            notified.as_mut().enable();
            let drained = match &*self.state() {
                ToolsetState::Open { admitted } | ToolsetState::Closing { admitted, .. } => {
                    *admitted == 0
                }
                ToolsetState::Closed { .. } => true,
            };
            if drained {
                return;
            }
            notified.await;
        }
    }

    async fn terminal(&self) -> Result<(), ToolError> {
        let mut completion = match &*self.state() {
            ToolsetState::Closing { completion, .. } => completion.clone(),
            ToolsetState::Closed { terminal } => return terminal.clone(),
            ToolsetState::Open { .. } => {
                return Err(ToolError::Transport {
                    tool: None,
                    message: "toolset shutdown lost lifecycle ownership".to_owned(),
                });
            }
        };
        let terminal = match completion.wait_for(Option::is_some).await {
            Ok(terminal) => terminal
                .as_ref()
                .cloned()
                .ok_or_else(|| ToolError::Transport {
                    tool: None,
                    message: "toolset shutdown result is unavailable".to_owned(),
                })?,
            Err(_) => Err(ToolError::Transport {
                tool: None,
                message: "toolset shutdown owner exited without a terminal result".to_owned(),
            }),
        };
        loop {
            let settled = {
                let mut state = self.state();
                match &mut *state {
                    ToolsetState::Closed { terminal } => return terminal.clone(),
                    ToolsetState::Closing { task, .. } if task.is_finished() => {
                        let waker = std::task::Waker::noop();
                        let mut context = Context::from_waker(waker);
                        match Pin::new(task).poll(&mut context) {
                            Poll::Ready(joined) => {
                                let terminal = if joined.is_ok() {
                                    terminal.clone()
                                } else {
                                    Err(ToolError::Transport {
                                        tool: None,
                                        message: "toolset shutdown owner could not be joined"
                                            .to_owned(),
                                    })
                                };
                                *state = ToolsetState::Closed {
                                    terminal: terminal.clone(),
                                };
                                Some(terminal)
                            }
                            Poll::Pending => None,
                        }
                    }
                    ToolsetState::Closing { .. } => None,
                    ToolsetState::Open { .. } => {
                        let terminal = Err(ToolError::Transport {
                            tool: None,
                            message: "toolset shutdown lost lifecycle ownership".to_owned(),
                        });
                        *state = ToolsetState::Closed {
                            terminal: terminal.clone(),
                        };
                        Some(terminal)
                    }
                }
            };
            if let Some(terminal) = settled {
                self.changed.notify_waiters();
                return terminal;
            }
            tokio::task::yield_now().await;
        }
    }
}

impl Drop for ToolsetAdmission {
    fn drop(&mut self) {
        self.runtime.lifecycle.release();
    }
}