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

fix(server): enable session prewarming by default

alan_cheng68f0b374d096
diff --git a/.doc_project_maintainer/INDEX.md b/.doc_project_maintainer/INDEX.md
index e99e0bef77..8f2eb7ac5a 100644
--- a/.doc_project_maintainer/INDEX.md
+++ b/.doc_project_maintainer/INDEX.md
@@ -40,6 +40,7 @@ Status: partial. A 2026-07-15 semantic-hash scan at `10afedf2` found 0 expired a
- [CHG-20260731-001](changes/records/CHG-20260731-001-session-prewarm-allocation.md): session allocation and DeepAgent prewarming.
- [CHG-20260801-001](changes/records/CHG-20260801-001-prewarm-foreground-priority.md): foreground chat priority, lazy background warming, and non-blocking runtime probes.
- [CHG-20260801-005](changes/records/CHG-20260801-005-disable-session-prewarm.md): prewarming off by default behind `JIUWENSWARM_AGENT_PREWARM`.
+- [CHG-20260803-001](changes/records/CHG-20260803-001-enable-session-prewarm-by-default.md): prewarming on by default; `JIUWENSWARM_AGENT_PREWARM` becomes an opt-out.
## Priority Code Symbols
diff --git a/.doc_project_maintainer/changes/records/CHG-20260803-001-enable-session-prewarm-by-default.md b/.doc_project_maintainer/changes/records/CHG-20260803-001-enable-session-prewarm-by-default.md
new file mode 100644
index 0000000000..48338ee0e7
--- /dev/null
+++ b/.doc_project_maintainer/changes/records/CHG-20260803-001-enable-session-prewarm-by-default.md
@@ -0,0 +1,28 @@
+---
+id: CHG-20260803-001
+date: 2026-08-03
+title: Switch background session prewarming back on by default
+confidence: confirmed
+modules:
+ - agentserver-runtime
+flows:
+ - session-prewarm-allocation
+decisions:
+ - ADR-0001
+---
+
+# Switch Background Session Prewarming Back On By Default
+
+## Change
+
+`JIUWENSWARM_AGENT_PREWARM` turns from an opt-in into an opt-out. `AgentWarmPool` now enables itself unless the variable is set to `0`/`false`/`no`/`off`; `1`/`true`/`yes`/`on` keeps working, and any other non-empty value is logged as unrecognized and treated as enabled so a typo cannot silently disable warming.
+
+Nothing else changed. The dormant behavior introduced by [CHG-20260801-005](CHG-20260801-005-disable-session-prewarm.md) still applies verbatim while the switch is off: `sync` returns zero statistics and `claim` returns a freshly allocated ID with `prewarm_status="bypassed"`. The explicit `enabled` constructor argument still overrides the environment.
+
+## Compatibility
+
+No wire or call-site change. Deployments that relied on the default being off must now set `JIUWENSWARM_AGENT_PREWARM=0`. Deployments that had set `JIUWENSWARM_AGENT_PREWARM=1` are unaffected.
+
+## Verification
+
+- `tests/unit_tests/agentserver/test_agent_warm_pool.py`: added `test_prewarm_is_enabled_unless_the_environment_opts_out` covering unset/off/on, and switched the disabled-pool case to opt out explicitly. 12 passed, with the one pre-existing macOS-only failure in `test_warm_key_normalizes_project_directory`, where the test lowercases a path that `os.path.normcase` leaves untouched on POSIX.
diff --git a/.doc_project_maintainer/code/jiuwenswarm/server/runtime/agent_warm_pool.py/Class AgentWarmPool/Class AgentWarmPool.md b/.doc_project_maintainer/code/jiuwenswarm/server/runtime/agent_warm_pool.py/Class AgentWarmPool/Class AgentWarmPool.md
index 173f9d45f6..6e4ba55bf0 100644
--- a/.doc_project_maintainer/code/jiuwenswarm/server/runtime/agent_warm_pool.py/Class AgentWarmPool/Class AgentWarmPool.md
+++ b/.doc_project_maintainer/code/jiuwenswarm/server/runtime/agent_warm_pool.py/Class AgentWarmPool/Class AgentWarmPool.md
@@ -19,7 +19,7 @@ Maintains at most one speculative READY or warming session globally across eligi
## Key Contracts
-- The pool is inert unless `enabled` (default: the `JIUWENSWARM_AGENT_PREWARM` environment variable) opts in.
+- The pool is active unless `enabled` (default: the `JIUWENSWARM_AGENT_PREWARM` environment variable) opts out.
- Only single-Agent keys enter READY.
- Code keys acquire AgentManager roots with `mode=code, sub_mode=normal`; work keys use `mode=agent, sub_mode=None`.
- A claim is atomic; matching warming work retains its Session ID, and the claimed key is prioritized for replenishment after foreground chat.
diff --git a/.doc_project_maintainer/code/jiuwenswarm/server/runtime/agent_warm_pool.py/agent_warm_pool.py.md b/.doc_project_maintainer/code/jiuwenswarm/server/runtime/agent_warm_pool.py/agent_warm_pool.py.md
index e15af78564..a80818031d 100644
--- a/.doc_project_maintainer/code/jiuwenswarm/server/runtime/agent_warm_pool.py/agent_warm_pool.py.md
+++ b/.doc_project_maintainer/code/jiuwenswarm/server/runtime/agent_warm_pool.py/agent_warm_pool.py.md
@@ -4,14 +4,14 @@ source_role: runtime_source
audit_scope: default_health_audit
language: python
confidence: confirmed
-last_updated: 2026-08-01
+last_updated: 2026-08-03
---
# `agent_warm_pool.py`
## Actual Role
-Owns process-local reconciliation and atomic claiming of session-bound READY DeepAgents. It defines immutable warm identity/revision records, guarded marker cleanup, bounded background initialization, stale revision disposal, claimed-session waiting, and root-agent pinning. All of that stays dormant behind the `JIUWENSWARM_AGENT_PREWARM` opt-in switch, which is off by default.
+Owns process-local reconciliation and atomic claiming of session-bound READY DeepAgents. It defines immutable warm identity/revision records, guarded marker cleanup, bounded background initialization, stale revision disposal, claimed-session waiting, and root-agent pinning. All of that is active by default and stays dormant only when the `JIUWENSWARM_AGENT_PREWARM` switch opts out.
## Symbol Inventory
diff --git a/.doc_project_maintainer/manifest.yaml b/.doc_project_maintainer/manifest.yaml
index 3dd4c5b377..af3b2292cf 100644
--- a/.doc_project_maintainer/manifest.yaml
+++ b/.doc_project_maintainer/manifest.yaml
@@ -292,7 +292,7 @@ flows:
- AgentWebSocketServer._handle_history_get_stream
- id: session-prewarm-allocation
name: Session Prewarm And Allocation
- summary: Gateway syncs live channels; AgentServer prepares, claims, and replenishes session-bound single-Agent runtimes. Prewarming itself is off unless JIUWENSWARM_AGENT_PREWARM opts in.
+ summary: Gateway syncs live channels; AgentServer prepares, claims, and replenishes session-bound single-Agent runtimes. Prewarming itself is on unless JIUWENSWARM_AGENT_PREWARM opts out.
confidence: confirmed
doc: project/flows/session-prewarm-allocation.md
modules:
@@ -419,6 +419,9 @@ changes:
- id: CHG-20260801-005
title: Switch background session prewarming off by default
doc: changes/records/CHG-20260801-005-disable-session-prewarm.md
+- id: CHG-20260803-001
+ title: Switch background session prewarming back on by default
+ doc: changes/records/CHG-20260803-001-enable-session-prewarm-by-default.md
code_symbol_inventory:
generated_by: scripts/inventory_symbols.py
source_symbol_inventory: project/source-symbol-inventory.json
diff --git a/.doc_project_maintainer/modules/agentserver-runtime/changes.md b/.doc_project_maintainer/modules/agentserver-runtime/changes.md
index 2cc2055255..1d3a1a39ff 100644
--- a/.doc_project_maintainer/modules/agentserver-runtime/changes.md
+++ b/.doc_project_maintainer/modules/agentserver-runtime/changes.md
@@ -3,3 +3,4 @@
- [CHG-20260713-001: Close AgentWebSocketServer method audit delivery](../../changes/records/CHG-20260713-001-agentserver-method-closure.md) records the knowledge-model change that closed 128 method audits, refreshed the session flow, and added five AgentServer flows. It does not describe a product-code change.
- [CHG-20260731-001: AgentServer-owned session prewarming](../../changes/records/CHG-20260731-001-session-prewarm-allocation.md) adds session allocation, warm-pool reconciliation, and channel ownership changes.
- [CHG-20260801-005: Prewarming off by default](../../changes/records/CHG-20260801-005-disable-session-prewarm.md) puts warm-pool reconciliation and claiming behind the `JIUWENSWARM_AGENT_PREWARM` opt-in, leaving allocation intact.
+- [CHG-20260803-001: Prewarming on by default](../../changes/records/CHG-20260803-001-enable-session-prewarm-by-default.md) turns that switch into an opt-out; the dormant behavior it gates is unchanged.
diff --git a/.doc_project_maintainer/project/flows/session-prewarm-allocation.md b/.doc_project_maintainer/project/flows/session-prewarm-allocation.md
index a4227e1457..7d65d5112b 100644
--- a/.doc_project_maintainer/project/flows/session-prewarm-allocation.md
+++ b/.doc_project_maintainer/project/flows/session-prewarm-allocation.md
@@ -40,9 +40,9 @@ entrypoints:
## Outcome
-Prewarming is off unless `JIUWENSWARM_AGENT_PREWARM` is set to `1`/`true`/`yes`/`on`. While off, `sync` reports zero statistics and never schedules preparation, and every claim returns a freshly allocated ID with `prewarm_status="bypassed"`; the session initializes lazily on its first request. Allocation, `create_token` idempotency, and `session.create` validation stay in force, so the rest of this flow is unchanged and only its warm path is dormant.
+Prewarming is on unless `JIUWENSWARM_AGENT_PREWARM` is set to `0`/`false`/`no`/`off`. While off, `sync` reports zero statistics and never schedules preparation, and every claim returns a freshly allocated ID with `prewarm_status="bypassed"`; the session initializes lazily on its first request. Allocation, `create_token` idempotency, and `session.create` validation stay in force, so the rest of this flow is unchanged and only its warm path is dormant.
-When opted in, AgentServer keeps at most one speculative, unclaimed READY or warming DeepAgent across all eligible single-Agent work/code keys. Single-Agent identity is canonical: work uses `agent:<empty>:<project>` and code uses `code:normal:<project>` in prewarm, metadata, and chat selection. ACP and A2A never enter the target set. A warm hit avoids `create_instance` and `start_interaction` on the first `chat.send`.
+While on, AgentServer keeps at most one speculative, unclaimed READY or warming DeepAgent across all eligible single-Agent work/code keys. Single-Agent identity is canonical: work uses `agent:<empty>:<project>` and code uses `code:normal:<project>` in prewarm, metadata, and chat selection. ACP and A2A never enter the target set. A warm hit avoids `create_instance` and `start_interaction` on the first `chat.send`.
Team, `code.team`, and Swarm creation bypass the warm pool.
@@ -83,7 +83,7 @@ On startup, old-boot markers and unclaimed metadata-less directories are removed
## Verification
-- `tests/unit_tests/agentserver/test_agent_warm_pool.py` covers the default-off switch alongside global capacity, promotion/cancellation, code `sub_mode=normal`, work-mode canonicalization, and identical prewarm/chat cache identity.
+- `tests/unit_tests/agentserver/test_agent_warm_pool.py` covers the default-on switch and its opt-out alongside global capacity, promotion/cancellation, code `sub_mode=normal`, work-mode canonicalization, and identical prewarm/chat cache identity.
- Adapter tests cover off-loop/coalesced runtime probes and per-workspace/config MemoryRail reindex singleflight.
- AgentServer send/reload/ACP/plan-mode and Gateway ACP suites cover foreground guards and allocation boundaries.
- The latest contention/cache-identity follow-up passed 179 focused tests. Earlier channel-contract runs passed 139 tests, and the full Windows unit run recorded 3,791 passed, 4 skipped, with 14 unrelated failures.
diff --git a/jiuwenswarm/server/runtime/agent_warm_pool.py b/jiuwenswarm/server/runtime/agent_warm_pool.py
index 81b0f422f2..52de724921 100644
--- a/jiuwenswarm/server/runtime/agent_warm_pool.py
+++ b/jiuwenswarm/server/runtime/agent_warm_pool.py
@@ -29,19 +29,31 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__)
-# Background prewarming is opt-in. When it stays off, sessions are allocated an id
-# immediately and initialize lazily on their first request.
+# Background prewarming is on by default and can be opted out of through the
+# environment. Once off, sessions are allocated an id immediately and initialize
+# lazily on their first request.
_PREWARM_ENABLED_ENV_KEY = "JIUWENSWARM_AGENT_PREWARM"
+_PREWARM_ON_VALUES = frozenset({"1", "true", "yes", "on"})
+_PREWARM_OFF_VALUES = frozenset({"0", "false", "no", "off"})
def _prewarm_enabled_by_env() -> bool:
"""Return whether background session prewarming is switched on.
Returns:
- True only when the environment explicitly opts in.
+ The explicit environment choice, or True when the switch is unset or
+ carries an unrecognized value.
"""
raw = str(os.environ.get(_PREWARM_ENABLED_ENV_KEY, "") or "").strip().lower()
- return raw in ("1", "true", "yes", "on")
+ if raw in _PREWARM_OFF_VALUES:
+ return False
+ if raw not in _PREWARM_ON_VALUES and raw:
+ logger.warning(
+ "Ignoring unrecognized %s value %r; keeping prewarming enabled.",
+ _PREWARM_ENABLED_ENV_KEY,
+ raw,
+ )
+ return True
def _zero_stats() -> dict[str, int]:
diff --git a/tests/unit_tests/agentserver/test_agent_warm_pool.py b/tests/unit_tests/agentserver/test_agent_warm_pool.py
index 87a81c78f9..8c60ded0b1 100644
--- a/tests/unit_tests/agentserver/test_agent_warm_pool.py
+++ b/tests/unit_tests/agentserver/test_agent_warm_pool.py
@@ -82,12 +82,34 @@ def isolated_pool(monkeypatch, tmp_path: Path):
)
def factory(agent: _FakeRootAgent) -> AgentWarmPool:
- # Prewarming is off by default; these cases exercise the opted-in pool.
+ # Prewarming is on by default; stay explicit so a developer environment
+ # that opts out cannot silently turn these cases into no-ops.
return AgentWarmPool(_FakeManager(agent), max_concurrency=4, enabled=True)
yield factory
+def test_prewarm_is_enabled_unless_the_environment_opts_out(
+ monkeypatch, tmp_path: Path
+) -> None:
+ monkeypatch.setattr(
+ "jiuwenswarm.server.runtime.agent_warm_pool.get_agent_sessions_dir",
+ lambda: tmp_path,
+ )
+
+ def build() -> AgentWarmPool:
+ return AgentWarmPool(_FakeManager(_FakeRootAgent()))
+
+ monkeypatch.delenv("JIUWENSWARM_AGENT_PREWARM", raising=False)
+ assert build()._enabled is True
+
+ monkeypatch.setenv("JIUWENSWARM_AGENT_PREWARM", " OFF ")
+ assert build()._enabled is False
+
+ monkeypatch.setenv("JIUWENSWARM_AGENT_PREWARM", "1")
+ assert build()._enabled is True
+
+
@pytest.mark.asyncio
async def test_disabled_pool_never_warms_and_always_bypasses(
monkeypatch, tmp_path: Path
@@ -96,7 +118,7 @@ async def test_disabled_pool_never_warms_and_always_bypasses(
"jiuwenswarm.server.runtime.agent_warm_pool.get_agent_sessions_dir",
lambda: tmp_path,
)
- monkeypatch.delenv("JIUWENSWARM_AGENT_PREWARM", raising=False)
+ monkeypatch.setenv("JIUWENSWARM_AGENT_PREWARM", "0")
agent = _FakeRootAgent()
pool = AgentWarmPool(_FakeManager(agent))