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
//! In-memory doubles for sequential resolve classification tests.
//!
//! The classifier holds all of the policy and [`ResolveEvidence`] holds all of
//! the Git access, so the complete decision table is exercisable without a
//! process, a filesystem, or a repository. One shared double keeps the
//! classifier's own tests and the conflict-layer retry tests honest about the
//! same evidence shape.

use super::{EvidenceResult, ResolveEvidence, SequentialMergeItem};
use crate::vcs::git::commands::{CommitDiffEntry, WorktreeIdentity};
use async_trait::async_trait;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

/// Minimal in-memory commit graph.
///
/// Real ancestry, first-parent lineage, and subject-range queries are computed
/// from it, so the classifier's decision table is exercised without a process
/// or filesystem boundary.
#[derive(Default, Clone)]
pub(crate) struct FakeRepo {
    pub(crate) parents: HashMap<String, Vec<String>>,
    pub(crate) subjects: HashMap<String, String>,
    pub(crate) trees: HashMap<String, Vec<String>>,
    pub(crate) diffs: HashMap<String, Vec<CommitDiffEntry>>,
    /// Committed blob text, keyed by `(revision, repository-relative path)`.
    pub(crate) files: HashMap<(String, String), String>,
}

impl FakeRepo {
    pub(crate) fn commit(&mut self, id: &str, subject: &str, parents: &[&str]) -> &mut Self {
        self.parents.insert(
            id.to_string(),
            parents.iter().map(|p| p.to_string()).collect(),
        );
        self.subjects.insert(id.to_string(), subject.to_string());
        self
    }

    pub(crate) fn tree(&mut self, id: &str, paths: &[&str]) -> &mut Self {
        self.trees.insert(
            id.to_string(),
            paths.iter().map(|p| p.to_string()).collect(),
        );
        self
    }

    /// Record a committed file at `id`, adding its path to that revision's tree.
    ///
    /// Tree membership and blob content are set together because Git cannot
    /// have one without the other, and a fixture that could would let a test
    /// pass against a state the classifier can never see.
    pub(crate) fn file(&mut self, id: &str, path: &str, content: &str) -> &mut Self {
        let tree = self.trees.entry(id.to_string()).or_default();
        if !tree.iter().any(|existing| existing == path) {
            tree.push(path.to_string());
        }
        self.files
            .insert((id.to_string(), path.to_string()), content.to_string());
        self
    }

    /// Record a committed file whose blob exists in no tree, as an unreadable
    /// path does.
    pub(crate) fn tree_only(&mut self, id: &str, path: &str) -> &mut Self {
        let tree = self.trees.entry(id.to_string()).or_default();
        if !tree.iter().any(|existing| existing == path) {
            tree.push(path.to_string());
        }
        self
    }

    pub(crate) fn diff(&mut self, id: &str, entries: &[(char, &str)]) -> &mut Self {
        self.diffs.insert(
            id.to_string(),
            entries
                .iter()
                .map(|(status, path)| CommitDiffEntry {
                    status: *status,
                    path: path.to_string(),
                })
                .collect(),
        );
        self
    }

    /// Reachable commits including `id` itself.
    pub(crate) fn ancestors(&self, id: &str) -> Vec<String> {
        let mut seen = Vec::new();
        let mut stack = vec![id.to_string()];
        while let Some(current) = stack.pop() {
            if seen.contains(&current) {
                continue;
            }
            seen.push(current.clone());
            for parent in self.parents.get(&current).into_iter().flatten() {
                stack.push(parent.clone());
            }
        }
        seen
    }

    pub(crate) fn first_parent_lineage(&self, tip: &str) -> Vec<String> {
        let mut lineage = Vec::new();
        let mut current = tip.to_string();
        loop {
            if lineage.contains(&current) {
                break;
            }
            lineage.push(current.clone());
            match self.parents.get(&current).and_then(|p| p.first()) {
                Some(parent) => current = parent.clone(),
                None => break,
            }
        }
        lineage
    }
}

pub(crate) struct FakeEvidence {
    pub(crate) repo: FakeRepo,
    pub(crate) identities: HashMap<String, WorktreeIdentity>,
    pub(crate) worktree_merges: Vec<PathBuf>,
    pub(crate) worktree_conflicts: HashMap<PathBuf, Vec<String>>,
    pub(crate) head: String,
    pub(crate) merge_head: Option<String>,
    pub(crate) conflict_paths: Vec<String>,
    pub(crate) clean: bool,
    pub(crate) index_paths: Vec<String>,
    /// `(revision, path)` pairs whose blob read fails outright.
    pub(crate) unreadable: Vec<(String, String)>,
}

impl FakeEvidence {
    pub(crate) fn new(repo: FakeRepo, head: &str) -> Self {
        Self {
            repo,
            identities: HashMap::new(),
            worktree_merges: Vec::new(),
            worktree_conflicts: HashMap::new(),
            head: head.to_string(),
            merge_head: None,
            conflict_paths: Vec::new(),
            clean: true,
            index_paths: Vec::new(),
            unreadable: Vec::new(),
        }
    }

    /// Make one committed path fail to read, as a damaged object would.
    pub(crate) fn unreadable(mut self, revision: &str, path: &str) -> Self {
        self.unreadable
            .push((revision.to_string(), path.to_string()));
        self
    }

    pub(crate) fn worktree(mut self, branch: &str, path: &str, tip: &str) -> Self {
        self.identities.insert(
            branch.to_string(),
            WorktreeIdentity::Supplied {
                path: PathBuf::from(path),
                tip: tip.to_string(),
            },
        );
        self
    }

    pub(crate) fn unsafe_worktree(mut self, branch: &str, reason: &str) -> Self {
        self.identities.insert(
            branch.to_string(),
            WorktreeIdentity::Unsafe {
                reason: reason.to_string(),
            },
        );
        self
    }

    pub(crate) fn merge_head(mut self, tip: &str) -> Self {
        self.merge_head = Some(tip.to_string());
        self
    }

    pub(crate) fn index(mut self, paths: &[&str]) -> Self {
        self.index_paths = paths.iter().map(|p| p.to_string()).collect();
        self
    }

    pub(crate) fn conflicts(mut self, paths: &[&str]) -> Self {
        self.conflict_paths = paths.iter().map(|p| p.to_string()).collect();
        self
    }

    pub(crate) fn dirty(mut self) -> Self {
        self.clean = false;
        self
    }

    pub(crate) fn worktree_merging(mut self, path: &str) -> Self {
        self.worktree_merges.push(PathBuf::from(path));
        self
    }

    pub(crate) fn worktree_conflicted(mut self, path: &str, files: &[&str]) -> Self {
        self.worktree_conflicts.insert(
            PathBuf::from(path),
            files.iter().map(|f| f.to_string()).collect(),
        );
        self
    }
}

#[async_trait]
impl ResolveEvidence for FakeEvidence {
    async fn validate_worktree(
        &self,
        supplied_path: &Path,
        expected_branch: &str,
    ) -> WorktreeIdentity {
        self.identities
            .get(expected_branch)
            .cloned()
            .unwrap_or(WorktreeIdentity::Unsafe {
                reason: format!(
                    "no worktree metadata for branch '{}' (supplied {})",
                    expected_branch,
                    supplied_path.display()
                ),
            })
    }

    async fn worktree_merge_in_progress(&self, worktree: &Path) -> EvidenceResult<bool> {
        Ok(self.worktree_merges.iter().any(|p| p == worktree))
    }

    async fn worktree_conflicts(&self, worktree: &Path) -> EvidenceResult<Vec<String>> {
        Ok(self
            .worktree_conflicts
            .get(worktree)
            .cloned()
            .unwrap_or_default())
    }

    async fn target_head(&self) -> EvidenceResult<String> {
        Ok(self.head.clone())
    }

    async fn target_merge_head(&self) -> EvidenceResult<Option<String>> {
        Ok(self.merge_head.clone())
    }

    async fn target_conflict_paths(&self) -> EvidenceResult<Vec<String>> {
        Ok(self.conflict_paths.clone())
    }

    async fn target_is_clean(&self) -> EvidenceResult<bool> {
        Ok(self.clean)
    }

    async fn target_index_paths(&self, prefix: &str) -> EvidenceResult<Vec<String>> {
        Ok(self
            .index_paths
            .iter()
            .filter(|path| path.starts_with(prefix))
            .cloned()
            .collect())
    }

    async fn parents_of(&self, commit: &str) -> EvidenceResult<Vec<String>> {
        self.repo
            .parents
            .get(commit)
            .cloned()
            .ok_or_else(|| format!("unknown commit {}", commit))
    }

    async fn is_ancestor(&self, ancestor: &str, descendant: &str) -> EvidenceResult<bool> {
        Ok(self
            .repo
            .ancestors(descendant)
            .iter()
            .any(|c| c == ancestor))
    }

    async fn first_parent_lineage(&self, tip: &str) -> EvidenceResult<Vec<String>> {
        Ok(self.repo.first_parent_lineage(tip))
    }

    async fn merge_base(&self, a: &str, b: &str) -> EvidenceResult<Option<String>> {
        let b_ancestors = self.repo.ancestors(b);
        Ok(self
            .repo
            .ancestors(a)
            .into_iter()
            .find(|c| b_ancestors.contains(c)))
    }

    async fn commits_with_exact_subject(
        &self,
        from: Option<&str>,
        to: &str,
        subject: &str,
    ) -> EvidenceResult<Vec<String>> {
        let excluded = from.map(|f| self.repo.ancestors(f)).unwrap_or_default();
        Ok(self
            .repo
            .ancestors(to)
            .into_iter()
            .filter(|c| !excluded.contains(c))
            .filter(|c| self.repo.subjects.get(c).map(String::as_str) == Some(subject))
            .collect())
    }

    async fn commit_diff_entries(&self, commit: &str) -> EvidenceResult<Vec<CommitDiffEntry>> {
        Ok(self.repo.diffs.get(commit).cloned().unwrap_or_default())
    }

    async fn committed_tree_paths(
        &self,
        revision: &str,
        prefix: &str,
    ) -> EvidenceResult<Vec<String>> {
        Ok(self
            .repo
            .trees
            .get(revision)
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .filter(|path| path.starts_with(prefix))
            .collect())
    }

    async fn committed_file_text(
        &self,
        revision: &str,
        path: &str,
    ) -> EvidenceResult<Option<String>> {
        if self
            .unreadable
            .iter()
            .any(|(rev, file)| rev == revision && file == path)
        {
            return Err(format!("fatal: unable to read {}:{}", revision, path));
        }
        Ok(self
            .repo
            .files
            .get(&(revision.to_string(), path.to_string()))
            .cloned())
    }
}

pub(crate) fn item(revision: &str, change_id: &str, path: &str) -> SequentialMergeItem {
    SequentialMergeItem {
        revision: revision.to_string(),
        change_id: change_id.to_string(),
        archive_path: PathBuf::from(path),
        branch_base: None,
    }
}

pub(crate) fn live(change_id: &str) -> String {
    format!("openspec/changes/{}/proposal.md", change_id)
}

pub(crate) fn archived(change_id: &str) -> String {
    format!(
        "openspec/changes/archive/2026-08-03-{}/proposal.md",
        change_id
    )
}

/// Repository-relative active task list of `change_id`.
pub(crate) fn live_tasks(change_id: &str) -> String {
    format!("openspec/changes/{}/tasks.md", change_id)
}

/// Repository-relative archived task list of `change_id`.
pub(crate) fn archived_tasks(change_id: &str) -> String {
    format!("openspec/changes/archive/2026-08-03-{}/tasks.md", change_id)
}

/// Repository-relative active JSON task artifact of `change_id`.
pub(crate) fn live_tasks_json(change_id: &str) -> String {
    format!("openspec/changes/{}/tasks.json", change_id)
}

/// Repository-relative archived JSON task artifact of `change_id`.
pub(crate) fn archived_tasks_json(change_id: &str) -> String {
    format!(
        "openspec/changes/archive/2026-08-03-{}/tasks.json",
        change_id
    )
}

/// A JSON task document recording `completed` of `total` tasks done.
pub(crate) fn tasks_json(completed: u32, total: u32) -> String {
    let tasks = (0..total)
        .map(|index| {
            let status = if index < completed {
                "completed"
            } else {
                "pending"
            };
            format!(
                r#"{{"id":"task-{index}","title":"Task {index}","status":"{status}","section":"implementation"}}"#
            )
        })
        .collect::<Vec<_>>()
        .join(",");
    format!(r#"{{"schema_version":1,"tasks":[{tasks}]}}"#)
}

/// A task list recording `completed` of `total` tasks done.
pub(crate) fn tasks_markdown(completed: u32, total: u32) -> String {
    let mut content = String::from("## Implementation Tasks\n\n");
    for index in 0..total {
        let box_ = if index < completed { "x" } else { " " };
        content.push_str(&format!("- [{}] Task {}\n", box_, index + 1));
    }
    content
}

/// Add a complete archived task list for `change_id` at `revision`.
pub(crate) fn with_complete_tasks(repo: &mut FakeRepo, revision: &str, change_id: &str) {
    repo.file(revision, &archived_tasks(change_id), &tasks_markdown(7, 7));
}

/// `base -> t` on the target, with branch `ws-a` pre-synced onto `t`.
///
/// `a_tip` is the branch tip; `merge` (when integrated) is the final merge.
/// The branch tip carries the archived change, which is what a resolve batch
/// item always looks like: the change is archived on its own branch before the
/// final merge integrates it — with its archived task list complete, which is
/// what makes it mergeable at all.
pub(crate) fn presynced_repo() -> FakeRepo {
    let mut repo = FakeRepo::default();
    repo.commit("base", "Base", &[])
        .commit("t", "Target advance", &["base"])
        .commit("a1", "Work on change-a", &["base"])
        .commit("a_tip", "Pre-sync base into change-a", &["a1", "t"])
        .tree("a_tip", &[&archived("change-a")])
        .tree("t", &[])
        .tree("base", &[]);
    with_complete_tasks(&mut repo, "a_tip", "change-a");
    repo
}