@smthrs/sync 0.0.0-stage → 1.0.0-rc.3

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 (266) hide show
  1. package/CHANGELOG.md +208 -0
  2. package/LICENSE +21 -0
  3. package/README.md +208 -2
  4. package/dist/cjs/BranchCommands.d.ts +228 -0
  5. package/dist/cjs/BranchCommands.d.ts.map +1 -0
  6. package/dist/cjs/BranchCommands.js +316 -0
  7. package/dist/cjs/BranchCommands.js.map +7 -0
  8. package/dist/cjs/BranchIds.d.ts +65 -0
  9. package/dist/cjs/BranchIds.d.ts.map +1 -0
  10. package/dist/cjs/BranchIds.js +53 -0
  11. package/dist/cjs/BranchIds.js.map +7 -0
  12. package/dist/cjs/BranchPresence.d.ts +195 -0
  13. package/dist/cjs/BranchPresence.d.ts.map +1 -0
  14. package/dist/cjs/BranchPresence.js +276 -0
  15. package/dist/cjs/BranchPresence.js.map +7 -0
  16. package/dist/cjs/BranchProjection.d.ts +179 -0
  17. package/dist/cjs/BranchProjection.d.ts.map +1 -0
  18. package/dist/cjs/BranchProjection.js +197 -0
  19. package/dist/cjs/BranchProjection.js.map +7 -0
  20. package/dist/cjs/BranchProtocol.d.ts +415 -0
  21. package/dist/cjs/BranchProtocol.d.ts.map +1 -0
  22. package/dist/cjs/BranchProtocol.js +172 -0
  23. package/dist/cjs/BranchProtocol.js.map +7 -0
  24. package/dist/cjs/BranchRpcs.d.ts +177 -0
  25. package/dist/cjs/BranchRpcs.d.ts.map +1 -0
  26. package/dist/cjs/BranchRpcs.js +85 -0
  27. package/dist/cjs/BranchRpcs.js.map +7 -0
  28. package/dist/cjs/BranchServer.d.ts +55 -0
  29. package/dist/cjs/BranchServer.d.ts.map +1 -0
  30. package/dist/cjs/BranchServer.js +147 -0
  31. package/dist/cjs/BranchServer.js.map +7 -0
  32. package/dist/cjs/BranchShare.d.ts +159 -0
  33. package/dist/cjs/BranchShare.d.ts.map +1 -0
  34. package/dist/cjs/BranchShare.js +147 -0
  35. package/dist/cjs/BranchShare.js.map +7 -0
  36. package/dist/cjs/RunCatalog.d.ts +207 -0
  37. package/dist/cjs/RunCatalog.d.ts.map +1 -0
  38. package/dist/cjs/RunCatalog.js +122 -0
  39. package/dist/cjs/RunCatalog.js.map +7 -0
  40. package/dist/cjs/SyncAuth.d.ts +70 -0
  41. package/dist/cjs/SyncAuth.d.ts.map +1 -0
  42. package/dist/cjs/SyncAuth.js +109 -0
  43. package/dist/cjs/SyncAuth.js.map +7 -0
  44. package/dist/cjs/SyncClient.d.ts +274 -0
  45. package/dist/cjs/SyncClient.d.ts.map +1 -0
  46. package/dist/cjs/SyncClient.js +474 -0
  47. package/dist/cjs/SyncClient.js.map +7 -0
  48. package/dist/cjs/SyncError.d.ts +107 -0
  49. package/dist/cjs/SyncError.d.ts.map +1 -0
  50. package/dist/cjs/SyncError.js +110 -0
  51. package/dist/cjs/SyncError.js.map +7 -0
  52. package/dist/cjs/SyncPrincipal.d.ts +100 -0
  53. package/dist/cjs/SyncPrincipal.d.ts.map +1 -0
  54. package/dist/cjs/SyncPrincipal.js +48 -0
  55. package/dist/cjs/SyncPrincipal.js.map +7 -0
  56. package/dist/cjs/SyncProtocol.d.ts +592 -0
  57. package/dist/cjs/SyncProtocol.d.ts.map +1 -0
  58. package/dist/cjs/SyncProtocol.js +165 -0
  59. package/dist/cjs/SyncProtocol.js.map +7 -0
  60. package/dist/cjs/SyncRpcs.d.ts +92 -0
  61. package/dist/cjs/SyncRpcs.d.ts.map +1 -0
  62. package/dist/cjs/SyncRpcs.js +35 -0
  63. package/dist/cjs/SyncRpcs.js.map +7 -0
  64. package/dist/cjs/SyncServer.d.ts +184 -0
  65. package/dist/cjs/SyncServer.d.ts.map +1 -0
  66. package/dist/cjs/SyncServer.js +513 -0
  67. package/dist/cjs/SyncServer.js.map +7 -0
  68. package/dist/cjs/WorkspaceShare.d.ts +174 -0
  69. package/dist/cjs/WorkspaceShare.d.ts.map +1 -0
  70. package/dist/cjs/WorkspaceShare.js +149 -0
  71. package/dist/cjs/WorkspaceShare.js.map +7 -0
  72. package/dist/cjs/index.d.ts +91 -0
  73. package/dist/cjs/index.d.ts.map +1 -0
  74. package/dist/cjs/index.js +67 -0
  75. package/dist/cjs/index.js.map +7 -0
  76. package/dist/cjs/internal/Admission.d.ts +43 -0
  77. package/dist/cjs/internal/Admission.d.ts.map +1 -0
  78. package/dist/cjs/internal/Admission.js +90 -0
  79. package/dist/cjs/internal/Admission.js.map +7 -0
  80. package/dist/cjs/internal/CauseText.d.ts +55 -0
  81. package/dist/cjs/internal/CauseText.d.ts.map +1 -0
  82. package/dist/cjs/internal/CauseText.js +38 -0
  83. package/dist/cjs/internal/CauseText.js.map +7 -0
  84. package/dist/cjs/internal/JournalErrorCode.d.ts +23 -0
  85. package/dist/cjs/internal/JournalErrorCode.d.ts.map +1 -0
  86. package/dist/cjs/internal/JournalErrorCode.js +34 -0
  87. package/dist/cjs/internal/JournalErrorCode.js.map +7 -0
  88. package/dist/cjs/internal/PolicyOptions.d.ts +57 -0
  89. package/dist/cjs/internal/PolicyOptions.d.ts.map +1 -0
  90. package/dist/cjs/internal/PolicyOptions.js +62 -0
  91. package/dist/cjs/internal/PolicyOptions.js.map +7 -0
  92. package/dist/cjs/internal/ProtocolVersion.d.ts +17 -0
  93. package/dist/cjs/internal/ProtocolVersion.d.ts.map +1 -0
  94. package/dist/cjs/internal/ProtocolVersion.js +44 -0
  95. package/dist/cjs/internal/ProtocolVersion.js.map +7 -0
  96. package/dist/cjs/internal/ShareSigner.d.ts +173 -0
  97. package/dist/cjs/internal/ShareSigner.d.ts.map +1 -0
  98. package/dist/cjs/internal/ShareSigner.js +150 -0
  99. package/dist/cjs/internal/ShareSigner.js.map +7 -0
  100. package/dist/cjs/internal/SnapshotBoundary.d.ts +17 -0
  101. package/dist/cjs/internal/SnapshotBoundary.d.ts.map +1 -0
  102. package/dist/cjs/internal/SnapshotBoundary.js +97 -0
  103. package/dist/cjs/internal/SnapshotBoundary.js.map +7 -0
  104. package/dist/cjs/package.json +1 -0
  105. package/dist/cjs/test/TestSocket.d.ts +55 -0
  106. package/dist/cjs/test/TestSocket.d.ts.map +1 -0
  107. package/dist/cjs/test/TestSocket.js +145 -0
  108. package/dist/cjs/test/TestSocket.js.map +7 -0
  109. package/dist/cjs/test/TestSync.d.ts +54 -0
  110. package/dist/cjs/test/TestSync.d.ts.map +1 -0
  111. package/dist/cjs/test/TestSync.js +96 -0
  112. package/dist/cjs/test/TestSync.js.map +7 -0
  113. package/dist/esm/BranchCommands.d.ts +228 -0
  114. package/dist/esm/BranchCommands.d.ts.map +1 -0
  115. package/dist/esm/BranchCommands.js +496 -0
  116. package/dist/esm/BranchCommands.js.map +1 -0
  117. package/dist/esm/BranchIds.d.ts +65 -0
  118. package/dist/esm/BranchIds.d.ts.map +1 -0
  119. package/dist/esm/BranchIds.js +61 -0
  120. package/dist/esm/BranchIds.js.map +1 -0
  121. package/dist/esm/BranchPresence.d.ts +195 -0
  122. package/dist/esm/BranchPresence.d.ts.map +1 -0
  123. package/dist/esm/BranchPresence.js +361 -0
  124. package/dist/esm/BranchPresence.js.map +1 -0
  125. package/dist/esm/BranchProjection.d.ts +179 -0
  126. package/dist/esm/BranchProjection.d.ts.map +1 -0
  127. package/dist/esm/BranchProjection.js +269 -0
  128. package/dist/esm/BranchProjection.js.map +1 -0
  129. package/dist/esm/BranchProtocol.d.ts +415 -0
  130. package/dist/esm/BranchProtocol.d.ts.map +1 -0
  131. package/dist/esm/BranchProtocol.js +323 -0
  132. package/dist/esm/BranchProtocol.js.map +1 -0
  133. package/dist/esm/BranchRpcs.d.ts +177 -0
  134. package/dist/esm/BranchRpcs.d.ts.map +1 -0
  135. package/dist/esm/BranchRpcs.js +103 -0
  136. package/dist/esm/BranchRpcs.js.map +1 -0
  137. package/dist/esm/BranchServer.d.ts +55 -0
  138. package/dist/esm/BranchServer.d.ts.map +1 -0
  139. package/dist/esm/BranchServer.js +168 -0
  140. package/dist/esm/BranchServer.js.map +1 -0
  141. package/dist/esm/BranchShare.d.ts +159 -0
  142. package/dist/esm/BranchShare.d.ts.map +1 -0
  143. package/dist/esm/BranchShare.js +213 -0
  144. package/dist/esm/BranchShare.js.map +1 -0
  145. package/dist/esm/RunCatalog.d.ts +207 -0
  146. package/dist/esm/RunCatalog.d.ts.map +1 -0
  147. package/dist/esm/RunCatalog.js +196 -0
  148. package/dist/esm/RunCatalog.js.map +1 -0
  149. package/dist/esm/SyncAuth.d.ts +70 -0
  150. package/dist/esm/SyncAuth.d.ts.map +1 -0
  151. package/dist/esm/SyncAuth.js +119 -0
  152. package/dist/esm/SyncAuth.js.map +1 -0
  153. package/dist/esm/SyncClient.d.ts +274 -0
  154. package/dist/esm/SyncClient.d.ts.map +1 -0
  155. package/dist/esm/SyncClient.js +586 -0
  156. package/dist/esm/SyncClient.js.map +1 -0
  157. package/dist/esm/SyncError.d.ts +107 -0
  158. package/dist/esm/SyncError.d.ts.map +1 -0
  159. package/dist/esm/SyncError.js +133 -0
  160. package/dist/esm/SyncError.js.map +1 -0
  161. package/dist/esm/SyncPrincipal.d.ts +100 -0
  162. package/dist/esm/SyncPrincipal.d.ts.map +1 -0
  163. package/dist/esm/SyncPrincipal.js +58 -0
  164. package/dist/esm/SyncPrincipal.js.map +1 -0
  165. package/dist/esm/SyncProtocol.d.ts +592 -0
  166. package/dist/esm/SyncProtocol.d.ts.map +1 -0
  167. package/dist/esm/SyncProtocol.js +434 -0
  168. package/dist/esm/SyncProtocol.js.map +1 -0
  169. package/dist/esm/SyncRpcs.d.ts +92 -0
  170. package/dist/esm/SyncRpcs.d.ts.map +1 -0
  171. package/dist/esm/SyncRpcs.js +35 -0
  172. package/dist/esm/SyncRpcs.js.map +1 -0
  173. package/dist/esm/SyncServer.d.ts +184 -0
  174. package/dist/esm/SyncServer.d.ts.map +1 -0
  175. package/dist/esm/SyncServer.js +897 -0
  176. package/dist/esm/SyncServer.js.map +1 -0
  177. package/dist/esm/WorkspaceShare.d.ts +174 -0
  178. package/dist/esm/WorkspaceShare.d.ts.map +1 -0
  179. package/dist/esm/WorkspaceShare.js +208 -0
  180. package/dist/esm/WorkspaceShare.js.map +1 -0
  181. package/dist/esm/index.d.ts +91 -0
  182. package/dist/esm/index.d.ts.map +1 -0
  183. package/dist/esm/index.js +91 -0
  184. package/dist/esm/index.js.map +1 -0
  185. package/dist/esm/internal/Admission.d.ts +43 -0
  186. package/dist/esm/internal/Admission.d.ts.map +1 -0
  187. package/dist/esm/internal/Admission.js +80 -0
  188. package/dist/esm/internal/Admission.js.map +1 -0
  189. package/dist/esm/internal/CauseText.d.ts +55 -0
  190. package/dist/esm/internal/CauseText.d.ts.map +1 -0
  191. package/dist/esm/internal/CauseText.js +67 -0
  192. package/dist/esm/internal/CauseText.js.map +1 -0
  193. package/dist/esm/internal/JournalErrorCode.d.ts +23 -0
  194. package/dist/esm/internal/JournalErrorCode.d.ts.map +1 -0
  195. package/dist/esm/internal/JournalErrorCode.js +44 -0
  196. package/dist/esm/internal/JournalErrorCode.js.map +1 -0
  197. package/dist/esm/internal/PolicyOptions.d.ts +57 -0
  198. package/dist/esm/internal/PolicyOptions.d.ts.map +1 -0
  199. package/dist/esm/internal/PolicyOptions.js +70 -0
  200. package/dist/esm/internal/PolicyOptions.js.map +1 -0
  201. package/dist/esm/internal/ProtocolVersion.d.ts +17 -0
  202. package/dist/esm/internal/ProtocolVersion.d.ts.map +1 -0
  203. package/dist/esm/internal/ProtocolVersion.js +24 -0
  204. package/dist/esm/internal/ProtocolVersion.js.map +1 -0
  205. package/dist/esm/internal/ShareSigner.d.ts +173 -0
  206. package/dist/esm/internal/ShareSigner.d.ts.map +1 -0
  207. package/dist/esm/internal/ShareSigner.js +219 -0
  208. package/dist/esm/internal/ShareSigner.js.map +1 -0
  209. package/dist/esm/internal/SnapshotBoundary.d.ts +17 -0
  210. package/dist/esm/internal/SnapshotBoundary.d.ts.map +1 -0
  211. package/dist/esm/internal/SnapshotBoundary.js +68 -0
  212. package/dist/esm/internal/SnapshotBoundary.js.map +1 -0
  213. package/dist/esm/test/TestSocket.d.ts +55 -0
  214. package/dist/esm/test/TestSocket.d.ts.map +1 -0
  215. package/dist/esm/test/TestSocket.js +128 -0
  216. package/dist/esm/test/TestSocket.js.map +1 -0
  217. package/dist/esm/test/TestSync.d.ts +54 -0
  218. package/dist/esm/test/TestSync.d.ts.map +1 -0
  219. package/dist/esm/test/TestSync.js +76 -0
  220. package/dist/esm/test/TestSync.js.map +1 -0
  221. package/docs/README.md +156 -0
  222. package/docs/api.md +328 -0
  223. package/docs/concepts/authorization.md +117 -0
  224. package/docs/concepts/branches.md +195 -0
  225. package/docs/concepts/compaction.md +144 -0
  226. package/docs/concepts/replay-then-follow.md +129 -0
  227. package/docs/concepts/scopes-and-cursors.md +135 -0
  228. package/docs/guides/authorize-a-connection.md +174 -0
  229. package/docs/guides/follow-a-run.md +174 -0
  230. package/docs/guides/handle-a-compacted-run.md +158 -0
  231. package/docs/guides/list-workspace-runs.md +120 -0
  232. package/docs/guides/run-a-long-soak.md +80 -0
  233. package/docs/guides/serve-the-read-path.md +146 -0
  234. package/docs/guides/test-a-follower.md +273 -0
  235. package/docs/installation.md +92 -0
  236. package/docs/protocol.md +183 -0
  237. package/docs/quickstart.md +159 -0
  238. package/docs/troubleshooting.md +234 -0
  239. package/package.json +304 -3
  240. package/src/BranchCommands.ts +684 -0
  241. package/src/BranchIds.ts +76 -0
  242. package/src/BranchPresence.ts +512 -0
  243. package/src/BranchProjection.ts +308 -0
  244. package/src/BranchProtocol.ts +411 -0
  245. package/src/BranchRpcs.ts +163 -0
  246. package/src/BranchServer.ts +202 -0
  247. package/src/BranchShare.ts +297 -0
  248. package/src/RunCatalog.ts +308 -0
  249. package/src/SyncAuth.ts +146 -0
  250. package/src/SyncClient.ts +902 -0
  251. package/src/SyncError.ts +147 -0
  252. package/src/SyncPrincipal.ts +115 -0
  253. package/src/SyncProtocol.ts +594 -0
  254. package/src/SyncRpcs.ts +40 -0
  255. package/src/SyncServer.ts +1184 -0
  256. package/src/WorkspaceShare.ts +292 -0
  257. package/src/index.ts +107 -0
  258. package/src/internal/Admission.ts +119 -0
  259. package/src/internal/CauseText.ts +74 -0
  260. package/src/internal/JournalErrorCode.ts +46 -0
  261. package/src/internal/PolicyOptions.ts +97 -0
  262. package/src/internal/ProtocolVersion.ts +28 -0
  263. package/src/internal/ShareSigner.ts +300 -0
  264. package/src/internal/SnapshotBoundary.ts +91 -0
  265. package/src/test/TestSocket.ts +192 -0
  266. package/src/test/TestSync.ts +113 -0
@@ -0,0 +1,61 @@
1
+ /**
2
+ * The source of freshly minted branch and capability identifiers.
3
+ *
4
+ * Minting an id is nondeterminism against the host, so it enters through a
5
+ * port like every other host effect: `BranchServer.layerHandlers` asks
6
+ * this service for the ids it puts on the wire instead of calling a global
7
+ * generator, which is what lets a deterministic composition — a test, a
8
+ * replayed transcript, a deployment that derives ids from its own namespace —
9
+ * supply its own without touching the handlers.
10
+ *
11
+ * @since 0.1.0
12
+ */
13
+ import * as Context from "effect/Context";
14
+ import * as Effect from "effect/Effect";
15
+ import * as Layer from "effect/Layer";
16
+ /**
17
+ * The branch identifier source.
18
+ *
19
+ * @category services
20
+ * @since 0.1.0
21
+ */
22
+ export class BranchIds extends Context.Service()("@smthrs/sync/BranchIds") {
23
+ }
24
+ /**
25
+ * Constructs the default source: Web Crypto UUIDs.
26
+ *
27
+ * Web Crypto is used directly, as it is for the share signature in
28
+ * `BranchShare`, so the same module runs in the browser and on node.
29
+ * A branch id is not itself an authorization — every operation still
30
+ * authorizes through a signed capability — but an unguessable id keeps a
31
+ * branch from being enumerated, which is why the default stays with a
32
+ * cryptographic generator rather than `Random`.
33
+ *
34
+ * @category constructors
35
+ * @since 0.1.0
36
+ */
37
+ export const makeWebCrypto = () => BranchIds.of({ fresh: Effect.sync(() => crypto.randomUUID()) });
38
+ /**
39
+ * Provides {@link makeWebCrypto}.
40
+ *
41
+ * @category layers
42
+ * @since 0.1.0
43
+ */
44
+ export const layer = Layer.sync(BranchIds)(makeWebCrypto);
45
+ /**
46
+ * Provides a deterministic counter, `<prefix>-1`, `<prefix>-2`, and so on.
47
+ * For tests and reproducible transcripts only — the ids are guessable.
48
+ *
49
+ * @category layers
50
+ * @since 0.1.0
51
+ */
52
+ export const layerSequential = (prefix) => Layer.sync(BranchIds)(() => {
53
+ let sequence = 0;
54
+ return BranchIds.of({
55
+ fresh: Effect.sync(() => {
56
+ sequence += 1;
57
+ return `${prefix}-${sequence}`;
58
+ })
59
+ });
60
+ });
61
+ //# sourceMappingURL=BranchIds.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BranchIds.js","sourceRoot":"","sources":["../../src/BranchIds.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAA;AACzC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAA;AAarC;;;;;GAKG;AACH,MAAM,OAAO,SAAU,SAAQ,OAAO,CAAC,OAAO,EAAsB,CAAC,wBAAwB,CAAC;CAAG;AAEjG;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,GAAY,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC,EAAE,CAAC,CAAA;AAE3G;;;;;GAKG;AACH,MAAM,CAAC,MAAM,KAAK,GAA2B,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,aAAa,CAAC,CAAA;AAEjF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,MAAc,EAA0B,EAAE,CACxE,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE;IACzB,IAAI,QAAQ,GAAG,CAAC,CAAA;IAChB,OAAO,SAAS,CAAC,EAAE,CAAC;QAClB,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE;YACtB,QAAQ,IAAI,CAAC,CAAA;YACb,OAAO,GAAG,MAAM,IAAI,QAAQ,EAAE,CAAA;QAChC,CAAC,CAAC;KACH,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA","sourcesContent":["/**\n * The source of freshly minted branch and capability identifiers.\n *\n * Minting an id is nondeterminism against the host, so it enters through a\n * port like every other host effect: `BranchServer.layerHandlers` asks\n * this service for the ids it puts on the wire instead of calling a global\n * generator, which is what lets a deterministic composition — a test, a\n * replayed transcript, a deployment that derives ids from its own namespace —\n * supply its own without touching the handlers.\n *\n * @since 0.1.0\n */\n\nimport * as Context from \"effect/Context\"\nimport * as Effect from \"effect/Effect\"\nimport * as Layer from \"effect/Layer\"\n\n/**\n * Identifier minting operations.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Service {\n /** A fresh, unguessable identifier. Never returns the same value twice. */\n readonly fresh: Effect.Effect<string>\n}\n\n/**\n * The branch identifier source.\n *\n * @category services\n * @since 0.1.0\n */\nexport class BranchIds extends Context.Service<BranchIds, Service>()(\"@smthrs/sync/BranchIds\") {}\n\n/**\n * Constructs the default source: Web Crypto UUIDs.\n *\n * Web Crypto is used directly, as it is for the share signature in\n * `BranchShare`, so the same module runs in the browser and on node.\n * A branch id is not itself an authorization — every operation still\n * authorizes through a signed capability — but an unguessable id keeps a\n * branch from being enumerated, which is why the default stays with a\n * cryptographic generator rather than `Random`.\n *\n * @category constructors\n * @since 0.1.0\n */\nexport const makeWebCrypto = (): Service => BranchIds.of({ fresh: Effect.sync(() => crypto.randomUUID()) })\n\n/**\n * Provides {@link makeWebCrypto}.\n *\n * @category layers\n * @since 0.1.0\n */\nexport const layer: Layer.Layer<BranchIds> = Layer.sync(BranchIds)(makeWebCrypto)\n\n/**\n * Provides a deterministic counter, `<prefix>-1`, `<prefix>-2`, and so on.\n * For tests and reproducible transcripts only — the ids are guessable.\n *\n * @category layers\n * @since 0.1.0\n */\nexport const layerSequential = (prefix: string): Layer.Layer<BranchIds> =>\n Layer.sync(BranchIds)(() => {\n let sequence = 0\n return BranchIds.of({\n fresh: Effect.sync(() => {\n sequence += 1\n return `${prefix}-${sequence}`\n })\n })\n })\n"]}
@@ -0,0 +1,195 @@
1
+ /**
2
+ * Ephemeral presence and cursors for one shared branch.
3
+ *
4
+ * Presence is deliberately NOT journalled. A roster is a lease table: every
5
+ * announcement extends a lease, and a participant that stops announcing —
6
+ * because the tab closed, the network dropped, or the process died — ages out
7
+ * without anyone reporting the disconnect. Writing presence to the journal
8
+ * would make "who was looking at this" part of the durable, replayable history
9
+ * of a run, which is both unbounded and wrong: replaying a branch must not
10
+ * resurrect a stranger's caret.
11
+ *
12
+ * Every operation authorizes through {@link BranchShare}, so a capability for
13
+ * one branch can neither read nor write another branch's roster. A live
14
+ * participant is bound to the capability that announced it: announcing or
15
+ * leaving under that `participantId` with any other capability is refused
16
+ * with `unauthorized` until the lease lapses.
17
+ *
18
+ * @since 0.1.0
19
+ */
20
+ import * as Context from "effect/Context";
21
+ import * as Effect from "effect/Effect";
22
+ import * as Layer from "effect/Layer";
23
+ import * as Stream from "effect/Stream";
24
+ import { type Announcement, type BranchId, type LeaveRequest, Participant, type RosterRequest } from "./BranchProtocol.ts";
25
+ import * as BranchShare from "./BranchShare.ts";
26
+ import { SyncError } from "./SyncError.ts";
27
+ /**
28
+ * Ephemeral branch presence operations.
29
+ *
30
+ * @category models
31
+ * @since 0.1.0
32
+ */
33
+ export interface Service {
34
+ readonly announce: (announcement: Announcement) => Effect.Effect<Participant, SyncError>;
35
+ readonly leave: (request: LeaveRequest) => Effect.Effect<void, SyncError>;
36
+ /**
37
+ * One branch's live roster, as a fresh array of detached participants and
38
+ * cursors. Reading drops expired leases and advances a cross-branch sweep.
39
+ */
40
+ readonly list: (request: RosterRequest) => Effect.Effect<ReadonlyArray<Participant>, SyncError>;
41
+ /**
42
+ * Returns `unknown` for one lease after host startup or while a required
43
+ * heartbeat source is unavailable; otherwise reports `present` or `empty`.
44
+ *
45
+ * @since 1.0.0
46
+ */
47
+ readonly presenceOn: (request: RosterRequest) => Effect.Effect<"unknown" | "present" | "empty", SyncError>;
48
+ readonly changes: Stream.Stream<BranchId>;
49
+ /**
50
+ * How long one announcement keeps a participant on the roster, in
51
+ * milliseconds.
52
+ *
53
+ * A lease lapses without anyone reporting it, and nothing publishes on
54
+ * `changes` when it does, so a watcher cannot learn of the last
55
+ * participant's departure from the change feed alone. It re-lists on this
56
+ * cadence capped at one second, bounding how long an expired lease stays visible.
57
+ */
58
+ readonly leaseMs: number;
59
+ }
60
+ declare const BranchPresence_base: Context.ServiceClass<BranchPresence, "@smthrs/sync/BranchPresence", Service>;
61
+ /**
62
+ * The branch presence registry.
63
+ *
64
+ * @category services
65
+ * @since 0.1.0
66
+ */
67
+ export declare class BranchPresence extends BranchPresence_base {
68
+ }
69
+ /**
70
+ * The lease {@link makeNoop} reports. It holds no one, so the value only has
71
+ * to be a positive number a watcher can build a re-list cadence from.
72
+ *
73
+ * @category constants
74
+ * @since 1.0.0-rc.0
75
+ */
76
+ export declare const defaultLeaseMs = 30000;
77
+ /**
78
+ * Constructs an unavailable presence registry.
79
+ *
80
+ * @category constructors
81
+ * @since 0.1.0
82
+ */
83
+ export declare const makeNoop: (overrides?: Partial<Service>) => Service;
84
+ /**
85
+ * Provides an unavailable presence registry.
86
+ *
87
+ * @category layers
88
+ * @since 0.1.0
89
+ */
90
+ export declare const layerNoop: Layer.Layer<BranchPresence>;
91
+ /**
92
+ * The policy one presence registry runs under. Every field defaults, and
93
+ * {@link makeMemory} and {@link layerWith} validate what a caller supplies.
94
+ *
95
+ * @category models
96
+ * @since 0.1.0
97
+ */
98
+ export interface PresenceOptions {
99
+ /**
100
+ * How long an announcement keeps a participant on the roster, in
101
+ * milliseconds. Defaults to {@link defaultLeaseMs}.
102
+ */
103
+ readonly leaseMs?: number | undefined;
104
+ /**
105
+ * Roster changes a stalled {@link Service.changes} subscriber may fall
106
+ * behind by. Defaults to {@link defaultChangesCapacity}.
107
+ */
108
+ readonly changesCapacity?: number | undefined;
109
+ /**
110
+ * Participants one branch may hold at once. Defaults to
111
+ * {@link defaultMaxParticipants}.
112
+ */
113
+ readonly maxParticipants?: number | undefined;
114
+ /**
115
+ * Host readiness, including authorization, revocation, bridge and session
116
+ * sources. Defaults to unavailable.
117
+ *
118
+ * @since 1.0.0
119
+ */
120
+ readonly sourcesReady?: ((branchId: BranchId) => Effect.Effect<boolean>) | undefined;
121
+ }
122
+ /**
123
+ * Roster changes a stalled {@link Service.changes} subscriber may fall behind
124
+ * by before the oldest are dropped.
125
+ *
126
+ * Presence is a lease table, and `changes` only says that some branch's roster
127
+ * moved: a follower answers it with `list`. Holding an unbounded backlog for a
128
+ * subscriber that has stopped pulling would let one abandoned watcher retain
129
+ * every announcement the process has seen since. A subscriber that falls
130
+ * further behind than this bound loses the oldest notifications and re-lists;
131
+ * because every notification is answered by a fresh `list`, dropping one never
132
+ * loses roster state.
133
+ *
134
+ * @category constants
135
+ * @since 0.1.0
136
+ */
137
+ export declare const defaultChangesCapacity = 256;
138
+ /**
139
+ * Participants one branch may hold at once.
140
+ *
141
+ * A roster is unbounded fan-out in two directions: it is walked on every
142
+ * `list`, and `Branch.Roster` returns it whole, which no frame ceiling covers.
143
+ * Nothing caps how many distinct `participantId`s one write capability may
144
+ * announce, so without this a single share link could pin an arbitrary number
145
+ * of `Participant` objects until their leases expired. Two hundred and fifty
146
+ * six is far above any real collaborative session; a further announce is
147
+ * refused with `backpressure`.
148
+ *
149
+ * @category constants
150
+ * @since 1.0.0-rc.0
151
+ */
152
+ export declare const defaultMaxParticipants = 256;
153
+ /**
154
+ * Constructs the in-memory, lease-expiring presence registry.
155
+ *
156
+ * Announcing requires write access: a read-only share link may watch the
157
+ * roster but never appears on it, so a shared read link cannot be used to
158
+ * impersonate a collaborator. A live participant belongs to the capability
159
+ * that announced it, so another write link can neither overwrite nor evict it.
160
+ * Holders of one link share its `capabilityId` and stay indistinguishable.
161
+ *
162
+ * The roster is keyed by branch and then by participant, so listing one branch
163
+ * costs that branch plus a bounded sweep of other branches, and no two
164
+ * branches can share a slot. One flat key built by concatenation collided
165
+ * for valid branded ids, which let one announcement overwrite another branch's
166
+ * participant.
167
+ *
168
+ * The change feed slides at {@link defaultChangesCapacity}: announcing never
169
+ * waits on a stalled watcher, and never grows the process on its behalf.
170
+ *
171
+ * Fails with `invalid_request` when an option is not a positive safe integer.
172
+ *
173
+ * @category constructors
174
+ * @since 0.1.0
175
+ */
176
+ export declare const makeMemory: (options?: PresenceOptions) => Effect.Effect<Service, SyncError, BranchShare.BranchShare>;
177
+ /**
178
+ * Provides the in-memory, lease-expiring presence registry under the default
179
+ * policy, which is valid by construction and so cannot fail.
180
+ *
181
+ * @category layers
182
+ * @since 0.1.0
183
+ */
184
+ export declare const layer: Layer.Layer<BranchPresence, never, BranchShare.BranchShare>;
185
+ /**
186
+ * Provides the in-memory, lease-expiring presence registry under an explicit
187
+ * policy. Fails with `invalid_request` when an option is not a positive safe
188
+ * integer, so a bad policy fails the composition rather than an announcement.
189
+ *
190
+ * @category layers
191
+ * @since 1.0.0-rc.0
192
+ */
193
+ export declare const layerWith: (options: PresenceOptions) => Layer.Layer<BranchPresence, SyncError, BranchShare.BranchShare>;
194
+ export {};
195
+ //# sourceMappingURL=BranchPresence.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BranchPresence.d.ts","sourceRoot":"","sources":["../../src/BranchPresence.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,361 @@
1
+ /**
2
+ * Ephemeral presence and cursors for one shared branch.
3
+ *
4
+ * Presence is deliberately NOT journalled. A roster is a lease table: every
5
+ * announcement extends a lease, and a participant that stops announcing —
6
+ * because the tab closed, the network dropped, or the process died — ages out
7
+ * without anyone reporting the disconnect. Writing presence to the journal
8
+ * would make "who was looking at this" part of the durable, replayable history
9
+ * of a run, which is both unbounded and wrong: replaying a branch must not
10
+ * resurrect a stranger's caret.
11
+ *
12
+ * Every operation authorizes through {@link BranchShare}, so a capability for
13
+ * one branch can neither read nor write another branch's roster. A live
14
+ * participant is bound to the capability that announced it: announcing or
15
+ * leaving under that `participantId` with any other capability is refused
16
+ * with `unauthorized` until the lease lapses.
17
+ *
18
+ * @since 0.1.0
19
+ */
20
+ import * as Clock from "effect/Clock";
21
+ import * as Context from "effect/Context";
22
+ import * as Effect from "effect/Effect";
23
+ import * as Layer from "effect/Layer";
24
+ import * as PubSub from "effect/PubSub";
25
+ import * as Schema from "effect/Schema";
26
+ import * as Stream from "effect/Stream";
27
+ import { Cursor, Participant, PresenceWhere } from "./BranchProtocol.js";
28
+ import * as BranchShare from "./BranchShare.js";
29
+ import { positiveInt } from "./internal/PolicyOptions.js";
30
+ import { SyncError } from "./SyncError.js";
31
+ /**
32
+ * The branch presence registry.
33
+ *
34
+ * @category services
35
+ * @since 0.1.0
36
+ */
37
+ export class BranchPresence extends Context.Service()("@smthrs/sync/BranchPresence") {
38
+ }
39
+ const unavailable = new SyncError({ code: "unsupported", message: "Branch presence is unavailable" });
40
+ /**
41
+ * The lease {@link makeNoop} reports. It holds no one, so the value only has
42
+ * to be a positive number a watcher can build a re-list cadence from.
43
+ *
44
+ * @category constants
45
+ * @since 1.0.0-rc.0
46
+ */
47
+ export const defaultLeaseMs = 30_000;
48
+ /**
49
+ * Constructs an unavailable presence registry.
50
+ *
51
+ * @category constructors
52
+ * @since 0.1.0
53
+ */
54
+ export const makeNoop = (overrides = {}) => BranchPresence.of({
55
+ announce: () => Effect.fail(unavailable),
56
+ leave: () => Effect.fail(unavailable),
57
+ list: () => Effect.fail(unavailable),
58
+ presenceOn: () => Effect.succeed("unknown"),
59
+ changes: Stream.empty,
60
+ leaseMs: defaultLeaseMs,
61
+ ...overrides
62
+ });
63
+ /**
64
+ * Provides an unavailable presence registry.
65
+ *
66
+ * @category layers
67
+ * @since 0.1.0
68
+ */
69
+ export const layerNoop = Layer.succeed(BranchPresence, makeNoop());
70
+ const heldByAnother = (participantId) => new SyncError({
71
+ code: "unauthorized",
72
+ message: `Participant ${participantId} is held by another share capability`
73
+ });
74
+ /**
75
+ * Roster changes a stalled {@link Service.changes} subscriber may fall behind
76
+ * by before the oldest are dropped.
77
+ *
78
+ * Presence is a lease table, and `changes` only says that some branch's roster
79
+ * moved: a follower answers it with `list`. Holding an unbounded backlog for a
80
+ * subscriber that has stopped pulling would let one abandoned watcher retain
81
+ * every announcement the process has seen since. A subscriber that falls
82
+ * further behind than this bound loses the oldest notifications and re-lists;
83
+ * because every notification is answered by a fresh `list`, dropping one never
84
+ * loses roster state.
85
+ *
86
+ * @category constants
87
+ * @since 0.1.0
88
+ */
89
+ export const defaultChangesCapacity = 256;
90
+ /**
91
+ * Participants one branch may hold at once.
92
+ *
93
+ * A roster is unbounded fan-out in two directions: it is walked on every
94
+ * `list`, and `Branch.Roster` returns it whole, which no frame ceiling covers.
95
+ * Nothing caps how many distinct `participantId`s one write capability may
96
+ * announce, so without this a single share link could pin an arbitrary number
97
+ * of `Participant` objects until their leases expired. Two hundred and fifty
98
+ * six is far above any real collaborative session; a further announce is
99
+ * refused with `backpressure`.
100
+ *
101
+ * @category constants
102
+ * @since 1.0.0-rc.0
103
+ */
104
+ export const defaultMaxParticipants = 256;
105
+ const defaults = {
106
+ leaseMs: defaultLeaseMs,
107
+ changesCapacity: defaultChangesCapacity,
108
+ maxParticipants: defaultMaxParticipants,
109
+ sourcesReady: () => Effect.succeed(false)
110
+ };
111
+ /**
112
+ * Constructs the in-memory, lease-expiring presence registry.
113
+ *
114
+ * Announcing requires write access: a read-only share link may watch the
115
+ * roster but never appears on it, so a shared read link cannot be used to
116
+ * impersonate a collaborator. A live participant belongs to the capability
117
+ * that announced it, so another write link can neither overwrite nor evict it.
118
+ * Holders of one link share its `capabilityId` and stay indistinguishable.
119
+ *
120
+ * The roster is keyed by branch and then by participant, so listing one branch
121
+ * costs that branch plus a bounded sweep of other branches, and no two
122
+ * branches can share a slot. One flat key built by concatenation collided
123
+ * for valid branded ids, which let one announcement overwrite another branch's
124
+ * participant.
125
+ *
126
+ * The change feed slides at {@link defaultChangesCapacity}: announcing never
127
+ * waits on a stalled watcher, and never grows the process on its behalf.
128
+ *
129
+ * Fails with `invalid_request` when an option is not a positive safe integer.
130
+ *
131
+ * @category constructors
132
+ * @since 0.1.0
133
+ */
134
+ export const makeMemory = (options = {}) => Effect.flatMap(Effect.all({
135
+ leaseMs: positiveInt("BranchPresence.PresenceOptions.leaseMs", options.leaseMs, defaults.leaseMs),
136
+ changesCapacity: positiveInt("BranchPresence.PresenceOptions.changesCapacity", options.changesCapacity, defaults.changesCapacity),
137
+ maxParticipants: positiveInt("BranchPresence.PresenceOptions.maxParticipants", options.maxParticipants, defaults.maxParticipants)
138
+ }), (resolved) => makeResolved({ ...resolved, sourcesReady: options.sourcesReady ?? defaults.sourcesReady }));
139
+ /** The registry over an already-validated policy. */
140
+ const makeResolved = ({ changesCapacity, leaseMs, maxParticipants, sourcesReady }) => Effect.gen(function* () {
141
+ const startedAtMs = yield* Clock.currentTimeMillis;
142
+ const share = yield* BranchShare.BranchShare;
143
+ const roster = new Map();
144
+ const changes = yield* PubSub.sliding(changesCapacity);
145
+ const expire = (branchId, nowMs) => {
146
+ const branch = roster.get(branchId);
147
+ if (branch === undefined)
148
+ return undefined;
149
+ for (const [participantId, seat] of branch) {
150
+ if (seat.participant.leaseExpiresAtMs <= nowMs)
151
+ branch.delete(participantId);
152
+ }
153
+ if (branch.size === 0) {
154
+ roster.delete(branchId);
155
+ return undefined;
156
+ }
157
+ return branch;
158
+ };
159
+ // Each announce/list advances through at most 16 branch maps. Their
160
+ // participant counts are capped, so unrelated activity reclaims abandoned
161
+ // rosters without scanning the entire registry in a single request.
162
+ let sweepCursor = roster.keys();
163
+ const sweep = (nowMs) => {
164
+ for (let index = 0; index < 16; index++) {
165
+ const next = sweepCursor.next();
166
+ if (next.done) {
167
+ sweepCursor = roster.keys();
168
+ break;
169
+ }
170
+ expire(next.value, nowMs);
171
+ }
172
+ };
173
+ const seatKey = (request) => JSON.stringify([request.participantId, request.sessionId ?? ""]);
174
+ const copyWhere = (where) => ({ ...where });
175
+ const detach = (participant) => new Participant({
176
+ ...participant,
177
+ ...(participant.where === undefined ? {} : { where: copyWhere(participant.where) }),
178
+ cursor: participant.cursor === null ? null : new Cursor(participant.cursor)
179
+ });
180
+ /**
181
+ * Drop this branch's expired leases and advance cleanup of other branches.
182
+ * Abandoned maps are reclaimed as announce/list calls advance the sweep;
183
+ * an idle registry keeps them until activity resumes. Results are detached
184
+ * from storage, including each participant's cursor.
185
+ */
186
+ const live = (branchId, nowMs) => {
187
+ sweep(nowMs);
188
+ const branch = expire(branchId, nowMs);
189
+ if (branch === undefined)
190
+ return [];
191
+ // Map entries have distinct seat keys, so compared seats cannot be equal.
192
+ return Array.from(branch.values(), (seat) => detach(seat.participant)).sort((left, right) => seatKey(left) < seatKey(right) ? -1 : 1);
193
+ };
194
+ /**
195
+ * Copy a request out of the caller's object before the first await.
196
+ *
197
+ * `share.verify` awaits Web Crypto between authorizing `branchId` and the
198
+ * roster access that follows. The request objects are plain structs the
199
+ * in-process caller still holds, so reading them again after the await let
200
+ * a caller move `branchId` (or the participant identity) onto a branch the
201
+ * capability never authorized. Matches the claim snapshot in `BranchShare`.
202
+ */
203
+ const detachRoster = (request) => ({
204
+ capability: request.capability,
205
+ branchId: request.branchId
206
+ });
207
+ const detachLeave = (request) => ({
208
+ ...detachRoster(request),
209
+ participantId: request.participantId,
210
+ ...(request.sessionId === undefined ? {} : { sessionId: request.sessionId })
211
+ });
212
+ const detachAnnouncement = (request) => {
213
+ const suppliedCursor = request.cursor;
214
+ const cursor = suppliedCursor === null
215
+ ? null
216
+ : typeof suppliedCursor === "object"
217
+ ? { cardId: suppliedCursor.cardId, offset: suppliedCursor.offset }
218
+ : { cardId: undefined, offset: undefined };
219
+ return {
220
+ ...detachLeave(request),
221
+ displayName: request.displayName,
222
+ cursor,
223
+ ...(request.kind === undefined ? {} : { kind: request.kind }),
224
+ ...(request.where === undefined ? {} : { where: copyWhere(request.where) }),
225
+ ...(request.watching === undefined ? {} : { watching: request.watching }),
226
+ ...(request.for_member === undefined ? {} : { for_member: request.for_member }),
227
+ ...(request.agentKind === undefined ? {} : { agentKind: request.agentKind }),
228
+ ...(request.via === undefined ? {} : { via: request.via }),
229
+ ...(request.runId === undefined ? {} : { runId: request.runId })
230
+ };
231
+ };
232
+ const announce = Effect.fn("BranchPresence.announce")(function* (supplied) {
233
+ const detached = detachAnnouncement(supplied);
234
+ yield* Effect.annotateCurrentSpan({
235
+ branchId: detached.branchId,
236
+ participantId: detached.participantId
237
+ });
238
+ const claims = yield* share.verify(detached.capability, {
239
+ branchId: detached.branchId,
240
+ access: "write"
241
+ });
242
+ // The wire schema IS `Announcement`, so a remote caller cannot reach
243
+ // here with an empty name. An in-process caller can, and `Participant`
244
+ // requires a `NonEmptyString`: without this the constructor threw a
245
+ // defect out of an operation whose type promises a `SyncError`.
246
+ if (detached.displayName.length === 0) {
247
+ return yield* Effect.fail(new SyncError({ code: "invalid_request", message: "A participant's display name must not be empty" }));
248
+ }
249
+ if (detached.where !== undefined && !Schema.is(PresenceWhere)(detached.where)) {
250
+ return yield* Effect.fail(new SyncError({ code: "invalid_request", message: "Invalid presence location" }));
251
+ }
252
+ if (detached.sessionId !== undefined && detached.sessionId.length === 0) {
253
+ return yield* Effect.fail(new SyncError({ code: "invalid_request", message: "Invalid presence session" }));
254
+ }
255
+ let cursor = null;
256
+ if (detached.cursor !== null) {
257
+ const { cardId, offset } = detached.cursor;
258
+ if (typeof cardId !== "string" || cardId.length === 0) {
259
+ return yield* Effect.fail(new SyncError({ code: "invalid_request", message: "A cursor's card ID must not be empty" }));
260
+ }
261
+ if (typeof offset !== "number" || !Number.isSafeInteger(offset) || offset < 0) {
262
+ return yield* Effect.fail(new SyncError({ code: "invalid_request", message: "A cursor's offset must be a nonnegative safe integer" }));
263
+ }
264
+ cursor = new Cursor({ cardId, offset });
265
+ }
266
+ const announcement = { ...detached, cursor };
267
+ const nowMs = yield* Clock.currentTimeMillis;
268
+ // Run-out leases are dropped before the cap is judged, so a branch that
269
+ // has simply been busy over time is never refused for a stale roster.
270
+ live(announcement.branchId, nowMs);
271
+ const branch = roster.get(announcement.branchId) ?? new Map();
272
+ const held = branch.get(seatKey(announcement));
273
+ // A session suffix must not bypass the existing participant ownership
274
+ // fence. The host may bind multiple sessions to one actor capability.
275
+ const foreignActor = Array.from(branch.values()).some((seat) => seat.participant.participantId === announcement.participantId && seat.capabilityId !== claims.capabilityId);
276
+ if (foreignActor || (held !== undefined && held.capabilityId !== claims.capabilityId)) {
277
+ return yield* Effect.fail(heldByAnother(announcement.participantId));
278
+ }
279
+ if (held === undefined && branch.size >= maxParticipants) {
280
+ return yield* Effect.fail(new SyncError({
281
+ code: "backpressure",
282
+ message: `Branch ${announcement.branchId} already holds ${maxParticipants} participants`
283
+ }));
284
+ }
285
+ const participant = new Participant({
286
+ branchId: announcement.branchId,
287
+ participantId: announcement.participantId,
288
+ displayName: announcement.displayName,
289
+ cursor: announcement.cursor,
290
+ ...(announcement.sessionId === undefined ? {} : { sessionId: announcement.sessionId }),
291
+ ...(announcement.kind === undefined ? {} : { kind: announcement.kind }),
292
+ ...(announcement.where === undefined ? {} : { where: announcement.where }),
293
+ ...(announcement.watching === undefined ? {} : { watching: announcement.watching }),
294
+ ...(announcement.for_member === undefined ? {} : { for_member: announcement.for_member }),
295
+ ...(announcement.agentKind === undefined ? {} : { agentKind: announcement.agentKind }),
296
+ ...(announcement.via === undefined ? {} : { via: announcement.via }),
297
+ ...(announcement.runId === undefined ? {} : { runId: announcement.runId }),
298
+ leaseExpiresAtMs: nowMs + leaseMs
299
+ });
300
+ branch.set(seatKey(announcement), { participant, capabilityId: claims.capabilityId });
301
+ roster.set(announcement.branchId, branch);
302
+ yield* PubSub.publish(changes, announcement.branchId);
303
+ return detach(participant);
304
+ });
305
+ const leave = Effect.fn("BranchPresence.leave")(function* (supplied) {
306
+ const request = detachLeave(supplied);
307
+ yield* Effect.annotateCurrentSpan({ branchId: request.branchId, participantId: request.participantId });
308
+ const claims = yield* share.verify(request.capability, { branchId: request.branchId, access: "write" });
309
+ const branch = expire(request.branchId, yield* Clock.currentTimeMillis);
310
+ if (branch !== undefined) {
311
+ const keys = request.sessionId === undefined
312
+ ? [...branch].filter(([, seat]) => seat.participant.participantId === request.participantId)
313
+ .map(([key]) => key)
314
+ : [seatKey(request)];
315
+ for (const key of keys) {
316
+ const held = branch.get(key);
317
+ if (held !== undefined && held.capabilityId !== claims.capabilityId) {
318
+ return yield* Effect.fail(heldByAnother(request.participantId));
319
+ }
320
+ }
321
+ for (const key of keys)
322
+ branch.delete(key);
323
+ if (branch.size === 0)
324
+ roster.delete(request.branchId);
325
+ }
326
+ yield* PubSub.publish(changes, request.branchId);
327
+ });
328
+ const list = Effect.fn("BranchPresence.list")(function* (supplied) {
329
+ const request = detachRoster(supplied);
330
+ yield* Effect.annotateCurrentSpan({ branchId: request.branchId });
331
+ yield* share.verify(request.capability, { branchId: request.branchId, access: "read" });
332
+ return live(request.branchId, yield* Clock.currentTimeMillis);
333
+ });
334
+ const presenceOn = Effect.fn("BranchPresence.presenceOn")(function* (supplied) {
335
+ const request = detachRoster(supplied);
336
+ yield* share.verify(request.capability, { branchId: request.branchId, access: "read" });
337
+ const nowMs = yield* Clock.currentTimeMillis;
338
+ if (nowMs - startedAtMs < leaseMs || !(yield* sourcesReady(request.branchId)))
339
+ return "unknown";
340
+ return live(request.branchId, nowMs).length === 0 ? "empty" : "present";
341
+ });
342
+ return BranchPresence.of({ announce, leave, list, presenceOn, changes: Stream.fromPubSub(changes), leaseMs });
343
+ });
344
+ /**
345
+ * Provides the in-memory, lease-expiring presence registry under the default
346
+ * policy, which is valid by construction and so cannot fail.
347
+ *
348
+ * @category layers
349
+ * @since 0.1.0
350
+ */
351
+ export const layer = Layer.effect(BranchPresence, makeResolved(defaults));
352
+ /**
353
+ * Provides the in-memory, lease-expiring presence registry under an explicit
354
+ * policy. Fails with `invalid_request` when an option is not a positive safe
355
+ * integer, so a bad policy fails the composition rather than an announcement.
356
+ *
357
+ * @category layers
358
+ * @since 1.0.0-rc.0
359
+ */
360
+ export const layerWith = (options) => Layer.effect(BranchPresence, makeMemory(options));
361
+ //# sourceMappingURL=BranchPresence.js.map