@devries-cameron20 / composio-1 / commits / 715c55cf1
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(review): preserve directory recovery copy

jkomyno committed Aug 1, 2026 at 19:47 UTC 715c55cf1f5877489ca600686fbcde18edd24ad9
2 files changed +145 -15
ts/packages/cli/src/utils/atomic-replace.ts
+46 -15
@@ -54,8 +54,9 @@ export const atomicReplaceFile = ({
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.
57 + * is restored when publishing the replacement fails. An abrupt process exit
58 + * between the renames can leave the target temporarily absent, with its prior
59 + * contents retained in a same-directory recovery path.
60 */
61 export const atomicReplaceDirectory = ({
62 sourcePath,
@@ -68,13 +69,13 @@ export const atomicReplaceDirectory = ({
69 Effect.gen(function* () {
70 const fs = yield* FileSystem.FileSystem;
71 const path = yield* Path.Path;
72 + const targetDirectory = path.dirname(targetPath);
73 const stagingDirectory = yield* fs.makeTempDirectoryScoped({
72 - directory: path.dirname(targetPath),
74 + directory: targetDirectory,
75 prefix: '.composio-atomic-replace-',
76 });
77 const targetName = path.basename(targetPath);
78 const stagedPath = path.join(stagingDirectory, targetName);
77 - const asidePath = path.join(stagingDirectory, `${targetName}.aside`);
79
80 yield* fs.copy(sourcePath, stagedPath);
81
@@ -85,20 +86,50 @@ export const atomicReplaceDirectory = ({
86
87 yield* Effect.uninterruptible(
88 Effect.gen(function* () {
88 - yield* fs.rename(targetPath, asidePath);
89 + const recoveryDirectory = yield* fs.makeTempDirectory({
90 + directory: targetDirectory,
91 + prefix: '.composio-atomic-recovery-',
92 + });
93 + const asidePath = path.join(recoveryDirectory, targetName);
94 yield* fs
90 - .rename(stagedPath, targetPath)
91 - .pipe(Effect.tapError(() => fs.rename(asidePath, targetPath).pipe(Effect.ignore)));
92 - yield* fs.remove(asidePath, { recursive: true });
95 + .rename(targetPath, asidePath)
96 + .pipe(
97 + Effect.tapError(() =>
98 + fs.remove(recoveryDirectory, { recursive: true }).pipe(Effect.ignore)
99 + )
100 + );
101 + yield* fs.rename(stagedPath, targetPath).pipe(
102 + Effect.matchEffect({
103 + onSuccess: () => fs.remove(recoveryDirectory, { recursive: true }),
104 + onFailure: publishCause =>
105 + fs.rename(asidePath, targetPath).pipe(
106 + Effect.matchEffect({
107 + onSuccess: () =>
108 + fs
109 + .remove(recoveryDirectory, { recursive: true })
110 + .pipe(Effect.zipRight(Effect.fail(publishCause))),
111 + onFailure: restoreCause =>
112 + Effect.fail(
113 + new AtomicReplaceError({
114 + targetPath,
115 + message: `Failed to replace directory at ${targetPath}. Previous contents retained at ${asidePath}`,
116 + cause: { publishCause, restoreCause },
117 + })
118 + ),
119 + })
120 + ),
121 + })
122 + );
123 })
124 );
125 }).pipe(
96 - Effect.mapError(
97 - cause =>
98 - new AtomicReplaceError({
99 - targetPath,
100 - message: `Failed to replace directory at ${targetPath}`,
101 - cause,
102 - })
126 + Effect.mapError(cause =>
127 + cause instanceof AtomicReplaceError
128 + ? cause
129 + : new AtomicReplaceError({
130 + targetPath,
131 + message: `Failed to replace directory at ${targetPath}`,
132 + cause,
133 + })
134 )
135 );
ts/packages/cli/test/src/utils/atomic-replace.test.ts
+99
@@ -1,4 +1,5 @@
1 import { FileSystem, Path } from '@effect/platform';
2 +import * as PlatformError from '@effect/platform/Error';
3 import { BunFileSystem, BunPath } from '@effect/platform-bun';
4 import { describe, expect, layer } from '@effect/vitest';
5 import { Effect, Layer, Option } from 'effect';
@@ -10,6 +11,55 @@ import {
11
12 const TestPlatform = Layer.mergeAll(BunFileSystem.layer, BunPath.layer);
13
14 +const failDirectoryPublish = (targetPath: string, failRestore = false) =>
15 + Layer.effect(
16 + FileSystem.FileSystem,
17 + Effect.map(
18 + FileSystem.FileSystem,
19 + fs =>
20 + new Proxy(fs, {
21 + get(target, property, receiver) {
22 + if (property !== 'rename') {
23 + return Reflect.get(target, property, receiver);
24 + }
25 +
26 + return (oldPath: string, newPath: string) => {
27 + const shouldFail =
28 + newPath === targetPath &&
29 + (oldPath.includes('.composio-atomic-replace-') ||
30 + (failRestore && oldPath.includes('.composio-atomic-recovery-')));
31 + return shouldFail
32 + ? Effect.fail(
33 + new PlatformError.SystemError({
34 + reason: 'Busy',
35 + module: 'FileSystem',
36 + method: 'rename',
37 + pathOrDescriptor: newPath,
38 + })
39 + )
40 + : target.rename(oldPath, newPath);
41 + };
42 + },
43 + })
44 + )
45 + ).pipe(Layer.provide(BunFileSystem.layer));
46 +
47 +const makeDirectoryFixture = (withTarget = true) =>
48 + Effect.gen(function* () {
49 + const fs = yield* FileSystem.FileSystem;
50 + const path = yield* Path.Path;
51 + const directory = yield* fs.makeTempDirectoryScoped();
52 + const sourcePath = path.join(directory, 'source');
53 + const targetPath = path.join(directory, 'target');
54 + yield* fs.makeDirectory(sourcePath);
55 + yield* fs.writeFileString(path.join(sourcePath, 'new.txt'), 'new contents');
56 + if (withTarget) {
57 + yield* fs.makeDirectory(targetPath);
58 + yield* fs.writeFileString(path.join(targetPath, 'old.txt'), 'old contents');
59 + }
60 + return { directory, fs, path, sourcePath, targetPath };
61 + });
62 +
63 describe('atomic replace', () => {
64 layer(TestPlatform)(it => {
65 it.scoped('replaces an existing file without modifying its open inode', () =>
@@ -95,5 +145,54 @@ describe('atomic replace', () => {
145 expect((yield* fs.readDirectory(directory)).sort()).toEqual(['source', 'target']);
146 })
147 );
148 +
149 + it.scoped('creates a directory target that does not exist', () =>
150 + Effect.gen(function* () {
151 + const { directory, fs, path, sourcePath, targetPath } = yield* makeDirectoryFixture(false);
152 +
153 + yield* Effect.scoped(atomicReplaceDirectory({ sourcePath, targetPath }));
154 +
155 + expect(yield* fs.readFileString(path.join(targetPath, 'new.txt'))).toBe('new contents');
156 + expect((yield* fs.readDirectory(directory)).sort()).toEqual(['source', 'target']);
157 + })
158 + );
159 +
160 + it.scoped('restores the previous directory when publishing fails', () =>
161 + Effect.gen(function* () {
162 + const { directory, fs, path, sourcePath, targetPath } = yield* makeDirectoryFixture();
163 +
164 + const error = yield* Effect.flip(
165 + Effect.scoped(atomicReplaceDirectory({ sourcePath, targetPath })).pipe(
166 + Effect.provide(failDirectoryPublish(targetPath))
167 + )
168 + );
169 +
170 + expect(error).toBeInstanceOf(AtomicReplaceError);
171 + expect(yield* fs.readFileString(path.join(targetPath, 'old.txt'))).toBe('old contents');
172 + expect((yield* fs.readDirectory(directory)).sort()).toEqual(['source', 'target']);
173 + })
174 + );
175 +
176 + it.scoped('retains the previous directory when publishing and restoration fail', () =>
177 + Effect.gen(function* () {
178 + const { directory, fs, path, sourcePath, targetPath } = yield* makeDirectoryFixture();
179 +
180 + const error = yield* Effect.flip(
181 + Effect.scoped(atomicReplaceDirectory({ sourcePath, targetPath })).pipe(
182 + Effect.provide(failDirectoryPublish(targetPath, true))
183 + )
184 + );
185 + const recoveryDirectory = (yield* fs.readDirectory(directory)).find(entry =>
186 + entry.startsWith('.composio-atomic-recovery-')
187 + );
188 +
189 + expect(error).toBeInstanceOf(AtomicReplaceError);
190 + expect(recoveryDirectory).toBeDefined();
191 + expect(error.message).toContain('Previous contents retained at');
192 + expect(
193 + yield* fs.readFileString(path.join(directory, recoveryDirectory!, 'target', 'old.txt'))
194 + ).toBe('old contents');
195 + })
196 + );
197 });
198 });