Skip to content

fix: replace per-instance disk log with in-memory ring buffer - #41

Merged
linletian merged 5 commits into
developfrom
fix/disk-massive-write-issue
Jun 20, 2026
Merged

fix: replace per-instance disk log with in-memory ring buffer#41
linletian merged 5 commits into
developfrom
fix/disk-massive-write-issue

Conversation

@linletian

Copy link
Copy Markdown
Owner

Summary

Eliminates massive disk write amplification caused by per-instance files, and enhances resource monitoring with daemon stats and ring buffer visibility.

Changes

  1. In-memory ring buffer (678490f) — replaces per-instance disk log files with process-in-memory bounded ring buffers (16–256 MB per instance, adaptive to system RAM). Eliminates ~280 MB+ disk writes caused by the old append + truncate cycle under heavy TUI workloads.

  2. Daemon resource monitoring (f1ed5ab) — stats API now includes daemon CPU/memory in global totals. Per-instance stats expose ring buffer used/cap. UI shows dedicated mw daemon row and per-instance buffer annotations.

  3. UI translation fix (2befdd1) — translate subprocess disclaimer to English.

  4. Reduce polling interval (99349d7) — resource monitor polling from 1s to 2s to halve per-second process.NewProcess() syscall overhead.

Risk

  • Breaking schema change: log_path field removed from state.json. Old files load safely (unknown fields ignored by JSON decoder). External tools reading state.json should drop the log_path field.
  • All API changes are additive (new fields only), fully backward compatible.

Eliminate ~100,000× disk write amplification caused by the old
enforceMaxLogSize loop (read 10MB + truncate + write 10MB per 1024-byte
PTY chunk). Under TUI redraw workloads, this could generate 280MB+ of
accumulated disk writes in the data directory.

Changes:
- Add RingBuffer (internal/instance/logbuf.go): bounded, thread-safe,
  in-memory ring buffer. Backing slice pre-allocated eagerly. Hot-path
  optimized with WriteString (no []byte(chunk) copy) and atomic pointer
  access (no stateMu per chunk).
- Add adaptive sizing (internal/instance/sizing.go): per-instance cap
  clamp(available/16, 16MB, 256MB), default 32MB. Global budget capped
  at 25% system RAM. User override via config.GlobalConfig.LogBufferBytes.
- Refactor Manager (internal/instance/manager.go): pumpLogs writes to
  ring buffer instead of disk. Tail/ReadSince read from buffer. Removed
  enforceMaxLogSize, logPathByID, and all LogPath references.
- Remove LogPath from ManagedInstance (internal/store/state.go).
  Old state.json files with log_path load cleanly (JSON silently ignores).
- Add PurgeOrphanLogFiles() called at daemon startup to clean up dead
  .log artifacts left by pre-buffer code.
- HTTP/MCP: budget-exceeded returns 503 with structured
  log_buffer_budget_exceeded body. UI shows dedicated modal.
- Test coverage: RingBuffer (15 tests), sizing/budget (9 tests),
  manager budget integration, backward-compat state loading.
- Docs: PRD, ARCHITECTURE §4.1, API (error shape + cursor semantics),
  CHANGELOG Unreleased section, and detailed implementation plan.

BREAKING: state.json schema — log_path field removed. Old files
continue to load; external tooling should drop log_path dependency.
- Stats API now includes daemon_cpu_percent/daemon_memory_bytes in global totals
- Per-instance stats expose memory_buffer_bytes (used) and memory_buffer_cap_bytes (cap)
- Collector refactored: system calls moved outside lock, 3-phase pipeline
- UI adds mw daemon row and buffer used/cap annotation per instance
- Add .omo/ to .gitignore for OpenCode internal session data
Halves the per-second process.NewProcess() syscall storm when the
resource monitor modal is open, reducing daemon CPU overhead.
@linletian
linletian merged commit c72827c into develop Jun 20, 2026
2 checks passed
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.

1 participant