Summary
Add a shutdown/exit hook to the plugin system so plugins can perform cleanup operations before OpenCode exits.
Use Case
I'm developing opencode-sync, a plugin that syncs OpenCode data (sessions, messages, config, etc.) to a private GitHub repository.
The plugin needs to sync local changes to remote before exit to prevent data loss. Currently, there's no way for plugins to:
- Know when OpenCode is about to exit
- Perform async cleanup operations before process termination
Current Behavior
Looking at the source:
packages/plugin/src/index.ts - The Hooks interface only has event hook for Bus events
packages/opencode/src/cli/cmd/tui/thread.ts - Has onExit callback but it's internal, not exposed to plugins
- Session events are
Created, Updated, Deleted, Diff, Error - no shutdown event
When OpenCode exits (Ctrl+C, :q, etc.), plugins have no opportunity to run cleanup code.
Proposed Solution
Add a shutdown hook to the plugin Hooks interface:
export interface Hooks {
event?(input: { event: Bus.Event }): void;
shutdown?(): Promise<void>; // New hook
}
The shutdown hook should:
- Be called before OpenCode terminates
- Support async operations (return Promise)
- Have a reasonable timeout (e.g., 5 seconds) to prevent hanging
Alternatives Considered
- Node.js process events (
beforeExit, SIGINT, SIGTERM) - OpenCode intercepts signals before plugin handlers run
- Frequent background syncing - Works but can't guarantee final changes are synced
- File watching with debounce - Same limitation, final changes may be lost
Additional Context
This would enable plugins to:
- Sync data to remote storage
- Close database connections
- Flush logs
- Clean up temporary files
- Save state
Thank you for considering this feature!
Summary
Add a shutdown/exit hook to the plugin system so plugins can perform cleanup operations before OpenCode exits.
Use Case
I'm developing opencode-sync, a plugin that syncs OpenCode data (sessions, messages, config, etc.) to a private GitHub repository.
The plugin needs to sync local changes to remote before exit to prevent data loss. Currently, there's no way for plugins to:
Current Behavior
Looking at the source:
packages/plugin/src/index.ts- TheHooksinterface only haseventhook for Bus eventspackages/opencode/src/cli/cmd/tui/thread.ts- HasonExitcallback but it's internal, not exposed to pluginsCreated,Updated,Deleted,Diff,Error- no shutdown eventWhen OpenCode exits (Ctrl+C,
:q, etc.), plugins have no opportunity to run cleanup code.Proposed Solution
Add a
shutdownhook to the pluginHooksinterface:The shutdown hook should:
Alternatives Considered
beforeExit,SIGINT,SIGTERM) - OpenCode intercepts signals before plugin handlers runAdditional Context
This would enable plugins to:
Thank you for considering this feature!