Skip to content

feat(plugins): add appendContext to before_prompt_build hook#193

Merged
dgarson merged 1 commit intodgarson/forkfrom
dgarson/add-append-context-for-prompt-caching
Mar 3, 2026
Merged

feat(plugins): add appendContext to before_prompt_build hook#193
dgarson merged 1 commit intodgarson/forkfrom
dgarson/add-append-context-for-prompt-caching

Conversation

@dgarson
Copy link
Owner

@dgarson dgarson commented Mar 2, 2026

Summary

  • Problem: Plugins that need to inject transient, per-turn context (enforcement nudges, dynamic warnings) into the agent prompt must use prependContext, which prepends text before the user prompt. This invalidates the stable system-prompt prefix and breaks prompt caching for that block and everything after it.
  • Why it matters: Prompt caching is critical for latency and cost. A single transient nudge from any plugin destroys cache hits for the entire prependContext portion of the system prompt, even though the rest (role instructions, fleet status, etc.) is stable turn-over-turn.
  • What changed: Added appendContext field to PluginHookBeforePromptBuildResult. Text returned in appendContext is appended after the user prompt, leaving the stable prependContext prefix intact and preserving prompt cache hits. Merge semantics mirror prependContext -- multiple hooks' values are concatenated with "\n\n". Empty results are coerced to undefined.
  • What did NOT change (scope boundary): prependContext behavior is unchanged. systemPrompt behavior is unchanged. No existing plugin behavior is affected -- this is purely additive.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

None -- discovered during agent-orchestrator enforcement work where memory-search nudges were breaking prompt caching.

User-visible / Behavior Changes

None. This is a new opt-in capability for plugin authors. No existing behavior changes.

Security Impact (required)

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? No
  • Command/tool execution surface changed? No
  • Data access scope changed? No

Repro + Verification

Steps

  1. Create a plugin that returns { appendContext: "some transient text" } from a before_prompt_build hook
  2. Observe the agent prompt includes the text appended after the user prompt
  3. Verify that prependContext from other hooks is unaffected

Expected

  • appendContext text appears after the user prompt
  • prependContext prefix remains cache-stable across turns

Actual

  • Confirmed via integration tests (350 passing)

Evidence

  • Failing test/log before + passing after
  • All existing tests pass (350/350), including attempt.test.ts which validates resolvePromptBuildHookResult
  • New integration tests in agent-orchestrator validate appendContext for nudge injection (in consuming branch)

Human Verification (required)

  • Verified scenarios: Plugin returning appendContext only, prependContext only, both, neither. Multiple hooks returning appendContext (merge concatenation).
  • Edge cases checked: Empty appendContext coerced to undefined (no empty string pollution). Legacy before_agent_start hook appendContext merged correctly.
  • What you did not verify: Production prompt caching metrics (requires deployed environment).

Compatibility / Migration

  • Backward compatible? Yes -- purely additive field, no existing behavior changes
  • Config/env changes? No
  • Migration needed? No

Failure Recovery (if this breaks)

  • How to disable/revert: Revert commit. No config to change.
  • Files to restore: src/plugins/types.ts, src/plugins/hooks.ts, src/agents/pi-embedded-runner/run/attempt.ts
  • Known bad symptoms: appendContext text missing from agent prompt (would indicate merge regression)

Risks and Mitigations

None -- purely additive type field with no behavioral impact on existing code paths.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8d9471caad

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@dgarson dgarson changed the base branch from main to luis/onboarding-v2 March 2, 2026 16:04
@dgarson dgarson changed the base branch from luis/onboarding-v2 to main March 2, 2026 16:04
@dgarson dgarson force-pushed the dgarson/add-append-context-for-prompt-caching branch from 8d9471c to da43291 Compare March 2, 2026 16:07
@dgarson dgarson changed the base branch from main to dgarson/fork March 3, 2026 01:02
Cherry-picked from da43291 with conflict resolution.

Conflict resolved in src/agents/pi-embedded-runner/run/attempt.ts:
- Added appendContext handling after prependContext
- Added legacySystemPrompt override support
- Changed systemPromptText from const to let to allow reassignment

Original commit message:
Plugins returning prependContext from before_prompt_build inject text
before the user prompt, which is part of the stable system-prompt
prefix and benefits from prompt caching. However, transient or
per-turn context (e.g. enforcement nudges, dynamic warnings) inserted
via prependContext invalidates the cached prefix for the entire block.

Add appendContext as the counterpart: text returned in appendContext
is appended after the user prompt, leaving the stable prefix intact
and preserving prompt cache hits for the prependContext portion.

Merge semantics mirror prependContext — multiple hooks' appendContext
values are concatenated with "\n\n" separators. Empty results are
coerced to undefined to avoid polluting the return type.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@dgarson dgarson force-pushed the dgarson/add-append-context-for-prompt-caching branch from da43291 to 3371142 Compare March 3, 2026 01:49
@dgarson dgarson merged commit a777c05 into dgarson/fork Mar 3, 2026
1 of 3 checks passed
@dgarson dgarson deleted the dgarson/add-append-context-for-prompt-caching branch March 3, 2026 01:55
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