Skip to content

perf(pi): 缓存超限包结果并回收会话临时目录 - #3524

Open
lizzjin wants to merge 5 commits into
makecindy:mainfrom
lizzjin:codex/issue-3468
Open

perf(pi): 缓存超限包结果并回收会话临时目录#3524
lizzjin wants to merge 5 commits into
makecindy:mainfrom
lizzjin:codex/issue-3468

Conversation

@lizzjin

@lizzjin lizzjin commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

这次改了什么

摘要

修复新建 Pi 会话时反复检查、复制和指纹计算大型 Pi 包的问题,并加强会话临时目录的生命周期管理。

确定性的 package inspection / snapshot 限制结果现在会跨会话缓存;安装内容发生变化时自动失效。Pi 启动链路增加分阶段、可关联且脱敏的耗时日志,方便区分 package、MCP、进程启动、RPC ready 和首个模型请求的耗时。

会话仍保留私有、已校验的 ripgrep 副本作为安全边界,但通过带进程实例身份的 owner marker 和保守的遗留目录扫描,避免 run-tmp 长期堆积。

变更类型

  • feat 新功能
  • fix 缺陷修复
  • refactor / perf 重构或性能优化
  • docs / test / chore 文档、测试或工程维护
  • 其他:

范围

  • 关联 Issue / 需求:Closes 新建 PI 会话会重复 snapshot 大型 Pi 包并复制 rg.exe,新会话变慢40秒且残留堆积 #3468
  • 本 PR 包含:
    • 将确定性的 Pi package inspection / snapshot 限制结果持久化并跨会话复用。
    • 使用安装目录信息和有界 package.json 哈希构建 installation identity;包原地升级或 manifest 变化后自动重新检查。
    • 为 package list、inspection、compatibility、fingerprint、snapshot 增加结构化耗时日志。
    • 为 MCP ready、Pi spawn、RPC ready 和首次模型请求增加同一 startupTraceId 下的启动耗时日志。
    • 将本地 Pi config-home owner marker 升级为包含 PID、进程启动时间、runtime ID 和 session hash 的 v2 格式,同时兼容 v1。
    • 识别 PID 复用;仅在全系统扫描能确认不存在 Cindy 管理的 Pi 进程时回收 markerless 遗留目录。
    • 保持远程 config-home 不参与本地清理。
    • 修复 Windows 下 Bash 反斜杠符号链接测试夹具。
    • 修复 OAuth 测试 helper 吞掉 loopback callback 错误后只能等待 20 秒超时的问题;不改变产品 OAuth 实现。
  • 明确不包含:
    • 不改为全局共享或硬链接 ripgrep;本 PR 继续使用每会话私有副本,并保证其生命周期可回收。
    • 不修改 Pi package 的权限、安全边界或用户批准语义。
    • 不修改远程会话的 config-home 生命周期。
    • 不包含 UI、服务端或跨端协议变更。
  • 用户可见变化:
    • 大型 Pi 包首次触发确定性限制后,后续新会话不会重复遍历、复制和计算整棵目录。
    • 已关闭或崩溃会话留下的本地 Pi 临时目录可被安全回收。
    • 启动性能问题可通过结构化日志定位,不记录路径、会话 ID、prompt 或包来源等敏感内容。
  • 是否存在 breaking change:无。

UI 变化

不涉及:本 PR 仅修改 Desktop main 进程、maker-core 和测试,不改变界面、交互或 UI 文案。

  • 引用的设计规范:不涉及。

怎么验证的

自动验证

pnpm --filter desktop exec vitest run src/main/maker-host/__tests__/pi-package-store-security.test.ts
结果:84 passed,4 个平台条件用例 skipped

pnpm --filter desktop exec vitest run src/main/process-monitor/__tests__/agent-scan.test.ts
结果:11 passed

pnpm --filter @cindy/maker-core exec vitest run src/agents/pi/__tests__/pi-startsession-cleanup.test.ts
结果:69 passed

pnpm --filter @cindy/maker-core exec vitest run src/agents/pi/__tests__/pi-provider-routing.test.ts
结果:73 passed

pnpm --filter desktop exec vitest run src/main/cindy-brain/__tests__/ghostOauthFlow.test.ts --pool=forks --maxWorkers=1
结果:42 passed

pnpm test:unit:related
结果:
- apps/desktop:PASS
- packages/lizi-mcps:PASS
- packages/maker-core:PASS
- packages/orca-workflow:PASS

pnpm --filter desktop run --if-present typecheck
结果:PASS

pnpm --filter @cindy/maker-core run --if-present typecheck
结果:PASS

git diff --check
结果:PASS

手工验证

不涉及 UI 手工验证。本次在 Windows 11 x64 环境完成专项测试、Desktop 全量相关门禁及类型检查。

未执行的验证

  • 未使用真实 npm:context-mode 安装执行 Pi 冷启动耗时 benchmark。
  • 未在 macOS / Linux 实机验证进程扫描与遗留目录回收;相关平台分支由单元测试覆盖。

风险

风险分类

  • 无已知风险
  • SQLite / migration
  • system prompt
  • 协议兼容
  • 权限 / 安全 / 用户数据
  • 存量插件兼容(批准状态 / 指纹 / manifest 校验 / 安装布局 / 包格式)
  • 原生层 / fingerprint / OTA
  • 跨平台差异
  • 其他:Pi package 缓存失效与本地临时目录回收

影响与回滚

  • 影响范围:
    • Desktop Pi managed-package inspection / snapshot 状态。
    • 本地 Pi 会话 config-home 的 owner marker 与陈旧目录回收。
    • Pi 启动阶段日志。
    • 不影响远程 config-home、.cindy 插件、数据库 schema 或跨端协议。
  • 兼容与安全措施:
    • v1 owner marker 继续兼容读取。
    • installation identity 变化会安全失效旧负缓存并重新检查。
    • 进程身份无法确认、全系统扫描失败或发现存活 Cindy Pi 进程时均 fail-closed 保留目录。
    • 原始 session ID 不落盘,只存 SHA-256 hash。
  • 回滚 / 降级方式:
    • 可整体 revert 本 PR。
    • 回滚后新的缓存状态会被旧版本忽略或重新建立,不影响已安装 Pi 包内容。
    • 临时目录清理失败只会退化为保留目录,不会阻止 Pi 会话启动。

提交前检查

  • 已 review 完整 diff
  • 每个 commit 都带 DCO 签名(git commit -spnpm check:dco 已通过)
  • UI 改动已在「UI 变化」注明引用的设计规范章节(不涉及 UI)
  • 未提交凭证、令牌或授权文件
  • 已确认本次无需新增用户文档
  • 已确认测试结果或说明未执行原因

Signed-off-by: lizzjin <lizzjin37@gmail.com>
@lizzjin
lizzjin requested a review from a team as a code owner August 27, 2026 11:19

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 091b2cdda7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread apps/desktop/src/main/maker-host/pi-package-store.ts
Comment thread apps/desktop/src/main/maker-host/pi-package-store.ts Outdated
@greptile-apps

greptile-apps Bot commented Aug 27, 2026

Copy link
Copy Markdown

Greptile Summary

此 PR 为 Pi 包的确定性超限结果增加跨会话缓存,并为本地会话临时目录引入带进程实例身份的回收机制,同时补充启动阶段耗时日志。

  • 将 package inspection/snapshot 超限状态迁移到按安装身份记录的 v4 持久化格式
  • 在 Pi 启动链路中关联 package、MCP、spawn、RPC ready 与首个模型请求耗时
  • 为本地 config home 增加 v2 owner marker、PID 复用识别和保守的遗留目录扫描
  • 更新 OAuth、跨平台符号链接、包安全及会话清理测试

Confidence Score: 4/5

合并前应修复 Pi 包负缓存无法因深层内容变化而失效的问题,否则已修复的包仍可能跨会话持续被禁用。

新的 installation identity 只覆盖根目录元数据与 package.json,而被缓存的 entries/bytes 超限可以由更深层的资源文件触发,因此同一路径下的内容修复不一定触发重新检查。

Files Needing Attention: apps/desktop/src/main/maker-host/pi-package-store.ts

Important Files Changed

Filename Overview
apps/desktop/src/main/maker-host/pi-package-store.ts 新增确定性超限负缓存和启动计时,但安装身份无法感知 package.json 之外的深层内容修复。
packages/maker-core/src/agents/pi/index.ts 新增本地 config-home owner marker、保守回收、清理重试及 Pi 启动阶段计时,未确认阻塞性缺陷。
apps/desktop/src/main/process-monitor/agent-scan.ts 新增无缓存的全系统进程快照及 Cindy 管理 Pi 进程识别,用于遗留目录回收决策。
apps/desktop/src/main/maker-host/pi-host.ts 将主进程的 fresh process scan 注入 Pi 本地遗留 config-home 回收判定。
packages/maker-core/src/agents/pi/pi-subagent-runs.ts 导出既有进程实例启动时间与存活检查,供 config-home owner 判定复用。

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[列出 Pi 包] --> B[计算安装身份]
  B --> C{命中持久化超限缓存?}
  C -->|是| D[跳过遍历并禁用包]
  C -->|否| E[检查与快照]
  E -->|确定性超限| F[持久化 inspection-limit]
  E -->|成功| G[加载受管资源]
  F --> D
Loading
Prompt To Fix All With AI
### Issue 1
apps/desktop/src/main/maker-host/pi-package-store.ts:401-437
**负缓存身份失效不完整**

当本地包因深层 skills、prompts 或 extensions 的条目数或元数据字节数超限后,仅修改或删除这些深层文件不会改变由根目录 stat 和 `package.json` 构成的 `installationIdentity`。后续会话会继续复用 `inspection-limit` 并跳过遍历,导致已经缩小或修复的包仍持续被禁用,直至执行包 mutation 或修改 `package.json`---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "perf(pi): cache package limits and recla..." | Re-trigger Greptile

Comment thread apps/desktop/src/main/maker-host/pi-package-store.ts
Signed-off-by: lizzjin <lizzjin37@gmail.com>
Signed-off-by: lizzjin <lizzjin37@gmail.com>
@lizzjin

lizzjin commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

补充本轮 review 修复与验证(commit ab5b6ac39):

  • package-scope 的确定性负缓存增加 6 小时有界重试;manifest 不变但深层 skill / prompt / extension 内容缩减后,不会被永久禁用。
  • 缺少 retry metadata 的旧 v4 负缓存会在升级后立即重试一次。
  • aggregate-scope 预算耗尽仍在当前会话 fail-closed,但不再跨会话持久化到后续健康包。
  • 两条 Windows 测试把 fixture 与 spy 参数统一到 canonical realpath namespace,兼容 8.3 / 长路径混用。

新增回归覆盖:

  • 深层 package 内容缩减后的有界恢复;
  • aggregate 跳过不持久化,并证明下一会话健康包恢复;
  • 旧 v4 entry 兼容;
  • Windows canonical path spy 命中。

最新提交前验证:

  • pnpm test:unit:related:Desktop、lizi-mcps、maker-core、orca-workflow 全部 PASS;
  • pnpm --filter desktop run --if-present typecheck:PASS;
  • pnpm --filter @cindy/maker-core run --if-present typecheck:该 package 无 typecheck script,按规则自动跳过;
  • pnpm check:dco:PASS(2 个签名提交,1 个 merge commit 豁免);
  • git diff --check:PASS;
  • 最终 Standards / Spec 双轴复核:无剩余必修 finding。

maker-core 指标补充:未修改 system prompt、prompt 拼接顺序、tool / MCP schema、translator、模型路由、usage 计量或逐 token event loop;影响限于新会话启动资源装配与结构化 timing。未执行真实 npm:context-mode 冷启动 benchmark,macOS / Linux 仍由 CI 与平台测试覆盖。

@MagicLizi

Copy link
Copy Markdown
Contributor

@lizzjin 👋 这个 PR 还有 3 条 review conversation 没 resolve(apps/desktop/src/main/maker-host/pi-package-store.ts),auto-review 因此暂时跳过、没法继续审查 / 合并。

如果你已经按评论改完或回应了,请到对应 thread 上点 Resolve conversation;全部 resolve 后,下一轮 auto-review 会自动重新审查这个 PR。

@MagicLizi MagicLizi added awaiting-discussion 等待维护者讨论(review-pr) touches:core 改动碰到架构核心路径(review-pr 自动维护,仅展示) touches:large-diff 改动量较大(review-pr 自动维护,仅展示) touches:plugin-base 改动碰到插件基座(review-pr 自动维护,仅展示) labels Aug 27, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ab5b6ac39d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/maker-core/src/agents/pi/index.ts Outdated
@MagicLizi

Copy link
Copy Markdown
Contributor

这条 PR 被维护者确认门拦住,需要维护者在 PR 上 Approve 后才能继续合并流程。

当前在拦的是 插件基座ghostOauthFlow.test.ts 的改动会按插件基座规则处理(影响全部已装插件的兼容口径,测试文件不豁免)。另外还有 Pi 包检查结果跨会话缓存、以及本地会话临时目录回收相关的核心路径大改动。

请维护者在讨论 issue 里看范围后,直接在本 PR Approve;若要改,请 Request Changes。作者这边先不用反复请求 review——球不在你手里。

讨论 issue:#3527

Signed-off-by: lizzjin <lizzjin37@gmail.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 40e62cba61

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread apps/desktop/src/main/maker-host/pi-package-store.ts Outdated
Comment thread packages/maker-core/src/agents/pi/index.ts Outdated
Signed-off-by: lizzjin <lizzjin37@gmail.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b65a38b3ef

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

// expensive start-time sample within this sweep; each check still performs
// a fresh signal-0 liveness probe and deletion still re-reads the marker.
const ownerStartTimeMemo: ProcessStartTimeMemo = new Map();
for (const entry of entries) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 为启动期目录回收设置工作预算

run-tmp 因多次崩溃积累了大量带 marker 的目录时,这个无上限循环会在每次 startSession 前串行处理全部条目;最终版本新增的 PID memo 只复用同一 owner 的启动时间探测,无法限制不同 PID 的同步 ps/PowerShell 调用,也无法限制每个目录最多 125 ms 的删除重试及大型目录递归删除,因此恰好在残留最多的用户环境中会长时间冻结 Main 并阻塞所有新任务启动。请给单次 sweep 设置条目或时间预算,并把剩余回收延后执行。弱端性能应作为基线,尤其需要约束 Windows 上的文件系统密集路径。 docs/dev-rules/engineering-conventions.mdL128-L131

Useful? React with 👍 / 👎.

@MagicLizi

Copy link
Copy Markdown
Contributor

@lizzjin 👋 这个 PR 还有 1 条 review conversation 没 resolve(packages/maker-core/src/agents/pi/index.ts),auto-review 因此暂时跳过、没法继续审查 / 合并。

如果你已经按评论改完或回应了,请到对应 thread 上点 Resolve conversation;全部 resolve 后,下一轮 auto-review 会自动重新审查这个 PR。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-discussion 等待维护者讨论(review-pr) touches:core 改动碰到架构核心路径(review-pr 自动维护,仅展示) touches:large-diff 改动量较大(review-pr 自动维护,仅展示) touches:plugin-base 改动碰到插件基座(review-pr 自动维护,仅展示)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

新建 PI 会话会重复 snapshot 大型 Pi 包并复制 rg.exe,新会话变慢40秒且残留堆积

2 participants