Dual-System Architecture for Plugin-Based AI Agent Runtimes
As AI agent applications transition from prototypes to production, a minimal execution loop is no longer sufficient. This article explores a dual-system architecture for plugin-based agent runtimes, separating system composition via a Runtime Plugin Graph from execution history via an Append-only Event Stream. We discuss capability visibility, lazy dependency startup, lifecycle management, and how the agent loop bridges these two systems to build scalable, recoverable, and observable runtime frameworks.
Dual-System Architecture for Plugin-Based AI Agent Runtimes
As AI agent applications transition from demonstrations to production, many teams initially implement a minimal closed loop: stitching prompts, calling models, executing tools, and passing results back to the model. While this loop is sufficient for validating ideas, it falls short of supporting a complete production-grade runtime framework. A real system must handle model and credential management, session recovery and context compression, tool policies and approvals, sandboxing and remote execution, multi-tenant access, sub-agent collaboration, plugin lifecycle management, and UI observation of the same runtime facts. Stuffing all this complexity into a single “agent loop” quickly makes the system unmaintainable.
A more reasonable approach is to separate “what the system is currently composed of” from “what the system just did.” The former is maintained by a runtime plugin graph, while the latter is maintained by an append-only event stream. The agent loop sits between the two, responsible for fetching capabilities and writing the execution process back to the fact stream. This article explores this dual-system architecture, discussing the plugin graph’s scope, dependencies, lifecycle, and configuration mounting, as well as the event stream’s recording, projection, and recovery mechanisms, and how they jointly support a production-grade runtime.
I. Why a Complete Runtime Requires More Than Just an Agent Loop
The minimal agent loop only cares about four things: how prompts are assembled, how the model is called, how tools are executed, and how results are returned. Its advantage is its intuitiveness; its disadvantage is the assumption that all dependencies are ready, all history is readily accessible, and all tools are directly callable. A production-grade runtime must answer more questions: Where do model credentials come from? How do we recover after a session interruption? How do we compress context when it exceeds limits? Do tool calls require approval? How is remote execution isolated? How do multiple tenants share the same set of capabilities? Who cleans up listeners and processes after a plugin is unloaded? How does the UI see the same consistent facts as the recovery logic?
Therefore, an evolvable runtime framework typically requires two parallel systems. The plugin graph solves the “composition” problem: what capabilities currently exist, what they depend on, which scope they operate in, and how they are cleaned up upon unloading. The event stream solves the “history” problem: what actually happened during an agent task, and how to project the data needed for model context, UI trajectory, recovery, and forking. The agent loop is not the entirety of the architecture; it is the orchestration layer between the two systems.
II. Runtime Plugin Graph: Describing System Composition
The core of the plugin graph is not about having “many packages” or “entry functions for every plugin,” but about using a single mechanism to handle five problems that often become disconnected.
1. Capability Visibility: What a Plugin Can See at Its Current Location
Plugins should not have default access to all global capabilities. A more robust approach is to use scope and dependency declarations so that the plugin context only exposes the capabilities visible at its current location. This prevents global singleton pollution and allows different tenants, sessions, and hosts to have different capability views. Capability visibility is the first boundary for plugin isolation.
2. Lazy Dependency Startup: Whether to Start When Required Capabilities Are Absent
After a plugin declares its required capabilities, the loader should not recklessly start it if dependencies are missing. It can wait for dependencies to appear, suspend the plugin, or fail gracefully according to policy with diagnostic feedback. The value of lazy startup is that the system can mount base capabilities first and then incrementally mount upper-layer plugins based on configuration, preventing startup order from becoming an implicit convention.
3. Scope Resolution for Same-Named Capabilities
The same capability identifier may resolve to different implementations in different sessions or scopes. For example, a test session uses a mock implementation while a production session uses the real one; a sub-agent uses a restricted toolset while the main agent uses the full toolset. The plugin graph needs to support scope chain resolution so that same-named capabilities find the correct implementation in the right context, rather than relying on global registry overrides.
4. Lifecycle Cleanup: Who Reclaims Registrations, Listeners, Processes, and Handles
When a plugin exits, resources such as registrations, event listeners, child processes, and file handles must have clear ownership. A common practice is for plugin initialization to return a disposable object uniformly reclaimed by the composition root; the plugin context provides a registration interface where all resources are attached to the plugin’s lifecycle. Without a unified cleanup mechanism, hot-plugging and fault isolation degenerate into process restarts.
5. From Static Config to an Updatable, Inspectable Runtime Plugin Tree
At startup, a fixed application should not be constructed directly. Instead, configuration profiles, capability packs, and user patches should be read, and the loader should mount them incrementally into plugin instances. A trackable relationship is maintained between config lines and plugin instances, allowing the runtime plugin tree to be inspected, updated, and diagnosed. Configuration is not a one-time script but the declaration source for the runtime graph.
interface Plugin {name: stringscope?: ScopeIdrequires?: CapabilityId[]setup(ctx: PluginContext): Disposable}interface PluginContext {resolve<T>(id: CapabilityId, scope: ScopeId): Tregister(disposable: Disposable): void}
III. Append-Only Event Stream: Recording What the System Just Did
The event stream is an append-only log: every model call, tool execution, approval result, and state change is appended as an event without directly rewriting history. It records what actually happened during an agent task and generates data for model context, UI trajectory, recovery, and forking through projections. Events need to include a sequence number, type, scope, causal chain, and necessary payload to facilitate replay and diagnostics. Context compression can generate snapshot events, allowing recovery to be reconstructed from the snapshot plus subsequent events. The UI and recovery logic should read from the same source of truth to avoid dual-write inconsistencies. In general cloud environments like Baidu AI Cloud, the event stream can integrate with log services, monitoring alerts, and container platforms to form an observable runtime fact chain.
IV. Agent Loop: Bridging the Two Systems
The agent loop retrieves models, tools, prompts, and session services from the plugin graph, and then writes the execution steps back to the event stream. Policy checks before tool calls, approvals, sandbox execution, failure retries, and sub-agent derivation should all be orchestrated in the loop, but their specific implementations are provided by plugins. The loop itself should remain thin, leaving complex composition to the plugin graph and historical continuity to the event stream. This way, replacing a model adapter, adjusting tool policies, or adding a host does not require rewriting the core loop.
V. Engineering Best Practices
First, plugin contracts must be stable; capability identifiers, dependency declarations, and disposal semantics must be clear. Second, scope should be a first-class concept to prevent global state leakage. Third, all resources must be brought under lifecycle management for controllable hot-plugging. Fourth, event schemas should be versioned, and projection logic must be reconstructable. Fifth, security policies must shift left; tool approvals and sandbox isolation cannot rely solely on the UI layer. Sixth, the mounting process from configuration to the runtime graph must be diagnosable to easily locate missing dependencies and scope errors.
VI. Conclusion
The main body of a production-grade agent runtime is not a single agent loop, but a dual-system composed of the “Runtime Plugin Graph + Append-only Event Stream.” The plugin graph answers what the system is composed of, the event stream answers what the system just did, and the agent loop fetches capabilities and records facts between the two. Delegating composition complexity to the plugin graph and historical continuity to the event stream is the key to building an extensible, recoverable, and observable runtime framework.
