Prioritize real backend control state
This commit is contained in:
parent
93b4bf4c6a
commit
505e62ad8b
@ -127,9 +127,12 @@ function controlUrlFromManifest() {
|
||||
return url.href;
|
||||
}
|
||||
|
||||
function stateUrlFromManifest() {
|
||||
function stateUrlFromManifest({ compact = false } = {}) {
|
||||
const url = new URL(manifestUrl);
|
||||
url.pathname = url.pathname.replace(/\/manifest$/, "/state");
|
||||
if (compact) {
|
||||
url.searchParams.set("compact", "1");
|
||||
}
|
||||
return url.href;
|
||||
}
|
||||
|
||||
@ -149,8 +152,8 @@ async function postControl(action) {
|
||||
return { ok: response.ok, status: response.status, json };
|
||||
}
|
||||
|
||||
async function fetchState() {
|
||||
const response = await fetch(stateUrlFromManifest(), { cache: "no-store" });
|
||||
async function fetchState(options) {
|
||||
const response = await fetch(stateUrlFromManifest(options), { cache: "no-store" });
|
||||
const json = await response.json();
|
||||
return { ok: response.ok, status: response.status, json };
|
||||
}
|
||||
@ -265,11 +268,13 @@ async function readBrowserStats(page) {
|
||||
async function waitForProductScene(page) {
|
||||
await page.waitForFunction(([limits]) => {
|
||||
const num = (selector) => Number((document.querySelector(selector)?.textContent ?? "0").match(/-?\d+(?:\.\d+)?/)?.[0] ?? "0");
|
||||
const status = document.querySelector("#status")?.textContent ?? "";
|
||||
const groupText = document.querySelector("#stat-groups")?.textContent ?? "";
|
||||
const groupMatch = groupText.match(/(\d+)\s*\/\s*(\d+)/);
|
||||
const groups = groupMatch ? Number(groupMatch[1]) : 0;
|
||||
const groupedObjects = groupMatch ? Number(groupMatch[2]) : 0;
|
||||
return num("#stat-objects") >= limits.minObjects
|
||||
return /已加载 manifest|loaded manifest|manifest loaded/i.test(status)
|
||||
&& num("#stat-objects") >= limits.minObjects
|
||||
&& num("#stat-bindings") >= limits.minBodyBindings
|
||||
&& num("#stat-meshes") >= limits.minMeshes
|
||||
&& groups >= limits.minEditableGroups
|
||||
@ -277,7 +282,7 @@ async function waitForProductScene(page) {
|
||||
&& num("#stat-pixels") > 0
|
||||
&& (document.querySelector("#stat-step")?.textContent ?? "-") !== "-";
|
||||
}, [thresholds], { timeout: thresholds.browserReadyMs });
|
||||
await page.waitForTimeout(1000);
|
||||
await page.waitForTimeout(2000);
|
||||
}
|
||||
|
||||
async function waitForMobileScene(page) {
|
||||
@ -338,7 +343,7 @@ async function waitForDom(page, predicate, arg) {
|
||||
try {
|
||||
const alreadySatisfied = await page.evaluate(predicate, arg);
|
||||
if (!alreadySatisfied) {
|
||||
await page.waitForFunction(predicate, arg, { timeout: thresholds.maxUiControlMs });
|
||||
await page.waitForFunction(predicate, arg, { polling: 50, timeout: thresholds.maxUiControlMs });
|
||||
}
|
||||
return { ok: true, elapsedMs: Math.round(performance.now() - started) };
|
||||
} catch (error) {
|
||||
@ -351,7 +356,7 @@ async function waitForBackendPaused(expectedPaused) {
|
||||
let lastState;
|
||||
while (performance.now() - started <= thresholds.maxUiControlMs) {
|
||||
try {
|
||||
const state = await fetchState();
|
||||
const state = await fetchState({ compact: true });
|
||||
lastState = state.json;
|
||||
if (state.ok && state.json?.paused === expectedPaused) {
|
||||
return { ok: true, elapsedMs: Math.round(performance.now() - started), state: compactStateFrame(state.json) };
|
||||
@ -366,12 +371,15 @@ async function waitForBackendPaused(expectedPaused) {
|
||||
|
||||
async function controlThroughUi(page, selector, expectedPaused) {
|
||||
const clickMs = await clickButton(page, selector);
|
||||
const backend = await waitForBackendPaused(expectedPaused);
|
||||
const dom = await waitForDom(
|
||||
page,
|
||||
(expected) => (document.querySelector("#stat-paused")?.textContent ?? "") === String(expected),
|
||||
expectedPaused,
|
||||
);
|
||||
const [backend, domAfterClick] = await Promise.all([
|
||||
waitForBackendPaused(expectedPaused),
|
||||
waitForDom(
|
||||
page,
|
||||
(expected) => (document.querySelector("#stat-paused")?.textContent ?? "") === String(expected),
|
||||
expectedPaused,
|
||||
),
|
||||
]);
|
||||
const dom = { ...domAfterClick, elapsedMs: clickMs + domAfterClick.elapsedMs, waitElapsedMs: domAfterClick.elapsedMs };
|
||||
return { clickMs, backend, dom, expectedPaused };
|
||||
}
|
||||
|
||||
@ -383,13 +391,12 @@ async function verifyBrowser() {
|
||||
await desktop.addInitScript(() => window.localStorage.removeItem("motrixsim-web-locale"));
|
||||
await desktop.goto(frontendUrl.href, { waitUntil: "domcontentloaded" });
|
||||
await waitForProductScene(desktop);
|
||||
await clickButton(desktop, "#acceptance-run");
|
||||
await desktop.waitForFunction(() => (document.querySelector("#acceptance-score")?.textContent ?? "").includes("/"), undefined, { timeout: 10000 });
|
||||
|
||||
const paused = await readBrowserStats(desktop);
|
||||
const resumeControl = await controlThroughUi(desktop, "#viewport-resume", false);
|
||||
const resumed = await readBrowserStats(desktop);
|
||||
const pauseControl = await controlThroughUi(desktop, "#viewport-pause", true);
|
||||
await clickButton(desktop, "#acceptance-run");
|
||||
await desktop.waitForFunction(() => (document.querySelector("#acceptance-score")?.textContent ?? "").includes("/"), undefined, { timeout: 10000 });
|
||||
const desktopStats = await readBrowserStats(desktop);
|
||||
const desktopScreenshot = await safeScreenshot("browser-desktop-screenshot", desktop, path.join(outDir, "product-readiness-desktop.jpg"));
|
||||
|
||||
|
||||
@ -854,17 +854,43 @@ class MotrixRuntime:
|
||||
# immediately reacquire the runtime lock.
|
||||
time.sleep(0.001)
|
||||
|
||||
def control_status_snapshot(self) -> dict[str, Any]:
|
||||
values = list(self.step_ms)
|
||||
if values:
|
||||
ordered = sorted(values)
|
||||
p95 = ordered[max(0, min(len(ordered) - 1, int(len(ordered) * 0.95) - 1))]
|
||||
mean = statistics.fmean(values)
|
||||
max_v = max(values)
|
||||
else:
|
||||
mean = p95 = max_v = 0.0
|
||||
return {
|
||||
"ok": True,
|
||||
"host": socket.gethostname(),
|
||||
"scene": self.scene.as_posix(),
|
||||
"step": self.step,
|
||||
"paused": self.paused,
|
||||
"running": self.running,
|
||||
"policy": self.policy_name,
|
||||
"server_time": time.time(),
|
||||
"command": list(self.command),
|
||||
"step_ms": {"mean": mean, "p95": p95, "max": max_v},
|
||||
}
|
||||
|
||||
def control(self, action: str, payload: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
payload = payload or {}
|
||||
steps = int(payload.get("steps") or 1)
|
||||
with self.lock:
|
||||
if action in {"pause", "resume", "toggle"}:
|
||||
if action == "pause":
|
||||
self.paused = True
|
||||
elif action == "resume":
|
||||
self.paused = False
|
||||
elif action == "toggle":
|
||||
else:
|
||||
self.paused = not self.paused
|
||||
elif action == "reset":
|
||||
snapshot = self.control_status_snapshot()
|
||||
snapshot["control_action"] = action
|
||||
return snapshot
|
||||
with self.lock:
|
||||
if action == "reset":
|
||||
self.reset_locked()
|
||||
elif action == "step":
|
||||
self.paused = True
|
||||
@ -959,6 +985,8 @@ class MotrixRuntime:
|
||||
return actuators
|
||||
|
||||
def snapshot(self, compact: bool = False) -> dict[str, Any]:
|
||||
if compact:
|
||||
return self.control_status_snapshot()
|
||||
with self.lock:
|
||||
values = list(self.step_ms)
|
||||
if values:
|
||||
@ -980,10 +1008,6 @@ class MotrixRuntime:
|
||||
"command": list(self.command),
|
||||
"step_ms": {"mean": mean, "p95": p95, "max": max_v},
|
||||
}
|
||||
if compact:
|
||||
base["joints"] = self.joint_state_locked()
|
||||
base["actuators"] = self.actuator_state_locked()
|
||||
return base
|
||||
bodies: dict[str, Any] = {}
|
||||
seen: set[str] = set()
|
||||
for name in self.link_names:
|
||||
|
||||
11
src/main.ts
11
src/main.ts
@ -1147,7 +1147,18 @@ function compactStateUrl(stateUrl: string): string {
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
function isStaleRuntimeFrame(frame: PreviewFrame): boolean {
|
||||
if (frame.server_time === undefined || latestFrame?.server_time === undefined) {
|
||||
return false;
|
||||
}
|
||||
return frame.server_time < latestFrame.server_time;
|
||||
}
|
||||
|
||||
function applyRuntimeFrame(frame: PreviewFrame, renderPanels = true): PreviewFrame {
|
||||
if (isStaleRuntimeFrame(frame) && latestFrame) {
|
||||
renderFrameStatus(latestFrame);
|
||||
return latestFrame;
|
||||
}
|
||||
const merged = mergeLatestFrame(frame);
|
||||
previewer.applyFrame(frame);
|
||||
updateDragAvailability(merged);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user