Spaces:
Running
Running
File size: 21,699 Bytes
39ff632 37e3d5a 39ff632 37e3d5a 39ff632 37e3d5a 39ff632 37e3d5a 39ff632 37e3d5a 39ff632 37e3d5a 0ecd5b0 37e3d5a 0ecd5b0 37e3d5a 0ecd5b0 37e3d5a 39ff632 37e3d5a 39ff632 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | """Subprocess bridge to the vendored upstream ``forge/`` pipeline scripts.
This module is intentionally *standard-library only* so it can also run
inside the minimal Docker build stage that regenerates the test fixture
factory (``scripts/build_fixture_factory.py``).
Safety contract:
* every invocation is a list-argv ``subprocess.run`` (never shell=True);
* child processes get a scrubbed environment (no ``LLM_*``/``ANTHROPIC_*``
variables can leak into them);
* every call has a hard timeout and bounded captured output;
* all file paths passed to the scripts live inside a per-job directory
owned by the caller -- the scripts resolve and write whatever path they
are given, so containment is enforced here by construction.
"""
from __future__ import annotations
import copy
import json
import math
import os
import re
import subprocess
import sys
from dataclasses import dataclass
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[1]
FORGE = REPO_ROOT / "forge"
PROBE = FORGE / "stage1_intake" / "probe_image.py"
VALIDATE = FORGE / "stage2_spec" / "validate_sculpt_spec.py"
ORCHESTRATE = FORGE / "stage3_build" / "orchestrate_passes.py"
GENERATE = FORGE / "stage3_build" / "generate_threejs_factory.py"
_MAX_CAPTURE = 1024 * 1024 # 1 MiB per stream
class ForgeError(Exception):
"""A forge script exited non-zero (or timed out). Carries diagnostics."""
def __init__(self, script: str, message: str, *, returncode: int | None = None,
stdout: str = "", stderr: str = "") -> None:
super().__init__(message)
self.script = script
self.returncode = returncode
self.stdout = stdout
self.stderr = stderr
@dataclass(frozen=True)
class ForgeResult:
returncode: int
stdout: str
stderr: str
def _scrubbed_env() -> dict[str, str]:
env = {
"PATH": os.environ.get("PATH", "/usr/local/bin:/usr/bin:/bin"),
"HOME": os.environ.get("HOME", "/tmp"),
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONUNBUFFERED": "1",
"LANG": "C.UTF-8",
}
tmp = os.environ.get("TMPDIR")
if tmp:
env["TMPDIR"] = tmp
return env
def run_forge(script: Path, *args: str, timeout: int = 120, cwd: Path | None = None) -> ForgeResult:
"""Run a forge script; return captured output. Never raises on rc != 0."""
argv = [sys.executable, str(script), *args]
try:
proc = subprocess.run(
argv,
cwd=str(cwd) if cwd else None,
env=_scrubbed_env(),
capture_output=True,
text=True,
timeout=timeout,
errors="replace",
)
except subprocess.TimeoutExpired as exc:
raise ForgeError(
script.name, f"{script.name} timed out after {timeout}s",
returncode=None, stdout=exc.stdout or "", stderr=exc.stderr or "",
) from exc
return ForgeResult(
returncode=proc.returncode,
stdout=proc.stdout[:_MAX_CAPTURE],
stderr=proc.stderr[:_MAX_CAPTURE],
)
def probe_image(image_path: Path, *, timeout: int = 60) -> dict:
"""Run stage1_intake/probe_image.py and parse its JSON stdout."""
result = run_forge(PROBE, str(image_path), timeout=timeout)
if result.returncode != 0:
raise ForgeError(
PROBE.name, f"probe_image failed (rc={result.returncode})",
returncode=result.returncode, stdout=result.stdout, stderr=result.stderr,
)
try:
payload = json.loads(result.stdout)
except json.JSONDecodeError as exc:
raise ForgeError(PROBE.name, "probe_image did not return JSON",
returncode=result.returncode, stdout=result.stdout,
stderr=result.stderr) from exc
if not isinstance(payload, dict):
raise ForgeError(PROBE.name, "probe_image returned non-object JSON",
returncode=result.returncode, stdout=result.stdout)
return payload
def validate_spec(spec_path: Path, *, strict: bool = True, timeout: int = 60) -> dict:
"""Run the spec validator. Returns ``{ok, errors, warnings, ...}``.
The validator exits 0 on PASS and 1 on FAIL; both are normal outcomes.
rc > 1 (argparse/IO errors) raises ForgeError.
"""
args = [str(spec_path), "--json"]
if strict:
args.append("--strict-quality")
result = run_forge(VALIDATE, *args, timeout=timeout)
if result.returncode not in (0, 1):
raise ForgeError(
VALIDATE.name, f"validate_sculpt_spec crashed (rc={result.returncode})",
returncode=result.returncode, stdout=result.stdout, stderr=result.stderr,
)
try:
payload = json.loads(result.stdout)
except json.JSONDecodeError as exc:
raise ForgeError(VALIDATE.name, "validator did not return JSON",
returncode=result.returncode, stdout=result.stdout,
stderr=result.stderr) from exc
payload.setdefault("ok", result.returncode == 0)
return payload
def orchestrate_sync(spec_path: Path, *, timeout: int = 60) -> None:
"""Refresh sculptPipeline from reviewHistory (sync --in-place)."""
result = run_forge(ORCHESTRATE, "sync", str(spec_path), "--in-place", timeout=timeout)
if result.returncode != 0:
raise ForgeError(
ORCHESTRATE.name, f"orchestrate sync failed (rc={result.returncode})",
returncode=result.returncode, stdout=result.stdout, stderr=result.stderr,
)
def generate_factory(spec_path: Path, out_path: Path, *, pass_id: str | None = None,
timeout: int = 120) -> None:
"""Emit the TypeScript factory. Raises ForgeError on gate/IO failure."""
args = [str(spec_path), "--out", str(out_path), "--force"]
if pass_id:
args.extend(["--pass-id", pass_id])
result = run_forge(GENERATE, *args, timeout=timeout)
if result.returncode != 0:
raise ForgeError(
GENERATE.name,
f"factory generation failed for pass {pass_id or '(current)'} "
f"(rc={result.returncode}): {result.stderr.strip()[:400]}",
returncode=result.returncode, stdout=result.stdout, stderr=result.stderr,
)
if not out_path.exists() or out_path.stat().st_size == 0:
raise ForgeError(GENERATE.name, "generator exited 0 but produced no output",
returncode=result.returncode, stdout=result.stdout,
stderr=result.stderr)
HOSTED_PREVIEW_PASS = "hosted-preview"
HOSTED_PRIMITIVES = {
"box", "sphere", "ellipsoid", "cylinder", "cone", "capsule", "torus",
"tube", "lathe", "extrude", "ground-blade", "curve-sweep",
"plane-card", "instanced-cluster",
}
def _is_finite_number(value: object) -> bool:
"""Return true only for real, finite JSON-style numbers.
``bool`` is an ``int`` subclass in Python, but accepting ``True`` for a
radius or coordinate produces subtly malformed generated JavaScript.
Very large Python integers may also overflow during float conversion, so
keep the conversion guarded.
"""
if isinstance(value, bool) or not isinstance(value, (int, float)):
return False
try:
return math.isfinite(float(value))
except (OverflowError, ValueError):
return False
def _is_positive_number(value: object) -> bool:
return _is_finite_number(value) and float(value) > 0
def _is_integer_at_least(value: object, minimum: int) -> bool:
return (
_is_finite_number(value)
and float(value).is_integer()
and float(value) >= minimum
)
def _is_coordinate(value: object, size: int) -> bool:
return (
isinstance(value, (list, tuple))
and len(value) == size
and all(_is_finite_number(item) for item in value)
)
def _valid_point_array(value: object, size: int, minimum: int) -> bool:
if (
not isinstance(value, list)
or len(value) < minimum
or not all(_is_coordinate(point, size) for point in value)
):
return False
# Degenerate paths/polygons with repeated copies of one coordinate satisfy
# a simple length check but still fail inside Three.js geometry builders.
points = {tuple(float(item) for item in point) for point in value}
return len(points) >= minimum
def _advanced_geometry_errors(
label: str, primitive: str, descriptor: dict
) -> list[str]:
"""Validate descriptor payloads consumed directly by generated TypeScript."""
errors: list[str] = []
prefix = f"hosted preview: component {label!r} geometryDescriptor"
if primitive == "extrude":
profile = descriptor.get("profile2D")
field = f"{prefix}.profile2D"
if not isinstance(profile, dict):
return [f"{field} must be an object"]
if not _valid_point_array(profile.get("points"), 2, 3):
errors.append(
f"{field}.points must contain at least 3 distinct finite [x, y] coordinates"
)
if not _is_positive_number(profile.get("depth")):
errors.append(f"{field}.depth must be a finite positive number")
holes = profile.get("holes")
if holes is not None and (
not isinstance(holes, list)
or not all(_valid_point_array(loop, 2, 3) for loop in holes)
):
errors.append(
f"{field}.holes must contain polygons of at least 3 distinct finite "
"[x, y] coordinates"
)
oval_holes = profile.get("ovalHoles")
if oval_holes is not None:
valid_ovals = isinstance(oval_holes, list) and all(
isinstance(oval, dict)
and _is_finite_number(oval.get("cx"))
and _is_finite_number(oval.get("cy"))
and _is_positive_number(oval.get("rx"))
and _is_positive_number(oval.get("ry"))
for oval in oval_holes
)
if not valid_ovals:
errors.append(
f"{field}.ovalHoles must contain finite cx/cy and positive finite rx/ry"
)
elif primitive == "lathe":
profile = descriptor.get("latheProfile")
field = f"{prefix}.latheProfile"
if not isinstance(profile, dict):
return [f"{field} must be an object"]
points = profile.get("points")
if not _valid_point_array(points, 2, 2):
errors.append(
f"{field}.points must contain at least 2 distinct finite [radius, y] coordinates"
)
elif any(float(point[0]) < 0 for point in points):
errors.append(f"{field}.points radii must be finite non-negative numbers")
if not _is_integer_at_least(profile.get("segments"), 3):
errors.append(f"{field}.segments must be a finite integer of at least 3")
elif primitive == "tube":
path = descriptor.get("tubePath")
field = f"{prefix}.tubePath"
if not isinstance(path, dict):
return [f"{field} must be an object"]
if not _valid_point_array(path.get("points"), 3, 2):
errors.append(
f"{field}.points must contain at least 2 distinct finite [x, y, z] coordinates"
)
if not _is_positive_number(path.get("radius")):
errors.append(f"{field}.radius must be a finite positive number")
if "radialSegments" in path and not _is_integer_at_least(
path["radialSegments"], 3
):
errors.append(
f"{field}.radialSegments must be a finite integer of at least 3"
)
if not isinstance(path.get("closed"), bool):
errors.append(f"{field}.closed must be boolean")
elif primitive == "ground-blade":
blade = descriptor.get("bladeSpec")
field = f"{prefix}.bladeSpec"
if not isinstance(blade, dict):
return [f"{field} must be an object"]
stations = blade.get("stations")
if not _valid_point_array(stations, 3, 2):
errors.append(
f"{field}.stations must contain at least 2 distinct finite "
"[x, spineY, edgeY] coordinates"
)
elif any(float(station[1]) <= float(station[2]) for station in stations):
errors.append(f"{field}.stations must keep spineY greater than edgeY")
elif any(
float(stations[index][0]) <= float(stations[index - 1][0])
for index in range(1, len(stations))
):
errors.append(f"{field}.stations x coordinates must be strictly increasing")
if not _is_positive_number(blade.get("thickness")):
errors.append(f"{field}.thickness must be a finite positive number")
fraction_defaults = {
"grindFrac": 0.55,
"swedgeFromTipFrac": 0.34,
"spineFlat": 0.30,
}
for fraction, default in fraction_defaults.items():
value = blade.get(fraction, default)
if not (
_is_finite_number(value)
and float(value) >= 0
and float(value) <= 1
):
errors.append(
f"{field}.{fraction} must be a finite number from 0 to 1"
)
grind = blade.get("grindFrac", fraction_defaults["grindFrac"])
spine_flat = blade.get("spineFlat", fraction_defaults["spineFlat"])
if (
_is_finite_number(grind)
and 0 <= float(grind) <= 1
and _is_finite_number(spine_flat)
and 0 <= float(spine_flat) <= 1
and float(grind) + float(spine_flat) >= 1
):
errors.append(f"{field}.grindFrac + spineFlat must be less than 1")
elif primitive == "curve-sweep":
sweep = descriptor.get("curveSweep")
field = f"{prefix}.curveSweep"
if not isinstance(sweep, dict):
return [f"{field} must be an object"]
if not _valid_point_array(sweep.get("spine"), 3, 2):
errors.append(
f"{field}.spine must contain at least 2 distinct finite [x, y, z] coordinates"
)
cross_section = sweep.get("crossSection")
if not isinstance(cross_section, dict):
errors.append(f"{field}.crossSection must be an object")
elif not _valid_point_array(cross_section.get("points"), 2, 3):
errors.append(
f"{field}.crossSection.points must contain at least 3 distinct finite "
"[x, y] coordinates"
)
if not isinstance(sweep.get("closed"), bool):
errors.append(f"{field}.closed must be boolean")
return errors
def _resolved_dimensions(component: dict) -> tuple[object, object, object]:
dimensions = component.get("dimensions")
if not isinstance(dimensions, dict):
return None, None, None
radius = dimensions.get("radius")
diameter = float(radius) * 2 if _is_positive_number(radius) else None
width = dimensions.get("width", diameter)
height = dimensions.get("height", dimensions.get("length"))
depth = dimensions.get("depth", diameter)
if component.get("primitive") == "plane-card" and depth is None:
depth = 1.0
return width, height, depth
def hosted_spec_errors(spec: dict) -> list[str]:
"""Return deterministic limitations of the hosted preview compiler.
The upstream schema accepts procedural primitive families that the
vendored generator still represents with placeholder boxes. The hosted
service rejects those before generation rather than returning a model
that silently disagrees with its spec. Parent cycles are also rejected
explicitly so generator recursion cannot hang.
"""
components = [
item for item in spec.get("componentTree", []) if isinstance(item, dict)
]
errors: list[str] = []
ids = {
str(item.get("id")) for item in components
if isinstance(item.get("id"), str) and item.get("id")
}
parents: dict[str, str | None] = {}
for index, component in enumerate(components):
component_id = component.get("id")
label = str(component_id or f"componentTree[{index}]")
primitive = str(component.get("primitive") or "")
if primitive not in HOSTED_PRIMITIVES:
errors.append(
f"hosted preview: component {label!r} uses unsupported primitive "
f"{primitive!r}; choose one of {', '.join(sorted(HOSTED_PRIMITIVES))}"
)
dimensions = _resolved_dimensions(component)
if not all(_is_positive_number(value) for value in dimensions):
errors.append(
f"hosted preview: component {label!r} needs positive width, height, "
"and depth dimensions (radius/length aliases are accepted)"
)
if primitive in {
"extrude", "lathe", "tube", "ground-blade", "curve-sweep",
"instanced-cluster",
}:
descriptor = component.get("geometryDescriptor")
if not isinstance(descriptor, dict):
errors.append(
f"hosted preview: component {label!r} geometryDescriptor must be an object"
)
elif primitive == "instanced-cluster":
base = descriptor.get("baseGeometry")
if not isinstance(base, str) or not base:
errors.append(
f"hosted preview: component {label!r} "
"geometryDescriptor.baseGeometry must name a supported primitive"
)
elif base == "instanced-cluster":
errors.append(
f"hosted preview: component {label!r} "
"geometryDescriptor.baseGeometry cannot reference instanced-cluster itself"
)
elif base not in HOSTED_PRIMITIVES:
errors.append(
f"hosted preview: component {label!r} "
f"geometryDescriptor.baseGeometry uses unsupported primitive {base!r}"
)
else:
errors.extend(
_advanced_geometry_errors(label, base, descriptor)
)
else:
errors.extend(
_advanced_geometry_errors(label, primitive, descriptor)
)
if isinstance(component_id, str) and component_id:
parent = component.get("parent")
parents[component_id] = str(parent) if parent is not None else None
for component_id, parent in parents.items():
if parent is not None and parent not in ids:
errors.append(
f"hosted preview: component {component_id!r} references missing parent {parent!r}"
)
continue
seen: set[str] = set()
cursor: str | None = component_id
while cursor is not None and cursor in parents:
if cursor in seen:
errors.append(
f"hosted preview: component parent cycle contains {cursor!r}"
)
break
seen.add(cursor)
cursor = parents[cursor]
return list(dict.fromkeys(errors))
def prepare_hosted_preview(spec: dict) -> dict:
"""Create an app-only compile manifest without claiming visual approval.
``spec`` remains the strict-gated upstream artifact with its locked pass
order and empty review history. The returned deep copy contains one
explicitly unreviewed preview pass referencing every declared component.
It is compiled solely to let users inspect the procedural draft; it is
not an upstream ``continue`` decision and carries no scores or evidence.
"""
preview = copy.deepcopy(spec)
component_refs = [
item["id"] for item in preview.get("componentTree", [])
if isinstance(item, dict) and isinstance(item.get("id"), str) and item["id"]
]
source_order = pass_order(spec)
preview["buildPasses"] = [{
"id": HOSTED_PREVIEW_PASS,
"label": "Hosted unreviewed preview",
"goal": "Compile all strict-validated components for inspection only",
"componentRefs": component_refs,
"acceptance": ["No visual acceptance is claimed by this preview"],
"acceptanceCriteria": ["No visual acceptance is claimed by this preview"],
}]
preview["sculptPipeline"] = {
"passGateMode": "hosted-unreviewed-preview",
"passOrder": [HOSTED_PREVIEW_PASS],
"currentPass": HOSTED_PREVIEW_PASS,
"completedPasses": [],
"sourcePassOrder": source_order,
}
preview["reviewHistory"] = []
preview["hostedPreview"] = {
"reviewStatus": "unreviewed",
"sourcePassOrder": source_order,
"notice": (
"Compile-only preview. No screenshot comparison, AI-vision score, "
"or upstream build-pass approval has occurred."
),
}
return preview
def pass_order(spec: dict) -> list[str]:
ids = [
item["id"]
for item in spec.get("buildPasses", [])
if isinstance(item, dict) and isinstance(item.get("id"), str) and item["id"].strip()
]
return ids or ["blockout"]
_FACTORY_EXPORT_RE = re.compile(r"export function (create\w+Model)\b")
def factory_export_name(ts_source: str) -> str | None:
"""Extract the ``create<Name>Model`` export from generated TypeScript."""
match = _FACTORY_EXPORT_RE.search(ts_source)
return match.group(1) if match else None
|