@agentshouse/kit 0.2.1-c8179f06883b6 → 0.2.1-c83d468784cc9

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 (133) hide show
  1. package/README.md +37 -36
  2. package/dist/acp.js +90 -19
  3. package/dist/afk.js +17 -0
  4. package/dist/agents.js +154 -48
  5. package/dist/api.js +5 -5
  6. package/dist/app-git.js +10 -26
  7. package/dist/authority-main.js +3 -3
  8. package/dist/bridge.js +7 -10
  9. package/dist/building.js +50 -0
  10. package/dist/calls.js +71 -0
  11. package/dist/chatgpt.js +32 -0
  12. package/dist/chunks.js +49 -0
  13. package/dist/clis.js +57 -42
  14. package/dist/connection.js +105 -0
  15. package/dist/conversations.js +572 -182
  16. package/dist/copies.js +350 -162
  17. package/dist/end.js +15 -4
  18. package/dist/events.js +70 -8
  19. package/dist/files.js +139 -31
  20. package/dist/git.js +57 -21
  21. package/dist/home.js +56 -12
  22. package/dist/house-cli.js +299 -0
  23. package/dist/house-main.js +7 -228
  24. package/dist/instructions.js +7 -11
  25. package/dist/kit-cli.js +68 -0
  26. package/dist/kit-main.js +8 -67
  27. package/dist/login.js +43 -19
  28. package/dist/logout.js +1 -3
  29. package/dist/mcp.js +6 -2
  30. package/dist/parts.js +10 -0
  31. package/dist/placement.js +19 -36
  32. package/dist/relay.js +21 -0
  33. package/dist/resident.js +100 -44
  34. package/dist/scope.js +15 -4
  35. package/dist/scripts.js +255 -0
  36. package/dist/secret-input.js +1 -1
  37. package/dist/service.js +23 -1
  38. package/dist/sign-in.js +22 -39
  39. package/dist/silence.js +28 -0
  40. package/dist/skills.js +49 -35
  41. package/dist/transcription.js +85 -45
  42. package/dist/translate.js +2 -8
  43. package/dist/update.js +113 -126
  44. package/dist/wire.js +37 -3
  45. package/dist/work.js +3 -5
  46. package/package.json +20 -20
  47. package/dist/bootstrap.json +0 -24
  48. package/dist/clis-main.js +0 -23
  49. package/dist/codex.js +0 -61
  50. package/dist/stream.js +0 -77
  51. package/how-we-work.md +0 -23
  52. package/skills/ask-matt/LICENSE +0 -21
  53. package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
  54. package/skills/ask-matt/SKILL.md +0 -91
  55. package/skills/ask-matt/agents/openai.yaml +0 -4
  56. package/skills/code-review/LICENSE +0 -21
  57. package/skills/code-review/SKILL.md +0 -89
  58. package/skills/code-review/agents/openai.yaml +0 -3
  59. package/skills/codebase-design/DEEPENING.md +0 -37
  60. package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
  61. package/skills/codebase-design/LICENSE +0 -21
  62. package/skills/codebase-design/SKILL.md +0 -116
  63. package/skills/codebase-design/agents/openai.yaml +0 -3
  64. package/skills/diagnosing-bugs/LICENSE +0 -21
  65. package/skills/diagnosing-bugs/SKILL.md +0 -140
  66. package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
  67. package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
  68. package/skills/domain-modeling/ADR-FORMAT.md +0 -47
  69. package/skills/domain-modeling/CONTEXT-FORMAT.md +0 -60
  70. package/skills/domain-modeling/LICENSE +0 -21
  71. package/skills/domain-modeling/SKILL.md +0 -76
  72. package/skills/domain-modeling/agents/openai.yaml +0 -3
  73. package/skills/grill-me/LICENSE +0 -21
  74. package/skills/grill-me/SKILL.md +0 -8
  75. package/skills/grill-me/agents/openai.yaml +0 -4
  76. package/skills/grill-with-docs/LICENSE +0 -21
  77. package/skills/grill-with-docs/SKILL.md +0 -8
  78. package/skills/grill-with-docs/agents/openai.yaml +0 -4
  79. package/skills/grilling/LICENSE +0 -21
  80. package/skills/grilling/SKILL.md +0 -30
  81. package/skills/grilling/agents/openai.yaml +0 -3
  82. package/skills/handoff/LICENSE +0 -21
  83. package/skills/handoff/SKILL.md +0 -17
  84. package/skills/handoff/agents/openai.yaml +0 -4
  85. package/skills/implement/LICENSE +0 -21
  86. package/skills/implement/SKILL.md +0 -16
  87. package/skills/implement/agents/openai.yaml +0 -4
  88. package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
  89. package/skills/improve-codebase-architecture/LICENSE +0 -21
  90. package/skills/improve-codebase-architecture/SKILL.md +0 -72
  91. package/skills/improve-codebase-architecture/agents/openai.yaml +0 -4
  92. package/skills/prototype/LICENSE +0 -21
  93. package/skills/prototype/LOGIC.md +0 -67
  94. package/skills/prototype/SKILL.md +0 -28
  95. package/skills/prototype/UI.md +0 -112
  96. package/skills/prototype/agents/openai.yaml +0 -3
  97. package/skills/research/LICENSE +0 -21
  98. package/skills/research/SKILL.md +0 -14
  99. package/skills/research/agents/openai.yaml +0 -3
  100. package/skills/resolving-merge-conflicts/LICENSE +0 -21
  101. package/skills/resolving-merge-conflicts/SKILL.md +0 -16
  102. package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
  103. package/skills/tdd/LICENSE +0 -21
  104. package/skills/tdd/SKILL.md +0 -40
  105. package/skills/tdd/agents/openai.yaml +0 -3
  106. package/skills/tdd/mocking.md +0 -59
  107. package/skills/tdd/tests.md +0 -77
  108. package/skills/teach/GLOSSARY-FORMAT.md +0 -35
  109. package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
  110. package/skills/teach/LICENSE +0 -21
  111. package/skills/teach/MISSION-FORMAT.md +0 -31
  112. package/skills/teach/RESOURCES-FORMAT.md +0 -32
  113. package/skills/teach/SKILL.md +0 -141
  114. package/skills/teach/agents/openai.yaml +0 -4
  115. package/skills/to-questionnaire/LICENSE +0 -21
  116. package/skills/to-questionnaire/SKILL.md +0 -55
  117. package/skills/to-questionnaire/agents/openai.yaml +0 -4
  118. package/skills/to-spec/LICENSE +0 -21
  119. package/skills/to-spec/SKILL.md +0 -76
  120. package/skills/to-spec/agents/openai.yaml +0 -4
  121. package/skills/to-tickets/LICENSE +0 -21
  122. package/skills/to-tickets/SKILL.md +0 -106
  123. package/skills/to-tickets/agents/openai.yaml +0 -4
  124. package/skills/wait-what/LICENSE +0 -21
  125. package/skills/wait-what/SKILL.md +0 -8
  126. package/skills/wait-what/agents/openai.yaml +0 -4
  127. package/skills/wayfinder/LICENSE +0 -21
  128. package/skills/wayfinder/SKILL.md +0 -129
  129. package/skills/wayfinder/agents/openai.yaml +0 -4
  130. package/skills/writing-for-agents/LICENSE +0 -21
  131. package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
  132. package/skills/writing-for-agents/SKILL.md +0 -83
  133. package/skills/writing-for-agents/agents/openai.yaml +0 -3
package/dist/kit-main.js CHANGED
@@ -1,69 +1,10 @@
1
1
  #!/usr/bin/env node
2
- import { setGlobalProxyFromEnv } from 'node:http';
3
- import { parseArgs } from 'node:util';
4
- import { answerGitCredential } from './app-git.js';
5
- import { codex } from './codex.js';
6
- import { login } from './login.js';
7
- import { logout } from './logout.js';
8
- import { resident } from './resident.js';
9
- const USAGE = 'usage: kit <login|logout|resident|codex>\n';
10
- const COMMANDS = new Map([
11
- [
12
- 'logout',
13
- {
14
- usage: 'usage: kit logout',
15
- about: "disconnects this computer, stops House Kit and ends your own agent's connection",
16
- operands: 0,
17
- run: logout,
18
- },
19
- ],
20
- [
21
- 'resident',
22
- {
23
- usage: 'usage: kit resident',
24
- about: "keeps this Environment connected; House Kit's service runs it",
25
- operands: 0,
26
- run: resident,
27
- },
28
- ],
29
- [
30
- 'git-credential',
31
- {
32
- usage: 'usage: kit git-credential <get|store|erase>',
33
- about: "answers git with your own agent's House credential; git runs it",
34
- operands: 1,
35
- run: ([operation]) => answerGitCredential(operation),
36
- },
37
- ],
38
- ]);
39
- const [command, ...argv] = process.argv.slice(2);
40
- setGlobalProxyFromEnv();
41
- async function run() {
42
- if (command === 'login')
43
- return login(argv);
44
- if (command === 'codex')
45
- return codex(argv);
46
- if (command === '--help') {
47
- parseArgs({ args: argv });
48
- process.stdout.write(USAGE);
49
- return;
50
- }
51
- const chosen = COMMANDS.get(command ?? '');
52
- if (chosen === undefined) {
53
- process.stderr.write(USAGE);
54
- process.exitCode = 2;
55
- return;
56
- }
57
- const { values, positionals } = parseArgs({ args: argv, options: { help: { type: 'boolean', default: false } }, allowPositionals: true });
58
- if (positionals.length > chosen.operands)
59
- throw new Error(`unexpected argument ${positionals[chosen.operands]}; ${chosen.usage}`);
60
- if (values.help) {
61
- process.stdout.write(`${chosen.usage}\n ${chosen.about}\n`);
62
- return;
63
- }
64
- return chosen.run(positionals);
65
- }
66
- run().catch((error) => {
67
- process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
2
+ const [major = 0, minor = 0, patch = 0] = process.versions.node.split('.').map(Number);
3
+ if (major < 24 || (major === 24 && (minor < 22 || (minor === 22 && patch < 1)))) {
4
+ process.stderr.write(`kit: the Kit needs Node 24.22.1 or newer; this is ${process.versions.node}\n`);
68
5
  process.exitCode = 1;
69
- });
6
+ }
7
+ else {
8
+ await import('./kit-cli.js');
9
+ }
10
+ export {};
package/dist/login.js CHANGED
@@ -1,15 +1,16 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { createHash, randomBytes } from 'node:crypto';
3
- import { hostname } from 'node:os';
4
3
  import { parseArgs } from 'node:util';
5
- import { post, retrying } from './api.js';
4
+ import { HouseRefusal, post, retrying } from './api.js';
6
5
  import { placeAppGit } from './app-git.js';
7
6
  import { writeCodexRule } from './clis.js';
8
- import { forgetOwnAgent, readDisconnected, readEnrolment, readOwnAgent, writeEnrolment, writeOwnAgent } from './home.js';
7
+ import { forgetDisconnected, forgetOwnAgent, readDisconnected, readEnrolment, readOwnAgent, writeEnrolment, writeOwnAgent, } from './home.js';
9
8
  import { callHouse, rpc } from './mcp.js';
9
+ import { refusalOf } from './refusals.js';
10
10
  import { selectRelay } from './relay.js';
11
11
  import { startService } from './service.js';
12
12
  import { placeSkillSet } from './skills.js';
13
+ import { endUnserved } from './wire.js';
13
14
  export const DEFAULT_HOUSE = 'https://agents.house';
14
15
  const OPENER = process.platform === 'darwin' ? 'open' : 'xdg-open';
15
16
  // Polling every two seconds brings the User's confirmation to the waiting Kit moments after they give it.
@@ -25,6 +26,7 @@ export async function accepted(house, credential) {
25
26
  return true;
26
27
  if (checked.status === 401)
27
28
  return false;
29
+ endUnserved(checked.text);
28
30
  throw new Error(`House answered ${checked.status} when Kit checked this computer's own agent's connection: ${checked.text}`);
29
31
  }
30
32
  const USAGE = `usage: kit login [--house <origin>] [--environment <id>] [--rooms <ref>[,<ref>...] | --no-private]
@@ -35,6 +37,16 @@ const USAGE = `usage: kit login [--house <origin>] [--environment <id>] [--rooms
35
37
  --no-private give this Environment every Room but your Private Room
36
38
  --code <code> connect this machine with a login code
37
39
  `;
40
+ function connected(house, named, environment) {
41
+ if (named === undefined)
42
+ return `Environment ${environment} is connected to ${house}.\n`;
43
+ if (named === environment)
44
+ return `Environment ${environment} is reconnected to ${house}.\n`;
45
+ return `Environment ${named} stays with its account; this computer is connected to ${house} as new Environment ${environment} of the account that confirmed.\n`;
46
+ }
47
+ function gone(error) {
48
+ return error instanceof HouseRefusal && refusalOf(error.text)?.code === 'kit_environment_not_found';
49
+ }
38
50
  function chosenRooms(rooms, noPrivate) {
39
51
  if (rooms !== undefined && noPrivate)
40
52
  throw new Error('--rooms and --no-private are two choices of Rooms; pass one');
@@ -73,51 +85,63 @@ export async function login(argv) {
73
85
  selectRelay(relay);
74
86
  const house = values.house ?? enrolled?.house ?? DEFAULT_HOUSE;
75
87
  const replaces = process.env.HOUSE_KIT_REPLACES;
76
- const environment = replaces === undefined ? (values.environment ?? (enrolled?.house === house ? enrolled.environment : undefined)) : undefined;
88
+ const remembered = replaces === undefined && values.environment === undefined && enrolled?.house === house ? enrolled.environment : undefined;
89
+ const environment = replaces === undefined ? (values.environment ?? remembered) : undefined;
77
90
  const ownAgent = await readOwnAgent();
78
91
  let held = false;
92
+ let named = environment;
79
93
  let exchanged;
80
94
  if (values.code !== undefined) {
81
- exchanged = await post(house, '/kit/token', { code: values.code, label: hostname() });
95
+ exchanged = await post(house, '/kit/token', { code: values.code });
82
96
  }
83
97
  else {
84
98
  held = ownAgent !== null && ownAgent.house === house && (await accepted(house, ownAgent.credential));
85
99
  const verifier = randomBytes(32).toString('base64url');
86
- const started = await retrying(() => post(house, '/kit', {
87
- label: hostname(),
88
- ...(environment === undefined
100
+ const start = () => retrying(() => post(house, '/kit', {
101
+ ...(named === undefined
89
102
  ? { act: 'new_environment', ...(replaces === undefined ? {} : { replaces }) }
90
- : { act: 'existing_environment', environment_id: environment }),
103
+ : { act: 'existing_environment', environment_id: named }),
91
104
  ...rooms,
92
105
  code_challenge: createHash('sha256').update(verifier).digest('base64url'),
93
106
  ...(held ? {} : { headless: true }),
94
107
  }));
108
+ let started;
109
+ try {
110
+ started = await start();
111
+ }
112
+ catch (error) {
113
+ if (remembered === undefined || !gone(error))
114
+ throw error;
115
+ await forgetDisconnected();
116
+ process.stdout.write(`House no longer holds Environment ${remembered}; connecting this computer as a new one.\n`);
117
+ named = undefined;
118
+ started = await start();
119
+ }
95
120
  process.stdout.write(`Open this link and confirm: ${started.start_url}\n`);
96
121
  open(started.start_url);
97
122
  exchanged = await retrying(() => post(house, '/kit/token', { login: started.login, code_verifier: verifier }), undefined, () => POLL_INTERVAL_MS);
98
123
  }
99
- try {
100
- await placeSkillSet();
101
- }
102
- catch (error) {
103
- process.stderr.write(`kit: the skills could not be placed: ${error.message}\n`);
104
- }
124
+ await placeSkillSet(house);
105
125
  await writeCodexRule().catch((error) => process.stderr.write(`kit: ${error.message}\n`));
126
+ const relayRecord = relay === undefined ? {} : { relay };
106
127
  await writeEnrolment({
107
128
  house,
108
- ...(relay === undefined ? {} : { relay }),
129
+ ...relayRecord,
109
130
  environment: exchanged.environment,
110
131
  credential: exchanged.credential,
111
132
  });
112
- process.stdout.write(`Environment ${exchanged.environment} is connected to ${house}.\n`);
133
+ process.stdout.write(connected(house, named, exchanged.environment));
113
134
  if (exchanged.headless_credential !== undefined) {
114
- await writeOwnAgent({ house, user: exchanged.user, credential: exchanged.headless_credential });
135
+ await writeOwnAgent({ house, ...relayRecord, user: exchanged.user, credential: exchanged.headless_credential });
115
136
  }
116
137
  else if (held && ownAgent !== null && ownAgent.user !== exchanged.user) {
117
138
  await forgetOwnAgent();
118
139
  process.stdout.write("This computer's own agent was another User's; run kit login again to connect it to you.\n");
119
140
  }
120
- await placeAppGit(ownAgent, await readOwnAgent());
141
+ else if (held && ownAgent !== null) {
142
+ await writeOwnAgent({ house, ...relayRecord, user: ownAgent.user, credential: ownAgent.credential });
143
+ }
144
+ await placeAppGit(await readOwnAgent()).catch((error) => process.stderr.write(`kit: ${error.message}\n`));
121
145
  try {
122
146
  startService();
123
147
  }
package/dist/logout.js CHANGED
@@ -1,11 +1,10 @@
1
1
  import { HouseRefusal, post } from './api.js';
2
- import { forgetAppGit } from './app-git.js';
3
2
  import { forgetEnrolment, forgetOwnAgent, readEnrolment } from './home.js';
4
3
  export async function logout() {
5
4
  const enrolled = await readEnrolment();
6
5
  if (enrolled !== null) {
7
6
  try {
8
- await post(enrolled.house, '/kit/logout', {}, enrolled.credential);
7
+ await post(enrolled.house, '/kit/kits/log-out', {}, enrolled.credential);
9
8
  }
10
9
  catch (error) {
11
10
  if (!(error instanceof HouseRefusal && error.status === 401))
@@ -13,7 +12,6 @@ export async function logout() {
13
12
  }
14
13
  await forgetEnrolment(enrolled);
15
14
  }
16
- await forgetAppGit();
17
15
  await forgetOwnAgent();
18
16
  process.stdout.write(enrolled === null
19
17
  ? 'This computer is not connected to House.\n'
package/dist/mcp.js CHANGED
@@ -1,8 +1,11 @@
1
1
  import { randomUUID } from 'node:crypto';
2
- import { KIT_VERSION } from './clis.js';
2
+ import { KIT_RELEASE } from './clis.js';
3
3
  import { relayed } from './relay.js';
4
+ import { KIT_WIRE, KIT_WIRE_HEADER } from './wire.js';
4
5
  const MCP_PROTOCOL_VERSION = '2026-07-28';
5
6
  export const UNREACHABLE = 'House is unreachable; call again in a minute';
7
+ export class Unsettled extends Error {
8
+ }
6
9
  export function rpc(method, params) {
7
10
  return { jsonrpc: '2.0', id: randomUUID(), method, params };
8
11
  }
@@ -13,7 +16,7 @@ export function mcpBody(message, operation) {
13
16
  ...message.params,
14
17
  _meta: {
15
18
  'io.modelcontextprotocol/protocolVersion': MCP_PROTOCOL_VERSION,
16
- 'io.modelcontextprotocol/clientInfo': { name: '@agentshouse/kit', version: KIT_VERSION },
19
+ 'io.modelcontextprotocol/clientInfo': { name: '@agentshouse/kit', version: KIT_RELEASE },
17
20
  'io.modelcontextprotocol/clientCapabilities': {},
18
21
  ...(operation === null ? {} : { 'agents.house/agent-operation': operation }),
19
22
  },
@@ -31,6 +34,7 @@ export async function callHouse(origin, credential, message, operation = null, s
31
34
  'mcp-protocol-version': MCP_PROTOCOL_VERSION,
32
35
  'mcp-method': message.method,
33
36
  ...(tool === null ? {} : { 'mcp-name': tool }),
37
+ [KIT_WIRE_HEADER]: String(KIT_WIRE),
34
38
  },
35
39
  body: mcpBody(message, operation),
36
40
  signal,
package/dist/parts.js CHANGED
@@ -1,3 +1,13 @@
1
+ export function addedTokens(sum, tokens) {
2
+ if (sum === null)
3
+ return tokens;
4
+ return {
5
+ input: sum.input + tokens.input,
6
+ output: sum.output + tokens.output,
7
+ cache_read: sum.cache_read + tokens.cache_read,
8
+ cache_write: sum.cache_write + tokens.cache_write,
9
+ };
10
+ }
1
11
  export class Parts {
2
12
  parts = [];
3
13
  context = null;
package/dist/placement.js CHANGED
@@ -1,27 +1,9 @@
1
1
  import { readFileSync } from 'node:fs';
2
- import { join } from 'node:path';
3
- import { run } from './clis.js';
2
+ import { join, resolve } from 'node:path';
4
3
  import { kitHome } from './home.js';
5
- import { relayOrigin } from './relay.js';
6
- export function service() {
7
- if (process.platform !== 'linux' || !process.env.INVOCATION_ID)
8
- return null;
9
- return process.env.MANAGERPID ? 'user' : 'system';
10
- }
11
- export function throughSudo() {
12
- return service() === 'system' && process.getuid() !== 0;
13
- }
14
- export async function updatesItself() {
15
- if (service() === null)
16
- return false;
17
- if (!throughSudo())
18
- return true;
19
- // sudo answers at once, so ten seconds only bounds a stuck sudo.
20
- return (await run('sudo', ['-n', 'true'], 10_000)).status === 0;
21
- }
22
- function installed() {
4
+ function recorded(name) {
23
5
  try {
24
- return readFileSync(join(kitHome(), 'placement'), 'utf8').trim();
6
+ return readFileSync(join(kitHome(), name), 'utf8').trim();
25
7
  }
26
8
  catch (error) {
27
9
  if (error.code === 'ENOENT')
@@ -29,20 +11,21 @@ function installed() {
29
11
  throw error;
30
12
  }
31
13
  }
32
- function entries(house) {
33
- const host = new URL(house).host;
34
- const relay = relayOrigin();
35
- if (relay === undefined)
36
- return [`curl -fsSL ${host}/kit | sh`, `irm ${host}/kit.ps1 | iex`];
37
- const entry = `${relay}/${host}/kit`;
38
- return [`curl -fsSL ${entry} | HOUSE_KIT_RELAY=${relay} sh`, `$env:HOUSE_KIT_RELAY='${relay}'; irm ${entry}.ps1 | iex`];
14
+ export function installCommand() {
15
+ return recorded('install-command');
16
+ }
17
+ export function machine() {
18
+ const placed = recorded('placement');
19
+ if (placed === 'container' || placed === 'server' || placed === 'windows')
20
+ return placed;
21
+ return process.platform === 'darwin' ? 'macos' : 'linux';
22
+ }
23
+ export function placedPrefix() {
24
+ const prefix = process.env.HOUSE_KIT_PREFIX;
25
+ if (prefix === undefined || prefix === '')
26
+ throw new Error('this Kit process was not started by a launcher its installer placed');
27
+ return resolve(prefix);
39
28
  }
40
- export function installCommand(house) {
41
- const [shell, windows] = entries(house);
42
- const placement = installed();
43
- if (placement === 'native')
44
- return shell;
45
- if (placement === 'server')
46
- return `${shell} -s -- --linux`;
47
- return `${shell} -s -- --container, or ${windows} on Windows`;
29
+ export function releaseDirectory(release) {
30
+ return join(placedPrefix(), 'releases', release);
48
31
  }
package/dist/relay.js CHANGED
@@ -1,6 +1,11 @@
1
+ import { setGlobalProxyFromEnv } from 'node:http';
2
+ const PROXY = /^https?_proxy$/i;
3
+ const LOOPBACK = ['127.0.0.1', 'localhost', '::1'];
1
4
  let selected;
5
+ let restoreProxy;
2
6
  export function selectRelay(relay) {
3
7
  selected = relay === undefined || relay === '' ? null : relay;
8
+ proxyUnlessRelayed();
4
9
  }
5
10
  export function relayOrigin() {
6
11
  if (selected !== undefined)
@@ -8,6 +13,22 @@ export function relayOrigin() {
8
13
  const ambient = process.env.HOUSE_KIT_RELAY;
9
14
  return ambient === undefined || ambient === '' ? undefined : ambient;
10
15
  }
16
+ function unlessRelayed() {
17
+ if (relayOrigin() === undefined)
18
+ return { ...process.env };
19
+ return Object.fromEntries(Object.entries(process.env).filter(([name]) => !PROXY.test(name)));
20
+ }
21
+ export function proxyUnlessRelayed() {
22
+ restoreProxy?.();
23
+ restoreProxy = setGlobalProxyFromEnv(unlessRelayed());
24
+ }
25
+ export function childEnvironment() {
26
+ const environment = unlessRelayed();
27
+ if (!Object.keys(environment).some((name) => PROXY.test(name)))
28
+ return environment;
29
+ const bypassed = [environment.NO_PROXY ?? environment.no_proxy, ...LOOPBACK].filter((entry) => entry !== undefined && entry !== '').join(',');
30
+ return { ...environment, NO_PROXY: bypassed, no_proxy: bypassed };
31
+ }
11
32
  export function relayed(target) {
12
33
  const url = new URL(target);
13
34
  const relay = relayOrigin();
package/dist/resident.js CHANGED
@@ -1,22 +1,26 @@
1
1
  import { once } from 'node:events';
2
2
  import { watch } from 'node:fs';
3
3
  import { mkdir, rm, writeFile } from 'node:fs/promises';
4
- import { createServer } from 'node:http';
4
+ import { createServer, get } from 'node:http';
5
5
  import { dirname, extname, join } from 'node:path';
6
+ import { DatabaseSync } from 'node:sqlite';
6
7
  import { fileURLToPath } from 'node:url';
7
8
  import { Agents } from './agents.js';
8
9
  import { house } from './api.js';
9
10
  import { received } from './bridge.js';
10
- import { CONVERSATION_INPUTS, Conversations } from './conversations.js';
11
+ import { CONVERSATION_METHODS, Conversations } from './conversations.js';
11
12
  import { LocalCopies } from './copies.js';
12
- import { restart } from './end.js';
13
- import { agentBase, enrolled, forgetRestart, kitHome, restartAsked } from './home.js';
13
+ import { restart, unknownMethod } from './end.js';
14
+ import { agentBase, enrolled, kitHome, ownerLock, ownerSocket, readEnrolment } from './home.js';
15
+ import { Calls } from './calls.js';
14
16
  import { killDescendants } from './scope.js';
17
+ import { removeInputs, Scripts } from './scripts.js';
15
18
  import { quoted } from './shell.js';
16
19
  import { SignIns } from './sign-in.js';
17
20
  import { placeSkillSet } from './skills.js';
18
- import { holdStream } from './stream.js';
19
- import { Updates } from './update.js';
21
+ import { holdConnection } from './connection.js';
22
+ import { KitUpdate } from './update.js';
23
+ import { takeUnservedRelease } from './wire.js';
20
24
  import { codexLauncher } from './work.js';
21
25
  function report(error) {
22
26
  process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
@@ -29,13 +33,32 @@ async function installShim(path, module) {
29
33
  await mkdir(dirname(path), { recursive: true });
30
34
  await writeFile(path, `#!/bin/sh\nexec ${quoted(process.execPath)} ${quoted(main)} "$@"\n`, { mode: 0o755 });
31
35
  }
36
+ function claimed(lock) {
37
+ try {
38
+ lock.exec('BEGIN EXCLUSIVE');
39
+ return true;
40
+ }
41
+ catch (error) {
42
+ if (error.errcode === 5)
43
+ return false;
44
+ throw error;
45
+ }
46
+ }
32
47
  async function serveOwner(copies) {
33
- const socketPath = join(kitHome(), 'owner.sock');
34
- await rm(socketPath, { force: true });
48
+ const lock = new DatabaseSync(ownerLock());
49
+ if (!claimed(lock)) {
50
+ lock.close();
51
+ const running = await runningResident();
52
+ throw new Error(`${running === null ? 'another Kit process' : `Kit process ${running}`} already runs this Environment from Kit home ${kitHome()}; stop it first to run another Kit process here`);
53
+ }
35
54
  const server = createServer(async (request, response) => {
36
55
  try {
37
56
  const text = await received(request);
38
57
  let answered;
58
+ if (request.url === '/resident') {
59
+ response.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify({ pid: process.pid }));
60
+ return;
61
+ }
39
62
  if (request.url === '/git/fetch') {
40
63
  answered = await copies.fetch();
41
64
  }
@@ -53,9 +76,23 @@ async function serveOwner(copies) {
53
76
  response.writeHead(502).end(error instanceof Error ? error.message : String(error));
54
77
  }
55
78
  });
56
- server.listen(socketPath);
79
+ server.on('close', () => lock.close());
80
+ await rm(ownerSocket(), { force: true });
81
+ server.listen(ownerSocket());
57
82
  await once(server, 'listening');
58
83
  }
84
+ function runningResident() {
85
+ return new Promise((resolve, reject) => {
86
+ get({ socketPath: ownerSocket(), path: '/resident' }, (answer) => {
87
+ received(answer).then((text) => resolve(JSON.parse(text).pid), reject);
88
+ }).on('error', (error) => {
89
+ if (error.code === 'ENOENT' || error.code === 'ECONNREFUSED')
90
+ resolve(null);
91
+ else
92
+ reject(error);
93
+ });
94
+ });
95
+ }
59
96
  function connected() {
60
97
  return new Promise((resolve) => {
61
98
  const watcher = watch(kitHome(), () => {
@@ -72,81 +109,100 @@ function connected() {
72
109
  resolve();
73
110
  });
74
111
  }
75
- export async function resident() {
112
+ async function started() {
76
113
  await connected();
77
- await forgetRestart();
78
114
  watch(kitHome(), () => {
79
115
  if (enrolled())
80
116
  return;
81
117
  killDescendants();
82
118
  process.exit(1);
83
119
  });
84
- await placeSkillSet();
120
+ await placeSkillSet((await readEnrolment()).house);
85
121
  await mkdir(agentBase(), { recursive: true });
86
- process.env.HOUSE_KIT_RESIDENT = String(process.pid);
87
- const agents = new Agents(house);
88
- const copies = new LocalCopies(house);
89
- await copies.load();
90
122
  await installShim(join(kitHome(), 'shim', 'git'), 'git-main');
91
123
  await installShim(codexLauncher(), 'codex-main');
124
+ }
125
+ export async function resident() {
126
+ await mkdir(kitHome(), { recursive: true, mode: 0o700 });
127
+ const copies = new LocalCopies(house);
128
+ await copies.load();
92
129
  await serveOwner(copies);
93
- let stream = null;
130
+ await copies.swept();
131
+ await started().catch((error) => {
132
+ report(error);
133
+ process.exit(1);
134
+ });
135
+ process.env.HOUSE_KIT_RESIDENT = String(process.pid);
136
+ const agents = new Agents(house, () => changed());
137
+ let connection = null;
94
138
  let reported = false;
95
- const idle = () => conversations.idle() && signIns.idle();
139
+ const idle = () => calls.idle() && conversations.idle() && agents.idle();
96
140
  const changed = () => {
97
141
  if (!idle()) {
98
142
  reported = false;
99
143
  }
100
- else if (restartAsked() && conversations.quiet()) {
101
- restart('restarting for an updated CLI');
144
+ else if (update.placedRelease() !== null && conversations.quiet()) {
145
+ restart(`Kit ${update.placedRelease()} is placed; its supervisor starts it`);
102
146
  }
103
147
  else if (!reported) {
104
148
  reported = true;
105
- logged(house.deliver('/kit/idle', {}, idle));
149
+ logged(house.deliver('/kit/kits/report-idle', {}, idle));
106
150
  }
107
151
  };
152
+ const calls = new Calls(house, changed);
108
153
  const conversations = new Conversations({
109
154
  house,
110
155
  agents,
111
156
  copies,
112
- send: (frame) => stream?.send(frame) ?? false,
157
+ calls,
158
+ send: (oneWayCall) => connection?.send(oneWayCall) ?? false,
113
159
  changed,
114
160
  });
115
- const signIns = new SignIns(house, agents, changed);
116
- const updates = new Updates(house);
161
+ const signIns = new SignIns(house, agents, calls);
162
+ const update = new KitUpdate(calls);
163
+ if (process.env.HOUSE_KIT_SANDBOX !== '1')
164
+ takeUnservedRelease((release) => logged(update.takeRefused(release)));
165
+ removeInputs();
166
+ const scripts = new Scripts(calls, (oneWayCall) => connection?.send(oneWayCall) ?? false);
117
167
  for (const signal of ['SIGTERM', 'SIGINT']) {
118
168
  process.once(signal, () => {
119
169
  killDescendants();
170
+ removeInputs();
120
171
  process.kill(process.pid, signal);
121
172
  });
122
173
  }
123
- await house.deliver('/kit/restarted', {}).catch(report);
124
- await updates.resumed().catch(report);
174
+ await house.deliver('/kit/kits/report-restart', {}).catch(report);
125
175
  logged(agents.refresh());
126
- stream = holdStream({
176
+ agents.keepCurrent();
177
+ connection = holdConnection({
127
178
  opened: () => {
128
179
  conversations.opened();
129
180
  changed();
130
181
  logged(copies.connected());
131
182
  },
132
- frame: (arrived) => {
133
- if (arrived.type === 'watching')
134
- conversations.watching(arrived.watching === true);
135
- if (arrived.type === 'work_available' && arrived.subject === 'agents')
136
- logged(agents.refresh());
137
- if (arrived.type === 'input') {
138
- reported = false;
139
- if (arrived.kind === 'sign_in')
140
- signIns.start(arrived);
141
- else if (arrived.kind === 'update')
142
- updates.start(arrived);
143
- else if (CONVERSATION_INPUTS.has(arrived.kind))
144
- conversations.input(arrived);
145
- else {
146
- const refused = { refused: `This Kit does not know ${arrived.kind}; update the Kit` };
147
- logged(house.deliver(`/kit/inputs/${arrived.input_id}/ack`, refused).finally(changed));
148
- }
183
+ closed: () => scripts.disconnected(),
184
+ call: (call) => {
185
+ if (call.kind === 'sign_in')
186
+ signIns.start(call);
187
+ else if (call.kind === 'update')
188
+ update.take(call);
189
+ else if (call.kind === 'run_wizard' || call.kind === 'run_function')
190
+ scripts.start(call);
191
+ else if (CONVERSATION_METHODS.has(call.kind))
192
+ conversations.call(call);
193
+ else {
194
+ const refused = { refused: unknownMethod(call.kind) };
195
+ void calls.carry(call.input_id, async () => ({ result: refused }));
149
196
  }
150
197
  },
198
+ oneWayCall: (oneWayCall) => {
199
+ if (oneWayCall.type === 'watching')
200
+ conversations.watching(oneWayCall.watching === true);
201
+ if (oneWayCall.type === 'authority_changed')
202
+ logged(copies.changed(oneWayCall.rooms));
203
+ if (oneWayCall.type === 'work_available' && oneWayCall.subject === 'agents')
204
+ logged(agents.refresh());
205
+ scripts.notified(oneWayCall);
206
+ },
151
207
  });
152
208
  }
package/dist/scope.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import { execFileSync } from 'node:child_process';
2
- import { mkdirSync, readdirSync, readFileSync, rmdirSync, statSync, writeFileSync } from 'node:fs';
2
+ import { mkdirSync, readdirSync, readFileSync, realpathSync, rmdirSync, statSync, writeFileSync } from 'node:fs';
3
3
  import { join } from 'node:path';
4
4
  const LISTING_BYTES = 1 << 28;
5
5
  const CGROUPS = '/sys/fs/cgroup';
6
6
  const GONE = new Set(['ENOENT', 'ESRCH']);
7
+ const EXITED = new Set(['Z', 'X']);
7
8
  function failure(error) {
8
9
  return error.code ?? '';
9
10
  }
@@ -24,6 +25,8 @@ function read(pid) {
24
25
  try {
25
26
  const stat = readFileSync(`/proc/${pid}/stat`, 'utf8');
26
27
  const fields = stat.slice(stat.lastIndexOf(')') + 2).split(' ');
28
+ if (EXITED.has(fields[0]))
29
+ return null;
27
30
  const command = readFileSync(`/proc/${pid}/cmdline`, 'utf8').replaceAll('\0', ' ');
28
31
  return { parent: Number(fields[1]), session: fields[3], command };
29
32
  }
@@ -119,15 +122,23 @@ function members(group) {
119
122
  }
120
123
  return pids;
121
124
  }
125
+ function besideCli(cli) {
126
+ try {
127
+ return `${realpathSync(cli)}-`;
128
+ }
129
+ catch {
130
+ return null;
131
+ }
132
+ }
122
133
  export class Scope {
123
134
  marker;
124
- cli;
135
+ helpers;
125
136
  group;
126
137
  session = null;
127
138
  recorded = null;
128
139
  constructor(marker, name, cli) {
129
140
  this.marker = marker;
130
- this.cli = cli;
141
+ this.helpers = besideCli(cli);
131
142
  const own = ownGroup();
132
143
  this.group = own === null ? null : join(own, name);
133
144
  try {
@@ -165,7 +176,7 @@ export class Scope {
165
176
  return true;
166
177
  if (this.recorded === null)
167
178
  return found.size > 0;
168
- return [...found].some(([pid, { session, command }]) => session !== this.session || !(this.recorded.has(pid) || command.startsWith(`${this.cli}-`)));
179
+ return [...found].some(([pid, { session, command }]) => session !== this.session || !(this.recorded.has(pid) || (this.helpers !== null && command.startsWith(this.helpers))));
169
180
  }
170
181
  empty() {
171
182
  return this.found()?.size === 0;