agent-Specialization/modules/sub_agent/prompts.py
JOJO 493ac160eb fix(windows): 平台适配与稳定性修复(进程终止/编码/原子写/事件竞态/子智能体)
- 进程终止:Windows 用 CTRL_BREAK + taskkill /F /T 替代不存在的 killpg/SIGKILL,
  超时/取消全路径有界;_is_pid_alive 改 ctypes(原 os.kill(pid,0) 在 Windows 会真杀进程);
  reader 任务 finally 收口,消除 Task was destroyed but it is pending / unclosed transport
- 子智能体调度链:_run_coro 超时 60s + 失败 cancel/close 防幽灵协程;create 改先调度后提交、
  失败回滚;state 归属守卫 + 新建 120s 宽限期防误标已终止;_ensure_event_loop 加锁;
  子智能体 todo 改 per-agent 隔离存储,不再串到主智能体前端
- 子智能体执行环境(execution_env_text.py):创建/切换/恢复三时点注入环境说明,
  Windows 下 sandbox=WSL2 bash、direct=cmd;inject_notification 纯通知不触发新一轮工作,
  tool 序列中延迟到安全点 flush;传统子智能体按既定语义不通知
- 编码:git 侧边栏 subprocess 显式 utf-8 + errors=replace(修中文 Windows GBK
  _readerthread 崩溃导致侧边栏空白);check_environment/install_package 用 self.python_cmd
- 安全/路径:permission.py Windows deny 列表生效 + 驱动器根拦截;路径比较统一 normcase;
  /tmp 白名单平台分流;正斜杠判断归一化;os-release 平台守卫;PowerShell -ExecutionPolicy Bypass
- 稳定性:任务事件轮询持锁快照(修 deque mutated during iteration);
  原子写新增 replace_with_retry(修 WinError 5/32 瞬时持锁);
  共享文件兜底链 symlink→os.link 硬链接→copy2
- 部署:新增 _bootstrap.bat / setup.bat / start.bat Windows 启动脚本
2026-07-31 11:33:57 +08:00

83 lines
2.5 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.

"""子智能体提示词构建。
所有 prompt 正文均从 prompts/sub_agent/ 下的文本文件加载,避免在代码中硬编码。
"""
from __future__ import annotations
import platform
from datetime import datetime
from pathlib import Path
from typing import Optional
try:
from config import PROMPTS_DIR
except ImportError:
import sys
project_root = Path(__file__).resolve().parents[2]
if str(project_root) not in sys.path:
sys.path.insert(0, str(project_root))
from config import PROMPTS_DIR
_SUB_AGENT_PROMPTS_DIR = Path(PROMPTS_DIR) / "sub_agent"
_TEMPLATE_CACHE: dict[str, str] = {}
def _load_template(name: str) -> str:
"""从 prompts/sub_agent/<name>.txt 加载模板,带缓存。"""
cached = _TEMPLATE_CACHE.get(name)
if cached is not None:
return cached
template_path = _SUB_AGENT_PROMPTS_DIR / f"{name}.txt"
if not template_path.exists():
raise FileNotFoundError(f"子智能体 prompt 模板缺失: {template_path}")
content = template_path.read_text(encoding="utf-8")
_TEMPLATE_CACHE[name] = content
return content
def _format_template(name: str, **kwargs) -> str:
"""加载模板并用 str.format 填充占位符。"""
template = _load_template(name)
return template.format(**kwargs)
def build_user_message(
agent_id: int,
summary: str,
task: str,
deliverables_path: str,
timeout_seconds: int,
) -> str:
"""构建发送给子智能体的用户消息。"""
return _format_template(
"user_message",
agent_id=agent_id,
summary=summary,
task=task,
deliverables_path=deliverables_path,
timeout_seconds=timeout_seconds,
)
def build_system_prompt(workspace_path: str, execution_mode: str = "") -> str:
"""构建子智能体的系统提示。
`execution_mode` 为创建时刻的执行环境sandbox/direct快照写入提示词
后续若发生切换,由运行期通知机制补充告知(见 manager.notify_execution_mode_changed
"""
from modules.execution_env_text import build_sub_agent_env_section
system_info = f"{platform.system()} {platform.release()}"
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return _format_template(
"system",
workspace_path=workspace_path,
system_info=system_info,
current_time=current_time,
execution_env_section=build_sub_agent_env_section(workspace_path, execution_mode),
)