ai-ipc 0.3.1

API server for inter-process communication between AI agents.
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
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
use anyhow::Result;
use chrono::Utc;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::process::Command;
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::time::{sleep, Duration, Instant};

use crate::client::CollabClient;

const TRIVIAL_REPLY_PATTERN: &str = r"(?i)^(acknowledged|got it|thanks|thank you|ok|okay|will do|on it|roger)$";
const PING_PATTERN: &str = r"(?i)^(ping|status|are you there\??|health ?check|you up\??)$";
/// Matches messages that are pure acknowledgments — no new information, just confirming receipt.
/// These start with ack-like phrases and contain no task assignments or new requests.
const ACK_START_PATTERN: &str = r"(?i)^(@[\w-]+\s+)*\s*(acknowledged|ack\b|aligned|standing by|same gate|holding|received|noted|roger|unchanged|freeze (holds|respected|unchanged)|gate freeze|doc freeze|standby)";
pub const DEFAULT_CLI_TEMPLATE: &str = "claude -p {prompt} --model {model} --allowedTools Bash,Read,Write,Edit";

#[derive(Debug, Clone, Copy, PartialEq)]
enum PromptTier {
    /// Handled entirely by the harness — no CLI spawn
    Harness,
    /// Minimal prompt — role + message + compact schema
    Light,
    /// Full prompt — teammates, state, todos, full schema
    Full,
}

impl std::fmt::Display for PromptTier {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PromptTier::Harness => write!(f, "harness"),
            PromptTier::Light => write!(f, "light"),
            PromptTier::Full => write!(f, "full"),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    pub sender: String,
    pub content: String,
    pub hash: String,
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub recipient: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct WorkerState {
    #[serde(default)]
    pub last_task: Option<String>,
    #[serde(default)]
    pub pending: Option<String>,
    #[serde(default)]
    pub files_touched: Vec<String>,
    /// Shown on roster — what this worker is currently doing
    #[serde(default)]
    pub status: Option<String>,
}

/// Deserialize a Vec that might be null (models output null instead of [])
fn null_as_empty_vec<'de, D, T>(deserializer: D) -> std::result::Result<Vec<T>, D::Error>
where
    D: serde::Deserializer<'de>,
    T: serde::Deserialize<'de>,
{
    Option::<Vec<T>>::deserialize(deserializer).map(|v| v.unwrap_or_default())
}

#[derive(Debug, Serialize, Deserialize)]
struct CollabOutput {
    #[serde(default)]
    pub response: Option<String>,
    #[serde(default, deserialize_with = "null_as_empty_vec")]
    pub delegate: Vec<DelegateTask>,
    #[serde(default)]
    pub state_update: WorkerState,
    #[serde(default, deserialize_with = "null_as_empty_vec")]
    pub completed_tasks: Vec<String>,
    #[serde(default, deserialize_with = "null_as_empty_vec")]
    pub messages: Vec<DirectMessage>,
    #[serde(default)]
    pub r#continue: bool,
}

#[derive(Debug, Serialize, Deserialize)]
struct DelegateTask {
    pub to: String,
    pub task: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct DirectMessage {
    pub to: String,
    pub text: String,
}

pub struct WorkerHarness {
    client: Arc<CollabClient>,
    instance_id: String,
    workdir: PathBuf,
    model: String,
    /// CLI command template for full-tier (agent mode) — {prompt}, {model}, {workdir} placeholders
    cli_template: String,
    /// CLI command template for light-tier (plan/think mode) — if unset, falls back to cli_template
    cli_template_light: Option<String>,
    auto_reply: bool,
    batch_wait_ms: u64,
    message_queue: Arc<Mutex<Vec<Message>>>,
    first_message_time: Arc<Mutex<Option<Instant>>>,
    /// Pipeline: auto-dispatch to these workers on task completion
    hands_off_to: Vec<String>,
    /// All teammates (name + role) for prompt injection
    teammates: Vec<(String, String)>,
}

impl WorkerHarness {
    pub fn new(
        client: CollabClient,
        instance_id: String,
        workdir: PathBuf,
        model: String,
        cli_template: Option<String>,
        cli_template_light: Option<String>,
        auto_reply: bool,
        batch_wait_ms: u64,
        hands_off_to: Vec<String>,
        teammates: Vec<(String, String)>,
    ) -> Self {
        Self {
            client: Arc::new(client),
            instance_id,
            workdir,
            model,
            cli_template: cli_template.unwrap_or_else(|| DEFAULT_CLI_TEMPLATE.to_string()),
            cli_template_light,
            auto_reply,
            batch_wait_ms,
            message_queue: Arc::new(Mutex::new(Vec::new())),
            first_message_time: Arc::new(Mutex::new(None)),
            hands_off_to,
            teammates,
        }
    }

    /// Classify how much context a set of messages needs
    async fn classify_tier(&self, messages: &[Message]) -> PromptTier {
        // Ping/status checks → harness handles directly
        let ping_re = Regex::new(PING_PATTERN).unwrap();
        if messages.iter().all(|m| ping_re.is_match(m.content.trim())) {
            return PromptTier::Harness;
        }

        // Ack loop detection — swallow pure acknowledgments from other workers.
        // These are messages that start with ack-like phrases and carry no new information.
        let ack_re = Regex::new(ACK_START_PATTERN).unwrap();
        let non_self_msgs: Vec<_> = messages.iter().filter(|m| m.sender != self.instance_id).collect();
        if !non_self_msgs.is_empty() && non_self_msgs.iter().all(|m| ack_re.is_match(m.content.trim())) {
            // All external messages are acks — swallow them
            return PromptTier::Harness;
        }

        // Self-messages always need full context — the worker must see its todo list
        // to act on tasks. Light tier omits todos, making these calls useless.
        if messages.iter().any(|m| m.sender == self.instance_id) {
            return PromptTier::Full;
        }

        // Multiple messages batched → full context
        if messages.len() > 1 {
            return PromptTier::Full;
        }

        // Single short external message with no todos → light
        if let Some(msg) = messages.first() {
            if msg.content.len() < 200 {
                return PromptTier::Light;
            }
        }

        PromptTier::Full
    }

    /// Handle harness-tier messages without spawning CLI.
    /// Pings get a status reply; acks get swallowed silently to break ack loops.
    async fn handle_harness_tier(&self, messages: &[Message]) -> Result<()> {
        let ping_re = Regex::new(PING_PATTERN).unwrap();
        let is_ping = messages.iter().all(|m| ping_re.is_match(m.content.trim()));

        if is_ping {
            // Respond to pings with current status
            let state = self.load_state();
            let status = state.status.as_deref().unwrap_or("idle");
            let files_count = state.files_touched.len();
            let pending = state.pending.as_deref().unwrap_or("none");

            let reply = format!(
                "Online. Status: {}. Files touched: {}. Pending: {}",
                status, files_count, pending
            );

            let mut replied = std::collections::HashSet::new();
            for msg in messages {
                if msg.sender != self.instance_id && replied.insert(msg.sender.clone()) {
                    if let Err(e) = self.client.add_message(&msg.sender, &reply, None).await {
                        self.log_error(&format!("Failed to reply to @{}: {}", msg.sender, e));
                    }
                }
            }
            self.log(&format!("harness-handled ping → {}", status));
        } else {
            // Ack messages — swallow silently to break ack loops
            let senders: Vec<_> = messages.iter()
                .filter(|m| m.sender != self.instance_id)
                .map(|m| format!("@{}", m.sender))
                .collect::<std::collections::HashSet<_>>()
                .into_iter().collect();
            self.log(&format!("swallowed {} ack(s) from {} — no CLI spawn",
                messages.len(), senders.join(", ")));
        }
        Ok(())
    }

    pub async fn run(&self) -> Result<()> {
        // Shared status string for dynamic roster presence
        let current_status = Arc::new(Mutex::new(self.get_role()));

        // Spawn batch processor task that wakes on timer
        let queue = self.message_queue.clone();
        let first_time = self.first_message_time.clone();
        let batch_wait_ms = self.batch_wait_ms;
        let client = self.client.clone();
        let instance_id = self.instance_id.clone();
        let workdir = self.workdir.clone();
        let model = self.model.clone();
        let cli_template = self.cli_template.clone();
        let cli_template_light = self.cli_template_light.clone();
        let auto_reply = self.auto_reply;
        let hands_off_to = self.hands_off_to.clone();
        let teammates = self.teammates.clone();
        let batch_status = current_status.clone();

        let max_self_kicks: u32 = 3;

        // Serializes CLI invocations — only one claude process at a time per worker,
        // but the batch loop itself is never blocked waiting for claude to finish.
        let cli_lock: Arc<tokio::sync::Mutex<()>> = Arc::new(tokio::sync::Mutex::new(()));

        // Watchdog: restart batch processor if it ever panics or exits unexpectedly
        let watchdog_queue = queue.clone();
        let watchdog_first_time = first_time.clone();
        let watchdog_client = client.clone();
        let watchdog_instance_id = instance_id.clone();
        let watchdog_workdir = workdir.clone();
        let watchdog_model = model.clone();
        let watchdog_cli_template = cli_template.clone();
        let watchdog_cli_template_light = cli_template_light.clone();
        let watchdog_hands_off_to = hands_off_to.clone();
        let watchdog_teammates = teammates.clone();
        let watchdog_batch_status = current_status.clone();
        let watchdog_cli_lock = cli_lock.clone();

        tokio::spawn(async move {
            loop {
                let handle = {
                    let queue = watchdog_queue.clone();
                    let first_time = watchdog_first_time.clone();
                    let client = watchdog_client.clone();
                    let instance_id = watchdog_instance_id.clone();
                    let workdir = watchdog_workdir.clone();
                    let model = watchdog_model.clone();
                    let cli_template = watchdog_cli_template.clone();
                    let cli_template_light = watchdog_cli_template_light.clone();
                    let hands_off_to = watchdog_hands_off_to.clone();
                    let teammates = watchdog_teammates.clone();
                    let batch_status = watchdog_batch_status.clone();
                    let cli_lock = watchdog_cli_lock.clone();

                    tokio::spawn(async move {
                        let mut consecutive_kicks: u32 = 0;
                        loop {
                sleep(Duration::from_millis(batch_wait_ms)).await;

                // Check if queue has messages and batch window has passed
                let should_process = {
                    let q = queue.lock().await;
                    if q.is_empty() {
                        false
                    } else if let Some(first) = *first_time.lock().await {
                        first.elapsed() >= Duration::from_millis(batch_wait_ms)
                    } else {
                        false
                    }
                };

                if !should_process {
                    continue;
                }

                let mut messages = {
                    let mut q = queue.lock().await;
                    std::mem::take(&mut *q)
                };
                *first_time.lock().await = None;

                // Always strip self-messages before building prompt — never feed them as input
                messages.retain(|m| m.sender != instance_id);

                let has_external = !messages.is_empty();
                let is_self_kick = !has_external;
                if is_self_kick {
                    consecutive_kicks += 1;
                    if consecutive_kicks > max_self_kicks {
                        eprintln!("[{}] self-kick cap reached ({}) — pausing until external message",
                            Utc::now().format("%H:%M:%S UTC"), max_self_kicks);
                        consecutive_kicks = 0;
                        continue;
                    }
                } else {
                    consecutive_kicks = 0;
                }

                let harness = WorkerHarness {
                    client: client.clone(),
                    instance_id: instance_id.clone(),
                    workdir: workdir.clone(),
                    model: model.clone(),
                    cli_template: cli_template.clone(),
                    cli_template_light: cli_template_light.clone(),
                    auto_reply,
                    batch_wait_ms,
                    message_queue: Arc::new(Mutex::new(Vec::new())),
                    first_message_time: Arc::new(Mutex::new(None)),
                    hands_off_to: hands_off_to.clone(),
                    teammates: teammates.clone(),
                };

                let tier = harness.classify_tier(&messages).await;

                match tier {
                    PromptTier::Harness => {
                        // Harness-tier is instant — handle inline, no lock needed
                        if let Err(e) = harness.handle_harness_tier(&messages).await {
                            harness.log_error(&format!("Harness tier failed: {}", e));
                        }
                    }
                    _ => {
                        // Spawn CLI in a background task so the batch loop stays unblocked.
                        // cli_lock serializes invocations — only one claude process at a time.
                        let cli_lock = cli_lock.clone();
                        let batch_status = batch_status.clone();
                        let queue = queue.clone();
                        let full_context = tier == PromptTier::Full;
                        let instance_id_for_log = instance_id.clone();

                        let handle = tokio::spawn(async move {
                            let _guard = cli_lock.lock().await;

                            let worker_continued = match harness.spawn_cli(&messages, full_context).await {
                                Ok(c) => c,
                                Err(e) => {
                                    harness.log_error(&format!("Failed to process {} messages: {}", messages.len(), e));
                                    false
                                }
                            };

                            // Update roster presence from worker state
                            let state = harness.load_state();
                            if let Some(status) = &state.status {
                                *batch_status.lock().await = status.clone();
                            }

                            // Auto-kick if worker has pending todos but didn't self-continue.
                            // Skip if this was an auto-kick (avoid kick→kick→kick loops).
                            let was_auto_kick = is_self_kick && messages.iter().any(|m| m.content.contains("pending tasks"));
                            if !worker_continued && !was_auto_kick {
                                if let Ok(todos) = harness.client.fetch_todos(&harness.instance_id).await {
                                    if !todos.is_empty() {
                                        let q = queue.lock().await;
                                        if q.is_empty() {
                                            drop(q);
                                            let _ = harness.client.add_message(
                                                &harness.instance_id,
                                                &format!("You have {} pending tasks — pick up the next one when ready.", todos.len()),
                                                None
                                            ).await;
                                        }
                                    }
                                }
                            }
                        });

                        // Monitor for panics — log them so they're not silently swallowed
                        tokio::spawn(async move {
                            if let Err(e) = handle.await {
                                if e.is_panic() {
                                    eprintln!("[{}] [{}] CLI task panicked — cli_lock released, batch loop continues",
                                        Utc::now().format("%H:%M:%S UTC"), instance_id_for_log);
                                }
                            }
                        });
                    }
                }
                        } // end loop
                    }) // end inner batch loop task
                }; // end handle assignment

                if let Err(e) = handle.await {
                    if e.is_panic() {
                        eprintln!("[{}] [{}] Batch processor panicked — restarting in 1s",
                            Utc::now().format("%H:%M:%S UTC"), watchdog_instance_id);
                        sleep(Duration::from_secs(1)).await;
                    }
                }
                // If handle returned Ok(()), the loop exited normally — restart immediately
            }
        }); // end watchdog

        // Heartbeat presence every 30s — role updates dynamically from worker state
        let hb_client = self.client.clone();
        let hb_status = current_status.clone();
        tokio::spawn(async move {
            loop {
                let role = hb_status.lock().await.clone();
                let _ = hb_client.heartbeat(Some(&role)).await;
                sleep(Duration::from_secs(30)).await;
            }
        });

        let mut booted = false;
        let mut backoff_secs = 1u64;

        loop {
            let url = format!("{}/events/{}", self.client.base_url, self.instance_id);
            let mut req = self.client.client.get(&url).header("Accept", "text/event-stream");

            if let Some(token) = &self.client.token {
                req = req.header("Authorization", format!("Bearer {}", token));
            }

            match req.send().await {
                Ok(response) if response.status().is_success() => {
                    backoff_secs = 1;
                    self.log(&format!("idle — listening for @{}", self.instance_id));

                    // On connect, fetch any messages that arrived while offline and queue them
                    match self.client.fetch_pending_messages().await {
                        Ok(pending) => {
                            let mut queue = self.message_queue.lock().await;
                            for msg in pending {
                                // Skip self-messages — they're noise
                                if msg.sender != self.instance_id {
                                    queue.push(Message {
                                        sender: msg.sender,
                                        content: msg.content,
                                        hash: msg.hash,
                                        timestamp: msg.timestamp,
                                        recipient: msg.recipient,
                                    });
                                }
                            }
                            if !queue.is_empty() {
                                *self.first_message_time.lock().await = Some(Instant::now());
                                self.log(&format!("queued {} offline message(s)", queue.len()));
                            }
                        }
                        Err(e) => self.log_error(&format!("Failed to fetch pending messages: {}", e)),
                    }

                    // Auto-kick: send boot message AFTER SSE is connected (only once)
                    if !booted {
                        booted = true;
                        if let Err(e) = self.client.add_message(&self.instance_id, "Session start — welcome back. Check your pending tasks and pick up where you left off. Set continue:true to keep working through your task list, or continue:false when you're blocked or done.", None).await {
                            self.log_error(&format!("Failed to send boot message: {}", e));
                        }
                    }

                    let mut buffer = String::new();
                    let mut response = response;

                    loop {
                        match response.chunk().await {
                            Ok(Some(chunk)) => {
                                buffer.push_str(&String::from_utf8_lossy(&chunk));
                                while let Some(end) = buffer.find("\n\n") {
                                    let event_str = buffer[..end].to_string();
                                    buffer.drain(..end + 2);

                                    for line in event_str.lines() {
                                        if let Some(data) = line.strip_prefix("data: ") {
                                            if let Ok(msg) = serde_json::from_str::<Message>(data) {
                                                // Pings get answered immediately — never block on queue
                                                let ping_re = Regex::new(PING_PATTERN).unwrap();
                                                if ping_re.is_match(msg.content.trim()) {
                                                    let _ = self.handle_harness_tier(&[msg]).await;
                                                } else {
                                                    // Queue the message
                                                    let mut queue = self.message_queue.lock().await;
                                                    queue.push(msg);

                                                    // Record first message time for batching
                                                    if queue.len() == 1 {
                                                        *self.first_message_time.lock().await = Some(Instant::now());
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            Ok(None) => {
                                self.log(&format!("connection closed, reconnecting in {}s", backoff_secs));
                                break;
                            }
                            Err(e) => {
                                self.log(&format!("stream error: {} — reconnecting in {}s", e, backoff_secs));
                                break;
                            }
                        }
                    }
                }
                Ok(response) => {
                    self.log(&format!("server error: {} — reconnecting in {}s", response.status(), backoff_secs));
                }
                Err(e) => {
                    self.log(&format!("connection error: {} — reconnecting in {}s", e, backoff_secs));
                }
            }

            sleep(Duration::from_secs(backoff_secs)).await;
            backoff_secs = (backoff_secs * 2).min(30);
        }
    }

    fn is_trivial_reply(&self, content: &str) -> bool {
        Regex::new(TRIVIAL_REPLY_PATTERN)
            .map(|re| re.is_match(content.trim()))
            .unwrap_or(false)
    }

    /// Build the prompt for a CLI invocation.
    /// `full_context`: true = full prompt (teammates, state, todos, full schema), false = light prompt
    async fn build_prompt(&self, messages: &[Message], full_context: bool) -> Result<String> {
        // Format message lines (shared by both tiers)
        let mut msg_lines = String::new();
        for msg in messages {
            let body = if msg.content.len() > 2000 {
                let hash_short = &msg.hash[..7.min(msg.hash.len())];
                let tmp_path = format!("/tmp/collab-msg-{}.md", hash_short);
                let _ = std::fs::write(&tmp_path, &msg.content);
                format!("(see file: {})", tmp_path)
            } else {
                msg.content.clone()
            };
            msg_lines.push_str(&format!("@{}: {}\n", msg.sender, body));
        }

        if !full_context {
            // Light prompt — minimal context
            return Ok(format!(
                "You are @{}. Role: {}

Messages ({}):
{}

Act on the messages above. Use Bash/Read/Write/Edit to do your actual work.

When done, your FINAL output must be ONLY a JSON object (no other text before or after):

{{
  \"response\": \"your reply to the sender (string or null)\",
  \"continue\": false,
  \"state_update\": {{\"status\": \"what you're doing now\"}}
}}

Do NOT run any collab CLI commands. Focus on your actual work.",
                self.instance_id,
                self.get_role(),
                messages.len(),
                msg_lines
            ));
        }

        // Full prompt — complete context
        let state = self.load_state();
        let state_str = serde_json::to_string_pretty(&state).unwrap_or_else(|_| "No previous state.".to_string());

        let todos_str = match self.client.fetch_todos(&self.instance_id).await {
            Ok(todos) if !todos.is_empty() => {
                let mut lines = String::from("Pending tasks assigned to you:\n");
                for todo in &todos {
                    lines.push_str(&format!("  - [{}] (from @{}): {}\n",
                        &todo.hash[..7.min(todo.hash.len())],
                        todo.assigned_by,
                        todo.description
                    ));
                }
                lines
            }
            _ => "No pending tasks.".to_string(),
        };

        let teammates_str = if self.teammates.is_empty() {
            "No teammates configured.".to_string()
        } else {
            let mut lines = String::from("Your team:\n");
            for (name, role) in &self.teammates {
                if name != &self.instance_id {
                    lines.push_str(&format!("  @{} — {}\n", name, role));
                }
            }
            if !self.hands_off_to.is_empty() {
                lines.push_str(&format!("\nWhen you complete a task, your work auto-routes to: {}\n",
                    self.hands_off_to.iter().map(|w| format!("@{}", w)).collect::<Vec<_>>().join(", ")));
            }
            lines
        };

        // Fetch recent message history for conversational context
        let current_hashes: std::collections::HashSet<_> = messages.iter().map(|m| m.hash.as_str()).collect();
        let history_str = match self.client.fetch_history_pub(&self.instance_id).await {
            Ok(history) => {
                let recent: Vec<_> = history.iter()
                    .filter(|m| !current_hashes.contains(m.hash.as_str()))
                    .rev()
                    .take(20)
                    .collect::<Vec<_>>()
                    .into_iter()
                    .rev()
                    .collect();
                if recent.is_empty() {
                    String::new()
                } else {
                    let mut lines = String::from("Recent conversation history (for context — do NOT re-process these, only act on the new messages below):\n");
                    for m in &recent {
                        let content = if m.content.len() > 300 { format!("{}…", &m.content[..300]) } else { m.content.clone() };
                        lines.push_str(&format!("  @{} → @{}: {}\n", m.sender, m.recipient, content));
                    }
                    lines
                }
            }
            Err(_) => String::new(),
        };

        Ok(format!(
            "You are @{}. Role: {}

{}

Previous state:
{}

{}

{}
New messages ({}):
{}

Act on the new messages above. Use Bash/Read/Write/Edit to do your actual work (coding, research, testing).

When done, your FINAL output must be ONLY a JSON object (no other text before or after):

{{
  \"response\": \"your reply to the sender (string or null)\",
  \"delegate\": [{{\"to\": \"@worker\", \"task\": \"description\"}}],
  \"messages\": [{{\"to\": \"@worker\", \"text\": \"message\"}}],
  \"completed_tasks\": [\"hash1\", \"hash2\"],
  \"continue\": false,
  \"state_update\": {{\"key\": \"value\"}}
}}

Fields:
- response: reply back to whoever messaged you
- delegate: assign tasks to ANY instance (teammates, humans, anyone) — creates a persistent todo and pings them. If someone messages you asking for something and you need THEM to act, delegate back to THEM — not to a random teammate. IMPORTANT: do NOT put task assignments in response or messages, those are ephemeral and will be lost on context reset. The task description MUST be self-contained: include all facts, URLs, decisions, and context the recipient needs — they will NOT see the original messages that led to this task
- messages: send messages to any teammate directly — for status updates and context only, NOT for assigning work (optional)
- completed_tasks: task hashes you finished — marks done and routes to downstream workers (optional)
- continue: true to keep working autonomously, false when blocked or done
- state_update: persist state for next invocation. Include \"status\" to update your roster presence

Do NOT run any collab CLI commands. The harness handles all messaging and task delivery. Focus on your actual work.",
            self.instance_id,
            self.get_role(),
            teammates_str,
            state_str,
            todos_str,
            history_str,
            messages.len(),
            msg_lines
        ))
    }

    /// Returns Ok(true) if the worker set continue: true, Ok(false) otherwise.
    async fn spawn_cli(&self, messages: &[Message], full_context: bool) -> Result<bool> {
        let start = std::time::Instant::now();
        let tier = if full_context { PromptTier::Full } else { PromptTier::Light };

        let prompt = self.build_prompt(messages, full_context).await?;

        // Select template: light tier uses cli_template_light if available
        let active_template = if !full_context {
            self.cli_template_light.as_deref().unwrap_or(&self.cli_template)
        } else {
            &self.cli_template
        };

        // Validate: error if template uses {model} but no model is set
        if active_template.contains("{model}") && self.model.is_empty() {
            return Err(anyhow::anyhow!(
                "cli_template uses {{model}} but no model is configured.\n\
                 Set 'model' in workers.yaml or pass --model to collab worker."
            ));
        }

        // Validate: catch unconfigured placeholder from collab init
        if active_template.contains("{agent}") {
            return Err(anyhow::anyhow!(
                "cli_template still contains {{agent}} placeholder — you need to configure it.\n\
                 Edit .collab/workers.json or workers.yaml and replace {{agent}} with your CLI tool.\n\
                 Examples:\n\
                 \x20 claude -p {{prompt}} --model {{model}} --allowedTools Bash,Read,Write,Edit\n\
                 \x20 cursor -p {{prompt}} --model {{model}}\n\
                 \x20 ollama run {{model}} {{prompt}}"
            ));
        }

        // Shell-split the template BEFORE substitution so {prompt} stays as one arg
        let template_parts = shlex::split(active_template).ok_or_else(|| {
            anyhow::anyhow!("Invalid cli_template (bad quoting): {}", active_template)
        })?;
        if template_parts.is_empty() {
            return Err(anyhow::anyhow!("cli_template expanded to empty command"));
        }

        let workdir_str = self.workdir.to_string_lossy();
        let mut parts: Vec<String> = template_parts.iter().map(|part| {
            part.replace("{prompt}", &prompt)
                .replace("{model}", &self.model)
                .replace("{workdir}", &workdir_str)
        }).collect();

        // Detect claude CLI — inject --output-format json to get real token counts and cost
        let is_claude_cli = parts[0].ends_with("claude");
        if is_claude_cli {
            parts.push("--output-format".to_string());
            parts.push("json".to_string());
        }

        let mut cmd = Command::new(&parts[0]);
        cmd.args(&parts[1..])
            .current_dir(&self.workdir);

        // Remove collab env vars from subprocess — harness handles all communication
        cmd.env_remove("COLLAB_INSTANCE");
        cmd.env_remove("COLLAB_SERVER");
        cmd.env_remove("COLLAB_TOKEN");

        let output = match cmd.output()
        {
            Ok(out) => out,
            Err(e) => {
                self.log_error(&format!("Failed to spawn '{}': {}", parts[0], e));
                return Err(e.into());
            }
        };

        // Debug: always dump claude output on failure
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            let stdout = String::from_utf8_lossy(&output.stdout);
            let debug_path = format!("/tmp/collab-debug-{}.txt", self.instance_id);
            let _ = std::fs::write(&debug_path, format!(
                "EXIT: {}\nSTDOUT ({} bytes):\n{}\nSTDERR ({} bytes):\n{}\nPROMPT:\n{}",
                output.status, stdout.len(), stdout, stderr.len(), stderr, prompt
            ));
            let detail = if stderr.trim().is_empty() && stdout.trim().is_empty() {
                format!("(empty output — see {})", debug_path)
            } else if stderr.trim().is_empty() {
                stdout.to_string()
            } else {
                stderr.to_string()
            };
            self.log_error(&format!("CLI exited with status {}: {}", output.status, detail));
            return Err(anyhow::anyhow!("CLI failed: {}", detail));
        }

        let raw_stdout = String::from_utf8_lossy(&output.stdout);

        // For claude CLI: unwrap --output-format json envelope to get real token counts and cost
        let (stdout, real_input_tokens, real_output_tokens, cost_usd) = if is_claude_cli {
            match serde_json::from_str::<serde_json::Value>(&raw_stdout) {
                Ok(v) => {
                    let inner = v.get("result")
                        .and_then(|r| r.as_str())
                        .unwrap_or("")
                        .to_string();
                    let input_tok = v.pointer("/usage/input_tokens").and_then(|t| t.as_u64()).unwrap_or(0)
                        + v.pointer("/usage/cache_creation_input_tokens").and_then(|t| t.as_u64()).unwrap_or(0)
                        + v.pointer("/usage/cache_read_input_tokens").and_then(|t| t.as_u64()).unwrap_or(0);
                    let output_tok = v.pointer("/usage/output_tokens").and_then(|t| t.as_u64()).unwrap_or(0);
                    let cost = v.get("total_cost_usd").and_then(|c| c.as_f64());
                    (std::borrow::Cow::Owned(inner), input_tok, output_tok, cost)
                }
                Err(e) => {
                    self.log_error(&format!("Failed to parse claude JSON envelope: {e}"));
                    (raw_stdout, 0u64, 0u64, None)
                }
            }
        } else {
            (raw_stdout, 0u64, 0u64, None)
        };

        let duration = start.elapsed().as_secs();

        // Parse structured output
        let mut did_continue = false;
        if let Some(collab_output) = self.parse_collab_output(&stdout) {
            // Build set of delegate targets to avoid duplicate messages
            let delegated_to: std::collections::HashSet<String> = collab_output.delegate.iter()
                .map(|t| t.to.trim_start_matches('@').to_string())
                .collect();

            // Send response once per unique sender (skip self and delegate targets)
            let mut replied: std::collections::HashSet<String> = std::collections::HashSet::new();
            if let Some(response) = &collab_output.response {
                if !response.is_empty() {
                    for msg in messages {
                        if msg.sender != self.instance_id
                            && !delegated_to.contains(&msg.sender)
                            && replied.insert(msg.sender.clone())
                        {
                            if let Err(e) = self.client.add_message(&msg.sender, response, None).await {
                                self.log_error(&format!("Failed to send response to @{}: {}", msg.sender, e));
                            }
                        }
                    }
                }
            }

            // Delegate tasks — create todo (todo_add already sends a ping message)
            for task in &collab_output.delegate {
                let to = task.to.trim_start_matches('@');
                if let Err(e) = self.client.todo_add(to, &task.task).await {
                    self.log_error(&format!("Failed to add todo for @{}: {}", to, e));
                }
            }

            // Send direct messages to specific teammates (skip if already delegated to them)
            for dm in &collab_output.messages {
                let to = dm.to.trim_start_matches('@');
                if delegated_to.contains(to) {
                    self.log(&format!("skipped duplicate message to @{} (already delegated)", to));
                    continue;
                }
                if let Err(e) = self.client.add_message(to, &dm.text, None).await {
                    self.log_error(&format!("Failed to message @{}: {}", to, e));
                }
            }

            // Mark completed tasks and auto-route to downstream workers
            // Cap at 5 per call — process first 5, warn about any beyond that
            let max_completions = 5;
            if collab_output.completed_tasks.len() > max_completions {
                self.log_error(&format!(
                    "Worker tried to mark {} tasks done in one call (cap: {}) — processing first {}, ignoring rest",
                    collab_output.completed_tasks.len(), max_completions, max_completions
                ));
            }
            for hash in collab_output.completed_tasks.iter().take(max_completions) {
                let hash_clean = hash.trim();
                if hash_clean.is_empty() { continue; }
                match self.client.todo_done(hash_clean).await {
                    Ok(_) => self.log(&format!("task {} marked done", hash_clean)),
                    Err(e) => self.log_error(&format!("Failed to mark task {} done: {}", hash_clean, e)),
                }
            }

            // Pipeline: auto-dispatch to downstream workers (skip those already replied to)
            if !collab_output.completed_tasks.is_empty() && !self.hands_off_to.is_empty() {
                let summary = collab_output.response.as_deref().unwrap_or("Task completed.");
                let handoff_msg = format!("Completed work from @{}: {}", self.instance_id, summary);
                for downstream in &self.hands_off_to {
                    let to = downstream.trim_start_matches('@');
                    if replied.contains(to) { continue; }
                    if let Err(e) = self.client.add_message(to, &handoff_msg, None).await {
                        self.log_error(&format!("Failed to route to @{}: {}", to, e));
                    } else {
                        self.log(&format!("pipeline → @{}", to));
                    }
                }
            }

            // Self-kick: worker wants to keep going
            did_continue = collab_output.r#continue;
            if collab_output.r#continue {
                let kick_msg = collab_output.response.as_deref().unwrap_or("Continuing...");
                let self_msg = format!("(self-continue) Previous output: {}", kick_msg);
                if let Err(e) = self.client.add_message(&self.instance_id, &self_msg, None).await {
                    self.log_error(&format!("Failed to self-kick: {}", e));
                } else {
                    self.log("continuing → self-kick");
                }
            }

            // Update state
            self.save_state(&collab_output.state_update);
        } else {
            // Fallback: no markers found
            let raw = stdout.trim().to_string();
            if !raw.is_empty() {
                // If it looks like a failed JSON parse (contains "response" key), don't send raw JSON
                if raw.contains("\"response\"") && raw.contains("{") {
                    self.log_error(&format!("JSON parse failed — output looks like structured JSON but couldn't be parsed. Not sending raw JSON to team."));
                } else {
                    // Plain text response — send it
                    self.log(&format!("no markers — sending raw response"));
                    for msg in messages {
                        if msg.sender != self.instance_id {
                            if let Err(e) = self.client.add_message(&msg.sender, &raw, None).await {
                                self.log_error(&format!("Failed to send response to @{}: {}", msg.sender, e));
                            }
                        }
                    }
                }
            }
        }

        // Token usage — real counts from claude JSON envelope, estimates for other CLIs
        let (log_input_tokens, log_output_tokens) = if is_claude_cli {
            (real_input_tokens, real_output_tokens)
        } else {
            (prompt.len() as u64 / 4, stdout.len() as u64 / 4)
        };
        let cost_str = cost_usd.map(|c| format!(", ${:.4}", c)).unwrap_or_default();
        self.log(&format!("done — {}s, {}+{} tokens{}", duration, log_input_tokens, log_output_tokens, cost_str));

        // Append to usage log
        let log_line = format!("{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n",
            Utc::now().format("%Y-%m-%dT%H:%M:%SZ"),
            self.instance_id,
            duration,
            log_input_tokens,
            log_output_tokens,
            self.cli_template.split_whitespace().next().unwrap_or("unknown"),
            tier,
            cost_usd.map(|c| format!("{:.6}", c)).unwrap_or_default()
        );
        let log_path = self.workdir.join("../../.collab/usage.log");
        let _ = std::fs::OpenOptions::new().create(true).append(true).open(&log_path)
            .and_then(|mut f| std::io::Write::write_all(&mut f, log_line.as_bytes()));

        // Clean up temp files from this invocation
        for msg in messages {
            if msg.content.len() > 2000 {
                let hash_short = &msg.hash[..7.min(msg.hash.len())];
                let tmp_path = format!("/tmp/collab-msg-{}.md", hash_short);
                let _ = std::fs::remove_file(&tmp_path);
            }
        }
        // Remove debug dump from previous failure (if this call succeeded)
        let debug_path = format!("/tmp/collab-debug-{}.txt", self.instance_id);
        let _ = std::fs::remove_file(&debug_path);

        Ok(did_continue)
    }

    fn parse_collab_output(&self, output: &str) -> Option<CollabOutput> {
        parse_collab_output(output)
    }

    fn load_state(&self) -> WorkerState {
        let path = self.workdir.join(".worker-state.json");
        if let Ok(contents) = std::fs::read_to_string(&path) {
            serde_json::from_str(&contents).unwrap_or_default()
        } else {
            WorkerState::default()
        }
    }

    fn save_state(&self, state: &WorkerState) {
        let path = self.workdir.join(".worker-state.json");
        if let Ok(json) = serde_json::to_string_pretty(state) {
            let _ = std::fs::write(&path, json);
        }
    }

    fn get_role(&self) -> String {
        // Try AGENT.md first, fall back to CLAUDE.md for existing setups
        for filename in &["AGENT.md", "CLAUDE.md"] {
            let path = self.workdir.join(filename);
            if let Ok(contents) = std::fs::read_to_string(&path) {
                for line in contents.lines() {
                    if line.contains("Your role:") {
                        if let Some(rest) = line.split("Your role:").nth(1) {
                            return rest.trim().trim_end_matches('*').to_string();
                        }
                    }
                }
            }
        }
        "Worker".to_string()
    }

    fn log(&self, msg: &str) {
        let now = Utc::now().format("%H:%M:%S UTC");
        println!("[{}] {}", now, msg);
    }

    fn log_error(&self, msg: &str) {
        let now = Utc::now().format("%Y-%m-%d %H:%M:%S UTC");
        let log_entry = format!("[{}] @{}: {}\n", now, self.instance_id, msg);

        // Append to error log file
        use std::io::Write;
        if let Ok(mut file) = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open("/tmp/collab-worker-errors.log")
        {
            let _ = file.write_all(log_entry.as_bytes());
        }

        // Also print to stderr
        eprintln!("{}", log_entry);
    }
}

fn parse_collab_output(output: &str) -> Option<CollabOutput> {
    // Strip markdown code fences if present
    let cleaned = if output.contains("```") {
        let mut result = String::new();
        let mut in_fence = false;
        for line in output.lines() {
            if line.trim().starts_with("```") {
                in_fence = !in_fence;
                if !in_fence { continue; } // closing fence
                continue; // opening fence (```json etc)
            }
            if in_fence {
                result.push_str(line);
                result.push('\n');
            }
        }
        if result.trim().is_empty() { output.to_string() } else { result }
    } else {
        output.to_string()
    };

    // Try to find valid CollabOutput JSON — scan from the end backwards
    let bytes = cleaned.as_bytes();
    let mut depth = 0i32;
    let mut end_pos = None;

    for i in (0..bytes.len()).rev() {
        if bytes[i] == b'}' {
            if depth == 0 { end_pos = Some(i); }
            depth += 1;
        } else if bytes[i] == b'{' {
            depth -= 1;
            if depth == 0 {
                if let Some(end) = end_pos {
                    let json_str = &cleaned[i..=end];
                    if let Ok(parsed) = serde_json::from_str::<CollabOutput>(json_str) {
                        return Some(parsed);
                    }
                }
            }
        }
    }
    None
}

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

    #[test]
    fn parse_handles_null_fields() {
        let input = r#"{"response": "hi", "delegate": null, "messages": null, "completed_tasks": null, "continue": false, "state_update": {}}"#;
        let result = parse_collab_output(input).expect("should parse");
        assert_eq!(result.response.as_deref(), Some("hi"));
        assert!(result.delegate.is_empty());
        assert!(result.messages.is_empty());
        assert!(result.completed_tasks.is_empty());
        assert!(!result.r#continue);
    }

    #[test]
    fn parse_handles_missing_fields() {
        let input = r#"{"response": "hi"}"#;
        let result = parse_collab_output(input).expect("should parse");
        assert_eq!(result.response.as_deref(), Some("hi"));
        assert!(result.delegate.is_empty());
        assert!(result.messages.is_empty());
        assert!(result.completed_tasks.is_empty());
    }

    #[test]
    fn parse_handles_markdown_fences() {
        let input = "Here is the output:\n\n```json\n{\"response\": \"done\", \"continue\": false}\n```\n";
        let result = parse_collab_output(input).expect("should parse");
        assert_eq!(result.response.as_deref(), Some("done"));
    }

    #[test]
    fn parse_handles_text_before_json() {
        let input = "Let me check...\n\n{\"response\": \"found it\", \"continue\": false}";
        let result = parse_collab_output(input).expect("should parse");
        assert_eq!(result.response.as_deref(), Some("found it"));
    }

    #[test]
    fn parse_handles_text_after_json() {
        let input = "{\"response\": \"all good\", \"continue\": false}\n\nHope that helps!";
        let result = parse_collab_output(input).expect("should parse");
        assert_eq!(result.response.as_deref(), Some("all good"));
    }

    #[test]
    fn parse_handles_nested_json_in_state() {
        let input = r#"{"response": "ok", "state_update": {"status": "working", "files_touched": ["a.rs", "b.rs"]}, "continue": false}"#;
        let result = parse_collab_output(input).expect("should parse");
        assert_eq!(result.response.as_deref(), Some("ok"));
        assert_eq!(result.state_update.status.as_deref(), Some("working"));
        assert_eq!(result.state_update.files_touched, vec!["a.rs", "b.rs"]);
    }

    #[test]
    fn parse_handles_empty_string() {
        assert!(parse_collab_output("").is_none());
    }

    #[test]
    fn parse_handles_no_json() {
        assert!(parse_collab_output("Just some plain text response").is_none());
    }

    #[test]
    fn parse_handles_invalid_json() {
        assert!(parse_collab_output("{response: broken}").is_none());
    }

    #[test]
    fn parse_handles_continue_true() {
        let input = r#"{"response": null, "continue": true}"#;
        let result = parse_collab_output(input).expect("should parse");
        assert!(result.response.is_none());
        assert!(result.r#continue);
    }

    #[test]
    fn parse_handles_messages_field() {
        let input = r#"{"response": "sent", "messages": [{"to": "@frontend", "text": "API ready"}], "continue": false}"#;
        let result = parse_collab_output(input).expect("should parse");
        assert_eq!(result.messages.len(), 1);
        assert_eq!(result.messages[0].to, "@frontend");
        assert_eq!(result.messages[0].text, "API ready");
    }

    #[test]
    fn parse_handles_delegate_field() {
        let input = r#"{"response": "delegated", "delegate": [{"to": "@backend", "task": "fix the bug"}], "continue": false}"#;
        let result = parse_collab_output(input).expect("should parse");
        assert_eq!(result.delegate.len(), 1);
        assert_eq!(result.delegate[0].to, "@backend");
        assert_eq!(result.delegate[0].task, "fix the bug");
    }

    #[test]
    fn parse_handles_completed_tasks() {
        let input = r#"{"response": "done", "completed_tasks": ["abc123", "def456"], "continue": false}"#;
        let result = parse_collab_output(input).expect("should parse");
        assert_eq!(result.completed_tasks, vec!["abc123", "def456"]);
    }

    #[test]
    fn parse_extracts_status_from_state() {
        let input = r#"{"response": "ok", "state_update": {"status": "building UI"}, "continue": false}"#;
        let result = parse_collab_output(input).expect("should parse");
        assert_eq!(result.state_update.status.as_deref(), Some("building UI"));
    }

    #[test]
    fn parse_handles_extra_unknown_fields() {
        let input = r#"{"response": "ok", "unknown_field": 42, "another": "value", "continue": false}"#;
        let result = parse_collab_output(input).expect("should parse");
        assert_eq!(result.response.as_deref(), Some("ok"));
    }

    #[test]
    fn ack_pattern_matches_acknowledged() {
        let re = Regex::new(ACK_START_PATTERN).unwrap();
        assert!(re.is_match("Acknowledged — gate freeze holds"));
        assert!(re.is_match("Ack — freeze unchanged"));
        assert!(re.is_match("Aligned on gate freeze"));
        assert!(re.is_match("Standing by for joint build"));
        assert!(re.is_match("Same gate on my side"));
        assert!(re.is_match("Holding research/dataset churn per gate"));
        assert!(re.is_match("Received — holding Option A"));
        assert!(re.is_match("Noted; unchanged until PM records"));
        assert!(re.is_match("Gate freeze respected — no validator-driven spec churn"));
        assert!(re.is_match("Freeze holds — standing by"));
    }

    #[test]
    fn ack_pattern_matches_with_at_mentions() {
        let re = Regex::new(ACK_START_PATTERN).unwrap();
        assert!(re.is_match("@researcher Acknowledged — holding"));
        assert!(re.is_match("@project-manager @validator Acknowledged freeze"));
        assert!(re.is_match("@database Aligned: holding research churn"));
    }

    #[test]
    fn ack_pattern_does_not_match_real_messages() {
        let re = Regex::new(ACK_START_PATTERN).unwrap();
        assert!(!re.is_match("Fixed the auth redirect issue"));
        assert!(!re.is_match("New dataset ready for integration"));
        assert!(!re.is_match("Found bug in payment processor"));
        assert!(!re.is_match("Please review the schema changes"));
        assert!(!re.is_match("Write access is unblocked on my side"));
        assert!(!re.is_match("Completed work from @builder: API ready"));
    }
}