@agentshouse/kit 0.2.1-cf110d15f69c8 → 0.2.1-cf2c570b41f24

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 (147) hide show
  1. package/README.md +38 -44
  2. package/dist/acp.js +96 -22
  3. package/dist/afk.js +17 -0
  4. package/dist/agents.js +221 -65
  5. package/dist/api.js +21 -3
  6. package/dist/app-git.js +10 -26
  7. package/dist/binaries.js +60 -0
  8. package/dist/bridge.js +101 -49
  9. package/dist/building.js +50 -0
  10. package/dist/calls.js +88 -0
  11. package/dist/chunks.js +54 -0
  12. package/dist/clis.js +80 -52
  13. package/dist/connect.js +121 -0
  14. package/dist/connection.js +120 -0
  15. package/dist/content.js +106 -0
  16. package/dist/conversations.js +711 -267
  17. package/dist/copies.js +1293 -595
  18. package/dist/earlier.js +273 -0
  19. package/dist/end.js +15 -3
  20. package/dist/events.js +553 -281
  21. package/dist/files.js +206 -32
  22. package/dist/git.js +2 -70
  23. package/dist/grok.js +39 -0
  24. package/dist/home.js +123 -15
  25. package/dist/house-cli.js +282 -0
  26. package/dist/house-main.js +7 -206
  27. package/dist/instructions.js +7 -13
  28. package/dist/keys.js +238 -0
  29. package/dist/kit-cli.js +78 -0
  30. package/dist/kit-main.js +8 -22
  31. package/dist/login.js +71 -39
  32. package/dist/logout.js +2 -4
  33. package/dist/manifest.js +75 -0
  34. package/dist/mcp.js +47 -7
  35. package/dist/owner.js +46 -0
  36. package/dist/placement.js +25 -0
  37. package/dist/refusals.js +50 -23
  38. package/dist/relay.js +22 -1
  39. package/dist/resident.js +153 -53
  40. package/dist/scope.js +15 -4
  41. package/dist/scripts.js +319 -0
  42. package/dist/secret-input.js +1 -1
  43. package/dist/settings.js +247 -0
  44. package/dist/sign-in.js +66 -38
  45. package/dist/silence.js +28 -0
  46. package/dist/skills.js +49 -35
  47. package/dist/supervisor.js +163 -0
  48. package/dist/transcription.js +90 -47
  49. package/dist/update.js +119 -102
  50. package/dist/wire.js +32 -0
  51. package/dist/work.js +30 -7
  52. package/dist/workspace.js +113 -0
  53. package/package.json +25 -21
  54. package/dist/authority-main.js +0 -22
  55. package/dist/bootstrap.json +0 -24
  56. package/dist/clis-main.js +0 -23
  57. package/dist/configure-main.js +0 -6
  58. package/dist/enrol-main.js +0 -10
  59. package/dist/git-main.js +0 -153
  60. package/dist/parts.js +0 -82
  61. package/dist/service-main.js +0 -9
  62. package/dist/service.js +0 -33
  63. package/dist/stream.js +0 -74
  64. package/dist/translate.js +0 -51
  65. package/how-we-work.md +0 -23
  66. package/skills/ask-matt/LICENSE +0 -21
  67. package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
  68. package/skills/ask-matt/SKILL.md +0 -91
  69. package/skills/ask-matt/agents/openai.yaml +0 -4
  70. package/skills/code-review/LICENSE +0 -21
  71. package/skills/code-review/SKILL.md +0 -89
  72. package/skills/code-review/agents/openai.yaml +0 -3
  73. package/skills/codebase-design/DEEPENING.md +0 -37
  74. package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
  75. package/skills/codebase-design/LICENSE +0 -21
  76. package/skills/codebase-design/SKILL.md +0 -116
  77. package/skills/codebase-design/agents/openai.yaml +0 -3
  78. package/skills/diagnosing-bugs/LICENSE +0 -21
  79. package/skills/diagnosing-bugs/SKILL.md +0 -140
  80. package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
  81. package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
  82. package/skills/domain-modeling/ADR-FORMAT.md +0 -47
  83. package/skills/domain-modeling/CONTEXT-FORMAT.md +0 -60
  84. package/skills/domain-modeling/LICENSE +0 -21
  85. package/skills/domain-modeling/SKILL.md +0 -76
  86. package/skills/domain-modeling/agents/openai.yaml +0 -3
  87. package/skills/grill-me/LICENSE +0 -21
  88. package/skills/grill-me/SKILL.md +0 -8
  89. package/skills/grill-me/agents/openai.yaml +0 -4
  90. package/skills/grill-with-docs/LICENSE +0 -21
  91. package/skills/grill-with-docs/SKILL.md +0 -8
  92. package/skills/grill-with-docs/agents/openai.yaml +0 -4
  93. package/skills/grilling/LICENSE +0 -21
  94. package/skills/grilling/SKILL.md +0 -30
  95. package/skills/grilling/agents/openai.yaml +0 -3
  96. package/skills/handoff/LICENSE +0 -21
  97. package/skills/handoff/SKILL.md +0 -17
  98. package/skills/handoff/agents/openai.yaml +0 -4
  99. package/skills/implement/LICENSE +0 -21
  100. package/skills/implement/SKILL.md +0 -16
  101. package/skills/implement/agents/openai.yaml +0 -4
  102. package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
  103. package/skills/improve-codebase-architecture/LICENSE +0 -21
  104. package/skills/improve-codebase-architecture/SKILL.md +0 -72
  105. package/skills/improve-codebase-architecture/agents/openai.yaml +0 -4
  106. package/skills/prototype/LICENSE +0 -21
  107. package/skills/prototype/LOGIC.md +0 -67
  108. package/skills/prototype/SKILL.md +0 -28
  109. package/skills/prototype/UI.md +0 -112
  110. package/skills/prototype/agents/openai.yaml +0 -3
  111. package/skills/research/LICENSE +0 -21
  112. package/skills/research/SKILL.md +0 -14
  113. package/skills/research/agents/openai.yaml +0 -3
  114. package/skills/resolving-merge-conflicts/LICENSE +0 -21
  115. package/skills/resolving-merge-conflicts/SKILL.md +0 -16
  116. package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
  117. package/skills/tdd/LICENSE +0 -21
  118. package/skills/tdd/SKILL.md +0 -40
  119. package/skills/tdd/agents/openai.yaml +0 -3
  120. package/skills/tdd/mocking.md +0 -59
  121. package/skills/tdd/tests.md +0 -77
  122. package/skills/teach/GLOSSARY-FORMAT.md +0 -35
  123. package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
  124. package/skills/teach/LICENSE +0 -21
  125. package/skills/teach/MISSION-FORMAT.md +0 -31
  126. package/skills/teach/RESOURCES-FORMAT.md +0 -32
  127. package/skills/teach/SKILL.md +0 -141
  128. package/skills/teach/agents/openai.yaml +0 -4
  129. package/skills/to-questionnaire/LICENSE +0 -21
  130. package/skills/to-questionnaire/SKILL.md +0 -55
  131. package/skills/to-questionnaire/agents/openai.yaml +0 -4
  132. package/skills/to-spec/LICENSE +0 -21
  133. package/skills/to-spec/SKILL.md +0 -76
  134. package/skills/to-spec/agents/openai.yaml +0 -4
  135. package/skills/to-tickets/LICENSE +0 -21
  136. package/skills/to-tickets/SKILL.md +0 -106
  137. package/skills/to-tickets/agents/openai.yaml +0 -4
  138. package/skills/wait-what/LICENSE +0 -21
  139. package/skills/wait-what/SKILL.md +0 -8
  140. package/skills/wait-what/agents/openai.yaml +0 -4
  141. package/skills/wayfinder/LICENSE +0 -21
  142. package/skills/wayfinder/SKILL.md +0 -129
  143. package/skills/wayfinder/agents/openai.yaml +0 -4
  144. package/skills/writing-for-agents/LICENSE +0 -21
  145. package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
  146. package/skills/writing-for-agents/SKILL.md +0 -83
  147. package/skills/writing-for-agents/agents/openai.yaml +0 -3
package/dist/mcp.js CHANGED
@@ -1,26 +1,35 @@
1
+ import { KIT_WIRE, KIT_WIRE_HEADER } from '@agentshouse/kit-wire';
1
2
  import { randomUUID } from 'node:crypto';
2
- import { KIT_VERSION } from './clis.js';
3
- import { relayed } from './relay.js';
3
+ import { KIT_RELEASE } from './clis.js';
4
+ import { readOwnAgent } from './home.js';
5
+ import { refusalOf, retryable } from './refusals.js';
6
+ import { relayed, selectRelay } from './relay.js';
4
7
  const MCP_PROTOCOL_VERSION = '2026-07-28';
5
8
  export const UNREACHABLE = 'House is unreachable; call again in a minute';
9
+ const UNCONNECTED = "this computer's own agent has no House connection; run kit login";
10
+ export const DISCONNECTED = "House no longer accepts this computer's own agent; run kit login";
11
+ const UNCONFIRMED = 'with no result; a write may or may not have taken effect, so read before you repeat it';
12
+ export class Unsettled extends Error {
13
+ }
6
14
  export function rpc(method, params) {
7
15
  return { jsonrpc: '2.0', id: randomUUID(), method, params };
8
16
  }
9
- export function mcpBody(message, operation) {
17
+ export function mcpBody(message, { operation = null, named = {} } = {}) {
10
18
  return JSON.stringify({
11
19
  ...message,
12
20
  params: {
13
21
  ...message.params,
14
22
  _meta: {
15
23
  'io.modelcontextprotocol/protocolVersion': MCP_PROTOCOL_VERSION,
16
- 'io.modelcontextprotocol/clientInfo': { name: '@agentshouse/kit', version: KIT_VERSION },
24
+ 'io.modelcontextprotocol/clientInfo': { name: '@agentshouse/kit', version: KIT_RELEASE },
17
25
  'io.modelcontextprotocol/clientCapabilities': {},
26
+ ...named,
18
27
  ...(operation === null ? {} : { 'agents.house/agent-operation': operation }),
19
28
  },
20
29
  },
21
30
  });
22
31
  }
23
- export async function callHouse(origin, credential, message, operation = null, signal) {
32
+ export async function callHouse(origin, credential, message, sending = {}) {
24
33
  const tool = message.method === 'tools/call' ? String(message.params.name) : null;
25
34
  const answer = await fetch(relayed(new URL('/', origin)), {
26
35
  method: 'POST',
@@ -31,9 +40,40 @@ export async function callHouse(origin, credential, message, operation = null, s
31
40
  'mcp-protocol-version': MCP_PROTOCOL_VERSION,
32
41
  'mcp-method': message.method,
33
42
  ...(tool === null ? {} : { 'mcp-name': tool }),
43
+ [KIT_WIRE_HEADER]: KIT_WIRE,
34
44
  },
35
- body: mcpBody(message, operation),
36
- signal,
45
+ body: mcpBody(message, sending),
46
+ signal: sending.signal,
37
47
  });
38
48
  return { status: answer.status, text: await answer.text() };
39
49
  }
50
+ export function unconfirmed(status) {
51
+ return `House answered ${status} ${UNCONFIRMED}`;
52
+ }
53
+ export async function ownAgentCall(message) {
54
+ const ownAgent = await readOwnAgent();
55
+ if (ownAgent === null)
56
+ throw new Error(UNCONNECTED);
57
+ selectRelay(process.env.HOUSE_KIT_RELAY || ownAgent.relay);
58
+ const answer = await callHouse(ownAgent.house, ownAgent.credential, message).catch(() => {
59
+ throw new Unsettled(UNREACHABLE);
60
+ });
61
+ if (answer.status === 200)
62
+ return answer.text;
63
+ if (answer.status === 401)
64
+ throw new Error(DISCONNECTED);
65
+ const refusal = refusalOf(answer.text)?.text ?? unconfirmed(answer.status);
66
+ throw retryable(answer.text) === false ? new Error(refusal) : new Unsettled(refusal);
67
+ }
68
+ export function refusalIn(text, status) {
69
+ return refusalOf(text) ?? { code: '', text: text === '' ? `House refused it with ${status}` : text, conflicts: [], detail: {} };
70
+ }
71
+ export function toolAnswer(forwarded) {
72
+ if (forwarded.status !== 200)
73
+ return { refused: refusalIn(forwarded.text, forwarded.status) };
74
+ const answer = JSON.parse(forwarded.text);
75
+ if (answer.error !== undefined)
76
+ return { refused: refusalIn(answer.error.message, forwarded.status) };
77
+ const text = answer.result.content.map((content) => content.text).join('\n');
78
+ return answer.result.isError === true ? { refused: refusalIn(text, forwarded.status) } : { text };
79
+ }
package/dist/owner.js ADDED
@@ -0,0 +1,46 @@
1
+ import { request } from 'node:http';
2
+ import { DatabaseSync } from 'node:sqlite';
3
+ import { received } from './bridge.js';
4
+ import { kitHome, ownerLock, ownerSocket } from './home.js';
5
+ function claimed(lock) {
6
+ try {
7
+ lock.exec('BEGIN EXCLUSIVE');
8
+ return true;
9
+ }
10
+ catch (error) {
11
+ if (error.errcode === 5)
12
+ return false;
13
+ throw error;
14
+ }
15
+ }
16
+ export function askOwner(path, body) {
17
+ return new Promise((resolve, reject) => {
18
+ const sent = request({ socketPath: ownerSocket(), path, method: body === undefined ? 'GET' : 'POST', headers: { 'content-type': 'application/json' } }, (answer) => {
19
+ received(answer).then((text) => {
20
+ if (answer.statusCode === 200)
21
+ resolve(JSON.parse(text));
22
+ else
23
+ reject(new Error(text));
24
+ }, reject);
25
+ });
26
+ sent.on('error', (error) => {
27
+ if (error.code === 'ENOENT' || error.code === 'ECONNREFUSED')
28
+ resolve(null);
29
+ else
30
+ reject(error);
31
+ });
32
+ sent.end(body === undefined ? undefined : JSON.stringify(body));
33
+ });
34
+ }
35
+ export async function holdOwner() {
36
+ const lock = new DatabaseSync(ownerLock());
37
+ if (claimed(lock))
38
+ return lock;
39
+ lock.close();
40
+ const running = (await askOwner('/resident'))?.pid ?? null;
41
+ 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`);
42
+ }
43
+ export async function runningRelease() {
44
+ const running = await askOwner('/resident');
45
+ return running === null ? null : (running.release ?? '');
46
+ }
@@ -0,0 +1,25 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { join, resolve } from 'node:path';
3
+ import { kitHome } from './home.js';
4
+ function recorded(name) {
5
+ try {
6
+ return readFileSync(join(kitHome(), name), 'utf8').trim();
7
+ }
8
+ catch (error) {
9
+ if (error.code === 'ENOENT')
10
+ return null;
11
+ throw error;
12
+ }
13
+ }
14
+ export function installCommand() {
15
+ return recorded('install-command');
16
+ }
17
+ export function placedPrefix() {
18
+ const prefix = process.env.HOUSE_KIT_PREFIX;
19
+ if (prefix === undefined || prefix === '')
20
+ throw new Error('this Kit process was not started by a launcher its installer placed');
21
+ return resolve(prefix);
22
+ }
23
+ export function releaseDirectory(release) {
24
+ return join(placedPrefix(), 'releases', release);
25
+ }
package/dist/refusals.js CHANGED
@@ -1,53 +1,80 @@
1
1
  import { parse } from 'yaml';
2
2
  const HEAD = /^([a-z][a-z0-9_]*): \S/;
3
- function conflictCodes(detail) {
3
+ const PUSH_STOPS = new Set(['limit_reached', 'cap_reached']);
4
+ function detailOf(text) {
4
5
  let parsed;
5
6
  try {
6
- parsed = parse(detail);
7
+ parsed = parse(text);
7
8
  }
8
9
  catch {
9
- return [];
10
+ return {};
10
11
  }
11
- const conflicts = parsed?.conflicts;
12
- if (!Array.isArray(conflicts))
12
+ return typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed) ? parsed : {};
13
+ }
14
+ function conflictsOf(detail) {
15
+ if (!Array.isArray(detail.conflicts))
13
16
  return [];
14
- return conflicts.flatMap((conflict) => typeof conflict?.code === 'string' ? [conflict.code] : []);
17
+ return detail.conflicts.flatMap((conflict) => typeof conflict?.code === 'string'
18
+ ? [
19
+ {
20
+ code: conflict.code,
21
+ path: typeof conflict.path === 'string' ? conflict.path : '',
22
+ ...(typeof conflict.revision === 'string' ? { revision: conflict.revision } : {}),
23
+ ...(typeof conflict.content === 'string' ? { content: conflict.content } : {}),
24
+ },
25
+ ]
26
+ : []);
15
27
  }
16
28
  function spoken(text) {
17
29
  const head = HEAD.exec(text);
18
30
  if (head === null)
19
31
  return null;
20
32
  const said = text.replace(/\n+$/, '');
21
- const detail = said.indexOf('\n');
22
- return { code: head[1], text: said, conflicts: detail === -1 ? [] : conflictCodes(said.slice(detail + 1)) };
33
+ const line = said.indexOf('\n');
34
+ const detail = line === -1 ? {} : detailOf(said.slice(line + 1));
35
+ return { code: head[1], text: said, conflicts: conflictsOf(detail), detail };
23
36
  }
24
- export function retryable(text) {
25
- let body;
37
+ export function stopsPush(refusal) {
38
+ return refusal !== null && PUSH_STOPS.has(refusal.code) && refusal.retryable === true;
39
+ }
40
+ export function firstLine(refusal) {
41
+ return refusal.text.split('\n')[0];
42
+ }
43
+ function parsed(text) {
26
44
  try {
27
- body = JSON.parse(text);
45
+ return { body: JSON.parse(text) };
28
46
  }
29
47
  catch {
30
48
  return null;
31
49
  }
50
+ }
51
+ function errorIn(body) {
32
52
  const error = body?.error;
53
+ return typeof error === 'object' && error !== null ? error : null;
54
+ }
55
+ function verdictOf(error) {
33
56
  const verdict = error?.retryable ?? error?.data?.retryable;
34
57
  return typeof verdict === 'boolean' ? verdict : null;
35
58
  }
59
+ export function retryable(text) {
60
+ const answer = parsed(text);
61
+ return answer === null ? null : verdictOf(errorIn(answer.body));
62
+ }
36
63
  export function refusalOf(text) {
37
- let body;
38
- try {
39
- body = JSON.parse(text);
40
- }
41
- catch {
64
+ const answer = parsed(text);
65
+ if (answer === null)
42
66
  return spoken(text);
43
- }
44
- const error = body?.error;
45
- if (typeof error !== 'object' || error === null)
67
+ const error = errorIn(answer.body);
68
+ if (error === null)
46
69
  return null;
47
- const { code, message, data } = error;
70
+ const { code, message, data, retryable: _retryable, ...fields } = error;
71
+ const verdict = verdictOf(error);
72
+ const judged = verdict === null ? {} : { retryable: verdict };
48
73
  const said = typeof message === 'string' ? spoken(message) : null;
49
- if (said !== null)
50
- return said;
74
+ if (said !== null) {
75
+ const detail = { ...said.detail, ...fields };
76
+ return { ...said, detail, conflicts: conflictsOf(detail), ...judged };
77
+ }
51
78
  const named = typeof code === 'string' ? code : typeof data?.code === 'string' ? data.code : null;
52
- return named === null ? null : { code: named, text: named, conflicts: [] };
79
+ return named === null ? null : { code: named, text: named, conflicts: [], detail: {}, ...judged };
53
80
  }
package/dist/relay.js CHANGED
@@ -1,13 +1,34 @@
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
- function relayOrigin() {
10
+ export function relayOrigin() {
6
11
  if (selected !== undefined)
7
12
  return selected ?? undefined;
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
@@ -8,15 +8,22 @@ import { Agents } from './agents.js';
8
8
  import { house } from './api.js';
9
9
  import { received } from './bridge.js';
10
10
  import { Conversations } from './conversations.js';
11
- import { LocalCopies } from './copies.js';
12
- import { restart } from './end.js';
13
- import { agentBase, enrolled, forgetRestart, kitHome, restartAsked } from './home.js';
11
+ import { DEFAULT_MAX_SIZE, LocalCopies } from './copies.js';
12
+ import { restart, restartPlaced } from './end.js';
13
+ import { agentBase, enrolled, kitHome, ownerSocket, readEnrolment, recordPlannedRestart, roomsFolder, takePlannedRestart } from './home.js';
14
+ import { Calls } from './calls.js';
15
+ import { KIT_RELEASE } from './clis.js';
16
+ import { moveEarlier } from './earlier.js';
17
+ import { holdOwner } from './owner.js';
14
18
  import { killDescendants } from './scope.js';
19
+ import { removeInputs, Scripts } from './scripts.js';
20
+ import { applySettings, enrolledRoutes, launchDirectories, moveIntoHouse, resumeWorkspaceMove } from './settings.js';
15
21
  import { quoted } from './shell.js';
16
22
  import { SignIns } from './sign-in.js';
17
23
  import { placeSkillSet } from './skills.js';
18
- import { holdStream } from './stream.js';
19
- import { Updates } from './update.js';
24
+ import { holdConnection } from './connection.js';
25
+ import { KitUpdate } from './update.js';
26
+ import { takeUnservedRelease } from './wire.js';
20
27
  import { codexLauncher } from './work.js';
21
28
  function report(error) {
22
29
  process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
@@ -29,19 +36,27 @@ async function installShim(path, module) {
29
36
  await mkdir(dirname(path), { recursive: true });
30
37
  await writeFile(path, `#!/bin/sh\nexec ${quoted(process.execPath)} ${quoted(main)} "$@"\n`, { mode: 0o755 });
31
38
  }
32
- async function serveOwner(copies) {
33
- const socketPath = join(kitHome(), 'owner.sock');
34
- await rm(socketPath, { force: true });
39
+ async function serveOwner(lock, copies, settling) {
35
40
  const server = createServer(async (request, response) => {
36
41
  try {
37
42
  const text = await received(request);
38
43
  let answered;
39
- if (request.url === '/git/fetch') {
40
- answered = await copies.fetch();
44
+ if (request.url === '/resident') {
45
+ response.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify({ pid: process.pid, release: KIT_RELEASE }));
46
+ return;
47
+ }
48
+ if (request.url === '/copy/download') {
49
+ const { attachments = false, max_size: maxSize = DEFAULT_MAX_SIZE } = JSON.parse(text);
50
+ answered = await copies.download(copies.owner, { attachments, maxSize });
51
+ }
52
+ else if (request.url === '/copy/upload') {
53
+ const { cwd } = JSON.parse(text);
54
+ answered = await copies.upload(cwd, copies.owner);
41
55
  }
42
- else if (request.url === '/git/push') {
43
- const { cwd, commit } = JSON.parse(text);
44
- answered = await copies.push(cwd, commit, copies.owner);
56
+ else if (request.url === '/settings') {
57
+ const applied = await settling.settle(JSON.parse(text));
58
+ response.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify(applied), () => settling.answered(applied));
59
+ return;
45
60
  }
46
61
  else {
47
62
  response.writeHead(404).end();
@@ -53,7 +68,9 @@ async function serveOwner(copies) {
53
68
  response.writeHead(502).end(error instanceof Error ? error.message : String(error));
54
69
  }
55
70
  });
56
- server.listen(socketPath);
71
+ server.on('close', () => lock.close());
72
+ await rm(ownerSocket(), { force: true });
73
+ server.listen(ownerSocket());
57
74
  await once(server, 'listening');
58
75
  }
59
76
  function connected() {
@@ -72,77 +89,160 @@ function connected() {
72
89
  resolve();
73
90
  });
74
91
  }
75
- export async function resident() {
92
+ async function started() {
76
93
  await connected();
77
- await forgetRestart();
78
- watch(kitHome(), () => {
79
- if (enrolled())
94
+ await placeSkillSet((await readEnrolment()).house);
95
+ await mkdir(agentBase(), { recursive: true, mode: 0o700 });
96
+ await mkdir(roomsFolder(), { recursive: true, mode: 0o700 });
97
+ await rm(join(kitHome(), 'shim'), { recursive: true, force: true });
98
+ await installShim(codexLauncher(), 'codex-main');
99
+ }
100
+ export async function resident() {
101
+ await moveEarlier();
102
+ await mkdir(kitHome(), { recursive: true, mode: 0o700 });
103
+ const lock = await holdOwner();
104
+ await resumeWorkspaceMove();
105
+ const copies = new LocalCopies(house, launchDirectories);
106
+ await copies.load();
107
+ const moved = await moveIntoHouse(copies);
108
+ if (moved !== null)
109
+ restart(moved);
110
+ const planned = takePlannedRestart();
111
+ let running = null;
112
+ let settings = Promise.resolve();
113
+ const settle = (change) => {
114
+ const applied = settings.then(() => applySettings(change, {
115
+ working: () => running !== null && (running.conversations.turnOpen() || running.calls.busy()),
116
+ end: () => {
117
+ running?.conversations.stopped();
118
+ killDescendants();
119
+ },
120
+ routes: async () => {
121
+ if (running === null)
122
+ return enrolledRoutes();
123
+ await running.agents.read();
124
+ return running.agents.desired.routes;
125
+ },
126
+ }));
127
+ settings = applied.catch(() => undefined);
128
+ return applied.then((answer) => {
129
+ if ('settings' in answer && !answer.moved)
130
+ running?.agents.report().catch(report);
131
+ return answer;
132
+ });
133
+ };
134
+ const answered = (applied) => {
135
+ if ('settings' in applied ? !applied.moved : !applied.ended)
80
136
  return;
81
- killDescendants();
137
+ const ended = 'settings' in applied
138
+ ? `the workspace moved to ${applied.settings.workspace}; its supervisor starts Kit there`
139
+ : 'the workspace stays where it was; its supervisor starts Kit again';
140
+ if (running === null)
141
+ restart(ended);
142
+ else
143
+ void running.stop(() => restart(ended));
144
+ };
145
+ await copies.swept();
146
+ await serveOwner(lock, copies, { settle, answered });
147
+ await started().catch((error) => {
148
+ report(error);
82
149
  process.exit(1);
83
150
  });
84
- await placeSkillSet();
85
- await mkdir(agentBase(), { recursive: true });
86
151
  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
- await installShim(join(kitHome(), 'shim', 'git'), 'git-main');
91
- await installShim(codexLauncher(), 'codex-main');
92
- await serveOwner(copies);
93
- let stream = null;
152
+ const agents = new Agents(house, () => changed());
153
+ let connection = null;
94
154
  let reported = false;
95
- const idle = () => conversations.idle() && signIns.idle();
155
+ let stopping = false;
156
+ const stop = async (then) => {
157
+ if (stopping)
158
+ return;
159
+ stopping = true;
160
+ conversations.stopped();
161
+ killDescendants();
162
+ await connection?.close();
163
+ then();
164
+ };
165
+ const idle = () => calls.idle() && conversations.idle() && agents.idle();
96
166
  const changed = () => {
97
167
  if (!idle()) {
98
168
  reported = false;
99
169
  }
100
- else if (restartAsked() && conversations.quiet()) {
101
- restart('restarting for an updated CLI');
170
+ else if (update.placedRelease() !== null && conversations.quiet() && process.env.HOUSE_KIT_SANDBOX !== '1') {
171
+ const placed = update.placedRelease();
172
+ void stop(() => restartPlaced(placed));
102
173
  }
103
174
  else if (!reported) {
104
175
  reported = true;
105
- logged(house.deliver('/kit/idle', {}, idle));
176
+ logged(house.deliver('kits/report-idle', {}, idle));
106
177
  }
107
178
  };
179
+ const calls = new Calls(house, changed);
108
180
  const conversations = new Conversations({
109
181
  house,
110
182
  agents,
111
183
  copies,
112
- send: (frame) => stream?.send(frame) ?? false,
184
+ calls,
185
+ send: (oneWayCall) => connection?.send(oneWayCall) ?? false,
113
186
  changed,
114
187
  });
115
- const signIns = new SignIns(house, agents, changed);
116
- const updates = new Updates(house);
188
+ const signIns = new SignIns(house, agents, calls);
189
+ const update = new KitUpdate(calls);
190
+ takeUnservedRelease((release) => logged(update.takeRefused(release)));
191
+ removeInputs();
192
+ const scripts = new Scripts(calls, (oneWayCall) => connection?.send(oneWayCall) ?? false, house, copies);
193
+ running = { conversations, agents, calls, stop };
117
194
  for (const signal of ['SIGTERM', 'SIGINT']) {
118
195
  process.once(signal, () => {
119
- killDescendants();
120
- process.kill(process.pid, signal);
196
+ recordPlannedRestart();
197
+ void stop(() => {
198
+ removeInputs();
199
+ process.kill(process.pid, signal);
200
+ });
121
201
  });
122
202
  }
123
- await house.deliver('/kit/restarted', {}).catch(report);
124
- await updates.resumed().catch(report);
203
+ const unenrolled = () => {
204
+ if (!enrolled())
205
+ void stop(() => process.exit(1));
206
+ };
207
+ watch(kitHome(), unenrolled);
208
+ unenrolled();
209
+ await house.deliver('kits/report-restart', planned ? { planned } : {}).catch(report);
125
210
  logged(agents.refresh());
126
- stream = holdStream({
211
+ agents.keepCurrent(() => process.env.HOUSE_KIT_SANDBOX === '1' && reported);
212
+ connection = holdConnection({
127
213
  opened: () => {
128
214
  conversations.opened();
215
+ scripts.connected();
129
216
  changed();
130
217
  logged(copies.connected());
131
218
  },
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
144
- conversations.input(arrived);
219
+ closed: () => scripts.disconnected(),
220
+ call: (call) => {
221
+ if (call.kind === 'sign_in')
222
+ signIns.start(call);
223
+ else if (call.kind === 'update')
224
+ update.take(call);
225
+ else if (call.kind === 'settings') {
226
+ void calls.carrySettings(call.input_id, async () => {
227
+ const applied = await settle(call);
228
+ const result = 'refused' in applied ? { refused: applied.refused } : { settings: applied.settings };
229
+ if ('settings' in applied ? !applied.moved : !applied.ended)
230
+ return { result };
231
+ calls.keepResult(call.input_id, result);
232
+ return new Promise(() => answered(applied));
233
+ });
145
234
  }
235
+ else if (call.kind === 'run_wizard' || call.kind === 'run_function')
236
+ scripts.start(call);
237
+ else
238
+ conversations.call(call);
239
+ },
240
+ oneWayCall: (oneWayCall) => {
241
+ if (oneWayCall.type === 'watching')
242
+ conversations.watching(oneWayCall.watching);
243
+ if (oneWayCall.type === 'work_available' && oneWayCall.subject === 'agents')
244
+ logged(agents.refresh());
245
+ scripts.notified(oneWayCall);
146
246
  },
147
247
  });
148
248
  }
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;