a3s 0.9.6

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
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};

use a3s_code_core::tools::{Tool, ToolContext, ToolExecutor, ToolOutput};

#[derive(Clone)]
struct PlannedLoopTaskTool {
    tool_name: &'static str,
    planner_calls: Arc<AtomicUsize>,
    checker_calls: Arc<AtomicUsize>,
    maker_calls: Arc<AtomicUsize>,
    investigation: bool,
    targeted_direct: bool,
    repeated_direct: bool,
    digest_regression: bool,
    linked_url_priority: bool,
    maker_failure: bool,
    maker_then_direct: bool,
    first_checker_delay_ms: u64,
    retrieval_timeout_override_ms: u64,
    checker_failure_at: Option<usize>,
}

struct PlannedLoopSearchTool;
struct NoisyPlannedLoopSearchTool;
struct OversizedPlannedLoopSearchTool;
struct PlannedLoopFetchTool;
struct TransientPlannedLoopFetchTool {
    calls: Arc<AtomicUsize>,
}
struct MetadataOnlyPlannedLoopFetchTool;
struct LateEntityPlannedSeedTool;
struct LateEntityPlannedSeedFetchTool;
struct ObservedLinkFetchTool {
    fetched_urls: Arc<Mutex<Vec<String>>>,
}

fn parallel_output(structured: serde_json::Value) -> ToolOutput {
    ToolOutput::success("structured loop role completed").with_metadata(serde_json::json!({
        "task_count": 1,
        "result_count": 1,
        "success_count": 1,
        "failed_count": 0,
        "all_success": true,
        "partial_failure": false,
        "allow_partial_failure": false,
        "results": [{
            "task_id": "fixture-task",
            "agent": "deep-research",
            "success": true,
            "structured": structured
        }]
    }))
}

fn generated_object_output(object: serde_json::Value) -> ToolOutput {
    ToolOutput::success(
        serde_json::json!({
            "object": object,
            "repair_rounds": 0,
            "mode_used": "prompt"
        })
        .to_string(),
    )
    .with_metadata(serde_json::json!({
        "schema_name": "fixture",
        "mode_used": "prompt",
        "repair_rounds": 0
    }))
}

fn register_planned_loop_tools(executor: &ToolExecutor, tool: PlannedLoopTaskTool) {
    let mut object_tool = tool.clone();
    object_tool.tool_name = "generate_object";
    executor.register_dynamic_tool(Arc::new(object_tool));

    let mut task_tool = tool;
    task_tool.tool_name = "parallel_task";
    executor.register_dynamic_tool(Arc::new(task_tool));
}

fn use_planned_web_tools(source: &str, search_tool: &str, fetch_tool: &str) -> String {
    source
        .replace(
            "ctx.tool(\"web_search\"",
            &format!("ctx.tool(\"{search_tool}\""),
        )
        .replace(
            "ctx.tool(\"web_fetch\"",
            &format!("ctx.tool(\"{fetch_tool}\""),
        )
        .replace("tool: \"web_search\"", &format!("tool: \"{search_tool}\""))
        .replace("tool: \"web_fetch\"", &format!("tool: \"{fetch_tool}\""))
}

#[async_trait::async_trait]
impl Tool for PlannedLoopTaskTool {
    fn name(&self) -> &str {
        self.tool_name
    }

    fn description(&self) -> &str {
        "Returns deterministic planner and checker decisions for the engineered-loop test."
    }

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({ "type": "object" })
    }

    async fn execute(
        &self,
        args: &serde_json::Value,
        _ctx: &ToolContext,
    ) -> anyhow::Result<ToolOutput> {
        let schema_name = args
            .get("schema_name")
            .and_then(serde_json::Value::as_str)
            .unwrap_or_default();
        let description = args
            .pointer("/tasks/0/description")
            .and_then(serde_json::Value::as_str)
            .unwrap_or_default();
        let role_output = |value| {
            if self.tool_name == "generate_object" {
                generated_object_output(value)
            } else {
                parallel_output(value)
            }
        };
        if schema_name == "deep_research_plan" || description.starts_with("Plan research") {
            self.planner_calls.fetch_add(1, Ordering::SeqCst);
            if self.investigation
                || self.digest_regression
                || self.maker_failure
                || self.maker_then_direct
            {
                return Ok(role_output(serde_json::json!({
                    "answer_shape": "investigation",
                    "freshness_required": false,
                    "workspace_evidence_required": false,
                    "execution_route": if self.digest_regression { "direct_then_maker" } else { "maker_first" },
                    "report_title": "Adaptive Loop Evidence Assessment",
                    "phases": [{
                        "name": "primary investigation",
                        "success_criterion": "Primary evidence and counterevidence are both covered."
                    }],
                    "tracks": if self.digest_regression {
                        vec![
                            serde_json::json!({
                                "title": "Primary explanation",
                                "focus": "Establish the primary evidence and causal account."
                            }),
                            serde_json::json!({
                                "title": "Independent challenge",
                                "focus": "Test the explanation against independent evidence."
                            }),
                        ]
                    } else {
                        vec![serde_json::json!({
                            "title": "Primary explanation",
                            "focus": "Establish the primary evidence and causal account."
                        })]
                    },
                    "search_queries": ["adaptive loop current status evidence"],
                    "seed_urls": if self.digest_regression {
                        vec![
                            "https://seed-1.example/status",
                            "https://seed-2.example/status",
                            "https://seed-3.example/status",
                            "https://seed-4.example/status",
                        ]
                    } else {
                        Vec::new()
                    },
                    "budget": {
                        "retrieval_timeout_ms": if self.retrieval_timeout_override_ms > 0 {
                            self.retrieval_timeout_override_ms
                        } else {
                            60000
                        },
                        "synthesis_timeout_ms": 30000,
                        "max_iterations": 2,
                        "max_parallel_tasks": 2,
                        "max_steps_per_task": 2,
                        "per_task_timeout_ms": 15000,
                        "direct_searches": 1,
                        "direct_fetches": if self.digest_regression { 4 } else { 2 }
                    },
                    "stop_conditions": ["checker confirms the recommendation survives counterevidence"]
                })));
            }
            return Ok(role_output(serde_json::json!({
                "answer_shape": "lookup",
                "freshness_required": true,
                "workspace_evidence_required": false,
                "execution_route": "direct_only",
                "report_title": "Adaptive Loop Current Status",
                "phases": [{
                    "name": "retrieve and verify",
                    "success_criterion": "The current fact is traceable and corroborated."
                }],
                "tracks": [{
                    "title": "Current fact",
                    "focus": "Retrieve and corroborate the requested status without broadening."
                }],
                "search_queries": ["adaptive loop current status official", "adaptive loop current status independent"],
                "seed_urls": [],
                "budget": {
                    "retrieval_timeout_ms": 30000,
                    "synthesis_timeout_ms": 15000,
                    "max_iterations": if self.targeted_direct || self.repeated_direct || self.linked_url_priority { 2 } else { 1 },
                    "max_parallel_tasks": 1,
                    "max_steps_per_task": 2,
                    "per_task_timeout_ms": 10000,
                    "direct_searches": if self.linked_url_priority { 1 } else { 2 },
                    "direct_fetches": if self.linked_url_priority { 1 } else { 2 }
                },
                "stop_conditions": ["the checker confirms the requested status is traceable"]
            })));
        }
        if schema_name == "deep_research_check" || description.starts_with("Check evidence") {
            anyhow::ensure!(
                args.get("max_repair_attempts")
                    .or_else(|| args.pointer("/tasks/0/max_repair_attempts"))
                    .and_then(serde_json::Value::as_u64)
                    == Some(1),
                "checker must receive one bounded structured-output repair attempt"
            );
            let checker_index = self.checker_calls.fetch_add(1, Ordering::SeqCst);
            let prompt = args
                .get("prompt")
                .or_else(|| args.pointer("/tasks/0/prompt"))
                .and_then(serde_json::Value::as_str)
                .unwrap_or_default();
            anyhow::ensure!(
                prompt.contains("Workflow budget:"),
                "checker prompt must expose the remaining workflow budget"
            );
            anyhow::ensure!(
                prompt.contains("A URL, title, or search snippet alone is a discovery lead")
                    && prompt.contains("exact supporting source URL"),
                "checker prompt must enforce source quality and claim traceability"
            );
            let checked_role_output = |mut value: serde_json::Value| {
                let source_url = [
                    "https://oversized-1.example/status",
                    "https://official.example/status",
                    "https://evidence2.example/research",
                    "https://evidence1.example/research",
                ]
                .into_iter()
                .find(|candidate| prompt.contains(candidate));
                let supported =
                    value.get("decision").and_then(serde_json::Value::as_str) == Some("finalize");
                let finding = value
                    .get("verified_findings")
                    .and_then(serde_json::Value::as_array)
                    .and_then(|findings| findings.first())
                    .and_then(serde_json::Value::as_str)
                    .unwrap_or("The planned evidence obligation remains bounded.")
                    .to_string();
                let source_urls = source_url
                    .filter(|_| supported)
                    .into_iter()
                    .map(str::to_string)
                    .collect::<Vec<_>>();
                let track_count = if self.digest_regression { 2 } else { 1 };
                value["track_assessments"] = serde_json::Value::Array(
                    (0..track_count)
                        .map(|plan_index| {
                            serde_json::json!({
                                "plan_index": plan_index,
                                "status": if supported { "supported" } else { "bounded" },
                                "finding": finding.clone(),
                                "source_urls": source_urls.clone(),
                            })
                        })
                        .collect(),
                );
                value["stop_condition_assessments"] = serde_json::json!([{
                    "plan_index": 0,
                    "status": if supported { "supported" } else { "bounded" },
                    "finding": finding,
                    "source_urls": source_urls,
                }]);
                value["limitations"] = serde_json::json!([]);
                role_output(value)
            };
            if self.checker_failure_at == Some(checker_index) {
                return Ok(ToolOutput::error("simulated checker timeout"));
            }
            if checker_index == 0 && self.first_checker_delay_ms > 0 {
                tokio::time::sleep(std::time::Duration::from_millis(
                    self.first_checker_delay_ms,
                ))
                .await;
            }
            if self.linked_url_priority && checker_index == 0 {
                return Ok(checked_role_output(serde_json::json!({
                    "decision": "continue",
                    "coverage_summary": "One linked primary detail should be fetched before finalizing.",
                    "report_summary": "The current status is supported, pending one linked primary detail.",
                    "verified_findings": ["The retained sources support the current status."],
                    "unresolved_gaps": ["linked primary detail"],
                    "contradictions": [],
                    "next_action": "direct_retrieval",
                    "search_queries": [],
                    "seed_urls": ["https://invented.example/missing"],
                    "next_tracks": [],
                    "reason": "A bounded linked-source fetch can close the gap."
                })));
            }
            if self.maker_failure {
                anyhow::ensure!(
                    prompt.chars().count() <= 30_000,
                    "checker prompt exceeded the bounded convergence envelope"
                );
                anyhow::ensure!(
                    prompt.contains("no usable evidence"),
                    "checker did not receive the failed maker attempt in cumulative evidence"
                );
                return Ok(checked_role_output(serde_json::json!({
                    "decision": "finalize",
                    "coverage_summary": "Direct recovery evidence is sufficient after the maker timed out.",
                    "report_summary": "Direct sources establish the requested current status.",
                    "verified_findings": ["The current status is confirmed by retained direct evidence."],
                    "unresolved_gaps": [],
                    "contradictions": [],
                    "next_action": "none",
                    "search_queries": [],
                    "seed_urls": [],
                    "next_tracks": [],
                    "reason": "The direct recovery sources satisfy the observable completion criterion."
                })));
            }
            if self.maker_then_direct {
                if checker_index == 0 {
                    return Ok(checked_role_output(serde_json::json!({
                        "decision": "continue",
                        "coverage_summary": "Maker evidence needs one current primary source.",
                        "report_summary": "The analysis is supported but still needs one current primary source.",
                        "verified_findings": ["The maker evidence supports the current analysis."],
                        "unresolved_gaps": ["one current primary source"],
                        "contradictions": [],
                        "next_action": "direct_retrieval",
                        "search_queries": ["adaptive loop current status official"],
                        "seed_urls": [],
                        "next_tracks": [],
                        "reason": "One bounded direct lookup closes the checked gap."
                    })));
                }
                anyhow::ensure!(
                    prompt.contains("https://official.example/status"),
                    "checker lost direct evidence gathered after a maker-first round"
                );
                return Ok(checked_role_output(serde_json::json!({
                    "decision": "finalize",
                    "coverage_summary": "Maker and direct evidence now cover the question.",
                    "report_summary": "Maker analysis and a current primary source jointly answer the question.",
                    "verified_findings": ["The current primary source corroborates the maker analysis."],
                    "unresolved_gaps": [],
                    "contradictions": [],
                    "next_action": "none",
                    "search_queries": [],
                    "seed_urls": [],
                    "next_tracks": [],
                    "reason": "The cumulative evidence satisfies the stop condition."
                })));
            }
            if (self.targeted_direct || self.repeated_direct) && checker_index == 0 {
                return Ok(checked_role_output(serde_json::json!({
                    "decision": "continue",
                    "coverage_summary": "The baseline is supported, but one current externally retrievable fact is missing.",
                    "report_summary": "The retained sources support the baseline finding.",
                    "verified_findings": ["The baseline finding is source-backed."],
                    "unresolved_gaps": ["one current fact"],
                    "contradictions": [],
                    "next_action": "direct_retrieval",
                    "search_queries": ["adaptive loop missing current fact official"],
                    "seed_urls": [],
                    "next_tracks": [],
                    "reason": "One bounded direct lookup can close the remaining gap."
                })));
            }
            if self.repeated_direct
                && checker_index == 1
                && self.maker_calls.load(Ordering::SeqCst) == 0
            {
                return Ok(checked_role_output(serde_json::json!({
                    "decision": "continue",
                    "coverage_summary": "The bounded direct retry did not close the checked gap.",
                    "report_summary": "The baseline remains supported, while one consequential gap is unresolved.",
                    "verified_findings": ["The baseline finding remains source-backed."],
                    "unresolved_gaps": ["one consequential gap still needs evidence production"],
                    "contradictions": [],
                    "next_action": "maker",
                    "search_queries": ["adaptive loop repeated lookup"],
                    "seed_urls": [],
                    "next_tracks": [],
                    "reason": "The direct attempt did not close the gap."
                })));
            }
            if self.investigation && checker_index == 0 {
                return Ok(checked_role_output(serde_json::json!({
                    "decision": "continue",
                    "coverage_summary": "The primary explanation is covered but counterevidence is still missing.",
                    "report_summary": "The current evidence supports the primary explanation.",
                    "verified_findings": ["The primary explanation is supported by traceable evidence."],
                    "unresolved_gaps": ["credible counterevidence"],
                    "contradictions": [],
                    "next_action": "maker",
                    "search_queries": [],
                    "seed_urls": [],
                    "next_tracks": [{
                        "title": "Counterevidence",
                        "focus": "Test the primary explanation against credible counterevidence."
                    }],
                    "reason": "One consequential gap justifies one targeted follow-up iteration."
                })));
            }
            if self.digest_regression && checker_index == 0 {
                anyhow::ensure!(
                    self.maker_calls.load(Ordering::SeqCst) == 1,
                    "direct_then_maker must run the planned maker before its first checker"
                );
                anyhow::ensure!(
                    prompt.contains("MAKER_DIGEST_MARKER")
                        && prompt.contains("https://oversized-1.example/status")
                        && prompt.contains("SOURCE_FACT_TAIL"),
                    "the first post-maker checker lost cumulative direct and maker evidence"
                );
                return Ok(checked_role_output(serde_json::json!({
                    "decision": "continue",
                    "coverage_summary": "Maker evidence is preserved; one direct fact remains.",
                    "report_summary": "The maker analysis is preserved and supported by direct evidence.",
                    "verified_findings": ["The maker analysis survives comparison with direct evidence."],
                    "unresolved_gaps": ["one bounded fact after maker analysis"],
                    "contradictions": [],
                    "next_action": "direct_retrieval",
                    "search_queries": ["adaptive loop post-maker fact"],
                    "seed_urls": [],
                    "next_tracks": [],
                    "reason": "One direct lookup can close the remaining gap."
                })));
            }
            if self.digest_regression && checker_index == 1 {
                anyhow::ensure!(
                    prompt.chars().count() <= 30_000,
                    "combined checker prompt exceeded the bounded convergence envelope"
                );
                anyhow::ensure!(
                    prompt.contains("MAKER_DIGEST_MARKER"),
                    "checker prompt dropped schema-validated maker evidence"
                );
                anyhow::ensure!(
                    prompt.contains("SOURCE_FACT_TAIL"),
                    "checker prompt truncated the retained source fact"
                );
                anyhow::ensure!(
                    prompt.contains("https://oversized-1.example/status"),
                    "checker prompt dropped the direct evidence class"
                );
                anyhow::ensure!(
                    prompt.contains("MAKER_DIGEST_MARKER")
                        && prompt.contains("https://oversized-1.example/status"),
                    "post-maker direct follow-up checker lost cumulative evidence"
                );
            }
            return Ok(checked_role_output(serde_json::json!({
                "decision": "finalize",
                "coverage_summary": "Two independently hosted fetched sources support the current fact.",
                "report_summary": "Two independent sources confirm the current fact.",
                "verified_findings": ["The requested current fact is independently corroborated."],
                "unresolved_gaps": [],
                "contradictions": [],
                "next_action": "none",
                "search_queries": [],
                "seed_urls": [],
                "next_tracks": [],
                "reason": "The observable completion criterion is satisfied."
            })));
        }
        if self.digest_regression {
            let prompt = args
                .get("prompt")
                .or_else(|| args.pointer("/tasks/0/prompt"))
                .and_then(serde_json::Value::as_str)
                .unwrap_or_default();
            anyhow::ensure!(
                prompt.contains("Observed candidate URLs")
                    && prompt.contains("https://oversized-1.example/status"),
                "maker prompt did not surface observed direct-evidence URLs"
            );
            anyhow::ensure!(
                prompt.contains("Do not call any tool")
                    && prompt.contains("return the existing source-backed evidence"),
                "prompt fallback maker did not reuse the existing evidence package"
            );
            if self.tool_name == "generate_object" {
                anyhow::ensure!(
                    args.get("schema_name").and_then(serde_json::Value::as_str)
                        == Some("deep_research_evidence")
                        && args
                            .get("max_repair_attempts")
                            .and_then(serde_json::Value::as_u64)
                            == Some(1),
                    "source-grounded prompt maker must use one v5.2.2 structured call"
                );
            } else {
                anyhow::ensure!(
                    args.pointer("/tasks/0/max_steps")
                        .and_then(serde_json::Value::as_u64)
                        == Some(3),
                    "maker must reserve a structured-finalization turn after bounded evidence collection"
                );
                anyhow::ensure!(
                    args.get("tasks")
                        .and_then(serde_json::Value::as_array)
                        .is_some_and(|tasks| tasks.len() == 1),
                    "prompt-based structured output must pack planned tracks into one maker request"
                );
            }
            anyhow::ensure!(
                prompt.contains("Primary explanation") && prompt.contains("Independent challenge"),
                "packed maker request lost a planned evidence track"
            );
        }
        let maker_index = self.maker_calls.fetch_add(1, Ordering::SeqCst);
        anyhow::ensure!(
            args.get("min_success_count").is_none(),
            "DeepResearch must not cancel unverified maker outputs after a raw success count"
        );
        if self.maker_failure {
            return Ok(ToolOutput::error("simulated maker timeout"));
        }
        let url = if self.tool_name == "generate_object" {
            if self.digest_regression {
                "https://oversized-1.example/status".to_string()
            } else {
                "https://official.example/status".to_string()
            }
        } else {
            format!("https://evidence{}.example/research", maker_index + 1)
        };
        let gaps = if maker_index == 0 {
            vec!["credible counterevidence"]
        } else {
            Vec::new()
        };
        let structured = serde_json::json!({
            "summary": if self.digest_regression {
                "MAKER_DIGEST_MARKER: schema-validated maker evidence survived compaction."
                    .to_string()
            } else {
                format!("Iteration {} produced decision-relevant evidence.", maker_index + 1)
            },
            "sources": [{
                "title": format!("Evidence {}", maker_index + 1),
                "url_or_path": url.clone(),
                "date": "2026-07-12",
                "quote_or_fact": if self.digest_regression {
                    format!("Retained source fact: {} SOURCE_FACT_TAIL", "x".repeat(500))
                } else {
                    "Traceable evidence tests the requested decision.".to_string()
                },
                "reliability": "Deterministic integration fixture."
            }],
            "key_evidence": ["Traceable evidence tests the requested decision."],
            "contradictions": [],
            "confidence": "medium",
            "gaps": gaps
        });
        if self.tool_name == "generate_object" {
            return Ok(generated_object_output(structured));
        }
        Ok(
            ToolOutput::success("maker evidence completed").with_metadata(serde_json::json!({
                // A maker owns an independent child-task clock. Simulate a maker
                // that used more wall time than the direct retrieval budget so the
                // checker can still route one bounded direct follow-up afterward.
                "duration_ms": if self.maker_then_direct { 60_001 } else { 0 },
                "task_count": 1,
                "result_count": 1,
                "success_count": 1,
                "failed_count": 0,
                "all_success": true,
                "partial_failure": false,
                "allow_partial_failure": true,
                "results": [{
                    "task_id": format!("maker-{}", maker_index + 1),
                    "agent": "deep-research",
                    "success": true,
                    "source_anchors": [{ "tool": "web_fetch", "url_or_path": url }],
                    "structured": structured
                }]
            })),
        )
    }
}

#[async_trait::async_trait]
impl Tool for PlannedLoopSearchTool {
    fn name(&self) -> &str {
        "planned_web_search"
    }

    fn description(&self) -> &str {
        "Returns two independently hosted current-status sources."
    }

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({ "type": "object" })
    }

    async fn execute(
        &self,
        _args: &serde_json::Value,
        _ctx: &ToolContext,
    ) -> anyhow::Result<ToolOutput> {
        Ok(ToolOutput::success(
            serde_json::json!([
                {
                    "title": "Official current status",
                    "url": "https://official.example/status",
                    "content": "Adaptive loop current status is operational.",
                    "published_date": "2026-07-12",
                    "engines": ["fixture"]
                },
                {
                    "title": "Independent current status",
                    "url": "https://independent.example/status",
                    "content": "Adaptive loop current status is operational.",
                    "published_date": "2026-07-12",
                    "engines": ["fixture"]
                }
            ])
            .to_string(),
        ))
    }
}

#[async_trait::async_trait]
impl Tool for NoisyPlannedLoopSearchTool {
    fn name(&self) -> &str {
        "noisy_planned_web_search"
    }

    fn description(&self) -> &str {
        "Returns useful pages mixed with non-document web assets."
    }

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({ "type": "object" })
    }

    async fn execute(
        &self,
        _args: &serde_json::Value,
        _ctx: &ToolContext,
    ) -> anyhow::Result<ToolOutput> {
        let result = |title: &str, url: &str| {
            serde_json::json!({
                "title": title,
                "url": url,
                "content": "Adaptive loop current status is operational.",
                "published_date": "2026-07-12",
                "engines": ["fixture"]
            })
        };
        Ok(ToolOutput::success(
            serde_json::json!([
                result(
                    "Adaptive loop status avatar",
                    "https://avatars.githubusercontent.com/u/42"
                ),
                result(
                    "Adaptive loop status avatar",
                    "https://secure.gravatar.com/avatar/abc"
                ),
                result(
                    "Adaptive loop status profile",
                    "https://api.github.com/users/example"
                ),
                result(
                    "Adaptive loop status followers",
                    "https://api.github.com/users/example/followers"
                ),
                result(
                    "Adaptive loop status following template",
                    "https://api.github.com/users/example/following{/other_user}"
                ),
                result(
                    "Adaptive loop status gist template",
                    "https://api.github.com/users/example/gists{/gist_id}"
                ),
                result(
                    "Adaptive loop status archive",
                    "https://downloads.example/adaptive-loop-current-status.zip"
                ),
                result(
                    "Truncated adaptive loop result",
                    "https://truncated.example/adaptive-loop-current-status…"
                ),
                result(
                    "Adaptive loop status font",
                    "https://cdn.example/adaptive-loop-current-status.woff2"
                ),
                result("Official current status", "https://official.example/status"),
                result(
                    "Independent current status",
                    "https://independent.example/status"
                )
            ])
            .to_string(),
        ))
    }
}

#[async_trait::async_trait]
impl Tool for OversizedPlannedLoopSearchTool {
    fn name(&self) -> &str {
        "oversized_planned_web_search"
    }

    fn description(&self) -> &str {
        "Returns enough direct evidence to exceed the former blind prefix limit."
    }

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({ "type": "object" })
    }

    async fn execute(
        &self,
        _args: &serde_json::Value,
        _ctx: &ToolContext,
    ) -> anyhow::Result<ToolOutput> {
        let results = (1..=12)
            .map(|index| {
                serde_json::json!({
                    "title": format!("Oversized direct source {index}"),
                    "url": format!("https://oversized-{index}.example/status"),
                    "content": format!(
                        "Adaptive loop current status evidence from source {index}. {}",
                        "Detailed direct evidence. ".repeat(80)
                    ),
                    "published_date": "2026-07-12",
                    "engines": ["fixture"]
                })
            })
            .collect::<Vec<_>>();
        Ok(ToolOutput::success(
            serde_json::to_string(&results).expect("fixture search results should serialize"),
        ))
    }
}

#[async_trait::async_trait]
impl Tool for PlannedLoopFetchTool {
    fn name(&self) -> &str {
        "planned_web_fetch"
    }

    fn description(&self) -> &str {
        "Returns fetched evidence for the planned-loop test."
    }

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({ "type": "object" })
    }

    async fn execute(
        &self,
        args: &serde_json::Value,
        _ctx: &ToolContext,
    ) -> anyhow::Result<ToolOutput> {
        let url = args
            .get("url")
            .and_then(serde_json::Value::as_str)
            .unwrap_or_default();
        Ok(ToolOutput::success(format!(
            "# Provide feedback\n\n# Search code, repositories, users, issues, pull requests\n\n* [Code](/adaptive-loop-current-status) * [Issues](/adaptive-loop-current-status/issues)\n\n[data-color-mode=\"light\"] .HeaderMenu-link:focus-visible {{ outline-color: var(--color-accent-fg); }}\n\n# Adaptive loop current status\n\nAs of 2026-07-12 the service is operational. Source: {url}"
        )))
    }
}

#[async_trait::async_trait]
impl Tool for TransientPlannedLoopFetchTool {
    fn name(&self) -> &str {
        "transient_planned_web_fetch"
    }

    fn description(&self) -> &str {
        "Fails the initial fetch batch with transport errors, then succeeds on retry."
    }

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({ "type": "object" })
    }

    async fn execute(
        &self,
        args: &serde_json::Value,
        _ctx: &ToolContext,
    ) -> anyhow::Result<ToolOutput> {
        let call = self.calls.fetch_add(1, Ordering::SeqCst);
        if call < 2 {
            return Ok(ToolOutput::error("TLS handshake EOF"));
        }
        let url = args
            .get("url")
            .and_then(serde_json::Value::as_str)
            .unwrap_or_default();
        Ok(ToolOutput::success(format!(
            "# Adaptive loop current status\n\nAs of 2026-07-12 the service is operational. Source: {url}"
        )))
    }
}

#[async_trait::async_trait]
impl Tool for MetadataOnlyPlannedLoopFetchTool {
    fn name(&self) -> &str {
        "metadata_planned_web_fetch"
    }

    fn description(&self) -> &str {
        "Returns a JSON-LD-only page body so direct evidence falls back to the clean search fact."
    }

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({ "type": "object" })
    }

    async fn execute(
        &self,
        _args: &serde_json::Value,
        _ctx: &ToolContext,
    ) -> anyhow::Result<ToolOutput> {
        Ok(ToolOutput::success(
            r#"\[{"@context":"https://schema.org/","@type":"BlogPosting","headline":"Adaptive loop status","description":"Machine metadata must not become the reader-facing evidence quote."}\]"#,
        ))
    }
}

#[async_trait::async_trait]
impl Tool for LateEntityPlannedSeedTool {
    fn name(&self) -> &str {
        "generate_object"
    }

    fn description(&self) -> &str {
        "Returns a plan whose relevant seed identity occurs late in a long query."
    }

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({ "type": "object" })
    }

    async fn execute(
        &self,
        args: &serde_json::Value,
        _ctx: &ToolContext,
    ) -> anyhow::Result<ToolOutput> {
        let schema_name = args
            .get("schema_name")
            .and_then(serde_json::Value::as_str)
            .unwrap_or_default();
        if schema_name == "deep_research_plan" {
            return Ok(generated_object_output(serde_json::json!({
                "answer_shape": "lookup",
                "freshness_required": false,
                "workspace_evidence_required": false,
                "execution_route": "direct_only",
                "report_title": "Project Quasar isolation",
                "phases": ["retrieve the planned source"],
                "tracks": ["Project Quasar isolation boundary"],
                "search_queries": [],
                "seed_urls": ["https://github.com/example/project-quasar"],
                "budget": {
                    "retrieval_timeout_ms": 30000,
                    "synthesis_timeout_ms": 15000,
                    "max_iterations": 1,
                    "max_parallel_tasks": 1,
                    "max_steps_per_task": 1,
                    "per_task_timeout_ms": 10000,
                    "direct_searches": 0,
                    "direct_fetches": 1
                },
                "stop_conditions": ["the planned source establishes the isolation boundary"]
            })));
        }
        anyhow::ensure!(
            schema_name == "deep_research_check",
            "unexpected structured call: {schema_name}"
        );
        let prompt = args
            .get("prompt")
            .and_then(serde_json::Value::as_str)
            .unwrap_or_default();
        anyhow::ensure!(
            prompt.contains("Project Quasar uses a separate guest kernel"),
            "the checker did not receive the fetched planned-seed evidence"
        );
        Ok(generated_object_output(serde_json::json!({
            "decision": "finalize",
            "coverage_summary": "The fetched project source establishes the requested boundary.",
            "report_summary": "Project Quasar uses a separate guest kernel for isolation.",
            "verified_findings": ["Project Quasar uses a separate guest kernel."],
            "track_assessments": [{
                "plan_index": 0,
                "status": "supported",
                "finding": "Project Quasar uses a separate guest kernel.",
                "source_urls": ["https://github.com/example/project-quasar"]
            }],
            "stop_condition_assessments": [{
                "plan_index": 0,
                "status": "supported",
                "finding": "The planned project source establishes the isolation boundary.",
                "source_urls": ["https://github.com/example/project-quasar"]
            }],
            "unresolved_gaps": [],
            "limitations": [],
            "contradictions": [],
            "next_action": "none",
            "search_queries": [],
            "seed_urls": [],
            "next_tracks": [],
            "reason": "The planned source directly answers the bounded question."
        })))
    }
}

#[async_trait::async_trait]
impl Tool for LateEntityPlannedSeedFetchTool {
    fn name(&self) -> &str {
        "late_entity_planned_seed_fetch"
    }

    fn description(&self) -> &str {
        "Returns substantive text for a planned seed with a late query identity."
    }

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({ "type": "object" })
    }

    async fn execute(
        &self,
        _args: &serde_json::Value,
        _ctx: &ToolContext,
    ) -> anyhow::Result<ToolOutput> {
        Ok(ToolOutput::success(
            "# Project Quasar\n\nProject Quasar uses a separate guest kernel to isolate untrusted workloads from the host kernel. Its project documentation also describes explicit filesystem and network boundaries.",
        ))
    }
}

#[async_trait::async_trait]
impl Tool for ObservedLinkFetchTool {
    fn name(&self) -> &str {
        "observed_link_web_fetch"
    }

    fn description(&self) -> &str {
        "Records fetch order and exposes a source-observed absolute link."
    }

    fn parameters(&self) -> serde_json::Value {
        serde_json::json!({ "type": "object" })
    }

    async fn execute(
        &self,
        args: &serde_json::Value,
        _ctx: &ToolContext,
    ) -> anyhow::Result<ToolOutput> {
        let url = args
            .get("url")
            .and_then(serde_json::Value::as_str)
            .unwrap_or_default()
            .to_string();
        self.fetched_urls.lock().unwrap().push(url.clone());
        Ok(ToolOutput::success(format!(
            "# Adaptive loop current status\n\nAdaptive loop current status is operational. Read the [primary status detail](/detail) for the source-backed isolation boundary. The unrelated [project license](/LICENSE) is not evidence for that gap. Source: {url}"
        )))
    }
}

include!("engineered_loop_integration_tests.rs");
include!("engineered_loop_direct_review_tests.rs");
include!("engineered_loop_search_fallback_tests.rs");