Skip to content

fix(memory): scope queue merge key to (thread_id, agent_name) and skip idle reschedule#2592

Open
octo-patch wants to merge 1 commit intobytedance:mainfrom
octo-patch:fix/memory-queue-agent-key-and-timer-churn
Open

fix(memory): scope queue merge key to (thread_id, agent_name) and skip idle reschedule#2592
octo-patch wants to merge 1 commit intobytedance:mainfrom
octo-patch:fix/memory-queue-agent-key-and-timer-churn

Conversation

@octo-patch
Copy link
Copy Markdown
Contributor

Fixes #2547
Fixes #2548

Problem

Issue #2547 — wrong merge key in memory queue
_enqueue_locked matched pending queue entries by thread_id alone. When two agents ran on the same thread and both called add() before the debounce fired, the second call silently replaced the first, dropping the first agent's conversation from the pending batch.

Issue #2548 — zero-delay timer spin when busy
_process_queue always called _schedule_timer(0) when _processing was True, even if the queue was empty. Repeated calls to add_nowait or flush_nowait during an active processing run would each create a new zero-delay timer, causing unnecessary wakeups and scheduling overhead.

Solution

#2547: Changed the deduplication key from thread_id to (thread_id, agent_name). Entries for different agents on the same thread are now kept as separate queue items and processed independently.

#2548: Added a guard in _process_queue so the follow-up zero-delay timer is only scheduled when self._queue actually contains pending work. No-op reschedules no longer occur.

Testing

Updated test_memory_queue.py:

  • Replaced the old test_process_queue_reschedules_immediately_when_already_processing with two focused tests covering the with-work and no-work branches of the busy-guard.
  • Added test_enqueue_two_agents_same_thread_stored_independently to verify the new (thread_id, agent_name) key keeps separate entries.
  • Added test_enqueue_same_agent_same_thread_merges to confirm same-agent deduplication still works.

All 11 tests pass (uv run pytest tests/test_memory_queue.py -v).

…p idle reschedule

Fixes bytedance#2547: _enqueue_locked used thread_id alone as the deduplication key,
causing updates from different agents on the same thread to overwrite each other.
The merge key is now (thread_id, agent_name) so per-agent pending entries are
kept separate.

Fixes bytedance#2548: _process_queue unconditionally scheduled a zero-delay timer when
_processing was already True, spinning idle wakeups even when the queue was
empty. The reschedule now only fires when there is pending work.

Co-Authored-By: Octopus <liyuan851277048@icloud.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes memory update queue behavior when multiple agents enqueue updates for the same thread, and reduces unnecessary zero-delay rescheduling while processing is already active.

Changes:

  • Scope queue deduplication/merge behavior to (thread_id, agent_name) instead of just thread_id.
  • Avoid rescheduling an immediate (0s) timer from _process_queue when already processing and there’s no pending work.
  • Update/add tests to cover multi-agent enqueue behavior and the “busy + empty queue” no-reschedule case.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
backend/packages/harness/deerflow/agents/memory/queue.py Adjusts merge key to include agent_name and adds a busy-guard to skip idle reschedules.
backend/tests/test_memory_queue.py Updates queue tests for the new keying behavior and adds coverage for the busy-guard branches.

Comment on lines 151 to 157
with self._lock:
if self._processing:
# Preserve immediate flush semantics even if another worker is active.
self._schedule_timer(0)
# Only reschedule if there is pending work; avoids spinning zero-delay
# timers when add_nowait/flush_nowait fire while the worker is busy.
if self._queue:
self._schedule_timer(0)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[harness][memory] Memory queue can spin zero-delay timers while busy [harness][memory] Memory queue merges pending updates across agents

2 participants