[PR #5698] feat(plugin): add experimental.session.compacting hook for pre-compaction context injection #11536

Closed
opened 2026-02-16 18:16:23 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/5698

State: closed
Merged: Yes


Summary

Adds a session.compacting plugin hook that fires before compaction starts, allowing plugins to append additional context to the compaction prompt.

Use Case

The opencode-swarm-plugin uses multi-agent coordination with beads (issue tracker), Swarm Mail (agent communication), and semantic memory. When compaction happens, the generic prompt loses all swarm coordination state. This hook lets plugins inject:

  • Active bead ID and status
  • Agent name and role
  • File reservations
  • Recent swarm mail context
  • Instructions to re-sync state after resuming

Changes

File Change
packages/plugin/src/index.ts Add session.compacting hook type
packages/opencode/src/session/compaction.ts Call hook before processor.process(), append context to prompt
packages/web/src/content/docs/plugins.mdx Add example showing hook usage

Hook Signature

"session.compacting"?: (
  input: { sessionID: string },
  output: { context: string[] },
) => Promise<void>

Example Usage

export const SwarmPlugin: Plugin = async (ctx) => {
  return {
    "session.compacting": async (input, output) => {
      output.context.push(`
## Swarm Coordination State

IMPORTANT: This session is part of a multi-agent swarm. After resuming:
1. Run beads_query to check current task status
2. Run swarmmail_inbox to sync communications
3. Check file reservations before editing
`)
    }
  }
}

Design Decisions

  • Append-only: Plugins can only add context, not replace the base prompt
  • Minimal input: Just sessionID - plugins fetch their own state
  • Not experimental: This is a stable hook for a real use case
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/5698 **State:** closed **Merged:** Yes --- ## Summary Adds a `session.compacting` plugin hook that fires **before** compaction starts, allowing plugins to append additional context to the compaction prompt. ## Use Case The [opencode-swarm-plugin](https://github.com/joelhooks/swarm-tools) uses multi-agent coordination with beads (issue tracker), Swarm Mail (agent communication), and semantic memory. When compaction happens, the generic prompt loses all swarm coordination state. This hook lets plugins inject: - Active bead ID and status - Agent name and role - File reservations - Recent swarm mail context - Instructions to re-sync state after resuming ## Changes | File | Change | |------|--------| | `packages/plugin/src/index.ts` | Add `session.compacting` hook type | | `packages/opencode/src/session/compaction.ts` | Call hook before processor.process(), append context to prompt | | `packages/web/src/content/docs/plugins.mdx` | Add example showing hook usage | ## Hook Signature ```typescript "session.compacting"?: ( input: { sessionID: string }, output: { context: string[] }, ) => Promise<void> ``` ## Example Usage ```typescript export const SwarmPlugin: Plugin = async (ctx) => { return { "session.compacting": async (input, output) => { output.context.push(` ## Swarm Coordination State IMPORTANT: This session is part of a multi-agent swarm. After resuming: 1. Run beads_query to check current task status 2. Run swarmmail_inbox to sync communications 3. Check file reservations before editing `) } } } ``` ## Design Decisions - **Append-only**: Plugins can only add context, not replace the base prompt - **Minimal input**: Just `sessionID` - plugins fetch their own state - **Not experimental**: This is a stable hook for a real use case
yindo added the pull-request label 2026-02-16 18:16:23 -05:00
yindo closed this issue 2026-02-16 18:16:23 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#11536