Skip to content

usage_fold skips usage that shares its watermark's second #1030

Closed

Description

@zlareb1

Component

Background workers

Describe what's wrong

usage_fold can leave recall_count and evidence_count short of the usage that arrived during the pass. It does not double-count: the counters it writes are absolute totals, applied with greatest.

The pass groups loom.memory_usage_totals and keeps groups whose max(bumped_at) is greater than the watermark. bumped_at is a DateTime, so it has one-second resolution. After a batch that is not full, the watermark is set to that max. The next pass's predicate is bumped > watermark. The one-second step back runs only when the batch is full (max_memories_per_pass, default 1000).

A usage row that becomes visible after the read, with bumped_at in that same second, never matches again. Its counts stay off loom.memory until some later event bumps the memory past the watermark.

The same pass applies the totals with a lightweight UPDATE of loom.memory. A version inserted after that update, copied from a head read before it, keeps the old counters. That version wins under FINAL, and the watermark does not bring the memory back until bumped_at moves.

Run on commit 5fbc62c. One totals row with bumped_at 2026-01-02 03:04:05 and max_memories_per_pass 1000. _fold_batch returned a non-full batch and set the watermark to 2026-01-02T03:04:05+00:00. ClickHouse was not queried.

Does it reproduce on the latest release or main?

Yes

How to reproduce

Default worker settings (LOOM_USAGE_FOLD_ENABLED=true, the 1000-row cap, the 300s interval).

  1. Note the process watermark (usage_fold logs it).
  2. Insert two memory_usage_events for one memory in the same second. Let the first become visible to the fold's SELECT and hold the second until that SELECT has returned.
  3. Let the pass finish. The watermark equals that second, because the batch is not full.
  4. Release the second event so it lands with the same bumped_at.
  5. Run another pass and read recall_count / evidence_count on loom.memory.

The second event is absent from the counters. A pass that folds 1000 memories steps the watermark back one second and will see it. A shorter pass will not.

Expected behavior

Usage that commits in a second the fold has not fully observed is still folded. The full-batch path already re-reads the boundary second because bumped_at is one-second resolution. A short batch needs the same overlap, or the watermark needs to stay behind rows that were not visible yet. Re-folding the same totals stays idempotent because the write is greatest of an absolute total.

Environment

Source: ClickHouse/loom main, commit 5fbc62c21a, package version 1.3.0
Python: 3.14.6
Platform: macOS arm64
LOOM_STORAGE_AUTHORITY: clickhouse (the default)
ClickHouse: not queried

How this was found

An AI tool found it, and I have not reproduced it myself

Additional context

The watermark value above is from _fold_batch with a stub client, one totals row, and an empty identity read. No second event was inserted into ClickHouse.

Redaction

  • I removed tokens, keys, passwords and personal or customer data from everything above.

Activity

  1. added
    bug
    on Nov 20, 2026
  2. self-assigned this
    on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions