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

perf(cli): store large execute output by byte size, not tokens

`composio execute` stored a response in a session file once js-tiktoken counted more than 10,000 o200k tokens. Nothing reads that count as a number, and o200k is not the tokenizer of the model reading the output, so the exact count only moved the cutoff while costing the 2.3MB rank table, ~330ms after large tool calls, and a separately shipped companion file with its own repair path. Responses over 40,000 UTF-8 bytes are stored now. The summary keeps `tokenCount`, now estimated at four bytes per token, and gains `sizeBytes`. The execute-output-encoder-runtime companion, js-tiktoken, and their build, packaging and test entries are removed. The uninstall lists keep the companion's file name so installs that shipped it can still remove it.

jkomyno committed Sep 14, 2026 at 20:25 UTC fc5ecdce99806a6b633c78597d2f4315b0c7cc70
13 files changed +71 -343
pnpm-lock.yaml
-3
@@ -1230,9 +1230,6 @@ importers:
1230 indent-string:
1231 specifier: ^5.0.0
1232 version: 5.0.0
1233 - js-tiktoken:
1234 - specifier: ^1.0.21
1235 - version: 1.0.21
1233 json5:
1234 specifier: ^2.2.3
1235 version: 2.2.3
ts/e2e-tests/cli/upgrade/e2e.test.ts
-1
@@ -16,7 +16,6 @@ const companionRelativePaths = [
16 'run-subagent-legacy.mjs',
17 'run-subagent-output-mcp.mjs',
18 'generation-runtime.mjs',
19 - 'execute-output-encoder-runtime.mjs',
19 // RUN_COMPANION_ALL_STATIC_ASSET_RELATIVE_PATHS
20 'acp-adapters/claude-code-acp.mjs',
21 'acp-adapters/cli.js',
ts/packages/cli/AGENTS.md
+1 -1
@@ -97,7 +97,7 @@ Pipeline for `composio generate {ts,py}`:
97
98 `--type-tools` includes full type definitions.
99
100 -Steps 3–4 (and the TypeScript compiler they need) ship as the `generation-runtime` companion module — `dist/generation-runtime.mjs` next to the executable, not inside it — and the tokenizer behind `composio execute`'s inline-vs-file decision ships the same way as `execute-output-encoder-runtime`. Commands reach them through `loadInstalledCompanionModule` (`src/services/run-companion-modules.ts`), never by importing `src/generation/*`, `src/commands/run-source-transforms.ts`, `typescript`, or `js-tiktoken` directly: the binary build fails if the executable's bundle reaches any of those (`assertExecutableExcludesCompanionModules` in `scripts/_shared.ts`). A companion bundles its own copy of `effect`, so its API is plain functions and promises; failures cross as values that `src/generation/errors.ts` turns back into the CLI's error classes.
100 +Steps 3–4 (and the TypeScript compiler they need) ship as the `generation-runtime` companion module — `dist/generation-runtime.mjs` next to the executable, not inside it. Commands reach it through `loadInstalledCompanionModule` (`src/services/run-companion-modules.ts`), never by importing `src/generation/*`, `src/commands/run-source-transforms.ts`, or `typescript` directly: the binary build fails if the executable's bundle reaches any of those (`assertExecutableExcludesCompanionModules` in `scripts/_shared.ts`). A companion bundles its own copy of `effect`, so its API is plain functions and promises; failures cross as values that `src/generation/errors.ts` turns back into the CLI's error classes.
101
102 ### Configuration
103
ts/packages/cli/package.json
-1
@@ -81,7 +81,6 @@
81 "extract-zip": "^2.0.1",
82 "fast-string-width": "^3.0.2",
83 "indent-string": "^5.0.0",
84 - "js-tiktoken": "^1.0.21",
84 "json5": "^2.2.3",
85 "open": "^11.0.2",
86 "openapi-typescript": "^7.13.0",
ts/packages/cli/scripts/_shared.ts
+2 -7
@@ -327,24 +327,19 @@ const buildCompanionServiceBundles = async (outputDir: string): Promise<void> =>
327 // Module paths that must only ever be reached through a companion module. The
328 // executable's bundle is checked after every build because the guard against
329 // regressions is otherwise invisible: a stray static import of any of these
330 -// would silently put the TypeScript compiler or the tokenizer rank table back
331 -// into the executable, and `--version` would quietly get ~70ms slower.
330 +// would silently put the TypeScript compiler back into the executable, and
331 +// `--version` would quietly get ~70ms slower.
332 const EXECUTABLE_EXCLUDED_MODULE_PATTERNS: ReadonlyArray<{
333 readonly pattern: RegExp;
334 readonly reason: string;
335 }> = [
336 { pattern: /\/node_modules\/typescript\//, reason: 'the TypeScript compiler' },
337 - { pattern: /\/node_modules\/js-tiktoken\//, reason: 'the tokenizer and its rank table' },
337 { pattern: /(?:^|\/)src\/generation\/(?!errors\.ts$)/, reason: 'the generation pipeline' },
338 {
339 pattern: /(?:^|\/)src\/commands\/run-source-transforms\.ts$/,
340 reason: 'the run source rewrites',
341 },
342 { pattern: /(?:^|\/)src\/services\/generation-runtime\.ts$/, reason: 'the generation companion' },
344 - {
345 - pattern: /(?:^|\/)src\/services\/execute-output-encoder-runtime\.ts$/,
346 - reason: 'the encoder companion',
347 - },
343 ];
344
345 // `--define` pairs as `Bun.build` takes them.
ts/packages/cli/src/commands/tools/commands/tools.execute.cmd.ts
+28 -58
@@ -18,7 +18,6 @@ import {
18 } from 'src/services/tool-input-validation';
19 import { TerminalUI } from 'src/services/terminal-ui';
20 import { logToolDebug, makePerfDebugLogger } from 'src/services/runtime-debug-logger';
21 -import { loadInstalledCompanionModule } from 'src/services/run-companion-modules';
21 import {
22 LocalToolsDisabledError,
23 ToolsExecutor,
@@ -321,50 +320,23 @@ const redactRequestId = (value: object): object => {
320 };
321 };
322
324 -const EXECUTE_INLINE_OUTPUT_TOKEN_THRESHOLD = 10_000;
323 +// Responses larger than this are stored in a session file instead of printed,
324 +// so a caller such as an agent does not take a huge payload into its context.
325 +// Measured in UTF-8 bytes: about 10,000 tokens of JSON at the estimate below.
326 +const EXECUTE_INLINE_OUTPUT_BYTE_THRESHOLD = 40_000;
327
326 -// The tokenizer lives in the `execute-output-encoder-runtime` companion module
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.
331 -const loadExecuteOutputEncoder = loadInstalledCompanionModule<
332 - typeof import('src/services/execute-output-encoder-runtime')
333 ->('execute-output-encoder-runtime', ['countOutputTokens']);
334 -
335 -// JSON averages roughly four bytes per o200k token.
328 +// JSON averages roughly four bytes per token. The `tokenCount` of a stored
329 +// response is this estimate rather than a tokenizer count, since the real count
330 +// depends on the model that reads the file.
331 const ESTIMATED_BYTES_PER_OUTPUT_TOKEN = 4;
332
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, 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(
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(
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 - );
360 -
361 -// A BPE token always covers at least one UTF-8 byte, so a payload of at most
362 -// THRESHOLD bytes can never exceed THRESHOLD tokens. Checking the byte length
363 -// first keeps the common (small) response off the tokenizer entirely: building
364 -// the o200k rank table measured ~390ms in a compiled binary, against ~4ms to
365 -// encode a 7.5KB payload once it exists, and microseconds to measure the bytes.
366 -const mayExceedInlineOutputThreshold = (json: string): boolean =>
367 - new TextEncoder().encode(json).length > EXECUTE_INLINE_OUTPUT_TOKEN_THRESHOLD;
333 +const describeStoredOutputSize = ({
334 + sizeBytes,
335 + tokenCount,
336 +}: {
337 + readonly sizeBytes: number;
338 + readonly tokenCount: number;
339 +}): string => `${Math.ceil(sizeBytes / 1024)} KB, ~${tokenCount} tokens`;
340
341 const shouldStoreLargeExecuteOutput = APP_CONFIG.CLI_INVOCATION_ORIGIN.pipe(
342 Effect.orDie,
@@ -377,6 +349,7 @@ type StoredExecuteOutputSummary = {
349 readonly logId: string;
350 readonly storedInFile: true;
351 readonly tokenCount: number;
352 + readonly sizeBytes: number;
353 readonly outputFilePath: string;
354 };
355
@@ -416,7 +389,7 @@ const executionSuccessSuffix = (result: {
389 const persistLargeExecuteOutput = (
390 toolSlug: string,
391 json: string,
419 - tokenCount: number,
392 + sizeBytes: number,
393 sharedDirectory?: string
394 ) =>
395 Effect.gen(function* () {
@@ -433,7 +406,8 @@ const persistLargeExecuteOutput = (
406 error: null,
407 logId: '',
408 storedInFile: true,
436 - tokenCount,
409 + tokenCount: Math.ceil(sizeBytes / ESTIMATED_BYTES_PER_OUTPUT_TOKEN),
410 + sizeBytes,
411 outputFilePath: outputFilePath ?? '(could not write to disk)',
412 } satisfies StoredExecuteOutputSummary;
413 });
@@ -447,17 +421,12 @@ const prepareExecuteOutput = (
421 ) =>
422 Effect.gen(function* () {
423 const json = serializeExecuteOutput(result);
450 - // `composio run` always prints inline, so its origin is checked before the
451 - // tokenizer is built: the count would be thrown away.
452 - if (!mayExceedInlineOutputThreshold(json) || !(yield* shouldStoreLargeExecuteOutput)) {
453 - return {
454 - kind: 'inline',
455 - json,
456 - } satisfies PreparedExecuteOutput;
457 - }
458 -
459 - const { tokenCount, exact } = yield* countOutputTokens(json);
460 - if (exact && tokenCount <= EXECUTE_INLINE_OUTPUT_TOKEN_THRESHOLD) {
424 + const sizeBytes = new TextEncoder().encode(json).length;
425 + // `composio run` always prints inline.
426 + if (
427 + sizeBytes <= EXECUTE_INLINE_OUTPUT_BYTE_THRESHOLD ||
428 + !(yield* shouldStoreLargeExecuteOutput)
429 + ) {
430 return {
431 kind: 'inline',
432 json,
@@ -467,7 +436,7 @@ const prepareExecuteOutput = (
436 return {
437 kind: 'file',
438 summary: {
470 - ...(yield* persistLargeExecuteOutput(toolSlug, json, tokenCount, sharedDirectory)),
439 + ...(yield* persistLargeExecuteOutput(toolSlug, json, sizeBytes, sharedDirectory)),
440 logId: result.logId,
441 } satisfies StoredExecuteOutputSummary,
442 } satisfies PreparedExecuteOutput;
@@ -1520,7 +1489,7 @@ const runExecuteWithSpinner = (params: {
1489 const output = yield* prepareExecuteOutput(params.slug, result, params.executeOutputDir);
1490 if (output.kind === 'file') {
1491 yield* params.ui.log.message(
1523 - `Response stored in ${output.summary.outputFilePath} (${output.summary.tokenCount} tokens)`
1492 + `Response stored in ${output.summary.outputFilePath} (${describeStoredOutputSize(output.summary)})`
1493 );
1494 yield* writeExecuteStdout(params.ui, JSON.stringify(output.summary, ciRedactReplacer, 2));
1495 yield* appendCliSessionHistory({
@@ -1533,6 +1502,7 @@ const runExecuteWithSpinner = (params: {
1502 storedInFile: true,
1503 outputFilePath: output.summary.outputFilePath,
1504 tokenCount: output.summary.tokenCount,
1505 + sizeBytes: output.summary.sizeBytes,
1506 logId: result.logId,
1507 },
1508 }).pipe(Effect.catch(() => Effect.void));
@@ -2105,7 +2075,7 @@ const runParallelToolsExecuteFromParsed = (params: ParsedParallelExecuteArgs) =>
2075
2076 if ('storedInFile' in result && result.storedInFile) {
2077 yield* ui.log.step(
2108 - `[${result.slug}] Response stored in ${result.outputFilePath} (${result.tokenCount} tokens)`
2078 + `[${result.slug}] Response stored in ${result.outputFilePath} (${describeStoredOutputSize(result)})`
2079 );
2080 continue;
2081 }
ts/packages/cli/src/services/execute-output-encoder-runtime.ts deleted
-31
@@ -1,31 +0,0 @@
1 -// Entry point of the `execute-output-encoder-runtime` companion module: the
2 -// tokenizer `composio execute` uses to decide whether a large response is
3 -// printed inline or stored to a file, bundled into
4 -// `dist/execute-output-encoder-runtime.mjs` next to the executable. The o200k
5 -// rank table is a 2.3MB string literal that would otherwise sit in the
6 -// executable for every command, while only responses past the inline threshold
7 -// ever need it. See `RUN_COMPANION_MODULE_BASENAMES`.
8 -//
9 -// The CLI loads this file with
10 -// `loadInstalledCompanionModule('execute-output-encoder-runtime')`. It exposes
11 -// plain functions on purpose; see `generation-runtime.ts` for why nothing
12 -// Effect-shaped crosses a companion boundary.
13 -import { Tiktoken } from 'js-tiktoken/lite';
14 -import o200kBase from 'js-tiktoken/ranks/o200k_base';
15 -
16 -let executeOutputEncoder: Tiktoken | undefined;
17 -
18 -const getExecuteOutputEncoder = () => {
19 - if (!executeOutputEncoder) {
20 - executeOutputEncoder = new Tiktoken(o200kBase);
21 - }
22 - return executeOutputEncoder;
23 -};
24 -
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 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
+5 -7
@@ -16,12 +16,11 @@ import { CLI_RELEASE_TAG_PREFIX } from 'src/utils/cli-release-version';
16
17 // Modules the binary build bundles separately (`dist/<name>.mjs` next to the
18 // executable) instead of into the executable itself. The first five are what
19 -// `composio run` preloads into the script it spawns. The last two are loaded
20 -// into the CLI's own process, on demand, through `loadInstalledCompanionModule`:
21 -// they carry the TypeScript compiler and the tokenizer rank table, which
22 -// together were ~70% of the executable's JavaScript and cost every command
23 -// parse time even though only `generate`, `run`, and large `execute` responses
24 -// ever reach them.
19 +// `composio run` preloads into the script it spawns. The last one is loaded into
20 +// the CLI's own process, on demand, through `loadInstalledCompanionModule`: it
21 +// carries the TypeScript compiler, which was 44% of the executable's JavaScript
22 +// and cost every command parse time even though only `generate` and `run`
23 +// reach it.
24 export const RUN_COMPANION_MODULE_BASENAMES: ReadonlyArray<string> = [
25 'run-helpers-runtime',
26 'run-subagent-shared',
@@ -29,7 +28,6 @@ export const RUN_COMPANION_MODULE_BASENAMES: ReadonlyArray<string> = [
28 'run-subagent-legacy',
29 'run-subagent-output-mcp',
30 'generation-runtime',
32 - 'execute-output-encoder-runtime',
31 ];
32
33 export const RUN_COMPANION_MODULE_FILENAMES = RUN_COMPANION_MODULE_BASENAMES.map(
ts/packages/cli/test/src/commands/startup-imports.test.ts
+3 -6
@@ -3,10 +3,9 @@ import { describe, expect, it } from 'vitest';
3
4 /**
5 * Modules that must never be evaluated by merely building the command tree.
6 - * The TypeScript compiler, the generation pipeline and the tokenizer ship as
7 - * companion modules that `composio run`, `composio generate` and large
8 - * `composio execute` responses load on demand, the same list the binary build
9 - * guard in `scripts/_shared.ts` checks. A static import anywhere on the
6 + * The TypeScript compiler and the generation pipeline ship as a companion
7 + * module that `composio run` and `composio generate` load on demand, the same
8 + * list the binary build guard in `scripts/_shared.ts` checks. A static import anywhere on the
9 * startup path silently puts that cost back on every invocation, so this test
10 * loads the command tree in a fresh Bun process and inspects what it pulled in.
11 */
@@ -16,9 +15,7 @@ const FORBIDDEN_AT_STARTUP: ReadonlyArray<string> = [
15 '/packages/ts-builders/',
16 '/src/generation/',
17 '/src/commands/run-source-transforms',
19 - '/node_modules/js-tiktoken/',
18 '/src/services/generation-runtime',
21 - '/src/services/execute-output-encoder-runtime',
19 ];
20
21 /**
ts/packages/cli/test/src/commands/tools/tools.execute.cmd.test.ts
+22 -72
@@ -1128,13 +1128,15 @@ describe('CLI: composio execute', () => {
1128 logId: string;
1129 storedInFile: boolean;
1130 tokenCount: number;
1131 + sizeBytes: number;
1132 outputFilePath: string;
1133 };
1134
1135 expect(output.successful).toBe(true);
1136 expect(output.storedInFile).toBe(true);
1137 expect(output.logId).toBe('log_large_output');
1137 - expect(output.tokenCount).toBeGreaterThan(10_000);
1138 + expect(output.sizeBytes).toBeGreaterThan(40_000);
1139 + expect(output.tokenCount).toBe(Math.ceil(output.sizeBytes / 4));
1140 // Session artifacts fall back to COMPOSIO_CACHE_DIR, which the shared
1141 // vitest setup pins to a per-test temp directory.
1142 const cacheDir = yield* Config.string('COMPOSIO_CACHE_DIR').parse(ConfigProvider.fromEnv());
@@ -1143,6 +1145,7 @@ describe('CLI: composio execute', () => {
1145 expect(fs.existsSync(output.outputFilePath)).toBe(true);
1146 const storedJson = fs.readFileSync(output.outputFilePath, 'utf8');
1147 expect(storedJson).toContain('token token token');
1148 + expect(Buffer.byteLength(storedJson, 'utf8')).toBe(output.sizeBytes);
1149
1150 fs.rmSync(output.outputFilePath.slice(0, output.outputFilePath.lastIndexOf('/')), {
1151 recursive: true,
@@ -1160,9 +1163,8 @@ describe('CLI: composio execute', () => {
1163 toolsExecutor: {
1164 respondWith: {
1165 data: {
1163 - // ~18KB that o200k encodes in ~4k tokens: past the byte pre-filter,
1164 - // under the token threshold.
1165 - content: 'composio '.repeat(2_000),
1166 + // ~36KB: under the 40KB threshold once serialized.
1167 + content: 'composio '.repeat(4_000),
1168 },
1169 error: null,
1170 successful: true,
@@ -1170,75 +1172,23 @@ describe('CLI: composio execute', () => {
1172 },
1173 },
1174 })
1173 - )(
1174 - '[Given] a response over 10KB that stays under the token threshold [Then] it prints inline',
1175 - it => {
1176 - it.effect('does not store the payload in a file', () =>
1177 - Effect.gen(function* () {
1178 - yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '{"recipient":"a"}']);
1179 - const lines = yield* MockConsole.getLines({ stripAnsi: true });
1180 - const output = parseLastJson(lines) as unknown as {
1181 - successful: boolean;
1182 - storedInFile?: boolean;
1183 - data: { content: string };
1184 - };
1185 -
1186 - expect(output.successful).toBe(true);
1187 - expect(output.storedInFile).toBeUndefined();
1188 - expect(output.data.content).toHaveLength(18_000);
1189 - })
1190 - );
1191 - }
1192 - );
1193 - layer(
1194 - TestLive({
1195 - baseConfigProvider: largeOutputConfigProvider,
1196 - fixture: 'global-test-user-id',
1197 - stdin: { isTTY: true, data: '' },
1198 - toolsExecutor: {
1199 - respondWith: {
1200 - data: {
1201 - // Both of o200k's special tokens, in a payload past the inline
1202 - // threshold so the token count is actually computed. Reading a file
1203 - // that documents a tokenizer is enough to hit this in real use.
1204 - content: `<|endoftext|> <|endofprompt|> ${'token '.repeat(20_000)}`,
1205 - },
1206 - error: null,
1207 - successful: true,
1208 - logId: 'log_special_tokens',
1209 - },
1210 - },
1211 - })
1212 - )(
1213 - '[Given] a response containing tiktoken special-token literals [Then] it still reports the execution',
1214 - it => {
1215 - it.effect('counts the literals as special tokens instead of failing the command', () =>
1216 - Effect.gen(function* () {
1217 - yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '{"recipient":"a"}']);
1218 - const lines = yield* MockConsole.getLines({ stripAnsi: true });
1219 - const output = parseLastJson(lines) as unknown as {
1220 - successful: boolean;
1221 - storedInFile: boolean;
1222 - tokenCount: number;
1223 - outputFilePath: string;
1224 - };
1225 -
1226 - expect(output.successful).toBe(true);
1227 - expect(output.storedInFile).toBe(true);
1228 - expect(output.tokenCount).toBeGreaterThan(10_000);
1229 -
1230 - const storedJson = fs.readFileSync(output.outputFilePath, 'utf8');
1231 - expect(storedJson).toContain('<|endoftext|>');
1232 - expect(storedJson).toContain('<|endofprompt|>');
1175 + )('[Given] a response under the byte threshold [Then] it prints inline', it => {
1176 + it.effect('does not store the payload in a file', () =>
1177 + Effect.gen(function* () {
1178 + yield* cli(['execute', 'GMAIL_SEND_EMAIL', '-d', '{"recipient":"a"}']);
1179 + const lines = yield* MockConsole.getLines({ stripAnsi: true });
1180 + const output = parseLastJson(lines) as unknown as {
1181 + successful: boolean;
1182 + storedInFile?: boolean;
1183 + data: { content: string };
1184 + };
1185
1234 - fs.rmSync(output.outputFilePath.slice(0, output.outputFilePath.lastIndexOf('/')), {
1235 - recursive: true,
1236 - force: true,
1237 - });
1238 - })
1239 - );
1240 - }
1241 - );
1186 + expect(output.successful).toBe(true);
1187 + expect(output.storedInFile).toBeUndefined();
1188 + expect(output.data.content).toHaveLength(36_000);
1189 + })
1190 + );
1191 + });
1192
1193 layer(
1194 TestLive({
ts/packages/cli/test/src/commands/tools/tools.execute.encoder-fallback.test.ts deleted
-140
@@ -1,140 +0,0 @@
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, 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<Record<string, unknown>>(baseName, requiredExports),
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 -
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
+10 -15
@@ -524,9 +524,9 @@ describe('resolveRunCompanionAssetPath', () => {
524
525 describe('loadInstalledCompanionModule', () => {
526 layer(BunServices.layer)(it => {
527 - it('registers the in-process companions alongside the run preload set', () => {
527 + it('registers the in-process companion alongside the run preload set', () => {
528 expect(RUN_COMPANION_MODULE_BASENAMES).toEqual(
529 - expect.arrayContaining(['generation-runtime', 'execute-output-encoder-runtime'])
529 + expect.arrayContaining(['generation-runtime'])
530 );
531 });
532
@@ -536,14 +536,11 @@ describe('loadInstalledCompanionModule', () => {
536 // is covered by the Docker E2E suite, which runs the real binary.
537 it.effect('[Given] a source checkout [Then] it loads the module from its .ts source', () =>
538 Effect.gen(function* () {
539 - const encoder = yield* loadInstalledCompanionModule<
540 - typeof import('src/services/execute-output-encoder-runtime')
541 - >('execute-output-encoder-runtime', ['countOutputTokens']);
542 -
543 - expect(encoder.countOutputTokens('hello world')).toBe(2);
544 - // A special-token literal counts as its one special token rather than
545 - // being rejected.
546 - expect(encoder.countOutputTokens('<|endoftext|>')).toBe(1);
539 + const generation = yield* loadInstalledCompanionModule<
540 + typeof import('src/services/generation-runtime')
541 + >('generation-runtime', ['wrapInlineCodeForRun']);
542 +
543 + expect(generation.wrapInlineCodeForRun('1 + 1')).toBe('return (1 + 1);');
544 })
545 );
546
@@ -563,14 +560,12 @@ describe('loadInstalledCompanionModule', () => {
560 () =>
561 Effect.gen(function* () {
562 const error = yield* loadInstalledCompanionModule<{
566 - readonly countOutputTokens: unknown;
563 + readonly wrapInlineCodeForRun: unknown;
564 readonly retiredExport: unknown;
568 - }>('execute-output-encoder-runtime', ['countOutputTokens', 'retiredExport']).pipe(
569 - Effect.flip
570 - );
565 + }>('generation-runtime', ['wrapInlineCodeForRun', 'retiredExport']).pipe(Effect.flip);
566
567 expect(error._tag).toBe('services/RunCompanionRepairError');
573 - expect(error.message).toContain('execute-output-encoder-runtime');
568 + expect(error.message).toContain('generation-runtime');
569 expect(error.message).toContain('missing retiredExport');
570 })
571 );
ts/packages/cli/tsdown.config.ts
-1
@@ -10,7 +10,6 @@ export default defineConfig({
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',
13 ],
14 format: ['esm'],
15 shims: true,