@devries-cameron20 / composio-1 / commits / e79846e00
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.

fix(cli): add atomic file replacement

jkomyno committed Aug 1, 2026 at 18:46 UTC e79846e00acc3455f25faa1861f33f8fd884995c
2 files changed +209
ts/packages/cli/src/utils/atomic-replace.ts new
+110
@@ -0,0 +1,110 @@
1 +import { FileSystem, Path } from '@effect/platform';
2 +import { Data, Effect, Scope } from 'effect';
3 +
4 +export class AtomicReplaceError extends Data.TaggedError('utils/AtomicReplaceError')<{
5 + readonly targetPath: string;
6 + readonly message: string;
7 + readonly cause: unknown;
8 +}> {}
9 +
10 +type AtomicReplaceFileOptions = {
11 + readonly sourcePath: string;
12 + readonly targetPath: string;
13 + readonly mode?: number;
14 +};
15 +
16 +type AtomicReplaceDirectoryOptions = {
17 + readonly sourcePath: string;
18 + readonly targetPath: string;
19 +};
20 +
21 +export const atomicReplaceFile = ({
22 + sourcePath,
23 + targetPath,
24 + mode,
25 +}: AtomicReplaceFileOptions): Effect.Effect<
26 + void,
27 + AtomicReplaceError,
28 + FileSystem.FileSystem | Path.Path | Scope.Scope
29 +> =>
30 + Effect.gen(function* () {
31 + const fs = yield* FileSystem.FileSystem;
32 + const path = yield* Path.Path;
33 + const stagingDirectory = yield* fs.makeTempDirectoryScoped({
34 + directory: path.dirname(targetPath),
35 + prefix: '.composio-atomic-replace-',
36 + });
37 + const stagedPath = path.join(stagingDirectory, path.basename(targetPath));
38 +
39 + yield* fs.copyFile(sourcePath, stagedPath);
40 + if (mode !== undefined) {
41 + yield* fs.chmod(stagedPath, mode);
42 + }
43 + yield* fs.rename(stagedPath, targetPath);
44 + }).pipe(
45 + Effect.mapError(
46 + cause =>
47 + new AtomicReplaceError({
48 + targetPath,
49 + message: `Failed to replace file at ${targetPath}`,
50 + cause,
51 + })
52 + )
53 + );
54 +
55 +/**
56 + * Replaces a directory with a staged copy using two renames. The old directory
57 + * is restored when publishing the replacement fails, but an abrupt process
58 + * exit between the renames can leave the target temporarily absent.
59 + */
60 +export const atomicReplaceDirectory = ({
61 + sourcePath,
62 + targetPath,
63 +}: AtomicReplaceDirectoryOptions): Effect.Effect<
64 + void,
65 + AtomicReplaceError,
66 + FileSystem.FileSystem | Path.Path | Scope.Scope
67 +> =>
68 + Effect.gen(function* () {
69 + const fs = yield* FileSystem.FileSystem;
70 + const path = yield* Path.Path;
71 + const stagingDirectory = yield* fs.makeTempDirectoryScoped({
72 + directory: path.dirname(targetPath),
73 + prefix: '.composio-atomic-replace-',
74 + });
75 + const targetName = path.basename(targetPath);
76 + const stagedPath = path.join(stagingDirectory, targetName);
77 + const asidePath = path.join(stagingDirectory, `${targetName}.aside`);
78 +
79 + yield* fs.copy(sourcePath, stagedPath);
80 +
81 + if (!(yield* fs.exists(targetPath))) {
82 + yield* fs.rename(stagedPath, targetPath);
83 + return;
84 + }
85 +
86 + yield* Effect.uninterruptible(
87 + Effect.gen(function* () {
88 + yield* fs.rename(targetPath, asidePath);
89 + yield* fs
90 + .rename(stagedPath, targetPath)
91 + .pipe(
92 + Effect.catchAll(cause =>
93 + fs
94 + .rename(asidePath, targetPath)
95 + .pipe(Effect.ignore, Effect.zipRight(Effect.fail(cause)))
96 + )
97 + );
98 + yield* fs.remove(asidePath, { recursive: true });
99 + })
100 + );
101 + }).pipe(
102 + Effect.mapError(
103 + cause =>
104 + new AtomicReplaceError({
105 + targetPath,
106 + message: `Failed to replace directory at ${targetPath}`,
107 + cause,
108 + })
109 + )
110 + );
ts/packages/cli/test/src/utils/atomic-replace.test.ts new
+99
@@ -0,0 +1,99 @@
1 +import { FileSystem, Path } from '@effect/platform';
2 +import { BunFileSystem, BunPath } from '@effect/platform-bun';
3 +import { describe, expect, layer } from '@effect/vitest';
4 +import { Effect, Layer, Option } from 'effect';
5 +import {
6 + AtomicReplaceError,
7 + atomicReplaceDirectory,
8 + atomicReplaceFile,
9 +} from 'src/utils/atomic-replace';
10 +
11 +const TestPlatform = Layer.mergeAll(BunFileSystem.layer, BunPath.layer);
12 +
13 +describe('atomic replace', () => {
14 + layer(TestPlatform)(it => {
15 + it.scoped('replaces an existing file without modifying its open inode', () =>
16 + Effect.gen(function* () {
17 + const fs = yield* FileSystem.FileSystem;
18 + const path = yield* Path.Path;
19 + const directory = yield* fs.makeTempDirectoryScoped();
20 + const sourcePath = path.join(directory, 'source');
21 + const targetPath = path.join(directory, 'target');
22 + yield* fs.writeFileString(sourcePath, 'new contents');
23 + yield* fs.writeFileString(targetPath, 'old contents');
24 +
25 + const before = yield* fs.stat(targetPath);
26 + const oldTarget = yield* fs.open(targetPath);
27 + yield* Effect.scoped(atomicReplaceFile({ sourcePath, targetPath }));
28 + const after = yield* fs.stat(targetPath);
29 + const openContents = yield* oldTarget.readAlloc(FileSystem.Size('old contents'.length));
30 +
31 + expect(yield* fs.readFileString(targetPath)).toBe('new contents');
32 + expect(after.dev).toBe(before.dev);
33 + expect(Option.getOrThrow(after.ino)).not.toBe(Option.getOrThrow(before.ino));
34 + expect(new TextDecoder().decode(Option.getOrThrow(openContents))).toBe('old contents');
35 + expect((yield* fs.readDirectory(directory)).sort()).toEqual(['source', 'target']);
36 + })
37 + );
38 +
39 + it.scoped('creates an executable target when mode is provided', () =>
40 + Effect.gen(function* () {
41 + const fs = yield* FileSystem.FileSystem;
42 + const path = yield* Path.Path;
43 + const directory = yield* fs.makeTempDirectoryScoped();
44 + const sourcePath = path.join(directory, 'source');
45 + const targetPath = path.join(directory, 'target');
46 + yield* fs.writeFileString(sourcePath, 'binary');
47 +
48 + yield* Effect.scoped(atomicReplaceFile({ sourcePath, targetPath, mode: 0o755 }));
49 +
50 + expect(yield* fs.readFileString(targetPath)).toBe('binary');
51 + expect((yield* fs.stat(targetPath)).mode & 0o777).toBe(0o755);
52 + expect((yield* fs.readDirectory(directory)).sort()).toEqual(['source', 'target']);
53 + })
54 + );
55 +
56 + it.scoped('preserves the target and cleans staging when the source is missing', () =>
57 + Effect.gen(function* () {
58 + const fs = yield* FileSystem.FileSystem;
59 + const path = yield* Path.Path;
60 + const directory = yield* fs.makeTempDirectoryScoped();
61 + const sourcePath = path.join(directory, 'missing');
62 + const targetPath = path.join(directory, 'target');
63 + yield* fs.writeFileString(targetPath, 'old contents');
64 +
65 + const error = yield* Effect.flip(
66 + Effect.scoped(atomicReplaceFile({ sourcePath, targetPath }))
67 + );
68 +
69 + expect(error).toBeInstanceOf(AtomicReplaceError);
70 + expect(error.targetPath).toBe(targetPath);
71 + expect(error.cause).toBeDefined();
72 + expect(yield* fs.readFileString(targetPath)).toBe('old contents');
73 + expect(yield* fs.readDirectory(directory)).toEqual(['target']);
74 + })
75 + );
76 +
77 + it.scoped('replaces an existing non-empty directory without leaving residue', () =>
78 + Effect.gen(function* () {
79 + const fs = yield* FileSystem.FileSystem;
80 + const path = yield* Path.Path;
81 + const directory = yield* fs.makeTempDirectoryScoped();
82 + const sourcePath = path.join(directory, 'source');
83 + const targetPath = path.join(directory, 'target');
84 + yield* fs.makeDirectory(path.join(sourcePath, 'nested'), { recursive: true });
85 + yield* fs.writeFileString(path.join(sourcePath, 'nested', 'new.txt'), 'new contents');
86 + yield* fs.makeDirectory(targetPath);
87 + yield* fs.writeFileString(path.join(targetPath, 'old.txt'), 'old contents');
88 +
89 + yield* Effect.scoped(atomicReplaceDirectory({ sourcePath, targetPath }));
90 +
91 + expect(yield* fs.readFileString(path.join(targetPath, 'nested', 'new.txt'))).toBe(
92 + 'new contents'
93 + );
94 + expect(yield* fs.exists(path.join(targetPath, 'old.txt'))).toBe(false);
95 + expect((yield* fs.readDirectory(directory)).sort()).toEqual(['source', 'target']);
96 + })
97 + );
98 + });
99 +});