surjection-core 1.1.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
//! Push, pull and status for every configured remote alike (SPEC 9.6 to
//! 9.9), shared by the CLI and the app.
//!
//! Each push and pull is recorded per remote in the machine-local
//! `state/remote-status.toml` (time, head pushed, last error), so the
//! History page and Settings can show `ahead N`, `behind N` and the last
//! error without reaching the remote (SPEC 9.8). [`status`] reads that
//! record and, for a folder remote, the folder itself: this machine's
//! `machine.json` (what was pushed) and the siblings' heads (what there is
//! to pull). It never fetches from a git remote: `behind` comes from the
//! last fetch.

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

use serde::{Deserialize, Serialize};

use crate::env::Env;
use crate::error::{CoreError, ErrorCode};
use crate::fs::Fs;
use crate::model::SCHEMA_VERSION;
use crate::remote::config::{RemoteConfig, RemoteKind, RemotesFile};
use crate::remote::folder::{self, PullReport, PushOutcome};
use crate::remote::git::{self, GitCli, GitPush, Runner};
use crate::remote::manifest::MachineManifest;
use crate::remote::unc::UncResolver;
use crate::store::diff::{ChangeKind, EntityChange};
use crate::store::{CommitId, Machine, Store, utc_datetime};

/// The record file in `state/`.
pub const STATUS_FILE: &str = "remote-status.toml";
/// Incoming commits counted per sibling machine at most.
const INCOMING_LIMIT: usize = 1000;

/// What pushes and pulls run with.
pub struct SyncEnv<'a> {
    pub unc: &'a dyn UncResolver,
    pub runner: &'a dyn Runner,
    /// The `surject` binary, the credential helper of `auth = token`.
    pub helper: Option<PathBuf>,
    /// Written to a folder remote's `machine.json`.
    pub app_version: &'a str,
}

impl SyncEnv<'_> {
    fn git(&self, store: &Store<'_>) -> Result<GitCli<'_>, CoreError> {
        GitCli::from_env(store.env(), store.fs(), self.runner, self.helper.clone())
    }
}

/// What one push did.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Pushed {
    Folder(PushOutcome),
    Git(GitPush),
}

/// One push: the remote's name and how it went.
pub type PushResult = (String, Result<Pushed, CoreError>);
/// One pull: the remote's name and how it went.
pub type PullResult = (String, Result<PullReport, CoreError>);

/// The error recorded for a remote's last push or pull.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RecordedError {
    pub code: ErrorCode,
    pub message: String,
    /// RFC 3339, UTC.
    pub at: String,
}

/// One remote's record.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct RemoteRecord {
    /// RFC 3339, UTC, of the last successful push.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pushed_at: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pushed_head: Option<String>,
    /// RFC 3339, UTC, of the last successful pull.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pulled_at: Option<String>,
    /// The last push or pull failed with this; cleared by a success.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<RecordedError>,
}

/// `state/remote-status.toml`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StatusFile {
    pub schema: i64,
    #[serde(default, rename = "remote")]
    pub remotes: BTreeMap<String, RemoteRecord>,
    #[serde(flatten)]
    pub extra: toml::Table,
}

impl Default for StatusFile {
    fn default() -> Self {
        Self {
            schema: SCHEMA_VERSION,
            remotes: BTreeMap::new(),
            extra: toml::Table::new(),
        }
    }
}

impl StatusFile {
    /// The records; none when the file is absent or unreadable (it is a
    /// cache of what happened, never needed for work).
    pub fn load(fs: &dyn Fs, state_dir: &Path) -> Self {
        fs.read_to_string(&state_dir.join(STATUS_FILE))
            .ok()
            .and_then(|text| toml::from_str::<Self>(&text).ok())
            .filter(|file| file.schema == SCHEMA_VERSION)
            .unwrap_or_default()
    }

    /// Writes the records. A file a newer Surjection wrote (one running at the
    /// same time as this app, say) is left alone: it is only a cache.
    pub fn save(&self, fs: &dyn Fs, state_dir: &Path) -> Result<(), CoreError> {
        let path = state_dir.join(STATUS_FILE);
        if crate::versions::written_by_newer(
            fs,
            &path,
            crate::versions::Format::Toml,
            SCHEMA_VERSION,
        ) {
            return Ok(());
        }
        let text = toml::to_string_pretty(self).map_err(|err| {
            CoreError::new(
                ErrorCode::InvalidValue,
                format!("cannot write remote status: {err}"),
            )
        })?;
        crate::apply::write_atomic(fs, &path, text.as_bytes())
    }

    /// Records are kept only for configured remotes.
    fn retain(&mut self, remotes: &RemotesFile) {
        self.remotes.retain(|name, _| remotes.get(name).is_some());
    }
}

fn now(env: &Env) -> String {
    utc_datetime(env.now()).to_string()
}

fn record_error(env: &Env, err: &CoreError) -> RecordedError {
    RecordedError {
        code: err.code(),
        message: err.message().to_owned(),
        at: now(env),
    }
}

/// Saves the outcome of pushes and pulls; a failure to save is ignored,
/// the record is informational.
fn save_records(store: &Store<'_>, remotes: &RemotesFile, update: impl FnOnce(&mut StatusFile)) {
    let mut file = StatusFile::load(store.fs(), store.state_dir());
    update(&mut file);
    file.retain(remotes);
    let _ = file.save(store.fs(), store.state_dir());
}

/// Pushes to the selected remotes (every remote when `name` is `None`);
/// one result per remote, each recorded. `progress` gets git's progress
/// lines with the remote's name.
pub fn push_to(
    store: &Store<'_>,
    remotes: &RemotesFile,
    name: Option<&str>,
    sx: &SyncEnv<'_>,
    progress: &mut dyn FnMut(&str, &str),
) -> Result<Vec<PushResult>, CoreError> {
    let results: Vec<PushResult> = remotes
        .select(name)?
        .into_iter()
        .map(|remote| {
            let result = push_one(store, remote, sx, &mut |line| progress(&remote.name, line));
            (remote.name.clone(), result)
        })
        .collect();
    let env = store.env();
    save_records(store, remotes, |file| {
        for (name, result) in &results {
            let record = file.remotes.entry(name.clone()).or_default();
            match result {
                Ok(pushed) => {
                    record.pushed_at = Some(now(env));
                    record.pushed_head = Some(
                        match pushed {
                            Pushed::Folder(outcome) => &outcome.head,
                            Pushed::Git(outcome) => &outcome.head,
                        }
                        .to_string(),
                    );
                    record.error = None;
                }
                Err(err) => record.error = Some(record_error(env, err)),
            }
        }
    });
    Ok(results)
}

fn push_one(
    store: &Store<'_>,
    remote: &RemoteConfig,
    sx: &SyncEnv<'_>,
    progress: &mut dyn FnMut(&str),
) -> Result<Pushed, CoreError> {
    match remote.kind {
        RemoteKind::Folder => {
            folder::push(store, remote, sx.unc, sx.app_version).map(Pushed::Folder)
        }
        RemoteKind::Git => {
            let cli = sx.git(store)?;
            git::push(store, &cli, remote, progress).map(Pushed::Git)
        }
    }
}

/// Pulls from the selected remotes and merges (SPEC 9.9); one result per
/// remote, each recorded. `progress` as for [`push_to`].
pub fn pull_from(
    store: &Store<'_>,
    remotes: &RemotesFile,
    name: Option<&str>,
    sx: &SyncEnv<'_>,
    progress: &mut dyn FnMut(&str, &str),
) -> Result<Vec<PullResult>, CoreError> {
    let mut results = Vec::new();
    for remote in remotes.select(name)? {
        let report = match remote.kind {
            RemoteKind::Folder => folder::pull(store, remote, sx.unc),
            RemoteKind::Git => sx.git(store).and_then(|cli| {
                git::pull(store, &cli, remote, &mut |line| {
                    progress(&remote.name, line)
                })
            }),
        };
        results.push((remote.name.clone(), report));
    }
    let env = store.env();
    save_records(store, remotes, |file| {
        for (name, result) in &results {
            let record = file.remotes.entry(name.clone()).or_default();
            match result {
                Ok(_) => {
                    record.pulled_at = Some(now(env));
                    record.error = None;
                }
                Err(err) => record.error = Some(record_error(env, err)),
            }
        }
    });
    Ok(results)
}

/// Servers and skills a pull changed (SPEC 9.9 step 6).
pub fn changed_by_pull(store: &Store<'_>, report: &PullReport) -> (usize, usize) {
    if report.head_before == report.head_after {
        return (0, 0);
    }
    let changes: Vec<EntityChange> = store
        .semantic_diff(Some(&report.head_before), &report.head_after)
        .unwrap_or_default();
    let mut servers: Vec<&str> = Vec::new();
    let mut skills: Vec<&str> = Vec::new();
    for change in &changes {
        if change.kind == ChangeKind::Changed && change.fields.is_empty() && change.text.is_none() {
            continue;
        }
        let list = if change.entity.starts_with("server/") {
            &mut servers
        } else if change.entity.starts_with("skill/") {
            &mut skills
        } else {
            continue;
        };
        if !list.contains(&change.entity.as_str()) {
            list.push(&change.entity);
        }
    }
    (servers.len(), skills.len())
}

/// A remote as Settings and History show it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RemoteStatus {
    pub name: String,
    /// Folder remotes: whether the folder is there now. Git remotes are not
    /// contacted (`None`).
    pub reachable: Option<bool>,
    /// What this machine pushed last, as far as is known.
    pub pushed_head: Option<CommitId>,
    pub pushed_at: Option<String>,
    pub pulled_at: Option<String>,
    /// Commits of HEAD the remote does not have; `None` when unknown (never
    /// pushed, or the pushed head is not in this store's history).
    pub ahead: Option<usize>,
    /// Commits the remote has that HEAD lacks: other machines' heads in a
    /// folder remote, the last fetch of a git remote. `None` when unknown.
    pub behind: Option<usize>,
    pub last_error: Option<RecordedError>,
}

/// The status of `remote`, without contacting a git remote. `store` is
/// `None` before the store exists.
pub fn status(
    env: &Env,
    fs: &dyn Fs,
    unc: &dyn UncResolver,
    state_dir: &Path,
    store: Option<&Store<'_>>,
    remote: &RemoteConfig,
) -> RemoteStatus {
    let record = StatusFile::load(fs, state_dir)
        .remotes
        .remove(&remote.name)
        .unwrap_or_default();
    let mut status = RemoteStatus {
        name: remote.name.clone(),
        reachable: None,
        pushed_head: record
            .pushed_head
            .as_deref()
            .and_then(|id| CommitId::from_text(id).ok()),
        pushed_at: record.pushed_at,
        pulled_at: record.pulled_at,
        ahead: None,
        behind: None,
        last_error: record.error,
    };
    match remote.kind {
        RemoteKind::Folder => folder_status(env, fs, unc, state_dir, store, remote, &mut status),
        RemoteKind::Git => git_status(store, remote, &mut status),
    }
    status
}

fn folder_status(
    env: &Env,
    fs: &dyn Fs,
    unc: &dyn UncResolver,
    state_dir: &Path,
    store: Option<&Store<'_>>,
    remote: &RemoteConfig,
    status: &mut RemoteStatus,
) {
    let Ok(root) = folder::reachable_root(fs, env.os(), unc, remote) else {
        status.reachable = Some(false);
        return;
    };
    status.reachable = Some(true);
    let Some(store) = store else {
        return;
    };
    let Ok(Some(machine)) = Machine::load(fs, state_dir) else {
        return;
    };
    // What the folder says this machine pushed wins over the record: the
    // folder may have been restored or emptied since.
    let own = MachineManifest::load(fs, &root.join(store.store_id()).join(&machine.id));
    status.pushed_head = own
        .as_ref()
        .and_then(|manifest| manifest.head.as_deref())
        .and_then(|id| CommitId::from_text(id).ok());
    if let Some(manifest) = &own {
        status.pushed_at = Some(manifest.pushed_at.clone());
    }
    status.ahead = ahead(store, status.pushed_head.as_ref());
    let Ok(listing) = folder::list(fs, &root, Some(store.store_id())) else {
        return;
    };
    let mut behind = 0;
    for sibling in &listing.machines {
        if sibling.machine_id == machine.id || !sibling.has_repo(fs) {
            continue;
        }
        let Some(head) = sibling.manifest.head.as_deref() else {
            continue;
        };
        // A sibling still being copied counts as nothing yet.
        behind += store
            .bare_incoming(&sibling.dir.join("store.git"), head, INCOMING_LIMIT)
            .unwrap_or(0);
    }
    status.behind = Some(behind);
}

fn git_status(store: Option<&Store<'_>>, remote: &RemoteConfig, status: &mut RemoteStatus) {
    let Some(store) = store else {
        return;
    };
    let tracking = store
        .reference_target(&format!("refs/remotes/{}/main", remote.name))
        .ok()
        .flatten();
    // What the remote has, as far as is known: what this machine pushed and
    // what the last fetch brought. HEAD's commits reachable from neither
    // are ahead (after a pull that fast-forwarded past the pushed head,
    // nothing is; after a merge pull, the local commits and the merge).
    let known: Vec<CommitId> = [status.pushed_head.clone(), tracking.clone()]
        .into_iter()
        .flatten()
        .collect();
    let head = store.head().ok();
    status.ahead = head
        .as_ref()
        .and_then(|head| store.count_not_in(head, &known).ok().flatten());
    status.behind = match (&tracking, &head) {
        (Some(fetched), Some(head)) => store
            .ahead_behind(head, fetched)
            .ok()
            .map(|(_, behind)| behind),
        _ => status.ahead.map(|_| 0),
    };
}

/// Commits of HEAD not in `upstream`, when `upstream` is in HEAD's history.
fn ahead(store: &Store<'_>, upstream: Option<&CommitId>) -> Option<usize> {
    let upstream = upstream?;
    if !store.contains(upstream) {
        return None;
    }
    let head = store.head().ok()?;
    store
        .ahead_behind(&head, upstream)
        .ok()
        .map(|(ahead, _)| ahead)
}