🐛 Overlay existing action drafts in autosave payloads

This commit is contained in:
d-robotics 2026-10-16 11:10:34 +08:00
parent 9074f8b57d
commit 630f0296ce
4 changed files with 70 additions and 3 deletions

View File

@ -73,3 +73,9 @@ Status: ready-for-agent(D1–D4 已于 2026-10-12 按推荐确认;实施遵
- issues/02-review-autoflush.md —— 审查/终审问题草稿自动落盘
- issues/03-local-crash-snapshot.md —— 本地崩溃兜底快照(P2,可选)
- issues/04-verification.md —— E2E + 压测 + 回归验证
## D4 归并修正(2026-10-16)
编辑已有动作时,编辑器草稿与工作区动作沿用同一 ID;自动保存按 ID 将草稿覆盖到对应位置,仅不同 ID 的新动作追加。新切分的动作已存在于本地工作区时也采用覆盖,不能再次追加为重复区间。合并不修改原工作区/编辑器对象,不自动确认描述、不移动区间,也不放宽服务端唯一 ID、连续覆盖和重叠校验。不对本来就非法的工作区静默删动作或改 ID。
修复范围仅前端请求载荷组装,无数据迁移和后端接口变更;旧合法请求保持兼容。定向 10 项自动保存测试通过(新增 2 项先失败后通过),前端基线 13/14 已知失败、无新增失败,lint/format/web build 通过。真实工作台合成页面编辑已有动作后仍为两个唯一 ID、文本保留;没有向线上提交业务写入。核对的线上已保存工作区没有重复或重叠,失败校验未将该非法请求写入;并不据此宣称所有客户任务均已检查或所有未保存编辑均已持久化。

View File

@ -63,18 +63,30 @@ export function segmentForDraftPayload(segment) {
}
/**
* D4 描述草稿归并:未确认草稿片段并入 segments 一起保存(草稿层语义,不触碰确认)。
* D4 描述草稿归并:同 ID 草稿覆盖对应动作,只有新 ID 才追加(不触碰正式确认)。
* 仅并入区间完整的草稿(start<end);正在选终点、区间不完整的草稿跳过——
* 其文本仍由编辑器持有,待区间确定后由下一触发点覆盖。
*/
export function autosaveSegmentsWithDrafts(segments = [], drafts = []) {
const merged = (Array.isArray(segments) ? segments : []).map(segmentForDraftPayload);
const positions = new Map();
merged.forEach((segment, index) => {
if (typeof segment.id === "string" && segment.id) positions.set(segment.id, index);
});
for (const draft of Array.isArray(drafts) ? drafts : []) {
const annotation = draft?.annotation;
if (!annotation) continue;
const { start, end } = annotation;
if (typeof start !== "number" || typeof end !== "number" || !(end > start)) continue;
merged.push(segmentForDraftPayload(annotation));
const value = segmentForDraftPayload(annotation);
// Existing editors and newly split actions both retain the action ID.
// Appending would duplicate the ID and overlap the entire original interval.
const index = positions.get(annotation.id);
if (index !== undefined) merged[index] = value;
else {
if (typeof annotation.id === "string" && annotation.id) positions.set(annotation.id, merged.length);
merged.push(value);
}
}
return merged;
}

View File

@ -1,7 +1,8 @@
import React, { useEffect, useRef, useState } from "react";
import { createRoot } from "react-dom/client";
import { CaptureAnnotationWorkbench } from "/src/components/CaptureAnnotationWorkbench";
import { CaptureAnnotationWorkbench, type AnnotationDraft } from "/src/components/CaptureAnnotationWorkbench";
import { sliceEditDispositionCommands, previewSliceEditDispositions } from "/src/lib/slice-edit-dispositions.js";
import { autosaveSegmentsWithDrafts } from "/src/lib/annotation-autosave.js";
import { createAnnotationHistory } from "/src/lib/annotation-history.js";
import "/src/index.css";
const query = new URLSearchParams(location.search);
@ -78,6 +79,7 @@ function Harness() {
},
],
);
const [drafts, setDrafts] = useState<AnnotationDraft[]>([]);
const [commands, setCommands] = useState<object[]>([]);
const [count, setCount] = useState(0);
const history = useRef(createAnnotationHistory());
@ -113,6 +115,7 @@ function Harness() {
discardReasons={[{ id: "reason", name: "测试原因", version: 1 }]}
sliceDictionary={[{ id: "name", name: "测试切片" }]}
onChange={setActions}
onDraftsChange={setDrafts}
onApplySliceEdit={async (next, segments) => {
setCommands(
sliceEditDispositionCommands(slices, next, { resetRestoredActions: true, restoredActions: segments }),
@ -138,6 +141,7 @@ function Harness() {
onSubmitTask={() => {}}
onReturnToPool={() => {}}
/>
<pre data-testid="autosave-payload">{JSON.stringify(autosaveSegmentsWithDrafts(actions, drafts))}</pre>
<pre data-testid="disposition-commands">{JSON.stringify(commands)}</pre>
<pre data-testid="slice-state">{JSON.stringify(slices)}</pre>
<pre data-testid="track-state">{JSON.stringify(actions)}</pre>

View File

@ -153,3 +153,48 @@ test("slice-edit session releases trigger exactly one immediate draft flush (acc
assert.equal(sliceEditSessionFlushEdge(false, false), false, "会话外不补");
assert.equal(sliceEditSessionFlushEdge(true, true), false, "持续暂停不补");
});
test("autosave overlays an edited existing action instead of duplicating its ID and interval", () => {
const segments = [
{ id: "left", start: 0, end: 2, status: "pending", description: "" },
{ id: "edited", start: 2, end: 5, status: "described", description: "old" },
{ id: "right", start: 5, end: 8, status: "invalid", isInvalid: true },
];
const original = structuredClone(segments);
const draft = {
sliceId: "s",
annotation: { ...segments[1], description: "new text" },
selectingEnd: false,
isNew: false,
};
const merged = autosaveSegmentsWithDrafts(segments, [draft]);
assert.equal(merged.length, 3);
assert.equal(new Set(merged.map((a) => a.id)).size, 3);
assert.deepEqual(
merged.map((a) => [a.start, a.end]),
[
[0, 2],
[2, 5],
[5, 8],
],
);
assert.equal(merged[1].description, "new text");
assert.deepEqual(segments, original, "autosave must not mutate confirmed actions");
});
test("autosave keeps one copy when a new split action already exists locally and ignores unfinished ends", () => {
const action = { id: "split-child", start: 3, end: 5, status: "pending", description: "" };
const merged = autosaveSegmentsWithDrafts(
[action],
[
{ sliceId: "s", annotation: { ...action, description: "typing new action" }, isNew: false },
{ sliceId: "other", annotation: { id: "new", start: 5, end: 7, description: "new action" }, isNew: true },
{ sliceId: "s", annotation: { ...action, end: null, description: "unfinished" }, selectingEnd: true },
],
);
assert.deepEqual(
merged.map((a) => a.id),
["split-child", "new"],
);
assert.equal(merged[0].description, "typing new action");
});