diff --git a/pkgs/id/web/src/auto-save.test.ts b/pkgs/id/web/src/auto-save.test.ts index 38dea5ea16..231cc328f8 100644 --- a/pkgs/id/web/src/auto-save.test.ts +++ b/pkgs/id/web/src/auto-save.test.ts @@ -130,6 +130,38 @@ describe("AutoSaveManager serialization (#1440)", () => { expect(save.calls).toBe(2); }); + it("runs exactly one follow-up when an edit lands during a generically failing save (#1616)", async () => { + const save = deferredSaveFn(); + const mgr = new AutoSaveManager(save.fn); + + mgr.onContentChange(); + await vi.advanceTimersByTimeAsync(AUTOSAVE_DEBOUNCE_MS); + expect(save.calls).toBe(1); + expect(mgr.state).toBe("saving"); + + // Edit during the flight; its debounce fires mid-flight too, so the + // follow-up is both requested and visible in the edit generation. + mgr.onContentChange(); + await vi.advanceTimersByTimeAsync(AUTOSAVE_DEBOUNCE_MS); + expect(save.calls).toBe(1); // still serialized + + // The save fails generically: the coalesced edit must not be stranded — + // exactly one follow-up runs. + save.resolveNext({ ok: false }); + await flush(); + expect(save.calls).toBe(2); + expect(save.maxInFlight).toBe(1); + + // The follow-up also fails generically, with no new edits: it must stop + // in a terminal unsaved/error state — no third call, no retry loop. + save.resolveNext({ ok: false }); + await flush(); + expect(mgr.state).toBe("error"); + await vi.advanceTimersByTimeAsync(10_000); + expect(save.calls).toBe(2); + expect(mgr.state).toBe("unsaved"); + }); + it("saveNow saves immediately when idle and is a no-op while saving", async () => { const save = deferredSaveFn(); const mgr = new AutoSaveManager(save.fn); diff --git a/pkgs/id/web/src/auto-save.ts b/pkgs/id/web/src/auto-save.ts index 240b78d2d8..62cfaf3f31 100644 --- a/pkgs/id/web/src/auto-save.ts +++ b/pkgs/id/web/src/auto-save.ts @@ -180,7 +180,8 @@ export class AutoSaveManager { } else if (result.retryAfterMs) { // Rate limited — schedule retry. The retry captures the document when // it fires, so it also serves as the follow-up for any edits that - // arrived during the failed save. + // arrived during the failed save: consume the follow-up request here. + this.followUpRequested = false; this.state = "rate-limited"; this.updateIndicator(); @@ -189,7 +190,14 @@ export class AutoSaveManager { this.triggerSave(); }, result.retryAfterMs); } else { - // Generic error — don't auto-retry (prevents infinite loops on network outage) + // Generic error — don't auto-retry a save that covered everything + // (prevents infinite loops on network outage). But an edit coalesced + // during this save is not covered by it: consume the request and run + // exactly one follow-up (#1616). The follow-up starts with the request + // cleared, so if it also fails generically with no new edits, it stops + // in the error state below — no retry loop. + const followUp = this.followUpRequested || this.editGeneration !== startGeneration; + this.followUpRequested = false; this.state = "error"; this.updateIndicator(); @@ -201,6 +209,10 @@ export class AutoSaveManager { this.updateIndicator(); } }, 2000); + + if (followUp) { + void this.triggerSave(); + } } }