@agentshouse/kit 0.2.1-ccf188e02985d → 0.2.1-ccffb3f637815

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/README.md +37 -41
  2. package/dist/acp.js +66 -12
  3. package/dist/afk.js +17 -0
  4. package/dist/agents.js +122 -53
  5. package/dist/api.js +15 -10
  6. package/dist/binaries.js +89 -0
  7. package/dist/bridge.js +29 -28
  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 +62 -50
  12. package/dist/connect.js +108 -0
  13. package/dist/connection.js +120 -0
  14. package/dist/content.js +106 -0
  15. package/dist/conversations.js +594 -219
  16. package/dist/copies.js +772 -279
  17. package/dist/earlier.js +263 -0
  18. package/dist/end.js +12 -5
  19. package/dist/events.js +549 -281
  20. package/dist/files.js +206 -38
  21. package/dist/git-main.js +24 -29
  22. package/dist/git.js +90 -15
  23. package/dist/grok.js +39 -0
  24. package/dist/home.js +80 -29
  25. package/dist/house-cli.js +155 -164
  26. package/dist/house-main.js +3 -3
  27. package/dist/instructions.js +29 -11
  28. package/dist/keys.js +238 -0
  29. package/dist/kit-cli.js +18 -8
  30. package/dist/kit-main.js +3 -3
  31. package/dist/login.js +36 -22
  32. package/dist/logout.js +2 -2
  33. package/dist/mcp.js +42 -3
  34. package/dist/old-references.js +27 -0
  35. package/dist/owner.js +46 -0
  36. package/dist/placement.js +15 -20
  37. package/dist/refusals.js +37 -21
  38. package/dist/relay.js +21 -0
  39. package/dist/resident.js +128 -82
  40. package/dist/scope.js +15 -4
  41. package/dist/scripts.js +303 -0
  42. package/dist/secret-input.js +1 -1
  43. package/dist/settings.js +188 -0
  44. package/dist/sign-in.js +67 -21
  45. package/dist/silence.js +28 -0
  46. package/dist/skills.js +42 -97
  47. package/dist/supervisor.js +163 -0
  48. package/dist/transcription.js +88 -45
  49. package/dist/translate.js +19 -18
  50. package/dist/update.js +118 -120
  51. package/dist/wire.js +30 -4
  52. package/dist/work.js +27 -2
  53. package/dist/workspace.js +103 -0
  54. package/package.json +26 -22
  55. package/dist/authority-main.js +0 -22
  56. package/dist/bootstrap.json +0 -24
  57. package/dist/chatgpt.js +0 -32
  58. package/dist/clis-main.js +0 -23
  59. package/dist/configure-main.js +0 -6
  60. package/dist/enrol-main.js +0 -10
  61. package/dist/inputs.js +0 -37
  62. package/dist/parts.js +0 -82
  63. package/dist/service-main.js +0 -9
  64. package/dist/service.js +0 -55
  65. package/dist/stream.js +0 -123
  66. package/how-we-work.md +0 -33
  67. package/skills/ask-matt/LICENSE +0 -21
  68. package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
  69. package/skills/ask-matt/SKILL.md +0 -97
  70. package/skills/ask-matt/agents/openai.yaml +0 -5
  71. package/skills/code-review/LICENSE +0 -21
  72. package/skills/code-review/SKILL.md +0 -89
  73. package/skills/code-review/agents/openai.yaml +0 -3
  74. package/skills/codebase-design/DEEPENING.md +0 -37
  75. package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
  76. package/skills/codebase-design/LICENSE +0 -21
  77. package/skills/codebase-design/SKILL.md +0 -114
  78. package/skills/codebase-design/agents/openai.yaml +0 -3
  79. package/skills/diagnosing-bugs/LICENSE +0 -21
  80. package/skills/diagnosing-bugs/SKILL.md +0 -138
  81. package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
  82. package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
  83. package/skills/domain-modeling/ADR-FORMAT.md +0 -47
  84. package/skills/domain-modeling/GLOSSARY-FORMAT.md +0 -60
  85. package/skills/domain-modeling/LICENSE +0 -21
  86. package/skills/domain-modeling/SKILL.md +0 -74
  87. package/skills/domain-modeling/agents/openai.yaml +0 -3
  88. package/skills/grill-me/LICENSE +0 -21
  89. package/skills/grill-me/SKILL.md +0 -7
  90. package/skills/grill-me/agents/openai.yaml +0 -5
  91. package/skills/grill-with-docs/LICENSE +0 -21
  92. package/skills/grill-with-docs/SKILL.md +0 -7
  93. package/skills/grill-with-docs/agents/openai.yaml +0 -5
  94. package/skills/grilling/LICENSE +0 -21
  95. package/skills/grilling/SKILL.md +0 -30
  96. package/skills/grilling/agents/openai.yaml +0 -3
  97. package/skills/handoff/LICENSE +0 -21
  98. package/skills/handoff/SKILL.md +0 -16
  99. package/skills/handoff/agents/openai.yaml +0 -5
  100. package/skills/implement/LICENSE +0 -21
  101. package/skills/implement/SKILL.md +0 -17
  102. package/skills/implement/agents/openai.yaml +0 -5
  103. package/skills/implement-spec/LICENSE +0 -21
  104. package/skills/implement-spec/SKILL.md +0 -40
  105. package/skills/implement-spec/agents/openai.yaml +0 -5
  106. package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
  107. package/skills/improve-codebase-architecture/LICENSE +0 -21
  108. package/skills/improve-codebase-architecture/SKILL.md +0 -71
  109. package/skills/improve-codebase-architecture/agents/openai.yaml +0 -5
  110. package/skills/pr/CREDITS.md +0 -3
  111. package/skills/pr/LICENSE +0 -21
  112. package/skills/pr/SKILL.md +0 -170
  113. package/skills/pr/agents/openai.yaml +0 -3
  114. package/skills/prototype/LICENSE +0 -21
  115. package/skills/prototype/LOGIC.md +0 -67
  116. package/skills/prototype/SKILL.md +0 -26
  117. package/skills/prototype/UI.md +0 -112
  118. package/skills/prototype/agents/openai.yaml +0 -3
  119. package/skills/research/LICENSE +0 -21
  120. package/skills/research/SKILL.md +0 -12
  121. package/skills/research/agents/openai.yaml +0 -3
  122. package/skills/retro/LICENSE +0 -21
  123. package/skills/retro/SKILL.md +0 -44
  124. package/skills/retro/agents/openai.yaml +0 -5
  125. package/skills/tdd/LICENSE +0 -21
  126. package/skills/tdd/SKILL.md +0 -38
  127. package/skills/tdd/agents/openai.yaml +0 -3
  128. package/skills/tdd/mocking.md +0 -59
  129. package/skills/tdd/tests.md +0 -77
  130. package/skills/teach/GLOSSARY-FORMAT.md +0 -35
  131. package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
  132. package/skills/teach/LICENSE +0 -21
  133. package/skills/teach/MISSION-FORMAT.md +0 -31
  134. package/skills/teach/RESOURCES-FORMAT.md +0 -32
  135. package/skills/teach/SKILL.md +0 -140
  136. package/skills/teach/agents/openai.yaml +0 -5
  137. package/skills/to-questionnaire/LICENSE +0 -21
  138. package/skills/to-questionnaire/SKILL.md +0 -54
  139. package/skills/to-questionnaire/agents/openai.yaml +0 -5
  140. package/skills/to-spec/LICENSE +0 -21
  141. package/skills/to-spec/SKILL.md +0 -75
  142. package/skills/to-spec/agents/openai.yaml +0 -5
  143. package/skills/to-tickets/LICENSE +0 -21
  144. package/skills/to-tickets/SKILL.md +0 -105
  145. package/skills/to-tickets/agents/openai.yaml +0 -5
  146. package/skills/triage/AGENT-BRIEF.md +0 -207
  147. package/skills/triage/LICENSE +0 -21
  148. package/skills/triage/OUT-OF-SCOPE.md +0 -105
  149. package/skills/triage/SKILL.md +0 -112
  150. package/skills/triage/agents/openai.yaml +0 -5
  151. package/skills/wait-what/LICENSE +0 -21
  152. package/skills/wait-what/SKILL.md +0 -7
  153. package/skills/wait-what/agents/openai.yaml +0 -5
  154. package/skills/wayfinder/LICENSE +0 -21
  155. package/skills/wayfinder/SKILL.md +0 -128
  156. package/skills/wayfinder/agents/openai.yaml +0 -5
  157. package/skills/wizard/LICENSE +0 -21
  158. package/skills/wizard/SKILL.md +0 -44
  159. package/skills/wizard/agents/openai.yaml +0 -3
  160. package/skills/wizard/template.sh +0 -223
  161. package/skills/writing-for-agents/LICENSE +0 -21
  162. package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
  163. package/skills/writing-for-agents/SKILL.md +0 -81
  164. package/skills/writing-for-agents/agents/openai.yaml +0 -3
@@ -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
+ }
@@ -2,7 +2,7 @@ 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')
@@ -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/sign-in.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { createInterface } from 'node:readline';
3
- import { CLIS, withoutProxy } from './clis.js';
3
+ import { CLIS, cliEnvironment } from './clis.js';
4
4
  import { killTree } from './scope.js';
5
5
  import { holdSecretInput } from './secret-input.js';
6
6
  const ESCAPE = /\u001b\[[\d;]*m/g;
@@ -9,54 +9,100 @@ const CODE = /^[A-Z0-9]+-[A-Z0-9]+$/;
9
9
  function causeOf(error) {
10
10
  return error instanceof Error ? error.message : String(error);
11
11
  }
12
+ function keySteps(name) {
13
+ return [
14
+ { kind: 'collect', label: 'API key', name: 'key' },
15
+ {
16
+ kind: 'collect',
17
+ label: 'Endpoint, for another provider',
18
+ name: 'endpoint',
19
+ description: `Leave Endpoint empty for ${name}’s own. Both stay on this computer.`,
20
+ optional: true,
21
+ },
22
+ ];
23
+ }
24
+ function endpointOf(given) {
25
+ const endpoint = given.trim();
26
+ if (endpoint === '')
27
+ return null;
28
+ let protocol;
29
+ try {
30
+ protocol = new URL(endpoint).protocol;
31
+ }
32
+ catch {
33
+ protocol = '';
34
+ }
35
+ if (protocol !== 'https:' && protocol !== 'http:')
36
+ throw new Error('the endpoint is no http or https address');
37
+ return endpoint;
38
+ }
12
39
  function endOf(signal) {
13
40
  return new Promise((resolve) => signal.addEventListener('abort', () => resolve(true), { once: true }));
14
41
  }
15
42
  export class SignIns {
16
43
  house;
17
44
  agents;
18
- inputs;
19
- constructor(house, agents, inputs) {
45
+ calls;
46
+ constructor(house, agents, calls) {
20
47
  this.house = house;
21
48
  this.agents = agents;
22
- this.inputs = inputs;
49
+ this.calls = calls;
23
50
  }
24
- start(input) {
25
- void this.inputs.carry(input.input_id, () => this.signIn(input));
51
+ start(call) {
52
+ void this.calls.carry(call.input_id, () => this.signIn(call));
26
53
  }
27
- async signIn(input) {
54
+ async signIn(call) {
28
55
  const ending = new AbortController();
29
56
  // 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.
30
- const timer = setTimeout(() => ending.abort(), input.ends_in * 1000);
31
- const ended = await Promise.race([this.signedIn(input, ending.signal), endOf(ending.signal)]);
57
+ const timer = setTimeout(() => ending.abort(), call.ends_in * 1000);
58
+ const ended = await Promise.race([this.signedIn(call, ending.signal), endOf(ending.signal)]);
32
59
  clearTimeout(timer);
33
60
  if (ended)
34
- process.stderr.write(`kit: the ${input.cli} sign-in reached the end House set for it\n`);
35
- return ended ? { ack: {} } : { ack: {}, acknowledged: () => this.agents.reread(input.cli) };
61
+ process.stderr.write(`kit: the ${call.cli} sign-in reached the end House set for it\n`);
62
+ return ended ? { result: {} } : { result: {}, returned: () => this.agents.reread(call.cli) };
36
63
  }
37
- async signedIn(input, signal) {
38
- await this.login(input, signal);
64
+ async signedIn(call, signal) {
65
+ await (call.api_key === true ? this.keyed(call, signal) : this.login(call, signal));
39
66
  if (!signal.aborted)
40
- await this.agents.recheck(input.cli, signal);
67
+ await this.agents.recheck(call.cli, signal);
41
68
  return false;
42
69
  }
43
- async login(input, signal) {
70
+ async keyed(call, signal) {
71
+ const { name, key } = CLIS[call.cli];
72
+ let secret = '';
73
+ try {
74
+ const cli = await this.agents.located(call.cli, signal);
75
+ const given = await holdSecretInput(this.house, call.input_id, keySteps(name), () => !signal.aborted);
76
+ if (given === null || signal.aborted)
77
+ return;
78
+ secret = given.key.trim();
79
+ await key.write(secret, endpointOf(given.endpoint), cli, signal);
80
+ }
81
+ catch (error) {
82
+ const cause = secret === '' ? causeOf(error) : causeOf(error).replaceAll(secret, 'the key');
83
+ if (!signal.aborted)
84
+ process.stderr.write(`kit: the ${call.cli} sign-in failed: ${cause}\n`);
85
+ }
86
+ }
87
+ async login(call, signal) {
44
88
  let cli;
89
+ let env;
45
90
  try {
46
- cli = await this.agents.located(input.cli, signal);
91
+ cli = await this.agents.located(call.cli, signal);
92
+ env = await cliEnvironment(call.cli);
47
93
  }
48
94
  catch (error) {
49
95
  if (!signal.aborted)
50
- process.stderr.write(`kit: the ${input.cli} sign-in failed: ${causeOf(error)}\n`);
96
+ process.stderr.write(`kit: the ${call.cli} sign-in failed: ${causeOf(error)}\n`);
51
97
  return;
52
98
  }
53
99
  if (signal.aborted)
54
100
  return;
55
- const login = CLIS[input.cli].login;
101
+ const login = CLIS[call.cli].login;
56
102
  const child = spawn(cli, login.args, {
57
103
  stdio: ['pipe', 'pipe', 'pipe'],
58
104
  detached: true,
59
- env: withoutProxy(),
105
+ env,
60
106
  });
61
107
  const running = () => child.exitCode === null && child.signalCode === null;
62
108
  const stop = () => killTree(child);
@@ -84,7 +130,7 @@ export class SignIns {
84
130
  return;
85
131
  held = true;
86
132
  const steps = [{ kind: 'visit', label: 'Open this link and sign in', url: link }, step];
87
- void holdSecretInput(this.house, input.input_id, steps, running).then((content) => {
133
+ void holdSecretInput(this.house, call.input_id, steps, running).then((content) => {
88
134
  if (!running())
89
135
  return;
90
136
  if (content === null)
@@ -101,6 +147,6 @@ export class SignIns {
101
147
  });
102
148
  signal.removeEventListener('abort', stop);
103
149
  if (failed !== null && !signal.aborted)
104
- process.stderr.write(`kit: the ${input.cli} sign-in failed: ${failed}\n`);
150
+ process.stderr.write(`kit: the ${call.cli} sign-in failed: ${failed}\n`);
105
151
  }
106
152
  }
@@ -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
+ }