@aisac/cli 0.0.0-stage → 0.4.0-beta.2
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 +166 -2
- package/dist/assets/claude/.claude-plugin/plugin.json +147 -0
- package/dist/assets/claude/hooks/context-compaction.ts +6930 -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 +4689 -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 +771 -0
- package/dist/codex-compatibility.mjs +481 -0
- package/dist/codex-diagnostics.mjs +447 -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 +722 -0
- package/dist/engineering.mjs +194 -0
- package/dist/launch.mjs +690 -0
- package/dist/mcp.mjs +962 -0
- package/dist/monitor.mjs +133 -0
- package/dist/package.json +5 -0
- package/dist/platform.mjs +126 -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/state.mjs
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chmod,
|
|
3
|
+
lstat,
|
|
4
|
+
mkdir,
|
|
5
|
+
readFile,
|
|
6
|
+
rename,
|
|
7
|
+
rm,
|
|
8
|
+
stat,
|
|
9
|
+
unlink,
|
|
10
|
+
writeFile,
|
|
11
|
+
} from 'node:fs/promises';
|
|
12
|
+
import { dirname, join, resolve } from 'node:path';
|
|
13
|
+
import { homedir } from 'node:os';
|
|
14
|
+
import { createHash } from 'node:crypto';
|
|
15
|
+
import { isValidPolicy } from './policy.mjs';
|
|
16
|
+
|
|
17
|
+
/** @typedef {'claude' | 'codex' | 'both'} ClientSelection */
|
|
18
|
+
/** @typedef {{ ok: true, providerBatches: 0, providerModel: null, protocol?: 'v1' | 'v2' }} Probe */
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {{
|
|
21
|
+
* schemaVersion: 1 | 2,
|
|
22
|
+
* packageVersion: string,
|
|
23
|
+
* disclosureVersion: string,
|
|
24
|
+
* disclosedAt: string,
|
|
25
|
+
* endpoint: string,
|
|
26
|
+
* clientSelection: ClientSelection,
|
|
27
|
+
* claudeVersion: string | null,
|
|
28
|
+
* codexVersion: string | null,
|
|
29
|
+
* packageRoot: string,
|
|
30
|
+
* token: string,
|
|
31
|
+
* tokenSha256: string,
|
|
32
|
+
* probe: Probe,
|
|
33
|
+
* context?: import('./policy.mjs').ContextPolicy,
|
|
34
|
+
* telemetry?: { enabled: boolean, disclosedAt: string | null },
|
|
35
|
+
* claudeObservation?: { shadowEnabled: boolean, diagnosticsEnabled: boolean, disclosedAt: string | null }
|
|
36
|
+
* }} Config
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
export const DISCLOSURE_VERSION = 'aibrain-paid-context-v2-2026-10-10';
|
|
40
|
+
export const LEGACY_DISCLOSURE_VERSION = 'aibrain-paid-context-v1-2026-10-09';
|
|
41
|
+
|
|
42
|
+
/** @param {unknown} error @returns {error is NodeJS.ErrnoException} */
|
|
43
|
+
function isNodeError(error) {
|
|
44
|
+
return (
|
|
45
|
+
typeof error === 'object' && error !== null && 'code' in error && typeof error.code === 'string'
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** @param {unknown} error */
|
|
50
|
+
export function errorMessage(error) {
|
|
51
|
+
return error instanceof Error ? error.message : 'unknown_error';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** @param {string} endpoint */
|
|
55
|
+
function validEndpoint(endpoint) {
|
|
56
|
+
try {
|
|
57
|
+
const url = new URL(endpoint);
|
|
58
|
+
return (
|
|
59
|
+
(url.protocol === 'https:' ||
|
|
60
|
+
(url.protocol === 'http:' &&
|
|
61
|
+
['127.0.0.1', 'localhost', '::1', '[::1]'].includes(url.hostname))) &&
|
|
62
|
+
url.username === '' &&
|
|
63
|
+
url.password === '' &&
|
|
64
|
+
url.hash === '' &&
|
|
65
|
+
url.search === ''
|
|
66
|
+
);
|
|
67
|
+
} catch {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function stateRoot() {
|
|
73
|
+
return process.env.AIBRAIN_STATE_DIR
|
|
74
|
+
? resolve(process.env.AIBRAIN_STATE_DIR)
|
|
75
|
+
: join(homedir(), '.aibrain');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Check every existing path component so a state directory cannot be reached
|
|
80
|
+
* through a symlinked ancestor. Missing components are allowed for creation.
|
|
81
|
+
* @param {string} path
|
|
82
|
+
* @param {boolean} [allowMissing]
|
|
83
|
+
*/
|
|
84
|
+
async function assertSafePath(path, allowMissing = true) {
|
|
85
|
+
let cursor = resolve(path);
|
|
86
|
+
let targetMissing = false;
|
|
87
|
+
let first = true;
|
|
88
|
+
while (true) {
|
|
89
|
+
try {
|
|
90
|
+
const entry = await lstat(cursor);
|
|
91
|
+
if (entry.isSymbolicLink()) throw new Error('symlink_state_rejected');
|
|
92
|
+
if (
|
|
93
|
+
cursor === resolve(path) &&
|
|
94
|
+
entry.uid !== undefined &&
|
|
95
|
+
typeof process.getuid === 'function' &&
|
|
96
|
+
entry.uid !== process.getuid()
|
|
97
|
+
)
|
|
98
|
+
throw new Error('state_owner_mismatch');
|
|
99
|
+
} catch (error) {
|
|
100
|
+
if (!isNodeError(error) || error.code !== 'ENOENT') throw error;
|
|
101
|
+
if (first) targetMissing = true;
|
|
102
|
+
}
|
|
103
|
+
const parent = dirname(cursor);
|
|
104
|
+
if (parent === cursor) break;
|
|
105
|
+
cursor = parent;
|
|
106
|
+
first = false;
|
|
107
|
+
}
|
|
108
|
+
if (targetMissing && !allowMissing) throw new Error('state_path_missing');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export async function ensureStateRoot() {
|
|
112
|
+
const root = stateRoot();
|
|
113
|
+
await assertSafePath(root);
|
|
114
|
+
await mkdir(root, { recursive: true, mode: 0o700 });
|
|
115
|
+
await chmod(root, 0o700);
|
|
116
|
+
await assertSafePath(root, false);
|
|
117
|
+
return root;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** @param {unknown} value @returns {value is Record<string, unknown>} */
|
|
121
|
+
function isRecord(value) {
|
|
122
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** @param {unknown} value */
|
|
126
|
+
function isDisclosure(value) {
|
|
127
|
+
return value === DISCLOSURE_VERSION || value === LEGACY_DISCLOSURE_VERSION;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** @param {unknown} value */
|
|
131
|
+
function isTelemetry(value) {
|
|
132
|
+
return (
|
|
133
|
+
isRecord(value) &&
|
|
134
|
+
typeof value.enabled === 'boolean' &&
|
|
135
|
+
(value.disclosedAt === null ||
|
|
136
|
+
(typeof value.disclosedAt === 'string' &&
|
|
137
|
+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/u.test(value.disclosedAt) &&
|
|
138
|
+
Number.isFinite(Date.parse(value.disclosedAt)))) &&
|
|
139
|
+
Object.keys(value).every((key) => ['enabled', 'disclosedAt'].includes(key))
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** @param {unknown} value */
|
|
144
|
+
function isClaudeObservation(value) {
|
|
145
|
+
return (
|
|
146
|
+
isRecord(value) &&
|
|
147
|
+
typeof value.shadowEnabled === 'boolean' &&
|
|
148
|
+
typeof value.diagnosticsEnabled === 'boolean' &&
|
|
149
|
+
(value.disclosedAt === null ||
|
|
150
|
+
(typeof value.disclosedAt === 'string' &&
|
|
151
|
+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/u.test(value.disclosedAt) &&
|
|
152
|
+
Number.isFinite(Date.parse(value.disclosedAt)))) &&
|
|
153
|
+
Object.keys(value).every((key) =>
|
|
154
|
+
['shadowEnabled', 'diagnosticsEnabled', 'disclosedAt'].includes(key),
|
|
155
|
+
)
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** @param {unknown} value */
|
|
160
|
+
function isConfig(value) {
|
|
161
|
+
if (!isRecord(value)) return false;
|
|
162
|
+
const endpoint = value.endpoint;
|
|
163
|
+
const clientSelection = value.clientSelection;
|
|
164
|
+
if (
|
|
165
|
+
(value.schemaVersion !== 1 && value.schemaVersion !== 2) ||
|
|
166
|
+
typeof value.packageVersion !== 'string' ||
|
|
167
|
+
!isDisclosure(value.disclosureVersion) ||
|
|
168
|
+
typeof value.disclosedAt !== 'string' ||
|
|
169
|
+
!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/u.test(value.disclosedAt) ||
|
|
170
|
+
!Number.isFinite(Date.parse(value.disclosedAt)) ||
|
|
171
|
+
typeof endpoint !== 'string' ||
|
|
172
|
+
!validEndpoint(endpoint) ||
|
|
173
|
+
typeof clientSelection !== 'string' ||
|
|
174
|
+
!['claude', 'codex', 'both'].includes(clientSelection) ||
|
|
175
|
+
(value.claudeVersion !== null && typeof value.claudeVersion !== 'string') ||
|
|
176
|
+
(value.codexVersion !== null && typeof value.codexVersion !== 'string') ||
|
|
177
|
+
typeof value.packageRoot !== 'string' ||
|
|
178
|
+
typeof value.token !== 'string' ||
|
|
179
|
+
value.token.length < 1 ||
|
|
180
|
+
value.token.length > 16 * 1024 ||
|
|
181
|
+
/[\u0000-\u001f\u007f-\u009f]/u.test(value.token) ||
|
|
182
|
+
!/^[a-f0-9]{64}$/u.test(typeof value.tokenSha256 === 'string' ? value.tokenSha256 : '') ||
|
|
183
|
+
!isRecord(value.probe) ||
|
|
184
|
+
value.probe.ok !== true ||
|
|
185
|
+
value.probe.providerBatches !== 0 ||
|
|
186
|
+
value.probe.providerModel !== null ||
|
|
187
|
+
Object.keys(value.probe).some(
|
|
188
|
+
(key) => !['ok', 'providerBatches', 'providerModel', 'protocol'].includes(key),
|
|
189
|
+
) ||
|
|
190
|
+
(value.schemaVersion === 2 && value.probe.protocol !== 'v2') ||
|
|
191
|
+
(value.schemaVersion === 1 && value.probe.protocol !== undefined)
|
|
192
|
+
)
|
|
193
|
+
return false;
|
|
194
|
+
if (
|
|
195
|
+
value.schemaVersion === 2 &&
|
|
196
|
+
(value.disclosureVersion !== DISCLOSURE_VERSION || !isValidPolicy(value.context))
|
|
197
|
+
)
|
|
198
|
+
return false;
|
|
199
|
+
if (value.schemaVersion === 1 && value.context !== undefined) return false;
|
|
200
|
+
if (value.telemetry !== undefined && !isTelemetry(value.telemetry)) return false;
|
|
201
|
+
if (value.claudeObservation !== undefined && !isClaudeObservation(value.claudeObservation))
|
|
202
|
+
return false;
|
|
203
|
+
const allowed = new Set([
|
|
204
|
+
'schemaVersion',
|
|
205
|
+
'packageVersion',
|
|
206
|
+
'disclosureVersion',
|
|
207
|
+
'disclosedAt',
|
|
208
|
+
'endpoint',
|
|
209
|
+
'clientSelection',
|
|
210
|
+
'claudeVersion',
|
|
211
|
+
'codexVersion',
|
|
212
|
+
'packageRoot',
|
|
213
|
+
'token',
|
|
214
|
+
'tokenSha256',
|
|
215
|
+
'probe',
|
|
216
|
+
'context',
|
|
217
|
+
'telemetry',
|
|
218
|
+
'claudeObservation',
|
|
219
|
+
]);
|
|
220
|
+
return Object.keys(value).every((key) => allowed.has(key));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** @param {unknown} value */
|
|
224
|
+
export function isValidConfig(value) {
|
|
225
|
+
return isConfig(value);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** @param {string | undefined} [rootOverride] @returns {Promise<Config | null>} */
|
|
229
|
+
export async function readConfig(rootOverride) {
|
|
230
|
+
const root = rootOverride === undefined ? stateRoot() : resolve(rootOverride);
|
|
231
|
+
await assertSafePath(root);
|
|
232
|
+
const path = join(root, 'config.json');
|
|
233
|
+
try {
|
|
234
|
+
const rootEntry = await lstat(root);
|
|
235
|
+
if (
|
|
236
|
+
!rootEntry.isDirectory() ||
|
|
237
|
+
(typeof process.getuid === 'function' && rootEntry.uid !== process.getuid()) ||
|
|
238
|
+
(rootEntry.mode & 0o077) !== 0
|
|
239
|
+
)
|
|
240
|
+
throw new Error('state_directory_permissions_invalid');
|
|
241
|
+
const entry = await lstat(path);
|
|
242
|
+
if (
|
|
243
|
+
!entry.isFile() ||
|
|
244
|
+
entry.isSymbolicLink() ||
|
|
245
|
+
(typeof process.getuid === 'function' && entry.uid !== process.getuid()) ||
|
|
246
|
+
(entry.mode & 0o077) !== 0
|
|
247
|
+
)
|
|
248
|
+
throw new Error('config_permissions_invalid');
|
|
249
|
+
if (entry.size < 1 || entry.size > 256 * 1024) throw new Error('config_size_invalid');
|
|
250
|
+
await assertSafePath(path, false);
|
|
251
|
+
let value;
|
|
252
|
+
try {
|
|
253
|
+
value = JSON.parse(await readFile(path, 'utf8'));
|
|
254
|
+
} catch (error) {
|
|
255
|
+
if (isNodeError(error) && error.code === 'ENOENT') return null;
|
|
256
|
+
throw new Error('config_json_invalid');
|
|
257
|
+
}
|
|
258
|
+
if (!isConfig(value)) throw new Error('config_schema_invalid');
|
|
259
|
+
if ((await tokenDigest(value.token)) !== value.tokenSha256)
|
|
260
|
+
throw new Error('config_token_digest_invalid');
|
|
261
|
+
return /** @type {Config} */ (value);
|
|
262
|
+
} catch (error) {
|
|
263
|
+
if (isNodeError(error) && error.code === 'ENOENT') return null;
|
|
264
|
+
throw error;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Replace one owned JSON document. The existing document is left intact if
|
|
270
|
+
* serialization or the temporary write fails.
|
|
271
|
+
* @param {string} path
|
|
272
|
+
* @param {unknown} value
|
|
273
|
+
*/
|
|
274
|
+
export async function atomicJson(path, value) {
|
|
275
|
+
await assertSafePath(dirname(path), false);
|
|
276
|
+
await assertSafePath(path);
|
|
277
|
+
const temp = `${path}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
278
|
+
try {
|
|
279
|
+
await writeFile(temp, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600, flag: 'wx' });
|
|
280
|
+
await chmod(temp, 0o600);
|
|
281
|
+
await rename(temp, path);
|
|
282
|
+
} catch (error) {
|
|
283
|
+
await unlink(temp).catch(() => undefined);
|
|
284
|
+
throw error;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** @param {string} token */
|
|
289
|
+
export async function tokenDigest(token) {
|
|
290
|
+
return createHash('sha256').update(token, 'utf8').digest('hex');
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** @param {{endpoint: string, token: string, telemetry?: {enabled: boolean, disclosedAt?: string | null}}} config */
|
|
294
|
+
export function telemetryConfigBinding(config) {
|
|
295
|
+
if (
|
|
296
|
+
!isRecord(config) ||
|
|
297
|
+
typeof config.endpoint !== 'string' ||
|
|
298
|
+
typeof config.token !== 'string' ||
|
|
299
|
+
!isRecord(config.telemetry) ||
|
|
300
|
+
config.telemetry.enabled !== true ||
|
|
301
|
+
typeof config.telemetry.disclosedAt !== 'string'
|
|
302
|
+
)
|
|
303
|
+
throw new Error('telemetry_state_mismatch');
|
|
304
|
+
let endpoint;
|
|
305
|
+
try {
|
|
306
|
+
endpoint = new URL(config.endpoint).toString();
|
|
307
|
+
} catch {
|
|
308
|
+
throw new Error('telemetry_state_mismatch');
|
|
309
|
+
}
|
|
310
|
+
return createHash('sha256')
|
|
311
|
+
.update(`${endpoint}\u0000${config.token}\u0000${config.telemetry.disclosedAt}`, 'utf8')
|
|
312
|
+
.digest('hex');
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** @param {{endpoint: string, token: string, claudeObservation?: {shadowEnabled?: boolean, diagnosticsEnabled?: boolean, disclosedAt?: string | null}}} config */
|
|
316
|
+
export function claudeObservationConfigBinding(config) {
|
|
317
|
+
if (
|
|
318
|
+
!isRecord(config) ||
|
|
319
|
+
typeof config.endpoint !== 'string' ||
|
|
320
|
+
typeof config.token !== 'string' ||
|
|
321
|
+
!isRecord(config.claudeObservation) ||
|
|
322
|
+
typeof config.claudeObservation.disclosedAt !== 'string'
|
|
323
|
+
)
|
|
324
|
+
throw new Error('claude_observation_state_mismatch');
|
|
325
|
+
let endpoint;
|
|
326
|
+
try {
|
|
327
|
+
endpoint = new URL(config.endpoint).toString();
|
|
328
|
+
} catch {
|
|
329
|
+
throw new Error('claude_observation_state_mismatch');
|
|
330
|
+
}
|
|
331
|
+
return createHash('sha256')
|
|
332
|
+
.update(`${endpoint}\u0000${config.token}\u0000${config.claudeObservation.disclosedAt}`, 'utf8')
|
|
333
|
+
.digest('hex');
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Bind launcher-managed Claude checkpoints to the authenticated account and
|
|
338
|
+
* disclosure, independently of shadow/diagnostics opt-ins.
|
|
339
|
+
* @param {{endpoint: string, token: string, disclosedAt?: string}} config
|
|
340
|
+
*/
|
|
341
|
+
export function claudeCheckpointConfigBinding(config) {
|
|
342
|
+
if (
|
|
343
|
+
!isRecord(config) ||
|
|
344
|
+
typeof config.endpoint !== 'string' ||
|
|
345
|
+
typeof config.token !== 'string' ||
|
|
346
|
+
typeof config.disclosedAt !== 'string'
|
|
347
|
+
)
|
|
348
|
+
throw new Error('claude_checkpoint_state_mismatch');
|
|
349
|
+
let endpoint;
|
|
350
|
+
try {
|
|
351
|
+
endpoint = new URL(config.endpoint).toString();
|
|
352
|
+
} catch {
|
|
353
|
+
throw new Error('claude_checkpoint_state_mismatch');
|
|
354
|
+
}
|
|
355
|
+
return createHash('sha256')
|
|
356
|
+
.update(`${endpoint}\u0000${config.token}\u0000${config.disclosedAt}`, 'utf8')
|
|
357
|
+
.digest('hex');
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** @param {string} path */
|
|
361
|
+
export async function readPrivateToken(path) {
|
|
362
|
+
const resolved = resolve(path);
|
|
363
|
+
await assertSafePath(resolved, false);
|
|
364
|
+
const entry = await stat(resolved);
|
|
365
|
+
if (
|
|
366
|
+
!entry.isFile() ||
|
|
367
|
+
(typeof process.getuid === 'function' && entry.uid !== process.getuid()) ||
|
|
368
|
+
(entry.mode & 0o077) !== 0
|
|
369
|
+
)
|
|
370
|
+
throw new Error('token_file_permissions_invalid');
|
|
371
|
+
if (entry.size < 1 || entry.size > 16 * 1024) throw new Error('token_file_size_invalid');
|
|
372
|
+
return (await readFile(resolved, 'utf8')).trim();
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
export async function removeOwnedSetup() {
|
|
376
|
+
const root = stateRoot();
|
|
377
|
+
await assertSafePath(root);
|
|
378
|
+
const path = join(root, 'config.json');
|
|
379
|
+
try {
|
|
380
|
+
await assertSafePath(path, false);
|
|
381
|
+
} catch (error) {
|
|
382
|
+
if (isNodeError(error) && error.code === 'ENOENT') return;
|
|
383
|
+
if (error instanceof Error && error.message === 'state_path_missing') return;
|
|
384
|
+
throw error;
|
|
385
|
+
}
|
|
386
|
+
const entry = await lstat(path);
|
|
387
|
+
if (
|
|
388
|
+
!entry.isFile() ||
|
|
389
|
+
(typeof process.getuid === 'function' && entry.uid !== process.getuid()) ||
|
|
390
|
+
(entry.mode & 0o077) !== 0 ||
|
|
391
|
+
entry.size > 256 * 1024
|
|
392
|
+
)
|
|
393
|
+
throw new Error('foreign_config_preserved');
|
|
394
|
+
let value;
|
|
395
|
+
try {
|
|
396
|
+
value = JSON.parse(await readFile(path, 'utf8'));
|
|
397
|
+
} catch {
|
|
398
|
+
throw new Error('foreign_config_preserved');
|
|
399
|
+
}
|
|
400
|
+
if (
|
|
401
|
+
!isConfig(value) ||
|
|
402
|
+
typeof value.packageVersion !== 'string' ||
|
|
403
|
+
!/^\d+\.\d+\.\d+-beta\./u.test(value.packageVersion) ||
|
|
404
|
+
(await tokenDigest(value.token)) !== value.tokenSha256
|
|
405
|
+
)
|
|
406
|
+
throw new Error('foreign_config_preserved');
|
|
407
|
+
await rm(path, { force: true });
|
|
408
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { readConfig, telemetryConfigBinding } from './state.mjs';
|
|
2
|
+
import { reportContextTelemetry } from './mcp.mjs';
|
|
3
|
+
// @ts-expect-error This validator is generated into dist from the shared TypeScript contract.
|
|
4
|
+
import { parseContextTelemetryReport } from './context-telemetry.mjs';
|
|
5
|
+
import { resolve } from 'node:path';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
|
|
8
|
+
export const MAX_STDIN_BYTES = 8 * 1024;
|
|
9
|
+
const ERROR_CODES = new Set([
|
|
10
|
+
'telemetry_input_too_large',
|
|
11
|
+
'telemetry_input_invalid',
|
|
12
|
+
'telemetry_consent_required',
|
|
13
|
+
'telemetry_state_invalid',
|
|
14
|
+
'telemetry_state_mismatch',
|
|
15
|
+
'telemetry_state_changed',
|
|
16
|
+
'telemetry_report_failed',
|
|
17
|
+
]);
|
|
18
|
+
|
|
19
|
+
/** @param {unknown} error */
|
|
20
|
+
function fixedErrorCode(error) {
|
|
21
|
+
const value = error instanceof Error ? error.message : '';
|
|
22
|
+
return ERROR_CODES.has(value) ? value : 'telemetry_report_failed';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** @param {unknown[]} argv */
|
|
26
|
+
function parseStateDir(argv) {
|
|
27
|
+
if (
|
|
28
|
+
argv.length !== 4 ||
|
|
29
|
+
argv[0] !== '--state-dir' ||
|
|
30
|
+
typeof argv[1] !== 'string' ||
|
|
31
|
+
argv[1].length === 0 ||
|
|
32
|
+
!argv[1].startsWith('/') ||
|
|
33
|
+
Buffer.byteLength(argv[1], 'utf8') > 4096 ||
|
|
34
|
+
argv[2] !== '--config-digest' ||
|
|
35
|
+
typeof argv[3] !== 'string' ||
|
|
36
|
+
!/^[0-9a-f]{64}$/u.test(argv[3])
|
|
37
|
+
)
|
|
38
|
+
throw new Error('telemetry_state_invalid');
|
|
39
|
+
return { stateDir: argv[1], configDigest: argv[3] };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** @param {unknown} raw @param {Date} [now] */
|
|
43
|
+
export function parseTelemetryInput(raw, now = new Date()) {
|
|
44
|
+
if (typeof raw !== 'string' || Buffer.byteLength(raw, 'utf8') > MAX_STDIN_BYTES)
|
|
45
|
+
throw new Error('telemetry_input_too_large');
|
|
46
|
+
let value;
|
|
47
|
+
try {
|
|
48
|
+
value = JSON.parse(raw);
|
|
49
|
+
} catch {
|
|
50
|
+
throw new Error('telemetry_input_invalid');
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
return parseContextTelemetryReport(value, now);
|
|
54
|
+
} catch {
|
|
55
|
+
throw new Error('telemetry_input_invalid');
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @param {string} stateDir
|
|
61
|
+
* @param {string} configDigest
|
|
62
|
+
* @param {Record<string, unknown>} report
|
|
63
|
+
* @param {(endpoint: string, token: string, report: Record<string, unknown>, deadlineMs: number) => Promise<{accepted: true, duplicate: boolean}>} [transport]
|
|
64
|
+
*/
|
|
65
|
+
export async function executeTelemetryReport(
|
|
66
|
+
stateDir,
|
|
67
|
+
configDigest,
|
|
68
|
+
report,
|
|
69
|
+
transport = reportContextTelemetry,
|
|
70
|
+
) {
|
|
71
|
+
const previousStateDir = process.env.AIBRAIN_STATE_DIR;
|
|
72
|
+
process.env.AIBRAIN_STATE_DIR = stateDir;
|
|
73
|
+
try {
|
|
74
|
+
const config = await readConfig();
|
|
75
|
+
if (
|
|
76
|
+
config === null ||
|
|
77
|
+
config.telemetry?.enabled !== true ||
|
|
78
|
+
typeof config.telemetry.disclosedAt !== 'string'
|
|
79
|
+
)
|
|
80
|
+
throw new Error('telemetry_consent_required');
|
|
81
|
+
let currentDigest;
|
|
82
|
+
try {
|
|
83
|
+
currentDigest = telemetryConfigBinding(config);
|
|
84
|
+
} catch {
|
|
85
|
+
throw new Error('telemetry_state_mismatch');
|
|
86
|
+
}
|
|
87
|
+
if (currentDigest !== configDigest) throw new Error('telemetry_state_changed');
|
|
88
|
+
return await transport(config.endpoint, config.token, report, 8_000);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
if (error instanceof Error && ERROR_CODES.has(error.message)) throw error;
|
|
91
|
+
throw new Error('telemetry_report_failed');
|
|
92
|
+
} finally {
|
|
93
|
+
if (previousStateDir === undefined) delete process.env.AIBRAIN_STATE_DIR;
|
|
94
|
+
else process.env.AIBRAIN_STATE_DIR = previousStateDir;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function readStdin() {
|
|
99
|
+
const chunks = [];
|
|
100
|
+
let total = 0;
|
|
101
|
+
for await (const chunk of process.stdin) {
|
|
102
|
+
const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
|
|
103
|
+
total += bytes.byteLength;
|
|
104
|
+
if (total > MAX_STDIN_BYTES) throw new Error('telemetry_input_too_large');
|
|
105
|
+
chunks.push(bytes);
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
return new TextDecoder('utf-8', { fatal: true }).decode(Buffer.concat(chunks));
|
|
109
|
+
} catch {
|
|
110
|
+
throw new Error('telemetry_input_invalid');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** @param {string[]} [argv] */
|
|
115
|
+
export async function main(argv = process.argv.slice(2)) {
|
|
116
|
+
try {
|
|
117
|
+
const { stateDir, configDigest } = parseStateDir(argv);
|
|
118
|
+
const report = parseTelemetryInput(await readStdin());
|
|
119
|
+
const result = await executeTelemetryReport(stateDir, configDigest, report);
|
|
120
|
+
process.stdout.write(
|
|
121
|
+
`${JSON.stringify({ schema_version: 1, accepted: true, duplicate: result.duplicate })}\n`,
|
|
122
|
+
);
|
|
123
|
+
return 0;
|
|
124
|
+
} catch (error) {
|
|
125
|
+
process.stdout.write(
|
|
126
|
+
`${JSON.stringify({ schema_version: 1, accepted: false, error_code: fixedErrorCode(error) })}\n`,
|
|
127
|
+
);
|
|
128
|
+
return 1;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (process.argv[1] !== undefined && fileURLToPath(import.meta.url) === resolve(process.argv[1]))
|
|
133
|
+
process.exitCode = await main();
|
package/package.json
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aisac/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.4.0-beta.2",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "AISAC client for Claude Code and Codex: AI Brain memory, context management and AISAC Engineering",
|
|
6
|
+
"license": "UNLICENSED",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"bin": {
|
|
9
|
+
"aisac": "./dist/cli.mjs",
|
|
10
|
+
"aibrain": "./dist/cli.mjs"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=22"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"ws": "8.22.0"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "node build.mjs",
|
|
23
|
+
"typecheck": "tsc --project tsconfig.json",
|
|
24
|
+
"test": "npm run build && node --test test/*.test.mjs"
|
|
25
|
+
}
|
|
26
|
+
}
|