@agentshouse/kit 0.2.1-cca496635583e → 0.2.1-ccf188e02985d

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 (94) hide show
  1. package/README.md +23 -22
  2. package/dist/acp.js +34 -14
  3. package/dist/agents.js +108 -21
  4. package/dist/api.js +5 -1
  5. package/dist/app-git.js +10 -26
  6. package/dist/bootstrap.json +4 -4
  7. package/dist/bridge.js +3 -6
  8. package/dist/chatgpt.js +32 -0
  9. package/dist/clis.js +51 -9
  10. package/dist/conversations.js +137 -70
  11. package/dist/copies.js +78 -34
  12. package/dist/end.js +5 -0
  13. package/dist/events.js +4 -0
  14. package/dist/files.js +8 -2
  15. package/dist/git.js +10 -8
  16. package/dist/home.js +56 -5
  17. package/dist/house-cli.js +291 -0
  18. package/dist/house-main.js +7 -189
  19. package/dist/inputs.js +37 -0
  20. package/dist/instructions.js +4 -28
  21. package/dist/kit-cli.js +68 -0
  22. package/dist/kit-main.js +8 -22
  23. package/dist/login.js +47 -13
  24. package/dist/logout.js +0 -2
  25. package/dist/parts.js +34 -16
  26. package/dist/placement.js +30 -0
  27. package/dist/relay.js +1 -1
  28. package/dist/resident.js +65 -16
  29. package/dist/service.js +22 -0
  30. package/dist/sign-in.js +5 -23
  31. package/dist/skills.js +81 -12
  32. package/dist/stream.js +66 -17
  33. package/dist/transcription.js +3 -3
  34. package/dist/update.js +48 -29
  35. package/dist/wire.js +6 -0
  36. package/dist/work.js +3 -5
  37. package/how-we-work.md +13 -3
  38. package/package.json +2 -1
  39. package/skills/ask-matt/SKILL.md +17 -11
  40. package/skills/ask-matt/agents/openai.yaml +1 -0
  41. package/skills/code-review/SKILL.md +5 -5
  42. package/skills/codebase-design/DESIGN-IT-TWICE.md +1 -1
  43. package/skills/codebase-design/SKILL.md +0 -2
  44. package/skills/diagnosing-bugs/SKILL.md +2 -4
  45. package/skills/domain-modeling/{CONTEXT-FORMAT.md → GLOSSARY-FORMAT.md} +10 -10
  46. package/skills/domain-modeling/SKILL.md +12 -14
  47. package/skills/grill-me/SKILL.md +1 -2
  48. package/skills/grill-me/agents/openai.yaml +1 -0
  49. package/skills/grill-with-docs/SKILL.md +1 -2
  50. package/skills/grill-with-docs/agents/openai.yaml +1 -0
  51. package/skills/grilling/SKILL.md +2 -2
  52. package/skills/handoff/SKILL.md +2 -3
  53. package/skills/handoff/agents/openai.yaml +1 -0
  54. package/skills/implement/SKILL.md +5 -4
  55. package/skills/implement/agents/openai.yaml +1 -0
  56. package/skills/implement-spec/SKILL.md +40 -0
  57. package/skills/implement-spec/agents/openai.yaml +5 -0
  58. package/skills/improve-codebase-architecture/SKILL.md +6 -7
  59. package/skills/improve-codebase-architecture/agents/openai.yaml +1 -0
  60. package/skills/pr/CREDITS.md +3 -0
  61. package/skills/pr/LICENSE +21 -0
  62. package/skills/pr/SKILL.md +170 -0
  63. package/skills/pr/agents/openai.yaml +3 -0
  64. package/skills/prototype/SKILL.md +0 -2
  65. package/skills/research/SKILL.md +0 -2
  66. package/skills/retro/LICENSE +21 -0
  67. package/skills/retro/SKILL.md +44 -0
  68. package/skills/retro/agents/openai.yaml +5 -0
  69. package/skills/tdd/SKILL.md +2 -4
  70. package/skills/teach/SKILL.md +3 -4
  71. package/skills/teach/agents/openai.yaml +1 -0
  72. package/skills/to-questionnaire/SKILL.md +1 -2
  73. package/skills/to-questionnaire/agents/openai.yaml +1 -0
  74. package/skills/to-spec/SKILL.md +1 -2
  75. package/skills/to-spec/agents/openai.yaml +1 -0
  76. package/skills/to-tickets/SKILL.md +3 -4
  77. package/skills/to-tickets/agents/openai.yaml +1 -0
  78. package/skills/triage/AGENT-BRIEF.md +207 -0
  79. package/skills/triage/LICENSE +21 -0
  80. package/skills/triage/OUT-OF-SCOPE.md +105 -0
  81. package/skills/triage/SKILL.md +112 -0
  82. package/skills/triage/agents/openai.yaml +5 -0
  83. package/skills/wait-what/SKILL.md +2 -3
  84. package/skills/wait-what/agents/openai.yaml +1 -0
  85. package/skills/wayfinder/SKILL.md +5 -6
  86. package/skills/wayfinder/agents/openai.yaml +1 -0
  87. package/skills/wizard/LICENSE +21 -0
  88. package/skills/wizard/SKILL.md +44 -0
  89. package/skills/wizard/agents/openai.yaml +3 -0
  90. package/skills/wizard/template.sh +223 -0
  91. package/skills/writing-for-agents/SKILL.md +0 -2
  92. package/skills/resolving-merge-conflicts/SKILL.md +0 -16
  93. package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
  94. /package/skills/{resolving-merge-conflicts → implement-spec}/LICENSE +0 -0
package/dist/login.js CHANGED
@@ -2,13 +2,16 @@ import { spawn } from 'node:child_process';
2
2
  import { createHash, randomBytes } from 'node:crypto';
3
3
  import { hostname } from 'node:os';
4
4
  import { parseArgs } from 'node:util';
5
- import { post, retrying } from './api.js';
5
+ import { HouseRefusal, post, retrying } from './api.js';
6
6
  import { placeAppGit } from './app-git.js';
7
- import { forgetOwnAgent, readDisconnected, readEnrolment, readOwnAgent, writeEnrolment, writeOwnAgent } from './home.js';
7
+ import { writeCodexRule } from './clis.js';
8
+ import { forgetDisconnected, forgetOwnAgent, readDisconnected, readEnrolment, readOwnAgent, writeEnrolment, writeOwnAgent, } from './home.js';
8
9
  import { callHouse, rpc } from './mcp.js';
10
+ import { refusalOf } from './refusals.js';
9
11
  import { selectRelay } from './relay.js';
10
12
  import { startService } from './service.js';
11
- const DEFAULT_HOUSE = 'https://agents.house';
13
+ import { placeSkillSet } from './skills.js';
14
+ export const DEFAULT_HOUSE = 'https://agents.house';
12
15
  const OPENER = process.platform === 'darwin' ? 'open' : 'xdg-open';
13
16
  // Polling every two seconds brings the User's confirmation to the waiting Kit moments after they give it.
14
17
  const POLL_INTERVAL_MS = 2000;
@@ -17,7 +20,7 @@ function open(link) {
17
20
  .on('error', () => undefined)
18
21
  .unref();
19
22
  }
20
- async function accepted(house, credential) {
23
+ export async function accepted(house, credential) {
21
24
  const checked = await callHouse(house, credential, rpc('tools/list', {}));
22
25
  if (checked.status === 200)
23
26
  return true;
@@ -25,13 +28,24 @@ async function accepted(house, credential) {
25
28
  return false;
26
29
  throw new Error(`House answered ${checked.status} when Kit checked this computer's own agent's connection: ${checked.text}`);
27
30
  }
28
- const USAGE = `usage: kit login [--house <origin>] [--environment <id>] [--rooms <ref>[,<ref>...] | --no-private] [--code <code>]
31
+ const USAGE = `usage: kit login [--house <origin>] [--environment <id>] [--rooms <ref>[,<ref>...] | --no-private]
32
+ kit login [--house <origin>] --code <code>
29
33
  --house <origin> the House to connect to, ${DEFAULT_HOUSE} unless one is stored
30
34
  --environment <id> reconnect this Environment
31
35
  --rooms <ref>[,<ref>...] give this Environment only these Rooms
32
36
  --no-private give this Environment every Room but your Private Room
33
37
  --code <code> connect this machine with a login code
34
38
  `;
39
+ function connected(house, named, environment) {
40
+ if (named === undefined)
41
+ return `Environment ${environment} is connected to ${house}.\n`;
42
+ if (named === environment)
43
+ return `Environment ${environment} is reconnected to ${house}.\n`;
44
+ return `Environment ${named} stays with its account; this computer is connected to ${house} as new Environment ${environment} of the account that confirmed.\n`;
45
+ }
46
+ function gone(error) {
47
+ return error instanceof HouseRefusal && refusalOf(error.text)?.code === 'kit_environment_not_found';
48
+ }
35
49
  function chosenRooms(rooms, noPrivate) {
36
50
  if (rooms !== undefined && noPrivate)
37
51
  throw new Error('--rooms and --no-private are two choices of Rooms; pass one');
@@ -70,9 +84,11 @@ export async function login(argv) {
70
84
  selectRelay(relay);
71
85
  const house = values.house ?? enrolled?.house ?? DEFAULT_HOUSE;
72
86
  const replaces = process.env.HOUSE_KIT_REPLACES;
73
- const environment = replaces === undefined ? (values.environment ?? (enrolled?.house === house ? enrolled.environment : undefined)) : undefined;
87
+ const remembered = replaces === undefined && values.environment === undefined && enrolled?.house === house ? enrolled.environment : undefined;
88
+ const environment = replaces === undefined ? (values.environment ?? remembered) : undefined;
74
89
  const ownAgent = await readOwnAgent();
75
90
  let held = false;
91
+ let named = environment;
76
92
  let exchanged;
77
93
  if (values.code !== undefined) {
78
94
  exchanged = await post(house, '/kit/token', { code: values.code, label: hostname() });
@@ -80,34 +96,52 @@ export async function login(argv) {
80
96
  else {
81
97
  held = ownAgent !== null && ownAgent.house === house && (await accepted(house, ownAgent.credential));
82
98
  const verifier = randomBytes(32).toString('base64url');
83
- const started = await retrying(() => post(house, '/kit', {
99
+ const start = () => retrying(() => post(house, '/kit', {
84
100
  label: hostname(),
85
- ...(environment === undefined
101
+ ...(named === undefined
86
102
  ? { act: 'new_environment', ...(replaces === undefined ? {} : { replaces }) }
87
- : { act: 'existing_environment', environment_id: environment }),
103
+ : { act: 'existing_environment', environment_id: named }),
88
104
  ...rooms,
89
105
  code_challenge: createHash('sha256').update(verifier).digest('base64url'),
90
106
  ...(held ? {} : { headless: true }),
91
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
+ }
92
120
  process.stdout.write(`Open this link and confirm: ${started.start_url}\n`);
93
121
  open(started.start_url);
94
122
  exchanged = await retrying(() => post(house, '/kit/token', { login: started.login, code_verifier: verifier }), undefined, () => POLL_INTERVAL_MS);
95
123
  }
124
+ await placeSkillSet();
125
+ await writeCodexRule().catch((error) => process.stderr.write(`kit: ${error.message}\n`));
126
+ const relayRecord = relay === undefined ? {} : { relay };
96
127
  await writeEnrolment({
97
128
  house,
98
- ...(relay === undefined ? {} : { relay }),
129
+ ...relayRecord,
99
130
  environment: exchanged.environment,
100
131
  credential: exchanged.credential,
101
132
  });
102
- process.stdout.write(`Environment ${exchanged.environment} is connected to ${house}.\n`);
133
+ process.stdout.write(connected(house, named, exchanged.environment));
103
134
  if (exchanged.headless_credential !== undefined) {
104
- await writeOwnAgent({ house, user: exchanged.user, credential: exchanged.headless_credential });
135
+ await writeOwnAgent({ house, ...relayRecord, user: exchanged.user, credential: exchanged.headless_credential });
105
136
  }
106
137
  else if (held && ownAgent !== null && ownAgent.user !== exchanged.user) {
107
138
  await forgetOwnAgent();
108
139
  process.stdout.write("This computer's own agent was another User's; run kit login again to connect it to you.\n");
109
140
  }
110
- 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`));
111
145
  try {
112
146
  startService();
113
147
  }
package/dist/logout.js CHANGED
@@ -1,5 +1,4 @@
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();
@@ -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/parts.js CHANGED
@@ -2,7 +2,10 @@ export class Parts {
2
2
  parts = [];
3
3
  context = null;
4
4
  emit;
5
+ made = [];
6
+ events = 0;
5
7
  writing = null;
8
+ counting = null;
6
9
  planned = null;
7
10
  constructor(emit) {
8
11
  this.emit = emit;
@@ -10,32 +13,34 @@ export class Parts {
10
13
  write(type, id, text) {
11
14
  if (text === '')
12
15
  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 });
16
+ if (this.writing?.type === type && this.writing.id === id) {
17
+ this.parts[this.writing.index].text += text;
18
+ this.emit({ op: 'append', index: this.writing.index, text });
18
19
  return;
19
20
  }
20
- this.start({ type, text });
21
- this.writing = { type, id };
21
+ this.writing = { type, id, index: this.start({ type, text }) };
22
22
  }
23
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 });
24
+ if (this.counting === null) {
25
+ this.counting = this.start({ type: 'commands', count: 1 });
28
26
  return;
29
27
  }
28
+ const part = this.parts[this.counting];
30
29
  part.count++;
31
- this.emit({ op: 'count', index: last, count: part.count });
30
+ this.emit({ op: 'count', index: this.counting, count: part.count });
32
31
  }
33
32
  start(part) {
34
33
  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;
34
+ this.counting = null;
35
+ return this.add(part, ++this.events);
36
+ }
37
+ reserve() {
38
+ this.writing = null;
39
+ this.counting = null;
40
+ return ++this.events;
41
+ }
42
+ arrive(part, reserved) {
43
+ this.add(part, reserved);
39
44
  }
40
45
  done(index, result) {
41
46
  const part = this.parts[index];
@@ -55,10 +60,23 @@ export class Parts {
55
60
  this.context = context;
56
61
  this.emit({ op: 'context', ...context });
57
62
  }
63
+ saved() {
64
+ return this.parts
65
+ .map((part, index) => ({ part, made: this.made[index] }))
66
+ .sort((a, b) => a.made - b.made)
67
+ .map(({ part }) => part);
68
+ }
58
69
  operations() {
59
70
  return [
60
71
  ...this.parts.map((part, index) => ({ op: 'start', index, part })),
61
72
  ...(this.context === null ? [] : [{ op: 'context', ...this.context }]),
62
73
  ];
63
74
  }
75
+ add(part, made) {
76
+ this.parts.push(part);
77
+ this.made.push(made);
78
+ const index = this.parts.length - 1;
79
+ this.emit({ op: 'start', index, part });
80
+ return index;
81
+ }
64
82
  }
@@ -0,0 +1,30 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { run } from './clis.js';
4
+ import { kitHome } from './home.js';
5
+ export function service() {
6
+ if (process.platform !== 'linux' || !process.env.INVOCATION_ID)
7
+ return null;
8
+ return process.env.MANAGERPID ? 'user' : 'system';
9
+ }
10
+ export function throughSudo() {
11
+ return service() === 'system' && process.getuid() !== 0;
12
+ }
13
+ export async function updatesItself() {
14
+ if (service() === null)
15
+ return false;
16
+ if (!throughSudo())
17
+ return true;
18
+ // sudo answers at once, so ten seconds only bounds a stuck sudo.
19
+ return (await run('sudo', ['-n', 'true'], 10_000)).status === 0;
20
+ }
21
+ export function installCommand() {
22
+ try {
23
+ return readFileSync(join(kitHome(), 'install-command'), 'utf8').trim();
24
+ }
25
+ catch (error) {
26
+ if (error.code === 'ENOENT')
27
+ return null;
28
+ throw error;
29
+ }
30
+ }
package/dist/relay.js CHANGED
@@ -2,7 +2,7 @@ let selected;
2
2
  export function selectRelay(relay) {
3
3
  selected = relay === undefined || relay === '' ? null : relay;
4
4
  }
5
- function relayOrigin() {
5
+ export function relayOrigin() {
6
6
  if (selected !== undefined)
7
7
  return selected ?? undefined;
8
8
  const ambient = process.env.HOUSE_KIT_RELAY;
package/dist/resident.js CHANGED
@@ -1,16 +1,18 @@
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 { Conversations } from './conversations.js';
11
+ import { CONVERSATION_INPUTS, Conversations } from './conversations.js';
11
12
  import { LocalCopies } from './copies.js';
12
13
  import { restart } from './end.js';
13
- import { agentBase, enrolled, forgetRestart, kitHome, restartAsked } from './home.js';
14
+ import { agentBase, enrolled, forgetRestart, kitHome, ownerLock, ownerSocket, restartAsked } from './home.js';
15
+ import { Inputs } from './inputs.js';
14
16
  import { killDescendants } from './scope.js';
15
17
  import { quoted } from './shell.js';
16
18
  import { SignIns } from './sign-in.js';
@@ -29,13 +31,32 @@ async function installShim(path, module) {
29
31
  await mkdir(dirname(path), { recursive: true });
30
32
  await writeFile(path, `#!/bin/sh\nexec ${quoted(process.execPath)} ${quoted(main)} "$@"\n`, { mode: 0o755 });
31
33
  }
34
+ function claimed(lock) {
35
+ try {
36
+ lock.exec('BEGIN EXCLUSIVE');
37
+ return true;
38
+ }
39
+ catch (error) {
40
+ if (error.errcode === 5)
41
+ return false;
42
+ throw error;
43
+ }
44
+ }
32
45
  async function serveOwner(copies) {
33
- const socketPath = join(kitHome(), 'owner.sock');
34
- await rm(socketPath, { force: true });
46
+ const lock = new DatabaseSync(ownerLock());
47
+ if (!claimed(lock)) {
48
+ lock.close();
49
+ const running = await runningResident();
50
+ throw new Error(`${running === null ? 'another Kit' : `Kit ${running}`} already runs this Environment from Kit home ${kitHome()}; stop it first to run another here`);
51
+ }
35
52
  const server = createServer(async (request, response) => {
36
53
  try {
37
54
  const text = await received(request);
38
55
  let answered;
56
+ if (request.url === '/resident') {
57
+ response.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify({ pid: process.pid }));
58
+ return;
59
+ }
39
60
  if (request.url === '/git/fetch') {
40
61
  answered = await copies.fetch();
41
62
  }
@@ -53,9 +74,23 @@ async function serveOwner(copies) {
53
74
  response.writeHead(502).end(error instanceof Error ? error.message : String(error));
54
75
  }
55
76
  });
56
- server.listen(socketPath);
77
+ server.on('close', () => lock.close());
78
+ await rm(ownerSocket(), { force: true });
79
+ server.listen(ownerSocket());
57
80
  await once(server, 'listening');
58
81
  }
82
+ function runningResident() {
83
+ return new Promise((resolve, reject) => {
84
+ get({ socketPath: ownerSocket(), path: '/resident' }, (answer) => {
85
+ received(answer).then((text) => resolve(JSON.parse(text).pid), reject);
86
+ }).on('error', (error) => {
87
+ if (error.code === 'ENOENT' || error.code === 'ECONNREFUSED')
88
+ resolve(null);
89
+ else
90
+ reject(error);
91
+ });
92
+ });
93
+ }
59
94
  function connected() {
60
95
  return new Promise((resolve) => {
61
96
  const watcher = watch(kitHome(), () => {
@@ -72,7 +107,7 @@ function connected() {
72
107
  resolve();
73
108
  });
74
109
  }
75
- export async function resident() {
110
+ async function started() {
76
111
  await connected();
77
112
  await forgetRestart();
78
113
  watch(kitHome(), () => {
@@ -83,16 +118,23 @@ export async function resident() {
83
118
  });
84
119
  await placeSkillSet();
85
120
  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
121
  await installShim(join(kitHome(), 'shim', 'git'), 'git-main');
91
122
  await installShim(codexLauncher(), 'codex-main');
123
+ }
124
+ export async function resident() {
125
+ await mkdir(kitHome(), { recursive: true, mode: 0o700 });
126
+ const copies = new LocalCopies(house);
127
+ await copies.load();
92
128
  await serveOwner(copies);
129
+ await started().catch((error) => {
130
+ report(error);
131
+ process.exit(1);
132
+ });
133
+ process.env.HOUSE_KIT_RESIDENT = String(process.pid);
134
+ const agents = new Agents(house, () => changed());
93
135
  let stream = null;
94
136
  let reported = false;
95
- const idle = () => conversations.idle() && signIns.idle();
137
+ const idle = () => inputs.idle() && conversations.idle() && agents.idle();
96
138
  const changed = () => {
97
139
  if (!idle()) {
98
140
  reported = false;
@@ -105,15 +147,17 @@ export async function resident() {
105
147
  logged(house.deliver('/kit/idle', {}, idle));
106
148
  }
107
149
  };
150
+ const inputs = new Inputs(house, changed);
108
151
  const conversations = new Conversations({
109
152
  house,
110
153
  agents,
111
154
  copies,
155
+ inputs,
112
156
  send: (frame) => stream?.send(frame) ?? false,
113
157
  changed,
114
158
  });
115
- const signIns = new SignIns(house, agents, changed);
116
- const updates = new Updates(house);
159
+ const signIns = new SignIns(house, agents, inputs);
160
+ const updates = new Updates(inputs);
117
161
  for (const signal of ['SIGTERM', 'SIGINT']) {
118
162
  process.once(signal, () => {
119
163
  killDescendants();
@@ -132,16 +176,21 @@ export async function resident() {
132
176
  frame: (arrived) => {
133
177
  if (arrived.type === 'watching')
134
178
  conversations.watching(arrived.watching === true);
179
+ if (arrived.type === 'authority_changed')
180
+ logged(copies.changed(arrived.rooms));
135
181
  if (arrived.type === 'work_available' && arrived.subject === 'agents')
136
182
  logged(agents.refresh());
137
183
  if (arrived.type === 'input') {
138
- reported = false;
139
184
  if (arrived.kind === 'sign_in')
140
185
  signIns.start(arrived);
141
186
  else if (arrived.kind === 'update')
142
187
  updates.start(arrived);
143
- else
188
+ else if (CONVERSATION_INPUTS.has(arrived.kind))
144
189
  conversations.input(arrived);
190
+ else {
191
+ const refused = { refused: `This Kit does not know ${arrived.kind}; update the Kit` };
192
+ void inputs.carry(String(arrived.input_id), async () => ({ ack: refused }));
193
+ }
145
194
  }
146
195
  },
147
196
  });
package/dist/service.js CHANGED
@@ -1,4 +1,7 @@
1
1
  import { spawnSync } from 'node:child_process';
2
+ import { existsSync, realpathSync } from 'node:fs';
3
+ import { resolve } from 'node:path';
4
+ import { kitHome } from './home.js';
2
5
  const UNIT = 'house-kit.service';
3
6
  // launchctl and systemctl answer at once, so ten seconds only bounds a stuck service manager.
4
7
  const MANAGER_MS = 10_000;
@@ -12,18 +15,37 @@ function start(command, args) {
12
15
  const cause = started.error?.message ?? (started.stderr.trim() || `${command} exited with ${started.status ?? 'a signal'}`);
13
16
  throw new Error(`the House Kit service could not be started: ${cause}`);
14
17
  }
18
+ function physical(path) {
19
+ return existsSync(path) ? realpathSync(path) : resolve(path);
20
+ }
21
+ function systemdHome(environment) {
22
+ for (const [, quoted, bare] of environment.matchAll(/"((?:[^"\\]|\\.)*)"|(\S+)/g)) {
23
+ const assignment = bare ?? quoted.replace(/\\(.)/g, '$1');
24
+ if (assignment.startsWith('HOUSE_KIT_HOME='))
25
+ return assignment.slice('HOUSE_KIT_HOME='.length);
26
+ }
27
+ return undefined;
28
+ }
29
+ function runsThisHome(served) {
30
+ return served !== undefined && physical(served) === physical(kitHome());
31
+ }
15
32
  export function startService() {
16
33
  if (process.platform === 'darwin') {
17
34
  const service = `gui/${process.getuid()}/house-kit`;
18
35
  const printed = manage('launchctl', ['print', service]);
19
36
  if (printed.status !== 0 || /^\tstate = running$/m.test(printed.stdout))
20
37
  return false;
38
+ if (!runsThisHome(/^\t+HOUSE_KIT_HOME => (.+)$/m.exec(printed.stdout)?.[1]))
39
+ return false;
21
40
  start('launchctl', ['kickstart', service]);
22
41
  return true;
23
42
  }
24
43
  for (const scope of [['--user'], []]) {
25
44
  if (manage('systemctl', [...scope, 'is-enabled', '--quiet', UNIT]).status !== 0)
26
45
  continue;
46
+ const environment = manage('systemctl', [...scope, 'show', '--property=Environment', '--value', UNIT]).stdout;
47
+ if (!runsThisHome(systemdHome(environment)))
48
+ continue;
27
49
  if (manage('systemctl', [...scope, 'is-active', '--quiet', UNIT]).status === 0)
28
50
  return false;
29
51
  start('systemctl', [...scope, 'start', UNIT]);
package/dist/sign-in.js CHANGED
@@ -9,36 +9,20 @@ const CODE = /^[A-Z0-9]+-[A-Z0-9]+$/;
9
9
  function causeOf(error) {
10
10
  return error instanceof Error ? error.message : String(error);
11
11
  }
12
- function logged(error) {
13
- process.stderr.write(`kit: ${causeOf(error)}\n`);
14
- }
15
12
  function endOf(signal) {
16
13
  return new Promise((resolve) => signal.addEventListener('abort', () => resolve(true), { once: true }));
17
14
  }
18
15
  export class SignIns {
19
16
  house;
20
17
  agents;
21
- changed;
22
- running = new Set();
23
- constructor(house, agents, changed) {
18
+ inputs;
19
+ constructor(house, agents, inputs) {
24
20
  this.house = house;
25
21
  this.agents = agents;
26
- this.changed = changed;
27
- }
28
- idle() {
29
- return this.running.size === 0;
22
+ this.inputs = inputs;
30
23
  }
31
24
  start(input) {
32
- if (this.running.has(input.input_id))
33
- return;
34
- this.running.add(input.input_id);
35
- this.changed();
36
- this.signIn(input)
37
- .catch(logged)
38
- .finally(() => {
39
- this.running.delete(input.input_id);
40
- this.changed();
41
- });
25
+ void this.inputs.carry(input.input_id, () => this.signIn(input));
42
26
  }
43
27
  async signIn(input) {
44
28
  const ending = new AbortController();
@@ -48,9 +32,7 @@ export class SignIns {
48
32
  clearTimeout(timer);
49
33
  if (ended)
50
34
  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);
35
+ return ended ? { ack: {} } : { ack: {}, acknowledged: () => this.agents.reread(input.cli) };
54
36
  }
55
37
  async signedIn(input, signal) {
56
38
  await this.login(input, signal);
package/dist/skills.js CHANGED
@@ -1,17 +1,31 @@
1
+ import { createHash } from 'node:crypto';
1
2
  import { readFileSync } from 'node:fs';
2
- import { cp, lstat, mkdir, readdir, realpath, rm, symlink, writeFile } from 'node:fs/promises';
3
+ import { cp, lstat, mkdir, readdir, realpath, rm, symlink } from 'node:fs/promises';
3
4
  import { homedir } from 'node:os';
4
5
  import { join, relative } from 'node:path';
5
6
  import { fileURLToPath } from 'node:url';
7
+ import { parse } from 'yaml';
6
8
  import { KIT_VERSION } from './clis.js';
7
9
  import { kitHome, readConfiguration, readHome, writeHome } from './home.js';
8
10
  import { refusalOf } from './refusals.js';
9
- export const HOW_WE_WORK = '/rooms/private/how-we-work.md';
11
+ const HOW_WE_WORK = '/rooms/private/how-we-work.md';
10
12
  const SKILL_SET = fileURLToPath(new URL('../skills/', import.meta.url));
11
13
  const HOW_WE_WORK_TEXT = readFileSync(new URL('../how-we-work.md', import.meta.url), 'utf8');
14
+ const SHIPPED_HOW_WE_WORK = [
15
+ 'ae4dda60da7b174f22dc5fa9bf07fc27c25864932a74230de4f9f509ddd91f41',
16
+ '0bdcd43e1603ba6673a3cae56d70bbf90657cbeefd124e91a47be2ca1cbd6c93',
17
+ '4106a87ec66da6f7b2a10e74d18e6559d68d5f4fe7415dae68f5c7bdb9081968',
18
+ 'ac814a2197038f037b657bb719340395f89f319ac9fcdd386629c3fe2cec68f1',
19
+ '29951ee394aad9979eecfed3e5dc32c2c86e2226b6a380db5dbd4f305f4d1156',
20
+ ];
12
21
  const RECORD = 'skill-set.json';
13
- const CREATED = 'how-we-work-created';
22
+ const WRITTEN = 'how-we-work.json';
14
23
  const UNWRITABLE = new Set(['room_not_found', 'operation_denied']);
24
+ const PRESENT = new Set(['path_exists']);
25
+ const CHANGED = new Set(['base_revision_stale', 'path_absent']);
26
+ const UNREADABLE = /^(?:\S+: )?(?:path_not_found|room_not_found|operation_denied)\b/;
27
+ const FAILED = /^exit: [1-9]\d*$/;
28
+ const CUT = /^stderr: shell: output_cut (\d+) of \d+ bytes; continue with: (.+)$/;
15
29
  function skillsDirectory() {
16
30
  return join(homedir(), '.agents', 'skills');
17
31
  }
@@ -49,6 +63,14 @@ async function install() {
49
63
  await writeHome(RECORD, { version: KIT_VERSION, names });
50
64
  }
51
65
  export async function placeSkillSet() {
66
+ try {
67
+ await replaceSkillSet();
68
+ }
69
+ catch (error) {
70
+ process.stderr.write(`kit: the skills could not be placed: ${error.message}\n`);
71
+ }
72
+ }
73
+ async function replaceSkillSet() {
52
74
  const accepted = (await readConfiguration()).skills;
53
75
  const installed = await readHome(RECORD);
54
76
  if (accepted && installed?.version === KIT_VERSION)
@@ -62,17 +84,64 @@ export async function placeSkillSet() {
62
84
  else
63
85
  await rm(join(kitHome(), RECORD), { force: true });
64
86
  }
87
+ function sha256(text) {
88
+ return createHash('sha256').update(text).digest('hex');
89
+ }
90
+ function cutOf(lines) {
91
+ const cut = CUT.exec(lines[0] ?? '');
92
+ return cut !== null && Buffer.byteLength(lines.slice(1).join('\n')) === Number(cut[1]) ? cut : null;
93
+ }
94
+ export async function readHowWeWork(bridge) {
95
+ let document = '';
96
+ for (let command = `cat ${HOW_WE_WORK}`; command !== undefined;) {
97
+ const read = await bridge.tool('shell', { command });
98
+ const text = read.content.map((content) => content.text).join('\n');
99
+ const lines = text.split('\n');
100
+ if (read.isError === true || (FAILED.test(lines[0] ?? '') && lines[1]?.startsWith('stderr: '))) {
101
+ const refusal = read.isError === true ? text : lines.slice(1).join('\n').replace(/^stderr: /gm, '');
102
+ if (UNREADABLE.test(refusal))
103
+ return '';
104
+ throw new Error(refusal);
105
+ }
106
+ const cut = cutOf(lines);
107
+ document += lines.slice(cut === null ? 0 : 1).join('\n');
108
+ command = cut?.[2];
109
+ }
110
+ return document;
111
+ }
112
+ async function currentRevision(bridge) {
113
+ const inspected = await bridge.tool('inspect', { path: HOW_WE_WORK });
114
+ const text = inspected.content.map((content) => content.text).join('\n');
115
+ if (inspected.isError === true)
116
+ throw new Error(text);
117
+ return parse(text).revisions[0].revision;
118
+ }
119
+ async function edit(bridge, change, kept) {
120
+ const edited = await bridge.tool('edit', { changes: [{ ...change, path: HOW_WE_WORK, content: HOW_WE_WORK_TEXT }] });
121
+ if (edited.isError !== true)
122
+ return 'written';
123
+ const text = edited.content.map((content) => content.text).join('\n');
124
+ const refusal = refusalOf(text);
125
+ if (UNWRITABLE.has(refusal?.code ?? ''))
126
+ return 'unwritable';
127
+ if (refusal?.code === 'edit_conflict' && refusal.conflicts.some((conflict) => kept.has(conflict)))
128
+ return 'kept';
129
+ throw new Error(text);
130
+ }
65
131
  export async function createHowWeWork(bridge) {
66
- if ((await readHome(RECORD)) === null || (await present(join(kitHome(), CREATED))))
132
+ if (!(await readConfiguration()).skills || (await readHome(RECORD))?.version === undefined)
133
+ return;
134
+ const current = sha256(HOW_WE_WORK_TEXT);
135
+ if ((await readHome(WRITTEN)) === current)
136
+ return;
137
+ const created = await edit(bridge, { op: 'create' }, PRESENT);
138
+ if (created === 'unwritable')
67
139
  return;
68
- const created = await bridge.tool('edit', { changes: [{ op: 'create', path: HOW_WE_WORK, content: HOW_WE_WORK_TEXT }] });
69
- if (created.isError === true) {
70
- const text = created.content.map((content) => content.text).join('\n');
71
- const refusal = refusalOf(text);
72
- if (UNWRITABLE.has(refusal?.code ?? ''))
140
+ if (created === 'kept') {
141
+ const base = await currentRevision(bridge);
142
+ const held = sha256(await readHowWeWork(bridge));
143
+ if (held !== current && SHIPPED_HOW_WE_WORK.includes(held) && (await edit(bridge, { op: 'replace', base }, CHANGED)) === 'unwritable')
73
144
  return;
74
- if (refusal?.code !== 'edit_conflict' || !refusal.conflicts.includes('path_exists'))
75
- throw new Error(text);
76
145
  }
77
- await writeFile(join(kitHome(), CREATED), '');
146
+ await writeHome(WRITTEN, current);
78
147
  }