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

JSONL ground truth, derived mirror

~16 min · jsonl, persistence, rebuild

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

왜 single source of truth 중요

cwkPippa가 프로덕션에서 배움 — 매 이벤트(프롬프트, delta, tool use, tool result, final usage)가 conversation_id로 키된 JSONL에 land, 저장 시 암호화, append-only. SQLite와 ChromaDB가 derived mirror — 애플리케이션의 fast 인덱스, JSONL에서 rebuildable. 어떤 inconsistency 복구든 patch-and-reconcile X, purge-and-replay.

Write before show

신뢰성 invariant — user에 렌더 또는 API surface 전에 각 delta를 JSONL에 persist. Crashed stream도 모든 visible 토큰이 디스크에. Line-buffered write와 explicit flush 페어 — write 중간 SIGTERM이 마지막 라인 안 잃게.

Read에 healing

cwkPippa가 매 대화 GET에 _heal_incomplete_turns run — delta 이벤트에서 abort된 턴 rebuild, 순서 보존하게 timestamp normalize. Frontend가 절대 broken 턴 안 봐. Healing이 JSONL canonical 둔 비용 — derived 상태가 drift 가능, read path가 repair해야.

원칙: Single source of truth, durable, append-only. 인덱스·캐시는 derived. 복구는 rebuild, patch X.

Code

Write before show·python
import json, pathlib

async def stream_with_persistence(conv_id: str, stream):
    p = pathlib.Path(f"/srv/sessions/{conv_id}.jsonl").open("a", buffering=1)
    try:
        async for event in stream:
            # Persist FIRST.
            p.write(json.dumps({"event": event.dict()}) + "\n")
            p.flush()
            # 그다음 user에 렌더.
            yield event
    finally:
        p.close()
Inconsistency에 JSONL에서 SQLite rebuild·python
import json

def rebuild_session(conn, conv_id: str, jsonl_path: str):
    # 이 세션의 derived 상태 퍼지.
    conn.execute("DELETE FROM messages WHERE conversation_id = ?", (conv_id,))
    # JSONL에서 replay.
    for line in open(jsonl_path):
        ev = json.loads(line)
        if ev["event"]["type"] == "message_complete":
            conn.execute(
                "INSERT INTO messages (conversation_id, role, content, created_at) VALUES (?, ?, ?, ?)",
                (conv_id, ev["event"]["role"], ev["event"]["text"], ev["event"]["created_at"]),
            )
    conn.commit()

External links

Exercise

Chat-style 기능 하나에 대해 매 이벤트를 렌더 전 JSONL로 route. 세션의 DB row를 JSONL에서 rebuild하는 도구 추가. 알려진 세션 대비 rebuild run, 결과가 live DB 매치 confirm.
Hint
Rebuild가 live와 다르면 JSONL에 버그(또는 write 놓침). Writer 먼저 fix; rebuild가 integrity 체크.

Progress

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

댓글 0

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

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