@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/hosting.ts ADDED
@@ -0,0 +1,72 @@
1
+ // The page host declares its mount and transport. The box needs no declaration;
2
+ // an app's web door names its own address and its store's existing API wire.
3
+ // `origin` is the address handed to a person; a browser page without one
4
+ // hands out its own location.
5
+ export type Hosting = {
6
+ home?: { title: string; query: string }
7
+ origin?: string
8
+ page: string
9
+ api: string
10
+ apply: string
11
+ owner: string
12
+ storage?: string
13
+ }
14
+ let declared = () => (globalThis as { YAK_WEB?: Hosting }).YAK_WEB
15
+ export let box: Hosting = {
16
+ origin: 'https://tasks.yak.sh',
17
+ page: '',
18
+ api: '',
19
+ apply: '/web/apply',
20
+ owner: '/web/owner',
21
+ }
22
+ export let hosting = (): Hosting => declared() ?? box
23
+ export let pagePath = (path: string): string => hosting().page + path
24
+ export let localPath = (path: string): string => {
25
+ let mount = hosting().page
26
+ return mount && (path == mount || path.startsWith(mount + '/'))
27
+ ? path.slice(mount.length) || '/'
28
+ : path
29
+ }
30
+ export let apiPath = (path: string): string => hosting().api + path
31
+ export let webOrigin = (): string =>
32
+ hosting().origin ?? globalThis.location?.origin ?? box.origin!
33
+ /** Host-only operations are never inferred from app data words. */
34
+ export let door = (_name: 'freeze'): boolean => false
35
+
36
+ // New app mounts have no legacy browser state. Box keys stay unchanged; an
37
+ // app/account never replays another store's cached rows, drafts or writes.
38
+ export let storageKey = (key: string): string =>
39
+ hosting().storage ? `${key}:${hosting().storage}` : key
40
+ export let scopedStorage = (store: Storage): Storage => {
41
+ let scope = hosting().storage
42
+ if (!scope) return store
43
+ let prefix = `yak-web:${scope}:`
44
+ let keys = () =>
45
+ Array.from({ length: store.length }, (_, i) => store.key(i)!)
46
+ .filter((key) => key.startsWith(prefix))
47
+ return {
48
+ get length() {
49
+ return keys().length
50
+ },
51
+ key: (i) => keys()[i]?.slice(prefix.length) ?? null,
52
+ getItem: (key) => store.getItem(prefix + key),
53
+ setItem: (key, value) => store.setItem(prefix + key, value),
54
+ removeItem: (key) => store.removeItem(prefix + key),
55
+ clear: () => keys().forEach((key) => store.removeItem(key)),
56
+ }
57
+ }
58
+
59
+ // Only old account-named hosted queues and the single-owner box have enough
60
+ // provenance to migrate. Leave an unscoped hosted queue untouched for recovery.
61
+ export let legacyOutboxName = (): string | undefined =>
62
+ hosting().storage
63
+ ? storageKey('tasks-outbox')
64
+ : declared()
65
+ ? undefined
66
+ : 'tasks-outbox'
67
+
68
+ // The queue a page wrote under @yaks/sync's default scope, the API and no
69
+ // person, until T-133002. Only the single-owner box knows whose those writes
70
+ // are; a hosted mount's stay where they are, unsent and kept.
71
+ export let formerOutboxScope = (api: string): string | undefined =>
72
+ declared() ? undefined : JSON.stringify([api, {}])
package/idb.js ADDED
@@ -0,0 +1,14 @@
1
+ import { hosting } from "./hosting.js";
2
+ // The graph floor and durable writes now belong to @yaks/client. Only the
3
+ // obsolete whole-graph database cleanup remains here.
4
+ let LEGACY = 'tasks';
5
+ // Drop the dead `tasks` database. Fire-and-forget: a delete waits for every
6
+ // tab still holding it open, and nothing here depends on when it lands.
7
+ export let forgetLegacy = ()=>{
8
+ if (hosting().storage) return;
9
+ try {
10
+ ;
11
+ globalThis.indexedDB?.deleteDatabase(LEGACY);
12
+ } catch {}
13
+ };
14
+ //# sourceMappingURL=idb.js.map
package/idb.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"idb.js","sources":["./idb.ts"],"names":[],"mappings":"AAAA,SAAS,OAAO,uBAAsB;AACtC,0EAA0E;AAC1E,sDAAsD;AACtD,IAAI,SAAS;AAEb,4EAA4E;AAC5E,wEAAwE;AACxE,OAAO,IAAI,eAAe;EACxB,IAAI,UAAU,OAAO,EAAE;EACvB,IAAI;;IACA,WAA0C,SAAS,EAAE,eACrD;EAEJ,EAAE,OAAM,CAAsC;AAChD,EAAC"}
package/idb.ts ADDED
@@ -0,0 +1,15 @@
1
+ import { hosting } from './hosting.js'
2
+ // The graph floor and durable writes now belong to @yaks/client. Only the
3
+ // obsolete whole-graph database cleanup remains here.
4
+ let LEGACY = 'tasks'
5
+
6
+ // Drop the dead `tasks` database. Fire-and-forget: a delete waits for every
7
+ // tab still holding it open, and nothing here depends on when it lands.
8
+ export let forgetLegacy = () => {
9
+ if (hosting().storage) return
10
+ try {
11
+ ;(globalThis as { indexedDB?: IDBFactory }).indexedDB?.deleteDatabase(
12
+ LEGACY,
13
+ )
14
+ } catch { /* no storage, nothing to forget */ }
15
+ }
package/index.js ADDED
@@ -0,0 +1,232 @@
1
+ // The cache's derived indexes — the reverse views the vocabulary implies, so
2
+ // a query resolves to a candidate set instead of a whole-graph scan. `comps`/
3
+ // `stamped` (types.ts) declare every `{eid}` reference; from that ONE list we
4
+ // build refs[comp.prop] = referenced → referrers, plus by-component-presence
5
+ // and edges-by-endpoint. Adding a component or an `{eid}` field gets indexed
6
+ // cache queries for free — the same list already drives the db schema and the
7
+ // dot-param router (CLAUDE.md, "the vocabulary is one list").
8
+ //
9
+ // This is the IN-MEMORY realization of that derivation. The DERIVATION
10
+ // (@yaks/vocab's `refProps`) is the reusable, backend-agnostic piece — it is what a SQLite
11
+ // `create index` / IndexedDB store set is generated from too (T-17046/T-12764).
12
+ // live.ts drives maintenance from the eids each patch touches, and `anchor()`
13
+ // turns a query's `Pred[]` into the smallest candidate set. When the store
14
+ // moves to IDB, `anchor` swaps its lookup; the derivation and callers stay put.
15
+ import { indexes, vocab } from "./types.js";
16
+ import { isRef } from "./props.js";
17
+ import { EXISTS, operandOf, ORDER, TEXT, WANT } from "./query.js";
18
+ import { literals } from "@yaks/sql";
19
+ // Every index a component declares — the ONE index vocabulary the cache, the
20
+ // SQL DDL (T-12764) and the IDB stores (T-17125) all read, so no backend
21
+ // hand-keeps its own set. Two sources merge: the composite/unique declarations
22
+ // in `indexes` (types.ts), and one single-column index per `{eid}` reference,
23
+ // auto-derived from the vocabulary's reference properties (never hand-listed). A single-column declaration
24
+ // OVERRIDES its auto twin — that is how a lone ref column earns the
25
+ // `unique`/`where` the plain derivation can't give it. The cache below realizes
26
+ // only the single-column half (its `refs` map maintains exactly those); the
27
+ // composites are for the durable backends. Nothing generates yet — this is the
28
+ // source those generators will read.
29
+ export let indexesFor = (comp)=>{
30
+ let declared = indexes[comp] ?? [];
31
+ let overridden = new Set(declared.filter((i)=>i.cols.length == 1).map((i)=>i.cols[0]));
32
+ let auto = vocab.refProps().filter(([c, p])=>c == comp && !overridden.has(p)).map(([, p])=>({
33
+ cols: [
34
+ p
35
+ ]
36
+ }));
37
+ return [
38
+ ...declared,
39
+ ...auto
40
+ ];
41
+ };
42
+ let refKey = (comp, prop)=>`${comp}.${prop}`;
43
+ // Vocabulary is immutable; learning another host replaces it. Group its
44
+ // references once so a row update examines only the components that row has.
45
+ let refGroups = new WeakMap();
46
+ let refsByComp = ()=>{
47
+ let found = refGroups.get(vocab);
48
+ if (found) return found;
49
+ found = new Map();
50
+ for (let [comp, prop] of vocab.refProps()){
51
+ let props = found.get(comp);
52
+ if (props) props.push(prop);
53
+ else found.set(comp, [
54
+ prop
55
+ ]);
56
+ }
57
+ refGroups.set(vocab, found);
58
+ return found;
59
+ };
60
+ export let emptyIndex = ()=>({
61
+ refs: new Map(),
62
+ byComp: new Map(),
63
+ byParent: new Map(),
64
+ byChild: new Map()
65
+ });
66
+ let add = (m, k, eid)=>{
67
+ let s = m.get(k);
68
+ if (s) s.add(eid);
69
+ else m.set(k, new Set([
70
+ eid
71
+ ]));
72
+ };
73
+ let drop = (m, k, eid)=>{
74
+ let s = m.get(k);
75
+ if (!s) return;
76
+ s.delete(eid);
77
+ if (!s.size) m.delete(k);
78
+ };
79
+ let refVal = (row, comp, prop)=>{
80
+ let v = row?.[comp]?.[prop];
81
+ return v == null ? undefined : String(v);
82
+ };
83
+ // Bring one eid's derived facets from `before` to `after` — the pair applyLocal
84
+ // already holds. Only a changed facet touches a set, so an unrelated column
85
+ // edit is O(1). A death is `after = undefined`; a birth `before = undefined`.
86
+ export let reindex = (ix, eid, before, after)=>{
87
+ let refs = refsByComp();
88
+ for (let c of new Set([
89
+ ...Object.keys(before ?? {}),
90
+ ...Object.keys(after ?? {})
91
+ ])){
92
+ if (before?.[c] !== after?.[c]) {
93
+ for (let p of refs.get(c) ?? []){
94
+ let was = refVal(before, c, p);
95
+ let now = refVal(after, c, p);
96
+ if (was == now) continue;
97
+ let key = refKey(c, p);
98
+ let m = ix.refs.get(key);
99
+ if (!m) ix.refs.set(key, m = new Map());
100
+ if (was != null) drop(m, was, eid);
101
+ if (now != null) add(m, now, eid);
102
+ if (!m.size) ix.refs.delete(key);
103
+ }
104
+ }
105
+ let had = !!before?.[c];
106
+ let has = !!after?.[c];
107
+ if (had == has) continue;
108
+ if (has) add(ix.byComp, c, eid);
109
+ else drop(ix.byComp, c, eid);
110
+ }
111
+ };
112
+ // An edge's endpoint index. A new array replaces the old on any change, so the
113
+ // narrow relation signals (live.ts publish()) see a fresh reference and fire.
114
+ export let reindexEdge = (ix, d, gone)=>{
115
+ let put = (m, k, same)=>{
116
+ let cur = m.get(k);
117
+ if (gone) {
118
+ if (!cur) return;
119
+ let n = cur.filter((x)=>!same(x));
120
+ if (n.length == cur.length) return;
121
+ n.length ? m.set(k, n) : m.delete(k);
122
+ } else if (!cur) m.set(k, [
123
+ d
124
+ ]);
125
+ else if (!cur.some(same)) m.set(k, [
126
+ ...cur,
127
+ d
128
+ ]);
129
+ };
130
+ put(ix.byParent, d.parent, (x)=>x.type == d.type && x.child == d.child);
131
+ put(ix.byChild, d.child, (x)=>x.type == d.type && x.parent == d.parent);
132
+ };
133
+ // A full build — the seed/reset path and the lazy rebuild after a wholesale
134
+ // cache replacement (tests, host integrations). Incremental maintenance is the
135
+ // steady state; this is the one O(graph) pass, never per patch.
136
+ export let indexAll = (ix, graph, deps)=>{
137
+ ix.refs.clear();
138
+ ix.byComp.clear();
139
+ ix.byParent.clear();
140
+ ix.byChild.clear();
141
+ for (let [eid, row] of Object.entries(graph))reindex(ix, eid, undefined, row);
142
+ for (let d of deps)reindexEdge(ix, d, false);
143
+ };
144
+ // Does satisfying this pred REQUIRE its component be present? Then byComp is a
145
+ // valid candidate superset. Absence tests (`= ''`, `!=`, a component `=`) match
146
+ // rows WITHOUT the component, so they anchor nothing. Exported so the durable
147
+ // backends (the IDB resolver, T-17125) make the SAME presence decision this
148
+ // in-memory anchor does — one anchoring predicate, every store.
149
+ export let implies = (p)=>{
150
+ if (!p.prop) return p.op == EXISTS || p.op == '~' // .comp / .comp~ present
151
+ ;
152
+ switch(p.op){
153
+ case '!':
154
+ return false // `!=` matches an absent column too
155
+ ;
156
+ case '':
157
+ return p.value != '' // `= ''` is absence; `= v` needs presence
158
+ ;
159
+ case '~':
160
+ return p.value != '' // `~ ''` matches everything
161
+ ;
162
+ default:
163
+ return true // EXISTS / < <= > >= all need a value present
164
+ ;
165
+ }
166
+ };
167
+ // The smallest candidate eid set for these preds, or undefined for a whole-cache
168
+ // scan. An eid-ref EQUALITY anchors on the reverse index (O(result)); any pred
169
+ // requiring a component present anchors on byComp. Path preds (`p.at`) anchor on
170
+ // their NEAR component's presence, never mistaking the near ref for a value.
171
+ // The returned set is the index's own — callers read it, never mutate it.
172
+ //
173
+ // A set nobody holds is deleted from the index (`drop`), so a missing one means
174
+ // NO candidates, not no anchor: a component no row carries, or a ref value
175
+ // nothing points at, anchors on the empty set. Reading it as "no anchor" made a
176
+ // query for an absent component scan the whole cache on every resolve.
177
+ let NONE = new Set();
178
+ export let anchor = (ix, preds)=>{
179
+ let best;
180
+ let consider = (s)=>{
181
+ if (s && (!best || s.size < best.size)) best = s;
182
+ };
183
+ for (let p of preds){
184
+ if (p.op == TEXT || p.op == ORDER || p.op == WANT) continue;
185
+ if (p.rev) {
186
+ // A reverse hop needing ≥1 child anchors on the parents that HAVE one —
187
+ // the KEYS of the reverse map (a superset of the matching-child parents).
188
+ // A negated hop (NONE) or a count that admits zero also matches childless
189
+ // parents — not in the keys — so it anchors nothing, a full scan.
190
+ if (!p.rev.not && !p.rev.count) {
191
+ let m = ix.refs.get(refKey(p.rev.comp, p.rev.prop));
192
+ consider(new Set(m?.keys() ?? []));
193
+ }
194
+ continue;
195
+ }
196
+ if (p.refs) {
197
+ // The multi-column reverse-union: the referrers of `value` are the union
198
+ // of every reverse map's set for it — O(referrers), never a graph scan.
199
+ // Presence/absence (`.refs`, `!refs`) admit rows with no reference at
200
+ // all, not in any reverse map, so they anchor nothing.
201
+ if (p.op == '' && p.value) {
202
+ let out = new Set();
203
+ for (let [c, pr] of vocab.refProps()){
204
+ for (let e of ix.refs.get(refKey(c, pr))?.get(p.value) ?? []){
205
+ out.add(e);
206
+ }
207
+ }
208
+ consider(out);
209
+ }
210
+ continue;
211
+ }
212
+ if (!p.at && p.comp && p.prop && p.op == '' && isRef(p.comp, p.prop)) {
213
+ let values = literals(operandOf(p) ?? null);
214
+ if (values) {
215
+ let refs = ix.refs.get(refKey(p.comp, p.prop));
216
+ consider(new Set(values.flatMap((v)=>[
217
+ ...refs?.get(v) ?? []
218
+ ])));
219
+ continue;
220
+ }
221
+ }
222
+ if (p.comp && implies(p)) consider(ix.byComp.get(p.comp) ?? NONE);
223
+ }
224
+ return best;
225
+ };
226
+ // The children a reverse hop resolves: the eids referring at `eid` through
227
+ // `comp.prop`. The reverse map IS this lookup — the whole reason index.ts keeps
228
+ // it — so live's Kids accessor is one read over it (live.ts binds the cache).
229
+ export let children = (ix, eid, comp, prop)=>[
230
+ ...ix.refs.get(refKey(comp, prop))?.get(eid) ?? []
231
+ ];
232
+ //# sourceMappingURL=index.js.map
package/index.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["./index.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,8EAA8E;AAC9E,8EAA8E;AAC9E,6EAA6E;AAC7E,6EAA6E;AAC7E,8EAA8E;AAC9E,8DAA8D;AAC9D,EAAE;AACF,uEAAuE;AACvE,2FAA2F;AAC3F,gFAAgF;AAChF,8EAA8E;AAC9E,2EAA2E;AAC3E,gFAAgF;AAChF,SAA6B,OAAO,EAAE,KAAK,qBAAoB;AAC/D,SAAS,KAAK,qBAAoB;AAClC,SAAS,MAAM,EAAE,SAAS,EAAE,KAAK,EAAa,IAAI,EAAE,IAAI,qBAAoB;AAC5E,SAAS,QAAQ,yBAA8B;AAO/C,6EAA6E;AAC7E,yEAAyE;AACzE,+EAA+E;AAC/E,8EAA8E;AAC9E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I,GAAG,UAAU,EAAE;MAClC;MACA;IACF;IACA,IAAI,EAAE,IAAI,EAAE;MACV,yEAAyE;MACzE,wEAAwE;MACxE,sEAAsE;MACtE,uDAAuD;MACvD,IAAI,EAAE,EAAE,IAAI,MAAM,EAAE,KAAK,EAAE;QACzB,IAAI,MAAM,IAAI;QACd,KAAK,IAAI,CAAC,GAAG,GAAG,IAAI,MAAM,QAAQ,GAAI;UACpC,KAAK,IAAI,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,GAAG,MAAM,IAAI,EAAE,KAAK,KAAK,EAAE,CAAE;YAC5D,IAAI,GAAG,CAAC;UACV;QACF;QACA,SAAS;MACX;MACA;IACF;IACA,IACE,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,IAAI,EAAE,IAAI,IAAI,EAAE,EAAE,IAAI,MAAM,MAAM,EAAE,IAAI,EAAE,EAAE,IAAI,GAC/D;MACA,IAAI,SAAS,SAAS,UAAU,MAAM;MACtC,IAAI,QAAQ;QACV,IAAI,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE,IAAI;QAC5C,SAAS,IAAI,IAAI,OAAO,OAAO,CAAC,CAAC,IAAM;eAAI,MAAM,IAAI,MAAM,EAAE;WAAC;QAC9D;MACF;IACF;IACA,IAAI,EAAE,IAAI,IAAI,QAAQ,IAAI,SAAS,GAAG,MAAM,CAAC,GAAG,CAAC,EAAE,IAAI,KAAK;EAC9D;EACA,OAAO;AACT,EAAC;AAED,2EAA2E;AAC3E,gFAAgF;AAChF,8EAA8E;AAC9E,OAAO,IAAI,WAAW,CACpB,IACA,KACA,MACA,OACa;OAAI,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,MAAM,QAAQ,IAAI,QAAQ,EAAE;GAAC,CAAA"}
package/index.ts ADDED
@@ -0,0 +1,252 @@
1
+ // The cache's derived indexes — the reverse views the vocabulary implies, so
2
+ // a query resolves to a candidate set instead of a whole-graph scan. `comps`/
3
+ // `stamped` (types.ts) declare every `{eid}` reference; from that ONE list we
4
+ // build refs[comp.prop] = referenced → referrers, plus by-component-presence
5
+ // and edges-by-endpoint. Adding a component or an `{eid}` field gets indexed
6
+ // cache queries for free — the same list already drives the db schema and the
7
+ // dot-param router (CLAUDE.md, "the vocabulary is one list").
8
+ //
9
+ // This is the IN-MEMORY realization of that derivation. The DERIVATION
10
+ // (@yaks/vocab's `refProps`) is the reusable, backend-agnostic piece — it is what a SQLite
11
+ // `create index` / IndexedDB store set is generated from too (T-17046/T-12764).
12
+ // live.ts drives maintenance from the eids each patch touches, and `anchor()`
13
+ // turns a query's `Pred[]` into the smallest candidate set. When the store
14
+ // moves to IDB, `anchor` swaps its lookup; the derivation and callers stay put.
15
+ import { type Dep, type Idx, indexes, vocab } from './types.js'
16
+ import { isRef } from './props.js'
17
+ import { EXISTS, operandOf, ORDER, type Pred, TEXT, WANT } from './query.js'
18
+ import { literals } from '@yaks/sql'
19
+
20
+ // A row as the index reads it — the merged-components shape both the live cache
21
+ // and a client Row speak. Kept structural so index.ts carries no cycle back to
22
+ // live.ts's `Comps`.
23
+ type Row = Record<string, Record<string, unknown> | undefined>
24
+
25
+ // Every index a component declares — the ONE index vocabulary the cache, the
26
+ // SQL DDL (T-12764) and the IDB stores (T-17125) all read, so no backend
27
+ // hand-keeps its own set. Two sources merge: the composite/unique declarations
28
+ // in `indexes` (types.ts), and one single-column index per `{eid}` reference,
29
+ // auto-derived from the vocabulary's reference properties (never hand-listed). A single-column declaration
30
+ // OVERRIDES its auto twin — that is how a lone ref column earns the
31
+ // `unique`/`where` the plain derivation can't give it. The cache below realizes
32
+ // only the single-column half (its `refs` map maintains exactly those); the
33
+ // composites are for the durable backends. Nothing generates yet — this is the
34
+ // source those generators will read.
35
+ export let indexesFor = (comp: string): Idx[] => {
36
+ let declared = indexes[comp] ?? []
37
+ let overridden = new Set(
38
+ declared.filter((i) => i.cols.length == 1).map((i) => i.cols[0]),
39
+ )
40
+ let auto = vocab.refProps()
41
+ .filter(([c, p]) => c == comp && !overridden.has(p))
42
+ .map(([, p]): Idx => ({ cols: [p] }))
43
+ return [...declared, ...auto]
44
+ }
45
+
46
+ let refKey = (comp: string, prop: string) => `${comp}.${prop}`
47
+
48
+ // Vocabulary is immutable; learning another host replaces it. Group its
49
+ // references once so a row update examines only the components that row has.
50
+ let refGroups = new WeakMap<typeof vocab, Map<string, string[]>>()
51
+ let refsByComp = () => {
52
+ let found = refGroups.get(vocab)
53
+ if (found) return found
54
+ found = new Map()
55
+ for (let [comp, prop] of vocab.refProps()) {
56
+ let props = found.get(comp)
57
+ if (props) props.push(prop)
58
+ else found.set(comp, [prop])
59
+ }
60
+ refGroups.set(vocab, found)
61
+ return found
62
+ }
63
+
64
+ export type Index = {
65
+ // "comp.prop" → referenced eid → the eids referring through that column
66
+ refs: Map<string, Map<string, Set<string>>>
67
+ // component name → the eids carrying it (kind resolves through here via
68
+ // kindPreds — no stored by-kind map to drift from kindOf)
69
+ byComp: Map<string, Set<string>>
70
+ // the edge triples by endpoint (replaces relationIndex/childIndex)
71
+ byParent: Map<string, Dep[]>
72
+ byChild: Map<string, Dep[]>
73
+ }
74
+
75
+ export let emptyIndex = (): Index => ({
76
+ refs: new Map(),
77
+ byComp: new Map(),
78
+ byParent: new Map(),
79
+ byChild: new Map(),
80
+ })
81
+
82
+ let add = (m: Map<string, Set<string>>, k: string, eid: string) => {
83
+ let s = m.get(k)
84
+ if (s) s.add(eid)
85
+ else m.set(k, new Set([eid]))
86
+ }
87
+ let drop = (m: Map<string, Set<string>>, k: string, eid: string) => {
88
+ let s = m.get(k)
89
+ if (!s) return
90
+ s.delete(eid)
91
+ if (!s.size) m.delete(k)
92
+ }
93
+
94
+ let refVal = (row: Row | undefined, comp: string, prop: string) => {
95
+ let v = row?.[comp]?.[prop]
96
+ return v == null ? undefined : String(v)
97
+ }
98
+
99
+ // Bring one eid's derived facets from `before` to `after` — the pair applyLocal
100
+ // already holds. Only a changed facet touches a set, so an unrelated column
101
+ // edit is O(1). A death is `after = undefined`; a birth `before = undefined`.
102
+ export let reindex = (
103
+ ix: Index,
104
+ eid: string,
105
+ before: Row | undefined,
106
+ after: Row | undefined,
107
+ ) => {
108
+ let refs = refsByComp()
109
+ for (
110
+ let c of new Set([
111
+ ...Object.keys(before ?? {}),
112
+ ...Object.keys(after ?? {}),
113
+ ])
114
+ ) {
115
+ if (before?.[c] !== after?.[c]) {
116
+ for (let p of refs.get(c) ?? []) {
117
+ let was = refVal(before, c, p)
118
+ let now = refVal(after, c, p)
119
+ if (was == now) continue
120
+ let key = refKey(c, p)
121
+ let m = ix.refs.get(key)
122
+ if (!m) ix.refs.set(key, m = new Map())
123
+ if (was != null) drop(m, was, eid)
124
+ if (now != null) add(m, now, eid)
125
+ if (!m.size) ix.refs.delete(key)
126
+ }
127
+ }
128
+ let had = !!before?.[c]
129
+ let has = !!after?.[c]
130
+ if (had == has) continue
131
+ if (has) add(ix.byComp, c, eid)
132
+ else drop(ix.byComp, c, eid)
133
+ }
134
+ }
135
+
136
+ // An edge's endpoint index. A new array replaces the old on any change, so the
137
+ // narrow relation signals (live.ts publish()) see a fresh reference and fire.
138
+ export let reindexEdge = (ix: Index, d: Dep, gone: boolean) => {
139
+ let put = (m: Map<string, Dep[]>, k: string, same: (x: Dep) => boolean) => {
140
+ let cur = m.get(k)
141
+ if (gone) {
142
+ if (!cur) return
143
+ let n = cur.filter((x) => !same(x))
144
+ if (n.length == cur.length) return
145
+ n.length ? m.set(k, n) : m.delete(k)
146
+ } else if (!cur) m.set(k, [d])
147
+ else if (!cur.some(same)) m.set(k, [...cur, d])
148
+ }
149
+ put(ix.byParent, d.parent, (x) => x.type == d.type && x.child == d.child)
150
+ put(ix.byChild, d.child, (x) => x.type == d.type && x.parent == d.parent)
151
+ }
152
+
153
+ // A full build — the seed/reset path and the lazy rebuild after a wholesale
154
+ // cache replacement (tests, host integrations). Incremental maintenance is the
155
+ // steady state; this is the one O(graph) pass, never per patch.
156
+ export let indexAll = (ix: Index, graph: Record<string, Row>, deps: Dep[]) => {
157
+ ix.refs.clear()
158
+ ix.byComp.clear()
159
+ ix.byParent.clear()
160
+ ix.byChild.clear()
161
+ for (let [eid, row] of Object.entries(graph)) reindex(ix, eid, undefined, row)
162
+ for (let d of deps) reindexEdge(ix, d, false)
163
+ }
164
+
165
+ // Does satisfying this pred REQUIRE its component be present? Then byComp is a
166
+ // valid candidate superset. Absence tests (`= ''`, `!=`, a component `=`) match
167
+ // rows WITHOUT the component, so they anchor nothing. Exported so the durable
168
+ // backends (the IDB resolver, T-17125) make the SAME presence decision this
169
+ // in-memory anchor does — one anchoring predicate, every store.
170
+ export let implies = (p: Pred): boolean => {
171
+ if (!p.prop) return p.op == EXISTS || p.op == '~' // .comp / .comp~ present
172
+ switch (p.op) {
173
+ case '!':
174
+ return false // `!=` matches an absent column too
175
+ case '':
176
+ return p.value != '' // `= ''` is absence; `= v` needs presence
177
+ case '~':
178
+ return p.value != '' // `~ ''` matches everything
179
+ default:
180
+ return true // EXISTS / < <= > >= all need a value present
181
+ }
182
+ }
183
+
184
+ // The smallest candidate eid set for these preds, or undefined for a whole-cache
185
+ // scan. An eid-ref EQUALITY anchors on the reverse index (O(result)); any pred
186
+ // requiring a component present anchors on byComp. Path preds (`p.at`) anchor on
187
+ // their NEAR component's presence, never mistaking the near ref for a value.
188
+ // The returned set is the index's own — callers read it, never mutate it.
189
+ //
190
+ // A set nobody holds is deleted from the index (`drop`), so a missing one means
191
+ // NO candidates, not no anchor: a component no row carries, or a ref value
192
+ // nothing points at, anchors on the empty set. Reading it as "no anchor" made a
193
+ // query for an absent component scan the whole cache on every resolve.
194
+ let NONE: Set<string> = new Set()
195
+ export let anchor = (ix: Index, preds: Pred[]): Set<string> | undefined => {
196
+ let best: Set<string> | undefined
197
+ let consider = (s: Set<string> | undefined) => {
198
+ if (s && (!best || s.size < best.size)) best = s
199
+ }
200
+ for (let p of preds) {
201
+ if (p.op == TEXT || p.op == ORDER || p.op == WANT) continue
202
+ if (p.rev) {
203
+ // A reverse hop needing ≥1 child anchors on the parents that HAVE one —
204
+ // the KEYS of the reverse map (a superset of the matching-child parents).
205
+ // A negated hop (NONE) or a count that admits zero also matches childless
206
+ // parents — not in the keys — so it anchors nothing, a full scan.
207
+ if (!p.rev.not && !p.rev.count) {
208
+ let m = ix.refs.get(refKey(p.rev.comp, p.rev.prop))
209
+ consider(new Set(m?.keys() ?? []))
210
+ }
211
+ continue
212
+ }
213
+ if (p.refs) {
214
+ // The multi-column reverse-union: the referrers of `value` are the union
215
+ // of every reverse map's set for it — O(referrers), never a graph scan.
216
+ // Presence/absence (`.refs`, `!refs`) admit rows with no reference at
217
+ // all, not in any reverse map, so they anchor nothing.
218
+ if (p.op == '' && p.value) {
219
+ let out = new Set<string>()
220
+ for (let [c, pr] of vocab.refProps()) {
221
+ for (let e of ix.refs.get(refKey(c, pr))?.get(p.value) ?? []) {
222
+ out.add(e)
223
+ }
224
+ }
225
+ consider(out)
226
+ }
227
+ continue
228
+ }
229
+ if (
230
+ !p.at && p.comp && p.prop && p.op == '' && isRef(p.comp, p.prop)
231
+ ) {
232
+ let values = literals(operandOf(p) ?? null)
233
+ if (values) {
234
+ let refs = ix.refs.get(refKey(p.comp, p.prop))
235
+ consider(new Set(values.flatMap((v) => [...refs?.get(v) ?? []])))
236
+ continue
237
+ }
238
+ }
239
+ if (p.comp && implies(p)) consider(ix.byComp.get(p.comp) ?? NONE)
240
+ }
241
+ return best
242
+ }
243
+
244
+ // The children a reverse hop resolves: the eids referring at `eid` through
245
+ // `comp.prop`. The reverse map IS this lookup — the whole reason index.ts keeps
246
+ // it — so live's Kids accessor is one read over it (live.ts binds the cache).
247
+ export let children = (
248
+ ix: Index,
249
+ eid: string,
250
+ comp: string,
251
+ prop: string,
252
+ ): string[] => [...ix.refs.get(refKey(comp, prop))?.get(eid) ?? []]