@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
@@ -1,34 +1,45 @@
1
- import { createHash } from 'node:crypto';
2
- import { createReadStream } from 'node:fs';
3
1
  import { chmod, mkdir, mkdtemp, readdir, readFile, rename, rm, stat } from 'node:fs/promises';
4
2
  import { availableParallelism, tmpdir } from 'node:os';
5
3
  import { basename, join } from 'node:path';
6
- import { pipeline } from 'node:stream/promises';
4
+ import { isDeepStrictEqual } from 'node:util';
7
5
  import { unlessAborted } from './abort.js';
6
+ import { downloadServed } from './building.js';
8
7
  import { failureOf, run } from './clis.js';
8
+ import { downloaded, fetched } from './files.js';
9
9
  import { kitHome } from './home.js';
10
10
  const RELEASE = 'https://github.com/agentshouse/kit/releases/download/engine-2';
11
11
  export const MODEL = {
12
- url: 'https://huggingface.co/ggml-org/parakeet-GGUF/resolve/35156454d1a39de06863303dd209fd2bed6ee079/ggml-parakeet-tdt-0.6b-v3-q4_k.bin',
12
+ name: 'ggml-parakeet-tdt-0.6b-v3-q4_k.bin',
13
+ from: 'https://huggingface.co/ggml-org/parakeet-GGUF/resolve/35156454d1a39de06863303dd209fd2bed6ee079/ggml-parakeet-tdt-0.6b-v3-q4_k.bin',
13
14
  sha256: '8b205b8b39c6535e153de6fb11c51db46125d45c4f16ba496fe41a0fe71b885e',
15
+ bytes: 415_611_879,
14
16
  executable: false,
15
17
  };
16
18
  export const ENGINES = {
17
19
  'linux-x64': {
18
- decoder: { url: `${RELEASE}/ffmpeg-linux-x86_64`, sha256: '1f8235d206428305afb89c6e8ab99d100425a0ba61e22bb4c7aff017db8c8de5', executable: true },
19
- engine: { url: `${RELEASE}/parakeet-cli-linux-x86_64`, sha256: 'bb2585695992fe1927a33960002cdf51b8a90a96ad65593c3d0a9f1fbc0865c2', executable: true },
20
+ decoder: { name: 'ffmpeg-linux-x86_64', from: `${RELEASE}/ffmpeg-linux-x86_64`, sha256: '1f8235d206428305afb89c6e8ab99d100425a0ba61e22bb4c7aff017db8c8de5', bytes: 5_175_832, executable: true },
21
+ engine: { name: 'parakeet-cli-linux-x86_64', from: `${RELEASE}/parakeet-cli-linux-x86_64`, sha256: 'bb2585695992fe1927a33960002cdf51b8a90a96ad65593c3d0a9f1fbc0865c2', bytes: 3_801_616, executable: true },
20
22
  },
21
23
  'linux-arm64': {
22
- decoder: { url: `${RELEASE}/ffmpeg-linux-aarch64`, sha256: 'a7e08a0fae0179d1078312f6bb13d2a9aa4d4c71a1d82a022953f25dee452f5e', executable: true },
23
- engine: { url: `${RELEASE}/parakeet-cli-linux-aarch64`, sha256: 'cbc0bedc8daae97f1ef9dcee4200c06226218e300e0fd62ead1306d22742b7ed', executable: true },
24
+ decoder: { name: 'ffmpeg-linux-aarch64', from: `${RELEASE}/ffmpeg-linux-aarch64`, sha256: 'a7e08a0fae0179d1078312f6bb13d2a9aa4d4c71a1d82a022953f25dee452f5e', bytes: 4_590_208, executable: true },
25
+ engine: { name: 'parakeet-cli-linux-aarch64', from: `${RELEASE}/parakeet-cli-linux-aarch64`, sha256: 'cbc0bedc8daae97f1ef9dcee4200c06226218e300e0fd62ead1306d22742b7ed', bytes: 3_477_624, executable: true },
24
26
  },
25
27
  'darwin-arm64': {
26
- decoder: { url: `${RELEASE}/ffmpeg-darwin-arm64`, sha256: '7abe154aaed3d6b49f5167ca7221bb66bed51388925d99a7e4261a41fe52fba6', executable: true },
27
- engine: { url: `${RELEASE}/parakeet-cli-darwin-arm64`, sha256: '9a2aafc0922ad7c0b6db6eff1641e96572ba32ac40bf995a1df242ed2febf297', executable: true },
28
+ decoder: { name: 'ffmpeg-darwin-arm64', from: `${RELEASE}/ffmpeg-darwin-arm64`, sha256: '7abe154aaed3d6b49f5167ca7221bb66bed51388925d99a7e4261a41fe52fba6', bytes: 3_696_768, executable: true },
29
+ engine: { name: 'parakeet-cli-darwin-arm64', from: `${RELEASE}/parakeet-cli-darwin-arm64`, sha256: '9a2aafc0922ad7c0b6db6eff1641e96572ba32ac40bf995a1df242ed2febf297', bytes: 1_939_728, executable: true },
28
30
  },
29
31
  };
30
32
  const CHUNK_SECONDS = 20;
33
+ const OFF = "transcription is off on the Agent's Environment, so this message's audio has no text; turn it on or write instead";
34
+ const NOT_INSTALLED = "the speech engine is not installed on the Agent's Environment, so this message's audio has no text";
35
+ const NOT_DOWNLOADED = 'an audio file this message links could not be downloaded; send the message again';
36
+ const UNDECODABLE = 'an audio file this message links could not be decoded';
37
+ const UNTRANSCRIBED = 'an audio file this message links could not be transcribed';
31
38
  let held = Promise.resolve();
39
+ let holding = Promise.resolve();
40
+ let setting = null;
41
+ let downloading = null;
42
+ let readiness = { installed: false, failure: null };
32
43
  function directory() {
33
44
  return join(kitHome(), 'transcription');
34
45
  }
@@ -36,6 +47,7 @@ function installed(artifact) {
36
47
  return join(directory(), artifact.sha256);
37
48
  }
38
49
  async function executed(command, args, signal) {
50
+ // A decode or a transcription runs as long as its audio needs; the setting turning off or the turn's interrupt or end aborts it instead.
39
51
  const ran = await run(command, args, 0, false, signal);
40
52
  if (ran.status !== 0)
41
53
  throw new Error(failureOf(ran, basename(command)));
@@ -44,15 +56,49 @@ async function executed(command, args, signal) {
44
56
  function present(path) {
45
57
  return stat(path).then(() => true, () => false);
46
58
  }
47
- async function cpus() {
59
+ async function threads() {
48
60
  const [quota, period] = (await readFile('/sys/fs/cgroup/cpu.max', 'utf8').catch(() => 'max')).split(' ');
49
61
  const allowed = quota === 'max' ? Infinity : Math.ceil(Number(quota) / Number(period));
50
- return Math.min(availableParallelism(), allowed);
62
+ return Math.max(1, Math.floor(Math.min(availableParallelism(), allowed) / 2));
51
63
  }
52
- async function sha256(path) {
53
- const hash = createHash('sha256');
54
- await pipeline(createReadStream(path), hash);
55
- return hash.digest('hex');
64
+ function causeOf(error) {
65
+ return error instanceof Error ? error.message : String(error);
66
+ }
67
+ async function failing(reason, work) {
68
+ try {
69
+ return await work;
70
+ }
71
+ catch (error) {
72
+ throw new Error(reason, { cause: error });
73
+ }
74
+ }
75
+ function ready() {
76
+ if (setting !== true)
77
+ throw new Error(OFF);
78
+ if (!readiness.installed)
79
+ throw new Error(NOT_INSTALLED);
80
+ }
81
+ function pinnedHere() {
82
+ return ENGINES[`${process.platform}-${process.arch}`];
83
+ }
84
+ const DOWNLOADS = 3;
85
+ async function downloadedAgain(artifact, signal) {
86
+ // An artifact arrives at a hundred kilobytes a second or its download ends, so a stalled Building cannot hold the install open.
87
+ const deadlineMs = Math.ceil(artifact.bytes / 100);
88
+ const deadline = AbortSignal.timeout(deadlineMs);
89
+ for (let attempt = 1;; attempt++) {
90
+ try {
91
+ return await downloadServed(artifact.name, artifact.sha256, `${installed(artifact)}.part`, AbortSignal.any([signal, deadline]), artifact.bytes);
92
+ }
93
+ catch (error) {
94
+ if (signal.aborted)
95
+ throw error;
96
+ if (deadline.aborted)
97
+ throw new Error(`${artifact.name} did not arrive within ${Math.ceil(deadlineMs / 1000)} seconds`);
98
+ if (attempt === DOWNLOADS)
99
+ throw error;
100
+ }
101
+ }
56
102
  }
57
103
  async function install(artifacts, signal) {
58
104
  await mkdir(directory(), { recursive: true });
@@ -66,12 +112,8 @@ async function install(artifacts, signal) {
66
112
  if (!(await present(installed(artifact))))
67
113
  missing.push(artifact);
68
114
  try {
69
- for (const artifact of missing) {
70
- const part = `${installed(artifact)}.part`;
71
- await executed('curl', ['-fsSL', '--retry', '3', '-o', part, artifact.url], signal);
72
- if ((await sha256(part)) !== artifact.sha256)
73
- throw new Error(`${artifact.url} does not match its sha256`);
74
- }
115
+ for (const artifact of missing)
116
+ await downloadedAgain(artifact, signal);
75
117
  for (const artifact of missing) {
76
118
  if (artifact.executable)
77
119
  await chmod(`${installed(artifact)}.part`, 0o755);
@@ -83,29 +125,63 @@ async function install(artifacts, signal) {
83
125
  await rm(`${installed(artifact)}.part`, { force: true });
84
126
  }
85
127
  }
86
- async function transcribed(path, signal) {
87
- const platform = `${process.platform}-${process.arch}`;
88
- const pinned = ENGINES[platform];
128
+ function reported(next) {
129
+ const changed = !isDeepStrictEqual(next, readiness);
130
+ readiness = next;
131
+ return changed;
132
+ }
133
+ async function settle(transcribe) {
134
+ if (transcribe === setting)
135
+ return false;
136
+ setting = transcribe;
137
+ if (!transcribe) {
138
+ await rm(directory(), { recursive: true, force: true });
139
+ return reported({ installed: false, failure: null });
140
+ }
141
+ const pinned = pinnedHere();
89
142
  if (pinned === undefined)
90
- throw new Error(`no transcription engine is pinned for ${platform}`);
91
- await install([pinned.decoder, pinned.engine, MODEL], signal);
92
- const work = await mkdtemp(join(tmpdir(), 'kit-transcription-'));
143
+ return reported({ installed: false, failure: null, no_engine: true });
144
+ const download = new AbortController();
145
+ downloading = download;
93
146
  try {
94
- await executed(installed(pinned.decoder), ['-nostdin', '-hide_banner', '-loglevel', 'error', '-i', path, '-ar', '16000', '-ac', '1', '-c:a', 'pcm_s16le',
95
- '-f', 'segment', '-segment_time', String(CHUNK_SECONDS), join(work, '%05d.wav')], signal);
96
- const parts = (await readdir(work)).sort().flatMap((part) => ['-f', join(work, part)]);
97
- const text = await executed(installed(pinned.engine), ['-t', String(await cpus()), '-np', '-m', installed(MODEL), ...parts], signal);
98
- return text
99
- .split('\n')
100
- .map((line) => line.trim())
101
- .filter((line) => line !== '')
102
- .join(' ');
147
+ await install([pinned.decoder, pinned.engine, MODEL], download.signal);
148
+ return reported({ installed: true, failure: null });
149
+ }
150
+ catch (error) {
151
+ await rm(directory(), { recursive: true, force: true });
152
+ return reported({ installed: false, failure: download.signal.aborted ? null : causeOf(error) });
103
153
  }
104
154
  finally {
105
- await rm(work, { recursive: true, force: true });
155
+ downloading = null;
106
156
  }
107
157
  }
108
- export async function transcribe(path, signal) {
158
+ export function holdTranscription(transcribe) {
159
+ if (!transcribe)
160
+ downloading?.abort();
161
+ const settled = holding.then(() => settle(transcribe));
162
+ holding = settled.catch(() => undefined);
163
+ return settled;
164
+ }
165
+ export function transcriptionReport() {
166
+ return readiness;
167
+ }
168
+ async function transcribed(audio, parts, signal) {
169
+ const pinned = pinnedHere();
170
+ await mkdir(parts);
171
+ await failing(UNDECODABLE, executed(installed(pinned.decoder), ['-nostdin', '-hide_banner', '-loglevel', 'error', '-i', audio, '-ar', '16000', '-ac', '1', '-c:a', 'pcm_s16le',
172
+ '-f', 'segment', '-segment_time', String(CHUNK_SECONDS), join(parts, '%05d.wav')], signal));
173
+ const segments = (await readdir(parts)).sort().flatMap((part) => ['-f', join(parts, part)]);
174
+ const said = await failing(UNTRANSCRIBED, executed(installed(pinned.engine), ['-t', String(await threads()), '-np', '-m', installed(MODEL), ...segments], signal));
175
+ const text = said
176
+ .split('\n')
177
+ .map((line) => line.trim())
178
+ .filter((line) => line !== '')
179
+ .join(' ');
180
+ if (text === '')
181
+ throw new Error(UNTRANSCRIBED);
182
+ return text;
183
+ }
184
+ async function alone(work, signal) {
109
185
  const before = held;
110
186
  let release;
111
187
  const mine = new Promise((resolve) => {
@@ -114,9 +190,24 @@ export async function transcribe(path, signal) {
114
190
  held = Promise.all([before, mine]);
115
191
  try {
116
192
  await unlessAborted(before, signal);
117
- return await transcribed(path, signal);
193
+ return await work();
118
194
  }
119
195
  finally {
120
196
  release();
121
197
  }
122
198
  }
199
+ export async function transcribe(address, signal) {
200
+ ready();
201
+ const work = await mkdtemp(join(tmpdir(), 'kit-transcription-'));
202
+ try {
203
+ const audio = join(work, 'audio');
204
+ await failing(NOT_DOWNLOADED, downloaded(address, (url) => fetched(url, signal), audio, 'audio'));
205
+ return await alone(() => {
206
+ ready();
207
+ return transcribed(audio, join(work, 'parts'), signal);
208
+ }, signal);
209
+ }
210
+ finally {
211
+ await rm(work, { recursive: true, force: true });
212
+ }
213
+ }
package/dist/translate.js CHANGED
@@ -1,18 +1,21 @@
1
- import { apply, parse, serialize } from '@agentshouse/mdmodel';
1
+ import { apply, definitionAt, HOUSE_TYPED_PATHS, parse, serialize, } from '@agentshouse/mdmodel';
2
2
  function same(left, right) {
3
3
  return JSON.stringify(left) === JSON.stringify(right);
4
4
  }
5
- function applied(document, changes) {
6
- let current = document;
7
- for (const change of changes) {
8
- const result = apply(current, change);
9
- if (result.outcome !== 'applied')
10
- return null;
11
- current = result.document;
12
- }
13
- return serialize(current);
5
+ function applied(document, changes, definition) {
6
+ const result = apply(document, changes, definition);
7
+ return result.outcome === 'applied' ? serialize(result.document) : null;
8
+ }
9
+ export function readDocument(content, file, model) {
10
+ const definitions = definitionAt(model, file, HOUSE_TYPED_PATHS);
11
+ const parsed = parse(content, file, definitions);
12
+ if (!parsed.ok)
13
+ return null;
14
+ const { document } = parsed;
15
+ const definition = definitions(document.type);
16
+ return definition !== undefined || document.type === undefined ? { document, definition } : null;
14
17
  }
15
- function components(path, before, after, text) {
18
+ function components(path, before, after, text, definition) {
16
19
  const edits = [];
17
20
  const changes = [];
18
21
  if (before.preamble !== after.preamble) {
@@ -39,13 +42,13 @@ function components(path, before, after, text) {
39
42
  edits.push({ op: 'replace_section', path, section: name, base: prior, content: value });
40
43
  changes.push({ kind: 'section', name, value, prior });
41
44
  }
42
- return edits.length > 0 && applied(before, changes) === text ? edits : null;
45
+ return edits.length > 0 && applied(before, changes, definition) === text ? edits : null;
43
46
  }
44
- export function changed(path, file, base, before, after) {
47
+ export function changed(path, file, base, before, after, model) {
45
48
  if (!file.endsWith('.md'))
46
49
  return [{ op: 'replace', path, base, content: after }];
47
- const parsedBefore = parse(before, file);
48
- const parsedAfter = parse(after, file);
49
- const structured = parsedBefore.ok && parsedAfter.ok ? components(path, parsedBefore.document, parsedAfter.document, after) : null;
50
+ const prior = readDocument(before, file, model);
51
+ const next = readDocument(after, file, model);
52
+ const structured = prior !== null && next !== null ? components(path, prior.document, next.document, after, prior.definition) : null;
50
53
  return structured ?? [{ op: 'replace', path, base, content: after }];
51
54
  }
package/dist/update.js CHANGED
@@ -1,117 +1,127 @@
1
1
  import { spawn } from 'node:child_process';
2
- import { createHash } from 'node:crypto';
3
- import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
4
- import { homedir } from 'node:os';
2
+ import { mkdir } from 'node:fs/promises';
5
3
  import { join } from 'node:path';
6
- import { run } from './clis.js';
7
- import { kitHome } from './home.js';
8
- const RECORD = 'outcome=$1; shift; bash "$0" --linux "$@"; printf %s "$?" > "$outcome"; systemctl start house-kit.service';
9
- function logged(error) {
10
- process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
4
+ import { downloadServed, servedRelease } from './building.js';
5
+ import { KIT_RELEASE } from './clis.js';
6
+ import { restart, updateLine } from './end.js';
7
+ import { kitHome, readEnrolment } from './home.js';
8
+ import { placedPrefix } from './placement.js';
9
+ import { childEnvironment, relayOrigin } from './relay.js';
10
+ const INSTALLER_BYTES = 1_000_000;
11
+ // A release's Node.js, Kit package and adapters are about a hundred megabytes, so an hour carries them at thirty kilobytes a second, and a placement still running then has hung.
12
+ const PLACEMENT_MS = 60 * 60_000;
13
+ // A refused Kit is offline until its placement succeeds, so it tries again within five minutes of its Building or this computer recovering, while a placement that fails only after its hundred-megabyte download runs at most twelve times an hour.
14
+ export const PLACEMENT_RETRY_MS = 5 * 60_000;
15
+ const REFUSED = 'kit_bootstrap_refused: ';
16
+ const OUTPUT_CHARACTERS = 4000;
17
+ function causeOf(error) {
18
+ if (error instanceof TypeError && error.cause instanceof Error)
19
+ return `the Building did not answer: ${error.cause.message}`;
20
+ return error instanceof Error ? error.message : String(error);
11
21
  }
12
- function directory() {
13
- return join(kitHome(), 'update');
14
- }
15
- function pending() {
16
- return join(directory(), 'pending.json');
17
- }
18
- function outcome() {
19
- return join(directory(), 'outcome');
20
- }
21
- function unit(inputId) {
22
- return `house-kit-update-${inputId}`;
22
+ function placing(installer, house, prefix, signal) {
23
+ return new Promise((resolve) => {
24
+ const relay = relayOrigin();
25
+ const child = spawn('bash', [installer, '--house', house, '--place', prefix], {
26
+ stdio: ['ignore', 'pipe', 'pipe'],
27
+ env: { ...childEnvironment(), ...(relay === undefined ? {} : { HOUSE_KIT_RELAY: relay }) },
28
+ detached: true,
29
+ });
30
+ const stop = () => {
31
+ try {
32
+ process.kill(-child.pid, 'SIGKILL');
33
+ }
34
+ catch { }
35
+ };
36
+ signal.addEventListener('abort', stop, { once: true });
37
+ if (signal.aborted)
38
+ stop();
39
+ let output = '';
40
+ for (const stream of [child.stdout, child.stderr]) {
41
+ stream.setEncoding('utf8').on('data', (chunk) => {
42
+ output = (output + chunk).slice(-OUTPUT_CHARACTERS);
43
+ });
44
+ }
45
+ const ended = (failure) => {
46
+ signal.removeEventListener('abort', stop);
47
+ resolve(failure);
48
+ };
49
+ child.on('error', (error) => ended(error.message));
50
+ child.on('close', (status) => {
51
+ if (status === 0)
52
+ return ended(null);
53
+ if (signal.aborted)
54
+ return ended(`the placement did not end within ${PLACEMENT_MS / 1000} seconds`);
55
+ const lines = output.split('\n').filter((line) => line.trim() !== '');
56
+ const refused = lines.findLast((line) => line.startsWith(REFUSED));
57
+ ended(refused?.slice(REFUSED.length) ?? lines.at(-1) ?? `the placement ended with status ${status ?? 'a signal'}`);
58
+ });
59
+ });
23
60
  }
24
- async function pendingInput() {
25
- try {
26
- return JSON.parse(await readFile(pending(), 'utf8')).input_id;
61
+ export class KitUpdate {
62
+ calls;
63
+ queue = Promise.resolve();
64
+ placed = null;
65
+ constructor(calls) {
66
+ this.calls = calls;
27
67
  }
28
- catch {
29
- return null;
68
+ take(call) {
69
+ void this.calls.carry(call.input_id, async () => ({ result: await this.queued(() => this.update(call.release)) }));
30
70
  }
31
- }
32
- async function recorded() {
33
- const status = await readFile(outcome(), 'utf8').catch(() => null);
34
- if (status === null)
35
- return null;
36
- return status.trim() === '0' ? {} : { refused: `the Kit bootstrap exited with ${status.trim()}` };
37
- }
38
- async function finished(inputId) {
39
- for (;;) {
40
- const ack = await recorded();
41
- if (ack !== null)
42
- return ack;
43
- if ((await run('systemctl', ['is-active', '--quiet', unit(inputId)], 10_000)).status !== 0) {
44
- return (await recorded()) ?? { refused: 'the Kit bootstrap did not finish' };
71
+ async takeRefused(release) {
72
+ for (;;) {
73
+ const failure = await this.queued(async () => (this.placed === null ? this.place(release, 'served') : null));
74
+ if (failure === null)
75
+ restart(`Kit ${this.placed} is placed; its supervisor starts it`);
76
+ process.stderr.write(`kit: ${updateLine(`Kit ${KIT_RELEASE} stays offline and tries again in ${PLACEMENT_RETRY_MS / 60_000} minutes`)}\n`);
77
+ await new Promise((resolve) => setTimeout(resolve, PLACEMENT_RETRY_MS));
45
78
  }
46
- await new Promise((resolve) => setTimeout(resolve, 1000));
47
79
  }
48
- }
49
- function rerun(script, inputId, flags) {
50
- return new Promise((resolve) => {
51
- const child = spawn('systemd-run', [
52
- '--wait',
53
- '--collect',
54
- '--quiet',
55
- `--unit=${unit(inputId)}`,
56
- `--setenv=HOME=${homedir()}`,
57
- '/bin/sh',
58
- '-c',
59
- RECORD,
60
- script,
61
- outcome(),
62
- ...flags,
63
- ], { stdio: ['ignore', 'ignore', 'inherit'] });
64
- child.on('error', () => resolve({ refused: 'no service manager runs this Kit' }));
65
- child.on('close', () => resolve(null));
66
- });
67
- }
68
- export class Updates {
69
- house;
70
- held = new Set();
71
- queue = Promise.resolve();
72
- constructor(house) {
73
- this.house = house;
80
+ placedRelease() {
81
+ return this.placed;
74
82
  }
75
- async resumed() {
76
- const inputId = await pendingInput();
77
- if (inputId === null)
78
- return;
79
- await this.acknowledge(inputId, await finished(inputId));
83
+ queued(work) {
84
+ const taken = this.queue.then(work);
85
+ this.queue = taken.catch(() => undefined);
86
+ return taken;
80
87
  }
81
- start(input) {
82
- if (this.held.has(input.input_id))
83
- return;
84
- this.held.add(input.input_id);
85
- this.queue = this.queue
86
- .then(() => this.carry(input))
87
- .then((ack) => this.acknowledge(input.input_id, ack))
88
- .catch(logged)
89
- .finally(() => this.held.delete(input.input_id));
88
+ async update(release) {
89
+ if (release === (this.placed ?? KIT_RELEASE))
90
+ return {};
91
+ if (this.placed !== null)
92
+ return { refused: `Kit ${this.placed} is placed and starts first, once no turn is open` };
93
+ const failure = await this.place(release, 'named');
94
+ if (failure !== null)
95
+ return { refused: failure };
96
+ process.stderr.write(`kit: Kit ${release} is placed; it starts once no turn is open\n`);
97
+ return {};
90
98
  }
91
- async carry(input) {
92
- if ((await pendingInput()) === input.input_id)
93
- return finished(input.input_id);
94
- const answer = await fetch(input.bootstrap);
95
- if (!answer.ok)
96
- return { refused: `the Kit bootstrap could not be downloaded: ${answer.status}` };
97
- const script = Buffer.from(await answer.arrayBuffer());
98
- if (createHash('sha256').update(script).digest('hex') !== input.sha256) {
99
- return { refused: 'the Kit bootstrap does not match its sha256' };
99
+ async place(release, which) {
100
+ const signal = AbortSignal.timeout(PLACEMENT_MS);
101
+ let version = release;
102
+ let failure;
103
+ try {
104
+ const prefix = placedPrefix();
105
+ const served = await servedRelease(signal);
106
+ if (which === 'named' && served.version !== release)
107
+ throw new Error(`the Building serves Kit ${served.version}, not Kit ${release}`);
108
+ version = served.version;
109
+ if (version === KIT_RELEASE)
110
+ throw new Error(`the Building serves Kit ${KIT_RELEASE}, which this Kit runs`);
111
+ await mkdir(join(kitHome(), 'update'), { recursive: true, mode: 0o700 });
112
+ const installer = join(kitHome(), 'update', served.installer);
113
+ await downloadServed(served.installer, served.sha256, installer, signal, INSTALLER_BYTES);
114
+ failure = await placing(installer, (await readEnrolment()).house, prefix, signal);
100
115
  }
101
- await mkdir(directory(), { recursive: true, mode: 0o700 });
102
- const path = join(directory(), 'connect-linux.sh');
103
- await writeFile(path, script, { mode: 0o700 });
104
- await rm(outcome(), { force: true });
105
- await writeFile(pending(), `${JSON.stringify({ input_id: input.input_id })}\n`, { mode: 0o600 });
106
- return (await rerun(path, input.input_id, input.update_clis ? ['--update-clis'] : [])) ?? (await recorded()) ?? {
107
- refused: 'the Kit bootstrap did not finish',
108
- };
109
- }
110
- async acknowledge(inputId, ack) {
111
- await this.house.deliver(`/kit/inputs/${inputId}/ack`, ack);
112
- if ((await pendingInput()) !== inputId)
113
- return;
114
- await rm(pending(), { force: true });
115
- await rm(outcome(), { force: true });
116
+ catch (error) {
117
+ failure = signal.aborted ? `the placement did not end within ${PLACEMENT_MS / 1000} seconds` : causeOf(error);
118
+ }
119
+ if (failure !== null) {
120
+ const refused = `Kit ${version} could not be placed: ${failure}`;
121
+ process.stderr.write(`kit: ${refused}\n`);
122
+ return refused;
123
+ }
124
+ this.placed = version;
125
+ return null;
116
126
  }
117
127
  }
package/dist/wire.js ADDED
@@ -0,0 +1,40 @@
1
+ import { newerThanHouse, updateRequired } from './end.js';
2
+ import { machine } from './placement.js';
3
+ import { refusalOf } from './refusals.js';
4
+ export const KIT_WIRE = 6;
5
+ export const KIT_WIRE_HEADER = 'x-house-kit-wire';
6
+ let take = null;
7
+ let taken = false;
8
+ function unservedBy(text) {
9
+ let body;
10
+ try {
11
+ body = JSON.parse(text);
12
+ }
13
+ catch {
14
+ return null;
15
+ }
16
+ const error = body?.error;
17
+ if (error?.code !== 'kit_update_required' && error?.code !== 'kit_newer_than_house')
18
+ return null;
19
+ return typeof error.release === 'string' ? error : null;
20
+ }
21
+ export function takeUnservedRelease(taking) {
22
+ take = taking;
23
+ }
24
+ export function unserved() {
25
+ return taken;
26
+ }
27
+ export function endUnserved(text) {
28
+ const refused = unservedBy(text);
29
+ if (refused !== null && take !== null) {
30
+ if (!taken) {
31
+ taken = true;
32
+ take(refused.release);
33
+ }
34
+ return;
35
+ }
36
+ if (refusalOf(text)?.code === 'kit_update_required')
37
+ updateRequired();
38
+ if (refused?.code === 'kit_newer_than_house')
39
+ newerThanHouse(refused.release, refused.commands[machine()]);
40
+ }