diff --git a/docs/README.md b/docs/README.md index dd8a41e..7d0fa31 100644 --- a/docs/README.md +++ b/docs/README.md @@ -10,7 +10,7 @@ | [monitoring.md](monitoring.md) | 监控指标清单与底层系统 API 技术方案 | | [testing-capabilities.md](testing-capabilities.md) | 当前智能体测试能力实测、环境限制与验证边界 | | [development.md](development.md) | 开发环境搭建、构建调试、测试 Target 添加与协作规范 | -| [unified-panel-plan.md](unified-panel-plan.md) | 新一轮统一面板设计审评、总体方案与 P0–P6 实施计划(尚未实施) | +| [unified-panel-plan.md](unified-panel-plan.md) | 新一轮统一面板设计审评、总体方案与 P0–P6 实施计划(实施中) | | [implementation-plan.md](implementation-plan.md) | 既有 A–F 方案与历史实施记录 | | [settings-brand-plan.md](settings-brand-plan.md) | 设置窗口展示、系统式侧边栏与分组表单、品牌图标实施记录 | | [stage-a-validation.md](stage-a-validation.md) | A 阶段采样、资源及 UI 验证记录 | @@ -29,3 +29,5 @@ - [B 阶段专项验证留档](stage-b-followups.md) - [磁盘 SMART 只读辅助服务实施方案](disk-smart-helper-plan.md) + +- [统一面板实施验证](unified-panel-validation.md) diff --git a/docs/architecture.md b/docs/architecture.md index 1e1d532..f6bd065 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -168,3 +168,7 @@ Debug 清理构建通过。macOS 27 独立构建副本实测菜单栏显示 `Bra 2026-11-02 新增 [统一面板设计审评与实施方案](unified-panel-plan.md)。规划以同一 MenuBarExtra 面板承载资源卡片与进程表,取代外侧悬浮详情;新增网络地址/接口选择、进程采样与可见性调度、GPU 能力分级和双向吞吐历史。Features/MenuBar 承载展示与窗口桥接,Core 接收可见性值并负责采样/缓存,UI 组件仍只接收数据;SMARTService 不扩展通用查询权限。 以上均未落地,本文前述 320pt 面板、串行采样与历史结构仍为当前代码事实。各阶段实施后再更新对应架构、并发及数据流章节,不能将概念图字段视为已实现能力。 + +### 统一面板 P0 探针(已实现) + +SystemSampler 在首次采样前仅当 `KYMO_PANEL_PROBE=1` 执行一次 PanelCapabilityProbe;探针通过 sysctl/proc、getifaddrs、IORegistry 记录覆盖计数、错误码、GPU 原始统计和地址族数量,不输出路径/IP 原文。开启时另输出采样耗时;普通运行不执行系统探测或诊断输出。进程/GPU 尚未接入正式快照;实测边界见 [统一面板验证](unified-panel-validation.md)。 diff --git a/docs/unified-panel-plan.md b/docs/unified-panel-plan.md index 971323d..d28a737 100644 --- a/docs/unified-panel-plan.md +++ b/docs/unified-panel-plan.md @@ -1,6 +1,6 @@ # 统一监控面板:设计审评、总体方案与实施计划 -日期:2026-11-02。状态:方案草案已完成,实施尚未开始。 +日期:2026-11-02。状态:已进入实施,P0 能力探针完成,后续阶段推进中。 本文依据 [深色概念图](design/unified-panel-dark-v2.png)、[浅色概念图](design/unified-panel-light-v2.png) 及当前代码编写。概念图确定信息层级与视觉方向,不作为指标真实性、像素尺寸或平台能力的证明。本文是下一轮统一面板工作的实施入口;旧 A–F 记录继续作为已实现能力和验证证据保留。 @@ -149,7 +149,7 @@ GPU:验证总体利用率、拓扑、逐核计数的实际返回、单位和 ## 7. 实施阶段与退出条件 -以下工期仅以完成证据驱动,不承诺固定天数。当前除本文档评审外,所有实施阶段均未开始。 +以下工期仅以完成证据驱动,不承诺固定天数。P0 本机能力验证结果见 [实施验证记录](unified-panel-validation.md),其余阶段按下表推进。 | 阶段 | 工作与产物 | 退出条件 | 依赖 | | --- | --- | --- | --- | @@ -220,6 +220,7 @@ xcodebuild -project kymo.xcodeproj -scheme kymo -configuration Debug build - [x] 审阅深浅概念图、当前调用链、模型/采样与现有验证边界。 - [x] 形成总体实现方案、设计修订与 P0–P6 实施/验收计划。 -- [ ] P0–P6 代码、探针与真实 UI 实施,尚未开始。 +- [x] P0 首轮签名沙盒探针:sysctl 进程路线成立,GPU 总体/拓扑和地址可读,每核独立数据未获得。 +- [ ] P1–P6 实施与完整验收。 -本轮仅产出文档,不声称新增系统能力已通过。每阶段追加实际决策、验证命令/结果、剩余限制;架构文档继续描述当前代码事实,新增规划明确标注。对应监控指标状态与路线图随实现同步更新,旧验收证据不得直接当作新面板已通过。 +能力探针和实测边界见 [实施验证记录](unified-panel-validation.md),不以原始字段可读代替正式 UI 与完整能力验收。每阶段追加实际决策、验证命令/结果、剩余限制;架构文档继续描述当前代码事实,新增规划明确标注。对应监控指标状态与路线图随实现同步更新,旧验收证据不得直接当作新面板已通过。 diff --git a/docs/unified-panel-validation.md b/docs/unified-panel-validation.md new file mode 100644 index 0000000..4bad44e --- /dev/null +++ b/docs/unified-panel-validation.md @@ -0,0 +1,21 @@ +# 统一面板实施验证 + +## P0:能力探针(2026-11-02) + +在实际 Debug 主应用中显式设置 `KYMO_PANEL_PROBE=1 KYMO_DIAGNOSTICS=1`,直接运行临时构建包可执行文件;同源码非沙盒 CLI 仅作对照。签名 entitlement 为 `com.apple.security.app-sandbox=true`,未扩展权限或 SMART 服务。测试前未发现已有 Kymo 可执行进程;仅终止本轮测试 PID 8293(首轮 8133)。探针不记录可执行路径和 IP 原文,不落盘缓存,普通运行不执行。 + +| 项目 | 主应用实测 | 决策 / 边界 | +| --- | --- | --- | +| 进程枚举 | `proc_listpids` EPERM;`sysctl(KERN_PROC_ALL)` 数据读取成功,825 项 | 正式采样采用 sysctl;不能把前一接口失败等同全部不可用 | +| 本用户进程 | 除自身外 525 项,CPU 累计计时、RSS、线程、启动标识全可读,524 项路径可读 | 进程路线成立;路径独立降级,数量仅为当次快照 | +| 其他用户进程 | 299 项,297 项 EPERM、2 项 ESRCH;路径 297 项可读 | 显示枚举数 / 可读数,保留受限行;非沙盒 CLI 同样受限,不引入提权 | +| GPU | IOAccelerator PerformanceStatistics 返回 Device Utilization %(本轮 13)、Renderer 11、Tiler 6;gpu-core-count=20 | 总体原始字段路线可用;正式接入需验证范围与缺失;本轮不把 Renderer/Tiler 当核心 | +| GPU 每核 | 当前注册表统计无独立每核字段 | 显示独立“不支持/未获得每核数据”状态;未重新验证 IOReport,不宣称所有硬件均不支持 | +| 本机网络地址 | getifaddrs 成功,en0 IPv4/IPv6,utun 等多接口可读 | 采用本机接口地址,不请求公网 IP 服务 | +| 快速链路基线 | 6 次:189.00、8.16、6.37、8.28、7.54、7.74ms | 首次含低频查询;不是最坏延迟证明,慢项隔离仍须实施 | + +非沙盒 CLI 同次枚举 822 项、本用户 524 项计时等可读;与沙盒查询非同时,不能逐项按数量差判失败。IORegistry 字段为驱动接口,不能承诺跨机型稳定;每次检查类型与有限数值,缺失即降级。 + +Debug 构建通过;默认执行沙盒阻止 Swift 宏插件,审批后构建成功,应用沙盒始终开启。现有 SamplingChecks 通过 CPU 差分、真实 CPU/内存/负载、历史边界、暂停冻结与恢复基线检查。无 Xcode 测试 Target,未执行 xcodebuild test。 + +P0 不包含 UI、可控 GPU 负载关联、进程 CPU 差分或最坏延迟测量;后续阶段继续验证。未改变现有正式面板;长期 CPU、内存和能耗未验收。 diff --git a/kymo/Core/Monitors/SystemSampler.swift b/kymo/Core/Monitors/SystemSampler.swift index de837ec..3d36e41 100644 --- a/kymo/Core/Monitors/SystemSampler.swift +++ b/kymo/Core/Monitors/SystemSampler.swift @@ -4,6 +4,7 @@ import Darwin /// Serial ownership keeps CPU baselines coherent and system calls off MainActor. actor SystemSampler { private var cpu = CPUMonitor() + private var didProbe = false private let diskHealth = DiskHealthMonitor() private let batteryHealth = SystemBatteryHealthMonitor() @@ -16,6 +17,13 @@ actor SystemSampler { func reset() { cpu = CPUMonitor(); network = NetworkMonitor(); diskIO = DiskIOMonitor() } func sample() async -> MetricsSnapshot { + if !didProbe { + didProbe = true + if ProcessInfo.processInfo.environment["KYMO_PANEL_PROBE"] == "1" { + PanelCapabilityProbe.run() + } + } + let sampleStart = ProcessInfo.processInfo.systemUptime var result = MetricsSnapshot() result.cpu = cpu.sample() result.memory = MemoryMonitor.sample() @@ -35,6 +43,10 @@ actor SystemSampler { result.uptime = ProcessInfo.processInfo.systemUptime var load = [Double](repeating: 0, count: 3) if getloadavg(&load, 3) == 3 { result.load = load } + if ProcessInfo.processInfo.environment["KYMO_PANEL_PROBE"] == "1" { + let line = "{\"event\":\"sampleDuration\",\"milliseconds\":\((ProcessInfo.processInfo.systemUptime - sampleStart) * 1000)}\n" + try? FileHandle.standardOutput.write(contentsOf: Data(line.utf8)) + } return result } } diff --git a/kymo/Core/Services/PanelCapabilityProbe.swift b/kymo/Core/Services/PanelCapabilityProbe.swift new file mode 100644 index 0000000..9a3723e --- /dev/null +++ b/kymo/Core/Services/PanelCapabilityProbe.swift @@ -0,0 +1,134 @@ +import Foundation +import Darwin +import IOKit + +/// One-shot, explicit local diagnostics. Never runs during ordinary sampling. +/// Reports coverage and errors without logging process paths or network addresses. +nonisolated enum PanelCapabilityProbe { + static func run() { + let start = ProcessInfo.processInfo.systemUptime + var result: [String: Any] = ["event": "panelCapabilities", "pid": getpid()] + errno = 0 + let required = proc_listpids(UInt32(PROC_ALL_PIDS), 0, nil, 0) + result["enumerationErrno"] = errno + var pids = [Int32](repeating: 0, count: max(1, Int(required) / MemoryLayout.size + 256)) + errno = 0 + let capacity = Int32(pids.count * MemoryLayout.size) + let bytes = pids.withUnsafeMutableBytes { proc_listpids(UInt32(PROC_ALL_PIDS), 0, $0.baseAddress, capacity) } + result["listErrno"] = errno + var listed = bytes > 0 ? Array(pids.prefix(Int(bytes) / MemoryLayout.size)).filter { $0 > 0 } : [] + result["enumerated"] = listed.count + var mib: [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0] + var processBytes = 0 + errno = 0 + let sysctlStatus = sysctl(&mib, u_int(mib.count), nil, &processBytes, nil, 0) + result["sysctlProcessStatus"] = sysctlStatus + result["sysctlProcessErrno"] = errno + result["sysctlProcessRequiredBytes"] = processBytes + var owners: [Int32: uid_t] = [:] + if sysctlStatus == 0 { + for _ in 0..<3 { + var entries = [kinfo_proc](repeating: kinfo_proc(), count: processBytes / MemoryLayout.stride + 256) + var actualBytes = entries.count * MemoryLayout.stride + errno = 0 + let status = entries.withUnsafeMutableBytes { sysctl(&mib, u_int(mib.count), $0.baseAddress, &actualBytes, nil, 0) } + result["sysctlDataStatus"] = status + result["sysctlDataErrno"] = errno + if status == 0 { + for entry in entries.prefix(actualBytes / MemoryLayout.stride) where entry.kp_proc.p_pid > 0 { + owners[entry.kp_proc.p_pid] = entry.kp_eproc.e_ucred.cr_uid + } + result["sysctlEnumerated"] = owners.count + listed = Array(Set(listed).union(owners.keys)) + break + } + if errno != ENOMEM { break } + processBytes = max(actualBytes, processBytes * 2) + } + } + result["possiblyTruncated"] = bytes == capacity + // Always probe self and launchd even when enumeration is denied. + var groups: [String: [String: Int]] = [:] + for pid in Set(listed + [getpid(), 1]).sorted() { + var info = proc_taskallinfo() + errno = 0 + let read = proc_pidinfo(pid, PROC_PIDTASKALLINFO, 0, &info, Int32(MemoryLayout.size(ofValue: info))) + let readError = errno + let valid = read == MemoryLayout.size(ofValue: info) + let owner = valid ? info.pbsd.pbi_uid : owners[pid] + let group = pid == getpid() ? "self" : (owner.map { $0 == getuid() ? "sameUser" : "otherUser" } ?? "unknownOwner") + var counts = groups[group, default: [:]] + counts["attempted", default: 0] += 1 + counts[valid ? "taskAndIdentityReadable" : "taskFailed", default: 0] += 1 + if !valid { counts["taskErrno_\(readError)", default: 0] += 1 } + if valid { + counts["rssReadable", default: 0] += 1 + counts["threadsReadable", default: 0] += 1 + counts["cpuCountersReadable", default: 0] += 1 + if info.pbsd.pbi_start_tvsec > 0 { counts["startIdentityReadable", default: 0] += 1 } + } + var bsd = proc_bsdinfo() + errno = 0 + let bsdBytes = proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &bsd, Int32(MemoryLayout.size(ofValue: bsd))) + counts[bsdBytes == MemoryLayout.size(ofValue: bsd) ? "bsdReadable" : "bsdFailed", default: 0] += 1 + if bsdBytes != MemoryLayout.size(ofValue: bsd) { counts["bsdErrno_\(errno)", default: 0] += 1 } + var task = proc_taskinfo() + errno = 0 + let taskBytes = proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &task, Int32(MemoryLayout.size(ofValue: task))) + counts[taskBytes == MemoryLayout.size(ofValue: task) ? "taskOnlyReadable" : "taskOnlyFailed", default: 0] += 1 + if taskBytes != MemoryLayout.size(ofValue: task) { counts["taskOnlyErrno_\(errno)", default: 0] += 1 } + var path = [CChar](repeating: 0, count: 4 * Int(MAXPATHLEN)) + errno = 0 + let pathBytes = proc_pidpath(pid, &path, UInt32(path.count)) + counts[pathBytes > 0 ? "pathReadable" : "pathFailed", default: 0] += 1 + if pathBytes <= 0 { counts["pathErrno_\(errno)", default: 0] += 1 } + groups[group] = counts + if pid == 1 { result["launchdTaskBytes"] = read; result["launchdTaskErrno"] = readError } + } + result["processCoverage"] = groups + + var addresses: [String: [String: Int]] = [:] + var head: UnsafeMutablePointer? + errno = 0 + let addressStatus = getifaddrs(&head) + result["addressStatus"] = addressStatus + result["addressErrno"] = errno + if addressStatus == 0 { + defer { freeifaddrs(head) } + var cursor = head + while let entry = cursor { + defer { cursor = entry.pointee.ifa_next } + guard let address = entry.pointee.ifa_addr else { continue } + let family = Int32(address.pointee.sa_family) + guard family == AF_INET || family == AF_INET6 else { continue } + let name = String(cString: entry.pointee.ifa_name) + addresses[name, default: [:]][family == AF_INET ? "ipv4" : "ipv6", default: 0] += 1 + } + } + result["addressFamiliesByInterface"] = addresses + + var iterator: io_iterator_t = 0 + let gpuStatus = IOServiceGetMatchingServices(kIOMainPortDefault, IOServiceMatching("IOAccelerator"), &iterator) + result["gpuRegistryStatus"] = gpuStatus + var accelerators: [[String: Any]] = [] + if gpuStatus == KERN_SUCCESS { + defer { IOObjectRelease(iterator) } + while true { + let service = IOIteratorNext(iterator) + guard service != 0 else { break } + defer { IOObjectRelease(service) } + let stats = IORegistryEntryCreateCFProperty(service, "PerformanceStatistics" as CFString, kCFAllocatorDefault, 0)?.takeRetainedValue() as? [String: Any] + var item: [String: Any] = ["performanceKeys": stats?.keys.sorted() ?? []] + // Raw values only: no claim about units or independent core counters. + item["numericStatistics"] = stats?.filter { $0.value is NSNumber } + item["gpuCoreCount"] = IORegistryEntryCreateCFProperty(service, "gpu-core-count" as CFString, kCFAllocatorDefault, 0)?.takeRetainedValue() as? NSNumber + accelerators.append(item) + } + } + result["accelerators"] = accelerators + result["elapsedMilliseconds"] = (ProcessInfo.processInfo.systemUptime - start) * 1000 + if let data = try? JSONSerialization.data(withJSONObject: result, options: [.sortedKeys]) { + try? FileHandle.standardOutput.write(contentsOf: data + Data([10])) + } + } +}