Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): 资源监视器标题缓存按活跃任务剪枝,不再长驻积累

Zhang Yunjin3484f8a8e138
diff --git a/apps/desktop/src/main/process-monitor/__tests__/sampler.test.ts b/apps/desktop/src/main/process-monitor/__tests__/sampler.test.ts
index 4357b05702..c1c17d3871 100644
--- a/apps/desktop/src/main/process-monitor/__tests__/sampler.test.ts
+++ b/apps/desktop/src/main/process-monitor/__tests__/sampler.test.ts
@@ -297,6 +297,49 @@ describe('createProcessMonitorSampler', () => {
});
});
+ it('进程退出后剪枝对应任务的标题缓存', async () => {
+ const lookups: string[] = [];
+ const { state, sampler } = makeHarness({
+ snapshot: snapshotOf([
+ osRow({ pid: 921, ppid: SELF_PID, cmdLineLower: 'pi-marker host' }),
+ ]),
+ deps: {
+ resolveAgentProcessRegistration: () => ({
+ kind: 'pi',
+ role: 'task-host',
+ sessionId: 'session-7',
+ instanceId: 'inst:921',
+ }),
+ resolveSessionLabel: (sessionId) => {
+ lookups.push(sessionId);
+ return Promise.resolve('任务七');
+ },
+ },
+ });
+ await sampler.sample();
+ await Promise.resolve();
+ await Promise.resolve();
+ await Promise.resolve();
+ expect(entryByPid(await sampler.sample(), 921)).toMatchObject({ sessionLabel: '任务七' });
+ expect(lookups).toEqual(['session-7']);
+
+ // 进程退出后,缓存被剪枝;进程重新出现时重新后台解析。
+ // (快照按 5s 周期缓存,先推进时钟让换入的快照生效。)
+ state.nowMs += 5_000;
+ state.snapshot = snapshotOf([]);
+ await sampler.sample();
+ state.nowMs += 5_000;
+ state.snapshot = snapshotOf([
+ osRow({ pid: 922, ppid: SELF_PID, cmdLineLower: 'pi-marker host' }),
+ ]);
+ await sampler.sample();
+ expect(lookups).toEqual(['session-7', 'session-7']);
+ await Promise.resolve();
+ await Promise.resolve();
+ await Promise.resolve();
+ expect(entryByPid(await sampler.sample(), 922)).toMatchObject({ sessionLabel: '任务七' });
+ });
+
it('OS 扫描按周期缓存:未过期不重扫,过期后重扫', async () => {
const { state, sampler } = makeHarness();
await sampler.sample();
diff --git a/apps/desktop/src/main/process-monitor/sampler.ts b/apps/desktop/src/main/process-monitor/sampler.ts
index 4319fb4bb5..07bc5dbf94 100644
--- a/apps/desktop/src/main/process-monitor/sampler.ts
+++ b/apps/desktop/src/main/process-monitor/sampler.ts
@@ -203,6 +203,7 @@ export function createProcessMonitorSampler(
const { rows, childrenByParent } = cachedSnapshot;
const rowByPid = new Map(rows.map((r) => [r.pid, r]));
const entries: ProcessUsageEntry[] = [];
+ const liveSessions = new Set<string>();
for (const row of rows) {
if (row.ppid !== deps.selfPid) continue;
const kind = deps.classify(row.cmdLineLower);
@@ -223,11 +224,12 @@ export function createProcessMonitorSampler(
}
const terminable =
row.startIdentity != null && registrationMatchesKind && registeredRole === 'task-host';
+ const liveSessionId =
+ registrationMatchesKind ? registration?.sessionId ?? null : null;
// 标题解析在后台进行(不阻塞采样帧):首帧可能还没有,下一帧生效;
// 解析失败或任务已删只丢标题,不影响 CPU / 内存与终止授权。
- const sessionLabel = registrationMatchesKind && registration?.sessionId
- ? cachedSessionLabel(registration.sessionId)
- : null;
+ const sessionLabel = liveSessionId ? cachedSessionLabel(liveSessionId) : null;
+ if (liveSessionId) liveSessions.add(liveSessionId);
entries.push({
pid: row.pid,
kind: AGENT_KIND_TO_USAGE_KIND[kind],
@@ -242,6 +244,10 @@ export function createProcessMonitorSampler(
...(sessionLabel ? { sessionLabel } : {}),
});
}
+ // 剪枝:本帧仍存活的任务以外,标题缓存逐条清除,长驻应用不积累旧任务。
+ for (const sessionId of [...sessionLabels.keys()]) {
+ if (!liveSessions.has(sessionId)) sessionLabels.delete(sessionId);
+ }
return entries;
}