Skip to content

feat(python-typescript-grammar-context-engineering): Extract precise data model positions for TypeScript and Python files for precise - #914

Merged
JayGhiya merged 4 commits into
devfrom
ingestion-context-engineering
Oct 18, 2025
Merged

feat(python-typescript-grammar-context-engineering): Extract precise data model positions for TypeScript and Python files for precise#914
JayGhiya merged 4 commits into
devfrom
ingestion-context-engineering

Conversation

@JayGhiya

@JayGhiya JayGhiya commented Oct 18, 2025

Copy link
Copy Markdown
Member

User description

previously we used to read entire files nd only had signal in terms of
codebase which files have data models. now we have a precise locations
in terms of start and end within a file saving lot of context and
serving better performance and precision


PR Type

Enhancement


Description

  • Refactor data model detection to use factory pattern with language-specific strategies

  • Extract precise data model positions (start/end lines) instead of boolean flags

  • Implement Python dataclass detection via Tree-sitter queries

  • Implement TypeScript interface/type alias detection via Tree-sitter queries

  • Remove linting step from codebase processing pipeline


Diagram Walkthrough

flowchart LR
  A["detect_data_model<br/>Main Entry Point"] -->|Factory Pattern| B["DataModelDetectorFactory"]
  B -->|Python| C["PythonDataModelDetectorStrategy"]
  B -->|TypeScript| D["TypeScriptDataModelDetectorStrategy"]
  C -->|Tree-sitter Query| E["dataclasses.scm"]
  D -->|Tree-sitter Query| F["types.scm"]
  E -->|Returns| G["DataModelPosition<br/>with line ranges"]
  F -->|Returns| G
  G -->|Stored in| H["UnoplatFile Model"]
Loading

File Walkthrough

Relevant files
Enhancement
11 files
data_model_detector.py
Refactor to factory pattern with position extraction         
+45/-38 
data_model_detector_factory.py
New factory for language-specific detector strategies       
+53/-0   
data_model_detector_strategy.py
Abstract base class for detection strategies                         
+32/-0   
python_data_model_detector_strategy.py
Python dataclass detection via Tree-sitter                             
+111/-0 
typescript_data_model_detector_strategy.py
TypeScript interface/type alias detection strategy             
+180/-0 
unoplat_file.py
Add data_model_positions field to file model                         
+10/-4   
generic_codebase_parser.py
Persist data_model_positions to Neo4j database                     
+7/-4     
python_processor.py
Update to handle tuple return from detect_data_model         
+3/-2     
generic_codebase_processing_activity.py
Remove linting step from processing pipeline                         
+1/-46   
dataclasses.scm
Tree-sitter query for Python dataclass detection                 
+13/-0   
types.scm
Simplified Tree-sitter query for TypeScript types               
+5/-45   
Miscellaneous
3 files
__init__.py
Python detector package initialization                                     
+1/-0     
__init__.py
TypeScript detector package initialization                             
+1/-0     
db.py
Remove unused SQLModel import                                                       
+0/-1     
Documentation
1 files
codebase_child_workflow.py
Update comment to reflect AST generation focus                     
+3/-3     
Tests
1 files
test_typescript_query_validation.py
Add tests for interface/type alias range extraction           
+155/-3 
Configuration changes
1 files
pyproject.toml
Switch to local commons dependency for development             
+2/-2     

@huly-for-github

Copy link
Copy Markdown

Connected to Huly®: UNOPL-1058

@claude

claude Bot commented Oct 18, 2025

Copy link
Copy Markdown

Claude finished @JayGhiya's task —— View job


PR Review: Data Model Position Extraction

Reviewing the pull request for precise data model position extraction in TypeScript and Python files.

Review Tasks

  • Analyze architectural changes (factory/strategy patterns)
  • Review Python data model detector implementation
  • Review TypeScript data model detector implementation
  • Examine Tree-sitter query files
  • Check test coverage and quality
  • Evaluate performance implications
  • Assess security considerations
  • Provide comprehensive feedback and recommendations

🏗️ Architectural Analysis

Positive Changes:

  • Clean Strategy Pattern: The factory/strategy pattern implementation is well-designed and follows good separation of concerns
  • Language Agnostic Interface: DataModelDetectorStrategy provides a clean abstraction for adding new language support
  • Graceful Error Handling: Factory properly handles unsupported languages by returning empty results rather than crashing

Factory Implementation:

  • Line coverage good in data_model_detector_factory.py:43-48
  • Custom exception UnsupportedLanguageForDataModelDetectionError is appropriate
  • Strategy mapping is clean and extensible

🐍 Python Implementation Review

Strengths:

  • Focused Detection: Tree-sitter query correctly targets @dataclass decorated classes only
  • Robust Error Handling: Empty source code and parsing exceptions handled gracefully (python_data_model_detector_strategy.py:61-69)
  • Efficient Caching: @lru_cache(maxsize=1) for parser/query loading is smart optimization
  • Precise Position Extraction: Correctly extracts line ranges with start_point[0] + 1 and end_point[0] + 1

Issues Found:

  1. Query Pattern Risk: The regex (?i)dataclass at dataclasses.scm:12 may match false positives like @my_dataclass_validator or @dataclassfield
  2. Limited Detection Scope: Only detects @dataclass - misses other common Python data models:
    • Pydantic models (BaseModel subclasses)
    • attrs classes (@attr.s)
    • NamedTuple subclasses
    • TypedDict classes

🔷 TypeScript Implementation Review

Strengths:

  • Dual Detection Paths: Supports both structural signature and direct Tree-sitter parsing
  • Comprehensive Coverage: Handles interfaces, type aliases, generic types, and exported constructs
  • Robust Name Extraction: _extract_declared_name() properly handles export/declare keywords and generics
  • Field-Based Parsing: Uses AST field names (child_by_field_name("name")) when available

Issues Found:

  1. Inconsistent Query Compilation: Line 179 uses language.query() vs line 110 uses tree_sitter.Query() - should be consistent
  2. Missing Error Context: Exception handling at lines 114, 82 swallows specific errors without logging
  3. Limited Type Coverage: Focuses only on interfaces/type aliases - could miss:
    • Zod schemas (z.object({...}))
    • Class-validator decorated classes
    • Enum declarations (data-like enums)

🔍 Tree-sitter Query Analysis

Python Query (dataclasses.scm):

  • Correct Structure: Properly targets decorated class definitions
  • Risk: Overly broad regex pattern (?i)dataclass may cause false positives

TypeScript Query (types.scm):

  • Simplified Correctly: Reduced from 50 lines to 7 lines - much cleaner
  • Good Coverage: Captures both interfaces and type aliases at top level

🧪 Test Coverage Assessment

Critical Gap: No dedicated unit tests for the new detector strategies were found. The PR adds substantial detection logic but relies only on integration tests.

Missing Tests:

  • Unit tests for PythonDataModelDetectorStrategy
  • Unit tests for TypeScriptDataModelDetectorStrategy
  • Edge case testing (malformed decorators, nested classes, etc.)
  • Performance testing with large files

Existing Test Quality:

  • TypeScript query validation tests are comprehensive (test_typescript_query_validation.py)
  • Good coverage of Tree-sitter query compilation and basic functionality

⚡ Performance Implications

Positive:

  • LRU Caching: Parser/query loading cached appropriately
  • Efficient Processing: Only parses AST once per file
  • Memory Efficient: Returns position tuples instead of full AST nodes

Potential Concerns:

  • Regex Performance: Case-insensitive regex on every decorator could be slow for files with many decorators
  • Double Parsing: TypeScript strategy may parse same content twice (structural signature + Tree-sitter)

🔒 Security Considerations

Low Risk Profile:

  • No user input directly processed
  • Tree-sitter provides memory-safe parsing
  • Error handling prevents crashes

Minor Considerations:

  • UTF-8 decoding with errors="ignore" may mask encoding issues
  • File path construction uses relative paths - generally safe in this context

📊 Data Model Integration

Good Integration:

  • UnoplatFile model properly updated with DataModelPosition field
  • Backwards compatible with existing has_data_model boolean flag
  • Integration with python_processor.py is clean

🎯 Recommendations

High Priority

  1. Add Unit Tests: Create dedicated test files for both detector strategies

    # tests/detector/test_python_data_model_detector_strategy.py
    # tests/detector/test_typescript_data_model_detector_strategy.py
  2. Fix Python Query Precision: Make dataclass regex more specific:

    - (#match? @decorator_target "(?i)dataclass")
    + (#match? @decorator_target "^(dataclasses?\\.)?dataclass$")
  3. Standardize TypeScript Query Compilation: Use consistent API throughout

Medium Priority

  1. Expand Python Detection: Consider supporting Pydantic models and attrs classes
  2. Add Error Logging: Log specific errors in TypeScript strategy exception blocks
  3. Performance Testing: Add benchmarks for large files with many decorators

Low Priority

  1. Consider Type Validation: Add runtime validation for extracted positions
  2. Documentation: Add docstring examples showing expected input/output

✅ Overall Assessment

This is a well-designed improvement that successfully replaces file-level detection with precise position extraction. The architectural changes are sound, and the implementation is generally robust. The main concerns are around test coverage and some edge cases in detection logic.

Recommendation: ✅ Approve with requested changes - The core functionality is solid, but adding unit tests and fixing the regex precision issues would significantly improve code quality.


@qodo-code-review

qodo-code-review Bot commented Oct 18, 2025

Copy link
Copy Markdown

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
Dependency source risk

Description: The dependency source for unoplat-code-confluence-commons is switched to a local path,
which can break reproducible builds or be abused in CI if path resolution is uncontrolled;
ensure this is not used in production pipelines.
pyproject.toml [64-66]

Referred Code
unoplat-code-confluence-commons = { path = "../../unoplat-code-confluence-commons" }
#unoplat-code-confluence-commons = { git = "https://github.com/unoplat/unoplat-code-confluence.git", subdirectory = "unoplat-code-confluence-commons", rev = "unoplat-code-confluence-commons-v0.31.2" }
Ticket Compliance
🎫 No ticket provided
- [ ] Create ticket/issue <!-- /create_ticket --create_ticket=true -->

</details></td></tr>
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
No custom compliance provided

Follow the guide to enable custom compliance check.

  • Update
Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

@claude

claude Bot commented Oct 18, 2025

Copy link
Copy Markdown

Claude finished @JayGhiya's task —— View job


Code Review Complete ✅

I've thoroughly reviewed this PR focusing on code quality, best practices, potential bugs, performance considerations, security concerns, and test coverage.

🏗️ Architecture & Design Quality - Excellent

Strengths

  • Clean Factory Pattern: Well-implemented factory pattern with proper separation of concerns
  • Strategy Pattern: Language-specific strategies enable easy extension for new languages
  • Single Responsibility: Each component has a clear, focused purpose
  • Type Safety: Excellent use of type hints throughout

📋 Code Quality Assessment

Factory Implementation (data_model_detector_factory.py:24)

  • Clean dictionary-based strategy mapping
  • Proper exception handling with custom UnsupportedLanguageForDataModelDetectionError
  • Good defensive programming with language validation

Strategy Abstraction (data_model_detector_strategy.py:18)

  • Well-defined abstract base class with clear return type specification
  • Consistent tuple return format: (bool, DataModelPosition)

Language-Specific Implementations

Python Strategy (python_data_model_detector_strategy.py:57-99):

  • Robust error handling in Tree-sitter parsing
  • Efficient LRU cache for parser/query loading
  • Proper byte encoding handling with error recovery
  • Good extraction of line numbers with 1-based indexing

TypeScript Strategy (typescript_data_model_detector_strategy.py:53-62):

  • Dual detection path: structural signature + Tree-sitter fallback
  • Excellent regex-based name extraction with proper keyword handling
  • Good field name extraction from AST nodes

🎯 Tree-sitter Query Quality

Python Query (dataclasses.scm:5-13)

  • Excellent: Precise targeting of @dataclass decorators
  • Case-insensitive matching with (?i)dataclass
  • Proper capture of both class definition and name

TypeScript Query (types.scm:3-6)

  • Well-simplified: Clear captures for type_alias_declaration and interface_declaration
  • Much cleaner than previous 45-line version
  • Enables precise range extraction

🧪 Test Coverage - Comprehensive

The test suite in test_typescript_query_validation.py is exceptionally thorough:

  • 554 lines of comprehensive test coverage
  • Validates query compilation, range extraction, and name parsing
  • Tests against real TypeScript AST samples
  • Includes edge cases and component-level validation

Notable Test Quality:

  • test_types_query_interface_ranges:455-501 - Validates exact line ranges
  • test_types_query_type_alias_ranges:503-549 - Tests type alias extraction
  • Proper fixture management with module-scope caching

Performance Considerations

Performance Optimizations

  • LRU Cache: Parser/query loading cached (@lru_cache(maxsize=1))
  • Early Returns: Empty source code handled efficiently
  • Lazy Loading: Tree-sitter resources loaded only when needed
  • Memory Efficient: Byte-level source processing

📊 Performance Impact

  • Positive: Precise line ranges reduce context size significantly
  • Efficient: One-time Tree-sitter parsing per file
  • Scalable: Factory pattern allows optimization per language

🔒 Security Assessment

Security Strengths

  • Input Sanitization: Proper UTF-8 encoding with error handling
  • Exception Safety: All Tree-sitter operations wrapped in try-catch
  • No Code Execution: Pure AST parsing, no dynamic code execution
  • Memory Safe: Bounded operations with proper error recovery

⚠️ Minor Considerations

  • File path construction uses relative paths - generally safe in controlled environments
  • Query file loading from disk - should validate query files are trusted

🐛 Potential Issues Found

1. Query Loading Bug (typescript_data_model_detector_strategy.py:179)

query = language.query(query_source)  # Should be tree_sitter.Query(language, query_source)

Impact: May cause inconsistent query compilation
Fix: Use consistent tree_sitter.Query() constructor

2. Missing Error Handling (python_data_model_detector_strategy.py:68)

The generic except Exception: is too broad. Consider:

except (tree_sitter.InvalidLanguageError, FileNotFoundError, UnicodeDecodeError):

3. Data Model Persistence (generic_codebase_parser.py:564)

Good: Proper JSON serialization of data_model_positions
Good: Cypher query updates both boolean flag and positions

🏆 Best Practices Adherence

Excellent Practices

  • Defensive Programming: Comprehensive null checks and validation
  • Clean Code: Clear variable names and method organization
  • Documentation: Good docstrings with type information
  • Error Recovery: Graceful degradation when parsing fails

📝 Minor Suggestions

  1. Add logging for debugging Tree-sitter failures
  2. Consider metrics for data model detection rates
  3. Validate query files during application startup

📊 Overall Assessment

Category Rating Notes
Architecture ⭐⭐⭐⭐⭐ Excellent factory/strategy pattern
Code Quality ⭐⭐⭐⭐⭐ Clean, well-structured, type-safe
Test Coverage ⭐⭐⭐⭐⭐ Comprehensive, real-world validation
Performance ⭐⭐⭐⭐⭐ Well-optimized with caching
Security ⭐⭐⭐⭐ Good practices, minor considerations
Maintainability ⭐⭐⭐⭐⭐ Easy to extend for new languages

🎯 Recommendations

High Priority

  1. Fix TypeScript query constructor inconsistency
  2. Add specific exception handling in Python strategy

Medium Priority

  1. Add debug logging for Tree-sitter operations
  2. Consider startup validation of query files
  3. Add performance metrics for detection operations

Low Priority

  1. Consider extracting regex patterns to constants
  2. Add integration tests with real repositories

Summary

This is an excellent PR that significantly improves the codebase:

  • Major Enhancement: Moves from boolean flags to precise line ranges
  • Clean Architecture: Well-implemented factory and strategy patterns
  • Performance Improvement: Reduces context size and improves precision
  • Excellent Test Coverage: Comprehensive validation with real examples
  • Future-Proof: Easy to extend for additional languages

The implementation demonstrates strong software engineering principles and should provide significant value for precise code analysis.

Todo List:

  • Read and analyze core factory pattern implementation
  • Review language-specific detection strategies
  • Examine Tree-sitter query files
  • Check data model changes and persistence
  • Review test coverage and quality
  • Analyze performance implications
  • Check for security concerns
  • Provide comprehensive feedback

@qodo-code-review

qodo-code-review Bot commented Oct 18, 2025

Copy link
Copy Markdown

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
High-level
Simplify data model detection logic

Simplify the data model detection logic by returning only the position data
instead of both a boolean flag and position data. The boolean can be derived
from the position data, reducing redundancy.

Examples:

unoplat-code-confluence-ingestion/code-confluence-flow-bridge/src/code_confluence_flow_bridge/detector/data_model_detector.py [27]
) -> Tuple[bool, DataModelPosition]:
unoplat-code-confluence-ingestion/code-confluence-flow-bridge/src/code_confluence_flow_bridge/parser/language_processors/python_processor.py [173-205]
            data_model_detected, data_model_positions = detect_data_model(
                source_code=content,
                imports=imports,
                language=metadata.language.value,
                structural_signature=signature,
            )

            custom_features_list = None
            framework_service = self.context.framework_detection_service
            if framework_service:

 ... (clipped 23 lines)

Solution Walkthrough:

Before:

# In data_model_detector.py
def detect_data_model(...) -> Tuple[bool, DataModelPosition]:
    ...
    strategy = DataModelDetectorFactory.get_strategy(...)
    return strategy.detect(...)

# In python_data_model_detector_strategy.py
def detect(...) -> Tuple[bool, DataModelPosition]:
    positions = self._detect_dataclasses_with_tree_sitter(...)
    has_data_model = bool(positions)
    return has_data_model, DataModelPosition(positions=positions)

# In python_processor.py
data_model_detected, data_model_positions = detect_data_model(...)
return UnoplatFile(
    ...,
    has_data_model=data_model_detected,
    data_model_positions=data_model_positions,
)

After:

# In data_model_detector.py
def detect_data_model(...) -> DataModelPosition:
    ...
    strategy = DataModelDetectorFactory.get_strategy(...)
    return strategy.detect(...)

# In python_data_model_detector_strategy.py
def detect(...) -> DataModelPosition:
    positions = self._detect_dataclasses_with_tree_sitter(...)
    return DataModelPosition(positions=positions)

# In python_processor.py
data_model_positions = detect_data_model(...)
data_model_detected = bool(data_model_positions.positions) # Assuming .positions
return UnoplatFile(
    ...,
    has_data_model=data_model_detected,
    data_model_positions=data_model_positions,
)
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly identifies redundancy where both a boolean has_data_model and data_model_positions are returned and stored, while the boolean can be derived from the position data, impacting multiple files and improving the design.

Medium
General
Use modern tree-sitter capture API

Refactor the tree-sitter query execution to use the recommended query.captures()
method instead of the deprecated QueryCursor.matches() for improved stability
and API consistency.

unoplat-code-confluence-ingestion/code-confluence-flow-bridge/src/code_confluence_flow_bridge/detector/python/python_data_model_detector_strategy.py [75-99]

-cursor = tree_sitter.QueryCursor(query)
-matches = cursor.matches(root_node)
+positions: Dict[str, Tuple[int, int]] = {}
+captures = query.captures(root_node)
 
-positions: Dict[str, Tuple[int, int]] = {}
+# Group captures by the node they belong to, to associate names with definitions
+node_to_captures: Dict[tree_sitter.Node, Dict[str, tree_sitter.Node]] = {}
+for node, capture_name in captures:
+    if capture_name in ("dataclass_definition", "dataclass_name"):
+        # The parent of the name identifier is the class_definition node
+        key_node = node if capture_name == "dataclass_definition" else node.parent
+        if key_node not in node_to_captures:
+            node_to_captures[key_node] = {}
+        node_to_captures[key_node][capture_name] = node
 
-for _, captures in matches:
-    class_nodes = captures.get("dataclass_definition")
-    name_nodes = captures.get("dataclass_name")
-
-    if not class_nodes or not name_nodes:
+for class_node, parts in node_to_captures.items():
+    name_node = parts.get("dataclass_name")
+    if not name_node:
         continue
-
-    class_node = class_nodes[0]
-    name_node = name_nodes[0]
 
     name = source_bytes[name_node.start_byte:name_node.end_byte].decode(
         "utf-8", errors="ignore"
     )
-
     start_line = class_node.start_point[0] + 1
     end_line = class_node.end_point[0] + 1
-
     positions[name] = (start_line, end_line)

[To ensure code accuracy, apply this suggestion manually]

Suggestion importance[1-10]: 6

__

Why: The suggestion correctly identifies the use of a deprecated tree-sitter API and proposes using the modern query.captures() method, which improves code consistency with other parts of the PR and enhances maintainability.

Low
Simplify name extraction logic

Simplify the name extraction logic by deriving the fallback_keyword directly
from the capture_name string, removing the need for a conditional block.

unoplat-code-confluence-ingestion/code-confluence-flow-bridge/src/code_confluence_flow_bridge/detector/typescript/typescript_data_model_detector_strategy.py [123-141]

 for node, capture_name in query.captures(root_node):
-    if capture_name == "type_alias":
-        name = self._extract_name_from_node(
-            node, source_bytes, fallback_keyword="type"
-        )
-    elif capture_name == "interface":
-        name = self._extract_name_from_node(
-            node, source_bytes, fallback_keyword="interface"
-        )
-    else:
-        name = None
+    if capture_name not in ("type_alias", "interface"):
+        continue
+
+    # "type_alias" -> "type", "interface" -> "interface"
+    keyword = capture_name.split("_")[0]
+    name = self._extract_name_from_node(
+        node, source_bytes, fallback_keyword=keyword
+    )
 
     if name:
         positions[name] = (
             node.start_point[0] + 1,
             node.end_point[0] + 1,
         )

[To ensure code accuracy, apply this suggestion manually]

Suggestion importance[1-10]: 5

__

Why: This is a valid refactoring that simplifies the code by removing a conditional block and deriving the keyword from the capture name, making the logic more concise and less repetitive.

Low
  • Update

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +71 to +83
source_bytes = source_code.encode("utf-8", errors="ignore")
tree = parser.parse(source_bytes)
root_node = tree.root_node

cursor = tree_sitter.QueryCursor(query)
matches = cursor.matches(root_node)

positions: Dict[str, Tuple[int, int]] = {}

for _, captures in matches:
class_nodes = captures.get("dataclass_definition")
name_nodes = captures.get("dataclass_name")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0 Badge Execute Tree‑sitter query with source bytes

The new Python detector constructs QueryCursor with the query and calls cursor.matches(root_node) without the source text. QueryCursor takes no arguments and predicates like #match? in dataclasses.scm require bytes to be supplied when executing the query, so _detect_dataclasses_with_tree_sitter will raise a TypeError/ValueError the first time it runs and the entire file processing short-circuits in extract_file_data. Use query.captures(root_node, source_bytes) or QueryCursor().exec(query, root_node, source_bytes) to avoid crashing and to evaluate the predicate against the file contents.

Useful? React with 👍 / 👎.

Comment on lines +30 to +56
def detect(
self,
source_code: str,
imports: Optional[List[str]] = None,
structural_signature: Optional[object] = None,
) -> Tuple[bool, DataModelPosition]:
"""
Detect Python dataclasses.

Returns detections based on a dedicated Tree-sitter query.

Args:
source_code: The Python source code to analyze
imports: Optional list of import statements (unused)
structural_signature: Optional structural signature (unused)

Returns:
Tuple containing:
- bool: True if dataclasses are detected
- DataModelPosition: Positions keyed by dataclass name
"""
_ = imports, structural_signature # unused in current detection path
positions = self._detect_dataclasses_with_tree_sitter(source_code)

has_data_model = bool(positions)
return has_data_model, DataModelPosition(positions=positions)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve Pydantic data model detection

The prior detect_data_model implementation flagged Python files as data models when dataclass or pydantic was imported. The new strategy narrows detection to only classes decorated with @dataclass via Tree‑sitter and ignores imports entirely. Any Pydantic or SQLModel definitions—which typically have no @dataclass decorator—will now yield has_data_model=False, so those files will never be persisted with data model metadata. If broader Python models are still required, keep the import-based fallback or add explicit detection for Pydantic signatures before replacing the old logic.

Useful? React with 👍 / 👎.

@claude

claude Bot commented Oct 18, 2025

Copy link
Copy Markdown

Claude finished @JayGhiya's task —— View job


Claude finished @JayGhiya's task —— View job


PR Review: Data Model Position Extraction

Reviewing the pull request for precise data model position extraction in TypeScript and Python files.

Review Tasks

  • Analyze architectural changes (factory/strategy patterns)
  • Review Python data model detector implementation
  • Review TypeScript data model detector implementation
  • Examine Tree-sitter query files
  • Check test coverage and quality
  • Evaluate performance implications
  • Assess security considerations
  • Investigate failing test issue
  • Provide comprehensive feedback and recommendations

🏗️ Architectural Analysis

Positive Changes:

  • Clean Strategy Pattern: The factory/strategy pattern implementation is well-designed and follows good separation of concerns
  • Language Agnostic Interface: DataModelDetectorStrategy provides a clean abstraction for adding new language support
  • Graceful Error Handling: Factory properly handles unsupported languages by returning empty results rather than crashing

Factory Implementation:

  • Line coverage good in data_model_detector_factory.py:43-48
  • Custom exception UnsupportedLanguageForDataModelDetectionError is appropriate
  • Strategy mapping is clean and extensible

🐍 Python Implementation Review

Strengths:

  • Focused Detection: Tree-sitter query correctly targets @dataclass decorated classes only
  • Robust Error Handling: Empty source code and parsing exceptions handled gracefully (python_data_model_detector_strategy.py:61-69)
  • Efficient Caching: @lru_cache(maxsize=1) for parser/query loading is smart optimization
  • Precise Position Extraction: Correctly extracts line ranges with start_point[0] + 1 and end_point[0] + 1

Issues Found:

  1. Critical Bug - Tree-sitter API: Line 75-76 in python_data_model_detector_strategy.py has incorrect usage:

    cursor = tree_sitter.QueryCursor(query)  # Wrong - QueryCursor() takes no args
    matches = cursor.matches(root_node)       # Missing source bytes for predicate evaluation

    Impact: This will crash when #match? predicate tries to evaluate against decorator text.
    Fix: Use query.captures(root_node) or QueryCursor().exec(query, root_node, source_bytes)

  2. Query Pattern Risk: The regex (?i)dataclass at dataclasses.scm:12 may match false positives like @my_dataclass_validator

  3. Limited Detection Scope: Only detects @dataclass - misses other common Python data models:

    • Pydantic models (BaseModel subclasses)
    • attrs classes (@attr.s)
    • NamedTuple subclasses

🔷 TypeScript Implementation Review

Strengths:

  • Dual Detection Paths: Supports both structural signature and direct Tree-sitter parsing
  • Comprehensive Coverage: Handles interfaces, type aliases, generic types, and exported constructs
  • Robust Name Extraction: _extract_declared_name() properly handles export/declare keywords and generics
  • Field-Based Parsing: Uses AST field names (child_by_field_name("name")) when available

Issues Found:

  1. Inconsistent Query Compilation: Line 179 uses language.query() vs line 110 uses tree_sitter.Query() - should be consistent
  2. Missing Error Context: Exception handling at lines 114, 82 swallows specific errors without logging
  3. Limited Type Coverage: Focuses only on interfaces/type aliases - could miss:
    • Zod schemas (z.object({...}))
    • Class-validator decorated classes
    • Enum declarations (data-like enums)

🔍 Tree-sitter Query Analysis

Python Query (dataclasses.scm):

  • Correct Structure: Properly targets decorated class definitions
  • Risk: Overly broad regex pattern (?i)dataclass may cause false positives

TypeScript Query (types.scm):

  • Simplified Correctly: Reduced from 50 lines to 7 lines - much cleaner
  • Good Coverage: Captures both interfaces and type aliases at top level

🧪 Test Coverage Assessment

Strong Coverage:

  • 554 lines of comprehensive TypeScript test coverage in test_typescript_query_validation.py
  • Validates exact line ranges, name parsing, and AST structure
  • New Python detector tests (test_python_data_model_detector.py) verify dataclass detection

Areas for Improvement:

  • Unit tests for edge cases (malformed decorators, nested classes)
  • Integration tests with real repository data
  • Performance testing with large files

⚡ Performance Implications

Positive:

  • LRU Caching: Parser/query loading cached appropriately
  • Efficient Processing: Only parses AST once per file
  • Memory Efficient: Returns position tuples instead of full AST nodes

Potential Concerns:

  • Regex Performance: Case-insensitive regex on every decorator could be slow for files with many decorators
  • Double Parsing: TypeScript strategy may parse same content twice (structural signature + Tree-sitter)

🔒 Security Considerations

Low Risk Profile:

  • No user input directly processed
  • Tree-sitter provides memory-safe parsing
  • Error handling prevents crashes

Minor Considerations:

  • UTF-8 decoding with errors="ignore" may mask encoding issues
  • File path construction uses relative paths - generally safe in this context

📊 Data Model Integration

Good Integration:

  • UnoplatFile model properly updated with DataModelPosition field
  • Backwards compatible with existing has_data_model boolean flag
  • Integration with python_processor.py is clean

🚨 Failing Test Analysis

Root Cause: Line 396 in test_generic_codebase_parser.py expects 2 dataclass files but finds only 1

  • Issue: Error message says "Expected 1 dataclass file, got 1" but assertion checks dataclass_count == 2
  • Likely Cause: New dataclass detection is more precise/strict than previous implementation
  • Impact: Integration test needs to be updated to match new detection behavior

🎯 Recommendations

Critical Priority

  1. Fix Python Tree-sitter Bug: Replace incorrect QueryCursor(query) usage with query.captures(root_node)
  2. Update Failing Test: Adjust expected dataclass count in test_generic_codebase_parser.py

High Priority

  1. Standardize TypeScript Query Compilation: Use consistent API throughout
  2. Make Python Query More Specific: Change regex from (?i)dataclass to ^(dataclasses?\\.)?dataclass$

Medium Priority

  1. Add Error Logging: Log specific errors in TypeScript strategy exception blocks
  2. Expand Python Detection: Consider supporting Pydantic models and attrs classes
  3. Performance Testing: Add benchmarks for large files with many decorators

Low Priority

  1. Consider Type Validation: Add runtime validation for extracted positions
  2. Documentation: Add docstring examples showing expected input/output

✅ Overall Assessment

This is a well-designed improvement that successfully replaces file-level detection with precise position extraction. The architectural changes are sound, and the implementation is generally robust. The main concerns are around a critical bug in the Python detector and test expectations.

Recommendation: ✅ Approve with critical fixes - The core functionality is excellent, but the Tree-sitter bug must be fixed before merging.


…data model positions for TypeScript and Python files for precise

previously we used to read entire files nd only had signal in terms of 
codebase which files have data models. now we have a precise locations 
in terms of start and end within a file saving lot of context and 
serving better performance and precision
Update dependency to latest version v0.32.0 for
development and production environments. Switch from
local path to git repository reference.
…are complexity

we had added linting in a way that will improve the user codebase in a 
way that would make it simpler for us to write s expression quries to 
extract signals we want for agents.md. the downside was for our agents 
we had to give them search pattern instead of start line end line which 
becomes harder for them in terms of how to search finite element as they
will not know the end location. we are reversing this and will take upon
grammar changes required but will make it easy/reliable/faster for our 
Agents
Update dependency to use GitHub source instead of local directory
Upgrade unoplat-code-confluence-commons from v0.31.2 to v0.32.0

Adjust test assertions for data model detection to reflect current
implementation, adding a note about BaseModel detection limitations
@JayGhiya
JayGhiya force-pushed the ingestion-context-engineering branch from f2577ea to 82f2810 Compare October 18, 2025 11:48
@claude

claude Bot commented Oct 18, 2025

Copy link
Copy Markdown

Claude finished @JayGhiya's task —— View job


PR Review: Data Model Position Extraction

Reviewing the pull request for precise data model position extraction in TypeScript and Python files.

Review Tasks

  • Analyze architectural changes (factory/strategy patterns)
  • Review Python data model detector implementation
  • Review TypeScript data model detector implementation
  • Examine Tree-sitter query files
  • Check test coverage and quality
  • Evaluate performance implications
  • Assess security considerations
  • Investigate failing test issue
  • Provide comprehensive feedback and recommendations

🏗️ Architectural Analysis

Positive Changes:

  • Clean Strategy Pattern: The factory/strategy pattern implementation is well-designed and follows good separation of concerns
  • Language Agnostic Interface: DataModelDetectorStrategy provides a clean abstraction for adding new language support
  • Graceful Error Handling: Factory properly handles unsupported languages by returning empty results rather than crashing

Factory Implementation:

  • Line coverage good in data_model_detector_factory.py:43-48
  • Custom exception UnsupportedLanguageForDataModelDetectionError is appropriate
  • Strategy mapping is clean and extensible

🐍 Python Implementation Review

Strengths:

  • Focused Detection: Tree-sitter query correctly targets @dataclass decorated classes only
  • Robust Error Handling: Empty source code and parsing exceptions handled gracefully (python_data_model_detector_strategy.py:61-69)
  • Efficient Caching: @lru_cache(maxsize=1) for parser/query loading is smart optimization
  • Precise Position Extraction: Correctly extracts line ranges with start_point[0] + 1 and end_point[0] + 1

Critical Issues Found:

  1. Tree-sitter API Bug: Line 75-76 in python_data_model_detector_strategy.py has incorrect usage:

    cursor = tree_sitter.QueryCursor(query)  # Wrong - QueryCursor() takes no args
    matches = cursor.matches(root_node)       # Missing source bytes for predicate evaluation

    Impact: This will crash when #match? predicate tries to evaluate against decorator text.
    Fix: Use query.captures(root_node) or QueryCursor().exec(query, root_node, source_bytes)

  2. Query Pattern Risk: The regex (?i)dataclass at dataclasses.scm:12 may match false positives like @my_dataclass_validator

  3. Limited Detection Scope: Only detects @dataclass - misses other common Python data models:

    • Pydantic models (BaseModel subclasses)
    • attrs classes (@attr.s)
    • NamedTuple subclasses

🔷 TypeScript Implementation Review

Strengths:

  • Dual Detection Paths: Supports both structural signature and direct Tree-sitter parsing
  • Comprehensive Coverage: Handles interfaces, type aliases, generic types, and exported constructs
  • Robust Name Extraction: _extract_declared_name() properly handles export/declare keywords and generics
  • Field-Based Parsing: Uses AST field names (child_by_field_name("name")) when available

Issues Found:

  1. Inconsistent Query Compilation: Line 179 uses language.query() vs line 110 uses tree_sitter.Query() - should be consistent
  2. Missing Error Context: Exception handling at lines 114, 82 swallows specific errors without logging
  3. Limited Type Coverage: Focuses only on interfaces/type aliases - could miss:
    • Zod schemas (z.object({...}))
    • Class-validator decorated classes
    • Enum declarations (data-like enums)

🔍 Tree-sitter Query Analysis

Python Query (dataclasses.scm):

  • Correct Structure: Properly targets decorated class definitions
  • Risk: Overly broad regex pattern (?i)dataclass may cause false positives

TypeScript Query (types.scm):

  • Simplified Correctly: Reduced from 50 lines to 7 lines - much cleaner
  • Good Coverage: Captures both interfaces and type aliases at top level

🧪 Test Coverage Assessment

Strong Coverage:

  • 554 lines of comprehensive TypeScript test coverage in test_typescript_query_validation.py
  • Validates exact line ranges, name parsing, and AST structure
  • New Python detector tests (test_python_data_model_detector.py) verify dataclass detection

Areas for Improvement:

  • Unit tests for edge cases (malformed decorators, nested classes)
  • Integration tests with real repository data
  • Performance testing with large files

⚡ Performance Implications

Positive:

  • LRU Caching: Parser/query loading cached appropriately
  • Efficient Processing: Only parses AST once per file
  • Memory Efficient: Returns position tuples instead of full AST nodes

Potential Concerns:

  • Regex Performance: Case-insensitive regex on every decorator could be slow for files with many decorators
  • Double Parsing: TypeScript strategy may parse same content twice (structural signature + Tree-sitter)

🔒 Security Considerations

Low Risk Profile:

  • No user input directly processed
  • Tree-sitter provides memory-safe parsing
  • Error handling prevents crashes

Minor Considerations:

  • UTF-8 decoding with errors="ignore" may mask encoding issues
  • File path construction uses relative paths - generally safe in this context

📊 Data Model Integration

Good Integration:

  • UnoplatFile model properly updated with DataModelPosition field
  • Backwards compatible with existing has_data_model boolean flag
  • Integration with python_processor.py is clean

🚨 Failing Test Analysis

Root Cause: Line 398 in test_generic_codebase_parser.py expects 1 dataclass file but CI shows the test was expecting 2 (error message inconsistency)

  • Issue: New dataclass detection is more precise/strict than previous implementation
  • Impact: Integration test needs to be updated to match new detection behavior
  • Fix: Update test expectations to align with the more accurate detection logic

🎯 Recommendations

Critical Priority

  1. Fix Python Tree-sitter Bug: Replace incorrect QueryCursor(query) usage with query.captures(root_node)
  2. Update Failing Test: Adjust expected dataclass count in test_generic_codebase_parser.py

High Priority

  1. Standardize TypeScript Query Compilation: Use consistent API throughout
  2. Make Python Query More Specific: Change regex from (?i)dataclass to ^(dataclasses?\\.)?dataclass$

Medium Priority

  1. Add Error Logging: Log specific errors in TypeScript strategy exception blocks
  2. Expand Python Detection: Consider supporting Pydantic models and attrs classes
  3. Performance Testing: Add benchmarks for large files with many decorators

Low Priority

  1. Consider Type Validation: Add runtime validation for extracted positions
  2. Documentation: Add docstring examples showing expected input/output

✅ Overall Assessment

This is a well-designed improvement that successfully replaces file-level detection with precise position extraction. The architectural changes are sound, and the implementation is generally robust. The main concerns are around a critical bug in the Python detector and test expectations.

Recommendation: ✅ Approve with critical fixes - The core functionality is excellent, but the Tree-sitter bug must be fixed before merging.


@JayGhiya
JayGhiya merged commit 2297666 into dev Oct 18, 2025
5 checks passed
JayGhiya added a commit that referenced this pull request Apr 8, 2026
feat(python-typescript-grammar-context-engineering): Extract precise data model positions for TypeScript and Python files for precise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant