Skip to content

Pruned domain_event gaps permanently wedge outbox consumers ("must be checkpointed in sequence order") #75

Open

Description

@mai1015

Summary

Outbox consumers can be permanently wedged when domain_event rows are pruned while a consumer lags behind: the sequence-continuity guard in complete_claimed_event rejects every subsequent claim because the expected cursor + 1 row no longer exists, and there is no recovery path. The consumer then logs a WARN every poll (~1/sec) forever, and silently stops doing its actual job (wake-turn delivery, chat memory indexing, attention projection).

Environment

  • Forge server v0.13.8 (started failing before the 0.13.8 upgrade, while on the previous release)
  • SQLite backend, single instance

Evidence from a production instance

event_consumer_cursor state (read-only inspection of a live forge.db):

consumer_name                        last_sequence   updated_at (UTC)
agent-coordination-outcomes          157339          2026-09-29T13:35:23   <- healthy, at tip
sse-broadcast                        157339          2026-09-29T13:35:23   <- healthy, at tip
agent-wake-turns                     156638          2026-09-28T20:25:47   <- stuck
attention_projection                 156643          2026-09-28T20:25:48   <- stuck
scoped-memory-agent-chat-indexer     156584          2026-09-28T20:25:46   <- stuck

domain_event currently holds only 1,415 scattered rows with MIN(sequence)=1, MAX(sequence)=157339. The ranges below/inside the stuck cursors have been pruned: e.g. SELECT sequence FROM domain_event WHERE sequence BETWEEN 156580 AND 156645 returns 0 rows. So cursor + 1 (156585, 156639, 156644) does not exist and never will.

All three stuck cursors stopped advancing within the same ~2-second window (20:25:46–48 UTC), consistent with a bulk retention/prune pass deleting events the lagging consumers had not yet processed.

Since 2026-09-28 16:48 local, every poll fails:

WARN services::wake_turn_consumer: wake turn delivery poll failed consumer=agent-wake-turns
  error=check constraint failed: domain events must be checkpointed in sequence order
WARN services::agent_chat_memory_consumer: ... consumer=scoped-memory-agent-chat-indexer ...
WARN services::attention_service: Attention projection poll failed ...

122,000+ such lines in ~17 hours. Survives service restarts (cursor state is durable), so it never self-heals.

Root cause

complete_claimed_event (crates/db/src/sqlite/domain_event.rs ~L269):

if input.event_sequence > cursor + 1 {
    return Err(DbError::Check(
        "domain events must be checkpointed in sequence order".to_owned(),
    ));
}

The ordering contract assumes contiguous sequences. Retention prunes rows (creating permanent gaps in the AUTOINCREMENT sequence), so any consumer whose cursor falls behind the prune watermark can never satisfy cursor + 1. The check conflates "consumer skipped an event it should have processed" (a real protocol violation) with "the event was garbage-collected before the consumer got to it" (normal under retention).

Note the healthy consumers (sse-broadcast, agent-coordination-outcomes) do advance across the pruned ranges to the tip, so their claim path effectively tolerates gaps — the invariant is only enforced on some paths.

Impact

  • agent-wake-turns: queued wake turns are never delivered
  • scoped-memory-agent-chat-indexer: agent chat memory is no longer indexed
  • attention_projection: attention projections go stale
  • Log noise: ~3 WARN/sec with no backoff, no escalation

Suggested fixes

  1. Treat gaps as skippable: in complete_claimed_event, allow event_sequence > cursor + 1 when the intermediate sequences are absent from domain_event (or simply set last_sequence = event_sequence — the cursor then means "last handled or pruned-past" sequence).
  2. Prune + cursor advance atomically: when retention deletes rows, bump any consumer cursor inside the pruned range to the prune watermark in the same transaction, ideally leaving a receipt/marker so consumers know events were dropped unprocessed.
  3. Align claim strategy: have lagging consumers claim "next existing sequence > cursor" instead of assuming cursor + 1 exists.
  4. Ops hygiene: exponential backoff on repeated identical poll failures + a distinct "consumer wedged" error state/metric, instead of a permanent 1/sec WARN.

Happy to provide more details from the affected DB (schema dumps, event-type distribution in the pruned range, journal excerpts).

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions