Code Execution Examples
Practical examples of running code in isolated containers using the DaggerRunner.
Overview
These examples demonstrate how to execute code generated by AI models in secure, isolated Dagger containers. The DaggerRunner supports multiple languages and automatically handles package detection and installation.
Running the Examples
# Run the Dagger test suite
npx tsx src/test/test-dagger-runner.tsExample 1: Simple Hello World
The simplest case - running a single line of code:
import { DaggerRunner } from '../evaluation/dagger-runner.js';
// TypeScript
const tsResult = await DaggerRunner.runCode({
code: 'console.log("Hello from Dagger!");',
language: 'typescript',
timeout: 30
});
console.log('Success:', tsResult.success);
console.log('Output:', tsResult.output); // "Hello from Dagger!"
// Python
const pyResult = await DaggerRunner.runCode({
code: 'print("Hello from Python!")',
language: 'python',
timeout: 30
});Example 2: TypeScript with Logic
Running TypeScript code with functions and computation:
const primeCode = `
function isPrime(n: number): boolean {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 === 0 || n % 3 === 0) return false;
for (let i = 5; i * i <= n; i += 6) {
if (n % i === 0 || n % (i + 2) === 0) return false;
}
return true;
}
const testNumbers = [2, 17, 100, 997];
testNumbers.forEach(n => {
console.log(\`\${n} is prime: \${isPrime(n)}\`);
});
`;
const result = await DaggerRunner.runCode({
code: primeCode,
language: 'typescript',
timeout: 30
});
// Output:
// 2 is prime: true
// 17 is prime: true
// 100 is prime: false
// 997 is prime: trueExample 3: Ruby with External Gems
Running Ruby code that requires external gems. The useAIConfig flag enables LLM-assisted container configuration:
import * as fs from 'fs';
// feed_reader.rb content
const feedReaderCode = `
require "feedjira"
require "faraday"
FEED_URL = "https://thefocus.ai/rss.xml"
begin
response = Faraday.get(FEED_URL)
feed = Feedjira.parse(response.body)
puts "Feed: #{feed.title}"
puts "=" * 60
feed.entries.first(5).each_with_index do |entry, index|
puts "#{index + 1}. #{entry.title}"
puts " Published: #{entry.published}"
puts
end
rescue => e
puts "Error: #{e.message}"
end
`;
const result = await DaggerRunner.runCode({
code: feedReaderCode,
language: 'ruby',
timeout: 120, // Longer timeout for gem installation
useAIConfig: true // Enable AI-assisted package detection
});
console.log('Success:', result.success);
console.log('Output:', result.output);
console.log('Container config:', JSON.stringify(result.containerConfig, null, 2));When useAIConfig is enabled, DaggerRunner:
- Analyzes the code to detect
requirestatements - Uses an LLM to generate the appropriate container configuration
- Caches the configuration for future runs
Example 4: Cache Statistics
Monitor cache usage for container configurations:
// Get cache statistics
const stats = DaggerRunner.getCacheStats();
console.log('Memory cache size:', stats.memorySize);
console.log('Disk cache size:', stats.diskSize);
console.log('Cache directory:', stats.cacheDir);
// Clear cache if needed
DaggerRunner.clearCache();Example 5: Checking Supported Languages
const languages = DaggerRunner.getSupportedLanguages();
console.log('Supported languages:', languages.join(', '));
// typescript, javascript, python, ruby, go, rust, java, php, perl, bash, swiftResult Structure
All execution results follow the DaggerRunResult interface:
interface DaggerRunResult {
success: boolean; // Whether execution succeeded
output?: string; // stdout from execution
error?: string; // stderr or error message
exitCode?: number; // Process exit code
modelName?: string; // Model name (if tracking)
containerConfig?: ContainerConfig; // Config used
cached?: boolean; // Whether config was from cache
executionTime?: number; // Total execution time in ms
}Error Handling
const result = await DaggerRunner.runCode({
code: 'invalid syntax here!!!',
language: 'python',
timeout: 30
});
if (!result.success) {
console.error('Execution failed:', result.error);
console.error('Exit code:', result.exitCode);
}Timeout Handling
const infiniteLoop = `
while True:
pass # This will timeout
`;
const result = await DaggerRunner.runCode({
code: infiniteLoop,
language: 'python',
timeout: 5 // 5 second timeout
});
if (result.exitCode === 124) {
console.log('Code execution timed out');
}Integration with Evaluations
The DaggerRunner is commonly used in evaluation pipelines to test code generated by AI models:
import { DaggerRunner } from '../evaluation/dagger-runner.js';
import { extractTypeScriptCode } from '../evaluation/typescript-code-extractor.js';
// After getting a model response with code
const extractedCode = extractTypeScriptCode(modelResponse.content);
if (extractedCode) {
const result = await DaggerRunner.runCode({
code: extractedCode,
language: 'typescript',
timeout: 30,
modelName: model.name
});
console.log(`Model ${model.name} code ${result.success ? 'executed successfully' : 'failed'}`);
}Full Test Script
See the complete test script at src/test/test-dagger-runner.ts:
npx tsx src/test/test-dagger-runner.tsThis runs all the examples above and produces detailed output for each test case.
Related Documentation
- Code Execution Guide - Complete guide to code execution
- Evaluation Framework - Using code execution in evaluations
- Tool Conversation Examples - Testing tool calling