@jam-overture/loom 0.1.0 → 0.2.0

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 (290) hide show
  1. package/README.md +3 -1
  2. package/dist/application/application.d.ts +109 -0
  3. package/dist/application/application.js +97 -0
  4. package/dist/application/memory.d.ts +20 -0
  5. package/dist/application/memory.js +79 -0
  6. package/dist/application/store.d.ts +101 -0
  7. package/dist/application/store.js +25 -0
  8. package/dist/audience/audience.d.ts +36 -0
  9. package/dist/audience/audience.js +41 -0
  10. package/dist/cli/templates.js +19 -0
  11. package/dist/data/plan.js +0 -0
  12. package/dist/data/resolution.d.ts +23 -1
  13. package/dist/data/resolve.d.ts +30 -3
  14. package/dist/data/resolve.js +11 -3
  15. package/dist/ids.d.ts +10 -0
  16. package/dist/ids.js +11 -1
  17. package/dist/index.d.ts +1 -0
  18. package/dist/index.js +1 -0
  19. package/dist/patch.d.ts +54 -0
  20. package/dist/patch.js +44 -0
  21. package/dist/project/closing.d.ts +51 -0
  22. package/dist/project/closing.js +53 -0
  23. package/dist/project/memory.d.ts +20 -0
  24. package/dist/project/memory.js +97 -0
  25. package/dist/project/project.d.ts +90 -0
  26. package/dist/project/project.js +91 -0
  27. package/dist/project/store.d.ts +135 -0
  28. package/dist/project/store.js +41 -0
  29. package/dist/registered.d.ts +64 -0
  30. package/dist/registered.js +118 -0
  31. package/dist/render/behaviour-adjust.d.ts +11 -19
  32. package/dist/render/behaviour-adjust.js +75 -21
  33. package/dist/render/behaviour-present.d.ts +8 -0
  34. package/dist/render/behaviour-present.js +57 -6
  35. package/dist/render/behaviour.d.ts +98 -17
  36. package/dist/render/behaviour.js +91 -26
  37. package/dist/render/control-name.d.ts +68 -0
  38. package/dist/render/control-name.js +37 -0
  39. package/dist/render/diagnostics.d.ts +72 -0
  40. package/dist/render/diagnostics.js +7 -0
  41. package/dist/render/index.d.ts +2 -0
  42. package/dist/render/index.js +2 -0
  43. package/dist/render/reads.d.ts +11 -0
  44. package/dist/render/reads.js +12 -1
  45. package/dist/render/render.js +102 -7
  46. package/dist/render/slots.d.ts +52 -0
  47. package/dist/render/slots.js +24 -0
  48. package/dist/render/theme.d.ts +54 -1
  49. package/dist/render/theme.js +23 -1
  50. package/dist/render/unshown.d.ts +152 -0
  51. package/dist/render/unshown.js +80 -0
  52. package/dist/runtime/analysis.d.ts +94 -9
  53. package/dist/runtime/analysis.js +54 -9
  54. package/dist/runtime/assessment.d.ts +36 -2
  55. package/dist/runtime/assessment.js +27 -2
  56. package/dist/runtime/checks.d.ts +148 -0
  57. package/dist/runtime/checks.js +112 -0
  58. package/dist/runtime/disposition.d.ts +96 -0
  59. package/dist/runtime/disposition.js +46 -0
  60. package/dist/runtime/gate.js +22 -0
  61. package/dist/runtime/index.d.ts +3 -0
  62. package/dist/runtime/index.js +3 -0
  63. package/dist/runtime/irreversibility.d.ts +64 -0
  64. package/dist/runtime/irreversibility.js +45 -0
  65. package/dist/runtime/pipeline.d.ts +43 -0
  66. package/dist/runtime/pipeline.js +10 -2
  67. package/dist/runtime/policy-change.d.ts +93 -0
  68. package/dist/runtime/policy-change.js +299 -0
  69. package/dist/runtime/policy-log.d.ts +406 -0
  70. package/dist/runtime/policy-log.js +285 -0
  71. package/dist/runtime/reversibility.d.ts +11 -9
  72. package/dist/runtime/stakes.d.ts +129 -1
  73. package/dist/runtime/stakes.js +240 -26
  74. package/dist/runtime/vocabulary.d.ts +164 -7
  75. package/dist/runtime/vocabulary.js +181 -8
  76. package/dist/sdk/audit.d.ts +174 -3
  77. package/dist/sdk/audit.js +176 -17
  78. package/dist/sdk/catalogue.d.ts +9 -1
  79. package/dist/sdk/conformance.d.ts +37 -3
  80. package/dist/sdk/conformance.js +25 -4
  81. package/dist/sdk/definition.d.ts +65 -0
  82. package/dist/sdk/definition.js +22 -0
  83. package/dist/sdk/registry.d.ts +29 -2
  84. package/dist/sdk/registry.js +63 -1
  85. package/dist/signals/action-change.d.ts +507 -0
  86. package/dist/signals/action-change.js +265 -0
  87. package/dist/signals/action.d.ts +331 -0
  88. package/dist/signals/action.js +182 -0
  89. package/dist/signals/applied-window.contract.d.ts +23 -0
  90. package/dist/signals/applied-window.contract.js +160 -0
  91. package/dist/signals/arriving.d.ts +210 -0
  92. package/dist/signals/arriving.js +133 -0
  93. package/dist/signals/audiences.d.ts +528 -0
  94. package/dist/signals/audiences.js +255 -0
  95. package/dist/signals/broadcast.d.ts +8 -2
  96. package/dist/signals/broadcast.js +58 -8
  97. package/dist/signals/change.d.ts +378 -0
  98. package/dist/signals/change.js +327 -0
  99. package/dist/signals/collect.d.ts +41 -0
  100. package/dist/signals/collect.js +88 -7
  101. package/dist/signals/copy-change.d.ts +553 -0
  102. package/dist/signals/copy-change.js +377 -0
  103. package/dist/signals/copy.d.ts +337 -0
  104. package/dist/signals/copy.js +211 -0
  105. package/dist/signals/counters.d.ts +46 -0
  106. package/dist/signals/counters.js +102 -0
  107. package/dist/signals/deliver.d.ts +23 -4
  108. package/dist/signals/deliver.js +34 -6
  109. package/dist/signals/deployment-change.d.ts +371 -0
  110. package/dist/signals/deployment-change.js +268 -0
  111. package/dist/signals/deployment.d.ts +265 -0
  112. package/dist/signals/deployment.js +152 -0
  113. package/dist/signals/door.d.ts +92 -0
  114. package/dist/signals/door.js +53 -0
  115. package/dist/signals/fold.d.ts +10 -0
  116. package/dist/signals/fold.js +5 -1
  117. package/dist/signals/form-action.d.ts +302 -0
  118. package/dist/signals/form-action.js +169 -0
  119. package/dist/signals/form-copy.d.ts +523 -0
  120. package/dist/signals/form-copy.js +346 -0
  121. package/dist/signals/forms.d.ts +247 -0
  122. package/dist/signals/forms.js +147 -0
  123. package/dist/signals/funnel-change.d.ts +548 -0
  124. package/dist/signals/funnel-change.js +361 -0
  125. package/dist/signals/funnel.d.ts +532 -0
  126. package/dist/signals/funnel.js +216 -0
  127. package/dist/signals/health.d.ts +497 -0
  128. package/dist/signals/health.js +361 -0
  129. package/dist/signals/index.d.ts +41 -0
  130. package/dist/signals/index.js +41 -0
  131. package/dist/signals/ingest.d.ts +56 -1
  132. package/dist/signals/ingest.js +91 -1
  133. package/dist/signals/journal.d.ts +11 -0
  134. package/dist/signals/kinds.d.ts +8 -6
  135. package/dist/signals/kinds.js +14 -6
  136. package/dist/signals/library-action.d.ts +430 -0
  137. package/dist/signals/library-action.js +336 -0
  138. package/dist/signals/library-change.d.ts +477 -0
  139. package/dist/signals/library-change.js +335 -0
  140. package/dist/signals/library-copy-change.d.ts +498 -0
  141. package/dist/signals/library-copy-change.js +476 -0
  142. package/dist/signals/library-pace.d.ts +319 -0
  143. package/dist/signals/library-pace.js +305 -0
  144. package/dist/signals/library-reach-change.d.ts +636 -0
  145. package/dist/signals/library-reach-change.js +534 -0
  146. package/dist/signals/library-reach.d.ts +377 -0
  147. package/dist/signals/library-reach.js +227 -0
  148. package/dist/signals/library-word-reach-change.d.ts +567 -0
  149. package/dist/signals/library-word-reach-change.js +568 -0
  150. package/dist/signals/library.d.ts +306 -0
  151. package/dist/signals/library.js +178 -0
  152. package/dist/signals/memory.d.ts +9 -0
  153. package/dist/signals/memory.js +105 -14
  154. package/dist/signals/migrate.d.ts +17 -2
  155. package/dist/signals/migrate.js +122 -5
  156. package/dist/signals/notice.d.ts +334 -0
  157. package/dist/signals/notice.js +228 -0
  158. package/dist/signals/pace-change.d.ts +501 -0
  159. package/dist/signals/pace-change.js +310 -0
  160. package/dist/signals/pace.d.ts +296 -0
  161. package/dist/signals/pace.js +291 -0
  162. package/dist/signals/page-views.d.ts +365 -0
  163. package/dist/signals/page-views.js +218 -0
  164. package/dist/signals/parts.d.ts +344 -0
  165. package/dist/signals/parts.js +273 -0
  166. package/dist/signals/postgres.d.ts +16 -2
  167. package/dist/signals/postgres.js +390 -87
  168. package/dist/signals/progress.d.ts +212 -0
  169. package/dist/signals/progress.js +150 -0
  170. package/dist/signals/ratios.d.ts +108 -0
  171. package/dist/signals/ratios.js +40 -0
  172. package/dist/signals/reach.d.ts +309 -0
  173. package/dist/signals/reach.js +93 -0
  174. package/dist/signals/readable.d.ts +34 -0
  175. package/dist/signals/readable.js +34 -0
  176. package/dist/signals/readership.d.ts +381 -0
  177. package/dist/signals/readership.js +308 -0
  178. package/dist/signals/region-pages.d.ts +173 -0
  179. package/dist/signals/region-pages.js +62 -0
  180. package/dist/signals/region.d.ts +287 -0
  181. package/dist/signals/region.js +198 -0
  182. package/dist/signals/retention.d.ts +141 -0
  183. package/dist/signals/retention.js +58 -0
  184. package/dist/signals/rollup.d.ts +67 -3
  185. package/dist/signals/rollup.js +0 -0
  186. package/dist/signals/schema.d.ts +432 -1
  187. package/dist/signals/schema.js +157 -3
  188. package/dist/signals/signal.d.ts +237 -11
  189. package/dist/signals/signal.js +70 -10
  190. package/dist/signals/silence-census.d.ts +205 -0
  191. package/dist/signals/silence-census.js +87 -0
  192. package/dist/signals/silences.d.ts +848 -0
  193. package/dist/signals/silences.js +753 -0
  194. package/dist/signals/tally.d.ts +140 -11
  195. package/dist/signals/window-figures.d.ts +148 -0
  196. package/dist/signals/window-figures.js +42 -0
  197. package/dist/signals/window.d.ts +866 -0
  198. package/dist/signals/window.js +693 -0
  199. package/dist/signals/words.d.ts +24 -0
  200. package/dist/signals/words.js +24 -0
  201. package/dist/store/index.d.ts +19 -0
  202. package/dist/store/index.js +19 -0
  203. package/dist/store/memory.d.ts +2 -1
  204. package/dist/store/memory.js +48 -12
  205. package/dist/store/migrate.d.ts +80 -0
  206. package/dist/store/migrate.js +259 -7
  207. package/dist/store/postgres-applications.d.ts +4 -0
  208. package/dist/store/postgres-applications.js +197 -0
  209. package/dist/store/postgres-holds.d.ts +10 -1
  210. package/dist/store/postgres-holds.js +116 -101
  211. package/dist/store/postgres-policy-log.d.ts +14 -0
  212. package/dist/store/postgres-policy-log.js +240 -0
  213. package/dist/store/postgres-projects.d.ts +4 -0
  214. package/dist/store/postgres-projects.js +217 -0
  215. package/dist/store/postgres.d.ts +16 -1
  216. package/dist/store/postgres.js +59 -9
  217. package/dist/store/replay.d.ts +102 -1
  218. package/dist/store/replay.js +190 -19
  219. package/dist/store/reseed.d.ts +238 -0
  220. package/dist/store/reseed.js +308 -0
  221. package/dist/store/schema.d.ts +450 -2
  222. package/dist/store/schema.js +141 -5
  223. package/dist/store/store.d.ts +29 -6
  224. package/dist/submit/resolve.js +2 -1
  225. package/dist/telemetry/calibration.d.ts +25 -0
  226. package/dist/telemetry/calibration.js +27 -1
  227. package/dist/telemetry/event.d.ts +391 -33
  228. package/dist/telemetry/event.js +84 -20
  229. package/dist/telemetry/index.d.ts +1 -0
  230. package/dist/telemetry/index.js +1 -0
  231. package/dist/telemetry/journal.d.ts +72 -3
  232. package/dist/telemetry/journal.js +9 -0
  233. package/dist/telemetry/lookup.d.ts +27 -0
  234. package/dist/telemetry/lookup.js +18 -0
  235. package/dist/telemetry/memory.js +23 -0
  236. package/dist/telemetry/migrate.js +22 -1
  237. package/dist/telemetry/postgres.d.ts +10 -1
  238. package/dist/telemetry/postgres.js +172 -90
  239. package/dist/telemetry/remeasure.d.ts +78 -0
  240. package/dist/telemetry/remeasure.js +83 -0
  241. package/dist/telemetry/retention.d.ts +2 -2
  242. package/dist/telemetry/schema.d.ts +17 -0
  243. package/dist/telemetry/schema.js +27 -1
  244. package/dist/telemetry/sink.d.ts +2 -2
  245. package/dist/testing/application-contract.d.ts +14 -0
  246. package/dist/testing/application-contract.js +270 -0
  247. package/dist/testing/contracts.d.ts +12 -7
  248. package/dist/testing/contracts.js +12 -7
  249. package/dist/testing/definitions.d.ts +78 -0
  250. package/dist/testing/definitions.js +134 -9
  251. package/dist/testing/doubles.d.ts +103 -0
  252. package/dist/testing/doubles.js +143 -0
  253. package/dist/testing/index.d.ts +2 -2
  254. package/dist/testing/index.js +2 -2
  255. package/dist/testing/journal-contract.js +140 -2
  256. package/dist/testing/model-contract.d.ts +110 -0
  257. package/dist/testing/model-contract.js +150 -0
  258. package/dist/testing/policy-log-contract.d.ts +26 -0
  259. package/dist/testing/policy-log-contract.js +344 -0
  260. package/dist/testing/project-contract.d.ts +14 -0
  261. package/dist/testing/project-contract.js +266 -0
  262. package/dist/testing/reader-signal-contract.d.ts +27 -2
  263. package/dist/testing/reader-signal-contract.js +475 -5
  264. package/dist/testing/store-contract.d.ts +7 -1
  265. package/dist/testing/store-contract.js +84 -3
  266. package/dist/theme/apply.d.ts +20 -1
  267. package/dist/theme/apply.js +5 -3
  268. package/dist/theme/derive.d.ts +22 -0
  269. package/dist/theme/derive.js +54 -1
  270. package/dist/theme/library.js +1 -1
  271. package/dist/theme/measure.d.ts +34 -0
  272. package/dist/theme/measure.js +30 -1
  273. package/dist/theme/palettes.js +7 -7
  274. package/dist/theme/ramp.d.ts +64 -0
  275. package/dist/theme/ramp.js +114 -0
  276. package/dist/theme/registry.d.ts +9 -0
  277. package/dist/theme/registry.js +11 -1
  278. package/dist/theme/separation.d.ts +97 -3
  279. package/dist/theme/separation.js +75 -0
  280. package/dist/tree/compare.d.ts +9 -0
  281. package/dist/tree/compare.js +18 -0
  282. package/dist/write/computed.d.ts +60 -0
  283. package/dist/write/computed.js +61 -0
  284. package/dist/write/held.d.ts +59 -0
  285. package/dist/write/index.d.ts +5 -2
  286. package/dist/write/index.js +5 -2
  287. package/dist/write/reseed.d.ts +151 -0
  288. package/dist/write/reseed.js +184 -0
  289. package/dist/write/revert.js +2 -16
  290. package/package.json +98 -19
@@ -0,0 +1,24 @@
1
+ /**
2
+ * What this subsystem calls a word, in one place.
3
+ *
4
+ * Two readings now cost the same page — the time its words take
5
+ * (`pace.ts`) and how much of what it says got read (`copy.ts`) — and a word
6
+ * is the unit both of them divide by. Two spellings of one rule that agree
7
+ * today is a thing this subsystem has already been bitten by once, in the
8
+ * counter keys the two stores wrote; the remedy there was to publish the rule
9
+ * once and have both sides import it, and this is the same remedy before the
10
+ * same bite.
11
+ */
12
+ /**
13
+ * Words in a run of strings.
14
+ *
15
+ * Whitespace-separated, which is the same rule `sdk/copy.ts` uses to decide a
16
+ * blank value is not a word, and the same one a person counting a paragraph
17
+ * would use. It is wrong for a language that does not put spaces between words,
18
+ * and the handle for that is the costing rate rather than this function: a rate
19
+ * expressed in whatever this calls a word stays internally consistent, and a
20
+ * share of a page's words is unaffected because the same rule is applied to the
21
+ * numerator and the denominator.
22
+ */
23
+ export declare const countWords: (values: readonly string[]) => number;
24
+ //# sourceMappingURL=words.d.ts.map
@@ -0,0 +1,24 @@
1
+ /**
2
+ * What this subsystem calls a word, in one place.
3
+ *
4
+ * Two readings now cost the same page — the time its words take
5
+ * (`pace.ts`) and how much of what it says got read (`copy.ts`) — and a word
6
+ * is the unit both of them divide by. Two spellings of one rule that agree
7
+ * today is a thing this subsystem has already been bitten by once, in the
8
+ * counter keys the two stores wrote; the remedy there was to publish the rule
9
+ * once and have both sides import it, and this is the same remedy before the
10
+ * same bite.
11
+ */
12
+ /**
13
+ * Words in a run of strings.
14
+ *
15
+ * Whitespace-separated, which is the same rule `sdk/copy.ts` uses to decide a
16
+ * blank value is not a word, and the same one a person counting a paragraph
17
+ * would use. It is wrong for a language that does not put spaces between words,
18
+ * and the handle for that is the costing rate rather than this function: a rate
19
+ * expressed in whatever this calls a word stays internally consistent, and a
20
+ * share of a page's words is unaffected because the same rule is applied to the
21
+ * numerator and the denominator.
22
+ */
23
+ export const countWords = (values) => values.reduce((count, value) => count + (value.match(/\S+/g)?.length ?? 0), 0);
24
+ //# sourceMappingURL=words.js.map
@@ -4,11 +4,30 @@
4
4
  * A store holds a snapshot and an append-only log beside it, so that any revision
5
5
  * can be replayed, attributed, or undone. Two implementations, one contract.
6
6
  */
7
+ /**
8
+ * The project a store speaks for (0260), re-exported here rather than given an
9
+ * entry point of its own: it is the scope every one of these reads sits
10
+ * inside, so a caller that imports a store already needs it.
11
+ *
12
+ * The audience is here on the same terms (0284). It is not a scope a store
13
+ * speaks for — it is a column on the reader tables and a field a sender may
14
+ * set — but it is an id a host has to be able to *name*, and the alternative
15
+ * to naming it here is a host that can read the field and not write one.
16
+ */
17
+ export * from "../application/application.js";
18
+ export * from "../application/memory.js";
19
+ export * from "../audience/audience.js";
20
+ export * from "../project/project.js";
21
+ export * from "../project/memory.js";
22
+ export * from "../project/closing.js";
23
+ export * from "../project/store.js";
24
+ export * from "../application/store.js";
7
25
  export * from "./attribution.js";
8
26
  export * from "./driver.js";
9
27
  export * from "./errors.js";
10
28
  export * from "./memory.js";
11
29
  export * from "./replay.js";
30
+ export * from "./reseed.js";
12
31
  export * from "./revert.js";
13
32
  export * from "./source.js";
14
33
  export * from "./store.js";
@@ -4,11 +4,30 @@
4
4
  * A store holds a snapshot and an append-only log beside it, so that any revision
5
5
  * can be replayed, attributed, or undone. Two implementations, one contract.
6
6
  */
7
+ /**
8
+ * The project a store speaks for (0260), re-exported here rather than given an
9
+ * entry point of its own: it is the scope every one of these reads sits
10
+ * inside, so a caller that imports a store already needs it.
11
+ *
12
+ * The audience is here on the same terms (0284). It is not a scope a store
13
+ * speaks for — it is a column on the reader tables and a field a sender may
14
+ * set — but it is an id a host has to be able to *name*, and the alternative
15
+ * to naming it here is a host that can read the field and not write one.
16
+ */
17
+ export * from "../application/application.js";
18
+ export * from "../application/memory.js";
19
+ export * from "../audience/audience.js";
20
+ export * from "../project/project.js";
21
+ export * from "../project/memory.js";
22
+ export * from "../project/closing.js";
23
+ export * from "../project/store.js";
24
+ export * from "../application/store.js";
7
25
  export * from "./attribution.js";
8
26
  export * from "./driver.js";
9
27
  export * from "./errors.js";
10
28
  export * from "./memory.js";
11
29
  export * from "./replay.js";
30
+ export * from "./reseed.js";
12
31
  export * from "./revert.js";
13
32
  export * from "./source.js";
14
33
  export * from "./store.js";
@@ -1,3 +1,4 @@
1
+ import { type StoreScope } from "../application/application.js";
1
2
  import { type TreeStore } from "./store.js";
2
- export declare const memoryTreeStore: () => TreeStore;
3
+ export declare const memoryTreeStore: (scope?: StoreScope) => TreeStore;
3
4
  //# sourceMappingURL=memory.d.ts.map
@@ -1,15 +1,40 @@
1
+ import { DEFAULT_APPLICATION_ID } from "../application/application.js";
1
2
  import { cursorPosition, pageEnds } from "../paging.js";
2
3
  import { err, ok } from "../result.js";
3
4
  import { applyDelta } from "../tree/apply.js";
4
5
  import { anchorBound, anchorResumes, clampListingLimit, clampRevisionLimit, } from "./store.js";
5
- export const memoryTreeStore = () => {
6
+ /**
7
+ * The summary, read off the log rather than assembled beside it.
8
+ *
9
+ * `changedAt` is the newest entry's `appliedAt`, and taking it from the log is
10
+ * what makes the contract's promise true here by construction: an empty log is
11
+ * revision 0 and yields no stamp, so the two cannot disagree without `append`
12
+ * having broken the invariant that advances them together.
13
+ */
14
+ const listingOf = (entry) => {
15
+ const newest = entry.log.at(-1);
16
+ return {
17
+ treeId: entry.snapshot.treeId,
18
+ revision: entry.snapshot.revision,
19
+ ...(newest === undefined ? {} : { changedAt: newest.appliedAt }),
20
+ };
21
+ };
22
+ export const memoryTreeStore = (scope = {}) => {
23
+ const project = scope.application ?? DEFAULT_APPLICATION_ID;
24
+ /*
25
+ * Keyed by project and tree even though a handle only ever sees one project,
26
+ * so that this store means the same thing the Postgres one does: a tree id
27
+ * is unique within a project and says nothing about any other. Without it a
28
+ * test that built one handle and expected two projects would quietly get one.
29
+ */
6
30
  const trees = new Map();
7
- const entryOf = (treeId) => trees.get(treeId);
31
+ const keyOf = (treeId) => `${project}\u0000${treeId}`;
32
+ const entryOf = (treeId) => trees.get(keyOf(treeId));
8
33
  return {
9
- create: (tree) => Promise.resolve(trees.has(tree.treeId)
34
+ create: (tree) => Promise.resolve(trees.has(keyOf(tree.treeId))
10
35
  ? err({ code: "already-exists", treeId: tree.treeId })
11
36
  : (() => {
12
- trees.set(tree.treeId, { snapshot: tree, log: [] });
37
+ trees.set(keyOf(tree.treeId), { snapshot: tree, log: [] });
13
38
  return ok(tree);
14
39
  })()),
15
40
  head: (treeId) => {
@@ -56,18 +81,29 @@ export const memoryTreeStore = () => {
56
81
  list: (request) => {
57
82
  const limit = clampListingLimit(request?.limit);
58
83
  const cursor = request?.cursor;
59
- const keys = Array.from(trees.keys())
84
+ /*
85
+ * Over tree ids rather than over the map's keys, which now carry the
86
+ * project in front of them. A cursor is opaque to whoever holds it, and
87
+ * a key would make it opaque *and* project-specific — so a page token
88
+ * from one handle, replayed against another, would silently return
89
+ * nothing instead of failing. The ids are what the caller was given and
90
+ * are what it gets back.
91
+ */
92
+ const ids = Array.from(trees.values())
93
+ .map((entry) => entry.snapshot.treeId)
60
94
  .sort()
61
- .filter((key) => cursor === undefined || key > cursor);
62
- const page = keys.slice(0, limit);
63
- const listings = page.flatMap((key) => {
64
- const entry = trees.get(key);
65
- return entry ? [{ treeId: entry.snapshot.treeId, revision: entry.snapshot.revision }] : [];
95
+ .filter((treeId) => cursor === undefined || treeId > cursor);
96
+ const page = ids.slice(0, limit);
97
+ const listings = page.flatMap((treeId) => {
98
+ const entry = entryOf(treeId);
99
+ if (!entry)
100
+ return [];
101
+ return [listingOf(entry)];
66
102
  });
67
103
  const last = page.at(-1);
68
104
  return Promise.resolve(ok({
69
105
  trees: listings,
70
- cursor: last !== undefined && keys.length > page.length ? last : null,
106
+ cursor: last !== undefined && ids.length > page.length ? last : null,
71
107
  }));
72
108
  },
73
109
  append: (treeId, request) => {
@@ -107,7 +143,7 @@ export const memoryTreeStore = () => {
107
143
  ...(request.answeredBy === undefined ? {} : { answeredBy: request.answeredBy }),
108
144
  };
109
145
  /** One write, so the log and the snapshot cannot be seen disagreeing. */
110
- trees.set(treeId, { snapshot: applied.value, log: [...entry.log, revision] });
146
+ trees.set(keyOf(treeId), { snapshot: applied.value, log: [...entry.log, revision] });
111
147
  return Promise.resolve(ok(applied.value));
112
148
  },
113
149
  };
@@ -37,10 +37,68 @@ export declare const TREE_STORE_DDL: readonly string[];
37
37
  * the entire reason the read exists.
38
38
  */
39
39
  export declare const HOLD_STORE_DDL: readonly string[];
40
+ /**
41
+ * The policy log's schema, as statements a host can run.
42
+ *
43
+ * Its own list for the reason the hold store's is: the three are separately
44
+ * useful. A deployment that judges with one policy it has never edited has no
45
+ * reason to carry this table, and one that records policy changes may well keep
46
+ * them beside its trees or somewhere else entirely.
47
+ *
48
+ * The index is not decoration. `judgedUnder` resolves the fingerprint on a
49
+ * disposition into the policy text it was judged under, and a screen showing a
50
+ * page of records asks it once per record; without the index each of those is a
51
+ * scan of every policy revision the deployment has recorded.
52
+ */
53
+ export declare const POLICY_LOG_DDL: readonly string[];
40
54
  /**
41
55
  * Creates the tables if they are absent. Every statement is `IF NOT EXISTS`, so
42
56
  * running it against a populated database is a no-op rather than a hazard.
43
57
  */
58
+ /**
59
+ * The projects table.
60
+ *
61
+ * Carries no `application_id` default and no backfill, unlike every other statement
62
+ * here: the rows that already exist belong to `app_default` by the default on
63
+ * their own columns, and a row *for* `app_default` is seeded by the deployment
64
+ * rather than conjured by DDL — a migration that invented a project's name
65
+ * would be guessing, which 0260 refuses.
66
+ */
67
+ export declare const APPLICATION_STORE_DDL: readonly string[];
68
+ /**
69
+ * The projects the applications are grouped under.
70
+ *
71
+ * Its own list rather than a statement in the one above, on the terms this file
72
+ * already sets: a deployment adds a seam by adding its `ensure` call on
73
+ * purpose. They are run together by `db:push` because an application's
74
+ * `project_id` would otherwise point at nothing.
75
+ *
76
+ * **It must run after `ensureApplicationStoreSchema`, not before.** That one
77
+ * renames the `loom_projects` which briefly held applications; this one then
78
+ * creates a fresh table under the freed name. The other order silently loses
79
+ * the grouping table — `CREATE TABLE IF NOT EXISTS` sees the old one, skips,
80
+ * and the rename carries it off — with no error anywhere.
81
+ */
82
+ export declare const PROJECT_STORE_DDL: readonly string[];
83
+ /**
84
+ * Moving the ids themselves, which the rename above does not.
85
+ *
86
+ * Every row written before 12 October names `prj_default` in a column that now
87
+ * means an application, and `app_default` is what `DEFAULT_APPLICATION_ID`
88
+ * parses to. Six tables carry it, and each update is idempotent because it
89
+ * matches only the old value.
90
+ *
91
+ * **Each one is guarded on its table existing**, which the first draft was not
92
+ * and a test caught: a deployment adds a seam by adding its `ensure` call on
93
+ * purpose, so one that never switched telemetry or the policy log on has no
94
+ * such table, and an unguarded `UPDATE` took the whole migration down with
95
+ * `42P01` rather than skipping what was not there.
96
+ *
97
+ * Separate from the DDL because it is data: a host reading `db:push` can see
98
+ * exactly which rows are touched, rather than finding an `UPDATE` folded into a
99
+ * list called schema.
100
+ */
101
+ export declare const APPLICATION_ID_MIGRATION: readonly string[];
44
102
  export declare const ensureTreeStoreSchema: (db: LoomDatabase) => Promise<void>;
45
103
  /**
46
104
  * Creates the hold table if it is absent. Idempotent on the same terms.
@@ -51,4 +109,26 @@ export declare const ensureTreeStoreSchema: (db: LoomDatabase) => Promise<void>;
51
109
  * something else.
52
110
  */
53
111
  export declare const ensureHoldStoreSchema: (db: LoomDatabase) => Promise<void>;
112
+ /**
113
+ * Creates the policy log's table if it is absent. Idempotent on the same terms,
114
+ * and separate for the same reason: a deployment that wants a record of who
115
+ * changed its acceptance policy gets it by adding this call on purpose.
116
+ */
117
+ export declare const ensurePolicyLogSchema: (db: LoomDatabase) => Promise<void>;
118
+ /**
119
+ * Creates the projects table if it is absent. Idempotent, and separate for the
120
+ * same reason the other three are: a deployment that wants more than one
121
+ * project gets the table by adding this call on purpose.
122
+ */
123
+ export declare const ensureApplicationStoreSchema: (db: LoomDatabase) => Promise<void>;
124
+ /**
125
+ * Creates the projects table if it is absent. Idempotent on the same terms.
126
+ */
127
+ export declare const ensureProjectStoreSchema: (db: LoomDatabase) => Promise<void>;
128
+ /**
129
+ * Carries `prj_default` to `app_default` across every table that names an
130
+ * application. Safe to run on a database that has already been carried, and on
131
+ * one that was never on the old value.
132
+ */
133
+ export declare const migrateApplicationIds: (db: LoomDatabase) => Promise<void>;
54
134
  //# sourceMappingURL=migrate.d.ts.map
@@ -21,12 +21,15 @@ import { sql } from "drizzle-orm";
21
21
  */
22
22
  export const TREE_STORE_DDL = [
23
23
  `CREATE TABLE IF NOT EXISTS loom_trees (
24
- tree_id text PRIMARY KEY,
24
+ application_id text NOT NULL DEFAULT 'app_default',
25
+ tree_id text NOT NULL,
25
26
  revision integer NOT NULL,
26
27
  document jsonb NOT NULL,
27
- updated_at timestamptz NOT NULL DEFAULT now()
28
+ updated_at timestamptz NOT NULL DEFAULT now(),
29
+ PRIMARY KEY (application_id, tree_id)
28
30
  )`,
29
31
  `CREATE TABLE IF NOT EXISTS loom_revisions (
32
+ application_id text NOT NULL DEFAULT 'app_default',
30
33
  tree_id text NOT NULL,
31
34
  revision integer NOT NULL,
32
35
  proposal_id text NOT NULL,
@@ -34,8 +37,33 @@ export const TREE_STORE_DDL = [
34
37
  provenance jsonb NOT NULL,
35
38
  applied_at text NOT NULL,
36
39
  answered_by text,
37
- PRIMARY KEY (tree_id, revision)
40
+ PRIMARY KEY (application_id, tree_id, revision)
38
41
  )`,
42
+ /*
43
+ * **The migration, for a database that predates projects (0260).**
44
+ *
45
+ * Two statements per table and both are needed. The column is additive and
46
+ * defaulted, so every row already written becomes the default project's —
47
+ * 0260 clause 5, *nothing is migrated by guessing*: those rows belong to the
48
+ * deployment that wrote them, and one project named for it is the honest
49
+ * answer rather than a distribution somebody invented.
50
+ *
51
+ * The key then has to widen, because a tree id is only unique **within** a
52
+ * project now and the old key says it is unique everywhere. Dropping by the
53
+ * name Postgres gives a primary key and re-adding is idempotent: after the
54
+ * first run the constraint exists again under the same name, so a second run
55
+ * drops and rebuilds an identical key. On a table created by the statements
56
+ * above it is already right and the pair is a no-op.
57
+ *
58
+ * Ordering matters and is why these are not beside their tables: the column
59
+ * must exist on both before either key can name it.
60
+ */
61
+ `ALTER TABLE loom_trees ADD COLUMN IF NOT EXISTS application_id text NOT NULL DEFAULT 'app_default'`,
62
+ `ALTER TABLE loom_revisions ADD COLUMN IF NOT EXISTS application_id text NOT NULL DEFAULT 'app_default'`,
63
+ `ALTER TABLE loom_trees DROP CONSTRAINT IF EXISTS loom_trees_pkey`,
64
+ `ALTER TABLE loom_trees ADD PRIMARY KEY (application_id, tree_id)`,
65
+ `ALTER TABLE loom_revisions DROP CONSTRAINT IF EXISTS loom_revisions_pkey`,
66
+ `ALTER TABLE loom_revisions ADD PRIMARY KEY (application_id, tree_id, revision)`,
39
67
  /**
40
68
  * For a database that already has the table: `CREATE TABLE IF NOT EXISTS`
41
69
  * silently leaves an existing table alone, columns and all, so a column added
@@ -83,23 +111,217 @@ export const TREE_STORE_DDL = [
83
111
  */
84
112
  export const HOLD_STORE_DDL = [
85
113
  `CREATE TABLE IF NOT EXISTS loom_holds (
86
- proposal_id text PRIMARY KEY,
114
+ application_id text NOT NULL DEFAULT 'app_default',
115
+ proposal_id text NOT NULL,
87
116
  tree_id text NOT NULL,
88
117
  base_revision integer NOT NULL,
89
118
  intent jsonb NOT NULL,
90
119
  proposal jsonb NOT NULL,
91
120
  disposition jsonb NOT NULL,
92
- held_at text NOT NULL
121
+ held_at text NOT NULL,
122
+ PRIMARY KEY (application_id, proposal_id)
93
123
  )`,
94
- `CREATE INDEX IF NOT EXISTS loom_holds_tree_id_held_at_idx ON loom_holds (tree_id, held_at)`,
95
- `CREATE INDEX IF NOT EXISTS loom_holds_held_at_proposal_id_idx ON loom_holds (held_at, proposal_id)`,
124
+ /*
125
+ * **The project, for a database that predates it (0260).** Same shape as the
126
+ * tree store's above: the column is additive and defaulted, then the key
127
+ * widens, then the indexes are rebuilt to lead with it.
128
+ *
129
+ * The indexes are dropped by name rather than created `IF NOT EXISTS`,
130
+ * because the names do not change — an existing index on the old columns
131
+ * would be left exactly as it was, and every read of this table would go on
132
+ * scanning across projects while appearing to be indexed.
133
+ */
134
+ `ALTER TABLE loom_holds ADD COLUMN IF NOT EXISTS application_id text NOT NULL DEFAULT 'app_default'`,
135
+ `ALTER TABLE loom_holds DROP CONSTRAINT IF EXISTS loom_holds_pkey`,
136
+ `ALTER TABLE loom_holds ADD PRIMARY KEY (application_id, proposal_id)`,
137
+ `DROP INDEX IF EXISTS loom_holds_tree_id_held_at_idx`,
138
+ `DROP INDEX IF EXISTS loom_holds_held_at_proposal_id_idx`,
139
+ `CREATE INDEX IF NOT EXISTS loom_holds_tree_id_held_at_idx ON loom_holds (application_id, tree_id, held_at)`,
140
+ `CREATE INDEX IF NOT EXISTS loom_holds_held_at_proposal_id_idx ON loom_holds (application_id, held_at, proposal_id)`,
96
141
  /** Locked on creation for the reason the tree store's tables are — see above. */
97
142
  `ALTER TABLE loom_holds ENABLE ROW LEVEL SECURITY`,
98
143
  ];
144
+ /**
145
+ * The policy log's schema, as statements a host can run.
146
+ *
147
+ * Its own list for the reason the hold store's is: the three are separately
148
+ * useful. A deployment that judges with one policy it has never edited has no
149
+ * reason to carry this table, and one that records policy changes may well keep
150
+ * them beside its trees or somewhere else entirely.
151
+ *
152
+ * The index is not decoration. `judgedUnder` resolves the fingerprint on a
153
+ * disposition into the policy text it was judged under, and a screen showing a
154
+ * page of records asks it once per record; without the index each of those is a
155
+ * scan of every policy revision the deployment has recorded.
156
+ */
157
+ export const POLICY_LOG_DDL = [
158
+ `CREATE TABLE IF NOT EXISTS loom_policy_revisions (
159
+ application_id text NOT NULL DEFAULT 'app_default',
160
+ policy_id text NOT NULL,
161
+ revision integer NOT NULL,
162
+ fingerprint text NOT NULL,
163
+ policy jsonb NOT NULL,
164
+ actor text NOT NULL,
165
+ recorded_at text NOT NULL,
166
+ note text,
167
+ PRIMARY KEY (application_id, policy_id, revision)
168
+ )`,
169
+ /* The project, for a database that predates it — see the hold store's note. */
170
+ `ALTER TABLE loom_policy_revisions ADD COLUMN IF NOT EXISTS application_id text NOT NULL DEFAULT 'app_default'`,
171
+ `ALTER TABLE loom_policy_revisions DROP CONSTRAINT IF EXISTS loom_policy_revisions_pkey`,
172
+ `ALTER TABLE loom_policy_revisions ADD PRIMARY KEY (application_id, policy_id, revision)`,
173
+ `DROP INDEX IF EXISTS loom_policy_revisions_fingerprint_idx`,
174
+ `CREATE INDEX IF NOT EXISTS loom_policy_revisions_fingerprint_idx ON loom_policy_revisions (application_id, policy_id, fingerprint)`,
175
+ /** Locked on creation for the reason the tree store's tables are — see above. */
176
+ `ALTER TABLE loom_policy_revisions ENABLE ROW LEVEL SECURITY`,
177
+ ];
99
178
  /**
100
179
  * Creates the tables if they are absent. Every statement is `IF NOT EXISTS`, so
101
180
  * running it against a populated database is a no-op rather than a hazard.
102
181
  */
182
+ /**
183
+ * The projects table.
184
+ *
185
+ * Carries no `application_id` default and no backfill, unlike every other statement
186
+ * here: the rows that already exist belong to `app_default` by the default on
187
+ * their own columns, and a row *for* `app_default` is seeded by the deployment
188
+ * rather than conjured by DDL — a migration that invented a project's name
189
+ * would be guessing, which 0260 refuses.
190
+ */
191
+ export const APPLICATION_STORE_DDL = [
192
+ /**
193
+ * The rename, first and guarded.
194
+ *
195
+ * `loom_applications` was created as `loom_projects` on 12 October and lived
196
+ * under that name for about an hour, because *project* was the wrong word for
197
+ * the thing with the pages — the maintainer's hierarchy is **account >
198
+ * project > application**, and this is the third. Postgres has no
199
+ * `RENAME … IF EXISTS`, so the catalogue is asked first; a database that never
200
+ * saw the old name skips it, and one that already renamed skips it too.
201
+ *
202
+ * It runs before the `CREATE TABLE IF NOT EXISTS` below rather than after, so
203
+ * a database holding the old table is renamed into the new one instead of
204
+ * ending up with both and the rows in the wrong one.
205
+ */
206
+ `DO $$
207
+ BEGIN
208
+ IF EXISTS (SELECT FROM pg_tables WHERE schemaname = current_schema() AND tablename = 'loom_projects')
209
+ AND EXISTS (SELECT FROM information_schema.columns
210
+ WHERE table_name = 'loom_projects' AND column_name = 'host')
211
+ AND NOT EXISTS (SELECT FROM pg_tables WHERE schemaname = current_schema() AND tablename = 'loom_applications')
212
+ THEN
213
+ ALTER TABLE loom_projects RENAME TO loom_applications;
214
+ ALTER TABLE loom_applications RENAME COLUMN project_id TO application_id;
215
+ END IF;
216
+ END
217
+ $$`,
218
+ `CREATE TABLE IF NOT EXISTS loom_applications (
219
+ application_id text PRIMARY KEY,
220
+ name text NOT NULL,
221
+ created_at text NOT NULL,
222
+ host text,
223
+ closed_at text
224
+ )`,
225
+ /**
226
+ * The grouping an application belongs to. Defaulted so a row written before
227
+ * projects existed keeps answering, which is the device every scoped column
228
+ * in this file uses.
229
+ */
230
+ `ALTER TABLE loom_applications ADD COLUMN IF NOT EXISTS project_id text NOT NULL DEFAULT 'prj_default'`,
231
+ /**
232
+ * As every other table here does. It matters on a managed Postgres whose
233
+ * anon role can reach the schema: without it, the list of a deployment's
234
+ * applications — their names and the hosts they run on — is readable by
235
+ * anyone holding the publishable key.
236
+ */
237
+ /**
238
+ * **Closing, for a database that predates it (0289).** Additive, nullable and
239
+ * undefaulted, which is the only honest value: every row written before this
240
+ * column existed belonged to an application that was open, and still does.
241
+ *
242
+ * This store has no delete, so there is no earlier spelling of *gone* to
243
+ * carry across — nothing was ever removed, which is what makes the backfill
244
+ * no backfill at all.
245
+ */
246
+ `ALTER TABLE loom_applications ADD COLUMN IF NOT EXISTS closed_at text`,
247
+ `ALTER TABLE loom_applications ENABLE ROW LEVEL SECURITY`,
248
+ ];
249
+ /**
250
+ * The projects the applications are grouped under.
251
+ *
252
+ * Its own list rather than a statement in the one above, on the terms this file
253
+ * already sets: a deployment adds a seam by adding its `ensure` call on
254
+ * purpose. They are run together by `db:push` because an application's
255
+ * `project_id` would otherwise point at nothing.
256
+ *
257
+ * **It must run after `ensureApplicationStoreSchema`, not before.** That one
258
+ * renames the `loom_projects` which briefly held applications; this one then
259
+ * creates a fresh table under the freed name. The other order silently loses
260
+ * the grouping table — `CREATE TABLE IF NOT EXISTS` sees the old one, skips,
261
+ * and the rename carries it off — with no error anywhere.
262
+ */
263
+ export const PROJECT_STORE_DDL = [
264
+ `CREATE TABLE IF NOT EXISTS loom_projects (
265
+ project_id text PRIMARY KEY,
266
+ name text NOT NULL,
267
+ created_at text NOT NULL,
268
+ repository text,
269
+ closed_at text
270
+ )`,
271
+ /** The repository a project names, for a database that predates it (0292). */
272
+ `ALTER TABLE loom_projects ADD COLUMN IF NOT EXISTS repository text`,
273
+ /**
274
+ * **At most one project per repository**, enforced here rather than checked
275
+ * before the insert.
276
+ *
277
+ * A check and then an insert is a race two people registering the same
278
+ * repository at the same moment would lose quietly — one of them would take
279
+ * ownership of the other's write-back destination, and nothing afterwards
280
+ * could say which. The index makes it the database's answer, the way the
281
+ * primary key already makes *already taken* the database's answer about an
282
+ * id.
283
+ *
284
+ * Partial, because `NULL` is the ordinary state and a plain unique index
285
+ * would admit exactly one project with no repository.
286
+ */
287
+ `CREATE UNIQUE INDEX IF NOT EXISTS loom_projects_repository_idx ON loom_projects (repository) WHERE repository IS NOT NULL`,
288
+ /** Closing, for a database that predates it — see the applications' note above. */
289
+ `ALTER TABLE loom_projects ADD COLUMN IF NOT EXISTS closed_at text`,
290
+ `ALTER TABLE loom_projects ENABLE ROW LEVEL SECURITY`,
291
+ ];
292
+ /**
293
+ * Moving the ids themselves, which the rename above does not.
294
+ *
295
+ * Every row written before 12 October names `prj_default` in a column that now
296
+ * means an application, and `app_default` is what `DEFAULT_APPLICATION_ID`
297
+ * parses to. Six tables carry it, and each update is idempotent because it
298
+ * matches only the old value.
299
+ *
300
+ * **Each one is guarded on its table existing**, which the first draft was not
301
+ * and a test caught: a deployment adds a seam by adding its `ensure` call on
302
+ * purpose, so one that never switched telemetry or the policy log on has no
303
+ * such table, and an unguarded `UPDATE` took the whole migration down with
304
+ * `42P01` rather than skipping what was not there.
305
+ *
306
+ * Separate from the DDL because it is data: a host reading `db:push` can see
307
+ * exactly which rows are touched, rather than finding an `UPDATE` folded into a
308
+ * list called schema.
309
+ */
310
+ export const APPLICATION_ID_MIGRATION = [
311
+ "loom_trees",
312
+ "loom_revisions",
313
+ "loom_holds",
314
+ "loom_policy_revisions",
315
+ "loom_telemetry",
316
+ "loom_applications",
317
+ ].map((table) => `DO $$
318
+ BEGIN
319
+ IF EXISTS (SELECT FROM pg_tables WHERE schemaname = current_schema() AND tablename = '${table}')
320
+ THEN
321
+ UPDATE ${table} SET application_id = 'app_default' WHERE application_id = 'prj_default';
322
+ END IF;
323
+ END
324
+ $$`);
103
325
  export const ensureTreeStoreSchema = async (db) => {
104
326
  await runStatements(db, TREE_STORE_DDL);
105
327
  };
@@ -114,6 +336,36 @@ export const ensureTreeStoreSchema = async (db) => {
114
336
  export const ensureHoldStoreSchema = async (db) => {
115
337
  await runStatements(db, HOLD_STORE_DDL);
116
338
  };
339
+ /**
340
+ * Creates the policy log's table if it is absent. Idempotent on the same terms,
341
+ * and separate for the same reason: a deployment that wants a record of who
342
+ * changed its acceptance policy gets it by adding this call on purpose.
343
+ */
344
+ export const ensurePolicyLogSchema = async (db) => {
345
+ await runStatements(db, POLICY_LOG_DDL);
346
+ };
347
+ /**
348
+ * Creates the projects table if it is absent. Idempotent, and separate for the
349
+ * same reason the other three are: a deployment that wants more than one
350
+ * project gets the table by adding this call on purpose.
351
+ */
352
+ export const ensureApplicationStoreSchema = async (db) => {
353
+ await runStatements(db, APPLICATION_STORE_DDL);
354
+ };
355
+ /**
356
+ * Creates the projects table if it is absent. Idempotent on the same terms.
357
+ */
358
+ export const ensureProjectStoreSchema = async (db) => {
359
+ await runStatements(db, PROJECT_STORE_DDL);
360
+ };
361
+ /**
362
+ * Carries `prj_default` to `app_default` across every table that names an
363
+ * application. Safe to run on a database that has already been carried, and on
364
+ * one that was never on the old value.
365
+ */
366
+ export const migrateApplicationIds = async (db) => {
367
+ await runStatements(db, APPLICATION_ID_MIGRATION);
368
+ };
117
369
  const runStatements = async (db, statements) => {
118
370
  for (const statement of statements) {
119
371
  await db.execute(sql.raw(statement));
@@ -0,0 +1,4 @@
1
+ import type { ApplicationStore } from "../application/store.js";
2
+ import type { LoomDatabase } from "./database.js";
3
+ export declare const postgresApplicationStore: (db: LoomDatabase) => ApplicationStore;
4
+ //# sourceMappingURL=postgres-applications.d.ts.map