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
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
//! Mark-stability settlement as the TUI adapter actually reaches it.
//!
//! The orchestration tests own the policy; these own the *adapter*. What has to
//! be true here is that Space and bulk `x` reach the shared coordinator with no
//! frontend timer and no new key, that an overlay which owns the keypress
//! produces no mark and therefore no deadline, that a parked persistent
//! scheduler is still eligible, that a process with no live scheduler stays
//! mark-only, and that a rejected Start leaves nothing behind for a deadline to
//! discover ten seconds later. It also owns the running-owner admission
//! regression: a live owner already executing one change must admit a second
//! marked target through settlement alone, including when the catalog admitted
//! that mark before the reducer had ever heard of the change.
//!
//! Integration-scoped within the process: the real coordinator, run-control
//! service, reducer, `DynamicQueue`, and dispatch owner are all wired, with only
//! the scheduler replaced by the shared recording double. No process,
//! repository, or network boundary is touched, and every deadline runs under
//! paused Tokio time.

use std::sync::Arc;
use std::time::Duration;

use super::tests::AdapterHarness;
use super::*;
use crate::orchestration::mark_settlement::{MarkSettlementCoordinator, MARK_STABILITY_WINDOW};
use crate::tui::state::AppState;
use crate::tui::types::AppExecutionMode;

/// A subscription to the settlement pass an armed deadline will produce.
///
/// Captured *before* time advances on purpose. Two harnesses armed in the same
/// paused instant both settle on the first advance, so a target computed after
/// that advance would already be in the past and could never be reached.
struct PendingPass {
    passes: tokio::sync::watch::Receiver<u64>,
    target: u64,
}

fn pending_pass(coordinator: &Arc<MarkSettlementCoordinator>) -> PendingPass {
    let passes = coordinator.passes();
    let target = *passes.borrow() + 1;
    PendingPass { passes, target }
}

impl PendingPass {
    async fn wait(mut self) {
        self.passes
            .wait_for(|observed| *observed >= self.target)
            .await
            .expect("the coordinator outlives its watchers");
    }
}

/// Advance past the stability deadline and wait for the settlement pass.
async fn settle(coordinator: &Arc<MarkSettlementCoordinator>) {
    let pending = pending_pass(coordinator);
    tokio::time::advance(MARK_STABILITY_WINDOW + Duration::from_millis(1)).await;
    pending.wait().await;
}

/// Advance past the deadline and assert no settlement pass ran at all.
async fn expect_no_settlement(coordinator: &Arc<MarkSettlementCoordinator>) {
    let before = *coordinator.passes().borrow();
    tokio::time::advance(MARK_STABILITY_WINDOW * 2).await;
    tokio::task::yield_now().await;
    assert_eq!(
        *coordinator.passes().borrow(),
        before,
        "no settlement pass may run"
    );
}

/// Drain the mark writes an interaction recorded, exactly as the runner does.
async fn drain_marks(harness: &AdapterHarness, app: &mut AppState) {
    let service = harness.application.run_control().operator();
    crate::tui::runner::apply_pending_mark_writes(app, &service).await;
}

fn coordinator(harness: &AdapterHarness) -> Arc<MarkSettlementCoordinator> {
    harness.marks.settlement()
}

/// A harness whose scheduler is live, i.e. capable of dynamic queue admission.
fn running_harness(change_ids: &[&str]) -> AdapterHarness {
    let harness = AdapterHarness::new(change_ids);
    harness.scheduler.set_running(true);
    harness
}

/// Dispatch the catalog refresh the TUI's own refresh loop produces.
///
/// Through the shared dispatch owner, not straight into the reducer: this is
/// what gives a newly created proposal reducer runtime state, and it is also
/// what runs mark reconciliation, so a test cannot accidentally admit a row the
/// production refresh would have cleaned up. Every named change is committed and
/// clean, i.e. parallel-eligible.
async fn refresh_catalog(harness: &AdapterHarness, change_ids: &[&str]) {
    harness
        .dispatcher
        .dispatch(crate::events::ExecutionEvent::ChangesRefreshed {
            changes: change_ids
                .iter()
                .map(|id| super::tests::create_test_change(id))
                .collect(),
            rejected_changes: Vec::new(),
            committed_change_ids: change_ids.iter().map(|id| (*id).to_string()).collect(),
            uncommitted_file_change_ids: std::collections::HashSet::new(),
            worktree_change_ids: std::collections::HashSet::new(),
            worktree_paths: std::collections::HashMap::new(),
            worktree_not_ahead_ids: std::collections::HashSet::new(),
            merge_wait_ids: std::collections::HashSet::new(),
        })
        .await;
}

/// Put `change_id` into live agent-driven execution, as a dispatched run does.
async fn start_executing(harness: &AdapterHarness, change_id: &str) {
    harness
        .application
        .apply(OperatorIntent::SetQueueIntent {
            change_id: change_id.to_string(),
            queued: true,
        })
        .await;
    harness
        .dispatcher
        .dispatch(crate::events::ExecutionEvent::ApplyStarted {
            change_id: change_id.to_string(),
            command: "echo".to_string(),
        })
        .await;
    assert_eq!(harness.status(change_id).await, "applying");
}

/// A live owner admits a newly marked target that the reducer has not seen yet.
///
/// The production failure this pins, taken from a live v0.6.298 owner: an
/// execution mark was accepted for a proposal created seconds earlier, the
/// stability window expired *before* the next catalog refresh gave that
/// proposal reducer runtime state, and settlement classified it `not_loadable`
/// and discarded the batch. One and a half seconds later the row was tracked,
/// eligible, still marked — and permanently `not queued`, with only a debug line
/// to say why. Nothing but another Start could recover it, which is exactly what
/// a mark on a running owner is supposed to make unnecessary.
///
/// Everything here is the production owner wiring: the shared operator/API
/// transaction, the real reducer, the real `DynamicQueue`, the real dispatch
/// owner with mark reconciliation bound, and a live scheduler with genuinely
/// concurrent active work.
#[tokio::test(start_paused = true)]
async fn running_mark_reanalysis_running_owner_admits_a_late_catalog_target() {
    // The owner is running `active` and knows nothing about `alpha` yet.
    let harness = running_harness(&["active"]);
    start_executing(&harness, "active").await;
    let wakes_before = harness.queue_port.wakes();

    // The operator marks `alpha` through the shared operator/API path. The
    // catalog admits it; the reducer has no runtime state for it at all.
    assert!(
        harness.state.read().await.change_runtime("alpha").is_none(),
        "the reducer must not yet track the freshly created proposal"
    );
    harness
        .application
        .apply(OperatorIntent::SetExecutionMark {
            change_id: "alpha".to_string(),
            marked: true,
        })
        .await;
    assert!(coordinator(&harness).is_armed());

    // The window expires first. There is nothing loadable to admit, so this pass
    // must keep the batch rather than answer with the absence of evidence.
    settle(&coordinator(&harness)).await;
    assert_eq!(
        coordinator(&harness).pending_snapshot(),
        Some(vec!["alpha".to_string()]),
        "an unloadable target keeps its deadline instead of losing the mark"
    );
    assert_eq!(
        harness.queue_port.wakes(),
        wakes_before,
        "nothing was applied"
    );

    // The refresh loop catches up and the row becomes loadable.
    refresh_catalog(&harness, &["active", "alpha"]).await;
    assert!(
        harness.marks.is_marked("alpha"),
        "the mark survives the refresh"
    );

    settle(&coordinator(&harness)).await;

    assert_eq!(
        harness.status("alpha").await,
        "queued",
        "a marked, eligible, ordinary target must gain queue intent without another Start; plan={:?}",
        coordinator(&harness).last_plan()
    );
    assert_eq!(
        harness.queue_port.wakes(),
        wakes_before + 1,
        "one applied membership change wakes scheduler analysis exactly once"
    );
    assert!(
        !coordinator(&harness).is_armed(),
        "a reconciled batch releases its deadline"
    );
    assert!(
        coordinator(&harness).last_failure().is_none(),
        "a batch that reconciled reports no lifecycle failure"
    );

    // The unrelated active change is untouched in every respect.
    assert_eq!(harness.status("active").await, "applying");
    assert!(harness.queue.contains("alpha").await);
}

/// A mark for a change that never becomes loadable stops silently waiting.
///
/// The bound on the repair: retaining the batch may not become an unbounded
/// re-arm loop, and the operator must be told the accepted mark produced no
/// admission instead of watching a marked row sit at `not queued` forever.
#[tokio::test(start_paused = true)]
async fn running_mark_reanalysis_unloadable_target_reports_a_stable_reason() {
    let harness = running_harness(&["active"]);
    start_executing(&harness, "active").await;

    harness
        .application
        .apply(OperatorIntent::SetExecutionMark {
            change_id: "ghost".to_string(),
            marked: true,
        })
        .await;

    for _ in 0..crate::orchestration::mark_settlement::MARK_SETTLEMENT_ATTEMPTS {
        settle(&coordinator(&harness)).await;
    }

    assert!(
        !coordinator(&harness).is_armed(),
        "the retry budget is finite"
    );
    assert_eq!(
        coordinator(&harness).last_failure(),
        Some(crate::orchestration::mark_settlement::MarkSettlementFailure::UnreconciledBatch),
    );
    expect_no_settlement(&coordinator(&harness)).await;
    assert_eq!(harness.status("active").await, "applying");
}

/// Which adapter accepted the mark must not change what settlement does.
#[derive(Debug, Clone, Copy)]
enum MarkEntryPoint {
    /// TUI `Space` on the cursor row.
    Space,
    /// TUI bulk `x` over every visible row.
    BulkX,
    /// `cflx client` / `/api/v2`, through the shared application transaction.
    Api,
}

impl MarkEntryPoint {
    /// Mark `change_id` the way this adapter does, and drain what it recorded.
    async fn mark(self, harness: &AdapterHarness, change_id: &str) {
        match self {
            Self::Space => {
                let mut app = harness.app(&[change_id]);
                app.execution_mode = AppExecutionMode::Running;
                app.cursor_index = 0;
                app.toggle_selection();
                drain_marks(harness, &mut app).await;
            }
            Self::BulkX => {
                let mut app = harness.app(&[change_id]);
                app.execution_mode = AppExecutionMode::Running;
                assert!(
                    crate::tui::key_handlers::handle_bulk_toggle_key(&mut app).is_empty(),
                    "a bulk mark emits no TUI command"
                );
                drain_marks(harness, &mut app).await;
            }
            Self::Api => {
                harness
                    .application
                    .apply(OperatorIntent::SetExecutionMark {
                        change_id: change_id.to_string(),
                        marked: true,
                    })
                    .await;
            }
        }
    }
}

/// Every mark adapter converges on the same settlement, and disturbs nothing.
///
/// The scope the repair must not widen: a retained batch re-reads its own
/// targets and only those, so an unmarked row the operator queued explicitly and
/// a marked row they removed from the queue explicitly both keep their intent
/// across every extra pass the retry costs.
#[tokio::test(start_paused = true)]
async fn running_mark_reanalysis_every_mark_adapter_settles_a_late_catalog_target() {
    for entry in [
        MarkEntryPoint::Space,
        MarkEntryPoint::BulkX,
        MarkEntryPoint::Api,
    ] {
        let harness = running_harness(&["active", "queued_only", "marked_but_removed"]);
        start_executing(&harness, "active").await;

        // An unmarked row the operator queued explicitly.
        harness
            .application
            .apply(OperatorIntent::SetQueueIntent {
                change_id: "queued_only".to_string(),
                queued: true,
            })
            .await;
        // A marked row the operator then removed from the queue explicitly.
        harness
            .application
            .apply(OperatorIntent::SetExecutionMark {
                change_id: "marked_but_removed".to_string(),
                marked: true,
            })
            .await;
        settle(&coordinator(&harness)).await;
        harness
            .application
            .apply(OperatorIntent::SetQueueIntent {
                change_id: "marked_but_removed".to_string(),
                queued: false,
            })
            .await;
        let wakes_before = harness.queue_port.wakes();

        entry.mark(&harness, "alpha").await;
        assert!(
            coordinator(&harness).is_armed(),
            "{entry:?} must reach the shared coordinator"
        );

        settle(&coordinator(&harness)).await;
        refresh_catalog(
            &harness,
            &["active", "queued_only", "marked_but_removed", "alpha"],
        )
        .await;
        settle(&coordinator(&harness)).await;

        assert_eq!(
            harness.status("alpha").await,
            "queued",
            "{entry:?} did not admit the late-catalog target"
        );
        assert_eq!(
            harness.queue_port.wakes(),
            wakes_before + 1,
            "{entry:?} woke scheduler analysis more than once"
        );
        assert_eq!(harness.status("active").await, "applying");
        assert_eq!(
            harness.status("queued_only").await,
            "queued",
            "{entry:?} disturbed an explicit queue addition"
        );
        assert_eq!(
            harness.status("marked_but_removed").await,
            "not queued",
            "{entry:?} undid an explicit queue removal"
        );
    }
}

#[tokio::test(start_paused = true)]
async fn running_mark_reanalysis_space_and_bulk_x_reach_the_same_settlement() {
    // Space: one row toggled, drained through the shared service.
    let space = running_harness(&["alpha"]);
    let mut app = space.app(&["alpha"]);
    app.execution_mode = AppExecutionMode::Running;
    app.cursor_index = 0;
    app.toggle_selection();
    drain_marks(&space, &mut app).await;
    assert!(
        coordinator(&space).is_armed(),
        "Space must reach the shared coordinator"
    );

    // Bulk `x`: the same store, the same coordinator, still no frontend timer.
    let bulk = running_harness(&["alpha"]);
    let mut bulk_app = bulk.app(&["alpha"]);
    bulk_app.execution_mode = AppExecutionMode::Running;
    assert!(
        crate::tui::key_handlers::handle_bulk_toggle_key(&mut bulk_app).is_empty(),
        "a bulk mark emits no TUI command"
    );
    drain_marks(&bulk, &mut bulk_app).await;
    assert!(
        coordinator(&bulk).is_armed(),
        "bulk `x` must reach the shared coordinator"
    );

    // One advance settles both: paused time is shared, so the two deadlines
    // armed in the same instant expire together. Both subscriptions are taken
    // before it for exactly that reason.
    let space_pass = pending_pass(&coordinator(&space));
    let bulk_pass = pending_pass(&coordinator(&bulk));
    tokio::time::advance(MARK_STABILITY_WINDOW + Duration::from_millis(1)).await;
    space_pass.wait().await;
    bulk_pass.wait().await;

    assert_eq!(space.status("alpha").await, "queued");
    assert_eq!(
        space.status("alpha").await,
        bulk.status("alpha").await,
        "both interactions settle into the identical queue state"
    );
}

#[tokio::test(start_paused = true)]
async fn running_mark_reanalysis_shared_operator_mark_command_schedules_the_same_settlement() {
    // The `/api/v2` equivalent of Space, through the same application
    // transaction. Neither frontend owns a timer, so both land on one deadline.
    let harness = running_harness(&["alpha"]);
    harness
        .application
        .apply(OperatorIntent::SetExecutionMark {
            change_id: "alpha".to_string(),
            marked: true,
        })
        .await;
    assert!(coordinator(&harness).is_armed());

    settle(&coordinator(&harness)).await;
    assert_eq!(harness.status("alpha").await, "queued");
}

#[tokio::test(start_paused = true)]
async fn running_mark_reanalysis_overlay_input_ownership_arms_nothing() {
    let harness = running_harness(&["alpha"]);
    let mut app = harness.app(&["alpha"]);
    app.execution_mode = AppExecutionMode::Running;
    app.show_warning_popup("blocked".to_string(), "an overlay owns input".to_string());

    // The overlay consumes the keypress, so `x` never reaches the bulk toggle.
    assert!(
        crate::tui::key_handlers::handle_warning_popup_key(
            &mut app,
            crossterm::event::KeyEvent::new(
                crossterm::event::KeyCode::Char('x'),
                crossterm::event::KeyModifiers::NONE,
            ),
        ),
        "the warning popup must own the keypress"
    );
    drain_marks(&harness, &mut app).await;

    assert!(harness.marks.marked_ids().is_empty());
    assert!(!coordinator(&harness).is_armed());
    expect_no_settlement(&coordinator(&harness)).await;
    assert_eq!(harness.status("alpha").await, "not queued");
}

#[tokio::test(start_paused = true)]
async fn running_mark_reanalysis_parked_persistent_scheduler_remains_eligible() {
    // A persistent scheduler reports Select while parked, and is still perfectly
    // able to admit work. Scheduler liveness is the authority, not the mode the
    // TUI happens to be presenting.
    let harness = running_harness(&["alpha"]);
    let mut app = harness.app(&["alpha"]);
    app.execution_mode = AppExecutionMode::Select;
    app.persistent_scheduler_idle = true;
    app.cursor_index = 0;
    app.toggle_selection();
    drain_marks(&harness, &mut app).await;

    assert!(coordinator(&harness).is_armed());
    settle(&coordinator(&harness)).await;
    assert_eq!(harness.status("alpha").await, "queued");
}

#[tokio::test(start_paused = true)]
async fn running_mark_reanalysis_process_without_live_scheduler_stays_mark_only() {
    // Default recording scheduler: nothing running, so nothing to admit into.
    let harness = AdapterHarness::new(&["alpha"]);
    for mode in [
        AppExecutionMode::Select,
        AppExecutionMode::Stopping,
        AppExecutionMode::Stopped,
        AppExecutionMode::Error,
    ] {
        let mut app = harness.app(&["alpha"]);
        app.execution_mode = mode;
        app.cursor_index = 0;
        app.toggle_selection();
        drain_marks(&harness, &mut app).await;

        assert_eq!(
            harness.marks.is_marked("alpha"),
            app.changes[0].selected,
            "{mode:?} still applies the mark itself"
        );
        assert!(
            !coordinator(&harness).is_armed(),
            "{mode:?} must arm no stability deadline without a live scheduler"
        );
    }

    expect_no_settlement(&coordinator(&harness)).await;
    assert_eq!(harness.status("alpha").await, "not queued");
    assert!(
        harness.queue.pop().await.is_none(),
        "DynamicQueue is untouched"
    );
}

#[tokio::test(start_paused = true)]
async fn running_mark_reanalysis_rejected_start_leaves_no_delayed_queue_effect() {
    // A parked persistent scheduler: live, so a deadline *could* be armed, which
    // is what makes "none was armed" a real assertion rather than a vacuous one.
    let harness = running_harness(&["alpha"]);
    let mut app = harness.app(&["alpha"]);
    app.execution_mode = AppExecutionMode::Select;
    // Arranged on the row and published from it, the way production does: an
    // `AppState` republishes the whole eligibility set from its own rows, so a
    // set written straight into the shared store would be erased the moment the
    // app joined it — and the Start fence would then admit the target.
    app.changes[0].parallel_eligibility =
        crate::orchestration::operator_command::ParallelEligibility::UncommittedProposalFiles;
    app.publish_parallel_runtime();
    assert_eq!(harness.parallel.ineligible_ids(), vec!["alpha".to_string()]);

    harness
        .run(
            &mut app,
            TuiCommand::StartProcessing(vec!["alpha".to_string()]),
        )
        .await;

    assert!(
        harness.marks.is_marked("alpha"),
        "the admission mark write still happened"
    );
    assert!(
        !coordinator(&harness).is_armed(),
        "a Start-admission mark write must arm no delayed settlement"
    );

    expect_no_settlement(&coordinator(&harness)).await;
    assert_eq!(
        harness.status("alpha").await,
        "not queued",
        "a rejected Start leaves no queue effect, immediate or delayed"
    );
    assert!(harness.queue.pop().await.is_none());
}