mahbot 0.7.3

An autonomous agentic engineering system that manages software development through role separation, subagents, and deterministic diagnostics.
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
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
//! Ticket management tools for the Manager role.
//!
//! These tools allow the Manager to create tickets on the board,
//! update their phase, list them, and inspect individual tickets.

use crate::Role;
use crate::Workspace;
use crate::pipeline::board::store as board_store;
use crate::pipeline::board::{TicketParams, TicketPhase};
use crate::tools::Tool;
use anyhow::Result;
use async_trait::async_trait;
use serde_json::json;
use std::fmt::Write;

const GET_TICKET_DESC_MAX: usize = 500;
const GET_TICKET_COMMENT_MAX: usize = 200;
const GET_TICKET_LAST_N_FULL: usize = 3;

// ── Ticket ID resolution ─────────────────────────────────────────

/// Resolve a raw ticket ID argument against the tool's bound workspace.
///
/// Accepts either a bare number (resolved to `{ws}-{number}`) or the fully
/// prefixed form for the bound workspace. Prefixed IDs from any other
/// workspace are rejected with a clear error.
fn resolve_ticket_id(ws_name: &str, raw: &str) -> Result<String> {
    let (prefix, id) = parse_ticket_id(raw)?;
    match prefix {
        None => Ok(format!("{ws_name}-{id}")),
        Some(p) if p == ws_name => Ok(id.to_string()),
        Some(_) => anyhow::bail!(
            "Ticket '{id}' belongs to a different workspace — ticket tools only manage \
             tickets from workspace '{ws_name}'"
        ),
    }
}

/// Like [`resolve_ticket_id`], but a well-formed prefixed ID from any
/// workspace is taken as written. Used by the read-only [`GetTicketTool`]:
/// the archive search spans the installation, and a hit is useless if the
/// ticket cannot be opened wherever it was filed.
fn resolve_any_ticket_id(ws_name: &str, raw: &str) -> Result<String> {
    let (prefix, id) = parse_ticket_id(raw)?;
    match prefix {
        None => Ok(format!("{ws_name}-{id}")),
        Some(_) => Ok(id.to_string()),
    }
}

/// Split `raw` into `(workspace prefix, id)`: the prefix is `None` for a bare
/// number — which the caller resolves against its bound workspace — and
/// `Some(prefix)` for a well-formed `{prefix}-{seq}` argument, in which case
/// `id` is `raw` trimmed, i.e. the full prefixed ticket ID. The prefix shape
/// mirrors `workspace::validate_name` ([a-zA-Z_]+ starting with a letter).
fn parse_ticket_id(raw: &str) -> Result<(Option<&str>, &str)> {
    let id = raw.trim();
    anyhow::ensure!(!id.is_empty(), "Ticket ID must not be empty");
    let seq_ok = |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit());
    if seq_ok(id) {
        return Ok((None, id));
    }
    if let Some((prefix, seq)) = id.rsplit_once('-') {
        let prefix_ok = prefix.starts_with(|c: char| c.is_ascii_alphabetic())
            && prefix.chars().all(|c| c.is_ascii_alphabetic() || c == '_');
        if prefix_ok && seq_ok(seq) {
            return Ok((Some(prefix), id));
        }
    }
    anyhow::bail!("Invalid ticket ID '{id}' — expected a bare number or '<workspace>-<number>'")
}

// ── CreateTicketTool ─────────────────────────────────────────────

/// Tool for creating tickets. The `reporter` field is set at construction
/// time based on which role is using the tool — invisible to the agent,
/// no parameter to pass. Bound to a single workspace at construction time.
pub struct CreateTicketTool {
    reporter: String,
    ws_name: String,
}

impl CreateTicketTool {
    pub fn new(reporter: impl Into<String>, ws: &Workspace) -> Self {
        Self {
            reporter: reporter.into(),
            ws_name: ws.name.clone(),
        }
    }

    /// Build a [`TicketParams`] from the shared fields used by both creation branches.
    fn build_params(
        &self,
        title: &str,
        description: &str,
        prerequisites: &[String],
        embedding_bytes: Option<Vec<u8>>,
        priority: i64,
    ) -> TicketParams {
        TicketParams {
            title: title.to_string(),
            description: description.to_string(),
            workspace_name: self.ws_name.clone(),
            phase: TicketPhase::Backlog,
            prerequisites: prerequisites.to_vec(),
            reporter: self.reporter.clone(),
            embedding: embedding_bytes,
            priority,
        }
    }
}

#[async_trait]
impl Tool for CreateTicketTool {
    fn name(&self) -> &'static str {
        "create_ticket"
    }

    fn parameters_schema(&self) -> serde_json::Value {
        let mut props = serde_json::Map::new();
        props.insert(
            "title".into(),
            json!({
                "type": "string",
                "description": "Short title/summary of the task"
            }),
        );
        props.insert(
            "description".into(),
            json!({
                "type": "string",
                "description": "Detailed description of the task"
            }),
        );
        props.insert(
            "prerequisites".into(),
            json!({
                "type": "array",
                "description": "Optional list of ticket IDs that must be completed (done, archived, or cancelled) before this ticket can be claimed",
                "items": {
                    "type": "string"
                }
            }),
        );
        props.insert(
            "supersede".into(),
            json!({
                "type": "string",
                "description": "Optional ticket ID to supersede — atomically cancels the old ticket, creates this one as its replacement, and rewires any dependents to point to the new ID"
            }),
        );

        // Only Manager sees the priority parameter — Maintainer always uses
        // a hardcoded value and must not have it in the schema.
        if self.reporter == "manager" {
            props.insert(
                "priority".into(),
                json!({
                    "type": "integer",
                    "description": "Priority level: 0 = highest urgency, 1 (default), 2, 3, ... Higher numbers = lower priority"
                }),
            );
        }

        super::tool_params_schema(&serde_json::Value::Object(props), &["title", "description"])
    }

    async fn execute(&self, _ws: &Workspace, args: serde_json::Value) -> Result<String> {
        let title = super::get_str(&args, "title")?;
        let description = super::get_str(&args, "description")?;

        let prerequisites: Vec<String> = match args.get("prerequisites") {
            Some(serde_json::Value::Array(arr)) => arr
                .iter()
                .map(|v| {
                    let s = v.as_str().ok_or_else(|| {
                        anyhow::anyhow!("prerequisites must contain ticket ID strings, got {v}")
                    })?;
                    resolve_ticket_id(&self.ws_name, s)
                })
                .collect::<Result<_>>()?,
            Some(_) => {
                anyhow::bail!("prerequisites must be an array of ticket ID strings");
            }
            None => Vec::new(),
        };

        let supersede_id: Option<String> = match args.get("supersede") {
            Some(serde_json::Value::String(s)) => Some(resolve_ticket_id(&self.ws_name, s)?),
            Some(_) => anyhow::bail!("supersede must be a ticket ID string"),
            None => None,
        };

        // Priority: Manager can set explicitly; otherwise inherit from the
        // superseded ticket (if superseding) or use the role default.
        let explicit_priority: Option<i64> = if self.reporter == "manager" {
            args.get("priority").and_then(serde_json::Value::as_i64)
        } else {
            None
        };

        let store = board_store();
        let embedding_bytes: Option<Vec<u8>> =
            crate::embedder::embed_document(title).map(|v| crate::vector::vec_to_bytes(&v));

        let prereq_note = if prerequisites.is_empty() {
            String::new()
        } else {
            format!(" with prerequisites: {}", prerequisites.join(", "))
        };

        let priority: i64 = match (&supersede_id, explicit_priority) {
            (Some(supersede_id), None) => {
                // No explicit priority — inherit from the superseded ticket.
                // Priority is immutable after ticket creation, so reading it
                // outside the supersede transaction has no TOCTOU race.
                match store.get_ticket_priority(supersede_id).await? {
                    Some(p) => p,
                    None => anyhow::bail!(
                        "Superseded ticket {supersede_id} not found when reading priority",
                    ),
                }
            }
            (_, Some(p)) => p,
            // No explicit priority: maintainer gets 3, while the manager
            // (and every other reporter) intentionally defaults to 1.
            (None, None) => {
                if self.reporter == "maintainer" {
                    3
                } else {
                    1
                }
            }
        };

        let params = self.build_params(
            title,
            description,
            &prerequisites,
            embedding_bytes,
            priority,
        );
        if let Some(supersede_id) = supersede_id {
            guard_not_pipeline_occupied(store, &supersede_id).await?;

            let id = store
                .supersede_and_create(&supersede_id, &params, &self.reporter)
                .await?;
            Ok(format!(
                "Superseded {supersede_id} → created ticket {id}: {title}{prereq_note}"
            ))
        } else {
            let id = store.create_ticket(&params).await?;
            Ok(format!("Created ticket {id}: {title}{prereq_note}"))
        }
    }
}

// ── UpdateTicketTool ─────────────────────────────────────────────

pub struct UpdateTicketTool {
    ws_name: String,
}

impl UpdateTicketTool {
    pub fn new(ws: &Workspace) -> Self {
        Self {
            ws_name: ws.name.clone(),
        }
    }
}

#[async_trait]
impl Tool for UpdateTicketTool {
    fn name(&self) -> &'static str {
        "update_ticket"
    }

    fn parameters_schema(&self) -> serde_json::Value {
        super::tool_params_schema(
            &json!({
                "ticket_id": {
                    "type": "string",
                    "description": "The ticket id"
                },
                "phase": {
                    "type": "string",
                    "description": "New phase for the ticket. Valid manual transitions: backlog (return to queue), planning (paused state awaiting further decision whether to proceed with the ticket or cancel it), queued (send to engineer), cancelled (abandon), failed (mark unsuccessful), done (mark complete). Do NOT manually set other pipeline-managed phases (analysis, in_development, verification, in_sanitation) — the board poller handles these automatically and manual transitions will interfere with running agents."
                }
            }),
            &["ticket_id", "phase"],
        )
    }

    async fn execute(&self, _ws: &Workspace, args: serde_json::Value) -> Result<String> {
        let ticket_id = resolve_ticket_id(&self.ws_name, super::get_str(&args, "ticket_id")?)?;
        let new_phase = super::get_str(&args, "phase")?;

        let parsed_phase = new_phase.parse::<TicketPhase>()?;

        let store = board_store();

        // Guard: refuse to update a ticket that is in a pipeline-occupied phase.
        guard_not_pipeline_occupied(store, &ticket_id).await?;

        store
            .transition_to(&ticket_id, None, parsed_phase, Role::Manager.as_str())
            .await?;

        Ok(format!("Ticket {ticket_id} phase updated to '{new_phase}'"))
    }
}

// ── ListTicketsTool ─────────────────────────────────────────────

pub struct ListTicketsTool {
    ws_name: String,
}

impl ListTicketsTool {
    pub fn new(ws: &Workspace) -> Self {
        Self {
            ws_name: ws.name.clone(),
        }
    }
}

#[async_trait]
impl Tool for ListTicketsTool {
    fn name(&self) -> &'static str {
        "list_tickets"
    }

    fn parameters_schema(&self) -> serde_json::Value {
        super::tool_params_schema(
            &json!({
                "phase": {
                    "type": "string",
                    "description": "Optional phase filter (e.g. 'queued', 'in_development', 'done', 'cancelled'). When omitted, 'done' and 'cancelled' tickets are excluded — use an explicit phase filter to include them. Use 'search_archived_tickets' to find archived tickets."
                }
            }),
            &[],
        )
    }

    fn side_effects(&self) -> bool {
        false // read-only board query
    }

    async fn execute(&self, _ws: &Workspace, args: serde_json::Value) -> Result<String> {
        let raw_phase = super::get_opt_str(&args, "phase");

        // "archived" is no longer a phase — redirect to search_archived_tickets
        if let Some(s) = raw_phase
            && s.eq_ignore_ascii_case("archived")
        {
            anyhow::bail!(
                "Archived tickets are not accessible via 'list_tickets'. \
                     Use 'search_archived_tickets' to search the archive."
            );
        }

        let phase_filter = match raw_phase {
            Some(s) => {
                let parsed = s.parse::<TicketPhase>()?;
                Some(parsed)
            }
            None => None,
        };

        let store = board_store();

        let mut tickets = store
            .list_all_tickets(Some(&self.ws_name), phase_filter)
            .await?;

        // Default: exclude unblocking phases. When an explicit phase filter
        // is provided, respect it as-is.
        if phase_filter.is_none() {
            tickets.retain(|t| !t.phase.is_unblocking());
        }

        if tickets.is_empty() {
            return Ok("No tickets found.".to_string());
        }

        let mut output = String::from("Tickets:\n");
        for t in &tickets {
            let _ = writeln!(output, "{}", t.short_display());
        }

        Ok(output)
    }
}

// ── GetTicketTool ───────────────────────────────────────────────

/// Read one ticket by ID. Unlike the write tools it is not confined to its
/// bound workspace: a fully prefixed ID from any workspace is accepted, while a
/// bare number still resolves against the bound workspace.
///
/// The archive search spans the installation, so `get_ticket` must be able to
/// open its hits wherever they were filed.
pub struct GetTicketTool {
    reporter: String,
    ws_name: String,
}

impl GetTicketTool {
    pub fn new(reporter: impl Into<String>, ws: &Workspace) -> Self {
        Self {
            reporter: reporter.into(),
            ws_name: ws.name.clone(),
        }
    }
}

#[async_trait]
impl Tool for GetTicketTool {
    fn name(&self) -> &'static str {
        "get_ticket"
    }

    fn parameters_schema(&self) -> serde_json::Value {
        super::tool_params_schema(
            &json!({
                "ticket_id": {
                    "type": "string",
                    "description": "The ticket id"
                },
                "full": {
                    "type": "boolean",
                    "description": "Return the complete un-truncated ticket. Default false."
                }
            }),
            &["ticket_id"],
        )
    }

    fn side_effects(&self) -> bool {
        false // read-only board query
    }

    fn preserve_full_output(&self) -> bool {
        // Ticket content (descriptions, comments) can exceed the 5 KB
        // budget — the LLM needs it in full.
        true
    }

    async fn execute(&self, _ws: &Workspace, args: serde_json::Value) -> Result<String> {
        let ticket_id = resolve_any_ticket_id(&self.ws_name, super::get_str(&args, "ticket_id")?)?;
        let full = super::get_bool(&args, "full", false)?;

        let store = board_store();

        let Some(ticket) = store.get_ticket(&ticket_id).await? else {
            anyhow::bail!("Ticket {ticket_id} not found");
        };

        if full || ticket.reporter != self.reporter {
            Ok(ticket.detailed_display())
        } else {
            Ok(ticket.detailed_display_limited(
                GET_TICKET_DESC_MAX,
                GET_TICKET_COMMENT_MAX,
                GET_TICKET_LAST_N_FULL,
            ))
        }
    }
}

// ── AddCommentTool ──────────────────────────────────────────────

pub struct AddCommentTool {
    ws_name: String,
}

impl AddCommentTool {
    pub fn new(ws: &Workspace) -> Self {
        Self {
            ws_name: ws.name.clone(),
        }
    }
}

#[async_trait]
impl Tool for AddCommentTool {
    fn name(&self) -> &'static str {
        "add_comment"
    }

    fn parameters_schema(&self) -> serde_json::Value {
        super::tool_params_schema(
            &json!({
                "ticket_id": {
                    "type": "string",
                    "description": "The ID of the ticket to comment on"
                },
                "content": {
                    "type": "string",
                    "description": "The comment content"
                }
            }),
            &["ticket_id", "content"],
        )
    }

    async fn execute(&self, _ws: &Workspace, args: serde_json::Value) -> Result<String> {
        let ticket_id = resolve_ticket_id(&self.ws_name, super::get_str(&args, "ticket_id")?)?;
        let content = super::get_str(&args, "content")?;

        let store = board_store();

        store
            .add_comment(&ticket_id, Role::Manager.as_str(), content)
            .await?;

        Ok(format!("Comment added to ticket {ticket_id}"))
    }
}

/// Guard: refuse to proceed if the ticket is in a pipeline-occupied phase.
/// The create-ticket tool (when superseding an existing ticket) and the
/// update-ticket tool use this to prevent modifications to in-flight tickets
/// during automated pipeline processing. `add_comment` deliberately bypasses
/// it — comments are allowed mid-pipeline and are delivered to running agents
/// as soft deferred messages.
async fn guard_not_pipeline_occupied(
    store: &crate::pipeline::board::BoardStore,
    ticket_id: &str,
) -> Result<()> {
    if let Some(current_phase) = store.get_ticket_phase(ticket_id).await?
        && current_phase.is_pipeline_occupied()
    {
        anyhow::bail!(
            "Ticket {ticket_id} is currently in phase '{current_phase}' — \
                 in-flight tickets cannot be modified through automated tools. \
                 Use the GUI to cancel or manage this ticket manually.",
        );
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::util::test::TicketBuilder;
    use crate::util::test::expect_ticket;
    use crate::util::test::make_ticket;
    use crate::workspace::test_ws;
    use serde_json::json;

    fn comment(role: &str, content: &str, ts: &str) -> crate::pipeline::board::TicketComment {
        crate::pipeline::board::TicketComment {
            role: role.to_string(),
            content: content.to_string(),
            created_at: ts.to_string(),
        }
    }

    #[tokio::test]
    async fn test_create_ticket_tool() {
        crate::util::test::init_test_stores().await;

        let ws = test_ws("/tmp/test_ws");
        let tool = CreateTicketTool::new("test", &ws);
        let args = json!({
            "title": "Test ticket",
            "description": "A test description",
        });
        let result = tool.execute(&ws, args).await.expect("execute");
        assert!(
            result.contains("Test ticket"),
            "Output should contain title"
        );
    }

    #[tokio::test]
    async fn test_create_ticket_rejects_non_string_prerequisite() {
        crate::util::test::init_test_stores().await;

        let ws = test_ws("/tmp/test_ws");
        let tool = CreateTicketTool::new("test", &ws);
        let args = json!({
            "title": "Test",
            "description": "desc",
            "prerequisites": [123],
        });
        let err = tool.execute(&ws, args).await.unwrap_err();
        assert!(
            format!("{err}").contains("ticket ID strings"),
            "non-string prerequisite should be rejected: {err}"
        );
    }

    #[tokio::test]
    #[serial_test::serial(reset_inflight)] // shared global board — a concurrent boot reset would clobber the Verification fixture
    async fn test_update_ticket_tool() {
        crate::util::test::init_test_stores().await;

        let store = board_store();
        let ws = test_ws("/ws");
        let id = make_ticket(
            store,
            &ws,
            "Test",
            crate::pipeline::board::TicketPhase::Backlog,
        )
        .await;

        let tool = UpdateTicketTool::new(&ws);
        let args = json!({
            "ticket_id": id,
            "phase": "verification"
        });
        tool.execute(&ws, args).await.expect("execute");
        let ticket = store
            .get_ticket(&id)
            .await
            .expect("get_ticket")
            .expect("ticket exists");
        assert_eq!(ticket.phase, TicketPhase::Verification);
    }

    #[tokio::test]
    async fn test_list_tickets_tool() {
        crate::util::test::init_test_stores().await;

        let store = board_store();
        let ws = test_ws("/tmp_ws");

        let _id_a = make_ticket(
            store,
            &ws,
            "A",
            crate::pipeline::board::TicketPhase::Backlog,
        )
        .await;
        let id_b = make_ticket(
            store,
            &ws,
            "B",
            crate::pipeline::board::TicketPhase::Backlog,
        )
        .await;
        let id_c = make_ticket(
            store,
            &ws,
            "C",
            crate::pipeline::board::TicketPhase::Backlog,
        )
        .await;
        // Transition C to 'done' and B to 'cancelled'
        store
            .transition_to(&id_c, Some(TicketPhase::Backlog), TicketPhase::Done, "test")
            .await
            .expect("transition to done");
        store
            .transition_to(
                &id_b,
                Some(TicketPhase::Backlog),
                TicketPhase::Cancelled,
                "test",
            )
            .await
            .expect("transition to cancelled");

        let tool = ListTicketsTool::new(&ws);

        // Default (no filter): excludes done and cancelled — only A should appear
        let args = json!({});
        let result = tool.execute(&ws, args).await.expect("execute");
        assert!(
            result.contains('A'),
            "Default listing should include active ticket A"
        );
        assert!(
            !result.contains('B'),
            "Default listing should exclude cancelled ticket B"
        );
        assert!(
            !result.contains('C'),
            "Default listing should exclude done ticket C"
        );

        // Explicit filter for 'done': includes done tickets
        let args = json!({"phase": "done"});
        let result = tool.execute(&ws, args).await.expect("execute");
        assert!(
            result.contains('C'),
            "Explicit 'done' filter should include done ticket C"
        );
        assert!(
            !result.contains('A'),
            "Explicit 'done' filter should exclude active ticket A"
        );

        // Explicit filter for 'cancelled': includes cancelled tickets
        let args = json!({"phase": "cancelled"});
        let result = tool.execute(&ws, args).await.expect("execute");
        assert!(
            result.contains('B'),
            "Explicit 'cancelled' filter should include cancelled ticket B"
        );

        // Filter for an active phase that ticket A is actually in
        let args = json!({"phase": "backlog"});
        let result = tool.execute(&ws, args).await.expect("execute");
        assert!(
            result.contains('A'),
            "Explicit 'backlog' filter should include ticket A"
        );
    }

    #[tokio::test]
    async fn test_list_tickets_invalid_phase() {
        crate::util::test::init_test_stores().await;

        let ws = crate::workspace::test_ws("/tmp");
        let tool = ListTicketsTool::new(&ws);
        let args = json!({"phase": "bogus_phase"});
        let result = tool.execute(&ws, args).await;
        assert!(result.is_err(), "Invalid phase should fail");
    }

    #[tokio::test]
    async fn test_get_ticket_tool() {
        crate::util::test::init_test_stores().await;

        let store = board_store();
        let ws = test_ws("/ws");
        let id = TicketBuilder::new(store, &ws)
            .title("GetTest")
            .desc("get me")
            .create()
            .await
            .expect("create");

        // Fetch the ticket to compare against detailed_display()
        let ticket = crate::util::test::expect_ticket(store, &id).await;
        let expected = ticket.detailed_display();

        let tool = GetTicketTool::new("manager", &ws);
        let args = json!({"ticket_id": id, "full": true});
        let result = tool.execute(&ws, args).await.expect("execute");
        assert_eq!(
            result, expected,
            "GetTicketTool output must match Ticket::detailed_display()"
        );
    }

    #[tokio::test]
    async fn test_add_comment_tool() {
        crate::util::test::init_test_stores().await;

        let store = board_store();
        let ws = test_ws("/ws");
        let id = TicketBuilder::new(store, &ws)
            .title("CommentTest")
            .desc("add a comment")
            .create()
            .await
            .expect("create");

        let tool = AddCommentTool::new(&ws);
        let args = json!({
            "ticket_id": id,
            "content": "This is a test comment",
        });
        let result = tool.execute(&ws, args).await.expect("execute");
        assert!(result.contains(&id), "Output should contain ticket id");

        // Verify comment was stored
        let ticket = crate::util::test::expect_ticket(store, &id).await;
        let comment = ticket.comments.last().expect("at least one comment");
        assert_eq!(comment.role, Role::Manager.as_str());
        assert_eq!(comment.content, "This is a test comment");
    }

    #[tokio::test]
    async fn test_invalid_phase() {
        crate::util::test::init_test_stores().await;

        let store = board_store();
        let ws = test_ws("/ws");
        let id = make_ticket(
            store,
            &ws,
            "Test",
            crate::pipeline::board::TicketPhase::Backlog,
        )
        .await;

        let tool = UpdateTicketTool::new(&ws);
        let args = json!({
            "ticket_id": id,
            "phase": "invalid_phase"
        });
        let result = tool.execute(&ws, args).await;
        assert!(result.is_err(), "Invalid phase should fail");
    }

    // ── Pipeline-occupied guard tests ────────────────────────────

    #[tokio::test]
    #[serial_test::serial(reset_inflight)] // shared global board — a concurrent boot reset would clobber the InDevelopment fixture
    async fn test_guard_not_pipeline_occupied_cases() {
        crate::util::test::init_test_stores().await;

        let store = board_store();
        let ws = test_ws("/ws");

        let occupied_id = TicketBuilder::new(store, &ws)
            .title("OccupiedGuard")
            .desc("in development")
            .phase(TicketPhase::InDevelopment)
            .create()
            .await
            .expect("create");

        let unoccupied_id = TicketBuilder::new(store, &ws)
            .title("UnoccupiedGuard")
            .desc("backlog")
            .phase(TicketPhase::Backlog)
            .create()
            .await
            .expect("create");

        let result = guard_not_pipeline_occupied(store, &occupied_id).await;
        assert!(result.is_err(), "Occupied-phase ticket should be rejected");
        let err = format!("{}", result.unwrap_err());
        assert!(
            err.contains("in-flight"),
            "Error should mention in-flight restriction"
        );

        let result = guard_not_pipeline_occupied(store, &unoccupied_id).await;
        assert!(result.is_ok(), "Unoccupied ticket should pass the guard");

        // Non-existent ticket silently passes (nothing to block)
        let result = guard_not_pipeline_occupied(store, "nonexistent_id").await;
        assert!(
            result.is_ok(),
            "Non-existent ticket should silently pass the guard"
        );
    }

    /// Create a ticket in a pipeline-occupied phase (InDevelopment).
    /// Used by wiring tests below to verify tools call `guard_not_pipeline_occupied`.
    async fn create_occupied_ticket() -> String {
        crate::util::test::init_test_stores().await;
        let store = board_store();
        TicketBuilder::new(store, &test_ws("/ws"))
            .title("BlockMe")
            .desc("in flight")
            .phase(TicketPhase::InDevelopment)
            .create()
            .await
            .expect("create")
    }

    #[tokio::test]
    #[serial_test::serial(reset_inflight)] // shared global board — a concurrent boot reset would clobber the InDevelopment fixture
    async fn test_update_ticket_blocked_by_pipeline() {
        let id = create_occupied_ticket().await;
        let ws = test_ws("/ws");
        let result = UpdateTicketTool::new(&ws)
            .execute(&ws, json!({"ticket_id": id, "phase": "backlog"}))
            .await;
        assert!(
            result.is_err(),
            "Pipeline-occupied ticket should reject update"
        );
    }

    #[tokio::test]
    #[serial_test::serial(reset_inflight)] // shared global board — a concurrent boot reset would clobber the InDevelopment fixture
    async fn test_create_ticket_supersede_blocked_by_pipeline() {
        let id = create_occupied_ticket().await;
        let ws = test_ws("/ws");
        let result = CreateTicketTool::new("test", &ws)
            .execute(
                &ws,
                json!({"title": "Replacement", "description": "trying to supersede", "supersede": id}),
            )
            .await;
        assert!(
            result.is_err(),
            "Supersede should be rejected for pipeline-occupied tickets"
        );
    }

    #[tokio::test]
    async fn test_supersede_inherits_priority() {
        crate::util::test::init_test_stores().await;

        let store = board_store();
        let ws = test_ws("/tmp/test_ws_supersede_priority");

        // Create a ticket with a non-default priority (0 = highest urgency).
        let old_id = TicketBuilder::new(store, &ws)
            .title("Urgent")
            .priority(0)
            .create()
            .await
            .expect("create old ticket");

        // Supersede without specifying priority — should inherit priority 0.
        let tool = CreateTicketTool::new("manager", &ws);
        let args = json!({
            "title": "Replacement",
            "description": "superseding the urgent ticket",
            "supersede": old_id,
        });
        let result = tool
            .execute(&ws, args)
            .await
            .expect("supersede should succeed");
        assert!(result.contains("Superseded"), "Expected supersede output");

        // Find the new ticket ID via the old ticket's superseded_by link.
        let old_ticket = expect_ticket(store, &old_id).await;
        let new_id = old_ticket
            .superseded_by
            .as_deref()
            .expect("old ticket should have superseded_by");

        // Verify the new ticket inherited priority 0 from the old ticket.
        let new_ticket = expect_ticket(store, new_id).await;
        assert_eq!(
            new_ticket.priority, 0,
            "Should inherit priority from superseded ticket"
        );
    }

    #[tokio::test]
    async fn test_supersede_explicit_priority_overrides_inheritance() {
        crate::util::test::init_test_stores().await;

        let store = board_store();
        let ws = test_ws("/tmp/test_ws_supersede_explicit_priority");

        // Create a ticket with a non-default priority.
        let old_id = TicketBuilder::new(store, &ws)
            .title("Old ticket")
            .priority(0)
            .create()
            .await
            .expect("create old ticket");

        // Supersede with explicit priority — should use the explicit value,
        // not inherit the old ticket's priority.
        let tool = CreateTicketTool::new("manager", &ws);
        let args = json!({
            "title": "Replacement",
            "description": "superseding with explicit priority",
            "supersede": old_id,
            "priority": 5,
        });
        let result = tool
            .execute(&ws, args)
            .await
            .expect("supersede should succeed");
        assert!(result.contains("Superseded"), "Expected supersede output");

        let old_ticket = expect_ticket(store, &old_id).await;
        let new_id = old_ticket
            .superseded_by
            .as_deref()
            .expect("old ticket should have superseded_by");

        let new_ticket = expect_ticket(store, new_id).await;
        assert_eq!(
            new_ticket.priority, 5,
            "Explicit priority should override inheritance from old ticket"
        );
    }

    #[tokio::test]
    async fn test_add_comment_allowed_on_pipeline_occupied() {
        let id = create_occupied_ticket().await;
        let ws = test_ws("/ws");
        let result = AddCommentTool::new(&ws)
            .execute(
                &ws,
                json!({"ticket_id": id, "content": "This should be allowed now"}),
            )
            .await;
        assert!(
            result.is_ok(),
            "Comments should be allowed on pipeline-occupied tickets"
        );
    }

    #[test]
    fn test_resolve_ticket_id() {
        // Bare numbers resolve against the bound workspace.
        assert_eq!(resolve_ticket_id("ws", "123").unwrap(), "ws-123");
        assert_eq!(resolve_ticket_id("ws", "  7 ").unwrap(), "ws-7");
        // Fully prefixed form for the bound workspace is accepted.
        assert_eq!(resolve_ticket_id("ws", "ws-123").unwrap(), "ws-123");
        assert_eq!(resolve_ticket_id("my_ws", "my_ws-42").unwrap(), "my_ws-42");
        // Foreign workspace prefixes are rejected.
        let err = format!("{}", resolve_ticket_id("ws", "other-123").unwrap_err());
        assert!(
            err.contains("different workspace"),
            "foreign prefix should be rejected: {err}"
        );
        // Malformed IDs are rejected.
        for bad in [
            "", "abc", "-5", "ws-abc", "123-456", "ws-", "ws-12-34", "___-123",
        ] {
            assert!(
                resolve_ticket_id("ws", bad).is_err(),
                "malformed ID '{bad}' should be rejected"
            );
        }

        // `resolve_any_ticket_id` binds a bare number to the bound workspace
        // but takes any well-formed prefix verbatim; malformed IDs are the
        // shared parser's business, already pinned above.
        assert_eq!(resolve_any_ticket_id("ws", "123").unwrap(), "ws-123");
        assert_eq!(resolve_any_ticket_id("ws", "  7 ").unwrap(), "ws-7");
        assert_eq!(
            resolve_any_ticket_id("ws", "other-123").unwrap(),
            "other-123"
        );
        assert_eq!(resolve_any_ticket_id("ws", "my_ws-42").unwrap(), "my_ws-42");
    }

    #[tokio::test]
    async fn test_get_ticket_id_resolution() {
        crate::util::test::init_test_stores().await;

        let store = board_store();
        let ws = test_ws("/ws");
        let id = TicketBuilder::new(store, &ws)
            .title("Bound")
            .desc("binding test")
            .create()
            .await
            .expect("create");

        let ticket = crate::util::test::expect_ticket(store, &id).await;
        let expected = ticket.detailed_display();

        // Bare numeric ID resolves against the bound workspace.
        let seq = id.strip_prefix("ws-").expect("prefixed id");
        let result = GetTicketTool::new("manager", &ws)
            .execute(&ws, json!({"ticket_id": seq, "full": true}))
            .await
            .expect("bare number should resolve");
        assert_eq!(result, expected);

        // A fully prefixed ID is accepted even from another workspace — the
        // archive search spans the installation, so its hits must open here.
        let foreign = test_ws("/other");
        let result = GetTicketTool::new("manager", &foreign)
            .execute(&foreign, json!({"ticket_id": id, "full": true}))
            .await
            .expect("a prefixed ID from any workspace is accepted");
        assert_eq!(result, expected);
    }

    #[tokio::test]
    async fn test_detailed_display_limited_truncates_description_and_non_tail_comments() {
        crate::util::test::init_test_stores().await;

        let store = board_store();
        let ws = test_ws("/ws");
        let long_desc = "x".repeat(600);
        let id = TicketBuilder::new(store, &ws)
            .title("Limited")
            .desc(&long_desc)
            .create()
            .await
            .expect("create");

        let mut ticket = crate::util::test::expect_ticket(store, &id).await;
        ticket.comments = vec![
            comment("engineer", &"a".repeat(300), "2026-01-01T00:00:00Z"),
            comment("qa", &"b".repeat(250), "2026-01-02T00:00:00Z"),
            comment("reviewer", &"c".repeat(150), "2026-01-03T00:00:00Z"),
            comment("manager", &"d".repeat(100), "2026-01-04T00:00:00Z"),
            comment("assistant", &"e".repeat(80), "2026-01-05T00:00:00Z"),
        ];

        let out = ticket.detailed_display_limited(500, 200, 3);

        // (a) description capped at 500 chars + ellipsis
        assert!(out.contains(&format!("Description: {}…", "x".repeat(500))));
        assert!(
            !out.contains(&long_desc),
            "full description must not appear"
        );
        // (d) header metadata never truncated
        assert!(out.contains(&format!("Ticket: {}", ticket.id)));
        assert!(out.contains("Title: Limited"));
        assert!(out.contains("Reporter: test"));
        assert!(out.contains("Priority: P1"));
        // (b) comments older than the last three capped at 200 chars + ellipsis
        assert!(out.contains(&format!(
            "[engineer] (2026-01-01T00:00:00): {}…",
            "a".repeat(200)
        )));
        assert!(out.contains(&format!("[qa] (2026-01-02T00:00:00): {}…", "b".repeat(200))));
        // (c) last three comments shown in full
        assert!(out.contains(&format!(
            "[reviewer] (2026-01-03T00:00:00): {}",
            "c".repeat(150)
        )));
        assert!(out.contains(&format!(
            "[manager] (2026-01-04T00:00:00): {}",
            "d".repeat(100)
        )));
        assert!(out.contains(&format!(
            "[assistant] (2026-01-05T00:00:00): {}",
            "e".repeat(80)
        )));
    }

    #[tokio::test]
    async fn test_detailed_display_limited_three_or_fewer_comments_all_full() {
        crate::util::test::init_test_stores().await;

        let store = board_store();
        let ws = test_ws("/ws");
        let id = TicketBuilder::new(store, &ws)
            .title("FewComments")
            .desc("short desc")
            .create()
            .await
            .expect("create");

        let mut ticket = crate::util::test::expect_ticket(store, &id).await;
        ticket.comments = vec![
            comment("engineer", &"a".repeat(300), "2026-01-01T00:00:00Z"),
            comment("qa", &"b".repeat(250), "2026-01-02T00:00:00Z"),
            comment("reviewer", &"c".repeat(150), "2026-01-03T00:00:00Z"),
        ];

        let out = ticket.detailed_display_limited(500, 200, 3);
        assert!(out.contains(&"a".repeat(300)));
        assert!(out.contains(&"b".repeat(250)));
        assert!(out.contains(&"c".repeat(150)));
        assert!(
            !out.contains('…'),
            "with ≤3 comments none should be truncated"
        );
    }

    #[tokio::test]
    async fn test_get_ticket_default_truncated_for_same_agent() {
        crate::util::test::init_test_stores().await;

        let store = board_store();
        let ws = test_ws("/ws");
        let long_desc = "y".repeat(600);
        let id = TicketBuilder::new(store, &ws)
            .title("SameAgent")
            .reporter("manager")
            .desc(&long_desc)
            .create()
            .await
            .expect("create");

        let tool = GetTicketTool::new("manager", &ws);
        let result = tool
            .execute(&ws, json!({"ticket_id": id}))
            .await
            .expect("execute");
        assert!(
            result.contains(&format!("Description: {}…", "y".repeat(500))),
            "same-agent default view must truncate the description"
        );
        assert!(!result.contains(&long_desc));
    }

    #[tokio::test]
    async fn test_get_ticket_full_output_for_different_creator() {
        crate::util::test::init_test_stores().await;

        let store = board_store();
        let ws = test_ws("/ws");
        let long_desc = "z".repeat(600);
        let id = TicketBuilder::new(store, &ws)
            .title("OtherAgent")
            .reporter("maintainer")
            .desc(&long_desc)
            .create()
            .await
            .expect("create");

        let ticket = crate::util::test::expect_ticket(store, &id).await;
        let expected = ticket.detailed_display();

        let tool = GetTicketTool::new("manager", &ws);
        let result = tool
            .execute(&ws, json!({"ticket_id": id}))
            .await
            .expect("execute");
        assert_eq!(
            result, expected,
            "a different creator must produce the full un-truncated ticket"
        );
        assert!(result.contains(&long_desc));
    }

    #[tokio::test]
    async fn test_get_ticket_full_flag_returns_complete() {
        crate::util::test::init_test_stores().await;

        let store = board_store();
        let ws = test_ws("/ws");
        let long_desc = "w".repeat(600);
        let id = TicketBuilder::new(store, &ws)
            .title("FullFlag")
            .reporter("manager")
            .desc(&long_desc)
            .create()
            .await
            .expect("create");

        let ticket = crate::util::test::expect_ticket(store, &id).await;
        let expected = ticket.detailed_display();

        let tool = GetTicketTool::new("manager", &ws);
        let result = tool
            .execute(&ws, json!({"ticket_id": id, "full": true}))
            .await
            .expect("execute");
        assert_eq!(
            result, expected,
            "full=true must return the complete ticket"
        );
        assert!(result.contains(&long_desc));
    }
}