dpc-tau-ext-shell 0.2.1

A minimal Unix-first coding agent.
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
//! Shell-facing artifact transfer state and off-loop filesystem preparation.

use std::collections::{HashMap, VecDeque};
#[cfg(unix)]
use std::fs::OpenOptions;
use std::fs::{self, File};
use std::io::{Read, Write};
#[cfg(unix)]
use std::os::unix::fs::OpenOptionsExt;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};

use tau_client::{ArtifactClient, ArtifactDownload, ArtifactUpload};
use tau_proto::{
    ARTIFACT_MAX_BYTES, ArtifactError, ArtifactRequestId, ArtifactResult, CborValue, Event,
    HarnessOutputMessage, ToolCancelled, ToolError, ToolResult, ToolResultKind, ToolStarted,
    artifact_reference, parse_artifact_reference,
};

use crate::Output;
use crate::scheduler::{WorkMeta, WorkPriority, WorkScheduler};
use crate::tool_lifecycle::ToolLifecycle;
use crate::tools::{EXPORT_TOOL_NAME, IMPORT_TOOL_NAME};

#[cfg(test)]
mod tests;

/// Maximum verified import bytes waiting for or executing local temp writes.
const IMPORT_WRITE_BYTES_LIMIT: u64 = ARTIFACT_MAX_BYTES;
/// Covers all scheduler-queued and running artifact jobs without blocking them.
const ARTIFACT_COMMAND_LIMIT: usize = 96;

/// Cloneable worker-side entry point for preparing artifact operations.
#[derive(Clone)]
pub(crate) struct ArtifactTransferControl {
    /// Bounded nonblocking completion queue consumed by the main loop.
    queue: Arc<Mutex<VecDeque<Command>>>,
    /// Manual-loop wake handle installed after startup.
    waker: Arc<Mutex<Option<tau_client::ManualRuntimeWaker>>>,
}

/// Main-loop-owned artifact transfers and exact request correlation.
pub(crate) struct ArtifactTransferManager {
    /// Typed nonblocking RPC writer, absent only in direct state-machine tests.
    client: Option<ArtifactClient>,
    /// Immutable current session used for exact request admission.
    session: Option<tau_proto::SessionId>,
    /// Worker-side control handle.
    control: ArtifactTransferControl,
    /// Active transfers by tool call.
    transfers: HashMap<tau_proto::ToolCallId, Transfer>,
    /// Exactly one outstanding request mapped back to its transfer.
    requests: HashMap<ArtifactRequestId, tau_proto::ToolCallId>,
    /// Independent byte budget for imported originals moved into shell workers.
    import_write_budget: ImportWriteBudget,
    #[cfg(test)]
    /// Deterministically rejects the next ordinary transfer operation in tests.
    reject_next_request: bool,
}

/// One off-loop preparation or finalization completion.
enum Command {
    /// A local regular file has been read and bounded.
    StartExport {
        /// Original invocation identity.
        invoke: ToolStarted,
        /// Lifecycle retained until the terminal is flushed.
        lifecycle: ToolLifecycle,
        /// Retry-safe upload state.
        upload: ArtifactUpload,
        /// Bounded per-use filename hint.
        filename: Option<String>,
    },
    /// A key has been validated and is ready to open.
    StartImport {
        /// Original invocation identity.
        invoke: ToolStarted,
        /// Lifecycle retained until the terminal is flushed.
        lifecycle: ToolLifecycle,
        /// Verified download state.
        download: ArtifactDownload,
    },
    /// Imported bytes have been written to a private local temporary file.
    ImportWritten {
        /// Owning tool call retained by the main-loop transfer.
        call_id: tau_proto::ToolCallId,
        /// Descriptor size from the verified download.
        size: u64,
        /// Completed local path or filesystem failure.
        result: Result<RetainedTemp, String>,
    },
}

/// Main-loop transfer state.
enum Transfer {
    /// Upload of one already-read original.
    Export {
        /// Original invocation identity.
        invoke: ToolStarted,
        /// Lifecycle retained until terminal publication.
        lifecycle: ToolLifecycle,
        /// Retry-safe typed upload helper.
        upload: ArtifactUpload,
        /// Bounded per-use filename hint.
        filename: Option<String>,
    },
    /// Download of one validated key.
    Import {
        /// Original invocation identity.
        invoke: ToolStarted,
        /// Lifecycle retained until terminal publication.
        lifecycle: ToolLifecycle,
        /// Verified typed download helper.
        download: ArtifactDownload,
    },
    /// Verified bytes are being written to a private local temporary file.
    ImportWriting {
        /// Original invocation identity.
        invoke: ToolStarted,
        /// Lifecycle retained until terminal publication.
        lifecycle: ToolLifecycle,
        /// Cancellation observed directly by the off-loop writer.
        cancel: Arc<AtomicBool>,
    },
}

/// Shared explicit admission budget for imported bytes owned by write jobs.
#[derive(Clone, Default)]
struct ImportWriteBudget {
    /// Bytes currently retained by queued or running import writers.
    used: Arc<Mutex<u64>>,
}

/// Reservation that releases imported-byte admission on every worker exit path.
struct ImportWriteReservation {
    /// Shared budget counter.
    budget: ImportWriteBudget,
    /// Bytes charged by this job.
    bytes: u64,
}

/// Retained private temp that unlinks itself unless a reported tool result
/// takes it.
struct RetainedTemp {
    /// Owned path removed on drop until successfully published to the caller.
    path: Option<PathBuf>,
}

impl ArtifactTransferManager {
    /// Creates the production transfer manager.
    pub(crate) fn new(client: ArtifactClient) -> Self {
        let waker = Arc::new(Mutex::new(None));
        let queue = Arc::new(Mutex::new(VecDeque::new()));
        Self {
            client: Some(client),
            session: None,
            control: ArtifactTransferControl { queue, waker },
            transfers: HashMap::new(),
            requests: HashMap::new(),
            import_write_budget: ImportWriteBudget::default(),
            #[cfg(test)]
            reject_next_request: false,
        }
    }

    /// Creates a manager that reports Artifact RPC unavailable in direct tests.
    #[cfg(any(test, feature = "echo-agent"))]
    pub(crate) fn unavailable() -> Self {
        let waker = Arc::new(Mutex::new(None));
        let queue = Arc::new(Mutex::new(VecDeque::new()));
        Self {
            client: None,
            session: None,
            control: ArtifactTransferControl { queue, waker },
            transfers: HashMap::new(),
            requests: HashMap::new(),
            import_write_budget: ImportWriteBudget::default(),
            #[cfg(test)]
            reject_next_request: false,
        }
    }

    /// Returns the worker-side transfer control.
    pub(crate) fn control(&self) -> ArtifactTransferControl {
        self.control.clone()
    }

    /// Installs the manual-loop wake handle used after worker completions.
    pub(crate) fn install_waker(&self, waker: tau_client::ManualRuntimeWaker) {
        *self.control.waker.lock().expect("artifact waker poisoned") = Some(waker);
    }

    /// Binds artifact requests to the runtime's immutable current session.
    pub(crate) fn bind_session(&mut self, session: tau_proto::SessionId) {
        self.session = Some(session);
    }

    /// Drains bounded worker completions without filesystem I/O on the main
    /// loop.
    pub(crate) fn drain(&mut self, scheduler: &WorkScheduler, output: &Output) {
        loop {
            let command = self
                .control
                .queue
                .lock()
                .expect("artifact command queue poisoned")
                .pop_front();
            let Some(command) = command else {
                break;
            };
            match command {
                Command::StartExport {
                    invoke,
                    lifecycle,
                    upload,
                    filename,
                } => {
                    if lifecycle.effect_cancel_requested() {
                        report_cancelled(output, invoke, lifecycle);
                        continue;
                    }
                    let call_id = invoke.call_id.clone();
                    self.transfers.insert(
                        call_id.clone(),
                        Transfer::Export {
                            invoke,
                            lifecycle,
                            upload,
                            filename,
                        },
                    );
                    self.start_next(&call_id, output);
                }
                Command::StartImport {
                    invoke,
                    lifecycle,
                    download,
                } => {
                    if lifecycle.effect_cancel_requested() {
                        report_cancelled(output, invoke, lifecycle);
                        continue;
                    }
                    let call_id = invoke.call_id.clone();
                    self.transfers.insert(
                        call_id.clone(),
                        Transfer::Import {
                            invoke,
                            lifecycle,
                            download,
                        },
                    );
                    self.start_next(&call_id, output);
                }
                Command::ImportWritten {
                    call_id,
                    size,
                    result,
                } => {
                    let Some(Transfer::ImportWriting {
                        invoke,
                        lifecycle,
                        cancel,
                    }) = self.transfers.remove(&call_id)
                    else {
                        drop(result);
                        continue;
                    };
                    if cancel.load(Ordering::Acquire) || lifecycle.effect_cancel_requested() {
                        drop(result);
                        report_cancelled(output, invoke, lifecycle);
                        continue;
                    }
                    match result {
                        Ok(mut temp) => {
                            let reported = report_result(
                                output,
                                &invoke,
                                CborValue::Map(vec![
                                    text_entry("path", temp.path().display().to_string()),
                                    int_entry("size", size),
                                ]),
                            );
                            if reported {
                                let _ = temp.publish();
                            }
                        }
                        Err(message) => report_error(output, &invoke, message),
                    }
                    lifecycle.finish();
                }
            }
        }
        let _ = scheduler;
    }

    /// Accepts one exact correlated, bounded Artifact response.
    pub(crate) fn handle_result(
        &mut self,
        result: ArtifactResult,
        scheduler: &WorkScheduler,
        output: &Output,
    ) {
        let frame = HarnessOutputMessage::ArtifactResult(Box::new(result.clone()));
        let frame_fits = tau_proto::artifact_frame_fits(&frame);
        let Some(call_id) = self.requests.remove(&result.request_id) else {
            return;
        };
        let Some(mut transfer) = self.transfers.remove(&call_id) else {
            return;
        };
        if !frame_fits {
            self.best_effort_release(&transfer);
            finish_error(
                output,
                transfer,
                "artifact response exceeded the complete frame limit".to_owned(),
            );
            return;
        }
        let value = match result.result {
            Ok(value) => value,
            Err(error) => {
                self.best_effort_release(&transfer);
                finish_error(output, transfer, artifact_error_message(error));
                return;
            }
        };
        let accepted = match &mut transfer {
            Transfer::Export { upload, .. } => upload.accept(value),
            Transfer::Import { download, .. } => download.accept(value),
            Transfer::ImportWriting { .. } => Err(ArtifactError::Invalid),
        };
        if let Err(error) = accepted {
            self.best_effort_release(&transfer);
            finish_error(output, transfer, artifact_error_message(error));
            return;
        }
        match transfer {
            Transfer::Export {
                invoke,
                lifecycle,
                upload,
                filename,
            } if upload.descriptor().is_some() => {
                let descriptor = upload.descriptor().expect("checked descriptor");
                let mut entries = vec![
                    text_entry("artifact", artifact_reference(&descriptor.key)),
                    int_entry("size", descriptor.size.get()),
                ];
                if let Some(filename) = filename {
                    entries.push(text_entry("filename", filename));
                }
                report_result(output, &invoke, CborValue::Map(entries));
                lifecycle.finish();
            }
            Transfer::Import {
                invoke,
                lifecycle,
                download,
            } if download.next_op().is_none() => {
                let size = download
                    .into_bytes()
                    .map(|bytes| (bytes.len() as u64, bytes));
                match size {
                    Ok((size, bytes)) => {
                        let reservation = match self.import_write_budget.reserve(size) {
                            Ok(reservation) => reservation,
                            Err(message) => {
                                report_error(output, &invoke, message);
                                lifecycle.finish();
                                return;
                            }
                        };
                        let tx = self.control.clone();
                        let call_id = invoke.call_id.clone();
                        let cancel = Arc::new(AtomicBool::new(false));
                        self.transfers.insert(
                            call_id.clone(),
                            Transfer::ImportWriting {
                                invoke: invoke.clone(),
                                lifecycle: lifecycle.clone(),
                                cancel: Arc::clone(&cancel),
                            },
                        );
                        let meta = WorkMeta {
                            call_id: Some(call_id.clone()),
                            agent_id: Some(invoke.agent_id.clone()),
                            queued_bytes: 0,
                        };
                        let enqueue = scheduler.enqueue(WorkPriority::Cheap, meta, move || {
                            let result = write_private_temp(&bytes, &cancel);
                            drop(reservation);
                            let _ = tx.send(Command::ImportWritten {
                                call_id,
                                size,
                                result,
                            });
                        });
                        if let Err(error) = enqueue {
                            self.transfers.remove(&invoke.call_id);
                            report_error(output, &invoke, error.message);
                            lifecycle.finish();
                        }
                    }
                    Err(error) => {
                        report_error(output, &invoke, artifact_error_message(error));
                        lifecycle.finish();
                    }
                }
            }
            transfer => {
                self.transfers.insert(call_id.clone(), transfer);
                self.start_next(&call_id, output);
            }
        }
    }

    /// Cancels one active transfer and submits Close/Abort best-effort.
    pub(crate) fn cancel(&mut self, call_id: &tau_proto::ToolCallId, output: &Output) {
        self.requests.retain(|_, owner| owner != call_id);
        let Some(transfer) = self.transfers.remove(call_id) else {
            return;
        };
        if let Transfer::ImportWriting { cancel, .. } = &transfer {
            cancel.store(true, Ordering::Release);
        }
        self.best_effort_release(&transfer);
        let (invoke, lifecycle) = transfer.parts();
        report_cancelled(output, invoke, lifecycle);
    }

    /// Drops all correlation and submits transfer release best-effort.
    pub(crate) fn shutdown(&mut self) {
        let transfers = self
            .transfers
            .drain()
            .map(|(_, transfer)| transfer)
            .collect::<Vec<_>>();
        self.requests.clear();
        for transfer in &transfers {
            if let Transfer::ImportWriting { cancel, .. } = transfer {
                cancel.store(true, Ordering::Release);
            }
            self.best_effort_release(transfer);
        }
        for transfer in transfers {
            transfer.parts().1.finish();
        }
    }

    fn start_next(&mut self, call_id: &tau_proto::ToolCallId, output: &Output) {
        let Some(transfer) = self.transfers.get(call_id) else {
            return;
        };
        let op = match transfer {
            Transfer::Export { upload, .. } => upload.next_op(),
            Transfer::Import { download, .. } => download.next_op(),
            Transfer::ImportWriting { .. } => None,
        };
        let Some(op) = op else {
            return;
        };
        #[cfg(test)]
        let result = if std::mem::take(&mut self.reject_next_request) {
            Err(tau_client::ClientError::Overloaded)
        } else {
            self.start_request(op)
        };
        #[cfg(not(test))]
        let result = self.start_request(op);
        match result {
            Ok(request_id) => {
                self.requests.insert(request_id, call_id.clone());
            }
            Err(error) => {
                if let Some(transfer) = self.transfers.remove(call_id) {
                    self.best_effort_release(&transfer);
                    finish_error(
                        output,
                        transfer,
                        format!("artifact request failed: {error}"),
                    );
                }
            }
        }
    }

    fn start_request(
        &self,
        op: tau_proto::ArtifactOp,
    ) -> Result<ArtifactRequestId, tau_client::ClientError> {
        let client = self
            .client
            .as_ref()
            .ok_or(tau_client::ClientError::InvalidArtifactRequest)?;
        let session = self
            .session
            .clone()
            .ok_or(tau_client::ClientError::InvalidArtifactRequest)?;
        client.start_request(session, op)
    }

    fn best_effort_release(&self, transfer: &Transfer) {
        let Some(client) = &self.client else {
            return;
        };
        let Some(session) = self.session.clone() else {
            return;
        };
        let op = match transfer {
            Transfer::Export { upload, .. } => upload.abort_op(),
            Transfer::Import { download, .. } => download.close_op(),
            Transfer::ImportWriting { .. } => None,
        };
        if let Some(op) = op {
            let _ = client.start_request(session, op);
        }
    }
}

impl ArtifactTransferControl {
    /// Reads or validates local inputs off-loop and hands typed state to the
    /// main loop.
    ///
    /// Returns true when the main loop now owns the lifecycle.
    pub(crate) fn prepare(
        &self,
        invoke: ToolStarted,
        lifecycle: ToolLifecycle,
        workdir: &Path,
        output: &Output,
    ) -> bool {
        let failure_invoke = invoke.clone();
        let command = if invoke.tool_name == EXPORT_TOOL_NAME {
            prepare_export(invoke, lifecycle, workdir)
        } else if invoke.tool_name == IMPORT_TOOL_NAME {
            prepare_import(invoke, lifecycle)
        } else {
            return false;
        };
        match command {
            Ok(command) => {
                if self.send(command).is_ok() {
                    true
                } else {
                    report_error(
                        output,
                        &failure_invoke,
                        "artifact completion capacity is busy; retry this tool call".to_owned(),
                    );
                    false
                }
            }
            Err((invoke, lifecycle, message)) => {
                report_error(output, &invoke, message);
                let _ = lifecycle;
                false
            }
        }
    }

    fn send(&self, command: Command) -> Result<(), Box<Command>> {
        {
            let mut queue = self.queue.lock().expect("artifact command queue poisoned");
            if queue.len() >= ARTIFACT_COMMAND_LIMIT {
                return Err(Box::new(command));
            }
            queue.push_back(command);
        }
        if let Some(waker) = self.waker.lock().expect("artifact waker poisoned").as_ref() {
            waker.wake();
        }
        Ok(())
    }
}

impl Transfer {
    fn parts(self) -> (ToolStarted, ToolLifecycle) {
        match self {
            Self::Export {
                invoke, lifecycle, ..
            }
            | Self::Import {
                invoke, lifecycle, ..
            }
            | Self::ImportWriting {
                invoke, lifecycle, ..
            } => (invoke, lifecycle),
        }
    }
}

#[expect(
    clippy::result_large_err,
    reason = "the error retains the exact invocation and lifecycle needed for one terminal"
)]
fn prepare_export(
    invoke: ToolStarted,
    lifecycle: ToolLifecycle,
    workdir: &Path,
) -> Result<Command, (ToolStarted, ToolLifecycle, String)> {
    let Some(path) = tau_proto::cbor_text_field(&invoke.arguments, "path") else {
        return Err((invoke, lifecycle, "path must be a string".to_owned()));
    };
    let path = absolute_path(workdir, &path);
    let mut file = open_export_file(&path).map_err(|error| {
        (
            invoke.clone(),
            lifecycle.clone(),
            format!("failed to open {}: {error}", path.display()),
        )
    })?;
    let metadata = file.metadata().map_err(|error| {
        (
            invoke.clone(),
            lifecycle.clone(),
            format!("failed to inspect {}: {error}", path.display()),
        )
    })?;
    if !metadata.is_file() {
        return Err((
            invoke,
            lifecycle,
            "export path is not a regular file".to_owned(),
        ));
    }
    if metadata.len() > ARTIFACT_MAX_BYTES {
        return Err((
            invoke,
            lifecycle,
            "artifact exceeds the 16 MiB limit".to_owned(),
        ));
    }
    let mut bytes = Vec::with_capacity(metadata.len() as usize);
    Read::by_ref(&mut file)
        .take(ARTIFACT_MAX_BYTES + 1)
        .read_to_end(&mut bytes)
        .map_err(|error| {
            (
                invoke.clone(),
                lifecycle.clone(),
                format!("failed to read {}: {error}", path.display()),
            )
        })?;
    let upload = ArtifactUpload::new(bytes).map_err(|error| {
        (
            invoke.clone(),
            lifecycle.clone(),
            artifact_error_message(error),
        )
    })?;
    let filename = path
        .file_name()
        .and_then(|name| name.to_str())
        .map(bounded_hint);
    Ok(Command::StartExport {
        invoke,
        lifecycle,
        upload,
        filename,
    })
}

/// Opens an export source without waiting for a FIFO writer on Unix.
#[cfg(unix)]
fn open_export_file(path: &Path) -> std::io::Result<File> {
    OpenOptions::new()
        .read(true)
        .custom_flags(libc::O_NONBLOCK)
        .open(path)
}

/// Opens an export source with the platform's ordinary read-only behavior.
#[cfg(not(unix))]
fn open_export_file(path: &Path) -> std::io::Result<File> {
    File::open(path)
}

#[expect(
    clippy::result_large_err,
    reason = "the error retains the exact invocation and lifecycle needed for one terminal"
)]
fn prepare_import(
    invoke: ToolStarted,
    lifecycle: ToolLifecycle,
) -> Result<Command, (ToolStarted, ToolLifecycle, String)> {
    let Some(key) = tau_proto::cbor_text_field(&invoke.arguments, "key") else {
        return Err((invoke, lifecycle, "key must be a string".to_owned()));
    };
    let key = parse_artifact_reference(&key).map_err(|_| {
        (
            invoke.clone(),
            lifecycle.clone(),
            "invalid artifact reference".to_owned(),
        )
    })?;
    Ok(Command::StartImport {
        invoke,
        lifecycle,
        download: ArtifactDownload::new(key),
    })
}

fn write_private_temp(bytes: &[u8], cancel: &AtomicBool) -> Result<RetainedTemp, String> {
    let mut temp = tempfile::Builder::new()
        .prefix("tau-artifact-")
        .tempfile()
        .map_err(|error| format!("failed to create private import file: {error}"))?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        temp.as_file()
            .set_permissions(fs::Permissions::from_mode(0o600))
            .map_err(|error| format!("failed to secure import file: {error}"))?;
    }
    temp.write_all(bytes)
        .and_then(|()| temp.as_file().sync_all())
        .map_err(|error| format!("failed to write imported original: {error}"))?;
    let (_, path) = temp
        .keep()
        .map_err(|error| format!("failed to retain imported original: {}", error.error))?;
    if cancel.load(Ordering::Acquire) {
        let _ = fs::remove_file(&path);
        return Err("artifact import was cancelled".to_owned());
    }
    Ok(RetainedTemp { path: Some(path) })
}

fn finish_error(output: &Output, transfer: Transfer, message: String) {
    let (invoke, lifecycle) = transfer.parts();
    report_error(output, &invoke, message);
    lifecycle.finish();
}

fn report_result(output: &Output, invoke: &ToolStarted, result: CborValue) -> bool {
    output
        .report_tool_terminal(Event::ToolResult(ToolResult {
            presentation: Default::default(),
            call_id: invoke.call_id.clone(),
            tool_name: invoke.tool_name.clone(),
            tool_type: tau_proto::ToolType::Function,
            result,
            provider_content: Vec::new(),
            kind: ToolResultKind::Final,
            display: None,
            originator: invoke.originator.clone(),
        }))
        .is_ok()
}

fn report_error(output: &Output, invoke: &ToolStarted, message: String) {
    let _ = output.report_tool_terminal(Event::ToolError(ToolError {
        presentation: Default::default(),
        call_id: invoke.call_id.clone(),
        tool_name: invoke.tool_name.clone(),
        tool_type: tau_proto::ToolType::Function,
        message,
        details: None,
        display: None,
        originator: invoke.originator.clone(),
    }));
}

fn report_cancelled(output: &Output, invoke: ToolStarted, lifecycle: ToolLifecycle) {
    let _ = output.report_tool_terminal(Event::ToolCancelled(ToolCancelled {
        presentation: Default::default(),
        call_id: invoke.call_id,
        tool_name: invoke.tool_name,
        tool_type: tau_proto::ToolType::Function,
        display: None,
    }));
    lifecycle.finish();
}

fn artifact_error_message(error: ArtifactError) -> String {
    match error {
        ArtifactError::Permission => {
            "artifact storage is unavailable; a persistent harness state root is required"
                .to_owned()
        }
        ArtifactError::SessionMismatch => "artifact request session no longer matches".to_owned(),
        ArtifactError::Invalid => "artifact request or response was invalid".to_owned(),
        ArtifactError::Busy => "artifact storage is busy; retry this tool call".to_owned(),
        ArtifactError::Unavailable => "artifact or transfer is unavailable".to_owned(),
        ArtifactError::Integrity => "artifact size or digest verification failed".to_owned(),
        ArtifactError::Io => "artifact storage I/O failed".to_owned(),
    }
}

fn absolute_path(workdir: &Path, path: &str) -> PathBuf {
    let path = PathBuf::from(path);
    if path.is_absolute() {
        path
    } else {
        workdir.join(path)
    }
}

fn bounded_hint(value: &str) -> String {
    let mut end = value.len().min(255);
    while !value.is_char_boundary(end) {
        end -= 1;
    }
    value[..end].to_owned()
}

fn text_entry(name: &str, value: String) -> (CborValue, CborValue) {
    (CborValue::Text(name.to_owned()), CborValue::Text(value))
}

fn int_entry(name: &str, value: u64) -> (CborValue, CborValue) {
    (
        CborValue::Text(name.to_owned()),
        CborValue::Integer(i64::try_from(value).unwrap_or(i64::MAX).into()),
    )
}

impl ImportWriteBudget {
    /// Reserves bounded bytes before moving an original into a scheduler job.
    fn reserve(&self, bytes: u64) -> Result<ImportWriteReservation, String> {
        let mut used = self.used.lock().expect("import write budget poisoned");
        let next = used.saturating_add(bytes);
        if IMPORT_WRITE_BYTES_LIMIT < next {
            return Err("artifact import write capacity is busy; retry this tool call".to_owned());
        }
        *used = next;
        Ok(ImportWriteReservation {
            budget: self.clone(),
            bytes,
        })
    }
}

impl Drop for ImportWriteReservation {
    fn drop(&mut self) {
        let mut used = self
            .budget
            .used
            .lock()
            .expect("import write budget poisoned");
        *used = used.saturating_sub(self.bytes);
    }
}

impl RetainedTemp {
    /// Borrows the private path while the guard still owns cleanup.
    fn path(&self) -> &Path {
        self.path.as_deref().expect("retained temp path")
    }

    /// Transfers cleanup ownership to the successful tool result.
    fn publish(&mut self) -> PathBuf {
        self.path.take().expect("retained temp path")
    }
}

impl Drop for RetainedTemp {
    fn drop(&mut self) {
        if let Some(path) = self.path.take() {
            let _ = fs::remove_file(path);
        }
    }
}