radial 0.1.0

Task orchestration for LLM agents with contract-based coordination
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
use jiff::Timestamp;
use serde::{Deserialize, Serialize};
use strum::{AsRefStr, EnumString};

use super::{Comment, Contract, Outcome};
use crate::id::{GoalId, TaskId};

/// A task's priority, from most (`P0`) to least (`P3`) urgent.
#[derive(
    Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, AsRefStr, EnumString,
)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
#[derive(Default)]
#[non_exhaustive]
pub enum Priority {
    /// Critical.
    P0,
    /// High.
    P1,
    /// Normal; the default.
    #[default]
    P2,
    /// Low.
    P3,
}

/// The lifecycle state of a [`Task`].
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, AsRefStr, EnumString)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "snake_case")]
#[non_exhaustive]
pub enum TaskState {
    /// Not yet started, and not blocked.
    Pending,
    /// Waiting on one or more other tasks in `blocked_by`.
    Blocked,
    /// Started and being worked on.
    InProgress,
    /// Work is done and awaiting verification against its contract.
    Verifying,
    /// Finished successfully.
    Completed,
    /// Finished unsuccessfully.
    Failed,
    /// Cancelled before completion.
    Cancelled,
}

/// Token, time, and retry counters for a single [`Task`].
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct TaskMetrics {
    tokens: i64,
    elapsed_ms: i64,
    retry_count: i64,
}

impl TaskMetrics {
    /// Creates a new set of metrics from its raw counters.
    pub fn new(tokens: i64, elapsed_ms: i64, retry_count: i64) -> Self {
        Self {
            tokens,
            elapsed_ms,
            retry_count,
        }
    }

    /// Tokens spent on the task.
    pub fn tokens(&self) -> i64 {
        self.tokens
    }

    /// Elapsed time spent on the task, in milliseconds.
    pub fn elapsed_ms(&self) -> i64 {
        self.elapsed_ms
    }

    /// Number of times the task has been retried.
    pub fn retry_count(&self) -> i64 {
        self.retry_count
    }
}

/// A tracked, verifiable unit of work under a [`Goal`](crate::models::Goal),
/// carrying a [`Contract`] and optionally an [`Outcome`] once completed.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Task {
    id: TaskId,
    goal_id: GoalId,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    seq: Option<u32>,
    #[serde(
        rename = "ref",
        skip_deserializing,
        skip_serializing_if = "Option::is_none"
    )]
    display_ref_field: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    parent_id: Option<TaskId>,
    description: String,
    #[serde(default)]
    priority: Priority,
    #[serde(skip_serializing_if = "Option::is_none")]
    contract: Option<Contract>,
    state: TaskState,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    blocked_by: Vec<TaskId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    assignee: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    started_at: Option<Timestamp>,
    #[serde(skip_serializing_if = "Option::is_none")]
    result: Option<Outcome>,
    created_at: Timestamp,
    updated_at: Timestamp,
    #[serde(skip_serializing_if = "Option::is_none")]
    completed_at: Option<Timestamp>,
    metrics: TaskMetrics,
    #[serde(default)]
    comments: Vec<Comment>,
    #[serde(default)]
    compacted: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    summary: Option<String>,
}

impl Task {
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn new(
        id: TaskId,
        goal_id: GoalId,
        seq: Option<u32>,
        parent_id: Option<TaskId>,
        description: String,
        priority: Priority,
        contract: Option<Contract>,
        state: TaskState,
        blocked_by: Vec<TaskId>,
        created_at: Timestamp,
        updated_at: Timestamp,
    ) -> Self {
        Self {
            id,
            goal_id,
            seq,
            display_ref_field: None, // Will be computed after loading
            parent_id,
            description,
            priority,
            contract,
            state,
            blocked_by,
            assignee: None,
            started_at: None,
            result: None,
            created_at,
            updated_at,
            completed_at: None,
            metrics: TaskMetrics::default(),
            comments: Vec::new(),
            compacted: false,
            summary: None,
        }
    }

    #[cfg(test)]
    #[must_use]
    pub(crate) fn with_metrics(mut self, metrics: TaskMetrics) -> Self {
        self.metrics = metrics;
        self
    }

    /// The task's unique ID.
    pub fn id(&self) -> &TaskId {
        &self.id
    }

    /// The ID of the goal this task belongs to.
    pub fn goal_id(&self) -> &GoalId {
        &self.goal_id
    }

    /// The task's sequence number, used to build its [`display_ref`](Self::display_ref).
    /// `None` for tasks created before sequence numbers existed.
    pub fn seq(&self) -> Option<u32> {
        self.seq
    }

    /// Returns the display reference for this task (e.g., "g1.2").
    /// Returns None if either this task's seq or the goal's seq is not assigned.
    pub fn display_ref(&self, goal_seq: u32) -> Option<String> {
        // If cached ref is available, use it; otherwise compute it
        self.display_ref_field
            .clone()
            .or_else(|| self.seq.map(|s| format!("g{goal_seq}.{s}")))
    }

    /// Compute and set the `display_ref_field` based on the `seq` and `goal_seq`.
    /// Called after deserialization to populate the computed field.
    pub(crate) fn compute_display_ref(&mut self, goal_seq: u32) {
        self.display_ref_field = self.seq.map(|s| format!("g{goal_seq}.{s}"));
    }

    /// The ID of the task this one is a subtask of, if any.
    pub fn parent_id(&self) -> Option<&TaskId> {
        self.parent_id.as_ref()
    }

    /// The task's description.
    pub fn description(&self) -> &str {
        &self.description
    }

    /// The task's priority.
    pub fn priority(&self) -> Priority {
        self.priority
    }

    /// The task's receives/produces/verify contract, if one was set.
    pub fn contract(&self) -> Option<&Contract> {
        self.contract.as_ref()
    }

    /// The task's current lifecycle state.
    pub fn state(&self) -> TaskState {
        self.state
    }

    /// IDs of the tasks that must complete before this one can start.
    pub fn blocked_by(&self) -> &[TaskId] {
        &self.blocked_by
    }

    /// The task's recorded completion outcome, if it has one.
    pub fn result(&self) -> Option<&Outcome> {
        self.result.as_ref()
    }

    /// When the task was created.
    pub fn created_at(&self) -> Timestamp {
        self.created_at
    }

    /// When the task was last updated.
    pub fn updated_at(&self) -> Timestamp {
        self.updated_at
    }

    /// When the task was completed, if it has been.
    pub fn completed_at(&self) -> Option<Timestamp> {
        self.completed_at
    }

    /// The task's token/time/retry metrics.
    pub fn metrics(&self) -> &TaskMetrics {
        &self.metrics
    }

    /// Comments left on the task, in the order they were added.
    pub fn comments(&self) -> &[Comment] {
        &self.comments
    }

    /// Whether the task has been compacted (its heavy fields replaced with a summary).
    pub fn compacted(&self) -> bool {
        self.compacted
    }

    /// The task's compaction summary, if it has been compacted.
    pub fn summary(&self) -> Option<&str> {
        self.summary.as_deref()
    }

    /// Compact this task by replacing heavy fields with a summary.
    /// Only valid for Completed or Failed tasks that aren't already compacted.
    pub(crate) fn compact(&mut self, summary: String) -> bool {
        if self.compacted {
            return false;
        }
        if self.state != TaskState::Completed && self.state != TaskState::Failed {
            return false;
        }
        self.compacted = true;
        self.summary = Some(summary);
        self.description = "[compacted]".to_string();
        self.contract = None;
        self.comments = Vec::new();
        self.updated_at = Timestamp::now();
        true
    }

    /// Set state directly for parent tasks whose state is derived from subtasks.
    pub(crate) fn set_derived_state(&mut self, state: TaskState) {
        self.state = state;
        let now = Timestamp::now();
        self.updated_at = now;
        if state == TaskState::Completed && self.completed_at.is_none() {
            self.completed_at = Some(now);
        }
    }

    pub(crate) fn set_description(&mut self, description: String) {
        self.description = description;
        self.updated_at = Timestamp::now();
    }

    pub(crate) fn set_priority(&mut self, priority: Priority) {
        self.priority = priority;
        self.updated_at = Timestamp::now();
    }

    pub(crate) fn set_contract(&mut self, contract: Contract) {
        self.contract = Some(contract);
        self.updated_at = Timestamp::now();
    }

    pub(crate) fn set_blocked_by(&mut self, blocked_by: Vec<TaskId>) {
        self.blocked_by = blocked_by;
        self.updated_at = Timestamp::now();
    }

    /// Who the task is currently assigned to, if anyone.
    pub fn assignee(&self) -> Option<&str> {
        self.assignee.as_deref()
    }

    /// When the task was started, if it has been.
    pub fn started_at(&self) -> Option<Timestamp> {
        self.started_at
    }

    pub(crate) fn set_assignee(&mut self, assignee: Option<String>) {
        self.assignee = assignee;
        self.updated_at = Timestamp::now();
    }

    pub(crate) fn release(&mut self) -> bool {
        if self.assignee.is_none() {
            return false;
        }
        self.assignee = None;
        self.started_at = None;
        self.state = TaskState::Pending;
        self.updated_at = Timestamp::now();
        true
    }

    pub(crate) fn transition_from_any(&mut self, from: &[TaskState], to: TaskState) -> bool {
        if !from.contains(&self.state) {
            return false;
        }
        self.state = to;
        let now = Timestamp::now();
        self.updated_at = now;
        if to == TaskState::InProgress {
            self.started_at = Some(now);
        }
        true
    }

    pub(crate) fn complete(&mut self, outcome: Outcome, metrics: TaskMetrics) -> bool {
        if self.state != TaskState::InProgress {
            return false;
        }
        self.state = TaskState::Completed;
        self.result = Some(outcome);
        self.metrics = metrics;
        let now = Timestamp::now();
        self.updated_at = now;
        self.completed_at = Some(now);
        true
    }

    pub(crate) fn retry(&mut self) -> bool {
        if self.state != TaskState::Failed {
            return false;
        }
        self.state = TaskState::InProgress;
        self.metrics.retry_count += 1;
        let now = Timestamp::now();
        self.updated_at = now;
        self.started_at = Some(now);
        true
    }

    pub(crate) fn cancel(&mut self) -> bool {
        // Cancelled is terminal - already cancelled tasks cannot be re-cancelled
        if self.state == TaskState::Cancelled {
            return false;
        }
        // Completed work is history - cannot cancel completed tasks
        if self.state == TaskState::Completed {
            return false;
        }
        // All other states (Pending, Blocked, InProgress, Verifying, Failed) can transition to Cancelled
        self.state = TaskState::Cancelled;
        self.updated_at = Timestamp::now();
        true
    }

    pub(crate) fn unblock(&mut self) {
        self.state = TaskState::Pending;
        self.updated_at = Timestamp::now();
    }

    pub(crate) fn add_comment(&mut self, comment: Comment) {
        self.comments.push(comment);
        self.updated_at = Timestamp::now();
    }

    pub(crate) fn set_result(&mut self, outcome: Outcome) {
        self.result = Some(outcome);
        self.updated_at = Timestamp::now();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::id::{GoalId, TaskId};
    use rstest::{fixture, rstest};

    #[fixture]
    fn task() -> Task {
        let now = Timestamp::now();
        Task {
            id: TaskId::new_unchecked("t_abc123".to_string()),
            goal_id: GoalId::new_unchecked("g_xyz789".to_string()),
            seq: None,
            display_ref_field: None,
            parent_id: None,
            description: "test task".to_string(),
            priority: Priority::default(),
            contract: None,
            state: TaskState::Pending,
            blocked_by: Vec::new(),
            assignee: None,
            started_at: None,
            result: None,
            created_at: now,
            updated_at: now,
            completed_at: None,
            metrics: TaskMetrics::default(),
            comments: Vec::new(),
            compacted: false,
            summary: None,
        }
    }

    // -- transition_from_any --

    // transition_from_any() only succeeds when the task's current state is in
    // `from`. Cases where initial is in `from` should succeed; mismatches
    // should leave the task unchanged with its original updated_at timestamp.
    #[rstest]
    #[case::matching_pending(TaskState::Pending, TaskState::Pending, TaskState::InProgress, true)]
    #[case::matching_in_progress(
        TaskState::InProgress,
        TaskState::InProgress,
        TaskState::Completed,
        true
    )]
    #[case::mismatch_completed(
        TaskState::Completed,
        TaskState::Pending,
        TaskState::InProgress,
        false
    )]
    #[case::mismatch_failed(TaskState::Failed, TaskState::Pending, TaskState::InProgress, false)]
    fn transition_from_any_checks_current_state(
        mut task: Task,
        #[case] initial: TaskState,
        #[case] from: TaskState,
        #[case] to: TaskState,
        #[case] expected: bool,
    ) {
        task.state = initial;
        let before = task.updated_at;
        let result = task.transition_from_any(&[from], to);
        assert_eq!(result, expected);
        if expected {
            assert_eq!(task.state, to);
            assert!(task.updated_at >= before);
        } else {
            assert_eq!(task.state, initial);
            assert_eq!(task.updated_at, before);
        }
    }

    // transition_from_any() accepts a list of valid source states.
    // Only states in the list should transition; others are rejected.
    #[rstest]
    #[case::in_progress_matches(TaskState::InProgress, true)]
    #[case::verifying_matches(TaskState::Verifying, true)]
    #[case::pending_rejected(TaskState::Pending, false)]
    #[case::completed_rejected(TaskState::Completed, false)]
    fn transition_from_any_matches_list(
        mut task: Task,
        #[case] current: TaskState,
        #[case] expected: bool,
    ) {
        task.state = current;
        let result = task.transition_from_any(
            &[TaskState::InProgress, TaskState::Verifying],
            TaskState::Failed,
        );
        assert_eq!(result, expected);
        if expected {
            assert_eq!(task.state, TaskState::Failed);
        } else {
            assert_eq!(task.state, current);
        }
    }

    // -- complete --

    // Completing an InProgress task should set state, result, metrics,
    // completed_at, and updated_at all in one shot.
    #[rstest]
    fn complete_sets_all_fields(mut task: Task) {
        task.state = TaskState::InProgress;
        let outcome = Outcome::new("done".to_string(), vec!["file.txt".to_string()]);
        let metrics = TaskMetrics::new(100, 5000, 1);

        assert!(task.complete(outcome, metrics));
        assert_eq!(task.state, TaskState::Completed);
        assert!(task.completed_at.is_some());
        assert_eq!(task.result.as_ref().unwrap().summary(), "done");
        assert_eq!(task.metrics.tokens, 100);
        assert_eq!(task.metrics.retry_count, 1);
    }

    // complete() is only valid from InProgress. Every other state should
    // be rejected, leaving the task untouched.
    #[rstest]
    #[case::from_pending(TaskState::Pending)]
    #[case::from_blocked(TaskState::Blocked)]
    #[case::from_completed(TaskState::Completed)]
    #[case::from_failed(TaskState::Failed)]
    fn complete_rejects_non_in_progress(mut task: Task, #[case] state: TaskState) {
        task.state = state;
        let outcome = Outcome::new("done".to_string(), Vec::new());
        assert!(!task.complete(outcome, TaskMetrics::default()));
        assert_eq!(task.state, state);
        assert!(task.completed_at.is_none());
    }

    // -- retry --

    // Retrying a failed task should move it back to InProgress and
    // bump the retry counter.
    #[rstest]
    fn retry_increments_and_transitions(mut task: Task) {
        task.state = TaskState::Failed;
        task.metrics.retry_count = 2;
        assert!(task.retry());
        assert_eq!(task.state, TaskState::InProgress);
        assert_eq!(task.metrics.retry_count, 3);
    }

    // retry() is only valid from Failed. Every other state should be rejected.
    #[rstest]
    #[case::from_pending(TaskState::Pending)]
    #[case::from_in_progress(TaskState::InProgress)]
    #[case::from_completed(TaskState::Completed)]
    #[case::from_blocked(TaskState::Blocked)]
    fn retry_rejects_non_failed(mut task: Task, #[case] state: TaskState) {
        task.state = state;
        assert!(!task.retry());
        assert_eq!(task.state, state);
    }

    // -- cancel --

    // Cancelling from valid states (Pending, Blocked, InProgress, Verifying, Failed)
    // should transition to Cancelled and update timestamp.
    #[rstest]
    #[case::from_pending(TaskState::Pending)]
    #[case::from_blocked(TaskState::Blocked)]
    #[case::from_in_progress(TaskState::InProgress)]
    #[case::from_verifying(TaskState::Verifying)]
    #[case::from_failed(TaskState::Failed)]
    fn cancel_succeeds_from_valid_states(mut task: Task, #[case] state: TaskState) {
        task.state = state;
        let before = task.updated_at;
        assert!(task.cancel());
        assert_eq!(task.state, TaskState::Cancelled);
        assert!(task.updated_at >= before);
    }

    // cancel() rejects Completed tasks (completed work is history).
    #[rstest]
    fn cancel_rejects_completed(mut task: Task) {
        task.state = TaskState::Completed;
        assert!(!task.cancel());
        assert_eq!(task.state, TaskState::Completed);
    }

    // cancel() rejects already-Cancelled tasks (idempotency check).
    #[rstest]
    fn cancel_rejects_already_cancelled(mut task: Task) {
        task.state = TaskState::Cancelled;
        let before = task.updated_at;
        assert!(!task.cancel());
        assert_eq!(task.state, TaskState::Cancelled);
        assert_eq!(task.updated_at, before); // No timestamp change
    }

    // -- unblock --

    // Unblocking sets the task to Pending unconditionally and bumps updated_at.
    #[rstest]
    fn unblock_sets_pending(mut task: Task) {
        task.state = TaskState::Blocked;
        let before = task.updated_at;
        task.unblock();
        assert_eq!(task.state, TaskState::Pending);
        assert!(task.updated_at >= before);
    }

    // -- add_comment --

    // Adding a comment should append to the list and bump updated_at.
    #[rstest]
    fn add_comment_appends_and_updates_timestamp(mut task: Task) {
        let before = task.updated_at;
        let comment = Comment::new("c_1".to_string(), "hello".to_string(), Timestamp::now());
        task.add_comment(comment);

        assert_eq!(task.comments.len(), 1);
        assert_eq!(task.comments[0].text(), "hello");
        assert!(task.updated_at >= before);
    }

    // -- assignee --

    // Setting an assignee should store the value and bump updated_at.
    #[rstest]
    fn set_assignee_stores_value(mut task: Task) {
        let before = task.updated_at;
        task.set_assignee(Some("agent-1".to_string()));
        assert_eq!(task.assignee(), Some("agent-1"));
        assert!(task.updated_at >= before);
    }

    // Clearing an assignee should set it to None.
    #[rstest]
    fn set_assignee_clears_value(mut task: Task) {
        task.assignee = Some("agent-1".to_string());
        task.set_assignee(None);
        assert_eq!(task.assignee(), None);
    }

    // -- release --

    // Releasing an assigned in-progress task should clear assignee and set Pending.
    #[rstest]
    fn release_clears_assignee_and_sets_pending(mut task: Task) {
        task.state = TaskState::InProgress;
        task.assignee = Some("agent-1".to_string());
        let before = task.updated_at;
        assert!(task.release());
        assert_eq!(task.state, TaskState::Pending);
        assert_eq!(task.assignee(), None);
        assert!(task.updated_at >= before);
    }

    // Releasing a failed task with an assignee should also work.
    #[rstest]
    fn release_works_from_failed(mut task: Task) {
        task.state = TaskState::Failed;
        task.assignee = Some("agent-1".to_string());
        assert!(task.release());
        assert_eq!(task.state, TaskState::Pending);
        assert_eq!(task.assignee(), None);
    }

    // Releasing a task with no assignee should fail.
    #[rstest]
    fn release_rejects_unassigned(mut task: Task) {
        task.state = TaskState::InProgress;
        assert!(!task.release());
        assert_eq!(task.state, TaskState::InProgress);
    }

    // -- started_at --

    // Transitioning to InProgress should set started_at.
    #[rstest]
    fn transition_to_in_progress_sets_started_at(mut task: Task) {
        assert!(task.started_at.is_none());
        task.transition_from_any(&[TaskState::Pending], TaskState::InProgress);
        assert!(task.started_at.is_some());
    }

    // Transitioning to a non-InProgress state should not set started_at.
    #[rstest]
    fn transition_to_other_state_does_not_set_started_at(mut task: Task) {
        task.state = TaskState::InProgress;
        task.transition_from_any(&[TaskState::InProgress], TaskState::Completed);
        assert!(task.started_at.is_none());
    }

    // Retrying a failed task should set started_at.
    #[rstest]
    fn retry_sets_started_at(mut task: Task) {
        task.state = TaskState::Failed;
        assert!(task.started_at.is_none());
        task.retry();
        assert!(task.started_at.is_some());
    }

    // Releasing a task should clear started_at.
    #[rstest]
    fn release_clears_started_at(mut task: Task) {
        task.state = TaskState::InProgress;
        task.assignee = Some("agent-1".to_string());
        task.started_at = Some(Timestamp::now());
        task.release();
        assert!(task.started_at.is_none());
    }

    // Completing a task should preserve started_at.
    #[rstest]
    fn complete_preserves_started_at(mut task: Task) {
        task.state = TaskState::InProgress;
        let ts = Timestamp::now();
        task.started_at = Some(ts);
        let outcome = Outcome::new("done".to_string(), Vec::new());
        task.complete(outcome, TaskMetrics::default());
        assert_eq!(task.started_at, Some(ts));
    }

    // -- compact --

    // Compacting a completed task should set compacted=true, store the summary,
    // replace description, clear contract and comments, and bump updated_at.
    #[rstest]
    fn compact_completed_task(mut task: Task) {
        task.state = TaskState::Completed;
        task.contract = Some(Contract::new(
            "input".to_string(),
            "output".to_string(),
            "verify".to_string(),
        ));
        task.comments = vec![Comment::new(
            "c1".to_string(),
            "a comment".to_string(),
            Timestamp::now(),
        )];
        let before = task.updated_at;

        assert!(task.compact("Summarized the task.".to_string()));
        assert!(task.compacted);
        assert_eq!(task.summary.as_deref(), Some("Summarized the task."));
        assert_eq!(task.description, "[compacted]");
        assert!(task.contract.is_none());
        assert!(task.comments.is_empty());
        assert!(task.updated_at >= before);
    }

    // Compacting a failed task should also work.
    #[rstest]
    fn compact_failed_task(mut task: Task) {
        task.state = TaskState::Failed;
        assert!(task.compact("Failed task summary.".to_string()));
        assert!(task.compacted);
        assert_eq!(task.summary.as_deref(), Some("Failed task summary."));
    }

    // Compacting a pending task should be rejected.
    #[rstest]
    #[case::pending(TaskState::Pending)]
    #[case::blocked(TaskState::Blocked)]
    #[case::in_progress(TaskState::InProgress)]
    fn compact_rejects_active_states(mut task: Task, #[case] state: TaskState) {
        task.state = state;
        assert!(!task.compact("nope".to_string()));
        assert!(!task.compacted);
        assert!(task.summary.is_none());
    }

    // Compacting an already-compacted task should be rejected.
    #[rstest]
    fn compact_rejects_already_compacted(mut task: Task) {
        task.state = TaskState::Completed;
        assert!(task.compact("first".to_string()));
        assert!(!task.compact("second".to_string()));
        assert_eq!(task.summary.as_deref(), Some("first"));
    }

    #[test]
    fn display_ref_with_seq() {
        let mut task = task();
        task.seq = Some(3);
        assert_eq!(task.display_ref(5), Some("g5.3".to_string()));
    }

    #[test]
    fn display_ref_without_seq() {
        let task = task();
        assert_eq!(task.display_ref(5), None);
    }
}