Principle:1jehuang Jcode Single Server Multi Client
| Knowledge Sources | |
|---|---|
| Domains | Systems_Architecture, Concurrency |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
Architecture where one persistent daemon process owns all sessions and serves multiple TUI/SDK clients over a shared Unix socket, enabling session sharing, hot reload, and zero-downtime upgrades.
Description
The single-server, multi-client architecture is a process model where one long-lived daemon process owns all agent sessions, provider connections, and tool state. Multiple clients (TUI instances, SDK integrations, headless runners) connect to this daemon over a Unix socket and share the same sessions. A client disconnect does not destroy the session; another client can resume it.
The problem solved is session fragility: if each client process owns its own session, a client crash kills the session and all its context. By centralizing session ownership in a daemon, clients become thin: they connect, interact, and disconnect without losing state. The daemon survives client exits, terminal closes, and network interruptions.
The key design elements are: (1) socket detection — a new client checks if a live daemon is listening before spawning one, (2) daemon spawning — if no server exists, the client spawns one via setsid and waits for readiness, (3) session creation or resume — clients create new sessions or resume existing ones by ID, and (4) idle shutdown — temporary servers with no clients for a configurable timeout exit gracefully.
Usage
Use this principle when designing a tool that should maintain state across client disconnects and support multiple simultaneous clients. The trigger is: "the tool needs persistent sessions that survive client exits and can be shared between clients." It is appropriate for: CLI agent tools, IDE integrations, and SDK-embedded engines.
Theoretical Basis
The lifecycle has six phases:
- Detection:
socket_has_live_listener(path)checks if a daemon is already listening on the socket. If yes, the client connects to it. - Spawning: If no server exists, the client spawns one with
setsid()(detaching from the terminal), binds the socket, acquires a daemon lock, and writes server metadata. - Session management: Clients send
CreateSessionorResumeSessionrequests over the socket. Sessions are identified by animal-noun names and managed in a shared map. - Reconnection: On disconnect or reload, clients use exponential backoff to reconnect to the same socket. The daemon preserves the socket through
exec()reloads. - Hot reload: The daemon can
exec()into a newer binary on the same socket, with all sessions preserved via reload markers. - Shutdown: Temporary servers (with
JCODE_TEMP_SERVER) monitor client count and exit after an idle timeout. The daemon drains in-flight session saves before exit.
Pseudo-code logic:
# Abstract algorithm (NOT real implementation)
def client_connect(socket_path):
# 1. Detect
if socket_has_live_listener(socket_path):
return connect(socket_path)
# 2. Spawn daemon
child = spawn_server(socket_path)
wait_for_ready(child, timeout=10s)
# 3. Connect
return connect(socket_path)
def server_run(socket_path):
lock = acquire_daemon_lock(socket_path)
listener = bind(socket_path)
sessions = {}
while True:
stream = listener.accept()
request = read_request(stream)
if request.type == "create_session":
session = Session.create()
sessions[session.id] = session
send(stream, SessionCreated(session))
elif request.type == "resume_session":
session = sessions[request.session_id]
send(stream, SessionResumed(session))
elif request.type == "reload":
save_reload_markers(sessions)
exec(new_binary) # new process resumes here
Related Pages
Implemented By
- Implementation:1jehuang_Jcode_socket_path
- Implementation:1jehuang_Jcode_Server_run
- Implementation:1jehuang_Jcode_handle_client
- Implementation:1jehuang_Jcode_connect_socket
- Implementation:1jehuang_Jcode_handle_reload
- Implementation:1jehuang_Jcode_Sdk_Socket_Path_Resolution
- Implementation:1jehuang_Jcode_Lib_Rs_Run