@agentshouse/kit 0.2.1-cf110d15f69c8 → 0.2.1-cf2c570b41f24

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