@commonlyai/cli 0.1.367 → 0.1.369

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.367",
3
+ "version": "0.1.369",
4
4
  "license": "Apache-2.0",
5
5
  "description": "The Commonly CLI — connect agents, manage pods, iterate fast",
6
6
  "type": "module",
@@ -679,6 +679,51 @@ const substitutePublicPlaceholders = (value, ctx) => {
679
679
  return value.replace(PLACEHOLDER_RE, (whole, key) => replacements[key] || whole);
680
680
  };
681
681
 
682
+ // F193: OpenCode expands `{env:NAME}` and then `{file:path}` across the config
683
+ // it loads (so `{file:{env:X}}` nests). A remote entry that spells one itself
684
+ // reaches a file or env value buildMcpConfig never wrote, the seat credential
685
+ // included, and is never recorded in `bearerFileServers`. Only the bearer
686
+ // rewrite below may carry one.
687
+ const OPENCODE_SUBSTITUTION_RE = /\{(?:env|file):/;
688
+
689
+ // F484: the same rule for the whole config, since OpenCode substitutes all of
690
+ // its text, keys included: a stdio entry's command, environment or cwd, and the
691
+ // provider's baseURL and models, are operator-written too. A stdio command part
692
+ // spelling `{file:<credential>}` also passed the argv credential check, which
693
+ // runs before OpenCode expands it. The only substitutions allowed are the two
694
+ // this adapter writes, each at its own place: the bearer header of a remote
695
+ // entry recorded in `bearerFileServers`, and the selected provider's apiKey.
696
+ // Anything else refuses the spawn; the error names where, never the value.
697
+ const assertOwnSubstitutionsOnly = (config, {
698
+ credentialFile = null, bearerFileServers = [], providerId = null, keyFile = null,
699
+ } = {}) => {
700
+ const bearer = credentialFile ? `Bearer {file:${credentialFile}}` : null;
701
+ const apiKey = keyFile ? `{file:${keyFile}}` : null;
702
+ const own = (path, value) => (
703
+ (bearer !== null && value === bearer && path.length === 4 && path[0] === 'mcp'
704
+ && bearerFileServers.includes(path[1]) && path[2] === 'headers'
705
+ && String(path[3]).toLowerCase() === 'authorization')
706
+ || (apiKey !== null && value === apiKey && path.length === 4 && path[0] === 'provider'
707
+ && providerId !== null && path[1] === providerId && path[2] === 'options' && path[3] === 'apiKey')
708
+ );
709
+ const refuse = (path) => {
710
+ throw new Error(`OpenCode config refused: an OpenCode substitution ({env: or {file:) at ${path.join('.')}; `
711
+ + 'only the adapter\'s own credential and provider key spellings may carry one');
712
+ };
713
+ const walk = (node, path) => {
714
+ if (typeof node === 'string') {
715
+ if (OPENCODE_SUBSTITUTION_RE.test(node) && !own(path, node)) refuse(path);
716
+ return;
717
+ }
718
+ if (!node || typeof node !== 'object') return;
719
+ for (const [key, value] of Object.entries(node)) {
720
+ if (OPENCODE_SUBSTITUTION_RE.test(key)) refuse([...path, key]);
721
+ walk(value, [...path, key]);
722
+ }
723
+ };
724
+ walk(config, []);
725
+ };
726
+
682
727
  const isSafeMcpName = (name) => typeof name === 'string'
683
728
  && /^[A-Za-z0-9_-]+$/.test(name);
684
729
 
@@ -762,11 +807,18 @@ const buildMcpConfig = (servers, ctx = {}) => {
762
807
  // Runtime credentials only travel through OpenCode's measured {file:}
763
808
  // header expansion, never as environment values or literal config text.
764
809
  if (runtimeToken && url.includes(runtimeToken)) continue;
810
+ if (OPENCODE_SUBSTITUTION_RE.test(url)) {
811
+ // eslint-disable-next-line no-console
812
+ console.warn(`[opencode] skipping MCP server ${server.name}: its url spells an OpenCode substitution`);
813
+ continue;
814
+ }
765
815
  const headers = {};
766
816
  let unsupportedCredential = false;
817
+ let ownSubstitution = '';
767
818
  for (const [key, rawValue] of Object.entries(server.headers || {})) {
768
819
  if (typeof rawValue !== 'string') continue;
769
820
  let value = substitutePublicPlaceholders(rawValue, ctx);
821
+ let rewritten = false;
770
822
  if (rawValue.includes('${COMMONLY_AGENT_TOKEN}')) {
771
823
  if (key.toLowerCase() !== 'authorization'
772
824
  || rawValue !== 'Bearer ${COMMONLY_AGENT_TOKEN}'
@@ -775,13 +827,24 @@ const buildMcpConfig = (servers, ctx = {}) => {
775
827
  break;
776
828
  }
777
829
  value = `Bearer {file:${ctx.credentialFile}}`;
830
+ rewritten = true;
778
831
  }
779
832
  if (runtimeToken && value.includes(runtimeToken)) {
780
833
  unsupportedCredential = true;
781
834
  break;
782
835
  }
836
+ // The name is checked too: OpenCode substitutes the config text, keys included.
837
+ if (OPENCODE_SUBSTITUTION_RE.test(key) || (!rewritten && OPENCODE_SUBSTITUTION_RE.test(value))) {
838
+ ownSubstitution = key;
839
+ break;
840
+ }
783
841
  headers[key] = value;
784
842
  }
843
+ if (ownSubstitution) {
844
+ // eslint-disable-next-line no-console
845
+ console.warn(`[opencode] skipping MCP server ${server.name}: a header spells an OpenCode substitution`);
846
+ continue;
847
+ }
785
848
  if (unsupportedCredential) {
786
849
  // eslint-disable-next-line no-console
787
850
  console.warn(`[opencode] skipping MCP server ${server.name}: remote credentials must use the bearer file channel`);
@@ -1559,6 +1622,12 @@ export default {
1559
1622
  } : {}),
1560
1623
  ...(Object.keys(mcp).length ? { mcp } : {}),
1561
1624
  };
1625
+ assertOwnSubstitutionsOnly(config, {
1626
+ credentialFile: credential?.path || null,
1627
+ bearerFileServers,
1628
+ providerId: provider?.id ?? null,
1629
+ keyFile: provider ? (providerProxy?.tokenFile || providerKeyFile) : null,
1630
+ });
1562
1631
  await writeFile(configPath, `${JSON.stringify(config, null, 2)}\n`, {
1563
1632
  encoding: 'utf8', mode: 0o600,
1564
1633
  });
@@ -1731,6 +1800,7 @@ export {
1731
1800
  DISABLED_ENV,
1732
1801
  TESTED_OPENCODE_VERSIONS,
1733
1802
  buildArgs,
1803
+ assertOwnSubstitutionsOnly,
1734
1804
  buildMcpConfig,
1735
1805
  buildProviderConfig,
1736
1806
  makeEventParser,