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
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' };