Skip to content

daemon: serialize transcript metric events incrementally - #2246

Draft
scouredimage wants to merge 2 commits into
git-ai-project:mainfrom
scouredimage:fix/2244-2-metrics-single-copy
Draft

daemon: serialize transcript metric events incrementally#2246
scouredimage wants to merge 2 commits into
git-ai-project:mainfrom
scouredimage:fix/2244-2-metrics-single-copy

Conversation

@scouredimage

@scouredimage scouredimage commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Part 2/7 of the #2244 stack.

store_metrics_in_db materialized a Vec<String> of every re-serialized event while the full Vec<MetricEvent> (each holding the redacted JSON tree) was still alive - two complete copies of a batch resident at once. The stream worker now serializes each event as it is built and drops the tree immediately.

Behavior note: previously one unserializable event failed the whole batch; the stream-worker path now drops the failing event with a warning and persists the rest (best-effort diagnostics).

Stacked on #2245 - review the last commit only for this part's diff.

Stack

  1. streams: cap JSONL line size and transcript batch bytes #2245 - streams: cap JSONL line size and transcript batch bytes
  2. daemon: serialize transcript metric events incrementally #2246 - daemon: serialize transcript metric events incrementally
  3. daemon: cap control/trace socket line size #2247 - daemon: cap control/trace socket line size
  4. daemon: advance stream watermark before batch processing #2248 - daemon: advance stream watermark before batch processing
  5. daemon: memory watchdog decides on current RSS, not lifetime peak #2249 - daemon: memory watchdog decides on current RSS, not lifetime peak
  6. daemon: never defer self-restart while processing is stalled #2250 - daemon: never defer self-restart while processing is stalled
  7. streams: cap the initial backfill of first-seen stream files #2251 - streams: cap the initial backfill of first-seen stream files

Each PR targets main; the incremental diff of part N is its last commit. Root-cause analysis, production evidence, and the repro live in #2244.

Verification

  • Deterministic repro from Daemon RSS balloons on large agent transcript sweeps; watchdog abort loops from watermark 0 and blocks traced git (v1.6.24) #2244 (315 MB synthetic Cursor transcript + one commit, 384 MB
    daemon memory limit): unpatched v1.6.24 aborts in ~1 s and re-aborts after respawn
    (watermark 0); with the full stack the daemon survives, peak RSS 76 MB vs 589 MB,
    zero memory emergencies, traced-git probes at 0.05-0.07 s throughout.
  • cargo fmt --check clean; cargo test --lib: 2,401 passed. Three failures
    (commands::upgrade::...pending_update, daemon::...conflict_resolution_note_read_errors...,
    git::authorship_traversal::...ai_touched_files...) reproduce identically on clean main
    in this environment - pre-existing, unrelated.

Transcript batches were bounded by event count only (1000), and
read_jsonl_line buffered lines of unbounded length. A transcript whose
events embed file contents (normal for agent tool results) could put
hundreds of MB into a single batch, which downstream redaction and
metrics conversion amplify several times over -- ballooning daemon RSS
past the memory watchdog within seconds (git-ai-project#2244).

- read_jsonl_line: cap a single line at MAX_JSONL_LINE_BYTES (8 MiB).
  The read is byte-based (read_until), NOT read_line: the cap can
  slice a multi-byte character, and read_line's UTF-8 validation would
  return InvalidData instead of classifying the line as Oversized --
  wedging the stream at a fixed watermark. Oversized lines are skipped
  without being buffered (skip_until); callers advance their watermark
  past them via the new JsonlLineState::Oversized state. Non-UTF-8
  content within the cap keeps read_line's InvalidData contract.
- All JSONL byte-offset stream parsers (claude, codex, copilot,
  cursor, droid, gemini, pi, windsurf): stop a batch early once
  MAX_BATCH_BYTES (8 MiB) of raw JSON has been accepted; remaining
  events arrive in later batches.

Not covered here: amp, continue_cli, and opencode parse whole files
into a DOM (serde_json::from_reader) and need a separate treatment --
called out in git-ai-project#2244 as follow-up.

Part 1/7 of the git-ai-project#2244 fix stack.
store_metrics_in_db materialized a Vec<String> of every re-serialized
event while the full Vec<MetricEvent> (each holding the redacted JSON
tree) was still alive -- two complete copies of a transcript batch
resident at once (git-ai-project#2244).

The stream worker now serializes each MetricEvent as it is built and
drops the tree immediately; persistence takes the pre-serialized rows
via the new persist_metric_jsons_blocking. Existing callers of
persist_metrics_blocking are unchanged (it now delegates to the same
insert path).

Behavior note: previously one unserializable event failed the whole
batch (all-or-nothing insert); the stream-worker path now drops the
failing event with a warning and persists the rest. These are
best-effort diagnostics, and partial persistence beats losing the
batch.

Part 2/7 of the git-ai-project#2244 fix stack (stacked on 1/7).
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