Building a Replaceable Agent Runtime with a Configuration-Driven Plugin Graph and Append-Only Event Stream

    An agent runtime that relies only on a minimal prompt–model–tool–result loop tends to accumulate hidden complexity around credentials, session recovery, tool policy, sandboxing, multiple hosts, and UI observation. This article describes an architecture that separates the runtime into two coordinated layers: an inspectable, incrementally updateable plugin graph that defines the capabilities currently available, and an append-only event stream that records what actually happened. It explains how layered configuration becomes a runtime tree, how dependencies, scopes, and lifecycle cleanup work, how session history supports replay, compaction, and forks, and where product-level concerns should live. The result is a runtime in which models, tools, modes, and hosts can be replaced without rewriting the core loop.

    An agent product that stops at a minimal loop—assemble a prompt, call a model, execute tools, and return results to the model—will soon lose control over credentials, session recovery, tool policy, sandboxing, multiple hosts, and UI observability. A safer approach is to split the runtime into two parallel tracks: one maintains which capabilities the system is composed of at this moment, and the other records what actually happened during the run. The first is an updateable, inspectable plugin graph. The second is an append-only event stream. The agent loop sits between them: it obtains model adapters, prompts, tool catalogs, and session services from the plugin graph, then writes execution back into the event stream.

    Why a Large Plugin Set Is Not Enough

    Making tools, prompts, and storage individually pluggable solves packaging, but not composition. Five concerns that engineering teams commonly separate need to be handled together by the same mechanism.

    First, visibility: the capabilities available at the current location should not be the same across the global level, a workspace, and a single session. Second, startup timing: should a plugin start when a required capability has not appeared yet? Starting too early can expose empty implementations; starting too late can block an entire dependency tree. Third, scoped resolution: can services with the same name resolve to different implementations in different scopes? Models, sandboxes, and approval policies often need this. Fourth, teardown: when a plugin exits, who releases its registrations, listeners, child processes, and handles? Missing any of these turns hot swapping into a resource leak. Fifth, configuration: how can static configuration become an incrementally updateable, inspectable runtime tree instead of an object graph fixed at startup?

    The distinguishing factor is therefore not that every package has a mount entry point. It is that configuration lines can be turned into plugin instances by a loader, and that dependencies, scopes, and lifecycles among those instances are queryable.

    From Configuration to Runtime Graph

    At startup, do not directly instantiate a fixed application. A more sound sequence is to read the environment profile and capability bundles, overlay user patches, and let the loader interpret the configuration into a plugin tree.

    The profile describes the host and default policies of the current environment, such as a local process, a remote executor, or a UI-observation-only mode. A capability bundle describes a set of plugins that can be delivered together, including their version constraints. A user patch expresses only differences: replace a model adapter, disable a class of tools, or switch storage from an in-memory implementation to a persistent one. After merging these layers, the result is still declarative configuration rather than conditionals scattered throughout the codebase.

    The loading process can be abstracted into four steps.

    1. Parse each configuration line to obtain the plugin identifier, parameters, and expected scope.
    2. Build a directed graph from dependency declarations, detecting cycles and missing required services.
    3. Instantiate within the appropriate scope and register name-to-implementation mappings in the service table.
    4. Emit readiness events so dependents can begin subscribing, instead of implicitly starting side effects in constructors.

    The structure does not need to depend on any particular framework:

    1. profile
    2. + bundle
    3. + user patch
    4. -> loader
    5. -> plugin instances
    6. -> scoped service table
    7. -> dispose hooks

    When configuration changes, compare the old and new trees, unload only the changed subtrees, and mount them again. Unchanged session scopes should remain untouched, so that a prompt adjustment does not interrupt an in-progress tool call.

    Dependencies, Scope, and Cleanup

    Visibility should use explicit context rather than global singletons. A plugin declares the service names it requires and those it optionally consumes. Resolution walks outward from the current scope: a session overrides a workspace, and a workspace overrides process defaults. This allows the same “model service” name to point to different adapters in different sessions while logging and metrics continue to share an outer implementation.

    When a required dependency is not ready, the plugin should remain in a waiting state rather than pretending to be usable with an empty object. The waiting state needs a timeout and a diagnosable reason; otherwise, the UI can only show “startup failed.” Optional dependencies allow degradation: without a remote executor, for example, the tool catalog can expose only a safe local subset.

    The teardown path must be paired with the registration path. Each plugin receives a lifecycle token when mounted, and registered listeners, timers, and child processes are attached to that token. On unload, the runtime invokes release hooks in reverse dependency order: stop dependents before stopping the services they depend on. Release failures should be written to the event stream rather than swallowed and left behind as zombie processes.

    The Session Event Stream: Continuity of History

    The plugin graph answers what the system is composed of now. It cannot answer what just happened. A session should be an append-only log: user input, model requests and responses, tool calls and results, approval decisions, compaction operations, and forks are all immutable events. The current context, UI trace, and recoverable checkpoints are projections over this log, not separate authoritative states.

    This approach has three direct benefits. During recovery, replaying events can reconstruct the model context without relying on whether in-process objects are still alive. During compaction, generate an event stating that a summary replaced an earlier segment; the original events remain auditable. During a fork, copy the log prefix up to a given event sequence number, after which the two branches append independently and do not overwrite each other.

    The agent loop should not hard-code prompt assembly and tool execution in the main program. It should request from the plugin graph the current model, system-prompt contributors, allowed tool set, and session read/write interface. Each execution step only appends events. The UI and evaluation tasks subscribe to the same log and see the same runtime facts, rather than separate cached copies.

    Where Product-Level Complexity Belongs

    The minimal loop can be short, but a complete product must cover model and credential isolation, context compaction, tool policy and human approval, sandboxing and remote execution, multiple hosts, subtasks, plugin start and stop, and how the UI observes all of this. This combinatorial complexity should be assigned to the plugin graph; continuity over time should be assigned to the event stream. If approval, sandboxing, and prompts are all packed into the loop function, every new mode requires changes to core code, and configuration loses its ability to replace behavior.

    Modes themselves can also be plugins: a standard flow, a minimal flow, and a planning-oriented flow differ in the prompt fragments, tool filters, and stop conditions they contribute to the loop, rather than branching into three separate main programs. Switching modes means replacing a set of plugins in the configuration tree and taking effect in a new session or after an explicit checkpoint. Running sessions should not hot-swap dangerous capabilities by default, so that tool semantics do not change in the middle of a call.

    Design Checklist

    Use the following questions during implementation. They do not require introducing additional product concepts.

    • Can replacing a model or storage implementation be done only through configuration and patches, without changing loop code?
    • Can the same service name resolve to different instances in two sessions while shared outer services remain unmodified?
    • When dependencies are missing, does the plugin wait, degrade, or fail, and is diagnostic information written to the log?
    • After unloading, are listeners, handles, and child processes all released through the lifecycle token?
    • Do the UI, recovery, and evaluation read projections of the same event stream?
    • Can configuration diffs be applied incrementally to the plugin tree without interrupting unchanged sessions?

    These constraints do not promise that any particular interface will remain stable. In early development, configuration schemas and event types should be treated as evolving internal protocols: externally, guarantee only that capabilities can be declared, scoped, and released, and that history can be replayed. When deploying to the cloud, object storage is suitable for event logs and checkpoints, container platforms are suitable for isolating tool execution, and logging and monitoring can observe plugin mount failures and disposal timeouts. These are general capability categories and should not be tied to the business semantics of any particular session.

    Conclusion

    The key to replaceable capabilities is not the number of plugins. It is how configuration becomes a runtime tree with dependency, scope, and cleanup semantics, and how execution history becomes an independent append-only log. The plugin graph manages composition; the event stream manages facts; the loop only schedules between them. With this boundary in place, modes, tools, models, and hosts can be replaced while recovery, auditing, and the UI still operate on the same runtime record.