Claude Code chats survive server restarts in a detachable host
A pipelines-claude-host process owns each claude -p CLI and its pipes, spools its stream-json output with offsets and serves a private Unix socket. The server detaches on shutdown and reattaches on start, replaying the spool to rebuild turns, approvals, questions, background tasks and the live model. Explicit close, idle release and CLI exit stop the host; a crashed host falls back to --resume with a notice.
This commit is contained in:
+49
@@ -72,6 +72,15 @@ def user_message(text):
|
||||
send({"type": "system", "subtype": "init", "session_id": state["session"],
|
||||
"model": "claude-test", "permissionMode": "default", "cwd": os.getcwd()})
|
||||
message_id = "msg_fake_%d" % state["turn"]
|
||||
if "gate" in text:
|
||||
gated(message_id)
|
||||
return
|
||||
if "background" in text:
|
||||
background(message_id)
|
||||
return
|
||||
if "notify" in text:
|
||||
send({"type": "system", "subtype": "task_notification", "task_id": "b1",
|
||||
"tool_use_id": "toolu_bg_fake", "status": "completed", "summary": "Background command completed (exit code 0)"})
|
||||
send({"type": "stream_event", "event": {"type": "message_start", "message": {"id": message_id}}})
|
||||
send({"type": "stream_event", "event": {"type": "content_block_start", "index": 0, "content_block": {"type": "text"}}})
|
||||
for delta in ("Hello ", "world"):
|
||||
@@ -125,6 +134,46 @@ def user_message(text):
|
||||
finish(message_id)
|
||||
|
||||
|
||||
def wait_for(path):
|
||||
import time
|
||||
while not os.path.exists(path):
|
||||
time.sleep(0.02)
|
||||
|
||||
|
||||
def gated(message_id):
|
||||
"""A turn that streams, then waits for FAKE_CLAUDE_GATE to exist before it
|
||||
streams the rest and finishes: the test restarts the server in between."""
|
||||
gate = os.environ["FAKE_CLAUDE_GATE"]
|
||||
send({"type": "stream_event", "event": {"type": "message_start", "message": {"id": message_id}}})
|
||||
send({"type": "stream_event", "event": {"type": "content_block_start", "index": 0, "content_block": {"type": "text"}}})
|
||||
send({"type": "stream_event", "event": {"type": "content_block_delta", "index": 0,
|
||||
"delta": {"type": "text_delta", "text": "Working"}}})
|
||||
wait_for(gate)
|
||||
send({"type": "stream_event", "event": {"type": "content_block_delta", "index": 0,
|
||||
"delta": {"type": "text_delta", "text": " after the restart"}}})
|
||||
send({"type": "stream_event", "event": {"type": "content_block_stop", "index": 0}})
|
||||
send({"type": "stream_event", "event": {"type": "message_stop"}})
|
||||
text = "Working after the restart"
|
||||
append({"type": "assistant", "uuid": "asst-%d" % state["turn"], "timestamp": stamp(),
|
||||
"message": {"id": message_id, "role": "assistant", "model": "claude-test",
|
||||
"content": [{"type": "text", "text": text}]}})
|
||||
send({"type": "assistant", "message": {"id": message_id, "role": "assistant", "model": "claude-test",
|
||||
"content": [{"type": "text", "text": text}]}})
|
||||
finish(message_id)
|
||||
|
||||
|
||||
def background(message_id):
|
||||
"""Starts a background Bash command that a later "notify" turn finishes."""
|
||||
tool_id = "toolu_bg_fake"
|
||||
use = {"type": "tool_use", "id": tool_id, "name": "Bash",
|
||||
"input": {"command": "sleep 600", "run_in_background": True}}
|
||||
send({"type": "assistant", "message": {"id": message_id, "role": "assistant", "model": "claude-test", "content": [use]}})
|
||||
block = {"type": "tool_result", "tool_use_id": tool_id,
|
||||
"content": "Command running in background with ID: b1. Output is being written to: /tmp/b1.output"}
|
||||
send({"type": "user", "message": {"role": "user", "content": [block]}})
|
||||
finish(message_id)
|
||||
|
||||
|
||||
def finish(message_id):
|
||||
# Shape of SDKRateLimitEvent in Claude Code 2.1.316 (synthetic numbers).
|
||||
if os.environ.get("FAKE_CLAUDE_RATE_LIMIT"):
|
||||
|
||||
Reference in New Issue
Block a user