C.W.K.
Stream
Lesson 07 of 08 · published

세션 · resume · JSONL truth

~16 min · sessions, resume, jsonl, persistence

Level 0Observer
0 XP0/64 lessons0/13 achievements
0/150 XP to next level150 XP to go0% complete

세션이 SDK의 메모리

Agent SDK가 세션 id 할당하고 대화 상태를 internally persist. ClaudeSDKClient면 세션이 클라이언트만큼 살아; query()면 single-use. Id로 세션 resume해서 멈춘 데서 계속 가능 — SDK가 internal 상태 reload하고 새 프롬프트 적용.

Durable 기록으로서의 JSONL

SDK가 이벤트를 JSONL 파일로 stream 가능. 각 라인이 typed 이벤트 — 프롬프트, 응답 chunk, tool use, tool result. cwkPippa가 이걸 ground truth로 — SQLite와 ChromaDB가 JSONL에서 rebuild 가능한 derived mirror. 복구는 patch-and-reconcile X, purge-and-replay.

프로세스 사이 Resume

Worker 프로세스 재시작하면 같은 세션 id로 새 ClaudeSDKClient spin up하고 SDK가 recover. 이게 cwkPippa의 heartbeat job이 conversational 상태 안 잃고 서버 재시작 살아남는 거 enable.

원칙: JSONL을 source of truth로 다뤄. 다른 거(DB row, indices)는 rebuildable해야 하는 cache.

Code

이벤트를 JSONL로 stream·python
import json, pathlib
from claude_agent_sdk import ClaudeSDKClient, ClaudeAgentOptions

async def run_with_jsonl(conv_id: str, prompt: str):
    log_path = pathlib.Path(f"/var/lib/myagent/sessions/{conv_id}.jsonl")
    log_path.parent.mkdir(parents=True, exist_ok=True)
    log = log_path.open("a", buffering=1)  # line-buffered

    client = ClaudeSDKClient(options=ClaudeAgentOptions(cwd="/srv"))
    await client.connect()
    try:
        async for event in client.send_message(prompt):
            log.write(json.dumps({"event": event.dict()}) + "\n")
            log.flush()  # write before show
            forward_to_user(event)
    finally:
        await client.disconnect()
        log.close()
Id로 세션 resume·python
# session_id는 SDK가 할당한 거(또는 connect 시 너가 할당).
options = ClaudeAgentOptions(
    cwd="/srv",
    resume=session_id,  # SDK가 internal 상태 reload
)
client = ClaudeSDKClient(options=options)
await client.connect()
async for event in client.send_message("Continue where we left off."):
    print(event)

External links

Exercise

프로젝트의 Agent SDK 호출 하나에 JSONL streaming 추가. 프로세스를 stream 중간 crash. 같은 세션 id로 재시작, 대화 계속 가능 confirm. Crashed 턴의 모든 delta가 JSONL에 있는지 verify.
Hint
Downstream store와 JSONL이 disagree면 JSONL 신뢰 — 퍼지하고 replay. 그게 규율.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.