@omgbase/core 0.24.0 → 0.27.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/src/core/edit-script.d.ts +15 -0
  2. package/dist/src/core/edit-script.js +72 -0
  3. package/dist/src/core/ingest.js +42 -12
  4. package/dist/src/core/read/document.d.ts +7 -0
  5. package/dist/src/core/read/document.js +38 -17
  6. package/dist/src/core/store/archive.d.ts +28 -0
  7. package/dist/src/core/store/archive.js +106 -17
  8. package/dist/src/core/store/delta.d.ts +131 -0
  9. package/dist/src/core/store/delta.js +470 -0
  10. package/dist/src/core/store/gc.d.ts +10 -0
  11. package/dist/src/core/store/gc.js +23 -7
  12. package/dist/src/core/store/retention.d.ts +1 -1
  13. package/dist/src/core/store/retention.js +3 -17
  14. package/dist/src/core/store/schema.d.ts +1 -1
  15. package/dist/src/core/store/schema.js +2 -1
  16. package/dist/src/core/store/store.js +7 -0
  17. package/dist/src/core/store/writers.d.ts +42 -6
  18. package/dist/src/core/store/writers.js +67 -25
  19. package/dist/src/format/json.js +5 -0
  20. package/dist/src/format/yaml.js +15 -3
  21. package/dist/src/graph/history.d.ts +1 -15
  22. package/dist/src/graph/history.js +14 -74
  23. package/dist/src/mcp/arguments.d.ts +11 -0
  24. package/dist/src/mcp/arguments.js +47 -4
  25. package/dist/src/mcp/errors.js +1 -1
  26. package/dist/src/mcp/reference.d.ts +1 -1
  27. package/dist/src/mcp/reference.js +1 -1
  28. package/dist/src/mcp/server.js +4 -2
  29. package/dist/src/mutate/effects.js +87 -13
  30. package/dist/src/mutate/frontmatter-parts.d.ts +71 -0
  31. package/dist/src/mutate/frontmatter-parts.js +237 -0
  32. package/dist/src/mutate/load.js +9 -9
  33. package/dist/src/mutate/select.js +9 -3
  34. package/dist/src/oqx-js/context.js +21 -10
  35. package/dist/src/oqx-js/names.js +1 -1
  36. package/dist/src/oqx-js/run.d.ts +8 -0
  37. package/dist/src/oqx-js/run.js +91 -4
  38. package/dist/src/oqx-js/sql/translate.js +21 -1
  39. package/dist/src/search/cel/parser.d.ts +3 -1
  40. package/dist/src/search/cel/parser.js +4 -1
  41. package/dist/src/spec-versions.d.ts +4 -4
  42. package/dist/src/spec-versions.js +4 -4
  43. package/package.json +1 -1
@@ -1 +1 @@
1
- export declare const QUERY_SYNTAX = "# query \u2014 OQX syntax reference\n\nThe `query` tool takes ONE plain OQX string (+ optional `limit`/`cursor`, and\n`bare: true` to drop the automatic `id`/`path` envelope \u2014 needs a `select`).\nThere is no {from, filter, order} envelope: every concern is a clause of the\nstring. Two rules tell the two syntaxes apart: DOT navigation belongs to the\nhost object model (`doc.layer`, `section.blocks`, `dst.title`); WHITESPACE\nquery directives belong to OQX \u2014 a nested query is `<receiver> collect|exists|\ncount|first|single { <block> }` (a postfix directive + a `{ \u2026 }` block), never a\nmethod. Its distinctive power is the receiver-constrained nested query that\ncorrelates to the current row: `from docs where nodes exists { where kind ==\n\"md:task\" }` returns only the docs that themselves contain a matching node.\n\n [select <items>] from <target> [where <pred>] [frontier <pred>] [follow <dest> [{ \u2026 }], \u2026]\n [flatten <expr> [{ \u2026 }] [as <name>]]\u2026 [group by <key>, \u2026] [having <pred>]\n [order by <expr> [asc|desc], \u2026] [limit N] [offset N]\n <target> count|exists|none|first|single { <block> } (scalar/one-row form: a bare target at the root scope)\n\nCLAUSE ORDER IS FIXED: select, from, where, frontier, follow, flatten, group by, having, order by, limit, offset \u2014\neach at most once (`flatten` may repeat); an out-of-order clause is a parse error naming the order.\nOnly `select` may drop its keyword, and only as the first clause\n(`path, era from docs where \u2026`). A top-level predicate needs `where`, because\n`where` follows `from` (`era > 1600 from docs` is the clause-order error); a\nblock may LEAD with a predicate (see Sugar), but a predicate after a projection\nstill needs the keyword (`{ name where kind == \"md:task\" }`, not\n`{ name, kind == \"md:task\" }`); `from docs count` is an error (write\n`docs count { \u2026 }`). `where` and later `select` items may use the same body's\n`select` aliases (`select path, old: era < 1000 from docs where old`; an alias\nshadows a same-named field there). `and` / `or` / `not` are the connectives;\nthe symbols `&&` / `||` / `!` are accepted synonyms (the words are canonical and\nwhat `print` writes).\n\n## Sugar (OQX 0.17)\n\nEvery form here is shorthand for an explicit directive (the AST is the explicit form):\n nodes { kind == \"md:task\" } \u2261 nodes collect { where kind == \"md:task\" } \u2014 a receiver block with\n no consumer is collect; a block whose LEADING expression is a predicate\n (comparison, call, !/is/not, in, literal, (\u2026), a consumer test \u2014 anything\n but a bare name, a dotted path or a ^lift) is where-first. Bare names still\n project: nodes { name } \u2261 nodes collect { name }; filter a bare field with\n nodes { is checked }. Wherever a value stands (0.20), call arguments included:\n size(children { is checked }); x distinct { \u2026 } \u2261 x collect distinct { \u2026 }. After a\n follow destination too: follow children { depth 2 } is that destination's block.\n (a, b) a tuple (0.20): two or more expressions, concatenated one level flat \u2014 a value\n (size((tags, aliases)), \"x\" in (tags, aliases)) and a receiver ((a, b) { \u2026 },\n follow (before, after) { depth 2 }); (a) alone is grouping\n refs(x)[0] \u2261 refs(x) first { offset 0 } positional (integer literal or ${binding});\n out of range \u21D2 absent; refs(x)[0].path navigates the row\n ^docs[path == ^company] \u2261 ^docs first { where path == ^company } first match or absent\n ^docs[path == ^company]! \u2261 \u2026 single { \u2026 } required: exactly one, else filter_invalid\n title! required: title, or an error naming the expression (and the row's $key \u2014 its id)\n \u2014 never a filter, never a coercion (0!, \"\"! are values); tightest: a!.b vs a.b!\n is x / not x truthiness of x, and its negation (prefix; `not` \u2261 `!`)\n x is y / x is not y identity ($key: a store row's id, else structural) \u2014 compares rows, which == does not;\n x is null = absent; for scalars is \u2261 ==; comparison precedence, no chaining\n a and b / a or b the connectives (\u2261 && / ||: same precedence, short-circuit, value: data.title or path coalesces)\n boss: ^docs[path == ^manager], bossName: boss.title a select item may use the items to its LEFT\n Reserved words (never a bare field name): from where select is not and or, true false null.\n Contextual words (limit order offset follow collect count first distinct desc by in frontier depth group having\n flatten as) parse as fields in expression position (select o: order from docs).\n\nResults are LEAN hits \u2014 { id, path } + whatever `select` projects (or `values`,\n`count`, `exists`, `none`). The envelope keys ARE the attributes: copy a hit's\n`id` straight into `where id == \"\u2026\"` (no translation); `select id, path` is simply\nthe envelope (no duplicate columns); any OTHER projection whose output key would\nbe `id` or `path` must be aliased \u2014 `select data.path` is refused (\"projection\n`path` collides with the hit envelope \u2014 alias it (`p: data.path`)\"); a row\nsurfacing as a value (an empty `collect { }`) is the same `{ id, path }`. Every\nother tool spells a row the same way (`docs_read` \u2192 `{ id, path, rev, properties,\nraw }`, `blocks_read` \u2192 `{ id, type, text, raw, hash, \u2026 }`, `docs_list` \u2192 `{ id,\npath, updated_at, blocks }`, a revision \u2192 `{ id, path, hash }`): hydrate by id via\nblocks_read/docs_read.\n\n## Paths\n\nEvery path the surface returns is `/`-rooted \u2014 the form a reference is\nwritten in (`[x](/projects/oqx.md)`, `before: [/timeline/kickoff.md]`): a\nhit's `path`, an edge's `dst_path`, every tool's paths. Every path it\naccepts tolerates both forms (`/a.md` or `a.md`): tool arguments, `refs(x)`,\n`within(...)`, and a string LITERAL compared with `path`/`dst_path` (bare, or\nthrough a row handle: `doc.path`) by `==`/`!=` or passed to their\n`.startsWith(...)` (it is rooted first, so `path == \"a.md\"` and `path == \"/a.md\"`\nboth match). A property holding a reference compares directly \u2014 and is never\nrooted: `where ^path in list(after)`, `where customer == ^path`,\n`where data.path == \"/x.md\"` (the author's `path:` key, as written) \u2014 no\n`\"/\" + ...` glue.\n\n## Targets & field namespaces\n\nTHE NAME RULE: `$` belongs to OQX \u2014 $it, $key, $depth, $stop, $leaf, $frontier,\n$ordinal are values the LANGUAGE injects into a scope; omgbase adds NO $ names\n(a `$id` in a query is an unknown name, absent). Every engine attribute is a BARE\nsnake_case name that WINS on collision \u2014 the convention the relations (children,\nblocks, out, in, doc, frontmatter, \u2026) always followed: a bare name that is an\nengine attribute reads the engine's value, never yours. Your data is bare too;\nwhere a key of yours collides with an engine name, reach it through a source\nhandle: `data.<k>` (the merged authored view \u2014 frontmatter + inline, exactly the\nread a bare non-colliding key performs), `frontmatter.<k>`, `inline.<k>`. So\n`path` is the engine's /-rooted location and `data.path` your `path:` key;\n`tags` is your tags \u222A the body #hashtags and `data.tags` the authored list alone;\n`title` the computed title (the first H1) and `data.title` your `title:`;\n`entries(data)` lists every authored top-level key. Same value, same name\neverywhere: a hit's `id` is docs_read's `id` and the `id` argument of blocks_read.\n\n Reserved bare names per kind (engine attributes; handles; self; relations):\n docs id path raw hash updated_at created_at title tags format | data frontmatter inline | doc | blocks nodes out_links in_links revisions\n blocks id path doc_id raw hash updated_at created_at type text attrs depth ordinal | block doc | children nodes out_links section\n nodes id path doc_id block_id kind name value attrs | section doc block | blocks subsections children\n links id path doc_id block_id via_id dst_id dst_path uri field dst_kind predicate provenance anchor | doc\n revisions id path rev seq doc_id commit ts origin actor reason hash is_current archived | doc\n A key of yours with any of these names is `data.<k>` (or frontmatter.<k> /\n inline.<k> by source; attrs.<k> on a block or node).\n UNKNOWN NAMES: a name that is none of the above AND that no object of the\n kind in this repository carries is reported before any row is read \u2014 on\n links as a filter_invalid error naming the fields; on docs/blocks/nodes the\n result gains `hints` (one line per name, nearest match when there is one)\n and the rows still come back; in a plan/apply/execute selector it is refused.\n A property some objects carry is known (partial presence is normal).\n\ndocs:\n - bare identifier = a document PROPERTY (frontmatter + inline `key:: value`,\n unioned; nested via dots: meta.owner). YAML/JSON docs\n expose the parsed object's keys the same way.\n - source handles = data.<k> (merged authored view) / frontmatter.<k> /\n inline.<k>; the whole bag as a collection via entries(data)\n / entries(frontmatter) / entries(inline) (see entries()).\n - attributes = id, path (/-rooted, see Paths), updated_at (ISO-8601,\n sorts chronologically), raw (the whole file, frontmatter\n included), hash (its sha256, 64 hex), title (the computed\n title: the first H1; data.title is the authored key), tags\n (authored tags \u222A body #hashtags, deduped, authored first;\n data.tags is the authored list), format (the doc's format \u2014\n \"markdown\"/\"yaml\"/\"json\"; present on EVERY doc, so\n has(format) is always true and `format == \"yaml\"` is the\n filter).\n - relations = nodes, blocks, doc.out_links / doc.in_links (a doc's\n links as rows; the documents at their other end are\n out_links collect distinct { from dst } and\n in_links collect distinct { from doc } \u2014 there is no\n out / in relation), revisions (the document's stored\n revisions, oldest first \u2014 see revisions below).\n\nblocks:\n - bare fields = doc_id (the owning doc), type, text (normalized), ordinal\n (0-based among siblings), depth (0 at the top level), and\n every attrs key FLATTENED onto the row (bare `checked`\n reads attrs.checked; `attrs.<k>` still works; the\n structural fields win on collision; absent \u21D2 false).\n - attributes = id, path (the owning doc's path), raw (the block's raw\n source \u2014 a container's raw holds its children), hash\n (its sha256 \u2014 the value update/split expect as\n expect.content_hash), updated_at.\n - reach-through = doc.<key> / doc.path / doc.id / doc.format = the\n CONTAINING doc (doc.id and doc.path cost nothing \u2014 they\n read the row's own doc_id / joined path, no lookup).\n - relations = block.children, block.nodes, block.out_links, section.\n NOTE: inside a `follow`, $depth/$ordinal are ALWAYS the walk's metadata; a\n block's own nesting is `depth` / `ordinal` (no $) everywhere.\n\nnodes:\n - bare fields = doc_id, block_id (null for a block-less node), kind\n (md:task / md:link / md:section / yaml:\u2026), name, value,\n + attrs keys flattened (checked, level).\n - attributes = id, path.\n - reach-through = doc.<key> / doc.id / doc.path, block.type / block.text\n / block.id / block.path.\n - relations = section.blocks, section.children, section.subsections.\n\nlinks: (the authored link graph as rows \u2014 one per open link: the relation a link\n establishes; the md:link NODE is its occurrence in the text and via_id\n points at it \u2014 a frontmatter link has a row and no node)\n - bare fields = doc_id (the SOURCE doc), block_id (the source block; null\n for a frontmatter edge), via_id, dst_id (the destination\n node id: a doc id, an external node id or phantom:<path>),\n dst_path (target doc path, /-rooted; NULL when\n dangling/external), uri (external URL; NULL otherwise),\n predicate (\"references\"/\"embeds\"/freeform), provenance\n (\"link\"/\"frontmatter\"/\"inline_field\"/\u2026), dst_kind\n (\"document\"/\"external\"/\"collection\"), anchor, field (the\n frontmatter key / inline field it was authored in).\n - attributes = id, path (the SOURCE doc's path); doc.<key> reaches the\n source document. There is no from_commit: a query sees\n open edges only (history is blocks_history/changes_since).\n - text()/semantic() are N/A on links.\n\nrevisions: (history as rows \u2014 one per stored revision of every document of the\n repository, deleted documents included: what docs_history lists, queryable)\n - bare fields = rev (= id), seq (1, 2, \u2026 per document), doc_id, commit,\n ts (the commit's time, sorts chronologically), origin\n (\"observed\"/\"api\"/\"import\"/\"projection\"), actor, reason,\n hash (the rendered file's sha256 \u2014 docs_history's hash),\n is_current (true for the document's current revision),\n archived (true when the bodies moved to an archive segment:\n the row stays, docs_read { at } reads through the segment).\n - attributes = id (the revision id, r_\u2026), path (the path the revision\n was committed under \u2014 a move re-paths only the current\n revision, so earlier ones keep the old path). doc.<key>\n reaches the document's CURRENT row; absent for a deleted\n document, so where doc == null is the revisions of deleted documents.\n - on docs = revisions is a relation (oldest first): a document with\n more than one version is docs { revisions count { } > 1 }.\n - text()/semantic() are N/A on revisions. The body as of a revision is\n docs_read { path, at: \"<rev>\" } (docs_read_at is the same call).\n\n## Scoping (ADR-015; the root row since surface 2.0): bare names are LOCAL\n\n name the CURRENT row only \u2014 never falls through to an enclosing row\n or the repository; an absent field is simply absent.\n ^name one scope OUT (the enclosing row's fields/intrinsics/lifts);\n ^^name two scopes out. `^path` = the enclosing row's path.\n ^docs ^nodes ^blocks ^links ^revisions\n THE ROOT ROW (surface 2.0, oqx 0.18): the repository is the root\n scope's row, one scope out from a top-level row, so ^docs there is\n the whole documents scan (uncorrelated until you add a ^ predicate);\n one caret per enclosing block (^^docs from depth two), or the\n absolute 0^docs from ANY depth. ^id / 0^id = the repository id.\n ^$it = the root object: ^$it.docs \u2261 ^docs, entries(^$it) names the\n five collections. At the root scope itself a bare target is the scan\n (docs count { \u2026 }, from docs, entries(docs)).\n ERRORS: a bare `docs` INSIDE a block reads a property of the current\n row, which has none \u2014 refused (\"did you mean ^docs\"); `^docs` AT THE\n TOP LEVEL reaches past the root \u2014 refused (write the bare `docs`);\n so is `$repo` (surface < 2.0; the message names the replacement).\n nodes/blocks on a doc row are that doc's RELATIONS, not the root.\n $it the current item itself (a row, or each ELEMENT when the\n receiver is a list property); ^$it = the enclosing row.\n $value is NOT an intrinsic (since oqx 0.15): it reads a\n property literally named $value, absent when there is none.\n $key the row's identity (0.20): a store row's id (doc/block/node/edge), an entries(x)\n entry's key, a follow occurrence's walk identity; project $key: <expr> to rekey the\n rows a block yields (a destination block's $key: is not a column).\n\n## Scalar semantics (@omgbase/oqx)\n\n comparisons == != < <= > >= strict & typed (5 == \"5\" is false); either\n side may be a field (field-vs-field OK).\n absence null and a missing field are the same \"absent\" value; two absents\n are EQUAL, so `absent != \"x\"` is TRUE and `absent == null` is TRUE.\n Relational (< <= > >=) on an absent operand \u2192 FALSE. Bare `f` in\n boolean position uses JS truthiness (absent/false/0/\"\" \u21D2 false;\n NOTE: \"false\", [] and {} are truthy). has(f) = explicit presence.\n boolean and or not grouping ( \u2026 ) \u2014 && || ! are accepted synonyms\n arithmetic + - * / % (+ concatenates if either side is a string)\n membership x in y array \u2192 typed membership; string \u2192 substring;\n object \u2192 key exists; range \u2192 coverage. Any list\n property works directly: \"pricing\" in tags. list(f)\n coerces scalar-or-list (absent \u2192 []) \u2014 use it when a\n key is sometimes scalar, sometimes a list.\n ranges lo..hi (inclusive) lo...hi (exclusive hi) ..hi lo..\n era in 800..1680; works over ISO dates. range(prop) reads a\n range-shaped STRING property as an interval: \"2026-01-15\" in range(window).\n strings x.contains(\"s\") x.startsWith(\"p\") x.endsWith(\"s\") x.matches(\"^re$\")\n (real regex) x.lower() x.upper() size(x). ALL CASE-SENSITIVE \u2014\n fold with .lower(): title.lower().contains(\"aurora\").\n Infix regex: x ~ \"re\" is x.matches(\"re\"); x ~* \"re\" is the\n case-insensitive match; x !~ \"re\" and x !~* \"re\" negate them.\n Unanchored unless the pattern anchors; an absent x is false\n under ~ and TRUE under !~ (not SQL's NULL).\n functions has(f) size(x) list(x) entries(x) range(s)\n dates a date is an ISO string, a duration a NUMBER OF SECONDS; arithmetic\n is METHODS, never + or - (+ concatenates strings).\n date(\"today\"|\"now\"|\"yesterday\"|\"tomorrow\"|\"sow\"|\"eow\"|\"som\"|\"eom\"|\n \"soy\"|\"eoy\") \u2014 the host clock, UTC; date(\"2026-10-15\") canonical ISO\n (absent if unparseable). dur(\"1h30m\") dur(\"2 days, 3 hours\")\n dur(\"P1DT2H\") dur(90) \u2192 seconds (year = 365 d, month = 30 d).\n d.plus(dur(\"30 days\")) d.minus(86400) d.plus(\"1 month\") (calendar:\n Jan 31 \u2192 Feb 28) a.until(b) \u2192 signed seconds d.year() d.month()\n d.day() d.weekday() (1 = Mon) d.hour() d.minute() d.second()\n d.date() d.format(\"EEE dd MMM yyyy\") n.dur_text() \u2192 \"1h 30m\"\n n.dur_iso() \u2192 \"PT1H30M\". Total: a non-date is absent, so\n `where date(due) < date(\"today\").minus(dur(\"30 days\"))` just\n filters. A datetime is never < its own date: compare\n updated_at.date() == date(\"today\"), or < date(\"tomorrow\").\n literals \"str\" 'str' 123 1.5 true false null\n determinism no random; the only clock is date(\"today\"/\"now\"/\u2026), read once per\n query. Lists/structs are not literals; ternary is not supported.\n Errors surface as filter_invalid.\n scalars vs lists a list-authored key never == a scalar (tags == \"x\" is false\n when tags is a list) \u2014 use `\"x\" in tags` / `in list(tags)`.\n\n## Domain predicates (omgbase)\n\n text(\"terms\") FTS5 PRUNING predicate (docs match via their blocks, nodes\n via the node index); composes anywhere, incl. nested scopes.\n semantic(\"phrase\") embedding cosine SCORE (docs/blocks): threshold it\n (semantic(\"x\") > 0.6), project it, or `order by semantic(\"x\")\n desc` for top-K. Needs a provider that has embedded the repository; else provider_unavailable.\n blocks target only:\n under(id_or_heading) block in the containment subtree / section range\n under_heading(\"Launch\") an ancestor section heading contains the text (ci)\n under_kind(type[, name]) an ancestor block has that type (name: its text\n contains / its key equals name)\n within(\"path\" | \"d_..\") containing doc = id, exact path, or path glob (*);\n either path form\n yaml_path(\"a.b\") YAML docs: the block at that key path\n json_pointer(\"#/a/b\") JSON docs: the block at that pointer\n has_edge(\"pred\"[, target]) an open authored edge with that predicate leaves here\n has_anchor() block carries an authored ^ref\n parent_type() == \"x\" parent block's type (must be compared)\n child_count() > 0 number of direct children (must be compared)\n refs(x) the live documents a property's document references name:\n x is a string, a list or absent; each \"/a/b.md\", \"a/b.md\"\n or \"d_\u2026\" element resolves to that doc's row; dangling and\n non-string elements are dropped. Yields docs rows, so it is\n a source, a receiver or a follow destination:\n follow refs(before), refs(after) walks a timeline both ways.\n The reverse direction needs no function: a document's\n path is already the reference form, so\n ^docs { where ^path in list(after) } is\n \"the documents whose `after` names me\".\n A HIT IS A STORE ROW: a top-level row that is not a doc/block/node/edge fails\n (filter_invalid). `follow before` over a list of paths\n reaches the STRINGS \u2014 write `follow refs(before)`.\n\n## Nested queries & consumers\n\n <receiver> collect|exists|none|count|first|single { <block> }\n receiver = a relation (nodes, blocks, doc.out_links, section.blocks, \u2026), a\n reach-through row (dst, doc: one row \u2014 dst { title }), a\n list property (tags), entries(x), or a root collection (^docs; 0^docs at depth \u2265 2).\n block = [select \u2026] [from E] [where \u2026] [frontier \u2026] [follow \u2026] [flatten \u2026] [group by \u2026] [having \u2026] [order by \u2026] [limit N] [offset N]\n \u2014 the same fixed order as the top level (`from` optional; the\n receiver supplies the rows). A leading name/alias list is the\n projection (`select` dropped); a predicate always needs `where`.\n A `from E` inside a block RE-PROJECTS the rows through a relation\n (a typed flatMap): `docs collect { from nodes where kind == \"md:task\" }`\n starts at docs, projects each through `nodes`, and queries the\n resulting nodes; a scalar relation (`from doc`) contributes one\n row; a missing or non-navigable relation is a loud error, never a\n global scan.\n exists \u22651 row nodes exists { where kind == \"md:task\" and not checked }\n none 0 rows nodes none { where kind == \"md:task\" and not checked } (\"every task done\")\n count a number nodes count { where kind == \"md:task\" } >= 2\n collect rows (default) \u2014 in select for nested results; in where with a lift\n first / single zero-or-one (single errors on >1) \u2014 lookups in select\n distinct on any consumer: dedup by projected value \u2014 nodes count distinct { select kind }\n limit/offset bound the block's rows AFTER where/order/distinct, BEFORE the\n consumer reduces: nodes exists { offset 1 } = at least two;\n first { \u2026 offset 1 } = the second.\n lift `^name:` in a where-collect binds values outward:\n select path, open from docs where nodes collect { ^open: value where kind == \"md:task\" and not checked }\n joins ^<t> exists { where slug == ^ref } semi-join; not \u2026 exists anti-join;\n ^nodes single { where kind == \"person\" and attrs.id == ^owner_id } lookup;\n x in ^keys membership over a lifted set.\n\n## select \u2014 projection\n\n select a, alias: expr, nested: nodes collect { \u2026 }\n bare key \u2192 the attribute when it is one (id, path, title, \u2026), else the property\n (scalar-authored \u2192 scalar; list \u2192 array); data.<k> forces the property;\n a call/arithmetic needs an alias (n: size(tags)) unless in values mode;\n an output key of id/path other than the attribute itself needs an alias.\n raw whole file bytes (docs) \u2014 docs_read is cheaper for one doc.\n distinct select distinct type (dedup hits by projected value)\n values select <ONE item> values \u2192 bare values instead of records:\n select distinct type values from docs \u2192 values: [\"hub\",\"lab-note\",\u2026]\n select path values from docs \u2192 values: [\"/index.md\", \u2026]\n tags: tags collect { $it values } \u2192 a plain array\n h: nodes first { name values where kind == \"md:section\" order by first_ordinal }\n entries(x) a record as a collection ($key / $it per entry), key order:\n select fm: entries(data) collect { k: $key, v: $it }\n where entries(frontmatter) exists { where $key == \"era\" and $it > 1600 }\n entries(inline), entries(attrs), a nested map, or a list (index keys)\n $semantic_score is not a field \u2014 project semantic(\"\u2026\") under an alias instead.\n\n## flatten (OQX 0.25)\n\n flatten <expr> [{ \u2026 }] [as <name>] after follow, before group by; may repeat\n Splits one value into rows. Every hit is still the PARENT row \u2014 its id/path\n repeat per element, as Dataview's TABLE \u2026 FLATTEN \u2014 with <name> bound beside\n its fields at the same depth: an array \u2192 one row per element (an empty list\n yields none), a missing field \u2192 one row with the name absent (never dropped),\n an object or a scalar \u2192 one row with the value. A plain navigation names\n itself (flatten tags rebinds tags to each element); anything else needs as.\n The block is the ordinary receiver block over the elements (where, a\n projection, order by, limit, distinct), applied first. Later clauses read the\n name \u2014 a later flatten (flatten jobs as j flatten j.tags as t), group by,\n having, select, order by; where runs before it. A flattened query pages by\n position (cursor null); as id / as path is refused (the envelope reads them).\n select path, tags from docs where type == \"process\" flatten tags order by path, tags\n select path, t: b.text from docs flatten blocks { where type == \"task\" and not checked } as b\n select tags, n: count() from docs flatten tags group by tags order by count() desc\n\n## order by, limit/offset, pagination\n\n order by era desc, path asc absent values sort LAST (asc); ties \u2192 (path, id)\n limit N / offset N define the result SET (integer literals);\n the tool's limit/cursor then page WITHIN it\n cursor opaque keyset on (path,id); a custom order by\n disables it (you get the top page + truncated)\n\n## Graph traversal (follow) and links\n\n The citation graph is the LINK ROWS walked through a destination block:\n dst is a link's destination document (absent for an external or dangling\n link \u2014 it then contributes no successor), doc its source.\n from docs where <seed> follow out_links collect { from dst } docs the seed links TO\n from docs where <seed> follow in_links collect { from doc } backlinks\n follow out_links collect { from dst depth 3 } bound (1..8, default 8) \u2014 a clause of the block\n follow out_links collect { from dst where layer == \"canon\" } keep only matching successors\n follow in_links collect { from doc where ^path in list(before) } ^ = the row being expanded\n (^^ = the walk's enclosing scope)\n follow out_links collect { from dst where predicate == \"depends_on\" } one predicate only\n follow out_links collect { from dst }, in_links collect { from doc }\n several destinations: their union\n (one step dedups by identity)\n follow ^docs { where out_links exists { where dst_path == ^^path } }\n a destination block over the root's\n documents, re-read per frontier row\n (^ = that row) = the backlinks walk\n follow out_links collect { from dst frontier type == \"practitioner\" } cut, keeping the frontier row\n from docs where type == \"process\" frontier !verified follow out_links collect { from dst }\n the BODY's frontier applies to the seeds:\n an unverified seed is kept, not expanded\n follow distinct out_links collect { from dst } / \u2026 { $key: group from dst } identity control ($key)\n follow refs(before), ^docs { where ^path in list(after) } forward over `before`, backward\n over every doc whose `after` names me\n Per-occurrence metadata: $depth (seed = 1), $stop (interior|leaf|frontier|\n depth|cycle), $leaf/$frontier, $ordinal (a deterministic 1..N rank over the\n walk, ordered by depth then path \u2014 `order by $ordinal` for walk order,\n `where $ordinal <= N` for a deterministic budget cut), $key \u2014 usable in\n select/order by and the top-level post-walk where (a where conjunct that\n reads one filters the walk's RESULT; the other conjuncts remain the seed\n predicate), NOT metadata of the candidates a destination block's where reads,\n nor valid inside a consumer directive. Cyclic graphs are safe: a revisited\n node is admitted once as a `$stop == \"cycle\"` occurrence and never\n re-expanded. The default keeps one occurrence per distinct walk path;\n `follow distinct` dedups reached rows by identity.\n Destination blocks (OQX 0.20): a brace after ANY destination opens that\n destination's block \u2014 children { depth 2 }, refs(x) { depth 2 }, out_links\n collect { from dst depth 2 }, ^docs { where ^path in list(after) } \u2014 with the\n 0.17 body rules (where-first, projections) and two clauses of its own after\n `where`: frontier <pred> (occurrences reached through this destination that\n match are frontiers; ^ there is the walk's enclosing scope) and depth <n>\n (this destination expands a frontier row only while $depth < n). The body\n that carries the follow may write frontier <pred> too, after its where and\n before follow: it applies to the SEEDS (read like the body's where) \u2014 a\n matching seed stays in the result with $stop == \"frontier\" and is not\n expanded; each destination's frontier is its own. There is no options block;\n the old `by` is $key. A bare follow ^docs (no brace, no consumer) is an\n error naming both block spellings.\n Type-preserving relations: block.children, section.children, section.subsections;\n the citation graph is out_links collect { from dst } / in_links collect { from doc }.\n from links where dst_path == \"/notes/x.md\" inbound links to a doc, as rows\n select d: dst.title, k: dst_kind from links the destination document's title\n from docs where out_links exists { where dst.layer == \"draft\" } links into drafts\n select b: in_links collect distinct { from doc } from docs backlinks as { id, path }\n select doc_id, dst_path from links where predicate == \"depends_on\"\n from docs where not doc.in_links exists { } orphans (nothing links in)\n (The `graph` tool is a convenience wrapper that compiles to a follow query.)\n\n## semantic grain\n\nfrom docs ranks whole DOCUMENTS (one hit per file); from blocks ranks PASSAGES.\ntext()/where prune the candidate set; only order by reweights.\n\n## Examples\n\n from docs where layer == \"working\"\n from docs where path.startsWith(\"/guides/\") and updated_at >= \"2026-08-01\"\n select layer, tags from docs where \"pricing\" in list(tags)\n from docs where inline.owner == \"alice\"\n from docs where title.lower().contains(\"q3 plan\")\n from docs where format == \"yaml\"\n from docs where type == \"practitioner\" order by era desc limit 2\n from docs where nodes none { where kind == \"md:task\" and not checked }\n from docs where tags exists { where $it == \"tria-prima\" }\n select fm: entries(data) collect { k: $key, v: $it } from docs where path == \"/x.md\"\n select owner_id, owner: ^nodes single { where kind == \"person\" and attrs.id == ^owner_id } from docs\n from blocks where type == \"task\" and not checked and under_heading(\"Launch\") and doc.layer == \"working\"\n from blocks where type == \"paragraph\" and has_edge(\"references\", \"d_92aaaaa\")\n select path, $depth from docs where path == \"/timeline/review.md\" follow refs(before), refs(after)\n select path, next: ^docs collect { path where ^path in list(before) } from docs where type == \"milestone\"\n select path, n: size(^docs), repo: ^id from docs limit 1\n select p: data.path, t: data.title from docs where has(data.path)\n docs { type == \"substance\" and not verified }\n select subject, process: ^docs[slug == ^subject]!.path from docs where type == \"lab-note\"\n select path, back: nodes first { select p: 0^docs collect { path where ^^path in list(after) } values } from docs where type == \"milestone\"\n select path, prior: refs(before) collect { path, when } from docs where type == \"milestone\"\n from blocks order by semantic(\"identity preservation across edits\") desc limit 10\n select uri from links where dst_kind == \"external\"\n docs count { where layer == \"canon\" }\n select seq, origin, ts, hash from revisions where path == \"/processes/calcination.md\" order by seq desc\n from docs where revisions count { where origin == \"api\" } > 0\n select p: path, d: doc.path from revisions where doc == null\n select layer, n: count(), titles: collect(title), open: rows count { where nodes exists { where kind == \"md:task\" and not checked } } from docs group by layer having count() > 1 order by count() desc\n select path, tag: tags from docs where \"process\" in tags flatten tags order by path, tags\n\nCommon mistake: `$path`/`$id` (the 2.x spelling) \u2014 omgbase has no $ names; write\n`path`/`id`. Another: `where tags == \"x\"` on a list-valued key is false \u2014 write\n`\"x\" in tags`. Another: `select data.path` \u2014 alias it (`p: data.path`).\n";
1
+ export declare const QUERY_SYNTAX = "# query \u2014 OQX syntax reference\n\nThe `query` tool takes ONE plain OQX string (+ optional `limit`/`cursor`, and\n`bare: true` to drop the automatic `id`/`path` envelope \u2014 needs a `select`).\nThere is no {from, filter, order} envelope: every concern is a clause of the\nstring. Two rules tell the two syntaxes apart: DOT navigation belongs to the\nhost object model (`doc.layer`, `section.blocks`, `dst.title`); WHITESPACE\nquery directives belong to OQX \u2014 a nested query is `<receiver> collect|exists|\ncount|first|single { <block> }` (a postfix directive + a `{ \u2026 }` block), never a\nmethod. Its distinctive power is the receiver-constrained nested query that\ncorrelates to the current row: `from docs where nodes exists { where kind ==\n\"md:task\" }` returns only the docs that themselves contain a matching node.\n\n [select <items>] from <target> [where <pred>] [frontier <pred>] [follow <dest> [{ \u2026 }], \u2026]\n [flatten <expr> [{ \u2026 }] [as <name>]]\u2026 [group by <key>, \u2026] [having <pred>]\n [order by <expr> [asc|desc], \u2026] [limit N] [offset N]\n <target> count|exists|none|first|single { <block> } (scalar/one-row form: a bare target at the root scope)\n\nCLAUSE ORDER IS FIXED: select, from, where, frontier, follow, flatten, group by, having, order by, limit, offset \u2014\neach at most once (`flatten` may repeat); an out-of-order clause is a parse error naming the order.\nOnly `select` may drop its keyword, and only as the first clause\n(`path, era from docs where \u2026`). A top-level predicate needs `where`, because\n`where` follows `from` (`era > 1600 from docs` is the clause-order error); a\nblock may LEAD with a predicate (see Sugar), but a predicate after a projection\nstill needs the keyword (`{ name where kind == \"md:task\" }`, not\n`{ name, kind == \"md:task\" }`); `from docs count` is an error (write\n`docs count { \u2026 }`). `where` and later `select` items may use the same body's\n`select` aliases (`select path, old: era < 1000 from docs where old`; an alias\nshadows a same-named field there). `and` / `or` / `not` are the connectives;\nthe symbols `&&` / `||` / `!` are accepted synonyms (the words are canonical and\nwhat `print` writes).\n\n## Sugar (OQX 0.17)\n\nEvery form here is shorthand for an explicit directive (the AST is the explicit form):\n nodes { kind == \"md:task\" } \u2261 nodes collect { where kind == \"md:task\" } \u2014 a receiver block with\n no consumer is collect; a block whose LEADING expression is a predicate\n (comparison, call, !/is/not, in, literal, (\u2026), a consumer test \u2014 anything\n but a bare name, a dotted path or a ^lift) is where-first. Bare names still\n project: nodes { name } \u2261 nodes collect { name }; filter a bare field with\n nodes { is checked }. Wherever a value stands (0.20), call arguments included:\n size(children { is checked }); x distinct { \u2026 } \u2261 x collect distinct { \u2026 }. After a\n follow destination too: follow children { depth 2 } is that destination's block.\n (a, b) a tuple (0.20): two or more expressions, concatenated one level flat \u2014 a value\n (size((tags, aliases)), \"x\" in (tags, aliases)) and a receiver ((a, b) { \u2026 },\n follow (before, after) { depth 2 }); (a) alone is grouping\n refs(x)[0] \u2261 refs(x) first { offset 0 } positional (integer literal or ${binding});\n out of range \u21D2 absent; refs(x)[0].path navigates the row\n ^docs[path == ^company] \u2261 ^docs first { where path == ^company } first match or absent\n ^docs[path == ^company]! \u2261 \u2026 single { \u2026 } required: exactly one, else filter_invalid\n title! required: title, or an error naming the expression (and the row's $key \u2014 its id)\n \u2014 never a filter, never a coercion (0!, \"\"! are values); tightest: a!.b vs a.b!\n is x / not x truthiness of x, and its negation (prefix; `not` \u2261 `!`)\n x is y / x is not y identity ($key: a store row's id, else structural) \u2014 compares rows, which == does not;\n x is null = absent; for scalars is \u2261 ==; comparison precedence, no chaining\n a and b / a or b the connectives (\u2261 && / ||: same precedence, short-circuit, value: data.title or path coalesces)\n boss: ^docs[path == ^manager], bossName: boss.title a select item may use the items to its LEFT\n Reserved words (never a bare field name): from where select is not and or, true false null.\n Contextual words (limit order offset follow collect count first distinct desc by in frontier depth group having\n flatten as) parse as fields in expression position (select o: order from docs).\n\nResults are LEAN hits \u2014 { id, path } + whatever `select` projects (or `values`,\n`count`, `exists`, `none`). The envelope keys ARE the attributes: copy a hit's\n`id` straight into `where id == \"\u2026\"` (no translation); `select id, path` is simply\nthe envelope (no duplicate columns); any OTHER projection whose output key would\nbe `id` or `path` must be aliased \u2014 `select data.path` is refused (\"projection\n`path` collides with the hit envelope \u2014 alias it (`p: data.path`)\"); a row\nsurfacing as a value (an empty `collect { }`) is the same `{ id, path }`. Every\nother tool spells a row the same way (`docs_read` \u2192 `{ id, path, rev, properties,\nraw }`, `blocks_read` \u2192 `{ id, type, text, raw, hash, \u2026 }`, `docs_list` \u2192 `{ id,\npath, updated_at, blocks }`, a revision \u2192 `{ id, path, hash }`): hydrate by id via\nblocks_read/docs_read.\n\n## Paths\n\nEvery path the surface returns is `/`-rooted \u2014 the form a reference is\nwritten in (`[x](/projects/oqx.md)`, `before: [/timeline/kickoff.md]`): a\nhit's `path`, an edge's `dst_path`, every tool's paths. Every path it\naccepts tolerates both forms (`/a.md` or `a.md`): tool arguments, `refs(x)`,\n`within(...)`, and a string LITERAL compared with `path`/`dst_path` (bare, or\nthrough a row handle: `doc.path`) by `==`/`!=` or passed to their\n`.startsWith(...)` (it is rooted first, so `path == \"a.md\"` and `path == \"/a.md\"`\nboth match). A property holding a reference compares directly \u2014 and is never\nrooted: `where ^path in list(after)`, `where customer == ^path`,\n`where data.path == \"/x.md\"` (the author's `path:` key, as written) \u2014 no\n`\"/\" + ...` glue.\n\n## Targets & field namespaces\n\nTHE NAME RULE: `$` belongs to OQX \u2014 $it, $key, $depth, $stop, $leaf, $frontier,\n$ordinal are values the LANGUAGE injects into a scope; omgbase adds NO $ names\n(a `$id` in a query is an unknown name, absent). Every engine attribute is a BARE\nsnake_case name that WINS on collision \u2014 the convention the relations (children,\nblocks, out, in, doc, frontmatter, \u2026) always followed: a bare name that is an\nengine attribute reads the engine's value, never yours. Your data is bare too;\nwhere a key of yours collides with an engine name, reach it through a source\nhandle: `data.<k>` (the merged authored view \u2014 frontmatter + inline, exactly the\nread a bare non-colliding key performs), `frontmatter.<k>`, `inline.<k>`. So\n`path` is the engine's /-rooted location and `data.path` your `path:` key;\n`tags` is your tags \u222A the body #hashtags and `data.tags` the authored list alone;\n`title` the computed title (the first H1) and `data.title` your `title:`;\n`entries(data)` lists every authored top-level key. Same value, same name\neverywhere: a hit's `id` is docs_read's `id` and the `id` argument of blocks_read.\n\n Reserved bare names per kind (engine attributes; handles; self; relations):\n docs id path raw hash updated_at created_at title tags format | data frontmatter inline | doc | blocks nodes out_links in_links revisions\n blocks id path doc_id raw hash updated_at created_at type text attrs depth ordinal | block doc | children nodes out_links section\n nodes id path doc_id block_id kind name value attrs | section doc block | blocks subsections children\n links id path doc_id block_id via_id dst_id dst_path uri field dst_kind predicate provenance anchor | doc dst block\n revisions id path rev seq doc_id commit ts origin actor reason hash is_current archived | doc\n A key of yours with any of these names is `data.<k>` (or frontmatter.<k> /\n inline.<k> by source; attrs.<k> on a block or node).\n UNKNOWN NAMES: a name that is none of the above AND that no object of the\n kind in this repository carries is reported before any row is read \u2014 on\n links as a filter_invalid error naming the fields; on docs/blocks/nodes the\n result gains `hints` (one line per name, nearest match when there is one)\n and the rows still come back; in a plan/apply/execute selector it is refused.\n A property some objects carry is known (partial presence is normal).\n\ndocs:\n - bare identifier = a document PROPERTY (frontmatter + inline `key:: value`,\n unioned; nested via dots: meta.owner). YAML/JSON docs\n expose the parsed object's keys the same way.\n - source handles = data.<k> (merged authored view) / frontmatter.<k> /\n inline.<k>; the whole bag as a collection via entries(data)\n / entries(frontmatter) / entries(inline) (see entries()).\n - attributes = id, path (/-rooted, see Paths), updated_at (ISO-8601,\n sorts chronologically), raw (the whole file, frontmatter\n included), hash (its sha256, 64 hex), title (the computed\n title: the first H1; data.title is the authored key), tags\n (authored tags \u222A body #hashtags, deduped, authored first;\n data.tags is the authored list), format (the doc's format \u2014\n \"markdown\"/\"yaml\"/\"json\"; present on EVERY doc, so\n has(format) is always true and `format == \"yaml\"` is the\n filter).\n - relations = nodes, blocks, doc.out_links / doc.in_links (a doc's\n links as rows; the documents at their other end are\n out_links collect distinct { from dst } and\n in_links collect distinct { from doc } \u2014 there is no\n out / in relation), revisions (the document's stored\n revisions, oldest first \u2014 see revisions below).\n\nblocks:\n - bare fields = doc_id (the owning doc), type, text (normalized), ordinal\n (0-based among siblings), depth (0 at the top level), and\n every attrs key FLATTENED onto the row (bare `checked`\n reads attrs.checked; `attrs.<k>` still works; the\n structural fields win on collision; absent \u21D2 false).\n - attributes = id, path (the owning doc's path), raw (the block's raw\n source \u2014 a container's raw holds its children), hash\n (its sha256 \u2014 the value update/split expect as\n expect.content_hash), updated_at.\n - reach-through = doc.<key> / doc.path / doc.id / doc.format = the\n CONTAINING doc (doc.id and doc.path cost nothing \u2014 they\n read the row's own doc_id / joined path, no lookup).\n - relations = block.children, block.nodes, block.out_links, section.\n NOTE: inside a `follow`, $depth/$ordinal are ALWAYS the walk's metadata; a\n block's own nesting is `depth` / `ordinal` (no $) everywhere.\n\nnodes:\n - bare fields = doc_id, block_id (null for a block-less node), kind\n (md:task / md:link / md:section / yaml:\u2026), name, value,\n + attrs keys flattened (checked, level).\n - attributes = id, path.\n - reach-through = doc.<key> / doc.id / doc.path, block.type / block.text\n / block.id / block.path.\n - relations = section.blocks, section.children, section.subsections.\n\nlinks: (the authored link graph as rows \u2014 one per open link: the relation a link\n establishes; the md:link NODE is its occurrence in the text and via_id\n points at it \u2014 a frontmatter link has a row and no node)\n - bare fields = doc_id (the SOURCE doc), block_id (the source block; null\n for a frontmatter edge), via_id, dst_id (the destination\n node id: a doc id, an external node id or phantom:<path>),\n dst_path (target doc path, /-rooted; NULL when\n dangling/external), uri (external URL; NULL otherwise),\n predicate (\"references\"/\"embeds\"/freeform), provenance\n (\"link\"/\"frontmatter\"/\"inline_field\"/\u2026), dst_kind\n (\"document\"/\"external\"/\"collection\"), anchor, field (the\n frontmatter key / inline field it was authored in).\n - attributes = id, path (the SOURCE doc's path); doc.<key> reaches the\n source document. There is no from_commit: a query sees\n open edges only (history is blocks_history/changes_since).\n - text()/semantic() are N/A on links.\n\nrevisions: (history as rows \u2014 one per stored revision of every document of the\n repository, deleted documents included: what docs_history lists, queryable)\n - bare fields = rev (= id), seq (1, 2, \u2026 per document), doc_id, commit,\n ts (the commit's time, sorts chronologically), origin\n (\"observed\"/\"api\"/\"import\"/\"projection\"), actor, reason,\n hash (the rendered file's sha256 \u2014 docs_history's hash),\n is_current (true for the document's current revision),\n archived (true when the bodies moved to an archive segment:\n the row stays, docs_read { at } reads through the segment).\n - attributes = id (the revision id, r_\u2026), path (the path the revision\n was committed under \u2014 a move re-paths only the current\n revision, so earlier ones keep the old path). doc.<key>\n reaches the document's CURRENT row; absent for a deleted\n document, so where doc == null is the revisions of deleted documents.\n - on docs = revisions is a relation (oldest first): a document with\n more than one version is docs { revisions count { } > 1 }.\n - text()/semantic() are N/A on revisions. The body as of a revision is\n docs_read { path, at: \"<rev>\" } (docs_read_at is the same call).\n\n## Scoping (ADR-015; the root row since surface 2.0): bare names are LOCAL\n\n name the CURRENT row only \u2014 never falls through to an enclosing row\n or the repository; an absent field is simply absent.\n ^name one scope OUT (the enclosing row's fields/intrinsics/lifts);\n ^^name two scopes out. `^path` = the enclosing row's path.\n ^docs ^nodes ^blocks ^links ^revisions\n THE ROOT ROW (surface 2.0, oqx 0.18): the repository is the root\n scope's row, one scope out from a top-level row, so ^docs there is\n the whole documents scan (uncorrelated until you add a ^ predicate);\n one caret per enclosing block (^^docs from depth two), or the\n absolute 0^docs from ANY depth. ^id / 0^id = the repository id.\n ^$it = the root object: ^$it.docs \u2261 ^docs, entries(^$it) names the\n five collections. At the root scope itself a bare target is the scan\n (docs count { \u2026 }, from docs, entries(docs)).\n ERRORS: a bare `docs` INSIDE a block reads a property of the current\n row, which has none \u2014 refused (\"did you mean ^docs\"); `^docs` AT THE\n TOP LEVEL reaches past the root \u2014 refused (write the bare `docs`);\n so is `$repo` (surface < 2.0; the message names the replacement).\n nodes/blocks on a doc row are that doc's RELATIONS, not the root.\n $it the current item itself (a row, or each ELEMENT when the\n receiver is a list property); ^$it = the enclosing row.\n $value is NOT an intrinsic (since oqx 0.15): it reads a\n property literally named $value, absent when there is none.\n $key the row's identity (0.20): a store row's id (doc/block/node/edge), an entries(x)\n entry's key, a follow occurrence's walk identity; project $key: <expr> to rekey the\n rows a block yields (a destination block's $key: is not a column).\n\n## Scalar semantics (@omgbase/oqx)\n\n comparisons == != < <= > >= strict & typed (5 == \"5\" is false); either\n side may be a field (field-vs-field OK).\n absence null and a missing field are the same \"absent\" value; two absents\n are EQUAL, so `absent != \"x\"` is TRUE and `absent == null` is TRUE.\n Relational (< <= > >=) on an absent operand \u2192 FALSE. Bare `f` in\n boolean position uses JS truthiness (absent/false/0/\"\" \u21D2 false;\n NOTE: \"false\", [] and {} are truthy). has(f) = explicit presence.\n boolean and or not grouping ( \u2026 ) \u2014 && || ! are accepted synonyms\n arithmetic + - * / % (+ concatenates if either side is a string)\n membership x in y array \u2192 typed membership; string \u2192 substring;\n object \u2192 key exists; range \u2192 coverage. Any list\n property works directly: \"pricing\" in tags. list(f)\n coerces scalar-or-list (absent \u2192 []) \u2014 use it when a\n key is sometimes scalar, sometimes a list.\n ranges lo..hi (inclusive) lo...hi (exclusive hi) ..hi lo..\n era in 800..1680; works over ISO dates. range(prop) reads a\n range-shaped STRING property as an interval: \"2026-01-15\" in range(window).\n strings x.contains(\"s\") x.startsWith(\"p\") x.endsWith(\"s\") x.matches(\"^re$\")\n (real regex) x.lower() x.upper() size(x). ALL CASE-SENSITIVE \u2014\n fold with .lower(): title.lower().contains(\"aurora\").\n Infix regex: x ~ \"re\" is x.matches(\"re\"); x ~* \"re\" is the\n case-insensitive match; x !~ \"re\" and x !~* \"re\" negate them.\n Unanchored unless the pattern anchors; an absent x is false\n under ~ and TRUE under !~ (not SQL's NULL).\n functions has(f) size(x) list(x) entries(x) range(s)\n dates a date is an ISO string, a duration a NUMBER OF SECONDS; arithmetic\n is METHODS, never + or - (+ concatenates strings).\n date(\"today\"|\"now\"|\"yesterday\"|\"tomorrow\"|\"sow\"|\"eow\"|\"som\"|\"eom\"|\n \"soy\"|\"eoy\") \u2014 the host clock, UTC; date(\"2026-10-15\") canonical ISO\n (absent if unparseable). dur(\"1h30m\") dur(\"2 days, 3 hours\")\n dur(\"P1DT2H\") dur(90) \u2192 seconds (year = 365 d, month = 30 d).\n d.plus(dur(\"30 days\")) d.minus(86400) d.plus(\"1 month\") (calendar:\n Jan 31 \u2192 Feb 28) a.until(b) \u2192 signed seconds d.year() d.month()\n d.day() d.weekday() (1 = Mon) d.hour() d.minute() d.second()\n d.date() d.format(\"EEE dd MMM yyyy\") n.dur_text() \u2192 \"1h 30m\"\n n.dur_iso() \u2192 \"PT1H30M\". Total: a non-date is absent, so\n `where date(due) < date(\"today\").minus(dur(\"30 days\"))` just\n filters. A datetime is never < its own date: compare\n updated_at.date() == date(\"today\"), or < date(\"tomorrow\").\n literals \"str\" 'str' 123 1.5 true false null\n determinism no random; the only clock is date(\"today\"/\"now\"/\u2026), read once per\n query. Lists/structs are not literals; ternary is not supported.\n Errors surface as filter_invalid.\n scalars vs lists a list-authored key never == a scalar (tags == \"x\" is false\n when tags is a list) \u2014 use `\"x\" in tags` / `in list(tags)`.\n\n## Domain predicates (omgbase)\n\n text(\"terms\") FTS5 PRUNING predicate (docs match via their blocks, nodes\n via the node index); composes anywhere, incl. nested scopes.\n semantic(\"phrase\") embedding cosine SCORE (docs/blocks): threshold it\n (semantic(\"x\") > 0.6), project it, or `order by semantic(\"x\")\n desc` for top-K. Needs a provider that has embedded the repository; else provider_unavailable.\n blocks target only:\n under(id_or_heading) block in the containment subtree / section range\n under_heading(\"Launch\") an ancestor section heading contains the text (ci)\n under_kind(type[, name]) an ancestor block has that type (name: its text\n contains / its key equals name)\n within(\"path\" | \"d_..\") containing doc = id, exact path, or path glob (*);\n either path form\n yaml_path(\"a.b\") YAML docs: the block at that key path\n json_pointer(\"#/a/b\") JSON docs: the block at that pointer\n has_edge(\"pred\"[, target]) an open authored edge with that predicate leaves here\n has_anchor() block carries an authored ^ref\n parent_type() == \"x\" parent block's type (must be compared)\n child_count() > 0 number of direct children (must be compared)\n refs(x) the live documents a property's document references name:\n x is a string, a list or absent; each \"/a/b.md\", \"a/b.md\"\n or \"d_\u2026\" element resolves to that doc's row; dangling and\n non-string elements are dropped. Yields docs rows, so it is\n a source, a receiver or a follow destination:\n follow refs(before), refs(after) walks a timeline both ways.\n The reverse direction needs no function: a document's\n path is already the reference form, so\n ^docs { where ^path in list(after) } is\n \"the documents whose `after` names me\".\n A HIT IS A STORE ROW: a top-level row that is not a doc/block/node/edge fails\n (filter_invalid). `follow before` over a list of paths\n reaches the STRINGS \u2014 write `follow refs(before)`.\n\n## Nested queries & consumers\n\n <receiver> collect|exists|none|count|first|single { <block> }\n receiver = a relation (nodes, blocks, doc.out_links, section.blocks, \u2026), a\n reach-through row (dst, doc: one row \u2014 dst { title }), a\n list property (tags), entries(x), or a root collection (^docs; 0^docs at depth \u2265 2).\n block = [select \u2026] [from E] [where \u2026] [frontier \u2026] [follow \u2026] [flatten \u2026] [group by \u2026] [having \u2026] [order by \u2026] [limit N] [offset N]\n \u2014 the same fixed order as the top level (`from` optional; the\n receiver supplies the rows). A leading name/alias list is the\n projection (`select` dropped); a predicate always needs `where`.\n A `from E` inside a block RE-PROJECTS the rows through a relation\n (a typed flatMap): `docs collect { from nodes where kind == \"md:task\" }`\n starts at docs, projects each through `nodes`, and queries the\n resulting nodes; a scalar relation (`from doc`) contributes one\n row; a missing or non-navigable relation is a loud error, never a\n global scan.\n exists \u22651 row nodes exists { where kind == \"md:task\" and not checked }\n none 0 rows nodes none { where kind == \"md:task\" and not checked } (\"every task done\")\n count a number nodes count { where kind == \"md:task\" } >= 2\n collect rows (default) \u2014 in select for nested results; in where with a lift\n first / single zero-or-one (single errors on >1) \u2014 lookups in select\n distinct on any consumer: dedup by projected value \u2014 nodes count distinct { select kind }\n limit/offset bound the block's rows AFTER where/order/distinct, BEFORE the\n consumer reduces: nodes exists { offset 1 } = at least two;\n first { \u2026 offset 1 } = the second.\n lift `^name:` in a where-collect binds values outward:\n select path, open from docs where nodes collect { ^open: value where kind == \"md:task\" and not checked }\n joins ^<t> exists { where slug == ^ref } semi-join; not \u2026 exists anti-join;\n ^nodes single { where kind == \"person\" and attrs.id == ^owner_id } lookup;\n x in ^keys membership over a lifted set.\n\n## select \u2014 projection\n\n select a, alias: expr, nested: nodes collect { \u2026 }\n bare key \u2192 the attribute when it is one (id, path, title, \u2026), else the property\n (scalar-authored \u2192 scalar; list \u2192 array); data.<k> forces the property;\n a call/arithmetic needs an alias (n: size(tags)) unless in values mode;\n an output key of id/path other than the attribute itself needs an alias.\n raw whole file bytes (docs) \u2014 docs_read is cheaper for one doc.\n distinct select distinct type (dedup hits by projected value)\n values select <ONE item> values \u2192 bare values instead of records:\n select distinct type values from docs \u2192 values: [\"hub\",\"lab-note\",\u2026]\n select path values from docs \u2192 values: [\"/index.md\", \u2026]\n tags: tags collect { $it values } \u2192 a plain array\n h: nodes first { name values where kind == \"md:section\" order by first_ordinal }\n entries(x) a record as a collection ($key / $it per entry), key order:\n select fm: entries(data) collect { k: $key, v: $it }\n where entries(frontmatter) exists { where $key == \"era\" and $it > 1600 }\n entries(inline), entries(attrs), a nested map, or a list (index keys)\n $semantic_score is not a field \u2014 project semantic(\"\u2026\") under an alias instead.\n\n## flatten (OQX 0.25)\n\n flatten <expr> [{ \u2026 }] [as <name>] after follow, before group by; may repeat\n Splits one value into rows. Every hit is still the PARENT row \u2014 its id/path\n repeat per element, as Dataview's TABLE \u2026 FLATTEN \u2014 with <name> bound beside\n its fields at the same depth: an array \u2192 one row per element (an empty list\n yields none), a missing field \u2192 one row with the name absent (never dropped),\n an object or a scalar \u2192 one row with the value. A plain navigation names\n itself (flatten tags rebinds tags to each element); anything else needs as.\n The block is the ordinary receiver block over the elements (where, a\n projection, order by, limit, distinct), applied first. Later clauses read the\n name \u2014 a later flatten (flatten jobs as j flatten j.tags as t), group by,\n having, select, order by; where runs before it. A flattened query pages by\n position (cursor null); as id / as path is refused (the envelope reads them).\n select path, tags from docs where type == \"process\" flatten tags order by path, tags\n select path, t: b.text from docs flatten blocks { where type == \"task\" and not checked } as b\n select tags, n: count() from docs flatten tags group by tags order by count() desc\n\n## order by, limit/offset, pagination\n\n order by era desc, path asc absent values sort LAST (asc); ties \u2192 (path, id)\n limit N / offset N define the result SET (integer literals);\n the tool's limit/cursor then page WITHIN it\n cursor opaque keyset on (path,id); a custom order by\n disables it (you get the top page + truncated)\n\n## Graph traversal (follow) and links\n\n The citation graph is the LINK ROWS walked through a destination block:\n dst is a link's destination document (absent for an external or dangling\n link \u2014 it then contributes no successor), doc its source.\n from docs where <seed> follow out_links collect { from dst } docs the seed links TO\n from docs where <seed> follow in_links collect { from doc } backlinks\n follow out_links collect { from dst depth 3 } bound (1..8, default 8) \u2014 a clause of the block\n follow out_links collect { from dst where layer == \"canon\" } keep only matching successors\n follow in_links collect { from doc where ^path in list(before) } ^ = the row being expanded\n (^^ = the walk's enclosing scope)\n follow out_links collect { from dst where predicate == \"depends_on\" } one predicate only\n follow out_links collect { from dst }, in_links collect { from doc }\n several destinations: their union\n (one step dedups by identity)\n follow ^docs { where out_links exists { where dst_path == ^^path } }\n a destination block over the root's\n documents, re-read per frontier row\n (^ = that row) = the backlinks walk\n follow out_links collect { from dst frontier type == \"practitioner\" } cut, keeping the frontier row\n from docs where type == \"process\" frontier !verified follow out_links collect { from dst }\n the BODY's frontier applies to the seeds:\n an unverified seed is kept, not expanded\n follow distinct out_links collect { from dst } / \u2026 { $key: group from dst } identity control ($key)\n follow refs(before), ^docs { where ^path in list(after) } forward over `before`, backward\n over every doc whose `after` names me\n Per-occurrence metadata: $depth (seed = 1), $stop (interior|leaf|frontier|\n depth|cycle), $leaf/$frontier, $ordinal (a deterministic 1..N rank over the\n walk, ordered by depth then path \u2014 `order by $ordinal` for walk order,\n `where $ordinal <= N` for a deterministic budget cut), $key \u2014 usable in\n select/order by and the top-level post-walk where (a where conjunct that\n reads one filters the walk's RESULT; the other conjuncts remain the seed\n predicate), NOT metadata of the candidates a destination block's where reads,\n nor valid inside a consumer directive. Cyclic graphs are safe: a revisited\n node is admitted once as a `$stop == \"cycle\"` occurrence and never\n re-expanded. The default keeps one occurrence per distinct walk path;\n `follow distinct` dedups reached rows by identity.\n Destination blocks (OQX 0.20): a brace after ANY destination opens that\n destination's block \u2014 children { depth 2 }, refs(x) { depth 2 }, out_links\n collect { from dst depth 2 }, ^docs { where ^path in list(after) } \u2014 with the\n 0.17 body rules (where-first, projections) and two clauses of its own after\n `where`: frontier <pred> (occurrences reached through this destination that\n match are frontiers; ^ there is the walk's enclosing scope) and depth <n>\n (this destination expands a frontier row only while $depth < n). The body\n that carries the follow may write frontier <pred> too, after its where and\n before follow: it applies to the SEEDS (read like the body's where) \u2014 a\n matching seed stays in the result with $stop == \"frontier\" and is not\n expanded; each destination's frontier is its own. There is no options block;\n the old `by` is $key. A bare follow ^docs (no brace, no consumer) is an\n error naming both block spellings.\n Type-preserving relations: block.children, section.children, section.subsections;\n the citation graph is out_links collect { from dst } / in_links collect { from doc }.\n from links where dst_path == \"/notes/x.md\" inbound links to a doc, as rows\n select d: dst.title, k: dst_kind from links the destination document's title\n from docs where out_links exists { where dst.layer == \"draft\" } links into drafts\n select b: in_links collect distinct { from doc } from docs backlinks as { id, path }\n select doc_id, dst_path from links where predicate == \"depends_on\"\n from docs where not doc.in_links exists { } orphans (nothing links in)\n (The `graph` tool is a convenience wrapper that compiles to a follow query.)\n\n## semantic grain\n\nfrom docs ranks whole DOCUMENTS (one hit per file); from blocks ranks PASSAGES.\ntext()/where prune the candidate set; only order by reweights.\n\n## Examples\n\n from docs where layer == \"working\"\n from docs where path.startsWith(\"/guides/\") and updated_at >= \"2026-08-01\"\n select layer, tags from docs where \"pricing\" in list(tags)\n from docs where inline.owner == \"alice\"\n from docs where title.lower().contains(\"q3 plan\")\n from docs where format == \"yaml\"\n from docs where type == \"practitioner\" order by era desc limit 2\n from docs where nodes none { where kind == \"md:task\" and not checked }\n from docs where tags exists { where $it == \"tria-prima\" }\n select fm: entries(data) collect { k: $key, v: $it } from docs where path == \"/x.md\"\n select owner_id, owner: ^nodes single { where kind == \"person\" and attrs.id == ^owner_id } from docs\n from blocks where type == \"task\" and not checked and under_heading(\"Launch\") and doc.layer == \"working\"\n from blocks where type == \"paragraph\" and has_edge(\"references\", \"d_92aaaaa\")\n select path, $depth from docs where path == \"/timeline/review.md\" follow refs(before), refs(after)\n select path, next: ^docs collect { path where ^path in list(before) } from docs where type == \"milestone\"\n select path, n: size(^docs), repo: ^id from docs limit 1\n select p: data.path, t: data.title from docs where has(data.path)\n docs { type == \"substance\" and not verified }\n select subject, process: ^docs[slug == ^subject]!.path from docs where type == \"lab-note\"\n select path, back: nodes first { select p: 0^docs collect { path where ^^path in list(after) } values } from docs where type == \"milestone\"\n select path, prior: refs(before) collect { path, when } from docs where type == \"milestone\"\n from blocks order by semantic(\"identity preservation across edits\") desc limit 10\n select uri from links where dst_kind == \"external\"\n docs count { where layer == \"canon\" }\n select seq, origin, ts, hash from revisions where path == \"/processes/calcination.md\" order by seq desc\n from docs where revisions count { where origin == \"api\" } > 0\n select p: path, d: doc.path from revisions where doc == null\n select layer, n: count(), titles: collect(title), open: rows count { where nodes exists { where kind == \"md:task\" and not checked } } from docs group by layer having count() > 1 order by count() desc\n select path, tag: tags from docs where \"process\" in tags flatten tags order by path, tags\n\nCommon mistake: `$path`/`$id` (the 2.x spelling) \u2014 omgbase has no $ names; write\n`path`/`id`. Another: `where tags == \"x\"` on a list-valued key is false \u2014 write\n`\"x\" in tags`. Another: `select data.path` \u2014 alias it (`p: data.path`).\n";
@@ -113,7 +113,7 @@ everywhere: a hit's \`id\` is docs_read's \`id\` and the \`id\` argument of bloc
113
113
  docs id path raw hash updated_at created_at title tags format | data frontmatter inline | doc | blocks nodes out_links in_links revisions
114
114
  blocks id path doc_id raw hash updated_at created_at type text attrs depth ordinal | block doc | children nodes out_links section
115
115
  nodes id path doc_id block_id kind name value attrs | section doc block | blocks subsections children
116
- links id path doc_id block_id via_id dst_id dst_path uri field dst_kind predicate provenance anchor | doc
116
+ links id path doc_id block_id via_id dst_id dst_path uri field dst_kind predicate provenance anchor | doc dst block
117
117
  revisions id path rev seq doc_id commit ts origin actor reason hash is_current archived | doc
118
118
  A key of yours with any of these names is \`data.<k>\` (or frontmatter.<k> /
119
119
  inline.<k> by source; attrs.<k> on a block or node).
@@ -615,6 +615,8 @@ export function buildServer(ctx) {
615
615
  ...(args.limit !== undefined ? { limit: args.limit } : {}),
616
616
  ...(args.cursor !== undefined ? { cursor: args.cursor } : {}),
617
617
  ...(args.bare !== undefined ? { bare: args.bare } : {}),
618
+ // (4.11) the call as received, for a parse error's corrected `fix`.
619
+ request: args,
618
620
  }, rankableEmbed(repoId)));
619
621
  }
620
622
  catch (e) {
@@ -701,7 +703,7 @@ export function buildServer(ctx) {
701
703
  // commits exactly a plan (the plan form, or the digest form `plan_id` +
702
704
  // `effects`); `apply { effects }` above is plan + execute in one call.
703
705
  tool("plan", {
704
- description: ladder("plan — Preview a write: the Plan (summary, $new.n ids) burns no id; execute next. diffs: true for file bodies.", "{\"effects\":[{\"op\":\"set\",\"target\":\"/processes/calcination.md\",\"field\":\"frontmatter.verified\",\"value\":false}]} path = doc", "{\"effects\":[{\"op\":\"insert\",\"into\":{\"kind\":\"section\",\"heading_id\":\"b_153\"},\"markdown\":\"- [ ] Weigh the calx\"}]} in a section", "{\"effects\":[{\"op\":\"create\",\"path\":\"lab/march.md\",\"markdown\":\"# March\\n\"},{\"op\":\"insert\",\"into\":\"$new.0\",\"markdown\":\"Hi.\"}]} fill it: $new.0", "{\"select\":\"from docs where stage == \\\"nigredo\\\"\",\"effects\":[{\"op\":\"set\",\"field\":\"frontmatter.ok\",\"value\":true}]} no target: the hit", "{\"select\":\"from docs where type == \\\"lab-note\\\"\",\"effects\":[{\"op\":\"insert\",\"into\":\"-> `/processes/${subject}.md`\",\"markdown\":\"-> `[${title}](${path})`\"}]} -> reads the row", "Ops: create insert set unset move remove split merge retarget; grammar: query_syntax."),
706
+ description: ladder("plan — Preview a write: the Plan (summary, $new.n ids) burns no id; execute next. Need file bodies? add diffs: true.", "{\"effects\":[{\"op\":\"set\",\"target\":\"/processes/calcination.md\",\"field\":\"frontmatter.verified\",\"value\":false}]} path = doc", "{\"effects\":[{\"op\":\"insert\",\"into\":{\"kind\":\"section\",\"heading_id\":\"b_153\"},\"markdown\":\"- [ ] Weigh the calx\"}]} in a section", "{\"effects\":[{\"op\":\"create\",\"path\":\"lab/march.md\",\"markdown\":\"# March\\n\"},{\"op\":\"insert\",\"into\":\"$new.0\",\"markdown\":\"Hi.\"}]} fill it: $new.0", "{\"select\":\"from docs where stage == \\\"nigredo\\\"\",\"effects\":[{\"op\":\"set\",\"field\":\"frontmatter.ok\",\"value\":true}]} no target: the hit", "{\"select\":\"from docs where type == \\\"lab-note\\\"\",\"effects\":[{\"op\":\"insert\",\"into\":\"-> `/processes/${subject}.md`\",\"markdown\":\"-> `[${title}](${path})`\"}]} -> reads the row", "Ops: create insert set unset move remove split merge retarget; grammar: query_syntax."),
705
707
  inputSchema: { effects: z.array(effectSchema), select: z.string().optional(), expect: selectionExpectSchema.optional(), diffs: z.boolean().optional(), ...REPO_ARG },
706
708
  }, async (args) => {
707
709
  try {
@@ -722,7 +724,7 @@ export function buildServer(ctx) {
722
724
  }
723
725
  });
724
726
  tool("execute", {
725
- description: ladder("execute — Commit exactly a plan from plan: the plan form (the whole Plan object) or the digest form (plan_id plus the same effects, re-planned and committed only if the digest still matches).", "{\"plan_id\":\"3ece126452106400210f07047029a961691b814252ba4437f5a5f1c3301629da\",\"effects\":[{\"op\":\"set\",\"target\":\"/processes/calcination.md\",\"field\":\"frontmatter.verified\",\"value\":false}]} digest form: the plan_id plan returned for these effects", "Or pass the whole object: {\"plan\": the Plan as returned}. A document that moved on fails stale_plan (retriable: call plan again). Grammar: query_syntax."),
727
+ description: ladder("execute — Commit exactly a plan from plan: the plan form (the whole Plan object) or the digest form (plan_id plus the same effects, re-planned and committed only if the digest still matches).", "{\"plan_id\":\"ab416d8686fe527fd13439fcf0c7accb9e23307c0730edc17149c8b46f0f032a\",\"effects\":[{\"op\":\"set\",\"target\":\"/processes/calcination.md\",\"field\":\"frontmatter.verified\",\"value\":false}]} digest form: the plan_id plan returned for these effects", "Or pass the whole object: {\"plan\": the Plan as returned}. A document that moved on fails stale_plan (retriable: call plan again). Grammar: query_syntax."),
726
728
  inputSchema: { plan: z.record(z.string(), z.unknown()).optional(), plan_id: z.string().optional(), effects: z.array(effectSchema).optional(), select: z.string().optional(), expect: selectionExpectSchema.optional(), reason: z.string().optional(), ...REPO_ARG },
727
729
  }, async (args) => {
728
730
  try {
@@ -4,11 +4,14 @@ import { referencePath } from "../core/paths.js";
4
4
  import { isValidId, withIdMinter } from "../core/ids.js";
5
5
  import { inboundLinksTo } from "../graph/inbound-links.js";
6
6
  import { readDocBase } from "./commit.js";
7
+ import { adapterForPath } from "../format/index.js";
7
8
  import { canonicalDocPath, composeFile, parseNewDoc, planInboundRetarget, splitFrontmatter } from "./docs.js";
8
9
  import { linksRepair, nodeSet } from "./macros.js";
9
10
  import { planUpdate } from "./plan-update.js";
10
11
  import { MutationError, rawHashHex, renderDoc } from "./tree.js";
11
12
  import { fixRequest, withMember } from "./canonical.js";
13
+ import { joinFrontmatterParts, leadOf, lineEndingOf, splitFrontmatterRaw, splitFrontmatterTrivia } from "./frontmatter-parts.js";
14
+ import { stringify as stringifyYaml } from "yaml";
12
15
  export const EFFECT_OPS = ["create", "insert", "set", "unset", "move", "remove", "split", "merge", "retarget"];
13
16
  /** `$new.<n>`: an identity an earlier effect of the same request creates (§11.1). */
14
17
  export const NEW_SYMBOL_RE = /^\$new\.(0|[1-9][0-9]*)$/;
@@ -821,15 +824,86 @@ function nodeTarget(t) {
821
824
  throw effectInvalid(`expected a node target, got ${t.kind}`);
822
825
  return t;
823
826
  }
824
- /** The current frontmatter patch base of a loaded document: `(frontmatter, body)` of its render. */
825
- function frontmatterOf(ctx, docId) {
827
+ /**
828
+ * The frontmatter effects are Markdown's (spec/properties §3.4, 1.2; spec/surface
829
+ * Decisions 2026-10-17): a `yaml`/`json` document's "frontmatter" IS its whole
830
+ * top-level mapping, and the entry patch below would fence it with `---` lines —
831
+ * a second YAML document, or invalid JSON. Until the effect learns those formats
832
+ * the request is refused outright — `type_mismatch`, the target's kind does not
833
+ * admit the op — and the mapping is edited with `set content` (the whole file).
834
+ */
835
+ function requireMarkdownForFrontmatter(ctx, docId, field) {
826
836
  const d = ctx.planner.ensureDoc(docId);
827
- const { frontmatter, body } = splitFrontmatter(renderDoc(d));
828
- return { d, frontmatter, body };
837
+ if (d.format === "markdown")
838
+ return;
839
+ throw frontmatterNotMarkdown(docPathOf(ctx, docId), d.format, field, isNewSymbol(docId) ? null : docId);
840
+ }
841
+ function frontmatterNotMarkdown(path, format, field, docId) {
842
+ return new MutationError("type_mismatch", `frontmatter effects apply to markdown documents; ${referencePath(path)} is a ${format} document — its top-level mapping is edited with \`set content\` (the whole file) until frontmatter effects learn yaml and json`, { ...(docId !== null ? { doc: docId } : {}), path: referencePath(path), format, field });
829
843
  }
830
844
  /**
831
- * Patch a loaded document's frontmatter: compose the whole file as
832
- * `docs_set_meta` does (`composeFile(body, merged)`, §6), then set the file
845
+ * spec/mutate §11.3 `set`/`unset frontmatter.<key>` (3.6, "Frontmatter
846
+ * entries"): the frontmatter is edited BY ENTRY. The document's head is cut
847
+ * into its parts (`splitFrontmatterRaw`, the §11.3 split rule): a `set`
848
+ * replaces the key's part — the lead lines (a comment documenting the key)
849
+ * kept, the entry's own lines re-rendered as `stringify({ key: value })` in
850
+ * the fence's line endings — removes any duplicate of the key, or inserts the
851
+ * new key's part after the last entry (before the trailing comments and the
852
+ * closing fence); an `unset` removes every part of the key. Every other part
853
+ * is byte-identical afterwards — a flow list, a quoted string, a comment the
854
+ * author wrote survive a change to a different key. When nothing but the
855
+ * fences would remain, the frontmatter goes and the separator's first line
856
+ * ending with it (the §6 `set_meta` rule: a body that followed a blank line
857
+ * keeps it as leading trivia). A document WITHOUT frontmatter (or one whose
858
+ * head the fence rule does not recognise) still composes as `docs_set_meta`
859
+ * did through 3.5 — `composeFile(body, merged)` — which for a new fence is
860
+ * the same bytes a one-key part would give.
861
+ */
862
+ function patchFrontmatterKey(ctx, docId, key, set) {
863
+ const d = ctx.planner.ensureDoc(docId);
864
+ const fence = d.frontmatterRaw === null ? null : splitFrontmatterTrivia(d.frontmatterRaw);
865
+ if (fence === null) {
866
+ const { frontmatter, body } = splitFrontmatter(renderDoc(d));
867
+ const merged = { ...frontmatter };
868
+ if (set)
869
+ merged[key] = set.value;
870
+ else
871
+ delete merged[key];
872
+ patchFrontmatter(ctx, docId, merged, body);
873
+ return;
874
+ }
875
+ let parts = splitFrontmatterRaw(fence.raw);
876
+ const eol = lineEndingOf(parts[0].bytes) || "\n";
877
+ if (set) {
878
+ let rendered = stringifyYaml({ [key]: set.value });
879
+ if (!rendered.endsWith("\n"))
880
+ rendered += "\n";
881
+ if (eol !== "\n")
882
+ rendered = rendered.split("\n").join(eol);
883
+ const at = parts.findIndex((p) => p.key === key);
884
+ if (at >= 0) {
885
+ parts[at] = { key, bytes: leadOf(parts[at]) + rendered };
886
+ parts = parts.filter((p, i) => i === at || p.key !== key);
887
+ }
888
+ else {
889
+ let last = -1;
890
+ parts.forEach((p, i) => {
891
+ if (p.key !== null)
892
+ last = i;
893
+ });
894
+ parts.splice(last >= 0 ? last + 1 : parts.length - 1, 0, { key, bytes: rendered });
895
+ }
896
+ }
897
+ else {
898
+ parts = parts.filter((p) => p.key !== key);
899
+ }
900
+ const head = parts.length === 2 ? fence.trivia.replace(/^\r?\n/, "") : joinFrontmatterParts(parts) + fence.trivia;
901
+ ctx.planner.setHead(docId, head);
902
+ ctx.heads.set(docId, head);
903
+ }
904
+ /**
905
+ * Patch a document WITHOUT frontmatter: compose the whole file as
906
+ * `docs_set_meta` did (`composeFile(body, merged)`, §6), then set the file
833
907
  * head so the known-id ingest renders exactly those bytes — the body blocks
834
908
  * keep their ids and bytes (a file without a trailing newline gains one on its
835
909
  * last block's trivia, as `composeFile` would add it).
@@ -997,6 +1071,11 @@ function applyLowering(ctx, i, e) {
997
1071
  // — its blocks are the next `$new.k` in pre-order — and adopted as a loaded
998
1072
  // document, so every later effect addresses it and its blocks like any other.
999
1073
  const symbol = ctx.symbols.alloc("document");
1074
+ if (e.frontmatter && Object.keys(e.frontmatter).length > 0) {
1075
+ const format = adapterForPath(e.path)?.format ?? "markdown";
1076
+ if (format !== "markdown")
1077
+ throw frontmatterNotMarkdown(e.path, format, "frontmatter", null);
1078
+ }
1000
1079
  const source = composeFile(e.markdown, e.frontmatter);
1001
1080
  const tree = parseNewDoc(symbol, e.path, source);
1002
1081
  ctx.planner.adoptNew(symbol, tree);
@@ -1032,13 +1111,8 @@ function applyLowering(ctx, i, e) {
1032
1111
  }
1033
1112
  // `frontmatter.<key>` — on a created document too (3.0): its tree is loaded, the patch is a head like any other.
1034
1113
  const key = FRONTMATTER_FIELD.exec(e.field)[1];
1035
- const { frontmatter, body } = frontmatterOf(ctx, docId);
1036
- const merged = { ...frontmatter };
1037
- if (e.op === "set")
1038
- merged[key] = e.value;
1039
- else
1040
- delete merged[key];
1041
- patchFrontmatter(ctx, docId, merged, body);
1114
+ requireMarkdownForFrontmatter(ctx, docId, e.field);
1115
+ patchFrontmatterKey(ctx, docId, key, e.op === "set" ? { value: e.value } : null);
1042
1116
  ctx.summary.push(e.op === "set" ? `set ${e.field} = ${ctx.canonical(e.value)} in ${docPathOf(ctx, docId)}` : `unset ${e.field} in ${docPathOf(ctx, docId)}`);
1043
1117
  return;
1044
1118
  }
@@ -0,0 +1,71 @@
1
+ /** One part: `key` is the entry's key (`null` for a fence, the lines before the first entry, the trailing lines) and `bytes` its text, line endings included. */
2
+ export interface FrontmatterPart {
3
+ key: string | null;
4
+ bytes: string;
5
+ }
6
+ /** Split text into lines, each keeping its own line ending (`\n` or `\r\n`); the last line has none. `""` → `[""]`. */
7
+ export declare function splitLines(text: string): string[];
8
+ /** The line ending of `line` (`"\r\n"`, `"\n"`, or `""` when it has none). */
9
+ export declare function lineEndingOf(line: string): string;
10
+ /** A blank line (empty, or spaces and tabs only). */
11
+ export declare function isBlankLine(line: string): boolean;
12
+ /** A comment line — `#` in column 0. (An indented `#` is content: a block scalar's line, for one.) */
13
+ export declare function isCommentLine(line: string): boolean;
14
+ /**
15
+ * The entry boundary: the key of a column-0 line that opens a top-level
16
+ * mapping entry, or `null` when the line does not. A line opens an entry when
17
+ * it is not blank, not a comment, not a sequence item (`-` followed by a
18
+ * space, a tab or the end of the line), not an explicit key or value
19
+ * indicator (`?` / `:` the same way), not a document marker (`---` / `...`
20
+ * followed by whitespace or the end of the line), and reads as `<key>:`
21
+ * followed by a space, a tab or the end of the line — the key either
22
+ * - quoted: a `"…"` string (`\` escapes the next character) or a `'…'` string
23
+ * (`''` is a literal quote), closed on the same line, then optional spaces/
24
+ * tabs, then the colon; the key is the content with `\\` → `\`, `\"` → `"`
25
+ * (double) or `''` → `'` (single) undone and every other escape kept as
26
+ * written; or
27
+ * - plain: the text before the FIRST `:` that is followed by a space, a tab or
28
+ * the end of the line, trailing spaces/tabs trimmed — non-empty, not
29
+ * starting with a flow indicator, an anchor/alias/tag/block-scalar/
30
+ * directive/reserved character (`[ ] { } , & * ! | > % @ \``) and not
31
+ * containing ` #` or `\t#` (a comment would start there).
32
+ * Anything else — an indented line, a flow collection's continuation, a block
33
+ * scalar's content — is a continuation of the entry above it.
34
+ */
35
+ export declare function entryKeyOf(line: string): string | null;
36
+ /**
37
+ * The frontmatter block's raw (fences included, no trailing line ending) → its
38
+ * parts, in order. The parts always concatenate back to `raw` byte for byte:
39
+ * every line of the raw lands in exactly one part.
40
+ *
41
+ * - part 0 is the opening fence line with its line ending, key `null`;
42
+ * - the last part is the closing fence line (no line ending), key `null`;
43
+ * - between them, the body lines: every line before the first entry line is
44
+ * one part with key `null` (comments, blank lines, or — when no line opens
45
+ * an entry: a bare scalar, a sequence — the whole body); then one part per
46
+ * entry, from the first of the lead lines (blank / column-0 comment)
47
+ * immediately preceding its key line (never reaching back past the
48
+ * previous entry's key line) through the line before the next entry's
49
+ * lead, the last entry ending before the trailing lead run; the trailing
50
+ * lead run (blank / comment lines after the last entry's content) is one
51
+ * part with key `null`.
52
+ *
53
+ * A raw with fewer than two lines (not a frontmatter block) is one part.
54
+ */
55
+ export declare function splitFrontmatterRaw(raw: string): FrontmatterPart[];
56
+ /** The inverse of {@link splitFrontmatterRaw}: the parts' bytes concatenated. */
57
+ export declare function joinFrontmatterParts(parts: readonly FrontmatterPart[]): string;
58
+ /**
59
+ * The frontmatter block's raw and its trailing trivia, out of a document's
60
+ * `frontmatter_raw` (`raw + docs.frontmatter_trivia`, spec/mutate §2): the
61
+ * closing fence is the first line after the opening one that is `---`
62
+ * followed only by spaces/tabs (the format layer's rule); the raw ends with
63
+ * that line's content, the trivia is everything after. `null` when no closing
64
+ * fence is found (not a frontmatter the format layer would have produced).
65
+ */
66
+ export declare function splitFrontmatterTrivia(frontmatterRaw: string): {
67
+ raw: string;
68
+ trivia: string;
69
+ } | null;
70
+ /** The lines of a part that LEAD its key line (blank / column-0 comment lines), as one string; `""` for a part without a key. */
71
+ export declare function leadOf(part: FrontmatterPart): string;