@commonlyai/cli 0.1.260 → 0.1.262

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commonlyai/cli",
3
- "version": "0.1.260",
3
+ "version": "0.1.262",
4
4
  "license": "Apache-2.0",
5
5
  "description": "The Commonly CLI — connect agents, manage pods, iterate fast",
6
6
  "type": "module",
@@ -324,6 +324,16 @@ const buildMcpOverrideArgs = (mcpServers, ctx = {}) => {
324
324
  && typeof value === 'string'
325
325
  && value.includes(ctx.runtimeToken);
326
326
  if (carriesRuntimeToken) {
327
+ // Codex reads these as its own credential or model route, so a value
328
+ // forwarded under one of these names would be sent to the model endpoint.
329
+ // Compared in upper case on every platform: Windows env names are
330
+ // case-insensitive, and refusing a lower-case spelling elsewhere costs nothing.
331
+ if (CODEX_OWN_AUTH_ENVS.includes(key.toUpperCase())) {
332
+ throw new Error(
333
+ `codex MCP server ${server.name} sets ${key} to the seat token; codex reads ${key} `
334
+ + 'as its own credential, so declare the token under another name',
335
+ );
336
+ }
327
337
  if (forwardedEnv[key] !== undefined && forwardedEnv[key] !== value) {
328
338
  throw new Error(
329
339
  `codex MCP servers declare conflicting token-bearing values for env var ${key}`,
@@ -431,10 +441,12 @@ const realpathOfNearest = async (path) => {
431
441
  }
432
442
  };
433
443
 
444
+ const operatorCodexHome = (ctx) => ctx.env?.CODEX_HOME
445
+ || process.env.CODEX_HOME
446
+ || join(homedir(), '.codex');
447
+
434
448
  const preparePublicCodexHome = async (ctx, childEnv) => {
435
- const operatorHome = ctx.env?.CODEX_HOME
436
- || process.env.CODEX_HOME
437
- || join(homedir(), '.codex');
449
+ const operatorHome = operatorCodexHome(ctx);
438
450
  const identity = ctx.agentName || ctx.cwd || 'anonymous';
439
451
  const identityHash = createHash('sha256').update(identity).digest('hex').slice(0, 20);
440
452
  const homesRoot = publicCodexHomesRoot(ctx);
@@ -704,6 +716,66 @@ const publicPermissionProfileFlags = (
704
716
  ];
705
717
  };
706
718
 
719
+ // F188: the host's OpenAI-compatible model route, for a public seat. That seat
720
+ // runs with `--ignore-user-config`, so config.toml's `openai_base_url` never
721
+ // loads, and codex-cli 0.162.1 does not read the OPENAI_BASE_URL env either
722
+ // (Growth's D252 walk, 11-05): a host routed at LiteLLM or another
723
+ // OpenAI-compatible endpoint sent every turn to api.openai.com and got a 401.
724
+ // The env is the declaration, as ANTHROPIC_BASE_URL is for the claude adapter;
725
+ // buildArgs passes it on as `-c openai_base_url=...`, the top-level key the
726
+ // 0.162.1 binary carries. Only a plain http(s) URL: one carrying credentials,
727
+ // a query or a fragment could put a key into argv, so it is refused rather
728
+ // than trimmed, and the refusal does not echo the value. The key itself stays
729
+ // where codex already reads it (auth.json in the public home, or the env).
730
+ export const publicModelRouteBaseUrl = (source = {}) => {
731
+ const raw = typeof source.OPENAI_BASE_URL === 'string' ? source.OPENAI_BASE_URL.trim() : '';
732
+ if (!raw) return null;
733
+ let url = null;
734
+ try { url = new URL(raw); } catch { /* refused below */ }
735
+ if (!url || !['http:', 'https:'].includes(url.protocol) || !url.hostname
736
+ || url.username || url.password || /[?#\s]/.test(raw)) {
737
+ throw new Error('codex adapter: OPENAI_BASE_URL must be a plain http(s) URL with no credentials, '
738
+ + 'query or fragment (e.g. https://litellm.example.com/v1); fix or unset it before a public seat runs.');
739
+ }
740
+ return raw;
741
+ };
742
+
743
+ // F188 guard: `openai_base_url` moves wherever the seat's credential goes, a
744
+ // ChatGPT session included (the built-in provider swaps only its default base
745
+ // for a ChatGPT login). So the route is forwarded only to a seat that signs in
746
+ // with an API key, and a ChatGPT session token is never pointed at a URL from
747
+ // the env. The public home links the operator's auth.json, so that file decides:
748
+ // its `auth_mode` alone is read (codex-cli 0.162.1 writes "apikey", "chatgpt",
749
+ // "chatgptAuthTokens", ...), the parsed object is dropped, and nothing else from
750
+ // it is kept or logged. Only "apikey" counts as a key; a file with any other
751
+ // mode, no mode, or that does not parse is treated as a login and fails closed,
752
+ // whatever OPENAI_API_KEY says, since codex prefers its stored login. With no
753
+ // auth.json, an OPENAI_API_KEY or CODEX_API_KEY in the env is the key. A
754
+ // CODEX_ACCESS_TOKEN in the env is a token codex sends in place of either, so
755
+ // it counts as a login whatever auth.json says.
756
+ const CODEX_OWN_AUTH_ENVS = ['OPENAI_API_KEY', 'CODEX_API_KEY', 'CODEX_ACCESS_TOKEN', 'OPENAI_BASE_URL'];
757
+
758
+ const CHATGPT_ROUTE_IGNORED = 'OPENAI_BASE_URL is ignored while Codex is signed in with ChatGPT; '
759
+ + 'use an API key for a custom route';
760
+
761
+ const envSet = (source, name) => typeof source[name] === 'string' && source[name].trim() !== '';
762
+
763
+ export const codexAuthKind = async (operatorHome, source = {}) => {
764
+ if (envSet(source, 'CODEX_ACCESS_TOKEN')) return 'login';
765
+ let text = null;
766
+ try {
767
+ text = await readFile(join(operatorHome, 'auth.json'), 'utf8');
768
+ } catch (err) {
769
+ if (err?.code !== 'ENOENT' && err?.code !== 'ENOTDIR') return 'login';
770
+ }
771
+ if (text === null) {
772
+ return envSet(source, 'OPENAI_API_KEY') || envSet(source, 'CODEX_API_KEY') ? 'apikey' : 'none';
773
+ }
774
+ let mode = null;
775
+ try { mode = JSON.parse(text)?.auth_mode ?? null; } catch { /* unreadable: a login */ }
776
+ return mode === 'apikey' ? 'apikey' : 'login';
777
+ };
778
+
707
779
  // Build the argv after the `codex` binary. Resume vs new turn is a
708
780
  // subcommand-level distinction in modern codex, not an option flag — keep
709
781
  // that detail isolated here so the spawn path stays linear.
@@ -718,6 +790,7 @@ const buildArgs = ({
718
790
  commitAttributionEnv = null,
719
791
  mcpReadDirectories = [],
720
792
  codexHelperReadPaths = [],
793
+ modelBaseUrl = null,
721
794
  }) => {
722
795
  const publicSandbox = publicSandboxMode !== null;
723
796
  // `--dangerously-bypass-approvals-and-sandbox` disables codex CLI's
@@ -749,6 +822,10 @@ const buildArgs = ({
749
822
  '--json',
750
823
  '--skip-git-repo-check',
751
824
  ...executionPolicy,
825
+ // F188: only a public seat drops config.toml; a private one keeps its own
826
+ // route there. In `common`, so it lands after `exec` / `exec resume <id>`,
827
+ // the only position codex parses it from.
828
+ ...(publicSandbox && modelBaseUrl ? ['-c', `openai_base_url=${toml(modelBaseUrl)}`] : []),
752
829
  ...(model ? ['--model', String(model)] : []),
753
830
  ...(effort ? ['-c', 'model_reasoning_effort=' + toml(effort)] : []),
754
831
  ...mcpFlags,
@@ -823,7 +900,7 @@ const CODEX_PROVIDER_KEY_ENVS = [
823
900
  // `ownGroup`: see runClaude in claude.js.
824
901
  const runCodex = ({
825
902
  args, cwd, env, timeoutMs, credentials = [], spawnImpl = childSpawn, onSpawn, controls = {}, ownGroup = false,
826
- turnReap = turnReapEnabled(), commandResolution = {},
903
+ turnReap = turnReapEnabled(), commandResolution = {}, confined = false,
827
904
  }) => new Promise((resolve, reject) => { // stdio: ['ignore', 'pipe', 'pipe'] — without this, child_process.spawn
828
905
  // defaults stdin to a fresh pipe. Codex 0.125.0's `exec` then blocks on
829
906
  // `Reading additional input from stdin...` because it sees an open pipe
@@ -832,7 +909,8 @@ const runCodex = ({
832
909
  // `'ignore'` gives codex /dev/null → immediate EOF → it falls back to the
833
910
  // argv prompt as intended. Surfaced live during ADR-005 Phase 2 smoke.
834
911
  // What the turn leaves running is stopped once it has closed (see claude.js).
835
- const turn = markTurn(env ?? process.env, { enabled: turnReap });
912
+ // A confined turn's leftovers are stopped whatever the keep setting says.
913
+ const turn = markTurn(env ?? process.env, { enabled: turnReap, confined });
836
914
  // On Windows `npm i -g @openai/codex` leaves a `codex.cmd`; it is read and
837
915
  // its target spawned directly, never through cmd.exe, because the prompt is
838
916
  // the last argv element and is pod content (#2141). Identity elsewhere.
@@ -978,6 +1056,16 @@ export default {
978
1056
  const publicSandboxMode = sandbox?.trust === 'public'
979
1057
  ? sandbox?.mode ?? 'workspace'
980
1058
  : null;
1059
+ // Checked before the public home is prepared, so a refused route changes nothing.
1060
+ // Only a seat that will forward the URL validates it: a ChatGPT host with an
1061
+ // OPENAI_BASE_URL set for some other tool keeps running, as it did before.
1062
+ let modelBaseUrl = null;
1063
+ const routeEnv = ctx.env || process.env;
1064
+ if (publicSandboxMode !== null && String(routeEnv.OPENAI_BASE_URL || '').trim()) {
1065
+ const authKind = await codexAuthKind(operatorCodexHome(ctx), routeEnv);
1066
+ if (authKind === 'apikey') modelBaseUrl = publicModelRouteBaseUrl(routeEnv);
1067
+ else if (authKind === 'login') process.stderr.write(`[codex] ${CHATGPT_ROUTE_IGNORED}\n`);
1068
+ }
981
1069
  const childEnv = { ...(ctx.env || process.env), ...mcp.forwardedEnv };
982
1070
  const publicCodexHome = publicSandboxMode !== null
983
1071
  ? await preparePublicCodexHome(ctx, childEnv)
@@ -993,6 +1081,7 @@ export default {
993
1081
  effort: ctx.environment?.effort,
994
1082
  commitAttributionEnv: ctx.commitAttributionEnv,
995
1083
  codexHelperReadPaths: publicCodexHome?.helperReadPaths || [],
1084
+ modelBaseUrl,
996
1085
  });
997
1086
  // childEnv is derived from the process environment, so the bootstrap export has to be
998
1087
  // taken back out: the declaration above moved our server onto the file,
@@ -1032,6 +1121,10 @@ export default {
1032
1121
  spawnImpl: ctx._spawnImpl, onSpawn: ctx.onSpawn, controls: controlsFromArguments(args.slice(args.indexOf('exec') + (args[args.indexOf('exec') + 1] === 'resume' ? 3 : 1), -1), { modelFlag: '--model', valueFlags: ['-c', '-o', '--permission-profile'], codex: true }), // test seam only — do not use in production
1033
1122
  ownGroup: wantsOwnProcessGroup(ctx),
1034
1123
  turnReap: ctx._turnReap,
1124
+ // A public seat keeps nothing a turn leaves running, whatever the turn
1125
+ // set (F201, as claude.js does for F197). `publicSandboxMode` is set
1126
+ // exactly when trust is `public`; workspace trust is refused above.
1127
+ confined: publicSandboxMode !== null,
1035
1128
  commandResolution: ctx._commandResolution, // test seam only
1036
1129
  });
1037
1130