@agentshouse/mdruntime-adapter 0.1.0-c50b979d9313f → 0.1.0-c57d433251ca5

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.
package/dist/adapter.js CHANGED
@@ -1,14 +1,17 @@
1
+ import { sortedKeys, stepped } from '@agentshouse/mdruntime';
1
2
  import { DISCOVER_PREFIXES, capabilities, discoveredRoom, mapDiscovery, parseDiscovery, trackedFrom, } from './discovery.js';
2
3
  import { partModels, streamedModels, viewModels } from './model.js';
3
4
  import { gathered, gatheredEach, selections } from './gather.js';
4
5
  import { createSiteAdapter } from './site.js';
5
- import { bootstrapOf, contentPrefixes, matchesScope, parseEnumeration, parseEnvelope, parseGrant, refreshFrom, viewOf, } from './read.js';
6
+ import { authorityRoot, bootstrapping, emptyScope, housePart, parseEnumeration, parseGrant, parseParted, refreshFrom, viewOf, wholeView, } from './read.js';
7
+ import { PART_ATTEMPTS, unverifiedPart } from './part.js';
8
+ import { sha256Hex } from './digest.js';
6
9
  import { acceptedHanded, enumeratedHanded, heldHanded, scopeHanded, streamedHanded, } from './handed.js';
7
10
  import { houseAction } from './actions.js';
8
11
  import { classifyDoor, classifyRead, discoveryFailure, doorRefusal } from './refusal.js';
9
12
  import { houseNotifications } from './stream.js';
10
13
  import { DOOR_BATCH, DOOR_BATCH_UPLOAD, DOOR_BOOTSTRAP, DOOR_DISCOVER, DOOR_ENUMERATE, DOOR_FILES_GET, DOOR_REVISIONS, } from './transport.js';
11
- import { batchPaths, deltaBody, deltaBytes, deltaDigest, historyOutcome, houseChanges, mutationOutcome, operationHead, rejectedMapping, revisionBody, uploadable, } from './write.js';
14
+ import { batchPaths, deltaBody, deltaBytes, deltaDigest, historyOutcome, houseChanges, houseMove, mutationOutcome, operationHead, rejectedMapping, revisionBody, uploadable, } from './write.js';
12
15
  import { houseReferrer } from './referrer.js';
13
16
  import { followsVersion, logPosition, presented, reachedVersion, roomVersion, } from './version.js';
14
17
  const UNCERTAIN = { kind: 'transport-uncertainty' };
@@ -16,9 +19,48 @@ const UNCERTAIN_READ = { kind: 'transport-uncertainty' };
16
19
  function lapsedGrant(outcome) {
17
20
  return outcome.kind === 'rejected' && outcome.code === 'upload_unavailable';
18
21
  }
22
+ const heldPaths = new WeakMap();
23
+ async function sortedHeld(held) {
24
+ const known = heldPaths.get(held.documents);
25
+ if (known !== undefined)
26
+ return known;
27
+ const sorted = await stepped(sortedKeys(held.documents));
28
+ heldPaths.set(held.documents, sorted);
29
+ return sorted;
30
+ }
31
+ function heldFiles(authority, held, paths, bound) {
32
+ const files = [];
33
+ let through = null;
34
+ for (let at = 0; at < paths.length && through === null; at += 1) {
35
+ const revision = held.revisions[paths[at]];
36
+ if (revision === undefined)
37
+ continue;
38
+ if (files.length === bound)
39
+ through = files.at(-1).path;
40
+ else
41
+ files.push({ path: paths[at], revision });
42
+ }
43
+ return files.length === 0 ? null : { room: authority, through, files };
44
+ }
45
+ async function reused(scope, held) {
46
+ const sent = new Set(scope.contents.map((entry) => entry.path));
47
+ const contents = [...scope.contents];
48
+ for (const file of scope.files) {
49
+ if (file.binary !== undefined || sent.has(file.path))
50
+ continue;
51
+ const content = held?.documents[file.path];
52
+ if (content === undefined ||
53
+ held?.revisions[file.path] !== file.revision ||
54
+ (await sha256Hex(content)) !== file.sha256) {
55
+ return null;
56
+ }
57
+ contents.push({ path: file.path, content });
58
+ }
59
+ return { ...scope, contents };
60
+ }
19
61
  export function createAgentsHouseAdapter(options) {
20
62
  if ('site' in options)
21
- return createSiteAdapter(options.site);
63
+ return createSiteAdapter(options.site, options.snapshot === true);
22
64
  const transport = options.transport;
23
65
  const notifications = houseNotifications(transport, 'credential');
24
66
  const tracked = new Map();
@@ -29,36 +71,98 @@ export function createAgentsHouseAdapter(options) {
29
71
  throw new Error('no Discovery declared the bounds');
30
72
  return bounds;
31
73
  }
32
- async function bootstrappedRoom(envelope, scope, entry, continued) {
33
- if (scope === undefined)
34
- return { kind: 'room-not-readable', locator: entry.locator };
35
- const next = envelope.next?.room === entry.authority ? envelope.next.path : null;
36
- if (continued === null && next === null)
37
- handed.set(entry.locator, scopeHanded(scope));
74
+ function hand(locator, steps) {
75
+ const held = handed.get(locator);
76
+ const next = stepped(steps);
77
+ if (!(next instanceof Promise)) {
78
+ handed.set(locator, next);
79
+ return;
80
+ }
81
+ return next.then((room) => {
82
+ if (handed.get(locator) === held)
83
+ handed.set(locator, room);
84
+ else
85
+ handed.delete(locator);
86
+ });
87
+ }
88
+ async function fetchedPart(entry, continued, listed) {
89
+ for (let attempt = 0; attempt < PART_ATTEMPTS; attempt += 1) {
90
+ const answered = await transport.door(DOOR_BOOTSTRAP, {
91
+ prefixes: [authorityRoot(entry.handle)],
92
+ part: listed.part,
93
+ ...(continued.held === undefined ? {} : { held: [continued.held] }),
94
+ });
95
+ const failure = classifyRead(answered.status, answered.body, entry.locator);
96
+ if (failure !== null)
97
+ return failure;
98
+ const scope = await housePart(continued.room, answered.body, listed, 'credential');
99
+ if (scope !== null)
100
+ return scope;
101
+ }
102
+ return unverifiedPart(entry.locator);
103
+ }
104
+ async function partPage(asked, continued) {
105
+ const { entry } = asked;
106
+ const listed = continued.room.parts[continued.next];
107
+ const sent = listed === undefined
108
+ ? emptyScope(continued.room)
109
+ : await fetchedPart(entry, continued, listed);
110
+ if ('kind' in sent)
111
+ return sent;
112
+ const scope = await reused(sent, asked.held);
113
+ if (scope === null) {
114
+ return asked.held === undefined
115
+ ? UNCERTAIN_READ
116
+ : bootstrapTogether({ entry, held: undefined });
117
+ }
118
+ const next = continued.next + 1;
119
+ const more = next < continued.room.parts.length;
120
+ if (continued.next === 0 && !more)
121
+ await hand(entry.locator, scopeHanded(scope));
38
122
  else
39
123
  handed.delete(entry.locator);
40
124
  notifications.reach(entry.authority, scope);
41
125
  const seen = [
42
- ...(continued?.seen ?? []),
126
+ ...continued.seen,
43
127
  ...entry.schemas.filter((schema) => scope.files.some((file) => file.path === schema)),
44
128
  ];
45
- const models = await partModels(scope, entry, new Set(seen), next === null);
46
- const continuation = next === null ? null : { after: next, seen };
129
+ const models = await partModels(scope, entry, new Set(seen), !more);
47
130
  return {
48
- ...bootstrapOf(scope),
131
+ ...(await stepped(bootstrapping(scope))),
49
132
  ...(models === undefined ? {} : { models }),
50
- ...(continuation === null ? {} : { pageToken: JSON.stringify(continuation) }),
133
+ ...(more ? { pageToken: JSON.stringify({ ...continued, next, seen }) } : {}),
51
134
  };
52
135
  }
136
+ async function enumeratedPage(ask, page) {
137
+ const answer = await transport.door(DOOR_ENUMERATE, {
138
+ prefixes: [authorityRoot(ask.entry.handle)],
139
+ positions: [
140
+ {
141
+ ...presented(ask.entry.authority, { position: page.after, logEpoch: ask.from.logEpoch }),
142
+ page: { through: page.through, read: page.read },
143
+ },
144
+ ],
145
+ });
146
+ const failure = classifyRead(answer.status, answer.body, ask.entry.locator);
147
+ if (failure !== null)
148
+ return failure;
149
+ const enumeration = parseEnumeration(answer.body, 'credential');
150
+ if (enumeration === null)
151
+ return UNCERTAIN_READ;
152
+ return (viewOf(enumeration, ask.entry) ?? { kind: 'room-not-readable', locator: ask.entry.locator });
153
+ }
53
154
  async function enumeratedRoom(enumeration, ask) {
54
- const view = viewOf(enumeration, ask.entry);
55
- if (view === undefined)
155
+ const first = viewOf(enumeration, ask.entry);
156
+ if (first === undefined)
56
157
  return { kind: 'room-not-readable', locator: ask.entry.locator };
158
+ const view = await wholeView(first, (page) => enumeratedPage(ask, page));
159
+ if ('kind' in view)
160
+ return view;
57
161
  notifications.reach(ask.entry.authority, view);
58
- const answer = refreshFrom(view, ask.fromRoomVersion);
162
+ const answer = await refreshFrom(view, ask.fromRoomVersion);
59
163
  if (answer.kind !== 'net-advancement')
60
164
  return answer;
61
- handed.set(ask.entry.locator, enumeratedHanded(view, handed.get(ask.entry.locator)));
165
+ await hand(ask.entry.locator, enumeratedHanded(view, handed.get(ask.entry.locator)));
62
166
  const models = await viewModels(view, ask.entry);
63
167
  return models === undefined ? answer : { ...answer, models };
64
168
  }
@@ -66,10 +170,10 @@ export function createAgentsHouseAdapter(options) {
66
170
  const held = handed.get(entry.locator);
67
171
  if (held?.roomVersion !== request.fromRoomVersion)
68
172
  return null;
69
- const advanced = notifications.advance(entry, from, (path, revision) => held.revisions.get(path) === revision);
173
+ const advanced = await stepped(notifications.advance(entry, from, (path, revision) => held.revisions[path] === revision));
70
174
  if (advanced === null)
71
175
  return null;
72
- handed.set(entry.locator, streamedHanded(held, advanced));
176
+ await hand(entry.locator, streamedHanded(held, advanced));
73
177
  const models = await streamedModels(advanced.schemas, entry, 'credential');
74
178
  return models === undefined ? advanced.page : { ...advanced.page, models };
75
179
  }
@@ -85,14 +189,20 @@ export function createAgentsHouseAdapter(options) {
85
189
  });
86
190
  notifications.send(kept);
87
191
  }
88
- function adopted(entry, outcome) {
192
+ async function adoptedMove(entry, into, outcome) {
193
+ if (outcome.kind === 'accepted' && outcome.destination !== undefined) {
194
+ await adopted(into, { kind: 'accepted', ...outcome.destination });
195
+ }
196
+ return adopted(entry, outcome);
197
+ }
198
+ async function adopted(entry, outcome) {
89
199
  if (outcome.kind !== 'accepted')
90
200
  return outcome;
91
201
  const held = handed.get(entry.locator);
92
202
  if (held === undefined)
93
203
  return outcome;
94
204
  const follows = followsVersion(held.roomVersion, outcome.roomVersion);
95
- handed.set(entry.locator, acceptedHanded(held, outcome, follows ? outcome.roomVersion : held.roomVersion));
205
+ await hand(entry.locator, acceptedHanded(held, outcome, follows ? outcome.roomVersion : held.roomVersion));
96
206
  const at = logPosition(outcome.roomVersion);
97
207
  if (follows && at !== null)
98
208
  notifications.reach(entry.authority, at);
@@ -115,99 +225,107 @@ export function createAgentsHouseAdapter(options) {
115
225
  return result;
116
226
  return { ...result, at: { locator, roomVersion: roomVersion({ position, logEpoch }) } };
117
227
  }
118
- function weight(entry) {
119
- return contentPrefixes(entry).length;
228
+ async function holding(asks) {
229
+ const bound = Math.floor(declared().max_held_files / Math.max(1, asks.filter((ask) => ask.held !== undefined).length));
230
+ const held = [];
231
+ for (const ask of asks) {
232
+ if (ask.held === undefined || bound === 0)
233
+ continue;
234
+ const paths = await sortedHeld(ask.held);
235
+ const files = heldFiles(ask.entry.authority, ask.held, paths, bound);
236
+ if (files !== null)
237
+ held.push(files);
238
+ }
239
+ return held;
120
240
  }
121
- async function bootstrapped(entries, answer) {
122
- let open = entries.map((entry, ask) => ({ entry, ask }));
241
+ async function bootstrapped(asks, answer) {
242
+ let open = asks.map((asked, ask) => ({ entry: asked.entry, asked, ask }));
123
243
  while (open.length > 0) {
124
244
  const unreached = [];
125
- for (const part of selections(open, ({ entry }) => weight(entry), declared().max_prefixes)) {
245
+ for (const part of selections(open, declared().max_prefixes)) {
246
+ const held = await holding(part.map(({ asked }) => asked));
126
247
  const answered = await transport.door(DOOR_BOOTSTRAP, {
127
- prefixes: part.flatMap(({ entry }) => contentPrefixes(entry)),
248
+ prefixes: part.map(({ entry }) => authorityRoot(entry.handle)),
249
+ ...(held.length === 0 ? {} : { held }),
128
250
  });
129
251
  const classified = part.map(({ entry }) => classifyDoor(answered.status, answered.body, entry.locator));
130
- const envelope = classified[0] === null ? parseEnvelope(answered.body) : null;
252
+ const parted = classified[0] === null ? parseParted(answered.body) : null;
131
253
  for (const [index, asked] of part.entries()) {
132
254
  const failure = classified[index];
133
255
  if (failure !== null && failure !== undefined) {
134
256
  answer(asked.ask, failure);
135
257
  continue;
136
258
  }
137
- if (envelope === null) {
259
+ if (parted === null) {
138
260
  answer(asked.ask, UNCERTAIN_READ);
139
261
  continue;
140
262
  }
141
- const scope = envelope.scopes.find((candidate) => matchesScope(candidate, asked.entry));
142
- if (scope === undefined && envelope.next !== undefined) {
263
+ const room = parted.rooms.find((candidate) => candidate.anchor === asked.entry.authority);
264
+ if (room === undefined && parted.next !== undefined) {
143
265
  unreached.push(asked);
144
266
  continue;
145
267
  }
146
- answer(asked.ask, await bootstrappedRoom(envelope, scope, asked.entry, null));
268
+ if (room === undefined) {
269
+ answer(asked.ask, { kind: 'room-not-readable', locator: asked.entry.locator });
270
+ continue;
271
+ }
272
+ const kept = held.find((files) => files.room === asked.entry.authority);
273
+ answer(asked.ask, await partPage(asked.asked, {
274
+ room,
275
+ next: 0,
276
+ seen: [],
277
+ ...(kept === undefined ? {} : { held: kept }),
278
+ }));
147
279
  }
148
280
  }
149
281
  open = unreached;
150
282
  }
151
283
  }
152
- async function continuedBootstrap(entry, continued) {
153
- const after = { room: entry.authority, path: continued.after };
154
- const answered = await transport.door(DOOR_BOOTSTRAP, {
155
- prefixes: contentPrefixes(entry),
156
- after,
157
- });
158
- const failure = classifyDoor(answered.status, answered.body, entry.locator);
159
- if (failure !== null)
160
- return failure;
161
- const envelope = parseEnvelope(answered.body);
162
- if (envelope === null)
163
- return UNCERTAIN_READ;
164
- const scope = envelope.scopes.find((candidate) => matchesScope(candidate, entry));
165
- return bootstrappedRoom(envelope, scope, entry, continued);
166
- }
167
284
  function layered(asks) {
168
285
  const depths = new Map();
169
286
  const layers = [];
170
287
  for (const ask of asks) {
171
- const depth = depths.get(ask.entry.authority) ?? 0;
172
- depths.set(ask.entry.authority, depth + 1);
288
+ const depth = depths.get(ask.ask.entry.authority) ?? 0;
289
+ depths.set(ask.ask.entry.authority, depth + 1);
173
290
  (layers[depth] ??= []).push(ask);
174
291
  }
175
292
  return layers;
176
293
  }
177
294
  async function enumerated(asks) {
178
- const answers = new Map();
179
- const parts = layered(asks).flatMap((layer) => selections(layer, (ask) => weight(ask.entry), declared().max_prefixes));
295
+ const answers = [];
296
+ const parts = layered(asks.map((ask, index) => ({ ask, index }))).flatMap((layer) => selections(layer, declared().max_prefixes));
180
297
  for (const part of parts) {
181
298
  let open = [...part];
182
299
  while (open.length > 0) {
183
300
  const answer = await transport.door(DOOR_ENUMERATE, {
184
- prefixes: open.flatMap((ask) => contentPrefixes(ask.entry)),
185
- positions: open.map((ask) => presented(ask.entry.authority, ask.from)),
301
+ prefixes: open.map(({ ask }) => authorityRoot(ask.entry.handle)),
302
+ positions: open.map(({ ask }) => presented(ask.entry.authority, ask.from)),
186
303
  });
187
- const classified = open.map((ask) => classifyRead(answer.status, answer.body, ask.entry.locator));
304
+ const classified = open.map(({ ask }) => classifyRead(answer.status, answer.body, ask.entry.locator));
188
305
  if (classified[0]?.kind === 'beyond-horizon') {
189
306
  const named = doorRefusal(answer.body)?.fields.authority;
190
- const expired = open.filter((ask) => ask.entry.authority === named);
307
+ const expired = open.filter(({ ask }) => ask.entry.authority === named);
191
308
  const answered = expired.length === 0 ? UNCERTAIN_READ : { kind: 'beyond-horizon' };
192
309
  const gone = expired.length === 0 ? open : expired;
193
310
  for (const ask of gone)
194
- answers.set(ask, answered);
311
+ answers[ask.index] = answered;
195
312
  open = open.filter((ask) => !gone.includes(ask));
196
313
  continue;
197
314
  }
198
315
  const enumeration = classified[0] === null ? parseEnumeration(answer.body, 'credential') : null;
199
- for (const [index, ask] of open.entries()) {
200
- const failure = classified[index];
316
+ for (const [answerIndex, { ask, index }] of open.entries()) {
317
+ const failure = classified[answerIndex];
201
318
  if (failure !== null && failure !== undefined) {
202
- answers.set(ask, failure);
319
+ answers[index] = failure;
203
320
  continue;
204
321
  }
205
- answers.set(ask, enumeration === null ? UNCERTAIN_READ : await enumeratedRoom(enumeration, ask));
322
+ answers[index] =
323
+ enumeration === null ? UNCERTAIN_READ : await enumeratedRoom(enumeration, ask);
206
324
  }
207
325
  open = [];
208
326
  }
209
327
  }
210
- return asks.map((ask) => answers.get(ask) ?? UNCERTAIN_READ);
328
+ return answers;
211
329
  }
212
330
  const bootstrapTogether = gatheredEach(bootstrapped);
213
331
  const enumerateTogether = gathered(enumerated);
@@ -219,7 +337,9 @@ export function createAgentsHouseAdapter(options) {
219
337
  }
220
338
  const minted = await transport.door(DOOR_BATCH_UPLOAD, { ...head, paths, bytes, sha256 });
221
339
  if (minted.status !== 200) {
222
- return { outcome: mutationOutcome(minted.status, minted.body, entry.locator, entry.handle) };
340
+ return {
341
+ outcome: await stepped(mutationOutcome(minted.status, minted.body, entry.locator, entry.handle)),
342
+ };
223
343
  }
224
344
  const grant = parseGrant(minted.body);
225
345
  if (grant === null || grant.method !== 'POST')
@@ -238,7 +358,7 @@ export function createAgentsHouseAdapter(options) {
238
358
  await keep(upload);
239
359
  return { body: { ...head, paths, upload, runtime: notifications.runtime } };
240
360
  }
241
- async function staged(answer, batch) {
361
+ async function staged(answer, { changeBatch: batch, file }) {
242
362
  const refusal = doorRefusal(answer.body);
243
363
  if (refusal?.code !== 'files_unstaged')
244
364
  return null;
@@ -253,7 +373,7 @@ export function createAgentsHouseAdapter(options) {
253
373
  const delivered = await transport.transfer({
254
374
  act: 'binary_stage',
255
375
  url,
256
- body: change.blob,
376
+ body: await file(change.localId),
257
377
  operation,
258
378
  });
259
379
  if (delivered.status !== 200)
@@ -261,7 +381,7 @@ export function createAgentsHouseAdapter(options) {
261
381
  }
262
382
  return true;
263
383
  }
264
- async function submit(batch, head, changes, delta, bytes, entry, handed, keep) {
384
+ async function submit(request, head, changes, delta, bytes, entry, handed, keep, into) {
265
385
  const inline = { ...head, changes, runtime: notifications.runtime };
266
386
  const prepared = uploadable(inline, bytes, declared())
267
387
  ? await uploaded(head, changes, delta, bytes, entry, handed, keep)
@@ -269,15 +389,20 @@ export function createAgentsHouseAdapter(options) {
269
389
  if ('outcome' in prepared)
270
390
  return prepared.outcome;
271
391
  const answer = await transport.door(DOOR_BATCH, prepared.body);
272
- const stagedFiles = await staged(answer, batch);
392
+ const stagedFiles = await staged(answer, request);
273
393
  if (stagedFiles === false)
274
394
  return UNCERTAIN;
275
395
  const settled = stagedFiles === null ? answer : await transport.door(DOOR_BATCH, prepared.body);
276
- if (stagedFiles === true && (await staged(settled, batch)) !== null)
396
+ if (stagedFiles === true && (await staged(settled, request)) !== null)
277
397
  return UNCERTAIN;
278
- return mutationOutcome(settled.status, settled.body, entry.locator, entry.handle);
398
+ return stepped(mutationOutcome(settled.status, settled.body, entry.locator, entry.handle, into === undefined
399
+ ? undefined
400
+ : {
401
+ source: { authority: entry.authority, handle: entry.handle },
402
+ destination: { authority: into.authority, handle: into.handle },
403
+ }));
279
404
  }
280
- async function downloaded(answer, locator) {
405
+ function granted(answer, locator) {
281
406
  const failure = classifyDoor(answer.status, answer.body, locator);
282
407
  if (failure !== null) {
283
408
  return failure.kind === 'scoped-refusal'
@@ -290,19 +415,40 @@ export function createAgentsHouseAdapter(options) {
290
415
  : failure;
291
416
  }
292
417
  const grant = parseGrant(answer.body);
293
- if (grant === null || grant.method !== 'GET')
418
+ const name = answer.body.name;
419
+ if (grant === null || grant.method !== 'GET' || typeof name !== 'string') {
294
420
  return UNCERTAIN_READ;
295
- const read = await transport.download(grant.url);
421
+ }
422
+ return { kind: 'link', url: grant.url, name };
423
+ }
424
+ async function downloaded(answer, locator) {
425
+ const link = granted(answer, locator);
426
+ if (link.kind !== 'link')
427
+ return link;
428
+ const read = await transport.download(link.url);
296
429
  if (read.status !== 200 || read.blob === null)
297
430
  return UNCERTAIN_READ;
298
431
  return { kind: 'binary', blob: read.blob };
299
432
  }
433
+ function pathGrant(request) {
434
+ const entry = authorityFor(request.locator);
435
+ if (entry === undefined)
436
+ return null;
437
+ return transport.door(DOOR_FILES_GET, {
438
+ room_ref: entry.authority,
439
+ path: request.path,
440
+ revision: request.revision,
441
+ });
442
+ }
300
443
  return {
301
444
  async authorize() {
302
445
  return options.authorize();
303
446
  },
304
- async discover(_authorization) {
305
- const answer = await transport.door(DOOR_DISCOVER, { prefixes: [...DISCOVER_PREFIXES] });
447
+ async discover(_authorization, scope) {
448
+ const answer = await transport.door(DOOR_DISCOVER, {
449
+ prefixes: [...DISCOVER_PREFIXES],
450
+ ...(scope === undefined ? {} : { rooms: scope.authorities }),
451
+ });
306
452
  const failed = discoveryFailure(answer.status, answer.body);
307
453
  if (failed !== null)
308
454
  return failed;
@@ -311,26 +457,42 @@ export function createAgentsHouseAdapter(options) {
311
457
  return { kind: 'transport-uncertainty' };
312
458
  bounds = document.bounds;
313
459
  const entries = trackedFrom(document);
314
- tracked.clear();
460
+ const covered = scope === undefined
461
+ ? null
462
+ : [
463
+ ...new Set([
464
+ ...[...tracked.values()]
465
+ .filter((entry) => scope.authorities.includes(entry.authority))
466
+ .map((entry) => entry.locator),
467
+ ...entries.map((entry) => entry.locator),
468
+ ]),
469
+ ];
470
+ if (covered === null)
471
+ tracked.clear();
472
+ else
473
+ for (const locator of covered)
474
+ tracked.delete(locator);
315
475
  for (const entry of entries)
316
476
  tracked.set(entry.locator, entry);
317
477
  for (const locator of handed.keys())
318
478
  if (!tracked.has(locator))
319
479
  handed.delete(locator);
320
- notifications.track(entries);
480
+ notifications.track([...tracked.values()]);
321
481
  return {
322
- ...mapDiscovery(document, document.authorities.map((authority) => discoveredRoom(authority)), capabilities(document)),
482
+ ...mapDiscovery(document, document.authorities.map((authority) => discoveredRoom(authority)), capabilities(document.actions, [...tracked.values()])),
323
483
  member: document.member,
484
+ ...(covered === null ? {} : { covered }),
324
485
  };
325
486
  },
326
487
  async bootstrap(_authorization, request) {
327
488
  const entry = authorityFor(request.locator);
328
489
  if (entry === undefined)
329
490
  return { kind: 'room-not-readable', locator: request.locator };
491
+ const asked = { entry, held: request.held };
330
492
  if (request.pageToken !== undefined) {
331
- return continuedBootstrap(entry, JSON.parse(request.pageToken));
493
+ return partPage(asked, JSON.parse(request.pageToken));
332
494
  }
333
- return bootstrapTogether(entry);
495
+ return bootstrapTogether(asked);
334
496
  },
335
497
  async refresh(_authorization, request) {
336
498
  const entry = authorityFor(request.locator);
@@ -350,18 +512,27 @@ export function createAgentsHouseAdapter(options) {
350
512
  const entry = authorityFor(request.locator);
351
513
  if (entry === undefined)
352
514
  return { kind: 'room-not-readable', locator: request.locator };
353
- const mapped = houseChanges(request.changeBatch, entry.handle);
515
+ const batch = request.changeBatch;
516
+ const destination = batch.between?.destination.room;
517
+ const into = destination === undefined ? undefined : authorityFor(destination);
518
+ if (destination !== undefined && into === undefined) {
519
+ return { kind: 'room-not-readable', locator: destination };
520
+ }
521
+ const mapped = into === undefined
522
+ ? houseChanges(batch, entry.handle)
523
+ : houseMove(batch, entry.handle, into.handle);
354
524
  if (mapped.kind === 'unmapped')
355
525
  return rejectedMapping(mapped);
356
- const head = operationHead(request.changeBatch);
526
+ const head = operationHead(batch);
357
527
  const delta = deltaBody(mapped.changes);
358
528
  const bytes = deltaBytes(delta);
359
529
  const handed = request.upload;
360
- const batch = request.changeBatch;
361
- const outcome = await submit(batch, head, mapped.changes, delta, bytes, entry, handed, keep);
530
+ const answer = (upload) => submit(request, head, mapped.changes, delta, bytes, entry, upload, keep, into);
531
+ const settle = (outcome) => into === undefined ? adopted(entry, outcome) : adoptedMove(entry, into, outcome);
532
+ const outcome = await answer(handed);
362
533
  if (handed === undefined || !lapsedGrant(outcome))
363
- return adopted(entry, outcome);
364
- return adopted(entry, await submit(batch, head, mapped.changes, delta, bytes, entry, undefined, keep));
534
+ return settle(outcome);
535
+ return settle(await answer(undefined));
365
536
  },
366
537
  async history(_authorization, request) {
367
538
  const entry = authorityFor(request.locator);
@@ -380,15 +551,10 @@ export function createAgentsHouseAdapter(options) {
380
551
  },
381
552
  async binary(_authorization, request) {
382
553
  if (request.kind === 'path') {
383
- const entry = authorityFor(request.locator);
384
- if (entry === undefined)
554
+ const asked = pathGrant(request);
555
+ if (asked === null)
385
556
  return { kind: 'room-not-readable', locator: request.locator };
386
- const answer = await transport.door(DOOR_FILES_GET, {
387
- room_ref: entry.authority,
388
- path: request.path,
389
- revision: request.revision,
390
- });
391
- return downloaded(answer, request.locator);
557
+ return downloaded(await asked, request.locator);
392
558
  }
393
559
  const referrer = houseReferrer(request.referrer, tracked);
394
560
  if (referrer === null) {
@@ -400,14 +566,18 @@ export function createAgentsHouseAdapter(options) {
400
566
  });
401
567
  return downloaded(answer, referrer.locator);
402
568
  },
569
+ async link(_authorization, request) {
570
+ const asked = pathGrant(request);
571
+ if (asked === null)
572
+ return { kind: 'room-not-readable', locator: request.locator };
573
+ return granted(await asked, request.locator);
574
+ },
403
575
  referrerScope(referrer) {
404
576
  const held = houseReferrer(referrer, tracked);
405
577
  if (held === null)
406
578
  return null;
407
- if (held.locator === '')
408
- return { kind: 'conversations' };
409
579
  const { path } = held.referrer;
410
- return { kind: 'room', locator: held.locator, ...(path === undefined ? {} : { path }) };
580
+ return { locator: held.locator, ...(path === undefined ? {} : { path }) };
411
581
  },
412
582
  reached(locator, version, target) {
413
583
  return reachedVersion(version, target, authorityFor(locator)?.logEpoch);
@@ -418,7 +588,16 @@ export function createAgentsHouseAdapter(options) {
418
588
  async action(_authorization, request) {
419
589
  return withAt(await houseAction(transport, request));
420
590
  },
421
- roomStream: { hold: held, reopen: notifications.reopen, observe: notifications.observe },
591
+ roomStream: {
592
+ hold: held,
593
+ reopen: notifications.reopen,
594
+ observe: notifications.observe,
595
+ published(locator, roomVersion) {
596
+ const at = logPosition(roomVersion);
597
+ if (at !== null)
598
+ notifications.published(locator, at);
599
+ },
600
+ },
422
601
  conversations: notifications.conversations,
423
602
  serviceMode: notifications.serviceMode,
424
603
  };
@@ -13,15 +13,16 @@ export type HouseBounds = {
13
13
  max_inline_body_bytes: number;
14
14
  max_upload_bytes: number;
15
15
  max_prefixes: number;
16
+ max_held_files: number;
16
17
  change_retention_seconds: number;
17
18
  operation_retention_seconds: number;
19
+ max_move_documents?: number;
18
20
  };
19
21
  export type HouseOperations = {
20
- read: readonly string[];
21
- sync: readonly string[];
22
22
  write: readonly string[];
23
23
  delete: readonly string[];
24
24
  };
25
+ export type HouseReadLevel = 'mcp' | 'online' | 'local';
25
26
  export type HouseModel = {
26
27
  root: string;
27
28
  paths: readonly string[];
@@ -34,6 +35,7 @@ export type HouseAuthority = LogPosition & {
34
35
  houseModel: HouseModel | null;
35
36
  managedReplication: 'admitted' | 'unavailable';
36
37
  protectedPaths: readonly string[];
38
+ read: HouseReadLevel;
37
39
  operations: HouseOperations;
38
40
  };
39
41
  export type HouseDiscoveryDocument = {
@@ -65,7 +67,7 @@ export declare function siteTracked(document: HouseSiteDiscovery): TrackedAuthor
65
67
  export declare function trackedFrom(document: HouseDiscoveryDocument): TrackedAuthority[];
66
68
  export declare function providerLimits(bounds: HouseBounds): readonly ProviderLimit[];
67
69
  export declare function declaredRetention(bounds: HouseBounds): Retention;
68
- export declare function capabilities(document: HouseDiscoveryDocument): readonly Capability[];
70
+ export declare function capabilities(actions: HouseDiscoveryDocument['actions'], rooms: readonly Pick<TrackedAuthority, 'authority' | 'houseModel'>[]): readonly Capability[];
69
71
  export declare function siteCapabilities(): readonly Capability[];
70
72
  export declare function discoveredRoom(authority: HouseAuthority): DiscoveredRoom;
71
73
  export declare function mapDiscovery(document: Pick<HouseDiscoveryDocument, 'bounds'>, rooms: readonly DiscoveredRoom[], declared: readonly Capability[]): Discovery;