Skip to content

fix(adk): prevent session event duplication in KAgentSessionService#1500

Merged
EItanya merged 2 commits intokagent-dev:mainfrom
onematchfox:fix-session-event-duplication
Mar 13, 2026
Merged

fix(adk): prevent session event duplication in KAgentSessionService#1500
EItanya merged 2 commits intokagent-dev:mainfrom
onematchfox:fix-session-event-duplication

Conversation

@onematchfox
Copy link
Contributor

Fixes a session event duplication bug in KAgentSessionService.get_session where every event loaded from the database was appended twice to session.events. get_session constructed the Session with events=events (pre-populating the list), then immediately called super().append_event() for each event. BaseSessionService.append_event unconditionally calls session.events.append(event), so every event ended up in the list twice.

The bug was latent for normal usage because the LLM tolerates redundant history in simple Q&A flows. It was discovered while testing #1491 locally, where the duplicated context caused incorrect agent behaviour. However, it does cause context bloat since, ultimately, every conversation turn currently sends 2× the necessary history to the model. 😬

Signed-off-by: Brian Fox <878612+onematchfox@users.noreply.github.com>
Signed-off-by: Brian Fox <878612+onematchfox@users.noreply.github.com>
@onematchfox onematchfox force-pushed the fix-session-event-duplication branch from 67fc1af to 7c62ef3 Compare March 13, 2026 15:16
@onematchfox onematchfox marked this pull request as ready for review March 13, 2026 15:55
Copilot AI review requested due to automatic review settings March 13, 2026 15:55
Copy link
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 duplicated session events in KAgentSessionService.get_session by avoiding pre-populating Session.events before re-appending via BaseSessionService.append_event.

Changes:

  • Initialize Session with an empty events list in get_session to prevent double-appends.
  • Add unit tests covering 404/empty responses, event ID preservation, non-duplication, and state-delta expectations.

Reviewed changes

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

File Description
python/packages/kagent-adk/src/kagent/adk/_session_service.py Stops event duplication by constructing Session with events=[] before appending events via the base service.
python/packages/kagent-adk/tests/unittests/test_session_service.py Adds regression tests to ensure events aren’t duplicated and basic session loading behavior is correct.
Comments suppressed due to low confidence (1)

python/packages/kagent-adk/tests/unittests/test_session_service.py:1

  • These assertions won’t actually fail if the same state_delta is applied twice via dict.update(...) (applying {'counter': 7} twice still results in 7, and setting key_a/key_b twice is also indistinguishable). To make this a real regression test, consider asserting the number of base append_event / _update_session_state invocations (e.g., by spying/patching) equals len(events), or use a non-idempotent delta format if the ADK supports one.
"""Tests for KAgentSessionService."""

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +50 to +51
mock_response.raise_for_status = MagicMock()

@EItanya EItanya merged commit 6c152e9 into kagent-dev:main Mar 13, 2026
26 of 27 checks passed
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.

4 participants