Conversation
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The CI matrix includes Python
3.14, which is not yet generally available; consider limiting the matrix to released Python versions to avoid spurious workflow failures. - In
scripts/smoke_startup_check.py, usingtempfile.TemporaryDirectoryas a context manager instead ofmkdtempplus manual cleanup would simplify lifecycle management and make it harder to leak temporary directories if the script is modified in the future.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The CI matrix includes Python `3.14`, which is not yet generally available; consider limiting the matrix to released Python versions to avoid spurious workflow failures.
- In `scripts/smoke_startup_check.py`, using `tempfile.TemporaryDirectory` as a context manager instead of `mkdtemp` plus manual cleanup would simplify lifecycle management and make it harder to leak temporary directories if the script is modified in the future.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This pull request downgrades the python-ripgrep dependency to version 0.0.8 and introduces a new cross-platform smoke test script, scripts/smoke_startup_check.py, to verify the application's startup health. Feedback was provided to improve resource management in the smoke test script, specifically recommending the use of tempfile.TemporaryDirectory for more robust cleanup and the removal of redundant file unlinking.
| smoke_root = Path(tempfile.mkdtemp(prefix="astrbot-smoke-root-")) | ||
| env["ASTRBOT_ROOT"] = str(smoke_root) | ||
| log_path = smoke_root / "smoke.log" | ||
| webui_dir = smoke_root / "webui" | ||
| webui_dir.mkdir() | ||
| (webui_dir / "index.html").write_text( | ||
| "<!doctype html><title>AstrBot</title>", | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| with log_path.open("wb") as log_file: | ||
| proc = subprocess.Popen( | ||
| [ | ||
| sys.executable, | ||
| str(REPO_ROOT / "main.py"), | ||
| "--webui-dir", | ||
| str(webui_dir), | ||
| ], | ||
| cwd=REPO_ROOT, | ||
| stdout=log_file, | ||
| stderr=subprocess.STDOUT, | ||
| env=env, | ||
| ) | ||
|
|
||
| print(f"Starting smoke test on {HEALTH_URL}") | ||
| deadline = time.monotonic() + STARTUP_TIMEOUT_SECONDS | ||
| try: | ||
| while time.monotonic() < deadline: | ||
| if _is_ready(): | ||
| print("Smoke test passed") | ||
| return 0 | ||
|
|
||
| return_code = proc.poll() | ||
| if return_code is not None: | ||
| print( | ||
| f"AstrBot exited before becoming healthy. Exit code: {return_code}", | ||
| file=sys.stderr, | ||
| ) | ||
| print(_tail(log_path), file=sys.stderr) | ||
| return 1 | ||
|
|
||
| time.sleep(1) | ||
|
|
||
| print( | ||
| "Smoke test failed: health endpoint did not become ready in time.", | ||
| file=sys.stderr, | ||
| ) | ||
| print(_tail(log_path), file=sys.stderr) | ||
| return 1 | ||
| finally: | ||
| _stop_process(proc) | ||
| try: | ||
| log_path.unlink() | ||
| except OSError: | ||
| pass | ||
| shutil.rmtree(smoke_root, ignore_errors=True) |
There was a problem hiding this comment.
The current implementation of main has a few issues regarding resource management:
- If an exception occurs during setup (e.g.,
webui_dir.mkdir()fails orsubprocess.Popenraises an error), the temporary directory will not be cleaned up because thetry...finallyblock starts too late. - The
log_path.unlink()call is redundant asshutil.rmtree(smoke_root)is called immediately after. - Using
tempfile.TemporaryDirectoryas a context manager is more idiomatic and ensures cleanup even if setup fails.
Consider refactoring the main function to use a context manager for the temporary directory and ensure the process is stopped in a finally block that covers the entire lifecycle.
with tempfile.TemporaryDirectory(prefix="astrbot-smoke-root-") as smoke_root_str:
smoke_root = Path(smoke_root_str)
env["ASTRBOT_ROOT"] = str(smoke_root)
log_path = smoke_root / "smoke.log"
webui_dir = smoke_root / "webui"
webui_dir.mkdir()
(webui_dir / "index.html").write_text(
"<!doctype html><title>AstrBot</title>",
encoding="utf-8",
)
proc = None
try:
with log_path.open("wb") as log_file:
proc = subprocess.Popen(
[
sys.executable,
str(REPO_ROOT / "main.py"),
"--webui-dir",
str(webui_dir),
],
cwd=REPO_ROOT,
stdout=log_file,
stderr=subprocess.STDOUT,
env=env,
)
print(f"Starting smoke test on {HEALTH_URL}")
deadline = time.monotonic() + STARTUP_TIMEOUT_SECONDS
while time.monotonic() < deadline:
if _is_ready():
print("Smoke test passed")
return 0
return_code = proc.poll()
if return_code is not None:
print(
f"AstrBot exited before becoming healthy. Exit code: {return_code}",
file=sys.stderr,
)
print(_tail(log_path), file=sys.stderr)
return 1
time.sleep(1)
print(
"Smoke test failed: health endpoint did not become ready in time.",
file=sys.stderr,
)
print(_tail(log_path), file=sys.stderr)
return 1
finally:
if proc:
_stop_process(proc)
Modifications / 改动点
Screenshots or Test Results / 运行截图或测试结果
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Update smoke test infrastructure for cross-platform, multi-version coverage and adjust a dependency version for compatibility.
Bug Fixes:
Enhancements:
CI:
Tests: