Skip to content

Commit 0cd6955

Browse files
committed
Handle deleted pages in stale ID detection
1 parent fa960ac commit 0cd6955

1 file changed

Lines changed: 40 additions & 15 deletions

File tree

src/doc_builder/embeddings_tracker.py

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,18 @@ def filter_new_chunks(chunks: list, existing_ids: set[str]) -> tuple[list, list]
7171
return new_chunks, existing_chunks
7272

7373

74-
def find_stale_ids(chunks: list, existing_ids: set[str]) -> set[str]:
74+
def find_stale_ids(chunks: list, existing_ids: set[str], existing_dataset=None) -> set[str]:
7575
"""
76-
Find document IDs that are stale (page was updated, so old version should be removed).
76+
Find document IDs that are stale (page was updated or deleted).
7777
78-
When a page is updated, the content hash changes, creating a new ID.
79-
The old ID for the same (library, page) should be removed.
78+
Handles two cases:
79+
1. Page updated: content hash changes, old ID should be removed
80+
2. Page deleted: all IDs for that page should be removed
8081
8182
Args:
8283
chunks: List of current Chunk objects
8384
existing_ids: Set of document IDs that exist in the tracker
85+
existing_dataset: Optional dataset with library info for detecting deleted pages
8486
8587
Returns:
8688
Set of stale document IDs that should be removed
@@ -96,11 +98,25 @@ def find_stale_ids(chunks: list, existing_ids: set[str]) -> set[str]:
9698
current_ids_by_page[key] = set()
9799
current_ids_by_page[key].add(doc_id)
98100

99-
# Find stale IDs: existing IDs whose (library, page) prefix matches
100-
# a current page but the full ID is not in the current set
101+
# Build set of all current IDs
102+
all_current_ids = set()
103+
for ids in current_ids_by_page.values():
104+
all_current_ids.update(ids)
105+
106+
# Build set of current prefixes (library-page combinations)
107+
current_prefixes = set()
108+
for library, page in current_ids_by_page.keys():
109+
prefix = f"{sanitize_for_id(library)}-{sanitize_for_id(page)}"
110+
current_prefixes.add(prefix)
111+
112+
# Find stale IDs
101113
stale_ids = set()
102114
for existing_id in existing_ids:
103-
# Parse the existing ID to extract library and page
115+
# Skip if this ID is still current
116+
if existing_id in all_current_ids:
117+
continue
118+
119+
# Parse the existing ID to extract prefix (library-page)
104120
# Format: {library}-{page}-{hash}
105121
# The hash is always 8 characters at the end
106122
parts = existing_id.rsplit("-", 1)
@@ -109,15 +125,24 @@ def find_stale_ids(chunks: list, existing_ids: set[str]) -> set[str]:
109125

110126
prefix = parts[0] # library-page part
111127

112-
# Check if this prefix matches any current (library, page)
113-
for (library, page), current_ids in current_ids_by_page.items():
114-
expected_prefix = f"{sanitize_for_id(library)}-{sanitize_for_id(page)}"
115-
if prefix == expected_prefix:
116-
# This existing ID is for a page we currently have
117-
# If the full ID is not in current IDs, it's stale
118-
if existing_id not in current_ids:
128+
# Check if this prefix still exists in current pages
129+
if prefix in current_prefixes:
130+
# Page still exists but content changed (updated page)
131+
stale_ids.add(existing_id)
132+
else:
133+
# Check if this is a page from a library we're currently processing
134+
# We only want to delete pages from libraries that are in our current chunks
135+
# to avoid deleting pages from libraries we didn't process
136+
current_libraries = {sanitize_for_id(lib) for lib, _ in current_ids_by_page.keys()}
137+
138+
# Extract library from prefix (everything before the first underscore after library name)
139+
# This is tricky because both library and page can have underscores
140+
# We need to check if any current library is a prefix of this ID's prefix
141+
for lib in current_libraries:
142+
if prefix.startswith(f"{lib}-"):
143+
# This is a deleted page from a library we're processing
119144
stale_ids.add(existing_id)
120-
break
145+
break
121146

122147
return stale_ids
123148

0 commit comments

Comments
 (0)