@commonlyai/cli 0.1.260 → 0.1.261

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.261",
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,
@@ -978,6 +1055,16 @@ export default {
978
1055
  const publicSandboxMode = sandbox?.trust === 'public'
979
1056
  ? sandbox?.mode ?? 'workspace'
980
1057
  : null;
1058
+ // Checked before the public home is prepared, so a refused route changes nothing.
1059
+ // Only a seat that will forward the URL validates it: a ChatGPT host with an
1060
+ // OPENAI_BASE_URL set for some other tool keeps running, as it did before.
1061
+ let modelBaseUrl = null;
1062
+ const routeEnv = ctx.env || process.env;
1063
+ if (publicSandboxMode !== null && String(routeEnv.OPENAI_BASE_URL || '').trim()) {
1064
+ const authKind = await codexAuthKind(operatorCodexHome(ctx), routeEnv);
1065
+ if (authKind === 'apikey') modelBaseUrl = publicModelRouteBaseUrl(routeEnv);
1066
+ else if (authKind === 'login') process.stderr.write(`[codex] ${CHATGPT_ROUTE_IGNORED}\n`);
1067
+ }
981
1068
  const childEnv = { ...(ctx.env || process.env), ...mcp.forwardedEnv };
982
1069
  const publicCodexHome = publicSandboxMode !== null
983
1070
  ? await preparePublicCodexHome(ctx, childEnv)
@@ -993,6 +1080,7 @@ export default {
993
1080
  effort: ctx.environment?.effort,
994
1081
  commitAttributionEnv: ctx.commitAttributionEnv,
995
1082
  codexHelperReadPaths: publicCodexHome?.helperReadPaths || [],
1083
+ modelBaseUrl,
996
1084
  });
997
1085
  // childEnv is derived from the process environment, so the bootstrap export has to be
998
1086
  // taken back out: the declaration above moved our server onto the file,