@devries-cameron20 / composio Mirror · read-only / commits / 8b7ebadef
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

refactor(cli): share one atomic tmp-write helper with per-process tmp names

Three from-scratch tmp-write→rename implementations coexisted (install.cmd.ts, tool-permissions writeCacheFile, analytics writeAnalyticsStateFile), and the install ones raced concurrent installs through a fixed .composio-tmp suffix. Extract src/utils/atomic-write.ts: the tmp name embeds the pid, an optional preserveMode creates the tmp with the target's mode and pins it via chmod, and the best-effort tmp cleanup now also covers a failure of the initial write itself (ENOSPC mid-write no longer leaves tmp litter). All three call sites migrate with their semantics intact; tests scan for any .composio-tmp remnant instead of probing the old fixed name. Extracting the block-append loop also brings installShellIntegration back under the function-length lint that the merge had pushed over.

jkomyno committed Aug 3, 2026 at 21:00 UTC 8b7ebadef90af2db130bc383b7745da9b3ca6bd4
5 files changed +133 -100
ts/packages/cli/src/analytics/dispatch.ts
+2 -5
@@ -14,6 +14,7 @@ import {
14 } from 'effect';
15 import * as constants from 'src/constants';
16 import { getDetachedWorkerSpawnArgs, spawnDetached } from 'src/services/detached-process';
17 +import { atomicWriteFileString } from 'src/utils/atomic-write';
18 import { sha256Hex } from 'src/utils/checksums';
19 import { djb2Hash } from 'src/utils/djb2';
20 import { NodeOs } from 'src/services/node-os';
@@ -291,11 +292,7 @@ const writeAnalyticsStateFile = (contents: string) =>
292 Effect.gen(function* () {
293 const fs = yield* FileSystem.FileSystem;
294 const paths = yield* getAnalyticsPaths;
294 - const temporaryPath = `${paths.analyticsStatePath}.${crypto.randomUUID().slice(0, 8)}.tmp`;
295 - yield* fs.writeFileString(temporaryPath, contents);
296 - yield* fs
297 - .rename(temporaryPath, paths.analyticsStatePath)
298 - .pipe(Effect.tapError(() => fs.remove(temporaryPath).pipe(Effect.ignore)));
295 + yield* atomicWriteFileString({ fs, target: paths.analyticsStatePath, contents });
296 });
297
298 const getOrCreateInstallIdUnlocked = Effect.gen(function* () {
ts/packages/cli/src/commands/install.cmd.ts
+50 -74
@@ -7,6 +7,7 @@ import { NodeOs } from 'src/services/node-os';
7 import { NodeProcess } from 'src/services/node-process';
8 import { TerminalUI } from 'src/services/terminal-ui';
9 import { getCompletionScript } from 'src/effects/shell-completions';
10 +import { atomicWriteFileString } from 'src/utils/atomic-write';
11
12 // ---------------------------------------------------------------------------
13 // Options
@@ -384,27 +385,50 @@ const planPathBlockWrites = (params: {
385 return { appendPathFiles, replacementsByTarget, loginOverrideWritten };
386 };
387
387 -/** Atomically replace a physical target file's contents, preserving its file mode. */
388 -const atomicWriteWithMode = (
388 +/**
389 + * Replace an rc file's contents via the shared atomic-write helper, preserving
390 + * the existing file's mode so a private rc stays private across the rewrite.
391 + */
392 +const replaceFilePreservingMode = (
393 fs: FileSystem.FileSystem,
390 - writeTarget: string,
394 + target: string,
395 contents: string
396 ): Effect.Effect<void, PlatformError> =>
397 + atomicWriteFileString({ fs, target, contents, preserveMode: true });
398 +
399 +/** Append each file's queued blocks in one atomic, mode-preserving rewrite per file. */
400 +const appendQueuedBlocks = (params: {
401 + readonly blocksByFile: ReadonlyMap<string, ReadonlyArray<string>>;
402 + readonly pathFileTargets: ReadonlyMap<string, string>;
403 + readonly fs: FileSystem.FileSystem;
404 + readonly path: Path.Path;
405 + readonly homedir: string;
406 + readonly report: (message: string) => Effect.Effect<void>;
407 +}): Effect.Effect<void, PlatformError> =>
408 Effect.gen(function* () {
394 - const existingTargetInfo = yield* fs.stat(writeTarget).pipe(Effect.option);
395 - const tmpPath = `${writeTarget}.composio-tmp`;
396 -
397 - yield* fs.writeFileString(tmpPath, contents);
398 - // Mirror install.sh's write_path_block: when the chmod or rename step
399 - // fails, remove the temp file before propagating the error so no
400 - // `.composio-tmp` litter is left next to the target.
401 - const promoteTmp = Effect.gen(function* () {
402 - if (Option.isSome(existingTargetInfo)) {
403 - yield* fs.chmod(tmpPath, existingTargetInfo.value.mode & 0o7777);
404 - }
405 - yield* fs.rename(tmpPath, writeTarget);
406 - });
407 - yield* promoteTmp.pipe(Effect.onError(() => fs.remove(tmpPath).pipe(Effect.ignore)));
409 + const { fs, path } = params;
410 + for (const [filePath, blocks] of params.blocksByFile.entries()) {
411 + // Re-read instead of reusing the pre-check snapshot: two configured paths
412 + // can alias the same physical file through symlinks, and this write must
413 + // not discard a previous iteration's append.
414 + const existingContents = yield* readMaybeMissingFile(filePath, fs);
415 + // PATH files were already resolved by the caller; only the completion
416 + // file needs a fresh resolve.
417 + const writeTarget =
418 + params.pathFileTargets.get(filePath) ?? (yield* resolveWriteTarget(filePath, fs));
419 +
420 + yield* fs
421 + .makeDirectory(path.dirname(writeTarget), { recursive: true })
422 + .pipe(
423 + Effect.catchAll(e =>
424 + Effect.logDebug('Could not create parent directory (may already exist):', e)
425 + )
426 + );
427 +
428 + const appendContent = '\n' + blocks.join('\n\n') + '\n';
429 + yield* replaceFilePreservingMode(fs, writeTarget, existingContents + appendContent);
430 + yield* params.report(`Updated ${tildify(filePath, params.homedir)}`);
431 + }
432 });
433
434 /** Atomically rewrite each reconciled physical target, preserving its file mode. */
@@ -416,7 +440,7 @@ const writeReconciledTargets = (params: {
440 }): Effect.Effect<void, PlatformError> =>
441 Effect.gen(function* () {
442 for (const [writeTarget, replacement] of params.replacementsByTarget.entries()) {
419 - yield* atomicWriteWithMode(params.fs, writeTarget, replacement.contents);
443 + yield* replaceFilePreservingMode(params.fs, writeTarget, replacement.contents);
444 yield* params.report(`Updated ${tildify(replacement.displayPath, params.homedir)}`);
445 }
446 });
@@ -527,38 +551,6 @@ const resolveWriteTarget = (
551 Effect.catchAll(() => Effect.succeed(filePath))
552 );
553
530 -/**
531 - * Atomically replace `writeTarget` via a same-directory tmp file. The tmp copy
532 - * is created with the target's mode from the start so a private rc's contents
533 - * never sit in a default-mode, world-readable tmp file. open(2) masks the
534 - * requested mode with the process umask (only ever clearing bits), so a chmod
535 - * still follows to pin the exact mode.
536 - */
537 -const replaceFilePreservingMode = (
538 - writeTarget: string,
539 - contents: string,
540 - fs: FileSystem.FileSystem
541 -): Effect.Effect<void, PlatformError> =>
542 - Effect.gen(function* () {
543 - const existingTargetInfo = yield* fs.stat(writeTarget).pipe(Effect.option);
544 - const preservedMode = Option.map(existingTargetInfo, info => info.mode & 0o7777);
545 - const tmpPath = `${writeTarget}.composio-tmp`;
546 - yield* Option.match(preservedMode, {
547 - onNone: () => fs.writeFileString(tmpPath, contents),
548 - onSome: mode => fs.writeFileString(tmpPath, contents, { mode }),
549 - });
550 - // Mirror install.sh's write_path_block: when the chmod or rename step
551 - // fails, remove the temp file before propagating the error so no
552 - // `.composio-tmp` litter is left next to the target.
553 - const promoteTmp = Effect.gen(function* () {
554 - if (Option.isSome(preservedMode)) {
555 - yield* fs.chmod(tmpPath, preservedMode.value);
556 - }
557 - yield* fs.rename(tmpPath, writeTarget);
558 - });
559 - yield* promoteTmp.pipe(Effect.onError(() => fs.remove(tmpPath).pipe(Effect.ignore)));
560 - });
561 -
554 // ---------------------------------------------------------------------------
555 // Exported logic (reusable from install.sh post-install delegation)
556 // ---------------------------------------------------------------------------
@@ -771,30 +763,14 @@ export const installShellIntegration = (params: {
763 report: success,
764 });
765
774 - if (blocksByFile.size > 0) {
775 - for (const [filePath, blocks] of blocksByFile.entries()) {
776 - // Re-read instead of reusing the pre-check snapshot: two configured paths
777 - // can alias the same physical file through symlinks, and this write must
778 - // not discard a previous iteration's append.
779 - const existingContents = yield* readMaybeMissingFile(filePath, fs);
780 - // PATH files were already resolved above; only the completion file needs a fresh resolve.
781 - const writeTarget =
782 - pathFileTargets.get(filePath) ?? (yield* resolveWriteTarget(filePath, fs));
783 -
784 - yield* fs
785 - .makeDirectory(path.dirname(writeTarget), { recursive: true })
786 - .pipe(
787 - Effect.catchAll(e =>
788 - Effect.logDebug('Could not create parent directory (may already exist):', e)
789 - )
790 - );
791 -
792 - const appendContent = '\n' + blocks.join('\n\n') + '\n';
793 - yield* replaceFilePreservingMode(writeTarget, existingContents + appendContent, fs);
794 -
795 - yield* success(`Updated ${tildify(filePath, os.homedir)}`);
796 - }
797 - }
766 + yield* appendQueuedBlocks({
767 + blocksByFile,
768 + pathFileTargets,
769 + fs,
770 + path,
771 + homedir: os.homedir,
772 + report: success,
773 + });
774
775 const changesWritten = blocksByFile.size > 0 || replacementsByTarget.size > 0;
776 if (!changesWritten) {
ts/packages/cli/src/services/tool-permissions.ts
+13 -17
@@ -5,6 +5,7 @@ import { detectCliPlatform } from '@composio/cli-local-tools';
5 import { Data, Effect, Option, Record as EffectRecord, Schema } from 'effect';
6 import { JsonRecordSchema } from 'src/effects/json';
7 import { resolveCliConfigDirectorySync } from 'src/services/cli-user-config';
8 +import { atomicWriteFileString } from 'src/utils/atomic-write';
9 import { collectDecodedEntries } from 'src/utils/collect-decoded-entries';
10 import {
11 detectNativeUiCallerAgent,
@@ -205,25 +206,20 @@ const writeCacheFile = (fs: FileSystem.FileSystem, path: Path.Path, cache: Cache
206 const targetPath = cachePath(path);
207 yield* fs.makeDirectory(path.dirname(targetPath), { recursive: true });
208
208 - const tempPath = `${targetPath}.${process.pid}.${Date.now()}.${crypto.randomUUID()}.tmp`;
209 // Atomic write: on failure the temp file is dropped (best effort) and the
210 // original error is surfaced.
211 - yield* fs
212 - .writeFileString(
213 - tempPath,
214 - `${JSON.stringify(
215 - {
216 - entries: cache.entries,
217 - allowEntries: pruneAllowEntries(cache.allowEntries),
218 - } satisfies CacheFile,
219 - null,
220 - 2
221 - )}\n`
222 - )
223 - .pipe(
224 - Effect.andThen(fs.rename(tempPath, targetPath)),
225 - Effect.onError(() => fs.remove(tempPath, { force: true }).pipe(Effect.ignore))
226 - );
211 + yield* atomicWriteFileString({
212 + fs,
213 + target: targetPath,
214 + contents: `${JSON.stringify(
215 + {
216 + entries: cache.entries,
217 + allowEntries: pruneAllowEntries(cache.allowEntries),
218 + } satisfies CacheFile,
219 + null,
220 + 2
221 + )}\n`,
222 + });
223 });
224
225 // Serializes the read-modify-write cycles on the cache file so concurrent
ts/packages/cli/src/utils/atomic-write.ts new
+56
@@ -0,0 +1,56 @@
1 +/**
2 + * Atomic file replacement shared by every tmp-write→rename site in the CLI
3 + * (shell rc rewrites, the tool-permissions cache, analytics state).
4 + *
5 + * The tmp file lives next to the target so the final rename(2) stays on one
6 + * filesystem and is atomic: readers observe either the old or the new
7 + * contents, never a partial write. The tmp name embeds the pid so concurrent
8 + * CLI processes writing the same target never share (and clobber) one tmp
9 + * file. On any failure — including a failure of the initial write itself,
10 + * e.g. ENOSPC mid-write — the tmp file is removed best-effort before the
11 + * original error propagates, so no tmp litter is left next to the target.
12 + */
13 +
14 +import type { FileSystem } from '@effect/platform';
15 +import type { PlatformError } from '@effect/platform/Error';
16 +import { Effect, Option } from 'effect';
17 +
18 +export const atomicTmpPath = (target: string): string => `${target}.composio-tmp.${process.pid}`;
19 +
20 +export const atomicWriteFileString = (params: {
21 + readonly fs: FileSystem.FileSystem;
22 + readonly target: string;
23 + readonly contents: string;
24 + /**
25 + * Preserve an existing target's file mode across the replacement: the tmp
26 + * copy is created with the target's mode from the start so private contents
27 + * never sit in a default-mode, world-readable tmp file. open(2) masks the
28 + * requested mode with the process umask (only ever clearing bits), so a
29 + * chmod still follows to pin the exact mode. When the target does not
30 + * exist (or `preserveMode` is false) the tmp is created with default mode.
31 + */
32 + readonly preserveMode?: boolean;
33 +}): Effect.Effect<void, PlatformError> =>
34 + Effect.gen(function* () {
35 + const { fs, target, contents } = params;
36 + const preservedMode = params.preserveMode
37 + ? Option.map(yield* fs.stat(target).pipe(Effect.option), info => info.mode & 0o7777)
38 + : Option.none<number>();
39 + const tmpPath = atomicTmpPath(target);
40 +
41 + const writeAndPromote = Effect.gen(function* () {
42 + yield* Option.match(preservedMode, {
43 + onNone: () => fs.writeFileString(tmpPath, contents),
44 + onSome: mode => fs.writeFileString(tmpPath, contents, { mode }),
45 + });
46 + yield* Option.match(preservedMode, {
47 + onNone: () => Effect.void,
48 + onSome: mode => fs.chmod(tmpPath, mode),
49 + });
50 + yield* fs.rename(tmpPath, target);
51 + });
52 +
53 + yield* writeAndPromote.pipe(
54 + Effect.onError(() => fs.remove(tmpPath, { force: true }).pipe(Effect.ignore))
55 + );
56 + });
ts/packages/cli/test/src/commands/install.cmd.test.ts
+12 -4
@@ -52,6 +52,14 @@ const install = (
52 shell: params.shell,
53 });
54
55 +// The atomic-write tmp name embeds the pid, so litter checks scan the target's
56 +// directory for any `.composio-tmp` remnant instead of probing one fixed name.
57 +const expectNoTmpLitter = (fs: FileSystem.FileSystem, targetPath: string) =>
58 + Effect.gen(function* () {
59 + const entries = yield* fs.readDirectory(path.dirname(targetPath));
60 + expect(entries.filter(name => name.includes('.composio-tmp'))).toEqual([]);
61 + });
62 +
63 // NOTE: `@effect/vitest`'s `layer(...)` builds one shared TestLive instance
64 // (home dir, MockConsole buffer, etc.) for every `it.scoped` nested inside a
65 // single call. Sharing is only safe when a later test doesn't read state a
@@ -121,7 +129,7 @@ describe('CLI: composio install', () => {
129
130 expect(yield* fs.readLink(rcPath)).toBe(linkTarget);
131 expect(yield* fs.readFileString(managedPath)).toContain('# Composio CLI');
124 - expect(yield* fs.exists(`${rcPath}.composio-tmp`)).toBe(false);
132 + yield* expectNoTmpLitter(fs, rcPath);
133 })
134 );
135 });
@@ -253,7 +261,7 @@ describe('CLI: composio install', () => {
261
262 const info = yield* fs.stat(bashProfilePath);
263 expect(info.mode & 0o777).toBe(0o600);
256 - expect(yield* fs.exists(`${bashProfilePath}.composio-tmp`)).toBe(false);
264 + yield* expectNoTmpLitter(fs, bashProfilePath);
265 })
266 );
267 });
@@ -734,7 +742,7 @@ describe('CLI: composio install', () => {
742 // Both aliases must survive as symlinks to the same physical file.
743 expect(yield* fs.readLink(bashrcPath)).toBe(managedPath);
744 expect(yield* fs.readLink(bashProfilePath)).toBe(managedPath);
737 - expect(yield* fs.exists(`${managedPath}.composio-tmp`)).toBe(false);
745 + yield* expectNoTmpLitter(fs, managedPath);
746 })
747 );
748 });
@@ -757,7 +765,7 @@ describe('CLI: composio install', () => {
765 const exit = yield* install().pipe(Effect.exit);
766
767 expect(Exit.isFailure(exit)).toBe(true);
760 - expect(yield* fs.exists(`${rcPath}.composio-tmp`)).toBe(false);
768 + yield* expectNoTmpLitter(fs, rcPath);
769 })
770 );
771 });