@aisac/cli 0.4.0-beta.1 → 0.4.0-beta.3

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.
@@ -47,12 +47,12 @@ export function engineeringLicenseUrl(endpoint) {
47
47
  }
48
48
 
49
49
  /** @param {unknown} value @returns {value is Record<string, unknown>} */
50
- function isRecord(value) {
50
+ export function isRecord(value) {
51
51
  return typeof value === 'object' && value !== null && !Array.isArray(value);
52
52
  }
53
53
 
54
- /** @param {unknown} value */
55
- function isPurchaseUrl(value) {
54
+ /** @param {unknown} value @returns {value is string} */
55
+ export function isPurchaseUrl(value) {
56
56
  if (typeof value !== 'string' || value.length > MAX_URL_LENGTH) return false;
57
57
  try {
58
58
  const url = new URL(value);
@@ -97,10 +97,14 @@ export function parseLicenseBody(text) {
97
97
  };
98
98
  }
99
99
 
100
- /** @param {Response} response */
101
- async function boundedText(response) {
100
+ /**
101
+ * Read a response body as UTF-8, refusing anything above `limit` bytes.
102
+ * @param {Response} response
103
+ * @param {number} [limit]
104
+ */
105
+ export async function readBoundedText(response, limit = MAX_LICENSE_BODY_BYTES) {
102
106
  const declared = Number(response.headers.get('content-length') ?? '0');
103
- if (declared > MAX_LICENSE_BODY_BYTES) throw new Error('license_body_too_large');
107
+ if (declared > limit) throw new Error('body_too_large');
104
108
  if (response.body === null) return '';
105
109
  const reader = response.body.getReader();
106
110
  /** @type {Uint8Array[]} */
@@ -110,9 +114,9 @@ async function boundedText(response) {
110
114
  const { done, value } = await reader.read();
111
115
  if (done) break;
112
116
  size += value.byteLength;
113
- if (size > MAX_LICENSE_BODY_BYTES) {
117
+ if (size > limit) {
114
118
  await reader.cancel().catch(() => undefined);
115
- throw new Error('license_body_too_large');
119
+ throw new Error('body_too_large');
116
120
  }
117
121
  chunks.push(value);
118
122
  }
@@ -120,12 +124,12 @@ async function boundedText(response) {
120
124
  }
121
125
 
122
126
  /** @param {unknown} error */
123
- function isTimeout(error) {
127
+ export function isTimeout(error) {
124
128
  return error instanceof Error && (error.name === 'TimeoutError' || error.name === 'AbortError');
125
129
  }
126
130
 
127
131
  /** @param {number} status */
128
- function unavailableReasonFor(status) {
132
+ export function unavailableReasonFor(status) {
129
133
  return (
130
134
  UNAVAILABLE_BY_STATUS.get(status) ??
131
135
  (status >= SERVER_ERROR_STATUS ? 'server_error' : 'unexpected_status')
@@ -165,7 +169,7 @@ export async function checkEngineeringLicense(endpoint, token, options = {}) {
165
169
  }
166
170
  let text;
167
171
  try {
168
- text = await boundedText(response);
172
+ text = await readBoundedText(response);
169
173
  } catch (error) {
170
174
  return unavailableLicense(isTimeout(error) ? 'timeout' : 'invalid_response');
171
175
  }
package/dist/launch.mjs CHANGED
@@ -13,6 +13,16 @@ import {
13
13
  telemetryConfigBinding,
14
14
  } from './state.mjs';
15
15
  import { codexPolicyJson, makePolicy } from './policy.mjs';
16
+ import {
17
+ prepareEngineeringPack,
18
+ sweepStaleLaunches,
19
+ writeLaunchMarker,
20
+ } from './engineering-pack.mjs';
21
+ import {
22
+ CODEX_ENGINEERING_ENV,
23
+ CODEX_ENGINEERING_SKILLS_ROOT_ENV,
24
+ supportsCodexEngineering,
25
+ } from './codex-engineering.mjs';
16
26
  import {
17
27
  CODEX_LAUNCH_ENVELOPE,
18
28
  CODEX_MONITOR_CWD,
@@ -127,6 +137,8 @@ function childEnvironment(root) {
127
137
  delete environment.CODEX_CONTEXT_COMPACT_AT;
128
138
  delete environment[CODEX_LAUNCH_ENVELOPE];
129
139
  delete environment[CODEX_MONITOR_CWD];
140
+ delete environment[CODEX_ENGINEERING_ENV];
141
+ delete environment[CODEX_ENGINEERING_SKILLS_ROOT_ENV];
130
142
  environment.AIBRAIN_STATE_DIR = resolve(root);
131
143
  return environment;
132
144
  }
@@ -361,14 +373,26 @@ async function assertClaudeCheckpointHelper(packageRoot) {
361
373
  return helperPath;
362
374
  }
363
375
 
364
- /** @param {string} root @param {string} prefix */
376
+ /**
377
+ * A fresh private directory for one launch. Directories left by launches whose
378
+ * processes are gone are swept first; this one records its launcher.
379
+ * @param {string} root @param {string} prefix
380
+ */
365
381
  async function launchDirectory(root, prefix) {
382
+ await sweepStaleLaunches(root);
366
383
  const parent = await privateDirectory(join(root, 'runtime'));
367
384
  const directory = await mkdtemp(join(parent, `${prefix}-`));
368
385
  await chmod(directory, 0o700);
386
+ await writeLaunchMarker(directory, { launcherPid: process.pid, childPid: null });
369
387
  return directory;
370
388
  }
371
389
 
390
+ /** @param {string} directory @param {number | undefined} childPid */
391
+ async function recordChild(directory, childPid) {
392
+ if (childPid === undefined) return;
393
+ await writeLaunchMarker(directory, { launcherPid: process.pid, childPid }).catch(() => undefined);
394
+ }
395
+
372
396
  /** @param {import('node:child_process').ChildProcess} child @param {string} directory */
373
397
  function waitForChild(child, directory) {
374
398
  return new Promise((resolvePromise) => {
@@ -401,8 +425,19 @@ function waitForChild(child, directory) {
401
425
  });
402
426
  }
403
427
 
404
- /** @param {LaunchConfig} config @param {string} root @param {string[]} args */
405
- export async function launchClaude(config, root, args) {
428
+ /**
429
+ * @typedef {object} LaunchDependencies
430
+ * @property {typeof prepareEngineeringPack} [preparePack] AISAC Engineering pack loader (E4b)
431
+ */
432
+
433
+ /**
434
+ * @param {LaunchConfig} config
435
+ * @param {string} root
436
+ * @param {string[]} args
437
+ * @param {LaunchDependencies} [dependencies]
438
+ */
439
+ export async function launchClaude(config, root, args, dependencies = {}) {
440
+ const preparePack = dependencies.preparePack ?? prepareEngineeringPack;
406
441
  const validated = assertConfig(config);
407
442
  const launchArgs = await parseClaudeLaunchArgs(args, process.cwd());
408
443
  assertClaudeArgs(launchArgs.args);
@@ -534,6 +569,15 @@ export async function launchClaude(config, root, args) {
534
569
  await rm(directory, { recursive: true, force: true });
535
570
  throw error;
536
571
  }
572
+ // The pack never blocks the launch: every failure starts Claude without it.
573
+ const engineering = await preparePack({
574
+ endpoint: validated.endpoint,
575
+ token: validated.token,
576
+ root,
577
+ directory,
578
+ packageRoot: validated.packageRoot,
579
+ }).catch(() => ({ pluginRoot: null, line: null }));
580
+ if (engineering.line !== null) process.stderr.write(`${engineering.line}\n`);
537
581
  const environment = childEnvironment(root);
538
582
  environment.CLAUDE_CODE_ENABLE_FUNCTION_HOOKS = '1';
539
583
  /** @type {import('node:child_process').ChildProcess} */
@@ -550,6 +594,7 @@ export async function launchClaude(config, root, args) {
550
594
  requiredAllow.join(','),
551
595
  '--plugin-dir',
552
596
  pluginRoot,
597
+ ...(engineering.pluginRoot === null ? [] : ['--plugin-dir', engineering.pluginRoot]),
553
598
  ...launchArgs.args,
554
599
  ],
555
600
  { cwd: process.cwd(), env: environment, stdio: 'inherit' },
@@ -558,11 +603,20 @@ export async function launchClaude(config, root, args) {
558
603
  await rm(directory, { recursive: true, force: true }).catch(() => undefined);
559
604
  throw error;
560
605
  }
561
- return waitForChild(child, directory);
606
+ // Listen for exit before any await, so a child that exits at once is not missed.
607
+ const finished = waitForChild(child, directory);
608
+ await recordChild(directory, child.pid);
609
+ return finished;
562
610
  }
563
611
 
564
- /** @param {LaunchConfig} config @param {string} root @param {string[]} args */
565
- export async function launchCodex(config, root, args) {
612
+ /**
613
+ * @param {LaunchConfig} config
614
+ * @param {string} root
615
+ * @param {string[]} args
616
+ * @param {LaunchDependencies} [dependencies]
617
+ */
618
+ export async function launchCodex(config, root, args, dependencies = {}) {
619
+ const preparePack = dependencies.preparePack ?? prepareEngineeringPack;
566
620
  const validated = assertConfig(config);
567
621
  assertCodexArgs(args);
568
622
  const captureLockPlatform = captureLockPlatformCheck();
@@ -613,6 +667,19 @@ export async function launchCodex(config, root, args) {
613
667
  await rm(directory, { recursive: true, force: true });
614
668
  throw error;
615
669
  }
670
+ // The pack never blocks the launch: every failure starts Codex without it.
671
+ const engineering = await preparePack({
672
+ endpoint: validated.endpoint,
673
+ token: validated.token,
674
+ root,
675
+ directory,
676
+ packageRoot: validated.packageRoot,
677
+ client: 'codex',
678
+ hostSupported: supportsCodexEngineering(compatibility.version),
679
+ }).catch(() => ({ pluginRoot: null, codex: null, line: null }));
680
+ if (engineering.line !== null) process.stderr.write(`${engineering.line}\n`);
681
+ if (engineering.codex != null)
682
+ environment[CODEX_ENGINEERING_ENV] = JSON.stringify(engineering.codex);
616
683
  environment.AI_BRAIN_MCP_SERVER = MCP_ALIAS;
617
684
  environment.CODEX_BIN = executable;
618
685
  environment.CODEX_MONITOR_COMMAND = monitor;
@@ -640,7 +707,10 @@ export async function launchCodex(config, root, args) {
640
707
  env: environment,
641
708
  stdio: 'inherit',
642
709
  });
643
- return waitForChild(child, directory);
710
+ // Listen for exit before any await, so a child that exits at once is not missed.
711
+ const finished = waitForChild(child, directory);
712
+ await recordChild(directory, child.pid);
713
+ return finished;
644
714
  }
645
715
 
646
716
  export const launchInternals = { MCP_ALIAS, PLUGIN_NAME, PLUGIN_INLINE_NAME };
package/dist/monitor.mjs CHANGED
@@ -12,6 +12,12 @@ import {
12
12
  ownedAppServerConfigArgs,
13
13
  parseCodexLaunchEnvelope,
14
14
  } from './codex-permissions.mjs';
15
+ import {
16
+ CODEX_ENGINEERING_ENV,
17
+ CODEX_ENGINEERING_SKILLS_ROOT_ENV,
18
+ codexEngineeringConfigArgs,
19
+ parseCodexEngineering,
20
+ } from './codex-engineering.mjs';
15
21
 
16
22
  const root = dirname(fileURLToPath(import.meta.url));
17
23
  const endpoint = process.env.AIBRAIN_MCP_URL?.trim();
@@ -32,6 +38,17 @@ if (parsed.search) throw new Error('aibrain_monitor_endpoint_query');
32
38
  const canonical = join(root, 'assets', 'codex-context-compaction.mjs');
33
39
  await access(canonical, constants.R_OK);
34
40
  const launchEnvelope = parseCodexLaunchEnvelope(process.env[CODEX_LAUNCH_ENVELOPE]);
41
+ // AISAC Engineering (E4c): a malformed value starts the session without the pack.
42
+ let engineering = null;
43
+ try {
44
+ const stateDir = process.env.AIBRAIN_STATE_DIR;
45
+ engineering = parseCodexEngineering(
46
+ process.env[CODEX_ENGINEERING_ENV],
47
+ stateDir === undefined ? {} : { stateDir },
48
+ );
49
+ } catch {
50
+ engineering = null;
51
+ }
35
52
  const policyText = process.env.CODEX_CONTEXT_POLICY_JSON;
36
53
  let policy;
37
54
  if (policyText === undefined) {
@@ -89,6 +106,9 @@ const env = {
89
106
  };
90
107
  delete env[CODEX_LAUNCH_ENVELOPE];
91
108
  delete env[CODEX_APP_SERVER_PROFILE];
109
+ delete env[CODEX_ENGINEERING_ENV];
110
+ delete env[CODEX_ENGINEERING_SKILLS_ROOT_ENV];
111
+ if (engineering !== null) env[CODEX_ENGINEERING_SKILLS_ROOT_ENV] = engineering.skillsRoot;
92
112
  if (launchEnvelope?.profile !== undefined) env[CODEX_APP_SERVER_PROFILE] = launchEnvelope.profile;
93
113
  if (policy !== null) env.CODEX_CONTEXT_POLICY_JSON = JSON.stringify(policy);
94
114
  else delete env.CODEX_CONTEXT_POLICY_JSON;
@@ -109,6 +129,7 @@ const child = spawn(
109
129
  '-c',
110
130
  'mcp_servers.aibrain_context.bearer_token_env_var="CODEX_RECOVERY_MCP_TOKEN"',
111
131
  ...(launchEnvelope === null ? [] : ownedAppServerConfigArgs(launchEnvelope)),
132
+ ...(engineering === null ? [] : codexEngineeringConfigArgs(engineering)),
112
133
  ],
113
134
  { cwd: process.cwd(), env, stdio: 'inherit' },
114
135
  );
package/dist/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@aisac/cli",
3
- "version": "0.4.0-beta.1",
3
+ "version": "0.4.0-beta.3",
4
4
  "type": "module"
5
5
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aisac/cli",
3
- "version": "0.4.0-beta.1",
3
+ "version": "0.4.0-beta.3",
4
4
  "private": false,
5
5
  "description": "AISAC client for Claude Code and Codex: AI Brain memory, context management and AISAC Engineering",
6
6
  "license": "UNLICENSED",