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): own memoized runs with a detached fiber
`Effect.cached` makes the first caller the owner of the shared run, so interrupting that caller stores the interrupt in the cell and replays it to every fiber already waiting on it. On `composio execute --parallel` two specs sharing a slug could take the second one down when the first one's `Effect.all` failed on the tool-detail fetch. Run the memoized effect on a detached fiber that completes a Deferred, so a caller's interruption cancels only its own wait. Claude-Session: https://claude.ai/code/session_01MTb47vexN35pLGsSZwBmrJ
jkomyno committed
Sep 14, 2026 at 20:54 UTC
caead95fdd5eecc8252bde248f165410c75b6f70
2 files changed
+93
-23
ts/packages/cli/src/utils/memoize-in-process.ts
+38
-22
@@ -1,9 +1,16 @@
1
-import { Effect } from 'effect';
1
+import { Deferred, Effect, Exit } from 'effect';
2
3
/**
4
* Memoizes an Effect per key for the lifetime of the process, sharing one run
5
- * between concurrent callers. A success stays cached; a failure, defect, or
6
- * interruption is dropped so the next caller retries instead of replaying it.
5
+ * between concurrent callers. A success stays cached; a failure or defect is
6
+ * delivered to the callers already waiting and then dropped so the next caller
7
+ * retries instead of replaying it.
8
+ *
9
+ * The run is owned by a detached fiber rather than by whichever caller arrived
10
+ * first. Interrupting a caller therefore cancels only that caller's wait: the
11
+ * run completes and every other waiter still receives its result. With
12
+ * `Effect.cached` the first caller owns the run, so interrupting it would store
13
+ * the interrupt and replay it to every fiber already parked on the cell.
14
*
15
* Meant for reads that cannot change within one CLI invocation (a tool's
16
* latest version, the user's connected accounts) but were being fetched more
@@ -13,25 +20,34 @@ export const memoizeInProcess = <I, A, E, R>(options: {
20
readonly keyOf: (input: I) => string;
21
readonly make: (input: I) => Effect.Effect<A, E, R>;
22
}): ((input: I) => Effect.Effect<A, E, R>) => {
16
- const cache = new Map<string, Effect.Effect<A, E, R>>();
23
+ const cache = new Map<string, Deferred.Deferred<A, E>>();
24
+
25
+ const load = (input: I): Effect.Effect<A, E, R> =>
26
+ // The lookup, the insert, and the fork run without an interruption point
27
+ // between them, so a cell is never left in the map without an owner.
28
+ Effect.uninterruptible(
29
+ Effect.suspend(() => {
30
+ const key = options.keyOf(input);
31
+ const existing = cache.get(key);
32
+ if (existing) {
33
+ return Effect.succeed(existing);
34
+ }
35
18
- return input =>
19
- Effect.suspend(() => {
20
- const key = options.keyOf(input);
21
- const existing = cache.get(key);
22
- if (existing) {
23
- return existing;
24
- }
36
+ const cell = Deferred.makeUnsafe<A, E>();
37
+ cache.set(key, cell);
38
+ return options.make(input).pipe(
39
+ Effect.exit,
40
+ Effect.flatMap(exit => {
41
+ if (!Exit.isSuccess(exit) && cache.get(key) === cell) {
42
+ cache.delete(key);
43
+ }
44
+ return Deferred.done(cell, exit);
45
+ }),
46
+ Effect.forkDetach,
47
+ Effect.as(cell)
48
+ );
49
+ })
50
+ ).pipe(Effect.flatMap(cell => Deferred.await(cell)));
51
26
- // `Effect.cached` only allocates the memo cell; running it here is
27
- // synchronous, so the lookup and the insert cannot interleave with
28
- // another fiber.
29
- const cached = Effect.runSync(
30
- Effect.cached(
31
- options.make(input).pipe(Effect.onError(() => Effect.sync(() => cache.delete(key))))
32
- )
33
- );
34
- cache.set(key, cached);
35
- return cached;
36
- });
52
+ return load;
53
};
ts/packages/cli/test/src/utils/memoize-in-process.test.ts
+55
-1
@@ -1,5 +1,5 @@
1
import { describe, expect, it } from '@effect/vitest';
2
-import { Effect, Exit } from 'effect';
2
+import { Deferred, Effect, Exit, Fiber } from 'effect';
3
import { memoizeInProcess } from 'src/utils/memoize-in-process';
4
5
describe('memoizeInProcess', () => {
@@ -60,4 +60,58 @@ describe('memoizeInProcess', () => {
60
expect(calls).toBe(2);
61
})
62
);
63
+
64
+ it.effect('an interrupted caller does not take the shared run down with it', () =>
65
+ Effect.gen(function* () {
66
+ const release = yield* Deferred.make<void>();
67
+ let calls = 0;
68
+ const load = memoizeInProcess({
69
+ keyOf: (key: string) => key,
70
+ make: () =>
71
+ Effect.gen(function* () {
72
+ calls += 1;
73
+ yield* Deferred.await(release);
74
+ return calls;
75
+ }),
76
+ });
77
+
78
+ const first = yield* Effect.forkChild(load('k'));
79
+ const second = yield* Effect.forkChild(load('k'));
80
+ yield* Effect.yieldNow;
81
+
82
+ yield* Fiber.interrupt(first);
83
+ yield* Deferred.succeed(release, undefined);
84
+
85
+ expect(yield* Fiber.join(second)).toBe(1);
86
+ expect(yield* load('k')).toBe(1);
87
+ expect(calls).toBe(1);
88
+ })
89
+ );
90
+
91
+ it.effect('a failure reaches every caller already waiting, then is dropped', () =>
92
+ Effect.gen(function* () {
93
+ const release = yield* Deferred.make<void>();
94
+ let calls = 0;
95
+ const load = memoizeInProcess({
96
+ keyOf: (key: string) => key,
97
+ make: () =>
98
+ Effect.gen(function* () {
99
+ calls += 1;
100
+ yield* Deferred.await(release);
101
+ if (calls === 1) return yield* Effect.fail(new Error('first call fails'));
102
+ return calls;
103
+ }),
104
+ });
105
+
106
+ const first = yield* Effect.forkChild(Effect.exit(load('k')));
107
+ const second = yield* Effect.forkChild(Effect.exit(load('k')));
108
+ yield* Effect.yieldNow;
109
+ yield* Deferred.succeed(release, undefined);
110
+
111
+ expect(Exit.isFailure(yield* Fiber.join(first))).toBe(true);
112
+ expect(Exit.isFailure(yield* Fiber.join(second))).toBe(true);
113
+ expect(yield* load('k')).toBe(2);
114
+ expect(calls).toBe(2);
115
+ })
116
+ );
117
});