Skip to content

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

jkomyno715c55cf1f58
diff --git a/ts/packages/cli/src/utils/atomic-replace.ts b/ts/packages/cli/src/utils/atomic-replace.ts
index 1cef136db..cdcc9ca10 100644
--- a/ts/packages/cli/src/utils/atomic-replace.ts
+++ b/ts/packages/cli/src/utils/atomic-replace.ts
@@ -54,8 +54,9 @@ export const atomicReplaceFile = ({
/**
* Replaces a directory with a staged copy using two renames. The old directory
- * is restored when publishing the replacement fails, but an abrupt process
- * exit between the renames can leave the target temporarily absent.
+ * is restored when publishing the replacement fails. An abrupt process exit
+ * between the renames can leave the target temporarily absent, with its prior
+ * contents retained in a same-directory recovery path.
*/
export const atomicReplaceDirectory = ({
sourcePath,
@@ -68,13 +69,13 @@ export const atomicReplaceDirectory = ({
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
+ const targetDirectory = path.dirname(targetPath);
const stagingDirectory = yield* fs.makeTempDirectoryScoped({
- directory: path.dirname(targetPath),
+ directory: targetDirectory,
prefix: '.composio-atomic-replace-',
});
const targetName = path.basename(targetPath);
const stagedPath = path.join(stagingDirectory, targetName);
- const asidePath = path.join(stagingDirectory, `${targetName}.aside`);
yield* fs.copy(sourcePath, stagedPath);
@@ -85,20 +86,50 @@ export const atomicReplaceDirectory = ({
yield* Effect.uninterruptible(
Effect.gen(function* () {
- yield* fs.rename(targetPath, asidePath);
+ const recoveryDirectory = yield* fs.makeTempDirectory({
+ directory: targetDirectory,
+ prefix: '.composio-atomic-recovery-',
+ });
+ const asidePath = path.join(recoveryDirectory, targetName);
yield* fs
- .rename(stagedPath, targetPath)
- .pipe(Effect.tapError(() => fs.rename(asidePath, targetPath).pipe(Effect.ignore)));
- yield* fs.remove(asidePath, { recursive: true });
+ .rename(targetPath, asidePath)
+ .pipe(
+ Effect.tapError(() =>
+ fs.remove(recoveryDirectory, { recursive: true }).pipe(Effect.ignore)
+ )
+ );
+ yield* fs.rename(stagedPath, targetPath).pipe(
+ Effect.matchEffect({
+ onSuccess: () => fs.remove(recoveryDirectory, { recursive: true }),
+ onFailure: publishCause =>
+ fs.rename(asidePath, targetPath).pipe(
+ Effect.matchEffect({
+ onSuccess: () =>
+ fs
+ .remove(recoveryDirectory, { recursive: true })
+ .pipe(Effect.zipRight(Effect.fail(publishCause))),
+ onFailure: restoreCause =>
+ Effect.fail(
+ new AtomicReplaceError({
+ targetPath,
+ message: `Failed to replace directory at ${targetPath}. Previous contents retained at ${asidePath}`,
+ cause: { publishCause, restoreCause },
+ })
+ ),
+ })
+ ),
+ })
+ );
})
);
}).pipe(
- Effect.mapError(
- cause =>
- new AtomicReplaceError({
- targetPath,
- message: `Failed to replace directory at ${targetPath}`,
- cause,
- })
+ Effect.mapError(cause =>
+ cause instanceof AtomicReplaceError
+ ? cause
+ : new AtomicReplaceError({
+ targetPath,
+ message: `Failed to replace directory at ${targetPath}`,
+ cause,
+ })
)
);
diff --git a/ts/packages/cli/test/src/utils/atomic-replace.test.ts b/ts/packages/cli/test/src/utils/atomic-replace.test.ts
index 3bd4ee09c..b3f20e45b 100644
--- a/ts/packages/cli/test/src/utils/atomic-replace.test.ts
+++ b/ts/packages/cli/test/src/utils/atomic-replace.test.ts
@@ -1,4 +1,5 @@
import { FileSystem, Path } from '@effect/platform';
+import * as PlatformError from '@effect/platform/Error';
import { BunFileSystem, BunPath } from '@effect/platform-bun';
import { describe, expect, layer } from '@effect/vitest';
import { Effect, Layer, Option } from 'effect';
@@ -10,6 +11,55 @@ import {
const TestPlatform = Layer.mergeAll(BunFileSystem.layer, BunPath.layer);
+const failDirectoryPublish = (targetPath: string, failRestore = false) =>
+ Layer.effect(
+ FileSystem.FileSystem,
+ Effect.map(
+ FileSystem.FileSystem,
+ fs =>
+ new Proxy(fs, {
+ get(target, property, receiver) {
+ if (property !== 'rename') {
+ return Reflect.get(target, property, receiver);
+ }
+
+ return (oldPath: string, newPath: string) => {
+ const shouldFail =
+ newPath === targetPath &&
+ (oldPath.includes('.composio-atomic-replace-') ||
+ (failRestore && oldPath.includes('.composio-atomic-recovery-')));
+ return shouldFail
+ ? Effect.fail(
+ new PlatformError.SystemError({
+ reason: 'Busy',
+ module: 'FileSystem',
+ method: 'rename',
+ pathOrDescriptor: newPath,
+ })
+ )
+ : target.rename(oldPath, newPath);
+ };
+ },
+ })
+ )
+ ).pipe(Layer.provide(BunFileSystem.layer));
+
+const makeDirectoryFixture = (withTarget = true) =>
+ Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const directory = yield* fs.makeTempDirectoryScoped();
+ const sourcePath = path.join(directory, 'source');
+ const targetPath = path.join(directory, 'target');
+ yield* fs.makeDirectory(sourcePath);
+ yield* fs.writeFileString(path.join(sourcePath, 'new.txt'), 'new contents');
+ if (withTarget) {
+ yield* fs.makeDirectory(targetPath);
+ yield* fs.writeFileString(path.join(targetPath, 'old.txt'), 'old contents');
+ }
+ return { directory, fs, path, sourcePath, targetPath };
+ });
+
describe('atomic replace', () => {
layer(TestPlatform)(it => {
it.scoped('replaces an existing file without modifying its open inode', () =>
@@ -95,5 +145,54 @@ describe('atomic replace', () => {
expect((yield* fs.readDirectory(directory)).sort()).toEqual(['source', 'target']);
})
);
+
+ it.scoped('creates a directory target that does not exist', () =>
+ Effect.gen(function* () {
+ const { directory, fs, path, sourcePath, targetPath } = yield* makeDirectoryFixture(false);
+
+ yield* Effect.scoped(atomicReplaceDirectory({ sourcePath, targetPath }));
+
+ expect(yield* fs.readFileString(path.join(targetPath, 'new.txt'))).toBe('new contents');
+ expect((yield* fs.readDirectory(directory)).sort()).toEqual(['source', 'target']);
+ })
+ );
+
+ it.scoped('restores the previous directory when publishing fails', () =>
+ Effect.gen(function* () {
+ const { directory, fs, path, sourcePath, targetPath } = yield* makeDirectoryFixture();
+
+ const error = yield* Effect.flip(
+ Effect.scoped(atomicReplaceDirectory({ sourcePath, targetPath })).pipe(
+ Effect.provide(failDirectoryPublish(targetPath))
+ )
+ );
+
+ expect(error).toBeInstanceOf(AtomicReplaceError);
+ expect(yield* fs.readFileString(path.join(targetPath, 'old.txt'))).toBe('old contents');
+ expect((yield* fs.readDirectory(directory)).sort()).toEqual(['source', 'target']);
+ })
+ );
+
+ it.scoped('retains the previous directory when publishing and restoration fail', () =>
+ Effect.gen(function* () {
+ const { directory, fs, path, sourcePath, targetPath } = yield* makeDirectoryFixture();
+
+ const error = yield* Effect.flip(
+ Effect.scoped(atomicReplaceDirectory({ sourcePath, targetPath })).pipe(
+ Effect.provide(failDirectoryPublish(targetPath, true))
+ )
+ );
+ const recoveryDirectory = (yield* fs.readDirectory(directory)).find(entry =>
+ entry.startsWith('.composio-atomic-recovery-')
+ );
+
+ expect(error).toBeInstanceOf(AtomicReplaceError);
+ expect(recoveryDirectory).toBeDefined();
+ expect(error.message).toContain('Previous contents retained at');
+ expect(
+ yield* fs.readFileString(path.join(directory, recoveryDirectory!, 'target', 'old.txt'))
+ ).toBe('old contents');
+ })
+ );
});
});