From 4184d5ed2c7b16456872ad32b678edfd508fd687 Mon Sep 17 00:00:00 2001 From: rayhpeng Date: Fri, 10 Apr 2026 18:21:55 +0800 Subject: [PATCH] feat(feedback): add upsert() method with UNIQUE enforcement Co-Authored-By: Claude Sonnet 4.6 --- .../deerflow/persistence/feedback/sql.py | 40 +++++++++++++++++++ backend/tests/test_feedback.py | 36 +++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/backend/packages/harness/deerflow/persistence/feedback/sql.py b/backend/packages/harness/deerflow/persistence/feedback/sql.py index f580dc0b1..508c86fa7 100644 --- a/backend/packages/harness/deerflow/persistence/feedback/sql.py +++ b/backend/packages/harness/deerflow/persistence/feedback/sql.py @@ -122,6 +122,46 @@ class FeedbackRepository: await session.commit() return True + async def upsert( + self, + *, + run_id: str, + thread_id: str, + rating: int, + user_id: str | None | _AutoSentinel = AUTO, + comment: str | None = None, + ) -> dict: + """Create or update feedback for (thread_id, run_id, user_id). rating must be +1 or -1.""" + if rating not in (1, -1): + raise ValueError(f"rating must be +1 or -1, got {rating}") + resolved_user_id = resolve_user_id(user_id, method_name="FeedbackRepository.upsert") + async with self._sf() as session: + stmt = select(FeedbackRow).where( + FeedbackRow.thread_id == thread_id, + FeedbackRow.run_id == run_id, + FeedbackRow.user_id == resolved_user_id, + ) + result = await session.execute(stmt) + row = result.scalar_one_or_none() + if row is not None: + row.rating = rating + row.comment = comment + row.created_at = datetime.now(UTC) + else: + row = FeedbackRow( + feedback_id=str(uuid.uuid4()), + run_id=run_id, + thread_id=thread_id, + user_id=resolved_user_id, + rating=rating, + comment=comment, + created_at=datetime.now(UTC), + ) + session.add(row) + await session.commit() + await session.refresh(row) + return self._row_to_dict(row) + async def aggregate_by_run(self, thread_id: str, run_id: str) -> dict: """Aggregate feedback stats for a run using database-side counting.""" stmt = select( diff --git a/backend/tests/test_feedback.py b/backend/tests/test_feedback.py index 972343339..01735ffbb 100644 --- a/backend/tests/test_feedback.py +++ b/backend/tests/test_feedback.py @@ -154,6 +154,42 @@ class TestFeedbackRepository: assert stats["negative"] == 0 await _cleanup() + @pytest.mark.anyio + async def test_upsert_creates_new(self, tmp_path): + repo = await _make_feedback_repo(tmp_path) + record = await repo.upsert(run_id="r1", thread_id="t1", rating=1, user_id="u1") + assert record["rating"] == 1 + assert record["feedback_id"] + assert record["user_id"] == "u1" + await _cleanup() + + @pytest.mark.anyio + async def test_upsert_updates_existing(self, tmp_path): + repo = await _make_feedback_repo(tmp_path) + first = await repo.upsert(run_id="r1", thread_id="t1", rating=1, user_id="u1") + second = await repo.upsert(run_id="r1", thread_id="t1", rating=-1, user_id="u1", comment="changed my mind") + assert second["feedback_id"] == first["feedback_id"] + assert second["rating"] == -1 + assert second["comment"] == "changed my mind" + await _cleanup() + + @pytest.mark.anyio + async def test_upsert_different_users_separate(self, tmp_path): + repo = await _make_feedback_repo(tmp_path) + r1 = await repo.upsert(run_id="r1", thread_id="t1", rating=1, user_id="u1") + r2 = await repo.upsert(run_id="r1", thread_id="t1", rating=-1, user_id="u2") + assert r1["feedback_id"] != r2["feedback_id"] + assert r1["rating"] == 1 + assert r2["rating"] == -1 + await _cleanup() + + @pytest.mark.anyio + async def test_upsert_invalid_rating(self, tmp_path): + repo = await _make_feedback_repo(tmp_path) + with pytest.raises(ValueError): + await repo.upsert(run_id="r1", thread_id="t1", rating=0, user_id="u1") + await _cleanup() + # -- Follow-up association --