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 산출물은 커밋에서 제외했다.
131 lines
4.9 KiB
Python
131 lines
4.9 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
from datetime import datetime, timedelta, timezone
|
|
from pathlib import Path
|
|
|
|
from .services.claude_usage_backfill import (
|
|
load_claude_usage_candidates,
|
|
match_claude_usage,
|
|
)
|
|
|
|
|
|
class ClaudeUsageBackfillTest(unittest.TestCase):
|
|
def test_loads_cache_inclusive_usage_without_exposing_text(self) -> None:
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
path = Path(temp_dir) / "session.jsonl"
|
|
path.write_text(
|
|
json.dumps(
|
|
{
|
|
"type": "assistant",
|
|
"uuid": "assistant-1",
|
|
"timestamp": "2026-07-01T00:00:00Z",
|
|
"message": {
|
|
"model": "claude-opus-4-8",
|
|
"content": [{"type": "text", "text": "응답"}],
|
|
"usage": {
|
|
"input_tokens": 11,
|
|
"cache_read_input_tokens": 101,
|
|
"cache_creation_input_tokens": 23,
|
|
"output_tokens": 7,
|
|
},
|
|
},
|
|
},
|
|
ensure_ascii=False,
|
|
)
|
|
+ "\n",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
candidates = load_claude_usage_candidates(Path(temp_dir))
|
|
|
|
self.assertEqual(len(candidates), 1)
|
|
self.assertEqual(candidates[0].tokens_in, 135)
|
|
self.assertEqual(candidates[0].tokens_out, 7)
|
|
self.assertNotIn("응답", candidates[0].source_key)
|
|
|
|
def test_matches_only_one_exact_text_and_time_candidate(self) -> None:
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
root = Path(temp_dir)
|
|
for index, seconds in enumerate((0, 400)):
|
|
(root / f"{index}.jsonl").write_text(
|
|
json.dumps(
|
|
{
|
|
"type": "assistant",
|
|
"uuid": f"assistant-{index}",
|
|
"timestamp": (
|
|
datetime(2026, 7, 1, tzinfo=timezone.utc)
|
|
+ timedelta(seconds=seconds)
|
|
).isoformat(),
|
|
"message": {
|
|
"model": "claude-opus-4-8",
|
|
"content": [{"type": "text", "text": "같은 응답\r\n"}],
|
|
"usage": {
|
|
"input_tokens": 10 + index,
|
|
"output_tokens": 2,
|
|
},
|
|
},
|
|
},
|
|
ensure_ascii=False,
|
|
)
|
|
+ "\n",
|
|
encoding="utf-8",
|
|
)
|
|
candidates = load_claude_usage_candidates(root)
|
|
|
|
report = match_claude_usage(
|
|
[
|
|
{
|
|
"id": "turn-1",
|
|
"created_at": datetime(2026, 7, 1, tzinfo=timezone.utc)
|
|
+ timedelta(seconds=20),
|
|
"text_masked": "같은 응답",
|
|
}
|
|
],
|
|
candidates,
|
|
)
|
|
self.assertEqual(len(report.matches), 1)
|
|
self.assertEqual(report.matches[0].tokens_in, 10)
|
|
self.assertEqual(report.ambiguous_turns, 0)
|
|
|
|
def test_rejects_multiple_candidates_in_the_same_time_window(self) -> None:
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
root = Path(temp_dir)
|
|
path = root / "session.jsonl"
|
|
lines = []
|
|
for index in range(2):
|
|
lines.append(
|
|
json.dumps(
|
|
{
|
|
"type": "assistant",
|
|
"uuid": f"assistant-{index}",
|
|
"timestamp": f"2026-07-01T00:00:0{index}Z",
|
|
"message": {
|
|
"content": [{"type": "text", "text": "반복"}],
|
|
"usage": {"input_tokens": 10, "output_tokens": 1},
|
|
},
|
|
},
|
|
ensure_ascii=False,
|
|
)
|
|
)
|
|
path.write_text("\n".join(lines) + "\n", encoding="utf-8")
|
|
candidates = load_claude_usage_candidates(root)
|
|
|
|
report = match_claude_usage(
|
|
[
|
|
{
|
|
"id": "turn-1",
|
|
"created_at": datetime(2026, 7, 1, 0, 0, 20, tzinfo=timezone.utc),
|
|
"text_masked": "반복",
|
|
}
|
|
],
|
|
candidates,
|
|
)
|
|
self.assertEqual(report.matches, ())
|
|
self.assertEqual(report.ambiguous_turns, 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|