Skip to content

perf(api): skip saved prompts when loading agent history - #44529

Open
Takuya-ops wants to merge 1 commit into
langgenius:mainfrom
Takuya-ops:fix/agent-history-memory-quadratic
Open

Takuya-ops wants to merge 1 commit into
langgenius:mainfrom
Takuya-ops:fix/agent-history-memory-quadratic

Conversation

@Takuya-ops

Copy link
Copy Markdown
Contributor

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

Fixes #44528

This is the agent-app counterpart of #44521, which covers chat history and the chatflow thread-length helper. The two PRs touch different files and can be merged in either order.

On every agent reply, BaseAgentRunner.organize_agent_history loads every Message row of the conversation, with no limit, as whole rows. Those rows include Message.message, which stores the full prompt of the turn: earlier history, the query and that turn's tool calls and results. Building history therefore decoded about n²/2 turns of text on every reply. The method never reads that column. It rebuilds history from query, answer and the agent thoughts.

This PR adds load_only to the query, so it selects just id, conversation_id, parent_message_id, query and answer. conversation_id is needed because organize_agent_user_prompt calls Message.app_model_config_with_session when a message has files. The rebuilt history is unchanged.

Tests:

  • I added a test that persists a message chain and expunges the session so the rows are really reloaded. It checks two things: the single FROM messages query does not select messages.message, and no per-row lazy load follows. It also checks the rebuilt prompts. The test fails on main and passes with this change.
  • make lint and make type-check pass.
  • pytest tests/unit_tests/core tests/unit_tests/services: 15,834 passed, 4 skipped.

Screenshots

These numbers come from calling the real organize_agent_history against PostgreSQL 15, with one call per fresh process. The conversation and the saved prompts are described in #44528, which also has the script.

Turns Before: RSS growth Before: time After: RSS growth After: time
100 26.5 MB 0.19 s 5.8 MB 0.14 s
300 240.6 MB 0.93 s 7.2 MB 0.30 s
500 692.8 MB 2.13 s 8.8 MB 0.56 s
700 1,355.6 MB 4.17 s 10.3 MB 0.80 s

The remaining time comes from the existing per-message MessageFile and MessageAgentThought queries. This PR does not change them.

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've verified the change and added or updated tests where meaningful regression risk justifies coverage.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and vp staged (frontend) to appease the lint gods

🤖 Generated with Claude Code

From Claude Code

BaseAgentRunner.organize_agent_history selected whole Message rows for the
entire conversation. Message.message stores the full prompt of each turn,
earlier history and tool results included, so every agent reply decoded
about n^2/2 turns of text even though history is rebuilt from query and
answer.

Load only the columns the history needs. The rebuilt history is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Agent history loads every turn's saved prompt for the whole conversation, so API memory grows quadratically

1 participant