Skip to content

fix(python): harden prompt isolation and centralize error handling#143

Merged
calchiwo merged 1 commit intomainfrom
hardening/prompt-isolation-and-cli-cleanup
Feb 12, 2026
Merged

fix(python): harden prompt isolation and centralize error handling#143
calchiwo merged 1 commit intomainfrom
hardening/prompt-isolation-and-cli-cleanup

Conversation

@calchiwo
Copy link
Owner

This PR hardens the Python runtime to match the Node implementation

It introduces prompt isolation blocks and escapes repository content to prevent delimiter breakout

It also centralizes error handling by extracting safe_read_repo_files and generate_with_exit, removing duplicated try/except logic

@vercel
Copy link

vercel bot commented Feb 12, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
explainthisrepo Ready Ready Preview, Comment Feb 12, 2026 9:01pm

@gemini-code-assist
Copy link

Summary of Changes

Hello @calchiwo, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the robustness and maintainability of the Python runtime. It introduces mechanisms to prevent prompt injection by isolating and escaping repository content within LLM prompts. Concurrently, it refactors error handling by centralizing common try/except patterns into reusable functions, leading to cleaner and more consistent error management throughout the application.

Highlights

  • Prompt Isolation Hardening: Implemented prompt isolation blocks and escaped repository content to prevent delimiter breakout, aligning the Python runtime with the Node implementation.
  • Centralized Error Handling: Extracted safe_read_repo_files and generate_with_exit functions to centralize error handling logic, reducing duplicated try/except blocks across the codebase.
  • LLM Instruction Reinforcement: Added critical instructions to prompt templates to ensure the LLM treats repository content strictly as data and ignores any embedded instructions.
Changelog
  • explain_this_repo/cli.py
    • Added safe_read_repo_files function.
    • Added generate_with_exit function.
    • Replaced inline try/except blocks with calls to the new centralized error handling functions.
  • explain_this_repo/prompt.py
    • Added escape_for_prompt_block function.
    • Modified build_prompt, build_quick_prompt, and build_simple_prompt to use new prompt isolation blocks (e.g., <repository_metadata>, <readme>) and escape content.
    • Added critical instructions to prompt templates to prevent LLM instruction following from repository content.
  • pyproject.toml
    • Updated project version from 0.4.1 to 0.4.2.
    • Revised project description for clarity.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@calchiwo calchiwo merged commit e8fccaf into main Feb 12, 2026
6 checks passed
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request does a great job of centralizing error handling and hardening the prompts against injection. The introduction of safe_read_repo_files and generate_with_exit significantly improves code structure by removing duplicated try/except blocks. The prompt isolation techniques, like escaping content and adding explicit XML-like tags, are excellent for robustness.

My feedback focuses on a couple of areas for improvement:

  • Consistently using stderr for warnings and error messages to follow standard CLI practices.
  • Addressing a minor formatting issue in prompt construction that could lead to unintended extra newlines.

Overall, these are solid improvements to the tool's reliability and maintainability.

try:
return read_repo_signal_files(owner, repo)
except Exception as e:
print(f"warning: could not read repository files: {e}")

Choose a reason for hiding this comment

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

medium

It's standard practice for command-line tools to write warning messages to stderr. This allows users to redirect stdout (the primary output) without losing important diagnostic messages. Please direct this warning to sys.stderr.

Suggested change
print(f"warning: could not read repository files: {e}")
print(f"warning: could not read repository files: {e}", file=sys.stderr)

Comment on lines +143 to +147
print("Failed to generate explanation.")
print(f"error: {e}")
print("\nfix:")
print("- Ensure GEMINI_API_KEY is set")
print("- Or run: explainthisrepo --doctor")

Choose a reason for hiding this comment

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

medium

Error messages and user guidance for fixing issues should be printed to stderr rather than stdout. This separates diagnostic output from the program's main output, which is a standard convention for CLI applications.

Suggested change
print("Failed to generate explanation.")
print(f"error: {e}")
print("\nfix:")
print("- Ensure GEMINI_API_KEY is set")
print("- Or run: explainthisrepo --doctor")
print("Failed to generate explanation.", file=sys.stderr)
print(f"error: {e}", file=sys.stderr)
print("\nfix:", file=sys.stderr)
print("- Ensure GEMINI_API_KEY is set", file=sys.stderr)
print("- Or run: explainthisrepo --doctor", file=sys.stderr)

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.

1 participant