[PR #9361] feat(plugin): add session.creating hook for customizing session creation #13086

Open
opened 2026-02-16 18:17:57 -05:00 by yindo · 0 comments
Owner

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

State: open
Merged: No


Summary

Add a new plugin hook session.creating that is triggered before a session is created. This allows plugins to customize session creation, including setting a custom working directory.

What does this PR do?

This PR adds a new plugin hook session.creating that fires before a session is created. Plugins can use this hook to customize session creation - most notably, setting a custom working directory for the session (e.g., for git worktrees, sandboxes, etc.).

Currently, there's no way for plugins to intercept session creation and customize it. Having a hook like this enables powerful use cases like:

  • Git worktrees: Create isolated worktrees for each session so multiple sessions can work on different branches without conflicts
  • Sandboxed environments: Run sessions in isolated directories for security or testing
  • Custom project layouts: Support monorepos or non-standard project structures

API

"session.creating"?: (
  input: { sessionID: string; parentID?: string },
  output: { directory: string },
) => Promise<void>
  • input.sessionID: The ID of the session being created
  • input.parentID: The parent session ID (if creating a child session)
  • output.directory: The working directory for the session (defaults to Instance.directory)

Example Plugin

export default {
  name: "worktree",
  hooks: {
    "session.creating": async (input, output) => {
      const worktreePath = await createWorktree(input.sessionID)
      output.directory = worktreePath
    }
  }
}

How did you verify your code works?

Created a test plugin that uses the session.creating hook to create a git worktree workflow using opencode sessions. Verified that:

  1. Created a test plugin using the session.creating hook to set a custom directory
  2. Verified the hook is called with correct sessionID and parentID
  3. Verified output.directory is persisted to the session's directory field
  4. Verified sessions created without plugins still default to Instance.directory

Closes #9366

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/9361 **State:** open **Merged:** No --- ## Summary Add a new plugin hook `session.creating` that is triggered before a session is created. This allows plugins to customize session creation, including setting a custom working directory. ### What does this PR do? This PR adds a new plugin hook session.creating that fires before a session is created. Plugins can use this hook to customize session creation - most notably, setting a custom working directory for the session (e.g., for git worktrees, sandboxes, etc.). Currently, there's no way for plugins to intercept session creation and customize it. Having a hook like this enables powerful use cases like: - **Git worktrees**: Create isolated worktrees for each session so multiple sessions can work on different branches without conflicts - **Sandboxed environments**: Run sessions in isolated directories for security or testing - **Custom project layouts**: Support monorepos or non-standard project structures ## API ```typescript "session.creating"?: ( input: { sessionID: string; parentID?: string }, output: { directory: string }, ) => Promise<void> ``` - `input.sessionID`: The ID of the session being created - `input.parentID`: The parent session ID (if creating a child session) - `output.directory`: The working directory for the session (defaults to `Instance.directory`) ## Example Plugin ```typescript export default { name: "worktree", hooks: { "session.creating": async (input, output) => { const worktreePath = await createWorktree(input.sessionID) output.directory = worktreePath } } } ``` ### How did you verify your code works? Created a test plugin that uses the session.creating hook to create a git worktree workflow using opencode sessions. Verified that: 1. Created a test plugin using the session.creating hook to set a custom directory 2. Verified the hook is called with correct sessionID and parentID 3. Verified output.directory is persisted to the session's directory field 4. Verified sessions created without plugins still default to Instance.directory Closes #9366
yindo added the pull-request label 2026-02-16 18:17:57 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13086