Merge pull request #2632 from codeflash-ai/codeflash/optimize-pr2627-2026-04-28T13.21.43

️ Speed up function `generate_and_validate_go_test_code` by 12% in PR #2627 (`cf-go-language-support`)
This commit is contained in:
mohammed ahmed 2026-04-30 22:27:27 +03:00 committed by GitHub
commit 8310f7f1c2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -15,7 +15,7 @@ from openai.types.chat import ChatCompletionMessageParam
from aiservice.analytics.posthog import ph
from aiservice.common_utils import validate_trace_id
from aiservice.env_specific import debug_log_sensitive_data
from aiservice.env_specific import IS_PRODUCTION, debug_log_sensitive_data
from aiservice.llm import llm_client
from aiservice.llm_models import EXECUTE_MODEL
from aiservice.validators.go_validator import validate_go_syntax
@ -173,16 +173,18 @@ def parse_and_validate_go_output(response_content: str) -> str:
if pattern_res is None:
raise ValueError("No Go code block found in the LLM response.")
code = pattern_res.strip()
# Defer stripping until after validation to avoid an extra temporary allocation
# in the common case where validation/parsing fails.
code_candidate = pattern_res
is_valid, error = validate_go_syntax(code)
is_valid, error = validate_go_syntax(code_candidate)
if not is_valid:
raise SyntaxError(f"Invalid Go code: {error}")
if not _has_test_functions(code):
if not _has_test_functions(code_candidate):
raise ValueError("Generated code does not contain any Test functions.")
return code
return code_candidate.strip()
def _has_test_functions(code: str) -> bool:
@ -226,7 +228,9 @@ async def generate_and_validate_go_test_code(
llm_cost = output.cost
cost_tracker.append(llm_cost)
debug_log_sensitive_data(f"LLM testgen response:\n{output.content}")
# Avoid constructing the large debug message string in production.
if not IS_PRODUCTION:
debug_log_sensitive_data("LLM testgen response:\n" + output.content)
return parse_and_validate_go_output(output.content)