Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies = [
"tree-sitter-cpp>=0.23.0",
"tree-sitter-java>=0.23.0",
"uvicorn>=0.41.0",
"google-genai>=1.0.0",
]

[project.scripts]
Expand Down
44 changes: 43 additions & 1 deletion backend/src/lions/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,40 @@ async def _run():
asyncio.run(_run())


def cmd_lint(args):
"""Run semantic lint checks on source files."""
from lions.lint import run_lint
from lions.lint.formatters import FORMATTERS

if args.init:
from lions.lint.baseline_miner import find_project_root, load_or_infer_rules, write_inferred_rules
from lions.lint.manifesto import gather_manifesto, write_manifesto

root = find_project_root(args.path or ".")
rules = load_or_infer_rules(str(root), refresh=True)
rules_path = write_inferred_rules(str(root), rules)
manifesto = gather_manifesto(str(root), refresh_inferred_rules=False)
manifesto_path = write_manifesto(str(root), manifesto)
print(f"Project root: {root}")
print(f"Manifesto written to {manifesto_path}")
print(f"Inferred rules written to {rules_path} ({len(rules)} rules)")
return

if not args.path:
print("Error: path is required (use 'lions lint <path>')", file=sys.stderr)
sys.exit(1)

checks = [args.check] if args.check else None
result = run_lint(args.path, checks=checks, model=args.model)

formatter = FORMATTERS[args.format]
print(formatter(result))

# Exit code: 1 if any errors found
if any(d.severity == "error" for d in result.diagnostics):
sys.exit(1)


def cmd_serve(args):
import uvicorn
from lions.db import init_db
Expand Down Expand Up @@ -475,6 +509,14 @@ def main():
p_guide.add_argument("--version", help="Version (commit SHA, default: auto-detect from DB)")
p_guide.add_argument("--model", default="claude-sonnet-4-6", help="Claude model")

# lint
p_lint = sub.add_parser("lint", help="Run semantic lint checks")
p_lint.add_argument("path", nargs="?", help="File or directory to lint")
p_lint.add_argument("--format", "-f", choices=["json", "text", "sarif"], default="text", help="Output format (default: text)")
p_lint.add_argument("--check", choices=["intent", "arch", "contract"], help="Run only one check")
p_lint.add_argument("--model", default=None, help="LLM model (default: claude-haiku-4-5)")
p_lint.add_argument("--init", action="store_true", help="Generate/refresh .lions/manifesto.txt")

# costs
p_costs = sub.add_parser("costs", help="Show pipeline cost summary")
p_costs.add_argument("--repo", help="Filter by repo (e.g., antirez/rax)")
Expand All @@ -491,7 +533,7 @@ def main():
parser.print_help()
sys.exit(1)

{"parse": cmd_parse, "analyze": cmd_analyze, "annotate": cmd_annotate, "migrate": cmd_migrate, "summarize": cmd_summarize, "guide": cmd_guide, "costs": cmd_costs, "serve": cmd_serve}[
{"parse": cmd_parse, "analyze": cmd_analyze, "annotate": cmd_annotate, "migrate": cmd_migrate, "summarize": cmd_summarize, "guide": cmd_guide, "lint": cmd_lint, "costs": cmd_costs, "serve": cmd_serve}[
args.command
](args)

Expand Down
97 changes: 97 additions & 0 deletions backend/src/lions/lint/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""Lions Code semantic linter -- orchestrator."""

import sys
from pathlib import Path

from lions.models.lint import LintDiagnostic, LintResult


def _collect_files(path: str) -> list[Path]:
"""Collect lintable source files from a path."""
from lions.pipeline.stage1_parse import is_language_supported

p = Path(path).resolve()
if p.is_file():
if is_language_supported(str(p)):
return [p]
print(f"Warning: {p} is not a supported language, skipping.", file=sys.stderr)
return []

if p.is_dir():
files = []
for f in sorted(p.rglob("*")):
if f.is_file() and is_language_supported(str(f)):
files.append(f)
return files

print(f"Error: {path} is not a file or directory.", file=sys.stderr)
return []


def run_lint(
path: str,
checks: list[str] | None = None,
model: str | None = None,
) -> LintResult:
"""Run semantic lint checks on a file or directory.

Args:
path: File or directory to lint.
checks: List of check names to run. None = all checks.
model: LLM model to use. None = default.
"""
from lions.lint.llm import DEFAULT_MODEL

model = model or DEFAULT_MODEL
all_checks = checks or ["intent", "arch", "contract"]
files = _collect_files(path)

if not files:
return LintResult(files_checked=0, diagnostics=[])

# Parse all files with Stage 1
from lions.pipeline.stage1_parse import parse_file_extended

file_atoms = {}
for f in files:
source = f.read_text()
file_atoms[str(f)] = (source, parse_file_extended(source, file_path=str(f)))

diagnostics: list[LintDiagnostic] = []

# Run selected checks
if "intent" in all_checks:
from lions.lint.checks.intent_alignment import check_intent_alignment

for fpath, (source, atoms) in file_atoms.items():
diagnostics.extend(check_intent_alignment(source, atoms, model=model))

if "arch" in all_checks:
from lions.lint.checks.arch_drift import check_arch_drift
from lions.lint.manifesto import load_or_gather_manifesto

root = Path(path).resolve()
if root.is_file():
root = root.parent
manifesto = load_or_gather_manifesto(str(root))
if manifesto:
for fpath, (source, atoms) in file_atoms.items():
diagnostics.extend(check_arch_drift(source, atoms, manifesto, model=model))

if "contract" in all_checks and len(files) > 1:
from lions.lint.checks.silent_contract import check_silent_contract
from lions.models.atoms import ExtendedFileAtoms
from lions.pipeline.stage2_analyze import analyze_repo

all_atoms: dict[str, ExtendedFileAtoms] = {fp: atoms for fp, (_, atoms) in file_atoms.items()}
analysis = analyze_repo(all_atoms, provider="local_file", resource_id=path, version="local")
diagnostics.extend(check_silent_contract(analysis, file_atoms, model=model))

# Sort diagnostics by file, then line
diagnostics.sort(key=lambda d: (d.location.file, d.location.line))

return LintResult(
files_checked=len(files),
diagnostics=diagnostics,
manifesto_used="arch" in all_checks,
)
Loading