Peach: Local-first video metadata scraper and media manager.
feat(diagnostics): 随技术诊断聚合上传故障次数
longmeidaobf97481bc299
diff --git a/docs/DIAGNOSTICS_AUDIT.md b/docs/DIAGNOSTICS_AUDIT.mdindex ea7ac0e49..e0e356906 100644--- a/docs/DIAGNOSTICS_AUDIT.md+++ b/docs/DIAGNOSTICS_AUDIT.md@@ -35,9 +35,13 @@| 媒体工具 | 退出码,以及确定匹配的 AV1 解码器缺失、非法媒体、权限、磁盘空间或未知解码器类别;stderr 与媒体名称留在本机 || 浏览器错误 | 仅发送 Peach 正式脚本的行列位置;没有可定位堆栈的错误与扩展脚本不发送 |-正常取消、来源未命中/暂不可用、HTTP 4xx、互斥、普通 warning 和无堆栈的泛化日志不发送。后台任务的纯文本失败回执仍保留在本机;有异常链的意外失败可发送。每机每天最多 20 条错误,同类指纹每天一次,托盘和子服务共享额度文件;发送失败不补传历史。SDK 内部日志不进入远程钩子。+正常取消、来源未命中/暂不可用、HTTP 4xx、互斥、普通 warning 和无堆栈的泛化日志不发送,也不计入故障次数。后台任务的纯文本失败回执仍保留在本机;有异常链的意外失败可发送。SDK 内部日志不进入远程钩子。-两项均通过公开 DSN 发送到 Sentry 托管服务,接收服务会接触连接 IP。客户端不发送 IP 字段、用户身份、设备标识、观看记录、媒体信息、凭据、完整日志或诊断 ZIP。发送队列不落盘,后台发送前复查本机同意版本;关闭或重新选择会让排队旧事件失效,已在途或已接收的请求无法撤回。本机 ZIP 导出独立可用。+同类故障首次在额度内立即上传,重复次数每小时或正常退出时合并提交。`occurrence_count` 是本批增量次数;例如先上传 1 次,随后又发生 9 次,下一批上传 9,合计 10。`occurrence_day` 是实际发生的 UTC 日期,`diagnostic_key` 由脱敏技术字段计算,包含版本但排除每次 HTTP 错误编号;相同代码故障可跨机器对应,不是安装标识。Sentry 的事件数代表上传批次,频率需按统计日期汇总 `occurrence_count`,不能把最新一批当累计总数。普通标签不提供 Sentry 原生的数值求和;汇总需读取事件字段后计算,不能把原生事件趋势图当成真实触发趋势。++每机每天最多接收 20 类故障的本机计数,远程每天最多提交 20 批,重复补报与首次诊断共用批次上限。托盘与子服务使用同一文件锁及聚合文件 `state/remote-reporting-frequency.json`,保留至多 7 个 UTC 日期的技术样本、累计数和已提交数;不读取历史日志。额度不足的待提交次数可在后续启动或定时检查时继续提交,超过保留期失效。SDK 接受入队后更新已提交数;连接失败、服务端丢弃、强制退出、锁争用或超过种类及保留上限均可能造成缺口,因此这是尽力交付的已接收次数,不是完整运行审计。SDK 尚未接受的次数保留在聚合文件中。++两项均通过公开 DSN 发送到 Sentry 托管服务,接收服务会接触连接 IP。客户端不发送 IP 字段、用户身份、设备标识、观看记录、媒体信息、凭据、完整日志或诊断 ZIP。SDK 发送队列不落盘,本机聚合文件仅存技术投影和次数;后台发送前复查本机同意版本。关闭或重新选择会清除未提交聚合,并让排队旧事件失效;已在途或已接收的请求无法撤回。本机 ZIP 导出独立可用。接收端由维护者配置:源码与 Docker 使用 `PEACH_SENTRY_DSN`;Windows 发行构建从同名环境变量写入包内 `sentry.json`,GitHub Release 使用仓库变量 `PEACH_SENTRY_DSN`。只接受 Sentry 的公开 HTTPS DSN(含 US / DE 区域),不需要 API token。没有 DSN 时不会创建 SDK 客户端。SDK 自动框架集成、日志、性能追踪和指标均关闭;版本统计作为普通事件计入 Sentry 错误事件额度。账号套餐和总量以 [Sentry 订阅页](https://sentry.io/settings/) 为准。@@ -108,12 +112,12 @@ ZIP 不包含 ledger、配置文件、私钥、登录 Cookie 或媒体文件。| 影响面 | 适用性 || --- | --- |-| 数据层 | 本机独立同意与限额文件;研究只读账本,复用派生偏好;没有迁移或真实 ledger 写入 |+| 数据层 | 本机独立同意、限额与故障频率聚合文件;研究只读账本,复用派生偏好;没有迁移或真实 ledger 写入 || API/契约 | 诊断选择、研究预览与同意;浏览器接收可选异常类型用于取消识别;本机、鉴权、同源限制 || 页面 | 配置页「应用与维护」、首次设置;Windows 安装器默认勾选推荐的诊断选项;其他馆藏页面不适用 || 反向状态 | 诊断与研究分别关闭;关闭使排队载荷失效,研究重新开启须预览并同意 || 视口 | 桌面和 390×844 手机验证明细折行、内部滚动、同意与关闭;无横向溢出 |-| 测试 | 诊断与研究选择、SDK 内存传输、脱敏与权限;实际 socket 和服务端清理、收尾失败、预期状态、解析错误、浏览器取消与构建参数堆栈 |+| 测试 | 诊断与研究选择、SDK 内存传输、脱敏与权限;实际 socket 和服务端清理、收尾失败、预期状态、解析错误、浏览器取消与构建参数堆栈;跨进程次数、重启与退出补报、日期及限额、关闭清除与 SDK 拒收 || 文档 | 中英文 README、STATUS、REUSE、待办、本记录与 ADR-0109 || 智能体入口 | 没有新增规则、技能或入口 || 生产 | 本机源码运行态按托盘入口重启并严格校验 HTTPS;外部测试包发布独立验收 |diff --git a/docs/REUSE.md b/docs/REUSE.mdindex 4e3f9b88a..112f64e92 100644--- a/docs/REUSE.md+++ b/docs/REUSE.md@@ -148,7 +148,9 @@ Peach 播放器皮肤直接装饰 Video.js 10 的 light DOM:右侧控制组用启动取证复用 Python 3.14 标准库的 `sys.excepthook`、`threading.excepthook`、`sys.unraisablehook`、`logging.Handler`、`faulthandler` 和 `zipfile`(PSF 许可,无额外安装体积)。`error_reporting` 在配置与第三方依赖之前加载,`support_bundle` 仅收集选定日志和系统字段;Peach 保留脱敏、备用目录、本机权限与报告内容策略。已有诊断报告用于页面和运行期导出。临时目录与独立子进程覆盖无控制台、导入失败、线程异常、备用目录、配置损坏和脱敏。-可选远程诊断复用官方 [sentry-sdk 2.72.0](https://github.com/getsentry/sentry-python/tree/2.72.0)(MIT,维护中,支持 Python 3.14);固定版源码通过 `opensrc path pypi:sentry-sdk` 核对。SDK 负责后台 HTTP、TLS、Sentry 信封和服务端限流;Peach 负责本机选择、技术字段投影、同类错误去重与每机每日上限。版本统计与错误诊断共用默认勾选的开关,关闭时不构造 SDK;SDK 全局自动集成、追踪、回放、日志、会话与指标采集均关闭。`HttpTransport._send_envelope` 的窄覆盖在真正发送前复查本机选择版本并移除本机传输元数据;它是固定版本的内部接口,升级必须通过真实 SDK 信封回归。无真实外发的内存接收端检查最终字节、停用后队列失效与断网不影响本机取证。新增依赖仅 `sentry-sdk`,复用已声明的 urllib3 / certifi;体积以发行构建为准。未采用自写 Sentry 协议或 Workers 收集后台,避免另行维护传输、去重和查看界面。没有真实 ledger 写入。+可选远程诊断复用官方 [sentry-sdk 2.72.0](https://github.com/getsentry/sentry-python/tree/2.72.0)(MIT,维护中,支持 Python 3.14);固定版源码通过 `opensrc path pypi:sentry-sdk` 核对。SDK 负责后台 HTTP、TLS、Sentry 信封和服务端限流;Peach 负责本机选择、技术字段投影、同类错误聚合与每机每日上限。版本统计与错误诊断共用默认勾选的开关,关闭时不构造 SDK;SDK 全局自动集成、追踪、回放、日志、会话与指标采集均关闭。`HttpTransport._send_envelope` 的窄覆盖在真正发送前复查本机选择版本并移除本机传输元数据;它是固定版本的内部接口,升级必须通过真实 SDK 信封回归。无真实外发的内存接收端检查最终字节、停用后队列失效与断网不影响本机取证。新增依赖仅 `sentry-sdk`,复用已声明的 urllib3 / certifi;体积以发行构建为准。未采用自写 Sentry 协议或 Workers 收集后台,避免另行维护传输、去重和查看界面。没有真实 ledger 写入。++故障频率复用已固定的 `filelock`、`fsutil.atomic_write_text` 和 SDK 事件标签,保留 Peach 的同意版本、技术投影、跨进程计数与每日批次策略。`diagnostic_frequency` 保存有界技术样本及待提交增量,首次、每小时和正常退出通过同一 SDK 提交;关闭会清除聚合。Sentry 默认事件数统计批次,不会将自定义次数标签自动求和;汇总按事件的 `occurrence_day` 与 `occurrence_count` 计算。未开启 SDK metrics 或新增收集服务:当前需求是随技术诊断附带次数,独立指标通道会增加同意边界、额度与交付口径。计数只覆盖已筛选技术故障,发送及保留限制见 [诊断核验](DIAGNOSTICS_AUDIT.md)。没有新增依赖或真实 ledger 写入。Windows 连接清理复用 [CPython 3.14.7 的 Proactor 对象协议](https://github.com/python/cpython/blob/v3.14.7/Lib/asyncio/proactor_events.py)(PSF 许可,无新增依赖)。精确识别纯标准库清理回调中的 `WinError 10054`,只补完 socket 关闭、服务端解除登记和关闭状态,不重复通知协议,不全局修改标准库类或替换事件循环。它使用 `_sock`、`_server._detach` 与 `_called_connection_lost` 私有接口,升级 Python 时须核验。真实 socket 和 `asyncio.Server` 配合合成故障验证资源释放;收尾自身失败保留原始异常链并上报。33 次回环实验未复现原始竞争时序,不据此认定客户端来源。协议类别只写本机日志;来源、任务、媒体和浏览器的远程筛选复用现有领域状态,见 [诊断核验](DIAGNOSTICS_AUDIT.md#远程错误筛选核验)。diff --git a/src/peach/diagnostic_frequency.py b/src/peach/diagnostic_frequency.pynew file mode 100644index 000000000..5a36f7c76--- /dev/null+++ b/src/peach/diagnostic_frequency.py@@ -0,0 +1,110 @@+"""跨进程聚合已筛选的技术诊断;不读取日志或账本。"""+from __future__ import annotations++import hashlib+import json+from datetime import datetime, timedelta, timezone++from filelock import FileLock++from .fsutil import atomic_write_text++INTERVAL = 3600+DAILY_LIMIT = 20+RETENTION_DAYS = 7+++def _now():+ return datetime.now(timezone.utc)+++def _path(config):+ return config.directory("state") / "remote-reporting-frequency.json"+++def _load(path, revision, now):+ try:+ state = json.loads(path.read_text(encoding="utf-8"))+ except (OSError, ValueError, UnicodeError):+ state = {}+ if not isinstance(state, dict) or state.get("revision") != revision:+ state = {"revision": revision, "records": {}}+ day = now.date().isoformat()+ if state.get("day") != day:+ state.update(day=day, batches=0)+ cutoff = (now.date() - timedelta(days=RETENTION_DAYS - 1)).isoformat()+ records = state.get("records", {})+ if not isinstance(records, dict):+ records = {}+ state["records"] = {key: row for key, row in records.items()+ if isinstance(key, str) and len(key) == 75 and isinstance(row, dict)+ and isinstance(row.get("day"), str) and cutoff <= row["day"] <= day+ and isinstance(row.get("event"), dict)+ and type(row.get("total")) is int and type(row.get("submitted")) is int+ and 0 <= row["submitted"] <= row["total"]+ and type(row.get("last_sent")) in (int, float)}+ if type(state.get("batches")) is not int or state["batches"] < 0:+ state["batches"] = 0+ return state+++def _emit(state, row, key, now, emit, *, force=False, event_id=None):+ pending = row["total"] - row["submitted"]+ if pending <= 0 or state["batches"] >= DAILY_LIMIT:+ return+ if not force and row["submitted"] and now.timestamp() - row["last_sent"] < INTERVAL:+ return+ event = {**row["event"], "tags": {**row["event"].get("tags", {}),+ "diagnostic_key": key.split(":", 1)[1], "occurrence_day": row["day"],+ "occurrence_count": str(pending)}}+ if event_id:+ event["event_id"] = event_id+ try:+ accepted = emit(event)+ except Exception:+ accepted = False+ if accepted:+ row.update(submitted=row["total"], last_sent=now.timestamp())+ state["batches"] += 1+++def record(config, revision, event, emit):+ """记录一次筛选后的故障;首次立即提交,重复故障合并提交增量次数。"""+ path = _path(config)+ path.parent.mkdir(parents=True, exist_ok=True)+ with FileLock(str(path) + ".lock", timeout=.1):+ now = _now()+ state = _load(path, revision, now)+ sample = {key: value for key, value in event.items() if key != "event_id"}+ fingerprint = hashlib.sha256(json.dumps(sample, sort_keys=True).encode()).hexdigest()+ day = now.date().isoformat()+ key = day + ":" + fingerprint+ records = state["records"]+ if key not in records:+ if sum(row["day"] == day for row in records.values()) >= DAILY_LIMIT:+ return+ records[key] = {"day": day, "event": sample, "total": 0, "submitted": 0, "last_sent": 0}+ row = records[key]+ row["total"] += 1+ _emit(state, row, key, now, emit, event_id=event.get("event_id") if row["total"] == 1 else None)+ atomic_write_text(path, json.dumps(state))+++def flush(config, revision, emit, *, force=False):+ """按发生日期提交未提交次数,退出时可提前提交,仍遵守每日批次上限。"""+ path = _path(config)+ if not path.exists():+ return+ with FileLock(str(path) + ".lock", timeout=.1):+ now = _now()+ state = _load(path, revision, now)+ for key, row in state["records"].items():+ _emit(state, row, key, now, emit, force=force)+ atomic_write_text(path, json.dumps(state))+++def clear(config):+ path = _path(config)+ path.parent.mkdir(parents=True, exist_ok=True)+ with FileLock(str(path) + ".lock", timeout=.1):+ path.unlink(missing_ok=True)diff --git a/src/peach/remote_reporting.py b/src/peach/remote_reporting.pyindex 0078a9857..e750bfa5e 100644--- a/src/peach/remote_reporting.py+++ b/src/peach/remote_reporting.py@@ -19,13 +19,15 @@ from . import __version__FIELDS = {"usage": "Peach 版本、操作系统类型;每天最多一次,不含安装标识。",- "errors": "Peach 版本、系统类型、Python 版本、CPU 架构、故障环节、异常链、Peach 代码文件名/函数名/行号、结构化错误码、媒体工具故障类别、HTTP 方法/路由模板/关联错误编号;每天最多 20 条,同类错误每天一次。",+ "errors": "Peach 版本、系统类型、Python 版本、CPU 架构、故障环节、异常链、Peach 代码文件名/函数名/行号、结构化错误码、媒体工具故障类别、HTTP 方法/路由模板/关联错误编号、同类故障次数与统计日期;首次立即上传,重复次数每小时或退出时合并,每天最多 20 批。",}NOTICE = "发送至 Sentry。不上报错误原文、完整日志、诊断 ZIP、媒体名称、路径、网址、观看记录或凭据。接收服务会接触连接 IP;关闭后停止新发送,已发送的信息无法撤回。"_lock = threading.RLock()_client = None_client_dsn = ""_disabled = False+_frequency_timer = None+_frequency_configs = {}MEDIA_STAGES = frozenset({"frame-capture", "frame-tile", "media-probe", "preview", "library-preview"})LOCAL_EXCEPTION_TYPES = frozenset({("asyncio.exceptions", "CancelledError"), ("concurrent.futures._base", "CancelledError"),@@ -109,6 +111,8 @@ def save(body, config=None):config = _config(config)payload["revision"] = uuid.uuid4().hexatomic_write_text(config.directory("state") / "remote-reporting.json", json.dumps(payload))+ from . import diagnostic_frequency+ diagnostic_frequency.clear(config)return snapshot(config)@@ -177,9 +181,55 @@ def _new_client(target, *, envelope_gate=None):before_send=final_event)-def send(kind, event, config=None):- """发送失败不影响应用;关闭时不初始化 SDK,也不生成补传文件。"""+def _submit(kind, event, config, current, target):global _client, _client_dsn+ with _lock:+ if _client is None or _client_dsn != target:+ if _client is not None:+ _client.close(timeout=0)+ _client, _client_dsn = _new_client(target), target+ return _client.capture_event({**event, "platform": "python",+ "extra": {"consent": str(config.directory("state") / "remote-reporting.json"),+ "kind": kind, "revision": current["revision"]}})+++def _schedule_frequency(config):+ global _frequency_timer+ with _lock:+ _frequency_configs[str(config.directory("state"))] = config+ if _frequency_timer is None:+ _frequency_timer = threading.Timer(60, _frequency_tick)+ _frequency_timer.daemon = True+ _frequency_timer.start()+++def _frequency_tick():+ global _frequency_timer+ with _lock:+ _frequency_timer = None+ configs = list(_frequency_configs.values())+ for config in configs:+ flush_counts(config)+ with _lock:+ if _frequency_configs and not _disabled:+ _schedule_frequency(next(iter(_frequency_configs.values())))+++def flush_counts(config=None, *, force=False):+ try:+ from . import diagnostic_frequency+ config = _config(config)+ current, target = consent(config), dsn()+ if _disabled or not current["errors"] or not target:+ return+ diagnostic_frequency.flush(config, current["revision"],+ lambda event: _submit("errors", event, config, current, target), force=force)+ except Exception:+ pass+++def send(kind, event, config=None):+ """发送失败不影响应用;技术诊断按同意版本聚合,关闭时不初始化 SDK。"""if _disabled:returntry:@@ -188,16 +238,14 @@ def send(kind, event, config=None):target = dsn()if kind not in FIELDS or not current[kind] or not target:return- if not _reserve(config, kind, {**event, "release": __version__}):- return- with _lock:- if _client is None or _client_dsn != target:- if _client is not None:- _client.close(timeout=0)- _client, _client_dsn = _new_client(target), target- _client.capture_event({**event, "release": __version__, "platform": "python",- "extra": {"consent": str(config.directory("state") / "remote-reporting.json"),- "kind": kind, "revision": current["revision"]}})+ event = {**event, "release": __version__}+ if kind == "errors":+ from . import diagnostic_frequency+ diagnostic_frequency.record(config, current["revision"], event,+ lambda batch: _submit(kind, batch, config, current, target))+ _schedule_frequency(config)+ elif _reserve(config, kind, event):+ _submit(kind, event, config, current, target)except Exception:# 不能把遥测自身故障写回错误钩子而递归发送。pass@@ -205,6 +253,12 @@ def send(kind, event, config=None):def usage(config=None):send("usage", {"message": "Peach started", "level": "info", "tags": {"os": platform.system()}}, config)+ flush_counts(config)+ try:+ if not _disabled and consent(config)["errors"] and dsn():+ _schedule_frequency(_config(config))+ except Exception:+ passdef _http_status(error):@@ -374,8 +428,12 @@ def disable_for_process():def close():- global _client, _client_dsn+ global _client, _client_dsn, _frequency_timerwith _lock:+ if _frequency_timer is not None:+ _frequency_timer.cancel()+ _frequency_timer = None+ _frequency_configs.clear()if _client is not None:_client.close(timeout=0)_client, _client_dsn = None, ""@@ -383,6 +441,10 @@ def close():def _shutdown():try:+ with _lock:+ configs = list(_frequency_configs.values())+ for config in configs:+ flush_counts(config, force=True)with _lock:if _client is not None:_client.flush(timeout=1)diff --git a/tests/test_remote_reporting.py b/tests/test_remote_reporting.pyindex 3d4b84d75..7ba4f90fc 100644--- a/tests/test_remote_reporting.py+++ b/tests/test_remote_reporting.py@@ -6,6 +6,7 @@ import subprocessimport sysimport tempfileimport unittest+from datetime import datetime, timedelta, timezonefrom pathlib import Pathfrom unittest import mock@@ -112,8 +113,154 @@ class RemoteReportingTests(unittest.TestCase):reporting.send("errors", event)reporting.send("errors", event)self.assertEqual(client.capture_event.call_count, 21)- payload = json.loads((self.config.directory("state") / "remote-reporting-budget.json").read_text())- self.assertEqual(len(payload["errors"]), 20)+ payload = json.loads((self.config.directory("state") / "remote-reporting-frequency.json").read_text())+ self.assertEqual(len(payload["records"]), 20)+ self.assertTrue(all(row['total'] == 2 for row in payload['records'].values()))++ def test_frequency_batches_send_disjoint_counts_and_keep_group_identity(self):+ from peach import diagnostic_frequency as frequency+ reporting.save({"usage": True, "errors": True})+ client = mock.Mock()+ now = datetime(2026, 10, 18, 12, tzinfo=timezone.utc)+ with mock.patch.object(reporting, '_new_client', return_value=client), \+ mock.patch.object(frequency, '_now', return_value=now) as clock:+ for index in range(5):+ reporting.capture('unhandled-main', ValueError('private title'), error_id=str(index))+ self.assertEqual(client.capture_event.call_count, 1)+ first = client.capture_event.call_args.args[0]+ self.assertEqual(first['tags']['occurrence_count'], '1')+ clock.return_value = now + timedelta(hours=1)+ reporting.flush_counts()+ second = client.capture_event.call_args.args[0]+ self.assertEqual(second['tags']['occurrence_count'], '4')+ self.assertEqual(second['tags']['diagnostic_key'], first['tags']['diagnostic_key'])+ self.assertEqual(second['tags']['occurrence_day'], '2026-10-18')+ reporting.flush_counts(force=True)+ self.assertEqual(client.capture_event.call_count, 2)+ payload = (self.config.directory('state') / 'remote-reporting-frequency.json').read_text()+ self.assertNotIn('private title', payload)+ self.assertNotIn('installation', payload)++ def test_frequency_survives_client_restart_and_http_ids_do_not_split_groups(self):+ reporting.save({"usage": True, "errors": True})+ client = mock.Mock()+ with mock.patch.object(reporting, '_new_client', return_value=client):+ for error_id in ('a' * 32, 'b' * 32, 'c' * 32):+ reporting.capture('http-request', RuntimeError('private'), status_code=500, error_id=error_id)+ first = client.capture_event.call_args.args[0]+ self.assertEqual(first['event_id'], 'a' * 32)+ reporting.close()+ reporting.flush_counts(force=True)+ second = client.capture_event.call_args.args[0]+ self.assertNotIn('event_id', second)+ self.assertEqual(second['tags']['occurrence_count'], '2')+ self.assertEqual(first['tags']['diagnostic_key'], second['tags']['diagnostic_key'])+ self.assertEqual(client.capture_event.call_count, 2)++ def test_frequency_pending_counts_expire_with_consent_and_expected_states_are_excluded(self):+ import asyncio+ reporting.save({"usage": True, "errors": True})+ client = mock.Mock()+ with mock.patch.object(reporting, '_new_client', return_value=client):+ reporting.capture('background-job', asyncio.CancelledError())+ reporting.capture('unhandled-main', ValueError('private'))+ reporting.capture('unhandled-main', ValueError('private'))+ reporting.save({"usage": False, "errors": False})+ self.assertFalse((self.config.directory('state') / 'remote-reporting-frequency.json').exists())+ reporting.flush_counts(force=True)+ reporting.save({"usage": True, "errors": True})+ reporting.flush_counts(force=True)+ self.assertEqual(client.capture_event.call_count, 1)+ reporting.capture('unhandled-main', ValueError('private'))+ self.assertEqual(client.capture_event.call_args.args[0]['tags']['occurrence_count'], '1')++ def test_frequency_daily_limit_defers_counts_and_preserves_occurrence_day(self):+ from peach import diagnostic_frequency as frequency+ reporting.save({"usage": True, "errors": True})+ client = mock.Mock()+ now = datetime(2026, 10, 18, 12, tzinfo=timezone.utc)+ with mock.patch.object(reporting, '_new_client', return_value=client), \+ mock.patch.object(frequency, '_now', return_value=now) as clock:+ for index in range(20):+ reporting.send('errors', {'message': str(index)})+ reporting.send('errors', {'message': '0'})+ reporting.flush_counts(force=True)+ self.assertEqual(client.capture_event.call_count, 20)+ clock.return_value = now + timedelta(days=1)+ reporting.flush_counts(force=True)+ self.assertEqual(client.capture_event.call_count, 21)+ self.assertEqual(client.capture_event.call_args.args[0]['tags']['occurrence_count'], '1')+ self.assertEqual(client.capture_event.call_args.args[0]['tags']['occurrence_day'], '2026-10-18')++ def test_frequency_sdk_rejection_keeps_pending_counts_for_later_batch(self):+ reporting.save({"usage": True, "errors": True})+ client = mock.Mock()+ client.capture_event.return_value = None+ with mock.patch.object(reporting, '_new_client', return_value=client):+ reporting.capture('unhandled-main', ValueError('private'))+ client.capture_event.return_value = 'accepted'+ reporting.flush_counts(force=True)+ self.assertEqual(client.capture_event.call_args.args[0]['tags']['occurrence_count'], '1')+ reporting.flush_counts(force=True)+ self.assertEqual(client.capture_event.call_count, 2)++ def test_frequency_shared_file_serializes_independent_processes(self):+ from peach import diagnostic_frequency as frequency+ root = Path(__file__).resolve().parents[1]+ reporting.save({"usage": True, "errors": True})+ program = ('from peach import diagnostic_frequency as f; '+ 'from peach.settings_file import active; '+ 'from peach.remote_reporting import consent; '+ 'f.record(active(), consent()["revision"], {"release":"test", "tags":{"stage":"cli"}}, lambda e: True)')+ env = {**os.environ, 'PYTHONPATH': str(root / 'src'), 'PEACH_SENTRY_DSN': ''}+ processes = [subprocess.Popen([sys.executable, '-X', 'utf8', '-c', program], env=env,+ stdout=subprocess.PIPE, stderr=subprocess.PIPE) for _ in range(2)]+ for process in processes:+ try:+ stdout, stderr = process.communicate(timeout=30)+ self.assertEqual(process.returncode, 0, (stdout, stderr))+ finally:+ if process.poll() is None:+ process.kill()+ process.communicate()+ sent = []+ frequency.flush(self.config, reporting.consent()['revision'], lambda e: sent.append(e) or True, force=True)+ self.assertEqual(len(sent), 1)+ self.assertEqual(sent[0]['tags']['occurrence_count'], '1')++ def test_frequency_shutdown_submits_pending_counts_and_stops_timer(self):+ reporting.save({"usage": True, "errors": True})+ client = mock.Mock()+ with mock.patch.object(reporting, '_new_client', return_value=client):+ for _ in range(3):+ reporting.capture('unhandled-main', ValueError('private'))+ reporting._shutdown()+ self.assertEqual(client.capture_event.call_count, 2)+ self.assertEqual(client.capture_event.call_args.args[0]['tags']['occurrence_count'], '2')+ self.assertIsNone(reporting._frequency_timer)+ self.assertFalse(reporting._frequency_configs)++ def test_frequency_background_failure_does_not_break_startup(self):+ reporting.save({"usage": True, "errors": True})+ with mock.patch.object(reporting, '_schedule_frequency', side_effect=OSError('unavailable')), \+ mock.patch.object(reporting, '_new_client'):+ reporting.usage()++ def test_frequency_timer_flushes_without_another_occurrence(self):+ from peach import diagnostic_frequency as frequency+ reporting.save({"usage": True, "errors": True})+ now = datetime(2026, 10, 18, 12, tzinfo=timezone.utc)+ client = mock.Mock()+ with mock.patch.object(reporting, '_new_client', return_value=client), \+ mock.patch.object(reporting.threading, 'Timer') as timer, \+ mock.patch.object(frequency, '_now', return_value=now) as clock:+ for _ in range(4):+ reporting.capture('unhandled-main', ValueError('private'))+ callback = timer.call_args.args[1]+ clock.return_value = now + timedelta(hours=1)+ callback()+ self.assertEqual(client.capture_event.call_count, 2)+ self.assertEqual(client.capture_event.call_args.args[0]['tags']['occurrence_count'], '3')def test_actionable_faults_keep_codes_and_omit_private_text(self):with mock.patch.object(reporting, "send") as send:@@ -284,6 +431,8 @@ class RemoteReportingTests(unittest.TestCase):for value in ('private title', 'secret', str(self.root), 'server_name', 'contexts', 'extra', 'consent'):self.assertNotIn(value, wire)self.assertIn('ValueError', wire)+ self.assertIn('occurrence_count', wire)+ self.assertIn('diagnostic_key', wire)def test_queued_event_requires_current_joint_consent_revision(self):from sentry_sdk.transport import HttpTransport