@agentshouse/kit 0.2.1-ccf188e02985d → 0.2.1-cd423aab3dbcc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/README.md +35 -42
  2. package/dist/acp.js +66 -12
  3. package/dist/afk.js +17 -0
  4. package/dist/agents.js +122 -53
  5. package/dist/api.js +15 -10
  6. package/dist/binaries.js +60 -0
  7. package/dist/bridge.js +100 -45
  8. package/dist/building.js +50 -0
  9. package/dist/calls.js +88 -0
  10. package/dist/chunks.js +54 -0
  11. package/dist/clis.js +62 -50
  12. package/dist/connect.js +121 -0
  13. package/dist/connection.js +120 -0
  14. package/dist/content.js +106 -0
  15. package/dist/conversations.js +617 -240
  16. package/dist/copies.js +1277 -623
  17. package/dist/earlier.js +273 -0
  18. package/dist/end.js +12 -5
  19. package/dist/events.js +549 -281
  20. package/dist/files.js +206 -38
  21. package/dist/git.js +2 -72
  22. package/dist/grok.js +39 -0
  23. package/dist/home.js +86 -29
  24. package/dist/house-cli.js +180 -189
  25. package/dist/house-main.js +3 -3
  26. package/dist/instructions.js +29 -11
  27. package/dist/keys.js +238 -0
  28. package/dist/kit-cli.js +18 -8
  29. package/dist/kit-main.js +3 -3
  30. package/dist/login.js +36 -22
  31. package/dist/logout.js +2 -2
  32. package/dist/manifest.js +75 -0
  33. package/dist/mcp.js +47 -7
  34. package/dist/owner.js +46 -0
  35. package/dist/placement.js +15 -20
  36. package/dist/refusals.js +50 -23
  37. package/dist/relay.js +21 -0
  38. package/dist/resident.js +141 -90
  39. package/dist/scope.js +15 -4
  40. package/dist/scripts.js +319 -0
  41. package/dist/secret-input.js +1 -1
  42. package/dist/settings.js +247 -0
  43. package/dist/sign-in.js +67 -21
  44. package/dist/silence.js +28 -0
  45. package/dist/skills.js +42 -97
  46. package/dist/supervisor.js +163 -0
  47. package/dist/transcription.js +88 -45
  48. package/dist/update.js +118 -120
  49. package/dist/wire.js +30 -4
  50. package/dist/work.js +27 -2
  51. package/dist/workspace.js +113 -0
  52. package/package.json +25 -22
  53. package/dist/authority-main.js +0 -22
  54. package/dist/bootstrap.json +0 -24
  55. package/dist/chatgpt.js +0 -32
  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/inputs.js +0 -37
  61. package/dist/parts.js +0 -82
  62. package/dist/service-main.js +0 -9
  63. package/dist/service.js +0 -55
  64. package/dist/stream.js +0 -123
  65. package/dist/translate.js +0 -51
  66. package/how-we-work.md +0 -33
  67. package/skills/ask-matt/LICENSE +0 -21
  68. package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
  69. package/skills/ask-matt/SKILL.md +0 -97
  70. package/skills/ask-matt/agents/openai.yaml +0 -5
  71. package/skills/code-review/LICENSE +0 -21
  72. package/skills/code-review/SKILL.md +0 -89
  73. package/skills/code-review/agents/openai.yaml +0 -3
  74. package/skills/codebase-design/DEEPENING.md +0 -37
  75. package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
  76. package/skills/codebase-design/LICENSE +0 -21
  77. package/skills/codebase-design/SKILL.md +0 -114
  78. package/skills/codebase-design/agents/openai.yaml +0 -3
  79. package/skills/diagnosing-bugs/LICENSE +0 -21
  80. package/skills/diagnosing-bugs/SKILL.md +0 -138
  81. package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
  82. package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
  83. package/skills/domain-modeling/ADR-FORMAT.md +0 -47
  84. package/skills/domain-modeling/GLOSSARY-FORMAT.md +0 -60
  85. package/skills/domain-modeling/LICENSE +0 -21
  86. package/skills/domain-modeling/SKILL.md +0 -74
  87. package/skills/domain-modeling/agents/openai.yaml +0 -3
  88. package/skills/grill-me/LICENSE +0 -21
  89. package/skills/grill-me/SKILL.md +0 -7
  90. package/skills/grill-me/agents/openai.yaml +0 -5
  91. package/skills/grill-with-docs/LICENSE +0 -21
  92. package/skills/grill-with-docs/SKILL.md +0 -7
  93. package/skills/grill-with-docs/agents/openai.yaml +0 -5
  94. package/skills/grilling/LICENSE +0 -21
  95. package/skills/grilling/SKILL.md +0 -30
  96. package/skills/grilling/agents/openai.yaml +0 -3
  97. package/skills/handoff/LICENSE +0 -21
  98. package/skills/handoff/SKILL.md +0 -16
  99. package/skills/handoff/agents/openai.yaml +0 -5
  100. package/skills/implement/LICENSE +0 -21
  101. package/skills/implement/SKILL.md +0 -17
  102. package/skills/implement/agents/openai.yaml +0 -5
  103. package/skills/implement-spec/LICENSE +0 -21
  104. package/skills/implement-spec/SKILL.md +0 -40
  105. package/skills/implement-spec/agents/openai.yaml +0 -5
  106. package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
  107. package/skills/improve-codebase-architecture/LICENSE +0 -21
  108. package/skills/improve-codebase-architecture/SKILL.md +0 -71
  109. package/skills/improve-codebase-architecture/agents/openai.yaml +0 -5
  110. package/skills/pr/CREDITS.md +0 -3
  111. package/skills/pr/LICENSE +0 -21
  112. package/skills/pr/SKILL.md +0 -170
  113. package/skills/pr/agents/openai.yaml +0 -3
  114. package/skills/prototype/LICENSE +0 -21
  115. package/skills/prototype/LOGIC.md +0 -67
  116. package/skills/prototype/SKILL.md +0 -26
  117. package/skills/prototype/UI.md +0 -112
  118. package/skills/prototype/agents/openai.yaml +0 -3
  119. package/skills/research/LICENSE +0 -21
  120. package/skills/research/SKILL.md +0 -12
  121. package/skills/research/agents/openai.yaml +0 -3
  122. package/skills/retro/LICENSE +0 -21
  123. package/skills/retro/SKILL.md +0 -44
  124. package/skills/retro/agents/openai.yaml +0 -5
  125. package/skills/tdd/LICENSE +0 -21
  126. package/skills/tdd/SKILL.md +0 -38
  127. package/skills/tdd/agents/openai.yaml +0 -3
  128. package/skills/tdd/mocking.md +0 -59
  129. package/skills/tdd/tests.md +0 -77
  130. package/skills/teach/GLOSSARY-FORMAT.md +0 -35
  131. package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
  132. package/skills/teach/LICENSE +0 -21
  133. package/skills/teach/MISSION-FORMAT.md +0 -31
  134. package/skills/teach/RESOURCES-FORMAT.md +0 -32
  135. package/skills/teach/SKILL.md +0 -140
  136. package/skills/teach/agents/openai.yaml +0 -5
  137. package/skills/to-questionnaire/LICENSE +0 -21
  138. package/skills/to-questionnaire/SKILL.md +0 -54
  139. package/skills/to-questionnaire/agents/openai.yaml +0 -5
  140. package/skills/to-spec/LICENSE +0 -21
  141. package/skills/to-spec/SKILL.md +0 -75
  142. package/skills/to-spec/agents/openai.yaml +0 -5
  143. package/skills/to-tickets/LICENSE +0 -21
  144. package/skills/to-tickets/SKILL.md +0 -105
  145. package/skills/to-tickets/agents/openai.yaml +0 -5
  146. package/skills/triage/AGENT-BRIEF.md +0 -207
  147. package/skills/triage/LICENSE +0 -21
  148. package/skills/triage/OUT-OF-SCOPE.md +0 -105
  149. package/skills/triage/SKILL.md +0 -112
  150. package/skills/triage/agents/openai.yaml +0 -5
  151. package/skills/wait-what/LICENSE +0 -21
  152. package/skills/wait-what/SKILL.md +0 -7
  153. package/skills/wait-what/agents/openai.yaml +0 -5
  154. package/skills/wayfinder/LICENSE +0 -21
  155. package/skills/wayfinder/SKILL.md +0 -128
  156. package/skills/wayfinder/agents/openai.yaml +0 -5
  157. package/skills/wizard/LICENSE +0 -21
  158. package/skills/wizard/SKILL.md +0 -44
  159. package/skills/wizard/agents/openai.yaml +0 -3
  160. package/skills/wizard/template.sh +0 -223
  161. package/skills/writing-for-agents/LICENSE +0 -21
  162. package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
  163. package/skills/writing-for-agents/SKILL.md +0 -81
  164. package/skills/writing-for-agents/agents/openai.yaml +0 -3
package/dist/copies.js CHANGED
@@ -1,17 +1,39 @@
1
- import { parse } from '@agentshouse/mdmodel';
2
- import { randomUUID } from 'node:crypto';
1
+ import { isUtf8 } from 'node:buffer';
2
+ import { createHash, randomUUID } from 'node:crypto';
3
3
  import { mkdir, readFile, readdir, realpath, rename, rm, rmdir, stat, writeFile } from 'node:fs/promises';
4
- import { dirname, join } from 'node:path';
4
+ import { basename, dirname, join } from 'node:path';
5
5
  import { finalRefusal, HouseRefusal, retrying } from './api.js';
6
- import { blobId, blobText, git, isText, HOUSE_REF, importCommits, localCopies, revision, run, succeeded, tree, } from './git.js';
7
- import { kitHome } from './home.js';
6
+ import { Changed, eachAtOnce, isDocument, mediaType, receivedBinary, sentBinary } from './binaries.js';
7
+ import { chunkEntries, stagedChunks } from './chunks.js';
8
+ import { fileInfo, paused } from './files.js';
9
+ import { kitHome, roomsFolder } from './home.js';
10
+ import { digest, hashed, holding, marked, placed, readManifest, removed, scanned, writeManifest, } from './manifest.js';
11
+ import { ownAgentCall, rpc, toolAnswer, Unsettled } from './mcp.js';
8
12
  import { operationId } from './operation.js';
9
- import { refusalOf } from './refusals.js';
10
- import { changed } from './translate.js';
11
- import { relayed } from './relay.js';
12
- const BRANCH = 'main';
13
+ import { firstLine, refusalOf, retryable, stopsPush } from './refusals.js';
14
+ import { keptName, moved as carried, present as found, removeFolder, rerooted, within } from './workspace.js';
15
+ const ROOMS = 'rooms/';
16
+ const NOT_READY = 'not ready yet; Kit copies it once House has built it';
13
17
  const PROVENANCE = '_provenance';
14
- const IDENTITY = { GIT_COMMITTER_NAME: '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
  }
@@ -21,31 +43,34 @@ function houseCause(error) {
21
43
  function logged(error) {
22
44
  process.stderr.write(`kit: ${causeOf(error)}\n`);
23
45
  }
24
- const REBASED = new Set([
25
- 'field_base_stale',
26
- 'section_base_stale',
27
- 'preamble_base_stale',
28
- 'base_revision_stale',
29
- 'path_exists',
30
- 'path_absent',
31
- ]);
32
- function rebaseFixes(refusal) {
33
- return refusal.code === 'edit_conflict' && refusal.conflicts.some((code) => REBASED.has(code));
34
- }
35
- function textOnly(paths) {
46
+ function markdownOnly(paths, maxBytes) {
36
47
  const [named, them] = paths.length === 1 ? [`${paths[0]} is`, 'it'] : [`${paths.join(', ')} are`, 'them'];
37
- return `${named} not text, and house push sends text only; remove ${them} from the commit and place ${them} with house upload`;
48
+ return `${named} Markdown, which holds text only: UTF-8 without NUL, at most ${maxBytes} bytes; save ${them} as text and run house upload again`;
38
49
  }
39
- function outside(path) {
40
- return { refused: true, text: `${path} is not a House Local copy; run house push inside one` };
50
+ async function stopping(sent) {
51
+ try {
52
+ return await sent;
53
+ }
54
+ catch (error) {
55
+ const refusal = error instanceof HouseRefusal ? refusalOf(error.text) : null;
56
+ if (refusal === null || !stopsPush(refusal))
57
+ throw error;
58
+ return { refused: refusal };
59
+ }
60
+ }
61
+ function unstagedUploads(refusal) {
62
+ const named = refusal.detail.upload;
63
+ return Array.isArray(named) ? named : [];
41
64
  }
42
- function short(commit) {
43
- return commit.slice(0, 12);
65
+ function currentHandle(refusal) {
66
+ const path = typeof refusal.detail.current_path === 'string' ? refusal.detail.current_path : /use (\/rooms\/\S+)/.exec(refusal.text)?.[1];
67
+ return path === undefined ? null : (/^\/rooms\/([^/]+)/.exec(path)?.[1] ?? null);
68
+ }
69
+ function outside(path) {
70
+ return { refused: true, text: `${path} is not in a House Local copy; run house upload inside one` };
44
71
  }
45
- function moved(before, after) {
46
- if (before.house !== after.house)
47
- return [`${rootOf(after.room_handle)}: ${HOUSE_REF} is at ${short(after.house)}`];
48
- return before.position === after.position ? [] : [`${rootOf(after.room_handle)}: read-only files refreshed`];
72
+ function counted(count, one, many = `${one}s`) {
73
+ return `${count} ${count === 1 ? one : many}`;
49
74
  }
50
75
  function expiredAuthority(error) {
51
76
  if (!(error instanceof HouseRefusal))
@@ -58,6 +83,9 @@ function expiredAuthority(error) {
58
83
  return undefined;
59
84
  }
60
85
  }
86
+ function grouped(items, size) {
87
+ return items.flatMap((_, index) => (index % size === 0 ? [items.slice(index, index + size)] : []));
88
+ }
61
89
  function covers(path, prefix) {
62
90
  return prefix === '' || path === prefix || path.startsWith(`${prefix}/`);
63
91
  }
@@ -69,136 +97,333 @@ function sidecar(path) {
69
97
  function recordOf(key) {
70
98
  return join(kitHome(), 'local-copies', `${key}.json`);
71
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
+ }
72
114
  function rootOf(handle) {
73
115
  return `/rooms/${handle}`;
74
116
  }
75
117
  function relative(path) {
76
118
  return path.replace(/^\/rooms\/[^/]+\//, '');
77
119
  }
120
+ function kitsOwn(path) {
121
+ return !path.includes('/') && KIT_NAMES.includes(path.toLowerCase());
122
+ }
78
123
  function declaredWritable(path, copy) {
79
124
  const deepest = (scopes) => Math.max(-1, ...scopes.filter((scope) => covers(path, scope)).map((scope) => scope.length));
80
125
  return deepest(copy.write) > deepest(copy.protected);
81
126
  }
82
- function pattern(path) {
83
- 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);
84
140
  }
85
- async function exists(path) {
86
- return stat(path).then(() => true, () => false);
141
+ function whole(theirs, mine) {
142
+ return `<<<<<<< current\n${ended(theirs)}=======\n${ended(mine)}>>>>>>> yours\n`;
87
143
  }
88
- async function writeText(path, content) {
89
- await mkdir(dirname(path), { recursive: true });
90
- await writeFile(path, content);
144
+ function ended(text) {
145
+ return text === '' || text.endsWith('\n') ? text : `${text}\n`;
91
146
  }
92
- async function removeFile(repository, path) {
93
- await rm(join(repository, path), { force: true });
94
- for (let folder = dirname(path); folder !== '.'; folder = dirname(folder)) {
95
- if (!(await rmdir(join(repository, folder)).then(() => true, () => false)))
96
- return;
97
- }
147
+ function sameBytes(file) {
148
+ return `${file.sha256}:${file.bytes}`;
98
149
  }
99
- async function readOnlyPlaced(repository, held, written, removed) {
100
- const tracked = new Set((await git(repository, ['ls-files', '-z'])).split('\0'));
101
- const holding = new Set(held);
102
- for (const path of removed) {
103
- if (!holding.has(path))
104
- continue;
105
- if (!tracked.has(path))
106
- await removeFile(repository, path);
107
- holding.delete(path);
108
- }
109
- for (const [path, content] of written) {
110
- if (tracked.has(path))
111
- continue;
112
- if (!holding.has(path) && (await exists(join(repository, path))))
113
- continue;
114
- await writeText(join(repository, path), content);
115
- holding.add(path);
116
- }
117
- return [...holding].sort();
118
- }
119
- async function ruled(path, rules) {
120
- if ((await readFile(path, 'utf8').catch(() => null)) !== rules)
121
- await writeFile(path, rules);
122
- }
123
- async function ignoreRules(repository, writable, copy) {
124
- const readOnly = ('read_only' in copy ? copy.read_only : [...copy.readOnly.keys()]).filter((path) => !path.split('/').includes(PROVENANCE));
125
- const protectedPath = (path) => copy.protected.some((prefix) => prefix !== '' && covers(path, prefix));
126
- const whole = copy.protected.filter((prefix) => prefix !== '' && !writable.some((path) => covers(path, prefix)));
127
- const house = [
128
- ...whole,
129
- ...readOnly.filter((path) => protectedPath(path) && !whole.some((prefix) => covers(path, prefix))),
130
- ].map(pattern);
131
- const granted = readOnly.filter((path) => !protectedPath(path)).map(pattern);
132
- await ruled(join(repository, '.gitignore'), [
133
- '# House Kit writes this file and .ignore. The paths below are read-only House',
134
- '# content: Git does not track them, and House refuses a change to them even',
135
- '# when this file is edited or a path is force-added.',
136
- '/.gitignore',
137
- '/.ignore',
138
- PROVENANCE,
139
- '',
140
- '# House maintains or protects these paths.',
141
- ...house,
142
- '',
143
- '# Your current House grant does not allow writing these paths.',
144
- ...granted,
145
- '',
146
- ].join('\n'));
147
- await ruled(join(repository, '.ignore'), [
148
- '# House Kit writes this file so native search still reads the read-only House',
149
- '# content that .gitignore keeps out of Git.',
150
- `!${PROVENANCE}`,
151
- ...[...house, ...granted].map((rule) => `!${rule}`),
152
- '',
153
- ].join('\n'));
154
- }
155
- function revisionAt(copy, commit, path) {
156
- let manifest = copy.manifests[commit];
157
- while (manifest !== undefined) {
158
- if (path in manifest.files)
159
- return manifest.files[path] ?? undefined;
160
- manifest = manifest.parent === null ? undefined : copy.manifests[manifest.parent];
161
- }
162
- return undefined;
150
+ async function exists(path) {
151
+ return stat(path).then(() => true, () => false);
163
152
  }
164
153
  function without(copy) {
165
- const { push, ...settled } = copy;
154
+ const { pending, ...settled } = copy;
166
155
  return settled;
167
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
+ }
168
174
  export function changedPaths(changes) {
169
- return [
170
- ...new Set(changes.flatMap((change) => (change.op === 'rename' ? [change.path, change.to] : [change.path]))),
171
- ].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;
213
+ }
214
+ function recordedRevisions(copy, path) {
215
+ return [...new Set(Object.values(copy.manifests).flatMap((manifest) => (typeof manifest.files[path] === 'string' ? [manifest.files[path]] : [])))];
172
216
  }
173
- function movedPaths(moves, before, after) {
174
- const pairs = moves.filter(({ from, to }) => before.has(from) && !after.has(from) && after.has(to) && !before.has(to));
175
- return new Map(pairs
176
- .filter((pair) => pairs.filter((other) => other.from === pair.from || other.to === pair.to).length === 1)
177
- .map(({ from, to }) => [from, to]));
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
+ }
178
381
  }
179
382
  export class LocalCopies {
180
383
  owner;
181
384
  house;
385
+ launched;
182
386
  copies = new Map();
183
387
  queues = new Map();
388
+ waiting = new Set();
184
389
  answering = Promise.resolve();
185
- constructor(house) {
390
+ constructor(house, launched) {
186
391
  this.house = house;
392
+ this.launched = launched;
187
393
  this.owner = {
188
394
  key: 'owner',
189
395
  submit: async (operation, changes) => {
190
396
  try {
191
- const outcome = await retrying(() => house.post('/kit/door/batch', { operation_id: operation, changes }));
192
- 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 };
193
399
  }
194
400
  catch (error) {
195
401
  if (!finalRefusal(error))
196
402
  throw error;
197
403
  return {
198
- 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: {} },
199
405
  };
200
406
  }
201
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
+ },
202
427
  };
203
428
  }
204
429
  async load() {
@@ -212,25 +437,17 @@ export class LocalCopies {
212
437
  this.copies.set(copy.key, copy);
213
438
  }
214
439
  }
215
- async mapping() {
216
- await this.answering;
217
- return [...this.copies.values()]
218
- .filter((copy) => copy.erasing === undefined)
219
- .map((copy) => `${rootOf(copy.room_handle)}: ${copy.repository}`)
220
- .sort();
440
+ async swept() {
441
+ await sweptIn(roomsFolder());
221
442
  }
222
443
  connected() {
223
444
  return this.selected();
224
445
  }
225
- async changed(rooms) {
226
- if (rooms.length > 0)
227
- await this.selected(rooms);
228
- }
229
- fetch() {
230
- return this.answered(() => this.fetched(true));
446
+ download(caller, fetching) {
447
+ return this.answered(() => this.fetched(true, caller, fetching));
231
448
  }
232
- async selected(rooms) {
233
- const copied = await this.answered(() => this.fetched(false, rooms));
449
+ async selected() {
450
+ const copied = await this.answered(() => this.fetched(false, this.owner, HELD));
234
451
  if (copied.refused)
235
452
  logged(copied.text);
236
453
  }
@@ -239,101 +456,131 @@ export class LocalCopies {
239
456
  this.answering = answered.catch(() => undefined);
240
457
  return answered;
241
458
  }
242
- async fetched(refreshing, rooms) {
243
- const { local_copy: answer } = await this.house.post('/kit/local-copy/selection', rooms === undefined ? {} : { rooms });
244
- const synced = new Map(answer.rooms.map((room) => [`rooms/${room.room_ref}`, room.room_handle]));
245
- const named = rooms === undefined ? null : new Set(rooms.map((room) => `rooms/${room}`));
246
- const held = () => [...this.copies.values()].filter((copy) => named === null || named.has(copy.key));
247
- return this.holding([...new Set([...held().map((copy) => copy.key), ...synced.keys()])].sort(), async () => {
459
+ async fetched(refreshing, caller, fetching) {
460
+ const { local_copy: answer } = await this.house.act('local-copies/selection/read', {});
461
+ const synced = new Map(answer.rooms.map((room) => [`${ROOMS}${room.room_ref}`, room.room_handle]));
462
+ return this.holding([...new Set([...this.copies.keys(), ...synced.keys()])].sort(), async () => {
463
+ const chosen = await this.launched();
248
464
  const said = [];
249
465
  let refused = false;
250
- for (const copy of held()) {
251
- const returned = synced.has(copy.key);
252
- if (returned && copy.erasing === undefined)
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)
253
469
  continue;
254
- try {
255
- await this.erase(copy);
256
- said.push(`${rootOf(copy.room_handle)}: ${returned ? '' : 'no longer synced to this Environment; '}erased from the Local copy`);
257
- }
258
- catch (error) {
259
- refused = true;
260
- said.push(`${rootOf(copy.room_handle)}: ${causeOf(error)}`);
261
- }
470
+ await this.save({ ...copy, unfollowed: mark });
471
+ if (refreshing)
472
+ said.push(`${rootOf(copy.room_handle)}: no longer followed; kept as it is`);
262
473
  }
263
474
  const current = [];
475
+ const wanted = new Map();
264
476
  for (const [key, handle] of synced) {
265
477
  try {
266
478
  const held = this.copies.get(key);
267
- if (held?.erasing)
268
- continue;
269
479
  if (held === undefined || !(await exists(held.repository))) {
270
- said.push(await this.materialize(key, handle));
480
+ wanted.set(key, handle);
271
481
  continue;
272
482
  }
273
- current.push(held.room_handle === handle ? held : await this.save({ ...held, room_handle: handle }));
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));
274
486
  }
275
487
  catch (error) {
276
488
  refused = true;
277
489
  said.push(`${rootOf(handle)}: ${causeOf(error)}`);
278
490
  }
279
491
  }
280
- if (refreshing && current.length > 0) {
281
- try {
282
- const refreshed = await this.refreshed(current);
283
- current.forEach((copy, index) => said.push(...moved(copy, refreshed[index])));
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
+ }
284
514
  }
285
- 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) {
286
534
  refused = true;
287
- said.push(houseCause(error));
535
+ unsettled.push(...reading.map((copy) => copy.room_ref));
536
+ said.push(failed);
288
537
  }
289
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);
290
548
  return { refused, text: said.length === 0 ? 'Nothing new from House.' : said.join('\n') };
291
549
  });
292
550
  }
293
- async observed(report) {
294
- const key = await this.keyAt(dirname(report.common));
295
- if (key === undefined)
296
- return;
297
- await this.exclusive(key, async () => {
298
- const copy = this.copies.get(key);
299
- if (copy === undefined || copy.erasing)
300
- return;
301
- let moves = copy.moves;
302
- for (const { from, to } of report.moves) {
303
- const held = moves.find((move) => move.worktree === report.worktree && move.to === from);
304
- moves = moves.filter((move) => move !== held);
305
- if (held === undefined)
306
- moves.push({ worktree: report.worktree, from, to });
307
- else if (held.from !== to)
308
- moves.push({ ...held, to });
309
- }
310
- if (moves.some((move) => move.worktree === report.worktree)) {
311
- const index = new Set((await git(report.worktree, ['ls-files', '-z'])).split('\0'));
312
- const head = await tree(report.worktree, 'HEAD').catch(() => new Map());
313
- const live = ({ from, to }) => (index.has(to) && !index.has(from)) || (head.has(to) && !head.has(from));
314
- moves = moves.filter((move) => move.worktree !== report.worktree || live(move));
315
- }
316
- if (JSON.stringify(moves) !== JSON.stringify(copy.moves))
317
- await this.save({ ...copy, moves });
318
- });
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
+ ];
319
561
  }
320
- async push(cwd, selector, caller) {
321
- const located = await run(cwd, ['rev-parse', '--path-format=absolute', '--git-common-dir', '--show-toplevel']);
322
- if (located.status !== 0)
323
- return outside(cwd);
324
- const [common, worktree] = located.stdout.toString('utf8').split('\n');
325
- const key = await this.keyAt(dirname(common));
326
- if (key === undefined)
327
- return outside(worktree);
328
- return this.exclusive(key, () => this.pushed(key, worktree, selector, caller));
329
- }
330
- async keyAt(repository) {
331
- const real = await realpath(repository).catch(() => repository);
332
- for (const copy of this.copies.values()) {
333
- if ((await realpath(copy.repository).catch(() => copy.repository)) === real)
334
- 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;
335
583
  }
336
- return undefined;
337
584
  }
338
585
  exclusive(key, work) {
339
586
  const queued = (this.queues.get(key) ?? Promise.resolve()).then(work);
@@ -353,133 +600,298 @@ export class LocalCopies {
353
600
  this.copies.set(copy.key, copy);
354
601
  return copy;
355
602
  }
356
- async erase(copy) {
357
- await this.save({ ...copy, erasing: true });
358
- await rm(copy.repository, { recursive: true, force: true });
359
- await rm(recordOf(copy.key), { force: true });
360
- this.copies.delete(copy.key);
361
- }
362
- async read(handle) {
363
- const prefixes = [rootOf(handle)];
364
- const discovery = await this.house.post('/kit/door/discover', { prefixes });
365
- let bundle = await this.house.post('/kit/door/bootstrap', { prefixes });
366
- if ('transfer' in bundle)
367
- bundle = (await (await fetch(relayed(bundle.transfer.url))).json());
368
- const [view] = bundle.scopes;
369
- if (view === undefined)
370
- return null;
371
- const authority = discovery.authorities.find((entry) => entry.room_ref === view.manifest.anchor);
372
- const contents = new Map(view.contents.map((entry) => [entry.path, entry.content]));
373
- const read = {
374
- anchor: view.manifest.anchor,
375
- position: view.manifest.position,
376
- log_epoch: view.manifest.log_epoch,
377
- write: authority.operations.write,
378
- protected: authority.protected_paths,
379
- writable: new Map(),
380
- revisions: {},
381
- readOnly: new Map(),
382
- };
383
- for (const file of view.manifest.files) {
384
- const content = contents.get(file.path);
385
- if (file.writable) {
386
- read.writable.set(file.path, content);
387
- read.revisions[file.path] = file.revision;
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);
388
658
  }
389
659
  else {
390
- read.readOnly.set(file.path, content);
660
+ target = await keptName(join(rooms, basename(source)));
391
661
  }
392
- if (file.provenance !== undefined)
393
- 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));
394
665
  }
395
- return read;
666
+ await sweptIn(earlier);
667
+ await removeFolder(earlier);
668
+ await removeFolder(dirname(earlier));
669
+ return { moved: moving.length > 0, kept: staying };
396
670
  }
397
- async materialize(key, handle) {
398
- const read = await this.read(handle);
399
- if (read === null)
400
- throw new Error('House serves nothing of it to copy');
401
- const repository = join(localCopies(), 'rooms', read.anchor);
402
- if (await exists(repository))
403
- throw new Error(`${repository} already exists and House Kit leaves it as it is`);
404
- const staging = join(dirname(repository), `.${randomUUID()}`);
405
- await mkdir(staging, { recursive: true });
406
- let house;
407
- 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;
408
690
  try {
409
- await git(staging, ['init', '-q', '-b', BRANCH]);
410
- house = await importCommits(staging, [
411
- { message: `House ${rootOf(handle)}`, from: null, files: read.writable },
412
- ]);
413
- await git(staging, ['update-ref', `refs/heads/${BRANCH}`, house]);
414
- await git(staging, ['reset', '-q', '--hard']);
415
- readOnly = await readOnlyPlaced(staging, [], read.readOnly, []);
416
- await ignoreRules(staging, [...read.writable.keys()], read);
417
- 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 };
418
720
  }
419
721
  catch (error) {
420
- await rm(staging, { recursive: true, force: true });
722
+ await rm(folder, { recursive: true, force: true });
421
723
  throw error;
422
724
  }
423
- await this.save({
424
- key,
425
- room_ref: read.anchor,
426
- room_handle: handle,
427
- repository,
428
- house,
429
- position: read.position,
430
- log_epoch: read.log_epoch,
431
- write: read.write,
432
- protected: read.protected,
433
- read_only: readOnly,
434
- manifests: { [house]: { parent: null, files: read.revisions } },
435
- moves: [],
436
- });
437
- return `${rootOf(handle)}: copied to ${repository}`;
725
+ finally {
726
+ await rm(chunks, { recursive: true, force: true });
727
+ }
728
+ return this.save(copy);
438
729
  }
439
- async received(copy) {
440
- const read = await this.read(copy.room_handle);
441
- if (read === null)
442
- return copy;
443
- const house = await importCommits(copy.repository, [
444
- { message: `House ${rootOf(copy.room_handle)} at position ${read.position}`, from: copy.house, replaces: true, files: read.writable },
445
- ]);
446
- const removed = copy.read_only.filter((path) => !read.readOnly.has(path));
447
- const readOnly = await readOnlyPlaced(copy.repository, copy.read_only, read.readOnly, removed);
448
- const next = {
449
- ...copy,
450
- house,
451
- position: read.position,
452
- log_epoch: read.log_epoch,
453
- write: read.write,
454
- protected: read.protected,
455
- read_only: readOnly,
456
- manifests: { ...copy.manifests, [house]: { parent: null, files: read.revisions } },
457
- };
458
- await ignoreRules(copy.repository, [...read.writable.keys()], next);
459
- return next;
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}`;
460
762
  }
461
- async refreshed(copies) {
462
- const discovery = await this.house.post('/kit/door/discover', {
463
- prefixes: copies.map((copy) => rootOf(copy.room_handle)),
464
- });
465
- const regranted = new Map();
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 = [];
466
833
  for (const copy of copies) {
467
- const authority = discovery.authorities.find((entry) => entry.room_ref === copy.room_ref);
834
+ const authority = authorities.find((entry) => entry.room_ref === copy.room_ref);
468
835
  if (authority !== undefined &&
469
836
  JSON.stringify([authority.operations.write, authority.protected_paths]) !== JSON.stringify([copy.write, copy.protected])) {
470
- regranted.set(copy.key, await this.save(await this.received(copy)));
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
+ }
471
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;
472
883
  }
473
- const caught = await this.caughtUp(copies.filter((copy) => !regranted.has(copy.key)), false);
474
- return copies.map((copy) => regranted.get(copy.key) ?? caught.find((held) => held.key === copy.key));
884
+ await writeManifest(copy.repository, work, manifest);
475
885
  }
476
- async caughtUp(copies, whole) {
886
+ async caughtUp(copies, whole, caller, fetching, reports, unready) {
477
887
  const caught = new Map(copies.map((copy) => [copy.key, copy]));
888
+ const read = new Set();
889
+ const waiting = unready ?? [];
478
890
  let asking = [...copies];
479
891
  while (asking.length > 0) {
480
892
  let answer;
481
893
  try {
482
- answer = await this.house.post('/kit/door/enumerate', {
894
+ answer = await this.house.door('changes/enumerate', {
483
895
  prefixes: asking.map((copy) => rootOf(copy.room_handle)),
484
896
  positions: asking.map((copy) => ({ authority: copy.room_ref, position: copy.position, log_epoch: copy.log_epoch })),
485
897
  });
@@ -488,8 +900,22 @@ export class LocalCopies {
488
900
  const expired = asking.find((copy) => copy.room_ref === expiredAuthority(error));
489
901
  if (expired === undefined)
490
902
  throw error;
491
- caught.set(expired.key, await this.save(await this.received(expired)));
492
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
+ }
493
919
  continue;
494
920
  }
495
921
  const advanced = [];
@@ -497,371 +923,599 @@ export class LocalCopies {
497
923
  const view = answer.authorities.find((entry) => entry.authority === copy.room_ref);
498
924
  if (view === undefined || (view.position === copy.position && view.log_epoch === copy.log_epoch))
499
925
  continue;
500
- 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)));
501
927
  caught.set(copy.key, next);
502
928
  advanced.push(next);
503
929
  }
504
- asking = whole ? advanced : [];
930
+ asking = advanced.filter((copy) => whole || read.has(copy.key));
505
931
  }
932
+ if (unready === undefined && waiting.length > 0)
933
+ throw new NotReady(waiting);
506
934
  return copies.map((copy) => caught.get(copy.key));
507
935
  }
508
- async incorporated(copy, changes, position, logEpoch) {
509
- const houseTree = await tree(copy.repository, copy.house);
510
- const held = new Set(copy.read_only);
511
- const files = new Map();
512
- const revisions = {};
513
- const written = new Map();
514
- const removed = [];
515
- for (const change of changes) {
516
- const gone = 'removed' in change;
517
- if (houseTree.has(change.path) || (!gone && !held.has(change.path) && declaredWritable(change.path, copy))) {
518
- const unchanged = gone
519
- ? !houseTree.has(change.path)
520
- : houseTree.get(change.path) === blobId(change.content) &&
521
- revisionAt(copy, copy.house, change.path) === change.revision;
522
- 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);
523
990
  continue;
524
- files.set(change.path, gone ? null : change.content);
525
- 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
+ }
526
1002
  }
527
- else if (gone) {
528
- 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
+ }
529
1044
  }
530
- else {
531
- written.set(change.path, change.content);
532
- if (change.provenance !== undefined)
533
- written.set(sidecar(change.path), change.provenance);
534
- }
535
- }
536
- let { house, manifests } = copy;
537
- if (files.size > 0) {
538
- house = await importCommits(copy.repository, [
539
- { message: `House ${rootOf(copy.room_handle)} at position ${position}`, from: copy.house, files },
540
- ]);
541
- manifests = { ...manifests, [house]: { parent: copy.house, files: revisions } };
542
- }
543
- const next = {
544
- ...copy,
545
- house,
546
- manifests,
547
- position,
548
- log_epoch: logEpoch,
549
- read_only: await readOnlyPlaced(copy.repository, copy.read_only, written, removed),
550
- };
551
- await ignoreRules(copy.repository, [...(await tree(copy.repository, house)).keys()], next);
552
- return next;
553
- }
554
- async pushed(key, worktree, selector, caller) {
555
- let copy = this.copies.get(key);
556
- if (copy === undefined || copy.erasing)
557
- return outside(worktree);
558
- if (copy.push?.state === 'submitted')
559
- return this.settle(copy, caller);
560
- const selected = await run(worktree, ['rev-parse', '--verify', '--quiet', '--end-of-options', `${selector}^{commit}`]);
561
- if (selected.status !== 0)
562
- return { refused: true, text: `${selector} names no commit` };
563
- const commit = selected.stdout.toString('utf8').trim();
564
- if (copy.push !== undefined) {
565
- const held = copy.push.acknowledged;
566
- if (held === undefined || !(await succeeded(worktree, ['merge-base', '--is-ancestor', held, commit]))) {
567
- return this.finished(copy);
568
- }
569
- copy = await this.save(without(copy));
570
- }
571
- const listed = (await git(copy.repository, ['rev-list', '--topo-order', commit])).split('\n');
572
- const base = listed.find((ancestor) => ancestor in copy.manifests);
573
- if (base === undefined) {
574
- return { refused: true, text: `${short(commit)} holds no House state; merge or rebase onto ${HOUSE_REF}` };
575
- }
576
- 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;
577
1081
  try {
578
- changes = await this.derived(copy, base, commit);
1082
+ copy = await this.converted(this.copies.get(key));
1083
+ }
1084
+ catch (error) {
1085
+ return { refused: true, text: error instanceof NotReady ? `${rootOf(this.copies.get(key).room_handle)}: ${NOT_READY}` : causeOf(error) };
1086
+ }
1087
+ const lines = [];
1088
+ let files = 0;
1089
+ let landed = 0;
1090
+ if (copy.pending !== undefined) {
1091
+ const settled = await this.settle(copy, caller);
1092
+ if (settled.refused)
1093
+ return settled;
1094
+ files += settled.files;
1095
+ landed++;
1096
+ lines.push(...settled.lines);
1097
+ copy = this.copies.get(key);
1098
+ }
1099
+ const root = rootOf(copy.room_handle);
1100
+ const manifest = await readManifest(copy.repository);
1101
+ const present = await scanned(copy.repository);
1102
+ const unresolved = [];
1103
+ for (const [path, entry] of Object.entries(manifest.files)) {
1104
+ if (entry.conflicted && (await marked(copy.repository, present.get(path)?.name ?? path)))
1105
+ unresolved.push(`${root}/${path}`);
1106
+ }
1107
+ if (unresolved.length > 0) {
1108
+ const [named, they] = unresolved.length === 1 ? [`${unresolved[0]} still holds`, 'it'] : [`${unresolved.join(', ')} still hold`, 'each'];
1109
+ return { refused: true, text: `${named} conflict markers; resolve ${they}, then run house upload again` };
1110
+ }
1111
+ let built;
1112
+ try {
1113
+ built = await this.built(copy, manifest, present);
579
1114
  }
580
1115
  catch (error) {
581
1116
  return { refused: true, text: houseCause(error) };
582
1117
  }
583
- if (changes.length === 0)
584
- return { refused: false, text: `Nothing to submit: ${short(commit)} matches House.` };
585
- const branch = await git(worktree, ['symbolic-ref', '-q', 'HEAD']).then((name) => name.trim(), () => null);
586
- const submitted = await this.save({
587
- ...copy,
588
- push: {
589
- state: 'submitted',
590
- caller: caller.key,
591
- operation_id: operationId(),
592
- commit,
593
- base,
594
- changes,
595
- worktree,
596
- branch,
597
- house: copy.house,
598
- },
599
- });
600
- return this.settle(submitted, caller);
1118
+ if ('refused' in built)
1119
+ return built;
1120
+ const batches = batched(built.changes);
1121
+ for (const [index, changes] of batches.entries()) {
1122
+ const read = Object.fromEntries(changedPaths(changes).flatMap((path) => (built.read[relative(path)] === undefined ? [] : [[relative(path), built.read[relative(path)]]])));
1123
+ copy = await this.save({ ...this.copies.get(key), pending: { operation_id: operationId(), caller: caller.key, changes, read } });
1124
+ const settled = await this.settle(copy, caller);
1125
+ if (settled.refused) {
1126
+ const done = landed === 0 ? [] : [`House accepted ${counted(landed, 'batch', 'batches')} of ${rootOf(copy.room_handle)} before this one.`];
1127
+ return { refused: true, text: [...done, ...lines, settled.text, ...(index + 1 < batches.length ? [`${counted(batches.length - index - 1, 'batch', 'batches')} after it ${batches.length - index - 1 === 1 ? 'was' : 'were'} not sent`] : [])].join('\n') };
1128
+ }
1129
+ files += settled.files;
1130
+ landed++;
1131
+ lines.push(...settled.lines);
1132
+ }
1133
+ const now = rootOf(this.copies.get(key).room_handle);
1134
+ if (landed === 0)
1135
+ return { refused: false, text: `Nothing to upload: ${root} matches House.` };
1136
+ return {
1137
+ refused: false,
1138
+ text: [`House accepted ${counted(files, 'file')} of ${now}${landed > 1 ? ` in ${landed} batches` : ''}.`, ...lines].join('\n'),
1139
+ };
601
1140
  }
602
- async derived(copy, base, commit) {
1141
+ async built(copy, manifest, present) {
603
1142
  const root = rootOf(copy.room_handle);
604
- const before = await tree(copy.repository, base);
605
- const after = await tree(copy.repository, commit);
606
- const moved = movedPaths(copy.moves, before, after);
607
- const arrived = new Set(moved.values());
608
- const arriving = [...after].filter(([path, entry]) => before.get(path) !== entry);
609
- if (arriving.length > 0) {
610
- const { bounds } = await this.house.post('/kit/door/discover', { prefixes: [root] });
611
- const binary = [];
612
- for (const [path, entry] of arriving) {
613
- if (!(await isText(copy.repository, entry, bounds.max_authored_file_bytes)))
614
- binary.push(path);
615
- }
616
- if (binary.length > 0)
617
- throw new Error(textOnly(binary));
618
- }
619
- const text = (entry) => blobText(copy.repository, entry);
1143
+ const added = [];
1144
+ const changed = [];
1145
+ const gone = [];
1146
+ let adopted = false;
1147
+ for (const [path, file] of present) {
1148
+ const entry = manifest.files[path];
1149
+ if (kitsOwn(path))
1150
+ continue;
1151
+ if (entry === undefined)
1152
+ added.push(path);
1153
+ else if (entry.read_only)
1154
+ continue;
1155
+ else if (entry.sha256 !== file.sha256)
1156
+ changed.push(path);
1157
+ else if (entry.skipped) {
1158
+ manifest.files[path] = { revision: entry.revision, sha256: entry.sha256, bytes: entry.bytes };
1159
+ adopted = true;
1160
+ }
1161
+ }
1162
+ for (const [path, entry] of Object.entries(manifest.files)) {
1163
+ if (!kitsOwn(path) && !entry.read_only && !entry.skipped && !present.has(path))
1164
+ gone.push(path);
1165
+ }
1166
+ if (adopted)
1167
+ await writeManifest(copy.repository, await staging(), manifest);
1168
+ const listed = (paths, file) => {
1169
+ const found = new Map();
1170
+ for (const path of paths)
1171
+ found.set(sameBytes(file(path)), [...(found.get(sameBytes(file(path))) ?? []), path]);
1172
+ return found;
1173
+ };
1174
+ const leaving = listed(gone, (path) => manifest.files[path]);
1175
+ const arriving = listed(added, (path) => present.get(path));
1176
+ const moves = new Map();
1177
+ for (const [held, [from, ...others]] of leaving) {
1178
+ const [to, ...more] = arriving.get(held) ?? [];
1179
+ if (others.length === 0 && more.length === 0 && to !== undefined && isDocument(from) === isDocument(to))
1180
+ moves.set(from, to);
1181
+ }
1182
+ const arrived = new Set(moves.values());
1183
+ const documents = [...added.filter((path) => !arrived.has(path)), ...changed].filter(isDocument).sort();
1184
+ const texts = new Map();
1185
+ if (documents.length > 0) {
1186
+ const { bounds } = await this.house.door('rooms/discover', { prefixes: [root] });
1187
+ const refused = [];
1188
+ for (const path of documents) {
1189
+ const bytes = await readFile(join(copy.repository, present.get(path).name));
1190
+ if (bytes.length > bounds.max_authored_file_bytes || !isUtf8(bytes) || bytes.includes(0))
1191
+ refused.push(path);
1192
+ else
1193
+ texts.set(path, bytes.toString('utf8'));
1194
+ }
1195
+ if (refused.length > 0)
1196
+ return { refused: true, text: markdownOnly(refused, bounds.max_authored_file_bytes) };
1197
+ }
620
1198
  const changes = [];
621
- 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()) {
622
1201
  if (arrived.has(path))
623
1202
  continue;
624
- const prior = before.get(path);
625
- const next = after.get(path);
626
- const known = revisionAt(copy, base, path);
627
- const to = moved.get(path);
1203
+ const named = `${root}/${path}`;
1204
+ const entry = manifest.files[path];
1205
+ const to = moves.get(path);
628
1206
  if (to !== undefined) {
629
- changes.push({ op: 'rename', path: `${root}/${path}`, to: `${root}/${to}`, base: known });
630
- const moving = after.get(to);
631
- if (moving !== prior) {
632
- changes.push({ op: 'replace', path: `${root}/${to}`, base: known, content: await text(moving) });
633
- }
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 };
634
1209
  continue;
635
1210
  }
636
- if (prior === next)
1211
+ const file = present.get(path);
1212
+ if (file === undefined) {
1213
+ changes.push({ op: 'delete', path: named, base: entry.revision });
637
1214
  continue;
638
- if (next === undefined)
639
- changes.push({ op: 'remove', path: `${root}/${path}`, base: known });
640
- else if (prior === undefined)
641
- changes.push({ op: 'create', path: `${root}/${path}`, content: await text(next) });
642
- else
643
- changes.push(...changed(`${root}/${path}`, path, known, await text(prior), await text(next)));
1215
+ }
1216
+ const base = entry === undefined ? {} : { base: entry.revision };
1217
+ const text = texts.get(path);
1218
+ if (text !== undefined) {
1219
+ changes.push({ op: 'write', path: named, content: text, ...base });
1220
+ read[path] = { name: file.name, sha256: digest(text) };
1221
+ }
1222
+ else {
1223
+ changes.push({ op: 'upload', path: named, ...base, bytes: file.bytes, sha256: file.sha256, media_type: mediaType(path) });
1224
+ read[path] = { name: file.name, sha256: file.sha256 };
1225
+ }
644
1226
  }
645
- return changes;
1227
+ return { changes, read };
646
1228
  }
647
1229
  async settle(start, caller, restaged = false) {
648
1230
  let copy = start;
649
- const push = copy.push;
650
- if (push.caller !== caller.key)
651
- return this.recovered(copy);
652
- let outcome;
653
- try {
654
- outcome = await caller.submit(push.operation_id, push.changes, push.upload, async (upload) => {
655
- copy = await this.save({ ...copy, push: { ...push, upload } });
656
- });
657
- }
658
- catch (error) {
659
- const refusal = error instanceof HouseRefusal ? refusalOf(error.text) : null;
660
- return {
661
- refused: true,
662
- text: refusal?.text ?? `House's answer to ${short(push.commit)} did not arrive; run house push again`,
663
- };
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 } });
664
1234
  }
665
- if ('refused' in outcome) {
666
- if (outcome.refused.code === 'upload_unavailable' && copy.push.upload !== undefined && !restaged) {
667
- const { upload, ...unstaged } = copy.push;
668
- return this.settle(await this.save({ ...copy, push: unstaged }), caller, true);
669
- }
670
- if (outcome.refused.code === 'expired')
671
- return this.recovered(copy);
672
- const rebased = rebaseFixes(outcome.refused);
673
- 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;
674
1240
  try {
675
- 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
+ });
676
1244
  }
677
- catch {
678
- return {
679
- refused: true,
680
- text: rebased ? `${outcome.refused.text}\nHouse's current state did not arrive; push again later` : outcome.refused.text,
681
- };
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" };
682
1248
  }
683
- return {
684
- refused: true,
685
- text: rebased
686
- ? `${outcome.refused.text}\n${HOUSE_REF} holds House's current state; rebase onto it and push again`
687
- : outcome.refused.text,
688
- };
689
- }
690
- return this.finished(await this.save({ ...copy, push: { ...copy.push, state: 'accepted', written: outcome.accepted } }));
691
- }
692
- async recovered(copy) {
693
- const push = copy.push;
694
- const current = (await this.caughtUp([copy], true))[0];
695
- const house = await tree(copy.repository, current.house);
696
- const root = rootOf(current.room_handle);
697
- if (!(await this.landed(copy.repository, push.changes, house, await tree(copy.repository, push.commit)))) {
698
- await this.save(without(current));
699
- return {
700
- refused: true,
701
- text: `House does not hold ${short(push.commit)}; run house push again`,
702
- };
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));
703
1268
  }
704
- const written = [];
705
- for (const path of changedPaths(push.changes).map(relative)) {
706
- const entry = house.get(path);
707
- written.push({
708
- path: `${root}/${path}`,
709
- revision: entry === undefined ? null : (revisionAt(current, current.house, path) ?? null),
710
- content: entry === undefined ? null : await blobText(copy.repository, entry),
711
- });
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') };
712
1281
  }
713
- return this.finished(await this.save({ ...current, push: { ...push, state: 'accepted', written } }));
1282
+ return this.integrated(copy, outcome.accepted);
714
1283
  }
715
- async finished(start) {
1284
+ async sentParts(start, named, caller) {
716
1285
  let copy = start;
717
- const { commit } = copy.push;
718
- try {
719
- if (copy.push.acknowledged === undefined)
720
- copy = await this.save(await this.acknowledged(copy));
721
- const integrated = await this.integrated(copy.push);
722
- if (integrated.done)
723
- await this.save(without(copy));
724
- return { refused: false, text: integrated.text };
725
- }
726
- catch {
727
- return {
728
- refused: false,
729
- text: `House accepted ${short(commit)}; run house push again to finish integrating it here`,
730
- };
731
- }
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;
732
1309
  }
733
- async acknowledged(start) {
734
- let copy = start;
735
- const push = copy.push;
736
- let written = push.written;
737
- if (push.changes.some((change) => change.op === 'rename')) {
738
- copy = (await this.caughtUp([copy], true))[0];
739
- const house = await tree(copy.repository, copy.house);
740
- const sent = await tree(copy.repository, push.house);
741
- if (push.changes.some((change) => change.op === 'rename' && !house.has(relative(change.to)))) {
742
- throw new Error(`House's result for ${short(push.commit)} is not readable yet`);
743
- }
744
- const known = new Set(written.map((entry) => relative(entry.path)));
745
- for (const path of new Set([...sent.keys(), ...house.keys()])) {
746
- const entry = house.get(path);
747
- if (known.has(path) || sent.get(path) === entry)
748
- continue;
749
- written = [
750
- ...written,
751
- {
752
- path: `${rootOf(copy.room_handle)}/${path}`,
753
- revision: entry === undefined ? null : (revisionAt(copy, copy.house, path) ?? null),
754
- content: entry === undefined ? null : await blobText(copy.repository, entry),
755
- },
756
- ];
757
- }
758
- }
759
- const selected = await tree(copy.repository, push.commit);
760
- const houseTree = await tree(copy.repository, copy.house);
761
- const files = new Map();
762
- const revisions = {};
763
- for (const entry of written) {
764
- const path = relative(entry.path);
765
- if (!selected.has(path) && !houseTree.has(path) && !declaredWritable(path, copy))
766
- continue;
767
- files.set(path, entry.revision === null ? null : (entry.content ?? (await this.revisionText(entry.path, entry.revision))));
768
- revisions[path] = entry.revision;
769
- }
770
- const edits = new Map([...files].filter(([path, content]) => (content === null ? selected.has(path) : selected.get(path) !== blobId(content))));
771
- const received = [...files.keys()].filter((path) => revisionAt(copy, copy.house, path) === revisionAt(copy, push.house, path));
772
- const accepted = `House ${rootOf(copy.room_handle)} with ${push.commit} accepted`;
773
- const house = await importCommits(copy.repository, [
774
- ...(edits.size === 0 ? [] : [{ message: accepted, from: push.commit, files: edits }]),
775
- {
776
- message: accepted,
777
- from: copy.house,
778
- merge: edits.size === 0 ? push.commit : ':1',
779
- files: new Map(received.map((path) => [path, files.get(path)])),
780
- },
781
- ]);
782
- const acknowledged = edits.size === 0 ? push.commit : await revision(copy.repository, `${house}^2`);
783
- 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'));
784
1325
  return {
785
- ...copy,
786
- house,
787
- manifests: {
788
- ...copy.manifests,
789
- [acknowledged]: { parent: push.base, files: revisions },
790
- [house]: { parent: copy.house, files: Object.fromEntries(received.map((path) => [path, revisions[path] ?? null])) },
791
- },
792
- moves: copy.moves.filter(({ from, to }) => !(after.has(to) && !after.has(from))),
793
- 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'),
794
1328
  };
795
1329
  }
796
- async landed(repository, changes, house, selected) {
797
- const text = async (path) => {
798
- const entry = house.get(relative(path));
799
- return entry === undefined ? undefined : blobText(repository, entry);
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;
800
1363
  };
801
- for (const change of changes) {
802
- const current = await text(change.path);
803
- 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)
804
1368
  continue;
805
- 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`);
806
1375
  continue;
807
1376
  }
808
- 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`);
809
1386
  continue;
810
- const parsed = current === undefined ? null : parse(current, change.path);
811
- const document = parsed?.ok === true ? parsed.document : null;
812
- if (document === null)
813
- return false;
814
- 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`);
815
1395
  continue;
816
- 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
+ }
817
1403
  continue;
818
- 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];
819
1448
  continue;
820
- 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
+ }
821
1475
  }
822
- 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 };
823
1491
  }
824
- async revisionText(path, wanted) {
825
- 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', {
826
1505
  path,
827
1506
  revision: wanted,
828
1507
  });
829
- const content = page.revisions.find((entry) => entry.revision === wanted)?.content;
830
- if (content === undefined)
831
- throw new Error(`House's result for ${path} is not readable yet`);
832
- return content;
833
- }
834
- async integrated(push) {
835
- const { commit, worktree, branch } = push;
836
- const acknowledged = push.acknowledged;
837
- const accepted = `House accepted ${short(commit)}`;
838
- if (acknowledged === commit)
839
- return { done: true, text: `${accepted}.` };
840
- const pending = (reason) => ({
841
- done: false,
842
- text: `${accepted}; local integration is pending (${reason}): ${acknowledged} holds House's result.`,
843
- });
844
- const head = await git(worktree, ['symbolic-ref', '-q', 'HEAD']).then((name) => name.trim(), () => null);
845
- if (branch === null || head !== branch)
846
- return pending('the branch changed');
847
- const tip = await revision(worktree, 'HEAD');
848
- const name = branch.replace(/^refs\/heads\//, '');
849
- if (await succeeded(worktree, ['merge-base', '--is-ancestor', acknowledged, tip])) {
850
- return { done: true, text: `${accepted}; ${name} holds it.` };
851
- }
852
- if (!(await succeeded(worktree, ['merge-base', '--is-ancestor', commit, tip])))
853
- return pending('the branch changed');
854
- if ((await git(worktree, ['status', '--porcelain', '--untracked-files=no'])).trim() !== '') {
855
- return pending('uncommitted changes');
856
- }
857
- if (tip === commit) {
858
- if (!(await succeeded(worktree, ['merge', '--ff-only', '-q', acknowledged])))
859
- return pending('a conflict');
860
- }
861
- else if (!(await succeeded(worktree, ['rebase', '-q', '--onto', acknowledged, commit], IDENTITY))) {
862
- await succeeded(worktree, ['rebase', '--abort']);
863
- return pending('a conflict');
864
- }
865
- 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;
866
1520
  }
867
1521
  }