Skip to content

feat: implement PluginPipeline for improved plugin execution flow#78

Merged
akshaydeo merged 1 commit intomainfrom
06-13-feat_plugin_handling_in_bifrost_enchancements
Jun 14, 2025
Merged

feat: implement PluginPipeline for improved plugin execution flow#78
akshaydeo merged 1 commit intomainfrom
06-13-feat_plugin_handling_in_bifrost_enchancements

Conversation

@Pratham-Mishra04
Copy link
Copy Markdown
Collaborator

Refactor Plugin Pipeline for Better Error Handling

This PR introduces a new PluginPipeline abstraction to improve the handling of plugin execution in Bifrost. The key improvements include:

  1. Added proper error aggregation for both PreHooks and PostHooks

  2. Enhanced PostHook interface to allow plugins to:

    • Recover from errors (transform an error into a valid response)
    • Invalidate responses (transform a response into an error)
    • Modify errors before they're returned to the caller
  3. Implemented consistent execution flow for both text and chat completion requests

  4. Added a GetName() method to the Plugin interface for better error reporting

The pipeline tracks which plugins ran during PreHook to ensure only those plugins have their PostHook called, and in the correct reverse order. This maintains the expected plugin execution lifecycle while providing more flexibility in how plugins can transform the request/response flow.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 13, 2025

Summary by CodeRabbit

  • New Features
    • Improved plugin handling with a new system for running plugin hooks, allowing plugins to modify or recover from errors and responses.
  • Refactor
    • Centralized plugin execution and error management for more consistent and maintainable request processing.
  • Chores
    • Updated plugin interface to support enhanced error handling and plugin identification.

Walkthrough

A new PluginPipeline abstraction was introduced to centralize and manage the execution of plugin PreHooks and PostHooks, including error aggregation and short-circuit logic. The Plugin interface was updated to include a GetName method and an expanded PostHook signature. Core methods now use the pipeline for plugin hook handling.

Changes

File(s) Change Summary
core/bifrost.go Introduced PluginPipeline struct and methods for managing plugin PreHooks/PostHooks, refactored core logic to use it.
core/schemas/plugin.go Updated Plugin interface: added GetName(), changed PostHook signature to handle errors and return more values.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Bifrost
    participant PluginPipeline
    participant Provider

    Client->>Bifrost: Send Request
    Bifrost->>PluginPipeline: RunPreHooks(ctx, req)
    PluginPipeline-->>Bifrost: (req', shortCircuitResp, count)
    alt Short-circuit response
        Bifrost->>PluginPipeline: RunPostHooks(ctx, shortCircuitResp, nil, count)
        PluginPipeline-->>Bifrost: (resp, err)
        Bifrost-->>Client: resp or err
    else No short-circuit
        Bifrost->>Provider: Process Request
        Provider-->>Bifrost: (resp, err)
        Bifrost->>PluginPipeline: RunPostHooks(ctx, resp, err, count)
        PluginPipeline-->>Bifrost: (finalResp, finalErr)
        Bifrost-->>Client: finalResp or finalErr
    end
Loading

Possibly related PRs

Suggested reviewers

  • akshaydeo
  • danpiths

Poem

In the warren where plugins hop and play,
A pipeline now guides them on their way.
PreHooks and PostHooks, in order they run,
With errors and names, their work is well done.
No more confusion, just clean, clever flow—
The code-rabbits cheer as the plugins all go!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Commit Unit Tests in branch 06-13-feat_plugin_handling_in_bifrost_enchancements
  • Post Copyable Unit Tests in Comment

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Collaborator Author

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 022c8a1 and db1a389.

📒 Files selected for processing (2)
  • core/bifrost.go (5 hunks)
  • core/schemas/plugin.go (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
core/schemas/plugin.go (1)
core/schemas/bifrost.go (2)
  • BifrostResponse (237-247)
  • BifrostError (365-371)
core/bifrost.go (3)
core/schemas/plugin.go (1)
  • Plugin (21-41)
core/schemas/logger.go (1)
  • Logger (18-35)
core/schemas/bifrost.go (3)
  • BifrostRequest (59-69)
  • BifrostResponse (237-247)
  • BifrostError (365-371)
🔇 Additional comments (4)
core/schemas/plugin.go (2)

22-24: LGTM! The GetName() method enhances plugin identification.

This addition improves error reporting and debugging capabilities by allowing the pipeline to identify plugins by name in log messages.


32-35: LGTM! Enhanced PostHook signature enables flexible error handling.

The updated signature allows plugins to transform both responses and errors, enabling sophisticated error recovery and response modification scenarios. This is a well-designed enhancement that provides greater flexibility in the plugin lifecycle.

core/bifrost.go (2)

53-72: LGTM! Well-designed PluginPipeline abstraction.

The struct properly encapsulates plugin execution state with clear separation of concerns. Tracking which plugins ran their PreHook ensures correct PostHook execution order.


635-642: ⚠️ Potential issue

Fix variable shadowing in error handling.

The := operator creates a new bifrostErrPtr variable in the inner scope, shadowing the outer one. This prevents the PostHook from properly modifying the error.

 	case bifrostErrVal := <-msg.Err:
 		bifrostErrPtr := &bifrostErrVal
-		resp, bifrostErrPtr := pipeline.RunPostHooks(&ctx, nil, bifrostErrPtr, len(bifrost.plugins))
+		resp, bifrostErrPtr = pipeline.RunPostHooks(&ctx, nil, bifrostErrPtr, len(bifrost.plugins))
 		bifrost.releaseChannelMessage(msg)
 		if bifrostErrPtr != nil {
 			return nil, bifrostErrPtr
 		}
 		return resp, nil

Likely an incorrect or invalid review comment.

Comment thread core/bifrost.go
Comment thread core/bifrost.go
Comment thread core/bifrost.go
Comment thread core/bifrost.go
@akshaydeo akshaydeo marked this pull request as ready for review June 13, 2025 07:10
Comment thread core/bifrost.go
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Plugin Lifecycle Violation in Completion Hooks

The PluginPipeline's RunPostHooks is incorrectly called with len(bifrost.plugins) instead of preCount in tryTextCompletion and tryChatCompletion functions. This bug occurs for both successful responses and error paths, causing PostHooks to execute for plugins whose PreHooks did not run, violating the intended plugin lifecycle.

core/bifrost.go#L627-L628

bifrost/core/bifrost.go

Lines 627 to 628 in db1a389

case result = <-msg.Response:
resp, bifrostErr := pipeline.RunPostHooks(&ctx, result, nil, len(bifrost.plugins))

core/bifrost.go#L746-L747

bifrost/core/bifrost.go

Lines 746 to 747 in db1a389

case result = <-msg.Response:
resp, bifrostErr := pipeline.RunPostHooks(&ctx, result, nil, len(bifrost.plugins))

core/bifrost.go#L755-L756

bifrost/core/bifrost.go

Lines 755 to 756 in db1a389

bifrostErrPtr := &bifrostErrVal
resp, bifrostErrPtr := pipeline.RunPostHooks(&ctx, nil, bifrostErrPtr, len(bifrost.plugins))

Fix in Cursor


BugBot free trial expires on June 17, 2025
You have used $0.00 of your $50.00 spend limit so far. Manage your spend limit in the Cursor dashboard.

Was this report helpful? Give feedback by reacting with 👍 or 👎

Comment thread core/bifrost.go
// Indices of plugins whose PreHook ran (for reverse PostHook)
preHookRan []int
// Errors from PreHooks and PostHooks
preHookErrors []error
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually we can wrap it under a separate struct for better abstraction. Also we can create pool of these objects to avoid any runtime allocations

type Errs struct {
    Errors []errors
}

func (e *Errs) Print() {
}

func (e *Errs) Add(e error) {
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I've made changes to this, I pushed it to the next pr by mistake

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add sync pools, even I was thinking about it

@akshaydeo akshaydeo merged commit 8751cfe into main Jun 14, 2025
2 checks passed
@akshaydeo akshaydeo deleted the 06-13-feat_plugin_handling_in_bifrost_enchancements branch August 31, 2025 17:31
akshaydeo added a commit that referenced this pull request Nov 17, 2025
…rost_enchancements

feat: implement PluginPipeline for improved plugin execution flow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants