fix(sessions): use UPSERT in Save() to persist first-run cron sessions#553
Open
duhd-vnpay wants to merge 1 commit intonextlevelbuilder:mainfrom
Open
fix(sessions): use UPSERT in Save() to persist first-run cron sessions#553duhd-vnpay wants to merge 1 commit intonextlevelbuilder:mainfrom
duhd-vnpay wants to merge 1 commit intonextlevelbuilder:mainfrom
Conversation
Save() used UPDATE-only, which silently affected 0 rows when the session didn't exist in DB yet (first-run cron jobs). This caused two problems: 1. Cron sessions never persisted — lost on restart 2. Stale in-memory cache polluted retry attempts with leftover messages Change UPDATE to INSERT...ON CONFLICT (tenant_id, session_key) DO UPDATE so first-run sessions are created atomically and subsequent saves update the existing row. Fixes nextlevelbuilder#379 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
duhd-vnpay
added a commit
to duhd-vnpay/goclaw
that referenced
this pull request
Mar 30, 2026
Key changes (21 commits): - fix(security): harden env file permissions, block NUL byte injection - fix(agent): panic recovery, unblock stuck /stop, graceful shutdown - fix(providers): prevent crash on cancel, nil guard, thinking signature - refactor(cron): normalize payload columns into dedicated DB fields (000033) - feat(cron): add stateless mode + promote payload fields - fix(store): session Save() UPSERT fallback (upstream merged our PR nextlevelbuilder#553) - fix(config): MCP env resolution, channel field filter, orphan provider - fix(agent): group session unresponsive during team task - fix(tools): delivered media tracker prevents duplicate delivery - fix(channels): display name in [From:] for Telegram/WhatsApp - feat(ui): KG graph depth visualization, perf 808ms→<100ms Local patches preserved: - Project-as-a-channel (ConsumerDeps, MCP project overrides) - Security audit logs (memory.go) - Zalo OA bot fixes Migration collision fixed: - 000033_system_configs → 000037 (was duplicate with cron_payload_columns) - RequiredSchemaVersion bumped to 37 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #379
Problem
PGSessionStore.Save()usesUPDATE sessions ... WHERE session_key = $19. When a cron job runs for the first time, the session row doesn't exist in DB yet. The UPDATE affects 0 rows and returnsnil— silently losing the session.This causes two cascading failures:
ExecuteWithRetryreads leftover messages from the failed attempt's in-memory cache, polluting retries with empty user messages +"..."assistant fallback. The stale messages cause LLM 400 errors (messages.0.content.0.text.text: Field required), and all retry attempts fail with the same error.Fix
Change
Save()fromUPDATEtoINSERT ... ON CONFLICT (tenant_id, session_key) DO UPDATE SET ....Before:
After:
Why UPSERT over alternatives?
The UUID generation cost on conflict is negligible —
uuid.NewV7()is ~50ns.Changes
internal/store/pg/sessions_list.goUPDATE→INSERT ... ON CONFLICT ... DO UPDATEVerification
The fix uses the same
ON CONFLICT (tenant_id, session_key)unique index already used byGetOrCreate()(line 146 ofsessions.go), confirming the constraint exists.🤖 Generated with Claude Code