← Back to PRs

#8178: feat: allow before_agent_start to override the prompt

by richardfogaca open 2026-02-03 17:43 View on GitHub →
agents stale
## Summary This PR adds `promptOverride` support to the `before_agent_start` hook so plugins can safely replace the prompt before the model call. This enables context‑management plugins to truncate or redact oversized *current* user prompts, preventing context‑limit errors even when history pruning is insufficient. ## Problem Plugins can only prepend context or adjust the system prompt. If the current prompt itself is large, plugins cannot shrink it, so requests can still fail with errors like: ``` input length and max_tokens exceed context limit ``` ## Fix - Extend `PluginHookBeforeAgentStartResult` with `promptOverride?: string`. - Merge `promptOverride` in `runBeforeAgentStart` so the last plugin can supply it. - Apply the override in the embedded runner before `prependContext`. - When both are provided, `prependContext` is applied to the **effective** prompt (override first, then prepend). ## Why this is safe - Fully opt‑in: behavior is unchanged unless a plugin returns `promptOverride`. - Does not alter default prompt assembly logic for existing plugins. - Enables guardrails without touching core request flow elsewhere. ## Testing - Type-only change + runtime behavior verified via local run using a context‑manager plugin that truncates oversized prompts. Closes #8176 <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR extends the `before_agent_start` plugin hook result with an optional `promptOverride` field and wires it into the embedded attempt runner so plugins can replace the user prompt before it’s sent to the model. The hook runner merges `promptOverride` alongside existing `systemPrompt`/`prependContext`, and the embedded runner applies `promptOverride` first, then prepends context to the resulting effective prompt. This fits into the existing plugin hook system by treating `before_agent_start` as a sequential “modifying” hook and keeping behavior unchanged unless a plugin opts in by returning `promptOverride`. <h3>Confidence Score: 4/5</h3> - This PR is likely safe to merge with low risk. - Changes are small and localized, but there are two behavior/expectation mismatches worth confirming: (1) the merged `promptOverride` currently cannot be replaced by lower-priority hooks (contrary to the description wording), and (2) plugins always receive the original prompt in the hook event even if an earlier hook effectively changed it. Neither is a runtime break, but both can surprise plugin authors and lead to incorrect plugin logic. - src/plugins/hooks.ts and src/agents/pi-embedded-runner/run/attempt.ts <!-- greptile_other_comments_section --> <sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub> **Context used:** - Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8)) - Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13)) <!-- /greptile_comment -->

Most Similar PRs