@agentshouse/kit 0.2.1-cd646adba3f64 → 0.2.1-cdc381ebf021f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -48
- package/dist/acp.js +102 -108
- package/dist/afk.js +17 -0
- package/dist/agents.js +227 -68
- package/dist/api.js +19 -10
- package/dist/app-git.js +10 -26
- package/dist/bridge.js +18 -47
- package/dist/building.js +50 -0
- package/dist/calls.js +88 -0
- package/dist/chunks.js +49 -0
- package/dist/clis.js +157 -100
- package/dist/codex-main.js +58 -0
- package/dist/connect.js +108 -0
- package/dist/connection.js +120 -0
- package/dist/content.js +106 -0
- package/dist/conversations.js +815 -335
- package/dist/copies.js +439 -232
- package/dist/earlier.js +263 -0
- package/dist/end.js +23 -5
- package/dist/events.js +793 -0
- package/dist/files.js +139 -17
- package/dist/git.js +57 -21
- package/dist/grok.js +39 -0
- package/dist/home.js +122 -8
- package/dist/house-cli.js +359 -0
- package/dist/house-main.js +7 -159
- package/dist/instructions.js +7 -11
- package/dist/kit-cli.js +78 -0
- package/dist/kit-main.js +8 -22
- package/dist/login.js +132 -107
- package/dist/logout.js +15 -17
- package/dist/mcp.js +6 -2
- package/dist/owner.js +46 -0
- package/dist/placement.js +25 -0
- package/dist/relay.js +33 -2
- package/dist/resident.js +160 -58
- package/dist/scope.js +214 -0
- package/dist/scripts.js +303 -0
- package/dist/secret-input.js +1 -1
- package/dist/settings.js +188 -0
- package/dist/sign-in.js +40 -33
- package/dist/silence.js +28 -0
- package/dist/skills.js +49 -35
- package/dist/supervisor.js +163 -0
- package/dist/transcription.js +90 -47
- package/dist/translate.js +19 -16
- package/dist/update.js +119 -102
- package/dist/wire.js +32 -0
- package/dist/work.js +353 -0
- package/dist/workspace.js +103 -0
- package/package.json +25 -21
- package/dist/authority-main.js +0 -22
- package/dist/blocks.js +0 -51
- package/dist/bootstrap.json +0 -24
- package/dist/clis-main.js +0 -23
- package/dist/configure-main.js +0 -6
- package/dist/enrol-main.js +0 -4
- package/dist/stream.js +0 -72
- package/how-we-work.md +0 -23
- package/skills/ask-matt/LICENSE +0 -21
- package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
- package/skills/ask-matt/SKILL.md +0 -91
- package/skills/ask-matt/agents/openai.yaml +0 -4
- package/skills/code-review/LICENSE +0 -21
- package/skills/code-review/SKILL.md +0 -89
- package/skills/code-review/agents/openai.yaml +0 -3
- package/skills/codebase-design/DEEPENING.md +0 -37
- package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
- package/skills/codebase-design/LICENSE +0 -21
- package/skills/codebase-design/SKILL.md +0 -116
- package/skills/codebase-design/agents/openai.yaml +0 -3
- package/skills/diagnosing-bugs/LICENSE +0 -21
- package/skills/diagnosing-bugs/SKILL.md +0 -140
- package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
- package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
- package/skills/domain-modeling/ADR-FORMAT.md +0 -47
- package/skills/domain-modeling/CONTEXT-FORMAT.md +0 -60
- package/skills/domain-modeling/LICENSE +0 -21
- package/skills/domain-modeling/SKILL.md +0 -76
- package/skills/domain-modeling/agents/openai.yaml +0 -3
- package/skills/grill-me/LICENSE +0 -21
- package/skills/grill-me/SKILL.md +0 -8
- package/skills/grill-me/agents/openai.yaml +0 -4
- package/skills/grill-with-docs/LICENSE +0 -21
- package/skills/grill-with-docs/SKILL.md +0 -8
- package/skills/grill-with-docs/agents/openai.yaml +0 -4
- package/skills/grilling/LICENSE +0 -21
- package/skills/grilling/SKILL.md +0 -30
- package/skills/grilling/agents/openai.yaml +0 -3
- package/skills/handoff/LICENSE +0 -21
- package/skills/handoff/SKILL.md +0 -17
- package/skills/handoff/agents/openai.yaml +0 -4
- package/skills/implement/LICENSE +0 -21
- package/skills/implement/SKILL.md +0 -16
- package/skills/implement/agents/openai.yaml +0 -4
- package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
- package/skills/improve-codebase-architecture/LICENSE +0 -21
- package/skills/improve-codebase-architecture/SKILL.md +0 -72
- package/skills/improve-codebase-architecture/agents/openai.yaml +0 -4
- package/skills/prototype/LICENSE +0 -21
- package/skills/prototype/LOGIC.md +0 -67
- package/skills/prototype/SKILL.md +0 -28
- package/skills/prototype/UI.md +0 -112
- package/skills/prototype/agents/openai.yaml +0 -3
- package/skills/research/LICENSE +0 -21
- package/skills/research/SKILL.md +0 -14
- package/skills/research/agents/openai.yaml +0 -3
- package/skills/resolving-merge-conflicts/LICENSE +0 -21
- package/skills/resolving-merge-conflicts/SKILL.md +0 -16
- package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
- package/skills/tdd/LICENSE +0 -21
- package/skills/tdd/SKILL.md +0 -40
- package/skills/tdd/agents/openai.yaml +0 -3
- package/skills/tdd/mocking.md +0 -59
- package/skills/tdd/tests.md +0 -77
- package/skills/teach/GLOSSARY-FORMAT.md +0 -35
- package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
- package/skills/teach/LICENSE +0 -21
- package/skills/teach/MISSION-FORMAT.md +0 -31
- package/skills/teach/RESOURCES-FORMAT.md +0 -32
- package/skills/teach/SKILL.md +0 -141
- package/skills/teach/agents/openai.yaml +0 -4
- package/skills/to-questionnaire/LICENSE +0 -21
- package/skills/to-questionnaire/SKILL.md +0 -55
- package/skills/to-questionnaire/agents/openai.yaml +0 -4
- package/skills/to-spec/LICENSE +0 -21
- package/skills/to-spec/SKILL.md +0 -76
- package/skills/to-spec/agents/openai.yaml +0 -4
- package/skills/to-tickets/LICENSE +0 -21
- package/skills/to-tickets/SKILL.md +0 -106
- package/skills/to-tickets/agents/openai.yaml +0 -4
- package/skills/wait-what/LICENSE +0 -21
- package/skills/wait-what/SKILL.md +0 -8
- package/skills/wait-what/agents/openai.yaml +0 -4
- package/skills/wayfinder/LICENSE +0 -21
- package/skills/wayfinder/SKILL.md +0 -129
- package/skills/wayfinder/agents/openai.yaml +0 -4
- package/skills/writing-for-agents/LICENSE +0 -21
- package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
- package/skills/writing-for-agents/SKILL.md +0 -83
- package/skills/writing-for-agents/agents/openai.yaml +0 -3
package/dist/clis.js
CHANGED
|
@@ -1,62 +1,112 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { constants, readFileSync } from 'node:fs';
|
|
4
|
+
import { access, mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
4
5
|
import { homedir, userInfo } from 'node:os';
|
|
5
|
-
import { join } from 'node:path';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
import { delimiter, dirname, isAbsolute, join } from 'node:path';
|
|
7
|
+
import { releaseDirectory } from './placement.js';
|
|
8
|
+
import { childEnvironment } from './relay.js';
|
|
9
|
+
const HOUSE_EXECUTABLES = /^host_executable\(name = "house", paths = (\[.*\])\)$/m;
|
|
10
|
+
function houseRule(houses) {
|
|
11
|
+
return `prefix_rule(pattern = ["house"], decision = "allow")\nhost_executable(name = "house", paths = ${JSON.stringify(houses)})\n`;
|
|
12
|
+
}
|
|
13
|
+
const RAW_SUBTYPES = [
|
|
14
|
+
'background_tasks_changed',
|
|
15
|
+
'task_started',
|
|
16
|
+
'task_updated',
|
|
17
|
+
'task_notification',
|
|
18
|
+
'task_progress',
|
|
19
|
+
'session_state_changed',
|
|
20
|
+
'hook_started',
|
|
21
|
+
'hook_response',
|
|
22
|
+
];
|
|
23
|
+
const RAW_TYPES = ['result', 'user', 'assistant', 'rate_limit_event', 'prompt_suggestion'];
|
|
24
|
+
async function executable(path) {
|
|
25
|
+
try {
|
|
26
|
+
await access(path, constants.X_OK);
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async function listedHouses(file) {
|
|
34
|
+
let text;
|
|
35
|
+
try {
|
|
36
|
+
text = await readFile(file, 'utf8');
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
if (error.code === 'ENOENT')
|
|
40
|
+
return [];
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
const listed = JSON.parse(HOUSE_EXECUTABLES.exec(text)?.[1] ?? '[]');
|
|
45
|
+
return Array.isArray(listed) ? listed.filter((house) => typeof house === 'string') : [];
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export async function allowHouseInCodex(path) {
|
|
52
|
+
const candidates = [...new Set(path.split(delimiter).filter((directory) => isAbsolute(directory)))].map((directory) => join(directory, 'house'));
|
|
9
53
|
const rules = join(process.env.CODEX_HOME || join(homedir(), '.codex'), 'rules');
|
|
54
|
+
const file = join(rules, 'house.rules');
|
|
55
|
+
const houses = [];
|
|
56
|
+
for (const house of [...(await listedHouses(file)), ...candidates]) {
|
|
57
|
+
if (!houses.includes(house) && (await executable(house)))
|
|
58
|
+
houses.push(house);
|
|
59
|
+
}
|
|
10
60
|
await mkdir(rules, { recursive: true });
|
|
11
|
-
|
|
61
|
+
const temporary = `${file}.${randomUUID()}`;
|
|
62
|
+
await writeFile(temporary, houseRule(houses));
|
|
63
|
+
await rename(temporary, file);
|
|
12
64
|
}
|
|
13
65
|
async function nothing() { }
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (method !== 'session/update' || update?.asyncTaskId === undefined)
|
|
22
|
-
return null;
|
|
23
|
-
if (update.sessionUpdate === 'async_task_spawned')
|
|
24
|
-
return { id: update.asyncTaskId, running: true };
|
|
25
|
-
if (update.sessionUpdate === 'async_task_state_update' && !RUNNING_JOB.has(update.state ?? '')) {
|
|
26
|
-
return { id: update.asyncTaskId, running: false };
|
|
27
|
-
}
|
|
28
|
-
return null;
|
|
66
|
+
function claudeQuestionFields(request) {
|
|
67
|
+
const properties = request.requestedSchema?.properties ?? {};
|
|
68
|
+
return Object.keys(properties).filter((name) => properties[name]._meta?.jetbrains?.air?.customAnswer !== undefined);
|
|
69
|
+
}
|
|
70
|
+
function codexQuestionFields(request) {
|
|
71
|
+
const { _meta, requestedSchema } = request;
|
|
72
|
+
return _meta?.codex === undefined ? [] : (requestedSchema?.required ?? []);
|
|
29
73
|
}
|
|
30
|
-
function
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return 'answer';
|
|
36
|
-
return null;
|
|
74
|
+
function answered(error) {
|
|
75
|
+
return typeof error === 'object' && error !== null ? error : {};
|
|
76
|
+
}
|
|
77
|
+
function bridgeInClaudeSandbox(bridge) {
|
|
78
|
+
return process.platform === 'darwin' ? { allowUnixSockets: [bridge] } : { allowAllUnixSockets: true };
|
|
37
79
|
}
|
|
38
80
|
export const CLIS = {
|
|
39
81
|
'codex-acp': {
|
|
40
82
|
bin: 'codex',
|
|
41
|
-
minimum: '0.
|
|
83
|
+
minimum: '0.165.0',
|
|
42
84
|
install: 'curl -fsSL https://chatgpt.com/codex/install.sh | sh',
|
|
43
|
-
|
|
85
|
+
update: 'codex update',
|
|
86
|
+
adapter: {
|
|
87
|
+
package: '@agentclientprotocol/codex-acp',
|
|
88
|
+
bin: 'codex-acp',
|
|
89
|
+
executable: 'CODEX_PATH',
|
|
90
|
+
env: { CODEX_CONFIG: JSON.stringify({ features: { shell_zsh_fork: true } }) },
|
|
91
|
+
},
|
|
44
92
|
args: [],
|
|
45
93
|
fullAccess: 'agent-full-access',
|
|
46
|
-
sessionMeta: {},
|
|
94
|
+
sessionMeta: () => ({}),
|
|
95
|
+
unknownSession: (error) => {
|
|
96
|
+
const { code, data } = answered(error);
|
|
97
|
+
return code === -32603 && typeof data?.details === 'string' && data.details.startsWith('no rollout found');
|
|
98
|
+
},
|
|
47
99
|
allowHouse: allowHouseInCodex,
|
|
100
|
+
questionFields: codexQuestionFields,
|
|
48
101
|
signedIn: { command: ['login', 'status'] },
|
|
49
102
|
login: { args: ['login', '--device-auth'], code: 'show' },
|
|
50
|
-
|
|
51
|
-
turnStarted: (notice) => threadStatus(notice) === 'active',
|
|
52
|
-
turnEnded: (notice) => threadStatus(notice) === 'idle',
|
|
53
|
-
job: asyncTask,
|
|
54
|
-
phase: airPhase,
|
|
103
|
+
air: ['asyncTasks', 'nativeSubagentSessions'],
|
|
55
104
|
},
|
|
56
105
|
'claude-agent-acp': {
|
|
57
106
|
bin: 'claude',
|
|
58
|
-
minimum: '2.1.
|
|
107
|
+
minimum: '2.1.316',
|
|
59
108
|
install: 'curl -fsSL https://claude.ai/install.sh | bash',
|
|
109
|
+
update: 'claude update',
|
|
60
110
|
adapter: {
|
|
61
111
|
package: '@agentclientprotocol/claude-agent-acp',
|
|
62
112
|
bin: 'claude-agent-acp',
|
|
@@ -65,78 +115,74 @@ export const CLIS = {
|
|
|
65
115
|
},
|
|
66
116
|
args: [],
|
|
67
117
|
fullAccess: 'bypassPermissions',
|
|
68
|
-
sessionMeta:
|
|
118
|
+
sessionMeta: (bridge) => ({
|
|
119
|
+
claudeCode: {
|
|
120
|
+
options: {
|
|
121
|
+
allowedTools: ['Bash(house:*)'],
|
|
122
|
+
includeHookEvents: true,
|
|
123
|
+
settings: { sandbox: { network: bridgeInClaudeSandbox(bridge) } },
|
|
124
|
+
},
|
|
125
|
+
emitRawSDKMessages: [...RAW_SUBTYPES.map((subtype) => ({ type: 'system', subtype })), ...RAW_TYPES.map((type) => ({ type }))],
|
|
126
|
+
},
|
|
127
|
+
}),
|
|
128
|
+
unknownSession: (error) => answered(error).code === -32002,
|
|
69
129
|
allowHouse: nothing,
|
|
130
|
+
questionFields: claudeQuestionFields,
|
|
70
131
|
signedIn: { command: ['auth', 'status'] },
|
|
71
132
|
login: { args: ['auth', 'login', '--claudeai'], code: 'collect', rejected: 'Invalid code' },
|
|
72
|
-
|
|
73
|
-
turnStarted: () => false,
|
|
74
|
-
turnEnded: ({ method, params }) => method === 'session/update' &&
|
|
75
|
-
params?.update?.sessionUpdate === 'usage_update' &&
|
|
76
|
-
params.update._meta?.['_claude/origin'] !== undefined,
|
|
77
|
-
job: asyncTask,
|
|
78
|
-
phase: () => null,
|
|
133
|
+
air: ['asyncTasks', 'sessionFailure'],
|
|
79
134
|
},
|
|
80
135
|
'grok-build': {
|
|
81
136
|
bin: 'grok',
|
|
82
137
|
minimum: '1.0.46',
|
|
83
138
|
install: 'curl -fsSL https://x.ai/cli/install.sh | bash',
|
|
139
|
+
update: 'grok update',
|
|
84
140
|
adapter: null,
|
|
85
|
-
args: ['agent', '--always-approve', '--no-leader', 'stdio'],
|
|
141
|
+
args: ['--sandbox', 'off', 'agent', '--always-approve', '--no-leader', 'stdio'],
|
|
86
142
|
fullAccess: null,
|
|
87
|
-
sessionMeta: {},
|
|
143
|
+
sessionMeta: () => ({}),
|
|
144
|
+
unknownSession: (error) => answered(error).data?.code === 'FS_NOT_FOUND',
|
|
88
145
|
allowHouse: nothing,
|
|
146
|
+
questionFields: () => [],
|
|
89
147
|
signedIn: { initializeMeta: 'defaultAuthMethodId' },
|
|
90
148
|
login: { args: ['login', '--device-auth'], code: 'show' },
|
|
91
|
-
|
|
92
|
-
turnStarted: ({ method, params }) => method === '_x.ai/queue/changed' && params?.runningPromptId !== undefined,
|
|
93
|
-
turnEnded: ({ method, params }) => method === '_x.ai/session_notification' && params?.update?.sessionUpdate === 'turn_completed',
|
|
94
|
-
job: ({ method, params }) => {
|
|
95
|
-
if (method === '_x.ai/task_backgrounded')
|
|
96
|
-
return { id: params.update.task_id, running: true };
|
|
97
|
-
if (method === '_x.ai/task_completed')
|
|
98
|
-
return { id: params.update.task_snapshot.task_id, running: false };
|
|
99
|
-
return null;
|
|
100
|
-
},
|
|
101
|
-
phase: () => null,
|
|
149
|
+
air: ['asyncTasks'],
|
|
102
150
|
},
|
|
103
151
|
};
|
|
104
152
|
const KIT_PACKAGE = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
|
|
105
|
-
export const
|
|
106
|
-
const PROXY = /^https?_proxy$/i;
|
|
153
|
+
export const KIT_RELEASE = KIT_PACKAGE.version;
|
|
107
154
|
const VERSION = /\d+\.\d+\.\d+(?:-[0-9A-Za-z.]+)?/;
|
|
108
|
-
// An install still running after fifteen minutes is ended
|
|
155
|
+
// An install or update still running after fifteen minutes is ended as failed, so a hung download can neither hold its CLI's report nor keep the Kit busy.
|
|
109
156
|
const INSTALL_MS = 15 * 60_000;
|
|
110
157
|
// A login shell or a version read is ended after half a minute, so a profile that waits for input cannot stall the report.
|
|
111
158
|
const READ_MS = 30_000;
|
|
112
159
|
const CAUSE_CHARACTERS = 4000;
|
|
113
|
-
function prefix(kind) {
|
|
114
|
-
return join(kitHome(), 'agents', kind);
|
|
115
|
-
}
|
|
116
160
|
export function adapterCommand(kind) {
|
|
117
|
-
return join(
|
|
118
|
-
}
|
|
119
|
-
async function installedAdapter(kind) {
|
|
120
|
-
try {
|
|
121
|
-
const manifest = join(prefix(kind), 'node_modules', CLIS[kind].adapter.package, 'package.json');
|
|
122
|
-
return JSON.parse(await readFile(manifest, 'utf8')).version;
|
|
123
|
-
}
|
|
124
|
-
catch {
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
export function withoutProxy() {
|
|
129
|
-
return Object.fromEntries(Object.entries(process.env).filter(([name]) => !PROXY.test(name)));
|
|
161
|
+
return join(releaseDirectory(KIT_RELEASE), 'adapters', kind, 'node_modules', '.bin', CLIS[kind].adapter.bin);
|
|
130
162
|
}
|
|
131
163
|
export function run(command, args, timeoutMs, detached = false, signal) {
|
|
132
164
|
return new Promise((resolve) => {
|
|
133
165
|
const child = spawn(command, args, {
|
|
134
166
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
135
167
|
timeout: timeoutMs,
|
|
136
|
-
env:
|
|
168
|
+
env: childEnvironment(),
|
|
137
169
|
detached,
|
|
138
|
-
signal,
|
|
170
|
+
signal: detached ? undefined : signal,
|
|
139
171
|
});
|
|
172
|
+
const stop = () => {
|
|
173
|
+
try {
|
|
174
|
+
process.kill(-child.pid, 'SIGKILL');
|
|
175
|
+
}
|
|
176
|
+
catch { }
|
|
177
|
+
};
|
|
178
|
+
if (detached && signal?.aborted)
|
|
179
|
+
stop();
|
|
180
|
+
if (detached)
|
|
181
|
+
signal?.addEventListener('abort', stop, { once: true });
|
|
182
|
+
const ended = (ran) => {
|
|
183
|
+
signal?.removeEventListener('abort', stop);
|
|
184
|
+
resolve(ran);
|
|
185
|
+
};
|
|
140
186
|
let stdout = '';
|
|
141
187
|
let output = '';
|
|
142
188
|
child.stdout.setEncoding('utf8').on('data', (chunk) => {
|
|
@@ -146,26 +192,25 @@ export function run(command, args, timeoutMs, detached = false, signal) {
|
|
|
146
192
|
child.stderr.setEncoding('utf8').on('data', (chunk) => {
|
|
147
193
|
output += chunk;
|
|
148
194
|
});
|
|
149
|
-
child.on('error', (error) =>
|
|
150
|
-
child.on('close', (status) =>
|
|
195
|
+
child.on('error', (error) => ended({ status: null, stdout, output: error.message }));
|
|
196
|
+
child.on('close', (status) => ended({ status, stdout, output }));
|
|
151
197
|
});
|
|
152
198
|
}
|
|
153
199
|
export function failureOf(ran, program) {
|
|
154
200
|
return ran.output.trim().slice(-CAUSE_CHARACTERS) || `${program} exited with ${ran.status ?? 'a signal'}`;
|
|
155
201
|
}
|
|
156
|
-
export
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
return ran.status === 0 ? null : failureOf(ran,
|
|
202
|
+
export function adapterArchive(kind) {
|
|
203
|
+
return `${kind}-${KIT_PACKAGE.peerDependencies[CLIS[kind].adapter.package]}.tar.gz`;
|
|
204
|
+
}
|
|
205
|
+
async function bounded(program, work, signal) {
|
|
206
|
+
const deadline = AbortSignal.timeout(INSTALL_MS);
|
|
207
|
+
const ran = await work(signal === undefined ? deadline : AbortSignal.any([signal, deadline]));
|
|
208
|
+
if (deadline.aborted && signal?.aborted !== true)
|
|
209
|
+
return `${program} did not end within ${INSTALL_MS / 1000} seconds`;
|
|
210
|
+
return ran.status === 0 ? null : failureOf(ran, program);
|
|
165
211
|
}
|
|
166
|
-
export
|
|
167
|
-
|
|
168
|
-
return ran.status === 0 ? null : failureOf(ran, 'the install');
|
|
212
|
+
export function installCli(kind, signal) {
|
|
213
|
+
return bounded('the install', (within) => run('bash', ['-o', 'pipefail', '-c', CLIS[kind].install], INSTALL_MS, true, within), signal);
|
|
169
214
|
}
|
|
170
215
|
function loginShell() {
|
|
171
216
|
return process.env.SHELL || userInfo().shell;
|
|
@@ -173,20 +218,32 @@ function loginShell() {
|
|
|
173
218
|
const FOUND = 'house-kit-cli ';
|
|
174
219
|
const LOOKUP = `found=$(command -v "$1") || exit 0; case $found in /*) ;; *) found=$(pwd -P)/$found ;; esac; printf "${FOUND}%s\\n" "$found"`;
|
|
175
220
|
const USER_BIN = 'case ":$PATH:" in *":$HOME/.local/bin:"*) ;; *) PATH="$PATH:$HOME/.local/bin" ;; esac; export PATH';
|
|
176
|
-
function inLoginShell(script, argument = '') {
|
|
177
|
-
return run(loginShell(), ['-l', '-i', '-c', `exec /bin/sh -c '${USER_BIN}; ${script}' sh ${argument}`],
|
|
221
|
+
function inLoginShell(script, argument = '', signal, timeoutMs = READ_MS) {
|
|
222
|
+
return run(loginShell(), ['-l', '-i', '-c', `exec /bin/sh -c '${USER_BIN}; ${script}' sh ${argument}`], timeoutMs, true, signal);
|
|
178
223
|
}
|
|
179
|
-
export async function
|
|
180
|
-
const ran = await inLoginShell(LOOKUP,
|
|
224
|
+
export async function onLoginPath(program, signal) {
|
|
225
|
+
const ran = await inLoginShell(LOOKUP, program, signal);
|
|
181
226
|
return ran.stdout.split('\n').findLast((line) => line.startsWith(FOUND))?.slice(FOUND.length) ?? null;
|
|
182
227
|
}
|
|
228
|
+
export function locate(kind, signal) {
|
|
229
|
+
return onLoginPath(CLIS[kind].bin, signal);
|
|
230
|
+
}
|
|
231
|
+
export function updateCli(kind) {
|
|
232
|
+
return bounded('the update', (within) => inLoginShell(CLIS[kind].update, '', within, INSTALL_MS));
|
|
233
|
+
}
|
|
183
234
|
export async function loginPath() {
|
|
184
235
|
const marker = 'house-kit-path ';
|
|
185
236
|
const ran = await inLoginShell(`printf "${marker}%s\\n" "$PATH"`);
|
|
186
237
|
return ran.stdout.split('\n').findLast((line) => line.startsWith(marker)).slice(marker.length);
|
|
187
238
|
}
|
|
188
|
-
export async function
|
|
189
|
-
|
|
239
|
+
export async function writeCodexRule() {
|
|
240
|
+
const shell = await loginPath().catch(() => '');
|
|
241
|
+
await allowHouseInCodex([dirname(process.execPath), process.env.PATH ?? '', shell].join(delimiter)).catch((error) => {
|
|
242
|
+
throw new Error(`Codex's rule for house could not be written: ${error.message}`);
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
export async function versionOf(path, signal) {
|
|
246
|
+
return VERSION.exec((await run(path, ['--version'], READ_MS, false, signal)).stdout)?.[0] ?? null;
|
|
190
247
|
}
|
|
191
248
|
function numbers(version) {
|
|
192
249
|
return version.split(/[.-]/, 3).map(Number);
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawn } from 'node:child_process';
|
|
3
|
+
import { connect } from 'node:net';
|
|
4
|
+
const KIT_ANSWER = '"house-kit-';
|
|
5
|
+
const SYNC = 'house-kit/sync';
|
|
6
|
+
function lines(each) {
|
|
7
|
+
let buffer = '';
|
|
8
|
+
return (chunk) => {
|
|
9
|
+
buffer += chunk;
|
|
10
|
+
for (let index = buffer.indexOf('\n'); index >= 0; index = buffer.indexOf('\n')) {
|
|
11
|
+
each(buffer.slice(0, index + 1));
|
|
12
|
+
buffer = buffer.slice(index + 1);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function answersKit(line) {
|
|
17
|
+
if (!line.includes(KIT_ANSWER))
|
|
18
|
+
return false;
|
|
19
|
+
try {
|
|
20
|
+
const message = JSON.parse(line);
|
|
21
|
+
return message.method === undefined && typeof message.id === 'string' && message.id.startsWith(KIT_ANSWER.slice(1));
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const args = process.argv.slice(2);
|
|
28
|
+
const serving = args[0] === 'app-server';
|
|
29
|
+
if (process.env.HOUSE_KIT_SANDBOX === '1' && serving)
|
|
30
|
+
args.push('--disable', 'memories');
|
|
31
|
+
const codex = spawn(process.env.HOUSE_KIT_CODEX, args, { stdio: ['pipe', 'pipe', 'inherit'] });
|
|
32
|
+
const stream = serving ? connect(process.env.HOUSE_KIT_CODEX_STREAM) : null;
|
|
33
|
+
stream?.on('error', () => undefined);
|
|
34
|
+
process.stdin.setEncoding('utf8').on('data', lines((line) => codex.stdin.write(line)));
|
|
35
|
+
process.stdin.on('end', () => codex.stdin.end());
|
|
36
|
+
stream?.setEncoding('utf8').on('data', lines((line) => {
|
|
37
|
+
const message = JSON.parse(line);
|
|
38
|
+
if (message.method === SYNC)
|
|
39
|
+
stream.write(`${JSON.stringify({ jsonrpc: '2.0', id: message.id, result: {} })}\n`);
|
|
40
|
+
else
|
|
41
|
+
codex.stdin.write(line);
|
|
42
|
+
}));
|
|
43
|
+
codex.stdout.setEncoding('utf8').on('data', lines((line) => {
|
|
44
|
+
stream?.write(line);
|
|
45
|
+
if (answersKit(line) || process.stdout.write(line))
|
|
46
|
+
return;
|
|
47
|
+
codex.stdout.pause();
|
|
48
|
+
process.stdout.once('drain', () => codex.stdout.resume());
|
|
49
|
+
}));
|
|
50
|
+
codex.stdin.on('error', () => undefined);
|
|
51
|
+
for (const signal of ['SIGTERM', 'SIGINT', 'SIGHUP'])
|
|
52
|
+
process.on(signal, () => codex.kill(signal));
|
|
53
|
+
codex.on('exit', (code, signal) => {
|
|
54
|
+
if (signal === null)
|
|
55
|
+
process.exit(code ?? 1);
|
|
56
|
+
process.removeAllListeners(signal);
|
|
57
|
+
process.kill(process.pid, signal);
|
|
58
|
+
});
|
package/dist/connect.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, renameSync, rmSync } from 'node:fs';
|
|
2
|
+
import { writeFile } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { parseArgs } from 'node:util';
|
|
5
|
+
import { HouseRefusal, kitAct } from './api.js';
|
|
6
|
+
import { KIT_RELEASE } from './clis.js';
|
|
7
|
+
import { takeOverEarlier } from './earlier.js';
|
|
8
|
+
import { kitHome, readEnrolment, storedConfiguration, writeConfiguration } from './home.js';
|
|
9
|
+
import { DEFAULT_HOUSE, enrol, placeConnection } from './login.js';
|
|
10
|
+
import { runningRelease } from './owner.js';
|
|
11
|
+
import { quoted } from './shell.js';
|
|
12
|
+
import { defineSupervisor, launcher, supervise, systemd } from './supervisor.js';
|
|
13
|
+
const USAGE = `usage: kit connect [--house <origin>] [--code <code>]
|
|
14
|
+
--house <origin> the Building this account's Kit serves, ${DEFAULT_HOUSE} unless one is stored
|
|
15
|
+
--code <code> connect this computer with a login code when Kit holds no credential House accepts
|
|
16
|
+
`;
|
|
17
|
+
const HELD = 'held-enrolment';
|
|
18
|
+
const RECORDS = ['credential.json', 'disconnected.json'];
|
|
19
|
+
const ENDS = ['SIGHUP', 'SIGINT', 'SIGTERM'];
|
|
20
|
+
function unanswered(error) {
|
|
21
|
+
return error instanceof HouseRefusal ? error.status >= 500 : error instanceof TypeError && error.message === 'fetch failed';
|
|
22
|
+
}
|
|
23
|
+
async function standing(house, credential) {
|
|
24
|
+
try {
|
|
25
|
+
await kitAct(house, 'agents/desired/list', {}, credential);
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (!unanswered(error))
|
|
30
|
+
return false;
|
|
31
|
+
process.stderr.write(`kit: House did not answer, so the stored credential stands: ${error.message}\n`);
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function settleHeld() {
|
|
36
|
+
const held = join(kitHome(), HELD);
|
|
37
|
+
if (!existsSync(held))
|
|
38
|
+
return;
|
|
39
|
+
if (!existsSync(join(kitHome(), RECORDS[0]))) {
|
|
40
|
+
for (const record of RECORDS)
|
|
41
|
+
if (existsSync(join(held, record)))
|
|
42
|
+
renameSync(join(held, record), join(kitHome(), record));
|
|
43
|
+
}
|
|
44
|
+
rmSync(held, { recursive: true, force: true });
|
|
45
|
+
}
|
|
46
|
+
async function heldWhile(work) {
|
|
47
|
+
const held = join(kitHome(), HELD);
|
|
48
|
+
mkdirSync(held, { mode: 0o700 });
|
|
49
|
+
const ended = () => {
|
|
50
|
+
settleHeld();
|
|
51
|
+
process.exit(130);
|
|
52
|
+
};
|
|
53
|
+
for (const signal of ENDS)
|
|
54
|
+
process.once(signal, ended);
|
|
55
|
+
try {
|
|
56
|
+
for (const record of RECORDS)
|
|
57
|
+
if (existsSync(join(kitHome(), record)))
|
|
58
|
+
renameSync(join(kitHome(), record), join(held, record));
|
|
59
|
+
await work();
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
for (const signal of ENDS)
|
|
63
|
+
process.off(signal, ended);
|
|
64
|
+
settleHeld();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export async function connect(argv) {
|
|
68
|
+
const { values } = parseArgs({
|
|
69
|
+
args: argv,
|
|
70
|
+
options: { house: { type: 'string' }, code: { type: 'string' }, help: { type: 'boolean', default: false } },
|
|
71
|
+
});
|
|
72
|
+
if (values.help) {
|
|
73
|
+
process.stdout.write(USAGE);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
await takeOverEarlier();
|
|
77
|
+
settleHeld();
|
|
78
|
+
const stored = await readEnrolment();
|
|
79
|
+
const house = values.house ?? stored?.house ?? DEFAULT_HOUSE;
|
|
80
|
+
if (stored !== null && stored.house !== house) {
|
|
81
|
+
throw new Error(`this account's Kit serves ${stored.house}; kit logout disconnects it from there, then this command connects it to ${house}`);
|
|
82
|
+
}
|
|
83
|
+
const install = process.env.HOUSE_KIT_INSTALL_COMMAND;
|
|
84
|
+
if (install !== undefined && install !== '')
|
|
85
|
+
await writeFile(join(kitHome(), 'install-command'), `${install}\n`, { mode: 0o600 });
|
|
86
|
+
if (stored === null || !(await standing(stored.house, stored.credential))) {
|
|
87
|
+
if (values.code === undefined)
|
|
88
|
+
await enrol({ house, 'no-private': false });
|
|
89
|
+
else
|
|
90
|
+
await heldWhile(() => enrol({ house, code: values.code, 'no-private': false }));
|
|
91
|
+
if ((await readEnrolment()) === null)
|
|
92
|
+
throw new Error('kit login connected no Environment');
|
|
93
|
+
}
|
|
94
|
+
const settings = await storedConfiguration();
|
|
95
|
+
if (settings.skills === undefined || settings.workspace === undefined) {
|
|
96
|
+
await writeConfiguration({ skills: true, workspace: kitHome(), ...settings });
|
|
97
|
+
}
|
|
98
|
+
await placeConnection(house);
|
|
99
|
+
const { environment } = (await readEnrolment());
|
|
100
|
+
if (process.platform !== 'darwin' && !systemd()) {
|
|
101
|
+
process.stdout.write(`Kit ${KIT_RELEASE} is installed for Environment ${environment}, but no systemd runs on this host to start it; run ${quoted(launcher())} resident under your own service manager, restarting it whenever it ends with a failure.\n`);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const changed = await defineSupervisor();
|
|
105
|
+
const running = await runningRelease();
|
|
106
|
+
await supervise(changed, running !== null && running !== KIT_RELEASE);
|
|
107
|
+
process.stdout.write(`Kit connected for Environment ${environment}.\n`);
|
|
108
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { KIT_CONNECTION_PATH, KIT_CONNECTION_PROTOCOL_PREFIX, KIT_WIRE, } from '@agentshouse/kit-wire';
|
|
2
|
+
import { subscribe } from 'node:diagnostics_channel';
|
|
3
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
4
|
+
import { HouseRefusal, retryDelay } from './api.js';
|
|
5
|
+
import { end, REPLACED, signInAgain } from './end.js';
|
|
6
|
+
import { readEnrolment } from './home.js';
|
|
7
|
+
import { relayed } from './relay.js';
|
|
8
|
+
import { Silence } from './silence.js';
|
|
9
|
+
import { endUnserved, unserved } from './wire.js';
|
|
10
|
+
const SUBPROTOCOL = `${KIT_CONNECTION_PROTOCOL_PREFIX}${KIT_WIRE}`;
|
|
11
|
+
const REFUSAL_HEADER = 'x-house-refusal';
|
|
12
|
+
// House beats each Kit connection every thirty seconds, so two silent beats mean this Kit process cannot hear House.
|
|
13
|
+
export const CONNECTION_SILENCE_MS = 60_000;
|
|
14
|
+
// A stopping Kit waits up to two seconds for House to answer its close, so what it sent last reaches House and a House that does not answer holds no stop.
|
|
15
|
+
const CLOSE_ANSWER_MS = 2_000;
|
|
16
|
+
function connectionUrl(house) {
|
|
17
|
+
const url = new URL(KIT_CONNECTION_PATH, house);
|
|
18
|
+
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
19
|
+
return url;
|
|
20
|
+
}
|
|
21
|
+
function header(headers, name) {
|
|
22
|
+
for (let index = 0; index < headers.length; index += 2) {
|
|
23
|
+
if (headers[index].toString('latin1').toLowerCase() === name)
|
|
24
|
+
return headers[index + 1].toString('latin1');
|
|
25
|
+
}
|
|
26
|
+
return '';
|
|
27
|
+
}
|
|
28
|
+
let refused = null;
|
|
29
|
+
subscribe('undici:request:headers', (message) => {
|
|
30
|
+
const { request, response } = message;
|
|
31
|
+
if (!request.path.endsWith(KIT_CONNECTION_PATH))
|
|
32
|
+
return;
|
|
33
|
+
refused = new HouseRefusal(KIT_CONNECTION_PATH, response.statusCode, header(response.headers, REFUSAL_HEADER) || response.statusText);
|
|
34
|
+
});
|
|
35
|
+
export function holdConnection(handlers) {
|
|
36
|
+
let connection = null;
|
|
37
|
+
let socket = null;
|
|
38
|
+
let failures = 0;
|
|
39
|
+
let stopping = false;
|
|
40
|
+
const connect = async () => {
|
|
41
|
+
const { house, credential } = (await readEnrolment());
|
|
42
|
+
if (socket !== null || unserved() || stopping)
|
|
43
|
+
return;
|
|
44
|
+
refused = null;
|
|
45
|
+
const opening = new WebSocket(relayed(connectionUrl(house)), {
|
|
46
|
+
protocols: [SUBPROTOCOL],
|
|
47
|
+
headers: { authorization: `Bearer ${credential}` },
|
|
48
|
+
});
|
|
49
|
+
socket = opening;
|
|
50
|
+
let opened = false;
|
|
51
|
+
let silence = null;
|
|
52
|
+
const closed = (event) => {
|
|
53
|
+
silence?.stop();
|
|
54
|
+
silence = null;
|
|
55
|
+
if (socket === opening)
|
|
56
|
+
socket = null;
|
|
57
|
+
if (connection === opening)
|
|
58
|
+
connection = null;
|
|
59
|
+
handlers.closed();
|
|
60
|
+
process.stderr.write(`kit: connection closed ${event.code} ${event.reason}\n`);
|
|
61
|
+
if (stopping)
|
|
62
|
+
return;
|
|
63
|
+
if (event.reason === 'replaced')
|
|
64
|
+
end(REPLACED);
|
|
65
|
+
if (event.reason === 'credential_rejected' || refused?.status === 401) {
|
|
66
|
+
void signInAgain(credential);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (refused !== null)
|
|
70
|
+
endUnserved(refused.text);
|
|
71
|
+
if (!opened && refused !== null)
|
|
72
|
+
process.stderr.write(`kit: ${refused.message}\n`);
|
|
73
|
+
// A closed connection reopens in one to three seconds, spread so Kit processes one restart closed return apart; an opening House refused or left unanswered backs off on the jittered curve, retryable or not, since a resident outlives a House deploy.
|
|
74
|
+
setTimeout(() => void connect(), opened ? 1000 + Math.random() * 2000 : retryDelay(failures++));
|
|
75
|
+
};
|
|
76
|
+
opening.onopen = () => {
|
|
77
|
+
opened = true;
|
|
78
|
+
failures = 0;
|
|
79
|
+
connection = opening;
|
|
80
|
+
silence = new Silence(CONNECTION_SILENCE_MS, () => {
|
|
81
|
+
if (connection !== opening)
|
|
82
|
+
return;
|
|
83
|
+
connection = null;
|
|
84
|
+
opening.onclose = null;
|
|
85
|
+
opening.close();
|
|
86
|
+
closed(new CloseEvent('close', { code: 1006, reason: 'heartbeat_missed' }));
|
|
87
|
+
});
|
|
88
|
+
silence.heard();
|
|
89
|
+
handlers.opened();
|
|
90
|
+
};
|
|
91
|
+
opening.onmessage = (event) => {
|
|
92
|
+
silence?.heard();
|
|
93
|
+
const arrived = JSON.parse(String(event.data));
|
|
94
|
+
if (arrived.type === 'input')
|
|
95
|
+
handlers.call(arrived);
|
|
96
|
+
else if (arrived.type !== 'beat')
|
|
97
|
+
handlers.oneWayCall(arrived);
|
|
98
|
+
};
|
|
99
|
+
opening.onerror = () => undefined;
|
|
100
|
+
opening.onclose = closed;
|
|
101
|
+
};
|
|
102
|
+
void connect();
|
|
103
|
+
return {
|
|
104
|
+
send(oneWayCall) {
|
|
105
|
+
if (connection === null || connection.readyState !== WebSocket.OPEN)
|
|
106
|
+
return false;
|
|
107
|
+
connection.send(JSON.stringify(oneWayCall));
|
|
108
|
+
return true;
|
|
109
|
+
},
|
|
110
|
+
close() {
|
|
111
|
+
stopping = true;
|
|
112
|
+
const open = socket;
|
|
113
|
+
if (open === null)
|
|
114
|
+
return Promise.resolve();
|
|
115
|
+
const answered = new Promise((resolve) => open.addEventListener('close', () => resolve(), { once: true }));
|
|
116
|
+
open.close();
|
|
117
|
+
return Promise.race([answered, delay(CLOSE_ANSWER_MS)]);
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|