Repository navigation
fix: apply order_by return value in deep-pagination helpers (unordered OFFSET/LIMIT can skip/dupe rows) - #17325
Conversation
The "get all ... " helpers in api/db/services called query.order_by(create_time.asc()) on its own line and discarded the result. Peewee query methods return a new query and never mutate in place, so the bare call is a no-op and the intended ORDER BY was silently dropped. Each helper then deep-paginates the unordered query with OFFSET/LIMIT, which is undefined without a stable ORDER BY and can skip or duplicate rows across batches. Assign the order_by result back so the ordering is actually applied. Co-Authored-By: Claude Opus 4.8 <[email protected]>
📝 WalkthroughWalkthroughThe changes ensure ascending ChangesPagination ordering
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
test/unit_test/api/db/services/test_deep_pagination_order_by.py (1)
90-141: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winExtend coverage to the other 4 fixed helpers.
This suite only covers 4 of the 8 helpers fixed in this PR.
DocumentService.get_all_doc_ids_by_kb_ids,DocumentService.get_all_docs_by_creator_id,KnowledgebaseService.get_all_kb_by_tenant_ids, andUserCanvasService.get_all_agents_by_tenant_idsgot the identicalorder_by()reassignment fix but have no regression test, so a future accidental regression on those call sites (e.g. someone refactoring the loop) wouldn't be caught.✅ Example additional test (repeat pattern for the other 3 helpers)
from api.db.services import document_service, knowledgebase_service, canvas_service `@pytest.mark.p2` def test_document_helper_orders_by_create_time(monkeypatch): _patch(monkeypatch, document_service, document_service.DocumentService) rows = document_service.DocumentService.get_all_doc_ids_by_kb_ids(["kb-1"]) assert [r["id"] for r in rows] == ["a", "b", "c"]🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/unit_test/api/db/services/test_deep_pagination_order_by.py` around lines 90 - 141, Extend the regression coverage in the existing pagination-order test module by importing the document, knowledgebase, and canvas service modules and adding tests matching the existing helper pattern. Cover DocumentService.get_all_doc_ids_by_kb_ids, DocumentService.get_all_docs_by_creator_id, KnowledgebaseService.get_all_kb_by_tenant_ids, and UserCanvasService.get_all_agents_by_tenant_ids using _patch and assert each returns IDs in the expected create_time order.api/db/services/conversation_service.py (1)
171-185: 📐 Maintainability & Code Quality | 🔵 Trivial | 🏗️ Heavy liftDuplicated offset/limit batch-loop is why this bug recurred 8 times. Each helper repeats the same "order → offset/limit until empty" loop; this duplication is exactly why the discarded-
order_by()bug fixed in this PR had to be fixed independently at 8 call sites. A shared helper (e.g. a generator that takes a query and batch size and yields all rows increate_timeorder) would collapse this to one implementation and prevent the same class of bug at a future 9th call site.
api/db/services/conversation_service.py#L171-L185: replaceget_all_conversation_by_dialog_ids's manual loop with the shared helper.api/db/services/dialog_service.py#L259-L274: replaceget_all_dialogs_by_tenant_id's manual loop with the shared helper.api/db/services/document_service.py#L355-L371: replaceget_all_doc_ids_by_kb_ids's manual loop with the shared helper.api/db/services/document_service.py#L429-L445: replaceget_all_docs_by_creator_id's manual loop with the shared helper.api/db/services/file_service.py#L184-L199: replaceget_all_file_ids_by_tenant_id's manual loop with the shared helper.api/db/services/user_canvas_version.py#L53-L68: replaceget_all_canvas_version_by_canvas_ids's manual loop with the shared helper.api/db/services/knowledgebase_service.py#L231-L261: replaceget_all_kb_by_tenant_ids's manual loop with the shared helper.api/db/services/canvas_service.py#L68-L87: replaceget_all_agents_by_tenant_ids's manual loop with the shared helper.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@api/db/services/conversation_service.py` around lines 171 - 185, Replace the duplicated ordered offset/limit loops with one shared batching helper that accepts a query and batch size, applies create_time ordering, and yields all rows until exhausted. Update get_all_conversation_by_dialog_ids in api/db/services/conversation_service.py:171-185, get_all_dialogs_by_tenant_id in api/db/services/dialog_service.py:259-274, get_all_doc_ids_by_kb_ids in api/db/services/document_service.py:355-371, get_all_docs_by_creator_id in api/db/services/document_service.py:429-445, get_all_file_ids_by_tenant_id in api/db/services/file_service.py:184-199, get_all_canvas_version_by_canvas_ids in api/db/services/user_canvas_version.py:53-68, get_all_kb_by_tenant_ids in api/db/services/knowledgebase_service.py:231-261, and get_all_agents_by_tenant_ids in api/db/services/canvas_service.py:68-87 to consume the helper while preserving each method’s existing query and returned rows.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@api/db/services/conversation_service.py`:
- Around line 171-185: Replace the duplicated ordered offset/limit loops with
one shared batching helper that accepts a query and batch size, applies
create_time ordering, and yields all rows until exhausted. Update
get_all_conversation_by_dialog_ids in
api/db/services/conversation_service.py:171-185, get_all_dialogs_by_tenant_id in
api/db/services/dialog_service.py:259-274, get_all_doc_ids_by_kb_ids in
api/db/services/document_service.py:355-371, get_all_docs_by_creator_id in
api/db/services/document_service.py:429-445, get_all_file_ids_by_tenant_id in
api/db/services/file_service.py:184-199, get_all_canvas_version_by_canvas_ids in
api/db/services/user_canvas_version.py:53-68, get_all_kb_by_tenant_ids in
api/db/services/knowledgebase_service.py:231-261, and
get_all_agents_by_tenant_ids in api/db/services/canvas_service.py:68-87 to
consume the helper while preserving each method’s existing query and returned
rows.
In `@test/unit_test/api/db/services/test_deep_pagination_order_by.py`:
- Around line 90-141: Extend the regression coverage in the existing
pagination-order test module by importing the document, knowledgebase, and
canvas service modules and adding tests matching the existing helper pattern.
Cover DocumentService.get_all_doc_ids_by_kb_ids,
DocumentService.get_all_docs_by_creator_id,
KnowledgebaseService.get_all_kb_by_tenant_ids, and
UserCanvasService.get_all_agents_by_tenant_ids using _patch and assert each
returns IDs in the expected create_time order.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: ecfa1288-341b-4429-897f-7c9cd6c18d93
📒 Files selected for processing (8)
api/db/services/canvas_service.pyapi/db/services/conversation_service.pyapi/db/services/dialog_service.pyapi/db/services/document_service.pyapi/db/services/file_service.pyapi/db/services/knowledgebase_service.pyapi/db/services/user_canvas_version.pytest/unit_test/api/db/services/test_deep_pagination_order_by.py
Summary
The fix is one line per helper — assign the
order_by(...)result back so theordering is actually applied to the query.