Skip to content

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

fix: downshift on sustained slow frames, re-arm budget for app-count warm-ups, clear diagnostics on mode transitions (#2592)

[._.]/ Adam Eivy662d64a8ba92
diff --git a/client/src/components/city/CityScene.jsx b/client/src/components/city/CityScene.jsx
index 758e756088..b1083e4340 100644
--- a/client/src/components/city/CityScene.jsx
+++ b/client/src/components/city/CityScene.jsx
@@ -68,6 +68,10 @@ export default function CityScene({ apps, agentMap, onBuildingClick, onToggleCam
() => typeof document !== 'undefined' && document.visibilityState === 'hidden'
);
const [resumeToken, setResumeToken] = useState(0);
+ // Bumped whenever the scene warm-up (re)starts — startup, an app-count change, or a
+ // visibility resume — so the adaptive budget re-arms and ignores the 1.2s of artificially
+ // cheap (forced-Low) warm-up frames instead of banking them as headroom/erasing pressure.
+ const [budgetRearmToken, setBudgetRearmToken] = useState(0);
useVisibilityEvent(useCallback((state) => {
const hidden = state === 'hidden';
setDocumentHidden(hidden);
@@ -92,6 +96,7 @@ export default function CityScene({ apps, agentMap, onBuildingClick, onToggleCam
}
setStartupSettled(false);
+ setBudgetRearmToken(t => t + 1);
const timer = window.setTimeout(() => setStartupSettled(true), 1200);
return () => window.clearTimeout(timer);
}, [apps.length, photoMode, resumeToken]);
@@ -347,7 +352,7 @@ export default function CityScene({ apps, agentMap, onBuildingClick, onToggleCam
<CityAdaptiveQuality
enabled={autoQuality}
startTier={autoStartTier}
- resumeToken={resumeToken}
+ resumeToken={budgetRearmToken}
resetToken={autoResetToken}
diagnosticsEnabled={diagnosticsEnabled}
onTierChange={onAutoTierChange}
diff --git a/client/src/pages/CyberCity.jsx b/client/src/pages/CyberCity.jsx
index e793cdabef..22dc436bfb 100644
--- a/client/src/pages/CyberCity.jsx
+++ b/client/src/pages/CyberCity.jsx
@@ -67,9 +67,10 @@ function CyberCityInner() {
const [autoDiagnostics, setAutoDiagnostics] = useState(null);
const effectiveTier = qualityMode === 'auto' ? autoTier : (settings?.qualityPreset ?? 'high');
- // On RESET DEFAULTS, clear the stale local diagnostics readout; the adaptive budget
- // itself re-arms via CityScene's autoResetToken and re-reports tier + fresh samples.
- useEffect(() => { setAutoDiagnostics(null); }, [resetNonce]);
+ // Clear the stale local diagnostics readout whenever the budget re-arms: a RESET DEFAULTS
+ // (resetNonce) or a quality-mode transition (Manual↔Auto). The adaptive budget itself
+ // re-arms via CityScene and re-reports tier + fresh samples on the next window.
+ useEffect(() => { setAutoDiagnostics(null); }, [resetNonce, qualityMode]);
const sceneSettings = useMemo(() => {
const base = { ...settings, effectiveTier, skyTheme: 'cyberpunk', timeOfDay: cityTimeOfDay.presetKey };
diff --git a/client/src/utils/cityRenderBudget.js b/client/src/utils/cityRenderBudget.js
index 39c2c21a6a..091658676f 100644
--- a/client/src/utils/cityRenderBudget.js
+++ b/client/src/utils/cityRenderBudget.js
@@ -28,6 +28,10 @@ export const DEFAULT_RENDER_BUDGET_CONFIG = Object.freeze({
upshiftFrameMs: 18,
upshiftWindows: 5,
cooldownMs: 10000, // no further tier change for this long after a change
+ // Consecutive over-cutoff (gap) frames that mean "the scene itself is rendering slower
+ // than ~4fps" rather than a one-off suspension/GC pause. Reaching this synthesizes a
+ // pressure window so Auto can still downshift instead of discarding every sample forever.
+ sustainedGapFrames: 8,
});
export function tierToIndex(tier) {
@@ -62,6 +66,7 @@ export function createRenderBudget(startTier = 'high', now = 0) {
startedAt: now, // warm-up baseline
windowStart: now,
samples: [],
+ gapStreak: 0, // consecutive over-cutoff frames (sustained-slowness detector)
pressureStreak: 0,
headroomStreak: 0,
lastChangeAt: -Infinity, // first change is allowed immediately once a streak is met
@@ -85,72 +90,26 @@ export function restartWarmup(state, now) {
startedAt: now,
windowStart: now,
samples: [],
+ gapStreak: 0,
pressureStreak: 0,
headroomStreak: 0,
windowClosed: false,
};
}
-// Reset the whole machine to a fresh start at `startTier` (e.g. Manual→Auto switch, or
-// the Auto starting tier changing). Keeps the module pure — caller passes `now`.
-export function resetRenderBudget(state, startTier, now) {
- return createRenderBudget(startTier, now);
-}
-
-// Feed one frame. Returns a NEW state object (never mutates). `sample` is { now, dt }
-// in milliseconds. Frames during warm-up or with a gap above maxFrameGapMs are dropped
-// from the window (but still advance nothing else). A window is classified only once
-// `windowMs` of wall-clock has elapsed AND it holds at least one valid sample.
-export function recordFrame(state, sample, config = DEFAULT_RENDER_BUDGET_CONFIG) {
- const { now, dt } = sample;
- const inWarmup = now - state.startedAt < config.warmupMs;
- const isGap = dt > config.maxFrameGapMs || dt <= 0;
-
- // A frame inside the warm-up is dropped AND keeps the window clock pinned to now, so
- // the first real window starts measuring only once warm-up ends.
- if (inWarmup) {
- return { ...state, windowStart: now, samples: [], windowClosed: false };
- }
-
- // A >250ms gap (tab hidden, main-thread stall, GC) breaks the continuity of the current
- // rolling window: discard whatever partial window we'd accumulated and restart the clock
- // after the gap, so a lone pre-gap sample can't be classified as a full window.
- if (isGap) {
- return { ...state, windowStart: now, samples: [], windowClosed: false };
- }
-
- // The window clock starts at its FIRST valid sample, not at reset time — otherwise the
- // warm-up (or a long gap) eats most of the first 2s window and it closes with a fraction
- // of a window's samples yet still counts toward a streak. Pinning windowStart to the
- // first sample guarantees every classified window spans a full windowMs of measured
- // frames.
- let samples = state.samples;
- let windowStart = state.windowStart;
- if (samples.length === 0) windowStart = now;
- samples = samples.concat(dt);
-
- // Window still open — accumulate and return.
- if (now - windowStart < config.windowMs) {
- return { ...state, samples, windowStart, windowClosed: false };
- }
-
- const p75 = percentile(samples, config.percentile);
- const fps = 1000 / mean(samples);
-
+// Classify a completed window (real, or a synthetic pressure window from sustained gaps)
+// and apply the cooldown/hysteresis decision. Pure; returns the closed-window state.
+function classifyWindow(state, { p75, fps, sampleCount, now, windowStart }, config) {
let tierIndex = state.tierIndex;
let lastChangeAt = state.lastChangeAt;
// Cooldown eligibility keys on the window's START, not its close — a window that opened
- // during the cooldown but happens to close just after expiry is NOT fresh evidence. Only
- // windows that began fully after the cooldown ended count toward a streak, so a change
- // requires two (or five) *complete* post-cooldown windows.
+ // during the cooldown but happens to close just after expiry is NOT fresh evidence.
const cooledDown = windowStart - state.lastChangeAt >= config.cooldownMs;
- // Observation freeze during cooldown: windows classified inside the cooldown do NOT
- // bank a streak. Otherwise a streak accumulated while cooling would fire the instant
- // the cooldown expires, producing a quality "pop" — and, for a workload that straddles
- // the two thresholds (comfortable at tier N, over-budget at tier N+1), an endless
- // downshift↔upshift bounce. Streaks are only counted from windows observed entirely
- // after the last change, so a change requires fresh, post-cooldown evidence.
+ // Observation freeze during cooldown: windows classified inside the cooldown do NOT bank
+ // a streak, so a change requires fresh, post-cooldown evidence and a banked streak can't
+ // fire the instant the cooldown expires (which would pop, and could oscillate for a
+ // workload that straddles the two adjacent-tier thresholds).
let pressureStreak = 0;
let headroomStreak = 0;
if (cooledDown) {
@@ -179,6 +138,7 @@ export function recordFrame(state, sample, config = DEFAULT_RENDER_BUDGET_CONFIG
tierIndex,
windowStart: now,
samples: [],
+ gapStreak: 0,
pressureStreak,
headroomStreak,
lastChangeAt,
@@ -187,8 +147,63 @@ export function recordFrame(state, sample, config = DEFAULT_RENDER_BUDGET_CONFIG
effectiveTier: QUALITY_TIERS[tierIndex],
p75,
fps,
- sampleCount: samples.length,
+ sampleCount,
windows: state.diagnostics.windows + 1,
},
};
}
+
+// Reset the whole machine to a fresh start at `startTier` (e.g. Manual→Auto switch, or
+// the Auto starting tier changing). Keeps the module pure — caller passes `now`.
+export function resetRenderBudget(state, startTier, now) {
+ return createRenderBudget(startTier, now);
+}
+
+// Feed one frame. Returns a NEW state object (never mutates). `sample` is { now, dt }
+// in milliseconds. Frames during warm-up or with a gap above maxFrameGapMs are dropped
+// from the window (but still advance nothing else). A window is classified only once
+// `windowMs` of wall-clock has elapsed AND it holds at least one valid sample.
+export function recordFrame(state, sample, config = DEFAULT_RENDER_BUDGET_CONFIG) {
+ const { now, dt } = sample;
+ const inWarmup = now - state.startedAt < config.warmupMs;
+ const isGap = dt > config.maxFrameGapMs || dt <= 0;
+
+ // A frame inside the warm-up is dropped AND keeps the window clock pinned to now, so
+ // the first real window starts measuring only once warm-up ends.
+ if (inWarmup) {
+ return { ...state, windowStart: now, samples: [], gapStreak: 0, windowClosed: false };
+ }
+
+ // A >250ms gap (tab hidden, main-thread stall, GC) breaks the continuity of the current
+ // rolling window: discard whatever partial window we'd accumulated and restart the clock
+ // after the gap, so a lone pre-gap sample can't be classified as a full window.
+ if (isGap) {
+ const gapStreak = state.gapStreak + 1;
+ // Sustained slowness (the SCENE itself renders slower than the gap cutoff, frame after
+ // frame — not a one-off suspension) must still be able to downshift. After enough
+ // consecutive gaps, synthesize a pressure window instead of discarding forever.
+ if (gapStreak >= config.sustainedGapFrames) {
+ return classifyWindow(state, { p75: dt, fps: 1000 / dt, sampleCount: 0, now, windowStart: now }, config);
+ }
+ return { ...state, windowStart: now, samples: [], gapStreak, windowClosed: false };
+ }
+
+ // The window clock starts at its FIRST valid sample, not at reset time — otherwise the
+ // warm-up (or a long gap) eats most of the first 2s window and it closes with a fraction
+ // of a window's samples yet still counts toward a streak. Pinning windowStart to the
+ // first sample guarantees every classified window spans a full windowMs of measured
+ // frames.
+ let samples = state.samples;
+ let windowStart = state.windowStart;
+ if (samples.length === 0) windowStart = now;
+ samples = samples.concat(dt);
+
+ // Window still open — accumulate and return (a valid frame resets the gap streak).
+ if (now - windowStart < config.windowMs) {
+ return { ...state, samples, windowStart, gapStreak: 0, windowClosed: false };
+ }
+
+ const p75 = percentile(samples, config.percentile);
+ const fps = 1000 / mean(samples);
+ return classifyWindow(state, { p75, fps, sampleCount: samples.length, now, windowStart }, config);
+}
diff --git a/client/src/utils/cityRenderBudget.test.js b/client/src/utils/cityRenderBudget.test.js
index 98306097bc..5c5ac56e18 100644
--- a/client/src/utils/cityRenderBudget.test.js
+++ b/client/src/utils/cityRenderBudget.test.js
@@ -84,6 +84,26 @@ describe('cityRenderBudget — warm-up & gap rejection (ignored samples)', () =>
expect(s.samples.length).toBe(0);
});
+ it('downshifts under sustained slow rendering (every frame over the gap cutoff)', () => {
+ // The scene itself renders < ~4fps: every delta exceeds maxFrameGapMs, so no normal
+ // window ever closes. Sustained gaps must still be able to downshift.
+ let s = createRenderBudget('high', 0);
+ let now = CFG.warmupMs + 1;
+ const slowDt = CFG.maxFrameGapMs + 100; // 350ms/frame, ~3fps
+ // Two synthetic pressure windows' worth of sustained gaps → one downshift.
+ for (let i = 0; i < CFG.sustainedGapFrames * 2 + 2; i += 1) {
+ now += slowDt;
+ s = recordFrame(s, { now, dt: slowDt });
+ }
+ expect(getEffectiveTier(s)).toBe('medium');
+ // A single isolated gap (streak resets on the next valid frame) does NOT downshift.
+ let s2 = createRenderBudget('high', 0);
+ s2 = recordFrame(s2, { now: CFG.warmupMs + 100, dt: 5000 }); // one-off suspension
+ s2 = recordFrame(s2, { now: CFG.warmupMs + 120, dt: 16 }); // resumes normally
+ expect(s2.gapStreak).toBe(0);
+ expect(getEffectiveTier(s2)).toBe('high');
+ });
+
it('starts the window clock at the first post-warm-up sample (full first window)', () => {
// The budget resets at t=0 but the first real frame lands well after warm-up. The
// first classified window must still span a full windowMs of samples, not close