Skip to content

perf(mapping-store): bound concurrent startup I/O reading (Refs #595) - #609

Closed
Tericerice wants to merge 4 commits into
BytePioneer-AI:mainfrom
Tericerice:fix/595-mapping-store-startup-performance
Closed

Tericerice wants to merge 4 commits into
BytePioneer-AI:mainfrom
Tericerice:fix/595-mapping-store-startup-performance

Conversation

@Tericerice

@Tericerice Tericerice commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

目的 (Purpose)

缓解在机械硬盘(HDD)或较慢存储介质上,当历史会话积累较多(如 700+ 会话)时,MappingStore.initialize() 因串行读取每个线程文件及元数据导致累积耗时过长甚至可能超时的问题(#595 部分缓解)。

关联 Issue (Linked Issues)

Refs #595(部分缓解)

实现范围 (Scope of Changes)

  1. 有限并发读取 (Bounded Concurrency I/O):
    • 增加 mapConcurrent 异步工作池,在 MappingStore.initialize() 读取线程、孤儿元数据与委托记录时引入可配置的并发上限(默认 16,options.concurrency 可定制,严格校验有限正整数),消除原串行 for ... await 的 I/O 寻道延迟累积。
    • 错误处理严格采用独立 failed 标志结合 await Promise.allSettled(workers),确保遇到首个读取异常时终止后续任务派发,并等待所有在途 worker 安全退出后再释放锁与向上抛错。
  2. 纯粹非侵入式演进 (Zero Data Semantic Mutation):
    • 本 PR 只做有上限的并发读取,完全保留原有的全量加载与内存索引模型,不改动记录实体结构、唯一性校验与 Section Placements 规则。
    • 关于已归档懒加载的说明:Issue [Bug] MappingStore loads every thread record (incl. archived) one file at a time on startup — 32s timeout / crash loop on HDD #595 原本期望的已归档线程懒加载及外部索引机制,在两轮独立双路复核(Codex & Pi)中均发现存在数据损坏、删除复活及竞态风险。为确保数据绝对安全与零回归,本 PR 决定将懒加载方案彻底撤回,解耦留作后续独立 Issue 深入设计与讨论。
  3. 提交与复核范围说明:
    • 两路复核审定的核心基准提交为 7ef776f64a1a033ed8b980040f02175eb08b8d70;
    • 最终提交 dcd7999da9c9b56d607e4d590db80d736935416e 系根据复核建议顺手追加的辅助性优化(增加独立布尔 failed 标志防范 falsy rejection、补全测试以及清理临时残留)。

验证与性能实测 (Validation & Benchmarks)

  1. 自动测试:
    • 新增 packages/mapping-store/test/startup-performance.test.ts,验证并发上限限制、非法并发参数归一化及异常路径 worker 安全排空。
    • packages/mapping-store 74 个单元测试全部通过。
    • 类型与规范检查:npm run typecheck、npm run lint、tools/check-boundaries.mjs 全部 0 错误。
  2. 实测性能数据说明:

后续工作 (Follow-up Work)

根据 Pi r3 与 Codex r3 复核建议,以下列入后续演进:

  1. 已归档线程的持久化快照 / SQLite 索引懒加载(彻底解决机械盘大量历史归档文件的读取瓶颈,需建立完善的自愈与代次保护);
  2. #cleanupResidue() 改造为有界并发清理;
  3. initialize() 内部记录列表增加按 ID 稳定排序契约(避免并发读取带来的微弱顺序不确定性);
  4. mapConcurrent 补充泛型自保与独立微工具包沉淀。

🤖 Generated with Claude Code

…hived threads (BytePioneer-AI#595)

- Bound file reading concurrency during MappingStore.initialize() with configurable concurrency limit (default 16)
- Persist archived thread index (archived-threads.json) to bypass reading archived files on startup
- Lazily load archived threads and their metadata on demand
- Preserve orphaned metadata cleanup semantics without deleting archived thread metadata
- Add startup performance and lazy loading unit tests

Fixes BytePioneer-AI#595

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 25, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Tericerice and others added 2 commits October 26, 2026 05:48
…I#595

- Ensure all records (active and archived stubs) are present in memory for full session tree and uniqueness resolution (M6, B1, M3)
- Retain createRequestId and nativeSessionKey indexes across all threads (M3)
- Deduplicate in-flight lazy detail loading with Promise sharing (B2, M2)
- Revert app-server-host list pre-filtering to preserve subagent session tree parent resolution (M6)
- Handle mapConcurrent worker errors cleanly with allSettled before releasing locks (M5)
- Clean up archived-threads.json.tmp-* residue in store root (Minor 2)
- Enforce finite concurrency validation (Minor 1)
- Add comprehensive regression tests for uniqueness, provisional conflict, and in-flight deduplication

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…r-AI#595)

- Introduce mapConcurrent worker pool in MappingStore.initialize() for bounded concurrent reading of thread files, orphaned metadata, and delegations (default concurrency 16, configurable via options.concurrency)
- Safely drain in-flight workers via Promise.allSettled on error before releasing the store lock
- Strictly validate concurrency input to ensure finite, positive integers
- Retain 100% of existing thread and turn mapping semantics without altering in-memory record structures or index contracts
- Add unit tests for bounded concurrency, concurrency option validation, and safe error handling

Fixes BytePioneer-AI#595

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Tericerice Tericerice changed the title perf(mapping-store): bound concurrent startup I/O and lazily load archived threads (#595) perf(mapping-store): bound concurrent startup I/O reading (#595) Oct 25, 2026
…ests (BytePioneer-AI#595)

- Use explicit boolean failed sentinel in mapConcurrent to guard against falsy rejections
- Clean up archived-threads.json* residue in store root
- Add integration test asserting failure propagation and store lock release on initialization error

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Tericerice Tericerice changed the title perf(mapping-store): bound concurrent startup I/O reading (#595) perf(mapping-store): bound concurrent startup I/O reading (Refs #595) Oct 25, 2026
@BytePioneer-AI

Copy link
Copy Markdown
Owner

感谢贡献!我们已关注到这个 PR,会尽快安排审查。
Thanks for your contribution! This PR is on our radar, and we’ll review it as soon as possible.

@BytePioneer-AI

Copy link
Copy Markdown
Owner

感谢你的贡献!想确认一下,这个 PR 当前是否已经准备好转为正式 PR(Ready for review),进入评审?

如果已经准备好,欢迎取消草稿状态;如果仍在开发、存在阻塞或希望暂缓,也欢迎简单说明当前进展,方便我们安排后续评审。谢谢!

@iruriQ

iruriQ commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

感谢贡献!本草稿已停滞一段时间且未转 Ready,这边先致谢关闭;同时我们已保留“启动时 MappingStore.initialize() 改有界并发读盘”这一有效优化点,后续可基于最新 main 轻量重做!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants