Skip to content

fix(agent): run post_llm_call hook before session persistence#14913

Open
aniruddhaadak80 wants to merge 4 commits into
NousResearch:mainfrom
aniruddhaadak80:fix/post-llm-call-persist-order
Open

fix(agent): run post_llm_call hook before session persistence#14913
aniruddhaadak80 wants to merge 4 commits into
NousResearch:mainfrom
aniruddhaadak80:fix/post-llm-call-persist-order

Conversation

@aniruddhaadak80

Copy link
Copy Markdown
Contributor

Summary

Fixes #14894

The post_llm_call plugin hook was invoked after _persist_session(), so any response overrides applied by plugins were never captured in the persisted session history. This caused a mismatch between the final_response returned to the caller and the history stored in SQLite/JSON logs.

Root Cause

In run_conversation():

  1. _persist_session(messages, conversation_history) writes to DB
  2. post_llm_call hook fires — plugin can modify final_response

The hook result was never re-persisted.

Changes

run_agent.pyrun_conversation():

  • Moved _persist_session() to fire after the post_llm_call hook block
  • Added support for plugins to return {"override_response": "..."} dicts
  • When an override is returned, both final_response and the last assistant message are updated before persistence
  • Restored the turn-exit diagnostic log block in its original position

Testing

  • The hook block is guarded by try/except — plugin failures cannot break persistence
  • _persist_session() still runs unconditionally after the hook
  • Turn-exit diagnostic logging is preserved (no regression)
  • Existing test_plugins.py tests remain compatible (invoke_hook return value was previously ignored)

Checklist

  • Follows Conventional Commits format
  • Single logical change
  • No breaking changes
  • Backward compatible (plugins that don't return override_response work as before)

The post_llm_call plugin hook was invoked AFTER _persist_session(),
so any response overrides applied by plugins were never captured in
the persisted session history. This caused a mismatch between the
final_response returned to the caller and the history stored in
SQLite/JSON logs.

Move _persist_session() to fire after the hook block and add support
for plugins to return override_response dicts that update both the
final_response and the last assistant message before persistence.

Closes NousResearch#14894
Copilot AI review requested due to automatic review settings April 24, 2026 04:49
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent loop, run_agent.py, prompt builder comp/plugins Plugin system and bundled plugins labels Apr 24, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder comp/plugins Plugin system and bundled plugins P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: post_llm_call response overrides are applied after persistence, causing final_response/history mismatch

3 participants