Skip to content

save_replay copies up to an hour of segments synchronously and writes the footage 3x #293

Closed

Description

@TMHSDigital

Summary

copyShared uses Buffer.alloc + readSync + writeFileSync per segment on the server's event loop (up to ~1800 two-second segments), then the data is rewritten by the joined.mp4 concat and again by the cut.

Where

  • src/utils/replay.ts:226-242 (copyShared)
  • src/utils/replay.ts:289-295
  • src/utils/replay.ts:319-342
  • src/utils/replay.ts:43-48 (REPLAY_SEGMENT_SEC, MAX_REPLAY_SEC)

Details

The code comment says "copying takes milliseconds", which only holds for short buffers.

Impact

Saving a long buffer (e.g. 3600 s at ~5 Mbps, ~2.2 GB) blocks the MCP event loop for seconds: tool calls, progress notifications and recorder child handlers stall; disk writes are ~3x the clip size.

Steps to reproduce

  1. start_recording { replayBufferSec: 3600 }, let it fill
  2. save_replay { lastSec: 3600 } and observe the server stop responding during the snapshot

Suggested fix

Copy with async streams (createReadStream/pipeline, still opened for shared read). Drop the joined.mp4 intermediate by cutting straight from the concat list (inpoint/outpoint in the list or -ss on the concat demuxer).

Acceptance criteria

  • No synchronous bulk IO on the event loop in save_replay
  • One fewer full rewrite of the footage

Getting started

  • Relevant files: src/utils/replay.ts
  • How to run tests: npm run build && npm test (also npm run typecheck and npm run lint)
  • Related docs: CONTRIBUTING.md, AGENTS.md

Filed by repo-audit/full-audit at f2fda28. Severity: medium. Effort: M. audit-key: performance|src/utils/replay.ts|copyShared

Activity

  1. added
    performance
    on Nov 9, 2026
  2. TMHSDigital commented on Nov 10, 2026

    @TMHSDigital
    OwnerAuthor

    Partly fixed in a6cfc9b: the snapshot copy (copyShared) now streams each segment asynchronously (a shared read up to its current size, abortable), so save_replay no longer does synchronous bulk IO on the event loop. Verified with a new copy test and the replay integration and tool-layer tests (75 passed).

    Still open: dropping the joined.mp4 intermediate. The cut seeks in that mp4 because MPEG-TS seeking is not exact, so cutting straight from the concat list (inpoint, or -ss on the concat demuxer) needs a frame-accuracy check against the current output before it replaces the join.

  3. TMHSDigital commented on Nov 16, 2026

    @TMHSDigital
    OwnerAuthor

    Closing. The impact (synchronous bulk IO blocking the server for seconds) was fixed in a6cfc9b, where segments are streamed asynchronously. I tested the remaining idea, dropping the joined.mp4 intermediate, against the current output on real 2 s MPEG-TS segments (keyframe each second), comparing durations and decoded frames at several points:

    • inpoint on the first segment: wrong for a mid-segment start (about 0.37 s of video lost, frames offset).
    • -ss on the concat demuxer: identical for 5.5 s and 7.3 s cuts, but a 9.9 s cut lost about 0.7 s of video. The total also has to come from summed segment durations (ffprobe reports N/A for a concat list), which is off by a few ms and shifts short cuts.
      The join-then-cut path is what makes the cut exact, so it stays on purpose. The cost is one extra write of at most the requested window.
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

    performancepriority: medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions