forked from longtao.wu/droboticflow
🛡️ Harden classification saves and recover frozen delivery selections
This commit is contained in:
parent
9546810895
commit
1469e1d9e1
@ -1,6 +1,6 @@
|
||||
# 最终分类交付与内部追溯分离
|
||||
|
||||
Status: claimed
|
||||
Status: done
|
||||
|
||||
2026-10-21 用户要求将两轮总 JSON 核查的问题一起本地修复,并明确选择独立 `tags` 列表、管理员维护。依据为马迪 10-09 至 10-14 的格式对话与本会话补充。原始核查证据在主仓 `local-data/diagnostics/delivery-json-audit-20261021/`。
|
||||
|
||||
@ -20,3 +20,14 @@ Status: claimed
|
||||
定向 Go/Python/前端回归,隔离本机 Mongo 与真实 router 的保存→返工→复审→验收→原生打包→批次及下载,检查最终分类/标签和完整内部追溯;覆盖失败重试、重启/旧版本读取、字典更新、未授权篡改、全舍弃、缺失历史证据。前端与 Go 全量门禁按既有基线对照;真实浏览器检查字典 tags 管理和选择,Web/Go 构建及生产包启动。
|
||||
|
||||
不上传镜像、不部署、不写公司数据、不补写历史交付、不 push/PR。本地完成后以新分支提交。
|
||||
|
||||
|
||||
## 本地完成证据(2026-10-21)
|
||||
|
||||
- 独立分支 `codex/delivery-classification-20261021`,基于 `bc00467d`。本次只做本地代码、合成数据验证及提交;未 push、提 PR、上传镜像、部署、启用工作流或改写历史交付。
|
||||
- Go 与前端全量门禁无新增失败,保留原有 70 / 13 个已知失败,基线文件未修改;类型、格式、构建、生成源码一致性及生产页面启动通过。
|
||||
- 定向 Python 37 项通过(含真实视频物化→v4 的 5 项验证);一个旧版 Go 物化 opt-in 用例未单独启用,新旧真实媒体链路已由独立用例覆盖。
|
||||
- 本机 27018 随机独立 Mongo 库的三轮退回和 API 重启、原生 v4 的正式接口批次发布/失败恢复/重试/下载、停用与移除词条后的直接及面板撤销重做、v5/v6 标签防伪造均通过。测试库执行后清理。
|
||||
- 静态复核的 Standards / Spec 两轴发现均已修正并关闭;保留无快照历史的名称事实,不以当前 tags 猜补。未确认临时名称不能绕过最终确认/提交。
|
||||
- 完整日志、分离的客户/内部样例和浏览器截图保留在本工作树 `local-data/verification/delivery-classification-20261021/`。客户样例子目录 `customer-samples/partNNNN/` 每份只含 `part.json`;`customer-samples/internal/` 不属于客户包。
|
||||
- 新协议默认关闭,线上尚未验证/启用;后续正式发布需要覆盖 API/processor、Worker、package 镜像及内部 artifact 存储,按现有发布授权流程执行。未把旧镜像 release 摘要伪装成新源码摘要。
|
||||
|
||||
@ -846,7 +846,10 @@ def _description_status(actions: list[dict[str, Any]]) -> str:
|
||||
|
||||
|
||||
def _emit_part_index_directory(run_id, node_id, output, node_data, helpers, payload, internal_audit=None):
|
||||
"""Use the established immutable directory publisher for the two JSON files.
|
||||
"""Use the established immutable publisher for versioned customer JSON.
|
||||
|
||||
V3 retains its historical sibling review file; v4 stores that audit in a
|
||||
separate internal artifact before publishing the customer directory.
|
||||
|
||||
Legacy single-file publishers deliberately accept only v1/v2. A directory
|
||||
artifact preserves their ownership/conditional-publication protocol without
|
||||
@ -927,9 +930,9 @@ def _emit_part_index_directory(run_id, node_id, output, node_data, helpers, payl
|
||||
action.pop(key, None)
|
||||
if action.get("dictionary") is None: action.pop("dictionary", None)
|
||||
body = (json.dumps(payload, ensure_ascii=False, sort_keys=True, indent=2) + "\n").encode("utf8")
|
||||
if len(json.dumps(audit, ensure_ascii=False).encode("utf8")) > 8 * 1024 * 1024:
|
||||
raise RuntimeError("internal delivery audit exceeds 8 MiB; refusing incomplete export")
|
||||
audit["partSha256"] = hashlib.sha256(body).hexdigest()
|
||||
if len(json.dumps(audit, ensure_ascii=False, indent=2).encode("utf8")) > 8 * 1024 * 1024:
|
||||
raise RuntimeError("internal delivery audit exceeds 8 MiB; refusing incomplete export")
|
||||
staging = pathlib.Path(tempfile.mkdtemp(prefix="part-index-v4-", dir=output))
|
||||
(staging / "part.json").write_bytes(body)
|
||||
# A sibling artifact stays in the internal run, never in the customer tree.
|
||||
@ -1729,7 +1732,11 @@ def _run_accepted_slice_directory(
|
||||
discarded_slices.append(row)
|
||||
else:
|
||||
kept_slices.append(row)
|
||||
if disposition_contract:
|
||||
params = helpers["hyperparams"](node_data) if callable(helpers.get("hyperparams")) else {}
|
||||
classification_v4 = _bool(params, "DELIVERY_JSON_V4", False)
|
||||
if classification_v4 and not coverage_required:
|
||||
raise RuntimeError("DELIVERY_JSON_V4 requires verified full-video coverage")
|
||||
if disposition_contract or classification_v4:
|
||||
pins = summary_payload.get("acceptedMaterializations", [])
|
||||
expected_sha = _sha256_file(revision_manifest_path)
|
||||
if summary_payload.get("decision") != "approved" or final_acceptance.get("confirmed") is not True or not any(
|
||||
@ -1739,7 +1746,6 @@ def _run_accepted_slice_directory(
|
||||
raise RuntimeError("slice dispositions are not pinned to the accepted manifest revision")
|
||||
if int(coverage.get("sliceCount", -1)) != len(all_slices):
|
||||
raise RuntimeError("review coverage slice count mismatch")
|
||||
params = helpers["hyperparams"](node_data) if callable(helpers.get("hyperparams")) else {}
|
||||
if disposition_contract and not kept_slices and not _bool(params, "DELIVERY_JSON_V4", False):
|
||||
if any(p.get("key") == "PROCESSING_PROFILE" and p.get("value") == "raw_human" for p in node_data.get("hyperparams", [])) and (summary_payload.get("processingProfile") != "raw_human" or final_acceptance.get("processingProfile") != "raw_human"):
|
||||
raise RuntimeError("raw-human package requires matching accepted task profile")
|
||||
@ -1758,7 +1764,6 @@ def _run_accepted_slice_directory(
|
||||
payloads = _payload_tree(_payloads_with_full_pose_manifests(inputs, output, helpers))
|
||||
headpose = _first_payload(payloads, "droboticflow.headpose/v1")
|
||||
handpose = _first_payload(payloads, "droboticflow.wilor.hand_pose_result/v1")
|
||||
params = helpers["hyperparams"](node_data) if callable(helpers.get("hyperparams")) else {}
|
||||
if _bool(params, "PART_INDEX_DELIVERY", False):
|
||||
if source_index and not _bool(params, "SOURCE_INDEX_DELIVERY", False):
|
||||
raise RuntimeError("source-index JSON delivery requires explicit SOURCE_INDEX_DELIVERY approval; use directory delivery to preserve physical discard exclusion")
|
||||
|
||||
@ -12,8 +12,8 @@
|
||||
- `discardedSlices[].classification.discardEvidence` 保留业务舍弃原因、原因词条/版本/标签和补充说明。全舍弃 part 也输出总分类 JSON。审核退回原因不放在这里。
|
||||
- `sourceMetadata.path` 与标定路径相对原采集目录。源索引 `sources` 的 workspace 路径基准显式标记。内部审核文件不在客户目录内,也不从主 JSON 引用。
|
||||
|
||||
内部 `droboticflow.delivery.review-history/v2` JSON 按 capture/part/revision 关联,以 `partSha256` 固定原生主文件字节;包含最终验收、抽检、审核人/批注/轮次/确认项、已有正式退回基线、处理记录和阶段事件。该 artifact 留在内部 Run 存储,不进入最终交付列表、不可变客户批次或客户下载。旧记录没有的追溯不制造补写。追溯超出限制时拒绝交付,不静默截断。
|
||||
内部 `droboticflow.delivery.review-history/v2` JSON 按 capture/part/revision 关联,以 `partSha256` 固定原生主文件字节;包含最终验收、抽检、审核人/批注/轮次/确认项、已有正式退回基线、处理记录和阶段事件。该 artifact 留在内部 Run 存储,不进入最终交付列表、不可变客户批次或客户下载。分类选择的服务端快照另外保存在当前任务的恢复记录中,使字典改名、更新或停用后的撤销仍可恢复原版本,客户端不能伪造历史标签。旧记录没有的追溯不制造补写。追溯超出限制时拒绝交付,不静默截断。
|
||||
|
||||
最终分类取通过验收的固定物化修订。返工正式修改分类后必须重新送检、验收;查看或处理意见本身不修改分类。相同名称而字典版本/标签不同的切片不能自动合并成一个分类。
|
||||
最终分类取通过验收的固定物化修订;沿用既有“未抽中”验收策略的任务,内部保留明确豁免,不能伪造人工通过日期。返工正式修改分类后必须重新送检、验收;查看或处理意见本身不修改分类。相同名称而字典版本/标签不同的切片不能自动合并成一个分类。
|
||||
|
||||
本地可用 `python -m tests.backend.export_delivery_v4_fixture <全新输出目录>` 生成明确标记的合成样例。目录中 `partNNNN/part.json` 是客户主文件;`internal/partNNNN/review-history.json` 是分离的内部示例,不属于客户包。不要把整个验证输出目录直接交付给客户。
|
||||
|
||||
@ -293,13 +293,16 @@ func (server *server) handleProjectAnnotationLabelsGet(writer http.ResponseWrite
|
||||
if selection == nil {
|
||||
selection = &domain.ProjectAnnotationLabelsRecord{ID: "project-annotation-labels-" + project.ID, OrganizationID: project.OrganizationID, ProjectID: project.ID, LabelIDs: []string{}}
|
||||
}
|
||||
if projectDictionaryKind(request) != "action" {
|
||||
{
|
||||
available, e := server.store.ListAnnotationLabels(request.Context(), project.OrganizationID)
|
||||
if e != nil {
|
||||
renderError(writer, 500, e)
|
||||
return
|
||||
}
|
||||
ids := selection.SliceLabelIDs
|
||||
ids := selection.LabelIDs
|
||||
if projectDictionaryKind(request) == "slice" {
|
||||
ids = selection.SliceLabelIDs
|
||||
}
|
||||
if projectDictionaryKind(request) == "discard_reason" {
|
||||
ids = selection.DiscardReasonLabelIDs
|
||||
}
|
||||
@ -316,14 +319,20 @@ func (server *server) handleProjectAnnotationLabelsGet(writer http.ResponseWrite
|
||||
items = append(items, label)
|
||||
}
|
||||
}
|
||||
response := map[string]any{"projectId": project.ID, "labelIds": ids, "items": items}
|
||||
response := struct {
|
||||
domain.ProjectAnnotationLabelsRecord
|
||||
Items []domain.AnnotationLabelRecord `json:"items"`
|
||||
AllowManual *bool `json:"allowManual,omitempty"`
|
||||
}{ProjectAnnotationLabelsRecord: *selection, Items: items}
|
||||
response.LabelIDs = ids
|
||||
if projectDictionaryKind(request) == "discard_reason" {
|
||||
response["allowManual"] = selection.OrganizationID == project.OrganizationID && len(ids) == 0
|
||||
manual := selection.OrganizationID == project.OrganizationID && len(ids) == 0
|
||||
response.AllowManual = &manual
|
||||
}
|
||||
|
||||
renderJSON(writer, http.StatusOK, response)
|
||||
return
|
||||
}
|
||||
renderJSON(writer, http.StatusOK, selection)
|
||||
}
|
||||
|
||||
func (server *server) handleProjectAnnotationLabelsPut(writer http.ResponseWriter, request *http.Request) {
|
||||
|
||||
80
internal/api/dictionary_selection_history.go
Normal file
80
internal/api/dictionary_selection_history.go
Normal file
@ -0,0 +1,80 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"droboticflow/internal/domain"
|
||||
)
|
||||
|
||||
const dictionarySelectionHistoryKey = "dictionarySelectionHistory"
|
||||
|
||||
// Only server-resolved selections enter this item-scoped recovery ledger.
|
||||
// It allows undo after a shared word changes or leaves the project, without
|
||||
// treating the client's undo snapshot as trustworthy dictionary metadata.
|
||||
func rememberDictionarySelections(item *domain.HumanItemRecord, selections ...*domain.DictionarySelection) []validationIssue {
|
||||
if item == nil {
|
||||
return nil
|
||||
}
|
||||
history := decodeMaterializationValue[domain.DictionarySelection](item.Subject.Payload[dictionarySelectionHistoryKey])
|
||||
seen := map[string]bool{}
|
||||
key := func(d domain.DictionarySelection) string { return fmt.Sprintf("%s/%s/%d", d.Kind, d.ID, d.Version) }
|
||||
for _, d := range history {
|
||||
seen[key(d)] = true
|
||||
}
|
||||
for _, d := range selections {
|
||||
if d != nil && !seen[key(*d)] {
|
||||
copy := *d
|
||||
copy.Tags = append([]string{}, d.Tags...)
|
||||
history = append(history, copy)
|
||||
seen[key(copy)] = true
|
||||
}
|
||||
}
|
||||
body, err := json.Marshal(history)
|
||||
if err != nil || len(body) > 1<<20 {
|
||||
return []validationIssue{{Field: "dictionary", Message: "分类恢复记录超过限制,请拆分任务后重试"}}
|
||||
}
|
||||
if item.Subject.Payload == nil {
|
||||
item.Subject.Payload = map[string]any{}
|
||||
}
|
||||
if len(history) > 0 {
|
||||
item.Subject.Payload[dictionarySelectionHistoryKey] = history
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func savedDictionarySelection(item *domain.HumanItemRecord, requested *domain.DictionarySelection, kind string) *domain.DictionarySelection {
|
||||
if item == nil || requested == nil {
|
||||
return nil
|
||||
}
|
||||
for _, saved := range decodeMaterializationValue[domain.DictionarySelection](item.Subject.Payload[dictionarySelectionHistoryKey]) {
|
||||
if saved.Kind == kind && saved.ID == requested.ID && saved.Version == requested.Version {
|
||||
copy := saved
|
||||
return ©
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func sliceDictionarySelections(slices []domain.SliceSegment) []*domain.DictionarySelection {
|
||||
selections := []*domain.DictionarySelection{}
|
||||
for _, slice := range slices {
|
||||
selections = append(selections, slice.Dictionary, slice.DiscardReasonDictionary)
|
||||
}
|
||||
return selections
|
||||
}
|
||||
func actionDictionarySelections(actions []domain.AnnotationSegment) []*domain.DictionarySelection {
|
||||
selections := []*domain.DictionarySelection{}
|
||||
for _, action := range actions {
|
||||
selections = append(selections, action.Dictionary)
|
||||
}
|
||||
return selections
|
||||
}
|
||||
|
||||
func copyClassificationRecoveryState(target, source *domain.HumanItemRecord) {
|
||||
for _, key := range []string{dictionarySelectionHistoryKey, "unconfirmedSliceNames"} {
|
||||
if value, ok := source.Subject.Payload[key]; ok {
|
||||
target.Subject.Payload[key] = value
|
||||
} else {
|
||||
delete(target.Subject.Payload, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -43,7 +43,7 @@ func sameDictionaryVersion(a, b *domain.DictionarySelection) bool {
|
||||
return a != nil && b != nil && a.ID == b.ID && a.Version == b.Version
|
||||
}
|
||||
|
||||
func (s *server) canonicalActionDictionaries(ctx context.Context, item *domain.HumanItemRecord, segments []domain.AnnotationSegment) []validationIssue {
|
||||
func (s *server) canonicalActionDictionaries(ctx context.Context, item *domain.HumanItemRecord, segments []domain.AnnotationSegment, mode ...string) []validationIssue {
|
||||
previous := decodeMaterializationValue[domain.AnnotationSegment](annotationWorkspacePayload(item)["segments"])
|
||||
if len(previous) == 0 && item.LegacyTaskID != "" {
|
||||
revisions, err := s.store.ListAnnotationRevisions(ctx, item.OrganizationID, item.LegacyTaskID)
|
||||
@ -56,6 +56,9 @@ func (s *server) canonicalActionDictionaries(ctx context.Context, item *domain.H
|
||||
}
|
||||
}
|
||||
}
|
||||
if issues := rememberDictionarySelections(item, actionDictionarySelections(previous)...); len(issues) > 0 {
|
||||
return issues
|
||||
}
|
||||
old := map[string]domain.AnnotationSegment{}
|
||||
for _, seg := range previous {
|
||||
old[seg.ID] = seg
|
||||
@ -69,6 +72,12 @@ func (s *server) canonicalActionDictionaries(ctx context.Context, item *domain.H
|
||||
seg.ClearDictionary = false
|
||||
continue
|
||||
}
|
||||
if len(mode) > 0 && mode[0] == "restore" && !seg.IsInvalid && strings.TrimSpace(seg.Description) != "" {
|
||||
if saved := savedDictionarySelection(item, seg.Dictionary, "action"); saved != nil {
|
||||
seg.Dictionary = saved
|
||||
continue
|
||||
}
|
||||
}
|
||||
if seg.IsInvalid || strings.TrimSpace(seg.Description) == "" {
|
||||
seg.Dictionary = nil
|
||||
continue
|
||||
@ -93,5 +102,5 @@ func (s *server) canonicalActionDictionaries(ctx context.Context, item *domain.H
|
||||
}
|
||||
seg.Dictionary = label.Selection()
|
||||
}
|
||||
return nil
|
||||
return rememberDictionarySelections(item, actionDictionarySelections(segments)...)
|
||||
}
|
||||
|
||||
@ -119,3 +119,293 @@ func TestDictionaryTagsHaveIndependentRevisionAndAdminAuthorization(t *testing.T
|
||||
t.Fatal("tag history lost")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLegacyClassificationDoesNotGuessCurrentTagsAtConfirmation(t *testing.T) {
|
||||
f := newCombinedDraftFixture(t)
|
||||
legacy := domain.SliceSegment{ID: "legacy", Name: "物品操作", Start: 0, End: 10}
|
||||
f.item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": []domain.SliceSegment{legacy}}
|
||||
f.store.UpsertHumanItem(f.ctx, f.item)
|
||||
f.store.UpsertAnnotationLabel(f.ctx, domain.AnnotationLabelRecord{ID: "current", OrganizationID: f.item.OrganizationID, Kind: "slice", Code: "base", Name: legacy.Name, Tags: []string{"现在新增的标签"}, Version: 9, Status: domain.AnnotationLabelActive})
|
||||
f.store.UpsertProjectSliceLabels(f.ctx, domain.ProjectAnnotationLabelsRecord{ProjectID: f.task.ProjectID, OrganizationID: f.item.OrganizationID, SliceLabelIDs: []string{"current"}})
|
||||
server := &server{store: f.store}
|
||||
slices := []domain.SliceSegment{legacy}
|
||||
if issues := server.validateProjectSliceNames(f.ctx, &f.item, slices, "confirm"); len(issues) > 0 || slices[0].Dictionary != nil {
|
||||
t.Fatal("legacy tags guessed at confirm")
|
||||
}
|
||||
slices[0].Dictionary = &domain.DictionarySelection{ID: "current", Version: 9}
|
||||
if issues := server.validateProjectSliceNames(f.ctx, &f.item, slices, "confirm"); len(issues) > 0 || slices[0].Dictionary == nil || slices[0].Dictionary.Tags[0] != "现在新增的标签" {
|
||||
t.Fatal("explicit new selection did not freeze current tags")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSliceEditApplicationCanonicalizesAgainstStoredDictionaryState(t *testing.T) {
|
||||
for _, foreign := range []bool{false, true} {
|
||||
t.Run(map[bool]string{false: "forged-tags", true: "foreign-word"}[foreign], func(t *testing.T) {
|
||||
f := continuousFixture(t)
|
||||
selection, _ := f.store.GetProjectAnnotationLabels(f.ctx, f.task.ProjectID)
|
||||
label, _ := f.store.GetAnnotationLabel(f.ctx, selection.SliceLabelIDs[0])
|
||||
label.Tags = []string{"服务端标签"}
|
||||
label.Version = 2
|
||||
f.store.UpsertAnnotationLabel(f.ctx, *label)
|
||||
before := []domain.SliceSegment{{ID: "s", Name: label.Name, Start: 0, End: 10}}
|
||||
item := f.item
|
||||
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed", "phase": "annotate"}
|
||||
item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": before, "segments": []domain.AnnotationSegment{}}
|
||||
f.store.UpsertHumanItem(f.ctx, item)
|
||||
incoming := *label.Selection()
|
||||
incoming.Tags = []string{"FORGED"}
|
||||
if foreign {
|
||||
incoming.ID = "foreign-label"
|
||||
}
|
||||
proposed := append([]domain.SliceSegment(nil), before...)
|
||||
proposed[0].Dictionary = &incoming
|
||||
body := sliceEditApplicationInput{ApplicationID: "classification-apply", combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: item.Version, SliceSegments: proposed}}
|
||||
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-edit-applications", f.annotator, body)
|
||||
if foreign {
|
||||
if r.Code != 400 {
|
||||
t.Fatalf("foreign classification accepted %d %s", r.Code, r.Body.String())
|
||||
}
|
||||
return
|
||||
}
|
||||
if r.Code != 200 {
|
||||
t.Fatalf("%d %s", r.Code, r.Body.String())
|
||||
}
|
||||
saved, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
||||
got := decodeMaterializationValue[domain.SliceSegment](annotationWorkspacePayload(saved)["sliceSegments"])
|
||||
if got[0].Dictionary == nil || !reflect.DeepEqual(got[0].Dictionary.Tags, label.Tags) {
|
||||
t.Fatal("client classification snapshot trusted")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProjectActionDictionaryReturnsVersionedItemsForAnnotator(t *testing.T) {
|
||||
f := newCombinedDraftFixture(t)
|
||||
label := domain.AnnotationLabelRecord{ID: "action-word", OrganizationID: f.item.OrganizationID, Code: "pick", Name: "拿取", Kind: "action", Version: 3, Tags: []string{"抓取"}, Status: domain.AnnotationLabelActive}
|
||||
f.store.UpsertAnnotationLabel(f.ctx, label)
|
||||
f.store.UpsertProjectAnnotationLabels(f.ctx, domain.ProjectAnnotationLabelsRecord{ID: "selection", ProjectID: f.task.ProjectID, OrganizationID: f.item.OrganizationID, LabelIDs: []string{label.ID}})
|
||||
r := f.request("GET", "/api/projects/"+f.task.ProjectID+"/annotation-labels", f.annotator, nil)
|
||||
var response struct {
|
||||
ID string `json:"id"`
|
||||
LabelIDs []string `json:"labelIds"`
|
||||
Items []domain.AnnotationLabelRecord `json:"items"`
|
||||
}
|
||||
if json.Unmarshal(r.Body.Bytes(), &response) != nil || r.Code != 200 || response.ID != "selection" || len(response.LabelIDs) != 1 || len(response.Items) != 1 || response.Items[0].Version != 3 || response.Items[0].Tags[0] != "抓取" {
|
||||
t.Fatalf("%d %s", r.Code, r.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUndoRestoresOnlyServerRememberedClassificationsAfterDictionaryMaintenance(t *testing.T) {
|
||||
f := continuousFixture(t)
|
||||
srv := &server{store: f.store}
|
||||
base := domain.AnnotationLabelRecord{ID: "base-old", OrganizationID: f.item.OrganizationID, Kind: "slice", Code: "base", Name: "旧基础分类", Version: 2, Tags: []string{"原标签"}, Status: domain.AnnotationLabelActive}
|
||||
action := domain.AnnotationLabelRecord{ID: "action-old", OrganizationID: f.item.OrganizationID, Kind: "action", Code: "pick", Name: "旧动作分类", Version: 3, Tags: []string{"原动作标签"}, Status: domain.AnnotationLabelActive}
|
||||
f.store.UpsertAnnotationLabel(f.ctx, base)
|
||||
f.store.UpsertAnnotationLabel(f.ctx, action)
|
||||
selection := domain.ProjectAnnotationLabelsRecord{ProjectID: f.task.ProjectID, OrganizationID: f.item.OrganizationID, LabelIDs: []string{action.ID}, SliceLabelIDs: []string{base.ID}}
|
||||
f.store.UpsertProjectAnnotationLabels(f.ctx, selection)
|
||||
f.store.UpsertProjectSliceLabels(f.ctx, selection)
|
||||
oldSlices := []domain.SliceSegment{{ID: "s", Name: base.Name, Start: 0, End: 10, Dictionary: base.Selection()}}
|
||||
oldActions := []domain.AnnotationSegment{{ID: "a", Description: "原描述", Start: 0, End: 10, Status: "described", Dictionary: action.Selection()}}
|
||||
item := f.item
|
||||
if issues := srv.validateProjectSliceNames(f.ctx, &item, oldSlices, "draft"); len(issues) > 0 {
|
||||
t.Fatal(issues)
|
||||
}
|
||||
if issues := srv.canonicalActionDictionaries(f.ctx, &item, oldActions); len(issues) > 0 {
|
||||
t.Fatal(issues)
|
||||
}
|
||||
item.Subject.Payload["sliceWorkflow"] = map[string]any{"phase": "annotate", "status": "completed"}
|
||||
// Later editing selected a different classification. The word is subsequently
|
||||
// renamed, retagged, disabled and unlinked by an administrator.
|
||||
item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": []domain.SliceSegment{{ID: "s", Name: "另一个分类", Start: 0, End: 10}}, "segments": []domain.AnnotationSegment{{ID: "a", Description: "修改后描述", Start: 0, End: 10, Status: "described"}}}
|
||||
f.store.UpsertHumanItem(f.ctx, item)
|
||||
base.Name = "新版"
|
||||
base.Tags = []string{"新标签"}
|
||||
base.Version++
|
||||
base.Status = domain.AnnotationLabelInactive
|
||||
f.store.UpsertAnnotationLabel(f.ctx, base)
|
||||
action.Version++
|
||||
action.Status = domain.AnnotationLabelInactive
|
||||
f.store.UpsertAnnotationLabel(f.ctx, action)
|
||||
f.store.UpsertProjectSliceLabels(f.ctx, domain.ProjectAnnotationLabelsRecord{ProjectID: f.task.ProjectID, OrganizationID: f.item.OrganizationID})
|
||||
f.store.UpsertProjectAnnotationLabels(f.ctx, domain.ProjectAnnotationLabelsRecord{ProjectID: f.task.ProjectID, OrganizationID: f.item.OrganizationID})
|
||||
// Simulate serialization and restart; do not use process-local snapshot state.
|
||||
f.handler = NewHandler(f.store, nil)
|
||||
oldSlices[0].Dictionary.Tags = []string{"FORGED"}
|
||||
oldActions[0].Dictionary.Tags = []string{"FORGED"}
|
||||
body := combinedAnnotationDraftInput{Version: item.Version, SliceSegments: oldSlices, Segments: oldActions}
|
||||
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-edit-restorations", f.annotator, body)
|
||||
if r.Code != 200 {
|
||||
t.Fatalf("undo %d %s", r.Code, r.Body.String())
|
||||
}
|
||||
saved, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
||||
slices := decodeMaterializationValue[domain.SliceSegment](annotationWorkspacePayload(saved)["sliceSegments"])
|
||||
actions := decodeMaterializationValue[domain.AnnotationSegment](annotationWorkspacePayload(saved)["segments"])
|
||||
if slices[0].Dictionary.Version != 2 || slices[0].Dictionary.Tags[0] != "原标签" || actions[0].Dictionary.Version != 3 || actions[0].Dictionary.Tags[0] != "原动作标签" {
|
||||
t.Fatal("undo lost or forged frozen classification")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectDispositionCannotPersistForeignOrForgedSliceDictionary(t *testing.T) {
|
||||
for _, foreign := range []bool{false, true} {
|
||||
t.Run(map[bool]string{false: "forged-tags", true: "foreign-word"}[foreign], func(t *testing.T) {
|
||||
f := continuousFixture(t)
|
||||
body := dispositionFixturePayload(t, f)
|
||||
selection, _ := f.store.GetProjectAnnotationLabels(f.ctx, f.task.ProjectID)
|
||||
label, _ := f.store.GetAnnotationLabel(f.ctx, selection.SliceLabelIDs[0])
|
||||
label.Tags = []string{"服务端标签"}
|
||||
label.Version = 2
|
||||
f.store.UpsertAnnotationLabel(f.ctx, *label)
|
||||
body.SliceSegments[0].Name = label.Name
|
||||
d := *label.Selection()
|
||||
d.Tags = []string{"FORGED"}
|
||||
if foreign {
|
||||
d.ID = "foreign"
|
||||
}
|
||||
body.SliceSegments[0].Dictionary = &d
|
||||
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", f.annotator, body)
|
||||
if foreign {
|
||||
if r.Code != 400 {
|
||||
t.Fatalf("foreign dictionary saved %d %s", r.Code, r.Body.String())
|
||||
}
|
||||
return
|
||||
}
|
||||
if r.Code != 200 {
|
||||
t.Fatalf("%d %s", r.Code, r.Body.String())
|
||||
}
|
||||
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
||||
saved := decodeMaterializationValue[domain.SliceSegment](annotationWorkspacePayload(item)["sliceSegments"])
|
||||
if saved[0].Dictionary == nil || !reflect.DeepEqual(saved[0].Dictionary.Tags, label.Tags) {
|
||||
t.Fatal("forged tags persisted through direct disposition")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispositionUndoRedoPreservesClassificationsAfterDictionaryRemoval(t *testing.T) {
|
||||
for _, application := range []bool{false, true} {
|
||||
t.Run(map[bool]string{false: "direct", true: "panel-application"}[application], func(t *testing.T) {
|
||||
f := continuousFixture(t)
|
||||
body := dispositionFixturePayload(t, f)
|
||||
selection, _ := f.store.GetProjectAnnotationLabels(f.ctx, f.task.ProjectID)
|
||||
base, _ := f.store.GetAnnotationLabel(f.ctx, selection.SliceLabelIDs[0])
|
||||
base.Tags = []string{"基础标签"}
|
||||
base.Version = 2
|
||||
f.store.UpsertAnnotationLabel(f.ctx, *base)
|
||||
reason, _ := f.store.GetAnnotationLabel(f.ctx, body.ReasonLabelID)
|
||||
reason.Tags = []string{"舍弃标签"}
|
||||
f.store.UpsertAnnotationLabel(f.ctx, *reason)
|
||||
for i := range body.SliceSegments {
|
||||
body.SliceSegments[i].Name = base.Name
|
||||
body.SliceSegments[i].Dictionary = base.Selection()
|
||||
}
|
||||
original := append([]domain.SliceSegment(nil), body.SliceSegments...)
|
||||
endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID
|
||||
r := f.request("POST", endpoint+"/slice-dispositions", f.annotator, body)
|
||||
if r.Code != 200 {
|
||||
t.Fatalf("discard %d %s", r.Code, r.Body.String())
|
||||
}
|
||||
var first struct {
|
||||
Item domain.HumanItemRecord `json:"item"`
|
||||
Workspace combinedAnnotationDraftInput `json:"workspace"`
|
||||
}
|
||||
json.Unmarshal(r.Body.Bytes(), &first)
|
||||
base.Name = "新名字"
|
||||
base.Version++
|
||||
base.Tags = []string{"新标签"}
|
||||
base.Status = domain.AnnotationLabelInactive
|
||||
f.store.UpsertAnnotationLabel(f.ctx, *base)
|
||||
reason.Version++
|
||||
reason.Status = domain.AnnotationLabelInactive
|
||||
f.store.UpsertAnnotationLabel(f.ctx, *reason)
|
||||
f.store.UpsertProjectSliceLabels(f.ctx, domain.ProjectAnnotationLabelsRecord{ProjectID: f.task.ProjectID, OrganizationID: f.item.OrganizationID})
|
||||
f.store.UpsertProjectDiscardReasonLabels(f.ctx, domain.ProjectAnnotationLabelsRecord{ProjectID: f.task.ProjectID, OrganizationID: f.item.OrganizationID})
|
||||
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
||||
item.Subject.Payload["sliceWorkflow"] = map[string]any{"phase": "annotate", "status": "completed"}
|
||||
f.store.UpsertHumanItem(f.ctx, *item)
|
||||
f.handler = NewHandler(f.store, nil)
|
||||
if application {
|
||||
input := sliceEditApplicationInput{ApplicationID: "undo-discard", RestoreClassifications: true, combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: item.Version, SliceSegments: original}, Dispositions: []sliceDispositionInput{{SliceID: body.SliceID, Disposition: "keep"}}}
|
||||
r = f.request("POST", endpoint+"/slice-edit-applications", f.annotator, input)
|
||||
} else {
|
||||
undo := body
|
||||
undo.combinedAnnotationDraftInput = first.Workspace
|
||||
undo.Version = item.Version
|
||||
undo.Disposition = "keep"
|
||||
undo.RestoreClassifications = true
|
||||
undo.RestorePreviousName = true
|
||||
undo.RestoreNameFromVersion = first.Item.Version
|
||||
r = f.request("POST", endpoint+"/slice-dispositions", f.annotator, undo)
|
||||
}
|
||||
if r.Code != 200 {
|
||||
t.Fatalf("undo %d %s", r.Code, r.Body.String())
|
||||
}
|
||||
var restored struct {
|
||||
Item domain.HumanItemRecord `json:"item"`
|
||||
Workspace combinedAnnotationDraftInput `json:"workspace"`
|
||||
}
|
||||
json.Unmarshal(r.Body.Bytes(), &restored)
|
||||
d := restored.Workspace.SliceSegments[1].Dictionary
|
||||
if d == nil || d.Version != 2 || d.Tags[0] != "基础标签" {
|
||||
t.Fatal("undo lost frozen base category")
|
||||
}
|
||||
if application {
|
||||
input := sliceEditApplicationInput{ApplicationID: "redo-discard", RestoreClassifications: true, combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: restored.Item.Version, SliceSegments: first.Workspace.SliceSegments}, Dispositions: []sliceDispositionInput{{SliceID: body.SliceID, Disposition: "discard", ReasonLabelID: body.ReasonLabelID, ReasonVersion: body.ReasonVersion}}}
|
||||
r = f.request("POST", endpoint+"/slice-edit-applications", f.annotator, input)
|
||||
} else {
|
||||
redo := body
|
||||
redo.combinedAnnotationDraftInput = restored.Workspace
|
||||
redo.Version = restored.Item.Version
|
||||
redo.RestoreClassifications = true
|
||||
r = f.request("POST", endpoint+"/slice-dispositions", f.annotator, redo)
|
||||
}
|
||||
if r.Code != 200 {
|
||||
t.Fatalf("redo %d %s", r.Code, r.Body.String())
|
||||
}
|
||||
var redone struct {
|
||||
Workspace combinedAnnotationDraftInput `json:"workspace"`
|
||||
}
|
||||
json.Unmarshal(r.Body.Bytes(), &redone)
|
||||
d = redone.Workspace.SliceSegments[1].DiscardReasonDictionary
|
||||
if d == nil || d.Version != 1 || d.Tags[0] != "舍弃标签" {
|
||||
t.Fatal("redo lost frozen discard reason")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnconfirmedRestoredNamesCannotBecomeFinalClassification(t *testing.T) {
|
||||
f := newCombinedDraftFixture(t)
|
||||
srv := &server{store: f.store}
|
||||
slices := []domain.SliceSegment{{ID: "s", Name: "未选择字典的临时名称", Start: 0, End: 10}}
|
||||
if issues := srv.validateProjectSliceNames(f.ctx, &f.item, slices, "restore"); len(issues) > 0 {
|
||||
t.Fatal(issues)
|
||||
}
|
||||
f.item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": slices}
|
||||
for _, mode := range []string{"confirm", "submit"} {
|
||||
if issues := srv.validateProjectSliceNames(f.ctx, &f.item, slices, mode); len(issues) == 0 {
|
||||
t.Fatalf("unfinished name bypassed %s", mode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLegacyDispositionCannotSeedDictionaryRecoveryHistory(t *testing.T) {
|
||||
f := newCombinedDraftFixture(t)
|
||||
f.task.TemplateVersion = 5
|
||||
f.store.UpsertAnnotationTask(f.ctx, f.task)
|
||||
body := dispositionFixturePayload(t, f)
|
||||
body.Segments[0].Handedness = "both"
|
||||
body.SliceSegments[0].Name = "原有名称"
|
||||
body.SliceSegments[0].TaskLabelID = "legacy-task-label"
|
||||
body.SliceSegments[0].TaskLabel = "原有任务分类"
|
||||
body.SliceSegments[0].Dictionary = &domain.DictionarySelection{ID: "FORGED", Kind: "action", Version: 9, Name: "伪造分类", Code: "forged", Tags: []string{"FORGED"}}
|
||||
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", f.annotator, body)
|
||||
if r.Code != 200 {
|
||||
t.Fatalf("legacy save %d %s", r.Code, r.Body.String())
|
||||
}
|
||||
saved, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
||||
slices := decodeMaterializationValue[domain.SliceSegment](annotationWorkspacePayload(saved)["sliceSegments"])
|
||||
if slices[0].Dictionary != nil || slices[0].Name != "原有名称" || slices[0].TaskLabelID != "legacy-task-label" || savedDictionarySelection(saved, &domain.DictionarySelection{ID: "FORGED", Version: 9}, "action") != nil {
|
||||
t.Fatal("legacy task seeded a dictionary snapshot or lost legacy labels")
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,6 +11,19 @@ func (s *server) resolveDiscardReason(ctx context.Context, item *domain.HumanIte
|
||||
issue := func(message string) (*domain.AnnotationLabelRecord, []validationIssue) {
|
||||
return nil, []validationIssue{{Field: "reasonLabelId", Code: "discard_reason_dictionary_required", Message: message}}
|
||||
}
|
||||
if body.RestoreClassifications {
|
||||
if prior := historicalDiscardReason(item, body); prior != nil {
|
||||
if prior.DiscardReasonDictionary != nil {
|
||||
d := prior.DiscardReasonDictionary
|
||||
return &domain.AnnotationLabelRecord{ID: d.ID, Kind: d.Kind, Code: d.Code, Name: d.Name, Version: d.Version, Tags: append([]string{}, d.Tags...), OrganizationID: item.OrganizationID}, nil
|
||||
}
|
||||
if prior.DiscardReasonLabelID == "" {
|
||||
return nil, nil
|
||||
}
|
||||
return &domain.AnnotationLabelRecord{ID: prior.DiscardReasonLabelID, Kind: "discard_reason", Name: prior.DiscardReasonName, Version: prior.DiscardReasonVersion, OrganizationID: item.OrganizationID}, nil
|
||||
}
|
||||
return issue("未找到当前任务已保存的舍弃原因,无法恢复该历史选择")
|
||||
}
|
||||
selection, err := s.store.GetProjectAnnotationLabels(ctx, item.ProjectID)
|
||||
if err != nil {
|
||||
return issue("舍弃原因字典加载失败,请重试")
|
||||
@ -75,3 +88,24 @@ func describeSliceNameIssues(issues []validationIssue, slices []domain.SliceSegm
|
||||
func discardReasonEvidence(slice domain.SliceSegment) map[string]any {
|
||||
return map[string]any{"dictionary": slice.DiscardReasonDictionary, "reason": slice.DiscardReason, "reasonLabelId": slice.DiscardReasonLabelID, "reasonVersion": slice.DiscardReasonVersion, "reasonName": slice.DiscardReasonName, "reasonNote": slice.DiscardReasonNote, "at": slice.DiscardedAt, "previousAnnotations": slice.DiscardedAnnotations}
|
||||
}
|
||||
|
||||
// Restore only a reason/note combination actually recorded for this slice;
|
||||
// administrator changes never let a client invent historical reason metadata.
|
||||
func historicalDiscardReason(item *domain.HumanItemRecord, body sliceDispositionInput) *domain.SliceSegment {
|
||||
history, _ := normalizeStructuredValue(item.Subject.Payload["sliceDispositionHistory"]).([]any)
|
||||
for i := len(history) - 1; i >= 0; i-- {
|
||||
entry := mapValue(history[i])
|
||||
if stringValue(entry["sliceId"]) != body.SliceID {
|
||||
continue
|
||||
}
|
||||
after := decodeMaterializationValue[domain.SliceSegment]([]any{entry["after"]})
|
||||
if len(after) != 1 {
|
||||
continue
|
||||
}
|
||||
saved := after[0]
|
||||
if saved.IsDiscarded() && saved.DiscardReasonLabelID == body.ReasonLabelID && saved.DiscardReasonVersion == body.ReasonVersion && saved.DiscardReasonNote == strings.TrimSpace(body.ReasonNote) && (saved.DiscardReasonLabelID != "" || saved.DiscardReason == strings.TrimSpace(body.Reason)) {
|
||||
return &saved
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -89,6 +89,7 @@ func TestReworkMongoHTTPRepeatedReturnAcrossRestart(t *testing.T) {
|
||||
batch, e := f.store.GetHumanBatch(ctx, f.item.BatchID)
|
||||
must(e)
|
||||
seed("humanBatches", batch)
|
||||
f.item.Subject.Payload[dictionarySelectionHistoryKey] = []domain.DictionarySelection{{ID: "historical-base", Kind: "slice", Version: 2, Code: "sort", Name: "物品操作", Tags: []string{"商品"}}}
|
||||
seed("humanItems", f.item)
|
||||
seed("annotationTasks", f.task)
|
||||
slices, e := f.store.ListSliceRevisions(ctx, f.item.OrganizationID, f.task.ID)
|
||||
@ -136,6 +137,9 @@ func TestReworkMongoHTTPRepeatedReturnAcrossRestart(t *testing.T) {
|
||||
if w.Code != 200 || len(out.Items) != 1 || out.Items[0].AssignedUserID != f.annotator.ID {
|
||||
t.Fatalf("persisted return missing from author pool: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if snap := savedDictionarySelection(&out.Items[0], &domain.DictionarySelection{ID: "historical-base", Version: 2}, "slice"); snap == nil || snap.Tags[0] != "商品" {
|
||||
t.Fatal("classification recovery ledger lost across Mongo restart")
|
||||
}
|
||||
baseline, e := ds.GetReworkBaseline(ctx, store.ReworkBaselineID(item.ID, out.Items[0].ReviewRound))
|
||||
must(e)
|
||||
if baseline == nil || len(baseline.Slices) == 0 || baseline.Slices[0].Dictionary == nil || baseline.Slices[0].Dictionary.Version != 2 || baseline.Slices[0].Dictionary.Tags[0] != "商品" {
|
||||
|
||||
@ -18,6 +18,11 @@ func (s *server) validateProjectSliceNames(ctx context.Context, item *domain.Hum
|
||||
return []validationIssue{{Field: "sliceSegments", Message: "无法读取切片字典规则"}}
|
||||
}
|
||||
if task == nil || task.TemplateVersion < 6 {
|
||||
// Older templates use taskLabelId/taskLabel; they cannot introduce the
|
||||
// independent v6 slice dictionary field, including through undo/discard.
|
||||
for i := range slices {
|
||||
slices[i].Dictionary = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
previous := decodeMaterializationValue[domain.SliceSegment](annotationWorkspacePayload(item)["sliceSegments"])
|
||||
@ -32,24 +37,47 @@ func (s *server) validateProjectSliceNames(ctx context.Context, item *domain.Hum
|
||||
}
|
||||
}
|
||||
}
|
||||
if issues := rememberDictionarySelections(item, sliceDictionarySelections(previous)...); len(issues) > 0 {
|
||||
return issues
|
||||
}
|
||||
old := map[string]domain.SliceSegment{}
|
||||
for _, p := range previous {
|
||||
old[p.ID] = p
|
||||
}
|
||||
unconfirmed := mapValue(item.Subject.Payload["unconfirmedSliceNames"])
|
||||
if unconfirmed == nil {
|
||||
unconfirmed = map[string]any{}
|
||||
}
|
||||
var allowed map[string]domain.AnnotationLabelRecord
|
||||
for i := range slices {
|
||||
c := &slices[i]
|
||||
p, exists := old[c.ID]
|
||||
if mode == "restore" && c.Dictionary != nil {
|
||||
if saved := savedDictionarySelection(item, c.Dictionary, "slice"); saved != nil && c.Name == saved.Name {
|
||||
c.Dictionary = saved
|
||||
delete(unconfirmed, c.ID)
|
||||
continue
|
||||
}
|
||||
}
|
||||
if c.IsDiscarded() {
|
||||
c.Dictionary = nil
|
||||
delete(unconfirmed, c.ID)
|
||||
continue
|
||||
}
|
||||
if exists && p.Name == c.Name && (c.Dictionary == nil || sameDictionaryVersion(p.Dictionary, c.Dictionary)) && (mode != "confirm" || p.Dictionary != nil) {
|
||||
unchanged := exists && p.Name == c.Name && (c.Dictionary == nil || sameDictionaryVersion(p.Dictionary, c.Dictionary))
|
||||
finalizing := mode == "confirm" || mode == "submit"
|
||||
pendingName := p.Dictionary == nil && c.Name != "" && stringValue(unconfirmed[c.ID]) == c.Name
|
||||
needsNameCheck := (mode == "confirm" && p.Dictionary == nil) || (mode == "submit" && strings.TrimSpace(c.Name) == "") || (finalizing && pendingName)
|
||||
if unchanged && !needsNameCheck {
|
||||
c.Dictionary = p.Dictionary
|
||||
continue
|
||||
}
|
||||
|
||||
if (mode == "restore" && c.Dictionary == nil) || (mode == "draft" && strings.TrimSpace(c.Name) == "") {
|
||||
c.Dictionary = nil
|
||||
if c.Name != "" {
|
||||
unconfirmed[c.ID] = c.Name
|
||||
}
|
||||
continue
|
||||
}
|
||||
if allowed == nil {
|
||||
@ -68,7 +96,15 @@ func (s *server) validateProjectSliceNames(ctx context.Context, item *domain.Hum
|
||||
if chosen == nil {
|
||||
return []validationIssue{{Field: fmt.Sprintf("sliceSegments.%d.name", i), Code: "slice_dictionary_name_required", Message: "请从本项目切片字典选择名称;词条已更新时请重新选择"}}
|
||||
}
|
||||
c.Dictionary = chosen
|
||||
if !(exists && p.Name == c.Name && p.Dictionary == nil && c.Dictionary == nil && stringValue(unconfirmed[c.ID]) != c.Name) {
|
||||
c.Dictionary = chosen
|
||||
}
|
||||
delete(unconfirmed, c.ID)
|
||||
}
|
||||
return nil
|
||||
if len(unconfirmed) > 0 {
|
||||
item.Subject.Payload["unconfirmedSliceNames"] = unconfirmed
|
||||
} else {
|
||||
delete(item.Subject.Payload, "unconfirmedSliceNames")
|
||||
}
|
||||
return rememberDictionarySelections(item, sliceDictionarySelections(slices)...)
|
||||
}
|
||||
|
||||
@ -346,6 +346,7 @@ func restoredDiscardAnnotations(slice domain.SliceSegment) []domain.AnnotationSe
|
||||
|
||||
type sliceDispositionInput struct {
|
||||
combinedAnnotationDraftInput
|
||||
RestoreClassifications bool `json:"restoreClassifications,omitempty"`
|
||||
SliceID string `json:"sliceId"`
|
||||
Disposition string `json:"disposition"`
|
||||
Reason string `json:"reason"`
|
||||
@ -358,6 +359,13 @@ type sliceDispositionInput struct {
|
||||
}
|
||||
|
||||
func sliceNameBeforeDiscard(item *domain.HumanItemRecord, sliceID string, version int) (string, bool) {
|
||||
prior := sliceBeforeDiscard(item, sliceID, version)
|
||||
if prior == nil {
|
||||
return "", false
|
||||
}
|
||||
return prior.Name, true
|
||||
}
|
||||
func sliceBeforeDiscard(item *domain.HumanItemRecord, sliceID string, version int) *domain.SliceSegment {
|
||||
history, _ := normalizeStructuredValue(item.Subject.Payload["sliceDispositionHistory"]).([]any)
|
||||
resets, _ := normalizeStructuredValue(item.Subject.Payload["sliceResetHistory"]).([]any)
|
||||
resetVersion := 0
|
||||
@ -376,10 +384,11 @@ func sliceNameBeforeDiscard(item *domain.HumanItemRecord, sliceID string, versio
|
||||
before := decodeMaterializationValue[domain.SliceSegment]([]any{entry["before"]})
|
||||
after := decodeMaterializationValue[domain.SliceSegment]([]any{entry["after"]})
|
||||
if len(before) == 1 && len(after) == 1 && !before[0].IsDiscarded() && after[0].IsDiscarded() {
|
||||
return before[0].Name, true
|
||||
copy := before[0]
|
||||
return ©
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *server) handleSliceDisposition(w http.ResponseWriter, r *http.Request) {
|
||||
@ -469,6 +478,7 @@ func (s *server) prepareSliceDisposition(w http.ResponseWriter, r *http.Request,
|
||||
}
|
||||
|
||||
slices, issues := s.canonicalSliceDispositionState(r.Context(), item, body.normalizedSliceSegments(), body.normalizedOperations())
|
||||
issues = append(issues, s.validateProjectSliceNames(r.Context(), item, slices, "restore")...)
|
||||
if task.SourceIsFullVideo {
|
||||
timeline, e := s.annotationFrameTimelineForTask(r.Context(), *task, "left")
|
||||
if e != nil {
|
||||
@ -481,7 +491,7 @@ func (s *server) prepareSliceDisposition(w http.ResponseWriter, r *http.Request,
|
||||
}
|
||||
issues = append(issues, validateSliceSegments(slices, *task)...)
|
||||
issues = append(issues, validateCompleteSliceCoverage(slices, *task)...)
|
||||
issues = append(issues, s.canonicalActionDictionaries(r.Context(), item, body.Segments)...)
|
||||
issues = append(issues, s.canonicalActionDictionaries(r.Context(), item, body.Segments, dispositionDictionaryMode(body))...)
|
||||
issues = append(issues, validateDraftCombinedAnnotationEvents(body.Segments, *task)...)
|
||||
idx := -1
|
||||
for i, v := range slices {
|
||||
@ -538,6 +548,7 @@ func (s *server) prepareSliceDisposition(w http.ResponseWriter, r *http.Request,
|
||||
if body.Disposition == "discard" {
|
||||
next.DeliveryDisposition = "discard"
|
||||
next.Name, next.TaskLabelID, next.TaskLabel = "", "", ""
|
||||
next.Dictionary = nil
|
||||
next.DiscardReason = body.Reason
|
||||
next.DiscardReasonLabelID = ""
|
||||
next.DiscardReasonVersion = 0
|
||||
@ -548,6 +559,9 @@ func (s *server) prepareSliceDisposition(w http.ResponseWriter, r *http.Request,
|
||||
next.DiscardReasonVersion = reasonLabel.Version
|
||||
next.DiscardReasonName = reasonLabel.Name
|
||||
next.DiscardReasonDictionary = reasonLabel.Selection()
|
||||
if body.RestoreClassifications {
|
||||
next.DiscardReasonDictionary = savedDictionarySelection(item, &domain.DictionarySelection{ID: reasonLabel.ID, Version: reasonLabel.Version}, "discard_reason")
|
||||
}
|
||||
}
|
||||
next.DiscardReasonNote = body.ReasonNote
|
||||
next.DiscardedByUserID = actorID
|
||||
@ -626,6 +640,12 @@ func (s *server) prepareSliceDisposition(w http.ResponseWriter, r *http.Request,
|
||||
// Precise undo targets a server-recorded discard, even after later
|
||||
// undo/redo writes. Ordinary restore deliberately remains unnamed.
|
||||
next.Name = restoredName
|
||||
next.Dictionary = nil
|
||||
if body.RestorePreviousName || body.RestoreNameFromVersion != 0 {
|
||||
if prior := sliceBeforeDiscard(item, before.ID, body.RestoreNameFromVersion); prior != nil {
|
||||
next.Dictionary = prior.Dictionary
|
||||
}
|
||||
}
|
||||
}
|
||||
next.DiscardReason = ""
|
||||
next.DiscardReasonLabelID = ""
|
||||
@ -643,13 +663,20 @@ func (s *server) prepareSliceDisposition(w http.ResponseWriter, r *http.Request,
|
||||
next = slices[idx]
|
||||
// Disposition is an incremental edit, not confirmation of the whole slicing
|
||||
// plan. Other slices may still have draft names; confirmation validates them.
|
||||
issues = validateDraftCombinedAnnotationEvents(segments, *task)
|
||||
issues = s.validateProjectSliceNames(r.Context(), item, slices, "restore")
|
||||
issues = append(issues, s.canonicalActionDictionaries(r.Context(), item, segments, "restore")...)
|
||||
issues = append(issues, validateDraftCombinedAnnotationEvents(segments, *task)...)
|
||||
|
||||
if len(issues) > 0 {
|
||||
renderValidationError(w, describeSliceNameIssues(issues, slices)...)
|
||||
return nil, domain.SliceSegment{}, false
|
||||
}
|
||||
statuses := body.normalizedStatuses()
|
||||
statuses[next.ID] = domain.SliceAnnotationPending
|
||||
if issues := rememberDictionarySelections(item, sliceDictionarySelections(slices)...); len(issues) > 0 {
|
||||
renderValidationError(w, issues...)
|
||||
return nil, domain.SliceSegment{}, false
|
||||
}
|
||||
workspace := map[string]any{"sliceSegments": slices, "segments": segments, "statuses": statuses, "operations": body.normalizedOperations(), "userId": actorID, "savedAt": nowISO()}
|
||||
if item.Subject.Payload == nil {
|
||||
item.Subject.Payload = map[string]any{}
|
||||
@ -670,3 +697,10 @@ func (s *server) prepareSliceDisposition(w http.ResponseWriter, r *http.Request,
|
||||
}
|
||||
return workspace, next, true
|
||||
}
|
||||
|
||||
func dispositionDictionaryMode(body sliceDispositionInput) string {
|
||||
if body.RestoreClassifications {
|
||||
return "restore"
|
||||
}
|
||||
return "draft"
|
||||
}
|
||||
|
||||
@ -82,11 +82,14 @@ func TestSliceDiscardMaterialization60Seconds(t *testing.T) {
|
||||
for i := 0; i < 3; i++ {
|
||||
start, end := float64(i*20), float64((i+1)*20)
|
||||
sl := domain.SliceSegment{ID: fmt.Sprintf("segment-%d", i), Name: fmt.Sprintf("片段 %d", i+1), Start: start, End: end}
|
||||
sl.Dictionary = &domain.DictionarySelection{ID: fmt.Sprintf("base-%d", i), Kind: "slice", Version: 2, Code: fmt.Sprintf("class-%d", i), Name: sl.Name, Tags: []string{"基础分类", "商品"}}
|
||||
if mode == "discard" || (mode == "mixed" && i == 1) {
|
||||
sl.DeliveryDisposition = "discard"
|
||||
sl.DiscardReason = "审查测试:画面被遮挡"
|
||||
sl.DiscardReasonLabelID = "reason-test"
|
||||
sl.DiscardReasonVersion = 3
|
||||
sl.Dictionary = nil
|
||||
sl.DiscardReasonDictionary = &domain.DictionarySelection{ID: "reason-test", Kind: "discard_reason", Version: 3, Code: "occluded", Name: "审查测试", Tags: []string{"不可用"}}
|
||||
sl.DiscardReasonName = "审查测试"
|
||||
sl.DiscardReasonNote = "画面被遮挡"
|
||||
sl.DiscardedAt = "2026-09-16T00:00:00Z"
|
||||
@ -94,7 +97,7 @@ func TestSliceDiscardMaterialization60Seconds(t *testing.T) {
|
||||
} else if mode == "partial" && i == 0 {
|
||||
segments = append(segments, domain.AnnotationSegment{ID: "partial", Start: 5, End: 10, Description: "仅描述这五秒", Source: "human"})
|
||||
} else if mode != "blank" && mode != "partial" {
|
||||
segments = append(segments, domain.AnnotationSegment{ID: fmt.Sprintf("action-%d", i), Start: start, End: end, Description: "测试动作", Source: "human"})
|
||||
segments = append(segments, domain.AnnotationSegment{ID: fmt.Sprintf("action-%d", i), Start: start, End: end, Description: "测试动作", Dictionary: &domain.DictionarySelection{ID: "action-test", Kind: "action", Version: 4, Code: "pick", Name: "拿取", Tags: []string{"抓取"}}, Source: "human"})
|
||||
}
|
||||
slices = append(slices, sl)
|
||||
}
|
||||
|
||||
@ -9,8 +9,9 @@ import (
|
||||
|
||||
type sliceEditApplicationInput struct {
|
||||
combinedAnnotationDraftInput
|
||||
ApplicationID string `json:"applicationId"`
|
||||
Dispositions []sliceDispositionInput `json:"dispositions"`
|
||||
RestoreClassifications bool `json:"restoreClassifications,omitempty"`
|
||||
ApplicationID string `json:"applicationId"`
|
||||
Dispositions []sliceDispositionInput `json:"dispositions"`
|
||||
}
|
||||
|
||||
// A panel application is one CAS, including explicit disposition commands and
|
||||
@ -71,6 +72,7 @@ func (s *server) handleSliceEditApplication(w http.ResponseWriter, r *http.Reque
|
||||
}
|
||||
commands := map[string]sliceDispositionInput{}
|
||||
for _, command := range body.Dispositions {
|
||||
command.RestoreClassifications = body.RestoreClassifications
|
||||
if command.SliceID == "" || commands[command.SliceID].SliceID != "" || (command.Disposition != "keep" && command.Disposition != "discard") {
|
||||
renderValidationError(w, validationIssue{Field: "dispositions", Message: "切片用途操作无效或重复"})
|
||||
return
|
||||
@ -81,6 +83,7 @@ func (s *server) handleSliceEditApplication(w http.ResponseWriter, r *http.Reque
|
||||
// Undo can first restore a discarded child before rejoining it. The restore
|
||||
// remains an explicit, audited command in the same atomic application.
|
||||
for _, command := range body.Dispositions {
|
||||
command.RestoreClassifications = body.RestoreClassifications
|
||||
if !ids[command.SliceID] {
|
||||
if command.Disposition != "keep" {
|
||||
renderValidationError(w, validationIssue{Field: "dispositions", Message: "舍弃操作必须保留目标切片"})
|
||||
@ -143,10 +146,15 @@ func (s *server) handleSliceEditApplication(w http.ResponseWriter, r *http.Reque
|
||||
}
|
||||
}
|
||||
baseline[i] = sliceEditPolicyBaseline(candidate, old)
|
||||
if command := commands[candidate.ID]; command.Disposition == "discard" && !old.IsDiscarded() {
|
||||
baseline[i].Name = old.Name
|
||||
baseline[i].Dictionary = old.Dictionary
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
slices, issues := s.canonicalSliceDispositionState(r.Context(), &item, baseline, body.normalizedOperations())
|
||||
issues = append(issues, s.validateProjectSliceNames(r.Context(), stored, slices, applicationDictionaryMode(body))...)
|
||||
issues = append(issues, s.validateSliceEditFrameBounds(r, task, &item, slices, body.Segments)...)
|
||||
issues = append(issues, validateSliceSegments(slices, *task)...)
|
||||
issues = append(issues, validateCompleteSliceCoverage(slices, *task)...)
|
||||
@ -194,10 +202,16 @@ func (s *server) handleSliceEditApplication(w http.ResponseWriter, r *http.Reque
|
||||
segmentsForApplication = retained
|
||||
}
|
||||
}
|
||||
if issues := s.canonicalActionDictionaries(r.Context(), stored, segmentsForApplication, applicationDictionaryMode(body)); len(issues) > 0 {
|
||||
renderValidationError(w, issues...)
|
||||
return
|
||||
}
|
||||
copyClassificationRecoveryState(&item, stored)
|
||||
workspace := map[string]any{"sliceSegments": slices, "segments": segmentsForApplication, "statuses": body.normalizedStatuses(), "operations": body.normalizedOperations(), "userId": principal.User.ID, "savedAt": nowISO()}
|
||||
item.Subject.Payload["annotationWorkspace"] = workspace
|
||||
delete(item.Subject.Payload, "annotationDraft")
|
||||
for _, command := range body.Dispositions {
|
||||
command.RestoreClassifications = body.RestoreClassifications
|
||||
if ids[command.SliceID] {
|
||||
command.combinedAnnotationDraftInput = workspaceSliceEditInput(workspace)
|
||||
var prepared bool
|
||||
@ -243,8 +257,9 @@ func (s *server) handleSliceEditApplication(w http.ResponseWriter, r *http.Reque
|
||||
}
|
||||
}
|
||||
}
|
||||
issues = s.validateProjectSliceNames(r.Context(), &item, slices, "draft")
|
||||
issues = append(issues, s.canonicalActionDictionaries(r.Context(), stored, segments)...)
|
||||
issues = s.validateProjectSliceNames(r.Context(), stored, slices, applicationDictionaryMode(body))
|
||||
issues = append(issues, s.canonicalActionDictionaries(r.Context(), stored, segments, applicationDictionaryMode(body))...)
|
||||
copyClassificationRecoveryState(&item, stored)
|
||||
issues = append(issues, s.validateSliceEditFrameBounds(r, task, &item, slices, segments)...)
|
||||
issues = append(issues, validateDraftCombinedAnnotationEvents(segments, *task)...)
|
||||
issues = append(issues, validateDraftFanyuV5CombinedSemanticSegments(segments, slices, annotationTemplateWithTaskSnapshot(template, task))...)
|
||||
@ -267,6 +282,7 @@ func (s *server) handleSliceEditApplication(w http.ResponseWriter, r *http.Reque
|
||||
item.Subject.Payload["annotationWorkspace"] = workspace
|
||||
// Recheck dictionary versions immediately before the single commit.
|
||||
for _, command := range body.Dispositions {
|
||||
command.RestoreClassifications = body.RestoreClassifications
|
||||
if command.Disposition == "discard" {
|
||||
if _, issues := s.resolveDiscardReason(r.Context(), &item, command); len(issues) > 0 {
|
||||
renderValidationError(w, issues...)
|
||||
@ -347,3 +363,10 @@ func (s *server) validateSliceEditFrameBounds(r *http.Request, task *domain.Anno
|
||||
}
|
||||
return issues
|
||||
}
|
||||
|
||||
func applicationDictionaryMode(body sliceEditApplicationInput) string {
|
||||
if body.RestoreClassifications {
|
||||
return "restore"
|
||||
}
|
||||
return "draft"
|
||||
}
|
||||
|
||||
@ -123,7 +123,7 @@ func (s *server) handleSliceEditRestoration(w http.ResponseWriter, r *http.Reque
|
||||
issues = append(issues, validateDraftCombinedAnnotationEvents(body.Segments, *task)...)
|
||||
issues = append(issues, validateDraftStatuses(body.normalizedStatuses(), slices)...)
|
||||
issues = append(issues, s.validateProjectSliceNames(r.Context(), item, slices, "restore")...)
|
||||
issues = append(issues, s.canonicalActionDictionaries(r.Context(), item, body.Segments)...)
|
||||
issues = append(issues, s.canonicalActionDictionaries(r.Context(), item, body.Segments, "restore")...)
|
||||
template, err := s.store.GetAnnotationTemplate(r.Context(), task.TemplateID)
|
||||
if err != nil {
|
||||
renderError(w, 500, err)
|
||||
|
||||
@ -135,7 +135,7 @@ func checkPart(p part) (Summary, error) {
|
||||
if (p.Schema != PartSchema && p.Schema != ClassificationPartSchema) || !safeName(p.Capture.ID) || !partName.MatchString(p.PartID) || p.Revision < 1 || !p.Acceptance.Final.Confirmed || len(p.Acceptance.Reviews) != 0 {
|
||||
return s, fmt.Errorf("part must pin an accepted native v3 revision without embedded review history")
|
||||
}
|
||||
if _, e := time.Parse(time.RFC3339Nano, p.Acceptance.Final.AcceptedAt); e != nil {
|
||||
if _, e := time.Parse(time.RFC3339Nano, p.Acceptance.Final.AcceptedAt); e != nil && p.Schema != ClassificationPartSchema {
|
||||
return s, fmt.Errorf("missing acceptance timestamp")
|
||||
}
|
||||
if p.SourceMetadata.Path != "meta_"+p.Capture.ID+".json" || path.Base(p.Capture.Prefix) != p.Capture.ID || path.Clean(p.Capture.Prefix) != p.Capture.Prefix || strings.HasPrefix(p.Capture.Prefix, "/") || strings.Contains(p.Capture.Prefix, "../") {
|
||||
@ -427,8 +427,10 @@ type classificationHistory struct {
|
||||
PartSHA string `json:"partSha256"`
|
||||
Acceptance struct {
|
||||
Final struct {
|
||||
Confirmed bool `json:"confirmed"`
|
||||
AcceptedAt string `json:"acceptedAt"`
|
||||
Confirmed bool `json:"confirmed"`
|
||||
AcceptedAt string `json:"acceptedAt"`
|
||||
Sampled *bool `json:"sampled"`
|
||||
SamplingDecision string `json:"samplingDecision"`
|
||||
} `json:"finalAcceptance"`
|
||||
} `json:"acceptance"`
|
||||
}
|
||||
@ -449,7 +451,11 @@ func classificationAudit(src Source) (classificationHistory, error) {
|
||||
return audit, fmt.Errorf("internal audit does not pin the accepted classification")
|
||||
}
|
||||
if _, err := time.Parse(time.RFC3339Nano, audit.Acceptance.Final.AcceptedAt); err != nil {
|
||||
return audit, fmt.Errorf("missing internal acceptance timestamp")
|
||||
final := audit.Acceptance.Final
|
||||
exempt := final.AcceptedAt == "" && final.Sampled != nil && !*final.Sampled && final.SamplingDecision == "not_selected"
|
||||
if !exempt {
|
||||
return audit, fmt.Errorf("missing internal acceptance timestamp or explicit sampling exemption")
|
||||
}
|
||||
}
|
||||
return audit, nil
|
||||
}
|
||||
|
||||
@ -54,3 +54,23 @@ func TestClassificationBatchRejectsMixedVersions(t *testing.T) {
|
||||
t.Fatal("mixed versions accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassificationBatchAcceptsExplicitSamplingExemptionWithoutInventingHumanDate(t *testing.T) {
|
||||
src := classificationFixture(false)
|
||||
var audit map[string]any
|
||||
json.Unmarshal(src.History, &audit)
|
||||
final := audit["acceptance"].(map[string]any)["finalAcceptance"].(map[string]any)
|
||||
delete(final, "acceptedAt")
|
||||
final["sampled"] = false
|
||||
final["samplingDecision"] = "not_selected"
|
||||
src.History = encode(audit)
|
||||
out := build(t, src)
|
||||
if bytes.Contains(out.Index, []byte("acceptedAt")) {
|
||||
t.Fatal("invented human acceptance date")
|
||||
}
|
||||
final["samplingDecision"] = "selected"
|
||||
src.History = encode(audit)
|
||||
if _, err := Build("project-A", "项目A", "delivery/项目A__project-A", []Source{src}); err == nil {
|
||||
t.Fatal("unverified human acceptance accepted")
|
||||
}
|
||||
}
|
||||
|
||||
@ -324,7 +324,19 @@ func (runtime *Runtime) normalizeOutputArtifacts(ctx context.Context, workflow *
|
||||
// the exact already-uploaded object refs instead of copying every byte a
|
||||
// second time.
|
||||
sharedDirectoryFileRefs := map[string]domain.ArtifactReference{}
|
||||
order := make([]int, 0, len(artifacts))
|
||||
for index, artifact := range artifacts {
|
||||
if domain.IsInternalDeliveryAudit(artifact) {
|
||||
order = append(order, index)
|
||||
}
|
||||
}
|
||||
for index, artifact := range artifacts {
|
||||
if !domain.IsInternalDeliveryAudit(artifact) {
|
||||
order = append(order, index)
|
||||
}
|
||||
}
|
||||
for _, index := range order {
|
||||
artifact := artifacts[index]
|
||||
result[index] = artifact
|
||||
if artifact.Payload != nil && artifact.Payload.Mode == domain.ArtifactTransferModeRef {
|
||||
if err := domain.ValidateArtifactPayload(*artifact.Payload, artifact.Type); err != nil {
|
||||
|
||||
@ -93,6 +93,9 @@ func (runtime *Runtime) classificationDeliveryEnabled(ctx context.Context, workf
|
||||
// inline transport limit with a multi-round audit. Customer publication is a
|
||||
// different path and never sees this source artifact.
|
||||
func (runtime *Runtime) persistClassificationSummary(ctx context.Context, run *domain.RunRecord, node domain.WorkflowNode, partitionID string, artifact domain.ExecutionArtifact, body []byte) (domain.ExecutionArtifact, error) {
|
||||
if len(body) > 8<<20 {
|
||||
return artifact, fmt.Errorf("acceptance audit summary exceeds 8 MiB")
|
||||
}
|
||||
if len(body) <= domain.MaxInlinePayloadBytes && !structuredJSONRequiresReference(artifact, artifact.Data) {
|
||||
return artifact, nil
|
||||
}
|
||||
|
||||
@ -3,6 +3,8 @@ package worker
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@ -71,3 +73,26 @@ func TestClassificationAuditTraceRetainsFormalReworkAndScopedEvidence(t *testing
|
||||
t.Fatal("compacted audit no longer excluded from final export")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInternalAuditFailurePreventsCustomerDirectoryPublication(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ds := store.NewMemoryStore()
|
||||
p := &deliveryPublisher{}
|
||||
runtime := &Runtime{store: ds, artifactPublisher: p}
|
||||
connection := domain.StorageConnectionRecord{ID: "audit-store", OrganizationID: "org", Provider: domain.StorageProviderBOS, Bucket: "b"}
|
||||
ds.UpsertStorageConnection(ctx, connection)
|
||||
workflow := &domain.WorkflowRecord{ID: "wf", OrganizationID: "org", DefinitionState: domain.WorkflowDefinitionStateReleased, ArtifactStorage: &domain.ArtifactStoragePolicy{ConnectionID: connection.ID, Bucket: connection.Bucket, PrefixTemplate: "artifacts/{runId}/{nodeId}"}}
|
||||
run := &domain.RunRecord{ID: "run-audit-first", OrganizationID: "org", WorkflowID: "wf"}
|
||||
ds.UpsertCaptureRecord(ctx, domain.CaptureRecord{ID: "capture", OrganizationID: "org", RunID: run.ID, Delivery: &domain.CaptureDeliveryTarget{ConnectionID: connection.ID, Bucket: connection.Bucket, Prefix: "deliveries/项目__project/capture/part0001"}})
|
||||
directory := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(directory, "part.json"), []byte(`{"classification":"synthetic"}`), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
artifacts := []domain.ExecutionArtifact{{ID: "customer", RunID: run.ID, Name: "delivery.directory.index", Type: domain.PortTypeFile, Path: directory}, {ID: "audit", RunID: run.ID, PortID: "deliveryAudit", Type: domain.PortTypeJSON, Path: filepath.Join(directory, "missing-internal-audit.json"), Data: map[string]any{"schemaVersion": "droboticflow.delivery.review-history/v2"}}}
|
||||
if _, err := runtime.normalizeOutputArtifacts(ctx, workflow, run, "package", artifacts); err == nil {
|
||||
t.Fatal("missing audit accepted")
|
||||
}
|
||||
if len(p.published) != 0 || len(p.puts) != 0 {
|
||||
t.Fatal("customer directory became visible before audit persisted")
|
||||
}
|
||||
}
|
||||
|
||||
@ -846,7 +846,10 @@ def _description_status(actions: list[dict[str, Any]]) -> str:
|
||||
|
||||
|
||||
def _emit_part_index_directory(run_id, node_id, output, node_data, helpers, payload, internal_audit=None):
|
||||
"""Use the established immutable directory publisher for the two JSON files.
|
||||
"""Use the established immutable publisher for versioned customer JSON.
|
||||
|
||||
V3 retains its historical sibling review file; v4 stores that audit in a
|
||||
separate internal artifact before publishing the customer directory.
|
||||
|
||||
Legacy single-file publishers deliberately accept only v1/v2. A directory
|
||||
artifact preserves their ownership/conditional-publication protocol without
|
||||
@ -927,9 +930,9 @@ def _emit_part_index_directory(run_id, node_id, output, node_data, helpers, payl
|
||||
action.pop(key, None)
|
||||
if action.get("dictionary") is None: action.pop("dictionary", None)
|
||||
body = (json.dumps(payload, ensure_ascii=False, sort_keys=True, indent=2) + "\n").encode("utf8")
|
||||
if len(json.dumps(audit, ensure_ascii=False).encode("utf8")) > 8 * 1024 * 1024:
|
||||
raise RuntimeError("internal delivery audit exceeds 8 MiB; refusing incomplete export")
|
||||
audit["partSha256"] = hashlib.sha256(body).hexdigest()
|
||||
if len(json.dumps(audit, ensure_ascii=False, indent=2).encode("utf8")) > 8 * 1024 * 1024:
|
||||
raise RuntimeError("internal delivery audit exceeds 8 MiB; refusing incomplete export")
|
||||
staging = pathlib.Path(tempfile.mkdtemp(prefix="part-index-v4-", dir=output))
|
||||
(staging / "part.json").write_bytes(body)
|
||||
# A sibling artifact stays in the internal run, never in the customer tree.
|
||||
@ -1729,7 +1732,11 @@ def _run_accepted_slice_directory(
|
||||
discarded_slices.append(row)
|
||||
else:
|
||||
kept_slices.append(row)
|
||||
if disposition_contract:
|
||||
params = helpers["hyperparams"](node_data) if callable(helpers.get("hyperparams")) else {}
|
||||
classification_v4 = _bool(params, "DELIVERY_JSON_V4", False)
|
||||
if classification_v4 and not coverage_required:
|
||||
raise RuntimeError("DELIVERY_JSON_V4 requires verified full-video coverage")
|
||||
if disposition_contract or classification_v4:
|
||||
pins = summary_payload.get("acceptedMaterializations", [])
|
||||
expected_sha = _sha256_file(revision_manifest_path)
|
||||
if summary_payload.get("decision") != "approved" or final_acceptance.get("confirmed") is not True or not any(
|
||||
@ -1739,7 +1746,6 @@ def _run_accepted_slice_directory(
|
||||
raise RuntimeError("slice dispositions are not pinned to the accepted manifest revision")
|
||||
if int(coverage.get("sliceCount", -1)) != len(all_slices):
|
||||
raise RuntimeError("review coverage slice count mismatch")
|
||||
params = helpers["hyperparams"](node_data) if callable(helpers.get("hyperparams")) else {}
|
||||
if disposition_contract and not kept_slices and not _bool(params, "DELIVERY_JSON_V4", False):
|
||||
if any(p.get("key") == "PROCESSING_PROFILE" and p.get("value") == "raw_human" for p in node_data.get("hyperparams", [])) and (summary_payload.get("processingProfile") != "raw_human" or final_acceptance.get("processingProfile") != "raw_human"):
|
||||
raise RuntimeError("raw-human package requires matching accepted task profile")
|
||||
@ -1758,7 +1764,6 @@ def _run_accepted_slice_directory(
|
||||
payloads = _payload_tree(_payloads_with_full_pose_manifests(inputs, output, helpers))
|
||||
headpose = _first_payload(payloads, "droboticflow.headpose/v1")
|
||||
handpose = _first_payload(payloads, "droboticflow.wilor.hand_pose_result/v1")
|
||||
params = helpers["hyperparams"](node_data) if callable(helpers.get("hyperparams")) else {}
|
||||
if _bool(params, "PART_INDEX_DELIVERY", False):
|
||||
if source_index and not _bool(params, "SOURCE_INDEX_DELIVERY", False):
|
||||
raise RuntimeError("source-index JSON delivery requires explicit SOURCE_INDEX_DELIVERY approval; use directory delivery to preserve physical discard exclusion")
|
||||
|
||||
@ -579,9 +579,7 @@ export function AnnotationLabelsPanel({
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<p className="text-sm text-slate-500">
|
||||
支持导出 CSV、批量导入与勾选批量删除;编辑或删除只影响本项目的可选词条,不影响其他项目及已保存的标注、审核记录。
|
||||
</p>
|
||||
<p className="text-sm text-slate-500">名称增删只影响本项目;标签随字典词条共享,已保存的分类保持原版本。</p>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
@ -155,6 +155,9 @@ type Props = ReviewViewingProps & {
|
||||
slices: SliceSegment[];
|
||||
segments: AnnotationSegment[];
|
||||
labels?: SemanticLabelDefinition[];
|
||||
actionDictionaryLoading?: boolean;
|
||||
actionDictionaryError?: string;
|
||||
onRefreshActionDictionary?: () => void;
|
||||
discardReasons?: AnnotationLabelRecord[];
|
||||
discardReasonsLoading?: boolean;
|
||||
discardReasonsAllowManual?: boolean;
|
||||
@ -342,6 +345,9 @@ export function CaptureAnnotationWorkbench({
|
||||
slices,
|
||||
segments: providedSegments,
|
||||
labels = [],
|
||||
actionDictionaryLoading = false,
|
||||
actionDictionaryError = "",
|
||||
onRefreshActionDictionary,
|
||||
discardReasons = [],
|
||||
discardReasonsLoading = false,
|
||||
discardReasonsAllowManual = false,
|
||||
@ -1988,6 +1994,19 @@ export function CaptureAnnotationWorkbench({
|
||||
setEditingAnnotation((current) => (current ? { ...current, ...patch } : current));
|
||||
};
|
||||
|
||||
const actionDictionaryStatus = actionDictionaryLoading ? (
|
||||
<p role="status">正在加载动作字典…</p>
|
||||
) : actionDictionaryError ? (
|
||||
<div role="alert">
|
||||
{userErrorMessage(actionDictionaryError)}
|
||||
<button type="button" onClick={onRefreshActionDictionary}>
|
||||
重试加载
|
||||
</button>
|
||||
</div>
|
||||
) : !labels.length ? (
|
||||
<p>暂无字典词条,可直接填写描述。</p>
|
||||
) : undefined;
|
||||
|
||||
const chooseTemplate = (definition: SemanticLabelDefinition) => {
|
||||
updateEditingAnnotation({
|
||||
labelId: "",
|
||||
@ -5701,7 +5720,7 @@ export function CaptureAnnotationWorkbench({
|
||||
text: item.description || item.name,
|
||||
searchText: `${item.name} ${item.description || ""}`,
|
||||
}))}
|
||||
status={!labels.length ? <p>暂无字典词条,可直接填写描述。</p> : undefined}
|
||||
status={actionDictionaryStatus}
|
||||
onSelect={(id) => {
|
||||
const item = labels.find((item) => item.id === id);
|
||||
if (item) chooseTemplate(item);
|
||||
@ -5978,7 +5997,10 @@ export function CaptureAnnotationWorkbench({
|
||||
disabled={isInvalid || saving}
|
||||
title="从字典选择(F / D,填写文本时不触发)"
|
||||
aria-keyshortcuts="F D"
|
||||
onClick={() => setShowDictionary((value) => !value)}
|
||||
onClick={() => {
|
||||
if (!showDictionary) onRefreshActionDictionary?.();
|
||||
setShowDictionary((value) => !value);
|
||||
}}
|
||||
className="inline-flex items-center gap-1.5 rounded border border-slate-300 px-2 py-1 text-xs text-slate-700 dark:border-slate-600 dark:text-gray-300 disabled:opacity-40 disabled:cursor-not-allowed"
|
||||
>
|
||||
<BookOpen className="h-3.5 w-3.5" />
|
||||
@ -6003,11 +6025,7 @@ export function CaptureAnnotationWorkbench({
|
||||
<p className="px-2 py-1 text-[10px] text-slate-500">
|
||||
↑ / ↓ 选择 · Enter 填入 · Esc 关闭
|
||||
</p>
|
||||
{!labels.length && (
|
||||
<p className="p-2 text-xs text-slate-500">
|
||||
暂无字典词条,可直接填写描述。
|
||||
</p>
|
||||
)}
|
||||
{actionDictionaryStatus}
|
||||
{labels.map((item) => (
|
||||
<button
|
||||
data-dictionary-option
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
const dictionaryKey = (value) => value ? JSON.stringify([value.id,value.kind,value.version,value.code,value.name,value.tags || []]) : "";
|
||||
|
||||
// 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) {
|
||||
@ -12,6 +14,8 @@ export function invalidSliceConsolidationCandidates(slices, actions) {
|
||||
!current.name?.trim() ||
|
||||
left.name !== current.name ||
|
||||
right.name !== current.name ||
|
||||
dictionaryKey(left.dictionary) !== dictionaryKey(current.dictionary) ||
|
||||
dictionaryKey(right.dictionary) !== dictionaryKey(current.dictionary) ||
|
||||
left.end !== current.start ||
|
||||
current.end !== right.start
|
||||
)
|
||||
|
||||
@ -3669,6 +3669,7 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
|
||||
...(recordHistory && disposition === "keep" ? { resetToPending: true } : {}),
|
||||
restorePreviousName,
|
||||
...(restorePreviousName && restoreNameFromVersion ? { restoreNameFromVersion } : {}),
|
||||
restoreClassifications: !recordHistory,
|
||||
...reason,
|
||||
sliceSegments,
|
||||
segments,
|
||||
@ -3756,6 +3757,7 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
|
||||
const payload = {
|
||||
version: token.version,
|
||||
applicationId,
|
||||
restoreClassifications: !recordHistory,
|
||||
sliceSegments: nextSlices,
|
||||
segments: nextSegments,
|
||||
operations: restoredOperations,
|
||||
@ -7530,6 +7532,9 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
|
||||
slices={sliceSegments}
|
||||
segments={segments}
|
||||
labels={selectedLabels}
|
||||
actionDictionaryLoading={actionDictionary.loading}
|
||||
actionDictionaryError={actionDictionary.error}
|
||||
onRefreshActionDictionary={actionDictionary.refresh}
|
||||
sliceDictionary={sliceDictionary.items}
|
||||
sliceDictionaryLoading={sliceDictionary.loading}
|
||||
sliceDictionaryError={userErrorMessage(sliceDictionary.error)}
|
||||
|
||||
@ -15,7 +15,7 @@ def export(destination):
|
||||
for part in ("part0001", "part0002"):
|
||||
work=destination/("work-"+part)
|
||||
work.mkdir()
|
||||
inputs=prepare(work,[{"id":"blank","description":"","clipStartMs":0,"clipEndMs":500},{"id":"pick","description":"拿起杯子","dictionary":ACTION,"clipStartMs":500,"clipEndMs":1000}])
|
||||
inputs=prepare(work,[{"id":"blank","description":"","clipStartMs":0,"clipEndMs":500,"startTimestampUs":1000000,"endTimestampUs":1500000},{"id":"pick","description":"拿起杯子","dictionary":ACTION,"clipStartMs":500,"clipEndMs":1000,"startTimestampUs":1500000,"endTimestampUs":2000000}])
|
||||
inputs[1]["data"]["capture"].update(captureId="capture-A/"+part,prefix="incoming/capture-A/"+part)
|
||||
manifest_path=Path(inputs[0]["path"])/"revision-manifest.json"
|
||||
m=json.loads(manifest_path.read_text());m["runId"]="source-run-"+part
|
||||
|
||||
46
tests/backend/test_delivery_v4_real_materialization.py
Normal file
46
tests/backend/test_delivery_v4_real_materialization.py
Normal file
@ -0,0 +1,46 @@
|
||||
"""Consume real Go materialization revisions, including metadata-only reuse."""
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from backend.operators.builtin.package import operator as package
|
||||
from tests.backend.test_part_index_real_materialization import load_context
|
||||
from tests.backend.test_part_index_package import helpers
|
||||
from tests.backend.test_delivery_v4_package import NODE
|
||||
|
||||
|
||||
@pytest.mark.parametrize("mode", ["keep", "mixed", "discard", "blank"])
|
||||
def test_v4_real_materialization_retains_final_dictionary_snapshots(tmp_path, mode):
|
||||
context=load_context()
|
||||
artifact=context[mode]["artifact"]
|
||||
root=Path(artifact["path"])
|
||||
mpath=root/"revision-manifest.json"
|
||||
manifest=json.loads(mpath.read_text())
|
||||
summary={"decision":"approved","processingProfile":"raw_human","capture":{"captureId":"real-capture/part0001","prefix":"incoming/real-capture/part0001"},"trace":[],"qualityReviews":[],"acceptedMaterializations":[{"artifactId":artifact["id"],"revision":manifest["revision"],"manifestSha256":hashlib.sha256(mpath.read_bytes()).hexdigest()}],"finalAcceptance":{"confirmed":True,"acceptedAt":"2026-10-21T00:00:00Z","processingProfile":"raw_human","confirmedByUserId":"internal-acceptor"}}
|
||||
inputs=[{**artifact,"portId":"acceptedSliceDirectory"},{"portId":"acceptanceSummary","data":summary}]
|
||||
for kind in ("camera","imu"):
|
||||
calibration=tmp_path/f"calibration_{kind}.yaml"
|
||||
calibration.write_text(f"{kind}: synthetic-calibration\n")
|
||||
inputs.append({"portId":"calibrations","name":calibration.name,"path":str(calibration)})
|
||||
# Also exercise a durable referenced acceptance summary (large trace transport).
|
||||
reference=tmp_path/"accepted.json"
|
||||
reference.write_text(json.dumps(summary))
|
||||
inputs[1]={"portId":"acceptanceSummary","path":str(reference),"data":{"schemaVersion":"droboticflow.acceptance-summary/v2"}}
|
||||
artifacts=package.run("real-package","package",tmp_path/"out",NODE,inputs,helpers())
|
||||
customer=Path(artifacts[0]["path"])
|
||||
assert {p.name for p in customer.iterdir()}=={"part.json"}
|
||||
part=json.loads((customer/"part.json").read_text())
|
||||
assert len(part["slices"])+len(part["discardedSlices"])==3
|
||||
for item in part["slices"]:
|
||||
snap=item["classification"]["dictionary"]
|
||||
assert snap["version"]==2 and snap["tags"]==["基础分类","商品"]
|
||||
if mode=="blank": assert item["classification"]["descriptionStatus"]=="empty_confirmed"
|
||||
for action in item["segments"]:
|
||||
assert action["dictionary"]["version"]==4 and action["dictionary"]["tags"]==["抓取"]
|
||||
for item in part["discardedSlices"]:
|
||||
reason=item["classification"]["discardEvidence"]
|
||||
assert reason["note"]=="画面被遮挡" and reason["dictionary"]["tags"]==["不可用"]
|
||||
audit=json.loads(Path(artifacts[1]["path"]).read_text())
|
||||
assert audit["acceptance"]["finalAcceptance"]["confirmedByUserId"]=="internal-acceptor"
|
||||
assert "internal-acceptor" not in (customer/"part.json").read_text()
|
||||
@ -2,18 +2,21 @@ import assert from "node:assert/strict";
|
||||
import fs from "node:fs/promises";
|
||||
import { chromium } from "playwright";
|
||||
|
||||
const base = process.env.DICTIONARY_TEST_URL || "http://127.0.0.1:5197";
|
||||
const base = process.env.DICTIONARY_TEST_URL || "http://127.0.0.1:5198";
|
||||
const out = process.env.DICTIONARY_TEST_OUTPUT || "local-data/verification/delivery-classification-20261021/browser";
|
||||
await fs.mkdir(out, { recursive: true });
|
||||
const browser = await chromium.launch({
|
||||
executablePath: process.env.BROWSER_EXECUTABLE || "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
|
||||
headless: true,
|
||||
});
|
||||
const page = await browser.newPage({ viewport: { width: 1365, height: 900 } });
|
||||
const errors = [];
|
||||
const failedRequests = [];
|
||||
page.on("requestfailed", (request) => failedRequests.push({ url: request.url(), failure: request.failure() }));
|
||||
page.on("pageerror", (error) => errors.push(error.message));
|
||||
try {
|
||||
const page = await browser.newPage({ viewport: { width: 1365, height: 900 } });
|
||||
page.setDefaultTimeout(60000);
|
||||
const errors = [];
|
||||
page.on("pageerror", (error) => errors.push(error.message));
|
||||
|
||||
let label = {
|
||||
id: "base",
|
||||
name: "物品操作",
|
||||
@ -56,6 +59,13 @@ try {
|
||||
`${out}/result.json`,
|
||||
JSON.stringify({ passed: true, edited: label, selected, pageErrors: errors }, null, 2),
|
||||
);
|
||||
} catch (error) {
|
||||
await fs.writeFile(
|
||||
`${out}/failure.json`,
|
||||
JSON.stringify({ errors, failedRequests, url: page.url(), html: await page.content() }, null, 2),
|
||||
);
|
||||
await page.screenshot({ path: `${out}/failure.png`, fullPage: true });
|
||||
throw error;
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
|
||||
13
tests/browser/dictionary-classification-vite.config.ts
Normal file
13
tests/browser/dictionary-classification-vite.config.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { defineConfig } from "vite";
|
||||
import react from "@vitejs/plugin-react";
|
||||
import tailwindcss from "@tailwindcss/vite";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [react(), tailwindcss()],
|
||||
build: {
|
||||
outDir: "local-data/verification/delivery-classification-20261021/browser-ui-dist",
|
||||
emptyOutDir: true,
|
||||
rollupOptions: { input: "tests/browser/fixtures/dictionary-classification-tags.html" },
|
||||
},
|
||||
preview: { host: "127.0.0.1", port: 5198, strictPort: true },
|
||||
});
|
||||
@ -38,3 +38,18 @@ test("never propose discarded, different-name, gapped or partially invalid parti
|
||||
test("invalid inside a single business slice stays inside it", () => {
|
||||
assert.deepEqual(candidates([{ id: "a", name: "盘点", start: 0, end: 3 }], actions), []);
|
||||
});
|
||||
|
||||
test("never merge same-name slices with distinct frozen dictionary classifications", () => {
|
||||
const dictionary = { id: "base", kind: "slice", version: 2, code: "sort", name: "盘点", tags: ["商品"] };
|
||||
const classified = slices.map((slice) => ({ ...slice, dictionary }));
|
||||
assert.equal(candidates(classified, actions).length, 1);
|
||||
for (const replacement of [{ ...dictionary, version: 3 }, { ...dictionary, tags: ["其他"] }, undefined]) {
|
||||
assert.deepEqual(
|
||||
candidates(
|
||||
classified.map((slice) => (slice.id === "c" ? { ...slice, dictionary: replacement } : slice)),
|
||||
actions,
|
||||
),
|
||||
[],
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user