Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/xagent/core/tools/core/RAG_tools/chunk/chunk_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ def chunk_document(
"""
Chunk parsed paragraphs and write to chunks table.

Concurrency note:
Chunk replacement is serialised per
``(collection, doc_id, parse_hash, user scope)`` via
``generation_ingestion_lock`` (in ``replace_chunks`` and the ingestion
pipeline through embedding writes).

Args:
collection: Collection name for data isolation
doc_id: Document ID whose parsed result to chunk
Expand Down Expand Up @@ -506,7 +512,12 @@ def _write_chunks_to_db(
user_id: Optional[int] = None,
is_admin: bool = False,
) -> bool:
"""Write chunk records to database using abstraction layer."""
"""Write chunk records to database using abstraction layer.

Replaces any existing rows for the same document parse (collection, doc_id,
parse_hash) and tenancy scope before inserting, so a new ``config_hash`` does
not leave stale chunks from a previous configuration (issue #199).
"""
try:
rows = []
for chunk in chunks:
Expand All @@ -532,13 +543,21 @@ def _write_chunks_to_db(
}
rows.append(row)

vector_store = get_vector_index_store()
vector_store.replace_chunks(
Comment thread
sqhyz55 marked this conversation as resolved.
rows,
replace_scope={
"collection": collection,
"doc_id": doc_id,
"parse_hash": parse_hash,
},
user_id=user_id,
is_admin=is_admin,
)

if not rows:
return False

# Use abstraction layer for upsert
vector_store = get_vector_index_store()
vector_store.upsert_chunks(rows)

logger.info(
"Chunk records written to database: doc_id=%s, parse_hash=%s, config_hash=%s",
doc_id,
Expand Down
Loading
Loading