ccd-cli 1.0.0-alpha.8

Bootstrap and validate Continuous Context Development repositories
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
use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};

use crate::paths::state::StateLayout;

#[derive(Debug)]
pub(crate) enum MigrationReport {
    AlreadyCanonical,
    Migrated { from: Vec<PathBuf> },
    NothingToMigrate,
}

pub(crate) fn migrate_repo_overlay_if_needed(
    layout: &StateLayout,
    locality_id: &str,
) -> Result<MigrationReport> {
    let config_path = layout.repo_overlay_config_path(locality_id)?;

    // If config.toml already exists with content, treat as canonical.
    if config_path.exists() {
        let contents = fs::read_to_string(&config_path)
            .with_context(|| format!("failed to read {}", config_path.display()))?;
        if !contents.trim().is_empty() {
            return Ok(MigrationReport::AlreadyCanonical);
        }
    }

    let overlay_root = layout.repo_overlay_root(locality_id)?;

    // Scan legacy files and attempt migration for each.
    let manifest_path = overlay_root.join("manifest.md");
    let validation_path = overlay_root.join("validation.toml");
    let backlog_path = overlay_root.join("backlog.toml");

    let has_any_legacy =
        manifest_path.exists() || validation_path.exists() || backlog_path.exists();

    if !has_any_legacy {
        return Ok(MigrationReport::NothingToMigrate);
    }

    // Build config.toml content from legacy files. Track which files
    // were successfully migrated so we only rename those.
    let mut sections: Vec<String> = Vec::new();
    let mut migrated_paths: Vec<PathBuf> = Vec::new();

    if manifest_path.exists() {
        if let Some(section) = migrate_manifest(&manifest_path)? {
            sections.push(section);
            migrated_paths.push(manifest_path);
        }
    }

    if validation_path.exists() {
        if let Some(section) = migrate_validation(&validation_path)? {
            sections.push(section);
            migrated_paths.push(validation_path);
        }
    }

    let backlog_path = overlay_root.join("backlog.toml");
    if backlog_path.exists() {
        if let Some(section) = migrate_backlog(&backlog_path)? {
            sections.push(section);
            migrated_paths.push(backlog_path);
        }
    }

    if sections.is_empty() {
        return Ok(MigrationReport::NothingToMigrate);
    }

    // Write merged config.toml.
    let config_content = sections.join("\n");
    fs::write(&config_path, &config_content)
        .with_context(|| format!("failed to write {}", config_path.display()))?;

    // Rename successfully migrated legacy files to *.migrated.
    let mut migrated_from = Vec::new();
    for path in &migrated_paths {
        let migrated = path.with_extension(format!(
            "{}.migrated",
            path.extension()
                .map(|e| e.to_str().unwrap_or(""))
                .unwrap_or("")
        ));
        fs::rename(path, &migrated).with_context(|| {
            format!(
                "failed to rename {} to {}",
                path.display(),
                migrated.display()
            )
        })?;
        migrated_from.push(path.clone());
    }

    Ok(MigrationReport::Migrated {
        from: migrated_from,
    })
}

fn migrate_manifest(path: &Path) -> Result<Option<String>> {
    let contents =
        fs::read_to_string(path).with_context(|| format!("failed to read {}", path.display()))?;

    // Use the canonical manifest parser so numbered entries, backtick-
    // wrapped entries, and all other accepted formats are normalized
    // identically to the runtime loader.
    let entries = crate::repo::truth::parse_manifest_entries(&contents)
        .with_context(|| format!("failed to parse manifest {}", path.display()))?;

    if entries.is_empty() {
        return Ok(None);
    }

    let values: Vec<toml::Value> = entries
        .iter()
        .map(|e| toml::Value::String(e.display().to_string()))
        .collect();

    let mut table = toml::map::Map::new();
    table.insert("always".to_owned(), toml::Value::Array(values));
    let mut root = toml::map::Map::new();
    root.insert("sources".to_owned(), toml::Value::Table(table));

    Ok(Some(toml::to_string_pretty(&root)?))
}

fn migrate_validation(path: &Path) -> Result<Option<String>> {
    let contents =
        fs::read_to_string(path).with_context(|| format!("failed to read {}", path.display()))?;

    if contents.trim().is_empty() {
        return Ok(None);
    }

    // Parse the old validation.toml which supports both flat keys
    // ([doctor] handoff_freshness = "error") and nested keys
    // ([doctor.handoff] freshness = "error"). We normalize to the new
    // config.toml [doctor.handoff] format.
    let parsed: BTreeMap<String, toml::Value> =
        toml::from_str(&contents).with_context(|| format!("failed to parse {}", path.display()))?;

    let doctor = match parsed.get("doctor") {
        Some(toml::Value::Table(t)) => t,
        _ => return Ok(None),
    };

    fn is_valid_severity(val: &str) -> bool {
        matches!(val, "warning" | "error")
    }

    let mut handoff_table = toml::map::Map::new();

    // Flat keys take lower priority than nested [doctor.handoff] keys.
    let flat_keys = [
        ("handoff_freshness", "freshness"),
        ("handoff_missing", "missing"),
        ("handoff_sections", "sections"),
        ("handoff_sections_empty", "sections_empty"),
        ("handoff_narration", "narration"),
    ];
    let mut has_invalid = false;
    for (old_key, new_key) in flat_keys {
        if let Some(val) = doctor.get(old_key).and_then(|v| v.as_str()) {
            if is_valid_severity(val) {
                handoff_table.insert(new_key.to_owned(), toml::Value::String(val.to_owned()));
            } else {
                has_invalid = true;
            }
        }
    }

    // Nested [doctor.handoff] keys override flat keys. Only accept known
    // field names so that typos (e.g. `freshnes`) are not silently migrated
    // — the legacy file stays in place and the strict legacy loader reports
    // the error via deny_unknown_fields.
    const KNOWN_HANDOFF_KEYS: &[&str] = &[
        "missing",
        "sections",
        "sections_empty",
        "narration",
        "freshness",
    ];

    if let Some(toml::Value::Table(handoff)) = doctor.get("handoff") {
        for (key, value) in handoff {
            if !KNOWN_HANDOFF_KEYS.contains(&key.as_str()) {
                has_invalid = true;
                continue;
            }
            if let Some(s) = value.as_str() {
                if is_valid_severity(s) {
                    handoff_table.insert(key.clone(), toml::Value::String(s.to_owned()));
                } else {
                    has_invalid = true;
                }
            }
        }
    }

    // If any values are invalid, skip migration for this file so the legacy
    // loader can report the parse error.
    if has_invalid {
        return Ok(None);
    }

    if handoff_table.is_empty() {
        return Ok(None);
    }

    let mut doctor_table = toml::map::Map::new();
    doctor_table.insert("handoff".to_owned(), toml::Value::Table(handoff_table));
    let mut root = toml::map::Map::new();
    root.insert("doctor".to_owned(), toml::Value::Table(doctor_table));

    Ok(Some(toml::to_string_pretty(&root)?))
}

fn migrate_backlog(path: &Path) -> Result<Option<String>> {
    let contents =
        fs::read_to_string(path).with_context(|| format!("failed to read {}", path.display()))?;

    if contents.trim().is_empty() {
        return Ok(None);
    }

    let parsed: BTreeMap<String, toml::Value> =
        toml::from_str(&contents).with_context(|| format!("failed to parse {}", path.display()))?;

    let backlog = match parsed.get("backlog") {
        Some(toml::Value::Table(t)) => t,
        _ => return Ok(None),
    };

    let adapter = backlog.get("adapter").and_then(|v| v.as_str());

    let mut ext_table = toml::map::Map::new();
    ext_table.insert("type".to_owned(), toml::Value::String("backlog".to_owned()));
    if let Some(name) = adapter {
        ext_table.insert("name".to_owned(), toml::Value::String(name.to_owned()));
    }

    if let Some(toml::Value::Table(inputs)) = backlog.get("inputs") {
        for (key, value) in inputs {
            ext_table.insert(key.clone(), value.clone());
        }
    }

    let mut root = toml::map::Map::new();
    root.insert(
        "extensions".to_owned(),
        toml::Value::Array(vec![toml::Value::Table(ext_table)]),
    );

    Ok(Some(toml::to_string_pretty(&root)?))
}

#[cfg(test)]
mod tests {
    use std::fs;

    use tempfile::tempdir;

    use super::*;
    use crate::profile::ProfileName;

    fn test_layout(temp: &tempfile::TempDir) -> (StateLayout, String) {
        let layout = StateLayout::new(
            temp.path().join(".ccd"),
            temp.path().join("repo/.git/ccd"),
            ProfileName::new("main").expect("profile"),
        );
        let locality_id = "ccdrepo_mig".to_owned();
        fs::create_dir_all(
            layout
                .repo_overlay_root(&locality_id)
                .expect("repo overlay"),
        )
        .expect("repo overlay");
        (layout, locality_id)
    }

    #[test]
    fn migration_creates_config_toml_from_legacy_files() {
        let temp = tempdir().expect("tempdir");
        let (layout, locality_id) = test_layout(&temp);
        let overlay = layout.repo_overlay_root(&locality_id).expect("overlay");

        fs::write(overlay.join("manifest.md"), "- AGENTS.md\n- README.md\n").expect("manifest");
        fs::write(
            overlay.join("validation.toml"),
            "[doctor.handoff]\nfreshness = \"error\"\n",
        )
        .expect("validation");
        fs::write(
            overlay.join("backlog.toml"),
            "[backlog]\nadapter = \"github\"\n\n[backlog.inputs]\nrepo = \"dusk-network/ccd\"\n",
        )
        .expect("backlog");

        let report = migrate_repo_overlay_if_needed(&layout, &locality_id).expect("migrate");

        match &report {
            MigrationReport::Migrated { from } => assert_eq!(from.len(), 3),
            other => panic!("expected Migrated, got {:?}", other),
        }

        // config.toml should exist and contain all sections
        let config = fs::read_to_string(overlay.join("config.toml")).expect("read config.toml");
        assert!(config.contains("[sources]"));
        assert!(config.contains("AGENTS.md"));
        assert!(config.contains("[doctor.handoff]"));
        assert!(config.contains("freshness"));
        assert!(config.contains("[[extensions]]"));
        assert!(config.contains("github"));

        // Legacy files should be renamed
        assert!(!overlay.join("manifest.md").exists());
        assert!(overlay.join("manifest.md.migrated").exists());
        assert!(!overlay.join("validation.toml").exists());
        assert!(overlay.join("validation.toml.migrated").exists());
        assert!(!overlay.join("backlog.toml").exists());
        assert!(overlay.join("backlog.toml.migrated").exists());
    }

    #[test]
    fn migration_is_idempotent_when_config_exists() {
        let temp = tempdir().expect("tempdir");
        let (layout, locality_id) = test_layout(&temp);
        let overlay = layout.repo_overlay_root(&locality_id).expect("overlay");

        fs::write(
            overlay.join("config.toml"),
            "[sources]\nalways = [\"AGENTS.md\"]\n",
        )
        .expect("config");

        let report = migrate_repo_overlay_if_needed(&layout, &locality_id).expect("migrate");
        assert!(matches!(report, MigrationReport::AlreadyCanonical));
    }

    #[test]
    fn migration_returns_nothing_when_no_legacy_files() {
        let temp = tempdir().expect("tempdir");
        let (layout, locality_id) = test_layout(&temp);

        let report = migrate_repo_overlay_if_needed(&layout, &locality_id).expect("migrate");
        assert!(matches!(report, MigrationReport::NothingToMigrate));
    }

    #[test]
    fn migration_handles_partial_legacy_files() {
        let temp = tempdir().expect("tempdir");
        let (layout, locality_id) = test_layout(&temp);
        let overlay = layout.repo_overlay_root(&locality_id).expect("overlay");

        // Only manifest.md exists
        fs::write(overlay.join("manifest.md"), "- AGENTS.md\n").expect("manifest");

        let report = migrate_repo_overlay_if_needed(&layout, &locality_id).expect("migrate");

        match &report {
            MigrationReport::Migrated { from } => assert_eq!(from.len(), 1),
            other => panic!("expected Migrated, got {:?}", other),
        }

        let config = fs::read_to_string(overlay.join("config.toml")).expect("read config.toml");
        assert!(config.contains("[sources]"));
    }

    #[test]
    fn migration_backlog_omits_name_when_adapter_is_absent() {
        let temp = tempdir().expect("tempdir");
        let (layout, locality_id) = test_layout(&temp);
        let overlay = layout.repo_overlay_root(&locality_id).expect("overlay");

        // backlog.toml with inputs but no explicit adapter
        fs::write(
            overlay.join("backlog.toml"),
            "[backlog]\n\n[backlog.inputs]\nrepo = \"dusk-network/ccd\"\n",
        )
        .expect("backlog");

        let report = migrate_repo_overlay_if_needed(&layout, &locality_id).expect("migrate");
        assert!(matches!(report, MigrationReport::Migrated { .. }));

        let config = fs::read_to_string(overlay.join("config.toml")).expect("read config.toml");
        assert!(config.contains("[[extensions]]"));
        assert!(config.contains("dusk-network/ccd"));
        // Should NOT contain name = "default" — omitting name preserves
        // profile-default resolution semantics.
        assert!(
            !config.contains("name ="),
            "migrated config should not contain name when adapter was absent, got:\n{config}"
        );
    }

    #[test]
    fn migration_skips_validation_with_unknown_handoff_keys() {
        let temp = tempdir().expect("tempdir");
        let (layout, locality_id) = test_layout(&temp);
        let overlay = layout.repo_overlay_root(&locality_id).expect("overlay");

        // validation.toml with a typo in the nested key
        fs::write(
            overlay.join("validation.toml"),
            "[doctor.handoff]\nfreshnes = \"error\"\n",
        )
        .expect("validation");

        let report = migrate_repo_overlay_if_needed(&layout, &locality_id).expect("migrate");

        // Migration should skip this file so the legacy deny_unknown_fields
        // loader reports the error.
        assert!(
            matches!(report, MigrationReport::NothingToMigrate),
            "expected NothingToMigrate for typo'd validation.toml, got: {:?}",
            report
        );
        // Original file should still be in place
        assert!(overlay.join("validation.toml").exists());
        assert!(!overlay.join("config.toml").exists());
    }
}