@agentshouse/mdruntime-adapter 0.1.0-c4c8594e5b8d9 → 0.1.0-c4db5ac3f619f

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