@agentshouse/mdruntime-adapter 0.1.0-c6ddfd288ac81 → 0.1.0-c736fea18a3c2

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 (51) hide show
  1. package/AGENTS.md +279 -181
  2. package/README.md +3 -3
  3. package/dist/actions.d.ts +63 -2
  4. package/dist/actions.d.ts.map +1 -1
  5. package/dist/actions.js +156 -138
  6. package/dist/adapter.d.ts +1 -0
  7. package/dist/adapter.d.ts.map +1 -1
  8. package/dist/adapter.js +347 -166
  9. package/dist/discovery.d.ts +15 -27
  10. package/dist/discovery.d.ts.map +1 -1
  11. package/dist/discovery.js +57 -102
  12. package/dist/gather.d.ts +1 -0
  13. package/dist/gather.d.ts.map +1 -1
  14. package/dist/gather.js +11 -6
  15. package/dist/handed.d.ts +15 -0
  16. package/dist/handed.d.ts.map +1 -0
  17. package/dist/handed.js +38 -0
  18. package/dist/index.d.ts +2 -2
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +1 -1
  21. package/dist/model.d.ts +7 -18
  22. package/dist/model.d.ts.map +1 -1
  23. package/dist/model.js +60 -39
  24. package/dist/read.d.ts +38 -11
  25. package/dist/read.d.ts.map +1 -1
  26. package/dist/read.js +166 -106
  27. package/dist/referrer.d.ts +7 -0
  28. package/dist/referrer.d.ts.map +1 -0
  29. package/dist/referrer.js +35 -0
  30. package/dist/refusal.d.ts +5 -7
  31. package/dist/refusal.d.ts.map +1 -1
  32. package/dist/refusal.js +31 -31
  33. package/dist/site.d.ts +1 -1
  34. package/dist/site.d.ts.map +1 -1
  35. package/dist/site.js +209 -106
  36. package/dist/stream.d.ts +12 -4
  37. package/dist/stream.d.ts.map +1 -1
  38. package/dist/stream.js +442 -105
  39. package/dist/transport.d.ts +89 -23
  40. package/dist/transport.d.ts.map +1 -1
  41. package/dist/transport.js +3 -2
  42. package/dist/version.d.ts +2 -0
  43. package/dist/version.d.ts.map +1 -1
  44. package/dist/version.js +10 -1
  45. package/dist/write.d.ts +4 -5
  46. package/dist/write.d.ts.map +1 -1
  47. package/dist/write.js +103 -26
  48. package/package.json +3 -4
  49. package/dist/links.d.ts +0 -11
  50. package/dist/links.d.ts.map +0 -1
  51. package/dist/links.js +0 -51
package/dist/stream.js CHANGED
@@ -1,73 +1,139 @@
1
1
  import { acceptedMessage } from './actions.js';
2
- import { headChange, selects } from './read.js';
2
+ import { sha256Hex } from './digest.js';
3
+ import { binaryFileOf, headChange, identities } from './read.js';
3
4
  import { roomVersion } from './version.js';
4
- const HELD_POSITIONS_MAX = 256;
5
+ const HELD_FRAMES_MAX = 256;
6
+ function turnOperation(frame) {
7
+ const { type: _type, conversation_id: _conversation, turn_id: _turn, ...operation } = frame;
8
+ return structuredClone(operation);
9
+ }
10
+ function listedConversation(listed) {
11
+ return {
12
+ conversation: listed.conversation_id,
13
+ agent: listed.agent_id,
14
+ name: listed.name,
15
+ created: listed.created_at,
16
+ finished: listed.finished_at,
17
+ outcome: listed.outcome,
18
+ };
19
+ }
5
20
  function position(value) {
6
21
  if (!/^(?:0|[1-9][0-9]*)$/.test(value))
7
22
  return null;
8
23
  return BigInt(value);
9
24
  }
25
+ function assembled(entries) {
26
+ const whole = [];
27
+ let open = null;
28
+ for (const entry of entries) {
29
+ if (entry.kind === 'piece') {
30
+ if (open === null || open.entry.path !== entry.path || entry.text === undefined)
31
+ return null;
32
+ open.texts.push(entry.text);
33
+ }
34
+ else {
35
+ if (open !== null)
36
+ return null;
37
+ if (entry.continued !== true) {
38
+ whole.push(entry);
39
+ continue;
40
+ }
41
+ if (entry.text === undefined)
42
+ return null;
43
+ open = { entry, texts: [entry.text] };
44
+ }
45
+ if (entry.continued === true)
46
+ continue;
47
+ const { continued: _continued, ...head } = open.entry;
48
+ whole.push({ ...head, text: open.texts.join('') });
49
+ open = null;
50
+ }
51
+ return open === null ? whole : null;
52
+ }
53
+ async function verified(entries) {
54
+ try {
55
+ for (const entry of entries) {
56
+ if (entry.text === undefined || entry.sha256 === undefined)
57
+ continue;
58
+ if ((await sha256Hex(entry.text)) !== entry.sha256)
59
+ return false;
60
+ }
61
+ return true;
62
+ }
63
+ catch {
64
+ return false;
65
+ }
66
+ }
67
+ function resumed(kept, frame) {
68
+ const last = frame.entries.at(-1);
69
+ const point = { parts: frame.index + 1, from: frame.from, target: frame.target };
70
+ if (last?.continued !== true)
71
+ return { ...point, handled: frame.handled, piece: null };
72
+ const text = last.text?.length ?? 0;
73
+ if (last.kind !== 'piece') {
74
+ return last.sha256 === undefined
75
+ ? kept.resume
76
+ : { ...point, handled: frame.handled, piece: { sha256: last.sha256, offset: text } };
77
+ }
78
+ const piece = kept.resume?.piece ?? null;
79
+ if (piece === null)
80
+ return kept.resume;
81
+ const pieces = frame.entries.reduce((sum, entry) => sum + (entry.kind === 'piece' ? (entry.text?.length ?? 0) : 0), 0);
82
+ return {
83
+ ...point,
84
+ handled: frame.handled,
85
+ piece: { sha256: piece.sha256, offset: piece.offset + pieces },
86
+ };
87
+ }
10
88
  export function houseNotifications(transport, mode) {
11
89
  const stream = transport.stream;
12
90
  const lines = new Map();
91
+ const kept = new Map();
92
+ let receipts = null;
93
+ let loaded = Promise.resolve();
94
+ let acknowledging = null;
95
+ let receiving = Promise.resolve();
96
+ let verifying = 0;
13
97
  const locators = new Map();
14
- const schemas = new Map();
15
98
  const observers = new Set();
16
99
  const conversationObservers = new Set();
17
100
  const modeObservers = new Set();
18
101
  let replicated = [];
19
102
  const live = new Set();
20
- const noted = new Set();
21
- let listening = false;
22
- let catching = false;
23
- function listen() {
24
- if (listening)
25
- return;
26
- listening = true;
27
- stream.observe(receive);
103
+ let opening = null;
104
+ const runtime = crypto.randomUUID();
105
+ function emit(event) {
106
+ for (const observer of observers)
107
+ observer(event);
28
108
  }
29
109
  function emitConversation(event) {
30
- if (event.kind !== 'gap' && event.kind !== 'accepted-message')
110
+ if (event.kind === 'turn' ||
111
+ event.kind === 'questions' ||
112
+ event.kind === 'process' ||
113
+ event.kind === 'commands' ||
114
+ event.kind === 'options') {
31
115
  live.add(event.conversation);
116
+ }
32
117
  for (const observer of conversationObservers)
33
118
  observer(event);
34
119
  }
35
- function unnote(conversation) {
36
- if (!noted.delete(conversation))
37
- return;
38
- emitConversation({ kind: 'working-note', conversation, note: null });
39
- }
40
- function reopened() {
120
+ function reopened(next) {
121
+ opening = next;
122
+ acknowledging = next;
41
123
  const gapped = new Set([...replicated, ...live]);
42
124
  live.clear();
43
- noted.clear();
44
125
  for (const conversation of gapped)
45
126
  emitConversation({ kind: 'gap', conversation });
127
+ emit({ kind: 'opened' });
46
128
  }
47
129
  function converse(frame) {
48
130
  const conversation = frame.conversation_id;
49
- if (frame.type === 'draft') {
50
- unnote(conversation);
51
- emitConversation({
52
- kind: 'growing-text',
53
- conversation,
54
- turn: frame.turn_id,
55
- text: frame.text,
56
- });
131
+ if (frame.type === 'turn') {
57
132
  emitConversation({
58
- kind: 'plan',
133
+ kind: 'turn',
59
134
  conversation,
60
135
  turn: frame.turn_id,
61
- plan: structuredClone(frame.steps),
62
- });
63
- return;
64
- }
65
- if (frame.type === 'working-note') {
66
- noted.add(conversation);
67
- emitConversation({
68
- kind: 'working-note',
69
- conversation,
70
- note: { noteId: frame.note_id, text: frame.text },
136
+ operation: turnOperation(frame),
71
137
  });
72
138
  return;
73
139
  }
@@ -77,7 +143,6 @@ export function houseNotifications(transport, mode) {
77
143
  return;
78
144
  emitConversation({ kind: 'commands', conversation, commands: [] });
79
145
  emitConversation({ kind: 'options', conversation, options: [] });
80
- unnote(conversation);
81
146
  return;
82
147
  }
83
148
  if (frame.type === 'commands') {
@@ -104,26 +169,174 @@ export function houseNotifications(transport, mode) {
104
169
  if (message === null)
105
170
  return;
106
171
  emitConversation({ kind: 'accepted-message', conversation, message });
107
- if (frame.message.role === 'user')
108
- return;
109
- unnote(conversation);
110
172
  }
111
- function announce(authority, at, modelChanged) {
173
+ function announce(authority, at) {
112
174
  const version = roomVersion(at);
113
175
  for (const locator of locators.get(authority) ?? []) {
114
- for (const observer of observers) {
115
- observer(modelChanged
116
- ? { locator, roomVersion: version, modelChanged: true }
117
- : { locator, roomVersion: version });
118
- }
176
+ emit({ kind: 'room-version', locator, roomVersion: version });
119
177
  }
120
178
  }
121
179
  function interrupt(authority) {
122
180
  const line = lines.get(authority);
123
181
  if (line !== undefined)
124
- lines.set(authority, { logEpoch: line.logEpoch, frames: [] });
182
+ lines.set(authority, { ...line, frames: [] });
183
+ }
184
+ async function forget(carries) {
185
+ const held = carries.filter((carry) => kept.delete(carry));
186
+ if (held.length > 0)
187
+ await receipts?.drop(held).catch(() => { });
188
+ }
189
+ async function lost(authority, carry) {
190
+ interrupt(authority);
191
+ await forget([carry]);
192
+ }
193
+ async function stage(frame, via) {
194
+ await loaded;
195
+ const port = receipts;
196
+ if (port === null)
197
+ return;
198
+ const held = kept.get(frame.carry);
199
+ if (held === undefined && frame.index !== 0) {
200
+ interrupt(frame.authority);
201
+ return;
202
+ }
203
+ if (held !== undefined &&
204
+ (held.fenced !== null || frame.index > held.parts || frame.index < (held.resume?.parts ?? 0))) {
205
+ await lost(frame.authority, frame.carry);
206
+ return;
207
+ }
208
+ if (held === undefined) {
209
+ await forget([...kept.values()]
210
+ .filter((other) => other.authority === frame.authority && other.fenced === null)
211
+ .map((other) => other.carry));
212
+ }
213
+ const start = held ?? {
214
+ authority: frame.authority,
215
+ carry: frame.carry,
216
+ logEpoch: lines.get(frame.authority)?.logEpoch ?? null,
217
+ after: frame.after,
218
+ parts: 0,
219
+ resume: null,
220
+ fenced: null,
221
+ };
222
+ const next = { ...start, parts: frame.index + 1, resume: resumed(start, frame) };
223
+ const part = { entries: frame.entries };
224
+ try {
225
+ await port.keep(frame.carry, next, { index: frame.index, value: part });
226
+ }
227
+ catch {
228
+ await lost(frame.authority, frame.carry);
229
+ return;
230
+ }
231
+ kept.set(frame.carry, next);
232
+ via?.received(frame.authority, frame.carry, frame.index);
233
+ }
234
+ async function keptEntries(carry, parts) {
235
+ if (parts === 0)
236
+ return [];
237
+ if (receipts === null)
238
+ return null;
239
+ const read = (await receipts.parts(carry, parts));
240
+ if (read.length !== parts)
241
+ return null;
242
+ const entries = assembled(read.flatMap((part) => part.entries));
243
+ return entries !== null && (await verified(entries)) ? entries : null;
244
+ }
245
+ async function carried(frame) {
246
+ await loaded;
247
+ const held = kept.get(frame.carry);
248
+ if (frame.parts > 0 && held?.parts !== frame.parts) {
249
+ await lost(frame.authority, frame.carry);
250
+ return;
251
+ }
252
+ const entries = await keptEntries(frame.carry, frame.parts).catch(() => null);
253
+ if (entries === null) {
254
+ await lost(frame.authority, frame.carry);
255
+ return;
256
+ }
257
+ const fenced = {
258
+ ...(held ?? {
259
+ authority: frame.authority,
260
+ carry: frame.carry,
261
+ logEpoch: frame.log_epoch,
262
+ after: frame.after,
263
+ parts: 0,
264
+ resume: null,
265
+ }),
266
+ after: frame.after,
267
+ fenced: { position: frame.position, logEpoch: frame.log_epoch },
268
+ };
269
+ try {
270
+ await receipts?.keep(frame.carry, fenced);
271
+ }
272
+ catch {
273
+ await lost(frame.authority, frame.carry);
274
+ return;
275
+ }
276
+ kept.set(frame.carry, fenced);
277
+ publish(frame, entries);
278
+ }
279
+ async function load(port) {
280
+ try {
281
+ for (const { key, head } of await port.heads())
282
+ kept.set(key, head);
283
+ }
284
+ catch {
285
+ kept.clear();
286
+ }
287
+ }
288
+ async function restored(room) {
289
+ const line = lines.get(room.authority);
290
+ let at = room.position;
291
+ const chain = [];
292
+ for (;;) {
293
+ const next = [...kept.values()].find((carry) => carry.authority === room.authority &&
294
+ carry.after === at &&
295
+ carry.fenced?.logEpoch === room.log_epoch);
296
+ if (next?.fenced == null)
297
+ break;
298
+ const entries = await keptEntries(next.carry, next.parts).catch(() => null);
299
+ if (entries === null)
300
+ break;
301
+ chain.push({ carry: next, entries });
302
+ at = next.fenced.position;
303
+ }
304
+ const partial = [...kept.values()].find((carry) => carry.authority === room.authority &&
305
+ carry.fenced === null &&
306
+ carry.logEpoch === room.log_epoch &&
307
+ carry.after === at &&
308
+ carry.resume !== null &&
309
+ carry.resume.parts > 0);
310
+ const stale = [...kept.values()].filter((carry) => carry.authority === room.authority &&
311
+ carry !== partial &&
312
+ !chain.some((link) => link.carry === carry));
313
+ await forget(stale.map((carry) => carry.carry));
314
+ if (line !== undefined && line.logEpoch === room.log_epoch && chain.length > 0) {
315
+ const frames = chain.map(({ carry, entries }) => ({
316
+ after: BigInt(carry.after),
317
+ position: BigInt(carry.fenced.position),
318
+ entries,
319
+ }));
320
+ lines.set(room.authority, { ...line, frames: [...line.frames, ...frames] });
321
+ }
322
+ return {
323
+ ...room,
324
+ position: at,
325
+ ...(partial?.resume == null
326
+ ? {}
327
+ : {
328
+ carry: {
329
+ id: partial.carry,
330
+ parts: partial.resume.parts,
331
+ from: partial.resume.from,
332
+ target: partial.resume.target,
333
+ handled: partial.resume.handled,
334
+ ...(partial.resume.piece === null ? {} : { piece: partial.resume.piece }),
335
+ },
336
+ }),
337
+ };
125
338
  }
126
- function receive(frame) {
339
+ function handle(frame, via) {
127
340
  if (frame.type === 'signal') {
128
341
  const mode = frame.service_mode;
129
342
  if (mode !== 'open' && mode !== 'write-halted')
@@ -132,8 +345,30 @@ export function houseNotifications(transport, mode) {
132
345
  observer(mode);
133
346
  return;
134
347
  }
135
- if (frame.type === 'draft' ||
136
- frame.type === 'working-note' ||
348
+ if (frame.type === 'opening') {
349
+ emit({
350
+ kind: 'opening',
351
+ expired: frame.expired.flatMap((authority) => locators.get(authority) ?? []),
352
+ });
353
+ return;
354
+ }
355
+ if (frame.type === 'authority_changed') {
356
+ emit({ kind: 'authority-changed' });
357
+ return;
358
+ }
359
+ if (frame.type === 'conversations') {
360
+ emitConversation({ kind: 'list', entries: frame.conversations.map(listedConversation) });
361
+ return;
362
+ }
363
+ if (frame.type === 'conversation') {
364
+ emitConversation({ kind: 'listed', entry: listedConversation(frame.conversation) });
365
+ return;
366
+ }
367
+ if (frame.type === 'conversation_deleted') {
368
+ emitConversation({ kind: 'unlisted', conversation: frame.conversation_id });
369
+ return;
370
+ }
371
+ if (frame.type === 'turn' ||
137
372
  frame.type === 'process' ||
138
373
  frame.type === 'commands' ||
139
374
  frame.type === 'options' ||
@@ -142,36 +377,64 @@ export function houseNotifications(transport, mode) {
142
377
  converse(frame);
143
378
  return;
144
379
  }
145
- if (frame.type !== 'entries')
146
- return;
380
+ if (frame.type === 'part')
381
+ return stage(frame, via);
382
+ if (frame.type === 'carried')
383
+ return carried(frame);
384
+ }
385
+ function publish(frame, entries) {
147
386
  const at = position(frame.position);
148
- if (at === null) {
387
+ const after = position(frame.after);
388
+ if (at === null || after === null || after >= at) {
149
389
  interrupt(frame.authority);
150
390
  return;
151
391
  }
152
392
  const line = lines.get(frame.authority);
153
393
  if (line?.logEpoch === frame.log_epoch) {
394
+ if (line.held !== null && at <= line.held)
395
+ return;
154
396
  const last = line.frames.at(-1);
155
- const continued = last !== undefined && last.position + 1n === at;
156
- const next = continued
157
- ? [...line.frames, { position: at, entries: frame.entries }]
158
- : [{ position: at, entries: frame.entries }];
159
- lines.set(frame.authority, {
160
- logEpoch: line.logEpoch,
161
- frames: next.length > HELD_POSITIONS_MAX ? [] : next,
162
- });
397
+ if (last !== undefined && at <= last.position)
398
+ return;
399
+ const held = { after, position: at, entries };
400
+ const next = last !== undefined && after <= last.position ? [...line.frames, held] : [held];
401
+ lines.set(frame.authority, { ...line, frames: next.length > HELD_FRAMES_MAX ? [] : next });
163
402
  }
164
403
  else {
165
404
  interrupt(frame.authority);
166
405
  }
167
- const addresses = schemas.get(frame.authority) ?? [];
168
- announce(frame.authority, { position: frame.position, logEpoch: frame.log_epoch }, frame.entries.some((entry) => addresses.includes(entry.path)));
406
+ announce(frame.authority, { position: frame.position, logEpoch: frame.log_epoch });
169
407
  }
408
+ stream.observe((frame) => {
409
+ const via = acknowledging;
410
+ if (verifying === 0) {
411
+ const handled = handle(frame, via);
412
+ if (handled === undefined)
413
+ return;
414
+ verifying += 1;
415
+ receiving = handled.finally(() => {
416
+ verifying -= 1;
417
+ });
418
+ return;
419
+ }
420
+ verifying += 1;
421
+ receiving = receiving
422
+ .then(() => handle(frame, via))
423
+ .finally(() => {
424
+ verifying -= 1;
425
+ });
426
+ });
427
+ stream.opened(reopened);
428
+ stream.closed(({ revoked, lost }) => {
429
+ opening = null;
430
+ acknowledging = null;
431
+ emit({ kind: 'closed', revoked, lost });
432
+ });
170
433
  return {
434
+ runtime,
171
435
  serviceMode: {
172
436
  observe(notify) {
173
437
  modeObservers.add(notify);
174
- listen();
175
438
  return () => {
176
439
  modeObservers.delete(notify);
177
440
  };
@@ -183,98 +446,172 @@ export function houseNotifications(transport, mode) {
183
446
  },
184
447
  observe(notify) {
185
448
  conversationObservers.add(notify);
186
- listen();
187
- if (!catching) {
188
- catching = true;
189
- stream.opened(reopened);
190
- }
191
449
  return () => {
192
450
  conversationObservers.delete(notify);
193
451
  };
194
452
  },
195
453
  },
454
+ observe(notify, port) {
455
+ observers.add(notify);
456
+ if (receipts === null) {
457
+ receipts = port;
458
+ loaded = load(port);
459
+ }
460
+ if (opening !== null) {
461
+ queueMicrotask(() => {
462
+ if (observers.has(notify))
463
+ notify({ kind: 'opened' });
464
+ });
465
+ }
466
+ return () => {
467
+ observers.delete(notify);
468
+ };
469
+ },
470
+ send(rooms, demand) {
471
+ for (const room of rooms) {
472
+ const line = lines.get(room.authority);
473
+ const held = position(room.position);
474
+ if (line === undefined || held === null || line.logEpoch !== room.log_epoch)
475
+ continue;
476
+ lines.set(room.authority, { ...line, frames: [], held });
477
+ }
478
+ const current = opening;
479
+ opening = null;
480
+ if (current === null)
481
+ return;
482
+ void loaded
483
+ .then(() => Promise.all(rooms.map(restored)))
484
+ .then((presented) => {
485
+ current.hold(presented, runtime, demand);
486
+ for (const [index, room] of presented.entries()) {
487
+ if (room.position === rooms[index].position)
488
+ continue;
489
+ announce(room.authority, { position: room.position, logEpoch: room.log_epoch });
490
+ }
491
+ });
492
+ },
493
+ published(locator, at) {
494
+ const authority = [...locators].find(([, held]) => held.includes(locator))?.[0];
495
+ const reached = position(at.position);
496
+ if (authority === undefined || reached === null)
497
+ return;
498
+ void forget([...kept.values()]
499
+ .filter((carry) => carry.authority === authority &&
500
+ carry.fenced !== null &&
501
+ carry.fenced.logEpoch === at.logEpoch &&
502
+ BigInt(carry.fenced.position) <= reached)
503
+ .map((carry) => carry.carry));
504
+ },
505
+ reopen() {
506
+ const current = opening;
507
+ opening = null;
508
+ current?.close();
509
+ },
196
510
  track(entries) {
197
511
  locators.clear();
198
- schemas.clear();
199
512
  for (const entry of entries) {
200
513
  locators.set(entry.authority, [...(locators.get(entry.authority) ?? []), entry.locator]);
201
- schemas.set(entry.authority, entry.schemas);
202
514
  if (lines.get(entry.authority)?.logEpoch !== entry.logEpoch) {
203
- lines.set(entry.authority, { logEpoch: entry.logEpoch, frames: [] });
515
+ lines.set(entry.authority, { logEpoch: entry.logEpoch, frames: [], held: null });
204
516
  }
205
517
  }
206
518
  for (const authority of lines.keys()) {
207
519
  if (!locators.has(authority))
208
520
  lines.delete(authority);
209
521
  }
522
+ void loaded.then(() => forget([...kept.values()]
523
+ .filter((carry) => !locators.has(carry.authority))
524
+ .map((carry) => carry.carry)));
210
525
  },
211
- select() { },
212
- observe(notify) {
213
- observers.add(notify);
214
- listen();
215
- return () => {
216
- observers.delete(notify);
217
- };
526
+ reach(authority, at) {
527
+ const line = lines.get(authority);
528
+ const held = position(at.position);
529
+ if (line === undefined || held === null || line.logEpoch !== at.logEpoch)
530
+ return;
531
+ if (line.held !== null && held <= line.held)
532
+ return;
533
+ lines.set(authority, {
534
+ ...line,
535
+ frames: line.frames.filter((frame) => frame.position > held),
536
+ held,
537
+ });
218
538
  },
219
- advance(tracked, from) {
539
+ advance(tracked, from, holds) {
220
540
  const line = lines.get(tracked.authority);
221
541
  const start = position(from.position);
222
542
  if (line === undefined || start === null || line.logEpoch !== from.logEpoch)
223
543
  return null;
224
- const at = line.frames.findIndex((frame) => frame.position === start + 1n);
225
- if (at === -1)
544
+ const pending = line.frames.filter((frame) => frame.position > start);
545
+ const first = pending[0];
546
+ if (first === undefined || first.after > start)
226
547
  return null;
227
- const pending = line.frames.slice(at);
228
548
  const documents = {};
229
549
  const revisions = {};
230
550
  const changes = {};
551
+ const binaryFiles = {};
231
552
  const removals = [];
553
+ const applied = [];
554
+ const schemas = new Map();
555
+ const forget = (path) => {
556
+ delete documents[path];
557
+ delete revisions[path];
558
+ delete changes[path];
559
+ delete binaryFiles[path];
560
+ const removed = removals.indexOf(path);
561
+ if (removed !== -1)
562
+ removals.splice(removed, 1);
563
+ };
232
564
  for (const frame of pending) {
233
565
  for (const entry of frame.entries) {
234
- if (!selects(tracked, entry.path))
566
+ if (entry.kind === 'edited' &&
567
+ entry.revision !== undefined &&
568
+ holds?.(entry.path, entry.revision) === true) {
569
+ forget(entry.path);
235
570
  continue;
571
+ }
572
+ applied.push(entry);
573
+ if (tracked.schemas.includes(entry.path))
574
+ schemas.set(entry.path, entry);
236
575
  if (entry.kind === 'removed') {
237
- delete documents[entry.path];
238
- delete revisions[entry.path];
239
- delete changes[entry.path];
240
- if (!removals.includes(entry.path))
241
- removals.push(entry.path);
576
+ forget(entry.path);
577
+ removals.push(entry.path);
242
578
  continue;
243
579
  }
244
- if (entry.text === undefined)
580
+ const binary = binaryFileOf(entry);
581
+ if (binary === null || (binary === undefined && entry.text === undefined))
245
582
  return null;
246
583
  if (mode === 'credential' && entry.revision === undefined)
247
584
  return null;
248
585
  const head = headChange(entry);
249
586
  if (head === null)
250
587
  return null;
251
- documents[entry.path] = entry.text;
252
- if (entry.revision !== undefined)
253
- revisions[entry.path] = entry.revision;
254
- if (head === undefined)
255
- delete changes[entry.path];
588
+ forget(entry.path);
589
+ if (binary === undefined)
590
+ documents[entry.path] = entry.text;
256
591
  else
592
+ binaryFiles[entry.path] = binary;
593
+ const revision = entry.revision ?? binary?.sha256;
594
+ if (revision !== undefined)
595
+ revisions[entry.path] = revision;
596
+ if (head !== undefined)
257
597
  changes[entry.path] = head;
258
- const removed = removals.indexOf(entry.path);
259
- if (removed !== -1)
260
- removals.splice(removed, 1);
261
598
  }
262
599
  }
263
600
  const last = pending.at(-1);
264
- if (last === undefined)
265
- return null;
266
- lines.set(tracked.authority, { logEpoch: line.logEpoch, frames: pending });
601
+ lines.set(tracked.authority, { ...line, frames: pending, held: last.position });
267
602
  return {
268
603
  page: {
269
604
  kind: 'net-advancement',
270
605
  roomVersion: roomVersion({ position: last.position.toString(), logEpoch: line.logEpoch }),
271
606
  documents,
272
607
  revisions,
608
+ ...identities(revisions),
273
609
  changes,
274
- binaryTargets: [],
610
+ binaryFiles,
275
611
  removals: removals.sort(),
276
612
  },
277
- entries: pending.flatMap((frame) => frame.entries),
613
+ entries: applied,
614
+ schemas,
278
615
  };
279
616
  },
280
617
  };