Repository navigation
feat(agent): keep early chat context with a rolling summary of older messages - #15817
Draft
viktoravelino wants to merge 5 commits into
Draft
viktoravelino wants to merge 5 commits into
viktoravelino wants to merge 5 commits into
Conversation
…messages Add an opt-in "Summarize Older Messages" Agent input. When on, messages older than the chat history window are folded into a per-session summary (one model call per 20 messages) that is prepended to the history as a user-role message, so a small window no longer forgets facts from the start of the session. - session_memory_state table (EXPAND migration) with optimistic versioning - lfx dispatch + stubs; standalone lfx and ephemeral runs fall back to last-N - cleanup on session delete, flow delete and end-user erase Refs #15799
Contributor
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Contributor
|
✅ Migration Validation Passed All migrations follow the Expand-Contract pattern correctly. |
- Break timestamp ties by message id so the history window and the older range agree, and keep tied rows when the cursor message was deleted - Store summary timestamps as timezone-aware UTC (SQLite read them back naive) - Delete summaries with shared-playground sessions and on builder-account erase - Fold up to 3 batches per turn on a long backlog instead of sending an out-of-order block, and pause folding for 10 minutes after a failed fold Refs #15799
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Agent resends the newest
n_messagesstored messages (100 by default) on every model call and silently drops anything older. So you either pay for 100 raw messages per call, or lower N and the Agent forgets what was said at the start of the session (the project, the customer, a decision).This is a POC for #15799. It adds an opt-in Summarize Older Messages switch to the Agent, off by default:
The system prompt is unchanged, and the summary is never stored as a chat message.
How it works
Storage. A new
session_memory_statetable (EXPAND migration) holds one row per flow, message owner, session and context. Each row has the summary, a cursor at the last folded message, and aversionused for optimistic concurrency.Each turn, the Agent loads the newest N messages as today, then fetches the unfolded messages between the cursor and that window:
Fallbacks. If the summary call fails, the turn runs with the previous summary and folding pauses for 10 minutes for that session. A long backlog folds at most 3 batches per turn; unfolded messages are sent as-is only when fewer than 20 remain, so the prompt never gets an out-of-order block. If another run saves first, the winner's summary is used. Standalone lfx (no database) and runs whose messages aren't stored fall back to today's last-N.
Cleanup. State rows are deleted with their session (both session-delete endpoints), with their flow (
cascade_delete_flow), and by the end-user erase step.Toggle off is unchanged behaviour.
Results
I tested against a real model (
gpt-4o-mini): one session, a first message stating "project PRJ-123 on the EU cluster", then 60 filler turns including a ~5,000-character pasted log, then "Which cluster and project were we on?":How to test
Run the backend with components loaded from source, so the new input shows up, and with INFO logs, so you can see the folds:
1. Recall after the window
gpt-4o-mini.2and leave Summarize Older Messages off.Expected: the Agent doesn't know.
Expected:
session_summary_fold latency_ms=… folded=20;Doing the toggle-off check first keeps the right answer out of the recent window.
2. Inspect the stored summary. Query the
session_memory_statetable in your Langflow database, for exampleselect session_id, version, folded_count, summary from session_memory_state;.Expected: a "Key facts" plus "Conversation so far" summary of at most 300 words.
versioncounts the folds.3. Cleanup. Delete that session from the Playground's session list.
Expected: its
session_memory_staterow is gone.4. Edge cases
0: memory is off, and no summary is read or written.Known gaps (POC)
LFX_DEV, or aftermake build_component_index.Refs #15799