@aisac/cli 0.0.0-stage → 0.4.0-beta.10
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 +202 -2
- package/dist/assets/claude/.claude-plugin/plugin.json +147 -0
- package/dist/assets/claude/hooks/context-compaction.ts +6934 -0
- package/dist/assets/claude/hooks/hooks.json +3 -0
- package/dist/assets/claude/hooks/native-host-contract-runtime.ts +18 -0
- package/dist/assets/claude/hooks/native-host-contract.test.ts +51 -0
- package/dist/assets/claude/hooks/provider-window.js +263 -0
- package/dist/assets/claude/hooks/state-fitting.d.mts +47 -0
- package/dist/assets/claude/hooks/state-fitting.mjs +324 -0
- package/dist/assets/codex-context-compaction.mjs +4786 -0
- package/dist/assets/context-selection-fitting.mjs +194 -0
- package/dist/assets/context-state-fitting.mjs +324 -0
- package/dist/assets/context-telemetry-reporter.mjs +305 -0
- package/dist/assets/jev-paid-pilot-terminal.mjs +1066 -0
- package/dist/claude-checkpoint.mjs +1338 -0
- package/dist/claude-compatibility.mjs +506 -0
- package/dist/claude-observation.mjs +1338 -0
- package/dist/cli.mjs +805 -0
- package/dist/codex-compatibility.mjs +531 -0
- package/dist/codex-diagnostics.mjs +447 -0
- package/dist/codex-engineering.mjs +165 -0
- package/dist/codex-permissions.mjs +590 -0
- package/dist/codex-protocol-fingerprint.json +131 -0
- package/dist/context-telemetry.mjs +257 -0
- package/dist/engineering-pack-public-key.json +10 -0
- package/dist/engineering-pack.mjs +845 -0
- package/dist/engineering.mjs +194 -0
- package/dist/extensions-catalog.mjs +310 -0
- package/dist/extensions-cli.mjs +486 -0
- package/dist/extensions-preferences.mjs +151 -0
- package/dist/guardrails/continuity.mjs +239 -0
- package/dist/guardrails/format.mjs +111 -0
- package/dist/guardrails/guards.mjs +397 -0
- package/dist/guardrails/run.mjs +211 -0
- package/dist/guardrails/shell-words.mjs +206 -0
- package/dist/guardrails-plugin.mjs +149 -0
- package/dist/launch.mjs +786 -0
- package/dist/mcp-servers.mjs +192 -0
- package/dist/mcp.mjs +962 -0
- package/dist/monitor.mjs +166 -0
- package/dist/package.json +5 -0
- package/dist/platform.mjs +156 -0
- package/dist/policy.mjs +218 -0
- package/dist/state.mjs +408 -0
- package/dist/telemetry-report.mjs +133 -0
- package/package.json +24 -4
package/dist/launch.mjs
ADDED
|
@@ -0,0 +1,786 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { access, chmod, lstat, mkdir, mkdtemp, open, rm, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { spawn } from 'node:child_process';
|
|
5
|
+
import { constants } from 'node:fs';
|
|
6
|
+
import { isAbsolute, join, resolve, sep } from 'node:path';
|
|
7
|
+
import { captureLockPlatformCheck, nativeVersion, resolveNative } from './platform.mjs';
|
|
8
|
+
import { claudeCompatibilityError, resolveClaudeCompatibility } from './claude-compatibility.mjs';
|
|
9
|
+
import { codexCompatibilityError, resolveCodexCompatibility } from './codex-compatibility.mjs';
|
|
10
|
+
import {
|
|
11
|
+
claudeCheckpointConfigBinding,
|
|
12
|
+
claudeObservationConfigBinding,
|
|
13
|
+
telemetryConfigBinding,
|
|
14
|
+
} from './state.mjs';
|
|
15
|
+
import { codexPolicyJson, makePolicy } from './policy.mjs';
|
|
16
|
+
import {
|
|
17
|
+
prepareEngineeringPack,
|
|
18
|
+
sweepStaleLaunches,
|
|
19
|
+
writeLaunchMarker,
|
|
20
|
+
} from './engineering-pack.mjs';
|
|
21
|
+
import { prepareGuardrails } from './guardrails-plugin.mjs';
|
|
22
|
+
import {
|
|
23
|
+
CODEX_MCP_ENV,
|
|
24
|
+
claudeDenyRules,
|
|
25
|
+
claudeMcpEntries,
|
|
26
|
+
mcpLaunchLines,
|
|
27
|
+
sessionServers,
|
|
28
|
+
} from './mcp-servers.mjs';
|
|
29
|
+
import { enabledServerIds, readPreferences } from './extensions-preferences.mjs';
|
|
30
|
+
import {
|
|
31
|
+
CODEX_ENGINEERING_ENV,
|
|
32
|
+
CODEX_ENGINEERING_SKILLS_ROOT_ENV,
|
|
33
|
+
supportsCodexEngineering,
|
|
34
|
+
} from './codex-engineering.mjs';
|
|
35
|
+
import {
|
|
36
|
+
CODEX_LAUNCH_ENVELOPE,
|
|
37
|
+
CODEX_MONITOR_CWD,
|
|
38
|
+
makeCodexLaunchEnvelope,
|
|
39
|
+
parseCodexLaunchArgs,
|
|
40
|
+
readEffectiveWritableRoots,
|
|
41
|
+
} from './codex-permissions.mjs';
|
|
42
|
+
|
|
43
|
+
const MCP_ALIAS = 'aibrain_context';
|
|
44
|
+
const PLUGIN_NAME = 'ai-brain-context-compaction';
|
|
45
|
+
const PLUGIN_INLINE_NAME = `${PLUGIN_NAME}@inline`;
|
|
46
|
+
const CLAUDE_HEADLESS_FLAGS = new Set([
|
|
47
|
+
'--print',
|
|
48
|
+
'-p',
|
|
49
|
+
'--output-format',
|
|
50
|
+
'--input-format',
|
|
51
|
+
'--output-style',
|
|
52
|
+
]);
|
|
53
|
+
const RESERVED_CLAUDE_FLAGS = new Set(['--mcp-config', '--plugin-dir', '--allowedTools']);
|
|
54
|
+
const MAX_CLAUDE_SETTINGS_BYTES = 64 * 1024;
|
|
55
|
+
const MAX_CLAUDE_SETTINGS_DEPTH = 12;
|
|
56
|
+
const MAX_CLAUDE_SETTINGS_NODES = 512;
|
|
57
|
+
const MAX_CLAUDE_SETTINGS_STRING_LENGTH = 16 * 1024;
|
|
58
|
+
const MAX_CLAUDE_PERMISSION_LIST_ITEMS = 256;
|
|
59
|
+
const MAX_CLAUDE_PERMISSION_STRING_LENGTH = 4096;
|
|
60
|
+
const CLAUDE_SETTINGS_SECTIONS = new Set(['sandbox', 'permissions']);
|
|
61
|
+
const CLAUDE_PERMISSION_LISTS = new Set(['allow', 'ask', 'deny', 'additionalDirectories']);
|
|
62
|
+
const CLAUDE_SETTINGS_FORBIDDEN_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @typedef {object} LaunchConfig
|
|
66
|
+
* @property {string} endpoint
|
|
67
|
+
* @property {string} token
|
|
68
|
+
* @property {string} packageRoot
|
|
69
|
+
* @property {string|null} claudeVersion
|
|
70
|
+
* @property {string|null} codexVersion
|
|
71
|
+
* @property {number} schemaVersion
|
|
72
|
+
* @property {import('./policy.mjs').ContextPolicy} [context]
|
|
73
|
+
* @property {{ enabled: boolean, disclosedAt?: string | null }} [telemetry]
|
|
74
|
+
* @property {{ shadowEnabled: boolean, diagnosticsEnabled: boolean, disclosedAt?: string | null }} [claudeObservation]
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
/** @param {LaunchConfig} config */
|
|
78
|
+
function assertConfig(config) {
|
|
79
|
+
if (config === null || typeof config !== 'object') throw new Error('setup_required');
|
|
80
|
+
if (typeof config.endpoint !== 'string' || typeof config.token !== 'string')
|
|
81
|
+
throw new Error('setup_config_invalid');
|
|
82
|
+
if (config.token.length === 0) throw new Error('setup_token_missing');
|
|
83
|
+
const endpoint = new URL(config.endpoint);
|
|
84
|
+
if (
|
|
85
|
+
endpoint.protocol !== 'https:' &&
|
|
86
|
+
!(
|
|
87
|
+
endpoint.protocol === 'http:' &&
|
|
88
|
+
['127.0.0.1', 'localhost', '::1', '[::1]'].includes(endpoint.hostname)
|
|
89
|
+
)
|
|
90
|
+
)
|
|
91
|
+
throw new Error('endpoint_must_use_https');
|
|
92
|
+
if (endpoint.username || endpoint.password || endpoint.hash)
|
|
93
|
+
throw new Error('endpoint_credentials_or_fragment_rejected');
|
|
94
|
+
if (endpoint.search) throw new Error('endpoint_query_rejected');
|
|
95
|
+
if (typeof config.packageRoot !== 'string' || !isAbsolute(config.packageRoot))
|
|
96
|
+
throw new Error('package_root_invalid');
|
|
97
|
+
return { ...config, endpoint: endpoint.toString() };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** @param {string} directory */
|
|
101
|
+
async function privateDirectory(directory) {
|
|
102
|
+
const target = resolve(directory);
|
|
103
|
+
const parts = target.split(sep);
|
|
104
|
+
let current = parts[0] === '' ? sep : parts[0];
|
|
105
|
+
for (const part of parts.slice(parts[0] === '' ? 1 : 0)) {
|
|
106
|
+
if (part.length === 0) continue;
|
|
107
|
+
current = current === sep ? `${sep}${part}` : join(current, part);
|
|
108
|
+
try {
|
|
109
|
+
const entry = await lstat(current);
|
|
110
|
+
if (entry.isSymbolicLink()) throw new Error('private_state_symlink_rejected');
|
|
111
|
+
if (!entry.isDirectory()) throw new Error('private_state_not_directory');
|
|
112
|
+
} catch (error) {
|
|
113
|
+
const errorCode =
|
|
114
|
+
error !== null && typeof error === 'object' && 'code' in error ? error.code : undefined;
|
|
115
|
+
if (errorCode !== 'ENOENT') throw error;
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
await mkdir(target, { recursive: true, mode: 0o700 });
|
|
120
|
+
const entry = await lstat(target);
|
|
121
|
+
if (entry.isSymbolicLink() || !entry.isDirectory()) throw new Error('private_state_invalid');
|
|
122
|
+
if (
|
|
123
|
+
entry.uid !== undefined &&
|
|
124
|
+
typeof process.getuid === 'function' &&
|
|
125
|
+
entry.uid !== process.getuid()
|
|
126
|
+
)
|
|
127
|
+
throw new Error('private_state_owner_mismatch');
|
|
128
|
+
await chmod(target, 0o700);
|
|
129
|
+
return target;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** @param {string} path @param {unknown} value */
|
|
133
|
+
async function privateJson(path, value) {
|
|
134
|
+
await writeFile(path, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600, flag: 'wx' });
|
|
135
|
+
await chmod(path, 0o600);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** @param {string} root */
|
|
139
|
+
function childEnvironment(root) {
|
|
140
|
+
/** @type {NodeJS.ProcessEnv} */
|
|
141
|
+
const environment = { ...process.env, AIBRAIN_MCP_SERVER: MCP_ALIAS };
|
|
142
|
+
delete environment.JEV_API_KEY;
|
|
143
|
+
delete environment.TYPESAFE_API_KEY;
|
|
144
|
+
delete environment.AIBRAIN_TOKEN_FILE;
|
|
145
|
+
delete environment.CODEX_CONTEXT_POLICY_JSON;
|
|
146
|
+
delete environment.CODEX_CONTEXT_COMPACT_AT;
|
|
147
|
+
delete environment[CODEX_LAUNCH_ENVELOPE];
|
|
148
|
+
delete environment[CODEX_MONITOR_CWD];
|
|
149
|
+
delete environment[CODEX_ENGINEERING_ENV];
|
|
150
|
+
delete environment[CODEX_ENGINEERING_SKILLS_ROOT_ENV];
|
|
151
|
+
delete environment[CODEX_MCP_ENV];
|
|
152
|
+
environment.AIBRAIN_STATE_DIR = resolve(root);
|
|
153
|
+
return environment;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** @param {string[]} args */
|
|
157
|
+
function assertClaudeArgs(args) {
|
|
158
|
+
for (const argument of args) {
|
|
159
|
+
if (argument === '--') break;
|
|
160
|
+
const option = argument.split('=', 1)[0];
|
|
161
|
+
if (CLAUDE_HEADLESS_FLAGS.has(option) || /^-p(?:=|[^-])/u.test(argument))
|
|
162
|
+
throw new Error('claude_headless_mode_unsupported');
|
|
163
|
+
if (RESERVED_CLAUDE_FLAGS.has(option)) throw new Error('claude_launch_flag_reserved');
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** @param {unknown} value @returns {value is Record<string, unknown>} */
|
|
168
|
+
function plainObject(value) {
|
|
169
|
+
return (
|
|
170
|
+
value !== null &&
|
|
171
|
+
typeof value === 'object' &&
|
|
172
|
+
!Array.isArray(value) &&
|
|
173
|
+
Object.getPrototypeOf(value) === Object.prototype
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** @param {unknown} value */
|
|
178
|
+
function validateClaudeSettingsValue(value, depth = 0, state = { nodes: 0 }) {
|
|
179
|
+
state.nodes += 1;
|
|
180
|
+
if (state.nodes > MAX_CLAUDE_SETTINGS_NODES || depth > MAX_CLAUDE_SETTINGS_DEPTH)
|
|
181
|
+
throw new Error('claude_settings_structure_invalid');
|
|
182
|
+
if (typeof value === 'string') {
|
|
183
|
+
if (value.length > MAX_CLAUDE_SETTINGS_STRING_LENGTH)
|
|
184
|
+
throw new Error('claude_settings_string_too_long');
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (typeof value === 'number') {
|
|
188
|
+
if (!Number.isFinite(value) || Math.abs(value) > Number.MAX_SAFE_INTEGER)
|
|
189
|
+
throw new Error('claude_settings_value_invalid');
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (value === null || typeof value === 'boolean') return;
|
|
193
|
+
if (Array.isArray(value)) {
|
|
194
|
+
if (value.length > MAX_CLAUDE_SETTINGS_NODES)
|
|
195
|
+
throw new Error('claude_settings_structure_invalid');
|
|
196
|
+
for (const item of value) validateClaudeSettingsValue(item, depth + 1, state);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (!plainObject(value)) throw new Error('claude_settings_value_invalid');
|
|
200
|
+
for (const [key, item] of Object.entries(value)) {
|
|
201
|
+
if (CLAUDE_SETTINGS_FORBIDDEN_KEYS.has(key)) throw new Error('claude_settings_prototype_key');
|
|
202
|
+
if (key.length > 256) throw new Error('claude_settings_key_too_long');
|
|
203
|
+
validateClaudeSettingsValue(item, depth + 1, state);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** @param {unknown} value */
|
|
208
|
+
function validateClaudeSettingsFragment(value) {
|
|
209
|
+
if (!plainObject(value)) throw new Error('claude_settings_root_invalid');
|
|
210
|
+
validateClaudeSettingsValue(value);
|
|
211
|
+
for (const key of Object.keys(value)) {
|
|
212
|
+
if (!CLAUDE_SETTINGS_SECTIONS.has(key)) throw new Error('claude_settings_section_unsupported');
|
|
213
|
+
const section = value[key];
|
|
214
|
+
if (!plainObject(section)) throw new Error('claude_settings_section_invalid');
|
|
215
|
+
}
|
|
216
|
+
const permissions = value.permissions;
|
|
217
|
+
if (permissions !== undefined) {
|
|
218
|
+
const permissionObject = /** @type {Record<string, unknown>} */ (permissions);
|
|
219
|
+
for (const key of CLAUDE_PERMISSION_LISTS) {
|
|
220
|
+
const list = permissionObject[key];
|
|
221
|
+
if (list === undefined) continue;
|
|
222
|
+
if (
|
|
223
|
+
!Array.isArray(list) ||
|
|
224
|
+
list.length > MAX_CLAUDE_PERMISSION_LIST_ITEMS ||
|
|
225
|
+
list.some(
|
|
226
|
+
(item) => typeof item !== 'string' || item.length > MAX_CLAUDE_PERMISSION_STRING_LENGTH,
|
|
227
|
+
)
|
|
228
|
+
)
|
|
229
|
+
throw new Error('claude_settings_permission_list_invalid');
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return /** @type {{sandbox?: Record<string, unknown>, permissions?: Record<string, unknown>}} */ (
|
|
233
|
+
value
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** @param {string} text */
|
|
238
|
+
function parseClaudeSettingsText(text) {
|
|
239
|
+
const bytes = Buffer.byteLength(text, 'utf8');
|
|
240
|
+
if (bytes > MAX_CLAUDE_SETTINGS_BYTES) throw new Error('claude_settings_too_large');
|
|
241
|
+
let value;
|
|
242
|
+
try {
|
|
243
|
+
value = JSON.parse(text);
|
|
244
|
+
} catch {
|
|
245
|
+
throw new Error('claude_settings_invalid_json');
|
|
246
|
+
}
|
|
247
|
+
return validateClaudeSettingsFragment(value);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** @param {string} path */
|
|
251
|
+
async function readClaudeSettingsFile(path) {
|
|
252
|
+
let handle;
|
|
253
|
+
try {
|
|
254
|
+
const link = await lstat(path);
|
|
255
|
+
if (link.isSymbolicLink() || !link.isFile()) throw new Error('claude_settings_file_invalid');
|
|
256
|
+
handle = await open(
|
|
257
|
+
path,
|
|
258
|
+
constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0),
|
|
259
|
+
);
|
|
260
|
+
const entry = await handle.stat();
|
|
261
|
+
if (!entry.isFile()) throw new Error('claude_settings_file_invalid');
|
|
262
|
+
if (entry.size > MAX_CLAUDE_SETTINGS_BYTES) throw new Error('claude_settings_too_large');
|
|
263
|
+
const buffer = Buffer.alloc(MAX_CLAUDE_SETTINGS_BYTES + 1);
|
|
264
|
+
let offset = 0;
|
|
265
|
+
while (offset < buffer.length) {
|
|
266
|
+
const result = await handle.read(buffer, offset, buffer.length - offset, null);
|
|
267
|
+
if (result.bytesRead === 0) break;
|
|
268
|
+
offset += result.bytesRead;
|
|
269
|
+
}
|
|
270
|
+
if (offset > MAX_CLAUDE_SETTINGS_BYTES) throw new Error('claude_settings_too_large');
|
|
271
|
+
let text;
|
|
272
|
+
try {
|
|
273
|
+
text = new TextDecoder('utf-8', { fatal: true }).decode(buffer.subarray(0, offset));
|
|
274
|
+
} catch {
|
|
275
|
+
throw new Error('claude_settings_invalid_utf8');
|
|
276
|
+
}
|
|
277
|
+
return parseClaudeSettingsText(text);
|
|
278
|
+
} catch (error) {
|
|
279
|
+
if (error instanceof Error && error.message.startsWith('claude_settings_')) throw error;
|
|
280
|
+
throw new Error('claude_settings_file_invalid');
|
|
281
|
+
} finally {
|
|
282
|
+
await handle?.close().catch(() => undefined);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** @param {string} value @param {string} cwd */
|
|
287
|
+
async function readClaudeSettings(value, cwd) {
|
|
288
|
+
const first = value.trimStart()[0];
|
|
289
|
+
const explicitPath = isAbsolute(value) || value.startsWith('./') || value.startsWith('../');
|
|
290
|
+
if (Buffer.byteLength(value, 'utf8') > MAX_CLAUDE_SETTINGS_BYTES)
|
|
291
|
+
throw new Error('claude_settings_too_large');
|
|
292
|
+
try {
|
|
293
|
+
return validateClaudeSettingsFragment(JSON.parse(value));
|
|
294
|
+
} catch (error) {
|
|
295
|
+
if (error instanceof Error && error.message.startsWith('claude_settings_')) throw error;
|
|
296
|
+
if (!explicitPath && (first === '{' || first === '[' || first === '"' || first === '-'))
|
|
297
|
+
throw new Error('claude_settings_invalid_json');
|
|
298
|
+
}
|
|
299
|
+
return readClaudeSettingsFile(resolve(cwd, value));
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* @param {string[]} args
|
|
304
|
+
* @param {string} cwd
|
|
305
|
+
* @returns {Promise<{args: string[], settings?: {sandbox?: Record<string, unknown>, permissions?: Record<string, unknown>}}>}
|
|
306
|
+
*/
|
|
307
|
+
async function parseClaudeLaunchArgs(args, cwd) {
|
|
308
|
+
const forwarded = [];
|
|
309
|
+
let settingsValue;
|
|
310
|
+
let terminated = false;
|
|
311
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
312
|
+
const argument = args[index];
|
|
313
|
+
if (terminated) {
|
|
314
|
+
forwarded.push(argument);
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
if (argument === '--') {
|
|
318
|
+
terminated = true;
|
|
319
|
+
forwarded.push(argument);
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
if (argument === '--settings' || argument.startsWith('--settings=')) {
|
|
323
|
+
if (settingsValue !== undefined) throw new Error('claude_settings_duplicate');
|
|
324
|
+
const value =
|
|
325
|
+
argument === '--settings' ? args[++index] : argument.slice('--settings='.length);
|
|
326
|
+
if (value === undefined || value === '' || value.startsWith('-'))
|
|
327
|
+
throw new Error('claude_settings_value_required');
|
|
328
|
+
settingsValue = value;
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
forwarded.push(argument);
|
|
332
|
+
}
|
|
333
|
+
return {
|
|
334
|
+
args: forwarded,
|
|
335
|
+
...(settingsValue === undefined
|
|
336
|
+
? {}
|
|
337
|
+
: { settings: await readClaudeSettings(settingsValue, cwd) }),
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** @param {string[]} args */
|
|
342
|
+
function assertCodexArgs(args) {
|
|
343
|
+
for (const argument of args) {
|
|
344
|
+
const option = argument.split('=', 1)[0];
|
|
345
|
+
if (option === '--remote' || option === '--listen' || option === 'app-server')
|
|
346
|
+
throw new Error('codex_launch_flag_reserved');
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** @param {string} packageRoot */
|
|
351
|
+
async function assertClaudeAssets(packageRoot) {
|
|
352
|
+
const pluginRoot = join(packageRoot, 'assets', 'claude');
|
|
353
|
+
const required = [
|
|
354
|
+
join(pluginRoot, '.claude-plugin', 'plugin.json'),
|
|
355
|
+
join(pluginRoot, 'hooks', 'hooks.json'),
|
|
356
|
+
join(pluginRoot, 'hooks', 'context-compaction.ts'),
|
|
357
|
+
join(pluginRoot, 'hooks', 'provider-window.js'),
|
|
358
|
+
join(pluginRoot, 'hooks', 'state-fitting.mjs'),
|
|
359
|
+
join(pluginRoot, 'hooks', 'state-fitting.d.mts'),
|
|
360
|
+
];
|
|
361
|
+
await Promise.all(required.map((path) => access(path, constants.R_OK)));
|
|
362
|
+
return pluginRoot;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/** @param {string} packageRoot */
|
|
366
|
+
async function assertTelemetryHelper(packageRoot) {
|
|
367
|
+
const helperPath = join(packageRoot, 'telemetry-report.mjs');
|
|
368
|
+
await access(helperPath, constants.R_OK);
|
|
369
|
+
return helperPath;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** @param {string} packageRoot */
|
|
373
|
+
async function assertClaudeObservationHelper(packageRoot) {
|
|
374
|
+
const helperPath = join(packageRoot, 'claude-observation.mjs');
|
|
375
|
+
await access(helperPath, constants.R_OK);
|
|
376
|
+
return helperPath;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** @param {string} packageRoot */
|
|
380
|
+
async function assertClaudeCheckpointHelper(packageRoot) {
|
|
381
|
+
const helperPath = join(packageRoot, 'claude-checkpoint.mjs');
|
|
382
|
+
await access(helperPath, constants.R_OK);
|
|
383
|
+
return helperPath;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* A fresh private directory for one launch. Directories left by launches whose
|
|
388
|
+
* processes are gone are swept first; this one records its launcher.
|
|
389
|
+
* @param {string} root @param {string} prefix
|
|
390
|
+
*/
|
|
391
|
+
async function launchDirectory(root, prefix) {
|
|
392
|
+
await sweepStaleLaunches(root);
|
|
393
|
+
const parent = await privateDirectory(join(root, 'runtime'));
|
|
394
|
+
const directory = await mkdtemp(join(parent, `${prefix}-`));
|
|
395
|
+
await chmod(directory, 0o700);
|
|
396
|
+
await writeLaunchMarker(directory, { launcherPid: process.pid, childPid: null });
|
|
397
|
+
return directory;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** @param {string} directory @param {number | undefined} childPid */
|
|
401
|
+
async function recordChild(directory, childPid) {
|
|
402
|
+
if (childPid === undefined) return;
|
|
403
|
+
await writeLaunchMarker(directory, { launcherPid: process.pid, childPid }).catch(() => undefined);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** @param {import('node:child_process').ChildProcess} child @param {string} directory */
|
|
407
|
+
function waitForChild(child, directory) {
|
|
408
|
+
return new Promise((resolvePromise) => {
|
|
409
|
+
let settled = false;
|
|
410
|
+
/** @param {NodeJS.Signals} signal */
|
|
411
|
+
const forwardSignal = (signal) => {
|
|
412
|
+
if (child.exitCode === null) child.kill(signal);
|
|
413
|
+
};
|
|
414
|
+
const signalHandlers = {
|
|
415
|
+
SIGINT: () => forwardSignal('SIGINT'),
|
|
416
|
+
SIGTERM: () => forwardSignal('SIGTERM'),
|
|
417
|
+
SIGHUP: () => forwardSignal('SIGHUP'),
|
|
418
|
+
};
|
|
419
|
+
process.once('SIGINT', signalHandlers.SIGINT);
|
|
420
|
+
process.once('SIGTERM', signalHandlers.SIGTERM);
|
|
421
|
+
process.once('SIGHUP', signalHandlers.SIGHUP);
|
|
422
|
+
/** @param {number} code */
|
|
423
|
+
const finish = (code) => {
|
|
424
|
+
if (settled) return;
|
|
425
|
+
settled = true;
|
|
426
|
+
process.removeListener('SIGINT', signalHandlers.SIGINT);
|
|
427
|
+
process.removeListener('SIGTERM', signalHandlers.SIGTERM);
|
|
428
|
+
process.removeListener('SIGHUP', signalHandlers.SIGHUP);
|
|
429
|
+
void rm(directory, { recursive: true, force: true })
|
|
430
|
+
.catch(() => undefined)
|
|
431
|
+
.finally(() => resolvePromise(code));
|
|
432
|
+
};
|
|
433
|
+
child.once('error', () => finish(1));
|
|
434
|
+
child.once('close', (code, signal) => finish(signal === null ? (code ?? 1) : 1));
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* The catalog servers that join this session: the user's enabled choices from
|
|
440
|
+
* the preferences file. Called only after the license was confirmed.
|
|
441
|
+
* @param {string} root
|
|
442
|
+
* @param {'claude' | 'codex'} client
|
|
443
|
+
* @param {NodeJS.ProcessEnv} environment
|
|
444
|
+
*/
|
|
445
|
+
export async function readSessionServers(root, client, environment) {
|
|
446
|
+
return sessionServers(enabledServerIds(await readPreferences(root)), client, environment);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* @typedef {object} LaunchDependencies
|
|
451
|
+
* @property {typeof prepareEngineeringPack} [preparePack] AISAC Engineering pack loader (E4b)
|
|
452
|
+
* @property {typeof prepareGuardrails} [prepareGuardrails] Guardrail hooks plugin writer (E6a)
|
|
453
|
+
* @property {typeof readSessionServers} [readSessionServers] MCP catalog servers to load (E6c)
|
|
454
|
+
*/
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* @param {LaunchConfig} config
|
|
458
|
+
* @param {string} root
|
|
459
|
+
* @param {string[]} args
|
|
460
|
+
* @param {LaunchDependencies} [dependencies]
|
|
461
|
+
*/
|
|
462
|
+
export async function launchClaude(config, root, args, dependencies = {}) {
|
|
463
|
+
const preparePack = dependencies.preparePack ?? prepareEngineeringPack;
|
|
464
|
+
const writeGuardrails = dependencies.prepareGuardrails ?? prepareGuardrails;
|
|
465
|
+
const readServers = dependencies.readSessionServers ?? readSessionServers;
|
|
466
|
+
const validated = assertConfig(config);
|
|
467
|
+
const launchArgs = await parseClaudeLaunchArgs(args, process.cwd());
|
|
468
|
+
assertClaudeArgs(launchArgs.args);
|
|
469
|
+
const executable = resolveNative('claude');
|
|
470
|
+
if (!executable) throw new Error('claude_not_found');
|
|
471
|
+
const compatibility = await resolveClaudeCompatibility(executable, validated.packageRoot);
|
|
472
|
+
if (compatibility.status === 'rejected') throw new Error(claudeCompatibilityError(compatibility));
|
|
473
|
+
const pinnedExecutable = compatibility.executable ?? executable;
|
|
474
|
+
if (compatibility.status === 'host_checked')
|
|
475
|
+
process.stderr.write(
|
|
476
|
+
`aisac: Claude ${compatibility.version ?? 'unknown'} accepted by the local native host contract; end-to-end recovery remains separately qualified.\n`,
|
|
477
|
+
);
|
|
478
|
+
const currentVersion = compatibility.version ?? nativeVersion(pinnedExecutable);
|
|
479
|
+
const pluginRoot = await assertClaudeAssets(validated.packageRoot);
|
|
480
|
+
const telemetryActive =
|
|
481
|
+
validated.telemetry?.enabled === true && validated.telemetry?.disclosedAt != null;
|
|
482
|
+
const telemetryHelperPath = telemetryActive
|
|
483
|
+
? await assertTelemetryHelper(validated.packageRoot)
|
|
484
|
+
: undefined;
|
|
485
|
+
const telemetryConfigDigest = telemetryActive ? telemetryConfigBinding(validated) : undefined;
|
|
486
|
+
const observationActive =
|
|
487
|
+
validated.claudeObservation?.shadowEnabled === true ||
|
|
488
|
+
validated.claudeObservation?.diagnosticsEnabled === true;
|
|
489
|
+
const observationHelperPath = observationActive
|
|
490
|
+
? await assertClaudeObservationHelper(validated.packageRoot)
|
|
491
|
+
: undefined;
|
|
492
|
+
const observationConfigDigest = observationActive
|
|
493
|
+
? claudeObservationConfigBinding(validated)
|
|
494
|
+
: undefined;
|
|
495
|
+
const checkpointHelperPath = await assertClaudeCheckpointHelper(validated.packageRoot);
|
|
496
|
+
const checkpointConfigDigest = claudeCheckpointConfigBinding(validated);
|
|
497
|
+
const checkpointStateDir = await privateDirectory(join(root, 'claude', 'checkpoints'));
|
|
498
|
+
const directory = await launchDirectory(root, 'claude');
|
|
499
|
+
const settingsPath = join(directory, 'settings.json');
|
|
500
|
+
const mcpPath = join(directory, 'mcp.json');
|
|
501
|
+
const context = validated.context;
|
|
502
|
+
const legacy = validated.schemaVersion !== 2 || context === undefined;
|
|
503
|
+
const v2Context = context ?? makePolicy('conservative');
|
|
504
|
+
/** @type {Record<string, unknown>} */
|
|
505
|
+
const options = {
|
|
506
|
+
brainServer: MCP_ALIAS,
|
|
507
|
+
...(legacy
|
|
508
|
+
? { compactAtPercent: 85 }
|
|
509
|
+
: {
|
|
510
|
+
policyVersion: 2,
|
|
511
|
+
compactAtPercent: v2Context.compactAtPercent,
|
|
512
|
+
keepThreshold: v2Context.keepThreshold,
|
|
513
|
+
preserveRecentMessages: v2Context.preserveRecentMessages,
|
|
514
|
+
minReductionRatio: v2Context.minReductionRatio,
|
|
515
|
+
truncateHeadChars: v2Context.truncateHeadChars,
|
|
516
|
+
maxStateTokens: v2Context.maxStateTokens,
|
|
517
|
+
maxRequestTokens: v2Context.maxRequestTokens,
|
|
518
|
+
toolPolicy: v2Context.toolPolicy,
|
|
519
|
+
}),
|
|
520
|
+
enabled: true,
|
|
521
|
+
disclosed: true,
|
|
522
|
+
compactionStrategy: 'native_summary_notes',
|
|
523
|
+
preservationMode: 'upstream_compatible',
|
|
524
|
+
diagnosticMode: false,
|
|
525
|
+
telemetryEnabled: validated.telemetry?.enabled === true,
|
|
526
|
+
telemetryDisclosed:
|
|
527
|
+
validated.telemetry?.enabled === true && validated.telemetry?.disclosedAt != null,
|
|
528
|
+
telemetryClientVersion: currentVersion.match(/\b\d+\.\d+\.\d+\b/u)?.[0] ?? '',
|
|
529
|
+
...(telemetryActive
|
|
530
|
+
? {
|
|
531
|
+
telemetryNodePath: process.execPath,
|
|
532
|
+
telemetryHelperPath,
|
|
533
|
+
telemetryStateDir: resolve(root),
|
|
534
|
+
telemetryConfigDigest,
|
|
535
|
+
}
|
|
536
|
+
: {}),
|
|
537
|
+
...(observationActive
|
|
538
|
+
? {
|
|
539
|
+
observationNodePath: process.execPath,
|
|
540
|
+
observationHelperPath,
|
|
541
|
+
observationStateDir: resolve(root),
|
|
542
|
+
observationConfigDigest,
|
|
543
|
+
observationShadowEnabled: validated.claudeObservation?.shadowEnabled === true,
|
|
544
|
+
observationDiagnosticsEnabled: validated.claudeObservation?.diagnosticsEnabled === true,
|
|
545
|
+
}
|
|
546
|
+
: {}),
|
|
547
|
+
checkpointNodePath: process.execPath,
|
|
548
|
+
checkpointHelperPath,
|
|
549
|
+
checkpointStateDir,
|
|
550
|
+
checkpointConfigDigest,
|
|
551
|
+
checkpointBinding: checkpointConfigDigest,
|
|
552
|
+
};
|
|
553
|
+
if (!legacy && context.goal !== undefined) options.goal = context.goal;
|
|
554
|
+
const requiredAllow = [
|
|
555
|
+
`mcp__${MCP_ALIAS}__brain_context_status`,
|
|
556
|
+
`mcp__${MCP_ALIAS}__brain_context_compact`,
|
|
557
|
+
`mcp__${MCP_ALIAS}__brain_context_select`,
|
|
558
|
+
`mcp__${MCP_ALIAS}__brain_context_budget`,
|
|
559
|
+
...(options.telemetryEnabled && options.telemetryDisclosed
|
|
560
|
+
? [`mcp__${MCP_ALIAS}__brain_context_report`]
|
|
561
|
+
: []),
|
|
562
|
+
];
|
|
563
|
+
const userPermissions = launchArgs.settings?.permissions ?? {};
|
|
564
|
+
const userAllow = Array.isArray(userPermissions.allow) ? userPermissions.allow : [];
|
|
565
|
+
/** @type {string[]} */
|
|
566
|
+
const mergedAllow = [];
|
|
567
|
+
for (const rule of [...userAllow, ...requiredAllow]) {
|
|
568
|
+
if (!mergedAllow.includes(rule)) mergedAllow.push(rule);
|
|
569
|
+
}
|
|
570
|
+
// Catalog tools that spend money stay denied whatever the user allows or the
|
|
571
|
+
// permission mode is (spec aisac-engineering-extensions FR7.2).
|
|
572
|
+
const userDeny = Array.isArray(userPermissions.deny) ? userPermissions.deny : [];
|
|
573
|
+
/** @type {unknown[]} */
|
|
574
|
+
const mergedDeny = [];
|
|
575
|
+
for (const rule of [...userDeny, ...claudeDenyRules()]) {
|
|
576
|
+
if (!mergedDeny.includes(rule)) mergedDeny.push(rule);
|
|
577
|
+
}
|
|
578
|
+
const settings = {
|
|
579
|
+
...(launchArgs.settings?.sandbox === undefined ? {} : { sandbox: launchArgs.settings.sandbox }),
|
|
580
|
+
pluginConfigs: {
|
|
581
|
+
[PLUGIN_NAME]: { options },
|
|
582
|
+
[PLUGIN_INLINE_NAME]: { options: { ...options } },
|
|
583
|
+
},
|
|
584
|
+
permissions: {
|
|
585
|
+
...userPermissions,
|
|
586
|
+
allow: mergedAllow,
|
|
587
|
+
deny: mergedDeny,
|
|
588
|
+
},
|
|
589
|
+
};
|
|
590
|
+
try {
|
|
591
|
+
await privateJson(settingsPath, settings);
|
|
592
|
+
} catch (error) {
|
|
593
|
+
await rm(directory, { recursive: true, force: true });
|
|
594
|
+
throw error;
|
|
595
|
+
}
|
|
596
|
+
// The pack never blocks the launch: every failure starts Claude without it.
|
|
597
|
+
const engineering = await preparePack({
|
|
598
|
+
endpoint: validated.endpoint,
|
|
599
|
+
token: validated.token,
|
|
600
|
+
root,
|
|
601
|
+
directory,
|
|
602
|
+
packageRoot: validated.packageRoot,
|
|
603
|
+
}).catch(() => ({ pluginRoot: null, line: null }));
|
|
604
|
+
if (engineering.line !== null) process.stderr.write(`${engineering.line}\n`);
|
|
605
|
+
// Guardrails need the license this launch confirmed (the pack loaded), and
|
|
606
|
+
// like the pack they never block the launch (spec aisac-engineering-extensions FR5).
|
|
607
|
+
const guardrails =
|
|
608
|
+
engineering.pluginRoot === null
|
|
609
|
+
? null
|
|
610
|
+
: await writeGuardrails({ directory, root, packageRoot: validated.packageRoot }).catch(
|
|
611
|
+
() => null,
|
|
612
|
+
);
|
|
613
|
+
if (guardrails !== null) process.stderr.write(`${guardrails.line}\n`);
|
|
614
|
+
// Catalog servers need the same confirmed license, and never block the launch.
|
|
615
|
+
const servers =
|
|
616
|
+
engineering.pluginRoot === null
|
|
617
|
+
? null
|
|
618
|
+
: await readServers(root, 'claude', process.env).catch(() => null);
|
|
619
|
+
for (const line of servers === null ? [] : mcpLaunchLines(servers, 'claude'))
|
|
620
|
+
process.stderr.write(`${line}\n`);
|
|
621
|
+
const mcp = {
|
|
622
|
+
mcpServers: {
|
|
623
|
+
...(servers === null ? {} : claudeMcpEntries(servers.ready)),
|
|
624
|
+
[MCP_ALIAS]: {
|
|
625
|
+
type: 'http',
|
|
626
|
+
url: validated.endpoint,
|
|
627
|
+
headers: { Authorization: `Bearer ${validated.token}` },
|
|
628
|
+
},
|
|
629
|
+
},
|
|
630
|
+
};
|
|
631
|
+
try {
|
|
632
|
+
await privateJson(mcpPath, mcp);
|
|
633
|
+
} catch (error) {
|
|
634
|
+
await rm(directory, { recursive: true, force: true });
|
|
635
|
+
throw error;
|
|
636
|
+
}
|
|
637
|
+
const environment = childEnvironment(root);
|
|
638
|
+
environment.CLAUDE_CODE_ENABLE_FUNCTION_HOOKS = '1';
|
|
639
|
+
/** @type {import('node:child_process').ChildProcess} */
|
|
640
|
+
let child;
|
|
641
|
+
try {
|
|
642
|
+
child = spawn(
|
|
643
|
+
pinnedExecutable,
|
|
644
|
+
[
|
|
645
|
+
'--settings',
|
|
646
|
+
settingsPath,
|
|
647
|
+
'--mcp-config',
|
|
648
|
+
mcpPath,
|
|
649
|
+
'--allowedTools',
|
|
650
|
+
requiredAllow.join(','),
|
|
651
|
+
'--plugin-dir',
|
|
652
|
+
pluginRoot,
|
|
653
|
+
...(engineering.pluginRoot === null ? [] : ['--plugin-dir', engineering.pluginRoot]),
|
|
654
|
+
...(guardrails === null ? [] : ['--plugin-dir', guardrails.pluginRoot]),
|
|
655
|
+
...launchArgs.args,
|
|
656
|
+
],
|
|
657
|
+
{ cwd: process.cwd(), env: environment, stdio: 'inherit' },
|
|
658
|
+
);
|
|
659
|
+
} catch (error) {
|
|
660
|
+
await rm(directory, { recursive: true, force: true }).catch(() => undefined);
|
|
661
|
+
throw error;
|
|
662
|
+
}
|
|
663
|
+
// Listen for exit before any await, so a child that exits at once is not missed.
|
|
664
|
+
const finished = waitForChild(child, directory);
|
|
665
|
+
await recordChild(directory, child.pid);
|
|
666
|
+
return finished;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
/**
|
|
670
|
+
* @param {LaunchConfig} config
|
|
671
|
+
* @param {string} root
|
|
672
|
+
* @param {string[]} args
|
|
673
|
+
* @param {LaunchDependencies} [dependencies]
|
|
674
|
+
*/
|
|
675
|
+
export async function launchCodex(config, root, args, dependencies = {}) {
|
|
676
|
+
const preparePack = dependencies.preparePack ?? prepareEngineeringPack;
|
|
677
|
+
const readServers = dependencies.readSessionServers ?? readSessionServers;
|
|
678
|
+
const validated = assertConfig(config);
|
|
679
|
+
assertCodexArgs(args);
|
|
680
|
+
const captureLockPlatform = captureLockPlatformCheck();
|
|
681
|
+
if (!captureLockPlatform.ok)
|
|
682
|
+
throw new Error(
|
|
683
|
+
captureLockPlatform.code === 'CODEX_MACOS_UNSUPPORTED'
|
|
684
|
+
? `${captureLockPlatform.code}:${captureLockPlatform.message}`
|
|
685
|
+
: captureLockPlatform.code,
|
|
686
|
+
);
|
|
687
|
+
const executable = resolveNative('codex');
|
|
688
|
+
const node = resolveNative('node');
|
|
689
|
+
if (!executable || !node) throw new Error('codex_or_node_not_found');
|
|
690
|
+
const compatibility = await resolveCodexCompatibility(executable);
|
|
691
|
+
if (compatibility.status === 'rejected') throw new Error(codexCompatibilityError(compatibility));
|
|
692
|
+
if (compatibility.version === null) throw new Error('codex_version_unverified');
|
|
693
|
+
if (compatibility.status === 'protocol_match')
|
|
694
|
+
process.stderr.write(
|
|
695
|
+
`aisac: Codex ${compatibility.version} matched the local protocol contract; native behavioral qualification is not established.\n`,
|
|
696
|
+
);
|
|
697
|
+
const launchPlan = await parseCodexLaunchArgs(args, process.cwd());
|
|
698
|
+
const packageRoot = resolve(validated.packageRoot);
|
|
699
|
+
const terminal = join(packageRoot, 'assets', 'jev-paid-pilot-terminal.mjs');
|
|
700
|
+
const monitor = join(packageRoot, 'monitor.mjs');
|
|
701
|
+
const diagnosticModule = join(packageRoot, 'codex-diagnostics.mjs');
|
|
702
|
+
await Promise.all([
|
|
703
|
+
access(terminal, constants.R_OK),
|
|
704
|
+
access(monitor, constants.X_OK),
|
|
705
|
+
access(diagnosticModule, constants.R_OK),
|
|
706
|
+
]);
|
|
707
|
+
const checkpoints = await privateDirectory(join(root, 'codex', 'checkpoints'));
|
|
708
|
+
const notes = await privateDirectory(join(root, 'codex', 'notes'));
|
|
709
|
+
const directory = await launchDirectory(root, 'codex');
|
|
710
|
+
const environment = childEnvironment(root);
|
|
711
|
+
try {
|
|
712
|
+
if (
|
|
713
|
+
launchPlan.additionalRoots.length > 0 ||
|
|
714
|
+
Object.keys(launchPlan.server).length > 0 ||
|
|
715
|
+
launchPlan.profile !== undefined
|
|
716
|
+
) {
|
|
717
|
+
const configuredRoots =
|
|
718
|
+
launchPlan.additionalRoots.length > 0
|
|
719
|
+
? await readEffectiveWritableRoots(
|
|
720
|
+
executable,
|
|
721
|
+
launchPlan.effectiveCwd,
|
|
722
|
+
launchPlan.profile,
|
|
723
|
+
environment,
|
|
724
|
+
)
|
|
725
|
+
: [];
|
|
726
|
+
const envelope = makeCodexLaunchEnvelope(launchPlan, configuredRoots);
|
|
727
|
+
environment[CODEX_LAUNCH_ENVELOPE] = JSON.stringify(envelope);
|
|
728
|
+
}
|
|
729
|
+
} catch (error) {
|
|
730
|
+
await rm(directory, { recursive: true, force: true });
|
|
731
|
+
throw error;
|
|
732
|
+
}
|
|
733
|
+
// The pack never blocks the launch: every failure starts Codex without it.
|
|
734
|
+
const engineering = await preparePack({
|
|
735
|
+
endpoint: validated.endpoint,
|
|
736
|
+
token: validated.token,
|
|
737
|
+
root,
|
|
738
|
+
directory,
|
|
739
|
+
packageRoot: validated.packageRoot,
|
|
740
|
+
client: 'codex',
|
|
741
|
+
hostSupported: supportsCodexEngineering(compatibility.version),
|
|
742
|
+
}).catch(() => ({ pluginRoot: null, codex: null, line: null }));
|
|
743
|
+
if (engineering.line !== null) process.stderr.write(`${engineering.line}\n`);
|
|
744
|
+
if (engineering.codex != null) {
|
|
745
|
+
environment[CODEX_ENGINEERING_ENV] = JSON.stringify(engineering.codex);
|
|
746
|
+
// Catalog servers need the license the pack confirmed (spec extensions FR6).
|
|
747
|
+
const servers = await readServers(root, 'codex', environment).catch(() => null);
|
|
748
|
+
for (const line of servers === null ? [] : mcpLaunchLines(servers, 'codex'))
|
|
749
|
+
process.stderr.write(`${line}\n`);
|
|
750
|
+
if (servers !== null && servers.ready.length > 0)
|
|
751
|
+
environment[CODEX_MCP_ENV] = JSON.stringify(servers.ready.map((entry) => entry.id));
|
|
752
|
+
}
|
|
753
|
+
environment.AI_BRAIN_MCP_SERVER = MCP_ALIAS;
|
|
754
|
+
environment.CODEX_BIN = executable;
|
|
755
|
+
environment.CODEX_MONITOR_COMMAND = monitor;
|
|
756
|
+
environment[CODEX_MONITOR_CWD] = launchPlan.effectiveCwd;
|
|
757
|
+
environment.CODEX_CONTEXT_RECOVERY_ENABLED = '1';
|
|
758
|
+
environment.CODEX_CONTEXT_RECOVERY_DISCLOSED = '1';
|
|
759
|
+
environment.CODEX_CONTEXT_CHECKPOINT_DIR = checkpoints;
|
|
760
|
+
environment.CODEX_CONTEXT_RECOVERY_DIR = notes;
|
|
761
|
+
environment.CODEX_CONTEXT_DIAGNOSTICS_MODULE = diagnosticModule;
|
|
762
|
+
environment.CODEX_CONTEXT_DIAGNOSTICS_DIR = resolve(root);
|
|
763
|
+
environment.CODEX_CONTEXT_DIAGNOSTICS_ENABLED = '1';
|
|
764
|
+
environment.AIBRAIN_MCP_URL = validated.endpoint;
|
|
765
|
+
environment.AIBRAIN_MCP_TOKEN = validated.token;
|
|
766
|
+
environment.CODEX_CLIENT_VERSION = compatibility.version;
|
|
767
|
+
environment.CODEX_CONTEXT_TELEMETRY_ENABLED = validated.telemetry?.enabled === true ? '1' : '0';
|
|
768
|
+
environment.CODEX_CONTEXT_TELEMETRY_DISCLOSED =
|
|
769
|
+
validated.telemetry?.enabled === true && validated.telemetry?.disclosedAt != null ? '1' : '0';
|
|
770
|
+
if (validated.schemaVersion === 2 && validated.context !== undefined) {
|
|
771
|
+
environment.CODEX_CONTEXT_POLICY_JSON = codexPolicyJson(validated.context);
|
|
772
|
+
} else {
|
|
773
|
+
delete environment.CODEX_CONTEXT_POLICY_JSON;
|
|
774
|
+
}
|
|
775
|
+
const child = spawn(node, [terminal, '--', ...launchPlan.nativeArgs], {
|
|
776
|
+
cwd: process.cwd(),
|
|
777
|
+
env: environment,
|
|
778
|
+
stdio: 'inherit',
|
|
779
|
+
});
|
|
780
|
+
// Listen for exit before any await, so a child that exits at once is not missed.
|
|
781
|
+
const finished = waitForChild(child, directory);
|
|
782
|
+
await recordChild(directory, child.pid);
|
|
783
|
+
return finished;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
export const launchInternals = { MCP_ALIAS, PLUGIN_NAME, PLUGIN_INLINE_NAME };
|