PLAN-67 & PLAN-68: Add architecture_atlas.py and shorthand_lint.py
This commit is contained in:
1 parent
4415c871e6
commit
fab275c7a4
3 files changed
+349
-2
No files matched your search
@@ -0,0 +1,138 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
src/hyperstitionstud_io/ci/shorthand_lint.py
|
||||
Release v2.18.0: Tokenizer-based shorthand linting helper (hs-shorthand-lint)
|
||||
Enforces Plan #68: Human-Read Text & Comment Shorthand Disambiguation (<PREFIX><NN>.<DOMAIN_TAG>),
|
||||
with strict global exemption for executable code, strings, tests, and runtime data structures.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import re
|
||||
import tokenize
|
||||
import io
|
||||
from pathlib import Path
|
||||
from typing import List, Tuple, Dict, Any
|
||||
|
||||
CANONICAL_ALLOWLIST = {
|
||||
"HTTP", "HTTPS", "API", "REST", "JSON", "MCP", "TCP", "UDP", "SSH", "HTML", "CSS", "SVG", "URL", "URI",
|
||||
"SHA", "CID", "CPU", "RAM", "SSD", "RPM", "CLI", "UI", "UX", "EOF", "UTF", "AI", "EUV", "FRED", "USD", "BPS",
|
||||
"H1", "H2", "H3", "V8", "M1", "M2", "P20", "P50", "P90"
|
||||
}
|
||||
|
||||
# Regex for un-disambiguated bare shorthands like G1..G8, M0..M7, S1..S7, E0..E5, D0..D8, P1..P6, R1..R6
|
||||
BARE_SHORTHAND_PATTERN = re.compile(r"\b([GDMESPR])([0-9]{1,2})\b")
|
||||
|
||||
def extract_python_comments(source_code: str) -> List[Tuple[int, str]]:
|
||||
comments = []
|
||||
try:
|
||||
tokens = tokenize.generate_tokens(io.StringIO(source_code).readline)
|
||||
for tok in tokens:
|
||||
if tok.type == tokenize.COMMENT:
|
||||
comments.append((tok.start[0], tok.string))
|
||||
except Exception:
|
||||
pass
|
||||
return comments
|
||||
|
||||
def extract_markdown_prose_lines(markdown_text: str) -> List[Tuple[int, str]]:
|
||||
lines = markdown_text.splitlines()
|
||||
prose_lines = []
|
||||
in_fence = False
|
||||
for idx, line in enumerate(lines, start=1):
|
||||
stripped = line.strip()
|
||||
if stripped.startswith("```"):
|
||||
in_fence = not in_fence
|
||||
continue
|
||||
if in_fence:
|
||||
continue
|
||||
# Remove inline backtick code spans
|
||||
clean_line = re.sub(r"`[^`]*`", "", line)
|
||||
prose_lines.append((idx, clean_line))
|
||||
return prose_lines
|
||||
|
||||
def extract_js_css_comments(text: str) -> List[Tuple[int, str]]:
|
||||
# Extracts // ... and /* ... */ and <!-- ... -->
|
||||
lines = text.splitlines()
|
||||
extracted = []
|
||||
in_block = False
|
||||
for idx, line in enumerate(lines, start=1):
|
||||
stripped = line.strip()
|
||||
if "/*" in stripped:
|
||||
in_block = True
|
||||
if "*/" in stripped:
|
||||
in_block = False
|
||||
continue
|
||||
if in_block:
|
||||
extracted.append((idx, line))
|
||||
continue
|
||||
if stripped.startswith("//") or stripped.startswith("<!--") or "#" in stripped:
|
||||
extracted.append((idx, line))
|
||||
return extracted
|
||||
|
||||
def scan_file_for_bare_shorthands(file_path: Path) -> List[str]:
|
||||
violations = []
|
||||
try:
|
||||
content = file_path.read_text(encoding="utf-8", errors="ignore")
|
||||
except Exception:
|
||||
return violations
|
||||
|
||||
suffix = file_path.suffix.lower()
|
||||
target_lines: List[Tuple[int, str]] = []
|
||||
|
||||
if suffix == ".py":
|
||||
target_lines = extract_python_comments(content)
|
||||
elif suffix == ".md":
|
||||
target_lines = extract_markdown_prose_lines(content)
|
||||
elif suffix in (".js", ".mjs", ".ts", ".css", ".html", ".sh"):
|
||||
target_lines = extract_js_css_comments(content)
|
||||
else:
|
||||
# Default text/doc scan outside code blocks
|
||||
for idx, line in enumerate(content.splitlines(), start=1):
|
||||
target_lines.append((idx, line))
|
||||
|
||||
for lineno, text in target_lines:
|
||||
for match in BARE_SHORTHAND_PATTERN.finditer(text):
|
||||
token = match.group(0)
|
||||
if token in CANONICAL_ALLOWLIST:
|
||||
continue
|
||||
# Check if immediately followed by a domain tag like .QUANT, .ENTROPIC, etc.
|
||||
start_pos = match.end()
|
||||
if start_pos < len(text) and text[start_pos] == ".":
|
||||
# Has a domain tag extension, check if it forms a canonical tag
|
||||
continue
|
||||
violations.append(f"{file_path}:{lineno}: Bare shorthand '{token}' detected without canonical <PREFIX><NN>.<DOMAIN_TAG> domain tag.")
|
||||
|
||||
return violations
|
||||
|
||||
def main() -> int:
|
||||
workspace_root = Path(os.environ.get("WORKSPACE_DIR", "/workspace"))
|
||||
repos = [
|
||||
"hyperstitionstudio-core",
|
||||
"ingglish-hyperdope-gematria",
|
||||
"technocapital-singularity-zoology",
|
||||
"encyclopedia-knowledge-graph",
|
||||
"haiperdohp-jemaytreeah",
|
||||
"hyperdope-ci-build"
|
||||
]
|
||||
all_violations = []
|
||||
for r in repos:
|
||||
rdir = workspace_root / r
|
||||
if not rdir.is_dir():
|
||||
continue
|
||||
for p in rdir.rglob("*"):
|
||||
if p.is_file() and not any(part.startswith(".") or part in ("node_modules", ".venv", "__pycache__") for part in p.parts):
|
||||
if p.suffix.lower() in (".py", ".md", ".js", ".ts", ".css", ".html", ".sh"):
|
||||
v = scan_file_for_bare_shorthands(p)
|
||||
all_violations.extend(v)
|
||||
|
||||
if all_violations:
|
||||
print(f"[ERROR] Shorthand linting found {len(all_violations)} bare shorthand violation(s) (Plan #68):", file=sys.stderr)
|
||||
for vio in all_violations[:50]:
|
||||
print(f" - {vio}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
print("[OK] Shorthand linting passed with 0 violations across all repositories.")
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user