@agentshouse/mdruntime 0.1.0-c56daf494ecb4 → 0.1.0-c59a5a6bd2f31

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 +19 -16
  2. package/dist/acceptance.d.ts +26 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +384 -0
  5. package/dist/adapter.d.ts +152 -57
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/attached-model.d.ts +7 -10
  8. package/dist/attached-model.d.ts.map +1 -1
  9. package/dist/attached-model.js +40 -41
  10. package/dist/binary.d.ts +0 -5
  11. package/dist/binary.d.ts.map +1 -1
  12. package/dist/binary.js +1 -1
  13. package/dist/bootstrap.d.ts +31 -11
  14. package/dist/bootstrap.d.ts.map +1 -1
  15. package/dist/bootstrap.js +323 -93
  16. package/dist/chain.d.ts +12 -0
  17. package/dist/chain.d.ts.map +1 -0
  18. package/dist/chain.js +64 -0
  19. package/dist/change-set.d.ts +9 -14
  20. package/dist/change-set.d.ts.map +1 -1
  21. package/dist/change-set.js +11 -63
  22. package/dist/change.d.ts +32 -1
  23. package/dist/change.d.ts.map +1 -1
  24. package/dist/change.js +10 -1
  25. package/dist/chat-types.d.ts +3 -0
  26. package/dist/chat-types.d.ts.map +1 -0
  27. package/dist/chat-types.js +2 -0
  28. package/dist/client.d.ts +38 -16
  29. package/dist/client.d.ts.map +1 -1
  30. package/dist/client.js +101 -12
  31. package/dist/collection.d.ts +11 -7
  32. package/dist/collection.d.ts.map +1 -1
  33. package/dist/collection.js +38 -79
  34. package/dist/compile.d.ts +16 -0
  35. package/dist/compile.d.ts.map +1 -0
  36. package/dist/compile.js +717 -0
  37. package/dist/connection.d.ts +44 -39
  38. package/dist/connection.d.ts.map +1 -1
  39. package/dist/connection.js +2307 -1712
  40. package/dist/context.d.ts +2 -0
  41. package/dist/context.d.ts.map +1 -0
  42. package/dist/{chat/context.js → context.js} +1 -4
  43. package/dist/conversation-row.d.ts +33 -0
  44. package/dist/conversation-row.d.ts.map +1 -0
  45. package/dist/conversation-row.js +47 -0
  46. package/dist/conversation.d.ts +50 -20
  47. package/dist/conversation.d.ts.map +1 -1
  48. package/dist/conversation.js +897 -303
  49. package/dist/digest.d.ts +7 -0
  50. package/dist/digest.d.ts.map +1 -0
  51. package/dist/digest.js +113 -0
  52. package/dist/durability.d.ts +10 -1
  53. package/dist/durability.d.ts.map +1 -1
  54. package/dist/durability.js +5 -0
  55. package/dist/entry.d.ts +7 -4
  56. package/dist/entry.d.ts.map +1 -1
  57. package/dist/entry.js +33 -28
  58. package/dist/figures.d.ts +48 -0
  59. package/dist/figures.d.ts.map +1 -0
  60. package/dist/figures.js +197 -0
  61. package/dist/file.d.ts +2 -19
  62. package/dist/file.d.ts.map +1 -1
  63. package/dist/file.js +7 -122
  64. package/dist/host.d.ts +11 -2
  65. package/dist/host.d.ts.map +1 -1
  66. package/dist/host.js +127 -17
  67. package/dist/index.d.ts +27 -11
  68. package/dist/index.d.ts.map +1 -1
  69. package/dist/index.js +10 -0
  70. package/dist/install.d.ts +86 -0
  71. package/dist/install.d.ts.map +1 -0
  72. package/dist/install.js +537 -0
  73. package/dist/job.d.ts +33 -0
  74. package/dist/job.d.ts.map +1 -0
  75. package/dist/job.js +54 -0
  76. package/dist/live.d.ts +23 -0
  77. package/dist/live.d.ts.map +1 -0
  78. package/dist/live.js +165 -0
  79. package/dist/log.d.ts +1 -0
  80. package/dist/log.d.ts.map +1 -1
  81. package/dist/media.d.ts +8 -0
  82. package/dist/media.d.ts.map +1 -0
  83. package/dist/media.js +218 -0
  84. package/dist/memory.d.ts +19 -5
  85. package/dist/memory.d.ts.map +1 -1
  86. package/dist/memory.js +253 -94
  87. package/dist/messages.d.ts +52 -0
  88. package/dist/messages.d.ts.map +1 -0
  89. package/dist/messages.js +168 -0
  90. package/dist/model.d.ts +11 -11
  91. package/dist/model.d.ts.map +1 -1
  92. package/dist/model.js +7 -10
  93. package/dist/name.d.ts +4 -0
  94. package/dist/name.d.ts.map +1 -0
  95. package/dist/name.js +122 -0
  96. package/dist/observation.d.ts +22 -4
  97. package/dist/observation.d.ts.map +1 -1
  98. package/dist/observation.js +120 -14
  99. package/dist/opened.d.ts +22 -0
  100. package/dist/opened.d.ts.map +1 -0
  101. package/dist/opened.js +107 -0
  102. package/dist/outcome.d.ts +50 -7
  103. package/dist/outcome.d.ts.map +1 -1
  104. package/dist/outcome.js +43 -0
  105. package/dist/overlay.d.ts +4 -4
  106. package/dist/overlay.d.ts.map +1 -1
  107. package/dist/overlay.js +111 -182
  108. package/dist/{chat/parts.d.ts → parts.d.ts} +11 -9
  109. package/dist/parts.d.ts.map +1 -0
  110. package/dist/{chat/parts.js → parts.js} +45 -46
  111. package/dist/paths.d.ts +1 -0
  112. package/dist/paths.d.ts.map +1 -1
  113. package/dist/paths.js +4 -0
  114. package/dist/query.d.ts +50 -183
  115. package/dist/query.d.ts.map +1 -1
  116. package/dist/query.js +3 -464
  117. package/dist/{chat/questions.d.ts → questions.d.ts} +3 -2
  118. package/dist/questions.d.ts.map +1 -0
  119. package/dist/{chat/questions.js → questions.js} +8 -1
  120. package/dist/reach.d.ts +19 -7
  121. package/dist/reach.d.ts.map +1 -1
  122. package/dist/reach.js +289 -89
  123. package/dist/react.d.ts +11 -6
  124. package/dist/react.d.ts.map +1 -1
  125. package/dist/react.js +7 -17
  126. package/dist/{chat/read.d.ts → read.d.ts} +2 -0
  127. package/dist/read.d.ts.map +1 -0
  128. package/dist/{chat/read.js → read.js} +4 -0
  129. package/dist/readiness.d.ts +50 -0
  130. package/dist/readiness.d.ts.map +1 -0
  131. package/dist/readiness.js +27 -0
  132. package/dist/reads.d.ts +35 -0
  133. package/dist/reads.d.ts.map +1 -0
  134. package/dist/reads.js +128 -0
  135. package/dist/recovery.d.ts +26 -0
  136. package/dist/recovery.d.ts.map +1 -0
  137. package/dist/recovery.js +232 -0
  138. package/dist/refresh.d.ts +17 -14
  139. package/dist/refresh.d.ts.map +1 -1
  140. package/dist/refresh.js +143 -51
  141. package/dist/replica.d.ts +55 -33
  142. package/dist/replica.d.ts.map +1 -1
  143. package/dist/replica.js +332 -154
  144. package/dist/room-draft.d.ts +37 -6
  145. package/dist/room-draft.d.ts.map +1 -1
  146. package/dist/room-draft.js +161 -74
  147. package/dist/room-drafts.d.ts +29 -0
  148. package/dist/room-drafts.d.ts.map +1 -0
  149. package/dist/room-drafts.js +216 -0
  150. package/dist/rows.d.ts +30 -37
  151. package/dist/rows.d.ts.map +1 -1
  152. package/dist/rows.js +59 -126
  153. package/dist/save.d.ts +53 -9
  154. package/dist/save.d.ts.map +1 -1
  155. package/dist/save.js +542 -172
  156. package/dist/saves.d.ts +96 -0
  157. package/dist/saves.d.ts.map +1 -0
  158. package/dist/saves.js +999 -0
  159. package/dist/search.d.ts +15 -55
  160. package/dist/search.d.ts.map +1 -1
  161. package/dist/search.js +10 -121
  162. package/dist/seed.d.ts +30 -0
  163. package/dist/seed.d.ts.map +1 -0
  164. package/dist/seed.js +141 -0
  165. package/dist/shared.d.ts +15 -0
  166. package/dist/shared.d.ts.map +1 -0
  167. package/dist/shared.js +421 -0
  168. package/dist/socket/admission.d.ts +7 -0
  169. package/dist/socket/admission.d.ts.map +1 -0
  170. package/dist/socket/admission.js +39 -0
  171. package/dist/socket/answers.d.ts +6 -0
  172. package/dist/socket/answers.d.ts.map +1 -0
  173. package/dist/socket/answers.js +20 -0
  174. package/dist/socket/reads.d.ts +10 -0
  175. package/dist/socket/reads.d.ts.map +1 -0
  176. package/dist/socket/reads.js +31 -0
  177. package/dist/socket.d.ts +43 -0
  178. package/dist/socket.d.ts.map +1 -0
  179. package/dist/socket.js +96 -0
  180. package/dist/source.d.ts +61 -0
  181. package/dist/source.d.ts.map +1 -0
  182. package/dist/source.js +81 -0
  183. package/dist/status.d.ts +5 -2
  184. package/dist/status.d.ts.map +1 -1
  185. package/dist/status.js +3 -7
  186. package/dist/store/fences.d.ts +16 -0
  187. package/dist/store/fences.d.ts.map +1 -0
  188. package/dist/store/fences.js +26 -0
  189. package/dist/store/installs.d.ts +30 -0
  190. package/dist/store/installs.d.ts.map +1 -0
  191. package/dist/store/installs.js +34 -0
  192. package/dist/store/records.d.ts +212 -0
  193. package/dist/store/records.d.ts.map +1 -0
  194. package/dist/store/records.js +408 -0
  195. package/dist/store/room-held.d.ts +8 -0
  196. package/dist/store/room-held.d.ts.map +1 -0
  197. package/dist/store/room-held.js +17 -0
  198. package/dist/store/watchers.d.ts +15 -0
  199. package/dist/store/watchers.d.ts.map +1 -0
  200. package/dist/store/watchers.js +36 -0
  201. package/dist/store/writes.d.ts +24 -0
  202. package/dist/store/writes.d.ts.map +1 -0
  203. package/dist/store/writes.js +125 -0
  204. package/dist/store.d.ts +77 -120
  205. package/dist/store.d.ts.map +1 -1
  206. package/dist/store.js +112 -529
  207. package/dist/stored.d.ts +156 -0
  208. package/dist/stored.d.ts.map +1 -0
  209. package/dist/stored.js +637 -0
  210. package/dist/tables.d.ts +407 -0
  211. package/dist/tables.d.ts.map +1 -0
  212. package/dist/tables.js +724 -0
  213. package/dist/tallies.d.ts +18 -0
  214. package/dist/tallies.d.ts.map +1 -0
  215. package/dist/tallies.js +129 -0
  216. package/dist/transfer.d.ts +0 -5
  217. package/dist/transfer.d.ts.map +1 -1
  218. package/dist/turn.d.ts +9 -0
  219. package/dist/turn.d.ts.map +1 -0
  220. package/dist/turn.js +75 -0
  221. package/dist/validation.d.ts +29 -0
  222. package/dist/validation.d.ts.map +1 -0
  223. package/dist/validation.js +104 -0
  224. package/dist/windows.d.ts +11 -0
  225. package/dist/windows.d.ts.map +1 -0
  226. package/dist/windows.js +16 -0
  227. package/dist/wire.d.ts +179 -32
  228. package/dist/wire.d.ts.map +1 -1
  229. package/dist/wire.js +61 -31
  230. package/package.json +18 -19
  231. package/AGENTS.md +0 -302
  232. package/dist/chat/acts.d.ts +0 -34
  233. package/dist/chat/acts.d.ts.map +0 -1
  234. package/dist/chat/acts.js +0 -36
  235. package/dist/chat/context.d.ts +0 -3
  236. package/dist/chat/context.d.ts.map +0 -1
  237. package/dist/chat/input.d.ts +0 -9
  238. package/dist/chat/input.d.ts.map +0 -1
  239. package/dist/chat/input.js +0 -99
  240. package/dist/chat/kit.d.ts +0 -60
  241. package/dist/chat/kit.d.ts.map +0 -1
  242. package/dist/chat/kit.js +0 -123
  243. package/dist/chat/lasting.d.ts +0 -6
  244. package/dist/chat/lasting.d.ts.map +0 -1
  245. package/dist/chat/lasting.js +0 -19
  246. package/dist/chat/list.d.ts +0 -26
  247. package/dist/chat/list.d.ts.map +0 -1
  248. package/dist/chat/list.js +0 -62
  249. package/dist/chat/markdown.d.ts +0 -20
  250. package/dist/chat/markdown.d.ts.map +0 -1
  251. package/dist/chat/markdown.js +0 -188
  252. package/dist/chat/message.d.ts +0 -6
  253. package/dist/chat/message.d.ts.map +0 -1
  254. package/dist/chat/message.js +0 -192
  255. package/dist/chat/messages.d.ts +0 -21
  256. package/dist/chat/messages.d.ts.map +0 -1
  257. package/dist/chat/messages.js +0 -151
  258. package/dist/chat/name.d.ts +0 -6
  259. package/dist/chat/name.d.ts.map +0 -1
  260. package/dist/chat/name.js +0 -37
  261. package/dist/chat/opened.d.ts +0 -11
  262. package/dist/chat/opened.d.ts.map +0 -1
  263. package/dist/chat/opened.js +0 -17
  264. package/dist/chat/overlay.d.ts +0 -31
  265. package/dist/chat/overlay.d.ts.map +0 -1
  266. package/dist/chat/overlay.js +0 -97
  267. package/dist/chat/parts.d.ts.map +0 -1
  268. package/dist/chat/process.d.ts +0 -20
  269. package/dist/chat/process.d.ts.map +0 -1
  270. package/dist/chat/process.js +0 -32
  271. package/dist/chat/provider.d.ts +0 -63
  272. package/dist/chat/provider.d.ts.map +0 -1
  273. package/dist/chat/provider.js +0 -645
  274. package/dist/chat/question.d.ts +0 -9
  275. package/dist/chat/question.d.ts.map +0 -1
  276. package/dist/chat/question.js +0 -80
  277. package/dist/chat/questions.d.ts.map +0 -1
  278. package/dist/chat/read.d.ts.map +0 -1
  279. package/dist/chat/ready.d.ts +0 -33
  280. package/dist/chat/ready.d.ts.map +0 -1
  281. package/dist/chat/ready.js +0 -91
  282. package/dist/chat/style.d.ts +0 -4
  283. package/dist/chat/style.d.ts.map +0 -1
  284. package/dist/chat/style.js +0 -302
  285. package/dist/chat.d.ts +0 -14
  286. package/dist/chat.d.ts.map +0 -1
  287. package/dist/chat.js +0 -8
  288. package/dist/read-side.d.ts +0 -18
  289. package/dist/read-side.d.ts.map +0 -1
  290. package/dist/read-side.js +0 -440
package/README.md CHANGED
@@ -12,14 +12,14 @@ npm install @agentshouse/mdruntime
12
12
 
13
13
  ## Entries
14
14
 
15
- | Entry | For |
16
- | ------------------------------- | ---------------------------------------------------------------- |
17
- | `@agentshouse/mdruntime/client` | The host's client: `connectMdRuntime(port)` |
18
- | `@agentshouse/mdruntime/host` | The host that serves a Runtime: `serveMdRuntime` |
19
- | `@agentshouse/mdruntime` | Shared types |
20
- | `@agentshouse/mdruntime/memory` | Tests: `createMemoryAdapter`, an in-memory Adapter |
21
- | `@agentshouse/mdruntime/react` | React over the client: `useObserve`, `useSave`, `saveRoomDrafts` |
22
- | `@agentshouse/mdruntime/chat` | The Chat: `Chat`, `mountChat`, its parts and `ChatProvider` |
15
+ | Entry | For |
16
+ | ------------------------------- | ----------------------------------------------------------------------------------- |
17
+ | `@agentshouse/mdruntime/client` | The host's client: `connectMdRuntime(port)` |
18
+ | `@agentshouse/mdruntime/host` | The host that serves a Runtime: `serveMdRuntime` |
19
+ | `@agentshouse/mdruntime` | Shared types |
20
+ | `@agentshouse/mdruntime/memory` | Tests: `createMemoryAdapter`, an in-memory Adapter |
21
+ | `@agentshouse/mdruntime/tables` | The host's database: the Authored store's and opened files' tables, `answerRuntime` |
22
+ | `@agentshouse/mdruntime/react` | React over the client: `useObserve`, `useSave` |
23
23
 
24
24
  ## Usage
25
25
 
@@ -37,17 +37,20 @@ const notes = await runtime.query({ types: ['note'], select: 'all', limit: 20 })
37
37
  `storage-loss` when unsynchronized Room Drafts and Pending Changes were lost.
38
38
  The host supplies the user-facing wording.
39
39
 
40
- Place the ready chat, themed by CSS custom properties, docked at a third of the
41
- window; outside React, `mountChat(element, { client: runtime })` places the same:
40
+ In House the bridge object the page holds as `window.houseBridge` places the Chat, which
41
+ House serves with the page: install and bundle nothing for it. Mount it in an element,
42
+ themed by CSS custom properties on that element, docked at a third of the window:
42
43
 
43
- ```tsx
44
- import { Chat } from '@agentshouse/mdruntime/chat';
45
-
46
- <Chat client={runtime} width="1/3" showChats style={{ '--chat-accent': '#2a6cf0' }} />;
44
+ ```ts
45
+ const chat = await window.houseBridge.chat.mount(element, { width: '1/3', showChats: true });
46
+ element.style.setProperty('--chat-accent', '#2a6cf0');
47
+ chat.update({ width: 'full', showChats: true });
48
+ chat.unmount();
47
49
  ```
48
50
 
49
- The rules an App's agent follows, including Local Query, Local Search, Room
50
- Draft, Save and the Chat, are in [`AGENTS.md`](AGENTS.md).
51
+ The rules an App's agent follows, including Local Query, search, Room
52
+ Draft, Save and the Chat, are the `apps/` pages of House's Guide, which House's
53
+ `guide/open` opens.
51
54
 
52
55
  ## License
53
56
 
@@ -0,0 +1,26 @@
1
+ import type { Authorization, DiscoveredRoom } from './adapter.js';
2
+ import { type Withheld } from './install.js';
3
+ import type { Recovery } from './recovery.js';
4
+ import { type Saver } from './saves.js';
5
+ import type { CycleProgress } from './status.js';
6
+ import type { RuntimeStore } from './store.js';
7
+ export type Accepting = Saver & {
8
+ readonly recovery: Recovery;
9
+ stopping(): boolean;
10
+ paused(): boolean;
11
+ continuing(): boolean;
12
+ discovered(locator: string): DiscoveredRoom | undefined;
13
+ readable(room: DiscoveredRoom): void;
14
+ unreadable(room: DiscoveredRoom): void;
15
+ advanced(room: DiscoveredRoom, local: RuntimeStore, authorized: Authorization): Promise<Withheld | null>;
16
+ withhold(withheld: Withheld, room: DiscoveredRoom, local: RuntimeStore, progress: CycleProgress): Promise<void>;
17
+ writesHeld(): boolean;
18
+ openSignals(): number;
19
+ haltedSince(opens: number): boolean;
20
+ rest(progress: CycleProgress): Promise<void>;
21
+ statusShown(): Promise<void>;
22
+ };
23
+ export declare function unpair(accepting: Accepting, local: RuntimeStore): Promise<void>;
24
+ export declare function submitLater(accepting: Accepting, locator: string): void;
25
+ export declare function submitAll(accepting: Accepting): Promise<void>;
26
+ //# sourceMappingURL=acceptance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"acceptance.d.ts","sourceRoot":"","sources":["../src/acceptance.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAkB,MAAM,cAAc,CAAC;AAIlF,OAAO,EAAsB,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAEjE,OAAO,KAAK,EAAa,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzD,OAAO,EAUL,KAAK,KAAK,EAEX,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,KAAK,EAAE,YAAY,EAAe,MAAM,YAAY,CAAC;AAI5D,MAAM,MAAM,SAAS,GAAG,KAAK,GAAG;IAC9B,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,IAAI,OAAO,CAAC;IACpB,MAAM,IAAI,OAAO,CAAC;IAClB,UAAU,IAAI,OAAO,CAAC;IACtB,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS,CAAC;IACxD,QAAQ,CAAC,IAAI,EAAE,cAAc,GAAG,IAAI,CAAC;IACrC,UAAU,CAAC,IAAI,EAAE,cAAc,GAAG,IAAI,CAAC;IACvC,QAAQ,CACN,IAAI,EAAE,cAAc,EACpB,KAAK,EAAE,YAAY,EACnB,UAAU,EAAE,aAAa,GACxB,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAAC;IAC5B,QAAQ,CACN,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,cAAc,EACpB,KAAK,EAAE,YAAY,EACnB,QAAQ,EAAE,aAAa,GACtB,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,UAAU,IAAI,OAAO,CAAC;IACtB,WAAW,IAAI,MAAM,CAAC;IACtB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IACpC,IAAI,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7C,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9B,CAAC;AAoOF,wBAAsB,MAAM,CAAC,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAgBrF;AAgOD,wBAAgB,WAAW,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,QAKhE;AA4BD,wBAAsB,SAAS,CAAC,SAAS,EAAE,SAAS,iBAMnD"}
@@ -0,0 +1,384 @@
1
+ import { changeSet } from './change-set.js';
2
+ import { leaving as leavingIn, partnerOf } from './change.js';
3
+ import { runtimeError } from './failure.js';
4
+ import { olderThan, showing } from './install.js';
5
+ import { landed, returnedPaths, returning, slicing, touchedBy } from './replica.js';
6
+ import { batchFile, landedWhole, settle, settlement, sliceAt, together, unpairing, withdrawable, } from './saves.js';
7
+ import { unconfirmed } from './transfer.js';
8
+ import { stepped } from './turn.js';
9
+ function pendingKey(locator, id) {
10
+ return `${locator}\u0000${id}`;
11
+ }
12
+ async function mark(accepting, local, locator, batch, submission, lost) {
13
+ const { store, continuing, publishChangeSet, statusShown } = accepting;
14
+ const partner = partnerOf(batch, locator);
15
+ for (const held of partner === null ? [locator] : [locator, partner]) {
16
+ const { state, room } = await local.pending(held, batch.id);
17
+ if (!continuing() ||
18
+ state === undefined ||
19
+ (JSON.stringify(state.submission) === JSON.stringify(submission) && state.lost === lost)) {
20
+ continue;
21
+ }
22
+ await local.submit(held, batch.id, submission, lost);
23
+ if (store.fences.has(held))
24
+ continue;
25
+ if (room)
26
+ publishChangeSet(changeSet(held, { queue: true }));
27
+ }
28
+ statusShown();
29
+ }
30
+ async function keepUpload({ store, continuing }, local, locator, identifier, upload) {
31
+ if (!continuing())
32
+ throw new Error('connection refused');
33
+ await local.keepUpload(locator, identifier, upload);
34
+ if (store.fences.has(locator))
35
+ throw runtimeError('unknown-room', `Unknown Room ${locator}`);
36
+ if (!continuing())
37
+ throw new Error('connection refused');
38
+ }
39
+ function leaving(batch, result) {
40
+ return {
41
+ batch,
42
+ returned: {
43
+ kind: result.kind,
44
+ code: result.code,
45
+ fields: result.fields,
46
+ retryable: result.retryable,
47
+ },
48
+ };
49
+ }
50
+ function logAccepted({ journal }, locator, submitted, accepted) {
51
+ journal.record(`outcome ${locator} ${submitted.id} accepted ${accepted.roomVersion}` +
52
+ (accepted.author === undefined ? '' : ` author ${accepted.author}`) +
53
+ (accepted.time === undefined ? '' : ` time ${accepted.time}`));
54
+ }
55
+ function settling(acceptance) {
56
+ return acceptance !== null && 'apply' in acceptance;
57
+ }
58
+ function failureOf(acceptance) {
59
+ return settling(acceptance) ? null : acceptance;
60
+ }
61
+ async function acceptance(accepting, room, local, submitted, accepted) {
62
+ const { store, socket } = accepting;
63
+ if (!accepting.continuing())
64
+ return unconfirmed;
65
+ const work = await store.work(room.locator);
66
+ const current = work.replica;
67
+ if (current === undefined || !current.queue.some((batch) => batch.id === submitted.id)) {
68
+ return null;
69
+ }
70
+ const held = current.record.roomVersion;
71
+ logAccepted(accepting, room.locator, submitted, accepted);
72
+ let record = current.record;
73
+ let changes;
74
+ if (!socket.reads.reached(room.locator, held, accepted.roomVersion) &&
75
+ !olderThan(accepting, room.locator, accepted.roomVersion, held)) {
76
+ const paths = [...returnedPaths(submitted, accepted)];
77
+ const confirmedLayer = await stepped(slicing(await local.entries(record.key, 'confirmed', paths), paths));
78
+ const back = await stepped(returning(confirmedLayer, submitted, accepted, accepted.roomVersion, store.composed(record)));
79
+ record = {
80
+ ...record,
81
+ roomVersion: socket.follows(room.locator, held, accepted.roomVersion)
82
+ ? accepted.roomVersion
83
+ : held,
84
+ };
85
+ changes = back;
86
+ }
87
+ const settled = await settlement(accepting, local, room.locator, work, record, current.queue.filter((batch) => batch.id !== submitted.id), { room: true, ...(changes === undefined ? {} : { changes }) });
88
+ const reached = record.roomVersion;
89
+ return {
90
+ ...settled,
91
+ apply(answer) {
92
+ const set = settled.apply(answer);
93
+ showing(accepting, room.locator, accepted.roomVersion, reached);
94
+ accepting.readable(room);
95
+ return set;
96
+ },
97
+ };
98
+ }
99
+ async function adoptedTogether(accepting, local, acceptances) {
100
+ const settlements = acceptances.filter(settling);
101
+ if (settlements.length > 0) {
102
+ const set = await together(accepting, local, settlements);
103
+ void accepting.statusShown();
104
+ void accepting.publishChangeSet(set);
105
+ }
106
+ await unpair(accepting, local);
107
+ }
108
+ async function adopted(accepting, room, local, submitted, accepted) {
109
+ const failed = await accepting.store.exclusive(async () => {
110
+ const adopting = await acceptance(accepting, room, local, submitted, accepted);
111
+ await adoptedTogether(accepting, local, [adopting]);
112
+ return failureOf(adopting);
113
+ });
114
+ return failed === null;
115
+ }
116
+ async function adoptedMove(accepting, room, partner, local, submitted, accepted) {
117
+ const { store } = accepting;
118
+ const other = accepting.discovered(partner);
119
+ const failed = await store.exclusive(async () => {
120
+ const left = await acceptance(accepting, room, local, submitted, accepted);
121
+ const arriving = (await store.replica(partner))?.queue.find((batch) => batch.id === submitted.id);
122
+ const arrived = other === undefined || arriving === undefined || accepted.destination === undefined
123
+ ? null
124
+ : await acceptance(accepting, other, local, arriving, {
125
+ kind: 'accepted',
126
+ ...accepted.destination,
127
+ });
128
+ await adoptedTogether(accepting, local, [left, arrived]);
129
+ return failureOf(left);
130
+ });
131
+ submitLater(accepting, partner);
132
+ return failed === null;
133
+ }
134
+ async function behindIn({ store }, locator, batch) {
135
+ const local = await store.open();
136
+ const [order, { room }] = await Promise.all([
137
+ local.order(locator),
138
+ local.pending(locator, batch.id),
139
+ ]);
140
+ return room ? order.indexOf(batch.id) > 0 : order.length > 0;
141
+ }
142
+ async function unpaired(local, skipped) {
143
+ const { queued, moving } = await local.moving();
144
+ for (const { locator, batch, state } of moving) {
145
+ const partner = partnerOf(batch, locator);
146
+ if (partner === null || skipped.has(pendingKey(locator, batch.id)))
147
+ continue;
148
+ if (queued.get(partner)?.includes(batch.id) === true)
149
+ continue;
150
+ if (!leavingIn(batch, locator) || withdrawable(new Map([[batch.id, state]]))(batch)) {
151
+ return { locator, batch };
152
+ }
153
+ }
154
+ return null;
155
+ }
156
+ export async function unpair(accepting, local) {
157
+ const { continuing, publishChangeSet } = accepting;
158
+ const handled = new Set();
159
+ for (let held = await unpaired(local, handled); held !== null && continuing(); held = await unpaired(local, handled)) {
160
+ const { locator, batch } = held;
161
+ handled.add(pendingKey(locator, batch.id));
162
+ publishChangeSet(await together(accepting, local, [await unpairing(accepting, local, locator, [batch])]));
163
+ }
164
+ if (handled.size === 0)
165
+ return;
166
+ accepting.statusShown();
167
+ }
168
+ async function submitRoom(accepting, room, local, authorized, progress, signal) {
169
+ const { store, socket, journal, continuing, writesHeld, advanced, withhold } = accepting;
170
+ while (continuing() && accepting.authorized() !== undefined && !signal.aborted) {
171
+ const headId = (await local.order(room.locator)).find((id) => store.landed.of(room.locator, id) === undefined);
172
+ if (headId === undefined)
173
+ return 'done';
174
+ const [head, pending] = await Promise.all([
175
+ local.batch(room.locator, headId),
176
+ local.pending(room.locator, headId),
177
+ ]);
178
+ if (head === null || !pending.room)
179
+ return 'done';
180
+ const { state } = pending;
181
+ const partner = partnerOf(head, room.locator);
182
+ if (partner !== null && !leavingIn(head, room.locator)) {
183
+ submitLater(accepting, partner);
184
+ return 'done';
185
+ }
186
+ if (partner !== null && (await behindIn(accepting, partner, head))) {
187
+ progress.complete = false;
188
+ return 'done';
189
+ }
190
+ const { submission, lost: earlier } = state ?? {
191
+ submission: { state: 'waiting' },
192
+ lost: false,
193
+ };
194
+ if (submission.state === 'reset' && Date.now() < Date.parse(submission.reset)) {
195
+ progress.complete = false;
196
+ return { at: Date.parse(submission.reset) };
197
+ }
198
+ if (writesHeld()) {
199
+ progress.complete = false;
200
+ journal.record(`submission ${room.locator} ${head.id} held`);
201
+ return 'done';
202
+ }
203
+ const lost = earlier || submission.state === 'submitted';
204
+ let result;
205
+ journal.record(`submission ${room.locator} ${head.id}`);
206
+ await store.exclusive(() => mark(accepting, local, room.locator, head, { state: 'submitted' }, lost));
207
+ if (accepting.paused() || store.fences.has(room.locator))
208
+ return 'done';
209
+ if (signal.aborted || writesHeld()) {
210
+ await store.exclusive(() => mark(accepting, local, room.locator, head, submission, earlier));
211
+ if (signal.aborted)
212
+ return 'done';
213
+ progress.complete = false;
214
+ journal.record(`submission ${room.locator} ${head.id} held`);
215
+ return 'done';
216
+ }
217
+ const opensBefore = accepting.openSignals();
218
+ const answeredHere = socket.awaitingAnswer(room.locator);
219
+ const answeredThere = partner === null ? () => { } : socket.awaitingAnswer(partner);
220
+ const answered = () => {
221
+ answeredHere();
222
+ answeredThere();
223
+ };
224
+ try {
225
+ result = await socket.mutate(authorized, {
226
+ locator: room.locator,
227
+ changeBatch: head,
228
+ file: batchFile(store, head),
229
+ ...(state?.upload === undefined ? {} : { upload: state.upload }),
230
+ }, (upload) => store.exclusive(() => keepUpload(accepting, local, room.locator, head.id, upload)));
231
+ }
232
+ catch {
233
+ answered();
234
+ progress.complete = false;
235
+ if (continuing())
236
+ progress.reachable = false;
237
+ return 'again';
238
+ }
239
+ if (signal.aborted) {
240
+ answered();
241
+ return 'done';
242
+ }
243
+ if (result.kind !== 'accepted' || !continuing())
244
+ answered();
245
+ if (!continuing())
246
+ return 'done';
247
+ if (result.kind === 'transport-uncertainty') {
248
+ progress.complete = false;
249
+ progress.reachable = false;
250
+ return 'again';
251
+ }
252
+ if (result.kind === 'room-not-readable') {
253
+ progress.complete = false;
254
+ accepting.unreadable(accepting.discovered(result.locator) ?? room);
255
+ return 'done';
256
+ }
257
+ if (result.kind === 'revoked-authorization') {
258
+ await accepting.refuse('revoked-authorization');
259
+ return 'done';
260
+ }
261
+ if (result.kind === 'scoped-refusal') {
262
+ await accepting.scoped(result, room, local);
263
+ progress.complete = false;
264
+ return 'done';
265
+ }
266
+ if (result.kind === 'write-halted') {
267
+ journal.record(`outcome ${room.locator} ${head.id} write-halted`);
268
+ await store.exclusive(() => mark(accepting, local, room.locator, head, { state: 'waiting' }, lost));
269
+ if (!accepting.haltedSince(opensBefore))
270
+ continue;
271
+ progress.complete = false;
272
+ return 'done';
273
+ }
274
+ const other = partner === null ? undefined : accepting.discovered(partner);
275
+ if (result.kind === 'conflicted' ||
276
+ result.kind === 'rejected' ||
277
+ result.kind === 'retryable-refusal') {
278
+ journal.record(`outcome ${room.locator} ${head.id} ${result.kind}` +
279
+ (result.kind === 'conflicted' && result.roomVersion !== undefined
280
+ ? ` ${result.roomVersion}`
281
+ : ''));
282
+ journal.record(`refusal ${result.kind} ${result.code}`);
283
+ }
284
+ if (result.kind === 'retryable-refusal') {
285
+ const reset = result.reset;
286
+ progress.complete = false;
287
+ await store.exclusive(() => mark(accepting, local, room.locator, head, reset === undefined ? { state: 'waiting' } : { state: 'reset', reset }, lost));
288
+ return reset === undefined ? 'again' : { at: Date.parse(reset) };
289
+ }
290
+ if (result.kind === 'conflicted' || result.kind === 'rejected') {
291
+ const left = leaving(head, result);
292
+ const refreshed = async () => ({
293
+ there: other === undefined ? null : await advanced(other, local, authorized),
294
+ here: await advanced(room, local, authorized),
295
+ });
296
+ const judged = result.kind === 'conflicted' && other !== undefined ? await refreshed() : null;
297
+ if (judged !== null && (judged.there !== null || judged.here !== null)) {
298
+ await store.exclusive(() => mark(accepting, local, room.locator, head, { state: 'waiting' }, lost));
299
+ if (other !== undefined && judged.there !== null) {
300
+ await withhold(judged.there, other, local, progress);
301
+ }
302
+ if (judged.here !== null)
303
+ await withhold(judged.here, room, local, progress);
304
+ return 'done';
305
+ }
306
+ await store.exclusive(async () => {
307
+ if (!continuing())
308
+ return;
309
+ const work = await store.work(room.locator);
310
+ const current = work.replica;
311
+ if (current === undefined || !current.queue.some((batch) => batch.id === head.id)) {
312
+ return;
313
+ }
314
+ const confirmedLayer = await sliceAt(store, current.record, 'confirmed', [head]);
315
+ const held = result.kind === 'conflicted' &&
316
+ (await landedWhole(store, room.locator, head, confirmedLayer, result.affected));
317
+ if (held &&
318
+ result.kind === 'conflicted' &&
319
+ partner === null &&
320
+ !landed(head, confirmedLayer)) {
321
+ store.landed.hold(room.locator, head.id, Object.fromEntries([...touchedBy([head])].map((path) => [path, confirmedLayer.revisions[path]])));
322
+ return;
323
+ }
324
+ accepting.publishChangeSet(await settle(accepting, local, room.locator, work, current.record, current.queue.filter((batch) => batch.id !== head.id), { leaving: [held ? { ...left, returned: null } : left] }));
325
+ await unpair(accepting, local);
326
+ accepting.statusShown();
327
+ });
328
+ const { there, here } = judged ?? (await refreshed());
329
+ if (other !== undefined && there !== null)
330
+ await withhold(there, other, local, progress);
331
+ if (here !== null) {
332
+ await withhold(here, room, local, progress);
333
+ return 'done';
334
+ }
335
+ continue;
336
+ }
337
+ const kept = await (partner === null
338
+ ? adopted(accepting, room, local, head, result)
339
+ : adoptedMove(accepting, room, partner, local, head, result)).finally(answered);
340
+ if (!kept) {
341
+ progress.complete = false;
342
+ return 'done';
343
+ }
344
+ }
345
+ return 'done';
346
+ }
347
+ export function submitLater(accepting, locator) {
348
+ if (accepting.localSaves)
349
+ return;
350
+ void accepting.recovery.attempt(`submit ${locator}`, (signal) => submitPass(accepting, locator, signal));
351
+ }
352
+ async function submitPass(accepting, locator, signal) {
353
+ const { store, socket } = accepting;
354
+ const room = accepting.discovered(locator);
355
+ if (accepting.closed() || accepting.stopping() || accepting.paused() || room === undefined) {
356
+ return 'done';
357
+ }
358
+ const progress = { complete: true, reachable: true };
359
+ let next;
360
+ try {
361
+ await accepting.ensureAuthorization();
362
+ const authorization = accepting.authorized();
363
+ if (authorization === undefined)
364
+ return 'done';
365
+ next = await submitRoom(accepting, room, await store.open(), authorization, progress, signal);
366
+ }
367
+ catch {
368
+ progress.reachable = false;
369
+ next = 'again';
370
+ }
371
+ if (signal.aborted)
372
+ return 'done';
373
+ await accepting.rest(progress);
374
+ return next === 'again' && !socket.isOpen() ? 'done' : next;
375
+ }
376
+ export async function submitAll(accepting) {
377
+ const { store } = accepting;
378
+ if (accepting.localSaves)
379
+ return;
380
+ for (const locator of await (await store.open()).queued()) {
381
+ if (!store.fences.has(locator))
382
+ submitLater(accepting, locator);
383
+ }
384
+ }