fix(web): key the rollback baseline per server, and pair it at completion

Review round 1 on #2091. A single record slot let a save on one server
discard the baseline a later failed write on another still needed — the
settings modal opens for any server, connected or not, so that sequence
reproduces #2089 through the very mechanism meant to prevent it. Records
are now kept per server id.

The record also paired with the `servers` entry captured when the write
was *issued*, so a list read landing while the write was in flight was
mistaken for one that happened after it and discarded a live record. It
now pairs with the entry as of completion, read through a ref (mirrored
in an effect — writing a ref during render is a `react-hooks/refs`
error). The residual — a read whose response predates the write but
arrives after it — needs a revision or ETag `useServers` does not
expose, and is documented on the hook.

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:18:34 -04:00
commit e359368892

View file

@ -26,71 +26,115 @@ const settings = (
});
/**
* One `servers` entry, as a fresh object each call — the point of most of these
* tests is object identity, so a shared fixture would defeat them.
* One `servers` entry, a fresh object per call — most of these tests turn on
* object identity, so a shared fixture would defeat them. A list rebuilt from
* fresh calls is what a successful `GET /api/servers` produces.
*/
const entry = (settingsNode?: InspectorServerSettings): ServerEntry => ({
id: "A",
name: "PlotRocket",
const entry = (
id: string,
settingsNode?: InspectorServerSettings,
): ServerEntry => ({
id,
name: id,
config: { type: "stdio", command: "node" },
connection: { status: "disconnected" },
settings: settingsNode,
});
interface Harness {
api: LastPersistedSettings;
/** Deliver a new server list, as a successful list read would. */
setServers: (next: ServerEntry[]) => void;
}
/**
* Hands the hook's API back to the test. The hook holds a ref, so the value has
* to be read from inside a rendered component.
* Hands the hook's API back to the test. The hook holds refs, so it has to be
* read from inside a rendered component; `setServers` re-renders it with a new
* list the way `useServers` does after a read.
*/
function harness(): LastPersistedSettings {
function harness(initial: ServerEntry[]): Harness {
let api: LastPersistedSettings | undefined;
function Probe() {
api = useLastPersistedSettings();
function Probe({ servers }: { servers: ServerEntry[] }) {
api = useLastPersistedSettings(servers);
return null;
}
renderWithMantine(<Probe />);
const { rerender } = renderWithMantine(<Probe servers={initial} />);
if (!api) throw new Error("hook did not render");
return api;
return {
api,
setServers: (next) => rerender(<Probe servers={next} />),
};
}
describe("useLastPersistedSettings", () => {
it("falls back to the caller's value when no write has been recorded", () => {
const { resolve } = harness();
const { api } = harness([entry("A")]);
const fallback = settings({ paginatedLists: true });
expect(resolve("A", entry(), fallback)).toBe(fallback);
expect(api.resolve("A", fallback)).toBe(fallback);
});
it("returns the recorded write while the entry has not been re-read", () => {
const { record, resolve } = harness();
const seen = entry();
const { api } = harness([entry("A")]);
const written = settings({ paginatedLists: true });
record("A", written, seen);
expect(resolve("A", seen, settings())).toBe(written);
api.record("A", written);
expect(api.resolve("A", settings())).toBe(written);
});
it("ignores a record made for a different server", () => {
const { record, resolve } = harness();
const seen = entry();
record("A", settings({ paginatedLists: true }), seen);
const { api } = harness([entry("A"), entry("B")]);
api.record("A", settings({ paginatedLists: true }));
const fallback = settings();
expect(resolve("B", seen, fallback)).toBe(fallback);
expect(api.resolve("B", fallback)).toBe(fallback);
});
it("keeps each server's record, so a save on B doesn't discard A's", () => {
// The settings modal can be opened for any server, connected or not, so
// this sequence is reachable: A's write lands while list reads are failing,
// B is saved from the modal, and only then does a write on A fail. A single
// 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 }));
expect(api.resolve("A", settings())).toBe(writtenA);
});
it("stops trusting the record once a fresh list read replaces the entry", () => {
const { record, resolve } = harness();
record("A", settings({ paginatedLists: true }), entry());
const { api, setServers } = harness([entry("A")]);
api.record("A", 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.
const fresh = settings();
expect(resolve("A", entry(fresh), fresh)).toBe(fresh);
setServers([entry("A", fresh)]);
expect(api.resolve("A", fresh)).toBe(fresh);
});
it("keeps only the most recent write", () => {
const { record, resolve } = harness();
const seen = entry();
record("A", settings({ paginatedLists: true }), seen);
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.
const { api, setServers } = harness([entry("A")]);
setServers([entry("A")]);
const written = settings({ paginatedLists: true });
api.record("A", 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 }));
const later = settings({ autoRefreshOnListChanged: true });
record("A", later, seen);
expect(resolve("A", seen, settings())).toBe(later);
api.record("A", later);
expect(api.resolve("A", settings())).toBe(later);
});
it("falls back when the server has left the list entirely", () => {
const { api, setServers } = harness([entry("A")]);
api.record("A", settings({ paginatedLists: true }));
setServers([]);
const fallback = settings();
expect(api.resolve("A", fallback)).toBe(fallback);
});
});