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
fix(server): make the injected git status a real conversation snapshot
alan_chengf6d7f652f431
diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.pyindex 8d1c380d06..322f12cf91 100644--- a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py+++ b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py@@ -488,6 +488,21 @@ def _stage_breakdown_logger(total_ms: float, threshold_ms: float) -> Callable[..return server_logger.info if total_ms >= threshold_ms else server_logger.debug+@dataclass(frozen=True)+class _GitSnapshot:+ """Git state as of a conversation's first turn, held for its whole life.++ Attributes:+ branch: Current branch name, or "HEAD" when detached.+ status: ``git status --short`` output, capped at 50 lines.+ recent_commits: ``git log --oneline -5`` output.+ """++ branch: str+ status: str+ recent_commits: str++@dataclass(frozen=True)class _StableGitFacts:"""Git facts about a project that do not change between turns of a chat.@@ -1051,6 +1066,11 @@ class JiuWenSwarmDeepAdapter:# Language the currently registered cron tools were built for, or None# when they are not registered yet. Doubles as the rebuild condition.self._cron_tools_registered_language: str | None = None+ # Git snapshot per (project dir, session), taken on a conversation's+ # first turn. Held on the adapter rather than module-globally so it is+ # reclaimed with the session instead of accumulating for the life of+ # the process.+ self._session_git_snapshots: dict[str, _GitSnapshot] = {}self._is_proactive_memory: bool | None = Noneself._model_cache: dict[str, Model] = {}self._model_name_to_keys: dict[str, list[str]] = {}@@ -1888,6 +1908,45 @@ class JiuWenSwarmDeepAdapter:return self._model_request_config.model_name or "unknown"return "unknown"+ def _resolve_session_git_snapshot(+ self,+ project_dir: str,+ session_id: str | None,+ run_git: Callable[[list[str]], str],+ ) -> _GitSnapshot:+ """Return this conversation's git snapshot, taking it on the first turn.++ The prompt these values feed states plainly that it is "the git status+ at the start of the conversation" which "will not update during the+ conversation" — but they used to be re-read on every turn, so editing a+ single file rewrote the system prompt mid-conversation. That breaks the+ prompt's own contract and, because the text sits in the cached prefix,+ invalidates the model's KV cache for every later turn.++ Args:+ project_dir: Directory the git commands run in.+ session_id: Conversation the snapshot belongs to; a new session+ takes a fresh one.+ run_git: Callable running git in ``project_dir`` and returning+ stripped stdout, or an empty string on failure.++ Returns:+ The snapshot for this conversation.+ """+ cache_key = f"{project_dir}\0{session_id or ''}"+ cached = self._session_git_snapshots.get(cache_key)+ if cached is not None:+ return cached++ status_lines = run_git(["status", "--short"]).splitlines()+ snapshot = _GitSnapshot(+ branch=run_git(["rev-parse", "--abbrev-ref", "HEAD"]) or "HEAD",+ status="\n".join(status_lines[:50]),+ recent_commits=run_git(["log", "--oneline", "-5"]),+ )+ self._session_git_snapshots[cache_key] = snapshot+ return snapshot+def _write_runtime_state(self,mode: str,@@ -1923,12 +1982,14 @@ class JiuWenSwarmDeepAdapter:if stable_facts.is_repo:git_user = stable_facts.user_namegit_main_branch = stable_facts.main_branch- # Re-read every turn: these are what the user changes- # while talking to the agent.- git_branch = _run_git(["rev-parse", "--abbrev-ref", "HEAD"]) or "HEAD"- git_status_lines = _run_git(["status", "--short"]).splitlines()- git_status = "\n".join(git_status_lines[:50])- git_recent_commits = _run_git(["log", "--oneline", "-5"])+ snapshot = self._resolve_session_git_snapshot(+ project_dir,+ session_id,+ _run_git,+ )+ git_branch = snapshot.branch+ git_status = snapshot.status+ git_recent_commits = snapshot.recent_commitsexcept Exception:pass@@ -8774,6 +8835,11 @@ class JiuWenSwarmDeepAdapter:"input_tokens": 0,"output_tokens": 0,"total_tokens": 0,+ # Prompt tokens the provider served from its KV cache. Reported as a+ # hit rate below: it is the direct read on whether the cached prefix+ # is holding steady across turns, which is what keeps a 30k-token+ # prompt from being re-processed on every message.+ "cache_tokens": 0,"input_cost": 0.0,"output_cost": 0.0,"total_cost": 0.0,@@ -9225,7 +9291,12 @@ class JiuWenSwarmDeepAdapter:else {})if isinstance(usage_meta, dict):- for token in ("input_tokens", "output_tokens", "total_tokens"):+ for token in (+ "input_tokens",+ "output_tokens",+ "total_tokens",+ "cache_tokens",+ ):usage_accumulator[token] += usage_meta.get(token, 0) or 0for cost in ("input_cost", "output_cost", "total_cost"):usage_accumulator[cost] += usage_meta.get(cost, 0.0) or 0.0@@ -9523,6 +9594,11 @@ class JiuWenSwarmDeepAdapter:"output_tokens": usage_accumulator["output_tokens"],"total_tokens": usage_accumulator["total_tokens"],}+ input_tokens = usage_accumulator["input_tokens"]+ if input_tokens > 0:+ cache_tokens = usage_accumulator["cache_tokens"]+ summary["cache_tokens"] = cache_tokens+ summary["cache_hit_rate"] = f"{cache_tokens / input_tokens:.1%}"if usage_accumulator["input_cost"] > 0:summary["input_cost"] = round(usage_accumulator["input_cost"], 6)if usage_accumulator["output_cost"] > 0:diff --git a/tests/unit_tests/agentserver/test_session_git_snapshot.py b/tests/unit_tests/agentserver/test_session_git_snapshot.pynew file mode 100644index 0000000000..fb2236ab9a--- /dev/null+++ b/tests/unit_tests/agentserver/test_session_git_snapshot.py@@ -0,0 +1,127 @@+# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.++"""Unit tests for the per-conversation git snapshot behind the runtime prompt."""++from __future__ import annotations++import pytest++from jiuwenswarm.server.runtime.agent_adapter.interface_deep import JiuWenSwarmDeepAdapter+++def _make_adapter() -> JiuWenSwarmDeepAdapter:+ """Create a bare adapter carrying only the snapshot cache."""+ adapter = object.__new__(JiuWenSwarmDeepAdapter)+ adapter._session_git_snapshots = {}+ return adapter+++def _make_runner(calls: list[list[str]], status: str = " M a.py") -> callable:+ """Return a git runner double recording commands and serving mutable status."""+ state = {"status": status}++ def _run(args: list[str]) -> str:+ calls.append(args)+ if args[:2] == ["rev-parse", "--abbrev-ref"]:+ return "dev/branch"+ if args[0] == "status":+ return state["status"]+ if args[0] == "log":+ return "abc123 first"+ return ""++ _run.state = state+ return _run+++def test_snapshot_is_taken_once_per_conversation() -> None:+ """Later turns reuse the first turn's snapshot instead of re-running git."""+ adapter = _make_adapter()+ calls: list[list[str]] = []+ runner = _make_runner(calls)++ first = adapter._resolve_session_git_snapshot("/repo", "sess_a", runner)+ for _ in range(4):+ adapter._resolve_session_git_snapshot("/repo", "sess_a", runner)++ assert first.branch == "dev/branch"+ assert first.status == " M a.py"+ assert first.recent_commits == "abc123 first"+ # branch + status + log on the first turn, nothing after.+ assert len(calls) == 3+++def test_edits_during_the_conversation_do_not_change_the_snapshot() -> None:+ """The injected prompt promises a start-of-conversation snapshot.++ Re-reading it per turn is what rewrote the system prompt mid-conversation+ and invalidated the model's cached prefix.+ """+ adapter = _make_adapter()+ calls: list[list[str]] = []+ runner = _make_runner(calls)+ adapter._resolve_session_git_snapshot("/repo", "sess_a", runner)++ runner.state["status"] = " M a.py\n M b.py\n?? c.py"+ later = adapter._resolve_session_git_snapshot("/repo", "sess_a", runner)++ assert later.status == " M a.py"+++def test_a_new_conversation_takes_a_fresh_snapshot() -> None:+ """A new session is a new conversation, so it re-reads git."""+ adapter = _make_adapter()+ calls: list[list[str]] = []+ runner = _make_runner(calls)+ adapter._resolve_session_git_snapshot("/repo", "sess_a", runner)+ runner.state["status"] = " M b.py"+ calls.clear()++ fresh = adapter._resolve_session_git_snapshot("/repo", "sess_b", runner)++ assert fresh.status == " M b.py"+ assert len(calls) == 3+++def test_switching_project_dir_takes_a_fresh_snapshot() -> None:+ """Two project dirs in one session must not share a snapshot."""+ adapter = _make_adapter()+ calls: list[list[str]] = []+ runner = _make_runner(calls)+ adapter._resolve_session_git_snapshot("/repo/a", "sess_a", runner)+ calls.clear()++ adapter._resolve_session_git_snapshot("/repo/b", "sess_a", runner)++ assert len(calls) == 3+++def test_status_is_capped_at_fifty_lines() -> None:+ """A large working tree must not flood the prompt."""+ adapter = _make_adapter()+ runner = _make_runner([], status="\n".join(f" M file{i}.py" for i in range(80)))++ snapshot = adapter._resolve_session_git_snapshot("/repo", "sess_a", runner)++ assert len(snapshot.status.splitlines()) == 50+++def test_detached_head_falls_back_to_a_stable_name() -> None:+ """An empty branch read must not leave the prompt with a blank field."""+ adapter = _make_adapter()++ def _run(args: list[str]) -> str:+ return ""++ snapshot = adapter._resolve_session_git_snapshot("/repo", "sess_a", _run)++ assert snapshot.branch == "HEAD"+++def test_snapshots_are_immutable() -> None:+ """A shared snapshot must not be editable by one caller."""+ adapter = _make_adapter()+ snapshot = adapter._resolve_session_git_snapshot("/repo", "sess_a", _make_runner([]))++ with pytest.raises(Exception):+ snapshot.status = "tampered"