diff --git a/packages/core/src/feature-status.ts b/packages/core/src/feature-status.ts index debc5eff..51c65884 100644 --- a/packages/core/src/feature-status.ts +++ b/packages/core/src/feature-status.ts @@ -87,7 +87,7 @@ export function featureThresholds(): Partial> { return out; } -let cache: { at: number; value: Record } | null = null; +let cache: { at: number; print: string; value: Record } | null = null; /** Tests and the superadmin's changes: read afresh next time. */ export function resetFeatureCache(): void { @@ -97,7 +97,10 @@ export function resetFeatureCache(): void { /** Every feature's state (cached 15 s); trouble with observe.db = everything available. */ export function featureStatuses(o: { now?: Date; fresh?: boolean } = {}): Record { const now = o.now ?? new Date(); - if (!o.fresh && !o.now && cache && Date.now() - cache.at < CACHE_MS) return cache.value; + // The announced states (written by the observer) are part of the key: a change shows at once, even in + // another copy of this module (Next bundles route handlers and pages separately). + const print = o.now ? "" : featureFingerprint(); + if (!o.fresh && !o.now && cache && cache.print === print && Date.now() - cache.at < CACHE_MS) return cache.value; let value: Record; try { value = computeFeatures(listMonitors(), featureThresholds(), now); @@ -105,7 +108,7 @@ export function featureStatuses(o: { now?: Date; fresh?: boolean } = {}): Record console.error("feature-status: observe.db unavailable, everything counts as available:", (e as Error).message); value = allAvailable(); } - if (!o.now) cache = { at: Date.now(), value }; + if (!o.now) cache = { at: Date.now(), print, value }; return value; } diff --git a/packages/runner/src/live.ts b/packages/runner/src/live.ts index 624e0eef..34077c9d 100644 --- a/packages/runner/src/live.ts +++ b/packages/runner/src/live.ts @@ -196,6 +196,9 @@ class LiveHub { if (v !== this.seen) { this.seen = v; this.wake(); + } else if (this.listeners.has("features") && featureFingerprint() !== this.prints.get("features")) { + // PEP-224: the observer writes observe.db only — watch it directly (one tiny query), not just pokes. + this.wake(); } }, 1000); this.timer.unref?.(); diff --git a/packages/testkit/test/feature-status.test.ts b/packages/testkit/test/feature-status.test.ts index 07c9eb96..6ee900d8 100644 --- a/packages/testkit/test/feature-status.test.ts +++ b/packages/testkit/test/feature-status.test.ts @@ -90,9 +90,12 @@ describe("Feature status", () => { check(relayMon.id, good, new Date()); assert.equal(core.featureStatus("egov_login", { fresh: true }).state, "unavailable", "one good check is not enough (recover_threshold 2)"); + // The cache follows the announced states: another module copy (a page) sees a change at once. + assert.equal(core.featureStatus("egov_login").state, "unavailable", "cached"); check(relayMon.id, good, new Date()); assert.equal(core.featureStatus("egov_login", { fresh: true }).state, "available"); await core.featureTick(db); + assert.equal(core.featureStatus("egov_login").state, "available", "announced — the cache is dropped by the fingerprint"); msgs = await sent(); assert.equal(msgs.length, 2); assert.match(msgs[1]!, /✅ «Вход через eGov» снова включено для пользователей/);