1212from pathlib import Path
1313
1414from src .agents .state import HedAnnotationState
15+ from src .lsp import HedLspClient
1516from src .utils .error_remediation import get_remediator
1617from src .utils .schema_loader import HedSchemaLoader
17- from src .validation .hed_lsp import is_hed_lsp_available , suggest_tags_for_keywords
1818from src .validation .hed_validator import (
1919 HedJavaScriptValidator ,
2020 HedPythonValidator ,
@@ -81,7 +81,7 @@ def __init__(
8181 use_javascript : bool = True ,
8282 validator_path : Path | None = None ,
8383 tests_json_path : Path | str | None = None ,
84- use_hed_lsp : bool = True ,
84+ lsp_client : HedLspClient | None = None ,
8585 ) -> None :
8686 """Initialize the validation agent.
8787
@@ -90,13 +90,14 @@ def __init__(
9090 use_javascript: Whether to use JavaScript validator (more detailed)
9191 validator_path: Path to hed-javascript repository (required if use_javascript=True)
9292 tests_json_path: Optional path to javascriptTests.json for error remediation
93- use_hed_lsp: Whether to use hed-lsp for tag suggestions (auto-detected)
93+ lsp_client: Pre-built HedLspClient for tag-replacement suggestions
94+ when validation finds invalid tags. None disables suggestions.
9495 """
9596 self .schema_loader = schema_loader
9697 self .use_javascript = use_javascript
9798 self .validator_path = validator_path
9899 self .error_remediator = get_remediator (tests_json_path )
99- self .use_hed_lsp = use_hed_lsp and is_hed_lsp_available ()
100+ self .lsp_client = lsp_client
100101
101102 # Validator is lazily initialized on first use via _get_or_create_validator
102103 self ._validator : HedJavaScriptValidator | HedPythonValidator | None = None
@@ -134,34 +135,37 @@ def _extract_problematic_tags(self, errors: list, warnings: list) -> list[str]:
134135
135136 return problematic_tags
136137
137- def _get_tag_suggestions (
138- self , problematic_tags : list [str ], schema_version : str
139- ) -> dict [str , list [str ]]:
140- """Get suggested valid tags for problematic tags using hed-lsp.
138+ async def _get_tag_suggestions (self , problematic_tags : list [str ]) -> dict [str , list [str ]]:
139+ """Get suggested valid tags for problematic tags via persistent LSP.
141140
142141 Args:
143142 problematic_tags: List of problematic tag names
144- schema_version: HED schema version
145143
146144 Returns:
147- Dictionary mapping problematic tags to suggested alternatives
145+ Dictionary mapping each problematic tag to a list of suggested
146+ valid alternatives. Empty if no LSP client is configured or the
147+ server returned no matches.
148148 """
149- if not self .use_hed_lsp or not problematic_tags :
149+ if self .lsp_client is None or not problematic_tags :
150150 return {}
151151
152152 try :
153- return suggest_tags_for_keywords (
154- problematic_tags ,
155- schema_version = schema_version ,
156- max_results = 5 , # Limit suggestions for clarity
157- )
158- except (RuntimeError , OSError ) as e :
153+ result = await self .lsp_client .suggest (* problematic_tags )
154+ except Exception as exc :
155+ # `suggest()` already converts transport errors to a failure
156+ # result; anything that lands here is a programming error and
157+ # deserves a traceback.
159158 logger .warning (
160- "Failed to get tag suggestions from hed-lsp for tags %s: %s" ,
159+ "hed-lsp suggest call failed for tags %s: %s" ,
161160 problematic_tags ,
162- e ,
161+ exc ,
162+ exc_info = True ,
163163 )
164164 return {}
165+ if not result .success :
166+ logger .debug ("hed-lsp suggest returned failure: %s" , result .error )
167+ return {}
168+ return result .raw
165169
166170 async def validate (self , state : HedAnnotationState ) -> dict :
167171 """Validate the current HED annotation.
@@ -202,10 +206,10 @@ async def validate(self, state: HedAnnotationState) -> dict:
202206
203207 # Extract problematic tags and get suggestions from hed-lsp
204208 tag_suggestions : dict [str , list [str ]] = {}
205- if not result .is_valid and self .use_hed_lsp :
209+ if not result .is_valid and self .lsp_client is not None :
206210 problematic_tags = self ._extract_problematic_tags (result .errors , result .warnings )
207211 if problematic_tags :
208- tag_suggestions = self ._get_tag_suggestions (problematic_tags , schema_version )
212+ tag_suggestions = await self ._get_tag_suggestions (problematic_tags )
209213
210214 # Determine validation status
211215 validation_attempts = state ["validation_attempts" ] + 1
0 commit comments