dp-zp-agent/tools/knowledge/graph_projection.py

32 lines
1.7 KiB
Python

"""Re-project the current checkpoint atomically; no LLM calls or wiki changes."""
from tools.rag.common import DATA_DIR, DB_FILE
from tools.sync.index_state import publication_lock, read_version, sqlite_version
from tools.rag.rebuild_index import acquire_reindex_lock
from tools.knowledge import store
from tools.graphrag.graph_db import get_neo4j_settings, create_neo4j_driver
def refresh_projection(data_dir=DATA_DIR, db_file=DB_FILE):
with acquire_reindex_lock(data_dir/'knowledge/.build.lock'), publication_lock(data_dir,exclusive=True):
index=store.load_index(data_dir,read_version(data_dir))
if index is None or not index.get('_checkpoint_sha256'):
raise RuntimeError('A published, validated content checkpoint is required')
if sqlite_version(db_file)!=index['index_version']:
raise RuntimeError('SQLite and content checkpoint belong to different snapshots')
settings=get_neo4j_settings()
with create_neo4j_driver(settings) as driver, driver.session(database=settings.database) as session:
store.create_constraints(session)
with session.begin_transaction() as tx:
store.write_graph_transaction(tx,index,checkpoint_sha256=index['_checkpoint_sha256'])
tx.commit()
state=store.graph_state(session)
if state.get('version')!=index['version'] or state.get('projection_schema')!=store.CONTENT_GRAPH_SCHEMA:
raise RuntimeError('Graph projection verification failed')
return {'version':index['version'],'projection_schema':store.CONTENT_GRAPH_SCHEMA,
'units':len(index['units']),'llm_requests':0}
if __name__=='__main__':
import json
print(json.dumps(refresh_projection()))