@agentshouse/kit 0.2.1-cf6a92e13f031 → 0.2.1-cfbadfb5b5e28

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 (48) hide show
  1. package/README.md +14 -15
  2. package/dist/acp.js +58 -7
  3. package/dist/afk.js +17 -0
  4. package/dist/agents.js +58 -32
  5. package/dist/authority-main.js +3 -3
  6. package/dist/bridge.js +4 -4
  7. package/dist/building.js +50 -0
  8. package/dist/calls.js +12 -4
  9. package/dist/chunks.js +49 -0
  10. package/dist/clis.js +41 -42
  11. package/dist/connection.js +27 -33
  12. package/dist/content.js +63 -0
  13. package/dist/conversations.js +348 -126
  14. package/dist/copies.js +294 -148
  15. package/dist/end.js +8 -2
  16. package/dist/events.js +742 -210
  17. package/dist/files.js +35 -19
  18. package/dist/git.js +46 -12
  19. package/dist/grok.js +39 -0
  20. package/dist/home.js +0 -7
  21. package/dist/house-cli.js +26 -13
  22. package/dist/house-main.js +3 -3
  23. package/dist/instructions.js +5 -7
  24. package/dist/kit-cli.js +3 -6
  25. package/dist/kit-main.js +3 -3
  26. package/dist/login.js +5 -3
  27. package/dist/logout.js +1 -1
  28. package/dist/mcp.js +2 -0
  29. package/dist/placement.js +10 -18
  30. package/dist/relay.js +21 -0
  31. package/dist/resident.js +47 -32
  32. package/dist/scope.js +12 -4
  33. package/dist/scripts.js +255 -0
  34. package/dist/secret-input.js +1 -1
  35. package/dist/sign-in.js +3 -2
  36. package/dist/silence.js +28 -0
  37. package/dist/skills.js +38 -12
  38. package/dist/transcription.js +37 -25
  39. package/dist/translate.js +18 -9
  40. package/dist/update.js +109 -120
  41. package/dist/wire.js +23 -5
  42. package/dist/work.js +9 -2
  43. package/package.json +18 -17
  44. package/dist/bootstrap.json +0 -24
  45. package/dist/chatgpt.js +0 -32
  46. package/dist/clis-main.js +0 -23
  47. package/dist/parts.js +0 -82
  48. package/dist/web-wizard.js +0 -100
package/dist/copies.js CHANGED
@@ -1,17 +1,32 @@
1
- import { parse } from '@agentshouse/mdmodel';
2
- import { randomUUID } from 'node:crypto';
1
+ import { HOUSE_ROOM_TYPES, readTypes } from '@agentshouse/mdmodel';
2
+ import { createHash, randomUUID } from 'node:crypto';
3
3
  import { mkdir, readFile, readdir, realpath, rename, rm, rmdir, stat, writeFile } from 'node:fs/promises';
4
4
  import { dirname, join } from 'node:path';
5
5
  import { finalRefusal, HouseRefusal, retrying } from './api.js';
6
+ import { chunkTexts, stagedChunks } from './chunks.js';
6
7
  import { blobId, blobText, git, isText, HOUSE_REF, importCommits, localCopies, revision, run, succeeded, tree, } from './git.js';
8
+ import { paused } from './files.js';
7
9
  import { kitHome } from './home.js';
8
10
  import { operationId } from './operation.js';
9
11
  import { refusalOf } from './refusals.js';
10
- import { changed } from './translate.js';
11
- import { relayed } from './relay.js';
12
+ import { changed, readDocument } from './translate.js';
12
13
  const BRANCH = 'main';
13
14
  const PROVENANCE = '_provenance';
14
15
  const IDENTITY = { GIT_COMMITTER_NAME: 'Kit', GIT_COMMITTER_EMAIL: 'house-kit@localhost' };
16
+ const ROOMS = 'rooms/';
17
+ const ENUMERATE = '/kit/door/changes/enumerate';
18
+ const NOT_READY = 'not ready yet; Kit copies it once House has built it';
19
+ const STAGED_CHUNKS = 'house-chunks';
20
+ const SCHEMA = 'settings/schema.yaml';
21
+ // A Room answered not ready is asked again on this schedule: its chunk build usually catches up within seconds, so the first asks come soon and the last repeats until it does.
22
+ const ASKED_AGAIN_MS = [1000, 2000, 3000, 5000, 8000, 11_000, 15_000];
23
+ class NotReady extends Error {
24
+ rooms;
25
+ constructor(rooms) {
26
+ super(NOT_READY);
27
+ this.rooms = rooms;
28
+ }
29
+ }
15
30
  function causeOf(error) {
16
31
  return error instanceof Error ? error.message : String(error);
17
32
  }
@@ -72,6 +87,9 @@ function sidecar(path) {
72
87
  function recordOf(key) {
73
88
  return join(kitHome(), 'local-copies', `${key}.json`);
74
89
  }
90
+ function stagingPrefix() {
91
+ return `.${createHash('sha256').update(kitHome()).digest('hex').slice(0, 16)}-`;
92
+ }
75
93
  function rootOf(handle) {
76
94
  return `/rooms/${handle}`;
77
95
  }
@@ -99,32 +117,59 @@ async function removeFile(repository, path) {
99
117
  return;
100
118
  }
101
119
  }
102
- async function readOnlyPlaced(repository, held, written, removed) {
120
+ async function readOnlyFiles(repository, held) {
103
121
  const tracked = new Set((await git(repository, ['ls-files', '-z'])).split('\0'));
104
122
  const holding = new Set(held);
105
- for (const path of removed) {
106
- if (!holding.has(path))
107
- continue;
108
- if (!tracked.has(path))
109
- await removeFile(repository, path);
110
- holding.delete(path);
111
- }
112
- for (const [path, content] of written) {
113
- if (tracked.has(path))
114
- continue;
115
- if (!holding.has(path) && (await exists(join(repository, path))))
116
- continue;
117
- await writeText(join(repository, path), content);
118
- holding.add(path);
119
- }
120
- return [...holding].sort();
123
+ return {
124
+ remove: async (path) => {
125
+ if (!holding.has(path))
126
+ return;
127
+ if (!tracked.has(path))
128
+ await removeFile(repository, path);
129
+ holding.delete(path);
130
+ },
131
+ place: async (path, content) => {
132
+ if (tracked.has(path))
133
+ return;
134
+ if (!holding.has(path) && (await exists(join(repository, path))))
135
+ return;
136
+ await writeText(join(repository, path), content);
137
+ holding.add(path);
138
+ },
139
+ held: () => [...holding].sort(),
140
+ };
141
+ }
142
+ async function readOnlyPlaced(repository, held, written, removed) {
143
+ const files = await readOnlyFiles(repository, held);
144
+ for (const path of removed)
145
+ await files.remove(path);
146
+ for (const [path, content] of written)
147
+ await files.place(path, content);
148
+ return files.held();
149
+ }
150
+ async function importedWhole(repository, scope, texts, commit, readOnly) {
151
+ const revisions = {};
152
+ async function* tracked() {
153
+ for await (const { path, content, revision: held, provenance } of texts) {
154
+ if (declaredWritable(path, scope)) {
155
+ revisions[path] = held;
156
+ yield [path, content];
157
+ }
158
+ else {
159
+ await readOnly.place(path, content);
160
+ }
161
+ if (provenance !== undefined)
162
+ await readOnly.place(sidecar(path), provenance);
163
+ }
164
+ }
165
+ return { house: await importCommits(repository, [{ ...commit, files: tracked() }]), revisions };
121
166
  }
122
167
  async function ruled(path, rules) {
123
168
  if ((await readFile(path, 'utf8').catch(() => null)) !== rules)
124
169
  await writeFile(path, rules);
125
170
  }
126
171
  async function ignoreRules(repository, writable, copy) {
127
- const readOnly = ('read_only' in copy ? copy.read_only : [...copy.readOnly.keys()]).filter((path) => !path.split('/').includes(PROVENANCE));
172
+ const readOnly = copy.read_only.filter((path) => !path.split('/').includes(PROVENANCE));
128
173
  const protectedPath = (path) => copy.protected.some((prefix) => prefix !== '' && covers(path, prefix));
129
174
  const whole = copy.protected.filter((prefix) => prefix !== '' && !writable.some((path) => covers(path, prefix)));
130
175
  const house = [
@@ -155,6 +200,12 @@ async function ignoreRules(repository, writable, copy) {
155
200
  '',
156
201
  ].join('\n'));
157
202
  }
203
+ async function modelOf(copy, files) {
204
+ const entry = files.get(SCHEMA);
205
+ if (entry !== undefined)
206
+ return readTypes(await blobText(copy.repository, entry), HOUSE_ROOM_TYPES);
207
+ return readTypes(copy.read_only.includes(SCHEMA) ? await readFile(join(copy.repository, SCHEMA), 'utf8') : '', HOUSE_ROOM_TYPES);
208
+ }
158
209
  function revisionAt(copy, commit, path) {
159
210
  let manifest = copy.manifests[commit];
160
211
  while (manifest !== undefined) {
@@ -184,6 +235,7 @@ export class LocalCopies {
184
235
  house;
185
236
  copies = new Map();
186
237
  queues = new Map();
238
+ waiting = new Set();
187
239
  answering = Promise.resolve();
188
240
  constructor(house) {
189
241
  this.house = house;
@@ -191,7 +243,7 @@ export class LocalCopies {
191
243
  key: 'owner',
192
244
  submit: async (operation, changes) => {
193
245
  try {
194
- const outcome = await retrying(() => house.post('/kit/door/batch', { operation_id: operation, changes }));
246
+ const outcome = await retrying(() => house.post('/kit/door/files/batch', { operation_id: operation, changes }));
195
247
  return { accepted: outcome.contents };
196
248
  }
197
249
  catch (error) {
@@ -215,25 +267,21 @@ export class LocalCopies {
215
267
  this.copies.set(copy.key, copy);
216
268
  }
217
269
  }
218
- async mapping() {
219
- await this.answering;
220
- return [...this.copies.values()]
221
- .filter((copy) => copy.unfollowed === undefined)
222
- .map((copy) => `${rootOf(copy.room_handle)}: ${copy.repository}`)
223
- .sort();
270
+ async swept() {
271
+ const rooms = join(localCopies(), 'rooms');
272
+ for (const name of await readdir(rooms).catch(() => [])) {
273
+ if (name.startsWith(stagingPrefix()))
274
+ await rm(join(rooms, name), { recursive: true, force: true });
275
+ }
224
276
  }
225
277
  connected() {
226
278
  return this.selected();
227
279
  }
228
- async changed(rooms) {
229
- if (rooms.length > 0)
230
- await this.selected(rooms);
231
- }
232
280
  fetch() {
233
281
  return this.answered(() => this.fetched(true));
234
282
  }
235
- async selected(rooms) {
236
- const copied = await this.answered(() => this.fetched(false, rooms));
283
+ async selected() {
284
+ const copied = await this.answered(() => this.fetched(false));
237
285
  if (copied.refused)
238
286
  logged(copied.text);
239
287
  }
@@ -242,16 +290,14 @@ export class LocalCopies {
242
290
  this.answering = answered.catch(() => undefined);
243
291
  return answered;
244
292
  }
245
- async fetched(refreshing, rooms) {
246
- const { local_copy: answer } = await this.house.post('/kit/local-copy/selection', rooms === undefined ? {} : { rooms });
247
- const synced = new Map(answer.rooms.map((room) => [`rooms/${room.room_ref}`, room.room_handle]));
248
- const named = rooms === undefined ? null : new Set(rooms.map((room) => `rooms/${room}`));
249
- const held = () => [...this.copies.values()].filter((copy) => named === null || named.has(copy.key));
250
- return this.holding([...new Set([...held().map((copy) => copy.key), ...synced.keys()])].sort(), async () => {
293
+ async fetched(refreshing) {
294
+ const { local_copy: answer } = await this.house.post('/kit/local-copies/selection/read', {});
295
+ const synced = new Map(answer.rooms.map((room) => [`${ROOMS}${room.room_ref}`, room.room_handle]));
296
+ return this.holding([...new Set([...this.copies.keys(), ...synced.keys()])].sort(), async () => {
251
297
  const said = [];
252
298
  let refused = false;
253
299
  const mark = refreshing ? 'said' : 'unsaid';
254
- for (const copy of held()) {
300
+ for (const copy of [...this.copies.values()]) {
255
301
  if (synced.has(copy.key) || copy.unfollowed === 'said' || copy.unfollowed === mark)
256
302
  continue;
257
303
  await this.save({ ...copy, unfollowed: mark });
@@ -259,11 +305,12 @@ export class LocalCopies {
259
305
  said.push(`${rootOf(copy.room_handle)}: no longer followed; kept as it is`);
260
306
  }
261
307
  const current = [];
308
+ const wanted = new Map();
262
309
  for (const [key, handle] of synced) {
263
310
  try {
264
311
  const held = this.copies.get(key);
265
312
  if (held === undefined || !(await exists(held.repository))) {
266
- said.push(await this.materialize(key, handle));
313
+ wanted.set(key, handle);
267
314
  continue;
268
315
  }
269
316
  const { unfollowed, ...followed } = held;
@@ -274,18 +321,62 @@ export class LocalCopies {
274
321
  said.push(`${rootOf(handle)}: ${causeOf(error)}`);
275
322
  }
276
323
  }
277
- if (refreshing && current.length > 0) {
278
- const failed = await this.refreshed(current).then(() => null, houseCause);
279
- for (const copy of current)
280
- said.push(...moved(copy, this.copies.get(copy.key)));
324
+ const unready = [];
325
+ const unsettled = [];
326
+ if (wanted.size > 0) {
327
+ const { authorities } = await this.discovered([...wanted.values()]);
328
+ for (const [key, handle] of wanted) {
329
+ const authority = authorities.find((entry) => `${ROOMS}${entry.room_ref}` === key);
330
+ try {
331
+ if (authority === undefined)
332
+ throw new Error('House serves nothing of it to copy');
333
+ said.push(await this.copied({ key, handle, authority }));
334
+ }
335
+ catch (error) {
336
+ if (error instanceof NotReady) {
337
+ unready.push(...error.rooms);
338
+ said.push(`${rootOf(handle)}: ${NOT_READY}`);
339
+ }
340
+ else {
341
+ refused = true;
342
+ unsettled.push(key.slice(ROOMS.length));
343
+ said.push(`${rootOf(handle)}: ${causeOf(error)}`);
344
+ }
345
+ }
346
+ }
347
+ }
348
+ const reading = refreshing ? current : current.filter((copy) => this.waiting.has(copy.room_ref));
349
+ if (reading.length > 0) {
350
+ const failed = await this.refreshed(reading, unready).then(() => null, houseCause);
351
+ for (const copy of reading) {
352
+ said.push(...(unready.includes(copy.room_ref) ? [`${rootOf(copy.room_handle)}: ${NOT_READY}`] : moved(copy, this.copies.get(copy.key))));
353
+ }
281
354
  if (failed !== null) {
282
355
  refused = true;
356
+ unsettled.push(...reading.map((copy) => copy.room_ref));
283
357
  said.push(failed);
284
358
  }
285
359
  }
360
+ for (const room of [...this.waiting]) {
361
+ if (!unready.includes(room) && !unsettled.includes(room))
362
+ this.waiting.delete(room);
363
+ }
364
+ const due = unready.filter((room) => !this.waiting.has(room));
365
+ for (const room of due)
366
+ this.waiting.add(room);
367
+ if (due.length > 0)
368
+ void this.askedAgain(due);
286
369
  return { refused, text: said.length === 0 ? 'Nothing new from House.' : said.join('\n') };
287
370
  });
288
371
  }
372
+ async askedAgain(rooms) {
373
+ let asking = rooms;
374
+ for (let step = 0; asking.length > 0; step++) {
375
+ await paused(ASKED_AGAIN_MS[Math.min(step, ASKED_AGAIN_MS.length - 1)]);
376
+ await this.selected().catch(logged);
377
+ asking = asking.filter((room) => this.waiting.has(room));
378
+ }
379
+ }
289
380
  async observed(report) {
290
381
  const key = await this.keyAt(dirname(report.common));
291
382
  if (key === undefined)
@@ -347,132 +438,170 @@ export class LocalCopies {
347
438
  this.copies.set(copy.key, copy);
348
439
  return copy;
349
440
  }
350
- async read(handle) {
351
- const prefixes = [rootOf(handle)];
352
- const discovery = await this.house.post('/kit/door/discover', { prefixes });
353
- let bundle = await this.house.post('/kit/door/bootstrap', { prefixes });
354
- if ('transfer' in bundle)
355
- bundle = (await (await fetch(relayed(bundle.transfer.url))).json());
356
- const [view] = bundle.scopes;
357
- if (view === undefined)
358
- return null;
359
- const authority = discovery.authorities.find((entry) => entry.room_ref === view.manifest.anchor);
360
- const contents = new Map(view.contents.map((entry) => [entry.path, entry.content]));
361
- const read = {
362
- anchor: view.manifest.anchor,
363
- position: view.manifest.position,
364
- log_epoch: view.manifest.log_epoch,
365
- write: authority.operations.write,
366
- protected: authority.protected_paths,
367
- writable: new Map(),
368
- revisions: {},
369
- readOnly: new Map(),
370
- };
371
- for (const file of view.manifest.files) {
372
- const content = contents.get(file.path);
373
- if (file.writable) {
374
- read.writable.set(file.path, content);
375
- read.revisions[file.path] = file.revision;
376
- }
377
- else {
378
- read.readOnly.set(file.path, content);
441
+ async discovered(handles) {
442
+ const discover = (group) => this.house.post('/kit/door/rooms/discover', { prefixes: group.map(rootOf) });
443
+ const [first, ...rest] = handles;
444
+ const { bounds, authorities } = await discover([first]);
445
+ for (const group of grouped(rest, bounds.max_prefixes))
446
+ authorities.push(...(await discover(group)).authorities);
447
+ return { bounds, authorities };
448
+ }
449
+ async chunked(root, room, directory) {
450
+ await mkdir(directory, { recursive: true });
451
+ for (let renewed = false;; renewed = true) {
452
+ const { rooms } = await this.house.post('/kit/door/room-chunks/list', { prefixes: [root] });
453
+ const list = rooms.find((entry) => entry.room_ref === room);
454
+ if (list === undefined)
455
+ throw new Error('House serves nothing of it to copy');
456
+ if (!list.ready)
457
+ throw new NotReady([room]);
458
+ const held = new Set(await readdir(directory));
459
+ const failure = await stagedChunks(directory, list.chunks.filter(({ hash }) => !held.has(hash)));
460
+ if (failure === undefined) {
461
+ return { position: list.position, log_epoch: list.log_epoch, texts: chunkTexts(directory, list.chunks, list.models) };
379
462
  }
380
- if (file.provenance !== undefined)
381
- read.readOnly.set(sidecar(file.path), file.provenance);
463
+ if (renewed && (await readdir(directory)).length === held.size)
464
+ throw failure;
382
465
  }
383
- return read;
384
466
  }
385
- async materialize(key, handle) {
386
- const read = await this.read(handle);
387
- if (read === null)
388
- throw new Error('House serves nothing of it to copy');
389
- const repository = join(localCopies(), 'rooms', read.anchor);
467
+ async materialized({ key, handle, authority }, whole) {
468
+ const repository = join(localCopies(), 'rooms', authority.room_ref);
390
469
  if (await exists(repository))
391
470
  throw new Error(`${repository} already exists and Kit leaves it as it is`);
392
- const staging = join(dirname(repository), `.${randomUUID()}`);
471
+ const staging = join(dirname(repository), `${stagingPrefix()}${randomUUID()}`);
472
+ const scope = { write: authority.operations.write, protected: authority.protected_paths };
393
473
  await mkdir(staging, { recursive: true });
394
- let house;
395
- let readOnly;
474
+ let copy;
396
475
  try {
397
476
  await git(staging, ['init', '-q', '-b', BRANCH]);
398
- house = await importCommits(staging, [
399
- { message: `House ${rootOf(handle)}`, from: null, files: read.writable },
400
- ]);
477
+ const read = await whole(join(staging, '.git', STAGED_CHUNKS));
478
+ const readOnly = await readOnlyFiles(staging, []);
479
+ const { house, revisions } = await importedWhole(staging, scope, read.texts, { message: `House ${rootOf(handle)}`, from: null }, readOnly);
401
480
  await git(staging, ['update-ref', `refs/heads/${BRANCH}`, house]);
402
481
  await git(staging, ['reset', '-q', '--hard']);
403
- readOnly = await readOnlyPlaced(staging, [], read.readOnly, []);
404
- await ignoreRules(staging, [...read.writable.keys()], read);
482
+ copy = {
483
+ key,
484
+ room_ref: authority.room_ref,
485
+ room_handle: handle,
486
+ repository,
487
+ house,
488
+ position: read.position,
489
+ log_epoch: read.log_epoch,
490
+ ...scope,
491
+ read_only: readOnly.held(),
492
+ manifests: { [house]: { parent: null, files: revisions } },
493
+ moves: [],
494
+ };
495
+ await ignoreRules(staging, Object.keys(revisions), copy);
496
+ await rm(join(staging, '.git', STAGED_CHUNKS), { recursive: true, force: true });
405
497
  await rename(staging, repository);
406
498
  }
407
499
  catch (error) {
408
500
  await rm(staging, { recursive: true, force: true });
409
501
  throw error;
410
502
  }
411
- await this.save({
412
- key,
413
- room_ref: read.anchor,
414
- room_handle: handle,
415
- repository,
416
- house,
417
- position: read.position,
418
- log_epoch: read.log_epoch,
419
- write: read.write,
420
- protected: read.protected,
421
- read_only: readOnly,
422
- manifests: { [house]: { parent: null, files: read.revisions } },
423
- moves: [],
424
- });
425
- return `${rootOf(handle)}: copied to ${repository}`;
503
+ return this.save(copy);
426
504
  }
427
- async received(copy) {
428
- const read = await this.read(copy.room_handle);
429
- if (read === null)
430
- return copy;
431
- const house = await importCommits(copy.repository, [
432
- { message: `House ${rootOf(copy.room_handle)} at position ${read.position}`, from: copy.house, replaces: true, files: read.writable },
433
- ]);
434
- const removed = copy.read_only.filter((path) => !read.readOnly.has(path));
435
- const readOnly = await readOnlyPlaced(copy.repository, copy.read_only, read.readOnly, removed);
436
- const next = {
437
- ...copy,
438
- house,
439
- position: read.position,
440
- log_epoch: read.log_epoch,
441
- write: read.write,
442
- protected: read.protected,
443
- read_only: readOnly,
444
- manifests: { ...copy.manifests, [house]: { parent: null, files: read.revisions } },
445
- };
446
- await ignoreRules(copy.repository, [...read.writable.keys()], next);
447
- return next;
505
+ async copied(wanted) {
506
+ const { handle, authority } = wanted;
507
+ const root = rootOf(handle);
508
+ let answer = null;
509
+ try {
510
+ if (!this.waiting.has(authority.room_ref)) {
511
+ answer = await this.house.post(ENUMERATE, {
512
+ prefixes: [root],
513
+ positions: [
514
+ { authority: authority.room_ref, position: '0', log_epoch: authority.log_epoch, page: { through: authority.position, read: 0 } },
515
+ ],
516
+ });
517
+ }
518
+ }
519
+ catch (error) {
520
+ if (expiredAuthority(error) !== authority.room_ref)
521
+ throw error;
522
+ }
523
+ const view = answer?.authorities.find((entry) => entry.authority === authority.room_ref);
524
+ if (answer !== null && view === undefined)
525
+ throw new Error('House serves nothing of it to copy');
526
+ if (view !== undefined && view.next === undefined) {
527
+ const texts = view.changes.filter((change) => !('removed' in change));
528
+ const copy = await this.materialized(wanted, async () => ({ position: view.position, log_epoch: view.log_epoch, texts }));
529
+ return `${root}: copied to ${copy.repository}`;
530
+ }
531
+ const copy = await this.materialized(wanted, (directory) => this.chunked(root, authority.room_ref, directory));
532
+ const [caught] = await this.caughtUp([copy], true);
533
+ await succeeded(copy.repository, ['merge', '--ff-only', '-q', caught.house]);
534
+ return `${root}: copied to ${copy.repository}`;
448
535
  }
449
- async refreshed(copies) {
450
- const discover = (group) => this.house.post('/kit/door/discover', { prefixes: group.map((copy) => rootOf(copy.room_handle)) });
451
- const [first, ...rest] = copies;
452
- const { bounds, authorities } = await discover([first]);
453
- for (const group of grouped(rest, bounds.max_prefixes))
454
- authorities.push(...(await discover(group)).authorities);
536
+ async received(copy, authority) {
537
+ const root = rootOf(copy.room_handle);
538
+ const directory = join(copy.repository, '.git', STAGED_CHUNKS);
539
+ const scope = { write: authority.operations.write, protected: authority.protected_paths };
540
+ try {
541
+ const read = await this.chunked(root, copy.room_ref, directory);
542
+ const readOnly = await readOnlyFiles(copy.repository, copy.read_only);
543
+ for (const path of copy.read_only)
544
+ await readOnly.remove(path);
545
+ const commit = { message: `House ${root} at position ${read.position}`, from: copy.house, replaces: true };
546
+ const { house, revisions } = await importedWhole(copy.repository, scope, read.texts, commit, readOnly);
547
+ const next = {
548
+ ...copy,
549
+ ...scope,
550
+ house,
551
+ position: read.position,
552
+ log_epoch: read.log_epoch,
553
+ read_only: readOnly.held(),
554
+ manifests: { ...copy.manifests, [house]: { parent: null, files: revisions } },
555
+ };
556
+ await ignoreRules(copy.repository, Object.keys(revisions), next);
557
+ return next;
558
+ }
559
+ finally {
560
+ await rm(directory, { recursive: true, force: true });
561
+ }
562
+ }
563
+ async reread(copy) {
564
+ const { authorities } = await this.discovered([copy.room_handle]);
565
+ const authority = authorities.find((entry) => entry.room_ref === copy.room_ref);
566
+ if (authority === undefined)
567
+ throw new NotReady([copy.room_ref]);
568
+ return this.received(copy, authority);
569
+ }
570
+ async refreshed(copies, unready) {
571
+ const { bounds, authorities } = await this.discovered(copies.map((copy) => copy.room_handle));
572
+ const read = [];
455
573
  const unchanged = [];
456
574
  for (const copy of copies) {
457
575
  const authority = authorities.find((entry) => entry.room_ref === copy.room_ref);
458
576
  if (authority !== undefined &&
459
577
  JSON.stringify([authority.operations.write, authority.protected_paths]) !== JSON.stringify([copy.write, copy.protected])) {
460
- await this.save(await this.received(copy));
578
+ try {
579
+ read.push(await this.save(await this.received(copy, authority)));
580
+ }
581
+ catch (error) {
582
+ if (!(error instanceof NotReady))
583
+ throw error;
584
+ unready.push(...error.rooms);
585
+ }
461
586
  }
462
587
  else {
463
588
  unchanged.push(copy);
464
589
  }
465
590
  }
466
591
  for (const group of grouped(unchanged, bounds.max_prefixes))
467
- await this.caughtUp(group, false);
592
+ await this.caughtUp(group, false, unready);
593
+ for (const group of grouped(read, bounds.max_prefixes))
594
+ await this.caughtUp(group, true, unready);
468
595
  }
469
- async caughtUp(copies, whole) {
596
+ async caughtUp(copies, whole, unready) {
470
597
  const caught = new Map(copies.map((copy) => [copy.key, copy]));
598
+ const read = new Set();
599
+ const waiting = unready ?? [];
471
600
  let asking = [...copies];
472
601
  while (asking.length > 0) {
473
602
  let answer;
474
603
  try {
475
- answer = await this.house.post('/kit/door/enumerate', {
604
+ answer = await this.house.post(ENUMERATE, {
476
605
  prefixes: asking.map((copy) => rootOf(copy.room_handle)),
477
606
  positions: asking.map((copy) => ({ authority: copy.room_ref, position: copy.position, log_epoch: copy.log_epoch })),
478
607
  });
@@ -481,8 +610,22 @@ export class LocalCopies {
481
610
  const expired = asking.find((copy) => copy.room_ref === expiredAuthority(error));
482
611
  if (expired === undefined)
483
612
  throw error;
484
- caught.set(expired.key, await this.save(await this.received(expired)));
485
613
  asking = asking.filter((copy) => copy !== expired);
614
+ if (read.has(expired.key)) {
615
+ waiting.push(expired.room_ref);
616
+ continue;
617
+ }
618
+ read.add(expired.key);
619
+ try {
620
+ const next = await this.save(await this.reread(expired));
621
+ caught.set(expired.key, next);
622
+ asking.push(next);
623
+ }
624
+ catch (refused) {
625
+ if (!(refused instanceof NotReady))
626
+ throw refused;
627
+ waiting.push(expired.room_ref);
628
+ }
486
629
  continue;
487
630
  }
488
631
  const advanced = [];
@@ -494,8 +637,10 @@ export class LocalCopies {
494
637
  caught.set(copy.key, next);
495
638
  advanced.push(next);
496
639
  }
497
- asking = whole ? advanced : [];
640
+ asking = advanced.filter((copy) => whole || read.has(copy.key));
498
641
  }
642
+ if (unready === undefined && waiting.length > 0)
643
+ throw new NotReady(waiting);
499
644
  return copies.map((copy) => caught.get(copy.key));
500
645
  }
501
646
  async incorporated(copy, changes, position, logEpoch) {
@@ -598,7 +743,7 @@ export class LocalCopies {
598
743
  const arrived = new Set(moved.values());
599
744
  const arriving = [...after].filter(([path, entry]) => before.get(path) !== entry);
600
745
  if (arriving.length > 0) {
601
- const { bounds } = await this.house.post('/kit/door/discover', { prefixes: [root] });
746
+ const { bounds } = await this.house.post('/kit/door/rooms/discover', { prefixes: [root] });
602
747
  const binary = [];
603
748
  for (const [path, entry] of arriving) {
604
749
  if (!(await isText(copy.repository, entry, bounds.max_authored_file_bytes)))
@@ -608,6 +753,7 @@ export class LocalCopies {
608
753
  throw new Error(textOnly(binary));
609
754
  }
610
755
  const text = (entry) => blobText(copy.repository, entry);
756
+ const model = await modelOf(copy, after);
611
757
  const changes = [];
612
758
  for (const path of [...new Set([...before.keys(), ...after.keys()])].sort()) {
613
759
  if (arrived.has(path))
@@ -631,7 +777,7 @@ export class LocalCopies {
631
777
  else if (prior === undefined)
632
778
  changes.push({ op: 'create', path: `${root}/${path}`, content: await text(next) });
633
779
  else
634
- changes.push(...changed(`${root}/${path}`, path, known, await text(prior), await text(next)));
780
+ changes.push(...changed(`${root}/${path}`, path, known, await text(prior), await text(next), model));
635
781
  }
636
782
  return changes;
637
783
  }
@@ -687,7 +833,8 @@ export class LocalCopies {
687
833
  const current = (await this.caughtUp([copy], true))[0];
688
834
  const house = await tree(copy.repository, current.house);
689
835
  const root = rootOf(current.room_handle);
690
- if (!(await this.landed(copy.repository, push.changes, house, await tree(copy.repository, push.commit)))) {
836
+ const model = await modelOf(current, house);
837
+ if (!(await this.landed(copy.repository, push.changes, house, await tree(copy.repository, push.commit), model))) {
691
838
  await this.save(without(current));
692
839
  return {
693
840
  refused: true,
@@ -786,7 +933,7 @@ export class LocalCopies {
786
933
  push: { ...push, acknowledged },
787
934
  };
788
935
  }
789
- async landed(repository, changes, house, selected) {
936
+ async landed(repository, changes, house, selected, model) {
790
937
  const text = async (path) => {
791
938
  const entry = house.get(relative(path));
792
939
  return entry === undefined ? undefined : blobText(repository, entry);
@@ -800,9 +947,8 @@ export class LocalCopies {
800
947
  }
801
948
  if ((change.op === 'create' || change.op === 'replace') && current === change.content)
802
949
  continue;
803
- const parsed = current === undefined ? null : parse(current, change.path);
804
- const document = parsed?.ok === true ? parsed.document : null;
805
- if (document === null)
950
+ const document = current === undefined ? undefined : readDocument(current, relative(change.path), model)?.document;
951
+ if (document === undefined)
806
952
  return false;
807
953
  if (change.op === 'set_field' && JSON.stringify(document.fields[change.field]) === JSON.stringify(change.value))
808
954
  continue;
@@ -815,7 +961,7 @@ export class LocalCopies {
815
961
  return true;
816
962
  }
817
963
  async revisionText(path, wanted) {
818
- const page = await this.house.post('/kit/door/revisions', {
964
+ const page = await this.house.post('/kit/door/files/revisions/list', {
819
965
  path,
820
966
  revision: wanted,
821
967
  });