diff --git a/runtime/datamate-python/app/module/annotation/service/editor.py b/runtime/datamate-python/app/module/annotation/service/editor.py index 878c389..a5f9789 100644 --- a/runtime/datamate-python/app/module/annotation/service/editor.py +++ b/runtime/datamate-python/app/module/annotation/service/editor.py @@ -1018,6 +1018,14 @@ class AnnotationEditorService: request.segment_index, annotation_payload, ) + segment_entries = self._extract_segment_annotations(final_payload) + if str(request.segment_index) not in segment_entries: + logger.warning( + "分段标注合并异常:未找到当前段落 key,project_id=%s file_id=%s segment_index=%s", + project_id, + file_id, + request.segment_index, + ) else: # 非分段模式:直接使用传入的 annotation annotation_payload["task"] = ls_task_id @@ -1057,6 +1065,18 @@ class AnnotationEditorService: else: raise HTTPException(status_code=400, detail="未发现标注内容,请确认无标注/不适用后再保存") + if request.segment_index is not None: + segment_entries = self._extract_segment_annotations(final_payload) + logger.info( + "分段标注保存:project_id=%s file_id=%s segment_index=%s segments=%s total=%s status=%s", + project_id, + file_id, + request.segment_index, + len(segment_entries), + segment_total, + final_status, + ) + if existing: if request.expected_updated_at and existing.updated_at: if existing.updated_at != request.expected_updated_at.replace(tzinfo=None): @@ -1126,7 +1146,15 @@ class AnnotationEditorService: if not base.get(SEGMENTED_KEY): base[SEGMENTED_KEY] = True segments = base.get(SEGMENTS_KEY) - if not isinstance(segments, dict): + if isinstance(segments, dict): + # 拷贝一份,避免原地修改导致 SQLAlchemy 变更检测失效 + segments = dict(segments) + base[SEGMENTS_KEY] = segments + elif isinstance(segments, list): + # 兼容旧的 list 结构,归一化为 dict 结构 + segments = self._extract_segment_annotations(base) + base[SEGMENTS_KEY] = segments + else: segments = {} base[SEGMENTS_KEY] = segments