@agentshouse/mdruntime 0.1.0-c0d767f8f799a → 0.1.0-c104334047dfe

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 (71) hide show
  1. package/AGENTS.md +200 -194
  2. package/README.md +8 -8
  3. package/dist/adapter.d.ts +18 -1
  4. package/dist/adapter.d.ts.map +1 -1
  5. package/dist/chat/earlier.d.ts +10 -0
  6. package/dist/chat/earlier.d.ts.map +1 -0
  7. package/dist/chat/earlier.js +54 -0
  8. package/dist/chat/list.d.ts.map +1 -1
  9. package/dist/chat/list.js +39 -3
  10. package/dist/chat/provider.d.ts.map +1 -1
  11. package/dist/chat/provider.js +9 -10
  12. package/dist/chat/question.d.ts +1 -1
  13. package/dist/chat/question.d.ts.map +1 -1
  14. package/dist/chat/question.js +7 -18
  15. package/dist/chat/style.js +1 -1
  16. package/dist/client.d.ts +6 -2
  17. package/dist/client.d.ts.map +1 -1
  18. package/dist/client.js +7 -3
  19. package/dist/connection.d.ts +9 -2
  20. package/dist/connection.d.ts.map +1 -1
  21. package/dist/connection.js +254 -62
  22. package/dist/conversation.d.ts +5 -0
  23. package/dist/conversation.d.ts.map +1 -1
  24. package/dist/conversation.js +94 -31
  25. package/dist/host.d.ts.map +1 -1
  26. package/dist/host.js +8 -0
  27. package/dist/index.d.ts +3 -3
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/memory.d.ts +2 -1
  30. package/dist/memory.d.ts.map +1 -1
  31. package/dist/memory.js +10 -0
  32. package/dist/observation.d.ts +2 -0
  33. package/dist/observation.d.ts.map +1 -1
  34. package/dist/observation.js +9 -3
  35. package/dist/outcome.d.ts +8 -2
  36. package/dist/outcome.d.ts.map +1 -1
  37. package/dist/outcome.js +3 -0
  38. package/dist/query.d.ts +21 -1
  39. package/dist/query.d.ts.map +1 -1
  40. package/dist/query.js +28 -5
  41. package/dist/reach.d.ts.map +1 -1
  42. package/dist/reach.js +13 -0
  43. package/dist/react.d.ts +1 -2
  44. package/dist/react.d.ts.map +1 -1
  45. package/dist/react.js +2 -14
  46. package/dist/read-side.d.ts.map +1 -1
  47. package/dist/read-side.js +173 -48
  48. package/dist/replica.d.ts +5 -1
  49. package/dist/replica.d.ts.map +1 -1
  50. package/dist/replica.js +19 -3
  51. package/dist/rows.d.ts +1 -0
  52. package/dist/rows.d.ts.map +1 -1
  53. package/dist/rows.js +5 -0
  54. package/dist/save.d.ts +1 -0
  55. package/dist/save.d.ts.map +1 -1
  56. package/dist/save.js +17 -1
  57. package/dist/search.d.ts +15 -2
  58. package/dist/search.d.ts.map +1 -1
  59. package/dist/search.js +76 -17
  60. package/dist/seed.d.ts.map +1 -1
  61. package/dist/seed.js +18 -4
  62. package/dist/store.d.ts +16 -2
  63. package/dist/store.d.ts.map +1 -1
  64. package/dist/store.js +48 -14
  65. package/dist/windows.d.ts +26 -0
  66. package/dist/windows.d.ts.map +1 -0
  67. package/dist/windows.js +93 -0
  68. package/dist/wire.d.ts +16 -0
  69. package/dist/wire.d.ts.map +1 -1
  70. package/dist/wire.js +4 -0
  71. package/package.json +2 -2
@@ -1,11 +1,12 @@
1
1
  import { attachedModel } from './attached-model.js';
2
2
  import { cached, modelTypes, NO_MODEL, sameModel, } from './model.js';
3
3
  import { observing, whole } from './observation.js';
4
- import { filterOf, identityOf, planOf, queryKey, } from './query.js';
4
+ import { filterOf, identityOf, planOf, plain, queryKey, } from './query.js';
5
5
  import { createReadSide } from './read-side.js';
6
6
  import { seededResult } from './seed.js';
7
7
  import { markdownPath, roomOfKey } from './rows.js';
8
8
  import { createSearchIndex, searchPlanOf, } from './search.js';
9
+ import { capped, MAXIMUM_WINDOW } from './windows.js';
9
10
  import { bootstrapRoom } from './bootstrap.js';
10
11
  import { changeSet, entering, mergeChangeSets } from './change-set.js';
11
12
  import { createConversationStore, } from './conversation.js';
@@ -14,13 +15,13 @@ import { indexReferrers, referencesIndexed, validRoot, } from './collection.js';
14
15
  import { runtimeError } from './failure.js';
15
16
  import { createLog } from './log.js';
16
17
  import { binaryRecord } from './binary.js';
17
- import { absentBinary, binaryAnswer, calledResult, failure, historyAnswer, undeclared, UNCERTAIN, } from './outcome.js';
18
+ import { absentBinary, binaryAnswer, binaryLinkAnswer, calledResult, failure, historyAnswer, undeclared, UNCERTAIN, } from './outcome.js';
18
19
  import { createRecovery } from './recovery.js';
19
20
  import { doorReads } from './reads.js';
20
21
  import { refreshRoom } from './refresh.js';
21
- import { buildBatch, joined, limitedIn, requeued } from './save.js';
22
- import { applyReturned, compositionOf, confirmedOf, landed, readReclassified, replicaOf, sameMembership, withdrawals, } from './replica.js';
23
- import { binaryBase, changedParts, changesOf, held, heldFile, joinedDraft, readParts, returnedBy, returnedInto, staleWrite, underneath, withdrawnDrafts, } from './room-draft.js';
22
+ import { buildBatch, joined, limitedIn, queuedDocuments, requeued, } from './save.js';
23
+ import { applyReturned, compositionOf, confirmedOf, landed, readReclassified, replicaOf, sameMembership, standing, } from './replica.js';
24
+ import { binaryBase, changedParts, changesOf, held, heldFile, joinedDraft, moveTarget, readParts, returnedBy, returnedInto, staleWrite, underneath, withdrawnDrafts, } from './room-draft.js';
24
25
  import { same, saving, STARTING, transferring, } from './status.js';
25
26
  import { detectStorageLoss, nextGeneration } from './durability.js';
26
27
  import { demanded, narrower, WHOLE_DEMAND, wider } from './acquisition.js';
@@ -38,6 +39,7 @@ function roomsAlone(plan) {
38
39
  plan.root.types === null &&
39
40
  plan.root.where === null &&
40
41
  plan.root.pathPrefix === null &&
42
+ plan.paths === null &&
41
43
  plan.hop === null &&
42
44
  plan.backlinks === null);
43
45
  }
@@ -58,6 +60,10 @@ const OPENING_LANE = 'opening';
58
60
  const CONVERSATION_LANE = 'conversations';
59
61
  const ROOM_READS = 256;
60
62
  const ROOM_ATTEMPTS = 16;
63
+ function operationOf(input) {
64
+ const operation = input?.operation_id;
65
+ return typeof operation === 'string' ? operation : null;
66
+ }
61
67
  const outside = new Map();
62
68
  function outsideOf(roots) {
63
69
  const key = JSON.stringify(roots);
@@ -148,6 +154,7 @@ export function composeRuntime(options) {
148
154
  let handed = false;
149
155
  const recovery = (options.recovery ?? createRecovery()).scope(ROOM_ATTEMPTS);
150
156
  const repairing = new Set();
157
+ const calling = new Set();
151
158
  const repairable = new Map();
152
159
  let outcomeRetention = null;
153
160
  const answering = new Map();
@@ -268,6 +275,15 @@ export function composeRuntime(options) {
268
275
  const value = presentations.get(app);
269
276
  return value === undefined ? null : structuredClone(value);
270
277
  }
278
+ function keepPresentation(app, next) {
279
+ return exclusive(async () => {
280
+ const local = await openLocal();
281
+ const stored = next(presentations.get(app));
282
+ await local.present({ app, value: stored });
283
+ presentations.set(app, stored);
284
+ publishChangeSet(changeSet('', null, null, { presentation: app }));
285
+ });
286
+ }
271
287
  function emitPresentation(app) {
272
288
  if (closed)
273
289
  return;
@@ -549,20 +565,28 @@ export function composeRuntime(options) {
549
565
  .map((room) => room.locator)
550
566
  .sort();
551
567
  }
568
+ function heldLocators(acquired) {
569
+ return demand.whole ? acquired : [...new Set([...acquired, ...demand.rooms])].sort();
570
+ }
552
571
  function heldDemand(acquired) {
553
572
  if (demand.whole)
554
573
  return null;
555
- return { rooms: acquired.map((room) => room.locator), conversations: demand.conversations };
574
+ return {
575
+ rooms: heldLocators(acquired.map((room) => room.locator)),
576
+ conversations: demand.conversations,
577
+ };
556
578
  }
557
579
  function applyDemand(next) {
558
- const before = acquiredLocators();
559
- const conversed = demand.conversations;
580
+ const before = heldLocators(acquiredLocators());
581
+ const { whole, conversations: conversed } = demand;
560
582
  demand = next;
561
- const after = acquiredLocators();
583
+ const after = heldLocators(acquiredLocators());
562
584
  queueReadiness();
563
585
  if (!conversed && next.conversations)
564
586
  scheduleCurrency();
565
- if (JSON.stringify(before) === JSON.stringify(after) && conversed === next.conversations)
587
+ if (JSON.stringify(before) === JSON.stringify(after) &&
588
+ whole === next.whole &&
589
+ conversed === next.conversations)
566
590
  return;
567
591
  journal.record(`acquisition ${next.whole ? 'whole' : after.length}`);
568
592
  if (options.adapter.roomStream === undefined)
@@ -862,9 +886,7 @@ export function composeRuntime(options) {
862
886
  awaitPreparation(cover);
863
887
  if ('pending' in cover)
864
888
  return cover;
865
- return 'groups' in rows
866
- ? { groups: rows.groups, coverage: cover.coverage }
867
- : { documents: rows.documents, coverage: cover.coverage };
889
+ return { ...rows, coverage: cover.coverage };
868
890
  }
869
891
  async function indexConversation(conversation) {
870
892
  searchIndex.indexMessages(conversation, [], false);
@@ -1008,7 +1030,7 @@ export function composeRuntime(options) {
1008
1030
  refusal: scopedRefusals.get(room.locator) ?? sourceRefusals.get(room.locator) ?? null,
1009
1031
  });
1010
1032
  }
1011
- async function applyDiscovery(found, started) {
1033
+ async function applyDiscovery(found, started, covered) {
1012
1034
  for (const room of found) {
1013
1035
  const fence = fences.get(room.locator);
1014
1036
  if (fence !== undefined && fence.erased && fence.at < started)
@@ -1021,10 +1043,14 @@ export function composeRuntime(options) {
1021
1043
  const listedLocators = new Set(listed.map((room) => room.locator));
1022
1044
  for (const locator of listedLocators)
1023
1045
  scopedRefusals.delete(locator);
1024
- discovered.clear();
1046
+ if (covered === undefined)
1047
+ discovered.clear();
1048
+ else
1049
+ for (const locator of covered)
1050
+ discovered.delete(locator);
1025
1051
  for (const room of listed)
1026
1052
  discovered.set(room.locator, room);
1027
- const unlisted = unlistedHeld(new Set(found.map((room) => room.locator)));
1053
+ const unlisted = unlistedHeld(new Set(found.map((room) => room.locator))).filter((locator) => covered === undefined || covered.includes(locator));
1028
1054
  if (options.adapter.exhaustiveDiscovery === true)
1029
1055
  await loseRooms(unlisted);
1030
1056
  else
@@ -1282,8 +1308,9 @@ export function composeRuntime(options) {
1282
1308
  }
1283
1309
  async function settle(local, locator, before, confirmed, queue, settling = {}) {
1284
1310
  const previousQueue = before?.queue ?? queuesWithoutReplica.get(locator) ?? [];
1285
- const withdrawn = withdrawals(confirmed, queue, withdrawable, localSaves);
1286
- const kept = queue.filter((batch) => !withdrawn.includes(batch));
1311
+ const { withdrawn, kept } = standing(confirmed, queue, withdrawable, localSaves);
1312
+ const founded = kept.filter((batch) => !queue.includes(batch));
1313
+ const queueChanged = kept.length !== previousQueue.length || founded.length > 0;
1287
1314
  const unqueued = withdrawn.map((batch) => ({
1288
1315
  batch,
1289
1316
  returned: localSaves || landed(batch, confirmed) ? null : returnedBy('withdrawn'),
@@ -1307,9 +1334,10 @@ export function composeRuntime(options) {
1307
1334
  });
1308
1335
  }
1309
1336
  }
1310
- await local.keepAuthored({ locator, queue: kept, roomDrafts: [...written.values()] });
1337
+ await local.keepAuthored({ locator, queue: kept, founded, roomDrafts: [...written.values()] });
1338
+ const staying = new Set(kept.map((batch) => batch.id));
1311
1339
  for (const batch of previousQueue) {
1312
- if (!kept.includes(batch))
1340
+ if (!staying.has(batch.id))
1313
1341
  pendings.delete(batch.id);
1314
1342
  }
1315
1343
  for (const record of written.values())
@@ -1330,7 +1358,7 @@ export function composeRuntime(options) {
1330
1358
  else
1331
1359
  queuesWithoutReplica.delete(locator);
1332
1360
  publishChangeSet(changeSet(locator, before ?? null, held, {
1333
- queue: kept.length !== previousQueue.length,
1361
+ queue: queueChanged,
1334
1362
  drafts: [...written.keys()],
1335
1363
  }));
1336
1364
  moveTo({ ...status, ...attention() });
@@ -1345,7 +1373,7 @@ export function composeRuntime(options) {
1345
1373
  replicas.set(locator, replicaOf(confirmed, kept, before));
1346
1374
  return changeSet(locator, before ?? null, replicas.get(locator) ?? null, {
1347
1375
  room: settling.room,
1348
- queue: kept.length !== previousQueue.length,
1376
+ queue: queueChanged,
1349
1377
  drafts: [...written.keys()],
1350
1378
  });
1351
1379
  };
@@ -1437,7 +1465,7 @@ export function composeRuntime(options) {
1437
1465
  return true;
1438
1466
  });
1439
1467
  }
1440
- async function saveRoomDrafts(locator, named, writtenBy) {
1468
+ async function savedRoom(locator) {
1441
1469
  const local = await openLocal();
1442
1470
  await referenced(locator);
1443
1471
  const replica = replicas.get(locator);
@@ -1445,6 +1473,10 @@ export function composeRuntime(options) {
1445
1473
  if (replica === undefined || room === undefined) {
1446
1474
  throw runtimeError('room-not-held', `No Local Replica for ${locator}`);
1447
1475
  }
1476
+ return { local, replica, room };
1477
+ }
1478
+ async function saveRoomDrafts(locator, named, writtenBy) {
1479
+ const target = await savedRoom(locator);
1448
1480
  const saved = [];
1449
1481
  for (const { path, revision } of named) {
1450
1482
  const record = roomDrafts.get(locator)?.get(path);
@@ -1454,6 +1486,38 @@ export function composeRuntime(options) {
1454
1486
  if (!saved.some((draft) => draft.path === path))
1455
1487
  saved.push({ path, parts: record.parts });
1456
1488
  }
1489
+ return committed(locator, target, saved, new Set(saved.map(({ path }) => path)), writtenBy);
1490
+ }
1491
+ async function saveParts(locator, named, writtenBy) {
1492
+ const read = named.map(({ path, parts }) => ({ path, parts: readParts(parts) }));
1493
+ if (read.some(({ path }, at) => path === '' ||
1494
+ normalize(path) !== path ||
1495
+ read.findIndex((one) => one.path === path) !== at)) {
1496
+ throw runtimeError('room-draft-malformed');
1497
+ }
1498
+ const target = await savedRoom(locator);
1499
+ const saved = [];
1500
+ for (const { path, parts } of read) {
1501
+ const file = parts.file === undefined
1502
+ ? undefined
1503
+ : await heldFile(parts.file.blob, binaryBase(target.replica.visible, path));
1504
+ saved.push({ path, parts: held(undefined, parts, file) });
1505
+ }
1506
+ return committed(locator, target, saved, new Set(), writtenBy);
1507
+ }
1508
+ function carriedDrafts(locator, saved, consumed) {
1509
+ const holding = roomDrafts.get(locator);
1510
+ return new Map(saved.flatMap(({ path, parts }) => {
1511
+ const record = holding?.get(path);
1512
+ if (parts.move === undefined || record === undefined || consumed.has(path))
1513
+ return [];
1514
+ const to = moveTarget(parts.move);
1515
+ if (to === path || holding?.has(to) === true)
1516
+ return [];
1517
+ return [[path, { ...record, path: to, revision: roomDraftRevision() }]];
1518
+ }));
1519
+ }
1520
+ async function committed(locator, { local, replica, room }, saved, consumed, writtenBy) {
1457
1521
  const changed = saved.flatMap(({ path, parts }) => changedParts(path, underneath(parts, path, replica.visible)));
1458
1522
  if (changed.length > 0)
1459
1523
  return { outcome: 'refused', code: 'changed-underneath', retryable: false, parts: changed };
@@ -1486,6 +1550,7 @@ export function composeRuntime(options) {
1486
1550
  visible: replica.visible,
1487
1551
  changes,
1488
1552
  bases: parts.flatMap(({ bases }) => bases),
1553
+ queued: queuedDocuments(replica.queue),
1489
1554
  writtenBy,
1490
1555
  clean: {
1491
1556
  text: replica.confirmed.record.documents,
@@ -1493,7 +1558,9 @@ export function composeRuntime(options) {
1493
1558
  composition: compositionOf(replica.confirmed.record),
1494
1559
  },
1495
1560
  };
1496
- const checked = withdrawing ? buildBatch({ ...input, queued: requeue.queued }) : null;
1561
+ const checked = withdrawing
1562
+ ? buildBatch({ ...input, queued: new Set([...input.queued, ...requeue.queued]) })
1563
+ : null;
1497
1564
  if (checked?.kind === 'refused')
1498
1565
  return { outcome: 'refused', ...checked.refusal };
1499
1566
  const owners = [...requeue.owners].flatMap((origin) => {
@@ -1516,6 +1583,7 @@ export function composeRuntime(options) {
1516
1583
  ...input,
1517
1584
  visible: replicaOf(replica.confirmed, requeue.queue, replica).visible,
1518
1585
  changes: requeue.changes,
1586
+ queued: queuedDocuments(requeue.queue),
1519
1587
  });
1520
1588
  if (built?.kind === 'refused')
1521
1589
  return { outcome: 'refused', ...built.refusal };
@@ -1550,13 +1618,16 @@ export function composeRuntime(options) {
1550
1618
  if (limited !== null)
1551
1619
  return { outcome: 'refused', ...limited };
1552
1620
  }
1553
- const consumed = saved.map(({ path }) => path);
1621
+ const carried = carriedDrafts(locator, saved, consumed);
1622
+ const left = [...consumed, ...carried.keys()];
1623
+ const arrived = [...carried.values()];
1554
1624
  try {
1555
1625
  await local.keepAuthored({
1556
1626
  locator,
1557
1627
  queue: batches,
1558
1628
  saved: drafted,
1559
- consumedRoomDrafts: consumed,
1629
+ consumedRoomDrafts: left,
1630
+ roomDrafts: arrived,
1560
1631
  });
1561
1632
  }
1562
1633
  catch {
@@ -1571,12 +1642,14 @@ export function composeRuntime(options) {
1571
1642
  }
1572
1643
  if (!localSaves)
1573
1644
  recorded = false;
1574
- for (const path of consumed)
1645
+ for (const path of left)
1575
1646
  forgetRoomDraft(locator, path);
1647
+ for (const record of arrived)
1648
+ rememberRoomDraft(record);
1576
1649
  replicas.set(locator, replicaOf(replica.confirmed, batches, replica));
1577
1650
  publishChangeSet(changeSet(locator, replica, replicas.get(locator) ?? null, {
1578
1651
  queue: true,
1579
- drafts: consumed,
1652
+ drafts: [...left, ...arrived.map(({ path }) => path)],
1580
1653
  }));
1581
1654
  moveTo({ ...(localSaves ? status : saving(status)), ...attention() });
1582
1655
  submitLater(locator);
@@ -1667,7 +1740,7 @@ export function composeRuntime(options) {
1667
1740
  const scope = options.adapter.referrerScope(request.referrer);
1668
1741
  return scope?.kind === 'room' ? scope.locator : null;
1669
1742
  }
1670
- async function readBinary(request) {
1743
+ async function transferBinary(request, transfer, answer) {
1671
1744
  if (closed || refusal !== null)
1672
1745
  return UNCERTAIN;
1673
1746
  try {
@@ -1683,40 +1756,41 @@ export function composeRuntime(options) {
1683
1756
  ? `transfer binary ${request.locator} ${request.path}`
1684
1757
  : `transfer binary ${request.fileRef}`);
1685
1758
  try {
1686
- const result = await options.adapter.binary(authorization, request);
1687
- journal.record(`outcome binary ${result.kind}`);
1759
+ const result = await transfer(authorization);
1760
+ const settled = result;
1761
+ journal.record(`outcome binary ${settled.kind}`);
1688
1762
  const locator = binaryRoom(request);
1689
1763
  if (locator !== null && fencedSince(locator, since))
1690
1764
  return failure({ kind: 'room-not-readable', locator });
1691
- if (result.kind === 'scoped-refusal') {
1692
- const room = replicas.get(result.locator)?.confirmed.record;
1765
+ if (settled.kind === 'scoped-refusal') {
1766
+ const room = replicas.get(settled.locator)?.confirmed.record;
1693
1767
  if (room !== undefined)
1694
- await scoped(result, room, await openLocal());
1768
+ await scoped(settled, room, await openLocal());
1695
1769
  }
1696
- if (result.kind === 'revoked-authorization')
1770
+ if (settled.kind === 'revoked-authorization')
1697
1771
  await refuse('revoked-authorization');
1698
- return binaryAnswer(result);
1772
+ return answer(result);
1699
1773
  }
1700
1774
  catch {
1701
1775
  journal.record('outcome binary transport-uncertainty');
1702
1776
  return UNCERTAIN;
1703
1777
  }
1704
1778
  }
1705
- async function readBinaryPath(locator, path, revision) {
1706
- await ready;
1779
+ function readBinary(request) {
1780
+ return transferBinary(request, (held) => options.adapter.binary(held, request), binaryAnswer);
1781
+ }
1782
+ function binaryPath(locator, path, revision) {
1707
1783
  if (revision !== undefined)
1708
- return readBinary({ kind: 'path', locator, path, revision });
1784
+ return { kind: 'path', locator, path, revision };
1709
1785
  const visible = replicas.get(locator)?.visible;
1710
1786
  const held = visible?.binaries[path];
1711
1787
  if (visible === undefined || held === undefined)
1712
- return absentBinary(path);
1713
- if (held.state === 'pending') {
1714
- const blob = queuedBlob(locator, path, held.localId);
1715
- return blob === null ? absentBinary(path) : { kind: 'binary', blob };
1716
- }
1788
+ return null;
1789
+ if (held.state === 'pending')
1790
+ return { kind: 'pending', localId: held.localId };
1717
1791
  const record = binaryRecord(path, held, visible.revisions[path]);
1718
1792
  if (record.state !== 'accepted')
1719
- return absentBinary(path);
1793
+ return null;
1720
1794
  const replica = replicas.get(locator);
1721
1795
  const queued = (replica?.queue ?? []).flatMap((batch) => batch.changes);
1722
1796
  const holds = (at) => {
@@ -1731,13 +1805,31 @@ export function composeRuntime(options) {
1731
1805
  if (change.kind === 'move' && change.to === confirmed)
1732
1806
  confirmed = change.path;
1733
1807
  }
1734
- return readBinary({
1808
+ return {
1735
1809
  kind: 'path',
1736
1810
  locator,
1737
1811
  path: confirmed,
1738
1812
  revision: record.revision,
1739
1813
  fileRef: record.fileRef,
1740
- });
1814
+ };
1815
+ }
1816
+ async function readBinaryPath(locator, path, revision) {
1817
+ await ready;
1818
+ const asked = binaryPath(locator, path, revision);
1819
+ if (asked === null)
1820
+ return absentBinary(path);
1821
+ if (asked.kind === 'pending') {
1822
+ const blob = queuedBlob(locator, path, asked.localId);
1823
+ return blob === null ? absentBinary(path) : { kind: 'binary', blob };
1824
+ }
1825
+ return readBinary(asked);
1826
+ }
1827
+ async function linkBinaryPath(locator, path, revision) {
1828
+ await ready;
1829
+ const asked = binaryPath(locator, path, revision);
1830
+ if (asked === null || asked.kind === 'pending')
1831
+ return absentBinary(path);
1832
+ return transferBinary(asked, (held) => options.adapter.link(held, asked), binaryLinkAnswer);
1741
1833
  }
1742
1834
  async function readBinaryReference(reference) {
1743
1835
  await ready;
@@ -1763,7 +1855,7 @@ export function composeRuntime(options) {
1763
1855
  }
1764
1856
  return undefined;
1765
1857
  }
1766
- async function invokeAction(name, input) {
1858
+ async function invokeAction(name, input, dispatching) {
1767
1859
  await ready;
1768
1860
  if (closed || refusal !== null)
1769
1861
  return UNCERTAIN;
@@ -1777,6 +1869,7 @@ export function composeRuntime(options) {
1777
1869
  }
1778
1870
  if (closed || authorization === undefined || refusal !== null)
1779
1871
  return UNCERTAIN;
1872
+ await dispatching?.();
1780
1873
  journal.record(`transfer action ${name}`);
1781
1874
  try {
1782
1875
  const result = await options.adapter.action(authorization, { name, input });
@@ -1797,6 +1890,60 @@ export function composeRuntime(options) {
1797
1890
  return UNCERTAIN;
1798
1891
  }
1799
1892
  }
1893
+ function unsettled(result) {
1894
+ return result.kind !== 'action' && result.retryable;
1895
+ }
1896
+ async function recoveredAction(name, input) {
1897
+ const operation = operationOf(input);
1898
+ if (operation === null)
1899
+ return invokeAction(name, input);
1900
+ const kept = async () => (await openLocal()).keepCall({ operation, name, input });
1901
+ calling.add(operation);
1902
+ try {
1903
+ let result = await invokeAction(name, input, kept);
1904
+ if (unsettled(result)) {
1905
+ await recovery.attempt(`action ${operation}`, async () => {
1906
+ if (closed || refusal !== null)
1907
+ return 'done';
1908
+ if (!socketOpen)
1909
+ return 'again';
1910
+ result = await invokeAction(name, input, kept);
1911
+ return unsettled(result) ? 'again' : 'done';
1912
+ }, { again: true });
1913
+ }
1914
+ if (!unsettled(result))
1915
+ await (await openLocal()).dropCall(operation);
1916
+ return result;
1917
+ }
1918
+ finally {
1919
+ calling.delete(operation);
1920
+ }
1921
+ }
1922
+ async function resendCalls(signal) {
1923
+ if (!socketOpen || closed || stopping)
1924
+ return 'done';
1925
+ const local = await openLocal();
1926
+ for (const held of await local.calls()) {
1927
+ if (calling.has(held.operation))
1928
+ continue;
1929
+ if (closed || signal.aborted)
1930
+ return 'done';
1931
+ if (pastHorizon(held.operation)) {
1932
+ await local.dropCall(held.operation);
1933
+ continue;
1934
+ }
1935
+ calling.add(held.operation);
1936
+ try {
1937
+ if (unsettled(await invokeAction(held.name, held.input)))
1938
+ return 'again';
1939
+ await local.dropCall(held.operation);
1940
+ }
1941
+ finally {
1942
+ calling.delete(held.operation);
1943
+ }
1944
+ }
1945
+ return 'done';
1946
+ }
1800
1947
  function continuing() {
1801
1948
  return !closed && !discoveryPaused;
1802
1949
  }
@@ -2484,7 +2631,7 @@ export function composeRuntime(options) {
2484
2631
  caughtUpTo(room.locator);
2485
2632
  }
2486
2633
  }
2487
- async function discoverRooms(local, progress, attempt, live) {
2634
+ async function discoverRooms(local, progress, attempt, live, scope) {
2488
2635
  if (closed || authorization === undefined) {
2489
2636
  notifyReady();
2490
2637
  return;
@@ -2494,7 +2641,7 @@ export function composeRuntime(options) {
2494
2641
  const started = discoveries;
2495
2642
  let result;
2496
2643
  try {
2497
- result = await options.adapter.discover(authorization);
2644
+ result = await options.adapter.discover(authorization, scope);
2498
2645
  }
2499
2646
  catch {
2500
2647
  result = { kind: 'transport-uncertainty' };
@@ -2571,7 +2718,7 @@ export function composeRuntime(options) {
2571
2718
  member = signedIn;
2572
2719
  emitMember();
2573
2720
  }
2574
- await synchronize(await applyDiscovery(result.rooms, started), local, progress, attempt, live);
2721
+ await synchronize(await applyDiscovery(result.rooms, started, result.covered), local, progress, attempt, live);
2575
2722
  }
2576
2723
  async function ensureAuthorization() {
2577
2724
  if (authorization !== undefined)
@@ -2668,6 +2815,36 @@ export function composeRuntime(options) {
2668
2815
  options.adapter.roomStream?.reopen();
2669
2816
  });
2670
2817
  }
2818
+ function rediscover(scope) {
2819
+ return task(OPENING_LANE, async (live) => {
2820
+ if (!live() || !socketOpen)
2821
+ return;
2822
+ const progress = { complete: true, reachable: true };
2823
+ const gate = discover();
2824
+ moveTo(transferring(status));
2825
+ try {
2826
+ const local = await openLocal();
2827
+ if (!live())
2828
+ return;
2829
+ await ensureAuthorization();
2830
+ if (!live())
2831
+ return;
2832
+ await discoverRooms(local, progress, 'authorized', live, scope);
2833
+ }
2834
+ catch {
2835
+ progress.complete = false;
2836
+ }
2837
+ finally {
2838
+ gate.release();
2839
+ notifyReady();
2840
+ }
2841
+ if (!live())
2842
+ return;
2843
+ if (!progress.complete)
2844
+ complete = false;
2845
+ await rest(progress, live);
2846
+ });
2847
+ }
2671
2848
  function closedSocket(revoked, lost) {
2672
2849
  const opened = socketOpen;
2673
2850
  socketOpen = false;
@@ -2874,6 +3051,8 @@ export function composeRuntime(options) {
2874
3051
  await conversations.catchUp(reading);
2875
3052
  if (progress.reachable && reading.reachable)
2876
3053
  await conversations.resend(reading);
3054
+ if (progress.reachable)
3055
+ void recovery.attempt('calls', resendCalls);
2877
3056
  if (!reading.reachable) {
2878
3057
  progress.reachable = false;
2879
3058
  resendLater();
@@ -2995,6 +3174,8 @@ export function composeRuntime(options) {
2995
3174
  closedSocket(event.revoked, event.lost);
2996
3175
  else if (event.kind === 'opening')
2997
3176
  void applyOpening(event.expired);
3177
+ else if (event.kind === 'authority-changed')
3178
+ void rediscover(event.scope);
2998
3179
  else
2999
3180
  void applyRoom(event.locator, event.roomVersion);
3000
3181
  }, receipts);
@@ -3116,7 +3297,11 @@ export function composeRuntime(options) {
3116
3297
  if (options.adapter.roomStream === undefined)
3117
3298
  void sync('unstreamed');
3118
3299
  function planned(input, reach) {
3119
- return planOf(input, roomFacts(reach), planReach(reach));
3300
+ const plan = planOf(input, roomFacts(reach), planReach(reach));
3301
+ const named = plan.paths !== null &&
3302
+ plan.rooms !== null &&
3303
+ plan.paths.length * plan.rooms.length <= MAXIMUM_WINDOW;
3304
+ return plan.limit === null && named ? plan : { ...plan, limit: capped(plan.limit) };
3120
3305
  }
3121
3306
  function watchPlan(plan, deliver) {
3122
3307
  let seeded = false;
@@ -3153,7 +3338,8 @@ export function composeRuntime(options) {
3153
3338
  };
3154
3339
  }
3155
3340
  async function searched(input, reach) {
3156
- const plan = searchPlanOf(input);
3341
+ const asked = searchPlanOf(input);
3342
+ const plan = { ...asked, limit: capped(asked.limit) };
3157
3343
  const filter = filterOf(plan.documents ?? {}, roomFacts(reach), planReach(reach));
3158
3344
  for (;;) {
3159
3345
  const cover = covered(filter);
@@ -3212,7 +3398,9 @@ export function composeRuntime(options) {
3212
3398
  index: searchIndex.generation(),
3213
3399
  overlay: answered.coverage.overlay,
3214
3400
  };
3215
- return complete ? { ...hits, coverage } : { pending, ...hits, coverage };
3401
+ return complete
3402
+ ? { ...hits, coverage }
3403
+ : { pending, documents: hits.documents, messages: hits.messages, coverage };
3216
3404
  }
3217
3405
  }
3218
3406
  const watch = (topic, input, deliver) => {
@@ -3397,27 +3585,31 @@ export function composeRuntime(options) {
3397
3585
  });
3398
3586
  },
3399
3587
  write(app, value) {
3400
- return exclusive(async () => {
3401
- const local = await openLocal();
3402
- const stored = structuredClone(value);
3403
- await local.present({ app, value: stored });
3404
- presentations.set(app, stored);
3405
- publishChangeSet(changeSet('', null, null, { presentation: app }));
3406
- });
3588
+ return keepPresentation(app, () => structuredClone(value));
3589
+ },
3590
+ edit(app, members) {
3591
+ return keepPresentation(app, (held) => Object.fromEntries([
3592
+ ...Object.entries(plain(held) ? held : {}).filter(([member]) => !Object.hasOwn(members, member)),
3593
+ ...Object.entries(structuredClone(members)).filter(([, value]) => value !== null),
3594
+ ]));
3407
3595
  },
3408
3596
  },
3409
3597
  save(room, named, writtenBy) {
3410
3598
  return exclusive(() => saveRoomDrafts(room, named, writtenBy));
3411
3599
  },
3600
+ saveParts(room, parts, writtenBy) {
3601
+ return exclusive(() => saveParts(room, parts, writtenBy));
3602
+ },
3412
3603
  history(locator, path, query) {
3413
3604
  return readHistory(locator, path, query);
3414
3605
  },
3415
3606
  binaryFile: {
3416
3607
  read: readBinaryPath,
3608
+ link: linkBinaryPath,
3417
3609
  readReference: readBinaryReference,
3418
3610
  },
3419
3611
  call(name, input) {
3420
- return invokeAction(name, input);
3612
+ return recoveredAction(name, input);
3421
3613
  },
3422
3614
  reached(locator, roomVersion, path) {
3423
3615
  return path === undefined
@@ -36,10 +36,14 @@ export type ChatDraft = {
36
36
  };
37
37
  export type ConversationOptions = {
38
38
  limit?: number;
39
+ after?: string;
40
+ total?: true;
39
41
  };
40
42
  export type ConversationView = {
41
43
  conversation: string;
42
44
  messages: readonly ConversationMessage[];
45
+ next?: string;
46
+ total?: number;
43
47
  turn: RunningTurn | null;
44
48
  questions: readonly unknown[];
45
49
  process: {
@@ -64,6 +68,7 @@ export type ConversationSurface = {
64
68
  };
65
69
  export type ChatDraftSurface = {
66
70
  write(conversation: string, draft: ChatDraft, revision?: string): Promise<void>;
71
+ prepend(conversation: string, draft: ChatDraft): Promise<void>;
67
72
  discard(conversation: string): Promise<void>;
68
73
  };
69
74
  export type ConversationDependencies = {