@agentshouse/kit 0.2.1-ccf188e02985d → 0.2.1-ccffb3f637815

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