-
Notifications
You must be signed in to change notification settings - Fork 9.7k
fix(memory): stop cross-flow chat-history leak in legacy flows (LE-1929) #14191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
157 changes: 157 additions & 0 deletions
157
src/backend/tests/unit/test_message_flow_context_scoping.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| """Regression tests: ambient flow-scope defaults ``aget_messages`` by ``flow_id`` (issue #13059). | ||
|
|
||
| Langflow executes the *frozen* component ``code`` embedded in each saved flow, not the installed | ||
| library version (``lfx.interface.initialize.loading.instantiate_class`` -> ``eval_custom_component_code``). | ||
| A flow saved before PR #13087 therefore carries the old ``retrieve_messages`` that calls | ||
| ``aget_messages`` WITHOUT ``flow_id`` and leaks chat history across flows on a colliding | ||
| ``session_id`` — even when running on a patched server. | ||
|
|
||
| Defense-in-depth: the engine binds the executing graph's ``flow_id`` to a ContextVar so | ||
| ``aget_messages`` can default the scope. This closes the leak for old frozen flows without: | ||
| - touching frozen code (impossible), | ||
| - changing callers that pass ``flow_id`` explicitly (the default only applies when it is ``None``), | ||
| - changing callers outside a graph run (the ContextVar is unset -> identical to today). | ||
| """ | ||
|
|
||
| from typing import cast | ||
| from uuid import uuid4 | ||
|
|
||
| from langflow.memory import aadd_messages, aget_messages | ||
| from langflow.schema.message import Message | ||
| from lfx.components.input_output import ChatOutput | ||
| from lfx.components.models_and_agents.memory import MemoryComponent | ||
| from lfx.graph.graph.base import Graph | ||
| from lfx.memory.flow_context import reset_current_flow_id, set_current_flow_id | ||
| from lfx.schema.data import Data | ||
|
|
||
|
|
||
| async def _store(session_id: str, flow_id, text: str) -> None: | ||
| msg = Message(text=text, sender="User", sender_name="User", session_id=session_id) | ||
| await aadd_messages([msg], flow_id=flow_id) | ||
|
|
||
|
|
||
| async def test_aget_messages_defaults_flow_id_from_context(client): # noqa: ARG001 | ||
| """The reproduction: two flows share a session_id; the ambient scope isolates them. | ||
|
|
||
| Simulates old frozen code calling ``aget_messages(session_id=...)`` with no flow_id while | ||
| Flow B's graph is executing. The ambient flow scope must prevent Flow A's row from leaking. | ||
| """ | ||
| flow_a, flow_b = uuid4(), uuid4() | ||
| session_id = "shared-session-13059" | ||
| await _store(session_id, flow_a, "secret from A") | ||
| await _store(session_id, flow_b, "hello from B") | ||
|
|
||
| token = set_current_flow_id(flow_b) | ||
| try: | ||
| scoped = await aget_messages(session_id=session_id) | ||
| finally: | ||
| reset_current_flow_id(token) | ||
|
|
||
| assert [m.text for m in scoped] == ["hello from B"] | ||
|
|
||
|
|
||
| async def test_explicit_flow_id_overrides_context(client): # noqa: ARG001 | ||
| """An explicitly-passed flow_id must win over the ambient context (no silent override).""" | ||
| flow_a, flow_b = uuid4(), uuid4() | ||
| session_id = "shared-session-13059-explicit" | ||
| await _store(session_id, flow_a, "secret from A") | ||
| await _store(session_id, flow_b, "hello from B") | ||
|
|
||
| token = set_current_flow_id(flow_b) | ||
| try: | ||
| a_msgs = await aget_messages(session_id=session_id, flow_id=flow_a) | ||
| finally: | ||
| reset_current_flow_id(token) | ||
|
|
||
| assert [m.text for m in a_msgs] == ["secret from A"] | ||
|
|
||
|
|
||
| async def test_no_context_preserves_legacy_unscoped(client): # noqa: ARG001 | ||
| """With no ambient scope and no explicit flow_id, behavior is unchanged (both rows).""" | ||
| flow_a, flow_b = uuid4(), uuid4() | ||
| session_id = "shared-session-13059-legacy" | ||
| await _store(session_id, flow_a, "secret from A") | ||
| await _store(session_id, flow_b, "hello from B") | ||
|
|
||
| unscoped = await aget_messages(session_id=session_id) | ||
|
|
||
| assert len(unscoped) == 2 | ||
|
|
||
|
|
||
| async def test_context_accepts_string_flow_id(client): # noqa: ARG001 | ||
| """``graph.flow_id`` is commonly a ``str``; the default path must coerce it, not crash. | ||
|
|
||
| ``MessageTable.flow_id`` is UUID-typed; on SQLite comparing it to a raw string makes the | ||
| ``Uuid`` bind processor call ``value.hex`` and raise. The default must coerce str -> UUID. | ||
| """ | ||
| flow_a, flow_b = uuid4(), uuid4() | ||
| session_id = "shared-session-13059-str" | ||
| await _store(session_id, flow_a, "secret from A") | ||
| await _store(session_id, flow_b, "hello from B") | ||
|
|
||
| token = set_current_flow_id(str(flow_b)) | ||
| try: | ||
| scoped = await aget_messages(session_id=session_id) | ||
| finally: | ||
| reset_current_flow_id(token) | ||
|
|
||
| assert [m.text for m in scoped] == ["hello from B"] | ||
|
|
||
|
|
||
| async def test_invalid_context_flow_id_degrades_to_unscoped(client): # noqa: ARG001 | ||
| """A non-UUID ambient flow_id (synthetic/test graphs) must degrade, never crash retrieval.""" | ||
| flow_a, flow_b = uuid4(), uuid4() | ||
| session_id = "shared-session-13059-badctx" | ||
| await _store(session_id, flow_a, "secret from A") | ||
| await _store(session_id, flow_b, "hello from B") | ||
|
|
||
| token = set_current_flow_id("not-a-uuid") | ||
| try: | ||
| result = await aget_messages(session_id=session_id) | ||
| finally: | ||
| reset_current_flow_id(token) | ||
|
|
||
| assert len(result) == 2 | ||
|
|
||
|
|
||
| class _OldStyleMemory(MemoryComponent): | ||
| """Simulates a flow saved before PR #13087: its frozen ``retrieve_messages`` omits ``flow_id``. | ||
|
|
||
| We cannot recreate frozen bytes in a unit test, so we reproduce the one behavior that matters: | ||
| the internal-memory retrieve calls ``aget_messages`` with no ``flow_id``. The engine's ambient | ||
| flow scope must still isolate it. | ||
| """ | ||
|
|
||
| name = "OldStyleMemory" | ||
|
|
||
| async def retrieve_messages(self) -> Data: | ||
| from langflow.memory import aget_messages as backend_aget_messages | ||
|
|
||
| stored = await backend_aget_messages(session_id=self.session_id, order="DESC") | ||
| return cast("Data", list(reversed(stored))) | ||
|
|
||
|
|
||
| async def test_graph_execution_binds_flow_scope_end_to_end(client): # noqa: ARG001 | ||
| """End-to-end: running Flow B's graph must not surface Flow A's message via unscoped frozen code. | ||
|
|
||
| This is the real reproduction path — ``get_instance_results`` binds ``graph.flow_id`` so the | ||
| old-style component's unscoped ``aget_messages`` call is defaulted to Flow B's scope. | ||
| """ | ||
| flow_a, flow_b = uuid4(), uuid4() | ||
| session_id = "shared-session-13059-e2e" | ||
| await _store(session_id, flow_a, "SECRET_FROM_A") | ||
| await _store(session_id, flow_b, "hello from B") | ||
|
|
||
| probe = _OldStyleMemory(_id="old_memory") | ||
| probe.set(session_id=session_id, n_messages=100, order="Ascending") | ||
| chat_output = ChatOutput(_id="chat_output") | ||
| chat_output.set(input_value=probe.retrieve_messages_as_text) | ||
|
|
||
| graph = Graph(probe, chat_output, flow_id=str(flow_b)) | ||
| async for _ in graph.async_start(): | ||
| pass | ||
|
|
||
| rendered = chat_output.get_output_by_method(chat_output.message_response).value | ||
| text = rendered.text if hasattr(rendered, "text") else str(rendered) | ||
| assert "SECRET_FROM_A" not in text, "Flow A's message leaked into Flow B's graph run" | ||
| assert "hello from B" in text |
50 changes: 50 additions & 0 deletions
50
src/frontend/src/utils/__tests__/reconcileNodeDimensions.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /** | ||
| * LE-1929: flows saved when the node card was `w-96` (384px) kept `width: 384`. React Flow then | ||
| * uses that stale width to place the right-side output handle ~64px past the actual `w-80` (320px) | ||
| * card, so edges render detached from the handle. Reconciling drops the persisted dimensions so | ||
| * React Flow re-measures the real DOM size. noteNodes are user-resizable and must be preserved. | ||
| */ | ||
|
|
||
| import { reconcileStaleGenericNodeDimensions } from "../reactflowUtils"; | ||
|
|
||
| describe("reconcileStaleGenericNodeDimensions", () => { | ||
| it("drops stale width/height/measured from genericNodes so React Flow re-measures", () => { | ||
| const nodes: any[] = [ | ||
| { | ||
| id: "Memory-1", | ||
| type: "genericNode", | ||
| width: 384, | ||
| height: 366, | ||
| measured: { width: 384, height: 366 }, | ||
| position: { x: 0, y: 0 }, | ||
| data: {}, | ||
| }, | ||
| ]; | ||
|
|
||
| reconcileStaleGenericNodeDimensions(nodes as any); | ||
|
|
||
| expect(nodes[0].width).toBeUndefined(); | ||
| expect(nodes[0].height).toBeUndefined(); | ||
| expect(nodes[0].measured).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("preserves dimensions on noteNodes (they are user-resizable)", () => { | ||
| const nodes: any[] = [ | ||
| { | ||
| id: "note-1", | ||
| type: "noteNode", | ||
| width: 500, | ||
| height: 300, | ||
| measured: { width: 500, height: 300 }, | ||
| position: { x: 0, y: 0 }, | ||
| data: {}, | ||
| }, | ||
| ]; | ||
|
|
||
| reconcileStaleGenericNodeDimensions(nodes as any); | ||
|
|
||
| expect(nodes[0].width).toBe(500); | ||
| expect(nodes[0].height).toBe(300); | ||
| expect(nodes[0].measured).toEqual({ width: 500, height: 300 }); | ||
| }); | ||
| }); |
108 changes: 108 additions & 0 deletions
108
src/frontend/src/utils/__tests__/updatePathEdgeMigration.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /** | ||
| * Reproduction for LE-1929: updating an outdated saved flow must not drop edges whose | ||
| * output type was renamed (DataFrame -> Table, #11554). | ||
| * | ||
| * Flow B (Memory Chatbot, legacy) has a Memory."dataframe" output that was typed ["DataFrame"] | ||
| * when saved but is ["Table"] in the current library. When the user clicks "Update All", the | ||
| * node definition is swapped to the current one (Table) while the edge still stores the old | ||
| * DataFrame handle. cleanEdges must migrate + keep the edge, not drop it. | ||
| */ | ||
|
|
||
| import { cleanEdges, scapedJSONStringfy } from "../reactflowUtils"; | ||
|
|
||
| const stringify = (obj: object): string => scapedJSONStringfy(obj); | ||
|
|
||
| describe("LE-1929 update-path edge migration (DataFrame -> Table)", () => { | ||
| it("keeps the Memory->TypeConverter edge after the Memory output migrates to Table", () => { | ||
| // Memory node AFTER "Update All": dataframe output is now typed Table. | ||
| const memoryNode: any = { | ||
| id: "Memory-8X8Cq", | ||
| type: "genericNode", | ||
| data: { | ||
| id: "Memory-8X8Cq", | ||
| type: "Memory", | ||
| selected_output: "dataframe", | ||
| node: { | ||
| display_name: "Message History", | ||
| template: {}, | ||
| outputs: [ | ||
| { | ||
| name: "dataframe", | ||
| display_name: "Table", | ||
| types: ["Table"], | ||
| selected: "Table", | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| // TypeConverter node AFTER update: input_data accepts the migrated types. | ||
| const typeConverterNode: any = { | ||
| id: "TypeConverterComponent-koSIz", | ||
| type: "genericNode", | ||
| data: { | ||
| id: "TypeConverterComponent-koSIz", | ||
| type: "TypeConverterComponent", | ||
| node: { | ||
| display_name: "Type Convert", | ||
| template: { | ||
| input_data: { | ||
| type: "other", | ||
| input_types: ["Message", "Data", "JSON", "DataFrame", "Table"], | ||
| display_name: "Input", | ||
| }, | ||
| }, | ||
| outputs: [], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| // Edge as SAVED in the legacy flow: source handle still says DataFrame. | ||
| const sourceHandle = stringify({ | ||
| dataType: "Memory", | ||
| id: "Memory-8X8Cq", | ||
| name: "dataframe", | ||
| output_types: ["DataFrame"], | ||
| }); | ||
| const targetHandle = stringify({ | ||
| fieldName: "input_data", | ||
| id: "TypeConverterComponent-koSIz", | ||
| inputTypes: ["Message", "Data", "DataFrame"], | ||
| type: "other", | ||
| }); | ||
|
|
||
| const edge: any = { | ||
| id: | ||
| "xy-edge__Memory-8X8Cq" + | ||
| sourceHandle + | ||
| "-TypeConverter" + | ||
| targetHandle, | ||
| source: "Memory-8X8Cq", | ||
| target: "TypeConverterComponent-koSIz", | ||
| sourceHandle, | ||
| targetHandle, | ||
| data: { | ||
| sourceHandle: { | ||
| dataType: "Memory", | ||
| id: "Memory-8X8Cq", | ||
| name: "dataframe", | ||
| output_types: ["DataFrame"], | ||
| }, | ||
| targetHandle: { | ||
| fieldName: "input_data", | ||
| id: "TypeConverterComponent-koSIz", | ||
| inputTypes: ["Message", "Data", "DataFrame"], | ||
| type: "other", | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const result = cleanEdges([memoryNode, typeConverterNode], [edge]); | ||
|
|
||
| expect(result.brokenEdges.length).toBe(0); | ||
| expect(result.edges.length).toBe(1); | ||
| // The kept edge must have its source handle rewritten to the migrated Table type. | ||
| expect(result.edges[0].sourceHandle).toContain("Table"); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Use the retained edge reference for every later removal.
After a handle migration rewrites
edgeInNewEdges.id, the source-invalid paths still filter by the originaledge.id(Lines 282 and 306), andfilterHiddenFieldsEdgesreceives that stale edge (Line 312). Those paths then report a broken/hidden edge but leave it innewEdges.Proposed fix
Also applies to: 205-205
🤖 Prompt for AI Agents