bohay 0.8.2

Next-Gen Agents multiplexer
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
//! The orchestration **board** tab (docs/22, ORCH-7): a ratatui dashboard for the
//! task ledger + path leases, rendered from `App.orch`. It follows the git-tab
//! pattern (`Tab::is_git`) — a placeholder-leaf tab with no real panes — so every
//! `layout()` path is untouched. **Interactive**: a task cursor (`j/k`, click) with
//! action keys — `s` start · `d` done · `m` merge · `⏎` jump · `x` release — so the
//! whole flow is drivable from the UI, not only the `bohay task …` CLI.

use super::*;

impl App {
    /// Open (or focus, if already open) the orchestration board in the active
    /// workspace. There's one board per workspace; the ledger behind it is global.
    pub fn open_orch_board(&mut self) {
        let ws = &self.workspaces[self.active_ws];
        if let Some(i) = ws.tabs.iter().position(Tab::is_orch) {
            self.workspaces[self.active_ws].active_tab = i;
            return;
        }
        let placeholder = PaneId::alloc(); // never inserted into `panes`
        let ws = &mut self.workspaces[self.active_ws];
        ws.tabs.push(Tab {
            layout: TileLayout::new(placeholder),
            git: None,
            orch: true,
            name: None,
        });
        ws.active_tab = ws.tabs.len() - 1;
        self.zoomed = false;
        self.orch_scroll = 0;
        self.session_dirty = true;
    }

    /// ORCH-3: spawn an **isolated worker** for a task — a git worktree on a fresh
    /// branch + a pane in it — then claim the task for that pane, mark it Running,
    /// lease its declared paths, and optionally launch an agent (which gets the
    /// task briefing as its opening prompt). If the task already has a worktree on
    /// disk (a restart, a closed pane), that worktree is **reopened** instead of
    /// creating a second one. Requires a git repo (worktree isolation is the whole
    /// point); returns the worker pane + worktree path. Explicit (`task start`),
    /// never automatic — nothing spawns unless asked.
    pub fn task_start(
        &mut self,
        id: &str,
        branch: Option<String>,
        agent: Option<String>,
    ) -> Result<(PaneId, std::path::PathBuf), (String, String)> {
        let task = self
            .orch
            .task(id)
            .cloned()
            .ok_or_else(|| ("not_found".to_string(), format!("no such task: {id}")))?;
        if task.assignee.is_some() {
            return Err((
                "already_claimed".to_string(),
                format!("{id} is already started/claimed"),
            ));
        }
        if !self.orch.ready(id) {
            return Err((
                "deps_unmet".to_string(),
                format!("{id} has dependencies that aren't done yet"),
            ));
        }
        // The branch this worker runs on: an explicit `--branch`, else the one
        // recorded on the task, else `bohay/<id>`.
        let branch = branch
            .map(|b| b.trim().to_string())
            .filter(|b| !b.is_empty())
            .or_else(|| task.branch.clone())
            .unwrap_or_else(|| format!("bohay/{id}"));
        // Reuse an existing worktree instead of creating a second one: the one
        // recorded on the task (a restart, a closed pane), or — when the ledger
        // was reset but a leftover worktree still has this branch checked out
        // (git would refuse to create another) — *adopt* that worktree.
        let existing = task
            .worktree
            .as_ref()
            .map(std::path::PathBuf::from)
            .filter(|p| p.exists())
            .or_else(|| {
                crate::git::local::worktrees(&self.ws().cwd)
                    .ok()
                    .and_then(|wts| {
                        wts.into_iter()
                            .find(|w| {
                                !w.is_main
                                    && w.branch.as_deref() == Some(branch.as_str())
                                    && w.path.exists()
                            })
                            .map(|w| w.path)
                    })
            });
        let path = if let Some(path) = existing {
            // Reopen the worktree: focus its live pane if one is still running
            // there, otherwise open the folder as a fresh workspace.
            let live = self
                .panes
                .iter()
                .find(|(_, p)| p.cwd == path)
                .map(|(&pid, _)| pid);
            match live {
                Some(pid) => self.focus_pane_global(pid),
                None => {
                    self.create_workspace_at(path.clone());
                    if self.ws().cwd != path {
                        return Err((
                            "spawn_failed".to_string(),
                            "the worker pane didn't start".to_string(),
                        ));
                    }
                }
            }
            path
        } else {
            let repo = self.ws().cwd.clone();
            if !crate::git::local::is_repo(&repo) {
                return Err((
                    "not_a_repo".to_string(),
                    "task start needs a git repo (for worktree isolation) — run it from a repo workspace".to_string(),
                ));
            }
            // Create the worktree; `create_worktree` opens it as the active
            // workspace with a fresh worker pane.
            let path = self
                .create_worktree(&repo, &branch)
                .map_err(|e| ("git_error".to_string(), e))?;
            if self.ws().cwd != path {
                return Err((
                    "spawn_failed".to_string(),
                    "worktree created but the worker pane didn't start".to_string(),
                ));
            }
            path
        };
        let pane = self.layout().focus;

        // Claim + record the binding + lease the declared paths for the worker.
        // A started worker is *running* — claimed is reserved for the CLI's
        // claim-without-start, so the board never shows live work as waiting.
        self.orch
            .claim(id, pane.0)
            .map_err(|r| (r.code.to_string(), r.message))?;
        let _ = self.orch.set_status(id, crate::orch::TaskStatus::Running);
        self.orch
            .bind_worktree(id, Some(path.display().to_string()), Some(branch.clone()));
        if !task.paths.is_empty() {
            // Best-effort — the worker owns a brand-new worktree, so a lease here
            // only ever conflicts if another worker already reserved these paths.
            let _ = self
                .orch
                .acquire_lease(pane.0, id.to_string(), task.paths.clone());
        }
        if let Some(cmd) = agent {
            if let Some(p) = self.panes.get(&pane) {
                p.send(agent_launch_line(&cmd, &task).as_bytes());
                p.send(b"\r");
            }
        }
        self.orch.save();
        self.emit_event(
            "task.started",
            serde_json::json!({
                "id": id,
                "pane": pane.0.to_string(),
                "worktree": path.display().to_string(),
                "branch": branch,
            }),
        );
        Ok((pane, path))
    }

    /// Reconcile the ledger's pane bindings with the live panes. Called at
    /// startup: pane ids are reallocated every run, so `orch.json`'s saved
    /// assignees are stale — and can even *collide* with unrelated new panes.
    /// A worktree-backed task is rebound to the pane actually running in its
    /// worktree (or detached — it stays Running, the branch persists, `s`
    /// reopens it); a pure claim with no worktree loses its dead claimer and
    /// goes back to the queue.
    pub fn orch_reconcile(&mut self) {
        use crate::orch::TaskStatus;
        let pane_cwds: Vec<(u32, std::path::PathBuf)> = self
            .panes
            .iter()
            .map(|(id, p)| (id.0, p.cwd.clone()))
            .collect();
        let mut changed = false;
        let mut requeued: Vec<String> = Vec::new();
        for t in &mut self.orch.tasks {
            let active = matches!(t.status, TaskStatus::Claimed | TaskStatus::Running);
            if t.assignee.is_none() && !active {
                continue;
            }
            match t.worktree.as_deref().map(std::path::PathBuf::from) {
                Some(wt) => {
                    let live = pane_cwds.iter().find(|(_, c)| *c == wt).map(|(id, _)| *id);
                    if t.assignee != live {
                        t.assignee = live;
                        changed = true;
                    }
                }
                None => {
                    if t.assignee.is_some() || active {
                        t.assignee = None;
                        if active {
                            t.status = TaskStatus::Queued;
                            requeued.push(t.id.clone());
                        }
                        changed = true;
                    }
                }
            }
        }
        if changed {
            self.orch.save();
        }
        for id in requeued {
            self.emit_event("task.released", serde_json::json!({ "id": id }));
        }
    }

    /// A pane died/closed: detach any task bound to it so the board stays
    /// truthful. Worktree-backed work stays Running (the branch persists — `s`
    /// reopens it); a pure claim goes back to the queue.
    pub fn orch_unbind_pane(&mut self, pane: u32) {
        use crate::orch::TaskStatus;
        let mut requeued: Vec<String> = Vec::new();
        let mut changed = false;
        for t in &mut self.orch.tasks {
            if t.assignee != Some(pane) {
                continue;
            }
            t.assignee = None;
            if t.worktree.is_none() && matches!(t.status, TaskStatus::Claimed | TaskStatus::Running)
            {
                t.status = TaskStatus::Queued;
                requeued.push(t.id.clone());
            }
            changed = true;
        }
        if changed {
            self.orch.save();
        }
        for id in requeued {
            self.emit_event("task.released", serde_json::json!({ "id": id }));
        }
    }

    /// ORCH-6: integrate a finished task's branch into `bohay/integration`, in an
    /// **isolated integration worktree** (never the user's checkout). A clean merge
    /// lands on the integration branch; a conflict aborts, blocks the task, and
    /// reports the clashing files so its agent can resolve them in its own worktree.
    /// Serialized by the single-writer loop — one integration at a time.
    pub fn merge_task(&mut self, id: &str) -> Result<serde_json::Value, (String, String)> {
        use crate::orch::TaskStatus;
        let task = self
            .orch
            .task(id)
            .cloned()
            .ok_or_else(|| ("not_found".to_string(), format!("no such task: {id}")))?;
        let branch = task.branch.clone().ok_or_else(|| {
            (
                "no_branch".to_string(),
                format!("{id} has no branch — start a worker first with `task start`"),
            )
        })?;
        if !matches!(task.status, TaskStatus::Done | TaskStatus::Blocked) {
            return Err((
                "not_done".to_string(),
                format!("{id} isn't done yet (status: {})", task.status.as_str()),
            ));
        }
        // Operate on the task's own worktree repo (any worktree resolves the repo).
        let repo = task
            .worktree
            .as_ref()
            .map(std::path::PathBuf::from)
            .unwrap_or_else(|| self.ws().cwd.clone());
        if !crate::git::local::is_repo(&repo) {
            return Err((
                "not_a_repo".to_string(),
                "the task's repository is no longer available".to_string(),
            ));
        }
        let base = crate::git::local::default_branch(&repo);
        let repo_name = crate::git::local::worktrees(&repo)
            .ok()
            .and_then(|wts| {
                wts.into_iter()
                    .find(|w| w.is_main)
                    .map(|w| ws_name(&w.path))
            })
            .unwrap_or_else(|| ws_name(&repo));
        let integ_dir = crate::persist::config_dir()
            .join("worktrees")
            .join(&repo_name)
            .join("__integration");
        let integ_branch = "bohay/integration";

        let outcome =
            crate::git::local::integrate_branch(&repo, &integ_dir, integ_branch, &base, &branch)
                .map_err(|e| ("merge_error".to_string(), e))?;
        match outcome {
            crate::git::local::MergeOutcome::Merged => {
                let _ = self
                    .orch
                    .add_note(id, format!("merged {branch}{integ_branch}"));
                self.orch.save();
                self.emit_event(
                    "task.merged",
                    serde_json::json!({ "id": id, "branch": branch, "into": integ_branch }),
                );
                Ok(serde_json::json!({
                    "type": "merge",
                    "outcome": "merged",
                    "task": id,
                    "branch": branch,
                    "into": integ_branch,
                }))
            }
            crate::git::local::MergeOutcome::Conflict(files) => {
                let _ = self.orch.set_status(id, TaskStatus::Blocked);
                self.orch.save();
                self.emit_event(
                    "task.merge_conflict",
                    serde_json::json!({ "id": id, "branch": branch, "files": files.clone() }),
                );
                Ok(serde_json::json!({
                    "type": "merge",
                    "outcome": "conflict",
                    "task": id,
                    "branch": branch,
                    "files": files,
                }))
            }
        }
    }

    /// ORCH-5: complete a task. If it declares a `gate` command, run it **async**
    /// (in the task's worktree) — the loop stays responsive and the gate's result
    /// (`AppEvent::TaskGateFinished`) decides Done vs Review. Returns whether a gate
    /// was launched (so the caller can report "gate running" vs "done").
    pub fn complete_task(&mut self, id: &str) -> Result<bool, (String, String)> {
        let task = self
            .orch
            .task(id)
            .cloned()
            .ok_or_else(|| ("not_found".to_string(), format!("no such task: {id}")))?;
        // ORCH-5 compaction gate: a context-saturated worker must compact (or hand
        // off to a fresh agent) before its work is accepted, so a confused agent
        // doesn't finalize sloppy output.
        if let Some(ctx) = task.context {
            if ctx > crate::orch::COMPACTION_THRESHOLD {
                return Err((
                    "needs_compaction".to_string(),
                    format!(
                        "context at {:.0}% — run /compact (or hand off to a fresh agent) before finishing",
                        ctx * 100.0
                    ),
                ));
            }
        }
        let Some(gate) = task.gate.clone().filter(|g| !g.trim().is_empty()) else {
            self.finalize_task_done(id); // no gate → done immediately
            return Ok(false);
        };
        // Run the gate where the work is: the task's worktree, else its worker
        // pane's cwd, else the active workspace.
        let cwd = task
            .worktree
            .as_ref()
            .map(std::path::PathBuf::from)
            .or_else(|| {
                task.assignee
                    .and_then(|p| self.panes.get(&PaneId(p)).map(|pane| pane.cwd.clone()))
            })
            .unwrap_or_else(|| self.ws().cwd.clone());
        let _ = self.orch.set_status(id, crate::orch::TaskStatus::Running);
        self.orch.save();
        self.emit_event(
            "task.gate_running",
            serde_json::json!({ "id": id, "gate": gate }),
        );
        spawn_gate(id.to_string(), cwd, gate, self.app_tx.clone());
        Ok(true)
    }

    /// Apply a finished gate (ORCH-5): exit 0 → Done (+ dependents announced);
    /// non-zero → held at `Review` with the tail of the output captured.
    pub fn task_gate_finished(&mut self, id: &str, code: Option<i32>, out: String) {
        if code == Some(0) {
            self.finalize_task_done(id);
            self.emit_event("task.gate_passed", serde_json::json!({ "id": id }));
        } else {
            let _ = self.orch.set_status(id, crate::orch::TaskStatus::Review);
            let tail = tail_lines(&out, 20);
            let code_s = code
                .map(|c| c.to_string())
                .unwrap_or_else(|| "?".to_string());
            let _ = self
                .orch
                .add_output(id, format!("gate failed (exit {code_s}):\n{tail}"));
            self.orch.save();
            self.emit_event(
                "task.gate_failed",
                serde_json::json!({ "id": id, "code": code }),
            );
        }
    }

    /// Mark a task Done, release its leases, and announce any dependents that just
    /// became ready (ORCH-4). Shared by the no-gate path and a passing gate.
    fn finalize_task_done(&mut self, id: &str) {
        let _ = self.orch.set_status(id, crate::orch::TaskStatus::Done);
        self.orch.release_task_leases(id);
        let ready = self.orch.newly_ready(id);
        self.orch.save();
        let tj = self
            .orch
            .task(id)
            .and_then(|t| serde_json::to_value(t).ok())
            .unwrap_or(serde_json::Value::Null);
        self.emit_event("task.done", tj);
        for rid in ready {
            self.emit_event("task.ready", serde_json::json!({ "id": rid }));
        }
    }

    pub fn active_is_orch(&self) -> bool {
        self.workspaces
            .get(self.active_ws)
            .and_then(|w| w.tabs.get(w.active_tab))
            .is_some_and(Tab::is_orch)
    }

    /// Close the focused board tab (mirrors `close_git_tab`).
    pub fn close_orch_board(&mut self) {
        let at = self.ws().active_tab;
        if self.ws().tabs.get(at).is_some_and(Tab::is_orch) {
            let ws = &mut self.workspaces[self.active_ws];
            ws.tabs.remove(at);
            if ws.tabs.is_empty() {
                self.close_active_ws();
            } else if ws.active_tab >= ws.tabs.len() {
                ws.active_tab = ws.tabs.len() - 1;
            }
            self.session_dirty = true;
        }
    }

    /// Key handling while the board is focused. `j/k` move the task cursor; the
    /// action keys drive the selected task without touching the CLI:
    /// `s` start a worker · `d` done (runs its gate) · `m` merge · `⏎` jump to its
    /// pane · `x` release · `g/G` ends · `q` close.
    pub fn handle_orch_key(&mut self, key: KeyEvent) {
        let last = self.orch.tasks.len().saturating_sub(1);
        match key.code {
            KeyCode::Char('j') | KeyCode::Down => {
                self.orch_cursor = (self.orch_cursor + 1).min(last)
            }
            KeyCode::Char('k') | KeyCode::Up => {
                self.orch_cursor = self.orch_cursor.saturating_sub(1)
            }
            KeyCode::Char('g') | KeyCode::Home => self.orch_cursor = 0,
            KeyCode::Char('G') | KeyCode::End => self.orch_cursor = last,
            KeyCode::Char('a') | KeyCode::Char('n') => self.open_orch_form(),
            KeyCode::Char('s') => self.orch_action_start(),
            KeyCode::Char('d') => self.orch_action_done(),
            KeyCode::Char('m') => self.orch_action_merge(),
            KeyCode::Char('x') => self.orch_action_release(),
            KeyCode::Char('o') => self.orch_action_detail(),
            KeyCode::Char('D') | KeyCode::Delete => self.orch_action_delete(),
            KeyCode::Enter => self.orch_action_jump(),
            KeyCode::Char('q') => self.close_orch_board(),
            _ => {}
        }
    }

    // ── in-TUI new-task form (ORCH-7) ──────────────────────────────────────

    /// Open the new-task form (board `a`/`n`).
    pub fn open_orch_form(&mut self) {
        self.orch_form = Some(crate::app::OrchForm::default());
    }

    /// Key handling while the new-task form is open.
    pub fn handle_orch_form_key(&mut self, key: KeyEvent) {
        // Esc/Enter act on the whole form, so handle them before borrowing it.
        match key.code {
            KeyCode::Esc => {
                self.orch_form = None;
                return;
            }
            KeyCode::Enter => {
                self.submit_orch_form();
                return;
            }
            _ => {}
        }
        let Some(form) = self.orch_form.as_mut() else {
            return;
        };
        let n = crate::app::OrchForm::FIELDS;
        match key.code {
            KeyCode::Tab | KeyCode::Down => form.field = (form.field + 1) % n,
            KeyCode::BackTab | KeyCode::Up => form.field = (form.field + n - 1) % n,
            KeyCode::Backspace => {
                form.active_mut().pop();
            }
            KeyCode::Char(c) => form.active_mut().push(c),
            _ => {}
        }
    }

    /// Create the task from the form (title required; paths/deps whitespace-split).
    /// On error the form stays open showing why.
    fn submit_orch_form(&mut self) {
        let (title, paths, deps, gate) = {
            let Some(f) = self.orch_form.as_ref() else {
                return;
            };
            (
                f.title.trim().to_string(),
                f.paths
                    .split_whitespace()
                    .map(String::from)
                    .collect::<Vec<_>>(),
                f.deps
                    .split_whitespace()
                    .map(String::from)
                    .collect::<Vec<_>>(),
                {
                    let g = f.gate.trim();
                    (!g.is_empty()).then(|| g.to_string())
                },
            )
        };
        match self.orch.add_task(title, paths, deps, gate) {
            Ok(t) => {
                self.orch.save();
                let id = t.id.clone();
                self.emit_event(
                    "task.added",
                    serde_json::to_value(&t).unwrap_or(serde_json::Value::Null),
                );
                self.orch_form = None;
                self.orch_cursor = self.orch.tasks.len().saturating_sub(1); // select the new one
                self.show_toast(format!("added {id}"));
            }
            Err(r) => {
                if let Some(f) = self.orch_form.as_mut() {
                    f.error = Some(r.message);
                }
            }
        }
    }

    /// The task under the board cursor, if any.
    fn orch_selected_id(&self) -> Option<String> {
        self.orch.tasks.get(self.orch_cursor).map(|t| t.id.clone())
    }

    /// Board `s`: open the **start-worker picker** for the selected task, after
    /// pre-flight checks so the picker never opens for an unstartable task.
    fn orch_action_start(&mut self) {
        let Some(id) = self.orch_selected_id() else {
            return;
        };
        let Some(task) = self.orch.task(&id) else {
            return;
        };
        if task.assignee.is_some() {
            self.show_toast(format!("{id} already has a worker — ⏎ jumps to it"));
            return;
        }
        if !self.orch.ready(&id) {
            self.show_toast(format!("{id}: dependencies aren't done yet"));
            return;
        }
        self.orch_start = Some(crate::app::OrchStart {
            task: id,
            cursor: self.orch_last_agent.min(agent_choices().len() - 1),
        });
    }

    /// Key handling while the start-worker picker is open: `j/k` choose the
    /// agent, `⏎` starts the worker with it, `esc` cancels.
    pub fn handle_orch_start_key(&mut self, key: KeyEvent) {
        let n = agent_choices().len();
        match key.code {
            KeyCode::Esc => self.orch_start = None,
            KeyCode::Char('j') | KeyCode::Down | KeyCode::Tab => {
                if let Some(s) = self.orch_start.as_mut() {
                    s.cursor = (s.cursor + 1) % n;
                }
            }
            KeyCode::Char('k') | KeyCode::Up | KeyCode::BackTab => {
                if let Some(s) = self.orch_start.as_mut() {
                    s.cursor = (s.cursor + n - 1) % n;
                }
            }
            KeyCode::Enter => {
                if let Some(s) = self.orch_start.take() {
                    self.orch_last_agent = s.cursor;
                    let agent = agent_choices()[s.cursor].1.map(str::to_string);
                    self.start_worker_from_board(&s.task, agent);
                }
            }
            _ => {}
        }
    }

    /// Start a worker from the board and **stay on the board**: the worker
    /// spawns in the background, a toast confirms it, and `⏎` jumps into it
    /// when wanted — starting five workers is five keypresses, not five
    /// context switches.
    fn start_worker_from_board(&mut self, id: &str, agent: Option<String>) {
        let prev_ws = self.active_ws;
        let prev_tab = self.workspaces[prev_ws].active_tab;
        match self.task_start(id, None, agent) {
            Ok(_) => {
                self.active_ws = prev_ws;
                self.workspaces[prev_ws].active_tab = prev_tab;
                self.show_toast(format!("{id}: worker started — ⏎ to jump in"));
            }
            Err((_, msg)) => self.show_toast(msg),
        }
    }

    /// Board `o`: open the detail overlay for the selected task (branch,
    /// worktree, gate output, notes — the things you need when a gate fails).
    fn orch_action_detail(&mut self) {
        if let Some(id) = self.orch_selected_id() {
            self.orch_detail = Some(id);
            self.orch_detail_scroll = 0;
        }
    }

    /// Key handling while the task detail overlay is open.
    pub fn handle_orch_detail_key(&mut self, key: KeyEvent) {
        match key.code {
            KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('o') => self.orch_detail = None,
            KeyCode::Char('j') | KeyCode::Down => self.orch_detail_scroll += 1,
            KeyCode::Char('k') | KeyCode::Up => {
                self.orch_detail_scroll = self.orch_detail_scroll.saturating_sub(1)
            }
            _ => {}
        }
    }

    /// Board `D`: delete the selected task (the ledger refuses if it's active).
    fn orch_action_delete(&mut self) {
        let Some(id) = self.orch_selected_id() else {
            return;
        };
        match self.orch.delete_task(&id) {
            Ok(_) => {
                self.orch.save();
                self.emit_event("task.deleted", serde_json::json!({ "id": id }));
                self.orch_cursor = self
                    .orch_cursor
                    .min(self.orch.tasks.len().saturating_sub(1));
                self.show_toast(format!("{id} deleted"));
            }
            Err(r) => self.show_toast(r.message),
        }
    }

    fn orch_action_done(&mut self) {
        let Some(id) = self.orch_selected_id() else {
            return;
        };
        match self.complete_task(&id) {
            Ok(true) => self.show_toast(format!("{id}: gate running…")),
            Ok(false) => self.show_toast(format!("{id} done")),
            Err((_, msg)) => self.show_toast(msg),
        }
    }

    fn orch_action_merge(&mut self) {
        let Some(id) = self.orch_selected_id() else {
            return;
        };
        match self.merge_task(&id) {
            Ok(v) => {
                let outcome = v.get("outcome").and_then(|o| o.as_str()).unwrap_or("done");
                self.show_toast(format!("{id}: merge {outcome}"));
            }
            Err((_, msg)) => self.show_toast(msg),
        }
    }

    fn orch_action_release(&mut self) {
        let Some(id) = self.orch_selected_id() else {
            return;
        };
        match self.orch.release_task(&id) {
            Ok(_) => {
                self.orch.release_task_leases(&id);
                self.orch.save();
                self.emit_event("task.released", serde_json::json!({ "id": id }));
                self.show_toast(format!("{id} released"));
            }
            Err(r) => self.show_toast(r.message),
        }
    }

    /// Jump to the selected task's worker pane (if it has one).
    fn orch_action_jump(&mut self) {
        let task = self.orch.tasks.get(self.orch_cursor);
        let pane = task.and_then(|t| t.assignee).map(PaneId);
        let has_worktree = task.is_some_and(|t| t.worktree.is_some());
        match pane {
            Some(id) if self.panes.contains_key(&id) => self.focus_pane_global(id),
            _ if has_worktree => self.show_toast("no worker pane — press s to reopen its worktree"),
            _ => self.show_toast("no worker pane for this task"),
        }
    }

    /// Scroll the board (mouse wheel); moves the cursor so the selection follows.
    pub fn orch_scroll_by(&mut self, delta: i32) {
        let last = self.orch.tasks.len().saturating_sub(1);
        self.orch_cursor = if delta < 0 {
            self.orch_cursor.saturating_sub((-delta) as usize)
        } else {
            (self.orch_cursor + delta as usize).min(last)
        };
    }
}

/// Agents offered by the board's start-worker picker: (label, launch command).
/// `None` = plain shell, no agent. Every listed CLI accepts a positional prompt,
/// so the task briefing rides along as the agent's opening instruction.
pub fn agent_choices() -> &'static [(&'static str, Option<&'static str>)] {
    &[
        ("claude", Some("claude")),
        ("codex", Some("codex")),
        ("gemini", Some("gemini")),
        ("opencode", Some("opencode")),
        ("kimi", Some("kimi")),
        ("aider", Some("aider")),
        ("shell", None),
    ]
}

/// The briefing a worker agent starts with: what the task is, its boundaries,
/// its gate, and the contract for reporting back over the socket. One line —
/// it's typed into the worker's shell as a quoted argument.
fn task_briefing(task: &crate::orch::Task) -> String {
    let id = &task.id;
    let mut b = format!(
        "You are the worker for bohay task {id}: {}. This directory is your isolated git worktree.",
        task.title
    );
    if !task.paths.is_empty() {
        b.push_str(&format!(
            " Only touch these paths: {}.",
            task.paths.join(" ")
        ));
    }
    if let Some(g) = task.gate.as_deref().filter(|g| !g.trim().is_empty()) {
        b.push_str(&format!(" The quality gate is `{g}` — it must pass."));
    }
    if let Some(note) = task.notes.last() {
        b.push_str(&format!(" Note from earlier work: {note}."));
    }
    b.push_str(&format!(
        " When finished: commit all changes here, then run `bohay task done {id}`. \
         Report progress with `bohay task update {id} --note <text>` and context usage \
         with `bohay task heartbeat {id} --context <0..1>`."
    ));
    b
}

/// The full line typed into a fresh worker shell to launch `agent` with the
/// task briefing (and, on Unix, `BOHAY_TASK_ID` in the agent's environment).
fn agent_launch_line(agent: &str, task: &crate::orch::Task) -> String {
    let brief = shell_quote(&task_briefing(task));
    if cfg!(windows) {
        format!("{agent} {brief}")
    } else {
        format!("BOHAY_TASK_ID={} {agent} {brief}", task.id)
    }
}

/// Quote `s` as one shell argument: POSIX single-quoting on Unix; on Windows
/// (cmd/PowerShell have no safe common quoting) double quotes with inner
/// double quotes softened to single quotes.
fn shell_quote(s: &str) -> String {
    if cfg!(windows) {
        format!("\"{}\"", s.replace('"', "'"))
    } else {
        format!("'{}'", s.replace('\'', "'\\''"))
    }
}

/// Run a task's `gate` shell command async and report the result back to the loop
/// via `AppEvent::TaskGateFinished` (ORCH-5). Fire-and-forget; the app stays
/// responsive while a `cargo test` / `npm test` gate runs.
fn spawn_gate(
    task: String,
    cwd: std::path::PathBuf,
    gate: String,
    app_tx: std::sync::mpsc::Sender<crate::event::AppEvent>,
) {
    std::thread::spawn(move || {
        let (code, out) = run_gate_command(&cwd, &gate);
        let _ = app_tx.send(crate::event::AppEvent::TaskGateFinished { task, code, out });
    });
}

/// Run `gate` through the platform shell in `cwd`; returns its exit code and the
/// combined stdout+stderr.
fn run_gate_command(cwd: &std::path::Path, gate: &str) -> (Option<i32>, String) {
    use std::process::{Command, Stdio};
    let mut cmd = if cfg!(windows) {
        let mut c = Command::new("cmd");
        c.arg("/C").arg(gate);
        c
    } else {
        let mut c = Command::new("sh");
        c.arg("-c").arg(gate);
        c
    };
    cmd.current_dir(cwd)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    match cmd.output() {
        Ok(o) => {
            let mut s = String::from_utf8_lossy(&o.stdout).into_owned();
            s.push_str(&String::from_utf8_lossy(&o.stderr));
            (o.status.code(), s)
        }
        Err(e) => (None, format!("failed to run gate: {e}")),
    }
}

/// The last `n` lines of `s` (for capturing a failed gate's tail in a note).
fn tail_lines(s: &str, n: usize) -> String {
    let lines: Vec<&str> = s.lines().collect();
    let start = lines.len().saturating_sub(n);
    lines[start..].join("\n")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::event::AppEvent;
    use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    #[test]
    fn board_opens_focuses_and_closes() {
        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        let tabs_before = app.ws().tabs.len();

        app.open_orch_board();
        assert!(app.active_is_orch(), "board tab is active after open");
        assert_eq!(app.ws().tabs.len(), tabs_before + 1);

        // Re-opening focuses the existing board rather than adding another.
        app.open_orch_board();
        assert_eq!(app.ws().tabs.len(), tabs_before + 1);

        // `q` closes it.
        app.handle_event(AppEvent::Key(KeyEvent::new(
            KeyCode::Char('q'),
            KeyModifiers::NONE,
        )));
        assert!(!app.active_is_orch(), "board closed with q");
        assert_eq!(app.ws().tabs.len(), tabs_before);
    }

    #[test]
    fn wheel_scroll_moves_the_cursor_clamped() {
        let _env = crate::persist::test_env("boardscroll");
        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        for _ in 0..3 {
            app.orch.add_task("t".into(), vec![], vec![], None).unwrap();
        }
        app.open_orch_board();
        app.orch_scroll_by(-5);
        assert_eq!(app.orch_cursor, 0); // clamped at the top
        app.orch_scroll_by(2);
        assert_eq!(app.orch_cursor, 2);
        app.orch_scroll_by(5);
        assert_eq!(app.orch_cursor, 2); // clamped at the last task (index 2 of 3)
    }

    #[test]
    fn task_start_spawns_a_worktree_worker() {
        // ORCH-3: `task start` creates a worktree + pane, claims the task for it,
        // binds the branch, and leases the task's paths. Needs a real repo (with a
        // commit, since `git worktree add` requires one). `test_env` isolates
        // BOHAY_HOME so the worktree lands in a temp dir.
        let _env = crate::persist::test_env("orch3");
        let base = std::env::temp_dir().join(format!("bohay-orch3-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&base);
        let repo = base.join("repo");
        std::fs::create_dir_all(&repo).unwrap();
        let git = |args: &[&str]| {
            std::process::Command::new("git")
                .args(args)
                .current_dir(&repo)
                .output()
                .unwrap();
        };
        git(&["init", "-q", "-b", "main"]);
        git(&[
            "-c",
            "user.email=t@t",
            "-c",
            "user.name=t",
            "commit",
            "-q",
            "--allow-empty",
            "-m",
            "init",
        ]);

        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        app.create_workspace_at(repo.clone()); // the repo becomes the active workspace
        app.orch
            .add_task("auth".into(), vec!["src/auth/**".into()], vec![], None)
            .unwrap();

        let (pane, path) = app.task_start("t1", None, None).expect("worker starts");

        // The worker's worktree is now the active workspace, under our managed dir.
        assert_eq!(app.ws().cwd, path);
        assert!(path.starts_with(crate::persist::config_dir().join("worktrees")));
        // The task is running in the worker pane and bound to its branch/worktree.
        let t = app.orch.task("t1").unwrap();
        assert_eq!(t.status, crate::orch::TaskStatus::Running);
        assert_eq!(t.assignee, Some(pane.0));
        assert_eq!(t.branch.as_deref(), Some("bohay/t1"));
        assert!(t.worktree.is_some());
        // Its declared paths were auto-leased for the worker.
        assert!(app
            .orch
            .leases
            .iter()
            .any(|l| l.pane == pane.0 && l.task == "t1"));

        // Starting again is rejected — it's already claimed.
        assert_eq!(
            app.task_start("t1", None, None).unwrap_err().0,
            "already_claimed"
        );

        let _ = std::fs::remove_dir_all(&base);
    }

    #[test]
    fn start_picker_opens_moves_and_cancels() {
        let _env = crate::persist::test_env("orchpick");
        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        app.orch.add_task("x".into(), vec![], vec![], None).unwrap();
        app.open_orch_board();
        let k = |c| KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE);

        // `s` opens the picker for the selected task.
        app.handle_orch_key(k('s'));
        let start = app.orch_start.as_ref().expect("picker opens");
        assert_eq!(start.task, "t1");

        // j/k move the agent cursor; esc cancels without starting anything.
        app.handle_orch_start_key(k('j'));
        assert_eq!(app.orch_start.as_ref().unwrap().cursor, 1);
        app.handle_orch_start_key(k('k'));
        assert_eq!(app.orch_start.as_ref().unwrap().cursor, 0);
        app.handle_orch_start_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
        assert!(app.orch_start.is_none());
        assert_eq!(
            app.orch.task("t1").unwrap().status,
            crate::orch::TaskStatus::Queued
        );

        // `s` on a task with unmet deps never opens the picker.
        app.orch
            .add_task("y".into(), vec![], vec!["t1".into()], None)
            .unwrap();
        app.handle_orch_key(k('j'));
        app.handle_orch_key(k('s'));
        assert!(app.orch_start.is_none(), "deps unmet — toast, no picker");
    }

    #[test]
    fn picker_start_stays_on_the_board() {
        // Full flow on a real repo: `s` → pick "shell" → ⏎ spawns the worker,
        // marks the task Running, and keeps the board focused.
        let _env = crate::persist::test_env("orchstay");
        let base = std::env::temp_dir().join(format!("bohay-orchstay-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&base);
        let repo = base.join("repo");
        std::fs::create_dir_all(&repo).unwrap();
        let git = |args: &[&str]| {
            std::process::Command::new("git")
                .args(args)
                .current_dir(&repo)
                .output()
                .unwrap();
        };
        git(&["init", "-q", "-b", "main"]);
        git(&[
            "-c",
            "user.email=t@t",
            "-c",
            "user.name=t",
            "commit",
            "-q",
            "--allow-empty",
            "-m",
            "init",
        ]);

        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        app.create_workspace_at(repo.clone());
        app.orch.add_task("x".into(), vec![], vec![], None).unwrap();
        app.open_orch_board();
        let k = |c| KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE);

        app.handle_orch_key(k('s'));
        // Select the "shell" choice (last row) and confirm.
        let last = agent_choices().len() - 1;
        if let Some(s) = app.orch_start.as_mut() {
            s.cursor = last;
        }
        app.handle_orch_start_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));

        let t = app.orch.task("t1").unwrap();
        assert_eq!(t.status, crate::orch::TaskStatus::Running);
        assert!(t.assignee.is_some());
        assert!(app.active_is_orch(), "the board keeps focus after start");
        assert_eq!(app.orch_last_agent, last, "the picker remembers the choice");

        let _ = std::fs::remove_dir_all(&base);
    }

    #[test]
    fn task_start_adopts_a_leftover_worktree_for_its_branch() {
        // The reported failure mode: the ledger was reset (fresh t1) but a
        // worktree from an earlier run still has `bohay/t1` checked out — git
        // refuses a second worktree for the branch, so starting kept failing
        // and the task sat at queued. Now the leftover worktree is adopted.
        let _env = crate::persist::test_env("orchadopt");
        let base = std::env::temp_dir().join(format!("bohay-orchadopt-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&base);
        let repo = base.join("repo");
        std::fs::create_dir_all(&repo).unwrap();
        let git = |args: &[&str]| {
            std::process::Command::new("git")
                .args(args)
                .current_dir(&repo)
                .output()
                .unwrap();
        };
        git(&["init", "-q", "-b", "main"]);
        git(&[
            "-c",
            "user.email=t@t",
            "-c",
            "user.name=t",
            "commit",
            "-q",
            "--allow-empty",
            "-m",
            "init",
        ]);
        // The leftover: a worktree with bohay/t1 checked out, unknown to the ledger.
        let leftover = base.join("leftover-wt");
        git(&[
            "worktree",
            "add",
            "-q",
            "-b",
            "bohay/t1",
            leftover.to_str().unwrap(),
        ]);

        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        app.create_workspace_at(repo.clone());
        app.orch
            .add_task("auth".into(), vec![], vec![], None)
            .unwrap();

        let (_, path) = app.task_start("t1", None, None).expect("start adopts");
        assert_eq!(
            path.canonicalize().unwrap(),
            leftover.canonicalize().unwrap(),
            "the existing worktree is reused, not duplicated"
        );
        let t = app.orch.task("t1").unwrap();
        assert_eq!(t.status, crate::orch::TaskStatus::Running);
        assert_eq!(t.branch.as_deref(), Some("bohay/t1"));

        let _ = std::fs::remove_dir_all(&base);
    }

    #[test]
    fn reconcile_rebinds_worktree_tasks_and_requeues_dead_claims() {
        let _env = crate::persist::test_env("orchrec");
        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        let live = *app.panes.keys().next().unwrap();
        let live_cwd = app.panes[&live].cwd.display().to_string();

        // t1: worktree-backed, bound to a stale pane id → rebound to the live
        // pane actually running in that folder.
        app.orch.add_task("a".into(), vec![], vec![], None).unwrap();
        app.orch.claim("t1", 9999).unwrap();
        app.orch
            .set_status("t1", crate::orch::TaskStatus::Running)
            .unwrap();
        app.orch
            .bind_worktree("t1", Some(live_cwd), Some("bohay/t1".into()));
        // t2: worktree-backed but its folder has no pane → detached, stays Running.
        app.orch.add_task("b".into(), vec![], vec![], None).unwrap();
        app.orch.claim("t2", 9998).unwrap();
        app.orch
            .set_status("t2", crate::orch::TaskStatus::Running)
            .unwrap();
        app.orch.bind_worktree(
            "t2",
            Some("/nonexistent/worktree".into()),
            Some("bohay/t2".into()),
        );
        // t3: a pure claim (no worktree) by a dead pane → back to the queue.
        app.orch.add_task("c".into(), vec![], vec![], None).unwrap();
        app.orch.claim("t3", 9997).unwrap();

        app.orch_reconcile();

        let t1 = app.orch.task("t1").unwrap();
        assert_eq!(t1.assignee, Some(live.0), "rebound to the live pane");
        assert_eq!(t1.status, crate::orch::TaskStatus::Running);
        let t2 = app.orch.task("t2").unwrap();
        assert_eq!(t2.assignee, None, "detached — no pane in its worktree");
        assert_eq!(t2.status, crate::orch::TaskStatus::Running, "work persists");
        let t3 = app.orch.task("t3").unwrap();
        assert_eq!(t3.assignee, None);
        assert_eq!(t3.status, crate::orch::TaskStatus::Queued, "requeued");
    }

    #[test]
    fn closing_a_worker_pane_detaches_its_task() {
        let _env = crate::persist::test_env("orchclose");
        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        let pane = *app.panes.keys().next().unwrap();

        // A worktree-backed worker: closing its pane detaches but stays Running.
        app.orch.add_task("a".into(), vec![], vec![], None).unwrap();
        app.orch.claim("t1", pane.0).unwrap();
        app.orch
            .set_status("t1", crate::orch::TaskStatus::Running)
            .unwrap();
        app.orch
            .bind_worktree("t1", Some("/tmp/wt".into()), Some("bohay/t1".into()));
        // A pure claim by the same pane: closing requeues it.
        app.orch.add_task("b".into(), vec![], vec![], None).unwrap();
        app.orch.claim("t2", pane.0).unwrap();

        app.close_pane(pane);

        let t1 = app.orch.task("t1").unwrap();
        assert_eq!(t1.assignee, None);
        assert_eq!(t1.status, crate::orch::TaskStatus::Running);
        let t2 = app.orch.task("t2").unwrap();
        assert_eq!(t2.assignee, None);
        assert_eq!(t2.status, crate::orch::TaskStatus::Queued);
    }

    #[test]
    fn detail_overlay_opens_scrolls_and_closes() {
        let _env = crate::persist::test_env("orchdetail");
        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        app.orch.add_task("x".into(), vec![], vec![], None).unwrap();
        app.open_orch_board();
        let k = |c| KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE);

        app.handle_orch_key(k('o'));
        assert_eq!(app.orch_detail.as_deref(), Some("t1"));
        app.handle_orch_detail_key(k('j'));
        assert_eq!(app.orch_detail_scroll, 1);
        app.handle_orch_detail_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
        assert!(app.orch_detail.is_none());
    }

    #[test]
    fn board_delete_removes_selected_queued_task() {
        let _env = crate::persist::test_env("orchdel");
        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        app.orch.add_task("a".into(), vec![], vec![], None).unwrap();
        app.orch.add_task("b".into(), vec![], vec![], None).unwrap();
        app.open_orch_board();
        let k = |c| KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE);

        app.handle_orch_key(k('j')); // select t2
        app.handle_orch_key(k('D'));
        assert!(app.orch.task("t2").is_none());
        assert_eq!(app.orch_cursor, 0, "cursor clamped after delete");
    }

    #[test]
    fn agent_launch_line_is_one_quoted_line_with_the_contract() {
        let mut s = crate::orch::OrchState::default();
        let t = s
            .add_task(
                "fix the auth's bug".into(),
                vec!["src/auth/**".into()],
                vec![],
                Some("cargo test auth".into()),
            )
            .unwrap();
        let line = agent_launch_line("claude", &t);
        assert!(!line.contains('\n'), "typed into a shell — one line");
        assert!(line.contains("claude"));
        assert!(line.contains("bohay task done t1"));
        assert!(line.contains("cargo test auth"));
        if !cfg!(windows) {
            assert!(line.starts_with("BOHAY_TASK_ID=t1 "));
            // The apostrophe in the title survives POSIX single-quoting.
            assert!(line.contains(r"auth'\''s"));
        }
    }

    #[test]
    fn gate_command_runner_reports_exit_and_output() {
        let dir = std::env::temp_dir();
        assert_eq!(run_gate_command(&dir, "exit 0").0, Some(0));
        assert_eq!(run_gate_command(&dir, "exit 7").0, Some(7));
        let (code, out) = run_gate_command(&dir, "echo hello");
        assert_eq!(code, Some(0));
        assert!(out.contains("hello"));
    }

    #[test]
    fn no_gate_completes_immediately() {
        let _env = crate::persist::test_env("gate0");
        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        app.orch.add_task("x".into(), vec![], vec![], None).unwrap();
        assert_eq!(app.complete_task("t1"), Ok(false));
        assert_eq!(
            app.orch.task("t1").unwrap().status,
            crate::orch::TaskStatus::Done
        );
    }

    #[test]
    fn gate_pass_marks_done_and_gate_fail_holds_at_review() {
        let _env = crate::persist::test_env("gate1");
        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        let focus = app.layout().focus;

        // A gated task: `done` launches the gate async and holds at Running.
        app.orch
            .add_task("x".into(), vec![], vec![], Some("true".into()))
            .unwrap();
        app.orch.claim("t1", focus.0).unwrap();
        assert_eq!(app.complete_task("t1"), Ok(true));
        assert_eq!(
            app.orch.task("t1").unwrap().status,
            crate::orch::TaskStatus::Running
        );
        // A passing gate finalizes it to Done.
        app.task_gate_finished("t1", Some(0), String::new());
        assert_eq!(
            app.orch.task("t1").unwrap().status,
            crate::orch::TaskStatus::Done
        );

        // A failing gate holds the task at Review and records the output.
        app.orch
            .add_task("y".into(), vec![], vec![], Some("false".into()))
            .unwrap();
        app.task_gate_finished("t2", Some(1), "boom\n".into());
        let t2 = app.orch.task("t2").unwrap();
        assert_eq!(t2.status, crate::orch::TaskStatus::Review);
        assert!(t2.outputs.iter().any(|o| o.contains("gate failed")));
    }

    #[test]
    fn board_cursor_navigates_and_acts_on_the_selected_task() {
        let _env = crate::persist::test_env("boardui");
        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        app.orch.add_task("a".into(), vec![], vec![], None).unwrap(); // t1
        app.orch.add_task("b".into(), vec![], vec![], None).unwrap(); // t2
        app.open_orch_board();
        let k = |c| KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE);

        // j/k move the selection cursor.
        assert_eq!(app.orch_cursor, 0);
        app.handle_orch_key(k('j'));
        assert_eq!(app.orch_cursor, 1);
        app.handle_orch_key(k('k'));
        assert_eq!(app.orch_cursor, 0);

        // `d` completes the selected (no-gate) task straight from the UI.
        app.handle_orch_key(k('d'));
        assert_eq!(
            app.orch.task("t1").unwrap().status,
            crate::orch::TaskStatus::Done
        );

        // Select t2, claim it, then `x` releases it — all without the CLI.
        app.handle_orch_key(k('j'));
        app.orch.claim("t2", 1).unwrap();
        app.handle_orch_key(k('x'));
        assert_eq!(app.orch.task("t2").unwrap().assignee, None);
    }

    #[test]
    fn new_task_form_creates_a_task_from_the_ui() {
        let _env = crate::persist::test_env("orchform");
        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        app.open_orch_board();
        let k = |c| KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE);

        // `a` on the board opens the form.
        app.handle_orch_key(k('a'));
        assert!(app.orch_form.is_some());

        // Type a title, Tab to Paths, type a glob, then submit with Enter.
        for c in "auth".chars() {
            app.handle_orch_form_key(k(c));
        }
        app.handle_orch_form_key(KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE));
        for c in "src/auth/**".chars() {
            app.handle_orch_form_key(k(c));
        }
        app.handle_orch_form_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));

        assert!(
            app.orch_form.is_none(),
            "form closes after a successful submit"
        );
        let t = app.orch.task("t1").expect("task was created from the UI");
        assert_eq!(t.title, "auth");
        assert_eq!(t.paths, vec!["src/auth/**".to_string()]);
    }

    #[test]
    fn new_task_form_requires_a_title() {
        let _env = crate::persist::test_env("orchform2");
        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        app.open_orch_form();
        // Submitting an empty title keeps the form open with an error.
        app.handle_orch_form_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
        assert!(app.orch_form.as_ref().is_some_and(|f| f.error.is_some()));
        assert!(app.orch.tasks.is_empty());
    }

    #[test]
    fn saturated_context_blocks_completion() {
        let _env = crate::persist::test_env("gate2");
        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = App::new(80, 24, tx).unwrap();
        app.orch.add_task("x".into(), vec![], vec![], None).unwrap();
        // Over the compaction threshold → done is refused.
        app.orch.heartbeat("t1", 0.92).unwrap();
        assert_eq!(app.complete_task("t1").unwrap_err().0, "needs_compaction");
        assert_ne!(
            app.orch.task("t1").unwrap().status,
            crate::orch::TaskStatus::Done
        );
        // After compacting (context drops), it completes.
        app.orch.heartbeat("t1", 0.4).unwrap();
        assert_eq!(app.complete_task("t1"), Ok(false));
        assert_eq!(
            app.orch.task("t1").unwrap().status,
            crate::orch::TaskStatus::Done
        );
    }
}