@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
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { constants } from 'node:fs';
|
|
5
|
+
import { mkdtemp, open, rm, lstat } from 'node:fs/promises';
|
|
6
|
+
import { spawn } from 'node:child_process';
|
|
7
|
+
import { join } from 'node:path';
|
|
8
|
+
import { tmpdir } from 'node:os';
|
|
9
|
+
import fingerprint from './codex-protocol-fingerprint.json' with { type: 'json' };
|
|
10
|
+
|
|
11
|
+
const MAX_OUTPUT_BYTES = 64 * 1024;
|
|
12
|
+
const MAX_SCHEMA_BYTES = 8 * 1024 * 1024;
|
|
13
|
+
const MAX_SCHEMA_SET_BYTES = 24 * 1024 * 1024;
|
|
14
|
+
const MAX_SCHEMA_DEPTH = 96;
|
|
15
|
+
const MAX_SCHEMA_NODES = 250_000;
|
|
16
|
+
const ROOTS = Object.keys(fingerprint.roots);
|
|
17
|
+
const PAGINATION_CONTRACT = fingerprint.consumed_pagination ?? null;
|
|
18
|
+
const PAGINATION_CONTRACT_VALID =
|
|
19
|
+
PAGINATION_CONTRACT === null ||
|
|
20
|
+
(PAGINATION_CONTRACT.method === 'thread/turns/list' &&
|
|
21
|
+
PAGINATION_CONTRACT.request_schema === 'v2/ThreadTurnsListParams.json' &&
|
|
22
|
+
PAGINATION_CONTRACT.response_schema === 'v2/ThreadTurnsListResponse.json' &&
|
|
23
|
+
PAGINATION_CONTRACT.request_schema_sha256 ===
|
|
24
|
+
fingerprint.roots[PAGINATION_CONTRACT.request_schema]?.sha256 &&
|
|
25
|
+
PAGINATION_CONTRACT.response_schema_sha256 ===
|
|
26
|
+
fingerprint.roots[PAGINATION_CONTRACT.response_schema]?.sha256 &&
|
|
27
|
+
typeof PAGINATION_CONTRACT.graph_sha256 === 'string' &&
|
|
28
|
+
PAGINATION_CONTRACT.graph_sha256.length === 64);
|
|
29
|
+
const QUALIFIED_VERSIONS = new Set([
|
|
30
|
+
'0.161.0',
|
|
31
|
+
'0.162.0',
|
|
32
|
+
'0.162.1',
|
|
33
|
+
'0.163.0',
|
|
34
|
+
'0.164.0',
|
|
35
|
+
'0.166.0',
|
|
36
|
+
]);
|
|
37
|
+
const ANNOTATIONS = new Set([
|
|
38
|
+
'$comment',
|
|
39
|
+
'title',
|
|
40
|
+
'description',
|
|
41
|
+
'examples',
|
|
42
|
+
'deprecated',
|
|
43
|
+
'readOnly',
|
|
44
|
+
'writeOnly',
|
|
45
|
+
'externalDocs',
|
|
46
|
+
]);
|
|
47
|
+
const SCHEMA_MAP_KEYS = new Set([
|
|
48
|
+
'properties',
|
|
49
|
+
'definitions',
|
|
50
|
+
'$defs',
|
|
51
|
+
'patternProperties',
|
|
52
|
+
'dependentSchemas',
|
|
53
|
+
]);
|
|
54
|
+
const LITERAL_KEYS = new Set(['enum', 'const', 'default']);
|
|
55
|
+
/** @typedef {Record<string, unknown>} JsonObject */
|
|
56
|
+
|
|
57
|
+
/** @param {unknown} value @param {boolean} [map] @param {boolean} [literal] @returns {unknown} */
|
|
58
|
+
function normalize(value, map = false, literal = false) {
|
|
59
|
+
if (literal) return value;
|
|
60
|
+
if (Array.isArray(value)) return value.map((item) => normalize(item));
|
|
61
|
+
if (value === null || typeof value !== 'object') return value;
|
|
62
|
+
/** @type {JsonObject} */
|
|
63
|
+
const output = {};
|
|
64
|
+
for (const [key, child] of Object.entries(value)) {
|
|
65
|
+
if (!map && ANNOTATIONS.has(key)) continue;
|
|
66
|
+
if (LITERAL_KEYS.has(key)) {
|
|
67
|
+
output[key] = normalize(child, false, true);
|
|
68
|
+
} else if (
|
|
69
|
+
SCHEMA_MAP_KEYS.has(key) &&
|
|
70
|
+
child &&
|
|
71
|
+
typeof child === 'object' &&
|
|
72
|
+
!Array.isArray(child)
|
|
73
|
+
) {
|
|
74
|
+
output[key] = Object.fromEntries(
|
|
75
|
+
Object.entries(child).map(([name, schema]) => [name, normalize(schema)]),
|
|
76
|
+
);
|
|
77
|
+
} else output[key] = normalize(child);
|
|
78
|
+
}
|
|
79
|
+
return output;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** @param {unknown} value @returns {string} */
|
|
83
|
+
function canonical(value) {
|
|
84
|
+
if (Array.isArray(value)) return `[${value.map(canonical).join(',')}]`;
|
|
85
|
+
if (value && typeof value === 'object') {
|
|
86
|
+
const object = /** @type {JsonObject} */ (value);
|
|
87
|
+
return `{${Object.keys(object)
|
|
88
|
+
.sort()
|
|
89
|
+
.map((key) => `${JSON.stringify(key)}:${canonical(object[key])}`)
|
|
90
|
+
.join(',')}}`;
|
|
91
|
+
}
|
|
92
|
+
return JSON.stringify(value);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** @param {unknown} value */
|
|
96
|
+
function sha(value) {
|
|
97
|
+
return createHash('sha256').update(canonical(value), 'utf8').digest('hex');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** @param {string} output */
|
|
101
|
+
function parseStableVersion(output) {
|
|
102
|
+
const match = output
|
|
103
|
+
.trim()
|
|
104
|
+
.match(
|
|
105
|
+
/^(?:codex(?:-cli)?\s+)?(0|[1-9]\d{0,2})\.(0|[1-9]\d{0,2})\.(0|[1-9]\d{0,2})(-[A-Za-z0-9.-]+)?$/iu,
|
|
106
|
+
);
|
|
107
|
+
if (!match) return null;
|
|
108
|
+
return {
|
|
109
|
+
major: Number(match[1]),
|
|
110
|
+
minor: Number(match[2]),
|
|
111
|
+
patch: Number(match[3]),
|
|
112
|
+
prerelease: match[4] !== undefined,
|
|
113
|
+
text: `${match[1]}.${match[2]}.${match[3]}`,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** @param {unknown} value */
|
|
118
|
+
function assertBoundedSchema(value) {
|
|
119
|
+
const state = { nodes: 0 };
|
|
120
|
+
/** @param {unknown} current @param {number} depth */
|
|
121
|
+
function visit(current, depth) {
|
|
122
|
+
state.nodes += 1;
|
|
123
|
+
if (depth > MAX_SCHEMA_DEPTH || state.nodes > MAX_SCHEMA_NODES)
|
|
124
|
+
throw new Error('schema_shape_too_large');
|
|
125
|
+
if (Array.isArray(current)) {
|
|
126
|
+
for (const item of current) visit(item, depth + 1);
|
|
127
|
+
} else if (current && typeof current === 'object') {
|
|
128
|
+
for (const child of Object.values(current)) visit(child, depth + 1);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
visit(value, 0);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** @param {JsonObject} parsed @param {string} reference @returns {unknown} */
|
|
135
|
+
function localDefinition(parsed, reference) {
|
|
136
|
+
const prefix = reference.startsWith('#/definitions/') ? '#/definitions/' : '#/$defs/';
|
|
137
|
+
const name = reference.slice(prefix.length);
|
|
138
|
+
let current = prefix === '#/definitions/' ? parsed.definitions : parsed.$defs;
|
|
139
|
+
for (const part of name.split('/')) {
|
|
140
|
+
if (part === '__proto__' || part === 'prototype' || part === 'constructor') return undefined;
|
|
141
|
+
if (!current || typeof current !== 'object' || Array.isArray(current)) return undefined;
|
|
142
|
+
current = /** @type {JsonObject} */ (current)[part];
|
|
143
|
+
}
|
|
144
|
+
return current;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** @param {JsonObject} target @param {string} reference @param {unknown} value */
|
|
148
|
+
function setLocalDefinition(target, reference, value) {
|
|
149
|
+
const prefix = reference.startsWith('#/definitions/') ? '#/definitions/' : '#/$defs/';
|
|
150
|
+
const parts = reference.slice(prefix.length).split('/');
|
|
151
|
+
let current = target;
|
|
152
|
+
for (const part of parts.slice(0, -1)) {
|
|
153
|
+
if (part === '__proto__' || part === 'prototype' || part === 'constructor')
|
|
154
|
+
throw new Error('schema_reference_unsafe');
|
|
155
|
+
if (!current[part] || typeof current[part] !== 'object' || Array.isArray(current[part]))
|
|
156
|
+
current[part] = {};
|
|
157
|
+
current = /** @type {JsonObject} */ (current[part]);
|
|
158
|
+
}
|
|
159
|
+
if (
|
|
160
|
+
parts.at(-1) === '__proto__' ||
|
|
161
|
+
parts.at(-1) === 'prototype' ||
|
|
162
|
+
parts.at(-1) === 'constructor'
|
|
163
|
+
)
|
|
164
|
+
throw new Error('schema_reference_unsafe');
|
|
165
|
+
current[parts.at(-1) ?? ''] = value;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Project one generated schema root using the same consumed-method and reachable-reference
|
|
170
|
+
* rules used by the runtime compatibility check. A missing or duplicated method is reported
|
|
171
|
+
* separately so the runtime can retain its bounded diagnostic.
|
|
172
|
+
* @param {unknown} parsed
|
|
173
|
+
* @param {string[]|null} methods
|
|
174
|
+
* @returns {{normalized:unknown, methodMissing:boolean}}
|
|
175
|
+
*/
|
|
176
|
+
export function projectCodexSchema(parsed, methods) {
|
|
177
|
+
if (methods === null) return { normalized: normalize(parsed), methodMissing: false };
|
|
178
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))
|
|
179
|
+
return { normalized: null, methodMissing: true };
|
|
180
|
+
const root = /** @type {JsonObject} */ (parsed);
|
|
181
|
+
const oneOf = Array.isArray(root.oneOf) ? root.oneOf : [];
|
|
182
|
+
const selected = [];
|
|
183
|
+
for (const methodName of methods) {
|
|
184
|
+
const matches = oneOf.filter((/** @type {unknown} */ rawBranch) => {
|
|
185
|
+
if (!rawBranch || typeof rawBranch !== 'object' || Array.isArray(rawBranch)) return false;
|
|
186
|
+
const branch = /** @type {JsonObject} */ (rawBranch);
|
|
187
|
+
const properties = /** @type {JsonObject} */ (branch.properties ?? {});
|
|
188
|
+
const method = /** @type {JsonObject} */ (properties.method ?? {});
|
|
189
|
+
const values = Array.isArray(method.enum) ? method.enum : [];
|
|
190
|
+
return values.length === 1 && values[0] === methodName;
|
|
191
|
+
});
|
|
192
|
+
if (matches.length !== 1) return { normalized: null, methodMissing: true };
|
|
193
|
+
selected.push(matches[0]);
|
|
194
|
+
}
|
|
195
|
+
/** @type {JsonObject} */
|
|
196
|
+
const definitions = {};
|
|
197
|
+
const found = new Set();
|
|
198
|
+
/** @param {unknown} value @param {boolean} [map] */
|
|
199
|
+
const visit = (value, map = false) => {
|
|
200
|
+
if (!value || typeof value !== 'object') return;
|
|
201
|
+
if (Array.isArray(value)) {
|
|
202
|
+
value.forEach((item) => visit(item));
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const object = /** @type {JsonObject} */ (value);
|
|
206
|
+
if (typeof object.$ref === 'string') {
|
|
207
|
+
if (!object.$ref.startsWith('#/definitions/') && !object.$ref.startsWith('#/$defs/'))
|
|
208
|
+
throw new Error('schema_reference_nonlocal');
|
|
209
|
+
const prefix = object.$ref.startsWith('#/definitions/') ? '#/definitions/' : '#/$defs/';
|
|
210
|
+
const name = object.$ref.slice(prefix.length);
|
|
211
|
+
const source = localDefinition(root, object.$ref);
|
|
212
|
+
if (source === undefined) throw new Error('schema_reference_missing');
|
|
213
|
+
if (!found.has(`${prefix}${name}`)) {
|
|
214
|
+
found.add(`${prefix}${name}`);
|
|
215
|
+
visit(source);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
for (const [key, child] of Object.entries(object)) {
|
|
219
|
+
if (map) {
|
|
220
|
+
visit(child);
|
|
221
|
+
} else if (
|
|
222
|
+
key !== 'definitions' &&
|
|
223
|
+
key !== '$defs' &&
|
|
224
|
+
!LITERAL_KEYS.has(key) &&
|
|
225
|
+
!ANNOTATIONS.has(key)
|
|
226
|
+
) {
|
|
227
|
+
if (
|
|
228
|
+
SCHEMA_MAP_KEYS.has(key) &&
|
|
229
|
+
child &&
|
|
230
|
+
typeof child === 'object' &&
|
|
231
|
+
!Array.isArray(child)
|
|
232
|
+
) {
|
|
233
|
+
for (const schema of Object.values(child)) visit(schema);
|
|
234
|
+
} else visit(child);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
for (const [key, child] of Object.entries(root)) {
|
|
239
|
+
if (
|
|
240
|
+
key === 'oneOf' ||
|
|
241
|
+
key === 'definitions' ||
|
|
242
|
+
key === '$defs' ||
|
|
243
|
+
LITERAL_KEYS.has(key) ||
|
|
244
|
+
ANNOTATIONS.has(key)
|
|
245
|
+
)
|
|
246
|
+
continue;
|
|
247
|
+
if (SCHEMA_MAP_KEYS.has(key) && child && typeof child === 'object' && !Array.isArray(child)) {
|
|
248
|
+
for (const schema of Object.values(child)) visit(schema);
|
|
249
|
+
} else visit(child);
|
|
250
|
+
}
|
|
251
|
+
visit(selected);
|
|
252
|
+
for (const reference of [...found].sort()) {
|
|
253
|
+
setLocalDefinition(definitions, reference, normalize(localDefinition(root, reference)));
|
|
254
|
+
}
|
|
255
|
+
const projectedRoot = /** @type {JsonObject} */ (normalize(root));
|
|
256
|
+
delete projectedRoot.oneOf;
|
|
257
|
+
delete projectedRoot.definitions;
|
|
258
|
+
delete projectedRoot.$defs;
|
|
259
|
+
projectedRoot.oneOf = selected.map((/** @type {unknown} */ branch) => normalize(branch));
|
|
260
|
+
projectedRoot.definitions = definitions;
|
|
261
|
+
return { normalized: projectedRoot, methodMissing: false };
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** @param {unknown} value @returns {string} */
|
|
265
|
+
export function codexSchemaHash(value) {
|
|
266
|
+
return sha(value);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** @param {string} path @param {number} maximumBytes @returns {Promise<string>} */
|
|
270
|
+
async function readBoundedFile(path, maximumBytes) {
|
|
271
|
+
const handle = await open(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
272
|
+
try {
|
|
273
|
+
const info = await handle.stat();
|
|
274
|
+
if (!info.isFile() || info.size > maximumBytes) throw new Error('schema_file_too_large');
|
|
275
|
+
const buffer = Buffer.alloc(info.size);
|
|
276
|
+
let offset = 0;
|
|
277
|
+
while (offset < buffer.length) {
|
|
278
|
+
const result = await handle.read(buffer, offset, buffer.length - offset, offset);
|
|
279
|
+
if (result.bytesRead === 0) break;
|
|
280
|
+
offset += result.bytesRead;
|
|
281
|
+
}
|
|
282
|
+
if (offset !== info.size) throw new Error('schema_file_short_read');
|
|
283
|
+
return buffer.toString('utf8');
|
|
284
|
+
} finally {
|
|
285
|
+
await handle.close();
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** @param {string} path */
|
|
290
|
+
async function assertOwnedDirectory(path) {
|
|
291
|
+
const info = await lstat(path);
|
|
292
|
+
const uid = process.getuid?.();
|
|
293
|
+
if (!info.isDirectory() || info.isSymbolicLink() || (uid !== undefined && info.uid !== uid))
|
|
294
|
+
throw new Error('schema_directory_invalid');
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** @param {string} executable @param {string[]} args @param {NodeJS.ProcessEnv} environment */
|
|
298
|
+
function runBounded(executable, args, environment) {
|
|
299
|
+
return new Promise((resolvePromise) => {
|
|
300
|
+
const child = spawn(executable, args, {
|
|
301
|
+
cwd: environment.CODEX_COMPAT_CWD,
|
|
302
|
+
env: environment,
|
|
303
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
304
|
+
detached: process.platform !== 'win32',
|
|
305
|
+
});
|
|
306
|
+
let output = '';
|
|
307
|
+
let error = '';
|
|
308
|
+
const outputState = { bytes: 0, overflow: false };
|
|
309
|
+
const errorState = { bytes: 0, overflow: false };
|
|
310
|
+
let settled = false;
|
|
311
|
+
/** @type {NodeJS.Timeout|undefined} */
|
|
312
|
+
let timeoutTimer;
|
|
313
|
+
/** @param {number|null} code @param {boolean} [timedOut] */
|
|
314
|
+
const finish = (code, timedOut = false) => {
|
|
315
|
+
if (settled) return;
|
|
316
|
+
settled = true;
|
|
317
|
+
clearTimeout(timer);
|
|
318
|
+
clearTimeout(timeoutTimer);
|
|
319
|
+
resolvePromise({
|
|
320
|
+
code,
|
|
321
|
+
output,
|
|
322
|
+
error,
|
|
323
|
+
outputOverflow: outputState.overflow,
|
|
324
|
+
errorOverflow: errorState.overflow,
|
|
325
|
+
timedOut,
|
|
326
|
+
});
|
|
327
|
+
};
|
|
328
|
+
const killOwnedProcess = () => {
|
|
329
|
+
if (child.pid === undefined) return;
|
|
330
|
+
try {
|
|
331
|
+
if (process.platform !== 'win32') process.kill(-child.pid, 'SIGKILL');
|
|
332
|
+
else child.kill('SIGKILL');
|
|
333
|
+
} catch {
|
|
334
|
+
try {
|
|
335
|
+
child.kill('SIGKILL');
|
|
336
|
+
} catch {
|
|
337
|
+
// The owned child may have exited between the group and direct kill.
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
const timer = setTimeout(() => {
|
|
342
|
+
killOwnedProcess();
|
|
343
|
+
timeoutTimer = setTimeout(() => finish(null, true), 1_000);
|
|
344
|
+
}, 15_000);
|
|
345
|
+
/** @param {string} target @param {Buffer|string} chunk @param {{bytes:number, overflow:boolean}} state */
|
|
346
|
+
const collect = (target, chunk, state) => {
|
|
347
|
+
const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk), 'utf8');
|
|
348
|
+
const remaining = MAX_OUTPUT_BYTES - state.bytes;
|
|
349
|
+
if (remaining <= 0) {
|
|
350
|
+
state.overflow = true;
|
|
351
|
+
return target;
|
|
352
|
+
}
|
|
353
|
+
if (bytes.length > remaining) state.overflow = true;
|
|
354
|
+
const accepted = bytes.subarray(0, remaining);
|
|
355
|
+
state.bytes += accepted.length;
|
|
356
|
+
return `${target}${accepted.toString('utf8')}`;
|
|
357
|
+
};
|
|
358
|
+
child.stdout.on('data', (chunk) => {
|
|
359
|
+
output = collect(output, chunk, outputState);
|
|
360
|
+
});
|
|
361
|
+
child.stderr.on('data', (chunk) => {
|
|
362
|
+
error = collect(error, chunk, errorState);
|
|
363
|
+
});
|
|
364
|
+
child.once('error', () => finish(null));
|
|
365
|
+
child.once('close', (code) => finish(code));
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** @param {string} executable @param {NodeJS.ProcessEnv} [baseEnvironment] */
|
|
370
|
+
export async function resolveCodexCompatibility(executable, baseEnvironment = process.env) {
|
|
371
|
+
if (!PAGINATION_CONTRACT_VALID)
|
|
372
|
+
return { status: 'rejected', reason: 'protocol_fingerprint_invalid', version: null };
|
|
373
|
+
const environment = { PATH: baseEnvironment.PATH ?? '', LANG: 'C.UTF-8' };
|
|
374
|
+
const versionRun = await runBounded(executable, ['--version'], {
|
|
375
|
+
...environment,
|
|
376
|
+
CODEX_COMPAT_CWD: process.cwd(),
|
|
377
|
+
});
|
|
378
|
+
if (versionRun.code !== 0 || versionRun.outputOverflow)
|
|
379
|
+
return { status: 'rejected', reason: 'version_unavailable', version: null };
|
|
380
|
+
const version = parseStableVersion(versionRun.output);
|
|
381
|
+
if (version === null) return { status: 'rejected', reason: 'version_unavailable', version: null };
|
|
382
|
+
if (version.prerelease || version.major !== 0)
|
|
383
|
+
return { status: 'rejected', reason: 'version_not_stable_0x', version: version.text };
|
|
384
|
+
if (QUALIFIED_VERSIONS.has(version.text))
|
|
385
|
+
return { status: 'qualified', reason: 'native_qualified', version: version.text };
|
|
386
|
+
if (version.minor < 163)
|
|
387
|
+
return { status: 'rejected', reason: 'version_unqualified', version: version.text };
|
|
388
|
+
const root = await mkdtemp(join(tmpdir(), 'aibrain-codex-compat-'));
|
|
389
|
+
const schemaDir = join(root, 'schema');
|
|
390
|
+
try {
|
|
391
|
+
const env = { ...environment, CODEX_HOME: join(root, 'codex-home'), CODEX_COMPAT_CWD: root };
|
|
392
|
+
const generated = await runBounded(
|
|
393
|
+
executable,
|
|
394
|
+
['app-server', 'generate-json-schema', '--experimental', '--out', schemaDir],
|
|
395
|
+
env,
|
|
396
|
+
);
|
|
397
|
+
if (generated.code !== 0)
|
|
398
|
+
return { status: 'rejected', reason: 'schema_generation_failed', version: version.text };
|
|
399
|
+
if (generated.outputOverflow)
|
|
400
|
+
return { status: 'rejected', reason: 'schema_generation_failed', version: version.text };
|
|
401
|
+
/** @type {Record<string, string>} */
|
|
402
|
+
const hashes = {};
|
|
403
|
+
let aggregateBytes = 0;
|
|
404
|
+
await assertOwnedDirectory(schemaDir);
|
|
405
|
+
for (const file of ROOTS) {
|
|
406
|
+
const path = join(schemaDir, file);
|
|
407
|
+
let directory = schemaDir;
|
|
408
|
+
for (const part of file.split('/').slice(0, -1)) {
|
|
409
|
+
directory = join(directory, part);
|
|
410
|
+
const directoryEntry = await lstat(directory).catch(() => null);
|
|
411
|
+
if (directoryEntry === null)
|
|
412
|
+
return {
|
|
413
|
+
status: 'rejected',
|
|
414
|
+
reason: 'schema_root_missing_or_invalid',
|
|
415
|
+
version: version.text,
|
|
416
|
+
};
|
|
417
|
+
await assertOwnedDirectory(directory);
|
|
418
|
+
}
|
|
419
|
+
const entry = await lstat(path).catch(() => null);
|
|
420
|
+
if (
|
|
421
|
+
entry === null ||
|
|
422
|
+
entry.isSymbolicLink() ||
|
|
423
|
+
!entry.isFile() ||
|
|
424
|
+
(process.getuid?.() !== undefined && entry.uid !== process.getuid()) ||
|
|
425
|
+
entry.size > MAX_SCHEMA_BYTES
|
|
426
|
+
)
|
|
427
|
+
return {
|
|
428
|
+
status: 'rejected',
|
|
429
|
+
reason: 'schema_root_missing_or_invalid',
|
|
430
|
+
version: version.text,
|
|
431
|
+
};
|
|
432
|
+
aggregateBytes += entry.size;
|
|
433
|
+
if (aggregateBytes > MAX_SCHEMA_SET_BYTES)
|
|
434
|
+
return { status: 'rejected', reason: 'schema_set_too_large', version: version.text };
|
|
435
|
+
const parsed = JSON.parse(await readBoundedFile(path, MAX_SCHEMA_BYTES));
|
|
436
|
+
assertBoundedSchema(parsed);
|
|
437
|
+
const rootMap = /** @type {Record<string, {methods: string[]|null, sha256: string}>} */ (
|
|
438
|
+
fingerprint.roots
|
|
439
|
+
);
|
|
440
|
+
const expectedRoot = rootMap[file];
|
|
441
|
+
const projection = projectCodexSchema(parsed, expectedRoot.methods);
|
|
442
|
+
if (projection.methodMissing)
|
|
443
|
+
return { status: 'rejected', reason: 'protocol_method_missing', version: version.text };
|
|
444
|
+
hashes[file] = sha(projection.normalized);
|
|
445
|
+
if (hashes[file] !== expectedRoot.sha256)
|
|
446
|
+
return { status: 'rejected', reason: 'protocol_schema_mismatch', version: version.text };
|
|
447
|
+
}
|
|
448
|
+
const help = await runBounded(executable, ['app-server', '--help'], env);
|
|
449
|
+
const terminalHelp = await runBounded(executable, ['--help'], env);
|
|
450
|
+
if (
|
|
451
|
+
help.code !== 0 ||
|
|
452
|
+
help.outputOverflow ||
|
|
453
|
+
!help.output.includes('--listen') ||
|
|
454
|
+
!help.output.includes('stdio://') ||
|
|
455
|
+
terminalHelp.code !== 0 ||
|
|
456
|
+
terminalHelp.outputOverflow ||
|
|
457
|
+
!terminalHelp.output.includes('--remote') ||
|
|
458
|
+
!terminalHelp.output.includes('unix://')
|
|
459
|
+
)
|
|
460
|
+
return { status: 'rejected', reason: 'transport_flags_missing', version: version.text };
|
|
461
|
+
const recheck = await runBounded(executable, ['--version'], {
|
|
462
|
+
...environment,
|
|
463
|
+
CODEX_COMPAT_CWD: process.cwd(),
|
|
464
|
+
});
|
|
465
|
+
const recheckedVersion = parseStableVersion(recheck.output);
|
|
466
|
+
if (
|
|
467
|
+
recheck.code !== 0 ||
|
|
468
|
+
recheck.outputOverflow ||
|
|
469
|
+
recheckedVersion?.text !== version.text ||
|
|
470
|
+
recheckedVersion?.prerelease
|
|
471
|
+
)
|
|
472
|
+
return { status: 'rejected', reason: 'version_changed_during_probe', version: version.text };
|
|
473
|
+
return {
|
|
474
|
+
status: 'protocol_match',
|
|
475
|
+
reason: 'protocol_fingerprint_match',
|
|
476
|
+
version: version.text,
|
|
477
|
+
};
|
|
478
|
+
} catch {
|
|
479
|
+
return { status: 'rejected', reason: 'protocol_probe_failed', version: version.text };
|
|
480
|
+
} finally {
|
|
481
|
+
await rm(root, { recursive: true, force: true });
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/** @param {string} left @param {string} right */
|
|
486
|
+
function compareStableVersions(left, right) {
|
|
487
|
+
const a = left.split('.').map(Number);
|
|
488
|
+
const b = right.split('.').map(Number);
|
|
489
|
+
for (let index = 0; index < 3; index += 1) {
|
|
490
|
+
const difference = (a[index] ?? 0) - (b[index] ?? 0);
|
|
491
|
+
if (difference !== 0) return difference;
|
|
492
|
+
}
|
|
493
|
+
return 0;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/** The newest Codex release this client was qualified against. */
|
|
497
|
+
export const LATEST_QUALIFIED_CODEX = /** @type {string} */ (
|
|
498
|
+
[...QUALIFIED_VERSIONS].sort(compareStableVersions).at(-1)
|
|
499
|
+
);
|
|
500
|
+
|
|
501
|
+
// Rejections a user fixes by installing a qualified Codex (or a newer aisac):
|
|
502
|
+
// the installed release's contract or transport differs from the bundled one.
|
|
503
|
+
const CONTRACT_REJECTIONS = new Set([
|
|
504
|
+
'protocol_schema_mismatch',
|
|
505
|
+
'protocol_method_missing',
|
|
506
|
+
'schema_root_missing_or_invalid',
|
|
507
|
+
'schema_set_too_large',
|
|
508
|
+
'schema_generation_failed',
|
|
509
|
+
'transport_flags_missing',
|
|
510
|
+
'protocol_probe_failed',
|
|
511
|
+
]);
|
|
512
|
+
|
|
513
|
+
/** @param {{reason:string, version:string|null}} decision @returns {string|null} */
|
|
514
|
+
export function codexCompatibilityRemedy(decision) {
|
|
515
|
+
const detected = decision.version === null ? 'The installed Codex' : `Codex ${decision.version}`;
|
|
516
|
+
const reinstall = `npm install -g @openai/codex@${LATEST_QUALIFIED_CODEX}`;
|
|
517
|
+
if (CONTRACT_REJECTIONS.has(decision.reason))
|
|
518
|
+
return `${detected} is not qualified by this aisac (newest qualified: ${LATEST_QUALIFIED_CODEX}). Install the qualified Codex with \`${reinstall}\`, or check for an aisac release that qualifies it with \`npm install -g @aisac/cli@beta\`.`;
|
|
519
|
+
if (decision.reason === 'version_unqualified')
|
|
520
|
+
return `${detected} is older than this aisac supports. Update it with \`${reinstall}\`.`;
|
|
521
|
+
if (decision.reason === 'version_not_stable_0x')
|
|
522
|
+
return `${detected} is not a stable 0.x release. Install a qualified one with \`${reinstall}\`.`;
|
|
523
|
+
return null;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/** @param {{reason:string, version:string|null}} decision @returns {string} */
|
|
527
|
+
export function codexCompatibilityError(decision) {
|
|
528
|
+
const coded = `codex_protocol_incompatible:${decision.reason}:detected=${decision.version ?? 'unavailable'};run aisac doctor --json`;
|
|
529
|
+
const remedy = codexCompatibilityRemedy(decision);
|
|
530
|
+
return remedy === null ? coded : `${coded}\n${remedy}`;
|
|
531
|
+
}
|