fix(web): order overlapping settings writes by issue, not by completion

Review round 2 on #2091. Recording on completion made the *last to
finish* the winner, and `updateServerSettings` waits for a list read
after its PUT — so an older write with a slow read finishes last and
would overwrite the record with a value a newer write had already
replaced on disk. Both writers can overlap: two quick toggles, or the
settings modal's debounced flush.

A write is now announced with `begin` when it is issued and confirmed
with `landed` when it completes, and a confirmation is dropped if a
later-issued write for that server has already confirmed. The high-water
mark is kept apart from the record so a straggler cannot resurrect a
record a fresh list read dropped.

Issue order is a proxy for disk order rather than a proof of it — HTTP
promises nothing about two in-flight PUTs — but it is the best signal on
this side until the servers route returns a revision to order by. Noted
on the hook alongside the other consequence of that gap.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UL9retfvAXRgvi6EWk4SY4
Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
cliffhall 2026-08-23 11:34:20 -04:00
commit 7fa42299c7

View file

@ -76,13 +76,13 @@ describe("useLastPersistedSettings", () => {
it("returns the recorded write while the entry has not been re-read", () => {
const { api } = harness([entry("A")]);
const written = settings({ paginatedLists: true });
api.record("A", written);
api.begin("A").landed(written);
expect(api.resolve("A", settings())).toBe(written);
});
it("ignores a record made for a different server", () => {
const { api } = harness([entry("A"), entry("B")]);
api.record("A", settings({ paginatedLists: true }));
api.begin("A").landed(settings({ paginatedLists: true }));
const fallback = settings();
expect(api.resolve("B", fallback)).toBe(fallback);
});
@ -94,14 +94,14 @@ describe("useLastPersistedSettings", () => {
// slot would have lost A's baseline and reproduced #2089.
const { api } = harness([entry("A"), entry("B")]);
const writtenA = settings({ paginatedLists: true });
api.record("A", writtenA);
api.record("B", settings({ autoRefreshOnListChanged: true }));
api.begin("A").landed(writtenA);
api.begin("B").landed(settings({ autoRefreshOnListChanged: true }));
expect(api.resolve("A", settings())).toBe(writtenA);
});
it("stops trusting the record once a fresh list read replaces the entry", () => {
const { api, setServers } = harness([entry("A")]);
api.record("A", settings({ paginatedLists: true }));
api.begin("A").landed(settings({ paginatedLists: true }));
// A successful `GET /api/servers` rebuilds the list, so the entry is a new
// object even when its values are unchanged. That is the signal the list
// has caught up, and it is authoritative again from here.
@ -110,29 +110,56 @@ describe("useLastPersistedSettings", () => {
expect(api.resolve("A", fresh)).toBe(fresh);
});
it("pairs the record with the entry as of the write, not of the render", () => {
// A list read that lands between a write being issued and completing must
// not be mistaken for one that happened after it. `record` is called on
// completion and pairs with the list as it stands then, so the read below
// — which precedes the write — does not invalidate the record.
it("pairs the record with the entry as of completion, not of issue", () => {
// A list read that lands while the write is in flight must not be mistaken
// for one that happened after it. The pairing is taken in `landed`, so the
// read below — which the write outlives — does not invalidate the record.
const { api, setServers } = harness([entry("A")]);
const write = api.begin("A");
setServers([entry("A")]);
const written = settings({ paginatedLists: true });
api.record("A", written);
write.landed(written);
expect(api.resolve("A", settings())).toBe(written);
});
it("keeps only the most recent write for a server", () => {
const { api } = harness([entry("A")]);
api.record("A", settings({ paginatedLists: true }));
api.begin("A").landed(settings({ paginatedLists: true }));
const later = settings({ autoRefreshOnListChanged: true });
api.record("A", later);
api.begin("A").landed(later);
expect(api.resolve("A", settings())).toBe(later);
});
it("lets the later-issued write win however the two finish", () => {
// `updateServerSettings` waits for a list read after its PUT, so an older
// write with a slow read can complete last. Ordering by completion would
// then record a value that a newer write has already replaced on disk, and
// the next failed write would roll back to it.
const { api } = harness([entry("A")]);
const first = api.begin("A");
const second = api.begin("A");
const newest = settings({ paginatedLists: true });
second.landed(newest);
first.landed(settings({ paginatedLists: false }));
expect(api.resolve("A", settings())).toBe(newest);
});
it("does not let a straggler resurrect a record a fresh read dropped", () => {
// The high-water mark outlives the record, so an earlier write reporting
// after `resolve` invalidated things cannot reinstate a superseded value.
const { api, setServers } = harness([entry("A")]);
const first = api.begin("A");
api.begin("A").landed(settings({ paginatedLists: true }));
const fresh = settings();
setServers([entry("A", fresh)]);
expect(api.resolve("A", fresh)).toBe(fresh);
first.landed(settings({ paginatedLists: false }));
expect(api.resolve("A", fresh)).toBe(fresh);
});
it("falls back when the server has left the list entirely", () => {
const { api, setServers } = harness([entry("A")]);
api.record("A", settings({ paginatedLists: true }));
api.begin("A").landed(settings({ paginatedLists: true }));
setServers([]);
const fallback = settings();
expect(api.resolve("A", fallback)).toBe(fallback);