Dynamic Composition in Agent Runtimes: Letting Long-Running Agents Evolve Without Crashing
This article explains why long-running AI agents cannot rely on restarts to recover from failures, since their sessions and memory must persist. It describes a dynamic composition runtime that tracks plugins across both structure and lifetime, using a shared context, dependency graphs, and lifecycle-bound side effects. It also covers hot updates, event logs, and controlled change flows that let an agent modify its own runtime safely.
Why Long-Running Agents Cannot Solve Problems by Restarting
Traditional software has one universal fix: restart. State is corrupted? Restart. A plugin crashed? Restart. An upgrade failed? Delete the container and bring it back up. This approach works because the normal running state of traditional software can be rebuilt cheaply. As long as the code and configuration remain, a restarted system returns to a consistent starting point.
Long-running agents break this assumption:
- Sessions cannot be lost. A conversation context that spans days or even weeks is an asset the agent has built up with the user. A restart resets it to zero.
- Memory cannot be cleared. Long-term memory, learned user preferences, and accumulated task knowledge are valuable. Clearing them means the agent forgets everything and has to learn again.
- In-progress tasks cannot be restarted from scratch. An agent may be running a multi-step workflow. Rolling back everything because a tool was swapped midway is too costly.
- A harder question is who fixes what. If the agent itself broke the runtime, “restart and fix it later” may leave no stable point for a human to take over. After the restart, an agent with the same broken configuration comes back to life.
So the core question becomes: when an agent can install its own tools, replace memory modules, create sub-agents, and even modify its own runtime environment, how does the system ensure it does not break itself during self-evolution?
Two Core Problems: How a Component Connects, and How It Lives
Once every capability in the runtime (tools, memory, loop controllers, sandboxes, and so on) is abstracted as a pluggable “component,” two intertwined problems appear.
The spatial dimension: how components connect
Components are not isolated. A web scraping tool may depend on an HTTP client and a cache. A sub-agent may depend on a memory module and a tool registry. If load order and dependency resolution rely on convention or coincidence, removing any single component can leave another component holding a dangling reference and crashing.
Traditional plugin systems usually use declared dependencies plus unified assembly at startup. That is not enough for dynamic scenarios. Dependencies may appear only at runtime, and they may change while the system runs.
The temporal dimension: what happens while a component is alive
This dimension gets less attention. After a component is unloaded, who cleans up the scheduled tasks it created, the callbacks it registered, the database connections it opened, and the background coroutines it started? After a component loads, when are its assumptions about the environment (a config exists, an upstream service is available) checked again?
Stated more formally, composability must cover both the current structure (space) and the ongoing lifetime (time). This is spatio-temporal composability. Handling only the spatial side yields clean architecture diagrams and a pile of memory leaks. Handling only the temporal side yields scattered side effects that nobody cleans up.
The Runtime Answer: Let It Track What Exists and What Depends on What
The core of a dynamic composition runtime is to have the framework itself do the bookkeeping. A typical implementation has three elements.
1. A unified context object
All components attach to the same context, and their lifecycle events (loaded, ready, unloaded, failed) are broadcast through it. Components do not hold direct references to each other. They communicate indirectly through the context, which provides a natural isolation layer for replacement.
const ctx = rootContext.child();ctx.plugin(memoryPlugin); // Attach the memory modulectx.plugin(toolPlugin, { // Attach the tool, declaring its dependencyrequires: ['memory'],});// On disposal, the context triggers cleanup and notifies dependentsawait ctx.dispose();
2. Automatic dependency graph tracking
When plugins declare dependencies, the context builds a dependency graph. On unload, it propagates along the graph in reverse. Before unloading memory, it checks whether any active dependents remain. If so, it either cascades the unload or rejects the operation. This turns the failure mode “a component leaves while others still hold its reference” from a runtime crash into a deterministic decision made by the framework.
3. Lifecycle binding for side effects
Timers, event listeners, and child processes registered by a plugin at startup should be bound to that plugin’s context. When the plugin unloads, the framework reclaims these resources in bulk. This addresses the temporal dimension: when a component dies, its traces should disappear with it, or be explicitly handed off.
Load plugin A└─ A registers listener / timer / subtaskUnload A├─ The framework walks A's list of side effects├─ Cancels, closes, and reclaims each item└─ Notifies B, which depends on A, to degrade or cascade the unload
Three Practical Points for Engineering
When applying this pattern to real agent systems, three lessons stand out:
Hot updates instead of cold restarts. Use module-level hot replacement so that changing a tool’s implementation does not touch the session, the memory, or the running loop. The cost is strict separation between replaceable code and state that must not be lost. State must live outside the code, in a persistence layer, before code can be swapped freely.
An event log as a replayable foundation. Record every key runtime event (plugin loads, configuration changes, task dispatches) as an append-only log. If the runtime gets corrupted, replaying the log rebuilds the state instead of hoping that memory is still intact. This is the substitute for restarting in long-running systems: rather than restarting the process, you rebuild consistency.
Guardrails on the agent’s self-modification. Let the agent propose changes, but route them through the same declare, validate, apply flow used for human operations. The agent first declares its intent. The framework checks dependencies and resources. Then the change takes effect atomically, and failures roll back automatically. The safety of self-evolution comes from the controlled nature of the change channel, not from trusting the agent.
Summary
Dynamic composition is not the same as a plugin system. Its real goal is to keep the runtime fully aware of who is present, who has left, what was left behind, and who still depends on whom. This lets long-running agents keep evolving without losing state or interrupting tasks. Once the spatio-temporal composability layer is understood, many architectural decisions in agent frameworks that look like implementation details (externalized state, event logs, cascading unloads) turn out to be the same idea applied in different places.
