a3s 0.9.8

a3s — A3S coding agent CLI; `a3s code` launches the interactive TUI
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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
//! DeepResearch launch, synthesis, timeout, and recovery controller actions.

use super::*;

impl App {
    fn arm_deep_research_report_repair_after_model_failure(
        &mut self,
        workflow_output: &str,
        workflow_metadata: Option<&serde_json::Value>,
        prior_text: &str,
        failure: &str,
    ) -> bool {
        if self.deep_research_report_repair_used
            || deep_research_workflow_needs_recovery_report_with_metadata(
                workflow_output,
                workflow_metadata,
            )
        {
            return false;
        }
        let prior_is_safe =
            !prior_text.trim().is_empty() && !deep_research_output_has_internal_leak(prior_text);
        if prior_is_safe {
            self.deep_research_workflow.last_synthesis_text = Some(prior_text.to_string());
        }
        if !arm_deep_research_report_repair(
            &mut self.loop_remaining,
            &mut self.deep_research_report_repair_used,
        ) {
            return false;
        }
        let repair_context = if prior_is_safe {
            format!(
                "Validation feedback (must be corrected): {failure}\n\nPrevious synthesis:\n{prior_text}"
            )
        } else {
            format!(
                "Validation feedback (must be corrected): {failure}\n\nThe previous synthesis was omitted because it was empty or contained internal output."
            )
        };
        self.pending_deep_research_report_repair_prompt =
            deep_research_report_repair_prompt_from_state(
                self.deep_research_loop.as_ref(),
                workflow_output,
                workflow_metadata,
                &repair_context,
            );
        if self.pending_deep_research_report_repair_prompt.is_none() {
            return false;
        }
        self.push_line(&Style::new().fg(TN_YELLOW).render(&format!(
            "{failure} Running one focused, evidence-closed report repair pass…"
        )));
        true
    }

    pub(super) fn start_ultracode_synthesis(
        &mut self,
        prompt: String,
        display_task: String,
    ) -> Option<Cmd<Msg>> {
        self.ultracode_synthesis_used = true;
        self.push_line(&Style::new().fg(TN_GRAY).render("  ⇉ synthesizing results…"));
        self.start_stream_inner(prompt, display_task, false, false, true)
    }

    pub(super) fn start_deep_research_report_generation(
        &mut self,
        prompt: String,
        display_task: String,
        phase: DeepResearchReportGenerationPhase,
    ) -> Option<Cmd<Msg>> {
        let query = self.deep_research_loop.as_ref()?.query.clone();
        self.deep_research_report_tool_gate.set_synthesis_only();
        self.streaming.clear();
        self.got_delta = false;
        self.turn_text.clear();
        self.turn_had_agent_activity = false;
        self.turn_text_after_activity = false;
        self.deep_research_report_tools.clear();
        self.running_task = Some(display_task);
        self.state = State::Streaming;
        self.host_progress_inflight = true;
        self.stream_started = Some(Instant::now());
        self.spinner.start();
        self.relayout();
        self.rebuild_viewport();

        self.deep_research_stream_timeout_token =
            self.deep_research_stream_timeout_token.wrapping_add(1);
        let token = self.deep_research_stream_timeout_token;
        let timeout_ms = if phase.is_repair() {
            DEEP_RESEARCH_REPAIR_TIMEOUT_MS
        } else {
            deep_research_planned_synthesis_timeout_ms(
                self.deep_research_workflow.output.as_deref(),
            )
            .unwrap_or(DEEP_RESEARCH_SYNTHESIS_TIMEOUT_MS)
        };
        let args = deep_research_report_generation_args(&prompt, timeout_ms);
        let session = Arc::clone(&self.session);

        Some(cmd::batch(vec![
            cmd::cmd(move || async move {
                let timeout = Duration::from_millis(timeout_ms);
                let result = match tokio::time::timeout(
                    timeout,
                    session.tool("generate_object", args),
                )
                .await
                {
                    Ok(Ok(result)) => Ok(result),
                    Ok(Err(error)) => Err(error.to_string()),
                    Err(_) => {
                        let _ = session
                            .cancel_and_settle(
                                Duration::from_millis(DEEP_RESEARCH_ABORT_GRACE_MS),
                                Duration::from_millis(GRACEFUL_QUIT_ABORT_SETTLE_MS),
                            )
                            .await;
                        Err(format!(
                            "DeepResearch {} model call timed out after {timeout_ms} ms",
                            if phase.is_repair() {
                                "repair"
                            } else {
                                "synthesis"
                            }
                        ))
                    }
                };
                Msg::DeepResearchReportGenerated {
                    token,
                    query,
                    phase,
                    result,
                }
            }),
            spinner_tick(),
            stream_commit_tick(),
        ]))
    }

    pub(super) fn on_deep_research_report_generated(
        &mut self,
        token: u64,
        query: String,
        phase: DeepResearchReportGenerationPhase,
        result: Result<ToolCallResult, String>,
    ) -> Option<Cmd<Msg>> {
        if token != self.deep_research_stream_timeout_token
            || self.state != State::Streaming
            || self.interrupting
            || self
                .deep_research_loop
                .as_ref()
                .map(|state| state.query.as_str())
                != Some(query.as_str())
        {
            return None;
        }
        self.host_progress_inflight = false;

        let generated = result.and_then(|result| {
            deep_research_report_from_generation(&result.output, result.exit_code)
        });
        let (generated_report, mut generation_error) = match generated {
            Ok(report) => (Some(report), None),
            Err(error) => (None, Some(error)),
        };
        let report_text = generated_report
            .as_ref()
            .map(|report| report.markdown.clone())
            .unwrap_or_default();
        if !phase.is_repair() && !report_text.trim().is_empty() {
            self.deep_research_workflow.last_synthesis_text = Some(report_text.clone());
        }

        let workflow_output = self
            .deep_research_workflow
            .output
            .clone()
            .unwrap_or_default();
        let workflow_metadata = self.deep_research_workflow.metadata.clone();
        let evidence_scope = self
            .deep_research_workflow
            .args
            .as_ref()
            .map(|args| deep_research_evidence_scope_from_args(args, &query))
            .unwrap_or_default();
        let report_outcome = deep_research_report_outcome_for_workflow(
            &query,
            evidence_scope,
            &workflow_output,
            workflow_metadata.as_ref(),
        );
        let workspace = PathBuf::from(&self.cwd);
        let artifacts = generated_report.as_ref().and_then(|report| {
            match materialize_deep_research_completed_report_from_generation(
                &workspace,
                &query,
                report,
                &workflow_output,
                workflow_metadata.as_ref(),
            ) {
                Ok(artifacts) => Some(artifacts),
                Err(error) => {
                    generation_error = Some(error);
                    None
                }
            }
        });

        if let Some(artifacts) = artifacts {
            let final_text = clean_deep_research_final_text_from_artifacts(&artifacts, &workspace)
                .unwrap_or(report_text);
            self.loop_remaining = 0;
            self.stage_deep_research_report(&artifacts, report_outcome);
            self.streaming.push(&final_text);
            self.turn_text.clear();
            self.turn_text.push_str(&final_text);
            self.mark_assistant_text(&final_text);
            self.finalize_streaming();
            self.push_line(&Style::new().fg(TN_GREEN).render(&format!(
                "  ✓ DeepResearch report validated and rendered at {}",
                artifacts.html.display()
            )));
            return self.complete_turn();
        }

        let diagnostic = generation_error.unwrap_or_else(|| {
            deep_research_report_rejection_diagnostic_from_answer_text(
                &query,
                &report_text,
                &workflow_output,
                workflow_metadata.as_ref(),
            )
            .unwrap_or_else(|| {
                "report artifacts were not accepted for an unknown reason".to_string()
            })
        });
        self.push_line(&Style::new().fg(TN_YELLOW).render(&format!(
            "  ⚠ DeepResearch {} report rejected: {diagnostic}",
            if phase.is_repair() {
                "repair"
            } else {
                "synthesis"
            }
        )));

        if !phase.is_repair()
            && self.arm_deep_research_report_repair_after_model_failure(
                &workflow_output,
                workflow_metadata.as_ref(),
                &report_text,
                &format!("The initial structured synthesis was rejected: {diagnostic}."),
            )
        {
            let repair_prompt = self.pending_deep_research_report_repair_prompt.take()?;
            self.loop_remaining = self.loop_remaining.saturating_sub(1);
            return self.start_deep_research_report_generation(
                repair_prompt,
                format!("\u{200A}repair report {query}"),
                DeepResearchReportGenerationPhase::Repair,
            );
        }

        let recovery_text = if report_text.trim().is_empty() {
            self.deep_research_workflow
                .last_synthesis_text
                .as_deref()
                .unwrap_or(&diagnostic)
        } else {
            report_text.as_str()
        };
        match materialize_deep_research_recovery_report(
            &workspace,
            &query,
            recovery_text,
            &workflow_output,
            workflow_metadata.as_ref(),
        ) {
            Ok(artifacts) => {
                let final_text =
                    clean_deep_research_final_text_from_artifacts(&artifacts, &workspace)
                        .unwrap_or_else(|| diagnostic.clone());
                self.stage_deep_research_report(&artifacts, DeepResearchRunOutcome::Degraded);
                self.streaming.push(&final_text);
                self.turn_text.clear();
                self.turn_text.push_str(&final_text);
                self.mark_assistant_text(&final_text);
                self.finalize_streaming();
                self.push_line(&Style::new().fg(TN_YELLOW).render(&format!(
                    "  ⚠ DeepResearch could not validate a completed report; wrote a degraded recovery report at {}",
                    artifacts.html.display()
                )));
            }
            Err(error) => {
                self.push_line(&Style::new().fg(TN_RED).render(&format!(
                    "  error: DeepResearch report recovery failed: {error}"
                )));
            }
        }
        self.loop_remaining = 0;
        self.deep_research_report_repair_used = true;
        self.complete_turn()
    }

    pub(super) fn start_deep_research_workflow(
        &mut self,
        query: String,
        _os_runtime: bool,
        evidence_scope: DeepResearchEvidenceScope,
        runtime_expectation: Option<RuntimeExpectation>,
    ) -> Option<Cmd<Msg>> {
        self.auto_review.on_user_turn();
        self.last_activity = Instant::now();
        let os_runtime = false;
        self.streaming.clear();
        self.got_delta = false;
        self.turn_text.clear();
        self.turn_had_agent_activity = false;
        self.turn_text_after_activity = false;
        if self.deep_research_goal_restore.is_none() {
            self.deep_research_goal_restore = Some((self.goal.clone(), self.goal_since));
        }
        self.goal = Some(deep_research_goal(&query));
        self.goal_since = Some(Instant::now());
        self.engage_single_turn_autonomy();
        let run_started_at = Instant::now();
        self.deep_research_loop = Some(DeepResearchLoop {
            query: query.clone(),
            total_layers: 1,
            os_runtime,
            evidence_scope,
            started_at: run_started_at,
            phase_started_at: None,
        });
        self.deep_research_report_repair_used = false;
        self.deep_research_workflow
            .reset_for_run(snapshot_deep_research_report_artifacts(
                Path::new(&self.cwd),
                &query,
            ));
        self.deep_research_outcome = DeepResearchRunOutcome::Active;
        self.deep_research_subagent_settlement_inflight = false;
        self.deep_research_journal_finalization_inflight = false;
        self.deep_research_terminal_artifacts = None;
        self.deep_research_agent_event_sequence = 0;
        self.deep_research_projection = None;
        self.pending_deep_research_report_repair_prompt = None;
        self.pending_deep_research_synthesis = None;
        self.pending_deep_research_report_view = None;
        self.deep_research_report_tools.clear();
        self.deep_research_report_tool_gate
            .set_report_target(Path::new(&self.cwd), &query);
        self.deep_research_report_tool_gate
            .set_evidence_scope(evidence_scope);
        if let Some(expectation) = runtime_expectation {
            self.runtime_expectation = Some(expectation);
        }
        self.ultracode_synthesis_inflight = false;
        self.ultracode_synthesis_used = false;
        self.last_paint = None;
        self.viewport.set_auto_scroll(true);
        self.plan.clear();
        self.runtime.clear_turn_entities();
        let display_task = format!("\u{200A}{query}");
        self.runtime.set_subagent_task(display_task.clone());
        self.running_task = Some(display_task);
        self.state = State::Streaming;
        self.relayout();
        self.stream_started = Some(Instant::now());
        self.spinner.start();
        self.push_line(
            &Style::new()
                .fg(TN_GRAY)
                .render("  ⇉ gathering evidence with bounded recursive DynamicWorkflowRuntime…"),
        );
        self.rebuild_viewport();

        let budget = deep_research_budget_for_effort_index(self.effort, self.context_limit);
        let mut args =
            deep_research_workflow_args_for_budget(&query, os_runtime, evidence_scope, budget);
        ensure_deep_research_workflow_run_id(&mut args);
        self.deep_research_workflow.args = Some(args.clone());
        let (progress_rx, workflow_join) = self
            .session
            .tool_with_events("dynamic_workflow", args.clone());
        let progress_rx = Arc::new(Mutex::new(progress_rx));
        self.rx = Some(progress_rx.clone());
        self.stream_join = None;
        self.host_tool_abort = Some(workflow_join.abort_handle());
        self.host_progress_inflight = true;
        self.host_tool_call_id = None;
        self.interrupting = false;
        let workflow_abort = workflow_join.abort_handle();
        let configured_timeout_ms = deep_research_workflow_host_timeout_ms(&args);
        let timeout = Duration::from_millis(configured_timeout_ms).min(
            Duration::from_millis(DEEP_RESEARCH_RUN_HARD_TIMEOUT_MS)
                .saturating_sub(run_started_at.elapsed()),
        );
        let timeout_ms = timeout.as_millis().min(u128::from(u64::MAX)) as u64;
        let workflow_workspace = PathBuf::from(&self.cwd);
        let args_for_timeout = args.clone();
        let journal_run_id = args
            .get("run_id")
            .and_then(serde_json::Value::as_str)
            .map(str::to_string);
        let journal_spec = ResearchSpec {
            query: query.clone(),
            current_date: chrono::Local::now().date_naive().to_string(),
            evidence_scope: evidence_scope.label().to_string(),
            required_claims: Vec::new(),
            total_budget_ms: timeout_ms,
            finalization_reserve_ms: timeout_ms.saturating_mul(15) / 100,
            host_pid: std::process::id(),
        };
        let finalization_reserve_ms = journal_spec.finalization_reserve_ms;
        Some(cmd::batch(vec![
            cmd::cmd(move || async move {
                if let Some(run_id) = journal_run_id.as_deref() {
                    let _ = record_deep_research_workflow_started(
                        &workflow_workspace,
                        run_id,
                        journal_spec,
                    )
                    .await;
                }
                let mut workflow_join = workflow_join;
                let result = match tokio::time::timeout(timeout, &mut workflow_join).await {
                    Ok(Ok(result)) => result.map_err(|err| err.to_string()),
                    Ok(Err(err)) => Err(err.to_string()),
                    Err(_) => {
                        workflow_abort.abort();
                        let _ = tokio::time::timeout(
                            Duration::from_millis(DEEP_RESEARCH_ABORT_GRACE_MS),
                            &mut workflow_join,
                        )
                        .await;
                        let message = format!(
                            "dynamic_workflow timed out after {timeout_ms} ms while gathering DeepResearch evidence"
                        );
                        deep_research_workflow_timeout_tool_result(
                            &workflow_workspace,
                            &args_for_timeout,
                            message,
                        )
                    }
                };
                let result = result.map(|mut result| {
                    result.output = deep_research_canonical_workflow_output(
                        &result.output,
                        result.metadata.as_ref(),
                    );
                    result
                });
                let (workflow_output, workflow_metadata) = match &result {
                    Ok(result) => (result.output.as_str(), result.metadata.as_ref()),
                    Err(error) => (error.as_str(), None),
                };
                let convergence = evaluate_convergence(deep_research_convergence_input(
                    DeepResearchConvergenceContext {
                        query: &query,
                        evidence_scope,
                        workflow_output,
                        workflow_metadata,
                        args: &args,
                        elapsed: run_started_at.elapsed(),
                        total_budget_ms: timeout_ms,
                        finalization_reserve_ms,
                    },
                ));
                let accepted_evidence =
                    accepted_evidence_ledger(workflow_output, workflow_metadata);
                if let Some(run_id) = journal_run_id.as_deref() {
                    let _ = record_deep_research_workflow_completed(
                        &workflow_workspace,
                        run_id,
                        result.is_ok(),
                    )
                    .await;
                    let contradictory_evidence = accepted_evidence
                        .iter()
                        .filter(|item| !item.contradictions.is_empty())
                        .cloned()
                        .collect::<Vec<_>>();
                    if !contradictory_evidence.is_empty() {
                        let _ = fork_current_for_contradiction_review(
                            &workflow_workspace,
                            run_id,
                            &contradictory_evidence,
                        )
                        .await;
                    }
                    let _ = record_deep_research_evidence_ledger(
                        &workflow_workspace,
                        run_id,
                        &accepted_evidence,
                    )
                    .await;
                    let _ =
                        record_deep_research_convergence(&workflow_workspace, run_id, &convergence)
                            .await;
                }
                Msg::DeepResearchWorkflowCompleted {
                    query,
                    os_runtime,
                    args,
                    result,
                    convergence,
                    accepted_evidence,
                }
            }),
            pump(progress_rx),
            spinner_tick(),
            stream_commit_tick(),
        ]))
    }

    pub(super) fn on_deep_research_workflow_completed(
        &mut self,
        query: String,
        os_runtime: bool,
        args: serde_json::Value,
        result: Result<ToolCallResult, String>,
        convergence: ConvergenceDecision,
        accepted_evidence: Vec<AcceptedEvidence>,
    ) -> Option<Cmd<Msg>> {
        let current_run_id = self
            .deep_research_workflow
            .args
            .as_ref()
            .and_then(|value| value.get("run_id"))
            .and_then(serde_json::Value::as_str);
        let completed_run_id = args.get("run_id").and_then(serde_json::Value::as_str);
        let current_query = self
            .deep_research_loop
            .as_ref()
            .map(|state| state.query.as_str());
        if self.state != State::Streaming
            || self.interrupting
            || current_query != Some(query.as_str())
            || current_run_id.is_none()
            || current_run_id != completed_run_id
        {
            return None;
        }
        self.host_tool_abort = None;
        self.host_progress_inflight = false;
        self.rx = None;
        let tool_id = self.host_tool_call_id.take().unwrap_or_else(|| {
            format!(
                "host-dynamic_workflow-{}",
                completed_run_id.unwrap_or("unknown")
            )
        });

        let (output, exit_code, metadata) = match result {
            Ok(result) => (result.output, result.exit_code, result.metadata),
            Err(error) => (error, 1, None),
        };
        self.deep_research_workflow.output = Some(output.clone());
        self.deep_research_workflow.metadata = metadata.clone();
        self.deep_research_workflow.args = Some(args.clone());
        let display_output = deep_research_tool_card_output(&output);
        let completed = self.runtime.end_tool(
            &tool_id,
            "dynamic_workflow".to_string(),
            Some(args.clone()),
            display_output.clone(),
            exit_code,
        );
        self.messages.finish_tool_with_state(
            &tool_id,
            "dynamic_workflow".to_string(),
            completed.args.clone(),
            completed.output.clone(),
            completed.exit_code,
            metadata.clone(),
            completed.state,
            true,
        );
        self.rebuild_viewport();
        self.record_runtime_tool_evidence("dynamic_workflow");
        if metadata
            .as_ref()
            .is_some_and(|value| json_contains_tool_evidence(value, "runtime"))
        {
            self.record_runtime_tool_evidence("runtime");
        }
        if metadata
            .as_ref()
            .is_some_and(|value| json_contains_tool_evidence(value, "parallel_task"))
        {
            self.record_runtime_parallel_evidence();
        }
        self.backfill_parallel_subagents_from_workflow_metadata(metadata.as_ref());
        if completed.first_terminal {
            self.capture_workflow("dynamic_workflow", completed.args.as_ref());
        }
        if let Some(spec) = self.find_remote_view_spec(&output) {
            self.remember_remote_view(spec);
        }
        let evidence_scope = deep_research_evidence_scope_from_args(&args, &query);
        if let Some(status) = deep_research_plan_status(&output) {
            self.push_line(&Style::new().fg(TN_GRAY).render(&status));
        }

        let report_outcome = deep_research_report_outcome_for_workflow(
            &query,
            evidence_scope,
            &output,
            metadata.as_ref(),
        );
        if accepted_evidence.is_empty()
            || matches!(report_outcome, DeepResearchRunOutcome::Degraded)
        {
            self.loop_remaining = 0;
            self.deep_research_outcome = DeepResearchRunOutcome::Degraded;
            let status = match materialize_deep_research_recovery_report(
                Path::new(&self.cwd),
                &query,
                &format!(
                    "Evidence collection ended without a validated evidence package. Convergence decision: {}.",
                    convergence.reason
                ),
                &output,
                metadata.as_ref(),
            ) {
                Ok(artifacts) => {
                    self.stage_deep_research_report(
                        &artifacts,
                        DeepResearchRunOutcome::Degraded,
                    );
                    format!(
                        "DeepResearch stopped after bounded evidence collection because {}. A low-confidence recovery report was written to `{}`.",
                        convergence.reason,
                        artifacts.html.display()
                    )
                }
                Err(error) => format!(
                    "DeepResearch stopped after bounded evidence collection and could not write its recovery report: {error}"
                ),
            };
            self.push_line(&Style::new().fg(TN_YELLOW).render(&format!("{status}")));
            self.mark_assistant_text(&status);
            self.turn_text.clear();
            self.turn_text.push_str(&status);
            self.messages
                .push(TranscriptEntry::assistant_markdown(status));
            self.rebuild_viewport();
            return self.complete_turn();
        }

        let synthesis_evidence = accepted_evidence_synthesis_payload(&accepted_evidence, &output);
        let prompt = if exit_code == 0 {
            self.push_line(
                &Style::new()
                    .fg(TN_GRAY)
                    .render("  ⇉ evidence gathered · synthesizing source-backed report…"),
            );
            deep_research_synthesis_prompt_with_scope(
                &query,
                os_runtime,
                &synthesis_evidence,
                None,
                evidence_scope,
            )
        } else {
            self.push_line(
                &Style::new()
                    .fg(TN_YELLOW)
                    .render("  ⚠ dynamic workflow failed; starting recovery synthesis…"),
            );
            deep_research_recovery_prompt_with_scope(
                &query,
                os_runtime,
                &synthesis_evidence,
                None,
                evidence_scope,
            )
        };
        self.deep_research_report_tool_gate.set_synthesis_only();
        if !self.queue.is_empty() {
            // Treat messages submitted during evidence collection as user
            // follow-ups. Run them before report synthesis, then resume this
            // exact synthesis once the user queue is empty.
            let display = format!("\u{200A}synthesize {query}");
            self.pending_deep_research_synthesis = Some((prompt, display));
            self.finish();
            return self.drain_queue();
        }
        self.start_deep_research_report_generation(
            prompt,
            format!("\u{200A}synthesize {query}"),
            DeepResearchReportGenerationPhase::Synthesis,
        )
    }

    pub(super) fn on_deep_research_synthesis_timed_out(&mut self, token: u64) -> Option<Cmd<Msg>> {
        if token != self.deep_research_stream_timeout_token
            || self.state != State::Streaming
            || self.host_progress_inflight
            || self.deep_research_loop.is_none()
        {
            return None;
        }

        let repair_phase = self.deep_research_report_repair_used;
        let timeout_ms = if repair_phase {
            DEEP_RESEARCH_REPAIR_TIMEOUT_MS
        } else {
            deep_research_planned_synthesis_timeout_ms(
                self.deep_research_workflow.output.as_deref(),
            )
            .unwrap_or(DEEP_RESEARCH_SYNTHESIS_TIMEOUT_MS)
        };
        let now = Instant::now();
        let loop_state = self.deep_research_loop.as_ref()?;
        let phase_started_at = loop_state.phase_started_at.unwrap_or(loop_state.started_at);
        if let Some(delay) = deep_research_synthesis_timeout_delay(
            loop_state.started_at,
            phase_started_at,
            now,
            Duration::from_millis(timeout_ms),
            self.runtime.active_tool_count(),
            self.deep_research_report_tools.is_empty(),
        ) {
            return Some(cmd::cmd(move || async move {
                tokio::time::sleep(delay).await;
                Msg::DeepResearchSynthesisTimedOut { token }
            }));
        }
        let phase = if repair_phase { "repair" } else { "synthesis" };
        let status = format!("DeepResearch {phase} model call timed out after {timeout_ms} ms.");

        let session = Arc::clone(&self.session);
        let join = self.stream_join.take();
        self.rx = None;
        let streamed_text = self.turn_text.clone();
        self.interrupting = true;
        self.push_line(&Style::new().fg(TN_YELLOW).render(&format!(
            "{status} Cancelling the timed-out DeepResearch run before writing recovery artifacts…"
        )));

        Some(cmd::cmd(move || async move {
            let _ = session
                .cancel_and_settle(
                    Duration::from_millis(DEEP_RESEARCH_ABORT_GRACE_MS),
                    Duration::from_millis(GRACEFUL_QUIT_ABORT_SETTLE_MS),
                )
                .await;
            if let Some(join) = join {
                let _ = settle_stream_join_for_quit(
                    join,
                    Duration::from_millis(GRACEFUL_QUIT_ABORT_SETTLE_MS),
                )
                .await;
            }
            Msg::DeepResearchSynthesisTimedOutAfterCancel {
                token,
                status,
                streamed_text,
                report_completed: false,
            }
        }))
    }

    pub(super) fn stop_deep_research_synthesis_if_report_ready(&mut self) -> Option<Cmd<Msg>> {
        if self.interrupting
            || self.state != State::Streaming
            || self.deep_research_loop.is_none()
            || !self.deep_research_report_tool_gate.report_only()
        {
            return None;
        }
        let query = &self.deep_research_loop.as_ref()?.query;
        let marker = format!(
            "{RESEARCH_VIEW_MARKER} .a3s/research/{}/index.html",
            deep_research_report_slug(query)
        );
        let baseline = self.deep_research_workflow.report_baseline.as_ref()?;
        deep_research_report_artifacts_from_output_for_current_run(
            &marker,
            Path::new(&self.cwd),
            query,
            self.deep_research_workflow
                .output
                .as_deref()
                .unwrap_or_default(),
            self.deep_research_workflow.metadata.as_ref(),
            baseline,
        )?;

        let token = self.deep_research_stream_timeout_token;
        let session = Arc::clone(&self.session);
        let join = self.stream_join.take();
        self.rx = None;
        self.interrupting = true;
        Some(cmd::cmd(move || async move {
            let _ = session
                .cancel_and_settle(
                    Duration::from_millis(DEEP_RESEARCH_ABORT_GRACE_MS),
                    Duration::from_millis(GRACEFUL_QUIT_ABORT_SETTLE_MS),
                )
                .await;
            if let Some(join) = join {
                let _ = settle_stream_join_for_quit(
                    join,
                    Duration::from_millis(GRACEFUL_QUIT_ABORT_SETTLE_MS),
                )
                .await;
            }
            Msg::DeepResearchSynthesisTimedOutAfterCancel {
                token,
                status: "DeepResearch report artifacts completed".to_string(),
                streamed_text: marker,
                report_completed: true,
            }
        }))
    }

    pub(super) fn on_deep_research_synthesis_timed_out_after_cancel(
        &mut self,
        token: u64,
        status: String,
        streamed_text: String,
        report_completed: bool,
    ) -> Option<Cmd<Msg>> {
        if token != self.deep_research_stream_timeout_token || self.deep_research_loop.is_none() {
            return None;
        }

        self.finalize_streaming();
        self.preserve_interrupted_tools();
        if report_completed {
            self.push_line(
                &Style::new()
                    .fg(TN_GRAY)
                    .render("  ✓ report artifacts validated; synthesis stream stopped"),
            );
        } else {
            self.push_line(&Style::new().fg(TN_YELLOW).render(&format!(
                "{status} Checking for a completed report before writing recovery artifacts."
            )));
        }

        let workspace = PathBuf::from(&self.cwd);
        let repair_phase = self.deep_research_report_repair_used;
        let query = self
            .deep_research_loop
            .as_ref()
            .map(|state| state.query.clone());
        let workflow_output = self
            .deep_research_workflow
            .output
            .clone()
            .unwrap_or_default();
        let workflow_metadata = self.deep_research_workflow.metadata.clone();
        let workflow_args = self.deep_research_workflow.args.clone();
        let report_outcome = query.as_deref().map(|query| {
            let evidence_scope = workflow_args
                .as_ref()
                .map(|args| deep_research_evidence_scope_from_args(args, query))
                .unwrap_or_default();
            deep_research_report_outcome_for_workflow(
                query,
                evidence_scope,
                &workflow_output,
                workflow_metadata.as_ref(),
            )
        });
        let validated_view = query.as_deref().and_then(|query| {
            let baseline = self.deep_research_workflow.report_baseline.as_ref()?;
            deep_research_report_view_spec_for_current_run(
                &streamed_text,
                &workspace,
                query,
                &workflow_output,
                workflow_metadata.as_ref(),
                baseline,
            )
        });
        if let Some(spec) = validated_view {
            self.deep_research_outcome =
                report_outcome.unwrap_or(DeepResearchRunOutcome::Completed);
            self.pending_deep_research_report_view = Some(spec);
            self.push_line(&Style::new().fg(TN_YELLOW).render(
                "  ⚠ DeepResearch timed out after writing a validated current-query report; preserving its RemoteUI view.",
            ));
        } else {
            let prior_synthesis_text = repair_phase
                .then_some(self.deep_research_workflow.last_synthesis_text.as_deref())
                .flatten()
                .map(str::to_string);
            match query {
                Some(query) => {
                    let (workflow_output, workflow_metadata) =
                        recover_deep_research_workflow_state_for_report_timeout(
                            &workspace,
                            &query,
                            workflow_args.as_ref(),
                            workflow_output,
                            workflow_metadata,
                        );
                    self.deep_research_workflow.output = Some(workflow_output.clone());
                    self.deep_research_workflow.metadata = workflow_metadata.clone();
                    let marker = format!(
                        "{RESEARCH_VIEW_MARKER} .a3s/research/{}/index.html",
                        deep_research_report_slug(&query)
                    );
                    let current_run_artifacts = self
                        .deep_research_workflow
                        .report_baseline
                        .as_ref()
                        .and_then(|baseline| {
                            deep_research_report_artifacts_from_output_for_current_run(
                                &marker,
                                &workspace,
                                &query,
                                &workflow_output,
                                workflow_metadata.as_ref(),
                                baseline,
                            )
                        });
                    let completed_artifacts = current_run_artifacts.or_else(|| {
                        materialize_deep_research_timeout_completed_report(
                            &workspace,
                            &query,
                            &streamed_text,
                            prior_synthesis_text.as_deref(),
                            &workflow_output,
                            workflow_metadata.as_ref(),
                        )
                    });
                    if let Some(artifacts) = completed_artifacts {
                        let evidence_scope = workflow_args
                            .as_ref()
                            .map(|args| deep_research_evidence_scope_from_args(args, &query))
                            .unwrap_or_default();
                        let report_outcome = deep_research_report_outcome_for_workflow(
                            &query,
                            evidence_scope,
                            &workflow_output,
                            workflow_metadata.as_ref(),
                        );
                        self.stage_deep_research_report(&artifacts, report_outcome);
                        self.push_line(&Style::new().fg(TN_YELLOW).render(&format!(
                            "  ⚠ DeepResearch timed out, but a completed report was recovered into {}",
                            artifacts.html.display()
                        )));
                    } else if !repair_phase
                        && self.arm_deep_research_report_repair_after_model_failure(
                            &workflow_output,
                            workflow_metadata.as_ref(),
                            &streamed_text,
                            "The initial synthesis timed out before producing a valid report.",
                        )
                    {
                        return self.complete_turn();
                    } else {
                        let recovery_text = [
                            prior_synthesis_text.as_deref(),
                            Some(streamed_text.as_str()),
                        ]
                        .into_iter()
                        .flatten()
                        .find(|text| {
                            !text.trim().is_empty() && !deep_research_output_has_internal_leak(text)
                        })
                        .unwrap_or(status.as_str());
                        match materialize_deep_research_recovery_report(
                            &workspace,
                            &query,
                            recovery_text,
                            &workflow_output,
                            workflow_metadata.as_ref(),
                        ) {
                            Ok(artifacts) => {
                                self.stage_deep_research_report(
                                    &artifacts,
                                    DeepResearchRunOutcome::Degraded,
                                );
                                self.push_line(&Style::new().fg(TN_YELLOW).render(&format!(
                                    "  ⚠ DeepResearch recovery report written at {}",
                                    artifacts.html.display()
                                )));
                            }
                            Err(error) => self.push_line(&Style::new().fg(TN_RED).render(
                                &format!("  error: DeepResearch recovery report failed: {error}"),
                            )),
                        }
                    }
                }
                None => self.push_line(&Style::new().fg(TN_RED).render(
                    "  error: DeepResearch timed out but the original query is unavailable",
                )),
            }
        }

        self.loop_remaining = 0;
        self.deep_research_report_repair_used = true;
        self.complete_turn()
    }

    pub(super) fn recover_deep_research_report_after_model_error(&mut self, message: &str) -> bool {
        let Some(query) = self
            .deep_research_loop
            .as_ref()
            .map(|state| state.query.clone())
        else {
            return false;
        };

        self.finalize_streaming();
        let workspace = PathBuf::from(&self.cwd);
        let workflow_output = self
            .deep_research_workflow
            .output
            .clone()
            .unwrap_or_default();
        let workflow_metadata = self.deep_research_workflow.metadata.clone();
        let evidence_scope = self
            .deep_research_workflow
            .args
            .as_ref()
            .map(|args| deep_research_evidence_scope_from_args(args, &query))
            .unwrap_or_default();
        let report_outcome = deep_research_report_outcome_for_workflow(
            &query,
            evidence_scope,
            &workflow_output,
            workflow_metadata.as_ref(),
        );
        let partial_text = self.turn_text.clone();
        if let Some(artifacts) = materialize_deep_research_timeout_completed_report(
            &workspace,
            &query,
            &partial_text,
            self.deep_research_workflow.last_synthesis_text.as_deref(),
            &workflow_output,
            workflow_metadata.as_ref(),
        ) {
            self.stage_deep_research_report(&artifacts, report_outcome);
            self.push_line(&Style::new().fg(TN_YELLOW).render(&format!(
                "  ⚠ DeepResearch synthesis failed; preserved a completed source-backed report at {}",
                artifacts.html.display()
            )));
            self.loop_remaining = 0;
            self.deep_research_report_repair_used = true;
            return true;
        }
        if self.arm_deep_research_report_repair_after_model_failure(
            &workflow_output,
            workflow_metadata.as_ref(),
            &partial_text,
            "The report model call failed.",
        ) {
            return true;
        }
        let artifacts = materialize_deep_research_recovery_report(
            &workspace,
            &query,
            message,
            &workflow_output,
            workflow_metadata.as_ref(),
        );
        match artifacts {
            Ok(artifacts) => {
                self.stage_deep_research_report(&artifacts, DeepResearchRunOutcome::Degraded);
                self.push_line(&Style::new().fg(TN_YELLOW).render(&format!(
                    "  ⚠ DeepResearch synthesis and repair failed; wrote an explicit low-confidence recovery report at {}",
                    artifacts.html.display()
                )));
            }
            Err(error) => self.push_line(&Style::new().fg(TN_RED).render(&format!(
                "  error: DeepResearch report recovery failed: {error}"
            ))),
        }
        self.loop_remaining = 0;
        self.deep_research_report_repair_used = true;
        true
    }

    pub(super) fn backfill_parallel_subagents_from_workflow_metadata(
        &mut self,
        metadata: Option<&serde_json::Value>,
    ) {
        let backfills = metadata
            .map(workflow_parallel_subagent_backfills)
            .unwrap_or_default();
        if backfills.is_empty() {
            return;
        }
        let now = Instant::now();
        for backfill in backfills {
            self.runtime.start_subagent(
                backfill.task_id.clone(),
                backfill.agent.clone(),
                backfill.description,
                now,
            );
            self.runtime.end_subagent(
                backfill.task_id,
                backfill.agent,
                String::new(),
                backfill.success,
                now,
            );
        }
        self.relayout();
    }
}