Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 435 lines 15.4 KB
Raw
1 import * as FileSystem from 'effect/FileSystem';
2 import * as Path from 'effect/Path';
3 import * as BunFileSystem from '@effect/platform-bun/BunFileSystem';
4 import * as BunPath from '@effect/platform-bun/BunPath';
5 import { afterEach, beforeEach, describe, expect, it } from '@effect/vitest';
6 import { vi } from 'vitest';
7 import { Config, ConfigProvider, Effect, Layer, Logger, Option, References } from 'effect';
8
9 // Enhanced controls are disabled on darwin-x64, so pin the platform: these
10 // scenarios are about the transport, not about where the CLI runs.
11 vi.mock('src/services/permission-ui', async importOriginal => ({
12 ...(await importOriginal<typeof import('src/services/permission-ui')>()),
13 isEnhancedControlsPlatformSupported: () => true,
14 }));
15 import {
16 decodeCacheFileTolerant,
17 decodeToolRouterPermissionsConfig,
18 gateToolExecution,
19 refreshConsumerPermissionSnapshot,
20 resolveGateState,
21 ToolPermissionDeniedError,
22 ToolPermissionsRequestError,
23 type ConsumerPermissionSnapshot,
24 } from 'src/services/tool-permissions';
25 import { NodeOs } from 'src/services/node-os';
26 import { ComposioClientConfigurationError } from 'src/services/composio-errors';
27 import { ComposioClientSingleton } from 'src/services/composio-clients';
28 import { ComposioUserContext } from 'src/services/user-context';
29 import { stubComposioClient } from 'test/__utils__/services/stub-composio-client';
30
31 // Pinned wall clock for deterministic fixtures. The SUT reads the real
32 // `Date.now()` (snapshot TTL, allow-decision expiry), so every timestamp
33 // below is expressed relative to this instant.
34 const PINNED_TIME = new Date('2026-01-01T00:00:00.000Z');
35 const PINNED_NOW = PINNED_TIME.getTime();
36
37 const ToolPermissionsTest = Layer.mergeAll(
38 BunFileSystem.layer,
39 BunPath.layer,
40 NodeOs.Default,
41 // fromEnv() snapshots the environment when built; build it per provide so the
42 // per-test COMPOSIO_CACHE_DIR stub is observed.
43 ConfigProvider.layer(Effect.sync(() => ConfigProvider.fromEnv()))
44 );
45
46 const snapshotFixture = (
47 overrides: Partial<ConsumerPermissionSnapshot> = {}
48 ): ConsumerPermissionSnapshot => ({
49 orgId: 'org_test',
50 projectId: 'project_test',
51 consumerUserId: 'user_test',
52 enhancedControlsEnabled: true,
53 permissions: { default: 'allow_all', overrides: {} },
54 connectedAccountIds: [],
55 fetchedAt: PINNED_NOW,
56 ...overrides,
57 });
58
59 // A retried request waits on the clock, so a failing answer asks for no wait.
60 const serverError = () =>
61 Response.json(
62 { error: { message: 'Internal error', code: 500, slug: 'Internal', status: 500 } },
63 { status: 500, headers: { 'retry-after-ms': '0' } }
64 );
65
66 /**
67 * The consumer endpoints the permission snapshot reads. Both are absent from
68 * the v3.1 spec, so production calls them through the client's generic
69 * `get`/`post`; the double answers below the client, at its transport. An
70 * endpoint with no answer responds 500.
71 */
72 const permissionsClientLayer = (responses: {
73 readonly config?: unknown;
74 readonly permissions?: unknown;
75 readonly clientUnavailable?: boolean;
76 }) => {
77 const { client, requests } = stubComposioClient(request => {
78 const body = request.method === 'GET' ? responses.config : responses.permissions;
79 return body === undefined ? serverError() : Response.json(body);
80 });
81 // Every debug record the permission paths log, which is where they report a
82 // request failure they recovered from.
83 const logged: Array<unknown> = [];
84
85 const layer = Layer.mergeAll(
86 Layer.succeed(
87 ComposioClientSingleton,
88 ComposioClientSingleton.of({
89 get: () => Effect.succeed(client),
90 getFor: () =>
91 responses.clientUnavailable === true
92 ? Effect.fail(
93 new ComposioClientConfigurationError({
94 message: 'invalid base URL',
95 cause: null,
96 })
97 )
98 : Effect.succeed(client),
99 getMetrics: () => Effect.succeed({ byteSize: 0, requests: 0 }),
100 })
101 ),
102 Layer.succeed(
103 ComposioUserContext,
104 ComposioUserContext.of({
105 data: {
106 apiKey: Option.some('uak_permissions_test'),
107 baseURL: 'https://backend.composio.dev',
108 webURL: 'https://app.composio.dev',
109 orgId: Option.some('org_test'),
110 projectId: Option.none(),
111 testUserId: Option.none(),
112 },
113 backendMismatch: Option.none(),
114 isLoggedIn: () => true,
115 logout: Effect.void,
116 login: () => Effect.void,
117 update: () => Effect.void,
118 })
119 ),
120 Logger.layer([
121 Logger.make<unknown, void>(({ message }) => {
122 logged.push(...(Array.isArray(message) ? message : [message]));
123 }),
124 ]),
125 Layer.succeed(References.MinimumLogLevel, 'Debug')
126 );
127
128 return {
129 layer,
130 paths: () => requests.map(request => `${request.method} ${request.path}`),
131 requestErrors: () =>
132 logged.filter(
133 (entry): entry is ToolPermissionsRequestError =>
134 entry instanceof ToolPermissionsRequestError
135 ),
136 };
137 };
138
139 const refreshParams = {
140 orgId: 'org_test',
141 projectId: 'project_test',
142 consumerUserId: 'user_test',
143 connectedAccountIds: ['ca_one'],
144 };
145
146 describe('tool permissions', () => {
147 beforeEach(() => {
148 // Fake ONLY `Date`: the SUT's cache reads/writes run on the live Effect
149 // runtime, so real timers and microtasks must keep running.
150 vi.useFakeTimers({ toFake: ['Date'] });
151 vi.setSystemTime(PINNED_TIME);
152 });
153
154 afterEach(() => {
155 vi.useRealTimers();
156 });
157
158 it('maps forward-version permission modes to interactive safe defaults', () => {
159 const decoded = decodeToolRouterPermissionsConfig({
160 default: 'future_default_mode',
161 overrides: {
162 'GMAIL_SEND_EMAIL:__none__': 'future_override_mode',
163 },
164 });
165
166 expect(decoded).toStrictEqual(
167 Option.some({
168 default: 'ask_every_call',
169 overrides: {
170 'GMAIL_SEND_EMAIL:__none__': 'ask_always',
171 },
172 })
173 );
174 });
175
176 it('drops only corrupt cache entries, keeping valid snapshots and allow decisions', () => {
177 const good = snapshotFixture();
178 const raw = JSON.stringify({
179 entries: {
180 good: good,
181 skewed: { ...good, fetchedAt: 'not-a-number' },
182 },
183 allowEntries: {
184 kept: { expiresAt: PINNED_NOW + 60_000 },
185 broken: { expiresAt: 'soon' },
186 },
187 });
188
189 const decoded = decodeCacheFileTolerant(raw);
190
191 expect(decoded).toStrictEqual({
192 entries: { good },
193 allowEntries: { kept: { expiresAt: PINNED_NOW + 60_000 } },
194 });
195 });
196
197 it('returns an empty cache for unparseable cache files', () => {
198 expect(decodeCacheFileTolerant('not json')).toStrictEqual({ entries: {} });
199 });
200
201 it('skips gating when no snapshot applies (developer mode)', () => {
202 expect(resolveGateState({ toolSlug: 'GMAIL_SEND_EMAIL' })).toBe('skip');
203 });
204
205 it('skips gating when enhanced controls are known disabled', () => {
206 expect(
207 resolveGateState({
208 toolSlug: 'GMAIL_SEND_EMAIL',
209 snapshot: snapshotFixture({ enhancedControlsEnabled: false }),
210 })
211 ).toBe('skip');
212 expect(
213 resolveGateState({
214 toolSlug: 'GMAIL_SEND_EMAIL',
215 snapshot: snapshotFixture({ permissions: undefined }),
216 })
217 ).toBe('skip');
218 });
219
220 it('fails closed via the synthesized ask-every-call snapshot when policy resolution failed', () => {
221 // Shape produced by refreshConsumerPermissionSnapshot when the org
222 // reports enhanced controls enabled but the permissions resolve fails.
223 expect(
224 resolveGateState({
225 toolSlug: 'GMAIL_SEND_EMAIL',
226 snapshot: snapshotFixture({ permissions: { default: 'ask_every_call' } }),
227 })
228 ).toBe('ask_every_call');
229 });
230
231 it('resolves overrides ahead of the default mode', () => {
232 expect(
233 resolveGateState({
234 toolSlug: 'GMAIL_SEND_EMAIL',
235 snapshot: snapshotFixture({
236 permissions: {
237 default: 'allow_all',
238 overrides: { 'GMAIL_SEND_EMAIL:__none__': 'always_deny' },
239 },
240 }),
241 })
242 ).toBe('always_deny');
243 });
244
245 it.effect('lets execution proceed ungated without a snapshot', () =>
246 Effect.gen(function* () {
247 const result = yield* gateToolExecution({ toolSlug: 'GMAIL_SEND_EMAIL' });
248
249 expect(result).toBeUndefined();
250 }).pipe(Effect.provide(ToolPermissionsTest))
251 );
252
253 it.effect('preserves permission-policy denial identity', () =>
254 Effect.gen(function* () {
255 const failure = yield* gateToolExecution({
256 toolSlug: 'GMAIL_SEND_EMAIL',
257 snapshot: {
258 orgId: 'org_test',
259 projectId: 'project_test',
260 consumerUserId: 'user_test',
261 enhancedControlsEnabled: true,
262 permissions: {
263 default: 'allow_all',
264 overrides: {
265 'GMAIL_SEND_EMAIL:__none__': 'always_deny',
266 },
267 },
268 connectedAccountIds: [],
269 fetchedAt: PINNED_NOW,
270 },
271 }).pipe(Effect.flip);
272
273 expect(failure).toBeInstanceOf(ToolPermissionDeniedError);
274 expect(failure).toMatchObject({ deniedBy: 'permissions', toolSlug: 'GMAIL_SEND_EMAIL' });
275 }).pipe(Effect.provide(ToolPermissionsTest))
276 );
277
278 it.effect('fails closed when interactive approval is needed but permission UI is disabled', () =>
279 Effect.gen(function* () {
280 vi.stubEnv('COMPOSIO_DISABLE_PERMISSION_UI', '1');
281
282 const failure = yield* gateToolExecution({
283 toolSlug: 'GMAIL_SEND_EMAIL',
284 snapshot: snapshotFixture({ permissions: { default: 'ask_every_call' } }),
285 }).pipe(Effect.flip);
286
287 expect(failure).toBeInstanceOf(ToolPermissionDeniedError);
288 expect(failure).toMatchObject({
289 deniedBy: 'permissions',
290 message: expect.stringContaining('permission prompts are disabled'),
291 });
292 }).pipe(Effect.provide(ToolPermissionsTest))
293 );
294
295 it.effect('honors cached allow decisions even when permission UI is disabled', () =>
296 Effect.gen(function* () {
297 vi.stubEnv('COMPOSIO_DISABLE_PERMISSION_UI', '1');
298
299 const fs = yield* FileSystem.FileSystem;
300 const path = yield* Path.Path;
301 // The shared vitest setup pins COMPOSIO_CACHE_DIR to a fresh temp directory.
302 const cacheDir = yield* Config.String('COMPOSIO_CACHE_DIR').parse(ConfigProvider.fromEnv());
303 // Key shape: `${orgId}:${projectId}:${consumerUserId}:${toolSlug}:${accountId}`.
304 const allowKey = 'org_cached_allow:project_test:user_test:GMAIL_SEND_EMAIL:__none__';
305 yield* fs.writeFileString(
306 path.join(cacheDir, 'tool-permissions-cache.json'),
307 JSON.stringify({
308 entries: {},
309 // Unexpired relative to the pinned clock the SUT's expiry check reads.
310 allowEntries: { [allowKey]: { expiresAt: PINNED_NOW + 60_000 } },
311 })
312 );
313
314 const result = yield* gateToolExecution({
315 toolSlug: 'GMAIL_SEND_EMAIL',
316 snapshot: snapshotFixture({
317 orgId: 'org_cached_allow',
318 permissions: { default: 'ask_every_call' },
319 }),
320 });
321
322 expect(result).toStrictEqual({ approvalStatus: 'cached_approved' });
323 }).pipe(Effect.provide(ToolPermissionsTest))
324 );
325
326 describe('consumer permission snapshot over the owned client', () => {
327 it.effect('reads the policy through the client and keeps it in the snapshot', () =>
328 Effect.gen(function* () {
329 const stub = permissionsClientLayer({
330 config: { enhanced_controls: true },
331 permissions: { experimental: { permissions: { default: 'allow_all', overrides: {} } } },
332 });
333
334 const snapshot = yield* refreshConsumerPermissionSnapshot(refreshParams).pipe(
335 Effect.provide(stub.layer)
336 );
337
338 expect(stub.paths()).toEqual([
339 'GET /api/v3.1/org/consumer/config',
340 'POST /api/v3.1/consumer/permissions/resolve',
341 ]);
342 expect(snapshot).toHaveProperty('enhancedControlsEnabled', true);
343 expect(snapshot).toHaveProperty('permissions', { default: 'allow_all', overrides: {} });
344 }).pipe(Effect.provide(ToolPermissionsTest))
345 );
346
347 it.live('fails closed to ask_every_call when the policy call fails', () =>
348 Effect.gen(function* () {
349 const stub = permissionsClientLayer({ config: { enhanced_controls: true } });
350
351 const snapshot = yield* refreshConsumerPermissionSnapshot(refreshParams).pipe(
352 Effect.provide(stub.layer)
353 );
354
355 expect(snapshot).toHaveProperty('enhancedControlsEnabled', true);
356 expect(snapshot).toHaveProperty('permissions', { default: 'ask_every_call' });
357 expect(resolveGateState({ snapshot, toolSlug: 'GMAIL_SEND_EMAIL' })).toBe('ask_every_call');
358 expect(stub.requestErrors()).toMatchObject([
359 {
360 path: '/api/v3.1/consumer/permissions/resolve',
361 message: 'Failed to resolve consumer permissions.',
362 cause: { _tag: '@composio/client/InternalServerError', status: 500 },
363 },
364 ]);
365 }).pipe(Effect.provide(ToolPermissionsTest))
366 );
367
368 it.effect('fails closed to ask_every_call when the policy does not match its schema', () =>
369 Effect.gen(function* () {
370 const stub = permissionsClientLayer({
371 config: { enhanced_controls: true },
372 permissions: { experimental: { permissions: { default: 42 } } },
373 });
374
375 const snapshot = yield* refreshConsumerPermissionSnapshot(refreshParams).pipe(
376 Effect.provide(stub.layer)
377 );
378
379 expect(snapshot).toHaveProperty('permissions', { default: 'ask_every_call' });
380 expect(stub.requestErrors()).toMatchObject([
381 { path: '/api/v3.1/consumer/permissions/resolve', cause: { _tag: 'SchemaError' } },
382 ]);
383 }).pipe(Effect.provide(ToolPermissionsTest))
384 );
385
386 it.live('returns no snapshot when the consumer config request fails', () =>
387 Effect.gen(function* () {
388 const stub = permissionsClientLayer({});
389
390 const snapshot = yield* refreshConsumerPermissionSnapshot(refreshParams).pipe(
391 Effect.provide(stub.layer)
392 );
393
394 expect(snapshot).toBeUndefined();
395 expect(stub.paths()).not.toContain('POST /api/v3.1/consumer/permissions/resolve');
396 expect(stub.requestErrors()).toMatchObject([
397 {
398 path: '/api/v3.1/org/consumer/config',
399 message: 'Failed to fetch the org consumer config.',
400 cause: { _tag: '@composio/client/InternalServerError', status: 500 },
401 },
402 ]);
403 }).pipe(Effect.provide(ToolPermissionsTest))
404 );
405
406 it.effect('returns no snapshot when the consumer config does not match its schema', () =>
407 Effect.gen(function* () {
408 const stub = permissionsClientLayer({ config: { enhanced_controls: 'yes' } });
409
410 const snapshot = yield* refreshConsumerPermissionSnapshot(refreshParams).pipe(
411 Effect.provide(stub.layer)
412 );
413
414 expect(snapshot).toBeUndefined();
415 expect(stub.paths()).toEqual(['GET /api/v3.1/org/consumer/config']);
416 expect(stub.requestErrors()).toMatchObject([
417 { path: '/api/v3.1/org/consumer/config', cause: { _tag: 'SchemaError' } },
418 ]);
419 }).pipe(Effect.provide(ToolPermissionsTest))
420 );
421
422 it.effect('returns no snapshot when a client cannot be built', () =>
423 Effect.gen(function* () {
424 const stub = permissionsClientLayer({ clientUnavailable: true });
425
426 const snapshot = yield* refreshConsumerPermissionSnapshot(refreshParams).pipe(
427 Effect.provide(stub.layer)
428 );
429
430 expect(snapshot).toBeUndefined();
431 expect(stub.paths()).toEqual([]);
432 }).pipe(Effect.provide(ToolPermissionsTest))
433 );
434 });
435 });