包含 CodeAgent 前后端源码、SKILLS、MCP、demo 代码与工作区文档。 排除依赖缓存、运行数据、备份与二进制附件,规则见 .gitignore。 Co-Authored-By: Claude Code <[email protected]>
67 lines
2.5 KiB
Python
67 lines
2.5 KiB
Python
"""
|
|
热重载冲突守卫
|
|
==============
|
|
|
|
【功能说明】
|
|
在「跑任务」与「热重载」结构性互斥的前提下,把静默被杀变成明确拒绝。
|
|
|
|
【为什么需要】
|
|
Claude 干活会写文件(临时脚本、改源码)。若后端以 `--reload` 运行,
|
|
一旦 Claude 写到被监听目录,WatchFiles 就重启后端进程,正在执行的任务当场被杀:
|
|
WatchFiles detected changes → Reloading → ASGI callable returned without completing response
|
|
这不是调参能解决的,二者在结构上互斥。
|
|
|
|
【设计取舍】
|
|
宁可「发起时明确报错」,也不要「跑一半静默失败」。
|
|
报错信息直接告诉用户换哪个脚本启动,不让他猜。
|
|
|
|
【只拦会写文件的任务】
|
|
direct_claude / agent 都会调 Claude CLI(会写文件),必须拦;
|
|
纯查询类接口不经过这里,不受影响。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
|
|
|
|
def is_reload_mode() -> bool:
|
|
"""检测当前进程是否运行在 uvicorn --reload 热重载模式下。"""
|
|
# 1. 命令行参数(最直接)
|
|
if any(a == "--reload" or a.startswith("--reload") for a in sys.argv):
|
|
return True
|
|
# 2. 环境变量兜底(部分启动方式会带上)
|
|
env = os.environ.get("UVICORN_RELOAD", "")
|
|
return env.lower() in ("1", "true", "yes")
|
|
|
|
|
|
# 报错文案:直接给出替代命令,用户照抄即可
|
|
_RELOAD_CONFLICT_MESSAGE = (
|
|
"当前后端以 --reload 热重载模式运行,无法执行改码任务。\n"
|
|
"原因:Claude 执行时会写文件,触发 WatchFiles 自动重启后端,任务会被中途杀死。\n"
|
|
"解决:停止当前后端,改用无热重载方式启动后重试:\n"
|
|
" cd D:\\Agent\\agent\\backend\n"
|
|
" powershell -ExecutionPolicy Bypass -File .\\start_backend.ps1\n"
|
|
"(若确需边改代码边调试,请用 start_backend.dev.ps1,它只监听 app/ 目录)"
|
|
)
|
|
|
|
|
|
class ReloadConflictError(RuntimeError):
|
|
"""热重载与任务执行冲突"""
|
|
|
|
def __init__(self) -> None:
|
|
super().__init__(_RELOAD_CONFLICT_MESSAGE)
|
|
self.error_type = "ReloadConflict"
|
|
|
|
|
|
def assert_can_run_task() -> None:
|
|
"""
|
|
断言当前环境允许执行会写文件的任务。
|
|
|
|
【抛出】ReloadConflictError:热重载模式下禁止执行
|
|
【调用点】create_and_run / continue_chat —— 在任务真正启动前拦截
|
|
"""
|
|
if is_reload_mode():
|
|
raise ReloadConflictError()
|