Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

feat(runtime-rail): enrich runtime prompt with CWD, platform, Python and git branch

SnapeK1f25e0975a67
diff --git a/jiuwenclaw/agentserver/deep_agent/rails/runtime_prompt_rail.py b/jiuwenclaw/agentserver/deep_agent/rails/runtime_prompt_rail.py
index c318b0142f..a034259558 100644
--- a/jiuwenclaw/agentserver/deep_agent/rails/runtime_prompt_rail.py
+++ b/jiuwenclaw/agentserver/deep_agent/rails/runtime_prompt_rail.py
@@ -7,6 +7,10 @@ static identity prompt and refreshed on every model call via before_model_call()
"""
from __future__ import annotations
+import os
+import platform
+import subprocess
+from shutil import which
from datetime import datetime, timedelta, timezone
from openjiuwen.core.single_agent.rail.base import AgentCallbackContext
@@ -54,6 +58,27 @@ class RuntimePromptRail(DeepAgentRail):
"""per-request 更新频道。"""
self._channel = channel
+ @staticmethod
+ def _get_git_branch() -> str:
+ """获取当前 git 分支名,失败时返回 N/A。"""
+ git_bin = which("git")
+ if not git_bin:
+ return "N/A"
+
+ try:
+ result = subprocess.run(
+ [git_bin, "rev-parse", "--abbrev-ref", "HEAD"],
+ capture_output=True,
+ text=True,
+ timeout=5,
+ )
+ except (subprocess.SubprocessError, OSError):
+ return "N/A"
+
+ if result.returncode == 0 and result.stdout.strip():
+ return result.stdout.strip()
+ return "N/A"
+
async def before_model_call(self, ctx: AgentCallbackContext) -> None:
"""每次 model call 注入最新的时间和运行时信息。"""
if not self.system_prompt_builder:
@@ -72,17 +97,34 @@ class RuntimePromptRail(DeepAgentRail):
priority=92,
))
+ cwd = os.getcwd()
+ plat = f"{platform.system()} {platform.machine()}"
+ python_ver = platform.python_version()
+ git_branch = self._get_git_branch()
+
if self._language == "cn":
runtime_content = (
"# 运行时\n\n"
- f"运行时:agent={self._agent_name} | model={self._model_name} "
- f"| channel={self._channel} | language={self._language}"
+ f"- 工作目录:{cwd}\n"
+ f"- 平台:{plat}\n"
+ f"- Python:{python_ver}\n"
+ f"- 模型:{self._model_name}\n"
+ f"- Git 分支:{git_branch}\n"
+ f"- Agent:{self._agent_name}\n"
+ f"- 频道:{self._channel}\n"
+ f"- 语言:{self._language}"
)
else:
runtime_content = (
"# Runtime\n\n"
- f"Runtime: agent={self._agent_name} | model={self._model_name} "
- f"| channel={self._channel} | language={self._language}"
+ f"- CWD: {cwd}\n"
+ f"- Platform: {plat}\n"
+ f"- Python: {python_ver}\n"
+ f"- Model: {self._model_name}\n"
+ f"- Git branch: {git_branch}\n"
+ f"- Agent: {self._agent_name}\n"
+ f"- Channel: {self._channel}\n"
+ f"- Language: {self._language}"
)
self.system_prompt_builder.add_section(PromptSection(
diff --git a/tests/unit_tests/agentserver/test_system_prompt_restructure.py b/tests/unit_tests/agentserver/test_system_prompt_restructure.py
index 554feac3fb..062bf048bc 100644
--- a/tests/unit_tests/agentserver/test_system_prompt_restructure.py
+++ b/tests/unit_tests/agentserver/test_system_prompt_restructure.py
@@ -60,7 +60,7 @@ async def test_runtime_time_section_participates_in_priority_order():
]
positions = [prompt.index(marker) for marker in ordered_markers]
assert positions == sorted(positions)
- assert "运行时:agent=main_agent | model=test-model | channel=web | language=cn" in prompt
+ assert "频道:web" in prompt
def test_resolve_skill_mode_accepts_all_and_auto_list():