@devries-cameron20 / composio Mirror · read-only / commits / 19f7f2a00
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): scope companion repair and keep unmeasured large output stored

- Import an in-process companion before checking the rest of the set, so a missing unrelated companion cannot fail generate or run through an offline repair. Repair runs only when the requested module fails to load. - Check each companion's required exports, so a file left by another release fails with a typed reinstall error instead of calling a missing export. - When the tokenizer cannot load, store any response past the byte pre-filter. The four-bytes-per-token estimate can undercount, so it no longer keeps a response inline. - Bundle the executable graph check with the release build's env inlining, defines, NODE_ENV and syntax minification. Claude-Session: https://claude.ai/code/session_01MTb47vexN35pLGsSZwBmrJ

jkomyno committed Sep 14, 2026 at 17:46 UTC 19f7f2a00228270c0971e37fccea96ca9cbb67ba
6 files changed +168 -47
ts/packages/cli/scripts/_shared.ts
+26 -1
@@ -9,6 +9,7 @@ import {
9 RUN_COMPANION_MODULE_BASENAMES,
10 type RunCodexAcpBinaryTarget,
11 } from '../src/services/run-companion-modules';
12 +import { buildCliReleaseVersionDefineArgs } from '../src/utils/cli-release-version';
13 import { materializeAcpAdaptersCache } from './_acp-adapters';
14
15 export { teardown } from './_teardown';
@@ -346,6 +347,16 @@ const EXECUTABLE_EXCLUDED_MODULE_PATTERNS: ReadonlyArray<{
347 },
348 ];
349
350 +// `--define` pairs as `Bun.build` takes them.
351 +const defineRecordFromArgs = (args: ReadonlyArray<string>): Record<string, string> =>
352 + Object.fromEntries(
353 + args.flatMap((arg, index) => {
354 + const pair = args[index - 1] === '--define' ? arg : undefined;
355 + const separator = pair?.indexOf('=') ?? -1;
356 + return pair && separator > 0 ? [[pair.slice(0, separator), pair.slice(separator + 1)]] : [];
357 + })
358 + );
359 +
360 // Bun's unminified output starts each module with a `// <path>` line, which is
361 // the only bundle-level record of what made it into the graph. A dynamic
362 // `import()` of a *literal* specifier is still bundled (lazily evaluated, but
@@ -357,8 +368,22 @@ const assertExecutableExcludesCompanionModules = async (): Promise<void> => {
368 target: 'bun',
369 format: 'esm',
370 packages: 'bundle',
360 - minify: false,
371 sourcemap: 'none',
372 + // The same environment inlining and defines as `build-binary.ts` and
373 + // `build-binary-cross.ts`, plus the NODE_ENV that `--production` sets, so a
374 + // branch the release build folds away is folded away here too.
375 + env: 'DEBUG_OVERRIDE_*',
376 + define: {
377 + 'process.env.NODE_ENV': JSON.stringify('production'),
378 + ...defineRecordFromArgs([
379 + ...posthogBakeArgs(),
380 + ...buildCliReleaseVersionDefineArgs(process.env.RELEASE_TAG),
381 + ]),
382 + },
383 + // `--production` also minifies. Syntax minification is the part that drops
384 + // unreachable code; whitespace minification would strip the module
385 + // comments this check reads, so it stays off.
386 + minify: { syntax: true, whitespace: false, identifiers: false },
387 });
388 if (!result.success) {
389 throw new Error(
ts/packages/cli/src/commands/tools/commands/tools.execute.cmd.ts
+14 -10
@@ -330,26 +330,30 @@ const EXECUTE_INLINE_OUTPUT_TOKEN_THRESHOLD = 10_000;
330 // `composio run`'s modules.
331 const loadExecuteOutputEncoder = loadInstalledCompanionModule<
332 typeof import('src/services/execute-output-encoder-runtime')
333 ->('execute-output-encoder-runtime');
333 +>('execute-output-encoder-runtime', ['countOutputTokens']);
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.
340 +// fails offline) must not fail the command, so it falls back to an estimate from
341 +// the byte length. The estimate is not exact: a response averaging fewer bytes
342 +// per token can exceed the threshold while its estimate does not, so only an
343 +// exact count keeps a response that passed the byte pre-filter inline.
344 const countOutputTokens = (json: string) =>
345 loadExecuteOutputEncoder.pipe(
345 - Effect.map(encoder => encoder.countOutputTokens(json)),
346 + Effect.map(encoder => ({ tokenCount: encoder.countOutputTokens(json), exact: true })),
347 Effect.catch(error =>
348 Effect.logDebug(
349 `[execute] tokenizer unavailable, estimating the output token count: ${error.message}`
350 ).pipe(
350 - Effect.as(
351 - Math.ceil(new TextEncoder().encode(json).length / ESTIMATED_BYTES_PER_OUTPUT_TOKEN)
352 - )
351 + Effect.as({
352 + tokenCount: Math.ceil(
353 + new TextEncoder().encode(json).length / ESTIMATED_BYTES_PER_OUTPUT_TOKEN
354 + ),
355 + exact: false,
356 + })
357 )
358 )
359 );
@@ -452,8 +456,8 @@ const prepareExecuteOutput = (
456 } satisfies PreparedExecuteOutput;
457 }
458
455 - const tokenCount = yield* countOutputTokens(json);
456 - if (tokenCount <= EXECUTE_INLINE_OUTPUT_TOKEN_THRESHOLD) {
459 + const { tokenCount, exact } = yield* countOutputTokens(json);
460 + if (exact && tokenCount <= EXECUTE_INLINE_OUTPUT_TOKEN_THRESHOLD) {
461 return {
462 kind: 'inline',
463 json,
ts/packages/cli/src/effects/generation-runtime.ts
+13 -2
@@ -12,8 +12,19 @@ export type GenerationRuntime = typeof import('src/services/generation-runtime')
12 * pipeline, `composio run` source rewrites), loaded on demand. See
13 * `src/services/generation-runtime.ts` for why its API is Effect-free.
14 */
15 -export const loadGenerationRuntime =
16 - loadInstalledCompanionModule<GenerationRuntime>('generation-runtime');
15 +export const loadGenerationRuntime = loadInstalledCompanionModule<GenerationRuntime>(
16 + 'generation-runtime',
17 + [
18 + 'BANNER',
19 + 'createToolkitIndex',
20 + 'extractInlineExecuteToolSlugs',
21 + 'wrapFileSourceForRun',
22 + 'wrapInlineCodeForRun',
23 + 'generateTypeScriptSourceFiles',
24 + 'transpileTypeScriptSourceFiles',
25 + 'generatePythonSourceFiles',
26 + ]
27 +);
28
29 /**
30 * Lifts a companion-module outcome into the CLI's runtime: successes succeed,
ts/packages/cli/src/services/run-companion-modules.ts
+50 -26
@@ -845,9 +845,15 @@ export const resolveRunCompanionModulePath = ({
845 *
846 * Resolution is the same as for the `run` companions: the `.ts` source next to
847 * this file when running from a checkout (tests, `bun run src/bin.ts`), the
848 - * `.mjs` bundle next to the executable in a packaged install. A packaged install
849 - * that lost the file goes through the same release-archive repair `composio run`
850 - * uses before failing.
848 + * `.mjs` bundle next to the executable in a packaged install.
849 + *
850 + * The module is imported before anything else is checked, so a companion that
851 + * loads is used even when an unrelated one is missing and the repair download
852 + * would fail offline. Only a module that fails to load, or that lacks one of
853 + * `requiredExports` (a file left behind by a different release), sends a
854 + * packaged install through the release-archive repair `composio run` uses. If
855 + * that repair restored missing files the module is imported once more;
856 + * otherwise the load error stands.
857 *
858 * The specifier is computed at runtime on purpose: a literal `import('./x')`
859 * would make the bundler fold the module back into the executable, which is the
@@ -857,7 +863,8 @@ export const resolveRunCompanionModulePath = ({
863 * is type-only and leaves no import behind.
864 */
865 export const loadInstalledCompanionModule = <M>(
860 - baseName: string
866 + baseName: string,
867 + requiredExports: ReadonlyArray<keyof M & string>
868 ): Effect.Effect<
869 M,
870 RunCompanionRepairError | PlatformError.PlatformError,
@@ -868,28 +875,45 @@ export const loadInstalledCompanionModule = <M>(
875 const callerImportMetaUrl = import.meta.url;
876 const execPath = process.execPath;
877
871 - yield* repairMissingInstalledRunCompanionModules({
872 - callerImportMetaUrl,
873 - execPath,
874 - appVersion: APP_VERSION,
875 - });
878 + const importModule = Effect.gen(function* () {
879 + const modulePath = yield* resolveRunCompanionModulePath({
880 + callerImportMetaUrl,
881 + execPath,
882 + relativeNoExtensionFromCaller: `./${baseName}`,
883 + });
884 + const fileName = path.basename(modulePath);
885 + const moduleUrl = yield* Effect.orDie(path.toFileUrl(modulePath));
886 +
887 + // A rejected import means the file resolved above is missing or unloadable:
888 + // a broken install. It stays a typed failure so callers can report it, or
889 + // fall back, instead of crashing with a stack trace.
890 + const exports = yield* Effect.tryPromise({
891 + try: () => import(moduleUrl.href) as Promise<Record<string, unknown>>,
892 + catch: cause =>
893 + new RunCompanionRepairError({
894 + message: `Unable to load the CLI's bundled support file ${fileName}. Reinstall the CLI and try again.`,
895 + cause,
896 + }),
897 + });
898
877 - const modulePath = yield* resolveRunCompanionModulePath({
878 - callerImportMetaUrl,
879 - execPath,
880 - relativeNoExtensionFromCaller: `./${baseName}`,
881 - });
882 - const moduleUrl = yield* Effect.orDie(path.toFileUrl(modulePath));
883 -
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 - }),
899 + const missingExports = requiredExports.filter(name => !(name in exports));
900 + if (missingExports.length > 0) {
901 + return yield* new RunCompanionRepairError({
902 + message: `The CLI's bundled support file ${fileName} does not match this CLI (missing ${missingExports.join(', ')}). Reinstall the CLI and try again.`,
903 + });
904 + }
905 + return exports as M;
906 });
907 +
908 + return yield* importModule.pipe(
909 + Effect.catchTag('services/RunCompanionRepairError', loadError =>
910 + repairMissingInstalledRunCompanionModules({
911 + callerImportMetaUrl,
912 + execPath,
913 + appVersion: APP_VERSION,
914 + }).pipe(
915 + Effect.flatMap(({ repaired }) => (repaired ? importModule : Effect.fail(loadError)))
916 + )
917 + )
918 + );
919 });
ts/packages/cli/test/src/commands/tools/tools.execute.encoder-fallback.test.ts
+43 -2
@@ -13,14 +13,14 @@ vi.mock('src/services/run-companion-modules', async importOriginal => {
13 const { Effect } = await import('effect');
14 return {
15 ...actual,
16 - loadInstalledCompanionModule: (baseName: string) =>
16 + loadInstalledCompanionModule: (baseName: string, requiredExports: ReadonlyArray<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),
23 + : actual.loadInstalledCompanionModule<Record<string, unknown>>(baseName, requiredExports),
24 };
25 });
26
@@ -96,4 +96,45 @@ describe('CLI: composio execute without the encoder companion', () => {
96 })
97 );
98 });
99 +
100 + layer(
101 + TestLive({
102 + baseConfigProvider: largeOutputConfigProvider,
103 + fixture: 'global-test-user-id',
104 + stdin: { isTTY: true, data: '' },
105 + toolsExecutor: {
106 + respondWith: {
107 + data: { content: 'token '.repeat(2_000) },
108 + error: null,
109 + successful: true,
110 + logId: 'log_encoder_missing_estimate_below_threshold',
111 + },
112 + },
113 + })
114 + )(
115 + '[Given] a response past the byte pre-filter whose estimate is below the threshold [Then] it is still stored',
116 + it => {
117 + it.effect('does not print an unmeasured response inline', () =>
118 + Effect.gen(function* () {
119 + yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '{"recipient":"a"}']);
120 + const lines = yield* MockConsole.getLines({ stripAnsi: true });
121 + const output = parseLastJson(lines) as {
122 + successful: boolean;
123 + storedInFile: boolean;
124 + tokenCount: number;
125 + outputFilePath: string;
126 + };
127 +
128 + expect(output.successful).toBe(true);
129 + expect(output.storedInFile).toBe(true);
130 + expect(output.tokenCount).toBeLessThan(10_000);
131 +
132 + fs.rmSync(output.outputFilePath.slice(0, output.outputFilePath.lastIndexOf('/')), {
133 + recursive: true,
134 + force: true,
135 + });
136 + })
137 + );
138 + }
139 + );
140 });
ts/packages/cli/test/src/services/run-companion-modules.test.ts
+22 -6
@@ -472,7 +472,7 @@ describe('loadInstalledCompanionModule', () => {
472 Effect.gen(function* () {
473 const encoder = yield* loadInstalledCompanionModule<
474 typeof import('src/services/execute-output-encoder-runtime')
475 - >('execute-output-encoder-runtime');
475 + >('execute-output-encoder-runtime', ['countOutputTokens']);
476
477 expect(encoder.countOutputTokens('hello world')).toBe(2);
478 // A special-token literal counts as its one special token rather than
@@ -483,7 +483,7 @@ describe('loadInstalledCompanionModule', () => {
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(
486 + const error = yield* loadInstalledCompanionModule('missing-companion-module', []).pipe(
487 Effect.flip
488 );
489
@@ -492,12 +492,28 @@ describe('loadInstalledCompanionModule', () => {
492 })
493 );
494
495 + it.effect(
496 + '[Given] a module from another release [Then] it fails with a typed error naming the missing export',
497 + () =>
498 + Effect.gen(function* () {
499 + const error = yield* loadInstalledCompanionModule<{
500 + readonly countOutputTokens: unknown;
501 + readonly retiredExport: unknown;
502 + }>('execute-output-encoder-runtime', ['countOutputTokens', 'retiredExport']).pipe(
503 + Effect.flip
504 + );
505 +
506 + expect(error._tag).toBe('services/RunCompanionRepairError');
507 + expect(error.message).toContain('execute-output-encoder-runtime');
508 + expect(error.message).toContain('missing retiredExport');
509 + })
510 + );
511 +
512 it.effect('[Given] the generation companion [Then] its API is promise-shaped', () =>
513 Effect.gen(function* () {
497 - const generation =
498 - yield* loadInstalledCompanionModule<typeof import('src/services/generation-runtime')>(
499 - 'generation-runtime'
500 - );
514 + const generation = yield* loadInstalledCompanionModule<
515 + typeof import('src/services/generation-runtime')
516 + >('generation-runtime', ['wrapInlineCodeForRun', 'generatePythonSourceFiles']);
517
518 expect(generation.wrapInlineCodeForRun('1 + 1')).toBe('return (1 + 1);');
519 const outcome = yield* Effect.promise(() =>