@agentshouse/kit 0.2.1-cf268dff1869b → 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 (54) hide show
  1. package/README.md +18 -25
  2. package/dist/acp.js +12 -9
  3. package/dist/afk.js +17 -0
  4. package/dist/agents.js +72 -26
  5. package/dist/api.js +18 -4
  6. package/dist/binaries.js +60 -0
  7. package/dist/bridge.js +99 -44
  8. package/dist/calls.js +18 -1
  9. package/dist/chunks.js +54 -0
  10. package/dist/clis.js +33 -15
  11. package/dist/connect.js +121 -0
  12. package/dist/connection.js +25 -10
  13. package/dist/content.js +106 -0
  14. package/dist/conversations.js +251 -158
  15. package/dist/copies.js +1260 -599
  16. package/dist/earlier.js +273 -0
  17. package/dist/end.js +6 -9
  18. package/dist/events.js +544 -297
  19. package/dist/files.js +77 -11
  20. package/dist/git.js +0 -70
  21. package/dist/grok.js +39 -0
  22. package/dist/home.js +86 -22
  23. package/dist/house-cli.js +145 -162
  24. package/dist/instructions.js +2 -4
  25. package/dist/keys.js +238 -0
  26. package/dist/kit-cli.js +14 -4
  27. package/dist/login.js +34 -20
  28. package/dist/logout.js +2 -2
  29. package/dist/manifest.js +75 -0
  30. package/dist/mcp.js +43 -7
  31. package/dist/owner.js +46 -0
  32. package/dist/placement.js +0 -6
  33. package/dist/refusals.js +50 -23
  34. package/dist/resident.js +117 -76
  35. package/dist/scripts.js +79 -15
  36. package/dist/secret-input.js +1 -1
  37. package/dist/settings.js +247 -0
  38. package/dist/sign-in.js +49 -4
  39. package/dist/supervisor.js +163 -0
  40. package/dist/transcription.js +5 -2
  41. package/dist/update.js +31 -22
  42. package/dist/wire.js +2 -10
  43. package/dist/work.js +27 -2
  44. package/dist/workspace.js +113 -0
  45. package/package.json +12 -8
  46. package/dist/authority-main.js +0 -22
  47. package/dist/chatgpt.js +0 -32
  48. package/dist/configure-main.js +0 -6
  49. package/dist/enrol-main.js +0 -10
  50. package/dist/git-main.js +0 -153
  51. package/dist/parts.js +0 -82
  52. package/dist/service-main.js +0 -9
  53. package/dist/service.js +0 -55
  54. package/dist/translate.js +0 -45
package/dist/resident.js CHANGED
@@ -1,20 +1,23 @@
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, get } from 'node:http';
4
+ import { createServer } from 'node:http';
5
5
  import { dirname, extname, join } from 'node:path';
6
- import { DatabaseSync } from 'node:sqlite';
7
6
  import { fileURLToPath } from 'node:url';
8
7
  import { Agents } from './agents.js';
9
8
  import { house } from './api.js';
10
9
  import { received } from './bridge.js';
11
- import { CONVERSATION_METHODS, Conversations } from './conversations.js';
12
- import { LocalCopies } from './copies.js';
13
- import { restart, unknownMethod } from './end.js';
14
- import { agentBase, enrolled, kitHome, ownerLock, ownerSocket, readEnrolment } from './home.js';
10
+ import { Conversations } from './conversations.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';
15
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';
16
18
  import { killDescendants } from './scope.js';
17
19
  import { removeInputs, Scripts } from './scripts.js';
20
+ import { applySettings, enrolledRoutes, launchDirectories, moveIntoHouse, resumeWorkspaceMove } from './settings.js';
18
21
  import { quoted } from './shell.js';
19
22
  import { SignIns } from './sign-in.js';
20
23
  import { placeSkillSet } from './skills.js';
@@ -33,38 +36,27 @@ async function installShim(path, module) {
33
36
  await mkdir(dirname(path), { recursive: true });
34
37
  await writeFile(path, `#!/bin/sh\nexec ${quoted(process.execPath)} ${quoted(main)} "$@"\n`, { mode: 0o755 });
35
38
  }
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
- }
47
- async function serveOwner(copies) {
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
- }
39
+ async function serveOwner(lock, copies, settling) {
54
40
  const server = createServer(async (request, response) => {
55
41
  try {
56
42
  const text = await received(request);
57
43
  let answered;
58
44
  if (request.url === '/resident') {
59
- response.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify({ pid: process.pid }));
45
+ response.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify({ pid: process.pid, release: KIT_RELEASE }));
60
46
  return;
61
47
  }
62
- if (request.url === '/git/fetch') {
63
- answered = await copies.fetch();
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 });
64
51
  }
65
- else if (request.url === '/git/push') {
66
- const { cwd, commit } = JSON.parse(text);
67
- answered = await copies.push(cwd, commit, copies.owner);
52
+ else if (request.url === '/copy/upload') {
53
+ const { cwd } = JSON.parse(text);
54
+ answered = await copies.upload(cwd, copies.owner);
55
+ }
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;
68
60
  }
69
61
  else {
70
62
  response.writeHead(404).end();
@@ -81,18 +73,6 @@ async function serveOwner(copies) {
81
73
  server.listen(ownerSocket());
82
74
  await once(server, 'listening');
83
75
  }
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
- }
96
76
  function connected() {
97
77
  return new Promise((resolve) => {
98
78
  const watcher = watch(kitHome(), () => {
@@ -111,22 +91,59 @@ function connected() {
111
91
  }
112
92
  async function started() {
113
93
  await connected();
114
- watch(kitHome(), () => {
115
- if (enrolled())
116
- return;
117
- killDescendants();
118
- process.exit(1);
119
- });
120
94
  await placeSkillSet((await readEnrolment()).house);
121
- await mkdir(agentBase(), { recursive: true });
122
- await installShim(join(kitHome(), 'shim', 'git'), 'git-main');
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 });
123
98
  await installShim(codexLauncher(), 'codex-main');
124
99
  }
125
100
  export async function resident() {
101
+ await moveEarlier();
126
102
  await mkdir(kitHome(), { recursive: true, mode: 0o700 });
127
- const copies = new LocalCopies(house);
103
+ const lock = await holdOwner();
104
+ await resumeWorkspaceMove();
105
+ const copies = new LocalCopies(house, launchDirectories);
128
106
  await copies.load();
129
- await serveOwner(copies);
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)
136
+ return;
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 });
130
147
  await started().catch((error) => {
131
148
  report(error);
132
149
  process.exit(1);
@@ -135,17 +152,28 @@ export async function resident() {
135
152
  const agents = new Agents(house, () => changed());
136
153
  let connection = null;
137
154
  let reported = false;
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
+ };
138
165
  const idle = () => calls.idle() && conversations.idle() && agents.idle();
139
166
  const changed = () => {
140
167
  if (!idle()) {
141
168
  reported = false;
142
169
  }
143
- else if (update.placedRelease() !== null && conversations.quiet()) {
144
- restart(`Kit ${update.placedRelease()} is placed; its supervisor starts it`);
170
+ else if (update.placedRelease() !== null && conversations.quiet() && process.env.HOUSE_KIT_SANDBOX !== '1') {
171
+ const placed = update.placedRelease();
172
+ void stop(() => restartPlaced(placed));
145
173
  }
146
174
  else if (!reported) {
147
175
  reported = true;
148
- logged(house.deliver('/kit/idle', {}, idle));
176
+ logged(house.deliver('kits/report-idle', {}, idle));
149
177
  }
150
178
  };
151
179
  const calls = new Calls(house, changed);
@@ -154,28 +182,37 @@ export async function resident() {
154
182
  agents,
155
183
  copies,
156
184
  calls,
157
- send: (notification) => connection?.send(notification) ?? false,
185
+ send: (oneWayCall) => connection?.send(oneWayCall) ?? false,
158
186
  changed,
159
187
  });
160
188
  const signIns = new SignIns(house, agents, calls);
161
189
  const update = new KitUpdate(calls);
162
- if (process.env.HOUSE_KIT_SANDBOX !== '1')
163
- takeUnservedRelease((release) => logged(update.takeRefused(release)));
190
+ takeUnservedRelease((release) => logged(update.takeRefused(release)));
164
191
  removeInputs();
165
- const scripts = new Scripts(calls, (notification) => connection?.send(notification) ?? false);
192
+ const scripts = new Scripts(calls, (oneWayCall) => connection?.send(oneWayCall) ?? false, house, copies);
193
+ running = { conversations, agents, calls, stop };
166
194
  for (const signal of ['SIGTERM', 'SIGINT']) {
167
195
  process.once(signal, () => {
168
- killDescendants();
169
- removeInputs();
170
- process.kill(process.pid, signal);
196
+ recordPlannedRestart();
197
+ void stop(() => {
198
+ removeInputs();
199
+ process.kill(process.pid, signal);
200
+ });
171
201
  });
172
202
  }
173
- await house.deliver('/kit/restarted', {}).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);
174
210
  logged(agents.refresh());
175
- agents.keepCurrent();
211
+ agents.keepCurrent(() => process.env.HOUSE_KIT_SANDBOX === '1' && reported);
176
212
  connection = holdConnection({
177
213
  opened: () => {
178
214
  conversations.opened();
215
+ scripts.connected();
179
216
  changed();
180
217
  logged(copies.connected());
181
218
  },
@@ -185,23 +222,27 @@ export async function resident() {
185
222
  signIns.start(call);
186
223
  else if (call.kind === 'update')
187
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
+ });
234
+ }
188
235
  else if (call.kind === 'run_wizard' || call.kind === 'run_function')
189
236
  scripts.start(call);
190
- else if (CONVERSATION_METHODS.has(call.kind))
237
+ else
191
238
  conversations.call(call);
192
- else {
193
- const refused = { refused: unknownMethod(call.kind) };
194
- void calls.carry(call.input_id, async () => ({ result: refused }));
195
- }
196
239
  },
197
- notification: (notification) => {
198
- if (notification.type === 'watching')
199
- conversations.watching(notification.watching === true);
200
- if (notification.type === 'authority_changed')
201
- logged(copies.changed(notification.rooms));
202
- if (notification.type === 'work_available' && notification.subject === 'agents')
240
+ oneWayCall: (oneWayCall) => {
241
+ if (oneWayCall.type === 'watching')
242
+ conversations.watching(oneWayCall.watching);
243
+ if (oneWayCall.type === 'work_available' && oneWayCall.subject === 'agents')
203
244
  logged(agents.refresh());
204
- scripts.notified(notification);
245
+ scripts.notified(oneWayCall);
205
246
  },
206
247
  });
207
248
  }
package/dist/scripts.js CHANGED
@@ -5,6 +5,7 @@ import { rmSync } from 'node:fs';
5
5
  import { mkdir, rm, writeFile } from 'node:fs/promises';
6
6
  import { constants } from 'node:os';
7
7
  import { join } from 'node:path';
8
+ import { openRunBridge } from './bridge.js';
8
9
  import { loginPath, onLoginPath } from './clis.js';
9
10
  import { kitHome } from './home.js';
10
11
  import { childEnvironment } from './relay.js';
@@ -15,6 +16,11 @@ const SIGNALLED = 128;
15
16
  const NEWLINE = 0x0a;
16
17
  const NO_BASH = "bash is not on the login shell's PATH";
17
18
  const LOST = { refused: 'the Kit lost its connection to House' };
19
+ const UNSENT_CHARS = 64_000;
20
+ // House holds a Web Wizard's run two minutes for its Kit to connect again, so the script waits as long for a dropped connection to come back.
21
+ const RETURN_MS = 120_000;
22
+ // House takes a run on again with its page's terminal size in the frames right after the Kit connects, so a script it sent nothing for in ten seconds is one House no longer carries.
23
+ const TAKEN_ON_MS = 10_000;
18
24
  function causeOf(error) {
19
25
  return error instanceof Error ? error.message : String(error);
20
26
  }
@@ -44,6 +50,12 @@ async function inputFile(value) {
44
50
  export function removeInputs() {
45
51
  rmSync(inputs(), { recursive: true, force: true });
46
52
  }
53
+ function latest(screen) {
54
+ if (screen.length <= UNSENT_CHARS)
55
+ return screen;
56
+ const line = screen.indexOf('\n', screen.length - UNSENT_CHARS);
57
+ return screen.slice(line < 0 ? screen.length - UNSENT_CHARS : line + 1);
58
+ }
47
59
  function struck(line, typed) {
48
60
  return typed.some((typedLine) => line.includes(typedLine));
49
61
  }
@@ -122,33 +134,68 @@ class KeptOutput {
122
134
  export class Scripts {
123
135
  calls;
124
136
  send;
137
+ house;
138
+ copies;
125
139
  runs = new Map();
126
- constructor(calls, send) {
140
+ constructor(calls, send, house, copies) {
127
141
  this.calls = calls;
128
142
  this.send = send;
143
+ this.house = house;
144
+ this.copies = copies;
129
145
  }
130
146
  start(call) {
131
147
  void this.calls.carry(call.input_id, () => this.run(call.input_id, planned(call)));
132
148
  }
133
- notified(notification) {
134
- const run = this.runs.get(String(notification.wizard));
149
+ notified(oneWayCall) {
150
+ if (!('wizard' in oneWayCall))
151
+ return;
152
+ const run = this.runs.get(oneWayCall.wizard);
135
153
  if (run === undefined)
136
154
  return;
137
- if (notification.type === 'wizard_input')
138
- this.typed(run, String(notification.data));
139
- if (notification.type === 'wizard_size')
140
- this.resized(run, { cols: Number(notification.cols), rows: Number(notification.rows) });
141
- if (notification.type === 'wizard_end' && run.plan.terminal === 'page')
155
+ this.waited(run, null);
156
+ if (oneWayCall.type === 'wizard_input')
157
+ this.typed(run, oneWayCall.data);
158
+ if (oneWayCall.type === 'wizard_size')
159
+ this.resized(run, { cols: oneWayCall.cols, rows: oneWayCall.rows });
160
+ if (oneWayCall.type === 'wizard_end' && run.plan.terminal === 'page')
142
161
  this.end(run, {});
143
162
  }
144
163
  disconnected() {
145
- for (const run of this.runs.values())
146
- this.end(run, LOST);
164
+ for (const run of this.runs.values()) {
165
+ if (run.plan.terminal === 'page')
166
+ this.waited(run, RETURN_MS);
167
+ else
168
+ this.end(run, LOST);
169
+ }
170
+ }
171
+ connected() {
172
+ for (const [id, run] of this.runs) {
173
+ if (run.waiting === null)
174
+ continue;
175
+ this.waited(run, TAKEN_ON_MS);
176
+ if (run.unsent !== '' && this.send({ type: 'wizard_output', wizard: id, data: run.unsent }))
177
+ run.unsent = '';
178
+ }
147
179
  }
148
180
  async run(id, plan) {
149
181
  const size = plan.terminal === 'page' ? null : FIRST_SIZE;
150
182
  const output = plan.function === null ? null : new KeptOutput(plan.function.outputCap);
151
- const run = { plan, input: null, output, process: null, pipes: [], terminal: null, size, typed: [], ended: null, sized: Promise.withResolvers() };
183
+ const run = {
184
+ plan,
185
+ input: null,
186
+ bridge: null,
187
+ closed: new AbortController(),
188
+ output,
189
+ process: null,
190
+ pipes: [],
191
+ terminal: null,
192
+ size,
193
+ typed: [],
194
+ unsent: '',
195
+ waiting: null,
196
+ ended: null,
197
+ sized: Promise.withResolvers(),
198
+ };
152
199
  if (size !== null)
153
200
  run.sized.resolve();
154
201
  this.runs.set(id, run);
@@ -157,6 +204,7 @@ export class Scripts {
157
204
  }
158
205
  finally {
159
206
  this.runs.delete(id);
207
+ run.bridge?.close();
160
208
  if (run.input !== null)
161
209
  await rm(run.input, { force: true });
162
210
  }
@@ -171,11 +219,18 @@ export class Scripts {
171
219
  return failed(plan, NO_BASH);
172
220
  const path = await loginPath();
173
221
  await run.sized.promise;
174
- if (plan.function !== null)
222
+ if (plan.function !== null) {
175
223
  run.input = await inputFile(plan.function.input);
224
+ run.bridge = await openRunBridge(this.house, this.copies, id, run.closed.signal);
225
+ }
176
226
  if (run.ended !== null)
177
227
  return run.ended;
178
- const environment = { ...childEnvironment(), PATH: path, ...(run.input === null ? {} : { HOUSE_INPUT: run.input }) };
228
+ const environment = {
229
+ ...childEnvironment(),
230
+ ...run.bridge?.env,
231
+ PATH: path,
232
+ ...(run.input === null ? {} : { HOUSE_INPUT: run.input }),
233
+ };
179
234
  if (plan.terminal === null) {
180
235
  ({ exited } = await this.piped(plan.script, shell, environment, run));
181
236
  }
@@ -184,7 +239,8 @@ export class Scripts {
184
239
  run.process = terminal;
185
240
  run.terminal = terminal;
186
241
  terminal.onData((data) => {
187
- this.send({ type: 'wizard_output', wizard: id, data });
242
+ if (!this.send({ type: 'wizard_output', wizard: id, data }))
243
+ run.unsent = latest(`${run.unsent}${data}`);
188
244
  output?.add(data);
189
245
  });
190
246
  for (const data of run.typed.splice(0))
@@ -193,7 +249,7 @@ export class Scripts {
193
249
  }
194
250
  }
195
251
  catch (error) {
196
- return failed(plan, causeOf(error));
252
+ return run.ended ?? failed(plan, causeOf(error));
197
253
  }
198
254
  let timedOut = false;
199
255
  // House sends a function run's time cap in seconds, and a run still going at its cap ends with its process group and the output pipes it holds.
@@ -242,10 +298,18 @@ export class Scripts {
242
298
  end(run, result) {
243
299
  if (run.ended !== null)
244
300
  return;
301
+ this.waited(run, null);
245
302
  run.ended = result;
303
+ run.closed.abort();
246
304
  run.sized.resolve();
247
305
  this.stop(run);
248
306
  }
307
+ waited(run, ms) {
308
+ if (run.waiting !== null)
309
+ clearTimeout(run.waiting);
310
+ run.waiting = ms === null ? null : setTimeout(() => this.end(run, LOST), ms);
311
+ run.waiting?.unref();
312
+ }
249
313
  stop(run) {
250
314
  if (run.process !== null)
251
315
  killTree(run.process);
@@ -2,7 +2,7 @@ import { finalRefusal, retryDelay } from './api.js';
2
2
  export async function holdSecretInput(house, subject, steps, waiting) {
3
3
  for (let failures = 0; waiting();) {
4
4
  try {
5
- const held = await house.post(`/kit/secret-input/${subject}`, { steps });
5
+ const held = await house.act('secret-inputs/await', { subject, steps });
6
6
  if (held.outcome === 'collected')
7
7
  return held.content;
8
8
  if (held.release !== 'held')