@devries-cameron20 / composio Mirror · read-only / commits / 4ce75c924
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, 12:15 UTC

fix(cli): keep large executes working when the encoder companion fails to load

A large execute measured its output after the tool call had already succeeded, so a missing encoder companion whose repair download failed turned a successful call into a failed command. Fall back to a byte-based token estimate instead. Load companion modules with Effect.tryPromise so an unloadable file is a typed RunCompanionRepairError rather than a defect, and add the two companions as tsdown entries so the dist build can resolve them. The executable graph check listed @composio/core's root entry with a pattern that could never match Bun's relative module paths. The root entry is still bundled behind the file-upload dynamic import, so drop that entry and correct the constants comment.

jkomyno committed Sep 14, 2026 at 14:07 UTC 4ce75c9245e6245b0157c8574291287b23b9faa2
8 files changed +153 -13
ts/packages/cli/scripts/_shared.ts
-1
@@ -334,7 +334,6 @@ const EXECUTABLE_EXCLUDED_MODULE_PATTERNS: ReadonlyArray<{
334 }> = [
335 { pattern: /\/node_modules\/typescript\//, reason: 'the TypeScript compiler' },
336 { pattern: /\/node_modules\/js-tiktoken\//, reason: 'the tokenizer and its rank table' },
337 - { pattern: /\/packages\/core\/dist\/index\.mjs$/, reason: "@composio/core's root entry" },
337 { pattern: /(?:^|\/)src\/generation\/(?!errors\.ts$)/, reason: 'the generation pipeline' },
338 {
339 pattern: /(?:^|\/)src\/commands\/run-source-transforms\.ts$/,
ts/packages/cli/src/commands/tools/commands/tools.execute.cmd.ts
+21 -2
@@ -327,13 +327,32 @@ const EXECUTE_INLINE_OUTPUT_TOKEN_THRESHOLD = 10_000;
327 // next to the executable, loaded from disk on first use: its rank table is
328 // 2.3MB that no other command, and no small response, has any use for. A
329 // missing companion in a packaged install goes through the same self-repair as
330 -// `composio run`'s modules and fails the command the same way.
330 +// `composio run`'s modules.
331 const loadExecuteOutputEncoder = loadInstalledCompanionModule<
332 typeof import('src/services/execute-output-encoder-runtime')
333 >('execute-output-encoder-runtime');
334
335 +// JSON averages roughly four bytes per o200k token.
336 +const ESTIMATED_BYTES_PER_OUTPUT_TOKEN = 4;
337 +
338 +// The tool call has already succeeded by the time its output is measured, so an
339 +// encoder that cannot be loaded (a damaged install, or a repair download that
340 +// fails offline) must not fail the command. The count only chooses between
341 +// inline output and a stored file, so it falls back to an estimate from the
342 +// byte length.
343 const countOutputTokens = (json: string) =>
336 - Effect.map(loadExecuteOutputEncoder, encoder => encoder.countOutputTokens(json));
344 + loadExecuteOutputEncoder.pipe(
345 + Effect.map(encoder => encoder.countOutputTokens(json)),
346 + Effect.catch(error =>
347 + Effect.logDebug(
348 + `[execute] tokenizer unavailable, estimating the output token count: ${error.message}`
349 + ).pipe(
350 + Effect.as(
351 + Math.ceil(new TextEncoder().encode(json).length / ESTIMATED_BYTES_PER_OUTPUT_TOKEN)
352 + )
353 + )
354 + )
355 + );
356
357 // A BPE token always covers at least one UTF-8 byte, so a payload of at most
358 // THRESHOLD bytes can never exceed THRESHOLD tokens. Checking the byte length
ts/packages/cli/src/constants.ts
+4 -3
@@ -4,9 +4,10 @@ declare const __COMPOSIO_CLI_RELEASE_VERSION__: string | undefined;
4
5 // These four values mirror `@composio/core`'s `constants` module. They are
6 // spelled out here rather than imported because importing anything from the
7 -// core package's root entry evaluates the whole SDK at startup (its zod schemas
8 -// alone cost ~25ms) and pulls `openai` and `pusher-js` into the binary, for two
9 -// strings and two URLs. `test/src/constants.test.ts` pins them to core's values.
7 +// core package's root entry at module scope evaluates the whole SDK at startup
8 +// (its zod schemas alone cost ~25ms), for two strings and two URLs. The root
9 +// entry is still bundled, behind the file-upload path's dynamic import.
10 +// `test/src/constants.test.ts` pins these to core's values.
11
12 /** Default base URL for the Composio API server (backend). */
13 export const DEFAULT_BASE_URL = 'https://backend.composio.dev';
ts/packages/cli/src/services/execute-output-encoder-runtime.ts
+2 -2
@@ -25,7 +25,7 @@ const getExecuteOutputEncoder = () => {
25 // `Tiktoken.encode` defaults `disallowedSpecial` to "all", which makes it throw
26 // on any tool response that happens to contain the literal text `<|endoftext|>`
27 // or `<|endofprompt|>` (a README about tokenizers is enough). Here the encoder
28 -// is only a length gauge, so those literals are ordinary characters: passing
29 -// `allowedSpecial: 'all'` counts them instead of rejecting the payload.
28 +// is only a length gauge, so passing `allowedSpecial: 'all'` counts each literal
29 +// as the single special token it encodes to instead of rejecting the payload.
30 export const countOutputTokens = (json: string): number =>
31 getExecuteOutputEncoder().encode(json, 'all').length;
ts/packages/cli/src/services/run-companion-modules.ts
+11 -3
@@ -881,7 +881,15 @@ export const loadInstalledCompanionModule = <M>(
881 });
882 const moduleUrl = yield* Effect.orDie(path.toFileUrl(modulePath));
883
884 - // A rejected import here means the file resolved above is unloadable, which
885 - // is a broken install rather than a recoverable failure.
886 - return yield* Effect.promise(() => import(moduleUrl.href) as Promise<M>);
884 + // A rejected import means the file resolved above is missing or unloadable:
885 + // a broken install. It stays a typed failure so callers can report it, or
886 + // fall back, instead of crashing with a stack trace.
887 + return yield* Effect.tryPromise({
888 + try: () => import(moduleUrl.href) as Promise<M>,
889 + catch: cause =>
890 + new RunCompanionRepairError({
891 + message: `Unable to load the CLI's bundled support file ${path.basename(modulePath)}. Reinstall the CLI and try again.`,
892 + cause,
893 + }),
894 + });
895 });
ts/packages/cli/test/src/commands/tools/tools.execute.encoder-fallback.test.ts new
+99
@@ -0,0 +1,99 @@
1 +import * as fs from 'node:fs';
2 +import { describe, expect, layer } from '@effect/vitest';
3 +import { vi } from 'vitest';
4 +import { ConfigProvider, Effect } from 'effect';
5 +import { extendConfigProvider } from 'src/services/config';
6 +import { cli, TestLive, MockConsole } from 'test/__utils__';
7 +import { liveEnvConfigProvider } from 'test/__utils__/live-env-config-provider';
8 +
9 +// The encoder companion is missing: the loader fails the way a damaged install
10 +// whose repair download also failed does. Every other companion loads as usual.
11 +vi.mock('src/services/run-companion-modules', async importOriginal => {
12 + const actual = await importOriginal<typeof import('src/services/run-companion-modules')>();
13 + const { Effect } = await import('effect');
14 + return {
15 + ...actual,
16 + loadInstalledCompanionModule: (baseName: string) =>
17 + baseName === 'execute-output-encoder-runtime'
18 + ? Effect.fail(
19 + new actual.RunCompanionRepairError({
20 + message: "Unable to restore the CLI's bundled support files.",
21 + })
22 + )
23 + : actual.loadInstalledCompanionModule(baseName),
24 + };
25 +});
26 +
27 +vi.hoisted(() => {
28 + delete process.env.CI;
29 +});
30 +
31 +const testArtifactConfigProvider = liveEnvConfigProvider.pipe(
32 + ConfigProvider.mapInput(configPath =>
33 + configPath.map(segment =>
34 + segment === 'SESSION_DIR'
35 + ? 'CACHE_DIR'
36 + : typeof segment === 'string'
37 + ? `UNSET_${segment}`
38 + : segment
39 + )
40 + )
41 +);
42 +
43 +const largeOutputConfigProvider = ConfigProvider.fromEnvRecord({
44 + COMPOSIO_USER_API_KEY: 'test_api_key',
45 +}).pipe(ConfigProvider.orElse(testArtifactConfigProvider), extendConfigProvider);
46 +
47 +const parseLastJson = (lines: ReadonlyArray<string>): Record<string, unknown> => {
48 + for (let i = lines.length - 1; i >= 0; i -= 1) {
49 + const line = lines[i];
50 + if (!line) continue;
51 + try {
52 + return JSON.parse(line) as Record<string, unknown>;
53 + } catch {
54 + // keep searching for the last JSON line
55 + }
56 + }
57 + throw new Error('Expected JSON output but none found');
58 +};
59 +
60 +describe('CLI: composio execute without the encoder companion', () => {
61 + layer(
62 + TestLive({
63 + baseConfigProvider: largeOutputConfigProvider,
64 + fixture: 'global-test-user-id',
65 + stdin: { isTTY: true, data: '' },
66 + toolsExecutor: {
67 + respondWith: {
68 + data: { content: 'token '.repeat(20_000) },
69 + error: null,
70 + successful: true,
71 + logId: 'log_encoder_missing',
72 + },
73 + },
74 + })
75 + )('[Given] a large response [Then] it estimates the token count instead of failing', it => {
76 + it.effect('stores the payload with a byte-based token estimate', () =>
77 + Effect.gen(function* () {
78 + yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '{"recipient":"a"}']);
79 + const lines = yield* MockConsole.getLines({ stripAnsi: true });
80 + const output = parseLastJson(lines) as {
81 + successful: boolean;
82 + storedInFile: boolean;
83 + tokenCount: number;
84 + outputFilePath: string;
85 + };
86 +
87 + expect(output.successful).toBe(true);
88 + expect(output.storedInFile).toBe(true);
89 + const storedJson = fs.readFileSync(output.outputFilePath, 'utf8');
90 + expect(output.tokenCount).toBe(Math.ceil(Buffer.byteLength(storedJson, 'utf8') / 4));
91 +
92 + fs.rmSync(output.outputFilePath.slice(0, output.outputFilePath.lastIndexOf('/')), {
93 + recursive: true,
94 + force: true,
95 + });
96 + })
97 + );
98 + });
99 +});
ts/packages/cli/test/src/services/run-companion-modules.test.ts
+14 -2
@@ -475,8 +475,20 @@ describe('loadInstalledCompanionModule', () => {
475 >('execute-output-encoder-runtime');
476
477 expect(encoder.countOutputTokens('hello world')).toBe(2);
478 - // Special-token literals are counted as text rather than rejected.
479 - expect(encoder.countOutputTokens('<|endoftext|>')).toBeGreaterThan(0);
478 + // A special-token literal counts as its one special token rather than
479 + // being rejected.
480 + expect(encoder.countOutputTokens('<|endoftext|>')).toBe(1);
481 + })
482 + );
483 +
484 + it.effect('[Given] a module that cannot be loaded [Then] it fails with a typed error', () =>
485 + Effect.gen(function* () {
486 + const error = yield* loadInstalledCompanionModule('missing-companion-module').pipe(
487 + Effect.flip
488 + );
489 +
490 + expect(error._tag).toBe('services/RunCompanionRepairError');
491 + expect(error.message).toContain('missing-companion-module.mjs');
492 })
493 );
494
ts/packages/cli/tsdown.config.ts
+2
@@ -9,6 +9,8 @@ export default defineConfig({
9 'src/services/run-subagent-acp.ts',
10 'src/services/run-subagent-legacy.ts',
11 'src/services/run-subagent-output-mcp.ts',
12 + 'src/services/generation-runtime.ts',
13 + 'src/services/execute-output-encoder-runtime.ts',
14 ],
15 format: ['esm'],
16 shims: true,