/** * ============================================================ * telemetryLog.ts —— Computer Use 遥测的落地点 * ------------------------------------------------------------ * `vision/telemetry.ts` 是**纯逻辑**(白名单过滤、连续失败计数、汇总); * 本模块是它与真实文件系统之间那一层:决定写哪个文件、失败怎么办、 * 以及从多少条记录里做判断。 * * 为什么要分两层:纯逻辑那层要能在单测里对着内存数组跑,不该知道 * `~/Library/Application Support` 存在;而路径与容量策略是部署问题, * 不该混进算法里。 * * ★ 遥测的职责是**观测**:验证两个假设(见 vision/telemetry.ts 头注)。 * 它**不承担运行时安全职责** —— 连续失败熔断曾经建在这个文件上,那是 * 个错误:全机唯一的日志没有会话隔离(A 的失败熔断 B)、读改写有竞态、 * 而且计数不分动作(一次成功 observe 就清零,于是「失败→观察→失败」 * 这个最常见的打转模式可以无限绕过熔断)。熔断现在住在 * `ComputerUseService`,按运行 + 动作指纹分桶,同步无 IO。 * * 隐私:写盘前必经 `sanitizeRecord` 白名单,只留命令名、耗时、枚举值、 * 比例。屏幕内容、窗口标题、输入文本一律进不来 —— 那是白名单的实现保证, * 不是调用方的自觉。 * ============================================================ */ import { join } from 'node:path'; import { computerUseStateDir } from './computerUseService.js'; import { CONSECUTIVE_FAILURE_LIMIT, readRecords, record, renderReport, summarize, } from './vision/telemetry.js'; function logPath(): string { return join(computerUseStateDir(), 'telemetry.jsonl'); } function exposureLogPath(): string { return join(computerUseStateDir(), 'exposure.jsonl'); } /** * 记一条调用元数据。 * * **永不抛异常、永不阻断主流程**:遥测是观测手段,不是功能。日志写不进去 * (磁盘满、目录只读)时宁可少一条记录,也不能让一次正常的桌面操作失败。 */ export async function recordComputerUseCall(raw: Record): Promise { try { await record(logPath(), raw); } catch { /* 观测失败不影响功能 */ } } /** Records only the boolean exposure decision and a bounded enum source. */ export async function recordComputerUseExposure( exposed: boolean, intentSource: 'explicit-current' | 'continuation' | 'none', ): Promise { try { await record(exposureLogPath(), { exposed, intentSource }); } catch { /* observability must never affect a turn */ } } export { CONSECUTIVE_FAILURE_LIMIT }; /** * 人类可读的遥测汇总,给 CLI/诊断用。 * * 这是 SPEC §9 声称的「验证两个假设」真正的出口 —— 在此之前遥测模块整个 * 没有生产调用方,汇总函数写了却没人能看到结果。 */ export async function readComputerUseTelemetryReport(): Promise { const records = await readRecords(logPath()).catch(() => []); if (records.length === 0) return '还没有 Computer Use 遥测记录。'; return renderReport(summarize(records)); }