@agentshouse/kit 0.2.1-cd9cbd8b6428c → 0.2.1-ce0af1aee9a57

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 (138) hide show
  1. package/README.md +46 -46
  2. package/dist/acp.js +102 -108
  3. package/dist/afk.js +17 -0
  4. package/dist/agents.js +218 -63
  5. package/dist/api.js +34 -2
  6. package/dist/app-git.js +48 -0
  7. package/dist/authority-main.js +3 -3
  8. package/dist/bridge.js +42 -62
  9. package/dist/building.js +50 -0
  10. package/dist/calls.js +71 -0
  11. package/dist/chunks.js +49 -0
  12. package/dist/clis.js +169 -95
  13. package/dist/codex-main.js +58 -0
  14. package/dist/connection.js +120 -0
  15. package/dist/content.js +63 -0
  16. package/dist/conversations.js +853 -333
  17. package/dist/copies.js +451 -251
  18. package/dist/end.js +33 -0
  19. package/dist/enrol-main.js +7 -1
  20. package/dist/events.js +1057 -0
  21. package/dist/files.js +139 -17
  22. package/dist/git.js +57 -21
  23. package/dist/grok.js +39 -0
  24. package/dist/home.js +89 -7
  25. package/dist/house-cli.js +299 -0
  26. package/dist/house-main.js +7 -159
  27. package/dist/instructions.js +7 -11
  28. package/dist/kit-cli.js +65 -0
  29. package/dist/kit-main.js +8 -19
  30. package/dist/login.js +117 -102
  31. package/dist/logout.js +14 -13
  32. package/dist/mcp.js +6 -2
  33. package/dist/placement.js +31 -0
  34. package/dist/refusals.js +12 -0
  35. package/dist/relay.js +33 -2
  36. package/dist/resident.js +138 -61
  37. package/dist/scope.js +214 -0
  38. package/dist/scripts.js +255 -0
  39. package/dist/secret-input.js +5 -2
  40. package/dist/service-main.js +9 -0
  41. package/dist/service.js +55 -0
  42. package/dist/shell.js +3 -0
  43. package/dist/sign-in.js +40 -32
  44. package/dist/silence.js +28 -0
  45. package/dist/skills.js +49 -35
  46. package/dist/transcription.js +131 -40
  47. package/dist/translate.js +19 -16
  48. package/dist/update.js +113 -103
  49. package/dist/wire.js +40 -0
  50. package/dist/work.js +353 -0
  51. package/package.json +22 -20
  52. package/dist/blocks.js +0 -51
  53. package/dist/bootstrap.json +0 -24
  54. package/dist/clis-main.js +0 -23
  55. package/dist/stream.js +0 -47
  56. package/how-we-work.md +0 -23
  57. package/skills/ask-matt/LICENSE +0 -21
  58. package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
  59. package/skills/ask-matt/SKILL.md +0 -91
  60. package/skills/ask-matt/agents/openai.yaml +0 -4
  61. package/skills/code-review/LICENSE +0 -21
  62. package/skills/code-review/SKILL.md +0 -89
  63. package/skills/code-review/agents/openai.yaml +0 -3
  64. package/skills/codebase-design/DEEPENING.md +0 -37
  65. package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
  66. package/skills/codebase-design/LICENSE +0 -21
  67. package/skills/codebase-design/SKILL.md +0 -116
  68. package/skills/codebase-design/agents/openai.yaml +0 -3
  69. package/skills/diagnosing-bugs/LICENSE +0 -21
  70. package/skills/diagnosing-bugs/SKILL.md +0 -140
  71. package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
  72. package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
  73. package/skills/domain-modeling/ADR-FORMAT.md +0 -47
  74. package/skills/domain-modeling/CONTEXT-FORMAT.md +0 -60
  75. package/skills/domain-modeling/LICENSE +0 -21
  76. package/skills/domain-modeling/SKILL.md +0 -76
  77. package/skills/domain-modeling/agents/openai.yaml +0 -3
  78. package/skills/grill-me/LICENSE +0 -21
  79. package/skills/grill-me/SKILL.md +0 -8
  80. package/skills/grill-me/agents/openai.yaml +0 -4
  81. package/skills/grill-with-docs/LICENSE +0 -21
  82. package/skills/grill-with-docs/SKILL.md +0 -8
  83. package/skills/grill-with-docs/agents/openai.yaml +0 -4
  84. package/skills/grilling/LICENSE +0 -21
  85. package/skills/grilling/SKILL.md +0 -30
  86. package/skills/grilling/agents/openai.yaml +0 -3
  87. package/skills/handoff/LICENSE +0 -21
  88. package/skills/handoff/SKILL.md +0 -17
  89. package/skills/handoff/agents/openai.yaml +0 -4
  90. package/skills/implement/LICENSE +0 -21
  91. package/skills/implement/SKILL.md +0 -16
  92. package/skills/implement/agents/openai.yaml +0 -4
  93. package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
  94. package/skills/improve-codebase-architecture/LICENSE +0 -21
  95. package/skills/improve-codebase-architecture/SKILL.md +0 -72
  96. package/skills/improve-codebase-architecture/agents/openai.yaml +0 -4
  97. package/skills/prototype/LICENSE +0 -21
  98. package/skills/prototype/LOGIC.md +0 -67
  99. package/skills/prototype/SKILL.md +0 -28
  100. package/skills/prototype/UI.md +0 -112
  101. package/skills/prototype/agents/openai.yaml +0 -3
  102. package/skills/research/LICENSE +0 -21
  103. package/skills/research/SKILL.md +0 -14
  104. package/skills/research/agents/openai.yaml +0 -3
  105. package/skills/resolving-merge-conflicts/LICENSE +0 -21
  106. package/skills/resolving-merge-conflicts/SKILL.md +0 -16
  107. package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
  108. package/skills/tdd/LICENSE +0 -21
  109. package/skills/tdd/SKILL.md +0 -40
  110. package/skills/tdd/agents/openai.yaml +0 -3
  111. package/skills/tdd/mocking.md +0 -59
  112. package/skills/tdd/tests.md +0 -77
  113. package/skills/teach/GLOSSARY-FORMAT.md +0 -35
  114. package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
  115. package/skills/teach/LICENSE +0 -21
  116. package/skills/teach/MISSION-FORMAT.md +0 -31
  117. package/skills/teach/RESOURCES-FORMAT.md +0 -32
  118. package/skills/teach/SKILL.md +0 -141
  119. package/skills/teach/agents/openai.yaml +0 -4
  120. package/skills/to-questionnaire/LICENSE +0 -21
  121. package/skills/to-questionnaire/SKILL.md +0 -55
  122. package/skills/to-questionnaire/agents/openai.yaml +0 -4
  123. package/skills/to-spec/LICENSE +0 -21
  124. package/skills/to-spec/SKILL.md +0 -76
  125. package/skills/to-spec/agents/openai.yaml +0 -4
  126. package/skills/to-tickets/LICENSE +0 -21
  127. package/skills/to-tickets/SKILL.md +0 -106
  128. package/skills/to-tickets/agents/openai.yaml +0 -4
  129. package/skills/wait-what/LICENSE +0 -21
  130. package/skills/wait-what/SKILL.md +0 -8
  131. package/skills/wait-what/agents/openai.yaml +0 -4
  132. package/skills/wayfinder/LICENSE +0 -21
  133. package/skills/wayfinder/SKILL.md +0 -129
  134. package/skills/wayfinder/agents/openai.yaml +0 -4
  135. package/skills/writing-for-agents/LICENSE +0 -21
  136. package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
  137. package/skills/writing-for-agents/SKILL.md +0 -83
  138. package/skills/writing-for-agents/agents/openai.yaml +0 -3
package/dist/copies.js CHANGED
@@ -1,21 +1,38 @@
1
- import { parse } from '@agentshouse/mdmodel';
2
- import { randomUUID } from 'node:crypto';
1
+ import { HOUSE_ROOM_TYPES, readTypes } from '@agentshouse/mdmodel';
2
+ import { createHash, randomUUID } from 'node:crypto';
3
3
  import { mkdir, readFile, readdir, realpath, rename, rm, rmdir, stat, writeFile } from 'node:fs/promises';
4
4
  import { dirname, join } from 'node:path';
5
- import { HouseRefusal } from './api.js';
6
- import { blobId, blobText, git, HOUSE_REF, importCommits, localCopies, revision, run, succeeded, tree, } from './git.js';
5
+ import { finalRefusal, HouseRefusal, retrying } from './api.js';
6
+ import { chunkTexts, stagedChunks } from './chunks.js';
7
+ import { blobId, blobText, git, isText, HOUSE_REF, importCommits, localCopies, revision, run, succeeded, tree, } from './git.js';
8
+ import { paused } from './files.js';
7
9
  import { kitHome } from './home.js';
8
10
  import { operationId } from './operation.js';
9
11
  import { refusalOf } from './refusals.js';
10
- import { changed } from './translate.js';
11
- import { relayed } from './relay.js';
12
+ import { changed, readDocument } from './translate.js';
12
13
  const BRANCH = 'main';
13
14
  const PROVENANCE = '_provenance';
14
- const REPLICA_RETRY_MS = 30_000;
15
- const IDENTITY = { GIT_COMMITTER_NAME: 'House Kit', GIT_COMMITTER_EMAIL: 'house-kit@localhost' };
15
+ const IDENTITY = { GIT_COMMITTER_NAME: 'Kit', GIT_COMMITTER_EMAIL: 'house-kit@localhost' };
16
+ const ROOMS = 'rooms/';
17
+ const ENUMERATE = '/kit/door/changes/enumerate';
18
+ const NOT_READY = 'not ready yet; Kit copies it once House has built it';
19
+ const STAGED_CHUNKS = 'house-chunks';
20
+ const SCHEMA = 'settings/schema.yaml';
21
+ // 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.
22
+ const ASKED_AGAIN_MS = [1000, 2000, 3000, 5000, 8000, 11_000, 15_000];
23
+ class NotReady extends Error {
24
+ rooms;
25
+ constructor(rooms) {
26
+ super(NOT_READY);
27
+ this.rooms = rooms;
28
+ }
29
+ }
16
30
  function causeOf(error) {
17
31
  return error instanceof Error ? error.message : String(error);
18
32
  }
33
+ function houseCause(error) {
34
+ return error instanceof HouseRefusal ? (refusalOf(error.text)?.text ?? causeOf(error)) : causeOf(error);
35
+ }
19
36
  function logged(error) {
20
37
  process.stderr.write(`kit: ${causeOf(error)}\n`);
21
38
  }
@@ -30,9 +47,35 @@ const REBASED = new Set([
30
47
  function rebaseFixes(refusal) {
31
48
  return refusal.code === 'edit_conflict' && refusal.conflicts.some((code) => REBASED.has(code));
32
49
  }
50
+ function textOnly(paths) {
51
+ const [named, them] = paths.length === 1 ? [`${paths[0]} is`, 'it'] : [`${paths.join(', ')} are`, 'them'];
52
+ return `${named} not text, and house push sends text only; remove ${them} from the commit and place ${them} with house upload`;
53
+ }
54
+ function outside(path) {
55
+ return { refused: true, text: `${path} is not a House Local copy; run house push inside one` };
56
+ }
33
57
  function short(commit) {
34
58
  return commit.slice(0, 12);
35
59
  }
60
+ function moved(before, after) {
61
+ if (before.house !== after.house)
62
+ return [`${rootOf(after.room_handle)}: ${HOUSE_REF} is at ${short(after.house)}`];
63
+ return before.position === after.position ? [] : [`${rootOf(after.room_handle)}: read-only files refreshed`];
64
+ }
65
+ function expiredAuthority(error) {
66
+ if (!(error instanceof HouseRefusal))
67
+ return undefined;
68
+ try {
69
+ const { error: refused } = JSON.parse(error.text);
70
+ return refused?.code === 'expired' && typeof refused.authority === 'string' ? refused.authority : undefined;
71
+ }
72
+ catch {
73
+ return undefined;
74
+ }
75
+ }
76
+ function grouped(items, size) {
77
+ return items.flatMap((_, index) => (index % size === 0 ? [items.slice(index, index + size)] : []));
78
+ }
36
79
  function covers(path, prefix) {
37
80
  return prefix === '' || path === prefix || path.startsWith(`${prefix}/`);
38
81
  }
@@ -41,11 +84,17 @@ function sidecar(path) {
41
84
  segments.splice(-1, 0, PROVENANCE);
42
85
  return segments.join('/');
43
86
  }
44
- function rootOf(copy) {
45
- return copy.private ? '/private' : `/rooms/${copy.room_handle}`;
87
+ function recordOf(key) {
88
+ return join(kitHome(), 'local-copies', `${key}.json`);
89
+ }
90
+ function stagingPrefix() {
91
+ return `.${createHash('sha256').update(kitHome()).digest('hex').slice(0, 16)}-`;
92
+ }
93
+ function rootOf(handle) {
94
+ return `/rooms/${handle}`;
46
95
  }
47
96
  function relative(path) {
48
- return path.replace(/^\/private\//, '').replace(/^\/rooms\/[^/]+\//, '');
97
+ return path.replace(/^\/rooms\/[^/]+\//, '');
49
98
  }
50
99
  function declaredWritable(path, copy) {
51
100
  const deepest = (scopes) => Math.max(-1, ...scopes.filter((scope) => covers(path, scope)).map((scope) => scope.length));
@@ -68,32 +117,59 @@ async function removeFile(repository, path) {
68
117
  return;
69
118
  }
70
119
  }
71
- async function readOnlyPlaced(repository, held, written, removed) {
120
+ async function readOnlyFiles(repository, held) {
72
121
  const tracked = new Set((await git(repository, ['ls-files', '-z'])).split('\0'));
73
122
  const holding = new Set(held);
74
- for (const path of removed) {
75
- if (!holding.has(path))
76
- continue;
77
- if (!tracked.has(path))
78
- await removeFile(repository, path);
79
- holding.delete(path);
80
- }
81
- for (const [path, content] of written) {
82
- if (tracked.has(path))
83
- continue;
84
- if (!holding.has(path) && (await exists(join(repository, path))))
85
- continue;
86
- await writeText(join(repository, path), content);
87
- holding.add(path);
88
- }
89
- return [...holding].sort();
123
+ return {
124
+ remove: async (path) => {
125
+ if (!holding.has(path))
126
+ return;
127
+ if (!tracked.has(path))
128
+ await removeFile(repository, path);
129
+ holding.delete(path);
130
+ },
131
+ place: async (path, content) => {
132
+ if (tracked.has(path))
133
+ return;
134
+ if (!holding.has(path) && (await exists(join(repository, path))))
135
+ return;
136
+ await writeText(join(repository, path), content);
137
+ holding.add(path);
138
+ },
139
+ held: () => [...holding].sort(),
140
+ };
141
+ }
142
+ async function readOnlyPlaced(repository, held, written, removed) {
143
+ const files = await readOnlyFiles(repository, held);
144
+ for (const path of removed)
145
+ await files.remove(path);
146
+ for (const [path, content] of written)
147
+ await files.place(path, content);
148
+ return files.held();
149
+ }
150
+ async function importedWhole(repository, scope, texts, commit, readOnly) {
151
+ const revisions = {};
152
+ async function* tracked() {
153
+ for await (const { path, content, revision: held, provenance } of texts) {
154
+ if (declaredWritable(path, scope)) {
155
+ revisions[path] = held;
156
+ yield [path, content];
157
+ }
158
+ else {
159
+ await readOnly.place(path, content);
160
+ }
161
+ if (provenance !== undefined)
162
+ await readOnly.place(sidecar(path), provenance);
163
+ }
164
+ }
165
+ return { house: await importCommits(repository, [{ ...commit, files: tracked() }]), revisions };
90
166
  }
91
167
  async function ruled(path, rules) {
92
168
  if ((await readFile(path, 'utf8').catch(() => null)) !== rules)
93
169
  await writeFile(path, rules);
94
170
  }
95
171
  async function ignoreRules(repository, writable, copy) {
96
- const readOnly = ('read_only' in copy ? copy.read_only : [...copy.readOnly.keys()]).filter((path) => !path.split('/').includes(PROVENANCE));
172
+ const readOnly = copy.read_only.filter((path) => !path.split('/').includes(PROVENANCE));
97
173
  const protectedPath = (path) => copy.protected.some((prefix) => prefix !== '' && covers(path, prefix));
98
174
  const whole = copy.protected.filter((prefix) => prefix !== '' && !writable.some((path) => covers(path, prefix)));
99
175
  const house = [
@@ -102,7 +178,7 @@ async function ignoreRules(repository, writable, copy) {
102
178
  ].map(pattern);
103
179
  const granted = readOnly.filter((path) => !protectedPath(path)).map(pattern);
104
180
  await ruled(join(repository, '.gitignore'), [
105
- '# House Kit writes this file and .ignore. The paths below are read-only House',
181
+ '# Kit writes this file and .ignore. The paths below are read-only House',
106
182
  '# content: Git does not track them, and House refuses a change to them even',
107
183
  '# when this file is edited or a path is force-added.',
108
184
  '/.gitignore',
@@ -117,13 +193,19 @@ async function ignoreRules(repository, writable, copy) {
117
193
  '',
118
194
  ].join('\n'));
119
195
  await ruled(join(repository, '.ignore'), [
120
- '# House Kit writes this file so native search still reads the read-only House',
196
+ '# Kit writes this file so native search still reads the read-only House',
121
197
  '# content that .gitignore keeps out of Git.',
122
198
  `!${PROVENANCE}`,
123
199
  ...[...house, ...granted].map((rule) => `!${rule}`),
124
200
  '',
125
201
  ].join('\n'));
126
202
  }
203
+ async function modelOf(copy, files) {
204
+ const entry = files.get(SCHEMA);
205
+ if (entry !== undefined)
206
+ return readTypes(await blobText(copy.repository, entry), HOUSE_ROOM_TYPES);
207
+ return readTypes(copy.read_only.includes(SCHEMA) ? await readFile(join(copy.repository, SCHEMA), 'utf8') : '', HOUSE_ROOM_TYPES);
208
+ }
127
209
  function revisionAt(copy, commit, path) {
128
210
  let manifest = copy.manifests[commit];
129
211
  while (manifest !== undefined) {
@@ -153,19 +235,19 @@ export class LocalCopies {
153
235
  house;
154
236
  copies = new Map();
155
237
  queues = new Map();
156
- delivering = new Set();
157
- selecting = Promise.resolve();
238
+ waiting = new Set();
239
+ answering = Promise.resolve();
158
240
  constructor(house) {
159
241
  this.house = house;
160
242
  this.owner = {
161
243
  key: 'owner',
162
244
  submit: async (operation, changes) => {
163
245
  try {
164
- const outcome = await house.post('/kit/door/batch', { operation_id: operation, changes });
246
+ const outcome = await retrying(() => house.post('/kit/door/files/batch', { operation_id: operation, changes }));
165
247
  return { accepted: outcome.contents };
166
248
  }
167
249
  catch (error) {
168
- if (!(error instanceof HouseRefusal) || error.status >= 500)
250
+ if (!finalRefusal(error))
169
251
  throw error;
170
252
  return {
171
253
  refused: refusalOf(error.text) ?? { code: '', text: `House refused it with ${error.status}`, conflicts: [] },
@@ -177,7 +259,7 @@ export class LocalCopies {
177
259
  async load() {
178
260
  const home = join(kitHome(), 'local-copies');
179
261
  const names = (await readdir(join(home, 'rooms')).catch(() => [])).filter((name) => name.endsWith('.json'));
180
- for (const path of [...names.map((name) => join(home, 'rooms', name)), join(home, 'private.json')]) {
262
+ for (const path of names.map((name) => join(home, 'rooms', name))) {
181
263
  const text = await readFile(path, 'utf8').catch(() => null);
182
264
  if (text === null)
183
265
  continue;
@@ -185,48 +267,115 @@ export class LocalCopies {
185
267
  this.copies.set(copy.key, copy);
186
268
  }
187
269
  }
188
- async mapping() {
189
- await this.selecting;
190
- return [...this.copies.values()]
191
- .filter((copy) => copy.active)
192
- .map((copy) => `${rootOf(copy)}: ${copy.repository}`)
193
- .sort();
194
- }
195
- select() {
196
- const selected = this.selecting.then(() => this.reselect());
197
- this.selecting = selected.catch(() => undefined);
198
- return selected;
199
- }
200
- async reselect() {
201
- const { local_copy: chosen } = await this.house.post('/kit/local-copy/selection', {});
202
- const selected = [
203
- ...(chosen?.rooms ?? [])
204
- .filter((room) => room.room_handle !== null)
205
- .map((room) => ({ key: `rooms/${room.room_ref}`, private: false, handle: room.room_handle })),
206
- ...(chosen?.private === true ? [{ key: 'private', private: true, handle: null }] : []),
207
- ];
208
- const keys = new Set(selected.map((scope) => scope.key));
209
- const work = [];
210
- for (const key of this.copies.keys()) {
211
- if (keys.has(key))
212
- continue;
213
- work.push(this.exclusive(key, async () => {
214
- const copy = this.copies.get(key);
215
- if (copy.active)
216
- await this.save({ ...copy, active: false });
217
- }));
218
- }
219
- for (const scope of selected)
220
- work.push(this.exclusive(scope.key, () => this.apply(scope)).catch(logged));
221
- await Promise.all(work);
222
- }
223
- entries(frame) {
224
- const copy = [...this.copies.values()].find((held) => held.active && held.room_ref === frame.authority);
225
- if (copy === undefined)
226
- return;
227
- if (copy.log_epoch === frame.log_epoch && BigInt(frame.position) <= BigInt(copy.position))
228
- return;
229
- this.deliver(copy.key);
270
+ async swept() {
271
+ const rooms = join(localCopies(), 'rooms');
272
+ for (const name of await readdir(rooms).catch(() => [])) {
273
+ if (name.startsWith(stagingPrefix()))
274
+ await rm(join(rooms, name), { recursive: true, force: true });
275
+ }
276
+ }
277
+ connected() {
278
+ return this.selected();
279
+ }
280
+ fetch() {
281
+ return this.answered(() => this.fetched(true));
282
+ }
283
+ async selected() {
284
+ const copied = await this.answered(() => this.fetched(false));
285
+ if (copied.refused)
286
+ logged(copied.text);
287
+ }
288
+ answered(work) {
289
+ const answered = this.answering.then(work);
290
+ this.answering = answered.catch(() => undefined);
291
+ return answered;
292
+ }
293
+ async fetched(refreshing) {
294
+ const { local_copy: answer } = await this.house.post('/kit/local-copies/selection/read', {});
295
+ const synced = new Map(answer.rooms.map((room) => [`${ROOMS}${room.room_ref}`, room.room_handle]));
296
+ return this.holding([...new Set([...this.copies.keys(), ...synced.keys()])].sort(), async () => {
297
+ const said = [];
298
+ let refused = false;
299
+ const mark = refreshing ? 'said' : 'unsaid';
300
+ for (const copy of [...this.copies.values()]) {
301
+ if (synced.has(copy.key) || copy.unfollowed === 'said' || copy.unfollowed === mark)
302
+ continue;
303
+ await this.save({ ...copy, unfollowed: mark });
304
+ if (refreshing)
305
+ said.push(`${rootOf(copy.room_handle)}: no longer followed; kept as it is`);
306
+ }
307
+ const current = [];
308
+ const wanted = new Map();
309
+ for (const [key, handle] of synced) {
310
+ try {
311
+ const held = this.copies.get(key);
312
+ if (held === undefined || !(await exists(held.repository))) {
313
+ wanted.set(key, handle);
314
+ continue;
315
+ }
316
+ const { unfollowed, ...followed } = held;
317
+ current.push(unfollowed === undefined && held.room_handle === handle ? held : await this.save({ ...followed, room_handle: handle }));
318
+ }
319
+ catch (error) {
320
+ refused = true;
321
+ said.push(`${rootOf(handle)}: ${causeOf(error)}`);
322
+ }
323
+ }
324
+ const unready = [];
325
+ const unsettled = [];
326
+ if (wanted.size > 0) {
327
+ const { authorities } = await this.discovered([...wanted.values()]);
328
+ for (const [key, handle] of wanted) {
329
+ const authority = authorities.find((entry) => `${ROOMS}${entry.room_ref}` === key);
330
+ try {
331
+ if (authority === undefined)
332
+ throw new Error('House serves nothing of it to copy');
333
+ said.push(await this.copied({ key, handle, authority }));
334
+ }
335
+ catch (error) {
336
+ if (error instanceof NotReady) {
337
+ unready.push(...error.rooms);
338
+ said.push(`${rootOf(handle)}: ${NOT_READY}`);
339
+ }
340
+ else {
341
+ refused = true;
342
+ unsettled.push(key.slice(ROOMS.length));
343
+ said.push(`${rootOf(handle)}: ${causeOf(error)}`);
344
+ }
345
+ }
346
+ }
347
+ }
348
+ const reading = refreshing ? current : current.filter((copy) => this.waiting.has(copy.room_ref));
349
+ if (reading.length > 0) {
350
+ const failed = await this.refreshed(reading, unready).then(() => null, houseCause);
351
+ for (const copy of reading) {
352
+ said.push(...(unready.includes(copy.room_ref) ? [`${rootOf(copy.room_handle)}: ${NOT_READY}`] : moved(copy, this.copies.get(copy.key))));
353
+ }
354
+ if (failed !== null) {
355
+ refused = true;
356
+ unsettled.push(...reading.map((copy) => copy.room_ref));
357
+ said.push(failed);
358
+ }
359
+ }
360
+ for (const room of [...this.waiting]) {
361
+ if (!unready.includes(room) && !unsettled.includes(room))
362
+ this.waiting.delete(room);
363
+ }
364
+ const due = unready.filter((room) => !this.waiting.has(room));
365
+ for (const room of due)
366
+ this.waiting.add(room);
367
+ if (due.length > 0)
368
+ void this.askedAgain(due);
369
+ return { refused, text: said.length === 0 ? 'Nothing new from House.' : said.join('\n') };
370
+ });
371
+ }
372
+ async askedAgain(rooms) {
373
+ let asking = rooms;
374
+ for (let step = 0; asking.length > 0; step++) {
375
+ await paused(ASKED_AGAIN_MS[Math.min(step, ASKED_AGAIN_MS.length - 1)]);
376
+ await this.selected().catch(logged);
377
+ asking = asking.filter((room) => this.waiting.has(room));
378
+ }
230
379
  }
231
380
  async observed(report) {
232
381
  const key = await this.keyAt(dirname(report.common));
@@ -256,11 +405,11 @@ export class LocalCopies {
256
405
  async push(cwd, selector, caller) {
257
406
  const located = await run(cwd, ['rev-parse', '--path-format=absolute', '--git-common-dir', '--show-toplevel']);
258
407
  if (located.status !== 0)
259
- return { refused: true, text: `${cwd} is not a House Local copy; run house push inside one` };
408
+ return outside(cwd);
260
409
  const [common, worktree] = located.stdout.toString('utf8').split('\n');
261
410
  const key = await this.keyAt(dirname(common));
262
411
  if (key === undefined)
263
- return { refused: true, text: `${worktree} is not a House Local copy; run house push inside one` };
412
+ return outside(worktree);
264
413
  return this.exclusive(key, () => this.pushed(key, worktree, selector, caller));
265
414
  }
266
415
  async keyAt(repository) {
@@ -276,19 +425,12 @@ export class LocalCopies {
276
425
  this.queues.set(key, queued.catch(() => undefined));
277
426
  return queued;
278
427
  }
279
- deliver(key) {
280
- if (this.delivering.has(key))
281
- return;
282
- this.delivering.add(key);
283
- void this.exclusive(key, async () => {
284
- this.delivering.delete(key);
285
- const copy = this.copies.get(key);
286
- if (copy.active)
287
- await this.save(await this.caughtUp(copy));
288
- }).catch(logged);
428
+ holding(keys, work) {
429
+ const [key, ...rest] = keys;
430
+ return key === undefined ? work() : this.exclusive(key, () => this.holding(rest, work));
289
431
  }
290
432
  async save(copy) {
291
- const path = join(kitHome(), 'local-copies', copy.private ? 'private.json' : `${copy.key}.json`);
433
+ const path = recordOf(copy.key);
292
434
  await mkdir(dirname(path), { recursive: true, mode: 0o700 });
293
435
  const temporary = `${path}.${process.pid}`;
294
436
  await writeFile(temporary, JSON.stringify(copy), { mode: 0o600 });
@@ -296,163 +438,210 @@ export class LocalCopies {
296
438
  this.copies.set(copy.key, copy);
297
439
  return copy;
298
440
  }
299
- async read(selected) {
300
- const prefixes = [rootOf(selected)];
301
- const discovery = await this.house.post('/kit/door/discover', { prefixes });
302
- let bundle = await this.house.post('/kit/door/bootstrap', { prefixes });
303
- if ('transfer' in bundle)
304
- bundle = (await (await fetch(relayed(bundle.transfer.url))).json());
305
- const [view] = bundle.scopes;
306
- if (view === undefined)
307
- return null;
308
- const authority = discovery.authorities.find((entry) => entry.room_ref === view.manifest.anchor);
309
- const contents = new Map(view.contents.map((entry) => [entry.path, entry.content]));
310
- const read = {
311
- anchor: view.manifest.anchor,
312
- position: view.manifest.position,
313
- log_epoch: view.manifest.log_epoch,
314
- write: authority.operations.write,
315
- protected: authority.protected_paths,
316
- writable: new Map(),
317
- revisions: {},
318
- readOnly: new Map(),
319
- };
320
- for (const file of view.manifest.files) {
321
- const content = contents.get(file.path);
322
- if (file.writable) {
323
- read.writable.set(file.path, content);
324
- read.revisions[file.path] = file.revision;
325
- }
326
- else {
327
- read.readOnly.set(file.path, content);
328
- }
329
- if (file.provenance !== undefined)
330
- read.readOnly.set(sidecar(file.path), file.provenance);
331
- }
332
- return read;
441
+ async discovered(handles) {
442
+ const discover = (group) => this.house.post('/kit/door/rooms/discover', { prefixes: group.map(rootOf) });
443
+ const [first, ...rest] = handles;
444
+ const { bounds, authorities } = await discover([first]);
445
+ for (const group of grouped(rest, bounds.max_prefixes))
446
+ authorities.push(...(await discover(group)).authorities);
447
+ return { bounds, authorities };
333
448
  }
334
- async apply(scope) {
335
- let copy = this.copies.get(scope.key);
336
- if (copy === undefined || !(await exists(copy.repository))) {
337
- await this.materialize(scope);
338
- return;
339
- }
340
- if (copy.room_handle !== scope.handle)
341
- copy = await this.save({ ...copy, room_handle: scope.handle });
342
- if (!copy.active) {
343
- await this.save(await this.received({ ...copy, active: true }));
344
- return;
345
- }
346
- const discovery = await this.house.post('/kit/door/discover', {
347
- prefixes: [rootOf(copy)],
348
- });
349
- const authority = discovery.authorities.find((entry) => entry.room_ref === copy.room_ref);
350
- if (authority === undefined)
351
- return;
352
- if (authority.managed_replication !== 'admitted') {
353
- logged(`operation_denied: ${rootOf(copy)} is not synchronized to this Environment`);
354
- return;
355
- }
356
- if (JSON.stringify([authority.operations.write, authority.protected_paths]) !==
357
- JSON.stringify([copy.write, copy.protected])) {
358
- await this.save(await this.received(copy));
359
- return;
449
+ async chunked(root, room, directory) {
450
+ await mkdir(directory, { recursive: true });
451
+ for (let renewed = false;; renewed = true) {
452
+ const { rooms } = await this.house.post('/kit/door/room-chunks/list', { prefixes: [root] });
453
+ const list = rooms.find((entry) => entry.room_ref === room);
454
+ if (list === undefined)
455
+ throw new Error('House serves nothing of it to copy');
456
+ if (!list.ready)
457
+ throw new NotReady([room]);
458
+ const held = new Set(await readdir(directory));
459
+ const failure = await stagedChunks(directory, list.chunks.filter(({ hash }) => !held.has(hash)));
460
+ if (failure === undefined) {
461
+ return { position: list.position, log_epoch: list.log_epoch, texts: chunkTexts(directory, list.chunks, list.models) };
462
+ }
463
+ if (renewed && (await readdir(directory)).length === held.size)
464
+ throw failure;
360
465
  }
361
- await this.save(await this.caughtUp(copy));
362
466
  }
363
- async materialize(scope) {
364
- const read = await this.read({ private: scope.private, room_handle: scope.handle });
365
- if (read === null) {
366
- logged(`House serves nothing of ${rootOf({ private: scope.private, room_handle: scope.handle })} to copy`);
367
- return;
368
- }
369
- const repository = scope.private ? join(localCopies(), 'private') : join(localCopies(), 'rooms', read.anchor);
370
- if (await exists(repository)) {
371
- logged(`${repository} already exists and House Kit leaves it as it is`);
372
- return;
373
- }
374
- const staging = join(dirname(repository), `.${randomUUID()}`);
467
+ async materialized({ key, handle, authority }, whole) {
468
+ const repository = join(localCopies(), 'rooms', authority.room_ref);
469
+ if (await exists(repository))
470
+ throw new Error(`${repository} already exists and Kit leaves it as it is`);
471
+ const staging = join(dirname(repository), `${stagingPrefix()}${randomUUID()}`);
472
+ const scope = { write: authority.operations.write, protected: authority.protected_paths };
375
473
  await mkdir(staging, { recursive: true });
376
- let house;
377
- let readOnly;
474
+ let copy;
378
475
  try {
379
476
  await git(staging, ['init', '-q', '-b', BRANCH]);
380
- house = await importCommits(staging, [
381
- { message: `House ${rootOf({ private: scope.private, room_handle: scope.handle })}`, from: null, files: read.writable },
382
- ]);
477
+ const read = await whole(join(staging, '.git', STAGED_CHUNKS));
478
+ const readOnly = await readOnlyFiles(staging, []);
479
+ const { house, revisions } = await importedWhole(staging, scope, read.texts, { message: `House ${rootOf(handle)}`, from: null }, readOnly);
383
480
  await git(staging, ['update-ref', `refs/heads/${BRANCH}`, house]);
384
481
  await git(staging, ['reset', '-q', '--hard']);
385
- readOnly = await readOnlyPlaced(staging, [], read.readOnly, []);
386
- await ignoreRules(staging, [...read.writable.keys()], read);
482
+ copy = {
483
+ key,
484
+ room_ref: authority.room_ref,
485
+ room_handle: handle,
486
+ repository,
487
+ house,
488
+ position: read.position,
489
+ log_epoch: read.log_epoch,
490
+ ...scope,
491
+ read_only: readOnly.held(),
492
+ manifests: { [house]: { parent: null, files: revisions } },
493
+ moves: [],
494
+ };
495
+ await ignoreRules(staging, Object.keys(revisions), copy);
496
+ await rm(join(staging, '.git', STAGED_CHUNKS), { recursive: true, force: true });
387
497
  await rename(staging, repository);
388
498
  }
389
499
  catch (error) {
390
500
  await rm(staging, { recursive: true, force: true });
391
501
  throw error;
392
502
  }
393
- await this.save({
394
- key: scope.key,
395
- private: scope.private,
396
- room_ref: read.anchor,
397
- room_handle: scope.handle,
398
- repository,
399
- active: true,
400
- house,
401
- position: read.position,
402
- log_epoch: read.log_epoch,
403
- write: read.write,
404
- protected: read.protected,
405
- read_only: readOnly,
406
- manifests: { [house]: { parent: null, files: read.revisions } },
407
- moves: [],
408
- });
503
+ return this.save(copy);
409
504
  }
410
- async received(copy) {
411
- const read = await this.read(copy);
412
- if (read === null)
413
- return copy;
414
- const house = await importCommits(copy.repository, [
415
- { message: `House ${rootOf(copy)} at position ${read.position}`, from: copy.house, replaces: true, files: read.writable },
416
- ]);
417
- const removed = copy.read_only.filter((path) => !read.readOnly.has(path));
418
- const readOnly = await readOnlyPlaced(copy.repository, copy.read_only, read.readOnly, removed);
419
- const next = {
420
- ...copy,
421
- house,
422
- position: read.position,
423
- log_epoch: read.log_epoch,
424
- write: read.write,
425
- protected: read.protected,
426
- read_only: readOnly,
427
- manifests: { ...copy.manifests, [house]: { parent: null, files: read.revisions } },
428
- };
429
- await ignoreRules(copy.repository, [...read.writable.keys()], next);
430
- return next;
505
+ async copied(wanted) {
506
+ const { handle, authority } = wanted;
507
+ const root = rootOf(handle);
508
+ let answer = null;
509
+ try {
510
+ if (!this.waiting.has(authority.room_ref)) {
511
+ answer = await this.house.post(ENUMERATE, {
512
+ prefixes: [root],
513
+ positions: [
514
+ { authority: authority.room_ref, position: '0', log_epoch: authority.log_epoch, page: { through: authority.position, read: 0 } },
515
+ ],
516
+ });
517
+ }
518
+ }
519
+ catch (error) {
520
+ if (expiredAuthority(error) !== authority.room_ref)
521
+ throw error;
522
+ }
523
+ const view = answer?.authorities.find((entry) => entry.authority === authority.room_ref);
524
+ if (answer !== null && view === undefined)
525
+ throw new Error('House serves nothing of it to copy');
526
+ if (view !== undefined && view.next === undefined) {
527
+ const texts = view.changes.filter((change) => !('removed' in change));
528
+ const copy = await this.materialized(wanted, async () => ({ position: view.position, log_epoch: view.log_epoch, texts }));
529
+ return `${root}: copied to ${copy.repository}`;
530
+ }
531
+ const copy = await this.materialized(wanted, (directory) => this.chunked(root, authority.room_ref, directory));
532
+ const [caught] = await this.caughtUp([copy], true);
533
+ await succeeded(copy.repository, ['merge', '--ff-only', '-q', caught.house]);
534
+ return `${root}: copied to ${copy.repository}`;
431
535
  }
432
- async caughtUp(start) {
433
- let copy = start;
434
- for (;;) {
536
+ async received(copy, authority) {
537
+ const root = rootOf(copy.room_handle);
538
+ const directory = join(copy.repository, '.git', STAGED_CHUNKS);
539
+ const scope = { write: authority.operations.write, protected: authority.protected_paths };
540
+ try {
541
+ const read = await this.chunked(root, copy.room_ref, directory);
542
+ const readOnly = await readOnlyFiles(copy.repository, copy.read_only);
543
+ for (const path of copy.read_only)
544
+ await readOnly.remove(path);
545
+ const commit = { message: `House ${root} at position ${read.position}`, from: copy.house, replaces: true };
546
+ const { house, revisions } = await importedWhole(copy.repository, scope, read.texts, commit, readOnly);
547
+ const next = {
548
+ ...copy,
549
+ ...scope,
550
+ house,
551
+ position: read.position,
552
+ log_epoch: read.log_epoch,
553
+ read_only: readOnly.held(),
554
+ manifests: { ...copy.manifests, [house]: { parent: null, files: revisions } },
555
+ };
556
+ await ignoreRules(copy.repository, Object.keys(revisions), next);
557
+ return next;
558
+ }
559
+ finally {
560
+ await rm(directory, { recursive: true, force: true });
561
+ }
562
+ }
563
+ async reread(copy) {
564
+ const { authorities } = await this.discovered([copy.room_handle]);
565
+ const authority = authorities.find((entry) => entry.room_ref === copy.room_ref);
566
+ if (authority === undefined)
567
+ throw new NotReady([copy.room_ref]);
568
+ return this.received(copy, authority);
569
+ }
570
+ async refreshed(copies, unready) {
571
+ const { bounds, authorities } = await this.discovered(copies.map((copy) => copy.room_handle));
572
+ const read = [];
573
+ const unchanged = [];
574
+ for (const copy of copies) {
575
+ const authority = authorities.find((entry) => entry.room_ref === copy.room_ref);
576
+ if (authority !== undefined &&
577
+ JSON.stringify([authority.operations.write, authority.protected_paths]) !== JSON.stringify([copy.write, copy.protected])) {
578
+ try {
579
+ read.push(await this.save(await this.received(copy, authority)));
580
+ }
581
+ catch (error) {
582
+ if (!(error instanceof NotReady))
583
+ throw error;
584
+ unready.push(...error.rooms);
585
+ }
586
+ }
587
+ else {
588
+ unchanged.push(copy);
589
+ }
590
+ }
591
+ for (const group of grouped(unchanged, bounds.max_prefixes))
592
+ await this.caughtUp(group, false, unready);
593
+ for (const group of grouped(read, bounds.max_prefixes))
594
+ await this.caughtUp(group, true, unready);
595
+ }
596
+ async caughtUp(copies, whole, unready) {
597
+ const caught = new Map(copies.map((copy) => [copy.key, copy]));
598
+ const read = new Set();
599
+ const waiting = unready ?? [];
600
+ let asking = [...copies];
601
+ while (asking.length > 0) {
435
602
  let answer;
436
603
  try {
437
- answer = await this.house.post('/kit/door/enumerate', {
438
- prefixes: [rootOf(copy)],
439
- positions: [{ authority: copy.room_ref, position: copy.position, log_epoch: copy.log_epoch }],
604
+ answer = await this.house.post(ENUMERATE, {
605
+ prefixes: asking.map((copy) => rootOf(copy.room_handle)),
606
+ positions: asking.map((copy) => ({ authority: copy.room_ref, position: copy.position, log_epoch: copy.log_epoch })),
440
607
  });
441
608
  }
442
609
  catch (error) {
443
- const code = error instanceof HouseRefusal ? (refusalOf(error.text)?.code ?? null) : null;
444
- if (code === 'position_expired')
445
- return this.received(copy);
446
- if (code !== 'replica_behind')
610
+ const expired = asking.find((copy) => copy.room_ref === expiredAuthority(error));
611
+ if (expired === undefined)
447
612
  throw error;
448
- setTimeout(() => this.deliver(copy.key), REPLICA_RETRY_MS).unref();
449
- return copy;
613
+ asking = asking.filter((copy) => copy !== expired);
614
+ if (read.has(expired.key)) {
615
+ waiting.push(expired.room_ref);
616
+ continue;
617
+ }
618
+ read.add(expired.key);
619
+ try {
620
+ const next = await this.save(await this.reread(expired));
621
+ caught.set(expired.key, next);
622
+ asking.push(next);
623
+ }
624
+ catch (refused) {
625
+ if (!(refused instanceof NotReady))
626
+ throw refused;
627
+ waiting.push(expired.room_ref);
628
+ }
629
+ continue;
450
630
  }
451
- const view = answer.authorities.find((entry) => entry.authority === copy.room_ref);
452
- if (view === undefined || (view.position === copy.position && view.log_epoch === copy.log_epoch))
453
- return copy;
454
- copy = await this.save(await this.incorporated(copy, view.changes, view.position, view.log_epoch));
631
+ const advanced = [];
632
+ for (const copy of asking) {
633
+ const view = answer.authorities.find((entry) => entry.authority === copy.room_ref);
634
+ if (view === undefined || (view.position === copy.position && view.log_epoch === copy.log_epoch))
635
+ continue;
636
+ const next = await this.save(await this.incorporated(copy, view.changes, view.position, view.log_epoch));
637
+ caught.set(copy.key, next);
638
+ advanced.push(next);
639
+ }
640
+ asking = advanced.filter((copy) => whole || read.has(copy.key));
455
641
  }
642
+ if (unready === undefined && waiting.length > 0)
643
+ throw new NotReady(waiting);
644
+ return copies.map((copy) => caught.get(copy.key));
456
645
  }
457
646
  async incorporated(copy, changes, position, logEpoch) {
458
647
  const houseTree = await tree(copy.repository, copy.house);
@@ -485,7 +674,7 @@ export class LocalCopies {
485
674
  let { house, manifests } = copy;
486
675
  if (files.size > 0) {
487
676
  house = await importCommits(copy.repository, [
488
- { message: `House ${rootOf(copy)} at position ${position}`, from: copy.house, files },
677
+ { message: `House ${rootOf(copy.room_handle)} at position ${position}`, from: copy.house, files },
489
678
  ]);
490
679
  manifests = { ...manifests, [house]: { parent: copy.house, files: revisions } };
491
680
  }
@@ -515,9 +704,6 @@ export class LocalCopies {
515
704
  }
516
705
  copy = await this.save(without(copy));
517
706
  }
518
- if (!copy.active) {
519
- return { refused: true, text: `${rootOf(copy)} is no longer synced to this Environment, so House takes no push from it` };
520
- }
521
707
  const listed = (await git(copy.repository, ['rev-list', '--topo-order', commit])).split('\n');
522
708
  const base = listed.find((ancestor) => ancestor in copy.manifests);
523
709
  if (base === undefined) {
@@ -528,7 +714,7 @@ export class LocalCopies {
528
714
  changes = await this.derived(copy, base, commit);
529
715
  }
530
716
  catch (error) {
531
- return { refused: true, text: causeOf(error) };
717
+ return { refused: true, text: houseCause(error) };
532
718
  }
533
719
  if (changes.length === 0)
534
720
  return { refused: false, text: `Nothing to submit: ${short(commit)} matches House.` };
@@ -550,12 +736,24 @@ export class LocalCopies {
550
736
  return this.settle(submitted, caller);
551
737
  }
552
738
  async derived(copy, base, commit) {
553
- const root = rootOf(copy);
739
+ const root = rootOf(copy.room_handle);
554
740
  const before = await tree(copy.repository, base);
555
741
  const after = await tree(copy.repository, commit);
556
742
  const moved = movedPaths(copy.moves, before, after);
557
743
  const arrived = new Set(moved.values());
558
- const text = (entry, path) => blobText(copy.repository, entry, path);
744
+ const arriving = [...after].filter(([path, entry]) => before.get(path) !== entry);
745
+ if (arriving.length > 0) {
746
+ const { bounds } = await this.house.post('/kit/door/rooms/discover', { prefixes: [root] });
747
+ const binary = [];
748
+ for (const [path, entry] of arriving) {
749
+ if (!(await isText(copy.repository, entry, bounds.max_authored_file_bytes)))
750
+ binary.push(path);
751
+ }
752
+ if (binary.length > 0)
753
+ throw new Error(textOnly(binary));
754
+ }
755
+ const text = (entry) => blobText(copy.repository, entry);
756
+ const model = await modelOf(copy, after);
559
757
  const changes = [];
560
758
  for (const path of [...new Set([...before.keys(), ...after.keys()])].sort()) {
561
759
  if (arrived.has(path))
@@ -568,7 +766,7 @@ export class LocalCopies {
568
766
  changes.push({ op: 'rename', path: `${root}/${path}`, to: `${root}/${to}`, base: known });
569
767
  const moving = after.get(to);
570
768
  if (moving !== prior) {
571
- changes.push({ op: 'replace', path: `${root}/${to}`, base: known, content: await text(moving, to) });
769
+ changes.push({ op: 'replace', path: `${root}/${to}`, base: known, content: await text(moving) });
572
770
  }
573
771
  continue;
574
772
  }
@@ -577,9 +775,9 @@ export class LocalCopies {
577
775
  if (next === undefined)
578
776
  changes.push({ op: 'remove', path: `${root}/${path}`, base: known });
579
777
  else if (prior === undefined)
580
- changes.push({ op: 'create', path: `${root}/${path}`, content: await text(next, path) });
778
+ changes.push({ op: 'create', path: `${root}/${path}`, content: await text(next) });
581
779
  else
582
- changes.push(...changed(`${root}/${path}`, path, known, await text(prior, path), await text(next, path)));
780
+ changes.push(...changed(`${root}/${path}`, path, known, await text(prior), await text(next), model));
583
781
  }
584
782
  return changes;
585
783
  }
@@ -606,12 +804,14 @@ export class LocalCopies {
606
804
  const { upload, ...unstaged } = copy.push;
607
805
  return this.settle(await this.save({ ...copy, push: unstaged }), caller, true);
608
806
  }
609
- if (outcome.refused.code === 'operation_expired')
807
+ if (outcome.refused.code === 'expired')
610
808
  return this.recovered(copy);
611
- const rebased = rebaseFixes(outcome.refused);
612
809
  let settled = await this.save(without(copy));
810
+ if (settled.unfollowed !== undefined)
811
+ return { refused: true, text: outcome.refused.text };
812
+ const rebased = rebaseFixes(outcome.refused);
613
813
  try {
614
- settled = await this.save(await this.caughtUp(settled));
814
+ settled = (await this.caughtUp([settled], true))[0];
615
815
  }
616
816
  catch {
617
817
  return {
@@ -630,10 +830,11 @@ export class LocalCopies {
630
830
  }
631
831
  async recovered(copy) {
632
832
  const push = copy.push;
633
- const current = await this.save(await this.caughtUp(copy));
833
+ const current = (await this.caughtUp([copy], true))[0];
634
834
  const house = await tree(copy.repository, current.house);
635
- const root = rootOf(current);
636
- if (!(await this.landed(copy.repository, push.changes, house, await tree(copy.repository, push.commit)))) {
835
+ const root = rootOf(current.room_handle);
836
+ const model = await modelOf(current, house);
837
+ if (!(await this.landed(copy.repository, push.changes, house, await tree(copy.repository, push.commit), model))) {
637
838
  await this.save(without(current));
638
839
  return {
639
840
  refused: true,
@@ -646,7 +847,7 @@ export class LocalCopies {
646
847
  written.push({
647
848
  path: `${root}/${path}`,
648
849
  revision: entry === undefined ? null : (revisionAt(current, current.house, path) ?? null),
649
- content: entry === undefined ? null : await blobText(copy.repository, entry, path),
850
+ content: entry === undefined ? null : await blobText(copy.repository, entry),
650
851
  });
651
852
  }
652
853
  return this.finished(await this.save({ ...current, push: { ...push, state: 'accepted', written } }));
@@ -674,7 +875,7 @@ export class LocalCopies {
674
875
  const push = copy.push;
675
876
  let written = push.written;
676
877
  if (push.changes.some((change) => change.op === 'rename')) {
677
- copy = await this.save(await this.caughtUp(copy));
878
+ copy = (await this.caughtUp([copy], true))[0];
678
879
  const house = await tree(copy.repository, copy.house);
679
880
  const sent = await tree(copy.repository, push.house);
680
881
  if (push.changes.some((change) => change.op === 'rename' && !house.has(relative(change.to)))) {
@@ -688,9 +889,9 @@ export class LocalCopies {
688
889
  written = [
689
890
  ...written,
690
891
  {
691
- path: `${rootOf(copy)}/${path}`,
892
+ path: `${rootOf(copy.room_handle)}/${path}`,
692
893
  revision: entry === undefined ? null : (revisionAt(copy, copy.house, path) ?? null),
693
- content: entry === undefined ? null : await blobText(copy.repository, entry, path),
894
+ content: entry === undefined ? null : await blobText(copy.repository, entry),
694
895
  },
695
896
  ];
696
897
  }
@@ -708,7 +909,7 @@ export class LocalCopies {
708
909
  }
709
910
  const edits = new Map([...files].filter(([path, content]) => (content === null ? selected.has(path) : selected.get(path) !== blobId(content))));
710
911
  const received = [...files.keys()].filter((path) => revisionAt(copy, copy.house, path) === revisionAt(copy, push.house, path));
711
- const accepted = `House ${rootOf(copy)} with ${push.commit} accepted`;
912
+ const accepted = `House ${rootOf(copy.room_handle)} with ${push.commit} accepted`;
712
913
  const house = await importCommits(copy.repository, [
713
914
  ...(edits.size === 0 ? [] : [{ message: accepted, from: push.commit, files: edits }]),
714
915
  {
@@ -732,10 +933,10 @@ export class LocalCopies {
732
933
  push: { ...push, acknowledged },
733
934
  };
734
935
  }
735
- async landed(repository, changes, house, selected) {
936
+ async landed(repository, changes, house, selected, model) {
736
937
  const text = async (path) => {
737
938
  const entry = house.get(relative(path));
738
- return entry === undefined ? undefined : blobText(repository, entry, path);
939
+ return entry === undefined ? undefined : blobText(repository, entry);
739
940
  };
740
941
  for (const change of changes) {
741
942
  const current = await text(change.path);
@@ -746,9 +947,8 @@ export class LocalCopies {
746
947
  }
747
948
  if ((change.op === 'create' || change.op === 'replace') && current === change.content)
748
949
  continue;
749
- const parsed = current === undefined ? null : parse(current, change.path);
750
- const document = parsed?.ok === true ? parsed.document : null;
751
- if (document === null)
950
+ const document = current === undefined ? undefined : readDocument(current, relative(change.path), model)?.document;
951
+ if (document === undefined)
752
952
  return false;
753
953
  if (change.op === 'set_field' && JSON.stringify(document.fields[change.field]) === JSON.stringify(change.value))
754
954
  continue;
@@ -761,7 +961,7 @@ export class LocalCopies {
761
961
  return true;
762
962
  }
763
963
  async revisionText(path, wanted) {
764
- const page = await this.house.post('/kit/door/revisions', {
964
+ const page = await this.house.post('/kit/door/files/revisions/list', {
765
965
  path,
766
966
  revision: wanted,
767
967
  });