@agentshouse/mdruntime-adapter 0.1.0-cc7fa1e066844 → 0.1.0-ccd2ccb1f7214

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 (51) hide show
  1. package/AGENTS.md +280 -188
  2. package/README.md +3 -3
  3. package/dist/actions.d.ts +63 -2
  4. package/dist/actions.d.ts.map +1 -1
  5. package/dist/actions.js +157 -138
  6. package/dist/adapter.d.ts +1 -0
  7. package/dist/adapter.d.ts.map +1 -1
  8. package/dist/adapter.js +359 -171
  9. package/dist/discovery.d.ts +6 -12
  10. package/dist/discovery.d.ts.map +1 -1
  11. package/dist/discovery.js +31 -35
  12. package/dist/gather.d.ts +1 -0
  13. package/dist/gather.d.ts.map +1 -1
  14. package/dist/gather.js +11 -6
  15. package/dist/handed.d.ts +15 -0
  16. package/dist/handed.d.ts.map +1 -0
  17. package/dist/handed.js +38 -0
  18. package/dist/index.d.ts +2 -1
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +1 -0
  21. package/dist/model.d.ts +7 -18
  22. package/dist/model.d.ts.map +1 -1
  23. package/dist/model.js +60 -39
  24. package/dist/read.d.ts +35 -7
  25. package/dist/read.d.ts.map +1 -1
  26. package/dist/read.js +164 -86
  27. package/dist/referrer.d.ts +7 -0
  28. package/dist/referrer.d.ts.map +1 -0
  29. package/dist/referrer.js +35 -0
  30. package/dist/refusal.d.ts +5 -9
  31. package/dist/refusal.d.ts.map +1 -1
  32. package/dist/refusal.js +31 -17
  33. package/dist/site.d.ts +1 -1
  34. package/dist/site.d.ts.map +1 -1
  35. package/dist/site.js +208 -107
  36. package/dist/stream.d.ts +10 -4
  37. package/dist/stream.d.ts.map +1 -1
  38. package/dist/stream.js +413 -47
  39. package/dist/transport.d.ts +94 -11
  40. package/dist/transport.d.ts.map +1 -1
  41. package/dist/transport.js +3 -1
  42. package/dist/version.d.ts +2 -0
  43. package/dist/version.d.ts.map +1 -1
  44. package/dist/version.js +10 -1
  45. package/dist/write.d.ts +2 -1
  46. package/dist/write.d.ts.map +1 -1
  47. package/dist/write.js +97 -15
  48. package/package.json +3 -4
  49. package/dist/links.d.ts +0 -12
  50. package/dist/links.d.ts.map +0 -1
  51. package/dist/links.js +0 -66
package/dist/adapter.js CHANGED
@@ -1,107 +1,163 @@
1
- import { DISCOVER_PREFIXES, capabilities, discoveredRoom, locatorOf, mapDiscovery, parseDiscovery, trackedFrom, } from './discovery.js';
2
- import { MODEL_ABSENT, failedModel, heldModel, modelKey, modelShelf, } from './model.js';
3
- import { gathered, selections } from './gather.js';
1
+ import { DISCOVER_PREFIXES, capabilities, discoveredRoom, mapDiscovery, parseDiscovery, trackedFrom, } from './discovery.js';
2
+ import { partModels, streamedModels, viewModels } from './model.js';
3
+ import { gathered, gatheredEach, selections } from './gather.js';
4
4
  import { createSiteAdapter } from './site.js';
5
- import { bootstrapOf, contentPrefixes, matchesScope, parseEnumeration, parseEnvelope, parseGrant, refreshFrom, schemaFrom, schemaPath, viewOf, } from './read.js';
6
- import { enumeratedLinks, scopeLinks, streamedLinks } from './links.js';
5
+ import { bootstrapOf, contentPrefixes, matchesScope, parseEnumeration, parseEnvelope, parseGrant, refreshFrom, viewOf, wholeView, } from './read.js';
6
+ import { sha256Hex } from './digest.js';
7
+ import { acceptedHanded, enumeratedHanded, heldHanded, scopeHanded, streamedHanded, } from './handed.js';
7
8
  import { houseAction } from './actions.js';
8
- import { classifyDoor, discoveryFailure, doorRefusal } from './refusal.js';
9
+ import { classifyDoor, classifyRead, discoveryFailure, doorRefusal } from './refusal.js';
9
10
  import { houseNotifications } from './stream.js';
10
- import { DOOR_BATCH, DOOR_BATCH_UPLOAD, DOOR_BOOTSTRAP, DOOR_DISCOVER, DOOR_ENUMERATE, DOOR_REVISIONS, PRIVATE_LOCATOR, } from './transport.js';
11
+ import { DOOR_BATCH, DOOR_BATCH_UPLOAD, DOOR_BOOTSTRAP, DOOR_DISCOVER, DOOR_ENUMERATE, DOOR_FILES_GET, DOOR_REVISIONS, } from './transport.js';
11
12
  import { batchPaths, deltaBody, deltaBytes, deltaDigest, historyOutcome, houseChanges, mutationOutcome, operationHead, rejectedMapping, revisionBody, uploadable, } from './write.js';
12
- import { logPosition, presented, reachedVersion, roomVersion, } from './version.js';
13
+ import { houseReferrer } from './referrer.js';
14
+ import { followsVersion, logPosition, presented, reachedVersion, roomVersion, } from './version.js';
13
15
  const UNCERTAIN = { kind: 'transport-uncertainty' };
14
16
  const UNCERTAIN_READ = { kind: 'transport-uncertainty' };
15
17
  function lapsedGrant(outcome) {
16
18
  return outcome.kind === 'rejected' && outcome.code === 'upload_unavailable';
17
19
  }
18
- function modelFailure(classified, envelope) {
19
- if (classified === null) {
20
- return envelope === null ? { kind: 'transport-uncertainty' } : MODEL_ABSENT;
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 };
30
+ }
31
+ async function reused(scope, held) {
32
+ const sent = new Set(scope.contents.map((entry) => entry.path));
33
+ const contents = [...scope.contents];
34
+ for (const file of scope.files) {
35
+ if (file.binary !== undefined || sent.has(file.path))
36
+ continue;
37
+ const content = held?.documents[file.path];
38
+ if (content === undefined ||
39
+ held?.revisions[file.path] !== file.revision ||
40
+ (await sha256Hex(content)) !== file.sha256) {
41
+ return null;
42
+ }
43
+ contents.push({ path: file.path, content });
21
44
  }
22
- return failedModel(classified);
45
+ return { ...scope, contents };
23
46
  }
24
47
  export function createAgentsHouseAdapter(options) {
25
48
  if ('site' in options)
26
- return createSiteAdapter(options.site);
49
+ return createSiteAdapter(options.site, options.snapshot === true);
27
50
  const transport = options.transport;
28
51
  const notifications = houseNotifications(transport, 'credential');
29
52
  const tracked = new Map();
30
53
  const handed = new Map();
31
- const models = modelShelf();
32
54
  let bounds;
33
55
  function declared() {
34
56
  if (bounds === undefined)
35
57
  throw new Error('no Discovery declared the bounds');
36
58
  return bounds;
37
59
  }
38
- function bootstrappedRoom(envelope, entry) {
39
- const scope = envelope.scopes.find((candidate) => matchesScope(candidate, entry));
40
- if (scope === undefined)
60
+ async function bootstrappedRoom(envelope, sent, asked, continued) {
61
+ const { entry } = asked;
62
+ if (sent === undefined)
41
63
  return { kind: 'room-not-readable', locator: entry.locator };
42
- handed.set(entry.locator, scopeLinks(scope));
43
- notifications.hold(entry.authority, scope);
44
- return bootstrapOf(scope);
64
+ const scope = await reused(sent, asked.held);
65
+ if (scope === null) {
66
+ return asked.held === undefined
67
+ ? UNCERTAIN_READ
68
+ : continued === null
69
+ ? bootstrapTogether({ entry, held: undefined })
70
+ : continuedBootstrap({ entry, held: undefined }, continued);
71
+ }
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));
76
+ else
77
+ handed.delete(entry.locator);
78
+ notifications.reach(entry.authority, scope);
79
+ const seen = [
80
+ ...(continued?.seen ?? []),
81
+ ...entry.schemas.filter((schema) => scope.files.some((file) => file.path === schema)),
82
+ ];
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 }) };
87
+ return {
88
+ ...bootstrapOf(scope),
89
+ ...(models === undefined ? {} : { models }),
90
+ ...(continuation === null ? {} : { pageToken: JSON.stringify(continuation) }),
91
+ };
45
92
  }
46
- function enumeratedRoom(enumeration, ask) {
47
- const view = viewOf(enumeration, ask.entry);
48
- if (view === undefined)
93
+ async function enumeratedPage(ask, page) {
94
+ const answer = await transport.door(DOOR_ENUMERATE, {
95
+ prefixes: contentPrefixes(ask.entry),
96
+ positions: [
97
+ {
98
+ ...presented(ask.entry.authority, { position: page.after, logEpoch: ask.from.logEpoch }),
99
+ page: { through: page.through, read: page.read },
100
+ },
101
+ ],
102
+ });
103
+ const failure = classifyRead(answer.status, answer.body, ask.entry.locator);
104
+ if (failure !== null)
105
+ return failure;
106
+ const enumeration = parseEnumeration(answer.body, 'credential');
107
+ if (enumeration === null)
108
+ return UNCERTAIN_READ;
109
+ return (viewOf(enumeration, ask.entry) ?? { kind: 'room-not-readable', locator: ask.entry.locator });
110
+ }
111
+ async function enumeratedRoom(enumeration, ask) {
112
+ const first = viewOf(enumeration, ask.entry);
113
+ if (first === undefined)
49
114
  return { kind: 'room-not-readable', locator: ask.entry.locator };
50
- notifications.hold(ask.entry.authority, view);
115
+ const view = await wholeView(first, (page) => enumeratedPage(ask, page));
116
+ if ('kind' in view)
117
+ return view;
118
+ notifications.reach(ask.entry.authority, view);
51
119
  const answer = refreshFrom(view, ask.fromRoomVersion);
52
- if (answer.kind === 'net-advancement') {
53
- handed.set(ask.entry.locator, enumeratedLinks(view, handed.get(ask.entry.locator)));
54
- }
55
- return answer;
120
+ if (answer.kind !== 'net-advancement')
121
+ return answer;
122
+ handed.set(ask.entry.locator, enumeratedHanded(view, handed.get(ask.entry.locator)));
123
+ const models = await viewModels(view, ask.entry);
124
+ return models === undefined ? answer : { ...answer, models };
56
125
  }
57
- function streamed(entry, request, from) {
58
- const links = handed.get(entry.locator);
59
- if (links?.roomVersion !== request.fromRoomVersion)
126
+ async function streamed(entry, request, from) {
127
+ const held = handed.get(entry.locator);
128
+ if (held?.roomVersion !== request.fromRoomVersion)
60
129
  return null;
61
- const advanced = notifications.advance(entry, from, (path, revision) => links.revisions.get(path) === revision);
130
+ const advanced = notifications.advance(entry, from, (path, revision) => held.revisions.get(path) === revision);
62
131
  if (advanced === null)
63
132
  return null;
64
- if (request.minPosition !== undefined &&
65
- !reachedVersion(advanced.page.roomVersion, request.minPosition, entry.logEpoch))
66
- return null;
67
- const relinked = streamedLinks(links, advanced);
68
- if (relinked === null)
69
- return null;
70
- handed.set(entry.locator, relinked);
71
- return advanced.page;
72
- }
73
- function held(envelope, entry, schema, failure) {
74
- return heldModel(envelope === null ? null : schemaFrom(envelope, entry, schema), schemaPath(entry.handle, schema), failure);
75
- }
76
- function modelPrefixes(entry) {
77
- return entry.schemas.map((schema) => schemaPath(entry.handle, schema));
133
+ handed.set(entry.locator, streamedHanded(held, advanced));
134
+ const models = await streamedModels(advanced.schemas, entry, 'credential');
135
+ return models === undefined ? advanced.page : { ...advanced.page, models };
78
136
  }
79
- async function acquireModels(entries) {
80
- const acquired = new Map();
81
- for (const part of selections(entries, (entry) => modelPrefixes(entry).length, declared().max_prefixes)) {
82
- const answer = await transport.door(DOOR_BOOTSTRAP, {
83
- prefixes: part.flatMap((entry) => modelPrefixes(entry)),
84
- });
85
- const classified = classifyDoor(answer.status, answer.body, PRIVATE_LOCATOR);
86
- const envelope = classified === null ? parseEnvelope(await bundled(answer.body)) : null;
87
- const failure = modelFailure(classified, envelope);
88
- for (const entry of part) {
89
- acquired.set(modelKey(entry.locator, 'collection'), await held(envelope, entry, entry.schema, failure));
90
- if (entry.houseModel === null)
91
- continue;
92
- acquired.set(modelKey(entry.locator, 'typed-selection'), await held(envelope, entry, entry.houseModel.root, failure));
93
- }
94
- }
95
- return acquired;
137
+ function held(rooms) {
138
+ const kept = rooms.flatMap((room) => {
139
+ const entry = tracked.get(room.locator);
140
+ const at = logPosition(room.roomVersion);
141
+ if (entry === undefined || at === null)
142
+ return [];
143
+ handed.set(room.locator, heldHanded(room));
144
+ notifications.reach(entry.authority, at);
145
+ return [presented(entry.authority, at)];
146
+ });
147
+ notifications.send(kept);
96
148
  }
97
- function roomOf(authority, acquired) {
98
- const locator = locatorOf(authority);
99
- const key = modelKey(locator, 'collection');
100
- const collection = models.retain(key, acquired.get(key));
101
- const selection = authority.houseModel === null
102
- ? undefined
103
- : models.retain(modelKey(locator, 'typed-selection'), acquired.get(modelKey(locator, 'typed-selection')));
104
- return discoveredRoom(authority, collection?.identity ?? null, selection?.identity ?? null);
149
+ function adopted(entry, outcome) {
150
+ if (outcome.kind !== 'accepted')
151
+ return outcome;
152
+ const held = handed.get(entry.locator);
153
+ if (held === undefined)
154
+ return outcome;
155
+ const follows = followsVersion(held.roomVersion, outcome.roomVersion);
156
+ handed.set(entry.locator, acceptedHanded(held, outcome, follows ? outcome.roomVersion : held.roomVersion));
157
+ const at = logPosition(outcome.roomVersion);
158
+ if (follows && at !== null)
159
+ notifications.reach(entry.authority, at);
160
+ return outcome;
105
161
  }
106
162
  function authorityFor(locator) {
107
163
  return tracked.get(locator);
@@ -115,116 +171,126 @@ export function createAgentsHouseAdapter(options) {
115
171
  const { authority, position, log_epoch: logEpoch } = body;
116
172
  if (typeof position !== 'string' || typeof logEpoch !== 'string')
117
173
  return result;
118
- const locator = authority === undefined
119
- ? PRIVATE_LOCATOR
120
- : [...tracked.values()].find((entry) => entry.authority === authority)?.locator;
174
+ const locator = [...tracked.values()].find((entry) => entry.authority === authority)?.locator;
121
175
  if (locator === undefined)
122
176
  return result;
123
177
  return { ...result, at: { locator, roomVersion: roomVersion({ position, logEpoch }) } };
124
178
  }
125
- async function bundled(body) {
126
- const grant = parseGrant(body);
127
- if (grant === null)
128
- return body;
129
- if (grant.method !== 'GET')
130
- return null;
131
- const answer = await transport.transfer({
132
- act: 'local_copy_bundle',
133
- method: 'GET',
134
- url: grant.url,
135
- answer: 'json',
136
- });
137
- return answer.status === 200 ? answer.body : null;
138
- }
139
179
  function weight(entry) {
140
180
  return contentPrefixes(entry).length;
141
181
  }
142
- async function bootstrapped(entries) {
143
- const answers = [];
144
- for (const part of selections(entries, weight, declared().max_prefixes)) {
145
- const answer = await transport.door(DOOR_BOOTSTRAP, {
146
- prefixes: part.flatMap((entry) => contentPrefixes(entry)),
147
- });
148
- const classified = part.map((entry) => classifyDoor(answer.status, answer.body, entry.locator));
149
- const envelope = classified[0] === null ? parseEnvelope(await bundled(answer.body)) : null;
150
- part.forEach((entry, index) => {
151
- const failure = classified[index];
152
- if (failure === null || failure === undefined) {
153
- answers.push(envelope === null ? UNCERTAIN_READ : bootstrappedRoom(envelope, entry));
154
- return;
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) ?? []);
185
+ }
186
+ async function bootstrapped(asks, answer) {
187
+ let open = asks.map((asked, ask) => ({ entry: asked.entry, asked, ask }));
188
+ while (open.length > 0) {
189
+ const unreached = [];
190
+ for (const part of selections(open, ({ entry }) => weight(entry), declared().max_prefixes)) {
191
+ const held = holding(part.map(({ asked }) => asked), null);
192
+ const answered = await transport.door(DOOR_BOOTSTRAP, {
193
+ prefixes: part.flatMap(({ entry }) => contentPrefixes(entry)),
194
+ ...(held.length === 0 ? {} : { held }),
195
+ });
196
+ const classified = part.map(({ entry }) => classifyDoor(answered.status, answered.body, entry.locator));
197
+ const envelope = classified[0] === null ? parseEnvelope(answered.body) : null;
198
+ for (const [index, asked] of part.entries()) {
199
+ const failure = classified[index];
200
+ if (failure !== null && failure !== undefined) {
201
+ answer(asked.ask, failure);
202
+ continue;
203
+ }
204
+ if (envelope === null) {
205
+ answer(asked.ask, UNCERTAIN_READ);
206
+ continue;
207
+ }
208
+ const scope = envelope.scopes.find((candidate) => matchesScope(candidate, asked.entry));
209
+ if (scope === undefined && envelope.next !== undefined) {
210
+ unreached.push(asked);
211
+ continue;
212
+ }
213
+ answer(asked.ask, await bootstrappedRoom(envelope, scope, asked.asked, null));
155
214
  }
156
- answers.push(failure.kind === 'beyond-horizon' ? UNCERTAIN_READ : failure);
157
- });
215
+ }
216
+ open = unreached;
158
217
  }
159
- return answers;
218
+ }
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);
160
240
  }
161
241
  function layered(asks) {
162
242
  const depths = new Map();
163
243
  const layers = [];
164
244
  for (const ask of asks) {
165
- const depth = depths.get(ask.entry.authority) ?? 0;
166
- depths.set(ask.entry.authority, depth + 1);
245
+ const depth = depths.get(ask.ask.entry.authority) ?? 0;
246
+ depths.set(ask.ask.entry.authority, depth + 1);
167
247
  (layers[depth] ??= []).push(ask);
168
248
  }
169
249
  return layers;
170
250
  }
171
251
  async function enumerated(asks) {
172
- const answers = new Map();
173
- const parts = layered(asks).flatMap((layer) => selections(layer, (ask) => weight(ask.entry), declared().max_prefixes));
252
+ const answers = [];
253
+ const parts = layered(asks.map((ask, index) => ({ ask, index }))).flatMap((layer) => selections(layer, ({ ask }) => weight(ask.entry), declared().max_prefixes));
174
254
  for (const part of parts) {
175
255
  let open = [...part];
176
256
  while (open.length > 0) {
177
257
  const answer = await transport.door(DOOR_ENUMERATE, {
178
- prefixes: open.flatMap((ask) => contentPrefixes(ask.entry)),
179
- positions: open.map((ask) => ({
180
- ...presented(ask.entry.authority, ask.from),
181
- ...(ask.minPosition === undefined ? {} : { min_position: ask.minPosition }),
182
- })),
258
+ prefixes: open.flatMap(({ ask }) => contentPrefixes(ask.entry)),
259
+ positions: open.map(({ ask }) => presented(ask.entry.authority, ask.from)),
183
260
  });
184
- const refusal = doorRefusal(answer.body);
185
- if (refusal?.code === 'replica_behind') {
186
- const named = open.filter((ask) => ask.entry.authority === refusal.fields.authority);
187
- const behind = named.length === 0 ? open : named;
188
- for (const ask of behind) {
189
- answers.set(ask, refusal.retryable
190
- ? { kind: 'retryable-refusal', code: refusal.code, fields: refusal.fields }
191
- : (classifyDoor(answer.status, answer.body, ask.entry.locator) ?? UNCERTAIN_READ));
192
- }
193
- open = open.filter((ask) => !behind.includes(ask));
194
- continue;
195
- }
196
- const classified = open.map((ask) => classifyDoor(answer.status, answer.body, ask.entry.locator));
261
+ const classified = open.map(({ ask }) => classifyRead(answer.status, answer.body, ask.entry.locator));
197
262
  if (classified[0]?.kind === 'beyond-horizon') {
198
263
  const named = doorRefusal(answer.body)?.fields.authority;
199
- const expired = open.filter((ask) => ask.entry.authority === named);
264
+ const expired = open.filter(({ ask }) => ask.entry.authority === named);
200
265
  const answered = expired.length === 0 ? UNCERTAIN_READ : { kind: 'beyond-horizon' };
201
266
  const gone = expired.length === 0 ? open : expired;
202
267
  for (const ask of gone)
203
- answers.set(ask, answered);
268
+ answers[ask.index] = answered;
204
269
  open = open.filter((ask) => !gone.includes(ask));
205
270
  continue;
206
271
  }
207
272
  const enumeration = classified[0] === null ? parseEnumeration(answer.body, 'credential') : null;
208
- open.forEach((ask, index) => {
209
- const failure = classified[index];
273
+ for (const [answerIndex, { ask, index }] of open.entries()) {
274
+ const failure = classified[answerIndex];
210
275
  if (failure !== null && failure !== undefined) {
211
- answers.set(ask, failure);
212
- return;
276
+ answers[index] = failure;
277
+ continue;
213
278
  }
214
- answers.set(ask, enumeration === null ? UNCERTAIN_READ : enumeratedRoom(enumeration, ask));
215
- });
279
+ answers[index] =
280
+ enumeration === null ? UNCERTAIN_READ : await enumeratedRoom(enumeration, ask);
281
+ }
216
282
  open = [];
217
283
  }
218
284
  }
219
- return asks.map((ask) => answers.get(ask) ?? UNCERTAIN_READ);
285
+ return answers;
220
286
  }
221
- const bootstrapTogether = gathered(bootstrapped);
287
+ const bootstrapTogether = gatheredEach(bootstrapped);
222
288
  const enumerateTogether = gathered(enumerated);
223
289
  async function uploaded(head, changes, delta, bytes, entry, handed, keep) {
224
290
  const paths = batchPaths(changes);
225
291
  const sha256 = await deltaDigest(delta);
226
292
  if (handed !== undefined && handed.sha256 === sha256) {
227
- return { body: { ...head, paths, upload: handed } };
293
+ return { body: { ...head, paths, upload: handed, runtime: notifications.runtime } };
228
294
  }
229
295
  const minted = await transport.door(DOOR_BATCH_UPLOAD, { ...head, paths, bytes, sha256 });
230
296
  if (minted.status !== 200) {
@@ -235,9 +301,7 @@ export function createAgentsHouseAdapter(options) {
235
301
  return { outcome: UNCERTAIN };
236
302
  const delivered = await transport.transfer({
237
303
  act: 'authored_batch_upload',
238
- method: 'POST',
239
304
  url: grant.url,
240
- answer: 'json',
241
305
  body: `${grant.operation}\n${delta}`,
242
306
  });
243
307
  if (delivered.status !== 200)
@@ -247,53 +311,138 @@ export function createAgentsHouseAdapter(options) {
247
311
  return { outcome: UNCERTAIN };
248
312
  const upload = { url: holding.url, operation: holding.operation, bytes, sha256 };
249
313
  await keep(upload);
250
- return { body: { ...head, paths, upload } };
314
+ return { body: { ...head, paths, upload, runtime: notifications.runtime } };
315
+ }
316
+ async function staged(answer, batch) {
317
+ const refusal = doorRefusal(answer.body);
318
+ if (refusal?.code !== 'files_unstaged')
319
+ return null;
320
+ const grants = refusal.fields.upload;
321
+ if (!Array.isArray(grants))
322
+ return false;
323
+ for (const grant of grants) {
324
+ const { url, operation, sha256 } = grant;
325
+ const change = batch.changes.find((held) => held.kind === 'binary' && held.sha256 === sha256);
326
+ if (typeof url !== 'string' || typeof operation !== 'string' || change === undefined)
327
+ return false;
328
+ const delivered = await transport.transfer({
329
+ act: 'binary_stage',
330
+ url,
331
+ body: change.blob,
332
+ operation,
333
+ });
334
+ if (delivered.status !== 200)
335
+ return false;
336
+ }
337
+ return true;
251
338
  }
252
- async function submit(head, changes, delta, bytes, entry, handed, keep) {
253
- const inline = { ...head, changes };
339
+ async function submit(batch, head, changes, delta, bytes, entry, handed, keep) {
340
+ const inline = { ...head, changes, runtime: notifications.runtime };
254
341
  const prepared = uploadable(inline, bytes, declared())
255
342
  ? await uploaded(head, changes, delta, bytes, entry, handed, keep)
256
343
  : { body: inline };
257
344
  if ('outcome' in prepared)
258
345
  return prepared.outcome;
259
346
  const answer = await transport.door(DOOR_BATCH, prepared.body);
260
- return mutationOutcome(answer.status, answer.body, entry.locator, entry.handle);
347
+ const stagedFiles = await staged(answer, batch);
348
+ if (stagedFiles === false)
349
+ return UNCERTAIN;
350
+ const settled = stagedFiles === null ? answer : await transport.door(DOOR_BATCH, prepared.body);
351
+ if (stagedFiles === true && (await staged(settled, batch)) !== null)
352
+ return UNCERTAIN;
353
+ return mutationOutcome(settled.status, settled.body, entry.locator, entry.handle);
354
+ }
355
+ function granted(answer, locator) {
356
+ const failure = classifyDoor(answer.status, answer.body, locator);
357
+ if (failure !== null) {
358
+ return failure.kind === 'scoped-refusal'
359
+ ? {
360
+ kind: 'rejected',
361
+ code: failure.code,
362
+ fields: failure.fields,
363
+ retryable: failure.retryable,
364
+ }
365
+ : failure;
366
+ }
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 };
373
+ }
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);
379
+ if (read.status !== 200 || read.blob === null)
380
+ return UNCERTAIN_READ;
381
+ return { kind: 'binary', blob: read.blob };
382
+ }
383
+ function pathGrant(request) {
384
+ const entry = authorityFor(request.locator);
385
+ if (entry === undefined)
386
+ return null;
387
+ return transport.door(DOOR_FILES_GET, {
388
+ room_ref: entry.authority,
389
+ path: request.path,
390
+ revision: request.revision,
391
+ });
261
392
  }
262
393
  return {
263
394
  async authorize() {
264
395
  return options.authorize();
265
396
  },
266
- async model(_authorization, request) {
267
- return models.answer(request);
268
- },
269
- async discover(_authorization) {
270
- const answer = await transport.door(DOOR_DISCOVER, { prefixes: [...DISCOVER_PREFIXES] });
271
- const classified = classifyDoor(answer.status, answer.body, PRIVATE_LOCATOR);
272
- if (classified !== null)
273
- return discoveryFailure(classified);
397
+ async discover(_authorization, scope) {
398
+ const answer = await transport.door(DOOR_DISCOVER, {
399
+ prefixes: [...DISCOVER_PREFIXES],
400
+ ...(scope === undefined ? {} : { rooms: scope.authorities }),
401
+ });
402
+ const failed = discoveryFailure(answer.status, answer.body);
403
+ if (failed !== null)
404
+ return failed;
274
405
  const document = parseDiscovery(answer.body);
275
406
  if (document === null)
276
407
  return { kind: 'transport-uncertainty' };
277
408
  bounds = document.bounds;
278
409
  const entries = trackedFrom(document);
279
- tracked.clear();
410
+ const covered = scope === undefined
411
+ ? null
412
+ : [
413
+ ...new Set([
414
+ ...[...tracked.values()]
415
+ .filter((entry) => scope.authorities.includes(entry.authority))
416
+ .map((entry) => entry.locator),
417
+ ...entries.map((entry) => entry.locator),
418
+ ]),
419
+ ];
420
+ if (covered === null)
421
+ tracked.clear();
422
+ else
423
+ for (const locator of covered)
424
+ tracked.delete(locator);
280
425
  for (const entry of entries)
281
426
  tracked.set(entry.locator, entry);
282
427
  for (const locator of handed.keys())
283
428
  if (!tracked.has(locator))
284
429
  handed.delete(locator);
285
- notifications.track(entries);
286
- const acquired = await acquireModels(entries);
287
- models.keep(entries.map((entry) => entry.locator));
288
- return mapDiscovery(document, document.authorities.map((authority) => roomOf(authority, acquired)), capabilities(document.actions));
430
+ notifications.track([...tracked.values()]);
431
+ return {
432
+ ...mapDiscovery(document, document.authorities.map((authority) => discoveredRoom(authority)), capabilities(document.actions, [...tracked.values()])),
433
+ member: document.member,
434
+ ...(covered === null ? {} : { covered }),
435
+ };
289
436
  },
290
437
  async bootstrap(_authorization, request) {
291
438
  const entry = authorityFor(request.locator);
292
439
  if (entry === undefined)
293
440
  return { kind: 'room-not-readable', locator: request.locator };
294
- if (request.pageToken !== undefined)
295
- return { kind: 'transport-uncertainty' };
296
- return bootstrapTogether(entry);
441
+ const asked = { entry, held: request.held };
442
+ if (request.pageToken !== undefined) {
443
+ return continuedBootstrap(asked, JSON.parse(request.pageToken));
444
+ }
445
+ return bootstrapTogether(asked);
297
446
  },
298
447
  async refresh(_authorization, request) {
299
448
  const entry = authorityFor(request.locator);
@@ -304,16 +453,10 @@ export function createAgentsHouseAdapter(options) {
304
453
  const from = logPosition(request.fromRoomVersion);
305
454
  if (from === null)
306
455
  return { kind: 'beyond-horizon' };
307
- const answered = streamed(entry, request, from);
456
+ const answered = await streamed(entry, request, from);
308
457
  if (answered !== null)
309
458
  return answered;
310
- const minPosition = request.minPosition === undefined ? null : logPosition(request.minPosition);
311
- return enumerateTogether({
312
- entry,
313
- from,
314
- fromRoomVersion: request.fromRoomVersion,
315
- ...(minPosition?.logEpoch === from.logEpoch ? { minPosition: minPosition.position } : {}),
316
- });
459
+ return enumerateTogether({ entry, from, fromRoomVersion: request.fromRoomVersion });
317
460
  },
318
461
  async mutate(_authorization, request, keep) {
319
462
  const entry = authorityFor(request.locator);
@@ -326,10 +469,11 @@ export function createAgentsHouseAdapter(options) {
326
469
  const delta = deltaBody(mapped.changes);
327
470
  const bytes = deltaBytes(delta);
328
471
  const handed = request.upload;
329
- const outcome = await submit(head, mapped.changes, delta, bytes, entry, handed, keep);
472
+ const batch = request.changeBatch;
473
+ const outcome = await submit(batch, head, mapped.changes, delta, bytes, entry, handed, keep);
330
474
  if (handed === undefined || !lapsedGrant(outcome))
331
- return outcome;
332
- return submit(head, mapped.changes, delta, bytes, entry, undefined, keep);
475
+ return adopted(entry, outcome);
476
+ return adopted(entry, await submit(batch, head, mapped.changes, delta, bytes, entry, undefined, keep));
333
477
  },
334
478
  async history(_authorization, request) {
335
479
  const entry = authorityFor(request.locator);
@@ -346,13 +490,57 @@ export function createAgentsHouseAdapter(options) {
346
490
  ...(request.revision === undefined ? {} : { revision: request.revision }),
347
491
  });
348
492
  },
493
+ async binary(_authorization, request) {
494
+ if (request.kind === 'path') {
495
+ const asked = pathGrant(request);
496
+ if (asked === null)
497
+ return { kind: 'room-not-readable', locator: request.locator };
498
+ return downloaded(await asked, request.locator);
499
+ }
500
+ const referrer = houseReferrer(request.referrer, tracked);
501
+ if (referrer === null) {
502
+ return { kind: 'rejected', code: 'referrer_unknown', fields: {}, retryable: false };
503
+ }
504
+ const answer = await transport.door(DOOR_FILES_GET, {
505
+ file: request.fileRef,
506
+ referrer: referrer.referrer,
507
+ });
508
+ return downloaded(answer, referrer.locator);
509
+ },
510
+ async link(_authorization, request) {
511
+ const asked = pathGrant(request);
512
+ if (asked === null)
513
+ return { kind: 'room-not-readable', locator: request.locator };
514
+ return granted(await asked, request.locator);
515
+ },
516
+ referrerScope(referrer) {
517
+ const held = houseReferrer(referrer, tracked);
518
+ if (held === null)
519
+ return null;
520
+ if (held.locator === '')
521
+ return { kind: 'conversations' };
522
+ const { path } = held.referrer;
523
+ return { kind: 'room', locator: held.locator, ...(path === undefined ? {} : { path }) };
524
+ },
349
525
  reached(locator, version, target) {
350
526
  return reachedVersion(version, target, authorityFor(locator)?.logEpoch);
351
527
  },
528
+ follows(_locator, version, next) {
529
+ return followsVersion(version, next);
530
+ },
352
531
  async action(_authorization, request) {
353
532
  return withAt(await houseAction(transport, request));
354
533
  },
355
- roomVersions: notifications,
534
+ roomStream: {
535
+ hold: held,
536
+ reopen: notifications.reopen,
537
+ observe: notifications.observe,
538
+ published(locator, roomVersion) {
539
+ const at = logPosition(roomVersion);
540
+ if (at !== null)
541
+ notifications.published(locator, at);
542
+ },
543
+ },
356
544
  conversations: notifications.conversations,
357
545
  serviceMode: notifications.serviceMode,
358
546
  };