fix(v1.1): item 5 — section counter keys off in-section events (2/2 -> 3/2 bug)

A card is decided only when a swipe event carries THIS section's id;
the lifetime swipes.action a second look wears into its section no longer
paints it as pre-decided. progress/undecided, report counts (latest
in-section event per listing) and maybe_exhaust all use the new
section_decided_ids() set; read path only, no writes. Every section_state
item carries decided_this_section; the FE counts server truth unless a
local intent is in flight, and sectDecided is clamped to sectTotal
everywhere (commit + paintSect) so the pill can never read past N/N.

Semantics note: completing a section now means striking EVERY item in it
(returns re-struck on their second look) — the exhaust selfcheck was
updated to strike all items, matching the deck's real flow. Undo-on-a-
return residual (event stays after the DELETE) is the accepted Q5 clamp-
only behavior, covered by a dedicated selfcheck case.
This commit is contained in:
Torres
2026-11-13 13:38:18 -04:00
parent ac613e680a
commit 98d345ceee
4 changed files with 141 additions and 10 deletions

View File

@@ -425,16 +425,44 @@ def section_items(conn, section):
ORDER BY si.position ASC""", (section['id'],)).fetchall()
def section_decided_ids(conn, section):
"""Listing ids with a swipe event stamped in THIS section.
The decided set for progress/report/exhaustion is always these ids —
never the lifetime `swipes.action` verdict (a second-look card enters
its section wearing an old decision; only an in-section event means it
was actually decided here). The join keeps out-of-section stray events
honest (same shape `section_counts` uses).
"""
return {r['listing_id'] for r in conn.execute(
"""SELECT e.listing_id FROM swipe_events e
JOIN section_items si
ON si.section_id = e.section_id
AND si.listing_id = e.listing_id
WHERE e.section_id = ?""", (section['id'],))}
def section_counts(conn, section):
"""Per-action counts + second-look / wildcard engagement for this
section (events stamped with this section_id only)."""
counts = {'likes': 0, 'maybes': 0, 'nopes': 0}
for item in section_items(conn, section):
if item['decision'] == 'like':
# one verdict per listing: the latest in-section event wins (an undo +
# re-swipe within a section must not double-count the report numbers)
latest = {}
for ev in conn.execute(
"""SELECT e.listing_id, e.action FROM swipe_events e
JOIN section_items si
ON si.section_id = e.section_id
AND si.listing_id = e.listing_id
WHERE e.section_id = ?
ORDER BY e.id ASC""", (section['id'],)):
latest[ev['listing_id']] = ev['action']
for action in latest.values():
if action == 'like':
counts['likes'] += 1
elif item['decision'] == 'maybe':
elif action == 'maybe':
counts['maybes'] += 1
elif item['decision'] == 'nope':
elif action == 'nope':
counts['nopes'] += 1
restruck = conn.execute(
"""SELECT COUNT(DISTINCT e.listing_id) AS n FROM swipe_events e
@@ -458,8 +486,9 @@ def maybe_exhaust(conn, section, now):
"""Flip an all-decided open section to 'report' (returns the fresh row)."""
if section['status'] != 'open':
return section
decided_ids = section_decided_ids(conn, section)
items = section_items(conn, section)
if all(it['decision'] is not None for it in items):
if items and not [it for it in items if it['id'] not in decided_ids]:
close_section(conn, section, now, status='report')
return conn.execute('SELECT * FROM sections WHERE id=?',
(section['id'],)).fetchone()
@@ -470,7 +499,8 @@ def read_section(conn, section, now):
"""Meta + items + progress + exhausted for GET /api/section."""
section = maybe_exhaust(conn, section, now)
items = section_items(conn, section)
decided = sum(1 for it in items if it['decision'] is not None)
decided_ids = section_decided_ids(conn, section)
decided = sum(1 for it in items if it['id'] in decided_ids)
return {
'section': {
'id': section['id'],