Repository navigation
perf: agent 多时 CPU 高、灵动岛卡顿——Cursor 子任务识别移出主线程,会话发现去掉重复 stat - #374
Conversation
… actor rebrandMisattributedCursorTaskCards() resolved every default-Claude card by walking every ~/.cursor/projects/*/agent-transcripts tree with a recursive FileManager enumerator. It runs synchronously on the main actor from handleEvent (every hook), integrateDiscovered (every discovery scan) and restore, with no cache, and a miss walks every tree. With merge/hide Agent Sub-Sessions and a large Cursor history this pinned the main thread: ~80% of main-thread samples were inside this walk, and the island stuttered. Walk the store once, off the main actor, into a CursorSubagentTranscriptIndex (child session id -> transcript paths, same `subagents/<id>.jsonl` match at any depth, still preferring the card's own cwd project) and look cards up in it. A card the index does not resolve asks for a throttled rebuild: right away when first seen, once more after 5 s in case its transcript lands just after its first hook, then at most every 60 s when an event brings it back. When a rebuild resolves a card it is rebranded and folded then. Cards whose transcript_path already names a Cursor transcript are still rebranded synchronously, as before. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
claudeSession(for:root:excluding:) listed its process's project dir and called attributesOfItem on every .jsonl in it, once per Claude process. Processes in the same cwd share a project dir, so with a dozen terminals in ~ and ~80 transcripts there, every discovery scan (every ~2 s while agents write) cost ~1,000 stats of the same files. Read the dir's .jsonl names and modification dates in one bulk listing (contentsOfDirectory with .contentModificationDateKey) and memoize it per project dir for the duration of one scan. Selection is unchanged: the newest transcript modified after the process started, within the same freshness window. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
contentsOfDirectory(at:includingPropertiesForKeys:) fails (Cocoa error 256) when the last path component is a symlink to a directory, where the contentsOfDirectory(atPath:) it replaced follows it. A project dir under ~/.claude/projects that is itself a symlink (history kept across a moved checkout, say) would silently stop yielding sessions. Resolve the dir before listing; entries keep lstat semantics, so a symlinked transcript still reports its own mtime as the per-file stat did. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The walk and its re-check timer were declared nonisolated(unsafe), the convention here for tasks cancelled from deinit, but nothing cancelled them: a walk in flight when stopSessionDiscovery() ran (app quit, the debug harness's idle preview) still installed its index and could rebrand and fold cards afterwards. Cancel both in stopSessionDiscovery() and deinit, as discoveryScanTask is, and have the walk drop its result once cancelled. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test passed whatever the preference did: APFS lists b-own before a-other, so paths.first was already the expected path. Also look up with the other project preferred, so the listing order cannot decide. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A remote card's transcripts live on its own host, so the local index can never resolve it; yet every remote Claude card counted as unresolved and kept the walks over ~/.cursor/projects coming. Leave remote cards out of the index lookup and its candidates. The synchronous rebrand from a Cursor transcript_path, a string check that needs no local disk, still applies to them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Every real Claude card stays unresolved, so the open-ended refresh re-walked ~/.cursor/projects every 60 s for as long as any Claude session was open and events kept coming, while a Task transcript that landed 5-60 s after the first hook waited for that 60 s window. Each card the index does not resolve now gets its own schedule: a walk 0, 5, 15, 45 and 120 s after it first goes unresolved, driven by the re-check timer rather than by hook traffic, and none after that. A hook that later brings a Cursor transcript_path still rebrands the card synchronously. Still at most one walk and one timer pending. A walk counts only for the lookups that were due when it started: a card first seen while a walk was already running keeps its immediate lookup, a walk some other card asked for does not use up this one's next delay, and a late walk (after sleep, say) covers every lookup due by then. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…gain Separate folds nothing, but the re-check timer and a walk's completion kept scheduling walks whatever the mode, and a card whose lookups were used up before a switch to separate stayed unresolved after switching back to merge or hide, though its transcript may have landed meanwhile. Scheduling now stands down outside merge and hide, dropping every card's schedule, and applyCurrentPluginSessionMode drops them on entering separate too, so coming back looks every default-Claude card up at once. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Only one test still called findCursorSubagentTranscriptPath, and since the index it had become a full walk of the store per lookup. The test now scans with CursorSubagentTranscriptIndex and prefers the project from cursorProjectTranscriptBase, with the same Task id under two projects so that the card's cwd, not the directory listing order, picks the path. installCursorSubagentIndex is only called from the walk, so it is private, and a test message that still described the old re-check interval now names the lookup delay. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@x4vierli 感谢这个 PR!分析写得很扎实( 我们做的验证:
我们在你的两个提交之上加了 7 个提交(已推到这个分支后合并),每个新测试都确认过去掉对应改动就会失败: 修复:
重查策略: 以后可以考虑的方向(不在这次范围内):现有的 FSEvents 监听已经在看 PR 描述最后提到的两个打包问题( |
…jumps land on each session's pane (#373) Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cursor runs the hooks in ~/.claude/settings.json as its own ("Include
third-party Plugins, Skills, and other configs", on by default) and hands
each one Cursor's payload, cursor_version included. CodeIsland's hook there
has no --source and Cursor's processes are never inferred from the ancestry
(#220), so Cursor's agent showed up on the island as Claude (#377).
The bridge now reads cursor_version as Cursor declaring itself: cursor, or
cursor-cli under cursor-agent, and never a _via_plugin proxy. No ancestry
rule stands in for it: npm Claude Code runs as `node`, so in Cursor's
terminal its hooks have the same ancestors as a hook Cursor runs itself.
beforeSubmitPrompt and stop are wired in both of CodeIsland's configs, and
Cursor ran both hooks with one payload: every prompt and every finished turn
chimed twice. The bridge stamps a digest of Cursor's stdin and the island
lets one copy through. A hook Cursor runs never waits on the island or
prints its answer either: Cursor imports no PermissionRequest or
Notification hook and reads stdout against its own schema.
A Task's own hooks carry no transcript_path, and the Task transcript
index (#374) only looked up default-Claude cards; it now also looks up
local Cursor cards without one, so relabelled Task cards still fold. A new
chat's sessionStart arrives before Cursor has its transcript, so session
start and end no longer take the _ppid fallback, which could fold the new
chat into a sibling as a Task that never finishes.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
问题
同时开着较多 agent(本地 + 远程)时,CodeIsland 的 CPU 占用很高(
top里最高到 140%),灵动岛悬停、展开时明显卡顿。原因
用
sample采样正在运行的 1.0.38,主线程 80% 以上的样本都在同一条调用链里:rebrandMisattributedCursorTaskCards()对每张 source 为 Claude(或空)的卡片调用一次findCursorSubagentTranscriptPath。后者用递归 enumerator 遍历~/.cursor/projects/*/agent-transcripts找subagents/<id>.jsonl:先找卡片 cwd 对应的项目,找不到再把所有项目都遍历一遍。nonisolated static,但调用方是@MainActor。只要有 Claude 卡片,每个 hook 事件、每次会话发现扫描(FSEvents,agent 活跃时约每 2 秒一次)、Codex Desktop 轮询都会触发它。测试机上
~/.cursor/projects有 187 个项目目录、49 棵 agent-transcripts 树、约 7,200 个条目(424 MB)。8 张 Claude 卡片 → 每次调用约 390 次树遍历;遍历一遍全部目录实测 0.15 s(热缓存)到 0.65 s(冷缓存)。修掉这个之后再采样,剩下最大的一块在后台的会话发现:
claudeSession(for:root:excluding:)对每个 Claude 进程,都把它项目目录里的每个.jsonl单独attributesOfItem一次。同一 cwd 的进程共用一个项目目录。测试机上有 13 个 Claude 进程的 cwd 都是~,那个目录里有 78 个 transcript,所以每轮扫描约 1,000 次 stat,同一个目录被列了 13 遍。改了什么
两个独立的 commit:
1.
perf(cursor):Cursor 子任务识别改查后台建的索引新增
CursorSubagentTranscriptIndex(CodeIslandCore):遍历一次~/.cursor/projects,建立「子任务 session id → transcript 路径」的索引。匹配规则不变:任意深度下的subagents/<id>.jsonl,多个项目都有时优先卡片 cwd 对应的项目。rebrandMisattributedCursorTaskCards()只查索引,主线程不再碰磁盘。索引在后台(
.utility)重建,并做了节流:后台查到后,再做 rebrand 和折叠(
installCursorSubagentIndex)。hook 的
transcript_path已经指向 Cursor transcript 的卡片,仍然同步 rebrand,行为不变。仿照
discoveryScanner加了cursorSubagentIndexScanner,测试进程默认不扫真实的 home 目录。findCursorSubagentTranscriptPath保留,改为基于同一套扫描和查找,原有测试不用改。2.
perf(discovery):每轮扫描,每个 Claude 项目目录只列一次claudeTranscriptFiles(in:fm:):用contentsOfDirectory(at:includingPropertiesForKeys: [.contentModificationDateKey])一次拿到文件名和修改时间,不再逐个 stat。findActiveClaudeSessions在一轮扫描内按项目目录缓存列表,同 cwd 的进程共用。实测效果
测试环境:
测量方法:
ps读两次 CPU 时间,取 60 s 内的差值;sample采 10 s,统计不在 runloop 等待的样本比例。两个版本在同一时段、同为 18 张卡片、都刚重启的情况下对比:
rebrandMisattributedCursorTaskCards剩下约 17% 的 CPU 主要来自这几处,不在本 PR 范围内:
cleanupIdleSessions;TimelineView的逐帧刷新。验证
新增 9 个测试:
CursorSubagentTranscriptIndexTests(5 个):agent-transcripts/subagents/下的文件;AppStateCursorSubsessionTests(新增 2 个):transcript_path的误标卡片,由后台索引完成 rebrand 和折叠,且扫描不在主线程;ClaudeTranscriptFilesTests(2 个):attributesOfItem的结果一致;已有测试:
swift test:CodeIslandTests 1291 个(5 个 skipped),CodeIslandCoreTests 893 个,0 失败。实机运行:打包后替换 1.0.38,在测试机上跑了 30 多分钟。hook 事件接收、会话标题、进程绑定、
sessions.json持久化、远程 SSH 隧道、刘海显示都正常。已知影响
transcript_path的 Cursor 子任务卡片。它们现在要等后台索引建好才 rebrand 和折叠,通常在 1 秒内;以前是同步立即完成,代价是卡住主线程。如果这类卡片的 transcript 在第一个 hook 之后超过约 5 秒才写出来,最晚要等到下一个事件、且离上次遍历满 60 秒时才会被识别。~/.cursor/projects:每张新卡片最多触发两次,之后只在有事件时最多每分钟一次,都不在主线程。顺带发现(本 PR 没有修改)
用 Swift 6.4 / Xcode 27 在本地打包时,
swift build默认使用 swift-build 后端,会遇到两个问题:.build/out/Products/Release/,build.sh写死的.build/arm64-apple-macosx/release/找不到它,打包失败;LC_BUILD_VERSION里的sdk被写成 14.0(等于 minos),macOS 会按旧版兼容模式渲染,设置窗口的侧栏和标题栏会错位。加
--build-system native可以同时避开这两个问题。CI 用的是 Xcode 26(官方 1.0.38 二进制的 sdk 是 26.2),不受影响。🤖 Generated with Claude Code