Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(inspector): scope unknown-save blocking to the save's own epoch

超超超超超级喜欢你的达妮娅2ff45621a58f
diff --git a/src/lib/ai-edition/store/useTimeline.test.ts b/src/lib/ai-edition/store/useTimeline.test.ts
index d17268bbd..5fd041c42 100644
--- a/src/lib/ai-edition/store/useTimeline.test.ts
+++ b/src/lib/ai-edition/store/useTimeline.test.ts
@@ -1410,6 +1410,77 @@ describe("useTimeline undo history", () => {
}
});
+ // Rebase-review follow-up (unknown save × replacement): the refusal must not outlive
+ // its reason. Once an undo bumps the epoch, the stuck save can no longer install
+ // anything (`saveDocument` drops it), so it must stop blocking zoom writes — even if
+ // the bridge never answers. A project switch does NOT bump the epoch, so there the
+ // stuck save can still land and the refusal correctly stays.
+ it("stops refusing zoom writes once a replacement makes the unknown save unable to land", async () => {
+ seed(docWithZoom);
+ vi.useFakeTimers();
+ try {
+ let hungDoc: AxcutDocument | undefined;
+ let releaseHungSave: (result: { success: boolean; document: AxcutDocument }) => void = () => {
+ // replaced once the hung save registers
+ };
+ let saveCalls = 0;
+ bridgeMocks.save.mockImplementation((doc: AxcutDocument) => {
+ saveCalls += 1;
+ if (saveCalls === 2) {
+ // The write whose answer never comes; the test never releases it until
+ // the very end, and then only to prove the epoch guard drops it.
+ hungDoc = doc;
+ return new Promise((resolve) => {
+ releaseHungSave = resolve;
+ });
+ }
+ return Promise.resolve({ success: true, document: doc });
+ });
+ const { result } = renderTimeline();
+
+ // One settled write so the undo has a recorded state to restore.
+ await act(async () => {
+ await result.current.updateZoomDepth("zoom_a", 4);
+ });
+ expect(useProjectStore.getState().document?.zoomRanges[0]?.depth).toBe(4);
+
+ let depthOk: boolean | undefined;
+ act(() => {
+ void result.current.updateZoomDepth("zoom_a", 5).then((ok) => {
+ depthOk = ok;
+ });
+ });
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(10_000);
+ });
+ expect(depthOk).toBe(false);
+
+ // The undo replaces the document; the stuck save can no longer install it.
+ let undid = false;
+ act(() => {
+ undid = undo();
+ });
+ expect(undid).toBe(true);
+
+ // Zoom writes must work again on the restored document.
+ await act(async () => {
+ const ok = await result.current.updateZoomDepth("zoom_a", 5);
+ expect(ok).toBe(true);
+ });
+ expect(useProjectStore.getState().document?.zoomRanges[0]?.depth).toBe(5);
+
+ // When the stuck save finally settles, the epoch guard drops it — the restored
+ // (and since re-edited) document stands.
+ await act(async () => {
+ releaseHungSave({ success: true, document: hungDoc! });
+ for (let i = 0; i < 20; i++) await Promise.resolve();
+ });
+ expect(useProjectStore.getState().document?.zoomRanges[0]?.depth).toBe(5);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
it("resolves a zoom-level write with whether the save took effect", async () => {
seed(docWithZoom);
const { result } = renderTimeline();
diff --git a/src/lib/ai-edition/store/useTimeline.ts b/src/lib/ai-edition/store/useTimeline.ts
index 2e5dfff10..37796941c 100644
--- a/src/lib/ai-edition/store/useTimeline.ts
+++ b/src/lib/ai-edition/store/useTimeline.ts
@@ -728,8 +728,11 @@ export function useTimeline() {
// that replaced its target. A save whose answer is unknown (`saveWithDeadline` timed out
// with the bridge still silent) may still land, so later zoom writes are refused until it
// settles instead of racing it — the same "a queued write racing a stuck one" the
- // `waitForDocumentSaves` header calls out.
- const unknownZoomSavesRef = useRef(0);
+ // `waitForDocumentSaves` header calls out. The block is keyed to the save's own epoch:
+ // once a replacement moves the epoch, that save can no longer install anything
+ // (`saveDocument` drops it) and must stop blocking; a project switch does not move the
+ // epoch, so there the stuck save can still land and the block correctly stays.
+ const unknownZoomSavesRef = useRef<Array<number>>([]);
const saveZoomPatch = useCallback(
(id: string, patch: Partial<AxcutDocument["zoomRanges"][number]>) => {
const epoch = currentWriteEpoch();
@@ -738,7 +741,9 @@ export function useTimeline() {
if (useProjectStore.getState().projectId !== projectId || currentWriteEpoch() !== epoch) {
return false;
}
- if (unknownZoomSavesRef.current > 0) return false;
+ if (unknownZoomSavesRef.current.filter((stuck) => stuck === epoch).length > 0) {
+ return false;
+ }
const doc = useProjectStore.getState().document;
if (!doc) return false;
const save = saveDocument(
@@ -751,15 +756,18 @@ export function useTimeline() {
const outcome = await saveWithDeadline(save);
if (outcome !== "timeout") return outcome === true;
// Unknown, not failed: the write may still land. Report not-taken (the
- // buttons retry) and refuse later zoom writes until the save settles.
- unknownZoomSavesRef.current += 1;
+ // buttons retry) and refuse later writes into this same document generation
+ // until the save settles or the epoch moves past it.
+ unknownZoomSavesRef.current.push(epoch);
void save
.then(
() => undefined,
() => undefined,
)
.finally(() => {
- unknownZoomSavesRef.current -= 1;
+ unknownZoomSavesRef.current = unknownZoomSavesRef.current.filter(
+ (stuck) => stuck !== epoch,
+ );
});
return false;
});