SCHEMA = MongoCollectionSchema( name=WORKFLOW_CONVERSATIONS_COLLECTION, description="워크플로우 채팅 세션 상태.", fields=( MongoFieldSpec("session_id","str",True,"채팅 세션 ID","storage.conversations"), MongoFieldSpec("owner_scope","str",False,"Spring 인증 주체의 범위 식별자",...), MongoFieldSpec("messages","list[dict]",False,"대화 메시지 목록",...),), indexes=( MongoIndexSpec("session_id",{"unique":True}), MongoIndexSpec("workflow_id"), MongoIndexSpec([("owner_scope",1),("workflow_id",1),("conversation_scope",1)]),),)
MONGO_COLLECTION_SCHEMAS =( WORKFLOW_CONVERSATIONS_SCHEMA, ANALYSIS_PLANS_SCHEMA, AGENT_TRACE_SCHEMA,...)asyncdefensure_indexes(): db = get_mongo_db() existing =set(await db.list_collection_names())for schema in MONGO_COLLECTION_SCHEMAS:if schema.name notin existing:try:await db.create_collection(schema.name)except CollectionInvalid:# 여러 worker가 동시에 부팅하는 경합 — 먼저 만든 쪽이 있으면 정상 수렴 logger.debug("다른 worker가 먼저 생성함: %s", schema.name)for index in schema.indexes:await db[schema.name].create_index(index.keys,**index.options)