kimi-code icon indicating copy to clipboard operation
kimi-code copied to clipboard

fix(telemetry): make CloudAppender shutdown durable

Open Correctover opened this issue 4 months ago • 1 comments

Summary

Fixes #2246.

Gives CloudAppender durable ownership of flush work across threshold, periodic, and shutdown triggers.

Changes

Serial flush lock

  • flush() is now serialized through a single in-flight promise. Concurrent callers (periodic timer, threshold trigger, manual flush, shutdown) wait for the current flush to complete before starting a new one, preventing buffer races where two callers swap the buffer simultaneously and lose a batch.

Deadline-bounded shutdown

  • shutdown(deadlineMs?) provides a deadline-bounded, idempotent close:
    1. Stops the periodic flush timer
    2. Creates an AbortController with the remaining deadline budget
    3. Flushes the remaining buffer (in-flight sends observe the abort signal)
    4. Hands unsent buffered events to durable storage (saveToDisk) so they survive a restart
    5. Replays recoverable v2 spool data (retryDiskEvents) before completing
    6. Marks the appender as shut down; subsequent track() calls are silently ignored

At-least-once delivery

  • Stable event IDs (randomUUID) preserve the server's deduplication key across ambiguous cancellation boundaries.
  • Legacy spool files remain owned by the legacy pipeline — only the v2 failed_*.jsonl namespace is touched.

Tests added

  • Concurrent flush serialization (no event loss)
  • Idempotent shutdown
  • Deadline expiry hands events to disk
  • Spool replay during shutdown
  • track() after shutdown is ignored

Correctover avatar Jul 27 '26 10:07 Correctover

🦋 Changeset detected

Latest commit: d0764ca6218742f2dc78ed33200eab4561568766

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/agent-core-v2 Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Jul 27 '26 10:07 changeset-bot[bot]