@agentshouse/kit 0.2.1-cd9cbd8b6428c → 0.2.1-cdc381ebf021f

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 (143) hide show
  1. package/README.md +42 -49
  2. package/dist/acp.js +102 -108
  3. package/dist/afk.js +17 -0
  4. package/dist/agents.js +256 -69
  5. package/dist/api.js +43 -6
  6. package/dist/app-git.js +48 -0
  7. package/dist/bridge.js +42 -62
  8. package/dist/building.js +50 -0
  9. package/dist/calls.js +88 -0
  10. package/dist/chunks.js +49 -0
  11. package/dist/clis.js +169 -95
  12. package/dist/codex-main.js +58 -0
  13. package/dist/connect.js +108 -0
  14. package/dist/connection.js +120 -0
  15. package/dist/content.js +106 -0
  16. package/dist/conversations.js +817 -333
  17. package/dist/copies.js +450 -251
  18. package/dist/earlier.js +263 -0
  19. package/dist/end.js +26 -0
  20. package/dist/events.js +793 -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 +122 -8
  25. package/dist/house-cli.js +359 -0
  26. package/dist/house-main.js +7 -159
  27. package/dist/instructions.js +7 -11
  28. package/dist/kit-cli.js +78 -0
  29. package/dist/kit-main.js +8 -19
  30. package/dist/login.js +133 -104
  31. package/dist/logout.js +15 -14
  32. package/dist/mcp.js +6 -2
  33. package/dist/owner.js +46 -0
  34. package/dist/placement.js +25 -0
  35. package/dist/refusals.js +12 -0
  36. package/dist/relay.js +33 -2
  37. package/dist/resident.js +161 -61
  38. package/dist/scope.js +214 -0
  39. package/dist/scripts.js +303 -0
  40. package/dist/secret-input.js +5 -2
  41. package/dist/settings.js +188 -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/supervisor.js +163 -0
  47. package/dist/transcription.js +131 -40
  48. package/dist/translate.js +19 -16
  49. package/dist/update.js +119 -100
  50. package/dist/wire.js +32 -0
  51. package/dist/work.js +353 -0
  52. package/dist/workspace.js +103 -0
  53. package/package.json +25 -21
  54. package/dist/authority-main.js +0 -22
  55. package/dist/blocks.js +0 -51
  56. package/dist/bootstrap.json +0 -24
  57. package/dist/clis-main.js +0 -23
  58. package/dist/configure-main.js +0 -6
  59. package/dist/enrol-main.js +0 -4
  60. package/dist/stream.js +0 -47
  61. package/how-we-work.md +0 -23
  62. package/skills/ask-matt/LICENSE +0 -21
  63. package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
  64. package/skills/ask-matt/SKILL.md +0 -91
  65. package/skills/ask-matt/agents/openai.yaml +0 -4
  66. package/skills/code-review/LICENSE +0 -21
  67. package/skills/code-review/SKILL.md +0 -89
  68. package/skills/code-review/agents/openai.yaml +0 -3
  69. package/skills/codebase-design/DEEPENING.md +0 -37
  70. package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
  71. package/skills/codebase-design/LICENSE +0 -21
  72. package/skills/codebase-design/SKILL.md +0 -116
  73. package/skills/codebase-design/agents/openai.yaml +0 -3
  74. package/skills/diagnosing-bugs/LICENSE +0 -21
  75. package/skills/diagnosing-bugs/SKILL.md +0 -140
  76. package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
  77. package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
  78. package/skills/domain-modeling/ADR-FORMAT.md +0 -47
  79. package/skills/domain-modeling/CONTEXT-FORMAT.md +0 -60
  80. package/skills/domain-modeling/LICENSE +0 -21
  81. package/skills/domain-modeling/SKILL.md +0 -76
  82. package/skills/domain-modeling/agents/openai.yaml +0 -3
  83. package/skills/grill-me/LICENSE +0 -21
  84. package/skills/grill-me/SKILL.md +0 -8
  85. package/skills/grill-me/agents/openai.yaml +0 -4
  86. package/skills/grill-with-docs/LICENSE +0 -21
  87. package/skills/grill-with-docs/SKILL.md +0 -8
  88. package/skills/grill-with-docs/agents/openai.yaml +0 -4
  89. package/skills/grilling/LICENSE +0 -21
  90. package/skills/grilling/SKILL.md +0 -30
  91. package/skills/grilling/agents/openai.yaml +0 -3
  92. package/skills/handoff/LICENSE +0 -21
  93. package/skills/handoff/SKILL.md +0 -17
  94. package/skills/handoff/agents/openai.yaml +0 -4
  95. package/skills/implement/LICENSE +0 -21
  96. package/skills/implement/SKILL.md +0 -16
  97. package/skills/implement/agents/openai.yaml +0 -4
  98. package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
  99. package/skills/improve-codebase-architecture/LICENSE +0 -21
  100. package/skills/improve-codebase-architecture/SKILL.md +0 -72
  101. package/skills/improve-codebase-architecture/agents/openai.yaml +0 -4
  102. package/skills/prototype/LICENSE +0 -21
  103. package/skills/prototype/LOGIC.md +0 -67
  104. package/skills/prototype/SKILL.md +0 -28
  105. package/skills/prototype/UI.md +0 -112
  106. package/skills/prototype/agents/openai.yaml +0 -3
  107. package/skills/research/LICENSE +0 -21
  108. package/skills/research/SKILL.md +0 -14
  109. package/skills/research/agents/openai.yaml +0 -3
  110. package/skills/resolving-merge-conflicts/LICENSE +0 -21
  111. package/skills/resolving-merge-conflicts/SKILL.md +0 -16
  112. package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
  113. package/skills/tdd/LICENSE +0 -21
  114. package/skills/tdd/SKILL.md +0 -40
  115. package/skills/tdd/agents/openai.yaml +0 -3
  116. package/skills/tdd/mocking.md +0 -59
  117. package/skills/tdd/tests.md +0 -77
  118. package/skills/teach/GLOSSARY-FORMAT.md +0 -35
  119. package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
  120. package/skills/teach/LICENSE +0 -21
  121. package/skills/teach/MISSION-FORMAT.md +0 -31
  122. package/skills/teach/RESOURCES-FORMAT.md +0 -32
  123. package/skills/teach/SKILL.md +0 -141
  124. package/skills/teach/agents/openai.yaml +0 -4
  125. package/skills/to-questionnaire/LICENSE +0 -21
  126. package/skills/to-questionnaire/SKILL.md +0 -55
  127. package/skills/to-questionnaire/agents/openai.yaml +0 -4
  128. package/skills/to-spec/LICENSE +0 -21
  129. package/skills/to-spec/SKILL.md +0 -76
  130. package/skills/to-spec/agents/openai.yaml +0 -4
  131. package/skills/to-tickets/LICENSE +0 -21
  132. package/skills/to-tickets/SKILL.md +0 -106
  133. package/skills/to-tickets/agents/openai.yaml +0 -4
  134. package/skills/wait-what/LICENSE +0 -21
  135. package/skills/wait-what/SKILL.md +0 -8
  136. package/skills/wait-what/agents/openai.yaml +0 -4
  137. package/skills/wayfinder/LICENSE +0 -21
  138. package/skills/wayfinder/SKILL.md +0 -129
  139. package/skills/wayfinder/agents/openai.yaml +0 -4
  140. package/skills/writing-for-agents/LICENSE +0 -21
  141. package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
  142. package/skills/writing-for-agents/SKILL.md +0 -83
  143. package/skills/writing-for-agents/agents/openai.yaml +0 -3
@@ -0,0 +1,303 @@
1
+ import { spawn as spawnPiped } from 'node:child_process';
2
+ import { randomUUID } from 'node:crypto';
3
+ import { once } from 'node:events';
4
+ import { rmSync } from 'node:fs';
5
+ import { mkdir, rm, writeFile } from 'node:fs/promises';
6
+ import { constants } from 'node:os';
7
+ import { join } from 'node:path';
8
+ import { loginPath, onLoginPath } from './clis.js';
9
+ import { kitHome } from './home.js';
10
+ import { childEnvironment } from './relay.js';
11
+ import { killTree } from './scope.js';
12
+ const TERMINAL = 'xterm-256color';
13
+ const FIRST_SIZE = { cols: 80, rows: 24 };
14
+ const SIGNALLED = 128;
15
+ const NEWLINE = 0x0a;
16
+ const NO_BASH = "bash is not on the login shell's PATH";
17
+ const LOST = { refused: 'the Kit lost its connection to House' };
18
+ const UNSENT_CHARS = 64_000;
19
+ // House holds a Web Wizard's run two minutes for its Kit to connect again, so the script waits as long for a dropped connection to come back.
20
+ const RETURN_MS = 120_000;
21
+ // House takes a run on again with its page's terminal size in the frames right after the Kit connects, so a script it sent nothing for in ten seconds is one House no longer carries.
22
+ const TAKEN_ON_MS = 10_000;
23
+ function causeOf(error) {
24
+ return error instanceof Error ? error.message : String(error);
25
+ }
26
+ function failed(plan, cause) {
27
+ process.stderr.write(`kit: ${plan.named} could not start: ${cause}\n`);
28
+ return { refused: `the script could not start: ${cause}` };
29
+ }
30
+ function planned(call) {
31
+ if (call.kind === 'run_wizard')
32
+ return { named: 'a Web Wizard', script: call.script, terminal: 'page', function: null };
33
+ return {
34
+ named: 'an Environment function',
35
+ script: call.script,
36
+ terminal: call.terminal ? 'at once' : null,
37
+ function: { input: call.input, timeCap: call.time_cap, outputCap: call.output_cap },
38
+ };
39
+ }
40
+ function inputs() {
41
+ return join(kitHome(), 'inputs');
42
+ }
43
+ async function inputFile(value) {
44
+ await mkdir(inputs(), { recursive: true, mode: 0o700 });
45
+ const path = join(inputs(), `${randomUUID()}.json`);
46
+ await writeFile(path, JSON.stringify(value), { mode: 0o600, flag: 'wx' });
47
+ return path;
48
+ }
49
+ export function removeInputs() {
50
+ rmSync(inputs(), { recursive: true, force: true });
51
+ }
52
+ function latest(screen) {
53
+ if (screen.length <= UNSENT_CHARS)
54
+ return screen;
55
+ const line = screen.indexOf('\n', screen.length - UNSENT_CHARS);
56
+ return screen.slice(line < 0 ? screen.length - UNSENT_CHARS : line + 1);
57
+ }
58
+ function struck(line, typed) {
59
+ return typed.some((typedLine) => line.includes(typedLine));
60
+ }
61
+ class KeptOutput {
62
+ cap;
63
+ typedText = '';
64
+ text = '';
65
+ bytes = 0;
66
+ line = '';
67
+ lineBytes = 0;
68
+ dropping = false;
69
+ cut = false;
70
+ constructor(cap) {
71
+ this.cap = cap;
72
+ }
73
+ typed(data) {
74
+ this.typedText += data;
75
+ }
76
+ add(data) {
77
+ const pieces = data.split('\n');
78
+ const rest = pieces.pop();
79
+ for (const piece of pieces)
80
+ this.ended(piece);
81
+ this.extend(rest);
82
+ }
83
+ kept() {
84
+ const typed = this.typedLines(true);
85
+ this.text = [...this.text.split(/(?<=\n)/), this.line].filter((line) => line !== '' && !struck(line, typed)).join('');
86
+ this.bytes = Buffer.byteLength(this.text);
87
+ this.trim();
88
+ return { output: this.text, cut: this.cut };
89
+ }
90
+ typedLines(all) {
91
+ const lines = this.typedText.split(/\r\n|\r|\n/);
92
+ if (!all)
93
+ lines.pop();
94
+ return lines.filter((line) => line !== '');
95
+ }
96
+ ended(piece) {
97
+ const line = `${this.line}${piece}\n`;
98
+ const dropped = this.dropping;
99
+ this.line = '';
100
+ this.lineBytes = 0;
101
+ this.dropping = false;
102
+ if (dropped || struck(line, this.typedLines(false)))
103
+ return;
104
+ this.text += line;
105
+ this.bytes += Buffer.byteLength(line);
106
+ if (this.bytes > 2 * this.cap)
107
+ this.trim();
108
+ }
109
+ extend(piece) {
110
+ if (this.dropping)
111
+ return;
112
+ this.line += piece;
113
+ this.lineBytes += Buffer.byteLength(piece);
114
+ if (this.lineBytes <= this.cap)
115
+ return;
116
+ this.text = '';
117
+ this.bytes = 0;
118
+ this.line = '';
119
+ this.lineBytes = 0;
120
+ this.dropping = true;
121
+ this.cut = true;
122
+ }
123
+ trim() {
124
+ if (this.bytes <= this.cap)
125
+ return;
126
+ const bytes = Buffer.from(this.text);
127
+ const start = bytes.indexOf(NEWLINE, bytes.length - this.cap - 1) + 1;
128
+ this.text = bytes.subarray(start).toString('utf8');
129
+ this.bytes = bytes.length - start;
130
+ this.cut = true;
131
+ }
132
+ }
133
+ export class Scripts {
134
+ calls;
135
+ send;
136
+ runs = new Map();
137
+ constructor(calls, send) {
138
+ this.calls = calls;
139
+ this.send = send;
140
+ }
141
+ start(call) {
142
+ void this.calls.carry(call.input_id, () => this.run(call.input_id, planned(call)));
143
+ }
144
+ notified(oneWayCall) {
145
+ if (!('wizard' in oneWayCall))
146
+ return;
147
+ const run = this.runs.get(oneWayCall.wizard);
148
+ if (run === undefined)
149
+ return;
150
+ this.waited(run, null);
151
+ if (oneWayCall.type === 'wizard_input')
152
+ this.typed(run, oneWayCall.data);
153
+ if (oneWayCall.type === 'wizard_size')
154
+ this.resized(run, { cols: oneWayCall.cols, rows: oneWayCall.rows });
155
+ if (oneWayCall.type === 'wizard_end' && run.plan.terminal === 'page')
156
+ this.end(run, {});
157
+ }
158
+ disconnected() {
159
+ for (const run of this.runs.values()) {
160
+ if (run.plan.terminal === 'page')
161
+ this.waited(run, RETURN_MS);
162
+ else
163
+ this.end(run, LOST);
164
+ }
165
+ }
166
+ connected() {
167
+ for (const [id, run] of this.runs) {
168
+ if (run.waiting === null)
169
+ continue;
170
+ this.waited(run, TAKEN_ON_MS);
171
+ if (run.unsent !== '' && this.send({ type: 'wizard_output', wizard: id, data: run.unsent }))
172
+ run.unsent = '';
173
+ }
174
+ }
175
+ async run(id, plan) {
176
+ const size = plan.terminal === 'page' ? null : FIRST_SIZE;
177
+ const output = plan.function === null ? null : new KeptOutput(plan.function.outputCap);
178
+ const run = {
179
+ plan,
180
+ input: null,
181
+ output,
182
+ process: null,
183
+ pipes: [],
184
+ terminal: null,
185
+ size,
186
+ typed: [],
187
+ unsent: '',
188
+ waiting: null,
189
+ ended: null,
190
+ sized: Promise.withResolvers(),
191
+ };
192
+ if (size !== null)
193
+ run.sized.resolve();
194
+ this.runs.set(id, run);
195
+ try {
196
+ return { result: await this.ran(id, run), release: plan.function !== null };
197
+ }
198
+ finally {
199
+ this.runs.delete(id);
200
+ if (run.input !== null)
201
+ await rm(run.input, { force: true });
202
+ }
203
+ }
204
+ async ran(id, run) {
205
+ const { plan, output } = run;
206
+ let exited;
207
+ try {
208
+ const { spawn } = await import('@lydell/node-pty');
209
+ const shell = await onLoginPath('bash');
210
+ if (shell === null)
211
+ return failed(plan, NO_BASH);
212
+ const path = await loginPath();
213
+ await run.sized.promise;
214
+ if (plan.function !== null)
215
+ run.input = await inputFile(plan.function.input);
216
+ if (run.ended !== null)
217
+ return run.ended;
218
+ const environment = { ...childEnvironment(), PATH: path, ...(run.input === null ? {} : { HOUSE_INPUT: run.input }) };
219
+ if (plan.terminal === null) {
220
+ ({ exited } = await this.piped(plan.script, shell, environment, run));
221
+ }
222
+ else {
223
+ const terminal = spawn(shell, ['-c', plan.script], { name: TERMINAL, ...run.size, cwd: process.cwd(), env: environment });
224
+ run.process = terminal;
225
+ run.terminal = terminal;
226
+ terminal.onData((data) => {
227
+ if (!this.send({ type: 'wizard_output', wizard: id, data }))
228
+ run.unsent = latest(`${run.unsent}${data}`);
229
+ output?.add(data);
230
+ });
231
+ for (const data of run.typed.splice(0))
232
+ terminal.write(data);
233
+ exited = new Promise((resolve) => terminal.onExit(({ exitCode, signal }) => resolve(signal ? SIGNALLED + signal : exitCode)));
234
+ }
235
+ }
236
+ catch (error) {
237
+ return failed(plan, causeOf(error));
238
+ }
239
+ let timedOut = false;
240
+ // House sends a function run's time cap in seconds, and a run still going at its cap ends with its process group and the output pipes it holds.
241
+ const capped = plan.function === null ? undefined : setTimeout(() => {
242
+ timedOut = true;
243
+ this.stop(run);
244
+ }, plan.function.timeCap * 1000);
245
+ const code = await exited;
246
+ clearTimeout(capped);
247
+ if (run.ended !== null)
248
+ return run.ended;
249
+ if (output === null)
250
+ return { exit_code: code };
251
+ const kept = output.kept();
252
+ return timedOut ? { timed_out: true, ...kept } : { exit_code: code, ...kept };
253
+ }
254
+ async piped(script, shell, environment, run) {
255
+ const child = spawnPiped(shell, ['-c', script], { cwd: process.cwd(), env: environment, detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
256
+ const exited = new Promise((resolve) => child.once('close', (code, signal) => resolve(signal === null ? code : SIGNALLED + constants.signals[signal])));
257
+ await once(child, 'spawn');
258
+ run.process = { pid: child.pid };
259
+ run.pipes = [child.stdout, child.stderr];
260
+ for (const pipe of run.pipes) {
261
+ pipe.setEncoding('utf8');
262
+ pipe.on('data', (data) => run.output.add(data));
263
+ }
264
+ if (run.ended !== null)
265
+ this.stop(run);
266
+ return { exited };
267
+ }
268
+ typed(run, data) {
269
+ run.output?.typed(data);
270
+ if (run.terminal === null)
271
+ run.typed.push(data);
272
+ else
273
+ run.terminal.write(data);
274
+ }
275
+ resized(run, size) {
276
+ run.size = size;
277
+ run.sized.resolve();
278
+ try {
279
+ run.terminal?.resize(size.cols, size.rows);
280
+ }
281
+ catch { }
282
+ }
283
+ end(run, result) {
284
+ if (run.ended !== null)
285
+ return;
286
+ this.waited(run, null);
287
+ run.ended = result;
288
+ run.sized.resolve();
289
+ this.stop(run);
290
+ }
291
+ waited(run, ms) {
292
+ if (run.waiting !== null)
293
+ clearTimeout(run.waiting);
294
+ run.waiting = ms === null ? null : setTimeout(() => this.end(run, LOST), ms);
295
+ run.waiting?.unref();
296
+ }
297
+ stop(run) {
298
+ if (run.process !== null)
299
+ killTree(run.process);
300
+ for (const pipe of run.pipes)
301
+ pipe.destroy();
302
+ }
303
+ }
@@ -1,8 +1,8 @@
1
- import { retryDelay } from './api.js';
1
+ import { finalRefusal, retryDelay } from './api.js';
2
2
  export async function holdSecretInput(house, subject, steps, waiting) {
3
3
  for (let failures = 0; waiting();) {
4
4
  try {
5
- const held = await house.post(`/kit/secret-input/${subject}`, { steps });
5
+ const held = await house.act('secret-inputs/await', { subject, steps });
6
6
  if (held.outcome === 'collected')
7
7
  return held.content;
8
8
  if (held.release !== 'held')
@@ -10,7 +10,10 @@ export async function holdSecretInput(house, subject, steps, waiting) {
10
10
  failures = 0;
11
11
  }
12
12
  catch (error) {
13
+ if (finalRefusal(error))
14
+ return null;
13
15
  process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
16
+ // A wait that did not reach House is held again on the jittered curve for as long as its asker still waits.
14
17
  await new Promise((resolve) => setTimeout(resolve, retryDelay(failures++)));
15
18
  }
16
19
  }
@@ -0,0 +1,188 @@
1
+ import { constants } from 'node:fs';
2
+ import { access, mkdir, readdir, rm, stat } from 'node:fs/promises';
3
+ import { dirname, isAbsolute, join, resolve } from 'node:path';
4
+ import { house } from './api.js';
5
+ import { earlierPrefix, enrolled, kitHome, readDisconnected, oneFolder, readConfiguration, readEnrolment, readHome, storedConfiguration, workspaceRoot, writeConfiguration, writeHome, } from './home.js';
6
+ import { askOwner, holdOwner } from './owner.js';
7
+ import { placeSkillSet } from './skills.js';
8
+ import { followRoot, merged, present, within } from './workspace.js';
9
+ const FOLDERS = ['agents', 'local-copies'];
10
+ const RECORDS = join('local-copies', 'rooms');
11
+ const MOVE = 'workspace-move.json';
12
+ const WORKING = 'an Agent is working; change the workspace once its turn ends';
13
+ const LOGGED_OUT = "Kit is logged out, so it cannot tell which Agents' folders their owners chose; run kit login, then change its workspace";
14
+ export const SETTINGS_USAGE = 'usage: kit settings [skills on|off | workspace <absolute path>]';
15
+ export async function enrolledRoutes() {
16
+ if (enrolled())
17
+ return (await house.act('agents/desired/list', {})).routes;
18
+ return (await readDisconnected()) === null ? [] : null;
19
+ }
20
+ const IDLE = { working: () => false, end: () => undefined, routes: enrolledRoutes };
21
+ export async function heldSettings() {
22
+ return { skills: (await readConfiguration()).skills, workspace: workspaceRoot() };
23
+ }
24
+ function records() {
25
+ return join(kitHome(), RECORDS);
26
+ }
27
+ function placing(kept) {
28
+ return (source, entry) => {
29
+ if ((dirname(source) === records() && entry.isFile()) || kept.includes(source))
30
+ return 'stays';
31
+ return [records(), ...kept].some((folder) => within(folder, source)) ? 'opens' : 'moves';
32
+ };
33
+ }
34
+ function causeOf(error) {
35
+ return error instanceof Error ? error.message : String(error);
36
+ }
37
+ async function recordsOnly(folder) {
38
+ const entries = await readdir(folder);
39
+ if (entries.some((name) => name !== 'rooms'))
40
+ return false;
41
+ if (entries.length === 0)
42
+ return true;
43
+ return (await readdir(join(folder, 'rooms'), { withFileTypes: true })).every((entry) => entry.isFile());
44
+ }
45
+ async function writable(root) {
46
+ for (let at = root;; at = dirname(at)) {
47
+ const found = await stat(at).catch((error) => {
48
+ if (error.code === 'ENOENT')
49
+ return null;
50
+ throw error;
51
+ });
52
+ if (found !== null) {
53
+ return found.isDirectory() && access(at, constants.W_OK | constants.X_OK).then(() => true, () => false);
54
+ }
55
+ if (dirname(at) === at)
56
+ return false;
57
+ }
58
+ }
59
+ async function refusedRoot(from, to) {
60
+ for (const name of FOLDERS) {
61
+ const moving = join(from, name);
62
+ if (within(to, moving))
63
+ return `${to} is inside ${moving}, which the change moves`;
64
+ }
65
+ if (!(await writable(to)))
66
+ return `this account cannot write ${to}`;
67
+ for (const name of FOLDERS) {
68
+ const held = join(to, name);
69
+ if ((await present(held)) === null)
70
+ continue;
71
+ if (name === 'local-copies' && to === kitHome() && (await recordsOnly(held)))
72
+ continue;
73
+ return `${to} already holds ${held}`;
74
+ }
75
+ return null;
76
+ }
77
+ async function forward({ from, to, kept = [] }) {
78
+ await mkdir(to, { recursive: true, mode: 0o700 });
79
+ for (const name of FOLDERS)
80
+ await merged(join(from, name), join(to, name), placing(kept));
81
+ await followRoot(records(), from, to, kept);
82
+ await writeConfiguration({ ...(await storedConfiguration()), workspace: to });
83
+ await rm(join(kitHome(), MOVE), { force: true });
84
+ }
85
+ async function backward({ from, to, kept = [] }) {
86
+ for (const name of FOLDERS)
87
+ await merged(join(to, name), join(from, name), placing([]));
88
+ await followRoot(records(), to, from, kept);
89
+ await rm(join(kitHome(), MOVE), { force: true });
90
+ }
91
+ async function finished(move) {
92
+ try {
93
+ await forward(move);
94
+ return null;
95
+ }
96
+ catch (error) {
97
+ const cause = causeOf(error);
98
+ try {
99
+ await backward(move);
100
+ return cause;
101
+ }
102
+ catch (again) {
103
+ return `${cause}; its folders could not all go back to ${move.from} (${causeOf(again)}), so Kit moves them again when it next starts`;
104
+ }
105
+ }
106
+ }
107
+ export async function resumeWorkspaceMove() {
108
+ const move = await readHome(MOVE);
109
+ if (move === null)
110
+ return;
111
+ process.stderr.write(`kit: moving the workspace ${move.from} to ${move.to}, as a change that did not finish asked\n`);
112
+ const failure = await finished(move);
113
+ if (failure !== null)
114
+ process.stderr.write(`kit: the workspace could not be moved to ${move.to}: ${failure}\n`);
115
+ }
116
+ async function changeSkills(on) {
117
+ const stored = await storedConfiguration();
118
+ await writeConfiguration({ ...stored, skills: on });
119
+ const enrolment = await readEnrolment();
120
+ if (enrolment !== null)
121
+ await placeSkillSet(enrolment.house);
122
+ }
123
+ export async function applySettings(change, kit) {
124
+ const from = workspaceRoot();
125
+ let move = null;
126
+ if (change.workspace !== undefined) {
127
+ if (earlierPrefix() !== null) {
128
+ return { refused: `Kit runs where an earlier install placed it; its workspace changes once Kit moves into ${oneFolder()}`, ended: false };
129
+ }
130
+ if (!isAbsolute(change.workspace))
131
+ return { refused: `${change.workspace} is not an absolute path`, ended: false };
132
+ const to = resolve(change.workspace);
133
+ if (to !== from) {
134
+ const refused = await refusedRoot(from, to);
135
+ if (refused !== null)
136
+ return { refused, ended: false };
137
+ const routes = await kit.routes();
138
+ if (routes === null)
139
+ return { refused: LOGGED_OUT, ended: false };
140
+ const agents = join(from, 'agents');
141
+ const copies = join(from, 'local-copies');
142
+ const kept = routes
143
+ .filter(({ agent_id, working_directory }) => resolve(working_directory) !== join(agents, agent_id))
144
+ .map(({ working_directory }) => resolve(working_directory))
145
+ .filter((folder) => folder.startsWith(`${agents}/`) || folder.startsWith(`${copies}/`));
146
+ move = { from, to, kept };
147
+ }
148
+ }
149
+ if (move !== null) {
150
+ if (kit.working())
151
+ return { refused: WORKING, ended: false };
152
+ kit.end();
153
+ await writeHome(MOVE, move);
154
+ const failure = await finished(move);
155
+ if (failure !== null)
156
+ return { refused: `the workspace could not be moved to ${move.to}: ${failure}`, ended: true };
157
+ }
158
+ if (change.skills !== undefined)
159
+ await changeSkills(change.skills);
160
+ return { settings: await heldSettings(), moved: move !== null };
161
+ }
162
+ function changeOf(operands) {
163
+ const [name, value, ...rest] = operands;
164
+ if (name === undefined)
165
+ return null;
166
+ if (rest.length === 0 && name === 'skills' && (value === 'on' || value === 'off'))
167
+ return { skills: value === 'on' };
168
+ if (rest.length === 0 && name === 'workspace' && value !== undefined)
169
+ return { workspace: value };
170
+ throw new Error(SETTINGS_USAGE);
171
+ }
172
+ async function appliedHere(change) {
173
+ const lock = await holdOwner();
174
+ try {
175
+ await resumeWorkspaceMove();
176
+ return await applySettings(change, IDLE);
177
+ }
178
+ finally {
179
+ lock.close();
180
+ }
181
+ }
182
+ export async function kitSettings(operands) {
183
+ const change = changeOf(operands);
184
+ const answer = change === null ? { settings: await heldSettings(), moved: false } : ((await askOwner('/settings', change)) ?? (await appliedHere(change)));
185
+ if ('refused' in answer)
186
+ throw new Error(answer.refused);
187
+ process.stdout.write(`skills ${answer.settings.skills ? 'on' : 'off'}\nworkspace ${answer.settings.workspace}\n`);
188
+ }
package/dist/shell.js ADDED
@@ -0,0 +1,3 @@
1
+ export function quoted(value) {
2
+ return `'${value.replaceAll("'", `'\\''`)}'`;
3
+ }
package/dist/sign-in.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { createInterface } from 'node:readline';
3
- import { killTree } from './acp.js';
4
- import { CLIS, withoutProxy } from './clis.js';
3
+ import { CLIS } from './clis.js';
4
+ import { childEnvironment } from './relay.js';
5
+ import { killTree } from './scope.js';
5
6
  import { holdSecretInput } from './secret-input.js';
6
7
  const ESCAPE = /\u001b\[[\d;]*m/g;
7
8
  const LINK = /https:\/\/\S+/;
@@ -9,52 +10,58 @@ const CODE = /^[A-Z0-9]+-[A-Z0-9]+$/;
9
10
  function causeOf(error) {
10
11
  return error instanceof Error ? error.message : String(error);
11
12
  }
12
- function logged(error) {
13
- process.stderr.write(`kit: ${causeOf(error)}\n`);
13
+ function endOf(signal) {
14
+ return new Promise((resolve) => signal.addEventListener('abort', () => resolve(true), { once: true }));
14
15
  }
15
16
  export class SignIns {
16
17
  house;
17
18
  agents;
18
- changed;
19
- running = new Set();
20
- constructor(house, agents, changed) {
19
+ calls;
20
+ constructor(house, agents, calls) {
21
21
  this.house = house;
22
22
  this.agents = agents;
23
- this.changed = changed;
23
+ this.calls = calls;
24
24
  }
25
- idle() {
26
- return this.running.size === 0;
25
+ start(call) {
26
+ void this.calls.carry(call.input_id, () => this.signIn(call));
27
27
  }
28
- start(input) {
29
- if (this.running.has(input.input_id))
30
- return;
31
- this.running.add(input.input_id);
32
- this.changed();
33
- this.login(input)
34
- .then(() => this.agents.reread(input.cli))
35
- .then(() => this.house.deliver(`/kit/inputs/${input.input_id}/ack`, {}))
36
- .catch(logged)
37
- .finally(() => {
38
- this.running.delete(input.input_id);
39
- this.changed();
40
- });
28
+ async signIn(call) {
29
+ const ending = new AbortController();
30
+ // House stops waiting for a sign-in ten minutes after it wrote it and sends the seconds left, so its install and login end with the page.
31
+ const timer = setTimeout(() => ending.abort(), call.ends_in * 1000);
32
+ const ended = await Promise.race([this.signedIn(call, ending.signal), endOf(ending.signal)]);
33
+ clearTimeout(timer);
34
+ if (ended)
35
+ process.stderr.write(`kit: the ${call.cli} sign-in reached the end House set for it\n`);
36
+ return ended ? { result: {} } : { result: {}, returned: () => this.agents.reread(call.cli) };
41
37
  }
42
- async login(input) {
38
+ async signedIn(call, signal) {
39
+ await this.login(call, signal);
40
+ if (!signal.aborted)
41
+ await this.agents.recheck(call.cli, signal);
42
+ return false;
43
+ }
44
+ async login(call, signal) {
43
45
  let cli;
44
46
  try {
45
- cli = await this.agents.located(input.cli);
47
+ cli = await this.agents.located(call.cli, signal);
46
48
  }
47
49
  catch (error) {
48
- process.stderr.write(`kit: the ${input.cli} sign-in failed: ${causeOf(error)}\n`);
50
+ if (!signal.aborted)
51
+ process.stderr.write(`kit: the ${call.cli} sign-in failed: ${causeOf(error)}\n`);
49
52
  return;
50
53
  }
51
- const login = CLIS[input.cli].login;
54
+ if (signal.aborted)
55
+ return;
56
+ const login = CLIS[call.cli].login;
52
57
  const child = spawn(cli, login.args, {
53
58
  stdio: ['pipe', 'pipe', 'pipe'],
54
59
  detached: true,
55
- env: withoutProxy(),
60
+ env: childEnvironment(),
56
61
  });
57
62
  const running = () => child.exitCode === null && child.signalCode === null;
63
+ const stop = () => killTree(child);
64
+ signal.addEventListener('abort', stop, { once: true });
58
65
  let link;
59
66
  let code;
60
67
  let last = '';
@@ -78,7 +85,7 @@ export class SignIns {
78
85
  return;
79
86
  held = true;
80
87
  const steps = [{ kind: 'visit', label: 'Open this link and sign in', url: link }, step];
81
- void holdSecretInput(this.house, input.input_id, steps, running).then((content) => {
88
+ void holdSecretInput(this.house, call.input_id, steps, running).then((content) => {
82
89
  if (!running())
83
90
  return;
84
91
  if (content === null)
@@ -91,9 +98,10 @@ export class SignIns {
91
98
  createInterface({ input: output }).on('line', read);
92
99
  const failed = await new Promise((resolve) => {
93
100
  child.on('error', (error) => resolve(error.message));
94
- child.on('close', (status, signal) => resolve(status === 0 ? null : `exited with ${status ?? signal}${last ? `: ${last}` : ''}`));
101
+ child.on('close', (status, killed) => resolve(status === 0 ? null : `exited with ${status ?? killed}${last ? `: ${last}` : ''}`));
95
102
  });
96
- if (failed !== null)
97
- process.stderr.write(`kit: the ${input.cli} sign-in failed: ${failed}\n`);
103
+ signal.removeEventListener('abort', stop);
104
+ if (failed !== null && !signal.aborted)
105
+ process.stderr.write(`kit: the ${call.cli} sign-in failed: ${failed}\n`);
98
106
  }
99
107
  }
@@ -0,0 +1,28 @@
1
+ const SYSTEM_CLOCK = {
2
+ later: (delay, callback) => setTimeout(callback, delay),
3
+ clear: (timer) => clearTimeout(timer),
4
+ };
5
+ export class Silence {
6
+ timer = null;
7
+ interval;
8
+ silent;
9
+ clock;
10
+ constructor(interval, silent, clock = SYSTEM_CLOCK) {
11
+ this.interval = interval;
12
+ this.silent = silent;
13
+ this.clock = clock;
14
+ }
15
+ heard() {
16
+ this.stop();
17
+ this.timer = this.clock.later(this.interval, () => {
18
+ this.timer = null;
19
+ this.silent();
20
+ });
21
+ }
22
+ stop() {
23
+ if (this.timer === null)
24
+ return;
25
+ this.clock.clear(this.timer);
26
+ this.timer = null;
27
+ }
28
+ }