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
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
610
611
612
613
614
615
616
617
618
619
//! `Store::transaction` (SPEC 4.1, 9.2): every core mutation stages its
//! file changes, then validates, writes and commits exactly once under the
//! cross-process lock.

use std::collections::BTreeMap;
use std::path::Path;

use git2::{Commit, IndexEntry, IndexTime, Oid, Tree};

use crate::error::{CoreError, ErrorCode};
use crate::model::server::Server;
use crate::model::{StoreDocument, StoreFile, TomlDocument, validate_store_file};
use crate::secrets::scanner::{ALLOWLIST_FILE, Allowlist, leak_error, scan_text};
use crate::store::lock::{LOCK_TIMEOUT, StoreLock};
use crate::store::trailers::{ApplyRef, MachineRef, Scope, Trailers, compose_message};
use crate::store::{
    CommitId, Machine, Store, StoreRead, check_relative, git_error, io_error, server_path,
};

/// What a transaction does, for its commit message and trailers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Operation {
    op: String,
    summary: String,
    entities: Vec<String>,
    scope: Scope,
    apply: Option<ApplyRef>,
}

impl Operation {
    /// `op` is the `Surjection-Op` value (`server.add`); `summary` the commit
    /// subject (`server: add github`). The scope defaults to `system`.
    pub fn new(op: impl Into<String>, summary: impl Into<String>) -> Self {
        Self {
            op: op.into(),
            summary: summary.into(),
            entities: Vec::new(),
            scope: Scope::System,
            apply: None,
        }
    }

    /// Adds a `Surjection-Entity` (`server/github`). A bulk change adds one per
    /// entity.
    pub fn entity(mut self, entity: impl Into<String>) -> Self {
        self.entities.push(entity.into());
        self
    }

    pub fn scope(mut self, scope: Scope) -> Self {
        self.scope = scope;
        self
    }

    fn check(&self) -> Result<(), CoreError> {
        let op_ok = !self.op.is_empty()
            && self
                .op
                .bytes()
                .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b"._-".contains(&b));
        if !op_ok {
            return Err(CoreError::new(
                ErrorCode::InvalidValue,
                format!("invalid operation name {:?}", self.op),
            ));
        }
        if self.summary.trim().is_empty() {
            return Err(CoreError::new(
                ErrorCode::InvalidValue,
                "a commit summary must not be empty",
            ));
        }
        Ok(())
    }

    fn trailers(&self, machine: &Machine, client: &str) -> Trailers {
        Trailers {
            op: Some(self.op.clone()),
            entities: self.entities.clone(),
            scope: Some(self.scope.clone()),
            machine: Some(MachineRef {
                id: machine.id.clone(),
                label: machine.label.clone(),
            }),
            client: Some(client.to_owned()),
            apply: self.apply.clone(),
            other: Vec::new(),
        }
    }
}

/// Result of [`Store::transaction`].
#[derive(Debug)]
pub struct Committed<T> {
    /// What the closure returned.
    pub value: T,
    /// The new commit; `None` when the staged changes left the tree as it was.
    pub commit: Option<CommitId>,
}

/// Staged changes, keyed by store-relative path; `None` removes the file.
pub(crate) type Changes = BTreeMap<String, Option<Vec<u8>>>;

/// Changes staged inside [`Store::transaction`]. Reads see staged changes.
pub struct Transaction<'a> {
    store: &'a Store<'a>,
    machine: Machine,
    operation: Operation,
    changes: Changes,
}

impl Transaction<'_> {
    /// The store this transaction commits to.
    pub fn store(&self) -> &Store<'_> {
        self.store
    }

    /// The machine making the change (the commit author).
    pub fn machine(&self) -> &Machine {
        &self.machine
    }

    /// Replaces the commit subject (an apply run names its files only once
    /// it has run).
    pub fn set_summary(&mut self, summary: impl Into<String>) {
        self.operation.summary = summary.into();
    }

    /// Adds a `Surjection-Entity` trailer, once per entity.
    pub fn add_entity(&mut self, entity: impl Into<String>) {
        let entity = entity.into();
        if !self.operation.entities.contains(&entity) {
            self.operation.entities.push(entity);
        }
    }

    /// Sets the `Surjection-Apply: <apply-id> <outcome>` trailer.
    pub fn set_apply(&mut self, apply: ApplyRef) {
        self.operation.apply = Some(apply);
    }

    /// Whether anything is staged.
    pub fn has_changes(&self) -> bool {
        !self.changes.is_empty()
    }

    /// Validates and leak-scans what is staged so far, as the commit will
    /// (SPEC 5, 11.8), without writing anything. For work inside the
    /// transaction that has effects outside the store (an instant apply).
    pub fn check(&self) -> Result<(), CoreError> {
        self.store.validate_changes(&self.changes)?;
        let message = compose_message(
            &self.operation.summary,
            &self.operation.trailers(&self.machine, &self.store.client),
        );
        self.store.scan_changes(&self.changes, &message)
    }

    /// A store file as this transaction sees it; `None` if absent.
    pub fn read_to_string(&self, relative: &str) -> Result<Option<String>, CoreError> {
        check_relative(relative)?;
        match self.changes.get(relative) {
            Some(Some(bytes)) => String::from_utf8(bytes.clone()).map(Some).map_err(|_| {
                CoreError::new(ErrorCode::InvalidValue, "staged file is not UTF-8")
                    .with_path(relative)
            }),
            Some(None) => Ok(None),
            None => self.store.read_to_string(relative),
        }
    }

    pub fn exists(&self, relative: &str) -> Result<bool, CoreError> {
        check_relative(relative)?;
        match self.changes.get(relative) {
            Some(change) => Ok(change.is_some()),
            None => Ok(self.store.fs.exists(&self.store.path(relative)?)),
        }
    }

    /// Stages `contents` for `relative`.
    pub fn write(&mut self, relative: &str, contents: impl Into<Vec<u8>>) -> Result<(), CoreError> {
        check_relative(relative)?;
        self.changes
            .insert(relative.to_owned(), Some(contents.into()));
        Ok(())
    }

    /// Stages the removal of `relative`.
    pub fn remove(&mut self, relative: &str) -> Result<(), CoreError> {
        check_relative(relative)?;
        self.changes.insert(relative.to_owned(), None);
        Ok(())
    }

    /// Stages a new server; `already_exists` if the id is taken.
    pub fn add_server(&mut self, server: Server) -> Result<(), CoreError> {
        let relative = server_path(&server.id)?;
        if self.exists(&relative)? {
            return Err(CoreError::new(
                ErrorCode::AlreadyExists,
                format!("server {:?} already exists", server.id),
            )
            .with_path(self.store.path(&relative)?));
        }
        let text = TomlDocument::new(server).to_text()?;
        self.write(&relative, text)
    }

    /// Stages the removal of a server; `not_found` if there is none.
    pub fn remove_server(&mut self, id: &str) -> Result<(), CoreError> {
        let relative = server_path(id)?;
        if !self.exists(&relative)? {
            return Err(CoreError::new(
                ErrorCode::NotFound,
                format!("server {id:?} does not exist"),
            ));
        }
        self.remove(&relative)
    }
}

impl StoreRead for Transaction<'_> {
    fn read_file(&self, relative: &str) -> Result<Option<String>, CoreError> {
        self.read_to_string(relative)
    }

    fn list_files(&self, dir: &str) -> Result<Vec<String>, CoreError> {
        let mut names = self.store.list_files(dir)?;
        let prefix = format!("{dir}/");
        for (relative, change) in &self.changes {
            let Some(name) = relative.strip_prefix(&prefix) else {
                continue;
            };
            if name.contains('/') {
                continue;
            }
            match change {
                Some(_) if !names.iter().any(|known| known == name) => names.push(name.to_owned()),
                Some(_) => {}
                None => names.retain(|known| known != name),
            }
        }
        names.sort();
        Ok(names)
    }
}

impl Store<'_> {
    /// Runs one mutation: takes the cross-process lock (waiting up to
    /// [`LOCK_TIMEOUT`]), lets `f` stage changes, validates every staged
    /// store file, runs the leak scanner (SPEC 11.8) over the staged files
    /// and the commit message, writes them to the working tree and creates
    /// exactly one commit with the [`Operation`]'s trailers. When `f`,
    /// validation or the scanner fails nothing is written and nothing is
    /// committed.
    pub fn transaction<'s, T>(
        &'s self,
        operation: Operation,
        f: impl FnOnce(&mut Transaction<'s>) -> Result<T, CoreError>,
    ) -> Result<Committed<T>, CoreError> {
        operation.check()?;
        let _lock = StoreLock::acquire(self.fs, &self.state_dir, LOCK_TIMEOUT)?;
        let machine = Machine::load_or_create(&self.env, self.fs, &self.state_dir)?;
        let mut tx = Transaction {
            store: self,
            machine,
            operation,
            changes: Changes::new(),
        };
        let value = f(&mut tx)?;
        tx.operation.check()?;
        let commit = self.commit_changes(&tx.machine, &tx.operation, tx.changes)?;
        Ok(Committed { value, commit })
    }

    /// Store files edited, added or removed in the working tree since HEAD
    /// (hand edits), store-relative and sorted. A nested repository (shown
    /// by git as a directory) is not a store file and is left out.
    pub fn worktree_changes(&self) -> Result<Vec<String>, CoreError> {
        let mut options = git2::StatusOptions::new();
        options
            .include_untracked(true)
            .recurse_untracked_dirs(true)
            .include_ignored(false)
            .exclude_submodules(true);
        let statuses = self.repo.statuses(Some(&mut options)).map_err(git_error)?;
        let mut files: Vec<String> = statuses
            .iter()
            .filter(|entry| !entry.status().is_ignored())
            .filter_map(|entry| entry.path().ok().map(|path| path.replace('\\', "/")))
            .filter(|path| !path.ends_with('/'))
            .collect();
        files.sort();
        files.dedup();
        Ok(files)
    }

    /// Commits the working tree's hand edits ([`Store::worktree_changes`],
    /// read under the lock) as one commit with their entities as trailers,
    /// after the same validation and leak scan as every transaction.
    /// Returns the commit (`None` when the working tree is clean) and the
    /// files it holds.
    pub fn commit_worktree(
        &self,
        operation: Operation,
    ) -> Result<(Option<CommitId>, Vec<String>), CoreError> {
        operation.check()?;
        let _lock = StoreLock::acquire(self.fs, &self.state_dir, LOCK_TIMEOUT)?;
        let machine = Machine::load_or_create(&self.env, self.fs, &self.state_dir)?;
        let files = self.worktree_changes()?;
        let mut operation = operation;
        for entity in crate::store::diff::trailer_entities(&files) {
            operation = operation.entity(entity);
        }
        let mut changes = Changes::new();
        for relative in &files {
            let path = self.path(relative)?;
            let bytes = if self.fs.exists(&path) {
                Some(
                    self.fs
                        .read(&path)
                        .map_err(|err| io_error("cannot read file", &path, &err))?,
                )
            } else {
                None
            };
            changes.insert(relative.clone(), bytes);
        }
        let commit = self.commit_changes(&machine, &operation, changes)?;
        Ok((commit, files))
    }

    /// Validates, scans, writes and commits `changes`. The caller holds the
    /// lock. The scanner runs before the first blob reaches the object
    /// database.
    pub(crate) fn commit_changes(
        &self,
        machine: &Machine,
        operation: &Operation,
        changes: Changes,
    ) -> Result<Option<CommitId>, CoreError> {
        if changes.is_empty() {
            return Ok(None);
        }
        self.commit_with(machine, operation, changes, None, CommitMode::Normal)
    }

    /// Commits the files a schema migration rewrote (`crate::versions`),
    /// from HEAD's blobs, without touching the working tree: the caller
    /// writes it after the commit, so a crash in between leaves old files
    /// the next run upgrades. The caller holds the lock. `validate` is false
    /// only when the files were upgraded past the version this build reads
    /// (the test hook's migrations); the leak scanner always runs.
    pub(crate) fn commit_migration(
        &self,
        operation: &Operation,
        files: crate::store::merge::Files,
        validate: bool,
    ) -> Result<Option<CommitId>, CoreError> {
        let machine = Machine::load_or_create(&self.env, self.fs, &self.state_dir)?;
        let changes: Changes = files
            .into_iter()
            .map(|(relative, bytes)| (relative, Some(bytes)))
            .collect();
        if changes.is_empty() {
            return Ok(None);
        }
        operation.check()?;
        self.commit_with(
            &machine,
            operation,
            changes,
            None,
            CommitMode::Migration { validate },
        )
    }

    /// A merge commit of HEAD and `merged` with `changes` over HEAD's tree
    /// (SPEC 9.9 step 4): validated, scanned, and made even when the tree
    /// equals HEAD's, so the incoming history is recorded. The caller
    /// holds the lock.
    pub(crate) fn commit_merge(
        &self,
        machine: &Machine,
        operation: &Operation,
        changes: Changes,
        merged: Oid,
    ) -> Result<CommitId, CoreError> {
        let merged = self.repo.find_commit(merged).map_err(git_error)?;
        self.commit_with(
            machine,
            operation,
            changes,
            Some(&merged),
            CommitMode::Normal,
        )?
        .ok_or_else(|| CoreError::new(ErrorCode::GitError, "the merge commit was not made"))
    }

    fn commit_with(
        &self,
        machine: &Machine,
        operation: &Operation,
        changes: Changes,
        merged: Option<&Commit<'_>>,
        mode: CommitMode,
    ) -> Result<Option<CommitId>, CoreError> {
        if !matches!(mode, CommitMode::Migration { validate: false }) {
            self.validate_changes(&changes)?;
        }
        let message = compose_message(
            &operation.summary,
            &operation.trailers(machine, &self.client),
        );
        self.scan_changes(&changes, &message)?;

        let parent = match self.repo.head() {
            Ok(head) => Some(head.peel_to_commit().map_err(git_error)?),
            Err(_) => None,
        };
        // The new tree is HEAD's tree plus exactly the staged changes, built
        // from the bytes in memory: no worktree scan, no filters.
        let mut index = self.repo.index().map_err(git_error)?;
        match &parent {
            Some(parent) => index.read_tree(&parent.tree().map_err(git_error)?),
            None => index.clear(),
        }
        .map_err(git_error)?;
        for (relative, change) in &changes {
            match change {
                Some(bytes) => index.add_frombuffer(&index_entry(relative), bytes),
                None if index.get_path(Path::new(relative), 0).is_some() => {
                    index.remove_path(Path::new(relative))
                }
                None => Ok(()),
            }
            .map_err(git_error)?;
        }
        let tree_id = index.write_tree().map_err(git_error)?;

        if mode == CommitMode::Normal {
            self.write_worktree(&changes)?;
        }
        if merged.is_none()
            && parent
                .as_ref()
                .is_some_and(|parent| parent.tree_id() == tree_id)
        {
            return Ok(None);
        }

        let tree = self.repo.find_tree(tree_id).map_err(git_error)?;
        let oid = self.commit(machine, &message, &tree, parent.as_ref(), merged)?;
        index.write().map_err(git_error)?;
        Ok(Some(CommitId::from_oid(oid)))
    }

    /// The leak scanner over every staged file and the commit message.
    fn scan_changes(&self, changes: &Changes, message: &str) -> Result<(), CoreError> {
        let allow_text = match changes.get(ALLOWLIST_FILE) {
            Some(Some(bytes)) => Some(String::from_utf8_lossy(bytes).into_owned()),
            Some(None) => None,
            None => self.read_to_string(ALLOWLIST_FILE)?,
        };
        let allow = Allowlist::parse(allow_text.as_deref().unwrap_or_default());
        let mut findings = Vec::new();
        for (relative, change) in changes {
            if let Some(bytes) = change {
                let toml = relative.ends_with(".toml");
                findings.extend(scan_text(relative, bytes, toml, &self.secrets, &allow));
            }
        }
        findings.extend(scan_text(
            "commit message",
            message.as_bytes(),
            false,
            &self.secrets,
            &allow,
        ));
        match leak_error(&findings) {
            Some(err) => Err(err),
            None => Ok(()),
        }
    }

    /// Schema validation of every staged store file (SPEC 5); the first
    /// error aborts. Warnings pass.
    fn validate_changes(&self, changes: &Changes) -> Result<(), CoreError> {
        for (relative, change) in changes {
            let Some(bytes) = change else {
                continue;
            };
            let Some(file) = StoreFile::classify(Path::new(relative)) else {
                continue;
            };
            let path = self.path(relative)?;
            let text = std::str::from_utf8(bytes).map_err(|_| {
                CoreError::new(ErrorCode::InvalidValue, "store files must be UTF-8")
                    .with_path(&path)
            })?;
            let shared_id = match &file {
                StoreFile::Project { id, local: true } => {
                    self.shared_project_id(changes, &format!("projects/{id}/project.toml"))?
                }
                _ => None,
            };
            if let Some(err) =
                validate_store_file(&file, text, shared_id.as_deref()).first_error(Some(&path))
            {
                return Err(err);
            }
        }
        Ok(())
    }

    /// The id in the `project.toml` next to a staged `project.local.toml`.
    fn shared_project_id(
        &self,
        changes: &Changes,
        relative: &str,
    ) -> Result<Option<String>, CoreError> {
        let text = match changes.get(relative) {
            Some(Some(bytes)) => Some(String::from_utf8_lossy(bytes).into_owned()),
            Some(None) => None,
            None => self.read_to_string(relative)?,
        };
        let file = StoreFile::Project {
            id: String::new(),
            local: false,
        };
        Ok(text
            .and_then(|text| StoreDocument::parse(&file, &text).ok())
            .and_then(|doc| match doc {
                StoreDocument::Project(doc) => doc.into_value().id,
                _ => None,
            }))
    }

    pub(crate) fn write_worktree(&self, changes: &Changes) -> Result<(), CoreError> {
        for (relative, change) in changes {
            let path = self.path(relative)?;
            match change {
                Some(bytes) => {
                    if let Some(dir) = path.parent() {
                        self.fs
                            .create_dir_all(dir)
                            .map_err(|err| io_error("cannot create directory", dir, &err))?;
                    }
                    self.fs
                        .write(&path, bytes)
                        .map_err(|err| io_error("cannot write file", &path, &err))?;
                }
                None if self.fs.exists(&path) => self
                    .fs
                    .remove_file(&path)
                    .map_err(|err| io_error("cannot remove file", &path, &err))?,
                None => {}
            }
        }
        Ok(())
    }

    /// Commits `tree` on HEAD (plus `merged`, for a merge commit) with the
    /// machine's signature. libgit2 signs the reflog entries of a commit
    /// with its committer, so no identity from git configuration reaches
    /// `.git/logs` either.
    fn commit(
        &self,
        machine: &Machine,
        message: &str,
        tree: &Tree<'_>,
        parent: Option<&Commit<'_>>,
        merged: Option<&Commit<'_>>,
    ) -> Result<Oid, CoreError> {
        let signature = machine.signature(self.env.now())?;
        let parents: Vec<&Commit<'_>> = parent.into_iter().chain(merged).collect();
        let oid = self
            .repo
            .commit(
                Some("HEAD"),
                &signature,
                &signature,
                message,
                tree,
                &parents,
            )
            .map_err(git_error)?;
        let id = oid.to_string();
        crate::diagnostics::log::event("store.commit", &[("commit", &&id[..id.len().min(7)])]);
        Ok(oid)
    }
}

fn index_entry(relative: &str) -> IndexEntry {
    IndexEntry {
        ctime: IndexTime::new(0, 0),
        mtime: IndexTime::new(0, 0),
        dev: 0,
        ino: 0,
        mode: 0o100_644,
        uid: 0,
        gid: 0,
        file_size: 0,
        id: Oid::ZERO_SHA1,
        flags: 0,
        flags_extended: 0,
        path: relative.as_bytes().to_vec(),
    }
}

/// How [`Store::commit_with`] treats the staged files.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CommitMode {
    /// Validated, written to the working tree, committed.
    Normal,
    /// A schema migration: committed only; validated unless the files are
    /// past the version this build reads.
    Migration { validate: bool },
}