workspace-mgr 0.8.1

Fixed-policy repository workspace manager for coding agents
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
use std::fs;
use std::path::Path;

use semver::Version;
use serde::Serialize;

use crate::config::{
    CONFIG_NAME, Config, cli_version_satisfies, declared_minimum_cli_version,
    installed_cli_version, minimum_cli_version_at,
};
use crate::error::Result;
use crate::git::GitRepo;
use crate::path::reject_symlink_traversal;
use crate::process::command_exists;
use crate::scaffold;
use crate::storage_metadata;

#[derive(Debug, Clone, Serialize)]
pub struct DoctorReport {
    pub status: String,
    pub repo: String,
    pub cli_version: String,
    pub checks: Vec<DoctorCheck>,
}

#[derive(Debug, Clone, Serialize)]
pub struct DoctorCheck {
    pub name: String,
    pub status: String,
    pub detail: String,
}

impl DoctorReport {
    pub fn healthy(&self) -> bool {
        self.checks.iter().all(|check| check.status == "ok")
    }
}

pub fn inspect(path: &Path) -> Result<DoctorReport> {
    let repo = GitRepo::discover(path)?;
    let mut checks = Vec::new();
    let mut repository_runtime = command_check("git", true);
    repository_runtime.name = "repository-runtime".to_owned();
    checks.push(repository_runtime);

    let config_path = repo.root.join(CONFIG_NAME);
    // A repository that requires a newer CLI is reported below instead of
    // stopping the diagnosis.
    let config = match Config::load_compatible_ignoring_cli_requirement(&repo) {
        Ok(config) => {
            checks.push(DoctorCheck {
                name: "repository-config".to_owned(),
                status: "ok".to_owned(),
                detail: config_path.display().to_string(),
            });
            Some(config)
        }
        Err(error) => {
            checks.push(DoctorCheck {
                name: "repository-config".to_owned(),
                status: "error".to_owned(),
                detail: error.to_string(),
            });
            None
        }
    };
    if let Some(check) = cli_version_check(&repo, config.as_ref(), &installed_cli_version()) {
        checks.push(check);
    }

    if let Some(config) = &config {
        checks.push(match scaffold::validate_owned_files(&repo, config) {
            Ok(()) => DoctorCheck {
                name: "repository-scaffold".to_owned(),
                status: "ok".to_owned(),
                detail: "product-owned files match the installed CLI".to_owned(),
            },
            Err(error) => DoctorCheck {
                name: "repository-scaffold".to_owned(),
                status: "error".to_owned(),
                detail: error.to_string(),
            },
        });
        let head = repo
            .current_branch()?
            .unwrap_or_else(|| "detached".to_owned());
        let expected = &config.git.branch;
        let branch_ok = head == *expected;
        checks.push(DoctorCheck {
            name: "checkout-branch".to_owned(),
            status: if branch_ok { "ok" } else { "error" }.to_owned(),
            detail: format!("current {head}, configured shared branch {expected}"),
        });
        let identity = repo.run_unchecked(["var", "GIT_AUTHOR_IDENT"])?;
        checks.push(DoctorCheck {
            name: "publication-identity".to_owned(),
            status: if identity.success() { "ok" } else { "error" }.to_owned(),
            detail: if identity.success() {
                "publication author name and email are configured".to_owned()
            } else {
                "publication author name or email is not configured".to_owned()
            },
        });
        let remote_name_ok = repo.validate_remote_name(&config.git.remote).is_ok();
        let remote = if remote_name_ok {
            repo.run_unchecked(["remote", "get-url", "--", &config.git.remote])?
        } else {
            crate::process::CommandOutput {
                code: 1,
                stdout: String::new(),
                stderr: "invalid configured remote name".to_owned(),
            }
        };
        checks.push(DoctorCheck {
            name: "publication-remote".to_owned(),
            status: if remote.success() { "ok" } else { "error" }.to_owned(),
            detail: if remote.success() {
                format!("configured remote {:?} exists", config.git.remote)
            } else {
                format!("configured remote {:?} does not exist", config.git.remote)
            },
        });

        if config.s3_enabled() {
            checks.push(match storage_metadata::require_runtime(&repo) {
                Ok(version) => DoctorCheck {
                    name: "managed-storage-runtime".to_owned(),
                    status: "ok".to_owned(),
                    detail: format!("internal engine {version}"),
                },
                Err(error) => DoctorCheck {
                    name: "managed-storage-runtime".to_owned(),
                    status: "error".to_owned(),
                    detail: error.to_string(),
                },
            });
            checks.push(match crate::native_s3::load_credentials_config(&repo) {
                Ok(_) => DoctorCheck {
                    name: "managed-storage-config".to_owned(),
                    status: "ok".to_owned(),
                    detail: "repository S3 configuration and optional local credentials are valid"
                        .to_owned(),
                },
                Err(error) => DoctorCheck {
                    name: "managed-storage-config".to_owned(),
                    status: "error".to_owned(),
                    detail: error.to_string(),
                },
            });
            if config.requires_object_versioning() {
                checks.push(
                    match storage_metadata::verify_object_versioning(&repo, config) {
                        Ok(detail) => DoctorCheck {
                            name: "managed-storage-object-versioning".to_owned(),
                            status: "ok".to_owned(),
                            detail: detail.to_string(),
                        },
                        Err(error) => DoctorCheck {
                            name: "managed-storage-object-versioning".to_owned(),
                            status: "error".to_owned(),
                            detail: error.to_string(),
                        },
                    },
                );
            }
            let local = crate::native_s3::credentials_path(&repo)?;
            if local.exists() {
                let relative = crate::native_s3::CREDENTIALS_NAME;
                let primary =
                    GitRepo::discover(local.parent().expect("credentials have a parent"))?;
                let ignored = primary.run_unchecked(["check-ignore", "--quiet", "--", relative])?;
                checks.push(DoctorCheck {
                    name: "managed-storage-local-secrets".to_owned(),
                    status: if ignored.code == 0 { "ok" } else { "error" }.to_owned(),
                    detail: if ignored.code == 0 {
                        format!("{relative} is ignored")
                    } else {
                        format!("{relative} exists but is not ignored")
                    },
                });
            }
        }
    }

    let status = if checks.iter().all(|check| check.status == "ok") {
        "ok"
    } else {
        "error"
    };
    Ok(DoctorReport {
        status: status.to_owned(),
        repo: repo.root.display().to_string(),
        cli_version: env!("CARGO_PKG_VERSION").to_owned(),
        checks,
    })
}

/// Compares this CLI with the repository's `minimum_cli_version`. The
/// declaration is read leniently so a configuration written by a newer
/// release still reports it. The shared branch may already require more than
/// the checkout, so the declaration last fetched from it, at
/// `refs/remotes/<remote>/<branch>`, counts too; doctor itself never fetches.
fn cli_version_check(
    repo: &GitRepo,
    config: Option<&Config>,
    installed: &Version,
) -> Option<DoctorCheck> {
    reject_symlink_traversal(&repo.root, CONFIG_NAME, "repository configuration").ok()?;
    let raw = fs::read_to_string(repo.root.join(CONFIG_NAME)).ok()?;
    let local = declared_minimum_cli_version(&raw);
    let shared = config.and_then(|config| {
        let reference = format!("refs/remotes/{}/{}", config.git.remote, config.git.branch);
        let declared = minimum_cli_version_at(repo, &reference).ok()??;
        Some((
            format!("{}/{}", config.git.remote, config.git.branch),
            declared,
        ))
    });
    let (source, required) = match (local, shared) {
        (Some(local), Some((name, shared))) if shared.cmp_precedence(&local).is_gt() => {
            (name, shared)
        }
        (Some(local), _) => ("repository".to_owned(), local),
        (None, Some((name, shared))) => (name, shared),
        (None, None) => {
            return Some(DoctorCheck {
                name: "cli-version".to_owned(),
                status: "ok".to_owned(),
                detail: format!("installed {installed}, repository declares no minimum version"),
            });
        }
    };
    Some(DoctorCheck {
        name: "cli-version".to_owned(),
        status: if cli_version_satisfies(installed, &required) {
            "ok"
        } else {
            "error"
        }
        .to_owned(),
        detail: format!("installed {installed}, {source} requires {required}"),
    })
}

fn command_check(command: &str, required: bool) -> DoctorCheck {
    let exists = command_exists(command);
    DoctorCheck {
        name: format!("command:{command}"),
        status: if exists || !required { "ok" } else { "error" }.to_owned(),
        detail: if exists {
            which::which(command)
                .map(|path| path.display().to_string())
                .unwrap_or_else(|_| "available".to_owned())
        } else {
            "not found on PATH".to_owned()
        },
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const PLAIN: &str = "[git]\nremote = \"origin\"\nbranch = \"main\"\n";

    struct Fixture {
        _temp: tempfile::TempDir,
        repo: GitRepo,
    }

    impl Fixture {
        fn new(config: Option<&str>) -> Self {
            let temp = tempfile::tempdir().unwrap();
            let repo = GitRepo {
                root: temp.path().to_path_buf(),
            };
            repo.run(["init", "-q", "-b", "main"]).unwrap();
            repo.run(["config", "user.name", "workspace-mgr test"])
                .unwrap();
            repo.run(["config", "user.email", "test@example.invalid"])
                .unwrap();
            if let Some(config) = config {
                fs::write(temp.path().join(CONFIG_NAME), config).unwrap();
            }
            Self { _temp: temp, repo }
        }

        /// Records `config` as the last fetched state of `origin/main`.
        fn fetched(&self, config: &str) {
            let blob = self
                .repo
                .run_bytes(["hash-object", "-w", "--stdin"], Some(config.as_bytes()))
                .unwrap();
            let blob = String::from_utf8_lossy(&blob.stdout).trim().to_owned();
            let tree = self
                .repo
                .run_bytes(
                    ["mktree"],
                    Some(format!("100644 blob {blob}\t{CONFIG_NAME}\n").as_bytes()),
                )
                .unwrap();
            let tree = String::from_utf8_lossy(&tree.stdout).trim().to_owned();
            let commit = self
                .repo
                .run(["commit-tree", &tree, "-m", "fetched"])
                .unwrap()
                .stdout
                .trim()
                .to_owned();
            self.repo
                .run(["update-ref", "refs/remotes/origin/main", &commit])
                .unwrap();
        }

        fn check(&self, installed: &str) -> Option<DoctorCheck> {
            let config = Config::default();
            cli_version_check(
                &self.repo,
                Some(&config),
                &Version::parse(installed).unwrap(),
            )
        }
    }

    fn declaring(version: &str) -> String {
        format!("minimum_cli_version = \"{version}\"\n\n{PLAIN}")
    }

    fn assert_check(check: Option<DoctorCheck>, status: &str, detail: &str) {
        let check = check.unwrap();
        assert_eq!(check.name, "cli-version");
        assert_eq!(
            (check.status.as_str(), check.detail.as_str()),
            (status, detail)
        );
    }

    #[test]
    fn cli_version_check_reports_the_repository_requirement() {
        assert!(Fixture::new(None).check("0.4.0").is_none());
        assert_check(
            Fixture::new(Some(PLAIN)).check("0.4.0"),
            "ok",
            "installed 0.4.0, repository declares no minimum version",
        );
        let met = Fixture::new(Some(&declaring("0.4.0")));
        assert_check(
            met.check("0.4.0"),
            "ok",
            "installed 0.4.0, repository requires 0.4.0",
        );
        // A release candidate meets its own release.
        assert_check(
            met.check("0.4.0-rc.1"),
            "ok",
            "installed 0.4.0-rc.1, repository requires 0.4.0",
        );
        assert_check(
            met.check("0.3.0"),
            "error",
            "installed 0.3.0, repository requires 0.4.0",
        );

        // Unknown fields from a newer release do not hide the requirement.
        let newer = Fixture::new(Some(
            "minimum_cli_version = \"99.0.0\"\n\n[future]\nsetting = true\n",
        ));
        assert_check(
            newer.check("0.4.0"),
            "error",
            "installed 0.4.0, repository requires 99.0.0",
        );
    }

    #[test]
    fn cli_version_check_includes_the_last_fetched_shared_branch() {
        // The checkout is stale: the shared branch was raised after it was
        // last refreshed.
        let stale = Fixture::new(Some(PLAIN));
        stale.fetched(&declaring("99.0.0"));
        assert_check(
            stale.check("0.4.0"),
            "error",
            "installed 0.4.0, origin/main requires 99.0.0",
        );
        stale.fetched(&declaring("0.4.0"));
        assert_check(
            stale.check("0.4.0"),
            "ok",
            "installed 0.4.0, origin/main requires 0.4.0",
        );

        // The higher of both declarations decides.
        let raised = Fixture::new(Some(&declaring("0.5.0")));
        raised.fetched(&declaring("0.4.0"));
        assert_check(
            raised.check("0.4.0"),
            "error",
            "installed 0.4.0, repository requires 0.5.0",
        );
        raised.fetched(&declaring("0.6.0"));
        assert_check(
            raised.check("0.5.0"),
            "error",
            "installed 0.5.0, origin/main requires 0.6.0",
        );
        raised.fetched(PLAIN);
        assert_check(
            raised.check("0.5.0"),
            "ok",
            "installed 0.5.0, repository requires 0.5.0",
        );

        // Without a readable configuration there is no shared branch to read.
        let unreadable = Fixture::new(Some(PLAIN));
        unreadable.fetched(&declaring("99.0.0"));
        assert_check(
            cli_version_check(&unreadable.repo, None, &Version::new(0, 4, 0)),
            "ok",
            "installed 0.4.0, repository declares no minimum version",
        );
    }
}