perf(web): reuse incremental build and status caches
Some checks failed
M6 deployable RC / quick (push) Has been cancelled
M6 deployable RC / chromium (push) Has been cancelled
M6 deployable RC / release (push) Has been cancelled

This commit is contained in:
mes123456
2026-09-06 10:27:26 +08:00
parent 1184a51ddf
commit 6090995999
9 changed files with 131 additions and 11 deletions

View File

@@ -10,6 +10,7 @@ from __future__ import annotations
import argparse
import hashlib
import json
import os
import statistics
import re
import time
@@ -179,12 +180,22 @@ def project_progress() -> dict:
batch_units = 0.0
remaining_hours = unverified / unit_rate if unit_rate > 0 else None
stage_durations = {}
stage_labels = {}
for item in productive:
for index, command in enumerate(item.get("commands", [])):
duration = command.get("durationMs") if isinstance(command, dict) else None
if isinstance(duration, (int, float)) and duration >= 0:
stage_durations.setdefault(str(index + 1), []).append(duration)
stage = str(index + 1)
stage_durations.setdefault(stage, []).append(duration)
if stage not in stage_labels and isinstance(command.get("command"), str):
stage_labels[stage] = command["command"].split("&&", 1)[0].strip()[:120]
stage_medians = {stage: round(statistics.median(values)) for stage, values in stage_durations.items() if values}
bottleneck_stage = max(stage_medians, key=stage_medians.get) if stage_medians else None
bottleneck = {
"stage": bottleneck_stage,
"label": stage_labels.get(bottleneck_stage) if bottleneck_stage else None,
"medianDurationMs": stage_medians.get(bottleneck_stage) if bottleneck_stage else None,
}
historical = max(historical_candidates, default=("", {}), key=lambda item: item[0])[1]
workflow_modules = product_backlog.get("workflowModules", []) if isinstance(product_backlog.get("workflowModules"), list) else []
product_tasks = product_backlog.get("tasks", []) if isinstance(product_backlog.get("tasks"), list) else []
@@ -213,6 +224,7 @@ def project_progress() -> dict:
"reusedBatches": sum(1 for item in receipts if item["reused"]),
"observedExecutionMs": round(sum(item["durationMs"] for item in productive)),
"stageMedianDurationMs": stage_medians,
"bottleneck": bottleneck,
"medianIncrementalUnitsPerBatch": round(batch_units, 2),
"medianIncrementalUnitsPerHour": round(unit_rate, 2),
"estimatedRemainingHours": round(remaining_hours, 2) if remaining_hours is not None else None,
@@ -286,6 +298,14 @@ def wasm_module_status() -> dict:
"""Report module preload policy and artifact integrity without fetching or rebuilding."""
manifest_path = ROOT / "web/app/public/wasm-module-manifest.json"
manifest = _read_json(manifest_path)
cache_path = CONTROL_ROOT / "wasm-module-status-cache.json"
cache = _read_json(cache_path)
cached_entries = cache.get("entries", {}) if isinstance(cache.get("entries"), dict) else {}
next_cache = {"schemaVersion": 1, "manifestMtimeNs": None, "entries": {}}
try:
next_cache["manifestMtimeNs"] = str(manifest_path.stat().st_mtime_ns)
except OSError:
pass
entries = []
for entry in manifest.get("modules", []) if isinstance(manifest.get("modules"), list) else []:
resources = entry.get("resources", {}) if isinstance(entry, dict) else {}
@@ -296,11 +316,26 @@ def wasm_module_status() -> dict:
relative = resource["url"].lstrip("/")
path = ROOT / "web/app/public" / relative
actual = None
if path.is_file():
bytes_count = 0
mtime_ns = None
try:
stat = path.stat()
bytes_count = stat.st_size
mtime_ns = str(stat.st_mtime_ns)
except OSError:
stat = None
cache_key = str(path.relative_to(ROOT))
cached = next_cache["entries"].get(cache_key)
if not isinstance(cached, dict):
cached = cached_entries.get(cache_key, {}) if isinstance(cached_entries.get(cache_key), dict) else {}
if stat and cached.get("bytes") == bytes_count and cached.get("mtimeNs") == mtime_ns and isinstance(cached.get("sha256"), str):
actual = cached["sha256"]
elif stat:
actual = hashlib.sha256(path.read_bytes()).hexdigest()
next_cache["entries"][cache_key] = {"bytes": bytes_count, "mtimeNs": mtime_ns, "sha256": actual}
variants[variant] = {
"url": resource["url"],
"bytes": path.stat().st_size if path.is_file() else 0,
"bytes": bytes_count,
"integrity": actual == resource.get("sha256") if actual else False,
}
entries.append({
@@ -309,6 +344,14 @@ def wasm_module_status() -> dict:
"preload": entry.get("preload") is True,
"variants": variants,
})
try:
temporary = cache_path.with_suffix(f".tmp-{os.getpid()}")
temporary.write_text(json.dumps(next_cache, ensure_ascii=False, separators=(",", ":")) + "\n", encoding="utf-8")
temporary.replace(cache_path)
except OSError:
# Status reporting remains read-only from the caller's perspective if
# the control directory is unavailable; the next poll simply rehashes.
pass
return {
"manifest": "web/app/public/wasm-module-manifest.json" if manifest else None,
"moduleCount": len(entries),
@@ -340,6 +383,7 @@ def format_progress(progress: dict, fast_path: dict | None = None) -> list[str]:
f" source traceability reference: {progress.get('verifiedUnits', 0)}/{progress.get('totalUnits', 0)} bound; excluded from product completion denominator",
f" task plan: {plan.get('done', 0)}/{plan.get('tasks', 0)} done, active={','.join(plan.get('active', [])) or 'none'}, blocked={plan.get('blocked', 0)}",
f" old verification estimate: remaining={estimate_text}, total={total_text} (reference only; strategy retired)",
f" observed bottleneck: stage={progress.get('bottleneck', {}).get('stage') or 'n/a'} median={progress.get('bottleneck', {}).get('medianDurationMs') or 0}ms command={progress.get('bottleneck', {}).get('label') or 'n/a'}",
f" fast path: batch={batch.get('batchId') or 'none'} selected={batch.get('selectedRecordCount', 0)} completed={batch.get('completed', False)}",
f" context cache: {context_cache.get('status', 'MISSING')} task={context_cache.get('task') or 'none'} inputs={context_cache.get('inputCount', 0)}; failure ledger entries={ledger.get('entryCount', 0)} hydratedLogs={ledger.get('hydratedLogs', 0)}",
f" command budget: max={fast_path.get('commandBudget', {}).get('maxCommands', 48)}, read/exploration max={fast_path.get('commandBudget', {}).get('maxReadCommands', 16)}; verification={fast_path.get('verificationPolicy', 'n/a')}",

View File

@@ -20,8 +20,10 @@ if test -d /dev/shm && test -w /dev/shm; then
export EM_CACHE="${EM_CACHE:-$WEB_RAM_CACHE_ROOT/emcache}"
export TMPDIR="${TMPDIR:-$WEB_RAM_CACHE_ROOT/tmp}"
fi
export CMAKE_BUILD_PARALLEL_LEVEL="${CMAKE_BUILD_PARALLEL_LEVEL:-8}"
export NINJAFLAGS="${NINJAFLAGS:--j8}"
WEB_BUILD_JOBS="${WEB_BUILD_JOBS:-$(WEB_BUILD_MEMORY_GIB_PER_JOB=4 WEB_BUILD_JOBS_CAP=16 bash "$ROOT_DIR/tools/web/compute-build-jobs.sh")}"
export WEB_BUILD_JOBS
export CMAKE_BUILD_PARALLEL_LEVEL="${CMAKE_BUILD_PARALLEL_LEVEL:-$WEB_BUILD_JOBS}"
export NINJAFLAGS="${NINJAFLAGS:--j$WEB_BUILD_JOBS}"
mkdir -p "$CONTROL_DIR"