RuntimeUI is a framework for building AI-assisted applications where coding agents can modify UI at runtime while maintaining strict security boundaries around critical components, data flows, and APIs.
- Problem: AI might modify critical business logic (auth, payments, core features)
- Solution: Multi-level protection system with
@Locked,@Protected,@AIModifiabledecorators
- Problem: AI could access sensitive APIs or data stores
- Solution: API Guard proxy layer + Data Store Guard with allowlist/denylist
- Problem: Unbounded AI generation could create bloated, dangerous code
- Solution: Configurable constraints on tokens, lines, components, and patterns
- Problem: Malicious or buggy AI code could break the application
- Solution: Multi-stage validation + sandbox testing + rollback mechanism
┌─────────────────────────────────────────────────────────┐
│ USER QUERY │
│ "Make the grid 4 columns" │
└────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ AI CODE ENGINE │
│ (GPT-4, Claude, or custom model) │
│ Generates component code │
└────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ RUNTIME ENGINE │
│ ┌──────────────────────────────────────────────────┐ │
│ │ 1. Constraint Validation │ │
│ │ - Check token limits │ │
│ │ - Check forbidden patterns │ │
│ │ - Check component size │ │
│ └──────────────────────────────────────────────────┘ │
│ ┌──────────────────────────────────────────────────┐ │
│ │ 2. Component Protection Check │ │
│ │ - Query ComponentRegistry │ │
│ │ - Verify allowed operations │ │
│ │ - Reject locked component modifications │ │
│ └──────────────────────────────────────────────────┘ │
│ ┌──────────────────────────────────────────────────┐ │
│ │ 3. Security Analysis │ │
│ │ - Static code analysis │ │
│ │ - Check for XSS, injection risks │ │
│ │ - Validate dependencies │ │
│ └──────────────────────────────────────────────────┘ │
│ ┌──────────────────────────────────────────────────┐ │
│ │ 4. Sandbox Testing │ │
│ │ - Parse and test in isolated environment │ │
│ │ - Verify no runtime errors │ │
│ └──────────────────────────────────────────────────┘ │
└────────────────────┬────────────────────────────────────┘
│
┌──────────┴──────────┐
│ │
SUCCESS FAIL
│ │
▼ ▼
┌─────────┐ ┌──────────┐
│ APPLY │ │ REJECT │
│ + Save │ │ + Report │
│ Rollback│ │ Errors │
│ Token │ └──────────┘
└─────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ UPDATED UI RENDERED │
└─────────────────────────────────────────────────────────┘
Purpose: Central registry for all component metadata and protection rules
ComponentRegistry.getInstance()
.register({
id: 'PaymentForm_123',
name: 'PaymentForm',
protectionLevel: 'locked',
allowedOperations: []
});Responsibilities:
- Track all components and their protection levels
- Provide lookup by component name or ID
- Enable quick checks:
isLocked(),isProtected(),isModifiable()
Purpose: Main orchestrator that processes AI modifications
Flow:
- Receives AI-generated code
- Validates against constraints
- Checks component protection rules
- Runs security analysis
- Tests in sandbox
- Applies or rejects modification
- Saves rollback point
API:
const engine = new RuntimeEngine({
maxTokens: 4000,
maxComponents: 10,
maxLinesPerComponent: 200
});
const result = await engine.processModification({
userQuery: "Make grid responsive",
generatedCode: aiCode
});
if (result.success) {
// Code applied, can rollback with result.rollbackToken
} else {
// Show result.validation.errors to user
}Purpose: Define and enforce code generation limits
Default Constraints:
- Max 4000 tokens per generation
- Max 10 components modified
- Max 200 lines per component
- Forbidden patterns:
eval(),Function(), directfetch(), etc. - Allowed libraries: React, React-DOM
Customization:
engine.updateConstraints({
maxTokens: 8000,
allowedLibraries: ['react', 'react-dom', 'styled-components']
});Purpose: Secure proxy for all network requests
Features:
- Register protected endpoints
- Create secure fetch with policies
- Enforce read/write permissions
- Log all access attempts
Usage:
// Register protected endpoint
APIGuard.registerEndpoint({
path: '/api/admin',
method: 'POST',
protected: true
});
// Create secure fetch
const secureFetch = APIGuard.createSecureFetch({
canReadData: true,
canWriteData: false,
allowedEndpoints: ['/api/products'],
forbiddenEndpoints: ['/api/admin']
});
// AI-generated code uses this instead of raw fetch
const data = await secureFetch('/api/products');Purpose: Protect sensitive data in local stores
Features:
- Mark keys as protected
- Create proxied store objects
- Enforce read/write policies
Usage:
// Protect sensitive keys
DataStoreGuard.protect('authToken');
DataStoreGuard.protect('creditCard');
// Create secure proxy
const secureStore = DataStoreGuard.createSecureStore(store, {
canReadData: true,
canWriteData: false
});
// AI code can read public data
console.log(secureStore.username); // ✓ Works
// But not protected data
console.log(secureStore.authToken); // ✗ Throws errorPurpose: Multi-stage validation of AI-generated code
Validation Stages:
- Constraint Check: Token limits, size limits, forbidden patterns
- Component Protection: Check if modifying locked/protected components
- Security Analysis: Static analysis for XSS, injection, unsafe patterns
- Sandbox Test: Parse and test execution in isolated environment
Output:
{
valid: false,
errors: [
"Code contains forbidden pattern: /eval\\(/",
"Cannot modify locked component: PaymentProcessor"
],
warnings: [
"Usage of dangerouslySetInnerHTML detected"
],
metadata: {
tokensUsed: 1250,
componentsModified: 3
}
}- Use Case: Critical business logic, authentication, payments
- AI Permissions: NONE - Cannot read or modify
- Example: Payment processors, authentication providers
- Use Case: Important components that need limited customization
- AI Permissions: Restricted operations (style updates, prop changes)
- Example: Navigation bars, headers, footers
- Use Case: UI components that should adapt to user preferences
- AI Permissions: Full control within constraints
- Example: Dashboards, product grids, content layouts
@AIModifiable({
dataAccess: {
canReadData: true,
canWriteData: false,
allowedEndpoints: ['/api/products']
}
}){
allowedEndpoints: ['/api/public/*', '/api/user-preferences'],
forbiddenEndpoints: ['/api/admin', '/api/payment', '/api/auth']
}maxTokens: 4000 // ~1000 words of codemaxComponents: 10 // Max components modified per request
maxLinesPerComponent: 200 // Max lines per componentallowedOperations: [
'read-only', // Can only read component
'update-props', // Can update props
'update-style', // Can update styles
'update-children', // Can modify children
'full-control' // Complete rewrite allowed
]Every successful modification creates a rollback token:
const result = await engine.processModification({...});
if (result.success) {
const rollbackToken = result.rollbackToken;
// Later, if needed:
engine.rollback(rollbackToken);
}Keeps last 10 modifications in memory for quick rollback.
Begin with tight constraints and relax as needed:
maxTokens: 2000,
maxComponents: 5,
maxLinesPerComponent: 100Prefer allowlists over denylists for better security:
allowedEndpoints: ['/api/safe1', '/api/safe2']
// Better than forbiddenEndpointsUse multiple protection layers:
- Component decorators
- API guards
- Data store protection
- Constraint validation
const log = APIGuard.getRequestLog();
const history = engine.getHistory();Mark existing components gradually:
// Week 1: Mark critical components as @Locked
// Week 2: Mark semi-critical as @Protected
// Week 3: Mark customizable as @AIModifiable// 1. Setup
const engine = new RuntimeEngine(customConstraints);
// 2. User query
const userQuery = "Make the dashboard cards bigger";
// 3. AI generates code
const aiCode = await callAIAPI(userQuery);
// 4. Validate and apply
const result = await engine.processModification({
userQuery,
generatedCode: aiCode
});
// 5. Handle result
if (result.success) {
hotReloadComponent(result.appliedCode);
showSuccess("UI updated!");
} else {
showErrors(result.validation.errors);
}- Fine-grained permissions: Per-user constraint profiles
- ML-based threat detection: Learn dangerous patterns over time
- Visual diff preview: Show changes before applying
- A/B testing integration: Test AI modifications with subset of users
- Undo/redo stack: Full history with time-travel debugging
RuntimeUI provides a secure, controlled environment for AI coding agents to modify UI at runtime while protecting:
✓ Critical business logic (locked components) ✓ Sensitive data and APIs (guards and proxies) ✓ Application stability (validation and constraints) ✓ User safety (rollback mechanism)
The framework is reusable, extensible, and production-ready for any AI-assisted UI application.