@agentshouse/kit 0.2.1-cb6679d048e60 → 0.2.1-cb8dc89b78e07

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 (104) hide show
  1. package/README.md +32 -31
  2. package/dist/acp.js +46 -103
  3. package/dist/agents.js +178 -49
  4. package/dist/api.js +36 -3
  5. package/dist/app-git.js +48 -0
  6. package/dist/bootstrap.json +4 -4
  7. package/dist/bridge.js +39 -58
  8. package/dist/chatgpt.js +32 -0
  9. package/dist/clis.js +137 -65
  10. package/dist/codex-main.js +58 -0
  11. package/dist/conversations.js +525 -287
  12. package/dist/copies.js +202 -147
  13. package/dist/end.js +27 -0
  14. package/dist/enrol-main.js +7 -1
  15. package/dist/events.js +525 -0
  16. package/dist/files.js +24 -3
  17. package/dist/git.js +10 -8
  18. package/dist/home.js +96 -7
  19. package/dist/house-cli.js +291 -0
  20. package/dist/house-main.js +7 -159
  21. package/dist/inputs.js +63 -0
  22. package/dist/instructions.js +10 -32
  23. package/dist/kit-cli.js +68 -0
  24. package/dist/kit-main.js +8 -19
  25. package/dist/login.js +118 -103
  26. package/dist/logout.js +14 -13
  27. package/dist/mcp.js +2 -1
  28. package/dist/parts.js +82 -0
  29. package/dist/placement.js +39 -0
  30. package/dist/refusals.js +12 -0
  31. package/dist/relay.js +20 -0
  32. package/dist/resident.js +91 -35
  33. package/dist/scope.js +206 -0
  34. package/dist/secret-input.js +4 -1
  35. package/dist/service-main.js +9 -0
  36. package/dist/service.js +55 -0
  37. package/dist/shell.js +3 -0
  38. package/dist/sign-in.js +34 -27
  39. package/dist/skills.js +81 -12
  40. package/dist/stream.js +89 -12
  41. package/dist/transcription.js +57 -9
  42. package/dist/translate.js +2 -8
  43. package/dist/update.js +50 -29
  44. package/dist/wire.js +22 -0
  45. package/dist/work.js +346 -0
  46. package/how-we-work.md +13 -3
  47. package/package.json +3 -2
  48. package/skills/ask-matt/SKILL.md +17 -11
  49. package/skills/ask-matt/agents/openai.yaml +1 -0
  50. package/skills/code-review/SKILL.md +5 -5
  51. package/skills/codebase-design/DESIGN-IT-TWICE.md +1 -1
  52. package/skills/codebase-design/SKILL.md +0 -2
  53. package/skills/diagnosing-bugs/SKILL.md +2 -4
  54. package/skills/domain-modeling/{CONTEXT-FORMAT.md → GLOSSARY-FORMAT.md} +10 -10
  55. package/skills/domain-modeling/SKILL.md +12 -14
  56. package/skills/grill-me/SKILL.md +1 -2
  57. package/skills/grill-me/agents/openai.yaml +1 -0
  58. package/skills/grill-with-docs/SKILL.md +1 -2
  59. package/skills/grill-with-docs/agents/openai.yaml +1 -0
  60. package/skills/grilling/SKILL.md +2 -2
  61. package/skills/handoff/SKILL.md +2 -3
  62. package/skills/handoff/agents/openai.yaml +1 -0
  63. package/skills/implement/SKILL.md +5 -4
  64. package/skills/implement/agents/openai.yaml +1 -0
  65. package/skills/implement-spec/SKILL.md +40 -0
  66. package/skills/implement-spec/agents/openai.yaml +5 -0
  67. package/skills/improve-codebase-architecture/SKILL.md +6 -7
  68. package/skills/improve-codebase-architecture/agents/openai.yaml +1 -0
  69. package/skills/pr/CREDITS.md +3 -0
  70. package/skills/pr/LICENSE +21 -0
  71. package/skills/pr/SKILL.md +170 -0
  72. package/skills/pr/agents/openai.yaml +3 -0
  73. package/skills/prototype/SKILL.md +0 -2
  74. package/skills/research/SKILL.md +0 -2
  75. package/skills/retro/LICENSE +21 -0
  76. package/skills/retro/SKILL.md +44 -0
  77. package/skills/retro/agents/openai.yaml +5 -0
  78. package/skills/tdd/SKILL.md +2 -4
  79. package/skills/teach/SKILL.md +3 -4
  80. package/skills/teach/agents/openai.yaml +1 -0
  81. package/skills/to-questionnaire/SKILL.md +1 -2
  82. package/skills/to-questionnaire/agents/openai.yaml +1 -0
  83. package/skills/to-spec/SKILL.md +1 -2
  84. package/skills/to-spec/agents/openai.yaml +1 -0
  85. package/skills/to-tickets/SKILL.md +3 -4
  86. package/skills/to-tickets/agents/openai.yaml +1 -0
  87. package/skills/triage/AGENT-BRIEF.md +207 -0
  88. package/skills/triage/LICENSE +21 -0
  89. package/skills/triage/OUT-OF-SCOPE.md +105 -0
  90. package/skills/triage/SKILL.md +112 -0
  91. package/skills/triage/agents/openai.yaml +5 -0
  92. package/skills/wait-what/SKILL.md +2 -3
  93. package/skills/wait-what/agents/openai.yaml +1 -0
  94. package/skills/wayfinder/SKILL.md +5 -6
  95. package/skills/wayfinder/agents/openai.yaml +1 -0
  96. package/skills/wizard/LICENSE +21 -0
  97. package/skills/wizard/SKILL.md +44 -0
  98. package/skills/wizard/agents/openai.yaml +3 -0
  99. package/skills/wizard/template.sh +223 -0
  100. package/skills/writing-for-agents/SKILL.md +0 -2
  101. package/dist/blocks.js +0 -51
  102. package/skills/resolving-merge-conflicts/SKILL.md +0 -16
  103. package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
  104. /package/skills/{resolving-merge-conflicts → implement-spec}/LICENSE +0 -0
package/dist/agents.js CHANGED
@@ -1,15 +1,25 @@
1
1
  import { client } from '@agentclientprotocol/sdk';
2
2
  import { readFile, realpath } from 'node:fs/promises';
3
- import { killTree, startAdapter } from './acp.js';
3
+ import { setTimeout as delay } from 'node:timers/promises';
4
+ import { startAdapter } from './acp.js';
4
5
  import { CLIS, KIT_VERSION, below, installAdapter, installCli, locate, run, 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
+ const SIGNED_OUT = { signed_in: false, models: [], modes: [] };
13
+ const INSTALLING = { release: null, failure: null, installing: true, ...SIGNED_OUT };
7
14
  function select(options, category) {
8
15
  const option = options.find((candidate) => candidate.category === category);
9
16
  return option?.type === 'select' ? option : undefined;
10
17
  }
18
+ function choices(options) {
19
+ return options.flatMap((entry) => ('group' in entry ? entry.options : [entry]));
20
+ }
11
21
  function values(options) {
12
- return options.flatMap((entry) => ('group' in entry ? entry.options : [entry])).map((entry) => entry.value);
22
+ return choices(options).map((entry) => entry.value);
13
23
  }
14
24
  function causeOf(error) {
15
25
  return error instanceof Error ? error.message : String(error);
@@ -17,13 +27,31 @@ function causeOf(error) {
17
27
  function logged(error) {
18
28
  process.stderr.write(`kit: ${causeOf(error)}\n`);
19
29
  }
30
+ const LAPTOP_CHASSIS = new Set(['8', '9', '10', '14', '30', '31', '32']);
31
+ const DESKTOP_CHASSIS = new Set(['3', '4', '5', '6', '7', '15', '16']);
32
+ const SERVER_CHASSIS = new Set(['17', '18', '19', '20', '21', '22', '23', '24']);
33
+ export function chassisForm(value) {
34
+ const chassis = value.trim();
35
+ if (LAPTOP_CHASSIS.has(chassis))
36
+ return 'laptop';
37
+ if (DESKTOP_CHASSIS.has(chassis))
38
+ return 'desktop';
39
+ if (SERVER_CHASSIS.has(chassis))
40
+ return 'server';
41
+ return undefined;
42
+ }
43
+ export function hostVm(version) {
44
+ return /docker desktop|microsoft|wsl|linuxkit/i.test(version);
45
+ }
20
46
  async function models(kind, adapter, known) {
21
47
  const agent = adapter.connection.agent;
22
48
  const opened = await agent.request('session/new', { cwd: kitHome(), mcpServers: [] });
23
49
  let options = opened.configOptions ?? [];
50
+ const mode = select(options, 'mode');
51
+ const modes = mode === undefined ? [] : choices(mode.options).map(({ value, name }) => ({ mode: value, name }));
24
52
  const model = select(options, 'model');
25
53
  if (model === undefined)
26
- return [];
54
+ return { models: [], modes };
27
55
  const offered = [];
28
56
  for (const value of values(model.options)) {
29
57
  if (select(options, 'model')?.currentValue !== value) {
@@ -39,29 +67,49 @@ async function models(kind, adapter, known) {
39
67
  const effort = select(options, 'thought_level');
40
68
  offered.push({ model: value, efforts: effort === undefined ? [] : values(effort.options) });
41
69
  }
42
- return offered;
70
+ return { models: offered, modes };
43
71
  }
44
- async function offer(kind, cli, known) {
72
+ async function signedIn(kind, cli, known, signal) {
45
73
  const probe = CLIS[kind].signedIn;
46
- if ('command' in probe && (await run(cli, probe.command, 30_000)).status !== 0)
74
+ // A sign-in check that has not answered in half a minute counts as signed out rather than stalling the report.
75
+ if ('command' in probe)
76
+ return (await run(cli, probe.command, 30_000, false, signal)).status === 0;
77
+ return (await offer(kind, cli, known, signal)).signed_in;
78
+ }
79
+ function ended(signal) {
80
+ return new Promise((_, reject) => {
81
+ if (signal.aborted)
82
+ reject(signal.reason);
83
+ signal.addEventListener('abort', () => reject(signal.reason), { once: true });
84
+ });
85
+ }
86
+ async function offer(kind, cli, known, signal) {
87
+ const probe = CLIS[kind].signedIn;
88
+ // A sign-in check that has not answered in half a minute counts as signed out rather than stalling the report.
89
+ if ('command' in probe && (await run(cli, probe.command, 30_000, false, signal)).status !== 0)
47
90
  return SIGNED_OUT;
48
91
  let adapter;
49
92
  try {
50
- adapter = await startAdapter(kind, cli, kitHome(), client({ name: '@agentshouse/kit' }));
93
+ adapter = await startAdapter(kind, cli, kitHome(), client({ name: '@agentshouse/kit' }), () => undefined, {}, signal);
94
+ signal?.throwIfAborted();
51
95
  if ('initializeMeta' in probe) {
52
96
  const method = adapter.initialized._meta?.[probe.initializeMeta];
53
97
  if (typeof method !== 'string' || method.length === 0)
54
98
  return SIGNED_OUT;
55
99
  }
56
- return { signed_in: true, models: await models(kind, adapter, known.models) };
100
+ return { signed_in: true, ...(await models(kind, adapter, known.models)) };
57
101
  }
58
102
  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 };
103
+ if (!signal?.aborted)
104
+ process.stderr.write(`kit: ${kind} did not offer its models: ${causeOf(error)}\n`);
105
+ return {
106
+ signed_in: adapter === undefined && 'initializeMeta' in probe ? known.signed_in : true,
107
+ models: known.models,
108
+ modes: known.modes,
109
+ };
61
110
  }
62
111
  finally {
63
- if (adapter !== undefined)
64
- killTree(adapter.child);
112
+ adapter?.retire();
65
113
  }
66
114
  }
67
115
  function reported(kind, reading) {
@@ -70,36 +118,66 @@ function reported(kind, reading) {
70
118
  release: reading.release,
71
119
  ...(reading.minimum === undefined ? {} : { minimum: reading.minimum }),
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
  }
128
+ const NOT_READY = "the Agent's CLI is not ready on this computer";
77
129
  function tooOld(kind, release) {
78
130
  const cli = CLIS[kind];
79
131
  return `${cli.bin} ${release} is older than ${cli.minimum}, the oldest this Kit runs`;
80
132
  }
81
133
  async function sshHostKey() {
134
+ // Listing a local host key takes a moment, so ten seconds only bounds a stuck ssh-keygen.
82
135
  const listed = await run('ssh-keygen', ['-l', '-E', 'sha256', '-f', '/etc/ssh/ssh_host_ed25519_key.pub'], 10_000);
83
136
  return listed.status === 0 ? (/^\d+ (SHA256:\S+)/.exec(listed.output)?.[1] ?? null) : null;
84
137
  }
85
138
  async function operatingSystem() {
139
+ // sw_vers answers at once, so ten seconds only bounds a stuck call.
86
140
  if (process.platform === 'darwin')
87
141
  return `macOS ${(await run('sw_vers', ['-productVersion'], 10_000)).output.trim()}`;
88
142
  const release = await readFile('/etc/os-release', 'utf8');
89
143
  return /^PRETTY_NAME="?([^"\n]*)"?$/m.exec(release)?.[1] ?? release;
90
144
  }
145
+ async function hostForm() {
146
+ if (process.platform === 'darwin') {
147
+ const battery = await run('pmset', ['-g', 'batt'], 10_000);
148
+ return battery.status === 0 && battery.output.includes('InternalBattery') ? 'laptop' : 'desktop';
149
+ }
150
+ if (process.platform !== 'linux')
151
+ return undefined;
152
+ const [container, version, chassis] = await Promise.all([
153
+ readFile('/.dockerenv', 'utf8').then(() => true).catch(() => false),
154
+ readFile('/proc/version', 'utf8').catch(() => ''),
155
+ readFile('/sys/class/dmi/id/chassis_type', 'utf8').catch(() => ''),
156
+ ]);
157
+ if (container && hostVm(version))
158
+ return undefined;
159
+ return chassisForm(chassis);
160
+ }
91
161
  export class Agents {
92
- desired = { agents: [], routes: [] };
162
+ desired = { agents: [], routes: [], transcribe: false };
93
163
  house;
94
164
  reading = Promise.resolve();
95
165
  queues = new Map();
96
166
  installing = Promise.resolve();
167
+ installs = new Map();
97
168
  readings = new Map();
98
169
  reads = new Map();
170
+ starts = 0;
99
171
  sending = Promise.resolve();
100
172
  sent = null;
101
- constructor(house) {
173
+ refreshing = 0;
174
+ changed;
175
+ constructor(house, changed = () => undefined) {
102
176
  this.house = house;
177
+ this.changed = changed;
178
+ }
179
+ idle() {
180
+ return this.refreshing === 0;
103
181
  }
104
182
  read() {
105
183
  return this.reading.then(() => undefined, () => undefined);
@@ -108,32 +186,45 @@ export class Agents {
108
186
  return this.desired.routes.find((route) => route.agent_id === agentId);
109
187
  }
110
188
  refresh() {
189
+ this.refreshing++;
190
+ this.changed();
111
191
  const reading = this.read().then(async () => {
112
192
  this.desired = await this.house.deliver('/kit/agents/desired', {});
113
193
  return this.desired;
114
194
  });
115
195
  this.reading = reading;
116
- return reading.then(({ agents }) => {
117
- Promise.all(agents.map((kind) => this.queue(kind, () => this.load(kind))))
196
+ reading
197
+ .then(({ agents, transcribe }) => Promise.all([
198
+ holdTranscription(transcribe)
199
+ .then((changed) => (changed ? this.report() : undefined))
200
+ .catch(logged),
201
+ Promise.all(agents.map((kind) => this.load(kind)))
118
202
  .then(() => this.report())
119
- .catch(logged);
203
+ .catch(logged),
204
+ ]))
205
+ .catch(() => undefined)
206
+ .finally(() => {
207
+ this.refreshing--;
208
+ this.changed();
120
209
  });
210
+ return reading.then(() => undefined);
121
211
  }
122
212
  cli(kind) {
213
+ this.starts += 1;
123
214
  return this.queue(kind, async () => {
124
215
  const resolved = await this.resolve(kind);
125
216
  if (!('cli' in resolved)) {
126
217
  this.update(kind, resolved);
127
- throw new Error(resolved.failure ?? tooOld(kind, resolved.release));
218
+ throw new Error(resolved.failure === null ? tooOld(kind, resolved.release) : NOT_READY);
128
219
  }
129
220
  const { cli, release } = resolved;
130
221
  const known = this.readings.get(kind);
131
222
  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 });
223
+ if (known?.release !== release || known.minimum !== undefined || this.installs.has(kind)) {
224
+ this.update(kind, { release, failure: null, signed_in: kept.signed_in, models: kept.models, modes: kept.modes });
134
225
  }
135
226
  const read = this.nextRead(kind);
136
- offer(kind, cli, kept)
227
+ this.quietOffer(kind, cli, kept)
137
228
  .then((offered) => {
138
229
  const current = this.readings.get(kind);
139
230
  if (this.reads.get(kind) === read && current?.release === release)
@@ -143,16 +234,26 @@ export class Agents {
143
234
  return cli;
144
235
  });
145
236
  }
146
- located(kind) {
147
- return this.queue(kind, () => this.ensure(kind));
237
+ located(kind, signal) {
238
+ const owner = {};
239
+ return this.queue(kind, () => this.ensure(kind, signal, owner).finally(() => this.settle(kind, owner)), signal);
148
240
  }
149
241
  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
- });
242
+ await this.examine(kind, this.readings.get(kind) ?? SIGNED_OUT, offer);
154
243
  await this.report();
155
244
  }
245
+ async recheck(kind, signal) {
246
+ const known = this.readings.get(kind) ?? SIGNED_OUT;
247
+ const owner = {};
248
+ try {
249
+ await this.examine(kind, known, async (kind, cli) => ((await signedIn(kind, cli, known, signal)) ? { ...known, signed_in: true } : SIGNED_OUT), signal, owner);
250
+ }
251
+ finally {
252
+ this.settle(kind, owner);
253
+ }
254
+ if (!signal.aborted)
255
+ await this.report();
256
+ }
156
257
  report() {
157
258
  const sending = this.sending.catch(() => undefined).then(() => this.send());
158
259
  this.sending = sending;
@@ -163,43 +264,58 @@ export class Agents {
163
264
  this.reads.set(kind, read);
164
265
  return read;
165
266
  }
166
- update(kind, reading) {
267
+ hold(kind, reading) {
167
268
  this.readings.set(kind, reading);
269
+ this.installs.delete(kind);
270
+ }
271
+ update(kind, reading) {
272
+ this.hold(kind, reading);
168
273
  this.report().catch(logged);
169
274
  }
170
- queue(kind, work) {
171
- const queued = (this.queues.get(kind) ?? Promise.resolve()).then(work);
275
+ settle(kind, owner) {
276
+ if (this.installs.get(kind) !== owner)
277
+ return;
278
+ this.installs.delete(kind);
279
+ this.report().catch(logged);
280
+ }
281
+ queue(kind, work, signal) {
282
+ const queued = (this.queues.get(kind) ?? Promise.resolve()).then(() => {
283
+ signal?.throwIfAborted();
284
+ return work();
285
+ });
172
286
  this.queues.set(kind, queued.catch(() => undefined));
173
- return queued;
287
+ return signal === undefined ? queued : Promise.race([queued, ended(signal)]);
174
288
  }
175
- async ensure(kind) {
176
- const failed = await installAdapter(kind);
289
+ async ensure(kind, signal, owner = {}) {
290
+ const failed = await installAdapter(kind, signal);
177
291
  if (failed !== null)
178
292
  throw new Error(failed);
179
- const found = await locate(kind);
293
+ const found = await locate(kind, signal);
180
294
  if (found !== null)
181
295
  return found;
182
- const installing = this.installing.catch(() => undefined).then(() => installCli(kind));
296
+ this.installs.set(kind, owner);
297
+ this.report().catch(logged);
298
+ const installing = this.installing.catch(() => undefined).then(() => installCli(kind, signal));
183
299
  this.installing = installing;
184
300
  const failure = await installing;
185
301
  if (failure !== null)
186
302
  throw new Error(failure);
187
- const installed = await locate(kind);
303
+ const installed = await locate(kind, signal);
188
304
  if (installed === null)
189
305
  throw new Error(`the login shell does not find ${CLIS[kind].bin} after its install`);
190
306
  return installed;
191
307
  }
192
- async resolve(kind) {
308
+ async resolve(kind, signal, owner) {
193
309
  let cli;
194
310
  try {
195
- cli = await realpath(await this.ensure(kind));
311
+ cli = await realpath(await this.ensure(kind, signal, owner));
196
312
  }
197
313
  catch (error) {
198
314
  const failure = causeOf(error);
199
315
  process.stderr.write(`kit: ${kind} did not install: ${failure}\n`);
200
316
  return { release: null, failure, ...SIGNED_OUT };
201
317
  }
202
- const release = await versionOf(cli);
318
+ const release = await versionOf(cli, signal);
203
319
  if (release === null)
204
320
  return { release: null, failure: `${cli} --version names no version`, ...SIGNED_OUT };
205
321
  const minimum = CLIS[kind].minimum;
@@ -207,30 +323,43 @@ export class Agents {
207
323
  return { release, minimum, failure: null, ...SIGNED_OUT };
208
324
  return { cli, release };
209
325
  }
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)) };
326
+ async examine(kind, known, read, signal, owner) {
327
+ const current = this.nextRead(kind);
328
+ const resolved = await this.queue(kind, () => this.resolve(kind, signal, owner), signal);
329
+ const reading = 'cli' in resolved ? { release: resolved.release, failure: null, ...(await read(kind, resolved.cli, known)) } : resolved;
330
+ if (this.reads.get(kind) === current && !signal?.aborted)
331
+ this.hold(kind, reading);
332
+ }
333
+ async quietOffer(kind, cli, known) {
334
+ for (let seen = -1; seen !== this.starts;) {
335
+ seen = this.starts;
336
+ await delay(MODEL_PROBE_QUIET_MS);
337
+ }
338
+ return offer(kind, cli, known);
215
339
  }
216
340
  async load(kind) {
217
341
  const known = this.readings.get(kind);
218
- if (known !== undefined && known.failure === null)
342
+ if (known !== undefined && known.failure === null && known.minimum === undefined)
219
343
  return;
220
- this.nextRead(kind);
221
- this.readings.set(kind, await this.examine(kind, SIGNED_OUT));
344
+ await this.examine(kind, SIGNED_OUT, (kind, cli, known) => this.quietOffer(kind, cli, known));
345
+ await this.report();
222
346
  }
223
347
  async send() {
224
348
  const agents = this.desired.agents.flatMap((kind) => {
225
- const reading = this.readings.get(kind);
349
+ const reading = this.installs.has(kind) ? INSTALLING : this.readings.get(kind);
226
350
  return reading === undefined ? [] : [reported(kind, reading)];
227
351
  });
228
352
  const hostKey = await sshHostKey();
353
+ const form = await hostForm();
229
354
  const body = {
230
355
  os: await operatingSystem(),
356
+ ...(form === undefined ? {} : { form }),
231
357
  kit_version: KIT_VERSION,
358
+ wire: KIT_WIRE,
359
+ updates_itself: await updatesItself(),
232
360
  agent_base: agentBase(),
233
361
  agents,
362
+ transcription: transcriptionReport(),
234
363
  ...(hostKey === null ? {} : { ssh_host_key: hostKey }),
235
364
  };
236
365
  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 Kits 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,23 +1,23 @@
1
1
  {
2
2
  "schemaVersion": 2,
3
- "version": "0.2.1-cb6679d048e60",
3
+ "version": "0.2.1-cb8dc89b78e07",
4
4
  "hosts": {
5
5
  "macos": {
6
6
  "asset": {
7
7
  "name": "connect-linux.sh",
8
- "url": "https://github.com/agentshouse/kit/releases/download/v0.2.1-cb6679d048e60/connect-linux.sh"
8
+ "url": "https://github.com/agentshouse/kit/releases/download/v0.2.1-cb8dc89b78e07/connect-linux.sh"
9
9
  }
10
10
  },
11
11
  "linux": {
12
12
  "asset": {
13
13
  "name": "connect-linux.sh",
14
- "url": "https://github.com/agentshouse/kit/releases/download/v0.2.1-cb6679d048e60/connect-linux.sh"
14
+ "url": "https://github.com/agentshouse/kit/releases/download/v0.2.1-cb8dc89b78e07/connect-linux.sh"
15
15
  }
16
16
  },
17
17
  "windows": {
18
18
  "asset": {
19
19
  "name": "connect-windows.ps1",
20
- "url": "https://github.com/agentshouse/kit/releases/download/v0.2.1-cb6679d048e60/connect-windows.ps1"
20
+ "url": "https://github.com/agentshouse/kit/releases/download/v0.2.1-cb8dc89b78e07/connect-windows.ps1"
21
21
  }
22
22
  }
23
23
  }