Code Generation and Execution
Generate verified software code, validate syntax via AST parsing, and execute sandboxed scripts with E2B and Docker.
TL;DR
- Prompt frontier coding models to generate executable code blocks strictly in
markdown. - Validate generated source syntax using TypeScript abstract syntax tree
createSourceFile()APIs. - Execute untrusted generated scripts securely within isolated
E2BmicroVM sandbox containers.
Code Extraction & AST Validation
Markdown Fence Code ExtractorIsolate pure code payload from conversational LLM response.
function extractCode(text: string, lang = 'ts') {
const fence = String.fromCharCode(96).repeat(3);
const re = `${fence}${lang}([\\s\\S]*?)${fence}`;
const pat = new RegExp(re);
const match = text.match(pat);
return match ? match[1].trim() : text.trim();
}TypeScript AST Syntax CheckerDetect syntax errors before attempting execution.
import ts from 'typescript';
function validateSyntax(code: string): string[] {
const sf = ts.createSourceFile(
'temp.ts', code, ts.ScriptTarget.Latest, true
);
const diags = (sf as any).parseDiagnostics || [];
return diags.map((d: any) => d.messageText);
}Disallowed Token ScannerBlock dangerous system primitives before sandbox execution.
function scanDangerousTokens(code: string) {
const BANNED = ['child_process', 'eval('];
for (const b of BANNED) {
if (code.includes(b)) {
throw new Error(`Forbidden: ${b}`);
}
}
}Sandboxed Execution with E2B
E2B Cloud Sandbox RunnerExecute untrusted Python or Node code in an isolated microVM.
import { CodeInterpreter }
from '@e2b/code-interpreter';
async function runInSandbox(code: string) {
const sandbox = await CodeInterpreter.create();
try {
const execution = await sandbox.runCode(code);
return execution.logs.stdout.join('\n');
} finally {
await sandbox.kill();
}
}Execution Timeout BoundaryKill hanging scripts that exceed predefined runtime limits.
const MAX_MS = 10000; // 10s ceiling
const execution = await sandbox.runCode(code, {
timeoutMs: MAX_MS,
});Sandbox File InjectionUpload input data files directly into sandbox filesystem.
await sandbox.files.write(
'/home/user/data.csv',
csvContent
);Self-Repair & Test-Driven Loops
Iterative Test Self-Repair LoopProvide compiler error back to model until tests pass.
async function repairCode(
goal: string, maxAttempts = 3, client: any
) {
let code = await generateInitialCode(goal, client);
for (let i = 0; i < maxAttempts; i++) {
const err = await testCode(code);
if (!err) return code;
code = await requestFix(code, err, client);
}
throw new Error('Self-repair exceeded max attempts');
}Error Feedback Prompt BuilderStructure compiler or runtime error for model correction.
function buildFixPrompt(code: string, error: string) {
const f = String.fromCharCode(96).repeat(3);
return `Code error:\n${error}\n\n` +
`Fix code and output corrected file:\n` +
`${f}typescript\n${code}\n${f}`;
}Unit Test Harness InjectionAppend automated assertions to verify generated functions.
function wrapWithTests(
fnCode: string, testCode: string
) {
const hdr = '// Automated Test Harness';
return `${fnCode}\n\n${hdr}\n${testCode}`;
}Production Guardrails & Sanitization
Deterministic Seed For ConsistencyUse temperature 0 and seed for reproducible code output.
const params = {
model: 'gpt-4o',
temperature: 0,
seed: 42,
messages: [{ role: 'user', content: prompt }],
};Strict Function Signature PromptConstrain function name, parameters, and return types.
const spec = 'Implement calc(a: number, b: number).' +
' Output ONLY valid typescript without comments.';Diff Patch ApplicationApply model emitted unified diffs to existing source files.
import { applyPatch } from 'diff';
const updated = applyPatch(originalFile, modelPatch);
if (!updated) throw new Error('Patch failed to apply');Tips
- Parse code blocks from model outputs using regex delimiters like
markdownFencesrather than blindly evaluating raw conversational text. - Feed compilation and runtime error messages back into the conversation to enable autonomous code
selfRepairiteration cycles.
Warnings
- Never execute AI-generated code directly on your host operating system or production web server without containerized
sandboxing. - Avoid executing code blocks that import unrestricted filesystem, child_process, or network socket modules without strict runtime
permissions.
In Practice
Validates code syntax with TypeScript compiler AST and executes securely in an isolated E2B microVM.
- Extract code block from raw model response text.
- Perform AST syntax validation using TypeScript compiler.
- Initialize secure E2B cloud sandbox microVM.
- Execute script, capture stdout logs, and terminate sandbox.
import ts from 'typescript';
import { CodeInterpreter }
from '@e2b/code-interpreter';
async function execSafe(rawCode: string) {
const sf = ts.createSourceFile(
't.ts', rawCode, ts.ScriptTarget.Latest, true
);
const d = (sf as any).parseDiagnostics ?? [];
if (d.length > 0) throw new Error('Syntax error');
const sb = await CodeInterpreter.create();
try {
const r = await sb.runCode(rawCode);
return r.logs.stdout.join('\n');
} finally {
await sb.kill();
}
}
console.log(await execSafe('print(2 + 2)'));FAQ
Generated code is untrusted. Without an isolated sandbox like Docker or E2B, malicious or buggy code could delete files, leak environment secrets, or launch network attacks.
When generated code fails linting or unit tests, the test failure output is captured and appended to the prompt history, allowing the model to analyze the error and emit a patch.
Use regular expressions targeting markdown code fences, capturing the language tag and inner code content while ignoring introductory and concluding conversational text.