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

test(cli): cover custom toolkit cache retry

shams haroon committed Aug 20, 2026 at 14:42 UTC d4b96d9c83cbabb48fccd499424cf4f0c96801c4
3 files changed +120 -33
ts/packages/cli/src/commands/tools/commands/tools.search.cmd.ts
+8 -4
@@ -376,7 +376,7 @@ const runToolsSearch = (params: {
376 orgId: resolvedProject.orgId,
377 projectId: resolvedProject.projectId,
378 });
379 - let cacheRefreshAttempted = false;
379 + let cacheRefreshSucceeded = false;
380 if (resolvedProject.projectType === 'CONSUMER') {
381 const cachedToolkits = yield* getFreshConsumerConnectedToolkitsFromCache({
382 orgId: resolvedProject.orgId,
@@ -392,11 +392,15 @@ const runToolsSearch = (params: {
392 forceRefresh || Option.isNone(cachedToolkits) || requestedCustomToolkitMissing;
393
394 if (shouldRefresh) {
395 - cacheRefreshAttempted = true;
395 yield* refreshConsumerConnectedToolkitsCache({
396 orgId: resolvedProject.orgId,
397 consumerUserId: resolvedUserId.value,
398 }).pipe(
399 + Effect.tap(() =>
400 + Effect.sync(() => {
401 + cacheRefreshSucceeded = true;
402 + })
403 + ),
404 Effect.catchAll(() => invalidateConsumerConnectedToolkitsCache().pipe(Effect.ignore))
405 );
406 }
@@ -442,7 +446,7 @@ const runToolsSearch = (params: {
446 toolRouterSessionId: sessionId,
447 }
448 : undefined,
445 - cacheRefreshAttempted,
449 + cacheRefreshSucceeded,
450 };
451 });
452
@@ -460,7 +464,7 @@ const runToolsSearch = (params: {
464 const customToolkitFilters = (toolkitList ?? []).filter(isRemoteCustomToolSlug);
465 const shouldRetryAfterCustomToolkitMiss =
466 customToolkitFilters.length > 0 &&
463 - !firstSearchResult.cacheRefreshAttempted &&
467 + !firstSearchResult.cacheRefreshSucceeded &&
468 customToolkitFilters.some(
469 toolkit => !searchResponseIncludesToolkit(firstSearchResult.searchResponse, toolkit)
470 );
ts/packages/cli/src/services/tool-input-validation.ts
+29 -28
@@ -40,6 +40,28 @@ const toolDefinitionPath = (path: Path.Path, cacheDir: string, slug: string) =>
40 const ensureToolDefinitionsDir = (fs: FileSystem.FileSystem, path: Path.Path, cacheDir: string) =>
41 fs.makeDirectory(path.join(cacheDir, TOOL_DEFINITIONS_DIR), { recursive: true });
42
43 +const writeCachedToolInputDefinition = (params: {
44 + readonly fs: FileSystem.FileSystem;
45 + readonly schemaPath: string;
46 + readonly schema: Record<string, unknown>;
47 + readonly version: string | null;
48 +}) =>
49 + Effect.gen(function* () {
50 + yield* params.fs.writeFileString(
51 + params.schemaPath,
52 + serializeCachedToolDefinition({
53 + version: params.version,
54 + inputSchema: params.schema,
55 + })
56 + );
57 +
58 + return {
59 + schemaPath: params.schemaPath,
60 + schema: params.schema,
61 + version: params.version,
62 + };
63 + });
64 +
65 const parseSchemaFile = (raw: string, schemaPath: string) =>
66 decodeJsonObject(raw).pipe(
67 Effect.mapError(
@@ -154,19 +176,12 @@ export const cacheToolInputDefinition = (params: {
176 const schemaPath = toolDefinitionPath(path, cacheDir, params.slug);
177
178 yield* ensureToolDefinitionsDir(fs, path, cacheDir);
157 - yield* fs.writeFileString(
158 - schemaPath,
159 - serializeCachedToolDefinition({
160 - version: params.version ?? null,
161 - inputSchema: params.schema,
162 - })
163 - );
164 -
165 - return {
179 + return yield* writeCachedToolInputDefinition({
180 + fs,
181 schemaPath,
182 schema: params.schema,
183 version: params.version ?? null,
169 - };
184 + });
185 });
186
187 export const invalidateToolInputDefinition = (slug: string) =>
@@ -225,18 +240,12 @@ const fetchAndCacheToolInputDefinition = (
240 yield* ensureToolDefinitionsDir(fs, path, cacheDir);
241
242 if (localDefinition) {
228 - yield* fs.writeFileString(
229 - schemaPath,
230 - serializeCachedToolDefinition({
231 - version: localDefinition.version,
232 - inputSchema: localDefinition.schema,
233 - })
234 - );
235 - return {
243 + return yield* writeCachedToolInputDefinition({
244 + fs,
245 schemaPath,
246 schema: localDefinition.schema,
247 version: localDefinition.version,
239 - };
248 + });
249 }
250
251 const [tool, latestVersion] = yield* Effect.all(
@@ -264,15 +273,7 @@ const fetchAndCacheToolInputDefinition = (
273 resolvedVersion: version,
274 cachePath: schemaPath,
275 });
267 - yield* fs.writeFileString(
268 - schemaPath,
269 - serializeCachedToolDefinition({
270 - version,
271 - inputSchema: schema,
272 - })
273 - );
274 -
275 - return { schemaPath, schema, version };
276 + return yield* writeCachedToolInputDefinition({ fs, schemaPath, schema, version });
277 });
278
279 export const getOrFetchToolInputDefinition = (
ts/packages/cli/test/src/commands/tools/tools.search.cmd.test.ts
+83 -1
@@ -1,10 +1,13 @@
1 import { describe, expect, layer } from '@effect/vitest';
2 -import { ConfigProvider, Effect } from 'effect';
2 import type {
3 SessionCreateParams,
4 SessionSearchParams,
5 + SessionSearchResponse,
6 } from '@composio/client/resources/tool-router';
7 +import { ConfigProvider, Effect, Option } from 'effect';
8 +import { afterEach, vi } from 'vitest';
9 import { extendConfigProvider } from 'src/services/config';
10 +import * as consumerShortTermCache from 'src/services/consumer-short-term-cache';
11 import { cli, TestLive, MockConsole } from 'test/__utils__';
12 import type { TestLiveInput } from 'test/__utils__/services/test-layer';
13 import type { Tools } from 'src/models/tools';
@@ -98,6 +101,10 @@ const extractFirstJsonObject = (output: string): Record<string, unknown> | null
101 };
102
103 describe('CLI: composio search', () => {
104 + afterEach(() => {
105 + vi.restoreAllMocks();
106 + });
107 +
108 layer(TestLive(testLiveOptions))('[Given] a blank query [Then] typed validation fails', it => {
109 it.scoped('returns the structured missing-query error', () =>
110 Effect.gen(function* () {
@@ -199,6 +206,81 @@ describe('CLI: composio search', () => {
206 }
207 );
208
209 + layer(TestLive(testLiveOptions))(
210 + '[Given] a fresh cache and a custom toolkit miss [Then] search refreshes and retries once',
211 + it => {
212 + it.scoped('refreshes only after the requested custom toolkit is absent', () =>
213 + Effect.gen(function* () {
214 + const customSlug = 'CUSTOM_DEEPWIKI_READ_WIKI_CONTENTS';
215 + let searchCalls = 0;
216 + const refreshSpy = vi
217 + .spyOn(consumerShortTermCache, 'refreshConsumerConnectedToolkitsCache')
218 + .mockReturnValue(Effect.void);
219 + vi.spyOn(consumerShortTermCache, 'getFreshConsumerConnectedToolkitsFromCache').mockReturnValue(
220 + Effect.succeed(Option.some(['CUSTOM_DEEPWIKI']))
221 + );
222 +
223 + const live = TestLive({
224 + ...testLiveOptions,
225 + toolRouter: {
226 + search: async (_sessionId, params) => {
227 + searchCalls += 1;
228 + const found = searchCalls === 2;
229 + const toolSchemas: SessionSearchResponse['tool_schemas'] = found
230 + ? {
231 + [customSlug]: {
232 + tool_slug: customSlug,
233 + toolkit: 'CUSTOM_DEEPWIKI',
234 + description: 'Read wiki contents',
235 + hasFullSchema: true,
236 + input_schema: { type: 'object', properties: {} },
237 + output_schema: { type: 'object', properties: {} },
238 + },
239 + }
240 + : {};
241 + return {
242 + success: true,
243 + error: null,
244 + results: [
245 + {
246 + index: 1,
247 + use_case: params.queries[0]?.use_case ?? '',
248 + primary_tool_slugs: found ? [customSlug] : [],
249 + related_tool_slugs: [],
250 + toolkits: found ? ['CUSTOM_DEEPWIKI'] : [],
251 + },
252 + ],
253 + tool_schemas: toolSchemas,
254 + toolkit_connection_statuses: [],
255 + next_steps_guidance: [],
256 + session: {
257 + id: 'trs_test_session',
258 + generate_id: false,
259 + instructions: 'Reuse this session id for follow-up calls.',
260 + },
261 + time_info: {
262 + current_time_utc: '2026-01-01T00:00:00.000Z',
263 + current_time_utc_epoch_seconds: 1767225600,
264 + message: 'UTC time',
265 + },
266 + };
267 + },
268 + },
269 + });
270 +
271 + yield* cli(['search', 'read wiki contents', '--toolkits', 'CUSTOM_DEEPWIKI']).pipe(
272 + Effect.provide(live)
273 + );
274 +
275 + expect(searchCalls).toBe(2);
276 + expect(refreshSpy).toHaveBeenCalledOnce();
277 + const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');
278 + expect(output).toContain(customSlug);
279 + })
280 + );
281 + }
282 + );
283 +
284 layer(TestLive(testLiveOptions))(
285 '[Given] multiple queries [Then] the CLI returns a batched JSON response',
286 it => {