@agentshouse/mdruntime-adapter 0.1.0-alpha.5 → 0.1.0-alpha.50

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 (62) hide show
  1. package/AGENTS.md +157 -25
  2. package/README.md +32 -0
  3. package/dist/actions.d.ts +3 -11
  4. package/dist/actions.d.ts.map +1 -1
  5. package/dist/actions.js +201 -232
  6. package/dist/actions.js.map +1 -1
  7. package/dist/adapter.d.ts +3 -1
  8. package/dist/adapter.d.ts.map +1 -1
  9. package/dist/adapter.js +242 -173
  10. package/dist/adapter.js.map +1 -1
  11. package/dist/discovery.d.ts +39 -15
  12. package/dist/discovery.d.ts.map +1 -1
  13. package/dist/discovery.js +169 -43
  14. package/dist/discovery.js.map +1 -1
  15. package/dist/gather.d.ts +3 -0
  16. package/dist/gather.d.ts.map +1 -0
  17. package/dist/gather.js +41 -0
  18. package/dist/gather.js.map +1 -0
  19. package/dist/index.d.ts +1 -1
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js.map +1 -1
  22. package/dist/links.d.ts +11 -0
  23. package/dist/links.d.ts.map +1 -0
  24. package/dist/links.js +57 -0
  25. package/dist/links.js.map +1 -0
  26. package/dist/map.d.ts +2 -2
  27. package/dist/map.d.ts.map +1 -1
  28. package/dist/map.js +2 -3
  29. package/dist/map.js.map +1 -1
  30. package/dist/model.d.ts +19 -0
  31. package/dist/model.d.ts.map +1 -0
  32. package/dist/model.js +64 -0
  33. package/dist/model.js.map +1 -0
  34. package/dist/read.d.ts +39 -27
  35. package/dist/read.d.ts.map +1 -1
  36. package/dist/read.js +246 -91
  37. package/dist/read.js.map +1 -1
  38. package/dist/refusal.d.ts +4 -1
  39. package/dist/refusal.d.ts.map +1 -1
  40. package/dist/refusal.js +13 -1
  41. package/dist/refusal.js.map +1 -1
  42. package/dist/site.d.ts +4 -0
  43. package/dist/site.d.ts.map +1 -0
  44. package/dist/site.js +228 -0
  45. package/dist/site.js.map +1 -0
  46. package/dist/stream.d.ts +11 -5
  47. package/dist/stream.d.ts.map +1 -1
  48. package/dist/stream.js +220 -142
  49. package/dist/stream.js.map +1 -1
  50. package/dist/transport.d.ts +57 -29
  51. package/dist/transport.d.ts.map +1 -1
  52. package/dist/transport.js +6 -1
  53. package/dist/transport.js.map +1 -1
  54. package/dist/version.d.ts +15 -0
  55. package/dist/version.d.ts.map +1 -0
  56. package/dist/version.js +39 -0
  57. package/dist/version.js.map +1 -0
  58. package/dist/write.d.ts +9 -3
  59. package/dist/write.d.ts.map +1 -1
  60. package/dist/write.js +51 -9
  61. package/dist/write.js.map +1 -1
  62. package/package.json +4 -12
package/dist/adapter.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  import type { Adapter, Authorization } from '@agentshouse/mdruntime';
2
- import { type HouseTransport } from './transport.js';
2
+ import { type HouseSiteTransport, type HouseTransport } from './transport.js';
3
3
  export type AgentsHouseAdapterOptions = {
4
4
  transport: HouseTransport;
5
5
  authorize(): Promise<Authorization>;
6
+ } | {
7
+ site: HouseSiteTransport;
6
8
  };
7
9
  export declare function createAgentsHouseAdapter(options: AgentsHouseAdapterOptions): Adapter;
8
10
  //# sourceMappingURL=adapter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,OAAO,EACP,aAAa,EAcd,MAAM,wBAAwB,CAAC;AA2BhC,OAAO,EAQL,KAAK,cAAc,EACpB,MAAM,gBAAgB,CAAC;AAexB,MAAM,MAAM,yBAAyB,GAAG;IACtC,SAAS,EAAE,cAAc,CAAC;IAC1B,SAAS,IAAI,OAAO,CAAC,aAAa,CAAC,CAAC;CACrC,CAAC;AAqCF,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CA0RpF"}
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,OAAO,EACP,aAAa,EAad,MAAM,wBAAwB,CAAC;AA0ChC,OAAO,EASL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACpB,MAAM,gBAAgB,CAAC;AAuBxB,MAAM,MAAM,yBAAyB,GACjC;IACE,SAAS,EAAE,cAAc,CAAC;IAC1B,SAAS,IAAI,OAAO,CAAC,aAAa,CAAC,CAAC;CACrC,GACD;IAAE,IAAI,EAAE,kBAAkB,CAAA;CAAE,CAAC;AAiCjC,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CA4YpF"}
package/dist/adapter.js CHANGED
@@ -1,24 +1,21 @@
1
- import { DISCOVER_PREFIXES, PROVIDER_SELECTION_MODEL_ROOT, discoveredRoom, locatorOf, mapDiscovery, parseDiscovery, providerSelection, trackedFrom } from './discovery.js';
2
- import { authorityRoot, bootstrapFrom, contentPrefixes, definitionsFrom, modelPrefix, parseEnumeration, parseEnvelope, parseGrant, refreshFrom } from './read.js';
1
+ import { DISCOVER_PREFIXES, HOUSE_SCHEMA, capabilities, discoveredRoom, locatorOf, mapDiscovery, parseDiscovery, providerSelection, trackedFrom } from './discovery.js';
2
+ import { MODEL_ABSENT, failedModel, heldModel, modelKey, modelShelf } from './model.js';
3
+ import { gathered, selections } from './gather.js';
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';
3
7
  import { houseAction } from './actions.js';
4
- import { classifyDoor } from './refusal.js';
8
+ import { classifyDoor, discoveryFailure, doorRefusal } from './refusal.js';
5
9
  import { houseNotifications } from './stream.js';
6
- import { DOOR_BATCH, DOOR_BATCH_UPLOAD, DOOR_BOOTSTRAP, DOOR_DISCOVER, DOOR_ENUMERATE, DOOR_REVISIONS, PRIVATE_AUTHORITY } from './transport.js';
7
- import { sha256Hex } from './digest.js';
8
- import { batchPaths, deltaBody, deltaBytes, deltaDigest, historyOutcome, houseChanges, mutationOutcome, rejectedMapping, revisionBody, uploadable } from './write.js';
9
- const MODEL_ABSENT = {
10
- kind: 'model-unavailable',
11
- reason: 'not-readable'
12
- };
10
+ import { DOOR_BATCH, DOOR_BATCH_UPLOAD, DOOR_BOOTSTRAP, DOOR_DISCOVER, DOOR_ENUMERATE, DOOR_REVISIONS, DOOR_SELECTION_MAX, PRIVATE_LOCATOR } from './transport.js';
11
+ 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
13
  const UNCERTAIN = {
14
14
  kind: 'transport-uncertainty'
15
15
  };
16
- const RESOLVED_OUTCOMES = new Set([
17
- 'accepted',
18
- 'conflicted',
19
- 'rejected',
20
- 'expired'
21
- ]);
16
+ const UNCERTAIN_READ = {
17
+ kind: 'transport-uncertainty'
18
+ };
22
19
  function lapsedGrant(outcome) {
23
20
  return outcome.kind === 'rejected' && outcome.code === 'upload_unavailable';
24
21
  }
@@ -28,111 +25,220 @@ function modelFailure(classified, envelope) {
28
25
  kind: 'transport-uncertainty'
29
26
  } : MODEL_ABSENT;
30
27
  }
31
- if (classified.kind === 'revoked-authorization') return {
32
- kind: 'revoked-authorization'
33
- };
34
- if (classified.kind === 'transport-uncertainty') return {
35
- kind: 'transport-uncertainty'
36
- };
37
- return MODEL_ABSENT;
38
- }
39
- function modelVersion(revisions) {
40
- return sha256Hex(JSON.stringify(revisions));
28
+ return failedModel(classified);
41
29
  }
42
30
  export function createAgentsHouseAdapter(options) {
31
+ if ('site' in options) return createSiteAdapter(options.site);
43
32
  const transport = options.transport;
44
- const notifications = houseNotifications(transport);
33
+ const notifications = houseNotifications(transport, 'credential');
45
34
  const tracked = new Map();
46
- const models = new Map();
47
- const submissions = new Map();
48
- function modelKey(locator, attached) {
49
- return `${locator} ${attached}`;
50
- }
51
- async function held(envelope, entry, modelRoot, identity, failure) {
52
- const read = envelope === null ? null : definitionsFrom(envelope, entry, modelRoot);
53
- if (read === null) return {
54
- snapshot: failure,
55
- identity: null
35
+ const handed = new Map();
36
+ const models = modelShelf();
37
+ function bootstrappedRoom(envelope, entry) {
38
+ const scope = envelope.scopes.find((candidate)=>matchesScope(candidate, entry));
39
+ if (scope === undefined) return {
40
+ kind: 'room-not-readable',
41
+ locator: entry.locator
56
42
  };
57
- const version = await modelVersion(read.revisions);
58
- return {
59
- snapshot: {
60
- kind: 'model',
61
- snapshot: {
62
- identity,
63
- version,
64
- definitions: read.definitions
65
- }
66
- },
67
- identity: {
68
- identity,
69
- version
70
- }
43
+ handed.set(entry.locator, scopeLinks(scope));
44
+ return bootstrapOf(scope, entry);
45
+ }
46
+ function enumeratedRoom(enumeration, ask) {
47
+ const view = viewOf(enumeration, ask.entry);
48
+ if (view === undefined) return {
49
+ kind: 'room-not-readable',
50
+ locator: ask.entry.locator
71
51
  };
52
+ const answer = refreshFrom(view, ask.entry, ask.fromRoomVersion);
53
+ if (answer.kind === 'net-advancement') {
54
+ handed.set(ask.entry.locator, enumeratedLinks(view, handed.get(ask.entry.locator)));
55
+ }
56
+ return answer;
57
+ }
58
+ function streamed(entry, request, from) {
59
+ const links = handed.get(entry.locator);
60
+ if (links?.roomVersion !== request.fromRoomVersion) return null;
61
+ const advanced = notifications.advance(entry, from);
62
+ if (advanced === null) return null;
63
+ if (request.minPosition !== undefined && !reachedVersion(advanced.page.roomVersion, request.minPosition, entry.logEpoch)) return null;
64
+ const relinked = streamedLinks(links, advanced);
65
+ if (relinked === null) return null;
66
+ handed.set(entry.locator, relinked);
67
+ return advanced.page;
68
+ }
69
+ function held(envelope, entry, schema, failure) {
70
+ return heldModel(envelope === null ? null : schemaFrom(envelope, entry, schema), schemaPath(entry.handle, schema), failure);
71
+ }
72
+ function modelPrefixes(entry) {
73
+ return entry.schemas.map((schema)=>schemaPath(entry.handle, schema));
72
74
  }
73
75
  async function acquireModels(entries) {
74
76
  const acquired = new Map();
75
- const privateEntry = entries.find((entry)=>entry.authority === PRIVATE_AUTHORITY);
76
- const prefixes = entries.map((entry)=>modelPrefix(entry, entry.modelRoot));
77
- if (privateEntry !== undefined) {
78
- prefixes.push(`${authorityRoot(undefined)}/${PROVIDER_SELECTION_MODEL_ROOT}`);
79
- }
80
- const answer = await transport.door(DOOR_BOOTSTRAP, {
81
- prefixes
82
- });
83
- const classified = classifyDoor(answer.status, answer.body, PRIVATE_AUTHORITY);
84
- const envelope = classified === null ? parseEnvelope(await bundled(answer.body)) : null;
85
- const failure = modelFailure(classified, envelope);
86
- for (const entry of entries){
87
- acquired.set(modelKey(entry.locator, 'collection'), await held(envelope, entry, entry.modelRoot, `${authorityRoot(entry.handle)}/${entry.modelRoot}`, failure));
88
- }
89
- if (privateEntry !== undefined) {
90
- acquired.set(modelKey(privateEntry.locator, 'typed-selection'), await held(envelope, privateEntry, PROVIDER_SELECTION_MODEL_ROOT, `${authorityRoot(undefined)}/${PROVIDER_SELECTION_MODEL_ROOT}`, failure));
91
- }
92
- return acquired;
93
- }
94
- function retainModel(key, acquired) {
95
- if (acquired === undefined) return models.get(key);
96
- if (acquired.snapshot.kind === 'transport-uncertainty') {
97
- const previous = models.get(key);
98
- if (previous !== undefined && previous.identity !== null) return previous;
77
+ for (const part of selections(entries, (entry)=>modelPrefixes(entry).length, DOOR_SELECTION_MAX)){
78
+ const privateEntry = part.find((entry)=>entry.locator === PRIVATE_LOCATOR);
79
+ const answer = await transport.door(DOOR_BOOTSTRAP, {
80
+ prefixes: part.flatMap((entry)=>modelPrefixes(entry))
81
+ });
82
+ const classified = classifyDoor(answer.status, answer.body, PRIVATE_LOCATOR);
83
+ const envelope = classified === null ? parseEnvelope(await bundled(answer.body)) : null;
84
+ const failure = modelFailure(classified, envelope);
85
+ for (const entry of part){
86
+ acquired.set(modelKey(entry.locator, 'collection'), await held(envelope, entry, entry.schema, failure));
87
+ }
88
+ if (privateEntry !== undefined) {
89
+ acquired.set(modelKey(privateEntry.locator, 'typed-selection'), await held(envelope, privateEntry, HOUSE_SCHEMA, failure));
90
+ }
99
91
  }
100
- models.set(key, acquired);
101
92
  return acquired;
102
93
  }
103
94
  function roomOf(authority, acquired) {
104
95
  const locator = locatorOf(authority);
105
96
  const key = modelKey(locator, 'collection');
106
- const collection = retainModel(key, acquired.get(key));
107
- if (authority.kind !== 'private') {
97
+ const collection = models.retain(key, acquired.get(key));
98
+ if (locator !== PRIVATE_LOCATOR) {
108
99
  return discoveredRoom(authority, collection?.identity ?? null, undefined);
109
100
  }
110
- const selection = retainModel(modelKey(locator, 'typed-selection'), acquired.get(modelKey(locator, 'typed-selection')));
101
+ const selection = models.retain(modelKey(locator, 'typed-selection'), acquired.get(modelKey(locator, 'typed-selection')));
111
102
  return discoveredRoom(authority, collection?.identity ?? null, providerSelection(selection?.identity ?? null));
112
103
  }
113
104
  function authorityFor(locator) {
114
105
  return tracked.get(locator);
115
106
  }
107
+ function withAt(result) {
108
+ if (result.kind !== 'action') return result;
109
+ const body = result.result;
110
+ if (typeof body !== 'object' || body === null) return result;
111
+ const { authority, position, log_epoch: logEpoch } = body;
112
+ if (typeof position !== 'string' || typeof logEpoch !== 'string') return result;
113
+ const locator = authority === undefined ? PRIVATE_LOCATOR : [
114
+ ...tracked.values()
115
+ ].find((entry)=>entry.authority === authority)?.locator;
116
+ if (locator === undefined) return result;
117
+ return {
118
+ ...result,
119
+ at: {
120
+ locator,
121
+ roomVersion: roomVersion({
122
+ position,
123
+ logEpoch
124
+ })
125
+ }
126
+ };
127
+ }
116
128
  async function bundled(body) {
117
129
  const grant = parseGrant(body);
118
130
  if (grant === null) return body;
119
131
  if (grant.method !== 'GET') return null;
120
132
  const answer = await transport.transfer({
121
- act: 'working_copy_bundle',
133
+ act: 'local_copy_bundle',
122
134
  method: 'GET',
123
- url: grant.url
135
+ url: grant.url,
136
+ answer: 'json'
124
137
  });
125
138
  return answer.status === 200 ? answer.body : null;
126
139
  }
127
- async function uploaded(identity, changes, delta, bytes, entry) {
140
+ function weight(entry) {
141
+ return contentPrefixes(entry).length;
142
+ }
143
+ async function bootstrapped(entries) {
144
+ const answers = [];
145
+ for (const part of selections(entries, weight, DOOR_SELECTION_MAX)){
146
+ const answer = await transport.door(DOOR_BOOTSTRAP, {
147
+ prefixes: part.flatMap((entry)=>contentPrefixes(entry))
148
+ });
149
+ const classified = part.map((entry)=>classifyDoor(answer.status, answer.body, entry.locator));
150
+ const envelope = classified[0] === null ? parseEnvelope(await bundled(answer.body)) : null;
151
+ part.forEach((entry, index)=>{
152
+ const failure = classified[index];
153
+ if (failure === null || failure === undefined) {
154
+ answers.push(envelope === null ? UNCERTAIN_READ : bootstrappedRoom(envelope, entry));
155
+ return;
156
+ }
157
+ answers.push(failure.kind === 'beyond-horizon' ? UNCERTAIN_READ : failure);
158
+ });
159
+ }
160
+ return answers;
161
+ }
162
+ function layered(asks) {
163
+ const depths = new Map();
164
+ const layers = [];
165
+ for (const ask of asks){
166
+ const depth = depths.get(ask.entry.authority) ?? 0;
167
+ depths.set(ask.entry.authority, depth + 1);
168
+ (layers[depth] ??= []).push(ask);
169
+ }
170
+ return layers;
171
+ }
172
+ async function enumerated(asks) {
173
+ const answers = new Map();
174
+ const parts = layered(asks).flatMap((layer)=>selections(layer, (ask)=>weight(ask.entry), DOOR_SELECTION_MAX));
175
+ for (const part of parts){
176
+ let open = [
177
+ ...part
178
+ ];
179
+ while(open.length > 0){
180
+ const answer = await transport.door(DOOR_ENUMERATE, {
181
+ prefixes: open.flatMap((ask)=>contentPrefixes(ask.entry)),
182
+ positions: open.map((ask)=>({
183
+ ...presented(ask.entry.authority, ask.from),
184
+ ...ask.minPosition === undefined ? {} : {
185
+ min_position: ask.minPosition
186
+ }
187
+ }))
188
+ });
189
+ const refusal = doorRefusal(answer.body);
190
+ if (refusal?.code === 'replica_behind') {
191
+ const named = open.filter((ask)=>ask.entry.authority === refusal.fields.authority);
192
+ const behind = named.length === 0 ? open : named;
193
+ for (const ask of behind)answers.set(ask, {
194
+ kind: 'retryable-refusal',
195
+ ...refusal
196
+ });
197
+ open = open.filter((ask)=>!behind.includes(ask));
198
+ continue;
199
+ }
200
+ const classified = open.map((ask)=>classifyDoor(answer.status, answer.body, ask.entry.locator));
201
+ if (classified[0]?.kind === 'beyond-horizon') {
202
+ const named = doorRefusal(answer.body)?.fields.authority;
203
+ const expired = open.filter((ask)=>ask.entry.authority === named);
204
+ const answered = expired.length === 0 ? UNCERTAIN_READ : {
205
+ kind: 'beyond-horizon'
206
+ };
207
+ const gone = expired.length === 0 ? open : expired;
208
+ for (const ask of gone)answers.set(ask, answered);
209
+ open = open.filter((ask)=>!gone.includes(ask));
210
+ continue;
211
+ }
212
+ const enumeration = classified[0] === null ? parseEnumeration(answer.body, 'credential') : null;
213
+ open.forEach((ask, index)=>{
214
+ const failure = classified[index];
215
+ if (failure !== null && failure !== undefined) {
216
+ answers.set(ask, failure);
217
+ return;
218
+ }
219
+ answers.set(ask, enumeration === null ? UNCERTAIN_READ : enumeratedRoom(enumeration, ask));
220
+ });
221
+ open = [];
222
+ }
223
+ }
224
+ return asks.map((ask)=>answers.get(ask) ?? UNCERTAIN_READ);
225
+ }
226
+ const bootstrapTogether = gathered(bootstrapped);
227
+ const enumerateTogether = gathered(enumerated);
228
+ async function uploaded(head, changes, delta, bytes, entry, handed, keep) {
128
229
  const paths = batchPaths(changes);
129
230
  const sha256 = await deltaDigest(delta);
130
- const retained = submissions.get(identity);
131
- if (retained !== undefined && retained.sha256 === sha256) return {
132
- body: retained.body
133
- };
231
+ if (handed !== undefined && handed.sha256 === sha256) {
232
+ return {
233
+ body: {
234
+ ...head,
235
+ paths,
236
+ upload: handed
237
+ }
238
+ };
239
+ }
134
240
  const minted = await transport.door(DOOR_BATCH_UPLOAD, {
135
- operation_id: identity,
241
+ ...head,
136
242
  paths,
137
243
  bytes,
138
244
  sha256
@@ -150,37 +256,35 @@ export function createAgentsHouseAdapter(options) {
150
256
  act: 'authored_batch_upload',
151
257
  method: 'POST',
152
258
  url: grant.url,
259
+ answer: 'json',
153
260
  body: `${grant.operation}\n${delta}`
154
261
  });
155
262
  if (delivered.status !== 200) return {
156
263
  outcome: UNCERTAIN
157
264
  };
158
265
  const holding = parseGrant(delivered.body);
159
- if (holding === null) return {
266
+ if (holding === null || holding.method !== 'POST') return {
160
267
  outcome: UNCERTAIN
161
268
  };
162
- const body = {
163
- operation_id: identity,
164
- paths,
165
- upload: {
166
- url: holding.url,
167
- operation: holding.operation,
168
- bytes,
169
- sha256
170
- }
269
+ const upload = {
270
+ url: holding.url,
271
+ operation: holding.operation,
272
+ bytes,
273
+ sha256
171
274
  };
172
- submissions.set(identity, {
173
- sha256,
174
- body
175
- });
275
+ await keep(upload);
176
276
  return {
177
- body
277
+ body: {
278
+ ...head,
279
+ paths,
280
+ upload
281
+ }
178
282
  };
179
283
  }
180
- async function submit(identity, changes, delta, bytes, entry) {
181
- const prepared = uploadable(bytes) ? await uploaded(identity, changes, delta, bytes, entry) : {
284
+ async function submit(head, changes, delta, bytes, entry, handed, keep) {
285
+ const prepared = uploadable(bytes) ? await uploaded(head, changes, delta, bytes, entry, handed, keep) : {
182
286
  body: {
183
- operation_id: identity,
287
+ ...head,
184
288
  changes
185
289
  }
186
290
  };
@@ -193,7 +297,7 @@ export function createAgentsHouseAdapter(options) {
193
297
  return options.authorize();
194
298
  },
195
299
  async model (_authorization, request) {
196
- return models.get(modelKey(request.locator, request.attached))?.snapshot ?? MODEL_ABSENT;
300
+ return models.answer(request);
197
301
  },
198
302
  async discover (_authorization) {
199
303
  const answer = await transport.door(DOOR_DISCOVER, {
@@ -201,20 +305,8 @@ export function createAgentsHouseAdapter(options) {
201
305
  ...DISCOVER_PREFIXES
202
306
  ]
203
307
  });
204
- const classified = classifyDoor(answer.status, answer.body, PRIVATE_AUTHORITY);
205
- if (classified !== null) {
206
- if (classified.kind === 'revoked-authorization') return classified;
207
- if (classified.kind === 'scoped-refusal') {
208
- return {
209
- kind: 'contract-unavailable',
210
- code: classified.code,
211
- fields: classified.fields
212
- };
213
- }
214
- return {
215
- kind: 'transport-uncertainty'
216
- };
217
- }
308
+ const classified = classifyDoor(answer.status, answer.body, PRIVATE_LOCATOR);
309
+ if (classified !== null) return discoveryFailure(classified);
218
310
  const document = parseDiscovery(answer.body);
219
311
  if (document === null) return {
220
312
  kind: 'transport-uncertainty'
@@ -222,14 +314,11 @@ export function createAgentsHouseAdapter(options) {
222
314
  const entries = trackedFrom(document);
223
315
  tracked.clear();
224
316
  for (const entry of entries)tracked.set(entry.locator, entry);
317
+ for (const locator of handed.keys())if (!tracked.has(locator)) handed.delete(locator);
225
318
  notifications.track(entries);
226
319
  const acquired = await acquireModels(entries);
227
- for (const key of [
228
- ...models.keys()
229
- ]){
230
- if (!entries.some((entry)=>key.startsWith(`${entry.locator} `))) models.delete(key);
231
- }
232
- return mapDiscovery(document, document.authorities.map((authority)=>roomOf(authority, acquired)));
320
+ models.keep(entries.map((entry)=>entry.locator));
321
+ return mapDiscovery(document, document.authorities.map((authority)=>roomOf(authority, acquired)), capabilities(document.actions));
233
322
  },
234
323
  async bootstrap (_authorization, request) {
235
324
  const entry = authorityFor(request.locator);
@@ -240,22 +329,7 @@ export function createAgentsHouseAdapter(options) {
240
329
  if (request.pageToken !== undefined) return {
241
330
  kind: 'transport-uncertainty'
242
331
  };
243
- const answer = await transport.door(DOOR_BOOTSTRAP, {
244
- prefixes: [
245
- ...contentPrefixes(entry)
246
- ]
247
- });
248
- const classified = classifyDoor(answer.status, answer.body, entry.locator);
249
- if (classified !== null) {
250
- return classified.kind === 'beyond-horizon' ? {
251
- kind: 'transport-uncertainty'
252
- } : classified;
253
- }
254
- const envelope = parseEnvelope(await bundled(answer.body));
255
- if (envelope === null) return {
256
- kind: 'transport-uncertainty'
257
- };
258
- return bootstrapFrom(envelope, entry);
332
+ return bootstrapTogether(entry);
259
333
  },
260
334
  async refresh (_authorization, request) {
261
335
  const entry = authorityFor(request.locator);
@@ -266,28 +340,23 @@ export function createAgentsHouseAdapter(options) {
266
340
  if (request.pageToken !== undefined) return {
267
341
  kind: 'transport-uncertainty'
268
342
  };
269
- const streamed = notifications.advance(entry, request.fromRoomVersion);
270
- if (streamed !== null) return streamed;
271
- const answer = await transport.door(DOOR_ENUMERATE, {
272
- prefixes: [
273
- ...contentPrefixes(entry)
274
- ],
275
- positions: [
276
- {
277
- authority: entry.authority,
278
- position: request.fromRoomVersion
279
- }
280
- ]
281
- });
282
- const classified = classifyDoor(answer.status, answer.body, entry.locator);
283
- if (classified !== null) return classified;
284
- const enumeration = parseEnumeration(answer.body);
285
- if (enumeration === null) return {
286
- kind: 'transport-uncertainty'
343
+ const from = logPosition(request.fromRoomVersion);
344
+ if (from === null) return {
345
+ kind: 'beyond-horizon'
287
346
  };
288
- return refreshFrom(enumeration, entry, request.fromRoomVersion);
347
+ const answered = streamed(entry, request, from);
348
+ if (answered !== null) return answered;
349
+ const minPosition = request.minPosition === undefined ? null : logPosition(request.minPosition);
350
+ return enumerateTogether({
351
+ entry,
352
+ from,
353
+ fromRoomVersion: request.fromRoomVersion,
354
+ ...minPosition?.logEpoch === from.logEpoch ? {
355
+ minPosition: minPosition.position
356
+ } : {}
357
+ });
289
358
  },
290
- async mutate (_authorization, request) {
359
+ async mutate (_authorization, request, keep) {
291
360
  const entry = authorityFor(request.locator);
292
361
  if (entry === undefined) return {
293
362
  kind: 'room-not-readable',
@@ -295,17 +364,13 @@ export function createAgentsHouseAdapter(options) {
295
364
  };
296
365
  const mapped = houseChanges(request.changeBatch, entry.handle);
297
366
  if (mapped.kind === 'unmapped') return rejectedMapping(mapped);
298
- const identity = request.changeBatch.id;
367
+ const head = operationHead(request.changeBatch);
299
368
  const delta = deltaBody(mapped.changes);
300
369
  const bytes = deltaBytes(delta);
301
- const resent = submissions.has(identity);
302
- let outcome = await submit(identity, mapped.changes, delta, bytes, entry);
303
- if (resent && lapsedGrant(outcome)) {
304
- submissions.delete(identity);
305
- outcome = await submit(identity, mapped.changes, delta, bytes, entry);
306
- }
307
- if (RESOLVED_OUTCOMES.has(outcome.kind)) submissions.delete(identity);
308
- return outcome;
370
+ const handed = request.upload;
371
+ const outcome = await submit(head, mapped.changes, delta, bytes, entry, handed, keep);
372
+ if (handed === undefined || !lapsedGrant(outcome)) return outcome;
373
+ return submit(head, mapped.changes, delta, bytes, entry, undefined, keep);
309
374
  },
310
375
  async history (_authorization, request) {
311
376
  const entry = authorityFor(request.locator);
@@ -330,11 +395,15 @@ export function createAgentsHouseAdapter(options) {
330
395
  }
331
396
  });
332
397
  },
398
+ reached (locator, version, target) {
399
+ return reachedVersion(version, target, authorityFor(locator)?.logEpoch);
400
+ },
333
401
  async action (_authorization, request) {
334
- return houseAction(transport, request);
402
+ return withAt(await houseAction(transport, request));
335
403
  },
336
404
  roomVersions: notifications,
337
- conversations: notifications.conversations
405
+ conversations: notifications.conversations,
406
+ serviceMode: notifications.serviceMode
338
407
  };
339
408
  }
340
409