Skip to content

fix(session/database): delete a session's events explicitly in Delete - #1774

Merged
wolo-lab merged 4 commits into
google:mainfrom
davideimola:fix/database-delete-orphaned-events
Nov 5, 2026
Merged

wolo-lab merged 4 commits into
google:mainfrom
davideimola:fix/database-delete-orphaned-events

Conversation

@davideimola

Copy link
Copy Markdown
Contributor

Closes: #1773

Problem

Delete relied on the ON DELETE CASCADE foreign key to remove a session's events. SQLite enforces foreign keys only when the connection enables them (PRAGMA foreign_keys), so on a default SQLite connection Delete removed the session and left its events, tool responses included, in the events table. adk-python enables the pragma for SQLite, so the Go implementation diverged.

Solution

Inside the existing transaction, delete the session's events explicitly before deleting the session. This works on every dialect and does not depend on how the caller configured the connection, which matters for NewSessionServiceFromDB, where the *gorm.DB comes from the user. Enabling the pragma inside ADK would not cover that case: the pragma is per connection and the pool belongs to the caller.

Behavior change for existing users

  • SQLite without PRAGMA foreign_keys: Delete now removes the session's events, which it previously left behind. Events already orphaned by earlier deletes are not cleaned up.
  • Postgres, MySQL, and SQLite with foreign keys on: no observable change, the events were already removed by the cascade.

Testing plan

  • New TestDatabaseService_DeleteRemovesSessionEvents: two sessions with one event each, delete one, assert its events are gone and the other session's event remains. The test database is SQLite without the pragma, like the existing helpers. Without the source change it fails with events left for deleted session = 1, want 0; with it, it passes.
  • go test -race -count=1 -shuffle=on ./session/... passes.
  • golangci-lint run (v2.3.1) reports 0 issues, golangci-lint fmt --diff is empty, go mod tidy -diff is clean.
  • Manual E2E: the reproduction from DatabaseService.Delete leaves the session's events behind on SQLite #1773 run against this branch (via a replace directive) now leaves no events behind:
== default DSN (file:adk-default.db)
Delete returned no error. Rows left for session e6ed9e6c-d4d5-4d1c-9e30-6e65ec98c376: sessions=0 events=0
== foreign keys on (file:adk-fk.db?_pragma=foreign_keys(1))
Delete returned no error. Rows left for session 2d0ee8f1-5863-432b-86ad-bdfdcc562c4f: sessions=0 events=0

Delete relied on the ON DELETE CASCADE foreign key to remove a session's events. SQLite enforces foreign keys only when the connection enables them, so on a default SQLite connection Delete removed the session row and left its events, tool output included, in the events table.
Comment thread session/database/service_test.go Resolved
Comment thread session/database/service.go Resolved
Create sessions that share the session ID with a different user and a
different app, and assert their events survive deleting the target
session, so a delete that drops app_name or user_id from its condition
fails the test.
@davideimola
davideimola requested a review from prami25r October 22, 2026 07:46

@wolo-lab wolo-lab left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@wolo-lab
wolo-lab merged commit 7af0f5f into google:main Nov 5, 2026
14 checks passed
wolo-lab added a commit that referenced this pull request Nov 16, 2026
, #1919, #2189, #2285) (#2346)

* fix(session/vertexai): drop temp: keys from the initial state on Create (#2100)

(cherry picked from commit 0c109f7)

* fix(session): keep in-memory Get from returning events before After (#2089)

(cherry picked from commit 10395f0)

* fix(session/database): lock app and user state rows in Create (#1930)

(cherry picked from commit 2f3b2c2)

* fix(session/database): delete a session's events explicitly in Delete (#1774)

(cherry picked from commit 7af0f5f)

* fix(session): define List order as last update time ascending (#1919)

(cherry picked from commit 8be7e25)

* fix(session): skip nil parts in IsFinalResponse classification (#2189)

(cherry picked from commit 75ae920)

* fix(session): count a final error event as a final response (#2285)

(cherry picked from commit d9ecb34)
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.

DatabaseService.Delete leaves the session's events behind on SQLite

3 participants