@agentshouse/mdruntime 0.1.0-c08b314dc5105 → 0.1.0-c091c0855b688

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 (304) hide show
  1. package/README.md +10 -10
  2. package/dist/acceptance.d.ts +3 -0
  3. package/dist/acceptance.d.ts.map +1 -0
  4. package/dist/acceptance.js +309 -0
  5. package/dist/actions.d.ts +20 -0
  6. package/dist/actions.d.ts.map +1 -0
  7. package/dist/actions.js +251 -0
  8. package/dist/adapter.d.ts +99 -107
  9. package/dist/adapter.d.ts.map +1 -1
  10. package/dist/attached-model.d.ts +3 -10
  11. package/dist/attached-model.d.ts.map +1 -1
  12. package/dist/attached-model.js +29 -47
  13. package/dist/bootstrap.d.ts +15 -18
  14. package/dist/bootstrap.d.ts.map +1 -1
  15. package/dist/bootstrap.js +252 -158
  16. package/dist/cell.d.ts +6 -0
  17. package/dist/cell.d.ts.map +1 -0
  18. package/dist/cell.js +9 -0
  19. package/dist/chain.d.ts +2 -1
  20. package/dist/chain.d.ts.map +1 -1
  21. package/dist/change-set.d.ts +4 -0
  22. package/dist/change-set.d.ts.map +1 -1
  23. package/dist/change-set.js +6 -0
  24. package/dist/change.d.ts +1 -0
  25. package/dist/change.d.ts.map +1 -1
  26. package/dist/change.js +4 -2
  27. package/dist/chat-types.d.ts +5 -0
  28. package/dist/chat-types.d.ts.map +1 -0
  29. package/dist/chat-types.js +13 -0
  30. package/dist/client.d.ts +24 -7
  31. package/dist/client.d.ts.map +1 -1
  32. package/dist/client.js +9 -5
  33. package/dist/collection.d.ts +23 -7
  34. package/dist/collection.d.ts.map +1 -1
  35. package/dist/collection.js +13 -19
  36. package/dist/compile.d.ts +6 -6
  37. package/dist/compile.d.ts.map +1 -1
  38. package/dist/compile.js +272 -326
  39. package/dist/connection.d.ts +5 -143
  40. package/dist/connection.d.ts.map +1 -1
  41. package/dist/connection.js +101 -5101
  42. package/dist/context.d.ts +2 -0
  43. package/dist/context.d.ts.map +1 -0
  44. package/dist/context.js +7 -0
  45. package/dist/conversation-live.d.ts +135 -0
  46. package/dist/conversation-live.d.ts.map +1 -0
  47. package/dist/conversation-live.js +247 -0
  48. package/dist/conversation-row.d.ts +33 -0
  49. package/dist/conversation-row.d.ts.map +1 -0
  50. package/dist/conversation-row.js +53 -0
  51. package/dist/conversation.d.ts +41 -45
  52. package/dist/conversation.d.ts.map +1 -1
  53. package/dist/conversation.js +885 -822
  54. package/dist/declaration.d.ts +10 -0
  55. package/dist/declaration.d.ts.map +1 -0
  56. package/dist/declaration.js +24 -0
  57. package/dist/discovery.d.ts +10 -0
  58. package/dist/discovery.d.ts.map +1 -0
  59. package/dist/discovery.js +177 -0
  60. package/dist/durability.d.ts +15 -1
  61. package/dist/durability.d.ts.map +1 -1
  62. package/dist/entry.d.ts +3 -2
  63. package/dist/entry.d.ts.map +1 -1
  64. package/dist/entry.js +18 -8
  65. package/dist/expectation.d.ts +4 -2
  66. package/dist/expectation.d.ts.map +1 -1
  67. package/dist/expectation.js +33 -11
  68. package/dist/failure.d.ts +1 -0
  69. package/dist/failure.d.ts.map +1 -1
  70. package/dist/failure.js +3 -0
  71. package/dist/figures.d.ts +49 -0
  72. package/dist/figures.d.ts.map +1 -0
  73. package/dist/figures.js +197 -0
  74. package/dist/holdings.d.ts +29 -0
  75. package/dist/holdings.d.ts.map +1 -0
  76. package/dist/holdings.js +95 -0
  77. package/dist/host.d.ts +20 -8
  78. package/dist/host.d.ts.map +1 -1
  79. package/dist/host.js +44 -34
  80. package/dist/identity.d.ts +0 -1
  81. package/dist/identity.d.ts.map +1 -1
  82. package/dist/identity.js +0 -3
  83. package/dist/index.d.ts +27 -8
  84. package/dist/index.d.ts.map +1 -1
  85. package/dist/index.js +9 -1
  86. package/dist/install.d.ts +55 -0
  87. package/dist/install.d.ts.map +1 -0
  88. package/dist/install.js +543 -0
  89. package/dist/job.d.ts +5 -0
  90. package/dist/job.d.ts.map +1 -0
  91. package/dist/job.js +7 -0
  92. package/dist/lifecycle.d.ts +7 -0
  93. package/dist/lifecycle.d.ts.map +1 -0
  94. package/dist/lifecycle.js +271 -0
  95. package/dist/listing.d.ts +52 -0
  96. package/dist/listing.d.ts.map +1 -0
  97. package/dist/listing.js +168 -0
  98. package/dist/live.d.ts +5 -4
  99. package/dist/live.d.ts.map +1 -1
  100. package/dist/live.js +64 -23
  101. package/dist/locks.d.ts +9 -0
  102. package/dist/locks.d.ts.map +1 -0
  103. package/dist/locks.js +62 -0
  104. package/dist/log.d.ts +1 -0
  105. package/dist/log.d.ts.map +1 -1
  106. package/dist/lost.d.ts +5 -0
  107. package/dist/lost.d.ts.map +1 -0
  108. package/dist/lost.js +61 -0
  109. package/dist/media.d.ts +2 -2
  110. package/dist/media.d.ts.map +1 -1
  111. package/dist/media.js +30 -17
  112. package/dist/memory.d.ts +15 -6
  113. package/dist/memory.d.ts.map +1 -1
  114. package/dist/memory.js +180 -86
  115. package/dist/messages.d.ts +24 -3
  116. package/dist/messages.d.ts.map +1 -1
  117. package/dist/messages.js +89 -31
  118. package/dist/model.d.ts +2 -11
  119. package/dist/model.d.ts.map +1 -1
  120. package/dist/model.js +0 -23
  121. package/dist/models.d.ts +10 -0
  122. package/dist/models.d.ts.map +1 -0
  123. package/dist/models.js +170 -0
  124. package/dist/observation.d.ts +2 -2
  125. package/dist/observation.d.ts.map +1 -1
  126. package/dist/observation.js +14 -14
  127. package/dist/opened.js +3 -3
  128. package/dist/opening.d.ts +4 -0
  129. package/dist/opening.d.ts.map +1 -0
  130. package/dist/opening.js +59 -0
  131. package/dist/outcome.d.ts +4 -4
  132. package/dist/outcome.d.ts.map +1 -1
  133. package/dist/outcome.js +3 -0
  134. package/dist/overlay.d.ts +3 -1
  135. package/dist/overlay.d.ts.map +1 -1
  136. package/dist/overlay.js +43 -21
  137. package/dist/parts.d.ts +11 -0
  138. package/dist/parts.d.ts.map +1 -0
  139. package/dist/parts.js +21 -0
  140. package/dist/paths.d.ts +2 -0
  141. package/dist/paths.d.ts.map +1 -1
  142. package/dist/paths.js +26 -0
  143. package/dist/preparation.d.ts +20 -0
  144. package/dist/preparation.d.ts.map +1 -0
  145. package/dist/preparation.js +233 -0
  146. package/dist/queries.d.ts +16 -0
  147. package/dist/queries.d.ts.map +1 -0
  148. package/dist/queries.js +272 -0
  149. package/dist/query.d.ts +14 -210
  150. package/dist/query.d.ts.map +1 -1
  151. package/dist/query.js +3 -505
  152. package/dist/questions.d.ts +34 -0
  153. package/dist/questions.d.ts.map +1 -0
  154. package/dist/questions.js +122 -0
  155. package/dist/queue.d.ts +39 -0
  156. package/dist/queue.d.ts.map +1 -0
  157. package/dist/queue.js +455 -0
  158. package/dist/reach.d.ts +15 -12
  159. package/dist/reach.d.ts.map +1 -1
  160. package/dist/reach.js +189 -142
  161. package/dist/react.d.ts +14 -2
  162. package/dist/react.d.ts.map +1 -1
  163. package/dist/react.js +10 -1
  164. package/dist/read.d.ts +7 -0
  165. package/dist/read.d.ts.map +1 -0
  166. package/dist/read.js +17 -0
  167. package/dist/readiness.d.ts +3 -18
  168. package/dist/readiness.d.ts.map +1 -1
  169. package/dist/readiness.js +4 -10
  170. package/dist/reads.d.ts +36 -10
  171. package/dist/reads.d.ts.map +1 -1
  172. package/dist/reads.js +159 -85
  173. package/dist/recovery.d.ts +1 -0
  174. package/dist/recovery.d.ts.map +1 -1
  175. package/dist/recovery.js +35 -1
  176. package/dist/refresh.d.ts +7 -5
  177. package/dist/refresh.d.ts.map +1 -1
  178. package/dist/refresh.js +35 -7
  179. package/dist/refusal.d.ts +5 -0
  180. package/dist/refusal.d.ts.map +1 -0
  181. package/dist/refusal.js +57 -0
  182. package/dist/replica.d.ts +12 -18
  183. package/dist/replica.d.ts.map +1 -1
  184. package/dist/replica.js +64 -34
  185. package/dist/room-draft.d.ts +24 -10
  186. package/dist/room-draft.d.ts.map +1 -1
  187. package/dist/room-draft.js +133 -102
  188. package/dist/room-drafts.d.ts +24 -0
  189. package/dist/room-drafts.d.ts.map +1 -0
  190. package/dist/room-drafts.js +217 -0
  191. package/dist/rows.d.ts +14 -21
  192. package/dist/rows.d.ts.map +1 -1
  193. package/dist/rows.js +49 -186
  194. package/dist/runtime.d.ts +32 -0
  195. package/dist/runtime.d.ts.map +1 -0
  196. package/dist/runtime.js +1 -0
  197. package/dist/save.d.ts +41 -84
  198. package/dist/save.d.ts.map +1 -1
  199. package/dist/save.js +249 -120
  200. package/dist/saves.d.ts +32 -0
  201. package/dist/saves.d.ts.map +1 -0
  202. package/dist/saves.js +413 -0
  203. package/dist/search.d.ts +12 -63
  204. package/dist/search.d.ts.map +1 -1
  205. package/dist/search.js +9 -52
  206. package/dist/seed.d.ts +11 -2
  207. package/dist/seed.d.ts.map +1 -1
  208. package/dist/seed.js +71 -1
  209. package/dist/shared.d.ts +1 -7
  210. package/dist/shared.d.ts.map +1 -1
  211. package/dist/shared.js +13 -105
  212. package/dist/shown.d.ts +32 -0
  213. package/dist/shown.d.ts.map +1 -0
  214. package/dist/shown.js +299 -0
  215. package/dist/socket/admission.d.ts +7 -0
  216. package/dist/socket/admission.d.ts.map +1 -0
  217. package/dist/socket/admission.js +39 -0
  218. package/dist/socket/answers.d.ts +6 -0
  219. package/dist/socket/answers.d.ts.map +1 -0
  220. package/dist/socket/answers.js +20 -0
  221. package/dist/socket/lanes.d.ts +20 -0
  222. package/dist/socket/lanes.d.ts.map +1 -0
  223. package/dist/socket/lanes.js +77 -0
  224. package/dist/socket/reads.d.ts +10 -0
  225. package/dist/socket/reads.d.ts.map +1 -0
  226. package/dist/socket/reads.js +31 -0
  227. package/dist/socket/rooms.d.ts +41 -0
  228. package/dist/socket/rooms.d.ts.map +1 -0
  229. package/dist/socket/rooms.js +90 -0
  230. package/dist/socket/session.d.ts +38 -0
  231. package/dist/socket/session.d.ts.map +1 -0
  232. package/dist/socket/session.js +23 -0
  233. package/dist/socket.d.ts +69 -0
  234. package/dist/socket.d.ts.map +1 -0
  235. package/dist/socket.js +175 -0
  236. package/dist/source.d.ts +61 -0
  237. package/dist/source.d.ts.map +1 -0
  238. package/dist/source.js +82 -0
  239. package/dist/status.d.ts +4 -5
  240. package/dist/status.d.ts.map +1 -1
  241. package/dist/status.js +3 -7
  242. package/dist/store/fences.d.ts +16 -0
  243. package/dist/store/fences.d.ts.map +1 -0
  244. package/dist/store/fences.js +26 -0
  245. package/dist/store/installs.d.ts +30 -0
  246. package/dist/store/installs.d.ts.map +1 -0
  247. package/dist/store/installs.js +34 -0
  248. package/dist/store/readiness.d.ts +10 -0
  249. package/dist/store/readiness.d.ts.map +1 -0
  250. package/dist/store/readiness.js +52 -0
  251. package/dist/store/records.d.ts +210 -0
  252. package/dist/store/records.d.ts.map +1 -0
  253. package/dist/store/records.js +428 -0
  254. package/dist/store/room-held.d.ts +8 -0
  255. package/dist/store/room-held.d.ts.map +1 -0
  256. package/dist/store/room-held.js +17 -0
  257. package/dist/store/space.d.ts +11 -0
  258. package/dist/store/space.d.ts.map +1 -0
  259. package/dist/store/space.js +41 -0
  260. package/dist/store/watchers.d.ts +17 -0
  261. package/dist/store/watchers.d.ts.map +1 -0
  262. package/dist/store/watchers.js +56 -0
  263. package/dist/store/writes.d.ts +24 -0
  264. package/dist/store/writes.d.ts.map +1 -0
  265. package/dist/store/writes.js +125 -0
  266. package/dist/store.d.ts +115 -171
  267. package/dist/store.d.ts.map +1 -1
  268. package/dist/store.js +143 -416
  269. package/dist/stored.d.ts +61 -57
  270. package/dist/stored.d.ts.map +1 -1
  271. package/dist/stored.js +312 -234
  272. package/dist/surface.d.ts +88 -0
  273. package/dist/surface.d.ts.map +1 -0
  274. package/dist/surface.js +224 -0
  275. package/dist/sync.d.ts +19 -0
  276. package/dist/sync.d.ts.map +1 -0
  277. package/dist/sync.js +530 -0
  278. package/dist/tables.d.ts +122 -64
  279. package/dist/tables.d.ts.map +1 -1
  280. package/dist/tables.js +218 -105
  281. package/dist/tallies.d.ts +18 -0
  282. package/dist/tallies.d.ts.map +1 -0
  283. package/dist/tallies.js +129 -0
  284. package/dist/transfer.d.ts +0 -5
  285. package/dist/transfer.d.ts.map +1 -1
  286. package/dist/validation.d.ts +3 -4
  287. package/dist/validation.d.ts.map +1 -1
  288. package/dist/validation.js +42 -100
  289. package/dist/windows.d.ts +1 -12
  290. package/dist/windows.d.ts.map +1 -1
  291. package/dist/windows.js +2 -75
  292. package/dist/wire.d.ts +21 -9
  293. package/dist/wire.d.ts.map +1 -1
  294. package/dist/wire.js +10 -4
  295. package/package.json +3 -2
  296. package/dist/fence.d.ts +0 -6
  297. package/dist/fence.d.ts.map +0 -1
  298. package/dist/fence.js +0 -10
  299. package/dist/fulltext.d.ts +0 -27
  300. package/dist/fulltext.d.ts.map +0 -1
  301. package/dist/fulltext.js +0 -368
  302. package/dist/words.d.ts +0 -19
  303. package/dist/words.d.ts.map +0 -1
  304. package/dist/words.js +0 -150
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/tables` | The host's database: the Authored store's and opened files' tables, `answerRuntime` |
22
- | `@agentshouse/mdruntime/react` | React over the client: `useObserve`, `useSave` |
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`; `useReading`, a reader of an expectation store |
23
23
 
24
24
  ## Usage
25
25
 
@@ -48,9 +48,9 @@ chat.update({ width: 'full', showChats: true });
48
48
  chat.unmount();
49
49
  ```
50
50
 
51
- The rules an App's agent follows, including Local Query, Local Search, Room
51
+ The rules an App's agent follows, including Local Query, search, Room
52
52
  Draft, Save and the Chat, are the `apps/` pages of House's Guide, which House's
53
- `open_guide` opens.
53
+ `guide/open` opens.
54
54
 
55
55
  ## License
56
56
 
@@ -0,0 +1,3 @@
1
+ import type { Runtime } from './runtime.js';
2
+ export declare function submitLater(runtime: Runtime, locator: string): void;
3
+ //# 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,OAAO,EAAE,MAAM,cAAc,CAAC;AAiX5C,wBAAgB,WAAW,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,QAK5D"}
@@ -0,0 +1,309 @@
1
+ import { changeSet } from './change-set.js';
2
+ import { declared } from './declaration.js';
3
+ import { leaving as leavingIn, partnerOf } from './change.js';
4
+ import { olderThan, showing } from './install.js';
5
+ import { readable } from './listing.js';
6
+ import { scoped } from './models.js';
7
+ import { answered, settle } from './queue.js';
8
+ import { refuse } from './refusal.js';
9
+ import { returnedPaths, returning, slicing } from './replica.js';
10
+ import { batchFile } from './saves.js';
11
+ import { rest, roomUnreadable, statusShown } from './shown.js';
12
+ import { advanced, withhold } from './sync.js';
13
+ import { unconfirmed } from './transfer.js';
14
+ import { stepped } from './turn.js';
15
+ async function mark(runtime, local, locator, batch, submission, lost) {
16
+ const { store, socket } = runtime;
17
+ const partner = partnerOf(batch, locator);
18
+ for (const held of partner === null ? [locator] : [locator, partner]) {
19
+ const { state, room } = await local.pending(held, batch.id);
20
+ if (!socket.continuing() ||
21
+ state === undefined ||
22
+ (JSON.stringify(state.submission) === JSON.stringify(submission) && state.lost === lost)) {
23
+ continue;
24
+ }
25
+ await local.submit(held, batch.id, submission, lost);
26
+ if (store.fences.has(held))
27
+ continue;
28
+ if (room)
29
+ store.changed(changeSet(held, { queue: true }));
30
+ }
31
+ statusShown(runtime);
32
+ }
33
+ async function keepUpload({ store, socket }, local, locator, identifier, upload) {
34
+ if (!socket.continuing())
35
+ throw new Error();
36
+ await local.keepUpload(locator, identifier, upload);
37
+ if (store.fences.has(locator))
38
+ throw new Error();
39
+ if (!socket.continuing())
40
+ throw new Error();
41
+ }
42
+ function logAccepted({ journal }, locator, submitted, accepted) {
43
+ journal.record(`outcome ${locator} ${submitted.id} accepted ${accepted.roomVersion}` +
44
+ (accepted.author === undefined ? '' : ` author ${accepted.author}`) +
45
+ (accepted.time === undefined ? '' : ` time ${accepted.time}`));
46
+ }
47
+ function restands(adoptions) {
48
+ return adoptions.flatMap((one) => (one === null || !('record' in one) ? [] : [one]));
49
+ }
50
+ async function adoption(runtime, room, current, local, submitted, accepted) {
51
+ const { store, socket } = runtime;
52
+ if (!socket.continuing())
53
+ return unconfirmed;
54
+ if (current === undefined || !current.queue.some((batch) => batch.id === submitted.id)) {
55
+ return null;
56
+ }
57
+ const held = current.record.roomVersion;
58
+ logAccepted(runtime, room.locator, submitted, accepted);
59
+ let record = current.record;
60
+ let adopted;
61
+ if (!socket.reads.reached(room.locator, held, accepted.roomVersion) &&
62
+ !olderThan(runtime, room.locator, accepted.roomVersion, held)) {
63
+ const paths = [...returnedPaths(submitted, accepted)];
64
+ const confirmedLayer = await stepped(slicing(await local.entries(record.key, 'confirmed', paths), paths));
65
+ adopted = await stepped(returning(confirmedLayer, submitted, accepted, accepted.roomVersion, store.composed(record)));
66
+ record = {
67
+ ...record,
68
+ roomVersion: socket.follows(room.locator, held, accepted.roomVersion)
69
+ ? accepted.roomVersion
70
+ : held,
71
+ };
72
+ }
73
+ const reached = record.roomVersion;
74
+ return {
75
+ locator: room.locator,
76
+ record,
77
+ accepted: submitted.id,
78
+ ...(adopted === undefined ? {} : { adopted }),
79
+ after() {
80
+ showing(runtime, room.locator, accepted.roomVersion, reached);
81
+ readable(runtime, room);
82
+ },
83
+ };
84
+ }
85
+ async function adopted(runtime, room, local, submitted, accepted) {
86
+ let kept = true;
87
+ await settle(runtime, [room.locator], async (held) => {
88
+ const adopting = await adoption(runtime, room, held.get(room.locator)?.replica, local, submitted, accepted);
89
+ kept = adopting !== unconfirmed;
90
+ return restands([adopting]);
91
+ });
92
+ return kept;
93
+ }
94
+ async function adoptedMove(runtime, room, partner, local, submitted, accepted) {
95
+ const other = runtime.socket.rooms.discovered(partner);
96
+ let kept = true;
97
+ await settle(runtime, [room.locator, partner], async (held) => {
98
+ const left = await adoption(runtime, room, held.get(room.locator)?.replica, local, submitted, accepted);
99
+ const there = held.get(partner)?.replica;
100
+ const arriving = there?.queue.find((batch) => batch.id === submitted.id);
101
+ const arrived = other === undefined || arriving === undefined || accepted.destination === undefined
102
+ ? null
103
+ : await adoption(runtime, other, there, local, arriving, {
104
+ kind: 'accepted',
105
+ ...accepted.destination,
106
+ });
107
+ kept = left !== unconfirmed;
108
+ return restands([left, arrived]);
109
+ });
110
+ submitLater(runtime, partner);
111
+ return kept;
112
+ }
113
+ async function behindIn({ store }, locator, batch) {
114
+ const local = await store.open();
115
+ const [order, { room }] = await Promise.all([
116
+ local.order(locator),
117
+ local.pending(locator, batch.id),
118
+ ]);
119
+ return room ? order.indexOf(batch.id) > 0 : order.length > 0;
120
+ }
121
+ async function submitRoom(runtime, room, local, authorized, progress, signal) {
122
+ const { store, socket, journal } = runtime;
123
+ while (socket.continuing() &&
124
+ socket.session.now().authorization !== undefined &&
125
+ !signal.aborted) {
126
+ const headId = (await local.order(room.locator)).find((id) => store.landed.of(room.locator, id) === undefined);
127
+ if (headId === undefined)
128
+ return 'done';
129
+ const [head, pending] = await Promise.all([
130
+ local.batch(room.locator, headId),
131
+ local.pending(room.locator, headId),
132
+ ]);
133
+ if (head === null || !pending.room)
134
+ return 'done';
135
+ const { state } = pending;
136
+ const partner = partnerOf(head, room.locator);
137
+ if (partner !== null && !leavingIn(head, room.locator)) {
138
+ submitLater(runtime, partner);
139
+ return 'done';
140
+ }
141
+ if (partner !== null && (await behindIn(runtime, partner, head))) {
142
+ progress.complete = false;
143
+ return 'done';
144
+ }
145
+ const { submission, lost: earlier } = state ?? {
146
+ submission: { state: 'waiting' },
147
+ lost: false,
148
+ };
149
+ if (submission.state === 'reset' && Date.now() < Date.parse(submission.reset)) {
150
+ progress.complete = false;
151
+ return { at: Date.parse(submission.reset) };
152
+ }
153
+ if (socket.writesHeld()) {
154
+ progress.complete = false;
155
+ journal.record(`submission ${room.locator} ${head.id} held`);
156
+ return 'done';
157
+ }
158
+ const lost = earlier || submission.state === 'submitted';
159
+ let result;
160
+ journal.record(`submission ${room.locator} ${head.id}`);
161
+ await store.exclusive(() => mark(runtime, local, room.locator, head, { state: 'submitted' }, lost));
162
+ if (socket.session.now().paused || store.fences.has(room.locator))
163
+ return 'done';
164
+ if (signal.aborted || socket.writesHeld()) {
165
+ await store.exclusive(() => mark(runtime, local, room.locator, head, submission, earlier));
166
+ if (signal.aborted)
167
+ return 'done';
168
+ progress.complete = false;
169
+ journal.record(`submission ${room.locator} ${head.id} held`);
170
+ return 'done';
171
+ }
172
+ const opensBefore = socket.session.now().openSignals;
173
+ const answeredHere = socket.awaitingAnswer(room.locator);
174
+ const answeredThere = partner === null ? () => { } : socket.awaitingAnswer(partner);
175
+ const heard = () => {
176
+ answeredHere();
177
+ answeredThere();
178
+ };
179
+ try {
180
+ result = await socket.mutate(authorized, {
181
+ locator: room.locator,
182
+ changeBatch: head,
183
+ file: batchFile(store, head),
184
+ ...(state?.upload === undefined ? {} : { upload: state.upload }),
185
+ }, (upload) => store.exclusive(() => keepUpload(runtime, local, room.locator, head.id, upload)));
186
+ }
187
+ catch {
188
+ heard();
189
+ progress.complete = false;
190
+ if (socket.continuing())
191
+ progress.reachable = false;
192
+ return 'again';
193
+ }
194
+ if (signal.aborted) {
195
+ heard();
196
+ return 'done';
197
+ }
198
+ if (result.kind !== 'accepted' || !socket.continuing())
199
+ heard();
200
+ if (!socket.continuing())
201
+ return 'done';
202
+ if (result.kind === 'transport-uncertainty') {
203
+ progress.complete = false;
204
+ progress.reachable = false;
205
+ return 'again';
206
+ }
207
+ if (result.kind === 'room-not-readable') {
208
+ progress.complete = false;
209
+ roomUnreadable(runtime, socket.rooms.discovered(result.locator) ?? room);
210
+ return 'done';
211
+ }
212
+ if (result.kind === 'revoked-authorization') {
213
+ await refuse(runtime, 'revoked-authorization');
214
+ return 'done';
215
+ }
216
+ if (result.kind === 'scoped-refusal') {
217
+ await scoped(runtime, result, room.locator, declared(room));
218
+ progress.complete = false;
219
+ return 'done';
220
+ }
221
+ if (result.kind === 'write-halted') {
222
+ journal.record(`outcome ${room.locator} ${head.id} write-halted`);
223
+ await store.exclusive(() => mark(runtime, local, room.locator, head, { state: 'waiting' }, lost));
224
+ if (!socket.haltedSince(opensBefore))
225
+ continue;
226
+ progress.complete = false;
227
+ return 'done';
228
+ }
229
+ const other = partner === null ? undefined : socket.rooms.discovered(partner);
230
+ if (result.kind === 'conflicted' ||
231
+ result.kind === 'rejected' ||
232
+ result.kind === 'retryable-refusal') {
233
+ journal.record(`outcome ${room.locator} ${head.id} ${result.kind}` +
234
+ (result.kind === 'conflicted' && result.roomVersion !== undefined
235
+ ? ` ${result.roomVersion}`
236
+ : ''));
237
+ journal.record(`refusal ${result.kind} ${result.code}`);
238
+ }
239
+ if (result.kind === 'retryable-refusal') {
240
+ const reset = result.reset;
241
+ progress.complete = false;
242
+ await store.exclusive(() => mark(runtime, local, room.locator, head, reset === undefined ? { state: 'waiting' } : { state: 'reset', reset }, lost));
243
+ return reset === undefined ? 'again' : { at: Date.parse(reset) };
244
+ }
245
+ if (result.kind === 'conflicted' || result.kind === 'rejected') {
246
+ const refreshed = async () => ({
247
+ there: other === undefined ? null : await advanced(runtime, other, local, authorized),
248
+ here: await advanced(runtime, room, local, authorized),
249
+ });
250
+ const judged = result.kind === 'conflicted' && other !== undefined ? await refreshed() : null;
251
+ if (judged !== null && (judged.there !== null || judged.here !== null)) {
252
+ await store.exclusive(() => mark(runtime, local, room.locator, head, { state: 'waiting' }, lost));
253
+ if (other !== undefined && judged.there !== null) {
254
+ await withhold(runtime, judged.there, other, local, progress);
255
+ }
256
+ if (judged.here !== null)
257
+ await withhold(runtime, judged.here, room, local, progress);
258
+ return 'done';
259
+ }
260
+ await answered(runtime, room.locator, head, result);
261
+ const { there, here } = judged ?? (await refreshed());
262
+ if (other !== undefined && there !== null) {
263
+ await withhold(runtime, there, other, local, progress);
264
+ }
265
+ if (here !== null) {
266
+ await withhold(runtime, here, room, local, progress);
267
+ return 'done';
268
+ }
269
+ continue;
270
+ }
271
+ const kept = await (partner === null
272
+ ? adopted(runtime, room, local, head, result)
273
+ : adoptedMove(runtime, room, partner, local, head, result)).finally(heard);
274
+ if (!kept) {
275
+ progress.complete = false;
276
+ return 'done';
277
+ }
278
+ }
279
+ return 'done';
280
+ }
281
+ export function submitLater(runtime, locator) {
282
+ if (runtime.localSaves)
283
+ return;
284
+ void runtime.recovery.attempt(`submit ${locator}`, (signal) => submitPass(runtime, locator, signal));
285
+ }
286
+ async function submitPass(runtime, locator, signal) {
287
+ const { store, socket } = runtime;
288
+ const room = socket.rooms.discovered(locator);
289
+ if (!socket.continuing() || socket.stopping() || room === undefined) {
290
+ return 'done';
291
+ }
292
+ const progress = { complete: true, reachable: true };
293
+ let next;
294
+ try {
295
+ await socket.authorize();
296
+ const { authorization } = socket.session.now();
297
+ if (authorization === undefined)
298
+ return 'done';
299
+ next = await submitRoom(runtime, room, await store.open(), authorization, progress, signal);
300
+ }
301
+ catch {
302
+ progress.reachable = false;
303
+ next = 'again';
304
+ }
305
+ if (signal.aborted)
306
+ return 'done';
307
+ await rest(runtime, progress);
308
+ return next === 'again' && !socket.isOpen() ? 'done' : next;
309
+ }
@@ -0,0 +1,20 @@
1
+ import type { ConversationHistoryRequest } from './adapter.js';
2
+ import { type CallResult, type HistoryAnswer, type Resolution } from './outcome.js';
3
+ import type { Attempted } from './recovery.js';
4
+ import type { Runtime } from './runtime.js';
5
+ export type HistoryQuery = {
6
+ revision?: string;
7
+ pageToken?: string;
8
+ };
9
+ export declare function readHistory(runtime: Runtime, locator: string, path: string, query?: HistoryQuery): Promise<HistoryAnswer>;
10
+ export declare function located(runtime: Runtime, room: string, path: string, destination: string): Promise<{
11
+ room: string;
12
+ path: string;
13
+ } | null>;
14
+ export declare function resolveReference(runtime: Runtime, room: string, path: string, destination: string): Promise<Resolution>;
15
+ export declare function invokeAction(runtime: Runtime, name: string, input: unknown, dispatching?: () => Promise<void>): Promise<CallResult>;
16
+ export declare function readConversationHistory(runtime: Runtime, request: ConversationHistoryRequest): Promise<CallResult>;
17
+ export declare function called(runtime: Runtime, name: string, input: unknown): Promise<CallResult>;
18
+ export declare function resendCalls(runtime: Runtime, signal: AbortSignal): Promise<Attempted>;
19
+ export declare function resendLater(runtime: Runtime): void;
20
+ //# sourceMappingURL=actions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"actions.d.ts","sourceRoot":"","sources":["../src/actions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAK/D,OAAO,EAUL,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,UAAU,EAChB,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAE/C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAG5C,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAOF,wBAAsB,WAAW,CAC/B,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,KAAK,GAAE,YAAiB,GACvB,OAAO,CAAC,aAAa,CAAC,CAqCxB;AAED,wBAAsB,OAAO,CAC3B,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAAC,CAShD;AAED,wBAAsB,gBAAgB,CACpC,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,UAAU,CAAC,CAuBrB;AAwBD,wBAAsB,YAAY,CAChC,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,OAAO,EACd,WAAW,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAChC,OAAO,CAAC,UAAU,CAAC,CA6BrB;AAED,wBAAsB,uBAAuB,CAC3C,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,0BAA0B,GAClC,OAAO,CAAC,UAAU,CAAC,CAiBrB;AAuCD,wBAAsB,MAAM,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,UAAU,CAAC,CAKhG;AAED,wBAAsB,WAAW,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC,CAiB3F;AAiBD,wBAAgB,WAAW,CAAC,OAAO,EAAE,OAAO,QAG3C"}
@@ -0,0 +1,251 @@
1
+ import { roomTarget, target } from '@agentshouse/mdmodel';
2
+ import { resendConversations } from './conversation.js';
3
+ import { listingNow } from './listing.js';
4
+ import { locked, lockedRooms } from './locks.js';
5
+ import { scoped } from './models.js';
6
+ import { ABSENT_TARGET, calledResult, failure, historyAnswer, LOCKED_UNREADABLE, lockedRoom, undeclared, UNCERTAIN, UNREADABLE, } from './outcome.js';
7
+ import { refuse } from './refusal.js';
8
+ function operationOf(input) {
9
+ const operation = input?.operation_id;
10
+ return typeof operation === 'string' ? operation : null;
11
+ }
12
+ export async function readHistory(runtime, locator, path, query = {}) {
13
+ const { store, socket, journal } = runtime;
14
+ const { session } = socket;
15
+ if (socket.closed() || session.now().refusal !== null)
16
+ return UNCERTAIN;
17
+ await socket.firstDiscovery;
18
+ if (!session.now().roomsNamed)
19
+ return UNCERTAIN;
20
+ try {
21
+ await socket.authorize();
22
+ }
23
+ catch {
24
+ return UNCERTAIN;
25
+ }
26
+ const { closed, authorization, refusal } = session.now();
27
+ if (closed || authorization === undefined || refusal !== null)
28
+ return UNCERTAIN;
29
+ const since = store.fences.count();
30
+ const transfer = ['transfer', 'history', locator, path];
31
+ if (query.revision !== undefined)
32
+ transfer.push(query.revision);
33
+ if (query.pageToken !== undefined)
34
+ transfer.push(query.pageToken);
35
+ journal.record(transfer.join(' '));
36
+ try {
37
+ const result = await socket.history(authorization, {
38
+ locator,
39
+ path,
40
+ ...(query.revision === undefined ? {} : { revision: query.revision }),
41
+ ...(query.pageToken === undefined ? {} : { pageToken: query.pageToken }),
42
+ });
43
+ journal.record(`outcome history ${result.kind}`);
44
+ if (store.fences.since(locator, since))
45
+ return failure({ kind: 'room-not-readable', locator });
46
+ if (result.kind === 'scoped-refusal') {
47
+ const room = await store.record(result.locator);
48
+ if (room !== undefined)
49
+ await scoped(runtime, result, room.locator, room.declaration);
50
+ }
51
+ if (result.kind === 'revoked-authorization')
52
+ await refuse(runtime, 'revoked-authorization');
53
+ return historyAnswer(result);
54
+ }
55
+ catch {
56
+ journal.record('outcome history transport-uncertainty');
57
+ return UNCERTAIN;
58
+ }
59
+ }
60
+ export async function located(runtime, room, path, destination) {
61
+ const crossing = roomTarget(destination);
62
+ const listing = await listingNow(runtime);
63
+ if (crossing === null) {
64
+ const named = target(path, destination);
65
+ return named === null || !listing.has(room) ? null : { room, path: named };
66
+ }
67
+ const held = [...listing.values()].find((listed) => listed.handle === crossing.handle);
68
+ return held === undefined ? null : { room: held.locator, path: crossing.path };
69
+ }
70
+ export async function resolveReference(runtime, room, path, destination) {
71
+ const { store } = runtime;
72
+ if (roomTarget(destination) === null && target(path, destination) === null) {
73
+ return ABSENT_TARGET;
74
+ }
75
+ const at = await located(runtime, room, path, destination);
76
+ if (at === null)
77
+ return UNREADABLE;
78
+ if (await locked(runtime, at.room))
79
+ return LOCKED_UNREADABLE;
80
+ const held = await store.record(at.room);
81
+ if (held !== undefined &&
82
+ (await (await store.open()).entries(held.key, 'visible', [at.path])).length > 0) {
83
+ return { kind: 'document', ...at };
84
+ }
85
+ const history = await readHistory(runtime, at.room, at.path);
86
+ if (history.kind === 'no-history' || history.kind === 'revision')
87
+ return ABSENT_TARGET;
88
+ if (history.kind !== 'history')
89
+ return history;
90
+ const [last] = history.revisions;
91
+ if (last === undefined)
92
+ return ABSENT_TARGET;
93
+ return history.deleted === true
94
+ ? { kind: 'deleted', ...at, revision: last }
95
+ : { kind: 'document', ...at };
96
+ }
97
+ async function declaredAction({ store }, name) {
98
+ for (const capability of (await (await store.open()).facts()).capabilities) {
99
+ for (const action of capability.actions ?? []) {
100
+ if (action.name === name)
101
+ return action;
102
+ }
103
+ }
104
+ return undefined;
105
+ }
106
+ async function calledRooms(runtime, name, input) {
107
+ const member = (await declaredAction(runtime, name))?.room;
108
+ if (member === undefined || input === null || typeof input !== 'object')
109
+ return [];
110
+ const named = input[member];
111
+ return (Array.isArray(named) ? named : [named]).filter((room) => typeof room === 'string');
112
+ }
113
+ export async function invokeAction(runtime, name, input, dispatching) {
114
+ const { store, socket, journal } = runtime;
115
+ const { session } = socket;
116
+ await socket.ready;
117
+ if (socket.closed() || session.now().refusal !== null)
118
+ return UNCERTAIN;
119
+ if ((await declaredAction(runtime, name)) === undefined)
120
+ return undeclared(name);
121
+ try {
122
+ await socket.authorize();
123
+ }
124
+ catch {
125
+ return UNCERTAIN;
126
+ }
127
+ const { closed, authorization, refusal } = session.now();
128
+ if (closed || authorization === undefined || refusal !== null)
129
+ return UNCERTAIN;
130
+ await dispatching?.();
131
+ journal.record(`transfer action ${name}`);
132
+ try {
133
+ const result = await socket.action(authorization, { name, input });
134
+ if (result.kind === 'scoped-refusal') {
135
+ const room = await store.record(result.locator);
136
+ if (room !== undefined)
137
+ await scoped(runtime, result, room.locator, room.declaration);
138
+ }
139
+ if (result.kind === 'revoked-authorization')
140
+ await refuse(runtime, 'revoked-authorization');
141
+ journal.record(`outcome action ${result.kind}`);
142
+ if (result.kind === 'rejected')
143
+ journal.record(`refusal rejected ${result.code}`);
144
+ return calledResult(result);
145
+ }
146
+ catch {
147
+ journal.record('outcome action transport-uncertainty');
148
+ return UNCERTAIN;
149
+ }
150
+ }
151
+ export async function readConversationHistory(runtime, request) {
152
+ const { socket, journal } = runtime;
153
+ await socket.ready;
154
+ const stream = socket.conversations();
155
+ if (socket.closed() || socket.session.now().refusal !== null || stream?.history === undefined) {
156
+ return UNCERTAIN;
157
+ }
158
+ journal.record('transfer conversation history');
159
+ try {
160
+ const result = await stream.history(request);
161
+ if (result.kind === 'revoked-authorization')
162
+ await refuse(runtime, 'revoked-authorization');
163
+ journal.record(`outcome conversation history ${result.kind}`);
164
+ return calledResult(result);
165
+ }
166
+ catch {
167
+ journal.record('outcome conversation history transport-uncertainty');
168
+ return UNCERTAIN;
169
+ }
170
+ }
171
+ function unsettled(result) {
172
+ if (result.kind === 'action')
173
+ return false;
174
+ return result.retryable && (result.kind !== 'rejected' || result.reset === undefined);
175
+ }
176
+ async function recoveredAction(runtime, name, input) {
177
+ const { store, socket, recovery } = runtime;
178
+ const { lanes } = socket;
179
+ const operation = operationOf(input);
180
+ if (operation === null)
181
+ return invokeAction(runtime, name, input);
182
+ const kept = async () => (await store.open()).keepCall({ operation, name, input });
183
+ lanes.call(operation);
184
+ try {
185
+ let result = await invokeAction(runtime, name, input, kept);
186
+ if (unsettled(result)) {
187
+ await recovery.attempt(`action ${operation}`, async () => {
188
+ if (socket.closed() || socket.session.now().refusal !== null)
189
+ return 'done';
190
+ if (!socket.isOpen())
191
+ return 'again';
192
+ result = await invokeAction(runtime, name, input, kept);
193
+ return unsettled(result) ? 'again' : 'done';
194
+ }, { again: true });
195
+ }
196
+ if (!unsettled(result))
197
+ await (await store.open()).dropCall(operation);
198
+ return result;
199
+ }
200
+ finally {
201
+ lanes.called(operation);
202
+ }
203
+ }
204
+ export async function called(runtime, name, input) {
205
+ await runtime.socket.ready;
206
+ const lockedNow = await lockedRooms(runtime);
207
+ const room = (await calledRooms(runtime, name, input)).find((one) => lockedNow.includes(one));
208
+ return room === undefined ? recoveredAction(runtime, name, input) : lockedRoom(room);
209
+ }
210
+ export async function resendCalls(runtime, signal) {
211
+ const { store, socket } = runtime;
212
+ const { lanes } = socket;
213
+ if (!socket.isOpen() || socket.closed() || socket.stopping())
214
+ return 'done';
215
+ const local = await store.open();
216
+ for (const held of await local.calls()) {
217
+ if (lanes.calling(held.operation))
218
+ continue;
219
+ if (socket.closed() || signal.aborted)
220
+ return 'done';
221
+ lanes.call(held.operation);
222
+ try {
223
+ if (unsettled(await invokeAction(runtime, held.name, held.input)))
224
+ return 'again';
225
+ await local.dropCall(held.operation);
226
+ }
227
+ finally {
228
+ lanes.called(held.operation);
229
+ }
230
+ }
231
+ return 'done';
232
+ }
233
+ async function resendPass({ socket, conversations }, signal) {
234
+ if (!socket.continuing() || !socket.isOpen() || socket.stopping())
235
+ return 'done';
236
+ const reading = { complete: true, reachable: true };
237
+ try {
238
+ await resendConversations(conversations, reading);
239
+ }
240
+ catch {
241
+ reading.reachable = false;
242
+ }
243
+ if (signal.aborted)
244
+ return 'done';
245
+ return reading.reachable && reading.complete ? 'done' : 'again';
246
+ }
247
+ export function resendLater(runtime) {
248
+ if (!runtime.socket.isOpen())
249
+ return;
250
+ void runtime.recovery.attempt('resend', (signal) => resendPass(runtime, signal), { again: true });
251
+ }