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

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 (57) hide show
  1. package/README.md +2 -2
  2. package/dist/actions.d.ts +120 -27
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +8 -99
  5. package/dist/adapter.d.ts.map +1 -1
  6. package/dist/adapter.js +85 -135
  7. package/dist/app-reference.d.ts +2 -0
  8. package/dist/app-reference.d.ts.map +1 -0
  9. package/dist/app-reference.js +2 -0
  10. package/dist/crockford.d.ts +4 -0
  11. package/dist/crockford.d.ts.map +1 -0
  12. package/dist/crockford.js +3 -0
  13. package/dist/discovery.d.ts +5 -10
  14. package/dist/discovery.d.ts.map +1 -1
  15. package/dist/discovery.js +18 -32
  16. package/dist/gather.d.ts +0 -1
  17. package/dist/gather.d.ts.map +1 -1
  18. package/dist/gather.js +1 -1
  19. package/dist/house-model.d.ts +2938 -0
  20. package/dist/house-model.d.ts.map +1 -0
  21. package/dist/house-model.js +1614 -0
  22. package/dist/index.d.ts +7 -3
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +6 -2
  25. package/dist/model.d.ts +5 -1
  26. package/dist/model.d.ts.map +1 -1
  27. package/dist/model.js +11 -13
  28. package/dist/part.d.ts +12 -11
  29. package/dist/part.d.ts.map +1 -1
  30. package/dist/part.js +66 -56
  31. package/dist/read.d.ts +15 -13
  32. package/dist/read.d.ts.map +1 -1
  33. package/dist/read.js +114 -127
  34. package/dist/room-stream.d.ts +19 -0
  35. package/dist/room-stream.d.ts.map +1 -0
  36. package/dist/room-stream.js +57 -0
  37. package/dist/room-types.d.ts +2 -1
  38. package/dist/room-types.d.ts.map +1 -1
  39. package/dist/room-types.js +62 -2
  40. package/dist/site.d.ts.map +1 -1
  41. package/dist/site.js +40 -97
  42. package/dist/stream.d.ts +3 -6
  43. package/dist/stream.d.ts.map +1 -1
  44. package/dist/stream.js +38 -288
  45. package/dist/transport.d.ts +12 -21
  46. package/dist/transport.d.ts.map +1 -1
  47. package/dist/transport.js +1 -1
  48. package/dist/upload.d.ts.map +1 -1
  49. package/dist/upload.js +25 -14
  50. package/dist/write.d.ts +4 -0
  51. package/dist/write.d.ts.map +1 -1
  52. package/dist/write.js +12 -15
  53. package/package.json +8 -4
  54. package/AGENTS.md +0 -319
  55. package/dist/referrer.d.ts +0 -9
  56. package/dist/referrer.d.ts.map +0 -1
  57. package/dist/referrer.js +0 -41
package/dist/site.js CHANGED
@@ -1,14 +1,13 @@
1
1
  import { stepped } from '@agentshouse/mdruntime';
2
2
  import { actionOutcome, SITE_SUBMIT } from './actions.js';
3
3
  import { mapDiscovery, parseSiteDiscovery, siteCapabilities, siteTracked, } from './discovery.js';
4
- import { partModels, streamedModels, viewModels } from './model.js';
5
- import { advancedScope, bootstrapping, emptyScope, housePart, parseEnumeration, parseSiteParted, refreshFrom, viewOf, wholeView, } from './read.js';
6
- import { PART_ATTEMPTS, unverifiedPart } from './part.js';
4
+ import { streamedModels, viewModels } from './model.js';
5
+ import { bootstrapping, parseEnumeration, refreshFrom, viewOf, wholeView, } from './read.js';
6
+ import { roomStreams } from './room-stream.js';
7
7
  import { classifyDoor, classifyRead, discoveryFailure } from './refusal.js';
8
- import { HOUSE_ROOM_TYPES } from './room-types.js';
8
+ import { HOUSE_NAMEABLE_ROOM_TYPES, HOUSE_ROOM_TYPES } from './room-types.js';
9
9
  import { houseNotifications } from './stream.js';
10
- import { SITE_DOOR_BOOTSTRAP, SITE_DOOR_DISCOVER, SITE_DOOR_ENUMERATE, SITE_DOOR_SUBMIT, SITE_FILE_RECORD, SITE_FILES, } from './transport.js';
11
- import { houseReferrer } from './referrer.js';
10
+ import { SITE_DOOR_BOOTSTRAP, SITE_DOOR_DISCOVER, SITE_DOOR_ENUMERATE, SITE_DOOR_SUBMIT, SITE_FILES, } from './transport.js';
12
11
  import { followsVersion, logPosition, presented, reachedVersion, roomVersion, } from './version.js';
13
12
  const UNCERTAIN = { kind: 'transport-uncertainty' };
14
13
  function unfound() {
@@ -25,28 +24,16 @@ function siteRoom(tracked) {
25
24
  providerOwnedPaths: [],
26
25
  };
27
26
  }
28
- function settled(refused) {
29
- return refused.kind === 'beyond-horizon' ? UNCERTAIN : refused;
30
- }
31
27
  export function createSiteAdapter(transport, snapshot = false) {
32
28
  const notifications = houseNotifications(transport, 'site');
33
29
  const tracked = new Map();
34
- const expired = new Map();
35
- function published(referrer) {
36
- const named = houseReferrer(referrer, tracked);
37
- return named === null || !tracked.has(named.locator) ? null : named;
38
- }
30
+ const read = new Map();
31
+ const rooms = roomStreams('site');
39
32
  function fileRoute(request) {
40
- const named = published(request.kind === 'path' ? { room: request.locator, path: request.path } : request.referrer);
41
- if (named === null) {
42
- return { kind: 'rejected', code: 'referrer_unknown', fields: {}, retryable: false };
43
- }
44
- if (request.fileRef === undefined)
45
- return unfound();
46
- const { room_ref, path, record } = named.referrer;
47
- return `${SITE_FILES}${room_ref}/${encodeURIComponent(request.fileRef)}/${path === undefined
48
- ? `${SITE_FILE_RECORD}/${encodeURIComponent(record)}`
49
- : encodeURIComponent(path)}`;
33
+ const entry = tracked.get(request.locator);
34
+ if (entry === undefined)
35
+ return { kind: 'room-not-readable', locator: request.locator };
36
+ return `${SITE_FILES}${entry.authority}/${encodeURIComponent(request.revision)}/${encodeURIComponent(request.path)}`;
50
37
  }
51
38
  async function enumerated(entry, from, page) {
52
39
  const answer = await transport.door(SITE_DOOR_ENUMERATE, {
@@ -71,42 +58,29 @@ export function createSiteAdapter(transport, snapshot = false) {
71
58
  const first = await enumerated(entry, from);
72
59
  return 'kind' in first ? first : wholeView(first, (page) => enumerated(entry, from, page));
73
60
  }
74
- async function partedRoom(entry) {
75
- const cut = expired.get(entry.locator);
76
- const answer = await transport.read(`${SITE_DOOR_BOOTSTRAP}${entry.authority}${cut === undefined ? '' : `?expired=${cut}`}`);
77
- const classified = classifyDoor(answer.status, answer.body, entry.locator);
78
- if (classified !== null)
79
- return classified;
80
- const room = parseSiteParted(answer.body);
81
- return room === null || room.anchor !== entry.authority ? UNCERTAIN : room;
82
- }
83
- async function fetchedPart(entry, room, listed) {
84
- for (let attempt = 0; attempt < PART_ATTEMPTS; attempt += 1) {
85
- const answer = await transport.read(`${SITE_DOOR_BOOTSTRAP}${entry.authority}?part=${encodeURIComponent(listed.part)}`);
86
- const failure = classifyRead(answer.status, answer.body, entry.locator);
87
- if (failure?.kind === 'beyond-horizon')
88
- expired.set(entry.locator, room.position);
89
- if (failure !== null)
90
- return failure;
91
- const scope = await housePart(room, answer.body, listed, 'site');
92
- if (scope !== null)
93
- return scope;
94
- }
95
- return unverifiedPart(entry.locator);
61
+ async function openedRoom(entry, arriving, signal) {
62
+ const cut = read.get(entry.locator);
63
+ const answered = await transport.streamed(`${SITE_DOOR_BOOTSTRAP}${entry.authority}${cut === undefined ? '' : `?expired=${cut}`}`, signal ?? new AbortController().signal);
64
+ if (answered.room === null)
65
+ return classifyDoor(answered.status, answered.body, entry.locator) ?? UNCERTAIN;
66
+ const head = await rooms.head(entry, answered.room, arriving, signal);
67
+ const at = head.kind === 'bootstrap' ? logPosition(head.roomVersion) : null;
68
+ if (at !== null)
69
+ read.set(entry.locator, at.position);
70
+ return head;
96
71
  }
97
- async function caughtUp(entry, continued) {
98
- const listed = continued.room.parts[continued.next];
99
- const scope = listed === undefined
100
- ? emptyScope(continued.room)
101
- : await fetchedPart(entry, continued.room, listed);
102
- if ('kind' in scope)
103
- return scope;
104
- if (continued.room.parts.length > 1 || roomVersion(scope) === roomVersion(entry))
105
- return scope;
106
- const view = await enumeration(entry, scope);
107
- if ('kind' in view)
108
- return settled(view);
109
- return stepped(advancedScope(scope, view));
72
+ async function streamedPage(entry, token) {
73
+ const part = await rooms.part(entry, token);
74
+ if ('kind' in part)
75
+ return part;
76
+ notifications.reach(entry.authority, part.scope);
77
+ const { folders: _folders, ...page } = await stepped(bootstrapping(part.scope));
78
+ return {
79
+ ...page,
80
+ ...(part.models === undefined ? {} : { models: part.models }),
81
+ ...(part.pageToken === undefined ? {} : { pageToken: part.pageToken }),
82
+ bytes: part.bytes,
83
+ };
110
84
  }
111
85
  async function refreshed(entry, fromRoomVersion) {
112
86
  const from = logPosition(fromRoomVersion);
@@ -131,6 +105,7 @@ export function createSiteAdapter(transport, snapshot = false) {
131
105
  localSaves: true,
132
106
  exhaustiveDiscovery: true,
133
107
  roomTypes: HOUSE_ROOM_TYPES,
108
+ nameableRoomTypes: HOUSE_NAMEABLE_ROOM_TYPES,
134
109
  async authorize() {
135
110
  return {};
136
111
  },
@@ -153,32 +128,9 @@ export function createSiteAdapter(transport, snapshot = false) {
153
128
  const entry = tracked.get(request.locator);
154
129
  if (entry === undefined)
155
130
  return { kind: 'room-not-readable', locator: request.locator };
156
- let continued;
157
- if (request.pageToken === undefined) {
158
- const room = await partedRoom(entry);
159
- if ('kind' in room)
160
- return room;
161
- continued = { room, next: 0, seen: [] };
162
- }
163
- else
164
- continued = JSON.parse(request.pageToken);
165
- const scope = await caughtUp(entry, continued);
166
- if ('kind' in scope)
167
- return scope;
168
- notifications.reach(entry.authority, scope);
169
- const next = continued.next + 1;
170
- const more = next < continued.room.parts.length;
171
- const seen = [
172
- ...continued.seen,
173
- ...entry.schemas.filter((schema) => scope.files.some((file) => file.path === schema)),
174
- ];
175
- const models = await partModels(scope, entry, new Set(seen), !more);
176
- const { folders: _folders, ...page } = await stepped(bootstrapping(scope));
177
- return {
178
- ...page,
179
- ...(models === undefined ? {} : { models }),
180
- ...(more ? { pageToken: JSON.stringify({ ...continued, next, seen }) } : {}),
181
- };
131
+ return request.pageToken === undefined
132
+ ? openedRoom(entry, request.arriving, request.signal)
133
+ : streamedPage(entry, request.pageToken);
182
134
  },
183
135
  async refresh(_authorization, request) {
184
136
  const entry = tracked.get(request.locator);
@@ -196,6 +148,9 @@ export function createSiteAdapter(transport, snapshot = false) {
196
148
  async mutate() {
197
149
  return { kind: 'rejected', affected: {}, code: 'write-grant', fields: {}, retryable: false };
198
150
  },
151
+ async attach() {
152
+ return { kind: 'rejected', code: 'write-grant', fields: {}, retryable: false };
153
+ },
199
154
  async history() {
200
155
  return { kind: 'no-history' };
201
156
  },
@@ -220,13 +175,6 @@ export function createSiteAdapter(transport, snapshot = false) {
220
175
  name: request.path.slice(request.path.lastIndexOf('/') + 1),
221
176
  };
222
177
  },
223
- referrerScope(referrer) {
224
- const named = published(referrer);
225
- if (named === null)
226
- return null;
227
- const { path } = named.referrer;
228
- return { locator: named.locator, ...(path === undefined ? {} : { path }) };
229
- },
230
178
  reached(locator, version, target) {
231
179
  return reachedVersion(version, target, tracked.get(locator)?.logEpoch);
232
180
  },
@@ -261,11 +209,6 @@ export function createSiteAdapter(transport, snapshot = false) {
261
209
  },
262
210
  reopen: notifications.reopen,
263
211
  observe: notifications.observe,
264
- published(locator, roomVersion) {
265
- const at = logPosition(roomVersion);
266
- if (at !== null)
267
- notifications.published(locator, at);
268
- },
269
212
  },
270
213
  serviceMode: notifications.serviceMode,
271
214
  };
package/dist/stream.d.ts CHANGED
@@ -1,22 +1,19 @@
1
- import { type ConversationStream, type RefreshPage, type RoomStreamEvent, type ServiceModeSignal, type StreamReceipts } from '@agentshouse/mdruntime';
1
+ import { type ConversationStream, type RefreshPage, type RoomStreamEvent, type ServiceModeSignal } from '@agentshouse/mdruntime';
2
2
  import type { TrackedAuthority } from './discovery.js';
3
3
  import type { HouseDoor, HouseHeldRoom, HouseMode, HouseStreamEntry } from './transport.js';
4
4
  import { type LogPosition } from './version.js';
5
- export type HeldRevision = (path: string, revision: string) => boolean;
6
5
  export type StreamedAdvancement = {
7
6
  page: RefreshPage;
8
7
  schemas: ReadonlyMap<string, HouseStreamEntry>;
9
8
  };
10
9
  export type HouseNotifications = {
11
10
  runtime: string;
12
- observe(notify: (event: RoomStreamEvent) => void, receipts: StreamReceipts): () => void;
11
+ observe(notify: (event: RoomStreamEvent) => void): () => void;
13
12
  send(rooms: readonly HouseHeldRoom[]): void;
14
- published(locator: string, at: LogPosition): void;
15
13
  reopen(): void;
16
14
  track(authorities: readonly TrackedAuthority[]): void;
17
15
  reach(authority: string, at: LogPosition): void;
18
- edited(tracked: TrackedAuthority, from: LogPosition): readonly string[];
19
- advance(tracked: TrackedAuthority, from: LogPosition, holds?: HeldRevision): Generator<number, StreamedAdvancement | null, void>;
16
+ advance(tracked: TrackedAuthority, from: LogPosition): Generator<number, StreamedAdvancement | null, void>;
20
17
  conversations: ConversationStream;
21
18
  serviceMode: ServiceModeSignal;
22
19
  };
@@ -1 +1 @@
1
- {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,kBAAkB,EAEvB,KAAK,WAAW,EAChB,KAAK,eAAe,EAEpB,KAAK,iBAAiB,EACtB,KAAK,cAAc,EAEpB,MAAM,wBAAwB,CAAC;AAGhC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAUvD,OAAO,KAAK,EACV,SAAS,EACT,aAAa,EAEb,SAAS,EAET,gBAAgB,EAEjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAe,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AA0C7D,MAAM,MAAM,YAAY,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC;AAEvE,MAAM,MAAM,mBAAmB,GAAG;IAChC,IAAI,EAAE,WAAW,CAAC;IAClB,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;CAChD,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,EAAE,QAAQ,EAAE,cAAc,GAAG,MAAM,IAAI,CAAC;IACxF,IAAI,CAAC,KAAK,EAAE,SAAS,aAAa,EAAE,GAAG,IAAI,CAAC;IAC5C,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,WAAW,GAAG,IAAI,CAAC;IAClD,MAAM,IAAI,IAAI,CAAC;IACf,KAAK,CAAC,WAAW,EAAE,SAAS,gBAAgB,EAAE,GAAG,IAAI,CAAC;IACtD,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,WAAW,GAAG,IAAI,CAAC;IAChD,MAAM,CAAC,OAAO,EAAE,gBAAgB,EAAE,IAAI,EAAE,WAAW,GAAG,SAAS,MAAM,EAAE,CAAC;IACxE,OAAO,CACL,OAAO,EAAE,gBAAgB,EACzB,IAAI,EAAE,WAAW,EACjB,KAAK,CAAC,EAAE,YAAY,GACnB,SAAS,CAAC,MAAM,EAAE,mBAAmB,GAAG,IAAI,EAAE,IAAI,CAAC,CAAC;IACvD,aAAa,EAAE,kBAAkB,CAAC;IAClC,WAAW,EAAE,iBAAiB,CAAC;CAChC,CAAC;AAmFF,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,GAAG,kBAAkB,CA8jB5F"}
1
+ {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,kBAAkB,EAEvB,KAAK,WAAW,EAChB,KAAK,eAAe,EAEpB,KAAK,iBAAiB,EAEvB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAUvD,OAAO,KAAK,EACV,SAAS,EACT,aAAa,EAEb,SAAS,EAET,gBAAgB,EAEjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAe,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AA0B7D,MAAM,MAAM,mBAAmB,GAAG;IAChC,IAAI,EAAE,WAAW,CAAC;IAClB,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC;CAChD,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;IAC9D,IAAI,CAAC,KAAK,EAAE,SAAS,aAAa,EAAE,GAAG,IAAI,CAAC;IAC5C,MAAM,IAAI,IAAI,CAAC;IACf,KAAK,CAAC,WAAW,EAAE,SAAS,gBAAgB,EAAE,GAAG,IAAI,CAAC;IACtD,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,WAAW,GAAG,IAAI,CAAC;IAChD,OAAO,CACL,OAAO,EAAE,gBAAgB,EACzB,IAAI,EAAE,WAAW,GAChB,SAAS,CAAC,MAAM,EAAE,mBAAmB,GAAG,IAAI,EAAE,IAAI,CAAC,CAAC;IACvD,aAAa,EAAE,kBAAkB,CAAC;IAClC,WAAW,EAAE,iBAAiB,CAAC;CAChC,CAAC;AAyBF,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,GAAG,kBAAkB,CA+W5F"}
package/dist/stream.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { ROW_WEIGHT, } from '@agentshouse/mdruntime';
2
- import { acceptedMessage } from './actions.js';
3
- import { sha256Hex } from './digest.js';
2
+ import { acceptedMessage, cursorPosition } from './actions.js';
4
3
  import { answered, answering, binaryFileOf, folded, forgotten, headChange, revised, } from './read.js';
5
4
  import { roomVersion } from './version.js';
6
5
  const HELD_FRAMES_MAX = 256;
@@ -17,6 +16,7 @@ function listedConversation(listed) {
17
16
  finished: listed.finished_at,
18
17
  outcome: listed.outcome,
19
18
  waiting: listed.waiting,
19
+ newest: listed.newest === null ? null : cursorPosition(listed.newest),
20
20
  };
21
21
  }
22
22
  function position(value) {
@@ -24,78 +24,11 @@ function position(value) {
24
24
  return null;
25
25
  return BigInt(value);
26
26
  }
27
- function assembled(entries) {
28
- const whole = [];
29
- let open = null;
30
- for (const entry of entries) {
31
- if (entry.kind === 'piece') {
32
- if (open === null || open.entry.path !== entry.path || entry.text === undefined)
33
- return null;
34
- open.texts.push(entry.text);
35
- }
36
- else {
37
- if (open !== null)
38
- return null;
39
- if (entry.continued !== true) {
40
- whole.push(entry);
41
- continue;
42
- }
43
- if (entry.text === undefined)
44
- return null;
45
- open = { entry, texts: [entry.text] };
46
- }
47
- if (entry.continued === true)
48
- continue;
49
- const { continued: _continued, ...head } = open.entry;
50
- whole.push({ ...head, text: open.texts.join('') });
51
- open = null;
52
- }
53
- return open === null ? whole : null;
54
- }
55
- async function verified(entries) {
56
- try {
57
- for (const entry of entries) {
58
- if (entry.text === undefined || entry.sha256 === undefined)
59
- continue;
60
- if ((await sha256Hex(entry.text)) !== entry.sha256)
61
- return false;
62
- }
63
- return true;
64
- }
65
- catch {
66
- return false;
67
- }
68
- }
69
- function resumed(kept, frame) {
70
- const last = frame.entries.at(-1);
71
- const point = { parts: frame.index + 1, from: frame.from, target: frame.target };
72
- if (last?.continued !== true)
73
- return { ...point, handled: frame.handled, piece: null };
74
- const text = last.text?.length ?? 0;
75
- if (last.kind !== 'piece') {
76
- return last.sha256 === undefined
77
- ? kept.resume
78
- : { ...point, handled: frame.handled, piece: { sha256: last.sha256, offset: text } };
79
- }
80
- const piece = kept.resume?.piece ?? null;
81
- if (piece === null)
82
- return kept.resume;
83
- const pieces = frame.entries.reduce((sum, entry) => sum + (entry.kind === 'piece' ? (entry.text?.length ?? 0) : 0), 0);
84
- return {
85
- ...point,
86
- handled: frame.handled,
87
- piece: { sha256: piece.sha256, offset: piece.offset + pieces },
88
- };
89
- }
90
27
  export function houseNotifications(transport, mode) {
91
28
  const stream = transport.stream;
92
29
  const lines = new Map();
93
- const kept = new Map();
94
- let receipts = null;
95
- let loaded = Promise.resolve();
30
+ const staged = new Map();
96
31
  let acknowledging = null;
97
- let receiving = Promise.resolve();
98
- let verifying = 0;
99
32
  const locators = new Map();
100
33
  const observers = new Set();
101
34
  const conversationObservers = new Set();
@@ -125,6 +58,7 @@ export function houseNotifications(transport, mode) {
125
58
  function reopened(next) {
126
59
  opening = next;
127
60
  acknowledging = next;
61
+ staged.clear();
128
62
  const gapped = new Set([...replicated, ...live]);
129
63
  live.clear();
130
64
  for (const conversation of gapped)
@@ -198,160 +132,36 @@ export function houseNotifications(transport, mode) {
198
132
  if (line !== undefined)
199
133
  lines.set(authority, { ...line, frames: [] });
200
134
  }
201
- async function forget(carries) {
202
- const held = carries.filter((carry) => kept.delete(carry));
203
- if (held.length > 0)
204
- await receipts?.drop(held).catch(() => { });
205
- }
206
- async function lost(authority, carry) {
135
+ function lost(authority, carry) {
136
+ staged.delete(carry);
207
137
  interrupt(authority);
208
- await forget([carry]);
209
138
  }
210
- async function stage(frame, via) {
211
- await loaded;
212
- const port = receipts;
213
- if (port === null)
214
- return;
215
- const held = kept.get(frame.carry);
216
- if (held === undefined && frame.index !== 0) {
217
- interrupt(frame.authority);
218
- return;
219
- }
220
- if (held !== undefined &&
221
- (held.fenced !== null || frame.index > held.parts || frame.index < (held.resume?.parts ?? 0))) {
222
- await lost(frame.authority, frame.carry);
139
+ function stage(frame, via) {
140
+ const held = staged.get(frame.carry);
141
+ if (frame.index !== (held?.parts ?? 0)) {
142
+ lost(frame.authority, frame.carry);
223
143
  return;
224
144
  }
225
145
  if (held === undefined) {
226
- await forget([...kept.values()]
227
- .filter((other) => other.authority === frame.authority && other.fenced === null)
228
- .map((other) => other.carry));
229
- }
230
- const start = held ?? {
231
- authority: frame.authority,
232
- carry: frame.carry,
233
- logEpoch: lines.get(frame.authority)?.logEpoch ?? null,
234
- after: frame.after,
235
- parts: 0,
236
- resume: null,
237
- fenced: null,
238
- };
239
- const next = { ...start, parts: frame.index + 1, resume: resumed(start, frame) };
240
- const part = { entries: frame.entries };
241
- try {
242
- await port.keep(frame.carry, next, { index: frame.index, value: part });
243
- }
244
- catch {
245
- await lost(frame.authority, frame.carry);
246
- return;
146
+ for (const [carry, other] of staged) {
147
+ if (other.authority === frame.authority)
148
+ staged.delete(carry);
149
+ }
247
150
  }
248
- kept.set(frame.carry, next);
151
+ const next = held ?? { authority: frame.authority, parts: 0, entries: [] };
152
+ next.entries.push(...frame.entries);
153
+ next.parts += 1;
154
+ staged.set(frame.carry, next);
249
155
  via?.received(frame.authority, frame.carry, frame.index);
250
156
  }
251
- async function keptEntries(carry, parts) {
252
- if (parts === 0)
253
- return [];
254
- if (receipts === null)
255
- return null;
256
- const read = (await receipts.parts(carry, parts));
257
- if (read.length !== parts)
258
- return null;
259
- const entries = assembled(read.flatMap((part) => part.entries));
260
- return entries !== null && (await verified(entries)) ? entries : null;
261
- }
262
- async function carried(frame) {
263
- await loaded;
264
- const held = kept.get(frame.carry);
265
- if (frame.parts > 0 && held?.parts !== frame.parts) {
266
- await lost(frame.authority, frame.carry);
267
- return;
268
- }
269
- const entries = await keptEntries(frame.carry, frame.parts).catch(() => null);
270
- if (entries === null) {
271
- await lost(frame.authority, frame.carry);
272
- return;
273
- }
274
- const fenced = {
275
- ...(held ?? {
276
- authority: frame.authority,
277
- carry: frame.carry,
278
- logEpoch: frame.log_epoch,
279
- after: frame.after,
280
- parts: 0,
281
- resume: null,
282
- }),
283
- after: frame.after,
284
- fenced: { position: frame.position, logEpoch: frame.log_epoch },
285
- };
286
- try {
287
- await receipts?.keep(frame.carry, fenced);
288
- }
289
- catch {
290
- await lost(frame.authority, frame.carry);
157
+ function carried(frame) {
158
+ const held = staged.get(frame.carry);
159
+ staged.delete(frame.carry);
160
+ if ((held?.parts ?? 0) !== frame.parts) {
161
+ interrupt(frame.authority);
291
162
  return;
292
163
  }
293
- kept.set(frame.carry, fenced);
294
- publish(frame, entries);
295
- }
296
- async function load(port) {
297
- try {
298
- for (const { key, head } of await port.heads())
299
- kept.set(key, head);
300
- }
301
- catch {
302
- kept.clear();
303
- }
304
- }
305
- async function restored(room) {
306
- const line = lines.get(room.authority);
307
- let at = room.position;
308
- const chain = [];
309
- for (;;) {
310
- const next = [...kept.values()].find((carry) => carry.authority === room.authority &&
311
- carry.after === at &&
312
- carry.fenced?.logEpoch === room.log_epoch);
313
- if (next?.fenced == null)
314
- break;
315
- const entries = await keptEntries(next.carry, next.parts).catch(() => null);
316
- if (entries === null)
317
- break;
318
- chain.push({ carry: next, entries });
319
- at = next.fenced.position;
320
- }
321
- const partial = [...kept.values()].find((carry) => carry.authority === room.authority &&
322
- carry.fenced === null &&
323
- carry.logEpoch === room.log_epoch &&
324
- carry.after === at &&
325
- carry.resume !== null &&
326
- carry.resume.parts > 0);
327
- const stale = [...kept.values()].filter((carry) => carry.authority === room.authority &&
328
- carry !== partial &&
329
- !chain.some((link) => link.carry === carry));
330
- await forget(stale.map((carry) => carry.carry));
331
- if (line !== undefined && line.logEpoch === room.log_epoch && chain.length > 0) {
332
- const frames = chain.map(({ carry, entries }) => ({
333
- after: BigInt(carry.after),
334
- position: BigInt(carry.fenced.position),
335
- entries,
336
- }));
337
- lines.set(room.authority, { ...line, frames: [...line.frames, ...frames] });
338
- }
339
- return {
340
- ...room,
341
- position: at,
342
- ...(partial?.resume == null
343
- ? {}
344
- : {
345
- carry: {
346
- id: partial.carry,
347
- parts: partial.resume.parts,
348
- from: partial.resume.from,
349
- target: partial.resume.target,
350
- handled: partial.resume.handled,
351
- ...(partial.resume.piece === null ? {} : { piece: partial.resume.piece }),
352
- },
353
- }),
354
- };
164
+ publish(frame, held?.entries ?? []);
355
165
  }
356
166
  function handle(frame, via) {
357
167
  if (frame.type === 'signal') {
@@ -398,9 +208,9 @@ export function houseNotifications(transport, mode) {
398
208
  return;
399
209
  }
400
210
  if (frame.type === 'part')
401
- return stage(frame, via);
402
- if (frame.type === 'carried')
403
- return carried(frame);
211
+ stage(frame, via);
212
+ else if (frame.type === 'carried')
213
+ carried(frame);
404
214
  }
405
215
  function publish(frame, entries) {
406
216
  const at = position(frame.position);
@@ -425,29 +235,12 @@ export function houseNotifications(transport, mode) {
425
235
  }
426
236
  announce(frame.authority, { position: frame.position, logEpoch: frame.log_epoch });
427
237
  }
428
- stream.observe((frame) => {
429
- const via = acknowledging;
430
- if (verifying === 0) {
431
- const handled = handle(frame, via);
432
- if (handled === undefined)
433
- return;
434
- verifying += 1;
435
- receiving = handled.finally(() => {
436
- verifying -= 1;
437
- });
438
- return;
439
- }
440
- verifying += 1;
441
- receiving = receiving
442
- .then(() => handle(frame, via))
443
- .finally(() => {
444
- verifying -= 1;
445
- });
446
- });
238
+ stream.observe((frame) => handle(frame, acknowledging));
447
239
  stream.opened(reopened);
448
240
  stream.closed(({ revoked, lost }) => {
449
241
  opening = null;
450
242
  acknowledging = null;
243
+ staged.clear();
451
244
  emit({ kind: 'closed', revoked, lost });
452
245
  });
453
246
  return {
@@ -471,12 +264,8 @@ export function houseNotifications(transport, mode) {
471
264
  };
472
265
  },
473
266
  },
474
- observe(notify, port) {
267
+ observe(notify) {
475
268
  observers.add(notify);
476
- if (receipts === null) {
477
- receipts = port;
478
- loaded = load(port);
479
- }
480
269
  if (opening !== null) {
481
270
  queueMicrotask(() => {
482
271
  if (observers.has(notify))
@@ -497,30 +286,7 @@ export function houseNotifications(transport, mode) {
497
286
  }
498
287
  const current = opening;
499
288
  opening = null;
500
- if (current === null)
501
- return;
502
- void loaded
503
- .then(() => Promise.all(rooms.map(restored)))
504
- .then((presented) => {
505
- current.hold(presented, runtime);
506
- for (const [index, room] of presented.entries()) {
507
- if (room.position === rooms[index].position)
508
- continue;
509
- announce(room.authority, { position: room.position, logEpoch: room.log_epoch });
510
- }
511
- });
512
- },
513
- published(locator, at) {
514
- const authority = [...locators].find(([, held]) => held.includes(locator))?.[0];
515
- const reached = position(at.position);
516
- if (authority === undefined || reached === null)
517
- return;
518
- void forget([...kept.values()]
519
- .filter((carry) => carry.authority === authority &&
520
- carry.fenced !== null &&
521
- carry.fenced.logEpoch === at.logEpoch &&
522
- BigInt(carry.fenced.position) <= reached)
523
- .map((carry) => carry.carry));
289
+ current?.hold(rooms, runtime);
524
290
  },
525
291
  reopen() {
526
292
  const current = opening;
@@ -539,9 +305,10 @@ export function houseNotifications(transport, mode) {
539
305
  if (!locators.has(authority))
540
306
  lines.delete(authority);
541
307
  }
542
- void loaded.then(() => forget([...kept.values()]
543
- .filter((carry) => !locators.has(carry.authority))
544
- .map((carry) => carry.carry)));
308
+ for (const [carry, held] of staged) {
309
+ if (!locators.has(held.authority))
310
+ staged.delete(carry);
311
+ }
545
312
  },
546
313
  reach(authority, at) {
547
314
  const line = lines.get(authority);
@@ -556,18 +323,7 @@ export function houseNotifications(transport, mode) {
556
323
  held,
557
324
  });
558
325
  },
559
- edited(tracked, from) {
560
- const line = lines.get(tracked.authority);
561
- const start = position(from.position);
562
- if (line === undefined || start === null || line.logEpoch !== from.logEpoch)
563
- return [];
564
- return [
565
- ...new Set(line.frames
566
- .filter((frame) => frame.position > start)
567
- .flatMap((frame) => frame.entries.flatMap((entry) => entry.kind === 'edited' && entry.revision !== undefined ? [entry.path] : []))),
568
- ];
569
- },
570
- *advance(tracked, from, holds) {
326
+ *advance(tracked, from) {
571
327
  const line = lines.get(tracked.authority);
572
328
  const start = position(from.position);
573
329
  if (line === undefined || start === null || line.logEpoch !== from.logEpoch)
@@ -585,13 +341,7 @@ export function houseNotifications(transport, mode) {
585
341
  folded(answer, entry.path, entry.removed === true);
586
342
  continue;
587
343
  }
588
- if (entry.kind === 'edited' &&
589
- entry.revision !== undefined &&
590
- holds?.(entry.path, entry.revision) === true) {
591
- forgotten(answer, entry.path);
592
- continue;
593
- }
594
- if (tracked.schemas.includes(entry.path))
344
+ if (entry.path === tracked.schema)
595
345
  schemas.set(entry.path, entry);
596
346
  if (entry.kind === 'removed') {
597
347
  forgotten(answer, entry.path);
@@ -599,7 +349,7 @@ export function houseNotifications(transport, mode) {
599
349
  continue;
600
350
  }
601
351
  const binary = binaryFileOf(entry);
602
- if (binary === null || (binary === undefined && entry.text === undefined))
352
+ if (binary === null)
603
353
  return null;
604
354
  if (mode === 'credential' && entry.revision === undefined)
605
355
  return null;