@agentshouse/mdruntime-adapter 0.1.0-c2bcef775f3b3 → 0.1.0-c2c0ec6c1f436

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