Skip to content

feat(plan): unify workflow location in system prompt to optimize caching#18258

Merged
jerop merged 1 commit intomainfrom
refactor/move-primary-workflow-prompt
Feb 4, 2026
Merged

feat(plan): unify workflow location in system prompt to optimize caching#18258
jerop merged 1 commit intomainfrom
refactor/move-primary-workflow-prompt

Conversation

@jerop
Copy link
Copy Markdown
Contributor

@jerop jerop commented Feb 4, 2026

Moves the 'Primary Workflows' and 'Planning Workflows' sections to a shared, consistent location within the system prompt.

Previously, 'Primary Workflows' appeared in the middle of the prompt, while 'Planning Workflows' appeared at the very end. By placing both sections in the same logical slot, we ensure that all preceding static content (Preamble, Mandates, Skills, Contexts) remains identical regardless of the active mode. The system now renders exactly one of these two mutually exclusive workflows at this position. This change maximizes the static shared prefix between modes, improving prompt caching performance and reducing latency.

Changes:

  • Unified Location: Both workflow sections now reside immediately after 'Hook Context' and before 'Operational Guidelines', following the shared static prefix.
  • Renaming: Renamed 'ApprovalModePlanOptions' to 'PlanningWorkflowOptions' and 'renderApprovalModePlan' to 'renderPlanningWorkflow' for consistency.
  • Simplification: 'renderFinalShell' now strictly handles appending user memory only.
  • Logic Consistency: Wrapped 'planningWorkflow' in 'withSection' with an explicit 'isPlanMode' guard, matching the pattern of other optional prompt components.
  • Configurable: 'Planning Workflows' can now be disabled via environment variables GEMINI_PROMPT_PLANNINGWORKFLOW=0, just like 'Primary Workflows' can be disabled with GEMINI_PROMPT_PRIMARYWORKFLOWS=0.

Closes #17555

@jerop jerop requested a review from a team as a code owner February 4, 2026 02:43
@jerop jerop requested a review from abhipatel12 February 4, 2026 02:43
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @jerop, 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 refactors the system prompt generation logic to enhance efficiency and maintainability. It unifies the placement of primary and planning workflow sections within the prompt, ensuring a consistent structure that maximizes the static content prefix. This strategic change is designed to improve prompt caching performance and reduce latency by allowing more of the prompt to be cached across different operational modes. Additionally, the refactor includes renaming key interfaces and functions for better clarity and introduces environmental variable control for the planning workflow.

Highlights

  • Unified Workflow Location: Both 'Primary Workflows' and 'Planning Workflows' are now placed in a consistent position within the system prompt, immediately after 'Hook Context' and before 'Operational Guidelines'.
  • Improved Caching Performance: By ensuring a larger static shared prefix in the prompt, the change optimizes prompt caching and reduces latency for different operational modes.
  • Renaming for Consistency: ApprovalModePlanOptions and renderApprovalModePlan have been renamed to PlanningWorkflowOptions and renderPlanningWorkflow, respectively.
  • Simplified Final Prompt Rendering: The renderFinalShell function now exclusively handles appending user memory, streamlining its responsibility.
  • Configurable Planning Workflow: The 'Planning Workflows' section can now be disabled via the GEMINI_PROMPT_PLANNINGWORKFLOW=0 environment variable, mirroring the existing control for 'Primary Workflows'.
Changelog
  • packages/core/src/core/snapshots/prompts.test.ts.snap
    • Relocated "Operational Guidelines" and subsequent sections within the "Active Approval Mode: Plan" block, moving them from an earlier position to after the "Constraints" section.
  • packages/core/src/prompts/promptProvider.ts
    • Removed the direct initialization of planOptions and integrated the planning workflow logic into a this.withSection call, ensuring conditional rendering based on isPlanMode.
    • Updated the renderFinalShell function call to no longer pass planOptions, as planning workflow content is now handled earlier in the prompt construction.
  • packages/core/src/prompts/snippets.ts
    • Added planningWorkflow?: PlanningWorkflowOptions; to the SystemPromptOptions interface.
    • Renamed the ApprovalModePlanOptions interface to PlanningWorkflowOptions.
    • Modified the prompt rendering to conditionally include either renderPlanningWorkflow or renderPrimaryWorkflows in a unified slot, based on the presence of options.planningWorkflow.
    • Updated the renderFinalShell function signature and implementation to remove the planOptions parameter and the call to renderApprovalModePlan.
    • Renamed the renderApprovalModePlan function to renderPlanningWorkflow.
Activity
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.

…ze caching

Moves the 'Primary Workflows' and 'Planning Workflows' sections to a shared, consistent location within the system prompt.

Previously, 'Primary Workflows' appeared in the middle of the prompt, while 'Planning Workflows' appeared at the very end. By placing both sections in the same logical slot, we ensure that all preceding static content (Preamble, Mandates, Skills, Contexts) remains identical regardless of the active mode. The system now renders exactly one of these two mutually exclusive workflows at this position.

Changes:
- Unified Location: Both workflow sections now reside immediately after 'Hook Context' and before 'Operational Guidelines', following the shared static prefix.
- Renaming: Renamed 'ApprovalModePlanOptions' to 'PlanningWorkflowOptions' and 'renderApprovalModePlan' to 'renderPlanningWorkflow' for consistency.
- Simplification: 'renderFinalShell' now strictly handles appending user memory, removing hidden logic injections.
- Logic Consistency: Wrapped 'planningWorkflow' in 'withSection' with an explicit 'isPlanMode' guard, matching the pattern of other optional prompt components.
- Configurable: 'Planning Workflows' can now be disabled via environment variables (GEMINI_PROMPT_PLANNINGWORKFLOW=0), just like 'Primary Workflows' can be disabled with GEMINI_PROMPT_PRIMARYWORKFLOWS=0.

This change maximizes the static shared prefix between modes, ensuring that core workflow instructions remain part of the high-value cached prefix even as user memory evolves.
@jerop jerop force-pushed the refactor/move-primary-workflow-prompt branch from c4b66ae to 11770aa Compare February 4, 2026 02:44
@jerop jerop changed the title refactor(prompts): unify workflow location in system prompt to optimize caching feat(plan): unify workflow location in system prompt to optimize caching Feb 4, 2026
@jerop jerop enabled auto-merge February 4, 2026 02:45
Copy link
Copy Markdown
Contributor

@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 refactors the system prompt generation to unify the location of 'Primary Workflows' and 'Planning Workflows', enhancing prompt caching performance. However, this refactor inadvertently weakens 'Plan Mode' security by moving its core constraints from the end to the middle of the prompt, increasing susceptibility to prompt injection attacks via userMemory. Specifically, moving planningWorkflow and removing planOptions from the end of the prompt allows untrusted userMemory to more easily override critical constraints. To address this, consider moving planningWorkflow back to the end or reinforcing Plan Mode constraints in the Final Reminder section when isPlanMode is active. Additionally, for performance optimization, it is recommended to create the Set of tool names only once at the beginning of the getCoreSystemPrompt method.

Comment thread packages/core/src/prompts/snippets.ts
Comment thread packages/core/src/prompts/snippets.ts
Comment thread packages/core/src/prompts/promptProvider.ts
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 4, 2026

Size Change: +94 B (0%)

Total Size: 23.7 MB

ℹ️ View Unchanged
Filename Size Change
./bundle/gemini.js 23.7 MB +94 B (0%)
./bundle/sandbox-macos-permissive-closed.sb 1.03 kB 0 B
./bundle/sandbox-macos-permissive-open.sb 890 B 0 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB 0 B
./bundle/sandbox-macos-restrictive-closed.sb 3.29 kB 0 B
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB 0 B
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB 0 B

compressed-size-action

Copy link
Copy Markdown
Contributor

@abhipatel12 abhipatel12 left a comment

Choose a reason for hiding this comment

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

LGTM

@jerop jerop added this pull request to the merge queue Feb 4, 2026
Merged via the queue into main with commit d866e7e Feb 4, 2026
45 of 46 checks passed
@jerop jerop deleted the refactor/move-primary-workflow-prompt branch February 4, 2026 03:22
theerud pushed a commit to theerud/gemini-cli that referenced this pull request Feb 4, 2026
yuvrajangadsingh pushed a commit to yuvrajangadsingh/gemini-cli that referenced this pull request Feb 4, 2026
sidwan02 pushed a commit to sidwan02/gemini-cli-gemma that referenced this pull request Feb 6, 2026
kuishou68 pushed a commit to iOfficeAI/aioncli that referenced this pull request Feb 27, 2026
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.

Unify workflow locations in system prompt to maximize prefix caching

2 participants