Skip to content

@W-21102094 - Refactor create rule tool implementation - #384

Merged
aruntyagiTutu merged 9 commits into
arun.tyagi/feature/pmd_apex_rule_creation_toolfrom
arun.tyagi/create_rule_refactor
Feb 13, 2026
Merged

@W-21102094 - Refactor create rule tool implementation #384
aruntyagiTutu merged 9 commits into
arun.tyagi/feature/pmd_apex_rule_creation_toolfrom
arun.tyagi/create_rule_refactor

Conversation

@aruntyagiTutu

Copy link
Copy Markdown
Contributor

What does this PR do?

@W-21102094@ - refactoring implementation

What issues does this PR fix or reference?

updating prompt and refactoring implementation

@aruntyagiTutu
aruntyagiTutu requested review from a team as code owners February 12, 2026 15:22
@aruntyagiTutu
aruntyagiTutu changed the base branch from main to arun.tyagi/feature/pmd_apex_rule_creation_tool February 12, 2026 15:22
@aruntyagiTutu
aruntyagiTutu merged commit 16a02e7 into arun.tyagi/feature/pmd_apex_rule_creation_tool Feb 13, 2026
16 checks passed
iowillhoit added a commit that referenced this pull request Mar 18, 2026
…prompt generation (#381)

* create action declaration which helps in returning ast nodes of sample code

* @W-21085164 - method to extract ast nodes from ast xml (#369)

* extractAstNodes from xml implementation

* optimize the method using regex

* extract ast nodes from xml

* orchestartion setps for xpath creation for apex code (#370)

* tool declaration (#371)

* @W-21085164 - mcp tool added in provider (#372)

* mcp tool added in provider

* add GenerateXpathPromptMcpTool in test

* update test

* ast nodes generation via pmd cli method (#376)

* ast nodes generation via pmd cli method (#377)

* @W-21102083 -  creation ast local cache (#378)

* get metadata of ast nodes of apex langugae

* pmd apex ast cache

* @W-21102094 - implemented create custom rule tool (#380)

* implemented create custom rule tool

* test update

* create custom rule implementation (#383)

* @W-21102094 - Refactor create rule tool implementation  (#384)

* add validation that this implementation supports pmd only for now

* update prompt - optimize for output  size

* refactor create tool

* file name santization

* updated action name

* update prompt

* update prompt optimize for simple xpath

* fix tests

* fix test with mock

* @W-21102094 - follow clean code for create rule tool and remove chunky methods (#386)

* langugae in ruleset template

* update telemetry on success

* xpath is optional

* clean chuncky methods

* implement patterns whereever possible (#387)

* path updation in code analyzer yml to  be relative (#388)

* add comprehensive guidline in the prompt

* custom code analyzer yaml template

* @W-21102094 - create rule unit tests for ast and actions functions (#392)

* tests for ast and actions functions

* path platform agnostic

* test cases for create rule tools and services code (#393)

* file ops validations (#394)

* add clean-all scripts for missing packages

* remove unused field

* remove empty file

* @W-21364768 - PMS ast dump via code analyzer core api (#407)

* replace pmd dump cli coomand with engine api

* update code analyzer packages versions

* fix tests

* e2e update

* fix tests

* address points from PR review

* add iserror in create rule output

---------

Co-authored-by: Willhoit <iowillhoit@users.noreply.github.com>
iowillhoit added a commit that referenced this pull request Mar 25, 2026
…rce validation (#416)

* create action declaration which helps in returning ast nodes of sample code

* @W-21085164 - method to extract ast nodes from ast xml (#369)

* extractAstNodes from xml implementation

* optimize the method using regex

* extract ast nodes from xml

* orchestartion setps for xpath creation for apex code (#370)

* tool declaration (#371)

* @W-21085164 - mcp tool added in provider (#372)

* mcp tool added in provider

* add GenerateXpathPromptMcpTool in test

* update test

* ast nodes generation via pmd cli method (#376)

* ast nodes generation via pmd cli method (#377)

* @W-21102083 -  creation ast local cache (#378)

* get metadata of ast nodes of apex langugae

* pmd apex ast cache

* @W-21102094 - implemented create custom rule tool (#380)

* implemented create custom rule tool

* test update

* create custom rule implementation (#383)

* @W-21102094 - Refactor create rule tool implementation  (#384)

* add validation that this implementation supports pmd only for now

* update prompt - optimize for output  size

* refactor create tool

* file name santization

* updated action name

* update prompt

* update prompt optimize for simple xpath

* fix tests

* fix test with mock

* @W-21102094 - follow clean code for create rule tool and remove chunky methods (#386)

* langugae in ruleset template

* update telemetry on success

* xpath is optional

* clean chuncky methods

* implement patterns whereever possible (#387)

* path updation in code analyzer yml to  be relative (#388)

* add comprehensive guidline in the prompt

* custom code analyzer yaml template

* @W-21102094 - create rule unit tests for ast and actions functions (#392)

* tests for ast and actions functions

* path platform agnostic

* test cases for create rule tools and services code (#393)

* file ops validations (#394)

* add clean-all scripts for missing packages

* remove unused field

* remove empty file

* @W-21364768 - PMS ast dump via code analyzer core api (#407)

* replace pmd dump cli coomand with engine api

* update code analyzer packages versions

* fix tests

* e2e update

* fix tests

* address points from PR review

* add iserror in create rule output

* Update AST references for PMD languages

Generated from latest PMD source code with universal extraction script.

Languages updated:
- apex: 99 nodes\n- html: 7 nodes\n- visualforce: 17 nodes\n- javascript: 63 nodes\n
Total nodes: 186

- Includes all attributes, inheritance, and descriptions
- Auto-generated by universal extract-pmd-ast-reference.py script

* feat: Add multi-language AST support with Apex/Visualforce validation

Implemented generic AST metadata loader to support multiple PMD languages
and added validation to restrict automated XPath generation to Apex and
Visualforce only.

Changes:
- Created generic pmd-ast-reference.ts loader for all PMD languages
- Refactored apex-ast-reference.ts to use generic loader (backward compatible)
- Updated all type references from ApexAstNodeMetadata to AstNodeMetadata
- Added language validation in get_ast_nodes_to_generate_xpath tool
- Enhanced create_custom_rule tool description with workflow guidance
- Language-specific error messages for better user experience

Technical Details:
- Generic loader automatically supports new languages when JSON files added
- Single cache per language for optimal performance
- Graceful error handling for missing language reference files
- Maintains backward compatibility with existing Apex-specific code

Supported Languages:
- Apex: Full support with automated XPath generation
- Visualforce: Full support with automated XPath generation
- HTML: AST reference available, manual XPath generation required
- JavaScript: AST reference available, manual XPath generation required

Note: HTML has a known PMD bug preventing XML AST export (#document
is not a valid XML name), so automated XPath generation is not possible.
JavaScript and other languages require manual XPath creation due to
limited AST metadata availability.

* fix: Update test to work with generic AST metadata loader

- Removed mock for apex-ast-reference (now uses pmd-ast-reference)
- Updated test expectations to match full metadata structure
- Test now validates that Apex returns full metadata with attributes
- All 158 tests passing with 95.48% coverage

* test: Add comprehensive unit tests for multi-language AST support

- Add 20 unit tests for pmd-ast-reference.ts (generic loader)
  - Test loading all languages (Apex, Visualforce, HTML, JavaScript)
  - Test caching behavior per language
  - Test language normalization (lowercase, trim)
  - Test node finding (exact match, case-insensitive, interface matching)
  - Test order preservation and unknown node handling
  - Test concurrent requests and error handling
  - Test edge cases (empty arrays, metadata fields, attributes)

- Add 4 language validation tests for generate_xpath_prompt.ts
  - Test Visualforce language acceptance
  - Test HTML/JavaScript rejection with helpful error messages
  - Test case-insensitive language validation

- Add 1 integration test for get-ast-nodes.ts
  - Test Visualforce AST node generation

Coverage: 95%+ for new multi-language code
All 25 new tests passing

* test: Add unit tests for apex-ast-reference backward compatibility wrapper

Add comprehensive test suite for the deprecated apex-ast-reference module that delegates to the generic pmd-ast-reference loader. Tests verify delegation behavior, error propagation, and type exports. Achieves 100% coverage for apex-ast-reference.ts.

* test: Add comprehensive unit tests for PmdEngineAstXmlAdapter

Add 21 unit tests covering the PMD Engine adapter that generates AST XML:
- Successfully generates AST for various languages (apex, visualforce, js, html, xml)
- Error handling (PMD errors, missing AST, file I/O failures)
- Cleanup behavior in success and error paths
- Max source size enforcement and validation
- File extension sanitization
- Language normalization (vf→visualforce, js→javascript, ecmascript→javascript)
- AST output trimming

Achieves 100% statement and function coverage, 81% branch coverage for pmd-engine-adapter.ts.

* fix: Set isError flag on error responses in generate_xpath_prompt tool

Update buildErrorResult to set isError: true in CallToolResult so the LLM knows the response is an error. This applies to all validation errors and AST generation failures.

Also update all error test cases to verify that isError is set correctly, and success test cases to verify it is not set.

Addresses PR feedback from @iowillhoit in #416.

* refactor: Remove console.warn from PmdAstMetadataProvider

Remove console.warn when AST reference file is not found. This goes to stderr and is not visible to the LLM, only to humans debugging the MCP client. Since there's no logging system available in this context, the silent fallback (returning empty array) is sufficient.

Addresses PR feedback from @iowillhoit in #416.

* feat: Add language-specific prompt examples for multi-language AST support

Update PmdPromptBuilder to provide language-specific examples and guidance:
- Apex: Database.query, System.debug, DML patterns, hardcoded IDs
- Visualforce: apex:outputText, event handlers, escaping, component patterns
- Generic: Fallback guidance for unsupported languages

The prompt dynamically selects examples based on input.language, providing relevant patterns and best practices for each supported language.

Add comprehensive tests verifying:
- Apex prompts contain Apex-specific examples
- Visualforce prompts contain Visualforce-specific examples
- Unknown languages get generic guidance
- Language normalization works correctly

Achieves 100% statement/function coverage, 87.5% branch coverage for engine-strategies.ts.

Addresses PR feedback from @nikhil-mittal-165 in #416.

* fix: Use platform-independent paths in pmd-engine-adapter tests

Replace hardcoded Unix-style paths (forward slashes) with platform-independent paths using path.join() and path.sep. This fixes test failures on Windows where paths use backslashes.

Changes:
- Import path module
- Define platform-specific TEMP_DIR using path.sep check
- Use path.join() to construct all test paths
- Replace all hardcoded /tmp paths with TEMP_DIR constant
- Replace all hardcoded /tmp/pmd-ast-abc123 paths with PMD_TEMP_DIR
- Replace all hardcoded source file paths with SOURCE_FILE_APEX

All tests pass on Unix systems, and will now pass on Windows as well.

* refactor: Remove unused type aliases from apex-ast-reference

Remove ApexAstAttribute and ApexAstNodeMetadata type aliases as they are not used in production code and cause confusion. Use the generic AstAttribute and AstNodeMetadata types from pmd-ast-reference.ts directly.

Changes:
- Remove unused ApexAstAttribute export (never used)
- Remove ApexAstNodeMetadata type alias
- Update function return type to use AstNodeMetadata directly
- Update test to import AstNodeMetadata from pmd-ast-reference.ts

This makes the code clearer by having one canonical name per type instead of confusing aliases.

Addresses PR feedback from @namrata111f in #416.

---------

Co-authored-by: Willhoit <iowillhoit@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant