sac-cli 0.1.0

Terminal-based AI coding agent — fork of NAC with extended backend support and context management
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
use std::collections::BTreeMap;
use std::process::Stdio;
use std::sync::Arc;
use std::time::Duration;

use serde_json::Value;
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command;
use tokio::sync::Mutex;
use tokio::time::timeout;
use tracing::Instrument;

use crate::events::{decode_stderr_event, AgentEvent};
use crate::model::ModelClient;
use crate::process::{isolate_process_group, terminate_child_tree};
use crate::store;
use crate::tools::{require_str, require_string_array, ToolResult, ToolRuntime};
use crate::types::ToolDefinition;

pub const DEFAULT_THREAD_TIMEOUT_SECS: u64 = 60 * 60;
pub const MIN_THREAD_TIMEOUT_SECS: u64 = 30 * 60;

pub fn dispatch_definition() -> ToolDefinition {
    use serde_json::json;
    def(
        "thread",
        "Dispatch a named worker thread. The worker reuses its own retained history and can pull the latest retained episode from other named threads. Default timeout is configured by sac; built-in default is 3600 seconds and minimum timeout is 1800 seconds.",
        json!({
            "type": "object",
            "properties": {
                "name": { "type": "string", "description": "Thread name. Creates if new, reuses if existing." },
                "action": { "type": "string", "description": "Task for the worker." },
                "threads": {
                    "type": "array",
                    "items": { "type": "string" },
                    "description": "Other thread names whose latest retained episodes should be loaded."
                },
                "timeout": { "type": "integer", "description": "Timeout in seconds for this dispatch (default 3600, minimum 1800)." }
            },
            "required": ["name", "action"]
        }),
    )
}

pub fn threads_definition() -> ToolDefinition {
    use serde_json::json;
    def(
        "threads",
        "List retained thread lanes in the current orchestrator session. This reports persisted thread history, not whether a worker process is actively running right now.",
        json!({
            "type": "object",
            "properties": {}
        }),
    )
}

pub fn thread_read_definition() -> ToolDefinition {
    use serde_json::json;
    def(
        "thread_read",
        "Read the full retained episode history for one thread.",
        json!({
            "type": "object",
            "properties": {
                "name": { "type": "string", "description": "Thread name." }
            },
            "required": ["name"]
        }),
    )
}

pub fn thread_delete_definition() -> ToolDefinition {
    use serde_json::json;
    def(
        "thread_delete",
        "Delete one thread and all its retained episodes.",
        json!({
            "type": "object",
            "properties": {
                "name": { "type": "string", "description": "Thread name." }
            },
            "required": ["name"]
        }),
    )
}

pub async fn execute_dispatch(
    args: Value,
    runtime: &ToolRuntime,
    client: &ModelClient,
) -> ToolResult {
    let thread_name = match require_str(&args, "name") {
        Ok(s) => s,
        Err(e) => return e,
    };
    let action = match require_str(&args, "action") {
        Ok(s) => s,
        Err(e) => return e,
    };
    let source_threads = match require_string_array(&args, "threads") {
        Ok(v) => v,
        Err(e) => return e,
    };
    let session_id = match require_session(runtime) {
        Ok(s) => s.to_string(),
        Err(e) => return e,
    };
    let timeout_secs = resolve_thread_timeout_secs(&args, runtime.thread_timeout_secs);

    async {
        if !mark_thread_active(runtime, &thread_name).await {
            tracing::warn!(thread_name = %thread_name, "thread dispatch rejected because thread is already active");
            return ToolResult {
                content: format!(
                    "Thread '{}' is already running; retry after the current dispatch completes.",
                    thread_name
                ),
                is_error: true,
            };
        }

        tracing::info!(
            session_id = %session_id,
            thread_name = %thread_name,
            action_len = action.len(),
            source_threads = ?source_threads,
            timeout_secs,
            backend = ?client.backend(),
            model = %client.model,
            base_url = %client.base_url(),
            "dispatching managed worker thread"
        );

        runtime.event_sink.emit(AgentEvent::ThreadStarted {
            name: thread_name.clone(),
            action: action.clone(),
            source_threads: source_threads.clone(),
        });

        let result = run_worker(
            runtime,
            client,
            &session_id,
            &thread_name,
            &action,
            &source_threads,
            timeout_secs,
        )
        .await;
        unmark_thread_active(runtime, &thread_name).await;

        match result {
            Err(e) => {
                tracing::error!(thread_name = %thread_name, error = %e, "failed to spawn managed worker thread");
                runtime.event_sink.emit(AgentEvent::Error {
                    thread_name: Some(thread_name.clone()),
                    message: format!("Failed to spawn thread '{}': {}", thread_name, e),
                });
                ToolResult {
                    content: format!("Failed to spawn thread '{}': {}", thread_name, e),
                    is_error: true,
                }
            }
            Ok(run) if run.timed_out => {
                tracing::warn!(
                    thread_name = %thread_name,
                    exit_code = run.exit_code,
                    stderr_len = run.stderr.len(),
                    stdout_len = run.stdout.len(),
                    timeout_reason = ?run.timeout_reason,
                    timeout_secs,
                    "managed worker thread timed out"
                );
                let timeout_reason = run.timeout_reason.clone();
                runtime.event_sink.emit(AgentEvent::ThreadFinished {
                    name: thread_name.clone(),
                    exit_code: run.exit_code,
                    timed_out: true,
                    timeout_reason: timeout_reason.clone(),
                });
                ToolResult {
                    content: match timeout_reason {
                        Some(reason) => {
                            format!(
                                "Thread '{}' timed out after {}s.\n{}",
                                thread_name, timeout_secs, reason
                            )
                        }
                        None => format!("Thread '{}' timed out after {}s", thread_name, timeout_secs),
                    },
                    is_error: true,
                }
            }
            Ok(run) if run.exit_code != 0 => {
                tracing::error!(
                    thread_name = %thread_name,
                    exit_code = run.exit_code,
                    stderr_len = run.stderr.len(),
                    stdout_len = run.stdout.len(),
                    "managed worker thread exited with failure"
                );
                runtime.event_sink.emit(AgentEvent::ThreadFinished {
                    name: thread_name.clone(),
                    exit_code: run.exit_code,
                    timed_out: false,
                    timeout_reason: None,
                });
                let details = if !run.stderr.trim().is_empty() {
                    run.stderr.trim().to_string()
                } else if !run.stdout.trim().is_empty() {
                    run.stdout.trim().to_string()
                } else {
                    "no output".to_string()
                };
                ToolResult {
                    content: format!(
                        "Thread '{}' failed (exit {}):\n{}",
                        thread_name, run.exit_code, details
                    ),
                    is_error: true,
                }
            }
            Ok(run) => {
                tracing::info!(
                    thread_name = %thread_name,
                    exit_code = run.exit_code,
                    stderr_len = run.stderr.len(),
                    stdout_len = run.stdout.len(),
                    "managed worker thread completed successfully"
                );
                runtime.event_sink.emit(AgentEvent::ThreadFinished {
                    name: thread_name.clone(),
                    exit_code: run.exit_code,
                    timed_out: false,
                    timeout_reason: None,
                });
                ToolResult {
                    content: run.stdout.trim().to_string(),
                    is_error: false,
                }
            }
        }
    }
    .instrument(tracing::info_span!(
        "thread_dispatch",
        session_id = %session_id,
        thread_name = %thread_name,
        source_thread_count = source_threads.len(),
        timeout_secs,
        store_path = %runtime.store_path.display(),
        sandboxed = runtime.sandbox.is_some(),
    ))
    .await
}

pub async fn execute_threads(runtime: &ToolRuntime) -> ToolResult {
    let session_id = match require_session(runtime) {
        Ok(s) => s.to_string(),
        Err(e) => return e,
    };

    let store_path = runtime.store_path.clone();
    let sid = session_id.clone();
    let threads =
        match tokio::task::spawn_blocking(move || store::list_threads(&store_path, &sid)).await {
            Ok(Ok(threads)) => threads,
            Ok(Err(error)) => {
                return ToolResult {
                    content: format!("Error listing threads: {}", error),
                    is_error: true,
                }
            }
            Err(join_error) => {
                return ToolResult {
                    content: format!("Internal error listing threads: {}", join_error),
                    is_error: true,
                }
            }
        };

    if threads.is_empty() {
        return ToolResult {
            content: "No retained threads in this session.".to_string(),
            is_error: false,
        };
    }

    let active_threads = runtime.active_threads.lock().await.clone();

    let mut output = String::from("Retained threads:");
    for thread in threads {
        output.push_str(&format!(
            "\n- {} | {} episodes | status: {} | created {} | updated {}",
            thread.name,
            thread.episode_count,
            if active_threads.contains(&thread.name) {
                "running"
            } else {
                "retained"
            },
            thread.created_at,
            thread.updated_at
        ));
        if let Some(action) = thread.latest_action.as_deref() {
            output.push_str(&format!(" | last action: {}", action));
        }
    }

    ToolResult {
        content: output,
        is_error: false,
    }
}

pub async fn execute_thread_read(args: Value, runtime: &ToolRuntime) -> ToolResult {
    let thread_name = match require_str(&args, "name") {
        Ok(s) => s,
        Err(e) => return e,
    };
    let session_id = match require_session(runtime) {
        Ok(s) => s.to_string(),
        Err(e) => return e,
    };

    let store_path = runtime.store_path.clone();
    let sid = session_id.clone();
    let tname = thread_name.clone();
    match tokio::task::spawn_blocking(move || store::thread_read(&store_path, &sid, &tname)).await {
        Ok(Ok(episodes)) => ToolResult {
            content: store::render_thread_document(&thread_name, &episodes),
            is_error: false,
        },
        Ok(Err(error)) => ToolResult {
            content: format!("Error reading thread '{}': {}", thread_name, error),
            is_error: true,
        },
        Err(join_error) => ToolResult {
            content: format!(
                "Internal error reading thread '{}': {}",
                thread_name, join_error
            ),
            is_error: true,
        },
    }
}

pub async fn execute_thread_delete(args: Value, runtime: &ToolRuntime) -> ToolResult {
    let thread_name = match require_str(&args, "name") {
        Ok(s) => s,
        Err(e) => return e,
    };
    let session_id = match require_session(runtime) {
        Ok(s) => s.to_string(),
        Err(e) => return e,
    };

    if is_thread_active(runtime, &thread_name).await {
        return ToolResult {
            content: format!(
                "Thread '{}' is currently running; wait for it to finish before deleting it.",
                thread_name
            ),
            is_error: true,
        };
    }

    let store_path = runtime.store_path.clone();
    let sid = session_id.clone();
    let tname = thread_name.clone();
    match tokio::task::spawn_blocking(move || store::delete_thread(&store_path, &sid, &tname)).await
    {
        Ok(Ok(true)) => ToolResult {
            content: format!(
                "Deleted thread '{}' and its retained episodes.",
                thread_name
            ),
            is_error: false,
        },
        Ok(Ok(false)) => ToolResult {
            content: format!("Thread '{}' does not exist in this session.", thread_name),
            is_error: true,
        },
        Ok(Err(error)) => ToolResult {
            content: format!("Error deleting thread '{}': {}", thread_name, error),
            is_error: true,
        },
        Err(join_error) => ToolResult {
            content: format!(
                "Internal error deleting thread '{}': {}",
                thread_name, join_error
            ),
            is_error: true,
        },
    }
}

fn def(name: &str, description: &str, parameters: serde_json::Value) -> ToolDefinition {
    ToolDefinition {
        def_type: "function".to_string(),
        function: crate::types::FunctionDef {
            name: name.to_string(),
            description: description.to_string(),
            parameters,
        },
    }
}

fn require_session(runtime: &ToolRuntime) -> Result<&str, ToolResult> {
    runtime.session_id.as_deref().ok_or_else(|| ToolResult {
        content: "Error: thread tools require an active session".to_string(),
        is_error: true,
    })
}

fn resolve_thread_timeout_secs(args: &Value, default_timeout_secs: u64) -> u64 {
    args.get("timeout")
        .and_then(|v| v.as_u64())
        .unwrap_or(default_timeout_secs)
        .max(MIN_THREAD_TIMEOUT_SECS)
}

async fn mark_thread_active(runtime: &ToolRuntime, thread_name: &str) -> bool {
    let mut active = runtime.active_threads.lock().await;
    if active.contains(thread_name) {
        false
    } else {
        active.insert(thread_name.to_string());
        true
    }
}

async fn unmark_thread_active(runtime: &ToolRuntime, thread_name: &str) {
    runtime.active_threads.lock().await.remove(thread_name);
}

async fn is_thread_active(runtime: &ToolRuntime, thread_name: &str) -> bool {
    runtime.active_threads.lock().await.contains(thread_name)
}

struct WorkerRun {
    stdout: String,
    stderr: String,
    exit_code: i32,
    timed_out: bool,
    timeout_reason: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct ActiveToolCallTrace {
    name: String,
    args_detail: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
enum TimeoutLocation {
    Startup,
    ModelApi { iteration: usize },
    ToolCall,
    BetweenToolAndModel,
    Finalizing,
}

impl Default for TimeoutLocation {
    fn default() -> Self {
        Self::Startup
    }
}

#[derive(Default)]
struct WorkerTimeoutTrace {
    location: TimeoutLocation,
    active_tool_calls: BTreeMap<String, ActiveToolCallTrace>,
}

impl WorkerTimeoutTrace {
    fn observe(&mut self, event: &AgentEvent) {
        match event {
            AgentEvent::RunStarted { .. } => {
                self.location = TimeoutLocation::Startup;
                self.active_tool_calls.clear();
            }
            AgentEvent::ModelCallStarted { iteration, .. } => {
                self.location = TimeoutLocation::ModelApi {
                    iteration: *iteration,
                };
                self.active_tool_calls.clear();
            }
            AgentEvent::ToolCallStarted {
                call_id,
                name,
                args_detail,
                ..
            } => {
                self.location = TimeoutLocation::ToolCall;
                self.active_tool_calls.insert(
                    call_id.clone(),
                    ActiveToolCallTrace {
                        name: name.clone(),
                        args_detail: args_detail.clone(),
                    },
                );
            }
            AgentEvent::ToolCallFinished { call_id, .. } => {
                self.active_tool_calls.remove(call_id);
                if self.active_tool_calls.is_empty() {
                    self.location = TimeoutLocation::BetweenToolAndModel;
                } else {
                    self.location = TimeoutLocation::ToolCall;
                }
            }
            AgentEvent::AssistantMessage { .. } | AgentEvent::RunFinished { .. } => {
                self.location = TimeoutLocation::Finalizing;
                self.active_tool_calls.clear();
            }
            AgentEvent::Error { .. }
            | AgentEvent::ThreadLog { .. }
            | AgentEvent::TerminalSnapshot { .. } => {}
            AgentEvent::ThreadStarted { .. }
            | AgentEvent::ThreadSpawned { .. }
            | AgentEvent::ThreadFinished { .. } => {}
            AgentEvent::StreamTextDelta { .. } | AgentEvent::StreamComplete { .. } => {}
            AgentEvent::ModelIterationUsage { .. } => {}
            AgentEvent::GoalContinuation { .. }
            | AgentEvent::GoalTurnAccounted { .. }
            | AgentEvent::GoalErrorTransition { .. } => {}
            AgentEvent::LeanResumeTriggered { .. } => {}
        }
    }

    fn timeout_reason(&self) -> String {
        match &self.location {
            TimeoutLocation::ModelApi { iteration } => format!(
                "The thread timed out at a call to the model API.\nModel call: iteration {}",
                iteration
            ),
            TimeoutLocation::ToolCall if !self.active_tool_calls.is_empty() => {
                if self.active_tool_calls.len() == 1 {
                    let (call_id, call) = self.active_tool_calls.iter().next().unwrap();
                    return format!(
                        "The thread timed out at a tool call.\nTool call: {} {}\narguments: {}",
                        call.name,
                        call_id,
                        call.args_detail.as_deref().unwrap_or("<not captured>")
                    );
                }

                let mut reason = String::from("The thread timed out at tool calls:");
                for (call_id, call) in &self.active_tool_calls {
                    reason.push_str(&format!("\n- {} {}", call.name, call_id));
                    match call.args_detail.as_deref() {
                        Some(args_detail) => {
                            reason.push_str(&format!("\n  arguments: {}", args_detail));
                        }
                        None => reason.push_str("\n  arguments: <not captured>"),
                    }
                }
                reason
            }
            TimeoutLocation::BetweenToolAndModel => {
                "The thread timed out after tool call completion while preparing the next model API call."
                    .to_string()
            }
            TimeoutLocation::Finalizing => {
                "The thread timed out after producing a final response while the worker was exiting."
                    .to_string()
            }
            TimeoutLocation::Startup | TimeoutLocation::ToolCall => {
                "The thread timed out before entering a model API call or tool call.".to_string()
            }
        }
    }
}

async fn run_worker(
    runtime: &ToolRuntime,
    client: &ModelClient,
    session_id: &str,
    thread_name: &str,
    action: &str,
    source_threads: &[String],
    timeout_secs: u64,
) -> std::io::Result<WorkerRun> {
    let executable = runtime.worker_executable.clone().ok_or_else(|| {
        std::io::Error::new(
            std::io::ErrorKind::NotFound,
            "worker executable path is not configured",
        )
    })?;
    let executable_display = executable.display().to_string();
    let cwd = std::env::current_dir()?;
    tracing::debug!(
        thread_name = %thread_name,
        session_id = %session_id,
        executable = %executable_display,
        cwd = %cwd.display(),
        sandboxed = runtime.sandbox.is_some(),
        source_threads = ?source_threads,
        timeout_secs,
        "resolved managed worker spawn context"
    );
    let mut command = Command::new(executable);
    command
        .arg("__worker")
        .arg("--session-id")
        .arg(session_id)
        .arg("--thread-name")
        .arg(thread_name)
        .arg("--action")
        .arg(action)
        .arg("--api-model")
        .arg(client.model.as_str())
        .arg("--api-base-url")
        .arg(client.base_url())
        .arg("--backend")
        .arg(client.backend().as_str())
        .arg("--store-path")
        .arg(runtime.store_path.as_os_str())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());

    if let Some(reasoning_effort) = client.reasoning_effort() {
        command.arg("--effort").arg(reasoning_effort.as_str());
    }

    for source_thread in source_threads {
        command.arg("--source-thread").arg(source_thread);
    }
    if let Some(sandbox) = &runtime.sandbox {
        command.args(sandbox.worker_cli_args());
    }
    isolate_process_group(&mut command);

    let mut child = command.spawn().map_err(|error| {
        tracing::error!(
            thread_name = %thread_name,
            session_id = %session_id,
            executable = %executable_display,
            cwd = %cwd.display(),
            sandboxed = runtime.sandbox.is_some(),
            error = %error,
            "managed worker spawn failed"
        );
        error
    })?;

    runtime.event_sink.emit(AgentEvent::ThreadSpawned {
        name: thread_name.to_string(),
        executable: executable_display.clone(),
        cwd: cwd.display().to_string(),
        sandboxed: runtime.sandbox.is_some(),
    });
    tracing::info!(
        thread_name = %thread_name,
        session_id = %session_id,
        pid = ?child.id(),
        "managed worker process spawned"
    );

    let timeout_trace = Arc::new(Mutex::new(WorkerTimeoutTrace::default()));
    let stderr = child.stderr.take().unwrap();
    let event_sink = runtime.event_sink.clone();
    let thread_name_for_logs = thread_name.to_string();
    let timeout_trace_for_logs = timeout_trace.clone();
    let terminal_manager = runtime.terminal_manager.clone();
    let thread_name_for_terminal_events = thread_name.to_string();
    let stderr_handle = tokio::spawn(async move {
        let reader = BufReader::new(stderr);
        let mut lines = reader.lines();
        let mut output = String::new();
        while let Ok(Some(line)) = lines.next_line().await {
            if let Some(event) = decode_stderr_event(&line) {
                timeout_trace_for_logs.lock().await.observe(&event);
                event_sink.emit(event);
            } else {
                let terminals = terminal_manager.list().await;
                event_sink.emit(AgentEvent::TerminalSnapshot {
                    thread_name: Some(thread_name_for_terminal_events.clone()),
                    terminals,
                });
                event_sink.emit(AgentEvent::ThreadLog {
                    name: thread_name_for_logs.clone(),
                    line: line.clone(),
                });
                if !output.is_empty() {
                    output.push('\n');
                }
                output.push_str(&line);
            }
        }
        output
    });

    let stdout = child.stdout.take().unwrap();
    let stdout_handle = tokio::spawn(async move {
        let reader = BufReader::new(stdout);
        let mut lines = reader.lines();
        let mut output = String::new();
        while let Ok(Some(line)) = lines.next_line().await {
            if !output.is_empty() {
                output.push('\n');
            }
            output.push_str(&line);
        }
        output
    });

    let status = timeout(Duration::from_secs(timeout_secs), child.wait()).await;
    let timed_out = status.is_err();
    if timed_out {
        terminate_child_tree(&mut child).await;
    }

    let stderr = stderr_handle.await.unwrap_or_default();
    let stdout = stdout_handle.await.unwrap_or_default();
    let timeout_reason = if timed_out {
        Some(timeout_trace.lock().await.timeout_reason())
    } else {
        None
    };
    let exit_code = match status {
        Ok(wait_result) => wait_result?.code().unwrap_or(-1),
        Err(_) => -1,
    };

    Ok(WorkerRun {
        stdout,
        stderr,
        exit_code,
        timed_out,
        timeout_reason,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn thread_timeout_defaults_to_one_hour() {
        assert_eq!(
            resolve_thread_timeout_secs(&json!({}), DEFAULT_THREAD_TIMEOUT_SECS),
            60 * 60
        );
    }

    #[test]
    fn thread_timeout_is_clamped_to_thirty_minutes() {
        assert_eq!(resolve_thread_timeout_secs(&json!({}), 10), 30 * 60);
        assert_eq!(
            resolve_thread_timeout_secs(&json!({ "timeout": 20 }), DEFAULT_THREAD_TIMEOUT_SECS),
            30 * 60
        );
        assert_eq!(
            resolve_thread_timeout_secs(&json!({ "timeout": 7200 }), DEFAULT_THREAD_TIMEOUT_SECS),
            7200
        );
    }

    #[test]
    fn timeout_trace_reports_model_api_location() {
        let mut trace = WorkerTimeoutTrace::default();
        trace.observe(&AgentEvent::ModelCallStarted {
            thread_name: Some("impl/auth".to_string()),
            iteration: 2,
        });

        assert_eq!(
            trace.timeout_reason(),
            "The thread timed out at a call to the model API.\nModel call: iteration 2"
        );
    }

    #[test]
    fn timeout_trace_reports_active_tool_call_details() {
        let mut trace = WorkerTimeoutTrace::default();
        trace.observe(&AgentEvent::ToolCallStarted {
            thread_name: Some("impl/auth".to_string()),
            call_id: "call_123".to_string(),
            name: "exec_command".to_string(),
            args_preview: "cargo test -p sac".to_string(),
            args_detail: Some(
                r#"{"cmd":"cargo test -p sac","tty":false,"yield_time_ms":300000}"#.to_string(),
            ),
        });

        assert_eq!(
            trace.timeout_reason(),
            "The thread timed out at a tool call.\nTool call: exec_command call_123\narguments: {\"cmd\":\"cargo test -p sac\",\"tty\":false,\"yield_time_ms\":300000}"
        );
    }

    #[test]
    fn timeout_trace_ignores_thread_spawned_event() {
        let mut trace = WorkerTimeoutTrace::default();
        trace.observe(&AgentEvent::ThreadSpawned {
            name: "impl/auth".to_string(),
            executable: "/home/secemp9/.local/bin/sac".to_string(),
            cwd: "/workspace/project".to_string(),
            sandboxed: false,
        });

        assert_eq!(
            trace.timeout_reason(),
            "The thread timed out before entering a model API call or tool call."
        );
    }
}