nexo-core 0.1.12

Agent runtime: event bus, sessions, plugin trait, heartbeat, A2A delegation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
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
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
//! Phase 79.6 — five `Team*` tools sharing one `Arc<TeamTools>`
//! inner.
//!
//! Step 8 ships:
//!   * `TeamTools` shared inner (bag of `Arc`s the handlers
//!     consume).
//!   * Five `pub struct *Tool { inner: Arc<TeamTools> }`
//!     wrappers, each with its `tool_def() -> ToolDef`.
//!   * Placeholder `ToolHandler::call` impls returning
//!     `{"ok": false, "kind": "NotImplemented"}` so the tool
//!     surface is stable while step 9 wires the real handlers.
//!
//! Reference (PRIMARY):
//!   * `claude-code-leak/src/tools/TeamCreateTool/TeamCreateTool.ts:37-49`
//!     — `inputSchema { team_name, description?, agent_type? }`.
//!     We add `worktree_per_member?`.
//!   * `claude-code-leak/src/tools/TeamDeleteTool/TeamDeleteTool.ts:21-22`
//!     — empty `strictObject`. We widen to `team_id` because we
//!     allow multiple teams per leader (relaxed from the leak's
//!     single-team guard).
//!   * `claude-code-leak/src/tools/SendMessageTool/SendMessageTool.ts:46-58`
//!     — discriminated union of structured messages.

use super::context::AgentContext;
use super::tool_registry::ToolHandler;
use crate::team_message_router::TeamMessageRouter;
use async_trait::async_trait;
use nexo_broker::AnyBroker;
use nexo_config::types::team::TeamPolicy;
use nexo_llm::ToolDef;
use nexo_team_store::TeamStore;
use serde_json::{json, Value};
use std::sync::Arc;

/// Shared inner the five `Team*Tool` structs hold via `Arc`.
/// Handlers read from `store`, publish to `router`, and stamp audit
/// rows with `agent_id` + `current_goal_id`.
///
/// C2 — `policy` is no longer captured at construction. Each handler
/// reads the per-call [`TeamPolicy`] from `ctx.effective_policy().team`
/// via [`TeamTools::policy_for`] so a hot-reload of `team.max_*` (or
/// per-binding override) is observed on the next intake event without
/// re-registration. Mirrors the pattern in
/// `claude-code-leak/src/services/mcp/useManageMCPConnections.ts:624`
/// (invalidate-and-refetch, no actor teardown).
pub struct TeamTools {
    pub store: Arc<dyn TeamStore>,
    pub router: Arc<TeamMessageRouter<AnyBroker>>,
    pub broker: AnyBroker,
    pub agent_id: String,
    /// The lead's own goal_id at construction. When this agent
    /// runs *as a teammate* (delegated by another team's lead),
    /// `team_member_name` on `AgentContext` is `Some(...)` and
    /// the lead-only operations (`TeamCreate`, `TeamDelete`,
    /// `TeamSendMessage { to: "broadcast" }`) refuse. The
    /// in-team-as-non-lead guard runs at call time on the
    /// passed `&AgentContext`.
    pub current_goal_id: String,
}

impl TeamTools {
    pub fn new(
        store: Arc<dyn TeamStore>,
        router: Arc<TeamMessageRouter<AnyBroker>>,
        broker: AnyBroker,
        agent_id: impl Into<String>,
        current_goal_id: impl Into<String>,
    ) -> Arc<Self> {
        Arc::new(Self {
            store,
            router,
            broker,
            agent_id: agent_id.into(),
            current_goal_id: current_goal_id.into(),
        })
    }

    /// Return the per-call team policy resolved from the effective
    /// binding policy. Cheap clone — `TeamPolicy` is six primitive
    /// fields. Single source of truth so handlers stay consistent
    /// across hot-reload.
    pub(super) fn policy_for(&self, ctx: &super::context::AgentContext) -> TeamPolicy {
        ctx.effective_policy().team.clone()
    }
}

// ===============================================================
// Five wrappers. Each holds `Arc<TeamTools>` so they share one
// inner; each has its own `tool_def()` returning the right
// `ToolDef` for the LLM tool catalogue.
// ===============================================================

pub struct TeamCreateTool {
    pub inner: Arc<TeamTools>,
}

impl TeamCreateTool {
    pub fn new(inner: Arc<TeamTools>) -> Self {
        Self { inner }
    }

    pub fn tool_def() -> ToolDef {
        ToolDef {
            name: "TeamCreate".to_string(),
            description: r#"Create a new named team for coordinated parallel work. Returns a `team_id` you reference in subsequent calls.

After creating the team, members can be added by the operator (via `nexo team add-member` CLI — Phase 79.6.b) or registered programmatically through the team store. The MVP exposes `TeamSendMessage` (DM + broadcast), `TeamList`, and `TeamStatus` so the lead can coordinate already-spawned members. Direct goal-spawn-as-teammate from inside a turn lands in Phase 79.6.b.

Caps: 8 members per team (incl. lead), 4 concurrent teams per agent, 24 h idle timeout."#
                .to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "team_name": {
                        "type": "string",
                        "description": "Human-readable name. Sanitized to [a-z0-9-]+ for the team_id (unique). Max 64 chars."
                    },
                    "description": {
                        "type": "string",
                        "description": "Optional one-line summary of what the team is doing."
                    },
                    "agent_type": {
                        "type": "string",
                        "description": "Role label for the lead (e.g. \"coordinator\", \"researcher\"). Free-form; surfaces in TeamStatus."
                    },
                    "worktree_per_member": {
                        "type": "boolean",
                        "description": "When true, each member gets its own git worktree under <workspace>/.team/<team_id>/<member_name>/. Default false (members share the workspace)."
                    }
                },
                "required": ["team_name"]
            }),
        }
    }
}

pub struct TeamDeleteTool {
    pub inner: Arc<TeamTools>,
}

impl TeamDeleteTool {
    pub fn new(inner: Arc<TeamTools>) -> Self {
        Self { inner }
    }

    pub fn tool_def() -> ToolDef {
        ToolDef {
            name: "TeamDelete".to_string(),
            description: r#"Disband a team and clean up its members + DM topics. Refuses with `BlockedByActiveMembers` when any member's goal is still `Running`; send `[shutdown_request]` via `TeamSendMessage { to: "broadcast", message: { type: "shutdown_request" } }` first and wait for members to idle.

Idle members are gracefully cancelled. After the cleanup, the team_id is reusable; tasks + audit rows persist in the store under the deleted team."#
                .to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "team_id": {
                        "type": "string",
                        "description": "Sanitized id returned by TeamCreate. Must be a team this agent leads."
                    }
                },
                "required": ["team_id"]
            }),
        }
    }
}

pub struct TeamSendMessageTool {
    pub inner: Arc<TeamTools>,
}

impl TeamSendMessageTool {
    pub fn new(inner: Arc<TeamTools>) -> Self {
        Self { inner }
    }

    pub fn tool_def() -> ToolDef {
        ToolDef {
            name: "TeamSendMessage".to_string(),
            description: r#"Send a message to a teammate (point-to-point) or to all members (broadcast). Wakes idle teammates. Body capped at 64 KiB serialised JSON.

`to`: member name (e.g. "researcher") for DM, or the literal string "broadcast" (lead only).
`message`: free-form text OR a structured message:
- `{ "type": "shutdown_request", "reason"?: "..." }` — ask members to wind down.
- `{ "type": "shutdown_response", "request_id": "...", "approved": true|false, "reason"?: "..." }`.
- `{ "type": "task_assigned", "task_id": "..." }`.
- `{ "type": "done", "result"?: ... }`.

Only the lead can broadcast. Any team member can DM another."#
                .to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "team_id": {
                        "type": "string",
                        "description": "Sanitized id returned by TeamCreate."
                    },
                    "to": {
                        "type": "string",
                        "description": "Member name (point-to-point) or \"broadcast\" (lead only)."
                    },
                    "message": {
                        "description": "String or structured object {type, ...}. Capped at 64 KiB."
                    }
                },
                "required": ["team_id", "to", "message"]
            }),
        }
    }
}

pub struct TeamListTool {
    pub inner: Arc<TeamTools>,
}

impl TeamListTool {
    pub fn new(inner: Arc<TeamTools>) -> Self {
        Self { inner }
    }

    pub fn tool_def() -> ToolDef {
        ToolDef {
            name: "TeamList".to_string(),
            description: "List teams owned by this agent (read-only). Pass `active_only: true` to exclude soft-deleted teams.".to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "active_only": {
                        "type": "boolean",
                        "description": "Default true — only return teams with `deleted_at IS NULL`."
                    }
                }
            }),
        }
    }
}

pub struct TeamStatusTool {
    pub inner: Arc<TeamTools>,
}

impl TeamStatusTool {
    pub fn new(inner: Arc<TeamTools>) -> Self {
        Self { inner }
    }

    pub fn tool_def() -> ToolDef {
        ToolDef {
            name: "TeamStatus".to_string(),
            description: "Detailed status of one team: members + their last_active_at + agent_type + idle/running. Read-only. Caller must be the lead OR a current member (NotMember otherwise).".to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "team_id": {
                        "type": "string",
                        "description": "Sanitized id returned by TeamCreate."
                    }
                },
                "required": ["team_id"]
            }),
        }
    }
}

// ===============================================================
// Handlers
// ===============================================================
//
// Conventions:
//   * Capability gate (`policy.tool_enabled()`) is enforced at the
//     handler level too — main.rs only registers when enabled,
//     but a handler called without the gate (e.g. via tests with
//     a default policy) still refuses cleanly.
//   * Output shape mirrors Phase 79.5/79.10: `{ok: true, ...}`
//     or `{ok: false, kind: "...", error: "..."}`.
//   * Audit rows go through `store.record_event` best-effort —
//     a failed insert is logged via tracing but does not fail
//     the user-visible call.
//
// Reference (PRIMARY):
//   * `claude-code-leak/src/tools/TeamCreateTool/TeamCreateTool.ts:128-237`
//     — full `call()` body (single-team-per-leader guard at
//     `:133-140` we explicitly relax via `max_concurrent`).
//   * `claude-code-leak/src/tools/TeamDeleteTool/TeamDeleteTool.ts:71-135`
//     — call body, active-member guard.
//   * `claude-code-leak/src/tools/SendMessageTool/SendMessageTool.ts:1-58`
//     — discriminated union of structured messages.

use nexo_team_store::{
    sanitize_name, validate_member_name_for_lead, validate_team_name, TeamEventRow, TeamMemberRow,
    TeamRow, TeamStoreError, DM_BODY_MAX_BYTES, TEAM_LEAD_NAME,
};

fn now_ts() -> i64 {
    chrono::Utc::now().timestamp()
}

fn new_event_id() -> String {
    uuid::Uuid::new_v4().to_string()
}

fn err(kind: &str, msg: impl Into<String>) -> Value {
    json!({
        "ok": false,
        "kind": kind,
        "error": msg.into(),
    })
}

/// Best-effort audit insert. Failures are logged but never fail
/// the user-visible call.
async fn record_event(
    inner: &TeamTools,
    team_id: &str,
    kind: &str,
    actor: Option<&str>,
    payload: Value,
) {
    let row = TeamEventRow {
        event_id: new_event_id(),
        team_id: team_id.to_string(),
        kind: kind.to_string(),
        actor_member_name: actor.map(str::to_string),
        payload_json: payload.to_string(),
        created_at: now_ts(),
    };
    if let Err(e) = inner.store.record_event(&row).await {
        tracing::warn!(
            target: "team::audit",
            team_id,
            kind,
            error = %e,
            "[team] audit record failed"
        );
    }
}

#[async_trait]
impl ToolHandler for TeamCreateTool {
    async fn call(&self, ctx: &AgentContext, args: Value) -> anyhow::Result<Value> {
        // Guard: only the main goal of this agent (not a teammate
        // running inside another team) can spawn a team. Mirror
        // of leak's `claude-code-leak/src/tools/AgentTool/prompt.ts:279-282`
        // ("teammates cannot spawn other teammates").
        if ctx.is_teammate() {
            return Ok(err(
                "TeammateCannotSpawnTeammate",
                "teammates cannot spawn other teams",
            ));
        }
        // C2 — pull policy from the per-call effective binding
        // policy. Cheap clone (six primitives). Hot-reload pickup
        // semantics: a snapshot swap that flips `team.enabled` /
        // `team.max_*` is observed on the next intake event.
        let team_policy = self.inner.policy_for(ctx);
        if !team_policy.tool_enabled() {
            return Ok(err(
                "TeamingDisabled",
                "team feature not enabled for this agent",
            ));
        }

        let team_name = match args.get("team_name").and_then(|v| v.as_str()) {
            Some(s) => s.to_string(),
            None => return Ok(err("Wire", "TeamCreate requires `team_name` (string)")),
        };
        let description = args
            .get("description")
            .and_then(|v| v.as_str())
            .map(str::to_string);
        let agent_type = args
            .get("agent_type")
            .and_then(|v| v.as_str())
            .map(str::to_string);
        let worktree_default = team_policy.worktree_per_member;
        let worktree_per_member = args
            .get("worktree_per_member")
            .and_then(|v| v.as_bool())
            .unwrap_or(worktree_default);

        let team_id = match validate_team_name(&team_name) {
            Ok(s) => s,
            Err(_) => {
                return Ok(err(
                    "InvalidName",
                    format!("invalid team_name: `{team_name}`"),
                ))
            }
        };

        // Per-agent concurrent cap.
        let active_count = self
            .inner
            .store
            .count_active_for_agent(&self.inner.agent_id)
            .await
            .map_err(|e| anyhow::anyhow!("count_active_for_agent: {e}"))?;
        let cap = team_policy.effective_max_concurrent() as usize;
        if active_count >= cap {
            tracing::warn!(
                target: "team::cap_exceeded",
                agent = %self.inner.agent_id,
                count = active_count,
                cap,
                "[team] ConcurrentCapExceeded"
            );
            return Ok(json!({
                "ok": false,
                "kind": "ConcurrentCapExceeded",
                "count": active_count,
                "cap": cap,
                "error": format!("agent already leads {active_count} active teams (cap {cap})"),
            }));
        }

        let now = now_ts();
        let team_row = TeamRow {
            team_id: team_id.clone(),
            display_name: team_name.clone(),
            description: description.clone(),
            lead_agent_id: self.inner.agent_id.clone(),
            lead_goal_id: self.inner.current_goal_id.clone(),
            // 79.6.b will wire the real Phase 14 FlowFlow id;
            // the team_id is a stable placeholder so downstream
            // queries (TeamStatus) have a non-empty value.
            flow_id: team_id.clone(),
            worktree_per_member,
            created_at: now,
            deleted_at: None,
            last_active_at: now,
        };

        if let Err(e) = self.inner.store.create_team(&team_row).await {
            return match e {
                TeamStoreError::TeamNameTaken(existing) => Ok(json!({
                    "ok": false,
                    "kind": "TeamNameTaken",
                    "existing_team_id": existing,
                    "error": format!("team `{team_id}` already exists"),
                })),
                other => Err(anyhow::anyhow!("create_team: {other}")),
            };
        }

        // Seed the lead row.
        let lead_name = TEAM_LEAD_NAME.to_string();
        let lead_member = TeamMemberRow {
            team_id: team_id.clone(),
            name: lead_name.clone(),
            agent_id: self.inner.agent_id.clone(),
            agent_type: agent_type.clone(),
            model: None,
            goal_id: self.inner.current_goal_id.clone(),
            worktree_path: None,
            joined_at: now,
            is_active: true,
            last_active_at: now,
        };
        if let Err(e) = self.inner.store.add_member(&lead_member).await {
            tracing::warn!(
                target: "team::create",
                team_id = %team_id,
                error = %e,
                "[team] lead row insert failed — soft-deleting team"
            );
            let _ = self.inner.store.soft_delete_team(&team_id, now).await;
            return Err(anyhow::anyhow!("add_member(lead): {e}"));
        }

        record_event(
            &self.inner,
            &team_id,
            "team_created",
            Some(&lead_name),
            json!({
                "display_name": team_name,
                "description": description,
                "lead_agent_id": self.inner.agent_id,
                "agent_type": agent_type,
                "worktree_per_member": worktree_per_member,
            }),
        )
        .await;

        tracing::info!(
            target: "team::create",
            team_id = %team_id,
            agent = %self.inner.agent_id,
            "[team] created"
        );

        Ok(json!({
            "ok": true,
            "team_id": team_id,
            "lead_agent_id": self.inner.agent_id,
            "lead_member_name": lead_name,
            "flow_id": team_row.flow_id,
            "created_at": now,
            "instructions": "Members are added by the operator (Phase 79.6.b CLI) or by the runtime when sub-goals spawn. From inside a turn, the model coordinates members via `TeamSendMessage` (DM or broadcast) + `TeamStatus`. Wind down via `TeamSendMessage { to: \"broadcast\", message: { type: \"shutdown_request\" } }` then `TeamDelete`."
        }))
    }
}

#[async_trait]
impl ToolHandler for TeamDeleteTool {
    async fn call(&self, ctx: &AgentContext, args: Value) -> anyhow::Result<Value> {
        if ctx.is_teammate() {
            return Ok(err(
                "TeammateCannotDeleteTeam",
                "only the team lead can delete the team (you are running as a teammate)",
            ));
        }
        if !self.inner.policy_for(ctx).tool_enabled() {
            return Ok(err("TeamingDisabled", "team feature not enabled"));
        }
        let team_id_raw = match args.get("team_id").and_then(|v| v.as_str()) {
            Some(s) => s.to_string(),
            None => return Ok(err("Wire", "TeamDelete requires `team_id`")),
        };
        let team_id = sanitize_name(&team_id_raw);

        let team = match self.inner.store.get_team(&team_id).await {
            Ok(Some(t)) => t,
            Ok(None) => return Ok(err("TeamNotFound", format!("team `{team_id}` not found"))),
            Err(e) => return Err(anyhow::anyhow!("get_team: {e}")),
        };
        if team.lead_agent_id != self.inner.agent_id {
            return Ok(err(
                "NotLeader",
                format!(
                    "agent `{}` is not the lead of team `{team_id}`",
                    self.inner.agent_id
                ),
            ));
        }
        if team.deleted_at.is_some() {
            return Ok(err(
                "AlreadyDeleted",
                format!("team `{team_id}` was already soft-deleted"),
            ));
        }

        // Active members guard. Members whose `is_active = true`
        // are still running their turn; refuse and prompt the
        // model to send a shutdown_request first.
        let members = self
            .inner
            .store
            .list_members(&team_id)
            .await
            .map_err(|e| anyhow::anyhow!("list_members: {e}"))?;
        let active_non_lead: Vec<&TeamMemberRow> = members
            .iter()
            .filter(|m| m.is_active && m.name != TEAM_LEAD_NAME)
            .collect();
        if !active_non_lead.is_empty() {
            let names: Vec<String> = active_non_lead.iter().map(|m| m.name.clone()).collect();
            return Ok(json!({
                "ok": false,
                "kind": "BlockedByActiveMembers",
                "names": names,
                "error": format!(
                    "team `{team_id}` has {} active member(s): {}. Send `TeamSendMessage {{ to: \"broadcast\", message: {{ type: \"shutdown_request\" }} }}` and wait for them to idle before deleting.",
                    active_non_lead.len(),
                    names.join(", ")
                ),
            }));
        }

        let now = now_ts();
        if let Err(e) = self.inner.store.soft_delete_team(&team_id, now).await {
            return Err(anyhow::anyhow!("soft_delete_team: {e}"));
        }
        self.inner.router.drop_team(&team_id);

        let members_cleaned = members.len();
        record_event(
            &self.inner,
            &team_id,
            "team_deleted",
            Some(TEAM_LEAD_NAME),
            json!({ "members_cleaned": members_cleaned }),
        )
        .await;

        tracing::info!(
            target: "team::delete",
            team_id = %team_id,
            members_cleaned,
            "[team] deleted"
        );

        Ok(json!({
            "ok": true,
            "team_id": team_id,
            "members_cleaned": members_cleaned,
            // Force-kill of in-flight goals during drain is wired
            // in step 11 (main.rs SIGTERM hook); the model-driven
            // delete path here only proceeds when no member is
            // active, so this stays 0 by construction.
            "force_killed": 0,
        }))
    }
}

#[async_trait]
impl ToolHandler for TeamSendMessageTool {
    async fn call(&self, ctx: &AgentContext, args: Value) -> anyhow::Result<Value> {
        if !self.inner.policy_for(ctx).tool_enabled() {
            return Ok(err("TeamingDisabled", "team feature not enabled"));
        }
        let team_id_raw = match args.get("team_id").and_then(|v| v.as_str()) {
            Some(s) => s.to_string(),
            None => return Ok(err("Wire", "TeamSendMessage requires `team_id`")),
        };
        let team_id = sanitize_name(&team_id_raw);
        let to = match args.get("to").and_then(|v| v.as_str()) {
            Some(s) => s.to_string(),
            None => {
                return Ok(err(
                    "Wire",
                    "TeamSendMessage requires `to` (member name or \"broadcast\")",
                ))
            }
        };
        let body = args.get("message").cloned().unwrap_or(Value::Null);
        if body.is_null() {
            return Ok(err(
                "Wire",
                "TeamSendMessage requires `message` (string or structured object)",
            ));
        }

        // Body cap.
        let body_len = serde_json::to_vec(&body)
            .map(|b| b.len())
            .unwrap_or(usize::MAX);
        if body_len > DM_BODY_MAX_BYTES {
            return Ok(json!({
                "ok": false,
                "kind": "BodyTooLarge",
                "actual": body_len,
                "max": DM_BODY_MAX_BYTES,
                "error": format!("message body is {body_len} bytes (max {DM_BODY_MAX_BYTES})"),
            }));
        }

        let team = match self.inner.store.get_team(&team_id).await {
            Ok(Some(t)) => t,
            Ok(None) => return Ok(err("TeamNotFound", format!("team `{team_id}` not found"))),
            Err(e) => return Err(anyhow::anyhow!("get_team: {e}")),
        };
        if team.deleted_at.is_some() {
            return Ok(err(
                "TeamDeleted",
                format!("team `{team_id}` is soft-deleted"),
            ));
        }

        // Membership: caller must be lead OR a member of the team.
        let is_lead = team.lead_agent_id == self.inner.agent_id
            && (ctx.team_member_name.as_deref() == Some(TEAM_LEAD_NAME) || !ctx.is_teammate());
        let caller_member_name = if is_lead {
            TEAM_LEAD_NAME.to_string()
        } else {
            // Find the member entry by agent_id.
            let members = self
                .inner
                .store
                .list_members(&team_id)
                .await
                .map_err(|e| anyhow::anyhow!("list_members: {e}"))?;
            match members
                .iter()
                .find(|m| m.agent_id == self.inner.agent_id)
                .map(|m| m.name.clone())
            {
                Some(n) => n,
                None => {
                    return Ok(err(
                        "NotMember",
                        format!(
                            "agent `{}` is not a member of team `{team_id}`",
                            self.inner.agent_id
                        ),
                    ))
                }
            }
        };

        // Broadcast guard: only the lead can broadcast.
        if to == "broadcast" {
            if !is_lead {
                return Ok(err(
                    "OnlyLeadCanBroadcast",
                    "only the team lead can publish to `broadcast`",
                ));
            }
            self.inner
                .router
                .publish_broadcast(&team_id, &caller_member_name, body.clone())
                .await
                .map_err(|e| anyhow::anyhow!("publish_broadcast: {e}"))?;
            record_event(
                &self.inner,
                &team_id,
                "broadcast_sent",
                Some(&caller_member_name),
                json!({ "body_bytes": body_len }),
            )
            .await;
            // touch_team so the idle reaper sees activity.
            let _ = self.inner.store.touch_team(&team_id, now_ts()).await;
            tracing::info!(
                target: "team::broadcast_sent",
                team_id = %team_id,
                from = %caller_member_name,
                body_bytes = body_len,
                "[team] broadcast"
            );
            return Ok(json!({
                "ok": true,
                "team_id": team_id,
                "to": "broadcast",
            }));
        }

        // Point-to-point. Validate target name.
        let target_name = match validate_member_name_for_lead(&to) {
            Ok(n) => n,
            Err(_) => return Ok(err("InvalidMemberName", format!("invalid `to`: {to}"))),
        };
        let correlation_id = uuid::Uuid::new_v4().to_string();
        self.inner
            .router
            .publish_dm(
                &team_id,
                &caller_member_name,
                &target_name,
                body.clone(),
                Some(correlation_id.clone()),
            )
            .await
            .map_err(|e| anyhow::anyhow!("publish_dm: {e}"))?;
        record_event(
            &self.inner,
            &team_id,
            "dm_sent",
            Some(&caller_member_name),
            json!({
                "to": target_name,
                "body_bytes": body_len,
                "correlation_id": correlation_id,
            }),
        )
        .await;
        let _ = self.inner.store.touch_team(&team_id, now_ts()).await;
        tracing::info!(
            target: "team::dm_sent",
            team_id = %team_id,
            from = %caller_member_name,
            to = %target_name,
            body_bytes = body_len,
            "[team] DM"
        );
        Ok(json!({
            "ok": true,
            "team_id": team_id,
            "to": target_name,
            "correlation_id": correlation_id,
        }))
    }
}

#[async_trait]
impl ToolHandler for TeamListTool {
    async fn call(&self, ctx: &AgentContext, args: Value) -> anyhow::Result<Value> {
        if !self.inner.policy_for(ctx).tool_enabled() {
            return Ok(err("TeamingDisabled", "team feature not enabled"));
        }
        let active_only = args
            .get("active_only")
            .and_then(|v| v.as_bool())
            .unwrap_or(true);
        let teams = self
            .inner
            .store
            .list_teams(Some(&self.inner.agent_id), active_only)
            .await
            .map_err(|e| anyhow::anyhow!("list_teams: {e}"))?;
        let json_teams: Vec<Value> = teams
            .iter()
            .map(|t| {
                json!({
                    "team_id": t.team_id,
                    "display_name": t.display_name,
                    "description": t.description,
                    "lead_agent_id": t.lead_agent_id,
                    "created_at": t.created_at,
                    "deleted_at": t.deleted_at,
                    "last_active_at": t.last_active_at,
                })
            })
            .collect();
        Ok(json!({
            "ok": true,
            "n": teams.len(),
            "teams": json_teams,
        }))
    }
}

#[async_trait]
impl ToolHandler for TeamStatusTool {
    async fn call(&self, ctx: &AgentContext, args: Value) -> anyhow::Result<Value> {
        if !self.inner.policy_for(ctx).tool_enabled() {
            return Ok(err("TeamingDisabled", "team feature not enabled"));
        }
        let team_id_raw = match args.get("team_id").and_then(|v| v.as_str()) {
            Some(s) => s.to_string(),
            None => return Ok(err("Wire", "TeamStatus requires `team_id`")),
        };
        let team_id = sanitize_name(&team_id_raw);
        let team = match self.inner.store.get_team(&team_id).await {
            Ok(Some(t)) => t,
            Ok(None) => return Ok(err("TeamNotFound", format!("team `{team_id}` not found"))),
            Err(e) => return Err(anyhow::anyhow!("get_team: {e}")),
        };
        let members = self
            .inner
            .store
            .list_members(&team_id)
            .await
            .map_err(|e| anyhow::anyhow!("list_members: {e}"))?;
        // Membership gate: caller is either the lead OR a member.
        let is_lead = team.lead_agent_id == self.inner.agent_id;
        let is_member = members.iter().any(|m| m.agent_id == self.inner.agent_id);
        if !is_lead && !is_member {
            // Mirror the leak's "tool only available within the team"
            // semantics — a non-member shouldn't even confirm the
            // team's existence beyond the explicit refusal.
            let _ = ctx; // silence unused
            return Ok(err(
                "NotMember",
                "only the team lead or a current member may read the team status",
            ));
        }

        let n_running = members.iter().filter(|m| m.is_active).count();
        let n_idle = members.len().saturating_sub(n_running);

        let json_members: Vec<Value> = members
            .iter()
            .map(|m| {
                json!({
                    "name": m.name,
                    "agent_id": m.agent_id,
                    "agent_type": m.agent_type,
                    "model": m.model,
                    "joined_at": m.joined_at,
                    "is_active": m.is_active,
                    "last_active_at": m.last_active_at,
                })
            })
            .collect();

        Ok(json!({
            "ok": true,
            "team": {
                "team_id": team.team_id,
                "display_name": team.display_name,
                "description": team.description,
                "lead_agent_id": team.lead_agent_id,
                "created_at": team.created_at,
                "deleted_at": team.deleted_at,
                "last_active_at": team.last_active_at,
                "worktree_per_member": team.worktree_per_member,
            },
            "members": json_members,
            // Phase 14 FlowFlow integration is 79.6.b. Until
            // then, surface zero counts so the field is stable.
            "task_summary": {
                "pending": 0,
                "running": n_running,
                "done": n_idle,
            },
        }))
    }
}

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

    #[test]
    fn team_create_def_advertises_required_team_name() {
        let def = TeamCreateTool::tool_def();
        assert_eq!(def.name, "TeamCreate");
        let required = def.parameters["required"].as_array().unwrap();
        assert!(required.iter().any(|v| v == "team_name"));
        // Optional fields surfaced in properties.
        let props = def.parameters["properties"].as_object().unwrap();
        for k in [
            "team_name",
            "description",
            "agent_type",
            "worktree_per_member",
        ] {
            assert!(props.contains_key(k), "missing property `{k}`");
        }
    }

    #[test]
    fn team_delete_def_requires_team_id() {
        let def = TeamDeleteTool::tool_def();
        assert_eq!(def.name, "TeamDelete");
        let required = def.parameters["required"].as_array().unwrap();
        assert_eq!(required.len(), 1);
        assert_eq!(required[0], "team_id");
    }

    #[test]
    fn team_send_message_def_requires_team_id_to_message() {
        let def = TeamSendMessageTool::tool_def();
        assert_eq!(def.name, "TeamSendMessage");
        let required = def.parameters["required"].as_array().unwrap();
        let names: Vec<&str> = required.iter().map(|v| v.as_str().unwrap()).collect();
        assert!(names.contains(&"team_id"));
        assert!(names.contains(&"to"));
        assert!(names.contains(&"message"));
    }

    #[test]
    fn team_list_def_optional_active_only() {
        let def = TeamListTool::tool_def();
        assert_eq!(def.name, "TeamList");
        // No `required` field at all — every property is optional.
        assert!(def.parameters.get("required").is_none());
        let props = def.parameters["properties"].as_object().unwrap();
        assert!(props.contains_key("active_only"));
    }

    #[test]
    fn team_status_def_requires_team_id() {
        let def = TeamStatusTool::tool_def();
        assert_eq!(def.name, "TeamStatus");
        let required = def.parameters["required"].as_array().unwrap();
        assert_eq!(required.len(), 1);
        assert_eq!(required[0], "team_id");
    }

    // -----------------------------------------------------------
    // Handler tests (step 9). Use the real `SqliteTeamStore` in
    // memory + `LocalBroker` so every wire path exercises real
    // SQL + real broker subscribe/publish semantics.
    // -----------------------------------------------------------

    use crate::session::SessionManager;
    use nexo_broker::AnyBroker;
    use nexo_config::types::agents::{
        AgentConfig, AgentRuntimeConfig, DreamingYamlConfig, HeartbeatConfig, ModelConfig,
        OutboundAllowlistConfig, WorkspaceGitConfig,
    };
    use nexo_team_store::SqliteTeamStore;
    use std::sync::Arc;

    fn agent_cfg_with_team(team: TeamPolicy) -> AgentConfig {
        AgentConfig {
            id: "cody".into(),
            model: ModelConfig {
                provider: "x".into(),
                model: "y".into(),
            },
            plugins: Vec::new(),
            heartbeat: HeartbeatConfig::default(),
            config: AgentRuntimeConfig::default(),
            system_prompt: String::new(),
            workspace: String::new(),
            skills: Vec::new(),
            skills_dir: "./skills".into(),
            skill_overrides: Default::default(),
            transcripts_dir: String::new(),
            dreaming: DreamingYamlConfig::default(),
            workspace_git: WorkspaceGitConfig::default(),
            tool_rate_limits: None,
            tool_args_validation: None,
            extra_docs: Vec::new(),
            inbound_bindings: Vec::new(),
            allowed_tools: Vec::new(),
            sender_rate_limit: None,
            allowed_delegates: Vec::new(),
            accept_delegates_from: Vec::new(),
            description: String::new(),
            google_auth: None,
            credentials: Default::default(),
            link_understanding: serde_json::Value::Null,
            web_search: serde_json::Value::Null,
            pairing_policy: serde_json::Value::Null,
            language: None,
            outbound_allowlist: OutboundAllowlistConfig::default(),
            context_optimization: None,
            dispatch_policy: Default::default(),
            plan_mode: Default::default(),
            remote_triggers: Vec::new(),
            lsp: nexo_config::types::lsp::LspPolicy::default(),
            config_tool: nexo_config::types::config_tool::ConfigToolPolicy::default(),
            team,
            proactive: Default::default(),
            repl: Default::default(),
            auto_dream: None,
            assistant_mode: None,
            away_summary: None,
            brief: None,
            channels: None,
            auto_approve: false,
            extract_memories: None,
            event_subscribers: Vec::new(),
            tenant_id: None,
            extensions_config: std::collections::BTreeMap::new(),
            active: true,
        }
    }

    fn agent_cfg() -> AgentConfig {
        agent_cfg_with_team(TeamPolicy {
            enabled: true,
            ..TeamPolicy::default()
        })
    }

    /// C2 — build a ctx whose `effective_policy().team` returns the
    /// given policy. Tests that want to exercise handler-side policy
    /// gating use this helper; tests that just need a "team enabled"
    /// ctx use [`ctx_lead`].
    async fn ctx_lead_with_policy(policy: TeamPolicy) -> AgentContext {
        AgentContext::new(
            "cody",
            Arc::new(agent_cfg_with_team(policy)),
            AnyBroker::local(),
            Arc::new(SessionManager::new(std::time::Duration::from_secs(60), 8)),
        )
    }

    async fn ctx_lead() -> AgentContext {
        AgentContext::new(
            "cody",
            Arc::new(agent_cfg()),
            AnyBroker::local(),
            Arc::new(SessionManager::new(std::time::Duration::from_secs(60), 8)),
        )
    }

    async fn ctx_teammate() -> AgentContext {
        ctx_lead().await.with_team("feature-x", "researcher")
    }

    /// C2 — `policy` argument removed; handlers pull from
    /// `ctx.effective_policy().team`. Tests pass policy via ctx.
    async fn build_inner(agent_id: &str) -> Arc<TeamTools> {
        let broker = Arc::new(AnyBroker::local());
        let store: Arc<dyn TeamStore> = Arc::new(SqliteTeamStore::open_in_memory().await.unwrap());
        let router = TeamMessageRouter::new(broker.clone());
        let cancel = tokio_util::sync::CancellationToken::new();
        router.spawn(cancel);
        // Detach broker from Arc<AnyBroker> back to AnyBroker (it's
        // Clone) so TeamTools holds its own copy + the router holds
        // the Arc.
        TeamTools::new(store, router, (*broker).clone(), agent_id, "lead-goal-1")
    }

    #[tokio::test]
    async fn team_create_rejects_when_capability_disabled() {
        // C2 — `enabled` lives on the ctx's effective policy, not on
        // `TeamTools` anymore. Use `ctx_lead_with_policy` to get a
        // ctx that resolves `team.enabled = false`.
        let inner = build_inner("cody").await;
        let tool = TeamCreateTool::new(inner);
        let res = tool
            .call(
                &ctx_lead_with_policy(TeamPolicy::default()).await,
                json!({ "team_name": "feature-x" }),
            )
            .await
            .unwrap();
        assert_eq!(res["ok"], false);
        assert_eq!(res["kind"], "TeamingDisabled");
    }

    #[tokio::test]
    async fn team_create_returns_team_id_and_lead_member() {
        let inner = build_inner("cody").await;
        let tool = TeamCreateTool::new(inner.clone());
        let res = tool
            .call(
                &ctx_lead().await,
                json!({
                    "team_name": "Feature-X",
                    "description": "Build the new auth flow",
                    "agent_type": "coordinator"
                }),
            )
            .await
            .unwrap();
        assert_eq!(res["ok"], true);
        assert_eq!(res["team_id"], "feature-x");
        assert_eq!(res["lead_member_name"], "team-lead");

        // Audit row recorded.
        let events = inner
            .store
            .tail_events(Some("feature-x"), 10)
            .await
            .unwrap();
        assert!(events.iter().any(|e| e.kind == "team_created"));
    }

    #[tokio::test]
    async fn team_create_rejects_collision() {
        let inner = build_inner("cody").await;
        let tool = TeamCreateTool::new(inner);
        let _ = tool
            .call(&ctx_lead().await, json!({ "team_name": "feature-x" }))
            .await
            .unwrap();
        let res = tool
            .call(&ctx_lead().await, json!({ "team_name": "feature-x" }))
            .await
            .unwrap();
        assert_eq!(res["ok"], false);
        assert_eq!(res["kind"], "TeamNameTaken");
        assert_eq!(res["existing_team_id"], "feature-x");
    }

    #[tokio::test]
    async fn team_create_rejects_when_at_max_concurrent() {
        // C2 — `max_concurrent` lives on ctx.effective_policy().team
        // now. Build a ctx with the cap = 1, drive both calls through
        // it.
        let inner = build_inner("cody").await;
        let ctx = ctx_lead_with_policy(TeamPolicy {
            enabled: true,
            max_concurrent: 1,
            ..TeamPolicy::default()
        })
        .await;
        let tool = TeamCreateTool::new(inner);
        let _ = tool
            .call(&ctx, json!({ "team_name": "first" }))
            .await
            .unwrap();
        let res = tool
            .call(&ctx, json!({ "team_name": "second" }))
            .await
            .unwrap();
        assert_eq!(res["ok"], false);
        assert_eq!(res["kind"], "ConcurrentCapExceeded");
        assert_eq!(res["cap"], 1);
    }

    #[tokio::test]
    async fn team_create_refuses_from_teammate_context() {
        let inner = build_inner("cody").await;
        let tool = TeamCreateTool::new(inner);
        let res = tool
            .call(&ctx_teammate().await, json!({ "team_name": "nested" }))
            .await
            .unwrap();
        assert_eq!(res["ok"], false);
        assert_eq!(res["kind"], "TeammateCannotSpawnTeammate");
    }

    #[tokio::test]
    async fn team_delete_only_lead_can_invoke() {
        // Create the team as `cody`, then try to delete from a
        // different-agent inner.
        let inner_lead = build_inner("cody").await;
        TeamCreateTool::new(inner_lead.clone())
            .call(&ctx_lead().await, json!({ "team_name": "feature-x" }))
            .await
            .unwrap();

        // Build a SECOND inner pointing at the same store with a
        // different agent_id. C2 — policy comes from ctx.
        let inner_other = TeamTools::new(
            Arc::clone(&inner_lead.store),
            Arc::clone(&inner_lead.router),
            inner_lead.broker.clone(),
            "other-agent",
            "other-goal",
        );
        let res = TeamDeleteTool::new(inner_other)
            .call(
                &AgentContext::new(
                    "other-agent",
                    Arc::new(agent_cfg()),
                    AnyBroker::local(),
                    Arc::new(SessionManager::new(std::time::Duration::from_secs(60), 8)),
                ),
                json!({ "team_id": "feature-x" }),
            )
            .await
            .unwrap();
        assert_eq!(res["ok"], false);
        assert_eq!(res["kind"], "NotLeader");
    }

    #[tokio::test]
    async fn team_delete_blocks_when_running_members() {
        let inner = build_inner("cody").await;
        TeamCreateTool::new(inner.clone())
            .call(&ctx_lead().await, json!({ "team_name": "feature-x" }))
            .await
            .unwrap();
        // Add an active non-lead member directly via the store.
        inner
            .store
            .add_member(&TeamMemberRow {
                team_id: "feature-x".into(),
                name: "researcher".into(),
                agent_id: "rsr".into(),
                agent_type: None,
                model: None,
                goal_id: "g".into(),
                worktree_path: None,
                joined_at: 0,
                is_active: true,
                last_active_at: 0,
            })
            .await
            .unwrap();

        let res = TeamDeleteTool::new(inner)
            .call(&ctx_lead().await, json!({ "team_id": "feature-x" }))
            .await
            .unwrap();
        assert_eq!(res["ok"], false);
        assert_eq!(res["kind"], "BlockedByActiveMembers");
        let names = res["names"].as_array().unwrap();
        assert!(names.iter().any(|v| v == "researcher"));
    }

    #[tokio::test]
    async fn team_delete_soft_deletes_and_records_event() {
        let inner = build_inner("cody").await;
        TeamCreateTool::new(inner.clone())
            .call(&ctx_lead().await, json!({ "team_name": "feature-x" }))
            .await
            .unwrap();
        let res = TeamDeleteTool::new(inner.clone())
            .call(&ctx_lead().await, json!({ "team_id": "feature-x" }))
            .await
            .unwrap();
        assert_eq!(res["ok"], true);
        assert_eq!(res["team_id"], "feature-x");

        let team = inner.store.get_team("feature-x").await.unwrap().unwrap();
        assert!(team.deleted_at.is_some());

        let events = inner
            .store
            .tail_events(Some("feature-x"), 10)
            .await
            .unwrap();
        assert!(events.iter().any(|e| e.kind == "team_deleted"));
    }

    #[tokio::test]
    async fn team_send_message_rejects_oversized_body() {
        let inner = build_inner("cody").await;
        TeamCreateTool::new(inner.clone())
            .call(&ctx_lead().await, json!({ "team_name": "feature-x" }))
            .await
            .unwrap();
        let big = "x".repeat(DM_BODY_MAX_BYTES + 1);
        let res = TeamSendMessageTool::new(inner)
            .call(
                &ctx_lead().await,
                json!({
                    "team_id": "feature-x",
                    "to": "researcher",
                    "message": big
                }),
            )
            .await
            .unwrap();
        assert_eq!(res["ok"], false);
        assert_eq!(res["kind"], "BodyTooLarge");
    }

    #[tokio::test]
    async fn team_send_message_broadcast_requires_lead() {
        let inner = build_inner("cody").await;
        TeamCreateTool::new(inner.clone())
            .call(&ctx_lead().await, json!({ "team_name": "feature-x" }))
            .await
            .unwrap();
        // Add the caller as a member.
        inner
            .store
            .add_member(&TeamMemberRow {
                team_id: "feature-x".into(),
                name: "researcher".into(),
                agent_id: "cody".into(),
                agent_type: None,
                model: None,
                goal_id: "g".into(),
                worktree_path: None,
                joined_at: 0,
                is_active: true,
                last_active_at: 0,
            })
            .await
            .unwrap();
        // Send as a teammate (not the lead).
        let res = TeamSendMessageTool::new(inner)
            .call(
                &ctx_teammate().await,
                json!({
                    "team_id": "feature-x",
                    "to": "broadcast",
                    "message": { "type": "shutdown_request" }
                }),
            )
            .await
            .unwrap();
        assert_eq!(res["ok"], false);
        assert_eq!(res["kind"], "OnlyLeadCanBroadcast");
    }

    #[tokio::test]
    async fn team_send_message_dm_publishes_and_records() {
        let inner = build_inner("cody").await;
        TeamCreateTool::new(inner.clone())
            .call(&ctx_lead().await, json!({ "team_name": "feature-x" }))
            .await
            .unwrap();
        // Subscribe a "researcher" inbox.
        let _rx = inner.router.subscribe_member("feature-x", "researcher");
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        let res = TeamSendMessageTool::new(inner.clone())
            .call(
                &ctx_lead().await,
                json!({
                    "team_id": "feature-x",
                    "to": "researcher",
                    "message": { "ask": "ready?" }
                }),
            )
            .await
            .unwrap();
        assert_eq!(res["ok"], true);
        assert!(res["correlation_id"].is_string());

        let events = inner
            .store
            .tail_events(Some("feature-x"), 20)
            .await
            .unwrap();
        assert!(events.iter().any(|e| e.kind == "dm_sent"));
    }

    #[tokio::test]
    async fn team_list_filters_active_only() {
        let inner = build_inner("cody").await;
        TeamCreateTool::new(inner.clone())
            .call(&ctx_lead().await, json!({ "team_name": "a" }))
            .await
            .unwrap();
        TeamCreateTool::new(inner.clone())
            .call(&ctx_lead().await, json!({ "team_name": "b" }))
            .await
            .unwrap();
        TeamDeleteTool::new(inner.clone())
            .call(&ctx_lead().await, json!({ "team_id": "a" }))
            .await
            .unwrap();
        let res = TeamListTool::new(inner.clone())
            .call(&ctx_lead().await, json!({ "active_only": true }))
            .await
            .unwrap();
        assert_eq!(res["ok"], true);
        assert_eq!(res["n"], 1);
        let teams = res["teams"].as_array().unwrap();
        assert_eq!(teams[0]["team_id"], "b");

        let all = TeamListTool::new(inner)
            .call(&ctx_lead().await, json!({ "active_only": false }))
            .await
            .unwrap();
        assert_eq!(all["n"], 2);
    }

    #[tokio::test]
    async fn team_status_rejects_non_member() {
        let inner_lead = build_inner("cody").await;
        TeamCreateTool::new(inner_lead.clone())
            .call(&ctx_lead().await, json!({ "team_name": "feature-x" }))
            .await
            .unwrap();

        // Build a fresh inner pointing at the same store but a
        // different agent. C2 — policy comes from ctx.
        let inner_other = TeamTools::new(
            Arc::clone(&inner_lead.store),
            Arc::clone(&inner_lead.router),
            inner_lead.broker.clone(),
            "stranger",
            "g",
        );
        let res = TeamStatusTool::new(inner_other)
            .call(
                &AgentContext::new(
                    "stranger",
                    Arc::new(agent_cfg()),
                    AnyBroker::local(),
                    Arc::new(SessionManager::new(std::time::Duration::from_secs(60), 8)),
                ),
                json!({ "team_id": "feature-x" }),
            )
            .await
            .unwrap();
        assert_eq!(res["ok"], false);
        assert_eq!(res["kind"], "NotMember");
    }

    #[tokio::test]
    async fn team_status_returns_members_and_summary_for_lead() {
        let inner = build_inner("cody").await;
        TeamCreateTool::new(inner.clone())
            .call(&ctx_lead().await, json!({ "team_name": "feature-x" }))
            .await
            .unwrap();
        let res = TeamStatusTool::new(inner.clone())
            .call(&ctx_lead().await, json!({ "team_id": "feature-x" }))
            .await
            .unwrap();
        assert_eq!(res["ok"], true);
        assert_eq!(res["team"]["team_id"], "feature-x");
        let members = res["members"].as_array().unwrap();
        // Only the lead so far.
        assert_eq!(members.len(), 1);
        assert_eq!(members[0]["name"], "team-lead");
    }

    // ---- C2: per-call policy pull from EffectiveBindingPolicy ----

    /// Same shared `Arc<TeamTools>` driven through TWO ctxs with
    /// DIFFERENT `team.max_concurrent` values produces different
    /// outcomes — proves the handler reads policy fresh per call,
    /// not from a captured field. This is the reload-pickup proof
    /// at the unit level (integration test in `hot_reload_test.rs`
    /// drives the same path through `ConfigReloadCoordinator`).
    #[tokio::test]
    async fn team_create_policy_pulled_per_call_from_ctx() {
        let inner = build_inner("cody").await;
        let tool = TeamCreateTool::new(inner);

        // First ctx: cap=1. First call succeeds, second fails.
        let ctx_cap1 = ctx_lead_with_policy(TeamPolicy {
            enabled: true,
            max_concurrent: 1,
            ..TeamPolicy::default()
        })
        .await;
        let r1 = tool
            .call(&ctx_cap1, json!({ "team_name": "first" }))
            .await
            .unwrap();
        assert_eq!(r1["ok"], true);
        let r2 = tool
            .call(&ctx_cap1, json!({ "team_name": "second" }))
            .await
            .unwrap();
        assert_eq!(r2["ok"], false);
        assert_eq!(r2["kind"], "ConcurrentCapExceeded");

        // SAME tool instance — but a NEW ctx with cap=8 (mirrors a
        // hot-reload widening the cap). The next call now succeeds.
        let ctx_cap8 = ctx_lead_with_policy(TeamPolicy {
            enabled: true,
            max_concurrent: 8,
            ..TeamPolicy::default()
        })
        .await;
        let r3 = tool
            .call(&ctx_cap8, json!({ "team_name": "third" }))
            .await
            .unwrap();
        assert_eq!(r3["ok"], true, "after cap widened the call must succeed");
    }

    /// `team.enabled = false → true` on a hot-reload is observed by
    /// the same tool instance via the new ctx. This is the
    /// behaviour-equivalent of leak's
    /// `claude-code-leak/src/services/mcp/useManageMCPConnections.ts:624`
    /// (invalidate-and-refetch — no actor restart).
    #[tokio::test]
    async fn team_enabled_flip_picked_up_via_new_ctx() {
        let inner = build_inner("cody").await;
        let tool = TeamCreateTool::new(inner);

        // Pre-reload: disabled.
        let ctx_off = ctx_lead_with_policy(TeamPolicy::default()).await;
        let r1 = tool
            .call(&ctx_off, json!({ "team_name": "early" }))
            .await
            .unwrap();
        assert_eq!(r1["kind"], "TeamingDisabled");

        // Post-reload: enabled. Same tool, new ctx.
        let ctx_on = ctx_lead_with_policy(TeamPolicy {
            enabled: true,
            ..TeamPolicy::default()
        })
        .await;
        let r2 = tool
            .call(&ctx_on, json!({ "team_name": "post-reload" }))
            .await
            .unwrap();
        assert_eq!(r2["ok"], true);
    }
}