fix(query-cache): fence the scope only when session state or subject changes (R-3.2)

Every session notification aborted in-flight requests, bumped the generation
and cleared the cache, even when nothing changed. A repeated sign-out or a
token-refresh notification wiped the cache, and with V3 calling
onUnauthenticated() on a logged-out query it could loop.

The scope runtime now reads (getState, getSubject) and skips a notification
that leaves both unchanged. An identity it cannot prove unchanged still
fences: a session without getState (old behavior), a throwing owner, or an
authenticated session with no subject. A FAILED scope still retries its
reset on any notification.

To make the subject observable, SessionGateway gets an optional
`getSubject?(): string | null` and ExternalSessionOwner an optional
`readSubject?()` (additive; existing owners keep fencing on every
authenticated notification).

RED before fix: tests/unit/server-state-scope-runtime.test.ts "does not
fence, abort or clear on a repeated sign-out" / "... on a token refresh for
the same subject" saw FENCED. Pinned (passing before and after): "still
fences when the state changes / the subject changes / an authenticated
session cannot name its subject", "retries a failed reset ...".
Gates: check:architecture, check:types:app, check:types:test, lint,
test:component, test:integration, test:reference-feature pass. test:unit
passes except removal-fixture.test.ts (needs generated artifacts/ release
evidence absent in a fresh worktree; unrelated).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-24 12:25:01 +09:00
co-authored by Claude Opus 5.5
parent ebbd50d4fb
commit a39eed6c11
4 changed files with 160 additions and 1 deletions
@@ -24,7 +24,12 @@ export type ScopeResetParticipant = Readonly<{
}>;
export type ServerStateScopeDependencies = Readonly<{
session: Pick<AuthSessionPort, "subscribe">;
/**
* `getState`/`getSubject` let the runtime skip a notification that changes
* neither. Without `getState` every notification fences, as before.
*/
session: Pick<AuthSessionPort, "subscribe"> &
Partial<Pick<AuthSessionPort, "getState" | "getSubject">>;
queryInvalidation: Pick<QueryInvalidationCoordinator, "resetLocal">;
tokenFactory?: () => string;
participants?: readonly ScopeResetParticipant[];
@@ -49,6 +54,7 @@ export function createServerStateScopeRuntime(
let phase: ClientScopePhase = "READY";
let disposed = false;
let resetChain = Promise.resolve();
let sessionIdentity = readSessionIdentity(dependencies.session);
function createSnapshot(): CacheScopeSnapshot {
const capturedGeneration = generation;
@@ -89,6 +95,18 @@ export function createServerStateScopeRuntime(
const unsubscribe = dependencies.session.subscribe(() => {
if (disposed) return;
// §10.6 is for identity-affecting changes only. A notification that keeps
// (state, subject) — a token refresh, a repeated sign-out — must not abort
// requests and clear the cache. A FAILED scope still retries its reset.
const nextSessionIdentity = readSessionIdentity(dependencies.session);
if (
nextSessionIdentity !== null &&
nextSessionIdentity === sessionIdentity &&
phase !== "FAILED"
) {
return;
}
sessionIdentity = nextSessionIdentity;
const previousIdentities = identities;
const previousGeneration = generation;
@@ -185,6 +203,26 @@ export function createServerStateScopeRuntime(
});
}
/**
* `null` means the identity cannot be proven unchanged: no `getState`, a
* throwing owner, or an authenticated session without a subject. Those always
* fence, which keeps an account switch from reusing the previous cache.
*/
function readSessionIdentity(
session: ServerStateScopeDependencies["session"],
): string | null {
try {
const state = session.getState?.();
if (state === undefined) return null;
const subject = session.getSubject?.();
const knownSubject = typeof subject === "string" ? subject : null;
if (state === "authenticated" && knownSubject === null) return null;
return JSON.stringify([state, knownSubject]);
} catch {
return null;
}
}
function newIdentityRegistry(
tokenFactory: (() => string) | undefined,
): RuntimeIdentityRegistry {