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): install bundles with atomic replacement
jkomyno committed
Aug 1, 2026 at 19:10 UTC
94c21f2ed10b12dc975ba14387645a7d645b6cd0
4 files changed
+273
-6
.github/workflows/cli.test-installation.yml
+3
@@ -33,6 +33,9 @@ jobs:
33
- name: Test release resolution fallback
34
run: bash test/install-sh-release-resolution.test.sh
35
36
+ - name: Test atomic install replacement
37
+ run: bash test/install-sh-atomic-replace.test.sh
38
+
39
- name: Lint POSIX install scripts
40
run: shellcheck -s sh install.sh install/*.sh
41
install.sh
+54
-5
@@ -234,6 +234,32 @@ resolve_directory() {
234
(cd "$resolve_directory_value" && pwd -P)
235
}
236
237
+publish_staged_entry() {
238
+ publish_source=$1
239
+ publish_name=${publish_source##*/}
240
+ publish_target=$resolved_install_dir/$publish_name
241
+
242
+ if [ -d "$publish_source" ] && [ ! -L "$publish_source" ] &&
243
+ { [ -e "$publish_target" ] || [ -L "$publish_target" ]; }; then
244
+ publish_aside=$stage/.composio-aside.$publish_name
245
+ mv "$publish_target" "$publish_aside" ||
246
+ error "Failed to move existing install entry aside: $publish_target"
247
+ if mv "$publish_source" "$publish_target"; then
248
+ rm -rf "$publish_aside" || error "Failed to remove old install entry: $publish_aside"
249
+ return 0
250
+ fi
251
+
252
+ if mv "$publish_aside" "$publish_target"; then
253
+ error "Failed to publish install entry: $publish_target"
254
+ fi
255
+
256
+ preserve_stage=1
257
+ error "Failed to publish install entry and restore the previous entry. Recover it from $publish_aside"
258
+ fi
259
+
260
+ mv "$publish_source" "$publish_target" || error "Failed to publish install entry: $publish_target"
261
+}
262
+
263
install_bundle() {
264
install_bundle_root=$1
265
install_bundle_dir=$install_bundle_root/composio-$target
@@ -248,11 +274,31 @@ install_bundle() {
274
warn 'This release archive has no bundled support files. Some CLI features may be unavailable.'
275
fi
276
251
- cp -Rp "$install_bundle_dir"/. "$resolved_install_dir/" ||
252
- error "Failed to install the CLI bundle to \"$resolved_install_dir\""
253
- chmod +x "$resolved_install_dir/composio" || error 'Failed to set permissions on executable'
254
- printf '%s\n' "$version" >"$resolved_install_dir/release-tag.txt" ||
255
- error "Failed to write install metadata to \"$resolved_install_dir/release-tag.txt\""
277
+ stage=$(mktemp -d "$resolved_install_dir/.composio-install.XXXXXX") ||
278
+ error "Failed to create staging directory in \"$resolved_install_dir\""
279
+ debug "install staging directory: $stage"
280
+
281
+ # Staging briefly holds a second copy of the bundle until the binary is published last.
282
+ cp -Rp "$install_bundle_dir"/. "$stage/" ||
283
+ error "Failed to stage the CLI bundle in \"$resolved_install_dir\""
284
+ chmod +x "$stage/composio" || error 'Failed to set permissions on staged executable'
285
+ printf '%s\n' "$version" >"$stage/release-tag.txt" ||
286
+ error "Failed to stage install metadata in \"$resolved_install_dir\""
287
+
288
+ for staged_entry in "$stage"/* "$stage"/.[!.]* "$stage"/..?*; do
289
+ if [ ! -e "$staged_entry" ] && [ ! -L "$staged_entry" ]; then
290
+ continue
291
+ fi
292
+ case ${staged_entry##*/} in
293
+ composio | release-tag.txt) continue ;;
294
+ esac
295
+ publish_staged_entry "$staged_entry"
296
+ done
297
+
298
+ publish_staged_entry "$stage/release-tag.txt"
299
+ publish_staged_entry "$stage/composio"
300
+ rmdir "$stage" || error "Failed to remove staging directory: $stage"
301
+ stage=
302
}
303
304
install_entry_point() {
@@ -323,6 +369,9 @@ cleanup() {
369
if [ -n "${tmpdir:-}" ] && [ -d "$tmpdir" ]; then
370
rm -rf "$tmpdir"
371
fi
372
+ if [ "${preserve_stage:-0}" != 1 ] && [ -n "${stage:-}" ] && [ -d "$stage" ]; then
373
+ rm -rf "$stage"
374
+ fi
375
}
376
377
main() {
package.json
+1
-1
@@ -27,7 +27,7 @@
27
"cli:record": "turbo run record --filter=./ts/packages/cli",
28
"record": "turbo run record",
29
"test": "pnpm run test:install-sh && pnpm run test:release-workflow && turbo test --filter=./ts/packages/** --filter=!@e2e-tests/* && pnpm run test:examples",
30
- "test:install-sh": "bash test/install-sh-release-resolution.test.sh",
30
+ "test:install-sh": "bash test/install-sh-release-resolution.test.sh && bash test/install-sh-atomic-replace.test.sh",
31
"test:release-workflow": "bun run test/release-workflow.test.ts",
32
"test:examples": "tsx ts/scripts/validate-examples.ts",
33
"test:e2e": "turbo test:e2e --filter='@e2e-tests/*'",
test/install-sh-atomic-replace.test.sh
new
+215
@@ -0,0 +1,215 @@
1
+#!/usr/bin/env bash
2
+set -euo pipefail
3
+
4
+repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
5
+suite_tmp="$(mktemp -d)"
6
+survivor_pid=
7
+
8
+cleanup() {
9
+ if [[ -n ${survivor_pid:-} ]]; then
10
+ kill "$survivor_pid" 2>/dev/null || true
11
+ wait "$survivor_pid" 2>/dev/null || true
12
+ fi
13
+ rm -rf "$suite_tmp"
14
+}
15
+trap cleanup EXIT
16
+
17
+fail() {
18
+ printf 'FAIL: %s\n' "$*" >&2
19
+ exit 1
20
+}
21
+
22
+stat_inode() {
23
+ if stat -f '%i' "$1" >/dev/null 2>&1; then
24
+ stat -f '%i' "$1"
25
+ else
26
+ stat -c '%i' "$1"
27
+ fi
28
+}
29
+
30
+assert_no_residue() {
31
+ local install_dir=$1
32
+ if find "$install_dir" -maxdepth 1 \( -name '.composio-install.*' -o -name '.composio-aside.*' \) -print -quit | grep -q .; then
33
+ fail "installer residue remains in $install_dir"
34
+ fi
35
+}
36
+
37
+platform=$(uname -ms)
38
+case $platform in
39
+'Darwin x86_64') target=darwin-x64 ;;
40
+'Darwin arm64') target=darwin-aarch64 ;;
41
+'Linux aarch64'|'Linux arm64') target=linux-aarch64 ;;
42
+'Linux x86_64') target=linux-x64 ;;
43
+*) fail "unsupported test platform: $platform" ;;
44
+esac
45
+
46
+fake_bin="$suite_tmp/bin"
47
+mkdir -p "$fake_bin"
48
+
49
+cat >"$fake_bin/curl" <<'EOF'
50
+#!/usr/bin/env bash
51
+set -euo pipefail
52
+output=
53
+url=
54
+while (($# > 0)); do
55
+ case $1 in
56
+ --output|-o)
57
+ output=$2
58
+ shift 2
59
+ ;;
60
+ --proto|--proto-redir)
61
+ shift 2
62
+ ;;
63
+ --fail|--silent|--show-error|--location|--progress-bar)
64
+ shift
65
+ ;;
66
+ --*) shift ;;
67
+ *)
68
+ url=$1
69
+ shift
70
+ ;;
71
+ esac
72
+done
73
+
74
+case $url in
75
+*/checksums.txt) exit 22 ;;
76
+*) printf 'fixture archive\n' >"$output" ;;
77
+esac
78
+EOF
79
+chmod +x "$fake_bin/curl"
80
+
81
+cat >"$fake_bin/unzip" <<'EOF'
82
+#!/usr/bin/env bash
83
+set -euo pipefail
84
+dest=
85
+while (($# > 0)); do
86
+ case $1 in
87
+ -d)
88
+ dest=$2
89
+ shift 2
90
+ ;;
91
+ -*d)
92
+ dest=$2
93
+ shift 2
94
+ ;;
95
+ *) shift ;;
96
+ esac
97
+done
98
+
99
+bundle="$dest/composio-$TEST_TARGET"
100
+mkdir -p "$bundle/services" "$bundle/local-tools-binaries"
101
+printf '%s\n' 'new-support' >"$bundle/run-bun.mjs"
102
+printf '%s\n' 'new-service' >"$bundle/services/example.txt"
103
+printf '%s\n' 'new-local-tool' >"$bundle/local-tools-binaries/current.txt"
104
+
105
+if [[ ${TEST_ARCHIVE_MODE:-complete} == missing-binary ]]; then
106
+ exit 0
107
+fi
108
+
109
+cat >"$bundle/composio" <<'BIN'
110
+#!/bin/sh
111
+# new-binary-marker
112
+case ${1:-} in
113
+--version|version) printf '%s\n' 'composio fixture 98.0.0' ;;
114
+*) printf '%s\n' 'new-binary' ;;
115
+esac
116
+BIN
117
+chmod +x "$bundle/composio"
118
+EOF
119
+chmod +x "$fake_bin/unzip"
120
+
121
+interpreters=("$(command -v sh)")
122
+if command -v dash >/dev/null 2>&1 && [[ $(command -v dash) != "${interpreters[0]}" ]]; then
123
+ interpreters+=("$(command -v dash)")
124
+fi
125
+
126
+version='@composio/cli@98.0.0'
127
+for interpreter in "${interpreters[@]}"; do
128
+ interpreter_name=$(basename "$interpreter")
129
+ case_root="$suite_tmp/$interpreter_name"
130
+ install_dir="$case_root/install"
131
+ home="$case_root/home"
132
+ mkdir -p "$install_dir" "$home"
133
+
134
+ run_installer() {
135
+ local archive_mode=$1
136
+ env \
137
+ PATH="$fake_bin:$PATH" \
138
+ HOME="$home" \
139
+ COMPOSIO_INSTALL_DIR="$install_dir" \
140
+ COMPOSIO_BIN_DIR="$install_dir" \
141
+ COMPOSIO_INSTALL_PLUGINS=0 \
142
+ COMPOSIO_INSTALL_HELP=0 \
143
+ COMPOSIO_QUIET=1 \
144
+ COMPOSIO_GITHUB_URL=https://github.example.test \
145
+ COMPOSIO_GITHUB_OWNER=FakeOwner \
146
+ COMPOSIO_GITHUB_REPO=fake-repo \
147
+ TEST_TARGET="$target" \
148
+ TEST_ARCHIVE_MODE="$archive_mode" \
149
+ "$interpreter" "$repo_root/install.sh" "$version"
150
+ }
151
+
152
+ run_installer complete >/dev/null 2>&1
153
+ [[ -x "$install_dir/composio" ]] || fail "$interpreter_name empty install executable"
154
+ [[ -f "$install_dir/run-bun.mjs" ]] || fail "$interpreter_name empty install support file"
155
+ [[ -f "$install_dir/services/example.txt" ]] || fail "$interpreter_name empty install service"
156
+ [[ -f "$install_dir/local-tools-binaries/current.txt" ]] ||
157
+ fail "$interpreter_name empty install local tool"
158
+ [[ $(<"$install_dir/release-tag.txt") == "$version" ]] ||
159
+ fail "$interpreter_name empty install metadata"
160
+ assert_no_residue "$install_dir"
161
+
162
+ marker="$case_root/old-process"
163
+ cat >"$install_dir/composio" <<'EOF'
164
+#!/bin/sh
165
+marker=$1
166
+: >"$marker.ready"
167
+while [ ! -f "$marker.release" ]; do
168
+ sleep 0.05
169
+done
170
+printf '%s\n' old-process >"$marker.result"
171
+EOF
172
+ chmod +x "$install_dir/composio"
173
+ printf '%s\n' stale >"$install_dir/local-tools-binaries/stale.txt"
174
+ old_inode=$(stat_inode "$install_dir/composio")
175
+ "$install_dir/composio" "$marker" &
176
+ survivor_pid=$!
177
+ for _ in {1..100}; do
178
+ [[ -f "$marker.ready" ]] && break
179
+ sleep 0.05
180
+ done
181
+ [[ -f "$marker.ready" ]] || fail "$interpreter_name old process did not start"
182
+
183
+ run_installer complete >/dev/null 2>&1
184
+ new_inode=$(stat_inode "$install_dir/composio")
185
+ [[ $new_inode != "$old_inode" ]] || fail "$interpreter_name reinstall kept the binary inode"
186
+ grep -Fq 'new-binary-marker' "$install_dir/composio" ||
187
+ fail "$interpreter_name reinstall binary contents"
188
+ [[ ! -e "$install_dir/local-tools-binaries/stale.txt" ]] ||
189
+ fail "$interpreter_name reinstall retained stale directory contents"
190
+ : >"$marker.release"
191
+ wait "$survivor_pid"
192
+ survivor_pid=
193
+ [[ $(<"$marker.result") == old-process ]] || fail "$interpreter_name old process changed"
194
+ assert_no_residue "$install_dir"
195
+
196
+ binary_before=$(<"$install_dir/composio")
197
+ support_before=$(<"$install_dir/run-bun.mjs")
198
+ tag_before=$(<"$install_dir/release-tag.txt")
199
+ if missing_output=$(run_installer missing-binary 2>&1); then
200
+ fail "$interpreter_name missing binary archive succeeded"
201
+ fi
202
+ grep -Fq 'Binary not found in extracted archive' <<<"$missing_output" ||
203
+ fail "$interpreter_name missing binary error"
204
+ [[ $(<"$install_dir/composio") == "$binary_before" ]] ||
205
+ fail "$interpreter_name missing binary changed executable"
206
+ [[ $(<"$install_dir/run-bun.mjs") == "$support_before" ]] ||
207
+ fail "$interpreter_name missing binary changed support file"
208
+ [[ $(<"$install_dir/release-tag.txt") == "$tag_before" ]] ||
209
+ fail "$interpreter_name missing binary changed metadata"
210
+ assert_no_residue "$install_dir"
211
+
212
+ printf 'install.sh atomic replacement passed under %s\n' "$interpreter_name"
213
+done
214
+
215
+printf 'install.sh atomic replacement tests passed\n'