Official Codebase of 3DCodeVerse
Python
languages: one line_of, one ws_rel and one js_sources for the lints — two byte-identical _line_of, two _rel and two JS-file enumerations
gaoypeng0665679564ca
diff --git a/harness/codeverse3d/languages/_common.py b/harness/codeverse3d/languages/_common.pyindex bb539664..decd6391 100644--- a/harness/codeverse3d/languages/_common.py+++ b/harness/codeverse3d/languages/_common.py@@ -8,7 +8,8 @@ and the runtime turns those into a :class:`BuildResult`. This module owns:wrapper subprocess, then :func:`compose_build_result`;* :func:`compose_build_result` — wrapper json + process outcome → BuildResult,failing loud when the wrapper did not report, published as ``build.json``;-* :func:`read_json_file` / :func:`strip_blender_noise`.+* :func:`read_json_file` / :func:`strip_blender_noise`;+* :func:`line_of` / :func:`ws_rel` — the lints' finding locations.Wrappers themselves are standalone scripts in ``languages/wrappers/`` (they neverimport ``codeverse3d``; Blender's bundled python cannot see this package).@@ -56,6 +57,19 @@ class WrapperError(RuntimeError):"""The wrapper process did not produce a readable ``build.json`` (harness bug or crash)."""+def line_of(text: str, pos: int) -> int:+ """1-based line of character offset ``pos``."""+ return text.count("\n", 0, pos) + 1+++def ws_rel(ws: Workspace, p: Path) -> str:+ """``p`` workspace-relative (posix), or as given when it lies outside the workspace."""+ try:+ return p.relative_to(ws.root).as_posix()+ except ValueError:+ return str(p)++def strip_blender_noise(text: str) -> str:"""Drop Blender's boilerplate stderr/stdout lines so the agent sees signal only."""keep = [diff --git a/harness/codeverse3d/languages/_js_lint.py b/harness/codeverse3d/languages/_js_lint.pyindex 884b45ee..b4be6540 100644--- a/harness/codeverse3d/languages/_js_lint.py+++ b/harness/codeverse3d/languages/_js_lint.py@@ -26,6 +26,7 @@ from enum import StrEnumfrom pathlib import Pathfrom codeverse3d.contracts.artifacts import GateFinding+from codeverse3d.languages._common import line_offrom codeverse3d.spatial.node import NodeError, run_node, runtime_js_dir# threejs's regexes (the superset: side-effect ``import './x.js'``, ``export * from``,@@ -80,11 +81,19 @@ def node_check_syntax(paths: Sequence[Path]) -> dict[Path, SyntaxProblem]:return out+def js_sources(src: Path, *, dotfiles: bool = False, mjs: bool = False) -> list[Path]:+ """The ``*.js`` under ``src`` (never ``node_modules``); scene also lints dot-files and ``*.mjs``."""+ if not src.is_dir():+ return []+ js = sorted(p for p in src.rglob("*.js") if "node_modules" not in p.parts and (dotfiles or not p.name.startswith(".")))+ return js + (sorted(src.rglob("*.mjs")) if mjs else [])++def import_specs(src: str) -> list[tuple[str, int]]:"""``(specifier, 1-based line)`` for every static, re-export and dynamic import in ``src``."""out = [(m.group(1), m.start(1)) for m in _IMPORT_RE.finditer(src)]out += [(m.group(1), m.start(1)) for m in _DYN_IMPORT_RE.finditer(src)]- return [(spec, src.count("\n", 0, pos) + 1) for spec, pos in out]+ return [(spec, line_of(src, pos)) for spec, pos in out]class ImportKind(StrEnum):diff --git a/harness/codeverse3d/languages/blender/__init__.py b/harness/codeverse3d/languages/blender/__init__.pyindex 71ac3ac0..eb884ed5 100644--- a/harness/codeverse3d/languages/blender/__init__.py+++ b/harness/codeverse3d/languages/blender/__init__.py@@ -20,7 +20,7 @@ from codeverse3d.languages._ast_lint import (dotted,safe_parse,)-from codeverse3d.languages._common import run_wrapper_build, strip_blender_noise+from codeverse3d.languages._common import run_wrapper_build, strip_blender_noise, ws_relfrom codeverse3d.languages.base import RuntimeLayoutfrom codeverse3d.proc import scrub_secretsfrom codeverse3d.workspace import Workspace@@ -322,10 +322,6 @@ def source_files(ws: Workspace) -> list[Path]:return ([entry] if entry.is_file() else []) + rest-def _rel(ws: Workspace, p: Path) -> str:- return p.relative_to(ws.root).as_posix()--def _exported_functions(tree: ast.Module) -> set[str]:return {n.name for n in tree.body if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef))}@@ -355,7 +351,7 @@ def _layout_rules(ws: Workspace, parts: list[Path], entry_tree: ast.Module | NonE, W = Severity.ERROR, Severity.WARNimported = _imported_part_modules(entry_tree) if entry_tree is not None else set()for p in parts:- target = _rel(ws, p)+ target = ws_rel(ws, p)stem = p.stemif stem != to_snake(stem):out.append(_f(E, f"part file name '{p.name}' is not snake_case", target=target,@@ -394,7 +390,7 @@ def lint_workspace(ws: Workspace) -> GateReport:parts = part_files(ws)findings: list[GateFinding] = []for p in source_files(ws):- target = _rel(ws, p)+ target = ws_rel(ws, p)is_entry = p == entryis_helper = p.name.startswith("_")rep = lint_blender_source(p.read_text(), target=target,diff --git a/harness/codeverse3d/languages/glsl_shader/__init__.py b/harness/codeverse3d/languages/glsl_shader/__init__.pyindex 7590d98c..842c2f6e 100644--- a/harness/codeverse3d/languages/glsl_shader/__init__.py+++ b/harness/codeverse3d/languages/glsl_shader/__init__.py@@ -10,7 +10,7 @@ from pathlib import Pathfrom codeverse3d.contracts.artifacts import BuildResult, GateFinding, GateReport, Severityfrom codeverse3d.contracts.common import ENTRY_FILE, Languagefrom codeverse3d.contracts.plan import GraphicsPlan, Plan-from codeverse3d.languages._common import MISSING_ENTRY+from codeverse3d.languages._common import MISSING_ENTRY, line_of, ws_relfrom codeverse3d.languages._gl_common import ( # noqa: F401 — re-exportedGlslMessage,LineMap,@@ -157,10 +157,6 @@ _LEGACY = {}_FLOAT_LITERAL_MOD = re.compile(r"(\d\.\d*|\.\d+)\s*%|%\s*(\d\.\d*|\.\d+)")_PRECISION = re.compile(r"^[ \t]*precision\s+(lowp|mediump|highp)\s+\w+\s*;", re.M)-def _line_of(text: str, pos: int) -> int:- return text.count("\n", 0, pos) + 1--def defined_functions(text: str) -> dict[str, int]:"""``{name: line}`` of every top-level function definition in a GLSL text (comments blanked)."""out: dict[str, int] = {}@@ -183,38 +179,38 @@ def _check_file(rel: str, text: str, *, role: str, recipe_names: Collection[str]add(Severity.WARN, "too_long", f"file is {raw_len} chars; keep shaders compact", "remove dead code; move helpers to src/common.glsl")for m in _VERSION.finditer(text):add(Severity.ERROR, "version_line", "contains a #version line; the harness prepends `#version 330 core`",- "delete the #version line", _line_of(text, m.start()))+ "delete the #version line", line_of(text, m.start()))for m in _INCLUDE.finditer(text):add(Severity.ERROR, "include", "#include is not GLSL; src/common.glsl is pasted in automatically",- "delete the #include line; functions in src/common.glsl are already visible", _line_of(text, m.start()))+ "delete the #include line; functions in src/common.glsl are already visible", line_of(text, m.start()))for m in _UNIFORM_DECL.finditer(text):name = m.group(1)if name in UNIFORM_NAMES:add(Severity.ERROR, "redeclared_uniform", f"redeclares harness uniform `{name}`",- f"delete the declaration; `{name}` is provided by the harness header", _line_of(text, m.start()))+ f"delete the declaration; `{name}` is provided by the harness header", line_of(text, m.start()))else:add(Severity.ERROR, "custom_uniform", f"declares uniform `{name}` which nothing will set","only u_time / u_resolution / u_mouse / u_frame / u_prev / u_noise / u_buffer_a exist; turn it into a `const`",- _line_of(text, m.start()))+ line_of(text, m.start()))for m in _OUT_DECL.finditer(text):add(Severity.ERROR, "redeclared_output", f"declares output `{m.group(1)}`; the harness already declares `out vec4 fragColor`",- "delete the `out vec4 ...;` line and write to fragColor", _line_of(text, m.start()))+ "delete the `out vec4 ...;` line and write to fragColor", line_of(text, m.start()))for m in _TEXTURE.finditer(text):name = m.group(1)if name not in SAMPLERS:add(Severity.ERROR, "undeclared_sampler", f"texture() reads sampler `{name}` which does not exist","only u_prev (previous frame), u_noise (256² RGBA noise) and u_buffer_a (buffer pass) can be sampled; "- "generate patterns procedurally (hash/noise/fbm from the cookbook)", _line_of(text, m.start()))+ "generate patterns procedurally (hash/noise/fbm from the cookbook)", line_of(text, m.start()))for m in _MISSING_INPUTS.finditer(text):add(Severity.ERROR, "missing_input", f"`{m.group(1)}` is not available in this harness","available: u_time/iTime, u_resolution/iResolution, u_frame/iFrame, u_mouse (always 0), u_prev/iChannel0, u_noise/iChannel1",- _line_of(text, m.start()))+ line_of(text, m.start()))for needle, hint in _LEGACY.items():pos = text.find(needle)if pos >= 0:- add(Severity.ERROR, "legacy_glsl", f"uses `{needle.strip()}` (GLSL 1.x)", hint, _line_of(text, pos))+ add(Severity.ERROR, "legacy_glsl", f"uses `{needle.strip()}` (GLSL 1.x)", hint, line_of(text, pos))for m in _FLOAT_LITERAL_MOD.finditer(text):- add(Severity.ERROR, "float_modulo", "`%` on a float literal — GLSL `%` is integer-only", "use mod(x, y)", _line_of(text, m.start()))+ add(Severity.ERROR, "float_modulo", "`%` on a float literal — GLSL `%` is integer-only", "use mod(x, y)", line_of(text, m.start()))for name, line in defined_functions(text).items():if name in recipe_names:add(Severity.ERROR, "redefines_recipe", f"`{name}` is already provided by {RECIPES} — call it instead of redefining it",@@ -232,7 +228,7 @@ def _check_file(rel: str, text: str, *, role: str, recipe_names: Collection[str]add(Severity.WARN, "buffer_self_ref", "buffer_a.frag samples u_buffer_a; inside the buffer pass the previous buffer is `u_prev`","use texture(u_prev, uv) for feedback inside buffer_a.frag")for m in _PRECISION.finditer(text):- add(Severity.INFO, "precision", "precision qualifier is ignored on desktop GLSL 330", "safe to delete", _line_of(text, m.start()))+ add(Severity.INFO, "precision", "precision qualifier is ignored on desktop GLSL 330", "safe to delete", line_of(text, m.start()))if "u_mouse" in text or "iMouse" in text:add(Severity.INFO, "mouse", "u_mouse is always (0,0) when rendered headless", "do not depend on the mouse for the look")return out@@ -258,8 +254,8 @@ def lint_workspace(ws: Workspace) -> GateReport:p = ws.root / relif p.is_file():findings.extend(_check_file(rel, p.read_text(errors="replace"), role=role, recipe_names=reserved))- stray = [str(p.relative_to(ws.root)) for p in ws.src.rglob("*") if p.is_file()- and str(p.relative_to(ws.root)) not in (SHADER, COMMON, BUFFER_A, RECIPES) and p.suffix in (".frag", ".glsl", ".vert", ".py", ".js")]+ stray = [ws_rel(ws, p) for p in ws.src.rglob("*") if p.is_file()+ and ws_rel(ws, p) not in (SHADER, COMMON, BUFFER_A, RECIPES) and p.suffix in (".frag", ".glsl", ".vert", ".py", ".js")]for rel in stray:findings.append(GateFinding(gate=GATE, severity=Severity.WARN, target=rel, message=f"{rel} is not part of the shader contract and is ignored",fix_hint="keep code in src/shader.frag (+ src/common.glsl, src/buffer_a.frag)", data={"kind": "stray_file", "file": rel}))diff --git a/harness/codeverse3d/languages/opengl_python/__init__.py b/harness/codeverse3d/languages/opengl_python/__init__.pyindex af0c907c..462c682b 100644--- a/harness/codeverse3d/languages/opengl_python/__init__.py+++ b/harness/codeverse3d/languages/opengl_python/__init__.py@@ -15,7 +15,7 @@ from codeverse3d.languages._ast_lint import (describe_parse_failure,safe_parse,)-from codeverse3d.languages._common import MISSING_ENTRY+from codeverse3d.languages._common import MISSING_ENTRY, ws_relfrom codeverse3d.languages._gl_common import (finish_build,invalidate_stale_outputs,@@ -172,7 +172,7 @@ def lint_workspace(ws: Workspace) -> GateReport:return GateReport(gate=GATE, passed=False, findings=f)findings = lint_source(p.read_text(errors="replace"))for extra in sorted(ws.src.rglob("*.py")):- rel = str(extra.relative_to(ws.root))+ rel = ws_rel(ws, extra)if rel != PROGRAM:findings.append(GateFinding(gate=GATE, severity=Severity.WARN, target=rel, message=f"{rel}: extra python module is ignored by the harness",fix_hint="keep all code in src/program.py (GLSL may live in src/*.glsl)", data={"kind": "stray_file", "file": rel}))diff --git a/harness/codeverse3d/languages/scene_threejs/__init__.py b/harness/codeverse3d/languages/scene_threejs/__init__.pyindex 8d7549f2..7834d96b 100644--- a/harness/codeverse3d/languages/scene_threejs/__init__.py+++ b/harness/codeverse3d/languages/scene_threejs/__init__.py@@ -20,11 +20,13 @@ from codeverse3d.contracts.artifacts import (from codeverse3d.contracts.common import ENTRY_FILE, Languagefrom codeverse3d.contracts.plan import AssetPlan, CameraPlan, Plan, ScenePlan, ZonePlanfrom codeverse3d.conventions import to_pascal, to_snake+from codeverse3d.languages._common import line_of, ws_relfrom codeverse3d.languages._js_lint import (ImportKind,ImportVerdict,SyntaxProblem,check_imports,+ js_sources,node_check_syntax,)from codeverse3d.workspace import Workspace@@ -58,12 +60,6 @@ def _f(sev: Severity, msg: str, *, target: str, hint: str = "", **data: object)return GateFinding(gate=GATE, severity=sev, target=target, message=msg, fix_hint=hint, data=dict(data))-def _js_files(ws: Workspace) -> list[Path]:- if not ws.src.is_dir():- return []- return sorted(p for p in ws.src.rglob("*.js") if "node_modules" not in p.parts) + sorted(ws.src.rglob("*.mjs"))--def _is_lib(path: Path, ws: Workspace) -> bool:"""Is this one of the harness's own effect modules (``src/lib/*.js``)?@@ -104,7 +100,7 @@ def lint(ws: Workspace) -> GateReport:"""Run all static checks; passed iff no ERROR findings."""t0 = time.time()findings: list[GateFinding] = []- files = _js_files(ws)+ files = js_sources(ws.src, dotfiles=True, mjs=True)scene = ws.src / "scene.js"if not scene.is_file():findings.append(_f(Severity.ERROR, "src/scene.js is missing", target="src/scene.js",@@ -112,7 +108,7 @@ def lint(ws: Workspace) -> GateReport:big: list[str] = []syntax = node_check_syntax(files)for path in files:- rel = path.relative_to(ws.root).as_posix()+ rel = ws_rel(ws, path)text = path.read_text(errors="replace")if path in syntax:findings.append(_syntax_finding(syntax[path], rel))@@ -123,7 +119,7 @@ def lint(ws: Workspace) -> GateReport:for pat, sev, msg, hint in _PATTERNS:m = pat.search(text)if m:- line = text.count("\n", 0, m.start()) + 1+ line = line_of(text, m.start())findings.append(_f(sev, msg, target=f"{rel}:{line}", hint=hint))n_lines = text.count("\n") + 1if n_lines > MAX_LINES:diff --git a/harness/codeverse3d/languages/threejs/__init__.py b/harness/codeverse3d/languages/threejs/__init__.pyindex 2d6ffd0f..53061ebd 100644--- a/harness/codeverse3d/languages/threejs/__init__.py+++ b/harness/codeverse3d/languages/threejs/__init__.py@@ -14,11 +14,12 @@ from codeverse3d.contracts.artifacts import BuildResult, GateFinding, GateReportfrom codeverse3d.contracts.common import ENTRY_FILE, Languagefrom codeverse3d.contracts.plan import Plan, StaticPlanfrom codeverse3d.conventions import to_pascal, to_snake-from codeverse3d.languages._common import BUILD_TIMEOUT+from codeverse3d.languages._common import BUILD_TIMEOUT, line_of, ws_relfrom codeverse3d.languages._js_lint import (ImportKind,ImportVerdict,check_imports,+ js_sources,)from codeverse3d.languages._js_lint import (node_check_syntax as check_syntax, # module-level name: tests monkeypatch it@@ -133,26 +134,9 @@ def _strip(src: str) -> str:return _TOKEN_RE.sub(repl, src)-def _line_of(text: str, pos: int) -> int:- return text.count("\n", 0, pos) + 1---def _rel(ws: Workspace, p: Path) -> str:- try:- return str(p.relative_to(ws.root))- except ValueError:- return str(p)---def list_sources(ws: Workspace) -> list[Path]:- if not ws.src.is_dir():- return []- return sorted(p for p in ws.src.rglob("*.js") if "node_modules" not in p.parts and not p.name.startswith("."))--def _lint_imports(ws: Workspace, path: Path, src: str, findings: list[GateFinding]) -> set[Path]:"""Validate import specifiers; return the set of resolved relative targets."""- rel = _rel(ws, path)+ rel = ws_rel(ws, path)def make_finding(v: ImportVerdict, spec: str, line: int) -> GateFinding:if v.kind is ImportKind.ESCAPES:@@ -162,7 +146,7 @@ def _lint_imports(ws: Workspace, path: Path, src: str, findings: list[GateFindinassert v.target is not Nonereturn GateFinding(gate=GATE, severity=Severity.ERROR, target=rel,message=f"{rel}:{line}: imported file does not exist: '{spec}'",- fix_hint=f"create {_rel(ws, v.target)} or fix the path (extension '.js' is required)")+ fix_hint=f"create {ws_rel(ws, v.target)} or fix the path (extension '.js' is required)")kind = "URL" if v.kind is ImportKind.URL else "package"return GateFinding(gate=GATE, severity=Severity.ERROR, target=rel,message=f"{rel}:{line}: import of {kind} '{spec}' is not allowed",@@ -175,12 +159,12 @@ def _lint_imports(ws: Workspace, path: Path, src: str, findings: list[GateFindindef _lint_forbidden(ws: Workspace, path: Path, src: str, findings: list[GateFinding]) -> None:stripped = _strip(src)- rel = _rel(ws, path)+ rel = ws_rel(ws, path)for pattern, (sev, msg, hint) in _FORBIDDEN.items():m = re.search(pattern, stripped)if m:findings.append(GateFinding(gate=GATE, severity=sev, target=rel,- message=f"{rel}:{_line_of(stripped, m.start())}: {msg}", fix_hint=hint))+ message=f"{rel}:{line_of(stripped, m.start())}: {msg}", fix_hint=hint))def _exports(src: str) -> set[str]:@@ -202,7 +186,7 @@ def lint_workspace(ws: Workspace) -> GateReport:t0 = time.time()findings: list[GateFinding] = []entry = ws.src / "object.js"- sources = list_sources(ws)+ sources = js_sources(ws.src)syntax = check_syntax(sources)if not entry.is_file():@@ -212,7 +196,7 @@ def lint_workspace(ws: Workspace) -> GateReport:imported: set[Path] = set()for path in sources:src = path.read_text(errors="replace")- rel = _rel(ws, path)+ rel = ws_rel(ws, path)syn = syntax.get(path)if syn is not None:findings.append(GateFinding(gate=GATE, severity=Severity.ERROR, target=rel,@@ -237,8 +221,8 @@ def lint_workspace(ws: Workspace) -> GateReport:part_files = [p for p in sources if p.parent == ws.src / "parts"]for p in part_files:if p not in imported:- findings.append(GateFinding(gate=GATE, severity=Severity.WARN, target=_rel(ws, p),- message=f"{_rel(ws, p)} is not imported by any module (dead part file?)", fix_hint="import and add it in src/object.js or delete it"))+ findings.append(GateFinding(gate=GATE, severity=Severity.WARN, target=ws_rel(ws, p),+ message=f"{ws_rel(ws, p)} is not imported by any module (dead part file?)", fix_hint="import and add it in src/object.js or delete it"))plan = plan_or_none(ws.plan_path)if type(plan) is StaticPlan: # an articulated plan's parts are not threejs part files