@agentshouse/mdruntime 0.1.0-c1e540acf5f9d → 0.1.0-c1ea23b9ca7c6

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 (215) hide show
  1. package/README.md +14 -13
  2. package/dist/acceptance.js +1 -1
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +24 -5
  5. package/dist/adapter.d.ts +74 -2
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/authored.d.ts +88 -0
  8. package/dist/authored.d.ts.map +1 -0
  9. package/dist/authored.js +345 -0
  10. package/dist/change.d.ts +4 -0
  11. package/dist/change.d.ts.map +1 -1
  12. package/dist/change.js +3 -0
  13. package/dist/client.d.ts +10 -4
  14. package/dist/client.d.ts.map +1 -1
  15. package/dist/client.js +14 -44
  16. package/dist/collection.d.ts +5 -1
  17. package/dist/collection.d.ts.map +1 -1
  18. package/dist/collection.js +16 -8
  19. package/dist/column.d.ts +5 -0
  20. package/dist/column.d.ts.map +1 -0
  21. package/dist/column.js +9 -0
  22. package/dist/compile.d.ts.map +1 -1
  23. package/dist/compile.js +2 -3
  24. package/dist/connection.d.ts +1 -1
  25. package/dist/connection.d.ts.map +1 -1
  26. package/dist/connection.js +8 -4
  27. package/dist/context.d.ts.map +1 -1
  28. package/dist/context.js +1 -1
  29. package/dist/conversation-live.d.ts +7 -2
  30. package/dist/conversation-live.d.ts.map +1 -1
  31. package/dist/conversation-live.js +3 -0
  32. package/dist/conversation-row.d.ts +8 -1
  33. package/dist/conversation-row.d.ts.map +1 -1
  34. package/dist/conversation-row.js +20 -7
  35. package/dist/conversation.d.ts +8 -4
  36. package/dist/conversation.d.ts.map +1 -1
  37. package/dist/conversation.js +41 -22
  38. package/dist/discovery.d.ts +3 -1
  39. package/dist/discovery.d.ts.map +1 -1
  40. package/dist/discovery.js +30 -5
  41. package/dist/download.d.ts +6 -0
  42. package/dist/download.d.ts.map +1 -0
  43. package/dist/download.js +42 -0
  44. package/dist/durability.d.ts +1 -0
  45. package/dist/durability.d.ts.map +1 -1
  46. package/dist/durability.js +3 -0
  47. package/dist/entry.d.ts +14 -0
  48. package/dist/entry.d.ts.map +1 -1
  49. package/dist/entry.js +45 -16
  50. package/dist/failure.d.ts.map +1 -1
  51. package/dist/failure.js +3 -1
  52. package/dist/figures.d.ts.map +1 -1
  53. package/dist/figures.js +4 -2
  54. package/dist/file.d.ts +1 -1
  55. package/dist/file.d.ts.map +1 -1
  56. package/dist/file.js +3 -2
  57. package/dist/host.d.ts +5 -2
  58. package/dist/host.d.ts.map +1 -1
  59. package/dist/host.js +42 -13
  60. package/dist/house-query.d.ts +15 -0
  61. package/dist/house-query.d.ts.map +1 -0
  62. package/dist/house-query.js +43 -0
  63. package/dist/index.d.ts +8 -3
  64. package/dist/index.d.ts.map +1 -1
  65. package/dist/index.js +4 -0
  66. package/dist/install.d.ts +8 -2
  67. package/dist/install.d.ts.map +1 -1
  68. package/dist/install.js +40 -33
  69. package/dist/lifecycle.d.ts +1 -1
  70. package/dist/lifecycle.d.ts.map +1 -1
  71. package/dist/lifecycle.js +37 -24
  72. package/dist/listing.d.ts +24 -12
  73. package/dist/listing.d.ts.map +1 -1
  74. package/dist/listing.js +50 -21
  75. package/dist/live.d.ts +5 -3
  76. package/dist/live.d.ts.map +1 -1
  77. package/dist/live.js +71 -30
  78. package/dist/loaded.d.ts +5 -0
  79. package/dist/loaded.d.ts.map +1 -0
  80. package/dist/loaded.js +40 -0
  81. package/dist/locks.d.ts.map +1 -1
  82. package/dist/locks.js +4 -2
  83. package/dist/lost.js +1 -1
  84. package/dist/media.d.ts +18 -1
  85. package/dist/media.d.ts.map +1 -1
  86. package/dist/media.js +114 -24
  87. package/dist/memory.d.ts +2 -0
  88. package/dist/memory.d.ts.map +1 -1
  89. package/dist/memory.js +41 -9
  90. package/dist/messages.d.ts +3 -1
  91. package/dist/messages.d.ts.map +1 -1
  92. package/dist/messages.js +9 -3
  93. package/dist/modal.d.ts +2 -0
  94. package/dist/modal.d.ts.map +1 -0
  95. package/dist/modal.js +11 -0
  96. package/dist/model.d.ts +4 -2
  97. package/dist/model.d.ts.map +1 -1
  98. package/dist/model.js +7 -1
  99. package/dist/models.d.ts.map +1 -1
  100. package/dist/models.js +3 -3
  101. package/dist/observation.d.ts +26 -10
  102. package/dist/observation.d.ts.map +1 -1
  103. package/dist/observation.js +200 -69
  104. package/dist/online.d.ts +7 -0
  105. package/dist/online.d.ts.map +1 -0
  106. package/dist/online.js +29 -0
  107. package/dist/opening.d.ts.map +1 -1
  108. package/dist/opening.js +18 -2
  109. package/dist/outcome.d.ts +10 -1
  110. package/dist/outcome.d.ts.map +1 -1
  111. package/dist/outcome.js +17 -2
  112. package/dist/overlay.d.ts +1 -1
  113. package/dist/overlay.d.ts.map +1 -1
  114. package/dist/overlay.js +3 -5
  115. package/dist/parts.d.ts +3 -1
  116. package/dist/parts.d.ts.map +1 -1
  117. package/dist/parts.js +30 -7
  118. package/dist/preparation.d.ts +32 -7
  119. package/dist/preparation.d.ts.map +1 -1
  120. package/dist/preparation.js +186 -78
  121. package/dist/presence.d.ts +8 -2
  122. package/dist/presence.d.ts.map +1 -1
  123. package/dist/presence.js +24 -13
  124. package/dist/queries.d.ts +1 -1
  125. package/dist/queries.d.ts.map +1 -1
  126. package/dist/queries.js +62 -57
  127. package/dist/questions.d.ts +1 -0
  128. package/dist/questions.d.ts.map +1 -1
  129. package/dist/questions.js +3 -1
  130. package/dist/queue.d.ts.map +1 -1
  131. package/dist/queue.js +51 -21
  132. package/dist/reach.d.ts +14 -6
  133. package/dist/reach.d.ts.map +1 -1
  134. package/dist/reach.js +102 -16
  135. package/dist/react.d.ts +5 -1
  136. package/dist/react.d.ts.map +1 -1
  137. package/dist/readiness.d.ts +15 -3
  138. package/dist/readiness.d.ts.map +1 -1
  139. package/dist/readiness.js +10 -10
  140. package/dist/reads.d.ts +1 -1
  141. package/dist/reads.d.ts.map +1 -1
  142. package/dist/reads.js +2 -2
  143. package/dist/recovery.d.ts +1 -0
  144. package/dist/recovery.d.ts.map +1 -1
  145. package/dist/recovery.js +2 -1
  146. package/dist/replica.d.ts.map +1 -1
  147. package/dist/replica.js +2 -2
  148. package/dist/room-draft.d.ts +28 -3
  149. package/dist/room-draft.d.ts.map +1 -1
  150. package/dist/room-draft.js +230 -12
  151. package/dist/room-drafts.d.ts.map +1 -1
  152. package/dist/room-drafts.js +5 -2
  153. package/dist/room-list.d.ts +10 -0
  154. package/dist/room-list.d.ts.map +1 -0
  155. package/dist/room-list.js +45 -0
  156. package/dist/rows.d.ts +2 -1
  157. package/dist/rows.d.ts.map +1 -1
  158. package/dist/rows.js +6 -6
  159. package/dist/running.d.ts +5 -0
  160. package/dist/running.d.ts.map +1 -0
  161. package/dist/running.js +10 -0
  162. package/dist/runtime.d.ts +3 -2
  163. package/dist/runtime.d.ts.map +1 -1
  164. package/dist/save.d.ts.map +1 -1
  165. package/dist/save.js +17 -86
  166. package/dist/saves.d.ts +2 -0
  167. package/dist/saves.d.ts.map +1 -1
  168. package/dist/saves.js +41 -14
  169. package/dist/seed.d.ts.map +1 -1
  170. package/dist/seed.js +2 -2
  171. package/dist/shown.d.ts +3 -1
  172. package/dist/shown.d.ts.map +1 -1
  173. package/dist/shown.js +62 -38
  174. package/dist/socket/session.d.ts +4 -1
  175. package/dist/socket/session.d.ts.map +1 -1
  176. package/dist/socket/session.js +3 -0
  177. package/dist/socket.d.ts +1 -0
  178. package/dist/socket.d.ts.map +1 -1
  179. package/dist/socket.js +1 -0
  180. package/dist/source.d.ts +5 -0
  181. package/dist/source.d.ts.map +1 -1
  182. package/dist/source.js +13 -2
  183. package/dist/store/installs.d.ts +2 -2
  184. package/dist/store/installs.d.ts.map +1 -1
  185. package/dist/store/installs.js +10 -4
  186. package/dist/store/readiness.d.ts +5 -4
  187. package/dist/store/readiness.d.ts.map +1 -1
  188. package/dist/store/readiness.js +28 -12
  189. package/dist/store/records.d.ts +13 -1
  190. package/dist/store/records.d.ts.map +1 -1
  191. package/dist/store/records.js +49 -20
  192. package/dist/store/rooms-shown.d.ts +8 -0
  193. package/dist/store/rooms-shown.d.ts.map +1 -0
  194. package/dist/store/rooms-shown.js +13 -0
  195. package/dist/store.d.ts +14 -9
  196. package/dist/store.d.ts.map +1 -1
  197. package/dist/store.js +12 -5
  198. package/dist/stored.d.ts +31 -14
  199. package/dist/stored.d.ts.map +1 -1
  200. package/dist/stored.js +180 -44
  201. package/dist/surface.d.ts +10 -5
  202. package/dist/surface.d.ts.map +1 -1
  203. package/dist/surface.js +48 -15
  204. package/dist/sync.d.ts +4 -2
  205. package/dist/sync.d.ts.map +1 -1
  206. package/dist/sync.js +46 -24
  207. package/dist/tables.d.ts +10 -49
  208. package/dist/tables.d.ts.map +1 -1
  209. package/dist/tables.js +60 -322
  210. package/dist/validation.d.ts.map +1 -1
  211. package/dist/validation.js +3 -9
  212. package/dist/wire.d.ts +22 -8
  213. package/dist/wire.d.ts.map +1 -1
  214. package/dist/wire.js +11 -7
  215. package/package.json +3 -3
package/dist/tables.js CHANGED
@@ -1,9 +1,12 @@
1
1
  import { sql } from '@agentshouse/mdmodel';
2
2
  import { answerQuery } from './compile.js';
3
+ import { amended, authoredLocators, authoredPart, call, callRecords, chatDraft, chatDraftConversations, chatDraftRecord, chatDraftRecords, chatDraftsDropped, claim, fact, fenced, fencedWork, fences, FILE_PART_BYTES, authoredEmptied, heldOrder, kept, moving, pendingBatch, pendingState, queuedLocators, queueOf, queues, remember, roomDraft, roomDraftRecords, spooled, unfenced, unsent, unsentStatus, } from './authored.js';
4
+ import { count, text, texts } from './column.js';
3
5
  import { conversationFacts, conversationRows, forgottenConversations, heldConversations, keptFact, keptMessages, messageCount, messageWindow, MESSAGE_TABLES, turnArrived, } from './messages.js';
4
6
  import { keptRead, READ_TABLES, readsOfOtherBuildsForgotten, storedRead, } from './reads.js';
5
7
  import { conversationTallies, roomTallies, } from './tallies.js';
6
8
  import { abandon, release, commit, covering, demoted, ended, entries, erase, facts, fenceRooms, folders, heldLocators, heldRoom, heldRooms, offlineMode, opened, overlaid, presentation, rederive, referrers, refreshing, REPLICA_TABLES, resume, stage, typed, stagedLocators, tablesOf, typedWithin, unrefresh, } from './stored.js';
9
+ export { authoredEmptied, authoredTable, FILE_PART_BYTES, formatted, heldTables, } from './authored.js';
7
10
  const STATEMENTS_HELD = 256;
8
11
  export function sqliteConnection(engine, database) {
9
12
  const decoder = new TextDecoder();
@@ -71,36 +74,16 @@ export function sqliteConnection(engine, database) {
71
74
  },
72
75
  };
73
76
  }
74
- export const FILE_PART_BYTES = 1_048_576;
75
77
  const PROBE_BYTES = 4 * FILE_PART_BYTES;
76
- const TABLES = [
77
- ['authored_facts', 'name TEXT PRIMARY KEY, value TEXT NOT NULL'],
78
- ['authored_queues', 'locator TEXT PRIMARY KEY, ids TEXT NOT NULL'],
79
- [
80
- 'authored_pending',
81
- 'locator TEXT NOT NULL, id TEXT NOT NULL, batch TEXT NOT NULL, state TEXT NOT NULL, files TEXT NOT NULL, paths TEXT NOT NULL, moving INTEGER NOT NULL, PRIMARY KEY (locator, id)',
82
- ],
83
- [
84
- 'authored_room_drafts',
85
- 'locator TEXT NOT NULL, path TEXT NOT NULL, record TEXT NOT NULL, files TEXT NOT NULL, PRIMARY KEY (locator, path)',
86
- ],
87
- ['authored_chat_drafts', 'conversation TEXT PRIMARY KEY, record TEXT NOT NULL'],
88
- ['authored_calls', 'operation TEXT PRIMARY KEY, record TEXT NOT NULL, files TEXT NOT NULL'],
89
- ['authored_fences', 'locator TEXT PRIMARY KEY'],
90
- ];
91
78
  export const RUNTIME_TABLES = [
92
- ...TABLES.map(([name, columns]) => `CREATE TABLE ${name} (${columns}) WITHOUT ROWID`),
93
- 'CREATE TABLE file_parts (key TEXT NOT NULL, part INTEGER NOT NULL, bytes BLOB NOT NULL, PRIMARY KEY (key, part))',
94
79
  'CREATE TABLE opened_files (key TEXT PRIMARY KEY, locator TEXT NOT NULL, path TEXT NOT NULL, revision TEXT NOT NULL, media_type TEXT NOT NULL, sha256 TEXT, byte_length INTEGER) WITHOUT ROWID',
95
80
  'CREATE INDEX opened_files_name ON opened_files (locator, path, revision)',
81
+ 'CREATE TABLE opened_parts (key TEXT NOT NULL, part INTEGER NOT NULL, bytes BLOB NOT NULL, PRIMARY KEY (key, part))',
96
82
  ...REPLICA_TABLES,
97
83
  ...MESSAGE_TABLES,
98
84
  ...READ_TABLES,
99
85
  ];
100
- const OWNERS = ['authored_pending', 'authored_room_drafts', 'authored_calls'];
101
- const AUTHORED_KEYS = OWNERS.map((table) => `SELECT value FROM ${table}, json_each(${table}.files)`).join(' UNION ');
102
- const UNOWNED_PARTS = `DELETE FROM file_parts WHERE key NOT IN (${AUTHORED_KEYS} UNION SELECT key FROM opened_files)`;
103
- const CLEANABLE_BYTES = `SELECT coalesce(sum(length(bytes)), 0) AS bytes FROM file_parts WHERE key NOT IN (${AUTHORED_KEYS})`;
86
+ const UNOPENED_PARTS = 'DELETE FROM opened_parts WHERE key NOT IN (SELECT key FROM opened_files)';
104
87
  const READS = new Set([
105
88
  'authored.status',
106
89
  'authored.moving',
@@ -185,309 +168,75 @@ export function isRuntime(request) {
185
168
  export function readsRuntime(request) {
186
169
  return READS.has(request.kind);
187
170
  }
188
- function text(value) {
189
- return typeof value === 'string' ? value : String(value);
190
- }
191
- function texts(rows, column) {
192
- return rows.map((row) => text(row[column]));
193
- }
194
- function owned(files) {
195
- return JSON.stringify([...new Set(files)]);
196
- }
197
- function fact(connection, name) {
198
- const [held] = connection.rows('SELECT value FROM authored_facts WHERE name = ?', [name]);
199
- return held === undefined ? null : text(held.value);
200
- }
201
- function remember(connection, name, value) {
202
- connection.run('INSERT OR REPLACE INTO authored_facts (name, value) VALUES (?, ?)', [
203
- name,
204
- value,
205
- ]);
206
- return null;
207
- }
208
- function fenced(connection, locator) {
209
- return connection.rows('SELECT 1 FROM authored_fences WHERE locator = ?', [locator]).length > 0;
210
- }
211
- function stored(connection, key) {
212
- return connection.rows('SELECT 1 FROM file_parts WHERE key = ? LIMIT 1', [key]).length > 0;
213
- }
214
- function storedPart(connection, key, part, bytes) {
215
- connection.run('INSERT INTO file_parts (key, part, bytes) VALUES (?, ?, ?)', [key, part, bytes]);
216
- }
217
- async function storedFiles(connection, files) {
218
- for (const { key, blob } of files) {
219
- if (stored(connection, key))
220
- continue;
221
- for (let at = 0, part = 0; at < blob.size; at += FILE_PART_BYTES, part += 1) {
222
- const bytes = new Uint8Array(await blob.slice(at, at + FILE_PART_BYTES).arrayBuffer());
223
- storedPart(connection, key, part, bytes);
224
- }
225
- }
226
- }
227
- function filePart(connection, key, part) {
228
- const [held] = connection.rows('SELECT bytes FROM file_parts WHERE key = ? AND part = ?', [
229
- key,
230
- part,
231
- ]);
232
- return held?.bytes instanceof Uint8Array ? held.bytes : null;
233
- }
234
- function* fileParts(connection, key) {
235
- for (let part = 0;; part += 1) {
236
- const bytes = filePart(connection, key, part);
237
- if (bytes === null)
238
- return;
239
- yield bytes;
240
- }
241
- }
242
- async function spooled(connection, spool, { key, type }) {
243
- for (const held of [...spool.held()]) {
244
- if (held !== key && !stored(connection, held))
245
- await spool.drop(held);
246
- }
247
- return spool.spool(key, type, fileParts(connection, key));
248
- }
249
- function unowned(connection) {
250
- connection.run(UNOWNED_PARTS);
251
- }
252
- function queued(connection) {
253
- return connection.rows('SELECT locator, ids FROM authored_queues').map((row) => ({
254
- locator: text(row.locator),
255
- ids: JSON.parse(text(row.ids)),
256
- }));
257
- }
258
171
  function heldWork(connection, locator) {
259
172
  const room = heldRoom(connection, locator);
260
- const pending = new Map(connection
261
- .rows('SELECT id, batch, state FROM authored_pending WHERE locator = ?', [locator])
262
- .map((row) => [
263
- text(row.id),
264
- { id: text(row.id), batch: text(row.batch), state: text(row.state) },
265
- ]));
266
173
  return {
267
174
  room: room === null ? null : { ...room, overlaid: overlaid(connection, room.key) },
268
- queue: heldOrder(connection, locator).map((id) => {
269
- const held = pending.get(id);
270
- if (held === undefined)
271
- throw new Error(`Missing Pending Change ${locator}/${id}`);
272
- return held;
273
- }),
175
+ queue: queueOf(connection, locator),
274
176
  };
275
177
  }
276
178
  function status(connection) {
277
- const [completed] = connection.rows("SELECT value FROM replica_facts WHERE name = 'lastCompleted'");
278
179
  return {
279
- queued: texts(connection.rows('SELECT locator FROM authored_queues'), 'locator'),
280
- lastCompleted: completed === undefined ? null : text(completed.value),
281
- changes: connection
282
- .rows('SELECT p.locator, c.value AS path FROM authored_pending p, json_each(p.paths) c')
283
- .map((row) => ({ locator: text(row.locator), path: text(row.path) })),
284
- resets: connection
285
- .rows(`SELECT locator, json_extract(state, '$.submission.reset') AS reset FROM authored_pending
286
- WHERE json_extract(state, '$.submission.state') = 'reset'`)
287
- .map((row) => ({ locator: text(row.locator), reset: text(row.reset) })),
288
- returned: texts(connection.rows(`SELECT record FROM authored_room_drafts WHERE json_type(record, '$.returned') = 'object'`), 'record'),
180
+ ...unsentStatus(connection),
181
+ lastCompleted: facts(connection, ['lastCompleted']).lastCompleted ?? null,
289
182
  };
290
183
  }
291
184
  function pendingOf(connection, locator, id) {
292
- const [held] = connection.rows('SELECT state FROM authored_pending WHERE locator = ? AND id = ?', [locator, id]);
293
185
  return {
294
- state: held === undefined ? null : text(held.state),
186
+ state: pendingState(connection, locator, id),
295
187
  room: connection.rows('SELECT 1 FROM replica_rooms WHERE locator = ?', [locator]).length > 0,
296
188
  };
297
189
  }
298
- function roomDraftRecords(connection, locator, paths) {
299
- if (locator === null) {
300
- return texts(connection.rows('SELECT record FROM authored_room_drafts'), 'record');
301
- }
302
- if (paths === null) {
303
- return texts(connection.rows('SELECT record FROM authored_room_drafts WHERE locator = ?', [locator]), 'record');
304
- }
305
- return texts(connection.rows('SELECT record FROM authored_room_drafts WHERE locator = ? AND path IN (SELECT value FROM json_each(?))', [locator, JSON.stringify(paths)]), 'record');
306
- }
307
- function chatDraftRecord(connection, conversation) {
308
- const [held] = connection.rows('SELECT record FROM authored_chat_drafts WHERE conversation = ?', [
309
- conversation,
310
- ]);
311
- return held === undefined ? null : text(held.record);
312
- }
313
- function heldOrder(connection, locator) {
314
- const [held] = connection.rows('SELECT ids FROM authored_queues WHERE locator = ?', [locator]);
315
- return held === undefined ? [] : JSON.parse(text(held.ids));
316
- }
317
- function keptRows(connection, { locator, order, added, founded, roomDrafts, consumedRoomDrafts }) {
318
- for (const path of consumedRoomDrafts ?? []) {
319
- connection.run('DELETE FROM authored_room_drafts WHERE locator = ? AND path = ?', [
320
- locator,
321
- path,
322
- ]);
323
- }
324
- for (const { path, record, files } of roomDrafts ?? []) {
325
- connection.run('INSERT OR REPLACE INTO authored_room_drafts (locator, path, record, files) VALUES (?, ?, ?, ?)', [locator, path, record, owned(files)]);
326
- }
327
- if (order === undefined)
328
- return;
329
- const before = new Set(heldOrder(connection, locator));
330
- const after = new Set(order);
331
- if (after.size === 0)
332
- connection.run('DELETE FROM authored_queues WHERE locator = ?', [locator]);
333
- else {
334
- connection.run('INSERT OR REPLACE INTO authored_queues (locator, ids) VALUES (?, ?)', [
335
- locator,
336
- JSON.stringify([...after]),
337
- ]);
338
- }
339
- for (const id of before) {
340
- if (!after.has(id)) {
341
- connection.run('DELETE FROM authored_pending WHERE locator = ? AND id = ?', [locator, id]);
342
- }
343
- }
344
- for (const { id, batch, state, files, paths, moving } of added ?? []) {
345
- if (before.has(id))
346
- continue;
347
- connection.run('INSERT OR REPLACE INTO authored_pending (locator, id, batch, state, files, paths, moving) VALUES (?, ?, ?, ?, ?, ?, ?)', [locator, id, batch, state, owned(files), JSON.stringify(paths), moving ? 1 : 0]);
348
- }
349
- for (const { id, batch, files, paths, moving } of founded ?? []) {
350
- const [held] = connection.rows('SELECT files FROM authored_pending WHERE locator = ? AND id = ?', [locator, id]);
351
- if (held === undefined || !before.has(id))
352
- continue;
353
- connection.run('UPDATE authored_pending SET batch = ?, files = ?, paths = ?, moving = ? WHERE locator = ? AND id = ?', [
354
- batch,
355
- owned([...JSON.parse(text(held.files)), ...files]),
356
- JSON.stringify(paths),
357
- moving ? 1 : 0,
358
- locator,
359
- id,
360
- ]);
361
- }
362
- }
363
- async function kept(connection, { writes, files, whole }) {
364
- const refused = writes.find(({ locator }) => fenced(connection, locator));
365
- if (refused !== undefined && whole)
366
- return refused.locator;
367
- await storedFiles(connection, files);
368
- for (const write of writes) {
369
- if (!fenced(connection, write.locator))
370
- keptRows(connection, write);
371
- }
372
- unowned(connection);
373
- return refused?.locator ?? null;
374
- }
375
- function amended(connection, { locator, id, amendment }) {
376
- if (fenced(connection, locator))
377
- return null;
378
- const [held] = connection.rows('SELECT state FROM authored_pending WHERE locator = ? AND id = ?', [locator, id]);
379
- if (held === undefined)
380
- return null;
381
- connection.run('UPDATE authored_pending SET state = ? WHERE locator = ? AND id = ?', [
382
- JSON.stringify({
383
- ...JSON.parse(text(held.state)),
384
- ...JSON.parse(amendment),
385
- }),
386
- locator,
387
- id,
388
- ]);
389
- return null;
390
- }
391
- async function roomDraft(connection, { locator, path, kept: draft, files }) {
392
- if (fenced(connection, locator))
393
- return locator;
394
- await storedFiles(connection, files);
395
- if (draft === null) {
396
- connection.run('DELETE FROM authored_room_drafts WHERE locator = ? AND path = ?', [
397
- locator,
398
- path,
399
- ]);
400
- }
401
- else {
402
- connection.run('INSERT OR REPLACE INTO authored_room_drafts (locator, path, record, files) VALUES (?, ?, ?, ?)', [locator, path, draft.record, owned(draft.files)]);
403
- }
404
- unowned(connection);
405
- return null;
406
- }
407
- function conversationTables() {
408
- return [...tablesOf(MESSAGE_TABLES), 'authored_chat_drafts'];
409
- }
410
190
  function storedConversations(connection, conversations) {
411
191
  const named = conversations === null ? '' : ' WHERE conversation IN (SELECT value FROM json_each(?))';
412
192
  const bound = conversations === null ? [] : [JSON.stringify(conversations)];
413
193
  return [
414
- ...new Set(conversationTables().flatMap((table) => texts(connection.rows(`SELECT DISTINCT conversation FROM ${table}${named}`, bound), 'conversation'))),
194
+ ...new Set([
195
+ ...tablesOf(MESSAGE_TABLES).flatMap((table) => texts(connection.rows(`SELECT DISTINCT conversation FROM ${table}${named}`, bound), 'conversation')),
196
+ ...chatDraftConversations(connection, conversations),
197
+ ]),
415
198
  ];
416
199
  }
417
200
  function droppedConversations(connection, conversations) {
418
201
  const held = storedConversations(connection, conversations);
419
- for (const table of conversationTables()) {
202
+ for (const table of tablesOf(MESSAGE_TABLES)) {
420
203
  connection.run(`DELETE FROM ${table} WHERE conversation IN (SELECT value FROM json_each(?))`, [
421
204
  JSON.stringify(conversations),
422
205
  ]);
423
206
  }
207
+ chatDraftsDropped(connection, conversations);
424
208
  return held;
425
209
  }
426
- function chatDraft(connection, { kept: drafts, dropped }) {
427
- for (const conversation of dropped) {
428
- connection.run('DELETE FROM authored_chat_drafts WHERE conversation = ?', [conversation]);
429
- }
430
- for (const { conversation, record } of drafts) {
431
- connection.run('INSERT OR REPLACE INTO authored_chat_drafts (conversation, record) VALUES (?, ?)', [conversation, record]);
432
- }
433
- return null;
434
- }
435
- async function call(connection, { operation, kept: held, files }) {
436
- await storedFiles(connection, files);
437
- if (held === null)
438
- connection.run('DELETE FROM authored_calls WHERE operation = ?', [operation]);
439
- else {
440
- connection.run('INSERT OR REPLACE INTO authored_calls (operation, record, files) VALUES (?, ?, ?)', [operation, held.record, owned(held.files)]);
441
- }
442
- unowned(connection);
443
- return null;
444
- }
445
- function roomWork(connection, locator) {
446
- connection.run('DELETE FROM authored_queues WHERE locator = ?', [locator]);
447
- connection.run('DELETE FROM authored_pending WHERE locator = ?', [locator]);
210
+ function unopened(connection) {
211
+ connection.run(UNOPENED_PARTS);
448
212
  }
449
213
  function fence(connection, locators) {
214
+ fencedWork(connection, locators);
450
215
  for (const locator of locators) {
451
- connection.run('INSERT OR REPLACE INTO authored_fences (locator) VALUES (?)', [locator]);
452
- roomWork(connection, locator);
453
- connection.run('DELETE FROM authored_room_drafts WHERE locator = ?', [locator]);
454
216
  connection.run('DELETE FROM opened_files WHERE locator = ?', [locator]);
455
217
  }
456
- unowned(connection);
218
+ unopened(connection);
457
219
  return fenceRooms(connection, locators);
458
220
  }
459
221
  function erased(connection, { locator, limit }) {
460
222
  return erase(connection, locator, limit);
461
223
  }
462
- function unfenced(connection, locators) {
224
+ function unfencedRooms(connection, locators) {
463
225
  const erased = locators.filter((locator) => connection.rows('SELECT 1 FROM replica_erasing WHERE locator = ? LIMIT 1', [locator])
464
226
  .length === 0);
465
- for (const locator of erased) {
466
- connection.run('DELETE FROM authored_fences WHERE locator = ?', [locator]);
467
- }
227
+ unfenced(connection, erased);
468
228
  return erased;
469
229
  }
470
- function claim(connection, holder) {
471
- const held = fact(connection, 'holder');
472
- if (held !== null)
473
- return held === holder;
474
- remember(connection, 'holder', holder);
475
- return true;
476
- }
477
230
  function end(connection, holder) {
478
231
  ended(connection);
479
232
  forgottenConversations(connection);
480
233
  if (holder !== null && claim(connection, holder))
481
234
  return true;
482
- for (const [table] of TABLES)
483
- connection.run(`DELETE FROM ${table}`);
235
+ authoredEmptied(connection);
484
236
  connection.run('DELETE FROM opened_files');
485
- connection.run('DELETE FROM file_parts');
237
+ connection.run('DELETE FROM opened_parts');
486
238
  return false;
487
239
  }
488
- function count(value) {
489
- return typeof value === 'bigint' ? Number(value) : typeof value === 'number' ? value : 0;
490
- }
491
240
  function openedFile(connection, { locator, path, revision }) {
492
241
  const [held] = connection.rows('SELECT key, media_type, byte_length FROM opened_files WHERE locator = ? AND path = ? AND revision = ? AND byte_length IS NOT NULL', [locator, path, revision]);
493
242
  return held === undefined
@@ -502,7 +251,7 @@ function opening(connection, { key, locator, path, revision, mediaType }) {
502
251
  if (fenced(connection, locator))
503
252
  return false;
504
253
  connection.run('DELETE FROM opened_files WHERE locator = ? AND path = ? AND revision = ? AND byte_length IS NULL', [locator, path, revision]);
505
- unowned(connection);
254
+ unopened(connection);
506
255
  connection.run('INSERT INTO opened_files (key, locator, path, revision, media_type) VALUES (?, ?, ?, ?, ?)', [key, locator, path, revision, mediaType]);
507
256
  return true;
508
257
  }
@@ -515,19 +264,23 @@ function fetching(connection, key) {
515
264
  function openedPart(connection, { key, part, bytes }) {
516
265
  if (fetching(connection, key) === null)
517
266
  return false;
518
- storedPart(connection, key, part, bytes);
267
+ connection.run('INSERT INTO opened_parts (key, part, bytes) VALUES (?, ?, ?)', [
268
+ key,
269
+ part,
270
+ bytes,
271
+ ]);
519
272
  return true;
520
273
  }
521
274
  function dropped(connection, key) {
522
275
  connection.run('DELETE FROM opened_files WHERE key = ?', [key]);
523
- connection.run('DELETE FROM file_parts WHERE key = ?', [key]);
276
+ connection.run('DELETE FROM opened_parts WHERE key = ?', [key]);
524
277
  return null;
525
278
  }
526
279
  function keptOpened(connection, { key, sha256, byteLength, parts }) {
527
280
  const fetched = fetching(connection, key);
528
281
  if (fetched === null)
529
282
  return false;
530
- const [held] = connection.rows('SELECT count(*) AS parts, min(part) AS first, max(part) AS last, coalesce(sum(length(bytes)), 0) AS bytes FROM file_parts WHERE key = ?', [key]);
283
+ const [held] = connection.rows('SELECT count(*) AS parts, min(part) AS first, max(part) AS last, coalesce(sum(length(bytes)), 0) AS bytes FROM opened_parts WHERE key = ?', [key]);
531
284
  const whole = count(held.parts) === parts &&
532
285
  count(held.bytes) === byteLength &&
533
286
  (parts === 0 || (count(held.first) === 0 && count(held.last) === parts - 1));
@@ -541,27 +294,24 @@ function keptOpened(connection, { key, sha256, byteLength, parts }) {
541
294
  key,
542
295
  ]);
543
296
  connection.run('DELETE FROM opened_files WHERE locator = ? AND path = ? AND revision = ? AND key <> ?', [fetched.locator, fetched.path, fetched.revision, key]);
544
- unowned(connection);
297
+ unopened(connection);
545
298
  return true;
546
299
  }
547
- const UNSENT = [
548
- ['pending-change', 'authored_pending'],
549
- ['call', 'authored_calls'],
550
- ['room-draft', 'authored_room_drafts'],
551
- ['chat-draft', 'authored_chat_drafts'],
552
- ];
553
- function unsent(connection) {
554
- const [held] = connection.rows(`SELECT ${UNSENT.map(([kind, table]) => `(SELECT count(*) FROM ${table}) AS "${kind}"`).join(', ')}`);
555
- return UNSENT.map(([kind]) => ({ kind, count: count(held[kind]) })).filter((work) => work.count > 0);
300
+ function heldPart(connection, key, part) {
301
+ const [held] = connection.rows('SELECT bytes FROM opened_parts WHERE key = ? AND part = ?', [
302
+ key,
303
+ part,
304
+ ]);
305
+ return held?.bytes instanceof Uint8Array ? held.bytes : authoredPart(connection, key, part);
556
306
  }
557
307
  function cleanable(connection) {
558
- const [held] = connection.rows(CLEANABLE_BYTES);
308
+ const [held] = connection.rows('SELECT coalesce(sum(length(bytes)), 0) AS bytes FROM opened_parts');
559
309
  return count(held.bytes);
560
310
  }
561
311
  function cleaned(connection) {
562
312
  connection.run('DELETE FROM opened_files');
563
313
  const freed = cleanable(connection);
564
- unowned(connection);
314
+ unopened(connection);
565
315
  return freed;
566
316
  }
567
317
  function openedHeld(connection) {
@@ -587,51 +337,39 @@ export function answerRuntime(connection, request, spool) {
587
337
  case 'authored.status':
588
338
  return status(connection);
589
339
  case 'authored.moving':
590
- return {
591
- queues: queued(connection),
592
- pending: connection
593
- .rows('SELECT locator, id, batch, state FROM authored_pending WHERE moving = 1 ORDER BY locator')
594
- .map((row) => ({
595
- locator: text(row.locator),
596
- id: text(row.id),
597
- batch: text(row.batch),
598
- state: text(row.state),
599
- })),
600
- };
340
+ return moving(connection);
601
341
  case 'authored.pending':
602
342
  return pendingOf(connection, request.locator, request.id);
603
- case 'authored.batch': {
604
- const [held] = connection.rows('SELECT batch FROM authored_pending WHERE locator = ? AND id = ?', [request.locator, request.id]);
605
- return held === undefined ? null : text(held.batch);
606
- }
343
+ case 'authored.batch':
344
+ return pendingBatch(connection, request.locator, request.id);
607
345
  case 'authored.order':
608
346
  return heldOrder(connection, request.locator);
609
347
  case 'authored.queued':
610
- return texts(connection.rows('SELECT locator FROM authored_queues'), 'locator');
348
+ return queuedLocators(connection);
611
349
  case 'authored.roomDrafts':
612
350
  return roomDraftRecords(connection, request.locator, request.paths);
613
351
  case 'authored.chatDraft.read':
614
352
  return chatDraftRecord(connection, request.conversation);
615
353
  case 'authored.chatDrafts':
616
- return texts(connection.rows('SELECT record FROM authored_chat_drafts'), 'record');
354
+ return chatDraftRecords(connection);
617
355
  case 'authored.calls':
618
- return texts(connection.rows('SELECT record FROM authored_calls'), 'record');
356
+ return callRecords(connection);
619
357
  case 'authored.fences':
620
- return texts(connection.rows('SELECT locator FROM authored_fences'), 'locator');
358
+ return fences(connection);
621
359
  case 'authored.unsent':
622
360
  return unsent(connection);
623
361
  case 'authored.fact':
624
362
  return fact(connection, request.name);
625
363
  case 'authored.file':
626
- return spooled(connection, spool, request);
364
+ return spooled(connection, spool, request.key, request.type);
627
365
  case 'authored.amend':
628
- return amended(connection, request);
366
+ return amended(connection, request.locator, request.id, request.amendment);
629
367
  case 'authored.roomDraft':
630
- return roomDraft(connection, request);
368
+ return roomDraft(connection, request.locator, request.path, request.kept, request.files);
631
369
  case 'authored.chatDraft':
632
- return chatDraft(connection, request);
370
+ return chatDraft(connection, request.kept, request.dropped);
633
371
  case 'authored.call':
634
- return call(connection, request);
372
+ return call(connection, request.operation, request.kept, request.files);
635
373
  case 'authored.remember':
636
374
  return remember(connection, request.name, request.value);
637
375
  case 'authored.claim':
@@ -645,18 +383,18 @@ export function answerRuntime(connection, request, spool) {
645
383
  case 'replica.room':
646
384
  return heldWork(connection, request.locator);
647
385
  case 'replica.covering':
648
- return {
649
- rooms: covering(connection),
650
- queues: queued(connection),
651
- };
386
+ return { ...covering(connection, request.since), queues: queues(connection) };
652
387
  case 'replica.offline':
653
388
  return offlineMode(connection, request.locator);
654
389
  case 'replica.held':
655
- return { held: heldLocators(connection), staged: stagedLocators(connection) };
390
+ return {
391
+ held: [...new Set([...heldLocators(connection), ...authoredLocators(connection)])],
392
+ staged: stagedLocators(connection),
393
+ };
656
394
  case 'replica.demote':
657
395
  return demoted(connection);
658
396
  case 'replica.facts':
659
- return facts(connection);
397
+ return facts(connection, request.names);
660
398
  case 'replica.presentation':
661
399
  return presentation(connection, request.app);
662
400
  case 'replica.query':
@@ -690,7 +428,7 @@ export function answerRuntime(connection, request, spool) {
690
428
  case 'replica.abandon':
691
429
  return abandon(connection, request.locator);
692
430
  case 'replica.unfence':
693
- return unfenced(connection, request.locators);
431
+ return unfencedRooms(connection, request.locators);
694
432
  case 'replica.release':
695
433
  return release(connection, request.key);
696
434
  case 'replica.rederive':
@@ -722,7 +460,7 @@ export function answerRuntime(connection, request, spool) {
722
460
  case 'opened.held':
723
461
  return openedHeld(connection);
724
462
  case 'file.part':
725
- return filePart(connection, request.key, request.part);
463
+ return heldPart(connection, request.key, request.part);
726
464
  case 'conversation.held':
727
465
  return heldConversations(connection, request.conversations);
728
466
  case 'conversation.keep': {
@@ -1 +1 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../src/validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAOL,KAAK,UAAU,EAGhB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAA2B,KAAK,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAM5E,KAAK,QAAQ,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC;AAE3E,MAAM,MAAM,KAAK,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAA;CAAE,CAAC;AAEpF,MAAM,MAAM,IAAI,GAAG,CACjB,KAAK,EAAE,MAAM,GAAG,IAAI,KACjB,OAAO,CAAC;IAAE,QAAQ,CAAC,OAAO,EAAE,SAAS,QAAQ,EAAE,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC,CAAC;AAEtF,MAAM,MAAM,MAAM,GAAG,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,KAAK,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC;AAwBvF,wBAAsB,mBAAmB,CACvC,IAAI,EAAE,IAA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13 +1,10 @@
1
- import { assess, parse, parseFrontmatter, verdicts, } from '@agentshouse/mdmodel';
2
- import { definitionsAt, selected } from './collection.js';
1
+ import { assess, parse, verdicts, } from '@agentshouse/mdmodel';
2
+ import { definitionsAt, selected, textType, typedText } from './collection.js';
3
3
  import { paced, ROW_WEIGHT, yielded } from './turn.js';
4
4
  const NO_TYPES = { types: {} };
5
5
  function modelOf(model) {
6
6
  return model.status === 'unavailable' ? NO_TYPES : { types: model.types };
7
7
  }
8
- function typedText(read) {
9
- return read.ok && read.document.type !== undefined && read.document.type !== 'any';
10
- }
11
8
  function partVerdict(part) {
12
9
  return part.unmodeled !== null
13
10
  ? { verdict: 'refuse', rule: 'model-unavailable', file: part.unmodeled }
@@ -57,10 +54,7 @@ export async function validatedCollection(walk, lookup, composition) {
57
54
  return refusals.find((refusal) => refusal.rule !== 'model-unavailable') ?? refusals[0];
58
55
  }
59
56
  function declared(content) {
60
- if (content === undefined || content === null)
61
- return undefined;
62
- const read = parseFrontmatter(content);
63
- return read.ok ? (read.frontmatter.type ?? 'any') : undefined;
57
+ return content === undefined || content === null ? undefined : textType(parse(content));
64
58
  }
65
59
  function shifted({ was, now }) {
66
60
  return ((now === undefined) !== (was === undefined) ||