Skip to main content

omgbase_surface/
graph.rs

1//! The `graph` neighborhood macro (`spec/surface/README.md` §4): compiled to
2//! an OQX `follow out_links collect { from dst }` / `follow in_links collect
3//! { from doc }` walk over the link rows (3.0 follow-up: there are no
4//! `out`/`in` relations — `dst` is a link's destination document, `doc` its
5//! source; an absent `dst`, an external or phantom target, contributes no
6//! successor) run through the shared runner and
7//! shaped into `{ documents, links, frontier }`. Port of
8//! `packages/core/src/mcp/graph.ts`; nothing here walks the graph itself.
9
10use std::cmp::Ordering;
11use std::collections::HashMap;
12
13use omgbase_search::EmbeddingProvider;
14use omgbase_store::Store;
15use oqx::ast::{BinaryOp, Consumer, FollowDestination, SelectItem, Where};
16use oqx::{build, print_query};
17use serde_json::{Map, Value as Json, json};
18
19use crate::error::{Result, SurfaceError};
20use crate::query::{QueryOptions, query};
21use crate::read::find_doc_by_ref;
22
23const DEFAULT_DEGREES: i64 = 1;
24const DEFAULT_MAX_DOCUMENTS: i64 = 200;
25const MAX_DEPTH: i64 = 8;
26
27/// `graph`'s arguments.
28#[derive(Clone, Debug, Default)]
29pub struct GraphArgs {
30    pub roots: Vec<String>,
31    pub degrees: Option<i64>,
32    /// `in` | `out` | `both` (default).
33    pub direction: Option<String>,
34    pub predicate: Option<String>,
35    pub select: Vec<String>,
36    pub max_documents: Option<i64>,
37}
38
39/// The edge fields projected from `doc.out_links`/`doc.in_links` — the `links`
40/// target's own names, projected under themselves (`spec/surface` §1.2, 3.0:
41/// no rename between the language and the result) — identical for both
42/// directions so a single filter handles either scan (an edge is a directional
43/// fact regardless of which endpoint the walk reached it from).
44const EDGE_FIELDS: [&str; 12] = [
45    "id",
46    "doc_id",
47    "block_id",
48    "via_id",
49    "dst_id",
50    "dst_path",
51    "uri",
52    "dst_kind",
53    "predicate",
54    "provenance",
55    "anchor",
56    "field",
57];
58
59/// The extra `select` items for caller projections. Each expression gets a
60/// stable internal alias (`_u0`, `_u1`, …) so it can never collide with a
61/// reserved word or the macro's own aliases; the clean output name is derived
62/// separately. An expression is parsed as the item of a projection, so it may
63/// be anything a `select` item may be and nothing more; a malformed one is
64/// `filter_invalid`, as it would be in the `query` tool.
65fn build_user_select(select: &[String]) -> Result<(Vec<SelectItem>, Vec<Option<String>>)> {
66    let mut items = Vec::new();
67    let mut out_names: Vec<Option<String>> = vec![None; select.len()];
68    let mut used: Vec<String> = Vec::new();
69    for (i, expr) in select.iter().enumerate() {
70        let trimmed = expr.trim();
71        if trimmed.is_empty() {
72            continue;
73        }
74        // A bare identifier keeps its spelling as the output key (3.0: a
75        // `title` selection is `title` in the document, as a `query` hit's
76        // would be; an OQX `$` name too); anything else is `sel_<i>`, so a
77        // projection can never overwrite the envelope here.
78        let ident = trimmed.strip_prefix('$').unwrap_or(trimmed);
79        let is_ident = ident
80            .chars()
81            .next()
82            .is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
83            && ident.chars().all(|c| c.is_ascii_alphanumeric() || c == '_');
84        let mut name = if is_ident {
85            trimmed.to_owned()
86        } else {
87            format!("sel_{i}")
88        };
89        // The envelope keys are the row's own identity: a `id`/`path`
90        // selection lands on them rather than beside them (the engine reads
91        // the same value).
92        if name != "id" && name != "path" {
93            while used.contains(&name) {
94                name = format!("{name}_{i}");
95            }
96            used.push(name.clone());
97        }
98        out_names[i] = Some(name);
99        let parsed = oqx::parse_string(&format!("select _u{i}: {trimmed} from docs"))?;
100        items.extend(parsed.select);
101    }
102    Ok((items, out_names))
103}
104
105/// The walk, as an AST (`spec/oqx/AST.md` §7) printed canonically — never
106/// spliced strings: the seed ids are literals, the caller's projections parsed
107/// items.
108///
109/// ```text
110/// select _depth: $depth, _stop: $stop, _links: doc.<dir>_links collect { <edge fields> } <user items>
111/// from docs where id == <root> or … follow distinct <dir>_links collect { from <dst | doc> } { depth <depth> }
112/// ```
113fn build_query(root_ids: &[String], dir: &str, depth: u32, user_select: &[SelectItem]) -> String {
114    let mut edges_body = build::subquery();
115    edges_body.select = EDGE_FIELDS
116        .iter()
117        .map(|name| build::field(name, build::ident(name)))
118        .collect();
119    let edges = build::op(
120        build::path(&["doc", &format!("{dir}_links")]),
121        Consumer::Collect,
122        edges_body,
123    );
124    let mut seed: Vec<Where> = root_ids
125        .iter()
126        .map(|id| {
127            build::scalar(build::binary(
128                BinaryOp::Eq,
129                build::ident("id"),
130                build::lit(id.as_str()),
131            ))
132        })
133        .collect();
134    let mut q = build::query(build::ident("docs"));
135    q.select = vec![
136        build::field("_depth", build::ident("$depth")),
137        build::field("_stop", build::ident("$stop")),
138        build::collect("_links", edges),
139    ];
140    q.select.extend(user_select.iter().cloned());
141    q.r#where = Some(if seed.len() == 1 {
142        seed.pop().expect("one seed")
143    } else {
144        build::or(seed)
145    });
146    // `depth` is the destination block's own clause (oqx 0.20).
147    let mut step = build::subquery();
148    step.from = vec![build::ident(if dir == "out" { "dst" } else { "doc" })];
149    step.depth = Some(depth);
150    let mut walk = build::follow(vec![FollowDestination::Block(Box::new(build::op(
151        build::ident(&format!("{dir}_links")),
152        Consumer::Collect,
153        step,
154    )))]);
155    walk.distinct = true;
156    q.follow = Some(walk);
157    print_query(&q).expect("a built query has no bindings")
158}
159
160/// Bytewise order (§9: the reference's `localeCompare` was replaced).
161fn locale_compare(a: &str, b: &str) -> Ordering {
162    a.cmp(b)
163}
164
165struct Doc {
166    id: String,
167    path: String,
168    degree: i64,
169    extra: Vec<(String, Json)>,
170}
171
172/// Run the macro.
173pub fn graph_neighborhood(
174    store: &Store,
175    repo_id: &str,
176    args: &GraphArgs,
177    provider: Option<&dyn EmbeddingProvider>,
178    now_ms: Option<i64>,
179) -> Result<Json> {
180    if args.roots.is_empty() {
181        return Err(SurfaceError::new(
182            "target_missing",
183            "graph requires at least one root (path or id)",
184        ));
185    }
186    let mut root_ids: Vec<String> = Vec::new();
187    for r in &args.roots {
188        let Some(info) = find_doc_by_ref(store.conn(), repo_id, r)? else {
189            return Err(SurfaceError::with_data(
190                "doc_missing",
191                format!("no document for {}", Json::String(r.clone())),
192                json!({ "root": r }),
193            ));
194        };
195        if !root_ids.contains(&info.doc_id) {
196            root_ids.push(info.doc_id);
197        }
198    }
199    let degrees = args.degrees.unwrap_or(DEFAULT_DEGREES).max(0);
200    let depth = MAX_DEPTH.min(degrees + 1);
201    let walk_depth = u32::try_from(depth).expect("1..=8");
202    let effective_degrees = depth - 1;
203    let direction = args.direction.clone().unwrap_or_else(|| "both".to_owned());
204    let max_documents =
205        usize::try_from(args.max_documents.unwrap_or(DEFAULT_MAX_DOCUMENTS).max(1)).unwrap_or(1);
206    let dirs: Vec<&str> = match direction.as_str() {
207        "both" => vec!["out", "in"],
208        "in" => vec!["in"],
209        _ => vec!["out"],
210    };
211    let (user_select, out_names) = build_user_select(&args.select)?;
212
213    let mut queries = Vec::new();
214    let mut docs: Vec<Doc> = Vec::new();
215    let mut edges: Vec<Json> = Vec::new();
216    let mut query_truncated = false;
217    for dir in &dirs {
218        let q = build_query(&root_ids, dir, walk_depth, &user_select);
219        queries.push(q.clone());
220        let res = query(
221            store,
222            repo_id,
223            &q,
224            QueryOptions {
225                limit: Some(max_documents + 1),
226                cursor: None,
227                provider,
228                in_memory: false,
229                bare: false,
230                unknown_names: None,
231                vocabulary: None,
232                now_ms,
233            },
234        )?;
235        if res.truncated {
236            query_truncated = true;
237        }
238        for hit in &res.hits {
239            // The walk's hits carry `id`/`path` (§1.4, 3.0), and so does
240            // `graph`'s own envelope (§4): one spelling for a document's identity.
241            let id = hit["id"].as_str().unwrap_or_default().to_owned();
242            let path = hit["path"].as_str().unwrap_or_default().to_owned();
243            let hop = hit["_depth"].as_f64().unwrap_or(f64::NAN);
244            let degree = (hop - 1.0) as i64;
245            let replace = docs
246                .iter()
247                .position(|d| d.id == id)
248                .map(|i| (i, degree < docs[i].degree));
249            match replace {
250                Some((_, false)) => {}
251                found => {
252                    let extra: Vec<(String, Json)> = out_names
253                        .iter()
254                        .enumerate()
255                        .filter_map(|(i, n)| {
256                            n.as_ref().map(|name| {
257                                (
258                                    name.clone(),
259                                    hit.get(format!("_u{i}")).cloned().unwrap_or(Json::Null),
260                                )
261                            })
262                        })
263                        .collect();
264                    let doc = Doc {
265                        id: id.clone(),
266                        path,
267                        degree,
268                        extra,
269                    };
270                    match found {
271                        Some((i, true)) => docs[i] = doc,
272                        _ => docs.push(doc),
273                    }
274                }
275            }
276            if let Some(raw) = hit["_links"].as_array() {
277                for e in raw {
278                    let eid = e["id"].as_str().unwrap_or_default();
279                    if !edges.iter().any(|x| x["id"].as_str() == Some(eid)) {
280                        edges.push(e.clone());
281                    }
282                }
283            }
284        }
285    }
286
287    if let Some(p) = &args.predicate {
288        let mut adj: HashMap<String, Vec<String>> = HashMap::new();
289        for e in &edges {
290            if e["predicate"].as_str() != Some(p.as_str()) {
291                continue;
292            }
293            let src = e["doc_id"].as_str().unwrap_or_default().to_owned();
294            let dst = e["dst_id"].as_str().unwrap_or_default().to_owned();
295            if dirs.contains(&"out") {
296                adj.entry(src.clone()).or_default().push(dst.clone());
297            }
298            if dirs.contains(&"in") {
299                adj.entry(dst).or_default().push(src);
300            }
301        }
302        let mut depth_of: Vec<(String, i64)> = root_ids.iter().map(|id| (id.clone(), 0)).collect();
303        let mut wave: Vec<String> = root_ids.clone();
304        let mut lvl = 1;
305        while lvl <= effective_degrees && !wave.is_empty() {
306            let mut next = Vec::new();
307            for from in &wave {
308                for to in adj.get(from).map_or(&[][..], Vec::as_slice) {
309                    if docs.iter().any(|d| &d.id == to) && !depth_of.iter().any(|(id, _)| id == to)
310                    {
311                        depth_of.push((to.clone(), lvl));
312                        next.push(to.clone());
313                    }
314                }
315            }
316            wave = next;
317            lvl += 1;
318        }
319        let mut restricted: Vec<Doc> = Vec::new();
320        for (id, d) in depth_of {
321            if let Some(orig) = docs.iter().find(|x| x.id == id) {
322                restricted.push(Doc {
323                    id: orig.id.clone(),
324                    path: orig.path.clone(),
325                    degree: d,
326                    extra: orig.extra.clone(),
327                });
328            }
329        }
330        docs = restricted;
331    }
332
333    docs.sort_by(|a, b| {
334        a.degree
335            .cmp(&b.degree)
336            .then_with(|| locale_compare(&a.path, &b.path))
337    });
338    let capped = docs.len() > max_documents;
339    docs.truncate(max_documents);
340    let truncated = query_truncated || capped;
341
342    let mut frontier = Vec::new();
343    let documents: Vec<Json> = docs
344        .iter()
345        .map(|d| {
346            let is_frontier = d.degree == effective_degrees;
347            if is_frontier {
348                frontier.push(json!({ "id": d.id, "path": d.path, "degree": d.degree }));
349            }
350            let mut m = Map::new();
351            m.insert("id".to_owned(), json!(d.id));
352            m.insert("path".to_owned(), json!(d.path));
353            m.insert("degree".to_owned(), json!(d.degree));
354            m.insert("frontier".to_owned(), json!(is_frontier));
355            for (k, v) in &d.extra {
356                m.insert(k.clone(), v.clone());
357            }
358            Json::Object(m)
359        })
360        .collect();
361
362    let reached = |id: &str| docs.iter().any(|d| d.id == id);
363    let mut kept: Vec<Json> = edges
364        .into_iter()
365        .filter(|e| {
366            if let Some(p) = &args.predicate {
367                if e["predicate"].as_str() != Some(p.as_str()) {
368                    return false;
369                }
370            }
371            let src_in = reached(e["doc_id"].as_str().unwrap_or_default());
372            let dst_kind = e["dst_kind"].as_str().unwrap_or_default();
373            let dst_dangling =
374                dst_kind == "external" || (dst_kind == "document" && e["dst_path"].is_null());
375            let dst_in = reached(e["dst_id"].as_str().unwrap_or_default()) || dst_dangling;
376            src_in && dst_in
377        })
378        .collect();
379    kept.sort_by(|a, b| {
380        let (sa, sb) = (
381            a["doc_id"].as_str().unwrap_or_default(),
382            b["doc_id"].as_str().unwrap_or_default(),
383        );
384        if sa == sb {
385            locale_compare(
386                a["id"].as_str().unwrap_or_default(),
387                b["id"].as_str().unwrap_or_default(),
388            )
389        } else {
390            locale_compare(sa, sb)
391        }
392    });
393
394    Ok(json!({
395        "roots": root_ids,
396        "degrees": effective_degrees,
397        "direction": direction,
398        "documents": documents,
399        "links": kept,
400        "frontier": frontier,
401        "truncated": truncated,
402        "queries": queries,
403    }))
404}
405
406#[cfg(test)]
407mod tests {
408    use super::*;
409
410    #[test]
411    fn user_select_aliases() {
412        let (items, names) = build_user_select(&[
413            "layer".into(),
414            "path".into(),
415            "a + 1".into(),
416            "layer".into(),
417        ])
418        .unwrap();
419        let printed: Vec<String> = items
420            .iter()
421            .map(|it| oqx::print(oqx::Node::Select(it)).unwrap())
422            .collect();
423        assert_eq!(
424            printed,
425            ["_u0: layer", "_u1: path", "_u2: a + 1", "_u3: layer"]
426        );
427        assert_eq!(
428            names,
429            [
430                Some("layer".into()),
431                Some("path".into()),
432                Some("sel_2".into()),
433                Some("layer_3".into())
434            ]
435        );
436        assert!(build_user_select(&[]).unwrap().0.is_empty());
437        let e = build_user_select(&["a +".into()]).unwrap_err();
438        assert_eq!(e.code, "filter_invalid");
439    }
440
441    #[test]
442    fn query_shape() {
443        let q = build_query(&["d_0".to_owned()], "out", 2, &[]);
444        assert!(
445            q.starts_with("select _depth: $depth, _stop: $stop, _links: doc.out_links collect {")
446        );
447        assert!(q.ends_with(
448            "from docs where id == \"d_0\" follow distinct out_links collect { from dst depth 2 }"
449        ));
450        assert!(oqx::parse_string(&q).is_ok());
451        // several roots: an `or` seed (the canonical print of the `or` node since
452        // oqx 0.18); the generated text is exactly the macro's pinned shape
453        // (spec/surface `reads.json`)
454        let q = build_query(&["d_14".to_owned(), "d_15".to_owned()], "in", 8, &[]);
455        assert_eq!(
456            q,
457            "select _depth: $depth, _stop: $stop, _links: doc.in_links collect { id: id, doc_id: doc_id, block_id: block_id, via_id: via_id, dst_id: dst_id, dst_path: dst_path, uri: uri, dst_kind: dst_kind, predicate: predicate, provenance: provenance, anchor: anchor, field: field } from docs where id == \"d_14\" or id == \"d_15\" follow distinct in_links collect { from doc depth 8 }"
458        );
459    }
460
461    #[test]
462    fn order_is_bytewise() {
463        assert_eq!(locale_compare("B", "a"), Ordering::Less);
464        assert_eq!(locale_compare("a", "b"), Ordering::Less);
465    }
466}