fix(release): share one reload-once guard between chunk and boot recovery (Q-6.9-9)

Chunk recovery kept its own guard: marker keyed by the release pair, no
TTL, trusted write().ok. Round 1 fixed the boot guard (key-independent
marker within a TTL, read-back check) but the chunk copy still reloaded
on every failure when replicas alternated A->B, B->A.

platform/release gains createReloadOnceGuard (the round-1 boot rule,
now with a refusal reason); createReleaseMismatchRecovery wraps it with
unchanged boot semantics (marker within TTL blocks regardless of key,
markCoherentBoot clears), and decideChunkRecovery uses it over its own
CHUNK_RELOAD_GUARD key, keeping its reason vocabulary. Application ->
platform is allowed by .dependency-cruiser.json; platform may not import
application, so the guard lives in platform.

Behavior change (chunk only): a second reload within the TTL is refused
even for a different release pair, and a marker that does not read back
is guard-write-failed. tests/component/context-route-gateway.test.tsx
used a preferences stub that dropped writes; it now persists them.

Verified: tests/unit/chunk-recovery.test.ts characterization (read/write
failure, same pair, invariants) green before the change; alternating
pair test RED before, green after. Boot tests unchanged and green;
FE-RB-002 drill PASS; test:integration PASS; all gates PASS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-24 13:58:35 +09:00
co-authored by Claude Opus 5.5
parent e119eb667a
commit 40659de7ec
4 changed files with 152 additions and 26 deletions
@@ -29,15 +29,36 @@ export interface ReleaseMismatchRecovery {
markCoherentBoot(): void;
}
/**
* Q-6.9-9. Why a reload-once guard allowed or refused a reload. Every refusal
* fails closed: without a working marker there is no automatic reload.
*/
export type ReloadOnceDecision =
| "reload-once"
| "reload-already-attempted"
| "guard-read-failed"
| "guard-write-failed";
/**
* Q-6.9-9. The one "reload at most once" guard. Boot mismatch recovery and
* chunk recovery each keep their own marker (store) and key, but share this
* rule, so a fix to one cannot leave the other looping.
*/
export interface ReloadOnceGuard {
decide(key: string): ReloadOnceDecision;
/** Drops the marker, e.g. after a coherent boot. Never throws. */
clear(): void;
}
const MAX_KEY_LENGTH = 256;
export function createReleaseMismatchRecovery(
export function createReloadOnceGuard(
store: RecoveryMarkerStore | null,
options: Readonly<{
now?: () => number;
ttlMs?: number;
}> = {},
): ReleaseMismatchRecovery {
): ReloadOnceGuard {
const now = options.now ?? (() => Date.now());
const ttlMs = options.ttlMs ?? 10 * 60_000;
@@ -51,26 +72,30 @@ export function createReleaseMismatchRecovery(
}
return Object.freeze({
decide(mismatchKey: string): ReleaseMismatchDecision {
const key = mismatchKey.slice(0, MAX_KEY_LENGTH);
decide(rawKey: string): ReloadOnceDecision {
const key = rawKey.slice(0, MAX_KEY_LENGTH);
if (!store) return "guard-read-failed";
let current: number;
try {
if (!store) return "show-failure";
const previous = read();
const current = now();
// Key-independent: alternating replicas change the key on every boot.
current = now();
// Key-independent: alternating replicas change the key every time.
if (previous && current - previous.at < ttlMs) {
return "show-failure";
return "reload-already-attempted";
}
} catch {
return "guard-read-failed";
}
try {
store.write(`${key}@${current}`);
// Only a marker that is really persisted may authorize a reload;
// otherwise the next boot could not know it already reloaded.
return read()?.key === key ? "reload-once" : "show-failure";
// otherwise the next attempt could not know it already reloaded.
return read()?.key === key ? "reload-once" : "guard-write-failed";
} catch {
// Fail closed: without a working marker, never reload automatically.
return "show-failure";
return "guard-write-failed";
}
},
markCoherentBoot(): void {
clear(): void {
try {
store?.clear();
} catch {
@@ -79,3 +104,23 @@ export function createReleaseMismatchRecovery(
},
});
}
export function createReleaseMismatchRecovery(
store: RecoveryMarkerStore | null,
options: Readonly<{
now?: () => number;
ttlMs?: number;
}> = {},
): ReleaseMismatchRecovery {
const guard = createReloadOnceGuard(store, options);
return Object.freeze({
decide(mismatchKey: string): ReleaseMismatchDecision {
return guard.decide(mismatchKey) === "reload-once"
? "reload-once"
: "show-failure";
},
markCoherentBoot(): void {
guard.clear();
},
});
}