@indigoai-us/hq-cli 5.345.246 → 5.345.248

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 (256) hide show
  1. package/CHANGELOG.md +63 -0
  2. package/assets/lanes-hooks/hq-lanes-drain.sh +1 -1
  3. package/contracts/host-canary-release.json +14 -0
  4. package/dist/command-catalog.generated.d.ts +1116 -3
  5. package/dist/command-catalog.generated.js +1373 -3
  6. package/dist/command-registration-plan.d.ts +6 -0
  7. package/dist/command-registration-plan.js +1 -0
  8. package/dist/commands/install-global.d.ts +15 -0
  9. package/dist/commands/install-global.js +98 -4
  10. package/dist/commands/lanes-hold.js +7 -7
  11. package/dist/commands/lanes-resume.js +2 -2
  12. package/dist/commands/lanes-review.js +1 -1
  13. package/dist/commands/lanes-session-start.d.ts +1 -1
  14. package/dist/commands/lanes-session-start.js +3 -3
  15. package/dist/commands/lanes-shared.d.ts +1 -1
  16. package/dist/commands/lanes.d.ts +2 -2
  17. package/dist/commands/lanes.js +50 -50
  18. package/dist/commands/mcp-registration.d.ts +17 -0
  19. package/dist/commands/mcp-registration.js +119 -0
  20. package/dist/commands/mesh.d.ts +25 -0
  21. package/dist/commands/mesh.js +48 -8
  22. package/dist/commands/ontology-graph.d.ts +62 -0
  23. package/dist/commands/ontology-graph.js +439 -0
  24. package/dist/commands/pkg-install.js +1 -1
  25. package/dist/commands/reindex.js +1 -1
  26. package/dist/index.js +1 -1
  27. package/dist/lanes-monitor-check-argv.d.ts +1 -1
  28. package/dist/lanes-monitor-check-argv.js +1 -1
  29. package/dist/lanes-monitor-check-fast.d.ts +1 -1
  30. package/dist/lanes-monitor-check-fast.js +1 -1
  31. package/dist/lanes-session-start-argv.d.ts +1 -1
  32. package/dist/lanes-session-start-argv.js +1 -1
  33. package/dist/lib/capacity/sign-in.d.ts +1 -1
  34. package/dist/lib/capacity/sign-in.js +1 -1
  35. package/dist/lib/daemon/hook-requests-conduct-link.js +2 -2
  36. package/dist/lib/daemon/lanes-monitor-state.d.ts +2 -2
  37. package/dist/lib/daemon/lanes-monitor-state.js +2 -2
  38. package/dist/lib/daemon/merge-sections.d.ts +3 -3
  39. package/dist/lib/daemon/merge-sections.js +7 -7
  40. package/dist/lib/daemon/remote-control-pools.js +1 -1
  41. package/dist/lib/daemon/run.js +1 -1
  42. package/dist/lib/daemon/state-file.d.ts +1 -1
  43. package/dist/lib/daemon/state-file.js +1 -1
  44. package/dist/lib/doctor/checks/antigravity-wiring.d.ts +21 -0
  45. package/dist/lib/doctor/checks/antigravity-wiring.js +75 -0
  46. package/dist/lib/doctor/checks/anywhere.js +30 -0
  47. package/dist/lib/doctor/checks/runtime-probe.js +13 -0
  48. package/dist/lib/doctor/platform.d.ts +1 -1
  49. package/dist/lib/doctor/platform.js +5 -0
  50. package/dist/lib/doctor/registry.js +3 -0
  51. package/dist/lib/flag-snapshot-file.d.ts +2 -0
  52. package/dist/lib/flag-snapshot-file.js +118 -48
  53. package/dist/lib/hook-engine/canary-gate.d.ts +57 -3
  54. package/dist/lib/hook-engine/canary-gate.js +138 -6
  55. package/dist/lib/hook-engine/coverage.d.ts +91 -0
  56. package/dist/lib/hook-engine/coverage.js +184 -0
  57. package/dist/lib/hot-channels/graph-context.d.ts +105 -0
  58. package/dist/lib/hot-channels/graph-context.js +377 -0
  59. package/dist/lib/hot-channels/signals-pass.js +27 -1
  60. package/dist/lib/install/antigravity-targets.d.ts +39 -0
  61. package/dist/lib/install/antigravity-targets.js +122 -0
  62. package/dist/lib/lanes/claude-credential.d.ts +1 -1
  63. package/dist/lib/lanes/claude-credential.js +1 -1
  64. package/dist/lib/lanes/delivery-core.d.ts +1 -1
  65. package/dist/lib/lanes/delivery-core.js +1 -1
  66. package/dist/lib/lanes/delivery.d.ts +10 -10
  67. package/dist/lib/lanes/delivery.js +8 -8
  68. package/dist/lib/lanes/dropbox.d.ts +1 -1
  69. package/dist/lib/lanes/dropbox.js +1 -1
  70. package/dist/lib/lanes/hooks-install.js +2 -2
  71. package/dist/lib/lanes/index.d.ts +1 -1
  72. package/dist/lib/lanes/index.js +1 -1
  73. package/dist/lib/lanes/monitor.js +1 -1
  74. package/dist/lib/lanes/quota-notice.js +1 -1
  75. package/dist/lib/lanes/quota.d.ts +1 -1
  76. package/dist/lib/lanes/quota.js +1 -1
  77. package/dist/lib/lanes/repo-merge-hold.d.ts +2 -2
  78. package/dist/lib/lanes/repo-merge-hold.js +2 -2
  79. package/dist/lib/lanes/spawn.d.ts +4 -4
  80. package/dist/lib/lanes/spawn.js +5 -5
  81. package/dist/lib/mesh/live/company-flag-refresh.d.ts +61 -0
  82. package/dist/lib/mesh/live/company-flag-refresh.js +159 -0
  83. package/dist/lib/mesh/live/company-reporting-gaps.d.ts +61 -0
  84. package/dist/lib/mesh/live/company-reporting-gaps.js +238 -0
  85. package/dist/lib/mesh/live/daemon/duplicate-unit.d.ts +156 -0
  86. package/dist/lib/mesh/live/daemon/duplicate-unit.js +616 -0
  87. package/dist/lib/mesh/live/daemon/run.d.ts +13 -0
  88. package/dist/lib/mesh/live/daemon/run.js +35 -2
  89. package/dist/lib/mesh/live/direct-drain.js +1 -1
  90. package/dist/lib/mesh/live/emit.d.ts +3 -0
  91. package/dist/lib/mesh/live/emit.js +32 -9
  92. package/dist/lib/mesh/live/flush.d.ts +3 -0
  93. package/dist/lib/mesh/live/flush.js +115 -32
  94. package/dist/lib/mesh/live/liveness-carry.d.ts +1 -1
  95. package/dist/lib/mesh/live/liveness-carry.js +1 -1
  96. package/dist/lib/mesh/live/reporting-privacy.d.ts +119 -3
  97. package/dist/lib/mesh/live/reporting-privacy.js +235 -11
  98. package/dist/lib/mesh/live/spool.d.ts +17 -2
  99. package/dist/lib/mesh/live/spool.js +22 -6
  100. package/dist/lib/ontology-graph/legacy/coverage.d.ts +67 -0
  101. package/dist/lib/ontology-graph/legacy/coverage.js +192 -0
  102. package/dist/lib/ontology-graph/legacy/index.d.ts +199 -0
  103. package/dist/lib/ontology-graph/legacy/index.js +1150 -0
  104. package/dist/lib/ontology-graph/legacy/parse.d.ts +49 -0
  105. package/dist/lib/ontology-graph/legacy/parse.js +126 -0
  106. package/dist/lib/ontology-graph/legacy/report.d.ts +14 -0
  107. package/dist/lib/ontology-graph/legacy/report.js +254 -0
  108. package/dist/lib/ontology-graph/legacy/verb-map.d.ts +35 -0
  109. package/dist/lib/ontology-graph/legacy/verb-map.js +84 -0
  110. package/dist/lib/ontology-graph/lint/checks.d.ts +140 -0
  111. package/dist/lib/ontology-graph/lint/checks.js +717 -0
  112. package/dist/lib/ontology-graph/lint/coverage.d.ts +106 -0
  113. package/dist/lib/ontology-graph/lint/coverage.js +241 -0
  114. package/dist/lib/ontology-graph/lint/daily-job.d.ts +259 -0
  115. package/dist/lib/ontology-graph/lint/daily-job.js +595 -0
  116. package/dist/lib/ontology-graph/lint/index.d.ts +65 -0
  117. package/dist/lib/ontology-graph/lint/index.js +176 -0
  118. package/dist/lib/ontology-graph/model/files.d.ts +20 -0
  119. package/dist/lib/ontology-graph/model/files.js +89 -0
  120. package/dist/lib/ontology-graph/model/index.d.ts +65 -0
  121. package/dist/lib/ontology-graph/model/index.js +100 -0
  122. package/dist/lib/ontology-graph/model/pass-tasks.d.ts +156 -0
  123. package/dist/lib/ontology-graph/model/pass-tasks.js +345 -0
  124. package/dist/lib/ontology-graph/model/pass.d.ts +109 -0
  125. package/dist/lib/ontology-graph/model/pass.js +958 -0
  126. package/dist/lib/ontology-graph/model/runner.d.ts +50 -0
  127. package/dist/lib/ontology-graph/model/runner.js +160 -0
  128. package/dist/lib/ontology-graph/model/step.d.ts +195 -0
  129. package/dist/lib/ontology-graph/model/step.js +523 -0
  130. package/dist/lib/ontology-graph/model/tasks.d.ts +183 -0
  131. package/dist/lib/ontology-graph/model/tasks.js +349 -0
  132. package/dist/lib/ontology-graph/not-implemented.d.ts +5 -0
  133. package/dist/lib/ontology-graph/not-implemented.js +8 -0
  134. package/dist/lib/ontology-graph/org/compare.d.ts +28 -0
  135. package/dist/lib/ontology-graph/org/compare.js +239 -0
  136. package/dist/lib/ontology-graph/org/derive.d.ts +65 -0
  137. package/dist/lib/ontology-graph/org/derive.js +352 -0
  138. package/dist/lib/ontology-graph/org/index.d.ts +73 -0
  139. package/dist/lib/ontology-graph/org/index.js +513 -0
  140. package/dist/lib/ontology-graph/org/load.d.ts +31 -0
  141. package/dist/lib/ontology-graph/org/load.js +476 -0
  142. package/dist/lib/ontology-graph/org/role.d.ts +38 -0
  143. package/dist/lib/ontology-graph/org/role.js +268 -0
  144. package/dist/lib/ontology-graph/org/structure.d.ts +63 -0
  145. package/dist/lib/ontology-graph/org/structure.js +437 -0
  146. package/dist/lib/ontology-graph/org/text.d.ts +95 -0
  147. package/dist/lib/ontology-graph/org/text.js +315 -0
  148. package/dist/lib/ontology-graph/org/types.d.ts +452 -0
  149. package/dist/lib/ontology-graph/org/types.js +80 -0
  150. package/dist/lib/ontology-graph/pages/index.d.ts +64 -0
  151. package/dist/lib/ontology-graph/pages/index.js +193 -0
  152. package/dist/lib/ontology-graph/pages/render.d.ts +142 -0
  153. package/dist/lib/ontology-graph/pages/render.js +682 -0
  154. package/dist/lib/ontology-graph/proposals/apply.d.ts +109 -0
  155. package/dist/lib/ontology-graph/proposals/apply.js +468 -0
  156. package/dist/lib/ontology-graph/proposals/group.d.ts +63 -0
  157. package/dist/lib/ontology-graph/proposals/group.js +232 -0
  158. package/dist/lib/ontology-graph/proposals/index.d.ts +173 -0
  159. package/dist/lib/ontology-graph/proposals/index.js +474 -0
  160. package/dist/lib/ontology-graph/proposals/model.d.ts +162 -0
  161. package/dist/lib/ontology-graph/proposals/model.js +196 -0
  162. package/dist/lib/ontology-graph/proposals/org-chart.d.ts +82 -0
  163. package/dist/lib/ontology-graph/proposals/org-chart.js +340 -0
  164. package/dist/lib/ontology-graph/read/brief.d.ts +26 -0
  165. package/dist/lib/ontology-graph/read/brief.js +251 -0
  166. package/dist/lib/ontology-graph/read/graph-index.d.ts +76 -0
  167. package/dist/lib/ontology-graph/read/graph-index.js +198 -0
  168. package/dist/lib/ontology-graph/read/index.d.ts +59 -0
  169. package/dist/lib/ontology-graph/read/index.js +792 -0
  170. package/dist/lib/ontology-graph/read/member.d.ts +7 -0
  171. package/dist/lib/ontology-graph/read/member.js +34 -0
  172. package/dist/lib/ontology-graph/read/names.d.ts +44 -0
  173. package/dist/lib/ontology-graph/read/names.js +292 -0
  174. package/dist/lib/ontology-graph/read/reader-setting.d.ts +19 -0
  175. package/dist/lib/ontology-graph/read/reader-setting.js +62 -0
  176. package/dist/lib/ontology-graph/read/test-graph.d.ts +37 -0
  177. package/dist/lib/ontology-graph/read/test-graph.js +150 -0
  178. package/dist/lib/ontology-graph/replay/buffered-store.d.ts +33 -0
  179. package/dist/lib/ontology-graph/replay/buffered-store.js +50 -0
  180. package/dist/lib/ontology-graph/replay/config.d.ts +41 -0
  181. package/dist/lib/ontology-graph/replay/config.js +105 -0
  182. package/dist/lib/ontology-graph/replay/engine.d.ts +100 -0
  183. package/dist/lib/ontology-graph/replay/engine.js +1174 -0
  184. package/dist/lib/ontology-graph/replay/extractors.d.ts +59 -0
  185. package/dist/lib/ontology-graph/replay/extractors.js +1555 -0
  186. package/dist/lib/ontology-graph/replay/index.d.ts +90 -0
  187. package/dist/lib/ontology-graph/replay/index.js +220 -0
  188. package/dist/lib/ontology-graph/replay/listing-manifest.d.ts +66 -0
  189. package/dist/lib/ontology-graph/replay/listing-manifest.js +242 -0
  190. package/dist/lib/ontology-graph/replay/member.d.ts +19 -0
  191. package/dist/lib/ontology-graph/replay/member.js +65 -0
  192. package/dist/lib/ontology-graph/replay/mentions.d.ts +58 -0
  193. package/dist/lib/ontology-graph/replay/mentions.js +239 -0
  194. package/dist/lib/ontology-graph/replay/readers.d.ts +98 -0
  195. package/dist/lib/ontology-graph/replay/readers.js +1251 -0
  196. package/dist/lib/ontology-graph/replay/rules.d.ts +127 -0
  197. package/dist/lib/ontology-graph/replay/rules.js +374 -0
  198. package/dist/lib/ontology-graph/replay/scope.d.ts +35 -0
  199. package/dist/lib/ontology-graph/replay/scope.js +115 -0
  200. package/dist/lib/ontology-graph/replay/transcripts.d.ts +39 -0
  201. package/dist/lib/ontology-graph/replay/transcripts.js +224 -0
  202. package/dist/lib/ontology-graph/replay/util.d.ts +55 -0
  203. package/dist/lib/ontology-graph/replay/util.js +256 -0
  204. package/dist/lib/ontology-graph/resolve/entity-index.d.ts +159 -0
  205. package/dist/lib/ontology-graph/resolve/entity-index.js +652 -0
  206. package/dist/lib/ontology-graph/resolve/index.d.ts +113 -0
  207. package/dist/lib/ontology-graph/resolve/index.js +214 -0
  208. package/dist/lib/ontology-graph/resolve/machine.d.ts +34 -0
  209. package/dist/lib/ontology-graph/resolve/machine.js +95 -0
  210. package/dist/lib/ontology-graph/resolve/normalize.d.ts +35 -0
  211. package/dist/lib/ontology-graph/resolve/normalize.js +124 -0
  212. package/dist/lib/ontology-graph/resolve/org-chart.d.ts +107 -0
  213. package/dist/lib/ontology-graph/resolve/org-chart.js +578 -0
  214. package/dist/lib/ontology-graph/resolve/org-layer.d.ts +78 -0
  215. package/dist/lib/ontology-graph/resolve/org-layer.js +159 -0
  216. package/dist/lib/ontology-graph/resolve/org-seed.d.ts +141 -0
  217. package/dist/lib/ontology-graph/resolve/org-seed.js +631 -0
  218. package/dist/lib/ontology-graph/resolve/org-sources.d.ts +165 -0
  219. package/dist/lib/ontology-graph/resolve/org-sources.js +634 -0
  220. package/dist/lib/ontology-graph/resolve/roster.d.ts +76 -0
  221. package/dist/lib/ontology-graph/resolve/roster.js +226 -0
  222. package/dist/lib/ontology-graph/resolve/seed.d.ts +267 -0
  223. package/dist/lib/ontology-graph/resolve/seed.js +1198 -0
  224. package/dist/lib/ontology-graph/resolve/upsert.d.ts +58 -0
  225. package/dist/lib/ontology-graph/resolve/upsert.js +139 -0
  226. package/dist/lib/ontology-graph/resolve/writer.d.ts +128 -0
  227. package/dist/lib/ontology-graph/resolve/writer.js +351 -0
  228. package/dist/lib/ontology-graph/schema/authority.d.ts +73 -0
  229. package/dist/lib/ontology-graph/schema/authority.js +157 -0
  230. package/dist/lib/ontology-graph/schema/default-schema.d.ts +10 -0
  231. package/dist/lib/ontology-graph/schema/default-schema.js +242 -0
  232. package/dist/lib/ontology-graph/schema/grammar.d.ts +57 -0
  233. package/dist/lib/ontology-graph/schema/grammar.js +409 -0
  234. package/dist/lib/ontology-graph/schema/index.d.ts +12 -0
  235. package/dist/lib/ontology-graph/schema/index.js +10 -0
  236. package/dist/lib/ontology-graph/schema/validate.d.ts +13 -0
  237. package/dist/lib/ontology-graph/schema/validate.js +132 -0
  238. package/dist/lib/ontology-graph/store/files.d.ts +34 -0
  239. package/dist/lib/ontology-graph/store/files.js +207 -0
  240. package/dist/lib/ontology-graph/store/fs.d.ts +30 -0
  241. package/dist/lib/ontology-graph/store/fs.js +101 -0
  242. package/dist/lib/ontology-graph/store/graph-store.d.ts +14 -0
  243. package/dist/lib/ontology-graph/store/graph-store.js +520 -0
  244. package/dist/lib/ontology-graph/store/index.d.ts +18 -0
  245. package/dist/lib/ontology-graph/store/index.js +15 -0
  246. package/dist/lib/ontology-graph/store/ingest.d.ts +37 -0
  247. package/dist/lib/ontology-graph/store/ingest.js +323 -0
  248. package/dist/lib/ontology-graph/store/paths.d.ts +114 -0
  249. package/dist/lib/ontology-graph/store/paths.js +232 -0
  250. package/dist/lib/ontology-graph/types.d.ts +1183 -0
  251. package/dist/lib/ontology-graph/types.js +131 -0
  252. package/dist/lib/secret-scan/gitleaks-rules.generated.js +1 -1
  253. package/dist/utils/frontmatter.d.ts +14 -0
  254. package/dist/utils/frontmatter.js +18 -0
  255. package/dist/utils/interactive-caller.js +1 -1
  256. package/package.json +8 -8
@@ -0,0 +1,323 @@
1
+ /**
2
+ * applyIngest: one input's batch becomes rows on every entity it touches
3
+ * (design 2.9). Extractors hand over forward rows only; this file writes the
4
+ * inverse of every edge on the edge's target, closes superseded single-valued
5
+ * rows and their inverses, marks touched pages dirty, records unresolved
6
+ * mentions and appends one log entry.
7
+ *
8
+ * The whole batch is validated before the first write. Rows are placed in
9
+ * memory first and flushed once per facts file, so an entity file is
10
+ * rewritten at most twice per ingest.
11
+ */
12
+ import { RowRefusedError, } from "../types.js";
13
+ import { isAudienceKey, isEntityRef, isIsoDate, isSourceRef, rowId } from "../schema/grammar.js";
14
+ import { validateEntityUpsert, validateRow } from "../schema/validate.js";
15
+ import { inverseEdge, planSupersession } from "../schema/authority.js";
16
+ import { appendJsonLine, readJsonLines } from "./fs.js";
17
+ import { GRAPH_PATHS, graphPath } from "./paths.js";
18
+ const compareText = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
19
+ // ---------------------------------------------------------------------------
20
+ // Unresolved mentions (.state/unresolved.jsonl)
21
+ // ---------------------------------------------------------------------------
22
+ const unresolvedSeen = new WeakMap();
23
+ function unresolvedFile(store) {
24
+ return graphPath(store.root, GRAPH_PATHS.unresolved);
25
+ }
26
+ function unresolvedKey(mention) {
27
+ return JSON.stringify([mention.mention, mention.reason, mention.src, mention.date, mention.audience]);
28
+ }
29
+ function seenUnresolved(store) {
30
+ let seen = unresolvedSeen.get(store);
31
+ if (!seen) {
32
+ seen = new Set(readJsonLines(unresolvedFile(store)).map(unresolvedKey));
33
+ unresolvedSeen.set(store, seen);
34
+ }
35
+ return seen;
36
+ }
37
+ function checkUnresolved(mention) {
38
+ const refusals = [];
39
+ if (typeof mention?.mention !== "string" || mention.mention.trim() === "") {
40
+ refusals.push({ code: "empty_value", message: "an unresolved mention needs its text" });
41
+ }
42
+ if (typeof mention?.src !== "string" || mention.src.trim() === "") {
43
+ refusals.push({ code: "missing_src", message: "an unresolved mention needs a source (src)" });
44
+ }
45
+ else if (!isSourceRef(mention.src)) {
46
+ refusals.push({ code: "bad_src", message: `not a source ref: "${mention.src}"` });
47
+ }
48
+ if (!isIsoDate(mention?.date))
49
+ refusals.push({ code: "bad_date", message: "an unresolved mention needs a date" });
50
+ if (!isAudienceKey(mention?.audience)) {
51
+ refusals.push({ code: "bad_audience", message: `audience must be "company" or 16 hex, got "${String(mention?.audience)}"` });
52
+ }
53
+ return refusals;
54
+ }
55
+ /**
56
+ * Appends to `.state/unresolved.jsonl`. A mention already recorded (same
57
+ * text, reason, source, date and audience) is not written again, so a replay
58
+ * adds nothing. Returns how many lines were written.
59
+ */
60
+ export function appendUnresolved(store, mentions) {
61
+ const refusals = mentions.flatMap(checkUnresolved);
62
+ if (refusals.length > 0)
63
+ throw new RowRefusedError("unresolved", refusals);
64
+ const seen = seenUnresolved(store);
65
+ let written = 0;
66
+ for (const mention of mentions) {
67
+ const key = unresolvedKey(mention);
68
+ if (seen.has(key))
69
+ continue;
70
+ seen.add(key);
71
+ appendJsonLine(unresolvedFile(store), {
72
+ mention: mention.mention,
73
+ reason: mention.reason,
74
+ src: mention.src,
75
+ date: mention.date,
76
+ audience: mention.audience,
77
+ ...(mention.candidates && mention.candidates.length > 0 ? { candidates: [...mention.candidates].sort(compareText) } : {}),
78
+ });
79
+ written += 1;
80
+ }
81
+ return written;
82
+ }
83
+ export function readUnresolved(store) {
84
+ return readJsonLines(unresolvedFile(store));
85
+ }
86
+ // ---------------------------------------------------------------------------
87
+ // Ledger totals
88
+ // ---------------------------------------------------------------------------
89
+ /** Sums ledger entries (for the end-of-run report). */
90
+ export function ledgerTotals(entries) {
91
+ const totals = {
92
+ entries: 0,
93
+ rowsAdded: 0,
94
+ rowsClosed: 0,
95
+ entitiesCreated: 0,
96
+ modelCalls: 0,
97
+ cacheHits: 0,
98
+ inputTokens: 0,
99
+ outputTokens: 0,
100
+ pending: 0,
101
+ seconds: 0,
102
+ counters: {},
103
+ };
104
+ const num = (value) => (typeof value === "number" && Number.isFinite(value) ? value : 0);
105
+ for (const entry of entries) {
106
+ totals.entries += 1;
107
+ totals.rowsAdded += num(entry.rowsAdded);
108
+ totals.rowsClosed += num(entry.rowsClosed);
109
+ totals.entitiesCreated += num(entry.entitiesCreated);
110
+ totals.modelCalls += num(entry.modelCalls);
111
+ totals.cacheHits += num(entry.cacheHits);
112
+ totals.inputTokens += num(entry.inputTokens);
113
+ totals.outputTokens += num(entry.outputTokens);
114
+ totals.pending += num(entry.pending);
115
+ totals.seconds += num(entry.seconds);
116
+ for (const [name, value] of Object.entries(entry.counters ?? {})) {
117
+ totals.counters[name] = (totals.counters[name] ?? 0) + num(value);
118
+ }
119
+ }
120
+ totals.seconds = Math.round(totals.seconds * 1000) / 1000;
121
+ totals.counters = Object.fromEntries(Object.entries(totals.counters).sort(([a], [b]) => compareText(a, b)));
122
+ return totals;
123
+ }
124
+ function validateBatch(schema, batch) {
125
+ const head = [];
126
+ if (typeof batch?.input !== "string" || batch.input.trim() === "") {
127
+ head.push({ code: "missing_src", message: "an ingest batch needs its input ref" });
128
+ }
129
+ else if (!isSourceRef(batch.input)) {
130
+ head.push({ code: "bad_src", message: `batch input is not a source ref: "${batch.input}"` });
131
+ }
132
+ if (!isIsoDate(batch?.date))
133
+ head.push({ code: "bad_date", message: `batch date is not YYYY-MM-DD: "${String(batch?.date)}"` });
134
+ if (head.length > 0)
135
+ throw new RowRefusedError(String(batch?.input ?? "batch"), head);
136
+ for (const input of batch.entities ?? []) {
137
+ const refusals = validateEntityUpsert(schema, input);
138
+ if (refusals.length > 0)
139
+ throw new RowRefusedError(`${String(input?.type)}/${String(input?.slug)}`, refusals);
140
+ }
141
+ for (const item of batch.rows ?? []) {
142
+ const refusals = validateRow(schema, item?.entity, item?.row, item?.audience);
143
+ if (refusals.length === 0 && item.row.kind === "E") {
144
+ const inverse = inverseEdge(schema, item.entity, item.row);
145
+ if (inverse)
146
+ refusals.push(...validateRow(schema, inverse.entity, inverse.row, item.audience));
147
+ }
148
+ if (refusals.length > 0)
149
+ throw new RowRefusedError(String(item?.entity), refusals);
150
+ }
151
+ const unresolved = (batch.unresolved ?? []).flatMap(checkUnresolved);
152
+ if (unresolved.length > 0)
153
+ throw new RowRefusedError(batch.input, unresolved);
154
+ }
155
+ /** The day a row starts to matter: used to place a batch's rows oldest first. */
156
+ function rowDay(row) {
157
+ return row.kind === "T" ? row.date : row.valid.from;
158
+ }
159
+ /**
160
+ * Applies one input's batch: upserts its entities, writes its rows, writes the
161
+ * inverse of every edge on the edge's target, closes superseded single-valued
162
+ * rows (and their inverses), marks touched pages dirty, records unresolved
163
+ * mentions and appends one log entry. Applying the same batch twice adds 0
164
+ * rows and closes 0 rows, and the second run appends no log entry. Throws
165
+ * RowRefusedError before writing anything if any row is invalid.
166
+ *
167
+ * Authority (design 2.7): a row of lower authority never closes an open row
168
+ * of higher authority. It is written closed on its own day and reported in
169
+ * `contradictions`, so a single-valued key holds one open row at most
170
+ * (correction C8). A row that repeats the value an open row of the same or
171
+ * higher authority already states is counted as a duplicate and not written.
172
+ */
173
+ export function applyIngest(store, batch) {
174
+ const schema = store.schema();
175
+ validateBatch(schema, batch);
176
+ const touched = new Set();
177
+ const changed = new Set();
178
+ let entitiesCreated = 0;
179
+ for (const input of batch.entities) {
180
+ const result = store.upsertEntity(input);
181
+ touched.add(result.entity.id);
182
+ if (result.created) {
183
+ entitiesCreated += 1;
184
+ changed.add(result.entity.id);
185
+ }
186
+ }
187
+ const workings = new Map();
188
+ const working = (entity, audience) => {
189
+ const key = `${audience}\n${entity}`;
190
+ let entry = workings.get(key);
191
+ if (!entry) {
192
+ entry = { entity, audience, rows: new Map(), added: new Set(), closed: new Map() };
193
+ for (const stored of store.readRows(entity, { audiences: [audience] }))
194
+ entry.rows.set(stored.id, stored);
195
+ workings.set(key, entry);
196
+ }
197
+ return entry;
198
+ };
199
+ let rowsAdded = 0;
200
+ let rowsDuplicate = 0;
201
+ let rowsClosed = 0;
202
+ const contradictions = [];
203
+ /** Closes an open row in memory. False when it is absent, a T row, or already closed. */
204
+ const close = (entry, id, closure) => {
205
+ const stored = entry.rows.get(id);
206
+ if (!stored || stored.row.kind === "T" || stored.row.valid.to !== undefined || stored.row.closed)
207
+ return false;
208
+ if (closure.on < stored.row.valid.from)
209
+ return false;
210
+ const row = { ...stored.row, valid: { from: stored.row.valid.from, to: closure.on }, closed: { ...closure } };
211
+ entry.rows.set(id, { ...stored, row });
212
+ if (!entry.added.has(id))
213
+ entry.closed.set(id, { ...closure });
214
+ changed.add(entry.entity);
215
+ rowsClosed += 1;
216
+ return true;
217
+ };
218
+ const ordered = batch.rows
219
+ .map((item, index) => ({ item, index }))
220
+ .sort((a, b) => compareText(rowDay(a.item.row), rowDay(b.item.row)) || a.index - b.index)
221
+ .map((entry) => entry.item);
222
+ for (const item of ordered) {
223
+ const sides = [{ entity: item.entity, row: item.row }];
224
+ if (item.row.kind === "E") {
225
+ const inverse = inverseEdge(schema, item.entity, item.row);
226
+ // A symmetric verb between an entity and itself is its own inverse: one row, not two.
227
+ if (inverse && !(inverse.entity === item.entity && rowId(inverse.row) === rowId(item.row)))
228
+ sides.push(inverse);
229
+ }
230
+ for (const side of sides)
231
+ touched.add(side.entity);
232
+ const fresh = sides.filter((side) => {
233
+ const present = working(side.entity, item.audience).rows.has(rowId(side.row));
234
+ if (present)
235
+ rowsDuplicate += 1;
236
+ return !present;
237
+ });
238
+ if (fresh.length === 0)
239
+ continue;
240
+ // Plan both directions before changing either, so they close together.
241
+ let incomingClosed;
242
+ const closures = [];
243
+ const plans = fresh
244
+ .filter((side) => side.row.kind !== "T")
245
+ .map((side) => {
246
+ const entry = working(side.entity, item.audience);
247
+ return { entry, plan: planSupersession(schema, side.entity, item.audience, [...entry.rows.values()], side.row) };
248
+ });
249
+ if (plans.some(({ plan }) => plan.duplicateOf !== undefined)) {
250
+ // An open row already says this on a single-valued key (either direction of an edge): nothing is written.
251
+ rowsDuplicate += fresh.length;
252
+ continue;
253
+ }
254
+ for (const { entry, plan } of plans) {
255
+ contradictions.push(...plan.contradictions);
256
+ for (const planned of plan.close)
257
+ closures.push({ entry, id: planned.id, closure: planned.closure });
258
+ if (plan.incomingClosed && (!incomingClosed || plan.incomingClosed.to < incomingClosed.to)) {
259
+ incomingClosed = plan.incomingClosed;
260
+ }
261
+ }
262
+ for (const { entry, id, closure } of closures) {
263
+ const rival = entry.rows.get(id)?.row;
264
+ if (!close(entry, id, closure))
265
+ continue;
266
+ if (rival?.kind !== "E")
267
+ continue;
268
+ // The closed edge's other direction lives on its target: close it too.
269
+ const counterpart = inverseEdge(schema, entry.entity, rival);
270
+ if (counterpart)
271
+ close(working(counterpart.entity, entry.audience), rowId(counterpart.row), closure);
272
+ }
273
+ for (const side of fresh) {
274
+ const entry = working(side.entity, item.audience);
275
+ let row = side.row;
276
+ if (row.kind !== "T" && incomingClosed && row.valid.to === undefined) {
277
+ // The batch describes an earlier state than a row already open: written closed, never open.
278
+ row = { ...row, valid: { from: row.valid.from, to: incomingClosed.to }, closed: { ...incomingClosed.closure } };
279
+ }
280
+ const id = rowId(row);
281
+ entry.rows.set(id, { id, entity: side.entity, audience: item.audience, row });
282
+ entry.added.add(id);
283
+ changed.add(side.entity);
284
+ rowsAdded += 1;
285
+ }
286
+ }
287
+ // Flush, one facts file at a time, in a fixed order.
288
+ for (const key of [...workings.keys()].sort(compareText)) {
289
+ const entry = workings.get(key);
290
+ if (entry.added.size > 0) {
291
+ const rows = [...entry.added].map((id) => entry.rows.get(id).row);
292
+ store.writeRows(entry.entity, entry.audience, rows);
293
+ }
294
+ for (const [id, closure] of entry.closed)
295
+ store.closeRow(entry.entity, entry.audience, id, closure);
296
+ }
297
+ const unresolved = batch.unresolved && batch.unresolved.length > 0 ? appendUnresolved(store, batch.unresolved) : 0;
298
+ const touchedList = [...touched].filter(isEntityRef).sort(compareText);
299
+ if (changed.size > 0)
300
+ store.markDirty([...changed].sort(compareText));
301
+ if (rowsAdded > 0 || rowsClosed > 0 || entitiesCreated > 0 || unresolved > 0) {
302
+ store.appendLog({
303
+ date: batch.date,
304
+ op: "ingest",
305
+ subject: batch.input,
306
+ touched: touchedList,
307
+ rowsAdded,
308
+ rowsClosed,
309
+ ...(unresolved > 0 ? { note: `unresolved ${unresolved}` } : {}),
310
+ });
311
+ }
312
+ return {
313
+ input: batch.input,
314
+ touched: touchedList,
315
+ entitiesCreated,
316
+ rowsAdded,
317
+ rowsDuplicate,
318
+ rowsClosed,
319
+ contradictions,
320
+ unresolved,
321
+ };
322
+ }
323
+ //# sourceMappingURL=ingest.js.map
@@ -0,0 +1,114 @@
1
+ import type { AudienceKey, EntityRef } from "../types.js";
2
+ /** Paths relative to the graph root, forward slashes. */
3
+ export declare const GRAPH_PATHS: {
4
+ readonly schema: "schema.yaml";
5
+ readonly entities: "entities";
6
+ readonly facts: "facts";
7
+ readonly pages: "pages";
8
+ readonly index: "index.md";
9
+ readonly log: "log.md";
10
+ readonly brief: "brief.md";
11
+ readonly proposalsOpen: "proposals/open.jsonl";
12
+ readonly proposalsDecided: "proposals/decided.jsonl";
13
+ readonly state: ".state";
14
+ readonly cursor: ".state/cursor.json";
15
+ readonly ledger: ".state/ledger.jsonl";
16
+ readonly cache: ".state/cache";
17
+ readonly dirty: ".state/dirty.txt";
18
+ readonly unresolved: ".state/unresolved.jsonl";
19
+ readonly company: ".state/company";
20
+ };
21
+ /**
22
+ * The graph folder inside a company folder, forward slashes: `.hq/ontology-graph`.
23
+ * A `.hq` folder is ignored by sync at any depth and by HQ root git, so a pull
24
+ * can never overwrite the graph and it never uploads (correction C1). It is
25
+ * NOT under `ontology/`, which sync treats as server-owned.
26
+ */
27
+ export declare const GRAPH_ROOT_RELATIVE = ".hq/ontology-graph";
28
+ /** Environment variable that overrides the graph root (tests, the clean-copy check). */
29
+ export declare const GRAPH_ROOT_ENV = "HQ_ONTOLOGY_GRAPH_ROOT";
30
+ /** `<companyRoot>/.hq/ontology-graph`. */
31
+ export declare function defaultGraphRoot(companyRoot: string): string;
32
+ /** `<hqRoot>/companies/<company>/.hq/ontology-graph`. */
33
+ export declare function graphRootFor(hqRoot: string, company: string): string;
34
+ export interface ResolveGraphRootInput {
35
+ /** `<hqRoot>/companies/<company>`. */
36
+ companyRoot: string;
37
+ /** The `--graph-root` flag. Wins over the environment. */
38
+ flag?: string;
39
+ /** Default: process.env. */
40
+ env?: NodeJS.ProcessEnv;
41
+ }
42
+ /**
43
+ * The one place that decides where a company's graph lives: the `--graph-root`
44
+ * flag, else `HQ_ONTOLOGY_GRAPH_ROOT`, else `<companyRoot>/.hq/ontology-graph`.
45
+ * Always absolute. Every package takes `GraphContext.graphRoot`, which the
46
+ * command file fills from this function; nothing else builds the root.
47
+ */
48
+ export declare function resolveGraphRoot(input: ResolveGraphRootInput): string;
49
+ /** A graph folder holds one company's graph and was asked for by another. */
50
+ export declare class GraphCompanyMismatchError extends Error {
51
+ readonly graphRoot: string;
52
+ readonly stamped: string;
53
+ readonly company: string;
54
+ constructor(graphRoot: string, stamped: string, company: string);
55
+ }
56
+ /** The stamp of a graph folder cannot be trusted: it cannot be read, or it holds no company slug. */
57
+ export declare class GraphCompanyStampError extends Error {
58
+ readonly graphRoot: string;
59
+ constructor(graphRoot: string, reason: string);
60
+ }
61
+ /** An unstamped graph folder at a shared root already holds a graph nobody can say the owner of. */
62
+ export declare class GraphCompanyUnclaimedError extends Error {
63
+ readonly graphRoot: string;
64
+ readonly company: string;
65
+ constructor(graphRoot: string, company: string);
66
+ }
67
+ /**
68
+ * The company a graph folder is stamped for (`.state/company`), or null when
69
+ * it carries no stamp. Only a missing file is "no stamp": a stamp that cannot
70
+ * be read, is empty or is not a company slug throws, so a damaged stamp never
71
+ * opens the folder to everyone.
72
+ */
73
+ export declare function readGraphCompany(graphRoot: string): string | null;
74
+ /**
75
+ * Refuses a graph folder stamped for another company. The root can be shared
76
+ * (`--graph-root`, `HQ_ONTOLOGY_GRAPH_ROOT`), so the folder says whose it is.
77
+ * Reading never stamps: a folder with no stamp is read as it is.
78
+ */
79
+ export declare function assertGraphCompany(graphRoot: string, company: string): void;
80
+ /**
81
+ * May `company` use this graph folder, to read it or to write it. A folder
82
+ * stamped for the company: yes. Stamped for another: never. With no stamp it
83
+ * depends on where the folder is: the company's own
84
+ * `<companyRoot>/.hq/ontology-graph` is the company's by where it lies, and
85
+ * any other root (the flag, the env var) is taken only while it is empty,
86
+ * since a graph already there could be anyone's.
87
+ */
88
+ export declare function assertGraphWritableFor(graphRoot: string, company: string, companyRoot: string): void;
89
+ /**
90
+ * What every writer does before its first write: the folder has to be the
91
+ * company's to use (`assertGraphWritableFor`), and from then on it says so.
92
+ */
93
+ export declare function claimGraphRootFor(graphRoot: string, company: string, companyRoot: string): void;
94
+ /** Stamps a graph folder with its company, once. Refuses one stamped for another company. Writers only. */
95
+ export declare function stampGraphCompany(graphRoot: string, company: string): void;
96
+ export declare function graphPath(root: string, relative: string): string;
97
+ /** `entities/{type}/{slug}.md` */
98
+ export declare function entityFile(root: string, ref: EntityRef): string;
99
+ /** `facts/@{key}` */
100
+ export declare function audienceDir(root: string, audience: AudienceKey): string;
101
+ /** `facts/@{key}/{type}/{slug}.md` */
102
+ export declare function factsFile(root: string, audience: AudienceKey, ref: EntityRef): string;
103
+ /** `pages/{type}/{slug}.md` */
104
+ export declare function pageFile(root: string, ref: EntityRef): string;
105
+ /** `.state/cache/{sha256}.json` */
106
+ export declare function cacheFile(root: string, key: string): string;
107
+ /**
108
+ * The audience keys a graph folder holds rows for, without `company`, sorted:
109
+ * one per `facts/@<key>` folder whose name is an audience key. A graph folder is built for one member
110
+ * from files that member can read, so these are the keys the member holds.
111
+ * A missing folder holds none.
112
+ */
113
+ export declare function heldAudienceKeys(graphRoot: string): AudienceKey[];
114
+ //# sourceMappingURL=paths.d.ts.map
@@ -0,0 +1,232 @@
1
+ /**
2
+ * Where things live inside a graph folder. Every package builds paths through
3
+ * this file so the layout is written down once (see ../CONTRACT.md).
4
+ */
5
+ import * as fs from "node:fs";
6
+ import * as path from "node:path";
7
+ import { isAudienceKey, parseEntityRef } from "../schema/grammar.js";
8
+ /** Paths relative to the graph root, forward slashes. */
9
+ export const GRAPH_PATHS = {
10
+ schema: "schema.yaml",
11
+ entities: "entities",
12
+ facts: "facts",
13
+ pages: "pages",
14
+ index: "index.md",
15
+ log: "log.md",
16
+ brief: "brief.md",
17
+ proposalsOpen: "proposals/open.jsonl",
18
+ proposalsDecided: "proposals/decided.jsonl",
19
+ state: ".state",
20
+ cursor: ".state/cursor.json",
21
+ ledger: ".state/ledger.jsonl",
22
+ cache: ".state/cache",
23
+ dirty: ".state/dirty.txt",
24
+ unresolved: ".state/unresolved.jsonl",
25
+ company: ".state/company",
26
+ };
27
+ /**
28
+ * The graph folder inside a company folder, forward slashes: `.hq/ontology-graph`.
29
+ * A `.hq` folder is ignored by sync at any depth and by HQ root git, so a pull
30
+ * can never overwrite the graph and it never uploads (correction C1). It is
31
+ * NOT under `ontology/`, which sync treats as server-owned.
32
+ */
33
+ export const GRAPH_ROOT_RELATIVE = ".hq/ontology-graph";
34
+ /** Environment variable that overrides the graph root (tests, the clean-copy check). */
35
+ export const GRAPH_ROOT_ENV = "HQ_ONTOLOGY_GRAPH_ROOT";
36
+ /** `<companyRoot>/.hq/ontology-graph`. */
37
+ export function defaultGraphRoot(companyRoot) {
38
+ return path.join(companyRoot, ...GRAPH_ROOT_RELATIVE.split("/"));
39
+ }
40
+ /** `<hqRoot>/companies/<company>/.hq/ontology-graph`. */
41
+ export function graphRootFor(hqRoot, company) {
42
+ if (!/^[a-z0-9][a-z0-9-]*$/.test(company))
43
+ throw new Error(`ontology graph: not a company slug: ${company}`);
44
+ return defaultGraphRoot(path.join(hqRoot, "companies", company));
45
+ }
46
+ /**
47
+ * The one place that decides where a company's graph lives: the `--graph-root`
48
+ * flag, else `HQ_ONTOLOGY_GRAPH_ROOT`, else `<companyRoot>/.hq/ontology-graph`.
49
+ * Always absolute. Every package takes `GraphContext.graphRoot`, which the
50
+ * command file fills from this function; nothing else builds the root.
51
+ */
52
+ export function resolveGraphRoot(input) {
53
+ const flag = input.flag?.trim();
54
+ if (flag)
55
+ return path.resolve(flag);
56
+ const fromEnv = (input.env ?? process.env)[GRAPH_ROOT_ENV]?.trim();
57
+ if (fromEnv)
58
+ return path.resolve(fromEnv);
59
+ return path.resolve(defaultGraphRoot(input.companyRoot));
60
+ }
61
+ /** A graph folder holds one company's graph and was asked for by another. */
62
+ export class GraphCompanyMismatchError extends Error {
63
+ graphRoot;
64
+ stamped;
65
+ company;
66
+ constructor(graphRoot, stamped, company) {
67
+ super(`ontology graph: ${graphRoot} holds the graph of company ${stamped}; it is not opened for company ${company}`);
68
+ this.graphRoot = graphRoot;
69
+ this.stamped = stamped;
70
+ this.company = company;
71
+ this.name = "GraphCompanyMismatchError";
72
+ }
73
+ }
74
+ /** The stamp of a graph folder cannot be trusted: it cannot be read, or it holds no company slug. */
75
+ export class GraphCompanyStampError extends Error {
76
+ graphRoot;
77
+ constructor(graphRoot, reason) {
78
+ super(`ontology graph: the company stamp of ${graphRoot} ${reason}; the folder is not opened`);
79
+ this.graphRoot = graphRoot;
80
+ this.name = "GraphCompanyStampError";
81
+ }
82
+ }
83
+ /** An unstamped graph folder at a shared root already holds a graph nobody can say the owner of. */
84
+ export class GraphCompanyUnclaimedError extends Error {
85
+ graphRoot;
86
+ company;
87
+ constructor(graphRoot, company) {
88
+ super(`ontology graph: ${graphRoot} is not a company's own graph folder, holds a graph and says no company; it is not opened for company ${company}`);
89
+ this.graphRoot = graphRoot;
90
+ this.company = company;
91
+ this.name = "GraphCompanyUnclaimedError";
92
+ }
93
+ }
94
+ const COMPANY_SLUG = /^[a-z0-9][a-z0-9-]*$/;
95
+ /**
96
+ * The company a graph folder is stamped for (`.state/company`), or null when
97
+ * it carries no stamp. Only a missing file is "no stamp": a stamp that cannot
98
+ * be read, is empty or is not a company slug throws, so a damaged stamp never
99
+ * opens the folder to everyone.
100
+ */
101
+ export function readGraphCompany(graphRoot) {
102
+ let text;
103
+ try {
104
+ text = fs.readFileSync(graphPath(graphRoot, GRAPH_PATHS.company), "utf8");
105
+ }
106
+ catch (error) {
107
+ const code = error.code;
108
+ if (code === "ENOENT" || code === "ENOTDIR")
109
+ return null;
110
+ throw new GraphCompanyStampError(graphRoot, `cannot be read (${code ?? "error"})`);
111
+ }
112
+ const slug = text.trim();
113
+ if (!COMPANY_SLUG.test(slug))
114
+ throw new GraphCompanyStampError(graphRoot, slug === "" ? "is empty" : "is not a company slug");
115
+ return slug;
116
+ }
117
+ /**
118
+ * Refuses a graph folder stamped for another company. The root can be shared
119
+ * (`--graph-root`, `HQ_ONTOLOGY_GRAPH_ROOT`), so the folder says whose it is.
120
+ * Reading never stamps: a folder with no stamp is read as it is.
121
+ */
122
+ export function assertGraphCompany(graphRoot, company) {
123
+ const stamped = readGraphCompany(graphRoot);
124
+ if (stamped !== null && stamped !== company)
125
+ throw new GraphCompanyMismatchError(graphRoot, stamped, company);
126
+ }
127
+ /** True when the folder is missing or holds nothing. */
128
+ function graphFolderEmpty(graphRoot) {
129
+ try {
130
+ return fs.readdirSync(graphRoot).length === 0;
131
+ }
132
+ catch (error) {
133
+ if (error.code === "ENOENT")
134
+ return true;
135
+ throw error;
136
+ }
137
+ }
138
+ /**
139
+ * May `company` use this graph folder, to read it or to write it. A folder
140
+ * stamped for the company: yes. Stamped for another: never. With no stamp it
141
+ * depends on where the folder is: the company's own
142
+ * `<companyRoot>/.hq/ontology-graph` is the company's by where it lies, and
143
+ * any other root (the flag, the env var) is taken only while it is empty,
144
+ * since a graph already there could be anyone's.
145
+ */
146
+ export function assertGraphWritableFor(graphRoot, company, companyRoot) {
147
+ const stamped = readGraphCompany(graphRoot);
148
+ if (stamped !== null) {
149
+ if (stamped !== company)
150
+ throw new GraphCompanyMismatchError(graphRoot, stamped, company);
151
+ return;
152
+ }
153
+ if (path.resolve(graphRoot) === path.resolve(defaultGraphRoot(companyRoot)))
154
+ return;
155
+ if (!graphFolderEmpty(graphRoot))
156
+ throw new GraphCompanyUnclaimedError(graphRoot, company);
157
+ }
158
+ /**
159
+ * What every writer does before its first write: the folder has to be the
160
+ * company's to use (`assertGraphWritableFor`), and from then on it says so.
161
+ */
162
+ export function claimGraphRootFor(graphRoot, company, companyRoot) {
163
+ assertGraphWritableFor(graphRoot, company, companyRoot);
164
+ stampGraphCompany(graphRoot, company);
165
+ }
166
+ /** Stamps a graph folder with its company, once. Refuses one stamped for another company. Writers only. */
167
+ export function stampGraphCompany(graphRoot, company) {
168
+ if (!COMPANY_SLUG.test(company))
169
+ throw new Error(`ontology graph: not a company slug: ${company}`);
170
+ assertGraphCompany(graphRoot, company);
171
+ if (readGraphCompany(graphRoot) !== null)
172
+ return;
173
+ const file = graphPath(graphRoot, GRAPH_PATHS.company);
174
+ fs.mkdirSync(path.dirname(file), { recursive: true });
175
+ fs.writeFileSync(file, `${company}\n`);
176
+ }
177
+ function parts(ref) {
178
+ const parsed = parseEntityRef(ref);
179
+ if (!parsed)
180
+ throw new Error(`ontology graph: not an entity ref: ${String(ref)}`);
181
+ return parsed;
182
+ }
183
+ export function graphPath(root, relative) {
184
+ return path.join(root, ...relative.split("/"));
185
+ }
186
+ /** `entities/{type}/{slug}.md` */
187
+ export function entityFile(root, ref) {
188
+ const { type, slug } = parts(ref);
189
+ return path.join(root, GRAPH_PATHS.entities, type, `${slug}.md`);
190
+ }
191
+ /** `facts/@{key}` */
192
+ export function audienceDir(root, audience) {
193
+ if (!isAudienceKey(audience))
194
+ throw new Error(`ontology graph: not an audience key: ${String(audience)}`);
195
+ return path.join(root, GRAPH_PATHS.facts, `@${audience}`);
196
+ }
197
+ /** `facts/@{key}/{type}/{slug}.md` */
198
+ export function factsFile(root, audience, ref) {
199
+ const { type, slug } = parts(ref);
200
+ return path.join(audienceDir(root, audience), type, `${slug}.md`);
201
+ }
202
+ /** `pages/{type}/{slug}.md` */
203
+ export function pageFile(root, ref) {
204
+ const { type, slug } = parts(ref);
205
+ return path.join(root, GRAPH_PATHS.pages, type, `${slug}.md`);
206
+ }
207
+ /** `.state/cache/{sha256}.json` */
208
+ export function cacheFile(root, key) {
209
+ if (!/^[0-9a-f]{64}$/.test(key))
210
+ throw new Error("ontology graph: a cache key is 64 hex");
211
+ return path.join(graphPath(root, GRAPH_PATHS.cache), `${key}.json`);
212
+ }
213
+ /**
214
+ * The audience keys a graph folder holds rows for, without `company`, sorted:
215
+ * one per `facts/@<key>` folder whose name is an audience key. A graph folder is built for one member
216
+ * from files that member can read, so these are the keys the member holds.
217
+ * A missing folder holds none.
218
+ */
219
+ export function heldAudienceKeys(graphRoot) {
220
+ let names;
221
+ try {
222
+ names = fs.readdirSync(graphPath(graphRoot, GRAPH_PATHS.facts));
223
+ }
224
+ catch {
225
+ return [];
226
+ }
227
+ return names
228
+ .filter((name) => name.startsWith("@") && name !== "@company" && isAudienceKey(name.slice(1)))
229
+ .map((name) => name.slice(1))
230
+ .sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
231
+ }
232
+ //# sourceMappingURL=paths.js.map