DeepSeek Harness automatic cleanup and ctx.effect()
Previous chapter DeepSeek Harness loading local plugins It ends after printing one log line, but real plugins register listeners, tools, and timers.
This chapter addresses one question: when a plugin is unloaded, who cleans up these resources?
Registration is left to ctx, and cleanup is left to ctx as well.
Automatic cleanup mechanism
Anything registered through ctx - event listeners, tools, timers - is automatically cleaned up when the plugin is unloaded.
You do not need to manually call removeListener or clearInterval.
The framework can clean up automatically because all registrations through ctx are recorded in the plugin's Fiber scope.
On unload, the framework revokes them in the reverse order of registration.
Which operations are automatically tracked
The following operations are all automatically tracked and cleaned up.
| Registration operation | Behavior on unload |
|---|---|
| ctx.on(event, handler) | Event listeners are removed automatically |
| ctx.tools.register(tool) | Tool registrations are revoked automatically |
| ctx.llm.registerAdapter(names, adapter) | LLM adapter registrations are revoked automatically |
| ctx.effect(() => cleanup) | Cleanup functions (disposers) returned by execution |
ctx.effect(): handing manual resources to the framework
Some resources are not in the list above, such as a network connection.
Use ctx.effect() to tell the framework how to clean it up.
ctx.effect takes a callback that creates the resource and returns a cleanup function (disposer).
This disposer is executed when the plugin is unloaded.
disposer(cleanup function) is the return value of the ctx.effect callback.
It describes "how to destroy the resource created this time".
When the plugin is unloaded, the framework calls it.
Below is an example of a heartbeat timer:
Example
// File path: scratch-plugin/src/heartbeat.ts
import
type
{
Context
}
from
'@deepseek-ai/cordis'
export
function
apply
(
ctx
:
Context
)
{
ctx.
effect
(
(
)
=>
{
// Create a timer: print heartbeat every 5 seconds
const
timer
=
setInterval
(
(
)
=>
{
console.
log
(
'heartbeat'
)
}
,
5000
)
// The returned cleanup function runs when the plugin is unloaded
// Equivalent to: you do not need to manually clearInterval in unload logic
return
(
)
=>
clearInterval
(
timer
)
}
)
}
If nothing needs to be done on unload, the disposer can be omitted.
But once you create resources such as timers or connections, you must return the corresponding cleanup function.

Details of execution order
When a plugin is unloaded, disposers start being called in the reverse order of registration.
Multiple async disposers run concurrently, with no guarantee of completing one by one.
Cleanup steps with ordering dependencies must be placed into the disposer returned by the same ctx.effect(), which is responsible for serial waiting.
Tip: this rule means register first, clean up later - later-registered resources are cleaned up first.
Summary and self-test
Resources registered through ctx are cleaned up automatically on unload; for manual resources, provide a disposer with ctx.effect().
1. Does a timer registered through ctx need a manual clearInterval?
2. When is the disposer of ctx.effect(() => disposer) executed?
3. Why must cleanup with ordering dependencies be placed in the same effect?