@agentshouse/kit 0.2.1-c5262a8cc063d → 0.2.1-c534eec03292e

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 (136) hide show
  1. package/README.md +44 -43
  2. package/dist/acp.js +102 -108
  3. package/dist/agents.js +217 -60
  4. package/dist/api.js +36 -3
  5. package/dist/app-git.js +48 -0
  6. package/dist/authority-main.js +2 -2
  7. package/dist/bridge.js +40 -59
  8. package/dist/building.js +46 -0
  9. package/dist/calls.js +71 -0
  10. package/dist/chatgpt.js +32 -0
  11. package/dist/clis.js +185 -77
  12. package/dist/codex-main.js +58 -0
  13. package/dist/connection.js +116 -0
  14. package/dist/conversations.js +732 -330
  15. package/dist/copies.js +205 -150
  16. package/dist/end.js +33 -0
  17. package/dist/enrol-main.js +7 -1
  18. package/dist/events.js +546 -0
  19. package/dist/files.js +140 -17
  20. package/dist/git.js +13 -11
  21. package/dist/home.js +89 -7
  22. package/dist/house-cli.js +299 -0
  23. package/dist/house-main.js +7 -159
  24. package/dist/instructions.js +8 -10
  25. package/dist/kit-cli.js +68 -0
  26. package/dist/kit-main.js +8 -19
  27. package/dist/login.js +119 -104
  28. package/dist/logout.js +14 -13
  29. package/dist/mcp.js +8 -3
  30. package/dist/parts.js +82 -0
  31. package/dist/placement.js +39 -0
  32. package/dist/refusals.js +12 -0
  33. package/dist/relay.js +41 -0
  34. package/dist/resident.js +115 -51
  35. package/dist/scope.js +214 -0
  36. package/dist/scripts.js +255 -0
  37. package/dist/secret-input.js +4 -1
  38. package/dist/service-main.js +9 -0
  39. package/dist/service.js +55 -0
  40. package/dist/shell.js +3 -0
  41. package/dist/sign-in.js +40 -32
  42. package/dist/silence.js +28 -0
  43. package/dist/skills.js +49 -35
  44. package/dist/transcription.js +129 -41
  45. package/dist/translate.js +2 -8
  46. package/dist/update.js +117 -54
  47. package/dist/wire.js +22 -0
  48. package/dist/work.js +346 -0
  49. package/package.json +20 -20
  50. package/dist/blocks.js +0 -51
  51. package/dist/bootstrap.json +0 -24
  52. package/dist/clis-main.js +0 -23
  53. package/dist/stream.js +0 -46
  54. package/how-we-work.md +0 -23
  55. package/skills/ask-matt/LICENSE +0 -21
  56. package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
  57. package/skills/ask-matt/SKILL.md +0 -91
  58. package/skills/ask-matt/agents/openai.yaml +0 -4
  59. package/skills/code-review/LICENSE +0 -21
  60. package/skills/code-review/SKILL.md +0 -89
  61. package/skills/code-review/agents/openai.yaml +0 -3
  62. package/skills/codebase-design/DEEPENING.md +0 -37
  63. package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
  64. package/skills/codebase-design/LICENSE +0 -21
  65. package/skills/codebase-design/SKILL.md +0 -116
  66. package/skills/codebase-design/agents/openai.yaml +0 -3
  67. package/skills/diagnosing-bugs/LICENSE +0 -21
  68. package/skills/diagnosing-bugs/SKILL.md +0 -140
  69. package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
  70. package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
  71. package/skills/domain-modeling/ADR-FORMAT.md +0 -47
  72. package/skills/domain-modeling/CONTEXT-FORMAT.md +0 -60
  73. package/skills/domain-modeling/LICENSE +0 -21
  74. package/skills/domain-modeling/SKILL.md +0 -76
  75. package/skills/domain-modeling/agents/openai.yaml +0 -3
  76. package/skills/grill-me/LICENSE +0 -21
  77. package/skills/grill-me/SKILL.md +0 -8
  78. package/skills/grill-me/agents/openai.yaml +0 -4
  79. package/skills/grill-with-docs/LICENSE +0 -21
  80. package/skills/grill-with-docs/SKILL.md +0 -8
  81. package/skills/grill-with-docs/agents/openai.yaml +0 -4
  82. package/skills/grilling/LICENSE +0 -21
  83. package/skills/grilling/SKILL.md +0 -30
  84. package/skills/grilling/agents/openai.yaml +0 -3
  85. package/skills/handoff/LICENSE +0 -21
  86. package/skills/handoff/SKILL.md +0 -17
  87. package/skills/handoff/agents/openai.yaml +0 -4
  88. package/skills/implement/LICENSE +0 -21
  89. package/skills/implement/SKILL.md +0 -16
  90. package/skills/implement/agents/openai.yaml +0 -4
  91. package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
  92. package/skills/improve-codebase-architecture/LICENSE +0 -21
  93. package/skills/improve-codebase-architecture/SKILL.md +0 -72
  94. package/skills/improve-codebase-architecture/agents/openai.yaml +0 -4
  95. package/skills/prototype/LICENSE +0 -21
  96. package/skills/prototype/LOGIC.md +0 -67
  97. package/skills/prototype/SKILL.md +0 -28
  98. package/skills/prototype/UI.md +0 -112
  99. package/skills/prototype/agents/openai.yaml +0 -3
  100. package/skills/research/LICENSE +0 -21
  101. package/skills/research/SKILL.md +0 -14
  102. package/skills/research/agents/openai.yaml +0 -3
  103. package/skills/resolving-merge-conflicts/LICENSE +0 -21
  104. package/skills/resolving-merge-conflicts/SKILL.md +0 -16
  105. package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
  106. package/skills/tdd/LICENSE +0 -21
  107. package/skills/tdd/SKILL.md +0 -40
  108. package/skills/tdd/agents/openai.yaml +0 -3
  109. package/skills/tdd/mocking.md +0 -59
  110. package/skills/tdd/tests.md +0 -77
  111. package/skills/teach/GLOSSARY-FORMAT.md +0 -35
  112. package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
  113. package/skills/teach/LICENSE +0 -21
  114. package/skills/teach/MISSION-FORMAT.md +0 -31
  115. package/skills/teach/RESOURCES-FORMAT.md +0 -32
  116. package/skills/teach/SKILL.md +0 -141
  117. package/skills/teach/agents/openai.yaml +0 -4
  118. package/skills/to-questionnaire/LICENSE +0 -21
  119. package/skills/to-questionnaire/SKILL.md +0 -55
  120. package/skills/to-questionnaire/agents/openai.yaml +0 -4
  121. package/skills/to-spec/LICENSE +0 -21
  122. package/skills/to-spec/SKILL.md +0 -76
  123. package/skills/to-spec/agents/openai.yaml +0 -4
  124. package/skills/to-tickets/LICENSE +0 -21
  125. package/skills/to-tickets/SKILL.md +0 -106
  126. package/skills/to-tickets/agents/openai.yaml +0 -4
  127. package/skills/wait-what/LICENSE +0 -21
  128. package/skills/wait-what/SKILL.md +0 -8
  129. package/skills/wait-what/agents/openai.yaml +0 -4
  130. package/skills/wayfinder/LICENSE +0 -21
  131. package/skills/wayfinder/SKILL.md +0 -129
  132. package/skills/wayfinder/agents/openai.yaml +0 -4
  133. package/skills/writing-for-agents/LICENSE +0 -21
  134. package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
  135. package/skills/writing-for-agents/SKILL.md +0 -83
  136. package/skills/writing-for-agents/agents/openai.yaml +0 -3
package/dist/agents.js CHANGED
@@ -1,15 +1,29 @@
1
1
  import { client } from '@agentclientprotocol/sdk';
2
- import { readFile, realpath } from 'node:fs/promises';
3
- import { killTree, startAdapter } from './acp.js';
4
- import { CLIS, KIT_VERSION, below, installAdapter, installCli, locate, run, versionOf } from './clis.js';
2
+ import { readFile } from 'node:fs/promises';
3
+ import { setTimeout as delay } from 'node:timers/promises';
4
+ import { signedOut, startAdapter } from './acp.js';
5
+ import { CLIS, KIT_RELEASE, below, installAdapter, installCli, locate, run, updateCli, versionOf } from './clis.js';
5
6
  import { agentBase, kitHome } from './home.js';
6
- const SIGNED_OUT = { signed_in: false, models: [] };
7
+ import { updatesItself } from './placement.js';
8
+ import { holdTranscription, transcriptionReport } from './transcription.js';
9
+ import { KIT_WIRE } from './wire.js';
10
+ // Two quiet seconds let a fresh conversation start before background model discovery competes for its CPU.
11
+ const MODEL_PROBE_QUIET_MS = 2_000;
12
+ // A CLI's maker can publish several releases a day, so running each chosen CLI's update command hourly brings a new one to the next conversation within the hour for one small check.
13
+ export const CLI_UPDATE_MS = 60 * 60_000;
14
+ // A sign-in's model read that has not answered in ten seconds leaves its sign-in to the status check, so a hung CLI never holds the sign-in's result.
15
+ const SIGNED_IN_READ_MS = 10_000;
16
+ const SIGNED_OUT = { signed_in: false, models: [], modes: [] };
17
+ const INSTALLING = { release: null, failure: null, installing: true, ...SIGNED_OUT };
7
18
  function select(options, category) {
8
19
  const option = options.find((candidate) => candidate.category === category);
9
20
  return option?.type === 'select' ? option : undefined;
10
21
  }
22
+ function choices(options) {
23
+ return options.flatMap((entry) => ('group' in entry ? entry.options : [entry]));
24
+ }
11
25
  function values(options) {
12
- return options.flatMap((entry) => ('group' in entry ? entry.options : [entry])).map((entry) => entry.value);
26
+ return choices(options).map((entry) => entry.value);
13
27
  }
14
28
  function causeOf(error) {
15
29
  return error instanceof Error ? error.message : String(error);
@@ -17,13 +31,31 @@ function causeOf(error) {
17
31
  function logged(error) {
18
32
  process.stderr.write(`kit: ${causeOf(error)}\n`);
19
33
  }
34
+ const LAPTOP_CHASSIS = new Set(['8', '9', '10', '14', '30', '31', '32']);
35
+ const DESKTOP_CHASSIS = new Set(['3', '4', '5', '6', '7', '15', '16']);
36
+ const SERVER_CHASSIS = new Set(['17', '18', '19', '20', '21', '22', '23', '24']);
37
+ export function chassisForm(value) {
38
+ const chassis = value.trim();
39
+ if (LAPTOP_CHASSIS.has(chassis))
40
+ return 'laptop';
41
+ if (DESKTOP_CHASSIS.has(chassis))
42
+ return 'desktop';
43
+ if (SERVER_CHASSIS.has(chassis))
44
+ return 'server';
45
+ return undefined;
46
+ }
47
+ export function hostVm(version) {
48
+ return /docker desktop|microsoft|wsl|linuxkit/i.test(version);
49
+ }
20
50
  async function models(kind, adapter, known) {
21
51
  const agent = adapter.connection.agent;
22
52
  const opened = await agent.request('session/new', { cwd: kitHome(), mcpServers: [] });
23
53
  let options = opened.configOptions ?? [];
54
+ const mode = select(options, 'mode');
55
+ const modes = mode === undefined ? [] : choices(mode.options).map(({ value, name }) => ({ mode: value, name }));
24
56
  const model = select(options, 'model');
25
57
  if (model === undefined)
26
- return [];
58
+ return { models: [], modes };
27
59
  const offered = [];
28
60
  for (const value of values(model.options)) {
29
61
  if (select(options, 'model')?.currentValue !== value) {
@@ -39,67 +71,111 @@ async function models(kind, adapter, known) {
39
71
  const effort = select(options, 'thought_level');
40
72
  offered.push({ model: value, efforts: effort === undefined ? [] : values(effort.options) });
41
73
  }
42
- return offered;
74
+ return { models: offered, modes };
43
75
  }
44
- async function offer(kind, cli, known) {
76
+ function ended(signal) {
77
+ return new Promise((_, reject) => {
78
+ if (signal.aborted)
79
+ reject(signal.reason);
80
+ signal.addEventListener('abort', () => reject(signal.reason), { once: true });
81
+ });
82
+ }
83
+ async function offer(kind, cli, known, signal) {
45
84
  const probe = CLIS[kind].signedIn;
46
- if ('command' in probe && (await run(cli, probe.command, 30_000)).status !== 0)
85
+ // A sign-in check that has not answered in half a minute counts as signed out rather than stalling the report.
86
+ if ('command' in probe && (await run(cli, probe.command, 30_000, false, signal)).status !== 0)
47
87
  return SIGNED_OUT;
48
88
  let adapter;
49
89
  try {
50
- adapter = await startAdapter(kind, cli, kitHome(), client({ name: '@agentshouse/kit' }));
90
+ adapter = await startAdapter(kind, cli, kitHome(), client({ name: '@agentshouse/kit' }), () => undefined, {}, signal);
91
+ signal?.throwIfAborted();
51
92
  if ('initializeMeta' in probe) {
52
93
  const method = adapter.initialized._meta?.[probe.initializeMeta];
53
94
  if (typeof method !== 'string' || method.length === 0)
54
95
  return SIGNED_OUT;
55
96
  }
56
- return { signed_in: true, models: await models(kind, adapter, known.models) };
97
+ return { signed_in: true, ...(await models(kind, adapter, known.models)) };
57
98
  }
58
99
  catch (error) {
59
- process.stderr.write(`kit: ${kind} did not offer its models: ${causeOf(error)}\n`);
60
- return { signed_in: adapter === undefined && 'initializeMeta' in probe ? known.signed_in : true, models: known.models };
100
+ if (!signal?.aborted)
101
+ process.stderr.write(`kit: ${kind} did not offer its models: ${causeOf(error)}\n`);
102
+ if (signedOut(error))
103
+ return SIGNED_OUT;
104
+ return {
105
+ signed_in: adapter === undefined && 'initializeMeta' in probe ? known.signed_in : true,
106
+ models: known.models,
107
+ modes: known.modes,
108
+ };
61
109
  }
62
110
  finally {
63
- if (adapter !== undefined)
64
- killTree(adapter.child);
111
+ adapter?.retire();
65
112
  }
66
113
  }
67
114
  function reported(kind, reading) {
115
+ const { minimum, update } = CLIS[kind];
68
116
  return {
69
117
  kind,
70
118
  release: reading.release,
71
- ...(reading.minimum === undefined ? {} : { minimum: reading.minimum }),
119
+ ...(reading.release !== null && below(reading.release, minimum) ? { minimum, update } : {}),
72
120
  failure: reading.failure,
121
+ ...(reading.installing === undefined ? {} : { installing: reading.installing }),
73
122
  signed_in: reading.signed_in,
74
123
  models: reading.models,
124
+ modes: reading.modes,
125
+ full_access: CLIS[kind].fullAccess,
75
126
  };
76
127
  }
77
- function tooOld(kind, release) {
78
- const cli = CLIS[kind];
79
- return `${cli.bin} ${release} is older than ${cli.minimum}, the oldest this Kit runs`;
80
- }
128
+ const NOT_READY = "the Agent's CLI is not ready on this computer";
81
129
  async function sshHostKey() {
130
+ // Listing a local host key takes a moment, so ten seconds only bounds a stuck ssh-keygen.
82
131
  const listed = await run('ssh-keygen', ['-l', '-E', 'sha256', '-f', '/etc/ssh/ssh_host_ed25519_key.pub'], 10_000);
83
132
  return listed.status === 0 ? (/^\d+ (SHA256:\S+)/.exec(listed.output)?.[1] ?? null) : null;
84
133
  }
85
134
  async function operatingSystem() {
135
+ // sw_vers answers at once, so ten seconds only bounds a stuck call.
86
136
  if (process.platform === 'darwin')
87
137
  return `macOS ${(await run('sw_vers', ['-productVersion'], 10_000)).output.trim()}`;
88
138
  const release = await readFile('/etc/os-release', 'utf8');
89
139
  return /^PRETTY_NAME="?([^"\n]*)"?$/m.exec(release)?.[1] ?? release;
90
140
  }
141
+ async function hostForm() {
142
+ if (process.platform === 'darwin') {
143
+ const battery = await run('pmset', ['-g', 'batt'], 10_000);
144
+ return battery.status === 0 && battery.output.includes('InternalBattery') ? 'laptop' : 'desktop';
145
+ }
146
+ if (process.platform !== 'linux')
147
+ return undefined;
148
+ const [container, version, chassis] = await Promise.all([
149
+ readFile('/.dockerenv', 'utf8').then(() => true).catch(() => false),
150
+ readFile('/proc/version', 'utf8').catch(() => ''),
151
+ readFile('/sys/class/dmi/id/chassis_type', 'utf8').catch(() => ''),
152
+ ]);
153
+ if (container && hostVm(version))
154
+ return undefined;
155
+ return chassisForm(chassis);
156
+ }
91
157
  export class Agents {
92
- desired = { agents: [], routes: [] };
158
+ desired = { agents: [], routes: [], transcribe: false };
93
159
  house;
94
160
  reading = Promise.resolve();
95
161
  queues = new Map();
96
162
  installing = Promise.resolve();
163
+ installs = new Map();
97
164
  readings = new Map();
98
165
  reads = new Map();
166
+ starts = 0;
99
167
  sending = Promise.resolve();
100
168
  sent = null;
101
- constructor(house) {
169
+ refreshing = 0;
170
+ loaded = Promise.resolve();
171
+ updating = null;
172
+ changed;
173
+ constructor(house, changed = () => undefined) {
102
174
  this.house = house;
175
+ this.changed = changed;
176
+ }
177
+ idle() {
178
+ return this.refreshing === 0 && this.updating === null;
103
179
  }
104
180
  read() {
105
181
  return this.reading.then(() => undefined, () => undefined);
@@ -108,32 +184,49 @@ export class Agents {
108
184
  return this.desired.routes.find((route) => route.agent_id === agentId);
109
185
  }
110
186
  refresh() {
187
+ this.refreshing++;
188
+ this.changed();
111
189
  const reading = this.read().then(async () => {
112
190
  this.desired = await this.house.deliver('/kit/agents/desired', {});
113
191
  return this.desired;
114
192
  });
115
193
  this.reading = reading;
116
- return reading.then(({ agents }) => {
117
- Promise.all(agents.map((kind) => this.queue(kind, () => this.load(kind))))
194
+ this.loaded = reading
195
+ .then(({ agents, transcribe }) => Promise.all([
196
+ holdTranscription(transcribe)
197
+ .then((changed) => (changed ? this.report() : undefined))
198
+ .catch(logged),
199
+ Promise.all(agents.map((kind) => this.load(kind)))
118
200
  .then(() => this.report())
119
- .catch(logged);
201
+ .catch(logged),
202
+ ]))
203
+ .catch(() => undefined)
204
+ .finally(() => {
205
+ this.refreshing--;
206
+ this.changed();
120
207
  });
208
+ return reading.then(() => undefined);
209
+ }
210
+ keepCurrent() {
211
+ this.updateAll(this.loaded);
212
+ setInterval(() => this.updateAll(Promise.resolve()), CLI_UPDATE_MS);
121
213
  }
122
214
  cli(kind) {
215
+ this.starts += 1;
123
216
  return this.queue(kind, async () => {
124
217
  const resolved = await this.resolve(kind);
125
218
  if (!('cli' in resolved)) {
126
219
  this.update(kind, resolved);
127
- throw new Error(resolved.failure ?? tooOld(kind, resolved.release));
220
+ throw new Error(NOT_READY);
128
221
  }
129
222
  const { cli, release } = resolved;
130
223
  const known = this.readings.get(kind);
131
- const kept = known?.release === null || known?.minimum !== undefined ? SIGNED_OUT : (known ?? SIGNED_OUT);
132
- if (known?.release !== release || known.minimum !== undefined) {
133
- this.update(kind, { release, failure: null, signed_in: kept.signed_in, models: kept.models });
224
+ const kept = known?.release === null ? SIGNED_OUT : (known ?? SIGNED_OUT);
225
+ if (known?.release !== release || this.installs.has(kind)) {
226
+ this.update(kind, { release, failure: null, signed_in: kept.signed_in, models: kept.models, modes: kept.modes });
134
227
  }
135
228
  const read = this.nextRead(kind);
136
- offer(kind, cli, kept)
229
+ this.quietOffer(kind, cli, kept)
137
230
  .then((offered) => {
138
231
  const current = this.readings.get(kind);
139
232
  if (this.reads.get(kind) === read && current?.release === release)
@@ -143,16 +236,26 @@ export class Agents {
143
236
  return cli;
144
237
  });
145
238
  }
146
- located(kind) {
147
- return this.queue(kind, () => this.ensure(kind));
239
+ located(kind, signal) {
240
+ const owner = {};
241
+ return this.queue(kind, () => this.ensure(kind, signal, owner).finally(() => this.settle(kind, owner)), signal);
148
242
  }
149
243
  async reread(kind) {
150
- await this.queue(kind, async () => {
151
- this.nextRead(kind);
152
- this.readings.set(kind, await this.examine(kind, this.readings.get(kind) ?? SIGNED_OUT));
153
- });
244
+ await this.examine(kind, this.readings.get(kind) ?? SIGNED_OUT, offer);
154
245
  await this.report();
155
246
  }
247
+ async recheck(kind, signal) {
248
+ const known = this.readings.get(kind) ?? SIGNED_OUT;
249
+ const owner = {};
250
+ try {
251
+ await this.examine(kind, known, (kind, cli) => offer(kind, cli, known, AbortSignal.any([signal, AbortSignal.timeout(SIGNED_IN_READ_MS)])), signal, owner);
252
+ }
253
+ finally {
254
+ this.settle(kind, owner);
255
+ }
256
+ if (!signal.aborted)
257
+ await this.report();
258
+ }
156
259
  report() {
157
260
  const sending = this.sending.catch(() => undefined).then(() => this.send());
158
261
  this.sending = sending;
@@ -163,74 +266,128 @@ export class Agents {
163
266
  this.reads.set(kind, read);
164
267
  return read;
165
268
  }
166
- update(kind, reading) {
269
+ hold(kind, reading) {
167
270
  this.readings.set(kind, reading);
271
+ this.installs.delete(kind);
272
+ }
273
+ update(kind, reading) {
274
+ this.hold(kind, reading);
275
+ this.report().catch(logged);
276
+ }
277
+ settle(kind, owner) {
278
+ if (this.installs.get(kind) !== owner)
279
+ return;
280
+ this.installs.delete(kind);
168
281
  this.report().catch(logged);
169
282
  }
170
- queue(kind, work) {
171
- const queued = (this.queues.get(kind) ?? Promise.resolve()).then(work);
283
+ queue(kind, work, signal) {
284
+ const queued = (this.queues.get(kind) ?? Promise.resolve()).then(() => {
285
+ signal?.throwIfAborted();
286
+ return work();
287
+ });
172
288
  this.queues.set(kind, queued.catch(() => undefined));
173
- return queued;
289
+ return signal === undefined ? queued : Promise.race([queued, ended(signal)]);
174
290
  }
175
- async ensure(kind) {
176
- const failed = await installAdapter(kind);
291
+ async ensure(kind, signal, owner = {}) {
292
+ const failed = await installAdapter(kind, signal);
177
293
  if (failed !== null)
178
294
  throw new Error(failed);
179
- const found = await locate(kind);
295
+ const found = await locate(kind, signal);
180
296
  if (found !== null)
181
297
  return found;
182
- const installing = this.installing.catch(() => undefined).then(() => installCli(kind));
298
+ this.installs.set(kind, owner);
299
+ this.report().catch(logged);
300
+ const installing = this.installing.catch(() => undefined).then(() => installCli(kind, signal));
183
301
  this.installing = installing;
184
302
  const failure = await installing;
185
303
  if (failure !== null)
186
304
  throw new Error(failure);
187
- const installed = await locate(kind);
305
+ const installed = await locate(kind, signal);
188
306
  if (installed === null)
189
307
  throw new Error(`the login shell does not find ${CLIS[kind].bin} after its install`);
190
308
  return installed;
191
309
  }
192
- async resolve(kind) {
310
+ async resolve(kind, signal, owner) {
193
311
  let cli;
194
312
  try {
195
- cli = await realpath(await this.ensure(kind));
313
+ cli = await this.ensure(kind, signal, owner);
196
314
  }
197
315
  catch (error) {
198
316
  const failure = causeOf(error);
199
317
  process.stderr.write(`kit: ${kind} did not install: ${failure}\n`);
200
318
  return { release: null, failure, ...SIGNED_OUT };
201
319
  }
202
- const release = await versionOf(cli);
320
+ const release = await versionOf(cli, signal);
203
321
  if (release === null)
204
322
  return { release: null, failure: `${cli} --version names no version`, ...SIGNED_OUT };
205
- const minimum = CLIS[kind].minimum;
206
- if (below(release, minimum))
207
- return { release, minimum, failure: null, ...SIGNED_OUT };
208
323
  return { cli, release };
209
324
  }
210
- async examine(kind, known) {
211
- const resolved = await this.resolve(kind);
212
- if (!('cli' in resolved))
213
- return resolved;
214
- return { release: resolved.release, failure: null, ...(await offer(kind, resolved.cli, known)) };
325
+ async examine(kind, known, read, signal, owner) {
326
+ const current = this.nextRead(kind);
327
+ const resolved = await this.queue(kind, () => this.resolve(kind, signal, owner), signal);
328
+ const reading = 'cli' in resolved ? { release: resolved.release, failure: null, ...(await read(kind, resolved.cli, known)) } : resolved;
329
+ if (this.reads.get(kind) === current && !signal?.aborted)
330
+ this.hold(kind, reading);
331
+ }
332
+ updateAll(after) {
333
+ if (this.updating !== null)
334
+ return;
335
+ this.updating = after
336
+ .then(async () => {
337
+ for (const kind of this.desired.agents) {
338
+ if (this.installs.has(kind) || !this.readings.get(kind)?.release)
339
+ continue;
340
+ const failure = await updateCli(kind);
341
+ if (failure !== null)
342
+ process.stderr.write(`kit: ${kind} did not update: ${failure}\n`);
343
+ await this.readVersion(kind);
344
+ }
345
+ })
346
+ .catch(logged)
347
+ .finally(() => {
348
+ this.updating = null;
349
+ this.changed();
350
+ });
351
+ this.changed();
352
+ }
353
+ async readVersion(kind) {
354
+ const cli = await locate(kind);
355
+ const release = cli === null ? null : await versionOf(cli);
356
+ const current = this.readings.get(kind);
357
+ if (release === null || !current?.release || current.release === release)
358
+ return;
359
+ this.update(kind, { ...current, release });
360
+ }
361
+ async quietOffer(kind, cli, known) {
362
+ for (let seen = -1; seen !== this.starts;) {
363
+ seen = this.starts;
364
+ await delay(MODEL_PROBE_QUIET_MS);
365
+ }
366
+ return offer(kind, cli, known);
215
367
  }
216
368
  async load(kind) {
217
369
  const known = this.readings.get(kind);
218
370
  if (known !== undefined && known.failure === null)
219
371
  return;
220
- this.nextRead(kind);
221
- this.readings.set(kind, await this.examine(kind, SIGNED_OUT));
372
+ await this.examine(kind, SIGNED_OUT, (kind, cli, known) => this.quietOffer(kind, cli, known));
373
+ await this.report();
222
374
  }
223
375
  async send() {
224
376
  const agents = this.desired.agents.flatMap((kind) => {
225
- const reading = this.readings.get(kind);
377
+ const reading = this.installs.has(kind) ? INSTALLING : this.readings.get(kind);
226
378
  return reading === undefined ? [] : [reported(kind, reading)];
227
379
  });
228
380
  const hostKey = await sshHostKey();
381
+ const form = await hostForm();
229
382
  const body = {
230
383
  os: await operatingSystem(),
231
- kit_version: KIT_VERSION,
384
+ ...(form === undefined ? {} : { form }),
385
+ kit_release: KIT_RELEASE,
386
+ wire: KIT_WIRE,
387
+ updates_itself: await updatesItself(),
232
388
  agent_base: agentBase(),
233
389
  agents,
390
+ transcription: transcriptionReport(),
234
391
  ...(hostKey === null ? {} : { ssh_host_key: hostKey }),
235
392
  };
236
393
  const state = JSON.stringify(body);
package/dist/api.js CHANGED
@@ -1,4 +1,9 @@
1
+ import { setTimeout as delay } from 'node:timers/promises';
2
+ import { signInAgain } from './end.js';
1
3
  import { readEnrolment } from './home.js';
4
+ import { retryable } from './refusals.js';
5
+ import { relayed } from './relay.js';
6
+ import { endUnserved, KIT_WIRE, KIT_WIRE_HEADER } from './wire.js';
2
7
  export class HouseRefusal extends Error {
3
8
  status;
4
9
  text;
@@ -8,27 +13,54 @@ export class HouseRefusal extends Error {
8
13
  this.text = text;
9
14
  }
10
15
  }
16
+ export function finalRefusal(error) {
17
+ return error instanceof HouseRefusal && retryable(error.text) === false;
18
+ }
11
19
  export function retryDelay(attempt) {
20
+ // Full jitter, doubling from one second to a 30-second cap, spreads the Kit processes one outage dropped and retries within half a minute of House's return.
12
21
  return Math.random() * Math.min(30_000, 1000 * 2 ** attempt);
13
22
  }
23
+ export async function retrying(sent, signal, pause = retryDelay) {
24
+ for (let attempt = 0;; attempt++) {
25
+ try {
26
+ return await sent();
27
+ }
28
+ catch (error) {
29
+ if (!(error instanceof HouseRefusal) || retryable(error.text) !== true)
30
+ throw error;
31
+ }
32
+ // A request House refused as retryable is sent again after the caller's pause, the jittered curve unless it names its own.
33
+ await delay(pause(attempt), undefined, { signal });
34
+ }
35
+ }
14
36
  export async function post(house, path, body, credential, signal) {
15
- const answer = await fetch(new URL(path, house), {
37
+ const answer = await fetch(relayed(new URL(path, house)), {
16
38
  method: 'POST',
17
39
  headers: {
18
40
  'content-type': 'application/json',
41
+ [KIT_WIRE_HEADER]: String(KIT_WIRE),
19
42
  ...(credential === undefined ? {} : { authorization: `Bearer ${credential}` }),
20
43
  },
21
44
  body: JSON.stringify(body),
22
45
  signal,
23
46
  });
24
47
  const text = await answer.text();
48
+ if (!answer.ok)
49
+ endUnserved(text);
25
50
  if (!answer.ok)
26
51
  throw new HouseRefusal(path, answer.status, text);
27
52
  return JSON.parse(text);
28
53
  }
29
54
  async function enrolledPost(path, body, signal) {
30
55
  const { house, credential } = (await readEnrolment());
31
- return post(house, path, body, credential, signal);
56
+ try {
57
+ return await post(house, path, body, credential, signal);
58
+ }
59
+ catch (error) {
60
+ if (error instanceof HouseRefusal && error.status === 401)
61
+ await signInAgain(credential);
62
+ throw error;
63
+ }
32
64
  }
33
65
  export const house = {
34
66
  post: enrolledPost,
@@ -38,9 +70,10 @@ export const house = {
38
70
  return await enrolledPost(path, body);
39
71
  }
40
72
  catch (error) {
41
- if (error instanceof HouseRefusal && error.status < 500)
73
+ if (finalRefusal(error))
42
74
  throw error;
43
75
  process.stderr.write(`kit: ${path} did not reach House: ${error.message}\n`);
76
+ // A delivery House did not take is sent again on the jittered curve for as long as it is still current.
44
77
  await new Promise((resolve) => setTimeout(resolve, retryDelay(attempt)));
45
78
  if (!current())
46
79
  throw error;
@@ -0,0 +1,48 @@
1
+ import { run } from './git.js';
2
+ import { kitHome, readOwnAgent } from './home.js';
3
+ const HELPER = '!kit git-credential';
4
+ function section(house) {
5
+ return `credential.${new URL('/app/', house).href}`;
6
+ }
7
+ async function configured(args) {
8
+ try {
9
+ return await run(kitHome(), ['config', '--global', ...args]);
10
+ }
11
+ catch (error) {
12
+ if (error.code === 'ENOENT')
13
+ return null;
14
+ throw error;
15
+ }
16
+ }
17
+ export async function placeAppGit(ownAgent) {
18
+ if (ownAgent === null)
19
+ return;
20
+ await configured(['--remove-section', section(ownAgent.house)]);
21
+ for (const value of ['', HELPER]) {
22
+ const ran = await configured(['--add', `${section(ownAgent.house)}.helper`, value]);
23
+ if (ran === null)
24
+ return;
25
+ if (ran.status !== 0)
26
+ throw new Error(`git config failed: ${ran.stderr.trim()}`);
27
+ }
28
+ }
29
+ export async function answerGitCredential(operation) {
30
+ let asked = '';
31
+ for await (const chunk of process.stdin)
32
+ asked += String(chunk);
33
+ if (operation !== 'get')
34
+ return;
35
+ const attributes = new Map(asked
36
+ .split('\n')
37
+ .filter((line) => line.includes('='))
38
+ .map((line) => [line.slice(0, line.indexOf('=')), line.slice(line.indexOf('=') + 1)]));
39
+ const ownAgent = await readOwnAgent();
40
+ if (ownAgent === null) {
41
+ process.stderr.write(`kit: Kit home ${kitHome()} is not signed in; run kit login\n`);
42
+ return;
43
+ }
44
+ const house = new URL(ownAgent.house);
45
+ if (attributes.get('protocol') !== house.protocol.slice(0, -1) || attributes.get('host') !== house.host)
46
+ return;
47
+ process.stdout.write(`username=house\npassword=${ownAgent.credential}\n`);
48
+ }
@@ -1,10 +1,10 @@
1
- import { setGlobalProxyFromEnv } from 'node:http';
2
1
  import { HouseRefusal, post } from './api.js';
3
2
  import { readEnrolment } from './home.js';
3
+ import { proxyUnlessRelayed } from './relay.js';
4
4
  function unanswered(error) {
5
5
  return error instanceof HouseRefusal ? error.status >= 500 : error instanceof TypeError && error.message === 'fetch failed';
6
6
  }
7
- setGlobalProxyFromEnv();
7
+ proxyUnlessRelayed();
8
8
  try {
9
9
  const { house, credential } = (await readEnrolment());
10
10
  try {