8월 7일까지 워킹트리에만 남아 있던 미커밋 작업을 커밋한다. 여러 사본 폴더(worktree·clone)에 흩어져 있던 중간 스냅샷을 정리하기 전에 원본을 git 이력으로 고정하는 것이 목적이다. - contracts/routes/services: measurement, outcome_trajectory, rupture_repair, deliberate_practice, calibration_transfer, supervision_research, multimodal_alliance, continuous_improvement 계열 신규 모듈과 테스트 - infra/db/init: 07~16 마이그레이션(측정 기반~calibration transfer 실행) - apps/web: 세션 리뷰 카드·관리 화면·E2E 스펙 추가 - docs/ops: G0~G8 라이브 통합·배포·롤백 증거 문서와 evidence JSON/PNG - scripts: smoke·ledger·릴리스 에이전트·NAS 프리뷰 운영 스크립트 engine.public 로그 .bak과 apps/web/test-results 산출물은 커밋에서 제외했다.
181 lines
5.7 KiB
Python
181 lines
5.7 KiB
Python
"""Claude Code JSONL에서 과거 턴의 실제 토큰 사용량을 안전하게 복구한다."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
from collections import defaultdict
|
|
from dataclasses import dataclass
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
from typing import Iterable, Mapping, Sequence
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class ClaudeUsageCandidate:
|
|
text_digest: bytes
|
|
occurred_at: datetime
|
|
tokens_in: int
|
|
tokens_out: int
|
|
model: str
|
|
source_key: str
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class ClaudeUsageMatch:
|
|
turn_id: str
|
|
tokens_in: int
|
|
tokens_out: int
|
|
model: str
|
|
source_key: str
|
|
delta_seconds: float
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class ClaudeUsageMatchReport:
|
|
matches: tuple[ClaudeUsageMatch, ...]
|
|
unmatched_turns: int
|
|
ambiguous_turns: int
|
|
|
|
|
|
def normalize_text(value: object) -> str:
|
|
return str(value or "").replace("\r\n", "\n").strip()
|
|
|
|
|
|
def text_digest(value: object) -> bytes:
|
|
return hashlib.sha256(normalize_text(value).encode("utf-8")).digest()
|
|
|
|
|
|
def _safe_usage_int(usage: Mapping[str, object], key: str) -> int:
|
|
try:
|
|
return max(0, int(usage.get(key) or 0))
|
|
except (TypeError, ValueError):
|
|
return 0
|
|
|
|
|
|
def _parse_timestamp(value: object) -> datetime | None:
|
|
if not value:
|
|
return None
|
|
try:
|
|
return datetime.fromisoformat(str(value).replace("Z", "+00:00"))
|
|
except ValueError:
|
|
return None
|
|
|
|
|
|
def _assistant_text(message: Mapping[str, object]) -> str:
|
|
content = message.get("content")
|
|
if isinstance(content, str):
|
|
return content
|
|
if not isinstance(content, list):
|
|
return ""
|
|
return "".join(
|
|
str(block.get("text") or "")
|
|
for block in content
|
|
if isinstance(block, dict) and block.get("type") == "text"
|
|
)
|
|
|
|
|
|
def load_claude_usage_candidates(root: Path) -> list[ClaudeUsageCandidate]:
|
|
"""본문을 외부로 노출하지 않고 assistant text hash와 usage만 읽는다."""
|
|
|
|
candidates: list[ClaudeUsageCandidate] = []
|
|
for path in root.glob("*.jsonl"):
|
|
try:
|
|
lines = path.open("r", encoding="utf-8", errors="replace")
|
|
except OSError:
|
|
continue
|
|
with lines:
|
|
for line_number, raw in enumerate(lines, start=1):
|
|
try:
|
|
item = json.loads(raw)
|
|
except (json.JSONDecodeError, TypeError):
|
|
continue
|
|
if item.get("type") != "assistant":
|
|
continue
|
|
message = item.get("message")
|
|
if not isinstance(message, dict):
|
|
continue
|
|
usage = message.get("usage")
|
|
if not isinstance(usage, dict):
|
|
continue
|
|
occurred_at = _parse_timestamp(item.get("timestamp"))
|
|
text = normalize_text(_assistant_text(message))
|
|
if occurred_at is None or not text:
|
|
continue
|
|
tokens_in = sum(
|
|
_safe_usage_int(usage, key)
|
|
for key in (
|
|
"input_tokens",
|
|
"cache_read_input_tokens",
|
|
"cache_creation_input_tokens",
|
|
)
|
|
)
|
|
tokens_out = _safe_usage_int(usage, "output_tokens")
|
|
if tokens_in <= 0 and tokens_out <= 0:
|
|
continue
|
|
candidates.append(
|
|
ClaudeUsageCandidate(
|
|
text_digest=text_digest(text),
|
|
occurred_at=occurred_at,
|
|
tokens_in=tokens_in,
|
|
tokens_out=tokens_out,
|
|
model=str(message.get("model") or ""),
|
|
source_key=f"{path.name}:{item.get('uuid') or line_number}",
|
|
)
|
|
)
|
|
return candidates
|
|
|
|
|
|
def match_claude_usage(
|
|
rows: Iterable[Mapping[str, object]],
|
|
candidates: Sequence[ClaudeUsageCandidate],
|
|
*,
|
|
before_seconds: float = 30.0,
|
|
after_seconds: float = 180.0,
|
|
) -> ClaudeUsageMatchReport:
|
|
"""동일 본문 해시와 제한 시간창에 후보가 정확히 하나인 턴만 복구 대상으로 삼는다."""
|
|
|
|
by_digest: dict[bytes, list[ClaudeUsageCandidate]] = defaultdict(list)
|
|
for candidate in candidates:
|
|
by_digest[candidate.text_digest].append(candidate)
|
|
|
|
matches: list[ClaudeUsageMatch] = []
|
|
unmatched = 0
|
|
ambiguous = 0
|
|
used_sources: set[str] = set()
|
|
for row in rows:
|
|
created_at = row.get("created_at")
|
|
if not isinstance(created_at, datetime):
|
|
unmatched += 1
|
|
continue
|
|
options: list[tuple[float, ClaudeUsageCandidate]] = []
|
|
for candidate in by_digest.get(text_digest(row.get("text_masked")), []):
|
|
delta = (created_at - candidate.occurred_at).total_seconds()
|
|
if -before_seconds <= delta <= after_seconds:
|
|
options.append((delta, candidate))
|
|
if not options:
|
|
unmatched += 1
|
|
continue
|
|
if len(options) != 1:
|
|
ambiguous += 1
|
|
continue
|
|
delta, candidate = options[0]
|
|
if candidate.source_key in used_sources:
|
|
ambiguous += 1
|
|
continue
|
|
used_sources.add(candidate.source_key)
|
|
matches.append(
|
|
ClaudeUsageMatch(
|
|
turn_id=str(row.get("id") or ""),
|
|
tokens_in=candidate.tokens_in,
|
|
tokens_out=candidate.tokens_out,
|
|
model=candidate.model,
|
|
source_key=candidate.source_key,
|
|
delta_seconds=delta,
|
|
)
|
|
)
|
|
return ClaudeUsageMatchReport(
|
|
matches=tuple(matches),
|
|
unmatched_turns=unmatched,
|
|
ambiguous_turns=ambiguous,
|
|
)
|