Plugin Architecture in Practice: From Debugging Errors to Runtime Module Resolution
This article explains how plugin-based agent frameworks work in practice, using a "base bundle" as an example to show why the core itself is just another plugin. It walks through a layered method for diagnosing "module not found" errors, clarifies the boundaries between build, package, and runtime configuration, and compares source-based versus build-artifact startup paths. It also covers workspace protocols, publishing checklists, user-level profile isolation, and the full runtime resolution chain, ending with a concise mental model for adopting the architecture in a team.
1. Why You Should Understand “Everything Is a Plugin” First
When developers first work with a plugin-based agent framework, they instinctively look for the line between “core” and “extension”: which parts the framework must provide as a foundation, and which parts users are allowed to replace. Real designs are usually more radical than that. There is no privileged kernel that needs patching. Model adapters, tool registration, session logging, main-loop scheduling, subprocess execution, sandboxing, and approval policies all exist as plugins, and runtime behavior is composed from a configuration tree.
Take a bundle commonly named the “base package” as an example. The word “base” in its name makes it easy to mistake for a low-level library that you import. In fact it is a bundle: its package manifest declares a bundle marker, and it ships a patch-style configuration that inserts dozens of plugin entries into an otherwise empty configuration tree. That configuration roughly covers these capability domains:
- Scheduled tasks and hot reload
- Model capability abstraction and concrete model adapters
- Session event logging and in-memory storage
- Agent interfaces and the default driver (main loop)
- Tool registry and prompt assembly
- Subprocess and cross-platform shell execution
- Sandbox isolation and human approval
- Filesystem capabilities and access policies
- User settings and credential management
- Sub-agents and workflow orchestration
- Web search and page fetching
Making the foundation a plugin pays off structurally. First, replacement cost is minimal: swapping a model adapter requires no change to the main loop. Second, capabilities can be trimmed: a minimal runtime loads only the plugins it needs. Third, evolution can be gradual: hot reload lets new policies ship one plugin at a time. Fourth, test boundaries are clear: every plugin can construct its own dependencies and assert its own behavior. The price is a longer debugging path. An error may originate in configuration, dependency resolution, build output, or runtime loading, so you need a layered localization method.
2. Your First Plugin: Treat Editor Errors as Layered Signals
The most common frustration when following a tutorial is a screen full of red in the editor with only one message: module not found. The effective response is not to tweak configuration blindly, but to rule out four layers in order:
- Whether the dependency is actually installed locally: the package manager’s install result, whether the lockfile was updated, and whether duplicate copies exist.
- Whether dependency declarations are complete: runtime and dev dependencies mixed together, or a types-only package left undeclared.
- Whether type resolution is reachable: whether the types entry field, export map conditions, and module resolution mode match the dependency’s published shape.
- Whether the editor’s own cache is stale: when the language service index is outdated, reopening the workspace or rebuilding the index restores it.
A minimal illustrative plugin skeleton follows. The point is the export contract and dependency declarations, not any specific framework API:
// src/index.tsexport interface PluginContext {register(tool: { name: string; run: (input: unknown) => Promise<unknown> }): void;}export const meta = { name: "demo-plugin", version: "0.1.0" };export function apply(ctx: PluginContext) {ctx.register({name: "echo",async run(input) { return input; },});}
{"name": "demo-plugin","version": "0.1.0","type": "module","main": "./dist/index.js","types": "./dist/index.d.ts"}
The key diagnostic judgment is this: if the command-line build succeeds but the editor shows red, the problem is in type resolution or the language service; if both fail, the problem is in dependencies and declarations; if the build succeeds but the module cannot be found at runtime, the problem is in build output and loading paths. The three conclusions are mutually exclusive, which significantly shortens localization time.
3. Clarifying the Semantic Boundaries of Configuration Files
Three kinds of configuration are easily conflated. Separating them clears up most confusion:
- Build configuration: determines how source is checked and downleveled. Key items are the target syntax version, module output format, declaration generation, strict mode, and path aliases.
- Package manifest: determines how others find your artifacts. Key items are entry fields, export maps, the file list, and the publish scope.
- Runtime configuration: determines which plugins the framework loads, in what order, and with what injected parameters. It is usually generated by merging patch-style configurations into a final configuration tree.
A frequent pitfall is path aliases. Compile-time aliases only take effect during type checking and bundling; they are not rewritten automatically at runtime. So either resolve aliases at build time or provide an explicit mapping in the runtime configuration. The two must appear as a pair, otherwise you get “compiles fine, fails to start.”
4. What the Build Actually Does
A typical build command is usually orchestrated in four steps: clean old output, run type checking, produce runtime artifacts with a bundler, and generate type declarations. Understanding this pipeline lets you bisect failures quickly. A failure in the type-checking stage is a semantic problem; a failure in the bundling stage is usually resolution or circular dependencies; a failure in declaration generation often comes from exported types referencing internal types that are not visible.
The practical recommendation is to decouple type checking from artifact generation. The former serves development feedback and must be fast; the latter serves delivery and must be stable and reproducible. Also pin dependency versions, commit the lockfile, and run one clean build in CI. That eliminates the stubborn “works locally, fails in the pipeline” problem.
5. Two Runtime Paths: Source Startup and Artifact Startup
Plugin-based frameworks usually support two loading modes, and the difference must be recognized explicitly:
- Source path: loads uncompiled source directly, combined with transpilation and hot reload. Feedback is fast and it suits development, but it is more sensitive to module format, decorator syntax, and directory imports.
- Artifact path: loads built files, so behavior matches the published state. It suits acceptance testing and production. Stack traces point to generated files, so source maps are needed to reconstruct the original code.
Therefore “works in development, fails after release” almost always means the two paths apply different resolution rules. The sound practice is to develop on the source path, then run a smoke test on the artifact path before committing, eliminating the difference before the merge.
6. Workspace Mechanisms and Protocol-Based Dependencies
In a multi-package repository, plugins that reference each other should declare dependencies with the workspace protocol and let the package manager create local links, rather than copying code or hardcoding relative paths. This brings three benefits: the dependency graph becomes visible to tooling and can be built in topological order; changes are reflected through the links immediately, with no repeated installs; and at publish time the protocol is replaced with a real version number, keeping local and production semantics identical.
The common trap is the “phantom dependency”: code uses a package that is not declared in the manifest, and it happens to work locally thanks to hoisted installation, then fails immediately on another machine or with a different install strategy. Restricting yourself to explicitly declared dependencies is the baseline for long-term maintainability.
7. Publishing: From a Local Directory to an Installable Unit
The pre-publish checklist should include: whether the artifact contains only files required at runtime; whether entry fields and export maps point to files that actually exist; whether type declarations ship with the package; whether the version follows semantic versioning; and whether a license and changelog are included. If a plugin needs to be packaged as a self-contained unit, inline its runtime dependencies or declare them explicitly as peer dependencies, so nothing is missing after installation. After publishing, always run an “install and it works” verification in a clean environment. That step catches the vast majority of path-related problems.
8. Runtime Configuration Directory and User-Level Isolation
The framework generally maintains configuration and state under the user’s home directory. A common structure is a set of scenario-based “runtime profile” directories, for example one profile for browser interaction and another for command-line tasks. Their value is isolation: different profiles can load different plugin sets, use different models and credentials, and keep separate session histories without cross-contamination. Once you understand this, “why did my configuration change have no effect” usually has an answer: the profile you edited is not the one being launched. The engineering recommendation is to keep project-level configuration in the repository, keep only personal preferences and credentials in user-level configuration, and inject credentials through environment variables or a secret manager, never committing them.
9. The Complete Runtime Module Resolution Chain
The startup resolution order can be abstracted as: locate the entry configuration, merge patches into a configuration tree, resolve bundles and expand the plugin list, sort by dependency topology, resolve each plugin’s module entry one by one, inject context and execute registration, then enter the main loop. A missing step in any of these shows up as “the plugin did not take effect.” The localization rule of thumb is to read the list top-down and the error bottom-up: first check whether the entry exists in the list, then check which module is the first missing one in the error.
10. Mental Model and Adoption Advice
Compress the whole system into four sentences: the foundation is also a plugin, and configuration is assembly; develop on source, verify on artifacts; dependencies must be declared explicitly, and aliases must be configured in pairs; user-level profiles handle isolation, and credentials never go into the repository. To adopt this in a team, you can first build an observability loop in an environment such as Baidu AI Cloud using container platforms, log services, and monitoring alerts, turning plugin load failures, resolution exceptions, and performance regressions into measurable, traceable metrics, then gradually scale up the number of plugins.
