PageIndex icon indicating copy to clipboard operation
PageIndex copied to clipboard

Local tree text uses cached list position instead of page_index, allowing wrong-page content

Open llz1515 opened this issue 4 weeks ago • 1 comments

Problem

A deliberately out-of-order local page cache makes get_tree() attach page 2's text to a node covering page 1, while get_page_content(..., '1') returns page 1's text. Incomplete and duplicate-page caches can also be reported as retrieval-ready. This evidence comes from constructed abnormal caches; I have not observed the normal indexing writer spontaneously reorder pages.

Baseline and environment

Reproduced on current main at 646deeb650005885f44001471d15ab9eab1bbc13 (rechecked 2026-11-04), Python 3.11.15, macOS 26.6 arm64. Minimal reproduction dependencies: requests 2.34.2, urllib3 2.8.0, PyPDF2 3.0.1, PyYAML 6.0.3 and python-dotenv 1.2.4. Run from the repository root with PYTHON_DOTENV_DISABLED=1 PYTHONPATH=. python repro.py.

No model API or live PageIndex service was called. These are deterministic function/transport reproductions, not real-model end-to-end tests. No issue template or contribution guide was present in the repository at this baseline.

Minimal reproduction

import json
import tempfile
from pageindex.client import PageIndexClient
from pageindex.local_api import LocalAPI
from pageindex.local_store import DocStore

with tempfile.TemporaryDirectory() as directory:
    api = object.__new__(LocalAPI)
    api._store = DocStore(directory)
    meta = {"id": "demo", "name": "demo.pdf", "status": "completed",
            "createdAt": "2026-11-04", "pageNum": 2}
    tree = [{"title": "First", "node_id": "0000", "start_index": 1, "end_index": 1}]
    pages = [{"page_index": 2, "markdown": "SECOND FACT"},
             {"page_index": 1, "markdown": "FIRST FACT"}]
    api._store.save_document("demo", meta, tree, pages)
    client = object.__new__(PageIndexClient)
    client._api = api
    print(json.dumps({"case": "out_of_order", "tree_page_1": client.get_tree("demo")["result"][0]["text"],
                      "page_content_1": client.get_page_content("demo", "1")[0]["markdown"]}))
    for name, rows in (("missing_page", [pages[1]]), ("duplicate_page", [pages[1], pages[1]])):
        api._store.save_document("demo", meta, tree, rows)
        result = client.get_ocr("demo")
        print(json.dumps({"case": name, "retrieval_ready": result["retrieval_ready"],
                          "pageNum": 2, "page_indices": [p["page_index"] for p in result["result"]]}))

Actual result

{"case": "out_of_order", "tree_page_1": "SECOND FACT", "page_content_1": "FIRST FACT"}
{"case": "missing_page", "retrieval_ready": true, "pageNum": 2, "page_indices": [1]}
{"case": "duplicate_page", "retrieval_ready": true, "pageNum": 2, "page_indices": [1, 1]}

Expected behavior and scope

All reads should agree on page identity. Complete, uniquely numbered, out-of-order cache data could be normalized in memory by page_index; invalid or incomplete data should produce a clear stored-data error instead of wrong-page content or retrieval_ready=True. Validate positive integer page indices (excluding booleans), uniqueness, metadata coverage and text types. Empty text on a real blank page should remain valid. This is a cache-read contract issue, not a storage-backend rewrite or a request to silently rewrite existing files.

Cause and related work

LocalAPI._load_tree_with_text creates pdf_pages from list order; add_node_text slices it positionally. PageIndexClient.get_page_content selects by page_index. _require_pages checks unreadable/empty data but not page coverage or numbering. Rechecked open items, recently closed PRs, and issue/PR searches for page_index cache; no dedicated fix for this mismatch was found. #630 concerns broader storage backends; #389/#552 are related local-store/tree work, not a validation fix for these abnormal caches.

Source: tree text loading, page selection

AI-assisted investigation; reproduction results above were executed locally.

llz1515 avatar Nov 04 '26 04:11 llz1515

I can take this. Current main still has the mismatch you describe: _load_tree_with_text builds pdf_pages from list order and add_node_text slices it positionally, while get_page_content selects by page_index; _require_pages only rejects empty data, and _completed_envelope always reports retrieval_ready: true.

Plan: normalize a complete, uniquely numbered cache by page_index before attaching text, and reject caches that miss or repeat a page (or carry a non-positive/non-integer index) with a stored-data error instead of wrong-page text or retrieval_ready. Empty text on a real page stays valid. I'll cover it with tests built on your reproduction (out-of-order, missing, duplicate), keep the normal complete-cache path green, and run the local store/client tests. No model calls involved.

Ethereal49 avatar Nov 05 '26 04:11 Ethereal49