#8178: feat: allow before_agent_start to override the prompt
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
#11921: feat(hooks): support systemPrompt injection in before_agent_start hook
by jungdaesuh · 2026-02-08
86.8%
#6017: feat(hooks): add systemPrompt and tools to before_agent_start event
by yajatns · 2026-02-01
84.5%
#14602: fix(plugins): hook systemPrompt gets collected then thrown away (#1...
by yinghaosang · 2026-02-12
84.0%
#8022: feat: implement before_model_select plugin hook
by dead-pool-aka-wilson · 2026-02-03
81.3%
#14647: feat(plugins): allow before_agent_start hook to override model (#14...
by lailoo · 2026-02-12
81.0%
#14873: [Feature]: Extend before_agent_start hook context with Model, Tools...
by akv2011 · 2026-02-12
80.2%
#22624: feat(plugins): add before_context_send hook and model routing via b...
by davidrudduck · 2026-02-21
79.7%
#17614: feat: allow before_agent_start hook to override model selection
by plc · 2026-02-16
79.0%
#11155: feat(hooks): before_agent_start model/provider override (run-scoped...
by alanranger · 2026-02-07
78.9%
#11732: feat(plugins): add injectMessages to before_agent_start hook
by antra-tess · 2026-02-08
78.9%