@agentshouse/mdruntime-adapter 0.1.0-c1ae4a2aa7934 → 0.1.0-c1e7ca2d5752d

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 (83) hide show
  1. package/README.md +3 -3
  2. package/dist/actions.d.ts +6 -6
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +25 -326
  5. package/dist/adapter.d.ts +1 -1
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +281 -213
  8. package/dist/app-reference.d.ts +1 -0
  9. package/dist/app-reference.d.ts.map +1 -0
  10. package/dist/app-reference.js +2 -0
  11. package/dist/authored-path.d.ts +10 -0
  12. package/dist/authored-path.d.ts.map +1 -0
  13. package/dist/authored-path.js +35 -0
  14. package/dist/chunks.d.ts +37 -0
  15. package/dist/chunks.d.ts.map +1 -0
  16. package/dist/chunks.js +247 -0
  17. package/dist/crockford.d.ts +3 -0
  18. package/dist/crockford.d.ts.map +1 -0
  19. package/dist/crockford.js +3 -0
  20. package/dist/digest.d.ts +0 -1
  21. package/dist/discovery.d.ts +26 -20
  22. package/dist/discovery.d.ts.map +1 -1
  23. package/dist/discovery.js +109 -58
  24. package/dist/history.d.ts +11 -0
  25. package/dist/history.d.ts.map +1 -0
  26. package/dist/history.js +92 -0
  27. package/dist/house-model.d.ts +830 -0
  28. package/dist/house-model.d.ts.map +1 -0
  29. package/dist/house-model.js +465 -0
  30. package/dist/index.d.ts +12 -3
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +11 -1
  33. package/dist/map.d.ts +0 -1
  34. package/dist/model.d.ts +5 -5
  35. package/dist/model.d.ts.map +1 -1
  36. package/dist/model.js +40 -33
  37. package/dist/read.d.ts +63 -43
  38. package/dist/read.d.ts.map +1 -1
  39. package/dist/read.js +179 -262
  40. package/dist/refusal.d.ts +5 -12
  41. package/dist/refusal.d.ts.map +1 -1
  42. package/dist/refusal.js +31 -18
  43. package/dist/room-types.d.ts +4 -0
  44. package/dist/room-types.d.ts.map +1 -0
  45. package/dist/room-types.js +135 -0
  46. package/dist/search.d.ts +4 -0
  47. package/dist/search.d.ts.map +1 -0
  48. package/dist/search.js +47 -0
  49. package/dist/session-content/bounds.d.ts +7 -0
  50. package/dist/session-content/check.d.ts +18 -0
  51. package/dist/session-content/index.d.ts +7 -0
  52. package/dist/session-content/parts.d.ts +102 -0
  53. package/dist/session-content/state.d.ts +61 -0
  54. package/dist/session-content/turn.d.ts +328 -0
  55. package/dist/site.d.ts +1 -2
  56. package/dist/site.d.ts.map +1 -1
  57. package/dist/site.js +125 -105
  58. package/dist/space-schema.d.ts +6 -0
  59. package/dist/space-schema.d.ts.map +1 -0
  60. package/dist/space-schema.js +18 -0
  61. package/dist/stream.d.ts +15 -13
  62. package/dist/stream.d.ts.map +1 -1
  63. package/dist/stream.js +276 -149
  64. package/dist/transport.d.ts +98 -29
  65. package/dist/transport.d.ts.map +1 -1
  66. package/dist/transport.js +55 -10
  67. package/dist/upload.d.ts +11 -0
  68. package/dist/upload.d.ts.map +1 -0
  69. package/dist/upload.js +63 -0
  70. package/dist/version.d.ts +1 -1
  71. package/dist/version.d.ts.map +1 -1
  72. package/dist/version.js +1 -1
  73. package/dist/write.d.ts +20 -12
  74. package/dist/write.d.ts.map +1 -1
  75. package/dist/write.js +170 -65
  76. package/package.json +9 -6
  77. package/AGENTS.md +0 -232
  78. package/dist/gather.d.ts +0 -3
  79. package/dist/gather.d.ts.map +0 -1
  80. package/dist/gather.js +0 -40
  81. package/dist/links.d.ts +0 -20
  82. package/dist/links.d.ts.map +0 -1
  83. package/dist/links.js +0 -104
package/dist/stream.d.ts CHANGED
@@ -1,22 +1,24 @@
1
- import type { ConversationStream, RefreshPage, RoomStreamEvent, ServiceModeSignal } from '@agentshouse/mdruntime';
1
+ import { type AuthorityChange, type Discovery, type ConversationStream, type HeldRoom, type RoomStreamEvent, type ServiceModeSignal } from '@agentshouse/mdruntime';
2
2
  import type { TrackedAuthority } from './discovery.js';
3
- import type { HouseDoor, HouseHeldRoom, HouseMode, HouseStreamEntry } from './transport.js';
4
- import { type LogPosition } from './version.js';
5
- export type HeldRevision = (path: string, revision: string) => boolean;
6
- export type StreamedAdvancement = {
7
- page: RefreshPage;
8
- entries: readonly HouseStreamEntry[];
9
- schemas: ReadonlyMap<string, HouseStreamEntry>;
3
+ import type { SpaceSchemas } from './space-schema.js';
4
+ import { type HouseDoor, type HouseMode, type HouseStreamFrame } from './transport.js';
5
+ export type HouseOpeningFrame = Extract<HouseStreamFrame, {
6
+ type: 'opening';
7
+ }>;
8
+ export type HouseAuthorityChangedFrame = Extract<HouseStreamFrame, {
9
+ type: 'authority_changed';
10
+ }>;
11
+ export type HouseDiscovering = {
12
+ opened(frame: HouseOpeningFrame | null): Promise<Discovery | null>;
13
+ changed(frame: HouseAuthorityChangedFrame): AuthorityChange | null;
14
+ expired?(authorities: readonly string[]): void;
10
15
  };
11
16
  export type HouseNotifications = {
12
17
  runtime: string;
13
18
  observe(notify: (event: RoomStreamEvent) => void): () => void;
14
- send(rooms: readonly HouseHeldRoom[]): void;
19
+ send(rooms: readonly HeldRoom[]): void;
15
20
  track(authorities: readonly TrackedAuthority[]): void;
16
- reach(authority: string, at: LogPosition): void;
17
- advance(tracked: TrackedAuthority, from: LogPosition, holds?: HeldRevision): StreamedAdvancement | null;
18
21
  conversations: ConversationStream;
19
22
  serviceMode: ServiceModeSignal;
20
23
  };
21
- export declare function houseNotifications(transport: HouseDoor, mode: HouseMode): HouseNotifications;
22
- //# sourceMappingURL=stream.d.ts.map
24
+ export declare function houseNotifications(transport: HouseDoor, mode: HouseMode, discovering: HouseDiscovering, spaces: SpaceSchemas): HouseNotifications;
@@ -1 +1 @@
1
- {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,kBAAkB,EAElB,WAAW,EACX,eAAe,EAEf,iBAAiB,EAElB,MAAM,wBAAwB,CA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ChC,CAAC;AA+FF,wBAAgB,kBAAkB,CAChC,SAAS,EAAE,SAAS,EACpB,IAAI,EAAE,SAAS,EACf,WAAW,EAAE,gBAAgB,EAC7B,MAAM,EAAE,YAAY,GACnB,kBAAkB,CA0XpB"}
package/dist/stream.js CHANGED
@@ -1,47 +1,128 @@
1
- import { acceptedMessage } from './actions.js';
2
- import { headChange } from './read.js';
3
- import { roomVersion } from './version.js';
4
- const HELD_FRAMES_MAX = 256;
1
+ import { ROW_WEIGHT, stepped, } from '@agentshouse/mdruntime';
2
+ import { acceptedMessage, cursorPosition, houseHistory } from './history.js';
3
+ import { keptSpaceSchemas, streamedModels } from './model.js';
4
+ import { answered, answering, binaryFileOf, createdOf, folded, forgotten, headChange, revised, } from './read.js';
5
+ import { classifyDoor } from './refusal.js';
6
+ import { houseDoorRoute, } from './transport.js';
7
+ import { DECIMAL, logPosition, presented, roomVersion } from './version.js';
5
8
  function turnOperation(frame) {
6
- const { type: _type, conversation_id: _conversation, turn_id: _turn, ...operation } = frame;
7
- return structuredClone(operation);
9
+ return 'at' in frame ? { op: 'begin', at: frame.at } : structuredClone(frame.operation);
10
+ }
11
+ async function houseAgents(transport) {
12
+ const answer = await transport.door(houseDoorRoute('agents/list'), {});
13
+ if (classifyDoor(answer.status, answer.body, 'agents/list')?.kind === 'transport-uncertainty') {
14
+ throw new Error();
15
+ }
16
+ const agents = answer.status === 200 ? answer.body?.agents : null;
17
+ if (!Array.isArray(agents))
18
+ return null;
19
+ return agents.flatMap((one) => {
20
+ const { id, name, retired_at: retired } = (one ?? {});
21
+ return typeof id === 'string' && typeof name === 'string'
22
+ ? [{ agent: id, name, retired: retired !== null && retired !== undefined }]
23
+ : [];
24
+ });
8
25
  }
9
26
  function listedConversation(listed) {
10
27
  return {
11
28
  conversation: listed.conversation_id,
12
29
  agent: listed.agent_id,
30
+ agentName: listed.agent_name,
31
+ agentRetired: listed.agent_retired,
13
32
  name: listed.name,
14
33
  created: listed.created_at,
15
34
  finished: listed.finished_at,
16
35
  outcome: listed.outcome,
36
+ waiting: listed.waiting,
37
+ newest: listed.newest === null ? null : cursorPosition(listed.newest),
38
+ processEnded: listed.process_ended_at,
17
39
  };
18
40
  }
19
41
  function position(value) {
20
- if (!/^(?:0|[1-9][0-9]*)$/.test(value))
42
+ if (!DECIMAL.test(value))
21
43
  return null;
22
44
  return BigInt(value);
23
45
  }
24
- export function houseNotifications(transport, mode) {
46
+ function* changed(tracked, entries, version, schemas) {
47
+ const answer = answering();
48
+ for (const entry of entries) {
49
+ yield ROW_WEIGHT + (entry.text?.length ?? 0);
50
+ if (entry.kind === 'folder') {
51
+ folded(answer, entry.path, entry.removed === true);
52
+ continue;
53
+ }
54
+ if (entry.path === tracked.schema)
55
+ schemas.set(entry.path, entry);
56
+ forgotten(answer, entry.path);
57
+ if (entry.space !== undefined) {
58
+ answer.removals.add(entry.path);
59
+ continue;
60
+ }
61
+ if (entry.kind === 'removed') {
62
+ answer.removals.add(entry.path);
63
+ continue;
64
+ }
65
+ const binary = binaryFileOf(entry);
66
+ if (binary === undefined)
67
+ answer.documents.set(entry.path, entry.text ?? '');
68
+ else if (binary !== null)
69
+ answer.binaryFiles.set(entry.path, binary);
70
+ if (binary === undefined && entry.facts !== undefined)
71
+ answer.facts.set(entry.path, entry.facts);
72
+ const revision = entry.revision ?? binary?.sha256;
73
+ if (revision !== undefined)
74
+ revised(answer, entry.path, revision);
75
+ const head = headChange(entry);
76
+ if (head)
77
+ answer.changes.set(entry.path, head);
78
+ const created = createdOf(entry);
79
+ if (created)
80
+ answer.created.set(entry.path, created);
81
+ }
82
+ return { kind: 'net-advancement', roomVersion: version, ...(yield* answered(answer)) };
83
+ }
84
+ async function changePage(tracked, entries, version, mode, spaces, whole) {
85
+ const schemas = new Map();
86
+ const page = await stepped(changed(tracked, entries, version, schemas));
87
+ const models = await streamedModels(schemas, tracked, mode, spaces, whole);
88
+ return models === undefined ? page : { ...page, models };
89
+ }
90
+ export function houseNotifications(transport, mode, discovering, spaces) {
25
91
  const stream = transport.stream;
26
- const lines = new Map();
27
- const locators = new Map();
92
+ const staged = new Map();
93
+ let socket = null;
94
+ const tracking = new Map();
28
95
  const observers = new Set();
96
+ const replaying = new Map();
29
97
  const conversationObservers = new Set();
30
98
  const modeObservers = new Set();
31
99
  let replicated = [];
100
+ let listedCursor = null;
101
+ const unanswered = [];
102
+ const heldAtZero = new Set();
32
103
  const live = new Set();
33
104
  let opening = null;
105
+ let announced = null;
34
106
  const runtime = crypto.randomUUID();
35
107
  function emit(event) {
36
- for (const observer of observers)
37
- observer(event);
108
+ for (const observer of observers) {
109
+ const held = replaying.get(observer);
110
+ if (held === undefined)
111
+ observer(event);
112
+ else
113
+ held.push(event);
114
+ }
38
115
  }
39
116
  function emitConversation(event) {
40
117
  if (event.kind === 'turn' ||
41
118
  event.kind === 'questions' ||
42
119
  event.kind === 'process' ||
43
120
  event.kind === 'commands' ||
44
- event.kind === 'options') {
121
+ event.kind === 'options' ||
122
+ event.kind === 'starting' ||
123
+ event.kind === 'state' ||
124
+ event.kind === 'link' ||
125
+ event.kind === 'deliveries') {
45
126
  live.add(event.conversation);
46
127
  }
47
128
  for (const observer of conversationObservers)
@@ -49,11 +130,33 @@ export function houseNotifications(transport, mode) {
49
130
  }
50
131
  function reopened(next) {
51
132
  opening = next;
133
+ announced = null;
134
+ socket = next;
135
+ staged.clear();
136
+ unanswered.length = 0;
137
+ heldAtZero.clear();
52
138
  const gapped = new Set([...replicated, ...live]);
53
139
  live.clear();
54
140
  for (const conversation of gapped)
55
141
  emitConversation({ kind: 'gap', conversation });
56
142
  emit({ kind: 'opened' });
143
+ if (mode === 'site')
144
+ announce(next, null);
145
+ }
146
+ function announce(via, frame) {
147
+ void discovering.opened(frame).then((discovery) => {
148
+ if (socket !== via)
149
+ return;
150
+ if (discovery === null) {
151
+ via.close();
152
+ return;
153
+ }
154
+ announced = { kind: 'discovered', discovery };
155
+ emit(announced);
156
+ }, () => {
157
+ if (socket === via)
158
+ via.close();
159
+ });
57
160
  }
58
161
  function converse(frame) {
59
162
  const conversation = frame.conversation_id;
@@ -72,6 +175,7 @@ export function houseNotifications(transport, mode) {
72
175
  return;
73
176
  emitConversation({ kind: 'commands', conversation, commands: [] });
74
177
  emitConversation({ kind: 'options', conversation, options: [] });
178
+ emitConversation({ kind: 'state', conversation, state: null });
75
179
  return;
76
180
  }
77
181
  if (frame.type === 'commands') {
@@ -86,6 +190,22 @@ export function houseNotifications(transport, mode) {
86
190
  emitConversation({ kind: 'options', conversation, options: structuredClone(frame.options) });
87
191
  return;
88
192
  }
193
+ if (frame.type === 'starting') {
194
+ emitConversation({ kind: 'starting', conversation, step: frame.step });
195
+ return;
196
+ }
197
+ if (frame.type === 'state') {
198
+ emitConversation({ kind: 'state', conversation, state: structuredClone(frame.state) });
199
+ return;
200
+ }
201
+ if (frame.type === 'link') {
202
+ emitConversation({ kind: 'link', conversation, connected: frame.connected });
203
+ return;
204
+ }
205
+ if (frame.type === 'deliveries') {
206
+ emitConversation({ kind: 'deliveries', conversation, unknown: [...frame.unknown] });
207
+ return;
208
+ }
89
209
  if (frame.type === 'interactions') {
90
210
  emitConversation({
91
211
  kind: 'questions',
@@ -99,18 +219,33 @@ export function houseNotifications(transport, mode) {
99
219
  return;
100
220
  emitConversation({ kind: 'accepted-message', conversation, message });
101
221
  }
102
- function announce(authority, at) {
103
- const version = roomVersion(at);
104
- for (const locator of locators.get(authority) ?? []) {
105
- emit({ kind: 'room-version', locator, roomVersion: version });
222
+ function stage(frame, via) {
223
+ const held = staged.get(frame.carry);
224
+ if (frame.index !== (held?.parts ?? 0)) {
225
+ staged.delete(frame.carry);
226
+ return;
227
+ }
228
+ if (held === undefined) {
229
+ for (const [carry, other] of staged) {
230
+ if (other.authority === frame.authority)
231
+ staged.delete(carry);
232
+ }
106
233
  }
234
+ const next = held ?? { authority: frame.authority, parts: 0, entries: [] };
235
+ keptSpaceSchemas(frame.entries, spaces);
236
+ next.entries.push(...frame.entries);
237
+ next.parts += 1;
238
+ staged.set(frame.carry, next);
239
+ via?.received(frame.authority, frame.carry, frame.index);
107
240
  }
108
- function interrupt(authority) {
109
- const line = lines.get(authority);
110
- if (line !== undefined)
111
- lines.set(authority, { ...line, frames: [] });
241
+ function carried(frame) {
242
+ const held = staged.get(frame.carry);
243
+ staged.delete(frame.carry);
244
+ if ((held?.parts ?? 0) !== frame.parts)
245
+ return;
246
+ publish(frame, held?.entries ?? []);
112
247
  }
113
- function receive(frame) {
248
+ function handle(frame, via) {
114
249
  if (frame.type === 'signal') {
115
250
  const mode = frame.service_mode;
116
251
  if (mode !== 'open' && mode !== 'write-halted')
@@ -120,14 +255,35 @@ export function houseNotifications(transport, mode) {
120
255
  return;
121
256
  }
122
257
  if (frame.type === 'opening') {
258
+ if (via !== null && mode === 'credential')
259
+ announce(via, frame);
260
+ return;
261
+ }
262
+ if (frame.type === 'caught_up') {
263
+ discovering.expired?.(frame.expired);
123
264
  emit({
124
- kind: 'opening',
125
- expired: frame.expired.flatMap((authority) => locators.get(authority) ?? []),
265
+ kind: 'caught-up',
266
+ expired: [
267
+ ...frame.expired.flatMap((authority) => (tracking.get(authority) ?? []).map((tracked) => tracked.locator)),
268
+ ...(unanswered.shift() ?? []),
269
+ ],
126
270
  });
127
271
  return;
128
272
  }
273
+ if (frame.type === 'authority_changed') {
274
+ const change = discovering.changed(frame);
275
+ if (change !== null)
276
+ emit(change);
277
+ return;
278
+ }
129
279
  if (frame.type === 'conversations') {
130
- emitConversation({ kind: 'list', entries: frame.conversations.map(listedConversation) });
280
+ emitConversation({
281
+ kind: 'list',
282
+ entries: frame.conversations.map(listedConversation),
283
+ whole: frame.whole,
284
+ more: frame.more,
285
+ cursor: frame.cursor,
286
+ });
131
287
  return;
132
288
  }
133
289
  if (frame.type === 'conversation') {
@@ -142,40 +298,49 @@ export function houseNotifications(transport, mode) {
142
298
  frame.type === 'process' ||
143
299
  frame.type === 'commands' ||
144
300
  frame.type === 'options' ||
301
+ frame.type === 'starting' ||
302
+ frame.type === 'state' ||
303
+ frame.type === 'link' ||
304
+ frame.type === 'deliveries' ||
145
305
  frame.type === 'interactions' ||
146
306
  frame.type === 'message') {
147
307
  converse(frame);
148
308
  return;
149
309
  }
150
- if (frame.type !== 'entries')
151
- return;
310
+ if (frame.type === 'part')
311
+ stage(frame, via);
312
+ else if (frame.type === 'carried')
313
+ carried(frame);
314
+ }
315
+ function publish(frame, entries) {
152
316
  const at = position(frame.position);
153
317
  const after = position(frame.after);
154
- if (at === null || after === null || after >= at) {
155
- interrupt(frame.authority);
318
+ if (at === null || after === null || after >= at)
156
319
  return;
320
+ const from = after === 0n && !heldAtZero.has(frame.authority)
321
+ ? null
322
+ : roomVersion({ position: frame.after, logEpoch: frame.log_epoch });
323
+ const version = roomVersion({ position: frame.position, logEpoch: frame.log_epoch });
324
+ for (const tracked of tracking.get(frame.authority) ?? []) {
325
+ emit({
326
+ kind: 'room-change',
327
+ locator: tracked.locator,
328
+ from,
329
+ roomVersion: version,
330
+ read: () => changePage(tracked, entries, version, mode, spaces, from === null),
331
+ });
157
332
  }
158
- const line = lines.get(frame.authority);
159
- if (line?.logEpoch === frame.log_epoch) {
160
- if (line.held !== null && at <= line.held)
161
- return;
162
- const last = line.frames.at(-1);
163
- if (last !== undefined && at <= last.position)
164
- return;
165
- const held = { after, position: at, entries: frame.entries };
166
- const next = last !== undefined && after <= last.position ? [...line.frames, held] : [held];
167
- lines.set(frame.authority, { ...line, frames: next.length > HELD_FRAMES_MAX ? [] : next });
168
- }
169
- else {
170
- interrupt(frame.authority);
171
- }
172
- announce(frame.authority, { position: frame.position, logEpoch: frame.log_epoch });
173
333
  }
174
- stream.observe(receive);
334
+ stream.observe((frame) => handle(frame, socket));
175
335
  stream.opened(reopened);
176
- stream.closed((revoked) => {
336
+ stream.closed(({ revoked, lost, authorityChanged }) => {
177
337
  opening = null;
178
- emit({ kind: 'closed', revoked });
338
+ announced = null;
339
+ socket = null;
340
+ staged.clear();
341
+ unanswered.length = 0;
342
+ heldAtZero.clear();
343
+ emit({ kind: 'closed', revoked, lost, authorityChanged });
179
344
  });
180
345
  return {
181
346
  runtime,
@@ -191,19 +356,34 @@ export function houseNotifications(transport, mode) {
191
356
  replicate(next) {
192
357
  replicated = [...next];
193
358
  },
359
+ hold(cursor) {
360
+ listedCursor = cursor;
361
+ },
194
362
  observe(notify) {
195
363
  conversationObservers.add(notify);
196
364
  return () => {
197
365
  conversationObservers.delete(notify);
198
366
  };
199
367
  },
368
+ ...(mode === 'credential'
369
+ ? {
370
+ agents: () => houseAgents(transport),
371
+ history: (request) => houseHistory(transport, request),
372
+ }
373
+ : {}),
200
374
  },
201
375
  observe(notify) {
202
376
  observers.add(notify);
203
377
  if (opening !== null) {
378
+ const replayed = [{ kind: 'opened' }];
379
+ if (announced !== null)
380
+ replayed.push(announced);
381
+ replaying.set(notify, replayed);
204
382
  queueMicrotask(() => {
205
- if (observers.has(notify))
206
- notify({ kind: 'opened' });
383
+ replaying.delete(notify);
384
+ for (const event of replayed)
385
+ if (observers.has(notify))
386
+ notify(event);
207
387
  });
208
388
  }
209
389
  return () => {
@@ -211,112 +391,59 @@ export function houseNotifications(transport, mode) {
211
391
  };
212
392
  },
213
393
  send(rooms) {
214
- for (const room of rooms) {
215
- const line = lines.get(room.authority);
216
- const held = position(room.position);
217
- if (line === undefined || held === null || line.logEpoch !== room.log_epoch)
218
- continue;
219
- lines.set(room.authority, { ...line, frames: [], held });
220
- }
221
- const current = opening;
222
394
  opening = null;
223
- current?.hold(rooms, runtime);
224
- },
225
- track(entries) {
226
- locators.clear();
227
- for (const entry of entries) {
228
- locators.set(entry.authority, [...(locators.get(entry.authority) ?? []), entry.locator]);
229
- if (lines.get(entry.authority)?.logEpoch !== entry.logEpoch) {
230
- lines.set(entry.authority, { logEpoch: entry.logEpoch, frames: [], held: null });
231
- }
232
- }
233
- for (const authority of lines.keys()) {
234
- if (!locators.has(authority))
235
- lines.delete(authority);
236
- }
237
- },
238
- reach(authority, at) {
239
- const line = lines.get(authority);
240
- const held = position(at.position);
241
- if (line === undefined || held === null || line.logEpoch !== at.logEpoch)
395
+ announced = null;
396
+ if (socket === null)
242
397
  return;
243
- if (line.held !== null && held <= line.held)
398
+ const entries = new Map([...tracking.values()].flat().map((tracked) => [tracked.locator, tracked]));
399
+ const named = rooms.flatMap((room) => {
400
+ const entry = entries.get(room.locator);
401
+ const at = room.roomVersion === null ? null : logPosition(room.roomVersion);
402
+ return entry === undefined || at === null ? [] : [{ entry, at }];
403
+ });
404
+ const unheld = rooms.flatMap((room) => {
405
+ const entry = entries.get(room.locator);
406
+ return entry === undefined || room.roomVersion !== null ? [] : [entry];
407
+ });
408
+ for (const { entry, at } of named) {
409
+ if (at.position === '0')
410
+ heldAtZero.add(entry.authority);
411
+ else
412
+ heldAtZero.delete(entry.authority);
413
+ }
414
+ for (const entry of unheld)
415
+ heldAtZero.delete(entry.authority);
416
+ const site = mode === 'site';
417
+ unanswered.push(site ? unheld.map((entry) => entry.locator) : []);
418
+ socket.hold([
419
+ ...named.map(({ entry, at }) => presented(entry.authority, at)),
420
+ ...(site ? [] : unheld).map((entry) => presented(entry.authority, { position: '0', logEpoch: entry.logEpoch })),
421
+ ], runtime, listedCursor);
422
+ if (site)
244
423
  return;
245
- lines.set(authority, {
246
- ...line,
247
- frames: line.frames.filter((frame) => frame.position > held),
248
- held,
424
+ const made = unheld.filter((entry) => entry.position === '0');
425
+ queueMicrotask(() => {
426
+ for (const entry of made) {
427
+ const version = roomVersion(entry);
428
+ emit({
429
+ kind: 'room-change',
430
+ locator: entry.locator,
431
+ from: null,
432
+ roomVersion: version,
433
+ read: () => changePage(entry, [], version, mode, spaces, true),
434
+ });
435
+ }
249
436
  });
250
437
  },
251
- advance(tracked, from, holds) {
252
- const line = lines.get(tracked.authority);
253
- const start = position(from.position);
254
- if (line === undefined || start === null || line.logEpoch !== from.logEpoch)
255
- return null;
256
- const pending = line.frames.filter((frame) => frame.position > start);
257
- const first = pending[0];
258
- if (first === undefined || first.after > start)
259
- return null;
260
- const documents = {};
261
- const revisions = {};
262
- const changes = {};
263
- const removals = [];
264
- const applied = [];
265
- const schemas = new Map();
266
- const forget = (path) => {
267
- delete documents[path];
268
- delete revisions[path];
269
- delete changes[path];
270
- const removed = removals.indexOf(path);
271
- if (removed !== -1)
272
- removals.splice(removed, 1);
273
- };
274
- for (const frame of pending) {
275
- for (const entry of frame.entries) {
276
- if (entry.kind === 'edited' &&
277
- entry.revision !== undefined &&
278
- holds?.(entry.path, entry.revision) === true) {
279
- forget(entry.path);
280
- continue;
281
- }
282
- applied.push(entry);
283
- if (tracked.schemas.includes(entry.path))
284
- schemas.set(entry.path, entry);
285
- if (entry.kind === 'removed') {
286
- forget(entry.path);
287
- removals.push(entry.path);
288
- continue;
289
- }
290
- if (entry.text === undefined)
291
- return null;
292
- if (mode === 'credential' && entry.revision === undefined)
293
- return null;
294
- const head = headChange(entry);
295
- if (head === null)
296
- return null;
297
- forget(entry.path);
298
- documents[entry.path] = entry.text;
299
- if (entry.revision !== undefined)
300
- revisions[entry.path] = entry.revision;
301
- if (head !== undefined)
302
- changes[entry.path] = head;
303
- }
438
+ track(entries) {
439
+ tracking.clear();
440
+ for (const entry of entries) {
441
+ tracking.set(entry.authority, [...(tracking.get(entry.authority) ?? []), entry]);
442
+ }
443
+ for (const [carry, held] of staged) {
444
+ if (!tracking.has(held.authority))
445
+ staged.delete(carry);
304
446
  }
305
- const last = pending.at(-1);
306
- lines.set(tracked.authority, { ...line, frames: pending, held: last.position });
307
- return {
308
- page: {
309
- kind: 'net-advancement',
310
- roomVersion: roomVersion({ position: last.position.toString(), logEpoch: line.logEpoch }),
311
- documents,
312
- revisions,
313
- changes,
314
- binaryTargets: [],
315
- removals: removals.sort(),
316
- },
317
- entries: applied,
318
- schemas,
319
- };
320
447
  },
321
448
  };
322
449
  }