@agentshouse/mdruntime-adapter 0.1.0-c13e5bc38b145 → 0.1.0-c159093d95310

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,67 +1,49 @@
1
- import { sortedKeys, stepped } from '@agentshouse/mdruntime';
1
+ import { stepped } from '@agentshouse/mdruntime';
2
2
  import { DISCOVER_PREFIXES, capabilities, discoveredRoom, mapDiscovery, parseDiscovery, trackedFrom, } from './discovery.js';
3
3
  import { partModels, streamedModels, viewModels } from './model.js';
4
4
  import { gathered, gatheredEach, selections } from './gather.js';
5
5
  import { createSiteAdapter } from './site.js';
6
- import { authorityRoot, bootstrapping, 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';
7
8
  import { sha256Hex } from './digest.js';
8
- import { acceptedHanded, enumeratedHanded, heldHanded, scopeHanded, streamedHanded, } from './handed.js';
9
+ import { finishedUpload, houseUpload } from './upload.js';
9
10
  import { houseAction } from './actions.js';
10
11
  import { classifyDoor, classifyRead, discoveryFailure, doorRefusal } from './refusal.js';
11
12
  import { houseNotifications } from './stream.js';
12
13
  import { DOOR_BATCH, DOOR_BATCH_UPLOAD, DOOR_BOOTSTRAP, DOOR_DISCOVER, DOOR_ENUMERATE, DOOR_FILES_GET, DOOR_REVISIONS, } from './transport.js';
13
14
  import { batchPaths, deltaBody, deltaBytes, deltaDigest, historyOutcome, houseChanges, houseMove, mutationOutcome, operationHead, rejectedMapping, revisionBody, uploadable, } from './write.js';
14
- import { houseReferrer } from './referrer.js';
15
+ import { houseReferrer, houseReferrerScope } from './referrer.js';
16
+ import { HOUSE_ROOM_TYPES } from './room-types.js';
15
17
  import { followsVersion, logPosition, presented, reachedVersion, roomVersion, } from './version.js';
16
18
  const UNCERTAIN = { kind: 'transport-uncertainty' };
17
19
  const UNCERTAIN_READ = { kind: 'transport-uncertainty' };
20
+ const EXPIRED_ADDRESS = 403;
18
21
  function lapsedGrant(outcome) {
19
22
  return outcome.kind === 'rejected' && outcome.code === 'upload_unavailable';
20
23
  }
21
- const heldPaths = new WeakMap();
22
- async function sortedHeld(held) {
23
- const known = heldPaths.get(held.documents);
24
- if (known !== undefined)
25
- return known;
26
- const sorted = await stepped(sortedKeys(held.documents));
27
- heldPaths.set(held.documents, sorted);
28
- return sorted;
29
- }
30
- function heldFiles(authority, held, paths, after, bound) {
31
- let low = 0;
32
- for (let high = paths.length; low < high;) {
33
- const middle = (low + high) >>> 1;
34
- if (after !== null && paths[middle] <= after)
35
- low = middle + 1;
36
- else
37
- high = middle;
38
- }
39
- const files = [];
40
- let through = null;
41
- for (let at = low; at < paths.length && through === null; at += 1) {
42
- const revision = held.revisions[paths[at]];
43
- if (revision === undefined)
44
- continue;
45
- if (files.length === bound)
46
- through = files.at(-1).path;
47
- else
48
- files.push({ path: paths[at], revision });
49
- }
24
+ async function heldFiles(authority, held, bound) {
25
+ const listed = await held.files(null, bound + 1);
26
+ const files = listed.slice(0, bound);
27
+ const through = listed.length > bound ? (files.at(-1)?.path ?? null) : null;
50
28
  return files.length === 0 ? null : { room: authority, through, files };
51
29
  }
52
30
  async function reused(scope, held) {
53
31
  const sent = new Set(scope.contents.map((entry) => entry.path));
54
32
  const contents = [...scope.contents];
55
- for (const file of scope.files) {
56
- if (file.binary !== undefined || sent.has(file.path))
57
- continue;
58
- const content = held?.documents[file.path];
59
- if (content === undefined ||
60
- held?.revisions[file.path] !== file.revision ||
61
- (await sha256Hex(content)) !== file.sha256) {
33
+ const asked = scope.files.filter((file) => file.binary === undefined && !sent.has(file.path));
34
+ if (asked.length === 0)
35
+ return scope;
36
+ if (held === undefined)
37
+ return null;
38
+ const kept = await held.contents(asked.map(({ path }) => path));
39
+ for (const file of asked) {
40
+ const found = kept.get(file.path);
41
+ if (found === undefined ||
42
+ found.revision !== file.revision ||
43
+ (await sha256Hex(found.content)) !== file.sha256) {
62
44
  return null;
63
45
  }
64
- contents.push({ path: file.path, content });
46
+ contents.push({ path: file.path, content: found.content });
65
47
  }
66
48
  return { ...scope, contents };
67
49
  }
@@ -72,57 +54,59 @@ export function createAgentsHouseAdapter(options) {
72
54
  const notifications = houseNotifications(transport, 'credential');
73
55
  const tracked = new Map();
74
56
  const handed = new Map();
57
+ let heldRevisions;
75
58
  let bounds;
76
59
  function declared() {
77
60
  if (bounds === undefined)
78
61
  throw new Error('no Discovery declared the bounds');
79
62
  return bounds;
80
63
  }
81
- function hand(locator, steps) {
82
- const held = handed.get(locator);
83
- const next = stepped(steps);
84
- if (!(next instanceof Promise)) {
85
- handed.set(locator, next);
86
- return;
64
+ async function fetchedPart(entry, continued, listed) {
65
+ for (let attempt = 0; attempt < PART_ATTEMPTS; attempt += 1) {
66
+ const answered = await transport.door(DOOR_BOOTSTRAP, {
67
+ prefixes: [authorityRoot(entry.handle)],
68
+ part: listed.part,
69
+ ...(continued.held === undefined ? {} : { held: [continued.held] }),
70
+ });
71
+ const failure = classifyRead(answered.status, answered.body, entry.locator);
72
+ if (failure !== null)
73
+ return failure;
74
+ const scope = await housePart(continued.room, answered.body, listed, 'credential');
75
+ if (scope !== null)
76
+ return scope;
87
77
  }
88
- return next.then((room) => {
89
- if (handed.get(locator) === held)
90
- handed.set(locator, room);
91
- else
92
- handed.delete(locator);
93
- });
78
+ return unverifiedPart(entry.locator);
94
79
  }
95
- async function bootstrappedRoom(envelope, sent, asked, continued) {
80
+ async function partPage(asked, continued) {
96
81
  const { entry } = asked;
97
- if (sent === undefined)
98
- return { kind: 'room-not-readable', locator: entry.locator };
82
+ const listed = continued.room.parts[continued.next];
83
+ const sent = listed === undefined
84
+ ? emptyScope(continued.room)
85
+ : await fetchedPart(entry, continued, listed);
86
+ if ('kind' in sent)
87
+ return sent;
99
88
  const scope = await reused(sent, asked.held);
100
89
  if (scope === null) {
101
90
  return asked.held === undefined
102
91
  ? UNCERTAIN_READ
103
- : continued === null
104
- ? bootstrapTogether({ entry, held: undefined })
105
- : continuedBootstrap({ entry, held: undefined }, continued);
92
+ : bootstrapTogether({ entry, held: undefined });
106
93
  }
107
- const cursor = envelope.next?.room === entry.authority ? envelope.next : undefined;
108
- const next = cursor?.path ?? null;
109
- if (continued === null && next === null)
110
- await hand(entry.locator, scopeHanded(scope));
94
+ const next = continued.next + 1;
95
+ const more = next < continued.room.parts.length;
96
+ if (continued.next === 0 && !more)
97
+ handed.set(entry.locator, roomVersion(scope));
111
98
  else
112
99
  handed.delete(entry.locator);
113
100
  notifications.reach(entry.authority, scope);
114
101
  const seen = [
115
- ...(continued?.seen ?? []),
102
+ ...continued.seen,
116
103
  ...entry.schemas.filter((schema) => scope.files.some((file) => file.path === schema)),
117
104
  ];
118
- const models = await partModels(scope, entry, new Set(seen), next === null);
119
- const continuation = next === null
120
- ? null
121
- : { after: next, seen, ...(cursor?.cut === undefined ? {} : { cut: cursor.cut }) };
105
+ const models = await partModels(scope, entry, new Set(seen), !more);
122
106
  return {
123
107
  ...(await stepped(bootstrapping(scope))),
124
108
  ...(models === undefined ? {} : { models }),
125
- ...(continuation === null ? {} : { pageToken: JSON.stringify(continuation) }),
109
+ ...(more ? { pageToken: JSON.stringify({ ...continued, next, seen }) } : {}),
126
110
  };
127
111
  }
128
112
  async function enumeratedPage(ask, page) {
@@ -154,18 +138,21 @@ export function createAgentsHouseAdapter(options) {
154
138
  const answer = await refreshFrom(view, ask.fromRoomVersion);
155
139
  if (answer.kind !== 'net-advancement')
156
140
  return answer;
157
- await hand(ask.entry.locator, enumeratedHanded(view, handed.get(ask.entry.locator)));
141
+ handed.set(ask.entry.locator, roomVersion(view));
158
142
  const models = await viewModels(view, ask.entry);
159
143
  return models === undefined ? answer : { ...answer, models };
160
144
  }
161
145
  async function streamed(entry, request, from) {
162
- const held = handed.get(entry.locator);
163
- if (held?.roomVersion !== request.fromRoomVersion)
146
+ if (handed.get(entry.locator) !== request.fromRoomVersion)
164
147
  return null;
165
- const advanced = await stepped(notifications.advance(entry, from, (path, revision) => held.revisions[path] === revision));
148
+ const asked = notifications.edited(entry, from);
149
+ const kept = asked.length === 0 || heldRevisions === undefined
150
+ ? new Map()
151
+ : await heldRevisions(entry.locator, asked);
152
+ const advanced = await stepped(notifications.advance(entry, from, (path, revision) => kept.get(path) === revision));
166
153
  if (advanced === null)
167
154
  return null;
168
- await hand(entry.locator, streamedHanded(held, advanced));
155
+ handed.set(entry.locator, advanced.page.roomVersion);
169
156
  const models = await streamedModels(advanced.schemas, entry, 'credential');
170
157
  return models === undefined ? advanced.page : { ...advanced.page, models };
171
158
  }
@@ -175,7 +162,7 @@ export function createAgentsHouseAdapter(options) {
175
162
  const at = logPosition(room.roomVersion);
176
163
  if (entry === undefined || at === null)
177
164
  return [];
178
- handed.set(room.locator, heldHanded(room));
165
+ handed.set(room.locator, room.roomVersion);
179
166
  notifications.reach(entry.authority, at);
180
167
  return [presented(entry.authority, at)];
181
168
  });
@@ -193,8 +180,9 @@ export function createAgentsHouseAdapter(options) {
193
180
  const held = handed.get(entry.locator);
194
181
  if (held === undefined)
195
182
  return outcome;
196
- const follows = followsVersion(held.roomVersion, outcome.roomVersion);
197
- await hand(entry.locator, acceptedHanded(held, outcome, follows ? outcome.roomVersion : held.roomVersion));
183
+ const follows = followsVersion(held, outcome.roomVersion);
184
+ if (follows)
185
+ handed.set(entry.locator, outcome.roomVersion);
198
186
  const at = logPosition(outcome.roomVersion);
199
187
  if (follows && at !== null)
200
188
  notifications.reach(entry.authority, at);
@@ -217,14 +205,13 @@ export function createAgentsHouseAdapter(options) {
217
205
  return result;
218
206
  return { ...result, at: { locator, roomVersion: roomVersion({ position, logEpoch }) } };
219
207
  }
220
- async function holding(asks, after) {
208
+ async function holding(asks) {
221
209
  const bound = Math.floor(declared().max_held_files / Math.max(1, asks.filter((ask) => ask.held !== undefined).length));
222
210
  const held = [];
223
211
  for (const ask of asks) {
224
212
  if (ask.held === undefined || bound === 0)
225
213
  continue;
226
- const paths = await sortedHeld(ask.held);
227
- const files = heldFiles(ask.entry.authority, ask.held, paths, after, bound);
214
+ const files = await heldFiles(ask.entry.authority, ask.held, bound);
228
215
  if (files !== null)
229
216
  held.push(files);
230
217
  }
@@ -235,56 +222,44 @@ export function createAgentsHouseAdapter(options) {
235
222
  while (open.length > 0) {
236
223
  const unreached = [];
237
224
  for (const part of selections(open, declared().max_prefixes)) {
238
- const held = await holding(part.map(({ asked }) => asked), null);
225
+ const held = await holding(part.map(({ asked }) => asked));
239
226
  const answered = await transport.door(DOOR_BOOTSTRAP, {
240
227
  prefixes: part.map(({ entry }) => authorityRoot(entry.handle)),
241
228
  ...(held.length === 0 ? {} : { held }),
242
229
  });
243
230
  const classified = part.map(({ entry }) => classifyDoor(answered.status, answered.body, entry.locator));
244
- const envelope = classified[0] === null ? await stepped(parseEnvelope(answered.body)) : null;
231
+ const parted = classified[0] === null ? parseParted(answered.body) : null;
245
232
  for (const [index, asked] of part.entries()) {
246
233
  const failure = classified[index];
247
234
  if (failure !== null && failure !== undefined) {
248
235
  answer(asked.ask, failure);
249
236
  continue;
250
237
  }
251
- if (envelope === null) {
238
+ if (parted === null) {
252
239
  answer(asked.ask, UNCERTAIN_READ);
253
240
  continue;
254
241
  }
255
- const scope = envelope.scopes.find((candidate) => matchesScope(candidate, asked.entry));
256
- if (scope === undefined && envelope.next !== undefined) {
242
+ const room = parted.rooms.find((candidate) => candidate.anchor === asked.entry.authority);
243
+ if (room === undefined && parted.next !== undefined) {
257
244
  unreached.push(asked);
258
245
  continue;
259
246
  }
260
- answer(asked.ask, await bootstrappedRoom(envelope, scope, asked.asked, null));
247
+ if (room === undefined) {
248
+ answer(asked.ask, { kind: 'room-not-readable', locator: asked.entry.locator });
249
+ continue;
250
+ }
251
+ const kept = held.find((files) => files.room === asked.entry.authority);
252
+ answer(asked.ask, await partPage(asked.asked, {
253
+ room,
254
+ next: 0,
255
+ seen: [],
256
+ ...(kept === undefined ? {} : { held: kept }),
257
+ }));
261
258
  }
262
259
  }
263
260
  open = unreached;
264
261
  }
265
262
  }
266
- async function continuedBootstrap(asked, continued) {
267
- const { entry } = asked;
268
- const after = {
269
- room: entry.authority,
270
- path: continued.after,
271
- ...(continued.cut === undefined ? {} : { cut: continued.cut }),
272
- };
273
- const held = await holding([asked], continued.after);
274
- const answered = await transport.door(DOOR_BOOTSTRAP, {
275
- prefixes: [authorityRoot(entry.handle)],
276
- after,
277
- ...(held.length === 0 ? {} : { held }),
278
- });
279
- const failure = classifyRead(answered.status, answered.body, entry.locator);
280
- if (failure !== null)
281
- return failure;
282
- const envelope = await stepped(parseEnvelope(answered.body));
283
- if (envelope === null)
284
- return UNCERTAIN_READ;
285
- const scope = envelope.scopes.find((candidate) => matchesScope(candidate, entry));
286
- return bootstrappedRoom(envelope, scope, asked, continued);
287
- }
288
263
  function layered(asks) {
289
264
  const depths = new Map();
290
265
  const layers = [];
@@ -346,7 +321,7 @@ export function createAgentsHouseAdapter(options) {
346
321
  };
347
322
  }
348
323
  const grant = parseGrant(minted.body);
349
- if (grant === null || grant.method !== 'POST')
324
+ if (grant === null)
350
325
  return { outcome: UNCERTAIN };
351
326
  const delivered = await transport.transfer({
352
327
  act: 'authored_batch_upload',
@@ -356,36 +331,31 @@ export function createAgentsHouseAdapter(options) {
356
331
  if (delivered.status !== 200)
357
332
  return { outcome: UNCERTAIN };
358
333
  const holding = parseGrant(delivered.body);
359
- if (holding === null || holding.method !== 'POST')
334
+ if (holding === null)
360
335
  return { outcome: UNCERTAIN };
361
336
  const upload = { url: holding.url, operation: holding.operation, bytes, sha256 };
362
337
  await keep(upload);
363
338
  return { body: { ...head, paths, upload, runtime: notifications.runtime } };
364
339
  }
365
- async function staged(answer, batch) {
340
+ async function staged(answer, { changeBatch: batch, file }) {
366
341
  const refusal = doorRefusal(answer.body);
367
342
  if (refusal?.code !== 'files_unstaged')
368
343
  return null;
369
- const grants = refusal.fields.upload;
370
- if (!Array.isArray(grants))
344
+ const uploads = refusal.fields.upload;
345
+ if (!Array.isArray(uploads))
371
346
  return false;
372
- for (const grant of grants) {
373
- const { url, operation, sha256 } = grant;
374
- const change = batch.changes.find((held) => held.kind === 'binary' && held.sha256 === sha256);
375
- if (typeof url !== 'string' || typeof operation !== 'string' || change === undefined)
347
+ for (const named of uploads) {
348
+ const upload = houseUpload(named);
349
+ const change = batch.changes.find((held) => held.kind === 'binary' && held.sha256 === named.sha256);
350
+ if (upload === null || change === undefined)
376
351
  return false;
377
- const delivered = await transport.transfer({
378
- act: 'binary_stage',
379
- url,
380
- body: change.blob,
381
- operation,
382
- });
383
- if (delivered.status !== 200)
352
+ const finished = await finishedUpload(transport, upload, await file(change.localId));
353
+ if (finished?.body?.staged !== true)
384
354
  return false;
385
355
  }
386
356
  return true;
387
357
  }
388
- async function submit(batch, head, changes, delta, bytes, entry, handed, keep, into) {
358
+ async function submit(request, head, changes, delta, bytes, entry, handed, keep, into) {
389
359
  const inline = { ...head, changes, runtime: notifications.runtime };
390
360
  const prepared = uploadable(inline, bytes, declared())
391
361
  ? await uploaded(head, changes, delta, bytes, entry, handed, keep)
@@ -393,11 +363,11 @@ export function createAgentsHouseAdapter(options) {
393
363
  if ('outcome' in prepared)
394
364
  return prepared.outcome;
395
365
  const answer = await transport.door(DOOR_BATCH, prepared.body);
396
- const stagedFiles = await staged(answer, batch);
366
+ const stagedFiles = await staged(answer, request);
397
367
  if (stagedFiles === false)
398
368
  return UNCERTAIN;
399
369
  const settled = stagedFiles === null ? answer : await transport.door(DOOR_BATCH, prepared.body);
400
- if (stagedFiles === true && (await staged(settled, batch)) !== null)
370
+ if (stagedFiles === true && (await staged(settled, request)) !== null)
401
371
  return UNCERTAIN;
402
372
  return stepped(mutationOutcome(settled.status, settled.body, entry.locator, entry.handle, into === undefined
403
373
  ? undefined
@@ -406,7 +376,7 @@ export function createAgentsHouseAdapter(options) {
406
376
  destination: { authority: into.authority, handle: into.handle },
407
377
  }));
408
378
  }
409
- function granted(answer, locator) {
379
+ function addressed(answer, locator) {
410
380
  const failure = classifyDoor(answer.status, answer.body, locator);
411
381
  if (failure !== null) {
412
382
  return failure.kind === 'scoped-refusal'
@@ -418,33 +388,47 @@ export function createAgentsHouseAdapter(options) {
418
388
  }
419
389
  : failure;
420
390
  }
421
- const grant = parseGrant(answer.body);
422
- const name = answer.body.name;
423
- if (grant === null || grant.method !== 'GET' || typeof name !== 'string') {
424
- return UNCERTAIN_READ;
425
- }
426
- return { kind: 'link', url: grant.url, name };
391
+ const address = parseFileAddress(answer.body);
392
+ return address === null ? UNCERTAIN_READ : { kind: 'address', ...address };
427
393
  }
428
- async function downloaded(answer, locator) {
429
- const link = granted(answer, locator);
430
- if (link.kind !== 'link')
431
- return link;
432
- const read = await transport.download(link.url);
433
- if (read.status !== 200 || read.blob === null)
394
+ function linked(answer, locator) {
395
+ const address = addressed(answer, locator);
396
+ return address.kind === 'address'
397
+ ? { kind: 'link', url: address.url, name: address.name }
398
+ : address;
399
+ }
400
+ async function downloaded(ask, locator) {
401
+ let address = addressed(await ask(), locator);
402
+ if (address.kind !== 'address')
403
+ return address;
404
+ let read = await transport.download(address.url);
405
+ if (read.status === EXPIRED_ADDRESS) {
406
+ address = addressed(await ask(), locator);
407
+ if (address.kind !== 'address')
408
+ return address;
409
+ read = await transport.download(address.url);
410
+ }
411
+ if (read.status !== 200 || read.body === null)
434
412
  return UNCERTAIN_READ;
435
- return { kind: 'binary', blob: read.blob };
413
+ return {
414
+ kind: 'binary',
415
+ body: read.body,
416
+ mediaType: address.mediaType,
417
+ sha256: address.sha256,
418
+ };
436
419
  }
437
- function pathGrant(request) {
420
+ function pathAddress(request) {
438
421
  const entry = authorityFor(request.locator);
439
422
  if (entry === undefined)
440
423
  return null;
441
- return transport.door(DOOR_FILES_GET, {
424
+ return () => transport.door(DOOR_FILES_GET, {
442
425
  room_ref: entry.authority,
443
426
  path: request.path,
444
427
  revision: request.revision,
445
428
  });
446
429
  }
447
430
  return {
431
+ roomTypes: HOUSE_ROOM_TYPES,
448
432
  async authorize() {
449
433
  return options.authorize();
450
434
  },
@@ -494,7 +478,7 @@ export function createAgentsHouseAdapter(options) {
494
478
  return { kind: 'room-not-readable', locator: request.locator };
495
479
  const asked = { entry, held: request.held };
496
480
  if (request.pageToken !== undefined) {
497
- return continuedBootstrap(asked, JSON.parse(request.pageToken));
481
+ return partPage(asked, JSON.parse(request.pageToken));
498
482
  }
499
483
  return bootstrapTogether(asked);
500
484
  },
@@ -531,7 +515,7 @@ export function createAgentsHouseAdapter(options) {
531
515
  const delta = deltaBody(mapped.changes);
532
516
  const bytes = deltaBytes(delta);
533
517
  const handed = request.upload;
534
- const answer = (upload) => submit(batch, head, mapped.changes, delta, bytes, entry, upload, keep, into);
518
+ const answer = (upload) => submit(request, head, mapped.changes, delta, bytes, entry, upload, keep, into);
535
519
  const settle = (outcome) => into === undefined ? adopted(entry, outcome) : adoptedMove(entry, into, outcome);
536
520
  const outcome = await answer(handed);
537
521
  if (handed === undefined || !lapsedGrant(outcome))
@@ -555,35 +539,25 @@ export function createAgentsHouseAdapter(options) {
555
539
  },
556
540
  async binary(_authorization, request) {
557
541
  if (request.kind === 'path') {
558
- const asked = pathGrant(request);
559
- if (asked === null)
542
+ const ask = pathAddress(request);
543
+ if (ask === null)
560
544
  return { kind: 'room-not-readable', locator: request.locator };
561
- return downloaded(await asked, request.locator);
545
+ return downloaded(ask, request.locator);
562
546
  }
563
547
  const referrer = houseReferrer(request.referrer, tracked);
564
548
  if (referrer === null) {
565
549
  return { kind: 'rejected', code: 'referrer_unknown', fields: {}, retryable: false };
566
550
  }
567
- const answer = await transport.door(DOOR_FILES_GET, {
568
- file: request.fileRef,
569
- referrer: referrer.referrer,
570
- });
571
- return downloaded(answer, referrer.locator);
551
+ return downloaded(() => transport.door(DOOR_FILES_GET, { file: request.fileRef, referrer: referrer.referrer }), referrer.locator);
572
552
  },
573
553
  async link(_authorization, request) {
574
- const asked = pathGrant(request);
575
- if (asked === null)
554
+ const ask = pathAddress(request);
555
+ if (ask === null)
576
556
  return { kind: 'room-not-readable', locator: request.locator };
577
- return granted(await asked, request.locator);
557
+ return linked(await ask(), request.locator);
578
558
  },
579
559
  referrerScope(referrer) {
580
- const held = houseReferrer(referrer, tracked);
581
- if (held === null)
582
- return null;
583
- if (held.locator === '')
584
- return { kind: 'conversations' };
585
- const { path } = held.referrer;
586
- return { kind: 'room', locator: held.locator, ...(path === undefined ? {} : { path }) };
560
+ return houseReferrerScope(referrer, tracked);
587
561
  },
588
562
  reached(locator, version, target) {
589
563
  return reachedVersion(version, target, authorityFor(locator)?.logEpoch);
@@ -597,7 +571,10 @@ export function createAgentsHouseAdapter(options) {
597
571
  roomStream: {
598
572
  hold: held,
599
573
  reopen: notifications.reopen,
600
- observe: notifications.observe,
574
+ observe(notify, receipts, revisions) {
575
+ heldRevisions = revisions;
576
+ return notifications.observe(notify, receipts);
577
+ },
601
578
  published(locator, roomVersion) {
602
579
  const at = logPosition(roomVersion);
603
580
  if (at !== null)
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export { createAgentsHouseAdapter, type AgentsHouseAdapterOptions } from './adapter.js';
2
+ export { finishedUpload, houseUpload, type HouseUpload } from './upload.js';
2
3
  export { HOUSE_APP_ACTS, HOUSE_APP_BATCHES, HOUSE_SITE_ACTS } from './actions.js';
4
+ export { HOUSE_ROOM_TYPES, INBOX_CHANNEL_KINDS, INBOX_CHAT_TYPE, INBOX_MESSAGE_TYPE, type InboxChannelKind, } from './room-types.js';
3
5
  export type { HouseByteAct, HouseByteTransfer, HouseBytesAnswer, HouseClosing, HouseDoorAnswer, HouseHeldRoom, HouseOpening, HouseStream, HouseStreamEntry, HouseStreamFrame, HouseSiteTransport, HouseTransport, } from './transport.js';
4
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,KAAK,yBAAyB,EAAE,MAAM,cAAc,CAAC;AACxF,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAClF,YAAY,EACV,YAAY,EACZ,iBAAiB,EACjB,gBAAgB,EAChB,YAAY,EACZ,eAAe,EACf,aAAa,EACb,YAAY,EACZ,WAAW,EACX,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,cAAc,GACf,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,KAAK,yBAAyB,EAAE,MAAM,cAAc,CAAC;AACxF,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAC5E,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAClF,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACnB,eAAe,EACf,kBAAkB,EAClB,KAAK,gBAAgB,GACtB,MAAM,iBAAiB,CAAC;AACzB,YAAY,EACV,YAAY,EACZ,iBAAiB,EACjB,gBAAgB,EAChB,YAAY,EACZ,eAAe,EACf,aAAa,EACb,YAAY,EACZ,WAAW,EACX,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,cAAc,GACf,MAAM,gBAAgB,CAAC"}
package/dist/index.js CHANGED
@@ -1,2 +1,4 @@
1
1
  export { createAgentsHouseAdapter } from './adapter.js';
2
+ export { finishedUpload, houseUpload } from './upload.js';
2
3
  export { HOUSE_APP_ACTS, HOUSE_APP_BATCHES, HOUSE_SITE_ACTS } from './actions.js';
4
+ export { HOUSE_ROOM_TYPES, INBOX_CHANNEL_KINDS, INBOX_CHAT_TYPE, INBOX_MESSAGE_TYPE, } from './room-types.js';
package/dist/part.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import type { ScopedRefusal } from '@agentshouse/mdruntime';
2
+ export declare const PART_ATTEMPTS = 3;
3
+ export declare const PART_UNVERIFIED = "part_unverified";
4
+ export type HousePart = {
5
+ part: string;
6
+ bytes: number;
7
+ sha256: string;
8
+ };
9
+ export declare function parsePart(value: unknown): HousePart | null;
10
+ export declare function checkedPayload(body: unknown, listed: HousePart): Promise<Uint8Array<ArrayBuffer> | null>;
11
+ export declare function gunzipped(payload: Uint8Array<ArrayBuffer>): Promise<string | null>;
12
+ export declare function unverifiedPart(locator: string): ScopedRefusal;
13
+ //# sourceMappingURL=part.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"part.d.ts","sourceRoot":"","sources":["../src/part.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAU5D,eAAO,MAAM,aAAa,IAAI,CAAC;AAE/B,eAAO,MAAM,eAAe,oBAAoB,CAAC;AAEjD,MAAM,MAAM,SAAS,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAExE,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,SAAS,GAAG,IAAI,CAO1D;AAoBD,wBAAsB,cAAc,CAClC,IAAI,EAAE,OAAO,EACb,MAAM,EAAE,SAAS,GAChB,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC,GAAG,IAAI,CAAC,CAiBzC;AAED,wBAAsB,SAAS,CAAC,OAAO,EAAE,UAAU,CAAC,WAAW,CAAC,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAOxF;AAED,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,aAAa,CAS7D"}
package/dist/part.js ADDED
@@ -0,0 +1,80 @@
1
+ const PART_MAGIC = 0x48505431;
2
+ const PART_HEAD_BYTES = 8;
3
+ const PART_DIGEST_BYTES = 32;
4
+ const PART_SHA256 = /^[0-9a-f]{64}$/u;
5
+ export const PART_ATTEMPTS = 3;
6
+ export const PART_UNVERIFIED = 'part_unverified';
7
+ export function parsePart(value) {
8
+ if (typeof value !== 'object' || value === null)
9
+ return null;
10
+ const { part, bytes, sha256 } = value;
11
+ if (typeof part !== 'string' || part.length === 0)
12
+ return null;
13
+ if (typeof bytes !== 'number' || !Number.isSafeInteger(bytes) || bytes <= 0)
14
+ return null;
15
+ if (typeof sha256 !== 'string' || !PART_SHA256.test(sha256))
16
+ return null;
17
+ return { part, bytes, sha256 };
18
+ }
19
+ function decoded(text) {
20
+ let binary;
21
+ try {
22
+ binary = atob(text);
23
+ }
24
+ catch {
25
+ return null;
26
+ }
27
+ const bytes = new Uint8Array(binary.length);
28
+ for (let at = 0; at < binary.length; at += 1)
29
+ bytes[at] = binary.charCodeAt(at);
30
+ return bytes;
31
+ }
32
+ function hex(bytes) {
33
+ let text = '';
34
+ for (const byte of bytes)
35
+ text += byte.toString(16).padStart(2, '0');
36
+ return text;
37
+ }
38
+ export async function checkedPayload(body, listed) {
39
+ if (typeof body !== 'object' || body === null)
40
+ return null;
41
+ const { part } = body;
42
+ if (typeof part !== 'string')
43
+ return null;
44
+ const frame = decoded(part);
45
+ if (frame === null || frame.length !== listed.bytes)
46
+ return null;
47
+ if (frame.length < PART_HEAD_BYTES + PART_DIGEST_BYTES)
48
+ return null;
49
+ const view = new DataView(frame.buffer, frame.byteOffset, frame.byteLength);
50
+ if (view.getUint32(0) !== PART_MAGIC)
51
+ return null;
52
+ const length = view.getUint32(4);
53
+ if (PART_HEAD_BYTES + length + PART_DIGEST_BYTES !== frame.length)
54
+ return null;
55
+ const payload = frame.subarray(PART_HEAD_BYTES, PART_HEAD_BYTES + length);
56
+ const digest = hex(new Uint8Array(await crypto.subtle.digest('SHA-256', payload)));
57
+ if (digest !== listed.sha256 || hex(frame.subarray(PART_HEAD_BYTES + length)) !== digest) {
58
+ return null;
59
+ }
60
+ return payload;
61
+ }
62
+ export async function gunzipped(payload) {
63
+ try {
64
+ const stream = new Blob([payload]).stream().pipeThrough(new DecompressionStream('gzip'));
65
+ return await new Response(stream).text();
66
+ }
67
+ catch {
68
+ return null;
69
+ }
70
+ }
71
+ export function unverifiedPart(locator) {
72
+ return {
73
+ kind: 'scoped-refusal',
74
+ locator,
75
+ scope: 'undisclosed',
76
+ code: PART_UNVERIFIED,
77
+ fields: {},
78
+ retryable: true,
79
+ };
80
+ }