trusty-code 0.7.0

Per-project Claude-Code-compatible MPM orchestration harness (bin: tcode) — an original coding harness, tracked under epic #2052.
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
//! Deterministic, reversible legacy import from `.claude/` into `.trusty-code/`
//! (#5426, epic #2892).
//!
//! Why: switching discovery to `.trusty-code/` would strand every project whose
//! agents and skills live under `.claude/`. An import has to be observable
//! before it runs (a plan), it must never overwrite a file the project author
//! wrote, and it must be undoable — so it reports exactly which files it
//! created, and nothing else.
//!
//! What: [`plan_import`] walks `.claude/agents/`, `.claude/skills/`, and
//! `.claude/settings.json` and produces a SORTED [`ImportPlan`] naming, for each
//! source file, the target under `.trusty-code/` and the [`ImportAction`] that
//! would be taken. [`apply_import`] executes exactly that plan and returns an
//! [`ImportReport`] listing the files it created. Determinism is what makes the
//! dry run trustworthy: the same tree always yields the same plan in the same
//! order, so `--dry-run` output and the real run cannot disagree.
//!
//! Four refusals, all producing [`ImportAction::Refuse`] rather than an error
//! that aborts the whole import:
//!
//! - the target already exists (never overwrite a user-authored file);
//! - the source is, or reaches through, a symlink out of `.claude/`;
//! - the source carries the executable bit (an imported executable would be
//!   trusted by every later `.trusty-code/` consumer);
//! - a `settings.json` whose JSON carries a secret-bearing key
//!   ([`super::find_secret_key`]).
//!
//! Test: `paths::import_tests::*`.

use std::path::{Path, PathBuf};

use super::write_dir::NativeWriteDir;
use super::{CLAUDE_COMPAT_DIRNAME, SETTINGS_FILENAME, check_native_write_target, native_child};

/// The `.claude/` subtrees an import considers, in plan order.
///
/// Why: agents and skills are the authored content #5426 must carry across;
/// plugins are third-party trees whose provenance this import cannot vouch for,
/// so they are deliberately NOT copied — a plugin stays discoverable in place
/// through the compatibility search root.
/// What: `["agents", "skills"]`.
/// Test: `paths::import_tests::plan_is_sorted_and_deterministic`.
pub const IMPORTED_SUBTREES: &[&str] = &["agents", "skills"];

/// What an import would do with one source file.
///
/// Why: a plan that only listed copies could not explain a file's absence from
/// the result, which is the question an operator asks first.
/// What: [`ImportAction::Copy`] or [`ImportAction::Refuse`] with a
/// human-readable reason.
/// Test: `paths::import_tests::existing_target_is_never_overwritten`,
/// `paths::import_tests::executable_source_is_refused`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ImportAction {
    /// The file will be copied to its target.
    Copy,
    /// The file will be skipped; the string says why.
    Refuse(String),
}

/// One source file and the target it maps to.
///
/// Why: the unit both the dry-run listing and the applied report are built from,
/// so the two can never describe different work.
/// What: absolute `from` under `.claude/`, absolute `to` under `.trusty-code/`,
/// and the [`ImportAction`].
/// Test: `paths::import_tests::plan_is_sorted_and_deterministic`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImportEntry {
    /// The source file under `<project>/.claude/`.
    pub from: PathBuf,
    /// The target under `<project>/.trusty-code/`.
    pub to: PathBuf,
    /// What the import would do with it.
    pub action: ImportAction,
}

/// Everything an import would do, in a stable order.
///
/// Why: producing the plan and executing it are separate steps precisely so the
/// operator can read the first before authorising the second.
/// What: entries sorted by target path.
/// Test: `paths::import_tests::plan_is_sorted_and_deterministic`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ImportPlan {
    /// The per-file decisions, sorted by [`ImportEntry::to`].
    pub entries: Vec<ImportEntry>,
}

impl ImportPlan {
    /// The entries that would actually be copied.
    ///
    /// Why: the count an operator wants before confirming, without re-filtering
    /// at each call site.
    /// What: entries whose action is [`ImportAction::Copy`].
    /// Test: `paths::import_tests::plan_is_sorted_and_deterministic`.
    pub fn to_copy(&self) -> impl Iterator<Item = &ImportEntry> {
        self.entries
            .iter()
            .filter(|e| e.action == ImportAction::Copy)
    }
}

/// What an applied import actually created, and what it declined.
///
/// Why: reversibility is a property of the REPORT, not of a flag — deleting
/// exactly the paths in `created` restores the tree, and nothing else was
/// touched.
/// What: `created` holds the files written (sorted); `refused` pairs each
/// skipped source with its reason.
/// Test: `paths::import_tests::apply_creates_only_the_planned_files`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ImportReport {
    /// Files this import created, sorted — delete exactly these to undo it.
    pub created: Vec<PathBuf>,
    /// Sources not imported, each with the reason.
    pub refused: Vec<(PathBuf, String)>,
}

/// Build the import plan for a project root.
///
/// Why: the observable half of the import. It touches nothing, so it is safe to
/// run in a diagnostic.
/// What: walks each of [`IMPORTED_SUBTREES`] under `<root>/.claude/`, plus
/// `.claude/settings.json`, mapping every regular file to the same relative path
/// under `<root>/.trusty-code/` and classifying it per the module docs. Entries
/// are sorted by target path so the plan is a function of the tree alone. A
/// missing `.claude/` yields an empty plan, never an error.
/// Test: `paths::import_tests::plan_is_sorted_and_deterministic`,
/// `paths::import_tests::missing_claude_dir_yields_empty_plan`,
/// `paths::import_tests::symlink_escaping_claude_is_refused`.
pub fn plan_import(project_root: &Path) -> ImportPlan {
    let claude_root = project_root.join(CLAUDE_COMPAT_DIRNAME);
    let mut entries = Vec::new();

    for subtree in IMPORTED_SUBTREES {
        let dir = claude_root.join(subtree);
        let mut files = Vec::new();
        collect_files(&dir, &dir, subtree, &mut files);
        for (relative, from) in files {
            entries.push(classify(project_root, &claude_root, &relative, &from));
        }
    }

    let settings = claude_root.join(SETTINGS_FILENAME);
    if settings.is_file() {
        entries.push(classify(
            project_root,
            &claude_root,
            SETTINGS_FILENAME,
            &settings,
        ));
    }

    entries.sort_by(|a, b| a.to.cmp(&b.to));
    ImportPlan { entries }
}

/// Recursively collect the regular files under `dir`, relative to `base`.
///
/// Why: the plan must cover a skill's whole directory (`SKILL.md` plus its
/// `references/`), not just its top level.
/// What: appends `(relative_path_with_prefix, absolute_path)` pairs. A directory
/// that cannot be read is skipped and logged at `warn` with the path tried —
/// fail open, since one unreadable subtree must not abort the import of the
/// rest. Symlinked DIRECTORIES are not descended into; a symlinked file is
/// collected and refused later by [`classify`], where the reason can be
/// reported.
/// Test: `paths::import_tests::plan_is_sorted_and_deterministic`,
/// `paths::import_tests::unreadable_subtree_is_skipped_with_a_warning`.
fn collect_files(base: &Path, dir: &Path, prefix: &str, out: &mut Vec<(String, PathBuf)>) {
    if !dir.is_dir() {
        return;
    }
    let entries = match std::fs::read_dir(dir) {
        Ok(entries) => entries,
        Err(e) => {
            // #5426: fail open — an unreadable subtree loses its files, not the
            // whole import, and says so.
            tracing::warn!(
                path = %dir.display(),
                error = %e,
                "could not read a .claude subtree while planning the trusty-code \
                 import; skipping it and continuing with the rest"
            );
            return;
        }
    };
    for entry in entries.flatten() {
        let path = entry.path();
        let Ok(meta) = std::fs::symlink_metadata(&path) else {
            continue;
        };
        if meta.is_dir() {
            collect_files(base, &path, prefix, out);
        } else if let Ok(rel) = path.strip_prefix(base) {
            out.push((format!("{prefix}/{}", rel.display()), path.clone()));
        }
    }
}

/// Decide what to do with one source file.
///
/// Why: every refusal rule lives here so the plan and the applied run cannot
/// diverge on a judgement call.
/// What: applies, in order, the write-target guard (which catches a symlinked
/// `.trusty-code/` subdirectory pointing out of the project), the
/// symlink-escape check on the SOURCE, the existing-target check, the
/// executable-bit check, and — for `settings.json` — the secret-key check.
/// Test: `paths::import_tests::existing_target_is_never_overwritten`,
/// `paths::import_tests::executable_source_is_refused`,
/// `paths::import_tests::symlink_escaping_claude_is_refused`,
/// `paths::import_tests::secret_bearing_settings_is_refused`.
fn classify(project_root: &Path, claude_root: &Path, relative: &str, from: &Path) -> ImportEntry {
    let to = native_child(project_root, relative);
    let refuse = |reason: String| ImportEntry {
        from: from.to_path_buf(),
        to: to.clone(),
        action: ImportAction::Refuse(reason),
    };

    if let Err(e) = check_native_write_target(project_root, &to) {
        return refuse(e.to_string());
    }
    if !source_stays_inside(claude_root, from) {
        return refuse(format!(
            "source resolves outside {} — refusing to import through a symlink",
            claude_root.display()
        ));
    }
    if to.exists() {
        return refuse(format!(
            "{} already exists — an import never overwrites a user-authored file",
            to.display()
        ));
    }
    if is_executable(from) {
        return refuse(
            "source carries the executable bit — refusing to import an executable \
             into trusty-code's own configuration tree"
                .to_string(),
        );
    }
    if relative == SETTINGS_FILENAME
        && let Some(key) = settings_secret_key(from)
    {
        return refuse(format!(
            "settings carry a secret-bearing key `{key}` — remove it, or set it \
             in the environment, before importing"
        ));
    }

    ImportEntry {
        from: from.to_path_buf(),
        to,
        action: ImportAction::Copy,
    }
}

/// Whether a source file really lives inside `.claude/`.
///
/// Why: `.claude/agents/leak.md -> ~/.ssh/id_rsa` would otherwise be copied into
/// a tree the project commits.
/// What: canonicalises both and tests containment; a source that cannot be
/// canonicalised (a dangling symlink) is treated as escaping.
/// Test: `paths::import_tests::symlink_escaping_claude_is_refused`.
fn source_stays_inside(claude_root: &Path, from: &Path) -> bool {
    match (claude_root.canonicalize(), from.canonicalize()) {
        (Ok(root), Ok(file)) => file.starts_with(&root),
        _ => false,
    }
}

/// Whether a file has any executable bit set.
///
/// Why: an imported executable would be trusted by every later consumer of
/// `.trusty-code/`, which is exactly the provenance this import cannot vouch
/// for.
/// What: on Unix, any of `0o111`. On other platforms, `false` — there is no
/// executable bit to inspect, and the extension-based equivalent would be a
/// different rule wearing the same name.
/// Test: `paths::import_tests::executable_source_is_refused`.
#[cfg(unix)]
fn is_executable(path: &Path) -> bool {
    use std::os::unix::fs::PermissionsExt;
    std::fs::metadata(path)
        .map(|m| m.permissions().mode() & 0o111 != 0)
        .unwrap_or(false)
}

/// See the Unix variant — non-Unix targets expose no executable bit.
#[cfg(not(unix))]
fn is_executable(_path: &Path) -> bool {
    false
}

/// The first secret-bearing key in a settings file, if any.
///
/// Why: a settings file that will not parse is not evidence that it is clean, so
/// an unparseable file is refused too rather than waved through.
/// What: parses the file as JSON and delegates to [`super::find_secret_key`]; an
/// I/O or parse failure yields a synthetic reason naming the problem, logged at
/// `warn` with the path tried.
/// Test: `paths::import_tests::secret_bearing_settings_is_refused`,
/// `paths::import_tests::unparseable_settings_is_refused`.
fn settings_secret_key(path: &Path) -> Option<String> {
    let raw = match std::fs::read_to_string(path) {
        Ok(raw) => raw,
        Err(e) => {
            tracing::warn!(
                path = %path.display(),
                error = %e,
                "could not read .claude/settings.json while planning the import; \
                 refusing to import it rather than assuming it holds no secrets"
            );
            return Some("<unreadable>".to_string());
        }
    };
    match serde_json::from_str::<serde_json::Value>(&raw) {
        Ok(value) => super::find_secret_key(&value),
        Err(e) => {
            tracing::warn!(
                path = %path.display(),
                error = %e,
                "could not parse .claude/settings.json while planning the import; \
                 refusing to import it rather than assuming it holds no secrets"
            );
            Some("<unparseable>".to_string())
        }
    }
}

/// Execute a plan, creating only the files it marked [`ImportAction::Copy`].
///
/// Why: taking the plan as an ARGUMENT (rather than recomputing it) is what
/// makes `--dry-run` meaningful — the operator authorises the exact list they
/// read.
/// What: creates each target's parent directory and copies the file, in plan
/// order. Returns the [`ImportReport`]. A copy that fails mid-run is reported as
/// a refusal with the I/O error and does not abort the remaining entries, so one
/// bad file cannot leave the import half-done with no record of what landed.
/// Test: `paths::import_tests::apply_creates_only_the_planned_files`,
/// `paths::import_tests::apply_is_idempotent`,
/// `paths::import_tests::swapped_target_dir_never_reaches_the_victim`.
pub fn apply_import(project_root: &Path, plan: &ImportPlan) -> ImportReport {
    let mut report = ImportReport::default();
    for entry in &plan.entries {
        match &entry.action {
            ImportAction::Refuse(reason) => {
                report.refused.push((entry.from.clone(), reason.clone()));
            }
            ImportAction::Copy => match copy_one(project_root, &entry.from, &entry.to) {
                Ok(()) => report.created.push(entry.to.clone()),
                Err(e) => report
                    .refused
                    .push((entry.from.clone(), format!("copy failed: {e}"))),
            },
        }
    }
    report.created.sort();
    report
}

/// Copy one file through a pinned handle on its target directory.
///
/// Why: #7779. The plan's write-target check ran in [`classify`], and an
/// operator can sit on a `--dry-run` listing for minutes before authorising it —
/// the widest check-to-write window in the crate. A `create_dir_all` plus
/// `std::fs::copy` through the same path string followed whatever had been
/// swapped in since.
/// What: re-derives the target's path relative to `<project>/.trusty-code/`,
/// opens that directory with [`NativeWriteDir`] (which re-applies the membership
/// rule and then pins the directory), and writes with `create_new` — so the
/// "never overwrite a user-authored file" rule is decided by `O_EXCL` at the
/// moment of the write rather than by the plan's `exists()` test.
/// Test: `paths::import_tests::apply_creates_only_the_planned_files`,
/// `paths::import_tests::swapped_target_dir_never_reaches_the_victim`.
fn copy_one(project_root: &Path, from: &Path, to: &Path) -> std::io::Result<()> {
    let relative = to
        .strip_prefix(super::native_config_dir(project_root))
        .map_err(|_| {
            std::io::Error::other(format!(
                "{} is not beneath the project's own configuration directory",
                to.display()
            ))
        })?;
    let (parent, name) = match (
        relative.parent(),
        relative.file_name().and_then(|n| n.to_str()),
    ) {
        (Some(parent), Some(name)) => (parent, name),
        _ => {
            return Err(std::io::Error::other(format!(
                "{} is not a usable import target",
                to.display()
            )));
        }
    };
    let content = std::fs::read(from)?;
    NativeWriteDir::open(project_root, parent)
        .and_then(|dir| dir.create_new(name, &content))
        .map_err(std::io::Error::other)
}

#[cfg(test)]
#[path = "import_tests.rs"]
mod import_tests;