fix: allow session id reuse after delete#2331
Conversation
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>
0916d70 to
4ef9dc4
Compare
There was a problem hiding this comment.
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
UpsertSessionto cleardeleted_at, resetcreated_atonly 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.
|
Discussed a similar case in this thread, related to resurrecting tasks. |
|
Thanks for the pointer, hadn't seen that thread. Having read it, I think sessions warrant the opposite choice from tasks. Tasks have generated ids and an id-only PK, so permanently deleting an id costs nothing and closed a real cross-user takeover race. Sessions are the inverse: #2279 exists precisely because clients derive session ids deterministically from an external key (contextID), so an id that stays deleted forever would permanently brick recreate for that key. And the conflict target here is (id, user_id), so a resurrect only ever touches the caller's own row; the cross-user surprise from the tasks thread can't happen. Given that, I see two reasonable designs:
I went with 1 because it doesn't change delete semantics and I didn't want to assume the retention question. Are you okay with this solution? |
Fixes #2279.
What
DeleteSessionsoft-deletes, butUpsertSession'sON CONFLICTupdate never cleareddeleted_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.UpsertSessionnow resurrects a soft-deleted (id, user_id) as a fresh incarnation, in one statement:deleted_atand restartscreated_atNULL-owned tasks are left alone: the
created_atrestart 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_atpreserved).Tests
TestSessionRecreateAfterDelete(recreate succeeds; events/tasks/shares empty; another user's same-id session untouched) andTestUpsertLiveSessionKeepsHistory(no purge / nocreated_atreset on live upsert).