Skip to content

fix: allow session id reuse after delete#2331

Open
QuentinBisson wants to merge 1 commit into
kagent-dev:mainfrom
QuentinBisson:fix/session-recreate-after-delete
Open

fix: allow session id reuse after delete#2331
QuentinBisson wants to merge 1 commit into
kagent-dev:mainfrom
QuentinBisson:fix/session-recreate-after-delete

Conversation

@QuentinBisson

Copy link
Copy Markdown
Contributor

Fixes #2279.

What

DeleteSession soft-deletes, but UpsertSession's ON CONFLICT update never cleared deleted_at, so recreating a previously deleted session id upserted an invisible row: the create handler's own reload failed with 500 "Failed to load created session", forever. This breaks any client that derives session IDs deterministically from an external key.

UpsertSession now resurrects a soft-deleted (id, user_id) as a fresh incarnation, in one statement:

  • clears deleted_at and restarts created_at
  • soft-deletes the previous incarnation's events and owned tasks, and drops its shares, so the recreated session starts empty instead of inheriting the old history

NULL-owned tasks are left alone: the created_at restart already hides them from the new incarnation via the ownership bound in tasks.sql, and they may still resolve to another user's same-id session. Upserts of live sessions are unchanged (no purge, created_at preserved).

Tests

TestSessionRecreateAfterDelete (recreate succeeds; events/tasks/shares empty; another user's same-id session untouched) and TestUpsertLiveSessionKeepsHistory (no purge / no created_at reset on live upsert).

@github-actions github-actions Bot added the bug Something isn't working label Jul 25, 2026
UpsertSession resurrects a soft-deleted (id, user_id) as a fresh incarnation: deleted_at is cleared, created_at restarts, and the previous incarnation's events, owned tasks, and shares are purged so the recreated session starts empty.

Fixes kagent-dev#2279

Signed-off-by: QuentinBisson <quentin@giantswarm.io>
@QuentinBisson
QuentinBisson force-pushed the fix/session-recreate-after-delete branch from 0916d70 to 4ef9dc4 Compare July 25, 2026 13:21
@QuentinBisson
QuentinBisson marked this pull request as ready for review July 25, 2026 13:26
@QuentinBisson
QuentinBisson requested a review from a team as a code owner July 25, 2026 13:26
Copilot AI review requested due to automatic review settings July 25, 2026 13:26

Copilot AI 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.

Pull request overview

This PR fixes a correctness issue in the Go/Postgres persistence layer where a session ID could not be reused after a soft delete. It updates the UpsertSession SQL to properly “resurrect” a soft-deleted (id, user_id) session while preventing the recreated session from inheriting prior history.

Changes:

  • Update UpsertSession to clear deleted_at, reset created_at only when resurrecting, and purge prior incarnation data (events, owned tasks, shares) in the same statement.
  • Regenerate SQLC outputs to reflect the new query and add doc comments on the generated Go interface/methods.
  • Add database client tests covering session recreation after delete and ensuring live-session upserts preserve history.

Reviewed changes

Copilot reviewed 2 out of 4 changed files in this pull request and generated no comments.

File Description
go/core/internal/database/queries/sessions.sql Adjusts UpsertSession to resurrect soft-deleted sessions and purge prior data atomically.
go/core/internal/database/gen/sessions.sql.go SQLC-generated Go wrapper updated for the new UpsertSession query and docs.
go/core/internal/database/gen/querier.go SQLC-generated interface comment updated for UpsertSession.
go/core/internal/database/client_test.go Adds tests validating recreate-after-delete behavior and no-op behavior for live upserts.
Files not reviewed (2)
  • go/core/internal/database/gen/querier.go: Generated file
  • go/core/internal/database/gen/sessions.sql.go: Generated file

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

@iplay88keys

iplay88keys commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Discussed a similar case in this thread, related to resurrecting tasks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Session ID cannot be recreated after delete: upsert never clears deleted_at, and old events survive

3 participants