Build / AI /

Code Generation and Execution

Generate verified software code, validate syntax via AST parsing, and execute sandboxed scripts with E2B and Docker.

TL;DR

  1. Prompt frontier coding models to generate executable code blocks strictly in markdown.
  2. Validate generated source syntax using TypeScript abstract syntax tree createSourceFile() APIs.
  3. Execute untrusted generated scripts securely within isolated E2B microVM sandbox containers.

Code Extraction & AST Validation

    Markdown Fence Code Extractor

    Isolate 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 Checker

    Detect 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 Scanner

    Block 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 Runner

    Execute 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 Boundary

    Kill hanging scripts that exceed predefined runtime limits.

    const MAX_MS = 10000; // 10s ceiling
    const execution = await sandbox.runCode(code, {
      timeoutMs: MAX_MS,
    });
    Sandbox File Injection

    Upload 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 Loop

    Provide 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 Builder

    Structure 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 Injection

    Append 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 Consistency

    Use 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 Prompt

    Constrain function name, parameters, and return types.

    const spec = 'Implement calc(a: number, b: number).' +
      ' Output ONLY valid typescript without comments.';
    Diff Patch Application

    Apply 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

  1. Parse code blocks from model outputs using regex delimiters like markdownFences rather than blindly evaluating raw conversational text.
  2. Feed compilation and runtime error messages back into the conversation to enable autonomous code selfRepair iteration cycles.

Warnings

  1. Never execute AI-generated code directly on your host operating system or production web server without containerized sandboxing.
  2. Avoid executing code blocks that import unrestricted filesystem, child_process, or network socket modules without strict runtime permissions.

In Practice

FAQ