cflx 0.6.327

Conflux – a spec-driven parallel coding orchestrator that runs AI agents on git worktrees
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
use crate::runtime::ids::ProposalId;

/// Monotonic reducer revision used to ignore stale runtime observations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct RuntimeRevision(pub u64);

/// Worktree or workspace reference carried only while a proposal is being processed.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct WorkspaceRef {
    pub path: String,
    pub branch: Option<String>,
}

impl WorkspaceRef {
    pub fn new(path: impl Into<String>) -> Self {
        Self {
            path: path.into(),
            branch: None,
        }
    }

    pub fn with_branch(mut self, branch: impl Into<String>) -> Self {
        self.branch = Some(branch.into());
        self
    }
}

/// Why a proposal is `blocked`.
///
/// Lifecycle status and blocker kind are deliberately independent concepts: a
/// dependency wait and a validated external prerequisite wait share the
/// `blocked` lifecycle but never lose their distinct explanation. `Stalled` has
/// no blocker kind at all — it is an execution outcome, not a wait.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BlockerKind {
    /// Waiting on an unarchived proposal dependency, derived from the proposal
    /// graph.
    Dependency,
    /// Waiting on a validated non-repository prerequisite.
    External,
}

impl BlockerKind {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Dependency => "dependency",
            Self::External => "external",
        }
    }
}

/// Phase that observed and reported an external prerequisite.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BlockerOrigin {
    Apply,
    Acceptance,
}

impl BlockerOrigin {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Apply => "apply",
            Self::Acceptance => "acceptance",
        }
    }
}

/// Validated operator-facing detail for an external prerequisite wait.
///
/// Every field is authored by the reporting agent and validated by the
/// orchestrator before this value exists; nothing here is inferred from prose or
/// from a compatibility verdict token.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ExternalBlockerInfo {
    /// Phase that observed the prerequisite.
    pub origin: BlockerOrigin,
    /// Supported blocker category.
    pub category: String,
    /// Concrete non-empty evidence.
    pub evidence: Vec<String>,
    /// Owning team/role or named prerequisite, when supplied.
    pub prerequisite_owner: Option<String>,
    /// Verifiable condition that clears the wait.
    pub unblock_condition: String,
    /// Operator-facing action that can satisfy the condition.
    pub next_action: String,
    /// Whether execution can resume once the prerequisite is satisfied.
    pub resumable: bool,
}

impl ExternalBlockerInfo {
    /// One-line operator summary preserving category, condition, and action.
    pub fn summary(&self) -> String {
        format!(
            "external blocker ({}) reported by {}: {}; unblock when {}; next action {}",
            self.category,
            self.origin.as_str(),
            self.evidence.join(" | "),
            self.unblock_condition,
            self.next_action
        )
    }
}

/// Operator-facing blocker details for stalled or dependency-blocked proposals.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BlockerInfo {
    pub category: String,
    pub summary: String,
    pub retry_count: u32,
}

impl BlockerInfo {
    pub fn new(category: impl Into<String>, summary: impl Into<String>) -> Self {
        Self {
            category: category.into(),
            summary: summary.into(),
            retry_count: 0,
        }
    }

    pub fn with_retry_count(mut self, retry_count: u32) -> Self {
        self.retry_count = retry_count;
        self
    }
}

/// Single canonical lifecycle status for one proposal.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum ProposalStatus {
    #[default]
    NotQueued,
    Queued {
        revision: RuntimeRevision,
    },
    DependencyBlocked {
        blocker: BlockerInfo,
        revision: RuntimeRevision,
    },
    /// Waiting on a validated non-repository prerequisite.
    ///
    /// A separate variant (rather than a flag on `Stalled` or
    /// `DependencyBlocked`) is what makes simultaneous or ambiguous
    /// blocked/stalled state unrepresentable.
    ExternalBlocked {
        blocker: ExternalBlockerInfo,
        revision: RuntimeRevision,
    },
    Applying {
        workspace: WorkspaceRef,
        attempt: u32,
        revision: RuntimeRevision,
    },
    Accepting {
        workspace: WorkspaceRef,
        attempt: u32,
        revision: RuntimeRevision,
    },
    Rejecting {
        workspace: WorkspaceRef,
        attempt: u32,
        revision: RuntimeRevision,
    },
    Stalled {
        blocker: BlockerInfo,
        revision: RuntimeRevision,
    },
    Archiving {
        workspace: WorkspaceRef,
        attempt: u32,
        revision: RuntimeRevision,
    },
    MergeWait {
        workspace: WorkspaceRef,
        revision: RuntimeRevision,
    },
    Resolving {
        workspace: WorkspaceRef,
        attempt: u32,
        revision: RuntimeRevision,
    },
    Merged {
        revision: RuntimeRevision,
    },
    Rejected {
        reason: String,
        revision: RuntimeRevision,
    },
    Failed {
        error: String,
        revision: RuntimeRevision,
    },
    Stopped {
        reason: String,
        revision: RuntimeRevision,
    },
}

impl ProposalStatus {
    pub fn label(&self) -> &'static str {
        match self {
            Self::NotQueued => "not_queued",
            Self::Queued { .. } => "queued",
            Self::DependencyBlocked { .. } => "dependency_blocked",
            Self::ExternalBlocked { .. } => "external_blocked",
            Self::Applying { .. } => "applying",
            Self::Accepting { .. } => "accepting",
            Self::Rejecting { .. } => "rejecting",
            Self::Stalled { .. } => "stalled",
            Self::Archiving { .. } => "archiving",
            Self::MergeWait { .. } => "merge_wait",
            Self::Resolving { .. } => "resolving",
            Self::Merged { .. } => "merged",
            Self::Rejected { .. } => "rejected",
            Self::Failed { .. } => "failed",
            Self::Stopped { .. } => "stopped",
        }
    }

    pub fn revision(&self) -> RuntimeRevision {
        match self {
            Self::NotQueued => RuntimeRevision::default(),
            Self::Queued { revision }
            | Self::DependencyBlocked { revision, .. }
            | Self::ExternalBlocked { revision, .. }
            | Self::Applying { revision, .. }
            | Self::Accepting { revision, .. }
            | Self::Rejecting { revision, .. }
            | Self::Stalled { revision, .. }
            | Self::Archiving { revision, .. }
            | Self::MergeWait { revision, .. }
            | Self::Resolving { revision, .. }
            | Self::Merged { revision }
            | Self::Rejected { revision, .. }
            | Self::Failed { revision, .. }
            | Self::Stopped { revision, .. } => *revision,
        }
    }

    /// Operator-facing lifecycle status shared by TUI, WebSocket/API, and the
    /// dashboard.
    ///
    /// Dependency waits and external prerequisite waits collapse to the same
    /// `blocked` word here on purpose; [`Self::blocker_kind`] is what keeps them
    /// distinguishable.
    pub fn display_status(&self) -> &'static str {
        match self {
            Self::NotQueued => "not queued",
            Self::Queued { .. } => "queued",
            Self::DependencyBlocked { .. } | Self::ExternalBlocked { .. } => "blocked",
            Self::Applying { .. } => "applying",
            Self::Accepting { .. } => "accepting",
            Self::Rejecting { .. } => "rejecting",
            Self::Stalled { .. } => "stalled",
            Self::Archiving { .. } => "archiving",
            Self::MergeWait { .. } => "merge wait",
            Self::Resolving { .. } => "resolving",
            Self::Merged { .. } => "merged",
            Self::Rejected { .. } => "rejected",
            Self::Failed { .. } => "error",
            Self::Stopped { .. } => "stopped",
        }
    }

    /// Machine-readable blocker kind for a `blocked` proposal.
    ///
    /// `None` for every other status, including `Stalled`: an execution hold is
    /// never a wait on a named prerequisite.
    pub fn blocker_kind(&self) -> Option<BlockerKind> {
        match self {
            Self::DependencyBlocked { .. } => Some(BlockerKind::Dependency),
            Self::ExternalBlocked { .. } => Some(BlockerKind::External),
            _ => None,
        }
    }

    /// Validated external blocker detail, when this proposal waits on one.
    pub fn external_blocker(&self) -> Option<&ExternalBlockerInfo> {
        match self {
            Self::ExternalBlocked { blocker, .. } => Some(blocker),
            _ => None,
        }
    }

    /// Whether ordinary dispatch must skip this proposal because it is holding
    /// on a wait or an execution stop rather than making progress.
    pub fn suppresses_ordinary_dispatch(&self) -> bool {
        matches!(
            self,
            Self::DependencyBlocked { .. } | Self::ExternalBlocked { .. } | Self::Stalled { .. }
        )
    }

    pub fn is_terminal(&self) -> bool {
        matches!(
            self,
            Self::Merged { .. }
                | Self::Rejected { .. }
                | Self::Failed { .. }
                | Self::Stopped { .. }
        )
    }

    pub fn is_base_lane_status(&self) -> bool {
        matches!(
            self,
            Self::Rejecting { .. }
                | Self::Archiving { .. }
                | Self::MergeWait { .. }
                | Self::Resolving { .. }
        )
    }
}

/// Runtime state for a single OpenSpec proposal/change.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProposalRuntimeState {
    pub id: ProposalId,
    pub status: ProposalStatus,
}

impl ProposalRuntimeState {
    pub fn new(id: impl Into<ProposalId>) -> Self {
        Self {
            id: id.into(),
            status: ProposalStatus::NotQueued,
        }
    }

    pub fn with_status(id: impl Into<ProposalId>, status: ProposalStatus) -> Self {
        Self {
            id: id.into(),
            status,
        }
    }
}

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

    #[test]
    fn proposal_status_labels_cover_all_lifecycle_states() {
        let workspace = WorkspaceRef::new("/tmp/worktree").with_branch("feature/x");
        let blocker = BlockerInfo::new("dependency", "waiting for base");
        let statuses = vec![
            ProposalStatus::NotQueued,
            ProposalStatus::Queued {
                revision: RuntimeRevision(1),
            },
            ProposalStatus::DependencyBlocked {
                blocker: blocker.clone(),
                revision: RuntimeRevision(2),
            },
            ProposalStatus::ExternalBlocked {
                blocker: external_blocker(),
                revision: RuntimeRevision(2),
            },
            ProposalStatus::Applying {
                workspace: workspace.clone(),
                attempt: 1,
                revision: RuntimeRevision(3),
            },
            ProposalStatus::Accepting {
                workspace: workspace.clone(),
                attempt: 1,
                revision: RuntimeRevision(4),
            },
            ProposalStatus::Rejecting {
                workspace: workspace.clone(),
                attempt: 1,
                revision: RuntimeRevision(5),
            },
            ProposalStatus::Stalled {
                blocker: blocker.clone(),
                revision: RuntimeRevision(6),
            },
            ProposalStatus::Archiving {
                workspace: workspace.clone(),
                attempt: 1,
                revision: RuntimeRevision(7),
            },
            ProposalStatus::MergeWait {
                workspace: workspace.clone(),
                revision: RuntimeRevision(8),
            },
            ProposalStatus::Resolving {
                workspace,
                attempt: 1,
                revision: RuntimeRevision(9),
            },
            ProposalStatus::Merged {
                revision: RuntimeRevision(10),
            },
            ProposalStatus::Rejected {
                reason: "not acceptable".to_string(),
                revision: RuntimeRevision(11),
            },
            ProposalStatus::Failed {
                error: "boom".to_string(),
                revision: RuntimeRevision(12),
            },
            ProposalStatus::Stopped {
                reason: "operator".to_string(),
                revision: RuntimeRevision(13),
            },
        ];

        let labels: Vec<_> = statuses.iter().map(ProposalStatus::label).collect();
        assert_eq!(
            labels,
            vec![
                "not_queued",
                "queued",
                "dependency_blocked",
                "external_blocked",
                "applying",
                "accepting",
                "rejecting",
                "stalled",
                "archiving",
                "merge_wait",
                "resolving",
                "merged",
                "rejected",
                "failed",
                "stopped",
            ]
        );
        assert!(statuses
            .iter()
            .find(|status| status.label() == "merged")
            .unwrap()
            .is_terminal());
        assert!(!statuses
            .iter()
            .find(|status| status.label() == "applying")
            .unwrap()
            .is_terminal());
    }

    fn external_blocker() -> ExternalBlockerInfo {
        ExternalBlockerInfo {
            origin: BlockerOrigin::Acceptance,
            category: "credential".to_string(),
            evidence: vec!["STAGING_API_KEY is unset".to_string()],
            prerequisite_owner: Some("platform".to_string()),
            unblock_condition: "STAGING_API_KEY is present in the verification environment"
                .to_string(),
            next_action: "provision STAGING_API_KEY then retry acceptance".to_string(),
            resumable: true,
        }
    }

    /// `blocked` and `stalled` are distinct enum variants, so a proposal can
    /// never be simultaneously or ambiguously both, and a `blocked` proposal
    /// always names its blocker kind.
    #[test]
    fn blocked_and_stalled_are_mutually_exclusive_and_keep_blocker_kind() {
        let dependency = ProposalStatus::DependencyBlocked {
            blocker: BlockerInfo::new("dependency", "waiting for alpha"),
            revision: RuntimeRevision(1),
        };
        let external = ProposalStatus::ExternalBlocked {
            blocker: external_blocker(),
            revision: RuntimeRevision(1),
        };
        let stalled = ProposalStatus::Stalled {
            blocker: BlockerInfo::new("repeated_acceptance_findings", "no semantic progress"),
            revision: RuntimeRevision(1),
        };

        assert_eq!(dependency.display_status(), "blocked");
        assert_eq!(external.display_status(), "blocked");
        assert_eq!(stalled.display_status(), "stalled");

        assert_eq!(dependency.blocker_kind(), Some(BlockerKind::Dependency));
        assert_eq!(external.blocker_kind(), Some(BlockerKind::External));
        assert_eq!(stalled.blocker_kind(), None);

        // Only the external wait exposes external prerequisite detail.
        assert!(dependency.external_blocker().is_none());
        assert!(stalled.external_blocker().is_none());
        let detail = external.external_blocker().unwrap();
        assert_eq!(detail.origin.as_str(), "acceptance");
        assert_eq!(detail.category, "credential");
        assert!(detail.resumable);
        assert!(detail.summary().contains("unblock when"));

        for status in [&dependency, &external, &stalled] {
            assert!(status.suppresses_ordinary_dispatch());
            assert!(!status.is_terminal());
        }
    }

    /// A dependent proposal's own dependency wait is never overwritten by the
    /// external blocker kind of the proposal it waits on.
    #[test]
    fn dependent_proposal_keeps_dependency_blocker_kind() {
        let alpha = ProposalRuntimeState::with_status(
            "alpha",
            ProposalStatus::ExternalBlocked {
                blocker: external_blocker(),
                revision: RuntimeRevision(1),
            },
        );
        let beta = ProposalRuntimeState::with_status(
            "beta",
            ProposalStatus::DependencyBlocked {
                blocker: BlockerInfo::new("dependency", "waiting for alpha"),
                revision: RuntimeRevision(1),
            },
        );

        assert_eq!(alpha.status.display_status(), beta.status.display_status());
        assert_eq!(alpha.status.blocker_kind(), Some(BlockerKind::External));
        assert_eq!(beta.status.blocker_kind(), Some(BlockerKind::Dependency));
        assert!(beta.status.external_blocker().is_none());
    }

    #[test]
    fn proposal_runtime_state_has_one_canonical_status_field() {
        let proposal = ProposalRuntimeState::with_status(
            "change-a",
            ProposalStatus::Queued {
                revision: RuntimeRevision(1),
            },
        );

        assert_eq!(proposal.id.as_change_id(), "change-a");
        assert_eq!(proposal.status.label(), "queued");
    }
}