Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(diagnostics): 完整投影异常并校准上传状态

longmeidao117479b84ca3
diff --git a/README.en.md b/README.en.md
index b9cd340b1..7464b8708 100644
--- a/README.en.md
+++ b/README.en.md
@@ -54,7 +54,7 @@ Scanning and collection show video and recognizable JAV counts, with incremental
The combined "Send version statistics and error diagnostics (recommended)" option is checked by default. Clear it during installation, first setup or in settings to disable both. Saved opt-out choices are preserved. While enabled, it sends the listed technical fields to Sentry; full logs, diagnostic ZIPs, media details and watch history are excluded. See [remote diagnostics](docs/DIAGNOSTICS.md#可选远程诊断).
-Sharing library and taste details is a separate option in the installer and settings, off by default. Review the complete fields and local preview before explicitly consenting to weekly snapshots of works, tags, ratings, viewing summaries and every preference dimension. Account, device and installation identifiers are excluded. Detailed snapshots can still be linked or re-identified, and the receiving service can see the connection IP. See the [diagnostics reference](docs/DIAGNOSTICS.md#馆藏与口味群体研究).
+Sharing library and taste details is a separate option in the installer and settings, off by default. Review the complete fields and local preview before explicitly consenting to snapshots of works, tags, ratings, viewing summaries and every preference dimension. Each installation can send at most once per UTC week; snapshots without analyzable content wait for data without consuming that opportunity. Account, device and installation identifiers are excluded. Detailed snapshots can still be linked or re-identified, and the receiving service can see the connection IP. See the [diagnostics reference](docs/DIAGNOSTICS.md#馆藏与口味群体研究).
## Screenshots
diff --git a/README.md b/README.md
index 733462276..1fbcec12a 100644
--- a/README.md
+++ b/README.md
@@ -54,7 +54,7 @@ https://github.com/user-attachments/assets/a5610874-e611-41bb-be22-7d99f0d64b62
「发送版本统计与错误诊断(推荐)」默认勾选,可在安装、首次设置或设置页取消。两类技术事件由同一开关控制,已保存的关闭选择会保留。启用后向 Sentry 发送页面列出的技术字段,不自动上传完整日志、诊断 ZIP、媒体信息或观看记录。见[远程诊断说明](docs/DIAGNOSTICS.md#可选远程诊断)。
-馆藏与口味明细上传在安装器和设置页另设独立选项,默认关闭。设置页先展示完整字段与本机预览,经明确同意后,每周上传作品、标签、评分、观看汇总及全部偏好维度的明细;不含账号、设备或安装标识。明细仍有跨次关联与再识别风险,接收服务可见连接 IP。见[馆藏与口味群体研究](docs/DIAGNOSTICS.md#馆藏与口味群体研究)。
+馆藏与口味明细上传在安装器和设置页另设独立选项,默认关闭。设置页先展示完整字段与本机预览,经明确同意后,每周最多发送一次包含作品、标签、评分、观看汇总及全部偏好维度的明细;没有可分析内容时等待数据,不占周传输机会。不含账号、设备或安装标识。明细仍有跨次关联与再识别风险,接收服务可见连接 IP。见[馆藏与口味群体研究](docs/DIAGNOSTICS.md#馆藏与口味群体研究)。
## 截图
diff --git a/docs/DIAGNOSTICS.md b/docs/DIAGNOSTICS.md
index 8c9759f20..e56af88f7 100644
--- a/docs/DIAGNOSTICS.md
+++ b/docs/DIAGNOSTICS.md
@@ -31,17 +31,29 @@
| 类别 | 发送内容与筛选 |
| --- | --- |
-| 版本统计 | Peach 版本与操作系统类型,每机 UTC 自然日最多一次;在进程启动时尝试,没有安装标识,不能据此统计独立用户 |
-| Python 错误 | 版本、平台、Python、CPU 架构、故障环节、异常类型链、Peach 文件名/函数名/行号、整数错误码;错误文本、变量、源码上下文与用户目录不发送 |
+| 版本统计 | Peach 版本、操作系统类型、运行环境、源码/独立包形态与公开构建提交号,每机 UTC 自然日最多一次;在进程启动时尝试,没有安装标识,不能据此统计独立用户 |
+| Python 错误 | 版本、平台、Python、CPU 架构、故障环节、分层异常链、Peach 与白名单框架文件名/函数名/行号、结构化错误码;错误文本、变量、源码上下文与用户目录不发送 |
| HTTP 5xx | 追加方法、路由模板与关联错误编号;不含实例参数、查询参数、正文、鉴权头或用户网址 |
| 媒体工具 | 退出码,以及确定匹配的 AV1 解码器缺失、非法媒体、权限、磁盘空间或未知解码器类别;stderr 与媒体名称留在本机 |
-| 浏览器错误 | 仅发送 Peach 正式脚本的行列位置;没有可定位堆栈的错误与扩展脚本不发送 |
+| 浏览器错误 | 白名单异常类型、Peach 正式脚本的行列位置与公开构建映射 ID;没有可定位堆栈的错误与扩展脚本不发送 |
+
+事件的 `purpose` 区分 `version-statistics`、`technical-diagnostics` 与独立研究通道的 `population-research`。故障视图使用 `level:error purpose:technical-diagnostics`,生产故障可再加 `environment:production`。没有用途标签的历史事件须单独审阅,不按缺失字段推断来源。启动统计是普通 info 事件,应排除在故障视图与告警条件之外。
+
+`environment` 只接受 `production`、`development`、`test`;独立包与普通源码部署默认 `production`,Git 隔离工作树默认 `development`,可用 `PEACH_REPORTING_ENVIRONMENT` 明确指定。`runtime` 为 `packaged` 或 `source`;`dist` 使用构建提交号,不发送本机目录、主机名或自由文本标识。标准测试入口关闭本进程遥测,并向子进程传递 `PEACH_REPORTING_TEST_MODE=1`,在最终 HTTP 请求边界阻止两条通道真实外发;内存接收端仍可验证最终载荷。
+
+异常链按 Sentry 的原因在前、外层在后保存,每层保留自己的帧。框架帧只接受 `asyncio`、`anyio`、`starlette`、`httpx`、`sqlite3`、`urllib3`,只投影模块与代码位置;日志入口仍要求包含 Peach 帧。SQLite 故障附带整数错误码与 `SQLITE_*` 错误名,不含 SQL、表名或错误原文。
+
+同一异常实例及同一 traceback 被多个取证入口观察时只计一次;同一实例重新抛出形成新的 traceback 时仍计数。浏览器没有 stack 但提供正式包文件名和正整数行列时,也可定位;缺少这些证据的泛化错误留在本机。
+
+正式前端构建在 `build/sentry/browser/` 生成匹配的 JavaScript、source maps 与确定性 Debug ID,公开 `web/dist` 只包含正式 JS/CSS,页面路由不分发 maps。构建插件禁用遥测和自动上传。维护者在项目根运行 `npm --prefix frontend run build` 生成本次构建映射;只有显式配置 `SENTRY_AUTH_TOKEN`、`SENTRY_ORG` 和 `SENTRY_PROJECT` 后运行 `npm --prefix frontend run sentry:upload` 才上传。上传前核对私有 JS 与分发 JS 逐字节一致、map 与 Debug ID 对应。映射包含应用及依赖的构建源码,不包含运行日志、馆藏或本机绝对路径。Windows/macOS Release 在各自构建后执行相同上传入口;凭据只进入上传步骤,配置缺失时明确跳过。实际映射是否可用于 Sentry 定位须以接收端的新事件验收,构建成功不等于线上符号化成功。
正常取消、来源未命中/暂不可用、HTTP 4xx、互斥、普通 warning 和无堆栈的泛化日志不发送,也不计入故障次数。后台任务的纯文本失败回执仍保留在本机;有异常链的意外失败可发送。SDK 内部日志不进入远程钩子。
同类故障首次在额度内立即上传,重复次数每小时或正常退出时合并提交。`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 尚未接受的次数保留在聚合文件中。
+每机每天最多接收 20 类故障的本机计数,远程每天最多提交 20 批,重复补报与首次诊断共用批次上限。托盘与子服务使用同一文件锁及聚合文件 `state/remote-reporting-frequency.json`,保留至多 7 个 UTC 日期的技术样本、累计数和已入队数;不读取历史日志。额度不足的待提交次数可在后续启动或定时检查时继续提交,超过保留期失效。SDK 接受入队后更新已入队数;连接失败、服务端丢弃、强制退出、锁争用或超过种类及保留上限均可能造成缺口,因此这是尽力交付的故障计数,不是完整运行审计。SDK 尚未接受的次数保留在聚合文件中。
+
+`state/remote-reporting-delivery.json` 保存各技术用途最后一批的本机回执,配置 API 的 `remote_reporting.delivery` 提供相同的脱敏状态。`queued` 仅表示准备入队;`accepted` 表示传输取得 HTTP 2xx,不保证接收端长期保留;`failed` 表示明确拒绝或本机队列/限流阻断;`unknown` 表示未取得可靠结果,网络异常也可能发生在接收之后。HTTP 2xx 同时带限流头时按未知处理。回执不保存服务端正文、请求网址或载荷;旧事件与旧同意版本的回执不能覆盖当前记录。传输回执不把入队计数改写成接收计数,也不触发补传。
两项均通过公开 DSN 发送到 Sentry 托管服务,接收服务会接触连接 IP。客户端不发送 IP 字段、用户身份、设备标识、观看记录、媒体信息、凭据、完整日志或诊断 ZIP。SDK 发送队列不落盘,本机聚合文件仅存技术投影和次数;后台发送前复查本机同意版本。关闭或重新选择会清除未提交聚合,并让排队旧事件失效;已在途或已接收的请求无法撤回。本机 ZIP 导出独立可用。
@@ -71,6 +83,8 @@
本机策略不清除已有 Sentry 事件,也不控制邮件;邮件摘要与即时通知分别在接收端设置。
+两条通道都禁止底层 HTTP 重试与重定向,保持有限发送次数和 DSN 接收范围。传输回执随用户主动导出的 ZIP 提供;ZIP 不包含本机故障聚合样本或研究明细。
+
### 馆藏与口味群体研究
「上传馆藏与口味明细」是独立默认关闭的选项,位于「应用与维护 → 数据上传」,与技术诊断使用各自的开关。开关始终可见。设置页由用户生成本机预览,查看实际字段与内容后明确同意;预览数量、大小与排除的个人数据合并为一段说明。服务端要求当前进程生成、10 分钟内有效的一次性预览凭据、同意值与字段版本。单独操作开关或开启错误诊断不构成馆藏上传同意,关闭不要求预览。安装器提供默认不勾选的独立选项,短标签概括内容与风险;主动勾选后展示可滚动的完整字段、用途与风险说明页,继续表示同意,返回可取消选择。辅助进程只保存选择,不读取馆藏或初始化 SDK。静默安装保留已有选择。字段版本为 `4`,其他版本的同意无效。
@@ -88,7 +102,11 @@
预览与后台采集只读 ledger,不读取浏览历史、书签或写入偏好缓存。大预览在代码块展示前 24,000 个字符,可复制或下载完整本机 JSON。预览不是发送,也不会开启上传;同意覆盖每周发送相同字段的最新快照。同意后收起预览说明、数据预览和同意框;预览入口仍可重新打开,已开启时不重复要求同意。契约改变须推进 `SCHEMA` 并重新取得同意。
-后台只有在有效同意和独立接收端同时存在时才读取明细并初始化 SDK。每机每 UTC 自然周最多尝试一份,跨进程共用带字段版本的尝试记录;不补传历史快照。原始 JSON 上限 100 MiB,gzip 附件上限 18 MiB;任一超限均整份拒绝,不发送截断数据。gzip 头不含源文件名和生成时间。传输只接受固定用途事件、固定文件名附件与字段白名单,后台真正发送前复查同意版本。关闭让排队记录失效,已发送数据无法经本机开关撤回。发送尝试不表示服务端接收成功。
+后台只有在有效同意和独立接收端同时存在时才读取明细并初始化 SDK。快照须至少包含一部公共编号作品或一个偏好分组;没有可分析内容时记为 `waiting-data`,每小时重新检查,不占周传输机会。收集、压缩或入队前失败可以重新准备。每机每 UTC 自然周最多通过一次最终发送门槛,跨进程使用同一文件锁预约;重新选择同意不重置周限次。实际发送后无论明确失败还是结果未知,本周均不再发送,不补传历史快照。空快照不会进入研究样本,分析结果须披露这个筛选口径。
+
+`state/library-sharing-attempt.json` 区分 `waiting-data`、`queued`、`accepted`、`failed`、`unknown` 与 `blocked`,`attempted` 表示是否已通过最终发送门槛。`accepted` 仅证明取得 HTTP 2xx;未取得 HTTP 回执、网络中断和历史无回执记录均不解释为接收成功。旧字段版本、旧 UTC 周与旧同意版本的队列不能发送;旧尝试回执不能覆盖新尝试。
+
+原始 JSON 上限 100 MiB,gzip 附件上限 18 MiB;任一超限均整份拒绝,不发送截断数据。gzip 头不含源文件名和生成时间。传输只接受固定用途事件、固定文件名附件与字段白名单,后台真正发送前复查同意版本。关闭让排队记录失效,已发送数据无法经本机开关撤回。
维护者另设 `PEACH_LIBRARY_SENTRY_DSN`,Windows Release 读取同名仓库变量并写入包内 `library_dsn`。该 DSN 的项目必须与错误诊断项目不同,同项目配置视为未配置。研究快照作为 Sentry 附件接收,每次也产生一条事件;组织内的事件和附件额度仍共享。见 [Sentry 附件说明](https://docs.sentry.io/platforms/python/enriching-events/attachments/)。
diff --git a/docs/REUSE.md b/docs/REUSE.md
index 4e4c2c80e..a42e2ddc3 100644
--- a/docs/REUSE.md
+++ b/docs/REUSE.md
@@ -1,5 +1,13 @@
# 复用清单
+## 浏览器错误的构建映射
+
+前端复用官方 [`@sentry/vite-plugin` 11.8.0](https://github.com/getsentry/sentry-javascript-bundler-plugins)(MIT,维护中,Node ≥23.2,与项目 Node 24 和 Vite 8 兼容)生成确定性 Debug ID 与 hidden source maps;[`@sentry/cli` 3.8.0](https://github.com/getsentry/sentry-cli)(FSL-1.1-MIT)仅用于构建期本地定位和显式上传。两者精确固定在前端开发依赖,纳入现有 Dependabot;不进入 Python 运行时或独立包依赖。
+
+`frontend/scripts/sentry-build.ts` 只保留 Peach 的公开/私有目录边界与产物匹配检查。官方插件禁用遥测、自动上传与 release 注入,普通构建不需要凭据;私有目录保存 JS/CSS/maps,公开目录只复制四个正式 JS/CSS。显式上传复用官方 CLI,先核对分发字节和 map 身份。没有实现自定义 source map 编解码器或上传协议,也没有为此引入浏览器 SDK、页面回放或性能采集。
+
+回归使用真实 Vite 构建与官方 CLI 本地解析 source map,验证源码文件和行号;相同输入两次构建的文件字节一致,Debug ID 与 map 对应,映射源只使用相对路径。构建体积由 Vite 输出,source maps 只增加私有构建产物,不增加页面分发体积。字段与发布配置见[诊断核验](DIAGNOSTICS.md#可选远程诊断);真实接收端符号化独立验收。
+
## 空状态与操作入口
React 页面复用 `EmptyState`,HTML 页面复用 `emptyStateHtml`。图标统一为 32px、图标框为 54px;标准区域最小高度为 320px,紧凑区域为 240px,字体、背景和边框共用。统计页上下区域采用标准尺寸。空状态按原因提供添加内容、清除筛选、重试或返回浏览等操作;正常完成状态不增加操作。
@@ -218,7 +226,7 @@ Peach 播放器皮肤直接装饰 Video.js 10 的 light DOM:右侧控制组用
可选远程诊断复用官方 [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.md)。没有新增依赖或真实 ledger 写入。
+故障频率复用已固定的 `filelock`、`fsutil.atomic_write_text` 和 SDK 事件标签,保留 Peach 的同意版本、技术投影、跨进程计数与每日批次策略。`diagnostic_frequency` 保存有界技术样本及待提交增量,首次、每小时和正常退出通过同一 SDK 提交;关闭会清除聚合。Sentry 默认事件数统计批次,不会将自定义次数标签自动求和;汇总按事件的 `occurrence_day` 与 `occurrence_count` 计算。`reporting_transport` 复用官方 `HttpTransport` 的 `_handle_response`、`_handle_request_error` 与队列丢弃回调,取得 HTTP 状态且不重复实现请求、TLS 或重试;固定内部接口以真实 SDK 和模拟 HTTP 回归。`reporting_delivery` 只保存本机有限回执;SDK 入队、HTTP 接收与接收端留存分别解释。用途、环境与公开构建号复用 Sentry 原生字段,测试在同一传输边界拦截外发。未开启 SDK metrics 或新增收集服务:独立指标通道会增加同意边界、额度与交付口径。计数只覆盖已筛选技术故障,发送及保留限制见 [诊断核验](DIAGNOSTICS.md)。没有新增 Python 依赖或真实 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.md#远程错误筛选核验)。
diff --git a/frontend/src/react/settings/library-sharing-settings.tsx b/frontend/src/react/settings/library-sharing-settings.tsx
index fdd2ee7d3..70f343949 100644
--- a/frontend/src/react/settings/library-sharing-settings.tsx
+++ b/frontend/src/react/settings/library-sharing-settings.tsx
@@ -12,7 +12,7 @@ import { busyProps, useAction } from './use-action';
export interface LibrarySharingState {
enabled: boolean; available: boolean; schema: number; fields: string[]; notice: string;
- last_attempt?: { period?: string; status?: string; reason?: string };
+ last_attempt?: { period?: string; status?: string; reason?: string; attempted?: boolean };
}
interface Preview { data: { library: unknown[]; preferences: unknown; coverage: { uncoded_items: number } }; bytes: number; token: string; schema: number }
@@ -48,7 +48,12 @@ export function LibrarySharingSettings({ initial, receipt }: {
</SettingsRow></Rows>
{!value.available ? <Stack divided><Help>上传服务未配置。</Help></Stack> : null}
{value.enabled && value.last_attempt?.status === 'failed'
- ? <Stack divided><ErrorText>馆藏与口味上传失败,自动重试。</ErrorText></Stack> : null}
+ ? <Stack divided><ErrorText>{value.last_attempt.attempted
+ ? '馆藏与口味上传失败,本周不再发送。' : '馆藏与口味上传未完成,将自动重新准备。'}</ErrorText></Stack> : null}
+ {value.enabled && value.last_attempt?.status === 'waiting-data'
+ ? <Stack divided><Help>等待可分析的馆藏或偏好数据。</Help></Stack> : null}
+ {value.enabled && value.last_attempt?.status === 'unknown'
+ ? <Stack divided><Help>未取得上传结果,本周不再发送。</Help></Stack> : null}
{preview ? <Stack divided>
<Help>当前预览包含 {preview.data.library.length} 部有编号作品、{preview.data.coverage.uncoded_items} 条无编号馆藏的汇总,约 {(preview.bytes / 1024).toFixed(1)} KiB。不含账号、设备、文件路径和浏览器历史记录等个人数据。</Help>
<Disclosure summary="上传数据预览" defaultOpen>
diff --git a/frontend/test/react/library-sharing.test.tsx b/frontend/test/react/library-sharing.test.tsx
index 334e75ffd..b5fb5b080 100644
--- a/frontend/test/react/library-sharing.test.tsx
+++ b/frontend/test/react/library-sharing.test.tsx
@@ -7,6 +7,16 @@ const initial: LibrarySharingState = { enabled: false, available: true, schema:
fields: ['作品编号、标签、评分与全部偏好维度'], notice: '匿名处理后上传馆藏与各类偏好明细。' };
describe('馆藏与口味分享的明确同意', () => {
+ it.each([
+ [{ status: 'waiting-data', attempted: false }, '等待可分析的馆藏或偏好数据。'],
+ [{ status: 'failed', attempted: false }, '馆藏与口味上传未完成,将自动重新准备。'],
+ [{ status: 'failed', attempted: true }, '馆藏与口味上传失败,本周不再发送。'],
+ [{ status: 'unknown', attempted: true }, '未取得上传结果,本周不再发送。'],
+ ])('按实际发送状态显示反馈 %j', async (last_attempt, message) => {
+ const host = await mount(<LibrarySharingSettings initial={{ ...initial, enabled: true, last_attempt }} receipt={vi.fn()} />);
+ expect(host.textContent).toContain(message);
+ });
+
it('开关始终可见,上传预览和明确同意完整后才能开启', async () => {
const fetcher = vi.fn(async (url: string) => ({ ok: true, json: async () => url.endsWith('/preview')
? { data: { library: [{ code: 'EXM-001' }], preferences: {}, coverage: { uncoded_items: 0 } }, bytes: 80, token: 'preview-token', schema: 4 }
diff --git a/scripts/test_runner.py b/scripts/test_runner.py
index b605f928f..aeef087ad 100644
--- a/scripts/test_runner.py
+++ b/scripts/test_runner.py
@@ -104,7 +104,7 @@ SCOPES: dict[str, tuple[str, ...]] = {
"test_resource_identification.py",
"test_agency_entity.py", "test_agency_reject.py", "test_label_maker.py",
"test_seed_pack.py", "test_seed_followup.py", "test_seed_corrections.py"),
- "tooling": ("test_scripts.py", "test_trash_junk.py", "test_auth.py", "test_access.py", "test_cli.py", "test_server.py", "test_diagnostics.py", "test_error_reporting.py", "test_remote_reporting.py", "test_library_sharing.py", "test_library_diagnostics.py", "test_script_policy.py",
+ "tooling": ("test_scripts.py", "test_trash_junk.py", "test_auth.py", "test_access.py", "test_cli.py", "test_server.py", "test_diagnostics.py", "test_error_reporting.py", "test_remote_reporting.py", "test_reporting_*.py", "test_library_sharing.py", "test_library_diagnostics.py", "test_script_policy.py",
"test_scan.py", "test_record_rehome.py", "test_wants.py", "test_push_discovery.py", "test_media_probe.py", "test_subtitles.py", "test_onboarding.py", "test_configuration_sources.py", "test_clouddrive_proxy.py", "test_folder_picker.py", "test_ledger_backups.py", "test_clear_camera_filename_codes.py",
"test_agent_worktree.py", "test_test_evidence.py", "test_test_environment.py", "test_dependency_policy.py",
"test_version_bump.py", "test_changelog.py", "test_release_due.py",
diff --git a/src/peach/diagnostic_browser.py b/src/peach/diagnostic_browser.py
new file mode 100644
index 000000000..8c962de59
--- /dev/null
+++ b/src/peach/diagnostic_browser.py
@@ -0,0 +1,56 @@
+"""正式前端包的异常类型、帧与构建映射白名单。"""
+import re
+import uuid
+
+NAMES = frozenset({"Error", "TypeError", "ReferenceError", "SyntaxError", "RangeError", "URIError",
+ "EvalError", "AggregateError", "DOMException", "SecurityError", "NotAllowedError", "InvalidStateError",
+ "QuotaExceededError", "NotSupportedError", "TimeoutError", "NetworkError", "DataError", "OperationError"})
+BUNDLES = frozenset({"peach-app.js", "peach-pages.js"})
+
+
+def project(fields):
+ frames = []
+ pattern = r"(?<![\w.-])(peach-(?:app|pages)\.js)(?:\?[^\s):]*)?:(\d+):(\d+)"
+ for line in str(fields.get("stack", "")).splitlines()[:80]:
+ if "chrome-extension://" in line or "moz-extension://" in line:
+ continue
+ for name, row, column in re.findall(pattern, line):
+ frames.append({"filename": name, "abs_path": "app:///" + name,
+ "lineno": int(row), "colno": int(column)})
+ frames = list(reversed(frames[:20]))
+ if not frames and fields.get("source_file") in BUNDLES:
+ row, column = fields.get("line"), fields.get("column")
+ if type(row) is int and type(column) is int and row > 0 and column > 0:
+ bundle = fields["source_file"]
+ frames = [{"filename": bundle, "abs_path": "app:///" + bundle, "lineno": row, "colno": column}]
+ name = fields.get("error_name")
+ name = name if name in NAMES else "BrowserError"
+ value = {"type": name, "value": name, "stacktrace": {"frames": frames}}
+ images = []
+ identities = fields.get("bundle_debug_ids", {})
+ if isinstance(identities, dict):
+ for bundle in sorted({frame["filename"] for frame in frames}):
+ candidate = identities.get(bundle)
+ if not isinstance(candidate, str) or len(candidate) != 36:
+ continue
+ try:
+ identity = uuid.UUID(candidate)
+ if identity.int and str(identity) == candidate.lower():
+ images.append({"type": "sourcemap", "code_file": "app:///" + bundle, "debug_id": str(identity)})
+ except ValueError:
+ continue
+ return value, {"images": images} if images else None
+
+
+def clean_debug_meta(value):
+ """SDK scope 不能往受限事件中加入任意映射元数据。"""
+ if not isinstance(value, dict) or not isinstance(value.get("images"), list):
+ return None
+ fields = {"stack": "\n".join(bundle + ":1:1" for bundle in sorted(BUNDLES)), "bundle_debug_ids": {}}
+ for image in value["images"][:2]:
+ if isinstance(image, dict) and image.get("type") == "sourcemap":
+ code = image.get("code_file")
+ for bundle in BUNDLES:
+ if code == "app:///" + bundle:
+ fields["bundle_debug_ids"][bundle] = image.get("debug_id")
+ return project(fields)[1]
diff --git a/src/peach/diagnostic_exceptions.py b/src/peach/diagnostic_exceptions.py
new file mode 100644
index 000000000..c59e05d63
--- /dev/null
+++ b/src/peach/diagnostic_exceptions.py
@@ -0,0 +1,42 @@
+"""只保留代码位置、异常身份与结构化错误码的异常链投影。"""
+from pathlib import Path
+import re
+
+FRAME_MODULES = {"peach", "asyncio", "anyio", "starlette", "httpx", "sqlite3", "urllib3"}
+
+
+def project(error, http_status):
+ values, codes, chain = [], {}, []
+ current, visited = error, set()
+ remaining = 40
+ while current is not None and id(current) not in visited and len(chain) < 8:
+ visited.add(id(current))
+ name = type(current).__name__
+ chain.append(name)
+ for key in ("errno", "winerror", "returncode", "sqlite_errorcode"):
+ value = getattr(current, key, None)
+ if type(value) is int:
+ codes[key] = str(value)
+ sqlite_name = getattr(current, "sqlite_errorname", "")
+ if isinstance(sqlite_name, str) and re.fullmatch(r"SQLITE_[A-Z_]{1,50}", sqlite_name):
+ codes["sqlite_errorname"] = sqlite_name
+ if (status := http_status(current)) is not None:
+ codes["status_code"] = str(status)
+ frames = []
+ tb = current.__traceback__
+ while tb is not None and remaining:
+ filename = Path(tb.tb_frame.f_code.co_filename)
+ module = tb.tb_frame.f_globals.get("__name__", "")
+ own = filename.parent.name == "peach" or module.startswith("peach.")
+ if own or module.split(".", 1)[0] in FRAME_MODULES:
+ frame = {"filename": filename.name, "lineno": tb.tb_lineno,
+ "function": tb.tb_frame.f_code.co_name}
+ if not own:
+ frame["module"] = module
+ frames.append(frame)
+ remaining -= 1
+ tb = tb.tb_next
+ values.append({"type": name, "module": type(current).__module__, "value": name,
+ "stacktrace": {"frames": frames}})
+ current = current.__cause__ or (None if current.__suppress_context__ else current.__context__)
+ return list(reversed(values)), codes, chain
diff --git a/src/peach/remote_reporting.py b/src/peach/remote_reporting.py
index c5bdfef05..ad7986f41 100644
--- a/src/peach/remote_reporting.py
+++ b/src/peach/remote_reporting.py
@@ -18,8 +18,8 @@ from urllib.parse import urlsplit
from .reporting_identity import event_metadata
FIELDS = {
- "usage": "Peach 版本、操作系统类型;每天最多一次,不含安装标识。",
- "errors": "Peach 版本、系统类型、Python 版本、CPU 架构、故障环节、异常链、Peach 代码文件名/函数名/行号、结构化错误码、媒体工具故障类别、HTTP 方法/路由模板/关联错误编号、同类故障次数与统计日期;首次立即上传,重复次数每小时或退出时合并,每天最多 20 批。",
+ "usage": "Peach 版本、操作系统类型、运行环境与公开构建编号;每天最多一次,不含安装标识。",
+ "errors": "Peach 版本、运行环境与公开构建编号、系统类型、Python 版本、CPU 架构、故障环节、异常链、Peach 与白名单框架的代码文件名/函数名/行号、结构化错误码、媒体工具故障类别、浏览器白名单异常类型与正式脚本构建映射、HTTP 方法/路由模板/关联错误编号、同类故障次数与统计日期;首次立即上传,重复次数每小时或退出时合并,每天最多 20 批。",
}
NOTICE = "发送至 Sentry。不上报错误原文、完整日志、诊断 ZIP、媒体名称、路径、网址、观看记录或凭据。接收服务会接触连接 IP;关闭后停止新发送,已发送的信息无法撤回。"
_lock = threading.RLock()
@@ -168,9 +168,13 @@ def _new_client(target, *, envelope_gate=None, delivery_callback=None):
def final_event(event, _hint):
# SDK 的服务器名、上下文、SDK 包清单和全局 scope 均不进入发送载荷。
- return {key: value for key, value in event.items() if key in {
+ result = {key: value for key, value in event.items() if key in {
"event_id", "timestamp", "platform", "release", "environment", "dist", "level", "message", "tags", "exception", "extra",
}}
+ from .diagnostic_browser import clean_debug_meta
+ if event.get("platform") == "javascript" and (metadata := clean_debug_meta(event.get("debug_meta"))):
+ result["debug_meta"] = metadata
+ return result
return Client(dsn=target, transport=transport_class(gate, delivery_callback), default_integrations=False,
auto_enabling_integrations=False, auto_session_tracking=False,
@@ -254,6 +258,7 @@ def send(kind, event, config=None):
_schedule_frequency(config)
elif _reserve(config, kind, event):
_submit(kind, event, config, current, target)
+ return True
except Exception:
# 不能把遥测自身故障写回错误钩子而递归发送。
pass
@@ -280,26 +285,8 @@ def _http_status(error):
def _exception_details(error):
- frames, codes, chain = [], {}, []
- current, visited = error, set()
- while current is not None and id(current) not in visited and len(chain) < 8:
- visited.add(id(current))
- chain.append(type(current).__name__)
- for key in ("errno", "winerror", "returncode"):
- value = getattr(current, key, None)
- if type(value) is int:
- codes[key] = value
- if (status := _http_status(current)) is not None:
- codes["status_code"] = status
- tb = current.__traceback__
- while tb is not None and len(frames) < 40:
- filename = Path(tb.tb_frame.f_code.co_filename)
- if filename.parent.name == "peach":
- frames.append({"filename": filename.name, "lineno": tb.tb_lineno,
- "function": tb.tb_frame.f_code.co_name})
- tb = tb.tb_next
- current = current.__cause__ or (None if current.__suppress_context__ else current.__context__)
- return frames, codes, chain
+ from .diagnostic_exceptions import project
+ return project(error, _http_status)
def _media_details(error, fields, codes):
@@ -381,30 +368,20 @@ def _http_details(stage, fields):
return tags, error_id
-def _browser_frames(stack):
- frames = []
- pattern = r"(peach-(?:app|pages)\.js)(?:\?[^\s):]*)?:(\d+):(\d+)"
- for line in str(stack).splitlines()[:80]:
- if "chrome-extension://" in line or "moz-extension://" in line:
- continue
- for name, row, column in re.findall(pattern, line):
- frames.append({"filename": name, "lineno": int(row), "colno": int(column)})
- return frames[:20]
-
-
-def capture(stage, error=None, **fields):
- """只投影代码位置和错误类别,不发送异常文本或日志文本。"""
- if not _actionable(stage, error, fields):
- return
+def _technical_projection(stage, error, fields):
+ """投影各入口的故障证据,排除缺少定位信息与已知媒体输入条件的记录。"""
media_stage = stage in MEDIA_STAGES
if error is None and stage != "browser" and not media_stage:
return
- frames, codes, chain = _exception_details(error)
+ values, codes, _chain = _exception_details(error)
+ debug_meta = None
if error is None and stage == "browser":
- frames = _browser_frames(fields.get("stack", ""))
- if not frames:
+ from .diagnostic_browser import project
+ value, debug_meta = project(fields)
+ if not value["stacktrace"]["frames"]:
return
- if stage == "logging" and not frames:
+ values = [value]
+ if stage == "logging" and not any("module" not in frame for value in values for frame in value["stacktrace"]["frames"]):
return
for key in ("returncode", "status_code"):
if type(fields.get(key)) is int:
@@ -416,16 +393,39 @@ def capture(stage, error=None, **fields):
return
if not reason and not codes and error is None:
return
- category = type(error).__name__ if error is not None else ("BrowserError" if stage == "browser" else "LogError")
+ category = type(error).__name__ if error is not None else ("BrowserError" if stage == "browser" else "MediaToolError")
+ if not values:
+ values = [{"type": category, "value": reason or category, "stacktrace": {"frames": []}}]
+ return values, codes, reason, debug_meta
+
+
+def capture(stage, error=None, **fields):
+ """只投影代码位置和错误类别,不发送异常文本或日志文本。"""
+ if not _actionable(stage, error, fields):
+ return
+ capture_key = (os.getpid(), id(error.__traceback__)) if error is not None else None
+ if error is not None and getattr(error, "_peach_remote_capture", None) == capture_key:
+ return
+ projection = _technical_projection(stage, error, fields)
+ if projection is None:
+ return
+ values, codes, reason, debug_meta = projection
http, error_id = _http_details(stage, fields)
event = {"level": "error", "tags": {"os": platform.system(),
"python": platform.python_version(), "architecture": platform.machine(), "stage": stage,
**http, **{key: str(value) for key, value in codes.items()}, **({"media_failure": reason} if reason else {})},
- "exception": {"values": [{"type": category, "value": " / ".join(chain) or reason or category,
- "stacktrace": {"frames": frames}}]}}
+ "exception": {"values": values}}
+ if stage == "browser":
+ event["platform"] = "javascript"
+ if debug_meta:
+ event["debug_meta"] = debug_meta
if error_id:
event["event_id"] = error_id
- send("errors", event)
+ if send("errors", event) and error is not None:
+ try:
+ error._peach_remote_capture = capture_key
+ except (AttributeError, TypeError):
+ pass
def disable_for_process():
diff --git a/src/peach/reporting_delivery.py b/src/peach/reporting_delivery.py
index 4d72adf45..a0b5aec3c 100644
--- a/src/peach/reporting_delivery.py
+++ b/src/peach/reporting_delivery.py
@@ -1,5 +1,6 @@
"""诊断通道的本机传输回执,不保存载荷或服务端错误正文。"""
from datetime import datetime, timezone
+import json
from pathlib import Path
from filelock import FileLock
@@ -11,6 +12,14 @@ REASONS = {"network", "http_status", "rate_limit", "queue_full", "consent", "dis
def record(permit, event_id, result):
+ try:
+ _record(permit, event_id, result)
+ except Exception:
+ # 本机回执不可写不能阻止原本获准的诊断,也不能触发错误钩子。
+ pass
+
+
+def _record(permit, event_id, result):
from .remote_reporting import _consent, _read
if not isinstance(permit, dict) or permit.get("kind") not in {"usage", "errors"}:
return
@@ -24,10 +33,14 @@ def record(permit, event_id, result):
state_path = path.with_name("remote-reporting-delivery.json")
state_path.parent.mkdir(parents=True, exist_ok=True)
with FileLock(str(state_path) + ".lock", timeout=.1):
+ current = _consent(path)
+ if current["revision"] != permit.get("revision") or not current[permit["kind"]]:
+ return
state = _read(state_path)
if state.get("revision") != current["revision"]:
state = {"revision": current["revision"]}
previous = state.get(permit["kind"], {})
+ previous = previous if isinstance(previous, dict) else {}
if status != "queued" and previous.get("event_id") != event_id:
return
row = {"status": status, "event_id": event_id, "at": datetime.now(timezone.utc).isoformat()}
@@ -37,7 +50,6 @@ def record(permit, event_id, result):
if type(code) is int and 100 <= code <= 599:
row["status_code"] = code
state[permit["kind"]] = row
- import json
atomic_write_text(state_path, json.dumps(state))
diff --git a/src/peach/reporting_identity.py b/src/peach/reporting_identity.py
index 250c32f4c..7619635d9 100644
--- a/src/peach/reporting_identity.py
+++ b/src/peach/reporting_identity.py
@@ -18,7 +18,7 @@ def _source_commit():
from .settings_file import PROJECT_ROOT
try:
result = subprocess.run(["git", "rev-parse", "HEAD"], cwd=PROJECT_ROOT,
- capture_output=True, text=True, timeout=2,
+ capture_output=True, text=True, encoding="utf-8", timeout=2,
creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0)
commit = result.stdout.strip() if result.returncode == 0 else ""
return commit if re.fullmatch(r"[a-f0-9]{40}", commit) else None
@@ -27,10 +27,11 @@ def _source_commit():
def event_metadata(kind):
+ from .settings_file import PROJECT_ROOT
runtime = "packaged" if getattr(sys, "frozen", False) else "source"
environment = os.environ.get("PEACH_REPORTING_ENVIRONMENT", "")
if environment not in {"production", "development", "test"}:
- environment = "production" if runtime == "packaged" else "development"
+ environment = "development" if runtime == "source" and (PROJECT_ROOT / ".git").is_file() else "production"
build = frozen_build() if runtime == "packaged" else None
commit = build.commit if build else (_source_commit() if runtime == "source" else None)
result = {"release": __version__, "environment": environment,
diff --git a/src/peach/reporting_transport.py b/src/peach/reporting_transport.py
index eddfc94bc..617d491a6 100644
--- a/src/peach/reporting_transport.py
+++ b/src/peach/reporting_transport.py
@@ -40,10 +40,11 @@ def transport_class(gate, callback):
except Exception:
pass
- def _request(self, *args, **kwargs):
+ def _request(self, method, endpoint_type, body, headers):
if os.environ.get("PEACH_REPORTING_TEST_MODE") == "1":
raise OSError("测试进程禁止远程诊断外发")
- return super()._request(*args, **kwargs)
+ return self._pool.request(method, self._auth.get_api_url(endpoint_type), body=body,
+ headers=headers, retries=False, redirect=False)
def _send_envelope(self, envelope):
self._context(envelope)
@@ -79,6 +80,8 @@ def transport_class(gate, callback):
def on_dropped_event(self, reason):
super().on_dropped_event(reason)
if reason in {"full_queue", "self_rate_limits"}:
+ if reason == "self_rate_limits":
+ self._outcome = True
self._notify("failed", "queue_full" if reason == "full_queue" else "rate_limit")
return ReportingTransport
diff --git a/src/peach/routes_configuration.py b/src/peach/routes_configuration.py
index 4ae84c4ed..cca9a4132 100644
--- a/src/peach/routes_configuration.py
+++ b/src/peach/routes_configuration.py
@@ -161,6 +161,10 @@ async def client_error(request: Request):
if isinstance(value := data.get(key), str)}
fields.update({key: value for key in ('line', 'column')
if type(value := data.get(key)) is int and value >= 0})
+ identities = data.get('bundle_debug_ids')
+ if isinstance(identities, dict):
+ fields['bundle_debug_ids'] = {key: value for key, value in identities.items()
+ if key in {'peach-app.js', 'peach-pages.js'} and isinstance(value, str) and len(value) == 36}
fields['user_agent'] = request.headers.get('user-agent', '')[:512]
if error_reporting.event("browser", **fields) is None:
raise HTTPException(503, "错误日志无法写入")
diff --git a/src/peach/support_bundle.py b/src/peach/support_bundle.py
index 519f01b20..c520d05ec 100644
--- a/src/peach/support_bundle.py
+++ b/src/peach/support_bundle.py
@@ -15,7 +15,8 @@ LOG_NAMES = ("tray.log", "tray-lifecycle.log", "tray-setup.err.log", "tray-setup
"tray-http.err.log", "tray-http.out.log", "tray-https.err.log", "tray-https.out.log",
'tray-restart.out.log', 'tray-scan.out.log', 'windows-source-sync.log', 'cloudflare-tunnel.log')
STATE_NAMES = ('library-processing.json', 'runtime-prepare.json', 'standalone-update.json',
- 'automatic-updates.json', 'windows-source-sync.json', 'cloudflare-tunnel.json')
+ 'automatic-updates.json', 'windows-source-sync.json', 'cloudflare-tunnel.json',
+ 'remote-reporting-delivery.json', 'library-sharing-attempt.json')
TAIL_BYTES = 96 * 1024
diff --git a/tests/test_error_reporting.py b/tests/test_error_reporting.py
index e4e9a2a56..642f9aca5 100644
--- a/tests/test_error_reporting.py
+++ b/tests/test_error_reporting.py
@@ -76,6 +76,20 @@ class ErrorReportingTests(unittest.TestCase):
self.assertTrue(all(json.loads(line)['asset_id'] == 23 for line in text.splitlines()))
self.assertTrue(next(row for row in report['logs'] if row['name'] == path.name)['truncated'])
+ def test_support_bundle_contains_local_delivery_receipts_without_research_payloads(self):
+ from peach.settings_file import PeachConfig
+ config = PeachConfig(self.root, self.root / 'config.toml', present=True)
+ state = config.directory('state')
+ state.mkdir(parents=True)
+ (state / 'remote-reporting-delivery.json').write_text(json.dumps({'errors': {'status': 'accepted', 'status_code': 200}}))
+ (state / 'library-sharing-attempt.json').write_text(json.dumps({'status': 'unknown', 'attempted': True}))
+ (state / 'remote-reporting-frequency.json').write_text('private-technical-sample')
+ with mock.patch.object(error_reporting, 'log_directories', return_value=[]), \
+ zipfile.ZipFile(io.BytesIO(support_bundle.bundle_bytes(config=config))) as archive:
+ self.assertEqual(json.loads(archive.read('processing/remote-reporting-delivery.json'))['errors']['status'], 'accepted')
+ self.assertEqual(json.loads(archive.read('processing/library-sharing-attempt.json'))['status'], 'unknown')
+ self.assertNotIn('processing/remote-reporting-frequency.json', archive.namelist())
+
def test_bundle_contains_update_tunnel_rotated_and_startup_evidence_with_invalid_config(self):
from peach import settings_file
self.logs.mkdir()
diff --git a/tests/test_library_sharing.py b/tests/test_library_sharing.py
index 5948da56a..f7163ad8c 100644
--- a/tests/test_library_sharing.py
+++ b/tests/test_library_sharing.py
@@ -357,7 +357,7 @@ class LibrarySharingTests(unittest.TestCase):
{'status': 'failed', 'status_code': 403, 'reason': 'http_status'},
{'status': 'unknown', 'reason': 'network'}):
sharing._state_path(self.config).unlink(missing_ok=True)
- def make_client(*_args, envelope_gate, delivery_callback):
+ def make_client(*_args, envelope_gate, delivery_callback, result=result):
def capture(*_args, **_kwargs):
self.assertTrue(envelope_gate(self.envelope()))
delivery_callback({**result, 'url': 'private-url', 'detail': 'private-content'})
@@ -393,7 +393,6 @@ class LibrarySharingTests(unittest.TestCase):
self.assertNotIn('reason', self.attempt_state())
def test_real_sdk_http_receipt_controls_local_research_status(self):
- from sentry_sdk.transport import HttpTransport
self.add_work()
self.enable()
for status, headers, expected in ((200, {}, 'accepted'), (413, {}, 'failed'), (429, {}, 'failed'),
@@ -402,7 +401,7 @@ class LibrarySharingTests(unittest.TestCase):
sharing._state_path(self.config).unlink(missing_ok=True)
response = SimpleNamespace(status=status, headers=headers, close=mock.Mock(), data=b'private')
with mock.patch.dict(os.environ, {'PEACH_REPORTING_TEST_MODE': '0'}), \
- mock.patch.object(HttpTransport, '_request', return_value=response,
+ mock.patch('urllib3.PoolManager.request', return_value=response,
side_effect=OSError('private proxy') if status is None else None) as request:
self.service.attempt()
self.service.attempt()
diff --git a/tests/test_remote_reporting.py b/tests/test_remote_reporting.py
index b2da3a140..0908251c9 100644
--- a/tests/test_remote_reporting.py
+++ b/tests/test_remote_reporting.py
@@ -286,7 +286,7 @@ class RemoteReportingTests(unittest.TestCase):
send.assert_not_called()
reporting.capture('browser', message='private title', stack='at https://private/dist/peach-app.js:12:34')
self.assertEqual(send.call_args.args[1]['exception']['values'][0]['stacktrace']['frames'],
- [{'filename': 'peach-app.js', 'lineno': 12, 'colno': 34}])
+ [{'filename': 'peach-app.js', 'abs_path': 'app:///peach-app.js', 'lineno': 12, 'colno': 34}])
def test_control_and_source_states_keep_local_records_without_remote_events(self):
import asyncio
@@ -399,7 +399,7 @@ class RemoteReportingTests(unittest.TestCase):
reporting.capture('browser', stack='at https://example.test/dist/peach-app.js?v=build:12:34')
send.assert_called_once()
frames = send.call_args.args[1]['exception']['values'][0]['stacktrace']['frames']
- self.assertEqual(frames, [{'filename': 'peach-app.js', 'lineno': 12, 'colno': 34}])
+ self.assertEqual(frames, [{'filename': 'peach-app.js', 'abs_path': 'app:///peach-app.js', 'lineno': 12, 'colno': 34}])
def test_preview_environment_conditions_preserve_guidance_and_local_issue(self):
from peach import library_processing
diff --git a/tests/test_reporting_projection.py b/tests/test_reporting_projection.py
new file mode 100644
index 000000000..657098532
--- /dev/null
+++ b/tests/test_reporting_projection.py
@@ -0,0 +1,102 @@
+"""远程错误投影保留可定位信息且拒绝任意来源字段。"""
+import json
+import unittest
+from unittest import mock
+
+from peach import diagnostic_browser, diagnostic_exceptions, remote_reporting
+
+
+class ReportingProjectionTests(unittest.TestCase):
+ def test_python_chain_has_separate_frames_and_sqlite_error_identity(self):
+ code = compile('def fail():\n import sqlite3\n connection = sqlite3.connect(":memory:")\n'
+ ' try:\n connection.execute("SELECT private_column")\n'
+ ' except sqlite3.OperationalError as error:\n raise RuntimeError("private text") from error\n',
+ 'private/peach/fixture.py', 'exec')
+ namespace = {'__name__': 'peach.fixture'}
+ exec(code, namespace)
+ try:
+ namespace['fail']()
+ except RuntimeError as error:
+ values, codes, chain = diagnostic_exceptions.project(error, lambda error: None)
+ self.assertEqual(chain, ['RuntimeError', 'OperationalError'])
+ self.assertEqual([value['type'] for value in values], ['OperationalError', 'RuntimeError'])
+ self.assertEqual(codes['sqlite_errorname'], 'SQLITE_ERROR')
+ self.assertTrue(all(value['stacktrace']['frames'] for value in values))
+ self.assertNotIn('private', json.dumps(values))
+
+ def test_framework_frames_and_nested_peach_modules_are_locatable(self):
+ namespace = {'__name__': 'starlette.middleware.base'}
+ exec(compile('def fail():\n raise RuntimeError("private request")\n',
+ '/private/site-packages/starlette/middleware/base.py', 'exec'), namespace)
+ try:
+ namespace['fail']()
+ except RuntimeError as error:
+ values, _, _ = diagnostic_exceptions.project(error, lambda error: None)
+ frame = values[0]['stacktrace']['frames'][-1]
+ self.assertEqual(frame['module'], 'starlette.middleware.base')
+ self.assertEqual(frame['filename'], 'base.py')
+ self.assertNotIn('/private/', json.dumps(values))
+ with mock.patch.object(remote_reporting, 'send') as send:
+ try:
+ namespace['fail']()
+ except RuntimeError as error:
+ remote_reporting.capture('logging', error, logger='starlette')
+ send.assert_not_called()
+
+ def test_browser_type_frames_and_debug_id_are_projected_without_urls(self):
+ identity = 'fe664ca7-794e-4b41-841a-3106ec80fb1f'
+ with mock.patch.object(remote_reporting, 'send') as send:
+ remote_reporting.capture('browser', error_name='TypeError', message='private movie',
+ stack='at https://private/peach-app.js:12:2\nat https://private/peach-app.js:20:4',
+ bundle_debug_ids={'peach-app.js': identity, 'private.js': identity})
+ event = send.call_args.args[1]
+ self.assertEqual(event['platform'], 'javascript')
+ value = event['exception']['values'][0]
+ self.assertEqual(value['type'], 'TypeError')
+ self.assertEqual([frame['lineno'] for frame in value['stacktrace']['frames']], [20, 12])
+ self.assertEqual(event['debug_meta']['images'], [
+ {'type': 'sourcemap', 'code_file': 'app:///peach-app.js', 'debug_id': identity}])
+ self.assertNotIn('private', json.dumps(event))
+
+ def test_browser_untrusted_names_urls_ids_and_file_suffixes_are_rejected(self):
+ value, meta = diagnostic_browser.project({'error_name': 'private title',
+ 'stack': 'at https://private/not-peach-app.js:1:1\nat peach-pages.js:4:5',
+ 'bundle_debug_ids': {'peach-pages.js': 'https://private/'}})
+ self.assertEqual(value['type'], 'BrowserError')
+ self.assertEqual(len(value['stacktrace']['frames']), 1)
+ self.assertIsNone(meta)
+ self.assertIsNone(diagnostic_browser.clean_debug_meta({'images': [
+ {'type': 'sourcemap', 'code_file': 'https://private/app.js', 'debug_id': 'private'}]}))
+
+ def test_browser_debug_metadata_is_kept_in_final_sdk_bytes(self):
+ from sentry_sdk.transport import HttpTransport
+ identity = 'fe664ca7-794e-4b41-841a-3106ec80fb1f'
+ sent = []
+ self.enterContext(mock.patch.object(remote_reporting, '_disabled', False))
+ client = remote_reporting._new_client('https://[email protected]/456', envelope_gate=lambda envelope: True)
+ self.addCleanup(lambda: client.close(timeout=0))
+ with mock.patch.object(HttpTransport, '_send_envelope', side_effect=lambda envelope: sent.append(envelope.serialize())):
+ client.capture_event({'platform': 'javascript', 'debug_meta': {'images': [
+ {'type': 'sourcemap', 'code_file': 'app:///peach-app.js', 'debug_id': identity, 'private': 'secret'}]}})
+ client.flush(timeout=2)
+ self.assertIn(identity.encode(), sent[0])
+ self.assertNotIn(b'secret', sent[0])
+
+ def test_browser_event_position_is_usable_only_for_known_bundle_and_positive_coordinates(self):
+ value, _ = diagnostic_browser.project({'source_file': 'peach-pages.js', 'line': 8, 'column': 3})
+ self.assertEqual(value['stacktrace']['frames'][0]['filename'], 'peach-pages.js')
+ for source, line, column in [('private.js', 8, 3), ('peach-app.js', 0, 0), ('peach-app.js', True, 3)]:
+ value, _ = diagnostic_browser.project({'source_file': source, 'line': line, 'column': column})
+ self.assertEqual(value['stacktrace']['frames'], [])
+
+ def test_same_exception_trace_is_reported_once_across_hooks_and_a_new_raise_counts(self):
+ error = RuntimeError('private text')
+ with mock.patch.object(remote_reporting, 'send', return_value=True) as send:
+ remote_reporting.capture('background-job', error)
+ remote_reporting.capture('unhandled-main', error)
+ self.assertEqual(send.call_count, 1)
+ try:
+ raise error
+ except RuntimeError:
+ remote_reporting.capture('unhandled-main', error)
+ self.assertEqual(send.call_count, 2)
diff --git a/tests/test_reporting_transport.py b/tests/test_reporting_transport.py
index d3795dc55..ec6d05f28 100644
--- a/tests/test_reporting_transport.py
+++ b/tests/test_reporting_transport.py
@@ -21,6 +21,8 @@ class ReportingTransportTests(unittest.TestCase):
self.enterContext(mock.patch.object(reporting, "_disabled", False))
self.enterContext(mock.patch("peach.settings_file.active", return_value=self.config))
self.enterContext(mock.patch.dict(os.environ, {"PEACH_REPORTING_TEST_MODE": "0"}))
+ reporting.close()
+ self.addCleanup(reporting.close)
def envelope(self):
from sentry_sdk.envelope import Envelope
@@ -35,7 +37,6 @@ class ReportingTransportTests(unittest.TestCase):
return client
def test_actual_http_status_and_network_uncertainty_have_distinct_receipts(self):
- from sentry_sdk.transport import HttpTransport
for status, headers, expected in ((200, {}, "accepted"), (202, {}, "accepted"),
(200, {"x-sentry-rate-limits": "60:error:organization"}, "unknown"),
(429, {}, "failed"), (413, {}, "failed"), (500, {}, "failed")):
@@ -43,23 +44,24 @@ class ReportingTransportTests(unittest.TestCase):
outcomes = []
client = self.client(outcomes)
response = SimpleNamespace(status=status, headers=headers, close=mock.Mock(), data=b"private")
- with mock.patch.object(HttpTransport, "_request", return_value=response):
+ with mock.patch('urllib3.PoolManager.request', return_value=response) as request:
client.transport._send_envelope(self.envelope())
+ self.assertIs(request.call_args.kwargs['retries'], False)
+ self.assertIs(request.call_args.kwargs['redirect'], False)
self.assertEqual(outcomes[-1]["status"], expected)
self.assertEqual(outcomes[-1]["status_code"], status)
self.assertNotIn("private", str(outcomes))
outcomes = []
- with mock.patch.object(HttpTransport, "_request", side_effect=OSError("private proxy")):
+ with mock.patch('urllib3.PoolManager.request', side_effect=OSError("private proxy")):
with self.assertRaises(OSError):
self.client(outcomes).transport._send_envelope(self.envelope())
self.assertEqual(outcomes, [{"status": "unknown", "reason": "network"}])
def test_test_mode_blocks_the_base_http_request_even_with_a_valid_dsn(self):
- from sentry_sdk.transport import HttpTransport
outcomes = []
client = self.client(outcomes)
with mock.patch.dict(os.environ, {"PEACH_REPORTING_TEST_MODE": "1"}), \
- mock.patch.object(HttpTransport, "_request") as network:
+ mock.patch('urllib3.PoolManager.request') as network:
with self.assertRaises(OSError):
client.transport._send_envelope(self.envelope())
network.assert_not_called()
@@ -106,3 +108,41 @@ class ReportingTransportTests(unittest.TestCase):
mock.patch.object(reporting_identity, "frozen_build", return_value=SimpleNamespace(commit="bad")), \
mock.patch.dict(os.environ, {"PEACH_REPORTING_ENVIRONMENT": "production"}):
self.assertNotIn("dist", reporting_identity.event_metadata("research"))
+
+ def test_unwritable_local_receipt_does_not_block_event_submission(self):
+ reporting.save({"usage": True, "errors": True}, self.config)
+ client = mock.Mock()
+ with mock.patch.object(reporting, '_new_client', return_value=client), \
+ mock.patch.object(reporting_delivery, 'atomic_write_text', side_effect=PermissionError('read only')), \
+ mock.patch.object(reporting, 'dsn', return_value=DSN):
+ reporting._submit('usage', {'message': 'Peach started'}, self.config, reporting.consent(self.config), DSN)
+ client.capture_event.assert_called_once()
+ reporting.close()
+
+ def test_sdk_rate_limit_receipt_is_not_overwritten_with_unknown(self):
+ from datetime import datetime, timedelta, timezone
+ outcomes = []
+ client = self.client(outcomes)
+ client.transport._disabled_until[None] = datetime.now(timezone.utc) + timedelta(seconds=60)
+ with mock.patch('urllib3.PoolManager.request') as network:
+ client.transport._send_envelope(self.envelope())
+ network.assert_not_called()
+ self.assertEqual(outcomes, [{'status': 'failed', 'reason': 'rate_limit'}])
+
+ def test_consent_is_rechecked_inside_delivery_file_lock(self):
+ reporting.save({"usage": True, "errors": True}, self.config)
+ permit = {'kind': 'errors', 'revision': reporting.consent(self.config)['revision'],
+ 'consent': str(self.config.directory('state') / 'remote-reporting.json')}
+ original = reporting._consent
+ count = 0
+
+ def racing_consent(path):
+ nonlocal count
+ count += 1
+ if count == 2:
+ reporting.save({'usage': False, 'errors': False}, self.config)
+ return original(path)
+
+ with mock.patch.object(reporting, '_consent', side_effect=racing_consent):
+ reporting_delivery.record(permit, 'a' * 32, {'status': 'queued'})
+ self.assertEqual(reporting_delivery.snapshot(self.config), {})
diff --git a/web/dist/peach-app.js b/web/dist/peach-app.js
index 65bfddbdb..ae185f11b 100644
--- a/web/dist/peach-app.js
+++ b/web/dist/peach-app.js
@@ -2,7 +2,7 @@
(function() {
try {
var e = typeof window < "u" ? window : typeof global < "u" ? global : typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : {}, t = new e.Error().stack;
- t && (e._sentryDebugIds = e._sentryDebugIds || {}, e._sentryDebugIds[t] = "970d2958-dec1-4fc7-a87b-0bdba852d7c9", e._sentryDebugIdIdentifier = "sentry-dbid-970d2958-dec1-4fc7-a87b-0bdba852d7c9");
+ t && (e._sentryDebugIds = e._sentryDebugIds || {}, e._sentryDebugIds[t] = "18847759-1e92-447c-862a-d2db1459b8d7", e._sentryDebugIdIdentifier = "sentry-dbid-18847759-1e92-447c-862a-d2db1459b8d7");
} catch {}
})();
var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescriptor, r = Object.getOwnPropertyNames, i = Object.getPrototypeOf, a = Object.prototype.hasOwnProperty, o = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t.exports), s = (e, n) => {
@@ -75862,7 +75862,15 @@ function LIe({ initial: e, receipt: t }) {
}),
n.enabled && n.last_attempt?.status === "failed" ? /* @__PURE__ */ (0, D.jsx)(pA, {
divided: !0,
- children: /* @__PURE__ */ (0, D.jsx)(gA, { children: "馆藏与口味上传失败,自动重试。" })
+ children: /* @__PURE__ */ (0, D.jsx)(gA, { children: n.last_attempt.attempted ? "馆藏与口味上传失败,本周不再发送。" : "馆藏与口味上传未完成,将自动重新准备。" })
+ }) : null,
+ n.enabled && n.last_attempt?.status === "waiting-data" ? /* @__PURE__ */ (0, D.jsx)(pA, {
+ divided: !0,
+ children: /* @__PURE__ */ (0, D.jsx)(hA, { children: "等待可分析的馆藏或偏好数据。" })
+ }) : null,
+ n.enabled && n.last_attempt?.status === "unknown" ? /* @__PURE__ */ (0, D.jsx)(pA, {
+ divided: !0,
+ children: /* @__PURE__ */ (0, D.jsx)(hA, { children: "未取得上传结果,本周不再发送。" })
}) : null,
i ? /* @__PURE__ */ (0, D.jsxs)(pA, {
divided: !0,
@@ -114931,4 +114939,4 @@ Abt(jbt);
//#endregion
export { JT as applyTheme, $O as peachHistory, Go as popCount, igt as showToast };
-//# debugId=970d2958-dec1-4fc7-a87b-0bdba852d7c9
\ No newline at end of file
+//# debugId=18847759-1e92-447c-862a-d2db1459b8d7
\ No newline at end of file