From 4810602703e749af4fa74e2fb8ea395beabae06b Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 11 Nov 2026 01:34:14 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20Keep=20annotation=20sav?= =?UTF-8?q?es=20retryable=20after=20dependency=20read=20failures?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/operations/submission-recovery-guards.md | 2 + .../api/annotation_validation_dependency.go | 44 ++++++++ .../annotation_validation_dependency_test.go | 100 ++++++++++++++++++ internal/api/dictionary_selections.go | 8 +- internal/api/server.go | 10 +- internal/api/slice_dictionary_names.go | 6 +- internal/api/slice_disposition.go | 2 +- internal/api/slice_phase_browser_test.go | 10 +- tests/browser/initial-slice-confirmation.mjs | 7 +- 9 files changed, 175 insertions(+), 14 deletions(-) create mode 100644 internal/api/annotation_validation_dependency.go create mode 100644 internal/api/annotation_validation_dependency_test.go diff --git a/docs/operations/submission-recovery-guards.md b/docs/operations/submission-recovery-guards.md index 3e005abf..6bdc9f48 100644 --- a/docs/operations/submission-recovery-guards.md +++ b/docs/operations/submission-recovery-guards.md @@ -57,4 +57,6 @@ 自动保存对 408 和 429 不记录“同一内容校验失败”标记,恢复后可由下一次明确操作或保存触发点重试同一内容;不新增自动请求循环。真实校验拒绝和权限错误仍执行原阻断。页面模拟限流和请求超时,验证首次失败不进入生成,未编辑内容也能在恢复后保存并继续生成。 +切片名称、动作分类和历史切片用途的前置数据库读取失败,不作为客户内容校验错误返回:暂不可用返回 503,读取超时返回 504,并保留 `annotation_dependency_unavailable` 错误码。否则一次短暂读取故障返回 400 后,会使客户端将同一份编辑记为不可重试,持续阻止生成。恢复后的明确重试仍重新校验归属、权限、字典及版本;失败不写入工作区或草稿,也不受理生成。此修复不证明某条历史生产反馈的完整因果链;缺少原浏览器保存响应时仍需保留待核验状态。 + 保存容器校验同样用于自动保存、确认描述、切片重设、送检、舍弃、结构应用及恢复入口,防止旧客户端先写入一次空白内容绕过生成保护。校验在内容写入前返回同一错误码,保持原工作区、草稿、任务版本、归属和作业不变;已受理作业的状态读取与租约机制保持原义。 diff --git a/internal/api/annotation_validation_dependency.go b/internal/api/annotation_validation_dependency.go new file mode 100644 index 00000000..d7c70837 --- /dev/null +++ b/internal/api/annotation_validation_dependency.go @@ -0,0 +1,44 @@ +package api + +import ( + "context" + "errors" + "net/http" + + "go.mongodb.org/mongo-driver/mongo" +) + +// Keep failed prerequisite reads separate from invalid customer contents. +// Autosave deliberately suppresses repeated 4xx writes of an unchanged draft. +type annotationDictionaryReadError struct{ error } + +func (e annotationDictionaryReadError) Unwrap() error { return e.error } + +func annotationReadValidationIssue(field, message string, err error) validationIssue { + return validationIssue{Field: field, Code: "annotation_dependency_unavailable", Message: message, dependency: err} +} + +func dictionaryReadValidationIssue(field, message string, err error) validationIssue { + var dependency annotationDictionaryReadError + if errors.As(err, &dependency) { + return annotationReadValidationIssue(field, message, err) + } + return validationIssue{Field: field, Message: message} +} + +func renderValidationDependencyError(w http.ResponseWriter, issues []validationIssue) bool { + for _, issue := range issues { + if issue.dependency == nil { + continue + } + status := http.StatusServiceUnavailable + if errors.Is(issue.dependency, context.DeadlineExceeded) || mongo.IsTimeout(issue.dependency) { + status = http.StatusGatewayTimeout + } + // Never serialize the database error or mistake an unavailable read for + // success. The existing explicit retry will revalidate permissions/content. + renderJSON(w, status, map[string]any{"error": issue.Message, "code": "annotation_dependency_unavailable", "issues": issues}) + return true + } + return false +} diff --git a/internal/api/annotation_validation_dependency_test.go b/internal/api/annotation_validation_dependency_test.go new file mode 100644 index 00000000..c80c7ff6 --- /dev/null +++ b/internal/api/annotation_validation_dependency_test.go @@ -0,0 +1,100 @@ +package api + +import ( + "context" + "errors" + "net/http" + "reflect" + "strings" + "testing" + + "droboticflow/internal/domain" + "droboticflow/internal/store" +) + +type annotationDictionaryReadFailure struct { + store.Store + err error +} + +func (s annotationDictionaryReadFailure) GetProjectAnnotationLabels(context.Context, string) (*domain.ProjectAnnotationLabelsRecord, error) { + return nil, s.err +} + +func TestAnnotationDraftDictionaryReadFailureIsRetryable(t *testing.T) { + for _, failure := range []struct { + name string + err error + status int + }{{"unavailable", errors.New("synthetic database unavailable"), 503}, {"timeout", context.DeadlineExceeded, 504}} { + t.Run(failure.name, func(t *testing.T) { + f := newCombinedDraftFixture(t) + seedSliceDictionary(t, f, "测试切片") + // Historical first-pass state: saved cuts/actions but no sliceWorkflow. + f.item.Subject.Payload["annotationWorkspace"] = map[string]any{ + "sliceSegments": []domain.SliceSegment{{ID: "s", Start: 0, End: 10}}, + "segments": []domain.AnnotationSegment{{ID: "retained", Start: 0, End: 10, Status: "pending"}}, + "userId": f.annotator.ID, "savedAt": nowISO(), + } + if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil { + t.Fatal(err) + } + before, _ := f.store.GetHumanItem(f.ctx, f.item.ID) + body := combinedAnnotationDraftInput{Version: before.Version, + SliceSegments: []domain.SliceSegment{{ID: "s", Name: "测试切片", Start: 0, End: 10}}, + Segments: []domain.AnnotationSegment{{ID: "retained", Start: 0, End: 10, Status: "pending"}}, + } + endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/drafts" + f.handler = NewHandler(annotationDictionaryReadFailure{f.store, failure.err}, nil) + r := f.request(http.MethodPut, endpoint, f.annotator, body) + if r.Code != failure.status { + t.Fatalf("dependency read failure was cached as content rejection: %d %s", r.Code, r.Body.String()) + } + if strings.Contains(r.Body.String(), failure.err.Error()) { + t.Fatal("response exposed the prerequisite database error") + } + after, _ := f.store.GetHumanItem(f.ctx, f.item.ID) + if !reflect.DeepEqual(before, after) { + t.Fatal("failed save changed existing annotation state") + } + f.handler = NewHandler(f.store, nil) + r = f.request(http.MethodPut, endpoint, f.annotator, body) + if r.Code != 200 { + t.Fatalf("same contents cannot be retried after recovery: %d %s", r.Code, r.Body.String()) + } + after, _ = f.store.GetHumanItem(f.ctx, f.item.ID) + draft := mapValue(after.Subject.Payload["annotationDraft"]) + if got := decodeMaterializationValue[domain.AnnotationSegment](draft["segments"]); !reflect.DeepEqual(got, body.Segments) { + t.Fatal("retry lost retained actions") + } + if !reflect.DeepEqual(after.Subject.Payload["annotationWorkspace"], before.Subject.Payload["annotationWorkspace"]) { + t.Fatal("autosave overwrote confirmed work") + } + }) + } +} + +func TestAnnotationDraftDependencyFixKeepsContentAndPermissionRejections(t *testing.T) { + f := newCombinedDraftFixture(t) + seedSliceDictionary(t, f, "测试切片") + body := combinedAnnotationDraftInput{Version: f.item.Version, + SliceSegments: []domain.SliceSegment{{ID: "s", Name: "不属于项目字典的名称", Start: 0, End: 10}}, + Segments: []domain.AnnotationSegment{{ID: "retained", Start: 0, End: 10, Status: "pending"}}, + } + endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/drafts" + before, _ := f.store.GetHumanItem(f.ctx, f.item.ID) + for _, actor := range []domain.UserRecord{f.annotator, f.reviewer} { + want := http.StatusBadRequest + if actor.ID == f.reviewer.ID { + want = http.StatusForbidden + } + r := f.request(http.MethodPut, endpoint, actor, body) + if r.Code != want { + t.Fatalf("content/permission rejection changed: %d %s", r.Code, r.Body.String()) + } + after, _ := f.store.GetHumanItem(f.ctx, f.item.ID) + if !reflect.DeepEqual(before, after) { + t.Fatal("rejected save changed existing task") + } + } +} diff --git a/internal/api/dictionary_selections.go b/internal/api/dictionary_selections.go index bd05485d..bc7dc53a 100644 --- a/internal/api/dictionary_selections.go +++ b/internal/api/dictionary_selections.go @@ -14,7 +14,7 @@ import ( func (s *server) projectDictionarySelections(ctx context.Context, item *domain.HumanItemRecord, kind string) (map[string]domain.AnnotationLabelRecord, error) { selection, err := s.store.GetProjectAnnotationLabels(ctx, item.ProjectID) if err != nil { - return nil, err + return nil, annotationDictionaryReadError{err} } result := map[string]domain.AnnotationLabelRecord{} if selection == nil { @@ -30,7 +30,7 @@ func (s *server) projectDictionarySelections(ctx context.Context, item *domain.H for _, id := range ids { label, err := s.store.GetAnnotationLabel(ctx, id) if err != nil { - return nil, err + return nil, annotationDictionaryReadError{err} } if label != nil && label.OrganizationID == item.OrganizationID && dictionaryKind(label.Kind) == kind && label.Status == domain.AnnotationLabelActive { result[id] = *label @@ -48,7 +48,7 @@ func (s *server) canonicalActionDictionaries(ctx context.Context, item *domain.H if len(previous) == 0 && item.LegacyTaskID != "" { revisions, err := s.store.ListAnnotationRevisions(ctx, item.OrganizationID, item.LegacyTaskID) if err != nil { - return []validationIssue{{Field: "segments", Message: "无法读取已保存的动作分类"}} + return []validationIssue{annotationReadValidationIssue("segments", "无法读取已保存的动作分类", err)} } for _, rev := range revisions { if rev.Revision == item.CurrentRevision { @@ -93,7 +93,7 @@ func (s *server) canonicalActionDictionaries(ctx context.Context, item *domain.H var err error allowed, err = s.projectDictionarySelections(ctx, item, "action") if err != nil { - return []validationIssue{{Field: "segments", Message: "无法读取动作字典,请重试"}} + return []validationIssue{dictionaryReadValidationIssue("segments", "无法读取动作字典,请重试", err)} } } label, ok := allowed[seg.Dictionary.ID] diff --git a/internal/api/server.go b/internal/api/server.go index 76d9f674..86cfe547 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -42,9 +42,10 @@ import ( ) type validationIssue struct { - Field string `json:"field"` - Code string `json:"code,omitempty"` - Message string `json:"message"` + Field string `json:"field"` + Code string `json:"code,omitempty"` + Message string `json:"message"` + dependency error } type server struct { @@ -9440,6 +9441,9 @@ func pagedResponse[T any](request *http.Request, items []T, defaultLimit int, ma } func renderValidationError(writer http.ResponseWriter, issues ...validationIssue) { + if renderValidationDependencyError(writer, issues) { + return + } renderJSON(writer, http.StatusBadRequest, map[string]any{"error": "Validation failed", "issues": issues}) } diff --git a/internal/api/slice_dictionary_names.go b/internal/api/slice_dictionary_names.go index de02c6b2..518533c4 100644 --- a/internal/api/slice_dictionary_names.go +++ b/internal/api/slice_dictionary_names.go @@ -15,7 +15,7 @@ func (s *server) validateProjectSliceNames(ctx context.Context, item *domain.Hum } task, err := s.store.GetAnnotationTask(ctx, item.LegacyTaskID) if err != nil { - return []validationIssue{{Field: "sliceSegments", Message: "无法读取切片字典规则"}} + return []validationIssue{annotationReadValidationIssue("sliceSegments", "无法读取切片字典规则", err)} } if task == nil || task.TemplateVersion < 6 { // Older templates use taskLabelId/taskLabel; they cannot introduce the @@ -29,7 +29,7 @@ func (s *server) validateProjectSliceNames(ctx context.Context, item *domain.Hum if len(previous) == 0 { revisions, err := s.store.ListSliceRevisions(ctx, item.OrganizationID, item.LegacyTaskID) if err != nil { - return []validationIssue{{Field: "sliceSegments", Message: "无法读取历史切片名称"}} + return []validationIssue{annotationReadValidationIssue("sliceSegments", "无法读取历史切片名称", err)} } for _, rev := range revisions { if rev.Revision == item.CurrentRevision || len(previous) == 0 { @@ -83,7 +83,7 @@ func (s *server) validateProjectSliceNames(ctx context.Context, item *domain.Hum if allowed == nil { allowed, err = s.projectDictionarySelections(ctx, item, "slice") if err != nil { - return []validationIssue{{Field: "sliceSegments", Message: "无法读取项目切片字典,请重试"}} + return []validationIssue{dictionaryReadValidationIssue("sliceSegments", "无法读取项目切片字典,请重试", err)} } } var chosen *domain.DictionarySelection diff --git a/internal/api/slice_disposition.go b/internal/api/slice_disposition.go index 71b86249..94a9750a 100644 --- a/internal/api/slice_disposition.go +++ b/internal/api/slice_disposition.go @@ -47,7 +47,7 @@ func (s *server) canonicalSliceDispositionState(ctx context.Context, item *domai if len(previous) == 0 && item.LegacyTaskID != "" { revisions, err := s.store.ListSliceRevisions(ctx, item.OrganizationID, item.LegacyTaskID) if err != nil { - return nil, []validationIssue{{Field: "sliceSegments", Message: "无法读取已保存的切片用途"}} + return nil, []validationIssue{annotationReadValidationIssue("sliceSegments", "无法读取已保存的切片用途", err)} } for _, r := range revisions { if r.Revision == item.CurrentRevision || len(previous) == 0 { diff --git a/internal/api/slice_phase_browser_test.go b/internal/api/slice_phase_browser_test.go index 648182de..164a4e30 100644 --- a/internal/api/slice_phase_browser_test.go +++ b/internal/api/slice_phase_browser_test.go @@ -10,6 +10,7 @@ import ( "os/exec" "path/filepath" "strings" + "sync/atomic" "testing" "time" @@ -175,6 +176,9 @@ func TestSlicePhaseBrowserReplica(t *testing.T) { t.Fatalf("synthetic source validation did not complete: %s", validationStatus) } handler := NewHandlerWithConfig(f.store, nil, config.AppConfig{WorkspaceMountPath: root}) + unavailableDictionaryHandler := NewHandler(annotationDictionaryReadFailure{f.store, context.DeadlineExceeded}, nil) + var failDictionarySave atomic.Bool + failDictionarySave.Store(os.Getenv("INITIAL_SLICE_CONFIRM_SCENARIO") == "save-dictionary") server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/api/auth/me" { renderJSON(w, 200, map[string]any{"user": sanitizeUser(&f.annotator), "organization": domain.OrganizationRecord{ID: domain.DefaultOrganizationID, Status: domain.OrganizationStatusActive}}) @@ -200,6 +204,10 @@ func TestSlicePhaseBrowserReplica(t *testing.T) { r.AddCookie(&http.Cookie{Name: csrfCookieName, Value: "boundary-local-csrf"}) r.Header.Set(csrfHeaderName, "boundary-local-csrf") r = r.WithContext(context.WithValue(r.Context(), authContextKey{}, &authenticatedPrincipal{User: f.annotator, Organization: domain.OrganizationRecord{ID: domain.DefaultOrganizationID, Status: domain.OrganizationStatusActive}})) + if r.Method == http.MethodPut && strings.HasSuffix(r.URL.Path, "/drafts") && failDictionarySave.Swap(false) { + unavailableDictionaryHandler.ServeHTTP(w, r) + return + } handler.ServeHTTP(w, r) })) defer server.Close() @@ -224,7 +232,7 @@ func TestInitialSliceConfirmationBrowserReplica(t *testing.T) { t.Skip("local browser opt-in") } t.Setenv("REWORK_BOUNDARY_BROWSER_TEST", "1") - for _, scenario := range []string{"pending", "edited", "legacy", "failed", "save-retry", "save-throttle", "save-timeout", "malformed"} { + for _, scenario := range []string{"pending", "edited", "legacy", "failed", "save-retry", "save-throttle", "save-timeout", "save-dictionary", "malformed"} { t.Run(scenario, func(t *testing.T) { t.Setenv("INITIAL_SLICE_CONFIRM_SCENARIO", scenario) TestSlicePhaseBrowserReplica(t) diff --git a/tests/browser/initial-slice-confirmation.mjs b/tests/browser/initial-slice-confirmation.mjs index 3b15a167..5274a328 100644 --- a/tests/browser/initial-slice-confirmation.mjs +++ b/tests/browser/initial-slice-confirmation.mjs @@ -34,8 +34,11 @@ try { if (r.url().endsWith("/drafts") || r.url().endsWith("/slice-confirmations")) report.requests.push({ path: new URL(r.url()).pathname, status: r.status() }); }); - const saveFailureStatus = status === "save-throttle" ? 429 : status === "save-timeout" ? 408 : 503; - let failNextSave = status.startsWith("save-"); + const saveFailureStatus = + status === "save-dictionary" ? 504 : status === "save-throttle" ? 429 : status === "save-timeout" ? 408 : 503; + // The dictionary scenario fails a real prerequisite store read in the Go + // handler; the other scenarios inject transport/status failures here. + let failNextSave = status.startsWith("save-") && status !== "save-dictionary"; await page.route("**/*", (r) => { const u = new URL(r.request().url()); if (u.hostname !== "127.0.0.1") return r.abort();