heddle-cli 0.8.0

An AI-native version control system
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
// SPDX-License-Identifier: Apache-2.0
//! Stable JSON-first agent reservation API.

use std::{collections::BTreeSet, path::PathBuf};

use anyhow::{Result, anyhow};
use chrono::Utc;
use objects::{
    object::ThreadName,
    store::{
        AgentEntry, AgentRegistry, AgentStatus, AgentTaskRecord, AgentTaskStatus, AgentTaskStore,
        AgentUsageSummary, ObjectStore, ReserveOutcome, current_boot_id, validate_task_id,
    },
};
use oplog::OpLogRecorder;
use refs::{Head, RefExpectation};
use repo::{
    Repository, Thread, ThreadConfidenceSummary, ThreadFreshness, ThreadId,
    ThreadIntegrationPolicy, ThreadManager, ThreadMode, ThreadState, ThreadVerificationSummary,
};
use schemars::JsonSchema;
use serde::Serialize;

use super::{
    advice::RecoveryAdvice,
    git_overlay_health::{
        GitOverlayMutationPreflight, RepositoryVerificationState,
        build_repository_verification_state, git_overlay_mutation_preflight_advice,
    },
    thread::thread_name_invalid_advice,
    worktree_cmd::helpers::plan_worktree_target,
    worktree_safety::ensure_worktree_clean,
};
use crate::cli::{
    Cli,
    cli_args::{
        AgentApiListArgs, AgentFanoutCommands, AgentFanoutPlanArgs, AgentFanoutStartArgs,
        AgentHeartbeatArgs, AgentReleaseArgs, AgentReleaseStatusArg, AgentReserveArgs,
        AgentTaskCommands, AgentTaskCreateArgs, AgentTaskListArgs, AgentTaskShowArgs,
        AgentTaskStatusArg, AgentTaskUpdateArgs, ThreadStartArgs, WorkspaceModeArg,
    },
    render::shell_quote,
    should_output_json,
};

#[derive(Serialize, JsonSchema)]
pub struct AgentReservationOutput {
    pub session_id: String,
    pub reservation_token: Option<String>,
    pub thread: String,
    pub anchor_state: Option<String>,
    pub anchor_root: Option<String>,
    pub task_assignment_id: Option<String>,
    /// Lifecycle status as a stable kebab-case string
    /// (`active|abandoned|complete|merged`). Mirrors
    /// `objects::store::AgentStatus` but kept as a `String` here so
    /// the schema lives entirely in the CLI crate.
    pub status: String,
    pub path: Option<String>,
    pub task: Option<String>,
    pub provider: Option<String>,
    pub model: Option<String>,
    pub harness: Option<String>,
    pub thinking_level: Option<String>,
    pub probe_source: Option<String>,
    pub probe_confidence: Option<f32>,
}

#[derive(Serialize, JsonSchema)]
pub(crate) struct AgentReservationEnvelope {
    pub reservation: AgentReservationOutput,
    #[allow(dead_code)]
    #[serde(skip_serializing)]
    #[serde(rename = "verification")]
    pub trust: RepositoryVerificationState,
}

#[derive(Serialize, JsonSchema)]
pub(crate) struct AgentReservationListOutput {
    pub reservations: Vec<AgentReservationOutput>,
    pub alive_only: bool,
    pub thread: Option<String>,
    #[serde(rename = "verification")]
    pub trust: RepositoryVerificationState,
}

#[derive(Serialize, JsonSchema)]
pub(crate) struct AgentTaskEnvelope {
    pub output_kind: &'static str,
    pub task: AgentTaskOutput,
    #[serde(rename = "verification")]
    pub trust: RepositoryVerificationState,
}

#[derive(Serialize, JsonSchema)]
pub(crate) struct AgentTaskListOutput {
    pub output_kind: &'static str,
    pub tasks: Vec<AgentTaskOutput>,
    pub thread: Option<String>,
    pub status: Option<String>,
    #[serde(rename = "verification")]
    pub trust: RepositoryVerificationState,
}

#[derive(Serialize, JsonSchema)]
pub(crate) struct AgentTaskOutput {
    pub schema_version: u32,
    pub task_id: String,
    pub title: String,
    pub body: String,
    pub status: String,
    pub target_thread: String,
    pub base_state: Option<String>,
    pub base_root: Option<String>,
    pub parent_task_id: Option<String>,
    pub coordination_discussion_id: Option<String>,
    pub allow_offline: bool,
    pub delegated_by: Option<String>,
    pub created_at: String,
    pub updated_at: String,
    pub completed_at: Option<String>,
}

#[derive(Serialize, JsonSchema)]
pub(crate) struct AgentFanoutOutput {
    pub output_kind: &'static str,
    pub title: String,
    pub parent_thread: String,
    pub base_state: String,
    pub base_root: String,
    pub coordination_discussion_id: Option<String>,
    pub parent_task: Option<AgentTaskOutput>,
    pub lanes: Vec<AgentFanoutLaneOutput>,
    pub commands: Vec<AgentFanoutCommandOutput>,
    #[serde(rename = "verification")]
    pub trust: RepositoryVerificationState,
}

#[derive(Serialize, JsonSchema)]
pub(crate) struct AgentFanoutLaneOutput {
    pub thread: String,
    pub path: String,
    pub title: String,
    pub task: Option<AgentTaskOutput>,
    pub session_id: Option<String>,
    pub status: String,
}

#[derive(Serialize, JsonSchema)]
pub(crate) struct AgentFanoutCommandOutput {
    pub lane_thread: String,
    pub command: String,
    pub argv: Vec<String>,
}

impl From<&AgentEntry> for AgentReservationOutput {
    fn from(entry: &AgentEntry) -> Self {
        Self {
            session_id: entry.session_id.clone(),
            reservation_token: entry.reservation_token.clone(),
            thread: entry.thread.clone(),
            anchor_state: entry.anchor_state.clone(),
            anchor_root: entry.anchor_root.clone(),
            task_assignment_id: entry.task_assignment_id.clone(),
            status: entry.status.to_string(),
            path: entry.path.as_ref().map(|path| path.display().to_string()),
            task: entry.attach_reason.clone(),
            provider: entry.provider.clone(),
            model: entry.model.clone(),
            harness: entry.harness.clone(),
            thinking_level: entry.thinking_level.clone(),
            probe_source: entry.probe_source.clone(),
            probe_confidence: entry.probe_confidence,
        }
    }
}

impl From<&AgentTaskRecord> for AgentTaskOutput {
    fn from(task: &AgentTaskRecord) -> Self {
        Self {
            schema_version: task.schema_version,
            task_id: task.task_id.clone(),
            title: task.title.clone(),
            body: task.body.clone(),
            status: task.status.to_string(),
            target_thread: task.target_thread.clone(),
            base_state: task.base_state.clone(),
            base_root: task.base_root.clone(),
            parent_task_id: task.parent_task_id.clone(),
            coordination_discussion_id: task.coordination_discussion_id.clone(),
            allow_offline: task.allow_offline,
            delegated_by: task.delegated_by.clone(),
            created_at: task.created_at.to_rfc3339(),
            updated_at: task.updated_at.to_rfc3339(),
            completed_at: task.completed_at.map(|time| time.to_rfc3339()),
        }
    }
}

fn live_owner_conflict_advice(
    thread: &str,
    requested_anchor_full: &str,
    owner: &AgentEntry,
) -> RecoveryAdvice {
    let kind = if owner.anchor_state.as_deref() == Some(requested_anchor_full) {
        "live_owner"
    } else {
        "anchor_drift"
    };
    let primary_command = format!("heddle thread show {thread}");
    if kind == "live_owner" {
        RecoveryAdvice::safety_refusal(
            "live_owner",
            format!(
                "thread '{thread}' already has a live reservation on session '{}'",
                owner.session_id
            ),
            format!(
                "Inspect it with `{primary_command}`, or release that session before starting another writer."
            ),
            format!(
                "thread '{thread}' is reserved by live session '{}' at anchor {}",
                owner.session_id,
                owner.anchor_state.as_deref().unwrap_or("<unknown>")
            ),
            "starting another writer could create competing histories for the same thread",
            "no thread refs or reservation records were changed",
            primary_command.clone(),
            vec![primary_command],
        )
    } else {
        RecoveryAdvice::safety_refusal(
            "anchor_drift",
            format!(
                "thread '{thread}' is reserved by session '{}' on anchor {}, but reservation requested {requested_anchor_full}",
                owner.session_id,
                owner.anchor_state.as_deref().unwrap_or("<unknown>")
            ),
            "Refresh the thread or rebase before retrying.".to_string(),
            format!("thread '{thread}' has an active reservation at a different anchor"),
            "starting from the requested anchor could fork the same thread name into competing histories",
            "no thread refs or reservation records were changed",
            primary_command.clone(),
            vec![primary_command],
        )
    }
}

fn anchor_drift_no_owner_advice(
    thread: &str,
    requested_anchor_full: &str,
    reserved_anchor: &str,
) -> RecoveryAdvice {
    let primary_command = format!("heddle thread show {thread}");
    RecoveryAdvice::safety_refusal(
        "anchor_drift",
        format!(
            "thread '{thread}' is anchored at {reserved_anchor}, but reservation requested {requested_anchor_full}"
        ),
        "Refresh the thread or rebase before retrying.".to_string(),
        format!("thread '{thread}' already points at a different anchor"),
        "starting from the requested anchor could fork the same thread name into competing histories",
        "no thread refs or reservation records were changed",
        primary_command.clone(),
        vec![primary_command],
    )
}

fn agent_task_not_found_advice(task_id: &str) -> RecoveryAdvice {
    RecoveryAdvice::safety_refusal(
        "agent_task_not_found",
        format!("agent task '{task_id}' not found"),
        "Create the task locally, or reserve without --task-id if no task assignment exists.",
        format!("no task record exists for {task_id}"),
        "continuing would attach an unverifiable task provenance id to the reservation",
        "no thread refs or reservation records were changed",
        format!("heddle agent task show {task_id}"),
        vec![format!("heddle agent task show {task_id}")],
    )
}

fn agent_task_mismatch_advice(task_id: &str, message: String) -> RecoveryAdvice {
    RecoveryAdvice::safety_refusal(
        "agent_task_mismatch",
        message.clone(),
        "Reserve the task on its target thread and base, or update the local task record first.",
        message,
        "continuing would attach task provenance to work outside the delegated target",
        "no thread refs or reservation records were changed",
        format!("heddle agent task show {task_id}"),
        vec![format!("heddle agent task show {task_id}")],
    )
}

fn load_task_for_reservation(
    repo: &Repository,
    task_id: &str,
    thread_name: &str,
    anchor_full: &str,
    anchor_short: &str,
    anchor_root: &str,
) -> Result<AgentTaskRecord> {
    validate_task_id(task_id).map_err(|err| anyhow!(err))?;
    let store = AgentTaskStore::new(repo.heddle_dir());
    let task = store
        .load(task_id)?
        .ok_or_else(|| anyhow!(agent_task_not_found_advice(task_id)))?;
    if task.target_thread != thread_name {
        return Err(anyhow!(agent_task_mismatch_advice(
            task_id,
            format!(
                "agent task '{task_id}' targets thread '{}', but reservation requested '{}'",
                task.target_thread, thread_name
            ),
        )));
    }
    if let Some(base_state) = task.base_state.as_deref()
        && base_state != anchor_full
        && base_state != anchor_short
    {
        return Err(anyhow!(agent_task_mismatch_advice(
            task_id,
            format!(
                "agent task '{task_id}' base_state is {base_state}, but reservation anchor is {anchor_full}"
            ),
        )));
    }
    if let Some(base_root) = task.base_root.as_deref()
        && base_root != anchor_root
    {
        return Err(anyhow!(agent_task_mismatch_advice(
            task_id,
            format!(
                "agent task '{task_id}' base_root is {base_root}, but reservation anchor root is {anchor_root}"
            ),
        )));
    }
    Ok(task)
}

pub fn cmd_agent_reserve(cli: &Cli, args: AgentReserveArgs) -> Result<()> {
    // User/external creation boundary: `agent reserve` persists a thread
    // record, so reject an unsafe thread name here too (same early-reject
    // rule as `start_thread` / `thread create`). (heddle#464 close-the-class.)
    ThreadId::new(args.thread.as_str()).map_err(|err| anyhow!(thread_name_invalid_advice(&err)))?;
    let repo = cli.open_repo()?;
    let anchor = match &args.anchor {
        Some(spec) => repo
            .resolve_state(spec)?
            .ok_or_else(|| anyhow!("anchor state '{}' not found", spec))?,
        None => repo
            .head()?
            .ok_or_else(|| anyhow!("repository has no HEAD state to reserve from"))?,
    };
    let anchor_root = repo
        .store()
        .get_state(&anchor)?
        .map(|state| state.tree.short())
        .unwrap_or_default();
    let anchor_full = anchor.to_string_full();
    let anchor_short = anchor.short();
    let thread_name = args.thread.clone();
    let task_record = match args.task_id.as_deref() {
        Some(task_id) => Some(load_task_for_reservation(
            &repo,
            task_id,
            &thread_name,
            &anchor_full,
            &anchor_short,
            &anchor_root,
        )?),
        None => None,
    };

    // Hard pre-check: a thread ref already pointing at a different
    // state without any live owner is an anchor-drift case the caller
    // must resolve before we hand them a fresh reservation. We surface
    // it here (rather than letting set_thread_cas fail later) so the
    // standard JSON error envelope can describe the conflict.
    let existing_ref = repo.refs().get_thread(&ThreadName::new(&thread_name))?;
    if let Some(existing) = existing_ref
        && existing != anchor
    {
        // Look for a live owner first — if one exists, route through
        // the shared reservation advice so the caller sees the owner's
        // session_id alongside the drift in the standard error envelope.
        let registry = AgentRegistry::new(repo.heddle_dir());
        registry.reap_dead_for_thread(&thread_name)?;
        if let Some(owner) = registry
            .list()?
            .into_iter()
            .find(|entry| entry.status == AgentStatus::Active && entry.thread == thread_name)
        {
            return Err(anyhow!(live_owner_conflict_advice(
                &thread_name,
                &anchor_full,
                &owner
            )));
        }
        return Err(anyhow!(anchor_drift_no_owner_advice(
            &thread_name,
            &anchor_full,
            &existing.to_string_full(),
        )));
    }

    let registry = AgentRegistry::new(repo.heddle_dir());
    let task = args.task.clone();
    let task_assignment_id = task_record.as_ref().map(|task| task.task_id.clone());
    let anchor_full_for_entry = anchor_full.clone();
    let anchor_short = anchor.short();
    let reservation_path = existing_thread_execution_path(&repo, &thread_name)?;
    let probe = crate::harness::probe_current_process_harness(
        &repo,
        std::env::var("HEDDLE_AGENT_PROVIDER")
            .ok()
            .and_then(crate::attribution::clean_attribution_value),
        std::env::var("HEDDLE_AGENT_MODEL")
            .ok()
            .and_then(crate::attribution::clean_attribution_value),
        std::env::var("HEDDLE_AGENT_POLICY")
            .ok()
            .and_then(crate::attribution::clean_attribution_value),
    )?;
    // `--hold-for-pid PID` binds the reservation to an external
    // process (typically the orchestrator that wraps the heddle
    // CLI). Without it we record this one-shot CLI's pid, which
    // exits before the next liveness check — fine when the calling
    // script doesn't care about reaping, but means the dead-pid
    // reaper would recycle the reservation immediately if a second
    // agent races in. With `--hold-for-pid` the reservation tracks
    // the orchestrator's lifetime instead.
    let recorded_pid = args.hold_for_pid.unwrap_or_else(std::process::id);
    let outcome = registry.try_reserve_thread(&thread_name, |session_id| {
        Ok(AgentEntry {
            session_id: session_id.to_string(),
            client_instance_id: None,
            native_actor_key: None,
            native_parent_actor_key: None,
            native_instance_key: None,
            heddle_session_id: None,
            thread_id: Some(thread_name.clone()),
            thread: thread_name.clone(),
            pid: Some(recorded_pid),
            boot_id: current_boot_id(),
            liveness_path: Some(
                repo.heddle_dir()
                    .join("agents")
                    .join(format!("{session_id}.live")),
            ),
            heartbeat_at: Some(Utc::now()),
            anchor_state: Some(anchor_full_for_entry.clone()),
            anchor_root: Some(anchor_root.clone()),
            reservation_token: Some(objects::store::generate_agent_id()),
            path: reservation_path.clone(),
            base_state: anchor_short.clone(),
            started_at: Utc::now(),
            provider: probe.provider.clone(),
            model: probe.model.clone(),
            harness: probe
                .harness
                .clone()
                .or_else(|| Some("heddle-agent-api".to_string())),
            thinking_level: probe.thinking_level.clone(),
            usage_summary: AgentUsageSummary::default(),
            last_progress_at: None,
            report_flush_state: None,
            attach_reason: task.clone(),
            task_assignment_id: task_assignment_id.clone(),
            attach_precedence: vec!["agent-reserve".to_string()],
            winning_attach_rule: Some("agent-reserve".to_string()),
            probe_source: probe
                .probe_source
                .clone()
                .or_else(|| Some("agent_api".to_string())),
            probe_confidence: probe.confidence.or(Some(1.0)),
            status: AgentStatus::Active,
            completed_at: None,
            context_queries: vec![],
        })
    })?;

    let entry = match outcome {
        ReserveOutcome::Reserved(entry) => entry,
        ReserveOutcome::LiveOwner(existing) => {
            return Err(anyhow!(live_owner_conflict_advice(
                &thread_name,
                &anchor_full,
                &existing,
            )));
        }
    };

    // We hold the reservation. The remaining steps (CAS, oplog record,
    // thread metadata, JSON emit) must be all-or-nothing from the
    // caller's perspective: if any step fails after `try_reserve_thread`
    // wrote the Active entry, we have to mark that entry Abandoned
    // before returning the error. Otherwise the caller never sees a
    // session_id (no successful JSON output), but the registry retains
    // a live-owner row that ghost-blocks subsequent reservers — which
    // is exactly what `try_reserve_thread`'s own conflict logic would
    // hit, until pid-liveness reaping eventually clears it.
    //
    // The race the reviewer flagged: another writer advances the
    // thread ref between the pre-check at line 161 and the CAS below,
    // causing `set_thread_cas` to return ExpectationViolated. The
    // fallible closure here ensures we abandon the orphaned reservation
    // before bubbling that error up.
    let tn = ThreadName::new(&thread_name);
    let post_reserve = (|| -> Result<()> {
        if let Some(existing) = existing_ref {
            repo.refs()
                .set_thread_cas(&tn, RefExpectation::Value(existing), &anchor)?;
        } else {
            repo.refs()
                .set_thread_cas(&tn, RefExpectation::Missing, &anchor)?;
            // Agent-reservation flow writes the ThreadManager record via
            // `ensure_thread_record` below, after this op is recorded —
            // so there's no record to snapshot at recording time. Pass
            // `None`; the op records as `ThreadCreate` with no
            // manager snapshot. Reservations are an agent-internal API
            // that aren't expected to participate in human undo/redo
            // flows in 0.3. heddle#23 r2.
            repo.oplog()
                .record_thread_create(&tn, &anchor, None, Some(&repo.op_scope()))?;
        }

        // Ensure a Thread record exists so downstream commands
        // (`agent ready`, `thread show`, `ready`, `merge --preview`) have
        // first-class metadata to work with. `start_thread` does the
        // same; we mirror just the minimum required for the agent API.
        ensure_thread_record(&repo, &thread_name, &anchor, &args.task)?;

        render_agent_reservation_envelope(&repo, &entry)?;
        Ok(())
    })();

    if let Err(err) = post_reserve {
        // Best-effort abandon: if the registry write itself fails (FS
        // error mid-cleanup), surface the original error and let the
        // pid-based dead-owner reaper recycle the orphan on the next
        // reserve. Logging the secondary failure would be ideal but
        // would make the error wire-format dependent on transient FS
        // state, so we keep the structured surface clean.
        let _ = registry.update_entry(&entry.session_id, |e| {
            e.status = AgentStatus::Abandoned;
            e.completed_at = Some(Utc::now());
        });
        return Err(err);
    }

    Ok(())
}

fn existing_thread_execution_path(
    repo: &Repository,
    thread_name: &str,
) -> Result<Option<std::path::PathBuf>> {
    let Some(thread) = ThreadManager::new(repo.heddle_dir()).find_by_thread(thread_name)? else {
        return Ok(None);
    };
    let path = if !thread.execution_path.as_os_str().is_empty() {
        Some(thread.execution_path)
    } else {
        thread.materialized_path
    };
    Ok(path.map(|path| path.canonicalize().unwrap_or(path)))
}

/// Persist a minimal `Thread` record for `thread_name` if one does not
/// already exist. Mirrors the relevant fields from `start_thread`.
fn ensure_thread_record(
    repo: &Repository,
    thread_name: &str,
    anchor: &objects::object::ChangeId,
    task: &Option<String>,
) -> Result<()> {
    let manager = ThreadManager::new(repo.heddle_dir());
    if manager.load(thread_name)?.is_some() {
        return Ok(());
    }
    let state = repo
        .store()
        .get_state(anchor)?
        .ok_or_else(|| anyhow!("anchor state '{}' not found", anchor.short()))?;
    let base_short = anchor.short();
    let base_root = state.tree.short();
    let target_thread = match repo.head_ref()? {
        Head::Attached { thread } if thread != thread_name => Some(thread.to_string()),
        _ => None,
    };
    let thread_state = Thread {
        id: thread_name.to_string(),
        thread: thread_name.to_string(),
        target_thread,
        parent_thread: None,
        mode: ThreadMode::Materialized,
        state: ThreadState::Active,
        base_state: base_short.clone(),
        base_root,
        current_state: Some(base_short),
        merged_state: None,
        task: task.clone(),
        execution_path: repo.root().to_path_buf(),
        materialized_path: None,
        changed_paths: vec![],
        impact_categories: vec![],
        heavy_impact_paths: vec![],
        promotion_suggested: false,
        freshness: ThreadFreshness::Current,
        verification_summary: ThreadVerificationSummary::default(),
        confidence_summary: ThreadConfidenceSummary::default(),
        integration_policy_result: ThreadIntegrationPolicy::default(),
        created_at: Utc::now(),
        updated_at: Utc::now(),
        // Reservation-API-created threads aren't ephemeral by
        // default; orchestrators that want TTL-bounded threads pass
        // through `heddle thread create --ephemeral` instead.
        ephemeral: None,
        // Reservation API threads are user-orchestrated, not
        // harness-auto-created — leave them visible in the default
        // `thread list` view.
        auto: false,
        shared_target_dir: None,
    };
    manager.save(&thread_state)?;
    Ok(())
}

pub fn cmd_agent_heartbeat(cli: &Cli, args: AgentHeartbeatArgs) -> Result<()> {
    let repo = cli.open_repo()?;
    let registry = AgentRegistry::new(repo.heddle_dir());
    let entry = registry
        .update_entry(&args.session, |entry| {
            entry.heartbeat_at = Some(Utc::now());
            entry.last_progress_at = Some(Utc::now());
        })?
        .ok_or_else(|| anyhow!(agent_session_not_found_advice(&args.session)))?;
    render_agent_reservation_envelope(&repo, &entry)
}

pub fn cmd_agent_release(cli: &Cli, args: AgentReleaseArgs) -> Result<()> {
    let repo = cli.open_repo()?;
    let registry = AgentRegistry::new(repo.heddle_dir());
    let status = match args.status {
        AgentReleaseStatusArg::Complete => AgentStatus::Complete,
        AgentReleaseStatusArg::Abandoned => AgentStatus::Abandoned,
    };
    let entry = registry
        .update_entry(&args.session, |entry| {
            entry.status = status.clone();
            entry.completed_at = match entry.status {
                AgentStatus::Active => None,
                AgentStatus::Abandoned | AgentStatus::Complete | AgentStatus::Merged => {
                    Some(Utc::now())
                }
            };
        })?
        .ok_or_else(|| anyhow!(agent_session_not_found_advice(&args.session)))?;
    render_agent_reservation_envelope(&repo, &entry)
}

pub fn cmd_agent_list(cli: &Cli, args: AgentApiListArgs) -> Result<()> {
    let repo = cli.open_repo()?;
    let registry = AgentRegistry::new(repo.heddle_dir());
    if args.alive_only {
        // Sweep dead reservations before reporting so callers asking
        // "who is alive?" see a pid-checked, current view.
        registry.reap_dead()?;
    }
    let entries: Vec<_> = registry
        .list()?
        .into_iter()
        .filter(|entry| {
            args.thread
                .as_ref()
                .is_none_or(|thread| &entry.thread == thread)
        })
        .filter(|entry| !args.alive_only || entry.status == AgentStatus::Active)
        .map(|entry| AgentReservationOutput::from(&entry))
        .collect();
    render_agent_list(
        AgentReservationListOutput {
            reservations: entries,
            alive_only: args.alive_only,
            thread: args.thread.clone(),
            trust: build_repository_verification_state(&repo),
        },
        should_output_json(cli, Some(repo.config())),
    )
}

pub fn cmd_agent_task(cli: &Cli, command: AgentTaskCommands) -> Result<()> {
    match command {
        AgentTaskCommands::Create(args) => cmd_agent_task_create(cli, args),
        AgentTaskCommands::List(args) => cmd_agent_task_list(cli, args),
        AgentTaskCommands::Show(args) => cmd_agent_task_show(cli, args),
        AgentTaskCommands::Update(args) => cmd_agent_task_update(cli, args),
    }
}

pub fn cmd_agent_fanout(cli: &Cli, command: AgentFanoutCommands) -> Result<()> {
    match command {
        AgentFanoutCommands::Plan(args) => cmd_agent_fanout_plan(cli, args),
        AgentFanoutCommands::Start(args) => cmd_agent_fanout_start(cli, args),
    }
}

#[derive(Clone, Debug)]
struct FanoutLaneSpec {
    thread: String,
    path: PathBuf,
    title: String,
}

fn cmd_agent_fanout_plan(cli: &Cli, args: AgentFanoutPlanArgs) -> Result<()> {
    let repo = cli.open_repo()?;
    let lanes = parse_fanout_lanes(&args.lane)?;
    let (base_state, base_root) = fanout_base(&repo)?;
    let parent_thread = fanout_parent_thread(&repo)?;
    let output = build_fanout_output(
        &repo,
        "agent_fanout_plan",
        args.title,
        parent_thread,
        base_state,
        base_root,
        args.coordination_discussion_id,
        None,
        lanes
            .into_iter()
            .map(|lane| AgentFanoutLaneOutput {
                thread: lane.thread,
                path: lane.path.display().to_string(),
                title: lane.title,
                task: None,
                session_id: None,
                status: "planned".to_string(),
            })
            .collect(),
    );
    render_agent_fanout_output(output, should_output_json(cli, Some(repo.config())))
}

fn cmd_agent_fanout_start(cli: &Cli, args: AgentFanoutStartArgs) -> Result<()> {
    let repo = cli.open_repo()?;
    if let Some(advice) = git_overlay_mutation_preflight_advice(
        &repo,
        "agent fanout start",
        GitOverlayMutationPreflight::capture_like(),
    )? {
        return Err(anyhow!(advice));
    }
    ensure_worktree_clean(&repo, "agent fanout start")?;

    let lanes = parse_fanout_lanes(&args.lane)?;
    let (base_state, base_root) = fanout_base(&repo)?;
    let parent_thread = fanout_parent_thread(&repo)?;
    preflight_fanout_start(&repo, &lanes)?;
    let store = AgentTaskStore::new(repo.heddle_dir());

    let mut parent = AgentTaskRecord::new(String::new(), args.title.clone(), parent_thread.clone());
    parent.body = fanout_parent_body(&lanes);
    parent.base_state = Some(base_state.clone());
    parent.base_root = Some(base_root.clone());
    parent.coordination_discussion_id = args.coordination_discussion_id.clone();
    parent.allow_offline = true;
    parent.delegated_by = Some("heddle agent fanout start".to_string());
    parent.status = AgentTaskStatus::InProgress;
    let parent = store.create(parent)?;

    let mut created_task_ids = vec![parent.task_id.clone()];
    let start_result = (|| -> Result<Vec<AgentFanoutLaneOutput>> {
        let mut outputs = Vec::new();
        for lane in lanes {
            let mut child =
                AgentTaskRecord::new(String::new(), lane.title.clone(), lane.thread.clone());
            child.body = format!("Fan-out child lane for parent task {}", parent.task_id);
            child.base_state = Some(base_state.clone());
            child.base_root = Some(base_root.clone());
            child.parent_task_id = Some(parent.task_id.clone());
            child.coordination_discussion_id = args.coordination_discussion_id.clone();
            child.allow_offline = true;
            child.delegated_by = Some(parent.task_id.clone());
            child.status = AgentTaskStatus::InProgress;
            let child = store.create(child)?;
            created_task_ids.push(child.task_id.clone());

            let started = super::thread::start_thread(
                &repo,
                ThreadStartArgs {
                    name: lane.thread.clone(),
                    from: Some(base_state.clone()),
                    path: Some(lane.path.clone()),
                    workspace: WorkspaceModeArg::Auto,
                    agent_provider: None,
                    agent_model: None,
                    task: Some(lane.title.clone()),
                    parent_thread: Some(parent_thread.clone()),
                    automated: true,
                    print_cd_path: false,
                    daemon: true,
                    no_daemon: false,
                    shared_target: false,
                    hydrate: false,
                },
            )?;
            let session_id = started
                .thread
                .as_ref()
                .and_then(|thread| thread.session_id.clone());
            if let Some(session_id) = session_id.as_deref() {
                let registry = AgentRegistry::new(repo.heddle_dir());
                let _ = registry.update_entry(session_id, |entry| {
                    entry.task_assignment_id = Some(child.task_id.clone());
                    entry.attach_reason = Some(lane.title.clone());
                    entry
                        .attach_precedence
                        .push("agent-fanout-start".to_string());
                    entry.winning_attach_rule = Some("agent-fanout-start".to_string());
                })?;
            }
            outputs.push(AgentFanoutLaneOutput {
                thread: lane.thread,
                path: lane.path.display().to_string(),
                title: lane.title,
                task: Some(AgentTaskOutput::from(&child)),
                session_id,
                status: "started".to_string(),
            });
        }
        Ok(outputs)
    })();

    let outputs = match start_result {
        Ok(outputs) => outputs,
        Err(err) => {
            abandon_fanout_tasks(&store, &created_task_ids);
            return Err(err);
        }
    };

    let output = build_fanout_output(
        &repo,
        "agent_fanout_start",
        args.title,
        parent_thread,
        base_state,
        base_root,
        args.coordination_discussion_id,
        Some(AgentTaskOutput::from(&parent)),
        outputs,
    );
    render_agent_fanout_output(output, should_output_json(cli, Some(repo.config())))
}

fn cmd_agent_task_create(cli: &Cli, args: AgentTaskCreateArgs) -> Result<()> {
    ThreadId::new(args.thread.as_str()).map_err(|err| anyhow!(thread_name_invalid_advice(&err)))?;
    if let Some(task_id) = args.task_id.as_deref() {
        validate_task_id(task_id).map_err(|err| anyhow!(err))?;
    }
    let repo = cli.open_repo()?;
    let mut record = AgentTaskRecord::new(
        args.task_id.unwrap_or_default(),
        args.title,
        args.thread.clone(),
    );
    record.body = args.body.unwrap_or_default();
    record.base_state = args.base_state;
    record.base_root = args.base_root;
    record.parent_task_id = args.parent_task_id;
    record.coordination_discussion_id = args.coordination_discussion_id;
    record.allow_offline = args.allow_offline;
    record.delegated_by = args.delegated_by;
    let store = AgentTaskStore::new(repo.heddle_dir());
    let created = store.create(record)?;
    render_agent_task_envelope(
        &repo,
        &created,
        "agent_task_create",
        should_output_json(cli, Some(repo.config())),
    )
}

fn cmd_agent_task_list(cli: &Cli, args: AgentTaskListArgs) -> Result<()> {
    let repo = cli.open_repo()?;
    let status_filter = args.status.as_ref().map(agent_task_status_from_arg);
    let store = AgentTaskStore::new(repo.heddle_dir());
    let tasks: Vec<_> = store
        .list()?
        .into_iter()
        .filter(|task| {
            args.thread
                .as_ref()
                .is_none_or(|thread| &task.target_thread == thread)
        })
        .filter(|task| {
            status_filter
                .as_ref()
                .is_none_or(|status| &task.status == status)
        })
        .map(|task| AgentTaskOutput::from(&task))
        .collect();
    render_agent_task_list(
        AgentTaskListOutput {
            output_kind: "agent_task_list",
            tasks,
            thread: args.thread,
            status: status_filter.map(|status| status.to_string()),
            trust: build_repository_verification_state(&repo),
        },
        should_output_json(cli, Some(repo.config())),
    )
}

fn cmd_agent_task_show(cli: &Cli, args: AgentTaskShowArgs) -> Result<()> {
    validate_task_id(&args.task_id).map_err(|err| anyhow!(err))?;
    let repo = cli.open_repo()?;
    let store = AgentTaskStore::new(repo.heddle_dir());
    let task = store
        .load(&args.task_id)?
        .ok_or_else(|| anyhow!(agent_task_not_found_advice(&args.task_id)))?;
    render_agent_task_envelope(
        &repo,
        &task,
        "agent_task_show",
        should_output_json(cli, Some(repo.config())),
    )
}

fn cmd_agent_task_update(cli: &Cli, args: AgentTaskUpdateArgs) -> Result<()> {
    validate_task_id(&args.task_id).map_err(|err| anyhow!(err))?;
    if let Some(thread) = args.thread.as_deref() {
        ThreadId::new(thread).map_err(|err| anyhow!(thread_name_invalid_advice(&err)))?;
    }
    let repo = cli.open_repo()?;
    let store = AgentTaskStore::new(repo.heddle_dir());
    let updated = store
        .update(&args.task_id, |task| {
            if let Some(title) = args.title.clone() {
                task.title = title;
            }
            if let Some(body) = args.body.clone() {
                task.body = body;
            }
            if let Some(status) = args.status.as_ref() {
                task.status = agent_task_status_from_arg(status);
            }
            if let Some(thread) = args.thread.clone() {
                task.target_thread = thread;
            }
            if let Some(base_state) = args.base_state.clone() {
                task.base_state = Some(base_state);
            }
            if let Some(base_root) = args.base_root.clone() {
                task.base_root = Some(base_root);
            }
            if let Some(parent_task_id) = args.parent_task_id.clone() {
                task.parent_task_id = Some(parent_task_id);
            }
            if let Some(discussion_id) = args.coordination_discussion_id.clone() {
                task.coordination_discussion_id = Some(discussion_id);
            }
            if args.allow_offline {
                task.allow_offline = true;
            }
            if args.no_allow_offline {
                task.allow_offline = false;
            }
            if let Some(delegated_by) = args.delegated_by.clone() {
                task.delegated_by = Some(delegated_by);
            }
        })?
        .ok_or_else(|| anyhow!(agent_task_not_found_advice(&args.task_id)))?;
    render_agent_task_envelope(
        &repo,
        &updated,
        "agent_task_update",
        should_output_json(cli, Some(repo.config())),
    )
}

fn agent_task_status_from_arg(status: &AgentTaskStatusArg) -> AgentTaskStatus {
    match status {
        AgentTaskStatusArg::Open => AgentTaskStatus::Open,
        AgentTaskStatusArg::InProgress => AgentTaskStatus::InProgress,
        AgentTaskStatusArg::Blocked => AgentTaskStatus::Blocked,
        AgentTaskStatusArg::Complete => AgentTaskStatus::Complete,
        AgentTaskStatusArg::Abandoned => AgentTaskStatus::Abandoned,
    }
}

fn parse_fanout_lanes(raw_lanes: &[String]) -> Result<Vec<FanoutLaneSpec>> {
    if raw_lanes.is_empty() {
        return Err(anyhow!(RecoveryAdvice::invalid_usage(
            "agent_fanout_lane_required",
            "agent fanout requires at least one --lane <thread>=<path>:<title>",
            "Pass --lane once for each child checkout to create.",
            "heddle agent fanout plan --title <title> --lane <thread>=<path>:<title>",
        )));
    }
    raw_lanes.iter().map(|raw| parse_fanout_lane(raw)).collect()
}

fn parse_fanout_lane(raw: &str) -> Result<FanoutLaneSpec> {
    let (thread, rest) = raw.split_once('=').ok_or_else(|| {
        anyhow!(RecoveryAdvice::invalid_usage(
            "agent_fanout_lane_invalid",
            format!("invalid fanout lane '{raw}'"),
            "Use <thread>=<path>:<title>.",
            "heddle agent fanout plan --title <title> --lane feature/a=../a:Task title",
        ))
    })?;
    let (path, title) = rest.split_once(':').ok_or_else(|| {
        anyhow!(RecoveryAdvice::invalid_usage(
            "agent_fanout_lane_invalid",
            format!("invalid fanout lane '{raw}'"),
            "Use <thread>=<path>:<title>.",
            "heddle agent fanout plan --title <title> --lane feature/a=../a:Task title",
        ))
    })?;
    let thread = thread.trim();
    let path = path.trim();
    let title = title.trim();
    if path.is_empty() || title.is_empty() {
        return Err(anyhow!(RecoveryAdvice::invalid_usage(
            "agent_fanout_lane_invalid",
            format!("invalid fanout lane '{raw}'"),
            "Thread, path, and title must all be non-empty.",
            "heddle agent fanout plan --title <title> --lane feature/a=../a:Task title",
        )));
    }
    ThreadId::new(thread).map_err(|err| anyhow!(thread_name_invalid_advice(&err)))?;
    Ok(FanoutLaneSpec {
        thread: thread.to_string(),
        path: PathBuf::from(path),
        title: title.to_string(),
    })
}

fn fanout_base(repo: &Repository) -> Result<(String, String)> {
    let head = repo
        .head()?
        .ok_or_else(|| anyhow!("repository has no HEAD state for agent fanout"))?;
    let state = repo
        .store()
        .get_state(&head)?
        .ok_or_else(|| anyhow!("HEAD state '{}' not found", head.short()))?;
    Ok((head.to_string_full(), state.tree.short()))
}

fn fanout_parent_thread(repo: &Repository) -> Result<String> {
    Ok(match repo.head_ref()? {
        Head::Attached { thread } => thread.to_string(),
        Head::Detached { .. } => "detached".to_string(),
    })
}

fn fanout_parent_body(lanes: &[FanoutLaneSpec]) -> String {
    lanes
        .iter()
        .map(|lane| format!("- {}: {}", lane.thread, lane.title))
        .collect::<Vec<_>>()
        .join("\n")
}

fn abandon_fanout_tasks(store: &AgentTaskStore, task_ids: &[String]) {
    for task_id in task_ids {
        let _ = store.update(task_id, |task| {
            task.status = AgentTaskStatus::Abandoned;
        });
    }
}

fn preflight_fanout_start(repo: &Repository, lanes: &[FanoutLaneSpec]) -> Result<()> {
    let mut seen_threads = BTreeSet::new();
    let mut seen_paths = BTreeSet::new();
    let manager = ThreadManager::new(repo.heddle_dir());
    for lane in lanes {
        if !seen_threads.insert(lane.thread.clone()) {
            return Err(anyhow!(fanout_lane_unavailable_advice(
                "agent_fanout_duplicate_thread",
                lane,
                format!("fanout lane '{}' is listed more than once", lane.thread),
                "Use each child thread name once per fanout.",
            )));
        }
        if super::thread::find_active_thread_entry(repo, &lane.thread)?.is_some() {
            return Err(anyhow!(fanout_lane_unavailable_advice(
                "agent_fanout_live_owner",
                lane,
                format!(
                    "fanout lane '{}' already has an active agent reservation",
                    lane.thread
                ),
                "Release the active reservation or choose a fresh child thread.",
            )));
        }
        if repo
            .refs()
            .get_thread(&ThreadName::new(&lane.thread))?
            .is_some()
        {
            return Err(anyhow!(fanout_lane_unavailable_advice(
                "agent_fanout_thread_exists",
                lane,
                format!("fanout lane '{}' already exists", lane.thread),
                "Choose a fresh child thread or inspect the existing thread before retrying.",
            )));
        }
        if let Some(existing) = manager.find_by_thread(&lane.thread)?
            && existing.state == ThreadState::Active
        {
            return Err(anyhow!(fanout_lane_unavailable_advice(
                "agent_fanout_thread_exists",
                lane,
                format!(
                    "fanout lane '{}' already has an active thread record",
                    lane.thread
                ),
                "Drop or finish the existing thread before reusing the lane name.",
            )));
        }
        let prepared = plan_worktree_target(repo, &lane.path, Some(&lane.thread))?;
        if !seen_paths.insert(prepared.path.clone()) {
            return Err(anyhow!(fanout_lane_unavailable_advice(
                "agent_fanout_duplicate_path",
                lane,
                format!(
                    "fanout lane '{}' resolves to a checkout path used by another lane",
                    lane.thread
                ),
                "Use a distinct checkout path for each child lane.",
            )));
        }
    }
    Ok(())
}

fn fanout_lane_unavailable_advice(
    kind: &'static str,
    lane: &FanoutLaneSpec,
    error: String,
    guidance: &'static str,
) -> RecoveryAdvice {
    RecoveryAdvice::invalid_usage(
        kind,
        error,
        guidance,
        format!(
            "heddle agent fanout plan --title <title> --lane {}=<path>:<title>",
            lane.thread
        ),
    )
}

#[allow(clippy::too_many_arguments)]
fn build_fanout_output(
    repo: &Repository,
    output_kind: &'static str,
    title: String,
    parent_thread: String,
    base_state: String,
    base_root: String,
    coordination_discussion_id: Option<String>,
    parent_task: Option<AgentTaskOutput>,
    lanes: Vec<AgentFanoutLaneOutput>,
) -> AgentFanoutOutput {
    let commands = if output_kind == "agent_fanout_plan" {
        let mut argv = vec![
            "heddle".to_string(),
            "agent".to_string(),
            "fanout".to_string(),
            "start".to_string(),
            "--title".to_string(),
            title.clone(),
        ];
        if let Some(discussion_id) = coordination_discussion_id.as_ref() {
            argv.push("--coordination-discussion-id".to_string());
            argv.push(discussion_id.clone());
        }
        for lane in &lanes {
            argv.push("--lane".to_string());
            argv.push(format!("{}={}:{}", lane.thread, lane.path, lane.title));
        }
        let command = argv
            .iter()
            .map(|arg| shell_quote(arg))
            .collect::<Vec<_>>()
            .join(" ");
        vec![AgentFanoutCommandOutput {
            lane_thread: "all".to_string(),
            command,
            argv,
        }]
    } else {
        Vec::new()
    };
    AgentFanoutOutput {
        output_kind,
        title,
        parent_thread,
        base_state,
        base_root,
        coordination_discussion_id,
        parent_task,
        lanes,
        commands,
        trust: build_repository_verification_state(repo),
    }
}

fn render_agent_fanout_output(output: AgentFanoutOutput, json: bool) -> Result<()> {
    if json {
        println!("{}", serde_json::to_string(&output)?);
        return Ok(());
    }
    println!(
        "Agent fanout {}: {}",
        output
            .output_kind
            .strip_prefix("agent_fanout_")
            .unwrap_or(output.output_kind),
        output.title
    );
    if let Some(parent_task) = &output.parent_task {
        println!(
            "  parent task: {}",
            crate::cli::style::accent(&parent_task.task_id)
        );
    }
    for lane in &output.lanes {
        println!(
            "  {} [{}] {}",
            crate::cli::style::accent(&lane.thread),
            lane.status,
            crate::cli::style::dim(&lane.path),
        );
        if let Some(task) = &lane.task {
            println!("    task: {}", crate::cli::style::dim(&task.task_id));
        }
    }
    if output.output_kind == "agent_fanout_plan" {
        println!("Commands:");
        for command in &output.commands {
            println!("  {}", command.command);
        }
    }
    Ok(())
}

fn render_agent_list(output: AgentReservationListOutput, json: bool) -> Result<()> {
    if json {
        println!("{}", serde_json::to_string(&output)?);
        return Ok(());
    }
    let entries = output.reservations;
    if entries.is_empty() {
        println!("No agent reservations.");
        return Ok(());
    }
    println!("Agent reservations ({}):", entries.len());
    for entry in entries {
        println!(
            "  {} [{}] thread={}",
            crate::cli::style::accent(&entry.session_id),
            entry.status,
            entry.thread,
        );
        if let Some(task) = &entry.task {
            println!("    task: {}", crate::cli::style::dim(task));
        }
        if let Some(path) = &entry.path
            && !path.is_empty()
        {
            println!("    path: {}", crate::cli::style::dim(path));
        }
    }
    Ok(())
}

fn render_agent_task_envelope(
    repo: &Repository,
    task: &AgentTaskRecord,
    output_kind: &'static str,
    json: bool,
) -> Result<()> {
    let output = AgentTaskEnvelope {
        output_kind,
        task: AgentTaskOutput::from(task),
        trust: build_repository_verification_state(repo),
    };
    if json {
        println!("{}", serde_json::to_string(&output)?);
        return Ok(());
    }
    println!(
        "Agent task {} [{}]",
        crate::cli::style::accent(&output.task.task_id),
        output.task.status
    );
    println!("  title: {}", output.task.title);
    println!("  thread: {}", output.task.target_thread);
    if !output.task.body.is_empty() {
        println!("  body: {}", output.task.body);
    }
    if let Some(base_state) = &output.task.base_state {
        println!("  base_state: {}", crate::cli::style::dim(base_state));
    }
    if let Some(base_root) = &output.task.base_root {
        println!("  base_root: {}", crate::cli::style::dim(base_root));
    }
    Ok(())
}

fn render_agent_task_list(output: AgentTaskListOutput, json: bool) -> Result<()> {
    if json {
        println!("{}", serde_json::to_string(&output)?);
        return Ok(());
    }
    if output.tasks.is_empty() {
        println!("No agent tasks.");
        return Ok(());
    }
    println!("Agent tasks ({}):", output.tasks.len());
    for task in output.tasks {
        println!(
            "  {} [{}] thread={} title={}",
            crate::cli::style::accent(&task.task_id),
            task.status,
            task.target_thread,
            task.title,
        );
    }
    Ok(())
}

fn reservation_envelope(repo: &Repository, entry: &AgentEntry) -> AgentReservationEnvelope {
    AgentReservationEnvelope {
        reservation: AgentReservationOutput::from(entry),
        trust: build_repository_verification_state(repo),
    }
}

fn render_agent_reservation_envelope(repo: &Repository, entry: &AgentEntry) -> Result<()> {
    println!(
        "{}",
        serde_json::to_string(&reservation_envelope(repo, entry))?
    );
    Ok(())
}

fn agent_session_not_found_advice(session_id: &str) -> RecoveryAdvice {
    RecoveryAdvice::safety_refusal(
        "agent_session_not_found",
        format!("agent session '{session_id}' not found"),
        "Reserve the thread again before retrying the guarded agent command.",
        format!("no reservation entry exists for session {session_id}"),
        "continuing would let an unknown session mutate repository state",
        "no session heartbeat, capture, readiness, refs, or worktree changes were applied",
        "heddle agent reserve --thread <thread>",
        vec!["heddle agent reserve --thread <thread>".to_string()],
    )
}

/// Resolve `--session SID` to an Active reservation, refresh its
/// heartbeat, and return the entry. Errors out cleanly when the
/// session is missing, terminal, or reaped between calls — that's the
/// signal the orchestrator must re-reserve before continuing.
fn validate_active_session(
    registry: &AgentRegistry,
    session_id: &str,
) -> Result<objects::store::AgentEntry> {
    let entry = registry
        .update_entry(session_id, |entry| {
            entry.heartbeat_at = Some(Utc::now());
            entry.last_progress_at = Some(Utc::now());
        })?
        .ok_or_else(|| anyhow!(agent_session_not_found_advice(session_id)))?;
    if entry.status != AgentStatus::Active {
        return Err(anyhow!(RecoveryAdvice::safety_refusal(
            "agent_session_inactive",
            format!(
                "agent session '{}' is no longer active (status: {})",
                session_id, entry.status
            ),
            "Reserve the thread again before retrying the guarded agent command.",
            format!("session {session_id} has terminal status {}", entry.status),
            "continuing would let a stale reservation write or mark readiness after ownership ended",
            "the session heartbeat was refreshed, but no capture, ready, refs, or worktree changes were applied",
            "heddle agent reserve --thread <thread>",
            vec!["heddle agent reserve --thread <thread>".to_string()],
        )));
    }
    Ok(entry)
}

/// `heddle agent capture --session <SID>`: a session-validated
/// alias for `heddle capture` that proves the caller still owns the
/// reservation it claims to before writing.
pub async fn cmd_agent_capture(
    cli: &Cli,
    args: crate::cli::cli_args::AgentCaptureArgs,
) -> Result<()> {
    let repo_path = cli
        .repo
        .clone()
        .unwrap_or(std::env::current_dir().map_err(anyhow::Error::from)?);
    let repo = Repository::open(&repo_path)?;
    let registry = AgentRegistry::new(repo.heddle_dir());
    let entry = validate_active_session(&registry, &args.session)?;

    // Confirm the reservation still names the thread the caller is
    // attached to. We don't switch threads here — the agent must
    // already be on its reserved thread when invoking capture.
    if let Some(current) = repo.current_lane()?
        && current != entry.thread
    {
        return Err(anyhow!(RecoveryAdvice::safety_refusal(
            "agent_session_thread_mismatch",
            format!(
                "agent session '{}' reserved thread '{}', but the current thread is '{}'",
                args.session, entry.thread, current
            ),
            format!(
                "Switch to the reserved thread with `heddle switch {}` before capturing.",
                entry.thread
            ),
            format!(
                "session {} owns thread {}, current checkout is attached to {}",
                args.session, entry.thread, current
            ),
            "capturing from the wrong thread would attribute work to a reservation that does not own this checkout",
            "the session heartbeat was refreshed, but no capture, refs, or worktree changes were applied",
            format!("heddle switch {}", entry.thread),
            vec![format!("heddle switch {}", entry.thread)],
        )));
    }

    super::snapshot::cmd_snapshot(
        cli,
        args.message.clone(),
        args.confidence,
        false,
        super::snapshot::SnapshotAgentOverrides {
            provider: entry.provider.clone(),
            model: entry.model.clone(),
            session: Some(args.session.clone()),
            segment: None,
            policy: None,
            no_policy: false,
            no_agent: false,
        },
    )
    .await
}

/// `heddle agent ready --session <SID>`: a session-validated alias
/// for `heddle ready` that ensures the caller still owns the
/// reservation it's trying to mark ready.
pub async fn cmd_agent_ready(cli: &Cli, args: crate::cli::cli_args::AgentReadyArgs) -> Result<()> {
    let repo_path = cli
        .repo
        .clone()
        .unwrap_or(std::env::current_dir().map_err(anyhow::Error::from)?);
    let repo = Repository::open(&repo_path)?;
    let registry = AgentRegistry::new(repo.heddle_dir());
    let entry = validate_active_session(&registry, &args.session)?;

    super::ready_cmd::cmd_ready(
        cli,
        crate::cli::cli_args::ReadyArgs {
            thread: Some(entry.thread.clone()),
            message: args.message.clone(),
            confidence: args.confidence,
        },
    )
    .await
}

/// Return the combined JSON schema for the public agent-API output
/// types. Snapshot-tested in `tests/agent_api_schema.rs` so any
/// breaking change to the wire shape is caught at PR review.
pub fn agent_api_schema() -> serde_json::Value {
    serde_json::json!({
        "AgentReservationEnvelope": schemars::schema_for!(AgentReservationEnvelope),
        "AgentReservationListOutput": schemars::schema_for!(AgentReservationListOutput),
        "AgentReservationOutput": schemars::schema_for!(AgentReservationOutput),
        "AgentTaskEnvelope": schemars::schema_for!(AgentTaskEnvelope),
        "AgentTaskListOutput": schemars::schema_for!(AgentTaskListOutput),
        "AgentTaskOutput": schemars::schema_for!(AgentTaskOutput),
        "AgentFanoutOutput": schemars::schema_for!(AgentFanoutOutput),
        "AgentFanoutLaneOutput": schemars::schema_for!(AgentFanoutLaneOutput),
        "AgentFanoutCommandOutput": schemars::schema_for!(AgentFanoutCommandOutput),
    })
}