#!/usr/bin/env python3 """Stream Claude and Codex project JSONL into bounded, redacted health evidence. Deep mode scans every previous session for signals. Summary mode scans up to three recent previous sessions from a bounded candidate window. Every file modified inside the live window is excluded so parallel active sessions cannot become evidence. Codex sessions are included when their session metadata resolves inside the target project root. This is a project-scoped audit unless the caller explicitly enables all-project mode. Owns project-scoped, redacted conversation signals and coverage receipts. Structured runtime events define interruptions; cloned history must not multiply corrections. """ from __future__ import annotations import argparse import hashlib import json import os import re import time from collections import Counter, deque from dataclasses import dataclass, field from pathlib import Path from typing import Iterable, Optional MAX_SIGNALS = 20 MAX_FILE_LIST = 10 EXTRACT_HEAD = 30 EXTRACT_TAIL = 120 MAX_SIGNAL_CHARS = 320 MAX_EXTRACT_CHARS = 800 MAX_MESSAGE_CHARS = 4096 MAX_RECORD_CHARS = 1_048_576 MAX_CONTENT_ITEMS = 256 MAX_TOOL_NAME_CHARS = 120 LIVE_WINDOW_SECONDS = 300 SUMMARY_CANDIDATE_LIMIT = 200 MIN_CLONE_CONTEXT_MESSAGES = 4 CONTEXT_RE = re.compile( r"conversation was compressed|context limit|context window|truncat|/compact|" r"context management|token limit|window is full|compaction|" r"会话已压缩|上下文窗口|上下文限制", re.IGNORECASE, ) CORRECTION_RE = re.compile( r"^(?:please[ :]*)?(?:you misunderstood|that(?:'s| is) not what i meant|" r"do not do that again|please don't|don't|do not|stop|next time|" r"use\b.{0,100}\binstead\b|not\b.{0,100}\bbut\b)|" r"^.{0,20}(?:你理解错|理解有误|我说的是|肯定不是|不是.{0,32}而是|" r"还是不对|怎么还有问题|还是有问题|请不要|不要再|不要|" r"别再|别用|下次|改成|改为|少一点|短一点|简单一点|去掉|统一成)", re.IGNORECASE, ) DELIVERY_RE = re.compile( r"为啥不提交|都提交了吗|全部提交了吗|全部\s*push(?:了吗)?|" r"push了吗|提交了吗|why (?:didn't|did not) you (?:commit|push)", re.IGNORECASE, ) PERSISTENCE_RE = re.compile(r"^(?:继续|continue|keep going)[.!。!\s]*$", re.IGNORECASE) PLATFORM_INTERRUPTION_RE = re.compile( r"API Error|No response requested|Request interrupted|response failed|" r"connection (?:closed|reset)|internal server error", re.IGNORECASE, ) JAPANESE_RE = re.compile(r"[ぁ-んァ-ヶ]") CJK_RE = re.compile(r"[\u3400-\u9fffぁ-んァ-ヶ]") JAPANESE_REQUEST_RE = re.compile( r"(?:请\s*)?(?:用|改用|切换到)\s*(?:日语|日文).{0,12}(?:回复|回答|输出)|" r"(?:回复|回答|输出).{0,12}(?:日语|日文)|" r"(?:reply|respond|answer|output).{0,20}(?:in\s+)?japanese|" r"(?:use|switch\s+to)\s+japanese(?:\s+for\s+(?:the\s+)?(?:reply|response|answer|output))?|" r"日本語で.{0,12}(?:返信|回答|出力)", re.IGNORECASE, ) THEME_PATTERNS = { "authorization_delivery": re.compile( r"提交|commit|push|发布|publish|回复|关闭|close|不要提交|先不要提交", re.IGNORECASE, ), "completion_persistence": re.compile( r"全部|所有|彻底|查漏补缺|不要留下次|继续|做完|完成|仔细看看|" r"all of|everything|finish|keep going|continue", re.IGNORECASE, ), "simplicity_scope": re.compile( r"不要增加复杂度|如无必要勿增实体|过度设计|不要过度|臃肿|" r"keep it simple|over.?design|unnecessary", re.IGNORECASE, ), "brevity_style": re.compile( r"啰嗦|短一点|简短|简单一点|字太多|少一点|concise|shorter|too long", re.IGNORECASE, ), "visual_product_judgment": re.compile( r"图片|按钮|focus|边框|布局|界面|样式|高度|宽度|菜单|UI|视觉|" r"image|button|border|layout|menu", re.IGNORECASE, ), "localization_wording": re.compile( r"中文|英文|日语|日文|文案|翻译|单词|措辞|叫法|wording|translation", re.IGNORECASE, ), "release_version_truth": re.compile( r"版本|发布|release|nightly|preview|stable|appcast|已经上线|已经发布", re.IGNORECASE, ), "verification_evidence": re.compile( r"仔细|分析|判断|验证|测试|复核|看看所有|证据|verify|test|audit|evidence", re.IGNORECASE, ), } INJECTED_PREFIXES = ( "-{0,2}(?P[\x22\x27]?)(?:[A-Za-z0-9]+[_-])*" r"(?:secret[_-]access[_-]key|private[_-]?key|api[_-]?key|" r"authorization|password|passwd|pwd|token|secret)(?P=quote))" r"(?![A-Za-z0-9_-])" r"(?P\s*[:=]\s*)" r"(?:Bearer\s+|Basic\s+)?(?:\"(?:\\[^\r\n]|[^\"\\\r\n])*(?:\"|\\?(?=\r?\n|\Z))|" r"\x27(?:\\[^\r\n]|[^\x27\\\r\n])*(?:\x27|\\?(?=\r?\n|\Z))|[^\s,;]+)", re.IGNORECASE, ) PRIVATE_KEY_RE = re.compile( r"-----BEGIN (?P