R-Think

Runtime

The runtime is the execution engine that runs cognitive missions, enforces loop rules, and manages state transitions across the canonical algorithm steps.

Overview

The R-Think runtime is responsible for executing cognitive protocols. It manages the lifecycle of each step, applies validation rules at runtime, captures artifacts and evidence, and ensures the mission transitions only when the required governance has been satisfied.

The runtime operates in three phases:

  1. Preparation — Validates input, initializes mission state, and selects the adaptive depth.
  2. Execution — Runs protocol steps with state machine control, rule enforcement, and artifact capture.
  3. Completion — Aggregates artifacts, validates evidence sufficiency, determines authority acceptance, and returns the result.

Creating a Runtime

Use the Runtime factory to create a configured runtime instance for a mission.

create-runtime.ts typescript
import { Runtime, Protocol, Rule } from '@r-think/core';

const mission = Runtime.create({
  id: 'my-reasoning-mission',
  protocol: Protocol.sequential,
  rules: [
    Rule.required('observation'),
    Rule.type('observation', 'string'),
    Rule.minLength('observation', 1)
  ],
  adaptiveDepth: 'L1'
});

const result = await mission.execute({
  observation: 'Payment gateway timeout exceeded 5s threshold'
});

console.log(result.validated);  // true
console.log(result.trace.id);   // trace identifier
console.log(result.output);

Execution Model

R-Think supports three execution modes through protocol configuration:

  • Sequential — Steps run in canonical algorithm order. Use for ordered dependencies.
  • Parallel — Independent steps run concurrently. Use when steps do not depend on prior state.
  • Conditional — Step selection is based on runtime conditions from earlier steps.
execution-models.ts typescript
// Sequential execution (canonical)
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.priority === 'high', action: fastPath },
    { condition: (ctx) => ctx.priority === 'normal', action: standardPath }
  ]
});

Error Handling

The runtime provides structured error categories. Each error includes a code, message, step reference, and reason code.

Error Types

RuleViolation — A validation rule failed before or during execution. Execution halts. StateError — The mission attempted an invalid state transition. TimeoutError — A step exceeded the configured timeout. EvidenceInsufficient — The artifact or evidence at the current step does not satisfy the adaptive depth requirement.
error-handling.ts typescript
try {
  await mission.execute({ observation: '' });
} catch (error) {
  console.log(error.code);        // 'RuleViolation'
  console.log(error.field);       // 'observation'
  console.log(error.reasonCode);  // 'RULE_MIN_LENGTH'
  console.log(error.message);     // Human-readable explanation
}