DeepSeek Harness Dependency-Driven Loading, Auto-Reload and Nested Context
Earlier we learned about the Fiber state machine; this article looks at how the state machine is driven by service dependencies
Why does a plugin automatically unload when a depended-on service disappears? And why can it automatically reload when the service recovers?
Dependency-driven loading
A plugin that declares inject waits for all required services to be ready before entering LOADING to execute apply
This mechanism expresses load order through service dependencies, without manually orchestrating startup order
Example
// File path: scratch-plugin/src/my-plugin.ts
// Declare that this plugin depends on two services: tools and llm
export
const
inject
=
[
'tools'
,
'llm'
]
export
function
apply
(
ctx
:
Context
)
{
// At this point ctx.tools and ctx.llm are ready and safe to use
}
If a depended-on service disappears (e.g., when a provider is replaced), the plugin is automatically unloaded, going directly from ACTIVE to DISPOSED
When the service recovers, the plugin is reloaded, going through PENDING -> LOADING -> ACTIVE again
The official docs state: if a depended-on service disappears (e.g., when a provider is replaced), the plugin is automatically unloaded (ACTIVE -> DISPOSED), and reloaded after the service recovers
Guarantees of auto-reload
Auto-reload is safe because all registrations made through ctx are revoked on unload
Listeners, tools and adapters from the old instance don't remain; the new instance re-registers
This prevents the plugin from calling services that no longer exist, and prevents duplicate listeners or duplicate registrations
Nested context: ctx.plugin()
ctx.plugin() is used to register a child plugin inside the current plugin
A child plugin creates a child Fiber, which inherits the parent context but has an independent lifecycle
The child Fiber unloads together with the parent plugin
Example
// File path: scratch-plugin/src/parent-plugin.ts
export
function
apply
(
ctx
:
Context
)
{
// Register a child plugin; childPlugin can be a function, object, or Service subclass
ctx.
plugin
(
childPlugin
)
// The child plugin has its own Fiber and unloads together with the parent plugin
}
Nested contexts are common in plugin composition: an "aggregate" plugin organizes multiple small plugins together and manages their lifecycle uniformly
Manual early termination: fiber.dispose()
In most cases a plugin unloads when its context is destroyed, but sometimes you need to terminate a plugin instance early mid-run
The Fiber object returned by ctx.plugin() provides a dispose() method
Example
// File path: scratch-plugin/src/manual-dispose.ts
import
type
{
Context
}
from
'@deepseek-ai/cordis'
// Type declarations: context and plugin function (illustrative)
declare
const
ctx
:
Context
declare
function
myPlugin
(
ctx
:
Context
)
:
void
// Create a plugin instance and get its Fiber
const
fiber
=
ctx.
plugin
(
myPlugin
)
// Call dispose later when you need to terminate manually
// Note: dispose returns a Promise; you need await to wait for async cleanup to complete
await fiber.
dispose
(
)
dispose guarantees three things:
| Guarantee | Description |
|---|---|
| All registrations are removed | All registrations owned by the plugin - listeners, tools, adapters, etc. - are revoked |
| Child plugins unload recursively | Child Fibers created via ctx.plugin() are also cleaned up |
| Wait for async cleanup to complete | The returned Promise only resolves after all async disposers have finished |
In other words, when await fiber.dispose() returns, the plugin and its entire subtree are completely at rest
Summary and self-test
Dependency-driven loading lets plugins start only when services are ready, auto-unload when services disappear and auto-reload on recovery; ctx.plugin() creates child Fibers that can unload recursively
Self-test:
- When a service declared by inject isn't ready, what state does the plugin stay in?
- What is the relationship between a child Fiber created by ctx.plugin() and the parent Fiber?
- When await fiber.dispose() returns, what can be guaranteed to have completed?