@agentshouse/kit 0.2.1-c3895fcff473b → 0.2.1-c3cf4b145dcda

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.
package/dist/kit-main.js CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import { setGlobalProxyFromEnv } from 'node:http';
3
+ import { answerGitCredential } from './app-git.js';
3
4
  import { login } from './login.js';
4
5
  import { logout } from './logout.js';
5
6
  import { resident } from './resident.js';
@@ -12,6 +13,8 @@ async function run() {
12
13
  return logout();
13
14
  if (command === 'resident')
14
15
  return resident();
16
+ if (command === 'git-credential')
17
+ return answerGitCredential(argv[0]);
15
18
  process.stderr.write('usage: kit <login|logout|resident>\n');
16
19
  process.exitCode = 2;
17
20
  }
package/dist/login.js CHANGED
@@ -4,6 +4,7 @@ import { hostname } from 'node:os';
4
4
  import { createInterface } from 'node:readline/promises';
5
5
  import { parseArgs } from 'node:util';
6
6
  import { post } from './api.js';
7
+ import { placeAppGit } from './app-git.js';
7
8
  import { forgetOwnAgent, readDisconnected, readEnrolment, readOwnAgent, writeEnrolment, writeOwnAgent } from './home.js';
8
9
  import { callHouse, rpc } from './mcp.js';
9
10
  const DEFAULT_HOUSE = 'https://agents.house';
@@ -104,6 +105,7 @@ export async function login(argv) {
104
105
  process.stdout.write("This computer's own agent was another User's; run kit login again to connect it to you.\n");
105
106
  }
106
107
  };
108
+ const placeGit = async () => placeAppGit(ownAgent, await readOwnAgent());
107
109
  if (values.manual) {
108
110
  const { start_url } = await post(house, '/kit', {
109
111
  ...started,
@@ -111,6 +113,7 @@ export async function login(argv) {
111
113
  });
112
114
  process.stdout.write(`Open this link and confirm: ${start_url}\n`);
113
115
  await exchange(await typed('Code: '));
116
+ await placeGit();
114
117
  return;
115
118
  }
116
119
  const callback = await listen();
@@ -133,4 +136,5 @@ export async function login(argv) {
133
136
  finally {
134
137
  callback.close();
135
138
  }
139
+ await placeGit();
136
140
  }
package/dist/logout.js CHANGED
@@ -1,8 +1,10 @@
1
1
  import { HouseRefusal, post } from './api.js';
2
+ import { forgetAppGit } from './app-git.js';
2
3
  import { forgetEnrolment, readEnrolment } from './home.js';
3
4
  export async function logout() {
4
5
  const enrolled = await readEnrolment();
5
6
  if (enrolled === null) {
7
+ await forgetAppGit();
6
8
  process.stdout.write('This computer is not connected to House.\n');
7
9
  return;
8
10
  }
@@ -14,5 +16,6 @@ export async function logout() {
14
16
  throw error;
15
17
  }
16
18
  await forgetEnrolment(enrolled);
19
+ await forgetAppGit();
17
20
  process.stdout.write(`Environment ${enrolled.environment} is disconnected; kit login reconnects it.\n`);
18
21
  }
package/dist/parts.js ADDED
@@ -0,0 +1,64 @@
1
+ export class Parts {
2
+ parts = [];
3
+ context = null;
4
+ emit;
5
+ writing = null;
6
+ planned = null;
7
+ constructor(emit) {
8
+ this.emit = emit;
9
+ }
10
+ write(type, id, text) {
11
+ if (text === '')
12
+ return;
13
+ const last = this.parts.length - 1;
14
+ const part = this.parts[last];
15
+ if (this.writing?.type === type && this.writing.id === id && part?.type === type) {
16
+ part.text += text;
17
+ this.emit({ op: 'append', index: last, text });
18
+ return;
19
+ }
20
+ this.start({ type, text });
21
+ this.writing = { type, id };
22
+ }
23
+ command() {
24
+ const last = this.parts.length - 1;
25
+ const part = this.parts[last];
26
+ if (part?.type !== 'commands') {
27
+ this.start({ type: 'commands', count: 1 });
28
+ return;
29
+ }
30
+ part.count++;
31
+ this.emit({ op: 'count', index: last, count: part.count });
32
+ }
33
+ start(part) {
34
+ this.writing = null;
35
+ this.parts.push(part);
36
+ const index = this.parts.length - 1;
37
+ this.emit({ op: 'start', index, part });
38
+ return index;
39
+ }
40
+ done(index, result) {
41
+ const part = this.parts[index];
42
+ part.status = 'done';
43
+ part.result = result;
44
+ this.emit({ op: 'status', index, status: 'done', result });
45
+ }
46
+ plan(entries) {
47
+ if (this.planned === null) {
48
+ this.planned = this.start({ type: 'plan', entries });
49
+ return;
50
+ }
51
+ this.parts[this.planned].entries = entries;
52
+ this.emit({ op: 'plan', index: this.planned, entries });
53
+ }
54
+ measure(context) {
55
+ this.context = context;
56
+ this.emit({ op: 'context', ...context });
57
+ }
58
+ operations() {
59
+ return [
60
+ ...this.parts.map((part, index) => ({ op: 'start', index, part })),
61
+ ...(this.context === null ? [] : [{ op: 'context', ...this.context }]),
62
+ ];
63
+ }
64
+ }
package/dist/refusals.js CHANGED
@@ -21,6 +21,18 @@ function spoken(text) {
21
21
  const detail = said.indexOf('\n');
22
22
  return { code: head[1], text: said, conflicts: detail === -1 ? [] : conflictCodes(said.slice(detail + 1)) };
23
23
  }
24
+ export function retryable(text) {
25
+ let body;
26
+ try {
27
+ body = JSON.parse(text);
28
+ }
29
+ catch {
30
+ return null;
31
+ }
32
+ const error = body?.error;
33
+ const verdict = error?.retryable ?? error?.data?.retryable;
34
+ return typeof verdict === 'boolean' ? verdict : null;
35
+ }
24
36
  export function refusalOf(text) {
25
37
  let body;
26
38
  try {
package/dist/resident.js CHANGED
@@ -11,6 +11,7 @@ import { received } from './bridge.js';
11
11
  import { Conversations } from './conversations.js';
12
12
  import { LocalCopies } from './copies.js';
13
13
  import { agentBase, enrolled, kitHome } from './home.js';
14
+ import { quoted } from './shell.js';
14
15
  import { SignIns } from './sign-in.js';
15
16
  import { placeSkillSet } from './skills.js';
16
17
  import { holdStream } from './stream.js';
@@ -21,9 +22,6 @@ function report(error) {
21
22
  function logged(work) {
22
23
  work.catch(report);
23
24
  }
24
- function quoted(value) {
25
- return `'${value.replaceAll("'", `'\\''`)}'`;
26
- }
27
25
  async function installShim() {
28
26
  const main = fileURLToPath(new URL(`./git-main${extname(fileURLToPath(import.meta.url))}`, import.meta.url));
29
27
  await mkdir(join(kitHome(), 'shim'), { recursive: true });
@@ -120,6 +118,8 @@ export async function resident() {
120
118
  logged(copies.select());
121
119
  },
122
120
  frame: (arrived) => {
121
+ if (arrived.type === 'watching')
122
+ conversations.watching(arrived.watching === true);
123
123
  if (arrived.type === 'work_available' && arrived.subject === 'agents')
124
124
  logged(agents.refresh());
125
125
  if (arrived.type === 'work_available' && arrived.subject === 'local_copy')
@@ -1,4 +1,4 @@
1
- import { retryDelay } from './api.js';
1
+ import { finalRefusal, retryDelay } from './api.js';
2
2
  export async function holdSecretInput(house, subject, steps, waiting) {
3
3
  for (let failures = 0; waiting();) {
4
4
  try {
@@ -10,7 +10,10 @@ export async function holdSecretInput(house, subject, steps, waiting) {
10
10
  failures = 0;
11
11
  }
12
12
  catch (error) {
13
+ if (finalRefusal(error))
14
+ return null;
13
15
  process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
16
+ // A wait that did not reach House is held again on the jittered curve for as long as its asker still waits.
14
17
  await new Promise((resolve) => setTimeout(resolve, retryDelay(failures++)));
15
18
  }
16
19
  }
package/dist/shell.js ADDED
@@ -0,0 +1,3 @@
1
+ export function quoted(value) {
2
+ return `'${value.replaceAll("'", `'\\''`)}'`;
3
+ }
package/dist/sign-in.js CHANGED
@@ -12,6 +12,9 @@ function causeOf(error) {
12
12
  function logged(error) {
13
13
  process.stderr.write(`kit: ${causeOf(error)}\n`);
14
14
  }
15
+ function endOf(signal) {
16
+ return new Promise((resolve) => signal.addEventListener('abort', () => resolve(true), { once: true }));
17
+ }
15
18
  export class SignIns {
16
19
  house;
17
20
  agents;
@@ -30,24 +33,43 @@ export class SignIns {
30
33
  return;
31
34
  this.running.add(input.input_id);
32
35
  this.changed();
33
- this.login(input)
34
- .then(() => this.agents.reread(input.cli))
35
- .then(() => this.house.deliver(`/kit/inputs/${input.input_id}/ack`, {}))
36
+ this.signIn(input)
36
37
  .catch(logged)
37
38
  .finally(() => {
38
39
  this.running.delete(input.input_id);
39
40
  this.changed();
40
41
  });
41
42
  }
42
- async login(input) {
43
+ async signIn(input) {
44
+ const ending = new AbortController();
45
+ // 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.
46
+ const timer = setTimeout(() => ending.abort(), input.ends_in * 1000);
47
+ const ended = await Promise.race([this.signedIn(input, ending.signal), endOf(ending.signal)]);
48
+ clearTimeout(timer);
49
+ if (ended)
50
+ process.stderr.write(`kit: the ${input.cli} sign-in reached the end House set for it\n`);
51
+ await this.house.deliver(`/kit/inputs/${input.input_id}/ack`, {});
52
+ if (!ended)
53
+ await this.agents.reread(input.cli);
54
+ }
55
+ async signedIn(input, signal) {
56
+ await this.login(input, signal);
57
+ if (!signal.aborted)
58
+ await this.agents.recheck(input.cli, signal);
59
+ return false;
60
+ }
61
+ async login(input, signal) {
43
62
  let cli;
44
63
  try {
45
- cli = await this.agents.located(input.cli);
64
+ cli = await this.agents.located(input.cli, signal);
46
65
  }
47
66
  catch (error) {
48
- process.stderr.write(`kit: the ${input.cli} sign-in failed: ${causeOf(error)}\n`);
67
+ if (!signal.aborted)
68
+ process.stderr.write(`kit: the ${input.cli} sign-in failed: ${causeOf(error)}\n`);
49
69
  return;
50
70
  }
71
+ if (signal.aborted)
72
+ return;
51
73
  const login = CLIS[input.cli].login;
52
74
  const child = spawn(cli, login.args, {
53
75
  stdio: ['pipe', 'pipe', 'pipe'],
@@ -55,6 +77,8 @@ export class SignIns {
55
77
  env: withoutProxy(),
56
78
  });
57
79
  const running = () => child.exitCode === null && child.signalCode === null;
80
+ const stop = () => killTree(child);
81
+ signal.addEventListener('abort', stop, { once: true });
58
82
  let link;
59
83
  let code;
60
84
  let last = '';
@@ -91,9 +115,10 @@ export class SignIns {
91
115
  createInterface({ input: output }).on('line', read);
92
116
  const failed = await new Promise((resolve) => {
93
117
  child.on('error', (error) => resolve(error.message));
94
- child.on('close', (status, signal) => resolve(status === 0 ? null : `exited with ${status ?? signal}${last ? `: ${last}` : ''}`));
118
+ child.on('close', (status, killed) => resolve(status === 0 ? null : `exited with ${status ?? killed}${last ? `: ${last}` : ''}`));
95
119
  });
96
- if (failed !== null)
120
+ signal.removeEventListener('abort', stop);
121
+ if (failed !== null && !signal.aborted)
97
122
  process.stderr.write(`kit: the ${input.cli} sign-in failed: ${failed}\n`);
98
123
  }
99
124
  }
package/dist/stream.js CHANGED
@@ -1,17 +1,36 @@
1
- import { retryDelay } from './api.js';
1
+ import { subscribe } from 'node:diagnostics_channel';
2
+ import { finalRefusal, HouseRefusal, retryDelay } from './api.js';
3
+ import { end, REPLACED, SIGN_IN_AGAIN } from './end.js';
2
4
  import { readEnrolment } from './home.js';
3
5
  import { relayed } from './relay.js';
6
+ const PATH = '/kit/stream';
4
7
  const SUBPROTOCOL = 'house.kit.stream.1';
8
+ const REFUSAL_HEADER = 'x-house-refusal';
5
9
  function streamUrl(house) {
6
- const url = new URL('/kit/stream', house);
10
+ const url = new URL(PATH, house);
7
11
  url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
8
12
  return url;
9
13
  }
14
+ function header(headers, name) {
15
+ for (let index = 0; index < headers.length; index += 2) {
16
+ if (headers[index].toString('latin1').toLowerCase() === name)
17
+ return headers[index + 1].toString('latin1');
18
+ }
19
+ return '';
20
+ }
21
+ let refused = null;
22
+ subscribe('undici:request:headers', (message) => {
23
+ const { request, response } = message;
24
+ if (!request.path.endsWith(PATH))
25
+ return;
26
+ refused = new HouseRefusal(PATH, response.statusCode, header(response.headers, REFUSAL_HEADER) || response.statusText);
27
+ });
10
28
  export function holdStream(handlers) {
11
29
  let socket = null;
12
30
  let failures = 0;
13
31
  const connect = async () => {
14
32
  const { house, credential } = (await readEnrolment());
33
+ refused = null;
15
34
  const opening = new WebSocket(relayed(streamUrl(house)), {
16
35
  protocols: [SUBPROTOCOL],
17
36
  headers: { authorization: `Bearer ${credential}` },
@@ -31,8 +50,14 @@ export function holdStream(handlers) {
31
50
  if (socket === opening)
32
51
  socket = null;
33
52
  process.stderr.write(`kit: control stream closed ${event.code} ${event.reason}\n`);
34
- const delay = opened ? 1000 + Math.random() * 2000 : retryDelay(failures++);
35
- setTimeout(() => void connect(), delay);
53
+ if (event.reason === 'replaced')
54
+ end(REPLACED);
55
+ if (event.reason === 'credential_rejected' || refused?.status === 401)
56
+ end(SIGN_IN_AGAIN);
57
+ if (finalRefusal(refused))
58
+ end(refused.message);
59
+ // A closed socket reopens in one to three seconds, spread so Kits one restart closed return apart; an opening House refused as retryable or left unanswered backs off on the jittered curve.
60
+ setTimeout(() => void connect(), opened ? 1000 + Math.random() * 2000 : retryDelay(failures++));
36
61
  };
37
62
  };
38
63
  void connect();
@@ -29,6 +29,10 @@ export const ENGINES = {
29
29
  };
30
30
  const CHUNK_SECONDS = 20;
31
31
  let held = Promise.resolve();
32
+ let holding = Promise.resolve();
33
+ let setting = null;
34
+ let downloading = null;
35
+ let readiness = { installed: false, failure: null };
32
36
  function directory() {
33
37
  return join(kitHome(), 'transcription');
34
38
  }
@@ -36,6 +40,7 @@ function installed(artifact) {
36
40
  return join(directory(), artifact.sha256);
37
41
  }
38
42
  async function executed(command, args, signal) {
43
+ // A download or a transcription runs as long as its size needs; the setting turning off or the turn's interrupt or end aborts it instead.
39
44
  const ran = await run(command, args, 0, false, signal);
40
45
  if (ran.status !== 0)
41
46
  throw new Error(failureOf(ran, basename(command)));
@@ -54,6 +59,12 @@ async function sha256(path) {
54
59
  await pipeline(createReadStream(path), hash);
55
60
  return hash.digest('hex');
56
61
  }
62
+ function causeOf(error) {
63
+ return error instanceof Error ? error.message : String(error);
64
+ }
65
+ function pinnedHere() {
66
+ return ENGINES[`${process.platform}-${process.arch}`];
67
+ }
57
68
  async function install(artifacts, signal) {
58
69
  await mkdir(directory(), { recursive: true });
59
70
  const pinned = new Set(artifacts.map((artifact) => artifact.sha256));
@@ -68,6 +79,7 @@ async function install(artifacts, signal) {
68
79
  try {
69
80
  for (const artifact of missing) {
70
81
  const part = `${installed(artifact)}.part`;
82
+ // Three retries carry a large download past a dropped connection before the download fails.
71
83
  await executed('curl', ['-fsSL', '--retry', '3', '-o', part, artifact.url], signal);
72
84
  if ((await sha256(part)) !== artifact.sha256)
73
85
  throw new Error(`${artifact.url} does not match its sha256`);
@@ -83,12 +95,46 @@ async function install(artifacts, signal) {
83
95
  await rm(`${installed(artifact)}.part`, { force: true });
84
96
  }
85
97
  }
98
+ function reported(next) {
99
+ const changed = next.installed !== readiness.installed || next.failure !== readiness.failure;
100
+ readiness = next;
101
+ return changed;
102
+ }
103
+ async function settle(transcribe) {
104
+ if (transcribe === setting)
105
+ return false;
106
+ setting = transcribe;
107
+ if (!transcribe) {
108
+ await rm(directory(), { recursive: true, force: true });
109
+ return reported({ installed: false, failure: null });
110
+ }
111
+ const download = new AbortController();
112
+ downloading = download;
113
+ try {
114
+ const pinned = pinnedHere();
115
+ await install([pinned.decoder, pinned.engine, MODEL], download.signal);
116
+ return reported({ installed: true, failure: null });
117
+ }
118
+ catch (error) {
119
+ await rm(directory(), { recursive: true, force: true });
120
+ return reported({ installed: false, failure: download.signal.aborted ? null : causeOf(error) });
121
+ }
122
+ finally {
123
+ downloading = null;
124
+ }
125
+ }
126
+ export function holdTranscription(transcribe) {
127
+ if (!transcribe)
128
+ downloading?.abort();
129
+ const settled = holding.then(() => settle(transcribe));
130
+ holding = settled.catch(() => undefined);
131
+ return settled;
132
+ }
133
+ export function transcriptionReport() {
134
+ return readiness;
135
+ }
86
136
  async function transcribed(path, signal) {
87
- const platform = `${process.platform}-${process.arch}`;
88
- const pinned = ENGINES[platform];
89
- if (pinned === undefined)
90
- throw new Error(`no transcription engine is pinned for ${platform}`);
91
- await install([pinned.decoder, pinned.engine, MODEL], signal);
137
+ const pinned = pinnedHere();
92
138
  const work = await mkdtemp(join(tmpdir(), 'kit-transcription-'));
93
139
  try {
94
140
  await executed(installed(pinned.decoder), ['-nostdin', '-hide_banner', '-loglevel', 'error', '-i', path, '-ar', '16000', '-ac', '1', '-c:a', 'pcm_s16le',
@@ -106,6 +152,8 @@ async function transcribed(path, signal) {
106
152
  }
107
153
  }
108
154
  export async function transcribe(path, signal) {
155
+ if (!readiness.installed)
156
+ return null;
109
157
  const before = held;
110
158
  let release;
111
159
  const mine = new Promise((resolve) => {
@@ -114,7 +162,7 @@ export async function transcribe(path, signal) {
114
162
  held = Promise.all([before, mine]);
115
163
  try {
116
164
  await unlessAborted(before, signal);
117
- return await transcribed(path, signal);
165
+ return readiness.installed ? await transcribed(path, signal) : null;
118
166
  }
119
167
  finally {
120
168
  release();
package/dist/update.js CHANGED
@@ -40,9 +40,11 @@ async function finished(inputId) {
40
40
  const ack = await recorded();
41
41
  if (ack !== null)
42
42
  return ack;
43
+ // systemctl answers at once, so ten seconds only bounds a stuck service manager.
43
44
  if ((await run('systemctl', ['is-active', '--quiet', unit(inputId)], 10_000)).status !== 0) {
44
45
  return (await recorded()) ?? { refused: 'the Kit bootstrap did not finish' };
45
46
  }
47
+ // The bootstrap runs for minutes, so checking each second sees its outcome within a second at little cost.
46
48
  await new Promise((resolve) => setTimeout(resolve, 1000));
47
49
  }
48
50
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentshouse/kit",
3
- "version": "0.2.1-c3895fcff473b",
3
+ "version": "0.2.1-c3cf4b145dcda",
4
4
  "description": "House Kit connects an Environment to agents.house.",
5
5
  "license": "MIT",
6
6
  "repository": {
package/dist/blocks.js DELETED
@@ -1,51 +0,0 @@
1
- const FENCE = /^\s*(`{3,}|~{3,})\s*(\S*)/;
2
- export function blocksOf(text) {
3
- const blocks = [];
4
- let paragraph = [];
5
- let fence = null;
6
- const closeParagraph = () => {
7
- const joined = paragraph.join('\n').trim();
8
- if (joined.length > 0)
9
- blocks.push({ type: 'paragraph', text: joined });
10
- paragraph = [];
11
- };
12
- const closeFence = () => {
13
- const joined = fence.lines.join('\n');
14
- if (joined.length > 0)
15
- blocks.push({ type: 'code', language: fence.language, text: joined });
16
- fence = null;
17
- };
18
- for (const line of text.split('\n')) {
19
- if (fence !== null) {
20
- if (line.trim().startsWith(fence.marker))
21
- closeFence();
22
- else
23
- fence.lines.push(line);
24
- continue;
25
- }
26
- const opening = FENCE.exec(line);
27
- if (opening !== null) {
28
- closeParagraph();
29
- fence = { marker: opening[1], language: opening[2] || null, lines: [] };
30
- }
31
- else if (line.trim().length === 0) {
32
- closeParagraph();
33
- }
34
- else {
35
- paragraph.push(line);
36
- }
37
- }
38
- if (fence !== null)
39
- closeFence();
40
- closeParagraph();
41
- return blocks;
42
- }
43
- export function firstChange(before, after) {
44
- let index = 0;
45
- while (index < before.length &&
46
- index < after.length &&
47
- JSON.stringify(before[index]) === JSON.stringify(after[index])) {
48
- index++;
49
- }
50
- return index;
51
- }