aidaemon 0.9.33

A personal AI agent that runs as a background daemon, accessible via Telegram, Slack, or Discord, with tool use, MCP integration, and persistent memory
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
// ==========================================================================
// Full-Stack Tests
//
// These use `FullStackTestHarness` with a real TerminalTool + ChannelHub
// approval wiring. Tests exercise real shell commands through the agent loop,
// verifying stall detection doesn't false-positive on legitimate exploration.
// ==========================================================================

/// Full-stack regression test: 12+ consecutive terminal calls with unique
/// commands (website exploration scenario). Must complete without stall.
///
/// Replicates the "create a website about cars" production failure where the
/// agent explored a project with `ls`, `git status`, `pwd`, etc. and the
/// stall detection falsely triggered.
#[tokio::test]
async fn test_full_stack_website_exploration_no_stall() {
    let mut responses: Vec<ProviderResponse> = Vec::new();

    let commands = [
        ("Let me explore the project.", r#"{"command": "ls -la"}"#),
        ("Checking system.", r#"{"command": "pwd"}"#),
        ("Git status.", r#"{"command": "git status"}"#),
        ("OS info.", r#"{"command": "uname -a"}"#),
        ("Who am I.", r#"{"command": "whoami"}"#),
        ("Current date.", r#"{"command": "date"}"#),
        ("Disk space.", r#"{"command": "df -h ."}"#),
        ("Environment.", r#"{"command": "env | head -5"}"#),
        ("Shell.", r#"{"command": "echo $SHELL"}"#),
        ("Hostname.", r#"{"command": "hostname"}"#),
        ("Uptime.", r#"{"command": "uptime"}"#),
        ("Process list.", r#"{"command": "ps aux | head -3"}"#),
    ];

    for (narration, args) in &commands {
        let mut resp = MockProvider::tool_call_response("terminal", args);
        resp.content = Some(narration.to_string());
        responses.push(resp);
    }

    // Final text response
    responses.push(MockProvider::text_response(
        "Done! Here's the complete summary of the system exploration.",
    ));

    let harness = setup_full_stack_test_agent(MockProvider::with_responses(responses))
        .await
        .unwrap();

    let response = harness
        .agent
        .handle_message(
            "telegram_test",
            "Explore the current system thoroughly — check files, git, OS, user, disk, and processes.",
            None,
            UserRole::Owner,
            ChannelContext {
                visibility: ChannelVisibility::Private,
                platform: "telegram".to_string(),
                channel_name: None,
                channel_id: None,
                sender_name: Some("Alice".to_string()),
                sender_id: Some("telegram:12345".to_string()),
                channel_member_names: vec![],
                user_id_map: std::collections::HashMap::new(),
                trusted: false,
            },
            None,
        )
        .await
        .unwrap();

    // Agent should either complete normally or stop gracefully after making
    // progress. The key invariant: no crash, no error, and the response is not empty.
    assert!(
        !response.is_empty(),
        "Agent should return a non-empty response"
    );
    assert!(
        !response.contains("stuck in a loop"),
        "Should not trigger stall detection for diverse terminal commands"
    );
}

/// Full-stack test: terminal calls with duplicate commands (real pattern from
/// production). The agent sometimes re-checks things like `ls -la` or
/// `git remote -v` — this should NOT trigger stall detection.
#[tokio::test]
async fn test_full_stack_duplicate_commands_no_stall() {
    let mut responses: Vec<ProviderResponse> = Vec::new();

    let commands = [
        ("Checking project.", r#"{"command": "ls -la"}"#),
        ("Git info.", r#"{"command": "git status"}"#),
        // Duplicate: re-checking project structure
        ("Let me re-check.", r#"{"command": "ls -la"}"#),
        ("Remote.", r#"{"command": "git remote -v"}"#),
        // Duplicate: verifying remote
        ("Verify remote.", r#"{"command": "git remote -v"}"#),
        ("Date check.", r#"{"command": "date"}"#),
        ("Hostname.", r#"{"command": "hostname"}"#),
        // Duplicate: re-checking hostname
        ("Check again.", r#"{"command": "hostname"}"#),
        ("User.", r#"{"command": "whoami"}"#),
        ("Shell.", r#"{"command": "echo $SHELL"}"#),
    ];

    for (narration, args) in &commands {
        let mut resp = MockProvider::tool_call_response("terminal", args);
        resp.content = Some(narration.to_string());
        responses.push(resp);
    }

    responses.push(MockProvider::text_response(
        "Done! Here's what I found about the system.",
    ));

    let harness = setup_full_stack_test_agent(MockProvider::with_responses(responses))
        .await
        .unwrap();

    let response = harness
        .agent
        .handle_message(
            "telegram_test",
            "Check the project — files, git status, remote, hostname, user.",
            None,
            UserRole::Owner,
            ChannelContext {
                visibility: ChannelVisibility::Private,
                platform: "telegram".to_string(),
                channel_name: None,
                channel_id: None,
                sender_name: Some("Alice".to_string()),
                sender_id: Some("telegram:12345".to_string()),
                channel_member_names: vec![],
                user_id_map: std::collections::HashMap::new(),
                trusted: false,
            },
            None,
        )
        .await
        .unwrap();

    // Agent should either complete normally or gracefully stall after making
    // meaningful progress (the new stopping_phase detects stall-with-progress
    // when total_successful_tool_calls >= 3 and returns a partial stall response).
    assert!(
        !response.is_empty(),
        "Agent should return a non-empty response"
    );
    assert!(
        !response.contains("stuck in a loop"),
        "Duplicate commands with diverse patterns should not trigger stall"
    );
}

/// Full-stack test: cli_agent delegation followed by terminal follow-up work.
///
/// Verifies that stall counters reset after cli_agent completion, so the
/// follow-up terminal exploration doesn't inherit stall state from before.
#[tokio::test]
async fn test_full_stack_cli_agent_then_terminal_followup() {
    let mut responses: Vec<ProviderResponse> = Vec::new();

    // Step 1: delegate to cli_agent
    {
        let mut resp = MockProvider::tool_call_response(
            "cli_agent",
            r#"{"action":"run","tool":"claude","prompt":"build website"}"#,
        );
        resp.content = Some("I'll delegate the website build to the CLI agent.".to_string());
        responses.push(resp);
    }

    // Steps 2-9: follow-up terminal work after cli_agent completes
    let followup_commands = [
        ("CLI agent done. Let me verify.", r#"{"command": "ls -la"}"#),
        ("Git status.", r#"{"command": "git status"}"#),
        ("Check remote.", r#"{"command": "git remote -v"}"#),
        ("Who.", r#"{"command": "whoami"}"#),
        ("Date.", r#"{"command": "date"}"#),
        ("Pwd.", r#"{"command": "pwd"}"#),
        ("Uptime.", r#"{"command": "uptime"}"#),
        ("Host.", r#"{"command": "hostname"}"#),
    ];

    for (narration, args) in &followup_commands {
        let mut resp = MockProvider::tool_call_response("terminal", args);
        resp.content = Some(narration.to_string());
        responses.push(resp);
    }

    // Final response
    responses.push(MockProvider::text_response(
        "Done! Website deployed successfully.",
    ));

    // Add mock cli_agent tool
    let cli_agent_mock = Arc::new(MockTool::new(
        "cli_agent",
        "Delegates tasks to CLI agents",
        "Website built successfully. Files in /tmp/my-website",
    ));

    let harness = setup_full_stack_test_agent_with_extra_tools(
        MockProvider::with_responses(responses),
        vec![cli_agent_mock as Arc<dyn crate::traits::Tool>],
    )
    .await
    .unwrap();

    let response = harness
        .agent
        .handle_message(
            "telegram_test",
            "Build a website about cars then verify everything is set up correctly.",
            None,
            UserRole::Owner,
            ChannelContext {
                visibility: ChannelVisibility::Private,
                platform: "telegram".to_string(),
                channel_name: None,
                channel_id: None,
                sender_name: Some("Alice".to_string()),
                sender_id: Some("telegram:12345".to_string()),
                channel_member_names: vec![],
                user_id_map: std::collections::HashMap::new(),
                trusted: false,
            },
            None,
        )
        .await
        .unwrap();

    // Agent should either complete normally or stop gracefully after making
    // progress. The key invariant: no crash, no error, and the response is not empty.
    assert!(
        !response.is_empty(),
        "Agent should return a non-empty response"
    );
}

/// Full-stack test: verify StatusUpdate events flow correctly through the stack.
///
/// Sends a terminal command through the full agent loop and verifies that
/// ToolStart and ToolComplete status updates are emitted.
#[tokio::test]
async fn test_full_stack_status_updates_received() {
    let responses = vec![
        {
            let mut resp =
                MockProvider::tool_call_response("terminal", r#"{"command": "echo hello"}"#);
            resp.content = Some("Let me check something.".to_string());
            resp
        },
        MockProvider::text_response("Done! All good."),
    ];

    let harness = setup_full_stack_test_agent(MockProvider::with_responses(responses))
        .await
        .unwrap();

    // Create status channel to capture updates
    let (status_tx, mut status_rx) = tokio::sync::mpsc::channel::<StatusUpdate>(64);

    let response = harness
        .agent
        .handle_message(
            "telegram_test",
            "Run echo hello",
            Some(status_tx),
            UserRole::Owner,
            ChannelContext::private("telegram"),
            None,
        )
        .await
        .unwrap();

    assert!(
        response.contains("All good"),
        "Agent should complete normally. Got: {}",
        response
    );

    // Collect all status updates
    let mut updates = Vec::new();
    while let Ok(update) = status_rx.try_recv() {
        updates.push(update);
    }

    // Verify we got tool lifecycle events
    let has_tool_start = updates
        .iter()
        .any(|u| matches!(u, StatusUpdate::ToolStart { name, .. } if name == "terminal"));
    let has_tool_complete = updates
        .iter()
        .any(|u| matches!(u, StatusUpdate::ToolComplete { name, .. } if name == "terminal"));
    let has_thinking = updates
        .iter()
        .any(|u| matches!(u, StatusUpdate::Thinking(_)));

    assert!(
        has_tool_start,
        "Should have received ToolStart for terminal. Updates: {:?}",
        updates
    );
    assert!(
        has_thinking,
        "Should have received at least one Thinking update. Updates: {:?}",
        updates
    );
    assert!(
        has_tool_complete,
        "Should have received ToolComplete for terminal. Updates: {:?}",
        updates
    );
}

struct ExternalActionTool;

#[async_trait::async_trait]
impl crate::traits::Tool for ExternalActionTool {
    fn name(&self) -> &str {
        "external_action"
    }

    fn description(&self) -> &str {
        "Writes to an external service for testing."
    }

    fn schema(&self) -> serde_json::Value {
        json!({
            "name": "external_action",
            "description": self.description(),
            "parameters": {
                "type": "object",
                "properties": {}
            }
        })
    }

    fn capabilities(&self) -> crate::traits::ToolCapabilities {
        crate::traits::ToolCapabilities {
            read_only: false,
            external_side_effect: true,
            needs_approval: false,
            idempotent: true,
            high_impact_write: false,
        }
    }

    async fn call(&self, _arguments: &str) -> anyhow::Result<String> {
        Ok("Created remote record id=abc123".to_string())
    }
}

struct PreWrappedExternalActionTool;

#[async_trait::async_trait]
impl crate::traits::Tool for PreWrappedExternalActionTool {
    fn name(&self) -> &str {
        "prewrapped_external_action"
    }

    fn description(&self) -> &str {
        "Writes to an external service and returns already-wrapped output."
    }

    fn schema(&self) -> serde_json::Value {
        json!({
            "name": "prewrapped_external_action",
            "description": self.description(),
            "parameters": {
                "type": "object",
                "properties": {}
            }
        })
    }

    fn capabilities(&self) -> crate::traits::ToolCapabilities {
        crate::traits::ToolCapabilities {
            read_only: false,
            external_side_effect: true,
            needs_approval: false,
            idempotent: true,
            high_impact_write: false,
        }
    }

    async fn call(&self, _arguments: &str) -> anyhow::Result<String> {
        Ok(crate::tools::sanitize::wrap_untrusted_output(
            "prewrapped_external_action",
            "Created remote record id=wrapped123",
        ))
    }
}

struct UrlProbeTool;

#[async_trait::async_trait]
impl crate::traits::Tool for UrlProbeTool {
    fn name(&self) -> &str {
        "url_probe"
    }

    fn description(&self) -> &str {
        "Reads a URL for verification testing."
    }

    fn schema(&self) -> serde_json::Value {
        json!({
            "name": "url_probe",
            "description": self.description(),
            "parameters": {
                "type": "object",
                "properties": {
                    "url": { "type": "string" }
                },
                "required": ["url"],
                "additionalProperties": false
            }
        })
    }

    fn capabilities(&self) -> crate::traits::ToolCapabilities {
        crate::traits::ToolCapabilities {
            read_only: true,
            external_side_effect: false,
            needs_approval: false,
            idempotent: true,
            high_impact_write: false,
        }
    }

    async fn call(&self, arguments: &str) -> anyhow::Result<String> {
        let args: serde_json::Value = serde_json::from_str(arguments)?;
        let url = args
            .get("url")
            .and_then(|value| value.as_str())
            .ok_or_else(|| anyhow::anyhow!("missing url"))?;
        Ok(format!("Checked {} and confirmed the posts are visible.", url))
    }
}

#[tokio::test]
async fn test_successful_external_action_timeout_returns_deterministic_completion() {
    let responses = vec![
        {
            let mut resp = MockProvider::tool_call_response("external_action", "{}");
            resp.content = Some("I'll handle that.".to_string());
            resp
        },
        MockProvider::text_response("This reply should time out before it is used."),
    ];

    let harness = crate::testing::setup_test_agent_with_extra_tools_and_llm_timeout(
        MockProvider::with_delayed_responses(
            responses,
            vec![std::time::Duration::ZERO, std::time::Duration::from_secs(2)],
        ),
        vec![Arc::new(ExternalActionTool)],
        Some(1),
    )
    .await
    .unwrap();

    let (status_tx, mut status_rx) = tokio::sync::mpsc::channel::<StatusUpdate>(64);
    let response = tokio::time::timeout(
        std::time::Duration::from_secs(3),
        harness.agent.handle_message(
            "external_action_timeout",
            "Create the remote record.",
            Some(status_tx),
            UserRole::Owner,
            ChannelContext::private("test"),
            None,
        ),
    )
    .await
    .expect("request should not hang")
    .unwrap();

    assert!(
        response.contains("The requested action completed successfully."),
        "response should use deterministic completion ack: {}",
        response
    );
    assert!(
        response.contains("Created remote record id=abc123"),
        "response should include the latest external action result: {}",
        response
    );

    let mut updates = Vec::new();
    while let Ok(update) = status_rx.try_recv() {
        updates.push(update);
    }

    assert!(
        updates.iter().any(|update| matches!(
            update,
            StatusUpdate::ToolComplete { name, summary }
                if name == "external_action" && summary.contains("Created remote record id=abc123")
        )),
        "expected ToolComplete status update for external_action, got: {:?}",
        updates
    );
}

#[tokio::test]
async fn test_prewrapped_external_action_timeout_keeps_latest_result() {
    let responses = vec![
        {
            let mut resp = MockProvider::tool_call_response("prewrapped_external_action", "{}");
            resp.content = Some("I'll handle that.".to_string());
            resp
        },
        MockProvider::text_response("This reply should time out before it is used."),
    ];

    let harness = crate::testing::setup_test_agent_with_extra_tools_and_llm_timeout(
        MockProvider::with_delayed_responses(
            responses,
            vec![std::time::Duration::ZERO, std::time::Duration::from_secs(2)],
        ),
        vec![Arc::new(PreWrappedExternalActionTool)],
        Some(1),
    )
    .await
    .unwrap();

    let response = tokio::time::timeout(
        std::time::Duration::from_secs(3),
        harness.agent.handle_message(
            "prewrapped_external_action_timeout",
            "Create the remote record.",
            None,
            UserRole::Owner,
            ChannelContext::private("test"),
            None,
        ),
    )
    .await
    .expect("request should not hang")
    .unwrap();

    assert!(
        response.contains("The requested action completed successfully."),
        "response should use deterministic completion ack: {}",
        response
    );
    assert!(
        response.contains("Created remote record id=wrapped123"),
        "response should preserve the latest external action result: {}",
        response
    );
    assert!(
        !response.contains("UNTRUSTED EXTERNAL DATA"),
        "response should not leak wrapper markers into the final reply: {}",
        response
    );
}

#[tokio::test]
async fn test_visible_outcome_request_requires_matching_verification_before_completion() {
    let responses = vec![
        {
            let mut resp = MockProvider::tool_call_response("external_action", "{}");
            resp.content = Some("I'll fix that.".to_string());
            resp
        },
        MockProvider::text_response("Done."),
        MockProvider::tool_call_response(
            "url_probe",
            r#"{"url":"https://blog.aidaemon.ai"}"#,
        ),
        MockProvider::text_response(
            "I checked https://blog.aidaemon.ai and the posts are now visible.",
        ),
    ];

    let harness = crate::testing::setup_test_agent_with_extra_tools_and_llm_timeout(
        MockProvider::with_responses(responses),
        vec![Arc::new(ExternalActionTool), Arc::new(UrlProbeTool)],
        None,
    )
    .await
    .unwrap();

    let response = harness
        .agent
        .handle_message(
            "visible_outcome_verification",
            "I still don't see the posts here: https://blog.aidaemon.ai",
            None,
            UserRole::Owner,
            ChannelContext::private("test"),
            None,
        )
        .await
        .unwrap();

    assert!(
        response.contains("posts are now visible"),
        "final response should reflect a verified outcome: {}",
        response
    );
    assert!(
        !response.contains("The requested action completed successfully."),
        "generic success ack should be blocked until verification completes: {}",
        response
    );
    assert_eq!(
        harness.provider.call_count().await,
        4,
        "agent should continue past the low-signal completion and perform verification"
    );
}

/// Full-stack regression: duplicate identical send_file calls in one task
/// should only execute the underlying send once.
#[tokio::test]
async fn test_full_stack_duplicate_send_file_suppressed() {
    struct CountingSendFileTool {
        calls: Arc<AtomicUsize>,
    }

    #[async_trait::async_trait]
    impl crate::traits::Tool for CountingSendFileTool {
        fn name(&self) -> &str {
            "send_file"
        }

        fn description(&self) -> &str {
            "Test send_file tool that counts executions."
        }

        fn schema(&self) -> serde_json::Value {
            json!({
                "name": "send_file",
                "description": self.description(),
                "parameters": {
                    "type": "object",
                    "properties": {
                        "file_path": { "type": "string" },
                        "caption": { "type": "string" }
                    },
                    "required": ["file_path"]
                }
            })
        }

        async fn call(&self, _arguments: &str) -> anyhow::Result<String> {
            self.calls.fetch_add(1, Ordering::SeqCst);
            Ok("File sent by counting send_file tool".to_string())
        }
    }

    let send_file_args = r#"{"file_path":"/Users/testuser/projects/acme-corp/proposal/sow-project-plan.pdf","caption":"Here is the SOW PDF from the Acme project."}"#;
    let responses = vec![
        MockProvider::tool_call_response("send_file", send_file_args),
        MockProvider::tool_call_response("send_file", send_file_args),
        MockProvider::text_response("Done. I sent the file."),
    ];

    let send_file_calls = Arc::new(AtomicUsize::new(0));
    let send_file_tool = Arc::new(CountingSendFileTool {
        calls: send_file_calls.clone(),
    });

    let harness = setup_full_stack_test_agent_with_extra_tools(
        MockProvider::with_responses(responses),
        vec![send_file_tool as Arc<dyn crate::traits::Tool>],
    )
    .await
    .unwrap();

    let response = harness
        .agent
        .handle_message(
            "telegram_test",
            "Send me the SOW PDF from the Lodestar project",
            None,
            UserRole::Owner,
            ChannelContext::private("telegram"),
            None,
        )
        .await
        .unwrap();

    assert!(
        response.contains("Done."),
        "Agent should complete normally. Got: {}",
        response
    );

    assert_eq!(
        send_file_calls.load(Ordering::SeqCst),
        1,
        "send_file should execute only once for duplicate identical calls"
    );

    let history = harness
        .state
        .get_history("telegram_test", 200)
        .await
        .unwrap();
    let dedupe_msgs = history
        .iter()
        .filter(|m| {
            m.role == "tool"
                && m.tool_name.as_deref() == Some("send_file")
                && m.content
                    .as_deref()
                    .is_some_and(|c| c.contains("Duplicate send_file suppressed"))
        })
        .count();
    assert_eq!(
        dedupe_msgs, 1,
        "Expected one dedupe tool message for suppressed duplicate send_file"
    );
}

/// Regression: once a duplicate send_file is suppressed, the task should be
/// forced into text-only mode instead of repeatedly attempting more file sends.
#[tokio::test]
async fn test_duplicate_send_file_forces_text_closeout() {
    struct CountingSendFileTool {
        calls: Arc<AtomicUsize>,
    }

    #[async_trait::async_trait]
    impl crate::traits::Tool for CountingSendFileTool {
        fn name(&self) -> &str {
            "send_file"
        }

        fn description(&self) -> &str {
            "Test send_file tool that counts executions."
        }

        fn schema(&self) -> serde_json::Value {
            json!({
                "name": "send_file",
                "description": self.description(),
                "parameters": {
                    "type": "object",
                    "properties": {
                        "file_path": { "type": "string" },
                        "caption": { "type": "string" }
                    },
                    "required": ["file_path"]
                }
            })
        }

        async fn call(&self, _arguments: &str) -> anyhow::Result<String> {
            self.calls.fetch_add(1, Ordering::SeqCst);
            Ok("File sent by counting send_file tool".to_string())
        }
    }

    let send_file_args = r#"{"file_path":"/Users/testuser/projects/acme-corp/proposal/sow-project-plan.pdf","caption":"Here is the SOW PDF from the Acme project."}"#;
    let responses = vec![
        MockProvider::tool_call_response("send_file", send_file_args),
        MockProvider::tool_call_response("send_file", send_file_args),
        MockProvider::tool_call_response("send_file", send_file_args),
        MockProvider::text_response("Done. I already sent the file."),
    ];

    let send_file_calls = Arc::new(AtomicUsize::new(0));
    let send_file_tool = Arc::new(CountingSendFileTool {
        calls: send_file_calls.clone(),
    });

    let harness = setup_full_stack_test_agent_with_extra_tools(
        MockProvider::with_responses(responses),
        vec![send_file_tool as Arc<dyn crate::traits::Tool>],
    )
    .await
    .unwrap();

    let response = harness
        .agent
        .handle_message(
            "telegram_test_force_text",
            "Send me the SOW PDF from the Lodestar project",
            None,
            UserRole::Owner,
            ChannelContext::private("telegram"),
            None,
        )
        .await
        .unwrap();

    assert!(
        response.contains("Done. I already sent the file."),
        "Agent should close out with plain text after duplicate send_file. Got: {}",
        response
    );
    assert_eq!(
        send_file_calls.load(Ordering::SeqCst),
        1,
        "Only the first send_file call should execute"
    );

    let history = harness
        .state
        .get_history("telegram_test_force_text", 200)
        .await
        .unwrap();
    let dedupe_msgs = history
        .iter()
        .filter(|m| {
            m.role == "tool"
                && m.tool_name.as_deref() == Some("send_file")
                && m.content
                    .as_deref()
                    .is_some_and(|c| c.contains("Duplicate send_file suppressed"))
        })
        .count();
    assert_eq!(
        dedupe_msgs, 1,
        "Expected exactly one duplicate suppression message"
    );
}

/// Full-stack regression test: "What's the url of the site that you deployed?"
///
/// Real-world scenario: user asks about a previously deployed site. The agent
/// has no memory of the deployment so it searches for clues — checking git
/// remotes, config files, deployment manifests, environment variables, etc.
/// This triggers 10+ consecutive terminal calls as the agent hunts for the URL.
///
/// This is a particularly tricky case because:
/// 1. Many commands return similar "not found" results (low diversity)
/// 2. The agent may retry similar commands in different directories
/// 3. Some commands overlap semantically (git remote -v, cat CNAME, etc.)
#[tokio::test]
async fn test_full_stack_deployed_site_url_lookup_no_stall() {
    let mut responses: Vec<ProviderResponse> = Vec::new();

    // The agent tries to find deployment info through various commands
    let commands = [
        (
            "Let me check the git remote to find the deployment URL.",
            r#"{"command": "git remote -v"}"#,
        ),
        (
            "Let me look for deployment configuration files.",
            r#"{"command": "ls -la"}"#,
        ),
        (
            "Checking for a CNAME or deployment config.",
            r#"{"command": "ls public/ 2>/dev/null || echo 'no public dir'"}"#,
        ),
        (
            "Let me check package.json for deployment scripts.",
            r#"{"command": "cat package.json 2>/dev/null || echo 'no package.json'"}"#,
        ),
        (
            "Looking for Vercel or Netlify config.",
            r#"{"command": "ls vercel.json netlify.toml .vercel 2>/dev/null || echo 'none found'"}"#,
        ),
        (
            "Checking environment variables for URLs.",
            r#"{"command": "env | grep -i url || echo 'no URL env vars'"}"#,
        ),
        (
            "Let me check git log for deployment commits.",
            r#"{"command": "git log --oneline -5 2>/dev/null || echo 'not a git repo'"}"#,
        ),
        (
            "Checking for GitHub Pages or similar config.",
            r#"{"command": "cat CNAME 2>/dev/null || echo 'no CNAME'"}"#,
        ),
        (
            "Looking for docker or CI deployment files.",
            r#"{"command": "ls Dockerfile docker-compose.yml .github/workflows/ 2>/dev/null || echo 'none'"}"#,
        ),
        (
            "Checking the git config for any deploy URLs.",
            r#"{"command": "git config --list 2>/dev/null | grep -i url || echo 'no url in git config'"}"#,
        ),
        (
            "One more check — looking at recent branches.",
            r#"{"command": "git branch -a 2>/dev/null | head -10 || echo 'no branches'"}"#,
        ),
    ];

    for (narration, args) in &commands {
        let mut resp = MockProvider::tool_call_response("terminal", args);
        resp.content = Some(narration.to_string());
        responses.push(resp);
    }

    // Agent gives up and reports what it found
    responses.push(MockProvider::text_response(
        "I couldn't find a specific deployment URL in the current project. \
         The git remote points to github.com but I don't see a CNAME, \
         Vercel config, or Netlify config. Could you tell me which project \
         you're referring to? I may have that info stored from a previous session.",
    ));

    let harness = setup_full_stack_test_agent(MockProvider::with_responses(responses))
        .await
        .unwrap();

    let response = harness
        .agent
        .handle_message(
            "telegram_test",
            "What's the url of the site that you deployed?",
            None,
            UserRole::Owner,
            ChannelContext {
                visibility: ChannelVisibility::Private,
                platform: "telegram".to_string(),
                channel_name: None,
                channel_id: None,
                sender_name: Some("Alice".to_string()),
                sender_id: Some("telegram:12345".to_string()),
                channel_member_names: vec![],
                user_id_map: std::collections::HashMap::new(),
                trusted: false,
            },
            None,
        )
        .await
        .unwrap();

    // Agent should either complete normally or gracefully stall after making
    // meaningful progress (the new stopping_phase detects stall-with-progress
    // when total_successful_tool_calls >= 3 and returns a partial stall response).
    assert!(
        !response.contains("stuck in a loop"),
        "Should not trigger stuck-in-a-loop message. Got: {}",
        response.chars().take(400).collect::<String>()
    );
    // Agent should either complete with a meaningful answer or stop gracefully
    // after making progress. The key invariant: no crash, no error, and not empty.
    assert!(
        !response.is_empty(),
        "Agent should return a non-empty response"
    );
}

/// Full-stack regression test: blocked non-exempt tool triggers false-positive stall.
///
/// Root cause analysis: when the LLM calls a non-exempt tool (e.g. system_info,
/// web_search) more than 3 times, the call gets BLOCKED with a coaching message.
/// But the blocked call doesn't increment `successful_tool_calls`, so if the LLM
/// keeps trying the same tool, every iteration has `successful_tool_calls == 0`,
/// and after 3 such iterations, `stall_count >= 3` fires graceful_stall_response.
///
/// This reproduces the exact "What's the url of the site that you deployed?"
/// failure: the LLM called system_info to search for deployment config, got
/// blocked after 3 calls, then kept trying → stall after 4 tool calls total.
#[tokio::test]
async fn test_full_stack_blocked_tool_triggers_stall() {
    let mut responses: Vec<ProviderResponse> = Vec::new();

    // Iteration 1 (intent gate): narration required
    {
        let mut resp = MockProvider::tool_call_response("system_info", "{}");
        resp.content = Some(
            "Let me look up the deployment URL by checking the system configuration.".to_string(),
        );
        responses.push(resp);
    }

    // Iterations 2-4: system_info executes successfully (3 calls, hits per-tool limit)
    for i in 0..3 {
        let mut resp = MockProvider::tool_call_response(
            "system_info",
            &format!(r#"{{"check":"deploy_{}"}}"#, i),
        );
        resp.content = Some(format!("Checking deployment config {}.", i));
        responses.push(resp);
    }

    // Iterations 5-7: system_info gets BLOCKED (prior_calls >= 3, not exempt)
    // These iterations have successful_tool_calls == 0 → stall_count increments
    for i in 3..6 {
        let mut resp = MockProvider::tool_call_response(
            "system_info",
            &format!(r#"{{"check":"deploy_{}"}}"#, i),
        );
        resp.content = Some(format!("Let me try checking config {} again.", i));
        responses.push(resp);
    }

    // Final: should reach this if stall detection doesn't fire
    responses.push(MockProvider::text_response(
        "I couldn't find the deployment URL. Which project are you referring to?",
    ));

    let harness = setup_full_stack_test_agent(MockProvider::with_responses(responses))
        .await
        .unwrap();

    let response = harness
        .agent
        .handle_message(
            "telegram_test",
            "What's the url of the site that you deployed?",
            None,
            UserRole::Owner,
            ChannelContext {
                visibility: ChannelVisibility::Private,
                platform: "telegram".to_string(),
                channel_name: None,
                channel_id: None,
                sender_name: Some("Alice".to_string()),
                sender_id: Some("telegram:12345".to_string()),
                channel_member_names: vec![],
                user_id_map: std::collections::HashMap::new(),
                trusted: false,
            },
            None,
        )
        .await
        .unwrap();

    // Regression test: blocked tool calls now count as progress for stall
    // detection, so the agent gets a chance to adapt instead of stalling.
    assert!(
        !response.contains("stuck") && !response.contains("not making progress"),
        "Blocked non-exempt tool calls should NOT trigger stall detection. Got: {}",
        response.chars().take(400).collect::<String>()
    );
}