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.
id: session-prewarm-allocation name: Session Prewarm And Allocation status: partial confidence: confirmed last_updated: 2026-08-01 user_visible_surface: "Low-latency creation of single-Agent work/code sessions across enabled channels." source_of_truth: - "AgentServer session metadata" - "AgentServer process-local warm pool" modules: - agentserver-runtime - gateway-and-channels - agent-harness directories: - jiuwenswarm/server - jiuwenswarm/gateway - jiuwenswarm/channels code_symbols: - AgentWarmPool - AgentWarmPool.sync - AgentWarmPool.claim - AgentWarmPool.begin_foreground - AgentWarmPool.end_foreground - AgentWebSocketServer._handle_agent_prewarm_sync - AgentWebSocketServer._handle_session_create - AgentWebSocketServer._handle_stream - AgentWebSocketServer._handle_unary - resolve_agent_request_mode - JiuWenSwarmDeepAdapter.prepare_session - JiuWenSwarmDeepAdapter.configure_session_runtime - JiuWenSwarmDeepAdapter._schedule_runtime_state_write - JiuWenSwarmDeepAdapter._schedule_memory_reindex entrypoints: - jiuwenswarm/gateway/app_gateway.py - jiuwenswarm/server/agent_ws_server.py
Session Prewarm And Allocation
Outcome
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.
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.
Causal Path
- Gateway finishes channel registration and sends
agent.prewarm.syncwith deduplicated eligible channel IDs after filtering ACP/A2A. AgentServer repeats the exclusion defensively. Startup, configuration, and channel triggers are debounced and coalesced so reload completion is not followed by duplicate reconcile bursts; a periodic scan is the fallback. - AgentServer scans visible/default projects in a worker thread and stores one immutable
WarmKeytarget snapshot. Reconciliation retains missing targets as pending keys, but globally permits only one speculative READY or warming slot; it does not create one asyncio task per project/channel at startup. - Background preparation gets the root adapter with
(code, normal)for code or(agent, None)for work, creates the session child, configures stable runtime state, and starts the interaction without sending input. session.createvalidates project binding, derives the single-Agent canonical mode from finalwork_mode, and persists that mode before allocation. A READY slot is atomically claimed; matching warming work is promoted with its existing ID.- AgentServer writes normal metadata only after claim. The prewarm marker is retained through the claim and removed only after metadata commits, closing the crash gap without exposing blank slots in normal session listings.
- Chat selection reads the locked Session
work_mode(falling back to the request), canonicalizes stalemode=agentcode requests tocode.normal, awaits the claimed task, and selects the same AgentManager cache key. Foreground cancellation and the shared registry lock prevent competing initialization. - MemoryRail registration does not schedule a full reindex on first registration. A real embedding-configuration change is singleflight per normalized workspace and fingerprint, preventing parallel new sessions from repeating the same repository-wide indexing work.
- Web, TUI, IM, ACP, A2A, SSH, and single-Agent Cron use the returned ID. ACP/A2A/SSH retain protocol IDs as Gateway aliases. Fork IDs are also AgentServer allocated but do not consume blank warm slots.
State And Identity
- Source of truth: claimed session metadata and history under AgentServer ownership.
- Cache: READY slots, preparation tasks,
create_tokenresults, and external-channel aliases are process-local. - Scheduling state: one target snapshot, at most one speculative READY/warming slot globally, pending/prioritized keys, promoted tasks, foreground count, and a shared initialization lock are process-local.
- Disposable state:
.prewarm/<session_id>.jsonmarkers identify unclaimed slots and includeboot_id. - Revision:
boot_id + SHA-256(config/env) + sequence; only matching current fingerprints may publish READY. - ID format:
<channel>_<timestamp/random>.
On startup, old-boot markers and unclaimed metadata-less directories are removed. Claimed/history sessions are not removed. Configuration changes stale only unclaimed slots; active sessions continue through existing reload behavior.
Failure, Ordering, And Idempotency
- Project validation precedes allocation.
- Explicit IDs are rejected by
session.create; existing-session restoration usessession.switch. create_tokenis required by adapted frontends and enables response-loss retry.- Gateway-owned Web creation overwrites any request-body
user_idwith the authenticated connection identity before forwarding to AgentServer. - Initialization exceptions are logged and never publish READY.
- Continuous reconciles cancel superseded warm tasks; late old-revision completion cannot enter the pool.
- A READY root stays pinned until the claimed session reaches its first AgentServer request, preventing idle retirement in the create-to-send gap.
- Foreground entry cancels non-promoted background initialization. Cancellation remains cooperative, so a synchronous third-party call may finish before releasing the shared initialization lock, but no competing foreground initializer mutates process-global registries concurrently and no successor starts while chat is active.
- Runtime-state Git probes and Git-ignore checks execute in worker threads. Runtime-state writes are coalesced per adapter and globally bounded to two concurrent probes, so they do not synchronously hold the AgentServer loop.
- A lightweight/restored adapter without an initialized diagnostic-task slot treats that slot as idle, so optional runtime-state persistence cannot abort request configuration.
Verification
tests/unit_tests/agentserver/test_agent_warm_pool.pycovers the default-on switch and its opt-out alongside global capacity, promotion/cancellation, codesub_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.
- A restarted ham-snake Web session previously completed in about 7.5 seconds; sustained live-load verification of this contention correction remains pending.
Known Gaps
The pool is process-local and does not preserve idempotency tokens across AgentServer restart. Full live multi-channel integration, sustained load/resource limits, cancellation of a background initializer already inside a non-cooperative third-party call, checkpointer cleanup validation, and long-lived unclaimed-claim pin expiry remain pending. The repository-wide symbol audit was not expanded; changed symbols remain unaudited or audit-expired.