@agentshouse/mdruntime-adapter 0.1.0-c6e64cd278293 → 0.1.0-c6eb3c1455065

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,32 +1,48 @@
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, wholeView, } from './read.js';
6
+ import { authorityRoot, bootstrapping, emptyScope, housePart, parseEnumeration, parseFileAddress, parseGrant, parseParted, refreshFrom, viewOf, wholeView, } from './read.js';
7
+ import { PART_ATTEMPTS, unverifiedPart } from './part.js';
6
8
  import { sha256Hex } from './digest.js';
7
9
  import { acceptedHanded, enumeratedHanded, heldHanded, scopeHanded, streamedHanded, } from './handed.js';
8
10
  import { houseAction } from './actions.js';
9
11
  import { classifyDoor, classifyRead, discoveryFailure, doorRefusal } from './refusal.js';
10
12
  import { houseNotifications } from './stream.js';
11
13
  import { DOOR_BATCH, DOOR_BATCH_UPLOAD, DOOR_BOOTSTRAP, DOOR_DISCOVER, DOOR_ENUMERATE, DOOR_FILES_GET, DOOR_REVISIONS, } from './transport.js';
12
- 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';
13
15
  import { houseReferrer } from './referrer.js';
16
+ import { HOUSE_ROOM_TYPES } from './room-types.js';
14
17
  import { followsVersion, logPosition, presented, reachedVersion, roomVersion, } from './version.js';
15
18
  const UNCERTAIN = { kind: 'transport-uncertainty' };
16
19
  const UNCERTAIN_READ = { kind: 'transport-uncertainty' };
20
+ const EXPIRED_ADDRESS = 403;
17
21
  function lapsedGrant(outcome) {
18
22
  return outcome.kind === 'rejected' && outcome.code === 'upload_unavailable';
19
23
  }
20
- function heldFiles(authority, held, after, budget) {
21
- if (held === undefined || budget === 0)
22
- return null;
23
- const paths = Object.keys(held.revisions)
24
- .filter((path) => held.documents[path] !== undefined && (after === null || path > after))
25
- .sort();
26
- if (paths.length === 0)
27
- return null;
28
- const files = paths.slice(0, budget).map((path) => ({ path, revision: held.revisions[path] }));
29
- return { room: authority, through: paths.length > budget ? files.at(-1).path : null, files };
24
+ const heldPaths = new WeakMap();
25
+ async function sortedHeld(held) {
26
+ const known = heldPaths.get(held.documents);
27
+ if (known !== undefined)
28
+ return known;
29
+ const sorted = await stepped(sortedKeys(held.documents));
30
+ heldPaths.set(held.documents, sorted);
31
+ return sorted;
32
+ }
33
+ function heldFiles(authority, held, paths, bound) {
34
+ const files = [];
35
+ let through = null;
36
+ for (let at = 0; at < paths.length && through === null; at += 1) {
37
+ const revision = held.revisions[paths[at]];
38
+ if (revision === undefined)
39
+ continue;
40
+ if (files.length === bound)
41
+ through = files.at(-1).path;
42
+ else
43
+ files.push({ path: paths[at], revision });
44
+ }
45
+ return files.length === 0 ? null : { room: authority, through, files };
30
46
  }
31
47
  async function reused(scope, held) {
32
48
  const sent = new Set(scope.contents.map((entry) => entry.path));
@@ -57,42 +73,71 @@ export function createAgentsHouseAdapter(options) {
57
73
  throw new Error('no Discovery declared the bounds');
58
74
  return bounds;
59
75
  }
60
- async function bootstrappedRoom(envelope, sent, asked, continued) {
76
+ function hand(locator, steps) {
77
+ const held = handed.get(locator);
78
+ const next = stepped(steps);
79
+ if (!(next instanceof Promise)) {
80
+ handed.set(locator, next);
81
+ return;
82
+ }
83
+ return next.then((room) => {
84
+ if (handed.get(locator) === held)
85
+ handed.set(locator, room);
86
+ else
87
+ handed.delete(locator);
88
+ });
89
+ }
90
+ async function fetchedPart(entry, continued, listed) {
91
+ for (let attempt = 0; attempt < PART_ATTEMPTS; attempt += 1) {
92
+ const answered = await transport.door(DOOR_BOOTSTRAP, {
93
+ prefixes: [authorityRoot(entry.handle)],
94
+ part: listed.part,
95
+ ...(continued.held === undefined ? {} : { held: [continued.held] }),
96
+ });
97
+ const failure = classifyRead(answered.status, answered.body, entry.locator);
98
+ if (failure !== null)
99
+ return failure;
100
+ const scope = await housePart(continued.room, answered.body, listed, 'credential');
101
+ if (scope !== null)
102
+ return scope;
103
+ }
104
+ return unverifiedPart(entry.locator);
105
+ }
106
+ async function partPage(asked, continued) {
61
107
  const { entry } = asked;
62
- if (sent === undefined)
63
- return { kind: 'room-not-readable', locator: entry.locator };
108
+ const listed = continued.room.parts[continued.next];
109
+ const sent = listed === undefined
110
+ ? emptyScope(continued.room)
111
+ : await fetchedPart(entry, continued, listed);
112
+ if ('kind' in sent)
113
+ return sent;
64
114
  const scope = await reused(sent, asked.held);
65
115
  if (scope === null) {
66
116
  return asked.held === undefined
67
117
  ? UNCERTAIN_READ
68
- : continued === null
69
- ? bootstrapTogether({ entry, held: undefined })
70
- : continuedBootstrap({ entry, held: undefined }, continued);
118
+ : bootstrapTogether({ entry, held: undefined });
71
119
  }
72
- const cursor = envelope.next?.room === entry.authority ? envelope.next : undefined;
73
- const next = cursor?.path ?? null;
74
- if (continued === null && next === null)
75
- handed.set(entry.locator, scopeHanded(scope));
120
+ const next = continued.next + 1;
121
+ const more = next < continued.room.parts.length;
122
+ if (continued.next === 0 && !more)
123
+ await hand(entry.locator, scopeHanded(scope));
76
124
  else
77
125
  handed.delete(entry.locator);
78
126
  notifications.reach(entry.authority, scope);
79
127
  const seen = [
80
- ...(continued?.seen ?? []),
128
+ ...continued.seen,
81
129
  ...entry.schemas.filter((schema) => scope.files.some((file) => file.path === schema)),
82
130
  ];
83
- const models = await partModels(scope, entry, new Set(seen), next === null);
84
- const continuation = next === null
85
- ? null
86
- : { after: next, seen, ...(cursor?.cut === undefined ? {} : { cut: cursor.cut }) };
131
+ const models = await partModels(scope, entry, new Set(seen), !more);
87
132
  return {
88
- ...bootstrapOf(scope),
133
+ ...(await stepped(bootstrapping(scope))),
89
134
  ...(models === undefined ? {} : { models }),
90
- ...(continuation === null ? {} : { pageToken: JSON.stringify(continuation) }),
135
+ ...(more ? { pageToken: JSON.stringify({ ...continued, next, seen }) } : {}),
91
136
  };
92
137
  }
93
138
  async function enumeratedPage(ask, page) {
94
139
  const answer = await transport.door(DOOR_ENUMERATE, {
95
- prefixes: contentPrefixes(ask.entry),
140
+ prefixes: [authorityRoot(ask.entry.handle)],
96
141
  positions: [
97
142
  {
98
143
  ...presented(ask.entry.authority, { position: page.after, logEpoch: ask.from.logEpoch }),
@@ -116,10 +161,10 @@ export function createAgentsHouseAdapter(options) {
116
161
  if ('kind' in view)
117
162
  return view;
118
163
  notifications.reach(ask.entry.authority, view);
119
- const answer = refreshFrom(view, ask.fromRoomVersion);
164
+ const answer = await refreshFrom(view, ask.fromRoomVersion);
120
165
  if (answer.kind !== 'net-advancement')
121
166
  return answer;
122
- handed.set(ask.entry.locator, enumeratedHanded(view, handed.get(ask.entry.locator)));
167
+ await hand(ask.entry.locator, enumeratedHanded(view, handed.get(ask.entry.locator)));
123
168
  const models = await viewModels(view, ask.entry);
124
169
  return models === undefined ? answer : { ...answer, models };
125
170
  }
@@ -127,14 +172,14 @@ export function createAgentsHouseAdapter(options) {
127
172
  const held = handed.get(entry.locator);
128
173
  if (held?.roomVersion !== request.fromRoomVersion)
129
174
  return null;
130
- const advanced = notifications.advance(entry, from, (path, revision) => held.revisions.get(path) === revision);
175
+ const advanced = await stepped(notifications.advance(entry, from, (path, revision) => held.revisions[path] === revision));
131
176
  if (advanced === null)
132
177
  return null;
133
- handed.set(entry.locator, streamedHanded(held, advanced));
178
+ await hand(entry.locator, streamedHanded(held, advanced));
134
179
  const models = await streamedModels(advanced.schemas, entry, 'credential');
135
180
  return models === undefined ? advanced.page : { ...advanced.page, models };
136
181
  }
137
- function held(rooms, demand) {
182
+ function held(rooms) {
138
183
  const kept = rooms.flatMap((room) => {
139
184
  const entry = tracked.get(room.locator);
140
185
  const at = logPosition(room.roomVersion);
@@ -144,16 +189,22 @@ export function createAgentsHouseAdapter(options) {
144
189
  notifications.reach(entry.authority, at);
145
190
  return [presented(entry.authority, at)];
146
191
  });
147
- notifications.send(kept, demand === null ? null : { rooms: [...demand.rooms], conversations: demand.conversations });
192
+ notifications.send(kept);
148
193
  }
149
- function adopted(entry, outcome) {
194
+ async function adoptedMove(entry, into, outcome) {
195
+ if (outcome.kind === 'accepted' && outcome.destination !== undefined) {
196
+ await adopted(into, { kind: 'accepted', ...outcome.destination });
197
+ }
198
+ return adopted(entry, outcome);
199
+ }
200
+ async function adopted(entry, outcome) {
150
201
  if (outcome.kind !== 'accepted')
151
202
  return outcome;
152
203
  const held = handed.get(entry.locator);
153
204
  if (held === undefined)
154
205
  return outcome;
155
206
  const follows = followsVersion(held.roomVersion, outcome.roomVersion);
156
- handed.set(entry.locator, acceptedHanded(held, outcome, follows ? outcome.roomVersion : held.roomVersion));
207
+ await hand(entry.locator, acceptedHanded(held, outcome, follows ? outcome.roomVersion : held.roomVersion));
157
208
  const at = logPosition(outcome.roomVersion);
158
209
  if (follows && at !== null)
159
210
  notifications.reach(entry.authority, at);
@@ -176,68 +227,62 @@ export function createAgentsHouseAdapter(options) {
176
227
  return result;
177
228
  return { ...result, at: { locator, roomVersion: roomVersion({ position, logEpoch }) } };
178
229
  }
179
- function weight(entry) {
180
- return contentPrefixes(entry).length;
181
- }
182
- function holding(asks, after) {
183
- const budget = Math.floor(declared().max_held_files / Math.max(1, asks.filter((ask) => ask.held !== undefined).length));
184
- return asks.flatMap((ask) => heldFiles(ask.entry.authority, ask.held, after, budget) ?? []);
230
+ async function holding(asks) {
231
+ const bound = Math.floor(declared().max_held_files / Math.max(1, asks.filter((ask) => ask.held !== undefined).length));
232
+ const held = [];
233
+ for (const ask of asks) {
234
+ if (ask.held === undefined || bound === 0)
235
+ continue;
236
+ const paths = await sortedHeld(ask.held);
237
+ const files = heldFiles(ask.entry.authority, ask.held, paths, bound);
238
+ if (files !== null)
239
+ held.push(files);
240
+ }
241
+ return held;
185
242
  }
186
243
  async function bootstrapped(asks, answer) {
187
244
  let open = asks.map((asked, ask) => ({ entry: asked.entry, asked, ask }));
188
245
  while (open.length > 0) {
189
246
  const unreached = [];
190
- for (const part of selections(open, ({ entry }) => weight(entry), declared().max_prefixes)) {
191
- const held = holding(part.map(({ asked }) => asked), null);
247
+ for (const part of selections(open, declared().max_prefixes)) {
248
+ const held = await holding(part.map(({ asked }) => asked));
192
249
  const answered = await transport.door(DOOR_BOOTSTRAP, {
193
- prefixes: part.flatMap(({ entry }) => contentPrefixes(entry)),
250
+ prefixes: part.map(({ entry }) => authorityRoot(entry.handle)),
194
251
  ...(held.length === 0 ? {} : { held }),
195
252
  });
196
253
  const classified = part.map(({ entry }) => classifyDoor(answered.status, answered.body, entry.locator));
197
- const envelope = classified[0] === null ? parseEnvelope(answered.body) : null;
254
+ const parted = classified[0] === null ? parseParted(answered.body) : null;
198
255
  for (const [index, asked] of part.entries()) {
199
256
  const failure = classified[index];
200
257
  if (failure !== null && failure !== undefined) {
201
258
  answer(asked.ask, failure);
202
259
  continue;
203
260
  }
204
- if (envelope === null) {
261
+ if (parted === null) {
205
262
  answer(asked.ask, UNCERTAIN_READ);
206
263
  continue;
207
264
  }
208
- const scope = envelope.scopes.find((candidate) => matchesScope(candidate, asked.entry));
209
- if (scope === undefined && envelope.next !== undefined) {
265
+ const room = parted.rooms.find((candidate) => candidate.anchor === asked.entry.authority);
266
+ if (room === undefined && parted.next !== undefined) {
210
267
  unreached.push(asked);
211
268
  continue;
212
269
  }
213
- answer(asked.ask, await bootstrappedRoom(envelope, scope, asked.asked, null));
270
+ if (room === undefined) {
271
+ answer(asked.ask, { kind: 'room-not-readable', locator: asked.entry.locator });
272
+ continue;
273
+ }
274
+ const kept = held.find((files) => files.room === asked.entry.authority);
275
+ answer(asked.ask, await partPage(asked.asked, {
276
+ room,
277
+ next: 0,
278
+ seen: [],
279
+ ...(kept === undefined ? {} : { held: kept }),
280
+ }));
214
281
  }
215
282
  }
216
283
  open = unreached;
217
284
  }
218
285
  }
219
- async function continuedBootstrap(asked, continued) {
220
- const { entry } = asked;
221
- const after = {
222
- room: entry.authority,
223
- path: continued.after,
224
- ...(continued.cut === undefined ? {} : { cut: continued.cut }),
225
- };
226
- const held = holding([asked], continued.after);
227
- const answered = await transport.door(DOOR_BOOTSTRAP, {
228
- prefixes: contentPrefixes(entry),
229
- after,
230
- ...(held.length === 0 ? {} : { held }),
231
- });
232
- const failure = classifyRead(answered.status, answered.body, entry.locator);
233
- if (failure !== null)
234
- return failure;
235
- const envelope = parseEnvelope(answered.body);
236
- if (envelope === null)
237
- return UNCERTAIN_READ;
238
- const scope = envelope.scopes.find((candidate) => matchesScope(candidate, entry));
239
- return bootstrappedRoom(envelope, scope, asked, continued);
240
- }
241
286
  function layered(asks) {
242
287
  const depths = new Map();
243
288
  const layers = [];
@@ -250,12 +295,12 @@ export function createAgentsHouseAdapter(options) {
250
295
  }
251
296
  async function enumerated(asks) {
252
297
  const answers = [];
253
- const parts = layered(asks.map((ask, index) => ({ ask, index }))).flatMap((layer) => selections(layer, ({ ask }) => weight(ask.entry), declared().max_prefixes));
298
+ const parts = layered(asks.map((ask, index) => ({ ask, index }))).flatMap((layer) => selections(layer, declared().max_prefixes));
254
299
  for (const part of parts) {
255
300
  let open = [...part];
256
301
  while (open.length > 0) {
257
302
  const answer = await transport.door(DOOR_ENUMERATE, {
258
- prefixes: open.flatMap(({ ask }) => contentPrefixes(ask.entry)),
303
+ prefixes: open.map(({ ask }) => authorityRoot(ask.entry.handle)),
259
304
  positions: open.map(({ ask }) => presented(ask.entry.authority, ask.from)),
260
305
  });
261
306
  const classified = open.map(({ ask }) => classifyRead(answer.status, answer.body, ask.entry.locator));
@@ -294,10 +339,12 @@ export function createAgentsHouseAdapter(options) {
294
339
  }
295
340
  const minted = await transport.door(DOOR_BATCH_UPLOAD, { ...head, paths, bytes, sha256 });
296
341
  if (minted.status !== 200) {
297
- return { outcome: mutationOutcome(minted.status, minted.body, entry.locator, entry.handle) };
342
+ return {
343
+ outcome: await stepped(mutationOutcome(minted.status, minted.body, entry.locator, entry.handle)),
344
+ };
298
345
  }
299
346
  const grant = parseGrant(minted.body);
300
- if (grant === null || grant.method !== 'POST')
347
+ if (grant === null)
301
348
  return { outcome: UNCERTAIN };
302
349
  const delivered = await transport.transfer({
303
350
  act: 'authored_batch_upload',
@@ -307,13 +354,13 @@ export function createAgentsHouseAdapter(options) {
307
354
  if (delivered.status !== 200)
308
355
  return { outcome: UNCERTAIN };
309
356
  const holding = parseGrant(delivered.body);
310
- if (holding === null || holding.method !== 'POST')
357
+ if (holding === null)
311
358
  return { outcome: UNCERTAIN };
312
359
  const upload = { url: holding.url, operation: holding.operation, bytes, sha256 };
313
360
  await keep(upload);
314
361
  return { body: { ...head, paths, upload, runtime: notifications.runtime } };
315
362
  }
316
- async function staged(answer, batch) {
363
+ async function staged(answer, { changeBatch: batch, file }) {
317
364
  const refusal = doorRefusal(answer.body);
318
365
  if (refusal?.code !== 'files_unstaged')
319
366
  return null;
@@ -328,7 +375,7 @@ export function createAgentsHouseAdapter(options) {
328
375
  const delivered = await transport.transfer({
329
376
  act: 'binary_stage',
330
377
  url,
331
- body: change.blob,
378
+ body: await file(change.localId),
332
379
  operation,
333
380
  });
334
381
  if (delivered.status !== 200)
@@ -336,7 +383,7 @@ export function createAgentsHouseAdapter(options) {
336
383
  }
337
384
  return true;
338
385
  }
339
- async function submit(batch, head, changes, delta, bytes, entry, handed, keep) {
386
+ async function submit(request, head, changes, delta, bytes, entry, handed, keep, into) {
340
387
  const inline = { ...head, changes, runtime: notifications.runtime };
341
388
  const prepared = uploadable(inline, bytes, declared())
342
389
  ? await uploaded(head, changes, delta, bytes, entry, handed, keep)
@@ -344,15 +391,20 @@ export function createAgentsHouseAdapter(options) {
344
391
  if ('outcome' in prepared)
345
392
  return prepared.outcome;
346
393
  const answer = await transport.door(DOOR_BATCH, prepared.body);
347
- const stagedFiles = await staged(answer, batch);
394
+ const stagedFiles = await staged(answer, request);
348
395
  if (stagedFiles === false)
349
396
  return UNCERTAIN;
350
397
  const settled = stagedFiles === null ? answer : await transport.door(DOOR_BATCH, prepared.body);
351
- if (stagedFiles === true && (await staged(settled, batch)) !== null)
398
+ if (stagedFiles === true && (await staged(settled, request)) !== null)
352
399
  return UNCERTAIN;
353
- return mutationOutcome(settled.status, settled.body, entry.locator, entry.handle);
400
+ return stepped(mutationOutcome(settled.status, settled.body, entry.locator, entry.handle, into === undefined
401
+ ? undefined
402
+ : {
403
+ source: { authority: entry.authority, handle: entry.handle },
404
+ destination: { authority: into.authority, handle: into.handle },
405
+ }));
354
406
  }
355
- function granted(answer, locator) {
407
+ function addressed(answer, locator) {
356
408
  const failure = classifyDoor(answer.status, answer.body, locator);
357
409
  if (failure !== null) {
358
410
  return failure.kind === 'scoped-refusal'
@@ -364,38 +416,52 @@ export function createAgentsHouseAdapter(options) {
364
416
  }
365
417
  : failure;
366
418
  }
367
- const grant = parseGrant(answer.body);
368
- const name = answer.body.name;
369
- if (grant === null || grant.method !== 'GET' || typeof name !== 'string') {
370
- return UNCERTAIN_READ;
371
- }
372
- return { kind: 'link', url: grant.url, name };
419
+ const address = parseFileAddress(answer.body);
420
+ return address === null ? UNCERTAIN_READ : { kind: 'address', ...address };
421
+ }
422
+ function linked(answer, locator) {
423
+ const address = addressed(answer, locator);
424
+ return address.kind === 'address'
425
+ ? { kind: 'link', url: address.url, name: address.name }
426
+ : address;
373
427
  }
374
- async function downloaded(answer, locator) {
375
- const link = granted(answer, locator);
376
- if (link.kind !== 'link')
377
- return link;
378
- const read = await transport.download(link.url);
428
+ async function downloaded(ask, locator) {
429
+ let address = addressed(await ask(), locator);
430
+ if (address.kind !== 'address')
431
+ return address;
432
+ let read = await transport.download(address.url);
433
+ if (read.status === EXPIRED_ADDRESS) {
434
+ address = addressed(await ask(), locator);
435
+ if (address.kind !== 'address')
436
+ return address;
437
+ read = await transport.download(address.url);
438
+ }
379
439
  if (read.status !== 200 || read.blob === null)
380
440
  return UNCERTAIN_READ;
441
+ if ((await sha256Hex(await read.blob.arrayBuffer())) !== address.sha256)
442
+ return UNCERTAIN_READ;
381
443
  return { kind: 'binary', blob: read.blob };
382
444
  }
383
- function pathGrant(request) {
445
+ function pathAddress(request) {
384
446
  const entry = authorityFor(request.locator);
385
447
  if (entry === undefined)
386
448
  return null;
387
- return transport.door(DOOR_FILES_GET, {
449
+ return () => transport.door(DOOR_FILES_GET, {
388
450
  room_ref: entry.authority,
389
451
  path: request.path,
390
452
  revision: request.revision,
391
453
  });
392
454
  }
393
455
  return {
456
+ roomTypes: HOUSE_ROOM_TYPES,
394
457
  async authorize() {
395
458
  return options.authorize();
396
459
  },
397
- async discover(_authorization) {
398
- const answer = await transport.door(DOOR_DISCOVER, { prefixes: [...DISCOVER_PREFIXES] });
460
+ async discover(_authorization, scope) {
461
+ const answer = await transport.door(DOOR_DISCOVER, {
462
+ prefixes: [...DISCOVER_PREFIXES],
463
+ ...(scope === undefined ? {} : { rooms: scope.authorities }),
464
+ });
399
465
  const failed = discoveryFailure(answer.status, answer.body);
400
466
  if (failed !== null)
401
467
  return failed;
@@ -404,16 +470,31 @@ export function createAgentsHouseAdapter(options) {
404
470
  return { kind: 'transport-uncertainty' };
405
471
  bounds = document.bounds;
406
472
  const entries = trackedFrom(document);
407
- tracked.clear();
473
+ const covered = scope === undefined
474
+ ? null
475
+ : [
476
+ ...new Set([
477
+ ...[...tracked.values()]
478
+ .filter((entry) => scope.authorities.includes(entry.authority))
479
+ .map((entry) => entry.locator),
480
+ ...entries.map((entry) => entry.locator),
481
+ ]),
482
+ ];
483
+ if (covered === null)
484
+ tracked.clear();
485
+ else
486
+ for (const locator of covered)
487
+ tracked.delete(locator);
408
488
  for (const entry of entries)
409
489
  tracked.set(entry.locator, entry);
410
490
  for (const locator of handed.keys())
411
491
  if (!tracked.has(locator))
412
492
  handed.delete(locator);
413
- notifications.track(entries);
493
+ notifications.track([...tracked.values()]);
414
494
  return {
415
- ...mapDiscovery(document, document.authorities.map((authority) => discoveredRoom(authority)), capabilities(document)),
495
+ ...mapDiscovery(document, document.authorities.map((authority) => discoveredRoom(authority)), capabilities(document.actions, [...tracked.values()])),
416
496
  member: document.member,
497
+ ...(covered === null ? {} : { covered }),
417
498
  };
418
499
  },
419
500
  async bootstrap(_authorization, request) {
@@ -422,7 +503,7 @@ export function createAgentsHouseAdapter(options) {
422
503
  return { kind: 'room-not-readable', locator: request.locator };
423
504
  const asked = { entry, held: request.held };
424
505
  if (request.pageToken !== undefined) {
425
- return continuedBootstrap(asked, JSON.parse(request.pageToken));
506
+ return partPage(asked, JSON.parse(request.pageToken));
426
507
  }
427
508
  return bootstrapTogether(asked);
428
509
  },
@@ -444,18 +525,27 @@ export function createAgentsHouseAdapter(options) {
444
525
  const entry = authorityFor(request.locator);
445
526
  if (entry === undefined)
446
527
  return { kind: 'room-not-readable', locator: request.locator };
447
- const mapped = houseChanges(request.changeBatch, entry.handle);
528
+ const batch = request.changeBatch;
529
+ const destination = batch.between?.destination.room;
530
+ const into = destination === undefined ? undefined : authorityFor(destination);
531
+ if (destination !== undefined && into === undefined) {
532
+ return { kind: 'room-not-readable', locator: destination };
533
+ }
534
+ const mapped = into === undefined
535
+ ? houseChanges(batch, entry.handle)
536
+ : houseMove(batch, entry.handle, into.handle);
448
537
  if (mapped.kind === 'unmapped')
449
538
  return rejectedMapping(mapped);
450
- const head = operationHead(request.changeBatch);
539
+ const head = operationHead(batch);
451
540
  const delta = deltaBody(mapped.changes);
452
541
  const bytes = deltaBytes(delta);
453
542
  const handed = request.upload;
454
- const batch = request.changeBatch;
455
- const outcome = await submit(batch, head, mapped.changes, delta, bytes, entry, handed, keep);
543
+ const answer = (upload) => submit(request, head, mapped.changes, delta, bytes, entry, upload, keep, into);
544
+ const settle = (outcome) => into === undefined ? adopted(entry, outcome) : adoptedMove(entry, into, outcome);
545
+ const outcome = await answer(handed);
456
546
  if (handed === undefined || !lapsedGrant(outcome))
457
- return adopted(entry, outcome);
458
- return adopted(entry, await submit(batch, head, mapped.changes, delta, bytes, entry, undefined, keep));
547
+ return settle(outcome);
548
+ return settle(await answer(undefined));
459
549
  },
460
550
  async history(_authorization, request) {
461
551
  const entry = authorityFor(request.locator);
@@ -474,35 +564,29 @@ export function createAgentsHouseAdapter(options) {
474
564
  },
475
565
  async binary(_authorization, request) {
476
566
  if (request.kind === 'path') {
477
- const asked = pathGrant(request);
478
- if (asked === null)
567
+ const ask = pathAddress(request);
568
+ if (ask === null)
479
569
  return { kind: 'room-not-readable', locator: request.locator };
480
- return downloaded(await asked, request.locator);
570
+ return downloaded(ask, request.locator);
481
571
  }
482
572
  const referrer = houseReferrer(request.referrer, tracked);
483
573
  if (referrer === null) {
484
574
  return { kind: 'rejected', code: 'referrer_unknown', fields: {}, retryable: false };
485
575
  }
486
- const answer = await transport.door(DOOR_FILES_GET, {
487
- file: request.fileRef,
488
- referrer: referrer.referrer,
489
- });
490
- return downloaded(answer, referrer.locator);
576
+ return downloaded(() => transport.door(DOOR_FILES_GET, { file: request.fileRef, referrer: referrer.referrer }), referrer.locator);
491
577
  },
492
578
  async link(_authorization, request) {
493
- const asked = pathGrant(request);
494
- if (asked === null)
579
+ const ask = pathAddress(request);
580
+ if (ask === null)
495
581
  return { kind: 'room-not-readable', locator: request.locator };
496
- return granted(await asked, request.locator);
582
+ return linked(await ask(), request.locator);
497
583
  },
498
584
  referrerScope(referrer) {
499
585
  const held = houseReferrer(referrer, tracked);
500
586
  if (held === null)
501
587
  return null;
502
- if (held.locator === '')
503
- return { kind: 'conversations' };
504
588
  const { path } = held.referrer;
505
- return { kind: 'room', locator: held.locator, ...(path === undefined ? {} : { path }) };
589
+ return { locator: held.locator, ...(path === undefined ? {} : { path }) };
506
590
  },
507
591
  reached(locator, version, target) {
508
592
  return reachedVersion(version, target, authorityFor(locator)?.logEpoch);
package/dist/digest.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export declare function textBytes(value: string): number;
2
- export declare function sha256Hex(value: string): Promise<string>;
2
+ export declare function sha256Hex(value: string | ArrayBuffer): Promise<string>;
3
3
  //# sourceMappingURL=digest.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"digest.d.ts","sourceRoot":"","sources":["../src/digest.ts"],"names":[],"mappings":"AAAA,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE/C;AAED,wBAAsB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAG9D"}
1
+ {"version":3,"file":"digest.d.ts","sourceRoot":"","sources":["../src/digest.ts"],"names":[],"mappings":"AAAA,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE/C;AAED,wBAAsB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAM5E"}
package/dist/digest.js CHANGED
@@ -2,6 +2,6 @@ export function textBytes(value) {
2
2
  return new TextEncoder().encode(value).length;
3
3
  }
4
4
  export async function sha256Hex(value) {
5
- const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(value));
5
+ const digest = await crypto.subtle.digest('SHA-256', typeof value === 'string' ? new TextEncoder().encode(value) : value);
6
6
  return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, '0')).join('');
7
7
  }
@@ -16,13 +16,13 @@ export type HouseBounds = {
16
16
  max_held_files: number;
17
17
  change_retention_seconds: number;
18
18
  operation_retention_seconds: number;
19
+ max_move_documents?: number;
19
20
  };
20
21
  export type HouseOperations = {
21
- read: readonly string[];
22
- sync: readonly string[];
23
22
  write: readonly string[];
24
23
  delete: readonly string[];
25
24
  };
25
+ export type HouseReadLevel = 'mcp' | 'online' | 'local';
26
26
  export type HouseModel = {
27
27
  root: string;
28
28
  paths: readonly string[];
@@ -35,6 +35,7 @@ export type HouseAuthority = LogPosition & {
35
35
  houseModel: HouseModel | null;
36
36
  managedReplication: 'admitted' | 'unavailable';
37
37
  protectedPaths: readonly string[];
38
+ read: HouseReadLevel;
38
39
  operations: HouseOperations;
39
40
  };
40
41
  export type HouseDiscoveryDocument = {
@@ -66,7 +67,7 @@ export declare function siteTracked(document: HouseSiteDiscovery): TrackedAuthor
66
67
  export declare function trackedFrom(document: HouseDiscoveryDocument): TrackedAuthority[];
67
68
  export declare function providerLimits(bounds: HouseBounds): readonly ProviderLimit[];
68
69
  export declare function declaredRetention(bounds: HouseBounds): Retention;
69
- export declare function capabilities(document: HouseDiscoveryDocument): readonly Capability[];
70
+ export declare function capabilities(actions: HouseDiscoveryDocument['actions'], rooms: readonly Pick<TrackedAuthority, 'authority' | 'houseModel'>[]): readonly Capability[];
70
71
  export declare function siteCapabilities(): readonly Capability[];
71
72
  export declare function discoveredRoom(authority: HouseAuthority): DiscoveredRoom;
72
73
  export declare function mapDiscovery(document: Pick<HouseDiscoveryDocument, 'bounds'>, rooms: readonly DiscoveredRoom[], declared: readonly Capability[]): Discovery;
@@ -1 +1 @@
1
- {"version":3,"file":"discovery.d.ts","sourceRoot":"","sources":["../src/discovery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,UAAU,EACV,cAAc,EACd,SAAS,EACT,MAAM,EACN,aAAa,EACb,SAAS,EACV,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAA6B,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3E,eAAO,MAAM,gBAAgB,qBAAqB,CAAC;AAEnD,eAAO,MAAM,iBAAiB,EAAE,SAAS,MAAM,EAAU,CAAC;AAE1D,MAAM,MAAM,WAAW,GAAG;IACxB,uBAAuB,EAAE,MAAM,CAAC;IAChC,oBAAoB,EAAE,MAAM,CAAC;IAC7B,mBA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