@devries-cameron20 / composio Mirror · read-only / commits / 640679237
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, 03:37 UTC

fix(cli): check upgrades against the archive release manifest (#5470)

This PR: - closes [PRDE-2250](https://linear.app/composio/issue/PRDE-2250/let-composio-upgrade-accept-archives-without-a-companion-file) - writes a versioned `release-manifest.json` into every release archive: `schemaVersion`, the live `companions`, and `minimumUpgradeFrom` (currently `0.0.0-0`, so no build is refused, prerelease CI fixtures included) - makes `composio upgrade` and `repairMissingInstalledRunCompanionModules` check the download against the manifest via `resolveArchiveCompanions`, falling back to the compiled companion names when the archive has none - refuses an archive whose `minimumUpgradeFrom` is above the running version with `UpgradeBelowMinimumError`, before any installed file changes; a source build with no recorded release tag skips the check - deletes nothing by globbing: files a new archive omits stay in place; the only removals are `local-tools-binaries` and a stale installed manifest when the target archive has none - keeps the legacy placeholders, and a test fails if they are dropped while `minimumUpgradeFrom` is at or below 0.4.2 - extends `verify-archive-companions.sh` to validate the manifest, and adds `release-manifest.json` to the uninstall lists (INSTALL.md, cli.mdx, `cli.install-health-check.yml`) ## Context Clients released before this change still require the companion names and placeholders compiled into them, so those files have to stay until that support window ends. Downgrading to a release without a manifest still uses the old name check, so a target that predates a companion the current client knows about is refused, as it is today. The health check does not check for the manifest, because the hourly canary installs the newest stable release, which predates this change. ## Verification - Unit tests cover upgrades with a manifest, without one, and to a downgrade target, plus repair. The CLI suite passes: 3260 tests. - The e2e upgrade suite passes 9/9 on a freshly rebuilt image, including a manifest bundle that leaves out `generation-runtime` and every placeholder, and a refusal because of `minimumUpgradeFrom`. - `verify-archive-companions.sh` passes on a real `package-binaries.ts` archive and fails on eight broken variants, including unsorted and duplicate companions. - Locally on macOS arm64, in isolated homes: the published 0.6.2 upgraded to a packaged PR build, then to a future archive with no `generation-runtime` or placeholders, refused an archive whose floor is above it, repaired a deleted companion from the manifest, and downgraded to the published 0.6.2. The published 0.6.2 still refuses the future archive, which is why the placeholders stay. - `pnpm typecheck`, `lint:effect` and `test/release-workflow.test.ts` pass.

Alberto Schiabel committed Nov 19, 2026 at 16:03 UTC 64067923738281c4badc05bba4b1357e1436fcbe
14 files changed +812 -37
.github/scripts/cli-release/verify-archive-companions.sh
+46 -1
@@ -2,7 +2,7 @@
2 #
3 # Loud failure gate: assert one release archive carries the companion layout the CLI
4 # actually ships — the legacy paths present but empty, the live bundles present with
5 -# real bytes.
5 +# real bytes, and a release-manifest.json that lists exactly what upgrades must install.
6 #
7 # Each check guards a different failure:
8 #
@@ -14,6 +14,10 @@
14 # - An empty live bundle passes every existence check and still breaks `composio run`
15 # and `composio generate`. The root files of the same name are one-line re-export
16 # wrappers, so the bundles under services/ are what carries the code.
17 +# - Manifest-aware clients install what release-manifest.json lists and nothing else,
18 +# and refuse a manifest they cannot decode. A manifest that omits a live bundle
19 +# ships an install that cannot run; one that lists a placeholder or a missing entry
20 +# fails every upgrade to this release.
21 #
22 # The unit tests cover the packaging rules on synthetic inputs; only this gate looks at a
23 # real archive, so a change to the packaging pipeline cannot quietly reintroduce any of
@@ -83,4 +87,45 @@ for relative_path in "${live_bundles[@]}"; do
87 echo "ok: $relative_path carries $size bytes"
88 done
89
90 +manifest_entry="${ARTIFACT}/release-manifest.json"
91 +grep -Fxq "$manifest_entry" <<<"$entries" ||
92 + fail "missing '$manifest_entry'; manifest-aware clients check upgrades against it"
93 +manifest="$(unzip -p "$ARCHIVE" "$manifest_entry")"
94 +
95 +jq -e '.schemaVersion == 1' <<<"$manifest" >/dev/null ||
96 + fail "'$manifest_entry' must have schemaVersion 1"
97 +jq -e '.minimumUpgradeFrom | type == "string" and test("^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[0-9A-Za-z.-]+)?$")' \
98 + <<<"$manifest" >/dev/null ||
99 + fail "'$manifest_entry' must have a semver minimumUpgradeFrom"
100 +jq -e '.companions | type == "array" and length > 0 and all(type == "string")' \
101 + <<<"$manifest" >/dev/null ||
102 + fail "'$manifest_entry' must list its companions"
103 +jq -e '.companions == (.companions | unique)' <<<"$manifest" >/dev/null ||
104 + fail "'$manifest_entry' must list its companions sorted and without duplicates; the CLI refuses any other order"
105 +# jq on Windows writes CRLF line endings in text mode.
106 +manifest_companions="$(jq -r '.companions[]' <<<"$manifest" | tr -d '\r')"
107 +
108 +for relative_path in "${live_bundles[@]}"; do
109 + grep -Fxq "$relative_path" <<<"$manifest_companions" ||
110 + fail "'$manifest_entry' does not list '$relative_path'; upgrades would not install it"
111 +done
112 +
113 +for relative_path in "${legacy_placeholders[@]}"; do
114 + if grep -Fxq "$relative_path" <<<"$manifest_companions"; then
115 + fail "'$manifest_entry' lists the legacy placeholder '$relative_path'; it must list live companions only"
116 + fi
117 +done
118 +
119 +listed=0
120 +while IFS= read -r relative_path; do
121 + entry="${ARTIFACT}/${relative_path}"
122 + grep -Fxq "$entry" <<<"$entries" ||
123 + fail "'$manifest_entry' lists '$relative_path', which the archive does not contain"
124 + size="$(entry_size "$entry")"
125 + [ "$size" -gt 0 ] ||
126 + fail "'$manifest_entry' lists '$relative_path', which is empty"
127 + listed=$((listed + 1))
128 +done <<<"$manifest_companions"
129 +echo "ok: release-manifest.json lists $listed live companions"
130 +
131 echo "Archive companion layout verified for $ARTIFACT."
.github/workflows/cli.install-health-check.yml
+1
@@ -86,6 +86,7 @@ jobs:
86 "$HOME/.local/bin/composio" \
87 "$HOME/.composio/composio" \
88 "$HOME/.composio/release-tag.txt" \
89 + "$HOME/.composio/release-manifest.json" \
90 "$HOME/.composio/run-helpers-runtime.mjs" \
91 "$HOME/.composio/run-subagent-shared.mjs" \
92 "$HOME/.composio/run-subagent-acp.mjs" \
INSTALL.md
+1
@@ -120,6 +120,7 @@ rm -f \
120 "$bin_dir/composio" \
121 "$install_dir/composio" \
122 "$install_dir/release-tag.txt" \
123 + "$install_dir/release-manifest.json" \
124 "$install_dir/run-helpers-runtime.mjs" \
125 "$install_dir/run-subagent-shared.mjs" \
126 "$install_dir/run-subagent-acp.mjs" \
docs/content/docs/cli.mdx
+1
@@ -197,6 +197,7 @@ rm -f \
197 "$bin_dir/composio" \
198 "$install_dir/composio" \
199 "$install_dir/release-tag.txt" \
200 + "$install_dir/release-manifest.json" \
201 "$install_dir/run-helpers-runtime.mjs" \
202 "$install_dir/run-subagent-shared.mjs" \
203 "$install_dir/run-subagent-acp.mjs" \
ts/e2e-tests/cli/upgrade/README.md
+2
@@ -4,6 +4,8 @@ Verifies that the compiled Linux CLI atomically replaces its running executable.
4
5 The suite covers both the installed `/usr/local/bin/composio` path and a copied executable. Each case checks the inode replacement, executable bit, upgrade result, absence of Linux busy-file errors, and a subsequent `composio version` invocation.
6
7 +Those bundles carry no `release-manifest.json`, so the CLI checks them against the companion names compiled into itself. Two more bundles carry one: a bundle that omits the generation runtime and every legacy placeholder still upgrades and runs, and installs its manifest; a bundle whose manifest requires a newer CLI than the installed `release-tag.txt` records is refused with the install left untouched.
8 +
9 ## Running
10
11 ```bash
ts/e2e-tests/cli/upgrade/e2e.test.ts
+95 -7
@@ -8,8 +8,9 @@ import { e2e, type E2ETestResult } from '@e2e-tests/utils';
8 import { TIMEOUTS } from '@e2e-tests/utils/const';
9 import { beforeAll, describe, expect, it } from 'bun:test';
10
11 -// The "downloaded" bundle is laid out like a release archive: the live companions
12 -// with their real contents, plus the legacy paths as empty placeholders.
11 +// The "downloaded" bundle is laid out like a release archive from before release
12 +// manifests: the live companions with their real contents, plus the legacy paths
13 +// as empty placeholders.
14
15 // RUN_COMPANION_MODULE_FILENAMES
16 const liveCompanionFileNames = ['run-helpers-runtime.mjs', 'generation-runtime.mjs'] as const;
@@ -30,7 +31,7 @@ const legacyPlaceholderRelativePaths = [
31 'acp-adapters/codex/linux-x64/codex-acp',
32 ] as const;
33
33 -const sourceBundleSetup = [
34 +const legacyBundleSetup = [
35 'mkdir -p "$source_dir/services"',
36 ...liveCompanionFileNames.flatMap(fileName => [
37 `cp "/usr/local/bin/${fileName}" "$source_dir/${fileName}"`,
@@ -42,12 +43,40 @@ const sourceBundleSetup = [
43 ),
44 ].join('\n');
45
46 +// RELEASE_MANIFEST_FILENAME
47 +const manifestFileName = 'release-manifest.json';
48 +
49 +const manifestCompanions = ['run-helpers-runtime.mjs', 'services/run-helpers-runtime.mjs'];
50 +
51 +const releaseManifest = (minimumUpgradeFrom: string) => ({
52 + schemaVersion: 1,
53 + minimumUpgradeFrom,
54 + companions: manifestCompanions,
55 +});
56 +
57 +/**
58 + * A bundle from a release that dropped the generation runtime and the legacy
59 + * placeholders: its manifest lists only what it ships.
60 + */
61 +const manifestBundleSetup = (minimumUpgradeFrom: string) =>
62 + [
63 + 'mkdir -p "$source_dir/services"',
64 + ...manifestCompanions.map(
65 + relativePath => `cp "/usr/local/bin/${relativePath}" "$source_dir/${relativePath}"`
66 + ),
67 + `cat > "$source_dir/${manifestFileName}" <<'EOF'`,
68 + JSON.stringify(releaseManifest(minimumUpgradeFrom), null, 2),
69 + 'EOF',
70 + ].join('\n');
71 +
72 const upgradeCommand = ({
73 executablePath,
74 copyExecutable,
75 + bundleSetup = legacyBundleSetup,
76 }: {
77 executablePath: string;
78 copyExecutable: boolean;
79 + bundleSetup?: string;
80 }) => `
81 set -u
82 executable_path=${executablePath}
@@ -55,7 +84,7 @@ source_dir=/tmp/composio-upgrade-source
84 mkdir -p "$source_dir"
85 ${copyExecutable ? 'mkdir -p "$(dirname "$executable_path")"\ncp /usr/local/bin/composio "$executable_path"' : ''}
86 cp /usr/local/bin/composio "$source_dir/composio"
58 -${sourceBundleSetup}
87 +${bundleSetup}
88
89 before_inode=$(stat -c '%i' "$executable_path")
90 upgrade_status=0
@@ -72,8 +101,9 @@ version_output=$("$executable_path" version)
101 version_status=$?
102 run_output=$("$executable_path" run 'console.log(z.string().parse("${runOutputMarker}"))')
103 run_status=$?
75 -printf 'before_inode=%s\nafter_inode=%s\nupgrade_status=%s\nexecutable_status=%s\nversion_status=%s\nversion=%s\nrun_status=%s\nrun_output=%s\n' \
76 - "$before_inode" "$after_inode" "$upgrade_status" "$executable_status" "$version_status" "$version_output" "$run_status" "$run_output"
104 +installed_manifest=$(cat "$(dirname "$executable_path")/${manifestFileName}" 2>/dev/null | tr -d '\n ')
105 +printf 'before_inode=%s\nafter_inode=%s\nupgrade_status=%s\nexecutable_status=%s\nversion_status=%s\nversion=%s\nrun_status=%s\nrun_output=%s\ninstalled_manifest=%s\n' \
106 + "$before_inode" "$after_inode" "$upgrade_status" "$executable_status" "$version_status" "$version_output" "$run_status" "$run_output" "$installed_manifest"
107
108 if [ "$upgrade_status" -eq 0 ] &&
109 [ "$executable_status" -eq 0 ] &&
@@ -99,7 +129,7 @@ source_dir=/tmp/composio-upgrade-source
129 mkdir -p "$install_dir" "$source_dir"
130 cp -r /usr/local/bin/. "$install_dir/"
131 cp /usr/local/bin/composio "$source_dir/composio"
102 -${sourceBundleSetup}
132 +${legacyBundleSetup}
133 chmod -R a+rX "$source_dir"
134 chmod -R a-w "$install_dir"
135
@@ -115,6 +145,26 @@ printf 'upgrade_status=%s\nbefore=%s\nafter=%s\nversion_status=%s\nversion=%s\nl
145 "$upgrade_status" "$before" "$after" "$version_status" "$version_output" "$leftovers"
146 `;
147
148 +const belowMinimumCommand = `
149 +set -u
150 +install_dir=/opt/composio-below-minimum
151 +source_dir=/tmp/composio-upgrade-source
152 +mkdir -p "$install_dir" "$source_dir"
153 +cp -r /usr/local/bin/. "$install_dir/"
154 +printf '%s\\n' '@composio/cli@0.5.0' > "$install_dir/release-tag.txt"
155 +cp /usr/local/bin/composio "$source_dir/composio"
156 +${manifestBundleSetup('99.0.0')}
157 +
158 +before=$(stat -c '%i %s' "$install_dir/composio")
159 +upgrade_status=0
160 +DEBUG_OVERRIDE_UPGRADE_TARGET="$source_dir/composio" "$install_dir/composio" upgrade || upgrade_status=$?
161 +after=$(stat -c '%i %s' "$install_dir/composio")
162 +if [ -e "$install_dir/${manifestFileName}" ]; then manifest_installed=1; else manifest_installed=0; fi
163 +release_tag=$(cat "$install_dir/release-tag.txt")
164 +printf 'upgrade_status=%s\\nbefore=%s\\nafter=%s\\nmanifest_installed=%s\\nrelease_tag=%s\\n' \\
165 + "$upgrade_status" "$before" "$after" "$manifest_installed" "$release_tag"
166 +`;
167 +
168 const outputField = (result: E2ETestResult, name: string): string => {
169 const match = result.stdout.match(new RegExp(`^${name}=(.*)$`, 'm'));
170 expect(match).not.toBeNull();
@@ -141,6 +191,8 @@ e2e(import.meta.url, {
191 let installedResult: E2ETestResult;
192 let copiedResult: E2ETestResult;
193 let readOnlyResult: E2ETestResult;
194 + let manifestResult: E2ETestResult;
195 + let belowMinimumResult: E2ETestResult;
196
197 beforeAll(async () => {
198 installedResult = await runCmd(
@@ -150,6 +202,14 @@ e2e(import.meta.url, {
202 upgradeCommand({ executablePath: '/tmp/composio-copy/composio', copyExecutable: true })
203 );
204 readOnlyResult = await runCmd(readOnlyInstallCommand);
205 + manifestResult = await runCmd(
206 + upgradeCommand({
207 + executablePath: '/usr/local/bin/composio',
208 + copyExecutable: false,
209 + bundleSetup: manifestBundleSetup('0.1.26'),
210 + })
211 + );
212 + belowMinimumResult = await runCmd(belowMinimumCommand);
213 }, TIMEOUTS.FIXTURE);
214
215 describe('composio upgrade', () => {
@@ -162,6 +222,34 @@ e2e(import.meta.url, {
222 });
223 });
224
225 + describe('composio upgrade to a bundle with a release manifest', () => {
226 + it('installs a bundle that omits the generation runtime and every placeholder', () => {
227 + expectAtomicUpgrade(manifestResult);
228 + });
229 +
230 + it('installs the manifest next to the executable', () => {
231 + expect(outputField(manifestResult, 'installed_manifest')).toBe(
232 + JSON.stringify(releaseManifest('0.1.26'))
233 + );
234 + });
235 + });
236 +
237 + describe('composio upgrade to a bundle that requires a newer CLI', () => {
238 + it('refuses and names the reinstall path', () => {
239 + expect(outputField(belowMinimumResult, 'upgrade_status')).toBe('1');
240 + expect(belowMinimumResult.stderr).toContain('99.0.0');
241 + expect(belowMinimumResult.stderr).toContain('curl -fsSL https://composio.dev/install | sh');
242 + });
243 +
244 + it('leaves the install untouched', () => {
245 + expect(outputField(belowMinimumResult, 'after')).toBe(
246 + outputField(belowMinimumResult, 'before')
247 + );
248 + expect(outputField(belowMinimumResult, 'manifest_installed')).toBe('0');
249 + expect(outputField(belowMinimumResult, 'release_tag')).toBe('@composio/cli@0.5.0');
250 + });
251 + });
252 +
253 describe('composio upgrade without write permission on the install directory', () => {
254 it('fails and says which file it could not replace', () => {
255 expect(outputField(readOnlyResult, 'upgrade_status')).toBe('1');
ts/packages/cli/CHANGELOG.md
+2
@@ -4,6 +4,8 @@
4
5 ### Patch Changes
6
7 +- Release archives now include a `release-manifest.json` that lists the support files they ship. `composio upgrade` installs exactly what the manifest lists, so later releases can drop a support file without breaking upgrades. Files a release no longer lists stay in place. A release can name the oldest CLI version that may upgrade to it; an older CLI refuses it before changing anything and prints the install command to run instead.
8 +
9 - Update @composio/client to 2.1.35 to include the latest API changes.
10
11 - `composio listen --stream` shows control characters, including embedded tabs and line breaks,
ts/packages/cli/scripts/_release-artifacts.ts
+33 -1
@@ -5,7 +5,12 @@
5 * under Node.
6 */
7
8 -import { RUN_COMPANION_LEGACY_PLACEHOLDER_RELATIVE_PATHS } from '../src/services/run-companion-modules';
8 +import { Schema } from 'effect';
9 +import {
10 + RELEASE_MANIFEST_SCHEMA_VERSION,
11 + ReleaseManifest,
12 + RUN_COMPANION_LEGACY_PLACEHOLDER_RELATIVE_PATHS,
13 +} from '../src/services/run-companion-modules';
14
15 /**
16 * Every published platform: the Bun cross-compilation target and the artifact it produces.
@@ -60,3 +65,30 @@ export const archiveCompanionEntries = (
65 kind: 'placeholder' as const,
66 })),
67 ];
68 +
69 +/**
70 + * The earliest CLI version a manifest-aware client may upgrade from to an archive
71 + * built today. It starts at the lowest semver version, so no build is refused,
72 + * including prerelease builds such as the `0.0.0-beta.0` CI fixtures.
73 + *
74 + * Raise it when an archive drops a path that some client at or above it still
75 + * requires. Clients that predate manifests never read it, so it does not make
76 + * them accept a smaller archive: the legacy placeholders stay while it is at or
77 + * below 0.4.2, the last release that requires them.
78 + */
79 +export const MINIMUM_UPGRADE_FROM = '0.0.0-0';
80 +
81 +/**
82 + * The `release-manifest.json` every archive carries: the live companions, never
83 + * the legacy placeholders, which no current client needs. Decoded on the way out
84 + * so packaging cannot write a manifest the CLI would refuse.
85 + */
86 +export const buildReleaseManifest = (liveRelativePaths: ReadonlyArray<string>): ReleaseManifest =>
87 + Schema.decodeUnknownSync(ReleaseManifest)({
88 + schemaVersion: RELEASE_MANIFEST_SCHEMA_VERSION,
89 + minimumUpgradeFrom: MINIMUM_UPGRADE_FROM,
90 + companions: [...liveRelativePaths].sort(),
91 + });
92 +
93 +export const serializeReleaseManifest = (manifest: ReleaseManifest): string =>
94 + `${JSON.stringify(manifest, null, 2)}\n`;
ts/packages/cli/scripts/package-binaries.ts
+10 -1
@@ -22,12 +22,17 @@ import { teardown } from './_shared';
22 import { $ } from 'bun';
23 import { readdir, stat, writeFile } from 'node:fs/promises';
24 import path from 'node:path';
25 -import { collectExpectedRunCompanionAssetRelativePaths } from '../src/services/run-companion-modules';
25 +import {
26 + collectExpectedRunCompanionAssetRelativePaths,
27 + RELEASE_MANIFEST_FILENAME,
28 +} from '../src/services/run-companion-modules';
29 import {
30 archiveCompanionEntries,
31 archiveExecutableName,
32 + buildReleaseManifest,
33 compiledBinaryFileName,
34 RELEASE_TARGETS,
35 + serializeReleaseManifest,
36 } from './_release-artifacts';
37
38 const BINARIES_DIR = './dist/binaries';
@@ -68,6 +73,9 @@ export function packageBinaries() {
73
74 // See `archiveCompanionEntries` for why the legacy paths are present but empty.
75 const companionEntries = archiveCompanionEntries(liveCompanionRelativePaths);
76 + const releaseManifest = serializeReleaseManifest(
77 + buildReleaseManifest(liveCompanionRelativePaths)
78 + );
79
80 for (const binary of binaries) {
81 const binaryPath = path.join(BINARIES_DIR, compiledBinaryFileName(binary));
@@ -92,6 +100,7 @@ export function packageBinaries() {
100 }
101 await $`cp ${path.join(COMPANIONS_DIR, relativePath)} ${destinationPath}`.quiet();
102 }
103 + await writeFile(path.join(nestedDir, RELEASE_MANIFEST_FILENAME), releaseManifest, 'utf8');
104 if (RELEASE_TAG) {
105 await writeFile(path.join(nestedDir, 'release-tag.txt'), `${RELEASE_TAG}\n`, 'utf8');
106 }
ts/packages/cli/src/services/run-companion-modules.ts
+129 -3
@@ -5,6 +5,7 @@
5 import * as FileSystem from 'effect/FileSystem';
6 import * as Path from 'effect/Path';
7 import { Data, Effect, Option, PlatformError, Schema } from 'effect';
8 +import semver from 'semver';
9 import { extractZipSafely } from 'src/utils/extract-zip-safely';
10 import { APP_VERSION, IS_RELEASE_BUILD } from 'src/constants';
11 import { GitHubRelease } from 'src/effects/resolve-cli-release';
@@ -31,6 +32,10 @@ export const RUN_COMPANION_MODULE_FILENAMES = RUN_COMPANION_MODULE_BASENAMES.map
32
33 export const RUN_COMPANION_RELEASE_TAG_FILENAME = 'release-tag.txt';
34
35 +export const RELEASE_MANIFEST_FILENAME = 'release-manifest.json';
36 +
37 +export const RELEASE_MANIFEST_SCHEMA_VERSION = 1;
38 +
39 // Paths the removed `experimental_subAgent` helper used to ship. Release archives
40 // keep them as zero-byte files because every stable CLI from 0.2.12 to 0.4.2
41 // verifies a downloaded upgrade package against its own copy of this list, by
@@ -54,6 +59,106 @@ export const RUN_COMPANION_LEGACY_PLACEHOLDER_RELATIVE_PATHS: ReadonlyArray<stri
59 'acp-adapters/codex/linux-x64/codex-acp',
60 ];
61
62 +const isArchiveRelativePath = (value: string): boolean =>
63 + !value.startsWith('/') &&
64 + !value.includes('\\') &&
65 + !/^[A-Za-z]:/.test(value) &&
66 + value.split('/').every(segment => segment !== '' && segment !== '.' && segment !== '..');
67 +
68 +const ArchiveRelativePath = Schema.String.check(
69 + Schema.makeFilter(isArchiveRelativePath, {
70 + expected: 'a relative POSIX path with no empty, "." or ".." segments',
71 + })
72 +);
73 +
74 +const isSortedAndUnique = (values: ReadonlyArray<string>): boolean =>
75 + values.every((value, index) => index === 0 || values[index - 1]! < value);
76 +
77 +/**
78 + * What a release archive says it contains, written by packaging as
79 + * `release-manifest.json` next to the executable. A client that reads it checks
80 + * the download against `companions` instead of the companion names compiled
81 + * into itself, so a later archive can drop a file without breaking upgrades.
82 + *
83 + * Adding a field keeps schema version 1, since decoding ignores unknown keys. A
84 + * change an older reader would misread bumps the version, and a client refuses a
85 + * version it does not know.
86 + */
87 +export const ReleaseManifest = Schema.Struct({
88 + schemaVersion: Schema.Literal(RELEASE_MANIFEST_SCHEMA_VERSION),
89 + /** The earliest CLI version that may upgrade to this archive. */
90 + minimumUpgradeFrom: Schema.String.check(
91 + Schema.makeFilter(value => semver.valid(value) === value, { expected: 'a semver version' })
92 + ),
93 + companions: Schema.NonEmptyArray(ArchiveRelativePath).check(
94 + Schema.makeFilter(isSortedAndUnique, { expected: 'sorted, unique paths' })
95 + ),
96 +});
97 +export type ReleaseManifest = typeof ReleaseManifest.Type;
98 +
99 +const ReleaseManifestVersion = Schema.fromJsonString(
100 + Schema.Struct({ schemaVersion: Schema.Unknown })
101 +);
102 +const ReleaseManifestFromJson = Schema.fromJsonString(ReleaseManifest);
103 +
104 +export class ReleaseManifestError extends Data.TaggedError('services/ReleaseManifestError')<{
105 + readonly message: string;
106 + readonly cause?: unknown;
107 +}> {}
108 +
109 +export type ArchiveCompanions = Data.TaggedEnum<{
110 + Manifest: { readonly manifest: ReleaseManifest; readonly relativePaths: ReadonlyArray<string> };
111 + CompiledNames: { readonly relativePaths: ReadonlyArray<string> };
112 +}>;
113 +export const ArchiveCompanions = Data.taggedEnum<ArchiveCompanions>();
114 +
115 +const manifestError = (message: string) => (cause: unknown) =>
116 + new ReleaseManifestError({ message, cause });
117 +
118 +const decodeReleaseManifest = (
119 + text: string
120 +): Effect.Effect<ReleaseManifest, ReleaseManifestError> =>
121 + Effect.gen(function* () {
122 + const { schemaVersion } = yield* Schema.decodeUnknownEffect(ReleaseManifestVersion)(text).pipe(
123 + Effect.mapError(manifestError(`${RELEASE_MANIFEST_FILENAME} is not valid JSON`))
124 + );
125 + if (schemaVersion !== RELEASE_MANIFEST_SCHEMA_VERSION) {
126 + return yield* new ReleaseManifestError({
127 + message: `${RELEASE_MANIFEST_FILENAME} has schema version ${String(schemaVersion)}, which this CLI cannot read`,
128 + });
129 + }
130 + return yield* Schema.decodeUnknownEffect(ReleaseManifestFromJson)(text).pipe(
131 + Effect.mapError(manifestError(`${RELEASE_MANIFEST_FILENAME} is invalid`))
132 + );
133 + });
134 +
135 +/**
136 + * Resolves what the release package extracted at `packageDir` provides. A
137 + * manifest that is present but unreadable or invalid fails: the archive is
138 + * broken, and guessing from compiled names could install a partial release.
139 + */
140 +export const resolveArchiveCompanions = (
141 + packageDir: string
142 +): Effect.Effect<ArchiveCompanions, ReleaseManifestError, FileSystem.FileSystem | Path.Path> =>
143 + Effect.gen(function* () {
144 + const fs = yield* FileSystem.FileSystem;
145 + const path = yield* Path.Path;
146 + const manifestText = yield* fs
147 + .readFileString(path.join(packageDir, RELEASE_MANIFEST_FILENAME), 'utf8')
148 + .pipe(
149 + Effect.map(Option.some),
150 + Effect.catchReason('PlatformError', 'NotFound', () => Effect.succeed(Option.none())),
151 + Effect.mapError(manifestError(`Cannot read ${RELEASE_MANIFEST_FILENAME}`))
152 + );
153 + if (Option.isNone(manifestText)) {
154 + return ArchiveCompanions.CompiledNames({
155 + relativePaths: yield* collectExpectedRunCompanionAssetRelativePaths(packageDir),
156 + });
157 + }
158 + const manifest = yield* decodeReleaseManifest(manifestText.value);
159 + return ArchiveCompanions.Manifest({ manifest, relativePaths: manifest.companions });
160 + });
161 +
162 export class RunCompanionRepairError extends Data.TaggedError('services/RunCompanionRepairError')<{
163 readonly message: string;
164 readonly cause?: unknown;
@@ -246,6 +351,17 @@ export const resolveRunningCliVersion = (
351 ? Effect.succeed(normalizeCliReleaseVersion(appVersion))
352 : resolveInstalledCliVersion(execPath, appVersion);
353
354 +export const resolveRecordedRunningCliVersion = (
355 + execPath: string,
356 + appVersion: string,
357 + isReleaseBuild = IS_RELEASE_BUILD
358 +): Effect.Effect<Option.Option<string>, never, FileSystem.FileSystem | Path.Path> =>
359 + isReleaseBuild
360 + ? Effect.succeed(Option.some(normalizeCliReleaseVersion(appVersion)))
361 + : Effect.map(readInstalledReleaseTag(execPath), releaseTag =>
362 + Option.map(Option.fromNullishOr(releaseTag), normalizeCliReleaseVersion)
363 + );
364 +
365 export const resolveRunningCliReleaseTag = (
366 execPath: string,
367 appVersion: string,
@@ -544,10 +660,20 @@ export const repairMissingInstalledRunCompanionModules = ({
660 });
661
662 const installDirectory = path.dirname(execPath);
547 - const companionRelativePaths =
548 - yield* collectExpectedRunCompanionAssetRelativePaths(packageDirectory);
663 + const archiveCompanions = yield* resolveArchiveCompanions(packageDirectory).pipe(
664 + Effect.mapError(
665 + error =>
666 + new RunCompanionRepairError({
667 + message: `Release ${release.tag_name} has a broken ${RELEASE_MANIFEST_FILENAME} (${error.message}); cannot restore the CLI's bundled support files. Reinstall the CLI and try again.`,
668 + cause: error,
669 + })
670 + )
671 + );
672 + const restoredRelativePaths = ArchiveCompanions.$is('Manifest')(archiveCompanions)
673 + ? [...archiveCompanions.relativePaths, RELEASE_MANIFEST_FILENAME]
674 + : archiveCompanions.relativePaths;
675
550 - for (const relativePath of companionRelativePaths) {
676 + for (const relativePath of restoredRelativePaths) {
677 const sourcePath = path.join(packageDirectory, relativePath);
678 const sourceExists = yield* fileExists(fs, sourcePath);
679 if (!sourceExists) {
ts/packages/cli/src/services/upgrade-binary.ts
+130 -22
@@ -19,6 +19,7 @@ import { APP_VERSION } from '../constants';
19 import { DEBUG_CONFIG, GITHUB_CONFIG, type GitHubConfig } from 'src/config';
20 import { detectPlatform, type PlatformArch } from 'src/effects/detect-platform';
21 import { CompareSemverError, semverComparator } from 'src/effects/compare-semver';
22 +import { ExpectedFailure } from 'src/effects/expected-failure';
23 import { fetchLatestCliRelease, GitHubRelease } from 'src/effects/resolve-cli-release';
24 import { parseChecksumsText, sha256Hex } from 'src/utils/checksums';
25 import { atomicReplaceFile, type AtomicReplaceError } from 'src/utils/atomic-replace';
@@ -30,10 +31,14 @@ import { TerminalUI } from './terminal-ui';
31 import { NodeOs } from './node-os';
32 import { recordLatestStableRelease } from './update-check';
33 import {
33 - collectExpectedRunCompanionAssetRelativePaths,
34 + ArchiveCompanions,
35 + RELEASE_MANIFEST_FILENAME,
36 RUN_COMPANION_RELEASE_TAG_FILENAME,
37 + resolveArchiveCompanions,
38 + resolveRecordedRunningCliVersion,
39 resolveRunningCliReleaseTag,
40 writeInstalledReleaseTag,
41 + type ReleaseManifest,
42 } from './run-companion-modules';
43
44 export class UpgradeBinaryError extends Data.TaggedError('services/UpgradeBinaryError')<{
@@ -41,6 +46,23 @@ export class UpgradeBinaryError extends Data.TaggedError('services/UpgradeBinary
46 readonly message?: string;
47 }> {}
48
49 +export class UpgradeBelowMinimumError extends Data.TaggedError(
50 + 'services/UpgradeBelowMinimumError'
51 +)<{
52 + readonly message: string;
53 + readonly currentVersion: string;
54 + readonly minimumUpgradeFrom: string;
55 +}> {
56 + get [ExpectedFailure]() {
57 + return true;
58 + }
59 +}
60 +
61 +const reinstallCommand = (platform: NodeJS.Platform) =>
62 + platform === 'win32'
63 + ? 'irm https://raw.githubusercontent.com/ComposioHQ/composio/next/install.ps1 | iex'
64 + : 'curl -fsSL https://composio.dev/install | sh';
65 +
66 /**
67 * CLI binary name constant
68 */
@@ -514,47 +536,109 @@ const mapAtomicReplaceError = (message: string) => (error: AtomicReplaceError) =
536 message: `${message}: ${error.message}`,
537 });
538
539 +const ensureUpgradeAllowedFrom = (
540 + { minimumUpgradeFrom }: ReleaseManifest,
541 + currentVersion: Option.Option<string>,
542 + platform: NodeJS.Platform
543 +): Effect.Effect<void, UpgradeBelowMinimumError | UpgradeBinaryError> =>
544 + Effect.gen(function* () {
545 + if (Option.isNone(currentVersion)) {
546 + return yield* Effect.logDebug(
547 + `Running version unknown; not checking the archive's minimum of ${minimumUpgradeFrom}`
548 + );
549 + }
550 + const current = currentVersion.value;
551 + const comparison = yield* semverComparator(current, minimumUpgradeFrom).pipe(
552 + Effect.mapError(
553 + cause =>
554 + new UpgradeBinaryError({
555 + cause,
556 + message: `Cannot compare the running version ${current} with the release's minimum ${minimumUpgradeFrom}`,
557 + })
558 + )
559 + );
560 + if (comparison < 0) {
561 + return yield* new UpgradeBelowMinimumError({
562 + currentVersion: current,
563 + minimumUpgradeFrom,
564 + message: [
565 + `This release can only be installed over Composio CLI ${minimumUpgradeFrom} or newer, and this CLI is ${current}.`,
566 + 'Reinstall the CLI to get it:',
567 + ` ${reinstallCommand(platform)}`,
568 + ].join('\n'),
569 + });
570 + }
571 + });
572 +
573 +const syncInstalledReleaseManifest = (
574 + ctx: Pick<UpgradeBinaryContext, 'fs' | 'path'>,
575 + archiveCompanions: ArchiveCompanions,
576 + sourceDirectory: string,
577 + targetDirectory: string
578 +) => {
579 + const installedManifestPath = ctx.path.join(targetDirectory, RELEASE_MANIFEST_FILENAME);
580 + if (ArchiveCompanions.$is('CompiledNames')(archiveCompanions)) {
581 + return ctx.fs.remove(installedManifestPath, { force: true }).pipe(
582 + Effect.mapError(
583 + cause =>
584 + new UpgradeBinaryError({
585 + cause,
586 + message: `Failed to remove the previous release manifest: ${installedManifestPath}`,
587 + })
588 + )
589 + );
590 + }
591 + return provideFsAndPath(
592 + ctx,
593 + atomicReplaceFile({
594 + sourcePath: ctx.path.join(sourceDirectory, RELEASE_MANIFEST_FILENAME),
595 + targetPath: installedManifestPath,
596 + })
597 + ).pipe(Effect.mapError(mapAtomicReplaceError('Failed to update installed release manifest')));
598 +};
599 +
600 const replaceBinary = (
601 ctx: Pick<UpgradeBinaryContext, 'fs' | 'path' | 'platform'>,
602 sourcePath: string,
603 targetPath: string,
604 options: {
605 releaseTag?: string;
523 - } = {}
524 -): Effect.Effect<void, UpgradeBinaryError, Scope.Scope> =>
606 + currentVersion: Option.Option<string>;
607 + }
608 +): Effect.Effect<void, UpgradeBinaryError | UpgradeBelowMinimumError, Scope.Scope> =>
609 Effect.gen(function* () {
610 const { fs, path } = ctx;
611 yield* Effect.logDebug(`Replacing binary: ${sourcePath} -> ${targetPath}`);
612
529 - if (ctx.platform === 'win32') {
530 - // Windows keeps the previous executable locked until its process has fully exited,
531 - // which can be a moment after an upgrade that just ran returns.
532 - yield* fs.remove(`${targetPath}.old`, { force: true }).pipe(
533 - Effect.retry({ times: 10, schedule: Schedule.spaced('200 millis') }),
534 - Effect.mapError(
535 - cause =>
536 - new UpgradeBinaryError({
537 - cause,
538 - message:
539 - 'Could not remove the previous executable backup. Close other Composio processes and retry.',
540 - })
541 - )
542 - );
543 - }
544 -
613 const sourceDirectory = path.dirname(sourcePath);
614 const targetDirectory = path.dirname(targetPath);
547 - const companionRelativePaths = yield* provideFsAndPath(
615 + const archiveCompanions = yield* provideFsAndPath(
616 ctx,
549 - collectExpectedRunCompanionAssetRelativePaths(sourceDirectory)
617 + resolveArchiveCompanions(sourceDirectory)
618 + ).pipe(
619 + Effect.mapError(
620 + error =>
621 + new UpgradeBinaryError({
622 + cause: error.cause,
623 + message: `Downloaded binary package is invalid: ${error.message}. Reinstall the CLI: ${reinstallCommand(ctx.platform)}`,
624 + })
625 + )
626 );
627 + if (ArchiveCompanions.$is('Manifest')(archiveCompanions)) {
628 + yield* ensureUpgradeAllowedFrom(
629 + archiveCompanions.manifest,
630 + options.currentVersion,
631 + ctx.platform
632 + );
633 + }
634 +
635 const companionReplacements: Array<{
636 readonly relativePath: string;
637 readonly sourcePath: string;
638 readonly targetPath: string;
639 }> = [];
640
557 - for (const relativePath of companionRelativePaths) {
641 + for (const relativePath of archiveCompanions.relativePaths) {
642 const sourceCompanion = path.join(sourceDirectory, relativePath);
643 const sourceExists = yield* fs
644 .exists(sourceCompanion)
@@ -576,6 +660,22 @@ const replaceBinary = (
660 });
661 }
662
663 + if (ctx.platform === 'win32') {
664 + // Windows keeps the previous executable locked until its process has fully exited,
665 + // which can be a moment after an upgrade that just ran returns.
666 + yield* fs.remove(`${targetPath}.old`, { force: true }).pipe(
667 + Effect.retry({ times: 10, schedule: Schedule.spaced('200 millis') }),
668 + Effect.mapError(
669 + cause =>
670 + new UpgradeBinaryError({
671 + cause,
672 + message:
673 + 'Could not remove the previous executable backup. Close other Composio processes and retry.',
674 + })
675 + )
676 + );
677 + }
678 +
679 const releaseTag = options.releaseTag;
680 const stagedReleaseTagPath = path.join(sourceDirectory, RUN_COMPANION_RELEASE_TAG_FILENAME);
681 if (releaseTag) {
@@ -616,6 +716,8 @@ const replaceBinary = (
716 );
717 }
718
719 + yield* syncInstalledReleaseManifest(ctx, archiveCompanions, sourceDirectory, targetDirectory);
720 +
721 if (releaseTag) {
722 yield* provideFsAndPath(
723 ctx,
@@ -663,6 +765,10 @@ const upgrade = (
765 const prerelease = options.prerelease ?? false;
766 const explicitTag = options.tag;
767 const currentReleaseIdentifier = yield* resolveCurrentReleaseIdentifier(ctx, currentPath);
768 + const currentVersion = yield* provideFsAndPath(
769 + ctx,
770 + resolveRecordedRunningCliVersion(currentPath, APP_VERSION)
771 + );
772 yield* Effect.logDebug(`Current executable path: ${currentPath}`);
773 yield* Effect.logDebug(`Current release identifier: ${currentReleaseIdentifier}`);
774
@@ -673,6 +779,7 @@ const upgrade = (
779 yield* ui.log.info(`New local version available (current: ${currentReleaseIdentifier})`);
780 yield* replaceBinary(ctx, upgradeTarget, currentPath, {
781 releaseTag: explicitTag,
782 + currentVersion,
783 });
784 yield* ui.outro('Upgrade completed');
785 return undefined;
@@ -735,6 +842,7 @@ const upgrade = (
842 const extractedBinary = yield* extractBinary(ctx, { name, data }, tmpDir);
843 yield* replaceBinary(ctx, extractedBinary.binaryPath, currentPath, {
844 releaseTag: release.tag_name,
845 + currentVersion,
846 });
847
848 yield* spinner.stop('Upgrade completed!');
ts/packages/cli/test/src/scripts/release-artifacts.test.ts
+57 -2
@@ -1,7 +1,18 @@
1 +import { Schema } from 'effect';
2 +import semver from 'semver';
3 import { describe, expect, it } from 'vitest';
4
3 -import { RUN_COMPANION_MODULE_FILENAMES } from '../../../src/services/run-companion-modules';
4 -import { archiveCompanionEntries } from '../../../scripts/_release-artifacts';
5 +import {
6 + ReleaseManifest,
7 + RUN_COMPANION_LEGACY_PLACEHOLDER_RELATIVE_PATHS,
8 + RUN_COMPANION_MODULE_FILENAMES,
9 +} from '../../../src/services/run-companion-modules';
10 +import {
11 + archiveCompanionEntries,
12 + buildReleaseManifest,
13 + MINIMUM_UPGRADE_FROM,
14 + serializeReleaseManifest,
15 +} from '../../../scripts/_release-artifacts';
16
17 /**
18 * Everything `collectExpectedRunCompanionAssetRelativePaths` yields for a fully
@@ -110,3 +121,47 @@ describe('upgrade from already-released clients', () => {
121 expect(entryPaths).toEqual(expect.arrayContaining([...required]));
122 });
123 });
124 +
125 +describe('buildReleaseManifest', () => {
126 + it('lists exactly the live companions under schema version 1', () => {
127 + expect(buildReleaseManifest(LIVE_COMPANION_RELATIVE_PATHS)).toEqual({
128 + schemaVersion: 1,
129 + minimumUpgradeFrom: MINIMUM_UPGRADE_FROM,
130 + companions: [
131 + 'generation-runtime.mjs',
132 + 'run-helpers-runtime.mjs',
133 + 'services/generation-runtime.mjs',
134 + 'services/run-helpers-runtime.mjs',
135 + ],
136 + });
137 + });
138 +
139 + it('serializes to JSON the CLI decodes back to the same manifest', () => {
140 + const manifest = buildReleaseManifest(['run-helpers-runtime.mjs']);
141 + const serialized = serializeReleaseManifest(manifest);
142 +
143 + expect(Schema.decodeUnknownSync(ReleaseManifest)(JSON.parse(serialized))).toEqual(manifest);
144 + expect(serialized.endsWith('}\n')).toBe(true);
145 + });
146 +
147 + it('refuses a companion path the CLI would refuse to install', () => {
148 + expect(() => buildReleaseManifest(['../run-helpers-runtime.mjs'])).toThrow();
149 + });
150 +});
151 +
152 +describe('MINIMUM_UPGRADE_FROM', () => {
153 + it('keeps every legacy placeholder in the archive while clients up to 0.4.2 may upgrade', () => {
154 + const entryPaths = archiveCompanionEntries(LIVE_COMPANION_RELATIVE_PATHS).map(
155 + entry => entry.relativePath
156 + );
157 + const keepsPlaceholders = RUN_COMPANION_LEGACY_PLACEHOLDER_RELATIVE_PATHS.every(relativePath =>
158 + entryPaths.includes(relativePath)
159 + );
160 +
161 + expect(semver.gt(MINIMUM_UPGRADE_FROM, '0.4.2') || keepsPlaceholders).toBe(true);
162 + });
163 +
164 + it('lets a 0.0.0-beta.0 client upgrade, since no release has dropped a required path yet', () => {
165 + expect(semver.gte('0.0.0-beta.0', MINIMUM_UPGRADE_FROM)).toBe(true);
166 + });
167 +});
ts/packages/cli/test/src/services/run-companion-modules.test.ts
+109
@@ -13,6 +13,7 @@ import {
13 RUN_COMPANION_LEGACY_PLACEHOLDER_RELATIVE_PATHS,
14 RUN_COMPANION_MODULE_BASENAMES,
15 RUN_COMPANION_MODULE_FILENAMES,
16 + RELEASE_MANIFEST_FILENAME,
17 RUN_COMPANION_RELEASE_TAG_FILENAME,
18 resolveRunCompanionModulePath,
19 } from 'src/services/run-companion-modules';
@@ -97,6 +98,39 @@ const mockArchiveContents = (missingRelativePath?: string) => {
98 );
99 };
100
101 +const mockManifestArchive = (relativePaths: ReadonlyArray<string>, manifest: unknown) => {
102 + extractZipMock.mockImplementation(
103 + async (archivePath: string, options: { readonly dir: string }) => {
104 + const packageDirectory = path.join(options.dir, path.parse(archivePath).name);
105 + relativePaths.forEach(relativePath => {
106 + const filePath = path.join(packageDirectory, relativePath);
107 + fs.mkdirSync(path.dirname(filePath), { recursive: true });
108 + fs.writeFileSync(filePath, `new:${relativePath}`);
109 + });
110 + fs.writeFileSync(
111 + path.join(packageDirectory, RELEASE_MANIFEST_FILENAME),
112 + JSON.stringify(manifest)
113 + );
114 + }
115 + );
116 +};
117 +
118 +const LIVE_COMPANIONS_WITH_BUNDLES = [
119 + 'generation-runtime.mjs',
120 + 'run-helpers-runtime.mjs',
121 + 'services/generation-runtime.mjs',
122 + 'services/run-helpers-runtime.mjs',
123 +];
124 +
125 +const listFilesRecursively = (directory: string): ReadonlyArray<string> =>
126 + fs
127 + .readdirSync(directory, { recursive: true, withFileTypes: true })
128 + .filter(entry => entry.isFile())
129 + .map(entry =>
130 + path.relative(directory, path.join(entry.parentPath, entry.name)).replaceAll(path.sep, '/')
131 + )
132 + .sort();
133 +
134 describe('run-companion-modules', () => {
135 afterEach(() => {
136 extractZipMock.mockReset();
@@ -331,6 +365,81 @@ describe('run-companion-modules', () => {
365 );
366 });
367
368 + it.effect(
369 + '[Given] an archive with a manifest [Then] repair restores the files it lists and the manifest',
370 + () => {
371 + const installDirectory = fs.mkdtempSync(
372 + path.join(os.tmpdir(), 'composio-run-repair-manifest-')
373 + );
374 + const execPath = path.join(installDirectory, 'composio');
375 + const manifest = {
376 + schemaVersion: 1,
377 + minimumUpgradeFrom: '0.1.26',
378 + companions: LIVE_COMPANIONS_WITH_BUNDLES,
379 + };
380 + stubRepairFetch();
381 + mockManifestArchive(
382 + [...LIVE_COMPANIONS_WITH_BUNDLES, ...RUN_COMPANION_LEGACY_PLACEHOLDER_RELATIVE_PATHS],
383 + manifest
384 + );
385 +
386 + return Effect.gen(function* () {
387 + const result = yield* repairMissingInstalledRunCompanionModules({
388 + callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs',
389 + execPath,
390 + appVersion: '0.0.0-test',
391 + });
392 +
393 + expect(result).toEqual({ repaired: true, releaseTag: TEST_RELEASE_TAG });
394 + expect(listFilesRecursively(installDirectory)).toEqual(
395 + [
396 + ...LIVE_COMPANIONS_WITH_BUNDLES,
397 + RELEASE_MANIFEST_FILENAME,
398 + RUN_COMPANION_RELEASE_TAG_FILENAME,
399 + ].sort()
400 + );
401 + expect(
402 + JSON.parse(
403 + fs.readFileSync(path.join(installDirectory, RELEASE_MANIFEST_FILENAME), 'utf8')
404 + )
405 + ).toEqual(manifest);
406 + }).pipe(
407 + Effect.ensuring(
408 + Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true }))
409 + )
410 + );
411 + }
412 + );
413 +
414 + it.effect('[Given] an archive with an invalid manifest [Then] repair restores nothing', () => {
415 + const installDirectory = fs.mkdtempSync(
416 + path.join(os.tmpdir(), 'composio-run-repair-manifest-')
417 + );
418 + const execPath = path.join(installDirectory, 'composio');
419 + stubRepairFetch();
420 + mockManifestArchive(LIVE_COMPANIONS_WITH_BUNDLES, {
421 + schemaVersion: 1,
422 + minimumUpgradeFrom: '0.1.26',
423 + companions: ['../outside.mjs'],
424 + });
425 +
426 + return Effect.gen(function* () {
427 + const error = yield* repairMissingInstalledRunCompanionModules({
428 + callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs',
429 + execPath,
430 + appVersion: '0.0.0-test',
431 + }).pipe(Effect.flip);
432 +
433 + expect(error._tag).toBe('services/RunCompanionRepairError');
434 + expect(error.message).toContain(`broken ${RELEASE_MANIFEST_FILENAME}`);
435 + expect(fs.readdirSync(installDirectory)).toEqual([]);
436 + }).pipe(
437 + Effect.ensuring(
438 + Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true }))
439 + )
440 + );
441 + });
442 +
443 it.effect('[Given] an incomplete archive [Then] repair preserves release metadata', () => {
444 const installDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-repair-test-'));
445 const execPath = path.join(installDirectory, 'composio');
ts/packages/cli/test/src/services/upgrade-binary.test.ts
+196
@@ -14,12 +14,14 @@ import { getTerminalCapabilities, TerminalUI } from 'src/services/terminal-ui';
14 import {
15 formatDownloadProgress,
16 formatMegabytes,
17 + UpgradeBelowMinimumError,
18 UpgradeBinary,
19 UpgradeBinaryError,
20 } from 'src/services/upgrade-binary';
21 import { NodeOs } from 'src/services/node-os';
22 import {
23 collectExpectedRunCompanionAssetRelativePaths,
24 + RELEASE_MANIFEST_FILENAME,
25 RUN_COMPANION_RELEASE_TAG_FILENAME,
26 } from 'src/services/run-companion-modules';
27
@@ -762,6 +764,200 @@ describe('UpgradeBinary', () => {
764 });
765 });
766
767 +describe('UpgradeBinary with a release manifest', () => {
768 + const LIVE_COMPANIONS = [
769 + 'generation-runtime.mjs',
770 + 'run-helpers-runtime.mjs',
771 + 'services/generation-runtime.mjs',
772 + 'services/run-helpers-runtime.mjs',
773 + ];
774 + // An archive from a release that dropped the generation runtime.
775 + const RUN_HELPERS_ONLY = ['run-helpers-runtime.mjs', 'services/run-helpers-runtime.mjs'];
776 +
777 + const manifestJson = (fields: Record<string, unknown> = {}) =>
778 + `${JSON.stringify(
779 + { schemaVersion: 1, minimumUpgradeFrom: '0.1.26', companions: RUN_HELPERS_ONLY, ...fields },
780 + null,
781 + 2
782 + )}\n`;
783 +
784 + const setUpLocalUpgrade = (installedTag: string, archiveFiles: ReadonlyArray<string>) =>
785 + Effect.gen(function* () {
786 + const fs = yield* FileSystem.FileSystem;
787 + const path = yield* Path.Path;
788 + const installDir = yield* fs.makeTempDirectoryScoped({
789 + prefix: 'composio-manifest-install-',
790 + });
791 + const sourceDir = yield* fs.makeTempDirectoryScoped({ prefix: 'composio-manifest-source-' });
792 + const execPath = path.join(installDir, 'composio');
793 + const sourceBinaryPath = path.join(sourceDir, 'composio');
794 + yield* fs.writeFileString(execPath, 'old-binary');
795 + yield* fs.writeFileString(sourceBinaryPath, 'new-binary');
796 + yield* fs.writeFileString(
797 + path.join(installDir, RUN_COMPANION_RELEASE_TAG_FILENAME),
798 + `${installedTag}\n`
799 + );
800 + yield* writeCompanionFiles(installDir, LIVE_COMPANIONS, 'old-support');
801 + yield* writeCompanionFiles(sourceDir, archiveFiles, 'new-support');
802 + vi.spyOn(process, 'execPath', 'get').mockReturnValue(execPath);
803 +
804 + const installedFiles = Effect.gen(function* () {
805 + const entries = yield* fs.readDirectory(installDir, { recursive: true });
806 + const files = yield* Effect.filter(entries, entry =>
807 + Effect.map(fs.stat(path.join(installDir, entry)), info => info.type === 'File')
808 + );
809 + return files.map(entry => entry.replaceAll(path.sep, '/')).sort();
810 + });
811 + const readInstalled = (relativePath: string) =>
812 + fs.readFileString(path.join(installDir, relativePath));
813 + const writeSourceManifest = (contents: string) =>
814 + fs.writeFileString(path.join(sourceDir, RELEASE_MANIFEST_FILENAME), contents);
815 + const upgradeConfig: Array<[string, string]> = [
816 + ['DEBUG_OVERRIDE_UPGRADE_TARGET', sourceBinaryPath],
817 + ];
818 +
819 + return {
820 + installDir,
821 + upgradeConfig,
822 + installedFiles,
823 + readInstalled,
824 + writeSourceManifest,
825 + };
826 + });
827 +
828 + const UNCHANGED_INSTALL = [
829 + RUN_COMPANION_RELEASE_TAG_FILENAME,
830 + 'composio',
831 + ...LIVE_COMPANIONS,
832 + ].sort();
833 +
834 + it.effect('leaves companions the archive does not list in place when it downgrades', () => {
835 + vi.stubGlobal('Bun', { which: vi.fn(() => null) });
836 +
837 + return Effect.gen(function* () {
838 + const install = yield* setUpLocalUpgrade('@composio/cli@0.6.0', RUN_HELPERS_ONLY);
839 + yield* install.writeSourceManifest(manifestJson());
840 +
841 + yield* runUpgradeSuccess(install.upgradeConfig, { tag: '@composio/cli@0.5.0' });
842 +
843 + expect(yield* install.installedFiles).toEqual(
844 + [...UNCHANGED_INSTALL, RELEASE_MANIFEST_FILENAME].sort()
845 + );
846 + expect(yield* install.readInstalled('composio')).toBe('new-binary');
847 + expect(yield* install.readInstalled('services/run-helpers-runtime.mjs')).toBe('new-support');
848 + expect(yield* install.readInstalled('services/generation-runtime.mjs')).toBe('old-support');
849 + expect(yield* install.readInstalled(RELEASE_MANIFEST_FILENAME)).toBe(manifestJson());
850 + expect(yield* install.readInstalled(RUN_COMPANION_RELEASE_TAG_FILENAME)).toBe(
851 + '@composio/cli@0.5.0\n'
852 + );
853 + }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks));
854 + });
855 +
856 + it.effect('refuses an archive whose minimum version is above the running one', () => {
857 + vi.stubGlobal('Bun', { which: vi.fn(() => null) });
858 +
859 + return Effect.gen(function* () {
860 + const install = yield* setUpLocalUpgrade('@composio/cli@0.4.0', LIVE_COMPANIONS);
861 + yield* install.writeSourceManifest(
862 + manifestJson({ minimumUpgradeFrom: '0.5.0', companions: LIVE_COMPANIONS })
863 + );
864 +
865 + const error = yield* runUpgrade(install.upgradeConfig, { tag: '@composio/cli@0.9.0' });
866 +
867 + expect(error).toBeInstanceOf(UpgradeBelowMinimumError);
868 + expect(error).toMatchObject({ currentVersion: '0.4.0', minimumUpgradeFrom: '0.5.0' });
869 + expect(error.message).toContain('curl -fsSL https://composio.dev/install | sh');
870 + expect(yield* install.installedFiles).toEqual(UNCHANGED_INSTALL);
871 + expect(yield* install.readInstalled('composio')).toBe('old-binary');
872 + expect(yield* install.readInstalled('run-helpers-runtime.mjs')).toBe('old-support');
873 + expect(yield* install.readInstalled(RUN_COMPANION_RELEASE_TAG_FILENAME)).toBe(
874 + '@composio/cli@0.4.0\n'
875 + );
876 + }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks));
877 + });
878 +
879 + it.effect('does not check the minimum version from a source build with no release tag', () => {
880 + vi.stubGlobal('Bun', { which: vi.fn(() => null) });
881 +
882 + return Effect.gen(function* () {
883 + const fs = yield* FileSystem.FileSystem;
884 + const path = yield* Path.Path;
885 + const install = yield* setUpLocalUpgrade('@composio/cli@0.4.0', RUN_HELPERS_ONLY);
886 + yield* fs.remove(path.join(install.installDir, RUN_COMPANION_RELEASE_TAG_FILENAME));
887 + yield* install.writeSourceManifest(manifestJson({ minimumUpgradeFrom: '99.0.0' }));
888 +
889 + yield* runUpgradeSuccess(install.upgradeConfig);
890 +
891 + expect(yield* install.readInstalled('composio')).toBe('new-binary');
892 + }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks));
893 + });
894 +
895 + it.effect('fails without touching the install when a listed companion is missing', () => {
896 + vi.stubGlobal('Bun', { which: vi.fn(() => null) });
897 +
898 + return Effect.gen(function* () {
899 + const install = yield* setUpLocalUpgrade('@composio/cli@0.5.0', ['run-helpers-runtime.mjs']);
900 + yield* install.writeSourceManifest(manifestJson());
901 +
902 + const error = yield* runUpgrade(install.upgradeConfig, { tag: '@composio/cli@0.6.0' });
903 +
904 + expect(error).toBeInstanceOf(UpgradeBinaryError);
905 + expect(error.message).toBe('Downloaded binary package is incomplete');
906 + expect(String((error as UpgradeBinaryError).cause)).toContain(
907 + 'services/run-helpers-runtime.mjs'
908 + );
909 + expect(yield* install.installedFiles).toEqual(UNCHANGED_INSTALL);
910 + expect(yield* install.readInstalled('composio')).toBe('old-binary');
911 + expect(yield* install.readInstalled('run-helpers-runtime.mjs')).toBe('old-support');
912 + }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks));
913 + });
914 +
915 + it.effect.each([
916 + { problem: 'a path that leaves the package', fields: { companions: ['../x'] } },
917 + { problem: 'an absolute path', fields: { companions: ['/etc/passwd'] } },
918 + { problem: 'an unknown schema version', fields: { schemaVersion: 2 } },
919 + { problem: 'a minimum that is not a version', fields: { minimumUpgradeFrom: 'latest' } },
920 + { problem: 'no companions', fields: { companions: [] } },
921 + ])('fails without touching the install when the manifest has $problem', ({ fields }) => {
922 + vi.stubGlobal('Bun', { which: vi.fn(() => null) });
923 +
924 + return Effect.gen(function* () {
925 + const install = yield* setUpLocalUpgrade('@composio/cli@0.5.0', LIVE_COMPANIONS);
926 + yield* install.writeSourceManifest(manifestJson(fields));
927 +
928 + const error = yield* runUpgrade(install.upgradeConfig, { tag: '@composio/cli@0.6.0' });
929 +
930 + expect(error).toBeInstanceOf(UpgradeBinaryError);
931 + expect(error.message).toContain('Downloaded binary package is invalid');
932 + expect(yield* install.installedFiles).toEqual(UNCHANGED_INSTALL);
933 + expect(yield* install.readInstalled('composio')).toBe('old-binary');
934 + }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks));
935 + });
936 +
937 + it.effect('removes the installed manifest when the archive has none', () => {
938 + vi.stubGlobal('Bun', { which: vi.fn(() => null) });
939 +
940 + return Effect.gen(function* () {
941 + const fs = yield* FileSystem.FileSystem;
942 + const path = yield* Path.Path;
943 + const install = yield* setUpLocalUpgrade('@composio/cli@0.6.0', LIVE_COMPANIONS);
944 + yield* fs.writeFileString(
945 + path.join(install.installDir, RELEASE_MANIFEST_FILENAME),
946 + manifestJson()
947 + );
948 + yield* fs.writeFileString(path.join(install.installDir, 'run-subagent-shared.mjs'), '');
949 +
950 + yield* runUpgradeSuccess(install.upgradeConfig, { tag: '@composio/cli@0.4.2' });
951 +
952 + expect(yield* install.installedFiles).toEqual(
953 + [...UNCHANGED_INSTALL, 'run-subagent-shared.mjs'].sort()
954 + );
955 + expect(yield* install.readInstalled('composio')).toBe('new-binary');
956 + expect(yield* install.readInstalled('generation-runtime.mjs')).toBe('new-support');
957 + }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks));
958 + });
959 +});
960 +
961 describe('UpgradeBinary update-check cache', () => {
962 // The cache that misled the user: written before 0.5.0 was released.
963 const staleState = { lastChecked: '2026-10-21T14:31:56.646Z', latestVersion: '0.4.5' };