dalo 0.4.0

Git-backed skill management for AI agents.
Documentation
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
//! Source definitions and source operations.

use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use crate::config::UserConfig;
use crate::error::{DaloError, DaloResult};
use crate::git;
use crate::store::{self, StorePaths};

/// Source kind supported by the V1 config schema.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SourceKind {
    /// Private local source in the dalo store.
    Local,
    /// Git-backed team source.
    Team,
    /// Git-backed catalog source: a multi-skill repository whose skills are
    /// individually selected rather than taken wholesale.
    Catalog,
}

impl SourceKind {
    /// Lowercase label matching the serialized form.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Local => "local",
            Self::Team => "team",
            Self::Catalog => "catalog",
        }
    }
}

impl std::fmt::Display for SourceKind {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(self.as_str())
    }
}

/// Source entry in the user configuration.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SourceConfig {
    /// Stable source ID.
    pub id: String,
    /// Source kind.
    pub kind: SourceKind,
    /// Local checkout path.
    pub path: PathBuf,
    /// Source priority. Lower numbers win.
    pub priority: i32,
    /// Whether the source participates in resolution.
    pub enabled: bool,
    /// Whether this source is configured as trusted.
    pub trusted: bool,
    /// Optional Git URL for team sources.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    /// Optional branch for team sources.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branch: Option<String>,
    /// Update policy, such as `track`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub update_policy: Option<String>,
    /// Selected skill references for a catalog source. Each entry is a stable
    /// frontmatter ID, a `<source-id>:<slot>` ref, or a slot name. Always empty
    /// for non-catalog sources.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub selection: Vec<String>,
}

/// Source add report.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SourceAddReport {
    /// Added source.
    pub source: SourceConfig,
    /// Whether the command ran as dry-run.
    pub dry_run: bool,
}

/// Source list report.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SourceListReport {
    /// Configured sources.
    pub sources: Vec<SourceConfig>,
}

/// Source priority report.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SourcePriorityReport {
    /// Updated source.
    pub source: SourceConfig,
    /// Whether the command ran as dry-run.
    pub dry_run: bool,
}

/// Sort source configs by precedence and then stable ID.
pub fn sort_sources(sources: &mut [SourceConfig]) {
    sources.sort_by(|left, right| {
        left.priority
            .cmp(&right.priority)
            .then_with(|| left.id.cmp(&right.id))
    });
}

/// Add a team source and clone it into the store.
///
/// When `dry_run` is set, the planned source is computed and returned without
/// cloning, writing config, or recording an approval.
pub fn add_team_source(
    paths: &StorePaths,
    id: &str,
    url: &str,
    dry_run: bool,
) -> DaloResult<SourceAddReport> {
    add_team_source_with_config_writer(paths, id, url, dry_run, store::write_config)
}

fn add_team_source_with_config_writer<F>(
    paths: &StorePaths,
    id: &str,
    url: &str,
    dry_run: bool,
    write_config: F,
) -> DaloResult<SourceAddReport>
where
    F: FnOnce(&StorePaths, &UserConfig) -> DaloResult<()>,
{
    // Validate the id before anything touches the store: it is joined straight
    // into the checkout path and `git clone`d there, so an id like `../../evil`
    // or `a/b` would escape `sources/` to an attacker-chosen location.
    if !is_valid_source_id(id) {
        return Err(DaloError::InvalidSourceId {
            id: id.to_owned(),
            reason: "must be non-empty, not `.`/`..`, and only contain `[A-Za-z0-9._-]`".to_owned(),
        });
    }

    let mut config = store::read_config(paths)?;
    if config.sources.iter().any(|source| source.id == id) {
        return Err(DaloError::SourceAlreadyExists {
            source_id: id.to_owned(),
        });
    }

    let checkout = paths.sources_dir.join(id).join("checkout");
    if checkout.exists() {
        return Err(DaloError::InvalidStorePath {
            path: checkout,
            reason: "source checkout path already exists".to_owned(),
        });
    }

    let priority = config
        .sources
        .iter()
        .map(|source| source.priority)
        .max()
        .unwrap_or(0)
        + 10;
    let source = SourceConfig {
        id: id.to_owned(),
        kind: SourceKind::Team,
        path: checkout.clone(),
        priority,
        enabled: true,
        trusted: true,
        url: Some(url.to_owned()),
        branch: None,
        update_policy: Some("track".to_owned()),
        selection: Vec::new(),
    };

    if dry_run {
        return Ok(SourceAddReport {
            source,
            dry_run: true,
        });
    }

    if let Some(parent) = checkout.parent() {
        std::fs::create_dir_all(parent)?;
    }
    git::clone_repo(url, &checkout)?;

    // From here on the checkout exists on disk. If persisting the source fails,
    // remove the clone so a later `source add` does not trip over an orphaned
    // checkout that is absent from config (InvalidStorePath).
    finish_team_source(paths, &mut config, source.clone(), write_config).inspect_err(|_| {
        let _ = std::fs::remove_dir_all(&checkout);
    })?;

    Ok(SourceAddReport {
        source,
        dry_run: false,
    })
}

/// Return whether a source ID is safe to use as a store path component.
///
/// A source ID becomes a single directory below `sources/` and feeds approval
/// records and resolver output, so it is limited to a conservative path token:
/// non-empty, never the `.`/`..` traversal segments, and limited to
/// `[A-Za-z0-9._-]` (no `/` path separators). Skill slot names are stricter
/// because they are materialized into user-facing target directories.
#[must_use]
pub fn is_valid_source_id(value: &str) -> bool {
    if value.is_empty() || value == "." || value == ".." {
        return false;
    }

    value.chars().all(|character| {
        character.is_ascii_alphanumeric()
            || character == '-'
            || character == '_'
            || character == '.'
    })
}

/// Persist a freshly cloned team source into config.
///
/// User-added sources are `trusted: true`, and the resolver treats trusted
/// sources as approved, so no approval record is needed. If the config write
/// fails, the caller removes the clone so `config.toml` never references a
/// checkout that does not exist.
fn finish_team_source(
    paths: &StorePaths,
    config: &mut UserConfig,
    source: SourceConfig,
    write_config: impl FnOnce(&StorePaths, &UserConfig) -> DaloResult<()>,
) -> DaloResult<()> {
    config.sources.push(source);
    sort_sources(&mut config.sources);
    write_config(paths, config)?;
    Ok(())
}

/// List configured sources.
#[must_use = "the source list report should be rendered or inspected"]
pub fn list_sources(paths: &StorePaths) -> DaloResult<SourceListReport> {
    let mut sources = store::read_config(paths)?.sources;
    sort_sources(&mut sources);
    Ok(SourceListReport { sources })
}

/// Update source priority.
///
/// When `dry_run` is set, the updated source is returned without writing config.
pub fn set_source_priority(
    paths: &StorePaths,
    id: &str,
    priority: i32,
    dry_run: bool,
) -> DaloResult<SourcePriorityReport> {
    let mut config = store::read_config(paths)?;
    let Some(source) = config.sources.iter_mut().find(|source| source.id == id) else {
        return Err(DaloError::UnknownSource {
            source_id: id.to_owned(),
        });
    };
    // The local source is the guaranteed override (priority 0); refuse to move it,
    // otherwise a team skill could shadow a locally adapted one.
    if source.kind == SourceKind::Local {
        return Err(DaloError::LocalSourcePriorityFixed {
            source_id: id.to_owned(),
        });
    }
    source.priority = priority;
    let source = source.clone();

    if !dry_run {
        sort_sources(&mut config.sources);
        store::write_config(paths, &config)?;
    }

    Ok(SourcePriorityReport { source, dry_run })
}

/// Refresh clean tracking team sources before sync.
///
/// Missing or unknown update policies are treated as pinned and are not pulled.
pub fn refresh_tracking_team_sources(paths: &StorePaths) -> DaloResult<()> {
    let config = store::read_config(paths)?;
    refresh_tracking_team_sources_from_config(&config)
}

/// Refresh clean tracking team sources from an already-read config.
///
/// Missing or unknown update policies are treated as pinned and are not pulled.
pub fn refresh_tracking_team_sources_from_config(config: &UserConfig) -> DaloResult<()> {
    for source in config.sources.iter().filter(|source| {
        source.enabled
            && source.kind == SourceKind::Team
            && source.update_policy.as_deref() == Some("track")
    }) {
        if git::is_dirty(&source.path)? {
            return Err(DaloError::DirtySource {
                source_id: source.id.clone(),
            });
        }
        git::pull_ff_only(&source.path)?;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use std::path::Path;
    use std::process::Command;

    use super::*;
    use proptest::prelude::*;

    #[test]
    fn is_valid_source_id_should_reject_traversal_and_slash_ids() {
        assert!(!is_valid_source_id(".."));
        assert!(!is_valid_source_id("a/b"));
        assert!(!is_valid_source_id(""));
    }

    #[test]
    fn is_valid_source_id_should_accept_plain_id() {
        assert!(is_valid_source_id("company"));
    }

    proptest! {
        #[test]
        fn valid_source_ids_should_stay_single_path_components(value in "\\PC{0,64}") {
            if is_valid_source_id(&value) {
                prop_assert!(!value.is_empty());
                prop_assert_ne!(value.as_str(), ".");
                prop_assert_ne!(value.as_str(), "..");
                prop_assert!(!value.contains('/'));
                let portable = value.chars().all(|character| {
                    character.is_ascii_alphanumeric()
                        || character == '-'
                        || character == '_'
                        || character == '.'
                });
                prop_assert!(portable);
            }
        }
    }

    #[test]
    fn add_team_source_should_reject_traversal_id_without_cloning() {
        let temp_dir = tempfile::tempdir().expect("tempdir should be created");
        let store_root = temp_dir.path().join("store");
        store::init_store(store_root.clone(), false).expect("init should succeed");
        let paths = StorePaths::new(store_root);

        let error = add_team_source(
            &paths,
            "../../evil",
            "https://example.invalid/repo.git",
            false,
        )
        .expect_err("traversal id should be rejected");

        assert!(matches!(error, DaloError::InvalidSourceId { .. }));
        // The id must be rejected before any clone target is touched, so nothing
        // may have been written below (or alongside) the sources directory.
        assert!(
            !paths
                .sources_dir
                .join("../../evil")
                .join("checkout")
                .exists()
        );
        assert!(
            std::fs::read_dir(&paths.sources_dir)
                .expect("sources dir should exist")
                .next()
                .is_none()
        );
    }

    #[test]
    fn add_team_source_should_remove_checkout_when_persisting_fails() {
        let temp_dir = tempfile::tempdir().expect("tempdir should be created");
        let store_root = temp_dir.path().join("store");
        let repo = temp_dir.path().join("team-repo");
        create_git_repo(&repo);
        store::init_store(store_root.clone(), false).expect("init should succeed");
        let paths = StorePaths::new(store_root);
        let checkout = paths.sources_dir.join("company").join("checkout");

        let error = add_team_source_with_config_writer(
            &paths,
            "company",
            &repo.to_string_lossy(),
            false,
            |_, _| Err(DaloError::Io(std::io::Error::other("persist failed"))),
        )
        .expect_err("add should fail when config cannot be recorded");

        assert!(matches!(error, DaloError::Io(_)));
        // The orphaned checkout must be gone so a later `source add` is not blocked
        // by a stale checkout, and config must not reference the half-added source.
        assert!(!checkout.exists());
        let config = store::read_config(&paths).expect("config should remain readable");
        assert!(!config.sources.iter().any(|source| source.id == "company"));
    }

    #[test]
    fn add_team_source_should_persist_source_without_leftovers_on_success() {
        let temp_dir = tempfile::tempdir().expect("tempdir should be created");
        let store_root = temp_dir.path().join("store");
        let repo = temp_dir.path().join("team-repo");
        create_git_repo(&repo);
        store::init_store(store_root.clone(), false).expect("init should succeed");
        let paths = StorePaths::new(store_root);

        let report = add_team_source(&paths, "company", &repo.to_string_lossy(), false)
            .expect("add should succeed against a local repo");

        // The checkout exists and a second add of the same id is rejected as a
        // clean duplicate rather than tripping over a stale checkout.
        assert!(report.source.path.join(".git").is_dir());
        let second = add_team_source(&paths, "company", &repo.to_string_lossy(), false)
            .expect_err("a second add of the same id should be a clean duplicate error");
        assert!(matches!(second, DaloError::SourceAlreadyExists { .. }));
    }

    fn create_git_repo(repo: &Path) {
        std::fs::create_dir_all(repo).expect("repo dir should be created");
        run_git(repo, &["init", "-q"]);
        run_git(repo, &["config", "user.email", "test@example.com"]);
        run_git(repo, &["config", "user.name", "Test"]);
        std::fs::write(repo.join("README.md"), "# Repo\n").expect("file should be written");
        run_git(repo, &["add", "."]);
        run_git(repo, &["commit", "-q", "-m", "initial"]);
    }

    fn run_git(repo: &Path, args: &[&str]) {
        let status = Command::new("git")
            .current_dir(repo)
            .args(args)
            .status()
            .expect("git should run");
        assert!(status.success(), "git {args:?} should succeed");
    }
}