@devries-cameron20 / composio Mirror · read-only / commits / 124e896ed
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 17:15 UTC

fix(cli): align the TS and sh managed-block reconcilers on one contract

The CLI and install.sh implemented the managed-PATH-block contract twice with three divergences: TS matched marker lines after trimming while the sh awk program matches byte-exact (CRLF or padded markers reconciled on one side and duplicated on the other), TS replaced stale blocks in place while sh removes managed lines and appends the fresh block, and renderWithHome rendered a bin dir equal to $HOME as the $HOME literal while render_bin_dir left it as the raw path — making delegated verification permanently stale for that layout. Align both sides: TS goes byte-exact and remove-then-append (mirroring the awk program including its newline normalization), and render_bin_dir learns the $HOME-exact case. A shared fixture suite under test/managed-block-fixtures now runs against both reconcilers — vitest drives the exported TS functions, the harness sources install.sh's definitions and runs write_path_block under sh and dash — so future edits to one side fail loudly until the other matches byte for byte.

jkomyno committed Aug 3, 2026 at 21:10 UTC 124e896ed79997cdf6efeb1a86d80af5b2192780
53 files changed +273 -42
.gitattributes
+4
@@ -14,3 +14,7 @@ ts/packages/core/pack/generated/** linguist-generated=true
14 pnpm-lock.yaml linguist-generated=true
15 uv.lock linguist-generated=true
16 **/bun.lock linguist-generated=true
17 +
18 +# Managed-block conformance fixtures are byte-exact inputs/outputs shared by
19 +# the sh harness and the TS reconciler test; never normalize their line endings.
20 +test/managed-block-fixtures/** -text
install.sh
+9 -1
@@ -368,9 +368,17 @@ is_unsafe_path() {
368 return 1
369 }
370
371 +# Renders the home directory itself and paths under it with a literal $HOME
372 +# prefix. Must stay in lockstep with the CLI's renderWithHome (install.cmd.ts):
373 +# delegated_setup_verified compares the CLI-written line against this rendering
374 +# byte for byte, so any disagreement makes every delegated install look stale
375 +# and forces a needless inline rewrite.
376 render_bin_dir() {
377 render_bin_dir_value=$1
373 - case $render_bin_dir_value in "$HOME"/*) render_bin_dir_value=\$HOME/${render_bin_dir_value#"$HOME"/} ;; esac
378 + case $render_bin_dir_value in
379 + "$HOME") render_bin_dir_value=\$HOME ;;
380 + "$HOME"/*) render_bin_dir_value=\$HOME/${render_bin_dir_value#"$HOME"/} ;;
381 + esac
382 printf '%s\n' "$render_bin_dir_value"
383 }
384
test/install-sh-release-resolution.test.sh
+43
@@ -180,6 +180,49 @@ sanitize_path() {
180 }
181 ambient_path=$(sanitize_path "$PATH")
182
183 +# TS/sh managed-block contract conformance: the fixtures under
184 +# test/managed-block-fixtures also drive the CLI's reconciler
185 +# (ts/packages/cli/test/src/install-managed-block-conformance.test.ts), so an
186 +# edit to either implementation fails one of the two suites until the other
187 +# side produces byte-identical output again.
188 +conformance_fixtures="$repo_root/test/managed-block-fixtures"
189 +conformance_functions="$suite_tmp/install-sh-functions.sh"
190 +# The entry-point guard above pins install.sh to end with exactly one
191 +# `main "$@"` line, so dropping the final line leaves only definitions.
192 +sed '$d' "$repo_root/install.sh" >"$conformance_functions"
193 +conformance_driver="$suite_tmp/conformance-driver.sh"
194 +cat >"$conformance_driver" <<'EOF'
195 +#!/bin/sh
196 +set -eu
197 +functions_file=$1
198 +rc_file=$2
199 +shell_name=$3
200 +bin_dir=$4
201 +. "$functions_file"
202 +rendered=$(render_bin_dir "$bin_dir")
203 +write_path_block "$rc_file" "$shell_name" "$rendered"
204 +EOF
205 +[[ -d $conformance_fixtures ]] || fail 'managed-block fixtures directory is missing'
206 +for interpreter in "${interpreters[@]}"; do
207 + interpreter_name=$(basename "$interpreter")
208 + for fixture_dir in "$conformance_fixtures"/*/; do
209 + fixture_name=$(basename "$fixture_dir")
210 + fixture_home="$suite_tmp/conformance-$interpreter_name/$fixture_name"
211 + mkdir -p "$fixture_home"
212 + cp "$fixture_dir/before" "$fixture_home/rcfile"
213 + fixture_bin_dir=$(sed "s|__HOME__|$fixture_home|" "$fixture_dir/bin-dir")
214 + fixture_shell=$(<"$fixture_dir/shell")
215 + env HOME="$fixture_home" COMPOSIO_QUIET=1 \
216 + "$interpreter" "$conformance_driver" "$conformance_functions" \
217 + "$fixture_home/rcfile" "$fixture_shell" "$fixture_bin_dir" ||
218 + fail "$interpreter_name managed-block conformance: $fixture_name (driver failed)"
219 + cmp -s "$fixture_dir/after" "$fixture_home/rcfile" || {
220 + diff -u "$fixture_dir/after" "$fixture_home/rcfile" >&2 || :
221 + fail "$interpreter_name managed-block conformance: $fixture_name (output drifted)"
222 + }
223 + done
224 +done
225 +
226 for interpreter in "${interpreters[@]}"; do
227 interpreter_name=$(basename "$interpreter")
228 case_root="$suite_tmp/$interpreter_name"
test/managed-block-fixtures/absent-append-fish/after new
+4
@@ -0,0 +1,4 @@
1 +# fish config
2 +
3 +# Composio CLI
4 +set --export PATH "$HOME/.local/bin" $PATH
test/managed-block-fixtures/absent-append-fish/before new
+1
@@ -0,0 +1 @@
1 +# fish config
test/managed-block-fixtures/absent-append-fish/bin-dir new
+1
@@ -0,0 +1 @@
1 +__HOME__/.local/bin
test/managed-block-fixtures/absent-append-fish/shell new
+1
@@ -0,0 +1 @@
1 +fish
test/managed-block-fixtures/absent-append-zsh/after new
+5
@@ -0,0 +1,5 @@
1 +# my rc
2 +alias ll='ls -la'
3 +
4 +# Composio CLI
5 +export PATH="$HOME/.local/bin:$PATH"
test/managed-block-fixtures/absent-append-zsh/before new
+2
@@ -0,0 +1,2 @@
1 +# my rc
2 +alias ll='ls -la'
test/managed-block-fixtures/absent-append-zsh/bin-dir new
+1
@@ -0,0 +1 @@
1 +__HOME__/.local/bin
test/managed-block-fixtures/absent-append-zsh/shell new
+1
@@ -0,0 +1 @@
1 +zsh
test/managed-block-fixtures/crlf-marker-untouched-zsh/after new
+5
@@ -0,0 +1,5 @@
1 +# Composio CLI
2 +export PATH="/stale/bin:$PATH"
3 +
4 +# Composio CLI
5 +export PATH="$HOME/.local/bin:$PATH"
test/managed-block-fixtures/crlf-marker-untouched-zsh/before new
+2
@@ -0,0 +1,2 @@
1 +# Composio CLI
2 +export PATH="/stale/bin:$PATH"
test/managed-block-fixtures/crlf-marker-untouched-zsh/bin-dir new
+1
@@ -0,0 +1 @@
1 +__HOME__/.local/bin
test/managed-block-fixtures/crlf-marker-untouched-zsh/shell new
+1
@@ -0,0 +1 @@
1 +zsh
test/managed-block-fixtures/current-noop-zsh/after new
+4
@@ -0,0 +1,4 @@
1 +# existing config
2 +# Composio CLI
3 +export PATH="$HOME/.local/bin:$PATH"
4 +# more config
test/managed-block-fixtures/current-noop-zsh/before new
+4
@@ -0,0 +1,4 @@
1 +# existing config
2 +# Composio CLI
3 +export PATH="$HOME/.local/bin:$PATH"
4 +# more config
test/managed-block-fixtures/current-noop-zsh/bin-dir new
+1
@@ -0,0 +1 @@
1 +__HOME__/.local/bin
test/managed-block-fixtures/current-noop-zsh/shell new
+1
@@ -0,0 +1 @@
1 +zsh
test/managed-block-fixtures/duplicate-collapse-zsh/after new
+4
@@ -0,0 +1,4 @@
1 +# unmanaged line
2 +
3 +# Composio CLI
4 +export PATH="$HOME/.local/bin:$PATH"
test/managed-block-fixtures/duplicate-collapse-zsh/before new
+5
@@ -0,0 +1,5 @@
1 +# Composio CLI
2 +export PATH="/stale/bin:$PATH"
3 +# unmanaged line
4 +# Composio CLI
5 +export PATH="$HOME/.local/bin:$PATH"
test/managed-block-fixtures/duplicate-collapse-zsh/bin-dir new
+1
@@ -0,0 +1 @@
1 +__HOME__/.local/bin
test/managed-block-fixtures/duplicate-collapse-zsh/shell new
+1
@@ -0,0 +1 @@
1 +zsh
test/managed-block-fixtures/empty-file-zsh/after new
+3
@@ -0,0 +1,3 @@
1 +
2 +# Composio CLI
3 +export PATH="$HOME/.local/bin:$PATH"
test/managed-block-fixtures/empty-file-zsh/before
test/managed-block-fixtures/empty-file-zsh/bin-dir new
+1
@@ -0,0 +1 @@
1 +__HOME__/.local/bin
test/managed-block-fixtures/empty-file-zsh/shell new
+1
@@ -0,0 +1 @@
1 +zsh
test/managed-block-fixtures/home-as-bin-dir-zsh/after new
+4
@@ -0,0 +1,4 @@
1 +# rc prologue
2 +
3 +# Composio CLI
4 +export PATH="$HOME:$PATH"
test/managed-block-fixtures/home-as-bin-dir-zsh/before new
+1
@@ -0,0 +1 @@
1 +# rc prologue
test/managed-block-fixtures/home-as-bin-dir-zsh/bin-dir new
+1
@@ -0,0 +1 @@
1 +__HOME__
test/managed-block-fixtures/home-as-bin-dir-zsh/shell new
+1
@@ -0,0 +1 @@
1 +zsh
test/managed-block-fixtures/malformed-marker-comment-zsh/after new
+6
@@ -0,0 +1,6 @@
1 +# added by my dotfiles
2 +export PATH="/stale/bin:$PATH"
3 +# user epilogue
4 +
5 +# Composio CLI
6 +export PATH="$HOME/.local/bin:$PATH"
test/managed-block-fixtures/malformed-marker-comment-zsh/before new
+4
@@ -0,0 +1,4 @@
1 +# Composio CLI
2 +# added by my dotfiles
3 +export PATH="/stale/bin:$PATH"
4 +# user epilogue
test/managed-block-fixtures/malformed-marker-comment-zsh/bin-dir new
+1
@@ -0,0 +1 @@
1 +__HOME__/.local/bin
test/managed-block-fixtures/malformed-marker-comment-zsh/shell new
+1
@@ -0,0 +1 @@
1 +zsh
test/managed-block-fixtures/no-final-newline-append-zsh/after new
+4
@@ -0,0 +1,4 @@
1 +alias ll="ls -la"
2 +
3 +# Composio CLI
4 +export PATH="$HOME/.local/bin:$PATH"
test/managed-block-fixtures/no-final-newline-append-zsh/before new
+1
@@ -0,0 +1 @@
1 +alias ll="ls -la"
\ No newline at end of file
test/managed-block-fixtures/no-final-newline-append-zsh/bin-dir new
+1
@@ -0,0 +1 @@
1 +__HOME__/.local/bin
test/managed-block-fixtures/no-final-newline-append-zsh/shell new
+1
@@ -0,0 +1 @@
1 +zsh
test/managed-block-fixtures/stale-replace-zsh/after new
+6
@@ -0,0 +1,6 @@
1 +# user prologue
2 +# user epilogue
3 +alias ll="ls -la"
4 +
5 +# Composio CLI
6 +export PATH="$HOME/.local/bin:$PATH"
test/managed-block-fixtures/stale-replace-zsh/before new
+5
@@ -0,0 +1,5 @@
1 +# user prologue
2 +# Composio CLI
3 +export PATH="/stale/bin:$PATH"
4 +# user epilogue
5 +alias ll="ls -la"
test/managed-block-fixtures/stale-replace-zsh/bin-dir new
+1
@@ -0,0 +1 @@
1 +__HOME__/.local/bin
test/managed-block-fixtures/stale-replace-zsh/shell new
+1
@@ -0,0 +1 @@
1 +zsh
test/managed-block-fixtures/stale-whitespace-assignment-zsh/after new
+4
@@ -0,0 +1,4 @@
1 +# user prologue
2 +
3 +# Composio CLI
4 +export PATH="$HOME/.local/bin:$PATH"
test/managed-block-fixtures/stale-whitespace-assignment-zsh/before new
+3
@@ -0,0 +1,3 @@
1 +# user prologue
2 +# Composio CLI
3 +export PATH="$HOME/.local/bin:$PATH"
test/managed-block-fixtures/stale-whitespace-assignment-zsh/bin-dir new
+1
@@ -0,0 +1 @@
1 +__HOME__/.local/bin
test/managed-block-fixtures/stale-whitespace-assignment-zsh/shell new
+1
@@ -0,0 +1 @@
1 +zsh
test/managed-block-fixtures/trailing-space-marker-untouched-zsh/after new
+5
@@ -0,0 +1,5 @@
1 +# Composio CLI
2 +export PATH="/stale/bin:$PATH"
3 +
4 +# Composio CLI
5 +export PATH="$HOME/.local/bin:$PATH"
test/managed-block-fixtures/trailing-space-marker-untouched-zsh/before new
+2
@@ -0,0 +1,2 @@
1 +# Composio CLI
2 +export PATH="/stale/bin:$PATH"
test/managed-block-fixtures/trailing-space-marker-untouched-zsh/bin-dir new
+1
@@ -0,0 +1 @@
1 +__HOME__/.local/bin
test/managed-block-fixtures/trailing-space-marker-untouched-zsh/shell new
+1
@@ -0,0 +1 @@
1 +zsh
ts/packages/cli/src/commands/install.cmd.ts
+51 -41
@@ -138,15 +138,19 @@ const resolveBinDir = (params: {
138 /**
139 * Render a path for embedding in a shell rc file. `~` never expands inside
140 * double quotes, so home-relative paths use a literal `$HOME` prefix instead.
141 + * Must stay in lockstep with install.sh's render_bin_dir: the installer's
142 + * delegated_setup_verified compares its own rendering against the line this
143 + * command wrote byte for byte, so any disagreement makes every delegated
144 + * install look stale and forces a needless inline rewrite.
145 */
142 -const renderWithHome = (dir: string, homedir: string): string =>
146 +export const renderWithHome = (dir: string, homedir: string): string =>
147 dir === homedir
148 ? '$HOME'
149 : dir.startsWith(`${homedir}/`)
150 ? `$HOME/${dir.slice(homedir.length + 1)}`
151 : dir;
152
149 -const pathBlockForShell = (shell: Shell, binDir: string, homedir: string): string => {
153 +export const pathBlockForShell = (shell: Shell, binDir: string, homedir: string): string => {
154 const rendered = renderWithHome(binDir, homedir);
155 return shell === 'fish'
156 ? [MARKER, `set --export PATH "${rendered}" $PATH`].join('\n')
@@ -242,9 +246,13 @@ const buildShellConfig = (
246 completionBlock: completionScript ? `${COMPLETIONS_MARKER}\n${completionScript}` : undefined,
247 });
248
245 -/** Check whether a file already contains a given marker line. */
249 +/**
250 + * Check whether a file already contains a given marker line. Byte-exact, like
251 + * install.sh's awk `$0 == "# Composio CLI"` match: a marker mangled by CRLF
252 + * endings or stray whitespace is not ours and is left alone on both sides.
253 + */
254 const fileContains = (contents: string, marker: string): boolean =>
247 - contents.split('\n').some(line => line.trim() === marker.trim());
255 + contents.split('\n').some(line => line === marker);
256
257 /** Outcome of reconciling a startup file's managed PATH block against the resolved bin dir. */
258 type ManagedPathBlockState =
@@ -266,34 +274,44 @@ const isManagedPathAssignment = (line: string): boolean => {
274 );
275 };
276
277 +/**
278 + * Append managed blocks after existing content, separated by one blank line
279 + * and ending with a newline. Content lacking a final newline gains one first,
280 + * exactly as install.sh's line-based awk rewrite normalizes it.
281 + */
282 +export const appendManagedBlocks = (contents: string, blocks: readonly string[]): string => {
283 + const base = contents === '' || contents.endsWith('\n') ? contents : `${contents}\n`;
284 + return `${base}\n${blocks.join('\n\n')}\n`;
285 +};
286 +
287 /**
288 * Reconcile the managed PATH block — the `MARKER` line plus the PATH
289 * assignment right below it — with the currently resolved bin directory.
272 - * A well-formed block recording a stale bin dir is replaced in place,
273 - * duplicate managed PATH blocks collapse into a single current block, and
274 - * unmanaged content (including the completions block) is left byte-for-byte
275 - * untouched.
290 *
277 - * A marker whose next line is not a recognizable managed assignment (a user
278 - * annotation, a blank line, another marker) is malformed: only the marker
279 - * line itself is removed, and the fresh block is appended after all remaining
280 - * content instead of replacing in place. That guarantees no user content is
281 - * ever deleted, and — because these prepend-style assignments make the
282 - * last-sourced line win — the new bin dir still takes PATH precedence over
283 - * any orphaned stale assignment left behind.
291 + * This mirrors install.sh's write_path_block awk program step for step, and
292 + * the shared fixtures under test/managed-block-fixtures pin both
293 + * implementations to byte-identical outputs — any behavioral edit here must
294 + * land in install.sh too. A file whose single byte-exact marker is directly
295 + * followed by the expected assignment is current and left untouched.
296 + * Otherwise every managed line — each marker, plus the next line only when it
297 + * is a recognizable managed PATH assignment; anything else (a user
298 + * annotation, a blank line, another marker) is not ours to delete — is
299 + * removed and one fresh block is appended after the remaining content. No
300 + * user content is ever deleted, and because these prepend-style assignments
301 + * make the last-sourced line win, the new bin dir takes PATH precedence over
302 + * any orphaned stale assignment that had to be preserved.
303 */
285 -const reconcileManagedPathBlock = (contents: string, pathBlock: string): ManagedPathBlockState => {
304 +export const reconcileManagedPathBlock = (
305 + contents: string,
306 + pathBlock: string
307 +): ManagedPathBlockState => {
308 const lines = contents.split('\n');
287 - const markerIndexes = lines.flatMap((line, index) => (line.trim() === MARKER ? [index] : []));
309 + const markerIndexes = lines.flatMap((line, index) => (line === MARKER ? [index] : []));
310 const firstMarkerIndex = markerIndexes[0];
311 if (firstMarkerIndex === undefined) return { state: 'absent' };
312
313 const blockLines = pathBlock.split('\n');
292 - const expectedAssignment = (blockLines[1] ?? '').trim();
293 - if (
294 - markerIndexes.length === 1 &&
295 - (lines[firstMarkerIndex + 1] ?? '').trim() === expectedAssignment
296 - ) {
314 + if (markerIndexes.length === 1 && lines[firstMarkerIndex + 1] === blockLines[1]) {
315 return { state: 'current' };
316 }
317
@@ -307,24 +325,13 @@ const reconcileManagedPathBlock = (contents: string, pathBlock: string): Managed
325 : [markerIndex];
326 })
327 );
310 - const hasMalformedBlock = markerIndexes.some(
311 - markerIndex => !isManagedPathAssignment(lines[markerIndex + 1] ?? '')
312 - );
328
314 - if (!hasMalformedBlock) {
315 - const reconciledLines = lines.flatMap((line, index) => {
316 - if (index === firstMarkerIndex) return blockLines;
317 - return managedLineIndexes.has(index) ? [] : [line];
318 - });
319 - return { state: 'stale', reconciled: reconciledLines.join('\n') };
320 - }
321 -
322 - const keptLines = lines.flatMap((line, index) => (managedLineIndexes.has(index) ? [] : [line]));
323 - const endsWithNewline = keptLines[keptLines.length - 1] === '';
324 - const reconciledLines = endsWithNewline
325 - ? [...keptLines.slice(0, -1), ...blockLines, '']
326 - : [...keptLines, ...blockLines];
327 - return { state: 'stale', reconciled: reconciledLines.join('\n') };
329 + // Like the awk rewrite, kept content is a sequence of newline-terminated
330 + // lines: drop the split's trailing sentinel and re-terminate the last line.
331 + const keptLines = lines.filter((_, index) => !managedLineIndexes.has(index));
332 + const contentLines = keptLines[keptLines.length - 1] === '' ? keptLines.slice(0, -1) : keptLines;
333 + const kept = contentLines.length === 0 ? '' : contentLines.join('\n') + '\n';
334 + return { state: 'stale', reconciled: appendManagedBlocks(kept, [pathBlock]) };
335 };
336
337 interface ManagedBlockReplacement {
@@ -425,8 +432,11 @@ const appendQueuedBlocks = (params: {
432 )
433 );
434
428 - const appendContent = '\n' + blocks.join('\n\n') + '\n';
429 - yield* replaceFilePreservingMode(fs, writeTarget, existingContents + appendContent);
435 + yield* replaceFilePreservingMode(
436 + fs,
437 + writeTarget,
438 + appendManagedBlocks(existingContents, blocks)
439 + );
440 yield* params.report(`Updated ${tildify(filePath, params.homedir)}`);
441 }
442 });
ts/packages/cli/test/src/install-managed-block-conformance.test.ts new
+58
@@ -0,0 +1,58 @@
1 +import { readdirSync, readFileSync } from 'node:fs';
2 +import { describe, expect, it } from 'vitest';
3 +import {
4 + appendManagedBlocks,
5 + pathBlockForShell,
6 + reconcileManagedPathBlock,
7 + type Shell,
8 +} from 'src/commands/install.cmd';
9 +
10 +/**
11 + * Managed PATH block contract conformance.
12 + *
13 + * The fixtures under test/managed-block-fixtures also drive install.sh's
14 + * write_path_block through the shell harness
15 + * (test/install-sh-release-resolution.test.sh), so an edit to either
16 + * implementation fails one of the two suites until the other side produces
17 + * byte-identical output again. Each fixture directory holds the rc file
18 + * `before` the run, the raw `bin-dir` (with `__HOME__` standing in for the
19 + * home directory), the target `shell`, and the expected `after` bytes.
20 + */
21 +
22 +const FIXTURES_URL = new URL('../../../../../test/managed-block-fixtures/', import.meta.url);
23 +const HOME = '/home/conformance';
24 +
25 +const readFixture = (caseName: string, file: string): string =>
26 + readFileSync(new URL(`${caseName}/${file}`, FIXTURES_URL), 'utf8');
27 +
28 +const fixtureNames = readdirSync(FIXTURES_URL, { withFileTypes: true })
29 + .filter(entry => entry.isDirectory())
30 + .map(entry => entry.name)
31 + .sort();
32 +
33 +describe('managed PATH block conformance with install.sh', () => {
34 + it('finds the shared fixtures', () => {
35 + expect(fixtureNames.length).toBeGreaterThan(0);
36 + });
37 +
38 + it.each(fixtureNames)('reconciles %s byte-identically to install.sh', caseName => {
39 + const binDir = readFixture(caseName, 'bin-dir').trim().replace('__HOME__', HOME);
40 + const shell = readFixture(caseName, 'shell').trim() as Shell;
41 + const before = readFixture(caseName, 'before');
42 + const expected = readFixture(caseName, 'after');
43 +
44 + // Mirror installShellIntegration's write flow: a current block leaves the
45 + // file untouched, a stale block is reconciled, and an absent block is
46 + // appended after the existing content.
47 + const pathBlock = pathBlockForShell(shell, binDir, HOME);
48 + const reconciliation = reconcileManagedPathBlock(before, pathBlock);
49 + const after =
50 + reconciliation.state === 'current'
51 + ? before
52 + : reconciliation.state === 'stale'
53 + ? reconciliation.reconciled
54 + : appendManagedBlocks(before, [pathBlock]);
55 +
56 + expect(after).toBe(expected);
57 + });
58 +});