tuffcli 0.14.0

Manage and govern agent capabilities.
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
use std::path::{Path, PathBuf};

use clap::ValueEnum;
use serde::Serialize;
use tempfile::TempDir;

use crate::adapter::{AdapterKind, AgentAdapter, resolve_capability};
use crate::error::{Result, TuffError};
use crate::git;
use crate::lockfile::{self, CapabilitySource};
use crate::manifest::{self, load_manifest};
use crate::release::{self, ReleaseTag, VersionRequest};
use crate::resolver::{self, Scope};
use crate::tree_diff::{self, FileChange};

use super::{home_dir, resolve_agent_selection};

#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum DiffFormat {
    Unified,
    Json,
}

#[derive(Debug)]
pub(crate) struct MaterializedTree {
    _sources: Vec<TempDir>,
    path: PathBuf,
}

#[derive(Debug, Serialize)]
struct JsonDiff {
    capability: String,
    target: String,
    /// The release tag an `--upstream` diff was taken against, when the
    /// entry is pinned to one (RFC-101); absent for a HEAD comparison.
    #[serde(skip_serializing_if = "Option::is_none")]
    upstream: Option<String>,
    changes: Vec<FileChange>,
}

/// Which commit of a git source to materialize.
enum GitCheckout<'a> {
    /// The commit the lockfile pinned.
    Pinned,
    /// The latest commit on the default branch.
    Head,
    /// One release tag.
    Tag(&'a str),
}

pub fn cmd_diff(
    repo_root: &Path,
    capability_id: &str,
    target: Option<&str>,
    upstream: bool,
    format: DiffFormat,
) -> Result<()> {
    // `<id>@<requirement>` previews a different requirement than the
    // recorded one, the way `update <id>@<requirement>` would move.
    let (capability_id, new_request) = release::split_version_request(capability_id)?;
    if new_request.is_some() && !upstream {
        return Err(
            TuffError::usage("a version requirement only applies with --upstream").with_hint(
                format!(
                    "run 'tuff diff {capability_id}@{} --upstream'",
                    new_request.unwrap_or_default()
                ),
            ),
        );
    }
    let (scope, entry, scope_root) = match resolver::resolve_entry(capability_id, repo_root)? {
        Some((scope, entry)) => {
            let root = match scope {
                Scope::Project => repo_root.to_path_buf(),
                Scope::Global => home_dir()?,
            };
            (scope, entry, root)
        }
        None => {
            return Err(TuffError::not_found(format!(
                "capability is not installed: {capability_id}"
            )));
        }
    };
    let _ = scope;

    let requested = target
        .map(|value| vec![value.to_string()])
        .unwrap_or_default();
    let targets = resolve_agent_selection(&scope_root, &requested, scope == Scope::Global)?;
    // A release-pinned entry is compared against the newest release its
    // requirement allows, never HEAD: the same answer `update` would give.
    let upstream_release = if upstream {
        upstream_release(capability_id, &entry, new_request)?
    } else {
        None
    };
    if let Some((release, request)) = &upstream_release {
        eprintln!(
            "comparing against {}, the newest release matching {request}",
            release.tag
        );
    }
    let mut unified = String::new();
    let mut json = Vec::new();

    for target in targets {
        let target_entry = entry.targets.get(&target).ok_or_else(|| {
            TuffError::not_found(format!(
                "'{capability_id}' is not installed for target '{target}'"
            ))
        })?;
        let live = installed_path(&scope_root, capability_id, &entry, target_entry, &target);
        let baseline = if upstream {
            let checkout = match &upstream_release {
                Some((release, _)) => GitCheckout::Tag(&release.tag),
                None => GitCheckout::Head,
            };
            materialize_upstream(&scope_root, capability_id, &entry, &target, checkout)?
        } else {
            materialize_baseline(&scope_root, capability_id, &entry, &target)?
        };
        let live_temp = TempDir::new()?;
        if live.is_dir() {
            copy_tree(&live, live_temp.path())?;
        }
        let comparison = tree_diff::compare(&baseline.path, live_temp.path())?;

        if matches!(format, DiffFormat::Json) {
            json.push(JsonDiff {
                capability: capability_id.to_string(),
                target,
                upstream: upstream_release
                    .as_ref()
                    .map(|(release, _)| release.tag.clone()),
                changes: comparison.changes,
            });
        } else if !comparison.patch.is_empty() {
            unified.push_str(&comparison.patch);
        }
    }

    match format {
        DiffFormat::Json => println!("{}", serde_json::to_string_pretty(&json)?),
        DiffFormat::Unified => {
            if unified.is_empty() && upstream {
                match &upstream_release {
                    Some((release, _)) => println!("no upstream changes in {}", release.tag),
                    None => println!("no upstream changes"),
                }
            } else if !unified.is_empty() {
                print!("{}", style_diff(&unified));
            }
        }
    }
    Ok(())
}

/// The release an `--upstream` diff compares against, with the requirement
/// that chose it: the one given on the command line, else the recorded
/// one. `None` for anything not pinned to a release, which compares
/// against HEAD as always.
fn upstream_release(
    id: &str,
    entry: &lockfile::CapabilityLockEntry,
    new_request: Option<&str>,
) -> Result<Option<(ReleaseTag, VersionRequest)>> {
    let CapabilitySource::Git(git) = &entry.source else {
        if new_request.is_some() {
            return Err(TuffError::usage(format!(
                "'{id}' was not installed from git, so a version requirement does not apply"
            )));
        }
        return Ok(None);
    };
    let request = match new_request.or(git.requested.as_deref()) {
        Some(text) => VersionRequest::parse(text)?,
        None => return Ok(None),
    };
    let release = super::resolve_git_release(&git.url, id, &request)?;
    Ok(Some((release, request)))
}

pub(crate) fn materialize_baseline(
    scope_root: &Path,
    id: &str,
    entry: &lockfile::CapabilityLockEntry,
    target: &str,
) -> Result<MaterializedTree> {
    let target_entry = entry.targets.get(target).ok_or_else(|| {
        TuffError::not_found(format!("no lock entry for '{id}' at target '{target}'"))
    })?;
    let expected = &target_entry.sha256;
    if expected.is_empty() {
        return Err(TuffError::corrupt(format!(
            "lock entry for '{id}' has no materialized baseline hash"
        )));
    }
    let home = super::home_dir()?;
    if let Some(path) = crate::cache::read_verified(&home, expected)? {
        return Ok(MaterializedTree {
            _sources: Vec::new(),
            path,
        });
    }

    let materialized = materialize_source(scope_root, id, entry, target, GitCheckout::Pinned)?;
    let actual = crate::cache::hash_tree(&materialized.path)?;
    if actual != *expected {
        if let Some(source_path) = entry.source.local_path()
            && !Path::new(source_path).is_absolute()
            && !scope_root.join(source_path).exists()
        {
            return Err(TuffError::not_found(format!(
                "The recorded baseline for \"{id}\" is not cached, and its local source \"{source_path}\" is no longer available.\n\nRestore the source, reinstall the capability, or run the appropriate Tuff command to accept the current content as a new baseline."
            )));
        }
        return Err(TuffError::corrupt(format!(
            "recorded baseline verification failed for '{id}': expected {expected}, got {actual}; the source does not reproduce the recorded baseline"
        )));
    }
    let cached = crate::cache::populate(&home, expected, &materialized.path)?;
    Ok(MaterializedTree {
        _sources: materialized._sources,
        path: cached,
    })
}

fn materialize_upstream(
    scope_root: &Path,
    id: &str,
    entry: &lockfile::CapabilityLockEntry,
    target: &str,
    checkout: GitCheckout<'_>,
) -> Result<MaterializedTree> {
    match &entry.source {
        CapabilitySource::Local(_) | CapabilitySource::Pack(_) => {
            return Err(TuffError::unsupported(
                "upstream diff only available for git-sourced capabilities",
            ));
        }
        CapabilitySource::Catalog(_) => {
            return Err(TuffError::unsupported(
                "upstream diff is not available for catalog capabilities",
            )
            .with_hint("run 'tuff outdated' to compare against the built-in catalog"));
        }
        CapabilitySource::Git(_) => {}
    }
    materialize_source(scope_root, id, entry, target, checkout)
}

fn materialize_source(
    scope_root: &Path,
    id: &str,
    entry: &lockfile::CapabilityLockEntry,
    target: &str,
    checkout: GitCheckout<'_>,
) -> Result<MaterializedTree> {
    let adapter = AdapterKind::from_id(target)
        .ok_or_else(|| TuffError::usage(format!("unknown target '{target}'")))?;
    let missing_local = |source_path: &str| {
        TuffError::not_found(format!(
            "The recorded baseline for \"{id}\" is not cached, and its local source \"{source_path}\" is no longer available.\n\nRestore the source, reinstall the capability, or run the appropriate Tuff command to accept the current content as a new baseline."
        ))
    };

    // An adopted capability, or a pack member, has no source tree apart
    // from the installed one.
    let in_place = match &entry.source {
        CapabilitySource::Local(local) => is_in_place_source_path(&local.path),
        CapabilitySource::Pack(_) => true,
        CapabilitySource::Git(_) | CapabilitySource::Catalog(_) => false,
    };
    if in_place {
        let fallback = entry.source.local_path().unwrap_or_default();
        let path = entry
            .targets
            .get(target)
            .map(|target_entry| target_entry.installed_path.as_str())
            .filter(|path| !path.is_empty())
            .map(|path| scope_root.join(path))
            .unwrap_or_else(|| scope_root.join(fallback));
        if !path.is_dir() {
            return Err(missing_local(fallback));
        }
        return Ok(MaterializedTree {
            _sources: Vec::new(),
            path,
        });
    }

    let (source_guard, capability_dir) = match &entry.source {
        CapabilitySource::Catalog(catalog) => {
            let manifest = crate::catalog::lookup(&catalog.id)?.ok_or_else(|| {
                TuffError::not_found(format!(
                    "The recorded baseline for \"{id}\" is not cached, and '{}' is no longer in the built-in catalog.",
                    catalog.id
                ))
            })?;
            return plan_into_temp(adapter, &manifest, entry, id, None);
        }
        CapabilitySource::Git(git) => {
            let (guard, checkout, _) = match checkout {
                GitCheckout::Pinned => git::clone_to_temp(&git.url, Some(git.git_ref.as_str()))?,
                GitCheckout::Head => git::clone_to_temp(&git.url, None)?,
                GitCheckout::Tag(tag) => git::clone_tag_to_temp(&git.url, tag)?,
            };
            let dir = git::discover_capability(&checkout, &git.path, entry.capability_type)?;
            (Some(guard), dir)
        }
        CapabilitySource::Local(local) => {
            let dir = lockfile::absolutize(scope_root, Path::new(&local.path));
            if !dir.is_dir() {
                return Err(missing_local(&local.path));
            }
            (None, dir)
        }
        CapabilitySource::Pack(_) => unreachable!("pack members are materialized in place"),
    };
    let manifest = if capability_dir.join("tuff.toml").is_file() {
        load_manifest(&capability_dir)?
    } else {
        manifest::synthetic_manifest(&capability_dir, id, "materialized")?
    };
    plan_into_temp(adapter, &manifest, entry, id, source_guard)
}

/// Plan a manifest into a scratch tree and hand back the capability's
/// installed root within it, keeping any source checkout alive alongside.
fn plan_into_temp(
    adapter: AdapterKind,
    manifest: &manifest::CapabilityManifest,
    entry: &lockfile::CapabilityLockEntry,
    id: &str,
    source_guard: Option<TempDir>,
) -> Result<MaterializedTree> {
    let capability = resolve_capability(manifest)?;
    let temp = TempDir::new()?;
    let plans = adapter.plan(&capability, temp.path())?;
    let target_root = temp
        .path()
        .join(adapter.dir_prefix())
        .join(entry.capability_type.plural_dir())
        .join(id);
    for planned in plans {
        let path = temp.path().join(&planned.path);
        if path.starts_with(&target_root) {
            if let Some(parent) = path.parent() {
                std::fs::create_dir_all(parent)?;
            }
            std::fs::write(path, planned.content)?;
        }
    }
    let mut sources = vec![temp];
    if let Some(source_guard) = source_guard {
        sources.push(source_guard);
    }
    Ok(MaterializedTree {
        _sources: sources,
        path: target_root,
    })
}

fn installed_path(
    root: &Path,
    id: &str,
    entry: &lockfile::CapabilityLockEntry,
    target_entry: &lockfile::TargetLockEntry,
    target: &str,
) -> PathBuf {
    if !target_entry.installed_path.is_empty() {
        return root.join(&target_entry.installed_path);
    }
    let adapter = AdapterKind::from_id(target).unwrap_or(AdapterKind::OpenAgents);
    root.join(adapter.dir_prefix())
        .join(entry.capability_type.plural_dir())
        .join(id)
}

fn is_in_place_source_path(path: &str) -> bool {
    path.starts_with(".agents/") || path.starts_with(".claude/")
}

fn copy_tree(source: &Path, destination: &Path) -> Result<()> {
    std::fs::create_dir_all(destination)?;
    for entry in std::fs::read_dir(source)? {
        let entry = entry?;
        let source_path = entry.path();
        let destination_path = destination.join(entry.file_name());
        if source_path.is_dir() {
            copy_tree(&source_path, &destination_path)?;
        } else if source_path.is_file() {
            std::fs::copy(source_path, destination_path)?;
        }
    }
    Ok(())
}

fn style_diff(diff: &str) -> String {
    if !super::use_color() {
        return diff.to_string();
    }
    diff.split_inclusive('\n')
        .map(|line| {
            let code = if line.starts_with("+++") || line.starts_with("---") {
                "36"
            } else if line.starts_with('+') {
                "32"
            } else if line.starts_with('-') {
                "31"
            } else {
                return line.to_string();
            };
            format!("\x1b[{code}m{line}\x1b[0m")
        })
        .collect()
}