@agentshouse/kit 0.2.1-cd9cbd8b6428c → 0.2.1-cdc3937dea52e
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 +46 -49
- package/dist/acp.js +101 -108
- package/dist/afk.js +17 -0
- package/dist/agents.js +271 -73
- package/dist/api.js +43 -6
- package/dist/app-git.js +48 -0
- package/dist/bridge.js +42 -62
- package/dist/building.js +50 -0
- package/dist/calls.js +88 -0
- package/dist/chunks.js +49 -0
- package/dist/clis.js +179 -95
- 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 +817 -333
- package/dist/copies.js +451 -251
- package/dist/earlier.js +263 -0
- package/dist/end.js +26 -0
- package/dist/events.js +793 -0
- package/dist/files.js +139 -17
- package/dist/git-main.js +24 -29
- package/dist/git.js +57 -21
- package/dist/grok.js +39 -0
- package/dist/home.js +134 -9
- package/dist/house-cli.js +362 -0
- package/dist/house-main.js +7 -159
- package/dist/instructions.js +7 -11
- package/dist/keys.js +238 -0
- package/dist/kit-cli.js +78 -0
- package/dist/kit-main.js +8 -19
- package/dist/login.js +133 -104
- package/dist/logout.js +15 -14
- package/dist/mcp.js +6 -2
- package/dist/owner.js +46 -0
- package/dist/placement.js +25 -0
- package/dist/refusals.js +12 -0
- package/dist/relay.js +33 -2
- package/dist/resident.js +161 -61
- package/dist/scope.js +214 -0
- package/dist/scripts.js +303 -0
- package/dist/secret-input.js +5 -2
- package/dist/settings.js +188 -0
- package/dist/shell.js +3 -0
- package/dist/sign-in.js +85 -32
- package/dist/silence.js +28 -0
- package/dist/skills.js +49 -35
- package/dist/supervisor.js +163 -0
- package/dist/transcription.js +131 -40
- package/dist/translate.js +19 -16
- package/dist/update.js +119 -100
- package/dist/wire.js +32 -0
- package/dist/work.js +353 -0
- package/dist/workspace.js +103 -0
- package/package.json +26 -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 -47
- 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,125 +1,198 @@
|
|
|
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 { userInfo } from 'node:os';
|
|
5
|
-
import { join } from 'node:path';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
6
|
+
import { delimiter, dirname, isAbsolute, join } from 'node:path';
|
|
7
|
+
import { CLAUDE_KEY, CODEX_KEY, GROK_KEY, codexHome } from './keys.js';
|
|
8
|
+
import { releaseDirectory } from './placement.js';
|
|
9
|
+
import { childEnvironment } from './relay.js';
|
|
10
|
+
const HOUSE_EXECUTABLES = /^host_executable\(name = "house", paths = (\[.*\])\)$/m;
|
|
11
|
+
function houseRule(houses) {
|
|
12
|
+
return `prefix_rule(pattern = ["house"], decision = "allow")\nhost_executable(name = "house", paths = ${JSON.stringify(houses)})\n`;
|
|
13
|
+
}
|
|
14
|
+
const RAW_SUBTYPES = [
|
|
15
|
+
'background_tasks_changed',
|
|
16
|
+
'task_started',
|
|
17
|
+
'task_updated',
|
|
18
|
+
'task_notification',
|
|
19
|
+
'task_progress',
|
|
20
|
+
'session_state_changed',
|
|
21
|
+
'hook_started',
|
|
22
|
+
'hook_response',
|
|
23
|
+
];
|
|
24
|
+
const RAW_TYPES = ['result', 'user', 'assistant', 'rate_limit_event', 'prompt_suggestion'];
|
|
25
|
+
async function executable(path) {
|
|
26
|
+
try {
|
|
27
|
+
await access(path, constants.X_OK);
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async function listedHouses(file) {
|
|
35
|
+
let text;
|
|
36
|
+
try {
|
|
37
|
+
text = await readFile(file, 'utf8');
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (error.code === 'ENOENT')
|
|
41
|
+
return [];
|
|
42
|
+
throw error;
|
|
21
43
|
}
|
|
22
|
-
|
|
44
|
+
try {
|
|
45
|
+
const listed = JSON.parse(HOUSE_EXECUTABLES.exec(text)?.[1] ?? '[]');
|
|
46
|
+
return Array.isArray(listed) ? listed.filter((house) => typeof house === 'string') : [];
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
export async function allowHouseInCodex(path) {
|
|
53
|
+
const candidates = [...new Set(path.split(delimiter).filter((directory) => isAbsolute(directory)))].map((directory) => join(directory, 'house'));
|
|
54
|
+
const rules = join(codexHome(), 'rules');
|
|
55
|
+
const file = join(rules, 'house.rules');
|
|
56
|
+
const houses = [];
|
|
57
|
+
for (const house of [...(await listedHouses(file)), ...candidates]) {
|
|
58
|
+
if (!houses.includes(house) && (await executable(house)))
|
|
59
|
+
houses.push(house);
|
|
60
|
+
}
|
|
61
|
+
await mkdir(rules, { recursive: true });
|
|
62
|
+
const temporary = `${file}.${randomUUID()}`;
|
|
63
|
+
await writeFile(temporary, houseRule(houses));
|
|
64
|
+
await rename(temporary, file);
|
|
65
|
+
}
|
|
66
|
+
async function nothing() { }
|
|
67
|
+
function claudeQuestionFields(request) {
|
|
68
|
+
const properties = request.requestedSchema?.properties ?? {};
|
|
69
|
+
return Object.keys(properties).filter((name) => properties[name]._meta?.jetbrains?.air?.customAnswer !== undefined);
|
|
70
|
+
}
|
|
71
|
+
function codexQuestionFields(request) {
|
|
72
|
+
const { _meta, requestedSchema } = request;
|
|
73
|
+
return _meta?.codex === undefined ? [] : (requestedSchema?.required ?? []);
|
|
23
74
|
}
|
|
24
|
-
function
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return 'answer';
|
|
30
|
-
return null;
|
|
75
|
+
function answered(error) {
|
|
76
|
+
return typeof error === 'object' && error !== null ? error : {};
|
|
77
|
+
}
|
|
78
|
+
function bridgeInClaudeSandbox(bridge) {
|
|
79
|
+
return process.platform === 'darwin' ? { allowUnixSockets: [bridge] } : { allowAllUnixSockets: true };
|
|
31
80
|
}
|
|
32
81
|
export const CLIS = {
|
|
33
82
|
'codex-acp': {
|
|
83
|
+
name: 'Codex',
|
|
34
84
|
bin: 'codex',
|
|
35
|
-
minimum: '0.
|
|
85
|
+
minimum: '0.165.0',
|
|
36
86
|
install: 'curl -fsSL https://chatgpt.com/codex/install.sh | sh',
|
|
37
|
-
|
|
87
|
+
update: 'codex update',
|
|
88
|
+
adapter: {
|
|
89
|
+
package: '@agentclientprotocol/codex-acp',
|
|
90
|
+
bin: 'codex-acp',
|
|
91
|
+
executable: 'CODEX_PATH',
|
|
92
|
+
env: { CODEX_CONFIG: JSON.stringify({ features: { shell_zsh_fork: true } }) },
|
|
93
|
+
},
|
|
38
94
|
args: [],
|
|
95
|
+
fullAccess: 'agent-full-access',
|
|
96
|
+
sessionMeta: () => ({}),
|
|
97
|
+
unknownSession: (error) => {
|
|
98
|
+
const { code, data } = answered(error);
|
|
99
|
+
return code === -32603 && typeof data?.details === 'string' && data.details.startsWith('no rollout found');
|
|
100
|
+
},
|
|
101
|
+
allowHouse: allowHouseInCodex,
|
|
102
|
+
questionFields: codexQuestionFields,
|
|
39
103
|
signedIn: { command: ['login', 'status'] },
|
|
40
104
|
login: { args: ['login', '--device-auth'], code: 'show' },
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
turnEnded: (notice) => threadStatus(notice) === 'idle',
|
|
44
|
-
job: asyncTask,
|
|
45
|
-
phase: airPhase,
|
|
105
|
+
key: CODEX_KEY,
|
|
106
|
+
air: ['asyncTasks', 'nativeSubagentSessions'],
|
|
46
107
|
},
|
|
47
108
|
'claude-agent-acp': {
|
|
109
|
+
name: 'Claude Code',
|
|
48
110
|
bin: 'claude',
|
|
49
|
-
minimum: '2.1.
|
|
111
|
+
minimum: '2.1.316',
|
|
50
112
|
install: 'curl -fsSL https://claude.ai/install.sh | bash',
|
|
113
|
+
update: 'claude update',
|
|
51
114
|
adapter: {
|
|
52
115
|
package: '@agentclientprotocol/claude-agent-acp',
|
|
53
116
|
bin: 'claude-agent-acp',
|
|
54
117
|
executable: 'CLAUDE_CODE_EXECUTABLE',
|
|
55
|
-
env: { CLAUDE_CODE_ENTRYPOINT: 'claude-agent-acp' },
|
|
118
|
+
env: { CLAUDE_CODE_ENTRYPOINT: 'claude-agent-acp', IS_SANDBOX: '1' },
|
|
56
119
|
},
|
|
57
120
|
args: [],
|
|
121
|
+
fullAccess: 'bypassPermissions',
|
|
122
|
+
sessionMeta: (bridge) => ({
|
|
123
|
+
claudeCode: {
|
|
124
|
+
options: {
|
|
125
|
+
allowedTools: ['Bash(house:*)'],
|
|
126
|
+
includeHookEvents: true,
|
|
127
|
+
settings: { sandbox: { network: bridgeInClaudeSandbox(bridge) } },
|
|
128
|
+
},
|
|
129
|
+
emitRawSDKMessages: [...RAW_SUBTYPES.map((subtype) => ({ type: 'system', subtype })), ...RAW_TYPES.map((type) => ({ type }))],
|
|
130
|
+
},
|
|
131
|
+
}),
|
|
132
|
+
unknownSession: (error) => answered(error).code === -32002,
|
|
133
|
+
allowHouse: nothing,
|
|
134
|
+
questionFields: claudeQuestionFields,
|
|
58
135
|
signedIn: { command: ['auth', 'status'] },
|
|
59
136
|
login: { args: ['auth', 'login', '--claudeai'], code: 'collect', rejected: 'Invalid code' },
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
turnEnded: ({ method, params }) => method === 'session/update' &&
|
|
63
|
-
params?.update?.sessionUpdate === 'usage_update' &&
|
|
64
|
-
params.update._meta?.['_claude/origin'] !== undefined,
|
|
65
|
-
job: asyncTask,
|
|
66
|
-
phase: () => null,
|
|
137
|
+
key: CLAUDE_KEY,
|
|
138
|
+
air: ['asyncTasks', 'sessionFailure'],
|
|
67
139
|
},
|
|
68
140
|
'grok-build': {
|
|
141
|
+
name: 'Grok Build',
|
|
69
142
|
bin: 'grok',
|
|
70
143
|
minimum: '1.0.46',
|
|
71
144
|
install: 'curl -fsSL https://x.ai/cli/install.sh | bash',
|
|
145
|
+
update: 'grok update',
|
|
72
146
|
adapter: null,
|
|
73
|
-
args: ['agent', '--no-leader', 'stdio'],
|
|
74
|
-
|
|
147
|
+
args: ['--sandbox', 'off', 'agent', '--always-approve', '--no-leader', 'stdio'],
|
|
148
|
+
fullAccess: null,
|
|
149
|
+
sessionMeta: () => ({}),
|
|
150
|
+
unknownSession: (error) => answered(error).data?.code === 'FS_NOT_FOUND',
|
|
151
|
+
allowHouse: nothing,
|
|
152
|
+
questionFields: () => [],
|
|
153
|
+
signedIn: { initializeMeta: 'defaultAuthMethodId', apiKey: 'xai.api_key' },
|
|
75
154
|
login: { args: ['login', '--device-auth'], code: 'show' },
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
turnEnded: ({ method, params }) => method === '_x.ai/session_notification' && params?.update?.sessionUpdate === 'turn_completed',
|
|
79
|
-
job: ({ method, params }) => {
|
|
80
|
-
if (method === '_x.ai/task_backgrounded')
|
|
81
|
-
return { id: params.update.task_id, running: true };
|
|
82
|
-
if (method === '_x.ai/task_completed')
|
|
83
|
-
return { id: params.update.task_snapshot.task_id, running: false };
|
|
84
|
-
return null;
|
|
85
|
-
},
|
|
86
|
-
phase: () => null,
|
|
155
|
+
key: GROK_KEY,
|
|
156
|
+
air: ['asyncTasks'],
|
|
87
157
|
},
|
|
88
158
|
};
|
|
89
159
|
const KIT_PACKAGE = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
|
|
90
|
-
export const
|
|
91
|
-
const PROXY = /^https?_proxy$/i;
|
|
160
|
+
export const KIT_RELEASE = KIT_PACKAGE.version;
|
|
92
161
|
const VERSION = /\d+\.\d+\.\d+(?:-[0-9A-Za-z.]+)?/;
|
|
162
|
+
// 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.
|
|
93
163
|
const INSTALL_MS = 15 * 60_000;
|
|
164
|
+
// A login shell or a version read is ended after half a minute, so a profile that waits for input cannot stall the report.
|
|
94
165
|
const READ_MS = 30_000;
|
|
95
166
|
const CAUSE_CHARACTERS = 4000;
|
|
96
|
-
function prefix(kind) {
|
|
97
|
-
return join(kitHome(), 'agents', kind);
|
|
98
|
-
}
|
|
99
167
|
export function adapterCommand(kind) {
|
|
100
|
-
return join(
|
|
101
|
-
}
|
|
102
|
-
async function installedAdapter(kind) {
|
|
103
|
-
try {
|
|
104
|
-
const manifest = join(prefix(kind), 'node_modules', CLIS[kind].adapter.package, 'package.json');
|
|
105
|
-
return JSON.parse(await readFile(manifest, 'utf8')).version;
|
|
106
|
-
}
|
|
107
|
-
catch {
|
|
108
|
-
return null;
|
|
109
|
-
}
|
|
168
|
+
return join(releaseDirectory(KIT_RELEASE), 'adapters', kind, 'node_modules', '.bin', CLIS[kind].adapter.bin);
|
|
110
169
|
}
|
|
111
|
-
export function
|
|
112
|
-
return
|
|
170
|
+
export async function cliEnvironment(kind) {
|
|
171
|
+
return { ...childEnvironment(), ...(await CLIS[kind].key.environment?.()) };
|
|
113
172
|
}
|
|
114
|
-
export function run(command, args, timeoutMs, detached = false, signal) {
|
|
173
|
+
export function run(command, args, timeoutMs, detached = false, signal, env = childEnvironment()) {
|
|
115
174
|
return new Promise((resolve) => {
|
|
116
175
|
const child = spawn(command, args, {
|
|
117
176
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
118
177
|
timeout: timeoutMs,
|
|
119
|
-
env
|
|
178
|
+
env,
|
|
120
179
|
detached,
|
|
121
|
-
signal,
|
|
180
|
+
signal: detached ? undefined : signal,
|
|
122
181
|
});
|
|
182
|
+
const stop = () => {
|
|
183
|
+
try {
|
|
184
|
+
process.kill(-child.pid, 'SIGKILL');
|
|
185
|
+
}
|
|
186
|
+
catch { }
|
|
187
|
+
};
|
|
188
|
+
if (detached && signal?.aborted)
|
|
189
|
+
stop();
|
|
190
|
+
if (detached)
|
|
191
|
+
signal?.addEventListener('abort', stop, { once: true });
|
|
192
|
+
const ended = (ran) => {
|
|
193
|
+
signal?.removeEventListener('abort', stop);
|
|
194
|
+
resolve(ran);
|
|
195
|
+
};
|
|
123
196
|
let stdout = '';
|
|
124
197
|
let output = '';
|
|
125
198
|
child.stdout.setEncoding('utf8').on('data', (chunk) => {
|
|
@@ -129,26 +202,25 @@ export function run(command, args, timeoutMs, detached = false, signal) {
|
|
|
129
202
|
child.stderr.setEncoding('utf8').on('data', (chunk) => {
|
|
130
203
|
output += chunk;
|
|
131
204
|
});
|
|
132
|
-
child.on('error', (error) =>
|
|
133
|
-
child.on('close', (status) =>
|
|
205
|
+
child.on('error', (error) => ended({ status: null, stdout, output: error.message }));
|
|
206
|
+
child.on('close', (status) => ended({ status, stdout, output }));
|
|
134
207
|
});
|
|
135
208
|
}
|
|
136
209
|
export function failureOf(ran, program) {
|
|
137
210
|
return ran.output.trim().slice(-CAUSE_CHARACTERS) || `${program} exited with ${ran.status ?? 'a signal'}`;
|
|
138
211
|
}
|
|
139
|
-
export
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
return ran.status === 0 ? null : failureOf(ran,
|
|
212
|
+
export function adapterArchive(kind) {
|
|
213
|
+
return `${kind}-${KIT_PACKAGE.peerDependencies[CLIS[kind].adapter.package]}.tar.gz`;
|
|
214
|
+
}
|
|
215
|
+
async function bounded(program, work, signal) {
|
|
216
|
+
const deadline = AbortSignal.timeout(INSTALL_MS);
|
|
217
|
+
const ran = await work(signal === undefined ? deadline : AbortSignal.any([signal, deadline]));
|
|
218
|
+
if (deadline.aborted && signal?.aborted !== true)
|
|
219
|
+
return `${program} did not end within ${INSTALL_MS / 1000} seconds`;
|
|
220
|
+
return ran.status === 0 ? null : failureOf(ran, program);
|
|
148
221
|
}
|
|
149
|
-
export
|
|
150
|
-
|
|
151
|
-
return ran.status === 0 ? null : failureOf(ran, 'the install');
|
|
222
|
+
export function installCli(kind, signal) {
|
|
223
|
+
return bounded('the install', (within) => run('bash', ['-o', 'pipefail', '-c', CLIS[kind].install], INSTALL_MS, true, within), signal);
|
|
152
224
|
}
|
|
153
225
|
function loginShell() {
|
|
154
226
|
return process.env.SHELL || userInfo().shell;
|
|
@@ -156,20 +228,32 @@ function loginShell() {
|
|
|
156
228
|
const FOUND = 'house-kit-cli ';
|
|
157
229
|
const LOOKUP = `found=$(command -v "$1") || exit 0; case $found in /*) ;; *) found=$(pwd -P)/$found ;; esac; printf "${FOUND}%s\\n" "$found"`;
|
|
158
230
|
const USER_BIN = 'case ":$PATH:" in *":$HOME/.local/bin:"*) ;; *) PATH="$PATH:$HOME/.local/bin" ;; esac; export PATH';
|
|
159
|
-
function inLoginShell(script, argument = '') {
|
|
160
|
-
return run(loginShell(), ['-l', '-i', '-c', `exec /bin/sh -c '${USER_BIN}; ${script}' sh ${argument}`],
|
|
231
|
+
function inLoginShell(script, argument = '', signal, timeoutMs = READ_MS) {
|
|
232
|
+
return run(loginShell(), ['-l', '-i', '-c', `exec /bin/sh -c '${USER_BIN}; ${script}' sh ${argument}`], timeoutMs, true, signal);
|
|
161
233
|
}
|
|
162
|
-
export async function
|
|
163
|
-
const ran = await inLoginShell(LOOKUP,
|
|
234
|
+
export async function onLoginPath(program, signal) {
|
|
235
|
+
const ran = await inLoginShell(LOOKUP, program, signal);
|
|
164
236
|
return ran.stdout.split('\n').findLast((line) => line.startsWith(FOUND))?.slice(FOUND.length) ?? null;
|
|
165
237
|
}
|
|
238
|
+
export function locate(kind, signal) {
|
|
239
|
+
return onLoginPath(CLIS[kind].bin, signal);
|
|
240
|
+
}
|
|
241
|
+
export function updateCli(kind) {
|
|
242
|
+
return bounded('the update', (within) => inLoginShell(CLIS[kind].update, '', within, INSTALL_MS));
|
|
243
|
+
}
|
|
166
244
|
export async function loginPath() {
|
|
167
245
|
const marker = 'house-kit-path ';
|
|
168
246
|
const ran = await inLoginShell(`printf "${marker}%s\\n" "$PATH"`);
|
|
169
247
|
return ran.stdout.split('\n').findLast((line) => line.startsWith(marker)).slice(marker.length);
|
|
170
248
|
}
|
|
171
|
-
export async function
|
|
172
|
-
|
|
249
|
+
export async function writeCodexRule() {
|
|
250
|
+
const shell = await loginPath().catch(() => '');
|
|
251
|
+
await allowHouseInCodex([dirname(process.execPath), process.env.PATH ?? '', shell].join(delimiter)).catch((error) => {
|
|
252
|
+
throw new Error(`Codex's rule for house could not be written: ${error.message}`);
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
export async function versionOf(kind, path, signal) {
|
|
256
|
+
return VERSION.exec((await run(path, ['--version'], READ_MS, false, signal, await cliEnvironment(kind))).stdout)?.[0] ?? null;
|
|
173
257
|
}
|
|
174
258
|
function numbers(version) {
|
|
175
259
|
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
|
+
}
|