Orchestrata Ultimate / Specs / 063-repl-tool
063-repl-tool: REPLTool Specification
The REPLTool provides an interactive Read-Eval-Print Loop (REPL) environment for testing code snippets and evaluating expressions across multiple programming languages. It enables developers to execute code in an isolated, sandboxed environment with configurable resource limits.
At a glance
| Item | Value |
|---|---|
| Spec directory | specs/063-repl-tool/ |
| Status line in the spec | Production Ready |
| Requirement IDs defined | 32 |
With a @traces citation in src/ |
23 (71.9%) |
With a @traces citation in tests/ |
0 (0.0%) |
| Tracked files | 3 |
| RTM page | 063-repl-tool |
Reading this spec
This spec defines REPLTool, an interactive read-eval-print loop that lets developers and QA engineers test code snippets, evaluate expressions and try shell commands without risking the host system. It is one of the tool specs in the Claude Code parity plan (spec 062). Each session gets an isolated subprocess or container-based execution environment, with sandbox isolation, resource limits and persistent session state.
Four user stories frame the scope: evaluating Python expressions, running multi-line JavaScript, running Bash commands under resource limits, and keeping session state across calls. The requirements split into session management (REQ-063-001 to 007: unique session identifiers, on-demand termination, an idle timeout with a 300 second default, and a global cap on concurrent sessions), code execution (Python, JavaScript on a persistent Node.js process, and Bash, each captured as stdout and stderr with the value of a trailing expression returned), resource limits (a wall-clock timeout, CPU and memory limits, an output size cap, a per-session expression limit and per-execution usage reporting), sandbox and security (a separate OS subprocess per session, filesystem allowlisting, best-effort detection of malicious code patterns and three sandbox levels), and the API (execute, get-session and paginated history payloads, input validation, and REST endpoints). Some items the spec lists, such as WebSocket streaming, a standalone CLI and transport-level security, are marked as not part of the traced requirements.
The design supports Python 3.11 and later, Node.js 18 and later, and Bash, and limits are enforced both with resource.setrlimit and with timeouts.
The implementation lives in the REPL tool module. The requirements traceability matrix lists each ID. The spec's own status line reads Production Ready and it tracks 3 files. Measured coverage is 23 of 32 requirements cited by at least one @traces in src/ (71.9%), spread over 1 module; the module citing the most of them is orchestrata_ultimate/tools/repl_tool (23 requirements). In tests/, 0 requirements are cited by a test-side @traces. 9 requirements have no citing symbol; the behaviour may still exist in code that was never annotated.
Requirements
Checkbox is the state recorded in the requirements checklist; Traced by is the count of src/ symbols whose @traces names the ID.
| ID | Done | Requirement | Traced by |
|---|---|---|---|
REQ-063-001 |
yes | Create REPL sessions with unique, cryptographically | 2 |
REQ-063-002 |
yes | Reject create_session when a caller-supplied |
3 |
REQ-063-003 |
yes | Terminate a session on demand, killing its interpreter | 2 |
REQ-063-004 |
yes | Session state (variables, imports, function/def | 3 |
REQ-063-005 |
yes | Session idle timeout (default 300s, overridable per | 5 |
REQ-063-006 |
yes | Session activity (last_activity) is tracked and |
3 |
REQ-063-007 |
yes | A global concurrent-session cap rejects further | none |
REQ-063-008 |
yes | Python code execution via a persistent python3 |
3 |
REQ-063-009 |
yes | JavaScript code execution via a persistent Node.js | 1 |
REQ-063-010 |
yes | Bash/shell command execution via a persistent bash |
2 |
REQ-063-011 |
yes | stdout and stderr are captured and returned as | 2 |
REQ-063-012 |
yes | The value of a trailing bare expression is returned in | 1 |
REQ-063-013 |
yes | A wall-clock timeout (timeout_ms, defaulting to |
7 |
REQ-063-014 |
yes | resource.setrlimit(RLIMIT_CPU/RLIMIT_AS) (Python), |
3 |
REQ-063-015 |
yes | Combined stdout+stderr byte size is capped at | 2 |
REQ-063-016 |
yes | A session's max_expressions limit rejects further |
2 |
REQ-063-017 |
no | Per-execution resource-usage reporting (actual CPU/RSS | none |
REQ-063-018 |
yes | Each session runs in its own OS subprocess plus its | 2 |
REQ-063-019 |
no | Hard filesystem-access restriction (path allowlist / | none |
REQ-063-020 |
no | Network-access restriction in sandbox mode — NOT | none |
REQ-063-021 |
yes | Best-effort static-pattern malicious-code detection: | 3 |
REQ-063-022 |
yes | Three sandbox levels accepted: strict / standard |
1 |
REQ-063-023 |
yes | The tool's own subprocess invocations always use | 1 |
REQ-063-024 |
yes | execute returns an ExecutionResult-shaped payload: |
3 |
REQ-063-025 |
yes | get_session returns a REPLSession-shaped payload: |
1 |
REQ-063-026 |
yes | get_history returns paginated |
2 |
REQ-063-027 |
yes | Invalid/empty code input is rejected with |
2 |
REQ-063-028 |
yes | Unsupported/unavailable languages are rejected at | none |
REQ-063-029 |
no | REST API endpoints POST/GET/DELETE /sessions... |
none |
REQ-063-030 |
no | WebSocket streaming / bidirectional REPL (FR-026..028) | none |
REQ-063-031 |
no | Standalone CLI interface (FR-029..032) — NOT | none |
REQ-063-032 |
no | Transport-level security (SEC-020 TLS-in-transit, | none |
Code that traces this spec
| Module | Symbols | Requirements |
|---|---|---|
orchestrata_ultimate/tools/repl_tool |
26 | 23 |
Checklists
checklists/requirements.md
The requirement list above comes from this file; 25 of 32 checkbox items are ticked.
Other tracked files
specs/063-repl-tool/compliance-audit.md
Full spec text
Verbatim from specs/063-repl-tool/spec.md; headings are pushed down two levels.
SPEC-063: REPLTool Specification
Metadata
- Spec ID: SPEC-063
- Title: REPLTool - Interactive Code Execution Environment
- Author: Mohamed
- Status: Production Ready
- Created: 2026-04-08
- Last Updated: 2026-04-09
- Priority: P1
- Version: 1.0.0
Overview
What
The REPLTool provides an interactive Read-Eval-Print Loop (REPL) environment for testing code snippets and evaluating expressions across multiple programming languages. It enables developers to execute code in an isolated, sandboxed environment with configurable resource limits.
Why
Developers and QA engineers need a secure, isolated environment to test code snippets, evaluate mathematical expressions, and validate shell commands without risking the host system. The REPLTool addresses this need by providing a managed execution environment with sandbox isolation, resource limits, and session state persistence.
How
The REPLTool creates isolated subprocess or container-based execution environments for each session. It supports Python (3.11+), JavaScript (Node.js 18+), and Bash execution with real-time output capture, configurable resource limits (CPU time, memory, output size), and comprehensive error reporting.
User Scenarios
US-001: Python Expression Evaluation
As a Developer
I want to evaluate Python expressions and capture results
So that I can test code snippets and verify logic in an isolated environment
Acceptance Criteria:
- Session initializes with Python language support
- Expressions execute and return correct output
- stdout and stderr are captured separately
- Expression results are returned for evaluation expressions
US-002: Multi-line JavaScript Code Execution
As a Frontend Developer
I want to define and execute multi-line JavaScript functions
So that I can test complex logic and function definitions
Acceptance Criteria:
- Multi-line code blocks execute correctly
- Function definitions persist within the session
- Function invocations return correct results
- Variables and state persist across expressions
US-003: Bash Command Execution with Resource Limits
As a DevOps Engineer
I want to execute bash commands with enforced resource limits
So that I can safely test shell commands without system impact
Acceptance Criteria:
- Commands execute within configured resource limits
- Timeout enforcement terminates long-running commands
- Memory limits prevent excessive resource consumption
- Appropriate error messages display on limit violations
US-004: Session State Persistence
As a Data Scientist
I want to maintain state across multiple expressions
So that I can build complex computations incrementally
Acceptance Criteria:
- Variable assignments persist within the session
- Import statements remain active across expressions
- Functions defined in earlier expressions are callable later
- Session state resets on session termination
Terminology
| Term | Definition |
|---|---|
| REPL | Read-Eval-Print Loop - an interactive programming environment that reads user input, evaluates it, and prints the result |
| Session | An isolated execution context with persistent state, resource limits, and unique identifier |
| Sandbox | An isolated execution environment that restricts access to system resources |
| Expression | A single unit of code submitted for evaluation |
| Execution Result | The output, error, and result data returned after code execution |
| Resource Limit | Configurable boundaries for CPU time, memory, and output size |
| Sandbox Mode | Security enforcement level (strict, standard, relaxed) determining access restrictions |
| gVisor | Container runtime providing sandbox isolation for execution environments |
Data Models
Session Model
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "REPLSession",
"type": "object",
"properties": {
"session_id": {
"type": "string",
"description": "Unique session identifier",
"pattern": "^[a-zA-Z0-9]{8,32}$"
},
"language": {
"type": "string",
"enum": ["python", "javascript", "bash", "shell"],
"description": "Programming language for the REPL"
},
"created_at": {
"type": "string",
"format": "date-time",
"description": "Session creation timestamp"
},
"last_activity": {
"type": "string",
"format": "date-time",
"description": "Last activity timestamp"
},
"resource_limits": {
"$ref": "#/definitions/ResourceLimits"
},
"status": {
"type": "string",
"enum": ["active", "idle", "terminated", "error"],
"description": "Current session status"
}
},
"required": ["session_id", "language", "created_at", "status"],
"definitions": {
"ResourceLimits": {
"type": "object",
"properties": {
"max_cpu_time": {
"type": "integer",
"minimum": 1,
"maximum": 300,
"default": 30,
"description": "Maximum CPU time in seconds"
},
"max_memory_mb": {
"type": "integer",
"minimum": 16,
"maximum": 4096,
"default": 256,
"description": "Maximum memory allocation in MB"
},
"max_output_size": {
"type": "integer",
"minimum": 1024,
"maximum": 10485760,
"default": 1048576,
"description": "Maximum output size in bytes"
},
"max_expressions": {
"type": "integer",
"minimum": 1,
"maximum": 10000,
"default": 1000,
"description": "Maximum number of expressions per session"
}
}
}
}
}
Execution Request Model
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ExecutionRequest",
"type": "object",
"properties": {
"session_id": {
"type": "string",
"description": "Session identifier"
},
"code": {
"type": "string",
"description": "Code to execute",
"minLength": 1,
"maxLength": 1048576
},
"timeout_ms": {
"type": "integer",
"minimum": 100,
"maximum": 300000,
"description": "Execution timeout in milliseconds"
}
},
"required": ["session_id", "code"]
}
Execution Result Model
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ExecutionResult",
"type": "object",
"properties": {
"session_id": {
"type": "string",
"description": "Session identifier"
},
"success": {
"type": "boolean",
"description": "Execution success status"
},
"output": {
"type": "string",
"description": "Standard output"
},
"error": {
"type": "string",
"description": "Standard error output"
},
"result": {
"type": ["string", "number", "boolean", "object", "array", "null"],
"description": "Expression result (if applicable)"
},
"execution_time_ms": {
"type": "integer",
"description": "Execution time in milliseconds"
},
"timestamp": {
"type": "string",
"format": "date-time",
"description": "Execution timestamp"
}
},
"required": ["session_id", "success", "execution_time_ms", "timestamp"]
}
Session Configuration Model
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "SessionConfig",
"type": "object",
"properties": {
"language": {
"type": "string",
"enum": ["python", "javascript", "bash", "shell"],
"description": "Programming language"
},
"sandbox_mode": {
"type": "string",
"enum": ["strict", "standard", "relaxed"],
"default": "standard",
"description": "Sandbox enforcement level"
},
"environment": {
"type": "object",
"description": "Environment variables",
"additionalProperties": {
"type": "string"
}
},
"working_directory": {
"type": "string",
"description": "Working directory for execution"
},
"resource_limits": {
"$ref": "#/definitions/ResourceLimits"
}
},
"required": ["language"],
"definitions": {
"ResourceLimits": {
"type": "object",
"properties": {
"max_cpu_time": {
"type": "integer",
"minimum": 1,
"maximum": 300
},
"max_memory_mb": {
"type": "integer",
"minimum": 16,
"maximum": 4096
},
"max_output_size": {
"type": "integer",
"minimum": 1024,
"maximum": 10485760
},
"max_expressions": {
"type": "integer",
"minimum": 1,
"maximum": 10000
}
}
}
}
}
Functional Requirements
Core Features
Session Management
- FR-001: The system MUST support creating new REPL sessions with unique identifiers
- FR-002: The system MUST support terminating sessions and releasing resources
- FR-003: The system MUST support session state persistence across expressions
- FR-004: The system MUST support session idle timeout (default: 300 seconds)
- FR-005: The system MUST support session activity tracking
Code Execution
- FR-006: The system MUST support Python code execution (version 3.11+)
- FR-007: The system MUST support JavaScript code execution (Node.js 18+)
- FR-008: The system MUST support Bash command execution
- FR-009: The system MUST capture stdout and stderr separately
- FR-010: The system MUST return expression results for evaluation expressions
Resource Management
- FR-011: The system MUST enforce CPU time limits per execution
- FR-012: The system MUST enforce memory allocation limits
- FR-013: The system MUST enforce output size limits
- FR-014: The system MUST terminate executions that exceed resource limits
- FR-015: The system MUST report resource usage after each execution
Sandbox Security
- FR-016: The system MUST isolate each session in a separate sandbox
- FR-017: The system MUST restrict file system access in sandbox mode
- FR-018: The system MUST restrict network access in sandbox mode
- FR-019: The system MUST prevent execution of malicious code
- FR-020: The system MUST support multiple sandbox levels (strict/standard/relaxed)
5.2 API Features
5.2.1 REST API Endpoints
- FR-021: The system MUST provide POST /sessions for creating sessions
- FR-022: The system MUST provide GET /sessions/{id} for session info
- FR-023: The system MUST provide DELETE /sessions/{id} for session termination
- FR-024: The system MUST provide POST /sessions/{id}/execute for code execution
- FR-025: The system MUST provide GET /sessions/{id}/history for execution history
5.2.2 WebSocket Support
- FR-026: The system MUST support WebSocket connections for real-time REPL
- FR-027: The system MUST support streaming output via WebSocket
- FR-028: The system MUST support bidirectional communication
5.3 User Interface Features
5.3.1 CLI Interface
- FR-029: The system MUST provide a command-line interface for REPL interaction
- FR-030: The CLI MUST support session creation with language selection
- FR-031: The CLI MUST support interactive input mode
- FR-032: The CLI MUST support executing code from files
Error Conditions
| ID | Condition | Handling |
|---|---|---|
| ERR-001 | SessionNotFound - Session with specified ID does not exist | Return 404 with error message; suggest creating new session |
| ERR-002 | SessionAlreadyExists - Session with specified ID already exists | Return 409 with error message; suggest using different ID or terminating existing |
| ERR-003 | SessionTerminated - Session has been terminated | Return 410 with error message; suggest creating new session |
| ERR-004 | SessionTimeout - Session idle timeout exceeded | Return 408 with error message; session resources freed |
| ERR-005 | InvalidSessionId - Session ID format is invalid | Return 400 with validation error; show required format |
| ERR-006 | ExecutionTimeout - Code execution exceeded time limit | Return execution result with success=false and timeout error |
| ERR-007 | ExecutionMemoryExceeded - Code execution exceeded memory limit | Return execution result with success=false and memory error |
| ERR-008 | ExecutionOutputExceeded - Output size exceeded limit | Return execution result with success=false and truncated output |
| ERR-009 | ExecutionError - Generic execution error occurred | Return execution result with success=false and error details |
| ERR-010 | CompilationError - Code failed to compile/parse | Return execution result with success=false and syntax error details |
| ERR-011 | SandboxViolation - Sandbox security policy violated | Return 403 with security violation details; log for audit |
| ERR-012 | ResourceLimitExceeded - Global resource limit exceeded | Return 429 with retry-after header; show limit details |
| ERR-013 | UnauthorizedAccess - Access to resource denied | Return 401/403 with authorization error |
| ERR-014 | MaliciousCodeDetected - Potentially malicious code detected | Return 400 with detection details; block execution |
| ERR-015 | InvalidRequest - Request body is invalid | Return 400 with validation error details |
| ERR-016 | UnsupportedLanguage - Language not supported | Return 400 with supported languages list |
| ERR-017 | RateLimitExceeded - Too many requests | Return 429 with retry-after header |
| ERR-018 | ServiceUnavailable - Service is temporarily unavailable | Return 503 with retry-after header |
| ERR-019 | InvalidCodeInput - Code input is null or exceeds max length | Return 400 with validation error |
| ERR-020 | ConcurrentSessionLimit - Maximum concurrent sessions reached | Return 503 with queue information |
API Contracts
7.1 Create Session
Endpoint: POST /api/v1/sessions
Content-Type: application/json
Request:
{
"language": "python",
"sandbox_mode": "standard",
"resource_limits": {
"max_cpu_time": 30,
"max_memory_mb": 256,
"max_output_size": 1048576
}
}
Response (201 Created):
{
"session_id": "abc123xyz",
"language": "python",
"created_at": "2026-04-08T10:30:00Z",
"status": "active",
"resource_limits": {
"max_cpu_time": 30,
"max_memory_mb": 256,
"max_output_size": 1048576
}
}
7.2 Execute Code
Endpoint: POST /api/v1/sessions/{session_id}/execute
Content-Type: application/json
Request:
{
"code": "print('Hello, World!')",
"timeout_ms": 5000
}
Response (200 OK):
{
"session_id": "abc123xyz",
"success": true,
"output": "Hello, World!\n",
"error": "",
"result": null,
"execution_time_ms": 12,
"timestamp": "2026-04-08T10:30:05Z"
}
7.3 Get Session Info
Endpoint: GET /api/v1/sessions/{session_id}
Response (200 OK):
{
"session_id": "abc123xyz",
"language": "python",
"created_at": "2026-04-08T10:30:00Z",
"last_activity": "2026-04-08T10:30:05Z",
"status": "active",
"expression_count": 5,
"resource_limits": {
"max_cpu_time": 30,
"max_memory_mb": 256,
"max_output_size": 1048576
}
}
7.4 Terminate Session
Endpoint: DELETE /api/v1/sessions/{session_id}
Response (204 No Content): Null body
7.5 Get Execution History
Endpoint: GET /api/v1/sessions/{session_id}/history?limit=10&offset=0
Response (200 OK):
{
"session_id": "abc123xyz",
"total": 5,
"limit": 10,
"offset": 0,
"executions": [
{
"code": "print('Hello, World!')",
"success": true,
"execution_time_ms": 12,
"timestamp": "2026-04-08T10:30:05Z"
}
]
}
Non-Functional Requirements
8.1 Security Requirements
8.1.1 Authentication and Authorization
- SEC-001: All API endpoints MUST require authentication unless explicitly public
- SEC-002: Session operations MUST be restricted to authorized users only
- SEC-003: The system MUST validate user permissions for resource access
- SEC-004: Session identifiers MUST be cryptographically secure and unguessable
8.1.2 Sandboxing and Isolation
- SEC-005: Each session MUST execute in a completely isolated environment
- SEC-006: Sessions MUST NOT be able to access other session data or state
- SEC-007: File system access MUST be restricted based on sandbox mode
- SEC-008: Network access MUST be limited according to configured sandbox level
- SEC-009: Process isolation MUST prevent cross-session interference
8.1.3 Resource Protection
- SEC-010: Resource limits MUST be strictly enforced to prevent DoS
- SEC-011: CPU time limits MUST prevent infinite loop attacks
- SEC-012: Memory limits MUST prevent memory exhaustion attacks
- SEC-013: Output size limits MUST prevent buffer overflow attacks
- SEC-014: Execution timeouts MUST terminate runaway processes immediately
8.1.4 Input Validation and Sanitization
- SEC-015: All code input MUST be validated for malicious patterns
- SEC-016: Dangerous system calls MUST be blocked via seccomp filtering
- SEC-017: Shell injection attempts MUST be detected and prevented
- SEC-018: File system traversal patterns MUST be blocked
- SEC-019: Import of dangerous modules MUST be restricted (os, sys, subprocess, etc.)
8.1.5 Data Protection
- SEC-020: Execution results MUST be encrypted in transit (HTTPS/TLS 1.3+)
- SEC-021: Session state MUST be protected from unauthorized access
- SEC-022: Sensitive data MUST NOT be logged in plaintext
- SEC-023: Audit logs MUST be tamper-evident and securely stored
- SEC-024: Session cleanup MUST securely delete all sensitive data
8.1.6 Audit and Compliance
- SEC-025: All security events MUST be logged with timestamps
- SEC-026: Failed authentication attempts MUST be logged and monitored
- SEC-027: Sandbox violations MUST trigger immediate alerts
- SEC-028: Resource limit violations MUST be tracked for patterns
- SEC-029: Audit logs MUST be retained for minimum 90 days
- SEC-030: Security incidents MUST provide complete forensic data
8.1.7 Threat Mitigation
- SEC-031: The system MUST prevent code injection attacks
- SEC-032: The system MUST prevent remote code execution vulnerabilities
- SEC-033: The system MUST prevent privilege escalation attempts
- SEC-034: The system MUST detect and block malicious code patterns
- SEC-035: Rate limiting MUST prevent brute force and abuse attacks
8.1.8 Secure Configuration
- SEC-036: Default sandbox mode MUST be "standard" (secure by default)
- SEC-037: Default resource limits MUST prevent abuse while allowing normal use
- SEC-038: Debug mode MUST NOT expose sensitive information in production
- SEC-039: Configuration changes MUST require proper authorization
- SEC-040: Security configurations MUST be version controlled and auditable
8.1.9 Incident Response
- SEC-041: Security violations MUST be logged with full context
- SEC-042: The system MUST support real-time security monitoring
- SEC-043: Automatic session termination MUST occur on critical violations
- SEC-044: The system MUST provide security incident reporting
- SEC-045: Recovery procedures MUST be documented and tested
8.2 Performance Requirements
| Metric | Target | Maximum |
|---|---|---|
| Session creation latency | < 100ms | 500ms |
| Code execution latency | < 500ms | 2000ms |
| Concurrent session capacity | 50 sessions | 100 sessions |
| Memory usage per session | < 256MB | 4GB |
| Output streaming latency | < 50ms | 100ms |
8.3 Reliability Requirements
- System MUST achieve 99.9% uptime
- Sessions MUST be recoverable after system restart
- Failed executions MUST NOT affect other concurrent sessions
- Session cleanup MUST occur within 60 seconds of termination
8.4 Scalability Requirements
- System MUST support horizontal scaling via load balancing
- Session state MUST be distributable across multiple instances
- Resource limits MUST be enforced consistently in distributed mode
Security Requirements
This section consolidates all security-related requirements for the REPLTool system. All security requirements MUST be enforced at all times.
Authentication and Authorization
- SEC-001: All API endpoints MUST require authentication unless explicitly public
- SEC-002: Session operations MUST be restricted to authorized users only
- SEC-003: The system MUST validate user permissions for resource access
- SEC-004: Session identifiers MUST be cryptographically secure and unguessable
Sandboxing and Isolation
- SEC-005: Each session MUST execute in a completely isolated environment
- SEC-006: Sessions MUST NOT be able to access other session data or state
- SEC-007: File system access MUST be restricted based on sandbox mode
- SEC-008: Network access MUST be limited according to configured sandbox level
- SEC-009: Process isolation MUST prevent cross-session interference
Resource Protection
- SEC-010: Resource limits MUST be strictly enforced to prevent DoS
- SEC-011: CPU time limits MUST prevent infinite loop attacks
- SEC-012: Memory limits MUST prevent memory exhaustion attacks
- SEC-013: Output size limits MUST prevent buffer overflow attacks
- SEC-014: Execution timeouts MUST terminate runaway processes immediately
Input Validation and Sanitization
- SEC-015: All code input MUST be validated for malicious patterns
- SEC-016: Dangerous system calls MUST be blocked via seccomp filtering
- SEC-017: Shell injection attempts MUST be detected and prevented
- SEC-018: File system traversal patterns MUST be blocked
- SEC-019: Import of dangerous modules MUST be restricted (os, sys, subprocess, etc.)
Data Protection
- SEC-020: Execution results MUST be encrypted in transit (HTTPS/TLS 1.3+)
- SEC-021: Session state MUST be protected from unauthorized access
- SEC-022: Sensitive data MUST NOT be logged in plaintext
- SEC-023: Audit logs MUST be tamper-evident and securely stored
- SEC-024: Session cleanup MUST securely delete all sensitive data
Audit and Compliance
- SEC-025: All security events MUST be logged with timestamps
- SEC-026: Failed authentication attempts MUST be logged and monitored
- SEC-027: Sandbox violations MUST trigger immediate alerts
- SEC-028: Resource limit violations MUST be tracked for patterns
- SEC-029: Audit logs MUST be retained for minimum 90 days
- SEC-030: Security incidents MUST provide complete forensic data
Threat Mitigation
- SEC-031: The system MUST prevent code injection attacks
- SEC-032: The system MUST prevent remote code execution vulnerabilities
- SEC-033: The system MUST prevent privilege escalation attempts
- SEC-034: The system MUST detect and block malicious code patterns
- SEC-035: Rate limiting MUST prevent brute force and abuse attacks
Secure Configuration
- SEC-036: Default sandbox mode MUST be "standard" (secure by default)
- SEC-037: Default resource limits MUST prevent abuse while allowing normal use
- SEC-038: Debug mode MUST NOT expose sensitive information in production
- SEC-039: Configuration changes MUST require proper authorization
- SEC-040: Security configurations MUST be version controlled and auditable
Incident Response
- SEC-041: Security violations MUST be logged with full context
- SEC-042: The system MUST support real-time security monitoring
- SEC-043: Automatic session termination MUST occur on critical violations
- SEC-044: The system MUST provide security incident reporting
- SEC-045: Recovery procedures MUST be documented and tested
Technical Design
9.1 Architecture Overview
The REPLTool follows a microservice architecture with the following components:
- API Layer: FastAPI-based REST API handling session management and execution requests
- Session Manager: Orchestrates session lifecycle, state persistence, and resource allocation
- Execution Engine: Manages language-specific interpreters (Python, JavaScript, Bash)
- Sandbox Manager: Implements isolation via gVisor containers or subprocess isolation
- Resource Monitor: Tracks CPU, memory, and output usage per session
9.2 Execution Flow
- Client creates session via POST /sessions
- Session Manager initializes sandbox environment
- Client sends code via POST /sessions/{id}/execute
- Execution Engine validates input and applies resource limits
- Code runs in isolated sandbox with timeout enforcement
- Output captured and returned to client
- Session state updated with execution metadata
9.3 Sandbox Implementation
| Mode | Isolation Method | File Access | Network Access | Subprocess |
|---|---|---|---|---|
| Strict | gVisor container | None | None | None |
| Standard | gVisor subprocess | /tmp only | None | Shell built-ins |
| Relaxed | Subprocess isolation | /tmp, $HOME | Local only | Restricted |
Examples
10.1 Python Session Example
# Create Python session
curl -X POST http://localhost:8000/api/v1/sessions \
-H "Content-Type: application/json" \
-d '{"language": "python", "sandbox_mode": "standard"}'
# Response
{"session_id": "abc123xyz", "status": "active", ...}
# Execute code
curl -X POST http://localhost:8000/api/v1/sessions/abc123xyz/execute \
-H "Content-Type: application/json" \
-d '{"code": "print(2 + 2)"}'
# Response
{"success": true, "output": "4\n", "result": null, ...}
10.2 JavaScript Session Example
# Create JavaScript session
curl -X POST http://localhost:8000/api/v1/sessions \
-H "Content-Type: application/json" \
-d '{"language": "javascript"}'
# Execute multi-line code
curl -X POST http://localhost:8000/api/v1/sessions/xyz789abc/execute \
-H "Content-Type: application/json" \
-d '{"code": "function factorial(n) { return n <= 1 ? 1 : n * factorial(n-1); }\nfactorial(5)"}'
# Response
{"success": true, "output": "", "result": 120, ...}
10.3 CLI Usage Example
# Start interactive Python REPL
repltool interactive --language python
# Execute code from file
repltool execute --file script.py --language python
# Create session with custom limits
repltool create --language bash --max-cpu-time 10 --max-memory 128
Security Requirements
11.1 Sandboxing Architecture
The REPLTool implements a multi-layered sandbox architecture:
- Process Isolation: Each session runs in an isolated subprocess
- Container Isolation: Optional Docker-based container isolation for strict mode
- Filesystem Restrictions: Read-only filesystem with tmpfs for temporary files
- Network Restrictions: No network access in strict mode; limited in standard mode
- System Call Filtering: seccomp-bpf for system call restrictions
11.2 Resource Limits
| Resource | Default Limit | Maximum Limit |
|---|---|---|
| CPU Time | 30 seconds | 300 seconds |
| Memory | 256 MB | 4 GB |
| Output Size | 1 MB | 10 MB |
| Expressions/Session | 1,000 | 10,000 |
| Concurrent Sessions | 10 | 50 |
11.3 Security Policies
11.3.1 Strict Mode
- No file system access
- No network access
- No subprocess spawning
- No import of external modules (Python)
- Maximum resource restrictions
11.3.2 Standard Mode
- Temporary directory access only
- No network access
- Limited subprocess (shell built-ins only)
- Whitelisted module imports only
11.3.3 Relaxed Mode
- Temporary directory access
- Local network only
- Subprocess allowed (restricted)
- Most modules allowed
11.4 Malicious Code Detection
The system detects and blocks:
- Infinite loops (via timeout)
- Memory exhaustion attacks
- Recursive function abuse
- Import of dangerous modules (os, sys, subprocess in Python)
- Shell injection attempts
- File system traversal attempts
11.5 Audit Logging
All executions are logged with:
- Session ID
- Timestamp
- Code hash (for security analysis)
- Execution result
- Resource usage
- Error conditions
Governance
12.1 Policy Enforcement
- Code execution requires tool:execute permission
- Resource limits enforced by default
- Execution logged for audit trail
12.2 Audit Logging
async def log_execution(code: str, language: str, result: dict):
await audit_log({
"event_type": "repl_execution",
"language": language,
"success": result.success,
"duration_ms": result.duration_ms
})
Dependencies
13.1 Runtime Dependencies
| Package | Version | Purpose |
|---|---|---|
| Python | >= 3.11 | Runtime environment |
| Node.js | >= 18.0.0 | JavaScript execution |
| gVisor | >= 2.0.0 | Container sandboxing |
| seccomp | - | System call filtering |
13.2 Python Dependencies
| Package | Version | Purpose |
|---|---|---|
| fastapi | >= 0.104.0 | REST API framework |
| uvicorn | >= 0.24.0 | ASGI server |
| pydantic | >= 2.5.0 | Data validation |
| websockets | >= 12.0 | WebSocket support |
| pytest | >= 7.4.0 | Testing framework |
| pytest-cov | >= 4.1.0 | Coverage reporting |
| pytest-asyncio | >= 0.21.0 | Async testing |
13.3 External Services
| Service | Purpose | Required |
|---|---|---|
| gVisor | Container runtime | No (optional) |
| Redis | Session caching | No (optional) |
| Prometheus | Metrics | No (optional) |
Testing Requirements
Test Environment Requirements
| Requirement | Specification | Purpose |
|---|---|---|
| Python | >= 3.11 | Runtime for test execution |
| Node.js | >= 18.0.0 | JavaScript execution testing |
| gVisor | >= 2.0.0 | Sandbox isolation testing |
| Test Database | SQLite in-memory | Session state testing |
| Mock Services | pytest-mock | External service mocking |
Test Data Requirements
- Test Sessions: Pre-created sessions for Python, JavaScript, and Bash with various states
- Code Samples: Valid, invalid, and malicious code samples for each language
- Resource Scenarios: Code samples designed to trigger limit violations
- Error Scenarios: Request payloads that trigger each error condition
Test Coverage Requirements
| Category | Minimum Coverage |
|---|---|
| Unit Tests | 80% code coverage |
| Integration Tests | All API endpoints |
| Security Tests | All sandbox modes |
| Performance Tests | All latency thresholds |
Test Execution Requirements
- All tests MUST pass before release
- Performance tests MUST meet latency thresholds
- Security tests MUST pass without vulnerabilities
- Integration tests MUST run against actual (not mocked) dependencies
Testing Requirements
14.1 Unit Tests
| Test ID | Description | Coverage Target |
|---|---|---|
| UT-001 | Session creation with valid parameters | 100% |
| UT-002 | Session creation with invalid language | 100% |
| UT-003 | Session termination | 100% |
| UT-004 | Code execution for Python | 100% |
| UT-005 | Code execution for JavaScript | 100% |
| UT-006 | Code execution for Bash | 100% |
| UT-007 | Resource limit enforcement | 100% |
| UT-008 | Error handling for all error codes | 100% |
14.2 Integration Tests
| Test ID | Description | Coverage Target |
|---|---|---|
| IT-001 | End-to-end session lifecycle | 100% |
| IT-002 | Multi-expression session | 100% |
| IT-003 | Concurrent sessions | 100% |
| IT-004 | API endpoint integration | 100% |
| IT-005 | WebSocket integration | 100% |
14.3 Security Tests
| Test ID | Description | Coverage Target |
|---|---|---|
| ST-001 | Sandbox escape prevention | 100% |
| ST-002 | Resource exhaustion prevention | 100% |
| ST-003 | Malicious code detection | 100% |
| ST-004 | Input validation | 100% |
14.4 Performance Tests
| Test ID | Description | Threshold |
|---|---|---|
| PT-001 | Session creation latency | < 100ms |
| PT-002 | Code execution latency | < 500ms |
| PT-003 | Memory usage under load | < 512MB |
| PT-004 | Concurrent session capacity | 50 sessions |
14.5 Test Execution
# Run all unit tests
pytest tests/unit/ -v --cov=repltool --cov-report=html
# Run integration tests
pytest tests/integration/ -v
# Run security tests
pytest tests/security/ -v
# Run performance tests
pytest tests/performance/ -v --benchmark-only
# Run all tests
pytest tests/ -v --cov=repltool
Integration Points
15.1 Integration with Orchestrata CLI
The REPLTool integrates with the Orchestrata CLI as tool ID repl_execute:
{
"tool": "repl_execute",
"parameters": {
"language": "python",
"code": "print('Hello')",
"sandbox_mode": "standard",
"resource_limits": {
"max_cpu_time": 30,
"max_memory_mb": 256
}
}
}
15.2 Integration with Redis (Optional)
For distributed deployments, sessions can be cached in Redis:
# Session state serialization
await redis.set(f"session:{session_id}", json.dumps(session_state), ex=3600)
15.3 Integration with Prometheus Metrics
Exported metrics include:
repl_sessions_created_total- Counter of created sessionsrepl_executions_total- Counter of executionsrepl_execution_duration_seconds- Histogram of execution timesrepl_active_sessions- Gauge of active sessions
Success Criteria
16.1 Functional Acceptance
- [ ] Sessions can be created for Python, JavaScript, and Bash
- [ ] Code execution returns correct output and results
- [ ] Resource limits are enforced correctly
- [ ] Sandbox isolation prevents unauthorized access
- [ ] Session state persists across expressions
- [ ] API endpoints respond correctly
- [ ] WebSocket connections work for real-time REPL
16.2 Security Acceptance
- [ ] Sandbox escapes are prevented
- [ ] Resource exhaustion is prevented
- [ ] Malicious code is detected and blocked
- [ ] Input validation prevents injection attacks
16.3 Performance Acceptance
- [ ] Session creation completes in < 100ms
- [ ] Code execution completes in < 500ms
- [ ] System handles 50 concurrent sessions
- [ ] Memory usage stays under limits
16.4 Reliability Acceptance
- [ ] System recovers from crashes gracefully
- [ ] Sessions are cleaned up on termination
- [ ] Errors are logged appropriately
- [ ] No data corruption occurs
Observability
Logging Requirements
- Component Logging
- Log all major operations with appropriate log levels
- Include correlation IDs for request tracing
- Log performance metrics for critical operations
- Log errors with full context for debugging
Metrics Requirements
- Performance Metrics
- Operation duration histograms
- Request/error rate counters
- Resource utilization gauges
- Business metrics relevant to domain
Tracing Requirements
- Distributed Tracing
- Trace propagation across component boundaries
- Span attributes for domain-specific context
- Trace sampling based on operational needs
Alerting Requirements
- Critical Alerts
- Error rate spikes
- Performance degradation
- Resource exhaustion
- Business metric anomalies
Dashboard Requirements
- Operational Dashboard
- Real-time system health
- Performance trends
- Error analysis
- Resource monitoring
Revision History
| Version | Date | Changes | Author |
|---|---|---|---|
| 1.0.0 | 2026-04-08 | Initial production release | Mohamed |
| 1.0.1 | 2026-04-09 | Fixed spec to meet SPEC_STANDARD.md requirements | Mohamed |
End of Specification