lemma 0.9.4

A pure, declarative language for business rules.
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//! Shared registry install for CLI `install` and MCP `install`.
//!
//! Install = fetch + persist.

use crate::deps::{lemma_deps_dir, relative_dependency_cache_path};
use crate::workspace::{self, WorkspaceDiskError};
use lemma::Engine;
use std::collections::HashSet;
use std::fs;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::sync::{Arc, OnceLock};
use walkdir::WalkDir;

fn registry_runtime() -> &'static tokio::runtime::Runtime {
    static RUNTIME: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
    RUNTIME.get_or_init(|| {
        tokio::runtime::Runtime::new().expect("BUG: failed to create registry install runtime")
    })
}

/// Result of a successful single-id registry install.
#[derive(Debug)]
pub enum InstallOutcome {
    Written {
        relative_path: PathBuf,
        source: String,
    },
    AlreadyUpToDate {
        relative_path: PathBuf,
        source: String,
    },
}

/// Failure installing a registry dependency.
pub enum InstallError {
    Registry(lemma::RegistryError),
    UnparseableRegistry(lemma::Error),
    Conflict {
        spec_names: Vec<String>,
        path: PathBuf,
    },
    Io(io::Error),
    Workspace(WorkspaceDiskError),
    Plan(lemma::Errors),
}

impl std::fmt::Debug for InstallError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{self}")
    }
}

impl std::fmt::Display for InstallError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Registry(error) => write!(f, "Registry error: {}", error.message),
            Self::UnparseableRegistry(error) => {
                write!(f, "Registry returned unparseable dependency: {error}")
            }
            Self::Conflict { spec_names, path } => write!(
                f,
                "Dependency containing spec(s) {} already exists in {}.\n\
                 Content has changed on the registry. Re-run with --force to overwrite.",
                spec_names.join(", "),
                path.display()
            ),
            Self::Io(error) => write!(f, "{error}"),
            Self::Workspace(error) => write!(f, "{error}"),
            Self::Plan(errors) => write!(
                f,
                "Planning installed dependency failed ({} error(s))",
                errors.errors.len()
            ),
        }
    }
}

impl From<io::Error> for InstallError {
    fn from(error: io::Error) -> Self {
        Self::Io(error)
    }
}

impl From<WorkspaceDiskError> for InstallError {
    fn from(error: WorkspaceDiskError) -> Self {
        match error {
            WorkspaceDiskError::EngineLoad(errors) => Self::Plan(errors),
            other => Self::Workspace(other),
        }
    }
}

/// Drive an async registry future on the process-wide install runtime.
pub fn block_on_registry<F: std::future::Future>(future: F) -> F::Output {
    registry_runtime().block_on(future)
}

/// Fetch, conflict-scan, plan-probe, and persist one registry dependency.
///
/// Install = [`fetch_dependency`] + [`persist_registry_dependency`].
/// Canonical destination bytes match → already up to date; any spec-name conflict
/// → error unless `force` (then conflict files are removed after a successful plan probe).
pub fn install_registry_dependency(
    workdir: &Path,
    id: &str,
    force: bool,
    registry: &dyn lemma::Registry,
) -> Result<InstallOutcome, InstallError> {
    let bundle = block_on_registry(fetch_dependency(registry, id))?;
    persist_registry_dependency(workdir, id, &bundle.source, force)
}

/// Fetch a registry dependency body. Registry owns id reachability.
pub async fn fetch_dependency(
    registry: &dyn lemma::Registry,
    dependency: &str,
) -> Result<lemma::RegistryBundle, InstallError> {
    let bundle = registry
        .get(dependency)
        .await
        .map_err(InstallError::Registry)?;

    let limits = lemma::ResourceLimits::default();
    lemma::parse(
        &bundle.source,
        lemma::SourceType::Dependency(dependency.to_string()),
        &limits,
    )
    .map_err(InstallError::UnparseableRegistry)?;

    Ok(bundle)
}

fn spec_names_from_source(dependency: &str, source: &str) -> Result<HashSet<String>, InstallError> {
    let limits = lemma::ResourceLimits::default();
    let specs = lemma::parse(
        source,
        lemma::SourceType::Dependency(dependency.to_string()),
        &limits,
    )
    .map_err(InstallError::UnparseableRegistry)?
    .into_flattened_specs();
    Ok(specs.into_iter().map(|spec| spec.name).collect())
}

/// Conflict-scan, plan-probe, and persist one registry dependency source.
///
/// Used by single-id install after fetch, and by `install --all` after
/// `resolve_registry_references` (no second fetch).
///
/// Order: write tmp → plan probe → on fail remove tmp; on ok remove
/// `conflict_paths` (when force) then rename tmp → `destination_absolute`.
pub fn persist_registry_dependency(
    workdir: &Path,
    id: &str,
    source: &str,
    force: bool,
) -> Result<InstallOutcome, InstallError> {
    let spec_names = spec_names_from_source(id, source)?;
    let scan = scan_lemma_deps(workdir, id, &spec_names)?;

    if scan.destination_absolute.is_file() {
        let existing = fs::read_to_string(&scan.destination_absolute)?;
        if existing == source {
            return Ok(InstallOutcome::AlreadyUpToDate {
                relative_path: scan.destination_relative,
                source: source.to_string(),
            });
        }
    }

    if !scan.conflict_paths.is_empty() && !force {
        let path = scan.conflict_paths[0].clone();
        let existing_content = fs::read_to_string(&path)?;
        let limits = lemma::ResourceLimits::default();
        let existing_specs = lemma::parse(
            &existing_content,
            lemma::SourceType::Path(Arc::new(path.clone())),
            &limits,
        )
        .map_err(|error| {
            InstallError::Io(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("unparseable lemma_deps file {}: {error}", path.display()),
            ))
        })?
        .into_flattened_specs();
        let conflict: Vec<String> = existing_specs
            .iter()
            .filter(|spec| spec_names.contains(&spec.name))
            .map(|spec| spec.name.clone())
            .collect();
        return Err(InstallError::Conflict {
            spec_names: conflict,
            path,
        });
    }

    let tmp = write_tmp(&scan.destination_absolute, source)?;

    let mut exclude: Vec<&Path> = vec![scan.destination_absolute.as_path()];
    if force {
        for path in &scan.conflict_paths {
            exclude.push(path.as_path());
        }
    }

    let probe_result = (|| -> Result<(), InstallError> {
        let mut probe = Engine::new();
        workspace::load_workspace_excluding(&mut probe, workdir, &exclude)?;
        probe
            .load([(
                lemma::SourceType::Dependency(id.to_string()),
                source.to_string(),
            )])
            .map_err(InstallError::Plan)?;
        Ok(())
    })();

    if let Err(error) = probe_result {
        let _ = fs::remove_file(&tmp);
        return Err(error);
    }

    if force {
        for path in &scan.conflict_paths {
            if path != &scan.destination_absolute {
                fs::remove_file(path)?;
            }
        }
    }

    rename_tmp(&tmp, &scan.destination_absolute)?;

    Ok(InstallOutcome::Written {
        relative_path: scan.destination_relative,
        source: source.to_string(),
    })
}

/// Result of walking `lemma_deps/` against a candidate dependency source.
pub struct LemmaDepsScan {
    pub destination_absolute: PathBuf,
    pub destination_relative: PathBuf,
    /// Paths that define overlapping spec names with the candidate.
    pub conflict_paths: Vec<PathBuf>,
}

/// Walk `lemma_deps/` for spec-name conflicts.
/// Unparseable or unreadable `.lemma` files are errors.
pub fn scan_lemma_deps(
    workdir: &Path,
    dependency: &str,
    new_spec_names: &HashSet<String>,
) -> Result<LemmaDepsScan, InstallError> {
    let deps_dir = lemma_deps_dir(workdir);
    let destination_relative = relative_dependency_cache_path(dependency);
    let destination_absolute = deps_dir.join(&destination_relative);
    let limits = lemma::ResourceLimits::default();

    let mut conflict_paths = Vec::new();

    if deps_dir.exists() {
        for entry in WalkDir::new(&deps_dir) {
            let entry = entry.map_err(|error| InstallError::Io(io::Error::other(error)))?;
            if entry.path().extension().and_then(|s| s.to_str()) != Some("lemma") {
                continue;
            }
            let path = entry.path();
            let existing_content = fs::read_to_string(path)?;
            let existing_specs = lemma::parse(
                &existing_content,
                lemma::SourceType::Path(Arc::new(path.to_path_buf())),
                &limits,
            )
            .map_err(|error| {
                InstallError::Io(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("unparseable lemma_deps file {}: {error}", path.display()),
                ))
            })?
            .into_flattened_specs();
            let has_conflict = existing_specs
                .iter()
                .any(|spec| new_spec_names.contains(&spec.name));
            if has_conflict {
                conflict_paths.push(path.to_path_buf());
            }
        }
    }

    Ok(LemmaDepsScan {
        destination_absolute,
        destination_relative,
        conflict_paths,
    })
}

/// Temp path beside `path`: `.{file_name}.tmp` in the same directory.
fn tmp_path_for(path: &Path) -> io::Result<PathBuf> {
    let file_name = path.file_name().ok_or_else(|| {
        io::Error::new(
            io::ErrorKind::InvalidInput,
            "persist path must include a file name",
        )
    })?;
    let dir = match path.parent() {
        Some(p) if !p.as_os_str().is_empty() => p,
        _ => Path::new("."),
    };
    fs::create_dir_all(dir)?;
    Ok(dir.join(format!(".{}.tmp", file_name.to_string_lossy())))
}

/// Write `contents` to `.{file}.tmp` beside `path`, fsync. Does not rename.
pub fn write_tmp(path: &Path, contents: &str) -> io::Result<PathBuf> {
    let tmp = tmp_path_for(path)?;
    match (|| -> io::Result<()> {
        let mut f = fs::File::create(&tmp)?;
        f.write_all(contents.as_bytes())?;
        f.sync_all()?;
        Ok(())
    })() {
        Ok(()) => Ok(tmp),
        Err(e) => {
            let _ = fs::remove_file(&tmp);
            Err(e)
        }
    }
}

/// Rename a temp file produced by [`write_tmp`] onto `path`.
pub fn rename_tmp(tmp: &Path, path: &Path) -> io::Result<()> {
    fs::rename(tmp, path)
}

/// Atomic write: [`write_tmp`] then [`rename_tmp`].
pub fn atomic_write(path: &Path, contents: &str) -> io::Result<()> {
    let tmp = write_tmp(path, contents)?;
    match rename_tmp(&tmp, path) {
        Ok(()) => Ok(()),
        Err(e) => {
            let _ = fs::remove_file(&tmp);
            Err(e)
        }
    }
}

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

    #[test]
    fn write_tmp_then_rename_tmp_round_trip() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("out.lemma");
        let tmp = write_tmp(&path, "spec x\ndata a: 1\n").unwrap();
        assert!(tmp.exists(), "tmp must exist before rename");
        assert!(!path.exists(), "final path must not exist before rename");
        rename_tmp(&tmp, &path).unwrap();
        assert_eq!(fs::read_to_string(&path).unwrap(), "spec x\ndata a: 1\n");
        assert!(!tmp.exists(), "tmp must be gone after rename");
    }

    #[test]
    fn atomic_write_round_trip() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("out.lemma");
        atomic_write(&path, "spec x\ndata a: 1\n").unwrap();
        assert_eq!(fs::read_to_string(&path).unwrap(), "spec x\ndata a: 1\n");
        assert!(
            !dir.path().join(".out.lemma.tmp").exists(),
            "temp file must be cleaned up"
        );
    }

    #[test]
    fn scan_lemma_deps_rejects_unparseable_cache_file() {
        let dir = tempfile::tempdir().unwrap();
        let junk = dir.path().join("lemma_deps").join("@junk");
        fs::create_dir_all(&junk).unwrap();
        fs::write(junk.join("corrupt.lemma"), "{{{not valid lemma").unwrap();

        let mut names = HashSet::new();
        names.insert("alpha2".to_string());

        let result = scan_lemma_deps(dir.path(), "@iso/countries", &names);
        assert!(
            result.is_err(),
            "unparseable lemma_deps .lemma must hard-error"
        );
    }

    #[test]
    fn install_writes_fixture_dependency() {
        let dir = tempfile::tempdir().unwrap();
        let registry = lemma::LemmaBase::test();
        let outcome =
            install_registry_dependency(dir.path(), "@iso/countries", false, &registry).unwrap();
        match outcome {
            InstallOutcome::Written { relative_path, .. } => {
                assert_eq!(relative_path, PathBuf::from("@iso").join("countries.lemma"));
            }
            InstallOutcome::AlreadyUpToDate { .. } => panic!("expected Written"),
        }
        assert!(dir
            .path()
            .join("lemma_deps")
            .join("@iso")
            .join("countries.lemma")
            .exists());
        assert!(
            !dir.path()
                .join("lemma_deps")
                .join("@iso")
                .join(".countries.lemma.tmp")
                .exists(),
            "tmp must not remain after success"
        );
    }

    #[test]
    fn install_destination_match_is_up_to_date() {
        let dir = tempfile::tempdir().unwrap();
        let registry = lemma::LemmaBase::test();
        install_registry_dependency(dir.path(), "@iso/countries", false, &registry).unwrap();
        let second =
            install_registry_dependency(dir.path(), "@iso/countries", false, &registry).unwrap();
        assert!(
            matches!(second, InstallOutcome::AlreadyUpToDate { .. }),
            "canonical destination match must be up to date"
        );
    }

    #[test]
    fn install_identical_content_elsewhere_conflicts_without_force() {
        let dir = tempfile::tempdir().unwrap();
        let registry = lemma::LemmaBase::test();
        install_registry_dependency(dir.path(), "@iso/countries", false, &registry).unwrap();
        let other = dir
            .path()
            .join("lemma_deps")
            .join("@other")
            .join("copy.lemma");
        fs::create_dir_all(other.parent().unwrap()).unwrap();
        let dest = dir
            .path()
            .join("lemma_deps")
            .join("@iso")
            .join("countries.lemma");
        fs::rename(&dest, &other).unwrap();

        let err = install_registry_dependency(dir.path(), "@iso/countries", false, &registry)
            .expect_err("foreign copy with overlapping specs must conflict");
        assert!(
            matches!(err, InstallError::Conflict { .. }),
            "expected Conflict, got: {err}"
        );
        assert!(!dest.exists(), "conflict must not write destination");
        assert!(other.exists(), "foreign copy must remain without force");
    }

    #[test]
    fn install_identical_content_elsewhere_force_writes_canonical() {
        let dir = tempfile::tempdir().unwrap();
        let registry = lemma::LemmaBase::test();
        let first =
            install_registry_dependency(dir.path(), "@iso/countries", false, &registry).unwrap();
        let source = match first {
            InstallOutcome::Written { source, .. } => source,
            InstallOutcome::AlreadyUpToDate { source, .. } => source,
        };
        let other = dir
            .path()
            .join("lemma_deps")
            .join("@other")
            .join("copy.lemma");
        fs::create_dir_all(other.parent().unwrap()).unwrap();
        let dest = dir
            .path()
            .join("lemma_deps")
            .join("@iso")
            .join("countries.lemma");
        fs::rename(&dest, &other).unwrap();

        let second =
            install_registry_dependency(dir.path(), "@iso/countries", true, &registry).unwrap();
        match second {
            InstallOutcome::Written { relative_path, .. } => {
                assert_eq!(relative_path, PathBuf::from("@iso").join("countries.lemma"));
            }
            InstallOutcome::AlreadyUpToDate { .. } => {
                panic!("force install must write canonical destination")
            }
        }
        assert!(dest.exists(), "canonical destination must be written");
        assert_eq!(fs::read_to_string(&dest).unwrap(), source);
        assert!(
            !other.exists(),
            "force must remove conflicting foreign copy"
        );
        assert!(
            !dest
                .parent()
                .expect("parent")
                .join(".countries.lemma.tmp")
                .exists(),
            "tmp must not remain after force success"
        );
    }

    #[test]
    fn install_workspace_spec_name_collision_is_plan_error() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(
            dir.path().join("local.lemma"),
            "repo @iso/countries\nspec alpha2\ndata code: text\n",
        )
        .unwrap();
        let registry = lemma::LemmaBase::test();
        let err = install_registry_dependency(dir.path(), "@iso/countries", false, &registry)
            .expect_err("workspace name collision must fail plan probe");
        assert!(
            matches!(err, InstallError::Plan(_)),
            "expected Plan, got: {err}"
        );
        assert!(
            !dir.path()
                .join("lemma_deps")
                .join("@iso")
                .join("countries.lemma")
                .exists(),
            "plan failure must not write destination"
        );
        assert!(
            !dir.path()
                .join("lemma_deps")
                .join("@iso")
                .join(".countries.lemma.tmp")
                .exists(),
            "plan failure must not leave tmp"
        );
    }

    #[test]
    fn install_force_plan_failure_leaves_conflict_paths_and_destination() {
        let dir = tempfile::tempdir().unwrap();
        let registry = lemma::LemmaBase::test();
        let first =
            install_registry_dependency(dir.path(), "@iso/countries", false, &registry).unwrap();
        let source = match first {
            InstallOutcome::Written { source, .. } => source,
            InstallOutcome::AlreadyUpToDate { source, .. } => source,
        };
        let other = dir
            .path()
            .join("lemma_deps")
            .join("@other")
            .join("copy.lemma");
        fs::create_dir_all(other.parent().unwrap()).unwrap();
        let dest = dir
            .path()
            .join("lemma_deps")
            .join("@iso")
            .join("countries.lemma");
        fs::rename(&dest, &other).unwrap();

        fs::write(
            dir.path().join("local.lemma"),
            "repo @iso/countries\nspec alpha2\ndata code: text\n",
        )
        .unwrap();

        let err = install_registry_dependency(dir.path(), "@iso/countries", true, &registry)
            .expect_err("workspace collision must fail even with force");
        assert!(
            matches!(err, InstallError::Plan(_)),
            "expected Plan, got: {err}"
        );
        assert!(
            other.exists(),
            "force plan failure must leave foreign conflict file"
        );
        assert_eq!(fs::read_to_string(&other).unwrap(), source);
        assert!(!dest.exists(), "plan failure must not create destination");
        assert!(
            !dest
                .parent()
                .expect("parent")
                .join(".countries.lemma.tmp")
                .exists(),
            "plan failure must not leave tmp"
        );
    }

    #[test]
    fn install_non_at_id_reaches_registry() {
        let dir = tempfile::tempdir().unwrap();
        let registry = lemma::LemmaBase::test();
        let err = install_registry_dependency(dir.path(), "not-a-registry-id", false, &registry)
            .expect_err("missing id must fail via registry");
        assert!(
            matches!(err, InstallError::Registry(_)),
            "error must be Registry, got: {err}"
        );
    }
}