Skip to content

The open alternative to Salesforce, designed for AI.

Bound code interpreter output streamed to chat and read from the sandbox (#29274)

Charles Bochetf6aea9645358
diff --git a/packages/twenty-server/src/engine/core-modules/code-interpreter/code-interpreter.constants.ts b/packages/twenty-server/src/engine/core-modules/code-interpreter/code-interpreter.constants.ts
index e82d93e5ac7..0130739a8ea 100644
--- a/packages/twenty-server/src/engine/core-modules/code-interpreter/code-interpreter.constants.ts
+++ b/packages/twenty-server/src/engine/core-modules/code-interpreter/code-interpreter.constants.ts
@@ -1 +1,5 @@
export const DEFAULT_CODE_INTERPRETER_TIMEOUT_MS = 300_000;
+export const CODE_INTERPRETER_MAX_OUTPUT_LENGTH = 100_000;
+export const CODE_INTERPRETER_MAX_OUTPUT_FILE_COUNT = 20;
+export const CODE_INTERPRETER_MAX_OUTPUT_FILE_SIZE_BYTES = 20 * 1024 * 1024;
+export const CODE_INTERPRETER_UPDATE_INTERVAL_MS = 500;
diff --git a/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/__tests__/local.driver.spec.ts b/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/__tests__/local.driver.spec.ts
index 26c81bf05c9..19100df52ad 100644
--- a/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/__tests__/local.driver.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/__tests__/local.driver.spec.ts
@@ -1,3 +1,7 @@
+import {
+ CODE_INTERPRETER_MAX_OUTPUT_FILE_COUNT,
+ CODE_INTERPRETER_MAX_OUTPUT_LENGTH,
+} from 'src/engine/core-modules/code-interpreter/code-interpreter.constants';
import { LocalDriver } from 'src/engine/core-modules/code-interpreter/drivers/local.driver';
describe('Local code interpreter participant isolation', () => {
@@ -55,3 +59,32 @@ describe('Local code interpreter participant isolation', () => {
expect(next.stdout).toBe('still-running\n');
});
});
+
+describe('Local code interpreter output files', () => {
+ const driver = new LocalDriver({ timeoutMs: 5000 });
+
+ it('skips oversized files and stops at the file count limit', async () => {
+ const result = await driver.execute(
+ "open('/home/user/output/big.bin', 'wb').truncate(30 * 1024 * 1024)\nfor index in range(25):\n open(f'/home/user/output/file-{index}.txt', 'w').write('ok')",
+ [],
+ );
+
+ expect(result.exitCode).toBe(0);
+ expect(result.files).toHaveLength(CODE_INTERPRETER_MAX_OUTPUT_FILE_COUNT);
+ expect(result.files.map((file) => file.filename)).not.toContain('big.bin');
+ });
+
+ it('truncates stdout past the output length limit', async () => {
+ const result = await driver.execute(
+ "for index in range(5000):\n print('x' * 99)",
+ [],
+ );
+
+ expect(result.exitCode).toBe(0);
+ expect(result.stdout.startsWith('x'.repeat(99))).toBe(true);
+ expect(result.stdout.endsWith('[output truncated]')).toBe(true);
+ expect(result.stdout.length).toBeLessThan(
+ CODE_INTERPRETER_MAX_OUTPUT_LENGTH + 100,
+ );
+ });
+});
diff --git a/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/e2b.driver.ts b/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/e2b.driver.ts
index e93ce0d73f8..0efa91db979 100644
--- a/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/e2b.driver.ts
+++ b/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/e2b.driver.ts
@@ -3,14 +3,20 @@ import { join } from 'path';
import { Sandbox } from '@e2b/code-interpreter';
import { Logger } from '@nestjs/common';
+import { isNonEmptyString } from '@sniptt/guards';
import { isDefined } from 'twenty-shared/utils';
-import { DEFAULT_CODE_INTERPRETER_TIMEOUT_MS } from 'src/engine/core-modules/code-interpreter/code-interpreter.constants';
+import {
+ CODE_INTERPRETER_MAX_OUTPUT_FILE_COUNT,
+ CODE_INTERPRETER_MAX_OUTPUT_FILE_SIZE_BYTES,
+ DEFAULT_CODE_INTERPRETER_TIMEOUT_MS,
+} from 'src/engine/core-modules/code-interpreter/code-interpreter.constants';
import { getOrCreateSessionSandbox } from 'src/engine/core-modules/code-interpreter/drivers/utils/get-or-create-session-sandbox.util';
import { releaseSessionSandboxes } from 'src/engine/core-modules/code-interpreter/drivers/utils/release-session-sandboxes.util';
import { sweepExpiredSessionSandboxes } from 'src/engine/core-modules/code-interpreter/drivers/utils/sweep-expired-session-sandboxes.util';
import { getMimeType } from 'src/engine/core-modules/code-interpreter/utils/get-mime-type.util';
+import { truncateCodeInterpreterOutput } from 'src/engine/core-modules/code-interpreter/utils/truncate-code-interpreter-output.util';
import {
type CodeExecutionResult,
@@ -131,16 +137,21 @@ export class E2BDriver implements CodeInterpreterDriver {
onStdout: (data) => callbacks?.onStdout?.(data.line),
onStderr: (data) => callbacks?.onStderr?.(data.line),
onResult: async (result) => {
- if (result.png) {
- const outputFile: OutputFile = {
- filename: `chart-${chartCounter++}.png`,
- content: Buffer.from(result.png, 'base64'),
- mimeType: 'image/png',
- };
-
- outputFiles.push(outputFile);
- await callbacks?.onResult?.(outputFile);
+ if (
+ !isNonEmptyString(result.png) ||
+ outputFiles.length >= CODE_INTERPRETER_MAX_OUTPUT_FILE_COUNT
+ ) {
+ return;
}
+
+ const outputFile: OutputFile = {
+ filename: `chart-${chartCounter++}.png`,
+ content: Buffer.from(result.png, 'base64'),
+ mimeType: 'image/png',
+ };
+
+ outputFiles.push(outputFile);
+ await callbacks?.onResult?.(outputFile);
},
});
@@ -148,29 +159,42 @@ export class E2BDriver implements CodeInterpreterDriver {
const outputDir = await sandbox.files.list('/home/user/output');
for (const file of outputDir) {
- if (file.type === 'file') {
- const content = await sandbox.files.read(
- `/home/user/output/${file.name}`,
- { format: 'bytes' },
- );
+ if (outputFiles.length >= CODE_INTERPRETER_MAX_OUTPUT_FILE_COUNT) {
+ break;
+ }
- const outputFile: OutputFile = {
- filename: file.name,
- content: Buffer.from(content),
- mimeType: getMimeType(file.name),
- };
+ if (file.type !== 'file' || isDefined(file.symlinkTarget)) {
+ continue;
+ }
- outputFiles.push(outputFile);
- await callbacks?.onResult?.(outputFile);
+ if (file.size > CODE_INTERPRETER_MAX_OUTPUT_FILE_SIZE_BYTES) {
+ this.logger.warn(
+ `Skipping output file ${file.name}: ${file.size} bytes exceeds the size limit`,
+ );
+ continue;
}
+
+ const content = await sandbox.files.read(
+ `/home/user/output/${file.name}`,
+ { format: 'bytes' },
+ );
+
+ const outputFile: OutputFile = {
+ filename: file.name,
+ content: Buffer.from(content),
+ mimeType: getMimeType(file.name),
+ };
+
+ outputFiles.push(outputFile);
+ await callbacks?.onResult?.(outputFile);
}
} catch {
// Output directory doesn't exist - that's fine
}
return {
- stdout: execution.logs.stdout.join('\n'),
- stderr: execution.logs.stderr.join('\n'),
+ stdout: truncateCodeInterpreterOutput(execution.logs.stdout.join('\n')),
+ stderr: truncateCodeInterpreterOutput(execution.logs.stderr.join('\n')),
exitCode: execution.error ? 1 : 0,
files: outputFiles,
error: execution.error?.value,
diff --git a/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/local.driver.ts b/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/local.driver.ts
index db35bf5389a..4ba658f77d8 100644
--- a/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/local.driver.ts
+++ b/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/local.driver.ts
@@ -7,9 +7,15 @@ import { type Readable, type Writable } from 'stream';
import { isDefined } from 'twenty-shared/utils';
import { v4 as uuidv4 } from 'uuid';
-import { DEFAULT_CODE_INTERPRETER_TIMEOUT_MS } from 'src/engine/core-modules/code-interpreter/code-interpreter.constants';
+import {
+ CODE_INTERPRETER_MAX_OUTPUT_FILE_COUNT,
+ CODE_INTERPRETER_MAX_OUTPUT_FILE_SIZE_BYTES,
+ CODE_INTERPRETER_MAX_OUTPUT_LENGTH,
+ DEFAULT_CODE_INTERPRETER_TIMEOUT_MS,
+} from 'src/engine/core-modules/code-interpreter/code-interpreter.constants';
import { LOCAL_DRIVER_PERSISTENT_KERNEL_SCRIPT } from 'src/engine/core-modules/code-interpreter/drivers/local-driver-kernel.constant';
import { getMimeType } from 'src/engine/core-modules/code-interpreter/utils/get-mime-type.util';
+import { truncateCodeInterpreterOutput } from 'src/engine/core-modules/code-interpreter/utils/truncate-code-interpreter-output.util';
import {
type CodeExecutionResult,
@@ -173,8 +179,8 @@ export class LocalDriver implements CodeInterpreterDriver {
);
return {
- stdout: response.stdout,
- stderr: response.stderr,
+ stdout: truncateCodeInterpreterOutput(response.stdout),
+ stderr: truncateCodeInterpreterOutput(response.stderr),
exitCode: response.exitCode,
files: outputFiles,
};
@@ -269,8 +275,19 @@ export class LocalDriver implements CodeInterpreterDriver {
});
for (const entry of outputEntries) {
+ if (outputFiles.length >= CODE_INTERPRETER_MAX_OUTPUT_FILE_COUNT) {
+ break;
+ }
+
if (entry.isFile()) {
- const content = await fs.readFile(join(outputDir, entry.name));
+ const filePath = join(outputDir, entry.name);
+ const { size } = await fs.stat(filePath);
+
+ if (size > CODE_INTERPRETER_MAX_OUTPUT_FILE_SIZE_BYTES) {
+ continue;
+ }
+
+ const content = await fs.readFile(filePath);
const outputFile: OutputFile = {
filename: entry.name,
content,
@@ -501,7 +518,10 @@ export class LocalDriver implements CodeInterpreterDriver {
child.stdout.on('data', (data: Buffer) => {
const text = data.toString();
- stdout += text;
+ if (stdout.length <= CODE_INTERPRETER_MAX_OUTPUT_LENGTH) {
+ stdout = truncateCodeInterpreterOutput(stdout + text);
+ }
+
const lines = text.split('\n');
for (const line of lines) {
@@ -514,7 +534,10 @@ export class LocalDriver implements CodeInterpreterDriver {
child.stderr.on('data', (data: Buffer) => {
const text = data.toString();
- stderr += text;
+ if (stderr.length <= CODE_INTERPRETER_MAX_OUTPUT_LENGTH) {
+ stderr = truncateCodeInterpreterOutput(stderr + text);
+ }
+
const lines = text.split('\n');
for (const line of lines) {
diff --git a/packages/twenty-server/src/engine/core-modules/code-interpreter/utils/truncate-code-interpreter-output.util.ts b/packages/twenty-server/src/engine/core-modules/code-interpreter/utils/truncate-code-interpreter-output.util.ts
new file mode 100644
index 00000000000..ddd5ef50bbf
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/code-interpreter/utils/truncate-code-interpreter-output.util.ts
@@ -0,0 +1,8 @@
+import { CODE_INTERPRETER_MAX_OUTPUT_LENGTH } from 'src/engine/core-modules/code-interpreter/code-interpreter.constants';
+
+const TRUNCATION_MARKER = '\n[output truncated]';
+
+export const truncateCodeInterpreterOutput = (output: string): string =>
+ output.length > CODE_INTERPRETER_MAX_OUTPUT_LENGTH
+ ? output.slice(0, CODE_INTERPRETER_MAX_OUTPUT_LENGTH) + TRUNCATION_MARKER
+ : output;
diff --git a/packages/twenty-server/src/engine/core-modules/tool/tools/code-interpreter-tool/code-interpreter-tool.ts b/packages/twenty-server/src/engine/core-modules/tool/tools/code-interpreter-tool/code-interpreter-tool.ts
index 8c538d1685e..7c94eb763c9 100644
--- a/packages/twenty-server/src/engine/core-modules/tool/tools/code-interpreter-tool/code-interpreter-tool.ts
+++ b/packages/twenty-server/src/engine/core-modules/tool/tools/code-interpreter-tool/code-interpreter-tool.ts
@@ -22,7 +22,12 @@ import {
import { ApplicationService } from 'src/engine/core-modules/application/application.service';
import { ApplicationTokenService } from 'src/engine/core-modules/auth/token/services/application-token.service';
+import {
+ CODE_INTERPRETER_MAX_OUTPUT_LENGTH,
+ CODE_INTERPRETER_UPDATE_INTERVAL_MS,
+} from 'src/engine/core-modules/code-interpreter/code-interpreter.constants';
import { CodeInterpreterService } from 'src/engine/core-modules/code-interpreter/code-interpreter.service';
+import { truncateCodeInterpreterOutput } from 'src/engine/core-modules/code-interpreter/utils/truncate-code-interpreter-output.util';
import { FileStorageService } from 'src/engine/core-modules/file-storage/services/file-storage.service';
import { FileUrlService } from 'src/engine/core-modules/file/file-url/file-url.service';
import { FileService } from 'src/engine/core-modules/file/services/file.service';
@@ -99,6 +104,28 @@ export class CodeInterpreterTool implements Tool {
let accumulatedStdout = '';
let accumulatedStderr = '';
const streamedFiles: CodeExecutionFile[] = [];
+ let pendingRunningUpdate: NodeJS.Timeout | undefined;
+
+ // Each update carries the full output and is stored as a Redis stream chunk.
+ const scheduleRunningUpdate = () => {
+ if (isDefined(pendingRunningUpdate)) {
+ return;
+ }
+
+ pendingRunningUpdate = setTimeout(() => {
+ pendingRunningUpdate = undefined;
+ onCodeExecutionUpdate?.(
+ this.buildExecutionState(
+ executionId,
+ 'running',
+ code,
+ accumulatedStdout,
+ accumulatedStderr,
+ streamedFiles,
+ ),
+ );
+ }, CODE_INTERPRETER_UPDATE_INTERVAL_MS);
+ };
onCodeExecutionUpdate?.(
this.buildExecutionState(executionId, 'pending', code, '', '', []),
@@ -147,30 +174,24 @@ export class CodeInterpreterTool implements Tool {
},
{
onStdout: (line) => {
- accumulatedStdout += line + '\n';
- onCodeExecutionUpdate?.(
- this.buildExecutionState(
- executionId,
- 'running',
- code,
- accumulatedStdout,
- accumulatedStderr,
- streamedFiles,
- ),
- );
+ if (
+ accumulatedStdout.length <= CODE_INTERPRETER_MAX_OUTPUT_LENGTH
+ ) {
+ accumulatedStdout = truncateCodeInterpreterOutput(
+ accumulatedStdout + line + '\n',
+ );
+ scheduleRunningUpdate();
+ }
},
onStderr: (line) => {
- accumulatedStderr += line + '\n';
- onCodeExecutionUpdate?.(
- this.buildExecutionState(
- executionId,
- 'running',
- code,
- accumulatedStdout,
- accumulatedStderr,
- streamedFiles,
- ),
- );
+ if (
+ accumulatedStderr.length <= CODE_INTERPRETER_MAX_OUTPUT_LENGTH
+ ) {
+ accumulatedStderr = truncateCodeInterpreterOutput(
+ accumulatedStderr + line + '\n',
+ );
+ scheduleRunningUpdate();
+ }
},
onResult: async (outputFile: OutputFile) => {
const uploadedFile = await this.uploadSingleFile(
@@ -181,16 +202,7 @@ export class CodeInterpreterTool implements Tool {
if (uploadedFile) {
streamedFiles.push(uploadedFile);
- onCodeExecutionUpdate?.(
- this.buildExecutionState(
- executionId,
- 'running',
- code,
- accumulatedStdout,
- accumulatedStderr,
- streamedFiles,
- ),
- );
+ scheduleRunningUpdate();
}
},
},
@@ -266,6 +278,8 @@ export class CodeInterpreterTool implements Tool {
message: 'Code interpreter execution failed',
error: errorMessage,
};
+ } finally {
+ clearTimeout(pendingRunningUpdate);
}
}