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
//! The pending-plan cache and the scoped instant apply (#15): a cached plan
//! is served only while nothing it was planned from changed, and a plan
//! scoped to some servers equals the whole plan on the targets it covers
//! and leaves no work outside them.

mod support;

use std::io;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, mpsc};
use std::time::Duration;

use support::{Fx, stdio};
use surjection_core::Fs;
use surjection_core::apply::cache::{CachedPlan, PlanCache};
use surjection_core::apply::{
    self, ApplyEnv, ApplyRequest, Plan, PlanOptions, ScopeFilter, Touched,
};
use surjection_core::model::applied::ApplyTrigger;
use surjection_core::store::Store;

fn cached(fx: &Fx, cache: &PlanCache) -> Arc<CachedPlan> {
    cache
        .plan(&fx.ax(), &|fs| {
            Store::open(&fx.env, fs, &fx.home, "surjection-test/0")
        })
        .unwrap()
}

/// Debug text of each target, by file and container.
fn targets(plan: &Plan) -> Vec<(String, String)> {
    plan.targets
        .iter()
        .map(|target| {
            (
                format!("{}#{}", target.file.display(), target.pointer),
                format!("{target:?}"),
            )
        })
        .collect()
}

#[test]
fn a_cached_plan_is_served_until_something_it_read_changes() {
    let fx = Fx::new();
    fx.put_server(stdio("alpha", &["claude-code"], &["a"]));
    let cache = PlanCache::new();

    let first = cached(&fx, &cache);
    let again = cached(&fx, &cache);
    assert!(Arc::ptr_eq(&first, &again), "nothing changed");
    assert_eq!(targets(&first.plan), targets(&fx.plan()));

    // A client edits its file: the next plan sees the foreign entry.
    fx.write(
        &fx.claude(),
        "{\n  \"mcpServers\": {\n    \"theirs\": { \"command\": \"x\" }\n  }\n}\n",
    );
    let edited = cached(&fx, &cache);
    assert!(!Arc::ptr_eq(&first, &edited));
    assert!(
        edited.plan.targets[0]
            .entries
            .iter()
            .any(|entry| entry.name == "theirs")
    );
    assert_eq!(targets(&edited.plan), targets(&fx.plan()));

    // A store commit.
    fx.put_server(stdio("beta", &["claude-code"], &["b"]));
    let committed = cached(&fx, &cache);
    assert!(!Arc::ptr_eq(&edited, &committed));
    assert!(committed.desired().servers.contains_key("beta"));
    assert_eq!(targets(&committed.plan), targets(&fx.plan()));

    // An apply changes the client file and the ledger.
    fx.apply();
    let applied = cached(&fx, &cache);
    assert!(!Arc::ptr_eq(&committed, &applied));
    assert_eq!(targets(&applied.plan), targets(&fx.plan()));
    assert!(!applied.plan.has_work());

    // The drift watcher's invalidation.
    cache.invalidate();
    let invalidated = cached(&fx, &cache);
    assert!(!Arc::ptr_eq(&applied, &invalidated));
    assert!(Arc::ptr_eq(&invalidated, &cached(&fx, &cache)));
}

#[test]
fn a_trust_change_makes_the_cached_plan_stale() {
    let fx = Fx::new();
    fx.put_server(stdio("alpha", &["claude-code"], &["a"]));
    let root = fx.th.projects().join("app");
    fx.manifest(
        &root,
        "project.toml",
        "schema = 1\nid = \"app-1234\"\nname = \"app\"\n\n[defaults]\nclients = [\"claude-code\"]\n\n[servers.local]\ntransport = \"stdio\"\ncommand = \"local-mcp\"\n",
    );
    fx.know_projects(&[("app-1234", &root)]);
    let cache = PlanCache::new();
    let blocked = cached(&fx, &cache);
    assert!(
        blocked
            .plan
            .targets
            .iter()
            .any(|target| target.project_id.is_some() && target.problem.is_some()),
        "the project waits for trust"
    );

    fx.trust(&root);
    let trusted = cached(&fx, &cache);
    assert!(!Arc::ptr_eq(&blocked, &trusted));
    assert!(
        trusted
            .plan
            .targets
            .iter()
            .filter(|target| target.project_id.is_some())
            .all(|target| target.problem.is_none())
    );
    assert_eq!(targets(&trusted.plan), targets(&fx.plan()));
}

/// Client preferences that force Claude Code, omp and Codex on (so
/// `all-detected` selects them) and disable natively in omp.
const PREFS: &str = "schema = 1\n\n[claude-code]\nenabled = true\n\n[codex]\nenabled = true\n\n\
                     [omp]\nenabled = true\nnative_disable = true\n";

/// A known, trusted project with entries derived from store servers under
/// other names (`gh` from `server-0`, `docs` from `server-1`), a
/// suppression of `server-2` and a project-only server; its clients are
/// `all-detected`, so a transport a client cannot run drops it.
fn project(fx: &Fx) {
    let root = fx.th.projects().join("proj");
    fx.manifest(
        &root,
        "project.toml",
        "schema = 1\nid = \"proj-5678\"\nname = \"proj\"\n\n\
         [servers.gh]\nfrom = \"server-0\"\n\n\
         [servers.docs]\nfrom = \"server-1\"\nargs = [\"--docs\"]\n\n\
         [servers.server-2]\nenabled = false\n\n\
         [servers.local]\ntransport = \"stdio\"\ncommand = \"local-mcp\"\n",
    );
    fx.know_projects(&[("proj-5678", &root)]);
    fx.trust(&root);
}

/// `server` as an SSE server: Codex cannot run it.
fn sse(id: &str, clients: &[&str]) -> surjection_core::model::server::Server {
    let mut server = stdio(id, clients, &[]);
    server.body.transport = Some(surjection_core::model::server::Transport::Sse);
    server.body.command = None;
    server.body.args = None;
    server.body.url = Some(format!("https://{id}.example.test/sse"));
    server
}

#[test]
fn scope_parts_equal_scoped_plans() {
    let fx = Fx::new();
    fx.edit("prefs", |tx| tx.write("clients.toml", PREFS.to_owned()));
    for n in 0..3 {
        fx.put_server(stdio(
            &format!("server-{n}"),
            &["claude-code", "omp", "codex"],
            &["a"],
        ));
    }
    project(&fx);
    fx.apply();
    fx.put_server(sse("server-0", &["claude-code", "omp"]));
    let full = fx.plan();
    assert!(
        full.targets
            .iter()
            .any(|target| target.project_id.is_some())
    );
    for scope in [ScopeFilter::System, ScopeFilter::Project, ScopeFilter::All] {
        let alone = fx.plan_with(&PlanOptions {
            scope,
            ..PlanOptions::default()
        });
        let part = full.scope_part(scope);
        assert_eq!(targets(&part), targets(&alone), "{scope:?}");
        assert_eq!(
            format!("{:?}", part.warnings),
            format!("{:?}", alone.warnings)
        );
    }
}

/// A small deterministic generator, so the cases differ but repeat.
struct Lcg(u64);

impl Lcg {
    fn next(&mut self, below: usize) -> usize {
        self.0 = self
            .0
            .wrapping_mul(6_364_136_223_846_793_005)
            .wrapping_add(1_442_695_040_888_963_407);
        ((self.0 >> 33) as usize) % below
    }
}

#[test]
fn a_server_scoped_plan_is_the_whole_plan_on_the_targets_it_covers() {
    const CLIENTS: [&str; 3] = ["claude-code", "omp", "codex"];
    let mut rng = Lcg(14);
    for case in 0..8 {
        let fx = Fx::new();
        fx.edit("prefs", |tx| tx.write("clients.toml", PREFS.to_owned()));
        let ids: Vec<String> = (0..5).map(|n| format!("server-{n}")).collect();
        let pick = |rng: &mut Lcg| -> Vec<&'static str> {
            CLIENTS
                .iter()
                .copied()
                .filter(|_| rng.next(2) == 0)
                .collect()
        };
        for id in &ids {
            let clients = pick(&mut rng);
            fx.put_server(stdio(id, &clients, &["--v1"]));
        }
        project(&fx);
        fx.apply();
        // Change one server: its clients, its arguments, its transport (SSE,
        // which Codex cannot run), or remove it. The first case turns the
        // server `gh` starts from into SSE.
        let (changed, how) = if case == 0 {
            ("server-0".to_owned(), 2)
        } else {
            (ids[rng.next(ids.len())].clone(), rng.next(4))
        };
        match how {
            0 => fx.put_server(stdio(&changed, &pick(&mut rng), &["--v1"])),
            1 => fx.put_server(stdio(&changed, &pick(&mut rng), &["--v2"])),
            2 => fx.put_server(sse(&changed, &pick(&mut rng))),
            _ => fx.remove_server(&changed),
        };
        let full = fx.plan();
        let scoped = fx.plan_with(&Touched::Servers(vec![changed.clone()]).options());
        let full_targets = targets(&full);
        for (key, debug) in targets(&scoped) {
            assert!(
                full_targets.contains(&(key.clone(), debug)),
                "case {case}: {key} differs from the whole plan"
            );
        }
        let keys: Vec<String> = targets(&scoped).into_iter().map(|(key, _)| key).collect();
        for (target, (key, _)) in full.targets.iter().zip(&full_targets) {
            assert!(
                keys.contains(key) || !target.has_work(),
                "case {case}: {key} has work outside the scope of {changed}"
            );
        }
    }
}

/// What runs before the first read of a file.
type Hook<'a> = Box<dyn FnOnce() + Send + 'a>;

/// The file system with hooks: one run before the first read of a file,
/// and failing replaces of files whose path contains a text.
struct Hooked<'a> {
    inner: &'a dyn Fs,
    before_read: Mutex<Option<(PathBuf, Hook<'a>)>>,
    /// (path text, replaces of such paths that still succeed)
    fail_replace: Mutex<Option<(String, usize)>>,
}

impl<'a> Hooked<'a> {
    fn new(inner: &'a dyn Fs) -> Self {
        Self {
            inner,
            before_read: Mutex::new(None),
            fail_replace: Mutex::new(None),
        }
    }

    fn hook(&self, path: &Path) {
        let mut slot = self.before_read.lock().unwrap();
        if slot.as_ref().is_some_and(|(at, _)| at == path) {
            let (_, hook) = slot.take().unwrap();
            drop(slot);
            hook();
        }
    }
}

impl Fs for Hooked<'_> {
    fn root(&self) -> Option<&Path> {
        self.inner.root()
    }
    fn exists(&self, path: &Path) -> bool {
        self.inner.exists(path)
    }
    fn is_dir(&self, path: &Path) -> bool {
        self.inner.is_dir(path)
    }
    fn read_to_string(&self, path: &Path) -> io::Result<String> {
        self.hook(path);
        self.inner.read_to_string(path)
    }
    fn read(&self, path: &Path) -> io::Result<Vec<u8>> {
        self.hook(path);
        self.inner.read(path)
    }
    fn write(&self, path: &Path, contents: &[u8]) -> io::Result<()> {
        self.inner.write(path, contents)
    }
    fn write_new(&self, path: &Path, contents: &[u8], like: Option<&Path>) -> io::Result<()> {
        self.inner.write_new(path, contents, like)
    }
    fn replace(&self, from: &Path, to: &Path) -> io::Result<()> {
        if let Some((text, left)) = self.fail_replace.lock().unwrap().as_mut()
            && to.to_string_lossy().contains(text.as_str())
        {
            if *left == 0 {
                return Err(io::Error::other("injected failure"));
            }
            *left -= 1;
        }
        self.inner.replace(from, to)
    }
    fn write_private(&self, path: &Path, contents: &[u8]) -> io::Result<()> {
        self.inner.write_private(path, contents)
    }
    fn create_dir_all(&self, path: &Path) -> io::Result<()> {
        self.inner.create_dir_all(path)
    }
    fn create_private_dir_all(&self, path: &Path) -> io::Result<()> {
        self.inner.create_private_dir_all(path)
    }
    fn remove_file(&self, path: &Path) -> io::Result<()> {
        self.inner.remove_file(path)
    }
    fn is_regular_file(&self, path: &Path) -> bool {
        self.inner.is_regular_file(path)
    }
    fn overwrite_with_zeros(&self, path: &Path) -> io::Result<()> {
        self.inner.overwrite_with_zeros(path)
    }
    fn remove_dir_all(&self, path: &Path) -> io::Result<()> {
        self.inner.remove_dir_all(path)
    }
    fn read_dir(&self, path: &Path) -> io::Result<Vec<PathBuf>> {
        self.inner.read_dir(path)
    }
    fn open_lock_file(&self, path: &Path) -> io::Result<std::fs::File> {
        self.inner.open_lock_file(path)
    }
    fn canonicalize(&self, path: &Path) -> io::Result<PathBuf> {
        self.inner.canonicalize(path)
    }
}

/// Review M30-1: a commit lands while a plan is computed (after it read the
/// store); a request that arrives after the commit waits for that plan, and
/// must not get it.
#[test]
fn a_request_after_a_commit_never_gets_the_plan_started_before_it() {
    let fx = Fx::new();
    fx.put_server(stdio("alpha", &["claude-code"], &["a"]));
    fx.write(&fx.claude(), "{}\n");
    let cache = PlanCache::new();
    let hooked = Hooked::new(&fx.fs);
    let (committed, arrived) = mpsc::channel::<()>();
    let fx_ref = &fx;
    *hooked.before_read.lock().unwrap() = Some((
        fx.claude(),
        Box::new(move || {
            fx_ref.put_server(stdio("beta", &["claude-code"], &["b"]));
            committed.send(()).unwrap();
            // Long enough for the later request to arrive and wait.
            std::thread::sleep(Duration::from_millis(500));
        }),
    ));
    std::thread::scope(|scope| {
        let early = scope.spawn(|| {
            let ax = ApplyEnv {
                fs: &hooked,
                ..fx.ax()
            };
            cache
                .plan(&ax, &|fs| {
                    Store::open(&fx.env, fs, &fx.home, "surjection-test/0")
                })
                .unwrap()
        });
        arrived.recv().unwrap();
        let late = cached(&fx, &cache);
        let early = early.join().unwrap();
        assert!(!early.desired().servers.contains_key("beta"));
        assert!(
            late.desired().servers.contains_key("beta"),
            "the request after the commit got the plan started before it"
        );
    });
}

/// Review M30-2: the journal cannot be written after the first file was:
/// what that file got stays owned, so the next apply cleans it up.
#[test]
fn a_run_stopped_by_the_journal_keeps_what_it_wrote_owned() {
    let fx = Fx::new();
    fx.put_server(stdio("alpha", &["claude-code"], &["a"]));
    let failing = Hooked::new(&fx.fs);
    // The journal's start is written; its first record is not.
    *failing.fail_replace.lock().unwrap() = Some(("journal".to_owned(), 1));
    let ax = ApplyEnv {
        fs: &failing,
        ..fx.ax()
    };
    let request = ApplyRequest {
        options: PlanOptions::default(),
        trigger: ApplyTrigger::Review,
    };
    assert!(apply::run(&fx.store(), &ax, &request).is_err());
    assert!(fx.read(&fx.claude()).contains("\"alpha\""));
    assert!(
        fx.ledger().entries.iter().any(|row| row.key == "alpha"),
        "the written entry is owned"
    );

    fx.remove_server("alpha");
    fx.apply();
    assert!(!fx.read(&fx.claude()).contains("\"alpha\""));
}