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: start budget window at first post-warm-up sample, re-arm on resume inside frame loop, show effective Auto values in disabled panel fields (#2592)

[._.]/ Adam Eivy9f30f0dd1bf9
diff --git a/client/src/components/city/CityAdaptiveQuality.jsx b/client/src/components/city/CityAdaptiveQuality.jsx
index b86a13b280..0e8b1f9fee 100644
--- a/client/src/components/city/CityAdaptiveQuality.jsx
+++ b/client/src/components/city/CityAdaptiveQuality.jsx
@@ -28,32 +28,37 @@ export default function CityAdaptiveQuality({
onTierChange,
onDiagnostics,
}) {
+ const nowMs = () => (typeof performance !== 'undefined' ? performance.now() : 0);
const stateRef = useRef(null);
if (stateRef.current === null) {
- stateRef.current = createRenderBudget(startTier, typeof performance !== 'undefined' ? performance.now() : 0);
+ stateRef.current = createRenderBudget(startTier, nowMs());
}
+ // Tracks the last-seen resumeToken so the re-arm happens INSIDE the frame loop (below),
+ // before the first resumed frame is measured — a passive effect could run after the
+ // Canvas has already switched frameloop back to "always", letting one stale-window
+ // frame classify against pre-hide samples first.
+ const seenResumeRef = useRef(resumeToken);
// Re-start the budget whenever Auto (re)engages or the starting tier changes, so a
// Manual→Auto switch always begins at High rather than resuming a stale tier.
useEffect(() => {
if (!enabled) return;
- const now = typeof performance !== 'undefined' ? performance.now() : 0;
- stateRef.current = resetRenderBudget(stateRef.current, startTier, now);
+ stateRef.current = resetRenderBudget(stateRef.current, startTier, nowMs());
+ seenResumeRef.current = resumeToken;
onTierChange?.(getEffectiveTier(stateRef.current));
// Deps are intentionally just [enabled, startTier] — a fresh onTierChange callback
// identity must not reset the live budget mid-run.
}, [enabled, startTier]);
- // Re-arm warm-up on frameloop resume (tab became visible again).
- useEffect(() => {
- if (!enabled) return;
- const now = typeof performance !== 'undefined' ? performance.now() : 0;
- stateRef.current = restartWarmup(stateRef.current, now);
- }, [resumeToken]);
-
useFrame((_, delta) => {
if (!enabled) return;
- const now = typeof performance !== 'undefined' ? performance.now() : 0;
+ const now = nowMs();
+ // Frameloop just resumed from a hidden tab → re-arm warm-up before measuring, so the
+ // first sluggish post-resume frames (and any pre-hide streaks/samples) are dropped.
+ if (seenResumeRef.current !== resumeToken) {
+ seenResumeRef.current = resumeToken;
+ stateRef.current = restartWarmup(stateRef.current, now);
+ }
const prevTier = getEffectiveTier(stateRef.current);
stateRef.current = recordFrame(stateRef.current, { now, dt: delta * 1000 });
const nextTier = getEffectiveTier(stateRef.current);
diff --git a/client/src/components/city/CitySettingsPanel.jsx b/client/src/components/city/CitySettingsPanel.jsx
index bd097ae7dd..23d0eb5d11 100644
--- a/client/src/components/city/CitySettingsPanel.jsx
+++ b/client/src/components/city/CitySettingsPanel.jsx
@@ -123,6 +123,13 @@ export default function CitySettingsPanel({ qualityMode = 'manual', effectiveTie
else updateSetting('qualityPreset', key);
};
+ // In Auto mode the scene derives reflections + particle density from the effective
+ // tier, not the saved manual values — so the (disabled) controls display the effective
+ // preset's values and follow live tier changes rather than showing a stale manual value.
+ const effectivePreset = isAuto ? (QUALITY_PRESETS[effectiveTier] || QUALITY_PRESETS.high) : null;
+ const reflectionsValue = effectivePreset ? effectivePreset.reflectionsEnabled : settings.reflectionsEnabled;
+ const particleValue = effectivePreset ? effectivePreset.particleDensity : settings.particleDensity;
+
return (
<div className="absolute bottom-4 right-4 z-50 pointer-events-auto animate-in slide-in-from-bottom-4 duration-300">
<div
@@ -234,7 +241,7 @@ export default function CitySettingsPanel({ qualityMode = 'manual', effectiveTie
<SectionHeader title="VISUAL FX" subtitle={isAuto ? 'Reflections + density controlled by Auto' : 'Reflections and atmosphere'} />
<SettingToggle
label="REFLECTIONS"
- value={settings.reflectionsEnabled}
+ value={reflectionsValue}
onChange={(v) => updateSetting('reflectionsEnabled', v)}
description={isAuto ? 'Set automatically by Auto quality' : 'Wet street reflections and puddles'}
disabled={isAuto}
@@ -247,7 +254,7 @@ export default function CitySettingsPanel({ qualityMode = 'manual', effectiveTie
/>
<SettingSlider
label="PARTICLE DENSITY"
- value={settings.particleDensity}
+ value={particleValue}
onChange={(v) => updateSetting('particleDensity', v)}
min={0.25}
max={2}
diff --git a/client/src/utils/cityRenderBudget.js b/client/src/utils/cityRenderBudget.js
index a41c4937e4..c564be5707 100644
--- a/client/src/utils/cityRenderBudget.js
+++ b/client/src/utils/cityRenderBudget.js
@@ -106,11 +106,21 @@ export function recordFrame(state, sample, config = DEFAULT_RENDER_BUDGET_CONFIG
const inWarmup = now - state.startedAt < config.warmupMs;
const isGap = dt > config.maxFrameGapMs || dt <= 0;
- const samples = !inWarmup && !isGap ? state.samples.concat(dt) : state.samples;
+ // The window clock starts at its FIRST valid sample, not at reset time — otherwise the
+ // 1.2s 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 (!inWarmup && !isGap) {
+ if (samples.length === 0) windowStart = now;
+ samples = samples.concat(dt);
+ }
// Window still open — accumulate and return.
- if (now - state.windowStart < config.windowMs) {
- return { ...state, samples, windowClosed: false };
+ if (now - windowStart < config.windowMs) {
+ return { ...state, samples, windowStart, windowClosed: false };
}
// Window elapsed but no valid samples (e.g. entire window was warm-up or gaps):
diff --git a/client/src/utils/cityRenderBudget.test.js b/client/src/utils/cityRenderBudget.test.js
index ad65e45273..35a89af239 100644
--- a/client/src/utils/cityRenderBudget.test.js
+++ b/client/src/utils/cityRenderBudget.test.js
@@ -70,6 +70,25 @@ describe('cityRenderBudget — warm-up & gap rejection (ignored samples)', () =>
expect(s.samples.length).toBe(0);
expect(s.diagnostics.windows).toBe(0);
});
+
+ 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
+ // early because the reset time was long ago.
+ let s = createRenderBudget('high', 0);
+ const firstSampleAt = 5000; // arbitrary time past warm-up
+ let now = firstSampleAt;
+ let closed = null;
+ for (let i = 0; i < 500 && !closed; i += 1) {
+ now += 16;
+ s = recordFrame(s, { now, dt: 16 });
+ if (s.windowClosed) closed = now;
+ }
+ expect(closed).not.toBeNull();
+ // The window closed ~windowMs after the first sample, not ~windowMs after t=0.
+ expect(closed - firstSampleAt).toBeGreaterThanOrEqual(CFG.windowMs);
+ expect(closed - firstSampleAt).toBeLessThan(CFG.windowMs + 32);
+ });
});
describe('cityRenderBudget — downshift', () => {