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
use std::{collections::HashSet, time::Instant};

use crate::{
    openspec::Change, orchestration::operator_command::is_final_status, task_parser,
    tui::events::LogEntry,
};

use super::{AppState, ChangeState};

/// Drop catalog observations for rows the operator dismissed, and revive an ID
/// this refresh proves is active again.
///
/// Catalog processing is the only place rows are *generated*, so it is also the
/// only place dismissal has to be enforced: reducer synchronization mutates
/// existing rows and can never resurrect one. Two directions, both required by
/// the dismissal contract:
///
/// * a dismissed ID observed as an **active non-terminal** change is no longer
///   the retained terminal row the operator dismissed, so dismissal is cleared
///   and the row is rendered normally; and
/// * every other dismissed observation is removed from this pass entirely —
///   before new-row creation, NEW counting, and the detection log — so a
///   suppressed change is never announced as newly detected.
///
/// Terminality is judged from the reducer's own latest display-status snapshot
/// rather than from the row (the dismissed row is gone) or from the catalog
/// (which reports directory presence, not lifecycle).
fn suppress_dismissed_rows(
    state: &mut AppState,
    fetched_changes: Vec<Change>,
    rejected_changes: Vec<Change>,
) -> (Vec<Change>, Vec<Change>) {
    if state.dismissed_merged_ids.is_empty() {
        return (fetched_changes, rejected_changes);
    }

    let reactivated: Vec<String> = fetched_changes
        .iter()
        .filter(|change| state.dismissed_merged_ids.contains(&change.id))
        .filter(|change| {
            !state
                .reducer_display_status_snapshot
                .get(&change.id)
                .copied()
                .is_some_and(is_final_status)
        })
        .map(|change| change.id.clone())
        .collect();

    for id in &reactivated {
        state.dismissed_merged_ids.remove(id);
        state.add_log(
            LogEntry::info(format!(
                "Change '{}' is active again; restoring its dismissed row",
                id
            ))
            .with_change_id(id),
        );
    }

    let keep = |change: &Change| !state.dismissed_merged_ids.contains(&change.id);
    (
        fetched_changes.into_iter().filter(keep).collect(),
        rejected_changes.into_iter().filter(keep).collect(),
    )
}

pub(super) fn update_changes_with_rejected(
    state: &mut AppState,
    fetched_changes: Vec<Change>,
    rejected_changes: Vec<Change>,
) {
    // Progress is written to shared orchestration state from the *unfiltered*
    // observation on purpose. Dismissal is a local presentation decision, and a
    // row an operator stopped looking at must not change what the reducer, the
    // API, or any other frontend is told about the workspace.
    if let Some(shared_state) = &state.shared_orchestrator_state {
        if let Ok(mut guard) = shared_state.try_write() {
            for change in &fetched_changes {
                if change.total_tasks > 0 {
                    guard.set_task_progress(
                        change.id.clone(),
                        change.completed_tasks,
                        change.total_tasks,
                    );
                }
            }
            for change in &rejected_changes {
                if change.total_tasks > 0 {
                    guard.set_task_progress(
                        change.id.clone(),
                        change.completed_tasks,
                        change.total_tasks,
                    );
                }
            }
        }
    }

    // Everything below is presentation: rows, identity bookkeeping, the NEW
    // badge, and the detection log. Dismissed observations are removed here so
    // none of them can see a row the operator hid.
    let (fetched_changes, rejected_changes) =
        suppress_dismissed_rows(state, fetched_changes, rejected_changes);
    let active_ids: HashSet<String> = fetched_changes.iter().map(|c| c.id.clone()).collect();
    let rejected_ids: HashSet<String> = rejected_changes.iter().map(|c| c.id.clone()).collect();

    let new_ids: Vec<String> = fetched_changes
        .iter()
        .chain(rejected_changes.iter())
        .filter(|c| !state.known_change_ids.contains(&c.id))
        .map(|c| c.id.clone())
        .collect();
    let new_active_ids: Vec<String> = fetched_changes
        .iter()
        .filter(|c| !state.known_change_ids.contains(&c.id))
        .map(|c| c.id.clone())
        .collect();

    for fetched in &fetched_changes {
        if let Some(existing) = state.changes.iter_mut().find(|c| c.id == fetched.id) {
            let was_rejected = existing.display_status_cache == "rejected";
            let was_archived = existing.display_status_cache == "archived";
            let is_merge_wait = existing.display_status_cache == "merge wait";
            let is_resolve_wait = existing.display_status_cache == "resolve pending";

            if was_rejected && !rejected_ids.contains(&fetched.id) {
                existing.set_display_status_cache("not queued");
            }

            let (completed, total) = if let Some(shared_state) = &state.shared_orchestrator_state {
                if let Ok(guard) = shared_state.try_read() {
                    let progress = guard.task_progress(&fetched.id);
                    if progress.1 > 0 {
                        progress
                    } else {
                        (fetched.completed_tasks, fetched.total_tasks)
                    }
                } else {
                    (fetched.completed_tasks, fetched.total_tasks)
                }
            } else {
                (fetched.completed_tasks, fetched.total_tasks)
            };

            if was_archived {
                existing.set_display_status_cache("not queued");
                if total > 0 {
                    existing.completed_tasks = completed;
                    existing.total_tasks = total;
                }
            } else if is_merge_wait || is_resolve_wait {
                if total > 0 {
                    existing.completed_tasks = completed;
                    existing.total_tasks = total;
                }
            } else if total > 0 {
                existing.completed_tasks = completed;
                existing.total_tasks = total;
            } else {
                let worktree_path = state.worktree_paths.get(&fetched.id).map(|p| p.as_path());
                match existing.display_status_cache.as_str() {
                    "archiving" | "resolving" | "archived" | "merged" => {
                        if let Ok(progress) =
                            task_parser::parse_progress_with_fallback(&fetched.id, worktree_path)
                        {
                            if progress.total > 0 {
                                existing.completed_tasks = progress.completed;
                                existing.total_tasks = progress.total;
                            }
                        }
                    }
                    _ => {}
                }
            }
        }
    }

    for rejected in &rejected_changes {
        if let Some(existing) = state.changes.iter_mut().find(|c| c.id == rejected.id) {
            let (completed, total) = if let Some(shared_state) = &state.shared_orchestrator_state {
                if let Ok(guard) = shared_state.try_read() {
                    let progress = guard.task_progress(&rejected.id);
                    if progress.1 > 0 {
                        progress
                    } else {
                        (rejected.completed_tasks, rejected.total_tasks)
                    }
                } else {
                    (rejected.completed_tasks, rejected.total_tasks)
                }
            } else {
                (rejected.completed_tasks, rejected.total_tasks)
            };

            if total > 0 {
                existing.completed_tasks = completed;
                existing.total_tasks = total;
            }
            existing.set_display_status_cache("rejected");
        }
    }

    for id in &new_ids {
        if let Some(fetched) = fetched_changes.iter().find(|c| &c.id == id) {
            let mut new_state = ChangeState::from_change(fetched);
            new_state.is_new = true;
            state.changes.push(new_state);
            continue;
        }

        if let Some(rejected) = rejected_changes.iter().find(|c| &c.id == id) {
            let mut rejected_state = ChangeState::from_change(rejected);
            rejected_state.is_new = false;
            rejected_state.set_display_status_cache("rejected");
            state.changes.push(rejected_state);
        }
    }

    for id in &new_active_ids {
        state.add_log(LogEntry::info(format!("Detected new change: {}", id)));
    }

    state.known_change_ids.extend(new_ids);
    state.last_refresh = Instant::now();

    if let Some(shared_state) = &state.shared_orchestrator_state {
        if let Ok(guard) = shared_state.try_read() {
            for change in &mut state.changes {
                let apply_count = guard.apply_count(&change.id);
                if apply_count > 0 {
                    match change.iteration_number {
                        Some(existing) => {
                            if apply_count > existing {
                                change.iteration_number = Some(apply_count);
                            }
                        }
                        None => change.iteration_number = Some(apply_count),
                    }
                }
            }
        }
    }

    state.changes.retain(|c| {
        active_ids.contains(&c.id)
            || rejected_ids.contains(&c.id)
            // Either an open active interval or a retained accumulated
            // duration proves this row executed, so an inactive row whose
            // interval was already closed survives a temporary catalog absence
            // exactly as a still-running one does — no separate durable flag.
            || c.has_execution_history()
            || matches!(
                c.display_status_cache.as_str(),
                "archiving"
                    | "archived"
                    | "merged"
                    | "merge wait"
                    | "resolving"
                    | "resolve pending"
                    | "rejected"
                    | "error"
            )
    });

    // Identity bookkeeping converges to the row projection that just survived.
    //
    // An ID must not outlive its row. A change can be absent from a single
    // filesystem snapshot while its worktree is refreshed or merged; the retain
    // above drops the row, and a known-ID entry left behind would classify the
    // change as already seen when it is observed again, so the row could never
    // be reconstructed. Conversely an ID whose row was deliberately retained
    // through the absence — recorded execution history, or a terminal/wait
    // display status — stays known, so re-observing it updates that row in
    // place instead of pushing a duplicate NEW row.
    //
    // This is observability bookkeeping only: it decides whether a row is
    // painted as newly detected and logged, never queue membership, dispatch,
    // resume routing, acceptance, or archive routing.
    let retained_row_ids: HashSet<&str> = state.changes.iter().map(|c| c.id.as_str()).collect();
    state
        .known_change_ids
        .retain(|id| retained_row_ids.contains(id.as_str()));

    // Counted from the settled projection rather than from the pre-retain row
    // list, so the NEW badge can never advertise a row that this same pass
    // dropped.
    state.new_change_count = state.changes.iter().filter(|c| c.is_new).count();

    // Rows this pass created or re-created start with no archive-completion cache,
    // so the retained reducer snapshot is reapplied before anything renders. Doing
    // it here rather than waiting for the next reducer sync is what keeps a
    // post-archive row from flashing an empty checkbox for one frame.
    state.reapply_reducer_archive_completion();

    if state.cursor_index >= state.changes.len() && !state.changes.is_empty() {
        state.cursor_index = state.changes.len() - 1;
        state.list_state.select(Some(state.cursor_index));
    }
}

#[cfg(test)]
mod dismissal_tests {
    use std::collections::HashMap;

    use super::*;
    use crate::openspec::ProposalMetadata;

    fn change(id: &str) -> Change {
        Change {
            id: id.to_string(),
            completed_tasks: 1,
            total_tasks: 1,
            last_modified: "now".to_string(),
            dependencies: Vec::new(),
            metadata: ProposalMetadata::default(),
        }
    }

    /// An app whose rows carry exactly the reducer statuses given.
    ///
    /// The statuses go through the reducer sync rather than being written onto
    /// the rows, so the retained snapshot the suppression rule reads is the same
    /// one production fills.
    fn app(rows: &[(&str, &'static str)]) -> AppState {
        let mut app = AppState::new(rows.iter().map(|(id, _)| change(id)).collect());
        let display: HashMap<String, &'static str> = rows
            .iter()
            .map(|(id, status)| (id.to_string(), *status))
            .collect();
        app.apply_display_statuses_from_reducer(&display);
        app.logs.clear();
        app
    }

    fn row_ids(app: &AppState) -> Vec<String> {
        app.changes.iter().map(|c| c.id.clone()).collect()
    }

    /// Dismiss the focused row through the same single key action the operator
    /// takes: one call, no confirmation.
    fn dismiss_focused(app: &mut AppState) {
        assert!(app.dismiss_merged_row());
        assert_eq!(app.modal, None, "dismissal must not open an overlay");
    }

    #[test]
    fn a_dismissed_row_is_not_recreated_by_a_retained_terminal_observation() {
        let mut app = app(&[("alpha", "merged"), ("beta", "not queued")]);
        dismiss_focused(&mut app);
        app.logs.clear();

        // The catalog still lists `alpha` while the reducer keeps reporting the
        // terminal outcome the operator dismissed.
        for _ in 0..3 {
            update_changes_with_rejected(&mut app, vec![change("alpha"), change("beta")], vec![]);
        }

        assert_eq!(row_ids(&app), vec!["beta"]);
        assert!(app.dismissed_merged_ids().contains("alpha"));
        assert!(!app.known_change_ids.contains("alpha"));
        assert_eq!(app.new_change_count, 0);
        assert!(
            !app.logs
                .iter()
                .any(|entry| entry.message.contains("Detected new change: alpha")),
            "a suppressed row must not be announced as newly detected"
        );
    }

    #[test]
    fn a_rejected_observation_of_a_dismissed_id_stays_suppressed() {
        let mut app = app(&[("alpha", "merged")]);
        dismiss_focused(&mut app);

        update_changes_with_rejected(&mut app, vec![], vec![change("alpha")]);

        assert!(app.changes.is_empty());
        assert!(app.dismissed_merged_ids().contains("alpha"));
    }

    #[test]
    fn an_active_non_terminal_observation_revives_a_dismissed_id() {
        let mut app = app(&[("alpha", "merged"), ("beta", "not queued")]);
        dismiss_focused(&mut app);

        // The reducer no longer reports a terminal outcome for `alpha`, and the
        // catalog observes it again: this is a different change wearing the same
        // ID, not the row that was dismissed.
        app.apply_display_statuses_from_reducer(&HashMap::new());
        update_changes_with_rejected(&mut app, vec![change("alpha"), change("beta")], vec![]);

        assert!(row_ids(&app).contains(&"alpha".to_string()));
        assert!(app.dismissed_merged_ids().is_empty());
        assert_eq!(app.new_change_count, 1);

        // And it keeps rendering on every later refresh.
        update_changes_with_rejected(&mut app, vec![change("alpha"), change("beta")], vec![]);
        assert!(row_ids(&app).contains(&"alpha".to_string()));
    }

    #[test]
    fn suppression_leaves_every_other_row_untouched() {
        let mut app = app(&[
            ("alpha", "merged"),
            ("beta", "not queued"),
            ("gamma", "merged"),
        ]);
        dismiss_focused(&mut app);

        update_changes_with_rejected(
            &mut app,
            vec![change("alpha"), change("beta"), change("gamma")],
            vec![],
        );

        assert_eq!(row_ids(&app), vec!["beta", "gamma"]);
        assert_eq!(app.changes[1].display_status_cache, "merged");
    }

    #[test]
    fn dismissal_does_not_change_what_the_reducer_is_told_about_the_workspace() {
        // Presentation is local; shared orchestration state is not. A dismissed
        // change's observed task progress must still reach the shared state that
        // `/api/v2` and every other frontend read.
        let mut app = app(&[("alpha", "merged")]);
        dismiss_focused(&mut app);

        let shared = std::sync::Arc::new(tokio::sync::RwLock::new(
            crate::orchestration::state::OrchestratorState::new(Vec::new(), 1),
        ));
        app.shared_orchestrator_state = Some(shared.clone());

        let mut observed = change("alpha");
        observed.completed_tasks = 2;
        observed.total_tasks = 5;
        update_changes_with_rejected(&mut app, vec![observed], vec![]);

        let guard = shared.try_read().expect("uncontended shared state");
        assert_eq!(guard.task_progress("alpha"), (2, 5));
        assert!(app.changes.is_empty());
    }
}