Files
AgentScript/agent/backend/app/tools/proc_flags.py
T
HenryandClaude Code 26e13ff032 feat: Agent 工作区初始提交(源码+文档+Skills)
包含 CodeAgent 前后端源码、SKILLS、MCP、demo 代码与工作区文档。
排除依赖缓存、运行数据、备份与二进制附件,规则见 .gitignore。

Co-Authored-By: Claude Code <[email protected]>
2026-11-13 13:48:41 +08:00

64 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Windows 子进程窗口抑制
====================
【功能说明】
统一提供「子进程不弹出控制台窗口」的 Popen/run 参数。
【为什么需要】
claude CLI 的入口是 **claude.CMD 批处理脚本**(控制台程序),
直接拉起会分配一个可见控制台窗口 → 用户每次提问都弹出命令行。
【关键实测结论】(2026-10-23 探针验证,勿再改错)
标志组合 控制台窗口 父进程退出后存活
───────────────────────────────────────────── ───────── ────────────────
无标志 无 ?
CREATE_NO_WINDOW **无** **存活**
CREATE_NO_WINDOW | CREATE_NEW_PROCESS_GROUP **无** **存活**
DETACHED_PROCESS | CREATE_NO_WINDOW | GROUP **有!** 存活
→ **DETACHED_PROCESS 才是弹窗元凶**:它让控制台程序「无台可继承」时
**自己 AllocConsole 一个可见控制台**,并压过 CREATE_NO_WINDOW。
→ 只用 CREATE_NO_WINDOW 就同时拿到「不弹窗」和「独立存活」,
不要再加 DETACHED_PROCESS。
【教训】第一次修复往 DETACHED_PROCESS 上叠 CREATE_NO_WINDOW,等于没治病根。
"""
from __future__ import annotations
import os
import subprocess
def hidden_popen_kwargs() -> dict:
"""
返回隐藏窗口所需的额外 Popen/run 参数。
【用法】
subprocess.Popen(cmd, **hidden_popen_kwargs())
subprocess.run(cmd, capture_output=True, **hidden_popen_kwargs())
【返回】Windows 下为 {"creationflags": CREATE_NO_WINDOW},其它平台为空 dict。
"""
if os.name != "nt":
return {}
return {"creationflags": subprocess.CREATE_NO_WINDOW}
def worker_creation_flags() -> int:
"""
独立 worker 进程的创建标志。
【Windows】CREATE_NO_WINDOW 不弹出控制台窗口
CREATE_NEW_PROCESS_GROUP 不接收父进程 Ctrl+C
【绝不使用】DETACHED_PROCESS —— 会让控制台程序自建**可见**控制台,是弹窗元凶。
【独立性】Windows 上父进程退出不会连带杀死子进程(无 Job Object 时),
实测 CREATE_NO_WINDOW 下父退子活,足够支撑「API 重启不杀任务」。
【其它平台】返回 0(由调用方改用 start_new_session)
"""
if os.name != "nt":
return 0
return subprocess.CREATE_NO_WINDOW | subprocess.CREATE_NEW_PROCESS_GROUP