@aisac/cli 0.4.0-beta.1 → 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.
@@ -0,0 +1,192 @@
1
+ // @ts-check
2
+ /**
3
+ * Session configuration for the MCP catalog (spec aisac-engineering-extensions
4
+ * FR5.6, FR6, FR7): entries for Claude Code's `--mcp-config` file, and
5
+ * `-c mcp_servers.*` flags for the app-server that `aisac codex` owns. Only
6
+ * catalog ids reach this module from outside; every URL, command and argument
7
+ * comes from the catalog in this package. AISAC never reads or stores a
8
+ * third-party credential: logins live in the host's own store, and a token
9
+ * variable is only named, never read here.
10
+ */
11
+ import { MCP_CATALOG, MCP_SERVER_PREFIX, mcpById } from './extensions-catalog.mjs';
12
+ import { tomlBasicString } from './codex-engineering.mjs';
13
+
14
+ /** @typedef {import('./extensions-catalog.mjs').McpEntry} McpEntry */
15
+ /** @typedef {'claude' | 'codex'} McpClient */
16
+ /**
17
+ * @typedef {object} SessionServers
18
+ * @property {McpEntry[]} ready Servers that join the session.
19
+ * @property {{ id: string, variable: string }[]} missingEnv Skipped: the token variable is unset.
20
+ */
21
+
22
+ /** The launcher hands the Codex monitor the ready ids in this variable. */
23
+ export const CODEX_MCP_ENV = 'AISAC_EXTENSIONS_CODEX_MCP';
24
+ const STDIO_STARTUP_TIMEOUT_SEC = 60;
25
+ const MAX_IDS = 16;
26
+ const MAX_IDS_BYTES = 1024;
27
+
28
+ /** @param {string} id */
29
+ export function serverName(id) {
30
+ return `${MCP_SERVER_PREFIX}${id}`;
31
+ }
32
+
33
+ /**
34
+ * Catalog entries for these ids that support the client. A token server whose
35
+ * variable is unset is skipped with a reason instead of failing in the host.
36
+ * @param {readonly string[]} ids
37
+ * @param {McpClient} client
38
+ * @param {NodeJS.ProcessEnv} environment
39
+ * @returns {SessionServers}
40
+ */
41
+ export function sessionServers(ids, client, environment) {
42
+ /** @type {SessionServers} */
43
+ const result = { ready: [], missingEnv: [] };
44
+ for (const id of new Set(ids)) {
45
+ const entry = mcpById(id);
46
+ if (entry === undefined || !entry.clients.includes(client)) continue;
47
+ const variable = entry.transport.type === 'http' ? entry.transport.bearerEnv : undefined;
48
+ if (variable !== undefined && !environment[variable]?.trim()) {
49
+ result.missingEnv.push({ id, variable });
50
+ continue;
51
+ }
52
+ result.ready.push(entry);
53
+ }
54
+ return result;
55
+ }
56
+
57
+ /**
58
+ * `--mcp-config` entries. A token header uses Claude Code's `${VAR}` expansion,
59
+ * so the token never reaches the session file.
60
+ * @param {readonly McpEntry[]} entries
61
+ * @returns {Record<string, Record<string, unknown>>}
62
+ */
63
+ export function claudeMcpEntries(entries) {
64
+ /** @type {Record<string, Record<string, unknown>>} */
65
+ const servers = {};
66
+ for (const entry of entries) {
67
+ const { transport } = entry;
68
+ servers[serverName(entry.id)] =
69
+ transport.type === 'stdio'
70
+ ? { type: 'stdio', command: transport.command, args: [...transport.args] }
71
+ : {
72
+ type: 'http',
73
+ url: transport.url,
74
+ ...(transport.bearerEnv === undefined
75
+ ? {}
76
+ : { headers: { Authorization: `Bearer \${${transport.bearerEnv}}` } }),
77
+ };
78
+ }
79
+ return servers;
80
+ }
81
+
82
+ /**
83
+ * Claude Code deny rules for the tools that spend money (FR7.2). They cover every
84
+ * catalog server, loaded or not, so the settings file can be written before the
85
+ * license is confirmed; a rule for a server that is not loaded matches nothing.
86
+ * Claude evaluates deny rules before allow rules, in every permission mode, and a
87
+ * deny rule removes the tool from the model's context (measured M1, Claude 2.1.318).
88
+ * @param {readonly McpEntry[]} [entries]
89
+ * @returns {string[]}
90
+ */
91
+ export function claudeDenyRules(entries = MCP_CATALOG) {
92
+ return entries.flatMap((entry) => {
93
+ const prefix = `mcp__${serverName(entry.id)}__`;
94
+ return [
95
+ ...(entry.blockedTools ?? []).map((tool) => `${prefix}${tool}`),
96
+ ...(entry.blockedToolPrefixes ?? []).map((start) => `${prefix}${start}*`),
97
+ ];
98
+ });
99
+ }
100
+
101
+ /**
102
+ * Session-only `-c` overrides for the owned Codex app-server. A server with
103
+ * blocked tools gets `disabled_tools` (FR7.2; measured M2, Codex 0.164.0).
104
+ * @param {readonly McpEntry[]} entries
105
+ * @returns {string[]}
106
+ */
107
+ export function codexMcpConfigArgs(entries) {
108
+ return entries.flatMap((entry) => {
109
+ const key = `mcp_servers.${serverName(entry.id)}`;
110
+ const { transport } = entry;
111
+ const blocked = entry.blockedTools ?? [];
112
+ const disabled =
113
+ blocked.length === 0
114
+ ? []
115
+ : ['-c', `${key}.disabled_tools=[${blocked.map(tomlBasicString).join(',')}]`];
116
+ if (transport.type === 'stdio')
117
+ return [
118
+ '-c',
119
+ `${key}.command=${tomlBasicString(transport.command)}`,
120
+ '-c',
121
+ `${key}.args=[${transport.args.map(tomlBasicString).join(',')}]`,
122
+ '-c',
123
+ `${key}.startup_timeout_sec=${STDIO_STARTUP_TIMEOUT_SEC}`,
124
+ ...disabled,
125
+ ];
126
+ return [
127
+ '-c',
128
+ `${key}.url=${tomlBasicString(transport.url)}`,
129
+ ...(transport.bearerEnv === undefined
130
+ ? []
131
+ : ['-c', `${key}.bearer_token_env_var=${tomlBasicString(transport.bearerEnv)}`]),
132
+ ...disabled,
133
+ ];
134
+ });
135
+ }
136
+
137
+ /**
138
+ * The monitor's copy of the ready ids: a JSON array of catalog ids for Codex.
139
+ * Anything else throws, and the monitor then starts without catalog servers.
140
+ * @param {string | undefined} raw
141
+ * @returns {McpEntry[]}
142
+ */
143
+ export function parseCodexMcp(raw) {
144
+ if (raw === undefined || raw === '') return [];
145
+ if (Buffer.byteLength(raw, 'utf8') > MAX_IDS_BYTES) throw new Error('codex_mcp_too_large');
146
+ const value = JSON.parse(raw);
147
+ if (!Array.isArray(value) || value.length > MAX_IDS) throw new Error('codex_mcp_invalid');
148
+ return value.map((id) => {
149
+ const entry = typeof id === 'string' ? mcpById(id) : undefined;
150
+ if (entry === undefined || !entry.clients.includes('codex'))
151
+ throw new Error('codex_mcp_invalid');
152
+ return entry;
153
+ });
154
+ }
155
+
156
+ /**
157
+ * Arguments for `codex` that log in to one catalog server with the same session
158
+ * flags `aisac codex` passes, so the next session finds the stored login.
159
+ * Spike Q5 (2026-11-03, Codex 0.164.0) measured this path end to end.
160
+ * @param {McpEntry} entry
161
+ */
162
+ export function codexLoginArgs(entry) {
163
+ return [...codexMcpConfigArgs([entry]), 'mcp', 'login', serverName(entry.id)];
164
+ }
165
+
166
+ /** @param {McpEntry} entry */
167
+ export function supportsHostLogin(entry) {
168
+ return entry.login === 'host-oauth' || entry.login === 'optional-oauth';
169
+ }
170
+
171
+ /**
172
+ * Launch lines: counts and names only, never a value.
173
+ * @param {SessionServers} servers
174
+ * @param {McpClient} client
175
+ * @returns {string[]}
176
+ */
177
+ export function mcpLaunchLines(servers, client) {
178
+ /** @type {string[]} */
179
+ const lines = [];
180
+ const count = servers.ready.length;
181
+ if (count > 0) {
182
+ const login = client === 'claude' ? 'log in with /mcp' : 'log in with `aisac mcp login <id>`';
183
+ lines.push(
184
+ `AISAC Engineering: ${count} MCP server${count === 1 ? '' : 's'} on (${login}; aisac mcp to change them).`,
185
+ );
186
+ }
187
+ for (const skipped of servers.missingEnv)
188
+ lines.push(
189
+ `AISAC Engineering: ${serverName(skipped.id)} skipped: set ${skipped.variable} (aisac mcp info ${skipped.id}).`,
190
+ );
191
+ return lines;
192
+ }
package/dist/monitor.mjs CHANGED
@@ -12,6 +12,13 @@ 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';
21
+ import { CODEX_MCP_ENV, codexMcpConfigArgs, parseCodexMcp } from './mcp-servers.mjs';
15
22
 
16
23
  const root = dirname(fileURLToPath(import.meta.url));
17
24
  const endpoint = process.env.AIBRAIN_MCP_URL?.trim();
@@ -32,6 +39,26 @@ if (parsed.search) throw new Error('aibrain_monitor_endpoint_query');
32
39
  const canonical = join(root, 'assets', 'codex-context-compaction.mjs');
33
40
  await access(canonical, constants.R_OK);
34
41
  const launchEnvelope = parseCodexLaunchEnvelope(process.env[CODEX_LAUNCH_ENVELOPE]);
42
+ // AISAC Engineering (E4c): a malformed value starts the session without the pack.
43
+ let engineering = null;
44
+ try {
45
+ const stateDir = process.env.AIBRAIN_STATE_DIR;
46
+ engineering = parseCodexEngineering(
47
+ process.env[CODEX_ENGINEERING_ENV],
48
+ stateDir === undefined ? {} : { stateDir },
49
+ );
50
+ } catch {
51
+ engineering = null;
52
+ }
53
+ // MCP catalog servers (E6c) follow the pack: no license, no servers. A malformed
54
+ // value starts the session without them.
55
+ /** @type {import('./mcp-servers.mjs').McpEntry[]} */
56
+ let catalogServers = [];
57
+ try {
58
+ catalogServers = engineering === null ? [] : parseCodexMcp(process.env[CODEX_MCP_ENV]);
59
+ } catch {
60
+ catalogServers = [];
61
+ }
35
62
  const policyText = process.env.CODEX_CONTEXT_POLICY_JSON;
36
63
  let policy;
37
64
  if (policyText === undefined) {
@@ -89,6 +116,10 @@ const env = {
89
116
  };
90
117
  delete env[CODEX_LAUNCH_ENVELOPE];
91
118
  delete env[CODEX_APP_SERVER_PROFILE];
119
+ delete env[CODEX_ENGINEERING_ENV];
120
+ delete env[CODEX_ENGINEERING_SKILLS_ROOT_ENV];
121
+ delete env[CODEX_MCP_ENV];
122
+ if (engineering !== null) env[CODEX_ENGINEERING_SKILLS_ROOT_ENV] = engineering.skillsRoot;
92
123
  if (launchEnvelope?.profile !== undefined) env[CODEX_APP_SERVER_PROFILE] = launchEnvelope.profile;
93
124
  if (policy !== null) env.CODEX_CONTEXT_POLICY_JSON = JSON.stringify(policy);
94
125
  else delete env.CODEX_CONTEXT_POLICY_JSON;
@@ -109,6 +140,8 @@ const child = spawn(
109
140
  '-c',
110
141
  'mcp_servers.aibrain_context.bearer_token_env_var="CODEX_RECOVERY_MCP_TOKEN"',
111
142
  ...(launchEnvelope === null ? [] : ownedAppServerConfigArgs(launchEnvelope)),
143
+ ...(engineering === null ? [] : codexEngineeringConfigArgs(engineering)),
144
+ ...codexMcpConfigArgs(catalogServers),
112
145
  ],
113
146
  { cwd: process.cwd(), env, stdio: 'inherit' },
114
147
  );
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.10",
4
4
  "type": "module"
5
5
  }
package/dist/platform.mjs CHANGED
@@ -4,18 +4,23 @@ import { accessSync, constants, existsSync, statSync } from 'node:fs';
4
4
  import { spawnSync } from 'node:child_process';
5
5
  import { delimiter, isAbsolute, join } from 'node:path';
6
6
 
7
- export function platformCheck() {
8
- if (process.platform === 'win32')
7
+ /**
8
+ * Host operating system gate shared by every command. Linux (including WSL)
9
+ * and macOS run the Claude Code path; Codex has its own gate below.
10
+ * @param {NodeJS.Platform} [platform]
11
+ */
12
+ export function platformCheck(platform = process.platform) {
13
+ if (platform === 'win32')
9
14
  return {
10
15
  ok: false,
11
16
  code: 'WINDOWS_NATIVE_UNSUPPORTED',
12
17
  message: 'Run this beta inside WSL; native Windows launch is unsupported.',
13
18
  };
14
- if (process.platform !== 'linux')
19
+ if (platform !== 'linux' && platform !== 'darwin')
15
20
  return {
16
21
  ok: false,
17
22
  code: 'PLATFORM_UNSUPPORTED',
18
- message: 'This beta supports Linux/WSL only.',
23
+ message: 'This beta supports Linux, WSL and macOS.',
19
24
  };
20
25
  const major = Number.parseInt(process.versions.node.split('.')[0] ?? '', 10);
21
26
  if (!Number.isInteger(major) || major < 22)
@@ -23,10 +28,35 @@ export function platformCheck() {
23
28
  return { ok: true };
24
29
  }
25
30
 
26
- /** @param {NodeJS.ProcessEnv} [environment] */
27
- export function captureLockPlatformCheck(environment = process.env) {
28
- const platform = platformCheck();
29
- if (!platform.ok) return platform;
31
+ /**
32
+ * Codex capture identifies lock owners through Linux procfs (boot id, PID
33
+ * namespace, process start time) and coordinates through util-linux flock.
34
+ * macOS has neither, so Codex stays Linux/WSL-only until a reviewed backend
35
+ * exists (docs/specs/aisac-macos-claude.md).
36
+ * @param {NodeJS.Platform} [platform]
37
+ */
38
+ export function codexPlatformCheck(platform = process.platform) {
39
+ const host = platformCheck(platform);
40
+ if (!host.ok) return host;
41
+ if (platform === 'darwin')
42
+ return {
43
+ ok: false,
44
+ code: 'CODEX_MACOS_UNSUPPORTED',
45
+ message:
46
+ 'aisac codex is not available on macOS yet. Use `aisac setup --client claude` and `aisac claude`, or run Codex from Linux/WSL.',
47
+ };
48
+ return { ok: true };
49
+ }
50
+
51
+ /** Clients configured when `--client` is omitted: only those this OS can run. */
52
+ export function defaultClientSelection(platform = process.platform) {
53
+ return codexPlatformCheck(platform).ok ? 'both' : 'claude';
54
+ }
55
+
56
+ /** @param {NodeJS.ProcessEnv} [environment] @param {NodeJS.Platform} [platform] */
57
+ export function captureLockPlatformCheck(environment = process.env, platform = process.platform) {
58
+ const codex = codexPlatformCheck(platform);
59
+ if (!codex.ok) return codex;
30
60
  if (resolveNative('flock', environment) === null)
31
61
  return {
32
62
  ok: false,
@@ -86,7 +116,7 @@ export function isTestedNativeVersion(client, version) {
86
116
  return /^(?:(?:claude(?:\s+code)?)\s+)?2\.1\.(?:294|295|296|299|300|301|302|303)(?:\s+\(Claude\s+Code\))?$/iu.test(
87
117
  value,
88
118
  );
89
- return /^(?:codex(?:-cli)?\s+)?0\.16(?:1\.0|2\.0|2\.1|3\.0|4\.0)$/iu.test(value);
119
+ return /^(?:codex(?:-cli)?\s+)?0\.16(?:1\.0|2\.0|2\.1|3\.0|4\.0|6\.0)$/iu.test(value);
90
120
  }
91
121
 
92
122
  const TESTED_NATIVE_VERSIONS = Object.freeze({
@@ -100,7 +130,7 @@ const TESTED_NATIVE_VERSIONS = Object.freeze({
100
130
  '2.1.302',
101
131
  '2.1.303',
102
132
  ]),
103
- codex: Object.freeze(['0.161.0', '0.162.0', '0.162.1', '0.163.0', '0.164.0']),
133
+ codex: Object.freeze(['0.161.0', '0.162.0', '0.162.1', '0.163.0', '0.164.0', '0.166.0']),
104
134
  });
105
135
 
106
136
  /** @param {'claude'|'codex'} client @param {string} version */
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.10",
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",