@agentshouse/kit 0.2.1-cb6679d048e60 → 0.2.1-cbabe8ad337ff

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.
package/dist/copies.js CHANGED
@@ -2,15 +2,15 @@ import { parse } from '@agentshouse/mdmodel';
2
2
  import { 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
- import { HouseRefusal } from './api.js';
5
+ import { finalRefusal, HouseRefusal, retrying } from './api.js';
6
6
  import { blobId, blobText, git, HOUSE_REF, importCommits, localCopies, revision, run, succeeded, tree, } from './git.js';
7
7
  import { kitHome } from './home.js';
8
8
  import { operationId } from './operation.js';
9
9
  import { refusalOf } from './refusals.js';
10
10
  import { changed } from './translate.js';
11
+ import { relayed } from './relay.js';
11
12
  const BRANCH = 'main';
12
13
  const PROVENANCE = '_provenance';
13
- const REPLICA_RETRY_MS = 30_000;
14
14
  const IDENTITY = { GIT_COMMITTER_NAME: 'House Kit', GIT_COMMITTER_EMAIL: 'house-kit@localhost' };
15
15
  function causeOf(error) {
16
16
  return error instanceof Error ? error.message : String(error);
@@ -40,11 +40,11 @@ function sidecar(path) {
40
40
  segments.splice(-1, 0, PROVENANCE);
41
41
  return segments.join('/');
42
42
  }
43
- function rootOf(copy) {
44
- return copy.private ? '/private' : `/rooms/${copy.room_handle}`;
43
+ function rootOf(handle) {
44
+ return `/rooms/${handle}`;
45
45
  }
46
46
  function relative(path) {
47
- return path.replace(/^\/private\//, '').replace(/^\/rooms\/[^/]+\//, '');
47
+ return path.replace(/^\/rooms\/[^/]+\//, '');
48
48
  }
49
49
  function declaredWritable(path, copy) {
50
50
  const deepest = (scopes) => Math.max(-1, ...scopes.filter((scope) => covers(path, scope)).map((scope) => scope.length));
@@ -160,11 +160,11 @@ export class LocalCopies {
160
160
  key: 'owner',
161
161
  submit: async (operation, changes) => {
162
162
  try {
163
- const outcome = await house.post('/kit/door/batch', { operation_id: operation, changes });
163
+ const outcome = await retrying(() => house.post('/kit/door/batch', { operation_id: operation, changes }));
164
164
  return { accepted: outcome.contents };
165
165
  }
166
166
  catch (error) {
167
- if (!(error instanceof HouseRefusal) || error.status >= 500)
167
+ if (!finalRefusal(error))
168
168
  throw error;
169
169
  return {
170
170
  refused: refusalOf(error.text) ?? { code: '', text: `House refused it with ${error.status}`, conflicts: [] },
@@ -176,7 +176,7 @@ export class LocalCopies {
176
176
  async load() {
177
177
  const home = join(kitHome(), 'local-copies');
178
178
  const names = (await readdir(join(home, 'rooms')).catch(() => [])).filter((name) => name.endsWith('.json'));
179
- for (const path of [...names.map((name) => join(home, 'rooms', name)), join(home, 'private.json')]) {
179
+ for (const path of names.map((name) => join(home, 'rooms', name))) {
180
180
  const text = await readFile(path, 'utf8').catch(() => null);
181
181
  if (text === null)
182
182
  continue;
@@ -188,7 +188,7 @@ export class LocalCopies {
188
188
  await this.selecting;
189
189
  return [...this.copies.values()]
190
190
  .filter((copy) => copy.active)
191
- .map((copy) => `${rootOf(copy)}: ${copy.repository}`)
191
+ .map((copy) => `${rootOf(copy.room_handle)}: ${copy.repository}`)
192
192
  .sort();
193
193
  }
194
194
  select() {
@@ -198,12 +198,7 @@ export class LocalCopies {
198
198
  }
199
199
  async reselect() {
200
200
  const { local_copy: chosen } = await this.house.post('/kit/local-copy/selection', {});
201
- const selected = [
202
- ...(chosen?.rooms ?? [])
203
- .filter((room) => room.room_handle !== null)
204
- .map((room) => ({ key: `rooms/${room.room_ref}`, private: false, handle: room.room_handle })),
205
- ...(chosen?.private === true ? [{ key: 'private', private: true, handle: null }] : []),
206
- ];
201
+ const selected = (chosen?.rooms ?? []).flatMap((room) => room.room_handle === null ? [] : [{ key: `rooms/${room.room_ref}`, handle: room.room_handle }]);
207
202
  const keys = new Set(selected.map((scope) => scope.key));
208
203
  const work = [];
209
204
  for (const key of this.copies.keys()) {
@@ -287,7 +282,7 @@ export class LocalCopies {
287
282
  }).catch(logged);
288
283
  }
289
284
  async save(copy) {
290
- const path = join(kitHome(), 'local-copies', copy.private ? 'private.json' : `${copy.key}.json`);
285
+ const path = join(kitHome(), 'local-copies', `${copy.key}.json`);
291
286
  await mkdir(dirname(path), { recursive: true, mode: 0o700 });
292
287
  const temporary = `${path}.${process.pid}`;
293
288
  await writeFile(temporary, JSON.stringify(copy), { mode: 0o600 });
@@ -295,12 +290,12 @@ export class LocalCopies {
295
290
  this.copies.set(copy.key, copy);
296
291
  return copy;
297
292
  }
298
- async read(selected) {
299
- const prefixes = [rootOf(selected)];
293
+ async read(handle) {
294
+ const prefixes = [rootOf(handle)];
300
295
  const discovery = await this.house.post('/kit/door/discover', { prefixes });
301
296
  let bundle = await this.house.post('/kit/door/bootstrap', { prefixes });
302
297
  if ('transfer' in bundle)
303
- bundle = (await (await fetch(bundle.transfer.url)).json());
298
+ bundle = (await (await fetch(relayed(bundle.transfer.url))).json());
304
299
  const [view] = bundle.scopes;
305
300
  if (view === undefined)
306
301
  return null;
@@ -343,13 +338,13 @@ export class LocalCopies {
343
338
  return;
344
339
  }
345
340
  const discovery = await this.house.post('/kit/door/discover', {
346
- prefixes: [rootOf(copy)],
341
+ prefixes: [rootOf(copy.room_handle)],
347
342
  });
348
343
  const authority = discovery.authorities.find((entry) => entry.room_ref === copy.room_ref);
349
344
  if (authority === undefined)
350
345
  return;
351
346
  if (authority.managed_replication !== 'admitted') {
352
- logged(`operation_denied: ${rootOf(copy)} is not synchronized to this Environment`);
347
+ logged(`operation_denied: ${rootOf(copy.room_handle)} is not synchronized to this Environment`);
353
348
  return;
354
349
  }
355
350
  if (JSON.stringify([authority.operations.write, authority.protected_paths]) !==
@@ -360,12 +355,12 @@ export class LocalCopies {
360
355
  await this.save(await this.caughtUp(copy));
361
356
  }
362
357
  async materialize(scope) {
363
- const read = await this.read({ private: scope.private, room_handle: scope.handle });
358
+ const read = await this.read(scope.handle);
364
359
  if (read === null) {
365
- logged(`House serves nothing of ${rootOf({ private: scope.private, room_handle: scope.handle })} to copy`);
360
+ logged(`House serves nothing of ${rootOf(scope.handle)} to copy`);
366
361
  return;
367
362
  }
368
- const repository = scope.private ? join(localCopies(), 'private') : join(localCopies(), 'rooms', read.anchor);
363
+ const repository = join(localCopies(), 'rooms', read.anchor);
369
364
  if (await exists(repository)) {
370
365
  logged(`${repository} already exists and House Kit leaves it as it is`);
371
366
  return;
@@ -377,7 +372,7 @@ export class LocalCopies {
377
372
  try {
378
373
  await git(staging, ['init', '-q', '-b', BRANCH]);
379
374
  house = await importCommits(staging, [
380
- { message: `House ${rootOf({ private: scope.private, room_handle: scope.handle })}`, from: null, files: read.writable },
375
+ { message: `House ${rootOf(scope.handle)}`, from: null, files: read.writable },
381
376
  ]);
382
377
  await git(staging, ['update-ref', `refs/heads/${BRANCH}`, house]);
383
378
  await git(staging, ['reset', '-q', '--hard']);
@@ -391,7 +386,6 @@ export class LocalCopies {
391
386
  }
392
387
  await this.save({
393
388
  key: scope.key,
394
- private: scope.private,
395
389
  room_ref: read.anchor,
396
390
  room_handle: scope.handle,
397
391
  repository,
@@ -407,11 +401,11 @@ export class LocalCopies {
407
401
  });
408
402
  }
409
403
  async received(copy) {
410
- const read = await this.read(copy);
404
+ const read = await this.read(copy.room_handle);
411
405
  if (read === null)
412
406
  return copy;
413
407
  const house = await importCommits(copy.repository, [
414
- { message: `House ${rootOf(copy)} at position ${read.position}`, from: copy.house, replaces: true, files: read.writable },
408
+ { message: `House ${rootOf(copy.room_handle)} at position ${read.position}`, from: copy.house, replaces: true, files: read.writable },
415
409
  ]);
416
410
  const removed = copy.read_only.filter((path) => !read.readOnly.has(path));
417
411
  const readOnly = await readOnlyPlaced(copy.repository, copy.read_only, read.readOnly, removed);
@@ -434,7 +428,7 @@ export class LocalCopies {
434
428
  let answer;
435
429
  try {
436
430
  answer = await this.house.post('/kit/door/enumerate', {
437
- prefixes: [rootOf(copy)],
431
+ prefixes: [rootOf(copy.room_handle)],
438
432
  positions: [{ authority: copy.room_ref, position: copy.position, log_epoch: copy.log_epoch }],
439
433
  });
440
434
  }
@@ -444,7 +438,6 @@ export class LocalCopies {
444
438
  return this.received(copy);
445
439
  if (code !== 'replica_behind')
446
440
  throw error;
447
- setTimeout(() => this.deliver(copy.key), REPLICA_RETRY_MS).unref();
448
441
  return copy;
449
442
  }
450
443
  const view = answer.authorities.find((entry) => entry.authority === copy.room_ref);
@@ -484,7 +477,7 @@ export class LocalCopies {
484
477
  let { house, manifests } = copy;
485
478
  if (files.size > 0) {
486
479
  house = await importCommits(copy.repository, [
487
- { message: `House ${rootOf(copy)} at position ${position}`, from: copy.house, files },
480
+ { message: `House ${rootOf(copy.room_handle)} at position ${position}`, from: copy.house, files },
488
481
  ]);
489
482
  manifests = { ...manifests, [house]: { parent: copy.house, files: revisions } };
490
483
  }
@@ -515,7 +508,7 @@ export class LocalCopies {
515
508
  copy = await this.save(without(copy));
516
509
  }
517
510
  if (!copy.active) {
518
- return { refused: true, text: `${rootOf(copy)} is no longer synced to this Environment, so House takes no push from it` };
511
+ return { refused: true, text: `${rootOf(copy.room_handle)} is no longer synced to this Environment, so House takes no push from it` };
519
512
  }
520
513
  const listed = (await git(copy.repository, ['rev-list', '--topo-order', commit])).split('\n');
521
514
  const base = listed.find((ancestor) => ancestor in copy.manifests);
@@ -549,7 +542,7 @@ export class LocalCopies {
549
542
  return this.settle(submitted, caller);
550
543
  }
551
544
  async derived(copy, base, commit) {
552
- const root = rootOf(copy);
545
+ const root = rootOf(copy.room_handle);
553
546
  const before = await tree(copy.repository, base);
554
547
  const after = await tree(copy.repository, commit);
555
548
  const moved = movedPaths(copy.moves, before, after);
@@ -631,7 +624,7 @@ export class LocalCopies {
631
624
  const push = copy.push;
632
625
  const current = await this.save(await this.caughtUp(copy));
633
626
  const house = await tree(copy.repository, current.house);
634
- const root = rootOf(current);
627
+ const root = rootOf(current.room_handle);
635
628
  if (!(await this.landed(copy.repository, push.changes, house, await tree(copy.repository, push.commit)))) {
636
629
  await this.save(without(current));
637
630
  return {
@@ -687,7 +680,7 @@ export class LocalCopies {
687
680
  written = [
688
681
  ...written,
689
682
  {
690
- path: `${rootOf(copy)}/${path}`,
683
+ path: `${rootOf(copy.room_handle)}/${path}`,
691
684
  revision: entry === undefined ? null : (revisionAt(copy, copy.house, path) ?? null),
692
685
  content: entry === undefined ? null : await blobText(copy.repository, entry, path),
693
686
  },
@@ -707,7 +700,7 @@ export class LocalCopies {
707
700
  }
708
701
  const edits = new Map([...files].filter(([path, content]) => (content === null ? selected.has(path) : selected.get(path) !== blobId(content))));
709
702
  const received = [...files.keys()].filter((path) => revisionAt(copy, copy.house, path) === revisionAt(copy, push.house, path));
710
- const accepted = `House ${rootOf(copy)} with ${push.commit} accepted`;
703
+ const accepted = `House ${rootOf(copy.room_handle)} with ${push.commit} accepted`;
711
704
  const house = await importCommits(copy.repository, [
712
705
  ...(edits.size === 0 ? [] : [{ message: accepted, from: push.commit, files: edits }]),
713
706
  {
package/dist/end.js ADDED
@@ -0,0 +1,8 @@
1
+ import { killDescendants } from './scope.js';
2
+ export const REPLACED = 'another Kit started for this Environment';
3
+ export const SIGN_IN_AGAIN = 'sign in again';
4
+ export function end(reason) {
5
+ process.stderr.write(`kit: ${reason}\n`);
6
+ killDescendants();
7
+ process.exit(0);
8
+ }