Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions cli/__tests__/adapters.claude.environment.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,115 @@ describe('claude adapter — ctx.environment', () => {
expect(fs.existsSync(path.dirname(calls[0].configPath))).toBe(false);
});

test('TASK-174: a warmed MCP home makes claude execute the home build, not npx @latest', async () => {
// The control for this arm is the rest of this file: every other test here
// declares the shipped `npx -y @commonlyai/mcp@latest`, and the jest setup
// points COMMONLY_MCP_HOME at an EMPTY dir, so they all see it unchanged.
// This arm is the same call with a populated home.
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'cli-mcp-home-'));
const pkgDir = path.join(home, '0.3.13', 'node_modules', '@commonlyai', 'mcp');
fs.mkdirSync(path.join(pkgDir, 'src'), { recursive: true });
fs.writeFileSync(path.join(pkgDir, 'package.json'), JSON.stringify({
name: '@commonlyai/mcp', version: '0.3.13', type: 'module', bin: { 'commonly-mcp': 'src/index.js' },
}));
fs.writeFileSync(path.join(pkgDir, 'src', 'index.js'), '');
fs.writeFileSync(path.join(home, 'current'), '0.3.13\n');
const previous = process.env.COMMONLY_MCP_HOME;
process.env.COMMONLY_MCP_HOME = home;
try {
const { impl, calls } = makeSpawnImpl();
await claude.spawn('hi', {
sessionId: null,
cwd,
instanceUrl: 'https://api.example.test',
runtimeToken: 'cm_agent_secret',
environment: {
mcp: [{
name: 'commonly',
transport: 'stdio',
command: ['npx', '-y', '@commonlyai/mcp@latest'],
env: {
COMMONLY_API_URL: '${COMMONLY_API_URL}',
COMMONLY_AGENT_TOKEN: '${COMMONLY_AGENT_TOKEN}',
},
}],
},
_spawnImpl: impl,
});

const entry = calls[0].config.mcpServers.commonly;
const bin = path.join(pkgDir, 'src', 'index.js');
expect(entry.command).toBe('node');
expect(entry.args).toEqual([bin]);
expect(JSON.stringify(calls[0].config)).not.toContain('@latest');
// And the credential channel did NOT move with it: the executed build is
// 0.3.13, which reads the file, exactly as the unpinned declaration did.
expect(entry.env.COMMONLY_TOKEN_FILE).toBeDefined();
expect(entry.env.COMMONLY_AGENT_TOKEN).toBeUndefined();
} finally {
// `process.env.X = undefined` does not unset X — it sets the STRING
// "undefined", which is how a warm came to write ./undefined/ (TASK-174).
if (previous === undefined) delete process.env.COMMONLY_MCP_HOME;
else process.env.COMMONLY_MCP_HOME = previous;
}
});

test('TASK-174: an OLD build in the home keeps the env channel — the channel follows what RUNS', async () => {
// The interesting case, and the one that makes the coupling observable: the
// declaration is the unpinned `@latest` (which is never treated as old, so it
// lands on the file channel), while the home points at 0.3.7, whose reader
// only understands the environment. Deciding the channel on the declaration
// would hand that build a path it cannot read and take the seat's tools away;
// deciding it on the executed command keeps the token in the env for exactly
// that build (mcp-credential-delivery only does this below 0.3.12).
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'cli-mcp-home-old-'));
const pkgDir = path.join(home, '0.3.7', 'node_modules', '@commonlyai', 'mcp');
fs.mkdirSync(path.join(pkgDir, 'src'), { recursive: true });
fs.writeFileSync(path.join(pkgDir, 'package.json'), JSON.stringify({
name: '@commonlyai/mcp', version: '0.3.7', type: 'module', bin: { 'commonly-mcp': 'src/index.js' },
}));
fs.writeFileSync(path.join(pkgDir, 'src', 'index.js'), '');
fs.writeFileSync(path.join(home, 'current'), '0.3.7\n');
const previous = process.env.COMMONLY_MCP_HOME;
process.env.COMMONLY_MCP_HOME = home;
try {
const { impl, calls } = makeSpawnImpl();
await claude.spawn('hi', {
sessionId: null,
cwd,
instanceUrl: 'https://api.example.test',
runtimeToken: 'cm_agent_secret',
environment: {
mcp: [{
name: 'commonly',
transport: 'stdio',
command: ['npx', '-y', '@commonlyai/mcp@latest'],
env: {
COMMONLY_API_URL: '${COMMONLY_API_URL}',
COMMONLY_AGENT_TOKEN: '${COMMONLY_AGENT_TOKEN}',
},
}],
},
_spawnImpl: impl,
});

const entry = calls[0].config.mcpServers.commonly;
expect(entry.command).toBe('node');
expect(entry.args[0]).toContain(path.join('0.3.7', 'node_modules'));
expect(entry.env.COMMONLY_AGENT_TOKEN).toBe('${COMMONLY_AGENT_TOKEN}');
expect(entry.env.COMMONLY_TOKEN_FILE).toBeUndefined();
// The token rides in the CHILD ENV for this build, because claude expands
// `${VAR}` from its own environment — the channel is observable in two
// places, and asserting only the config would miss the child env.
expect(calls[0].opts.env.COMMONLY_AGENT_TOKEN).toBe('cm_agent_secret');
} finally {
// `process.env.X = undefined` does not unset X — it sets the STRING
// "undefined", which is how a warm came to write ./undefined/ (TASK-174).
if (previous === undefined) delete process.env.COMMONLY_MCP_HOME;
else process.env.COMMONLY_MCP_HOME = previous;
}
});

test('mounts skills.claude entries into <cwd>/.claude/skills/ as read-only copies', async () => {
const { impl } = makeSpawnImpl();
const skillSrc = fs.mkdtempSync(path.join(os.tmpdir(), 'cli-claude-skill-'));
Expand Down
61 changes: 61 additions & 0 deletions cli/__tests__/adapters.codex.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { EventEmitter } from 'events';
import { existsSync, readFileSync, statSync } from 'fs';
import {
lstat,
mkdir,
mkdtemp,
readFile,
readlink,
Expand Down Expand Up @@ -183,6 +184,66 @@ describe('codex adapter — spawn()', () => {
expect(resumed.calls[0].args).toContain('model_reasoning_effort="xhigh"');
});

test('TASK-174: a warmed MCP home turns the override into `node <bin>`, and the credential stays on the file channel', async () => {
// Control: the test below this one declares the same server with an EMPTY
// home (the jest setup points COMMONLY_MCP_HOME at a fresh dir) and expects
// npx @latest. Same declaration, same call — only the home differs.
const home = await mkdtemp(join(tmpdir(), 'cli-mcp-home-'));
const pkgDir = join(home, '0.3.13', 'node_modules', '@commonlyai', 'mcp');
await mkdir(join(pkgDir, 'src'), { recursive: true });
await writeFile(join(pkgDir, 'package.json'), JSON.stringify({
name: '@commonlyai/mcp', version: '0.3.13', type: 'module', bin: { 'commonly-mcp': 'src/index.js' },
}));
await writeFile(join(pkgDir, 'src', 'index.js'), '');
await writeFile(join(home, 'current'), '0.3.13\n');
const previous = process.env.COMMONLY_MCP_HOME;
process.env.COMMONLY_MCP_HOME = home;
try {
const { impl, calls } = makeSpawnImpl({
stdoutChunks: ['{"type":"turn.completed"}\n'],
outputContents: 'ok',
});
await codex.spawn('hi', {
sessionId: null,
_spawnImpl: impl,
env: spawnEnv(),
runtimeToken: 'cm_agent_secret',
instanceUrl: 'https://api.example.test',
environment: {
mcp: [{
name: 'commonly',
transport: 'stdio',
command: ['npx', '-y', '@commonlyai/mcp@latest'],
env: {
COMMONLY_API_URL: '${COMMONLY_API_URL}',
COMMONLY_AGENT_TOKEN: '${COMMONLY_AGENT_TOKEN}',
},
}],
},
});

const args = calls[0].args;
const cFlags = args.map((a, i) => (a === '-c' ? args[i + 1] : null)).filter(Boolean);
const bin = join(pkgDir, 'src', 'index.js');
expect(cFlags).toContain('mcp_servers.commonly.command="node"');
expect(cFlags).toContain(`mcp_servers.commonly.args=${JSON.stringify([bin])}`);
// The spec is gone from the spawn path entirely: no npx, no @latest.
// (The BIN PATH still contains '@commonlyai/mcp' — that is the package's
// install directory, not the spec that would resolve at spawn time.)
expect(args.join(' ')).not.toContain('@latest');
expect(args.join(' ')).not.toContain('npx');
// 0.3.13 reads the credential file, so the rewrite did not silently move
// this seat onto the env channel.
expect(cFlags.find((f) => f.startsWith('mcp_servers.commonly.env='))).toContain('COMMONLY_TOKEN_FILE');
expect(cFlags.find((f) => f.includes('env_vars'))).toBeUndefined();
} finally {
// `process.env.X = undefined` does not unset X — it sets the STRING
// "undefined", which is how a warm came to write ./undefined/ (TASK-174).
if (previous === undefined) delete process.env.COMMONLY_MCP_HOME;
else process.env.COMMONLY_MCP_HOME = previous;
}
});

test('environment.mcp servers become -c mcp_servers.* overrides with substituted token/env', async () => {
// Regression for the 2026-07-22 as-operator attribution incident: the
// adapter used to silently ignore environment.mcp, so a codex agent had
Expand Down
24 changes: 24 additions & 0 deletions cli/__tests__/adapters.pi.credential-channel.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,30 @@ describe('describeMcpCommand: is this our server, and which version', () => {
)).toEqual({ isCommonly: true, version: [0, 3, 4] });
});

test('a home bin PATH is a path, not a spec — the version comes from its package.json', () => {
// TASK-174. The seat home runs `node <home>/<version>/node_modules/@commonlyai/mcp/<bin>`,
// and that string CONTAINS the package name. Before this arm existed, the
// spec branch matched it first and answered `{ version: null }` — "unpinned,
// so never old" — for every home build. The version then decided nothing,
// and an old build in the home was handed the credential file channel it
// cannot read.
const pkg = JSON.stringify({ name: '@commonlyai/mcp', version: '0.3.7' });
expect(describeMcpCommand(
['node', '/home/me/.commonly/mcp/0.3.7/node_modules/@commonlyai/mcp/src/index.js'],
reader(pkg),
)).toEqual({ isCommonly: true, version: [0, 3, 7] });
// Control, same shape, unreadable package.json: it must NOT be read as an
// unpinned npx spec (which is what the bug did — it answered
// `{ isCommonly: true, version: null }` here).
expect(describeMcpCommand(
['node', '/home/me/.commonly/mcp/0.3.7/node_modules/@commonlyai/mcp/src/index.js'],
reader(null),
)).toBeNull();
// And the npx shapes are untouched by that rule.
expect(describeMcpCommand(['npx', '-y', '@commonlyai/mcp@latest']))
.toEqual({ isCommonly: true, version: null });
});

test('a package.json naming another package, or a malformed one, is not our server', () => {
expect(describeMcpCommand(['node', '/tmp/other/src/index.js'], reader('{"name":"other","version":"1.0.0"}'))).toBeNull();
expect(describeMcpCommand(['node', '/tmp/broken/src/index.js'], reader('{not json'))).toBeNull();
Expand Down
46 changes: 46 additions & 0 deletions cli/__tests__/adapters.pi.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,52 @@ describe('helpers', () => {
expect(resolveProvider({})).toEqual({ name: 'litellm', baseUrl: 'https://litellm.commonly.me/v1', api: 'openai-completions', apiKeyEnv: 'COMMONLY_LITELLM_KEY' });
expect(buildModelsJson(resolveProvider({}), 'm').providers.litellm.models[0].id).toBe('m');
});
test('TASK-174: a warmed MCP home makes pi exec the home build, not npx @latest', async () => {
// Control: the helper tests in this file call resolveMcpServers with the
// empty home the jest setup installs, and see `npx -y @commonlyai/mcp@latest`
// carried through unchanged. This arm is the same call with a real home.
const home = await mkdtemp(join(tmpdir(), 'cli-mcp-home-'));
const pkgDir = join(home, '0.3.13', 'node_modules', '@commonlyai', 'mcp');
await mkdir(join(pkgDir, 'src'), { recursive: true });
await writeFile(join(pkgDir, 'package.json'), JSON.stringify({
name: '@commonlyai/mcp', version: '0.3.13', type: 'module', bin: { 'commonly-mcp': 'src/index.js' },
}));
await writeFile(join(pkgDir, 'src', 'index.js'), '');
await writeFile(join(home, 'current'), '0.3.13\n');
const previous = process.env.COMMONLY_MCP_HOME;
process.env.COMMONLY_MCP_HOME = home;
try {
const ctx = {
instanceUrl: 'https://api.example.test',
credentialFile: '/tmp/credential-xyz',
credentialKey: 'COMMONLY_AGENT_TOKEN',
};
const declared = [{
name: 'commonly',
transport: 'stdio',
command: ['npx', '-y', '@commonlyai/mcp@latest'],
env: {
COMMONLY_API_URL: '${COMMONLY_API_URL}',
COMMONLY_AGENT_TOKEN: '${COMMONLY_AGENT_TOKEN}',
},
}];

const [carried] = resolveMcpServers(declared, ctx);

expect(carried.command).toEqual(['node', join(pkgDir, 'src', 'index.js')]);
// The executed build is 0.3.13, which reads the file — same channel the
// unpinned declaration landed on, so the rewrite is not a silent change
// of how this seat authenticates.
expect(carried.env.COMMONLY_TOKEN_FILE).toBe(ctx.credentialFile);
expect(carried.env.COMMONLY_AGENT_TOKEN).toBeUndefined();
} finally {
// `process.env.X = undefined` does not unset X — it sets the STRING
// "undefined", which is how a warm came to write ./undefined/ (TASK-174).
if (previous === undefined) delete process.env.COMMONLY_MCP_HOME;
else process.env.COMMONLY_MCP_HOME = previous;
}
});

test('resolveMcpServers carries stdio and HTTP entries, filling placeholders in env and headers alike', () => {
const resolved = resolveMcpServers([
{ name: 'a', command: ['x'], env: { K: '${COMMONLY_OTHER}' } },
Expand Down
Loading
Loading