@devries-cameron20 / composio Mirror · read-only / commits / 96b199c32
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, 17:15 UTC

fix(cli): replace upgraded binaries atomically

jkomyno committed Aug 1, 2026 at 19:01 UTC 96b199c3268eb423c4b9d65a43c5245b7f73c17f
2 files changed +263 -72
ts/packages/cli/src/services/upgrade-binary.ts
+91 -58
@@ -1,4 +1,13 @@
1 -import { Data, Effect, Config, Match, Option, Predicate, Record as EffectRecord } from 'effect';
1 +import {
2 + Data,
3 + Effect,
4 + Config,
5 + Match,
6 + Option,
7 + Predicate,
8 + Record as EffectRecord,
9 + Scope,
10 +} from 'effect';
11 import { HttpClient, HttpClientResponse, FileSystem, Path } from '@effect/platform';
12 import { APP_VERSION } from '../constants';
13 import { DEBUG_OVERRIDE_CONFIG } from 'src/effects/debug-config';
@@ -7,6 +16,11 @@ import { detectPlatform, type PlatformArch } from 'src/effects/detect-platform';
16 import { CompareSemverError, semverComparator } from 'src/effects/compare-semver';
17 import { fetchLatestCliRelease, GitHubRelease } from 'src/effects/resolve-cli-release';
18 import { parseChecksumsText, sha256Hex } from 'src/utils/checksums';
19 +import {
20 + atomicReplaceDirectory,
21 + atomicReplaceFile,
22 + type AtomicReplaceError,
23 +} from 'src/utils/atomic-replace';
24
25 // Note: `node:zlib` does not support Github's zip files
26 import extractZip from 'extract-zip';
@@ -14,6 +28,7 @@ import { renderPrettyError } from './utils/pretty-error';
28 import { TerminalUI } from './terminal-ui';
29 import {
30 collectExpectedRunCompanionAssetRelativePaths,
31 + RUN_COMPANION_RELEASE_TAG_FILENAME,
32 resolveRunningCliReleaseTag,
33 writeInstalledReleaseTag,
34 } from './run-companion-modules';
@@ -160,9 +175,9 @@ const fetchLatestRelease = (
175 return release;
176 });
177
163 -const provideFsAndPath = <A, E>(
164 - { fs, path }: UpgradeBinaryContext,
165 - effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path>
178 +const provideFsAndPath = <A, E, R>(
179 + { fs, path }: Pick<UpgradeBinaryContext, 'fs' | 'path'>,
180 + effect: Effect.Effect<A, E, FileSystem.FileSystem | Path.Path | R>
181 ) =>
182 effect.pipe(
183 Effect.provideService(FileSystem.FileSystem, fs),
@@ -414,31 +429,23 @@ const getCurrentExecutablePath = Effect.fn(function* () {
429 /**
430 * Replace current executable binary with the new target one.
431 */
432 +const mapAtomicReplaceError = (message: string) => (error: AtomicReplaceError) =>
433 + new UpgradeBinaryError({
434 + cause: error.cause,
435 + message,
436 + });
437 +
438 const replaceBinary = (
418 - ctx: UpgradeBinaryContext,
439 + ctx: Pick<UpgradeBinaryContext, 'fs' | 'path'>,
440 sourcePath: string,
441 targetPath: string,
442 options: {
443 releaseTag?: string;
444 } = {}
424 -): Effect.Effect<void, UpgradeBinaryError> =>
445 +): Effect.Effect<void, UpgradeBinaryError, Scope.Scope> =>
446 Effect.gen(function* () {
447 const { fs, path } = ctx;
448 yield* Effect.logDebug(`Replacing binary: ${sourcePath} -> ${targetPath}`);
428 - yield* fs
429 - .copy(sourcePath, targetPath, {
430 - // Note: without `overwrite: true`, the copy operation will silently bail out
431 - overwrite: true,
432 - })
433 - .pipe(
434 - Effect.mapError(
435 - cause =>
436 - new UpgradeBinaryError({
437 - cause,
438 - message: 'Failed to replace binary',
439 - })
440 - )
441 - );
449
450 const sourceDirectory = path.dirname(sourcePath);
451 const targetDirectory = path.dirname(targetPath);
@@ -446,6 +453,12 @@ const replaceBinary = (
453 ctx,
454 collectExpectedRunCompanionAssetRelativePaths(sourceDirectory)
455 );
456 + const companionReplacements: Array<{
457 + readonly relativePath: string;
458 + readonly sourcePath: string;
459 + readonly targetPath: string;
460 + }> = [];
461 +
462 for (const relativePath of companionRelativePaths) {
463 const sourceCompanion = path.join(sourceDirectory, relativePath);
464 const sourceExists = yield* fs
@@ -461,64 +474,82 @@ const replaceBinary = (
474 );
475 }
476
464 - const targetCompanion = path.join(targetDirectory, relativePath);
465 - yield* fs.makeDirectory(path.dirname(targetCompanion), { recursive: true }).pipe(
466 - Effect.mapError(
467 - cause =>
468 - new UpgradeBinaryError({
469 - cause,
470 - message: `Failed to create companion module directory: ${relativePath}`,
471 - })
472 - )
473 - );
474 -
475 - yield* fs
476 - .copy(sourceCompanion, targetCompanion, {
477 - overwrite: true,
478 - })
479 - .pipe(
480 - Effect.mapError(
481 - cause =>
482 - new UpgradeBinaryError({
483 - cause,
484 - message: `Failed to replace companion module: ${relativePath}`,
485 - })
486 - )
487 - );
477 + companionReplacements.push({
478 + relativePath,
479 + sourcePath: sourceCompanion,
480 + targetPath: path.join(targetDirectory, relativePath),
481 + });
482 }
483
484 const localToolsAssetSource = path.join(sourceDirectory, LOCAL_TOOLS_BINARY_ASSET_DIRNAME);
485 const localToolsAssetExists = yield* fs
486 .exists(localToolsAssetSource)
487 .pipe(Effect.catchAll(() => Effect.succeed(false)));
494 - if (localToolsAssetExists) {
495 - const localToolsAssetTarget = path.join(targetDirectory, LOCAL_TOOLS_BINARY_ASSET_DIRNAME);
496 - // Replace the whole asset directory: drop the previous tree, then copy
497 - // the new one (fs.copy is recursive for directories).
498 - yield* fs.remove(localToolsAssetTarget, { recursive: true, force: true }).pipe(
499 - Effect.andThen(fs.copy(localToolsAssetSource, localToolsAssetTarget, { overwrite: true })),
488 + const localToolsAssetTarget = path.join(targetDirectory, LOCAL_TOOLS_BINARY_ASSET_DIRNAME);
489 +
490 + const releaseTag = options.releaseTag;
491 + const stagedReleaseTagPath = path.join(sourceDirectory, RUN_COMPANION_RELEASE_TAG_FILENAME);
492 + if (releaseTag) {
493 + yield* provideFsAndPath(ctx, writeInstalledReleaseTag(sourceDirectory, releaseTag)).pipe(
494 Effect.mapError(
495 error =>
496 new UpgradeBinaryError({
497 cause: error,
504 - message: 'Failed to replace local-tool binary assets',
498 + message: 'Failed to update installed release metadata',
499 })
500 )
501 );
502 }
503
510 - const releaseTag = options.releaseTag;
511 - if (releaseTag) {
512 - yield* provideFsAndPath(ctx, writeInstalledReleaseTag(targetDirectory, releaseTag)).pipe(
504 + for (const replacement of companionReplacements) {
505 + const {
506 + relativePath,
507 + sourcePath: sourceCompanion,
508 + targetPath: targetCompanion,
509 + } = replacement;
510 + yield* fs.makeDirectory(path.dirname(targetCompanion), { recursive: true }).pipe(
511 Effect.mapError(
514 - error =>
512 + cause =>
513 new UpgradeBinaryError({
516 - cause: error,
517 - message: 'Failed to update installed release metadata',
514 + cause,
515 + message: `Failed to create companion module directory: ${relativePath}`,
516 })
517 )
518 );
519 +
520 + yield* provideFsAndPath(
521 + ctx,
522 + atomicReplaceFile({ sourcePath: sourceCompanion, targetPath: targetCompanion })
523 + ).pipe(
524 + Effect.mapError(
525 + mapAtomicReplaceError(`Failed to replace companion module: ${relativePath}`)
526 + )
527 + );
528 + }
529 +
530 + if (localToolsAssetExists) {
531 + yield* provideFsAndPath(
532 + ctx,
533 + atomicReplaceDirectory({
534 + sourcePath: localToolsAssetSource,
535 + targetPath: localToolsAssetTarget,
536 + })
537 + ).pipe(Effect.mapError(mapAtomicReplaceError('Failed to replace local-tool binary assets')));
538 }
539 +
540 + if (releaseTag) {
541 + yield* provideFsAndPath(
542 + ctx,
543 + atomicReplaceFile({
544 + sourcePath: stagedReleaseTagPath,
545 + targetPath: path.join(targetDirectory, RUN_COMPANION_RELEASE_TAG_FILENAME),
546 + })
547 + ).pipe(Effect.mapError(mapAtomicReplaceError('Failed to update installed release metadata')));
548 + }
549 +
550 + yield* provideFsAndPath(ctx, atomicReplaceFile({ sourcePath, targetPath, mode: 0o755 })).pipe(
551 + Effect.mapError(mapAtomicReplaceError('Failed to replace binary'))
552 + );
553 });
554
555 /**
@@ -546,7 +577,9 @@ const upgrade = (
577 // If local binary path is provided (for testing), use it directly
578 if (Option.isSome(upgradeTargetOpt)) {
579 yield* ui.log.info(`New local version available (current: ${currentReleaseIdentifier})`);
549 - yield* replaceBinary(ctx, upgradeTargetOpt.value, currentPath);
580 + yield* replaceBinary(ctx, upgradeTargetOpt.value, currentPath, {
581 + releaseTag: explicitTag,
582 + });
583 yield* ui.outro('Upgrade completed');
584 return undefined;
585 }
ts/packages/cli/test/src/services/upgrade-binary.test.ts
+172 -14
@@ -1,12 +1,16 @@
1 import { describe, expect, it, vi } from '@effect/vitest';
2 -import { ConfigProvider, Effect, Layer } from 'effect';
2 +import { ConfigProvider, Effect, Layer, Option } from 'effect';
3 import { FetchHttpClient, FileSystem, Path } from '@effect/platform';
4 +import * as PlatformError from '@effect/platform/Error';
5 import { BunFileSystem, BunPath } from '@effect/platform-bun';
6 import { withHttpServer } from 'test/__utils__/http-server';
7 import { getTerminalCapabilities, TerminalUI } from 'src/services/terminal-ui';
8 import { UpgradeBinary, UpgradeBinaryError } from 'src/services/upgrade-binary';
9 import { NodeOs } from 'src/services/node-os';
9 -import { collectExpectedRunCompanionAssetRelativePaths } from 'src/services/run-companion-modules';
10 +import {
11 + collectExpectedRunCompanionAssetRelativePaths,
12 + RUN_COMPANION_RELEASE_TAG_FILENAME,
13 +} from 'src/services/run-companion-modules';
14
15 const TerminalUINoop = Layer.succeed(
16 TerminalUI,
@@ -55,11 +59,15 @@ const NodeOsTest = Layer.succeed(
59
60 const TestPlatform = Layer.mergeAll(BunFileSystem.layer, BunPath.layer);
61
62 +type UpgradeOptions = {
63 + readonly prerelease?: boolean;
64 + readonly tag?: string;
65 +};
66 +
67 const makeUpgradeEffect = (
68 configEntries: ReadonlyArray<[string, string]>,
60 - options?: {
61 - prerelease?: boolean;
62 - }
69 + options?: UpgradeOptions,
70 + fileSystemLayer: Layer.Layer<FileSystem.FileSystem> = BunFileSystem.layer
71 ) =>
72 Effect.gen(function* () {
73 const service = yield* UpgradeBinary;
@@ -67,7 +75,7 @@ const makeUpgradeEffect = (
75 }).pipe(
76 Effect.provide(UpgradeBinary.Default),
77 Effect.provide(FetchHttpClient.layer),
70 - Effect.provide(BunFileSystem.layer),
78 + Effect.provide(fileSystemLayer),
79 Effect.provide(TerminalUINoop),
80 Effect.provide(NodeOsTest),
81 Effect.withConfigProvider(ConfigProvider.fromMap(new Map(configEntries))),
@@ -76,17 +84,43 @@ const makeUpgradeEffect = (
84
85 const runUpgrade = (
86 configEntries: ReadonlyArray<[string, string]>,
79 - options?: {
80 - prerelease?: boolean;
81 - }
82 -) => makeUpgradeEffect(configEntries, options).pipe(Effect.flip);
87 + options?: UpgradeOptions,
88 + fileSystemLayer?: Layer.Layer<FileSystem.FileSystem>
89 +) => makeUpgradeEffect(configEntries, options, fileSystemLayer).pipe(Effect.flip);
90
91 const runUpgradeSuccess = (
92 configEntries: ReadonlyArray<[string, string]>,
86 - options?: {
87 - prerelease?: boolean;
88 - }
89 -) => makeUpgradeEffect(configEntries, options);
93 + options?: UpgradeOptions,
94 + fileSystemLayer?: Layer.Layer<FileSystem.FileSystem>
95 +) => makeUpgradeEffect(configEntries, options, fileSystemLayer);
96 +
97 +const failRenameTo = (failedTargetPath: string) =>
98 + Layer.effect(
99 + FileSystem.FileSystem,
100 + Effect.map(
101 + FileSystem.FileSystem,
102 + fs =>
103 + new Proxy(fs, {
104 + get(target, property, receiver) {
105 + if (property !== 'rename') {
106 + return Reflect.get(target, property, receiver);
107 + }
108 +
109 + return (oldPath: string, newPath: string) =>
110 + newPath === failedTargetPath
111 + ? Effect.fail(
112 + new PlatformError.SystemError({
113 + reason: 'Busy',
114 + module: 'FileSystem',
115 + method: 'rename',
116 + pathOrDescriptor: newPath,
117 + })
118 + )
119 + : target.rename(oldPath, newPath);
120 + },
121 + })
122 + )
123 + ).pipe(Layer.provide(BunFileSystem.layer));
124
125 /**
126 * Bridge the promise-based `withHttpServer` harness into a scoped Effect:
@@ -434,11 +468,16 @@ describe('UpgradeBinary', () => {
468 'darwin-arm64',
469 'imessage-cli'
470 );
471 + const staleLocalToolPath = path.join(installDir, 'local-tools-binaries', 'stale-tool');
472
473 yield* fs.writeFileString(fakeExecPath, 'old-binary');
474 yield* fs.writeFileString(sourceBinaryPath, 'new-binary');
475 yield* fs.makeDirectory(path.dirname(sourceLocalToolPath), { recursive: true });
476 yield* fs.writeFileString(sourceLocalToolPath, 'imessage-sidecar');
477 + yield* fs.makeDirectory(path.dirname(installedLocalToolPath), { recursive: true });
478 + yield* fs.writeFileString(installedLocalToolPath, 'old-imessage-sidecar');
479 + yield* fs.writeFileString(staleLocalToolPath, 'stale');
480 + const originalBinaryInfo = yield* fs.stat(fakeExecPath);
481
482 vi.spyOn(process, 'execPath', 'get').mockReturnValue(fakeExecPath);
483
@@ -456,8 +495,127 @@ describe('UpgradeBinary', () => {
495
496 expect(result).toBeUndefined();
497 expect(yield* fs.readFileString(fakeExecPath)).toBe('new-binary');
498 + const replacedBinaryInfo = yield* fs.stat(fakeExecPath);
499 + expect(replacedBinaryInfo.dev).toBe(originalBinaryInfo.dev);
500 + expect(Option.getOrThrow(replacedBinaryInfo.ino)).not.toBe(
501 + Option.getOrThrow(originalBinaryInfo.ino)
502 + );
503 + expect(replacedBinaryInfo.mode & 0o777).toBe(0o755);
504 expect(yield* fs.exists(installedLocalToolPath)).toBe(true);
505 expect(yield* fs.readFileString(installedLocalToolPath)).toBe('imessage-sidecar');
506 + expect(yield* fs.exists(staleLocalToolPath)).toBe(false);
507 + }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks));
508 + });
509 +
510 + it.scoped('commits companions and release metadata before replacing the binary', () => {
511 + vi.stubGlobal('Bun', { which: vi.fn(() => null) });
512 +
513 + return Effect.gen(function* () {
514 + const fs = yield* FileSystem.FileSystem;
515 + const path = yield* Path.Path;
516 + const installDir = yield* fs.makeTempDirectoryScoped({
517 + prefix: 'composio-ordered-upgrade-target-',
518 + });
519 + const sourceDir = yield* fs.makeTempDirectoryScoped({
520 + prefix: 'composio-ordered-upgrade-source-',
521 + });
522 + const fakeExecPath = path.join(installDir, 'composio');
523 + const sourceBinaryPath = path.join(sourceDir, 'composio');
524 + const releaseTagPath = path.join(installDir, RUN_COMPANION_RELEASE_TAG_FILENAME);
525 + const newReleaseTag = '@composio/cli@0.3.0';
526 + yield* fs.writeFileString(fakeExecPath, 'old-binary');
527 + yield* fs.writeFileString(sourceBinaryPath, 'new-binary');
528 + yield* fs.writeFileString(releaseTagPath, '@composio/cli@0.2.31\n');
529 + const originalReleaseTagInfo = yield* fs.stat(releaseTagPath);
530 +
531 + const companionRelativePaths =
532 + yield* collectExpectedRunCompanionAssetRelativePaths(sourceDir);
533 + for (const relativePath of companionRelativePaths) {
534 + const companionPath = path.join(sourceDir, relativePath);
535 + yield* fs.makeDirectory(path.dirname(companionPath), { recursive: true });
536 + yield* fs.writeFileString(companionPath, 'new-support-file');
537 + }
538 + const observedCompanionPath = path.join(installDir, companionRelativePaths[0]!);
539 + yield* fs.makeDirectory(path.dirname(observedCompanionPath), { recursive: true });
540 + yield* fs.writeFileString(observedCompanionPath, 'old-support-file');
541 + vi.spyOn(process, 'execPath', 'get').mockReturnValue(fakeExecPath);
542 +
543 + const error = yield* runUpgrade(
544 + [['DEBUG_OVERRIDE_UPGRADE_TARGET', sourceBinaryPath]],
545 + { tag: newReleaseTag },
546 + failRenameTo(fakeExecPath)
547 + );
548 +
549 + expect(error).toBeInstanceOf(UpgradeBinaryError);
550 + if (!(error instanceof UpgradeBinaryError)) {
551 + throw error;
552 + }
553 + expect(error.message).toBe('Failed to replace binary');
554 + expect(error.cause).toBeInstanceOf(PlatformError.SystemError);
555 + expect(yield* fs.readFileString(fakeExecPath)).toBe('old-binary');
556 + expect(yield* fs.readFileString(observedCompanionPath)).toBe('new-support-file');
557 + expect(yield* fs.readFileString(releaseTagPath)).toBe(`${newReleaseTag}\n`);
558 + const replacedReleaseTagInfo = yield* fs.stat(releaseTagPath);
559 + expect(Option.getOrThrow(replacedReleaseTagInfo.ino)).not.toBe(
560 + Option.getOrThrow(originalReleaseTagInfo.ino)
561 + );
562 + }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks));
563 + });
564 +
565 + it.scoped('preflights every companion before changing installed files', () => {
566 + vi.stubGlobal('Bun', { which: vi.fn(() => null) });
567 +
568 + return Effect.gen(function* () {
569 + const fs = yield* FileSystem.FileSystem;
570 + const path = yield* Path.Path;
571 + const installDir = yield* fs.makeTempDirectoryScoped({
572 + prefix: 'composio-incomplete-upgrade-target-',
573 + });
574 + const sourceDir = yield* fs.makeTempDirectoryScoped({
575 + prefix: 'composio-incomplete-upgrade-source-',
576 + });
577 + const fakeExecPath = path.join(installDir, 'composio');
578 + const sourceBinaryPath = path.join(sourceDir, 'composio');
579 + const releaseTagPath = path.join(installDir, RUN_COMPANION_RELEASE_TAG_FILENAME);
580 + yield* fs.writeFileString(fakeExecPath, 'old-binary');
581 + yield* fs.writeFileString(sourceBinaryPath, 'new-binary');
582 + yield* fs.writeFileString(releaseTagPath, '@composio/cli@0.2.31\n');
583 +
584 + const companionRelativePaths =
585 + yield* collectExpectedRunCompanionAssetRelativePaths(sourceDir);
586 + const missingRelativePath = companionRelativePaths.at(-1)!;
587 + for (const relativePath of companionRelativePaths) {
588 + if (relativePath === missingRelativePath) continue;
589 + const companionPath = path.join(sourceDir, relativePath);
590 + yield* fs.makeDirectory(path.dirname(companionPath), { recursive: true });
591 + yield* fs.writeFileString(companionPath, 'new-support-file');
592 + }
593 + const observedCompanionPath = path.join(installDir, companionRelativePaths[0]!);
594 + yield* fs.makeDirectory(path.dirname(observedCompanionPath), { recursive: true });
595 + yield* fs.writeFileString(observedCompanionPath, 'old-support-file');
596 +
597 + const sourceLocalToolPath = path.join(sourceDir, 'local-tools-binaries', 'test-tool');
598 + const installedLocalToolPath = path.join(installDir, 'local-tools-binaries', 'test-tool');
599 + yield* fs.makeDirectory(path.dirname(sourceLocalToolPath), { recursive: true });
600 + yield* fs.writeFileString(sourceLocalToolPath, 'new-local-tool');
601 + yield* fs.makeDirectory(path.dirname(installedLocalToolPath), { recursive: true });
602 + yield* fs.writeFileString(installedLocalToolPath, 'old-local-tool');
603 + vi.spyOn(process, 'execPath', 'get').mockReturnValue(fakeExecPath);
604 +
605 + const error = yield* runUpgrade([['DEBUG_OVERRIDE_UPGRADE_TARGET', sourceBinaryPath]], {
606 + tag: '@composio/cli@0.3.0',
607 + });
608 +
609 + expect(error).toBeInstanceOf(UpgradeBinaryError);
610 + if (!(error instanceof UpgradeBinaryError)) {
611 + throw error;
612 + }
613 + expect(error.message).toBe('Downloaded binary package is incomplete');
614 + expect(String(error.cause)).toContain(missingRelativePath);
615 + expect(yield* fs.readFileString(fakeExecPath)).toBe('old-binary');
616 + expect(yield* fs.readFileString(observedCompanionPath)).toBe('old-support-file');
617 + expect(yield* fs.readFileString(installedLocalToolPath)).toBe('old-local-tool');
618 + expect(yield* fs.readFileString(releaseTagPath)).toBe('@composio/cli@0.2.31\n');
619 }).pipe(Effect.provide(TestPlatform), Effect.ensuring(restoreStubsAndMocks));
620 });
621