⚡ Bolt: Parallelize and optimize unit test gate - #476
Conversation
- Parallelized sample processing in scripts/03_unit_test_gate.py using ThreadPoolExecutor. - Pre-compiled regex patterns for code extraction, dangerous patterns, and Python keywords. - Implemented textwrap.indent for safer code wrapping in test execution. - Fixed an environment-specific os.makedirs bug. - Fixed a NameError in heidi_engine/telemetry.py state cache. Measured ~2.7x speedup on 100 samples (2.4s -> 0.9s) on 4-core system.
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Code Review
This pull request optimizes and parallelizes the unit test gate script by introducing ThreadPoolExecutor for parallel sample testing, pre-compiling regex patterns at the module level, and using textwrap.indent for safe code wrapping. It also cleans up a redundant cache check in telemetry.py and fixes a directory creation bug in save_jsonl. Feedback highlights a critical issue where worker thread exceptions can cause samples to be silently dropped from the final output, and suggests enhancing the code block regex to support Windows-style line endings.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| for i, future in enumerate(as_completed(futures)): | ||
| try: | ||
| idx, tested_sample = future.result() | ||
| results_map[idx] = tested_sample | ||
|
|
||
| # Count results | ||
| test_result = tested_sample.get("test_result", {}) | ||
| if test_result.get("passed", False): | ||
| passed_count += 1 | ||
| else: | ||
| failed_count += 1 | ||
|
|
||
| except Exception as e: | ||
| print(f"[ERROR] Worker failed: {e}", file=sys.stderr) |
There was a problem hiding this comment.
If a worker thread raises an unhandled exception, calling future.result() will raise that exception, skipping the assignment to results_map. This causes the affected sample to be silently dropped from the final output list, leading to silent data loss.
We should retrieve the index and original sample from the futures dictionary mapping to ensure the sample is preserved in the output and marked as failed.
for i, future in enumerate(as_completed(futures)):
idx, sample, _, _ = futures[future]
try:
_, tested_sample = future.result()
results_map[idx] = tested_sample
# Count results
test_result = tested_sample.get("test_result", {})
if test_result.get("passed", False):
passed_count += 1
else:
failed_count += 1
except Exception as e:
print(f"[ERROR] Worker failed for sample {idx}: {e}", file=sys.stderr)
sample["test_result"] = {
"passed": False,
"reason": f"worker_failed: {e}",
"blocks_tested": 0,
}
results_map[idx] = sample
failed_count += 1| CODE_BLOCK_RE = re.compile( | ||
| r"```python\n(.*?)```|```\n(.*?)```|`([^`\n]+)`", | ||
| re.DOTALL | ||
| ) |
There was a problem hiding this comment.
To ensure robustness against Windows-style line endings (\r\n), the regex pattern should support optional carriage returns (\r?\n) before the code block content.
| CODE_BLOCK_RE = re.compile( | |
| r"```python\n(.*?)```|```\n(.*?)```|`([^`\n]+)`", | |
| re.DOTALL | |
| ) | |
| CODE_BLOCK_RE = re.compile( | |
| r"```python\r?\n(.*?)```|```\r?\n(.*?)```|`([^`\n]+)`", | |
| re.DOTALL | |
| ) |
💡 What: Optimized
scripts/03_unit_test_gate.pyby implementing parallel execution viaThreadPoolExecutor, pre-compiling regex patterns, and optimizing the Python code heuristic. Also fixed aNameErrorinheidi_engine/telemetry.pyand aFileNotFoundErrorinos.makedirs.🎯 Why: The unit test gate was executing samples sequentially, which is a major bottleneck especially when spawning subprocesses for code execution. Regex patterns were also being re-compiled unnecessarily.
📊 Impact: Reduces execution time for 100 samples from ~2.41s to ~0.89s (a ~2.7x speedup) on a 4-core environment.
🔬 Measurement: Run
time python3 scripts/03_unit_test_gate.py --input raw.jsonl --output tested.jsonland compare against baseline. Verify correctness by checkingtested.jsonloutput and running project tests.PR created automatically by Jules for task 8342963269438898646 started by @heidi-dang