Skip to content

Commit e434653

Browse files
authored
Merge pull request #69 from BruinGrowly/cursor/harmonize-and-improve-codebase-5508
Harmonize and improve codebase
2 parents 7681406 + 435d6a7 commit e434653

29 files changed

+2387
-441
lines changed

CI_VERIFICATION_REPORT.md

Lines changed: 541 additions & 0 deletions
Large diffs are not rendered by default.

CODEBASE_IMPROVEMENT_REPORT.md

Lines changed: 803 additions & 0 deletions
Large diffs are not rendered by default.

IMPROVEMENTS_IMPLEMENTED.md

Lines changed: 435 additions & 0 deletions
Large diffs are not rendered by default.

check_harmony.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ def check_harmony(
1616
print(f"Running LJPW Harmony Check on: {os.path.abspath(target_dir)}")
1717
print("=" * 60)
1818

19-
# Load config explicitly if provided, otherwise auto-load
20-
# Note: LegacyCodeMapper loads config automatically from target_dir,
21-
# but if we want to override with a specific file, we might need to adjust ConfigLoader.
22-
# For now, we'll rely on auto-loading from target_dir.
23-
19+
# If analyzing a subdirectory, find project root for config
20+
# Otherwise use target_dir
21+
project_root = os.getcwd() if target_dir != "." else target_dir
22+
23+
# Create mapper - it will load config from project_root
2424
mapper = LegacyCodeMapper(target_dir, quiet=not verbose)
25+
26+
# If we're in project root, use config from there
27+
if os.path.exists(os.path.join(project_root, "pyproject.toml")):
28+
from harmonizer.config import ConfigLoader
29+
mapper.config = ConfigLoader.load(project_root)
30+
2531
mapper.analyze_codebase(show_progress=True)
2632

2733
failures = []

harmonizer/ast_semantic_parser.py

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class AST_Semantic_Parser(ast.NodeVisitor):
2323
"""
2424
A "Rosetta Stone" that translates Python AST nodes into
2525
DIVE-V2 conceptual keywords.
26+
27+
This parser walks through Python's Abstract Syntax Tree and categorizes
28+
code constructs into semantic dimensions (Love, Justice, Power, Wisdom).
29+
30+
Note: Visitor methods don't "visit" in the semantic sense - they record
31+
and categorize AST nodes into semantic concepts for later analysis.
2632
"""
2733

2834
def __init__(self, vocabulary: Set[str]):
@@ -109,9 +115,7 @@ def _map_word_to_concept(self, word: str) -> Optional[str]:
109115
return concept
110116
return None
111117

112-
def get_intent_concepts(
113-
self, function_name: str, docstring: Optional[str]
114-
) -> List[str]:
118+
def get_intent_concepts(self, function_name: str, docstring: Optional[str]) -> List[str]:
115119
"""
116120
Parses the function's name and docstring to find its "Stated Purpose" (Intent).
117121
"""
@@ -131,9 +135,7 @@ def get_intent_concepts(
131135
return [word for word in name_words if word in self.known_vocabulary]
132136
return list(concepts)
133137

134-
def get_execution_map(
135-
self, body: List[ast.AST]
136-
) -> Tuple[Dict[ast.AST, str], List[str]]:
138+
def get_execution_map(self, body: List[ast.AST]) -> Tuple[Dict[ast.AST, str], List[str]]:
137139
"""
138140
Parses the function's body to map each AST node to a semantic dimension
139141
and return the list of concepts found.
@@ -150,7 +152,13 @@ def _add_concept(self, node: ast.AST, concept: str):
150152
self._node_map[node] = concept
151153
self._concepts_found.add(concept)
152154

153-
def visit_Call(self, node: ast.Call):
155+
def visit_Call(self, node: ast.Call) -> None:
156+
"""
157+
Records function/method calls and categorizes them semantically.
158+
159+
Maps method names to semantic dimensions (e.g., 'execute' -> Power,
160+
'validate' -> Justice, 'get' -> Wisdom).
161+
"""
154162
concept = None
155163
if isinstance(node.func, ast.Attribute):
156164
method_name = node.func.attr
@@ -171,36 +179,83 @@ def visit_Call(self, node: ast.Call):
171179
self._add_concept(node, concept)
172180
self.generic_visit(node)
173181

174-
def visit_If(self, node: ast.If):
182+
def visit_If(self, node: ast.If) -> None:
183+
"""
184+
Records If statements as Justice concepts (control flow/decision-making).
185+
186+
If statements enforce conditions and control execution flow, which
187+
aligns with Justice (rules, structure, enforcement).
188+
"""
175189
self._add_concept(node, "justice")
176190
self.generic_visit(node)
177191

178-
def visit_Assert(self, node: ast.Assert):
192+
def visit_Assert(self, node: ast.Assert) -> None:
193+
"""
194+
Records Assert statements as Justice concepts (validation/enforcement).
195+
196+
Assertions enforce invariants and preconditions, directly representing
197+
Justice principles of validation and rule enforcement.
198+
"""
179199
self._add_concept(node, "justice")
180200
self.generic_visit(node)
181201

182-
def visit_Try(self, node: ast.Try):
202+
def visit_Try(self, node: ast.Try) -> None:
203+
"""
204+
Records Try-Except blocks with dual semantics.
205+
206+
Try blocks represent Justice (structural error handling), while
207+
exception handlers represent Love (mercy, graceful recovery).
208+
"""
183209
self._add_concept(node, "justice")
184210
if node.handlers:
185211
self._add_concept(node.handlers[0], "love")
186212
self.generic_visit(node)
187213

188-
def visit_Raise(self, node: ast.Raise):
214+
def visit_Raise(self, node: ast.Raise) -> None:
215+
"""
216+
Records Raise statements as Power concepts (forceful action).
217+
218+
Raising exceptions is an active, forceful interruption of normal
219+
flow, representing Power (control, force, action).
220+
"""
189221
self._add_concept(node, "power")
190222
self.generic_visit(node)
191223

192-
def visit_For(self, node: ast.For):
224+
def visit_For(self, node: ast.For) -> None:
225+
"""
226+
Records For loops as Justice concepts (structured iteration).
227+
228+
For loops impose structure and order on iteration, representing
229+
Justice (rules, patterns, systematic processing).
230+
"""
193231
self._add_concept(node, "justice")
194232
self.generic_visit(node)
195233

196-
def visit_While(self, node: ast.While):
234+
def visit_While(self, node: ast.While) -> None:
235+
"""
236+
Records While loops as Justice concepts (conditional iteration).
237+
238+
While loops enforce conditions for continued iteration, representing
239+
Justice (rules, enforcement, conditional control).
240+
"""
197241
self._add_concept(node, "justice")
198242
self.generic_visit(node)
199243

200-
def visit_Return(self, node: ast.Return):
244+
def visit_Return(self, node: ast.Return) -> None:
245+
"""
246+
Records Return statements as Wisdom concepts (providing results).
247+
248+
Return statements deliver computed results or knowledge back to
249+
callers, representing Wisdom (information, knowledge transfer).
250+
"""
201251
self._add_concept(node, "wisdom")
202252
self.generic_visit(node)
203253

204-
def generic_visit(self, node: ast.AST):
205-
"""This is the default visitor that just continues the walk."""
254+
def generic_visit(self, node: ast.AST) -> None:
255+
"""
256+
Default visitor that continues traversing the AST.
257+
258+
This method is called for AST node types that don't have
259+
specific visitor methods defined.
260+
"""
206261
super().generic_visit(node)

harmonizer/ast_semantic_parser_v2.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
from typing import Dict, List, Optional, Set, Tuple
2222

2323
from harmonizer.programming_constructs_vocabulary import (
24-
PROGRAMMING_VERBS,
2524
COMPOUND_PATTERNS,
25+
PROGRAMMING_VERBS,
2626
)
2727

2828

@@ -73,9 +73,7 @@ def _split_name(self, name: str) -> List[str]:
7373
else:
7474
return self._split_camel_case(name)
7575

76-
def _map_word_to_concept(
77-
self, word: str, context: str = "default"
78-
) -> Optional[str]:
76+
def _map_word_to_concept(self, word: str, context: str = "default") -> Optional[str]:
7977
"""
8078
Map a word to its semantic dimension.
8179
@@ -121,9 +119,7 @@ def _check_compound_pattern(self, words: List[str]) -> Optional[str]:
121119
return COMPOUND_PATTERNS[compound]
122120
return None
123121

124-
def get_intent_concepts(
125-
self, function_name: str, docstring: Optional[str]
126-
) -> List[str]:
122+
def get_intent_concepts(self, function_name: str, docstring: Optional[str]) -> List[str]:
127123
"""
128124
Parse function name and docstring to extract semantic intent.
129125
@@ -158,15 +154,11 @@ def get_intent_concepts(
158154

159155
# Fallback to words in vocabulary
160156
if not concepts and name_words:
161-
concepts.update(
162-
[word for word in name_words if word in self.known_vocabulary]
163-
)
157+
concepts.update([word for word in name_words if word in self.known_vocabulary])
164158

165159
return list(concepts)
166160

167-
def get_execution_map(
168-
self, body: List[ast.AST]
169-
) -> Tuple[Dict[ast.AST, str], List[str]]:
161+
def get_execution_map(self, body: List[ast.AST]) -> Tuple[Dict[ast.AST, str], List[str]]:
170162
"""
171163
Parse function body to map AST nodes to semantic dimensions.
172164

harmonizer/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import os
77
from dataclasses import dataclass, field
8-
from typing import List, Dict, Any
8+
from typing import Any, Dict, List
99

1010
# Try to import tomli for TOML parsing
1111
try:

0 commit comments

Comments
 (0)