You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
route_query() checks fallback_only on each document but does not check total_pages. A document with total_pages=0 (race condition between ingest completion and route call, or a data integrity gap) passes all guards, enters routing, and returns estimated_token_reduction='0%' — which is misleading. Worse, the router module may perform integer arithmetic with total_pages=0 causing division-by-zero or negative page-range comparisons.
Root Cause
backend/app/api/routing.py around line 99:
total_pages=sum(d.total_pagesfordindocs) # can be 0# ...ifany(d.fallback_onlyfordindocs):
raise ... # only fallback_only is checked, not total_pages=0
estimate_token_reduction(total_pages=0, ...) returns '0%'. The router module receives total_pages=0 and may compute section_coverage = selected_pages / total_pages → ZeroDivisionError.
Failure Scenario
Tenant uploads a PDF. Ingest starts but has not yet written total_pages to Postgres.
Tenant immediately routes a query against the document (race condition).
Or: returns estimated_token_reduction='0%' — caller believes all tokens are used, no reduction.
Proposed Fix
Add an explicit guard for total_pages=0:
fordocindocs:
ifdoc.total_pages==0:
raiseHTTPException(
status_code=409,
detail={
"code": "DOCUMENT_NOT_READY",
"document_id": doc.doc_id,
"message": "Document is still being indexed. Retry in a few seconds.",
}
)
ifdoc.fallback_only:
raise ...
This gives callers a clear, retryable signal instead of a cryptic 500.
Activity
piyushmakhija28 commentedon Jun 21, 2026
Fixed in PR #280 (Sprint R3/R4: fix(sprint-r3): root-cause fixes for all 39 bugs from code review round 3 + Sprint R4 round 4)