@yaks/browse 0.0.0-stage → 0.2.36

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 (271) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +324 -2
  3. package/_dist/app.d.ts +8 -0
  4. package/_dist/app.d.ts.map +1 -0
  5. package/_dist/cli.d.ts +16 -0
  6. package/_dist/cli.d.ts.map +1 -0
  7. package/_dist/hosting.d.ts +24 -0
  8. package/_dist/hosting.d.ts.map +1 -0
  9. package/_dist/mod.d.ts +7 -0
  10. package/_dist/mod.d.ts.map +1 -0
  11. package/_dist/types.d.ts +5 -0
  12. package/_dist/types.d.ts.map +1 -0
  13. package/_dist/url.d.ts +2 -0
  14. package/_dist/url.d.ts.map +1 -0
  15. package/app.js +10 -0
  16. package/app.js.map +1 -0
  17. package/app.ts +10 -0
  18. package/browser-links.js +22 -0
  19. package/browser-links.js.map +1 -0
  20. package/browser-links.ts +28 -0
  21. package/cli.js +172 -0
  22. package/cli.js.map +1 -0
  23. package/cli.ts +168 -0
  24. package/client.js +748 -0
  25. package/client.js.map +1 -0
  26. package/client.ts +734 -0
  27. package/clipboard.js +18 -0
  28. package/clipboard.js.map +1 -0
  29. package/clipboard.ts +17 -0
  30. package/commands.js +1157 -0
  31. package/commands.js.map +1 -0
  32. package/commands.ts +1108 -0
  33. package/components/Ansi.tsx +160 -0
  34. package/components/App.tsx +446 -0
  35. package/components/Canvas.tsx +625 -0
  36. package/components/Card.tsx +276 -0
  37. package/components/Chat.tsx +205 -0
  38. package/components/Configuration.tsx +218 -0
  39. package/components/Decision.tsx +49 -0
  40. package/components/Dot.tsx +52 -0
  41. package/components/Entity.tsx +609 -0
  42. package/components/Fault.tsx +43 -0
  43. package/components/Filter.tsx +103 -0
  44. package/components/Keybindings.tsx +100 -0
  45. package/components/Keys.tsx +67 -0
  46. package/components/Markdown.tsx +90 -0
  47. package/components/Navigation.tsx +161 -0
  48. package/components/New.tsx +490 -0
  49. package/components/Peek.tsx +168 -0
  50. package/components/Prio.tsx +20 -0
  51. package/components/Run.tsx +263 -0
  52. package/components/Search.tsx +280 -0
  53. package/components/Stamp.tsx +44 -0
  54. package/components/Status.tsx +514 -0
  55. package/components/Sync.tsx +173 -0
  56. package/components/Tile.tsx +50 -0
  57. package/components/answered.js +23 -0
  58. package/components/answered.js.map +1 -0
  59. package/components/answered.ts +25 -0
  60. package/components/comp.js +11 -0
  61. package/components/comp.js.map +1 -0
  62. package/components/comp.ts +10 -0
  63. package/components/drag.js +173 -0
  64. package/components/drag.js.map +1 -0
  65. package/components/drag.ts +198 -0
  66. package/components/fields.tsx +90 -0
  67. package/components/fixtures/README.md +10 -0
  68. package/components/fixtures/archetypes.json +3646 -0
  69. package/components/hits.js +63 -0
  70. package/components/hits.js.map +1 -0
  71. package/components/hits.ts +84 -0
  72. package/components/icons.tsx +177 -0
  73. package/components/inspect.tsx +331 -0
  74. package/components/mount.ts +79 -0
  75. package/components/nav.tsx +479 -0
  76. package/components/offers.js +26 -0
  77. package/components/offers.js.map +1 -0
  78. package/components/offers.ts +54 -0
  79. package/components/page.js +21 -0
  80. package/components/page.js.map +1 -0
  81. package/components/page.ts +18 -0
  82. package/components/read.js +5 -0
  83. package/components/read.js.map +1 -0
  84. package/components/read.ts +7 -0
  85. package/components/registry.js +285 -0
  86. package/components/registry.js.map +1 -0
  87. package/components/registry.ts +317 -0
  88. package/components/session_status.tsx +115 -0
  89. package/components/subscriptions.js +149 -0
  90. package/components/subscriptions.js.map +1 -0
  91. package/components/subscriptions.ts +161 -0
  92. package/components/title.tsx +21 -0
  93. package/components/useQuery.js +166 -0
  94. package/components/useQuery.js.map +1 -0
  95. package/components/useQuery.ts +211 -0
  96. package/components/views/Board.tsx +450 -0
  97. package/components/views/BoardTile.tsx +65 -0
  98. package/components/views/Dashboard.tsx +148 -0
  99. package/components/views/Debug.tsx +445 -0
  100. package/components/views/Dependency.tsx +51 -0
  101. package/components/views/Entry.tsx +500 -0
  102. package/components/views/Gist.tsx +28 -0
  103. package/components/views/Inline.tsx +54 -0
  104. package/components/views/Json.tsx +19 -0
  105. package/components/views/Layout.tsx +211 -0
  106. package/components/views/List.tsx +238 -0
  107. package/components/views/Md.tsx +43 -0
  108. package/components/views/Media.tsx +48 -0
  109. package/components/views/Memory.tsx +32 -0
  110. package/components/views/Relate.tsx +205 -0
  111. package/components/views/Search.tsx +42 -0
  112. package/components/views/Session.tsx +850 -0
  113. package/components/views/SessionManage.tsx +127 -0
  114. package/components/views/Show.tsx +758 -0
  115. package/components/views/Shown.tsx +72 -0
  116. package/components/views/Split.tsx +58 -0
  117. package/components/views/TaskTile.tsx +37 -0
  118. package/components/views/Title.tsx +105 -0
  119. package/components/views/Wake.tsx +77 -0
  120. package/components/views/Web.tsx +49 -0
  121. package/components/wells.js +30 -0
  122. package/components/wells.js.map +1 -0
  123. package/components/wells.ts +33 -0
  124. package/configuration.js +38 -0
  125. package/configuration.js.map +1 -0
  126. package/configuration.ts +41 -0
  127. package/decision_sync.js +88 -0
  128. package/decision_sync.js.map +1 -0
  129. package/decision_sync.ts +93 -0
  130. package/deno.json +1 -0
  131. package/domain-host.tsx +108 -0
  132. package/edge.js +193 -0
  133. package/edge.js.map +1 -0
  134. package/edge.ts +196 -0
  135. package/entry_log.js +241 -0
  136. package/entry_log.js.map +1 -0
  137. package/entry_log.ts +214 -0
  138. package/failure.js +22 -0
  139. package/failure.js.map +1 -0
  140. package/failure.ts +34 -0
  141. package/front.json +61 -0
  142. package/highlight.js +143 -0
  143. package/highlight.js.map +1 -0
  144. package/highlight.ts +149 -0
  145. package/history.js +68 -0
  146. package/history.js.map +1 -0
  147. package/history.ts +72 -0
  148. package/host_commands.js +107 -0
  149. package/host_commands.js.map +1 -0
  150. package/host_commands.ts +38 -0
  151. package/hosting.js +53 -0
  152. package/hosting.js.map +1 -0
  153. package/hosting.ts +72 -0
  154. package/idb.js +14 -0
  155. package/idb.js.map +1 -0
  156. package/idb.ts +15 -0
  157. package/index.js +232 -0
  158. package/index.js.map +1 -0
  159. package/index.ts +252 -0
  160. package/keybindings.js +136 -0
  161. package/keybindings.js.map +1 -0
  162. package/keybindings.ts +37 -0
  163. package/kit.js +103 -0
  164. package/kit.js.map +1 -0
  165. package/kit.ts +119 -0
  166. package/layout.js +254 -0
  167. package/layout.js.map +1 -0
  168. package/layout.ts +210 -0
  169. package/legacy_outbox.js +53 -0
  170. package/legacy_outbox.js.map +1 -0
  171. package/legacy_outbox.ts +63 -0
  172. package/live.js +2883 -0
  173. package/live.js.map +1 -0
  174. package/live.ts +3334 -0
  175. package/live_archetypes.js +92 -0
  176. package/live_archetypes.js.map +1 -0
  177. package/live_archetypes.ts +113 -0
  178. package/live_client.js +145 -0
  179. package/live_client.js.map +1 -0
  180. package/live_client.ts +120 -0
  181. package/main.tsx +5 -0
  182. package/mod.js +7 -0
  183. package/mod.js.map +1 -0
  184. package/mod.ts +7 -0
  185. package/mount.tsx +50 -0
  186. package/navigation.js +120 -0
  187. package/navigation.js.map +1 -0
  188. package/navigation.ts +152 -0
  189. package/near.js +83 -0
  190. package/near.js.map +1 -0
  191. package/near.ts +83 -0
  192. package/new.js +295 -0
  193. package/new.js.map +1 -0
  194. package/new.ts +361 -0
  195. package/outbox.js +112 -0
  196. package/outbox.js.map +1 -0
  197. package/outbox.ts +129 -0
  198. package/owner.js +39 -0
  199. package/owner.js.map +1 -0
  200. package/owner.ts +37 -0
  201. package/package.json +81 -4
  202. package/paste.js +157 -0
  203. package/paste.js.map +1 -0
  204. package/paste.ts +122 -0
  205. package/props.js +303 -0
  206. package/props.js.map +1 -0
  207. package/props.ts +358 -0
  208. package/providers.js +136 -0
  209. package/providers.js.map +1 -0
  210. package/providers.ts +194 -0
  211. package/query.js +987 -0
  212. package/query.js.map +1 -0
  213. package/query.ts +1113 -0
  214. package/remote.js +37 -0
  215. package/remote.js.map +1 -0
  216. package/remote.ts +46 -0
  217. package/resolver.js +149 -0
  218. package/resolver.js.map +1 -0
  219. package/resolver.ts +213 -0
  220. package/rfc2047.js +37 -0
  221. package/rfc2047.js.map +1 -0
  222. package/rfc2047.ts +40 -0
  223. package/row_holds.js +107 -0
  224. package/row_holds.js.map +1 -0
  225. package/row_holds.ts +93 -0
  226. package/served.ts +42 -0
  227. package/session_worktree.js +7 -0
  228. package/session_worktree.js.map +1 -0
  229. package/session_worktree.ts +16 -0
  230. package/sessions.js +57 -0
  231. package/sessions.js.map +1 -0
  232. package/sessions.ts +53 -0
  233. package/sha.js +161 -0
  234. package/sha.js.map +1 -0
  235. package/sha.ts +164 -0
  236. package/styles.css +2823 -0
  237. package/subs.js +36 -0
  238. package/subs.js.map +1 -0
  239. package/subs.ts +40 -0
  240. package/terminal-route.js +13 -0
  241. package/terminal-route.js.map +1 -0
  242. package/terminal-route.ts +17 -0
  243. package/terminal.js +251 -0
  244. package/terminal.js.map +1 -0
  245. package/terminal.ts +186 -0
  246. package/time.js +142 -0
  247. package/time.js.map +1 -0
  248. package/time.ts +133 -0
  249. package/title.js +25 -0
  250. package/title.js.map +1 -0
  251. package/title.ts +50 -0
  252. package/tui/doc.ts +6 -0
  253. package/tui/testing.ts +19 -0
  254. package/twin.js +11 -0
  255. package/twin.js.map +1 -0
  256. package/twin.ts +13 -0
  257. package/types.js +456 -0
  258. package/types.js.map +1 -0
  259. package/types.ts +1370 -0
  260. package/url.js +77 -0
  261. package/url.js.map +1 -0
  262. package/url.ts +85 -0
  263. package/verb.js +82 -0
  264. package/verb.js.map +1 -0
  265. package/verb.ts +137 -0
  266. package/warmth.js +41 -0
  267. package/warmth.js.map +1 -0
  268. package/warmth.ts +54 -0
  269. package/wire.js +86 -0
  270. package/wire.js.map +1 -0
  271. package/wire.ts +63 -0
package/query.js ADDED
@@ -0,0 +1,987 @@
1
+ // The FILTER grammar — dot-params in URL query-param form. One parser for
2
+ // every reader: a board's saved query, `task list`, MCP task_list, and
3
+ // the search box. (Writes keep the plain `.comp.prop=value` setter grammar in
4
+ // client.ts — a setter's comma is a literal comma; only filters interpret
5
+ // value forms.)
6
+ //
7
+ // .status=open&.priority<=1&.domain=Ops,Eng
8
+ // runner exit .status=done .updated.at=today (search-style mix)
9
+ //
10
+ // .prop=v equals (string-compared, like everything on the wire)
11
+ // .prop=a,b,c any of
12
+ // .prop=1..5 range, inclusive (1...5 excludes the end; ISO dates
13
+ // compare fine lexicographically)
14
+ // !prop null / absent
15
+ // .prop present (including an empty string)
16
+ // ?prop carried beside the filter, never filtered on
17
+ // .prop!=v not — negates any value form above
18
+ // .prop~=v contains, case-insensitive
19
+ // .prop~= present — an empty needle asks for the COLUMN, the same
20
+ // thing `.prop` asks (never "every row contains ''")
21
+ // .prop<v <=v >v >=v comparisons — a number column compares numerically,
22
+ // everything else as text (an ISO stamp compares right as
23
+ // text); an operand the column's type cannot hold selects
24
+ // nothing, the same mismatch @yaks/sql declines to compile
25
+ //
26
+ // .limit=200 a WINDOW: 200 matches, not the whole set
27
+ // .after=13882 continue that window past one entity, named by its number
28
+ // or eid — the next page, in whatever order was asked for
29
+ //
30
+ // Bare words are TEXT preds — FTS5 terms over the doc (title or body), with a
31
+ // trailing `*` for token-prefix matching; "quoted words" stay one phrase pred.
32
+ // Separators are '&' and
33
+ // whitespace both: every term stands alone; quote multiword values
34
+ // (.title~="two words"). Single/double quotes and escaping are shared grammar.
35
+ //
36
+ // When the ROW value is an ISO timestamp, the filter value may be a time
37
+ // PHRASE (see span): today, yesterday, tomorrow, now, this|last|next
38
+ // minute|hour|day|week|month|year, "5 minutes ago", "in 2 days" (short
39
+ // units too: in 60m, after 8h), a clock time on today or a named day
40
+ // (9am, 9:30pm, 14:00, noon, "9am tomorrow"), a date, or a full stamp
41
+ // (2026-07-25T09:00) — with '-'/'_' glue where quoting is awkward:
42
+ // 1-hour-ago. A phrase names a
43
+ // RANGE and the op picks its edge: = within, >= from its start, <= until
44
+ // its end, > strictly after, < strictly before. So .updated.at=today is
45
+ // midnight-to-midnight, .updated.at>="1 hour ago" is the last hour.
46
+ // Schedulers want one moment instead — that's instant(), below span.
47
+ //
48
+ // Names resolve the way the host resolves them, through @yaks/vocab `aim`: a
49
+ // property is named with its component (`.task.status=open`), and a name by
50
+ // itself tests the component: `!proposed` means absent, `.proposed` present.
51
+ //
52
+ // References are ordinary props: `.filed.assignee=jeff`; the VALUE resolves
53
+ // like any id (alias, T-3, raw eid) at whichever door or evaluator holds the
54
+ // graph (resolveRefs). A path is an N-hop CHAIN of `comp.prop` pairs: each
55
+ // `{eid}` deref moves to the target entity and the next pair reads there, so
56
+ // `.comment.target.doc.title~=foo` walks comment→target then tests doc.title,
57
+ // arbitrarily deep.
58
+ import { IdError } from "./types.js";
59
+ import { anyRef, isRef, parseProp, typed } from "./props.js";
60
+ import { edges, kindOrder, kindWord, lazy, statusOf, vocab } from "./types.js";
61
+ import { Unknown, unknownWord, valueless } from "@yaks/vocab";
62
+ import { term as ftsTerm } from "@yaks/fts";
63
+ import { check } from "@yaks/match";
64
+ import { literals, tagOf } from "@yaks/sql";
65
+ import { bare, compares, parse, scalar } from "@yaks/query";
66
+ import { frozen } from "@yaks/fp";
67
+ export { ftsTerm };
68
+ export { WALK_DEPTH, WALK_LIMIT } from "@yaks/query";
69
+ // A window over a RANKING (`.order=hot`, `.order=similar`) rather than over the
70
+ // spine. An explicit order SURVIVES a window — a window says how much of a
71
+ // sequence to answer with, never which sequence — so the cursor is the anchor's
72
+ // place in the RANKING, not a num to compare against. The cursor spelling never
73
+ // changes: `.after=<num>` names an entity, and each evaluator derives where that
74
+ // entity sits in the order it was asked for (the same rule @yaks/sql compiles as
75
+ // a keyset and @yaks/match answers in memory). An anchor the ranking does not
76
+ // hold restarts from the front, which is what a first page already is.
77
+ export let pageRanked = (rows, win)=>{
78
+ let at = win.after == null ? -1 : rows.findIndex((r)=>typeof win.after == 'number' ? r.num == win.after : r.eid == win.after);
79
+ let rest = rows.slice(at + 1);
80
+ return win.limit == null ? rest : rest.slice(0, win.limit);
81
+ };
82
+ // A path's leaf — the far column op/value tests — is the last hop; a plain
83
+ // pred is its own leaf. Every reader resolving the tested column (its type,
84
+ // ref-resolution, quarantine reveal) reads THROUGH this, never `p.comp`.
85
+ export let leafOf = (p)=>p.at ? p.at[p.at.length - 1] : p;
86
+ /** A property predicate's structured operand, including hand-built scalars. */ export let operandOf = (p)=>p.prop && [
87
+ '',
88
+ '!',
89
+ '~',
90
+ '<',
91
+ '<=',
92
+ '>',
93
+ '>='
94
+ ].includes(p.op) ? p.operand ?? scalar(p.value) : undefined;
95
+ // One token — '.filed.priority<=1', '.filed.domain=Ops,Eng' — to a Pred; null if the
96
+ // string isn't a dot-param at all.
97
+ export let EXISTS = 'exists';
98
+ // `?loan` asks for a component WITHOUT filtering on it: an optional
99
+ // presence, requested. It selects nothing and screens nothing — `.book` says
100
+ // which entities the answer is about, `.book&?loan` says the answer should
101
+ // carry their loans too, where they have one. A door that projects reads it
102
+ // (workers/yak/graph.ts); every evaluator lets it through like a ranking.
103
+ export let WANT = 'want';
104
+ let OPS = {
105
+ '=': '',
106
+ '!': EXISTS,
107
+ '!=': '!',
108
+ '~=': '~',
109
+ '<': '<',
110
+ '<=': '<=',
111
+ '>': '>',
112
+ '>=': '>='
113
+ };
114
+ // `.order=hot`, `.order=search`, and `.order=similar` are rankings, not
115
+ // filters: matchQuery lets them through, adopt() ignores them, and orderOf()
116
+ // hands the value to whoever sorts. The server answers a window of words best
117
+ // match first (@yaks/fts); a filter with no words stays newest first.
118
+ // Similar asks the evaluator that owns the embedding service for
119
+ // vector-neighbor rank.
120
+ export let ORDER = 'order';
121
+ export let RANKS = [
122
+ 'hot',
123
+ 'search',
124
+ 'similar'
125
+ ];
126
+ export let orderOf = (preds)=>preds.find((p)=>p.op == ORDER)?.value;
127
+ // `.near=T-3` names the entity whose doc supplies a similarity query. It is a
128
+ // ranking input beside ORDER, never graph membership: the evaluator resolves
129
+ // it and projects transient rank onto the selected neighbors.
130
+ export let NEAR = 'near';
131
+ export let nearOf = (preds)=>preds.find((p)=>p.op == NEAR)?.value;
132
+ // An AGGREGATE directive rides the pred list like ORDER —
133
+ // `.distinct=filed.domain`, `.tally=filed.domain`, `.count`. matchQuery passes AGG through (true), so the OTHER
134
+ // preds select the universe the aggregate reduces; a reader pulls the projection
135
+ // with aggOf() and computes it with tally() (or aggregateSql server-side).
136
+ export let AGG = 'agg';
137
+ // `count` names no column, so its `at` is the empty hop — a reader branches on
138
+ // `op`, never on whether `at` is populated.
139
+ export let aggOf = (preds)=>{
140
+ let p = preds.find((p)=>p.op == AGG);
141
+ return p?.agg ? {
142
+ op: p.agg,
143
+ at: {
144
+ comp: p.comp,
145
+ prop: p.prop
146
+ }
147
+ } : undefined;
148
+ };
149
+ // A WINDOW directive rides the pred list like ORDER/AGG/PROJECT —
150
+ // `.limit=200&.after=13882`. matchQuery passes WINDOW through (true), so the
151
+ // OTHER preds select the whole membership and the window only bounds how much
152
+ // of it a door answers with.
153
+ export let WINDOW = 'window';
154
+ // The window a query asks for, folded from every WINDOW pred it carries — so
155
+ // `.limit=50&.after=900` reads as one bound however it was spelled, and a later
156
+ // pred wins a repeated one. `{}` when the query names no window: a door reads
157
+ // `limit == null` as "the whole answer", which is what keeps an unwindowed
158
+ // query's frame semantics exactly what they were.
159
+ export let windowOf = (preds)=>{
160
+ let out = {};
161
+ for (let p of preds){
162
+ if (p.op != WINDOW || !p.win) continue;
163
+ if (p.win.limit != null) out.limit = p.win.limit;
164
+ if (p.win.after != null) out.after = p.win.after;
165
+ }
166
+ return out;
167
+ };
168
+ // The components a pred list READS — the dirty test an aggregate subscription
169
+ // applies to a committed batch: a change dirties the aggregate iff it touches a
170
+ // component the selection or the aggregated column reads. `null` means "every
171
+ // batch dirties it": a path hop, a reverse hop or a `.refs` union reads columns
172
+ // on OTHER entities, which no comp-name overlap can name, so those recompute
173
+ // unconditionally rather than answer stale. The spine (`entity`) and
174
+ // `quarantined` are the caller's to add — every query reads them.
175
+ export let predComps = (preds)=>{
176
+ let out = new Set();
177
+ for (let p of preds){
178
+ if (p.op == OR) {
179
+ for (let alt of p.alts){
180
+ let inner = predComps(alt);
181
+ if (!inner) return null;
182
+ for (let c of inner)out.add(c);
183
+ }
184
+ continue;
185
+ }
186
+ if (p.refs || p.at || p.rev) return null;
187
+ if (p.op == ORDER && p.value == 'hot') return null;
188
+ // A column order reads its column: a write to it can move a row across a
189
+ // window's edge. One through a reference reads another entity.
190
+ if (p.op == ORDER && !RANKS.includes(p.value)) {
191
+ let path = p.value.replace(/^-/, '').split('.');
192
+ if (path.length > 2) return null;
193
+ out.add(path[0]);
194
+ }
195
+ if (p.op == NEVER || p.op == ORDER || p.op == NEAR || p.op == PROJECT) continue;
196
+ if (p.op == WINDOW) continue;
197
+ // `.count` aggregates the selection, naming no column of its own.
198
+ if (p.op == AGG && !p.comp) continue;
199
+ if (!p.comp) return null;
200
+ // task.status is virtual: a filter/window/aggregate over it reads all four
201
+ // lifecycle facets, not only the task row that owns the public spelling.
202
+ // Without this expansion a claim or terminal mark changes the answer but
203
+ // never dirties a standing tally or exact window.
204
+ if (p.comp == 'task' && p.prop == 'status') {
205
+ for (let comp of [
206
+ 'task',
207
+ 'completed',
208
+ 'cancelled',
209
+ 'claim'
210
+ ]){
211
+ out.add(comp);
212
+ }
213
+ } else out.add(p.comp);
214
+ }
215
+ return out;
216
+ };
217
+ // The aggregate itself, over the rows a query matched: value → count, empties
218
+ // (null and '') dropped exactly as the census always has. Distinct values are
219
+ // its keys; a reader that wants them sorted takes `[...tally(rows, at).keys()]`.
220
+ export let tally = (rows, at)=>{
221
+ let m = new Map();
222
+ for (let c of rows){
223
+ // read() so a DERIVED column (task.status) tallies its computed value, not
224
+ // the absent stored one — board column counts stay honest (D-24102).
225
+ let v = read(c, at.comp, at.prop);
226
+ if (v == null || v === '') continue;
227
+ let k = String(v);
228
+ m.set(k, (m.get(k) ?? 0) + 1);
229
+ }
230
+ return m;
231
+ };
232
+ // The census in one line: the sorted distinct values of a column over `rows`.
233
+ export let distinctValues = (rows, at)=>[
234
+ ...tally(rows, at).keys()
235
+ ].sort();
236
+ // A FIELD PROJECTION directive rides the pred list like AGG/ORDER —
237
+ // `.fields=pin.x,pin.z~`. matchQuery passes PROJECT through (true), so the OTHER
238
+ // preds select the membership; fieldsOf() reads which columns each row carries
239
+ // (and which are volatile), and select() (sql.ts) selects them.
240
+ export let PROJECT = 'project';
241
+ // The projected columns of a query, or undefined when it names none — a plain
242
+ // membership query. The waking subset is `f.wake`; a caller that only cares which
243
+ // changes should re-fire reads `fields.filter((f) => f.wake)`.
244
+ export let fieldsOf = (preds)=>preds.find((p)=>p.op == PROJECT)?.fields;
245
+ // A WALK pred — `.requires[<=3]->T-42` — is a real filter, not a rider: it
246
+ // SELECTS a transitive closure over one step, optionally capped by hops.
247
+ // sql.ts compiles it to a recursive CTE over the indexed dep table (or the
248
+ // reference column); the JS matcher answers it from a `walk` that resolves the
249
+ // same closure once per query rather than per row.
250
+ export let REACHES = 'reaches';
251
+ // The traversal closures a pred list asks for, deduped — what a door precomputes
252
+ // before matching so the walk happens once, not per candidate row.
253
+ export let reachesOf = (preds)=>preds.filter((p)=>p.op == REACHES);
254
+ // A query addresses the LAZY entry partition when it names a component the
255
+ // kernel declares `lazy` — `.entry.session`, `.entry`. Those entities are omitted from the root
256
+ // snapshot, so a door reaches them only when the query OPTS IN by naming the
257
+ // partition; that is an explicit scope, not a silent boundary. Every query
258
+ // door reads this one predicate to decide whether entries are in its universe.
259
+ //
260
+ // A component-ABSENCE assertion (`p.prop == '' && p.op == ''`) does not opt in:
261
+ // it selects eager entities that LACK the comp, the opposite of wanting entries.
262
+ // kindPreds emits one per kindOrder-earlier comp, so `kind=comment` carries a
263
+ // synthetic `.entry absent` — without this guard it flipped every eager kind
264
+ // past `entry` into entry-partition mode and orderedEntries dropped every row.
265
+ // A positive reference (`.entry` EXISTS, `.entry.session=S-1`) still scopes in.
266
+ export let namesLazy = (preds)=>preds.some((p)=>lazy(p.comp) && !(p.prop == '' && p.op == ''));
267
+ // The sessions an entry query is scoped to — the eids of every scalar
268
+ // `.entry.session=` equality (a comma list is any-of). A range cannot name
269
+ // keyed partitions, so it stays unscoped and the bounded scan refines it.
270
+ // resolveRefs has already turned S-16765 into its eid by the time a door asks.
271
+ export let scopedSessions = (preds)=>preds.filter((p)=>p.comp == 'entry' && p.prop == 'session' && p.op == '').flatMap((p)=>literals(operandOf(p) ?? null) ?? []).filter(Boolean);
272
+ // Quarantine is invisible by default, but mentioning the facet is the
273
+ // deliberate extra step that lets a list ask about it. This stays beside
274
+ // matchQuery rather than inside it: writers and keyed internals still need to
275
+ // reason about a row without silently changing the question they asked.
276
+ export let reveals = (preds)=>preds.some((p)=>p.comp == 'quarantined' || leafOf(p).comp == 'quarantined');
277
+ export let listed = (comps, preds)=>!comps.quarantined || reveals(preds);
278
+ // A `blob` is content-addressed STORAGE wearing an entity's clothes: every doc
279
+ // body lands as one, and an attachment's bytes as another, so
280
+ // the store's own rows sit in the spine a filter selects from. They carry no
281
+ // doc and no kind, so a listing that catches one renders nothing for it — which
282
+ // is what a person saw (C-32498 item 4). Naming the component is the deliberate
283
+ // opt-in, the same step `quarantined` asks for.
284
+ //
285
+ // `image` names them too: dimensions belong to the content (its row keys on
286
+ // artifact), so a filter that asks for one can be asking for nothing else,
287
+ // and a photo wall reading what its pictures measure should not have to say
288
+ // `.artifact` to be allowed the answer (C-32706 item 1).
289
+ let ON_BLOB = [
290
+ 'artifact',
291
+ 'image'
292
+ ];
293
+ export let namesBlobs = (preds)=>preds.some((p)=>ON_BLOB.includes(p.comp) || ON_BLOB.includes(leafOf(p).comp));
294
+ // Does this row belong in the answer a FILTER selects? The caller's own preds
295
+ // decided that; these are the two screens every listing carries beside them.
296
+ // `id=` addresses entities instead of selecting them, so it keeps `listed`
297
+ // alone — naming a blob's sha IS asking for it.
298
+ export let selected = (comps, preds)=>listed(comps, preds) && (!comps.artifact || namesBlobs(preds));
299
+ // The pred list a COMPILED membership statement should carry: the caller's
300
+ // filter plus the universal screens a door otherwise applies in JS after the
301
+ // statement — quarantine and the store's blob rows (selected) and the lazy
302
+ // entry partition (namesLazy). All three are spelled as ordinary
303
+ // component-ABSENCE preds, so the existing compiler answers them from the same
304
+ // LEFT JOIN it gives any facet, and namesLazy's `.prop == '' && .op == ''`
305
+ // guard keeps the entry screen from reading as an opt-IN to the partition.
306
+ //
307
+ // Why it matters beyond tidiness: a JS filter that runs AFTER a statement's
308
+ // LIMIT under-fills the page, so a window can only be exact once the screens
309
+ // the answer depends on are inside the same statement.
310
+ let absent = (comp)=>({
311
+ comp,
312
+ prop: '',
313
+ op: '',
314
+ value: ''
315
+ });
316
+ export let screened = (preds, entries)=>[
317
+ ...preds,
318
+ ...reveals(preds) ? [] : [
319
+ absent('quarantined')
320
+ ],
321
+ ...namesBlobs(preds) ? [] : [
322
+ absent('artifact')
323
+ ],
324
+ ...entries || preds.some((p)=>p.op == TEXT) ? [] : [
325
+ absent('entry')
326
+ ]
327
+ ];
328
+ // kind=K as a filter, not a JS screen. kindOf is "the first kindOrder
329
+ // component present", so kind=K is K present AND every earlier component
330
+ // absent — a synthetic Pred[] the SQL compiler answers from the index, where
331
+ // a lone kind= otherwise built the whole 27 MB snapshot to screen it in JS.
332
+ // The absence clauses are what make it EXACT: an entity wearing both `memory`
333
+ // and `comment` is a comment (comment is earlier), so kind=memory must skip
334
+ // it — presence (`.memory`) cannot, and overcounts. null for a word naming
335
+ // no kind (kind=entity, a typo): the derived `entity` fallback is every
336
+ // kindOrder comp absent, and its only reader is the JS screen that stays.
337
+ export let kindPreds = (kind)=>{
338
+ let i = kindOrder.indexOf(kind);
339
+ if (i < 0) return null;
340
+ return [
341
+ {
342
+ comp: kind,
343
+ prop: '',
344
+ op: EXISTS,
345
+ value: ''
346
+ },
347
+ ...kindOrder.slice(0, i).map((c)=>({
348
+ comp: c,
349
+ prop: '',
350
+ op: '',
351
+ value: ''
352
+ }))
353
+ ];
354
+ };
355
+ // `.kind=memory` is a SCOPE, not a column: kind is derived (the first
356
+ // kindOrder component an entity wears), so it reads as the exact presence
357
+ // clauses kindPreds builds, as @yaks/sql reads it. kindWord folds the plural
358
+ // in (`.kind=projects` reads like `.kind=project`).
359
+ let kindScope = (value)=>{
360
+ let out = kindPreds(kindWord(value) ?? value);
361
+ if (!out) throw new Error(`no such kind: ${value || '(empty)'}`);
362
+ return out;
363
+ };
364
+ // Time operands stay authored; the shared matcher resolves their phrases and
365
+ // literal scalars. Other property input sugar uses the same parser writes use.
366
+ let atom = (p, value)=>{
367
+ if (!value) return value;
368
+ if (tagFor(p) == 'time') return value;
369
+ if (tagFor(p) == 'eid') {
370
+ try {
371
+ return String(parseProp(p, value));
372
+ } catch {
373
+ return value // aliases need the evaluator's graph
374
+ ;
375
+ }
376
+ }
377
+ return String(parseProp(p, value));
378
+ };
379
+ let mapValue = (value, f)=>value.kind == 'list' ? {
380
+ ...value,
381
+ items: value.items.map((v)=>mapValue(v, f))
382
+ } : value.kind == 'range' ? {
383
+ ...value,
384
+ lo: mapValue(value.lo, f),
385
+ hi: mapValue(value.hi, f)
386
+ } : {
387
+ ...value,
388
+ raw: f(value.raw)
389
+ };
390
+ // Grammar belongs to @yaks/query. This pass binds its schema-free clauses to
391
+ // the fleet's routed predicates (also consumed by browser matching, reference
392
+ // resolution and partition screens). Never stringify a clause to parse it again.
393
+ let flatValue = (v)=>{
394
+ if (!v) return '';
395
+ if (v.kind == 'scalar' || v.kind == 'time') return v.raw;
396
+ if (v.kind == 'list') return v.items.map(flatValue).join(',');
397
+ return `${flatValue(v.lo)}..${v.exclusiveEnd ? '.' : ''}${flatValue(v.hi)}`;
398
+ };
399
+ // What a column is to a value comparison, as @yaks/sql reads it: a shared
400
+ // reference names entities, and a column no component declares is text.
401
+ let tagFor = (hop)=>{
402
+ if (!hop.comp) return 'eid';
403
+ let p = vocab.prop(hop.comp, hop.prop);
404
+ return p ? tagOf(p) : 'text';
405
+ };
406
+ let columnOf = (path, directive)=>{
407
+ let hops = vocab.aim(path.join('.'));
408
+ let at = hops[hops.length - 1];
409
+ if (hops.length != 1 || !at.prop) {
410
+ throw new Error(`${directive} names one column, not a path: ${path.join('.')}`);
411
+ }
412
+ return at;
413
+ };
414
+ // A projected column: the row's own, or a path through its references to a
415
+ // column of the entity each one names.
416
+ let fieldOf = (path, wake)=>{
417
+ let hops = vocab.aim(path.join('.'));
418
+ let [first, ...at] = hops;
419
+ if (hops.length == 1 && !first.prop && !vocab.props(first.comp).length && !vocab.comp(first.comp)?.computed) {
420
+ return {
421
+ ...first,
422
+ wake
423
+ };
424
+ }
425
+ let through = (h, i)=>!!h.prop && (i == hops.length - 1 || vocab.prop(h.comp, h.prop)?.category == 'ref');
426
+ if (!hops.every(through)) {
427
+ throw new Error(`.fields names a column, or one through references: ${path.join('.')}`);
428
+ }
429
+ return {
430
+ comp: first.comp,
431
+ prop: first.prop,
432
+ wake,
433
+ ...at.length ? {
434
+ at
435
+ } : {}
436
+ };
437
+ };
438
+ // A directive inside an alternative would be read by nobody: orderOf, fieldsOf
439
+ // and the riders scan the top-level list. Refuse it there rather than let
440
+ // `(.a=1&.order=hot|.b=2)` quietly drop the order.
441
+ let alternative = (preds)=>{
442
+ let rider = preds.find((p)=>[
443
+ ORDER,
444
+ NEAR,
445
+ AGG,
446
+ WINDOW,
447
+ PROJECT,
448
+ WANT
449
+ ].includes(p.op));
450
+ if (rider) {
451
+ throw new Error(`.${rider.op} belongs outside the | alternatives`);
452
+ }
453
+ return preds;
454
+ };
455
+ let bindClause = (c)=>{
456
+ switch(c.kind){
457
+ case 'and':
458
+ return c.clauses.flatMap((c)=>bindClause(c));
459
+ case 'or':
460
+ return [
461
+ {
462
+ comp: '',
463
+ prop: '',
464
+ op: OR,
465
+ value: '',
466
+ alts: c.clauses.map((a)=>alternative(bindClause(a)))
467
+ }
468
+ ];
469
+ case 'never':
470
+ return [
471
+ never()
472
+ ];
473
+ case 'text':
474
+ return [
475
+ text(c.value)
476
+ ];
477
+ case 'resource':
478
+ if (/^[0-9a-f]{6,64}$/i.test(c.comp)) {
479
+ return bindClause(parse(`.entity.eid=#${c.comp}`));
480
+ }
481
+ throw new Error(`a fleet read cannot evaluate resource #${c.comp}`);
482
+ case 'every':
483
+ return [] // full rows are the fleet's default projection
484
+ ;
485
+ case 'order':
486
+ return [
487
+ {
488
+ comp: '',
489
+ prop: 'order',
490
+ op: ORDER,
491
+ value: c.value
492
+ }
493
+ ];
494
+ case 'near':
495
+ return [
496
+ {
497
+ comp: '',
498
+ prop: 'near',
499
+ op: NEAR,
500
+ value: c.value
501
+ }
502
+ ];
503
+ case 'refs':
504
+ return [
505
+ {
506
+ comp: '',
507
+ prop: '',
508
+ op: c.op == '!' ? EXISTS : '',
509
+ value: c.value,
510
+ refs: true
511
+ }
512
+ ];
513
+ case 'count':
514
+ return [
515
+ {
516
+ comp: '',
517
+ prop: '',
518
+ op: AGG,
519
+ value: '',
520
+ agg: 'count'
521
+ }
522
+ ];
523
+ case 'distinct':
524
+ case 'tally':
525
+ return [
526
+ {
527
+ ...columnOf(c.path, `.${c.kind}`),
528
+ op: AGG,
529
+ value: '',
530
+ agg: c.kind
531
+ }
532
+ ];
533
+ case 'fields':
534
+ return [
535
+ {
536
+ comp: '',
537
+ prop: '',
538
+ op: PROJECT,
539
+ value: '',
540
+ fields: c.fields.filter((f)=>f.path.join('.') != 'entity.eid').map((f)=>fieldOf(f.path, f.wake))
541
+ }
542
+ ];
543
+ case 'limit':
544
+ case 'after':
545
+ {
546
+ let cursor = 'n' in c ? c.n : c.eid;
547
+ return [
548
+ {
549
+ comp: '',
550
+ prop: '',
551
+ op: WINDOW,
552
+ value: String(cursor),
553
+ win: {
554
+ [c.kind]: cursor
555
+ }
556
+ }
557
+ ];
558
+ }
559
+ case 'walk':
560
+ {
561
+ let path = c.path.join('.');
562
+ let reach = {
563
+ type: path,
564
+ depth: c.depth,
565
+ dir: c.dir
566
+ };
567
+ if (!edges.includes(path)) {
568
+ let via = columnOf(c.path, 'a walk');
569
+ if (!isRef(via.comp, via.prop)) {
570
+ throw new Error(`a walk follows an edge type (${edges.join(', ')}) or a reference column — not ${path}`);
571
+ }
572
+ reach.via = via;
573
+ }
574
+ return [
575
+ {
576
+ comp: '',
577
+ prop: '',
578
+ op: REACHES,
579
+ value: c.target,
580
+ reach
581
+ }
582
+ ];
583
+ }
584
+ case 'pred':
585
+ break;
586
+ default:
587
+ throw new Error(`a fleet read cannot evaluate ${c.kind}`);
588
+ }
589
+ let segs = c.path, op = c.op, value = flatValue(c.value);
590
+ let assoc = vocab.assoc(segs[0]);
591
+ if (assoc) {
592
+ let inner = c.where ? bindClause(c.where) : segs.length > 1 ? bindClause({
593
+ ...c,
594
+ path: segs.slice(1),
595
+ not: undefined
596
+ }) : [];
597
+ let count = !inner.length && !(op == '!' || op == '~=' && !value || op == '=' && !value);
598
+ if (count && !/^\d+$/.test(value)) {
599
+ throw new Error(`.${segs[0]} needs a sub-path (.${segs[0]}.<prop>) or a count (.${segs[0]}>=N)`);
600
+ }
601
+ return [
602
+ {
603
+ ...assoc,
604
+ op: count ? OPS[op] : EXISTS,
605
+ value: count ? value : '',
606
+ rev: {
607
+ ...assoc,
608
+ name: segs[0],
609
+ preds: inner,
610
+ not: inner.length ? !!c.not : op == '=' && !value,
611
+ ...count ? {
612
+ count: true
613
+ } : {}
614
+ }
615
+ }
616
+ ];
617
+ }
618
+ if (c.not || c.where) {
619
+ throw new Error(`.${segs[0]} is not a reverse association`);
620
+ }
621
+ if (segs.length == 1 && segs[0] == 'kind') return kindScope(value);
622
+ if (op == '?') return [
623
+ {
624
+ ...wanted(c),
625
+ op: WANT,
626
+ value: ''
627
+ }
628
+ ];
629
+ let hops = aimed(c);
630
+ let leaf = hops[hops.length - 1], derefs = hops.slice(0, -1);
631
+ if (!leaf.prop && compares(c)) {
632
+ throw new Unknown(leaf.comp, valueless(vocab, segs.join('.'), op, value));
633
+ }
634
+ for (let d of derefs){
635
+ if (!isRef(d.comp, d.prop)) {
636
+ throw new Error(`.${d.prop || d.comp} is not a reference — paths walk reference columns`);
637
+ }
638
+ }
639
+ let p = derefs.length ? {
640
+ ...derefs[0],
641
+ op: OPS[op],
642
+ value,
643
+ at: [
644
+ ...derefs.slice(1),
645
+ leaf
646
+ ]
647
+ } : {
648
+ ...leaf,
649
+ op: OPS[op],
650
+ value
651
+ };
652
+ if (!p.prop) {
653
+ if (p.value || p.op != '' && p.op != '~' && p.op != EXISTS) {
654
+ throw new Error(`component filters are presence tests: !${p.comp} is absent, .${p.comp} is present`);
655
+ }
656
+ return [
657
+ p
658
+ ];
659
+ }
660
+ p.tag = tagFor(leaf);
661
+ p.operand = c.value ?? scalar('');
662
+ let type = typed(leaf.comp, leaf.prop);
663
+ if (type && p.op != '~' && p.value != '') {
664
+ p.operand = mapValue(p.operand, (raw)=>atom(type, raw));
665
+ p.value = flatValue(p.operand);
666
+ }
667
+ return [
668
+ p
669
+ ];
670
+ };
671
+ // A clause's path as the hops it names (@yaks/vocab `aim`). A presence test
672
+ // naming a component nothing declares is refused, as @yaks/sql refuses it.
673
+ let aimed = (c)=>{
674
+ let hops = vocab.aim(c.path.join('.'), bare(c));
675
+ let leaf = hops[hops.length - 1];
676
+ if (!leaf.prop && !vocab.comp(leaf.comp)) {
677
+ throw new Unknown(leaf.comp, unknownWord(vocab, leaf.comp));
678
+ }
679
+ return hops;
680
+ };
681
+ // `?loan` asks for a whole component beside the filter. A component this
682
+ // vocabulary does not declare is still a fair question: a store without it
683
+ // answers by leaving it off the row.
684
+ let wanted = (c)=>{
685
+ let hop = c.path.length == 1 && !c.value ? vocab.aim(c.path[0], true)[0] : undefined;
686
+ if (!hop || hop.prop) {
687
+ throw new Error(`?${c.path.join('.')}: ? asks for a whole component beside the filter, as .book&?loan does`);
688
+ }
689
+ return hop;
690
+ };
691
+ // A bare word: an FTS5 term over doc title/body. comp/prop are for show —
692
+ // matchQuery treats TEXT specially (one pred, two columns).
693
+ export let TEXT = 'text';
694
+ let text = (value)=>({
695
+ comp: 'doc',
696
+ prop: '*',
697
+ op: TEXT,
698
+ value
699
+ });
700
+ // The safe MATCH spelling shared by ranked retrieval and the membership SQL.
701
+ // User text is always a quoted phrase, never FTS operator syntax; only a
702
+ // trailing `*` has grammar meaning and prefix-matches the phrase's final token.
703
+ export let ftsQuery = (preds)=>preds.filter((p)=>p.op == TEXT).map((p)=>ftsTerm(p.value)).filter(Boolean).join(' ');
704
+ // TEXT may sit inside a reverse association's child filter. A client without
705
+ // SQLite must treat the WHOLE query as server-owned rather than locally
706
+ // approximating only the top-level terms.
707
+ export let textual = (preds)=>preds.some((p)=>p.op == TEXT || !!p.rev && textual(p.rev.preds));
708
+ // Empty reads select nothing. Whitespace, & and comma separation, quoting,
709
+ // escaping and malformed-clause refusals are all owned by the package parser.
710
+ export let NEVER = 'never';
711
+ // The OR pred: see Pred.alts. Every evaluator (matchQuery, sql.ts, predComps)
712
+ // recurses into the alternatives; every reader of directives ignores it.
713
+ export let OR = 'or';
714
+ export let never = ()=>({
715
+ comp: '',
716
+ prop: '',
717
+ op: NEVER,
718
+ value: ''
719
+ });
720
+ // Like the parser's AST, a bound query is immutable and belongs to its
721
+ // vocabulary. Repainting a board should not bind its projection again.
722
+ let bound = new WeakMap();
723
+ export let parseQuery = (q)=>{
724
+ let ast = parse(q);
725
+ let queries = bound.get(vocab);
726
+ if (!queries) bound.set(vocab, queries = new WeakMap());
727
+ let preds = queries.get(ast);
728
+ if (!preds) queries.set(ast, preds = frozen(bindClause(ast)));
729
+ return preds;
730
+ };
731
+ // The column's declared type, as the value rules read it: the tag its binder
732
+ // resolved, or for a pred built by hand, the vocabulary's.
733
+ let tagAt = (p)=>p.tag ?? tagFor(leafOf(p));
734
+ // One compile per pred, because a sweep asks the same question of every row in
735
+ // the set and the answer depends only on op, operand and tag. A time column is
736
+ // the exception: its operand is a PHRASE read against the clock, which moves
737
+ // (the subscription sweep hands the matcher a later moment on purpose).
738
+ let checks = new WeakMap();
739
+ let compiled = (p, tag)=>{
740
+ if (!checks.has(p)) {
741
+ checks.set(p, check(p.op, operandOf(p) ?? p.value, tag, 0));
742
+ }
743
+ return checks.get(p);
744
+ };
745
+ // One value against one predicate. Equality and its absent/list/range forms,
746
+ // not-equals, contains, the comparisons and the time phrases all belong to
747
+ // @yaks/match, which answers them exactly as the SQL lowering does — by the
748
+ // column's DECLARED type, never by whether both sides happen to look numeric.
749
+ // A question the type cannot answer (`.filed.priority>soon`, `.doc.title>5`)
750
+ // selects
751
+ // nothing: @yaks/sql declines the same compile, and a board mid-render is
752
+ // no place to throw.
753
+ let test = (v, p, now, tag = tagAt(p))=>{
754
+ let hit = tag == 'time' ? check(p.op, operandOf(p) ?? p.value, tag, now ?? Date.now()) : compiled(p, tag);
755
+ return !!hit && hit(v ?? null);
756
+ };
757
+ // The sugar's other half: a board-stored query carries values as typed
758
+ // ('jeff', 'T-3') — the door resolvers never saw it, so whoever EVALUATES
759
+ // resolves, against whatever graph they hold. Only reference columns with
760
+ // equality-shaped ops resolve; each part of an any-of list resolves
761
+ // alone; a miss stays as typed and matches nothing, because a board
762
+ // mid-render is no place to throw.
763
+ export let resolveRefs = (preds, lookup)=>preds.map((p)=>{
764
+ if (p.op == OR) {
765
+ return {
766
+ ...p,
767
+ alts: p.alts.map((a)=>resolveRefs(a, lookup))
768
+ };
769
+ }
770
+ // A multi-column reverse-union's value is an id like any reference — but it
771
+ // owns no comp/prop to type it, so resolve it through the entity target. A
772
+ // traversal's target is the same shape: one entity, no column to type it.
773
+ if (p.refs || p.op == REACHES) {
774
+ if (p.op != '' && p.op != REACHES || !p.value) return p;
775
+ try {
776
+ let value = String(parseProp(anyRef, p.value, {
777
+ resolve: lookup
778
+ }));
779
+ return value == p.value ? p : {
780
+ ...p,
781
+ value
782
+ };
783
+ } catch (error) {
784
+ if (error instanceof IdError) throw error;
785
+ return p;
786
+ }
787
+ }
788
+ // A reverse hop's own value is a count, never a ref — but its sub-filter
789
+ // carries the same typed values, so resolve THROUGH it (recursively).
790
+ if (p.rev) {
791
+ let inner = resolveRefs(p.rev.preds, lookup);
792
+ return inner == p.rev.preds ? p : {
793
+ ...p,
794
+ rev: {
795
+ ...p.rev,
796
+ preds: inner
797
+ }
798
+ };
799
+ }
800
+ let { comp, prop: target } = leafOf(p);
801
+ // The spine's `.entity.eid=` names entities, so its operands are ids like a
802
+ // reference's — resolved at the door the same way, so `.entity.eid=T-3` lands.
803
+ let spine = comp == 'entity' && target == 'eid';
804
+ if (!spine && !isRef(comp, target) || p.op != '' && p.op != '!') {
805
+ return p;
806
+ }
807
+ let operand = operandOf(p);
808
+ if (!operand || !literals(operand)?.length) return p;
809
+ let type = spine ? anyRef : typed(comp, target);
810
+ if (!type) return p;
811
+ let next = mapValue(operand, (part)=>{
812
+ if (!part) return part;
813
+ try {
814
+ return String(parseProp(type, part, {
815
+ resolve: lookup
816
+ }));
817
+ } catch (error) {
818
+ if (error instanceof IdError) throw error;
819
+ return part // a live saved query may name an entity not here yet
820
+ ;
821
+ }
822
+ });
823
+ let value = flatValue(next);
824
+ return value == p.value ? p : {
825
+ ...p,
826
+ value,
827
+ operand: next
828
+ };
829
+ });
830
+ // Does an entity satisfy every pred? A TEXT pred reads the doc itself —
831
+ // one pred, either column. A path pred dereferences through `ent` (the
832
+ // evaluator's graph), folding hop after hop; no ent, no ref, or no target
833
+ // anywhere along the chain reads as an absent value — so
834
+ // `.comment.target.doc.title=x` misses and `!=x` holds, same as any null
835
+ // column.
836
+ //
837
+ // `now` is the clock a time phrase reads. It defaults to the wall clock,
838
+ // which is what every door wants — a saved `today` must advance tomorrow.
839
+ // It is a parameter because a moving phrase names a window the clock moves
840
+ // THROUGH, so the only way to state "this row has aged out" as a test is to
841
+ // hand the matcher a later moment (see the subscription sweep).
842
+ // A bare component name (empty prop) is a presence test: `!`/`~=` hold when the
843
+ // bag wears the component, `=` when it does not — the same rule at depth 0 and
844
+ // at a path leaf (`.blocked` and `.filed.project.archived` mean the same thing
845
+ // one hop apart). A broken link hands in an undefined bag, which reads as absent.
846
+ let present = (bag, comp, op)=>op == '~' || op == EXISTS ? !!bag?.[comp] : !bag?.[comp];
847
+ // A Kids accessor over an in-memory universe (a byEid map). Lazily builds and
848
+ // caches the reverse map for each ref column a hop asks about, so a caller that
849
+ // holds the whole graph (the server's snapshot fallback, mcp, tests) answers a
850
+ // reverse hop the same way the live index and the SQL EXISTS do — one door, no
851
+ // per-caller reverse index. Generic over the row so a Map<eid, comps> of any
852
+ // exact shape passes without the Map-variance friction.
853
+ export let kidsOf = (byEid)=>{
854
+ let cache = new Map();
855
+ return (eid, comp, prop)=>{
856
+ let key = `${comp}.${prop}`;
857
+ let m = cache.get(key);
858
+ if (!m) {
859
+ m = new Map();
860
+ for (let [ce, cc] of byEid){
861
+ let v = cc[comp]?.[prop];
862
+ if (v == null) continue;
863
+ let k = String(v);
864
+ m.set(k, [
865
+ ...m.get(k) ?? [],
866
+ ce
867
+ ]);
868
+ }
869
+ cache.set(key, m);
870
+ }
871
+ return (m.get(eid) ?? []).map((k)=>byEid.get(k));
872
+ };
873
+ };
874
+ // An entity's own eid, read off any component bag it wears (every bag carries
875
+ // `eid`, readable's first column). A reverse hop needs it to ask "who points at
876
+ // ME"; the forward grammar never did, so matchQuery never took it as an argument.
877
+ let eidOf = (c)=>{
878
+ let e = c.entity?.eid;
879
+ if (e != null) return String(e);
880
+ for(let k in c){
881
+ let v = c[k]?.eid;
882
+ if (v != null) return String(v);
883
+ }
884
+ };
885
+ export let matchQuery = (c, preds, ent, now, kids, walk, fts)=>preds.every((p)=>{
886
+ if (p.op == NEVER) return false // the empty query: selects nothing
887
+ ;
888
+ if (p.op == OR) {
889
+ return p.alts.some((alt)=>matchQuery(c, alt, ent, now, kids, walk, fts));
890
+ }
891
+ if (p.op == ORDER || p.op == NEAR || p.op == AGG || p.op == PROJECT || p.op == WANT) {
892
+ return true;
893
+ }
894
+ // A window bounds the ANSWER, never membership: every match still matches,
895
+ // and the door that answers is what cuts the page.
896
+ if (p.op == WINDOW) return true;
897
+ if (p.op == REACHES) {
898
+ // The bounded closure, resolved ONCE per query by the caller's walk and
899
+ // tested here with a Set lookup — never a per-row traversal. No walk means
900
+ // no closure, the same reading a reverse hop gives a missing accessor.
901
+ let self = eidOf(c);
902
+ return !!self && !!walk && !!p.reach && walk(p.reach, p.value).has(self);
903
+ }
904
+ if (p.refs) {
905
+ // The multi-column reverse-union: read every {eid} column this bag
906
+ // carries. `.refs=X` holds when any equals X; `.refs` when any is set;
907
+ // `!refs` when none is — the same tri-state a reverse association marks.
908
+ let vals = vocab.refProps().map(([comp, prop])=>c[comp]?.[prop]).filter((v)=>v != null);
909
+ if (p.op == EXISTS) return vals.length > 0;
910
+ if (p.value == '') return vals.length == 0;
911
+ return vals.some((v)=>String(v) == p.value);
912
+ }
913
+ if (p.op == TEXT) {
914
+ let self = eidOf(c);
915
+ return !!self && !!fts && fts(self, p);
916
+ }
917
+ if (p.rev) {
918
+ // The reverse hop: resolve the children pointing back at me, then collapse
919
+ // the many to a yes/no — a count comparison, or ANY child matching the
920
+ // sub-filter (`not` flips that to NONE). No accessor → no children.
921
+ let self = eidOf(c);
922
+ let children = self && kids ? kids(self, p.rev.comp, p.rev.prop) : [];
923
+ // The cardinality half: the same value road, over a count — which is a
924
+ // number whatever the reference column it counts happens to be.
925
+ if (p.rev.count) return test(children.length, p, now, 'number');
926
+ let hit = children.some((k)=>!!k && matchQuery(k, p.rev.preds, ent, now, kids, walk, fts));
927
+ return p.rev.not ? !hit : hit;
928
+ }
929
+ if (!p.prop) return present(c, p.comp, p.op);
930
+ if (p.at) {
931
+ // Deref the near ref, then every intermediate hop, carrying the set of
932
+ // component-bags forward; test the leaf on each. A broken link yields one
933
+ // absent bag so absence tests still hold — the null-column reading.
934
+ let hops = [
935
+ {
936
+ comp: p.comp,
937
+ prop: p.prop
938
+ },
939
+ ...p.at.slice(0, -1)
940
+ ];
941
+ let leaf = p.at[p.at.length - 1];
942
+ let bags = [
943
+ c
944
+ ];
945
+ for (let h of hops){
946
+ bags = bags.flatMap((b)=>reads(b ?? {}, h.comp, h.prop).map((ref)=>ref != null ? ent?.(String(ref)) : undefined));
947
+ }
948
+ // A leaf with no prop is a component-presence test on the far entity —
949
+ // the depth-0 grammar one hop out, not a null column read that always fails.
950
+ return bags.some((b)=>leaf.prop ? reads(b ?? {}, leaf.comp, leaf.prop).some((value)=>test(value, p, now)) : present(b, leaf.comp, p.op));
951
+ }
952
+ return reads(c, p.comp, p.prop).some((value)=>test(value, p, now));
953
+ });
954
+ // One column read. A shared route has no component, so reads() below tests
955
+ // every component carrying that property; an entity may wear several.
956
+ //
957
+ // `updated.at` falls back to `created.at`, because the `updated` row is only
958
+ // stamped by a LATER write: an entity made and never touched since carries
959
+ // `created` and no `updated` at all. 1,656 of the graph's 10,767 entities are
960
+ // in that state, and every one of them was invisible to `.updated.at>=…` —
961
+ // including the two boards whose whole job is showing recent activity, which
962
+ // silently omitted anything filed and not revisited. Being made IS the last
963
+ // time a thing changed, so this is what the column already meant.
964
+ //
965
+ // Only `at`. Whether `.updated.by` should name the creator of an untouched
966
+ // entity is a different question about authorship, and nothing is broken by
967
+ // leaving it alone.
968
+ let read = (c, comp, prop)=>{
969
+ // The one DERIVED column (D-24102): task.status is computed from the
970
+ // completed/cancelled/claim comps, never stored — the same value statusOf
971
+ // gives every renderer, so a filter, projection or tally reads it identically.
972
+ if (comp == 'task' && prop == 'status') {
973
+ return c.task ? statusOf(c) : undefined;
974
+ }
975
+ let v = c[comp]?.[prop];
976
+ return v == null && comp == 'updated' && prop == 'at' ? c.created?.at : v;
977
+ };
978
+ let reads = (c, comp, prop)=>{
979
+ if (comp) return [
980
+ read(c, comp, prop)
981
+ ];
982
+ let values = Object.values(c).map((v)=>v?.[prop]).filter((v)=>v != null);
983
+ return values.length ? values : [
984
+ undefined
985
+ ];
986
+ };
987
+ //# sourceMappingURL=query.js.map