Skip to content

fix: unbounded disk growth in memory generations and heap snapshots #504

Description

@LeXwDeX

Why

深度审计确认(磁盘无限增长):

  1. packages/opencode/src/memory/store.ts:249-266:每次 memory 写事务(commit/updateTopics/markMatched 回写,含 match_count+1 高频 touch)都把全部 topic 复制进新目录 ~/.local/share/opencode/memory/projects/<hash>/generations/<rev>-<uuid>/,旧 generation 永不删除(:267 只删 legacy topics/)。增长 ≈ 每 commit × 全量 topic 集。ADR-0002 明文:GC 需要单独的 retention policy——至今无实现,属已知延期决策落地
  2. packages/opencode/src/cli/heap.ts:28-36:RSS > 2GB 且 OPENCODE_AUTO_HEAP_SNAPSHOT 开启时写 heap snapshot(单文件数百 MB~GB)到 log 目录;armed 棘轮在内存震荡(GC 周期性跌破阈值)下可反复触发,log 目录无任何轮转清理

Scope

  • packages/opencode/src/memory/store.ts(generations 写路径 + 新增 retention 清理)
  • packages/opencode/src/cli/heap.ts(snapshot 写路径 + 旧快照清理)
  • ADR-0002 引用的 retention policy 决策(保留代数上限)

Approach

  • generations:commit 成功后按 revision 降序保留最近 N 代(如 3),其余删除;启动/commit 时顺带清扫孤儿 staging 目录(rename 失败残留)
  • heap snapshot:armed 触发写快照前,清理同前缀旧快照,仅保留最近 M 个(如 2)
  • 清理失败不阻塞主路径(warn 日志),避免把 I/O 抖动放大为功能故障

Acceptance

  • 单测:连续多次 commit 后 generations 目录数量 ≤ N;旧 heap snapshot 被清理仅剩最近 M 个
  • 清理失败路径(注入 rm 失败)不影响 commit/快照主流程
  • 既有 memory/heap 相关测试全绿

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions