You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "weiqingy (via GitHub)" <gi...@apache.org> on 2026/08/12 05:41:49 UTC

[PR] [runtime][api] Scope long-term memory operations to the action that obtained the memory set [flink-agents]

weiqingy opened a new pull request, #1002:
URL: https://github.com/apache/flink-agents/pull/1002

   Linked issue: #998
   
   ### Purpose of change
   
   `Mem0LongTermMemory` is shared across partition keys and holds the current key, observation id and suppression flag in mutable fields. `switch_context` writes them on the mailbox thread before each action, while `add`, `get`, `search` and `delete` can run on a worker thread when an action passes them to `durable_execute_async`, and those operations read the fields themselves. The key they see is whatever the mailbox thread wrote most recently, not the key of the action that submitted the work.
   
   The key is the isolation boundary rather than observability metadata: it reaches Mem0 as `agent_id`, and two keys sharing a job id and a memory set name are separated by `agent_id` alone. Reading the wrong one means one key's items land in, or are read from, another key's set. The observation id and suppression flag come from the same fields, so observations can be misattributed independently.
   
   This binds the partition key, observation id and suppression flag onto the `MemorySet` when it is created, and has the four operations take them from the set. The four already receive `memory_set` as their first parameter, so nothing new is threaded through. A set is consequently scoped to one action and must not be held across actions.
   
   Java is covered in the same change rather than separately. Its operations all delegate into the same Python object, and the bridge rebuilt the Python set from its name alone, so once the Python side sources the key from the set, a Java-originated call would arrive without one. The Java wrapper now records the context when it switches and carries it across the bridge, and `to_python_memory_set` requires the key rather than defaulting it.
   
   Operating on a set that carries no binding raises. Mem0 tests `agent_id` for truthiness instead of matching on it, so an unbound set would widen an operation to every key sharing the job id and set name, which for a delete removes another key's items. An empty string is a legal partition key and is unaffected: the check is on `None` and `null` only.
   
   `delete_memory_set` is unchanged. It takes a name rather than a `MemorySet`, so it has no bound context and applies to the key currently in scope, which its documentation now records along with the fact that it can target a different key than `MemorySet.delete` on a same-named set. Giving it the same isolation means changing its signature, which is a public API question left open on #998.
   
   ### Tests
   
   Three new tests and two adapted, plus one new Java test.
   
   | Test | What it pins |
   |---|---|
   | `test_memory_set_stays_on_its_own_key_after_the_owner_switches` | all four operations keep the set's key after the owner switches |
   | `test_observations_stay_with_the_action_that_obtained_the_set` | observation ownership follows the set for add, get, search and delete |
   | `test_suppression_follows_the_set_not_the_current_context` | the set's own suppression flag decides, in both directions |
   | `test_unbound_memory_set_is_refused_rather_than_widened` | all four operations raise, and none reaches Mem0 |
   | `testForwardedSetCarriesTheContextItWasObtainedIn` | the Java bridge forwards the context bound at creation |
   | `testUnboundSetIsRefusedRatherThanWidened` | the Java guard throws before forwarding |
   
   `test_switch_context` and `test_context_switch_changes_observation_owner_and_current_suppression` reused a single set across a context switch and expected the current key to apply. They now assert that a set keeps its own key, and obtain one per context.
   
   Each assertion was checked against a deliberately reverted implementation: reverting the suppression binding, the observation id binding, either guard body, or the key binding itself each leaves at least one test failing, and reverting the key binding fails five including both parameterisations of `test_switch_context`.
   
   122 Python tests pass across `api/memory`, `runtime/memory` and `runtime/tests`. 75 Java tests pass across `MemorySetTest`, `Mem0LongTermMemoryTest`, `MemoryRefTest`, `TestMemoryObservationFlush`, `ActionTaskContextManagerTest` and `ActionExecutionOperatorTest`. `ruff check`, `ruff format --check` and `spotless:check` are clean.
   
   The long-term memory e2e test is gated on `ACTION_API_KEY` and needs a Flink cluster plus a local Ollama embedding model, so end-to-end coverage comes from CI.
   
   ### API
   
   `MemorySet` gains three fields in both languages, excluded from serialization like the existing long term memory back-reference. Java adds `setActionContext` and three getters. `BaseLongTermMemory.get_memory_set` / `getMemorySet` keeps its signature and gains the documented per-action scope. The runtime bridge helper `to_python_memory_set` now requires the partition key; its only caller is the Java wrapper, and a version mismatch fails loudly with a missing-argument error rather than silently.
   
   Existing code that obtains a memory set inside the action using it is unaffected. Code that cached one across actions was already reading whichever key happened to be current, and now raises or stays on its original key instead.
   
   ### Documentation
   
   - [ ] `doc-needed`
   - [ ] `doc-not-needed`
   - [x] `doc-included`
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [runtime][api] Scope long-term memory operations to the action that obtained the memory set [flink-agents]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on PR #1002:
URL: https://github.com/apache/flink-agents/pull/1002#issuecomment-5489678871

   @wenjin272 Thanks for the review. Rebased onto main and pushed.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [runtime][api] Scope long-term memory operations to the action that obtained the memory set [flink-agents]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on PR #1002:
URL: https://github.com/apache/flink-agents/pull/1002#issuecomment-5466872126

   Good catch, and it turned out to be two jobs: the cross-language one you spotted, and java-it, which runs `SkillsIntegrationTest` behind the same gate. Both fixed here.
   
   One limit worth knowing: secrets are withheld from `pull_request` runs on forks, so this restores coverage on push builds and same-repository PRs rather than on fork PRs.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [runtime][api] Scope long-term memory operations to the action that obtained the memory set [flink-agents]

Posted by "wenjin272 (via GitHub)" <gi...@apache.org>.
wenjin272 commented on code in PR #1002:
URL: https://github.com/apache/flink-agents/pull/1002#discussion_r3841621852


##########
python/flink_agents/runtime/memory/mem0/mem0_long_term_memory.py:
##########
@@ -429,18 +446,33 @@ def drain_ltm_observation_records(self, key: str, observation_id: str) -> str:
     def get_memory_set(self, name: str) -> MemorySet:
         """Get the memory set by name.
 
+        The current partition key and observation context are copied onto the set
+        so that operations submitted to a worker thread stay scoped to the action
+        that obtained it. Must be called on the mailbox thread.

Review Comment:
   **[P1] Enforce mailbox-thread access for memory-set management**
   
   I think `get_memory_set` and `delete_memory_set`, as memory-set-level management operations, should only run on the mailbox thread. Otherwise, passing either to `durable_execute_async` can preserve the same cross-key race. Could we enforce this in both Java and Python, similar to `RunnerContextImpl#getResource`?



##########
python/flink_agents/runtime/memory/mem0/mem0_long_term_memory.py:
##########
@@ -123,6 +123,23 @@ def validate_config(cls, v: Any, values: Any) -> Any:
     return _FlinkAgentsLlmConfig, _FlinkAgentsEmbedderConfig
 
 
+def _bound_partition_key(memory_set: MemorySet) -> str:
+    """Return the partition key the set is scoped to.
+
+    Mem0 ignores a falsy ``agent_id`` rather than matching on it, so an unbound set
+    would widen every operation to all keys sharing the job id and set name, which
+    for a delete means deleting another key's items. Refuse the operation instead.
+    """
+    if memory_set.partition_key is None:

Review Comment:
   **[P1] Handle empty partition keys safely**
   
   `""` is a valid Flink key, but Mem0 ignores a falsy `agent_id`, dropping partition isolation. Please reject empty keys or encode them into a non-empty, collision-free value. Also use `None`/`null`, rather than `""`, for the unbound state.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [runtime][api] Scope long-term memory operations to the action that obtained the memory set [flink-agents]

Posted by "wenjin272 (via GitHub)" <gi...@apache.org>.
wenjin272 merged PR #1002:
URL: https://github.com/apache/flink-agents/pull/1002


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [runtime][api] Scope long-term memory operations to the action that obtained the memory set [flink-agents]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on PR #1002:
URL: https://github.com/apache/flink-agents/pull/1002#issuecomment-5466948680

   The `ut-java [ubuntu-latest] [java-11] [python-3.12]` failure looks unrelated to this PR. Maven Central returned `502 Bad Gateway` while resolving `com.fasterxml.jackson.core:jackson-databind:2.18.2`, so the build stopped during dependency resolution and no test ran. Evidence: [job log](https://github.com/apache/flink-agents/actions/runs/33294432135/job/99211555301).
   
   That version is the existing `jackson.version` pin in the root `pom.xml`, this PR touches no `pom.xml` or dependency file, and the same job passed on the previous commit of this branch.
   
   Re-triggering it once the current run finishes.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [runtime][api] Scope long-term memory operations to the action that obtained the memory set [flink-agents]

Posted by "weiqingy (via GitHub)" <gi...@apache.org>.
weiqingy commented on code in PR #1002:
URL: https://github.com/apache/flink-agents/pull/1002#discussion_r3888534945


##########
python/flink_agents/runtime/memory/mem0/mem0_long_term_memory.py:
##########
@@ -123,6 +123,23 @@ def validate_config(cls, v: Any, values: Any) -> Any:
     return _FlinkAgentsLlmConfig, _FlinkAgentsEmbedderConfig
 
 
+def _bound_partition_key(memory_set: MemorySet) -> str:
+    """Return the partition key the set is scoped to.
+
+    Mem0 ignores a falsy ``agent_id`` rather than matching on it, so an unbound set
+    would widen every operation to all keys sharing the job id and set name, which
+    for a delete means deleting another key's items. Refuse the operation instead.
+    """
+    if memory_set.partition_key is None:

Review Comment:
   PR description updated.
   
   On refusing rather than encoding: to keep already-stored memories readable the mapping has to leave non-empty keys alone, which leaves no free non-empty value for `""` to take. A reserved sentinel just moves which key breaks. Collision-free would mean rewriting every key's `agent_id` and orphaning what is already stored, so refusing looked like the better trade. Does that match how you would weigh it?
   
   Both `None` and `""` are refused now in both languages, with different messages. The unbound default was `""` on both sides, so an unbound Java set already looked bound by the time it reached Python.
   
   Two things worth knowing. On `add`, mem0 drops `agent_id` from the stored metadata, so items written under an empty key stay unrecoverable even after the key is fixed. And an existing test was asserting that `agent_id=""` gets forwarded, so this was pinned as intended behaviour.
   



##########
python/flink_agents/runtime/memory/mem0/mem0_long_term_memory.py:
##########
@@ -429,18 +446,33 @@ def drain_ltm_observation_records(self, key: str, observation_id: str) -> str:
     def get_memory_set(self, name: str) -> MemorySet:
         """Get the memory set by name.
 
+        The current partition key and observation context are copied onto the set
+        so that operations submitted to a worker thread stay scoped to the action
+        that obtained it. Must be called on the mailbox thread.

Review Comment:
   Good point, done in both languages.
   
   Both management methods now run the same mailbox check `getResource` uses. The four set-scoped operations stay unchecked on purpose, since being safe on a worker thread is what binding the context to the set buys.
   
   Java needed its own check rather than relying on the bridge, since `getMemorySet` there never enters Python. On the Python side I passed the checker through the constructor rather than adding one to the `RunnerContext` ABC, since Java's equivalent sits on the runtime class rather than the API interface. Curious whether you would have put it on the interface instead.
   
   `getLongTermMemory` is left alone, since capturing the handle on the mailbox thread and using it inside the callable is normal.
   
   This settles the `delete_memory_set` question without touching its signature. One caveat: on JDK 11 the durable callable runs inline, so the same code will not raise there yet.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org