@agentshouse/mdruntime-adapter 0.1.0-c3bd45bca89d3 → 0.1.0-c3c5582303db5

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 (79) hide show
  1. package/README.md +3 -3
  2. package/dist/actions.d.ts +6 -7
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +45 -345
  5. package/dist/adapter.d.ts +1 -1
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +301 -252
  8. package/dist/authored-path.d.ts +61 -0
  9. package/dist/authored-path.d.ts.map +1 -0
  10. package/dist/authored-path.js +166 -0
  11. package/dist/chunks.d.ts +36 -0
  12. package/dist/chunks.d.ts.map +1 -0
  13. package/dist/chunks.js +281 -0
  14. package/dist/digest.d.ts +0 -1
  15. package/dist/discovery.d.ts +45 -18
  16. package/dist/discovery.d.ts.map +1 -1
  17. package/dist/discovery.js +254 -65
  18. package/dist/history.d.ts +6 -0
  19. package/dist/history.d.ts.map +1 -0
  20. package/dist/history.js +85 -0
  21. package/dist/index.d.ts +10 -3
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +9 -1
  24. package/dist/map.d.ts +0 -1
  25. package/dist/model.d.ts +6 -7
  26. package/dist/model.d.ts.map +1 -1
  27. package/dist/model.js +30 -50
  28. package/dist/read.d.ts +54 -53
  29. package/dist/read.d.ts.map +1 -1
  30. package/dist/read.js +209 -278
  31. package/dist/refusal.d.ts +6 -5
  32. package/dist/refusal.d.ts.map +1 -1
  33. package/dist/refusal.js +27 -10
  34. package/dist/renamed.d.ts +10 -0
  35. package/dist/renamed.d.ts.map +1 -0
  36. package/dist/renamed.js +20 -0
  37. package/dist/room-query.d.ts +4 -0
  38. package/dist/room-query.d.ts.map +1 -0
  39. package/dist/room-query.js +63 -0
  40. package/dist/search.d.ts +4 -0
  41. package/dist/search.d.ts.map +1 -0
  42. package/dist/search.js +47 -0
  43. package/dist/session-content/check.d.ts +20 -0
  44. package/dist/session-content/index.d.ts +6 -0
  45. package/dist/session-content/parts.d.ts +30 -0
  46. package/dist/session-content/state.d.ts +54 -0
  47. package/dist/session-content/turn.d.ts +147 -0
  48. package/dist/site.d.ts +1 -2
  49. package/dist/site.d.ts.map +1 -1
  50. package/dist/site.js +153 -107
  51. package/dist/space-schema.d.ts +6 -0
  52. package/dist/space-schema.d.ts.map +1 -0
  53. package/dist/space-schema.js +11 -0
  54. package/dist/stream.d.ts +15 -14
  55. package/dist/stream.d.ts.map +1 -1
  56. package/dist/stream.js +344 -153
  57. package/dist/transport.d.ts +152 -31
  58. package/dist/transport.d.ts.map +1 -1
  59. package/dist/transport.js +93 -9
  60. package/dist/uncertain.d.ts +5 -0
  61. package/dist/uncertain.d.ts.map +1 -0
  62. package/dist/uncertain.js +11 -0
  63. package/dist/upload.d.ts +17 -0
  64. package/dist/upload.d.ts.map +1 -0
  65. package/dist/upload.js +106 -0
  66. package/dist/version.d.ts +1 -1
  67. package/dist/version.d.ts.map +1 -1
  68. package/dist/version.js +1 -1
  69. package/dist/write.d.ts +14 -12
  70. package/dist/write.d.ts.map +1 -1
  71. package/dist/write.js +197 -137
  72. package/package.json +11 -7
  73. package/AGENTS.md +0 -269
  74. package/dist/gather.d.ts +0 -4
  75. package/dist/gather.d.ts.map +0 -1
  76. package/dist/gather.js +0 -45
  77. package/dist/links.d.ts +0 -20
  78. package/dist/links.d.ts.map +0 -1
  79. package/dist/links.js +0 -104
package/dist/stream.js CHANGED
@@ -1,10 +1,50 @@
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 { parse } from '@agentshouse/mdmodel';
2
+ import { ROW_WEIGHT, stepped, } from '@agentshouse/mdruntime';
3
+ import { acceptedMessage, changeCursor, houseHistory } from './history.js';
4
+ import { spaceSchemaTexts, streamedModels } from './model.js';
5
+ import { spaceSchemas } from './space-schema.js';
6
+ import { answered, answering, documentEntry, folded, forgotten, kept, } from './read.js';
7
+ import { DECIMAL, logPosition, presented, roomVersion } from './version.js';
8
+ const CURRENT_RUN_STATES = new Set(['awaiting_approval', 'started']);
9
+ const NO_RUN_MARKS = { rooms: {}, sent: null };
10
+ function heardRun(request) {
11
+ const read = parse(`---\n${request.document}---\n`);
12
+ if (!read.ok)
13
+ throw new Error(`the stream carried request ${request.name} as no document`);
14
+ const fields = read.document.fields;
15
+ return {
16
+ room: request.room_ref,
17
+ name: request.name,
18
+ document: request.document,
19
+ current: CURRENT_RUN_STATES.has(fields.state),
20
+ starter: typeof fields.requester === 'string' ? fields.requester : null,
21
+ };
22
+ }
5
23
  function turnOperation(frame) {
6
- const { type: _type, conversation_id: _conversation, turn_id: _turn, ...operation } = frame;
7
- return structuredClone(operation);
24
+ return 'at' in frame ? { op: 'begin', at: frame.at } : structuredClone(frame.operation);
25
+ }
26
+ function listedAgent(agent) {
27
+ return {
28
+ id: agent.id,
29
+ name: agent.name,
30
+ revision: agent.revision,
31
+ environment: agent.environment,
32
+ environment_name: agent.environment_name,
33
+ environment_connected: agent.environment_connected,
34
+ launch_directory: agent.launch_directory,
35
+ cli: agent.cli,
36
+ model: agent.model,
37
+ effort: agent.effort,
38
+ model_fixed: agent.model_fixed,
39
+ mode: agent.mode,
40
+ base_instructions: agent.base_instructions,
41
+ compaction_threshold: agent.compaction_threshold,
42
+ compaction_words: agent.compaction_words,
43
+ in_telegram: agent.in_telegram,
44
+ telegram_messages: agent.telegram_messages,
45
+ created: agent.created,
46
+ retired_at: agent.retired_at,
47
+ };
8
48
  }
9
49
  function listedConversation(listed) {
10
50
  return {
@@ -12,28 +52,88 @@ function listedConversation(listed) {
12
52
  agent: listed.agent_id,
13
53
  name: listed.name,
14
54
  created: listed.created_at,
15
- finished: listed.finished_at,
16
55
  outcome: listed.outcome,
56
+ waiting: listed.waiting,
57
+ newest: listed.newest === null ? null : changeCursor(listed.newest),
58
+ processEnded: listed.process_ended_at,
59
+ activity: listed.activity,
60
+ subject: listed.subject,
61
+ room: listed.room_ref,
62
+ model: listed.model,
63
+ effort: listed.effort,
17
64
  };
18
65
  }
19
66
  function position(value) {
20
- if (!/^(?:0|[1-9][0-9]*)$/.test(value))
67
+ if (!DECIMAL.test(value))
21
68
  return null;
22
69
  return BigInt(value);
23
70
  }
24
- export function houseNotifications(transport, mode) {
71
+ function* changed(tracked, entries, version, mode, schemas) {
72
+ const answer = answering();
73
+ for (const entry of entries) {
74
+ yield ROW_WEIGHT + (entry.content?.length ?? 0);
75
+ if (entry.kind === 'folder') {
76
+ folded(answer, entry.path, entry.removed === true);
77
+ continue;
78
+ }
79
+ if (entry.kind === 'removed') {
80
+ if (entry.path === tracked.schema)
81
+ schemas.set(entry.path, null);
82
+ forgotten(answer, entry.path);
83
+ answer.removals.add(entry.path);
84
+ continue;
85
+ }
86
+ const file = documentEntry(entry, mode);
87
+ if (file === null)
88
+ throw new Error(`the stream carried ${entry.path} as no file`);
89
+ if (entry.path === tracked.schema)
90
+ schemas.set(entry.path, file);
91
+ forgotten(answer, file.path);
92
+ if (file.space === undefined)
93
+ kept(answer, file);
94
+ else
95
+ answer.removals.add(file.path);
96
+ }
97
+ return { kind: 'net-advancement', roomVersion: version, ...(yield* answered(answer)) };
98
+ }
99
+ async function changePage(tracked, entries, version, mode, whole) {
100
+ const schemas = new Map();
101
+ const page = await stepped(changed(tracked, entries, version, mode, schemas));
102
+ const models = await streamedModels(schemas, tracked, whole);
103
+ return models === undefined ? page : { ...page, models };
104
+ }
105
+ export function houseNotifications(transport, mode, discovering) {
25
106
  const stream = transport.stream;
26
- const lines = new Map();
27
- const locators = new Map();
107
+ const staged = new Map();
108
+ let spaces = spaceSchemas();
109
+ let socket = null;
110
+ const tracking = new Map();
111
+ const located = new Map();
28
112
  const observers = new Set();
113
+ const replaying = new Map();
29
114
  const conversationObservers = new Set();
115
+ const runObservers = new Set();
30
116
  const modeObservers = new Set();
31
117
  let replicated = [];
118
+ let listedCursor = null;
119
+ let runMarks = NO_RUN_MARKS;
120
+ const unanswered = [];
121
+ const heldAtZero = new Set();
32
122
  const live = new Set();
33
123
  let opening = null;
124
+ let announced = null;
34
125
  const runtime = crypto.randomUUID();
35
126
  function emit(event) {
36
- for (const observer of observers)
127
+ for (const observer of observers) {
128
+ const held = replaying.get(observer);
129
+ if (held === undefined)
130
+ observer(event);
131
+ else
132
+ held.push(event);
133
+ }
134
+ }
135
+ function emitRun(event) {
136
+ for (const observer of runObservers)
37
137
  observer(event);
38
138
  }
39
139
  function emitConversation(event) {
@@ -41,7 +141,10 @@ export function houseNotifications(transport, mode) {
41
141
  event.kind === 'questions' ||
42
142
  event.kind === 'process' ||
43
143
  event.kind === 'commands' ||
44
- event.kind === 'options') {
144
+ event.kind === 'options' ||
145
+ event.kind === 'starting' ||
146
+ event.kind === 'state' ||
147
+ event.kind === 'deliveries') {
45
148
  live.add(event.conversation);
46
149
  }
47
150
  for (const observer of conversationObservers)
@@ -49,11 +152,34 @@ export function houseNotifications(transport, mode) {
49
152
  }
50
153
  function reopened(next) {
51
154
  opening = next;
155
+ announced = null;
156
+ socket = next;
157
+ staged.clear();
158
+ spaces = spaceSchemas();
159
+ unanswered.length = 0;
160
+ heldAtZero.clear();
52
161
  const gapped = new Set([...replicated, ...live]);
53
162
  live.clear();
54
163
  for (const conversation of gapped)
55
164
  emitConversation({ kind: 'gap', conversation });
56
165
  emit({ kind: 'opened' });
166
+ if (mode === 'site')
167
+ announce(next, null);
168
+ }
169
+ function announce(via, frame) {
170
+ void discovering.opened(frame).then((discovery) => {
171
+ if (socket !== via)
172
+ return;
173
+ if (discovery === null) {
174
+ via.close();
175
+ return;
176
+ }
177
+ announced = { kind: 'discovered', discovery };
178
+ emit(announced);
179
+ }, (failure) => {
180
+ if (socket === via)
181
+ via.close(failure);
182
+ });
57
183
  }
58
184
  function converse(frame) {
59
185
  const conversation = frame.conversation_id;
@@ -72,6 +198,7 @@ export function houseNotifications(transport, mode) {
72
198
  return;
73
199
  emitConversation({ kind: 'commands', conversation, commands: [] });
74
200
  emitConversation({ kind: 'options', conversation, options: [] });
201
+ emitConversation({ kind: 'state', conversation, state: null });
75
202
  return;
76
203
  }
77
204
  if (frame.type === 'commands') {
@@ -86,6 +213,23 @@ export function houseNotifications(transport, mode) {
86
213
  emitConversation({ kind: 'options', conversation, options: structuredClone(frame.options) });
87
214
  return;
88
215
  }
216
+ if (frame.type === 'starting') {
217
+ emitConversation({ kind: 'starting', conversation, step: frame.step });
218
+ return;
219
+ }
220
+ if (frame.type === 'state') {
221
+ emitConversation({ kind: 'state', conversation, state: structuredClone(frame.state) });
222
+ return;
223
+ }
224
+ if (frame.type === 'deliveries') {
225
+ emitConversation({
226
+ kind: 'deliveries',
227
+ conversation,
228
+ waiting: [...frame.waiting],
229
+ unknown: [...frame.unknown],
230
+ });
231
+ return;
232
+ }
89
233
  if (frame.type === 'interactions') {
90
234
  emitConversation({
91
235
  kind: 'questions',
@@ -99,18 +243,33 @@ export function houseNotifications(transport, mode) {
99
243
  return;
100
244
  emitConversation({ kind: 'accepted-message', conversation, message });
101
245
  }
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 });
246
+ function stage(frame, via) {
247
+ const entries = spaceSchemaTexts(frame.entries, spaces);
248
+ const held = staged.get(frame.carry);
249
+ if (frame.index !== (held?.parts ?? 0)) {
250
+ staged.delete(frame.carry);
251
+ return;
106
252
  }
253
+ if (held === undefined) {
254
+ for (const [carry, other] of staged) {
255
+ if (other.authority === frame.authority)
256
+ staged.delete(carry);
257
+ }
258
+ }
259
+ const next = held ?? { authority: frame.authority, parts: 0, entries: [] };
260
+ next.entries.push(...entries);
261
+ next.parts += 1;
262
+ staged.set(frame.carry, next);
263
+ via?.received(frame.authority, frame.carry, frame.index);
107
264
  }
108
- function interrupt(authority) {
109
- const line = lines.get(authority);
110
- if (line !== undefined)
111
- lines.set(authority, { ...line, frames: [] });
265
+ function carried(frame) {
266
+ const held = staged.get(frame.carry);
267
+ staged.delete(frame.carry);
268
+ if ((held?.parts ?? 0) !== frame.parts)
269
+ return;
270
+ publish(frame, held?.entries ?? []);
112
271
  }
113
- function receive(frame) {
272
+ function handle(frame, via) {
114
273
  if (frame.type === 'signal') {
115
274
  const mode = frame.service_mode;
116
275
  if (mode !== 'open' && mode !== 'write-halted')
@@ -120,20 +279,72 @@ export function houseNotifications(transport, mode) {
120
279
  return;
121
280
  }
122
281
  if (frame.type === 'opening') {
282
+ if (via !== null && mode === 'credential')
283
+ announce(via, frame);
284
+ return;
285
+ }
286
+ if (frame.type === 'caught_up') {
287
+ const expired = frame.expired.flatMap((authority) => tracking.get(authority) ?? []);
288
+ discovering.expired?.(expired);
123
289
  emit({
124
- kind: 'opening',
125
- expired: frame.expired.flatMap((authority) => locators.get(authority) ?? []),
290
+ kind: 'caught-up',
291
+ expired: [...expired.map(({ locator }) => locator), ...(unanswered.shift() ?? [])],
292
+ });
293
+ return;
294
+ }
295
+ if (frame.type === 'report') {
296
+ emit({ kind: 'reported' });
297
+ return;
298
+ }
299
+ if (frame.type === 'request') {
300
+ const room = frame.request.room_ref;
301
+ emitRun({
302
+ kind: 'run',
303
+ run: heardRun(frame.request),
304
+ listing: tracking.has(room) ? room : null,
305
+ });
306
+ return;
307
+ }
308
+ if (frame.type === 'request_removed') {
309
+ emitRun({ kind: 'removed', room: frame.room_ref, name: frame.request });
310
+ return;
311
+ }
312
+ if (frame.type === 'requests') {
313
+ emitRun({
314
+ kind: 'runs',
315
+ listing: frame.room_ref,
316
+ runs: frame.requests.map(heardRun),
317
+ whole: frame.whole,
318
+ more: frame.more,
319
+ mark: frame.cursor,
126
320
  });
127
321
  return;
128
322
  }
323
+ if (frame.type === 'authority_changed') {
324
+ const change = discovering.changed(frame);
325
+ if (change !== null)
326
+ emit(change);
327
+ return;
328
+ }
129
329
  if (frame.type === 'conversations') {
130
- emitConversation({ kind: 'list', entries: frame.conversations.map(listedConversation) });
330
+ emitConversation({
331
+ kind: 'list',
332
+ agents: frame.agents.map(listedAgent),
333
+ entries: frame.conversations.map(listedConversation),
334
+ whole: frame.whole,
335
+ more: frame.more,
336
+ cursor: frame.cursor,
337
+ });
131
338
  return;
132
339
  }
133
340
  if (frame.type === 'conversation') {
134
341
  emitConversation({ kind: 'listed', entry: listedConversation(frame.conversation) });
135
342
  return;
136
343
  }
344
+ if (frame.type === 'agent') {
345
+ emitConversation({ kind: 'agent', agent: listedAgent(frame.agent) });
346
+ return;
347
+ }
137
348
  if (frame.type === 'conversation_deleted') {
138
349
  emitConversation({ kind: 'unlisted', conversation: frame.conversation_id });
139
350
  return;
@@ -142,40 +353,49 @@ export function houseNotifications(transport, mode) {
142
353
  frame.type === 'process' ||
143
354
  frame.type === 'commands' ||
144
355
  frame.type === 'options' ||
356
+ frame.type === 'starting' ||
357
+ frame.type === 'state' ||
358
+ frame.type === 'deliveries' ||
145
359
  frame.type === 'interactions' ||
146
360
  frame.type === 'message') {
147
361
  converse(frame);
148
362
  return;
149
363
  }
150
- if (frame.type !== 'entries')
151
- return;
364
+ if (frame.type === 'part')
365
+ stage(frame, via);
366
+ else if (frame.type === 'carried')
367
+ carried(frame);
368
+ }
369
+ function publish(frame, entries) {
152
370
  const at = position(frame.position);
153
371
  const after = position(frame.after);
154
- if (at === null || after === null || after >= at) {
155
- interrupt(frame.authority);
372
+ if (at === null || after === null || after >= at)
156
373
  return;
374
+ const from = after === 0n && !heldAtZero.has(frame.authority)
375
+ ? null
376
+ : roomVersion({ position: frame.after, logEpoch: frame.log_epoch });
377
+ const version = roomVersion({ position: frame.position, logEpoch: frame.log_epoch });
378
+ for (const tracked of tracking.get(frame.authority) ?? []) {
379
+ emit({
380
+ kind: 'room-change',
381
+ locator: tracked.locator,
382
+ from,
383
+ roomVersion: version,
384
+ read: () => changePage(tracked, entries, version, mode, from === null),
385
+ });
157
386
  }
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
387
  }
174
- stream.observe(receive);
388
+ stream.observe((frame) => handle(frame, socket));
175
389
  stream.opened(reopened);
176
- stream.closed((revoked) => {
390
+ stream.closed(({ revoked, lost, authorityChanged }) => {
177
391
  opening = null;
178
- emit({ kind: 'closed', revoked });
392
+ announced = null;
393
+ socket = null;
394
+ staged.clear();
395
+ spaces = spaceSchemas();
396
+ unanswered.length = 0;
397
+ heldAtZero.clear();
398
+ emit({ kind: 'closed', revoked, lost, authorityChanged });
179
399
  });
180
400
  return {
181
401
  runtime,
@@ -191,19 +411,40 @@ export function houseNotifications(transport, mode) {
191
411
  replicate(next) {
192
412
  replicated = [...next];
193
413
  },
414
+ hold(cursor) {
415
+ listedCursor = cursor;
416
+ },
194
417
  observe(notify) {
195
418
  conversationObservers.add(notify);
196
419
  return () => {
197
420
  conversationObservers.delete(notify);
198
421
  };
199
422
  },
423
+ ...(mode === 'credential' ? { history: (request) => houseHistory(transport, request) } : {}),
424
+ },
425
+ runs: {
426
+ hold(marks) {
427
+ runMarks = marks;
428
+ },
429
+ observe(notify) {
430
+ runObservers.add(notify);
431
+ return () => {
432
+ runObservers.delete(notify);
433
+ };
434
+ },
200
435
  },
201
436
  observe(notify) {
202
437
  observers.add(notify);
203
438
  if (opening !== null) {
439
+ const replayed = [{ kind: 'opened' }];
440
+ if (announced !== null)
441
+ replayed.push(announced);
442
+ replaying.set(notify, replayed);
204
443
  queueMicrotask(() => {
205
- if (observers.has(notify))
206
- notify({ kind: 'opened' });
444
+ replaying.delete(notify);
445
+ for (const event of replayed)
446
+ if (observers.has(notify))
447
+ notify(event);
207
448
  });
208
449
  }
209
450
  return () => {
@@ -211,117 +452,67 @@ export function houseNotifications(transport, mode) {
211
452
  };
212
453
  },
213
454
  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
- opening = null;
223
- current?.hold(rooms, runtime);
224
- },
225
- reopen() {
226
- const current = opening;
227
455
  opening = null;
228
- current?.close();
456
+ announced = null;
457
+ if (socket === null)
458
+ return;
459
+ const named = rooms.flatMap((room) => {
460
+ const entry = located.get(room.locator);
461
+ const at = room.roomVersion === null ? null : logPosition(room.roomVersion);
462
+ return entry === undefined || at === null ? [] : [{ entry, at }];
463
+ });
464
+ const unheld = rooms.flatMap((room) => {
465
+ const entry = located.get(room.locator);
466
+ return entry === undefined || room.roomVersion !== null ? [] : [entry];
467
+ });
468
+ for (const { entry, at } of named) {
469
+ if (at.position === '0')
470
+ heldAtZero.add(entry.authority);
471
+ else
472
+ heldAtZero.delete(entry.authority);
473
+ }
474
+ for (const entry of unheld)
475
+ heldAtZero.delete(entry.authority);
476
+ const site = mode === 'site';
477
+ unanswered.push(site ? unheld.map((entry) => entry.locator) : []);
478
+ const marked = (held) => {
479
+ const mark = runMarks.rooms[held.authority];
480
+ return mark === undefined ? held : { ...held, requests: mark };
481
+ };
482
+ socket.hold([
483
+ ...named.map(({ entry, at }) => marked(presented(entry.authority, at))),
484
+ ...(site ? [] : unheld).map((entry) => marked(presented(entry.authority, { position: '0', logEpoch: entry.logEpoch }))),
485
+ ], runtime, listedCursor, runMarks.sent ?? undefined);
486
+ if (site)
487
+ return;
488
+ const made = unheld.filter((entry) => entry.position === '0');
489
+ queueMicrotask(() => {
490
+ for (const entry of made) {
491
+ const version = roomVersion(entry);
492
+ emit({
493
+ kind: 'room-change',
494
+ locator: entry.locator,
495
+ from: null,
496
+ roomVersion: version,
497
+ read: () => changePage(entry, [], version, mode, true),
498
+ });
499
+ }
500
+ });
229
501
  },
230
502
  track(entries) {
231
- locators.clear();
503
+ tracking.clear();
504
+ located.clear();
232
505
  for (const entry of entries) {
233
- locators.set(entry.authority, [...(locators.get(entry.authority) ?? []), entry.locator]);
234
- if (lines.get(entry.authority)?.logEpoch !== entry.logEpoch) {
235
- lines.set(entry.authority, { logEpoch: entry.logEpoch, frames: [], held: null });
236
- }
506
+ tracking.set(entry.authority, [...(tracking.get(entry.authority) ?? []), entry]);
237
507
  }
238
- for (const authority of lines.keys()) {
239
- if (!locators.has(authority))
240
- lines.delete(authority);
508
+ for (const held of tracking.values()) {
509
+ for (const entry of held)
510
+ located.set(entry.locator, entry);
241
511
  }
242
- },
243
- reach(authority, at) {
244
- const line = lines.get(authority);
245
- const held = position(at.position);
246
- if (line === undefined || held === null || line.logEpoch !== at.logEpoch)
247
- return;
248
- if (line.held !== null && held <= line.held)
249
- return;
250
- lines.set(authority, {
251
- ...line,
252
- frames: line.frames.filter((frame) => frame.position > held),
253
- held,
254
- });
255
- },
256
- advance(tracked, from, holds) {
257
- const line = lines.get(tracked.authority);
258
- const start = position(from.position);
259
- if (line === undefined || start === null || line.logEpoch !== from.logEpoch)
260
- return null;
261
- const pending = line.frames.filter((frame) => frame.position > start);
262
- const first = pending[0];
263
- if (first === undefined || first.after > start)
264
- return null;
265
- const documents = {};
266
- const revisions = {};
267
- const changes = {};
268
- const removals = [];
269
- const applied = [];
270
- const schemas = new Map();
271
- const forget = (path) => {
272
- delete documents[path];
273
- delete revisions[path];
274
- delete changes[path];
275
- const removed = removals.indexOf(path);
276
- if (removed !== -1)
277
- removals.splice(removed, 1);
278
- };
279
- for (const frame of pending) {
280
- for (const entry of frame.entries) {
281
- if (entry.kind === 'edited' &&
282
- entry.revision !== undefined &&
283
- holds?.(entry.path, entry.revision) === true) {
284
- forget(entry.path);
285
- continue;
286
- }
287
- applied.push(entry);
288
- if (tracked.schemas.includes(entry.path))
289
- schemas.set(entry.path, entry);
290
- if (entry.kind === 'removed') {
291
- forget(entry.path);
292
- removals.push(entry.path);
293
- continue;
294
- }
295
- if (entry.text === undefined)
296
- return null;
297
- if (mode === 'credential' && entry.revision === undefined)
298
- return null;
299
- const head = headChange(entry);
300
- if (head === null)
301
- return null;
302
- forget(entry.path);
303
- documents[entry.path] = entry.text;
304
- if (entry.revision !== undefined)
305
- revisions[entry.path] = entry.revision;
306
- if (head !== undefined)
307
- changes[entry.path] = head;
308
- }
512
+ for (const [carry, held] of staged) {
513
+ if (!tracking.has(held.authority))
514
+ staged.delete(carry);
309
515
  }
310
- const last = pending.at(-1);
311
- lines.set(tracked.authority, { ...line, frames: pending, held: last.position });
312
- return {
313
- page: {
314
- kind: 'net-advancement',
315
- roomVersion: roomVersion({ position: last.position.toString(), logEpoch: line.logEpoch }),
316
- documents,
317
- revisions,
318
- changes,
319
- binaryTargets: [],
320
- removals: removals.sort(),
321
- },
322
- entries: applied,
323
- schemas,
324
- };
325
516
  },
326
517
  };
327
518
  }