Skip to content

Commit 89c06ec

Browse files
cliffhallclaude
andcommitted
docs(auth): address #1385 review — clarify double-save + keepalive cap + test fetch default (#1384)
Code-review feedback on the OAuth Network-log persistence PR: - Documented the double-save: `FetchRequestLogState`'s `saveSession` listener is the backstop; `BrowserNavigation`'s `beforeNavigate` hook is the primary flush for the redirect case. Notes the listener may lose the navigation race and is harmless when it duplicates (last-writer-wins, identical payload). - Reworded the keepalive comment in `RemoteInspectorClientStorage.saveSession`: the 64KB cap is general (the method is also reachable from the listener with the full session log), so a long session could exceed it and drop silently — acceptable since the persisted log is best-effort, not load-bearing. - Added a regression test that constructs `RemoteInspectorClientStorage` without a `fetchFn`, stubs `globalThis.fetch`, and asserts the default wrapper calls it (locks in the "Illegal invocation" fix, which callers otherwise swallow). Optional items (logger.warn on swallowed save errors; setupClientForServer dep churn) acknowledged on the PR, not changed. Also merges the squash-merged #1383 base from v2/main. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 669c38d commit 89c06ec

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

‎clients/web/src/test/core/mcp/remote/sessionStorage.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,28 @@ describe("RemoteInspectorClientStorage", () => {
160160
],
161161
).toBeUndefined();
162162
});
163+
164+
it("defaults to a wrapper around globalThis.fetch (not the bare reference)", async () => {
165+
// Regression guard: the default must call `globalThis.fetch` with the
166+
// global as receiver. Assigning the bare reference and invoking it as
167+
// `this.fetchFn(...)` throws "Illegal invocation" — which callers swallow
168+
// via `.catch(() => {})`, so it would silently break all save/load.
169+
const spy = vi
170+
.spyOn(globalThis, "fetch")
171+
.mockResolvedValue(new Response(null, { status: 204 }));
172+
try {
173+
const storage = new RemoteInspectorClientStorage({
174+
baseUrl: "http://remote.example/",
175+
});
176+
await expect(
177+
storage.saveSession("sid", sampleState()),
178+
).resolves.toBeUndefined();
179+
expect(spy).toHaveBeenCalledTimes(1);
180+
expect(spy.mock.calls[0]?.[0]).toBe(
181+
"http://remote.example/api/storage/inspector-session-sid",
182+
);
183+
} finally {
184+
spy.mockRestore();
185+
}
186+
});
163187
});

‎core/mcp/remote/sessionStorage.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,16 @@ export class RemoteInspectorClientStorage implements InspectorClientStorage {
7474
method: "POST",
7575
headers,
7676
body: JSON.stringify(serializedState),
77-
// `saveSession` is only invoked right before the OAuth full-page
78-
// redirect (via the client's `saveSession` event in
79-
// `onBeforeOAuthRedirect`). Navigation is already scheduled by that
80-
// point, so a normal fetch would be cancelled mid-flight and the
77+
// `saveSession` runs as the OAuth full-page redirect is being
78+
// scheduled, so a normal fetch would be cancelled mid-flight and the
8179
// pre-redirect network log (discovery + DCR) would never reach disk.
82-
// `keepalive` lets the request outlive the unloading document. The
83-
// payload is the pre-redirect log only (a few small auth entries, no
84-
// captured bodies), comfortably under keepalive's 64KB cap.
80+
// `keepalive` lets the request outlive the unloading document.
81+
// Caveat: keepalive caps the body at 64KB. The pre-redirect-log payload
82+
// is small, but this method is also reachable from
83+
// `FetchRequestLogState`'s `saveSession` listener with the full session
84+
// log — a long session could exceed the cap and be dropped silently.
85+
// Acceptable here: the persisted log is a best-effort convenience, not
86+
// load-bearing state.
8587
keepalive: true,
8688
});
8789

‎core/mcp/state/fetchRequestLogState.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ export class FetchRequestLogState extends TypedEventTarget<FetchRequestLogStateE
103103
const sessionId = options.sessionId;
104104

105105
if (sessionStorage) {
106+
// Backstop persistence on the client's `saveSession` event. For the
107+
// OAuth full-page-redirect case the web client's `BrowserNavigation`
108+
// `beforeNavigate` hook is the *primary* flush (it runs synchronously
109+
// before navigation, so a keepalive request survives the unload); this
110+
// listener fires from the same event but can lose the race with an
111+
// already-scheduled navigation. It remains the save path for any
112+
// non-redirect `saveSession` caller (e.g. a future token-refresh save
113+
// point) and is harmless when it duplicates the primary flush —
114+
// last-writer-wins on an identical payload under the same id.
106115
const onSaveSession = (
107116
event: TypedEventGeneric<InspectorClientEventMap, "saveSession">,
108117
): void => {

0 commit comments

Comments
 (0)