oliver-zehentleitner / maity

hub: chat_unread scan with a bounded aggregate and catch-up at any arrival rate

Sign in to report

tsk-tgh0st6p2zag Done

Follow-up to #1490 (merged 4622961e, tsk-xemkg9x1kwt8). The merged chat_unread scan states two limits (hub/context/events.md 56357b6b): the database aggregate behind the count grows with the unread history, and catch-up is guaranteed only while fewer than SCAN_BUDGET human messages arrive between passes (a moving upto). eyqf saved patch 84383079 for both (the _counts fix and a fixed upto until the read reaches it). Taken from the hub roll-up tsk-b7mtzr8xybp5 (vuur nte-p4fgb3te62d5) as its own card so it does not collide with that PR.

Boundary: guarantees (1) the database work per pass is bounded by the budget, not by the unread history, and (2) every unread human message without a mention eventually causes one nudge at any arrival rate; each with a red/green test, (1) shown on PostgreSQL. The texts in chat_unread.py, docs/api.md §9.2 and events.md 56357b6b are updated to the new guarantees. Does not change when a nudge is sent (10 min, one per read cycle), who receives it, or its payload; exactness against a concurrent redaction stays as accepted in #1490. P3: wording, batch sizes.

Notes

  1. An agent · Nov 8, 2026, 14:20 UTC

    Plan (main 4622961e; the Boundary is the card's):
    (2) Catch-up at any arrival rate, from my saved patch 84383079:

    • _Scan.reached: the bound grows only once a call has seen the end of (mark, upto]. A finished read counts trailing redacted rows as read.
    • The call under the lock only confirms the range the pass found due (extend=False); a scan that had to start over may still extend.
    • Test (both backends): three human messages before each pass, budget 2. The nudge comes on the 2nd pass with exact counts. Red on main (no nudge in 5 passes), red without the gate, red when the locked call extends. Plus: a read ending on redacted rows finishes.

    (1) DB work bounded by the budget, not the unread history. Today _counts (max + redacted counts over seq > mark) and _oldest_human (min over seq > mark) cover the whole unread range on every call. Instead:

    • Newest: the chat's newest message seq, ORDER BY seq DESC LIMIT 1 on (project_id, conversation_id) — an index read, not an aggregate over the range. The human filter stays where the rows are read.
    • Redactions: no count over the range. The scan keeps a cursor over the chat's event_redacted events. Each call reads only those above it (each names the message's seq in its payload). If one names a seq in (mark, scanned_to] that was counted, the read starts over. Every path that redacts a project event (routes/events.py tombstone, jourfixe/redaction.py) emits event_redacted with the message's conversation_id in the same transaction, and seq follows commit order per project (events.py:3-4). So a redaction is seen by the next call; coherence as today, the call under the lock last.
    • _oldest_human goes; the delay is checked on the scan's oldest_at. The read itself is bounded by the budget.
    • Per pass and stay, that leaves: one index read for the newest seq, the new redaction events, at most SCAN_BUDGET rows, _cycle_open (≤ one nudge per participant), _mark (a primary key), _listening.
    • Test (PostgreSQL): EXPLAIN (ANALYZE, FORMAT JSON) on every statement a second sweep runs over an already-read, all-mentioned backlog of 10 and of 400 messages. The event rows the database reads are the same for both. Red on main (they grow with the backlog, as fmzu measured). Plus: a redaction of a counted message after the read restarts it (both backends); the earlier race tests stay.

    Texts: chat_unread.py's module docstring (the two guarantees, without the "may never finish" and "grows with history" clauses), _Scan/_scanned; docs/api.md §9.2 (the budget sentence); hub/context/events.md 56357b6b updated in place with a dated note, since the decision (an incremental in-process scan) stands and only its limits change; changelog.d.
    Unchanged: when a nudge is sent, who gets it, the payload, the concurrent-redaction boundary.
    Question: 56357b6b updated in place, or a new entry that supersedes it?

  2. Lotse · Nov 12, 2026, 15:23 UTC

    Owner decision 2026-11-12 (req-facn95y83yww, option 1, replaces req-wd4g7kccyj94 and req-ww0sds308jyj): yes to both new §9.2 sentences in #1512 (the nudge comes at any arrival rate, about n/2000 passes later; redactions may stay counted only from 2000 between two reads of that chat). The author was removed today; #1512 is CONFLICTING at 9607bf4f. Finishing moves to a new card.