Plugin-Based Agent Harness Architecture: Runtime Plugin Graph and Event Stream
This article breaks down a plugin-based Agent Harness architecture built around two systems running in parallel: a runtime plugin graph that defines what the system is composed of, and an append-only event stream that records what it has done. It explains why packaging capabilities alone is insufficient, how layered configuration trees enable capability replacement without code changes, and how dependency scopes, startup semantics, and lifecycle cleanup work in practice. Readers will find engineering recommendations for validation, replay-based testing, least privilege, gradual rollout, and unified observability.
1. Background: Why “Many Plugins” Does Not Explain a Harness
When building intelligent coding tools or enterprise-grade agent platforms, the first challenge teams face is usually not model capability but combinatorial complexity. Model adapters, system prompts, tool catalogs, session state, storage, sandboxes, the agent main loop, web interfaces, approval policies, and sub-agents all evolve rapidly. If every new capability requires changes to the main program, the system quickly degrades into a pile of conditional branches.
A common industry practice is to split capabilities into packages and give each package an initialization entry point. However, “many packages plus initialization functions” alone does not solve five key problems in engineering practice:
- Which capabilities a plugin can actually see at its current location;
- Whether a plugin should start, wait, or fail when a required capability is not yet ready;
- Whether the same service name can resolve to different implementations across sessions, tenants, or scopes;
- Who is responsible for reclaiming registry entries, event listeners, child processes, and file handles when a plugin exits;
- How static configuration becomes a runtime plugin tree that is updatable, observable, and inspectable.
A mature agent runtime framework must answer both “what the system is composed of right now” and “what the system just did.” The former is maintained by the runtime plugin graph; the latter is maintained by an append-only event stream. The agent main loop sits between them: it obtains the model, tools, prompts, and session services from the plugin graph, then writes each execution fact back to the event stream.
2. Overall Architecture: Two Systems Running Together
2.1 Runtime Plugin Graph: What the System Is Composed Of Now
The plugin graph is a directed dependency graph. Nodes are plugin instances, and edges represent capability provision and consumption. Each node declares which services it provides, which services it requires, the scope in which it takes effect, and its cleanup policy on unload. The runtime uses this information to perform assembly, validation, and scheduling.
A schematic plugin declaration can be abstracted as:
// Illustrative structure only, not a specific product interfaceconst plugin = {name: "model-adapter",provides: ["modelService"],requires: ["credentialService", "configService"],scope: "session",apply(ctx) {ctx.provide("modelService", createModelAdapter(ctx.config));return () => ctx.dispose("modelService"); // Cleanup on unload}};
The key point is that plugins do not look each other up through global singletons. Instead, they resolve dependencies by scope through a context object. This allows the same tool plugin to bind to different sandboxes and credentials in different sessions.
2.2 Append-Only Event Stream: What the System Just Did
The event stream is append-only and never modified in place. User messages, model requests, tool calls, tool results, policy approvals, compaction summaries, errors, and retries are all persisted as events. Its value comes from three properties:
- Recoverability: after a process restart, replaying the events rebuilds the session;
- Forkability: a new branch can be copied from any event position to compare different prompts or models;
- Projectability: model context, UI traces, and audit reports are different projections of the event stream, rather than four separate states that must be kept in sync.
2.3 Agent Main Loop: The Bridge Between the Two
The main loop itself should stay thin: assemble context, call the model, parse tool calls, execute tools, and write events back. Everything about “which model to use,” “whether a tool is allowed to run,” and “where state is stored” is resolved through the plugin graph. This means replacing a capability does not require changing the loop code.
3. Configuration Tree: The Core Mechanism for Replacing Capabilities Without Code Changes
3.1 Layered Overlay
At startup, the system reads a base profile, optional bundles, and then overlays a user patch. Finally, the loader mounts configuration entries one by one as plugin instances. Layering ensures that defaults, environment differences, and personal preferences do not overwrite source code:
# Base layer: default capability setplugins:- id: modelimpl: model-adapteroptions: { provider: default }- id: toolsimpl: tool-registryoptions: { allow: [read, search] }# User patch layer: override only the differencespatch:model.options.provider: enterprise-gatewaytools.options.allow: [read, search, write]
Replacing a model, switching storage, or enabling a sandbox is essentially changing one line in the configuration tree rather than modifying the main loop.
3.2 Dependency and Scope Resolution
The same service name can point to different implementations in different scopes. Common scopes include global, host, session, and sub-agent. The recommended resolution rule is “nearest wins, explicit override, missing means error,” which avoids implicit global state.
3.3 Startup Semantics: Wait or Fail
For missing required dependencies, the framework should support three strategies: fail immediately, wait for a bounded time, or degrade to optional. In production, immediate failure is recommended for hard dependencies such as credentials and storage, while optional degradation is recommended for soft dependencies such as telemetry and caching, to prevent indefinite hanging.
3.4 Lifecycle and Cleanup
When a plugin is unloaded, it must idempotently release four types of resources: registry entries, listeners, child processes, and handles and timers. A practical convention is “whoever registers, unregisters,” with the context collecting cleanup functions and executing them in reverse order when the scope is destroyed.
4. Engineering Practice Recommendations
- Make configuration inspectable: output the resolved plugin tree and dependency gaps before startup to facilitate review;
- Make events replayable: use the event stream for regression testing instead of testing only prompts;
- Enforce least privilege: combine tool allowlists, approval policies, and sandboxes as independent plugins;
- Roll out gradually: mount a new model or tool to a shadow scope via the patch layer first, then switch the main path;
- Unify observability: project logs, metrics, and traces from the event stream to reduce state divergence.
When implementing a similar architecture on Baidu AI Cloud, the model gateway, object storage, message queue, container platform, logging service, and monitoring and alerting can each be encapsulated as plugin implementations and switched by environment through the configuration tree, while keeping the main loop and event stream unchanged. This satisfies multi-environment delivery and allows capability replacement, auditing, and recovery to be built on a unified, verifiable runtime foundation.
