Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
fix(cli): replace repaired companions atomically
jkomyno committed
Aug 1, 2026 at 19:04 UTC
a32712ffd97dafe747094a3c494621fea4ab637b
2 files changed
+136
-3
ts/packages/cli/src/services/run-companion-modules.ts
+14
-1
@@ -10,6 +10,7 @@ import extractZip from 'extract-zip';
10
import { IS_RELEASE_BUILD } from 'src/constants';
11
import { GitHubRelease } from 'src/effects/resolve-cli-release';
12
import { BaseConfigProviderLive, extendConfigProvider } from 'src/services/config';
13
+import { atomicReplaceFile } from 'src/utils/atomic-replace';
14
import { parseChecksumsText, sha256Hex } from 'src/utils/checksums';
15
import { CLI_RELEASE_TAG_PREFIX } from 'src/utils/cli-release-version';
16
@@ -628,7 +629,19 @@ export const repairMissingInstalledRunCompanionModules = ({
629
630
const targetPath = path.join(installDirectory, relativePath);
631
yield* fs.makeDirectory(path.dirname(targetPath), { recursive: true });
631
- yield* fs.copyFile(sourcePath, targetPath);
632
+ yield* atomicReplaceFile({ sourcePath, targetPath }).pipe(
633
+ Effect.mapError(
634
+ error =>
635
+ new RunCompanionRepairError({
636
+ message: [
637
+ `Unable to restore the files required by 'composio run' for ${releaseTag}.`,
638
+ error.message,
639
+ `Reinstall the CLI, or set GITHUB_TAG to the exact release tag for this build and try again.`,
640
+ ].join('\n'),
641
+ cause: error.cause,
642
+ })
643
+ )
644
+ );
645
}
646
647
yield* writeInstalledReleaseTag(installDirectory, release.tag_name);
ts/packages/cli/test/src/services/run-companion-modules.test.ts
+122
-2
@@ -2,18 +2,138 @@ import * as fs from 'node:fs';
2
import * as os from 'node:os';
3
import * as path from 'node:path';
4
import { BunContext } from '@effect/platform-bun';
5
-import { afterEach, describe, expect, layer, vi } from '@effect/vitest';
5
+import { afterEach, describe, expect, layer } from '@effect/vitest';
6
import { Effect } from 'effect';
7
-import { repairMissingInstalledRunCompanionModules } from 'src/services/run-companion-modules';
7
+import { vi } from 'vitest';
8
+import {
9
+ repairMissingInstalledRunCompanionModules,
10
+ RUN_COMPANION_MODULE_FILENAMES,
11
+ RUN_COMPANION_RELEASE_TAG_FILENAME,
12
+ RUN_COMPANION_STATIC_ASSET_RELATIVE_PATHS,
13
+} from 'src/services/run-companion-modules';
14
import { BaseConfigProviderLive, extendConfigProvider } from 'src/services/config';
15
16
+const extractZipMock = vi.hoisted(() => vi.fn());
17
+vi.mock('extract-zip', () => ({ default: extractZipMock }));
18
+
19
+const TEST_RELEASE_TAG = '@composio/cli@0.3.0-test';
20
+const TEST_BINARY_ASSET_NAMES = [
21
+ 'composio-darwin-aarch64.zip',
22
+ 'composio-darwin-x64.zip',
23
+ 'composio-linux-aarch64.zip',
24
+ 'composio-linux-x64.zip',
25
+];
26
+const TEST_COMPANION_RELATIVE_PATHS = [
27
+ ...RUN_COMPANION_MODULE_FILENAMES,
28
+ ...RUN_COMPANION_STATIC_ASSET_RELATIVE_PATHS,
29
+].sort();
30
+
31
+const stubRepairFetch = () => {
32
+ const fetchMock = vi.fn((url: string) =>
33
+ Promise.resolve(
34
+ url.includes('/releases/tags/')
35
+ ? new Response(
36
+ JSON.stringify({
37
+ tag_name: TEST_RELEASE_TAG,
38
+ assets: TEST_BINARY_ASSET_NAMES.map(name => ({
39
+ name,
40
+ browser_download_url: `https://download.test/${name}`,
41
+ })),
42
+ }),
43
+ { status: 200, headers: { 'content-type': 'application/json' } }
44
+ )
45
+ : new Response(new Uint8Array([1]), { status: 200 })
46
+ )
47
+ );
48
+ vi.stubGlobal('fetch', fetchMock);
49
+ vi.stubEnv('GITHUB_TAG', TEST_RELEASE_TAG);
50
+ return fetchMock;
51
+};
52
+
53
+const mockArchiveContents = (missingRelativePath?: string) => {
54
+ extractZipMock.mockImplementation(
55
+ async (archivePath: string, options: { readonly dir: string }) => {
56
+ const packageDirectory = path.join(options.dir, path.parse(archivePath).name);
57
+ for (const relativePath of TEST_COMPANION_RELATIVE_PATHS) {
58
+ if (relativePath === missingRelativePath) continue;
59
+ const filePath = path.join(packageDirectory, relativePath);
60
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
61
+ fs.writeFileSync(filePath, `new:${relativePath}`);
62
+ }
63
+ }
64
+ );
65
+};
66
+
67
describe('run-companion-modules', () => {
68
afterEach(() => {
69
+ extractZipMock.mockReset();
70
vi.unstubAllGlobals();
71
vi.unstubAllEnvs();
72
});
73
74
layer(BunContext.layer)(it => {
75
+ it.effect('[Given] a complete archive [Then] repair atomically replaces companions', () => {
76
+ const installDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-repair-test-'));
77
+ const execPath = path.join(installDirectory, 'composio');
78
+ const companionRelativePath = TEST_COMPANION_RELATIVE_PATHS[0]!;
79
+ const companionPath = path.join(installDirectory, companionRelativePath);
80
+ const releaseTagPath = path.join(installDirectory, RUN_COMPANION_RELEASE_TAG_FILENAME);
81
+ fs.mkdirSync(path.dirname(companionPath), { recursive: true });
82
+ fs.writeFileSync(companionPath, 'old-companion');
83
+ fs.writeFileSync(releaseTagPath, '@composio/cli@0.2.31\n');
84
+ const originalCompanion = fs.statSync(companionPath);
85
+ const fetchMock = stubRepairFetch();
86
+ mockArchiveContents();
87
+
88
+ return Effect.gen(function* () {
89
+ const result = yield* repairMissingInstalledRunCompanionModules({
90
+ callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs',
91
+ execPath,
92
+ appVersion: '0.0.0-test',
93
+ });
94
+
95
+ expect(result).toEqual({ repaired: true, releaseTag: TEST_RELEASE_TAG });
96
+ expect(fs.readFileSync(companionPath, 'utf8')).toBe(`new:${companionRelativePath}`);
97
+ const replacedCompanion = fs.statSync(companionPath);
98
+ expect(replacedCompanion.dev).toBe(originalCompanion.dev);
99
+ expect(replacedCompanion.ino).not.toBe(originalCompanion.ino);
100
+ expect(fs.readFileSync(releaseTagPath, 'utf8')).toBe(`${TEST_RELEASE_TAG}\n`);
101
+ expect(fetchMock).toHaveBeenCalledTimes(2);
102
+ }).pipe(
103
+ Effect.ensuring(
104
+ Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true }))
105
+ )
106
+ );
107
+ });
108
+
109
+ it.effect('[Given] an incomplete archive [Then] repair preserves release metadata', () => {
110
+ const installDirectory = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-repair-test-'));
111
+ const execPath = path.join(installDirectory, 'composio');
112
+ const releaseTagPath = path.join(installDirectory, RUN_COMPANION_RELEASE_TAG_FILENAME);
113
+ const previousReleaseTag = '@composio/cli@0.2.31\n';
114
+ const missingRelativePath = TEST_COMPANION_RELATIVE_PATHS.at(-1)!;
115
+ fs.writeFileSync(releaseTagPath, previousReleaseTag);
116
+ stubRepairFetch();
117
+ mockArchiveContents(missingRelativePath);
118
+
119
+ return Effect.gen(function* () {
120
+ const error = yield* repairMissingInstalledRunCompanionModules({
121
+ callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs',
122
+ execPath,
123
+ appVersion: '0.0.0-test',
124
+ }).pipe(Effect.flip);
125
+
126
+ expect(error.message).toContain(
127
+ `missing ${missingRelativePath}; cannot restore the files required by 'composio run'`
128
+ );
129
+ expect(fs.readFileSync(releaseTagPath, 'utf8')).toBe(previousReleaseTag);
130
+ }).pipe(
131
+ Effect.ensuring(
132
+ Effect.sync(() => fs.rmSync(installDirectory, { recursive: true, force: true }))
133
+ )
134
+ );
135
+ });
136
+
137
it.effect(
138
'[Given] malformed release metadata [Then] repair rejects it before using release assets',
139
() => {