Runtime
The runtime is the execution engine that runs protocols, enforces rules, and manages state transitions.
Overview
The R-Think runtime is responsible for executing reasoning protocols. It manages the lifecycle of each step, applies validation rules at runtime, and captures artifacts for audit.
The runtime operates in three phases:
- Preparation - Validates input and initializes state.
- Execution - Runs protocol steps with rule enforcement.
- Completion - Aggregates artifacts and returns the result.
Creating a Runtime
Use the Runtime factory to create a configured runtime instance.
runtime-create.ts typescript
import { Runtime } from '@r-think/core';
const runtime = Runtime.create({
id: 'my-runtime',
protocol: Protocol.sequential,
rules: [Rule.required('input')],
artifacts: [Artifact.trace(), Artifact.metrics()]
});
const result = await runtime.execute({ input: 'data' }); Execution Model
R-Think supports two execution models: sequential and parallel. Sequential execution runs steps one after another. Parallel execution runs independent steps concurrently.
execution-models.ts typescript
// Sequential execution
const sequential = Protocol.define({
steps: [stepA, stepB, stepC]
});
// Parallel execution
const parallel = Protocol.define({
steps: [stepA, stepB, stepC],
mode: 'parallel'
});
// Conditional execution
const conditional = Protocol.define({
steps: [
{ condition: (ctx) => ctx.type === 'fast', action: fastPath },
{ condition: (ctx) => ctx.type === 'slow', action: slowPath }
]
}); Error Handling
The runtime provides structured error handling. Errors are categorized by type and include the full reasoning trace up to the point of failure.
Error Types
RuleViolation, ProtocolError, TimeoutError, and StateError are the four error types emitted by the runtime. Each includes a code, message, and trace context.