Plugin Lifecycle and Collaboration in an Agent Framework
A plugin-based agent runtime is easy to assemble and hard to tear down cleanly. This article explains why plugin install is a chain of runtime side effects, then describes three mechanisms—registered disposal, declared dependencies, and event-driven collaboration—that keep hot reload and large plugin sets from leaving residual state.
Introduction: Why “Everything Is a Plugin” Needs a Safety Net
A common design idea in modern agent runtimes is that everything is a plugin and everything is replaceable. Tools, system prompts, model adapters, and context management are assembled dynamically into a running host, called the Harness below. The benefits are clear: capabilities can be composed, hot-reloaded, and trimmed on demand.
To make that idea work, one sharp question has to be answered: installing a plugin is easy, but what guarantees that removing it leaves no residue? When a runtime hosts dozens of plugins and also supports hot reload, any gap in cleanup slowly fills the host with leftover state. This article starts from what a plugin actually does after it is installed, then presents three engineering mechanisms.
1. Installing a Plugin Is Not Copying a File
A common misconception is that installing a plugin means dropping a file into a directory. Installation is a series of side effects the plugin performs inside a running Harness. A tool plugin that provides command execution typically does the following during install:
- Register a tool entry so the model can see it in the available tool list and invoke it.
- Append guidance to the system prompt, for example explaining what certain return markers mean so the model can interpret execution results correctly.
- Attach event listeners, such as handlers for call timeouts and result callbacks.
The tool list and system prompt the model eventually sees are themselves the concatenated traces left by every installed plugin. That is the essence of request assembly: the Harness does not send a static configuration to the model. It aggregates the fragments contributed by each plugin at runtime.
Side effects produced by plugin install:registry.addTool("run_command", handler) // register a toolprompt.append("Command results include status markers...") // contribute prompt textbus.on("tool:timeout", onTimeout) // listen for eventsstartTimer() // may also start a timeropenFile() // may also hold a handle
2. The Uninstall Problem: Where Residue Comes From
Once install is understood as a chain of side effects, uninstall becomes difficult. Removing the plugin above cleanly means reversing every action exactly:
- Delete the tool-list entry. Otherwise the model still sees a ghost tool whose executor no longer exists, and calls fail.
- Retract the appended prompt text. Otherwise it misleads the model.
- Detach event listeners. Otherwise callbacks keep firing, causing memory leaks and logic errors.
- Stop timers and close file handles. Otherwise resources stay occupied.
The naive approach is to ask each plugin author to write two blocks: what install does and what uninstall undoes, with pairing left to human care. That approach has two fatal weaknesses:
- It is easy to miss a step. Install may do three things while uninstall only undoes two. The missed step becomes residue. As plugin count and release frequency grow, omissions become almost inevitable.
- Shared state is hard to handle. If two plugins register content with the same shared service, the plugin that uninstalls first may remove something the later plugin still uses, or it may refuse to remove anything so that nothing is ever cleaned up.
When a runtime hosts dozens of plugins and must support hot reload—one line of code taking effect immediately—“cleanup depends on author discipline” collapses.
3. Mechanism 1: Register the Undo at Install Time
A mature engineering fix is simple: do not ask authors to write install and uninstall separately. Every install action should hand over its matching undo when it runs.
The framework gives plugins a context in which registration also records cleanup. When a plugin calls a registration API, the API both completes the registration and records how to reverse that registration on the plugin’s cleanup list. On uninstall, the framework runs the recorded cleanups in reverse order. The author writes no uninstall logic by hand.
// Pseudocode: every side effect returns or records its inversectx.registerTool("run_command", handler) // internally records: removeTool on uninstallctx.appendPrompt("...") // internally records: removePrompt on uninstallctx.on("tool:timeout", onTimeout) // internally records: off on uninstallctx.effect(() => { const t = startTimer(); // author-defined side effectreturn () => clearTimer(t); }) // returns a cleanup function// On uninstall: the framework walks the cleanup list in reverse order
The gain is that pairing is guaranteed by the mechanism, not by the person. If a side effect is created through the framework context, its undo is recorded. There is no way to “forget to write uninstall.” For shared services, the framework uses reference counting or ownership tracking to decide when a resource is actually released, avoiding accidental teardown.
4. Mechanism 2: Declared Dependencies and Readiness
Plugins often depend on one another: A needs a service provided by B. If A polls until B is ready, the code fills up with races and timeout checks. A more robust approach is for a plugin to declare what it needs. The framework starts the plugin only after those dependencies are ready, and pauses or reclaims it when a dependency disappears.
This combines dependency injection with lifecycle orchestration. The framework maintains a dependency graph and activates plugins in topological order. When a depended-on service is hot-reloaded or uninstalled, the framework automatically restarts or stops downstream plugins. Authors write what they need, not how to wait for it.
5. Mechanism 3: Event-Driven Collaboration
Plugins should not hold direct references to one another and hard-code calls. That coupling makes replacement expensive. They communicate through an event bus. Typical collaboration patterns include:
- Broadcast notification: one plugin emits an event; any number of listeners respond. The sender does not care who is listening.
- Request–response: a request is sent and waits for a handler to return a result, decoupling caller from implementation.
- Intercept or filter chain: several plugins process the same data in sequence, forming a chain of responsibility.
- Aggregation: contributions are collected from all participants—for example, each plugin contributing a prompt fragment—and then merged.
- Negotiation: multiple handlers compete or vote on the same request, and the framework decides by priority.
Event communication lets plugins collaborate without knowing each other. That is a prerequisite for “everything is replaceable”: swapping one implementation does not affect other listeners.
6. Outlook and Practical Advice
The value of these mechanisms is not in any single plugin. It is maintainability under large-scale assembly. As a plugin ecosystem grows, the system avoids rotting across iterations only when uninstall leaves no residue, dependencies are orchestrated automatically, and collaboration stays loosely coupled.
Teams building a similar architecture can apply a few practices directly:
- Funnel all side effects through the framework context. Do not let plugins touch global state directly, so untraceable residue cannot arise in the first place.
- Replace imperative waiting with declarative dependencies, and leave lifecycle orchestration to the framework.
- Prefer the event bus, so replacement and extension remain possible.
At implementation time, this kind of plugin runtime can sit on ordinary cloud capabilities: object storage for plugin artifacts, a container platform for elastic scaling of the runtime, and logging plus monitoring and alerting to observe plugin load, unload, and event flow. General-purpose services on a platform such as Baidu AI Cloud can serve as infrastructure for running a plugin ecosystem at scale, so that hot reload and replaceability hold up in production.
Conclusion
“Everything is a plugin and everything is replaceable” is a slogan. What actually supports it is three plain engineering agreements: record how to undo an action at install time, let plugins declare what they need, and let plugins collaborate through events. Those three points are why a plugin system can dynamically assemble dozens of plugins at runtime without being dragged down by leftover state.
