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,
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,
349
readonly logId: string;
350
readonly storedInFile: true;
351
readonly tokenCount: number;
352
+ readonly sizeBytes: number;
353
readonly outputFilePath: string;
354
};
355
389
const persistLargeExecuteOutput = (
390
toolSlug: string,
391
json: string,
419
- tokenCount: number,
392
+ sizeBytes: number,
393
sharedDirectory?: string
394
) =>
395
Effect.gen(function* () {
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
});
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,
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;
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({
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));
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
}