@agentshouse/kit 0.2.1-ce145e9aadbf6 → 0.2.1-ce46b2b55463f

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 (146) hide show
  1. package/README.md +46 -49
  2. package/dist/acp.js +101 -108
  3. package/dist/afk.js +17 -0
  4. package/dist/agents.js +252 -73
  5. package/dist/api.js +43 -6
  6. package/dist/app-git.js +48 -0
  7. package/dist/binaries.js +79 -0
  8. package/dist/bridge.js +62 -79
  9. package/dist/building.js +50 -0
  10. package/dist/calls.js +88 -0
  11. package/dist/chunks.js +49 -0
  12. package/dist/clis.js +171 -102
  13. package/dist/codex-main.js +58 -0
  14. package/dist/connect.js +108 -0
  15. package/dist/connection.js +120 -0
  16. package/dist/content.js +106 -0
  17. package/dist/conversations.js +816 -335
  18. package/dist/copies.js +842 -309
  19. package/dist/earlier.js +263 -0
  20. package/dist/end.js +26 -0
  21. package/dist/events.js +793 -0
  22. package/dist/files.js +206 -18
  23. package/dist/git-main.js +24 -29
  24. package/dist/git.js +95 -18
  25. package/dist/grok.js +39 -0
  26. package/dist/home.js +134 -9
  27. package/dist/house-cli.js +282 -0
  28. package/dist/house-main.js +7 -159
  29. package/dist/instructions.js +7 -11
  30. package/dist/keys.js +238 -0
  31. package/dist/kit-cli.js +78 -0
  32. package/dist/kit-main.js +8 -19
  33. package/dist/login.js +133 -104
  34. package/dist/logout.js +15 -14
  35. package/dist/mcp.js +42 -3
  36. package/dist/owner.js +46 -0
  37. package/dist/placement.js +25 -0
  38. package/dist/refusals.js +43 -15
  39. package/dist/relay.js +33 -2
  40. package/dist/resident.js +161 -61
  41. package/dist/scope.js +214 -0
  42. package/dist/scripts.js +303 -0
  43. package/dist/secret-input.js +4 -3
  44. package/dist/settings.js +188 -0
  45. package/dist/shell.js +3 -0
  46. package/dist/sign-in.js +85 -33
  47. package/dist/silence.js +28 -0
  48. package/dist/skills.js +49 -35
  49. package/dist/supervisor.js +163 -0
  50. package/dist/transcription.js +131 -40
  51. package/dist/translate.js +19 -16
  52. package/dist/update.js +119 -100
  53. package/dist/wire.js +32 -0
  54. package/dist/work.js +353 -0
  55. package/dist/workspace.js +103 -0
  56. package/package.json +26 -21
  57. package/dist/authority-main.js +0 -22
  58. package/dist/blocks.js +0 -51
  59. package/dist/bootstrap.json +0 -24
  60. package/dist/clis-main.js +0 -23
  61. package/dist/configure-main.js +0 -6
  62. package/dist/enrol-main.js +0 -4
  63. package/dist/stream.js +0 -47
  64. package/how-we-work.md +0 -23
  65. package/skills/ask-matt/LICENSE +0 -21
  66. package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
  67. package/skills/ask-matt/SKILL.md +0 -91
  68. package/skills/ask-matt/agents/openai.yaml +0 -4
  69. package/skills/code-review/LICENSE +0 -21
  70. package/skills/code-review/SKILL.md +0 -89
  71. package/skills/code-review/agents/openai.yaml +0 -3
  72. package/skills/codebase-design/DEEPENING.md +0 -37
  73. package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
  74. package/skills/codebase-design/LICENSE +0 -21
  75. package/skills/codebase-design/SKILL.md +0 -116
  76. package/skills/codebase-design/agents/openai.yaml +0 -3
  77. package/skills/diagnosing-bugs/LICENSE +0 -21
  78. package/skills/diagnosing-bugs/SKILL.md +0 -140
  79. package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
  80. package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
  81. package/skills/domain-modeling/ADR-FORMAT.md +0 -47
  82. package/skills/domain-modeling/CONTEXT-FORMAT.md +0 -60
  83. package/skills/domain-modeling/LICENSE +0 -21
  84. package/skills/domain-modeling/SKILL.md +0 -76
  85. package/skills/domain-modeling/agents/openai.yaml +0 -3
  86. package/skills/grill-me/LICENSE +0 -21
  87. package/skills/grill-me/SKILL.md +0 -8
  88. package/skills/grill-me/agents/openai.yaml +0 -4
  89. package/skills/grill-with-docs/LICENSE +0 -21
  90. package/skills/grill-with-docs/SKILL.md +0 -8
  91. package/skills/grill-with-docs/agents/openai.yaml +0 -4
  92. package/skills/grilling/LICENSE +0 -21
  93. package/skills/grilling/SKILL.md +0 -30
  94. package/skills/grilling/agents/openai.yaml +0 -3
  95. package/skills/handoff/LICENSE +0 -21
  96. package/skills/handoff/SKILL.md +0 -17
  97. package/skills/handoff/agents/openai.yaml +0 -4
  98. package/skills/implement/LICENSE +0 -21
  99. package/skills/implement/SKILL.md +0 -16
  100. package/skills/implement/agents/openai.yaml +0 -4
  101. package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
  102. package/skills/improve-codebase-architecture/LICENSE +0 -21
  103. package/skills/improve-codebase-architecture/SKILL.md +0 -72
  104. package/skills/improve-codebase-architecture/agents/openai.yaml +0 -4
  105. package/skills/prototype/LICENSE +0 -21
  106. package/skills/prototype/LOGIC.md +0 -67
  107. package/skills/prototype/SKILL.md +0 -28
  108. package/skills/prototype/UI.md +0 -112
  109. package/skills/prototype/agents/openai.yaml +0 -3
  110. package/skills/research/LICENSE +0 -21
  111. package/skills/research/SKILL.md +0 -14
  112. package/skills/research/agents/openai.yaml +0 -3
  113. package/skills/resolving-merge-conflicts/LICENSE +0 -21
  114. package/skills/resolving-merge-conflicts/SKILL.md +0 -16
  115. package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
  116. package/skills/tdd/LICENSE +0 -21
  117. package/skills/tdd/SKILL.md +0 -40
  118. package/skills/tdd/agents/openai.yaml +0 -3
  119. package/skills/tdd/mocking.md +0 -59
  120. package/skills/tdd/tests.md +0 -77
  121. package/skills/teach/GLOSSARY-FORMAT.md +0 -35
  122. package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
  123. package/skills/teach/LICENSE +0 -21
  124. package/skills/teach/MISSION-FORMAT.md +0 -31
  125. package/skills/teach/RESOURCES-FORMAT.md +0 -32
  126. package/skills/teach/SKILL.md +0 -141
  127. package/skills/teach/agents/openai.yaml +0 -4
  128. package/skills/to-questionnaire/LICENSE +0 -21
  129. package/skills/to-questionnaire/SKILL.md +0 -55
  130. package/skills/to-questionnaire/agents/openai.yaml +0 -4
  131. package/skills/to-spec/LICENSE +0 -21
  132. package/skills/to-spec/SKILL.md +0 -76
  133. package/skills/to-spec/agents/openai.yaml +0 -4
  134. package/skills/to-tickets/LICENSE +0 -21
  135. package/skills/to-tickets/SKILL.md +0 -106
  136. package/skills/to-tickets/agents/openai.yaml +0 -4
  137. package/skills/wait-what/LICENSE +0 -21
  138. package/skills/wait-what/SKILL.md +0 -8
  139. package/skills/wait-what/agents/openai.yaml +0 -4
  140. package/skills/wayfinder/LICENSE +0 -21
  141. package/skills/wayfinder/SKILL.md +0 -129
  142. package/skills/wayfinder/agents/openai.yaml +0 -4
  143. package/skills/writing-for-agents/LICENSE +0 -21
  144. package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
  145. package/skills/writing-for-agents/SKILL.md +0 -83
  146. package/skills/writing-for-agents/agents/openai.yaml +0 -3
package/dist/copies.js CHANGED
@@ -1,21 +1,39 @@
1
- import { parse } from '@agentshouse/mdmodel';
2
- import { randomUUID } from 'node:crypto';
1
+ import { HOUSE_ROOM_TYPES, readTypes } from '@agentshouse/mdmodel';
2
+ import { createHash, randomUUID } from 'node:crypto';
3
3
  import { mkdir, readFile, readdir, realpath, rename, rm, rmdir, stat, writeFile } from 'node:fs/promises';
4
4
  import { dirname, join } from 'node:path';
5
- import { HouseRefusal } from './api.js';
5
+ import { finalRefusal, HouseRefusal, retrying } from './api.js';
6
+ import { eachAtOnce, receivedBinary, sentBinary, sorted, sweptTransfers, } from './binaries.js';
7
+ import { chunkEntries, stagedChunks } from './chunks.js';
6
8
  import { blobId, blobText, git, HOUSE_REF, importCommits, localCopies, revision, run, succeeded, tree, } from './git.js';
9
+ import { fileInfo, paused } from './files.js';
7
10
  import { kitHome } from './home.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';
13
+ import { firstLine, refusalOf, retryable, stopsPush } from './refusals.js';
14
+ import { changed, readDocument } from './translate.js';
12
15
  const BRANCH = 'main';
13
16
  const PROVENANCE = '_provenance';
14
- const REPLICA_RETRY_MS = 30_000;
15
- const IDENTITY = { GIT_COMMITTER_NAME: 'House Kit', GIT_COMMITTER_EMAIL: 'house-kit@localhost' };
17
+ const IDENTITY = { GIT_COMMITTER_NAME: 'Kit', GIT_COMMITTER_EMAIL: 'house-kit@localhost' };
18
+ const ROOMS = 'rooms/';
19
+ const NOT_READY = 'not ready yet; Kit copies it once House has built it';
20
+ const STAGED_CHUNKS = 'house-chunks';
21
+ const SCHEMA = 'settings/schema.yaml';
22
+ // 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.
23
+ const ASKED_AGAIN_MS = [1000, 2000, 3000, 5000, 8000, 11_000, 15_000];
24
+ class NotReady extends Error {
25
+ rooms;
26
+ constructor(rooms) {
27
+ super(NOT_READY);
28
+ this.rooms = rooms;
29
+ }
30
+ }
16
31
  function causeOf(error) {
17
32
  return error instanceof Error ? error.message : String(error);
18
33
  }
34
+ function houseCause(error) {
35
+ return error instanceof HouseRefusal ? (refusalOf(error.text)?.text ?? causeOf(error)) : causeOf(error);
36
+ }
19
37
  function logged(error) {
20
38
  process.stderr.write(`kit: ${causeOf(error)}\n`);
21
39
  }
@@ -26,13 +44,60 @@ const REBASED = new Set([
26
44
  'base_revision_stale',
27
45
  'path_exists',
28
46
  'path_absent',
47
+ 'base_required',
48
+ 'binary_file',
49
+ 'text_file',
29
50
  ]);
30
51
  function rebaseFixes(refusal) {
31
- return refusal.code === 'edit_conflict' && refusal.conflicts.some((code) => REBASED.has(code));
52
+ return refusal.code === 'edit_conflict' && refusal.conflicts.some(({ code }) => REBASED.has(code));
53
+ }
54
+ function markdownOnly(paths, maxBytes) {
55
+ const [named, them] = paths.length === 1 ? [`${paths[0]} is`, 'it'] : [`${paths.join(', ')} are`, 'them'];
56
+ return `${named} Markdown, which holds text only: UTF-8 without NUL, at most ${maxBytes} bytes; commit ${them} as text and push again`;
57
+ }
58
+ async function stopping(sent) {
59
+ try {
60
+ return await sent;
61
+ }
62
+ catch (error) {
63
+ const refusal = error instanceof HouseRefusal ? refusalOf(error.text) : null;
64
+ if (refusal === null || !stopsPush(refusal))
65
+ throw error;
66
+ return { refused: refusal };
67
+ }
68
+ }
69
+ function unstagedUploads(refusal) {
70
+ const named = refusal.detail.upload;
71
+ return Array.isArray(named) ? named : [];
72
+ }
73
+ function heldKey(file) {
74
+ return `${file.sha256}:${file.bytes}`;
75
+ }
76
+ function outside(path) {
77
+ return { refused: true, text: `${path} is not a House Local copy; run house push inside one` };
32
78
  }
33
79
  function short(commit) {
34
80
  return commit.slice(0, 12);
35
81
  }
82
+ function moved(before, after) {
83
+ if (before.house !== after.house)
84
+ return [`${rootOf(after.room_handle)}: ${HOUSE_REF} is at ${short(after.house)}`];
85
+ return before.position === after.position ? [] : [`${rootOf(after.room_handle)}: read-only files refreshed`];
86
+ }
87
+ function expiredAuthority(error) {
88
+ if (!(error instanceof HouseRefusal))
89
+ return undefined;
90
+ try {
91
+ const { error: refused } = JSON.parse(error.text);
92
+ return refused?.code === 'expired' && typeof refused.authority === 'string' ? refused.authority : undefined;
93
+ }
94
+ catch {
95
+ return undefined;
96
+ }
97
+ }
98
+ function grouped(items, size) {
99
+ return items.flatMap((_, index) => (index % size === 0 ? [items.slice(index, index + size)] : []));
100
+ }
36
101
  function covers(path, prefix) {
37
102
  return prefix === '' || path === prefix || path.startsWith(`${prefix}/`);
38
103
  }
@@ -41,6 +106,12 @@ function sidecar(path) {
41
106
  segments.splice(-1, 0, PROVENANCE);
42
107
  return segments.join('/');
43
108
  }
109
+ function recordOf(key) {
110
+ return join(kitHome(), 'local-copies', `${key}.json`);
111
+ }
112
+ function stagingPrefix() {
113
+ return `.${createHash('sha256').update(kitHome()).digest('hex').slice(0, 16)}-`;
114
+ }
44
115
  function rootOf(handle) {
45
116
  return `/rooms/${handle}`;
46
117
  }
@@ -68,32 +139,76 @@ async function removeFile(repository, path) {
68
139
  return;
69
140
  }
70
141
  }
71
- async function readOnlyPlaced(repository, held, written, removed) {
142
+ async function readOnlyFiles(repository, held) {
72
143
  const tracked = new Set((await git(repository, ['ls-files', '-z'])).split('\0'));
73
144
  const holding = new Set(held);
74
- for (const path of removed) {
75
- if (!holding.has(path))
76
- continue;
77
- if (!tracked.has(path))
78
- await removeFile(repository, path);
79
- holding.delete(path);
80
- }
81
- for (const [path, content] of written) {
82
- if (tracked.has(path))
83
- continue;
84
- if (!holding.has(path) && (await exists(join(repository, path))))
85
- continue;
86
- await writeText(join(repository, path), content);
87
- holding.add(path);
88
- }
89
- return [...holding].sort();
145
+ return {
146
+ remove: async (path) => {
147
+ if (!holding.has(path))
148
+ return;
149
+ if (!tracked.has(path))
150
+ await removeFile(repository, path);
151
+ holding.delete(path);
152
+ },
153
+ place: async (path, content) => {
154
+ if (tracked.has(path))
155
+ return;
156
+ if (!holding.has(path) && (await exists(join(repository, path))))
157
+ return;
158
+ await writeText(join(repository, path), content);
159
+ holding.add(path);
160
+ },
161
+ held: () => [...holding].sort(),
162
+ };
163
+ }
164
+ async function readOnlyPlaced(repository, held, written, removed) {
165
+ const files = await readOnlyFiles(repository, held);
166
+ for (const path of removed)
167
+ await files.remove(path);
168
+ for (const [path, content] of written)
169
+ await files.place(path, content);
170
+ return files.held();
171
+ }
172
+ async function importedWhole(repository, scope, entries, commit, readOnly, kept) {
173
+ const revisions = {};
174
+ const binary = {};
175
+ async function* tracked() {
176
+ const binaries = [];
177
+ for await (const entry of entries) {
178
+ if (!('content' in entry)) {
179
+ if (declaredWritable(entry.path, scope))
180
+ binaries.push(entry);
181
+ continue;
182
+ }
183
+ const { path, content, revision: held, provenance } = entry;
184
+ if (declaredWritable(path, scope)) {
185
+ revisions[path] = held;
186
+ yield [path, content];
187
+ }
188
+ else {
189
+ await readOnly.place(path, content);
190
+ }
191
+ if (provenance !== undefined)
192
+ await readOnly.place(sidecar(path), provenance);
193
+ }
194
+ const blobs = await kept(binaries);
195
+ for (const entry of binaries) {
196
+ const blob = blobs.get(entry.path);
197
+ if (blob === undefined)
198
+ continue;
199
+ revisions[entry.path] = entry.revision;
200
+ binary[entry.path] = { bytes: entry.bytes, sha256: entry.sha256 };
201
+ yield [entry.path, { blob }];
202
+ }
203
+ }
204
+ return { house: await importCommits(repository, [{ ...commit, files: tracked() }]), revisions, binary };
90
205
  }
91
206
  async function ruled(path, rules) {
92
207
  if ((await readFile(path, 'utf8').catch(() => null)) !== rules)
93
208
  await writeFile(path, rules);
94
209
  }
95
210
  async function ignoreRules(repository, writable, copy) {
96
- const readOnly = ('read_only' in copy ? copy.read_only : [...copy.readOnly.keys()]).filter((path) => !path.split('/').includes(PROVENANCE));
211
+ const readOnly = copy.read_only.filter((path) => !path.split('/').includes(PROVENANCE));
97
212
  const protectedPath = (path) => copy.protected.some((prefix) => prefix !== '' && covers(path, prefix));
98
213
  const whole = copy.protected.filter((prefix) => prefix !== '' && !writable.some((path) => covers(path, prefix)));
99
214
  const house = [
@@ -102,7 +217,7 @@ async function ignoreRules(repository, writable, copy) {
102
217
  ].map(pattern);
103
218
  const granted = readOnly.filter((path) => !protectedPath(path)).map(pattern);
104
219
  await ruled(join(repository, '.gitignore'), [
105
- '# House Kit writes this file and .ignore. The paths below are read-only House',
220
+ '# Kit writes this file and .ignore. The paths below are read-only House',
106
221
  '# content: Git does not track them, and House refuses a change to them even',
107
222
  '# when this file is edited or a path is force-added.',
108
223
  '/.gitignore',
@@ -117,22 +232,68 @@ async function ignoreRules(repository, writable, copy) {
117
232
  '',
118
233
  ].join('\n'));
119
234
  await ruled(join(repository, '.ignore'), [
120
- '# House Kit writes this file so native search still reads the read-only House',
235
+ '# Kit writes this file so native search still reads the read-only House',
121
236
  '# content that .gitignore keeps out of Git.',
122
237
  `!${PROVENANCE}`,
123
238
  ...[...house, ...granted].map((rule) => `!${rule}`),
124
239
  '',
125
240
  ].join('\n'));
126
241
  }
127
- function revisionAt(copy, commit, path) {
242
+ async function modelOf(copy, files) {
243
+ const entry = files.get(SCHEMA);
244
+ if (entry !== undefined)
245
+ return readTypes(await blobText(copy.repository, entry), HOUSE_ROOM_TYPES);
246
+ return readTypes(copy.read_only.includes(SCHEMA) ? await readFile(join(copy.repository, SCHEMA), 'utf8') : '', HOUSE_ROOM_TYPES);
247
+ }
248
+ function entryAt(copy, commit, path) {
128
249
  let manifest = copy.manifests[commit];
129
250
  while (manifest !== undefined) {
130
- if (path in manifest.files)
131
- return manifest.files[path] ?? undefined;
251
+ if (path in manifest.files) {
252
+ const revision = manifest.files[path];
253
+ if (revision === null || revision === undefined)
254
+ return undefined;
255
+ const held = manifest.binary?.[path];
256
+ return held === undefined ? { revision } : { revision, held };
257
+ }
132
258
  manifest = manifest.parent === null ? undefined : copy.manifests[manifest.parent];
133
259
  }
134
260
  return undefined;
135
261
  }
262
+ function revisionAt(copy, commit, path) {
263
+ return entryAt(copy, commit, path)?.revision;
264
+ }
265
+ function heldAt(copy, commit, path) {
266
+ return entryAt(copy, commit, path)?.held;
267
+ }
268
+ function manifest(parent, files, binary) {
269
+ return Object.keys(binary).length === 0 ? { parent, files } : { parent, files, binary };
270
+ }
271
+ function binaryParts(changes) {
272
+ return changes.filter((change) => change.op === 'upload');
273
+ }
274
+ function pushedBinary(copy, push) {
275
+ const sent = new Map();
276
+ for (const change of push.changes) {
277
+ if (change.op === 'upload')
278
+ sent.set(relative(change.path), { bytes: change.bytes, sha256: change.sha256 });
279
+ else if (change.op === 'rename') {
280
+ const held = heldAt(copy, push.base, relative(change.path));
281
+ if (held !== undefined)
282
+ sent.set(relative(change.to), held);
283
+ }
284
+ }
285
+ return sent;
286
+ }
287
+ async function writtenAt(copy, house, path) {
288
+ const named = `${rootOf(copy.room_handle)}/${path}`;
289
+ const entry = house.get(path);
290
+ if (entry === undefined)
291
+ return { path: named, revision: null, content: null };
292
+ const revision = revisionAt(copy, copy.house, path) ?? null;
293
+ return heldAt(copy, copy.house, path) === undefined
294
+ ? { path: named, revision, content: await blobText(copy.repository, entry) }
295
+ : { path: named, revision };
296
+ }
136
297
  function without(copy) {
137
298
  const { push, ...settled } = copy;
138
299
  return settled;
@@ -148,30 +309,73 @@ function movedPaths(moves, before, after) {
148
309
  .filter((pair) => pairs.filter((other) => other.from === pair.from || other.to === pair.to).length === 1)
149
310
  .map(({ from, to }) => [from, to]));
150
311
  }
312
+ function sameBytesMoved(before, after, held, binary, intended) {
313
+ const arriving = new Set(intended.values());
314
+ const leaving = new Map();
315
+ const added = new Map();
316
+ const listed = (paths, file, path) => {
317
+ paths.set(heldKey(file), [...(paths.get(heldKey(file)) ?? []), path]);
318
+ };
319
+ for (const [path, entry] of before) {
320
+ const file = held(path);
321
+ if (file !== undefined && !intended.has(path) && !binary.has(path) && after.get(path) !== entry)
322
+ listed(leaving, file, path);
323
+ }
324
+ for (const [path, file] of binary) {
325
+ if (!arriving.has(path) && held(path) === undefined)
326
+ listed(added, file, path);
327
+ }
328
+ const moved = new Map();
329
+ for (const [key, [from, ...others]] of leaving) {
330
+ const [to, ...more] = added.get(key) ?? [];
331
+ if (others.length === 0 && more.length === 0 && to !== undefined && !after.has(from) && !before.has(to))
332
+ moved.set(from, to);
333
+ }
334
+ return moved;
335
+ }
151
336
  export class LocalCopies {
152
337
  owner;
153
338
  house;
154
339
  copies = new Map();
155
340
  queues = new Map();
156
- delivering = new Set();
157
- selecting = Promise.resolve();
341
+ waiting = new Set();
342
+ answering = Promise.resolve();
158
343
  constructor(house) {
159
344
  this.house = house;
160
345
  this.owner = {
161
346
  key: 'owner',
162
347
  submit: async (operation, changes) => {
163
348
  try {
164
- const outcome = await house.post('/kit/door/batch', { operation_id: operation, changes });
165
- return { accepted: outcome.contents };
349
+ const outcome = await retrying(() => stopping(house.door('files/batch', { operation_id: operation, changes })));
350
+ return 'refused' in outcome ? outcome : { accepted: outcome.contents };
166
351
  }
167
352
  catch (error) {
168
- if (!(error instanceof HouseRefusal) || error.status >= 500)
353
+ if (!finalRefusal(error))
169
354
  throw error;
170
355
  return {
171
- refused: refusalOf(error.text) ?? { code: '', text: `House refused it with ${error.status}`, conflicts: [] },
356
+ refused: refusalOf(error.text) ?? { code: '', text: `House refused it with ${error.status}`, conflicts: [], detail: {} },
172
357
  };
173
358
  }
174
359
  },
360
+ finish: async (upload, _room, partSha256) => {
361
+ try {
362
+ return (await house.act('uploads/finish', { upload, part_sha256: partSha256 }));
363
+ }
364
+ catch (error) {
365
+ if (!(error instanceof HouseRefusal))
366
+ throw new Unsettled(causeOf(error));
367
+ throw retryable(error.text) === false ? new Error(houseCause(error)) : new Unsettled(houseCause(error));
368
+ }
369
+ },
370
+ info: async (path, revision) => {
371
+ const read = toolAnswer({
372
+ status: 200,
373
+ text: await ownAgentCall(rpc('tools/call', { name: 'info', arguments: { path, ...(revision === undefined ? {} : { ref: revision }) } })),
374
+ });
375
+ if ('refused' in read)
376
+ throw new Error(read.refused.text);
377
+ return fileInfo(read.text);
378
+ },
175
379
  };
176
380
  }
177
381
  async load() {
@@ -185,43 +389,117 @@ export class LocalCopies {
185
389
  this.copies.set(copy.key, copy);
186
390
  }
187
391
  }
188
- async mapping() {
189
- await this.selecting;
190
- return [...this.copies.values()]
191
- .filter((copy) => copy.active)
192
- .map((copy) => `${rootOf(copy.room_handle)}: ${copy.repository}`)
193
- .sort();
194
- }
195
- select() {
196
- const selected = this.selecting.then(() => this.reselect());
197
- this.selecting = selected.catch(() => undefined);
198
- return selected;
199
- }
200
- async reselect() {
201
- const { local_copy: chosen } = await this.house.post('/kit/local-copy/selection', {});
202
- const selected = (chosen?.rooms ?? []).flatMap((room) => room.room_handle === null ? [] : [{ key: `rooms/${room.room_ref}`, handle: room.room_handle }]);
203
- const keys = new Set(selected.map((scope) => scope.key));
204
- const work = [];
205
- for (const key of this.copies.keys()) {
206
- if (keys.has(key))
207
- continue;
208
- work.push(this.exclusive(key, async () => {
209
- const copy = this.copies.get(key);
210
- if (copy.active)
211
- await this.save({ ...copy, active: false });
212
- }));
213
- }
214
- for (const scope of selected)
215
- work.push(this.exclusive(scope.key, () => this.apply(scope)).catch(logged));
216
- await Promise.all(work);
217
- }
218
- entries(frame) {
219
- const copy = [...this.copies.values()].find((held) => held.active && held.room_ref === frame.authority);
220
- if (copy === undefined)
221
- return;
222
- if (copy.log_epoch === frame.log_epoch && BigInt(frame.position) <= BigInt(copy.position))
223
- return;
224
- this.deliver(copy.key);
392
+ async swept() {
393
+ const rooms = join(localCopies(), 'rooms');
394
+ for (const name of await readdir(rooms).catch(() => [])) {
395
+ if (name.startsWith(stagingPrefix()))
396
+ await rm(join(rooms, name), { recursive: true, force: true });
397
+ }
398
+ for (const copy of this.copies.values())
399
+ await sweptTransfers(copy.repository);
400
+ }
401
+ connected() {
402
+ return this.selected();
403
+ }
404
+ fetch(caller) {
405
+ return this.answered(() => this.fetched(true, caller));
406
+ }
407
+ async selected() {
408
+ const copied = await this.answered(() => this.fetched(false, this.owner));
409
+ if (copied.refused)
410
+ logged(copied.text);
411
+ }
412
+ answered(work) {
413
+ const answered = this.answering.then(work);
414
+ this.answering = answered.catch(() => undefined);
415
+ return answered;
416
+ }
417
+ async fetched(refreshing, caller) {
418
+ const { local_copy: answer } = await this.house.act('local-copies/selection/read', {});
419
+ const synced = new Map(answer.rooms.map((room) => [`${ROOMS}${room.room_ref}`, room.room_handle]));
420
+ return this.holding([...new Set([...this.copies.keys(), ...synced.keys()])].sort(), async () => {
421
+ const said = [];
422
+ let refused = false;
423
+ const mark = refreshing ? 'said' : 'unsaid';
424
+ for (const copy of [...this.copies.values()]) {
425
+ if (synced.has(copy.key) || copy.unfollowed === 'said' || copy.unfollowed === mark)
426
+ continue;
427
+ await this.save({ ...copy, unfollowed: mark });
428
+ if (refreshing)
429
+ said.push(`${rootOf(copy.room_handle)}: no longer followed; kept as it is`);
430
+ }
431
+ const current = [];
432
+ const wanted = new Map();
433
+ for (const [key, handle] of synced) {
434
+ try {
435
+ const held = this.copies.get(key);
436
+ if (held === undefined || !(await exists(held.repository))) {
437
+ wanted.set(key, handle);
438
+ continue;
439
+ }
440
+ const { unfollowed, ...followed } = held;
441
+ current.push(unfollowed === undefined && held.room_handle === handle ? held : await this.save({ ...followed, room_handle: handle }));
442
+ }
443
+ catch (error) {
444
+ refused = true;
445
+ said.push(`${rootOf(handle)}: ${causeOf(error)}`);
446
+ }
447
+ }
448
+ const unready = [];
449
+ const unsettled = [];
450
+ if (wanted.size > 0) {
451
+ const { authorities } = await this.discovered([...wanted.values()]);
452
+ for (const [key, handle] of wanted) {
453
+ const authority = authorities.find((entry) => `${ROOMS}${entry.room_ref}` === key);
454
+ try {
455
+ if (authority === undefined)
456
+ throw new Error('House serves nothing of it to copy');
457
+ said.push(await this.copied({ key, handle, authority }));
458
+ }
459
+ catch (error) {
460
+ if (error instanceof NotReady) {
461
+ unready.push(...error.rooms);
462
+ said.push(`${rootOf(handle)}: ${NOT_READY}`);
463
+ }
464
+ else {
465
+ refused = true;
466
+ unsettled.push(key.slice(ROOMS.length));
467
+ said.push(`${rootOf(handle)}: ${causeOf(error)}`);
468
+ }
469
+ }
470
+ }
471
+ }
472
+ const reading = refreshing ? current : current.filter((copy) => this.waiting.has(copy.room_ref));
473
+ if (reading.length > 0) {
474
+ const failed = await this.refreshed(reading, unready, caller).then(() => null, houseCause);
475
+ for (const copy of reading) {
476
+ said.push(...(unready.includes(copy.room_ref) ? [`${rootOf(copy.room_handle)}: ${NOT_READY}`] : moved(copy, this.copies.get(copy.key))));
477
+ }
478
+ if (failed !== null) {
479
+ refused = true;
480
+ unsettled.push(...reading.map((copy) => copy.room_ref));
481
+ said.push(failed);
482
+ }
483
+ }
484
+ for (const room of [...this.waiting]) {
485
+ if (!unready.includes(room) && !unsettled.includes(room))
486
+ this.waiting.delete(room);
487
+ }
488
+ const due = unready.filter((room) => !this.waiting.has(room));
489
+ for (const room of due)
490
+ this.waiting.add(room);
491
+ if (due.length > 0)
492
+ void this.askedAgain(due);
493
+ return { refused, text: said.length === 0 ? 'Nothing new from House.' : said.join('\n') };
494
+ });
495
+ }
496
+ async askedAgain(rooms) {
497
+ let asking = rooms;
498
+ for (let step = 0; asking.length > 0; step++) {
499
+ await paused(ASKED_AGAIN_MS[Math.min(step, ASKED_AGAIN_MS.length - 1)]);
500
+ await this.selected().catch(logged);
501
+ asking = asking.filter((room) => this.waiting.has(room));
502
+ }
225
503
  }
226
504
  async observed(report) {
227
505
  const key = await this.keyAt(dirname(report.common));
@@ -251,11 +529,11 @@ export class LocalCopies {
251
529
  async push(cwd, selector, caller) {
252
530
  const located = await run(cwd, ['rev-parse', '--path-format=absolute', '--git-common-dir', '--show-toplevel']);
253
531
  if (located.status !== 0)
254
- return { refused: true, text: `${cwd} is not a House Local copy; run house push inside one` };
532
+ return outside(cwd);
255
533
  const [common, worktree] = located.stdout.toString('utf8').split('\n');
256
534
  const key = await this.keyAt(dirname(common));
257
535
  if (key === undefined)
258
- return { refused: true, text: `${worktree} is not a House Local copy; run house push inside one` };
536
+ return outside(worktree);
259
537
  return this.exclusive(key, () => this.pushed(key, worktree, selector, caller));
260
538
  }
261
539
  async keyAt(repository) {
@@ -271,19 +549,12 @@ export class LocalCopies {
271
549
  this.queues.set(key, queued.catch(() => undefined));
272
550
  return queued;
273
551
  }
274
- deliver(key) {
275
- if (this.delivering.has(key))
276
- return;
277
- this.delivering.add(key);
278
- void this.exclusive(key, async () => {
279
- this.delivering.delete(key);
280
- const copy = this.copies.get(key);
281
- if (copy.active)
282
- await this.save(await this.caughtUp(copy));
283
- }).catch(logged);
552
+ holding(keys, work) {
553
+ const [key, ...rest] = keys;
554
+ return key === undefined ? work() : this.exclusive(key, () => this.holding(rest, work));
284
555
  }
285
556
  async save(copy) {
286
- const path = join(kitHome(), 'local-copies', `${copy.key}.json`);
557
+ const path = recordOf(copy.key);
287
558
  await mkdir(dirname(path), { recursive: true, mode: 0o700 });
288
559
  const temporary = `${path}.${process.pid}`;
289
560
  await writeFile(temporary, JSON.stringify(copy), { mode: 0o600 });
@@ -291,171 +562,296 @@ export class LocalCopies {
291
562
  this.copies.set(copy.key, copy);
292
563
  return copy;
293
564
  }
294
- async read(handle) {
295
- const prefixes = [rootOf(handle)];
296
- const discovery = await this.house.post('/kit/door/discover', { prefixes });
297
- let bundle = await this.house.post('/kit/door/bootstrap', { prefixes });
298
- if ('transfer' in bundle)
299
- bundle = (await (await fetch(relayed(bundle.transfer.url))).json());
300
- const [view] = bundle.scopes;
301
- if (view === undefined)
302
- return null;
303
- const authority = discovery.authorities.find((entry) => entry.room_ref === view.manifest.anchor);
304
- const contents = new Map(view.contents.map((entry) => [entry.path, entry.content]));
305
- const read = {
306
- anchor: view.manifest.anchor,
307
- position: view.manifest.position,
308
- log_epoch: view.manifest.log_epoch,
309
- write: authority.operations.write,
310
- protected: authority.protected_paths,
311
- writable: new Map(),
312
- revisions: {},
313
- readOnly: new Map(),
314
- };
315
- for (const file of view.manifest.files) {
316
- const content = contents.get(file.path);
317
- if (file.writable) {
318
- read.writable.set(file.path, content);
319
- read.revisions[file.path] = file.revision;
320
- }
321
- else {
322
- read.readOnly.set(file.path, content);
323
- }
324
- if (file.provenance !== undefined)
325
- read.readOnly.set(sidecar(file.path), file.provenance);
326
- }
327
- return read;
565
+ async discovered(handles) {
566
+ const discover = (group) => this.house.door('rooms/discover', { prefixes: group.map(rootOf) });
567
+ const [first, ...rest] = handles;
568
+ const { bounds, authorities } = await discover([first]);
569
+ for (const group of grouped(rest, bounds.max_prefixes))
570
+ authorities.push(...(await discover(group)).authorities);
571
+ return { bounds, authorities };
328
572
  }
329
- async apply(scope) {
330
- let copy = this.copies.get(scope.key);
331
- if (copy === undefined || !(await exists(copy.repository))) {
332
- await this.materialize(scope);
333
- return;
334
- }
335
- if (copy.room_handle !== scope.handle)
336
- copy = await this.save({ ...copy, room_handle: scope.handle });
337
- if (!copy.active) {
338
- await this.save(await this.received({ ...copy, active: true }));
339
- return;
340
- }
341
- const discovery = await this.house.post('/kit/door/discover', {
342
- prefixes: [rootOf(copy.room_handle)],
343
- });
344
- const authority = discovery.authorities.find((entry) => entry.room_ref === copy.room_ref);
345
- if (authority === undefined)
346
- return;
347
- if (authority.managed_replication !== 'admitted') {
348
- logged(`operation_denied: ${rootOf(copy.room_handle)} is not synchronized to this Environment`);
349
- return;
350
- }
351
- if (JSON.stringify([authority.operations.write, authority.protected_paths]) !==
352
- JSON.stringify([copy.write, copy.protected])) {
353
- await this.save(await this.received(copy));
354
- return;
573
+ async chunked(root, room, directory) {
574
+ await mkdir(directory, { recursive: true });
575
+ for (let renewed = false;; renewed = true) {
576
+ const { rooms } = await this.house.door('room-chunks/list', { prefixes: [root] });
577
+ const list = rooms.find((entry) => entry.room_ref === room);
578
+ if (list === undefined)
579
+ throw new Error('House serves nothing of it to copy');
580
+ if (!list.ready)
581
+ throw new NotReady([room]);
582
+ const held = new Set(await readdir(directory));
583
+ const failure = await stagedChunks(directory, list.chunks.filter(({ hash }) => !held.has(hash)));
584
+ if (failure === undefined) {
585
+ return { position: list.position, log_epoch: list.log_epoch, entries: chunkEntries(directory, list.chunks, list.models) };
586
+ }
587
+ if (renewed && (await readdir(directory)).length === held.size)
588
+ throw failure;
355
589
  }
356
- await this.save(await this.caughtUp(copy));
357
590
  }
358
- async materialize(scope) {
359
- const read = await this.read(scope.handle);
360
- if (read === null) {
361
- logged(`House serves nothing of ${rootOf(scope.handle)} to copy`);
362
- return;
363
- }
364
- const repository = join(localCopies(), 'rooms', read.anchor);
365
- if (await exists(repository)) {
366
- logged(`${repository} already exists and House Kit leaves it as it is`);
367
- return;
368
- }
369
- const staging = join(dirname(repository), `.${randomUUID()}`);
591
+ async materialized({ key, handle, authority }, whole) {
592
+ const repository = join(localCopies(), 'rooms', authority.room_ref);
593
+ if (await exists(repository))
594
+ throw new Error(`${repository} already exists and Kit leaves it as it is`);
595
+ const staging = join(dirname(repository), `${stagingPrefix()}${randomUUID()}`);
596
+ const scope = { write: authority.operations.write, protected: authority.protected_paths };
370
597
  await mkdir(staging, { recursive: true });
371
- let house;
372
- let readOnly;
598
+ let copy;
373
599
  try {
374
600
  await git(staging, ['init', '-q', '-b', BRANCH]);
375
- house = await importCommits(staging, [
376
- { message: `House ${rootOf(scope.handle)}`, from: null, files: read.writable },
377
- ]);
601
+ await git(staging, ['config', 'core.precomposeUnicode', 'true']);
602
+ const read = await whole(join(staging, '.git', STAGED_CHUNKS));
603
+ const readOnly = await readOnlyFiles(staging, []);
604
+ const commit = { message: `House ${rootOf(handle)}`, from: null };
605
+ const { house, revisions } = await importedWhole(staging, scope, read.entries, commit, readOnly, async () => new Map());
378
606
  await git(staging, ['update-ref', `refs/heads/${BRANCH}`, house]);
379
607
  await git(staging, ['reset', '-q', '--hard']);
380
- readOnly = await readOnlyPlaced(staging, [], read.readOnly, []);
381
- await ignoreRules(staging, [...read.writable.keys()], read);
608
+ copy = {
609
+ key,
610
+ room_ref: authority.room_ref,
611
+ room_handle: handle,
612
+ repository,
613
+ house,
614
+ position: read.position,
615
+ log_epoch: read.log_epoch,
616
+ ...scope,
617
+ read_only: readOnly.held(),
618
+ manifests: { [house]: { parent: null, files: revisions } },
619
+ moves: [],
620
+ };
621
+ await ignoreRules(staging, Object.keys(revisions), copy);
622
+ await rm(join(staging, '.git', STAGED_CHUNKS), { recursive: true, force: true });
382
623
  await rename(staging, repository);
383
624
  }
384
625
  catch (error) {
385
626
  await rm(staging, { recursive: true, force: true });
386
627
  throw error;
387
628
  }
388
- await this.save({
389
- key: scope.key,
390
- room_ref: read.anchor,
391
- room_handle: scope.handle,
392
- repository,
393
- active: true,
394
- house,
395
- position: read.position,
396
- log_epoch: read.log_epoch,
397
- write: read.write,
398
- protected: read.protected,
399
- read_only: readOnly,
400
- manifests: { [house]: { parent: null, files: read.revisions } },
401
- moves: [],
402
- });
629
+ return this.save(copy);
403
630
  }
404
- async received(copy) {
405
- const read = await this.read(copy.room_handle);
406
- if (read === null)
407
- return copy;
408
- const house = await importCommits(copy.repository, [
409
- { message: `House ${rootOf(copy.room_handle)} at position ${read.position}`, from: copy.house, replaces: true, files: read.writable },
410
- ]);
411
- const removed = copy.read_only.filter((path) => !read.readOnly.has(path));
412
- const readOnly = await readOnlyPlaced(copy.repository, copy.read_only, read.readOnly, removed);
413
- const next = {
414
- ...copy,
415
- house,
416
- position: read.position,
417
- log_epoch: read.log_epoch,
418
- write: read.write,
419
- protected: read.protected,
420
- read_only: readOnly,
421
- manifests: { ...copy.manifests, [house]: { parent: null, files: read.revisions } },
422
- };
423
- await ignoreRules(copy.repository, [...read.writable.keys()], next);
424
- return next;
631
+ async copied(wanted) {
632
+ const { handle, authority } = wanted;
633
+ const root = rootOf(handle);
634
+ let answer = null;
635
+ try {
636
+ if (!this.waiting.has(authority.room_ref)) {
637
+ answer = await this.house.door('changes/enumerate', {
638
+ prefixes: [root],
639
+ positions: [
640
+ { authority: authority.room_ref, position: '0', log_epoch: authority.log_epoch, page: { through: authority.position, read: 0 } },
641
+ ],
642
+ });
643
+ }
644
+ }
645
+ catch (error) {
646
+ if (expiredAuthority(error) !== authority.room_ref)
647
+ throw error;
648
+ }
649
+ const view = answer?.authorities.find((entry) => entry.authority === authority.room_ref);
650
+ if (answer !== null && view === undefined)
651
+ throw new Error('House serves nothing of it to copy');
652
+ if (view !== undefined && view.next === undefined) {
653
+ const entries = view.changes.filter((change) => 'content' in change);
654
+ const copy = await this.materialized(wanted, async () => ({ position: view.position, log_epoch: view.log_epoch, entries }));
655
+ return `${root}: copied to ${copy.repository}`;
656
+ }
657
+ const copy = await this.materialized(wanted, (directory) => this.chunked(root, authority.room_ref, directory));
658
+ const [caught] = await this.caughtUp([copy], true, this.owner);
659
+ await succeeded(copy.repository, ['merge', '--ff-only', '-q', caught.house]);
660
+ return `${root}: copied to ${copy.repository}`;
425
661
  }
426
- async caughtUp(start) {
427
- let copy = start;
428
- for (;;) {
662
+ async received(copy, authority, caller) {
663
+ const root = rootOf(copy.room_handle);
664
+ const directory = join(copy.repository, '.git', STAGED_CHUNKS);
665
+ const scope = { write: authority.operations.write, protected: authority.protected_paths };
666
+ try {
667
+ const read = await this.chunked(root, copy.room_ref, directory);
668
+ const readOnly = await readOnlyFiles(copy.repository, copy.read_only);
669
+ for (const path of copy.read_only)
670
+ await readOnly.remove(path);
671
+ const commit = { message: `House ${root} at position ${read.position}`, from: copy.house, replaces: true };
672
+ const houseTree = await tree(copy.repository, copy.house);
673
+ const pushing = await this.pushing(copy);
674
+ const kept = async (entries) => {
675
+ const present = new Set(entries.map((entry) => entry.path));
676
+ const leaving = new Map([...houseTree].flatMap(([path, blob]) => {
677
+ const held = present.has(path) ? undefined : heldAt(copy, copy.house, path);
678
+ return held === undefined ? [] : [[heldKey(held), blob]];
679
+ }));
680
+ const blobs = new Map();
681
+ for (const entry of entries) {
682
+ const blob = await this.binaryIn(copy, houseTree, entry, pushing, leaving, caller);
683
+ if (blob !== null)
684
+ blobs.set(entry.path, blob);
685
+ }
686
+ return blobs;
687
+ };
688
+ const { house, revisions, binary } = await importedWhole(copy.repository, scope, read.entries, commit, readOnly, kept);
689
+ const next = {
690
+ ...copy,
691
+ ...scope,
692
+ house,
693
+ position: read.position,
694
+ log_epoch: read.log_epoch,
695
+ read_only: readOnly.held(),
696
+ manifests: { ...copy.manifests, [house]: manifest(null, revisions, binary) },
697
+ };
698
+ await ignoreRules(copy.repository, Object.keys(revisions), next);
699
+ return next;
700
+ }
701
+ finally {
702
+ await rm(directory, { recursive: true, force: true });
703
+ }
704
+ }
705
+ async reread(copy, caller) {
706
+ const { authorities } = await this.discovered([copy.room_handle]);
707
+ const authority = authorities.find((entry) => entry.room_ref === copy.room_ref);
708
+ if (authority === undefined)
709
+ throw new NotReady([copy.room_ref]);
710
+ return this.received(copy, authority, caller);
711
+ }
712
+ async refreshed(copies, unready, caller) {
713
+ const { bounds, authorities } = await this.discovered(copies.map((copy) => copy.room_handle));
714
+ const read = [];
715
+ const unchanged = [];
716
+ for (const copy of copies) {
717
+ const authority = authorities.find((entry) => entry.room_ref === copy.room_ref);
718
+ if (authority !== undefined &&
719
+ JSON.stringify([authority.operations.write, authority.protected_paths]) !== JSON.stringify([copy.write, copy.protected])) {
720
+ try {
721
+ read.push(await this.save(await this.received(copy, authority, caller)));
722
+ }
723
+ catch (error) {
724
+ if (!(error instanceof NotReady))
725
+ throw error;
726
+ unready.push(...error.rooms);
727
+ }
728
+ }
729
+ else {
730
+ unchanged.push(copy);
731
+ }
732
+ }
733
+ for (const group of grouped(unchanged, bounds.max_prefixes))
734
+ await this.caughtUp(group, false, caller, unready);
735
+ for (const group of grouped(read, bounds.max_prefixes))
736
+ await this.caughtUp(group, true, caller, unready);
737
+ }
738
+ async caughtUp(copies, whole, caller, unready) {
739
+ const caught = new Map(copies.map((copy) => [copy.key, copy]));
740
+ const read = new Set();
741
+ const waiting = unready ?? [];
742
+ let asking = [...copies];
743
+ while (asking.length > 0) {
429
744
  let answer;
430
745
  try {
431
- answer = await this.house.post('/kit/door/enumerate', {
432
- prefixes: [rootOf(copy.room_handle)],
433
- positions: [{ authority: copy.room_ref, position: copy.position, log_epoch: copy.log_epoch }],
746
+ answer = await this.house.door('changes/enumerate', {
747
+ prefixes: asking.map((copy) => rootOf(copy.room_handle)),
748
+ positions: asking.map((copy) => ({ authority: copy.room_ref, position: copy.position, log_epoch: copy.log_epoch })),
434
749
  });
435
750
  }
436
751
  catch (error) {
437
- const code = error instanceof HouseRefusal ? (refusalOf(error.text)?.code ?? null) : null;
438
- if (code === 'position_expired')
439
- return this.received(copy);
440
- if (code !== 'replica_behind')
752
+ const expired = asking.find((copy) => copy.room_ref === expiredAuthority(error));
753
+ if (expired === undefined)
441
754
  throw error;
442
- setTimeout(() => this.deliver(copy.key), REPLICA_RETRY_MS).unref();
443
- return copy;
755
+ asking = asking.filter((copy) => copy !== expired);
756
+ if (read.has(expired.key)) {
757
+ waiting.push(expired.room_ref);
758
+ continue;
759
+ }
760
+ read.add(expired.key);
761
+ try {
762
+ const next = await this.save(await this.reread(expired, caller));
763
+ caught.set(expired.key, next);
764
+ asking.push(next);
765
+ }
766
+ catch (refused) {
767
+ if (!(refused instanceof NotReady))
768
+ throw refused;
769
+ waiting.push(expired.room_ref);
770
+ }
771
+ continue;
772
+ }
773
+ const advanced = [];
774
+ for (const copy of asking) {
775
+ const view = answer.authorities.find((entry) => entry.authority === copy.room_ref);
776
+ if (view === undefined || (view.position === copy.position && view.log_epoch === copy.log_epoch))
777
+ continue;
778
+ const next = await this.save(await this.incorporated(copy, view.changes, view.position, view.log_epoch, caller));
779
+ caught.set(copy.key, next);
780
+ advanced.push(next);
444
781
  }
445
- const view = answer.authorities.find((entry) => entry.authority === copy.room_ref);
446
- if (view === undefined || (view.position === copy.position && view.log_epoch === copy.log_epoch))
447
- return copy;
448
- copy = await this.save(await this.incorporated(copy, view.changes, view.position, view.log_epoch));
782
+ asking = advanced.filter((copy) => whole || read.has(copy.key));
449
783
  }
784
+ if (unready === undefined && waiting.length > 0)
785
+ throw new NotReady(waiting);
786
+ return copies.map((copy) => caught.get(copy.key));
787
+ }
788
+ async pushing(copy) {
789
+ const sent = copy.push === undefined ? new Map() : pushedBinary(copy, copy.push);
790
+ if (sent.size === 0)
791
+ return new Map();
792
+ const commit = await tree(copy.repository, copy.push.commit);
793
+ return new Map([...sent].map(([path, held]) => [path, { ...held, blob: commit.get(path) }]));
450
794
  }
451
- async incorporated(copy, changes, position, logEpoch) {
795
+ async binaryIn(copy, houseTree, change, pushing, leaving, caller) {
796
+ const pushed = pushing.get(change.path);
797
+ if (pushed !== undefined && heldKey(pushed) === heldKey(change))
798
+ return pushed.blob;
799
+ const moved = leaving.get(heldKey(change));
800
+ const kept = heldAt(copy, copy.house, change.path) ?? pushed;
801
+ if (kept === undefined)
802
+ return moved ?? null;
803
+ if (heldKey(kept) === heldKey(change))
804
+ return houseTree.get(change.path);
805
+ return moved ?? this.revisionBlob(copy, change.path, change.revision, caller);
806
+ }
807
+ revisionBlob(copy, path, revision, caller, first = null) {
808
+ const named = `${rootOf(copy.room_handle)}/${path}`;
809
+ let answered = first;
810
+ return receivedBinary(copy.repository, named, async () => {
811
+ const file = answered ?? (await caller.info(named, revision));
812
+ answered = null;
813
+ if (file === null)
814
+ throw new Error(`House holds no binary file at ${named}`);
815
+ return file;
816
+ });
817
+ }
818
+ async incorporated(copy, changes, position, logEpoch, caller) {
452
819
  const houseTree = await tree(copy.repository, copy.house);
453
820
  const held = new Set(copy.read_only);
454
821
  const files = new Map();
455
822
  const revisions = {};
823
+ const binary = {};
456
824
  const written = new Map();
457
825
  const removed = [];
826
+ const pushing = await this.pushing(copy);
827
+ const leaving = new Map(changes.flatMap((change) => {
828
+ const kept = 'removed' in change ? heldAt(copy, copy.house, change.path) : undefined;
829
+ return kept === undefined ? [] : [[heldKey(kept), houseTree.get(change.path)]];
830
+ }));
458
831
  for (const change of changes) {
832
+ if (!('removed' in change) && !('content' in change)) {
833
+ const blob = await this.binaryIn(copy, houseTree, change, pushing, leaving, caller);
834
+ if (blob === null) {
835
+ if (houseTree.has(change.path)) {
836
+ files.set(change.path, null);
837
+ revisions[change.path] = null;
838
+ }
839
+ else if (held.has(change.path))
840
+ removed.push(change.path, sidecar(change.path));
841
+ continue;
842
+ }
843
+ const kept = heldAt(copy, copy.house, change.path);
844
+ const unchanged = houseTree.get(change.path) === blob &&
845
+ kept !== undefined &&
846
+ heldKey(kept) === heldKey(change) &&
847
+ revisionAt(copy, copy.house, change.path) === change.revision;
848
+ if (unchanged)
849
+ continue;
850
+ files.set(change.path, { blob });
851
+ revisions[change.path] = change.revision;
852
+ binary[change.path] = { bytes: change.bytes, sha256: change.sha256 };
853
+ continue;
854
+ }
459
855
  const gone = 'removed' in change;
460
856
  if (houseTree.has(change.path) || (!gone && !held.has(change.path) && declaredWritable(change.path, copy))) {
461
857
  const unchanged = gone
@@ -481,7 +877,7 @@ export class LocalCopies {
481
877
  house = await importCommits(copy.repository, [
482
878
  { message: `House ${rootOf(copy.room_handle)} at position ${position}`, from: copy.house, files },
483
879
  ]);
484
- manifests = { ...manifests, [house]: { parent: copy.house, files: revisions } };
880
+ manifests = { ...manifests, [house]: manifest(copy.house, revisions, binary) };
485
881
  }
486
882
  const next = {
487
883
  ...copy,
@@ -496,22 +892,28 @@ export class LocalCopies {
496
892
  }
497
893
  async pushed(key, worktree, selector, caller) {
498
894
  let copy = this.copies.get(key);
499
- if (copy.push?.state === 'submitted')
500
- return this.settle(copy, caller);
501
895
  const selected = await run(worktree, ['rev-parse', '--verify', '--quiet', '--end-of-options', `${selector}^{commit}`]);
502
896
  if (selected.status !== 0)
503
897
  return { refused: true, text: `${selector} names no commit` };
504
898
  const commit = selected.stdout.toString('utf8').trim();
899
+ if (copy.push?.state === 'submitted') {
900
+ if (copy.push.commit === commit)
901
+ return this.settle(copy, caller);
902
+ const replaced = await this.recovered(copy, caller, async (current) => {
903
+ await this.save(without(current));
904
+ return { refused: false, text: '' };
905
+ });
906
+ if (replaced.refused)
907
+ return replaced;
908
+ copy = this.copies.get(key);
909
+ }
505
910
  if (copy.push !== undefined) {
506
911
  const held = copy.push.acknowledged;
507
912
  if (held === undefined || !(await succeeded(worktree, ['merge-base', '--is-ancestor', held, commit]))) {
508
- return this.finished(copy);
913
+ return this.finished(copy, caller);
509
914
  }
510
915
  copy = await this.save(without(copy));
511
916
  }
512
- if (!copy.active) {
513
- return { refused: true, text: `${rootOf(copy.room_handle)} is no longer synced to this Environment, so House takes no push from it` };
514
- }
515
917
  const listed = (await git(copy.repository, ['rev-list', '--topo-order', commit])).split('\n');
516
918
  const base = listed.find((ancestor) => ancestor in copy.manifests);
517
919
  if (base === undefined) {
@@ -522,7 +924,7 @@ export class LocalCopies {
522
924
  changes = await this.derived(copy, base, commit);
523
925
  }
524
926
  catch (error) {
525
- return { refused: true, text: causeOf(error) };
927
+ return { refused: true, text: houseCause(error) };
526
928
  }
527
929
  if (changes.length === 0)
528
930
  return { refused: false, text: `Nothing to submit: ${short(commit)} matches House.` };
@@ -543,13 +945,33 @@ export class LocalCopies {
543
945
  });
544
946
  return this.settle(submitted, caller);
545
947
  }
948
+ async binaryFiles(copy, arriving) {
949
+ if (arriving.length === 0)
950
+ return new Map();
951
+ const { bounds } = await this.house.door('rooms/discover', { prefixes: [rootOf(copy.room_handle)] });
952
+ const { binary, markdown } = await sorted(copy.repository, arriving, bounds.max_authored_file_bytes);
953
+ if (markdown.length > 0)
954
+ throw new Error(markdownOnly(markdown, bounds.max_authored_file_bytes));
955
+ return binary;
956
+ }
546
957
  async derived(copy, base, commit) {
547
958
  const root = rootOf(copy.room_handle);
548
959
  const before = await tree(copy.repository, base);
549
960
  const after = await tree(copy.repository, commit);
550
- const moved = movedPaths(copy.moves, before, after);
961
+ const binary = await this.binaryFiles(copy, [...after].filter(([path, entry]) => before.get(path) !== entry));
962
+ const held = (path) => heldAt(copy, base, path);
963
+ const heldBinary = (path) => held(path) !== undefined;
964
+ const intended = new Map([...movedPaths(copy.moves, before, after)].filter(([from, to]) => heldBinary(from) === binary.has(to)));
965
+ const moved = new Map([...intended, ...sameBytesMoved(before, after, held, binary, intended)]);
551
966
  const arrived = new Set(moved.values());
552
- const text = (entry, path) => blobText(copy.repository, entry, path);
967
+ const text = (entry) => blobText(copy.repository, entry);
968
+ const created = async (path, entry) => {
969
+ const part = binary.get(path);
970
+ return part === undefined
971
+ ? { op: 'create', path: `${root}/${path}`, content: await text(entry) }
972
+ : { op: 'upload', path: `${root}/${path}`, ...part };
973
+ };
974
+ const model = await modelOf(copy, after);
553
975
  const changes = [];
554
976
  for (const path of [...new Set([...before.keys(), ...after.keys()])].sort()) {
555
977
  if (arrived.has(path))
@@ -561,56 +983,89 @@ export class LocalCopies {
561
983
  if (to !== undefined) {
562
984
  changes.push({ op: 'rename', path: `${root}/${path}`, to: `${root}/${to}`, base: known });
563
985
  const moving = after.get(to);
986
+ const part = binary.get(to);
564
987
  if (moving !== prior) {
565
- changes.push({ op: 'replace', path: `${root}/${to}`, base: known, content: await text(moving, to) });
988
+ changes.push(part === undefined
989
+ ? { op: 'replace', path: `${root}/${to}`, base: known, content: await text(moving) }
990
+ : { op: 'upload', path: `${root}/${to}`, base: known, ...part });
566
991
  }
567
992
  continue;
568
993
  }
569
994
  if (prior === next)
570
995
  continue;
996
+ const part = binary.get(path);
571
997
  if (next === undefined)
572
998
  changes.push({ op: 'remove', path: `${root}/${path}`, base: known });
573
999
  else if (prior === undefined)
574
- changes.push({ op: 'create', path: `${root}/${path}`, content: await text(next, path) });
1000
+ changes.push(await created(path, next));
1001
+ else if (heldBinary(path) !== (part !== undefined)) {
1002
+ changes.push({ op: 'remove', path: `${root}/${path}`, base: known }, await created(path, next));
1003
+ }
1004
+ else if (part !== undefined)
1005
+ changes.push({ op: 'upload', path: `${root}/${path}`, base: known, ...part });
575
1006
  else
576
- changes.push(...changed(`${root}/${path}`, path, known, await text(prior, path), await text(next, path)));
1007
+ changes.push(...changed(`${root}/${path}`, path, known, await text(prior), await text(next), model));
577
1008
  }
578
1009
  return changes;
579
1010
  }
580
1011
  async settle(start, caller, restaged = false) {
581
1012
  let copy = start;
582
- const push = copy.push;
583
- if (push.caller !== caller.key)
584
- return this.recovered(copy);
585
- let outcome;
586
- try {
587
- outcome = await caller.submit(push.operation_id, push.changes, push.upload, async (upload) => {
588
- copy = await this.save({ ...copy, push: { ...push, upload } });
589
- });
1013
+ if (copy.push.caller !== caller.key) {
1014
+ return this.recovered(copy, caller, async (current) => this.settle(await this.save({ ...current, push: { ...current.push, caller: caller.key } }), caller));
590
1015
  }
591
- catch (error) {
592
- const refusal = error instanceof HouseRefusal ? refusalOf(error.text) : null;
593
- return {
594
- refused: true,
595
- text: refusal?.text ?? `House's answer to ${short(push.commit)} did not arrive; run house push again`,
596
- };
1016
+ const finished = new Set();
1017
+ let outcome;
1018
+ for (;;) {
1019
+ const push = copy.push;
1020
+ try {
1021
+ outcome = await caller.submit(push.operation_id, push.changes, push.upload, async (upload) => {
1022
+ copy = await this.save({ ...copy, push: { ...copy.push, upload } });
1023
+ });
1024
+ }
1025
+ catch (error) {
1026
+ const refusal = error instanceof HouseRefusal ? refusalOf(error.text) : null;
1027
+ return {
1028
+ refused: true,
1029
+ text: refusal?.text ?? `House's answer to ${short(push.commit)} did not arrive; run house push again`,
1030
+ };
1031
+ }
1032
+ if (!('refused' in outcome) || outcome.refused.code !== 'files_unstaged')
1033
+ break;
1034
+ const named = unstagedUploads(outcome.refused).filter(({ id }) => !finished.has(id));
1035
+ if (named.length === 0)
1036
+ break;
1037
+ for (const { id } of named)
1038
+ finished.add(id);
1039
+ const failed = await this.sentUploads(copy, named, caller);
1040
+ copy = this.copies.get(copy.key);
1041
+ if (failed.size > 0)
1042
+ return this.stopped(copy, failed);
597
1043
  }
598
1044
  if ('refused' in outcome) {
1045
+ if (stopsPush(outcome.refused))
1046
+ return this.stopped(copy, firstLine(outcome.refused));
599
1047
  if (outcome.refused.code === 'upload_unavailable' && copy.push.upload !== undefined && !restaged) {
600
1048
  const { upload, ...unstaged } = copy.push;
601
1049
  return this.settle(await this.save({ ...copy, push: unstaged }), caller, true);
602
1050
  }
603
- if (outcome.refused.code === 'operation_expired')
604
- return this.recovered(copy);
605
- const rebased = rebaseFixes(outcome.refused);
1051
+ if (outcome.refused.code === 'expired') {
1052
+ return this.recovered(copy, caller, async (current) => {
1053
+ await this.save(without(current));
1054
+ return { refused: true, text: `House does not hold ${short(current.push.commit)}; run house push again` };
1055
+ });
1056
+ }
606
1057
  let settled = await this.save(without(copy));
1058
+ if (settled.unfollowed !== undefined)
1059
+ return { refused: true, text: outcome.refused.text };
1060
+ const rebased = rebaseFixes(outcome.refused);
607
1061
  try {
608
- settled = await this.save(await this.caughtUp(settled));
1062
+ settled = (await this.caughtUp([settled], true, caller))[0];
1063
+ await this.save(await this.conflicted(settled, outcome.refused, copy.push, caller));
609
1064
  }
610
- catch {
1065
+ catch (error) {
611
1066
  return {
612
1067
  refused: true,
613
- text: rebased ? `${outcome.refused.text}\nHouse's current state did not arrive; push again later` : outcome.refused.text,
1068
+ text: rebased ? `${outcome.refused.text}\nHouse's current state did not arrive: ${houseCause(error)}` : outcome.refused.text,
614
1069
  };
615
1070
  }
616
1071
  return {
@@ -620,37 +1075,91 @@ export class LocalCopies {
620
1075
  : outcome.refused.text,
621
1076
  };
622
1077
  }
623
- return this.finished(await this.save({ ...copy, push: { ...copy.push, state: 'accepted', written: outcome.accepted } }));
1078
+ return this.finished(await this.save({ ...copy, push: { ...copy.push, state: 'accepted', written: outcome.accepted } }), caller);
1079
+ }
1080
+ async sentUploads(start, named, caller) {
1081
+ let copy = start;
1082
+ const parts = new Map(binaryParts(copy.push.changes).map((part) => [part.sha256, part]));
1083
+ const commit = await tree(copy.repository, copy.push.commit);
1084
+ const staged = new Set(copy.push.staged ?? []);
1085
+ const failed = new Map();
1086
+ let saving = Promise.resolve();
1087
+ await eachAtOnce(named, async (upload) => {
1088
+ const part = parts.get(upload.sha256);
1089
+ const path = relative(part.path);
1090
+ try {
1091
+ await sentBinary(copy.repository, commit.get(path), path, part, upload, (id, partSha256) => caller.finish(id, copy.room_ref, partSha256));
1092
+ }
1093
+ catch (error) {
1094
+ failed.set(upload.sha256, `its transfer failed: ${causeOf(error)}`);
1095
+ return;
1096
+ }
1097
+ staged.add(upload.sha256);
1098
+ saving = saving.then(async () => {
1099
+ copy = await this.save({ ...copy, push: { ...copy.push, staged: [...staged] } });
1100
+ });
1101
+ await saving;
1102
+ });
1103
+ return failed;
624
1104
  }
625
- async recovered(copy) {
1105
+ stopped(copy, reasons) {
626
1106
  const push = copy.push;
627
- const current = await this.save(await this.caughtUp(copy));
1107
+ const staged = new Set(push.staged ?? []);
1108
+ const unsent = binaryParts(push.changes).filter(({ sha256 }) => !staged.has(sha256));
1109
+ const unchanged = `House holds nothing of ${short(push.commit)} yet, so ${rootOf(copy.room_handle)} is unchanged`;
1110
+ const again = 'house push again resumes this push and sends only what it has not sent';
1111
+ if (typeof reasons === 'string' && unsent.length === 0)
1112
+ return { refused: true, text: `${unchanged}: ${reasons}\n${again}` };
1113
+ const why = (sha256) => (typeof reasons === 'string' ? reasons : (reasons.get(sha256) ?? 'not sent yet'));
1114
+ return {
1115
+ refused: true,
1116
+ text: [`${unchanged}; these binary files are not sent:`, ...unsent.map(({ path, sha256 }) => `${relative(path)}: ${why(sha256)}`), again].join('\n'),
1117
+ };
1118
+ }
1119
+ async conflicted(copy, refusal, push, caller) {
1120
+ const root = rootOf(copy.room_handle);
1121
+ const touched = new Set(changedPaths(push.changes));
1122
+ const houseTree = await tree(copy.repository, copy.house);
1123
+ const paths = [
1124
+ ...new Set(refusal.conflicts.filter(({ code, path }) => code !== 'path_absent' && touched.has(path)).map(({ path }) => relative(path))),
1125
+ ].filter((path) => !houseTree.has(path));
1126
+ const files = new Map();
1127
+ const revisions = {};
1128
+ const binary = {};
1129
+ for (const path of paths) {
1130
+ const file = await caller.info(`${root}/${path}`);
1131
+ if (file === null)
1132
+ continue;
1133
+ files.set(path, { blob: await this.revisionBlob(copy, path, file.revision, caller, file) });
1134
+ revisions[path] = file.revision;
1135
+ binary[path] = { bytes: file.bytes, sha256: file.sha256 };
1136
+ }
1137
+ if (files.size === 0)
1138
+ return copy;
1139
+ const house = await importCommits(copy.repository, [
1140
+ { message: `House ${root}: the binary files ${short(push.commit)} conflicts with`, from: copy.house, files },
1141
+ ]);
1142
+ return { ...copy, house, manifests: { ...copy.manifests, [house]: manifest(copy.house, revisions, binary) } };
1143
+ }
1144
+ async recovered(copy, caller, unheld) {
1145
+ const push = copy.push;
1146
+ const current = (await this.caughtUp([copy], true, caller))[0];
628
1147
  const house = await tree(copy.repository, current.house);
629
- const root = rootOf(current.room_handle);
630
- if (!(await this.landed(copy.repository, push.changes, house, await tree(copy.repository, push.commit)))) {
631
- await this.save(without(current));
632
- return {
633
- refused: true,
634
- text: `House does not hold ${short(push.commit)}; run house push again`,
635
- };
1148
+ const model = await modelOf(current, house);
1149
+ if (!(await this.landed(copy.repository, push.changes, house, await tree(copy.repository, push.commit), model))) {
1150
+ return unheld(current);
636
1151
  }
637
1152
  const written = [];
638
- for (const path of changedPaths(push.changes).map(relative)) {
639
- const entry = house.get(path);
640
- written.push({
641
- path: `${root}/${path}`,
642
- revision: entry === undefined ? null : (revisionAt(current, current.house, path) ?? null),
643
- content: entry === undefined ? null : await blobText(copy.repository, entry, path),
644
- });
645
- }
646
- return this.finished(await this.save({ ...current, push: { ...push, state: 'accepted', written } }));
1153
+ for (const path of changedPaths(push.changes).map(relative))
1154
+ written.push(await writtenAt(current, house, path));
1155
+ return this.finished(await this.save({ ...current, push: { ...push, state: 'accepted', written } }), caller);
647
1156
  }
648
- async finished(start) {
1157
+ async finished(start, caller) {
649
1158
  let copy = start;
650
1159
  const { commit } = copy.push;
651
1160
  try {
652
1161
  if (copy.push.acknowledged === undefined)
653
- copy = await this.save(await this.acknowledged(copy));
1162
+ copy = await this.save(await this.acknowledged(copy, caller));
654
1163
  const integrated = await this.integrated(copy.push);
655
1164
  if (integrated.done)
656
1165
  await this.save(without(copy));
@@ -663,45 +1172,61 @@ export class LocalCopies {
663
1172
  };
664
1173
  }
665
1174
  }
666
- async acknowledged(start) {
1175
+ async acknowledged(start, caller) {
667
1176
  let copy = start;
668
1177
  const push = copy.push;
669
1178
  let written = push.written;
670
- if (push.changes.some((change) => change.op === 'rename')) {
671
- copy = await this.save(await this.caughtUp(copy));
1179
+ const read = push.changes.some((change) => change.op === 'rename');
1180
+ if (read) {
1181
+ copy = (await this.caughtUp([copy], true, caller))[0];
672
1182
  const house = await tree(copy.repository, copy.house);
673
1183
  const sent = await tree(copy.repository, push.house);
674
- if (push.changes.some((change) => change.op === 'rename' && !house.has(relative(change.to)))) {
1184
+ const unread = push.changes.some((change) => change.op === 'rename' &&
1185
+ house.get(relative(change.path)) === sent.get(relative(change.path)) &&
1186
+ !house.has(relative(change.to)));
1187
+ if (unread)
675
1188
  throw new Error(`House's result for ${short(push.commit)} is not readable yet`);
676
- }
677
1189
  const known = new Set(written.map((entry) => relative(entry.path)));
678
1190
  for (const path of new Set([...sent.keys(), ...house.keys()])) {
679
- const entry = house.get(path);
680
- if (known.has(path) || sent.get(path) === entry)
681
- continue;
682
- written = [
683
- ...written,
684
- {
685
- path: `${rootOf(copy.room_handle)}/${path}`,
686
- revision: entry === undefined ? null : (revisionAt(copy, copy.house, path) ?? null),
687
- content: entry === undefined ? null : await blobText(copy.repository, entry, path),
688
- },
689
- ];
1191
+ if (!known.has(path) && sent.get(path) !== house.get(path))
1192
+ written = [...written, await writtenAt(copy, house, path)];
690
1193
  }
691
1194
  }
692
1195
  const selected = await tree(copy.repository, push.commit);
693
1196
  const houseTree = await tree(copy.repository, copy.house);
1197
+ const parts = pushedBinary(copy, push);
694
1198
  const files = new Map();
695
1199
  const revisions = {};
1200
+ const binary = {};
1201
+ const settled = new Map();
696
1202
  for (const entry of written) {
697
1203
  const path = relative(entry.path);
1204
+ if (entry.revision !== null || !settled.has(path))
1205
+ settled.set(path, entry);
1206
+ }
1207
+ for (const [path, entry] of settled) {
698
1208
  if (!selected.has(path) && !houseTree.has(path) && !declaredWritable(path, copy))
699
1209
  continue;
700
- files.set(path, entry.revision === null ? null : (entry.content ?? (await this.revisionText(entry.path, entry.revision))));
701
1210
  revisions[path] = entry.revision;
1211
+ const part = parts.get(path);
1212
+ const kept = heldAt(copy, copy.house, path);
1213
+ if (entry.revision === null)
1214
+ files.set(path, null);
1215
+ else if (part !== undefined) {
1216
+ files.set(path, { blob: selected.get(path) });
1217
+ binary[path] = part;
1218
+ }
1219
+ else if (kept !== undefined && revisionAt(copy, copy.house, path) === entry.revision) {
1220
+ files.set(path, { blob: houseTree.get(path) });
1221
+ binary[path] = kept;
1222
+ }
1223
+ else
1224
+ files.set(path, entry.content ?? (await this.revisionText(entry.path, entry.revision)));
702
1225
  }
703
1226
  const edits = new Map([...files].filter(([path, content]) => (content === null ? selected.has(path) : selected.get(path) !== blobId(content))));
704
- const received = [...files.keys()].filter((path) => revisionAt(copy, copy.house, path) === revisionAt(copy, push.house, path));
1227
+ const received = read
1228
+ ? []
1229
+ : [...files.keys()].filter((path) => revisionAt(copy, copy.house, path) === revisionAt(copy, push.house, path));
705
1230
  const accepted = `House ${rootOf(copy.room_handle)} with ${push.commit} accepted`;
706
1231
  const house = await importCommits(copy.repository, [
707
1232
  ...(edits.size === 0 ? [] : [{ message: accepted, from: push.commit, files: edits }]),
@@ -719,30 +1244,38 @@ export class LocalCopies {
719
1244
  house,
720
1245
  manifests: {
721
1246
  ...copy.manifests,
722
- [acknowledged]: { parent: push.base, files: revisions },
723
- [house]: { parent: copy.house, files: Object.fromEntries(received.map((path) => [path, revisions[path] ?? null])) },
1247
+ [acknowledged]: manifest(push.base, revisions, binary),
1248
+ [house]: manifest(copy.house, Object.fromEntries(received.map((path) => [path, revisions[path] ?? null])), Object.fromEntries(received.flatMap((path) => (binary[path] === undefined ? [] : [[path, binary[path]]])))),
724
1249
  },
725
1250
  moves: copy.moves.filter(({ from, to }) => !(after.has(to) && !after.has(from))),
726
1251
  push: { ...push, acknowledged },
727
1252
  };
728
1253
  }
729
- async landed(repository, changes, house, selected) {
730
- const text = async (path) => {
731
- const entry = house.get(relative(path));
732
- return entry === undefined ? undefined : blobText(repository, entry, path);
733
- };
1254
+ async landed(repository, changes, house, selected, model) {
1255
+ const created = new Set(changes.flatMap((change) => (change.op === 'create' || change.op === 'upload' ? [change.path] : [])));
734
1256
  for (const change of changes) {
735
- const current = await text(change.path);
736
- if (change.op === 'remove' && current === undefined)
737
- continue;
738
- if (change.op === 'rename' && current === undefined && house.get(relative(change.to)) === selected.get(relative(change.to))) {
739
- continue;
1257
+ const path = relative(change.path);
1258
+ if (change.op === 'upload') {
1259
+ if (house.get(path) === selected.get(path))
1260
+ continue;
1261
+ return false;
740
1262
  }
1263
+ if (change.op === 'remove') {
1264
+ if (created.has(change.path) || !house.has(path))
1265
+ continue;
1266
+ return false;
1267
+ }
1268
+ if (change.op === 'rename') {
1269
+ if (!house.has(path) && house.get(relative(change.to)) === selected.get(relative(change.to)))
1270
+ continue;
1271
+ return false;
1272
+ }
1273
+ const entry = house.get(path);
1274
+ const current = entry === undefined ? undefined : await blobText(repository, entry);
741
1275
  if ((change.op === 'create' || change.op === 'replace') && current === change.content)
742
1276
  continue;
743
- const parsed = current === undefined ? null : parse(current, change.path);
744
- const document = parsed?.ok === true ? parsed.document : null;
745
- if (document === null)
1277
+ const document = current === undefined ? undefined : readDocument(current, path, model)?.document;
1278
+ if (document === undefined)
746
1279
  return false;
747
1280
  if (change.op === 'set_field' && JSON.stringify(document.fields[change.field]) === JSON.stringify(change.value))
748
1281
  continue;
@@ -755,7 +1288,7 @@ export class LocalCopies {
755
1288
  return true;
756
1289
  }
757
1290
  async revisionText(path, wanted) {
758
- const page = await this.house.post('/kit/door/revisions', {
1291
+ const page = await this.house.door('files/revisions/list', {
759
1292
  path,
760
1293
  revision: wanted,
761
1294
  });