@agentshouse/kit 0.2.1-cf268dff1869b → 0.2.1-cf2c570b41f24

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 (54) hide show
  1. package/README.md +18 -25
  2. package/dist/acp.js +12 -9
  3. package/dist/afk.js +17 -0
  4. package/dist/agents.js +72 -26
  5. package/dist/api.js +18 -4
  6. package/dist/binaries.js +60 -0
  7. package/dist/bridge.js +99 -44
  8. package/dist/calls.js +18 -1
  9. package/dist/chunks.js +54 -0
  10. package/dist/clis.js +33 -15
  11. package/dist/connect.js +121 -0
  12. package/dist/connection.js +25 -10
  13. package/dist/content.js +106 -0
  14. package/dist/conversations.js +251 -158
  15. package/dist/copies.js +1260 -599
  16. package/dist/earlier.js +273 -0
  17. package/dist/end.js +6 -9
  18. package/dist/events.js +544 -297
  19. package/dist/files.js +77 -11
  20. package/dist/git.js +0 -70
  21. package/dist/grok.js +39 -0
  22. package/dist/home.js +86 -22
  23. package/dist/house-cli.js +145 -162
  24. package/dist/instructions.js +2 -4
  25. package/dist/keys.js +238 -0
  26. package/dist/kit-cli.js +14 -4
  27. package/dist/login.js +34 -20
  28. package/dist/logout.js +2 -2
  29. package/dist/manifest.js +75 -0
  30. package/dist/mcp.js +43 -7
  31. package/dist/owner.js +46 -0
  32. package/dist/placement.js +0 -6
  33. package/dist/refusals.js +50 -23
  34. package/dist/resident.js +117 -76
  35. package/dist/scripts.js +79 -15
  36. package/dist/secret-input.js +1 -1
  37. package/dist/settings.js +247 -0
  38. package/dist/sign-in.js +49 -4
  39. package/dist/supervisor.js +163 -0
  40. package/dist/transcription.js +5 -2
  41. package/dist/update.js +31 -22
  42. package/dist/wire.js +2 -10
  43. package/dist/work.js +27 -2
  44. package/dist/workspace.js +113 -0
  45. package/package.json +12 -8
  46. package/dist/authority-main.js +0 -22
  47. package/dist/chatgpt.js +0 -32
  48. package/dist/configure-main.js +0 -6
  49. package/dist/enrol-main.js +0 -10
  50. package/dist/git-main.js +0 -153
  51. package/dist/parts.js +0 -82
  52. package/dist/service-main.js +0 -9
  53. package/dist/service.js +0 -55
  54. package/dist/translate.js +0 -45
package/dist/copies.js CHANGED
@@ -1,17 +1,39 @@
1
- import { parse } from '@agentshouse/mdmodel';
2
- import { randomUUID } from 'node:crypto';
1
+ import { isUtf8 } from 'node:buffer';
2
+ import { createHash, randomUUID } from 'node:crypto';
3
3
  import { mkdir, readFile, readdir, realpath, rename, rm, rmdir, stat, writeFile } from 'node:fs/promises';
4
- import { dirname, join } from 'node:path';
4
+ import { basename, dirname, join } from 'node:path';
5
5
  import { finalRefusal, HouseRefusal, retrying } from './api.js';
6
- import { blobId, blobText, git, isText, HOUSE_REF, importCommits, localCopies, revision, run, succeeded, tree, } from './git.js';
7
- import { kitHome } from './home.js';
6
+ import { Changed, eachAtOnce, isDocument, mediaType, receivedBinary, sentBinary } from './binaries.js';
7
+ import { chunkEntries, stagedChunks } from './chunks.js';
8
+ import { fileInfo, paused } from './files.js';
9
+ import { kitHome, roomsFolder } from './home.js';
10
+ import { digest, hashed, holding, marked, placed, readManifest, removed, scanned, writeManifest, } from './manifest.js';
11
+ import { ownAgentCall, rpc, toolAnswer, Unsettled } from './mcp.js';
8
12
  import { operationId } from './operation.js';
9
- import { refusalOf } from './refusals.js';
10
- import { changed } from './translate.js';
11
- import { relayed } from './relay.js';
12
- const BRANCH = 'main';
13
+ import { firstLine, refusalOf, retryable, stopsPush } from './refusals.js';
14
+ import { keptName, moved as carried, present as found, removeFolder, rerooted, within } from './workspace.js';
15
+ const ROOMS = 'rooms/';
16
+ const NOT_READY = 'not ready yet; Kit copies it once House has built it';
13
17
  const PROVENANCE = '_provenance';
14
- const IDENTITY = { GIT_COMMITTER_NAME: 'Kit', GIT_COMMITTER_EMAIL: 'house-kit@localhost' };
18
+ const BATCH_FILES = 1000;
19
+ const BATCH_BYTES = 15_000_000;
20
+ const MEGABYTE = 1_000_000;
21
+ const CUT = /\noutput_cut \d+ of \d+ bytes$/;
22
+ const KIT_IGNORES = ['.gitignore', '.ignore'];
23
+ const KIT_GIT = ['.git/refs/house/received', '.git/house-chunks', '.git/house-uploads', '.git/house-downloads'];
24
+ const KIT_NAMES = ['agents.md', 'claude.md'];
25
+ export const DEFAULT_MAX_SIZE = 10;
26
+ export const UPLOAD_NEEDS_OWNER = 'outside an Agent conversation, house upload needs --owner';
27
+ // 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.
28
+ const ASKED_AGAIN_MS = [1000, 2000, 3000, 5000, 8000, 11_000, 15_000];
29
+ class NotReady extends Error {
30
+ rooms;
31
+ constructor(rooms) {
32
+ super(NOT_READY);
33
+ this.rooms = rooms;
34
+ }
35
+ }
36
+ const HELD = { attachments: false, maxSize: DEFAULT_MAX_SIZE };
15
37
  function causeOf(error) {
16
38
  return error instanceof Error ? error.message : String(error);
17
39
  }
@@ -21,31 +43,34 @@ function houseCause(error) {
21
43
  function logged(error) {
22
44
  process.stderr.write(`kit: ${causeOf(error)}\n`);
23
45
  }
24
- const REBASED = new Set([
25
- 'field_base_stale',
26
- 'section_base_stale',
27
- 'preamble_base_stale',
28
- 'base_revision_stale',
29
- 'path_exists',
30
- 'path_absent',
31
- ]);
32
- function rebaseFixes(refusal) {
33
- return refusal.code === 'edit_conflict' && refusal.conflicts.some((code) => REBASED.has(code));
34
- }
35
- function textOnly(paths) {
46
+ function markdownOnly(paths, maxBytes) {
36
47
  const [named, them] = paths.length === 1 ? [`${paths[0]} is`, 'it'] : [`${paths.join(', ')} are`, 'them'];
37
- return `${named} not text, and house push sends text only; remove ${them} from the commit and place ${them} with house upload`;
48
+ return `${named} Markdown, which holds text only: UTF-8 without NUL, at most ${maxBytes} bytes; save ${them} as text and run house upload again`;
38
49
  }
39
- function outside(path) {
40
- return { refused: true, text: `${path} is not a House Local copy; run house push inside one` };
50
+ async function stopping(sent) {
51
+ try {
52
+ return await sent;
53
+ }
54
+ catch (error) {
55
+ const refusal = error instanceof HouseRefusal ? refusalOf(error.text) : null;
56
+ if (refusal === null || !stopsPush(refusal))
57
+ throw error;
58
+ return { refused: refusal };
59
+ }
60
+ }
61
+ function unstagedUploads(refusal) {
62
+ const named = refusal.detail.upload;
63
+ return Array.isArray(named) ? named : [];
41
64
  }
42
- function short(commit) {
43
- return commit.slice(0, 12);
65
+ function currentHandle(refusal) {
66
+ const path = typeof refusal.detail.current_path === 'string' ? refusal.detail.current_path : /use (\/rooms\/\S+)/.exec(refusal.text)?.[1];
67
+ return path === undefined ? null : (/^\/rooms\/([^/]+)/.exec(path)?.[1] ?? null);
68
+ }
69
+ function outside(path) {
70
+ return { refused: true, text: `${path} is not in a House Local copy; run house upload inside one` };
44
71
  }
45
- function moved(before, after) {
46
- if (before.house !== after.house)
47
- return [`${rootOf(after.room_handle)}: ${HOUSE_REF} is at ${short(after.house)}`];
48
- return before.position === after.position ? [] : [`${rootOf(after.room_handle)}: read-only files refreshed`];
72
+ function counted(count, one, many = `${one}s`) {
73
+ return `${count} ${count === 1 ? one : many}`;
49
74
  }
50
75
  function expiredAuthority(error) {
51
76
  if (!(error instanceof HouseRefusal))
@@ -72,136 +97,333 @@ function sidecar(path) {
72
97
  function recordOf(key) {
73
98
  return join(kitHome(), 'local-copies', `${key}.json`);
74
99
  }
100
+ function stagingPrefix() {
101
+ return `.${createHash('sha256').update(kitHome()).digest('hex').slice(0, 16)}-`;
102
+ }
103
+ async function sweptIn(folder) {
104
+ for (const name of await readdir(folder).catch(() => [])) {
105
+ if (name.startsWith(stagingPrefix()))
106
+ await rm(join(folder, name), { recursive: true, force: true });
107
+ }
108
+ }
109
+ async function staging() {
110
+ const folder = join(roomsFolder(), `${stagingPrefix()}work`);
111
+ await mkdir(folder, { recursive: true });
112
+ return `${folder}/`;
113
+ }
75
114
  function rootOf(handle) {
76
115
  return `/rooms/${handle}`;
77
116
  }
78
117
  function relative(path) {
79
118
  return path.replace(/^\/rooms\/[^/]+\//, '');
80
119
  }
120
+ function kitsOwn(path) {
121
+ return !path.includes('/') && KIT_NAMES.includes(path.toLowerCase());
122
+ }
81
123
  function declaredWritable(path, copy) {
82
124
  const deepest = (scopes) => Math.max(-1, ...scopes.filter((scope) => covers(path, scope)).map((scope) => scope.length));
83
125
  return deepest(copy.write) > deepest(copy.protected);
84
126
  }
85
- function pattern(path) {
86
- return `/${path.replace(/[\\*?[]/g, '\\$&').replace(/ +$/, (spaces) => spaces.replace(/ /g, '\\ '))}`;
127
+ function differing(left, right) {
128
+ const counts = new Map();
129
+ for (const line of left.split('\n'))
130
+ counts.set(line, (counts.get(line) ?? 0) + 1);
131
+ let unmatched = 0;
132
+ for (const line of right.split('\n')) {
133
+ const held = counts.get(line) ?? 0;
134
+ if (held > 0)
135
+ counts.set(line, held - 1);
136
+ else
137
+ unmatched++;
138
+ }
139
+ return unmatched + [...counts.values()].reduce((sum, count) => sum + count, 0);
87
140
  }
88
- async function exists(path) {
89
- return stat(path).then(() => true, () => false);
141
+ function whole(theirs, mine) {
142
+ return `<<<<<<< current\n${ended(theirs)}=======\n${ended(mine)}>>>>>>> yours\n`;
90
143
  }
91
- async function writeText(path, content) {
92
- await mkdir(dirname(path), { recursive: true });
93
- await writeFile(path, content);
144
+ function ended(text) {
145
+ return text === '' || text.endsWith('\n') ? text : `${text}\n`;
94
146
  }
95
- async function removeFile(repository, path) {
96
- await rm(join(repository, path), { force: true });
97
- for (let folder = dirname(path); folder !== '.'; folder = dirname(folder)) {
98
- if (!(await rmdir(join(repository, folder)).then(() => true, () => false)))
99
- return;
100
- }
147
+ function sameBytes(file) {
148
+ return `${file.sha256}:${file.bytes}`;
101
149
  }
102
- async function readOnlyPlaced(repository, held, written, removed) {
103
- const tracked = new Set((await git(repository, ['ls-files', '-z'])).split('\0'));
104
- 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();
121
- }
122
- async function ruled(path, rules) {
123
- if ((await readFile(path, 'utf8').catch(() => null)) !== rules)
124
- await writeFile(path, rules);
125
- }
126
- 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));
128
- const protectedPath = (path) => copy.protected.some((prefix) => prefix !== '' && covers(path, prefix));
129
- const whole = copy.protected.filter((prefix) => prefix !== '' && !writable.some((path) => covers(path, prefix)));
130
- const house = [
131
- ...whole,
132
- ...readOnly.filter((path) => protectedPath(path) && !whole.some((prefix) => covers(path, prefix))),
133
- ].map(pattern);
134
- const granted = readOnly.filter((path) => !protectedPath(path)).map(pattern);
135
- await ruled(join(repository, '.gitignore'), [
136
- '# Kit writes this file and .ignore. The paths below are read-only House',
137
- '# content: Git does not track them, and House refuses a change to them even',
138
- '# when this file is edited or a path is force-added.',
139
- '/.gitignore',
140
- '/.ignore',
141
- PROVENANCE,
142
- '',
143
- '# House maintains or protects these paths.',
144
- ...house,
145
- '',
146
- '# Your current House grant does not allow writing these paths.',
147
- ...granted,
148
- '',
149
- ].join('\n'));
150
- await ruled(join(repository, '.ignore'), [
151
- '# Kit writes this file so native search still reads the read-only House',
152
- '# content that .gitignore keeps out of Git.',
153
- `!${PROVENANCE}`,
154
- ...[...house, ...granted].map((rule) => `!${rule}`),
155
- '',
156
- ].join('\n'));
157
- }
158
- function revisionAt(copy, commit, path) {
159
- let manifest = copy.manifests[commit];
160
- while (manifest !== undefined) {
161
- if (path in manifest.files)
162
- return manifest.files[path] ?? undefined;
163
- manifest = manifest.parent === null ? undefined : copy.manifests[manifest.parent];
164
- }
165
- return undefined;
150
+ async function exists(path) {
151
+ return stat(path).then(() => true, () => false);
166
152
  }
167
153
  function without(copy) {
168
- const { push, ...settled } = copy;
154
+ const { pending, ...settled } = copy;
169
155
  return settled;
170
156
  }
157
+ function earlier(copy) {
158
+ return 'manifests' in copy;
159
+ }
160
+ function rehandled(copy, handle) {
161
+ const from = `${rootOf(copy.room_handle)}/`;
162
+ const to = `${rootOf(handle)}/`;
163
+ const moved = (path) => (path.startsWith(from) ? `${to}${path.slice(from.length)}` : path);
164
+ const { upload, ...pending } = copy.pending;
165
+ return {
166
+ ...copy,
167
+ room_handle: handle,
168
+ pending: {
169
+ ...pending,
170
+ changes: pending.changes.map((change) => (change.op === 'move' ? { ...change, path: moved(change.path), to: moved(change.to) } : { ...change, path: moved(change.path) })),
171
+ },
172
+ };
173
+ }
171
174
  export function changedPaths(changes) {
172
- return [
173
- ...new Set(changes.flatMap((change) => (change.op === 'rename' ? [change.path, change.to] : [change.path]))),
174
- ].sort();
175
+ return [...new Set(changes.flatMap((change) => (change.op === 'move' ? [change.path, change.to] : [change.path])))].sort();
176
+ }
177
+ function batched(changes) {
178
+ const batches = [];
179
+ let batch = [];
180
+ let files = 0;
181
+ let bytes = 0;
182
+ for (const change of changes) {
183
+ const named = change.op === 'move' ? 2 : 1;
184
+ const size = Buffer.byteLength(JSON.stringify(change));
185
+ if (batch.length > 0 && (files + named > BATCH_FILES || bytes + size > BATCH_BYTES)) {
186
+ batches.push(batch);
187
+ batch = [];
188
+ files = 0;
189
+ bytes = 0;
190
+ }
191
+ batch.push(change);
192
+ files += named;
193
+ bytes += size;
194
+ }
195
+ if (batch.length > 0)
196
+ batches.push(batch);
197
+ return batches;
175
198
  }
176
- function movedPaths(moves, before, after) {
177
- const pairs = moves.filter(({ from, to }) => before.has(from) && !after.has(from) && after.has(to) && !before.has(to));
178
- return new Map(pairs
179
- .filter((pair) => pairs.filter((other) => other.from === pair.from || other.to === pair.to).length === 1)
180
- .map(({ from, to }) => [from, to]));
199
+ function recordedFiles(copy) {
200
+ const found = new Map();
201
+ const seen = new Set();
202
+ for (let manifest = copy.manifests[copy.house]; manifest !== undefined; manifest = manifest.parent === null ? undefined : copy.manifests[manifest.parent]) {
203
+ for (const [path, revision] of Object.entries(manifest.files)) {
204
+ if (seen.has(path))
205
+ continue;
206
+ seen.add(path);
207
+ const held = manifest.binary?.[path];
208
+ if (revision !== null)
209
+ found.set(path, held === undefined ? { revision } : { revision, held });
210
+ }
211
+ }
212
+ return found;
213
+ }
214
+ function recordedRevisions(copy, path) {
215
+ return [...new Set(Object.values(copy.manifests).flatMap((manifest) => (typeof manifest.files[path] === 'string' ? [manifest.files[path]] : [])))];
216
+ }
217
+ class Applying {
218
+ present = new Map();
219
+ binaries = [];
220
+ gone = [];
221
+ repository;
222
+ manifest;
223
+ scope;
224
+ staging;
225
+ report;
226
+ fetching;
227
+ bytesOf;
228
+ constructor(repository, manifest, scope, staging, report, fetching, bytesOf) {
229
+ this.repository = repository;
230
+ this.manifest = manifest;
231
+ this.scope = scope;
232
+ this.staging = staging;
233
+ this.report = report;
234
+ this.fetching = fetching;
235
+ this.bytesOf = bytesOf;
236
+ }
237
+ async start() {
238
+ this.present = await scanned(this.repository);
239
+ }
240
+ nameOf(path) {
241
+ return this.present.get(path)?.name ?? path;
242
+ }
243
+ touch(path) {
244
+ if (!this.report.touched.has(path))
245
+ this.report.touched.set(path, this.present.get(path)?.sha256 ?? null);
246
+ }
247
+ async put(path, content) {
248
+ this.touch(path);
249
+ await placed(this.staging, join(this.repository, this.nameOf(path)), content);
250
+ this.present.set(path, { name: this.nameOf(path), sha256: digest(content), bytes: Buffer.byteLength(content) });
251
+ }
252
+ async drop(path) {
253
+ this.touch(path);
254
+ await removed(this.repository, this.nameOf(path));
255
+ this.present.delete(path);
256
+ }
257
+ async readOnly(path, content, revision) {
258
+ const sha256 = digest(content);
259
+ if (this.present.get(path)?.sha256 !== sha256)
260
+ await this.put(path, content);
261
+ this.manifest.files[path] = { revision, sha256, bytes: Buffer.byteLength(content), read_only: true };
262
+ }
263
+ async text({ path, content, revision, provenance }) {
264
+ if (provenance !== undefined)
265
+ await this.readOnly(sidecar(path), provenance, revision);
266
+ if (!declaredWritable(path, this.scope)) {
267
+ await this.readOnly(path, content, revision);
268
+ return;
269
+ }
270
+ const entry = this.manifest.files[path];
271
+ if (entry !== undefined && !entry.read_only && entry.revision === revision)
272
+ return;
273
+ const sha256 = digest(content);
274
+ const kept = { revision, sha256, bytes: Buffer.byteLength(content) };
275
+ const local = this.present.get(path);
276
+ if (local?.sha256 === sha256) {
277
+ this.manifest.files[path] = kept;
278
+ return;
279
+ }
280
+ const unchanged = entry === undefined ? local === undefined : entry.read_only === true || (entry.conflicted === undefined && local?.sha256 === entry.sha256);
281
+ if (!unchanged || (local !== undefined && !(await holding(this.repository, local.name, local.sha256)))) {
282
+ this.report.kept.add(path);
283
+ return;
284
+ }
285
+ await this.put(path, content);
286
+ this.manifest.files[path] = kept;
287
+ }
288
+ binary(change) {
289
+ this.binaries.push(change);
290
+ }
291
+ removal(path) {
292
+ this.gone.push(path);
293
+ }
294
+ async finish() {
295
+ const leaving = new Map();
296
+ for (const path of this.gone) {
297
+ const entry = this.manifest.files[path];
298
+ if (entry === undefined || entry.skipped || isDocument(path) || this.present.get(path)?.sha256 !== entry.sha256)
299
+ continue;
300
+ leaving.set(sameBytes(entry), leaving.has(sameBytes(entry)) ? null : path);
301
+ }
302
+ const taken = new Set();
303
+ const movedFrom = (change) => {
304
+ const from = leaving.get(sameBytes(change));
305
+ if (from === undefined || from === null || taken.has(from))
306
+ return undefined;
307
+ taken.add(from);
308
+ return from;
309
+ };
310
+ for (const change of this.binaries)
311
+ await this.attachment(change, movedFrom);
312
+ for (const path of this.gone) {
313
+ if (taken.has(path))
314
+ continue;
315
+ await this.removedHere(path);
316
+ if (this.manifest.files[sidecar(path)]?.read_only)
317
+ await this.removedHere(sidecar(path));
318
+ }
319
+ }
320
+ async attachment(change, movedFrom) {
321
+ const { path, revision, bytes, sha256 } = change;
322
+ const entry = this.manifest.files[path];
323
+ if (entry !== undefined && entry.revision === revision)
324
+ return;
325
+ const held = { revision, sha256, bytes, ...(declaredWritable(path, this.scope) ? {} : { read_only: true }) };
326
+ const local = this.present.get(path);
327
+ if (local?.sha256 === sha256) {
328
+ this.manifest.files[path] = held;
329
+ return;
330
+ }
331
+ const holds = entry !== undefined && !entry.skipped;
332
+ if (holds && !entry.read_only && local?.sha256 !== entry.sha256) {
333
+ this.report.kept.add(path);
334
+ return;
335
+ }
336
+ if (!holds && local !== undefined) {
337
+ this.report.kept.add(path);
338
+ return;
339
+ }
340
+ const from = movedFrom(change);
341
+ if (from !== undefined) {
342
+ const target = join(this.repository, this.nameOf(path));
343
+ this.touch(path);
344
+ await mkdir(dirname(target), { recursive: true });
345
+ await rename(join(this.repository, this.nameOf(from)), target);
346
+ await removed(this.repository, this.nameOf(from));
347
+ this.present.set(path, { name: this.nameOf(path), sha256, bytes });
348
+ this.present.delete(from);
349
+ delete this.manifest.files[from];
350
+ this.manifest.files[path] = held;
351
+ return;
352
+ }
353
+ if (!holds && !(this.fetching.attachments && bytes <= this.fetching.maxSize * MEGABYTE)) {
354
+ this.manifest.files[path] = { ...held, skipped: true };
355
+ return;
356
+ }
357
+ const file = await this.bytesOf(change);
358
+ const target = join(this.repository, this.nameOf(path));
359
+ this.touch(path);
360
+ await mkdir(dirname(target), { recursive: true });
361
+ await rename(file, target);
362
+ this.present.set(path, { name: this.nameOf(path), sha256, bytes });
363
+ this.manifest.files[path] = held;
364
+ }
365
+ async removedHere(path) {
366
+ const entry = this.manifest.files[path];
367
+ if (entry === undefined)
368
+ return;
369
+ const local = this.present.get(path);
370
+ if (entry.skipped || local === undefined) {
371
+ delete this.manifest.files[path];
372
+ return;
373
+ }
374
+ if (!entry.read_only && (local.sha256 !== entry.sha256 || !(await holding(this.repository, local.name, entry.sha256)))) {
375
+ this.report.kept.add(path);
376
+ return;
377
+ }
378
+ await this.drop(path);
379
+ delete this.manifest.files[path];
380
+ }
181
381
  }
182
382
  export class LocalCopies {
183
383
  owner;
184
384
  house;
385
+ launched;
185
386
  copies = new Map();
186
387
  queues = new Map();
388
+ waiting = new Set();
187
389
  answering = Promise.resolve();
188
- constructor(house) {
390
+ constructor(house, launched) {
189
391
  this.house = house;
392
+ this.launched = launched;
190
393
  this.owner = {
191
394
  key: 'owner',
192
395
  submit: async (operation, changes) => {
193
396
  try {
194
- const outcome = await retrying(() => house.post('/kit/door/batch', { operation_id: operation, changes }));
195
- return { accepted: outcome.contents };
397
+ const outcome = await retrying(() => stopping(house.door('update', { operation_id: operation, changes })));
398
+ return 'refused' in outcome ? outcome : { accepted: outcome.contents };
196
399
  }
197
400
  catch (error) {
198
401
  if (!finalRefusal(error))
199
402
  throw error;
200
403
  return {
201
- refused: refusalOf(error.text) ?? { code: '', text: `House refused it with ${error.status}`, conflicts: [] },
404
+ refused: refusalOf(error.text) ?? { code: '', text: `House refused it with ${error.status}`, conflicts: [], detail: {} },
202
405
  };
203
406
  }
204
407
  },
408
+ finish: async (upload, _path, partSha256) => {
409
+ try {
410
+ return (await house.act('uploads/finish', { upload, part_sha256: partSha256 }));
411
+ }
412
+ catch (error) {
413
+ if (!(error instanceof HouseRefusal))
414
+ throw new Unsettled(causeOf(error));
415
+ throw retryable(error.text) === false ? new Error(houseCause(error)) : new Unsettled(houseCause(error));
416
+ }
417
+ },
418
+ info: async (path, revision) => {
419
+ const read = toolAnswer({
420
+ status: 200,
421
+ text: await ownAgentCall(rpc('tools/call', { name: 'info', arguments: { path, ...(revision === undefined ? {} : { ref: revision }) } })),
422
+ });
423
+ if ('refused' in read)
424
+ throw new Error(read.refused.text);
425
+ return fileInfo(read.text);
426
+ },
205
427
  };
206
428
  }
207
429
  async load() {
@@ -215,25 +437,17 @@ export class LocalCopies {
215
437
  this.copies.set(copy.key, copy);
216
438
  }
217
439
  }
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();
440
+ async swept() {
441
+ await sweptIn(roomsFolder());
224
442
  }
225
443
  connected() {
226
444
  return this.selected();
227
445
  }
228
- async changed(rooms) {
229
- if (rooms.length > 0)
230
- await this.selected(rooms);
231
- }
232
- fetch() {
233
- return this.answered(() => this.fetched(true));
446
+ download(caller, fetching) {
447
+ return this.answered(() => this.fetched(true, caller, fetching));
234
448
  }
235
- async selected(rooms) {
236
- const copied = await this.answered(() => this.fetched(false, rooms));
449
+ async selected() {
450
+ const copied = await this.answered(() => this.fetched(false, this.owner, HELD));
237
451
  if (copied.refused)
238
452
  logged(copied.text);
239
453
  }
@@ -242,16 +456,15 @@ export class LocalCopies {
242
456
  this.answering = answered.catch(() => undefined);
243
457
  return answered;
244
458
  }
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 () => {
459
+ async fetched(refreshing, caller, fetching) {
460
+ const { local_copy: answer } = await this.house.act('local-copies/selection/read', {});
461
+ const synced = new Map(answer.rooms.map((room) => [`${ROOMS}${room.room_ref}`, room.room_handle]));
462
+ return this.holding([...new Set([...this.copies.keys(), ...synced.keys()])].sort(), async () => {
463
+ const chosen = await this.launched();
251
464
  const said = [];
252
465
  let refused = false;
253
466
  const mark = refreshing ? 'said' : 'unsaid';
254
- for (const copy of held()) {
467
+ for (const copy of [...this.copies.values()]) {
255
468
  if (synced.has(copy.key) || copy.unfollowed === 'said' || copy.unfollowed === mark)
256
469
  continue;
257
470
  await this.save({ ...copy, unfollowed: mark });
@@ -259,75 +472,115 @@ export class LocalCopies {
259
472
  said.push(`${rootOf(copy.room_handle)}: no longer followed; kept as it is`);
260
473
  }
261
474
  const current = [];
475
+ const wanted = new Map();
262
476
  for (const [key, handle] of synced) {
263
477
  try {
264
478
  const held = this.copies.get(key);
265
479
  if (held === undefined || !(await exists(held.repository))) {
266
- said.push(await this.materialize(key, handle));
480
+ wanted.set(key, handle);
267
481
  continue;
268
482
  }
269
483
  const { unfollowed, ...followed } = held;
270
- current.push(unfollowed === undefined && held.room_handle === handle ? held : await this.save({ ...followed, room_handle: handle }));
484
+ const named = unfollowed === undefined && held.room_handle === handle ? held : await this.save({ ...followed, room_handle: handle });
485
+ current.push(await this.renamed(named, chosen));
271
486
  }
272
487
  catch (error) {
273
488
  refused = true;
274
489
  said.push(`${rootOf(handle)}: ${causeOf(error)}`);
275
490
  }
276
491
  }
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)));
492
+ const unready = [];
493
+ const unsettled = [];
494
+ if (wanted.size > 0) {
495
+ const { authorities } = await this.discovered([...wanted.values()]);
496
+ for (const [key, handle] of wanted) {
497
+ const authority = authorities.find((entry) => `${ROOMS}${entry.room_ref}` === key);
498
+ try {
499
+ if (authority === undefined)
500
+ throw new Error('House serves nothing of it to copy');
501
+ said.push(await this.copied({ key, handle, authority }, fetching, chosen));
502
+ }
503
+ catch (error) {
504
+ if (error instanceof NotReady) {
505
+ unready.push(...error.rooms);
506
+ said.push(`${rootOf(handle)}: ${NOT_READY}`);
507
+ }
508
+ else {
509
+ refused = true;
510
+ unsettled.push(key.slice(ROOMS.length));
511
+ said.push(`${rootOf(handle)}: ${causeOf(error)}`);
512
+ }
513
+ }
514
+ }
515
+ }
516
+ const reading = refreshing ? current : current.filter((copy) => this.waiting.has(copy.room_ref));
517
+ if (reading.length > 0) {
518
+ const reports = new Map(reading.map((copy) => [copy.key, { touched: new Map(), kept: new Set() }]));
519
+ const unconverted = new Map();
520
+ const failed = await this.refreshed(reading, unready, unconverted, caller, fetching, reports).then(() => null, houseCause);
521
+ for (const copy of reading) {
522
+ const cause = unconverted.get(copy.key);
523
+ if (cause !== undefined) {
524
+ refused = true;
525
+ unsettled.push(copy.room_ref);
526
+ said.push(cause);
527
+ }
528
+ else if (unready.includes(copy.room_ref))
529
+ said.push(`${rootOf(copy.room_handle)}: ${NOT_READY}`);
530
+ else
531
+ said.push(...(await this.reported(this.copies.get(copy.key), reports.get(copy.key))));
532
+ }
281
533
  if (failed !== null) {
282
534
  refused = true;
535
+ unsettled.push(...reading.map((copy) => copy.room_ref));
283
536
  said.push(failed);
284
537
  }
285
538
  }
539
+ for (const room of [...this.waiting]) {
540
+ if (!unready.includes(room) && !unsettled.includes(room))
541
+ this.waiting.delete(room);
542
+ }
543
+ const due = unready.filter((room) => !this.waiting.has(room));
544
+ for (const room of due)
545
+ this.waiting.add(room);
546
+ if (due.length > 0)
547
+ void this.askedAgain(due);
286
548
  return { refused, text: said.length === 0 ? 'Nothing new from House.' : said.join('\n') };
287
549
  });
288
550
  }
289
- async observed(report) {
290
- const key = await this.keyAt(dirname(report.common));
291
- if (key === undefined)
292
- return;
293
- await this.exclusive(key, async () => {
294
- const copy = this.copies.get(key);
295
- let moves = copy.moves;
296
- for (const { from, to } of report.moves) {
297
- const held = moves.find((move) => move.worktree === report.worktree && move.to === from);
298
- moves = moves.filter((move) => move !== held);
299
- if (held === undefined)
300
- moves.push({ worktree: report.worktree, from, to });
301
- else if (held.from !== to)
302
- moves.push({ ...held, to });
303
- }
304
- if (moves.some((move) => move.worktree === report.worktree)) {
305
- const index = new Set((await git(report.worktree, ['ls-files', '-z'])).split('\0'));
306
- const head = await tree(report.worktree, 'HEAD').catch(() => new Map());
307
- const live = ({ from, to }) => (index.has(to) && !index.has(from)) || (head.has(to) && !head.has(from));
308
- moves = moves.filter((move) => move.worktree !== report.worktree || live(move));
309
- }
310
- if (JSON.stringify(moves) !== JSON.stringify(copy.moves))
311
- await this.save({ ...copy, moves });
312
- });
551
+ async reported(copy, report) {
552
+ const root = rootOf(copy.room_handle);
553
+ let updated = 0;
554
+ for (const [path, before] of report.touched)
555
+ if (((await hashed(join(copy.repository, path)))?.sha256 ?? null) !== before)
556
+ updated++;
557
+ return [
558
+ ...(updated === 0 ? [] : [`${root}: ${counted(updated, 'file')} updated from House`]),
559
+ ...[...report.kept].sort().map((path) => `${root}/${path}: changed here, so House's change was not brought; the next house upload merges it`),
560
+ ];
313
561
  }
314
- async push(cwd, selector, caller) {
315
- const located = await run(cwd, ['rev-parse', '--path-format=absolute', '--git-common-dir', '--show-toplevel']);
316
- if (located.status !== 0)
317
- return outside(cwd);
318
- const [common, worktree] = located.stdout.toString('utf8').split('\n');
319
- const key = await this.keyAt(dirname(common));
320
- if (key === undefined)
321
- return outside(worktree);
322
- return this.exclusive(key, () => this.pushed(key, worktree, selector, caller));
323
- }
324
- async keyAt(repository) {
325
- const real = await realpath(repository).catch(() => repository);
326
- for (const copy of this.copies.values()) {
327
- if ((await realpath(copy.repository).catch(() => copy.repository)) === real)
328
- return copy.key;
562
+ async askedAgain(rooms) {
563
+ let asking = rooms;
564
+ for (let step = 0; asking.length > 0; step++) {
565
+ await paused(ASKED_AGAIN_MS[Math.min(step, ASKED_AGAIN_MS.length - 1)]);
566
+ await this.selected().catch(logged);
567
+ asking = asking.filter((room) => this.waiting.has(room));
568
+ }
569
+ }
570
+ upload(cwd, caller) {
571
+ return this.keyAt(cwd).then((key) => (key === undefined ? outside(cwd) : this.exclusive(key, () => this.uploaded(key, caller))));
572
+ }
573
+ async keyAt(cwd) {
574
+ const held = new Map();
575
+ for (const copy of this.copies.values())
576
+ held.set(await realpath(copy.repository).catch(() => copy.repository), copy.key);
577
+ for (let folder = await realpath(cwd).catch(() => cwd);; folder = dirname(folder)) {
578
+ const key = held.get(folder);
579
+ if (key !== undefined)
580
+ return key;
581
+ if (dirname(folder) === folder)
582
+ return undefined;
329
583
  }
330
- return undefined;
331
584
  }
332
585
  exclusive(key, work) {
333
586
  const queued = (this.queues.get(key) ?? Promise.resolve()).then(work);
@@ -347,132 +600,298 @@ export class LocalCopies {
347
600
  this.copies.set(copy.key, copy);
348
601
  return copy;
349
602
  }
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;
603
+ async discovered(handles) {
604
+ const discover = (group) => this.house.door('rooms/discover', { prefixes: group.map(rootOf) });
605
+ const [first, ...rest] = handles;
606
+ const { bounds, authorities } = await discover([first]);
607
+ for (const group of grouped(rest, bounds.max_prefixes))
608
+ authorities.push(...(await discover(group)).authorities);
609
+ return { bounds, authorities };
610
+ }
611
+ async chunked(root, room, directory) {
612
+ await mkdir(directory, { recursive: true });
613
+ for (let renewed = false;; renewed = true) {
614
+ const { rooms } = await this.house.door('room-chunks/list', { prefixes: [root] });
615
+ const list = rooms.find((entry) => entry.room_ref === room);
616
+ if (list === undefined)
617
+ throw new Error('House serves nothing of it to copy');
618
+ if (!list.ready)
619
+ throw new NotReady([room]);
620
+ const held = new Set(await readdir(directory));
621
+ const failure = await stagedChunks(directory, list.chunks.filter(({ hash }) => !held.has(hash)));
622
+ if (failure === undefined) {
623
+ return { position: list.position, log_epoch: list.log_epoch, entries: chunkEntries(directory, list.chunks, list.models) };
624
+ }
625
+ if (renewed && (await readdir(directory)).length === held.size)
626
+ throw failure;
627
+ }
628
+ }
629
+ chunksFolder() {
630
+ return join(roomsFolder(), `${stagingPrefix()}${randomUUID()}`);
631
+ }
632
+ async setAside(path, chosen) {
633
+ if ((await found(path)) === null)
634
+ return;
635
+ if (chosen.some((folder) => within(folder, path)))
636
+ throw new Error(`${path} holds a folder an Agent works in, so Kit leaves it as it is`);
637
+ const kept = await keptName(path);
638
+ await rename(path, kept);
639
+ for (const copy of [...this.copies.values()])
640
+ if (within(copy.repository, path))
641
+ await this.save(rerooted(copy, path, kept));
642
+ }
643
+ async laidOut(earlier, rooms, kept) {
644
+ const folders = (await readdir(earlier, { withFileTypes: true }).catch(() => []))
645
+ .filter((entry) => entry.isDirectory() && !entry.name.startsWith('.'))
646
+ .map((entry) => join(earlier, entry.name));
647
+ const held = (folder) => [...this.copies.values()].find((copy) => copy.repository === folder);
648
+ const followed = (folder) => held(folder)?.unfollowed === undefined && held(folder) !== undefined;
649
+ const staying = folders.filter((folder) => kept.some((chosen) => within(chosen, folder)));
650
+ const moving = folders.filter((folder) => !staying.includes(folder));
651
+ await mkdir(rooms, { recursive: true, mode: 0o700 });
652
+ for (const source of [...moving.filter(followed), ...moving.filter((folder) => !followed(folder))]) {
653
+ const copy = held(source);
654
+ let target;
655
+ if (copy !== undefined && copy.unfollowed === undefined) {
656
+ target = join(rooms, copy.room_handle);
657
+ await this.setAside(target, kept);
376
658
  }
377
659
  else {
378
- read.readOnly.set(file.path, content);
660
+ target = await keptName(join(rooms, basename(source)));
379
661
  }
380
- if (file.provenance !== undefined)
381
- read.readOnly.set(sidecar(file.path), file.provenance);
662
+ await carried(source, target);
663
+ if (copy !== undefined)
664
+ await this.save(rerooted(copy, source, target));
382
665
  }
383
- return read;
666
+ await sweptIn(earlier);
667
+ await removeFolder(earlier);
668
+ await removeFolder(dirname(earlier));
669
+ return { moved: moving.length > 0, kept: staying };
384
670
  }
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);
390
- if (await exists(repository))
391
- throw new Error(`${repository} already exists and Kit leaves it as it is`);
392
- const staging = join(dirname(repository), `.${randomUUID()}`);
393
- await mkdir(staging, { recursive: true });
394
- let house;
395
- let readOnly;
671
+ async renamed(copy, chosen) {
672
+ const folder = join(roomsFolder(), copy.room_handle);
673
+ if (dirname(copy.repository) !== roomsFolder() || basename(copy.repository) === copy.room_handle)
674
+ return copy;
675
+ if (chosen.some((launch) => within(launch, copy.repository)))
676
+ return copy;
677
+ await this.setAside(folder, chosen);
678
+ await rename(copy.repository, folder);
679
+ return this.save(rerooted(copy, copy.repository, folder));
680
+ }
681
+ async materialized({ key, handle, authority }, whole, chosen) {
682
+ const repository = join(roomsFolder(), handle);
683
+ await this.setAside(repository, chosen);
684
+ const folder = join(dirname(repository), `${stagingPrefix()}${randomUUID()}`);
685
+ const chunks = this.chunksFolder();
686
+ const scope = { write: authority.operations.write, protected: authority.protected_paths };
687
+ const manifest = { room_ref: authority.room_ref, files: {} };
688
+ await mkdir(folder, { recursive: true });
689
+ let copy;
396
690
  try {
397
- await git(staging, ['init', '-q', '-b', BRANCH]);
398
- house = await importCommits(staging, [
399
- { message: `House ${rootOf(handle)}`, from: null, files: read.writable },
400
- ]);
401
- await git(staging, ['update-ref', `refs/heads/${BRANCH}`, house]);
402
- await git(staging, ['reset', '-q', '--hard']);
403
- readOnly = await readOnlyPlaced(staging, [], read.readOnly, []);
404
- await ignoreRules(staging, [...read.writable.keys()], read);
405
- await rename(staging, repository);
691
+ const read = await whole(chunks);
692
+ const work = await staging();
693
+ for await (const entry of read.entries) {
694
+ if (!('content' in entry)) {
695
+ manifest.files[entry.path] = {
696
+ revision: entry.revision,
697
+ sha256: entry.sha256,
698
+ bytes: entry.bytes,
699
+ skipped: true,
700
+ ...(declaredWritable(entry.path, scope) ? {} : { read_only: true }),
701
+ };
702
+ continue;
703
+ }
704
+ const { path, content, revision, provenance } = entry;
705
+ await placed(work, join(folder, path), content);
706
+ manifest.files[path] = {
707
+ revision,
708
+ sha256: digest(content),
709
+ bytes: Buffer.byteLength(content),
710
+ ...(declaredWritable(path, scope) ? {} : { read_only: true }),
711
+ };
712
+ if (provenance !== undefined) {
713
+ await placed(work, join(folder, sidecar(path)), provenance);
714
+ manifest.files[sidecar(path)] = { revision, sha256: digest(provenance), bytes: Buffer.byteLength(provenance), read_only: true };
715
+ }
716
+ }
717
+ await writeManifest(folder, work, manifest);
718
+ await rename(folder, repository);
719
+ copy = { key, room_ref: authority.room_ref, room_handle: handle, repository, position: read.position, log_epoch: read.log_epoch, ...scope };
406
720
  }
407
721
  catch (error) {
408
- await rm(staging, { recursive: true, force: true });
722
+ await rm(folder, { recursive: true, force: true });
409
723
  throw error;
410
724
  }
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}`;
725
+ finally {
726
+ await rm(chunks, { recursive: true, force: true });
727
+ }
728
+ return this.save(copy);
426
729
  }
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;
730
+ async copied(wanted, fetching, chosen) {
731
+ const { handle, authority } = wanted;
732
+ const root = rootOf(handle);
733
+ let answer = null;
734
+ try {
735
+ if (!this.waiting.has(authority.room_ref)) {
736
+ answer = await this.house.door('changes/enumerate', {
737
+ prefixes: [root],
738
+ positions: [
739
+ { authority: authority.room_ref, position: '0', log_epoch: authority.log_epoch, page: { through: authority.position, read: 0 } },
740
+ ],
741
+ });
742
+ }
743
+ }
744
+ catch (error) {
745
+ if (expiredAuthority(error) !== authority.room_ref)
746
+ throw error;
747
+ }
748
+ const view = answer?.authorities.find((entry) => entry.authority === authority.room_ref);
749
+ if (answer !== null && view === undefined)
750
+ throw new Error('House serves nothing of it to copy');
751
+ if (view !== undefined && view.next === undefined) {
752
+ const entries = view.changes.filter((change) => !('removed' in change));
753
+ const copy = await this.materialized(wanted, async () => ({ position: view.position, log_epoch: view.log_epoch, entries }), chosen);
754
+ await this.attached(copy, this.owner, fetching, { touched: new Map(), kept: new Set() });
755
+ return `${root}: copied to ${copy.repository}`;
756
+ }
757
+ const copy = await this.materialized(wanted, (directory) => this.chunked(root, authority.room_ref, directory), chosen);
758
+ const report = { touched: new Map(), kept: new Set() };
759
+ const [caught] = await this.caughtUp([copy], true, this.owner, fetching, new Map([[copy.key, report]]));
760
+ await this.attached(caught, this.owner, fetching, report);
761
+ return `${root}: copied to ${copy.repository}`;
448
762
  }
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);
763
+ applying(copy, manifest, scope, work, report, fetching, caller) {
764
+ const root = rootOf(copy.room_handle);
765
+ return new Applying(copy.repository, manifest, scope, work, report, fetching, (change) => receivedBinary(work, `${root}/${change.path}`, async () => {
766
+ const file = await caller.info(`${root}/${change.path}`, change.revision);
767
+ if (file === null)
768
+ throw new Error(`House holds no attachment at ${root}/${change.path}`);
769
+ return file;
770
+ }));
771
+ }
772
+ async received(copy, authority, caller, fetching, report) {
773
+ const root = rootOf(copy.room_handle);
774
+ const directory = this.chunksFolder();
775
+ const scope = { write: authority.operations.write, protected: authority.protected_paths };
776
+ try {
777
+ const read = await this.chunked(root, copy.room_ref, directory);
778
+ const manifest = await readManifest(copy.repository);
779
+ const work = await staging();
780
+ const applying = this.applying(copy, manifest, scope, work, report, fetching, caller);
781
+ await applying.start();
782
+ try {
783
+ const whole = new Set();
784
+ for await (const entry of read.entries) {
785
+ whole.add(entry.path);
786
+ if (!('content' in entry)) {
787
+ applying.binary(entry);
788
+ continue;
789
+ }
790
+ if (entry.provenance !== undefined)
791
+ whole.add(sidecar(entry.path));
792
+ await applying.text(entry);
793
+ }
794
+ for (const path of Object.keys(manifest.files))
795
+ if (!whole.has(path))
796
+ applying.removal(path);
797
+ await applying.finish();
798
+ }
799
+ finally {
800
+ await writeManifest(copy.repository, work, manifest);
801
+ }
802
+ return { ...copy, ...scope, position: read.position, log_epoch: read.log_epoch };
803
+ }
804
+ finally {
805
+ await rm(directory, { recursive: true, force: true });
806
+ }
807
+ }
808
+ async reread(copy, caller, fetching, report) {
809
+ const { authorities } = await this.discovered([copy.room_handle]);
810
+ const authority = authorities.find((entry) => entry.room_ref === copy.room_ref);
811
+ if (authority === undefined)
812
+ throw new NotReady([copy.room_ref]);
813
+ return this.received(copy, authority, caller, fetching, report);
814
+ }
815
+ async refreshed(held, unready, unconverted, caller, fetching, reports) {
816
+ const copies = [];
817
+ for (const copy of held) {
818
+ try {
819
+ copies.push(await this.converted(copy));
820
+ }
821
+ catch (error) {
822
+ if (error instanceof NotReady)
823
+ unready.push(...error.rooms);
824
+ else
825
+ unconverted.set(copy.key, causeOf(error));
826
+ }
827
+ }
828
+ if (copies.length === 0)
829
+ return;
830
+ const { bounds, authorities } = await this.discovered(copies.map((copy) => copy.room_handle));
831
+ const read = [];
455
832
  const unchanged = [];
456
833
  for (const copy of copies) {
457
834
  const authority = authorities.find((entry) => entry.room_ref === copy.room_ref);
458
835
  if (authority !== undefined &&
459
836
  JSON.stringify([authority.operations.write, authority.protected_paths]) !== JSON.stringify([copy.write, copy.protected])) {
460
- await this.save(await this.received(copy));
837
+ try {
838
+ read.push(await this.save(await this.received(copy, authority, caller, fetching, reports.get(copy.key))));
839
+ }
840
+ catch (error) {
841
+ if (!(error instanceof NotReady))
842
+ throw error;
843
+ unready.push(...error.rooms);
844
+ }
461
845
  }
462
846
  else {
463
847
  unchanged.push(copy);
464
848
  }
465
849
  }
466
850
  for (const group of grouped(unchanged, bounds.max_prefixes))
467
- await this.caughtUp(group, false);
851
+ await this.caughtUp(group, false, caller, fetching, reports, unready);
852
+ for (const group of grouped(read, bounds.max_prefixes))
853
+ await this.caughtUp(group, true, caller, fetching, reports, unready);
854
+ for (const copy of copies) {
855
+ if (!unready.includes(copy.room_ref))
856
+ await this.attached(this.copies.get(copy.key), caller, fetching, reports.get(copy.key));
857
+ }
858
+ }
859
+ async attached(copy, caller, fetching, report) {
860
+ if (!fetching.attachments)
861
+ return;
862
+ const manifest = await readManifest(copy.repository);
863
+ const wanted = Object.entries(manifest.files).filter(([, entry]) => entry.skipped && entry.bytes <= fetching.maxSize * MEGABYTE);
864
+ if (wanted.length === 0)
865
+ return;
866
+ const root = rootOf(copy.room_handle);
867
+ const work = await staging();
868
+ const present = await scanned(copy.repository);
869
+ for (const [path, { skipped: _skipped, ...entry }] of wanted) {
870
+ if (present.has(path))
871
+ continue;
872
+ const file = await receivedBinary(work, `${root}/${path}`, async () => {
873
+ const held = await caller.info(`${root}/${path}`, entry.revision);
874
+ if (held === null)
875
+ throw new Error(`House holds no attachment at ${root}/${path}`);
876
+ return held;
877
+ });
878
+ if (!report.touched.has(path))
879
+ report.touched.set(path, null);
880
+ await mkdir(dirname(join(copy.repository, path)), { recursive: true });
881
+ await rename(file, join(copy.repository, path));
882
+ manifest.files[path] = entry;
883
+ }
884
+ await writeManifest(copy.repository, work, manifest);
468
885
  }
469
- async caughtUp(copies, whole) {
886
+ async caughtUp(copies, whole, caller, fetching, reports, unready) {
470
887
  const caught = new Map(copies.map((copy) => [copy.key, copy]));
888
+ const read = new Set();
889
+ const waiting = unready ?? [];
471
890
  let asking = [...copies];
472
891
  while (asking.length > 0) {
473
892
  let answer;
474
893
  try {
475
- answer = await this.house.post('/kit/door/enumerate', {
894
+ answer = await this.house.door('changes/enumerate', {
476
895
  prefixes: asking.map((copy) => rootOf(copy.room_handle)),
477
896
  positions: asking.map((copy) => ({ authority: copy.room_ref, position: copy.position, log_epoch: copy.log_epoch })),
478
897
  });
@@ -481,8 +900,22 @@ export class LocalCopies {
481
900
  const expired = asking.find((copy) => copy.room_ref === expiredAuthority(error));
482
901
  if (expired === undefined)
483
902
  throw error;
484
- caught.set(expired.key, await this.save(await this.received(expired)));
485
903
  asking = asking.filter((copy) => copy !== expired);
904
+ if (read.has(expired.key)) {
905
+ waiting.push(expired.room_ref);
906
+ continue;
907
+ }
908
+ read.add(expired.key);
909
+ try {
910
+ const next = await this.save(await this.reread(expired, caller, fetching, reports.get(expired.key)));
911
+ caught.set(expired.key, next);
912
+ asking.push(next);
913
+ }
914
+ catch (refused) {
915
+ if (!(refused instanceof NotReady))
916
+ throw refused;
917
+ waiting.push(expired.room_ref);
918
+ }
486
919
  continue;
487
920
  }
488
921
  const advanced = [];
@@ -490,371 +923,599 @@ export class LocalCopies {
490
923
  const view = answer.authorities.find((entry) => entry.authority === copy.room_ref);
491
924
  if (view === undefined || (view.position === copy.position && view.log_epoch === copy.log_epoch))
492
925
  continue;
493
- const next = await this.save(await this.incorporated(copy, view.changes, view.position, view.log_epoch));
926
+ const next = await this.save(await this.incorporated(copy, view, caller, fetching, reports.get(copy.key)));
494
927
  caught.set(copy.key, next);
495
928
  advanced.push(next);
496
929
  }
497
- asking = whole ? advanced : [];
930
+ asking = advanced.filter((copy) => whole || read.has(copy.key));
498
931
  }
932
+ if (unready === undefined && waiting.length > 0)
933
+ throw new NotReady(waiting);
499
934
  return copies.map((copy) => caught.get(copy.key));
500
935
  }
501
- async incorporated(copy, changes, position, logEpoch) {
502
- const houseTree = await tree(copy.repository, copy.house);
503
- const held = new Set(copy.read_only);
504
- const files = new Map();
505
- const revisions = {};
506
- const written = new Map();
507
- const removed = [];
508
- for (const change of changes) {
509
- const gone = 'removed' in change;
510
- if (houseTree.has(change.path) || (!gone && !held.has(change.path) && declaredWritable(change.path, copy))) {
511
- const unchanged = gone
512
- ? !houseTree.has(change.path)
513
- : houseTree.get(change.path) === blobId(change.content) &&
514
- revisionAt(copy, copy.house, change.path) === change.revision;
515
- if (unchanged)
936
+ async incorporated(copy, view, caller, fetching, report) {
937
+ const manifest = await readManifest(copy.repository);
938
+ const work = await staging();
939
+ const applying = this.applying(copy, manifest, copy, work, report, fetching, caller);
940
+ await applying.start();
941
+ try {
942
+ for (const change of view.changes) {
943
+ if ('removed' in change)
944
+ applying.removal(change.path);
945
+ else if ('content' in change)
946
+ await applying.text(change);
947
+ else
948
+ applying.binary(change);
949
+ }
950
+ await applying.finish();
951
+ }
952
+ finally {
953
+ await writeManifest(copy.repository, work, manifest);
954
+ }
955
+ return { ...copy, position: view.position, log_epoch: view.log_epoch };
956
+ }
957
+ async converted(held) {
958
+ if (!earlier(held))
959
+ return held;
960
+ const copy = {
961
+ key: held.key,
962
+ room_ref: held.room_ref,
963
+ room_handle: held.room_handle,
964
+ repository: held.repository,
965
+ position: held.position,
966
+ log_epoch: held.log_epoch,
967
+ write: held.write,
968
+ protected: held.protected,
969
+ ...(held.unfollowed === undefined ? {} : { unfollowed: held.unfollowed }),
970
+ };
971
+ const root = rootOf(copy.room_handle);
972
+ const directory = this.chunksFolder();
973
+ const recorded = recordedFiles(held);
974
+ const manifest = { room_ref: copy.room_ref, files: {} };
975
+ const work = await staging();
976
+ try {
977
+ const read = await this.chunked(root, copy.room_ref, directory);
978
+ const present = await scanned(copy.repository);
979
+ const current = new Map();
980
+ const unbrought = new Map();
981
+ const binaries = [];
982
+ const readOnly = async (path, content, revision) => {
983
+ if (present.get(path)?.sha256 !== digest(content))
984
+ await placed(work, join(copy.repository, path), content);
985
+ manifest.files[path] = { revision, sha256: digest(content), bytes: Buffer.byteLength(content), read_only: true };
986
+ };
987
+ for await (const entry of read.entries) {
988
+ if (!('content' in entry)) {
989
+ binaries.push(entry);
990
+ continue;
991
+ }
992
+ if (entry.provenance !== undefined)
993
+ await readOnly(sidecar(entry.path), entry.provenance, entry.revision);
994
+ if (declaredWritable(entry.path, copy)) {
995
+ current.set(entry.path, { revision: entry.revision, sha256: digest(entry.content), bytes: Buffer.byteLength(entry.content) });
996
+ if (!present.has(entry.path))
997
+ unbrought.set(entry.path, entry.content);
998
+ }
999
+ else {
1000
+ await readOnly(entry.path, entry.content, entry.revision);
1001
+ }
1002
+ }
1003
+ for (const [path, { revision, held: attached }] of recorded) {
1004
+ if (manifest.files[path]?.read_only)
1005
+ continue;
1006
+ if (attached !== undefined) {
1007
+ manifest.files[path] = present.has(path) ? { revision, ...attached } : { revision, ...attached, skipped: true };
1008
+ continue;
1009
+ }
1010
+ const named = `${root}/${path}`;
1011
+ const now = current.get(path);
1012
+ const read = now?.revision === revision ? null : await this.revisionRead(named, revision);
1013
+ if (now?.revision !== revision && read === null)
1014
+ continue;
1015
+ const latest = now?.revision === revision ? now : { revision, sha256: digest(read), bytes: Buffer.byteLength(read) };
1016
+ const local = present.get(path);
1017
+ if (local === undefined) {
1018
+ await placed(work, join(copy.repository, path), read ?? unbrought.get(path));
1019
+ manifest.files[path] = latest;
1020
+ continue;
1021
+ }
1022
+ if (local.sha256 === latest.sha256) {
1023
+ manifest.files[path] = latest;
1024
+ continue;
1025
+ }
1026
+ const edited = await readFile(join(copy.repository, local.name), 'utf8');
1027
+ const latestText = read ?? (await this.revisionText(named, revision));
1028
+ let closest = { revision, text: latestText, apart: differing(latestText, edited) };
1029
+ for (const before of recordedRevisions(held, path).filter((other) => other !== revision)) {
1030
+ const text = await this.revisionRead(named, before);
1031
+ if (text === null)
1032
+ continue;
1033
+ const apart = differing(text, edited);
1034
+ if (apart < closest.apart)
1035
+ closest = { revision: before, text, apart };
1036
+ }
1037
+ if (closest.revision !== revision && closest.text === edited) {
1038
+ await placed(work, join(copy.repository, local.name), latestText);
1039
+ manifest.files[path] = latest;
1040
+ }
1041
+ else {
1042
+ manifest.files[path] = { revision: closest.revision, sha256: digest(closest.text), bytes: Buffer.byteLength(closest.text) };
1043
+ }
1044
+ }
1045
+ for (const [path, now] of current) {
1046
+ if (manifest.files[path] === undefined && present.get(path)?.sha256 === now.sha256)
1047
+ manifest.files[path] = now;
1048
+ }
1049
+ for (const { path, revision, bytes, sha256 } of binaries) {
1050
+ if (manifest.files[path] !== undefined)
516
1051
  continue;
517
- files.set(change.path, gone ? null : change.content);
518
- revisions[change.path] = gone ? null : change.revision;
1052
+ const writable = declaredWritable(path, copy) ? {} : { read_only: true };
1053
+ manifest.files[path] = present.get(path)?.sha256 === sha256 ? { revision, sha256, bytes, ...writable } : { revision, sha256, bytes, skipped: true, ...writable };
519
1054
  }
520
- else if (gone) {
521
- removed.push(change.path, sidecar(change.path));
1055
+ for (const [path, local] of present) {
1056
+ if (manifest.files[path] !== undefined || current.has(path))
1057
+ continue;
1058
+ if (await this.deletedByHouse(held, `${root}/${path}`, path, local.sha256))
1059
+ await removed(copy.repository, local.name);
522
1060
  }
523
- else {
524
- written.set(change.path, change.content);
525
- if (change.provenance !== undefined)
526
- written.set(sidecar(change.path), change.provenance);
527
- }
528
- }
529
- let { house, manifests } = copy;
530
- if (files.size > 0) {
531
- house = await importCommits(copy.repository, [
532
- { message: `House ${rootOf(copy.room_handle)} at position ${position}`, from: copy.house, files },
533
- ]);
534
- manifests = { ...manifests, [house]: { parent: copy.house, files: revisions } };
535
- }
536
- const next = {
537
- ...copy,
538
- house,
539
- manifests,
540
- position,
541
- log_epoch: logEpoch,
542
- read_only: await readOnlyPlaced(copy.repository, copy.read_only, written, removed),
543
- };
544
- await ignoreRules(copy.repository, [...(await tree(copy.repository, house)).keys()], next);
545
- return next;
546
- }
547
- async pushed(key, worktree, selector, caller) {
548
- let copy = this.copies.get(key);
549
- if (copy.push?.state === 'submitted')
550
- return this.settle(copy, caller);
551
- const selected = await run(worktree, ['rev-parse', '--verify', '--quiet', '--end-of-options', `${selector}^{commit}`]);
552
- if (selected.status !== 0)
553
- return { refused: true, text: `${selector} names no commit` };
554
- const commit = selected.stdout.toString('utf8').trim();
555
- if (copy.push !== undefined) {
556
- const held = copy.push.acknowledged;
557
- if (held === undefined || !(await succeeded(worktree, ['merge-base', '--is-ancestor', held, commit]))) {
558
- return this.finished(copy);
559
- }
560
- copy = await this.save(without(copy));
561
- }
562
- const listed = (await git(copy.repository, ['rev-list', '--topo-order', commit])).split('\n');
563
- const base = listed.find((ancestor) => ancestor in copy.manifests);
564
- if (base === undefined) {
565
- return { refused: true, text: `${short(commit)} holds no House state; merge or rebase onto ${HOUSE_REF}` };
566
- }
567
- let changes;
1061
+ for (const path of held.read_only)
1062
+ if (manifest.files[path] === undefined)
1063
+ await removed(copy.repository, path);
1064
+ for (const name of [...KIT_IGNORES, ...KIT_GIT])
1065
+ await rm(join(copy.repository, name), { recursive: true, force: true });
1066
+ await rmdir(join(copy.repository, '.git', 'refs', 'house')).catch(() => undefined);
1067
+ await writeManifest(copy.repository, work, manifest);
1068
+ }
1069
+ catch (error) {
1070
+ if (error instanceof NotReady)
1071
+ throw error;
1072
+ throw new Error(`${root}: Kit could not convert the copy an earlier Kit kept yet (${houseCause(error)}); the next house download or house upload converts it`);
1073
+ }
1074
+ finally {
1075
+ await rm(directory, { recursive: true, force: true });
1076
+ }
1077
+ return this.save(copy);
1078
+ }
1079
+ async uploaded(key, caller) {
1080
+ let copy;
568
1081
  try {
569
- changes = await this.derived(copy, base, commit);
1082
+ copy = await this.converted(this.copies.get(key));
1083
+ }
1084
+ catch (error) {
1085
+ return { refused: true, text: error instanceof NotReady ? `${rootOf(this.copies.get(key).room_handle)}: ${NOT_READY}` : causeOf(error) };
1086
+ }
1087
+ const lines = [];
1088
+ let files = 0;
1089
+ let landed = 0;
1090
+ if (copy.pending !== undefined) {
1091
+ const settled = await this.settle(copy, caller);
1092
+ if (settled.refused)
1093
+ return settled;
1094
+ files += settled.files;
1095
+ landed++;
1096
+ lines.push(...settled.lines);
1097
+ copy = this.copies.get(key);
1098
+ }
1099
+ const root = rootOf(copy.room_handle);
1100
+ const manifest = await readManifest(copy.repository);
1101
+ const present = await scanned(copy.repository);
1102
+ const unresolved = [];
1103
+ for (const [path, entry] of Object.entries(manifest.files)) {
1104
+ if (entry.conflicted && (await marked(copy.repository, present.get(path)?.name ?? path)))
1105
+ unresolved.push(`${root}/${path}`);
1106
+ }
1107
+ if (unresolved.length > 0) {
1108
+ const [named, they] = unresolved.length === 1 ? [`${unresolved[0]} still holds`, 'it'] : [`${unresolved.join(', ')} still hold`, 'each'];
1109
+ return { refused: true, text: `${named} conflict markers; resolve ${they}, then run house upload again` };
1110
+ }
1111
+ let built;
1112
+ try {
1113
+ built = await this.built(copy, manifest, present);
570
1114
  }
571
1115
  catch (error) {
572
1116
  return { refused: true, text: houseCause(error) };
573
1117
  }
574
- if (changes.length === 0)
575
- return { refused: false, text: `Nothing to submit: ${short(commit)} matches House.` };
576
- const branch = await git(worktree, ['symbolic-ref', '-q', 'HEAD']).then((name) => name.trim(), () => null);
577
- const submitted = await this.save({
578
- ...copy,
579
- push: {
580
- state: 'submitted',
581
- caller: caller.key,
582
- operation_id: operationId(),
583
- commit,
584
- base,
585
- changes,
586
- worktree,
587
- branch,
588
- house: copy.house,
589
- },
590
- });
591
- return this.settle(submitted, caller);
1118
+ if ('refused' in built)
1119
+ return built;
1120
+ const batches = batched(built.changes);
1121
+ for (const [index, changes] of batches.entries()) {
1122
+ const read = Object.fromEntries(changedPaths(changes).flatMap((path) => (built.read[relative(path)] === undefined ? [] : [[relative(path), built.read[relative(path)]]])));
1123
+ copy = await this.save({ ...this.copies.get(key), pending: { operation_id: operationId(), caller: caller.key, changes, read } });
1124
+ const settled = await this.settle(copy, caller);
1125
+ if (settled.refused) {
1126
+ const done = landed === 0 ? [] : [`House accepted ${counted(landed, 'batch', 'batches')} of ${rootOf(copy.room_handle)} before this one.`];
1127
+ return { refused: true, text: [...done, ...lines, settled.text, ...(index + 1 < batches.length ? [`${counted(batches.length - index - 1, 'batch', 'batches')} after it ${batches.length - index - 1 === 1 ? 'was' : 'were'} not sent`] : [])].join('\n') };
1128
+ }
1129
+ files += settled.files;
1130
+ landed++;
1131
+ lines.push(...settled.lines);
1132
+ }
1133
+ const now = rootOf(this.copies.get(key).room_handle);
1134
+ if (landed === 0)
1135
+ return { refused: false, text: `Nothing to upload: ${root} matches House.` };
1136
+ return {
1137
+ refused: false,
1138
+ text: [`House accepted ${counted(files, 'file')} of ${now}${landed > 1 ? ` in ${landed} batches` : ''}.`, ...lines].join('\n'),
1139
+ };
592
1140
  }
593
- async derived(copy, base, commit) {
1141
+ async built(copy, manifest, present) {
594
1142
  const root = rootOf(copy.room_handle);
595
- const before = await tree(copy.repository, base);
596
- const after = await tree(copy.repository, commit);
597
- const moved = movedPaths(copy.moves, before, after);
598
- const arrived = new Set(moved.values());
599
- const arriving = [...after].filter(([path, entry]) => before.get(path) !== entry);
600
- if (arriving.length > 0) {
601
- const { bounds } = await this.house.post('/kit/door/discover', { prefixes: [root] });
602
- const binary = [];
603
- for (const [path, entry] of arriving) {
604
- if (!(await isText(copy.repository, entry, bounds.max_authored_file_bytes)))
605
- binary.push(path);
606
- }
607
- if (binary.length > 0)
608
- throw new Error(textOnly(binary));
609
- }
610
- const text = (entry) => blobText(copy.repository, entry);
1143
+ const added = [];
1144
+ const changed = [];
1145
+ const gone = [];
1146
+ let adopted = false;
1147
+ for (const [path, file] of present) {
1148
+ const entry = manifest.files[path];
1149
+ if (kitsOwn(path))
1150
+ continue;
1151
+ if (entry === undefined)
1152
+ added.push(path);
1153
+ else if (entry.read_only)
1154
+ continue;
1155
+ else if (entry.sha256 !== file.sha256)
1156
+ changed.push(path);
1157
+ else if (entry.skipped) {
1158
+ manifest.files[path] = { revision: entry.revision, sha256: entry.sha256, bytes: entry.bytes };
1159
+ adopted = true;
1160
+ }
1161
+ }
1162
+ for (const [path, entry] of Object.entries(manifest.files)) {
1163
+ if (!kitsOwn(path) && !entry.read_only && !entry.skipped && !present.has(path))
1164
+ gone.push(path);
1165
+ }
1166
+ if (adopted)
1167
+ await writeManifest(copy.repository, await staging(), manifest);
1168
+ const listed = (paths, file) => {
1169
+ const found = new Map();
1170
+ for (const path of paths)
1171
+ found.set(sameBytes(file(path)), [...(found.get(sameBytes(file(path))) ?? []), path]);
1172
+ return found;
1173
+ };
1174
+ const leaving = listed(gone, (path) => manifest.files[path]);
1175
+ const arriving = listed(added, (path) => present.get(path));
1176
+ const moves = new Map();
1177
+ for (const [held, [from, ...others]] of leaving) {
1178
+ const [to, ...more] = arriving.get(held) ?? [];
1179
+ if (others.length === 0 && more.length === 0 && to !== undefined && isDocument(from) === isDocument(to))
1180
+ moves.set(from, to);
1181
+ }
1182
+ const arrived = new Set(moves.values());
1183
+ const documents = [...added.filter((path) => !arrived.has(path)), ...changed].filter(isDocument).sort();
1184
+ const texts = new Map();
1185
+ if (documents.length > 0) {
1186
+ const { bounds } = await this.house.door('rooms/discover', { prefixes: [root] });
1187
+ const refused = [];
1188
+ for (const path of documents) {
1189
+ const bytes = await readFile(join(copy.repository, present.get(path).name));
1190
+ if (bytes.length > bounds.max_authored_file_bytes || !isUtf8(bytes) || bytes.includes(0))
1191
+ refused.push(path);
1192
+ else
1193
+ texts.set(path, bytes.toString('utf8'));
1194
+ }
1195
+ if (refused.length > 0)
1196
+ return { refused: true, text: markdownOnly(refused, bounds.max_authored_file_bytes) };
1197
+ }
611
1198
  const changes = [];
612
- for (const path of [...new Set([...before.keys(), ...after.keys()])].sort()) {
1199
+ const read = {};
1200
+ for (const path of [...new Set([...added, ...changed, ...gone])].sort()) {
613
1201
  if (arrived.has(path))
614
1202
  continue;
615
- const prior = before.get(path);
616
- const next = after.get(path);
617
- const known = revisionAt(copy, base, path);
618
- const to = moved.get(path);
1203
+ const named = `${root}/${path}`;
1204
+ const entry = manifest.files[path];
1205
+ const to = moves.get(path);
619
1206
  if (to !== undefined) {
620
- changes.push({ op: 'rename', path: `${root}/${path}`, to: `${root}/${to}`, base: known });
621
- const moving = after.get(to);
622
- if (moving !== prior) {
623
- changes.push({ op: 'replace', path: `${root}/${to}`, base: known, content: await text(moving) });
624
- }
1207
+ changes.push({ op: 'move', path: named, to: `${root}/${to}`, base: entry.revision });
1208
+ read[to] = { name: present.get(to).name, sha256: present.get(to).sha256 };
625
1209
  continue;
626
1210
  }
627
- if (prior === next)
1211
+ const file = present.get(path);
1212
+ if (file === undefined) {
1213
+ changes.push({ op: 'delete', path: named, base: entry.revision });
628
1214
  continue;
629
- if (next === undefined)
630
- changes.push({ op: 'remove', path: `${root}/${path}`, base: known });
631
- else if (prior === undefined)
632
- changes.push({ op: 'create', path: `${root}/${path}`, content: await text(next) });
633
- else
634
- changes.push(...changed(`${root}/${path}`, path, known, await text(prior), await text(next)));
1215
+ }
1216
+ const base = entry === undefined ? {} : { base: entry.revision };
1217
+ const text = texts.get(path);
1218
+ if (text !== undefined) {
1219
+ changes.push({ op: 'write', path: named, content: text, ...base });
1220
+ read[path] = { name: file.name, sha256: digest(text) };
1221
+ }
1222
+ else {
1223
+ changes.push({ op: 'upload', path: named, ...base, bytes: file.bytes, sha256: file.sha256, media_type: mediaType(path) });
1224
+ read[path] = { name: file.name, sha256: file.sha256 };
1225
+ }
635
1226
  }
636
- return changes;
1227
+ return { changes, read };
637
1228
  }
638
1229
  async settle(start, caller, restaged = false) {
639
1230
  let copy = start;
640
- const push = copy.push;
641
- if (push.caller !== caller.key)
642
- return this.recovered(copy);
643
- let outcome;
644
- try {
645
- outcome = await caller.submit(push.operation_id, push.changes, push.upload, async (upload) => {
646
- copy = await this.save({ ...copy, push: { ...push, upload } });
647
- });
1231
+ if (copy.pending.caller !== caller.key) {
1232
+ const { upload: _upload, ...pending } = copy.pending;
1233
+ copy = await this.save({ ...copy, pending: { ...pending, caller: caller.key } });
648
1234
  }
649
- catch (error) {
650
- const refusal = error instanceof HouseRefusal ? refusalOf(error.text) : null;
651
- return {
652
- refused: true,
653
- text: refusal?.text ?? `House's answer to ${short(push.commit)} did not arrive; run house push again`,
654
- };
1235
+ const finished = new Set();
1236
+ let rehandledOnce = false;
1237
+ let outcome;
1238
+ for (;;) {
1239
+ const pending = copy.pending;
1240
+ try {
1241
+ outcome = await caller.submit(pending.operation_id, pending.changes, pending.upload, async (upload) => {
1242
+ copy = await this.save({ ...copy, pending: { ...copy.pending, upload } });
1243
+ });
1244
+ }
1245
+ catch (error) {
1246
+ const refusal = error instanceof HouseRefusal ? refusalOf(error.text) : null;
1247
+ return { refused: true, text: refusal?.text ?? "House's answer to this upload did not arrive; run house upload again" };
1248
+ }
1249
+ if (!('refused' in outcome))
1250
+ break;
1251
+ if (outcome.refused.code === 'files_unstaged') {
1252
+ const named = unstagedUploads(outcome.refused).filter(({ id }) => !finished.has(id));
1253
+ if (named.length === 0)
1254
+ break;
1255
+ for (const { id } of named)
1256
+ finished.add(id);
1257
+ const failed = await this.sentParts(copy, named, caller);
1258
+ copy = this.copies.get(copy.key);
1259
+ if (failed.size > 0)
1260
+ return this.stopped(copy, failed);
1261
+ continue;
1262
+ }
1263
+ const handle = outcome.refused.code === 'handle_changed' && !rehandledOnce ? currentHandle(outcome.refused) : null;
1264
+ if (handle === null)
1265
+ break;
1266
+ rehandledOnce = true;
1267
+ copy = await this.save(rehandled(copy, handle));
655
1268
  }
656
1269
  if ('refused' in outcome) {
657
- if (outcome.refused.code === 'upload_unavailable' && copy.push.upload !== undefined && !restaged) {
658
- const { upload, ...unstaged } = copy.push;
659
- return this.settle(await this.save({ ...copy, push: unstaged }), caller, true);
660
- }
661
- if (outcome.refused.code === 'expired')
662
- return this.recovered(copy);
663
- let settled = await this.save(without(copy));
664
- if (settled.unfollowed !== undefined)
1270
+ if (stopsPush(outcome.refused))
1271
+ return this.stopped(copy, firstLine(outcome.refused));
1272
+ if (outcome.refused.code === 'upload_unavailable' && copy.pending.upload !== undefined && !restaged) {
1273
+ const { upload: _upload, ...unstaged } = copy.pending;
1274
+ return this.settle(await this.save({ ...copy, pending: unstaged }), caller, true);
1275
+ }
1276
+ const settled = await this.save(without(copy));
1277
+ if (outcome.refused.code !== 'edit_conflict')
665
1278
  return { refused: true, text: outcome.refused.text };
666
- const rebased = rebaseFixes(outcome.refused);
1279
+ const lines = await this.conflicted(settled, copy.pending, outcome.refused, caller);
1280
+ return { refused: true, text: [outcome.refused.text, ...lines].join('\n') };
1281
+ }
1282
+ return this.integrated(copy, outcome.accepted);
1283
+ }
1284
+ async sentParts(start, named, caller) {
1285
+ let copy = start;
1286
+ const pending = copy.pending;
1287
+ const parts = new Map(pending.changes.filter((change) => change.op === 'upload').map((part) => [part.sha256, part]));
1288
+ const staged = new Set(pending.staged ?? []);
1289
+ const failed = new Map();
1290
+ const work = await staging();
1291
+ let saving = Promise.resolve();
1292
+ await eachAtOnce(named, async (upload) => {
1293
+ const part = parts.get(upload.sha256);
1294
+ const path = relative(part.path);
667
1295
  try {
668
- settled = (await this.caughtUp([settled], true))[0];
1296
+ await sentBinary(work, join(copy.repository, pending.read[path]?.name ?? path), path, part, upload, (id, partSha256) => caller.finish(id, part.path, partSha256));
669
1297
  }
670
- catch {
671
- return {
672
- refused: true,
673
- text: rebased ? `${outcome.refused.text}\nHouse's current state did not arrive; push again later` : outcome.refused.text,
674
- };
1298
+ catch (error) {
1299
+ failed.set(upload.sha256, error instanceof Changed ? error : `its transfer failed: ${causeOf(error)}`);
1300
+ return;
675
1301
  }
676
- return {
677
- refused: true,
678
- text: rebased
679
- ? `${outcome.refused.text}\n${HOUSE_REF} holds House's current state; rebase onto it and push again`
680
- : outcome.refused.text,
681
- };
682
- }
683
- return this.finished(await this.save({ ...copy, push: { ...copy.push, state: 'accepted', written: outcome.accepted } }));
684
- }
685
- async recovered(copy) {
686
- const push = copy.push;
687
- const current = (await this.caughtUp([copy], true))[0];
688
- const house = await tree(copy.repository, current.house);
689
- const root = rootOf(current.room_handle);
690
- if (!(await this.landed(copy.repository, push.changes, house, await tree(copy.repository, push.commit)))) {
691
- await this.save(without(current));
692
- return {
693
- refused: true,
694
- text: `House does not hold ${short(push.commit)}; run house push again`,
695
- };
696
- }
697
- const written = [];
698
- for (const path of changedPaths(push.changes).map(relative)) {
699
- const entry = house.get(path);
700
- written.push({
701
- path: `${root}/${path}`,
702
- revision: entry === undefined ? null : (revisionAt(current, current.house, path) ?? null),
703
- content: entry === undefined ? null : await blobText(copy.repository, entry),
1302
+ staged.add(upload.sha256);
1303
+ saving = saving.then(async () => {
1304
+ copy = await this.save({ ...copy, pending: { ...copy.pending, staged: [...staged] } });
704
1305
  });
705
- }
706
- return this.finished(await this.save({ ...current, push: { ...push, state: 'accepted', written } }));
1306
+ await saving;
1307
+ });
1308
+ return failed;
707
1309
  }
708
- async finished(start) {
709
- let copy = start;
710
- const { commit } = copy.push;
711
- try {
712
- if (copy.push.acknowledged === undefined)
713
- copy = await this.save(await this.acknowledged(copy));
714
- const integrated = await this.integrated(copy.push);
715
- if (integrated.done)
716
- await this.save(without(copy));
717
- return { refused: false, text: integrated.text };
718
- }
719
- catch {
720
- return {
721
- refused: false,
722
- text: `House accepted ${short(commit)}; run house push again to finish integrating it here`,
723
- };
1310
+ async stopped(copy, reasons) {
1311
+ const pending = copy.pending;
1312
+ const root = rootOf(copy.room_handle);
1313
+ const unchanged = `House holds nothing of this upload yet, so ${root} is unchanged`;
1314
+ const changed = typeof reasons === 'string' ? [] : [...reasons.values()].filter((reason) => reason instanceof Changed);
1315
+ if (changed.length > 0) {
1316
+ await this.save(without(copy));
1317
+ return { refused: true, text: `${unchanged}: ${changed.map((reason) => reason.message).join('; ')}; run house upload again to send the copy as it is now` };
724
1318
  }
725
- }
726
- async acknowledged(start) {
727
- let copy = start;
728
- const push = copy.push;
729
- let written = push.written;
730
- if (push.changes.some((change) => change.op === 'rename')) {
731
- copy = (await this.caughtUp([copy], true))[0];
732
- const house = await tree(copy.repository, copy.house);
733
- const sent = await tree(copy.repository, push.house);
734
- if (push.changes.some((change) => change.op === 'rename' && !house.has(relative(change.to)))) {
735
- throw new Error(`House's result for ${short(push.commit)} is not readable yet`);
736
- }
737
- const known = new Set(written.map((entry) => relative(entry.path)));
738
- for (const path of new Set([...sent.keys(), ...house.keys()])) {
739
- const entry = house.get(path);
740
- if (known.has(path) || sent.get(path) === entry)
741
- continue;
742
- written = [
743
- ...written,
744
- {
745
- path: `${rootOf(copy.room_handle)}/${path}`,
746
- revision: entry === undefined ? null : (revisionAt(copy, copy.house, path) ?? null),
747
- content: entry === undefined ? null : await blobText(copy.repository, entry),
748
- },
749
- ];
750
- }
751
- }
752
- const selected = await tree(copy.repository, push.commit);
753
- const houseTree = await tree(copy.repository, copy.house);
754
- const files = new Map();
755
- const revisions = {};
756
- for (const entry of written) {
757
- const path = relative(entry.path);
758
- if (!selected.has(path) && !houseTree.has(path) && !declaredWritable(path, copy))
759
- continue;
760
- files.set(path, entry.revision === null ? null : (entry.content ?? (await this.revisionText(entry.path, entry.revision))));
761
- revisions[path] = entry.revision;
762
- }
763
- const edits = new Map([...files].filter(([path, content]) => (content === null ? selected.has(path) : selected.get(path) !== blobId(content))));
764
- const received = [...files.keys()].filter((path) => revisionAt(copy, copy.house, path) === revisionAt(copy, push.house, path));
765
- const accepted = `House ${rootOf(copy.room_handle)} with ${push.commit} accepted`;
766
- const house = await importCommits(copy.repository, [
767
- ...(edits.size === 0 ? [] : [{ message: accepted, from: push.commit, files: edits }]),
768
- {
769
- message: accepted,
770
- from: copy.house,
771
- merge: edits.size === 0 ? push.commit : ':1',
772
- files: new Map(received.map((path) => [path, files.get(path)])),
773
- },
774
- ]);
775
- const acknowledged = edits.size === 0 ? push.commit : await revision(copy.repository, `${house}^2`);
776
- const after = await tree(copy.repository, house);
1319
+ const again = 'house upload again resumes this upload and sends only what it has not sent';
1320
+ const staged = new Set(pending.staged ?? []);
1321
+ const unsent = pending.changes.filter((change) => change.op === 'upload' && !staged.has(change.sha256));
1322
+ if (typeof reasons === 'string' && unsent.length === 0)
1323
+ return { refused: true, text: `${unchanged}: ${reasons}\n${again}` };
1324
+ const why = (sha256) => (typeof reasons === 'string' ? reasons : String(reasons.get(sha256) ?? 'not sent yet'));
777
1325
  return {
778
- ...copy,
779
- house,
780
- manifests: {
781
- ...copy.manifests,
782
- [acknowledged]: { parent: push.base, files: revisions },
783
- [house]: { parent: copy.house, files: Object.fromEntries(received.map((path) => [path, revisions[path] ?? null])) },
784
- },
785
- moves: copy.moves.filter(({ from, to }) => !(after.has(to) && !after.has(from))),
786
- push: { ...push, acknowledged },
1326
+ refused: true,
1327
+ text: [`${unchanged}; these attachments are not sent:`, ...unsent.map(({ path, sha256 }) => `${relative(path)}: ${why(sha256)}`), again].join('\n'),
787
1328
  };
788
1329
  }
789
- async landed(repository, changes, house, selected) {
790
- const text = async (path) => {
791
- const entry = house.get(relative(path));
792
- return entry === undefined ? undefined : blobText(repository, entry);
1330
+ async conflicted(copy, pending, refusal, caller) {
1331
+ const root = rootOf(copy.room_handle);
1332
+ const manifest = await readManifest(copy.repository);
1333
+ const present = await scanned(copy.repository);
1334
+ const work = await staging();
1335
+ const lines = [];
1336
+ const nameOf = (path) => present.get(path)?.name ?? path;
1337
+ const holds = async (path, sha256) => present.has(path) && (await holding(copy.repository, nameOf(path), sha256));
1338
+ const brought = async (path, revision) => {
1339
+ const named = `${root}/${path}`;
1340
+ const target = join(copy.repository, nameOf(path));
1341
+ if (isDocument(path)) {
1342
+ const text = await this.revisionRead(named, revision);
1343
+ if (text === null)
1344
+ return false;
1345
+ await placed(work, target, text);
1346
+ manifest.files[path] = { revision, sha256: digest(text), bytes: Buffer.byteLength(text) };
1347
+ }
1348
+ else {
1349
+ const held = {};
1350
+ const file = await receivedBinary(work, named, async () => {
1351
+ const info = await caller.info(named, revision);
1352
+ if (info === null)
1353
+ throw new Error(`House holds no attachment at ${named}`);
1354
+ held.file = info;
1355
+ return info;
1356
+ });
1357
+ await mkdir(dirname(target), { recursive: true });
1358
+ await rename(file, target);
1359
+ manifest.files[path] = { revision, sha256: held.file.sha256, bytes: held.file.bytes };
1360
+ }
1361
+ present.set(path, { name: nameOf(path), sha256: manifest.files[path].sha256, bytes: manifest.files[path].bytes });
1362
+ return true;
793
1363
  };
794
- for (const change of changes) {
795
- const current = await text(change.path);
796
- if (change.op === 'remove' && current === undefined)
1364
+ for (const conflict of refusal.conflicts) {
1365
+ const path = relative(conflict.path);
1366
+ const change = pending.changes.find((sent) => relative(sent.path) === path || (sent.op === 'move' && relative(sent.to) === path));
1367
+ if (change === undefined)
1368
+ continue;
1369
+ const named = `${root}/${path}`;
1370
+ const read = pending.read[path];
1371
+ const busy = `${named}: changed while it uploaded; the next house upload merges it`;
1372
+ if (conflict.revision === undefined) {
1373
+ delete manifest.files[path];
1374
+ lines.push(`${named}: House no longer holds it; the next house upload sends it as a new file`);
797
1375
  continue;
798
- if (change.op === 'rename' && current === undefined && house.get(relative(change.to)) === selected.get(relative(change.to))) {
1376
+ }
1377
+ if (change.op === 'write') {
1378
+ if (read === undefined || !(await holds(path, read.sha256))) {
1379
+ lines.push(busy);
1380
+ continue;
1381
+ }
1382
+ const content = conflict.content ?? whole(await this.revisionText(named, conflict.revision), change.content);
1383
+ await placed(work, join(copy.repository, nameOf(path)), content);
1384
+ manifest.files[path] = { revision: conflict.revision, sha256: digest(content), bytes: Buffer.byteLength(content), conflicted: true };
1385
+ lines.push(`${named}: conflict markers written; resolve them, then run house upload again`);
1386
+ continue;
1387
+ }
1388
+ if (change.op === 'upload') {
1389
+ const current = await caller.info(named, conflict.revision);
1390
+ if (current === null)
1391
+ throw new Error(`House holds no attachment at ${named}`);
1392
+ manifest.files[path] = { revision: conflict.revision, sha256: current.sha256, bytes: current.bytes };
1393
+ const theirs = JSON.stringify({ path: named, file: join(copy.repository, nameOf(path)) });
1394
+ lines.push(`${named}: House holds ${conflict.revision}, a version this copy never held; the next house upload replaces it with this file, and house download '${theirs}' brings House's instead`);
1395
+ continue;
1396
+ }
1397
+ if (change.op === 'delete') {
1398
+ if (present.has(path))
1399
+ lines.push(busy);
1400
+ else if (await brought(path, conflict.revision)) {
1401
+ lines.push(`${named}: House changed it since this copy read it and now holds ${conflict.revision}; that version is back here, so delete it again if you still mean to`);
1402
+ }
1403
+ continue;
1404
+ }
1405
+ const from = relative(change.path);
1406
+ const to = relative(change.to);
1407
+ const moved = pending.read[to];
1408
+ if (!present.has(from) && moved !== undefined && (await holds(to, moved.sha256))) {
1409
+ await mkdir(dirname(join(copy.repository, from)), { recursive: true });
1410
+ await rename(join(copy.repository, nameOf(to)), join(copy.repository, from));
1411
+ await removed(copy.repository, nameOf(to));
1412
+ present.set(from, { ...present.get(to), name: from });
1413
+ present.delete(to);
1414
+ }
1415
+ const entry = manifest.files[from];
1416
+ if (path === from && entry !== undefined && (await holds(from, entry.sha256)) && (await brought(from, conflict.revision))) {
1417
+ lines.push(`${named}: House changed it since this copy read it and now holds ${conflict.revision}; it is back at ${named} with that version, so move it again if you still mean to`);
1418
+ }
1419
+ else if (path === to && !present.has(to) && (await brought(to, conflict.revision))) {
1420
+ lines.push(`${named}: House already holds a file there (${conflict.revision}); ${root}/${from} is back where it was and ${named} holds House's file`);
1421
+ }
1422
+ else
1423
+ lines.push(busy);
1424
+ }
1425
+ await writeManifest(copy.repository, work, manifest);
1426
+ return lines;
1427
+ }
1428
+ async textOf(written, sent) {
1429
+ if (typeof written.content === 'string' && !CUT.test(written.content))
1430
+ return written.content;
1431
+ if (written.content === undefined && sent !== undefined)
1432
+ return sent;
1433
+ return this.revisionText(written.path, written.revision);
1434
+ }
1435
+ async integrated(copy, accepted) {
1436
+ const pending = copy.pending;
1437
+ const root = rootOf(copy.room_handle);
1438
+ const manifest = await readManifest(copy.repository);
1439
+ const work = await staging();
1440
+ const answered = new Map(accepted.filter((written) => written.revision !== null).map((written) => [relative(written.path), written]));
1441
+ const lines = [];
1442
+ const sent = new Set();
1443
+ for (const change of pending.changes) {
1444
+ const path = relative(change.path);
1445
+ sent.add(path);
1446
+ if (change.op === 'delete') {
1447
+ delete manifest.files[path];
799
1448
  continue;
800
1449
  }
801
- if ((change.op === 'create' || change.op === 'replace') && current === change.content)
1450
+ if (change.op === 'move') {
1451
+ const to = relative(change.to);
1452
+ sent.add(to);
1453
+ const entry = manifest.files[path];
1454
+ delete manifest.files[path];
1455
+ manifest.files[to] = { revision: answered.get(to)?.revision ?? entry.revision, sha256: entry.sha256, bytes: entry.bytes };
802
1456
  continue;
803
- const parsed = current === undefined ? null : parse(current, change.path);
804
- const document = parsed?.ok === true ? parsed.document : null;
805
- if (document === null)
806
- return false;
807
- if (change.op === 'set_field' && JSON.stringify(document.fields[change.field]) === JSON.stringify(change.value))
1457
+ }
1458
+ const written = answered.get(path);
1459
+ if (change.op === 'upload') {
1460
+ manifest.files[path] = { revision: written.revision, sha256: change.sha256, bytes: change.bytes };
808
1461
  continue;
809
- if (change.op === 'replace_section' && document.sections[change.section] === change.content)
1462
+ }
1463
+ const text = await this.textOf(written, change.content);
1464
+ const read = pending.read[path];
1465
+ if (text === change.content) {
1466
+ manifest.files[path] = { revision: written.revision, sha256: read.sha256, bytes: Buffer.byteLength(text) };
1467
+ }
1468
+ else if (await holding(copy.repository, read.name, read.sha256)) {
1469
+ await placed(work, join(copy.repository, read.name), text);
1470
+ manifest.files[path] = { revision: written.revision, sha256: digest(text), bytes: Buffer.byteLength(text) };
1471
+ }
1472
+ else {
1473
+ lines.push(`${root}/${path}: changed while it uploaded; House accepted it and local integration is pending, so the next house upload merges it`);
1474
+ }
1475
+ }
1476
+ for (const [path, written] of answered) {
1477
+ const entry = manifest.files[path];
1478
+ if (sent.has(path) || entry === undefined || entry.revision === written.revision)
810
1479
  continue;
811
- if (change.op === 'replace_preamble' && document.preamble === change.content)
1480
+ const text = await this.textOf(written, undefined);
1481
+ if (!(await holding(copy.repository, path, entry.sha256))) {
1482
+ lines.push(`${root}/${path}: changed here, so House's change was not brought; the next house upload merges it`);
812
1483
  continue;
813
- return false;
1484
+ }
1485
+ await placed(work, join(copy.repository, path), text);
1486
+ manifest.files[path] = { revision: written.revision, sha256: digest(text), bytes: Buffer.byteLength(text) };
814
1487
  }
815
- return true;
1488
+ await writeManifest(copy.repository, work, manifest);
1489
+ await this.save(without(copy));
1490
+ return { refused: false, files: pending.changes.length, lines };
816
1491
  }
817
- async revisionText(path, wanted) {
818
- const page = await this.house.post('/kit/door/revisions', {
1492
+ async deletedByHouse(held, named, path, sha256) {
1493
+ const attachments = Object.values(held.manifests).flatMap((manifest) => (manifest.binary?.[path] === undefined ? [] : [manifest.binary[path].sha256]));
1494
+ if (attachments.length > 0)
1495
+ return attachments.includes(sha256);
1496
+ for (const revision of recordedRevisions(held, path)) {
1497
+ const text = await this.revisionRead(named, revision);
1498
+ if (text !== null && digest(text) === sha256)
1499
+ return true;
1500
+ }
1501
+ return false;
1502
+ }
1503
+ async revisionRead(path, wanted) {
1504
+ const page = await this.house.door('files/revisions/list', {
819
1505
  path,
820
1506
  revision: wanted,
821
1507
  });
822
- const content = page.revisions.find((entry) => entry.revision === wanted)?.content;
823
- if (content === undefined)
824
- throw new Error(`House's result for ${path} is not readable yet`);
825
- return content;
826
- }
827
- async integrated(push) {
828
- const { commit, worktree, branch } = push;
829
- const acknowledged = push.acknowledged;
830
- const accepted = `House accepted ${short(commit)}`;
831
- if (acknowledged === commit)
832
- return { done: true, text: `${accepted}.` };
833
- const pending = (reason) => ({
834
- done: false,
835
- text: `${accepted}; local integration is pending (${reason}): ${acknowledged} holds House's result.`,
836
- });
837
- const head = await git(worktree, ['symbolic-ref', '-q', 'HEAD']).then((name) => name.trim(), () => null);
838
- if (branch === null || head !== branch)
839
- return pending('the branch changed');
840
- const tip = await revision(worktree, 'HEAD');
841
- const name = branch.replace(/^refs\/heads\//, '');
842
- if (await succeeded(worktree, ['merge-base', '--is-ancestor', acknowledged, tip])) {
843
- return { done: true, text: `${accepted}; ${name} holds it.` };
844
- }
845
- if (!(await succeeded(worktree, ['merge-base', '--is-ancestor', commit, tip])))
846
- return pending('the branch changed');
847
- if ((await git(worktree, ['status', '--porcelain', '--untracked-files=no'])).trim() !== '') {
848
- return pending('uncommitted changes');
849
- }
850
- if (tip === commit) {
851
- if (!(await succeeded(worktree, ['merge', '--ff-only', '-q', acknowledged])))
852
- return pending('a conflict');
853
- }
854
- else if (!(await succeeded(worktree, ['rebase', '-q', '--onto', acknowledged, commit], IDENTITY))) {
855
- await succeeded(worktree, ['rebase', '--abort']);
856
- return pending('a conflict');
857
- }
858
- return { done: true, text: `${accepted}; ${name} is at ${short(await revision(worktree, 'HEAD'))}.` };
1508
+ const found = page.revisions.find((entry) => entry.revision === wanted);
1509
+ if (found === undefined)
1510
+ return null;
1511
+ if (typeof found.content !== 'string')
1512
+ throw new Error(`House's text of ${path} at ${wanted} is not readable yet`);
1513
+ return found.content;
1514
+ }
1515
+ async revisionText(path, wanted) {
1516
+ const text = await this.revisionRead(path, wanted);
1517
+ if (text === null)
1518
+ throw new Error(`House holds no revision ${wanted} of ${path}`);
1519
+ return text;
859
1520
  }
860
1521
  }