memstead-git-branch 0.6.0

Mem-repo engine for Memstead — multi-mem, git-backed typed entity graphs. Internal library surface consumed by the memstead binaries — pre-1.0, experimental, no API stability promise.
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
//! Read workspace-level schemas from the unified `__MEMSTEAD` ref.
//!
//! The schema loader delegates to
//! [`crate::storage_memstead::load_schemas_from_memstead_ref`]. All test
//! fixtures and bootstrap paths seed `__MEMSTEAD`;
//! `engine_from_workspace_root` boot promotion covers any
//! pre-existing legacy workspace by running the idempotent migrator
//! at first read.
//!
//! This module remains the [`LoadOutcome`] type owner and the public
//! entry point so call sites keep their import surface stable; the
//! body is now a thin compatibility wrapper.

use std::path::Path;
use std::sync::Arc;

use memstead_schema::{Schema, loader::SchemaLoadError};

/// Errors raised while reading workspace schemas from
/// `mem-repo-git:__MEMSTEAD:schemas/`.
#[derive(Debug, thiserror::Error)]
pub enum MemRepoSchemasError {
    /// `mem-repo/.git/` exists but cannot be opened (corrupt repo, IO
    /// failure under the object database).
    #[error("could not open mem-repo gitdir: {0}")]
    GixOpen(String),
    /// Generic gix-tree read failure (object missing, corrupt tree, IO
    /// underneath the object database). The wrapped message names the
    /// underlying gix error.
    #[error("git tree read error: {0}")]
    GitTree(String),
    /// A schema blob (`schema.yaml` or a type file) is not valid UTF-8.
    /// The schema name + offending file are folded into the message.
    #[error("schema blob {0} is not valid UTF-8: {1}")]
    NotUtf8(String, String),
    /// Parse / validation failure surfaced from the shared schema
    /// loader pipeline. Wraps `SchemaLoadError` so callers can branch
    /// on the inner kind if needed; the schema name is folded in to
    /// disambiguate when multiple schemas are loaded in one pass.
    #[error("schema '{name}': {source}")]
    Schema {
        name: String,
        #[source]
        source: SchemaLoadError,
    },
}

/// Outcome of [`load_schemas_from_ref`] — three discriminated cases
/// that callers branch on so the engine can decide whether to overlay
/// schemas, fall back to disk, or surface an error.
pub enum LoadOutcome {
    /// `mem-repo/.git/` is missing or carries no `__MEMSTEAD` ref — the
    /// workspace is not a real mem-repo (legacy disk-shaped, empty
    /// stub, or pre-migration). Caller falls back to its disk-based
    /// schema source.
    NoMemRepo,
    /// Real mem-repo exists but the `__MEMSTEAD:schemas/` subtree is
    /// absent (or its tree carries no schema directories). No schemas
    /// to overlay; caller may still load a disk-based source if
    /// configured.
    NoSchemas,
    /// Schemas successfully loaded from `__MEMSTEAD:schemas/`.
    Schemas(Vec<Arc<Schema>>),
}

/// Load every workspace-level schema from the mem-repo's unified
/// `__MEMSTEAD:schemas/<name>@<version>/` tree.
///
/// Thin wrapper around
/// [`crate::storage_memstead::load_schemas_from_memstead_ref`].
///
/// `workspace_root` is the directory holding `mem-repo/.git/`.
pub fn load_schemas_from_ref(workspace_root: &Path) -> Result<LoadOutcome, MemRepoSchemasError> {
    crate::storage_memstead::load_schemas_from_memstead_ref(workspace_root)
}

/// The git-branch backend's [`SchemaSource`](memstead_base::schema_source::SchemaSource):
/// schemas live on the `__MEMSTEAD:schemas/<name>@<version>/` ref of the
/// workspace's `mem-repo/.git`. `read_schemas` wraps
/// [`load_schemas_from_ref`]; `write_schema` wraps
/// [`crate::storage_memstead::write_schema_to_memstead_ref`]. The engine
/// owns mem-repo state, so this type is constructed and used inside the
/// engine layer, never by an external consumer directly.
pub struct GitBranchSchemaSource {
    workspace_root: std::path::PathBuf,
}

impl GitBranchSchemaSource {
    /// Build a git-branch source for a workspace (its `mem-repo/.git`).
    pub fn for_workspace(workspace_root: &Path) -> Self {
        Self {
            workspace_root: workspace_root.to_path_buf(),
        }
    }
}

impl memstead_base::schema_source::SchemaSource for GitBranchSchemaSource {
    fn read_schemas(
        &self,
    ) -> Result<Vec<Arc<Schema>>, memstead_base::schema_source::SchemaSourceError> {
        match load_schemas_from_ref(&self.workspace_root) {
            Ok(LoadOutcome::Schemas(schemas)) => Ok(schemas),
            // No mem-repo or no `__MEMSTEAD:schemas/` subtree → nothing
            // to overlay; the catalogue falls back to built-ins.
            Ok(LoadOutcome::NoMemRepo) | Ok(LoadOutcome::NoSchemas) => Ok(Vec::new()),
            Err(e) => Err(memstead_base::schema_source::SchemaSourceError::Read(
                e.to_string(),
            )),
        }
    }

    fn write_schema(
        &self,
        name: &str,
        version: &str,
        files: &[(String, Vec<u8>)],
    ) -> Result<(), memstead_base::schema_source::SchemaSourceError> {
        let gitdir = self.workspace_root.join("mem-repo").join(".git");
        crate::storage_memstead::write_schema_to_memstead_ref(&gitdir, name, version, files)
            .map(|_| ())
            .map_err(|e| memstead_base::schema_source::SchemaSourceError::Write(e.to_string()))
    }
}

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

    /// `(name, version, [(filename, body)])` schema seed tuple.
    type SchemaSeed<'a> = (&'a str, &'a str, &'a [(&'a str, &'a str)]);

    /// Seed `<root>/mem-repo/.git/` as a real bare repo carrying both
    /// `__SYSTEM` (empty seed) and `__SCHEMAS:<name>/{schema.yaml,
    /// types/*.yaml}` for each supplied schema. Returns the temp root.
    fn seed_mem_repo_with_schemas(schemas: &[SchemaSeed<'_>]) -> TempDir {
        let tmp = TempDir::new().unwrap();
        let gitdir = tmp.path().join("mem-repo").join(".git");
        std::fs::create_dir_all(&gitdir).unwrap();
        let repo = gix::init_bare(&gitdir).unwrap();

        let actor = gix::actor::Signature {
            name: "test".into(),
            email: "test@example.com".into(),
            time: gix::date::Time {
                seconds: 0,
                offset: 0,
            },
        };

        // Seed `__SYSTEM` with an empty tree so the "is real mem-repo"
        // gate flips to true.
        {
            let mut buf = gix::date::parse::TimeBuf::default();
            let actor_ref = actor.to_ref(&mut buf);
            repo.commit_as(
                actor_ref,
                actor_ref,
                "refs/heads/__SYSTEM",
                "seed __SYSTEM",
                repo.empty_tree().id().detach(),
                Vec::<gix::ObjectId>::new(),
            )
            .unwrap();
        }

        // Seed `__SCHEMAS` with the supplied schemas keyed at the tree
        // root.
        let mut editor = repo.empty_tree().edit().unwrap();
        for (schema_name, manifest_yaml, type_files) in schemas {
            let manifest_blob = repo.write_blob(manifest_yaml.as_bytes()).unwrap().detach();
            editor
                .upsert(
                    format!("{schema_name}/schema.yaml"),
                    gix::objs::tree::EntryKind::Blob,
                    manifest_blob,
                )
                .unwrap();
            for (stem, contents) in *type_files {
                let blob = repo.write_blob(contents.as_bytes()).unwrap().detach();
                editor
                    .upsert(
                        format!("{schema_name}/types/{stem}.yaml"),
                        gix::objs::tree::EntryKind::Blob,
                        blob,
                    )
                    .unwrap();
            }
        }
        let tree_id = editor.write().unwrap().detach();

        let mut buf = gix::date::parse::TimeBuf::default();
        let actor_ref = actor.to_ref(&mut buf);
        repo.commit_as(
            actor_ref,
            actor_ref,
            "refs/heads/__SCHEMAS",
            "seed __SCHEMAS",
            tree_id,
            Vec::<gix::ObjectId>::new(),
        )
        .unwrap();

        // Project the just-written legacy refs onto `__MEMSTEAD` so the
        // schema reader cutover (s139) doesn't break this fixture.
        // The migrator is idempotent and shares the canonical
        // projection logic with the production migration helper.
        crate::storage_memstead::migrate_to_memstead_ref(&gitdir).unwrap();

        tmp
    }

    /// A minimal `software` schema seeded into
    /// The git-branch `SchemaSource` writes a package onto the
    /// `__MEMSTEAD` ref and reads it back — the read/write surface the
    /// engine resolves authored git-branch schemas through.
    #[test]
    fn git_branch_source_round_trips_a_written_package() {
        use memstead_base::schema_source::SchemaSource;

        let tmp = TempDir::new().unwrap();
        let gitdir = tmp.path().join("mem-repo").join(".git");
        std::fs::create_dir_all(&gitdir).unwrap();
        gix::init_bare(&gitdir).unwrap();

        let source = GitBranchSchemaSource::for_workspace(tmp.path());
        // Empty ref → no authored schemas to overlay.
        assert!(source.read_schemas().unwrap().is_empty());

        let manifest = br#"name: refsrc
version: 0.1.0
description: A git-branch SchemaSource round-trip fixture.
when_to_use: tests
types:
  - doc
relationships:
  mode: strict
  definitions:
    - name: _default
      description: fallback
      default_weight: 1.0
community:
  resolution: 1.0
  seed: 42
"#;
        let doc = br#"name: doc
description: t
when_to_use: here
sections:
  - key: body
    heading: Body
    required: true
    search_weight: 10.0
    catch_all: true
    write_rules: []
metadata_fields: []
title_weight: 100.0
text_fields:
  - body
hierarchy_relationship: _default
no_self_loop_relationships: []
updatable_fields:
  - title
  - body
health_required_fields:
  - body
staleness_threshold_days: 90
write_rules: []
"#;
        source
            .write_schema(
                "refsrc",
                "0.1.0",
                &[
                    ("schema.yaml".to_string(), manifest.to_vec()),
                    ("types/doc.yaml".to_string(), doc.to_vec()),
                ],
            )
            .unwrap();

        let schemas = source.read_schemas().unwrap();
        assert_eq!(schemas.len(), 1);
        assert_eq!(schemas[0].manifest.name, "refsrc");
    }

    /// `mem-repo-git:__SCHEMAS:software/` loads as a single
    /// `Schema` whose manifest name + version match the embedded
    /// YAML. Pins the gix-tree-walk path against the smallest valid
    /// schema shape — one type, the required `_default` relationship.
    #[test]
    fn schema_registry_loads_software_schema_from_schemas_ref() {
        let manifest = r#"name: software
version: 1.0.0
description: Minimal software schema for the mem-repo gix-loader test.
when_to_use: In the mem_repo_schemas loader test only.
types:
  - sample
relationships:
  mode: strict
  definitions:
    - name: PART_OF
      description: Hierarchical containment
      default_weight: 3.0
    - name: REFERENCES
      description: Soft reference
      default_weight: 0.5
    - name: _default
      description: Fallback weight for unknown relationships
      default_weight: 1.0
community:
  resolution: 1.0
  seed: 42
"#;
        let sample_type = r#"name: sample
description: Sample type for tests
when_to_use: Whenever a minimal type is needed
sections:
  - key: body
    heading: Body
    required: true
    search_weight: 10.0
    catch_all: true
    write_rules:
      - One sentence describing the body.
metadata_fields:
  - key: status
    description: Lifecycle state
    field_type: string
    default_value: active
    enum_values:
      - active
      - closed
title_weight: 100.0
text_fields:
  - body
hierarchy_relationship: PART_OF
no_self_loop_relationships: []
updatable_fields:
  - title
  - body
  - status
health_required_fields:
  - body
staleness_threshold_days: 90
write_rules:
  - Keep it short.
"#;
        let tmp = seed_mem_repo_with_schemas(&[("software", manifest, &[("sample", sample_type)])]);

        let outcome = load_schemas_from_ref(tmp.path()).expect("loader must succeed");
        let schemas = match outcome {
            LoadOutcome::Schemas(s) => s,
            other => panic!(
                "expected Schemas outcome, got: {}",
                match other {
                    LoadOutcome::NoMemRepo => "NoMemRepo",
                    LoadOutcome::NoSchemas => "NoSchemas",
                    LoadOutcome::Schemas(_) => unreachable!(),
                }
            ),
        };
        assert_eq!(
            schemas.len(),
            1,
            "expected one schema, got {}",
            schemas.len()
        );
        let schema = &schemas[0];
        assert_eq!(schema.manifest.name, "software");
        assert_eq!(schema.version, semver::Version::new(1, 0, 0));
    }

    /// A workspace without `mem-repo/.git/` returns `NoMemRepo` so
    /// the caller can fall back to its disk-based schema source.
    #[test]
    fn no_mem_repo_returns_no_mem_repo() {
        let tmp = TempDir::new().unwrap();
        let outcome = load_schemas_from_ref(tmp.path()).expect("loader must not error");
        assert!(matches!(outcome, LoadOutcome::NoMemRepo));
    }

    /// An empty stub mem-repo (created by `init_mem_repo_stub` —
    /// `gix::init_bare` with no commits) carries no `__SYSTEM` ref and
    /// returns `NoMemRepo`. Pins the legacy-fixture compatibility
    /// also relied on by the engine's "is real mem-repo" gate.
    #[test]
    fn empty_stub_mem_repo_returns_no_mem_repo() {
        let tmp = TempDir::new().unwrap();
        let gitdir = tmp.path().join("mem-repo").join(".git");
        std::fs::create_dir_all(&gitdir).unwrap();
        gix::init_bare(&gitdir).unwrap();
        let outcome = load_schemas_from_ref(tmp.path()).expect("loader must not error");
        assert!(matches!(outcome, LoadOutcome::NoMemRepo));
    }

    /// A real mem-repo whose `__SYSTEM` ref exists but `__SCHEMAS` is
    /// absent returns `NoSchemas` — the caller may still consult its
    /// disk-based source. Pins the LoadOutcome variant the
    /// orchestrator branches on.
    ///
    /// Post-s139 cutover: the loader reads `__MEMSTEAD` only. A mem-repo
    /// with no `__MEMSTEAD` ref surfaces as `NoMemRepo`, not `NoSchemas`
    /// (the latter is reserved for `__MEMSTEAD` exists but its `schemas/`
    /// subtree is empty). The orchestrator branches both variants the
    /// same way (fall back to disk overlay), so the runtime contract
    /// is preserved even though the outcome variant changed.
    #[test]
    fn mem_repo_without_memstead_ref_returns_no_mem_repo() {
        let tmp = TempDir::new().unwrap();
        let gitdir = tmp.path().join("mem-repo").join(".git");
        std::fs::create_dir_all(&gitdir).unwrap();
        let repo = gix::init_bare(&gitdir).unwrap();

        let actor = gix::actor::Signature {
            name: "test".into(),
            email: "test@example.com".into(),
            time: gix::date::Time {
                seconds: 0,
                offset: 0,
            },
        };
        let mut buf = gix::date::parse::TimeBuf::default();
        let actor_ref = actor.to_ref(&mut buf);
        repo.commit_as(
            actor_ref,
            actor_ref,
            "refs/heads/__SYSTEM",
            "seed __SYSTEM",
            repo.empty_tree().id().detach(),
            Vec::<gix::ObjectId>::new(),
        )
        .unwrap();

        let outcome = load_schemas_from_ref(tmp.path()).expect("loader must not error");
        assert!(matches!(outcome, LoadOutcome::NoMemRepo));
    }

    // The earlier `loader_prefers_memstead_when_present` and
    // `loader_falls_back_to_legacy_when_memstead_absent` tests (and their
    // `minimal_manifest` / `minimal_type_yaml` helpers) were retired
    // in s139 alongside the legacy `__SCHEMAS` body — there is no
    // longer a fallback path to verify, and the fixture's migrator
    // call (s139) means every fixture-built workspace serves schemas
    // from `__MEMSTEAD` by construction. The
    // `schema_registry_loads_software_schema_from_schemas_ref` test
    // above already pins that path.

    /// Helper for the missing LoadOutcome::Schemas Debug — we want
    /// pattern matching to print the variant name on failure.
    impl std::fmt::Debug for LoadOutcome {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                LoadOutcome::NoMemRepo => f.write_str("NoMemRepo"),
                LoadOutcome::NoSchemas => f.write_str("NoSchemas"),
                LoadOutcome::Schemas(s) => write!(f, "Schemas({})", s.len()),
            }
        }
    }
}