Skip to content

fix: use single connection for upsert_by_logical_key#135

Merged
cmeans-claude-dev[bot] merged 1 commit into
mainfrom
fix/upsert-logical-key-single-conn
Mar 31, 2026
Merged

fix: use single connection for upsert_by_logical_key#135
cmeans-claude-dev[bot] merged 1 commit into
mainfrom
fix/upsert-logical-key-single-conn

Conversation

@cmeans-claude-dev
Copy link
Copy Markdown
Contributor

@cmeans-claude-dev cmeans-claude-dev Bot commented Mar 31, 2026

Summary

  • Refactored upsert_by_logical_key in postgres_store.py to perform the INSERT attempt, existing-row fetch, and conditional UPDATE within a single pooled connection and transaction
  • Previously acquired up to 3 separate connections (_query_entries and update_entry each took their own), causing unnecessary pool contention under concurrency
  • Addresses MEDIUM Non-blocking cleanup, tools reference, screenshot resize #2 from the code audit

QA

Prerequisites

  • pip install -e ".[dev]"
  • Deploy to test instance on alternate port (AWARENESS_PORT=8421)

Manual tests (via MCP tools)

    • Create entry via upsert
    remember(source="qa-upsert-test", description="original description", tags=["qa-upsert"], logical_key="upsert-qa-key")
    

    Expected: entry created successfully, returns the new entry

    • Update entry via upsert (same logical_key)
    remember(source="qa-upsert-test", description="updated description", tags=["qa-upsert", "v2"], logical_key="upsert-qa-key")
    

    Expected: existing entry updated (not duplicated), description and tags reflect new values, changelog records the change

    • Verify single entry exists
    get_knowledge(source="qa-upsert-test", tags=["qa-upsert"])
    

    Expected: exactly one entry with logical_key="upsert-qa-key", showing updated description and tags

    • Cleanup
    delete_entry(id="<entry-id-from-step-1>")
    

🤖 Generated with Claude Code

@cmeans-claude-dev cmeans-claude-dev Bot added the Dev Active Developer is actively working on this PR; QA should not start label Mar 31, 2026
@github-actions github-actions Bot added the Awaiting CI Dev complete, waiting for CI/Codecov to pass before QA label Mar 31, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 31, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Refactor upsert_by_logical_key to perform the INSERT attempt,
existing-row fetch, and conditional UPDATE within a single pooled
connection and transaction. Previously acquired up to 3 separate
connections (_query_entries + update_entry each took their own),
causing unnecessary pool contention under concurrency.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@cmeans cmeans force-pushed the fix/upsert-logical-key-single-conn branch from 206dd80 to 99e754b Compare March 31, 2026 12:10
@cmeans-claude-dev cmeans-claude-dev Bot removed the Dev Active Developer is actively working on this PR; QA should not start label Mar 31, 2026
@github-actions github-actions Bot added Ready for QA Dev work complete — QA can begin review and removed Awaiting CI Dev complete, waiting for CI/Codecov to pass before QA labels Mar 31, 2026
@cmeans cmeans added QA Active QA is actively reviewing; Dev should not push changes and removed Ready for QA Dev work complete — QA can begin review labels Mar 31, 2026
Copy link
Copy Markdown
Owner

@cmeans cmeans left a comment

Choose a reason for hiding this comment

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

QA Review — PR #135: Single-connection upsert_by_logical_key (MEDIUM #2)

Reviewer: QA Agent | Date: 2026-03-31

Code Review: PASS

The entire upsert flow (INSERT attempt, existing-row fetch, diff computation, conditional UPDATE) now runs within one with self._pool.connection() as conn, conn.transaction(), conn.cursor() as cur: block. Previously acquired up to 3 connections via _query_entries and update_entry.

Key implementation details:

  • Uses _load_sql("query_entries").format(...) directly instead of calling _query_entries — avoids the separate connection that method acquires.
  • Update logic inlined (changelog computation, update_entry SQL) — mirrors update_entry behavior but within the existing cursor.
  • Early return for no-diff case (if not updates: return (old, False)) — skips unnecessary UPDATE.
  • _cleanup_expired() only called on insert path (correct — updates don't create new entries).

Tests: 554/554 PASS

New test_upsert_by_logical_key_creates_then_updates covers create→update→changelog→single-entry verification. Existing concurrent upsert test still passes.

CI: All green

Findings: Zero

@cmeans
Copy link
Copy Markdown
Owner

cmeans commented Mar 31, 2026

QA Audit — PR #135

CI: All green | Tests: 554/554 pass

# Severity Finding
No findings
Adding Ready for QA Signoff.

@cmeans cmeans added Ready for QA Signoff QA passed — ready for maintainer final review and merge and removed QA Active QA is actively reviewing; Dev should not push changes labels Mar 31, 2026
Copy link
Copy Markdown
Owner

@cmeans cmeans left a comment

Choose a reason for hiding this comment

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

LGTM

@cmeans cmeans added QA Approved Manual QA testing completed and passed and removed Ready for QA Signoff QA passed — ready for maintainer final review and merge labels Mar 31, 2026
@cmeans-claude-dev cmeans-claude-dev Bot merged commit 7ef2a25 into main Mar 31, 2026
39 checks passed
@cmeans-claude-dev cmeans-claude-dev Bot deleted the fix/upsert-logical-key-single-conn branch March 31, 2026 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

QA Approved Manual QA testing completed and passed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant