surjection-core 1.2.0

Core library of Surjection: MCP servers, skills and secret references for AI coding clients
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
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
//! Pre-write copies of client files (SPEC 8.4 step 2, 9.5):
//! `backups/files/<ts>-<apply-id>/<encoded-path>`, readable by the current
//! user only, because client files may hold secrets. Machine-local; never
//! pushed.
//!
//! An import that moves literal secrets to references leaves the old values
//! in the backups its apply made (M35): [`mark_old_values`] records which
//! ones in the run's result, and [`purge_old_values`] removes exactly those.

use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use crate::apply::{ApplyEnv, journal};
use crate::error::{CoreError, ErrorCode};
use crate::fs::Fs;
use crate::paths::SurjectionHome;
use crate::secrets::SecretValue;
use crate::secrets::scanner::MIN_TRACKED_LEN;
use crate::store::{LOCK_TIMEOUT, StoreLock, utc_datetime};

/// Directory under `backups/` holding one directory per apply run.
pub const FILES_DIR: &str = "files";
/// Runs always kept.
const KEEP_RUNS: usize = 50;
/// Older runs beyond [`KEEP_RUNS`] are kept until they are this old.
const KEEP_FOR: Duration = Duration::from_secs(30 * 86_400);

/// `20260101T000000Z`: sorts by time, safe in a file name on every OS.
pub fn timestamp(time: SystemTime) -> String {
    let datetime = utc_datetime(time);
    let (date, clock) = (datetime.date.expect("set"), datetime.time.expect("set"));
    format!(
        "{:04}{:02}{:02}T{:02}{:02}{:02}Z",
        date.year,
        date.month,
        date.day,
        clock.hour,
        clock.minute,
        clock.second.unwrap_or(0)
    )
}

/// The directory of one apply run's backups.
pub fn run_dir(home: &SurjectionHome, time: SystemTime, apply_id: &str) -> PathBuf {
    home.backups_dir()
        .join(FILES_DIR)
        .join(format!("{}-{apply_id}", timestamp(time)))
}

/// An absolute path as one file name: `%`, the drive colon, both
/// separators and every other character a file name cannot hold on some
/// OS are percent-encoded, so the original path is unambiguous on every OS
/// (`C%3A%5CUsers%5Cme%5C.claude.json`).
pub fn encode_path(path: &Path) -> String {
    let text = path.to_string_lossy();
    let mut out = String::with_capacity(text.len());
    for c in text.chars() {
        let reserved = matches!(
            c,
            '%' | ':' | '/' | '\\' | '<' | '>' | '"' | '|' | '?' | '*'
        ) || c.is_control();
        if reserved {
            let mut buf = [0u8; 4];
            for byte in c.encode_utf8(&mut buf).bytes() {
                out.push_str(&format!("%{byte:02X}"));
            }
        } else {
            out.push(c);
        }
    }
    out
}

/// The path an [`encode_path`] name stands for.
pub fn decode_path(name: &str) -> Option<String> {
    let bytes = name.as_bytes();
    let mut out = Vec::with_capacity(bytes.len());
    let mut index = 0;
    while index < bytes.len() {
        if bytes[index] == b'%' {
            let hex = name.get(index + 1..index + 3)?;
            out.push(u8::from_str_radix(hex, 16).ok()?);
            index += 3;
        } else {
            out.push(bytes[index]);
            index += 1;
        }
    }
    String::from_utf8(out).ok()
}

/// Copies `bytes`, the current content of `file`, into the run directory
/// `dir`. The `backups/`, `backups/files/` and run directories and the copy
/// are restricted to the current user.
pub fn save(
    fs: &dyn Fs,
    home: &SurjectionHome,
    dir: &Path,
    file: &Path,
    bytes: &[u8],
) -> Result<PathBuf, CoreError> {
    for level in [
        home.backups_dir(),
        home.backups_dir().join(FILES_DIR),
        dir.to_path_buf(),
    ] {
        fs.create_private_dir_all(&level).map_err(|err| {
            CoreError::new(
                ErrorCode::IoError,
                format!("cannot create backup directory: {err}"),
            )
            .with_path(&level)
        })?;
    }
    let path = dir.join(encode_path(file));
    fs.write_private(&path, bytes).map_err(|err| {
        CoreError::new(ErrorCode::IoError, format!("cannot write backup: {err}")).with_path(&path)
    })?;
    Ok(path)
}

/// Retention (SPEC 9.5): the last 50 runs, or 30 days, whichever keeps
/// more. Directories that do not look like runs are left alone.
pub fn prune(fs: &dyn Fs, home: &SurjectionHome, now: SystemTime) -> Result<(), CoreError> {
    let root = home.backups_dir().join(FILES_DIR);
    if !fs.is_dir(&root) {
        return Ok(());
    }
    let mut runs: Vec<(PathBuf, SystemTime)> = fs
        .read_dir(&root)
        .map_err(|err| {
            CoreError::new(ErrorCode::IoError, format!("cannot list backups: {err}"))
                .with_path(&root)
        })?
        .into_iter()
        .filter_map(|path| {
            let name = path.file_name()?.to_str()?;
            let time = parse_timestamp(name.get(..16)?)?;
            Some((path, time))
        })
        .collect();
    runs.sort_by_key(|(_, time)| *time);
    let old = runs.len().saturating_sub(KEEP_RUNS);
    for (path, time) in runs.into_iter().take(old) {
        let age = now.duration_since(time).unwrap_or_default();
        if age > KEEP_FOR {
            fs.remove_dir_all(&path).map_err(|err| {
                CoreError::new(ErrorCode::IoError, format!("cannot prune backups: {err}"))
                    .with_path(&path)
            })?;
        }
    }
    Ok(())
}

/// One apply run's client file backups.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BackupRun {
    /// The run's directory name (`<ts>-<apply-id>`), unique per run.
    pub dir: String,
    /// The apply id, or `restore-<run id>-<ulid>` for the bytes a client
    /// file restore replaced.
    pub apply_id: String,
    pub time: SystemTime,
    /// The original paths of the files backed up, sorted.
    pub files: Vec<PathBuf>,
}

/// Every apply run with file backups on this machine, newest first.
/// Directories that do not look like runs are skipped.
pub fn list_runs(fs: &dyn Fs, home: &SurjectionHome) -> Result<Vec<BackupRun>, CoreError> {
    let root = home.backups_dir().join(FILES_DIR);
    if !fs.is_dir(&root) {
        return Ok(Vec::new());
    }
    let list = |dir: &Path| {
        fs.read_dir(dir).map_err(|err| {
            CoreError::new(ErrorCode::IoError, format!("cannot list backups: {err}")).with_path(dir)
        })
    };
    let mut runs = Vec::new();
    for dir in list(&root)? {
        let Some(name) = dir.file_name().and_then(|name| name.to_str()) else {
            continue;
        };
        let (Some(time), Some(apply_id)) = (
            name.get(..16).and_then(parse_timestamp),
            name.get(17..)
                .filter(|_| name.as_bytes().get(16) == Some(&b'-')),
        ) else {
            continue;
        };
        if !fs.is_dir(&dir) {
            continue;
        }
        let mut files: Vec<PathBuf> = list(&dir)?
            .iter()
            .filter_map(|file| decode_path(file.file_name()?.to_str()?))
            .map(PathBuf::from)
            .collect();
        files.sort();
        runs.push(BackupRun {
            dir: name.to_owned(),
            apply_id: apply_id.to_owned(),
            time,
            files,
        });
    }
    runs.sort_by(|a, b| {
        b.time
            .cmp(&a.time)
            .then_with(|| b.apply_id.cmp(&a.apply_id))
    });
    Ok(runs)
}

/// Reads a [`timestamp`].
pub fn parse_timestamp(text: &str) -> Option<SystemTime> {
    let bytes = text.as_bytes();
    if bytes.len() != 16 || bytes[8] != b'T' || bytes[15] != b'Z' {
        return None;
    }
    let number = |range: std::ops::Range<usize>| text.get(range)?.parse::<i64>().ok();
    let (year, month, day) = (number(0..4)?, number(4..6)?, number(6..8)?);
    let (hour, minute, second) = (number(9..11)?, number(11..13)?, number(13..15)?);
    let days = days_from_civil(year, month, day);
    let seconds = days * 86_400 + hour * 3600 + minute * 60 + second;
    Some(UNIX_EPOCH + Duration::from_secs(u64::try_from(seconds).ok()?))
}

/// Days since 1970-01-01 of a proleptic Gregorian date (Howard Hinnant's
/// `days_from_civil`).
fn days_from_civil(year: i64, month: i64, day: i64) -> i64 {
    let year = if month <= 2 { year - 1 } else { year };
    let era = year.div_euclid(400);
    let yoe = year - era * 400;
    let mp = (month + 9) % 12;
    let doy = (153 * mp + 2) / 5 + day - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    era * 146_097 + doe - 719_468
}

/// The client files whose backup of the finished run `apply_id` holds one
/// of `values` (an import's extracted secrets, which the run replaced with
/// references), each marked `old_values` in the run's result (M35). Values
/// are compared in memory and never kept; values shorter than the leak
/// scanner's minimum are not looked for, and a backup that cannot be read
/// is left unmarked.
pub fn mark_old_values(
    fs: &dyn Fs,
    state_dir: &Path,
    apply_id: &str,
    values: &[SecretValue],
) -> Result<Vec<PathBuf>, CoreError> {
    let needles = value_forms(values);
    if needles.is_empty() {
        return Ok(Vec::new());
    }
    let Some(mut run) = journal::result(fs, state_dir, apply_id)? else {
        return Ok(Vec::new());
    };
    let mut found = Vec::new();
    for record in run.files.iter_mut().filter(|record| !record.backup_removed) {
        let Some(backup) = record.backup.as_deref() else {
            continue;
        };
        let Ok(bytes) = fs.read(Path::new(backup)) else {
            continue;
        };
        if needles.iter().any(|needle| contains(&bytes, needle)) {
            record.old_values = true;
            found.push(PathBuf::from(&record.file));
        }
    }
    if !found.is_empty() {
        run.save_result(fs, state_dir)?;
    }
    Ok(found)
}

/// Each value as a client file may hold it: as is, trimmed, and escaped in
/// a JSON or TOML string.
fn value_forms(values: &[SecretValue]) -> Vec<Vec<u8>> {
    let mut forms: BTreeSet<Vec<u8>> = BTreeSet::new();
    for value in values {
        let value = value.expose();
        let escaped = serde_json::to_string(value).unwrap_or_default();
        let escaped = escaped
            .strip_prefix('"')
            .and_then(|rest| rest.strip_suffix('"'))
            .unwrap_or_default();
        for form in [value, value.trim(), escaped] {
            if form.len() >= MIN_TRACKED_LEN {
                forms.insert(form.as_bytes().to_vec());
            }
        }
    }
    forms.into_iter().collect()
}

fn contains(haystack: &[u8], needle: &[u8]) -> bool {
    haystack
        .windows(needle.len())
        .any(|window| window == needle)
}

/// The files of `files` that hold one of `values` (compared as in
/// [`mark_old_values`]); a file that cannot be read is left out.
pub(crate) fn files_holding(
    fs: &dyn Fs,
    files: &[PathBuf],
    values: &[SecretValue],
) -> Vec<PathBuf> {
    let needles = value_forms(values);
    if needles.is_empty() {
        return Vec::new();
    }
    files
        .iter()
        .filter(|file| {
            fs.read(file)
                .is_ok_and(|bytes| needles.iter().any(|needle| contains(&bytes, needle)))
        })
        .cloned()
        .collect()
}

/// What [`purge_old_values`] did.
#[derive(Debug, Default)]
pub struct Purge {
    /// The client files whose backup was overwritten with zeros (and, but
    /// for those in `not_deleted`, deleted): the old values are gone.
    pub removed: Vec<PathBuf>,
    /// Of `removed`, the client files whose zeroed backup could not be
    /// deleted, with why. It holds no old value any more and is never
    /// restored.
    pub not_deleted: Vec<(PathBuf, CoreError)>,
    /// The client files whose backup was left as it was, with why.
    pub failed: Vec<(PathBuf, CoreError)>,
}

/// Removes the backups of apply run `apply_id` that hold old secret values
/// (those [`mark_old_values`] marked; M35). A recorded backup is touched
/// only when it is this run's backup of its file
/// (`backups/files/<ts>-<apply-id>/<encoded file>`) and a regular file,
/// not a link. It is overwritten with zeros in place, recorded as removed
/// right then (so `restore --files` never writes the zeros back), and
/// deleted. A failure is reported for its file and the others still go. A
/// rollback or `restore --files` of that run then reports the file's
/// original as no longer kept. A run left without backups is removed.
/// Nothing marked is a no-op. Holds the store lock.
pub fn purge_old_values(ax: &ApplyEnv<'_>, apply_id: &str) -> Result<Purge, CoreError> {
    let fs = ax.fs;
    let state_dir = ax.home.state_dir();
    let _lock = StoreLock::acquire(fs, &state_dir, LOCK_TIMEOUT)?;
    let Some(mut run) = journal::result(fs, &state_dir, apply_id)? else {
        return Err(CoreError::new(
            ErrorCode::NotFound,
            format!("no apply run {apply_id:?} is recorded on this machine"),
        ));
    };
    let mut purge = Purge::default();
    let mut dirs: BTreeSet<PathBuf> = BTreeSet::new();
    for record in run
        .files
        .iter_mut()
        .filter(|record| record.old_values && !record.backup_removed)
    {
        let Some(backup) = record
            .backup
            .as_deref()
            .map(|recorded| rebased(ax.home, Path::new(recorded)))
        else {
            continue;
        };
        let file = PathBuf::from(&record.file);
        if let Err(err) = check_backup_path(ax.home, apply_id, &backup, &file) {
            purge.failed.push((file, err));
            continue;
        }
        let fail = |what: &str, err: std::io::Error| {
            CoreError::new(ErrorCode::IoError, format!("{what}: {err}")).with_path(&backup)
        };
        if fs.is_regular_file(&backup) {
            if let Err(err) = fs.overwrite_with_zeros(&backup) {
                purge
                    .failed
                    .push((file, fail("cannot overwrite backup", err)));
                continue;
            }
            // The old values are gone: from here on the backup is removed,
            // whether or not the zeroed file can be deleted.
            record.backup_removed = true;
            if let Err(err) = fs.remove_file(&backup) {
                purge
                    .not_deleted
                    .push((file.clone(), fail("cannot delete the zeroed backup", err)));
            }
        } else if fs.exists(&backup) || fs.is_dir(&backup) {
            purge.failed.push((
                file,
                CoreError::new(
                    ErrorCode::InvalidValue,
                    "the recorded backup is not a regular file (a link or a folder); left alone",
                )
                .with_path(&backup),
            ));
            continue;
        } else {
            // Gone already (pruned, deleted by hand), or a dangling link,
            // which holds no value either.
            record.backup_removed = true;
        }
        purge.removed.push(file);
        if let Some(dir) = backup.parent() {
            dirs.insert(dir.to_path_buf());
        }
    }
    if !purge.removed.is_empty() {
        run.save_result(fs, &state_dir)?;
    }
    for dir in dirs {
        if fs.read_dir(&dir).is_ok_and(|entries| entries.is_empty()) {
            // Best effort: an empty run directory lists as a run without files.
            let _ = fs.remove_dir_all(&dir);
        }
    }
    Ok(purge)
}

/// The recorded backup path rebuilt under this data folder's backups:
/// `backups/files/<run folder>/<file name>` from its last two components,
/// so a run recorded before the data folder moved (`surject migrate
/// loadout`) still finds its backups. The recorded prefix is never
/// trusted; [`check_backup_path`] checks the rest.
fn rebased(home: &SurjectionHome, recorded: &Path) -> PathBuf {
    let name = recorded.file_name();
    let run = recorded.parent().and_then(Path::file_name);
    match (run, name) {
        (Some(run), Some(name)) => home.backups_dir().join(FILES_DIR).join(run).join(name),
        _ => recorded.to_path_buf(),
    }
}

/// Whether the recorded `backup` is run `apply_id`'s backup of `file`:
/// `backups/files/<ts>-<apply-id>/<encode_path(file)>`, no `..`. The run
/// result is a file on disk, so it is never trusted to name what to
/// overwrite.
fn check_backup_path(
    home: &SurjectionHome,
    apply_id: &str,
    backup: &Path,
    file: &Path,
) -> Result<(), CoreError> {
    let root = home.backups_dir().join(FILES_DIR);
    let suffix = format!("-{apply_id}");
    let in_run = backup.parent().is_some_and(|dir| {
        dir.parent() == Some(root.as_path())
            && dir
                .file_name()
                .and_then(|name| name.to_str())
                .is_some_and(|name| {
                    name.get(..16).and_then(parse_timestamp).is_some()
                        && name.get(16..) == Some(suffix.as_str())
                })
    });
    let plain = backup
        .components()
        .all(|part| !matches!(part, std::path::Component::ParentDir));
    let named =
        backup.file_name().and_then(|name| name.to_str()) == Some(encode_path(file).as_str());
    if in_run && plain && named {
        Ok(())
    } else {
        Err(CoreError::new(
            ErrorCode::InvalidValue,
            format!(
                "the recorded backup is not apply {apply_id}'s backup of this file; left alone"
            ),
        )
        .with_path(backup))
    }
}

/// The client files whose backup of apply run `apply_id` was removed
/// because it held old secret values ([`purge_old_values`]): their content
/// from before that run is no longer kept.
pub fn removed_backups(
    fs: &dyn Fs,
    state_dir: &Path,
    apply_id: &str,
) -> Result<Vec<PathBuf>, CoreError> {
    Ok(journal::result(fs, state_dir, apply_id)?
        .map(|run| {
            run.files
                .into_iter()
                .filter(|record| record.backup_removed)
                .map(|record| PathBuf::from(record.file))
                .collect()
        })
        .unwrap_or_default())
}

#[cfg(test)]
mod rebase_tests {
    use std::path::{Path, PathBuf};

    use super::{FILES_DIR, rebased};
    use crate::paths::{HomeSource, SurjectionHome};

    /// A backup recorded under Loadout's old data folder is looked for under
    /// this one's backups, and a recorded prefix is never trusted.
    #[test]
    fn recorded_backups_are_rebuilt_under_this_data_folder() {
        let root = std::env::temp_dir().join("surjection-rebase");
        let home = SurjectionHome {
            path: root.join("Surjection"),
            source: HomeSource::Default,
            sync_root: None,
        };
        let run = "20260101T000000Z-01M8WVNEZ659GZ5Q8B30FY2344";
        let recorded: PathBuf = root
            .join("Loadout")
            .join("backups")
            .join(FILES_DIR)
            .join(run)
            .join("file.json");
        assert_eq!(
            rebased(&home, &recorded),
            home.backups_dir()
                .join(FILES_DIR)
                .join(run)
                .join("file.json")
        );
        let elsewhere = Path::new("file.json");
        assert_eq!(rebased(&home, elsewhere), elsewhere);
    }
}