omnigraph-engine 0.7.1

Runtime engine for the Omnigraph graph database.
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
mod helpers;

use std::fs;

use omnigraph::db::{InitOptions, Omnigraph, ReadTarget};
use omnigraph_compiler::schema::parser::parse_schema;
use omnigraph_compiler::{build_schema_ir, schema_ir_pretty_json};

use helpers::*;

#[tokio::test]
async fn init_creates_graph() {
    let dir = tempfile::tempdir().unwrap();
    let uri = dir.path().to_str().unwrap();

    let db = Omnigraph::init(uri, TEST_SCHEMA).await.unwrap();

    assert!(dir.path().join("_schema.pg").exists());
    assert!(dir.path().join("_schema.ir.json").exists());
    assert!(dir.path().join("__schema_state.json").exists());

    let snap = snapshot_main(&db).await.unwrap();
    assert!(snap.entry("node:Person").is_some());
    assert!(snap.entry("node:Company").is_some());
    assert!(snap.entry("edge:Knows").is_some());
    assert!(snap.entry("edge:WorksAt").is_some());

    assert_eq!(db.catalog().node_types.len(), 2);
    assert_eq!(db.catalog().edge_types.len(), 2);
    assert_eq!(
        db.catalog().node_types["Person"].key_property(),
        Some("name")
    );
}

#[tokio::test]
async fn open_reads_existing_graph() {
    let dir = tempfile::tempdir().unwrap();
    let uri = dir.path().to_str().unwrap();

    Omnigraph::init(uri, TEST_SCHEMA).await.unwrap();

    let db = Omnigraph::open(uri).await.unwrap();
    assert_eq!(db.catalog().node_types.len(), 2);
    assert_eq!(db.catalog().edge_types.len(), 2);
    let snap = snapshot_main(&db).await.unwrap();
    assert!(snap.entry("node:Person").is_some());
    assert!(snap.entry("edge:Knows").is_some());
}

#[tokio::test]
async fn open_bootstraps_legacy_schema_state_for_main_only_graph() {
    let dir = tempfile::tempdir().unwrap();
    let uri = dir.path().to_str().unwrap();
    Omnigraph::init(uri, TEST_SCHEMA).await.unwrap();

    fs::remove_file(dir.path().join("_schema.ir.json")).unwrap();
    fs::remove_file(dir.path().join("__schema_state.json")).unwrap();

    let db = Omnigraph::open(uri).await.unwrap();
    assert_eq!(db.catalog().node_types.len(), 2);
    assert!(dir.path().join("_schema.ir.json").exists());
    assert!(dir.path().join("__schema_state.json").exists());
}

#[tokio::test]
async fn open_rejects_legacy_graph_with_public_branch() {
    let dir = tempfile::tempdir().unwrap();
    let uri = dir.path().to_str().unwrap();
    let mut db = Omnigraph::init(uri, TEST_SCHEMA).await.unwrap();
    db.branch_create("feature").await.unwrap();

    fs::remove_file(dir.path().join("_schema.ir.json")).unwrap();
    fs::remove_file(dir.path().join("__schema_state.json")).unwrap();

    let err = match Omnigraph::open(uri).await {
        Ok(_) => panic!("expected legacy graph with public branch to fail schema bootstrap"),
        Err(err) => err,
    };
    assert!(
        err.to_string()
            .contains("public branches block schema evolution entirely")
    );
}

#[tokio::test]
async fn long_lived_handle_rejects_schema_source_drift() {
    let dir = tempfile::tempdir().unwrap();
    let uri = dir.path().to_str().unwrap();
    let db = Omnigraph::init(uri, TEST_SCHEMA).await.unwrap();

    let drifted = TEST_SCHEMA.replace("age: I32?", "age: I64?");
    fs::write(dir.path().join("_schema.pg"), drifted).unwrap();

    let err = match db.snapshot_of(ReadTarget::branch("main")).await {
        Ok(_) => panic!("expected schema source drift to be rejected"),
        Err(err) => err,
    };
    assert!(
        err.to_string()
            .contains("current _schema.pg no longer matches the accepted compiled schema")
    );
}

#[tokio::test]
async fn long_lived_handle_rejects_schema_ir_drift() {
    let dir = tempfile::tempdir().unwrap();
    let uri = dir.path().to_str().unwrap();
    let db = Omnigraph::init(uri, TEST_SCHEMA).await.unwrap();

    fs::write(dir.path().join("_schema.ir.json"), "{not valid json").unwrap();

    let err = match db.snapshot_of(ReadTarget::branch("main")).await {
        Ok(_) => panic!("expected schema IR drift to be rejected"),
        Err(err) => err,
    };
    assert!(
        err.to_string()
            .contains("accepted compiled schema contract in _schema.ir.json is invalid")
    );
}

#[tokio::test]
async fn long_lived_handle_rejects_ir_and_source_updates_without_state_update() {
    let dir = tempfile::tempdir().unwrap();
    let uri = dir.path().to_str().unwrap();
    let db = Omnigraph::init(uri, TEST_SCHEMA).await.unwrap();

    let drifted = TEST_SCHEMA.replace("age: I32?", "age: I64?");
    let drifted_ast = parse_schema(&drifted).unwrap();
    let drifted_ir = build_schema_ir(&drifted_ast).unwrap();
    let drifted_ir_json = schema_ir_pretty_json(&drifted_ir).unwrap();
    fs::write(dir.path().join("_schema.pg"), drifted).unwrap();
    fs::write(dir.path().join("_schema.ir.json"), drifted_ir_json).unwrap();

    let err = match db.snapshot_of(ReadTarget::branch("main")).await {
        Ok(_) => panic!("expected schema state mismatch to be rejected"),
        Err(err) => err,
    };
    assert!(
        err.to_string()
            .contains("accepted compiled schema does not match the recorded schema state")
    );
}

#[tokio::test]
async fn comment_only_schema_edit_keeps_schema_state_valid() {
    let dir = tempfile::tempdir().unwrap();
    let uri = dir.path().to_str().unwrap();
    let db = Omnigraph::init(uri, TEST_SCHEMA).await.unwrap();

    let commented = format!("// comment-only drift\n{}", TEST_SCHEMA);
    fs::write(dir.path().join("_schema.pg"), commented).unwrap();

    let snapshot = db.snapshot_of(ReadTarget::branch("main")).await.unwrap();
    assert!(snapshot.entry("node:Person").is_some());
}

#[tokio::test]
async fn open_nonexistent_fails() {
    let result = Omnigraph::open("/tmp/nonexistent_omnigraph_test_xyz").await;
    assert!(result.is_err());
}

#[tokio::test]
async fn snapshot_version_is_pinned() {
    let dir = tempfile::tempdir().unwrap();
    let uri = dir.path().to_str().unwrap();

    let mut db = Omnigraph::init(uri, TEST_SCHEMA).await.unwrap();

    let snap1 = snapshot_main(&db).await.unwrap();
    let v1 = snap1.version();

    omnigraph::loader::load_jsonl(
        &mut db,
        r#"{"type": "Person", "data": {"name": "Alice", "age": 30}}"#,
        omnigraph::loader::LoadMode::Overwrite,
    )
    .await
    .unwrap();

    let snap2 = snapshot_main(&db).await.unwrap();
    assert!(snap2.version() > v1);

    assert_eq!(snap1.version(), v1);
}

/// Regression for the `Omnigraph::init` re-init footgun (MR-668
/// follow-up): a second `init` against a URI that already holds a
/// graph must NOT modify or destroy the existing graph's schema
/// artifacts. Today's behavior is destructive either way — the
/// `write_text(_schema.pg, ...)` call at the top of
/// `init_storage_phase` overwrites the existing file before any
/// preflight, and `best_effort_cleanup_init_artifacts` will later
/// delete all three files if the inner `GraphCoordinator::init`
/// fails. Both outcomes corrupt an existing graph.
///
/// After the fix: strict-mode `init` (no `force` flag) errors out
/// before touching any file, and the original schema artifacts
/// match their pre-attempt contents byte-for-byte.
#[tokio::test]
async fn init_on_existing_graph_uri_does_not_destroy_existing_schema() {
    let dir = tempfile::tempdir().unwrap();
    let uri = dir.path().to_str().unwrap();

    // Establish the first graph and snapshot its three schema files.
    Omnigraph::init(uri, TEST_SCHEMA).await.unwrap();
    let original_schema_pg = fs::read_to_string(dir.path().join("_schema.pg")).unwrap();
    let original_schema_ir = fs::read_to_string(dir.path().join("_schema.ir.json")).unwrap();
    let original_schema_state = fs::read_to_string(dir.path().join("__schema_state.json")).unwrap();

    // Attempt a re-init with a deliberately different schema so any
    // overwrite would be observable in the file contents.
    let different_schema = "node Other { id: String @key }\n";
    let result = Omnigraph::init(uri, different_schema).await;

    // The new init must report the conflict, not silently mutate.
    assert!(
        result.is_err(),
        "init against an existing graph URI must error, not silently overwrite"
    );

    // The three schema files must remain present and byte-identical to
    // their pre-attempt contents.
    assert!(
        dir.path().join("_schema.pg").exists(),
        "_schema.pg must not be deleted by a failed re-init"
    );
    assert!(
        dir.path().join("_schema.ir.json").exists(),
        "_schema.ir.json must not be deleted by a failed re-init"
    );
    assert!(
        dir.path().join("__schema_state.json").exists(),
        "__schema_state.json must not be deleted by a failed re-init"
    );
    assert_eq!(
        fs::read_to_string(dir.path().join("_schema.pg")).unwrap(),
        original_schema_pg,
        "_schema.pg contents must be preserved when re-init is rejected"
    );
    assert_eq!(
        fs::read_to_string(dir.path().join("_schema.ir.json")).unwrap(),
        original_schema_ir,
        "_schema.ir.json contents must be preserved when re-init is rejected"
    );
    assert_eq!(
        fs::read_to_string(dir.path().join("__schema_state.json")).unwrap(),
        original_schema_state,
        "__schema_state.json contents must be preserved when re-init is rejected"
    );
}

/// Happy-path sibling to the strict re-init regression above:
/// `InitOptions { force: true }` must skip the schema-file preflight
/// when the operator deliberately wants to recover from orphan
/// schema artifacts (e.g. files left behind by a failed prior init).
///
/// Documented semantics per `InitOptions::force`: skips the preflight
/// only. Force does NOT purge existing Lance datasets or `__manifest/`
/// — that needs `StorageAdapter::delete_prefix`, which is tracked
/// separately. The realistic recovery scenario is "schema files
/// exist but Lance state doesn't," which this test reproduces.
///
/// Without this test, a future refactor could invert the `if !force`
/// branch and silently break the operator-facing escape hatch.
#[tokio::test]
async fn init_with_force_recovers_from_orphan_schema_files() {
    let dir = tempfile::tempdir().unwrap();
    let uri = dir.path().to_str().unwrap();

    // Simulate orphan schema files: write `_schema.pg` to disk
    // without running a full init. The preflight will see it and
    // bail in strict mode.
    fs::write(dir.path().join("_schema.pg"), TEST_SCHEMA).unwrap();

    // Strict mode refuses because `_schema.pg` exists.
    let strict_err = match Omnigraph::init(uri, TEST_SCHEMA).await {
        Ok(_) => panic!("strict init must refuse when orphan _schema.pg exists"),
        Err(e) => e,
    };
    assert!(
        strict_err.to_string().contains("already initialized"),
        "strict init must surface AlreadyInitialized (sanity check); got: {strict_err}"
    );

    // Force init succeeds: it skips the preflight, overwrites the
    // orphan file, and proceeds to initialize Lance state (which
    // didn't exist, so `GraphCoordinator::init` is unblocked).
    let db = Omnigraph::init_with_options(uri, TEST_SCHEMA, InitOptions { force: true })
        .await
        .expect("force init must succeed when only orphan schema files block strict init");

    // Confirm the catalog is populated as expected — proves the
    // graph is functional after force-recovery, not just that the
    // call returned Ok.
    assert!(
        db.catalog().node_types.contains_key("Person"),
        "force-recovered graph must have the new catalog installed"
    );
    assert!(
        dir.path().join("__schema_state.json").exists(),
        "force-recovered graph must have full schema state written"
    );
}

/// E2e for the schema-level `.pg` surface: `@description` (node / edge /
/// property) and `@instruction` (node / edge only) parse, validate, and
/// persist verbatim into the on-disk `_schema.ir.json` through `Omnigraph::init`
/// — the contract that surfaces them in catalog metadata for tooling.
#[tokio::test]
async fn schema_annotations_persist_into_ir_json_on_init() {
    let dir = tempfile::tempdir().unwrap();
    let uri = dir.path().to_str().unwrap();

    let schema = r#"
node Task @description("Tracked work item") @instruction("Prefer querying by slug") {
    slug: String @key @description("Stable external identifier")
}

edge DependsOn: Task -> Task @description("Hard dependency") @instruction("Use only for blockers")
"#;

    Omnigraph::init(uri, schema).await.unwrap();

    let ir_json = fs::read_to_string(dir.path().join("_schema.ir.json")).unwrap();
    let ir: serde_json::Value = serde_json::from_str(&ir_json).unwrap();

    // Helper: collect the {name -> value} map of annotations that carry a
    // string value. Value-less annotations (e.g. `@key`, which also desugars
    // to a constraint) are skipped — they aren't what this test asserts.
    let anns = |v: &serde_json::Value| -> std::collections::BTreeMap<String, String> {
        v["annotations"]
            .as_array()
            .unwrap()
            .iter()
            .filter_map(|a| {
                Some((
                    a["name"].as_str()?.to_string(),
                    a["value"].as_str()?.to_string(),
                ))
            })
            .collect()
    };

    let node = ir["nodes"]
        .as_array()
        .unwrap()
        .iter()
        .find(|n| n["name"] == "Task")
        .unwrap();
    let node_anns = anns(node);
    assert_eq!(node_anns.get("description").map(String::as_str), Some("Tracked work item"));
    assert_eq!(
        node_anns.get("instruction").map(String::as_str),
        Some("Prefer querying by slug"),
        "node @instruction persists into _schema.ir.json"
    );

    let prop = node["properties"]
        .as_array()
        .unwrap()
        .iter()
        .find(|p| p["name"] == "slug")
        .unwrap();
    assert_eq!(
        anns(prop).get("description").map(String::as_str),
        Some("Stable external identifier"),
        "property @description persists into _schema.ir.json"
    );

    let edge = ir["edges"]
        .as_array()
        .unwrap()
        .iter()
        .find(|e| e["name"] == "DependsOn")
        .unwrap();
    let edge_anns = anns(edge);
    assert_eq!(edge_anns.get("description").map(String::as_str), Some("Hard dependency"));
    assert_eq!(edge_anns.get("instruction").map(String::as_str), Some("Use only for blockers"));
}

/// `@instruction` is rejected on a property at compile time, so init aborts
/// before any graph state is written (mirrors the parser-level rejection from
/// the full engine boundary).
#[tokio::test]
async fn init_rejects_instruction_on_property() {
    let dir = tempfile::tempdir().unwrap();
    let uri = dir.path().to_str().unwrap();

    let schema = r#"
node Task {
    slug: String @key @instruction("bad")
}
"#;

    // `Omnigraph` is not `Debug`, so match rather than `unwrap_err`.
    let err = match Omnigraph::init(uri, schema).await {
        Ok(_) => panic!("property-level @instruction must abort init"),
        Err(err) => err,
    };
    assert!(
        err.to_string().contains("@instruction is only supported on node and edge types"),
        "property-level @instruction must abort init: {err}"
    );
    assert!(
        !dir.path().join("_schema.ir.json").exists(),
        "rejected init must not persist a schema IR"
    );
}