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

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 -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 +302 -310
  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 +45 -18
  16. package/dist/discovery.d.ts.map +1 -1
  17. package/dist/discovery.js +252 -66
  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 +176 -256
  31. package/dist/refusal.d.ts +6 -5
  32. package/dist/refusal.d.ts.map +1 -1
  33. package/dist/refusal.js +27 -11
  34. package/dist/room-query.d.ts +4 -0
  35. package/dist/room-query.d.ts.map +1 -0
  36. package/dist/room-query.js +48 -0
  37. package/dist/search.d.ts +4 -0
  38. package/dist/search.d.ts.map +1 -0
  39. package/dist/search.js +47 -0
  40. package/dist/session-content/check.d.ts +17 -0
  41. package/dist/session-content/index.d.ts +6 -0
  42. package/dist/session-content/parts.d.ts +30 -0
  43. package/dist/session-content/state.d.ts +59 -0
  44. package/dist/session-content/turn.d.ts +137 -0
  45. package/dist/site.d.ts +1 -2
  46. package/dist/site.d.ts.map +1 -1
  47. package/dist/site.js +127 -193
  48. package/dist/space-schema.d.ts +6 -0
  49. package/dist/space-schema.d.ts.map +1 -0
  50. package/dist/space-schema.js +11 -0
  51. package/dist/stream.d.ts +14 -14
  52. package/dist/stream.d.ts.map +1 -1
  53. package/dist/stream.js +267 -253
  54. package/dist/transport.d.ts +108 -30
  55. package/dist/transport.d.ts.map +1 -1
  56. package/dist/transport.js +89 -11
  57. package/dist/uncertain.d.ts +5 -0
  58. package/dist/uncertain.d.ts.map +1 -0
  59. package/dist/uncertain.js +11 -0
  60. package/dist/upload.d.ts +13 -0
  61. package/dist/upload.d.ts.map +1 -0
  62. package/dist/upload.js +65 -0
  63. package/dist/version.d.ts +1 -1
  64. package/dist/version.d.ts.map +1 -1
  65. package/dist/version.js +1 -1
  66. package/dist/write.d.ts +18 -4
  67. package/dist/write.d.ts.map +1 -1
  68. package/dist/write.js +123 -91
  69. package/package.json +9 -4
  70. package/AGENTS.md +0 -299
  71. package/dist/gather.d.ts +0 -4
  72. package/dist/gather.d.ts.map +0 -1
  73. package/dist/gather.js +0 -45
  74. package/dist/handed.d.ts +0 -15
  75. package/dist/handed.d.ts.map +0 -1
  76. package/dist/handed.js +0 -38
  77. package/dist/referrer.d.ts +0 -7
  78. package/dist/referrer.d.ts.map +0 -1
  79. 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);
242
+ const held = staged.get(frame.carry);
243
+ staged.delete(frame.carry);
244
+ if ((held?.parts ?? 0) !== frame.parts)
183
245
  return;
184
- }
185
- const digested = entries.filter((entry) => entry.text !== undefined && entry.sha256 !== undefined);
186
- if (digested.length === 0) {
187
- publish(frame, entries);
188
- 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,48 @@ 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 === 'report') {
272
+ emit({ kind: 'reported' });
273
+ return;
274
+ }
275
+ if (frame.type === 'authority_changed') {
276
+ const change = discovering.changed(frame);
277
+ if (change !== null)
278
+ emit(change);
279
+ return;
280
+ }
213
281
  if (frame.type === 'conversations') {
214
- emitConversation({ kind: 'list', entries: frame.conversations.map(listedConversation) });
282
+ emitConversation({
283
+ kind: 'list',
284
+ agents: frame.agents.map(listedAgent),
285
+ entries: frame.conversations.map(listedConversation),
286
+ whole: frame.whole,
287
+ more: frame.more,
288
+ cursor: frame.cursor,
289
+ });
215
290
  return;
216
291
  }
217
292
  if (frame.type === 'conversation') {
218
293
  emitConversation({ kind: 'listed', entry: listedConversation(frame.conversation) });
219
294
  return;
220
295
  }
296
+ if (frame.type === 'agent') {
297
+ emitConversation({ kind: 'agent', agent: listedAgent(frame.agent) });
298
+ return;
299
+ }
221
300
  if (frame.type === 'conversation_deleted') {
222
301
  emitConversation({ kind: 'unlisted', conversation: frame.conversation_id });
223
302
  return;
@@ -226,64 +305,50 @@ export function houseNotifications(transport, mode) {
226
305
  frame.type === 'process' ||
227
306
  frame.type === 'commands' ||
228
307
  frame.type === 'options' ||
308
+ frame.type === 'starting' ||
309
+ frame.type === 'state' ||
310
+ frame.type === 'link' ||
311
+ frame.type === 'deliveries' ||
229
312
  frame.type === 'interactions' ||
230
313
  frame.type === 'message') {
231
314
  converse(frame);
232
315
  return;
233
316
  }
234
- if (frame.type === 'part') {
235
- stage(frame);
236
- return;
237
- }
238
- if (frame.type === 'carried')
239
- return carried(frame);
317
+ if (frame.type === 'part')
318
+ stage(frame, via);
319
+ else if (frame.type === 'carried')
320
+ carried(frame);
240
321
  }
241
322
  function publish(frame, entries) {
242
323
  const at = position(frame.position);
243
324
  const after = position(frame.after);
244
- if (at === null || after === null || after >= at) {
245
- interrupt(frame.authority);
325
+ if (at === null || after === null || after >= at)
246
326
  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;
327
+ const from = after === 0n && !heldAtZero.has(frame.authority)
328
+ ? null
329
+ : roomVersion({ position: frame.after, logEpoch: frame.log_epoch });
330
+ const version = roomVersion({ position: frame.position, logEpoch: frame.log_epoch });
331
+ for (const tracked of tracking.get(frame.authority) ?? []) {
332
+ emit({
333
+ kind: 'room-change',
334
+ locator: tracked.locator,
335
+ from,
336
+ roomVersion: version,
337
+ read: () => changePage(tracked, entries, version, mode, from === null),
272
338
  });
273
- return;
274
339
  }
275
- verifying += 1;
276
- receiving = receiving
277
- .then(() => handle(frame))
278
- .finally(() => {
279
- verifying -= 1;
280
- });
281
- });
340
+ }
341
+ stream.observe((frame) => handle(frame, socket));
282
342
  stream.opened(reopened);
283
- stream.closed((revoked) => {
343
+ stream.closed(({ revoked, lost, authorityChanged }) => {
284
344
  opening = null;
345
+ announced = null;
346
+ socket = null;
285
347
  staged.clear();
286
- emit({ kind: 'closed', revoked });
348
+ spaces = spaceSchemas();
349
+ unanswered.length = 0;
350
+ heldAtZero.clear();
351
+ emit({ kind: 'closed', revoked, lost, authorityChanged });
287
352
  });
288
353
  return {
289
354
  runtime,
@@ -299,19 +364,29 @@ export function houseNotifications(transport, mode) {
299
364
  replicate(next) {
300
365
  replicated = [...next];
301
366
  },
367
+ hold(cursor) {
368
+ listedCursor = cursor;
369
+ },
302
370
  observe(notify) {
303
371
  conversationObservers.add(notify);
304
372
  return () => {
305
373
  conversationObservers.delete(notify);
306
374
  };
307
375
  },
376
+ ...(mode === 'credential' ? { history: (request) => houseHistory(transport, request) } : {}),
308
377
  },
309
378
  observe(notify) {
310
379
  observers.add(notify);
311
380
  if (opening !== null) {
381
+ const replayed = [{ kind: 'opened' }];
382
+ if (announced !== null)
383
+ replayed.push(announced);
384
+ replaying.set(notify, replayed);
312
385
  queueMicrotask(() => {
313
- if (observers.has(notify))
314
- notify({ kind: 'opened' });
386
+ replaying.delete(notify);
387
+ for (const event of replayed)
388
+ if (observers.has(notify))
389
+ notify(event);
315
390
  });
316
391
  }
317
392
  return () => {
@@ -319,124 +394,63 @@ export function houseNotifications(transport, mode) {
319
394
  };
320
395
  },
321
396
  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
397
  opening = null;
331
- current?.hold(rooms, runtime);
332
- },
333
- reopen() {
334
- const current = opening;
335
- opening = null;
336
- current?.close();
398
+ announced = null;
399
+ if (socket === null)
400
+ return;
401
+ const named = rooms.flatMap((room) => {
402
+ const entry = located.get(room.locator);
403
+ const at = room.roomVersion === null ? null : logPosition(room.roomVersion);
404
+ return entry === undefined || at === null ? [] : [{ entry, at }];
405
+ });
406
+ const unheld = rooms.flatMap((room) => {
407
+ const entry = located.get(room.locator);
408
+ return entry === undefined || room.roomVersion !== null ? [] : [entry];
409
+ });
410
+ for (const { entry, at } of named) {
411
+ if (at.position === '0')
412
+ heldAtZero.add(entry.authority);
413
+ else
414
+ heldAtZero.delete(entry.authority);
415
+ }
416
+ for (const entry of unheld)
417
+ heldAtZero.delete(entry.authority);
418
+ const site = mode === 'site';
419
+ unanswered.push(site ? unheld.map((entry) => entry.locator) : []);
420
+ socket.hold([
421
+ ...named.map(({ entry, at }) => presented(entry.authority, at)),
422
+ ...(site ? [] : unheld).map((entry) => presented(entry.authority, { position: '0', logEpoch: entry.logEpoch })),
423
+ ], runtime, listedCursor);
424
+ if (site)
425
+ return;
426
+ const made = unheld.filter((entry) => entry.position === '0');
427
+ queueMicrotask(() => {
428
+ for (const entry of made) {
429
+ const version = roomVersion(entry);
430
+ emit({
431
+ kind: 'room-change',
432
+ locator: entry.locator,
433
+ from: null,
434
+ roomVersion: version,
435
+ read: () => changePage(entry, [], version, mode, true),
436
+ });
437
+ }
438
+ });
337
439
  },
338
440
  track(entries) {
339
- locators.clear();
441
+ tracking.clear();
442
+ located.clear();
340
443
  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
- }
444
+ tracking.set(entry.authority, [...(tracking.get(entry.authority) ?? []), entry]);
345
445
  }
346
- for (const authority of lines.keys()) {
347
- if (!locators.has(authority))
348
- lines.delete(authority);
446
+ for (const held of tracking.values()) {
447
+ for (const entry of held)
448
+ located.set(entry.locator, entry);
349
449
  }
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
- }
450
+ for (const [carry, held] of staged) {
451
+ if (!tracking.has(held.authority))
452
+ staged.delete(carry);
424
453
  }
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
454
  },
441
455
  };
442
456
  }