Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): fail closed on unverifiable freshness and unparseable peer reads
[._.]/ Adam Eivy652e77d163a2
diff --git a/client/src/components/system-resources/MediaCapacityPanel.jsx b/client/src/components/system-resources/MediaCapacityPanel.jsxindex d5eed602cd..ce1a93479c 100644--- a/client/src/components/system-resources/MediaCapacityPanel.jsx+++ b/client/src/components/system-resources/MediaCapacityPanel.jsx@@ -88,14 +88,16 @@ export default function MediaCapacityPanel({ media }) {const loadPeers = useCallback(async () => {const data = await api.getInstances({ silent: true }).catch(() => null);- // `null` = the read failed, distinct from `[]` = read fine, no peers. A- // failed read must not render as "you have no providers configured".- if (!data) {+ // A read that failed, and a read that came back without a peer array, are+ // both "we do not know" — distinct from `[]`, which is "read fine, no+ // peers". Coercing a malformed body to `[]` would render the confident+ // claim "no providers configured" off a response we could not parse.+ if (!Array.isArray(data?.peers)) {setPeersFailed(true);return;}setPeersFailed(false);- setPeers(Array.isArray(data.peers) ? data.peers : []);+ setPeers(data.peers);}, []);// Poll on the same cadence as the health report this panel sits in. Loadingdiff --git a/client/src/components/system-resources/MediaCapacityPanel.test.jsx b/client/src/components/system-resources/MediaCapacityPanel.test.jsxindex 78444ab808..f16f535024 100644--- a/client/src/components/system-resources/MediaCapacityPanel.test.jsx+++ b/client/src/components/system-resources/MediaCapacityPanel.test.jsx@@ -128,6 +128,18 @@ describe('MediaCapacityPanel', () => {expect(screen.queryByText(/No peer is enabled as a media provider/)).not.toBeInTheDocument();});+ // A body we could not parse is "we do not know", not "there are none" —+ // coercing it to [] would state the opposite off a response we never read.+ it.each([{}, { peers: null }, { peers: 'nope' }])(+ 'treats the malformed body %p as a failed read, not an empty one',+ async (body) => {+ api.getInstances.mockResolvedValue(body);+ renderPanel();+ expect(await screen.findByText(/provider readiness is unknown/i)).toBeInTheDocument();+ expect(screen.queryByText(/No peer is enabled as a media provider/)).not.toBeInTheDocument();+ },+ );+it('shows a disabled peer connection as disabled, not as ready', async () => {api.getInstances.mockResolvedValue({ peers: [providerPeer({ enabled: false })] });renderPanel();diff --git a/client/src/lib/federatedMediaReadiness.js b/client/src/lib/federatedMediaReadiness.jsindex 893fbbaede..f772c4ed53 100644--- a/client/src/lib/federatedMediaReadiness.js+++ b/client/src/lib/federatedMediaReadiness.js@@ -90,8 +90,14 @@ export function peerMediaProviderConfig(peer) {return { raw, enabled: raw.enabled === true, models };}+// The states that assert currently-available capacity. Only these need a+// verifiable freshness window; `unreachable`, `disabled`, `unsupported`,+// `unauthorized`, `unavailable` and `invalid` legitimately carry none, because+// the probe never got a snapshot to date.+const CAPACITY_CLAIMING_STATES = new Set(['ready', 'busy']);+/**- * Has the stored snapshot outlived the freshness window it was probed under?+ * Can this stored snapshot still be trusted to describe current capacity?** The probe records `freshUntil` from the provider's own `generatedAt +* staleAfterMs`, then the record sits on disk until the next peer poll. A@@ -99,10 +105,16 @@ export function peerMediaProviderConfig(peer) {* server would refuse to submit against it, which is precisely the* "stale must not look available" rule the capacity contract is built on.* Re-deriving expiry at render time closes that gap without a second probe.+ *+ * A missing or unparseable `freshUntil` fails CLOSED for the capacity-claiming+ * states: `Date.parse` returns NaN there, and treating NaN as "not expired"+ * would let a corrupt record advertise a provider with no verifiable window at+ * all — the fail-open version of the same bug.*/-function snapshotExpired(status, now) {+function freshnessFailed(status, now) {const freshUntil = Date.parse(status?.freshUntil);- return Number.isFinite(freshUntil) && freshUntil < now;+ if (Number.isFinite(freshUntil)) return freshUntil < now;+ return CAPACITY_CLAIMING_STATES.has(status?.state);}/**@@ -118,11 +130,11 @@ export function resolvePeerMediaReadiness(peer, { now = Date.now() } = {}) {const config = peerMediaProviderConfig(peer);const status = isRecord(peer?.mediaProviderStatus) ? peer.mediaProviderStatus : null;const snapshot = isRecord(status?.snapshot) ? status.snapshot : null;- // An expired snapshot is stale whatever the probe concluded — including the- // `ready` it concluded at probe time.+ // An unverifiable snapshot is stale whatever the probe concluded — including+ // the `ready` it concluded at probe time.const state = !status? null- : (snapshotExpired(status, now) ? 'stale' : status.state);+ : (freshnessFailed(status, now) ? 'stale' : status.state);const meta = peer?.enabled === false? PEER_DISABLED: !config.enableddiff --git a/client/src/lib/federatedMediaReadiness.test.js b/client/src/lib/federatedMediaReadiness.test.jsindex d78d2c50c4..5555b97aac 100644--- a/client/src/lib/federatedMediaReadiness.test.js+++ b/client/src/lib/federatedMediaReadiness.test.js@@ -53,6 +53,26 @@ describe('resolvePeerMediaReadiness', () => {expect(resolvePeerMediaReadiness(peer(), { now: NOW + 60_000 }).state).toBe('ready');});+ // Date.parse returns NaN for a missing or malformed value. Reading that as+ // "not expired" is the fail-OPEN version of the same bug: a ready snapshot+ // with no verifiable window is not a ready snapshot.+ it.each([undefined, null, '', 'not-a-date'])(+ 'treats a ready snapshot whose freshUntil is %p as stale',+ (freshUntil) => {+ const broken = peer({+ mediaProviderStatus: { ...peer().mediaProviderStatus, freshUntil },+ });+ expect(resolvePeerMediaReadiness(broken, { now: NOW }).state).toBe('stale');+ },+ );++ it('treats a busy snapshot with no verifiable window as stale', () => {+ const broken = peer({+ mediaProviderStatus: { ...peer().mediaProviderStatus, state: 'busy', freshUntil: null },+ });+ expect(resolvePeerMediaReadiness(broken, { now: NOW }).state).toBe('stale');+ });+it('leaves a state with no freshness window alone', () => {const unreachable = peer({mediaProviderStatus: { checkedAt: iso(-1000), state: 'unreachable', reason: 'timeout', freshUntil: null, snapshot: null },