Skip to main content

> GUIDE // RUNTIME

Runtime Engine & State Synchronization

State-synced base classes, callable RPC methods, durable fibers, sessions, compaction trees, and task scheduling.

Executive Overview

Building robust agents requires a rock-solid distributed runtime. Discover how stateful Agent classes manage reactive state mutations, background FIFO task queues, token compaction trees, and durable fiber execution.

1. The Stateful Agent Base Class & RPC

In modern agent runtime engines, each agent instance acts as an isolated actor with identity, persistent storage, and network interfaces:

  • Instance Identity: Bound to unique workspace, user, or session IDs.
  • Callable RPC Methods: Type-safe methods exposed to WebSocket and HTTP clients without manual route registration.
  • Reactive State Persistence: Mutations to this.state automatically trigger SQLite write-ahead logging (WAL) and emit deltas to connected clients.
Universal Stateful Agent Class Pattern
export interface AgentState {
  sessionId: string;
  status: "idle" | "reasoning" | "executing_tool" | "waiting_for_input";
  memoryContext: Record<string, unknown>;
  tokensUsed: number;
}

export abstract class BaseAgent {
  protected state: AgentState;

  constructor(public readonly id: string) {
    this.state = this.loadInitialState(id);
  }

  // Atomically mutates state and notifies connected clients
  protected setState(updater: (prev: AgentState) => Partial<AgentState>): void {
    const patch = updater(this.state);
    this.state = { ...this.state, ...patch };
    this.persistState(this.state);
    this.broadcastStateDelta(patch);
  }

  abstract onMessage(message: string): Promise<string>;
}

2. Sessions & Token Compaction Trees

To prevent infinite turn histories from exceeding LLM context windows, runtime engines implement Tree Compaction:

  1. Turn Branching: Turns are recorded in an append-only DAG, supporting rollbacks and alternative exploration branches.
  2. Threshold Watchdog: When conversation token counts exceed 75% of context window capacity, an automated background job summarizes older turns into compact semantic memory checkpoints while preserving critical system instructions.

Frequently Asked Questions

How do durable fibers survive compute restarts?

At every network I/O boundary or sleep interval, the fiber checkpoints its call stack, variables, and step index to disk. When the worker resumes on new compute, it restores state and continues from the exact execution boundary.

AI Summary

Building robust agents requires a rock-solid distributed runtime. Discover how stateful Agent classes manage reactive state mutations, background FIFO task queues, token compaction trees, and durable fiber execution.