@agentshouse/mdruntime-adapter 0.1.0-cedb8e57837a7 → 0.1.0-cefdbbefd37e5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/README.md +3 -3
  2. package/dist/actions.d.ts +6 -70
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +22 -360
  5. package/dist/adapter.d.ts +0 -1
  6. package/dist/adapter.d.ts.map +1 -1
  7. package/dist/adapter.js +212 -429
  8. package/dist/app-reference.d.ts +1 -0
  9. package/dist/app-reference.d.ts.map +1 -0
  10. package/dist/app-reference.js +2 -0
  11. package/dist/authored-path.d.ts +10 -0
  12. package/dist/authored-path.d.ts.map +1 -0
  13. package/dist/authored-path.js +35 -0
  14. package/dist/chunks.d.ts +37 -0
  15. package/dist/chunks.d.ts.map +1 -0
  16. package/dist/chunks.js +247 -0
  17. package/dist/crockford.d.ts +3 -0
  18. package/dist/crockford.d.ts.map +1 -0
  19. package/dist/crockford.js +3 -0
  20. package/dist/digest.d.ts +0 -1
  21. package/dist/discovery.d.ts +23 -18
  22. package/dist/discovery.d.ts.map +1 -1
  23. package/dist/discovery.js +78 -59
  24. package/dist/history.d.ts +6 -0
  25. package/dist/history.d.ts.map +1 -0
  26. package/dist/history.js +84 -0
  27. package/dist/house-model.d.ts +830 -0
  28. package/dist/house-model.d.ts.map +1 -0
  29. package/dist/house-model.js +465 -0
  30. package/dist/index.d.ts +12 -3
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +11 -1
  33. package/dist/map.d.ts +0 -1
  34. package/dist/model.d.ts +5 -5
  35. package/dist/model.d.ts.map +1 -1
  36. package/dist/model.js +34 -41
  37. package/dist/read.d.ts +38 -61
  38. package/dist/read.d.ts.map +1 -1
  39. package/dist/read.js +104 -300
  40. package/dist/refusal.d.ts +1 -4
  41. package/dist/refusal.d.ts.map +1 -1
  42. package/dist/refusal.js +5 -6
  43. package/dist/room-types.d.ts +4 -0
  44. package/dist/room-types.d.ts.map +1 -0
  45. package/dist/room-types.js +135 -0
  46. package/dist/search.d.ts +4 -0
  47. package/dist/search.d.ts.map +1 -0
  48. package/dist/search.js +47 -0
  49. package/dist/session-content/bounds.d.ts +7 -0
  50. package/dist/session-content/check.d.ts +18 -0
  51. package/dist/session-content/index.d.ts +7 -0
  52. package/dist/session-content/parts.d.ts +102 -0
  53. package/dist/session-content/state.d.ts +61 -0
  54. package/dist/session-content/turn.d.ts +328 -0
  55. package/dist/site.d.ts +0 -1
  56. package/dist/site.d.ts.map +1 -1
  57. package/dist/site.js +85 -201
  58. package/dist/space-schema.d.ts +6 -0
  59. package/dist/space-schema.d.ts.map +1 -0
  60. package/dist/space-schema.js +18 -0
  61. package/dist/stream.d.ts +16 -15
  62. package/dist/stream.d.ts.map +1 -1
  63. package/dist/stream.js +240 -411
  64. package/dist/transport.d.ts +57 -44
  65. package/dist/transport.d.ts.map +1 -1
  66. package/dist/transport.js +54 -11
  67. package/dist/upload.d.ts +11 -0
  68. package/dist/upload.d.ts.map +1 -0
  69. package/dist/upload.js +63 -0
  70. package/dist/version.d.ts +0 -1
  71. package/dist/write.d.ts +6 -3
  72. package/dist/write.d.ts.map +1 -1
  73. package/dist/write.js +31 -44
  74. package/package.json +8 -4
  75. package/AGENTS.md +0 -304
  76. package/dist/gather.d.ts +0 -4
  77. package/dist/gather.d.ts.map +0 -1
  78. package/dist/gather.js +0 -35
  79. package/dist/handed.d.ts +0 -15
  80. package/dist/handed.d.ts.map +0 -1
  81. package/dist/handed.js +0 -45
  82. package/dist/referrer.d.ts +0 -7
  83. package/dist/referrer.d.ts.map +0 -1
  84. package/dist/referrer.js +0 -35
package/dist/adapter.js CHANGED
@@ -1,204 +1,124 @@
1
- import { sortedKeys, stepped } from '@agentshouse/mdruntime';
2
- import { DISCOVER_PREFIXES, capabilities, discoveredRoom, mapDiscovery, parseDiscovery, trackedFrom, } from './discovery.js';
3
- import { partModels, streamedModels, viewModels } from './model.js';
4
- import { gathered, gatheredEach, selections } from './gather.js';
1
+ import { stepped } from '@agentshouse/mdruntime';
2
+ import { capabilities, discoveredRoom, mapDiscovery, parseAuthorityChange, parseDiscovery, trackedFrom, } from './discovery.js';
5
3
  import { createSiteAdapter } from './site.js';
6
- import { authorityRoot, bootstrapping, matchesScope, parseEnumeration, parseEnvelope, parseGrant, refreshFrom, viewOf, wholeView, } from './read.js';
7
- import { sha256Hex } from './digest.js';
8
- import { acceptedHanded, enumeratedHanded, heldHanded, scopeHanded, streamedHanded, } from './handed.js';
9
- import { houseAction } from './actions.js';
10
- import { classifyDoor, classifyRead, discoveryFailure, doorRefusal } from './refusal.js';
11
- import { houseNotifications } from './stream.js';
12
- import { DOOR_BATCH, DOOR_BATCH_UPLOAD, DOOR_BOOTSTRAP, DOOR_DISCOVER, DOOR_ENUMERATE, DOOR_FILES_GET, DOOR_REVISIONS, } from './transport.js';
4
+ import { parseFileAddress, parseGrant } from './read.js';
5
+ import { chunkedHead, chunkLister, chunkPart } from './chunks.js';
6
+ import { spaceSchemas } from './space-schema.js';
7
+ import { finishedUpload, houseUpload } from './upload.js';
8
+ import { houseAction, HOUSE_LEADING_READS, HOUSE_UNKEPT_READS } from './actions.js';
9
+ import { houseSearch } from './search.js';
10
+ import { classifyDoor, doorRefusal } from './refusal.js';
11
+ import { houseNotifications, } from './stream.js';
12
+ import { houseDoorRoute, } from './transport.js';
13
13
  import { batchPaths, deltaBody, deltaBytes, deltaDigest, historyOutcome, houseChanges, houseMove, mutationOutcome, operationHead, rejectedMapping, revisionBody, uploadable, } from './write.js';
14
- import { houseReferrer } from './referrer.js';
15
- import { followsVersion, logPosition, presented, reachedVersion, roomVersion, } from './version.js';
14
+ import { admittedPath } from './authored-path.js';
15
+ import { HOUSE_TYPED_PATHS } from './house-model.js';
16
+ import { HOUSE_NAMEABLE_ROOM_TYPES, HOUSE_ROOM_TYPES } from './room-types.js';
17
+ import { followsVersion, reachedVersion, roomVersion } from './version.js';
16
18
  const UNCERTAIN = { kind: 'transport-uncertainty' };
17
19
  const UNCERTAIN_READ = { kind: 'transport-uncertainty' };
20
+ const OCTET_STREAM = 'application/octet-stream';
21
+ const EXPIRED_ADDRESS = 403;
18
22
  function lapsedGrant(outcome) {
19
23
  return outcome.kind === 'rejected' && outcome.code === 'upload_unavailable';
20
24
  }
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
- }
50
- return files.length === 0 ? null : { room: authority, through, files };
51
- }
52
- async function reused(scope, held) {
53
- const sent = new Set(scope.contents.map((entry) => entry.path));
54
- 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) {
62
- return null;
63
- }
64
- contents.push({ path: file.path, content });
65
- }
66
- return { ...scope, contents };
67
- }
68
25
  export function createAgentsHouseAdapter(options) {
69
26
  if ('site' in options)
70
27
  return createSiteAdapter(options.site, options.snapshot === true);
71
28
  const transport = options.transport;
72
- const notifications = houseNotifications(transport, 'credential');
29
+ const spaces = spaceSchemas();
30
+ const notifications = houseNotifications(transport, 'credential', {
31
+ opened: async (frame) => (frame === null ? null : opened(frame)),
32
+ changed,
33
+ expired: (authorities) => lister.askAll(authorities.flatMap((authority) => [...tracked.values()].filter((entry) => entry.authority === authority))),
34
+ }, spaces);
73
35
  const tracked = new Map();
74
- const handed = new Map();
36
+ const lister = chunkLister(transport, () => declared().max_prefixes);
37
+ const chunked = new Map();
38
+ const renewals = new WeakMap();
75
39
  let bounds;
76
40
  function declared() {
77
41
  if (bounds === undefined)
78
42
  throw new Error('no Discovery declared the bounds');
79
43
  return bounds;
80
44
  }
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;
87
- }
88
- return next.then((room) => {
89
- if (handed.get(locator) === held)
90
- handed.set(locator, room);
91
- else
92
- handed.delete(locator);
93
- });
94
- }
95
- async function bootstrappedRoom(envelope, sent, asked, continued) {
96
- const { entry } = asked;
97
- if (sent === undefined)
98
- return { kind: 'room-not-readable', locator: entry.locator };
99
- const scope = await reused(sent, asked.held);
100
- if (scope === null) {
101
- return asked.held === undefined
102
- ? UNCERTAIN_READ
103
- : continued === null
104
- ? bootstrapTogether({ entry, held: undefined })
105
- : continuedBootstrap({ entry, held: undefined }, continued);
106
- }
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));
111
- else
112
- handed.delete(entry.locator);
113
- notifications.reach(entry.authority, scope);
114
- const seen = [
115
- ...(continued?.seen ?? []),
116
- ...entry.schemas.filter((schema) => scope.files.some((file) => file.path === schema)),
117
- ];
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 }) };
45
+ function opened(frame) {
46
+ const document = parseDiscovery(frame);
47
+ if (document === null)
48
+ return null;
49
+ bounds = document.bounds;
50
+ tracked.clear();
51
+ lister.forget();
52
+ for (const entry of trackedFrom(document))
53
+ tracked.set(entry.locator, entry);
54
+ notifications.track([...tracked.values()]);
122
55
  return {
123
- ...(await stepped(bootstrapping(scope))),
124
- ...(models === undefined ? {} : { models }),
125
- ...(continuation === null ? {} : { pageToken: JSON.stringify(continuation) }),
56
+ ...mapDiscovery(document, document.authorities.map((authority) => discoveredRoom(authority)), capabilities(document.actions)),
57
+ member: document.member,
126
58
  };
127
59
  }
128
- async function enumeratedPage(ask, page) {
129
- const answer = await transport.door(DOOR_ENUMERATE, {
130
- prefixes: [authorityRoot(ask.entry.handle)],
131
- positions: [
132
- {
133
- ...presented(ask.entry.authority, { position: page.after, logEpoch: ask.from.logEpoch }),
134
- page: { through: page.through, read: page.read },
135
- },
136
- ],
137
- });
138
- const failure = classifyRead(answer.status, answer.body, ask.entry.locator);
139
- if (failure !== null)
140
- return failure;
141
- const enumeration = parseEnumeration(answer.body, 'credential');
142
- if (enumeration === null)
143
- return UNCERTAIN_READ;
144
- return (viewOf(enumeration, ask.entry) ?? { kind: 'room-not-readable', locator: ask.entry.locator });
60
+ function changed(frame) {
61
+ const change = parseAuthorityChange(frame);
62
+ if (change === null)
63
+ return null;
64
+ for (const locator of change.left)
65
+ tracked.delete(locator);
66
+ for (const entry of trackedFrom(change))
67
+ tracked.set(entry.locator, entry);
68
+ notifications.track([...tracked.values()]);
69
+ return {
70
+ kind: 'authority-changed',
71
+ rooms: change.authorities.map((authority) => discoveredRoom(authority)),
72
+ left: change.left,
73
+ member: change.member,
74
+ };
145
75
  }
146
- async function enumeratedRoom(enumeration, ask) {
147
- const first = viewOf(enumeration, ask.entry);
148
- if (first === undefined)
149
- return { kind: 'room-not-readable', locator: ask.entry.locator };
150
- const view = await wholeView(first, (page) => enumeratedPage(ask, page));
151
- if ('kind' in view)
152
- return view;
153
- notifications.reach(ask.entry.authority, view);
154
- const answer = await refreshFrom(view, ask.fromRoomVersion);
155
- if (answer.kind !== 'net-advancement')
156
- return answer;
157
- await hand(ask.entry.locator, enumeratedHanded(view, handed.get(ask.entry.locator)));
158
- const models = await viewModels(view, ask.entry);
159
- return models === undefined ? answer : { ...answer, models };
76
+ async function listedRoom(entry) {
77
+ const listed = await lister.list(entry);
78
+ if ('kind' in listed)
79
+ return listed;
80
+ const head = await chunkedHead(listed, entry, spaces);
81
+ if (head.kind === 'bootstrap')
82
+ chunked.set(entry.locator, listed);
83
+ return head;
160
84
  }
161
- async function streamed(entry, request, from) {
162
- const held = handed.get(entry.locator);
163
- if (held?.roomVersion !== request.fromRoomVersion)
85
+ async function chunkRead(entry, id) {
86
+ const room = chunked.get(entry.locator);
87
+ const address = room?.chunks.get(id);
88
+ if (room === undefined || address === undefined)
164
89
  return null;
165
- const advanced = await stepped(notifications.advance(entry, from, (path, revision) => held.revisions[path] === revision));
166
- if (advanced === null)
90
+ const read = await transport.download(address.url);
91
+ if (read.status !== EXPIRED_ADDRESS)
92
+ return { room, read };
93
+ const renewed = await renewal(entry, room);
94
+ if ('kind' in renewed || !renewed.chunks.has(id))
167
95
  return null;
168
- await hand(entry.locator, streamedHanded(held, advanced));
169
- const models = await streamedModels(advanced.schemas, entry, 'credential');
170
- return models === undefined ? advanced.page : { ...advanced.page, models };
96
+ chunked.set(entry.locator, renewed);
97
+ return { room: renewed, read: await transport.download(renewed.chunks.get(id).url) };
171
98
  }
172
- function held(rooms) {
173
- const kept = rooms.flatMap((room) => {
174
- const entry = tracked.get(room.locator);
175
- const at = logPosition(room.roomVersion);
176
- if (entry === undefined || at === null)
177
- return [];
178
- handed.set(room.locator, heldHanded(room));
179
- notifications.reach(entry.authority, at);
180
- return [presented(entry.authority, at)];
181
- });
182
- notifications.send(kept);
99
+ function renewal(entry, stale) {
100
+ const held = renewals.get(stale);
101
+ if (held !== undefined)
102
+ return held;
103
+ const renewed = lister.renew(entry, [...chunked.keys()].flatMap((locator) => tracked.get(locator) ?? []));
104
+ renewals.set(stale, renewed);
105
+ return renewed;
183
106
  }
184
- async function adoptedMove(entry, into, outcome) {
185
- if (outcome.kind === 'accepted' && outcome.destination !== undefined) {
186
- await adopted(into, { kind: 'accepted', ...outcome.destination });
107
+ function hold(rooms) {
108
+ for (const room of rooms) {
109
+ const listed = chunked.get(room.locator);
110
+ if (listed !== undefined &&
111
+ room.roomVersion !== null &&
112
+ reachedVersion(room.roomVersion, roomVersion(listed), listed.logEpoch))
113
+ chunked.delete(room.locator);
187
114
  }
188
- return adopted(entry, outcome);
115
+ notifications.send(rooms);
189
116
  }
190
- async function adopted(entry, outcome) {
191
- if (outcome.kind !== 'accepted')
192
- return outcome;
193
- const held = handed.get(entry.locator);
194
- if (held === undefined)
195
- return outcome;
196
- const follows = followsVersion(held.roomVersion, outcome.roomVersion);
197
- await hand(entry.locator, acceptedHanded(held, outcome, follows ? outcome.roomVersion : held.roomVersion));
198
- const at = logPosition(outcome.roomVersion);
199
- if (follows && at !== null)
200
- notifications.reach(entry.authority, at);
201
- return outcome;
117
+ async function chunkPage(entry, id, arriving) {
118
+ const fetched = await chunkRead(entry, id);
119
+ if (fetched === null || fetched.read.status !== 200)
120
+ return UNCERTAIN_READ;
121
+ return chunkPart(fetched.read.body, fetched.room, id, arriving);
202
122
  }
203
123
  function authorityFor(locator) {
204
124
  return tracked.get(locator);
@@ -217,187 +137,72 @@ export function createAgentsHouseAdapter(options) {
217
137
  return result;
218
138
  return { ...result, at: { locator, roomVersion: roomVersion({ position, logEpoch }) } };
219
139
  }
220
- async function holding(asks, after) {
221
- const bound = Math.floor(declared().max_held_files / Math.max(1, asks.filter((ask) => ask.held !== undefined).length));
222
- const held = [];
223
- for (const ask of asks) {
224
- if (ask.held === undefined || bound === 0)
225
- continue;
226
- const paths = await sortedHeld(ask.held);
227
- const files = heldFiles(ask.entry.authority, ask.held, paths, after, bound);
228
- if (files !== null)
229
- held.push(files);
230
- }
231
- return held;
232
- }
233
- async function bootstrapped(asks, answer) {
234
- let open = asks.map((asked, ask) => ({ entry: asked.entry, asked, ask }));
235
- while (open.length > 0) {
236
- const unreached = [];
237
- for (const part of selections(open, declared().max_prefixes)) {
238
- const held = await holding(part.map(({ asked }) => asked), null);
239
- const answered = await transport.door(DOOR_BOOTSTRAP, {
240
- prefixes: part.map(({ entry }) => authorityRoot(entry.handle)),
241
- ...(held.length === 0 ? {} : { held }),
242
- });
243
- const classified = part.map(({ entry }) => classifyDoor(answered.status, answered.body, entry.locator));
244
- const envelope = classified[0] === null ? await stepped(parseEnvelope(answered.body)) : null;
245
- for (const [index, asked] of part.entries()) {
246
- const failure = classified[index];
247
- if (failure !== null && failure !== undefined) {
248
- answer(asked.ask, failure);
249
- continue;
250
- }
251
- if (envelope === null) {
252
- answer(asked.ask, UNCERTAIN_READ);
253
- continue;
254
- }
255
- const scope = envelope.scopes.find((candidate) => matchesScope(candidate, asked.entry));
256
- if (scope === undefined && envelope.next !== undefined) {
257
- unreached.push(asked);
258
- continue;
259
- }
260
- answer(asked.ask, await bootstrappedRoom(envelope, scope, asked.asked, null));
261
- }
262
- }
263
- open = unreached;
264
- }
265
- }
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
- function layered(asks) {
289
- const depths = new Map();
290
- const layers = [];
291
- for (const ask of asks) {
292
- const depth = depths.get(ask.ask.entry.authority) ?? 0;
293
- depths.set(ask.ask.entry.authority, depth + 1);
294
- (layers[depth] ??= []).push(ask);
295
- }
296
- return layers;
297
- }
298
- async function enumerated(asks) {
299
- const answers = [];
300
- const parts = layered(asks.map((ask, index) => ({ ask, index }))).flatMap((layer) => selections(layer, declared().max_prefixes));
301
- for (const part of parts) {
302
- let open = [...part];
303
- while (open.length > 0) {
304
- const answer = await transport.door(DOOR_ENUMERATE, {
305
- prefixes: open.map(({ ask }) => authorityRoot(ask.entry.handle)),
306
- positions: open.map(({ ask }) => presented(ask.entry.authority, ask.from)),
307
- });
308
- const classified = open.map(({ ask }) => classifyRead(answer.status, answer.body, ask.entry.locator));
309
- if (classified[0]?.kind === 'beyond-horizon') {
310
- const named = doorRefusal(answer.body)?.fields.authority;
311
- const expired = open.filter(({ ask }) => ask.entry.authority === named);
312
- const answered = expired.length === 0 ? UNCERTAIN_READ : { kind: 'beyond-horizon' };
313
- const gone = expired.length === 0 ? open : expired;
314
- for (const ask of gone)
315
- answers[ask.index] = answered;
316
- open = open.filter((ask) => !gone.includes(ask));
317
- continue;
318
- }
319
- const enumeration = classified[0] === null ? parseEnumeration(answer.body, 'credential') : null;
320
- for (const [answerIndex, { ask, index }] of open.entries()) {
321
- const failure = classified[answerIndex];
322
- if (failure !== null && failure !== undefined) {
323
- answers[index] = failure;
324
- continue;
325
- }
326
- answers[index] =
327
- enumeration === null ? UNCERTAIN_READ : await enumeratedRoom(enumeration, ask);
328
- }
329
- open = [];
330
- }
331
- }
332
- return answers;
333
- }
334
- const bootstrapTogether = gatheredEach(bootstrapped);
335
- const enumerateTogether = gathered(enumerated);
336
140
  async function uploaded(head, changes, delta, bytes, entry, handed, keep) {
337
141
  const paths = batchPaths(changes);
338
142
  const sha256 = await deltaDigest(delta);
339
143
  if (handed !== undefined && handed.sha256 === sha256) {
340
144
  return { body: { ...head, paths, upload: handed, runtime: notifications.runtime } };
341
145
  }
342
- const minted = await transport.door(DOOR_BATCH_UPLOAD, { ...head, paths, bytes, sha256 });
146
+ const minted = await transport.door(houseDoorRoute('files/batch-upload'), {
147
+ ...head,
148
+ paths,
149
+ bytes,
150
+ sha256,
151
+ });
343
152
  if (minted.status !== 200) {
344
153
  return {
345
154
  outcome: await stepped(mutationOutcome(minted.status, minted.body, entry.locator, entry.handle)),
346
155
  };
347
156
  }
348
157
  const grant = parseGrant(minted.body);
349
- if (grant === null || grant.method !== 'POST')
158
+ if (grant === null)
350
159
  return { outcome: UNCERTAIN };
351
160
  const delivered = await transport.transfer({
352
- act: 'authored_batch_upload',
353
161
  url: grant.url,
354
162
  body: `${grant.operation}\n${delta}`,
355
163
  });
356
164
  if (delivered.status !== 200)
357
165
  return { outcome: UNCERTAIN };
358
166
  const holding = parseGrant(delivered.body);
359
- if (holding === null || holding.method !== 'POST')
167
+ if (holding === null)
360
168
  return { outcome: UNCERTAIN };
361
169
  const upload = { url: holding.url, operation: holding.operation, bytes, sha256 };
362
170
  await keep(upload);
363
171
  return { body: { ...head, paths, upload, runtime: notifications.runtime } };
364
172
  }
365
- async function staged(answer, batch) {
173
+ async function staged(answer, { changeBatch: batch, file }) {
366
174
  const refusal = doorRefusal(answer.body);
367
175
  if (refusal?.code !== 'files_unstaged')
368
176
  return null;
369
- const grants = refusal.fields.upload;
370
- if (!Array.isArray(grants))
177
+ const uploads = refusal.fields.upload;
178
+ if (!Array.isArray(uploads))
371
179
  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)
180
+ for (const named of uploads) {
181
+ const upload = houseUpload(named);
182
+ const change = batch.changes.find((held) => held.kind === 'binary' && held.sha256 === named.sha256);
183
+ if (upload === null || change === undefined)
376
184
  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)
185
+ const finished = await finishedUpload(transport, upload, await file(change.localId));
186
+ if (finished?.body?.staged !== true)
384
187
  return false;
385
188
  }
386
189
  return true;
387
190
  }
388
- async function submit(batch, head, changes, delta, bytes, entry, handed, keep, into) {
191
+ async function submit(request, head, changes, delta, bytes, entry, handed, keep, into) {
389
192
  const inline = { ...head, changes, runtime: notifications.runtime };
390
193
  const prepared = uploadable(inline, bytes, declared())
391
194
  ? await uploaded(head, changes, delta, bytes, entry, handed, keep)
392
195
  : { body: inline };
393
196
  if ('outcome' in prepared)
394
197
  return prepared.outcome;
395
- const answer = await transport.door(DOOR_BATCH, prepared.body);
396
- const stagedFiles = await staged(answer, batch);
198
+ const answer = await transport.door(houseDoorRoute('files/batch'), prepared.body);
199
+ const stagedFiles = await staged(answer, request);
397
200
  if (stagedFiles === false)
398
201
  return UNCERTAIN;
399
- const settled = stagedFiles === null ? answer : await transport.door(DOOR_BATCH, prepared.body);
400
- if (stagedFiles === true && (await staged(settled, batch)) !== null)
202
+ const settled = stagedFiles === null
203
+ ? answer
204
+ : await transport.door(houseDoorRoute('files/batch'), prepared.body);
205
+ if (stagedFiles === true && (await staged(settled, request)) !== null)
401
206
  return UNCERTAIN;
402
207
  return stepped(mutationOutcome(settled.status, settled.body, entry.locator, entry.handle, into === undefined
403
208
  ? undefined
@@ -406,7 +211,7 @@ export function createAgentsHouseAdapter(options) {
406
211
  destination: { authority: into.authority, handle: into.handle },
407
212
  }));
408
213
  }
409
- function granted(answer, locator) {
214
+ function addressed(answer, locator) {
410
215
  const failure = classifyDoor(answer.status, answer.body, locator);
411
216
  if (failure !== null) {
412
217
  return failure.kind === 'scoped-refusal'
@@ -418,99 +223,75 @@ export function createAgentsHouseAdapter(options) {
418
223
  }
419
224
  : failure;
420
225
  }
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 };
226
+ const address = parseFileAddress(answer.body);
227
+ return address === null ? UNCERTAIN_READ : { kind: 'address', ...address };
228
+ }
229
+ function linked(answer, locator) {
230
+ const address = addressed(answer, locator);
231
+ return address.kind === 'address'
232
+ ? { kind: 'link', url: address.url, name: address.name, mediaType: address.mediaType }
233
+ : address;
234
+ }
235
+ function refusedAttach(answer, locator) {
236
+ const failure = classifyDoor(answer.status, answer.body, locator);
237
+ if (failure === null)
238
+ return null;
239
+ return failure.kind === 'scoped-refusal'
240
+ ? {
241
+ kind: 'rejected',
242
+ code: failure.code,
243
+ fields: failure.fields,
244
+ retryable: failure.retryable,
245
+ }
246
+ : failure;
427
247
  }
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)
248
+ async function downloaded(ask, locator) {
249
+ let address = addressed(await ask(), locator);
250
+ if (address.kind !== 'address')
251
+ return address;
252
+ let read = await transport.download(address.url);
253
+ if (read.status === EXPIRED_ADDRESS) {
254
+ address = addressed(await ask(), locator);
255
+ if (address.kind !== 'address')
256
+ return address;
257
+ read = await transport.download(address.url);
258
+ }
259
+ if (read.status !== 200 || read.body === null)
434
260
  return UNCERTAIN_READ;
435
- return { kind: 'binary', blob: read.blob };
261
+ return {
262
+ kind: 'binary',
263
+ body: read.body,
264
+ mediaType: address.mediaType,
265
+ sha256: address.sha256,
266
+ };
436
267
  }
437
- function pathGrant(request) {
268
+ function pathAddress(request) {
438
269
  const entry = authorityFor(request.locator);
439
270
  if (entry === undefined)
440
271
  return null;
441
- return transport.door(DOOR_FILES_GET, {
272
+ return () => transport.door(houseDoorRoute('files/read'), {
442
273
  room_ref: entry.authority,
443
274
  path: request.path,
444
275
  revision: request.revision,
445
276
  });
446
277
  }
447
278
  return {
279
+ roomTypes: HOUSE_ROOM_TYPES,
280
+ nameableRoomTypes: HOUSE_NAMEABLE_ROOM_TYPES,
281
+ typedPaths: HOUSE_TYPED_PATHS,
282
+ pathRule: admittedPath,
283
+ unkeptReads: HOUSE_UNKEPT_READS,
284
+ leadingReads: HOUSE_LEADING_READS,
448
285
  async authorize() {
449
286
  return options.authorize();
450
287
  },
451
- async discover(_authorization, scope) {
452
- const answer = await transport.door(DOOR_DISCOVER, {
453
- prefixes: [...DISCOVER_PREFIXES],
454
- ...(scope === undefined ? {} : { rooms: scope.authorities }),
455
- });
456
- const failed = discoveryFailure(answer.status, answer.body);
457
- if (failed !== null)
458
- return failed;
459
- const document = parseDiscovery(answer.body);
460
- if (document === null)
461
- return { kind: 'transport-uncertainty' };
462
- bounds = document.bounds;
463
- const entries = trackedFrom(document);
464
- const covered = scope === undefined
465
- ? null
466
- : [
467
- ...new Set([
468
- ...[...tracked.values()]
469
- .filter((entry) => scope.authorities.includes(entry.authority))
470
- .map((entry) => entry.locator),
471
- ...entries.map((entry) => entry.locator),
472
- ]),
473
- ];
474
- if (covered === null)
475
- tracked.clear();
476
- else
477
- for (const locator of covered)
478
- tracked.delete(locator);
479
- for (const entry of entries)
480
- tracked.set(entry.locator, entry);
481
- for (const locator of handed.keys())
482
- if (!tracked.has(locator))
483
- handed.delete(locator);
484
- notifications.track([...tracked.values()]);
485
- return {
486
- ...mapDiscovery(document, document.authorities.map((authority) => discoveredRoom(authority)), capabilities(document.actions, [...tracked.values()])),
487
- member: document.member,
488
- ...(covered === null ? {} : { covered }),
489
- };
490
- },
491
288
  async bootstrap(_authorization, request) {
492
289
  const entry = authorityFor(request.locator);
493
290
  if (entry === undefined)
494
291
  return { kind: 'room-not-readable', locator: request.locator };
495
- const asked = { entry, held: request.held };
496
- if (request.pageToken !== undefined) {
497
- return continuedBootstrap(asked, JSON.parse(request.pageToken));
498
- }
499
- return bootstrapTogether(asked);
500
- },
501
- async refresh(_authorization, request) {
502
- const entry = authorityFor(request.locator);
503
- if (entry === undefined)
504
- return { kind: 'room-not-readable', locator: request.locator };
505
- if (request.pageToken !== undefined)
506
- return { kind: 'transport-uncertainty' };
507
- const from = logPosition(request.fromRoomVersion);
508
- if (from === null)
509
- return { kind: 'beyond-horizon' };
510
- const answered = await streamed(entry, request, from);
511
- if (answered !== null)
512
- return answered;
513
- return enumerateTogether({ entry, from, fromRoomVersion: request.fromRoomVersion });
292
+ return request.pageToken === undefined
293
+ ? listedRoom(entry)
294
+ : chunkPage(entry, request.pageToken, request.arriving);
514
295
  },
515
296
  async mutate(_authorization, request, keep) {
516
297
  const entry = authorityFor(request.locator);
@@ -527,63 +308,68 @@ export function createAgentsHouseAdapter(options) {
527
308
  : houseMove(batch, entry.handle, into.handle);
528
309
  if (mapped.kind === 'unmapped')
529
310
  return rejectedMapping(mapped);
530
- const head = operationHead(batch);
311
+ const head = operationHead(batch, entry.authority);
531
312
  const delta = deltaBody(mapped.changes);
532
313
  const bytes = deltaBytes(delta);
533
314
  const handed = request.upload;
534
- const answer = (upload) => submit(batch, head, mapped.changes, delta, bytes, entry, upload, keep, into);
535
- const settle = (outcome) => into === undefined ? adopted(entry, outcome) : adoptedMove(entry, into, outcome);
315
+ const answer = (upload) => submit(request, head, mapped.changes, delta, bytes, entry, upload, keep, into);
536
316
  const outcome = await answer(handed);
537
317
  if (handed === undefined || !lapsedGrant(outcome))
538
- return settle(outcome);
539
- return settle(await answer(undefined));
318
+ return outcome;
319
+ return answer(undefined);
540
320
  },
541
321
  async history(_authorization, request) {
542
322
  const entry = authorityFor(request.locator);
543
323
  if (entry === undefined)
544
324
  return { kind: 'room-not-readable', locator: request.locator };
545
- const answer = await transport.door(DOOR_REVISIONS, revisionBody({
325
+ const answer = await transport.door(houseDoorRoute('files/revisions/list'), revisionBody({
546
326
  path: request.path,
547
327
  handle: entry.handle,
548
328
  ...(request.revision === undefined ? {} : { revision: request.revision }),
549
329
  ...(request.pageToken === undefined ? {} : { pageToken: request.pageToken }),
550
330
  }));
551
- return historyOutcome(answer.status, answer.body, entry.locator, {
552
- path: request.path,
553
- ...(request.revision === undefined ? {} : { revision: request.revision }),
554
- });
331
+ return historyOutcome(answer.status, answer.body, entry.locator, request);
555
332
  },
556
333
  async binary(_authorization, request) {
557
- if (request.kind === 'path') {
558
- const asked = pathGrant(request);
559
- if (asked === null)
560
- return { kind: 'room-not-readable', locator: request.locator };
561
- return downloaded(await asked, request.locator);
562
- }
563
- const referrer = houseReferrer(request.referrer, tracked);
564
- if (referrer === null) {
565
- return { kind: 'rejected', code: 'referrer_unknown', fields: {}, retryable: false };
566
- }
567
- const answer = await transport.door(DOOR_FILES_GET, {
568
- file: request.fileRef,
569
- referrer: referrer.referrer,
570
- });
571
- return downloaded(answer, referrer.locator);
334
+ const ask = pathAddress(request);
335
+ if (ask === null)
336
+ return { kind: 'room-not-readable', locator: request.locator };
337
+ return downloaded(ask, request.locator);
572
338
  },
573
339
  async link(_authorization, request) {
574
- const asked = pathGrant(request);
575
- if (asked === null)
340
+ const ask = pathAddress(request);
341
+ if (ask === null)
576
342
  return { kind: 'room-not-readable', locator: request.locator };
577
- return granted(await asked, request.locator);
343
+ return linked(await ask(), request.locator);
578
344
  },
579
- 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 }) };
345
+ async attach(_authorization, request) {
346
+ const entry = authorityFor(request.locator);
347
+ if (entry === undefined)
348
+ return { kind: 'room-not-readable', locator: request.locator };
349
+ const declared = await transport.door(houseDoorRoute('files/attach'), {
350
+ room_ref: entry.authority,
351
+ name: request.name,
352
+ bytes: request.byteLength,
353
+ sha256: request.sha256,
354
+ media_type: request.mediaType.length > 0 ? request.mediaType : OCTET_STREAM,
355
+ });
356
+ const refused = refusedAttach(declared, request.locator);
357
+ if (refused !== null)
358
+ return refused;
359
+ const upload = houseUpload(declared.body?.upload);
360
+ if (upload === null)
361
+ return UNCERTAIN_READ;
362
+ const finished = await finishedUpload(transport, upload, request.blob);
363
+ if (finished === null)
364
+ return UNCERTAIN_READ;
365
+ const unfinished = refusedAttach(finished, request.locator);
366
+ if (unfinished !== null)
367
+ return unfinished;
368
+ const { path, revision } = (finished.body ?? {});
369
+ const root = `/rooms/${entry.handle}/`;
370
+ return typeof path === 'string' && path.startsWith(root) && typeof revision === 'string'
371
+ ? { kind: 'attached', path: path.slice(root.length), revision }
372
+ : UNCERTAIN_READ;
587
373
  },
588
374
  reached(locator, version, target) {
589
375
  return reachedVersion(version, target, authorityFor(locator)?.logEpoch);
@@ -594,15 +380,12 @@ export function createAgentsHouseAdapter(options) {
594
380
  async action(_authorization, request) {
595
381
  return withAt(await houseAction(transport, request));
596
382
  },
383
+ search(_authorization, request) {
384
+ return houseSearch(transport, [...tracked.values()], request);
385
+ },
597
386
  roomStream: {
598
- hold: held,
599
- reopen: notifications.reopen,
387
+ hold,
600
388
  observe: notifications.observe,
601
- published(locator, roomVersion) {
602
- const at = logPosition(roomVersion);
603
- if (at !== null)
604
- notifications.published(locator, at);
605
- },
606
389
  },
607
390
  conversations: notifications.conversations,
608
391
  serviceMode: notifications.serviceMode,