feat: 跨平台进程保护机制 v5.3.0

- 新增 process_protection.py 模块
  - 单实例保护: Windows命名Mutex / macOS+Linux fcntl文件锁
  - 进程优先级提升: Windows SetPriorityClass / POSIX os.nice()
  - Windows PPL风格保护: SetErrorMode抑制错误弹窗
  - POSIX信号保护: 忽略SIGHUP/SIGPIPE, 注册SIGTERM优雅关闭
  - 看门狗监控: 后台线程监控父进程状态
  - 关闭钩子系统: 进程退出时自动执行清理
  - /protect 命令查看保护状态
- 更新 platform_utils.py 新增进程工具函数
- 集成到 cli_base.py 启动时自动初始化
- 更新 CHANGELOG.md / README.md 至 v5.3.0
This commit is contained in:
xiaoli-cli committed 2026-05-09 20:28:17 +08:00
1 parent a7f64cd797
commit 97cc2bb0f7
6 files changed
+808 -2

No files matched your search

+76 -1
View File
@@ -121,4 +121,79 @@ def get_config_directory():
elif platform == 'macos':
return os.path.join(os.path.expanduser("~"), 'Library', 'Application Support', 'Xiaoli Pro-CLI 3')
else: # Linux
return os.path.join(os.path.expanduser('~'), '.config', 'xiaoli-pro-cli-3')
return os.path.join(os.path.expanduser('~'), '.config', 'xiaoli-pro-cli-3')
def is_process_running(pid):
"""检查指定 PID 的进程是否在运行"""
platform = get_platform()
try:
if platform == 'windows':
import ctypes
kernel32 = ctypes.windll.kernel32
handle = kernel32.OpenProcess(0x1000, False, pid) # PROCESS_QUERY_LIMITED_INFORMATION
if handle:
kernel32.CloseHandle(handle)
return True
return False
else:
os.kill(pid, 0)
return True
except (OSError, ProcessLookupError):
return False
def get_process_priority():
"""获取当前进程优先级"""
platform = get_platform()
try:
if platform == 'windows':
import ctypes
kernel32 = ctypes.windll.kernel32
handle = kernel32.GetCurrentProcess()
return kernel32.GetPriorityClass(handle)
else:
import resource
return resource.getpriority(resource.PRIO_PROCESS, 0)
except Exception:
return None
def set_process_priority(level="above_normal"):
"""
设置进程优先级
Args:
level: "below_normal" | "normal" | "above_normal" | "high"
Returns:
是否设置成功
"""
platform = get_platform()
try:
if platform == 'windows':
import ctypes
kernel32 = ctypes.windll.kernel32
priority_map = {
"below_normal": 0x00004000,
"normal": 0x00000020,
"above_normal": 0x00008000,
"high": 0x00000080,
}
priority = priority_map.get(level, 0x00008000)
handle = kernel32.GetCurrentProcess()
return bool(kernel32.SetPriorityClass(handle, priority))
else:
nice_map = {
"below_normal": 10,
"normal": 0,
"above_normal": -10,
"high": -15,
}
nice_val = nice_map.get(level, -10)
os.nice(nice_val)
return True
except Exception:
return False