Skip to content

Conversation

@TamiTakamiya
Copy link
Contributor

@TamiTakamiya TamiTakamiya commented Jul 16, 2025

Description

For #247

This PR adds the support of output_shields in agents used in lightspeed-stack.

Here are the naming convention of shield_id in this PR (also documented in README.md):

  1. If the shield_id starts with input_, it will be used for input only.
  2. If the shield_id starts with output_, it will be used for output only.
  3. If the shield_id starts with inout_, it will be used both for input and output.
  4. Otherwise, it will be used for input only.

Type of change

  • Refactor
  • New feature
  • Bug fix
  • CVE fix
  • Optimization
  • Documentation Update
  • Configuration Update
  • Bump-up service version
  • Bump-up dependent library
  • Bump-up library or tool used for development (does not change the final image)
  • CI configuration change
  • Konflux configuration change
  • Unit tests improvement
  • Integration tests improvement
  • End to end tests improvement

Related Tickets & Documents

Checklist before requesting a review

  • I have performed a self-review of my code.
  • PR has passed all pre-merge test jobs.
  • If it is a core feature, I have added thorough tests.

Testing

  • Please provide detailed steps to perform tests related to this code change.
  • How were the fix/results from this change verified? Please provide relevant screenshots or results.

Summary by CodeRabbit

  • Documentation

    • Added a "Safety Shields" section to the README, explaining the use and naming conventions for input, output, and input/output safety shields in agent configurations.
  • New Features

    • Introduced distinct handling for input and output safety shields, allowing agents to monitor input, output, or both streams based on shield identifiers.
  • Bug Fixes

    • Improved error handling for malformed JSON in streaming responses to prevent interruptions.
  • Tests

    • Added and updated tests to verify correct separation and handling of input and output shields in various scenarios.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 16, 2025

Walkthrough

The changes introduce support for distinguishing input and output shields in agent creation, based on shield ID naming conventions. Code and tests are updated to filter shields into input and output categories, pass them separately to agent constructors, and handle them distinctly in both synchronous and asynchronous query endpoints. Documentation is updated to describe the new conventions.

Changes

File(s) Change Summary
README.md Added "Safety Shields" section explaining shield ID conventions for input/output/inout shields.
src/app/endpoints/query.py
src/app/endpoints/streaming_query.py
Refactored to distinguish input/output shields by ID; updated agent creation and shield filtering logic.
tests/unit/app/endpoints/test_query.py
tests/unit/app/endpoints/test_streaming_query.py
Updated tests to use separate input/output shield parameters; added tests for four-shield scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Endpoint
    participant Client
    participant Agent

    User->>Endpoint: Submit query
    Endpoint->>Client: Get available shields
    Endpoint->>Endpoint: Filter shields (input/output/inout)
    Endpoint->>Agent: Create agent with input_shields, output_shields
    Agent->>Client: Process user message (with shields)
    Agent-->>Endpoint: Return response
    Endpoint-->>User: Return response
Loading

Assessment against linked issues

Objective Addressed Explanation
Distinguish input and output shields by shield_id naming convention (input_, output_, inout_, default) (#247)
Build and pass input_shields and output_shields separately to agent creation (#247)
Update agent creation in both sync and async endpoints to support output_shields (#247)
Update documentation to describe shield naming conventions (#247)

Poem

In the meadow of code, shields now abound,
Input and output, their roles are well found.
With prefixes clear, the agents delight,
Guarding our queries by day and by night.
🛡️🐇—A rabbit’s rhyme for shields in the stack,
Keeping your data safe, front and back!


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3c08902 and 52034bf.

📒 Files selected for processing (1)
  • src/app/endpoints/streaming_query.py (7 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/app/endpoints/streaming_query.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build-pr
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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 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.

@TamiTakamiya TamiTakamiya force-pushed the TamiTakamiya/support-output-shields-in-agents branch from 756eb51 to 28224e5 Compare July 16, 2025 18:41
Copy link
Contributor

@manstis manstis left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@tisnik PTAL.

@manstis
Copy link
Contributor

manstis commented Jul 16, 2025

@TamiTakamiya make verify will detect these errors locally.

@TamiTakamiya TamiTakamiya force-pushed the TamiTakamiya/support-output-shields-in-agents branch from 28224e5 to ae6da91 Compare July 16, 2025 18:54
@TamiTakamiya TamiTakamiya marked this pull request as ready for review July 16, 2025 18:58
@TamiTakamiya TamiTakamiya requested a review from tisnik July 16, 2025 18:58
Copy link
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: 1

🧹 Nitpick comments (2)
README.md (1)

142-144: Improve readability by varying sentence structure.

The static analysis tool correctly identified repetitive sentence beginnings. Consider rewording for better flow.

-1. If the `shield_id` starts with `input_`, it will be used for input only.
-1. If the `shield_id` starts with `output_`, it will be used for output only.
-1. If the `shield_id` starts with `inout_`, it will be used both for input and output.
+1. When the `shield_id` starts with `input_`, it will be used for input only.
+1. For `shield_id` starting with `output_`, it will be used for output only.
+1. If the `shield_id` starts with `inout_`, it will be used both for input and output.
src/app/endpoints/streaming_query.py (1)

174-180: Remove unnecessary finally block in JSON parsing.

The empty finally block serves no purpose and should be removed for cleaner code.

                                try:
                                    meta = json.loads(match.replace("'", '"'))
                                    metadata_map[meta["document_id"]] = meta
                                except JSONDecodeError:
                                    pass
-                                finally:
-                                    pass
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between eb13e53 and ae6da91.

📒 Files selected for processing (5)
  • README.md (1 hunks)
  • src/app/endpoints/query.py (6 hunks)
  • src/app/endpoints/streaming_query.py (7 hunks)
  • tests/unit/app/endpoints/test_query.py (15 hunks)
  • tests/unit/app/endpoints/test_streaming_query.py (15 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
src/app/endpoints/streaming_query.py (1)
src/app/endpoints/query.py (3)
  • is_input_shield (216-218)
  • is_output_shield (211-213)
  • get_agent (75-105)
src/app/endpoints/query.py (1)
src/app/endpoints/streaming_query.py (1)
  • get_agent (49-77)
tests/unit/app/endpoints/test_query.py (4)
tests/unit/app/endpoints/test_streaming_query.py (4)
  • test_retrieve_response_four_available_shields (446-507)
  • MockShield (350-360)
  • MockShield (398-408)
  • MockShield (449-459)
src/configuration.py (1)
  • mcp_servers (79-84)
src/models/requests.py (1)
  • QueryRequest (61-146)
src/app/endpoints/query.py (1)
  • retrieve_response (221-293)
🪛 LanguageTool
README.md

[style] ~144-~144: Three successive sentences begin with the same word. Consider rewording the sentence or use a thesaurus to find a synonym.
Context: ..., it will be used for output only. 1. If the shield_idstarts withinout`, i...

(ENGLISH_WORD_REPEAT_BEGINNING_RULE)

🔇 Additional comments (14)
README.md (1)

136-145: Documentation accurately describes the new shield naming conventions.

The Safety Shields section clearly explains how shield IDs are classified based on their prefixes. The logic matches the implementation in the code.

src/app/endpoints/streaming_query.py (4)

49-56: Function signature correctly updated for separate shield handling.

The get_agent function now properly accepts separate lists for input and output shields, aligning with the new shield classification system.


69-70: Agent creation properly uses both input and output shields.

The AsyncAgent constructor now receives both input and output shields separately, which is the correct implementation for the new feature.


281-288: Shield filtering logic implemented correctly.

The code properly filters shields into input and output categories using the imported helper functions that implement the naming convention logic.


6-6: JSON error handling improvement is well implemented.

Adding JSONDecodeError import and proper exception handling for malformed JSON prevents processing interruptions.

tests/unit/app/endpoints/test_streaming_query.py (3)

446-508: Comprehensive test coverage for shield classification.

The new test function effectively validates the shield filtering logic with all four shield types (default, input_, output_, inout_). The test correctly verifies that:

  • Default and input_ shields go to input list
  • output_ shields go to output list
  • inout_ shields go to both lists

The assertions properly validate the get_agent function is called with the correct separated shield lists.


920-921: Test function signatures correctly updated for separate shields.

All test functions that call get_agent have been properly updated to use the new signature with separate available_input_shields and available_output_shields parameters.

Also applies to: 965-966, 1024-1025, 1083-1084, 1141-1142, 1197-1198


732-733: Test assertions updated to match new function signature.

The test assertions for get_agent calls have been correctly updated to expect separate empty lists for input and output shields when no shields are available.

Also applies to: 799-800, 877-878

tests/unit/app/endpoints/test_query.py (2)

472-533: LGTM! Comprehensive test for shield categorization logic.

The test correctly verifies the new shield categorization functionality with four shields representing different naming conventions. The assertions properly validate that:

  • Input shields include shields with no prefix, input_ prefix, and inout_ prefix
  • Output shields include shields with output_ prefix and inout_ prefix
  • The get_agent function is called with the correctly separated shield lists

679-680: LGTM! Parameter updates align with new function signature.

The updates correctly modify all get_agent calls to use the new signature with separate available_input_shields and available_output_shields parameters, and the Agent constructor calls to use input_shields and output_shields parameters. The changes maintain backward compatibility by passing empty lists where no shields were previously used.

Also applies to: 743-744, 814-815, 969-970, 1010-1011, 1063-1064, 1116-1117, 1170-1171, 1222-1223

src/app/endpoints/query.py (4)

15-15: LGTM! Import addition supports new shield categorization functions.

The Shield import is necessary for the new helper functions that perform shield categorization based on identifier prefixes.


207-219: LGTM! Shield categorization logic correctly implements the naming convention.

The helper functions properly implement the shield categorization based on identifier prefixes:

  • _is_inout_shield: Identifies shields for both input and output (inout_ prefix)
  • is_output_shield: Identifies shields for output monitoring (output_ or inout_ prefix)
  • is_input_shield: Identifies shields for input monitoring (inout_ prefix or any other identifier)

The logic correctly handles the documented naming convention where shields without specific prefixes are treated as input-only.


75-82: LGTM! Function signature and implementation updated correctly.

The get_agent function signature properly accepts separate input and output shield parameters, and the Agent constructor is correctly updated to pass both input_shields and output_shields with appropriate empty list fallbacks.

Also applies to: 96-97


229-243: LGTM! Shield filtering and logging updated correctly.

The retrieve_response function properly:

  • Filters shields into separate input and output lists using the new helper functions
  • Provides clear logging for both input and output shield availability
  • Passes the correctly categorized shield lists to the get_agent function

The implementation maintains the existing functionality while adding the new shield categorization capability.

Also applies to: 258-260

@TamiTakamiya
Copy link
Contributor Author

@tisnik Would you take a look?

Copy link
Contributor

@tisnik tisnik left a comment

Choose a reason for hiding this comment

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

LGTM, just two nitpicks

@tisnik tisnik merged commit 0cb49fc into main Jul 18, 2025
32 checks passed
@tisnik tisnik deleted the TamiTakamiya/support-output-shields-in-agents branch July 25, 2025 12:28
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.

[RFE] Support output_shields in agent creation

4 participants