@agentshouse/kit 0.2.1-alpha.9 → 0.2.1-c073f4e9391b2

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 (48) hide show
  1. package/README.md +44 -26
  2. package/dist/abort.js +9 -0
  3. package/dist/acp.js +18 -75
  4. package/dist/agents.js +266 -53
  5. package/dist/api.js +40 -4
  6. package/dist/app-git.js +64 -0
  7. package/dist/bootstrap.json +10 -14
  8. package/dist/bridge.js +89 -71
  9. package/dist/chatgpt.js +25 -0
  10. package/dist/clis-main.js +23 -0
  11. package/dist/clis.js +203 -71
  12. package/dist/codex-main.js +58 -0
  13. package/dist/conversations.js +328 -199
  14. package/dist/copies.js +188 -161
  15. package/dist/end.js +22 -0
  16. package/dist/enrol-main.js +7 -1
  17. package/dist/events.js +521 -0
  18. package/dist/files.js +16 -1
  19. package/dist/git-main.js +2 -2
  20. package/dist/git.js +6 -2
  21. package/dist/home.js +72 -5
  22. package/dist/house-main.js +181 -57
  23. package/dist/instructions.js +16 -13
  24. package/dist/kit-main.js +55 -4
  25. package/dist/login.js +108 -97
  26. package/dist/logout.js +21 -0
  27. package/dist/mcp.js +39 -0
  28. package/dist/operation.js +7 -4
  29. package/dist/parts.js +82 -0
  30. package/dist/placement.js +48 -0
  31. package/dist/refusals.js +53 -0
  32. package/dist/relay.js +20 -0
  33. package/dist/resident.js +70 -34
  34. package/dist/scope.js +203 -0
  35. package/dist/secret-input.js +7 -2
  36. package/dist/service-main.js +9 -0
  37. package/dist/service.js +33 -0
  38. package/dist/shell.js +3 -0
  39. package/dist/sign-in.js +48 -11
  40. package/dist/skills.js +5 -10
  41. package/dist/stream.js +37 -7
  42. package/dist/transcription.js +170 -0
  43. package/dist/update.js +140 -0
  44. package/dist/wire.js +6 -0
  45. package/dist/work.js +348 -0
  46. package/how-we-work.md +10 -12
  47. package/package.json +4 -7
  48. package/dist/blocks.js +0 -51
package/README.md CHANGED
@@ -1,41 +1,59 @@
1
1
  # House Kit
2
2
 
3
- House Kit connects an Environment, a Linux computer or server, to
4
- [agents.house](https://agents.house). It runs your Agents' conversations
5
- through the agent CLIs you chose and gives each Agent the `house` command.
3
+ House Kit connects an Environment, a Linux computer or server, to [agents.house](https://agents.house). It runs
4
+ your Agents' conversations through the agent CLIs you chose and gives each Agent the `house` command.
6
5
 
7
6
  ## Install
8
7
 
9
- Connect a computer from agents.house. It gives you the one command that
10
- installs House Kit on that computer and updates it when you run it again. On
11
- Linux, macOS and Windows 11 it runs House Kit in one Docker container with
12
- your workspace, `~/AgentsHouse` unless you pass `--workspace <path>`, mounted
13
- at `/agents/house`. Append a `kit` or `house` command to the same command to
14
- run it in that container.
8
+ Run `curl -fsSL agents.house/kit | sh`, or `irm agents.house/kit.ps1 | iex` on Windows, and again to update;
9
+ options follow `sh -s --`. On macOS and Linux it installs House Kit natively under your own account, without an admin password,
10
+ as a service that runs while you are logged in. `kit` and `house` are then on
11
+ your login shell's `PATH`, and your workspace is `~/AgentsHouse` unless you
12
+ pass `--workspace <path>`.
15
13
 
16
- On your own Ubuntu 26.04 server, run the Linux command with `--linux` to
14
+ Add `--container` to run House Kit in one Docker container instead, with your
15
+ workspace mounted at `/agents/house`; your Agents then reach only that
16
+ workspace. On Windows 11 the container is the one choice. Append a `kit` or
17
+ `house` command to the container's options to run it in that container.
18
+ Running the command for the other choice on a connected computer asks you to
19
+ confirm, then replaces its Environment, whose Agents end, with a new one.
20
+
21
+ On your own Linux server, run the Linux command with `--linux` to
17
22
  install House Kit natively, with its workspace at `/agents/house`; `kit` and
18
23
  `house` are then on the server's `PATH`.
19
24
 
20
- House Kit also gives every Agent on the Environment a set of agent skills. Add
21
- `--no-skills` to the command to leave them out; running it again without the
22
- flag brings them back.
25
+ House Kit runs the agent CLIs your login shell finds and installs a missing one
26
+ through that CLI's own installer. Running the command again offers to update
27
+ each CLI older than House Kit needs; add `--update-clis` to update every one to
28
+ its latest release without asking. House Kit also gives every Agent on the
29
+ Environment a set of agent skills. Add `--no-skills` to the command to leave
30
+ them out; running it again without the flag brings them back.
23
31
 
24
32
  ## Commands
25
33
 
26
- `kit login` opens a link you confirm in your browser and keeps the
27
- Environment's credential in `~/.house-kit`, or in `$HOUSE_KIT_HOME` when it is
28
- set. Pass `--manual` to paste the code by hand on a machine without a browser,
29
- and `--environment <id>` to reconnect an Environment you already have.
30
-
31
- `kit resident` keeps the Environment connected, and keeps the Rooms you chose
32
- for this Environment as Git repositories under `/agents/house/working-copies`.
33
- Commit in one and run `house git push --owner` there to send the commit to
34
- House; on a computer, run it through the install command from the copy's
35
- directory. Inside an Agent conversation the Agent runs `house git push`.
34
+ `kit login` opens or prints a link you confirm in any browser, on this computer
35
+ or another, continues on its own and keeps the Environment's credential in
36
+ `~/.house-kit`, or in `$HOUSE_KIT_HOME` when it is set. `--environment <id>`
37
+ reconnects an Environment you already have; `--rooms <ref>[,<ref>...]` gives it
38
+ only those Rooms and `--no-private` every Room but your Private Room; `--code <code>` connects immediately without a link.
39
+ A login also connects your own agent: outside an Agent conversation, `house <tool>` calls
40
+ House as you and `git` clones and pushes your Apps at `<house>/app/` as you, until you revoke
41
+ that connection on Connections. `house help` lists the Tools. Codex sessions started after a
42
+ login run a lone `house` command outside their sandbox. `kit logout` disconnects this
43
+ computer, stops House Kit and ends your own agent's connection, so `house` and `git` no
44
+ longer act as you here; a later `kit login` reconnects the same Environment.
45
+ In a container you do not control, such as ChatGPT's, `npm i -g @agentshouse/kit` and `kit chatgpt --key <key>` with a key made on Connections let `house <tool>` act as you, starting and configuring nothing else.
46
+
47
+ `kit resident` keeps the Environment connected and, as it connects, copies each
48
+ Room you chose for it as a Git repository in the workspace's `local-copies`.
49
+ `house fetch` records House's changes on each copy's `refs/house/received`;
50
+ rebase onto it, commit and run `house push --owner` in the copy, through the
51
+ install command in the container. An Agent runs both, without `--owner`.
52
+
53
+ House Kit turns each audio file of your message to an Agent into text on this
54
+ computer before the Agent reads it. While transcription is on, the speech
55
+ engine and model use about 425 MB in `~/.house-kit`; turning it off deletes them.
36
56
 
37
57
  ## License
38
58
 
39
- MIT
40
-
41
- The skills come from [mattpocock/skills](https://github.com/mattpocock/skills); thank you, Matt Pocock.
59
+ MIT. The skills come from [mattpocock/skills](https://github.com/mattpocock/skills); thank you, Matt Pocock.
package/dist/abort.js ADDED
@@ -0,0 +1,9 @@
1
+ export function unlessAborted(work, signal) {
2
+ return new Promise((resolve, reject) => {
3
+ const aborted = () => reject(signal.reason);
4
+ signal.addEventListener('abort', aborted, { once: true });
5
+ if (signal.aborted)
6
+ aborted();
7
+ work.then(resolve, reject).finally(() => signal.removeEventListener('abort', aborted));
8
+ });
9
+ }
package/dist/acp.js CHANGED
@@ -1,17 +1,21 @@
1
1
  import { ndJsonStream, PROTOCOL_VERSION, } from '@agentclientprotocol/sdk';
2
2
  import { spawn } from 'node:child_process';
3
- import { readdirSync, readFileSync } from 'node:fs';
4
3
  import { Readable, Writable } from 'node:stream';
5
- import { CLIS, KIT_VERSION, cliCommand, withoutProxy } from './clis.js';
6
- export const TURN_STARTED = 'kit/turn_started';
7
- export const TURN_ENDED = 'kit/turn_ended';
8
- export async function startAdapter(kind, cwd, app, jobs = () => undefined, added = {}) {
9
- const child = spawn(cliCommand(kind), CLIS[kind].args, {
4
+ import { CLIS, KIT_VERSION, adapterCommand, withoutProxy } from './clis.js';
5
+ import { killTree } from './scope.js';
6
+ export async function startAdapter(kind, cli, cwd, app, events = () => undefined, added = {}, signal, scope = null) {
7
+ const adapter = CLIS[kind].adapter;
8
+ const started = [adapter === null ? cli : adapterCommand(kind), CLIS[kind].args];
9
+ const [command, args] = scope?.command(...started) ?? started;
10
+ const child = spawn(command, args, {
10
11
  cwd,
11
- env: { ...withoutProxy(), ...added },
12
+ env: { ...withoutProxy(), ...(adapter === null ? {} : { ...adapter.env, [adapter.executable]: cli }), ...added },
12
13
  stdio: ['pipe', 'pipe', 'pipe'],
13
14
  detached: true,
14
15
  });
16
+ const stop = () => killTree(child);
17
+ signal?.addEventListener('abort', stop, { once: true });
18
+ child.on('exit', () => signal?.removeEventListener('abort', stop));
15
19
  let stderr = '';
16
20
  child.stderr.on('data', (chunk) => {
17
21
  stderr = (stderr + chunk.toString('utf8')).slice(-4000);
@@ -28,16 +32,11 @@ export async function startAdapter(kind, cwd, app, jobs = () => undefined, added
28
32
  writable: stream.writable,
29
33
  readable: stream.readable.pipeThrough(new TransformStream({
30
34
  transform(message, controller) {
31
- const notice = message;
32
- const job = CLIS[kind].job(notice);
33
- if (job !== null)
34
- jobs(job);
35
- if (!notice.params?.update?.sessionUpdate?.startsWith('async_task_'))
35
+ const read = message;
36
+ events(read);
37
+ if (read.id !== undefined || (read.method !== 'session/update' && !read.method.startsWith('_'))) {
36
38
  controller.enqueue(message);
37
- if (CLIS[kind].turnStarted(notice))
38
- controller.enqueue({ jsonrpc: '2.0', method: TURN_STARTED });
39
- if (CLIS[kind].turnEnded(notice))
40
- controller.enqueue({ jsonrpc: '2.0', method: TURN_ENDED });
39
+ }
41
40
  },
42
41
  })),
43
42
  });
@@ -49,10 +48,12 @@ export async function startAdapter(kind, cwd, app, jobs = () => undefined, added
49
48
  fs: { readTextFile: false, writeTextFile: false },
50
49
  terminal: false,
51
50
  elicitation: { form: {} },
52
- _meta: { jetbrains: { air: { version: 1, capabilities: ['asyncTasks'] } } },
51
+ session: { compaction: {}, notices: {} },
52
+ _meta: { jetbrains: { air: { version: 1, capabilities: CLIS[kind].air } } },
53
53
  },
54
54
  clientInfo: { name: '@agentshouse/kit', version: KIT_VERSION },
55
55
  });
56
+ scope?.entered(child.pid);
56
57
  return { child, connection, initialized, exited };
57
58
  }
58
59
  catch (error) {
@@ -60,61 +61,3 @@ export async function startAdapter(kind, cwd, app, jobs = () => undefined, added
60
61
  throw error;
61
62
  }
62
63
  }
63
- function parents() {
64
- const children = new Map();
65
- for (const entry of readdirSync('/proc')) {
66
- if (!/^\d+$/.test(entry))
67
- continue;
68
- try {
69
- const stat = readFileSync(`/proc/${entry}/stat`, 'utf8');
70
- const parent = Number(stat.slice(stat.lastIndexOf(')') + 2).split(' ')[1]);
71
- children.set(parent, [...(children.get(parent) ?? []), Number(entry)]);
72
- }
73
- catch { }
74
- }
75
- return children;
76
- }
77
- function descendants(roots) {
78
- const children = parents();
79
- const tree = [];
80
- const pending = roots.flatMap((root) => children.get(root) ?? []);
81
- while (pending.length > 0) {
82
- const pid = pending.pop();
83
- tree.push(pid);
84
- pending.push(...(children.get(pid) ?? []));
85
- }
86
- return tree;
87
- }
88
- function killAll(pids) {
89
- for (const pid of pids) {
90
- for (const target of [-pid, pid]) {
91
- try {
92
- process.kill(target, 'SIGKILL');
93
- }
94
- catch { }
95
- }
96
- }
97
- }
98
- function marked(marker) {
99
- return readdirSync('/proc')
100
- .filter((entry) => {
101
- try {
102
- return /^\d+$/.test(entry) && readFileSync(`/proc/${entry}/environ`, 'utf8').split('\0').includes(marker);
103
- }
104
- catch {
105
- return false;
106
- }
107
- })
108
- .map(Number);
109
- }
110
- export function killTree(child) {
111
- killAll([child.pid, ...descendants([child.pid])]);
112
- }
113
- export function killMarked(marker) {
114
- const pids = marked(marker);
115
- killAll([...pids, ...descendants(pids)]);
116
- }
117
- export function killDescendants() {
118
- const pids = marked(`HOUSE_KIT_RESIDENT=${process.pid}`);
119
- killAll([...pids, ...descendants([process.pid, ...pids])]);
120
- }
package/dist/agents.js CHANGED
@@ -1,90 +1,303 @@
1
1
  import { client } from '@agentclientprotocol/sdk';
2
- import { readFile } from 'node:fs/promises';
2
+ import { readFile, realpath } from 'node:fs/promises';
3
3
  import { startAdapter } from './acp.js';
4
- import { CLIS, KIT_VERSION, cliCommand, install, run } from './clis.js';
5
- import { kitHome } from './home.js';
6
- async function signedIn(kind) {
7
- const probe = CLIS[kind].signedIn;
8
- if ('command' in probe) {
9
- return (await run(cliCommand(kind), probe.command, 30_000)).status === 0;
4
+ import { CLIS, KIT_VERSION, below, installAdapter, installCli, locate, run, versionOf } from './clis.js';
5
+ import { agentBase, kitHome } from './home.js';
6
+ import { updatesItself } from './placement.js';
7
+ import { killTree } from './scope.js';
8
+ import { holdTranscription, transcriptionReport } from './transcription.js';
9
+ import { KIT_WIRE } from './wire.js';
10
+ const SIGNED_OUT = { signed_in: false, models: [], modes: [] };
11
+ function select(options, category) {
12
+ const option = options.find((candidate) => candidate.category === category);
13
+ return option?.type === 'select' ? option : undefined;
14
+ }
15
+ function choices(options) {
16
+ return options.flatMap((entry) => ('group' in entry ? entry.options : [entry]));
17
+ }
18
+ function values(options) {
19
+ return choices(options).map((entry) => entry.value);
20
+ }
21
+ function causeOf(error) {
22
+ return error instanceof Error ? error.message : String(error);
23
+ }
24
+ function logged(error) {
25
+ process.stderr.write(`kit: ${causeOf(error)}\n`);
26
+ }
27
+ async function models(kind, adapter, known) {
28
+ const agent = adapter.connection.agent;
29
+ const opened = await agent.request('session/new', { cwd: kitHome(), mcpServers: [] });
30
+ let options = opened.configOptions ?? [];
31
+ const mode = select(options, 'mode');
32
+ const modes = mode === undefined ? [] : choices(mode.options).map(({ value, name }) => ({ mode: value, name }));
33
+ const model = select(options, 'model');
34
+ if (model === undefined)
35
+ return { models: [], modes };
36
+ const offered = [];
37
+ for (const value of values(model.options)) {
38
+ if (select(options, 'model')?.currentValue !== value) {
39
+ try {
40
+ options = (await agent.request('session/set_config_option', { sessionId: opened.sessionId, configId: model.id, value })).configOptions;
41
+ }
42
+ catch (error) {
43
+ process.stderr.write(`kit: ${kind} refused its model ${value}: ${causeOf(error)}\n`);
44
+ offered.push({ model: value, efforts: known.find((entry) => entry.model === value)?.efforts ?? [] });
45
+ continue;
46
+ }
47
+ }
48
+ const effort = select(options, 'thought_level');
49
+ offered.push({ model: value, efforts: effort === undefined ? [] : values(effort.options) });
10
50
  }
51
+ return { models: offered, modes };
52
+ }
53
+ async function signedIn(kind, cli, known, signal) {
54
+ const probe = CLIS[kind].signedIn;
55
+ // A sign-in check that has not answered in half a minute counts as signed out rather than stalling the report.
56
+ if ('command' in probe)
57
+ return (await run(cli, probe.command, 30_000, false, signal)).status === 0;
58
+ return (await offer(kind, cli, known, signal)).signed_in;
59
+ }
60
+ function ended(signal) {
61
+ return new Promise((_, reject) => {
62
+ if (signal.aborted)
63
+ reject(signal.reason);
64
+ signal.addEventListener('abort', () => reject(signal.reason), { once: true });
65
+ });
66
+ }
67
+ async function offer(kind, cli, known, signal) {
68
+ const probe = CLIS[kind].signedIn;
69
+ // A sign-in check that has not answered in half a minute counts as signed out rather than stalling the report.
70
+ if ('command' in probe && (await run(cli, probe.command, 30_000, false, signal)).status !== 0)
71
+ return SIGNED_OUT;
72
+ let adapter;
11
73
  try {
12
- const adapter = await startAdapter(kind, kitHome(), client({ name: '@agentshouse/kit' }));
13
- adapter.child.kill('SIGKILL');
14
- const method = adapter.initialized._meta?.[probe.initializeMeta];
15
- return typeof method === 'string' && method.length > 0;
74
+ adapter = await startAdapter(kind, cli, kitHome(), client({ name: '@agentshouse/kit' }), () => undefined, {}, signal);
75
+ signal?.throwIfAborted();
76
+ if ('initializeMeta' in probe) {
77
+ const method = adapter.initialized._meta?.[probe.initializeMeta];
78
+ if (typeof method !== 'string' || method.length === 0)
79
+ return SIGNED_OUT;
80
+ }
81
+ return { signed_in: true, ...(await models(kind, adapter, known.models)) };
82
+ }
83
+ catch (error) {
84
+ if (!signal?.aborted)
85
+ process.stderr.write(`kit: ${kind} did not offer its models: ${causeOf(error)}\n`);
86
+ return {
87
+ signed_in: adapter === undefined && 'initializeMeta' in probe ? known.signed_in : true,
88
+ models: known.models,
89
+ modes: known.modes,
90
+ };
16
91
  }
17
- catch {
18
- return false;
92
+ finally {
93
+ if (adapter !== undefined)
94
+ killTree(adapter.child);
19
95
  }
20
96
  }
97
+ function reported(kind, reading) {
98
+ return {
99
+ kind,
100
+ release: reading.release,
101
+ ...(reading.minimum === undefined ? {} : { minimum: reading.minimum }),
102
+ failure: reading.failure,
103
+ signed_in: reading.signed_in,
104
+ models: reading.models,
105
+ modes: reading.modes,
106
+ full_access: CLIS[kind].fullAccess,
107
+ };
108
+ }
109
+ function tooOld(kind, release) {
110
+ const cli = CLIS[kind];
111
+ return `${cli.bin} ${release} is older than ${cli.minimum}, the oldest this Kit runs`;
112
+ }
113
+ async function sshHostKey() {
114
+ // Listing a local host key takes a moment, so ten seconds only bounds a stuck ssh-keygen.
115
+ const listed = await run('ssh-keygen', ['-l', '-E', 'sha256', '-f', '/etc/ssh/ssh_host_ed25519_key.pub'], 10_000);
116
+ return listed.status === 0 ? (/^\d+ (SHA256:\S+)/.exec(listed.output)?.[1] ?? null) : null;
117
+ }
21
118
  async function operatingSystem() {
119
+ // sw_vers answers at once, so ten seconds only bounds a stuck call.
120
+ if (process.platform === 'darwin')
121
+ return `macOS ${(await run('sw_vers', ['-productVersion'], 10_000)).output.trim()}`;
22
122
  const release = await readFile('/etc/os-release', 'utf8');
23
123
  return /^PRETTY_NAME="?([^"\n]*)"?$/m.exec(release)?.[1] ?? release;
24
124
  }
25
125
  export class Agents {
26
- desired = { agents: [], routes: [] };
126
+ desired = { agents: [], routes: [], transcribe: false };
27
127
  house;
28
128
  reading = Promise.resolve();
129
+ queues = new Map();
29
130
  installing = Promise.resolve();
30
- releases = new Map();
31
- reporting = null;
32
- again = false;
33
- constructor(house) {
131
+ readings = new Map();
132
+ reads = new Map();
133
+ sending = Promise.resolve();
134
+ sent = null;
135
+ refreshing = 0;
136
+ changed;
137
+ constructor(house, changed = () => undefined) {
34
138
  this.house = house;
139
+ this.changed = changed;
140
+ }
141
+ idle() {
142
+ return this.refreshing === 0;
35
143
  }
36
144
  read() {
37
145
  return this.reading.then(() => undefined, () => undefined);
38
146
  }
39
- installed() {
40
- return this.installing.catch(() => undefined);
41
- }
42
147
  route(agentId) {
43
148
  return this.desired.routes.find((route) => route.agent_id === agentId);
44
149
  }
45
150
  refresh() {
151
+ this.refreshing++;
152
+ this.changed();
46
153
  const reading = this.read().then(async () => {
47
154
  this.desired = await this.house.deliver('/kit/agents/desired', {});
48
155
  return this.desired;
49
156
  });
50
157
  this.reading = reading;
51
- const installing = this.installed()
52
- .then(() => reading)
53
- .then(({ agents }) => this.install(agents));
54
- this.installing = installing;
55
- if (this.reporting === null)
56
- this.reporting = this.reports();
57
- else
58
- this.again = true;
59
- return Promise.all([installing, this.reporting]).then(() => undefined);
60
- }
61
- async report() {
62
- const agents = [];
63
- for (const [kind, release] of this.releases) {
64
- agents.push({ kind, release, signed_in: release !== null && (await signedIn(kind)) });
65
- }
66
- await this.house.deliver('/kit/agents/report', {
67
- os: await operatingSystem(),
68
- kit_version: KIT_VERSION,
69
- agents,
158
+ reading
159
+ .then(({ agents, transcribe }) => Promise.all([
160
+ holdTranscription(transcribe)
161
+ .then((changed) => (changed ? this.report() : undefined))
162
+ .catch(logged),
163
+ Promise.all(agents.map((kind) => this.load(kind)))
164
+ .then(() => this.report())
165
+ .catch(logged),
166
+ ]))
167
+ .catch(() => undefined)
168
+ .finally(() => {
169
+ this.refreshing--;
170
+ this.changed();
70
171
  });
172
+ return reading.then(() => undefined);
173
+ }
174
+ cli(kind) {
175
+ return this.queue(kind, async () => {
176
+ const resolved = await this.resolve(kind);
177
+ if (!('cli' in resolved)) {
178
+ this.update(kind, resolved);
179
+ throw new Error(resolved.failure ?? tooOld(kind, resolved.release));
180
+ }
181
+ const { cli, release } = resolved;
182
+ const known = this.readings.get(kind);
183
+ const kept = known?.release === null || known?.minimum !== undefined ? SIGNED_OUT : (known ?? SIGNED_OUT);
184
+ if (known?.release !== release || known.minimum !== undefined) {
185
+ this.update(kind, { release, failure: null, signed_in: kept.signed_in, models: kept.models, modes: kept.modes });
186
+ }
187
+ const read = this.nextRead(kind);
188
+ offer(kind, cli, kept)
189
+ .then((offered) => {
190
+ const current = this.readings.get(kind);
191
+ if (this.reads.get(kind) === read && current?.release === release)
192
+ this.update(kind, { ...current, ...offered });
193
+ })
194
+ .catch(logged);
195
+ return cli;
196
+ });
197
+ }
198
+ located(kind, signal) {
199
+ return this.queue(kind, () => this.ensure(kind, signal), signal);
71
200
  }
72
- async install(kinds) {
73
- const releases = new Map();
74
- for (const kind of kinds)
75
- releases.set(kind, await install(kind));
76
- this.releases = releases;
201
+ async reread(kind) {
202
+ await this.examine(kind, this.readings.get(kind) ?? SIGNED_OUT, offer);
203
+ await this.report();
77
204
  }
78
- async reports() {
205
+ async recheck(kind, signal) {
206
+ const known = this.readings.get(kind) ?? SIGNED_OUT;
207
+ await this.examine(kind, known, async (kind, cli) => ((await signedIn(kind, cli, known, signal)) ? { ...known, signed_in: true } : SIGNED_OUT), signal);
208
+ if (!signal.aborted)
209
+ await this.report();
210
+ }
211
+ report() {
212
+ const sending = this.sending.catch(() => undefined).then(() => this.send());
213
+ this.sending = sending;
214
+ return sending;
215
+ }
216
+ nextRead(kind) {
217
+ const read = (this.reads.get(kind) ?? 0) + 1;
218
+ this.reads.set(kind, read);
219
+ return read;
220
+ }
221
+ update(kind, reading) {
222
+ this.readings.set(kind, reading);
223
+ this.report().catch(logged);
224
+ }
225
+ queue(kind, work, signal) {
226
+ const queued = (this.queues.get(kind) ?? Promise.resolve()).then(() => {
227
+ signal?.throwIfAborted();
228
+ return work();
229
+ });
230
+ this.queues.set(kind, queued.catch(() => undefined));
231
+ return signal === undefined ? queued : Promise.race([queued, ended(signal)]);
232
+ }
233
+ async ensure(kind, signal) {
234
+ const failed = await installAdapter(kind, signal);
235
+ if (failed !== null)
236
+ throw new Error(failed);
237
+ const found = await locate(kind, signal);
238
+ if (found !== null)
239
+ return found;
240
+ const installing = this.installing.catch(() => undefined).then(() => installCli(kind, signal));
241
+ this.installing = installing;
242
+ const failure = await installing;
243
+ if (failure !== null)
244
+ throw new Error(failure);
245
+ const installed = await locate(kind, signal);
246
+ if (installed === null)
247
+ throw new Error(`the login shell does not find ${CLIS[kind].bin} after its install`);
248
+ return installed;
249
+ }
250
+ async resolve(kind, signal) {
251
+ let cli;
79
252
  try {
80
- do {
81
- this.again = false;
82
- await this.installing;
83
- await this.report();
84
- } while (this.again);
253
+ cli = await realpath(await this.ensure(kind, signal));
85
254
  }
86
- finally {
87
- this.reporting = null;
255
+ catch (error) {
256
+ const failure = causeOf(error);
257
+ process.stderr.write(`kit: ${kind} did not install: ${failure}\n`);
258
+ return { release: null, failure, ...SIGNED_OUT };
88
259
  }
260
+ const release = await versionOf(cli, signal);
261
+ if (release === null)
262
+ return { release: null, failure: `${cli} --version names no version`, ...SIGNED_OUT };
263
+ const minimum = CLIS[kind].minimum;
264
+ if (below(release, minimum))
265
+ return { release, minimum, failure: null, ...SIGNED_OUT };
266
+ return { cli, release };
267
+ }
268
+ async examine(kind, known, read, signal) {
269
+ const current = this.nextRead(kind);
270
+ const resolved = await this.queue(kind, () => this.resolve(kind, signal), signal);
271
+ const reading = 'cli' in resolved ? { release: resolved.release, failure: null, ...(await read(kind, resolved.cli, known)) } : resolved;
272
+ if (this.reads.get(kind) === current && !signal?.aborted)
273
+ this.readings.set(kind, reading);
274
+ }
275
+ async load(kind) {
276
+ const known = this.readings.get(kind);
277
+ if (known !== undefined && known.failure === null && known.minimum === undefined)
278
+ return;
279
+ await this.examine(kind, SIGNED_OUT, offer);
280
+ }
281
+ async send() {
282
+ const agents = this.desired.agents.flatMap((kind) => {
283
+ const reading = this.readings.get(kind);
284
+ return reading === undefined ? [] : [reported(kind, reading)];
285
+ });
286
+ const hostKey = await sshHostKey();
287
+ const body = {
288
+ os: await operatingSystem(),
289
+ kit_version: KIT_VERSION,
290
+ wire: KIT_WIRE,
291
+ updates_itself: await updatesItself(),
292
+ agent_base: agentBase(),
293
+ agents,
294
+ transcription: transcriptionReport(),
295
+ ...(hostKey === null ? {} : { ssh_host_key: hostKey }),
296
+ };
297
+ const state = JSON.stringify(body);
298
+ if (state === this.sent)
299
+ return;
300
+ await this.house.deliver('/kit/agents/report', body);
301
+ this.sent = state;
89
302
  }
90
303
  }
package/dist/api.js CHANGED
@@ -1,4 +1,9 @@
1
+ import { setTimeout as delay } from 'node:timers/promises';
2
+ import { signInAgain, updateRequired } from './end.js';
1
3
  import { readEnrolment } from './home.js';
4
+ import { retryable } from './refusals.js';
5
+ import { relayed } from './relay.js';
6
+ import { KIT_WIRE, KIT_WIRE_HEADER, tooOld } from './wire.js';
2
7
  export class HouseRefusal extends Error {
3
8
  status;
4
9
  text;
@@ -8,24 +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
+ }
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.
21
+ return Math.random() * Math.min(30_000, 1000 * 2 ** attempt);
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
+ }
11
36
  export async function post(house, path, body, credential, signal) {
12
- const answer = await fetch(new URL(path, house), {
37
+ const answer = await fetch(relayed(new URL(path, house)), {
13
38
  method: 'POST',
14
39
  headers: {
15
40
  'content-type': 'application/json',
41
+ [KIT_WIRE_HEADER]: String(KIT_WIRE),
16
42
  ...(credential === undefined ? {} : { authorization: `Bearer ${credential}` }),
17
43
  },
18
44
  body: JSON.stringify(body),
19
45
  signal,
20
46
  });
21
47
  const text = await answer.text();
48
+ if (!answer.ok && tooOld(text))
49
+ updateRequired(house);
22
50
  if (!answer.ok)
23
51
  throw new HouseRefusal(path, answer.status, text);
24
52
  return JSON.parse(text);
25
53
  }
26
54
  async function enrolledPost(path, body, signal) {
27
55
  const { house, credential } = (await readEnrolment());
28
- 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
+ }
29
64
  }
30
65
  export const house = {
31
66
  post: enrolledPost,
@@ -35,10 +70,11 @@ export const house = {
35
70
  return await enrolledPost(path, body);
36
71
  }
37
72
  catch (error) {
38
- if (error instanceof HouseRefusal && error.status < 500)
73
+ if (finalRefusal(error))
39
74
  throw error;
40
75
  process.stderr.write(`kit: ${path} did not reach House: ${error.message}\n`);
41
- await new Promise((resolve) => setTimeout(resolve, Math.min(30_000, 1000 * 2 ** attempt)));
76
+ // A delivery House did not take is sent again on the jittered curve for as long as it is still current.
77
+ await new Promise((resolve) => setTimeout(resolve, retryDelay(attempt)));
42
78
  if (!current())
43
79
  throw error;
44
80
  }