From bb802c6cd5ec0bbdd4d00f474c9a755eb65880c2 Mon Sep 17 00:00:00 2001 From: eust-w Date: Thu, 25 Jun 2026 12:34:32 +0800 Subject: [PATCH] Use conditional state polling --- docs/product-readiness.md | 61 ++++++++++++++----------- scripts/product-readiness-gate.mjs | 9 +++- scripts/verify-real-backend-editing.mjs | 34 ++++++++++++++ server/headless_motrix_bridge.py | 27 ++++++++++- src/main.ts | 5 +- src/state/StateClient.ts | 23 +++++++++- 6 files changed, 127 insertions(+), 32 deletions(-) diff --git a/docs/product-readiness.md b/docs/product-readiness.md index 826b99c..75b5f3a 100644 --- a/docs/product-readiness.md +++ b/docs/product-readiness.md @@ -56,9 +56,10 @@ Important properties: panels compare generated content before touching `innerHTML`, and active-body polling reuses the computed `/state?bodies=active` URL after the first full state frame. -- Repeated paused state frames are elided before JSON parsing when the backend - exposes `X-Motrix-State-Hash`; the older semantic frame comparison remains as - a fallback after parsing. +- Repeated paused state frames use conditional `/state` requests. The browser + sends `If-None-Match`, the backend can return `304 Not Modified` with no JSON + body, and the older semantic frame comparison remains as a fallback after + parsing. - Large-scene diagnostics use adaptive stats refresh: user-visible events still force an immediate panel update, while background periodic refreshes drop to a lower cadence for large or paused scenes so long diagnostic strings do not @@ -181,9 +182,10 @@ The product acceptance gate must pass with zero failures. It checks: - Runtime body-state apply/skip diagnostics, proving unchanged fixed shelf and inventory states are not repeatedly applied to the scene graph. - Runtime state-hash diagnostics, proving unchanged `/state` frames can be - skipped before JSON parsing while paused. The semantic duplicate-frame check - remains as a compatibility fallback for older bridges that do not expose - `X-Motrix-State-Hash`. + skipped before JSON parsing while paused. The gate also requires real + `304 Not Modified` responses so unchanged frames do not resend JSON bodies. + The semantic duplicate-frame check remains as a compatibility fallback for + older bridges that do not expose `X-Motrix-State-Hash`. - WebGL readback policy, proving the renderer does not keep `preserveDrawingBuffer` enabled during normal runtime and only renders for explicit screenshot or pixel-readback requests. @@ -263,20 +265,19 @@ Representative product gate baseline for the current thresholds: - Real backend doctor: `14 passed / 0 warned / 0 failed` - Real backend startup: synced and restarted the Wuying bridge, checked the local tunnel, and passed the editing verifier before browser acceptance. -- Real backend compose/editing: `42 passed / 0 warned / 0 failed` +- Real backend compose/editing: `43 passed / 0 warned / 0 failed` - Physical compose: `/compose` returned `objectCount=636`, `bodyCount=573`, and `4/4` product-gate primitive bodies in inline manifest, fetched manifest, - and live state. The latest representative gate reused the active compose - cache and measured `118ms`. + and live state. The latest representative gate measured `371ms`. - Repeated physical compose cache: a second identical `/compose` request returns `cached=true` with the same compose key; the current real-backend verification - measured `126ms`. + measured `156ms`. - Static shelf stability: selected shelf anchors had `max drift 0.00000m` after a 2500 ms resume window, preventing the shelf-toppling regression. - Fixed inventory protection: a pre-stocked product body returned HTTP `400` for `set_body_pose`, proving fixed stock is not accidentally treated as a movable free body. -- Dynamic object drop: a lifted user-added cube dropped `0.44816m` after resume, +- Dynamic object drop: a lifted user-added cube dropped `0.48012m` after resume, proving dynamic props still use backend physics. - Active state payload: full `/state` returns `602` MotrixSim body/link poses for initial binding, while `/state?bodies=active` returns `34` active poses @@ -284,8 +285,11 @@ Representative product gate baseline for the current thresholds: active payload still includes `4/4` user dynamic bodies. - State hash validation: full paused `/state` exposes `X-Motrix-State-Hash`/`ETag`, and repeated paused full-state fetches kept the - same hash prefix `4b7f1eec5f38`. The active-state response exposed hash prefix - `1ccd5b700a51`. + same hash prefix `383432332ba3`. The active-state response exposed hash prefix + `bd231dc24ea7`. +- Conditional state validation: the real-backend verifier sends + `If-None-Match` with the paused full-state ETag and expects HTTP `304` with an + empty body before browser acceptance starts. - Object physics tiers: the diagnostics panel and exported acceptance artifacts count backend dynamic, backend fixed, backend state-only, and visual-only objects. The product physical scene currently reports `dynamic=4`, @@ -363,11 +367,12 @@ Scene evidence: DPR at `1.00`, kept shadows off, instanced `568` fixed objects into `16` batches, and reduced state polling to `34/602` active poses after the first full frame. -- State hash skip: the large-scene desktop run recorded `hash=7/5/0`, so `5` - of `7` state responses were skipped before JSON parsing. The adaptive polling - check recorded `hash=5/3/0` while paused. -- Control latency: resume backend/DOM sync measured `57ms/50ms`; pause - backend/DOM sync measured `49ms/42ms`. +- Conditional state skip: the large-scene desktop run recorded `hash=7/5/0` + and `notModified=5`, so `5` of `7` state polls avoided retransmitting JSON + bodies. The adaptive polling check recorded `hash=5/3/0` and + `notModified=3` while paused. +- Control latency: resume backend/DOM sync measured `249ms/51ms`; pause + backend/DOM sync measured `256ms/58ms`. - Mobile preview: `636 objects / 635 bindings / 30 pixels` - Mobile acceptance: `11 通过 / 0 警告 / 0 失败,共 11 项` - Mobile acceptance telemetry: `0 ms / 11-0-0` @@ -467,11 +472,12 @@ Scene evidence: not forward the frame to the Three.js preview, avoiding body merge, binding traversal, matrix checks, diagnostics writes, and render dirties for repeated paused-state frames. -- State-hash parse elision: the MotrixSim bridge emits - `X-Motrix-State-Hash` and `ETag` for `/state` responses. When the hash is - unchanged, the browser skips `response.json()` entirely and counts the frame - as a duplicate before it reaches the semantic fallback, reducing main-thread - JSON parse work in paused or otherwise unchanged scenes. +- Conditional state elision: the MotrixSim bridge emits `X-Motrix-State-Hash` + and `ETag` for `/state` responses. The browser sends `If-None-Match` on later + polls, accepts `304 Not Modified` as a duplicate frame, and avoids both JSON + body transfer and `response.json()` parsing. If an older bridge only returns + `200`, the browser still uses the hash header to skip parsing unchanged + frames. - Incremental body-state merge: active-state frames mutate the retained body map by payload key instead of spreading/copying the full `602`-body map on every poll, reducing steady-state allocation and GC pressure while preserving full @@ -561,10 +567,11 @@ Bundle evidence: forwarded, and skipped poll frames as a fallback for older backends without hash headers, proving repeated paused state does not reach the renderer and UI panels. -- State-hash gate: the large-scene and adaptive-polling checks require hashed - state frames, at least one hash-level duplicate skip, and fewer parsed frames - than actual poll requests, proving unchanged frames are discarded before - JSON parsing when the backend supports hash headers. +- State-hash/304 gate: the large-scene and adaptive-polling checks require + hashed state frames, at least one hash-level duplicate skip, at least one + `304 Not Modified`, and fewer parsed frames than actual poll requests, + proving unchanged frames are discarded before JSON transfer/parsing when the + backend supports conditional state requests. - UI refresh gate: `browser-large-scene-performance-mode` now checks duplicate text-write skips, stable text-selector cache hits, and diagnostics-panel refresh skips, proving high-frequency `/state` polling does not force diff --git a/scripts/product-readiness-gate.mjs b/scripts/product-readiness-gate.mjs index 12557b4..96afdf6 100644 --- a/scripts/product-readiness-gate.mjs +++ b/scripts/product-readiness-gate.mjs @@ -735,6 +735,7 @@ async function readBrowserStats(page) { stateHashedPollFrameCount: numberData("stateHashedPollFrameCount"), stateSkippedDuplicateHashPollFrameCount: numberData("stateSkippedDuplicateHashPollFrameCount"), stateSkippedDuplicateSemanticPollFrameCount: numberData("stateSkippedDuplicateSemanticPollFrameCount"), + stateNotModifiedPollFrameCount: numberData("stateNotModifiedPollFrameCount"), batchedSceneRootCount: numberData("batchedSceneRootCount"), sceneRootBatchCount: numberData("sceneRootBatchCount"), objectPhysicsProfileCacheHitCount: numberData("objectPhysicsProfileCacheHitCount"), @@ -807,6 +808,7 @@ async function waitForAdaptiveStatePolling(page) { const skippedDuplicates = Number(data.stateSkippedDuplicatePollFrameCount ?? "0"); const hashedFrames = Number(data.stateHashedPollFrameCount ?? "0"); const hashSkippedFrames = Number(data.stateSkippedDuplicateHashPollFrameCount ?? "0"); + const notModifiedFrames = Number(data.stateNotModifiedPollFrameCount ?? "0"); return base > 0 && base <= 80 && paused >= 2000 @@ -818,6 +820,7 @@ async function waitForAdaptiveStatePolling(page) { && parsedFrames >= forwardedFrames && hashedFrames >= pausedPolls && hashSkippedFrames > 0 + && notModifiedFrames > 0 && parsedFrames < pausedPolls; }, [beforePausedPolls, beforeDuplicateSkips], { polling: 100, timeout: 10000 }); const stats = await readBrowserStats(page); @@ -833,10 +836,11 @@ async function waitForAdaptiveStatePolling(page) { && (perf.stateParsedPollFrameCount ?? 0) >= (perf.stateForwardedPollFrameCount ?? Number.POSITIVE_INFINITY) && (perf.stateHashedPollFrameCount ?? 0) >= (perf.statePausedPollRequestCount ?? Number.POSITIVE_INFINITY) && (perf.stateSkippedDuplicateHashPollFrameCount ?? 0) > 0 + && (perf.stateNotModifiedPollFrameCount ?? 0) > 0 && (perf.stateParsedPollFrameCount ?? Number.POSITIVE_INFINITY) < (perf.statePausedPollRequestCount ?? 0), before, stats, - detail: `poll=${perf.stateBasePollIntervalMs ?? "-"}/${perf.statePausedPollIntervalMs ?? "-"}/${perf.stateLastScheduledPollIntervalMs ?? "-"}ms requests=${perf.statePollRequestCount ?? "-"}/${perf.stateActivePollRequestCount ?? "-"}/${perf.statePausedPollRequestCount ?? "-"} semantic=${perf.stateParsedPollFrameCount ?? "-"}/${perf.stateForwardedPollFrameCount ?? "-"}/${perf.stateSkippedDuplicatePollFrameCount ?? "-"} hash=${perf.stateHashedPollFrameCount ?? "-"}/${perf.stateSkippedDuplicateHashPollFrameCount ?? "-"}/${perf.stateSkippedDuplicateSemanticPollFrameCount ?? "-"} before=${beforePausedPolls}/${beforeDuplicateSkips}`, + detail: `poll=${perf.stateBasePollIntervalMs ?? "-"}/${perf.statePausedPollIntervalMs ?? "-"}/${perf.stateLastScheduledPollIntervalMs ?? "-"}ms requests=${perf.statePollRequestCount ?? "-"}/${perf.stateActivePollRequestCount ?? "-"}/${perf.statePausedPollRequestCount ?? "-"} semantic=${perf.stateParsedPollFrameCount ?? "-"}/${perf.stateForwardedPollFrameCount ?? "-"}/${perf.stateSkippedDuplicatePollFrameCount ?? "-"} hash=${perf.stateHashedPollFrameCount ?? "-"}/${perf.stateSkippedDuplicateHashPollFrameCount ?? "-"}/${perf.stateSkippedDuplicateSemanticPollFrameCount ?? "-"} notModified=${perf.stateNotModifiedPollFrameCount ?? "-"} before=${beforePausedPolls}/${beforeDuplicateSkips}`, }; } @@ -1854,6 +1858,7 @@ async function verifyBrowser() { && (renderPerformance.stateParsedPollFrameCount ?? 0) >= (renderPerformance.stateForwardedPollFrameCount ?? Number.POSITIVE_INFINITY) && (renderPerformance.stateHashedPollFrameCount ?? 0) >= (renderPerformance.statePausedPollRequestCount ?? Number.POSITIVE_INFINITY) && (renderPerformance.stateSkippedDuplicateHashPollFrameCount ?? 0) > 0 + && (renderPerformance.stateNotModifiedPollFrameCount ?? 0) > 0 && (renderPerformance.stateParsedPollFrameCount ?? Number.POSITIVE_INFINITY) < (renderPerformance.statePollRequestCount ?? 0) && (renderPerformance.batchedSceneRootCount ?? Number.POSITIVE_INFINITY) <= maxInstancedSceneRoots && (renderPerformance.sceneRootBatchCount ?? 0) > 1 @@ -1874,7 +1879,7 @@ async function verifyBrowser() { && (renderPerformance.hoverPickTargetCount ?? Number.POSITIVE_INFINITY) <= maxHoverPickTargets && (renderPerformance.skippedBodyStateCount ?? 0) > 0 && desktopStats.diagnostics.renderPerformance.includes("large-scene"), - `mode=${renderPerformance.largeSceneMode} fps=${renderPerformance.targetRenderFps ?? "-"} statsRefresh=${renderPerformance.statsRefreshIntervalMs ?? "-"}ms ${renderPerformance.statsRefreshRenderCount ?? "-"}/${renderPerformance.statsRefreshSkipCount ?? "-"} dpr=${renderPerformance.renderPixelRatio ?? "-"} buffer=${renderPerformance.preserveDrawingBufferEnabled} readback=${renderPerformance.readbackRenderCount ?? "-"} shadows=${renderPerformance.shadowsEnabled} hover=${renderPerformance.hoverPickIntervalMs ?? "-"} skipped=${renderPerformance.renderSkipCount ?? "-"} frozen=${renderPerformance.frozenStaticObjectCount ?? "-"} prefrozen=${renderPerformance.preFrozenMeshCloneCount ?? "-"} spinSkip=${renderPerformance.spinningScanSkippedCount ?? "-"} instanced=${renderPerformance.instancedMeshObjectCount ?? "-"}/${renderPerformance.instancedMeshBatchCount ?? "-"}/${renderPerformance.instancedMeshDrawCount ?? "-"} matrices=${renderPerformance.instancedMeshMatrixUpdateCount ?? "-"} cull=${renderPerformance.instancedMeshFrustumCulledCount ?? "-"} static=${renderPerformance.instancedMeshStaticMatrixCount ?? "-"} elidedLocal=${renderPerformance.instancedLocalObjectElisionCount ?? "-"} manualRoots=${renderPerformance.manualMatrixRootCount ?? "-"} manualUpdates=${renderPerformance.manualMatrixUpdateCount ?? "-"} primitiveGeometries=${renderPerformance.primitiveGeometryCount ?? "-"} meshCache=${renderPerformance.meshCacheSourceCount ?? "-"} hit=${renderPerformance.meshCacheHitCount ?? "-"} miss=${renderPerformance.meshCacheMissCount ?? "-"} retained=${renderPerformance.retainedMeshGeometryCount ?? "-"}g/${renderPerformance.retainedMeshMaterialCount ?? "-"}m bounds=${renderPerformance.sceneObjectBoundsCount ?? "-"} fast=${renderPerformance.fastPickProxyBoundsCount ?? "-"} scratch=${renderPerformance.pickBoundsScratchTransformCount ?? "-"} source=${renderPerformance.meshSourceBoundsCount ?? "-"} hoverProxy=${renderPerformance.hoverProxyBoundsHitCount ?? "-"} cached=${renderPerformance.hoverCachedBoundsHitCount ?? "-"}/fallback=${renderPerformance.hoverRootBoundsFallbackCount ?? "-"} cameraTraversals=${renderPerformance.cameraFitTraversalCount ?? "-"} sceneBatches=${renderPerformance.sceneRootBatchCount ?? "-"}/${renderPerformance.batchedSceneRootCount ?? "-"} maxSceneRoots=${maxInstancedSceneRoots} maxBatches=${maxProgressiveSceneRootBatches} suppressed=${renderPerformance.sceneLoadSuppressedRenderCount ?? "-"} yields=${renderPerformance.sceneBuildYieldCount ?? "-"} statePayload=${renderPerformance.statePayloadBodyCount ?? "-"}/${renderPerformance.mergedBodyStateCount ?? "-"} reduced=${renderPerformance.reducedStateFrameCount ?? "-"} incremental=${renderPerformance.incrementalBodyMergeCount ?? "-"} poll=${renderPerformance.stateBasePollIntervalMs ?? "-"}/${renderPerformance.statePausedPollIntervalMs ?? "-"}/${renderPerformance.stateLastScheduledPollIntervalMs ?? "-"}ms ${renderPerformance.statePollRequestCount ?? "-"}/${renderPerformance.stateActivePollRequestCount ?? "-"}/${renderPerformance.statePausedPollRequestCount ?? "-"} semantic=${renderPerformance.stateParsedPollFrameCount ?? "-"}/${renderPerformance.stateForwardedPollFrameCount ?? "-"}/${renderPerformance.stateSkippedDuplicatePollFrameCount ?? "-"} hash=${renderPerformance.stateHashedPollFrameCount ?? "-"}/${renderPerformance.stateSkippedDuplicateHashPollFrameCount ?? "-"}/${renderPerformance.stateSkippedDuplicateSemanticPollFrameCount ?? "-"} uiCache=physics ${renderPerformance.objectPhysicsProfileCacheHitCount ?? "-"}/${renderPerformance.objectPhysicsProfileCacheMissCount ?? "-"} builder ${renderPerformance.builderPhysicsProfileCacheHitCount ?? "-"}/${renderPerformance.builderPhysicsProfileCacheMissCount ?? "-"} text ${renderPerformance.textWriteCount ?? "-"}/${renderPerformance.textWriteSkipCount ?? "-"} selector ${renderPerformance.textSelectorCacheHitCount ?? "-"}/${renderPerformance.textSelectorCacheMissCount ?? "-"} diagnostics ${renderPerformance.diagnosticsPanelRenderCount ?? "-"}/${renderPerformance.diagnosticsPanelSkipCount ?? "-"} builderPanels=${renderPerformance.builderPanelRenderCount ?? "-"}/${renderPerformance.builderPanelDeferredRenderCount ?? "-"}/${renderPerformance.builderPanelCoalescedRenderCount ?? "-"} pickTargets=${renderPerformance.hoverPickTargetCount ?? "-"}/${renderPerformance.pickProxyCount ?? "-"} hoverBudget=${maxHoverPickTargets} stateApply=${renderPerformance.appliedBodyStateCount ?? "-"}/skip=${renderPerformance.skippedBodyStateCount ?? "-"} unchanged=${renderPerformance.unchangedFrameCount ?? "-"} released=${renderPerformance.releasedGeometryCount ?? "-"}g/${renderPerformance.releasedMaterialCount ?? "-"}m/${renderPerformance.releasedTextureCount ?? "-"}t; ${desktopStats.diagnostics.renderPerformance}`, + `mode=${renderPerformance.largeSceneMode} fps=${renderPerformance.targetRenderFps ?? "-"} statsRefresh=${renderPerformance.statsRefreshIntervalMs ?? "-"}ms ${renderPerformance.statsRefreshRenderCount ?? "-"}/${renderPerformance.statsRefreshSkipCount ?? "-"} dpr=${renderPerformance.renderPixelRatio ?? "-"} buffer=${renderPerformance.preserveDrawingBufferEnabled} readback=${renderPerformance.readbackRenderCount ?? "-"} shadows=${renderPerformance.shadowsEnabled} hover=${renderPerformance.hoverPickIntervalMs ?? "-"} skipped=${renderPerformance.renderSkipCount ?? "-"} frozen=${renderPerformance.frozenStaticObjectCount ?? "-"} prefrozen=${renderPerformance.preFrozenMeshCloneCount ?? "-"} spinSkip=${renderPerformance.spinningScanSkippedCount ?? "-"} instanced=${renderPerformance.instancedMeshObjectCount ?? "-"}/${renderPerformance.instancedMeshBatchCount ?? "-"}/${renderPerformance.instancedMeshDrawCount ?? "-"} matrices=${renderPerformance.instancedMeshMatrixUpdateCount ?? "-"} cull=${renderPerformance.instancedMeshFrustumCulledCount ?? "-"} static=${renderPerformance.instancedMeshStaticMatrixCount ?? "-"} elidedLocal=${renderPerformance.instancedLocalObjectElisionCount ?? "-"} manualRoots=${renderPerformance.manualMatrixRootCount ?? "-"} manualUpdates=${renderPerformance.manualMatrixUpdateCount ?? "-"} primitiveGeometries=${renderPerformance.primitiveGeometryCount ?? "-"} meshCache=${renderPerformance.meshCacheSourceCount ?? "-"} hit=${renderPerformance.meshCacheHitCount ?? "-"} miss=${renderPerformance.meshCacheMissCount ?? "-"} retained=${renderPerformance.retainedMeshGeometryCount ?? "-"}g/${renderPerformance.retainedMeshMaterialCount ?? "-"}m bounds=${renderPerformance.sceneObjectBoundsCount ?? "-"} fast=${renderPerformance.fastPickProxyBoundsCount ?? "-"} scratch=${renderPerformance.pickBoundsScratchTransformCount ?? "-"} source=${renderPerformance.meshSourceBoundsCount ?? "-"} hoverProxy=${renderPerformance.hoverProxyBoundsHitCount ?? "-"} cached=${renderPerformance.hoverCachedBoundsHitCount ?? "-"}/fallback=${renderPerformance.hoverRootBoundsFallbackCount ?? "-"} cameraTraversals=${renderPerformance.cameraFitTraversalCount ?? "-"} sceneBatches=${renderPerformance.sceneRootBatchCount ?? "-"}/${renderPerformance.batchedSceneRootCount ?? "-"} maxSceneRoots=${maxInstancedSceneRoots} maxBatches=${maxProgressiveSceneRootBatches} suppressed=${renderPerformance.sceneLoadSuppressedRenderCount ?? "-"} yields=${renderPerformance.sceneBuildYieldCount ?? "-"} statePayload=${renderPerformance.statePayloadBodyCount ?? "-"}/${renderPerformance.mergedBodyStateCount ?? "-"} reduced=${renderPerformance.reducedStateFrameCount ?? "-"} incremental=${renderPerformance.incrementalBodyMergeCount ?? "-"} poll=${renderPerformance.stateBasePollIntervalMs ?? "-"}/${renderPerformance.statePausedPollIntervalMs ?? "-"}/${renderPerformance.stateLastScheduledPollIntervalMs ?? "-"}ms ${renderPerformance.statePollRequestCount ?? "-"}/${renderPerformance.stateActivePollRequestCount ?? "-"}/${renderPerformance.statePausedPollRequestCount ?? "-"} semantic=${renderPerformance.stateParsedPollFrameCount ?? "-"}/${renderPerformance.stateForwardedPollFrameCount ?? "-"}/${renderPerformance.stateSkippedDuplicatePollFrameCount ?? "-"} hash=${renderPerformance.stateHashedPollFrameCount ?? "-"}/${renderPerformance.stateSkippedDuplicateHashPollFrameCount ?? "-"}/${renderPerformance.stateSkippedDuplicateSemanticPollFrameCount ?? "-"} notModified=${renderPerformance.stateNotModifiedPollFrameCount ?? "-"} uiCache=physics ${renderPerformance.objectPhysicsProfileCacheHitCount ?? "-"}/${renderPerformance.objectPhysicsProfileCacheMissCount ?? "-"} builder ${renderPerformance.builderPhysicsProfileCacheHitCount ?? "-"}/${renderPerformance.builderPhysicsProfileCacheMissCount ?? "-"} text ${renderPerformance.textWriteCount ?? "-"}/${renderPerformance.textWriteSkipCount ?? "-"} selector ${renderPerformance.textSelectorCacheHitCount ?? "-"}/${renderPerformance.textSelectorCacheMissCount ?? "-"} diagnostics ${renderPerformance.diagnosticsPanelRenderCount ?? "-"}/${renderPerformance.diagnosticsPanelSkipCount ?? "-"} builderPanels=${renderPerformance.builderPanelRenderCount ?? "-"}/${renderPerformance.builderPanelDeferredRenderCount ?? "-"}/${renderPerformance.builderPanelCoalescedRenderCount ?? "-"} pickTargets=${renderPerformance.hoverPickTargetCount ?? "-"}/${renderPerformance.pickProxyCount ?? "-"} hoverBudget=${maxHoverPickTargets} stateApply=${renderPerformance.appliedBodyStateCount ?? "-"}/skip=${renderPerformance.skippedBodyStateCount ?? "-"} unchanged=${renderPerformance.unchangedFrameCount ?? "-"} released=${renderPerformance.releasedGeometryCount ?? "-"}g/${renderPerformance.releasedMaterialCount ?? "-"}m/${renderPerformance.releasedTextureCount ?? "-"}t; ${desktopStats.diagnostics.renderPerformance}`, ); check( "browser-debug-capture-png", diff --git a/scripts/verify-real-backend-editing.mjs b/scripts/verify-real-backend-editing.mjs index b6f159f..944f310 100644 --- a/scripts/verify-real-backend-editing.mjs +++ b/scripts/verify-real-backend-editing.mjs @@ -66,6 +66,29 @@ async function fetchJson(url, options = {}, timeoutMs = fetchTimeoutMs) { } } +async function fetchText(url, options = {}, timeoutMs = fetchTimeoutMs) { + const started = performance.now(); + const controller = new AbortController(); + const timeout = setTimeout(() => controller.abort(), timeoutMs); + try { + const response = await fetch(url, { + cache: "no-store", + ...options, + signal: controller.signal, + }); + return { + ok: response.ok, + status: response.status, + url: response.url, + elapsedMs: Math.round(performance.now() - started), + headers: Object.fromEntries(response.headers.entries()), + text: await response.text(), + }; + } finally { + clearTimeout(timeout); + } +} + async function postControl(controlUrl, action, payload = {}, timeoutMs = controlTimeoutMs) { return fetchJson(controlUrl, { method: "POST", @@ -125,6 +148,7 @@ let composeCacheResult; let initialState; let initialStateHash; let repeatInitialStateHash; +let conditionalStateResponse; let activeState; let activeStateResponse; let activeStateHash; @@ -243,6 +267,9 @@ try { initialStateHash = stateHashFromHeaders(stateResponse.headers); const repeatedStateResponse = await fetchJson(stateUrl, {}, stateTimeoutMs); repeatInitialStateHash = stateHashFromHeaders(repeatedStateResponse.headers); + conditionalStateResponse = initialStateHash + ? await fetchText(stateUrl, { headers: { "If-None-Match": `"${initialStateHash}"` } }, stateTimeoutMs) + : undefined; const bodyCount = Object.keys(initialState?.bodies ?? {}).length; checks.push(check("state", stateResponse.ok && bodyCount > 0, `${bodyCount} bodies in ${stateResponse.elapsedMs}ms`)); checks.push(check( @@ -255,6 +282,11 @@ try { Boolean(initialStateHash) && initialStateHash === repeatInitialStateHash, `first=${initialStateHash?.slice(0, 12) ?? "missing"} repeat=${repeatInitialStateHash?.slice(0, 12) ?? "missing"}`, )); + checks.push(check( + "paused-state-not-modified", + conditionalStateResponse?.status === 304 && conditionalStateResponse.text.length === 0, + `status=${conditionalStateResponse?.status ?? "missing"} bytes=${conditionalStateResponse?.text.length ?? "-"} hash=${stateHashFromHeaders(conditionalStateResponse?.headers)?.slice(0, 12) ?? "missing"}`, + )); checks.push(check("state-paused", initialState?.paused === true, `paused=${initialState?.paused}`)); const pelvisPose = initialState?.bodies?.pelvis?.pos; checks.push(check( @@ -465,6 +497,8 @@ const report = { initialPaused: initialState?.paused, initialStateHash, repeatInitialStateHash, + conditionalStatus: conditionalStateResponse?.status, + conditionalBytes: conditionalStateResponse?.text.length, activeStateHash, initialBodyCount: Object.keys(initialState?.bodies ?? {}).length, activeBodyCount: Object.keys(activeState?.bodies ?? {}).length, diff --git a/server/headless_motrix_bridge.py b/server/headless_motrix_bridge.py index 99a3fa1..69aecf7 100644 --- a/server/headless_motrix_bridge.py +++ b/server/headless_motrix_bridge.py @@ -477,6 +477,18 @@ def semantic_state_hash(payload: dict[str, Any]) -> str: return hashlib.sha256(body).hexdigest() +def normalize_etag(value: str) -> str: + return value.strip().removeprefix("W/").strip('"') + + +def if_none_match_contains(header_value: str | None, state_hash: str) -> bool: + if not header_value: + return False + if header_value.strip() == "*": + return True + return any(normalize_etag(part) == state_hash for part in header_value.split(",")) + + def write_pose_adjusted_mjcf(source_path: Path, target_path: Path, pose: list[float]) -> Path: tree = ET.parse(source_path) root = tree.getroot() @@ -1364,6 +1376,16 @@ def main() -> int: self.end_headers() self.wfile.write(body) + def send_not_modified(self, state_hash: str) -> None: + self.send_response(304) + self.send_header("Access-Control-Allow-Origin", "*") + self.send_header("Access-Control-Expose-Headers", "X-Motrix-State-Hash, ETag") + self.send_header("Cache-Control", "no-store") + self.send_header("X-Motrix-State-Hash", state_hash) + self.send_header("ETag", f'"{state_hash}"') + self.send_header("Content-Length", "0") + self.end_headers() + def send_asset(self, file_path: Path) -> None: if not file_path.is_file(): self.send_json(404, {"ok": False, "error": "asset not found"}) @@ -1382,7 +1404,7 @@ def main() -> int: self.send_response(204) self.send_header("Access-Control-Allow-Origin", "*") self.send_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS") - self.send_header("Access-Control-Allow-Headers", "Content-Type") + self.send_header("Access-Control-Allow-Headers", "Content-Type, If-None-Match") self.end_headers() def do_POST(self) -> None: @@ -1436,6 +1458,9 @@ def main() -> int: body_mode = "full" payload = active_runtime.snapshot(compact=compact, body_mode=body_mode) state_hash = semantic_state_hash(payload) + if if_none_match_contains(self.headers.get("If-None-Match"), state_hash): + self.send_not_modified(state_hash) + return self.send_json(200, payload, { "X-Motrix-State-Hash": state_hash, "ETag": f'"{state_hash}"', diff --git a/src/main.ts b/src/main.ts index 14b8257..15849e2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -231,6 +231,7 @@ interface RuntimeDiagnostics { stateHashedPollFrameCount: number; stateSkippedDuplicateHashPollFrameCount: number; stateSkippedDuplicateSemanticPollFrameCount: number; + stateNotModifiedPollFrameCount: number; batchedSceneRootCount: number; sceneRootBatchCount: number; objectPhysicsProfileCacheHitCount: number; @@ -2721,6 +2722,7 @@ function currentRuntimeDiagnostics( stateHashedPollFrameCount: stateStats.hashedPollFrameCount, stateSkippedDuplicateHashPollFrameCount: stateStats.skippedDuplicateHashPollFrameCount, stateSkippedDuplicateSemanticPollFrameCount: stateStats.skippedDuplicateSemanticPollFrameCount, + stateNotModifiedPollFrameCount: stateStats.notModifiedPollFrameCount, batchedSceneRootCount: stats.batchedSceneRootCount, sceneRootBatchCount: stats.sceneRootBatchCount, objectPhysicsProfileCacheHitCount, @@ -2797,7 +2799,7 @@ function currentDiagnosticsReport(): string { function renderPerformanceLabel(stats: RuntimeDiagnostics["stats"]): string { const mode = stats.largeSceneMode ? "large-scene" : "standard"; const shadows = stats.shadowsEnabled ? "shadows on" : "shadows off"; - return `${mode}; target ${stats.targetRenderFps} fps; actual ${stats.actualRenderFps.toFixed(1)} fps; stats refresh ${stats.statsRefreshIntervalMs}ms ${stats.statsRefreshRenderCount}/${stats.statsRefreshSkipCount}; dpr ${stats.renderPixelRatio.toFixed(2)}; buffer preserved ${stats.preserveDrawingBufferEnabled}; readback renders ${stats.readbackRenderCount}; ${shadows}; skipped ${stats.renderSkipCount}; hover ${stats.hoverPickIntervalMs}ms; frozen ${stats.frozenStaticObjectCount} prefrozen ${stats.preFrozenMeshCloneCount} spinSkip ${stats.spinningScanSkippedCount}; instanced ${stats.instancedMeshObjectCount}/${stats.instancedMeshBatchCount}/${stats.instancedMeshDrawCount} matrices ${stats.instancedMeshMatrixUpdateCount} cull ${stats.instancedMeshFrustumCulledCount} static ${stats.instancedMeshStaticMatrixCount} elidedLocal ${stats.instancedLocalObjectElisionCount}; manual roots ${stats.manualMatrixRootCount} updates ${stats.manualMatrixUpdateCount}; primitive geometries ${stats.primitiveGeometryCount}; mesh cache ${stats.meshCacheSourceCount} sources ${stats.meshCacheHitCount}/${stats.meshCacheMissCount} hit/miss retained ${stats.retainedMeshGeometryCount}g/${stats.retainedMeshMaterialCount}m; bounds cache ${stats.sceneObjectBoundsCount} fast ${stats.fastPickProxyBoundsCount} scratch ${stats.pickBoundsScratchTransformCount} source ${stats.meshSourceBoundsCount} hoverProxy ${stats.hoverProxyBoundsHitCount} cached ${stats.hoverCachedBoundsHitCount}/fallback ${stats.hoverRootBoundsFallbackCount} camera traversals ${stats.cameraFitTraversalCount}; scene batches ${stats.sceneRootBatchCount}/${stats.batchedSceneRootCount} suppressed ${stats.sceneLoadSuppressedRenderCount} yields ${stats.sceneBuildYieldCount}; state payload ${stats.statePayloadBodyCount}/${stats.mergedBodyStateCount} reduced ${stats.reducedStateFrameCount} incremental ${stats.incrementalBodyMergeCount} poll ${stats.stateBasePollIntervalMs}/${stats.statePausedPollIntervalMs}/${stats.stateLastScheduledPollIntervalMs}ms ${stats.statePollRequestCount}/${stats.stateActivePollRequestCount}/${stats.statePausedPollRequestCount} semantic ${stats.stateParsedPollFrameCount}/${stats.stateForwardedPollFrameCount}/${stats.stateSkippedDuplicatePollFrameCount} hash ${stats.stateHashedPollFrameCount}/${stats.stateSkippedDuplicateHashPollFrameCount}/${stats.stateSkippedDuplicateSemanticPollFrameCount}; ui cache physics ${stats.objectPhysicsProfileCacheHitCount}/${stats.objectPhysicsProfileCacheMissCount} builder ${stats.builderPhysicsProfileCacheHitCount}/${stats.builderPhysicsProfileCacheMissCount} text ${stats.textWriteCount}/${stats.textWriteSkipCount} selector ${stats.textSelectorCacheHitCount}/${stats.textSelectorCacheMissCount} diagnostics ${stats.diagnosticsPanelRenderCount}/${stats.diagnosticsPanelSkipCount} builderPanels ${stats.builderPanelRenderCount}/${stats.builderPanelDeferredRenderCount}/${stats.builderPanelCoalescedRenderCount}; pick targets ${stats.hoverPickTargetCount}/${stats.pickProxyCount}; state apply ${stats.appliedBodyStateCount}/skip ${stats.skippedBodyStateCount}; unchanged frames ${stats.unchangedFrameCount}; released ${stats.releasedGeometryCount}g/${stats.releasedMaterialCount}m/${stats.releasedTextureCount}t`; + return `${mode}; target ${stats.targetRenderFps} fps; actual ${stats.actualRenderFps.toFixed(1)} fps; stats refresh ${stats.statsRefreshIntervalMs}ms ${stats.statsRefreshRenderCount}/${stats.statsRefreshSkipCount}; dpr ${stats.renderPixelRatio.toFixed(2)}; buffer preserved ${stats.preserveDrawingBufferEnabled}; readback renders ${stats.readbackRenderCount}; ${shadows}; skipped ${stats.renderSkipCount}; hover ${stats.hoverPickIntervalMs}ms; frozen ${stats.frozenStaticObjectCount} prefrozen ${stats.preFrozenMeshCloneCount} spinSkip ${stats.spinningScanSkippedCount}; instanced ${stats.instancedMeshObjectCount}/${stats.instancedMeshBatchCount}/${stats.instancedMeshDrawCount} matrices ${stats.instancedMeshMatrixUpdateCount} cull ${stats.instancedMeshFrustumCulledCount} static ${stats.instancedMeshStaticMatrixCount} elidedLocal ${stats.instancedLocalObjectElisionCount}; manual roots ${stats.manualMatrixRootCount} updates ${stats.manualMatrixUpdateCount}; primitive geometries ${stats.primitiveGeometryCount}; mesh cache ${stats.meshCacheSourceCount} sources ${stats.meshCacheHitCount}/${stats.meshCacheMissCount} hit/miss retained ${stats.retainedMeshGeometryCount}g/${stats.retainedMeshMaterialCount}m; bounds cache ${stats.sceneObjectBoundsCount} fast ${stats.fastPickProxyBoundsCount} scratch ${stats.pickBoundsScratchTransformCount} source ${stats.meshSourceBoundsCount} hoverProxy ${stats.hoverProxyBoundsHitCount} cached ${stats.hoverCachedBoundsHitCount}/fallback ${stats.hoverRootBoundsFallbackCount} camera traversals ${stats.cameraFitTraversalCount}; scene batches ${stats.sceneRootBatchCount}/${stats.batchedSceneRootCount} suppressed ${stats.sceneLoadSuppressedRenderCount} yields ${stats.sceneBuildYieldCount}; state payload ${stats.statePayloadBodyCount}/${stats.mergedBodyStateCount} reduced ${stats.reducedStateFrameCount} incremental ${stats.incrementalBodyMergeCount} poll ${stats.stateBasePollIntervalMs}/${stats.statePausedPollIntervalMs}/${stats.stateLastScheduledPollIntervalMs}ms ${stats.statePollRequestCount}/${stats.stateActivePollRequestCount}/${stats.statePausedPollRequestCount} semantic ${stats.stateParsedPollFrameCount}/${stats.stateForwardedPollFrameCount}/${stats.stateSkippedDuplicatePollFrameCount} hash ${stats.stateHashedPollFrameCount}/${stats.stateSkippedDuplicateHashPollFrameCount}/${stats.stateSkippedDuplicateSemanticPollFrameCount} notModified ${stats.stateNotModifiedPollFrameCount}; ui cache physics ${stats.objectPhysicsProfileCacheHitCount}/${stats.objectPhysicsProfileCacheMissCount} builder ${stats.builderPhysicsProfileCacheHitCount}/${stats.builderPhysicsProfileCacheMissCount} text ${stats.textWriteCount}/${stats.textWriteSkipCount} selector ${stats.textSelectorCacheHitCount}/${stats.textSelectorCacheMissCount} diagnostics ${stats.diagnosticsPanelRenderCount}/${stats.diagnosticsPanelSkipCount} builderPanels ${stats.builderPanelRenderCount}/${stats.builderPanelDeferredRenderCount}/${stats.builderPanelCoalescedRenderCount}; pick targets ${stats.hoverPickTargetCount}/${stats.pickProxyCount}; state apply ${stats.appliedBodyStateCount}/skip ${stats.skippedBodyStateCount}; unchanged frames ${stats.unchangedFrameCount}; released ${stats.releasedGeometryCount}g/${stats.releasedMaterialCount}m/${stats.releasedTextureCount}t`; } function renderDiagnosticsPanel( @@ -2896,6 +2898,7 @@ function renderDiagnosticsPanel( data.stateHashedPollFrameCount = String(diagnostics.stats.stateHashedPollFrameCount); data.stateSkippedDuplicateHashPollFrameCount = String(diagnostics.stats.stateSkippedDuplicateHashPollFrameCount); data.stateSkippedDuplicateSemanticPollFrameCount = String(diagnostics.stats.stateSkippedDuplicateSemanticPollFrameCount); + data.stateNotModifiedPollFrameCount = String(diagnostics.stats.stateNotModifiedPollFrameCount); data.batchedSceneRootCount = String(diagnostics.stats.batchedSceneRootCount); data.sceneRootBatchCount = String(diagnostics.stats.sceneRootBatchCount); data.objectPhysicsProfileCacheHitCount = String(diagnostics.stats.objectPhysicsProfileCacheHitCount); diff --git a/src/state/StateClient.ts b/src/state/StateClient.ts index 0379365..c7b00b9 100644 --- a/src/state/StateClient.ts +++ b/src/state/StateClient.ts @@ -16,6 +16,7 @@ export interface StateClientStats { hashedPollFrameCount: number; skippedDuplicateHashPollFrameCount: number; skippedDuplicateSemanticPollFrameCount: number; + notModifiedPollFrameCount: number; lastFramePaused?: boolean; } @@ -45,6 +46,7 @@ export class StateClient { private hashedPollFrameCount = 0; private skippedDuplicateHashPollFrameCount = 0; private skippedDuplicateSemanticPollFrameCount = 0; + private notModifiedPollFrameCount = 0; private lastFramePaused: boolean | undefined; onFrame(listener: StateListener): void { @@ -78,6 +80,7 @@ export class StateClient { hashedPollFrameCount: this.hashedPollFrameCount, skippedDuplicateHashPollFrameCount: this.skippedDuplicateHashPollFrameCount, skippedDuplicateSemanticPollFrameCount: this.skippedDuplicateSemanticPollFrameCount, + notModifiedPollFrameCount: this.notModifiedPollFrameCount, lastFramePaused: this.lastFramePaused, }; } @@ -131,6 +134,7 @@ export class StateClient { this.hashedPollFrameCount = 0; this.skippedDuplicateHashPollFrameCount = 0; this.skippedDuplicateSemanticPollFrameCount = 0; + this.notModifiedPollFrameCount = 0; this.lastFramePaused = undefined; if (this.socket) { this.socket.close(); @@ -223,11 +227,28 @@ export class StateClient { this.pollAbortController = controller; try { const url = pollUrl(); - const response = await fetch(url, { cache: "no-store", signal: controller.signal }); + const headers = lastForwardedStateHash + ? { "If-None-Match": `"${lastForwardedStateHash}"` } + : undefined; + const response = await fetch(url, { cache: "no-store", headers, signal: controller.signal }); this.pollRequestCount += 1; if (lastPollUsedActiveUrl) { this.activePollRequestCount += 1; } + if (response.status === 304) { + const stateHash = stateHashFromResponse(response) ?? lastForwardedStateHash; + if (stateHash) { + this.hashedPollFrameCount += 1; + } + this.notModifiedPollFrameCount += 1; + this.skippedDuplicatePollFrameCount += 1; + this.skippedDuplicateHashPollFrameCount += 1; + if (this.lastFramePaused === true) { + this.pausedPollRequestCount += 1; + } + this.status(`state: poll connected`); + return; + } if (!response.ok) { this.status(`state: poll ${response.status}`); return;