omgbase 0.20.0

omgbase: a versioned, addressable graph layer over authored Markdown — the `omgbase` binary (`omgbase mcp` serves the tool catalog over MCP stdio, with the filesystem watcher and the background embed drain)
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! The document verbs (`spec/cli` §6 `new`, `mv`, `meta`, `rm --doc`; cli
//! 4.0): each builds its one document effect with the shared builders
//! (`omgbase_store::sugar` — the table the `docs_*` tools use) and applies it
//! locally through `apply` under the `spec/mutate` §6 reason (the reference's
//! library path: the planner's own checks, `path_taken { path }`), remotely
//! through the mirroring `docs_create` / `docs_move` / `docs_set_meta` /
//! `docs_delete` tool (`spec/surface` §4, each answering with the Receipt).
//! Rendered as document operations (§4): the confirmation `  ok <verb>
//! <path>` on stderr, the document id on stdout; `--json` prints `{ id,
//! path, committed }` (`mv` adds `dangling`/`retargeted`) — the verb's own
//! shape over the Receipt, as the reference renders it. The global
//! `--dry-run` is a **plan** (§3.6): the per-file diffs of the effect's
//! preview, nothing minted, nothing committed.

use omgbase_store::docs_ops::{canonical, retargeted_json};
use omgbase_store::links::{InboundLink, inbound_links_to};
use omgbase_store::{DocStore, FsDocStore, Retargeted, find_doc_by_ref, is_id_ref, sugar};
use omgbase_surface::reference_path;
use serde_json::{Map, Value as Json, json};

use crate::cli::argv::{Opt, parse_args};
use crate::cli::context::Cli;
use crate::cli::help::{render_help_for, wants_help};
use crate::cli::output::{CliError, EXIT_OK, Result};

use super::mutate::{
    effects_json, extract_content_opts, read_content, render_plan, render_summary, set_actor,
};
use super::{doc_locator, machine_out, str_of};

const ACTOR: Opt = Opt::value("actor");
const NO_RETARGET: Opt = Opt::flag("no-retarget");

/// Run one document operation: locally the builder's effects through
/// `apply` under its reason (the reference's `applySugar`), remotely the
/// mirroring tool with `args` — or, against an engine that does not list it
/// (the minimal profile), `apply` over the same effects; the global
/// `--dry-run` is the plan of the effects on both sides. Returns the Receipt (the tool's result remotely),
/// or `None` after a rendered plan.
fn run_doc(
    cli: &mut Cli,
    tool: &str,
    args: Map<String, Json>,
    actor: Option<&str>,
    built: &sugar::Sugar,
) -> Result<Option<Json>> {
    set_actor(cli, actor)?;
    let effects = effects_json(&built.effects);
    if cli.flags.dry_run {
        let plan = cli.preview("apply", json!({ "effects": effects }), effects)?;
        render_plan(cli, plan)?;
        return Ok(None);
    }
    if cli.remote_mode() {
        // The mirroring tool when the engine lists it, else `apply` over the
        // builder's effects (§2.3, cli 4.2).
        return Ok(Some(cli.remote_sugar(
            tool,
            Json::Object(args),
            &[],
            |_| Ok(effects),
        )?));
    }
    let (receipt, _) = cli.call_with_plan(
        "apply",
        json!({ "effects": effects, "reason": built.reason }),
    )?;
    Ok(Some(receipt))
}

/// A document a verb addresses: its id and storage-form path. Locally the
/// store's row (a miss is the surface's `doc_missing` naming the locator);
/// remotely a `d_` ref is the id already and a path is read back through
/// `docs_read` — the Receipt of a move or a delete need not carry it.
fn doc_identity(cli: &mut Cli, r: &str) -> Result<(String, String)> {
    if cli.remote_mode() {
        let read = if is_id_ref(r, "d") {
            cli.call("docs_read", json!({ "id": r }))?
        } else {
            cli.call("docs_read", json!({ "path": r }))?
        };
        return Ok((str_of(&read, "id"), canonical(&str_of(&read, "path"))));
    }
    let repo = cli.repo()?;
    match find_doc_by_ref(cli.store()?.conn(), &repo.repo_id, r)? {
        Some(info) => Ok((info.doc_id, info.path)),
        None => {
            let mut locator = Map::new();
            doc_locator(&mut locator, r);
            Err(CliError::engine_data(
                "doc_missing",
                format!("no document with id or path {}", Json::String(r.to_owned())),
                Json::Object(locator),
            ))
        }
    }
}

/// §4 document-op confirmation over `{ id, path, committed: true }` (+ `mv`'s
/// extras): the `--json` document in a machine mode, else `  ok <verb>
/// <path>` on stderr, the Receipt's `summary` lines under it (cli 4.3), and
/// the id on stdout.
fn report(cli: &mut Cli, verb: &str, res: &Json, receipt: &Json) -> Result<i32> {
    cli.capture(res);
    if let Some(code) = machine_out(cli, res, None, None) {
        return code;
    }
    cli.io.err(&cli.style.dim(&format!(
        "  {} {verb} {}",
        cli.style.ok(cli.style.glyphs().ok),
        cli.style.accent(&str_of(res, "path"))
    )));
    render_summary(cli, receipt);
    cli.io.out(&str_of(res, "id"));
    Ok(EXIT_OK)
}

/// The pre-check the `docs_create` / `docs_move` tools run, locally (the
/// reference's `requireAbsentPath`): a live document at `rel` is
/// `path_taken` with the verb's message, a file on disk `path_taken` "file
/// already exists on disk at <rel>" — the legacy messages, no payload (the
/// planner's own copy, which names the path in `data`, never fires behind it).
fn require_absent(cli: &mut Cli, rel: &str, taken_message: String) -> Result<()> {
    let repo = cli.repo()?;
    if cli.store()?.live_doc_at(&repo.repo_id, rel)? {
        return Err(CliError::engine("path_taken", taken_message));
    }
    if let Some(root) = &repo.root_path {
        if FsDocStore::new(root).exists(rel) {
            return Err(CliError::engine(
                "path_taken",
                format!("file already exists on disk at {rel}"),
            ));
        }
    }
    Ok(())
}

/// `{ id, path, committed: true }`, the path rooted.
fn doc_op(id: &str, path: &str) -> Json {
    json!({ "id": id, "path": reference_path(path), "committed": true })
}

// ---- new ------------------------------------------------------------------------------

/// `new <path> (-m <markdown> | -f <file> | -) [--actor <s>] [--dry-run]` —
/// `docs_create` from complete file bytes; the created document is the
/// Receipt's `new_ids["$new.0"]`.
pub fn new(cli: &mut Cli, args: &[String]) -> Result<i32> {
    if wants_help(args) {
        return render_help_for(cli, "new");
    }
    let content = extract_content_opts(args)?;
    let a = parse_args(&content.rest, &[ACTOR])?;
    let Some(path) = a.pos(0) else {
        return Err(CliError::usage("new requires a <path>"));
    };
    let bytes = read_content(&content)?;
    let built = sugar::create_document(path, &bytes, None);
    let rel = canonical(path);
    let mut m = Map::new();
    m.insert("path".to_owned(), json!(path));
    m.insert("markdown".to_owned(), json!(bytes));
    if !cli.remote_mode() && !cli.flags.dry_run {
        require_absent(cli, &rel, format!("document already exists at {rel}"))?;
    }
    let Some(res) = run_doc(cli, "docs_create", m, a.value("actor"), &built)? else {
        return Ok(EXIT_OK);
    };
    // The created document is the Receipt's `$new.0` (its `revisions[0]`).
    let id = res["new_ids"]["$new.0"]
        .as_str()
        .or_else(|| res["revisions"][0]["id"].as_str())
        .unwrap_or_default()
        .to_owned();
    report(cli, "created", &doc_op(&id, &rel), &res)
}

// ---- mv -------------------------------------------------------------------------------

/// `mv <doc> <new-path> [--no-retarget] [--actor <s>] [--dry-run]` —
/// `docs_move`: identity and history preserved; the inbound links are
/// rewritten to the new path by default (spec/mutate 1.3) and the human
/// rendering says how many; `--no-retarget` leaves them as written, and
/// whatever still dangles is reported on stderr with the fix for its kind
/// (spec/cli §6 `mv`).
pub fn mv(cli: &mut Cli, args: &[String]) -> Result<i32> {
    if wants_help(args) {
        return render_help_for(cli, "mv");
    }
    let a = parse_args(args, &[ACTOR, NO_RETARGET])?;
    let (Some(doc), Some(to)) = (a.pos(0), a.pos(1)) else {
        return Err(CliError::usage("mv requires <doc> and <new-path>"));
    };
    let retarget = !a.flag("no-retarget");
    let to_rel = canonical(to);
    let mut m = Map::new();
    doc_locator(&mut m, doc);
    m.insert("to_path".to_owned(), json!(to));
    if !retarget {
        m.insert("retarget_inbound".to_owned(), json!(false));
    }
    if cli.remote_mode() && cli.flags.dry_run {
        // The same effect the server builds, over the locator (the server
        // resolves it).
        let mut target = Map::new();
        target.insert("kind".to_owned(), json!("document"));
        doc_locator(&mut target, doc);
        let effects =
            json!([{ "op": "move", "target": target, "to": to_rel, "retarget_inbound": retarget }]);
        let plan = cli.preview("docs_move", Json::Object(m), effects)?;
        return render_plan(cli, plan);
    }
    // The document first (its id is what the confirmation prints; the
    // Receipt of a move lists the rewritten sources, not the moved document).
    let (id, from) = doc_identity(cli, doc)?;
    let built = sugar::move_document(&id, &from, &to_rel, retarget);
    // Locally: the pre-check the `docs_move` tool runs (the legacy
    // `path_taken` wording, the path as payload), then who links here BY
    // PATH as written — read before the path changes (pure, so equal to what
    // the planner lowers for the same `move` effect).
    let mut links: Option<(Vec<InboundLink>, Option<Retargeted>)> = None;
    if !cli.remote_mode() {
        if !cli.flags.dry_run {
            require_absent(
                cli,
                &to_rel,
                format!("a document already exists at {to_rel}"),
            )?;
        }
        let repo = cli.repo()?.repo_id;
        let store = cli.store()?;
        let inbound = inbound_links_to(store.conn(), &repo, &id, &from)?;
        links = Some(if retarget && !inbound.is_empty() {
            let p = store.plan_inbound_retarget(&id, &from, &to_rel, &inbound)?;
            (
                p.dangling,
                Some(Retargeted {
                    blocks: p.blocks,
                    docs: p.docs,
                }),
            )
        } else {
            (inbound, None)
        });
    }
    let Some(res) = run_doc(cli, "docs_move", m, a.value("actor"), &built)? else {
        return Ok(EXIT_OK);
    };
    let mut out = doc_op(&id, &to_rel);
    match links {
        // Each dangling link is a link row under the `links` target's names
        // (spec/surface §1.2) with its source `path` rooted.
        Some((dangling, retargeted)) => {
            out["dangling"] = Json::Array(
                dangling
                    .iter()
                    .map(|l| {
                        let mut v = l.to_json();
                        if let Some(p) = v.get("path").and_then(Json::as_str).map(reference_path) {
                            v["path"] = json!(p);
                        }
                        v
                    })
                    .collect(),
            );
            out["retargeted"] = retargeted_json(retargeted.as_ref());
        }
        None => {
            out["dangling"] = res.get("dangling").cloned().unwrap_or_else(|| json!([]));
            out["retargeted"] = res.get("retargeted").cloned().unwrap_or(Json::Null);
        }
    }
    let code = report(cli, "moved to", &out, &res)?;
    if cli.machine() {
        return Ok(code);
    }
    let style = cli.style;
    if let Some(r) = out.get("retargeted").filter(|r| !r.is_null()) {
        let count = |k: &str| r.get(k).and_then(Json::as_array).map_or(0, Vec::len);
        let (b, d) = (count("blocks"), count("docs"));
        if b > 0 {
            cli.io.err(&style.dim(&format!(
                "  {} retargeted {b} inbound link{} in {d} document{}",
                style.ok(style.glyphs().ok),
                if b == 1 { "" } else { "s" },
                if d == 1 { "" } else { "s" },
            )));
        }
    }
    let dangling: Vec<&Json> = out
        .get("dangling")
        .and_then(Json::as_array)
        .map(|d| d.iter().collect())
        .unwrap_or_default();
    if dangling.is_empty() {
        return Ok(code);
    }
    let where_ = dangling
        .iter()
        .map(|l| {
            let path = str_of(l, "path");
            match l.get("block_id").and_then(Json::as_str) {
                Some(b) => format!("{path} {b}"),
                None => format!("{path} (frontmatter)"),
            }
        })
        .collect::<Vec<_>>()
        .join(", ");
    let n = dangling.len();
    cli.io.err(&style.warn(&format!(
        "  {} {n} inbound link{} still name{} the old path: {where_}",
        style.warn(style.glyphs().warn),
        if n == 1 { "" } else { "s" },
        if n == 1 { "s" } else { "" },
    )));
    let new_path = str_of(&out, "path");
    // Authored links left as written (`--no-retarget`): the retarget that
    // rewrites them — from the path the move left behind, rooted.
    if dangling
        .iter()
        .any(|l| l.get("block_id").is_some_and(Json::is_string))
    {
        cli.io.err(&style.dim(&format!(
            "  fix: {} retarget {} {new_path} --apply",
            cli.prog,
            reference_path(&from)
        )));
    }
    // Frontmatter relations are never rewritten by a move: one `meta` per
    // (source, field).
    let mut seen: Vec<(String, String)> = Vec::new();
    for l in &dangling {
        if l.get("block_id").is_some_and(Json::is_string) {
            continue;
        }
        let Some(field) = l.get("field").and_then(Json::as_str) else {
            continue;
        };
        let key = (str_of(l, "path"), field.to_owned());
        if seen.contains(&key) {
            continue;
        }
        cli.io.err(&style.dim(&format!(
            "  fix: {} meta {} --set {field}={new_path}",
            cli.prog, key.0
        )));
        seen.push(key);
    }
    Ok(code)
}

// ---- rm --doc -------------------------------------------------------------------------

/// `rm --doc <doc>` — `docs_delete`, rendered as a document operation (§4)
/// `deleted <path>`; the Receipt of a delete carries no revision, so the
/// document is resolved first.
pub fn rm_doc(cli: &mut Cli, doc: &str, actor: Option<&str>) -> Result<i32> {
    let mut m = Map::new();
    doc_locator(&mut m, doc);
    if cli.remote_mode() && cli.flags.dry_run {
        let mut target = Map::new();
        target.insert("kind".to_owned(), json!("document"));
        doc_locator(&mut target, doc);
        let plan = cli.preview(
            "docs_delete",
            Json::Object(m),
            json!([{ "op": "remove", "target": target }]),
        )?;
        return render_plan(cli, plan);
    }
    let (id, path) = doc_identity(cli, doc)?;
    let built = sugar::delete_document(&id, &path);
    let Some(res) = run_doc(cli, "docs_delete", m, actor, &built)? else {
        return Ok(EXIT_OK);
    };
    report(cli, "deleted", &doc_op(&id, &path), &res)
}

// ---- meta -----------------------------------------------------------------------------

/// The repeatable `--set`, `--set-json` and `--unset` (and their `=` forms),
/// pulled out before the option parser (which keeps one value per option);
/// everything from a literal `--` on is left for the parser.
#[derive(Debug)]
struct MetaOpts {
    set: Vec<String>,
    set_json: Vec<String>,
    unset: Vec<String>,
    rest: Vec<String>,
}

fn extract_meta_opts(args: &[String]) -> Result<MetaOpts> {
    let mut out = MetaOpts {
        set: Vec::new(),
        set_json: Vec::new(),
        unset: Vec::new(),
        rest: Vec::new(),
    };
    let mut i = 0;
    while i < args.len() {
        let a = args[i].as_str();
        i += 1;
        if a == "--" {
            out.rest.push(a.to_owned());
            out.rest.extend(args[i..].iter().cloned());
            break;
        }
        let (name, inline) = match a.strip_prefix("--") {
            Some(body) => match body.split_once('=') {
                Some((n, v)) => (n, Some(v.to_owned())),
                None => (body, None),
            },
            None => {
                out.rest.push(a.to_owned());
                continue;
            }
        };
        let bucket = match name {
            "set" => &mut out.set,
            "set-json" => &mut out.set_json,
            "unset" => &mut out.unset,
            _ => {
                out.rest.push(a.to_owned());
                continue;
            }
        };
        let v = match inline {
            Some(v) => v,
            None => match args.get(i) {
                Some(v) => {
                    i += 1;
                    v.clone()
                }
                None => {
                    return Err(CliError::usage(format!(
                        "option '--{name}' requires a value"
                    )));
                }
            },
        };
        bucket.push(v);
    }
    Ok(out)
}

/// `--set k=v`: the value as the YAML core schema resolves it (`1702` a
/// number, `true` a boolean, `~` null, anything else a string).
fn yaml_scalar(v: &str) -> Json {
    omgbase_properties::parse_document(v)
        .map_or_else(|| Json::String(v.to_owned()), |y| y.to_json())
}

/// `meta <doc> [--set k=v]… [--set-json k=<json>]… [--unset k]… [--actor <s>] [--dry-run]`
/// — `docs_set_meta`: the frontmatter patched, the body untouched.
pub fn meta(cli: &mut Cli, args: &[String]) -> Result<i32> {
    if wants_help(args) {
        return render_help_for(cli, "meta");
    }
    let opts = extract_meta_opts(args)?;
    let a = parse_args(&opts.rest, &[ACTOR])?;
    let Some(doc) = a.pos(0) else {
        return Err(CliError::usage("meta requires a <doc>"));
    };
    let mut set = Map::new();
    for kv in &opts.set {
        let Some((k, v)) = kv.split_once('=') else {
            return Err(CliError::usage(format!("--set expects k=v, got '{kv}'")));
        };
        set.insert(k.to_owned(), yaml_scalar(v));
    }
    for kv in &opts.set_json {
        let Some((k, v)) = kv.split_once('=') else {
            return Err(CliError::usage(format!(
                "--set-json expects k=json, got '{kv}'"
            )));
        };
        let parsed: Json = serde_json::from_str(v)
            .map_err(|e| CliError::usage(format!("--set-json expects k=json, got '{kv}' ({e})")))?;
        set.insert(k.to_owned(), parsed);
    }
    if set.is_empty() && opts.unset.is_empty() {
        return Err(CliError::usage("meta requires --set or --unset"));
    }
    let mut m = Map::new();
    doc_locator(&mut m, doc);
    if !set.is_empty() {
        m.insert("set".to_owned(), Json::Object(set.clone()));
    }
    if !opts.unset.is_empty() {
        m.insert("unset".to_owned(), json!(opts.unset));
    }
    if cli.remote_mode() && cli.flags.dry_run {
        // The same effects the server builds, over the locator.
        let mut target = Map::new();
        target.insert("kind".to_owned(), json!("document"));
        doc_locator(&mut target, doc);
        let target = Json::Object(target);
        let mut effects: Vec<Json> = Vec::new();
        for (k, v) in &set {
            if !opts.unset.contains(k) {
                effects.push(json!({ "op": "set", "target": target, "field": format!("frontmatter.{k}"), "value": v }));
            }
        }
        for k in &opts.unset {
            effects.push(
                json!({ "op": "unset", "target": target, "field": format!("frontmatter.{k}") }),
            );
        }
        let plan = cli.preview("docs_set_meta", Json::Object(m), Json::Array(effects))?;
        return render_plan(cli, plan);
    }
    let (id, path) = doc_identity(cli, doc)?;
    let built = sugar::set_meta(&id, &path, Some(&set), &opts.unset);
    let Some(res) = run_doc(cli, "docs_set_meta", m, a.value("actor"), &built)? else {
        return Ok(EXIT_OK);
    };
    report(cli, "patched", &doc_op(&id, &path), &res)
}

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

    fn v(a: &[&str]) -> Vec<String> {
        a.iter().map(|s| (*s).to_owned()).collect()
    }

    #[test]
    fn meta_options_repeat() {
        let o = extract_meta_opts(&v(&[
            "d.md",
            "--set",
            "a=1",
            "--set=b=x",
            "--set-json",
            "c=[1]",
            "--unset",
            "d",
            "--actor",
            "human:spec",
        ]))
        .unwrap();
        assert_eq!(o.set, v(&["a=1", "b=x"]));
        assert_eq!(o.set_json, v(&["c=[1]"]));
        assert_eq!(o.unset, v(&["d"]));
        assert_eq!(o.rest, v(&["d.md", "--actor", "human:spec"]));
        let e = extract_meta_opts(&v(&["d.md", "--set"])).unwrap_err();
        assert_eq!(e.message(), "option '--set' requires a value");
    }

    #[test]
    fn set_values_are_yaml_scalars() {
        assert_eq!(yaml_scalar("1702"), json!(1702));
        assert_eq!(yaml_scalar("true"), json!(true));
        assert_eq!(yaml_scalar("canon"), json!("canon"));
        assert_eq!(yaml_scalar("~"), Json::Null);
        assert_eq!(yaml_scalar("0.5"), json!(0.5));
    }

    #[test]
    fn doc_op_roots_the_path() {
        assert_eq!(
            doc_op("d_1", "notes/a.md"),
            json!({ "id": "d_1", "path": "/notes/a.md", "committed": true })
        );
    }
}