🩹 Preserve action records when consolidating invalid-only slices

This commit is contained in:
d-robotics 2026-09-24 15:16:52 +08:00
parent 1a7eb3bfb3
commit ce133c0fa8
10 changed files with 358 additions and 6 deletions

View File

@ -0,0 +1,13 @@
# 无效动作与外层切片的无损整理
无效动作属于外层业务切片内部,设置无效不新增外层边界。旧版取消切分仍按既有规则清空合并区间判断;它不是无损整理入口。
送检前识别以下候选:连续三个保留切片名称完全相同、首尾精确相接,中间切片由明确无效的动作完整覆盖。用户逐项选择是否合并,最后再次确认提交。取消选择可保留原划分,不对所有同名切片做自动合并。舍弃、名称不同、缺口、部分无效都不匹配。
服务器重新验证选择,仅在已有领取权、版本、字典、覆盖与动作合法性校验全部通过后,改变送检版本的外层划分。动作数组不因合并改变:ID、描述、无效判断、起止边界、硬件时间戳均保留。重叠候选合成同一连续区间。操作历史记录原切片快照。
后台生成新的媒体和 JSON;不能把旧短片直接冒充合并后视频。JSON 中相对于切片起点的时间随新的切片起点正常重基,源视频时间、硬件时间戳、动作含义保持一致。旧版本文件和记录通过任务级历史保留标记保护,后续送检和重切也不会由普通版本清理删除。
部署无数据库迁移,不自动修改已送检、已审核任务,也不代替客户送检。现有在审任务需要正常退回至标注后使用此入口;无需重新填写动作。更新现有在审结果并保持原审核状态,需要独立的、明确授权的数据修订流程,不属于本次部署。
验证覆盖:候选识别、非法选择拒绝且不占用任务、重叠候选、真实持久队列与媒体生成、导出 JSON 动作与时间戳保留,以及原版本所有文件逐字节未变。

View File

@ -47,7 +47,7 @@ func TestContinuousBlankActionsReachReviewAndPublishedJSON(t *testing.T) {
}
}
func checkBlankActionsDelivery(t *testing.T, reuse bool, segments []domain.AnnotationSegment, want []blankActionExpectation) {
func checkBlankActionsDelivery(t *testing.T, reuse bool, segments []domain.AnnotationSegment, want []blankActionExpectation, consolidate ...bool) {
t.Helper()
t.Setenv("ANNOTATION_PROCESSOR_MODE", "external")
f, root := annotationProcessorFixture(t)
@ -103,6 +103,11 @@ func checkBlankActionsDelivery(t *testing.T, reuse bool, segments []domain.Annot
}
}
slices := []domain.SliceSegment{{ID: "whole", Name: "测试切片", Start: 0, End: 4}}
var selected []string
if len(consolidate) > 0 && consolidate[0] {
slices = []domain.SliceSegment{{ID: "left", Name: "测试切片", Start: 0, End: 1}, {ID: "invalid", Name: "测试切片", Start: 1, End: 2}, {ID: "right", Name: "测试切片", Start: 2, End: 4}}
selected = []string{"invalid"}
}
version := f.item.Version
if reuse {
response := f.request(http.MethodPost, endpoint+"/slice-confirmations", f.annotator, map[string]any{"version": version, "sliceSegments": slices})
@ -111,14 +116,40 @@ func checkBlankActionsDelivery(t *testing.T, reuse bool, segments []domain.Annot
}
version = waitFor(false)
}
priorFiles := map[string]string{}
if reuse && len(selected) > 0 {
priorRoot := filepath.Join(root, "system", "runs", f.task.RunID, "human-materializations", f.item.ID, f.task.ID, "revision-000001")
err := filepath.Walk(priorRoot, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
data, e := os.ReadFile(path)
if e != nil {
return e
}
priorFiles[path] = string(data)
}
return nil
})
if err != nil || len(priorFiles) == 0 {
t.Fatalf("prior evidence unavailable: %v", err)
}
}
response := f.request(http.MethodPost, endpoint+"/combined-submissions", f.annotator, map[string]any{
"version": version, "requireSliceCompletion": true,
"sliceSegments": slices, "segments": segments,
"sliceSegments": slices, "segments": segments, "consolidateInvalidSliceIds": selected,
})
if response.Code != http.StatusAccepted {
t.Fatalf("blank submission: %d %s", response.Code, response.Body.String())
}
waitFor(true)
for path, original := range priorFiles {
data, e := os.ReadFile(path)
if e != nil || string(data) != original {
t.Fatalf("old revision overwritten: %s %v", path, e)
}
}
response = f.request(http.MethodGet, endpoint, f.reviewer, nil)
if response.Code != http.StatusOK {
t.Fatalf("quality read: %d %s", response.Code, response.Body.String())
@ -149,7 +180,7 @@ func checkBlankActionsDelivery(t *testing.T, reuse bool, segments []domain.Annot
if reuse {
expectedGeneration = 2
}
if generation != expectedGeneration || detail.HumanItem.Subject.Payload.Materialization.MetadataOnly != reuse {
if generation != expectedGeneration || detail.HumanItem.Subject.Payload.Materialization.MetadataOnly != (reuse && len(selected) == 0) {
t.Fatalf("expected generation %d reuse %v: %s", expectedGeneration, reuse, response.Body.String())
}
path := filepath.Join(root, "system", "runs", f.task.RunID, "human-materializations", f.item.ID, f.task.ID, fmt.Sprintf("revision-%06d", generation), "slices", "slice-000001", "annotation.json")
@ -180,3 +211,9 @@ func checkBlankActionsDelivery(t *testing.T, reuse bool, segments []domain.Annot
}
}
}
func TestInvalidSliceConsolidationRebuildsMediaAndPreservesPublishedActions(t *testing.T) {
actions := []domain.AnnotationSegment{{ID: "left-action", Start: 0, End: 1, Status: "described", Description: "拿起"}, {ID: "invalid-action", Start: 1, End: 2, Status: "invalid", IsInvalid: true, Description: "无效片段"}, {ID: "right-action", Start: 2, End: 4, Status: "described", Description: "放下"}}
want := []blankActionExpectation{{"left-action", "described", "拿起", false, 0, 1, 1000000, 2000000}, {"invalid-action", "invalid", "无效片段", true, 1, 2, 2000000, 3000000}, {"right-action", "described", "放下", false, 2, 4, 3000000, 5000000}}
checkBlankActionsDelivery(t, true, actions, want, true)
}

View File

@ -23,6 +23,11 @@ func (s *server) retainCurrentAnnotationData(ctx context.Context, task domain.An
if item.LegacyTaskID != task.ID || item.OrganizationID != task.OrganizationID || item.RunID != task.RunID {
return fmt.Errorf("annotation retention scope mismatch")
}
// A lossless consolidation pins its original evidence for future reslicing
// and submissions, not just the first post-consolidation cleanup.
if item.Subject.Payload["preserveAnnotationRevisionHistory"] == true {
return nil
}
// Disposition history is review evidence. Keep its private prior generations;
// this does not add selectable delivery versions or alter project deletion.
if history, ok := item.Subject.Payload["sliceDispositionHistory"]; ok && history != nil {

View File

@ -0,0 +1,82 @@
package api
import (
"fmt"
"sort"
"strings"
"droboticflow/internal/domain"
)
// Consolidation is an explicit submission choice, never a migration or a
// cancel-cut. Action records are not passed by pointer or rewritten here.
func consolidateInvalidActionSlices(input []domain.SliceSegment, actions []domain.AnnotationSegment, selected []string) ([]domain.SliceSegment, []domain.SliceOperation, error) {
if len(selected) == 0 {
return input, nil, nil
}
ordered := append([]domain.SliceSegment(nil), input...)
sort.Slice(ordered, func(i, j int) bool { return ordered[i].Start < ordered[j].Start })
eligible := map[string]int{}
for i := 1; i < len(ordered)-1; i++ {
l, c, r := ordered[i-1], ordered[i], ordered[i+1]
if l.IsDiscarded() || c.IsDiscarded() || r.IsDiscarded() || strings.TrimSpace(c.Name) == "" || l.Name != c.Name || r.Name != c.Name || l.End != c.Start || c.End != r.Start {
continue
}
cursor, has, valid := c.Start, false, true
inner := append([]domain.AnnotationSegment(nil), actions...)
sort.Slice(inner, func(i, j int) bool { return inner[i].Start < inner[j].Start })
for _, a := range inner {
if a.End <= c.Start || a.Start >= c.End {
continue
}
if a.Start != cursor || a.End > c.End || a.End <= a.Start || !a.IsInvalid {
valid = false
break
}
cursor, has = a.End, true
}
if valid && has && cursor == c.End {
eligible[c.ID] = i
}
}
edges := make([]bool, len(ordered))
seen := map[string]bool{}
for _, id := range selected {
i, ok := eligible[id]
if !ok || seen[id] {
return nil, nil, fmt.Errorf("所选无效区间的相邻切片已变化,请重新核对后提交")
}
seen[id] = true
edges[i-1], edges[i] = true, true
}
var result []domain.SliceSegment
var operations []domain.SliceOperation
for i := 0; i < len(ordered); {
j := i
for j < len(ordered)-1 && edges[j] {
j++
}
merged := ordered[i]
if j > i {
// Keep original IDs in audit provenance; reuse the leftmost stable ID.
merged.SourceIDs = nil
provenance := map[string]bool{}
ids := []string{}
for k := i; k <= j; k++ {
ids = append(ids, ordered[k].ID)
for _, id := range append([]string{ordered[k].ID}, ordered[k].SourceIDs...) {
if !provenance[id] {
merged.SourceIDs = append(merged.SourceIDs, id)
provenance[id] = true
}
}
}
merged.End = ordered[j].End
merged.EndTimestampUS = ordered[j].EndTimestampUS
operations = append(operations, domain.SliceOperation{Type: "consolidate_invalid_actions", SegmentIDs: ids, Before: append([]domain.SliceSegment(nil), ordered[i:j+1]...), After: []domain.SliceSegment{merged}})
}
result = append(result, merged)
i = j + 1
}
return result, operations, nil
}

View File

@ -0,0 +1,82 @@
package api
import (
"encoding/json"
"reflect"
"testing"
"droboticflow/internal/domain"
)
func TestInvalidSliceConsolidationPreservesActionsAndAudit(t *testing.T) {
slices := []domain.SliceSegment{{ID: "a", Name: "收银", Start: 0, End: 2, StartTimestampUS: 100}, {ID: "b", Name: "收银", Start: 2, End: 3}, {ID: "c", Name: "收银", Start: 3, End: 5, EndTimestampUS: 900}, {ID: "d", Name: "其他", Start: 5, End: 9}}
actions := []domain.AnnotationSegment{{ID: "x", Start: 0, End: 2, Description: "拿起商品", Phrases: []string{"细节"}}, {ID: "y", Start: 2, End: 3, IsInvalid: true, Description: "无效片段", Status: "invalid", StartTimestampUS: 200, EndTimestampUS: 300}, {ID: "z", Start: 3, End: 5, Description: "放下商品"}}
before, _ := json.Marshal(actions)
original, _ := json.Marshal(slices)
got, ops, err := consolidateInvalidActionSlices(slices, actions, []string{"b"})
if err != nil || len(got) != 2 || got[0].Start != 0 || got[0].End != 5 || got[0].Name != "收银" || got[0].StartTimestampUS != 100 || got[0].EndTimestampUS != 900 {
t.Fatalf("%+v %v", got, err)
}
after, _ := json.Marshal(actions)
if string(before) != string(after) {
t.Fatal("action JSON changed")
}
unchanged, _ := json.Marshal(slices)
if string(original) != string(unchanged) {
t.Fatal("input slices mutated")
}
if !reflect.DeepEqual(ops[0].Before, slices[:3]) || ops[0].Type != "consolidate_invalid_actions" {
t.Fatal("audit lost")
}
if !reflect.DeepEqual(got[1], slices[3]) {
t.Fatal("unrelated slice changed")
}
kept, ops, err := consolidateInvalidActionSlices(slices, actions, nil)
if err != nil || len(ops) != 0 || !reflect.DeepEqual(kept, slices) {
t.Fatal("implicit consolidation")
}
for name, change := range map[string]func([]domain.SliceSegment, []domain.AnnotationSegment){
"discard": func(s []domain.SliceSegment, a []domain.AnnotationSegment) { s[1].DeliveryDisposition = "discard" },
"different-name": func(s []domain.SliceSegment, a []domain.AnnotationSegment) { s[2].Name = "盘点" },
"gap": func(s []domain.SliceSegment, a []domain.AnnotationSegment) { s[2].Start = 3.1 },
"valid-action": func(s []domain.SliceSegment, a []domain.AnnotationSegment) { a[1].IsInvalid = false },
"partial-invalid": func(s []domain.SliceSegment, a []domain.AnnotationSegment) { a[1].End = 2.5 },
} {
t.Run(name, func(t *testing.T) {
s := append([]domain.SliceSegment(nil), slices...)
a := append([]domain.AnnotationSegment(nil), actions...)
change(s, a)
if _, _, err := consolidateInvalidActionSlices(s, a, []string{"b"}); err == nil {
t.Fatal("unsafe consolidation accepted")
}
})
}
if _, _, err = consolidateInvalidActionSlices(slices, actions, []string{"b", "b"}); err == nil {
t.Fatal("duplicate accepted")
}
}
func TestInvalidSliceConsolidationOverlappingCandidates(t *testing.T) {
var ss []domain.SliceSegment
var aa []domain.AnnotationSegment
for i, id := range []string{"a", "b", "c", "d", "e"} {
ss = append(ss, domain.SliceSegment{ID: id, Name: "盘点", Start: float64(i), End: float64(i + 1)})
aa = append(aa, domain.AnnotationSegment{ID: id, Start: float64(i), End: float64(i + 1), IsInvalid: i == 1 || i == 3})
}
got, ops, err := consolidateInvalidActionSlices(ss, aa, []string{"b", "d"})
if err != nil || len(got) != 1 || got[0].End != 5 || len(ops) != 1 || len(ops[0].Before) != 5 {
t.Fatalf("%+v %v", got, err)
}
}
func TestInvalidSliceConsolidationRejectsStaleSelectionBeforeReservation(t *testing.T) {
f := continuousFixture(t)
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/combined-submissions", f.annotator, map[string]any{"version": f.item.Version, "sliceSegments": []domain.SliceSegment{{ID: "s", Name: "测试切片", Start: 0, End: 10}}, "segments": []domain.AnnotationSegment{{ID: "a", Start: 0, End: 10, Description: "动作"}}, "consolidateInvalidSliceIds": []string{"missing"}})
if r.Code != 400 {
t.Fatalf("%d %s", r.Code, r.Body.String())
}
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
if item.Version != f.item.Version || item.PendingCombinedSubmissionID != "" {
t.Fatal("invalid request mutated task")
}
}

View File

@ -1055,6 +1055,7 @@ type sliceRevisionInput struct {
// Slice correction and semantic labels are persisted together only after both
// validate, so a quality reviewer never receives one without the other.
type combinedAnnotationSubmissionInput struct {
ConsolidateInvalidSliceIDs []string `json:"consolidateInvalidSliceIds,omitempty"`
// Set only by the internal cut job; never accepted from JSON.
sliceOnly bool
Version int `json:"version"`
@ -2056,6 +2057,22 @@ func (server *server) handleCombinedAnnotationSubmission(writer http.ResponseWri
renderValidationError(writer, issues...)
return
}
// Original slices, names, disposition and actions have all been validated.
// Only an explicit user selection may change the outer partition. Retain
// action IDs, descriptions, invalid flags and absolute timestamp boundaries.
if len(payload.ConsolidateInvalidSliceIDs) > 0 {
merged, operations, mergeErr := consolidateInvalidActionSlices(payload.SliceSegments, payload.Segments, payload.ConsolidateInvalidSliceIDs)
if mergeErr != nil {
renderValidationError(writer, validationIssue{Field: "consolidateInvalidSliceIds", Message: mergeErr.Error()})
return
}
if issues := validateDescriptionIntervals(merged, payload.Segments, false); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
payload.SliceSegments = merged
payload.SliceOperations = append(payload.SliceOperations, operations...)
}
payloadSHA, hashErr := fanyuV5CombinedPayloadSHA(payload)
if hashErr != nil {
renderError(writer, http.StatusInternalServerError, hashErr)
@ -2201,6 +2218,9 @@ func (server *server) completeCombinedAnnotationSubmission(writer http.ResponseW
if humanItem.Subject.Payload == nil {
humanItem.Subject.Payload = map[string]any{}
}
if len(payload.ConsolidateInvalidSliceIDs) > 0 {
humanItem.Subject.Payload["preserveAnnotationRevisionHistory"] = true
}
humanItem.Subject.Payload["materialization"] = materialization.Payload
if workflow := mapValue(humanItem.Subject.Payload["sliceWorkflow"]); len(workflow) > 0 {
workflow["materialization"] = materialization.Payload
@ -2999,7 +3019,10 @@ func (server *server) handleFanyuV5CombinedQualityReview(writer http.ResponseWri
return true
}
}
if operationFromRequest(request) != nil { server.planQualityReviewDecision(writer, request, *task, *item, batch, payload); return true }
if operationFromRequest(request) != nil {
server.planQualityReviewDecision(writer, request, *task, *item, batch, payload)
return true
}
now := nowISO()
review := domain.AnnotationReviewRecord{ID: domain.NewID("annotation-review"), OrganizationID: task.OrganizationID, TaskID: task.ID, Revision: task.CurrentRevision, Stage: domain.AnnotationReviewStageQuality, Decision: payload.Decision, ReviewerUserID: principal.User.ID, Comment: strings.TrimSpace(payload.Comment), VideoQuality: strings.TrimSpace(payload.VideoQuality), AnnotationAccuracy: payload.AnnotationAccuracy, AnnotationCompleteness: payload.AnnotationCompleteness, OverallGrade: strings.TrimSpace(payload.OverallGrade), ReasonCodes: payload.ReasonCodes, ProblemSliceIDs: payload.ProblemSliceIDs, CreatedAt: now}
review.ReviewRound = item.ReviewRound
@ -3131,14 +3154,18 @@ func (server *server) handleAnnotationReviewCreate(writer http.ResponseWriter, r
renderJSON(writer, http.StatusForbidden, map[string]string{"error": string(requiredRole) + " assignment is required"})
return
}
if server.replayReviewDecision(writer, request, task.ID, payload.ReviewDecisionInput, payload) { return }
if server.replayReviewDecision(writer, request, task.ID, payload.ReviewDecisionInput, payload) {
return
}
humanItem, claimed := server.requireLegacyHumanItemClaim(writer, request, *task, "")
if !claimed {
return
}
var prepared bool
request, prepared = server.prepareReviewDecision(writer, request, *humanItem, "quality", payload.ReviewDecisionInput, payload, string(payload.Decision))
if !prepared { return }
if !prepared {
return
}
defer server.finishReviewDecisionPreparation(request)
finishReviewWrite, writeAllowed := server.beginReviewWrite(writer, request, *humanItem)
if !writeAllowed {

View File

@ -0,0 +1,44 @@
// Candidate discovery only. The server revalidates explicit selections and
// creates a new revision; viewing or marking an action invalid never merges.
export function invalidSliceConsolidationCandidates(slices, actions) {
const ordered = [...slices].sort((a, b) => a.start - b.start);
return ordered.flatMap((current, index) => {
const left = ordered[index - 1];
const right = ordered[index + 1];
if (
!left ||
!right ||
[left, current, right].some((s) => s.deliveryDisposition === "discard") ||
!current.name?.trim() ||
left.name !== current.name ||
right.name !== current.name ||
left.end !== current.start ||
current.end !== right.start
)
return [];
const inner = actions
.filter((a) => a.end > current.start && a.start < current.end)
.sort((a, b) => a.start - b.start);
let cursor = current.start;
if (
!inner.length ||
!inner.every((a) => {
if (!a.isInvalid || a.start !== cursor || a.end <= a.start || a.end > current.end) return false;
cursor = a.end;
return true;
}) ||
cursor !== current.end
)
return [];
return [
{
id: current.id,
name: current.name,
start: left.start,
end: right.end,
invalidStart: current.start,
invalidEnd: current.end,
},
];
});
}

View File

@ -1,3 +1,4 @@
import { invalidSliceConsolidationCandidates } from "../lib/invalid-slice-consolidation.js";
import { ReviewDecisionDialog } from "../components/ReviewDecisionDialog";
import { useReviewContinuation } from "../hooks/useReviewContinuation";
import { useReviewEntryIntent } from "../hooks/useReviewEntryIntent";
@ -3157,12 +3158,32 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
setError("请保存所有动作描述或明确设为无效");
return;
}
const candidates = invalidSliceConsolidationCandidates(sliceSegments, segments);
const consolidateInvalidSliceIds: string[] = [];
for (const candidate of candidates) {
if (
window.confirm(
`“${candidate.name}”中 ${candidate.invalidStart.toFixed(3)}–${candidate.invalidEnd.toFixed(3)} 秒的无效动作单独占了一个切片。\n\n是否与前后同名切片合并为 ${candidate.start.toFixed(3)}–${candidate.end.toFixed(3)} 秒的完整切片?全部动作描述、无效标记及时间保持不变。\n确定:合并外层;取消:保留原划分。`,
)
)
consolidateInvalidSliceIds.push(candidate.id);
}
if (
candidates.length &&
!window.confirm(
consolidateInvalidSliceIds.length
? `已选择 ${consolidateInvalidSliceIds.length} 处无损合并。提交后重新生成审核视频,原版本保留。确认提交质检?`
: "保留原切片划分,确认提交质检?",
)
)
return;
const currentTaskId = detail.task.id;
const token = beginAnnotationMutation(currentTaskId);
if (!token) return;
clearEditHistory();
try {
const submission = await annotationOperationsApi.submitCombined(detail.task.id, {
consolidateInvalidSliceIds,
version: detailRef.current?.humanItem?.version ?? detail.humanItem.version,
sliceSegments: applySliceStatuses(
sliceSegments.map((slice) => ({ ...slice, taskLabelId: "", taskLabel: "" })),

View File

@ -178,6 +178,7 @@ export const annotationOperationsApi = {
taskId: string,
payload: {
version: number;
consolidateInvalidSliceIds?: string[];
sliceSegments: SliceSegment[];
sliceOperations?: SliceOperation[];
segments: AnnotationSegment[];

View File

@ -0,0 +1,40 @@
import test from "node:test";
import assert from "node:assert/strict";
import { invalidSliceConsolidationCandidates as candidates } from "../../src/lib/invalid-slice-consolidation.js";
const slices = ["a", "b", "c"].map((id, i) => ({ id, name: "盘点", start: i, end: i + 1 }));
const actions = [{ id: "invalid", start: 1, end: 2, isInvalid: true, description: "无效片段" }];
test("identify invalid-only outer partition without modifying slices or action JSON", () => {
const before = JSON.stringify({ slices, actions });
assert.deepEqual(candidates(slices, actions), [
{ id: "b", name: "盘点", start: 0, end: 3, invalidStart: 1, invalidEnd: 2 },
]);
assert.equal(JSON.stringify({ slices, actions }), before);
});
test("never propose discarded, different-name, gapped or partially invalid partitions", () => {
assert.deepEqual(
candidates(
slices.map((s) => (s.id === "b" ? { ...s, deliveryDisposition: "discard" } : s)),
actions,
),
[],
);
assert.deepEqual(
candidates(
slices.map((s) => (s.id === "c" ? { ...s, name: "清洁" } : s)),
actions,
),
[],
);
assert.deepEqual(
candidates(
slices.map((s) => (s.id === "c" ? { ...s, start: 2.1 } : s)),
actions,
),
[],
);
assert.deepEqual(candidates(slices, [{ ...actions[0], end: 1.5 }]), []);
assert.deepEqual(candidates(slices, [{ ...actions[0], isInvalid: false }]), []);
});
test("invalid inside a single business slice stays inside it", () => {
assert.deepEqual(candidates([{ id: "a", name: "盘点", start: 0, end: 3 }], actions), []);
});