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): align the TS and sh managed-block reconcilers on one contract
jkomyno124e896ed799
diff --git a/.gitattributes b/.gitattributesindex b5c610492..8f1cb5722 100644--- a/.gitattributes+++ b/.gitattributes@@ -14,3 +14,7 @@ ts/packages/core/pack/generated/** linguist-generated=truepnpm-lock.yaml linguist-generated=trueuv.lock linguist-generated=true**/bun.lock linguist-generated=true++# Managed-block conformance fixtures are byte-exact inputs/outputs shared by+# the sh harness and the TS reconciler test; never normalize their line endings.+test/managed-block-fixtures/** -textdiff --git a/install.sh b/install.shindex a4f61a39e..c7ea46082 100755--- a/install.sh+++ b/install.sh@@ -368,9 +368,17 @@ is_unsafe_path() {return 1}+# Renders the home directory itself and paths under it with a literal $HOME+# prefix. Must stay in lockstep with the CLI's renderWithHome (install.cmd.ts):+# delegated_setup_verified compares the CLI-written line against this rendering+# byte for byte, so any disagreement makes every delegated install look stale+# and forces a needless inline rewrite.render_bin_dir() {render_bin_dir_value=$1- case $render_bin_dir_value in "$HOME"/*) render_bin_dir_value=\$HOME/${render_bin_dir_value#"$HOME"/} ;; esac+ case $render_bin_dir_value in+ "$HOME") render_bin_dir_value=\$HOME ;;+ "$HOME"/*) render_bin_dir_value=\$HOME/${render_bin_dir_value#"$HOME"/} ;;+ esacprintf '%s\n' "$render_bin_dir_value"}diff --git a/test/install-sh-release-resolution.test.sh b/test/install-sh-release-resolution.test.shindex 973ee921f..9a6614761 100755--- a/test/install-sh-release-resolution.test.sh+++ b/test/install-sh-release-resolution.test.sh@@ -180,6 +180,49 @@ sanitize_path() {}ambient_path=$(sanitize_path "$PATH")+# TS/sh managed-block contract conformance: the fixtures under+# test/managed-block-fixtures also drive the CLI's reconciler+# (ts/packages/cli/test/src/install-managed-block-conformance.test.ts), so an+# edit to either implementation fails one of the two suites until the other+# side produces byte-identical output again.+conformance_fixtures="$repo_root/test/managed-block-fixtures"+conformance_functions="$suite_tmp/install-sh-functions.sh"+# The entry-point guard above pins install.sh to end with exactly one+# `main "$@"` line, so dropping the final line leaves only definitions.+sed '$d' "$repo_root/install.sh" >"$conformance_functions"+conformance_driver="$suite_tmp/conformance-driver.sh"+cat >"$conformance_driver" <<'EOF'+#!/bin/sh+set -eu+functions_file=$1+rc_file=$2+shell_name=$3+bin_dir=$4+. "$functions_file"+rendered=$(render_bin_dir "$bin_dir")+write_path_block "$rc_file" "$shell_name" "$rendered"+EOF+[[ -d $conformance_fixtures ]] || fail 'managed-block fixtures directory is missing'+for interpreter in "${interpreters[@]}"; do+ interpreter_name=$(basename "$interpreter")+ for fixture_dir in "$conformance_fixtures"/*/; do+ fixture_name=$(basename "$fixture_dir")+ fixture_home="$suite_tmp/conformance-$interpreter_name/$fixture_name"+ mkdir -p "$fixture_home"+ cp "$fixture_dir/before" "$fixture_home/rcfile"+ fixture_bin_dir=$(sed "s|__HOME__|$fixture_home|" "$fixture_dir/bin-dir")+ fixture_shell=$(<"$fixture_dir/shell")+ env HOME="$fixture_home" COMPOSIO_QUIET=1 \+ "$interpreter" "$conformance_driver" "$conformance_functions" \+ "$fixture_home/rcfile" "$fixture_shell" "$fixture_bin_dir" ||+ fail "$interpreter_name managed-block conformance: $fixture_name (driver failed)"+ cmp -s "$fixture_dir/after" "$fixture_home/rcfile" || {+ diff -u "$fixture_dir/after" "$fixture_home/rcfile" >&2 || :+ fail "$interpreter_name managed-block conformance: $fixture_name (output drifted)"+ }+ done+done+for interpreter in "${interpreters[@]}"; dointerpreter_name=$(basename "$interpreter")case_root="$suite_tmp/$interpreter_name"diff --git a/test/managed-block-fixtures/absent-append-fish/after b/test/managed-block-fixtures/absent-append-fish/afternew file mode 100644index 000000000..0088060a3--- /dev/null+++ b/test/managed-block-fixtures/absent-append-fish/after@@ -0,0 +1,4 @@+# fish config++# Composio CLI+set --export PATH "$HOME/.local/bin" $PATHdiff --git a/test/managed-block-fixtures/absent-append-fish/before b/test/managed-block-fixtures/absent-append-fish/beforenew file mode 100644index 000000000..a8c1ac36b--- /dev/null+++ b/test/managed-block-fixtures/absent-append-fish/before@@ -0,0 +1 @@+# fish configdiff --git a/test/managed-block-fixtures/absent-append-fish/bin-dir b/test/managed-block-fixtures/absent-append-fish/bin-dirnew file mode 100644index 000000000..4b54df1d6--- /dev/null+++ b/test/managed-block-fixtures/absent-append-fish/bin-dir@@ -0,0 +1 @@+__HOME__/.local/bindiff --git a/test/managed-block-fixtures/absent-append-fish/shell b/test/managed-block-fixtures/absent-append-fish/shellnew file mode 100644index 000000000..568650a54--- /dev/null+++ b/test/managed-block-fixtures/absent-append-fish/shell@@ -0,0 +1 @@+fishdiff --git a/test/managed-block-fixtures/absent-append-zsh/after b/test/managed-block-fixtures/absent-append-zsh/afternew file mode 100644index 000000000..0c04748ed--- /dev/null+++ b/test/managed-block-fixtures/absent-append-zsh/after@@ -0,0 +1,5 @@+# my rc+alias ll='ls -la'++# Composio CLI+export PATH="$HOME/.local/bin:$PATH"diff --git a/test/managed-block-fixtures/absent-append-zsh/before b/test/managed-block-fixtures/absent-append-zsh/beforenew file mode 100644index 000000000..a039b225b--- /dev/null+++ b/test/managed-block-fixtures/absent-append-zsh/before@@ -0,0 +1,2 @@+# my rc+alias ll='ls -la'diff --git a/test/managed-block-fixtures/absent-append-zsh/bin-dir b/test/managed-block-fixtures/absent-append-zsh/bin-dirnew file mode 100644index 000000000..4b54df1d6--- /dev/null+++ b/test/managed-block-fixtures/absent-append-zsh/bin-dir@@ -0,0 +1 @@+__HOME__/.local/bindiff --git a/test/managed-block-fixtures/absent-append-zsh/shell b/test/managed-block-fixtures/absent-append-zsh/shellnew file mode 100644index 000000000..0abc84369--- /dev/null+++ b/test/managed-block-fixtures/absent-append-zsh/shell@@ -0,0 +1 @@+zshdiff --git a/test/managed-block-fixtures/crlf-marker-untouched-zsh/after b/test/managed-block-fixtures/crlf-marker-untouched-zsh/afternew file mode 100644index 000000000..45b830cdb--- /dev/null+++ b/test/managed-block-fixtures/crlf-marker-untouched-zsh/after@@ -0,0 +1,5 @@+# Composio CLI+export PATH="/stale/bin:$PATH"++# Composio CLI+export PATH="$HOME/.local/bin:$PATH"diff --git a/test/managed-block-fixtures/crlf-marker-untouched-zsh/before b/test/managed-block-fixtures/crlf-marker-untouched-zsh/beforenew file mode 100644index 000000000..c3d211877--- /dev/null+++ b/test/managed-block-fixtures/crlf-marker-untouched-zsh/before@@ -0,0 +1,2 @@+# Composio CLI+export PATH="/stale/bin:$PATH"diff --git a/test/managed-block-fixtures/crlf-marker-untouched-zsh/bin-dir b/test/managed-block-fixtures/crlf-marker-untouched-zsh/bin-dirnew file mode 100644index 000000000..4b54df1d6--- /dev/null+++ b/test/managed-block-fixtures/crlf-marker-untouched-zsh/bin-dir@@ -0,0 +1 @@+__HOME__/.local/bindiff --git a/test/managed-block-fixtures/crlf-marker-untouched-zsh/shell b/test/managed-block-fixtures/crlf-marker-untouched-zsh/shellnew file mode 100644index 000000000..0abc84369--- /dev/null+++ b/test/managed-block-fixtures/crlf-marker-untouched-zsh/shell@@ -0,0 +1 @@+zshdiff --git a/test/managed-block-fixtures/current-noop-zsh/after b/test/managed-block-fixtures/current-noop-zsh/afternew file mode 100644index 000000000..e4dbac6f5--- /dev/null+++ b/test/managed-block-fixtures/current-noop-zsh/after@@ -0,0 +1,4 @@+# existing config+# Composio CLI+export PATH="$HOME/.local/bin:$PATH"+# more configdiff --git a/test/managed-block-fixtures/current-noop-zsh/before b/test/managed-block-fixtures/current-noop-zsh/beforenew file mode 100644index 000000000..e4dbac6f5--- /dev/null+++ b/test/managed-block-fixtures/current-noop-zsh/before@@ -0,0 +1,4 @@+# existing config+# Composio CLI+export PATH="$HOME/.local/bin:$PATH"+# more configdiff --git a/test/managed-block-fixtures/current-noop-zsh/bin-dir b/test/managed-block-fixtures/current-noop-zsh/bin-dirnew file mode 100644index 000000000..4b54df1d6--- /dev/null+++ b/test/managed-block-fixtures/current-noop-zsh/bin-dir@@ -0,0 +1 @@+__HOME__/.local/bindiff --git a/test/managed-block-fixtures/current-noop-zsh/shell b/test/managed-block-fixtures/current-noop-zsh/shellnew file mode 100644index 000000000..0abc84369--- /dev/null+++ b/test/managed-block-fixtures/current-noop-zsh/shell@@ -0,0 +1 @@+zshdiff --git a/test/managed-block-fixtures/duplicate-collapse-zsh/after b/test/managed-block-fixtures/duplicate-collapse-zsh/afternew file mode 100644index 000000000..b45d0e755--- /dev/null+++ b/test/managed-block-fixtures/duplicate-collapse-zsh/after@@ -0,0 +1,4 @@+# unmanaged line++# Composio CLI+export PATH="$HOME/.local/bin:$PATH"diff --git a/test/managed-block-fixtures/duplicate-collapse-zsh/before b/test/managed-block-fixtures/duplicate-collapse-zsh/beforenew file mode 100644index 000000000..32bde744b--- /dev/null+++ b/test/managed-block-fixtures/duplicate-collapse-zsh/before@@ -0,0 +1,5 @@+# Composio CLI+export PATH="/stale/bin:$PATH"+# unmanaged line+# Composio CLI+export PATH="$HOME/.local/bin:$PATH"diff --git a/test/managed-block-fixtures/duplicate-collapse-zsh/bin-dir b/test/managed-block-fixtures/duplicate-collapse-zsh/bin-dirnew file mode 100644index 000000000..4b54df1d6--- /dev/null+++ b/test/managed-block-fixtures/duplicate-collapse-zsh/bin-dir@@ -0,0 +1 @@+__HOME__/.local/bindiff --git a/test/managed-block-fixtures/duplicate-collapse-zsh/shell b/test/managed-block-fixtures/duplicate-collapse-zsh/shellnew file mode 100644index 000000000..0abc84369--- /dev/null+++ b/test/managed-block-fixtures/duplicate-collapse-zsh/shell@@ -0,0 +1 @@+zshdiff --git a/test/managed-block-fixtures/empty-file-zsh/after b/test/managed-block-fixtures/empty-file-zsh/afternew file mode 100644index 000000000..963fc9f8e--- /dev/null+++ b/test/managed-block-fixtures/empty-file-zsh/after@@ -0,0 +1,3 @@++# Composio CLI+export PATH="$HOME/.local/bin:$PATH"diff --git a/test/managed-block-fixtures/empty-file-zsh/before b/test/managed-block-fixtures/empty-file-zsh/beforenew file mode 100644index 000000000..e69de29bbdiff --git a/test/managed-block-fixtures/empty-file-zsh/bin-dir b/test/managed-block-fixtures/empty-file-zsh/bin-dirnew file mode 100644index 000000000..4b54df1d6--- /dev/null+++ b/test/managed-block-fixtures/empty-file-zsh/bin-dir@@ -0,0 +1 @@+__HOME__/.local/bindiff --git a/test/managed-block-fixtures/empty-file-zsh/shell b/test/managed-block-fixtures/empty-file-zsh/shellnew file mode 100644index 000000000..0abc84369--- /dev/null+++ b/test/managed-block-fixtures/empty-file-zsh/shell@@ -0,0 +1 @@+zshdiff --git a/test/managed-block-fixtures/home-as-bin-dir-zsh/after b/test/managed-block-fixtures/home-as-bin-dir-zsh/afternew file mode 100644index 000000000..5e52b5b76--- /dev/null+++ b/test/managed-block-fixtures/home-as-bin-dir-zsh/after@@ -0,0 +1,4 @@+# rc prologue++# Composio CLI+export PATH="$HOME:$PATH"diff --git a/test/managed-block-fixtures/home-as-bin-dir-zsh/before b/test/managed-block-fixtures/home-as-bin-dir-zsh/beforenew file mode 100644index 000000000..c4e3b9d89--- /dev/null+++ b/test/managed-block-fixtures/home-as-bin-dir-zsh/before@@ -0,0 +1 @@+# rc prologuediff --git a/test/managed-block-fixtures/home-as-bin-dir-zsh/bin-dir b/test/managed-block-fixtures/home-as-bin-dir-zsh/bin-dirnew file mode 100644index 000000000..6216e0c11--- /dev/null+++ b/test/managed-block-fixtures/home-as-bin-dir-zsh/bin-dir@@ -0,0 +1 @@+__HOME__diff --git a/test/managed-block-fixtures/home-as-bin-dir-zsh/shell b/test/managed-block-fixtures/home-as-bin-dir-zsh/shellnew file mode 100644index 000000000..0abc84369--- /dev/null+++ b/test/managed-block-fixtures/home-as-bin-dir-zsh/shell@@ -0,0 +1 @@+zshdiff --git a/test/managed-block-fixtures/malformed-marker-comment-zsh/after b/test/managed-block-fixtures/malformed-marker-comment-zsh/afternew file mode 100644index 000000000..3e4509958--- /dev/null+++ b/test/managed-block-fixtures/malformed-marker-comment-zsh/after@@ -0,0 +1,6 @@+# added by my dotfiles+export PATH="/stale/bin:$PATH"+# user epilogue++# Composio CLI+export PATH="$HOME/.local/bin:$PATH"diff --git a/test/managed-block-fixtures/malformed-marker-comment-zsh/before b/test/managed-block-fixtures/malformed-marker-comment-zsh/beforenew file mode 100644index 000000000..2add51b97--- /dev/null+++ b/test/managed-block-fixtures/malformed-marker-comment-zsh/before@@ -0,0 +1,4 @@+# Composio CLI+# added by my dotfiles+export PATH="/stale/bin:$PATH"+# user epiloguediff --git a/test/managed-block-fixtures/malformed-marker-comment-zsh/bin-dir b/test/managed-block-fixtures/malformed-marker-comment-zsh/bin-dirnew file mode 100644index 000000000..4b54df1d6--- /dev/null+++ b/test/managed-block-fixtures/malformed-marker-comment-zsh/bin-dir@@ -0,0 +1 @@+__HOME__/.local/bindiff --git a/test/managed-block-fixtures/malformed-marker-comment-zsh/shell b/test/managed-block-fixtures/malformed-marker-comment-zsh/shellnew file mode 100644index 000000000..0abc84369--- /dev/null+++ b/test/managed-block-fixtures/malformed-marker-comment-zsh/shell@@ -0,0 +1 @@+zshdiff --git a/test/managed-block-fixtures/no-final-newline-append-zsh/after b/test/managed-block-fixtures/no-final-newline-append-zsh/afternew file mode 100644index 000000000..c23eddd12--- /dev/null+++ b/test/managed-block-fixtures/no-final-newline-append-zsh/after@@ -0,0 +1,4 @@+alias ll="ls -la"++# Composio CLI+export PATH="$HOME/.local/bin:$PATH"diff --git a/test/managed-block-fixtures/no-final-newline-append-zsh/before b/test/managed-block-fixtures/no-final-newline-append-zsh/beforenew file mode 100644index 000000000..42604cd21--- /dev/null+++ b/test/managed-block-fixtures/no-final-newline-append-zsh/before@@ -0,0 +1 @@+alias ll="ls -la"\ No newline at end of filediff --git a/test/managed-block-fixtures/no-final-newline-append-zsh/bin-dir b/test/managed-block-fixtures/no-final-newline-append-zsh/bin-dirnew file mode 100644index 000000000..4b54df1d6--- /dev/null+++ b/test/managed-block-fixtures/no-final-newline-append-zsh/bin-dir@@ -0,0 +1 @@+__HOME__/.local/bindiff --git a/test/managed-block-fixtures/no-final-newline-append-zsh/shell b/test/managed-block-fixtures/no-final-newline-append-zsh/shellnew file mode 100644index 000000000..0abc84369--- /dev/null+++ b/test/managed-block-fixtures/no-final-newline-append-zsh/shell@@ -0,0 +1 @@+zshdiff --git a/test/managed-block-fixtures/stale-replace-zsh/after b/test/managed-block-fixtures/stale-replace-zsh/afternew file mode 100644index 000000000..2ed45beec--- /dev/null+++ b/test/managed-block-fixtures/stale-replace-zsh/after@@ -0,0 +1,6 @@+# user prologue+# user epilogue+alias ll="ls -la"++# Composio CLI+export PATH="$HOME/.local/bin:$PATH"diff --git a/test/managed-block-fixtures/stale-replace-zsh/before b/test/managed-block-fixtures/stale-replace-zsh/beforenew file mode 100644index 000000000..3826b6036--- /dev/null+++ b/test/managed-block-fixtures/stale-replace-zsh/before@@ -0,0 +1,5 @@+# user prologue+# Composio CLI+export PATH="/stale/bin:$PATH"+# user epilogue+alias ll="ls -la"diff --git a/test/managed-block-fixtures/stale-replace-zsh/bin-dir b/test/managed-block-fixtures/stale-replace-zsh/bin-dirnew file mode 100644index 000000000..4b54df1d6--- /dev/null+++ b/test/managed-block-fixtures/stale-replace-zsh/bin-dir@@ -0,0 +1 @@+__HOME__/.local/bindiff --git a/test/managed-block-fixtures/stale-replace-zsh/shell b/test/managed-block-fixtures/stale-replace-zsh/shellnew file mode 100644index 000000000..0abc84369--- /dev/null+++ b/test/managed-block-fixtures/stale-replace-zsh/shell@@ -0,0 +1 @@+zshdiff --git a/test/managed-block-fixtures/stale-whitespace-assignment-zsh/after b/test/managed-block-fixtures/stale-whitespace-assignment-zsh/afternew file mode 100644index 000000000..7c7601bc7--- /dev/null+++ b/test/managed-block-fixtures/stale-whitespace-assignment-zsh/after@@ -0,0 +1,4 @@+# user prologue++# Composio CLI+export PATH="$HOME/.local/bin:$PATH"diff --git a/test/managed-block-fixtures/stale-whitespace-assignment-zsh/before b/test/managed-block-fixtures/stale-whitespace-assignment-zsh/beforenew file mode 100644index 000000000..a5d311f14--- /dev/null+++ b/test/managed-block-fixtures/stale-whitespace-assignment-zsh/before@@ -0,0 +1,3 @@+# user prologue+# Composio CLI+export PATH="$HOME/.local/bin:$PATH"diff --git a/test/managed-block-fixtures/stale-whitespace-assignment-zsh/bin-dir b/test/managed-block-fixtures/stale-whitespace-assignment-zsh/bin-dirnew file mode 100644index 000000000..4b54df1d6--- /dev/null+++ b/test/managed-block-fixtures/stale-whitespace-assignment-zsh/bin-dir@@ -0,0 +1 @@+__HOME__/.local/bindiff --git a/test/managed-block-fixtures/stale-whitespace-assignment-zsh/shell b/test/managed-block-fixtures/stale-whitespace-assignment-zsh/shellnew file mode 100644index 000000000..0abc84369--- /dev/null+++ b/test/managed-block-fixtures/stale-whitespace-assignment-zsh/shell@@ -0,0 +1 @@+zshdiff --git a/test/managed-block-fixtures/trailing-space-marker-untouched-zsh/after b/test/managed-block-fixtures/trailing-space-marker-untouched-zsh/afternew file mode 100644index 000000000..f649e5620--- /dev/null+++ b/test/managed-block-fixtures/trailing-space-marker-untouched-zsh/after@@ -0,0 +1,5 @@+# Composio CLI+export PATH="/stale/bin:$PATH"++# Composio CLI+export PATH="$HOME/.local/bin:$PATH"diff --git a/test/managed-block-fixtures/trailing-space-marker-untouched-zsh/before b/test/managed-block-fixtures/trailing-space-marker-untouched-zsh/beforenew file mode 100644index 000000000..742793f0c--- /dev/null+++ b/test/managed-block-fixtures/trailing-space-marker-untouched-zsh/before@@ -0,0 +1,2 @@+# Composio CLI+export PATH="/stale/bin:$PATH"diff --git a/test/managed-block-fixtures/trailing-space-marker-untouched-zsh/bin-dir b/test/managed-block-fixtures/trailing-space-marker-untouched-zsh/bin-dirnew file mode 100644index 000000000..4b54df1d6--- /dev/null+++ b/test/managed-block-fixtures/trailing-space-marker-untouched-zsh/bin-dir@@ -0,0 +1 @@+__HOME__/.local/bindiff --git a/test/managed-block-fixtures/trailing-space-marker-untouched-zsh/shell b/test/managed-block-fixtures/trailing-space-marker-untouched-zsh/shellnew file mode 100644index 000000000..0abc84369--- /dev/null+++ b/test/managed-block-fixtures/trailing-space-marker-untouched-zsh/shell@@ -0,0 +1 @@+zshdiff --git a/ts/packages/cli/src/commands/install.cmd.ts b/ts/packages/cli/src/commands/install.cmd.tsindex 81c0ecbbb..4aaeb8ba4 100644--- a/ts/packages/cli/src/commands/install.cmd.ts+++ b/ts/packages/cli/src/commands/install.cmd.ts@@ -138,15 +138,19 @@ const resolveBinDir = (params: {/*** Render a path for embedding in a shell rc file. `~` never expands inside* double quotes, so home-relative paths use a literal `$HOME` prefix instead.+ * Must stay in lockstep with install.sh's render_bin_dir: the installer's+ * delegated_setup_verified compares its own rendering against the line this+ * command wrote byte for byte, so any disagreement makes every delegated+ * install look stale and forces a needless inline rewrite.*/-const renderWithHome = (dir: string, homedir: string): string =>+export const renderWithHome = (dir: string, homedir: string): string =>dir === homedir? '$HOME': dir.startsWith(`${homedir}/`)? `$HOME/${dir.slice(homedir.length + 1)}`: dir;-const pathBlockForShell = (shell: Shell, binDir: string, homedir: string): string => {+export const pathBlockForShell = (shell: Shell, binDir: string, homedir: string): string => {const rendered = renderWithHome(binDir, homedir);return shell === 'fish'? [MARKER, `set --export PATH "${rendered}" $PATH`].join('\n')@@ -242,9 +246,13 @@ const buildShellConfig = (completionBlock: completionScript ? `${COMPLETIONS_MARKER}\n${completionScript}` : undefined,});-/** Check whether a file already contains a given marker line. */+/**+ * Check whether a file already contains a given marker line. Byte-exact, like+ * install.sh's awk `$0 == "# Composio CLI"` match: a marker mangled by CRLF+ * endings or stray whitespace is not ours and is left alone on both sides.+ */const fileContains = (contents: string, marker: string): boolean =>- contents.split('\n').some(line => line.trim() === marker.trim());+ contents.split('\n').some(line => line === marker);/** Outcome of reconciling a startup file's managed PATH block against the resolved bin dir. */type ManagedPathBlockState =@@ -266,34 +274,44 @@ const isManagedPathAssignment = (line: string): boolean => {);};+/**+ * Append managed blocks after existing content, separated by one blank line+ * and ending with a newline. Content lacking a final newline gains one first,+ * exactly as install.sh's line-based awk rewrite normalizes it.+ */+export const appendManagedBlocks = (contents: string, blocks: readonly string[]): string => {+ const base = contents === '' || contents.endsWith('\n') ? contents : `${contents}\n`;+ return `${base}\n${blocks.join('\n\n')}\n`;+};+/*** Reconcile the managed PATH block — the `MARKER` line plus the PATH* assignment right below it — with the currently resolved bin directory.- * A well-formed block recording a stale bin dir is replaced in place,- * duplicate managed PATH blocks collapse into a single current block, and- * unmanaged content (including the completions block) is left byte-for-byte- * untouched.*- * A marker whose next line is not a recognizable managed assignment (a user- * annotation, a blank line, another marker) is malformed: only the marker- * line itself is removed, and the fresh block is appended after all remaining- * content instead of replacing in place. That guarantees no user content is- * ever deleted, and — because these prepend-style assignments make the- * last-sourced line win — the new bin dir still takes PATH precedence over- * any orphaned stale assignment left behind.+ * This mirrors install.sh's write_path_block awk program step for step, and+ * the shared fixtures under test/managed-block-fixtures pin both+ * implementations to byte-identical outputs — any behavioral edit here must+ * land in install.sh too. A file whose single byte-exact marker is directly+ * followed by the expected assignment is current and left untouched.+ * Otherwise every managed line — each marker, plus the next line only when it+ * is a recognizable managed PATH assignment; anything else (a user+ * annotation, a blank line, another marker) is not ours to delete — is+ * removed and one fresh block is appended after the remaining content. No+ * user content is ever deleted, and because these prepend-style assignments+ * make the last-sourced line win, the new bin dir takes PATH precedence over+ * any orphaned stale assignment that had to be preserved.*/-const reconcileManagedPathBlock = (contents: string, pathBlock: string): ManagedPathBlockState => {+export const reconcileManagedPathBlock = (+ contents: string,+ pathBlock: string+): ManagedPathBlockState => {const lines = contents.split('\n');- const markerIndexes = lines.flatMap((line, index) => (line.trim() === MARKER ? [index] : []));+ const markerIndexes = lines.flatMap((line, index) => (line === MARKER ? [index] : []));const firstMarkerIndex = markerIndexes[0];if (firstMarkerIndex === undefined) return { state: 'absent' };const blockLines = pathBlock.split('\n');- const expectedAssignment = (blockLines[1] ?? '').trim();- if (- markerIndexes.length === 1 &&- (lines[firstMarkerIndex + 1] ?? '').trim() === expectedAssignment- ) {+ if (markerIndexes.length === 1 && lines[firstMarkerIndex + 1] === blockLines[1]) {return { state: 'current' };}@@ -307,24 +325,13 @@ const reconcileManagedPathBlock = (contents: string, pathBlock: string): Managed: [markerIndex];}));- const hasMalformedBlock = markerIndexes.some(- markerIndex => !isManagedPathAssignment(lines[markerIndex + 1] ?? '')- );- if (!hasMalformedBlock) {- const reconciledLines = lines.flatMap((line, index) => {- if (index === firstMarkerIndex) return blockLines;- return managedLineIndexes.has(index) ? [] : [line];- });- return { state: 'stale', reconciled: reconciledLines.join('\n') };- }-- const keptLines = lines.flatMap((line, index) => (managedLineIndexes.has(index) ? [] : [line]));- const endsWithNewline = keptLines[keptLines.length - 1] === '';- const reconciledLines = endsWithNewline- ? [...keptLines.slice(0, -1), ...blockLines, '']- : [...keptLines, ...blockLines];- return { state: 'stale', reconciled: reconciledLines.join('\n') };+ // Like the awk rewrite, kept content is a sequence of newline-terminated+ // lines: drop the split's trailing sentinel and re-terminate the last line.+ const keptLines = lines.filter((_, index) => !managedLineIndexes.has(index));+ const contentLines = keptLines[keptLines.length - 1] === '' ? keptLines.slice(0, -1) : keptLines;+ const kept = contentLines.length === 0 ? '' : contentLines.join('\n') + '\n';+ return { state: 'stale', reconciled: appendManagedBlocks(kept, [pathBlock]) };};interface ManagedBlockReplacement {@@ -425,8 +432,11 @@ const appendQueuedBlocks = (params: {));- const appendContent = '\n' + blocks.join('\n\n') + '\n';- yield* replaceFilePreservingMode(fs, writeTarget, existingContents + appendContent);+ yield* replaceFilePreservingMode(+ fs,+ writeTarget,+ appendManagedBlocks(existingContents, blocks)+ );yield* params.report(`Updated ${tildify(filePath, params.homedir)}`);}});diff --git a/ts/packages/cli/test/src/install-managed-block-conformance.test.ts b/ts/packages/cli/test/src/install-managed-block-conformance.test.tsnew file mode 100644index 000000000..5595943af--- /dev/null+++ b/ts/packages/cli/test/src/install-managed-block-conformance.test.ts@@ -0,0 +1,58 @@+import { readdirSync, readFileSync } from 'node:fs';+import { describe, expect, it } from 'vitest';+import {+ appendManagedBlocks,+ pathBlockForShell,+ reconcileManagedPathBlock,+ type Shell,+} from 'src/commands/install.cmd';++/**+ * Managed PATH block contract conformance.+ *+ * The fixtures under test/managed-block-fixtures also drive install.sh's+ * write_path_block through the shell harness+ * (test/install-sh-release-resolution.test.sh), so an edit to either+ * implementation fails one of the two suites until the other side produces+ * byte-identical output again. Each fixture directory holds the rc file+ * `before` the run, the raw `bin-dir` (with `__HOME__` standing in for the+ * home directory), the target `shell`, and the expected `after` bytes.+ */++const FIXTURES_URL = new URL('../../../../../test/managed-block-fixtures/', import.meta.url);+const HOME = '/home/conformance';++const readFixture = (caseName: string, file: string): string =>+ readFileSync(new URL(`${caseName}/${file}`, FIXTURES_URL), 'utf8');++const fixtureNames = readdirSync(FIXTURES_URL, { withFileTypes: true })+ .filter(entry => entry.isDirectory())+ .map(entry => entry.name)+ .sort();++describe('managed PATH block conformance with install.sh', () => {+ it('finds the shared fixtures', () => {+ expect(fixtureNames.length).toBeGreaterThan(0);+ });++ it.each(fixtureNames)('reconciles %s byte-identically to install.sh', caseName => {+ const binDir = readFixture(caseName, 'bin-dir').trim().replace('__HOME__', HOME);+ const shell = readFixture(caseName, 'shell').trim() as Shell;+ const before = readFixture(caseName, 'before');+ const expected = readFixture(caseName, 'after');++ // Mirror installShellIntegration's write flow: a current block leaves the+ // file untouched, a stale block is reconciled, and an absent block is+ // appended after the existing content.+ const pathBlock = pathBlockForShell(shell, binDir, HOME);+ const reconciliation = reconcileManagedPathBlock(before, pathBlock);+ const after =+ reconciliation.state === 'current'+ ? before+ : reconciliation.state === 'stale'+ ? reconciliation.reconciled+ : appendManagedBlocks(before, [pathBlock]);++ expect(after).toBe(expected);+ });+});