@devries-cameron20 / composio Mirror · read-only
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, 04:45 UTC

next
ts 1,039 lines 40.1 KB
Raw
1 import { CommandInputError } from 'src/commands/errors';
2 import * as fs from 'node:fs';
3 import * as os from 'node:os';
4 import * as path from 'node:path';
5 import { pathToFileURL } from 'node:url';
6 import { describe, expect, layer } from '@effect/vitest';
7 import { assertDefined } from '@effect/vitest/utils';
8 import * as BunServices from '@effect/platform-bun/BunServices';
9 import * as Path from 'effect/Path';
10 import * as FileSystem from 'effect/FileSystem';
11 import {
12 Cause,
13 ConfigProvider,
14 Deferred,
15 Effect,
16 Exit,
17 Fiber,
18 Layer,
19 Option,
20 PlatformError,
21 Redacted,
22 Runtime,
23 Sink,
24 Stream,
25 } from 'effect';
26 import { TestClock } from 'effect/testing';
27 import { ChildProcess, ChildProcessSpawner } from 'effect/process';
28 import { afterEach, beforeEach, it, vi } from 'vitest';
29 import { buildRunHelpersSource, inferCliInvocationPrefix } from 'src/commands/run/run.handler';
30 import {
31 extractInlineExecuteToolSlugs,
32 wrapInlineCodeForRun,
33 } from 'src/commands/run-source-transforms';
34 import {
35 RUN_COMPANION_MODULE_FILENAMES,
36 listMissingInstalledRunCompanionModules,
37 readInstalledReleaseTag,
38 resolveRunCompanionModulePath,
39 writeInstalledReleaseTag,
40 } from 'src/services/run-companion-modules';
41 import { telemetryDebugModeLayer } from 'src/services/runtime-flags';
42 import { cli, MockConsole, recordedInvocationCalls, TestLive } from 'test/__utils__';
43 import { exitCodeOf } from 'test/__utils__/exit-code';
44 import { CommandRunner } from 'src/services/command-runner';
45 import { ChildProcessExited, ChildProcessSignaled } from 'src/effects/child-process-exited';
46 import { ExitAction, exitActionFor } from 'src/run-main';
47 import {
48 makeShutdownController,
49 ShutdownSignal,
50 type ShutdownController,
51 type TerminationSignal,
52 } from 'src/services/shutdown-signal';
53
54 const enabledRuntimeFlagsConfigProvider = ConfigProvider.fromEnvRecord({
55 COMPOSIO_RUN_ACP_ONLY: '1',
56 COMPOSIO_PERF_DEBUG: '1',
57 COMPOSIO_TOOL_DEBUG: '1',
58 });
59
60 /** The run command is `[bun, '--preload', preloadPath, ...]`. */
61 const readRunPreloadSource = (command: ReadonlyArray<string>): string =>
62 fs.readFileSync(String(command[2]), 'utf8');
63
64 /** A script file in a temporary directory that is removed when the test's scope closes. */
65 const tempScript = Effect.acquireRelease(
66 Effect.sync(() => {
67 const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-test-'));
68 const scriptPath = path.join(tempDir, 'script.ts');
69 fs.writeFileSync(scriptPath, 'const value = 1 + 1;\nvalue * 2;\n', 'utf8');
70 return { tempDir, scriptPath };
71 }),
72 ({ tempDir }) => Effect.sync(() => fs.rmSync(tempDir, { recursive: true, force: true }))
73 );
74
75 const SCRIPT = 'console.log("hi")';
76 const SCRIPT_FLAGS = ['--perf-debug', '--tool-debug', '--telemetry-debug', '--help', '--version'];
77
78 const commandRuns = vi.fn(
79 (
80 _: ChildProcess.Command
81 ): Effect.Effect<ChildProcessSpawner.ExitCode, PlatformError.PlatformError> =>
82 Effect.succeed(ChildProcessSpawner.ExitCode(0))
83 );
84 /** Whether the stub child is still running, which the escalation after a signal checks. */
85 const childRunning = vi.fn(() => false);
86
87 // `composio run` starts the child through the platform `ChildProcessSpawner` so it owns the
88 // pid it forwards signals to, so the stub has to replace the spawner rather than
89 // `CommandRunner`. `exitCode` stays suspended: the command only awaits it after the signal
90 // handlers are registered, which is what makes the forwarding observable below.
91 const STUB_CHILD_PID = 987_654;
92
93 const stubHandle = (command: ChildProcess.Command): ChildProcessSpawner.ChildProcessHandle =>
94 ChildProcessSpawner.makeHandle({
95 pid: ChildProcessSpawner.ProcessId(STUB_CHILD_PID),
96 exitCode: Effect.suspend(() => commandRuns(command)),
97 isRunning: Effect.sync(() => childRunning()),
98 kill: () => Effect.void,
99 stdin: Sink.drain,
100 stdout: Stream.empty,
101 stderr: Stream.empty,
102 all: Stream.empty,
103 getInputFd: () => Sink.drain,
104 getOutputFd: () => Stream.empty,
105 unref: Effect.succeed(Effect.void),
106 });
107
108 const StubChildProcessSpawner = Layer.succeed(
109 ChildProcessSpawner.ChildProcessSpawner,
110 ChildProcessSpawner.make(command => Effect.succeed(stubHandle(command)))
111 );
112
113 const RunTestLive = (input: Parameters<typeof TestLive>[0] = {}) =>
114 Layer.merge(
115 TestLive({
116 ...input,
117 commandRunner: CommandRunner.of({
118 run: command => commandRuns(command),
119 capture: () => Effect.succeed({ exitCode: 0, stdout: '', stderr: '' }),
120 }),
121 }),
122 StubChildProcessSpawner
123 );
124
125 const inspectRunCommand = (command: ChildProcess.Command) => {
126 if (!ChildProcess.isStandardCommand(command)) {
127 throw new Error('Expected the run command to be a standard (non-piped) command.');
128 }
129 return {
130 cmd: [
131 command.command,
132 ...command.args.map(arg => (Redacted.isRedacted(arg) ? Redacted.value(arg) : arg)),
133 ],
134 env: Object.fromEntries(
135 Object.entries(command.options.env ?? {}).map(([key, value]) => [
136 key,
137 Redacted.isRedacted(value) ? Redacted.value(value) : value,
138 ])
139 ),
140 extendEnv: command.options.extendEnv,
141 stdio: [command.options.stdin, command.options.stdout, command.options.stderr],
142 };
143 };
144
145 describe('CLI: composio run', () => {
146 afterEach(() => {
147 commandRuns
148 .mockReset()
149 .mockImplementation(() => Effect.succeed(ChildProcessSpawner.ExitCode(0)));
150 childRunning.mockReset().mockImplementation(() => false);
151 vi.unstubAllGlobals();
152 vi.restoreAllMocks();
153 });
154
155 layer(RunTestLive())(it => {
156 it.effect(
157 '[Given] a root run [Then] the child receives the run id its telemetry recorded',
158 () =>
159 Effect.gen(function* () {
160 let childRunId: string | undefined;
161 commandRuns.mockImplementation(command => {
162 childRunId = inspectRunCommand(command).env.COMPOSIO_CLI_PARENT_RUN_ID;
163 return Effect.succeed(ChildProcessSpawner.ExitCode(0));
164 });
165
166 // The invocation mints the run id for its telemetry and hands it to the command, instead
167 // of publishing it through process-wide state.
168 yield* cli(['--telemetry-debug', 'run', 'console.log("hi")']);
169
170 expect(commandRuns).toHaveBeenCalledTimes(1);
171 const invoked = (yield* recordedInvocationCalls).find(
172 call => call.service === 'CommandLifecycleTelemetry' && call.event === 'invoked'
173 );
174 const runId =
175 invoked?.service === 'CommandLifecycleTelemetry' && invoked.event === 'invoked'
176 ? invoked.context.runId
177 : undefined;
178 assertDefined(runId);
179 expect(childRunId).toBe(runId);
180 })
181 );
182 });
183
184 describe('signals', () => {
185 // The runner's controller for this run: the test delivers the CLI's signals through it.
186 let controller: ShutdownController = makeShutdownController();
187 const signalled: Array<readonly [number, string]> = [];
188 // Completes once `process.kill` received each signal, so a test can wait for delivery.
189 let delivered = new Map<string, Deferred.Deferred<void>>();
190 const deliveryOf = (signal: string) => {
191 const deferred = delivered.get(signal) ?? Deferred.makeUnsafe<void>();
192 delivered.set(signal, deferred);
193 return deferred;
194 };
195
196 beforeEach(() => {
197 controller = makeShutdownController();
198 signalled.length = 0;
199 delivered = new Map();
200 vi.spyOn(process, 'kill').mockImplementation((pid, signal) => {
201 signalled.push([Number(pid), String(signal)]);
202 Deferred.doneUnsafe(deliveryOf(String(signal)), Effect.void);
203 return true;
204 });
205 });
206
207 /** `composio run` with the CLI receiving `signal` while the child runs. */
208 const runWithSignals = Effect.suspend(() =>
209 Effect.exit(
210 cli(['run', 'console.log("hi")']).pipe(
211 Effect.provideService(ShutdownSignal, controller.service)
212 )
213 )
214 );
215
216 /** A child that, once the CLI took `signal`, waits for it and then ends with `ends`. */
217 const childEndingAfter = (
218 signal: 'SIGINT' | 'SIGTERM' | 'SIGHUP',
219 ends: Effect.Effect<ChildProcessSpawner.ExitCode, PlatformError.PlatformError>
220 ) =>
221 commandRuns.mockImplementation(() =>
222 Effect.sync(() => expect(controller.offer(signal)).toBe(true)).pipe(
223 Effect.andThen(Deferred.await(deliveryOf(signal))),
224 Effect.andThen(ends)
225 )
226 );
227
228 /** The spawner's failure for a child a signal ended. */
229 const killedBy = (signal: TerminationSignal) =>
230 Effect.fail(
231 PlatformError.systemError({
232 _tag: 'Unknown',
233 module: 'ChildProcess',
234 method: 'exitCode',
235 cause: new Error(`Process interrupted due to receipt of signal: '${signal}'`),
236 })
237 );
238
239 layer(RunTestLive())(it => {
240 it.effect.each(['SIGINT', 'SIGTERM', 'SIGHUP'] as const)(
241 '[Given] the CLI receives %s [Then] it forwards it to the child process group',
242 signal =>
243 Effect.gen(function* () {
244 childEndingAfter(signal, Effect.succeed(ChildProcessSpawner.ExitCode(0)));
245
246 const exit = yield* runWithSignals;
247
248 // Negative pid: the detached child leads its own process group.
249 expect(signalled).toEqual([[-STUB_CHILD_PID, signal]]);
250 // A script that handles the signal and exits 0 makes the run succeed.
251 expect(exitCodeOf(exit)).toBe(0);
252 })
253 );
254
255 it.effect(
256 '[Given] the child exits non-zero after the signal [Then] the run mirrors its code',
257 () =>
258 Effect.gen(function* () {
259 childEndingAfter('SIGINT', Effect.succeed(ChildProcessSpawner.ExitCode(7)));
260
261 const exit = yield* runWithSignals;
262
263 expect(exitCodeOf(exit)).toBe(7);
264 })
265 );
266
267 it.effect('[Given] the signal ends the child [Then] the runner re-raises that signal', () =>
268 Effect.gen(function* () {
269 childEndingAfter('SIGTERM', killedBy('SIGTERM'));
270
271 const exit = yield* runWithSignals;
272 const failure = Exit.isFailure(exit) ? Cause.squash(exit.cause) : undefined;
273
274 expect(failure).toBeInstanceOf(ChildProcessSignaled);
275 expect(Runtime.getErrorReported(failure)).toBe(false);
276 expect(exitActionFor(exit, Option.some('SIGTERM'))).toEqual(
277 ExitAction.Reraise({ signal: 'SIGTERM' })
278 );
279 })
280 );
281
282 it.effect(
283 '[Given] a child that ignores the signal [Then] it gets SIGTERM, then SIGKILL, and the run ends by the first signal',
284 () =>
285 Effect.gen(function* () {
286 childRunning.mockImplementation(() => true);
287 const killed = deliveryOf('SIGKILL');
288 childEndingAfter(
289 'SIGINT',
290 Deferred.await(killed).pipe(Effect.andThen(killedBy('SIGKILL')))
291 );
292
293 const fiber = yield* Effect.forkChild(runWithSignals);
294 yield* Deferred.await(deliveryOf('SIGINT'));
295 yield* TestClock.adjust('2 seconds');
296 yield* Deferred.await(deliveryOf('SIGTERM'));
297 yield* TestClock.adjust('2 seconds');
298 const exit = yield* Fiber.join(fiber);
299
300 expect(signalled).toEqual([
301 [-STUB_CHILD_PID, 'SIGINT'],
302 [-STUB_CHILD_PID, 'SIGTERM'],
303 [-STUB_CHILD_PID, 'SIGKILL'],
304 ]);
305 // The CLI's own SIGKILL ended the script; the user cancelled with SIGINT.
306 expect(exitActionFor(exit, Option.some('SIGINT'))).toEqual(
307 ExitAction.Reraise({ signal: 'SIGINT' })
308 );
309 })
310 );
311
312 it.effect(
313 '[Given] a child that crashes by another signal during the escalation [Then] the run ends by that signal',
314 () =>
315 Effect.gen(function* () {
316 childRunning.mockImplementation(() => true);
317 // The script ignores SIGINT, then aborts while cleaning up after the CLI's SIGTERM.
318 childEndingAfter(
319 'SIGINT',
320 Deferred.await(deliveryOf('SIGTERM')).pipe(Effect.andThen(killedBy('SIGABRT')))
321 );
322
323 const fiber = yield* Effect.forkChild(runWithSignals);
324 yield* Deferred.await(deliveryOf('SIGINT'));
325 yield* TestClock.adjust('2 seconds');
326 const exit = yield* Fiber.join(fiber);
327 const failure = Exit.isFailure(exit) ? Cause.squash(exit.cause) : undefined;
328
329 expect(signalled).toEqual([
330 [-STUB_CHILD_PID, 'SIGINT'],
331 [-STUB_CHILD_PID, 'SIGTERM'],
332 ]);
333 // The crash is the script's own, not the user's cancellation.
334 expect(failure).toBeInstanceOf(ChildProcessSignaled);
335 expect(exitActionFor(exit, Option.some('SIGINT'))).toEqual(
336 ExitAction.Reraise({ signal: 'SIGABRT' })
337 );
338 })
339 );
340
341 it.effect(
342 '[Given] a second signal [Then] forcing the exit kills the child group and re-raises',
343 () =>
344 Effect.gen(function* () {
345 const forced = yield* Deferred.make<{ readonly reraise: boolean }>();
346 commandRuns.mockImplementation(() =>
347 Effect.sync(() => {
348 controller.offer('SIGINT');
349 // What the runner does on the second signal, while the child still runs.
350 Deferred.doneUnsafe(forced, Effect.succeed(controller.force()));
351 return ChildProcessSpawner.ExitCode(0);
352 })
353 );
354
355 yield* runWithSignals;
356
357 expect(yield* Deferred.await(forced)).toEqual({ reraise: true });
358 expect(signalled).toContainEqual([-STUB_CHILD_PID, 'SIGKILL']);
359 })
360 );
361 });
362 });
363
364 layer(RunTestLive())(it => {
365 it.effect(
366 '[Given] inline code and args [Then] it forwards them to the embedded Bun runtime',
367 () =>
368 Effect.gen(function* () {
369 commandRuns.mockImplementation(() => Effect.succeed(ChildProcessSpawner.ExitCode(7)));
370
371 const exit = yield* Effect.exit(cli(['run', 'console.log("hi")', '--flag', 'value']));
372 const output = yield* MockConsole.getLines();
373
374 // The process exits with the child's code.
375 expect(exitCodeOf(exit)).toBe(7);
376 expect(commandRuns).toHaveBeenCalledTimes(1);
377 const spawnConfig = inspectRunCommand(commandRuns.mock.calls[0]![0]);
378 expect(spawnConfig.cmd[0]).toBe(process.execPath);
379 expect(spawnConfig.cmd[1]).toBe('--preload');
380 expect(spawnConfig.cmd[2]).toMatch(/globals\.mjs$/);
381 expect(spawnConfig.cmd[3]).toBe('--eval');
382 expect(spawnConfig.cmd[4]).toContain('(async () => {');
383 expect(spawnConfig.cmd[4]).toContain('return (console.log("hi"));');
384 expect(spawnConfig.cmd[4]).toContain('if (__composioResult !== undefined) {');
385 expect(spawnConfig.cmd.slice(5)).toEqual(['--', '--flag', 'value']);
386 expect(spawnConfig.env).toEqual(expect.objectContaining({ BUN_BE_BUN: '1' }));
387 expect(spawnConfig.extendEnv).toBe(true);
388 expect(spawnConfig.stdio).toEqual(['inherit', 'inherit', 'inherit']);
389 expect(output).toContainEqual(expect.stringMatching(/^RUN_LOG_FILE=.*run\.log$/));
390 // The preload file lives in a scoped directory and is removed with it, ...
391 expect(fs.existsSync(spawnConfig.cmd[2]!)).toBe(false);
392 // ... but the run log is advertised to the caller on stderr, so it has to outlive
393 // the run that printed it.
394 const runLogPath = String(output.find(line => line.startsWith('RUN_LOG_FILE='))).slice(
395 'RUN_LOG_FILE='.length
396 );
397 expect(fs.existsSync(runLogPath)).toBe(true);
398 fs.rmSync(path.dirname(runLogPath), { recursive: true, force: true });
399 })
400 );
401 });
402
403 layer(RunTestLive())(it => {
404 it.effect(
405 '[Given] a child that exits non-zero [Then] it fails with ChildProcessExited, which the runtime does not log',
406 () =>
407 Effect.gen(function* () {
408 commandRuns.mockImplementation(() => Effect.succeed(ChildProcessSpawner.ExitCode(3)));
409
410 const failure = yield* cli(['run', 'console.log("hi")']).pipe(Effect.flip);
411
412 expect(failure).toBeInstanceOf(ChildProcessExited);
413 expect(failure).toMatchObject({ code: 3 });
414 // The child already wrote its own output; `BunRuntime.runMain` must not add a log.
415 expect(Runtime.getErrorReported(failure)).toBe(false);
416 })
417 );
418
419 it.effect('[Given] a child that exits 0 [Then] the run succeeds', () =>
420 Effect.gen(function* () {
421 const exit = yield* Effect.exit(cli(['run', 'console.log("hi")']));
422
423 expect(exitCodeOf(exit)).toBe(0);
424 })
425 );
426
427 // Ctrl-C interrupts the run while the child is still running: the process exits 130, not 0,
428 // so wrappers and `set -e` scripts do not read a cancelled run as a passing one.
429 it.effect('[Given] the run is interrupted [Then] the process exits 130', () =>
430 Effect.gen(function* () {
431 const started = yield* Deferred.make<void>();
432 commandRuns.mockImplementation(() =>
433 Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never))
434 );
435
436 const fiber = yield* Effect.forkChild(cli(['run', 'console.log("hi")']));
437 yield* Deferred.await(started);
438 yield* Fiber.interrupt(fiber);
439
440 expect(exitCodeOf(yield* Fiber.await(fiber))).toBe(130);
441 })
442 );
443 });
444
445 layer(RunTestLive())(it => {
446 it.effect('[Given] a run [Then] the preload context carries no sub-agent inputs', () =>
447 Effect.gen(function* () {
448 let preloadSource = '';
449 commandRuns.mockImplementation(command => {
450 preloadSource = readRunPreloadSource(inspectRunCommand(command).cmd);
451 return Effect.succeed(ChildProcessSpawner.ExitCode(0));
452 });
453
454 yield* cli(['run', 'console.log("hi")']);
455
456 expect(preloadSource).toContain('"runLogFilePath":');
457 expect(preloadSource).not.toContain('"master":');
458 expect(preloadSource).not.toContain('"readAccessRoots":');
459 expect(preloadSource).not.toContain('"cliConfigPath":');
460 })
461 );
462
463 it.effect('[Given] a run from source [Then] the preload imports the helper runtime', () =>
464 Effect.gen(function* () {
465 let preloadSource = '';
466 commandRuns.mockImplementation(command => {
467 preloadSource = readRunPreloadSource(inspectRunCommand(command).cmd);
468 return Effect.succeed(ChildProcessSpawner.ExitCode(0));
469 });
470
471 yield* cli(['run', 'console.log("hi")']);
472
473 const helpersRuntimeUrl = pathToFileURL(
474 path.resolve(import.meta.dirname, '../../../../src/services/run-helpers-runtime.ts')
475 ).href;
476 expect(preloadSource).toContain(
477 `import { installRunHelpers } from ${JSON.stringify(helpersRuntimeUrl)};`
478 );
479 })
480 );
481 });
482
483 layer(RunTestLive({ baseConfigProvider: enabledRuntimeFlagsConfigProvider }))(it => {
484 it.effect(
485 '[Given] COMPOSIO_RUN_ACP_ONLY=1 [Then] only the remaining debug flags reach the script',
486 () =>
487 Effect.gen(function* () {
488 let preloadSource = '';
489 commandRuns.mockImplementation(command => {
490 preloadSource = readRunPreloadSource(inspectRunCommand(command).cmd);
491 return Effect.succeed(ChildProcessSpawner.ExitCode(0));
492 });
493
494 yield* cli(['run', 'console.log("hi")']);
495
496 const command = inspectRunCommand(commandRuns.mock.calls[0]![0]);
497 expect(command.env).toEqual({
498 BUN_BE_BUN: '1',
499 COMPOSIO_CLI_PARENT_RUN_ID: expect.any(String),
500 COMPOSIO_PERF_DEBUG: '1',
501 COMPOSIO_TOOL_DEBUG: '1',
502 COMPOSIO_CLI_TELEMETRY_DEBUG: '0',
503 });
504 expect(preloadSource).toContain('"perfDebug":true');
505 expect(preloadSource).toContain('"toolDebug":true');
506 expect(preloadSource).toContain('"telemetryDebug":false');
507 expect(preloadSource).not.toContain('acpOnly');
508 expect(preloadSource).not.toContain('COMPOSIO_RUN_ACP_ONLY');
509 })
510 );
511
512 it.effect('[Given] explicit false flags [Then] inherited true values are cleared', () =>
513 Effect.gen(function* () {
514 let preloadSource = '';
515 commandRuns.mockImplementation(command => {
516 preloadSource = readRunPreloadSource(inspectRunCommand(command).cmd);
517 return Effect.succeed(ChildProcessSpawner.ExitCode(0));
518 });
519
520 yield* cli(['run', '--perf-debug=false', '--tool-debug=false', 'console.log("hi")']);
521
522 const command = inspectRunCommand(commandRuns.mock.calls[0]![0]);
523 expect(command.env).toMatchObject({
524 COMPOSIO_PERF_DEBUG: '0',
525 COMPOSIO_TOOL_DEBUG: '0',
526 COMPOSIO_CLI_TELEMETRY_DEBUG: '0',
527 });
528 expect(preloadSource).toContain('"perfDebug":false');
529 expect(preloadSource).toContain('"toolDebug":false');
530 })
531 );
532 });
533
534 const webURLForwarding = [
535 {
536 name: 'no URL overrides',
537 env: {},
538 forwarded: true,
539 },
540 {
541 name: 'a custom backend URL with no web URL',
542 env: { COMPOSIO_BASE_URL: 'https://composio.internal.example' },
543 forwarded: false,
544 },
545 {
546 name: 'a custom backend URL and a web URL',
547 env: {
548 COMPOSIO_BASE_URL: 'https://composio.internal.example',
549 COMPOSIO_WEB_URL: 'https://dashboard.internal.example',
550 },
551 forwarded: true,
552 },
553 ] as const;
554
555 describe.each(webURLForwarding)('$name', testCase => {
556 layer(
557 RunTestLive({
558 baseConfigProvider: ConfigProvider.fromEnv({ env: testCase.env }),
559 })
560 )(it => {
561 // A forwarded web URL is what moves a script's `execute()` onto the
562 // Dashboard, so it must not appear when this process stays on the backend.
563 it.effect(
564 `[Given] ${testCase.name} [Then] the web URL is ${testCase.forwarded ? '' : 'not '}forwarded to helpers`,
565 () =>
566 Effect.gen(function* () {
567 let preloadSource = '';
568 commandRuns.mockImplementation(command => {
569 preloadSource = readRunPreloadSource(inspectRunCommand(command).cmd);
570 return Effect.succeed(ChildProcessSpawner.ExitCode(0));
571 });
572
573 yield* cli(['run', 'console.log("hi")']);
574
575 expect(preloadSource).toContain('"baseURL":');
576 expect(preloadSource.includes('"webURL":')).toBe(testCase.forwarded);
577 })
578 );
579 });
580 });
581
582 layer(Layer.merge(RunTestLive(), telemetryDebugModeLayer(true)))(it => {
583 it.effect(
584 '[Given] --telemetry-debug [Then] the spawned script and its children observe it',
585 () =>
586 Effect.gen(function* () {
587 let preloadSource = '';
588 commandRuns.mockImplementation(command => {
589 preloadSource = readRunPreloadSource(inspectRunCommand(command).cmd);
590 return Effect.succeed(ChildProcessSpawner.ExitCode(0));
591 });
592
593 yield* cli(['run', 'console.log("hi")']);
594
595 const command = inspectRunCommand(commandRuns.mock.calls[0]![0]);
596 expect(command.env.COMPOSIO_CLI_TELEMETRY_DEBUG).toBe('1');
597 expect(preloadSource).toContain('"telemetryDebug":true');
598 })
599 );
600 });
601
602 layer(RunTestLive())(it => {
603 it.effect.each([
604 { flag: '--acp-only', name: '--acp-only' },
605 { flag: '--acp-only=false', name: '--acp-only' },
606 { flag: '--no-acp-only', name: '--no-acp-only' },
607 { flag: '--logs-off', name: '--logs-off' },
608 { flag: '--logs-off=false', name: '--logs-off' },
609 { flag: '--no-logs-off', name: '--no-logs-off' },
610 { flag: '--foo_bar', name: '--foo_bar' },
611 { flag: '--foo_bar=value', name: '--foo_bar' },
612 ])('[Given] the unknown $flag [Then] run rejects it as an unknown flag', ({ flag, name }) =>
613 Effect.gen(function* () {
614 const failure = yield* cli(['run', flag, 'console.log(1)']).pipe(Effect.flip);
615
616 expect(failure).toMatchObject({ _tag: 'ShowHelp' });
617 expect(commandRuns).not.toHaveBeenCalled();
618 const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');
619 expect(output).toContain(`Unrecognized flag: ${name} in command composio run`);
620 })
621 );
622
623 it.effect('[Given] repeated invocations [Then] hidden flags do not leak', () =>
624 Effect.gen(function* () {
625 const preloadSources: string[] = [];
626 commandRuns.mockImplementation(command => {
627 preloadSources.push(readRunPreloadSource(inspectRunCommand(command).cmd));
628 return Effect.succeed(ChildProcessSpawner.ExitCode(0));
629 });
630
631 yield* cli(['run', '--perf-debug', 'console.log("first")']);
632 yield* cli(['run', 'console.log("second")']);
633
634 expect(preloadSources).toHaveLength(2);
635 expect(preloadSources[0]).toContain('"perfDebug":true');
636 expect(preloadSources[1]).toContain('"perfDebug":false');
637 })
638 );
639 });
640
641 layer(RunTestLive())(it => {
642 it.effect(
643 '[Given] a multiline execute script [Then] run preserves the inline TypeScript source',
644 () =>
645 Effect.gen(function* () {
646 const script = `
647 const issue = await execute(
648 "GITHUB_CREATE_ISSUE",
649 {
650 owner: "acme",
651 title: [
652 "Deploy v2",
653 "Do not run terminal commands.",
654 ].join("\\n"),
655 }
656 );
657 console.log(JSON.stringify(issue));
658 console.log(JSON.stringify(issue.data));
659 `;
660 yield* cli(['run', script]);
661
662 expect(commandRuns).toHaveBeenCalledTimes(1);
663 const spawnConfig = inspectRunCommand(commandRuns.mock.calls[0]![0]);
664 expect(spawnConfig.cmd[3]).toBe('--eval');
665 expect(spawnConfig.cmd[4]).toContain('const issue = await execute(');
666 expect(spawnConfig.cmd[4]).toContain('"Do not run terminal commands."');
667 expect(spawnConfig.cmd[4]).toContain('].join("\\n"),');
668 expect(spawnConfig.cmd[4]).toContain('owner: "acme"');
669 expect(spawnConfig.cmd[4]).toContain('console.log(JSON.stringify(issue));');
670 expect(spawnConfig.cmd[4]).toContain('return (console.log(JSON.stringify(issue.data)));');
671 expect(spawnConfig.cmd[4]).not.toContain('"Do not run terminal\n');
672 })
673 );
674 });
675
676 layer(RunTestLive())(it => {
677 it.effect('[Given] --file [Then] it forwards file execution to the embedded Bun runtime', () =>
678 Effect.gen(function* () {
679 const { scriptPath } = yield* tempScript;
680 yield* cli(['run', '--file', scriptPath, '--', 'hello']);
681
682 expect(commandRuns).toHaveBeenCalledTimes(1);
683 const spawnConfig = inspectRunCommand(commandRuns.mock.calls[0]![0]);
684 expect(spawnConfig.cmd[0]).toBe(process.execPath);
685 expect(spawnConfig.cmd[1]).toBe('--preload');
686 expect(spawnConfig.cmd[2]).toMatch(/globals\.mjs$/);
687 expect(spawnConfig.cmd[3]).toMatch(/\.composio-run-.*\.ts$/);
688 expect(spawnConfig.cmd[4]).toBe('--');
689 expect(spawnConfig.cmd[5]).toBe('hello');
690 expect(spawnConfig.env).toEqual(expect.objectContaining({ BUN_BE_BUN: '1' }));
691 expect(spawnConfig.extendEnv).toBe(true);
692 expect(spawnConfig.stdio).toEqual(['inherit', 'inherit', 'inherit']);
693 expect(fs.existsSync(spawnConfig.cmd[2]!)).toBe(false);
694 expect(fs.existsSync(spawnConfig.cmd[3]!)).toBe(false);
695 })
696 );
697 });
698
699 layer(RunTestLive())(it => {
700 it.effect(
701 '[Given] no inline code and no --file [Then] it fails with a typed usage error, not a defect',
702 () =>
703 Effect.gen(function* () {
704 const failure = yield* cli(['run']).pipe(Effect.flip);
705
706 expect(failure).toBeInstanceOf(CommandInputError);
707 expect(failure).toMatchObject({
708 message: expect.stringContaining(
709 'Provide inline code or use --file to run a script file.'
710 ),
711 });
712 // Nothing was set up before the check, so no child process was started.
713 expect(commandRuns).not.toHaveBeenCalled();
714 })
715 );
716 });
717
718 layer(RunTestLive())(it => {
719 it.effect(
720 '[Given] --file=path inline form followed by --dry-run [Then] both parse as run flags',
721 () =>
722 Effect.gen(function* () {
723 const { scriptPath } = yield* tempScript;
724 yield* cli(['run', `--file=${scriptPath}`, '--dry-run']);
725
726 expect(commandRuns).toHaveBeenCalledTimes(1);
727 const spawnConfig = inspectRunCommand(commandRuns.mock.calls[0]![0]);
728 // `--file=...` must be recognized as a run flag: file mode compiles a
729 // wrapper script (not `--eval` inline code), and `--dry-run` must not
730 // leak into the forwarded script arguments.
731 expect(spawnConfig.cmd[3]).toMatch(/\.composio-run-.*\.ts$/);
732 expect(spawnConfig.cmd).not.toContain('--dry-run');
733 })
734 );
735 });
736
737 layer(RunTestLive())(it => {
738 it.effect.each([
739 { layout: 'no delimiter', args: [SCRIPT, ...SCRIPT_FLAGS] },
740 { layout: 'a delimiter after the code', args: [SCRIPT, '--', ...SCRIPT_FLAGS] },
741 { layout: 'a delimiter before the code', args: ['--', SCRIPT, ...SCRIPT_FLAGS] },
742 ])('forwards script flags with $layout without enabling them in the CLI', ({ args }) =>
743 Effect.gen(function* () {
744 yield* cli(['--perf-debug=false', 'run', ...args]);
745
746 expect(commandRuns).toHaveBeenCalledTimes(1);
747 const spawned = inspectRunCommand(commandRuns.mock.calls[0]![0]);
748 expect(spawned.cmd.slice(5)).toEqual(['--', ...SCRIPT_FLAGS]);
749 expect(spawned.env).toMatchObject({
750 COMPOSIO_PERF_DEBUG: '0',
751 COMPOSIO_TOOL_DEBUG: '0',
752 COMPOSIO_CLI_TELEMETRY_DEBUG: '0',
753 });
754 })
755 );
756 });
757
758 layer(RunTestLive())(it => {
759 it.effect(
760 '[Given] a second literal -- in passthrough args [Then] it is forwarded to the script',
761 () =>
762 Effect.gen(function* () {
763 yield* cli(['run', 'console.log("hi")', '--', 'alpha', '--', 'beta']);
764
765 expect(commandRuns).toHaveBeenCalledTimes(1);
766 const spawnConfig = inspectRunCommand(commandRuns.mock.calls[0]![0]);
767 // First `--` is the run/script boundary; the second is a script
768 // argument and must reach the script verbatim (v3 behavior).
769 expect(spawnConfig.cmd.slice(5)).toEqual(['--', 'alpha', '--', 'beta']);
770 })
771 );
772 });
773
774 layer(RunTestLive())(it => {
775 it.effect(
776 '[Given] a script arg literally starting with the old escape-marker string [Then] it reaches the script untouched',
777 () =>
778 Effect.gen(function* () {
779 yield* cli(['run', 'console.log("hi")', '@@composio-run-raw@@literal']);
780
781 expect(commandRuns).toHaveBeenCalledTimes(1);
782 const spawnConfig = inspectRunCommand(commandRuns.mock.calls[0]![0]);
783 expect(spawnConfig.cmd.slice(5)).toEqual(['--', '@@composio-run-raw@@literal']);
784 })
785 );
786 });
787
788 layer(RunTestLive())(it => {
789 it.effect(
790 '[Given] run help [Then] it documents injected execute, search, proxy, and z helpers without the removed sub-agent',
791 () =>
792 Effect.gen(function* () {
793 yield* cli(['run', '--help']);
794 const lines = yield* MockConsole.getLines({ stripAnsi: true });
795 const output = lines.join('\n');
796 expect(output).toContain(
797 'Run inline TS/JS code or a file with injected Composio helpers that behave like their CLI counterparts.'
798 );
799 expect(output).toContain('--skip-connection-check');
800 expect(output).toContain('--skip-tool-params-check');
801 expect(output).toContain('--skip-checks');
802 expect(output).not.toContain('--logs-off');
803 expect(output).not.toContain('experimental_subAgent');
804 expect(output).toContain('Injected helpers');
805 expect(output).toContain('execute(slug, data?)');
806 expect(output).toContain('search(query, options?)');
807 expect(output).toContain('result.prompt()');
808 expect(output).toContain('const f = await proxy(toolkit)');
809 expect(output).toContain('Injected global from `zod`');
810 expect(output).toContain('composio search "<query>"');
811 expect(output).toContain('composio execute <slug> --get-schema');
812 expect(output).not.toContain('--acp-only');
813 })
814 );
815 });
816 });
817
818 describe('buildRunHelpersSource', () => {
819 it('[Given] consumer context [Then] it embeds auth and consumer metadata in the helper source', () => {
820 const source = buildRunHelpersSource(
821 ['/tmp/composio'],
822 {
823 apiKey: 'test_api_key',
824 baseURL: 'https://api.example.test',
825 webURL: 'https://app.example.test',
826 orgId: 'org_test',
827 consumerUserId: 'consumer_user_test',
828 dryRun: true,
829 runLogFilePath: '/tmp/composio-run/run.log',
830 },
831 {
832 helpersRuntimeModuleUrl: pathToFileURL('/tmp/composio-run/run-helpers-runtime.mjs').href,
833 }
834 );
835
836 expect(source).toContain('import { installRunHelpers } from "file://');
837 expect(source).toContain('await installRunHelpers(');
838 expect(source).toContain('"cliPrefix":["/tmp/composio"]');
839 expect(source).toContain('"runLogFilePath":"/tmp/composio-run/run.log"');
840 expect(source).toContain('"consumerUserId":"consumer_user_test"');
841 expect(source).not.toContain('globalThis.execute = async (slug, data = {}) => {');
842 });
843 });
844
845 describe('inferCliInvocationPrefix', () => {
846 layer(BunServices.layer)(it => {
847 it.effect.each([
848 '/$bunfs/root/composio',
849 'B:\\$bunfs\\root\\composio.exe',
850 'B:/~BUN/root/composio.exe',
851 'B:\\~BUN\\root\\composio.exe',
852 ])(
853 '[Given] a compiled entrypoint %s [Then] it falls back to the binary path only',
854 entrypoint =>
855 Effect.gen(function* () {
856 const pathService = yield* Path.Path;
857 expect(
858 yield* inferCliInvocationPrefix(pathService, ['node', entrypoint]).pipe(
859 Effect.provide(FileSystem.layerNoop({ exists: () => Effect.succeed(true) }))
860 )
861 ).toEqual([process.execPath]);
862 })
863 );
864 });
865 });
866
867 describe('resolveRunCompanionModulePath', () => {
868 layer(BunServices.layer)(it => {
869 it.effect(
870 '[Given] a bundled dist chunk [Then] it resolves sibling companion modules in dist',
871 () =>
872 Effect.gen(function* () {
873 const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-companion-dist-'));
874 const callerPath = path.join(tempDir, 'commands-abc.mjs');
875 const servicesDir = path.join(tempDir, 'services');
876 const companionPath = path.join(servicesDir, 'run-helpers-runtime.mjs');
877 fs.writeFileSync(callerPath, '', 'utf8');
878 fs.mkdirSync(servicesDir);
879 fs.writeFileSync(companionPath, '', 'utf8');
880
881 expect(
882 yield* resolveRunCompanionModulePath({
883 callerImportMetaUrl: pathToFileURL(callerPath).href,
884 execPath: '/tmp/composio',
885 relativeNoExtensionFromCaller: '../services/run-helpers-runtime',
886 })
887 ).toBe(companionPath);
888 })
889 );
890
891 it.effect(
892 '[Given] a compiled bunfs caller [Then] it falls back to modules next to the binary',
893 () =>
894 Effect.gen(function* () {
895 const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-companion-bin-'));
896 const execPath = path.join(tempDir, 'composio');
897 const companionPath = path.join(tempDir, 'run-helpers-runtime.mjs');
898 fs.writeFileSync(companionPath, '', 'utf8');
899
900 expect(
901 yield* resolveRunCompanionModulePath({
902 callerImportMetaUrl: 'file:///$bunfs/root/commands.mjs',
903 execPath,
904 relativeNoExtensionFromCaller: '../services/run-helpers-runtime',
905 })
906 ).toBe(companionPath);
907 })
908 );
909 });
910 });
911
912 describe('run companion install metadata', () => {
913 layer(BunServices.layer)(it => {
914 it.effect(
915 '[Given] an installed release tag file [Then] run helpers can read it back from the install dir',
916 () =>
917 Effect.gen(function* () {
918 const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-release-tag-'));
919 const execPath = path.join(tempDir, 'composio');
920
921 yield* writeInstalledReleaseTag(tempDir, '@composio/cli@0.2.12');
922
923 expect(yield* readInstalledReleaseTag(execPath)).toBe('@composio/cli@0.2.12');
924 })
925 );
926
927 it.effect(
928 '[Given] a partial companion install [Then] it reports only the missing companion files',
929 () =>
930 Effect.gen(function* () {
931 const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-missing-'));
932 const execPath = path.join(tempDir, 'composio');
933 fs.writeFileSync(path.join(tempDir, RUN_COMPANION_MODULE_FILENAMES[0]!), '', 'utf8');
934
935 expect(yield* listMissingInstalledRunCompanionModules(execPath)).toEqual(
936 RUN_COMPANION_MODULE_FILENAMES.slice(1).slice().sort()
937 );
938 })
939 );
940
941 it.effect(
942 '[Given] a nested companion dependency is missing [Then] it reports the missing helper asset',
943 () =>
944 Effect.gen(function* () {
945 const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-missing-nested-'));
946 const execPath = path.join(tempDir, 'composio');
947 const servicesDir = path.join(tempDir, 'services');
948 fs.mkdirSync(servicesDir, { recursive: true });
949
950 fs.writeFileSync(
951 path.join(tempDir, 'run-helpers-runtime.mjs'),
952 'export * from "./services/run-helpers-runtime.mjs";\n',
953 'utf8'
954 );
955 fs.writeFileSync(
956 path.join(tempDir, 'generation-runtime.mjs'),
957 'export * from "./services/generation-runtime.mjs";\n',
958 'utf8'
959 );
960
961 fs.writeFileSync(
962 path.join(servicesDir, 'run-helpers-runtime.mjs'),
963 'export const runtimeValue = 1;\n',
964 'utf8'
965 );
966 fs.writeFileSync(
967 path.join(servicesDir, 'generation-runtime.mjs'),
968 'export * from "../run-companion-modules-abc123.mjs";\n',
969 'utf8'
970 );
971
972 expect(yield* listMissingInstalledRunCompanionModules(execPath)).toEqual([
973 'run-companion-modules-abc123.mjs',
974 ]);
975 })
976 );
977
978 it.effect(
979 '[Given] a named re-export dependency is missing [Then] it reports the missing helper asset',
980 () =>
981 Effect.gen(function* () {
982 const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-run-missing-reexport-'));
983 const execPath = path.join(tempDir, 'composio');
984 const servicesDir = path.join(tempDir, 'services');
985 fs.mkdirSync(servicesDir, { recursive: true });
986
987 fs.writeFileSync(
988 path.join(tempDir, 'run-helpers-runtime.mjs'),
989 'export * from "./services/run-helpers-runtime.mjs";\n',
990 'utf8'
991 );
992 fs.writeFileSync(
993 path.join(tempDir, 'generation-runtime.mjs'),
994 'export * from "./services/generation-runtime.mjs";\n',
995 'utf8'
996 );
997
998 fs.writeFileSync(
999 path.join(servicesDir, 'run-helpers-runtime.mjs'),
1000 'export const runtimeValue = 1;\n',
1001 'utf8'
1002 );
1003 fs.writeFileSync(
1004 path.join(servicesDir, 'generation-runtime.mjs'),
1005 'export { helperValue } from "../run-companion-modules-def456.mjs";\n',
1006 'utf8'
1007 );
1008
1009 expect(yield* listMissingInstalledRunCompanionModules(execPath)).toEqual([
1010 'run-companion-modules-def456.mjs',
1011 ]);
1012 })
1013 );
1014 });
1015 });
1016
1017 describe('extractInlineExecuteToolSlugs', () => {
1018 it('[Given] inline run source [Then] it finds static execute slugs from the AST', () => {
1019 expect(
1020 extractInlineExecuteToolSlugs(`
1021 const first = await execute("GMAIL_SEND_EMAIL", { to: "a@b.com" });
1022 const dynamic = await execute(slug, payload);
1023 execute('GITHUB_CREATE_ISSUE', { owner: 'acme' });
1024 execute("GMAIL_SEND_EMAIL", { to: "b@c.com" });
1025 `)
1026 ).toEqual(['GMAIL_SEND_EMAIL', 'GITHUB_CREATE_ISSUE']);
1027 });
1028 });
1029
1030 describe('wrapInlineCodeForRun', () => {
1031 it('[Given] inline code ending in an expression [Then] it rewrites the last expression to a return', () => {
1032 expect(
1033 wrapInlineCodeForRun(`
1034 const value = 1 + 1;
1035 value * 2;
1036 `)
1037 ).toContain('return (value * 2);');
1038 });
1039 });